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=1796245475; b=xUdP2QWskXT6k0qH/6/t6CzM2UBwyytCafPVvnBW6DIvEITAWLJz5AsvXxUJ5P7rgJq+ b+VNO0vcHqblTqnaALHMNVd0okG/7y5dkK7VOsQZLtN4zNLrLYp67/fNxsjVnHpdhVD2M zr08x85X8MDNa4uHR0IQcpwbp71MgBLEugDIXqEonVwPBrrTVZMCFcckD4nJ7ON5Qj62n rauLVFZW6P2ad18eVN/Kp1EJb3mmmHtLaI/O6GBzPnMIQJtjtvPimPflKaiOPEt5Gl5tG t40XbM24KmkuSkgFETm4mjP+ufG0ynI7fzQf23ANWVzxBx1NoyrRCcpyatb36XcWHWA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796245475; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=2Awvk9QuZ7PsNcvCuda7iR7fjHekUm2P+vdokTVO3+o=; b=z6c5E6A8e+swyPaH6QNRGlMiUpPx8Ztj6tdxSdmHACCD9RiVvBFVFNdNzSvtfK+O40Tv i7LgVjESpp2E/jU9QdiPrRS7xlZ3WYb7pozKNkGiliVYEh3QMuJSsQttcP52dNcpNfMhl v+7UwS5WP4F7sEMxfcH26lZdFP3GelX5UfJ4Y2z6S7JpvyXzTfPicOr4dAsOUpCPlp4dw PHECJMGu2EDyVzhCyNG0tYDZ+HV5o6zSXVav8XOlkZz4QS0/NoVNtvF2+o/VYMSZM3DVy 8Hwy0LokP3ZV/N6FxgL2ztx5DFC177UsitrDMvlPU2OVZ4PrQdnk0GTzNNgidHbmo/Q== 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=Vs91V22h; 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 8440460A2626; Wed, 2 Dec 2026 21:04:35 +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=Vs91V22h Received: by smtp.kernel.org (Postfix) with ESMTPSA id 43B341F0092B; Wed, 2 Dec 2026 21:04:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796245475; bh=2Awvk9QuZ7PsNcvCuda7iR7fjHekUm2P+vdokTVO3+o=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=Vs91V22hACaABxkCcP/PcFF0q41ep5AGYrHoDVgQ2x24Z7iL+9PKED3NRFqxlOXvT 4Tw06ih18UPQ5p1Fnxpgjw+j6LefE0GHfCaQlXwnZBMr2cS5tx8KXP/S11+yGqeJxi V6wEwBRfn+Rcn4VbEZtT4jPSgfyzrFKaT68gL/GFYswWXGQz0bqqe7gPLxpHh374/N QABcIyUJsyfeYrVY3zUQR3wvZr+uKCIZljGNSC7cN5bkf+V7TDsMyNVS8g8m62bOBo hb813B33sjV7qGXTc7pxJ5RL9wPoKPt+rKDuaiDsWqkgJ8i9e7dnbtwl9+xxOi3+Rk 7D4Ackn4VwX5Q== Date: Wed, 2 Dec 2026 21:04:24 +0000 From: "Lorenzo Stoakes (ARM)" To: "David Hildenbrand (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 Subject: Re: [PATCH 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset Message-ID: References: <20261128-cow_pfnmap_pgoff-v1-0-ad3733299f24@kernel.org> <20261128-cow_pfnmap_pgoff-v1-2-ad3733299f24@kernel.org> <51d348b4-fa35-404a-842e-4e1c34e63a2f@kernel.org> 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 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <51d348b4-fa35-404a-842e-4e1c34e63a2f@kernel.org> On Tue, Dec 01, 2026 at 09:07:59PM +0100, David Hildenbrand (Arm) wrote: > 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. I mean it's a bit of a stupid comment really, it doesn't 'increase' the chances of a merge it is the only way you might possibly get one. But really it's about keeping anon pgoff consistent. And since we do propagate and update (and on dump, report) it, it's good to keep that consistency where we can. But yeah I see the logic. I think it's better in general to minimise what gets special treatment htough. > > > > > 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! If I'm the father it can't be ;) > > [...] > > >> * > >> + * 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. Thanks and, yeah sorry maybe wasn't clear, do keep that 2nd part! > > ? > > > >> + * > >> * 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. Yeah, maybe could be a __xxx() function? Or as you suggest The motivation here is to abstract 'how do we see if a PFN map is CoWed' from the (hideous) mechanism basically. > > > We could wrap the linear_anon_page_index() into vma_cow_pfnmap_pfn() [see below]. Sounds good to me! > > [...] > > >> > >> - 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. Lol I'd moan about you and names but actually I knew this was a bad name when I typed it and it was like 'screw it he'll get the gist and think of something better maybe' lol. > > And we are setting the first pfn. Hm ... > > vma_set_cow_pfnmap_pfn ? (vma_set_remap_pfn is too generic IMHO) And there we are, much better thanks :) > > Thanks for the review! NP. Now I should rest probably, lol. > > -- > Cheers, > > David -- Cheers, Lorenzo