Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

bpf: Use kmalloc_nolock() in range tree

The range tree uses bpf_mem_alloc() that is safe to be called from all
contexts and uses a pre-allocated pool of memory to serve these
allocations.

Replace bpf_mem_alloc() with kmalloc_nolock() as it can be called safely
from all contexts and is more scalable than bpf_mem_alloc().

Remove the migrate_disable/enable pairs as they were only needed for
bpf_mem_alloc() as it does per-cpu operations, kmalloc_nolock() doesn't
need this.

Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Link: https://lore.kernel.org/r/20251106170608.4800-1-puranjay@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Puranjay Mohan and committed by
Alexei Starovoitov
f8c67d85 6f1f4c16

+6 -15
+6 -15
kernel/bpf/range_tree.c
··· 2 2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ 3 3 #include <linux/interval_tree_generic.h> 4 4 #include <linux/slab.h> 5 - #include <linux/bpf_mem_alloc.h> 6 5 #include <linux/bpf.h> 7 6 #include "range_tree.h" 8 7 ··· 20 21 * in commit 6772fcc8890a ("xfs: convert xbitmap to interval tree"). 21 22 * 22 23 * The implementation relies on external lock to protect rbtree-s. 23 - * The alloc/free of range_node-s is done via bpf_mem_alloc. 24 + * The alloc/free of range_node-s is done via kmalloc_nolock(). 24 25 * 25 26 * bpf arena is using range_tree to represent unallocated slots. 26 27 * At init time: ··· 149 150 range_it_insert(rn, rt); 150 151 151 152 /* Add a range */ 152 - migrate_disable(); 153 - new_rn = bpf_mem_alloc(&bpf_global_ma, sizeof(struct range_node)); 154 - migrate_enable(); 153 + new_rn = kmalloc_nolock(sizeof(struct range_node), 0, NUMA_NO_NODE); 155 154 if (!new_rn) 156 155 return -ENOMEM; 157 156 new_rn->rn_start = last + 1; ··· 169 172 } else { 170 173 /* in the middle of the clearing range */ 171 174 range_it_remove(rn, rt); 172 - migrate_disable(); 173 - bpf_mem_free(&bpf_global_ma, rn); 174 - migrate_enable(); 175 + kfree_nolock(rn); 175 176 } 176 177 } 177 178 return 0; ··· 222 227 range_it_remove(right, rt); 223 228 left->rn_last = right->rn_last; 224 229 range_it_insert(left, rt); 225 - migrate_disable(); 226 - bpf_mem_free(&bpf_global_ma, right); 227 - migrate_enable(); 230 + kfree_nolock(right); 228 231 } else if (left) { 229 232 /* Combine with the left range */ 230 233 range_it_remove(left, rt); ··· 234 241 right->rn_start = start; 235 242 range_it_insert(right, rt); 236 243 } else { 237 - migrate_disable(); 238 - left = bpf_mem_alloc(&bpf_global_ma, sizeof(struct range_node)); 239 - migrate_enable(); 244 + left = kmalloc_nolock(sizeof(struct range_node), 0, NUMA_NO_NODE); 240 245 if (!left) 241 246 return -ENOMEM; 242 247 left->rn_start = start; ··· 250 259 251 260 while ((rn = range_it_iter_first(rt, 0, -1U))) { 252 261 range_it_remove(rn, rt); 253 - bpf_mem_free(&bpf_global_ma, rn); 262 + kfree_nolock(rn); 254 263 } 255 264 } 256 265