freenode
Kernel & Low-Level

Kernel memory fixes chase KSM use-after-free and a khugepaged race

David Hildenbrand is preparing a large batch of core MM fixes, most of them AI-reported, including crashes an unprivileged process can trigger if KSM is on.

David Hildenbrand has sent a large set of fixes for core Linux memory management, flagging one as high urgency and a dozen more as medium, and saying he may try to get the worst of them to Linus Torvalds before the merge window.

The work is concentrated on transparent huge-page collapse, shared memory, and kernel same-page merging (KSM). Hildenbrand wrote that the batch is "Mostly AI-reported fixes," with further bugs found while reworking those reports. Chris Mason is named as the reporter on most of the individual issues.

The high-urgency item is in khugepaged, which collapses ordinary pages into huge pages. It can walk page tables without first checking that the virtual memory area is still a valid collapse target.

Two medium KSM bugs are the ones with the sharper practical edge. Both need KSM enabled, which is off by default on most systems but common on hosts that deduplicate guest memory, and both can be triggered by an unprivileged process. In one, a KSM page is swapped back in after the mapping has lost its mergeable flag, so later reverse-map walks miss it. If the process then moves that mapping while the kernel is migrating the page, a page-table entry can be left pointing at memory that has already been freed and possibly reused. The next access through that entry can hang, or worse. A second path turns a copy-on-write page that is still shared with another process into a KSM page, with the same class of missed mapping and stale migration entry. Hildenbrand's fix is to copy KSM pages on swap-in when the mapping is not mergeable, and to refuse to convert a folio into a KSM page if another address space might still share it.

Other medium items stop device migration from collecting KSM pages, and stop a zap path from treating a huge PUD mapping as a bad entry. Lower-urgency fixes cover the OOM reaper repopulating copy-on-write mappings in a way that can look like memory corruption, secret-memory pages being handed to drivers that then fault through the direct map, a hang while pinning memfd pages if isolation never succeeds, and lost device writes after migration back to system RAM under lazy freeing.

Hildenbrand is less sure about a userfaultfd change that rejects fills while a hole is being punched, because those operations do not match page-fault behavior. He said he does not see a cleaner approach than failing the calls, and he is unsure how much the bug matters in practice. He has run selftests and targeted reproducers, and asked for extra review of the harder KSM, device-memory, and userfaultfd changes. One trailing change is not a bugfix, only a locking cleanup in migration.