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=1796299079; b=nW+YbLnMPdywXMl0VzkWGlqlJvQPGqTyd0gFs8sWsuJlw7gx/OZzwKOktc5BoPoZw3qz eJdmB8n0R1Z94t84kATy7IXXbIEwcWPDuMhqGaL5T26N7nEqYR7c/5yyne/FjAmtyLfUl /UJYarioG3vTD8XQGa38jbGVp4BHkAVepUbaPb14bowyn431JGrlPvu04oN9eW12yUliJ YJmGEsZQwB7dbmy7q6fucyGyrwyhBmWN55ijzRHBRs0OCAte/r/O/Gm7UxN3ff24teaGT kpWSxUMpmprWrIFrXwH2wwCSK02jTu55sZqLqH9zpmF2aLymx1cZNIYpz94yU/ux5PQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796299079; 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=1EIg9qEb21xCVGENjyRpk7mrH+0QU3/ZeYvWR8MpBj4=; b=ija9nnxMc+vqP2O/NYM1ot7e2wuuZF26wQCeUfZ//ZwEIrX9l16/Ou1QU3/qgips8Sxb LVcKEzxDMDvUFS59/hT8xRVNTuTQxKcot/z60nzIDB9fg2J6+yA2mTrQlE0c71xi57odJ vImutxG0chKaRE8UDhLA2M16mlGcgCsOrREIeVVkrGuge3AzYI2YZd2sbrusMhGoiMv5y IB5tIUS6bnUStiyHm+WZ9S30lrITJM+8s4Md0AYFCJaXvcwylGWUg6KuK7xeMTc5VsWpR KJwwhKvI9Zm3gi8rh/2xnenIsiDl6+t9plwz09IjuEnLZIQxo+ZSUS2/CjnUP1KM66Q== 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=dsAJNB+1; 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 53F7E606A680; Thu, 3 Dec 2026 11:57:59 +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=dsAJNB+1 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67D821F00898; Thu, 3 Dec 2026 11:57:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796299079; bh=1EIg9qEb21xCVGENjyRpk7mrH+0QU3/ZeYvWR8MpBj4=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=dsAJNB+1pocdmBeWx5od6mKmz8q+wZ+zBFmSSItDHWy7rZc7jyrl6RxDBh/BvoY00 JivRRgaNugAHHxoic4CqLwcF6qcyffKKa/ORxIxNmRhJ187HJI3xnQOhPknZyHj4tK BChwMuASstV3yT9R7kedDzkzX+o6GnzzIBwcIycPtL9Y72PsRCn2qaIblt2CcOiWEd dzsqoxMrFCe3YAETeTesGI6iM/mD5a2xeK13L1MeGdZdDnRSPwkne3SL97JT/03AS6 1E0uagcTX3jHBXkqBb1v++fe0WAmjYj2K//e0AgFmDldRrJoMAcDP3raaDYQaXVS1d Q2I/y9XuQ7pbg== Message-ID: <68ea31b0-ca4c-4504-8fc7-99261446f7fe@kernel.org> Date: Thu, 3 Dec 2026 12:57:47 +0100 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 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] mm/memory: store the PFN of CoW PFNMAP mappings in the anon page offset To: YinFengwei 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 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+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5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x 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/3/26 03:30, YinFengwei 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. >> >> 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. >> */ Thanks for the review. > 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. Ack, I'll move this into the new vma_cow_pfnmap_pfn(). Thanks! -- Cheers, David