]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
lib/generic-radix-tree.c: Fix rare race in __genradix_ptr_alloc()
authorKent Overstreet <kent.overstreet@linux.dev>
Sun, 11 Aug 2024 01:04:35 +0000 (21:04 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Wed, 14 Aug 2024 02:56:50 +0000 (22:56 -0400)
If we need to increase the tree depth, allocate a new node, and then
race with another thread that increased the tree depth before us, we'll
still have a preallocated node that might be used later.

If we then use that node for a new non-root node, it'll still have a
pointer to the old root instead of being zeroed - fix this by zeroing it
in the cmpxchg failure path.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
lib/generic-radix-tree.c

index aaefb9b678c8e4e1277411a27c554604686d3ddc..fa692c86f06969239bd501bd30a4723d20e2b291 100644 (file)
@@ -121,6 +121,8 @@ void *__genradix_ptr_alloc(struct __genradix *radix, size_t offset,
                if ((v = cmpxchg_release(&radix->root, r, new_root)) == r) {
                        v = new_root;
                        new_node = NULL;
+               } else {
+                       new_node->children[0] = NULL;
                }
        }