Skip to content

Commit aaa52e3

Browse files
Hugh Dickinstorvalds
authored andcommitted
mm/khugepaged: fix crashes due to misaccounted holes
Huge tmpfs testing on a shortish file mapped into a pmd-rounded extent hit shmem_evict_inode()'s WARN_ON(inode->i_blocks) followed by clear_inode()'s BUG_ON(inode->i_data.nrpages) when the file was later closed and unlinked. khugepaged's collapse_shmem() was forgetting to update mapping->nrpages on the rollback path, after it had added but then needs to undo some holes. There is indeed an irritating asymmetry between shmem_charge(), whose callers want it to increment nrpages after successfully accounting blocks, and shmem_uncharge(), when __delete_from_page_cache() already decremented nrpages itself: oh well, just add a comment on that to them both. And shmem_recalc_inode() is supposed to be called when the accounting is expected to be in balance (so it can deduce from imbalance that reclaim discarded some pages): so change shmem_charge() to update nrpages earlier (though it's rare for the difference to matter at all). Link: http://lkml.kernel.org/r/alpine.LSU.2.11.1811261523450.2275@eggly.anvils Fixes: 800d8c6 ("shmem: add huge pages support") Fixes: f3f0e1d ("khugepaged: add support of collapse for tmpfs/shmem pages") Signed-off-by: Hugh Dickins <hughd@google.com> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Jerome Glisse <jglisse@redhat.com> Cc: Konstantin Khlebnikov <khlebnikov@yandex-team.ru> Cc: Matthew Wilcox <willy@infradead.org> Cc: <stable@vger.kernel.org> [4.8+] Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 701270f commit aaa52e3

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

mm/khugepaged.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,9 +1506,12 @@ static void collapse_shmem(struct mm_struct *mm,
15061506
khugepaged_pages_collapsed++;
15071507
} else {
15081508
struct page *page;
1509+
15091510
/* Something went wrong: roll back page cache changes */
1510-
shmem_uncharge(mapping->host, nr_none);
15111511
xas_lock_irq(&xas);
1512+
mapping->nrpages -= nr_none;
1513+
shmem_uncharge(mapping->host, nr_none);
1514+
15121515
xas_set(&xas, start);
15131516
xas_for_each(&xas, page, end - 1) {
15141517
page = list_first_entry_or_null(&pagelist,

mm/shmem.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,14 @@ bool shmem_charge(struct inode *inode, long pages)
297297
if (!shmem_inode_acct_block(inode, pages))
298298
return false;
299299

300+
/* nrpages adjustment first, then shmem_recalc_inode() when balanced */
301+
inode->i_mapping->nrpages += pages;
302+
300303
spin_lock_irqsave(&info->lock, flags);
301304
info->alloced += pages;
302305
inode->i_blocks += pages * BLOCKS_PER_PAGE;
303306
shmem_recalc_inode(inode);
304307
spin_unlock_irqrestore(&info->lock, flags);
305-
inode->i_mapping->nrpages += pages;
306308

307309
return true;
308310
}
@@ -312,6 +314,8 @@ void shmem_uncharge(struct inode *inode, long pages)
312314
struct shmem_inode_info *info = SHMEM_I(inode);
313315
unsigned long flags;
314316

317+
/* nrpages adjustment done by __delete_from_page_cache() or caller */
318+
315319
spin_lock_irqsave(&info->lock, flags);
316320
info->alloced -= pages;
317321
inode->i_blocks -= pages * BLOCKS_PER_PAGE;

0 commit comments

Comments
 (0)