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).
Unified · Split
| @@ -1506,9 +1506,12 @@ xa_unlocked: · 1505 unmodified lines | ||
| 1506 | 1506 | khugepaged_pages_collapsed++; |
| 1507 | 1507 | } else { |
| 1508 | 1508 | struct page *page; |
| 1509 | + | |
| 1509 | 1510 | /* Something went wrong: roll back page cache changes */ |
| 1510 | − shmem_uncharge(mapping->host, nr_none); | |
| 1511 | 1511 | xas_lock_irq(&xas); |
| 1512 | + mapping->nrpages -= nr_none; | |
| 1513 | + shmem_uncharge(mapping->host, nr_none); | |
| 1514 | + | |
| 1512 | 1515 | xas_set(&xas, start); |
| 1513 | 1516 | xas_for_each(&xas, page, end - 1) { |
| 1514 | 1517 | page = list_first_entry_or_null(&pagelist, |
| @@ -297,12 +297,14 @@ bool shmem_charge(struct inode *inode, long pages) · 296 unmodified lines | ||
| 297 | 297 | if (!shmem_inode_acct_block(inode, pages)) |
| 298 | 298 | return false; |
| 299 | 299 | |
| 300 | + /* nrpages adjustment first, then shmem_recalc_inode() when balanced */ | |
| 301 | + inode->i_mapping->nrpages += pages; | |
| 302 | + | |
| 300 | 303 | spin_lock_irqsave(&info->lock, flags); |
| 301 | 304 | info->alloced += pages; |
| 302 | 305 | inode->i_blocks += pages * BLOCKS_PER_PAGE; |
| 303 | 306 | shmem_recalc_inode(inode); |
| 304 | 307 | spin_unlock_irqrestore(&info->lock, flags); |
| 305 | − inode->i_mapping->nrpages += pages; | |
| 306 | 308 | |
| 307 | 309 | return true; |
| 308 | 310 | } |
| @@ -312,6 +314,8 @@ void shmem_uncharge(struct inode *inode, long pages) · 3 unmodified lines | ||
| 312 | 314 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 313 | 315 | unsigned long flags; |
| 314 | 316 | |
| 317 | + /* nrpages adjustment done by __delete_from_page_cache() or caller */ | |
| 318 | + | |
| 315 | 319 | spin_lock_irqsave(&info->lock, flags); |
| 316 | 320 | info->alloced -= pages; |
| 317 | 321 | inode->i_blocks -= pages * BLOCKS_PER_PAGE; |