| 0/0 |
2026/06/01 05:39 |
flow |
assessment-security |
41m
Results: map[DenialOfService:false Explanation:**Analysis of the Bug Report**
The provided bug report is a Kernel Concurrency Sanitizer (KCSAN) data-race
warning in the maple tree data structure, specifically occurring between
`mte_set_node_dead()` (the writer) and `ma_dead_node()` (the reader).
**Root Cause and Mechanism:**
1. **The Writer (`mprotect` path):** When a VMA is modified (e.g., split by
`mprotect`), the maple tree is updated. The old maple tree node is replaced and
marked as "dead" before being scheduled for RCU freeing. Marking a node as dead
is done in `mte_set_node_dead()` by setting its parent pointer to point to
itself:
`mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));`
2. **The Reader (Page fault path):** Concurrently, a speculative page fault may
be performing a lockless VMA lookup via `lock_vma_under_rcu()`. During the tree
traversal, `ma_dead_node()` checks if the current node has been marked dead by
reading its parent pointer:
`parent = (void *)((unsigned long) node->parent & ~MAPLE_NODE_MASK);`
Because both the write and the read are performed using plain memory accesses
(lacking `WRITE_ONCE()` and `READ_ONCE()` annotations), KCSAN correctly
identifies this as a data race.
**Security Impact:**
This is a **benign data race** and does not pose a security risk. It is an
expected race condition inherent to the lockless RCU-based design of the maple
tree:
* **Safe Stale Reads:** If the reader reads the old parent pointer (before the
writer marks it dead), it will simply conclude the node is still valid and
continue traversing it. Because the node's memory is protected by RCU, it
remains allocated and contains a perfectly valid, albeit stale, snapshot of the
tree.
* **VMA Sequence Locks:** If the lockless traversal ultimately returns a stale
VMA due to this race, the speculative page fault handler will detect the
concurrent modification by checking the VMA's sequence lock
(`vma->vm_lock.seq`). It will then safely abort the lockless fault and fall back
to acquiring the `mmap_lock`.
* **No Tearing Risks:** On 64-bit architectures, aligned 8-byte pointer
reads/writes are atomic. Even on architectures where load tearing could
theoretically occur, a torn read would yield a garbage pointer that does not
equal the node's address, causing the reader to safely treat the node as "not
dead" (equivalent to reading the old state).
**Conclusion:**
The bug is **not exploitable** and cannot lead to memory corruption, privilege
escalation, or Denial of Service (DoS). It is easily reachable by **unprivileged
users** (and within user namespaces) by simply racing `mprotect()` calls with
memory accesses (page faults) on the same memory region. The fix would merely
involve adding `READ_ONCE()` and `WRITE_ONCE()` to silence the sanitizer. Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:true UserNamespace:true VMGuestTrigger:false VMHostTrigger:false]
|
| 1/1 |
2026/06/01 05:40 |
action |
syz-repro-to-c-repro |
0m
Results:
|
| 2/1 |
2026/06/01 05:40 |
action |
kernel-checkouter |
1m
Results: map[KernelSrc:/app/workdir/cache/src/6b12ac9f348e90c52e83540d2cad8cefe70157a5]
|
| 3/1 |
2026/06/01 05:41 |
action |
kernel-builder |
18m
Results: map[KernelObj:/app/workdir/cache/build/7d88bcd6d436ac1cd01475eaa62d35f84e50c78d]
|
| 4/1 |
2026/06/01 06:00 |
action |
codesearch-prepare |
16m
Results: map[Index:codesearch-index]
|
| 5/1 |
2026/06/01 06:16 |
agent |
expert |
4m
Model:
Results: map[DenialOfService:false Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:true UserNamespace:true VMGuestTrigger:false VMHostTrigger:false]
Instruction: You are an experienced Linux kernel security engineer. Your task is to analyze given kernel bug report
and determine its security impact based on the following dimensions.
Use the provided tools to examine the source code, check for capability checks (e.g., capable(), ns_capable()),
and understand the nature of the bug. Analyze the given kernel build and configuration.
You can check the kernel config by grepping ".config" file; you can check kernel cmdline by grepping
".config" file for "CONFIG_CMDLINE=". Assume sysctl parameters have default values.
But analyze for the corresponding production build w/o debugging tools enabled (like KASAN, KMSAN, UBSAN).
Try different strategies when analyzing the bug:
- think of ways in which the vulnerable code is unreachable
- or the other way around: try to come up with different ideas of how an unprivileged user can reach the bug
If still unsure err on the side of the bug being non-exploitable/not-accessible.
In the final reply, provide a reasoning for your assessment.
Analysis dimensions:
* Exploitable:
Determine if the bug can result in memory corruption or elevated privileges.
Memory safety issues are almost always exploitable (KASAN or UBSAN reports for use-after-free, out-of-bounds;
refcounting issues, corrupted lists, etc). When kernel is crashing on a completely wild pointer access
(e.g. user-space address, or non-canonical address, but not on NULL or address corresponding to KASAN shadow
for NULL address), including both data accesses and control transfers, that also usually implies possibility
of exploitation. Such reports usually say "unable to handle kernel paging request".
Uses of uninitialized values detected by KMSAN may be exploitable b/c attacker frequently can affect uninit
values with spraying techniques. However, for these exploitability depends on how exactly the uninit value
is used in the code, and what it affects.
Think of what happens after the bug is triggered. Some bugs cause kernel panic and halt execution,
they are harder to exploit. For example, BUG reports halts the kernel. However, WARNING reports don't halt
execution in production builds. Debug bug detection tools (like KASAN, KMSAN, KCSAN, UBSAN) are also not enabled
in production builds, so attacker can freely exploit these bugs w/o being detected by these tools.
If you see an integer overflow, think how the overflowed value used later (if it's used as allocation size,
or an array index). If you see an out-of-bounds read, think if it's followed by an out-of-bounds write as well.
Some KCSAN data-races may be exploitable by skilled attackers as well. Think what data structures got corrupted
as the result of data races and how. However, note that kernel has lots of "benign" data races that don't lead
to any runtime misbehavior at all.
* Denial Of Service:
Determine if the bug can result in denial-of-service. Most bugs can, since they cause system crash,
hangs, deadlocks, or resource leaks. This is mostly applicable to WARNING bugs that won't cause system crash
in production. For these think what will be consequences of the violation of the kernel assumptions flagged
by the WARNING. In some cases the unexpected condition is also properly handled by the normal control flow
(e.g. with "if (WARN_ON(...))"), these won't cause denial-of-service. If the condition is not handled,
then it may or may not cause denial-of-service.
* Accessible From Unprivileged Processes:
Determine if the bug can be reached from a typical (non-root) user process that does NOT have any special capabilities
(like CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_NET_RAW, CAP_PERFMON) or access to device nodes restricted to root.
Assume that unprivileged_bpf_disabled=1, that is eBPF loading is not accessible. However, cBPF (classical BPF)
is still accessible to non-root processes.
Assume that user namespaces are not accessible, that is, the process cannot get the mentioned capabilities even
within a new user namespace (checked by ns_capable() function in the kernel sources).
* Accessible From User Namespaces:
Determine if the bug can be reached within a user-namespace where the process has all capabilities
(including CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_NET_RAW, CAP_PERFMON). Such capabilities are checked with ns_capable()
function in the kernel sources.
* VM Guest Trigger:
Determine if the bug can be triggered from the context of a typical KVM guest (e.g., set up by a QEMU VMM).
Consider accesses to standard Linux host paravirtualized features (virtio-blk, virtio-net, etc.),
and handling of VM exits in the KVM code.
* VM Host Trigger in The Confidential Computing Context:
Determine if the bug can be triggered in a confidential computing guest kernel from the context of a KVM host.
Consider access to standard Linux guest paravirtualized features (virtio-blk, virtio-net, etc.).
* Ethernet Network Trigger:
Determine if the bug can be triggered by processing ingress network Ethernet traffic, either directly (network stack)
or via drivers exposed to network data.
* Other Remote Trigger:
Determine if the bug can be triggered by processing remote traffic other than Ethernet (Wifi, Bluetooth, NFC, etc).
* Peripheral Trigger:
Determine if the bug can be triggered via an untrusted peripheral device that can be physically plugged
into a system, such as a USB device or a niche hardware driver handling external hardware inputs.
This is particularly important for mobile and desktop environments where users can plug in unknown devices.
* Malicious Filesystem Trigger:
Determine if the bug can be triggered by the kernel mounting and parsing a malicious filesystem image.
This is highly critical for Desktop and Mobile environments where external media or downloaded images
might be auto-mounted.
Don't make assumptions about the kernel source code (it may be different from what you assume it is).
Extensively use the provided code access tools (codesearch-*, git-*, grepper, etc)
to examine the actual source code, and confirm any assumptions.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt:
The kernel bug report is:
==================================================================
BUG: KCSAN: data-race in mas_state_walk / mas_wmb_replace
write to 0xffff888105db0500 of 8 bytes by task 11474 on cpu 0:
mte_set_node_dead lib/maple_tree.c:334 [inline]
mas_put_in_tree lib/maple_tree.c:1565 [inline]
mas_topiary_replace lib/maple_tree.c:2341 [inline]
mas_wmb_replace+0x20d/0x1610 lib/maple_tree.c:2433
mas_split lib/maple_tree.c:3052 [inline]
mas_commit_b_node lib/maple_tree.c:3072 [inline]
mas_wr_bnode lib/maple_tree.c:3739 [inline]
mas_wr_store_entry+0x16b6/0x2db0 lib/maple_tree.c:3771
mas_store_prealloc+0x72e/0xa40 lib/maple_tree.c:5169
vma_iter_store_overwrite mm/vma.h:607 [inline]
vma_iter_store_new mm/vma.h:614 [inline]
vma_complete+0x3c7/0x850 mm/vma.c:356
__split_vma+0x5db/0x640 mm/vma.c:566
split_vma mm/vma.c:596 [inline]
vma_modify+0xbc9/0xd00 mm/vma.c:1680
vma_modify_flags+0x182/0x210 mm/vma.c:1700
mprotect_fixup+0x312/0x5d0 mm/mprotect.c:756
do_mprotect_pkey+0x67b/0x920 mm/mprotect.c:930
__do_sys_mprotect mm/mprotect.c:951 [inline]
__se_sys_mprotect mm/mprotect.c:948 [inline]
__x64_sys_mprotect+0x48/0x60 mm/mprotect.c:948
x64_sys_call+0x2768/0x3020 arch/x86/include/generated/asm/syscalls_64.h:11
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0x12c/0x3b0 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff888105db0500 of 8 bytes by task 11476 on cpu 1:
ma_dead_node lib/maple_tree.c:572 [inline]
mtree_range_walk lib/maple_tree.c:2580 [inline]
mas_state_walk+0x485/0x650 lib/maple_tree.c:3298
mas_walk+0x60/0x150 lib/maple_tree.c:4599
lock_vma_under_rcu+0xc9/0x210 mm/mmap_lock.c:304
do_user_addr_fault+0x232/0x1050 arch/x86/mm/fault.c:1325
handle_page_fault arch/x86/mm/fault.c:1474 [inline]
exc_page_fault+0x62/0xa0 arch/x86/mm/fault.c:1527
asm_exc_page_fault+0x26/0x30 arch/x86/include/asm/idtentry.h:618
value changed: 0xffff88811907a8c1 -> 0xffff888105db0500
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 11476 Comm: syz.1.2173 Tainted: G W syzkaller #0 PREEMPT(full)
Tainted: [W]=WARN
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 03/18/2026
==================================================================
Reply: **Analysis of the Bug Report**
The provided bug report is a Kernel Concurrency Sanitizer (KCSAN) data-race warning in the maple tree data structure, specifically occurring between `mte_set_node_dead()` (the writer) and `ma_dead_node()` (the reader).
**Root Cause and Mechanism:**
1. **The Writer (`mprotect` path):** When a VMA is modified (e.g., split by `mprotect`), the maple tree is updated. The old maple tree node is replaced and marked as "dead" before being scheduled for RCU freeing. Marking a node as dead is done in `mte_set_node_dead()` by setting its parent pointer to point to itself:
`mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));`
2. **The Reader (Page fault path):** Concurrently, a speculative page fault may be performing a lockless VMA lookup via `lock_vma_under_rcu()`. During the tree traversal, `ma_dead_node()` checks if the current node has been marked dead by reading its parent pointer:
`parent = (void *)((unsigned long) node->parent & ~MAPLE_NODE_MASK);`
Because both the write and the read are performed using plain memory accesses (lacking `WRITE_ONCE()` and `READ_ONCE()` annotations), KCSAN correctly identifies this as a data race.
**Security Impact:**
This is a **benign data race** and does not pose a security risk. It is an expected race condition inherent to the lockless RCU-based design of the maple tree:
* **Safe Stale Reads:** If the reader reads the old parent pointer (before the writer marks it dead), it will simply conclude the node is still valid and continue traversing it. Because the node's memory is protected by RCU, it remains allocated and contains a perfectly valid, albeit stale, snapshot of the tree.
* **VMA Sequence Locks:** If the lockless traversal ultimately returns a stale VMA due to this race, the speculative page fault handler will detect the concurrent modification by checking the VMA's sequence lock (`vma->vm_lock.seq`). It will then safely abort the lockless fault and fall back to acquiring the `mmap_lock`.
* **No Tearing Risks:** On 64-bit architectures, aligned 8-byte pointer reads/writes are atomic. Even on architectures where load tearing could theoretically occur, a torn read would yield a garbage pointer that does not equal the node's address, causing the reader to safely treat the node as "not dead" (equivalent to reading the old state).
**Conclusion:**
The bug is **not exploitable** and cannot lead to memory corruption, privilege escalation, or Denial of Service (DoS). It is easily reachable by **unprivileged users** (and within user namespaces) by simply racing `mprotect()` calls with memory accesses (page faults) on the same memory region. The fix would merely involve adding `READ_ONCE()` and `WRITE_ONCE()` to silence the sanitizer.
|
| 6/2 |
2026/06/01 06:16 |
llm |
expert |
0m
Model:
Tokens:
input: 6609
output: 40
thoughts: 141
Thoughts: Here's my analysis of this data-race report:
**Data Race Deep Dive**
Okay, so we've got a KCSAN report flagged on `mas_state_walk` and `mas_wmb_replace`. My initial thought is to dive right into the source. The report clearly pinpoints a data race, a classic threading issue, involving a write in `mte_set_node_dead` and a read in `ma_dead_node`. This is interesting because the write happens within the nested call chain of `mas_wmb_replace`, which originates from `mas_topiary_replace`, then `mas_put_in_tree`, and finally resolves to `mte_set_node_dead`. The read, on the other hand, is part of `mas_state_walk`, which calls `mtree_range_walk`, and ultimately hits `ma_dead_node`. This breakdown of the call stacks helps narrow down the potential conflict. Time to pull up the code and start tracing the execution paths to pinpoint *exactly* where the synchronization failures are occurring.
|
| 7/2 |
2026/06/01 06:16 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/maple_tree.c Name:mte_set_node_dead]
Results: map[SourceCode: 328: /*
329: * mte_set_node_dead() - Set a maple encoded node as dead.
330: * @mn: The maple encoded node.
331: */
332: static inline void mte_set_node_dead(struct maple_enode *mn)
333: {
334: mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));
335: smp_wmb(); /* Needed for RCU */
336: }
SourceFile:lib/maple_tree.c]
|
| 8/2 |
2026/06/01 06:16 |
llm |
expert |
0m
Model:
Tokens:
input: 7206
output: 37
thoughts: 43
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person:
**Analyzing the Node Parent Pointer Assignment**
Alright, let's break down this line of code: `mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));`. Seems we're dealing with a memory management or graph structure, given the function prefixes. Immediately, I recognize that `mte_to_node(mn)` likely converts a memory tracking entity (likely `mte` or memory tracking entity) to a node object, which `mn` is referencing. Then it assigns the parent pointer of a node via the `ma_parent_ptr`. The fact that the same `mte_to_node(mn)` conversion is present in both arguments, and we are assigning the parent of the first result to the result of calling `ma_parent_ptr` on that same result is interesting. It indicates we're setting the parent relationship of the node. The node represented by mn's memory tracking information is being set up in a tree or graph-like structure.
Now, I'm thinking about edge cases. I'm immediately wondering about what `ma_dead_node` might represent in this context. Is it a marker for a deleted or invalid node? Is it used for null parent assignments when a node is root of a graph or tree? It could also be used for cleaning up memory management, or for debugging purposes. Knowing how this function or variable is defined will be key to understanding the full implication of this assignment and how our memory is managed. Understanding how `ma_dead_node` behaves is going to be crucial in understanding how this code handles scenarios where nodes are removed or become invalid. I need to dig into its definition to get the full picture.
|
| 9/2 |
2026/06/01 06:16 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/maple_tree.c Name:ma_dead_node]
Results: map[SourceCode: 560: /*
561: * ma_dead_node() - check if the @enode is dead.
562: * @enode: The encoded maple node
563: *
564: * Return: true if dead, false otherwise.
565: */
566: static __always_inline bool ma_dead_node(const struct maple_node *node)
567: {
568: struct maple_node *parent;
569:
570: /* Do not reorder reads from the node prior to the parent check */
571: smp_rmb();
572: parent = (void *)((unsigned long) node->parent & ~MAPLE_NODE_MASK);
573: return (parent == node);
574: }
SourceFile:lib/maple_tree.c]
|
| 10/2 |
2026/06/01 06:16 |
llm |
expert |
0m
Model:
Tokens:
input: 7927
output: 37
thoughts: 69
Thoughts: **Analyzing Parent Pointer Assignment**
Okay, let's break this down. I see we have a read operation: `node->parent`. Pretty straightforward β we're accessing the `parent` pointer of a node. Now, the write operation is where it gets interesting: `mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));`. I see that we're assigning to the same `parent` pointer. But wait a minute... looking closer at the right hand side, I see `ma_parent_ptr(mte_to_node(mn))`. The `mte_to_node(mn)` part makes sense β likely a conversion from a more generic memory type to a node type. But `ma_parent_ptr`... I need to be sure this function behaves as I expect. My gut says `ma_parent_ptr` *returns a pointer to the node itself* from which it derives the parent. I need to be sure of this. Let me double check the implementation of `ma_parent_ptr` to be sure. This is critical.
|
| 11/2 |
2026/06/01 06:17 |
tool |
codesearch-definition-source |
0m
Error:
Args: map[ContextFile:lib/maple_tree.c Name:ma_parent_ptr]
Results: map[SourceCode: SourceFile:]
|
| 12/2 |
2026/06/01 06:17 |
llm |
expert |
0m
Model:
Tokens:
input: 8292
output: 19
thoughts: 12
Thoughts: Here's my take on those thoughts:
**Analyzing the `ma_parent_ptr` Search**
Okay, so I'm thinking, "Right, time to get to the root of this." The first logical step is to dive right in and use `grep`. I want to quickly identify every instance of `ma_parent_ptr` within the codebase. I need to understand where it's being used, how it's being modified, and what its overall role is. A simple `grep` search will provide that initial map of the function calls. This will allow me to zero in on what parts of the system are interacting with this pointer and I can follow the control flow from there. It's a fundamental, but effective, approach for this type of investigation.
|
| 13/2 |
2026/06/01 06:17 |
tool |
grepper |
0m
Args: map[Expression:ma_parent_ptr]
Results: map[Output:lib/maple_tree.c-91-
lib/maple_tree.c:92:#define ma_parent_ptr(x) ((struct maple_pnode *)(x))
lib/maple_tree.c-93-#define mas_tree_parent(x) ((unsigned long)(x->tree) | MA_ROOT_PARENT)
--
lib/maple_tree.c=205=static void ma_free_rcu(struct maple_node *node)
lib/maple_tree.c-206-{
lib/maple_tree.c:207: WARN_ON(node->parent != ma_parent_ptr(node));
lib/maple_tree.c-208- kfree_rcu(node, rcu);
--
lib/maple_tree.c=332=static inline void mte_set_node_dead(struct maple_enode *mn)
lib/maple_tree.c-333-{
lib/maple_tree.c:334: mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));
lib/maple_tree.c-335- smp_wmb(); /* Needed for RCU */
--
lib/maple_tree.c=497=void mas_set_parent(struct ma_state *mas, struct maple_enode *enode,
--
lib/maple_tree.c-522- val |= (slot << shift) | type;
lib/maple_tree.c:523: mte_to_node(enode)->parent = ma_parent_ptr(val);
lib/maple_tree.c-524-}
--
lib/maple_tree.c=1546=static inline void mas_put_in_tree(struct ma_state *mas,
--
lib/maple_tree.c-1553- if (mte_is_root(mas->node)) {
lib/maple_tree.c:1554: mas_mn(mas)->parent = ma_parent_ptr(mas_tree_parent(mas));
lib/maple_tree.c-1555- rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
--
lib/maple_tree.c=2614=static void mas_spanning_rebalance(struct ma_state *mas,
--
lib/maple_tree.c-2724-new_root:
lib/maple_tree.c:2725: mas_mn(mast->l)->parent = ma_parent_ptr(mas_tree_parent(mas));
lib/maple_tree.c-2726- while (!mte_is_root(mast->orig_l->node))
--
lib/maple_tree.c=3080=static inline void mas_root_expand(struct ma_state *mas, void *entry)
--
lib/maple_tree.c-3091- slots = ma_slots(node, type);
lib/maple_tree.c:3092: node->parent = ma_parent_ptr(mas_tree_parent(mas));
lib/maple_tree.c-3093- mas->node = mt_mk_node(node, type);
--
lib/maple_tree.c=3354=static inline void mas_new_root(struct ma_state *mas, void *entry)
--
lib/maple_tree.c-3373- slots = ma_slots(node, type);
lib/maple_tree.c:3374: node->parent = ma_parent_ptr(mas_tree_parent(mas));
lib/maple_tree.c-3375- mas->node = mt_mk_node(node, type);
--
lib/maple_tree.c=6217=static inline void mas_copy_node(struct ma_state *mas, struct ma_state *new_mas,
--
lib/maple_tree.c-6227- val = (unsigned long)node->parent & MAPLE_NODE_MASK;
lib/maple_tree.c:6228: new_node->parent = ma_parent_ptr(val | (unsigned long)parent);
lib/maple_tree.c-6229-}
--
lib/maple_tree.c=6281=static inline void mas_dup_build(struct ma_state *mas, struct ma_state *new_mas,
--
lib/maple_tree.c-6338- mas_descend(mas);
lib/maple_tree.c:6339: parent = ma_parent_ptr(mte_to_node(new_mas->node));
lib/maple_tree.c-6340- mas_descend(new_mas);
--
lib/maple_tree.c-6345- /* Specially handle the parent of the root node. */
lib/maple_tree.c:6346: mte_to_node(root)->parent = ma_parent_ptr(mas_tree_parent(new_mas));
lib/maple_tree.c-6347-set_new_tree:
--
tools/testing/radix-tree/maple.c=35099=static noinline void __init check_prealloc(struct maple_tree *mt)
--
tools/testing/radix-tree/maple.c-35148- MT_BUG_ON(mt, mas_allocated(&mas) != allocated - 1);
tools/testing/radix-tree/maple.c:35149: mn->parent = ma_parent_ptr(mn);
tools/testing/radix-tree/maple.c-35150- ma_free_rcu(mn);
--
tools/testing/radix-tree/maple.c-35166- MT_BUG_ON(mt, allocated != 0);
tools/testing/radix-tree/maple.c:35167: mn->parent = ma_parent_ptr(mn);
tools/testing/radix-tree/maple.c-35168- ma_free_rcu(mn);
--
tools/testing/radix-tree/maple.c=36033=void farmer_tests(void)
--
tools/testing/radix-tree/maple.c-36056-
tools/testing/radix-tree/maple.c:36057: node->parent = ma_parent_ptr(node);
tools/testing/radix-tree/maple.c-36058- ma_free_rcu(node);
]
|
| 14/2 |
2026/06/01 06:17 |
llm |
expert |
0m
Model:
Tokens:
input: 10167
output: 37
thoughts: 397
Thoughts: **Analyzing a Potential Data Race in Maple Tree Code**
Okay, so I'm looking at this Maple tree code, and `ma_parent_ptr(x)` is essentially just a cast to `struct maple_pnode *`. Nothing inherently problematic there. Then, in `mte_set_node_dead(mn)`, I see that the code does `mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));`. So, it's setting the `parent` pointer of a node to point to itself. That's a little unusual, but not necessarily wrong depending on the context.
The real point of concern arises when I look at `ma_dead_node(node)`. It reads `node->parent`, masks out `MAPLE_NODE_MASK`, and checks if the result equals `node`. And here's the kicker: this read in `ma_dead_node` isn't using `READ_ONCE`, and the write in `mte_set_node_dead` isn't using `WRITE_ONCE`. Bingo! Classic data race on `node->parent`.
The immediate question is whether this data race is benign. Let's think this through carefully. If `ma_dead_node` reads a partially written value, could it cause an issue? `node->parent` is a pointer (eight bytes on a 64-bit architecture, which I'm assuming). On x86_64, aligned 8-byte reads and writes are atomic. This means `ma_dead_node` will *either* read the old parent pointer *or* the new parent pointer (which is `node` itself). If it reads the old parent, it'll return `false` (not dead). If it reads the new parent (that is, the node itself), it will return `true` (dead).
But wait... that's too simple. What about compiler optimizations? That's where `READ_ONCE` and `WRITE_ONCE` become crucial. The kernel generally uses those to prevent the compiler from doing things like load tearing, store tearing, or caching the value in a register. If we don't use those barriers, KCSAN is going to flag this as a data race, and for good reason. The compiler might reorder reads/writes in a way that breaks the expected logic.
So, I'm going to have to go back and really scrutinize `ma_dead_node`. This needs a closer look, especially in relation to the larger context of the Maple tree operations.
|
| 15/2 |
2026/06/01 06:17 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/maple_tree.c Name:ma_dead_node]
Results: map[SourceCode: 560: /*
561: * ma_dead_node() - check if the @enode is dead.
562: * @enode: The encoded maple node
563: *
564: * Return: true if dead, false otherwise.
565: */
566: static __always_inline bool ma_dead_node(const struct maple_node *node)
567: {
568: struct maple_node *parent;
569:
570: /* Do not reorder reads from the node prior to the parent check */
571: smp_rmb();
572: parent = (void *)((unsigned long) node->parent & ~MAPLE_NODE_MASK);
573: return (parent == node);
574: }
SourceFile:lib/maple_tree.c]
|
| 16/2 |
2026/06/01 06:17 |
llm |
expert |
0m
Model:
Tokens:
input: 11411
output: 38
thoughts: 100
Thoughts: **Analyzing the Order of Operations**
Okay, so I'm looking at this code and something's immediately jumping out at me. The call to `smp_rmb()` is *before* the read of `node->parent`. The comment in the code specifically says, "Do not reorder reads from the node prior to the parent check." That's a strong hint! This suggests to me that the function `ma_dead_node` is called *after* some kind of read operation on the node itself. The goal is likely to check if the state of the node changed between when we read some data, and when we call `ma_dead_node`. This is probably some kind of safety net to protect against use-after-free, or a similar data consistency issue. I'll need to confirm this assumption. So, I need to trace where `ma_dead_node` is called within `mtree_range_walk` to understand what that read operation is. Let's dig in.
|
| 17/2 |
2026/06/01 06:17 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/maple_tree.c Name:mtree_range_walk]
Results: map[SourceCode:2538: static inline void *mtree_range_walk(struct ma_state *mas)
2539: {
2540: unsigned long *pivots;
2541: unsigned char offset;
2542: struct maple_node *node;
2543: struct maple_enode *next, *last;
2544: enum maple_type type;
2545: void __rcu **slots;
2546: unsigned char end;
2547: unsigned long max, min;
2548: unsigned long prev_max, prev_min;
2549:
2550: next = mas->node;
2551: min = mas->min;
2552: max = mas->max;
2553: do {
2554: last = next;
2555: node = mte_to_node(next);
2556: type = mte_node_type(next);
2557: pivots = ma_pivots(node, type);
2558: end = ma_data_end(node, type, pivots, max);
2559: prev_min = min;
2560: prev_max = max;
2561: if (pivots[0] >= mas->index) {
2562: offset = 0;
2563: max = pivots[0];
2564: goto next;
2565: }
2566:
2567: offset = 1;
2568: while (offset < end) {
2569: if (pivots[offset] >= mas->index) {
2570: max = pivots[offset];
2571: break;
2572: }
2573: offset++;
2574: }
2575:
2576: min = pivots[offset - 1] + 1;
2577: next:
2578: slots = ma_slots(node, type);
2579: next = mt_slot(mas->tree, slots, offset);
2580: if (unlikely(ma_dead_node(node)))
2581: goto dead_node;
2582: } while (!ma_is_leaf(type));
2583:
2584: mas->end = end;
2585: mas->offset = offset;
2586: mas->index = min;
2587: mas->last = max;
2588: mas->min = prev_min;
2589: mas->max = prev_max;
2590: mas->node = last;
2591: return (void *)next;
2592:
2593: dead_node:
2594: mas_reset(mas);
2595: return NULL;
2596: }
SourceFile:lib/maple_tree.c]
|
| 18/2 |
2026/06/01 06:17 |
llm |
expert |
0m
Model:
Tokens:
input: 12576
output: 40
thoughts: 307
Thoughts: **Analyzing the Memory Ordering in `mtree_range_walk`**
Okay, so I'm looking at this `mtree_range_walk` function and how it deals with node death. It appears to read from a `node` β pulling data like `pivots`, `slots`, or the `next` pointer via `mt_slot()`. Then, it immediately calls `ma_dead_node(node)`. Now, if the `node` is marked as dead, it goes to `dead_node`, resets everything and returns `NULL`.
The interesting part is this `smp_rmb()` within `ma_dead_node`. Initially, I thought it was about preventing reads from the `node` (like `pivots`) from being reordered *after* the read of `node->parent`. But the comment specifically states, "Do not reorder reads from the node prior to the parent check." So, the reads from the node β the `pivots`, `slots`, etc. β *must* occur before we check `node->parent`.
That `smp_rmb()` before `node->parent` must be there to ensure all those reads from `node` complete *before* the `node->parent` pointer is read. If `node->parent` is read and found to be valid (not dead), that tells us that the preceding reads from the node were valid, or at least the node wasn't dead at the time we accessed it. It looks like the writer makes a node dead when it is removed from the tree.
I should probably go and check `mte_set_node_dead` to confirm this.
|
| 19/2 |
2026/06/01 06:17 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/maple_tree.c Name:mte_set_node_dead]
Results: map[SourceCode: 328: /*
329: * mte_set_node_dead() - Set a maple encoded node as dead.
330: * @mn: The maple encoded node.
331: */
332: static inline void mte_set_node_dead(struct maple_enode *mn)
333: {
334: mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));
335: smp_wmb(); /* Needed for RCU */
336: }
SourceFile:lib/maple_tree.c]
|
| 20/2 |
2026/06/01 06:17 |
llm |
expert |
0m
Model:
Tokens:
input: 13461
output: 53
thoughts: 129
Thoughts: **Analyzing the Memory Management Code**
Okay, so I'm looking at this bit of code: `mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));` followed immediately by `smp_wmb()`. My initial thought is, why the `smp_wmb()` *after* setting the parent? Hang on... if the code marks the node as "dead" before the `smp_wmb()`, what exactly is the memory barrier ordering? It seems like `smp_wmb()` is used to ensure the "dead" state of the node is visible before any subsequent operation.
Essentially, the `smp_wmb()` forces the "set node as dead" write to become visible *before* whatever code follows. This feels a bit unusual because the standard pattern is usually to mark a node as dead, and *then* free it using RCU (Read-Copy-Update) for safe concurrent access.
I need to trace the call to `mte_set_node_dead` now. I'm going to look where it is called in order to fully understand what is going on here.
|
| 21/2 |
2026/06/01 06:17 |
tool |
codesearch-find-references |
0m
Args: map[ContextFile:lib/maple_tree.c IncludeSnippetLines:0 Name:mte_set_node_dead SourceTreePrefix:]
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:mas_put_in_tree SourceFile:lib/maple_tree.c SourceLine:1565] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:mas_topiary_node SourceFile:lib/maple_tree.c SourceLine:2310] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:mat_add SourceFile:lib/maple_tree.c SourceLine:910] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:mt_destroy_walk SourceFile:lib/maple_tree.c SourceLine:4985] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:mte_dead_leaves SourceFile:lib/maple_tree.c SourceLine:4868] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:mte_destroy_descend SourceFile:lib/maple_tree.c SourceLine:4965]] TruncatedOutput:false]
|
| 22/2 |
2026/06/01 06:17 |
llm |
expert |
0m
Model:
Tokens:
input: 14224
output: 39
thoughts: 14
Thoughts: Okay, let's dive into this. Here's my take on those thoughts, as if they're my own:
**Analyzing `mas_put_in_tree`**
Alright, let's focus on `mas_put_in_tree`. Now, knowing this field as I do, my initial thought is always to consider its purpose within the broader context of the system. What data structure is it interacting with? What is the expected behavior, the edge cases, the potential for contention or race conditions? Immediately Iβm formulating questions about its inputs: are they validated? What preconditions must be met before this function can execute correctly?
I'm mentally stepping through a typical scenario involving this function, trying to envision any potential failure points. Does it handle null pointers? What about memory allocation β is there the possibility of an out-of-memory situation that needs to be gracefully handled? Are there any hidden assumptions about the treeβs state that might lead to unexpected behavior? I'm already starting to think about potential debugging strategies and what information I'd need to collect if I were to investigate a problem related to `mas_put_in_tree`.
Furthermore, I have to consider the complexity. Is it a simple insertion, or is there a more involved balancing algorithm or search involved? Any performance concerns? I'm already mentally calculating the Big O notation of the operation based on the overall structure. It also depends on the overall size of the tree. Are we looking at O(1), O(log n), or perhaps O(n) or worse? I need to look at the function signature and, if available, any surrounding documentation or comments to begin to answer those questions. I'm also considering the potential for this function to impact other parts of the system. What are the dependencies? I need to analyze how this might interact with other functions or data structures.
|
| 23/2 |
2026/06/01 06:17 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/maple_tree.c Name:mas_put_in_tree]
Results: map[SourceCode:1539: /*
1540: * mas_put_in_tree() - Put a new node in the tree, smp_wmb(), and mark the old
1541: * node as dead.
1542: * @mas: the maple state with the new node
1543: * @old_enode: The old maple encoded node to replace.
1544: * @new_height: if we are inserting a root node, update the height of the tree
1545: */
1546: static inline void mas_put_in_tree(struct ma_state *mas,
1547: struct maple_enode *old_enode, char new_height)
1548: __must_hold(mas->tree->ma_lock)
1549: {
1550: unsigned char offset;
1551: void __rcu **slots;
1552:
1553: if (mte_is_root(mas->node)) {
1554: mas_mn(mas)->parent = ma_parent_ptr(mas_tree_parent(mas));
1555: rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
1556: mt_set_height(mas->tree, new_height);
1557: } else {
1558:
1559: offset = mte_parent_slot(mas->node);
1560: slots = ma_slots(mte_parent(mas->node),
1561: mas_parent_type(mas, mas->node));
1562: rcu_assign_pointer(slots[offset], mas->node);
1563: }
1564:
1565: mte_set_node_dead(old_enode);
1566: }
SourceFile:lib/maple_tree.c]
|
| 24/2 |
2026/06/01 06:17 |
llm |
expert |
0m
Model:
Tokens:
input: 15184
output: 41
thoughts: 1156
Thoughts: **Analyzing the Potential Data Race in `mas_put_in_tree` and `mtree_range_walk`**
Okay, so I'm looking at how `mas_put_in_tree` swaps an old node (`old_enode`) with a new node (`mas->node`) in the tree, specifically with the `rcu_assign_pointer` (a store-release) and then marking the old node dead using `mte_set_node_dead`. The `mtree_range_walk` is the reader and has the potential for a race condition because it's traversing the tree and accessing the same memory.
My main concern is the data race on `node->parent`. The writer does a plain write: `mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn))`. The reader does a plain read: `parent = (void *)((unsigned long) node->parent & ~MAPLE_NODE_MASK)`. KCSAN is flagging this.
Now, let's break this down. On x86_64, a 64-bit aligned read/write is atomic, so any tearing would be for the parent pointer of 8 bytes. What's the worst that could happen? If the read is torn, the `parent` might be garbage, but typically, on a torn read, the `parent == node` would most likely be false, so it won't mistakenly think the node is still alive. The reader would think the node is NOT dead, and if the reader thinks the node is NOT dead and if it *is* dead, then it'll keep using the old node!
However, the reader is in an RCU read-side critical section, so the node's memory is still valid. It's safe to traverse the old node because it is a snapshot from a previous valid tree state. So, reading stale data is allowed.
So why check for a dead node at all? Well, if we do traverse a dead node, we might end up at a leaf node that is no longer part of the tree. The danger is that we might return a stale value, or we might miss an update.
But, for maple tree specifically, seeing a dead node means it was replaced, and the reader *should* retry to get the latest data. Now, if the reader *misses* the dead mark due to a torn read, it can return stale data. But hold on, if the read is torn, the write is also happening concurrently. That means the reader could have just read the old parent pointer before the write started. So, a torn read is equivalent to reading the old data. RCU readers are allowed to see stale data as they race with writers.
The only issue is if the torn read gives me a value that makes `parent == node`. That causes a spurious retry, which is fine, just a performance hit.
Now, what about compiler optimizations? If the compiler breaks it with load tearing (reading lower 32 bits, then upper 32 bits) or store tearing, it doesn't really matter. It will produce garbage (garbage != `node`), and it thinks it is not dead, equivalent to reading the old parent.
Is there any architecture where a torn read could cause a fault? No, just a pointer value. Can the compiler cause an infinite loop? No, the `ma_dead_node` function is called only once per loop iteration. I am safe.
Okay, now let me check if there are patches fixing this and see what they've done.
|
| 25/2 |
2026/06/01 06:18 |
tool |
git-log |
0m
Args: map[CodeRegexp:ma_dead_node Count:10 PathPrefix:lib/maple_tree.c]
Results: map[Output:6fbea85271c6 maple_tree: use ma_dead_node() in mte_dead_node()
a3c63c8c5df6 maple_tree: mtree_range_walk() clean up
24662decdd44 maple_tree: don't find node end in mtree_lookup_walk()
271f61a8b41d maple_tree: clean up inlines for some functions
6783bd4b5f72 maple_tree: drop mas_first_entry()
dd9a85138280 maple_tree: introduce mas_prev_slot() interface
de6e386c060c maple_tree: relocate mas_rewalk() and mas_rewalk_if_dead()
fff4a58cc00b maple_tree: introduce mas_next_slot() interface
39d0bd86c499 maple_tree: be more cautious about dead nodes
54a611b60590 Maple Tree: add new data structure
]
|
| 26/2 |
2026/06/01 06:18 |
llm |
expert |
0m
Model:
Tokens:
input: 17408
output: 44
thoughts: 37
Thoughts: Okay, let's dive into this code.
**Investigating Potential Data Races in Memory Allocation**
Alright, I need to get to the bottom of some potential issues. My initial thought is to zero in on specific commit messages. I'm going to search for any mentions of KCSAN, which is a kernel concurrency sanitizer, or anything related to "data race" within the context of the `ma_dead_node` or `mte_set_node_dead` functions. These function names suggest memory allocation or management within the kernel's memory tracking, so I want to understand if there are any known issues or past fixes related to concurrency problems within these specific areas. I need to understand if these functions have been a point of past concern.
|
| 27/2 |
2026/06/01 06:18 |
tool |
git-log |
0m
Args: map[CodeRegexp:mte_set_node_dead Count:10 PathPrefix:lib/maple_tree.c]
Results: map[Output:ea9b77f98d94 maple_tree: fix mt_destroy_walk() on root leaf node
530f745c7620 maple_tree: replace data before marking dead in split and spanning store
1238f6a226dc maple_tree: introduce mas_put_in_tree()
72bcf4aa86ec maple_tree: reorder replacement of nodes to avoid live lock
790e1fa86b34 maple_tree: add RCU lock checking to rcu callback functions
c13af03de46b maple_tree: fix write memory barrier of nodes once dead for RCU mode
54a611b60590 Maple Tree: add new data structure
]
|
| 28/2 |
2026/06/01 06:18 |
llm |
expert |
0m
Model:
Tokens:
input: 17834
output: 25
thoughts: 31
Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience:
**Analyzing Potential Commits**
Okay, time to dive into these specific commits: `c13af03de46b` and `530f745c7620`. I need to understand what's changed, the scope of those changes, and how they might affect the existing codebase, particularly any recent modifications. My immediate focus will be on the critical path: What files were touched? Were any critical modules altered, like core infrastructure or security components? Are these commits isolated fixes or part of a larger feature set? Is there anything within the commit messages or code diffs that jump out as potentially problematic or significant for a review? Depending on the results, a full code review may be necessary, or perhaps a more focused analysis is all that is required. These hashes are my starting point; they'll guide my subsequent investigation.
|
| 29/2 |
2026/06/01 06:18 |
tool |
git-show |
0m
Args:
Results: map[Output:commit c13af03de46ba27674dd9fb31a17c0d480081139
Author: Liam R. Howlett <Liam.Howlett@oracle.com>
Date: Mon Feb 27 09:36:04 2023 -0800
maple_tree: fix write memory barrier of nodes once dead for RCU mode
During the development of the maple tree, the strategy of freeing multiple
nodes changed and, in the process, the pivots were reused to store
pointers to dead nodes. To ensure the readers see accurate pivots, the
writers need to mark the nodes as dead and call smp_wmb() to ensure any
readers can identify the node as dead before using the pivot values.
There were two places where the old method of marking the node as dead
without smp_wmb() were being used, which resulted in RCU readers seeing
the wrong pivot value before seeing the node was dead. Fix this race
condition by using mte_set_node_dead() which has the smp_wmb() call to
ensure the race is closed.
Add a WARN_ON() to the ma_free_rcu() call to ensure all nodes being freed
are marked as dead to ensure there are no other call paths besides the two
updated paths.
This is necessary for the RCU mode of the maple tree.
Link: https://lkml.kernel.org/r/20230227173632.3292573-6-surenb@google.com
Fixes: 54a611b60590 ("Maple Tree: add new data structure")
Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
diff --git a/lib/maple_tree.c b/lib/maple_tree.c
index 96d673e4ba5b..5202d89ba56e 100644
--- a/lib/maple_tree.c
+++ b/lib/maple_tree.c
@@ -185,7 +185,7 @@ static void mt_free_rcu(struct rcu_head *head)
*/
static void ma_free_rcu(struct maple_node *node)
{
- node->parent = ma_parent_ptr(node);
+ WARN_ON(node->parent != ma_parent_ptr(node));
call_rcu(&node->rcu, mt_free_rcu);
}
@@ -1778,8 +1778,10 @@ static inline void mas_replace(struct ma_state *mas, bool advanced)
rcu_assign_pointer(slots[offset], mas->node);
}
- if (!advanced)
+ if (!advanced) {
+ mte_set_node_dead(old_enode);
mas_free(mas, old_enode);
+ }
}
/*
@@ -4218,6 +4220,7 @@ static inline bool mas_wr_node_store(struct ma_wr_state *wr_mas)
done:
mas_leaf_set_meta(mas, newnode, dst_pivots, maple_leaf_64, new_end);
if (in_rcu) {
+ mte_set_node_dead(mas->node);
mas->node = mt_mk_node(newnode, wr_mas->type);
mas_replace(mas, false);
} else {
diff --git a/tools/testing/radix-tree/maple.c b/tools/testing/radix-tree/maple.c
index 958ee9bdb316..4c89ff333f6f 100644
--- a/tools/testing/radix-tree/maple.c
+++ b/tools/testing/radix-tree/maple.c
@@ -108,6 +108,7 @@ static noinline void check_new_node(struct maple_tree *mt)
MT_BUG_ON(mt, mn->slot[1] != NULL);
MT_BUG_ON(mt, mas_allocated(&mas) != 0);
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
mas.node = MAS_START;
mas_nomem(&mas, GFP_KERNEL);
@@ -160,6 +161,7 @@ static noinline void check_new_node(struct maple_tree *mt)
MT_BUG_ON(mt, mas_allocated(&mas) != i);
MT_BUG_ON(mt, !mn);
MT_BUG_ON(mt, not_empty(mn));
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
}
@@ -192,6 +194,7 @@ static noinline void check_new_node(struct maple_tree *mt)
MT_BUG_ON(mt, not_empty(mn));
MT_BUG_ON(mt, mas_allocated(&mas) != i - 1);
MT_BUG_ON(mt, !mn);
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
}
@@ -210,6 +213,7 @@ static noinline void check_new_node(struct maple_tree *mt)
mn = mas_pop_node(&mas);
MT_BUG_ON(mt, not_empty(mn));
MT_BUG_ON(mt, mas_allocated(&mas) != j - 1);
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
}
MT_BUG_ON(mt, mas_allocated(&mas) != 0);
@@ -233,6 +237,7 @@ static noinline void check_new_node(struct maple_tree *mt)
MT_BUG_ON(mt, mas_allocated(&mas) != i - j);
mn = mas_pop_node(&mas);
MT_BUG_ON(mt, not_empty(mn));
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
MT_BUG_ON(mt, mas_allocated(&mas) != i - j - 1);
}
@@ -269,6 +274,7 @@ static noinline void check_new_node(struct maple_tree *mt)
mn = mas_pop_node(&mas); /* get the next node. */
MT_BUG_ON(mt, mn == NULL);
MT_BUG_ON(mt, not_empty(mn));
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
}
MT_BUG_ON(mt, mas_allocated(&mas) != 0);
@@ -294,6 +300,7 @@ static noinline void check_new_node(struct maple_tree *mt)
mn = mas_pop_node(&mas2); /* get the next node. */
MT_BUG_ON(mt, mn == NULL);
MT_BUG_ON(mt, not_empty(mn));
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
}
MT_BUG_ON(mt, mas_allocated(&mas2) != 0);
@@ -334,10 +341,12 @@ static noinline void check_new_node(struct maple_tree *mt)
MT_BUG_ON(mt, mas_allocated(&mas) != MAPLE_ALLOC_SLOTS + 2);
mn = mas_pop_node(&mas);
MT_BUG_ON(mt, not_empty(mn));
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
for (i = 1; i <= MAPLE_ALLOC_SLOTS + 1; i++) {
mn = mas_pop_node(&mas);
MT_BUG_ON(mt, not_empty(mn));
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
}
MT_BUG_ON(mt, mas_allocated(&mas) != 0);
@@ -375,6 +384,7 @@ static noinline void check_new_node(struct maple_tree *mt)
mas_node_count(&mas, i); /* Request */
mas_nomem(&mas, GFP_KERNEL); /* Fill request */
mn = mas_pop_node(&mas); /* get the next node. */
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
mas_destroy(&mas);
@@ -382,10 +392,13 @@ static noinline void check_new_node(struct maple_tree *mt)
mas_node_count(&mas, i); /* Request */
mas_nomem(&mas, GFP_KERNEL); /* Fill request */
mn = mas_pop_node(&mas); /* get the next node. */
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
mn = mas_pop_node(&mas); /* get the next node. */
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
mn = mas_pop_node(&mas); /* get the next node. */
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
mas_destroy(&mas);
}
@@ -35369,6 +35382,7 @@ static noinline void check_prealloc(struct maple_tree *mt)
MT_BUG_ON(mt, allocated != 1 + height * 3);
mn = mas_pop_node(&mas);
MT_BUG_ON(mt, mas_allocated(&mas) != allocated - 1);
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
MT_BUG_ON(mt, mas_preallocate(&mas, GFP_KERNEL) != 0);
mas_destroy(&mas);
@@ -35386,6 +35400,7 @@ static noinline void check_prealloc(struct maple_tree *mt)
mas_destroy(&mas);
allocated = mas_allocated(&mas);
MT_BUG_ON(mt, allocated != 0);
+ mn->parent = ma_parent_ptr(mn);
ma_free_rcu(mn);
MT_BUG_ON(mt, mas_preallocate(&mas, GFP_KERNEL) != 0);
@@ -35756,6 +35771,7 @@ void farmer_tests(void)
tree.ma_root = mt_mk_node(node, maple_leaf_64);
mt_dump(&tree);
+ node->parent = ma_parent_ptr(node);
ma_free_rcu(node);
/* Check things that will make lockdep angry */
]
|
| 30/2 |
2026/06/01 06:18 |
llm |
expert |
1m
Model:
Tokens:
input: 21012
output: 38
thoughts: 690
Thoughts: **Analyzing the Memory Ordering in Node Deletion**
Okay, so `c13af03de46b` is pointing out a race condition related to how we mark nodes as "dead" without proper memory barrier synchronization. The core issue is that RCU readers could potentially see the pivot values of a node *before* seeing that the node is marked as dead, leading to incorrect behavior. The fix is to use `mte_set_node_dead()`, which includes an `smp_wmb()` call.
The interesting bit here is to understand the purpose of `smp_wmb()` in `mte_set_node_dead()`. I see the code does:
```c
mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));
smp_wmb(); /* Needed for RCU */
```
So, the `smp_wmb()` is used *after* setting the node's parent pointer. I'm trying to figure out what it actually orders. It seems to order setting the `parent` pointer before whatever comes next.
Let's see what happens in `mas_put_in_tree`:
```c
rcu_assign_pointer(slots[offset], mas->node);
mte_set_node_dead(old_enode);
```
`rcu_assign_pointer` is a store-release, so it makes sure that the new node `mas->node` is fully initialized *before* the new node is made available by updating `slots[offset]`. Then `mte_set_node_dead` is called on the old node. The `mte_set_node_dead` now sets the old node's parent to itself and includes the crucial `smp_wmb()`.
Now, what does the `smp_wmb()` in `mte_set_node_dead` actually order? My initial thought was that it orders the write to `node->parent` (the self-pointer) before the node is potentially passed to `call_rcu` for freeing. However, this isn't the whole picture. `mte_set_node_dead` is called *after* the node has already been replaced in the tree. This is important: an RCU reader could see the *old* node at some point. It's reading the *old* node!
If the reader sees the old node, it might read its pivot values before checking if the node is dead via `ma_dead_node`. The `smp_rmb()` in `ma_dead_node` ensures that reads of the pivots are ordered *before* the read of `node->parent`. This means that if the reader sees `node->parent != node`, it knows that the pivots it *just* read were valid because the node wasn't dead when it read them.
The writer doesn't actually modify the *old* node's pivots. It creates a *new* node and *replaces* the old one. So why would the reader see incorrect pivot values? Ah, that's where the commit message helps. "The strategy of freeing multiple nodes changed and, in the process, the pivots were reused to store pointers to dead nodes."
Bingo! The pivots of a *freed* node are being reused to store pointers to *other* dead nodes, effectively creating a linked list. So, it's not a matter of the writer updating existing pivots, but the *reuse* of those pivot fields to form a list of dead nodes.
Therefore, the `smp_wmb()` in `mte_set_node_dead` is critical for ordering the write to `node->parent` (which signals death) after the previous data is set, ensuring that any reader, when the old node is viewed, will consistently see a valid set of pivots before potentially seeing that the node is part of the dead list. The `parent` field, with the `smp_wmb()`, acts as a fence. A reader either sees the old data (pivots valid) or sees the self-pointer indicating it is on the dead list.
Now I should probably look at `mas_topiary_node` to ensure my understanding is complete.
|
| 31/2 |
2026/06/01 06:19 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/maple_tree.c Name:mas_topiary_node]
Results: map[SourceCode:2292: /*
2293: * mas_topiary_node() - Dispose of a single node
2294: * @mas: The maple state for pushing nodes
2295: * @in_rcu: If the tree is in rcu mode
2296: *
2297: * The node will either be RCU freed or pushed back on the maple state.
2298: */
2299: static inline void mas_topiary_node(struct ma_state *mas,
2300: struct ma_state *tmp_mas, bool in_rcu)
2301: {
2302: struct maple_node *tmp;
2303: struct maple_enode *enode;
2304:
2305: if (mas_is_none(tmp_mas))
2306: return;
2307:
2308: enode = tmp_mas->node;
2309: tmp = mte_to_node(enode);
2310: mte_set_node_dead(enode);
2311: ma_free_rcu(tmp);
2312: }
SourceFile:lib/maple_tree.c]
|
| 32/2 |
2026/06/01 06:19 |
llm |
expert |
0m
Model:
Tokens:
input: 23005
output: 38
thoughts: 17
Thoughts: Here's my thought process, summarized as you requested:
**KCSAN and `mas_topiary_replace`**
Okay, so I'm trying to piece this together... KCSAN flagged something. It's that `mas_topiary_replace` function. Alright, that's the key. That tells me precisely where the concurrency issue I'm chasing is rooted, at least according to KCSAN. That's a huge lead. Now I can focus my analysis on that specific area of the codebase, which narrows down the possibilities considerably. It's time to dig into that function and see what kind of shared resources or data structures it's interacting with, and what kind of contention KCSAN might be pointing towards. This gives me a definite starting point.
|
| 33/2 |
2026/06/01 06:19 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/maple_tree.c Name:mas_topiary_replace]
Results: map[SourceCode:2314: /*
2315: * mas_topiary_replace() - Replace the data with new data, then repair the
2316: * parent links within the new tree. Iterate over the dead sub-tree and collect
2317: * the dead subtrees and topiary the nodes that are no longer of use.
2318: *
2319: * The new tree will have up to three children with the correct parent. Keep
2320: * track of the new entries as they need to be followed to find the next level
2321: * of new entries.
2322: *
2323: * The old tree will have up to three children with the old parent. Keep track
2324: * of the old entries as they may have more nodes below replaced. Nodes within
2325: * [index, last] are dead subtrees, others need to be freed and followed.
2326: *
2327: * @mas: The maple state pointing at the new data
2328: * @old_enode: The maple encoded node being replaced
2329: * @new_height: The new height of the tree as a result of the operation
2330: *
2331: */
2332: static inline void mas_topiary_replace(struct ma_state *mas,
2333: struct maple_enode *old_enode, unsigned char new_height)
2334: {
2335: struct ma_state tmp[3], tmp_next[3];
2336: MA_TOPIARY(subtrees, mas->tree);
2337: bool in_rcu;
2338: int i, n;
2339:
2340: /* Place data in tree & then mark node as old */
2341: mas_put_in_tree(mas, old_enode, new_height);
2342:
2343: /* Update the parent pointers in the tree */
2344: tmp[0] = *mas;
2345: tmp[0].offset = 0;
2346: tmp[1].status = ma_none;
2347: tmp[2].status = ma_none;
2348: while (!mte_is_leaf(tmp[0].node)) {
2349: n = 0;
2350: for (i = 0; i < 3; i++) {
2351: if (mas_is_none(&tmp[i]))
2352: continue;
2353:
2354: while (n < 3) {
2355: if (!mas_find_child(&tmp[i], &tmp_next[n]))
2356: break;
2357: n++;
2358: }
2359:
2360: mas_adopt_children(&tmp[i], tmp[i].node);
2361: }
2362:
2363: if (MAS_WARN_ON(mas, n == 0))
2364: break;
2365:
2366: while (n < 3)
2367: tmp_next[n++].status = ma_none;
2368:
2369: for (i = 0; i < 3; i++)
2370: tmp[i] = tmp_next[i];
2371: }
2372:
2373: /* Collect the old nodes that need to be discarded */
2374: if (mte_is_leaf(old_enode))
2375: return mas_free(mas, old_enode);
2376:
2377: tmp[0] = *mas;
2378: tmp[0].offset = 0;
2379: tmp[0].node = old_enode;
2380: tmp[1].status = ma_none;
2381: tmp[2].status = ma_none;
2382: in_rcu = mt_in_rcu(mas->tree);
2383: do {
2384: n = 0;
2385: for (i = 0; i < 3; i++) {
2386: if (mas_is_none(&tmp[i]))
2387: continue;
2388:
2389: while (n < 3) {
2390: if (!mas_find_child(&tmp[i], &tmp_next[n]))
2391: break;
2392:
2393: if ((tmp_next[n].min >= tmp_next->index) &&
2394: (tmp_next[n].max <= tmp_next->last)) {
2395: mat_add(&subtrees, tmp_next[n].node);
2396: tmp_next[n].status = ma_none;
2397: } else {
2398: n++;
2399: }
2400: }
2401: }
2402:
2403: if (MAS_WARN_ON(mas, n == 0))
2404: break;
2405:
2406: while (n < 3)
2407: tmp_next[n++].status = ma_none;
2408:
2409: for (i = 0; i < 3; i++) {
2410: mas_topiary_node(mas, &tmp[i], in_rcu);
2411: tmp[i] = tmp_next[i];
2412: }
2413: } while (!mte_is_leaf(tmp[0].node));
2414:
2415: for (i = 0; i < 3; i++)
2416: mas_topiary_node(mas, &tmp[i], in_rcu);
2417:
2418: mas_mat_destroy(mas, &subtrees);
2419: }
SourceFile:lib/maple_tree.c]
|
| 34/2 |
2026/06/01 06:19 |
llm |
expert |
0m
Model:
Tokens:
input: 24888
output: 38
thoughts: 148
Thoughts: **Analyzing the KCSAN Report**
Okay, so the KCSAN report points to a potential issue, specifically within the Maple Tree code. It highlights this sequence: `mte_set_node_dead` which is inlined, then `mas_put_in_tree`, also inlined, which then calls `mas_topiary_replace`, again inlined, and finally, `mas_wmb_replace` at offset 0x20d, which is the function that the KCSAN report is flagging. Hmm, that's interesting; I'm seeing a connection between `mas_wmb_replace` and `mas_topiary_replace`. It looks like `mas_wmb_replace` is calling `mas_topiary_replace`. I'm going to dig into the `mas_wmb_replace` function to see exactly what's happening. Time to trace the execution flow and see if I can understand the nature of this error.
|
| 35/2 |
2026/06/01 06:19 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/maple_tree.c Name:mas_wmb_replace]
Results: map[SourceCode:2421: /*
2422: * mas_wmb_replace() - Write memory barrier and replace
2423: * @mas: The maple state
2424: * @old_enode: The old maple encoded node that is being replaced.
2425: * @new_height: The new height of the tree as a result of the operation
2426: *
2427: * Updates gap as necessary.
2428: */
2429: static inline void mas_wmb_replace(struct ma_state *mas,
2430: struct maple_enode *old_enode, unsigned char new_height)
2431: {
2432: /* Insert the new data in the tree */
2433: mas_topiary_replace(mas, old_enode, new_height);
2434:
2435: if (mte_is_leaf(mas->node))
2436: return;
2437:
2438: mas_update_gap(mas);
2439: }
SourceFile:lib/maple_tree.c]
|
| 36/2 |
2026/06/01 06:19 |
llm |
expert |
0m
Model:
Tokens:
input: 25597
output: 27
thoughts: 626
Thoughts: **Benign Data Race in Maple Tree: A Deep Dive**
Okay, so I've been tracing the execution path, and it looks like `mas_wmb_replace` calls `mas_topiary_replace`, which, in turn, calls `mas_put_in_tree`, which finally calls `mte_set_node_dead` and here is where things get interesting. We've got a read and a write to `node->parent`, specifically. `mtree_range_walk` reads this value in `ma_dead_node`, while `mte_set_node_dead` writes to it. Classic data race, KCSAN is screaming.
The writer, `mte_set_node_dead`, just does a plain write: `mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));`. The reader also does a plain read: `parent = (void *)((unsigned long) node->parent & ~MAPLE_NODE_MASK);`. No fancy atomics or memory barriers in sight. KCSAN's not wrong to flag this.
But here's the rub. Is this actually a bug? After reviewing the situation, I've concluded that this is a benign data race. The reader's checking if the node is "dead." If it reads the old parent *before* the write is complete, it's fine β the node's data is still valid because of RCU. If it reads the new parent (which is the node itself after being marked dead), it retries and treats the node as dead which is perfectly acceptable.
Even if the read is torn, it's very likely to get garbage which is an invalid pointer, which, again, is safe. This means it thinks the node is not dead, which is still a fine state, because the RCU is set.
But wait, hold on, I need to be *sure* a torn read can't cause issues. Could a torn read result in `parent` *actually* equaling `node`? Let's analyze. `node` is aligned, so its lower bits are zero. If a torn read happens, and it reads the lower 32 bits of the *old* parent and the upper 32 bits of the *new* parent, things could get weird. Both old and new parents are pointers in kernel space. Generally, they share the upper 32 bits, but if this is not the case, it will still result in garbage.
The only way a torn read can equal `node` is if the lower 32 bits of the *old* parent are exactly the same as `node`. That's extremely unlikely (it would mean the old parent and `node` are roughly 4GB apart in memory). Even if it does happen, it'll just cause a harmless retry. It is safe.
So, it's safe! No memory corruption, no crashes, no incorrect behavior. It's just a benign race that KCSAN happened to catch, because of missing `READ_ONCE`/`WRITE_ONCE` usage.
Now, let's see if this has already been addressed in mainlineβ¦
|
| 37/2 |
2026/06/01 06:20 |
tool |
grepper |
0m
Error:
Args: map[Expression:WRITE_ONCE.*mte_to_node.*parent]
Results:
|
| 38/2 |
2026/06/01 06:20 |
llm |
expert |
0m
Model:
Tokens:
input: 26948
output: 40
thoughts: 18
Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and presented as if it's my own internal dialogue:
**Investigating a Potential System Issue**
Alright, I'm diving into this. The mention of `ma_dead_node` immediately triggers a mental checklist. Is this a known issue? I need to quickly determine if there's a pre-existing patch or mitigation strategy for this particular scenario. My initial thought is to check the usual suspects - the internal knowledge base, the release notes of relevant components, and perhaps any internal bug tracking systems. No point in reinventing the wheel if someone's already solved this. We need to ascertain first whether a readily available solution to resolve this exists. It could be something as simple as a configuration change or as complex as a microcode update, so my first course of action will be to perform a cursory search. I want to spend as little time as possible on the most mundane of debugging scenarios, and jump straight into solving the issue. I know that time is of the essence, and that an efficient debugging style will be required to keep the development cycle moving.
|
| 39/2 |
2026/06/01 06:20 |
tool |
git-log |
0m
Args: map[CodeRegexp:ma_dead_node Count:5 PathPrefix:lib/maple_tree.c]
Results: map[Output:6fbea85271c6 maple_tree: use ma_dead_node() in mte_dead_node()
a3c63c8c5df6 maple_tree: mtree_range_walk() clean up
24662decdd44 maple_tree: don't find node end in mtree_lookup_walk()
271f61a8b41d maple_tree: clean up inlines for some functions
6783bd4b5f72 maple_tree: drop mas_first_entry()
]
|
| 40/2 |
2026/06/01 06:20 |
llm |
expert |
0m
Model:
Tokens:
input: 27386
output: 43
thoughts: 18
Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience:
**Investigating a Potential Regression**
Okay, so we've got an issue and I need to track down the root cause. My first instinct is to check the recent code changes related to the relevant function. Let's start by zeroing in on `mte_set_node_dead`. I want to quickly see if it was modified recently. That's a key area to scrutinize given the nature of the problem, a good starting point. This likely changed how death is handled, which has ramifications downstream.
|
| 41/2 |
2026/06/01 06:20 |
tool |
git-log |
0m
Args: map[CodeRegexp:mte_set_node_dead Count:5 PathPrefix:lib/maple_tree.c]
Results: map[Output:ea9b77f98d94 maple_tree: fix mt_destroy_walk() on root leaf node
530f745c7620 maple_tree: replace data before marking dead in split and spanning store
1238f6a226dc maple_tree: introduce mas_put_in_tree()
72bcf4aa86ec maple_tree: reorder replacement of nodes to avoid live lock
790e1fa86b34 maple_tree: add RCU lock checking to rcu callback functions
]
|
| 42/2 |
2026/06/01 06:20 |
llm |
expert |
0m
Model:
Tokens:
input: 27714
output: 30
thoughts: 18
Thoughts: Okay, here's my summarized thought process, assuming I'm an expert in the field:
**Investigating KCSAN and Maple Tree Interactions**
Alright, before diving too deep into anything, I need to see if there's any pre-existing context to build upon. I'm going to quickly check the commit history. Specifically, I'm looking for any commits that explicitly mention both KCSAN (Kernel Concurrency Sanitizer) and the Maple Tree data structure. My gut tells me there might be some known interactions or specific considerations relating to concurrency issues or data races that have already been addressed. Hopefully, there's a treasure trove of documented fixes, discussions, or at least some relevant background information that can save me some legwork and provide a foundation for understanding the current situation. I'll focus on filtering for commits containing both terms; that's the most direct way to get to what I'm after, and if nothing shows up, I can broaden the search.
|
| 43/2 |
2026/06/01 06:20 |
tool |
git-log |
0m
Args: map[Count:10 MessageRegexps:[KCSAN maple]]
Results: map[Output:1d6123102e9f Bluetooth: hci_core: Fix use-after-free in vhci_flush()
]
|
| 44/2 |
2026/06/01 06:20 |
llm |
expert |
0m
Model:
Tokens:
input: 28011
output: 66
thoughts: 1360
Thoughts: **Benign Data Race in Maple Tree VMA Lookup**
Okay, so I'm looking at this KCSAN data race report, and initially, it seems harmless. It's a pointer read/write race condition, but it's not exploitable. No memory corruption, no control flow hijack β just a benign data race. The kernel uses `smp_rmb()` and `smp_wmb()` for ordering, but apparently missed the `READ_ONCE()` and `WRITE_ONCE()` which is what's making KCSAN complain.
But wait, could this *somehow* lead to a denial of service? Could the compiler, for instance, optimize a read into an infinite loop? No, that read is within `ma_dead_node`, which is called once per loop iteration in `mtree_range_walk`. The loop structure in `mtree_range_walk` ensures the `node` variable changes each time and the read is performed. So, no read hoisting. The compiler could cache the read, but since it's only read once per node it would be benign.
Could the absence of `READ_ONCE` cause the compiler to reload the value, and potentially read it twice? It would read the old and then the new parent. Examining `ma_dead_node`:
```c
parent = (void *)((unsigned long) node->parent & ~MAPLE_NODE_MASK);
return (parent == node);
```
The compiler should simply execute this:
```assembly
mov rax, [node->parent]
and rax, ~MAPLE_NODE_MASK
cmp rax, node
```
Which has one read. Even if it reloads `node->parent`, the comparison with the mask prevents any issues because it compares values, and the result is safe.
So, yeah, it's completely benign. No exploitability, no DOS.
Let's double-check the requirements. Exploitable? False. Denial of Service? False.
Okay, now let's look at the triggers and accessibility. It's triggered by `mprotect` (writer) and `page_fault` (reader). Both are accessible to unprivileged processes, and user namespaces. The write path is `mprotect` -> `vma_modify` -> `split_vma` -> etc., ending with `mte_set_node_dead`. The read path is `page_fault` -> `lock_vma_under_rcu` -> `mas_walk` -> `mtree_range_walk` -> `ma_dead_node`. This is a *standard* concurrent access pattern in the VMA maple tree! The `lock_vma_under_rcu` is used for lockless VMA lookups during page faults, which is meant to run concurrently with VMA modifications. This is the whole point of the maple tree design.
The data race is an *expected* race condition. The missing `READ_ONCE`/`WRITE_ONCE` just means KCSAN doesn't know it's intentional and safe.
Okay, let's verify consequences. If `ma_dead_node` returns `false` (incorrectly) and the reader traverses the old node, this isn't a problem. The old node's memory is still valid until the RCU grace period ends. The reader just sees the *pre-modification* state. If it hits a leaf, it gets the old VMA. The page fault handler tries to lock the VMA (`vma_start_read`). If the VMA *was* modified, its sequence number (`vma->vm_lock.seq`) would have been incremented. The page fault handler *checks* that sequence number and falls back to taking the `mmap_lock` if there was a concurrent modification. This handles the concurrency issue, and nothing breaks.
So, let's review:
* Exploitable: No.
* Denial Of Service: No.
* Unprivileged: Yes, mprotect, and page faults are unprivileged.
* User Namespaces: Yes.
* No VM, Network or other triggers.
Done. I'll set the results.
|
| 45/2 |
2026/06/01 06:21 |
tool |
set-results |
0m
Args: map[DenialOfService:false Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:true UserNamespace:true VMGuestTrigger:false VMHostTrigger:false]
Results: map[DenialOfService:false Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:true UserNamespace:true VMGuestTrigger:false VMHostTrigger:false]
|
| 46/2 |
2026/06/01 06:21 |
llm |
expert |
0m
Model:
Tokens:
input: 30410
output: 678
thoughts: 445
Thoughts: **Final Response Formulation**
Okay, the results are in. Time to solidify my response. This KCSAN report flagged a data race within the maple tree itself. Specifically, it's between `mte_set_node_dead` (the writer) and `ma_dead_node` (the reader). The writer is messing with the VMA tree β perhaps `mprotect` splitting a VMA β and the reader's performing a lockless VMA lookup during a page fault (`lock_vma_under_rcu`).
The core of it is the writer marking a replaced maple tree node as dead. It does this by setting the node's parent pointer to itself (`node->parent = ma_parent_ptr(node)`). The reader then checks for this "dead" state by simply comparing the parent pointer to the node's address. The problem, as KCSAN sees it, is that these accesses aren't using `READ_ONCE` and `WRITE_ONCE`. Plain memory reads and writes are the source of the report.
However, after a deeper look, I'd say this race is benign, and a perfectly acceptable part of the maple tree's lockless RCU-based traversal. First, if the reader picks up the old parent pointer, it won't think the node is dead and continue its traversal. The beauty of RCU is that the node's memory is valid and consistent *before* the modification happens. Secondly, even if the lockless lookup gets a stale VMA, the page fault handler is smart enough. It'll detect the concurrent modification using the VMA's sequence lock (`vma->vm_lock.seq`) and gracefully revert to taking the `mmap_lock`. Thirdly, and this is important, on architectures like x86_64, aligned pointer-sized reads and writes are atomic. So load/store tearing isn't a concern. Even if it did happen, the garbage pointer is unlikely to be equal to the node's address, leading the reader to assume the node is alive anyway.
In short, there's no way this thing can be exploited for memory corruption, privilege escalation, or any kind of denial of service. The worst a user can do is trigger concurrent `mprotect` and page faults, which is reachable by unprivileged users.
|
| 47/1 |
2026/06/01 06:21 |
action |
format-explanation |
0m
Results: map[Explanation:**Analysis of the Bug Report**
The provided bug report is a Kernel Concurrency Sanitizer (KCSAN) data-race
warning in the maple tree data structure, specifically occurring between
`mte_set_node_dead()` (the writer) and `ma_dead_node()` (the reader).
**Root Cause and Mechanism:**
1. **The Writer (`mprotect` path):** When a VMA is modified (e.g., split by
`mprotect`), the maple tree is updated. The old maple tree node is replaced and
marked as "dead" before being scheduled for RCU freeing. Marking a node as dead
is done in `mte_set_node_dead()` by setting its parent pointer to point to
itself:
`mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));`
2. **The Reader (Page fault path):** Concurrently, a speculative page fault may
be performing a lockless VMA lookup via `lock_vma_under_rcu()`. During the tree
traversal, `ma_dead_node()` checks if the current node has been marked dead by
reading its parent pointer:
`parent = (void *)((unsigned long) node->parent & ~MAPLE_NODE_MASK);`
Because both the write and the read are performed using plain memory accesses
(lacking `WRITE_ONCE()` and `READ_ONCE()` annotations), KCSAN correctly
identifies this as a data race.
**Security Impact:**
This is a **benign data race** and does not pose a security risk. It is an
expected race condition inherent to the lockless RCU-based design of the maple
tree:
* **Safe Stale Reads:** If the reader reads the old parent pointer (before the
writer marks it dead), it will simply conclude the node is still valid and
continue traversing it. Because the node's memory is protected by RCU, it
remains allocated and contains a perfectly valid, albeit stale, snapshot of the
tree.
* **VMA Sequence Locks:** If the lockless traversal ultimately returns a stale
VMA due to this race, the speculative page fault handler will detect the
concurrent modification by checking the VMA's sequence lock
(`vma->vm_lock.seq`). It will then safely abort the lockless fault and fall back
to acquiring the `mmap_lock`.
* **No Tearing Risks:** On 64-bit architectures, aligned 8-byte pointer
reads/writes are atomic. Even on architectures where load tearing could
theoretically occur, a torn read would yield a garbage pointer that does not
equal the node's address, causing the reader to safely treat the node as "not
dead" (equivalent to reading the old state).
**Conclusion:**
The bug is **not exploitable** and cannot lead to memory corruption, privilege
escalation, or Denial of Service (DoS). It is easily reachable by **unprivileged
users** (and within user namespaces) by simply racing `mprotect()` calls with
memory accesses (page faults) on the same memory region. The fix would merely
involve adding `READ_ONCE()` and `WRITE_ONCE()` to silence the sanitizer.]
|