Skip to content

Commit 6cec2b9

Browse files
MiaoheLinakpm00
authored andcommitted
mm/shmem: fix infinite loop when swap in shmem error at swapoff time
When swap in shmem error at swapoff time, there would be a infinite loop in the while loop in shmem_unuse_inode(). It's because swapin error is deliberately ignored now and thus info->swapped will never reach 0. So we can't escape the loop in shmem_unuse(). In order to fix the issue, swapin_error entry is stored in the mapping when swapin error occurs. So the swapcache page can be freed and the user won't end up with a permanently mounted swap because a sector is bad. If the page is accessed later, the user process will be killed so that corrupted data is never consumed. On the other hand, if the page is never accessed, the user won't even notice it. Link: https://lkml.kernel.org/r/20220519125030.21486-5-linmiaohe@huawei.com Signed-off-by: Miaohe Lin <linmiaohe@huawei.com> Reported-by: Naoya Horiguchi <naoya.horiguchi@nec.com> Reviewed-by: Naoya Horiguchi <naoya.horiguchi@nec.com> Cc: Alistair Popple <apopple@nvidia.com> Cc: David Hildenbrand <david@redhat.com> Cc: David Howells <dhowells@redhat.com> Cc: Hugh Dickins <hughd@google.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: NeilBrown <neilb@suse.de> Cc: Peter Xu <peterx@redhat.com> Cc: Ralph Campbell <rcampbell@nvidia.com> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 7b49514 commit 6cec2b9

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

mm/shmem.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,10 @@ static int shmem_find_swap_entries(struct address_space *mapping,
11741174
continue;
11751175

11761176
entry = radix_to_swp_entry(folio);
1177+
/*
1178+
* swapin error entries can be found in the mapping. But they're
1179+
* deliberately ignored here as we've done everything we can do.
1180+
*/
11771181
if (swp_type(entry) != type)
11781182
continue;
11791183

@@ -1671,6 +1675,36 @@ static int shmem_replace_page(struct page **pagep, gfp_t gfp,
16711675
return error;
16721676
}
16731677

1678+
static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index,
1679+
struct folio *folio, swp_entry_t swap)
1680+
{
1681+
struct address_space *mapping = inode->i_mapping;
1682+
struct shmem_inode_info *info = SHMEM_I(inode);
1683+
swp_entry_t swapin_error;
1684+
void *old;
1685+
1686+
swapin_error = make_swapin_error_entry(&folio->page);
1687+
old = xa_cmpxchg_irq(&mapping->i_pages, index,
1688+
swp_to_radix_entry(swap),
1689+
swp_to_radix_entry(swapin_error), 0);
1690+
if (old != swp_to_radix_entry(swap))
1691+
return;
1692+
1693+
folio_wait_writeback(folio);
1694+
delete_from_swap_cache(&folio->page);
1695+
spin_lock_irq(&info->lock);
1696+
/*
1697+
* Don't treat swapin error folio as alloced. Otherwise inode->i_blocks won't
1698+
* be 0 when inode is released and thus trigger WARN_ON(inode->i_blocks) in
1699+
* shmem_evict_inode.
1700+
*/
1701+
info->alloced--;
1702+
info->swapped--;
1703+
shmem_recalc_inode(inode);
1704+
spin_unlock_irq(&info->lock);
1705+
swap_free(swap);
1706+
}
1707+
16741708
/*
16751709
* Swap in the page pointed to by *pagep.
16761710
* Caller has to make sure that *pagep contains a valid swapped page.
@@ -1694,6 +1728,9 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
16941728
swap = radix_to_swp_entry(*foliop);
16951729
*foliop = NULL;
16961730

1731+
if (is_swapin_error_entry(swap))
1732+
return -EIO;
1733+
16971734
/* Look it up and read it in.. */
16981735
page = lookup_swap_cache(swap, NULL, 0);
16991736
if (!page) {
@@ -1761,6 +1798,8 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
17611798
failed:
17621799
if (!shmem_confirm_swap(mapping, index, swap))
17631800
error = -EEXIST;
1801+
if (error == -EIO)
1802+
shmem_set_folio_swapin_error(inode, index, folio, swap);
17641803
unlock:
17651804
if (folio) {
17661805
folio_unlock(folio);

0 commit comments

Comments
 (0)