Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kernfs: fix false-positive WARN(nr_mmapped) in kernfs_drain_open_files

Prior to this change 'on->nr_mmapped' tracked the total number of
mmaps across all of its associated open files via kernfs_fop_mmap().
Thus if the file descriptor associated with a kernfs_open_file was
mmapped 10 times then we would have: 'of->mmapped = true' and
'of_on(of)->nr_mmapped = 10'.

The problem is that closing or draining a 'of->mmapped' file would
only decrement one from the 'of_on(of)->nr_mmapped' counter.

For e.g. we have this from kernfs_unlink_open_file():
if (of->mmapped)
on->nr_mmapped--;

The WARN_ON_ONCE(on->nr_mmapped) in kernfs_drain_open_files() is
easy to reproduce by:
1. opening a (mmap-able) kernfs file.
2. mmap-ing that file more than once (mapping just once masks the issue).
3. trigger a drain of that kernfs file.

Modulo out-of-tree patches I was able to trigger this reliably by
identifying pci device nodes in sysfs that have resource regions
that are mmap-able and that don't have any driver attached to them
(steps 1 and 2). For step 3 we can "echo 1 > remove" to trigger a
kernfs_drain.

Signed-off-by: Neel Natu <neelnatu@google.com>
Link: https://lore.kernel.org/r/20240127234636.609265-1-neelnatu@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Neel Natu and committed by
Greg Kroah-Hartman
05d8f255 4207b556

+5 -3
+5 -3
fs/kernfs/file.c
··· 483 483 goto out_put; 484 484 485 485 rc = 0; 486 - of->mmapped = true; 487 - of_on(of)->nr_mmapped++; 488 - of->vm_ops = vma->vm_ops; 486 + if (!of->mmapped) { 487 + of->mmapped = true; 488 + of_on(of)->nr_mmapped++; 489 + of->vm_ops = vma->vm_ops; 490 + } 489 491 vma->vm_ops = &kernfs_vm_ops; 490 492 out_put: 491 493 kernfs_put_active(of->kn);