NOTE: This should be applied on mm-hotfixes-unstable in Andrew's mm tree as it relies on other pending hotfixes. The mmap_region() function is somewhat terrifying, with spaghetti-like control flow and numerous means by which issues can arise and incomplete state, memory leaks and other unpleasantness can occur. A large amount of the complexity arises from trying to handle errors late in the process of mapping a VMA, which forms the basis of recently observed issues with resource leaks and observable inconsistent state. This series goes to great lengths to simplify how mmap_region() works and to avoid unwinding errors late on in the process of setting up the VMA for the new mapping, and equally avoids such operations occurring while the VMA is in an inconsistent state. The patches in this series comprise the minimal changes required to resolve existing issues in mmap_region() error handling, in order that they can be hotfixed and backported. There is additionally a follow up series which goes further, separated out from the v1 series and sent and updated separately. v4: * Reworked solution to use arch_calc_vm_flag_bits() as suggested by Catalin. This also ensures we do not break MTE in a KVM scenario. v3: * Added correct handling for arm64 MTE which was otherwise broken, as reported by Mark Brown. https://lore.kernel.org/all/cover.1730206735.git.lorenzo.stoakes@xxxxxxxxxx/ v2: * Marked first 4 patches as hotfixes, the rest as not. * Improved comment in vma_close() as per Vlastimil. * Updated hole byte count as per Jann. * Updated comment in map_deny_write_exec() as per Jann. * Dropped unnecessary vma_iter_free() as per Vlastimil, Liam. * Corrected vms_abort_munmap_vmas() mistaken assumption about nr_pages as per Vlastimil. * Changed order of initial checks in mmap_region() to avoid user-visible side effects as per Vlastimil, Liam. * Corrected silly incorrect use of vma field. * Various style corrects as per Liam. * Fix horrid mistake with merge VMA, reworked the logic to avoid that nonsense altogether. * Add fields to map state rather than using vmg fields to avoid confusion/risk of vmg state changing breaking things. * Replaced last commit removing merge retry with one that retries the merge, only sanely. https://lore.kernel.org/all/cover.1729715266.git.lorenzo.stoakes@xxxxxxxxxx/ v1: https://lore.kernel.org/all/cover.1729628198.git.lorenzo.stoakes@xxxxxxxxxx/ Lorenzo Stoakes (5): mm: avoid unsafe VMA hook invocation when error arises on mmap hook mm: unconditionally close VMAs on error mm: refactor map_deny_write_exec() mm: refactor arch_calc_vm_flag_bits() and arm64 MTE handling mm: resolve faulty mmap_region() error path behaviour arch/arm64/include/asm/mman.h | 10 ++- arch/parisc/include/asm/mman.h | 5 +- include/linux/mman.h | 28 +++++-- mm/internal.h | 45 ++++++++++++ mm/mmap.c | 130 ++++++++++++++++++--------------- mm/mprotect.c | 2 +- mm/nommu.c | 9 +-- mm/shmem.c | 3 - mm/vma.c | 14 ++-- mm/vma.h | 6 +- 10 files changed, 159 insertions(+), 93 deletions(-) -- 2.47.0