Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

swap: avoid holding swap reference in swap_cache_get_folio

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@gmail.com
Signed-off-by: Kairui Song <kasong@tencent.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: "Huang, Ying" <ying.huang@intel.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Miaohe Lin <linmiaohe@huawei.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Kairui Song and committed by
Andrew Morton
cbc2bd98 16ba391e

+13 -6
+11
mm/shmem.c
··· 1739 1739 struct address_space *mapping = inode->i_mapping; 1740 1740 struct shmem_inode_info *info = SHMEM_I(inode); 1741 1741 struct mm_struct *charge_mm = vma ? vma->vm_mm : NULL; 1742 + struct swap_info_struct *si; 1742 1743 struct folio *folio = NULL; 1743 1744 swp_entry_t swap; 1744 1745 int error; ··· 1750 1749 1751 1750 if (is_swapin_error_entry(swap)) 1752 1751 return -EIO; 1752 + 1753 + si = get_swap_device(swap); 1754 + if (!si) { 1755 + if (!shmem_confirm_swap(mapping, index, swap)) 1756 + return -EEXIST; 1757 + else 1758 + return -EINVAL; 1759 + } 1753 1760 1754 1761 /* Look it up and read it in.. */ 1755 1762 folio = swap_cache_get_folio(swap, NULL, 0); ··· 1819 1810 delete_from_swap_cache(folio); 1820 1811 folio_mark_dirty(folio); 1821 1812 swap_free(swap); 1813 + put_swap_device(si); 1822 1814 1823 1815 *foliop = folio; 1824 1816 return 0; ··· 1833 1823 folio_unlock(folio); 1834 1824 folio_put(folio); 1835 1825 } 1826 + put_swap_device(si); 1836 1827 1837 1828 return error; 1838 1829 }
+2 -6
mm/swap_state.c
··· 321 321 * unlocked and with its refcount incremented - we rely on the kernel 322 322 * lock getting page table operations atomic even if we drop the folio 323 323 * lock before returning. 324 + * 325 + * Caller must lock the swap device or hold a reference to keep it valid. 324 326 */ 325 327 struct folio *swap_cache_get_folio(swp_entry_t entry, 326 328 struct vm_area_struct *vma, unsigned long addr) 327 329 { 328 330 struct folio *folio; 329 - struct swap_info_struct *si; 330 331 331 - si = get_swap_device(entry); 332 - if (!si) 333 - return NULL; 334 332 folio = filemap_get_folio(swap_address_space(entry), swp_offset(entry)); 335 - put_swap_device(si); 336 - 337 333 if (folio) { 338 334 bool vma_ra = swap_use_vma_readahead(); 339 335 bool readahead;