* [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; 11+ 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 [flat|nested] 11+ 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-27 22:01 ` Pedro Falcato
1 sibling, 1 reply; 11+ 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] 11+ 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
0 siblings, 0 replies; 11+ 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] 11+ 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; 11+ 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] 11+ 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; 11+ 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 [flat|nested] 11+ 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; 11+ 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] 11+ 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; 11+ 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 [flat|nested] 11+ 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; 11+ 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] 11+ 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; 11+ 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 [flat|nested] 11+ 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; 11+ 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] 11+ messages in thread