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