Linux Documentation
 help / color / mirror / Atom feed
* [PATCH 0/4] mm: AI-generated fixes for coda, madvise, mprotect and PAGEMAP_SCAN
@ 2026-11-27 19:45 Lorenzo Stoakes (ARM)
  2026-11-27 19:45 ` [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping Lorenzo Stoakes (ARM)
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-27 19:45 UTC (permalink / raw)
  To: Jan Harkes, coda, Andrew Morton, David Rheinsberg, Hugh Dickins,
	Vlastimil Babka, Liam R. Howlett, David Hildenbrand, Jann Horn,
	Alex Shi, Johannes Weiner, Pedro Falcato, David Rientjes,
	Rik van Riel, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Jonathan Corbet, Shuah Khan, Randy Dunlap,
	Michał Mirosław, Arnd Bergmann, Stephen Rothwell,
	Muhammad Usama Anjum
  Cc: codalist, linux-mm, Andrei Vagin, linux-doc, linux-kernel,
	linux-fsdevel, Lorenzo Stoakes (ARM), Chris Mason, stable

The usual stuff - glorious gifts of AI goodness transmuted into a smoother
form by your's truly.

Please check for any insanity. OK wait, let me clarify - in the patches.

The only one that needs a backport is the first coda fixup, whereupon, in
the unlikely scenario of a container manager doing something rather silly,
we might end up with a dangling pointer.

Whilst unlikely, that one might be worth sending out sooner rather than
later.

The rest are the usual edge case stuff, one involving the truly cursed
'guess what it's possible to map at address 0 if you're crazy enough!' type
thing.

I shan't talk about the evils contained in the last patch. PAGEMAP_SCAN
deep inside your soul ye masses to find salvation from PM_SCAN_WP_MATCHING!

Anyway, as usual, please review! Thanks.

(Also note that a lack of Cc: stable is not a mistake, rather it means that
 I think the fix doesn't warrant any back port shenanigans.)

Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
Chris Mason (2):
      mm/madvise: don't declare OOM for MADV_WILLNEED swapin
      mm/mprotect: fix unpaired notifier start at address 0

Lorenzo Stoakes (ARM) (2):
      coda: never point a file's f_mapping at a container's mapping
      fs/proc/task_mmu: write-protect only with ptrace attach

 Documentation/admin-guide/mm/pagemap.rst |  4 ++-
 fs/coda/file.c                           | 14 +++++++++-
 fs/proc/task_mmu.c                       | 47 ++++++++++++++++++++++++++++----
 mm/madvise.c                             | 19 +++++++++----
 mm/mprotect.c                            |  8 +++---
 5 files changed, 75 insertions(+), 17 deletions(-)
---
base-commit: c6f53520f32302ea64cbcfd7cc25a31081e34548
change-id: 20261127-mlock-madvise-mprotect-ai-fixes-efebbfabe99c

Cheers,
-- 
Lorenzo Stoakes (ARM) <ljs@kernel.org>


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping
  2026-11-27 19:45 [PATCH 0/4] mm: AI-generated fixes for coda, madvise, mprotect and PAGEMAP_SCAN Lorenzo Stoakes (ARM)
@ 2026-11-27 19:45 ` Lorenzo Stoakes (ARM)
  2026-11-27 20:44   ` Andrew Morton
  2026-11-27 22:01   ` Pedro Falcato
  2026-11-27 19:45 ` [PATCH 2/4] mm/madvise: don't declare OOM for MADV_WILLNEED swapin Lorenzo Stoakes (ARM)
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 13+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-27 19:45 UTC (permalink / raw)
  To: Jan Harkes, coda, Andrew Morton, David Rheinsberg, Hugh Dickins,
	Vlastimil Babka, Liam R. Howlett, David Hildenbrand, Jann Horn,
	Alex Shi, Johannes Weiner, Pedro Falcato, David Rientjes,
	Rik van Riel, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Jonathan Corbet, Shuah Khan, Randy Dunlap,
	Michał Mirosław, Arnd Bergmann, Stephen Rothwell,
	Muhammad Usama Anjum
  Cc: codalist, linux-mm, Andrei Vagin, linux-doc, linux-kernel,
	linux-fsdevel, Lorenzo Stoakes (ARM), Chris Mason, stable

Coda file systems stack differently from other stacked file systems.

On mmap(), coda_file_mmap() switches both the Coda inode's i_mapping and
the Coda file's f_mapping for the mapping of the container file. This
causes two problems.

Firstly, the container file may not remain pinned, at which point this may
result in a dangling reference in coda_file->f_mapping, and so a
use-after-free in mmap() or lseek().

This can happen when you open the same inode across two files, mmap()'ing
in one, which assigns the i_mapping, and open()'ing another, which assigns
that i_mapping to the file's f_mapping, if the cache manager gave the two
files different containers. Each file pins only its own container.

Consider (A, B = separate open files of the same inode):

    A, with container C1            B, with container C2
    ====================            ====================
    mmap()
      coda_file_mmap()
        i_mapping = C1's mapping
                                    open()
                                      do_dentry_open()
                                        f_mapping = i_mapping
                                        coda_open()
                                          cfi_container = C2
    munmap()
    close()
      coda_release()
        i_mapping = &i_data
        fput(C1), the last one
                                    mmap()
                                      mmap_region()
                                        mapping_map_writable(f_mapping)

Secondly, mmap_region() takes a temporary i_mmap_writable count on
file->f_mapping ahead of the mmap hook, and drops it on file->f_mapping
afterwards. As the hook changes f_mapping in between, the count is dropped
on the container's mapping rather than on the mapping it was taken on:

    i_mmap_writable of                          Coda  container
    mmap_region()
      mapping_map_writable(file->f_mapping)       1       0
      __mmap_region()
        coda_file_mmap()
          coda_file->f_mapping = host_file->f_mapping
          vma->vm_file = get_file(host_file)
        vma_link_file()
          mapping_allow_writable()                1       1
      mapping_unmap_writable(file->f_mapping)     1       0
    munmap()
      mapping_unmap_writable()                    1      -1

The container's count is negative after munmap(), so the next shared
mapping of that Coda file, if it is open for writing, fails with -EPERM.

Fix both issues by no longer updating coda_file->f_mapping in
coda_file_mmap(), and by having coda_open() always assign the canonical
mapping of the inode, as do_dentry_open() may have propagated an i_mapping
which coda_file_mmap() redirected.

Reported-by: Chris Mason <mason@kernel.org>
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes: 4bb5f5d9395b ("mm: allow drivers to prevent new writable mappings")
Fixes: 5de195060b2e ("mm: resolve faulty mmap_region() error path behaviour")
Cc: <stable@vger.kernel.org> # 5.3.x
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
 fs/coda/file.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/fs/coda/file.c b/fs/coda/file.c
index c9fba41f7cea..64f05f2fc4b3 100644
--- a/fs/coda/file.c
+++ b/fs/coda/file.c
@@ -182,7 +182,6 @@ coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
 
 	cii = ITOC(coda_inode);
 	spin_lock(&cii->c_lock);
-	coda_file->f_mapping = host_file->f_mapping;
 	if (coda_inode->i_mapping == &coda_inode->i_data)
 		coda_inode->i_mapping = host_inode->i_mapping;
 
@@ -248,6 +247,19 @@ int coda_open(struct inode *coda_inode, struct file *coda_file)
 
 	host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
 
+	/*
+	 * On mmap coda_inode->i_mapping is updated to point at the mapping of
+	 * the mmap()'d file's container, and do_dentry_open() propagates that
+	 * to coda_file->f_mapping.
+	 *
+	 * However coda_file's container may differ, in which case the lifetime
+	 * of that mapping may not match that of coda_file.
+	 *
+	 * To avoid a dangling pointer, reassign the mapping to the canonical
+	 * one.
+	 */
+	coda_file->f_mapping = &coda_inode->i_data;
+
 	cfi->cfi_magic = CODA_MAGIC;
 	cfi->cfi_mapcount = 0;
 	cfi->cfi_container = host_file;

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 2/4] mm/madvise: don't declare OOM for MADV_WILLNEED swapin
  2026-11-27 19:45 [PATCH 0/4] mm: AI-generated fixes for coda, madvise, mprotect and PAGEMAP_SCAN Lorenzo Stoakes (ARM)
  2026-11-27 19:45 ` [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping Lorenzo Stoakes (ARM)
@ 2026-11-27 19:45 ` Lorenzo Stoakes (ARM)
  2026-11-27 22:14   ` Pedro Falcato
  2026-11-27 19:45 ` [PATCH 3/4] mm/mprotect: fix unpaired notifier start at address 0 Lorenzo Stoakes (ARM)
  2026-11-27 19:45 ` [PATCH 4/4] fs/proc/task_mmu: write-protect only with ptrace attach Lorenzo Stoakes (ARM)
  3 siblings, 1 reply; 13+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-27 19:45 UTC (permalink / raw)
  To: Jan Harkes, coda, Andrew Morton, David Rheinsberg, Hugh Dickins,
	Vlastimil Babka, Liam R. Howlett, David Hildenbrand, Jann Horn,
	Alex Shi, Johannes Weiner, Pedro Falcato, David Rientjes,
	Rik van Riel, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Jonathan Corbet, Shuah Khan, Randy Dunlap,
	Michał Mirosław, Arnd Bergmann, Stephen Rothwell,
	Muhammad Usama Anjum
  Cc: codalist, linux-mm, Andrei Vagin, linux-doc, linux-kernel,
	linux-fsdevel, Lorenzo Stoakes (ARM), Chris Mason

From: Chris Mason <mason@kernel.org>

MADV_WILLNEED on memory in swap can OOM kill a task when the memcg that
the swap entries are charged to is at its limit.

The hint is only a prefetch, so it has to give up on a page when memory
is short. But swapin_walk_pmd_entry() and shmem_swapin_range() call
read_swap_cache_async() with GFP_HIGHUSER_MOVABLE and with the gfp mask
of the mapping, so the allocation and the memcg charge can both go as
far as the OOM killer.

The charge goes to the memcg of the swap entry, which need not be the
caller's.

The fix used here is to allocate with the gfp mask of readahead, and to
flush the block plug when a page fails. madvise_do_behavior() plugs the
whole walk, so the swap writes of reclaim can sit in that plug. Their
folios stay under writeback, and with no retries the rest of the range
can fail as well.

The hint can now skip pages when memory is short, whether in a memcg at
its limit or on the system as a whole, since neither the allocation nor
the memcg charge is retried any longer.

This applies in particular where reclaim has to write to swap to make
room. Skipped pages stay in swap until a fault reads them.

Fixes: 4c6355b25e8b ("mm: memcontrol: charge swapin pages on instantiation")
Signed-off-by: Chris Mason <mason@kernel.org>
[ ljs: commit message tweak ]
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
 mm/madvise.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/mm/madvise.c b/mm/madvise.c
index ebb4ce7daf6c..8b56f7aea935 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -196,6 +196,8 @@ static int madvise_update_vma(vm_flags_t new_flags,
 static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
 		unsigned long end, struct mm_walk *walk)
 {
+	/* Only a prefetch, as in readahead_gfp_mask() */
+	const gfp_t gfp = GFP_HIGHUSER_MOVABLE | __GFP_NORETRY | __GFP_NOWARN;
 	struct vm_area_struct *vma = walk->private;
 	struct swap_io_ctx ctx = {};
 	pte_t *ptep = NULL;
@@ -221,10 +223,13 @@ static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,
 		pte_unmap_unlock(ptep, ptl);
 		ptep = NULL;
 
-		folio = read_swap_cache_async(&ctx, entry, GFP_HIGHUSER_MOVABLE,
-					vma, addr);
-		if (folio)
+		folio = read_swap_cache_async(&ctx, entry, gfp, vma, addr);
+		if (folio) {
 			folio_put(folio);
+		} else {
+			/* Reclaim queues its swap writes in our plug */
+			blk_flush_plug(current->plug, false);
+		}
 	}
 
 	if (ptep)
@@ -267,9 +272,13 @@ static void shmem_swapin_range(struct vm_area_struct *vma,
 		rcu_read_unlock();
 
 		folio = read_swap_cache_async(&ctx, entry,
-				mapping_gfp_mask(mapping), vma, addr);
-		if (folio)
+				readahead_gfp_mask(mapping), vma, addr);
+		if (folio) {
 			folio_put(folio);
+		} else {
+			/* Reclaim queues its swap writes in our plug */
+			blk_flush_plug(current->plug, false);
+		}
 
 		rcu_read_lock();
 	}

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 3/4] mm/mprotect: fix unpaired notifier start at address 0
  2026-11-27 19:45 [PATCH 0/4] mm: AI-generated fixes for coda, madvise, mprotect and PAGEMAP_SCAN Lorenzo Stoakes (ARM)
  2026-11-27 19:45 ` [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping Lorenzo Stoakes (ARM)
  2026-11-27 19:45 ` [PATCH 2/4] mm/madvise: don't declare OOM for MADV_WILLNEED swapin Lorenzo Stoakes (ARM)
@ 2026-11-27 19:45 ` Lorenzo Stoakes (ARM)
  2026-11-27 22:15   ` Pedro Falcato
  2026-11-27 19:45 ` [PATCH 4/4] fs/proc/task_mmu: write-protect only with ptrace attach Lorenzo Stoakes (ARM)
  3 siblings, 1 reply; 13+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-27 19:45 UTC (permalink / raw)
  To: Jan Harkes, coda, Andrew Morton, David Rheinsberg, Hugh Dickins,
	Vlastimil Babka, Liam R. Howlett, David Hildenbrand, Jann Horn,
	Alex Shi, Johannes Weiner, Pedro Falcato, David Rientjes,
	Rik van Riel, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Jonathan Corbet, Shuah Khan, Randy Dunlap,
	Michał Mirosław, Arnd Bergmann, Stephen Rothwell,
	Muhammad Usama Anjum
  Cc: codalist, linux-mm, Andrei Vagin, linux-doc, linux-kernel,
	linux-fsdevel, Lorenzo Stoakes (ARM), Chris Mason

From: Chris Mason <mason@kernel.org>

A protection change on a VMA that starts at address 0 can leave an MMU
notifier invalidation open forever.

Waiters for its end, such as kvm_swap_active_memslots() and
mmu_interval_read_begin(), then sleep uninterruptibly and never wake up.

Fix by using a boolean value to track this instead of the start address of
the range.

Fixes: a5338093bfb4 ("mm: move mmu notifier call from change_protection to change_pmd_range")
Signed-off-by: Chris Mason <mason@kernel.org>
[ ljs: commit msg tweak ]
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
 mm/mprotect.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/mm/mprotect.c b/mm/mprotect.c
index e18d4e12fc34..0f94b0287895 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -579,8 +579,7 @@ static inline long change_pud_range(struct mmu_gather *tlb,
 	pud_t *pudp, pud;
 	unsigned long next;
 	long pages = 0, ret;
-
-	range.start = 0;
+	bool notified = false;
 
 	pudp = pud_offset(p4d, addr);
 	do {
@@ -596,7 +595,8 @@ static inline long change_pud_range(struct mmu_gather *tlb,
 		if (pud_none(pud))
 			continue;
 
-		if (!range.start) {
+		if (!notified) {
+			notified = true;
 			mmu_notifier_range_init(&range,
 						MMU_NOTIFY_PROTECTION_VMA, 0,
 						vma->vm_mm, addr, end);
@@ -624,7 +624,7 @@ static inline long change_pud_range(struct mmu_gather *tlb,
 					  cp_flags);
 	} while (pudp++, addr = next, addr != end);
 
-	if (range.start)
+	if (notified)
 		mmu_notifier_invalidate_range_end(&range);
 
 	return pages;

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 4/4] fs/proc/task_mmu: write-protect only with ptrace attach
  2026-11-27 19:45 [PATCH 0/4] mm: AI-generated fixes for coda, madvise, mprotect and PAGEMAP_SCAN Lorenzo Stoakes (ARM)
                   ` (2 preceding siblings ...)
  2026-11-27 19:45 ` [PATCH 3/4] mm/mprotect: fix unpaired notifier start at address 0 Lorenzo Stoakes (ARM)
@ 2026-11-27 19:45 ` Lorenzo Stoakes (ARM)
  2026-11-27 22:24   ` Pedro Falcato
  3 siblings, 1 reply; 13+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-11-27 19:45 UTC (permalink / raw)
  To: Jan Harkes, coda, Andrew Morton, David Rheinsberg, Hugh Dickins,
	Vlastimil Babka, Liam R. Howlett, David Hildenbrand, Jann Horn,
	Alex Shi, Johannes Weiner, Pedro Falcato, David Rientjes,
	Rik van Riel, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Jonathan Corbet, Shuah Khan, Randy Dunlap,
	Michał Mirosław, Arnd Bergmann, Stephen Rothwell,
	Muhammad Usama Anjum
  Cc: codalist, linux-mm, Andrei Vagin, linux-doc, linux-kernel,
	linux-fsdevel, Lorenzo Stoakes (ARM), Chris Mason

The abomination known as PM_SCAN_WP_MATCHING, which makes PAGEMAP_SCAN not
scan but mutate for eldritch uffd-wp purposes, also lacks a permission
check when scanning the mm of another process.

It defaults to PTRACE_MODE_READ, which is insufficient for this accursed
operation.

Therefore actually check for permissions correctly, and also make sure the
mm is as expected when doing so.

As it stands, a task may currently manipulate page tables of the remote
process, write-protecting uffd-wp marked pages, allocating page tables and
splitting THP, and invoking MMU notifiers as needed.

The check is made at the time of the ioctl against its caller, as
lock_trace() does for other /proc files, rather than at open.

A tracker which passes READ but not ATTACH on another process now gets
EACCES for PM_SCAN_WP_MATCHING, whether it opened the pagemap file itself
or was handed the descriptor.

Also update the pagemap documentation to reflect the change.

Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
 Documentation/admin-guide/mm/pagemap.rst |  4 ++-
 fs/proc/task_mmu.c                       | 47 ++++++++++++++++++++++++++++----
 2 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/Documentation/admin-guide/mm/pagemap.rst b/Documentation/admin-guide/mm/pagemap.rst
index 77447c454632..f000296e5ab3 100644
--- a/Documentation/admin-guide/mm/pagemap.rst
+++ b/Documentation/admin-guide/mm/pagemap.rst
@@ -240,7 +240,9 @@ in this IOCTL:
 - Write-protect the pages. The ``PM_SCAN_WP_MATCHING`` is used to write-protect
   the pages of interest. The ``PM_SCAN_CHECK_WPASYNC`` aborts the operation if
   non-Async Write Protected pages are found. The ``PM_SCAN_WP_MATCHING`` can be
-  used with or without ``PM_SCAN_CHECK_WPASYNC``.
+  used with or without ``PM_SCAN_CHECK_WPASYNC``. On the pagemap file of another
+  process, ``PM_SCAN_WP_MATCHING`` requires that the caller is allowed to attach
+  to that process with ptrace. Otherwise the IOCTL fails with ``EACCES``.
 - Both of those operations can be combined into one atomic operation where we can
   get and write protect the pages as well.
 
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index e3d894ed2b91..19acdddd4b23 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -3120,11 +3120,41 @@ static long pagemap_scan_flush_buffer(struct pagemap_scan_private *p)
 	return n;
 }
 
-static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
+/*
+ * Ensure the caller has PTRACE_MODE_ATTACH_FSCREDS permission for wp scan, and
+ * that the file's mm still matches the process's.
+ */
+static int pagemap_scan_check_wp(struct file *file, struct mm_struct *mm)
+{
+	struct task_struct *task;
+	struct mm_struct *task_mm;
+	bool same_mm;
+
+	if (mm == current->mm)
+		return 0;
+
+	task = get_proc_task(file_inode(file));
+	if (!task)
+		return -ESRCH;
+
+	task_mm = mm_access(task, PTRACE_MODE_ATTACH_FSCREDS);
+	put_task_struct(task);
+	if (IS_ERR(task_mm))
+		return PTR_ERR(task_mm);
+
+	same_mm = task_mm == mm;
+	mmput(task_mm);
+
+	return same_mm ? 0 : -EACCES;
+}
+
+static long do_pagemap_scan(struct file *file, unsigned long uarg)
 {
 	struct pagemap_scan_private p = {0};
+	struct mm_struct *mm = file->private_data;
 	unsigned long walk_start;
 	size_t n_ranges_out = 0;
+	bool scan_wp;
 	int ret;
 
 	if (!mmget_not_zero(mm))
@@ -3134,6 +3164,13 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
 	if (ret)
 		goto out_mmput;
 
+	scan_wp = p.arg.flags & PM_SCAN_WP_MATCHING;
+	if (scan_wp) {
+		ret = pagemap_scan_check_wp(file, mm);
+		if (ret)
+			goto out_mmput;
+	}
+
 	p.masks_of_interest = p.arg.category_mask | p.arg.category_anyof_mask |
 			      p.arg.return_mask;
 	ret = pagemap_scan_init_bounce_buffer(&p);
@@ -3155,7 +3192,7 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
 			break;
 
 		/* Protection change for the range is going to happen. */
-		if (p.arg.flags & PM_SCAN_WP_MATCHING) {
+		if (scan_wp) {
 			mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 0,
 						mm, walk_start, p.arg.end);
 			mmu_notifier_invalidate_range_start(&range);
@@ -3164,7 +3201,7 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
 		ret = walk_page_range(mm, walk_start, p.arg.end,
 				      &pagemap_scan_ops, &p);
 
-		if (p.arg.flags & PM_SCAN_WP_MATCHING)
+		if (scan_wp)
 			mmu_notifier_invalidate_range_end(&range);
 
 		mmap_read_unlock(mm);
@@ -3201,11 +3238,9 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
 static long do_pagemap_cmd(struct file *file, unsigned int cmd,
 			   unsigned long arg)
 {
-	struct mm_struct *mm = file->private_data;
-
 	switch (cmd) {
 	case PAGEMAP_SCAN:
-		return do_pagemap_scan(mm, arg);
+		return do_pagemap_scan(file, arg);
 
 	default:
 		return -ENOIOCTLCMD;

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping
  2026-11-27 19:45 ` [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping Lorenzo Stoakes (ARM)
@ 2026-11-27 20:44   ` Andrew Morton
  2026-11-27 22:05     ` Pedro Falcato
  2026-11-28  1:10     ` Jan Harkes
  2026-11-27 22:01   ` Pedro Falcato
  1 sibling, 2 replies; 13+ messages in thread
From: Andrew Morton @ 2026-11-27 20:44 UTC (permalink / raw)
  To: Lorenzo Stoakes (ARM)
  Cc: Jan Harkes, coda, David Rheinsberg, Hugh Dickins, Vlastimil Babka,
	Liam R. Howlett, David Hildenbrand, Jann Horn, Alex Shi,
	Johannes Weiner, Pedro Falcato, David Rientjes, Rik van Riel,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko, Jonathan Corbet,
	Shuah Khan, Randy Dunlap, Michał Mirosław,
	Arnd Bergmann, Stephen Rothwell, Muhammad Usama Anjum, codalist,
	linux-mm, Andrei Vagin, linux-doc, linux-kernel, linux-fsdevel,
	Chris Mason, stable

On Fri, 27 Nov 2026 19:45:40 +0000 "Lorenzo Stoakes (ARM)" <ljs@kernel.org> wrote:

> Coda file systems stack differently from other stacked file systems.
> 
> On mmap(), coda_file_mmap() switches both the Coda inode's i_mapping and
> the Coda file's f_mapping for the mapping of the container file. This
> causes two problems.
> 
> Firstly, the container file may not remain pinned, at which point this may
> result in a dangling reference in coda_file->f_mapping, and so a
> use-after-free in mmap() or lseek().
> 
> This can happen when you open the same inode across two files, mmap()'ing
> in one, which assigns the i_mapping, and open()'ing another, which assigns
> that i_mapping to the file's f_mapping, if the cache manager gave the two
> files different containers. Each file pins only its own container.

Yeah, I hit this while getting
tools/testing/selftests/mm/memory-failure.c to work on all Linux
filesystems.

> --- a/fs/coda/file.c
> +++ b/fs/coda/file.c
> @@ -182,7 +182,6 @@ coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
>  
>  	cii = ITOC(coda_inode);
>  	spin_lock(&cii->c_lock);
> -	coda_file->f_mapping = host_file->f_mapping;
>  	if (coda_inode->i_mapping == &coda_inode->i_data)
>  		coda_inode->i_mapping = host_inode->i_mapping;
>  

I had this change.

> @@ -248,6 +247,19 @@ int coda_open(struct inode *coda_inode, struct file *coda_file)
>  
>  	host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
>  
> +	/*
> +	 * On mmap coda_inode->i_mapping is updated to point at the mapping of
> +	 * the mmap()'d file's container, and do_dentry_open() propagates that
> +	 * to coda_file->f_mapping.
> +	 *
> +	 * However coda_file's container may differ, in which case the lifetime
> +	 * of that mapping may not match that of coda_file.
> +	 *
> +	 * To avoid a dangling pointer, reassign the mapping to the canonical
> +	 * one.
> +	 */
> +	coda_file->f_mapping = &coda_inode->i_data;
> +
>  	cfi->cfi_magic = CODA_MAGIC;
>  	cfi->cfi_mapcount = 0;
>  	cfi->cfi_container = host_file;

But not this one.

Is it truly needed, or is it a future-safe defensive thing?

If the latter, Tested-by-me:.  If the former, Mostly-tested-by:me. 
Either way, Acked-by:me.


It's always seemed a big wart that file->f_mapping even exists.  We
should get that via file->f_inode->i_mapping!  But you-know-what tells
me that "/dev/mem sets the opened file's f_mapping to the shared
I/O-memory mapping; /proc/bus/pci does the same.  Their f_inode still
identifies the device or proc entry" so sigh.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping
  2026-11-27 19:45 ` [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping Lorenzo Stoakes (ARM)
  2026-11-27 20:44   ` Andrew Morton
@ 2026-11-27 22:01   ` Pedro Falcato
  1 sibling, 0 replies; 13+ messages in thread
From: Pedro Falcato @ 2026-11-27 22:01 UTC (permalink / raw)
  To: Lorenzo Stoakes (ARM)
  Cc: Jan Harkes, coda, Andrew Morton, David Rheinsberg, Hugh Dickins,
	Vlastimil Babka, Liam R. Howlett, David Hildenbrand, Jann Horn,
	Alex Shi, Johannes Weiner, David Rientjes, Rik van Riel,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko, Jonathan Corbet,
	Shuah Khan, Randy Dunlap, Michał Mirosław,
	Arnd Bergmann, Stephen Rothwell, Muhammad Usama Anjum, codalist,
	linux-mm, Andrei Vagin, linux-doc, linux-kernel, linux-fsdevel,
	Chris Mason, stable

On Fri, Nov 27, 2026 at 07:45:40PM +0000, Lorenzo Stoakes (ARM) wrote:
> Coda file systems stack differently from other stacked file systems.
> 
> On mmap(), coda_file_mmap() switches both the Coda inode's i_mapping and
> the Coda file's f_mapping for the mapping of the container file. This
> causes two problems.
> 
> Firstly, the container file may not remain pinned, at which point this may
> result in a dangling reference in coda_file->f_mapping, and so a
> use-after-free in mmap() or lseek().
> 
> This can happen when you open the same inode across two files, mmap()'ing
> in one, which assigns the i_mapping, and open()'ing another, which assigns
> that i_mapping to the file's f_mapping, if the cache manager gave the two
> files different containers. Each file pins only its own container.
> 
> Consider (A, B = separate open files of the same inode):
> 
>     A, with container C1            B, with container C2
>     ====================            ====================
>     mmap()
>       coda_file_mmap()
>         i_mapping = C1's mapping
>                                     open()
>                                       do_dentry_open()
>                                         f_mapping = i_mapping
>                                         coda_open()
>                                           cfi_container = C2
>     munmap()
>     close()
>       coda_release()
>         i_mapping = &i_data
>         fput(C1), the last one
>                                     mmap()
>                                       mmap_region()
>                                         mapping_map_writable(f_mapping)
> 
> Secondly, mmap_region() takes a temporary i_mmap_writable count on
> file->f_mapping ahead of the mmap hook, and drops it on file->f_mapping
> afterwards. As the hook changes f_mapping in between, the count is dropped
> on the container's mapping rather than on the mapping it was taken on:
> 
>     i_mmap_writable of                          Coda  container
>     mmap_region()
>       mapping_map_writable(file->f_mapping)       1       0
>       __mmap_region()
>         coda_file_mmap()
>           coda_file->f_mapping = host_file->f_mapping
>           vma->vm_file = get_file(host_file)
>         vma_link_file()
>           mapping_allow_writable()                1       1
>       mapping_unmap_writable(file->f_mapping)     1       0
>     munmap()
>       mapping_unmap_writable()                    1      -1
> 
> The container's count is negative after munmap(), so the next shared
> mapping of that Coda file, if it is open for writing, fails with -EPERM.
> 
> Fix both issues by no longer updating coda_file->f_mapping in
> coda_file_mmap(), and by having coda_open() always assign the canonical
> mapping of the inode, as do_dentry_open() may have propagated an i_mapping
> which coda_file_mmap() redirected.

This might be the most confusing thing I've read this year?

Anyway, in the sense that filesystems definitely shouldn't be poking f_mapping
after open:

Acked-by: Pedro Falcato <pfalcato@suse.de>
 

-- 
Pedro

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping
  2026-11-27 20:44   ` Andrew Morton
@ 2026-11-27 22:05     ` Pedro Falcato
  2026-11-28  1:10     ` Jan Harkes
  1 sibling, 0 replies; 13+ messages in thread
From: Pedro Falcato @ 2026-11-27 22:05 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Lorenzo Stoakes (ARM), Jan Harkes, coda, David Rheinsberg,
	Hugh Dickins, Vlastimil Babka, Liam R. Howlett, David Hildenbrand,
	Jann Horn, Alex Shi, Johannes Weiner, David Rientjes,
	Rik van Riel, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Jonathan Corbet, Shuah Khan, Randy Dunlap,
	Michał Mirosław, Arnd Bergmann, Stephen Rothwell,
	Muhammad Usama Anjum, codalist, linux-mm, Andrei Vagin, linux-doc,
	linux-kernel, linux-fsdevel, Chris Mason, stable

On Fri, Nov 27, 2026 at 12:44:28PM -0800, Andrew Morton wrote:
> On Fri, 27 Nov 2026 19:45:40 +0000 "Lorenzo Stoakes (ARM)" <ljs@kernel.org> wrote:
> 
> > Coda file systems stack differently from other stacked file systems.
> > 
> > On mmap(), coda_file_mmap() switches both the Coda inode's i_mapping and
> > the Coda file's f_mapping for the mapping of the container file. This
> > causes two problems.
> > 
> > Firstly, the container file may not remain pinned, at which point this may
> > result in a dangling reference in coda_file->f_mapping, and so a
> > use-after-free in mmap() or lseek().
> > 
> > This can happen when you open the same inode across two files, mmap()'ing
> > in one, which assigns the i_mapping, and open()'ing another, which assigns
> > that i_mapping to the file's f_mapping, if the cache manager gave the two
> > files different containers. Each file pins only its own container.
> 
> Yeah, I hit this while getting
> tools/testing/selftests/mm/memory-failure.c to work on all Linux
> filesystems.
> 
> > --- a/fs/coda/file.c
> > +++ b/fs/coda/file.c
> > @@ -182,7 +182,6 @@ coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
> >  
> >  	cii = ITOC(coda_inode);
> >  	spin_lock(&cii->c_lock);
> > -	coda_file->f_mapping = host_file->f_mapping;
> >  	if (coda_inode->i_mapping == &coda_inode->i_data)
> >  		coda_inode->i_mapping = host_inode->i_mapping;
> >  
> 
> I had this change.
> 
> > @@ -248,6 +247,19 @@ int coda_open(struct inode *coda_inode, struct file *coda_file)
> >  
> >  	host_file->f_flags |= coda_file->f_flags & (O_APPEND | O_SYNC);
> >  
> > +	/*
> > +	 * On mmap coda_inode->i_mapping is updated to point at the mapping of
> > +	 * the mmap()'d file's container, and do_dentry_open() propagates that
> > +	 * to coda_file->f_mapping.
> > +	 *
> > +	 * However coda_file's container may differ, in which case the lifetime
> > +	 * of that mapping may not match that of coda_file.
> > +	 *
> > +	 * To avoid a dangling pointer, reassign the mapping to the canonical
> > +	 * one.
> > +	 */
> > +	coda_file->f_mapping = &coda_inode->i_data;
> > +
> >  	cfi->cfi_magic = CODA_MAGIC;
> >  	cfi->cfi_mapcount = 0;
> >  	cfi->cfi_container = host_file;
> 
> But not this one.
> 
> Is it truly needed, or is it a future-safe defensive thing?
> 
> If the latter, Tested-by-me:.  If the former, Mostly-tested-by:me. 
> Either way, Acked-by:me.
> 
> 
> It's always seemed a big wart that file->f_mapping even exists.  We
> should get that via file->f_inode->i_mapping!  But you-know-what tells
> me that "/dev/mem sets the opened file's f_mapping to the shared
> I/O-memory mapping; /proc/bus/pci does the same.  Their f_inode still
> identifies the device or proc entry" so sigh.

FWIW, I think it's a rather nice part of the Linux VFS design: "file" and
"inode" are separated well enough to allow for these kinds of shenanigans
without awful hacks (and, yes, this is also present for something standard
like a block device on /dev; /dev is a tmpfs mount, so the inode is tmpfs, but
the mapping and fops cleanly point to block).

But stacked filesystems, admittedly, do give me motion sickness.

-- 
Pedro

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 2/4] mm/madvise: don't declare OOM for MADV_WILLNEED swapin
  2026-11-27 19:45 ` [PATCH 2/4] mm/madvise: don't declare OOM for MADV_WILLNEED swapin Lorenzo Stoakes (ARM)
@ 2026-11-27 22:14   ` Pedro Falcato
  0 siblings, 0 replies; 13+ messages in thread
From: Pedro Falcato @ 2026-11-27 22:14 UTC (permalink / raw)
  To: Lorenzo Stoakes (ARM)
  Cc: Jan Harkes, coda, Andrew Morton, David Rheinsberg, Hugh Dickins,
	Vlastimil Babka, Liam R. Howlett, David Hildenbrand, Jann Horn,
	Alex Shi, Johannes Weiner, David Rientjes, Rik van Riel,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko, Jonathan Corbet,
	Shuah Khan, Randy Dunlap, Michał Mirosław,
	Arnd Bergmann, Stephen Rothwell, Muhammad Usama Anjum, codalist,
	linux-mm, Andrei Vagin, linux-doc, linux-kernel, linux-fsdevel,
	Chris Mason

On Fri, Nov 27, 2026 at 07:45:41PM +0000, Lorenzo Stoakes (ARM) wrote:
> From: Chris Mason <mason@kernel.org>
> 
> MADV_WILLNEED on memory in swap can OOM kill a task when the memcg that
> the swap entries are charged to is at its limit.
> 
> The hint is only a prefetch, so it has to give up on a page when memory
> is short. But swapin_walk_pmd_entry() and shmem_swapin_range() call
> read_swap_cache_async() with GFP_HIGHUSER_MOVABLE and with the gfp mask
> of the mapping, so the allocation and the memcg charge can both go as
> far as the OOM killer.
> 
> The charge goes to the memcg of the swap entry, which need not be the
> caller's.
> 
> The fix used here is to allocate with the gfp mask of readahead, and to
> flush the block plug when a page fails. madvise_do_behavior() plugs the
> whole walk, so the swap writes of reclaim can sit in that plug. Their
> folios stay under writeback, and with no retries the rest of the range
> can fail as well.

How about we don't do that? Something something separation of concerns.
If we want this functionality (and we very well might), I would suggest
we do this in reclaim code itself. Something like

blk_finish_plug_or_flush().

Because I really don't want to set the precedent of making every direct reclaim
caller aware of block plugs.

Either that, or we let it fail.

> 
> The hint can now skip pages when memory is short, whether in a memcg at
> its limit or on the system as a whole, since neither the allocation nor
> the memcg charge is retried any longer.
> 
> This applies in particular where reclaim has to write to swap to make
> room. Skipped pages stay in swap until a fault reads them.
> 
> Fixes: 4c6355b25e8b ("mm: memcontrol: charge swapin pages on instantiation")
> Signed-off-by: Chris Mason <mason@kernel.org>
> [ ljs: commit message tweak ]
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

Aside from the above:
Reviewed-by: Pedro Falcato <pfalcato@suse.de> 

-- 
Pedro

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 3/4] mm/mprotect: fix unpaired notifier start at address 0
  2026-11-27 19:45 ` [PATCH 3/4] mm/mprotect: fix unpaired notifier start at address 0 Lorenzo Stoakes (ARM)
@ 2026-11-27 22:15   ` Pedro Falcato
  0 siblings, 0 replies; 13+ messages in thread
From: Pedro Falcato @ 2026-11-27 22:15 UTC (permalink / raw)
  To: Lorenzo Stoakes (ARM)
  Cc: Jan Harkes, coda, Andrew Morton, David Rheinsberg, Hugh Dickins,
	Vlastimil Babka, Liam R. Howlett, David Hildenbrand, Jann Horn,
	Alex Shi, Johannes Weiner, David Rientjes, Rik van Riel,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko, Jonathan Corbet,
	Shuah Khan, Randy Dunlap, Michał Mirosław,
	Arnd Bergmann, Stephen Rothwell, Muhammad Usama Anjum, codalist,
	linux-mm, Andrei Vagin, linux-doc, linux-kernel, linux-fsdevel,
	Chris Mason

On Fri, Nov 27, 2026 at 07:45:42PM +0000, Lorenzo Stoakes (ARM) wrote:
> From: Chris Mason <mason@kernel.org>
> 
> A protection change on a VMA that starts at address 0 can leave an MMU
> notifier invalidation open forever.
> 
> Waiters for its end, such as kvm_swap_active_memslots() and
> mmu_interval_read_begin(), then sleep uninterruptibly and never wake up.
> 
> Fix by using a boolean value to track this instead of the start address of
> the range.
> 
> Fixes: a5338093bfb4 ("mm: move mmu notifier call from change_protection to change_pmd_range")
> Signed-off-by: Chris Mason <mason@kernel.org>
> [ ljs: commit msg tweak ]
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

Reviewed-by: Pedro Falcato <pfalcato@suse.de>

-- 
Pedro

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 4/4] fs/proc/task_mmu: write-protect only with ptrace attach
  2026-11-27 19:45 ` [PATCH 4/4] fs/proc/task_mmu: write-protect only with ptrace attach Lorenzo Stoakes (ARM)
@ 2026-11-27 22:24   ` Pedro Falcato
  0 siblings, 0 replies; 13+ messages in thread
From: Pedro Falcato @ 2026-11-27 22:24 UTC (permalink / raw)
  To: Lorenzo Stoakes (ARM)
  Cc: Jan Harkes, coda, Andrew Morton, David Rheinsberg, Hugh Dickins,
	Vlastimil Babka, Liam R. Howlett, David Hildenbrand, Jann Horn,
	Alex Shi, Johannes Weiner, David Rientjes, Rik van Riel,
	Mike Rapoport, Suren Baghdasaryan, Michal Hocko, Jonathan Corbet,
	Shuah Khan, Randy Dunlap, Michał Mirosław,
	Arnd Bergmann, Stephen Rothwell, Muhammad Usama Anjum, codalist,
	linux-mm, Andrei Vagin, linux-doc, linux-kernel, linux-fsdevel,
	Chris Mason

On Fri, Nov 27, 2026 at 07:45:43PM +0000, Lorenzo Stoakes (ARM) wrote:
> The abomination known as PM_SCAN_WP_MATCHING, which makes PAGEMAP_SCAN not
> scan but mutate for eldritch uffd-wp purposes, also lacks a permission
> check when scanning the mm of another process.
> 
> It defaults to PTRACE_MODE_READ, which is insufficient for this accursed
> operation.
> 
> Therefore actually check for permissions correctly, and also make sure the
> mm is as expected when doing so.
> 
> As it stands, a task may currently manipulate page tables of the remote
> process, write-protecting uffd-wp marked pages, allocating page tables and
> splitting THP, and invoking MMU notifiers as needed.
> 
> The check is made at the time of the ioctl against its caller, as
> lock_trace() does for other /proc files, rather than at open.
> 
> A tracker which passes READ but not ATTACH on another process now gets
> EACCES for PM_SCAN_WP_MATCHING, whether it opened the pagemap file itself
> or was handed the descriptor.
> 
> Also update the pagemap documentation to reflect the change.
> 
> Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
> Reported-by: Chris Mason <mason@kernel.org>
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

Reviewed-by: Pedro Falcato <pfalcato@suse.de>

-- 
Pedro

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping
  2026-11-27 20:44   ` Andrew Morton
  2026-11-27 22:05     ` Pedro Falcato
@ 2026-11-28  1:10     ` Jan Harkes
  2026-11-28  6:23       ` Andrew Morton
  1 sibling, 1 reply; 13+ messages in thread
From: Jan Harkes @ 2026-11-28  1:10 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Lorenzo Stoakes (ARM), David Rheinsberg, Hugh Dickins,
	Vlastimil Babka, Liam R. Howlett, David Hildenbrand, Jann Horn,
	Alex Shi, Johannes Weiner, Pedro Falcato, David Rientjes,
	Rik van Riel, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Jonathan Corbet, Shuah Khan, Randy Dunlap,
	Michał Mirosław, Arnd Bergmann, Stephen Rothwell,
	Muhammad Usama Anjum, linux-mm, Andrei Vagin, linux-doc,
	linux-kernel, linux-fsdevel, Chris Mason, stable

On Fri, Nov 27, 2026 at 12:44:28PM -0800, Andrew Morton wrote:
> On Fri, 27 Nov 2026 19:45:40 +0000 "Lorenzo Stoakes (ARM)" <ljs@kernel.org> wrote:
> 
> > Coda file systems stack differently from other stacked file systems.
> > 
> > On mmap(), coda_file_mmap() switches both the Coda inode's i_mapping and
> > the Coda file's f_mapping for the mapping of the container file. This
> > causes two problems.
> > 
> > Firstly, the container file may not remain pinned, at which point this may
> > result in a dangling reference in coda_file->f_mapping, and so a
> > use-after-free in mmap() or lseek().
> > 
> > This can happen when you open the same inode across two files, mmap()'ing
> > in one, which assigns the i_mapping, and open()'ing another, which assigns
> > that i_mapping to the file's f_mapping, if the cache manager gave the two
> > files different containers. Each file pins only its own container.

Coda uses open-close consistency, so between the first open and the last
close the cache manager does not make any changes to the cached copy, so
every open is guaranteed to get the exact same container file.

Now that happens to be the Coda cache manager, I guess changes that
would enforce this at the kernel module level can be useful, but
dropping f_mapping, I think will break mmap for us.

> Yeah, I hit this while getting
> tools/testing/selftests/mm/memory-failure.c to work on all Linux
> filesystems.

Did you test this without the actual Coda userspace? What was the
cache consistency model your userspace was using?

Jan

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping
  2026-11-28  1:10     ` Jan Harkes
@ 2026-11-28  6:23       ` Andrew Morton
  0 siblings, 0 replies; 13+ messages in thread
From: Andrew Morton @ 2026-11-28  6:23 UTC (permalink / raw)
  To: Jan Harkes
  Cc: Lorenzo Stoakes (ARM), David Rheinsberg, Hugh Dickins,
	Vlastimil Babka, Liam R. Howlett, David Hildenbrand, Jann Horn,
	Alex Shi, Johannes Weiner, Pedro Falcato, David Rientjes,
	Rik van Riel, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Jonathan Corbet, Shuah Khan, Randy Dunlap,
	Michał Mirosław, Arnd Bergmann, Stephen Rothwell,
	Muhammad Usama Anjum, linux-mm, Andrei Vagin, linux-doc,
	linux-kernel, linux-fsdevel, Chris Mason, stable

On Fri, 27 Nov 2026 20:10:13 -0500 Jan Harkes <jaharkes@cs.cmu.edu> wrote:

> On Fri, Nov 27, 2026 at 12:44:28PM -0800, Andrew Morton wrote:
> > On Fri, 27 Nov 2026 19:45:40 +0000 "Lorenzo Stoakes (ARM)" <ljs@kernel.org> wrote:
> > 
> > > Coda file systems stack differently from other stacked file systems.
> > > 
> > > On mmap(), coda_file_mmap() switches both the Coda inode's i_mapping and
> > > the Coda file's f_mapping for the mapping of the container file. This
> > > causes two problems.
> > > 
> > > Firstly, the container file may not remain pinned, at which point this may
> > > result in a dangling reference in coda_file->f_mapping, and so a
> > > use-after-free in mmap() or lseek().
> > > 
> > > This can happen when you open the same inode across two files, mmap()'ing
> > > in one, which assigns the i_mapping, and open()'ing another, which assigns
> > > that i_mapping to the file's f_mapping, if the cache manager gave the two
> > > files different containers. Each file pins only its own container.
> 
> Coda uses open-close consistency, so between the first open and the last
> close the cache manager does not make any changes to the cached copy, so
> every open is guaranteed to get the exact same container file.
> 
> Now that happens to be the Coda cache manager, I guess changes that
> would enforce this at the kernel module level can be useful, but
> dropping f_mapping, I think will break mmap for us.
> 
> > Yeah, I hit this while getting
> > tools/testing/selftests/mm/memory-failure.c to work on all Linux
> > filesystems.
> 
> Did you test this without the actual Coda userspace? What was the
> cache consistency model your userspace was using?

Couldn't figure out how to get Venus working (I wouldn't know what a
coda is if it bit me on the foot!) so a mockup thing over tmpfs was
used in place.

<tries harder>

OK, reran with real Venus and a writable file on the server.  The below
repro makes a writable shared mapping, then opens and maps it again
while the first mapping is live.  All 300 first mappings succeeded and
299 of the 300 second mappings failed with EPERM.  This uses one Coda
file and doesn't depend on Venus changing containers between opens.

That one-line-removal fixes this up.

: Coda changes file->f_mapping to the container file's mapping inside
: coda_file_mmap(). mmap_region() takes a temporary writable-mapping count
: before calling ->mmap() and releases it afterward. Coda's assignment
: makes those two operations use different mappings, leaving the
: container's count too low. Subsequent writable MAP_SHARED mappings can
: fail with -EPERM.
: 
: I reproduced this with real Venus and a writable file on a Coda server.
: Without the patch, 299 of 300 second mappings failed with -EPERM when
: another mapping of the same file was live. With the one-line-removal
: all 300 succeeded, and mapped writes passed the readback check.




I also hit a lockdep splat along the way.  That's also appended along
with a slop-coded "fix" which I haven't even looked at, sorry.  But it
did make the splat go away.




// SPDX-License-Identifier: GPL-2.0
/*
 * Reproduce Coda writable MAP_SHARED failures with a real Venus.
 * Usage: ./coda-mmap-repro /coda/REALM/test-directory/file [iterations]
 * The named file is created/truncated. Use a disposable writable test path.
 */
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/magic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/statfs.h>
#include <unistd.h>

int main(int argc, char **argv)
{
	long pagesize = sysconf(_SC_PAGESIZE), loops = 300;
	long first_ok = 0, second_ok = 0, first_eperm = 0;
	long second_eperm = 0, other = 0, coherence_ok = 0;
	struct statfs fs;
	char *end;
	int setup;

	if (argc < 2 || argc > 3 || pagesize < 1) {
		fprintf(stderr, "usage: %s CODA-TEST-FILE [iterations]\n", argv[0]);
		return 2;
	}
	if (argc == 3) {
		errno = 0;
		loops = strtol(argv[2], &end, 10);
		if (errno || *end || loops < 1 || loops > 1000000) {
			fprintf(stderr, "invalid iteration count\n");
			return 2;
		}
	}
	if (statfs(argv[1], &fs) < 0) {
		/* The file need not exist yet: check its parent in the runner. */
		if (errno != ENOENT) {
			perror("statfs");
			return 2;
		}
	} else if ((unsigned long)fs.f_type != CODA_SUPER_MAGIC) {
		fprintf(stderr, "%s is not on a Coda filesystem\n", argv[1]);
		return 2;
	}
	setup = open(argv[1], O_CREAT | O_TRUNC | O_RDWR, 0600);
	if (setup < 0 || ftruncate(setup, pagesize) < 0 || close(setup) < 0) {
		perror("create/truncate");
		return 2;
	}
	if (statfs(argv[1], &fs) < 0 ||
	    (unsigned long)fs.f_type != CODA_SUPER_MAGIC) {
		fprintf(stderr, "test file is not on a Coda filesystem\n");
		return 2;
	}
	printf("coda_magic=0x%lx path=%s\n", (unsigned long)fs.f_type, argv[1]);
	for (long i = 0; i < loops; i++) {
		int first = open(argv[1], O_RDWR), second;
		char *a, *b, check;
		if (first < 0) {
			perror("first open");
			return 2;
		}
		a = mmap(NULL, pagesize, PROT_READ | PROT_WRITE,
			 MAP_SHARED, first, 0);
		if (a == MAP_FAILED) {
			if (errno == EPERM) first_eperm++;
			else { fprintf(stderr, "first mmap %ld: %s\n", i, strerror(errno)); other++; }
			close(first);
			continue;
		}
		first_ok++;
		a[0] = (char)('A' + i % 26);
		/* Open while the first writable mapping is still live. */
		second = open(argv[1], O_RDWR);
		if (second < 0) {
			perror("second open");
			return 2;
		}
		b = mmap(NULL, pagesize, PROT_READ | PROT_WRITE,
			 MAP_SHARED, second, 0);
		if (b == MAP_FAILED) {
			if (errno == EPERM) second_eperm++;
			else { fprintf(stderr, "second mmap %ld: %s\n", i, strerror(errno)); other++; }
		} else {
			second_ok++;
			if (b[0] != a[0]) {
				fprintf(stderr, "mapping coherence mismatch at %ld\n", i);
				other++;
			} else {
				b[0] = (char)('a' + i % 26);
				if (msync(b, pagesize, MS_SYNC) < 0 ||
				    pread(first, &check, 1, 0) != 1 ||
				    a[0] != b[0] || check != b[0]) {
					fprintf(stderr, "write/msync/read mismatch at %ld: %s\n",
						i, strerror(errno));
					other++;
				} else coherence_ok++;
			}
			if (munmap(b, pagesize) < 0) { perror("munmap second"); return 2; }
		}
		if (close(second) < 0 || munmap(a, pagesize) < 0 ||
		    close(first) < 0) {
			perror("cleanup");
			return 2;
		}
	}
	printf("iterations=%ld first_ok=%ld first_EPERM=%ld second_ok=%ld "
	       "second_EPERM=%ld coherence_ok=%ld other=%ld\n",
	       loops, first_ok, first_eperm, second_ok, second_eperm,
	       coherence_ok, other);
	return first_eperm || second_eperm || other ? 1 : 0;
}



From: Andrew Morton <akpm@linux-foundation.org>
Subject: coda: copy Venus requests to userspace without holding vc_mutex
Date: Mon, 23 Nov 2026 15:07:44 -0800

coda_psdev_read() holds vc_mutex while copying an upcall into the Venus
process, and copy_to_user() may take mmap_lock.  coda_file_mmap() runs
with mmap_lock held and its access-intent upcall takes vc_mutex.  Lockdep
reports the inversion:

  WARNING: possible circular locking dependency detected
  init/1 is trying to acquire lock:
  (&(&coda_comms[i])->vc_mutex){+.+.}-{4:4}, at: coda_upcall+0x2e/0x437

  but task is already holding lock:
  (&mm->mmap_lock){++++}-{4:4}, at: vm_mmap_pgoff+0x71/0x10c

  which lock already depends on the new lock.

  -> #1 (&mm->mmap_lock){++++}-{4:4}:
         __might_fault+0x4e/0x6a
         _copy_to_user+0x22/0x4d
         coda_psdev_read+0x19a/0x203
         ksys_read+0x72/0xa2

  -> #0 (&(&coda_comms[i])->vc_mutex){+.+.}-{4:4}:
         coda_upcall+0x2e/0x437
         venus_access_intent+0xc6/0xfa
         coda_file_mmap+0xa2/0x1ef
         __mmap_region+0x87d/0xcac
         mmap_region+0xc3/0xe5
         do_mmap+0x5a4/0x5e3
         vm_mmap_pgoff+0xd4/0x10c
         ksys_mmap_pgoff+0x6c/0x95

   Possible unsafe locking scenario:

         CPU0                    CPU1
         ----                    ----
    lock(&mm->mmap_lock);
                                 lock(&(&coda_comms[i])->vc_mutex);
                                 lock(&mm->mmap_lock);
    lock(&(&coda_comms[i])->vc_mutex);

This is not a deadlock with a normal Venus, because the fault takes
Venus's mmap_lock and the task which calls mmap() holds its own.  The two
are the same lock only when the mmap() caller shares its mm with the task
which reads the Coda device.

Copy the upcall into a private buffer while holding vc_mutex.  Keep the
request pending if the allocation fails.  Once the request has been moved
to the processing list (or freed, for an asynchronous request), drop
vc_mutex before copying the private buffer to Venus.  The copy no longer
depends on the lifetime of the original request, which its sender is free
to remove once vc_mutex has been dropped.

The allocation is GFP_KERNEL, as are the ones which coda_upcall() already
makes under vc_mutex.

Return -EFAULT when the userspace copy fails.  Previously the nonzero
count took precedence over retval, reporting a successful read even though
the copy failed.

Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

 fs/coda/psdev.c |   28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

--- a/fs/coda/psdev.c~coda-avoid-user-page-faults-under-vc_mutex
+++ a/fs/coda/psdev.c
@@ -207,6 +207,7 @@ static ssize_t coda_psdev_read(struct fi
 	DECLARE_WAITQUEUE(wait, current);
         struct venus_comm *vcp = (struct venus_comm *) file->private_data;
         struct upc_req *req;
+	void *data;
 	ssize_t retval = 0, count = 0;
 
 	if (nbytes == 0)
@@ -238,7 +239,6 @@ static ssize_t coda_psdev_read(struct fi
 		goto out;
 
 	req = list_entry(vcp->vc_pending.next, struct upc_req,uc_chain);
-	list_del(&req->uc_chain);
 
 	/* Move the input args into userspace */
 	count = req->uc_inSize;
@@ -248,21 +248,33 @@ static ssize_t coda_psdev_read(struct fi
 		count = nbytes;
         }
 
-	if (copy_to_user(buf, req->uc_data, count))
-	        retval = -EFAULT;
-        
+	/*
+	 * The request can be freed when its reply arrives.  Keep a copy so
+	 * that the userspace fault below does not run under vc_mutex.
+	 */
+	data = kvmemdup(req->uc_data, count, GFP_KERNEL);
+	if (!data) {
+		retval = -ENOMEM;
+		goto out;
+	}
+	list_del(&req->uc_chain);
+
 	/* If request was not a signal, enqueue and don't free */
 	if (!(req->uc_flags & CODA_REQ_ASYNC)) {
 		req->uc_flags |= CODA_REQ_READ;
 		list_add_tail(&(req->uc_chain), &vcp->vc_processing);
-		goto out;
+	} else {
+		kvfree(req->uc_data);
+		kfree(req);
 	}
 
-	kvfree(req->uc_data);
-	kfree(req);
+	mutex_unlock(&vcp->vc_mutex);
+	retval = copy_to_user(buf, data, count) ? -EFAULT : count;
+	kvfree(data);
+	return retval;
 out:
 	mutex_unlock(&vcp->vc_mutex);
-	return (count ? count : retval);
+	return retval;
 }
 
 static int coda_psdev_open(struct inode * inode, struct file * file)
_


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2026-11-28  6:23 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 19:45 [PATCH 0/4] mm: AI-generated fixes for coda, madvise, mprotect and PAGEMAP_SCAN Lorenzo Stoakes (ARM)
2026-11-27 19:45 ` [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping Lorenzo Stoakes (ARM)
2026-11-27 20:44   ` Andrew Morton
2026-11-27 22:05     ` Pedro Falcato
2026-11-28  1:10     ` Jan Harkes
2026-11-28  6:23       ` Andrew Morton
2026-11-27 22:01   ` Pedro Falcato
2026-11-27 19:45 ` [PATCH 2/4] mm/madvise: don't declare OOM for MADV_WILLNEED swapin Lorenzo Stoakes (ARM)
2026-11-27 22:14   ` Pedro Falcato
2026-11-27 19:45 ` [PATCH 3/4] mm/mprotect: fix unpaired notifier start at address 0 Lorenzo Stoakes (ARM)
2026-11-27 22:15   ` Pedro Falcato
2026-11-27 19:45 ` [PATCH 4/4] fs/proc/task_mmu: write-protect only with ptrace attach Lorenzo Stoakes (ARM)
2026-11-27 22:24   ` Pedro Falcato

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox