1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2023 Meta, Inc */
3 #include <linux/bpf.h>
4 #include <linux/bpf_mem_alloc.h>
5 #include <linux/btf.h>
6 #include <linux/btf_ids.h>
7 #include <linux/cpumask.h>
8
9 /**
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
14 * RCU safety.
15 *
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.
24 */
25 struct bpf_cpumask {
26 cpumask_t cpumask;
27 refcount_t usage;
28 };
29
30 static struct bpf_mem_alloc bpf_cpumask_ma;
31
cpu_valid(u32 cpu)32 static bool cpu_valid(u32 cpu)
33 {
34 return cpu < nr_cpu_ids;
35 }
36
37 __bpf_kfunc_start_defs();
38
39 /**
40 * bpf_cpumask_create() - Create a mutable BPF cpumask.
41 *
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().
45 *
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.
48 */
bpf_cpumask_create(void)49 __bpf_kfunc struct bpf_cpumask *bpf_cpumask_create(void)
50 {
51 struct bpf_cpumask *cpumask;
52
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);
55
56 cpumask = bpf_mem_cache_alloc(&bpf_cpumask_ma);
57 if (!cpumask)
58 return NULL;
59
60 memset(cpumask, 0, sizeof(*cpumask));
61 refcount_set(&cpumask->usage, 1);
62
63 return cpumask;
64 }
65
66 /**
67 * bpf_cpumask_acquire() - Acquire a reference to a BPF cpumask.
68 * @cpumask: The BPF cpumask being acquired. The cpumask must be a trusted
69 * pointer.
70 *
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().
74 */
bpf_cpumask_acquire(struct bpf_cpumask * cpumask)75 __bpf_kfunc struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask)
76 {
77 refcount_inc(&cpumask->usage);
78 return cpumask;
79 }
80
81 /**
82 * bpf_cpumask_release() - Release a previously acquired BPF cpumask.
83 * @cpumask: The cpumask being released.
84 *
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.
88 */
bpf_cpumask_release(struct bpf_cpumask * cpumask)89 __bpf_kfunc void bpf_cpumask_release(struct bpf_cpumask *cpumask)
90 {
91 if (!refcount_dec_and_test(&cpumask->usage))
92 return;
93
94 bpf_mem_cache_free_rcu(&bpf_cpumask_ma, cpumask);
95 }
96
bpf_cpumask_release_dtor(void * cpumask)97 __bpf_kfunc void bpf_cpumask_release_dtor(void *cpumask)
98 {
99 bpf_cpumask_release(cpumask);
100 }
101 CFI_NOSEAL(bpf_cpumask_release_dtor);
102
103 /**
104 * bpf_cpumask_first() - Get the index of the first nonzero bit in the cpumask.
105 * @cpumask: The cpumask being queried.
106 *
107 * Find the index of the first nonzero bit of the cpumask. A struct bpf_cpumask
108 * pointer may be safely passed to this function.
109 */
bpf_cpumask_first(const struct cpumask * cpumask)110 __bpf_kfunc u32 bpf_cpumask_first(const struct cpumask *cpumask)
111 {
112 return cpumask_first(cpumask);
113 }
114
115 /**
116 * bpf_cpumask_first_zero() - Get the index of the first unset bit in the
117 * cpumask.
118 * @cpumask: The cpumask being queried.
119 *
120 * Find the index of the first unset bit of the cpumask. A struct bpf_cpumask
121 * pointer may be safely passed to this function.
122 */
bpf_cpumask_first_zero(const struct cpumask * cpumask)123 __bpf_kfunc u32 bpf_cpumask_first_zero(const struct cpumask *cpumask)
124 {
125 return cpumask_first_zero(cpumask);
126 }
127
128 /**
129 * bpf_cpumask_first_and() - Return the index of the first nonzero bit from the
130 * AND of two cpumasks.
131 * @src1: The first cpumask.
132 * @src2: The second cpumask.
133 *
134 * Find the index of the first nonzero bit of the AND of two cpumasks.
135 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
136 */
bpf_cpumask_first_and(const struct cpumask * src1,const struct cpumask * src2)137 __bpf_kfunc u32 bpf_cpumask_first_and(const struct cpumask *src1,
138 const struct cpumask *src2)
139 {
140 return cpumask_first_and(src1, src2);
141 }
142
143 /**
144 * bpf_cpumask_set_cpu() - Set a bit for a CPU in a BPF cpumask.
145 * @cpu: The CPU to be set in the cpumask.
146 * @cpumask: The BPF cpumask in which a bit is being set.
147 */
bpf_cpumask_set_cpu(u32 cpu,struct bpf_cpumask * cpumask)148 __bpf_kfunc void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask)
149 {
150 if (!cpu_valid(cpu))
151 return;
152
153 cpumask_set_cpu(cpu, (struct cpumask *)cpumask);
154 }
155
156 /**
157 * bpf_cpumask_clear_cpu() - Clear a bit for a CPU in a BPF cpumask.
158 * @cpu: The CPU to be cleared from the cpumask.
159 * @cpumask: The BPF cpumask in which a bit is being cleared.
160 */
bpf_cpumask_clear_cpu(u32 cpu,struct bpf_cpumask * cpumask)161 __bpf_kfunc void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask)
162 {
163 if (!cpu_valid(cpu))
164 return;
165
166 cpumask_clear_cpu(cpu, (struct cpumask *)cpumask);
167 }
168
169 /**
170 * bpf_cpumask_test_cpu() - Test whether a CPU is set in a cpumask.
171 * @cpu: The CPU being queried for.
172 * @cpumask: The cpumask being queried for containing a CPU.
173 *
174 * Return:
175 * * true - @cpu is set in the cpumask
176 * * false - @cpu was not set in the cpumask, or @cpu is an invalid cpu.
177 */
bpf_cpumask_test_cpu(u32 cpu,const struct cpumask * cpumask)178 __bpf_kfunc bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask)
179 {
180 if (!cpu_valid(cpu))
181 return false;
182
183 return cpumask_test_cpu(cpu, (struct cpumask *)cpumask);
184 }
185
186 /**
187 * bpf_cpumask_test_and_set_cpu() - Atomically test and set a CPU in a BPF cpumask.
188 * @cpu: The CPU being set and queried for.
189 * @cpumask: The BPF cpumask being set and queried for containing a CPU.
190 *
191 * Return:
192 * * true - @cpu is set in the cpumask
193 * * false - @cpu was not set in the cpumask, or @cpu is invalid.
194 */
bpf_cpumask_test_and_set_cpu(u32 cpu,struct bpf_cpumask * cpumask)195 __bpf_kfunc bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask)
196 {
197 if (!cpu_valid(cpu))
198 return false;
199
200 return cpumask_test_and_set_cpu(cpu, (struct cpumask *)cpumask);
201 }
202
203 /**
204 * bpf_cpumask_test_and_clear_cpu() - Atomically test and clear a CPU in a BPF
205 * cpumask.
206 * @cpu: The CPU being cleared and queried for.
207 * @cpumask: The BPF cpumask being cleared and queried for containing a CPU.
208 *
209 * Return:
210 * * true - @cpu is set in the cpumask
211 * * false - @cpu was not set in the cpumask, or @cpu is invalid.
212 */
bpf_cpumask_test_and_clear_cpu(u32 cpu,struct bpf_cpumask * cpumask)213 __bpf_kfunc bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask)
214 {
215 if (!cpu_valid(cpu))
216 return false;
217
218 return cpumask_test_and_clear_cpu(cpu, (struct cpumask *)cpumask);
219 }
220
221 /**
222 * bpf_cpumask_setall() - Set all of the bits in a BPF cpumask.
223 * @cpumask: The BPF cpumask having all of its bits set.
224 */
bpf_cpumask_setall(struct bpf_cpumask * cpumask)225 __bpf_kfunc void bpf_cpumask_setall(struct bpf_cpumask *cpumask)
226 {
227 cpumask_setall((struct cpumask *)cpumask);
228 }
229
230 /**
231 * bpf_cpumask_clear() - Clear all of the bits in a BPF cpumask.
232 * @cpumask: The BPF cpumask being cleared.
233 */
bpf_cpumask_clear(struct bpf_cpumask * cpumask)234 __bpf_kfunc void bpf_cpumask_clear(struct bpf_cpumask *cpumask)
235 {
236 cpumask_clear((struct cpumask *)cpumask);
237 }
238
239 /**
240 * bpf_cpumask_and() - AND two cpumasks and store the result.
241 * @dst: The BPF cpumask where the result is being stored.
242 * @src1: The first input.
243 * @src2: The second input.
244 *
245 * Return:
246 * * true - @dst has at least one bit set following the operation
247 * * false - @dst is empty following the operation
248 *
249 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
250 */
bpf_cpumask_and(struct bpf_cpumask * dst,const struct cpumask * src1,const struct cpumask * src2)251 __bpf_kfunc bool bpf_cpumask_and(struct bpf_cpumask *dst,
252 const struct cpumask *src1,
253 const struct cpumask *src2)
254 {
255 return cpumask_and((struct cpumask *)dst, src1, src2);
256 }
257
258 /**
259 * bpf_cpumask_or() - OR two cpumasks and store the result.
260 * @dst: The BPF cpumask where the result is being stored.
261 * @src1: The first input.
262 * @src2: The second input.
263 *
264 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
265 */
bpf_cpumask_or(struct bpf_cpumask * dst,const struct cpumask * src1,const struct cpumask * src2)266 __bpf_kfunc void bpf_cpumask_or(struct bpf_cpumask *dst,
267 const struct cpumask *src1,
268 const struct cpumask *src2)
269 {
270 cpumask_or((struct cpumask *)dst, src1, src2);
271 }
272
273 /**
274 * bpf_cpumask_xor() - XOR two cpumasks and store the result.
275 * @dst: The BPF cpumask where the result is being stored.
276 * @src1: The first input.
277 * @src2: The second input.
278 *
279 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
280 */
bpf_cpumask_xor(struct bpf_cpumask * dst,const struct cpumask * src1,const struct cpumask * src2)281 __bpf_kfunc void bpf_cpumask_xor(struct bpf_cpumask *dst,
282 const struct cpumask *src1,
283 const struct cpumask *src2)
284 {
285 cpumask_xor((struct cpumask *)dst, src1, src2);
286 }
287
288 /**
289 * bpf_cpumask_equal() - Check two cpumasks for equality.
290 * @src1: The first input.
291 * @src2: The second input.
292 *
293 * Return:
294 * * true - @src1 and @src2 have the same bits set.
295 * * false - @src1 and @src2 differ in at least one bit.
296 *
297 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
298 */
bpf_cpumask_equal(const struct cpumask * src1,const struct cpumask * src2)299 __bpf_kfunc bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2)
300 {
301 return cpumask_equal(src1, src2);
302 }
303
304 /**
305 * bpf_cpumask_intersects() - Check two cpumasks for overlap.
306 * @src1: The first input.
307 * @src2: The second input.
308 *
309 * Return:
310 * * true - @src1 and @src2 have at least one of the same bits set.
311 * * false - @src1 and @src2 don't have any of the same bits set.
312 *
313 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
314 */
bpf_cpumask_intersects(const struct cpumask * src1,const struct cpumask * src2)315 __bpf_kfunc bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2)
316 {
317 return cpumask_intersects(src1, src2);
318 }
319
320 /**
321 * bpf_cpumask_subset() - Check if a cpumask is a subset of another.
322 * @src1: The first cpumask being checked as a subset.
323 * @src2: The second cpumask being checked as a superset.
324 *
325 * Return:
326 * * true - All of the bits of @src1 are set in @src2.
327 * * false - At least one bit in @src1 is not set in @src2.
328 *
329 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
330 */
bpf_cpumask_subset(const struct cpumask * src1,const struct cpumask * src2)331 __bpf_kfunc bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2)
332 {
333 return cpumask_subset(src1, src2);
334 }
335
336 /**
337 * bpf_cpumask_empty() - Check if a cpumask is empty.
338 * @cpumask: The cpumask being checked.
339 *
340 * Return:
341 * * true - None of the bits in @cpumask are set.
342 * * false - At least one bit in @cpumask is set.
343 *
344 * A struct bpf_cpumask pointer may be safely passed to @cpumask.
345 */
bpf_cpumask_empty(const struct cpumask * cpumask)346 __bpf_kfunc bool bpf_cpumask_empty(const struct cpumask *cpumask)
347 {
348 return cpumask_empty(cpumask);
349 }
350
351 /**
352 * bpf_cpumask_full() - Check if a cpumask has all bits set.
353 * @cpumask: The cpumask being checked.
354 *
355 * Return:
356 * * true - All of the bits in @cpumask are set.
357 * * false - At least one bit in @cpumask is cleared.
358 *
359 * A struct bpf_cpumask pointer may be safely passed to @cpumask.
360 */
bpf_cpumask_full(const struct cpumask * cpumask)361 __bpf_kfunc bool bpf_cpumask_full(const struct cpumask *cpumask)
362 {
363 return cpumask_full(cpumask);
364 }
365
366 /**
367 * bpf_cpumask_copy() - Copy the contents of a cpumask into a BPF cpumask.
368 * @dst: The BPF cpumask being copied into.
369 * @src: The cpumask being copied.
370 *
371 * A struct bpf_cpumask pointer may be safely passed to @src.
372 */
bpf_cpumask_copy(struct bpf_cpumask * dst,const struct cpumask * src)373 __bpf_kfunc void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src)
374 {
375 cpumask_copy((struct cpumask *)dst, src);
376 }
377
378 /**
379 * bpf_cpumask_any_distribute() - Return a random set CPU from a cpumask.
380 * @cpumask: The cpumask being queried.
381 *
382 * Return:
383 * * A random set bit within [0, num_cpus) if at least one bit is set.
384 * * >= num_cpus if no bit is set.
385 *
386 * A struct bpf_cpumask pointer may be safely passed to @src.
387 */
bpf_cpumask_any_distribute(const struct cpumask * cpumask)388 __bpf_kfunc u32 bpf_cpumask_any_distribute(const struct cpumask *cpumask)
389 {
390 return cpumask_any_distribute(cpumask);
391 }
392
393 /**
394 * bpf_cpumask_any_and_distribute() - Return a random set CPU from the AND of
395 * two cpumasks.
396 * @src1: The first cpumask.
397 * @src2: The second cpumask.
398 *
399 * Return:
400 * * A random set bit within [0, num_cpus) from the AND of two cpumasks, if at
401 * least one bit is set.
402 * * >= num_cpus if no bit is set.
403 *
404 * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
405 */
bpf_cpumask_any_and_distribute(const struct cpumask * src1,const struct cpumask * src2)406 __bpf_kfunc u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,
407 const struct cpumask *src2)
408 {
409 return cpumask_any_and_distribute(src1, src2);
410 }
411
412 /**
413 * bpf_cpumask_weight() - Return the number of bits in @cpumask.
414 * @cpumask: The cpumask being queried.
415 *
416 * Count the number of set bits in the given cpumask.
417 */
bpf_cpumask_weight(const struct cpumask * cpumask)418 __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask)
419 {
420 return cpumask_weight(cpumask);
421 }
422
423 __bpf_kfunc_end_defs();
424
425 BTF_KFUNCS_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)
428 BTF_ID_FLAGS(func, bpf_cpumask_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS)
429 BTF_ID_FLAGS(func, bpf_cpumask_first, KF_RCU)
430 BTF_ID_FLAGS(func, bpf_cpumask_first_zero, KF_RCU)
431 BTF_ID_FLAGS(func, bpf_cpumask_first_and, KF_RCU)
432 BTF_ID_FLAGS(func, bpf_cpumask_set_cpu, KF_RCU)
433 BTF_ID_FLAGS(func, bpf_cpumask_clear_cpu, KF_RCU)
434 BTF_ID_FLAGS(func, bpf_cpumask_test_cpu, KF_RCU)
435 BTF_ID_FLAGS(func, bpf_cpumask_test_and_set_cpu, KF_RCU)
436 BTF_ID_FLAGS(func, bpf_cpumask_test_and_clear_cpu, KF_RCU)
437 BTF_ID_FLAGS(func, bpf_cpumask_setall, KF_RCU)
438 BTF_ID_FLAGS(func, bpf_cpumask_clear, KF_RCU)
439 BTF_ID_FLAGS(func, bpf_cpumask_and, KF_RCU)
440 BTF_ID_FLAGS(func, bpf_cpumask_or, KF_RCU)
441 BTF_ID_FLAGS(func, bpf_cpumask_xor, KF_RCU)
442 BTF_ID_FLAGS(func, bpf_cpumask_equal, KF_RCU)
443 BTF_ID_FLAGS(func, bpf_cpumask_intersects, KF_RCU)
444 BTF_ID_FLAGS(func, bpf_cpumask_subset, KF_RCU)
445 BTF_ID_FLAGS(func, bpf_cpumask_empty, KF_RCU)
446 BTF_ID_FLAGS(func, bpf_cpumask_full, KF_RCU)
447 BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU)
448 BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU)
449 BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU)
450 BTF_ID_FLAGS(func, bpf_cpumask_weight, KF_RCU)
451 BTF_KFUNCS_END(cpumask_kfunc_btf_ids)
452
453 static const struct btf_kfunc_id_set cpumask_kfunc_set = {
454 .owner = THIS_MODULE,
455 .set = &cpumask_kfunc_btf_ids,
456 };
457
458 BTF_ID_LIST(cpumask_dtor_ids)
BTF_ID(struct,bpf_cpumask)459 BTF_ID(struct, bpf_cpumask)
460 BTF_ID(func, bpf_cpumask_release_dtor)
461
462 static int __init cpumask_kfunc_init(void)
463 {
464 int ret;
465 const struct btf_id_dtor_kfunc cpumask_dtors[] = {
466 {
467 .btf_id = cpumask_dtor_ids[0],
468 .kfunc_btf_id = cpumask_dtor_ids[1]
469 },
470 };
471
472 ret = bpf_mem_alloc_init(&bpf_cpumask_ma, sizeof(struct bpf_cpumask), false);
473 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &cpumask_kfunc_set);
474 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &cpumask_kfunc_set);
475 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &cpumask_kfunc_set);
476 return ret ?: register_btf_id_dtor_kfuncs(cpumask_dtors,
477 ARRAY_SIZE(cpumask_dtors),
478 THIS_MODULE);
479 }
480
481 late_initcall(cpumask_kfunc_init);
482