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=1792788963; b=UscSUhf5qDDpRJ4TeBBYoMPDSvTUcb2409rRSk6t5usWOcNfWGYlrtX91n65iKi5jAgF k78D3epTXBMTBmmLjGFU46o+OBf2mrPVX16zkGCGmxn1jEIhQnDnrO7j/wCCxt1fxN1sK OuNjGDe2JeOhqGc0JRTsAegoxI4yTSz9fCluNl1y1xZXb99k2p1Vag7LsUuI09OnT/ZCh hP2l9wkMN7IAHKQCf5zhCNLOuDgHEdvY1F76dWQE53VFioOQjysbWEdx3fmTZ4uZfp9dh 8/H0LGPXPvRM0sRATHeo8mNQksTBsZglepb0ePnjYxiQkJciWoiBhs2YyFHiFJRj/5g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792788963; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:Content-Language:Autocrypt:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=a31hdiyyDF8ZUUv40jZBEIwm/U5+mj/EC+c4Mf/cga0=; b=e2Ebu1UANA2jBH4UeTC+YOfBV4an/Io48o5Tu6fq/z32ZCeL3fKd+70sLwbHltUgUG0T uf/y7Hc5FNvtkkOX5KajfZmC08OWJpXxBXBgQGCXmBBbM6F4XGcmoBEZkjTdE+mRGCzgq pKFWYNJYoV0x27omUE8/iE9yX7wUhzOqhDqevyVD5lEXQj7oop6apVDxCpxImPJP3+FuA tdKgIC7GtHflGpG6XbWO5/Cz7C5ywfAB8zWiorSQ2WwfdYveBIKSqDRRxpz+Lx0vvCspC dFeRFDJFg7FiWRt53vSNsc1APKY1ETYaoPE49BiwnpzNTZMAZwhjxO7fPfHNUKnSGRA== 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=NKktgR4r; 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 00613606A31B; Fri, 23 Oct 2026 20:56:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NKktgR4r Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A4021F01825; Fri, 23 Oct 2026 20:55:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1792788962; bh=a31hdiyyDF8ZUUv40jZBEIwm/U5+mj/EC+c4Mf/cga0=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=NKktgR4rcKUdiO2NkGJRqzAQyHyNqk4IJRrBJu5OAedX8PNttS1iKxSWrk4vI+QCk YNzgXsxxdNLNEM0LOhDMdTdWBRlT/hiT8BXaWGx9CzvO241Sp7xjXaQQMlT+li1Vmk xEV8BbhLNv3aETBLsnUYUea273eBkAXi2UlnleTBUcRzusje/zDVOTo7VpvS4HCzqP XZZoc2CkOJIPULxNsLE8pm7OWICD3bKx8L75lOJptyqzWNQxaZmKTKNYlZZ0t9JZgf /4WlgKEJZLnCIM0Tuna+bCKkOB1ER1mWz32qJhFlXkmDKNt4I8eWKojYMQm8qkFp7o rB+6gAabFfuTQ== Message-ID: Date: Fri, 23 Oct 2026 22:55:54 +0200 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] arm64/mm: restrict transparent contpte mappings to valid PTEs To: Zhao Gongyi , catalin.marinas@arm.com, will@kernel.org, Ryan Roberts Cc: dev.jain@arm.com, baolin.wang@linux.alibaba.com, lance.yang@linux.dev, mark.rutland@arm.com, akpm@linux-foundation.org, ljs@kernel.org, liam@infradead.org, vbabka@kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20261013093600.54995-1-zhaogongyi@bytedance.com> From: "David Hildenbrand (Arm)" Content-Language: en-US Autocrypt: addr=david@kernel.org; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzS5EYXZpZCBIaWxk ZW5icmFuZCAoQ3VycmVudCkgP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y9vnIa3w1uZ 3iyY2Nsd7JxfKu1PRhCGwXzRw5TlfEsoRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 10/19/26 09:06, Zhao Gongyi wrote: > On 10/14/26 5:41 PM, David Hildenbrand (Arm) wrote: >> CCing Ryan: >> >> https://lore.kernel.org/r/20261013093600.54995-1-zhaogongyi@bytedance.com >> >> On 10/13/26 11:36, Zhao Gongyi wrote: >>> The transparent contpte layer treats PTE_CONT as software state: when a >>> PTE has PTE_CONT set, the contpte helpers may align the operation to a >>> CONT_PTE_SIZE block and operate on the whole block. That invariant only >>> makes sense for valid mappings. Explicit users such as hugetlb manage >>> their contiguous encodings separately. >>> >>> contpte_set_ptes() currently sets PTE_CONT from alignment alone. Batched >>> set_ptes() users can therefore install present-invalid PTEs with >>> PTE_CONT set, for example mprotect(PROT_NONE), NUMA hinting through >>> modify_prot_commit_ptes(), fork() copying a PROT_NONE large folio, or a >>> PMD split installing PAGE_NONE PTEs. >>> >>> This is not a hardware programming problem: invalid descriptors are not >>> translated, so the hardware ignores descriptor bits such as PTE_CONT. It >>> is a software-state problem. Some arm64 fast paths intentionally test >>> pte_cont() directly instead of pte_valid_cont(), for example commit >>> 9970a9a27ffc ("arm64: mm: implement the architecture-specific >>> test_and_clear_young_ptes()"). Fixing the producer side preserves these >>> fast paths and covers all consumers. >>> >>> In the observed failure, mprotect(PROT_NONE) installed a full >>> CONT_PTE_SIZE range of present-invalid PTEs with PTE_CONT set. >>> MADV_GUARD_INSTALL then zapped one PTE. The nr == 1 clear_full_ptes() >>> path only unfolds pte_valid_cont() entries, so the neighbouring >>> present-invalid entries kept PTE_CONT while the target entry became a >>> guard marker. >> >> Hm, is there a good reason why we cannot just use the cont-pte bit for all >> *present* PTEs, even if HW would not be prepared to benefit (!pte_valid())? >> >> I guess we really only have to make sure to shatter properly, right? >> > > > Hi David, > > Right, there is no architectural restriction preventing PTE_CONT from > being used as software grouping state for present-invalid PTEs. For this > particular failure, making contpte_try_unfold() recognize something like > > pte_present(pte) && pte_cont(pte) > > would make clear_full_ptes(nr == 1) unfold the block before replacing one > entry with the guard marker. > > My reason for preferring the producer-side fix is that the existing > transparent contpte helpers mostly use pte_valid_cont() as the membership > test, including contpte_try_unfold(), ptep_get[_lockless](), > pte_batch_hint() and contpte_is_consistent(), while some range fast paths > test pte_cont() directly. > > If present-invalid PTEs are also considered members of a transparent > contpte block, I think we should audit the single-PTE modification paths, > rather than changing only this clear path. We would likely also need a > pte_present_cont() helper: the pte_present() guard matters because the > PTE_CONT bit position is part of the swap offset for non-present entries. > > There is no hardware benefit while the descriptors are invalid, and > set_ptes() can recreate the contiguous encoding when an eligible range is > made valid again. For a stable fix, I therefore preferred to stop > contpte_set_ptes() from creating present-invalid transparent contpte > mappings, which also matches the pte_valid_cont() contract used by most > of the existing helpers. > > I agree that saying this invariant "only makes sense" for valid mappings > is too strong. I will reword that to describe the current helper > contract instead. > > If preserving the software grouping across PROT_NONE is the intended > semantics, I can rework this as a broader unfold-path audit instead. Hi, sorry for the late reply. If we have the spare PTE bit, why shouldn't we just use it? It will make batching over PTEs faster. And we care about that for present PTEs. So I don't quite see why the stable fix couldn't just do it right away? Do you have a prototype that shows that it would require significant code changes? -- Cheers, David