From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6A376606A31A for ; Tue, 1 Dec 2026 18:25:56 +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=cgx5xdQe Received: by kanga.kvack.org (Postfix) id 51DEF6B0639; Tue, 1 Dec 2026 13:25:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4CEFD6B063D; Tue, 1 Dec 2026 13:25:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E53B6B0640; Tue, 1 Dec 2026 13:25:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B6F616B0639 for ; Tue, 1 Dec 2026 13:25:53 -0500 (EST) Received: from smtpin02.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 48F27140F91 for ; Tue, 1 Dec 2026 18:25:53 +0000 (UTC) X-FDA: 85496242986.02.96C74A6 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf26.hostedemail.com (Postfix) with ESMTP id 8EC2E140003 for ; Tue, 1 Dec 2026 18:25:51 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=cgx5xdQe; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of ljs@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=ljs@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796149551; b=SvS+mV884UkCBcw1yLLPHxV/2lpzhVYHbEK1Fth7IGnAuhvTsS58jaGbkCxP49i1dKefoV c786jO2lgDDgFl85orXnawZvAQ4sXA4FF+6/m+ne6fUl3XF85JXZ9XZSEz3SmySx8S4ErE m363HPFK2gy3hXp1hVbdtpqsowLxFA0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796149551; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HK4Gzst5vjWdqciHcXGHAyIIAmfdmSVxCLX7BATSAa4=; b=REI9AJRzBUkJKr/3TxbaoTRovqQPNTItvmQlZvTama9qp03WbrM87WDDrVC6EY6dubqhBb KT4MTY3Q0i+lXwFpnWHQ5Bn4wWk0Dn51gvmi90HGUbWrJjZgxx7pAkAZxirO6v07U+elMQ WmhnsknV+vJ4nz9k5+k4KHNhrANYUJg= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=cgx5xdQe; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of ljs@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=ljs@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id CDC3D60154; Tue, 1 Dec 2026 18:25:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F182F1F000C1; Tue, 1 Dec 2026 18:25:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796149550; bh=HK4Gzst5vjWdqciHcXGHAyIIAmfdmSVxCLX7BATSAa4=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=cgx5xdQeX+uJNnVyGe5FcfBiPwiJ9CjH0JHsrq1+WAOR0NDmu7AjqTpK9pPVINQ5t 7jik7NXu/eE9hSfq69otXn88Gyi7wJ9lWKg6iKvgUOW2iG1RDFFR/biVDLqfAZfj2O kiQ0Q7kb62hlIMWthXP8A5asKZf5m7rOv7c+PmrBCmZeYgaTIJ0Ci5inn2h1X/YfkJ 1dlzdeTNxvBERlTKRihXdD2PHwxQdnGvCc7x4yceSUqfFNlTBg5YnpyUL5vz6dBMdI yuwnfwL6UplchLtMwcml2qJNujKtHZL1a93F8wp9FSfdXjarxJhoZ7Q/ArFzcNs4ty VJp2AV/uD6rlg== Date: Tue, 1 Dec 2026 18:25:40 +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> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261128-cow_pfnmap_pgoff-v1-2-ad3733299f24@kernel.org> X-Stat-Signature: 5rrpymid956ww49ruf8sg44hknie8h1z X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 8EC2E140003 X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_V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JvafSJ0oBfhHHkfbc/rkQYlncSQvPR+ZZVf6h2g1GIKrHoZax+UQDFTLdP FF7wL0UK FzcWgRkG8NUKXAgapqG7QbFQ6KoOf82h3VisR8yfutmJJKv769EQqyrxHIK04d5ZZeDh8lFz+3LqyUHlM85Enx7O71Vr9OtfAoVG5NWCYWXcBZ77f4Lz0urBkqRz9qrtXdqj3KLMojTq0i7wwAqlrF/fezrU9ik8jDGIrS/OjMGJt6HZJjeLap0VCExb05FafGevIH414XMj8+gC2T7q4fBeDeQhmtn5S+A114NOkTnaLVHbPT/eibvOtI5/ZGHcYkeustKlI9Eibkulp/RVhT8DXij+PoTVseWl9qS0bZg4Kc3vEa5dQISNrwlLsG03J7Jwsv2qaD7wTNvvZCiJejE5+2rJ0zbw+2/txH0BG36xiW5nKN0i26kNZaneVoiZc3CrWQcQVXPsiWmpp8s4sBv3kFNuTKRc+zsJzbwANzIE0TrThpkgf+9eXChlw+amPl0CT7/XFdEoaVU0/WoErMcjNiM/LSRSpuPMvDaIkP3c4nD3ybtfIasvcLr56AfLKmu24A78SJnF5oxKiTCKSVkCY0VPg3+LnKTzPTZlAjJBmGxoCWRzSTizfAxGqI0NJ2glK Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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. 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. > > 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? > --- > fs/cramfs/inode.c | 7 +----- > include/linux/mm.h | 10 +++++--- > mm/memory.c | 55 ++++++++++++++++++++--------------------- > mm/vma.c | 6 ++--- > mm/vma.h | 3 +-- > tools/testing/vma/include/dup.h | 3 ++- > 6 files changed, 41 insertions(+), 43 deletions(-) > > diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c > index d4cd03f4f60d5..9b05250b2198c 100644 > --- a/fs/cramfs/inode.c > +++ b/fs/cramfs/inode.c > @@ -402,12 +402,7 @@ static int cramfs_physmem_mmap(struct file *file, struct vm_area_struct *vma) > } > > if (pages == vma_pages(vma)) { > - /* > - * The entire vma is mappable. remap_pfn_range() will > - * make it distinguishable from a non-direct mapping > - * in /proc//maps by substituting the file offset > - * with the actual physical address. > - */ > + /* The entire vma is mappable. */ > ret = remap_pfn_range(vma, vma->vm_start, address >> PAGE_SHIFT, > pages * PAGE_SIZE, vma->vm_page_prot); > } else { > diff --git a/include/linux/mm.h b/include/linux/mm.h > index abd427c136409..00abd4d250a99 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -1766,7 +1766,8 @@ static inline bool vma_flags_can_merge(const vma_flags_t *flags) > * mapping not embodied in any attribute of the VMA. > * > * Additionally, private (CoW) PFN maps encode the source PFN of the > - * range in vma->vm_pgoff, which may otherwise cause spurious merges. > + * range in the anonymous page offset, which may otherwise cause > + * spurious merges. > */ > if (!vma_flags_is_mm_backed(flags)) > return false; > @@ -4625,8 +4626,7 @@ static inline unsigned long vma_pages(const struct vm_area_struct *vma) > * If @vma is a MAP_PRIVATE file-backed mapping, then this returns the > * page offset within the file. > * > - * Edge cases: nommu does not abide by these and CoW MAP_PRIVATE-pfnmap regions > - * have their page offset set to the first PFN in the range. > + * Edge cases: nommu does not abide by these. > * > * Returns: The page offset of the start of @vma. > */ > @@ -4680,6 +4680,10 @@ static inline pgoff_t vma_last_pgoff(const struct vm_area_struct *vma) > * This value is used for tracking MAP_PRIVATE file-backed mappings by their > * anonymous page offset. > * > + * 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. > + * > * 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); } > } > } > > @@ -3217,22 +3216,17 @@ static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, > return 0; > } > > -static int get_remap_pgoff(bool is_cow, unsigned long addr, > - unsigned long end, unsigned long vm_start, unsigned long vm_end, > - unsigned long pfn, pgoff_t *vm_pgoff_p) > +static int check_cow_pfnmap_range(unsigned long addr, unsigned long end, > + unsigned long vm_start, unsigned long vm_end) > { > /* > * There's a horrible special case to handle copy-on-write > * behaviour that some programs depend on. We mark the "original" > - * un-COW'ed pages by matching them up with "vma->vm_pgoff". > - * See vm_normal_page() for details. > + * un-COW'ed pages by matching them up with the anonymous page > + * offset. See vm_normal_page() for details. > */ > - if (is_cow) { > - if (addr != vm_start || end != vm_end) > - return -EINVAL; > - *vm_pgoff_p = pfn; > - } > - > + if (addr != vm_start || end != vm_end) > + return -EINVAL; > return 0; > } > > @@ -3370,17 +3364,18 @@ int remap_pfn_range_prepare(struct vm_area_desc *desc) > const struct mmap_action *action = &desc->action; > const unsigned long start = action->remap.start; > const unsigned long end = start + action->remap.size; > - const unsigned long pfn = action->remap.start_pfn; > const bool is_cow = vma_desc_is_cow_mapping(desc); > int err; > > if (!range_in_vma_desc(desc, start, end)) > return -EFAULT; > > - err = get_remap_pgoff(is_cow, start, end, desc->start, desc->end, pfn, > - &desc->pgoff); > - if (err) > - return err; > + if (is_cow) { > + err = check_cow_pfnmap_range(start, end, desc->start, > + desc->end); > + if (err) > + return err; > + } > > vma_desc_set_flags_mask(desc, VMA_REMAP_FLAGS); > return 0; > @@ -3394,11 +3389,13 @@ static int remap_pfn_range_prepare_vma(struct vm_area_struct *vma, > const bool is_cow = vma_is_cow_mapping(vma); > int err; > > - 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) { VM_WARN_ON_ONCE(!vma_test(vma, VMA_PFNMAP_BIT)); VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma)); /* we stuff this in the anon pgoff which a PFN map doesn't use. */ vma_set_anon_pgoff(vma, pfn); } > + } > vma_set_flags_mask(vma, VMA_REMAP_FLAGS); > return 0; > } > @@ -3436,6 +3433,8 @@ int remap_pfn_range_complete(struct vm_area_struct *vma, > const unsigned long size = action->remap.size; > const pgprot_t prot = action->remap.pgprot; > > + if (vma_is_cow_mapping(vma)) > + vma_set_anon_pgoff(vma, pfn); Obviously similar abstraction comment to above. > return do_remap_pfn_range(vma, start, pfn, size, prot); > } > > diff --git a/mm/vma.c b/mm/vma.c > index c82e9306b11ac..2536862ff093a 100644 > --- a/mm/vma.c > +++ b/mm/vma.c > @@ -2006,10 +2006,10 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, > VMG_VMA_STATE(vmg, &vmi, NULL, vma, addr, addr + len); > > /* > - * If a vma has not yet been faulted, update its anonymous pgoff to > - * match the new location to increase its chance of merging. > + * If a vma has not yet been faulted and can merge, update its anonymous > + * pgoff to match the new location to increase its chance of merging. > */ > - if (!vma_has_anon_rmap(vma)) { > + if (!vma_has_anon_rmap(vma) && vma_can_merge(vma)) { Again vma_is_mm_managed() please. > anon_pgoff = addr >> PAGE_SHIFT; > > if (vma_is_anonymous(vma)) { > diff --git a/mm/vma.h b/mm/vma.h > index a74143bd22746..8e506f5d15a79 100644 > --- a/mm/vma.h > +++ b/mm/vma.h > @@ -290,8 +290,7 @@ static inline void assert_sane_pgoff(struct vm_area_struct *vma, pgoff_t pgoff) > return; > /* > * File-backed VMAs have arbitrary page offset (either page offset into > - * file or for pfnmap the PFN of the start of the range or drivers may > - * set arbitrary page offset). > + * file or drivers may set arbitrary page offset). > */ > if (!vma_is_anonymous(vma)) > return; > diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h > index f9bafe4e20158..9e72d5f06e10e 100644 > --- a/tools/testing/vma/include/dup.h > +++ b/tools/testing/vma/include/dup.h > @@ -1741,7 +1741,8 @@ static inline bool vma_flags_can_merge(const vma_flags_t *flags) > * mapping not embodied in any attribute of the VMA. > * > * Additionally, private (CoW) PFN maps encode the source PFN of the > - * range in vma->vm_pgoff, which may otherwise cause spurious merges. > + * range in the anonymous page offset, which may otherwise cause > + * spurious merges. > */ > if (!vma_flags_is_mm_backed(flags)) > return false; > > -- > 2.56.1 > -- Cheers, Lorenzo