From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.30.112 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796265036; b=K1ED66hCb615EWVKLoMCTzPZ7FEkweCwKX0K5wYBnSavR0P9LnDKn9EOlDz95osCOyH9 EDZ4dDwtHKCmO0Us4iPuifSFnm/lG/DK1aduPojmWlW/H7yd1ncyaMl8l39KyF213RwPp vUlvqDDU5z9Q5Phz5SB2peQTgq800jMD0z+0Ygt6ewYITnxkO2yY8HS+ncUyOPO3GYzHV llvDPnh9PwRTB2lNUFZgVKCfmQZZ5wewPlIt+iz2AtEKOpG8UrE7EXfmcLw+lahzEtx4l R31DCnyQyRn+ZlgwH44X1vhOKBGbdI4CfPrKqkahHNZZv0aDimhE8r7X3DRfUTVvkOQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796265036; h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:Date:From:To: Cc:Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=U+tuZq1cxjTzoj/cTqv1qOO/8DxPTybPH6Ag/U3yzbc=; b=sEWrJ0Kect5SSj+Gr55YumJYhjFBv2kMOHrKMUev3wMZYfJf/CJFkO7QdlK/dM4JuwSM B9/U1DzrjO6qgGCj02GOGEUpRsmE0WBfDBbf38q5Vh2PwvfnwUWUncf8x+HSRiQvllZlV rXH4kJCL199NaZRHKIT6R97fT9EYykeVvoXcVQX1/Zdc0/meQAaxHVCbBQ04n9WVXX8cE y937287M7Dpog+YkhidFUFu/bRbsOSMhIAP5iV1Jkl+YK3+3m2lWq4IfdpMeCY8exq6Uu hmf1grFb6aI9OmI5Cnv64Wk95/zFmqLDlMin5PVe2o/i2UfcCe77ztfKC4cb4EK5RKg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=PetR7bbY; arc=none smtp.remote-ip=115.124.30.112 Received: from out30-112.freemail.mail.aliyun.com (out30-112.freemail.mail.aliyun.com [115.124.30.112]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65A7B606A32C; Thu, 3 Dec 2026 02:30:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 65A7B606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=PetR7bbY DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796265031; h=Date:From:To:Subject:Message-ID:MIME-Version:Content-Type; bh=U+tuZq1cxjTzoj/cTqv1qOO/8DxPTybPH6Ag/U3yzbc=; b=PetR7bbYmE1sth3R2yYWnGMmlmcmuyCAciPrnp1duMPa+5+TLcGjkJExDKQkr7A/bPqLSR8E2TwC0qfsJJfUY/YtBZ+SRxHFy9PMilGMRYpHaUYMWHAhuyx3bPu6y1yoB4ZZIdn/EUJLA3eHXBxsP4hNa2aQw1ou5mXAV3e2t4I= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037026112;MF=fengwei_yin@linux.alibaba.com;NM=1;PH=DS;RN=34;SR=0;TI=SMTPD_---0XGCLpNU_1796265026; Received: from U-V2QX163P-2032.local(mailfrom:fengwei_yin@linux.alibaba.com fp:SMTPD_---0XGCLpNU_1796265026 cluster:ay36) by smtp.aliyun-inc.com; Thu, 03 Dec 2026 10:30:29 +0800 Date: Thu, 3 Dec 2026 10:30:26 +0800 From: YinFengwei 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 , Lorenzo Stoakes , 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-Mailing-List: linux-trace-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: <20261128-cow_pfnmap_pgoff-v1-2-ad3733299f24@kernel.org> 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. > > 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) > --- > 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. > + * > * 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; > } > } > > @@ -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. > */ I wonder if the comment above shoud be moved just before vma_set_anon_pgoff(vma, pfn); in remap_pfn_range_prepare_vma()? As this function doesn't update the anonymous page offset. Regards Yin, Fengwei