Commit fd8d4f86 authored by Kairui Song's avatar Kairui Song Committed by Andrew Morton
Browse files

mm, swap: cleanup swap cache API and add kerneldoc

In preparation for replacing the swap cache backend with the swap table,
clean up and add proper kernel doc for all swap cache APIs.  Now all swap
cache APIs are well-defined with consistent names.

No feature change, only renaming and documenting.

Link: https://lkml.kernel.org/r/20250916160100.31545-9-ryncsn@gmail.com


Signed-off-by: default avatarKairui Song <kasong@tencent.com>
Acked-by: default avatarChris Li <chrisl@kernel.org>
Reviewed-by: default avatarBarry Song <baohua@kernel.org>
Reviewed-by: default avatarBaolin Wang <baolin.wang@linux.alibaba.com>
Acked-by: default avatarDavid Hildenbrand <david@redhat.com>
Suggested-by: default avatarChris Li <chrisl@kernel.org>
Cc: Baoquan He <bhe@redhat.com>
Cc: "Huang, Ying" <ying.huang@linux.alibaba.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Kemeng Shi <shikemeng@huaweicloud.com>
Cc: kernel test robot <oliver.sang@intel.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Nhat Pham <nphamcs@gmail.com>
Cc: Yosry Ahmed <yosryahmed@google.com>
Cc: Zi Yan <ziy@nvidia.com>
Cc: SeongJae Park <sj@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 0fcf8ef4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -4525,7 +4525,7 @@ static void filemap_cachestat(struct address_space *mapping,
				 * invalidation, so there might not be
				 * a shadow in the swapcache (yet).
				 */
				shadow = get_shadow_from_swap_cache(swp);
				shadow = swap_cache_get_shadow(swp);
				if (!shadow)
					goto resched;
			}
+1 −1
Original line number Diff line number Diff line
@@ -1127,7 +1127,7 @@ static int me_swapcache_clean(struct page_state *ps, struct page *p)
	struct folio *folio = page_folio(p);
	int ret;

	delete_from_swap_cache(folio);
	swap_cache_del_folio(folio);

	ret = delete_from_lru_cache(folio) ? MF_FAILED : MF_RECOVERED;
	folio_unlock(folio);
+1 −1
Original line number Diff line number Diff line
@@ -4699,7 +4699,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)

				memcg1_swapin(entry, nr_pages);

				shadow = get_shadow_from_swap_cache(entry);
				shadow = swap_cache_get_shadow(entry);
				if (shadow)
					workingset_refault(folio, shadow);

+5 −5
Original line number Diff line number Diff line
@@ -1661,13 +1661,13 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug,
		}

		/*
		 * The delete_from_swap_cache() below could be left for
		 * The swap_cache_del_folio() below could be left for
		 * shrink_folio_list()'s folio_free_swap() to dispose of;
		 * but I'm a little nervous about letting this folio out of
		 * shmem_writeout() in a hybrid half-tmpfs-half-swap state
		 * e.g. folio_mapping(folio) might give an unexpected answer.
		 */
		delete_from_swap_cache(folio);
		swap_cache_del_folio(folio);
		goto redirty;
	}
	if (nr_pages > 1)
@@ -2045,7 +2045,7 @@ static struct folio *shmem_swap_alloc_folio(struct inode *inode,
	new->swap = entry;

	memcg1_swapin(entry, nr_pages);
	shadow = get_shadow_from_swap_cache(entry);
	shadow = swap_cache_get_shadow(entry);
	if (shadow)
		workingset_refault(new, shadow);
	folio_add_lru(new);
@@ -2183,7 +2183,7 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index,
	nr_pages = folio_nr_pages(folio);
	folio_wait_writeback(folio);
	if (!skip_swapcache)
		delete_from_swap_cache(folio);
		swap_cache_del_folio(folio);
	/*
	 * Don't treat swapin error folio as alloced. Otherwise inode->i_blocks
	 * won't be 0 when inode is released and thus trigger WARN_ON(i_blocks)
@@ -2422,7 +2422,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
		folio->swap.val = 0;
		swapcache_clear(si, swap, nr_pages);
	} else {
		delete_from_swap_cache(folio);
		swap_cache_del_folio(folio);
	}
	folio_mark_dirty(folio);
	swap_free_nr(swap, nr_pages);
+27 −21
Original line number Diff line number Diff line
@@ -167,17 +167,29 @@ static inline bool folio_matches_swap_entry(const struct folio *folio,
	return folio_entry.val == round_down(entry.val, nr_pages);
}

void show_swap_cache_info(void);
void *get_shadow_from_swap_cache(swp_entry_t entry);
int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
		      gfp_t gfp, void **shadowp);
void __delete_from_swap_cache(struct folio *folio,
/*
 * All swap cache helpers below require the caller to ensure the swap entries
 * used are valid and stablize the device by any of the following ways:
 * - Hold a reference by get_swap_device(): this ensures a single entry is
 *   valid and increases the swap device's refcount.
 * - Locking a folio in the swap cache: this ensures the folio's swap entries
 *   are valid and pinned, also implies reference to the device.
 * - Locking anything referencing the swap entry: e.g. PTL that protects
 *   swap entries in the page table, similar to locking swap cache folio.
 * - See the comment of get_swap_device() for more complex usage.
 */
struct folio *swap_cache_get_folio(swp_entry_t entry);
void *swap_cache_get_shadow(swp_entry_t entry);
int swap_cache_add_folio(struct folio *folio, swp_entry_t entry,
			 gfp_t gfp, void **shadow);
void swap_cache_del_folio(struct folio *folio);
void __swap_cache_del_folio(struct folio *folio,
			    swp_entry_t entry, void *shadow);
void delete_from_swap_cache(struct folio *folio);
void clear_shadow_from_swap_cache(int type, unsigned long begin,
void swap_cache_clear_shadow(int type, unsigned long begin,
			     unsigned long end);

void show_swap_cache_info(void);
void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry, int nr);
struct folio *swap_cache_get_folio(swp_entry_t entry);
struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
		struct vm_area_struct *vma, unsigned long addr,
		struct swap_iocb **plug);
@@ -305,28 +317,22 @@ static inline struct folio *swap_cache_get_folio(swp_entry_t entry)
	return NULL;
}

static inline void *get_shadow_from_swap_cache(swp_entry_t entry)
static inline void *swap_cache_get_shadow(swp_entry_t entry)
{
	return NULL;
}

static inline int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
					gfp_t gfp_mask, void **shadowp)
{
	return -1;
}

static inline void __delete_from_swap_cache(struct folio *folio,
					swp_entry_t entry, void *shadow)
static inline int swap_cache_add_folio(swp_entry_t entry, struct folio *folio,
				       gfp_t gfp, void **shadow)
{
	return -EINVAL;
}

static inline void delete_from_swap_cache(struct folio *folio)
static inline void swap_cache_del_folio(struct folio *folio)
{
}

static inline void clear_shadow_from_swap_cache(int type, unsigned long begin,
				unsigned long end)
static inline void __swap_cache_del_folio(struct folio *folio, swp_entry_t entry, void *shadow)
{
}

Loading