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=1796298303; b=3jrUEOfcGbVA9cZ4lKMv5oTBaCXfDTawfg0YeR++q/XYviRNFpDfb/2utJXDg93KJ3L4 naB19/Gz9W7kxt/O6DPRRZoh/Ck1b7inXQ7b1egDX1txX2umwSXYv5WS/6QrViEWltoXN 92mSfe2Pe1j6tJ0RNovuesAvCXUONWel2PrM+ZtEDZcosh7kvH2Iu9jbOfay4s0J0pg9l ndYi5/E7Yh77jbU+KB4LF1NTCVNfw/HzX5pTk0810hkjzYKHs4TJSk4rXrS5qfQWyzkmw wGXAklU3Zwq/jl152EQQNF4IWlFlNDsTuaVMkZdTSXJpb1SIzt+xV3T1cDRgVrgiepg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796298303; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=e2DwnaCz+epFXMv2TNx00GH3acRZ3P7h9U4/JLd74EI=; b=pm3LmgCxxlWY1MGcofrWycFwI4YYcO97ad9kXRahTyYLGyJ0/i1ThGTpCiik6IUQPgiC ydz/fvQ0fsRSzmm9SDmLrDxdE5GWT3ffH2YBfOfXAnw4pb90/XAwO/3X4Avp6wO+ilRyC Ms9sxq5bDEEjRoPZx8lNYl21M0AVPZu6pENC8/sLAvOXO4RefKWEifTsbbcZUHCeVzJEg totU/ztW0hqU4I9omomHSBjy8/AGspP9lhAylpBHLpvL7dMIMrDn7eEW+Pbv1l4F/9fk5 iL0wQrp3LXME0I2/j1/fN4e5smZbJqJwOSAucA/ZjvytfAbg8EPsYrbWP8EEB5F11Rg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Pi6f1bSq; 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 040E2606A334; Thu, 3 Dec 2026 11:45:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Pi6f1bSq Received: by smtp.kernel.org (Postfix) with ESMTPSA id D7FB31F000C1; Thu, 3 Dec 2026 11:44:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796298302; bh=e2DwnaCz+epFXMv2TNx00GH3acRZ3P7h9U4/JLd74EI=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Pi6f1bSqpW+0i5eCn73WLc3kDBvwW3iAcS5MCU4x1fURZVdUb5hJENglWHbqLP4V2 xuGZhbwAj5enrCiMbvZ6aGDoI2lEi/4xzchZ0+rMsx0wiFFAZ8sM+UhdQWXTO7rU+Z bbGsKqR2GV148b4E2zpRPr5k19gD7EbRGwdQXqCfmO9mb3ElIqLNVQ2qc5177qfr35 3K4b7tzRPpfpjII0YoDHr2ezKxonXGvrgXDa1LzLKi2HTlGa6tkTMZT1omQBjaa/wW V93T95pHGfP1TunZHKLkJrebnzYVbTlM+0dq9NI1f7VzTH4rHMyMMPM8LK1Upg9LK9 6vUISNcwJfuzw== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:52 +0100 Subject: [PATCH v2 29/35] mm/hugetlb: don't share page tables of a VMA during mprotect() X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-hugetlb_fixes_2-v2-29-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> In-Reply-To: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> To: Muchun Song , Oscar Salvador , Andrew Morton , Hongfu Li , Jinmeng Zhou , Mina Almasry , Davidlohr Bueso , Naoya Horiguchi , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Jonathan Corbet , Shuah Khan , Randy Dunlap , Vivek Kasireddy , Mel Gorman , Hugh Dickins , Baolin Wang , Dave Airlie , Jason Gunthorpe , Andy Whitcroft , Roman Gushchin , Barry Song , Andrea Arcangeli , Lorenzo Stoakes , Suren Baghdasaryan , Shakeel Butt , Pedro Falcato , Jann Horn , Guillaume Morin , Yang Shi , James Houghton , Rik van Riel , Harry Yoo , Lance Yang , Axel Rasmussen , Peter Xu , Sasha Levin , Kiryl Shutsemau , Christian Brauner , "Steven Rostedt (Google)" , Hongbo Li , Gregory Price , Zi Yan , Joshua Hahn , Rakie Kim , Ying Huang , Matthew Brost , Byungchul Park , Alistair Popple , Andi Kleen , David Rientjes , Joern Engel , "Aneesh Kumar K.V" Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, Gerd Hoffmann , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Johannes Weiner , Chris Mason , Jiaqi Yan , stable@vger.kernel.org, "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3165; i=david@kernel.org; h=from:subject:message-id; bh=e5RjHj2Uorf72WxvFP7lL1xp2BkZ4zA41gIgZKaktqc=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQ2rLJlj0Hrl23/7ffs3JClMl3Q/Gvnz5boeXi/qEZ /3HDt761lHKwiDGwSArpsgSFHzf4OXLdbXVbnzMMHNYmUCGMHBxCsBEAtMYGXbEOO08tb5QzDAy p2CT12zzkpthAnrX5L7o3Xy139FUTJfhf4hRzDrrz6lPs+KvX5Bw1zw4YVVa/GXJygUMSYtSlr4 1YAIA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A 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. Fixes: 39dde65c9940 ("[PATCH] shared page table for hugetlb page") Cc: Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- mm/hugetlb.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index a23d36e7b8e93..e529994ee9f73 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -7303,15 +7303,23 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, 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) -- 2.56.1