The patch titled Subject: swap: avoid holding swap reference in swap_cache_get_folio has been added to the -mm mm-unstable branch. Its filename is swap-avoid-holding-swap-reference-in-swap_cache_get_folio.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/swap-avoid-holding-swap-reference-in-swap_cache_get_folio.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via the mm-everything branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there every 2-3 working days ------------------------------------------------------ From: Kairui Song <kasong@xxxxxxxxxxx> Subject: swap: avoid holding swap reference in swap_cache_get_folio Date: Tue, 20 Dec 2022 02:58:40 +0800 All its callers either already hold a reference to, or lock the swap device while calling this function. There is only one exception in shmem_swapin_folio, just make this caller also hold a reference of the swap device, so this helper can be simplified and saves a few cycles. This also provides finer control of error handling in shmem_swapin_folio, on race (with swap off), it can just try again. For invalid swap entry, it can fail with a proper error code. Link: https://lkml.kernel.org/r/20221219185840.25441-5-ryncsn@xxxxxxxxx Signed-off-by: Kairui Song <kasong@xxxxxxxxxxx> Cc: David Hildenbrand <david@xxxxxxxxxx> Cc: "Huang, Ying" <ying.huang@xxxxxxxxx> Cc: Hugh Dickins <hughd@xxxxxxxxxx> Cc: Matthew Wilcox (Oracle) <willy@xxxxxxxxxxxxx> Cc: Miaohe Lin <linmiaohe@xxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- mm/shmem.c | 11 +++++++++++ mm/swap_state.c | 8 ++------ 2 files changed, 13 insertions(+), 6 deletions(-) --- a/mm/shmem.c~swap-avoid-holding-swap-reference-in-swap_cache_get_folio +++ a/mm/shmem.c @@ -1741,6 +1741,7 @@ static int shmem_swapin_folio(struct ino struct address_space *mapping = inode->i_mapping; struct shmem_inode_info *info = SHMEM_I(inode); struct mm_struct *charge_mm = vma ? vma->vm_mm : NULL; + struct swap_info_struct *si; struct folio *folio = NULL; swp_entry_t swap; int error; @@ -1752,6 +1753,14 @@ static int shmem_swapin_folio(struct ino if (is_swapin_error_entry(swap)) return -EIO; + si = get_swap_device(swap); + if (!si) { + if (!shmem_confirm_swap(mapping, index, swap)) + return -EEXIST; + else + return -EINVAL; + } + /* Look it up and read it in.. */ folio = swap_cache_get_folio(swap, NULL, 0); if (!folio) { @@ -1812,6 +1821,7 @@ static int shmem_swapin_folio(struct ino delete_from_swap_cache(folio); folio_mark_dirty(folio); swap_free(swap); + put_swap_device(si); *foliop = folio; return 0; @@ -1825,6 +1835,7 @@ unlock: folio_unlock(folio); folio_put(folio); } + put_swap_device(si); return error; } --- a/mm/swap_state.c~swap-avoid-holding-swap-reference-in-swap_cache_get_folio +++ a/mm/swap_state.c @@ -321,19 +321,15 @@ static inline bool swap_use_vma_readahea * unlocked and with its refcount incremented - we rely on the kernel * lock getting page table operations atomic even if we drop the folio * lock before returning. + * + * Caller must lock the swap device or hold a reference to keep it valid. */ struct folio *swap_cache_get_folio(swp_entry_t entry, struct vm_area_struct *vma, unsigned long addr) { struct folio *folio; - struct swap_info_struct *si; - si = get_swap_device(entry); - if (!si) - return NULL; folio = filemap_get_folio(swap_address_space(entry), swp_offset(entry)); - put_swap_device(si); - if (folio) { bool vma_ra = swap_use_vma_readahead(); bool readahead; _ Patches currently in -mm which might be from kasong@xxxxxxxxxxx are swapfile-get-rid-of-volatile-and-avoid-redundant-read.patch swap-avoid-a-redundant-pte-map-if-ra-window-is-1.patch swap-fold-swap_ra_clamp_pfn-into-swap_ra_info.patch swap-avoid-holding-swap-reference-in-swap_cache_get_folio.patch