1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2023 Meta, Inc */
4 #include <linux/bpf_mem_alloc.h>
6 #include <linux/btf_ids.h>
7 #include <linux/cpumask.h>
10 * struct bpf_cpumask - refcounted BPF cpumask wrapper structure
11 * @cpumask: The actual cpumask embedded in the struct.
12 * @usage: Object reference counter. When the refcount goes to 0, the
13 * memory is released back to the BPF allocator, which provides
16 * Note that we explicitly embed a cpumask_t rather than a cpumask_var_t. This
17 * is done to avoid confusing the verifier due to the typedef of cpumask_var_t
18 * changing depending on whether CONFIG_CPUMASK_OFFSTACK is defined or not. See
19 * the details in <linux/cpumask.h>. The consequence is that this structure is
20 * likely a bit larger than it needs to be when CONFIG_CPUMASK_OFFSTACK is
21 * defined due to embedding the whole NR_CPUS-size bitmap, but the extra memory
22 * overhead is minimal. For the more typical case of CONFIG_CPUMASK_OFFSTACK
23 * not being defined, the structure is the same size regardless.
30 static struct bpf_mem_alloc bpf_cpumask_ma;
32 static bool cpu_valid(u32 cpu)
34 return cpu < nr_cpu_ids;
37 __bpf_kfunc_start_defs();
40 * bpf_cpumask_create() - Create a mutable BPF cpumask.
42 * Allocates a cpumask that can be queried, mutated, acquired, and released by
43 * a BPF program. The cpumask returned by this function must either be embedded
44 * in a map as a kptr, or freed with bpf_cpumask_release().
46 * bpf_cpumask_create() allocates memory using the BPF memory allocator, and
47 * will not block. It may return NULL if no memory is available.
49 __bpf_kfunc struct bpf_cpumask *bpf_cpumask_create(void)
51 struct bpf_cpumask *cpumask;
53 /* cpumask must be the first element so struct bpf_cpumask be cast to struct cpumask. */
54 BUILD_BUG_ON(offsetof(struct bpf_cpumask, cpumask) != 0);
56 cpumask = bpf_mem_cache_alloc(&bpf_cpumask_ma);
60 memset(cpumask, 0, sizeof(*cpumask));
61 refcount_set(&cpumask->usage, 1);
67 * bpf_cpumask_acquire() - Acquire a reference to a BPF cpumask.
68 * @cpumask: The BPF cpumask being acquired. The cpumask must be a trusted
71 * Acquires a reference to a BPF cpumask. The cpumask returned by this function
72 * must either be embedded in a map as a kptr, or freed with
73 * bpf_cpumask_release().
75 __bpf_kfunc struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask)
77 refcount_inc(&cpumask->usage);
82 * bpf_cpumask_release() - Release a previously acquired BPF cpumask.
83 * @cpumask: The cpumask being released.
85 * Releases a previously acquired reference to a BPF cpumask. When the final
86 * reference of the BPF cpumask has been released, it is subsequently freed in
87 * an RCU callback in the BPF memory allocator.
89 __bpf_kfunc void bpf_cpumask_release(struct bpf_cpumask *cpumask)
91 if (!refcount_dec_and_test(&cpumask->usage))
95 bpf_mem_cache_free_rcu(&bpf_cpumask_ma, cpumask);
100 * bpf_cpumask_first() - Get the index of the first nonzero bit in the cpumask.
101 * @cpumask: The cpumask being queried.
103 * Find the index of the first nonzero bit of the cpumask. A struct bpf_cpumask
104 * pointer may be safely passed to this function.
106 __bpf_kfunc u32 bpf_cpumask_first(const struct cpumask *cpumask)
108 return cpumask_first(cpumask);
112 * bpf_cpumask_first_zero() - Get the index of the first unset bit in the
114 * @cpumask: The cpumask being queried.
116 * Find the index of the first unset bit of the cpumask. A struct bpf_cpumask
117 * pointer may be safely passed to this function.
119 __bpf_kfunc u32 bpf_cpumask_first_zero(const struct cpumask *cpumask)
121 return cpumask_first_zero(cpumask);
125 * bpf_cpumask_first_and() - Return the index of the first nonzero bit from the
126 * AND of two cpumasks.
127 * @src1: The first cpumask.
128 * @src2: The second cpumask.
130 * Find the index of the first nonzero bit of the AND of two cpumasks.
131 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
133 __bpf_kfunc u32 bpf_cpumask_first_and(const struct cpumask *src1,
134 const struct cpumask *src2)
136 return cpumask_first_and(src1, src2);
140 * bpf_cpumask_set_cpu() - Set a bit for a CPU in a BPF cpumask.
141 * @cpu: The CPU to be set in the cpumask.
142 * @cpumask: The BPF cpumask in which a bit is being set.
144 __bpf_kfunc void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask)
149 cpumask_set_cpu(cpu, (struct cpumask *)cpumask);
153 * bpf_cpumask_clear_cpu() - Clear a bit for a CPU in a BPF cpumask.
154 * @cpu: The CPU to be cleared from the cpumask.
155 * @cpumask: The BPF cpumask in which a bit is being cleared.
157 __bpf_kfunc void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask)
162 cpumask_clear_cpu(cpu, (struct cpumask *)cpumask);
166 * bpf_cpumask_test_cpu() - Test whether a CPU is set in a cpumask.
167 * @cpu: The CPU being queried for.
168 * @cpumask: The cpumask being queried for containing a CPU.
171 * * true - @cpu is set in the cpumask
172 * * false - @cpu was not set in the cpumask, or @cpu is an invalid cpu.
174 __bpf_kfunc bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask)
179 return cpumask_test_cpu(cpu, (struct cpumask *)cpumask);
183 * bpf_cpumask_test_and_set_cpu() - Atomically test and set a CPU in a BPF cpumask.
184 * @cpu: The CPU being set and queried for.
185 * @cpumask: The BPF cpumask being set and queried for containing a CPU.
188 * * true - @cpu is set in the cpumask
189 * * false - @cpu was not set in the cpumask, or @cpu is invalid.
191 __bpf_kfunc bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask)
196 return cpumask_test_and_set_cpu(cpu, (struct cpumask *)cpumask);
200 * bpf_cpumask_test_and_clear_cpu() - Atomically test and clear a CPU in a BPF
202 * @cpu: The CPU being cleared and queried for.
203 * @cpumask: The BPF cpumask being cleared and queried for containing a CPU.
206 * * true - @cpu is set in the cpumask
207 * * false - @cpu was not set in the cpumask, or @cpu is invalid.
209 __bpf_kfunc bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask)
214 return cpumask_test_and_clear_cpu(cpu, (struct cpumask *)cpumask);
218 * bpf_cpumask_setall() - Set all of the bits in a BPF cpumask.
219 * @cpumask: The BPF cpumask having all of its bits set.
221 __bpf_kfunc void bpf_cpumask_setall(struct bpf_cpumask *cpumask)
223 cpumask_setall((struct cpumask *)cpumask);
227 * bpf_cpumask_clear() - Clear all of the bits in a BPF cpumask.
228 * @cpumask: The BPF cpumask being cleared.
230 __bpf_kfunc void bpf_cpumask_clear(struct bpf_cpumask *cpumask)
232 cpumask_clear((struct cpumask *)cpumask);
236 * bpf_cpumask_and() - AND two cpumasks and store the result.
237 * @dst: The BPF cpumask where the result is being stored.
238 * @src1: The first input.
239 * @src2: The second input.
242 * * true - @dst has at least one bit set following the operation
243 * * false - @dst is empty following the operation
245 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
247 __bpf_kfunc bool bpf_cpumask_and(struct bpf_cpumask *dst,
248 const struct cpumask *src1,
249 const struct cpumask *src2)
251 return cpumask_and((struct cpumask *)dst, src1, src2);
255 * bpf_cpumask_or() - OR two cpumasks and store the result.
256 * @dst: The BPF cpumask where the result is being stored.
257 * @src1: The first input.
258 * @src2: The second input.
260 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
262 __bpf_kfunc void bpf_cpumask_or(struct bpf_cpumask *dst,
263 const struct cpumask *src1,
264 const struct cpumask *src2)
266 cpumask_or((struct cpumask *)dst, src1, src2);
270 * bpf_cpumask_xor() - XOR two cpumasks and store the result.
271 * @dst: The BPF cpumask where the result is being stored.
272 * @src1: The first input.
273 * @src2: The second input.
275 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
277 __bpf_kfunc void bpf_cpumask_xor(struct bpf_cpumask *dst,
278 const struct cpumask *src1,
279 const struct cpumask *src2)
281 cpumask_xor((struct cpumask *)dst, src1, src2);
285 * bpf_cpumask_equal() - Check two cpumasks for equality.
286 * @src1: The first input.
287 * @src2: The second input.
290 * * true - @src1 and @src2 have the same bits set.
291 * * false - @src1 and @src2 differ in at least one bit.
293 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
295 __bpf_kfunc bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2)
297 return cpumask_equal(src1, src2);
301 * bpf_cpumask_intersects() - Check two cpumasks for overlap.
302 * @src1: The first input.
303 * @src2: The second input.
306 * * true - @src1 and @src2 have at least one of the same bits set.
307 * * false - @src1 and @src2 don't have any of the same bits set.
309 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
311 __bpf_kfunc bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2)
313 return cpumask_intersects(src1, src2);
317 * bpf_cpumask_subset() - Check if a cpumask is a subset of another.
318 * @src1: The first cpumask being checked as a subset.
319 * @src2: The second cpumask being checked as a superset.
322 * * true - All of the bits of @src1 are set in @src2.
323 * * false - At least one bit in @src1 is not set in @src2.
325 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
327 __bpf_kfunc bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2)
329 return cpumask_subset(src1, src2);
333 * bpf_cpumask_empty() - Check if a cpumask is empty.
334 * @cpumask: The cpumask being checked.
337 * * true - None of the bits in @cpumask are set.
338 * * false - At least one bit in @cpumask is set.
340 * A struct bpf_cpumask pointer may be safely passed to @cpumask.
342 __bpf_kfunc bool bpf_cpumask_empty(const struct cpumask *cpumask)
344 return cpumask_empty(cpumask);
348 * bpf_cpumask_full() - Check if a cpumask has all bits set.
349 * @cpumask: The cpumask being checked.
352 * * true - All of the bits in @cpumask are set.
353 * * false - At least one bit in @cpumask is cleared.
355 * A struct bpf_cpumask pointer may be safely passed to @cpumask.
357 __bpf_kfunc bool bpf_cpumask_full(const struct cpumask *cpumask)
359 return cpumask_full(cpumask);
363 * bpf_cpumask_copy() - Copy the contents of a cpumask into a BPF cpumask.
364 * @dst: The BPF cpumask being copied into.
365 * @src: The cpumask being copied.
367 * A struct bpf_cpumask pointer may be safely passed to @src.
369 __bpf_kfunc void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src)
371 cpumask_copy((struct cpumask *)dst, src);
375 * bpf_cpumask_any_distribute() - Return a random set CPU from a cpumask.
376 * @cpumask: The cpumask being queried.
379 * * A random set bit within [0, num_cpus) if at least one bit is set.
380 * * >= num_cpus if no bit is set.
382 * A struct bpf_cpumask pointer may be safely passed to @src.
384 __bpf_kfunc u32 bpf_cpumask_any_distribute(const struct cpumask *cpumask)
386 return cpumask_any_distribute(cpumask);
390 * bpf_cpumask_any_and_distribute() - Return a random set CPU from the AND of
392 * @src1: The first cpumask.
393 * @src2: The second cpumask.
396 * * A random set bit within [0, num_cpus) from the AND of two cpumasks, if at
397 * least one bit is set.
398 * * >= num_cpus if no bit is set.
400 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
402 __bpf_kfunc u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,
403 const struct cpumask *src2)
405 return cpumask_any_and_distribute(src1, src2);
408 __bpf_kfunc_end_defs();
410 BTF_SET8_START(cpumask_kfunc_btf_ids)
411 BTF_ID_FLAGS(func, bpf_cpumask_create, KF_ACQUIRE | KF_RET_NULL)
412 BTF_ID_FLAGS(func, bpf_cpumask_release, KF_RELEASE)
413 BTF_ID_FLAGS(func, bpf_cpumask_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS)
414 BTF_ID_FLAGS(func, bpf_cpumask_first, KF_RCU)
415 BTF_ID_FLAGS(func, bpf_cpumask_first_zero, KF_RCU)
416 BTF_ID_FLAGS(func, bpf_cpumask_first_and, KF_RCU)
417 BTF_ID_FLAGS(func, bpf_cpumask_set_cpu, KF_RCU)
418 BTF_ID_FLAGS(func, bpf_cpumask_clear_cpu, KF_RCU)
419 BTF_ID_FLAGS(func, bpf_cpumask_test_cpu, KF_RCU)
420 BTF_ID_FLAGS(func, bpf_cpumask_test_and_set_cpu, KF_RCU)
421 BTF_ID_FLAGS(func, bpf_cpumask_test_and_clear_cpu, KF_RCU)
422 BTF_ID_FLAGS(func, bpf_cpumask_setall, KF_RCU)
423 BTF_ID_FLAGS(func, bpf_cpumask_clear, KF_RCU)
424 BTF_ID_FLAGS(func, bpf_cpumask_and, KF_RCU)
425 BTF_ID_FLAGS(func, bpf_cpumask_or, KF_RCU)
426 BTF_ID_FLAGS(func, bpf_cpumask_xor, KF_RCU)
427 BTF_ID_FLAGS(func, bpf_cpumask_equal, KF_RCU)
428 BTF_ID_FLAGS(func, bpf_cpumask_intersects, KF_RCU)
429 BTF_ID_FLAGS(func, bpf_cpumask_subset, KF_RCU)
430 BTF_ID_FLAGS(func, bpf_cpumask_empty, KF_RCU)
431 BTF_ID_FLAGS(func, bpf_cpumask_full, KF_RCU)
432 BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU)
433 BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU)
434 BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU)
435 BTF_SET8_END(cpumask_kfunc_btf_ids)
437 static const struct btf_kfunc_id_set cpumask_kfunc_set = {
438 .owner = THIS_MODULE,
439 .set = &cpumask_kfunc_btf_ids,
442 BTF_ID_LIST(cpumask_dtor_ids)
443 BTF_ID(struct, bpf_cpumask)
444 BTF_ID(func, bpf_cpumask_release)
446 static int __init cpumask_kfunc_init(void)
449 const struct btf_id_dtor_kfunc cpumask_dtors[] = {
451 .btf_id = cpumask_dtor_ids[0],
452 .kfunc_btf_id = cpumask_dtor_ids[1]
456 ret = bpf_mem_alloc_init(&bpf_cpumask_ma, sizeof(struct bpf_cpumask), false);
457 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &cpumask_kfunc_set);
458 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &cpumask_kfunc_set);
459 return ret ?: register_btf_id_dtor_kfuncs(cpumask_dtors,
460 ARRAY_SIZE(cpumask_dtors),
464 late_initcall(cpumask_kfunc_init);