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=1796155690; b=p6v/tKZwNA3Tk7LpA07N+3MxSxrKD593T8Yw4tjuSoJN0q8ISzMuf2J77KF7UW+NnJ4T zEYfL2pcCEUbee73kOqFYYQO1RtykFDTSXaN5C0WZnLX4E85IG3DL65tB6CdPsNERpoiM a/Wfi6CqVIXqGjsUbQhVD3uwZ1m+C4gI4XHKb4oBvMCw/MGAhQ5gMt3plmL5iRhkl3fOJ pz3Vlx3JfS4NFPx7SdHDmJtV6lXbpYsNSvJpzZ1umnGcw7Rfxixc43UQlGLexwwf3Q7IL mUxaq51FnNY/sS/Yw92bC9p8aJWyixSvP+4+O3lFMakMaayuT5/1LkeEpOIY/hCYDlQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796155690; 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=yMoaEEVVktpJmWG71D1rWaFtLk6NKBDF9MZB0mHhvhc=; b=Uw3zJAAcKc/8XDgR/gW3B2+gBB/d4Nl59TXKA4834XOXd5ntYZUaTUJvlgNqtVn+tVwx 6yXNu+2wgZFudvt2ENaZGYZN6nCh4sOK/kMLzdm8jFlSe57Ba5ABCKfl1xuOLDKCqFosk H1YF/0UrlwYBDP/E+fEDq7sohZTc6MpKIIRdkgvAs8+LFFP2hUeTzJkESbpBj8v9C2QQu 8QT9vgeaeRsMEuHi3I3rC+33oaXiuqJ52xT+R9TS16Mg59UbwZCiSw6WqA5kOErU+5WkX BcUpXfU4tYoU5B9/HDk9pbuK+g39gxxQ9xKjpb2vAMhLOvjFcYv/NOf97fJ5QkeQozw== 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=S6Loocwb; 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 A513F606A320; Tue, 1 Dec 2026 20:08:10 +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=S6Loocwb Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A47C1F000C1; Tue, 1 Dec 2026 20:08:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796155690; bh=yMoaEEVVktpJmWG71D1rWaFtLk6NKBDF9MZB0mHhvhc=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=S6LoocwbbRUzHROZWvb9Z09xCOOUupyPW88g69GTmVv5tYk1KWVV6jMzmaDu6Sj+2 F4RbfGbIaaC8osSAUkjigarp/oIywptldDdu8Bavxzs/5qB0bgdFDXZmAjciQfb2Il R/Vb9u4ov8YJd34BoQlSp8EF47kC3YiXGFOkxBwif505DyFUlcKXh9qDL+Q8cKJXs8 vqy/kPi+mNkyaqXL6lrTws6olgOtY3vtqkCvoMzUKfp+g9ktipp2iKMsSsJ8PyXj5U W1wNNwE8yd9PzI/WAWkdDDO1BavU1vPO0P8/gn+CbWhR6Y1tAq1TVCiZ7kPLMdgQS8 Q9FuCNzFOOXtQ== Message-ID: <51d348b4-fa35-404a-842e-4e1c34e63a2f@kernel.org> Date: Tue, 1 Dec 2026 21:07:59 +0100 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 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset To: "Lorenzo Stoakes (ARM)" Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Masami Hiramatsu , Oleg Nesterov , Andrii Nakryiko , Nicolas Pitre , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Andrew Morton , Suren Baghdasaryan , Shakeel Butt , Pedro Falcato , Jann Horn , Jan Kara , Stephen Smalley , Paul Moore , Chris Mason , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-mm@kvack.org, selinux@vger.kernel.org References: <20261128-cow_pfnmap_pgoff-v1-0-ad3733299f24@kernel.org> <20261128-cow_pfnmap_pgoff-v1-2-ad3733299f24@kernel.org> 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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GYl64glmccD8e87OnEgKnH3FbnJnT4iJchtSvx yJNi1+t0+qDti4m88+/9IuPqCKb6Stl+s2dnLtJNrjXBGJtsQG/sRpqsJz5x1/2nPJSRMsx9 5YfqbdrJSOFXDzZ8/r82HgQEtUvlSXNaXCa95ez0UkOG7+bDm2b3s0XahBQeLVCH0mw3RAQg r7xDAYKIrAwfHHmMTnBQDPJwVqxJjVNr7yBic4yfzVWGCGNE4DnOW0vcIeoyhy9vnIa3w1uZ 3iyY2Nsd7JxfKu1PRhCGwXzRw5TlfEsoRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 12/1/26 19:25, Lorenzo Stoakes (ARM) wrote: > On Sat, Nov 28, 2026 at 11:15:11PM +0100, David Hildenbrand (Arm) wrote: >> remap_pfn_range() and its mmap_prepare variant overwrite vm_pgoff with the >> first PFN of the range in CoW mappings, such that __vm_normal_page() can >> reliably identify CoW'ed anon folios without arch support for pte_special. >> >> However, vm_pgoff is also exposed to user space as the file offset, >> through /proc/pid/maps, /proc/pid/smaps, PROCMAP_QUERY, PERF_RECORD_MMAP >> and the NT_FILE note of core dumps, whereas /proc/pid/pagemap only >> reports PFNs to CAP_SYS_ADMIN. >> >> Consequently, such mappings can leak physical addresses to user space. In >> particular, since commit 8be71e1ba093 ("selinux: mark status and policy >> inodes as immutable, drop status mmap write checks"), a MAP_PRIVATE >> mapping of /sys/fs/selinux/status can leak PFNs that way. >> >> We don't want to expose the PFNs of mapped pages to user space. >> >> Now that we have the anon page offset per VMA, let's fix it by storing >> the PFN in there instead. That value is never exported to user space. >> >> We already handle the anon page offset properly during VMA splits etc. >> Only copy_vma() must be taught to not reset it for unmergeable VMAs, >> which includes any PFNMAP mappings. > > Hmm, I think unmergeable is not the right criteria here, and is in any case a > really confusing predicate to use, this has nothing to do with VMA merging. It's documents that way in the comment: "update its anonymous pgoff to match the new location to increase its chance of merging." Only merging really requires it. > > vma_is_mm_managed() I think would be better to use? It means mixed maps won't > get their anon offset updated but it's meaningless for those anyway. I can do that. > >> >> This has medium urgency. Do not CC stable, as backports would be tricky >> either way and not worth it. >> >> Fixes: 8be71e1ba093 ("selinux: mark status and policy inodes as immutable, drop status mmap write checks") >> Reported-by: Chris Mason >> Signed-off-by: David Hildenbrand (Arm) > > Oh David :)) what hast thou done to my baby? Hey, I took good care of your ugly (anonymous) baby, I swear! [...] >> * >> + * Edge cases: unmergeable VMAs keep their anonymous page offset when moved >> + * while unfaulted, and CoW MAP_PRIVATE-pfnmap regions have it set to the >> + * first PFN in the range. > > unmergeable -> non-mm-managed. But it feels like a bit overwrought to talk about > it there. > > I mean faulted VMAs of other kinds do not change them and that's not listed as > an 'edge case' really. > > So I'd prefer to just drop that bit. Whatever you prefer, it nicely matched the comment update in vma_start_pgoff(). But I'd keep the second part: Edge cases: CoW MAP_PRIVATE-pfnmap regions have it set to the first PFN in the range. ? > >> + * >> * Returns: The anonymous page offset of the start of @vma. >> */ >> static inline pgoff_t vma_start_anon_pgoff(const struct vm_area_struct *vma) >> diff --git a/mm/memory.c b/mm/memory.c >> index 90fc7c3951247..82e1ec8e44e74 100644 >> --- a/mm/memory.c >> +++ b/mm/memory.c >> @@ -687,10 +687,11 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, >> * >> * The way we recognize COWed pages within VM_PFNMAP mappings is through the >> * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit >> - * set, and the vm_pgoff will point to the first PFN mapped: thus every special >> - * mapping will always honor the rule >> + * set, and the anonymous page offset will point to the first PFN mapped: thus >> + * every special mapping will always honor the rule >> * >> - * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) >> + * pfn_of_page == vma_start_anon_pgoff(vma) + >> + * ((addr - vma->vm_start) >> PAGE_SHIFT) >> * >> * And for normal mappings this is false. >> * >> @@ -745,13 +746,11 @@ static inline struct page *__vm_normal_page(struct vm_area_struct *vma, >> if (!pfn_valid(pfn)) >> return NULL; >> } else { >> - const pgoff_t index = linear_page_index(vma, addr); >> - >> /* Only CoW'ed anon folios are "normal". */ >> - if (pfn == index) >> - return NULL; >> if (!vma_is_cow_mapping(vma)) >> return NULL; >> + if (pfn == linear_anon_page_index(vma, addr)) >> + return NULL; > > My word :)) > > Not sure if it'd be better as a helper at this point actually? Like: > > static bool vma_is_cowed_pfnmap(const struct vm_area_struct *vma, > unsigned long pfn, unsigned long addr) > { > VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT)); > > if (!vma_is_cow_mapping(vma)) > return false; > /* See vma_set_cowed_pfnmap(). */ > return pfn == linear_anon_page_index(vma, addr); > } Not sure. There is like a single user and we definetly don't want any more? I mean, __vm_normal_page() is all about exactly that. We could wrap the linear_anon_page_index() into vma_cow_pfnmap_pfn() [see below]. [...] >> >> - err = get_remap_pgoff(is_cow, addr, end, vma->vm_start, vma->vm_end, >> - pfn, &vma->vm_pgoff); >> - if (err) >> - return err; >> - >> + if (is_cow) { >> + err = check_cow_pfnmap_range(addr, end, vma->vm_start, >> + vma->vm_end); >> + if (err) >> + return err; >> + vma_set_anon_pgoff(vma, pfn); > > I think it would be nice to abstract this rather than to open-code the > implementation like: > > static void vma_set_cowed_pfnmap(struct vm_area_struct *vma, unsigned long pfn) cowed_pfnmap ... nothing is cowed yet. it's a cow pfnmap mapping. And we are setting the first pfn. Hm ... vma_set_cow_pfnmap_pfn ? (vma_set_remap_pfn is too generic IMHO) Thanks for the review! -- Cheers, David