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;
38 __diag_ignore_all("-Wmissing-prototypes",
39 "Global kfuncs as their definitions will be in BTF");
42 * bpf_cpumask_create() - Create a mutable BPF cpumask.
44 * Allocates a cpumask that can be queried, mutated, acquired, and released by
45 * a BPF program. The cpumask returned by this function must either be embedded
46 * in a map as a kptr, or freed with bpf_cpumask_release().
48 * bpf_cpumask_create() allocates memory using the BPF memory allocator, and
49 * will not block. It may return NULL if no memory is available.
51 __bpf_kfunc struct bpf_cpumask *bpf_cpumask_create(void)
53 struct bpf_cpumask *cpumask;
55 /* cpumask must be the first element so struct bpf_cpumask be cast to struct cpumask. */
56 BUILD_BUG_ON(offsetof(struct bpf_cpumask, cpumask) != 0);
58 cpumask = bpf_mem_alloc(&bpf_cpumask_ma, sizeof(*cpumask));
62 memset(cpumask, 0, sizeof(*cpumask));
63 refcount_set(&cpumask->usage, 1);
69 * bpf_cpumask_acquire() - Acquire a reference to a BPF cpumask.
70 * @cpumask: The BPF cpumask being acquired. The cpumask must be a trusted
73 * Acquires a reference to a BPF cpumask. The cpumask returned by this function
74 * must either be embedded in a map as a kptr, or freed with
75 * bpf_cpumask_release().
77 __bpf_kfunc struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask)
79 refcount_inc(&cpumask->usage);
84 * bpf_cpumask_kptr_get() - Attempt to acquire a reference to a BPF cpumask
86 * @cpumaskp: A pointer to a BPF cpumask map value.
88 * Attempts to acquire a reference to a BPF cpumask stored in a map value. The
89 * cpumask returned by this function must either be embedded in a map as a
90 * kptr, or freed with bpf_cpumask_release(). This function may return NULL if
91 * no BPF cpumask was found in the specified map value.
93 __bpf_kfunc struct bpf_cpumask *bpf_cpumask_kptr_get(struct bpf_cpumask **cpumaskp)
95 struct bpf_cpumask *cpumask;
97 /* The BPF memory allocator frees memory backing its caches in an RCU
98 * callback. Thus, we can safely use RCU to ensure that the cpumask is
103 cpumask = READ_ONCE(*cpumaskp);
104 if (cpumask && !refcount_inc_not_zero(&cpumask->usage))
112 * bpf_cpumask_release() - Release a previously acquired BPF cpumask.
113 * @cpumask: The cpumask being released.
115 * Releases a previously acquired reference to a BPF cpumask. When the final
116 * reference of the BPF cpumask has been released, it is subsequently freed in
117 * an RCU callback in the BPF memory allocator.
119 __bpf_kfunc void bpf_cpumask_release(struct bpf_cpumask *cpumask)
124 if (refcount_dec_and_test(&cpumask->usage)) {
126 bpf_mem_free(&bpf_cpumask_ma, cpumask);
132 * bpf_cpumask_first() - Get the index of the first nonzero bit in the cpumask.
133 * @cpumask: The cpumask being queried.
135 * Find the index of the first nonzero bit of the cpumask. A struct bpf_cpumask
136 * pointer may be safely passed to this function.
138 __bpf_kfunc u32 bpf_cpumask_first(const struct cpumask *cpumask)
140 return cpumask_first(cpumask);
144 * bpf_cpumask_first_zero() - Get the index of the first unset bit in the
146 * @cpumask: The cpumask being queried.
148 * Find the index of the first unset bit of the cpumask. A struct bpf_cpumask
149 * pointer may be safely passed to this function.
151 __bpf_kfunc u32 bpf_cpumask_first_zero(const struct cpumask *cpumask)
153 return cpumask_first_zero(cpumask);
157 * bpf_cpumask_set_cpu() - Set a bit for a CPU in a BPF cpumask.
158 * @cpu: The CPU to be set in the cpumask.
159 * @cpumask: The BPF cpumask in which a bit is being set.
161 __bpf_kfunc void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask)
166 cpumask_set_cpu(cpu, (struct cpumask *)cpumask);
170 * bpf_cpumask_clear_cpu() - Clear a bit for a CPU in a BPF cpumask.
171 * @cpu: The CPU to be cleared from the cpumask.
172 * @cpumask: The BPF cpumask in which a bit is being cleared.
174 __bpf_kfunc void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask)
179 cpumask_clear_cpu(cpu, (struct cpumask *)cpumask);
183 * bpf_cpumask_test_cpu() - Test whether a CPU is set in a cpumask.
184 * @cpu: The CPU being queried for.
185 * @cpumask: The cpumask being 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 an invalid cpu.
191 __bpf_kfunc bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask)
196 return cpumask_test_cpu(cpu, (struct cpumask *)cpumask);
200 * bpf_cpumask_test_and_set_cpu() - Atomically test and set a CPU in a BPF cpumask.
201 * @cpu: The CPU being set and queried for.
202 * @cpumask: The BPF cpumask being set and queried for containing a CPU.
205 * * true - @cpu is set in the cpumask
206 * * false - @cpu was not set in the cpumask, or @cpu is invalid.
208 __bpf_kfunc bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask)
213 return cpumask_test_and_set_cpu(cpu, (struct cpumask *)cpumask);
217 * bpf_cpumask_test_and_clear_cpu() - Atomically test and clear a CPU in a BPF
219 * @cpu: The CPU being cleared and queried for.
220 * @cpumask: The BPF cpumask being cleared and queried for containing a CPU.
223 * * true - @cpu is set in the cpumask
224 * * false - @cpu was not set in the cpumask, or @cpu is invalid.
226 __bpf_kfunc bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask)
231 return cpumask_test_and_clear_cpu(cpu, (struct cpumask *)cpumask);
235 * bpf_cpumask_setall() - Set all of the bits in a BPF cpumask.
236 * @cpumask: The BPF cpumask having all of its bits set.
238 __bpf_kfunc void bpf_cpumask_setall(struct bpf_cpumask *cpumask)
240 cpumask_setall((struct cpumask *)cpumask);
244 * bpf_cpumask_clear() - Clear all of the bits in a BPF cpumask.
245 * @cpumask: The BPF cpumask being cleared.
247 __bpf_kfunc void bpf_cpumask_clear(struct bpf_cpumask *cpumask)
249 cpumask_clear((struct cpumask *)cpumask);
253 * bpf_cpumask_and() - AND two cpumasks and store the result.
254 * @dst: The BPF cpumask where the result is being stored.
255 * @src1: The first input.
256 * @src2: The second input.
259 * * true - @dst has at least one bit set following the operation
260 * * false - @dst is empty following the operation
262 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
264 __bpf_kfunc bool bpf_cpumask_and(struct bpf_cpumask *dst,
265 const struct cpumask *src1,
266 const struct cpumask *src2)
268 return cpumask_and((struct cpumask *)dst, src1, src2);
272 * bpf_cpumask_or() - OR two cpumasks and store the result.
273 * @dst: The BPF cpumask where the result is being stored.
274 * @src1: The first input.
275 * @src2: The second input.
277 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
279 __bpf_kfunc void bpf_cpumask_or(struct bpf_cpumask *dst,
280 const struct cpumask *src1,
281 const struct cpumask *src2)
283 cpumask_or((struct cpumask *)dst, src1, src2);
287 * bpf_cpumask_xor() - XOR two cpumasks and store the result.
288 * @dst: The BPF cpumask where the result is being stored.
289 * @src1: The first input.
290 * @src2: The second input.
292 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
294 __bpf_kfunc void bpf_cpumask_xor(struct bpf_cpumask *dst,
295 const struct cpumask *src1,
296 const struct cpumask *src2)
298 cpumask_xor((struct cpumask *)dst, src1, src2);
302 * bpf_cpumask_equal() - Check two cpumasks for equality.
303 * @src1: The first input.
304 * @src2: The second input.
307 * * true - @src1 and @src2 have the same bits set.
308 * * false - @src1 and @src2 differ in at least one bit.
310 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
312 __bpf_kfunc bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2)
314 return cpumask_equal(src1, src2);
318 * bpf_cpumask_intersects() - Check two cpumasks for overlap.
319 * @src1: The first input.
320 * @src2: The second input.
323 * * true - @src1 and @src2 have at least one of the same bits set.
324 * * false - @src1 and @src2 don't have any of the same bits set.
326 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
328 __bpf_kfunc bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2)
330 return cpumask_intersects(src1, src2);
334 * bpf_cpumask_subset() - Check if a cpumask is a subset of another.
335 * @src1: The first cpumask being checked as a subset.
336 * @src2: The second cpumask being checked as a superset.
339 * * true - All of the bits of @src1 are set in @src2.
340 * * false - At least one bit in @src1 is not set in @src2.
342 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
344 __bpf_kfunc bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2)
346 return cpumask_subset(src1, src2);
350 * bpf_cpumask_empty() - Check if a cpumask is empty.
351 * @cpumask: The cpumask being checked.
354 * * true - None of the bits in @cpumask are set.
355 * * false - At least one bit in @cpumask is set.
357 * A struct bpf_cpumask pointer may be safely passed to @cpumask.
359 __bpf_kfunc bool bpf_cpumask_empty(const struct cpumask *cpumask)
361 return cpumask_empty(cpumask);
365 * bpf_cpumask_full() - Check if a cpumask has all bits set.
366 * @cpumask: The cpumask being checked.
369 * * true - All of the bits in @cpumask are set.
370 * * false - At least one bit in @cpumask is cleared.
372 * A struct bpf_cpumask pointer may be safely passed to @cpumask.
374 __bpf_kfunc bool bpf_cpumask_full(const struct cpumask *cpumask)
376 return cpumask_full(cpumask);
380 * bpf_cpumask_copy() - Copy the contents of a cpumask into a BPF cpumask.
381 * @dst: The BPF cpumask being copied into.
382 * @src: The cpumask being copied.
384 * A struct bpf_cpumask pointer may be safely passed to @src.
386 __bpf_kfunc void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src)
388 cpumask_copy((struct cpumask *)dst, src);
392 * bpf_cpumask_any() - Return a random set CPU from a cpumask.
393 * @cpumask: The cpumask being queried.
396 * * A random set bit within [0, num_cpus) if at least one bit is set.
397 * * >= num_cpus if no bit is set.
399 * A struct bpf_cpumask pointer may be safely passed to @src.
401 __bpf_kfunc u32 bpf_cpumask_any(const struct cpumask *cpumask)
403 return cpumask_any(cpumask);
407 * bpf_cpumask_any_and() - Return a random set CPU from the AND of two
409 * @src1: The first cpumask.
410 * @src2: The second cpumask.
413 * * A random set bit within [0, num_cpus) if at least one bit is set.
414 * * >= num_cpus if no bit is set.
416 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
418 __bpf_kfunc u32 bpf_cpumask_any_and(const struct cpumask *src1, const struct cpumask *src2)
420 return cpumask_any_and(src1, src2);
425 BTF_SET8_START(cpumask_kfunc_btf_ids)
426 BTF_ID_FLAGS(func, bpf_cpumask_create, KF_ACQUIRE | KF_RET_NULL)
427 BTF_ID_FLAGS(func, bpf_cpumask_release, KF_RELEASE | KF_TRUSTED_ARGS)
428 BTF_ID_FLAGS(func, bpf_cpumask_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS)
429 BTF_ID_FLAGS(func, bpf_cpumask_kptr_get, KF_ACQUIRE | KF_KPTR_GET | KF_RET_NULL)
430 BTF_ID_FLAGS(func, bpf_cpumask_first, KF_TRUSTED_ARGS)
431 BTF_ID_FLAGS(func, bpf_cpumask_first_zero, KF_TRUSTED_ARGS)
432 BTF_ID_FLAGS(func, bpf_cpumask_set_cpu, KF_TRUSTED_ARGS)
433 BTF_ID_FLAGS(func, bpf_cpumask_clear_cpu, KF_TRUSTED_ARGS)
434 BTF_ID_FLAGS(func, bpf_cpumask_test_cpu, KF_TRUSTED_ARGS)
435 BTF_ID_FLAGS(func, bpf_cpumask_test_and_set_cpu, KF_TRUSTED_ARGS)
436 BTF_ID_FLAGS(func, bpf_cpumask_test_and_clear_cpu, KF_TRUSTED_ARGS)
437 BTF_ID_FLAGS(func, bpf_cpumask_setall, KF_TRUSTED_ARGS)
438 BTF_ID_FLAGS(func, bpf_cpumask_clear, KF_TRUSTED_ARGS)
439 BTF_ID_FLAGS(func, bpf_cpumask_and, KF_TRUSTED_ARGS)
440 BTF_ID_FLAGS(func, bpf_cpumask_or, KF_TRUSTED_ARGS)
441 BTF_ID_FLAGS(func, bpf_cpumask_xor, KF_TRUSTED_ARGS)
442 BTF_ID_FLAGS(func, bpf_cpumask_equal, KF_TRUSTED_ARGS)
443 BTF_ID_FLAGS(func, bpf_cpumask_intersects, KF_TRUSTED_ARGS)
444 BTF_ID_FLAGS(func, bpf_cpumask_subset, KF_TRUSTED_ARGS)
445 BTF_ID_FLAGS(func, bpf_cpumask_empty, KF_TRUSTED_ARGS)
446 BTF_ID_FLAGS(func, bpf_cpumask_full, KF_TRUSTED_ARGS)
447 BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_TRUSTED_ARGS)
448 BTF_ID_FLAGS(func, bpf_cpumask_any, KF_TRUSTED_ARGS)
449 BTF_ID_FLAGS(func, bpf_cpumask_any_and, KF_TRUSTED_ARGS)
450 BTF_SET8_END(cpumask_kfunc_btf_ids)
452 static const struct btf_kfunc_id_set cpumask_kfunc_set = {
453 .owner = THIS_MODULE,
454 .set = &cpumask_kfunc_btf_ids,
457 BTF_ID_LIST(cpumask_dtor_ids)
458 BTF_ID(struct, bpf_cpumask)
459 BTF_ID(func, bpf_cpumask_release)
461 static int __init cpumask_kfunc_init(void)
464 const struct btf_id_dtor_kfunc cpumask_dtors[] = {
466 .btf_id = cpumask_dtor_ids[0],
467 .kfunc_btf_id = cpumask_dtor_ids[1]
471 ret = bpf_mem_alloc_init(&bpf_cpumask_ma, 0, false);
472 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &cpumask_kfunc_set);
473 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &cpumask_kfunc_set);
474 return ret ?: register_btf_id_dtor_kfuncs(cpumask_dtors,
475 ARRAY_SIZE(cpumask_dtors),
479 late_initcall(cpumask_kfunc_init);