Skip to contentSkip to conversation

xarray: add READ_ONCE() in xas_find_chunk()

PATCH New Shuai Liu 3 participants 3 messages Nov 29 – Dec 2

Subsystems (3)
lore
Share & tools
lore

Shuai Liu

author
#0 lore raw

PATCH 1/1 xarray: add READ_ONCE() in xas_find_chunk()

xas_find_chunk() reads xa_node->marks with a plain dereference while concurrent xa_set_mark()/xa_clear_mark() writers use non-atomic __test_and_set_bit()/__test_and_clear_bit() under xa_lock. RCU readers in xa_find() -> xas_find_marked() -> xas_find_chunk() call this without xa_lock, so the plain read races with the locked non-atomic write.

KCSAN flags this as a data race, and Marco Elver notes the failure scenario is worse than benign: the compiler can hoist *addr into a register on the reader. When a concurrent writer later clears a mark bit, the cached stale value causes xas_find_chunk() to keep returning the cleared offset. xas_find_marked() then loops forever looking for an entry at that offset under rcu_read_lock().

Load the marks word once via READ_ONCE(*addr) into a local variable, then do all bit operations on that snapshot. This silences KCSAN, forces a fresh load on each call, and prevents the compiler from caching the stale value across iterations.

---
 include/linux/xarray.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/xarray.h +4 -2
index be850174e802e..8c898b846bde8 100644
@@ -1744,19 +1744,21 @@ static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
 {
 	unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
 	unsigned int offset = xas->xa_offset;
+	unsigned long marks;
 
 	if (advance)
 		offset++;
+	marks = READ_ONCE(*addr);
 	if (XA_CHUNK_SIZE == BITS_PER_LONG) {
 		if (offset < XA_CHUNK_SIZE) {
-			unsigned long data = *addr & (~0UL << offset);
+			unsigned long data = marks & (~0UL << offset);
 			if (data)
 				return __ffs(data);
 		}
 		return XA_CHUNK_SIZE;
 	}
 
-	return find_next_bit(addr, XA_CHUNK_SIZE, offset);
+	return find_next_bit(&marks, XA_CHUNK_SIZE, offset);
 }
 
 /**
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Critical] xarray: stack out-of-bounds read and broken 32-bit mark search in xas_find_chunk() - [Low]…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Critical] xarray: stack out-of-bounds read and broken 32-bit mark search in xas_find_chunk()
- [Low] xarray: missing Fixes: trailer for xas_find_chunk() data race fix
--

commit c0ac2830e45356e29b88c9ec8263075b4152d250
Author: Shuai Liu <[email protected]>

Quoted 15 lines

xarray: add READ_ONCE() in xas_find_chunk()

xas_find_chunk() reads xa_node->marks with a plain dereference while concurrent xa_set_mark()/xa_clear_mark() writers use non-atomic __test_and_set_bit()/__test_and_clear_bit() under xa_lock. RCU readers in xa_find() -> xas_find_marked() -> xas_find_chunk() call this without xa_lock, so the plain read races with the locked non-atomic write.

KCSAN flags this as a data race, and Marco Elver notes the failure scenario is worse than benign: the compiler can hoist *addr into a register on the reader. When a concurrent writer later clears a mark bit, the cached stale value causes xas_find_chunk() to keep returning the cleared offset. xas_find_marked() then loops forever looking for an entry at that offset under rcu_read_lock().

Quoted 7 lines

Load the marks word once via READ_ONCE(*addr) into a local variable, then do all bit operations on that snapshot. This silences KCSAN, forces a fresh load on each call, and prevents the compiler from caching the stale value across iterations.

[Severity: Low]
Should this bug fix include a Fixes: tag for commit b803b42823d0 ("xarray:
Add XArray iterators"), which introduced xas_find_chunk()?

Quoted 16 lines
include/linux/xarray.h +2 -1
index be850174e802e..8c898b846bde8 100644
@@ -1744,19 +1744,21 @@ static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
 {
 	unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
 	unsigned int offset = xas->xa_offset;
+	unsigned long marks;
 
 	if (advance)
 		offset++;
+	marks = READ_ONCE(*addr);
 	if (XA_CHUNK_SIZE == BITS_PER_LONG) {
 		if (offset < XA_CHUNK_SIZE) {
-			unsigned long data = *addr & (~0UL << offset);

include/linux/xarray.h static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance, · line 1762

Quoted 10 lines
include/linux/xarray.h +2 -1
+			unsigned long data = marks & (~0UL << offset);
 			if (data)
 				return __ffs(data);
 		}
 		return XA_CHUNK_SIZE;
 	}
 
-	return find_next_bit(addr, XA_CHUNK_SIZE, offset);
+	return find_next_bit(&marks, XA_CHUNK_SIZE, offset);
 }

[Severity: Critical]
Does passing &marks to find_next_bit() read out-of-bounds from the stack on 32-bit architectures?

When CONFIG_BASE_SMALL is disabled, XA_CHUNK_SIZE is 64, so on 32-bit systems XA_MARK_LONGS is 2 and xas->xa_node->marks[mark] spans two 32-bit words. Because marks is a single unsigned long initialized with only READ_ONCE(*addr), find_next_bit(&marks, 64, offset) reads (&marks)[1] four bytes past the stack variable and never inspects addr[1] (slots 32..63).

Besides missing marked entries in slots 32..63, if (&marks)[1] on the stack has a bit set at an offset where node->slots[offset] is NULL, xas_find_marked() sets advance = false and xas->xa_offset = offset, sees no entry, and loops forever under rcu_read_lock() because xas_find_chunk() keeps returning the same offset from stack memory.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Shuai Liu
kernel test robot noticed "INFO:rcu_preempt_detected_stalls_on_CPUs/tasks" on:

Hello,

kernel test robot noticed "INFO:rcu_preempt_detected_stalls_on_CPUs/tasks" on:

commit: 7261a26dbf27afa6d894daa20cfd20a6dd35452c ("[PATCH] xarray: add READ_ONCE() in xas_find_chunk()")
url: https://github.com/intel-lab-lkp/linux/commits/Shuai-Liu/xarray-add-READ_ONCE-in-xas_find_chunk/20261129-150100
base: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git 4f069ab1ec243e6b60dc438772905855c60774e8 patch link: https://lore.kernel.org/all/[email protected]/
patch subject: [PATCH] xarray: add READ_ONCE() in xas_find_chunk()

in testcase: boot

config: i386-randconfig-141-20250330
compiler: gcc-14
test machine: qemu-system-i386 -enable-kvm -cpu SandyBridge -smp 4 -m 4G

(please refer to attached dmesg/kmsg for entire log/backtrace)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-lkp/[email protected]

[  127.996497][    C0] rcu: INFO: rcu_preempt detected stalls on CPUs/tasks:
[  128.006070][    C0] rcu: 	Tasks blocked on level-0 rcu_node (CPUs 0-0): P273
[  128.012204][    C0] rcu: 	(detected by 0, t=26252 jiffies, g=13441, q=932 ncpus=1)
[  128.017926][    C0] task:dpkg            state:R  running task     stack:6188  pid:273   tgid:273   ppid:222    task_flags:0x400100 flags:0x00080000
[  128.028151][    C0] Call Trace:
[  128.031404][    C0]  sched_show_task (sched/sched.h:1969)
[  128.035741][    C0]  sched_show_task (sched/core.c:8163 (discriminator 1))
[  128.043617][    C0]  print_other_cpu_stall (rcu/tree_stall.h:300 rcu/tree_stall.h:709)
[  128.047839][    C0]  ? __lock_release+0x48/0x110
[  128.052072][    C0]  check_cpu_stall+0x118/0x1a0
[  128.056630][    C0]  rcu_pending (rcu/tree.c:3838)
[  128.060374][    C0]  ? _raw_spin_unlock_irqrestore (linux/spinlock_api_smp.h:209 locking/spinlock.c:221)
[  128.064544][    C0]  rcu_sched_clock_irq (rcu/tree.c:2749)
[  128.072294][    C0]  update_process_times (time/timer.c:2476)
[  128.076305][    C0]  tick_periodic+0xc0/0xd0
[  128.080626][    C0]  tick_handle_periodic (time/tick-common.c:114)
[  128.084512][    C0]  ? sysvec_hyperv_stimer0 (x86/kernel/cpu/mshyperv.c:192)
[  128.092780][    C0]  __sysvec_apic_timer_interrupt (x86/kernel/apic/apic.c:1051 x86/kernel/apic/apic.c:1068)
[  128.097308][    C0]  sysvec_apic_timer_interrupt (x86/kernel/apic/apic.c:1062 (discriminator 1) x86/kernel/apic/apic.c:1062 (discriminator 1))
[  128.101825][    C0]  handle_exception (x86/entry/entry_32.S:1044)
[  128.105854][    C0] EIP: lock_is_held_type (locking/lockdep.c:6001)
[  128.113584][    C0] Code: 0f 94 c0 0f b6 c0 eb 0a b8 01 00 00 00 31 d2 31 c9 c3 8d 65 f4 5b 5e 5f 5d 31 d2 31 c9 c3 55 89 e5 57 56 53 83 ec 10 89 45 ec <83> c8 ff 83 3d 0c 7a 7f 43 00 0f 84 97 00 00 00 89 d3 8b 15 24 61
All code
========
   0:	0f 94 c0             	sete   %al
   3:	0f b6 c0             	movzbl %al,%eax
   6:	eb 0a                	jmp    0x12
   8:	b8 01 00 00 00       	mov    $0x1,%eax
   d:	31 d2                	xor    %edx,%edx
   f:	31 c9                	xor    %ecx,%ecx
  11:	c3                   	ret
  12:	8d 65 f4             	lea    -0xc(%rbp),%esp
  15:	5b                   	pop    %rbx
  16:	5e                   	pop    %rsi
  17:	5f                   	pop    %rdi
  18:	5d                   	pop    %rbp
  19:	31 d2                	xor    %edx,%edx
  1b:	31 c9                	xor    %ecx,%ecx
  1d:	c3                   	ret
  1e:	55                   	push   %rbp
  1f:	89 e5                	mov    %esp,%ebp
  21:	57                   	push   %rdi
  22:	56                   	push   %rsi
  23:	53                   	push   %rbx
  24:	83 ec 10             	sub    $0x10,%esp
  27:	89 45 ec             	mov    %eax,-0x14(%rbp)
  2a:*	83 c8 ff             	or     $0xffffffff,%eax		<-- trapping instruction
  2d:	83 3d 0c 7a 7f 43 00 	cmpl   $0x0,0x437f7a0c(%rip)        # 0x437f7a40
  34:	0f 84 97 00 00 00    	je     0xd1
  3a:	89 d3                	mov    %edx,%ebx
  3c:	8b                   	.byte 0x8b
  3d:	15                   	.byte 0x15
  3e:	24 61                	and    $0x61,%al

Code starting with the faulting instruction ===========================================

   0:	83 c8 ff             	or     $0xffffffff,%eax
   3:	83 3d 0c 7a 7f 43 00 	cmpl   $0x0,0x437f7a0c(%rip)        # 0x437f7a16
   a:	0f 84 97 00 00 00    	je     0xa7
  10:	89 d3                	mov    %edx,%ebx
  12:	8b                   	.byte 0x8b
  13:	15                   	.byte 0x15
  14:	24 61                	and    $0x61,%al
[  128.126904][    C0] EAX: bdfd0ddc EBX: 00000000 ECX: 00000020 EDX: ffffffff
[  128.132168][    C0] ESI: bdfd0dcc EDI: bdff7720 EBP: bd985dcc ESP: bd985db0
[  128.137356][    C0] DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 0068 EFLAGS: 00000282
[  128.142911][    C0]  ? dentry_name (vsprintf.c:929)
[  128.146770][    C0]  ? sysvec_hyperv_stimer0 (x86/kernel/cpu/mshyperv.c:192)
[  128.150956][    C0]  ? sysvec_hyperv_stimer0 (x86/kernel/cpu/mshyperv.c:192)
[  128.155173][    C0]  ? lock_is_held_type (locking/lockdep.c:6001)
[  128.163355][    C0]  lock_is_held (linux/lockdep.h:249)
[  128.167228][    C0]  xa_entry (linux/xarray.h:1226 (discriminator 3))
[  128.170871][    C0]  xas_find_marked (xarray.c:1528)
[  128.174932][    C0]  find_get_entry (filemap.c:2116)
[  128.178805][    C0]  filemap_get_folios_tag (filemap.c:2382)
[  128.182994][    C0]  __filemap_fdatawait_range (filemap.c:522)
[  128.187290][    C0]  ? validate_chain (locking/lockdep.c:3865 locking/lockdep.c:3920)
[  128.199120][    C0]  ? mark_lock (locking/lockdep.c:4775)
[  128.202891][    C0]  ? __lock_acquire (locking/lockdep.c:5288)
[  128.206882][    C0]  ? lock_acquire (linux/srcutree.h:387 linux/srcu.h:648 linux/srcu.h:752 trace/events/lock.h:23 trace/events/lock.h:23 locking/lockdep.c:5905)
[  128.210982][    C0]  file_fdatawait_range (filemap.c:604)
[  128.215106][    C0]  sync_file_range (sync.c:275)
[  128.218826][    C0]  ksys_sync_file_range (sync.c:356)
[  128.222337][    C0]  __ia32_sys_ia32_sync_file_range (x86/kernel/sys_ia32.c:108 x86/kernel/sys_ia32.c:104 x86/kernel/sys_ia32.c:104)
[  128.226522][    C0]  ia32_sys_call (kbuild/obj/consumer/i386-randconfig-141-20250330/./arch/x86/include/generated/asm/syscalls_32.h:315)
[  128.230212][    C0]  __do_fast_syscall_32 (x86/entry/syscall_32.c:79 x86/entry/syscall_32.c:286)
[  128.234153][    C0]  do_fast_syscall_32 (x86/entry/syscall_32.c:311)
[  128.237932][    C0]  do_SYSENTER_32 (x86/entry/syscall_32.c:349)
[  128.241431][    C0]  entry_SYSENTER_32 (x86/entry/entry_32.S:835)
[  128.245201][    C0] EIP: 0x37f7f10c
[  128.248245][    C0] Code: d2 74 05 c1 e8 0c 89 02 8b 5d fc 31 c0 c9 31 d2 31 c9 c3 90 90 90 90 90 90 90 90 90 90 90 8d 76 00 51 52 55 89 e5 0f 34 cd 80 <5d> 5a 59 c3 90 2e 8d b4 26 00 00 00 00 8d b4 26 00 00 00 00 58 b8
All code
========
   0:	d2 74 05 c1          	shlb   %cl,-0x3f(%rbp,%rax,1)
   4:	e8 0c 89 02 8b       	call   0xffffffff8b028915
   9:	5d                   	pop    %rbp
   a:	fc                   	cld
   b:	31 c0                	xor    %eax,%eax
   d:	c9                   	leave
   e:	31 d2                	xor    %edx,%edx
  10:	31 c9                	xor    %ecx,%ecx
  12:	c3                   	ret
  13:	90                   	nop
  14:	90                   	nop
  15:	90                   	nop
  16:	90                   	nop
  17:	90                   	nop
  18:	90                   	nop
  19:	90                   	nop
  1a:	90                   	nop
  1b:	90                   	nop
  1c:	90                   	nop
  1d:	90                   	nop
  1e:	8d 76 00             	lea    0x0(%rsi),%esi
  21:	51                   	push   %rcx
  22:	52                   	push   %rdx
  23:	55                   	push   %rbp
  24:	89 e5                	mov    %esp,%ebp
  26:	0f 34                	sysenter
  28:	cd 80                	int    $0x80
  2a:*	5d                   	pop    %rbp		<-- trapping instruction
  2b:	5a                   	pop    %rdx
  2c:	59                   	pop    %rcx
  2d:	c3                   	ret
  2e:	90                   	nop

2f: 2e 8d b4 26 00 00 00 cs lea 0x0(%rsi,%riz,1),%esi
36: 00
37: 8d b4 26 00 00 00 00 lea 0x0(%rsi,%riz,1),%esi
3e: 58 pop %rax
3f: b8 .byte 0xb8

Code starting with the faulting instruction ===========================================

   0:	5d                   	pop    %rbp
   1:	5a                   	pop    %rdx
   2:	59                   	pop    %rcx
   3:	c3                   	ret
   4:	90                   	nop

5: 2e 8d b4 26 00 00 00 cs lea 0x0(%rsi,%riz,1),%esi
c: 00
d: 8d b4 26 00 00 00 00 lea 0x0(%rsi,%riz,1),%esi
14: 58 pop %rax
15: b8 .byte 0xb8

The kernel config and materials to reproduce are available at:
https://download.01.org/0day-ci/archive/20261202/[email protected]

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki