From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796325256; b=jCU8rbvhP7vtI8rDT/yykVzvAHsR7zJjiEVGkmsUrZE068ttEeb5PnxwcyMHJoylgt3K LuNUaexj3fasFBzjr3xzBPPT8GCPOGe9q0d5/pftTGiHZSFoCoGHUzDxbgjJdnLLxeAOV m5W30hAsRiJUG4xuNlNH3e0It4YnNhMlhZNHvw/RPjSMts/0+q9a8xeBsAoSzkCdaSbCX TZjWFZfBkrywGTL73qpizXUnyWMgo+98B354xqHhVWFVYaYdDcMoLKpSAROm322kpJEEe OSENL38S3seXrJ7d7qg7ZiFfsA9L55diACU3sU2SknzUcyZcx0E7BU7ZK8Afkkqfltg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796325256; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=iaXqW6HxzWyNpyMZhhil5sXTWWS+Tqh/cd/7v5Xkco4=; b=NPCimiyDkkeB+eFJeDSy06N/t6rtMYhrYrIq0ngL1sucinOyFL6m7FwgdLWLTmr+BdIh I7zNhzzTHAD1gbitV/yrvTfCzlNZ9X9yEzL9Ln86MEbdAqrEpH5kBcKLXdTsJd1R18D0v Gt5cK6iwRagKfpR26STx8sBVkTLh4vBi+4miZhzOblQtpTxieB/kpLJCFFNj3oaKvcmSp vHLtzz+gj5ARpVUk3Chk+qI7YPYhqB500z99g2VnS8GErknZkK078w3wa5edRRvowTwfZ ygyI9fCLdbxSOHkP6b5TMaCbq2zX81itQAIRqSQBI8dUxYulO1Fc2NdDsDUX3fRtIvg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=BuG22vmB; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F155606A33E for ; Thu, 3 Dec 2026 19:14:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=BuG22vmB Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2AFB1F0092C; Thu, 3 Dec 2026 19:14:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796325256; bh=iaXqW6HxzWyNpyMZhhil5sXTWWS+Tqh/cd/7v5Xkco4=; h=Date:To:From:In-Reply-To:References:Subject; b=BuG22vmBtOKoCzA6Vv2WwyAJclPZh6ovUNM0FFYs+zIw+YIje+qDKVNNgUt4O+pRf 8thbbHDmY9NqUUvH56XcdlkI+UMRmh7o4Vk7jUo0GQtnfuBt4c4CjZ/UDwPrjS9FEM Wu36rptCsBlo6H9/KZMToi4mEO3cvb+5Zpphioi4= Date: Thu, 03 Dec 2026 11:14:15 -0800 To: mm-commits@vger.kernel.org,david@kernel.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261203-hugetlb_fixes_2-v2-29-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-29-9bbeabb555d2@kernel.org> Subject: + mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect.patch added to mm-unstable branch Message-Id: <20261203191415.E2AFB1F0092C@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: mm/hugetlb: don't share page tables of a VMA during mprotect() has been added to the -mm mm-unstable branch. Its filename is mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: "David Hildenbrand (Arm)" Subject: mm/hugetlb: don't share page tables of a VMA during mprotect() Date: Thu, 03 Dec 2026 12:37:52 +0100 When mprotect() of a shared hugetlb mapping races with a hugetlb fault in another process that can share the same PMD table, we can end up with page table entries that don't match the VMA flags, such as writable ones in a PROT_READ mapping or PROT_NONE ones in accessible VMAs. huge_pmd_share() takes i_mmap_rwsem and shares a PMD table if the flags of the two VMAs match, so while that lock is held the entries in a table have to match the flags of its VMA. However, mprotect_fixup() stores the new flags without i_mmap_rwsem, and only hugetlb_change_protection() takes it. A fault in between shares a table that still has the old entries, and the walk then unshares the table instead of changing them: process A process C VMA is RW, entries are RW VMA is R, PUD is empty ========================= ====================== mprotect_fixup() VMA flags = R huge_pmd_share() i_mmap_lock_read() flags match, take A's table i_mmap_unlock_read() hugetlb_change_protection() i_mmap_lock_write() huge_pmd_unshare() clear A's PUD, return 1 skip the entries VMA is R, entries are RW mprotect() changes both the flags and the entries while holding the mmap lock in write mode. Let's fix it by only sharing the page tables of another VMA if we can try-read-lock the mmap lock of its mm. If the trylock fails, we simply try the next VMA, or allocate our own page table. A trylock is fine, as we hold i_mmap_rwsem, which is usually taken under the mmap lock. This has medium urgency, as it can result in endless page fault loops if mprotect() races with PMD table sharing and we end up with PROT_NONE entries in accessible VMAs. If the kernel accesses such memory, for example in copy_to_user(), the loop happens in the kernel and the task cannot be killed. Link: https://patch.msgid.link/20261203-hugetlb_fixes_2-v2-29-9bbeabb555d2@kernel.org Fixes: 39dde65c9940 ("[PATCH] shared page table for hugetlb page") Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Andrew Morton Reported-by: Chris Mason Cc: Muchun Song Cc: Oscar Salvador Cc: Hongfu Li Cc: Jinmeng Zhou Cc: Mina Almasry Cc: Davidlohr Bueso Cc: Naoya Horiguchi Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Michal Hocko Cc: Jonathan Corbet Cc: Shuah Khan Cc: Randy Dunlap Cc: Vivek Kasireddy Cc: Mel Gorman Cc: Hugh Dickins Cc: Baolin Wang Cc: Dave Airlie Cc: Jason Gunthorpe Cc: Andy Whitcroft Cc: Roman Gushchin Cc: Barry Song Cc: Andrea Arcangeli Cc: Lorenzo Stoakes Cc: Suren Baghdasaryan Cc: Shakeel Butt Cc: Pedro Falcato Cc: Jann Horn Cc: Guillaume Morin Cc: Yang Shi Cc: James Houghton Cc: Rik van Riel Cc: Harry Yoo Cc: Lance Yang Cc: Axel Rasmussen Cc: Peter Xu Cc: Sasha Levin Cc: Kiryl Shutsemau Cc: Christian Brauner Cc: Steven Rostedt Cc: Hongbo Li Cc: Gregory Price Cc: Zi Yan Cc: Joshua Hahn Cc: Rakie Kim Cc: Ying Huang Cc: Matthew Brost Cc: Byungchul Park Cc: Alistair Popple Cc: Andi Kleen Cc: David Rientjes Cc: Joern Engel Cc: Aneesh Kumar K.V Cc: Gerd Hoffmann Cc: Johannes Weiner Cc: Jiaqi Yan Cc: mm/hugetlb.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) --- a/mm/hugetlb.c~mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect +++ a/mm/hugetlb.c @@ -7278,15 +7278,23 @@ pte_t *huge_pmd_share(struct mm_struct * if (svma == vma) continue; + /* + * Stabilize the svma flags, as mprotect() changes flags + * before adjusting page tables. + */ + if (!mmap_read_trylock(svma->vm_mm)) + continue; + saddr = page_table_shareable(svma, vma, addr, idx); if (saddr) { spte = hugetlb_walk(svma, saddr, vma_mmu_pagesize(svma)); - if (spte) { + if (spte) ptdesc_pmd_pts_inc(virt_to_ptdesc(spte)); - break; - } } + mmap_read_unlock(svma->vm_mm); + if (spte) + break; } if (!spte) _ Patches currently in -mm which might be from david@kernel.org are mm-mprotect-dont-skip-uffd-marker-install-when-racing-with-pte-table-reclaim.patch mm-userfaultfd-prevent-poison-marker-loss.patch mm-hugetlb-compare-against-persistent-count-when-freeing-lowmem-folios.patch mm-hugetlb-preserve-reservations-when-shrinking-on-highmem.patch mm-hugetlb-account-temporary-folios-separately.patch mm-hugetlb-account-temporary-folios-separately-fix.patch mm-hugetlb-serialize-page-cache-allocations-with-truncation.patch mm-hugetlb-serialize-page-cache-allocations-with-truncation-fix.patch mm-memfd-recheck-the-page-cache-before-allocating-a-hugetlb-folio.patch mm-hugetlb-count-again-if-the-reserve-map-shrank.patch mm-hugetlb-account-restored-reservations-before-freeing-folios.patch mm-hugetlb-delay-anon-folio-rmap-removal-until-after-the-tlb-flush.patch mm-userfaultfd-reject-dax-mappings.patch mm-filemap-avoid-retrying-faults-on-uffd-wp-markers.patch mm-hugetlb-restore-the-reservation-if-vma_needs_reservation-fails.patch mm-hugetlb-restore-the-reservation-when-racing-with-another-reservation.patch mm-hugetlb-return-the-reservation-to-the-subpool-on-error.patch mm-hugetlb-report-reservations-that-we-actually-added-from-hugetlb_reserve_pages.patch mm-hugetlb-keep-hpage_resv_unmapped-when-forking.patch mm-hugetlb-always-mark-new-page-cache-folios-migratable.patch mm-memfd-mark-new-hugetlb-folios-migratable.patch mm-hugetlb-free-surplus-folios-of-other-nodes-on-free.patch mm-hugetlb-wait-for-deferred-frees-before-failing-an-allocation.patch mm-hugetlb_cma-reset-hugetlb_cma_size-if-the-arch-doesnt-support-it.patch fs-proc-task_mmu-dont-clear-vm_softdirty-on-hugetlb-vmas.patch hugetlbfs-fix-integer-overflow-when-punching-holes.patch hugetlbfs-dont-zero-poisoned-pages-in-a-hole-punch.patch hugetlbfs-dont-overflow-the-pseudo-vma-address-in-fallocate.patch hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit.patch hugetlbfs-dont-skip-private-vmas-when-unmapping-on-truncation.patch mm-hugetlb-serialize-private-faults-with-truncation.patch mm-hugetlb-check-eof-for-uffdio_copy-and-uffdio_poison.patch hugetlbfs-zero-all-hugetlbfs-specific-fields-of-new-inodes.patch mm-rmap-dont-fail-unmapping-hugetlb-folios-from-private-vmas.patch mm-hugetlb-only-check-pmd-table-sharing-for-pmd-sized-folios.patch mm-migrate-check-hugetlb-pmd-sharing-in-move_pages.patch mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect.patch mm-hugetlb-count-migration-entries-at-fork-and-unmap.patch mm-hugetlb-stop-accounting-hugetlbpages-once-pmd-tables-were-shared.patch mm-hugetlb_cgroup-charge-faults-to-mm-owner.patch