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