KCSAN: data-race in osq_lock / osq_lock (3)

Status: moderation: reported on 2026/11/17 07:31
Subsystems: kernfs
Labels: prio:high race:benign
[Documentation on labels]
Reported-by: syzbot+f010ceb693420310ffc6@syzkaller.appspotmail.com
First crash: 5h45m, last: 5h45m
✨ AI cause bisection: introduced by (AI job) :
commit 5aff60a191e579ae00ae5ca6ce16c13b687bc8a3
Author: Pan Xinhui <xinhui.pan@linux.vnet.ibm.com>
Date: Wed Nov 2 09:08:29 2016 +0000

  locking/osq: Break out of spin-wait busy waiting loop for a preempted vCPU in osq_lock()

  
✨ AI-generated root cause summary:

In kernel/locking/osq_lock.c, an unmarked plain write to osq_node.cpu at line 145 during lock entry races with an unmarked plain read of node->prev->cpu via node_cpu() at lines 34 and 177 in smp_cond_load_relaxed(). While CPU 1 spins on node->locked with node->prev pointing to CPU 0's per-CPU osq_node, CPU 0 can unlock or unqueue from the OSQ lock and immediately invoke osq_lock() again, rewriting node->cpu = curr while CPU 1 evaluates vcpu_is_preempted(node_cpu(node->prev)). Because osq_node.cpu is always written with the same constant value (encode_cpu(cpu)), the race is benign in practice but violates data race free rules under LKMM due to lack of atomic markings or one-time initialization.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 666539eecdf4:

Root Cause Analysis

The reported KCSAN bug is a data race between an unmarked plain write and an unmarked plain read on the cpu field of struct optimistic_spin_node in kernel/locking/osq_lock.c.

Involved Components & State

Mechanism Leading to the Race

  1. Queueing and Waiting:

    • When CPU 1 contends on an optimistic spin queue (in this case, via rwsem_down_write_slowpath() -> rwsem_optimistic_spin() -> osq_lock()), it exchanges lock->tail with its encoded CPU index (encode_cpu(1) = 2).
    • CPU 1 observes old tail pointing to CPU 0 (encode_cpu(0) = 1). CPU 1 sets node->prev = decode_cpu(old) (pointing to CPU 0's static per-CPU osq_node) and links itself to CPU 0's node via osq_link_next(prev, node).
    • CPU 1 then enters the wait loop in smp_cond_load_relaxed():
      if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
                                vcpu_is_preempted(node_cpu(node->prev))))
          return true;
      
    • In each iteration where node->locked is 0 and need_resched() is false, CPU 1 executes vcpu_is_preempted(node_cpu(node->prev)). The inline helper node_cpu(node->prev) performs an unmarked read:
      static inline int node_cpu(struct optimistic_spin_node *node)
      {
          return node->cpu - 1;
      }
      
      Here, node->prev->cpu is loaded without atomic markings (e.g., READ_ONCE()).
  2. Unlock / Unqueue and Re-entry:

    • osq_node instances are permanently resident per-CPU structures (DEFINE_PER_CPU_SHARED_ALIGNED).
    • While CPU 1 is spinning and has evaluated VAL as 0 in a loop iteration, CPU 0 can release the lock (osq_unlock() sets next->locked = 1) or unqueue itself (Step-C sets next->prev = prev).
    • CPU 0 completes its critical section or fast-paths to another lock acquisition and calls osq_lock() again (on the same or a different rwsem/mutex).
    • At the beginning of osq_lock(), line 145:
      node->locked = 0;
      node->next = NULL;
      node->cpu = curr;
      
      CPU 0 unconditionally performs a plain, unmarked store to node->cpu before updating lock->tail.
  3. Data Race:

    • CPU 0 executes the plain write node->cpu = curr; (kernel/locking/osq_lock.c:145) concurrently with CPU 1 executing the plain read node->cpu - 1 in node_cpu() (kernel/locking/osq_lock.c:34) on CPU 0's osq_node.
    • Because neither access uses READ_ONCE()/WRITE_ONCE() or synchronization barriers, KCSAN detects a data race under the Linux Kernel Memory Model (LKMM). Note that because CPU 0 always writes encode_cpu(0) (1) into its own osq_node.cpu, the stored value never varies across calls.
▶ ▼ ✨ AI Jobs (3)
ID Workflow Result Correct Bug Created Started Finished Revision
cd77486f bisect-cause ❓ KCSAN: data-race in osq_lock / osq_lock (3) 2026/11/17 07:23 2026/11/17 07:23 2026/11/17 07:59 a3e64cfc
99488b0e assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in osq_lock / osq_lock (3) 2026/11/17 07:23 2026/11/17 07:23 2026/11/17 07:44 a3e64cfc
1bf79867 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ✅ NetworkTrigger: ✅ PeripheralTrigger: ❌ RemoteTrigger: ✅ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ✅ VMHostTrigger: ✅ ❓ KCSAN: data-race in osq_lock / osq_lock (3) 2026/11/17 07:04 2026/11/17 07:04 2026/11/17 07:30 a3e64cfc
▶ ▼ Similar bugs (2)
Kernel Title Rank 🛈 Repro Cause bisect Fix bisect Count Last Reported Patched Status
upstream KCSAN: data-race in osq_lock / osq_lock (2) mm 6 1 2526d 2526d 0/29 closed as invalid on 2020/01/23 11:36
upstream KCSAN: data-race in osq_lock / osq_lock mm 6 26 2562d 2581d 0/29 closed as invalid on 2019/11/19 14:54

Sample crash report:
==================================================================
BUG: KCSAN: data-race in osq_lock / osq_lock

write to 0xffff888237c2cd94 of 4 bytes by task 17185 on cpu 0:
 osq_lock+0x63/0x250 kernel/locking/osq_lock.c:145
 rwsem_optimistic_spin kernel/locking/rwsem.c:848 [inline]
 rwsem_down_write_slowpath+0xf7/0xcc0 kernel/locking/rwsem.c:1145
 __down_write_common kernel/locking/rwsem.c:1347 [inline]
 __down_write kernel/locking/rwsem.c:1356 [inline]
 down_write+0xd3/0xf0 kernel/locking/rwsem.c:1632
 kernfs_add_one+0x5a/0x310 fs/kernfs/dir.c:833
 __kernfs_create_file+0x145/0x180 fs/kernfs/file.c:1100
 sysfs_add_file_mode_ns+0x132/0x1b0 fs/sysfs/file.c:316
 create_files fs/sysfs/group.c:82 [inline]
 internal_create_group+0x43e/0x9e0 fs/sysfs/group.c:189
 internal_create_groups fs/sysfs/group.c:229 [inline]
 sysfs_create_groups+0x3f/0xf0 fs/sysfs/group.c:255
 setup_gid_attrs drivers/infiniband/core/sysfs.c:1123 [inline]
 ib_setup_port_attrs+0xeda/0x14a0 drivers/infiniband/core/sysfs.c:1399
 add_one_compat_dev+0x25c/0x360 drivers/infiniband/core/device.c:1002
 rdma_dev_init_net+0x17d/0x210 drivers/infiniband/core/device.c:1227
 ops_init+0x22a/0x2e0 net/core/net_namespace.c:137
 setup_net+0x8e/0x220 net/core/net_namespace.c:448
 copy_net_ns+0x379/0x4e0 net/core/net_namespace.c:589
 create_new_namespaces+0x208/0x440 kernel/nsproxy.c:132
 unshare_nsproxy_namespaces+0x115/0x150 kernel/nsproxy.c:234
 ksys_unshare+0x39b/0x670 kernel/fork.c:3306
 __do_sys_unshare kernel/fork.c:3372 [inline]
 __se_sys_unshare kernel/fork.c:3370 [inline]
 __x64_sys_unshare+0x1f/0x30 kernel/fork.c:3370
 x64_sys_call+0x1e33/0x2550 arch/x86/include/generated/asm/syscalls_64.h:273
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

read to 0xffff888237c2cd94 of 4 bytes by task 17174 on cpu 1:
 node_cpu kernel/locking/osq_lock.c:34 [inline]
 osq_lock+0x11c/0x250 kernel/locking/osq_lock.c:177
 rwsem_optimistic_spin kernel/locking/rwsem.c:848 [inline]
 rwsem_down_write_slowpath+0xf7/0xcc0 kernel/locking/rwsem.c:1145
 __down_write_common kernel/locking/rwsem.c:1347 [inline]
 __down_write kernel/locking/rwsem.c:1356 [inline]
 down_write+0xd3/0xf0 kernel/locking/rwsem.c:1632
 kernfs_add_one+0x5a/0x310 fs/kernfs/dir.c:833
 __kernfs_create_file+0x145/0x180 fs/kernfs/file.c:1100
 sysfs_add_file_mode_ns+0x132/0x1b0 fs/sysfs/file.c:316
 create_files fs/sysfs/group.c:82 [inline]
 internal_create_group+0x43e/0x9e0 fs/sysfs/group.c:189
 internal_create_groups fs/sysfs/group.c:229 [inline]
 sysfs_create_groups+0x3f/0xf0 fs/sysfs/group.c:255
 device_add_groups drivers/base/core.c:2913 [inline]
 device_add_attrs+0x64/0x410 drivers/base/core.c:2977
 device_add+0x37a/0x7c0 drivers/base/core.c:3717
 netdev_register_kobject+0x127/0x250 net/core/net-sysfs.c:2381
 register_netdevice+0xc68/0x1400 net/core/dev.c:11740
 register_netdev+0x3b/0x50 net/core/dev.c:11857
 ip6_tnl_init_net+0x22d/0x310 net/ipv6/ip6_tunnel.c:2364
 ops_init+0x22a/0x2e0 net/core/net_namespace.c:137
 setup_net+0x8e/0x220 net/core/net_namespace.c:448
 copy_net_ns+0x379/0x4e0 net/core/net_namespace.c:589
 create_new_namespaces+0x208/0x440 kernel/nsproxy.c:132
 unshare_nsproxy_namespaces+0x115/0x150 kernel/nsproxy.c:234
 ksys_unshare+0x39b/0x670 kernel/fork.c:3306
 __do_sys_unshare kernel/fork.c:3372 [inline]
 __se_sys_unshare kernel/fork.c:3370 [inline]
 __x64_sys_unshare+0x1f/0x30 kernel/fork.c:3370
 x64_sys_call+0x1e33/0x2550 arch/x86/include/generated/asm/syscalls_64.h:273
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 17174 Comm: syz-executor Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================

Crashes (1):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/17 02:17 upstream 666539eecdf4 a3e64cfc .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in osq_lock / osq_lock
* Struck through repros no longer work on HEAD.