CVE-2026-139799: mm/shmem: hold off eviction in shmem_writeout()
In the Linux kernel, the following vulnerability has been resolved:
mm/shmem: hold off eviction in shmemwriteout()
Once shmemwriteout() has moved the folio from the page cache to the swap cache, the folio lock no longer keeps the inode alive: truncation and eviction find a swap entry, free it and do not wait for anybody. That did not matter as long as swapwriteout() was the last thing the function did. But since it puts the folio back when swapwriteout() returns AOPWRITEPAGEACTIVATE, it uses the mapping and the inode afterwards:
reclaim last close of an unlinked file shmemwriteout() shmemdeletefrompagecache() swapwriteout() zswapstore() fails shmemevictinode() writeback is disabled shmemtruncaterange() return AOPWRITEPAGEACTIVATE shmemfreeswap() destroyinode() ... RCU grace period, freed shmemaddtopagecache(mapping) xaslockirq() <- use after free
swapwriteout() behaves that way if zswap is enabled, cannot take the folio and the memcg or one of its ancestors has memory.zswap.writeback set to 0.
shmemunuse() already added infrastructure to deal with that. Reuse info->stopeviction to protect the inode during writeout. Increment it before removing the folio from the page cache, and decrement it after swapwriteout() and any attempted reinsertion and inode accounting. In shmemevictinode(), wait for the counter to reach zero even if swapoff has already removed the inode from the swap list. Add some helpers to make the code easier to digest.
There is a known remaining issue with two swapoffs racing, and the empty list check not being able to detect that race. That will be addressed separately later.
Fix generated by an LLM and sent by Josef Bacik in private.
[ david: code cleanups; improve description ]
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Compensating control
In shmem_writeout(), use info->stop_eviction to protect the inode during writeout: increment the counter before removing the folio from the page cache, then decrement it after swap_writeout() and any attempted reinsertion and inode accounting. In shmem_evict_inode(), wait for the counter to reach zero.
Event History
Frequently Asked Questions
Which systems are exposed to the problematic writeout path?
The path is relevant when zswap is enabled and zswap cannot take the folio because memory.zswap.writeback is set to 0 for the folio's memory cgroup or one of its ancestors. The issue is triggered when swap_writeout() returns AOP_WRITEPAGE_ACTIVATE under those conditions.
What lifecycle event makes this a use-after-free risk?
After the folio is moved from the page cache to the swap cache, its lock no longer keeps the inode alive. Truncation and eviction can free the swap entry and destroy the inode while shmem_writeout() later uses the mapping and inode to add the folio back to the page cache.