kernfs: Use RCU to access kernfs_node::{parent|name}.
Subsystems (9)
v8 cover kernfs: Use RCU to access kernfs_node::{parent|name}.
Hi,
This started as a bug report by Hillf Danton and aims to access kernfs_node::{name|parent} with RCU to avoid the lock during kernfs_path_from_node().
I've split the individual fixes in separate patches (#1 to #4). I've also split the ::parent and ::name RCU conversation into a single patch (#5 and #6).
v7…v8 https://lore.kernel.org/all/[email protected]/
- A user in selftests/bpf is fixed in #5 as part of the rename.
v6…v7 https://lore.kernel.org/all/[email protected]/
- Rebase on v6.14-rc1
v5…v6 https://lore.kernel.org/all/[email protected]/
- s/rdt_kn_get_name/rdt_kn_name/
- s/rdt_get_kn_parent_priv/rdt_kn_parent_priv/
- s/kn_get_priv/kn_priv/
- The comment, that has been removed in kernfs_put(), is back.
- Using rcu_access_pointer() in kernfs_activate_one() and kernfs_dir_pos() instead of kernfs_parent() where the pointer is not dereferenced but just compared.
v4…v5 https://lore.kernel.org/all/[email protected]/
- rdtgroup:
- Add a comment to rdt_get_kn_parent_priv() regarding lifetime of parent.
- Move individual rcu_dereference_check() invocations into rdt_kn_parent() with a comment on lifetime.
- Use rcu_access_pointer() in kernfs_to_rdtgroup() instead rcu_dereference_check(, true)
- s/kernfs_rcu_get_name/kernfs_rcu_name/
- Move all rcu_dereference_check() within kernfs into kernfs_parent() and extend its checks to have all cases in one spot. Document why each case makes sense.
- kernfs_notify_workfn(): Do unlocks in the reverse order of locks.
- Add kernfs_root_flags() and use it in cgroup's kn_get_priv() to check the right KERNFS_ROOT_INVARIANT_PARENT flag.
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Sebastian
Sebastian Andrzej Siewior (6):
kernfs: Acquire kernfs_rwsem in kernfs_notify_workfn().
kernfs: Acquire kernfs_rwsem in kernfs_get_parent_dentry().
kernfs: Acquire kernfs_rwsem in kernfs_node_dentry().
kernfs: Don't re-lock kernfs_root::kernfs_rwsem in
kernfs_fop_readdir().
kernfs: Use RCU to access kernfs_node::parent.
kernfs: Use RCU to access kernfs_node::name.
arch/x86/kernel/cpu/resctrl/internal.h | 5 + arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 14 +- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 73 +++--- fs/kernfs/dir.c | 211 ++++++++++-------- fs/kernfs/file.c | 6 +- fs/kernfs/kernfs-internal.h | 37 ++- fs/kernfs/mount.c | 21 +- fs/kernfs/symlink.c | 30 +-- fs/sysfs/dir.c | 2 +- fs/sysfs/file.c | 24 +- include/linux/kernfs.h | 14 +- kernel/cgroup/cgroup-v1.c | 2 +- kernel/cgroup/cgroup.c | 24 +- security/selinux/hooks.c | 7 +- .../selftests/bpf/progs/profiler.inc.h | 2 +- 15 files changed, 308 insertions(+), 164 deletions(-)
PATCH v8 1/6 kernfs: Acquire kernfs_rwsem in kernfs_notify_workfn().
kernfs_notify_workfn() dereferences kernfs_node::name and passes it later to fsnotify(). If the node is renamed then the previously observed name pointer becomes invalid.
Acquire kernfs_root::kernfs_rwsem to block renames of the node.
--- fs/kernfs/file.c | 2 ++ 1 file changed, 2 insertions(+)
fs/kernfs/file.c +2 -0
@@ -911,6 +911,7 @@ static void kernfs_notify_workfn(struct work_struct *work)/* kick fsnotify */ down_read (&root->kernfs_supers_rwsem); +down_read (&root->kernfs_rwsem);list_for_each_entry (info, &kernfs_root (kn)->supers, node) {struct kernfs_node *parent;struct inode *p_inode =NULL ;
@@ -947,6 +948,7 @@ static void kernfs_notify_workfn(struct work_struct *work)iput (inode); } +up_read (&root->kernfs_rwsem);up_read (&root->kernfs_supers_rwsem);kernfs_put (kn); goto repeat;
Signature · 1 line
2.47.2
PATCH v8 2/6 kernfs: Acquire kernfs_rwsem in kernfs_get_parent_dentry().
In reply to Sebastian Andrzej Siewiorkernfs_get_parent_dentry() passes kernfs_node::parent to kernfs_get_inode().
Acquire kernfs_root::kernfs_rwsem to ensure kernfs_node::parent isn't replaced during the operation.
--- fs/kernfs/mount.c | 2 ++ 1 file changed, 2 insertions(+)
fs/kernfs/mount.c +2 -0
@@ -145,7 +145,9 @@ static struct dentry *kernfs_fh_to_parent(struct super_block *sb,static struct dentry *kernfs_get_parent_dentry (struct dentry *child) {struct kernfs_node *kn =kernfs_dentry_node (child); +struct kernfs_root *root =kernfs_root (kn); +guard (rwsem_read)(&root->kernfs_rwsem);return d_obtain_alias (kernfs_get_inode (child->d_sb, kn->parent)); }
Signature · 1 line
2.47.2
PATCH v8 3/6 kernfs: Acquire kernfs_rwsem in kernfs_node_dentry().
In reply to Sebastian Andrzej Siewiorkernfs_node_dentry() passes kernfs_node::name tolookup_positive_unlocked().
Acquire kernfs_root::kernfs_rwsem to ensure the node is not renamed during the operation.
--- fs/kernfs/mount.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/kernfs/mount.c +4 -0
@@ -209,6 +209,7 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn, {struct dentry *dentry;struct kernfs_node *knparent; +struct kernfs_root *root;BUG_ON (sb->s_op != &kernfs_sops);
@@ -218,6 +219,9 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,if (!kn->parent)return dentry; + root =kernfs_root (kn); +guard (rwsem_read)(&root->kernfs_rwsem); + knparent =find_next_ancestor (kn,NULL );if (WARN_ON (!knparent)) {dput (dentry);
Signature · 1 line
2.47.2
PATCH v8 4/6 kernfs: Don't re-lock kernfs_root::kernfs_rwsem in kernfs_fop_readdir().
In reply to Sebastian Andrzej SiewiorThe readdir operation iterates over all entries and invokes dir_emit() for every entry passing kernfs_node::name as argument.
Since the name argument can change, and become invalid, the kernfs_root::kernfs_rwsem lock should not be dropped to prevent renames during the operation.
The lock drop around dir_emit() has been initially introduced in commit
1e5289c97bba2 ("sysfs: Cache the last sysfs_dirent to improve readdir scalability v2")
to avoid holding a global lock during a page fault. The lock drop is wrong since the support of renames and not a big burden since the lock is no longer global.
Don't re-acquire kernfs_root::kernfs_rwsem while copying the name to the userpace buffer.
--- fs/kernfs/dir.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/kernfs/dir.c +3 -3
@@ -1869,10 +1869,10 @@ static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx) file->private_data = pos;kernfs_get (pos); -up_read (&root->kernfs_rwsem); -if (!dir_emit (ctx, name, len, ino, type)) +if (!dir_emit (ctx, name, len, ino, type)) { +up_read (&root->kernfs_rwsem);return 0 ; -down_read (&root->kernfs_rwsem); + } }up_read (&root->kernfs_rwsem); file->private_data =NULL ;
Signature · 1 line
2.47.2
PATCH v8 4/6 kernfs: Don't re-lock kernfs_root::kernfs_rwsem in kernfs_fop_readdir().
Hi Sebastian,
We hit a potential ABBA deadlock while running selftests vfio_pci_device_test with this patch.
Thread 1 calls syscall getdents(2) on a kernfs directory: lock B->C->A dependency
kernfs_fop_readdir()
-> mutex_lock(&root->kernfs_rwsem): acquired root->kernfs_rwsem (Lock_A) -> dir_emit()
-> filldir()
-> unsafe_copy_dirent_name(): <- Hit page fault
-> do_user_addr_fault()
-> lock_mm_and_find_vma()
-> mmap_read_lock_killable(): acquired read mm->mmap_lock (Lock_C): Lock C depends on A
...
-> handle_mm_fault()
-> handle_pte_fault()
-> __do_fault()
-> vm_ops->fault() = vfio_pci_mmap_page_fault()
-> vfio_pci_mmap_huge_fault(): will acquire vdev->memory_lock (Lock_B): Lock B depends on CThread 2 runs vfio_pci_device_test kernel selftests: lock A->B dependency
vfio_pci_irq_test__enable_trigger_disable()
-> vfio_pci_irq_enable()
-> vfio_pci_core_ioctl(VFIO_DEVICE_SET_IRQS...)
-> vfio_pci_ioctl_set_irqs()
-> vfio_irq_enable()
-> vfio_pci_memory_lock_and_enable(): acquired vdev->memory_lock (Lock_B)
-> pci_alloc_irq_vectors()
-> msi_setup_device_data()
-> msi_sysfs_create_group()
-> kernfs_create_dir_ns()
-> kernfs_add_one(): will acquire root->kernfs_rwsem (Lock_A): Lock A will depend on BThe full lockdep warning on v6.18 kernel can be found below.
Hardware: Intel SkylakeKbuild: CONFIG_VFIO=y, CONFIG_IOMMUFD=y
Kernel Parameters: intel_iommu=on
--
[ 1055.224342] ======================================================
[ 1055.224346] WARNING: possible circular locking dependency detected
[ 1055.224349] 6.18.0-dbg-DEV #48 Tainted: G S O
[ 1055.224353] ------------------------------------------------------
[ 1055.224356] vfio_pci_device/15895 is trying to acquire lock:
[ 1055.224359] ffff94f340a64188 (&root->kernfs_rwsem){++++}-{4:4}, at: kernfs_add_one+0x2d/0x330
[ 1055.224373]
but task is already holding lock:
[ 1055.224376] ffff94f3f376c960 (&vdev->memory_lock){++++}-{4:4}, at: vfio_pci_memory_lock_and_enable+0x2a/0x90
[ 1055.224387]
which lock already depends on the new lock.[ 1055.224391]
the existing dependency chain (in reverse order) is:
[ 1055.224395]
-> #2 (&vdev->memory_lock){++++}-{4:4}:
[ 1055.224400] down_read+0x3f/0x180
[ 1055.224406] vfio_pci_mmap_huge_fault+0xbc/0x1a0
[ 1055.224410] __do_fault+0x46/0x180
[ 1055.224417] do_pte_missing+0x214/0x1300
[ 1055.224421] handle_mm_fault+0x795/0xb00
[ 1055.224425] do_user_addr_fault+0x4b7/0x6d0
[ 1055.224431] exc_page_fault+0x7d/0xe0
[ 1055.224437] asm_exc_page_fault+0x26/0x30
[ 1055.224452]
-> #1 (&mm->mmap_lock){++++}-{4:4}:
[ 1055.224457] down_read_killable+0x47/0x1b0
[ 1055.224460] mmap_read_lock_killable+0x12/0x50
[ 1055.224463] lock_mm_and_find_vma+0x125/0x140
[ 1055.224470] do_user_addr_fault+0x3d8/0x6d0
[ 1055.224474] exc_page_fault+0x7d/0xe0
[ 1055.224478] asm_exc_page_fault+0x26/0x30
[ 1055.224481] filldir+0xe4/0x1a0
[ 1055.224501] kernfs_fop_readdir+0x1af/0x2d0
[ 1055.224505] iterate_dir+0x84/0x160
[ 1055.224510] __se_sys_getdents+0x6c/0x110
[ 1055.224514] do_syscall_64+0xf0/0x930
[ 1055.224518] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 1055.224521]
-> #0 (&root->kernfs_rwsem){++++}-{4:4}:
[ 1055.224526] __lock_acquire+0x15a7/0x2d10
[ 1055.224531] lock_acquire+0xe8/0x2b0
[ 1055.224534] down_write+0x3a/0xb0
[ 1055.224537] kernfs_add_one+0x2d/0x330
[ 1055.224541] kernfs_create_dir_ns+0xab/0xe0
[ 1055.224545] internal_create_group+0x19d/0x4f0
[ 1055.224549] devm_device_add_group+0x49/0x80
[ 1055.224553] msi_setup_device_data+0x5b/0x100
[ 1055.224557] pci_setup_msi_context+0x1a/0xa0
[ 1055.224561] __pci_enable_msix_range+0x19b/0x220
[ 1055.224565] pci_alloc_irq_vectors_affinity+0xa0/0x130
[ 1055.224568] vfio_pci_set_msi_trigger+0x12b/0x300
[ 1055.224571] vfio_pci_core_ioctl+0x84c/0xf20
[ 1055.224575] vfio_device_fops_unl_ioctl+0x18d/0x350
[ 1055.224581] __se_sys_ioctl+0x71/0xc0
[ 1055.224585] do_syscall_64+0xf0/0x930
[ 1055.224588] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 1055.224591]
other info that might help us debug this:[ 1055.224594] Chain exists of:
&root->kernfs_rwsem --> &mm->mmap_lock --> &vdev->memory_lock[ 1055.224602] Possible unsafe locking scenario:
[ 1055.224605] CPU0 CPU1
[ 1055.224607] ---- ----
[ 1055.224610] lock(&vdev->memory_lock);
[ 1055.224613] lock(&mm->mmap_lock);
[ 1055.224616] lock(&vdev->memory_lock);
[ 1055.224631] lock(&root->kernfs_rwsem);
[ 1055.224633]
*** DEADLOCK ***[ 1055.224636] 2 locks held by vfio_pci_device/15895:
[ 1055.224639] #0: ffff94f3f376c728 (&vdev->igate){+.+.}-{4:4}, at: vfio_pci_core_ioctl+0x830/0xf20
[ 1055.224646] #1: ffff94f3f376c960 (&vdev->memory_lock){++++}-{4:4}, at: vfio_pci_memory_lock_and_enable+0x2a/0x90
[ 1055.224654]
stack backtrace:
[ 1055.224657] CPU: 90 UID: 0 PID: 15895 Comm: vfio_pci_device Tainted: G S O 6.18.0-dbg-DEV #48 NONE
[ 1055.224660] Tainted: [S]=CPU_OUT_OF_SPEC, [O]=OOT_MODULE
[ 1055.224660] Hardware name: Google LLC Indus/Indus_QC_00, BIOS 30.116.4 08/29/2025
[ 1055.224662] Call Trace:
[ 1055.224663] <TASK>
[ 1055.224664] dump_stack_lvl+0x7d/0xc0
[ 1055.224667] print_circular_bug+0x2e8/0x300
[ 1055.224670] check_noncircular+0xff/0x120
[ 1055.224672] __lock_acquire+0x15a7/0x2d10
[ 1055.224675] ? __kernfs_new_node+0x8c/0x2b0
[ 1055.224679] ? kernfs_root+0x16/0x140
[ 1055.224680] ? kernfs_add_one+0x2d/0x330
[ 1055.224682] lock_acquire+0xe8/0x2b0
[ 1055.224684] ? kernfs_add_one+0x2d/0x330
[ 1055.224687] down_write+0x3a/0xb0
[ 1055.224689] ? kernfs_add_one+0x2d/0x330
[ 1055.224690] kernfs_add_one+0x2d/0x330
[ 1055.224693] kernfs_create_dir_ns+0xab/0xe0
[ 1055.224695] internal_create_group+0x19d/0x4f0
[ 1055.224698] devm_device_add_group+0x49/0x80
[ 1055.224700] msi_setup_device_data+0x5b/0x100
[ 1055.224702] pci_setup_msi_context+0x1a/0xa0
[ 1055.224703] __pci_enable_msix_range+0x19b/0x220
[ 1055.224705] pci_alloc_irq_vectors_affinity+0xa0/0x130
[ 1055.224708] vfio_pci_set_msi_trigger+0x12b/0x300
[ 1055.224710] vfio_pci_core_ioctl+0x84c/0xf20
[ 1055.224713] ? _raw_spin_unlock_irqrestore+0x35/0x50
[ 1055.224717] vfio_device_fops_unl_ioctl+0x18d/0x350
[ 1055.224720] __se_sys_ioctl+0x71/0xc0
[ 1055.224722] do_syscall_64+0xf0/0x930
[ 1055.224724] ? entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 1055.224725] ? exc_page_fault+0x9e/0xe0
[ 1055.224727] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 1055.224729] RIP: 0033:0x7fd526765887
[ 1055.224731] Code: cc cc cc 48 8b 05 99 39 07 00 64 c7 00 26 00 00 00 48 c7 c0 ff ff ff ff c3 cc cc cc cc cc cc cc cc cc cc b8 10 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 69 39 07 00 f7 d8 64 89 01 48
[ 1055.224733] RSP: 002b:00007ffff5c63438 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
[ 1055.224735] RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007fd526765887
[ 1055.224736] RDX: 00007ffff5c63440 RSI: 0000000000003b6e RDI: 0000000000000007
[ 1055.224737] RBP: 00007ffff5c634e0 R08: 0000000020ff67e8 R09: 0000000000000000
[ 1055.224738] R10: 0000000000000000 R11: 0000000000000246 R12: 000000000021a180
[ 1055.224738] R13: 00007fd526836798 R14: 00007ffff5c63c78 R15: 0000000000000001
[ 1055.224743] </TASK>Quoted 40 lines
The readdir operation iterates over all entries and invokes
dir_emit()for every entry passingkernfs_node::nameas argument.
Since the name argument can change, and become invalid, thekernfs_root::kernfs_rwsemlock should not be dropped to prevent renames during the operation.The lock drop around
dir_emit()has been initially introduced in commit
1e5289c97bba2("sysfs: Cache the lastsysfs_direntto improve readdir scalability v2")to avoid holding a global lock during a page fault. The lock drop is wrong since the support of renames and not a big burden since the lock is no longer global.
Don't re-acquire
kernfs_root::kernfs_rwsemwhile copying the name to the userpace buffer.Acked-by Tejun Heo <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>--- fs/kernfs/dir.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)fs/kernfs/dir.c +3
-3@@ -1869,10 +1869,10 @@ static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx) file->private_data = pos;kernfs_get (pos); -up_read (&root->kernfs_rwsem); -if (!dir_emit (ctx, name, len, ino, type)) +if (!dir_emit (ctx, name, len, ino, type)) { +up_read (&root->kernfs_rwsem);return 0 ; -down_read (&root->kernfs_rwsem); + } }up_read (&root->kernfs_rwsem); file->private_data =NULL ;
PATCH v8 5/6 kernfs: Use RCU to access kernfs_node::parent.
In reply to Sebastian Andrzej Siewiorkernfs_rename_lock is used to obtain stable kernfs_node::{name|parent} pointer. This is a preparation to access kernfs_node::parent under RCU and ensure that the pointer remains stable under the RCU lifetime guarantees.
For a complete path, as it is done in kernfs_path_from_node(), the kernfs_rename_lock is still required in order to obtain a stable parent relationship while computing the relevant node depth. This must not change while the nodes are inspected in order to build the path. If the kernfs user never moves the nodes (changes the parent) then the kernfs_rename_lock is not required and the RCU guarantees are sufficient. This "restriction" can be set with KERNFS_ROOT_INVARIANT_PARENT. Otherwise the lock is required.
Rename kernfs_node::parent to kernfs_node::__parent to denote the RCU access and use RCU accessor while accessing the node.
Make cgroup use KERNFS_ROOT_INVARIANT_PARENT since the parent here can not change.
--- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 65 +++++++++---- fs/kernfs/dir.c | 96 ++++++++++++------- fs/kernfs/kernfs-internal.h | 32 ++++++- fs/kernfs/mount.c | 10 +- fs/kernfs/symlink.c | 23 ++--- fs/sysfs/file.c | 24 +++-- include/linux/kernfs.h | 10 +- kernel/cgroup/cgroup-v1.c | 2 +- kernel/cgroup/cgroup.c | 24 ++++- .../selftests/bpf/progs/profiler.inc.h | 2 +- 10 files changed, 195 insertions(+), 93 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +44 -21
@@ -956,10 +956,20 @@ static int rdt_last_cmd_status_show(struct kernfs_open_file *of,return 0 ; } +static void *rdt_kn_parent_priv (struct kernfs_node *kn) +{ +/* +* The parent pointer is only valid within RCU section since it can be +* replaced. +*/ +guard (rcu)(); +return rcu_dereference (kn->__parent)->priv; +} +static int rdt_num_closids_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct resctrl_schema *s = of->kn->parent->priv; +struct resctrl_schema *s =rdt_kn_parent_priv (of->kn);seq_printf (seq,"%u\n" , s->num_closid);return 0 ;
@@ -968,7 +978,7 @@ static int rdt_num_closids_show(struct kernfs_open_file *of,static int rdt_default_ctrl_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct resctrl_schema *s = of->kn->parent->priv; +struct resctrl_schema *s =rdt_kn_parent_priv (of->kn);struct rdt_resource *r = s->res;seq_printf (seq,"%x\n" , r->default_ctrl);
@@ -978,7 +988,7 @@ static int rdt_default_ctrl_show(struct kernfs_open_file *of,static int rdt_min_cbm_bits_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct resctrl_schema *s = of->kn->parent->priv; +struct resctrl_schema *s =rdt_kn_parent_priv (of->kn);struct rdt_resource *r = s->res;seq_printf (seq,"%u\n" , r->cache.min_cbm_bits);
@@ -988,7 +998,7 @@ static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,static int rdt_shareable_bits_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct resctrl_schema *s = of->kn->parent->priv; +struct resctrl_schema *s =rdt_kn_parent_priv (of->kn);struct rdt_resource *r = s->res;seq_printf (seq,"%x\n" , r->cache.shareable_bits);
@@ -1012,7 +1022,7 @@ static int rdt_shareable_bits_show(struct kernfs_open_file *of,static int rdt_bit_usage_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct resctrl_schema *s = of->kn->parent->priv; +struct resctrl_schema *s =rdt_kn_parent_priv (of->kn);/* * Use unsigned long even though only 32 bits are used to ensure * test_bit() is used safely.
@@ -1094,7 +1104,7 @@ static int rdt_bit_usage_show(struct kernfs_open_file *of,static int rdt_min_bw_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct resctrl_schema *s = of->kn->parent->priv; +struct resctrl_schema *s =rdt_kn_parent_priv (of->kn);struct rdt_resource *r = s->res;seq_printf (seq,"%u\n" , r->membw.min_bw);
@@ -1104,7 +1114,7 @@ static int rdt_min_bw_show(struct kernfs_open_file *of,static int rdt_num_rmids_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct rdt_resource *r = of->kn->parent->priv; +struct rdt_resource *r =rdt_kn_parent_priv (of->kn);seq_printf (seq,"%d\n" , r->num_rmid);
@@ -1114,7 +1124,7 @@ static int rdt_num_rmids_show(struct kernfs_open_file *of,static int rdt_mon_features_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct rdt_resource *r = of->kn->parent->priv; +struct rdt_resource *r =rdt_kn_parent_priv (of->kn);struct mon_evt *mevt;list_for_each_entry (mevt, &r->evt_list, list) {
@@ -1129,7 +1139,7 @@ static int rdt_mon_features_show(struct kernfs_open_file *of,static int rdt_bw_gran_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct resctrl_schema *s = of->kn->parent->priv; +struct resctrl_schema *s =rdt_kn_parent_priv (of->kn);struct rdt_resource *r = s->res;seq_printf (seq,"%u\n" , r->membw.bw_gran);
@@ -1139,7 +1149,7 @@ static int rdt_bw_gran_show(struct kernfs_open_file *of,static int rdt_delay_linear_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct resctrl_schema *s = of->kn->parent->priv; +struct resctrl_schema *s =rdt_kn_parent_priv (of->kn);struct rdt_resource *r = s->res;seq_printf (seq,"%u\n" , r->membw.delay_linear);
@@ -1157,7 +1167,7 @@ static int max_threshold_occ_show(struct kernfs_open_file *of,static int rdt_thread_throttle_mode_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct resctrl_schema *s = of->kn->parent->priv; +struct resctrl_schema *s =rdt_kn_parent_priv (of->kn);struct rdt_resource *r = s->res;if (r->membw.throttle_mode ==THREAD_THROTTLE_PER_THREAD )
@@ -1222,7 +1232,7 @@ static enum resctrl_conf_type resctrl_peer_type(enum resctrl_conf_type my_type)static int rdt_has_sparse_bitmasks_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct resctrl_schema *s = of->kn->parent->priv; +struct resctrl_schema *s =rdt_kn_parent_priv (of->kn);struct rdt_resource *r = s->res;seq_printf (seq,"%u\n" , r->cache.arch_has_sparse_bitmasks);
@@ -1634,7 +1644,7 @@ static int mbm_config_show(struct seq_file *s, struct rdt_resource *r, u32 evtidstatic int mbm_total_bytes_config_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct rdt_resource *r = of->kn->parent->priv; +struct rdt_resource *r =rdt_kn_parent_priv (of->kn);mbm_config_show (seq, r,QOS_L3_MBM_TOTAL_EVENT_ID );
@@ -1644,7 +1654,7 @@ static int mbm_total_bytes_config_show(struct kernfs_open_file *of,static int mbm_local_bytes_config_show (struct kernfs_open_file *of,struct seq_file *seq,void *v) { -struct rdt_resource *r = of->kn->parent->priv; +struct rdt_resource *r =rdt_kn_parent_priv (of->kn);mbm_config_show (seq, r,QOS_L3_MBM_LOCAL_EVENT_ID );
@@ -1750,7 +1760,7 @@ static ssize_t mbm_total_bytes_config_write(struct kernfs_open_file *of, char *buf, size_t nbytes, loff_t off) { - struct rdt_resource *r = of->kn->parent->priv; + struct rdt_resource *r = rdt_kn_parent_priv(of->kn);int ret;/* Valid input requires a trailing newline */
@@ -1776,7 +1786,7 @@ static ssize_t mbm_local_bytes_config_write(struct kernfs_open_file *of, char *buf, size_t nbytes, loff_t off) { - struct rdt_resource *r = of->kn->parent->priv; + struct rdt_resource *r = rdt_kn_parent_priv(of->kn);int ret;/* Valid input requires a trailing newline */
@@ -2440,12 +2450,13 @@ static struct rdtgroup *kernfs_to_rdtgroup(struct kernfs_node *kn)* resource. "info" and its subdirectories don't * have rdtgroup structures, so return NULL here. */ -if (kn == kn_info || kn->parent == kn_info) +if (kn == kn_info || +rcu_access_pointer (kn->__parent) == kn_info)return NULL ;else return kn->priv; }else { -return kn->parent->priv; +return rdt_kn_parent_priv (kn); } }
@@ -3771,9 +3782,18 @@ static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)return 0 ; } +static struct kernfs_node *rdt_kn_parent (struct kernfs_node *kn) +{ +/* +* Valid within the RCU section it was obtained or while rdtgroup_mutex +* is held. +*/ +return rcu_dereference_check (kn->__parent,lockdep_is_held (&rdtgroup_mutex)); +} +static int rdtgroup_rmdir (struct kernfs_node *kn) { -struct kernfs_node *parent_kn = kn->parent; +struct kernfs_node *parent_kn;struct rdtgroup *rdtgrp;cpumask_var_t tmpmask;int ret =0 ;
@@ -3786,6 +3806,7 @@ static int rdtgroup_rmdir(struct kernfs_node *kn) ret = -EPERM ; goto out; } + parent_kn =rdt_kn_parent (kn);/* * If the rdtgroup is a ctrl_mon group and parent directory
@@ -3854,6 +3875,7 @@ static void mongrp_reparent(struct rdtgroup *rdtgrp,static int rdtgroup_rename (struct kernfs_node *kn,struct kernfs_node *new_parent,const char *new_name) { +struct kernfs_node *kn_parent;struct rdtgroup *new_prdtgrp;struct rdtgroup *rdtgrp;cpumask_var_t tmpmask;
@@ -3888,8 +3910,9 @@ static int rdtgroup_rename(struct kernfs_node *kn, goto out; } -if (rdtgrp->type !=RDTMON_GROUP || !kn->parent || - !is_mon_groups (kn->parent, kn->name)) { + kn_parent =rdt_kn_parent (kn); +if (rdtgrp->type !=RDTMON_GROUP || !kn_parent || + !is_mon_groups (kn_parent, kn->name)) {rdt_last_cmd_puts ("Source must be a MON group\n" ); ret = -EPERM ; goto out;
fs/kernfs/dir.c +60 -36
286 lines not on this page · raw message
fs/kernfs/kernfs-internal.h +29 -3
@@ -19,6 +19,8 @@#include <linux/kernfs.h> #include <linux/fs_context.h> +extern rwlock_t kernfs_rename_lock; +struct kernfs_iattrs {kuid_t ia_uid;kgid_t ia_gid;
@@ -64,11 +66,14 @@ struct kernfs_root {* * Return: the kernfs_root @kn belongs to. */ -static inline struct kernfs_root *kernfs_root (struct kernfs_node *kn) +static inline struct kernfs_root *kernfs_root (const struct kernfs_node *kn) { +const struct kernfs_node *knp;/* if parent exists, it's always a dir; otherwise, @sd is a dir */ -if (kn->parent) - kn = kn->parent; +guard (rcu)(); + knp =rcu_dereference (kn->__parent); +if (knp) + kn = knp;return kn->dir.root; }
@@ -97,6 +102,27 @@ struct kernfs_super_info { };#define kernfs_info (SB ) ((struct kernfs_super_info *)(SB->s_fs_info)) +static inline bool kernfs_root_is_locked (const struct kernfs_node *kn) +{ +return lockdep_is_held (&kernfs_root (kn)->kernfs_rwsem); +} + +static inline struct kernfs_node *kernfs_parent (const struct kernfs_node *kn) +{ +/* +* The kernfs_node::__parent remains valid within a RCU section. The kn +* can be reparented (and renamed) which changes the entry. This can be +* avoided by locking kernfs_root::kernfs_rwsem or kernfs_rename_lock. +* Both locks can be used to obtain a reference on __parent. Once the +* reference count reaches 0 then the node is about to be freed +* and can not be renamed (or become a different parent) anymore. +*/ +return rcu_dereference_check (kn->__parent, +kernfs_root_is_locked (kn) || +lockdep_is_held (&kernfs_rename_lock) || + !atomic_read (&kn->count)); +} +static inline struct kernfs_node *kernfs_dentry_node (struct dentry *dentry) {if (d_really_is_negative (dentry))
fs/kernfs/mount.c +5 -5
@@ -148,7 +148,7 @@ static struct dentry *kernfs_get_parent_dentry(struct dentry *child)struct kernfs_root *root =kernfs_root (kn);guard (rwsem_read)(&root->kernfs_rwsem); -return d_obtain_alias (kernfs_get_inode (child->d_sb, kn->parent)); +return d_obtain_alias (kernfs_get_inode (child->d_sb,kernfs_parent (kn))); }static const struct export_operations kernfs_export_ops = {
@@ -188,10 +188,10 @@ static struct kernfs_node *find_next_ancestor(struct kernfs_node *child,return NULL ; } -while (child->parent != parent) { -if (!child->parent) +while (kernfs_parent (child) != parent) { + child =kernfs_parent (child); +if (!child)return NULL ; - child = child->parent; }return child;
@@ -216,7 +216,7 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn, dentry =dget (sb->s_root);/* Check if this is the root kernfs_node */ -if (!kn->parent) +if (!rcu_access_pointer (kn->__parent))return dentry; root =kernfs_root (kn);
fs/kernfs/symlink.c +12 -11
@@ -62,10 +62,10 @@ static int kernfs_get_target_path(struct kernfs_node *parent,/* go up to the root, stop at the base */ base = parent; -while (base->parent) { - kn = target->parent; -while (kn->parent && base != kn) - kn = kn->parent; +while (kernfs_parent (base)) { + kn =kernfs_parent (target); +while (kernfs_parent (kn) && base != kn) + kn =kernfs_parent (kn);if (base == kn)break ;
@@ -75,14 +75,14 @@ static int kernfs_get_target_path(struct kernfs_node *parent,strcpy (s,"../" ); s +=3 ; - base = base->parent; + base =kernfs_parent (base); }/* determine end of target string for reverse fillup */ kn = target; -while (kn->parent && kn != base) { +while (kernfs_parent (kn) && kn != base) { len +=strlen (kn->name) +1 ; - kn = kn->parent; + kn =kernfs_parent (kn); }/* check limits */
@@ -94,7 +94,7 @@ static int kernfs_get_target_path(struct kernfs_node *parent,/* reverse fillup of target string from target to base */ kn = target; -while (kn->parent && kn != base) { +while (kernfs_parent (kn) && kn != base) {int slen =strlen (kn->name); len -= slen;
@@ -102,7 +102,7 @@ static int kernfs_get_target_path(struct kernfs_node *parent,if (len) s[--len] ='/' ; - kn = kn->parent; + kn =kernfs_parent (kn); }return 0 ;
@@ -111,12 +111,13 @@ static int kernfs_get_target_path(struct kernfs_node *parent,static int kernfs_getlink (struct inode *inode,char *path) {struct kernfs_node *kn = inode->i_private; -struct kernfs_node *parent = kn->parent; +struct kernfs_node *parent;struct kernfs_node *target = kn->symlink.target_kn; -struct kernfs_root *root =kernfs_root (parent); +struct kernfs_root *root =kernfs_root (kn);int error;down_read (&root->kernfs_rwsem); + parent =kernfs_parent (kn); error =kernfs_get_target_path (parent, target, path);up_read (&root->kernfs_rwsem);
fs/sysfs/file.c +15 -9
@@ -19,13 +19,19 @@#include "sysfs.h" +static struct kobject *sysfs_file_kobj (struct kernfs_node *kn) +{ +guard (rcu)(); +return rcu_dereference (kn->__parent)->priv; +} +/* * Determine ktype->sysfs_ops for the given kernfs_node. This function * must be called while holding an active reference. */ static const struct sysfs_ops *sysfs_file_ops (struct kernfs_node *kn) { -struct kobject *kobj = kn->parent->priv; +struct kobject *kobj =sysfs_file_kobj (kn);if (kn->flags &KERNFS_LOCKDEP )lockdep_assert_held (kn);
@@ -40,7 +46,7 @@ static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)static int sysfs_kf_seq_show (struct seq_file *sf,void *v) {struct kernfs_open_file *of = sf->private; -struct kobject *kobj = of->kn->parent->priv; +struct kobject *kobj =sysfs_file_kobj (of->kn);const struct sysfs_ops *ops =sysfs_file_ops (of->kn);ssize_t count;char *buf;
@@ -78,7 +84,7 @@ static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf, size_t count, loff_t pos) {struct bin_attribute *battr = of->kn->priv; -struct kobject *kobj = of->kn->parent->priv; +struct kobject *kobj =sysfs_file_kobj (of->kn);loff_t size =file_inode (of->file)->i_size;if (!count)
@@ -105,7 +111,7 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf, size_t count, loff_t pos) {const struct sysfs_ops *ops =sysfs_file_ops (of->kn); -struct kobject *kobj = of->kn->parent->priv; +struct kobject *kobj =sysfs_file_kobj (of->kn);ssize_t len;/*
@@ -131,7 +137,7 @@ static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf, size_t count, loff_t pos) {const struct sysfs_ops *ops =sysfs_file_ops (of->kn); -struct kobject *kobj = of->kn->parent->priv; +struct kobject *kobj =sysfs_file_kobj (of->kn);if (!count)return 0 ;
@@ -144,7 +150,7 @@ static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf, size_t count, loff_t pos) {struct bin_attribute *battr = of->kn->priv; -struct kobject *kobj = of->kn->parent->priv; +struct kobject *kobj =sysfs_file_kobj (of->kn);loff_t size =file_inode (of->file)->i_size;if (size) {
@@ -168,7 +174,7 @@ static int sysfs_kf_bin_mmap(struct kernfs_open_file *of, struct vm_area_struct *vma) {struct bin_attribute *battr = of->kn->priv; -struct kobject *kobj = of->kn->parent->priv; +struct kobject *kobj =sysfs_file_kobj (of->kn);return battr->mmap (of->file, kobj, battr, vma); }
@@ -177,7 +183,7 @@ static loff_t sysfs_kf_bin_llseek(struct kernfs_open_file *of, loff_t offset, int whence) {struct bin_attribute *battr = of->kn->priv; -struct kobject *kobj = of->kn->parent->priv; +struct kobject *kobj =sysfs_file_kobj (of->kn);if (battr->llseek)return battr->llseek (of->file, kobj, battr, offset, whence);
@@ -494,7 +500,7 @@ EXPORT_SYMBOL_GPL(sysfs_break_active_protection);*/ void sysfs_unbreak_active_protection (struct kernfs_node *kn) { -struct kobject *kobj = kn->parent->priv; +struct kobject *kobj =sysfs_file_kobj (kn);kernfs_unbreak_active_protection (kn);kernfs_put (kn);
include/linux/kernfs.h +9 -1
@@ -147,6 +147,11 @@ enum kernfs_root_flag {* Support user xattrs to be written to nodes rooted at this root. */ KERNFS_ROOT_SUPPORT_USER_XATTR =0x0008 , + +/* +* Renames must not change the parent node. +*/ + KERNFS_ROOT_INVARIANT_PARENT = 0x0010, };/* type-specific structures for kernfs_node union members */
@@ -199,8 +204,8 @@ struct kernfs_node {* never moved to a different parent, it is safe to access the * parent directly. */ -struct kernfs_node *parent;const char *name; +struct kernfs_node __rcu *__parent;struct rb_node rb;
@@ -416,6 +421,7 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,struct kernfs_root *kernfs_create_root (struct kernfs_syscall_ops *scops,unsigned int flags,void *priv);void kernfs_destroy_root (struct kernfs_root *root); +unsigned int kernfs_root_flags (struct kernfs_node *kn); struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, const char *name, umode_t mode,
@@ -514,6 +520,8 @@ kernfs_create_root(struct kernfs_syscall_ops *scops, unsigned int flags, {return ERR_PTR (-ENOSYS ); }static inline void kernfs_destroy_root (struct kernfs_root *root) { } +static inline unsigned int kernfs_root_flags (struct kernfs_node *kn) +{return 0 ; }static inline struct kernfs_node * kernfs_create_dir_ns(struct kernfs_node *parent, const char *name,
kernel/cgroup/cgroup-v1.c +1 -1
@@ -844,7 +844,7 @@ static int cgroup1_rename(struct kernfs_node *kn, struct kernfs_node *new_parentif (kernfs_type (kn) !=KERNFS_DIR )return -ENOTDIR ; -if (kn->parent != new_parent) +if (rcu_access_pointer (kn->__parent) != new_parent)return -EIO ;/*
kernel/cgroup/cgroup.c +19 -5
@@ -633,9 +633,22 @@ int cgroup_task_count(const struct cgroup *cgrp)return count; } +static struct cgroup *kn_priv (struct kernfs_node *kn) +{ +struct kernfs_node *parent; +/* +* The parent can not be replaced due to KERNFS_ROOT_INVARIANT_PARENT. +* Therefore it is always safe to dereference this pointer outside of a +* RCU section. +*/ + parent =rcu_dereference_check (kn->__parent, +kernfs_root_flags (kn) &KERNFS_ROOT_INVARIANT_PARENT ); +return parent->priv; +} +struct cgroup_subsys_state *of_css (struct kernfs_open_file *of) { -struct cgroup *cgrp = of->kn->parent->priv; +struct cgroup *cgrp =kn_priv (of->kn);struct cftype *cft =of_cft (of);/*
@@ -1612,7 +1625,7 @@ void cgroup_kn_unlock(struct kernfs_node *kn)if (kernfs_type (kn) ==KERNFS_DIR ) cgrp = kn->priv;else - cgrp = kn->parent->priv; + cgrp =kn_priv (kn);cgroup_unlock ();
@@ -1644,7 +1657,7 @@ struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)if (kernfs_type (kn) ==KERNFS_DIR ) cgrp = kn->priv;else - cgrp = kn->parent->priv; + cgrp =kn_priv (kn);/* * We're gonna grab cgroup_mutex which nests outside kernfs
@@ -2118,7 +2131,8 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask) root->kf_root =kernfs_create_root (kf_sops,KERNFS_ROOT_CREATE_DEACTIVATED |KERNFS_ROOT_SUPPORT_EXPORTOP | -KERNFS_ROOT_SUPPORT_USER_XATTR , +KERNFS_ROOT_SUPPORT_USER_XATTR | +KERNFS_ROOT_INVARIANT_PARENT , root_cgrp);if (IS_ERR (root->kf_root)) { ret =PTR_ERR (root->kf_root);
@@ -4119,7 +4133,7 @@ static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, size_t nbytes, loff_t off) {struct cgroup_file_ctx *ctx = of->priv; -struct cgroup *cgrp = of->kn->parent->priv; +struct cgroup *cgrp =kn_priv (of->kn);struct cftype *cft =of_cft (of);struct cgroup_subsys_state *css;int ret;
tools/testing/selftests/bpf/progs/profiler.inc.h +1 -1
@@ -223,7 +223,7 @@ static INLINE void* read_full_cgroup_path(struct kernfs_node* cgroup_node, if (bpf_cmp_likely(filepart_length, <=, MAX_PATH)) { payload += filepart_length; } - cgroup_node =BPF_CORE_READ (cgroup_node, parent); + cgroup_node =BPF_CORE_READ (cgroup_node, __parent); }return payload; }
Signature · 1 line
2.47.2
PATCH v8 6/6 kernfs: Use RCU to access kernfs_node::name.
In reply to Sebastian Andrzej SiewiorUsing RCU lifetime rules to access kernfs_node::name can avoid the trouble with kernfs_rename_lock in kernfs_name() and kernfs_path_from_node() if the fs was created with KERNFS_ROOT_INVARIANT_PARENT. This is usefull as it allows to implement kernfs_path_from_node() only with RCU protection and avoiding kernfs_rename_lock. The lock is only required if the __parent node can be changed and the function requires an unchanged hierarchy while it iterates from the node to its parent.
The change is needed to allow the lookup of the node's path (kernfs_path_from_node()) from context which runs always with disabled preemption and or interrutps even on PREEMPT_RT. The problem is that kernfs_rename_lock becomes a sleeping lock on PREEMPT_RT.
I went through all ::name users and added the required access for the lookup with a few extensions:
- rdtgroup_pseudo_lock_create() drops all locks and then uses the name later on. resctrl supports rename with different parents. Here I made a temporal copy of the name while it is used outside of the lock.
- kernfs_rename_ns() accepts NULL as new_parent. This simplifies sysfs_move_dir_ns() where it can set NULL in order to reuse the current name.
- kernfs_rename_ns() is only using kernfs_rename_lock if the parents are different. All users use either kernfs_rwsem (for stable path view) or just RCU for the lookup. The ::name uses always RCU free.
Use RCU lifetime guarantees to access kernfs_node::name.
--- arch/x86/kernel/cpu/resctrl/internal.h | 5 + arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 14 ++- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 10 +- fs/kernfs/dir.c | 113 ++++++++++++---------- fs/kernfs/file.c | 4 +- fs/kernfs/kernfs-internal.h | 5 + fs/kernfs/mount.c | 5 +- fs/kernfs/symlink.c | 7 +- fs/sysfs/dir.c | 2 +- include/linux/kernfs.h | 4 +- security/selinux/hooks.c | 7 +- 11 files changed, 105 insertions(+), 71 deletions(-)
arch/x86/kernel/cpu/resctrl/internal.h +5 -0
@@ -507,6 +507,11 @@ int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,extern struct mutex rdtgroup_mutex; +static inline const char *rdt_kn_name (const struct kernfs_node *kn) +{ +return rcu_dereference_check (kn->name,lockdep_is_held (&rdtgroup_mutex)); +} +extern struct rdt_hw_resource rdt_resources_all[];extern struct rdtgroup rdtgroup_default;extern struct dentry *debugfs_resctrl;
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +10 -4
@@ -52,7 +52,8 @@ static char *pseudo_lock_devnode(const struct device *dev, umode_t *mode) rdtgrp =dev_get_drvdata (dev);if (mode) *mode =0600 ; -return kasprintf (GFP_KERNEL ,"pseudo_lock/%s" , rdtgrp->kn->name); +guard (mutex)(&rdtgroup_mutex); +return kasprintf (GFP_KERNEL ,"pseudo_lock/%s" ,rdt_kn_name (rdtgrp->kn)); }static const struct class pseudo_lock_class = {
@@ -1293,6 +1294,7 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)struct task_struct *thread;unsigned int new_minor;struct device *dev; + char *kn_name __free(kfree) = NULL;int ret; ret =pseudo_lock_region_alloc (plr);
@@ -1304,6 +1306,11 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp) ret = -EINVAL ; goto out_region; } + kn_name =kstrdup (rdt_kn_name (rdtgrp->kn),GFP_KERNEL ); +if (!kn_name) { + ret = -ENOMEM ; + goto out_cstates; + } plr->thread_done =0 ;
@@ -1348,8 +1355,7 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)mutex_unlock (&rdtgroup_mutex);if (!IS_ERR_OR_NULL (debugfs_resctrl)) { - plr->debugfs_dir =debugfs_create_dir (rdtgrp->kn->name, - debugfs_resctrl); + plr->debugfs_dir =debugfs_create_dir (kn_name, debugfs_resctrl);if (!IS_ERR_OR_NULL (plr->debugfs_dir)) debugfs_create_file("pseudo_lock_measure", 0200, plr->debugfs_dir, rdtgrp,
@@ -1358,7 +1364,7 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp) dev =device_create (&pseudo_lock_class,NULL ,MKDEV (pseudo_lock_major, new_minor), - rdtgrp,"%s" , rdtgrp->kn->name); + rdtgrp,"%s" , kn_name);mutex_lock (&rdtgroup_mutex);
arch/x86/kernel/cpu/resctrl/rdtgroup.c +5 -5
@@ -916,14 +916,14 @@ int proc_resctrl_show(struct seq_file *s, struct pid_namespace *ns,continue ;seq_printf (s,"res:%s%s\n" , (rdtg == &rdtgroup_default) ?"/" :"" , - rdtg->kn->name); +rdt_kn_name (rdtg->kn));seq_puts (s,"mon:" );list_for_each_entry (crg, &rdtg->mon.crdtgrp_list, mon.crdtgrp_list) {if (!resctrl_arch_match_rmid (tsk, crg->mon.parent->closid, crg->mon.rmid))continue ; -seq_printf (s,"%s" , crg->kn->name); +seq_printf (s,"%s" ,rdt_kn_name (crg->kn));break ; }seq_putc (s,'\n' );
@@ -3675,7 +3675,7 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,*/ static bool is_mon_groups (struct kernfs_node *kn,const char *name) { -return (!strcmp (kn->name,"mon_groups" ) && +return (!strcmp (rdt_kn_name (kn),"mon_groups" ) &&strcmp (name,"mon_groups" )); }
@@ -3824,7 +3824,7 @@ static int rdtgroup_rmdir(struct kernfs_node *kn) ret =rdtgroup_rmdir_ctrl (rdtgrp, tmpmask); } }else if (rdtgrp->type ==RDTMON_GROUP && -is_mon_groups (parent_kn, kn->name)) { +is_mon_groups (parent_kn,rdt_kn_name (kn))) { ret =rdtgroup_rmdir_mon (rdtgrp, tmpmask); }else { ret = -EPERM ;
@@ -3912,7 +3912,7 @@ static int rdtgroup_rename(struct kernfs_node *kn, kn_parent =rdt_kn_parent (kn);if (rdtgrp->type !=RDTMON_GROUP || !kn_parent || - !is_mon_groups (kn_parent, kn->name)) { + !is_mon_groups (kn_parent,rdt_kn_name (kn))) {rdt_last_cmd_puts ("Source must be a MON group\n" ); ret = -EPERM ; goto out;
fs/kernfs/dir.c +62 -51
247 lines not on this page · raw message
fs/kernfs/file.c +3 -1
@@ -915,6 +915,7 @@ static void kernfs_notify_workfn(struct work_struct *work)list_for_each_entry (info, &kernfs_root (kn)->supers, node) {struct kernfs_node *parent;struct inode *p_inode =NULL ; +const char *kn_name;struct inode *inode;struct qstr name;
@@ -928,7 +929,8 @@ static void kernfs_notify_workfn(struct work_struct *work)if (!inode)continue ; - name =QSTR (kn->name); + kn_name =kernfs_rcu_name (kn); + name =QSTR (kn_name); parent =kernfs_get_parent (kn);if (parent) { p_inode =ilookup (info->sb,kernfs_ino (parent));
fs/kernfs/kernfs-internal.h +5 -0
@@ -107,6 +107,11 @@ static inline bool kernfs_root_is_locked(const struct kernfs_node *kn)return lockdep_is_held (&kernfs_root (kn)->kernfs_rwsem); } +static inline const char *kernfs_rcu_name (const struct kernfs_node *kn) +{ +return rcu_dereference_check (kn->name,kernfs_root_is_locked (kn)); +} +static inline struct kernfs_node *kernfs_parent (const struct kernfs_node *kn) {/*
fs/kernfs/mount.c +3 -2
@@ -231,6 +231,7 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn, do { struct dentry *dtmp; struct kernfs_node *kntmp; + const char *name; if (kn == knparent) return dentry;
@@ -239,8 +240,8 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,dput (dentry);return ERR_PTR (-EINVAL ); } - dtmp =lookup_positive_unlocked (kntmp->name, dentry, -strlen (kntmp->name)); + name =rcu_dereference (kntmp->name); + dtmp =lookup_positive_unlocked (name, dentry,strlen (name));dput (dentry);if (IS_ERR (dtmp))return dtmp;
fs/kernfs/symlink.c +4 -3
@@ -81,7 +81,7 @@ static int kernfs_get_target_path(struct kernfs_node *parent,/* determine end of target string for reverse fillup */ kn = target;while (kernfs_parent (kn) && kn != base) { - len +=strlen (kn->name) +1 ; + len +=strlen (kernfs_rcu_name (kn)) +1 ; kn =kernfs_parent (kn); }
@@ -95,10 +95,11 @@ static int kernfs_get_target_path(struct kernfs_node *parent,/* reverse fillup of target string from target to base */ kn = target;while (kernfs_parent (kn) && kn != base) { -int slen =strlen (kn->name); +const char *name =kernfs_rcu_name (kn); +int slen =strlen (name); len -= slen; -memcpy (s + len, kn->name, slen); +memcpy (s + len, name, slen);if (len) s[--len] ='/' ;
fs/sysfs/dir.c +1 -1
@@ -123,7 +123,7 @@ int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj, new_parent = new_parent_kobj && new_parent_kobj->sd ? new_parent_kobj->sd : sysfs_root_kn; -return kernfs_rename_ns (kn, new_parent, kn->name, new_ns); +return kernfs_rename_ns (kn, new_parent,NULL , new_ns); }/**
include/linux/kernfs.h +2 -2
@@ -204,8 +204,8 @@ struct kernfs_node {* never moved to a different parent, it is safe to access the * parent directly. */ -const char *name;struct kernfs_node __rcu *__parent; +const char __rcu *name;struct rb_node rb;
@@ -400,7 +400,7 @@ static inline bool kernfs_ns_enabled(struct kernfs_node *kn) }int kernfs_name (struct kernfs_node *kn,char *buf,size_t buflen); -int kernfs_path_from_node (struct kernfs_node *root_kn,struct kernfs_node *kn, +int kernfs_path_from_node (struct kernfs_node *kn_to,struct kernfs_node *kn_from,char *buf,size_t buflen);void pr_cont_kernfs_name (struct kernfs_node *kn);void pr_cont_kernfs_path (struct kernfs_node *kn);
security/selinux/hooks.c +5 -2
@@ -3584,10 +3584,13 @@ static int selinux_kernfs_init_security(struct kernfs_node *kn_dir, newsid = tsec->create_sid; }else {u16 secclass =inode_mode_to_security_class (kn->mode); +const char *kn_name;struct qstr q; - q.name = kn->name; - q.hash_len =hashlen_string (kn_dir, kn->name); +/* kn is fresh, can't be renamed, name goes not away */ + kn_name =rcu_dereference_check (kn->name, true); + q.name = kn_name; + q.hash_len =hashlen_string (kn_dir, kn_name); rc = security_transition_sid(tsec->sid, parent_sid, secclass, &q,
Signature · 1 line
2.47.2
James Morse
reviewerPATCH 1/1 x86/resctrl: Fix rdtgroup_mkdir()'s unlocked use of kernfs_node::name
Since commit 741c10b096bc ("kernfs: Use RCU to access kernfs_node::name."), a helper rdt_kn_name() that checks that rdtgroup_mutex is held has been used for all accesses to the kernfs node name.
rdtgroup_mkdir() uses the name to determine if a valid monitor group is being created by checking the parent name is "mon_groups". This is done without holding rdtgroup_mutex, and now triggers the following warning:
| WARNING: suspicious RCU usage | 6.15.0-rc1 #4465 Tainted: G E | ----------------------------- | arch/x86/kernel/cpu/resctrl/internal.h:408 suspicious rcu_dereference_check() usage! [...] | Call Trace: | <TASK> | dump_stack_lvl+0x6c/0xa0 | lockdep_rcu_suspicious.cold+0x4e/0x96 | is_mon_groups+0xba/0xd0 | rdtgroup_mkdir+0x118/0x1970 | kernfs_iop_mkdir+0xfa/0x1a0 | vfs_mkdir+0x456/0x760 | do_mkdirat+0x257/0x310 | __x64_sys_mkdir+0xd4/0x120 | do_syscall_64+0x6d/0x150 | entry_SYSCALL_64_after_hwframe+0x76/0x7e
Creating a control or monitor group calls mkdir_rdt_prepare(), which uses rdtgroup_kn_lock_live() to take the rdtgroup_mutex.
To avoid taking and dropping the lock, move the check for the monitor group name and position into mkdir_rdt_prepare() so that it occurs under rdtgroup_mutex. Hoist is_mon_groups() earlier in the file.
--- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 48 +++++++++++++++----------- 1 file changed, 27 insertions(+), 21 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +27 -21
@@ -3553,6 +3553,22 @@ static void mkdir_rdt_prepare_rmid_free(struct rdtgroup *rgrp)free_rmid (rgrp->closid, rgrp->mon.rmid); } +/* +* We allow creating mon groups only with in a directory called "mon_groups" +* which is present in every ctrl_mon group. Check if this is a valid +* "mon_groups" directory. +* +* 1. The directory should be named "mon_groups". +* 2. The mon group itself should "not" be named "mon_groups". +* This makes sure "mon_groups" directory always has a ctrl_mon group +* as parent. +*/ +static bool is_mon_groups (struct kernfs_node *kn,const char *name) +{ +return (!strcmp (rdt_kn_name (kn),"mon_groups" ) && +strcmp (name,"mon_groups" )); +} +static int mkdir_rdt_prepare (struct kernfs_node *parent_kn,const char *name,umode_t mode,enum rdt_group_type rtype,struct rdtgroup **r)
@@ -3568,6 +3584,15 @@ static int mkdir_rdt_prepare(struct kernfs_node *parent_kn, goto out_unlock; } +/* +* Check the parent directory for a monitor group is a "mon_groups" +* directory. +*/ +if (rtype ==RDTMON_GROUP && !is_mon_groups (parent_kn, name)) { + ret = -EPERM ; + goto out_unlock; + } +if (rtype ==RDTMON_GROUP && (prdtgrp->mode ==RDT_MODE_PSEUDO_LOCKSETUP || prdtgrp->mode ==RDT_MODE_PSEUDO_LOCKED )) {
@@ -3751,22 +3776,6 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,return ret; } -/* -* We allow creating mon groups only with in a directory called "mon_groups" -* which is present in every ctrl_mon group. Check if this is a valid -* "mon_groups" directory. -* -* 1. The directory should be named "mon_groups". -* 2. The mon group itself should "not" be named "mon_groups". -* This makes sure "mon_groups" directory always has a ctrl_mon group -* as parent. -*/ -static bool is_mon_groups (struct kernfs_node *kn,const char *name) -{ -return (!strcmp (rdt_kn_name (kn),"mon_groups" ) && -strcmp (name,"mon_groups" )); -} -static int rdtgroup_mkdir (struct kernfs_node *parent_kn,const char *name,umode_t mode) {
@@ -3782,11 +3791,8 @@ static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,if (resctrl_arch_alloc_capable () && parent_kn == rdtgroup_default.kn)return rdtgroup_mkdir_ctrl_mon (parent_kn, name, mode); -/* -* If RDT monitoring is supported and the parent directory is a valid -* "mon_groups" directory, add a monitoring subdirectory. -*/ -if (resctrl_arch_mon_capable () &&is_mon_groups (parent_kn, name)) +/* Else, attempt to add a monitoring subdirectory. */ +if (resctrl_arch_mon_capable ())return rdtgroup_mkdir_mon (parent_kn, name, mode);return -EPERM ;
Signature · 1 line
2.39.5
Re: [PATCH] x86/resctrl: Fix rdtgroup_mkdir()'s unlocked use of kernfs_node::name
Hi James,
Quoted 28 lines · James Morse
Since commit
741c10b096bc("kernfs: Use RCU to accesskernfs_node::name."), a helperrdt_kn_name()that checks thatrdtgroup_mutexis held has been used for all accesses to the kernfs node name.
rdtgroup_mkdir()uses the name to determine if a valid monitor group is being created by checking the parent name is "mon_groups". This is done without holdingrdtgroup_mutex, and now triggers the following warning:| WARNING: suspicious RCU usage | 6.15.0-rc1 #4465 Tainted: G E | ----------------------------- | arch/x86/kernel/cpu/resctrl/internal.h:408 suspicious rcu_dereference_check() usage! [...] | Call Trace: | <TASK> | dump_stack_lvl+0x6c/0xa0 | lockdep_rcu_suspicious.cold+0x4e/0x96 | is_mon_groups+0xba/0xd0 | rdtgroup_mkdir+0x118/0x1970 | kernfs_iop_mkdir+0xfa/0x1a0 | vfs_mkdir+0x456/0x760 | do_mkdirat+0x257/0x310 | __x64_sys_mkdir+0xd4/0x120 | do_syscall_64+0x6d/0x150 | entry_SYSCALL_64_after_hwframe+0x76/0x7eCreating a control or monitor group calls
mkdir_rdt_prepare(), which usesrdtgroup_kn_lock_live()to take therdtgroup_mutex.
Quoted 8 lines
To avoid taking and dropping the lock, move the check for the monitor group name and position into
mkdir_rdt_prepare()so that it occurs underrdtgroup_mutex. Hoistis_mon_groups()earlier in the file.CC Sebastian Andrzej Siewior <[email protected]>Fixes 741c10b096bc ("kernfs: Use RCU to access kernfs_node::name.")Signed-off-by James Morse <[email protected]>---
Thank you very much.
Reinette
Re: [PATCH] x86/resctrl: Fix rdtgroup_mkdir()'s unlocked use of kernfs_node::name
In reply to James Morsearch/x86/kernel/cpu/resctrl/rdtgroup.c static int mkdir_rdt_prepare(struct kernfs_node *parent_kn, · line 3590
Quoted 4 lines · James Morse
arch/x86/kernel/cpu/resctrl/rdtgroup.c +4
-0+/* +* Check the parent directory for a monitor group is a "mon_groups" +* directory. +*/
This sentence probably wants to be a bit less linguistically ambiguous, via something like:
s/Check the parent directory for a monitor group is a "mon_groups" directory.
/Check that the parent directory for a monitor group is a "mon_groups" directory.
With that:
Thanks,
Ingo
[tip: x86/urgent] x86/resctrl: Fix rdtgroup_mkdir()'s unlocked use of kernfs_node::name
In reply to James MorseThe following commit has been merged into the x86/urgent branch of tip:
The following commit has been merged into the x86/urgent branch of tip:
Commit-ID: 45c2e30bbd64d75559b99f3a5c455129a7a34b06 Gitweb: https://git.kernel.org/tip/45c2e30bbd64d75559b99f3a5c455129a7a34b06 Author: James Morse <[email protected]> AuthorDate: Mon, 07 Apr 2025 13:46:37 +01:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Wed, 09 Apr 2025 11:35:08 +02:00
x86/resctrl: Fix rdtgroup_mkdir()'s unlocked use of kernfs_node::name
Since
741c10b096bc ("kernfs: Use RCU to access kernfs_node::name.")
a helper rdt_kn_name() that checks that rdtgroup_mutex is held has been used for all accesses to the kernfs node name.
rdtgroup_mkdir() uses the name to determine if a valid monitor group is being created by checking the parent name is "mon_groups". This is done without holding rdtgroup_mutex, and now triggers the following warning:
| WARNING: suspicious RCU usage | 6.15.0-rc1 #4465 Tainted: G E | ----------------------------- | arch/x86/kernel/cpu/resctrl/internal.h:408 suspicious rcu_dereference_check() usage! [...] | Call Trace: | <TASK> | dump_stack_lvl | lockdep_rcu_suspicious.cold | is_mon_groups | rdtgroup_mkdir | kernfs_iop_mkdir | vfs_mkdir | do_mkdirat | __x64_sys_mkdir | do_syscall_64 | entry_SYSCALL_64_after_hwframe
Creating a control or monitor group calls mkdir_rdt_prepare(), which uses rdtgroup_kn_lock_live() to take the rdtgroup_mutex.
To avoid taking and dropping the lock, move the check for the monitor group name and position into mkdir_rdt_prepare() so that it occurs under rdtgroup_mutex. Hoist is_mon_groups() earlier in the file.
[ bp: Massage. ]
--- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 48 ++++++++++++++----------- 1 file changed, 27 insertions(+), 21 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +27 -21
@@ -3553,6 +3553,22 @@ static void mkdir_rdt_prepare_rmid_free(struct rdtgroup *rgrp)free_rmid (rgrp->closid, rgrp->mon.rmid); } +/* +* We allow creating mon groups only with in a directory called "mon_groups" +* which is present in every ctrl_mon group. Check if this is a valid +* "mon_groups" directory. +* +* 1. The directory should be named "mon_groups". +* 2. The mon group itself should "not" be named "mon_groups". +* This makes sure "mon_groups" directory always has a ctrl_mon group +* as parent. +*/ +static bool is_mon_groups (struct kernfs_node *kn,const char *name) +{ +return (!strcmp (rdt_kn_name (kn),"mon_groups" ) && +strcmp (name,"mon_groups" )); +} +static int mkdir_rdt_prepare (struct kernfs_node *parent_kn,const char *name,umode_t mode,enum rdt_group_type rtype,struct rdtgroup **r)
@@ -3568,6 +3584,15 @@ static int mkdir_rdt_prepare(struct kernfs_node *parent_kn, goto out_unlock; } +/* +* Check that the parent directory for a monitor group is a "mon_groups" +* directory. +*/ +if (rtype ==RDTMON_GROUP && !is_mon_groups (parent_kn, name)) { + ret = -EPERM ; + goto out_unlock; + } +if (rtype ==RDTMON_GROUP && (prdtgrp->mode ==RDT_MODE_PSEUDO_LOCKSETUP || prdtgrp->mode ==RDT_MODE_PSEUDO_LOCKED )) {
@@ -3751,22 +3776,6 @@ out_unlock:return ret; } -/* -* We allow creating mon groups only with in a directory called "mon_groups" -* which is present in every ctrl_mon group. Check if this is a valid -* "mon_groups" directory. -* -* 1. The directory should be named "mon_groups". -* 2. The mon group itself should "not" be named "mon_groups". -* This makes sure "mon_groups" directory always has a ctrl_mon group -* as parent. -*/ -static bool is_mon_groups (struct kernfs_node *kn,const char *name) -{ -return (!strcmp (rdt_kn_name (kn),"mon_groups" ) && -strcmp (name,"mon_groups" )); -} -static int rdtgroup_mkdir (struct kernfs_node *parent_kn,const char *name,umode_t mode) {
@@ -3782,11 +3791,8 @@ static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,if (resctrl_arch_alloc_capable () && parent_kn == rdtgroup_default.kn)return rdtgroup_mkdir_ctrl_mon (parent_kn, name, mode); -/* -* If RDT monitoring is supported and the parent directory is a valid -* "mon_groups" directory, add a monitoring subdirectory. -*/ -if (resctrl_arch_mon_capable () &&is_mon_groups (parent_kn, name)) +/* Else, attempt to add a monitoring subdirectory. */ +if (resctrl_arch_mon_capable ())return rdtgroup_mkdir_mon (parent_kn, name, mode);return -EPERM ;
Tejun Heo
maintainerQuoted 7 lines · Sebastian Andrzej Siewior
This started as a bug report by Hillf Danton and aims to access kernfs_node::{name|parent} with RCU to avoid the lock during
kernfs_path_from_node().I've split the individual fixes in separate patches (#1 to #4). I've also split the ::parent and ::name RCU conversation into a single patch (#5 and #6).
Greg, the whole series looks good to me. Probably ready to iterate in tree even if there are more updates to make?
Thanks.
Signature · 1 line
tejun
Greg Kroah-Hartman
maintainerQuoted 11 lines · Tejun Heo
Quoted 7 lines · Sebastian Andrzej Siewior
This started as a bug report by Hillf Danton and aims to access kernfs_node::{name|parent} with RCU to avoid the lock during
kernfs_path_from_node().I've split the individual fixes in separate patches (#1 to #4). I've also split the ::parent and ::name RCU conversation into a single patch (#5 and #6).
Greg, the whole series looks good to me. Probably ready to iterate in tree even if there are more updates to make?
Give me a chance to catch up with my pending patch review queue, I've been in CRA meetings lately and am way behind...
Don't worry, it's not lost.
thanks,
greg k-h