* [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)
0 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, 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] 11+ 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 0 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 related [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 ` (2 more replies) 2026-11-27 22:01 ` Pedro Falcato 1 sibling, 3 replies; 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 2026-11-28 1:10 ` Jan Harkes 2026-11-30 14:20 ` Lorenzo Stoakes (ARM) 2 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 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-30 14:20 ` Lorenzo Stoakes (ARM) 2 siblings, 1 reply; 11+ 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] 11+ 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 2026-11-28 19:40 ` Jan Harkes 0 siblings, 1 reply; 11+ 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] 11+ messages in thread
* Re: [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping 2026-11-28 6:23 ` Andrew Morton @ 2026-11-28 19:40 ` Jan Harkes 2026-11-30 14:09 ` Lorenzo Stoakes (ARM) 0 siblings, 1 reply; 11+ messages in thread From: Jan Harkes @ 2026-11-28 19:40 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 10:23:08PM -0800, Andrew Morton wrote: > On Fri, 27 Nov 2026 20:10:13 -0500 Jan Harkes <jaharkes@cs.cmu.edu> wrote: > > > 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. Thank you so much for running that test. Rereading what I wrote, I'm sorry for my tone and truly appreciate the effort you put in reproducing the bug. Now I am really confused as to what the use of file->f_mapping actually is. I thought I understood how this mmap stuff worked, but clearly I do not. > 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. Sweet, it may be slop coded but it looks good. It fixes both the lockdep my moving the upcall copy_to_user outside of the locked region, but it also corrects an obvious bug in the error handling when copy_to_user fails. Thank you, Jan ^ 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-28 19:40 ` Jan Harkes @ 2026-11-30 14:09 ` Lorenzo Stoakes (ARM) 0 siblings, 0 replies; 11+ messages in thread From: Lorenzo Stoakes (ARM) @ 2026-11-30 14:09 UTC (permalink / raw) To: 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, linux-mm, Andrei Vagin, linux-doc, linux-kernel, linux-fsdevel, Chris Mason, stable On Sat, Nov 28, 2026 at 02:40:20PM -0500, Jan Harkes wrote: > Now I am really confused as to what the use of file->f_mapping actually > is. I thought I understood how this mmap stuff worked, but clearly I do not. In general, you shouldn't touch this at all :)) This class of bug is really 'mmap() is getting pointers to stuff it shouldn't get, and mmap hook changing internal state that should not be changed'. The other stacked file systems use a fundamentally different approach, but it's all very forgivable because coda did it first, and before any of that machinery was in place. I am gradually modernising mmap() into mmap_prepare(), and as part of that I'll have to fix up all the machinery. I guess I'll make suggestions then as to how to bring coda into the fold, though it may need a carve-out for backwards compatibility. One for the future though! :) -- Cheers, Lorenzo ^ 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 2026-11-28 1:10 ` Jan Harkes @ 2026-11-30 14:20 ` Lorenzo Stoakes (ARM) 2 siblings, 0 replies; 11+ messages in thread From: Lorenzo Stoakes (ARM) @ 2026-11-30 14:20 UTC (permalink / raw) To: Andrew Morton 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, 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? It's absolutely needed, yes. mmap does: if (coda_inode->i_mapping == &coda_inode->i_data) coda_inode->i_mapping = host_inode->i_mapping; And do_dentry_open() does: f->f_mapping = inode->i_mapping; So removing the coda_file->f_mapping assignment there doesn't save you. It only addresses things at the point of mmap not open(). So you can still end up with a dangling pointer. The fundamental issue here is that state with 1 lifetime (container mapping) is being tied to an inode with a separate lifetime. And there's nothing holding a reference or expressing the dependency, meaning you get a dangling pointer if the container manager does something insane. (BTW I actually merged 2 AI patches into one for this fix, the original solutions were kinda horrible, this makes it into one cohesive change). > > If the latter, Tested-by-me:. If the former, Mostly-tested-by:me. > Either way, Acked-by:me. Thanks! To make David's life easier (I assume he's taking?): Acked-by: Andrew Morton <akpm@linux-foundation.org> Tested-by: Andrew Morton <akpm@linux-foundation.org> > > > 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. > Yeah anything that is changing state like that like this is very sketchy. And we definitely shouldn't have allowed it. Or an mmap hook with a pointer to key kernel state... -- Cheers, Lorenzo ^ 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 2026-11-30 14:11 ` Lorenzo Stoakes (ARM) 1 sibling, 1 reply; 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
* Re: [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping 2026-11-27 22:01 ` Pedro Falcato @ 2026-11-30 14:11 ` Lorenzo Stoakes (ARM) 0 siblings, 0 replies; 11+ messages in thread From: Lorenzo Stoakes (ARM) @ 2026-11-30 14:11 UTC (permalink / raw) To: Pedro Falcato 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 10:01:32PM +0000, Pedro Falcato wrote: > 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? I completely rewrote it, you should have seen what it originally said :) Unfortunately it is _just_ confusing this. > > Anyway, in the sense that filesystems definitely shouldn't be poking f_mapping > after open: > > Acked-by: Pedro Falcato <pfalcato@suse.de> Thanks! > > > -- > Pedro -- Cheers, Lorenzo ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-11-30 14:20 UTC | newest] Thread overview: 11+ 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-28 19:40 ` Jan Harkes 2026-11-30 14:09 ` Lorenzo Stoakes (ARM) 2026-11-30 14:20 ` Lorenzo Stoakes (ARM) 2026-11-27 22:01 ` Pedro Falcato 2026-11-30 14:11 ` Lorenzo Stoakes (ARM)
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox