385 lines · plain
1.. SPDX-License-Identifier: GPL-2.02 3.. _cpumasks-header-label:4 5==================6BPF cpumask kfuncs7==================8 91. Introduction10===============11 12``struct cpumask`` is a bitmap data structure in the kernel whose indices13reflect the CPUs on the system. Commonly, cpumasks are used to track which CPUs14a task is affinitized to, but they can also be used to e.g. track which cores15are associated with a scheduling domain, which cores on a machine are idle,16etc.17 18BPF provides programs with a set of :ref:`kfuncs-header-label` that can be19used to allocate, mutate, query, and free cpumasks.20 212. BPF cpumask objects22======================23 24There are two different types of cpumasks that can be used by BPF programs.25 262.1 ``struct bpf_cpumask *``27----------------------------28 29``struct bpf_cpumask *`` is a cpumask that is allocated by BPF, on behalf of a30BPF program, and whose lifecycle is entirely controlled by BPF. These cpumasks31are RCU-protected, can be mutated, can be used as kptrs, and can be safely cast32to a ``struct cpumask *``.33 342.1.1 ``struct bpf_cpumask *`` lifecycle35----------------------------------------36 37A ``struct bpf_cpumask *`` is allocated, acquired, and released, using the38following functions:39 40.. kernel-doc:: kernel/bpf/cpumask.c41 :identifiers: bpf_cpumask_create42 43.. kernel-doc:: kernel/bpf/cpumask.c44 :identifiers: bpf_cpumask_acquire45 46.. kernel-doc:: kernel/bpf/cpumask.c47 :identifiers: bpf_cpumask_release48 49For example:50 51.. code-block:: c52 53 struct cpumask_map_value {54 struct bpf_cpumask __kptr * cpumask;55 };56 57 struct array_map {58 __uint(type, BPF_MAP_TYPE_ARRAY);59 __type(key, int);60 __type(value, struct cpumask_map_value);61 __uint(max_entries, 65536);62 } cpumask_map SEC(".maps");63 64 static int cpumask_map_insert(struct bpf_cpumask *mask, u32 pid)65 {66 struct cpumask_map_value local, *v;67 long status;68 struct bpf_cpumask *old;69 u32 key = pid;70 71 local.cpumask = NULL;72 status = bpf_map_update_elem(&cpumask_map, &key, &local, 0);73 if (status) {74 bpf_cpumask_release(mask);75 return status;76 }77 78 v = bpf_map_lookup_elem(&cpumask_map, &key);79 if (!v) {80 bpf_cpumask_release(mask);81 return -ENOENT;82 }83 84 old = bpf_kptr_xchg(&v->cpumask, mask);85 if (old)86 bpf_cpumask_release(old);87 88 return 0;89 }90 91 /**92 * A sample tracepoint showing how a task's cpumask can be queried and93 * recorded as a kptr.94 */95 SEC("tp_btf/task_newtask")96 int BPF_PROG(record_task_cpumask, struct task_struct *task, u64 clone_flags)97 {98 struct bpf_cpumask *cpumask;99 int ret;100 101 cpumask = bpf_cpumask_create();102 if (!cpumask)103 return -ENOMEM;104 105 if (!bpf_cpumask_full(task->cpus_ptr))106 bpf_printk("task %s has CPU affinity", task->comm);107 108 bpf_cpumask_copy(cpumask, task->cpus_ptr);109 return cpumask_map_insert(cpumask, task->pid);110 }111 112----113 1142.1.1 ``struct bpf_cpumask *`` as kptrs115---------------------------------------116 117As mentioned and illustrated above, these ``struct bpf_cpumask *`` objects can118also be stored in a map and used as kptrs. If a ``struct bpf_cpumask *`` is in119a map, the reference can be removed from the map with bpf_kptr_xchg(), or120opportunistically acquired using RCU:121 122.. code-block:: c123 124 /* struct containing the struct bpf_cpumask kptr which is stored in the map. */125 struct cpumasks_kfunc_map_value {126 struct bpf_cpumask __kptr * bpf_cpumask;127 };128 129 /* The map containing struct cpumasks_kfunc_map_value entries. */130 struct {131 __uint(type, BPF_MAP_TYPE_ARRAY);132 __type(key, int);133 __type(value, struct cpumasks_kfunc_map_value);134 __uint(max_entries, 1);135 } cpumasks_kfunc_map SEC(".maps");136 137 /* ... */138 139 /**140 * A simple example tracepoint program showing how a141 * struct bpf_cpumask * kptr that is stored in a map can142 * be passed to kfuncs using RCU protection.143 */144 SEC("tp_btf/cgroup_mkdir")145 int BPF_PROG(cgrp_ancestor_example, struct cgroup *cgrp, const char *path)146 {147 struct bpf_cpumask *kptr;148 struct cpumasks_kfunc_map_value *v;149 u32 key = 0;150 151 /* Assume a bpf_cpumask * kptr was previously stored in the map. */152 v = bpf_map_lookup_elem(&cpumasks_kfunc_map, &key);153 if (!v)154 return -ENOENT;155 156 bpf_rcu_read_lock();157 /* Acquire a reference to the bpf_cpumask * kptr that's already stored in the map. */158 kptr = v->cpumask;159 if (!kptr) {160 /* If no bpf_cpumask was present in the map, it's because161 * we're racing with another CPU that removed it with162 * bpf_kptr_xchg() between the bpf_map_lookup_elem()163 * above, and our load of the pointer from the map.164 */165 bpf_rcu_read_unlock();166 return -EBUSY;167 }168 169 bpf_cpumask_setall(kptr);170 bpf_rcu_read_unlock();171 172 return 0;173 }174 175----176 1772.2 ``struct cpumask``178----------------------179 180``struct cpumask`` is the object that actually contains the cpumask bitmap181being queried, mutated, etc. A ``struct bpf_cpumask`` wraps a ``struct182cpumask``, which is why it's safe to cast it as such (note however that it is183**not** safe to cast a ``struct cpumask *`` to a ``struct bpf_cpumask *``, and184the verifier will reject any program that tries to do so).185 186As we'll see below, any kfunc that mutates its cpumask argument will take a187``struct bpf_cpumask *`` as that argument. Any argument that simply queries the188cpumask will instead take a ``struct cpumask *``.189 1903. cpumask kfuncs191=================192 193Above, we described the kfuncs that can be used to allocate, acquire, release,194etc a ``struct bpf_cpumask *``. This section of the document will describe the195kfuncs for mutating and querying cpumasks.196 1973.1 Mutating cpumasks198---------------------199 200Some cpumask kfuncs are "read-only" in that they don't mutate any of their201arguments, whereas others mutate at least one argument (which means that the202argument must be a ``struct bpf_cpumask *``, as described above).203 204This section will describe all of the cpumask kfuncs which mutate at least one205argument. :ref:`cpumasks-querying-label` below describes the read-only kfuncs.206 2073.1.1 Setting and clearing CPUs208-------------------------------209 210bpf_cpumask_set_cpu() and bpf_cpumask_clear_cpu() can be used to set and clear211a CPU in a ``struct bpf_cpumask`` respectively:212 213.. kernel-doc:: kernel/bpf/cpumask.c214 :identifiers: bpf_cpumask_set_cpu bpf_cpumask_clear_cpu215 216These kfuncs are pretty straightforward, and can be used, for example, as217follows:218 219.. code-block:: c220 221 /**222 * A sample tracepoint showing how a cpumask can be queried.223 */224 SEC("tp_btf/task_newtask")225 int BPF_PROG(test_set_clear_cpu, struct task_struct *task, u64 clone_flags)226 {227 struct bpf_cpumask *cpumask;228 229 cpumask = bpf_cpumask_create();230 if (!cpumask)231 return -ENOMEM;232 233 bpf_cpumask_set_cpu(0, cpumask);234 if (!bpf_cpumask_test_cpu(0, cast(cpumask)))235 /* Should never happen. */236 goto release_exit;237 238 bpf_cpumask_clear_cpu(0, cpumask);239 if (bpf_cpumask_test_cpu(0, cast(cpumask)))240 /* Should never happen. */241 goto release_exit;242 243 /* struct cpumask * pointers such as task->cpus_ptr can also be queried. */244 if (bpf_cpumask_test_cpu(0, task->cpus_ptr))245 bpf_printk("task %s can use CPU %d", task->comm, 0);246 247 release_exit:248 bpf_cpumask_release(cpumask);249 return 0;250 }251 252----253 254bpf_cpumask_test_and_set_cpu() and bpf_cpumask_test_and_clear_cpu() are255complementary kfuncs that allow callers to atomically test and set (or clear)256CPUs:257 258.. kernel-doc:: kernel/bpf/cpumask.c259 :identifiers: bpf_cpumask_test_and_set_cpu bpf_cpumask_test_and_clear_cpu260 261----262 263We can also set and clear entire ``struct bpf_cpumask *`` objects in one264operation using bpf_cpumask_setall() and bpf_cpumask_clear():265 266.. kernel-doc:: kernel/bpf/cpumask.c267 :identifiers: bpf_cpumask_setall bpf_cpumask_clear268 2693.1.2 Operations between cpumasks270---------------------------------271 272In addition to setting and clearing individual CPUs in a single cpumask,273callers can also perform bitwise operations between multiple cpumasks using274bpf_cpumask_and(), bpf_cpumask_or(), and bpf_cpumask_xor():275 276.. kernel-doc:: kernel/bpf/cpumask.c277 :identifiers: bpf_cpumask_and bpf_cpumask_or bpf_cpumask_xor278 279The following is an example of how they may be used. Note that some of the280kfuncs shown in this example will be covered in more detail below.281 282.. code-block:: c283 284 /**285 * A sample tracepoint showing how a cpumask can be mutated using286 bitwise operators (and queried).287 */288 SEC("tp_btf/task_newtask")289 int BPF_PROG(test_and_or_xor, struct task_struct *task, u64 clone_flags)290 {291 struct bpf_cpumask *mask1, *mask2, *dst1, *dst2;292 293 mask1 = bpf_cpumask_create();294 if (!mask1)295 return -ENOMEM;296 297 mask2 = bpf_cpumask_create();298 if (!mask2) {299 bpf_cpumask_release(mask1);300 return -ENOMEM;301 }302 303 // ...Safely create the other two masks... */304 305 bpf_cpumask_set_cpu(0, mask1);306 bpf_cpumask_set_cpu(1, mask2);307 bpf_cpumask_and(dst1, (const struct cpumask *)mask1, (const struct cpumask *)mask2);308 if (!bpf_cpumask_empty((const struct cpumask *)dst1))309 /* Should never happen. */310 goto release_exit;311 312 bpf_cpumask_or(dst1, (const struct cpumask *)mask1, (const struct cpumask *)mask2);313 if (!bpf_cpumask_test_cpu(0, (const struct cpumask *)dst1))314 /* Should never happen. */315 goto release_exit;316 317 if (!bpf_cpumask_test_cpu(1, (const struct cpumask *)dst1))318 /* Should never happen. */319 goto release_exit;320 321 bpf_cpumask_xor(dst2, (const struct cpumask *)mask1, (const struct cpumask *)mask2);322 if (!bpf_cpumask_equal((const struct cpumask *)dst1,323 (const struct cpumask *)dst2))324 /* Should never happen. */325 goto release_exit;326 327 release_exit:328 bpf_cpumask_release(mask1);329 bpf_cpumask_release(mask2);330 bpf_cpumask_release(dst1);331 bpf_cpumask_release(dst2);332 return 0;333 }334 335----336 337The contents of an entire cpumask may be copied to another using338bpf_cpumask_copy():339 340.. kernel-doc:: kernel/bpf/cpumask.c341 :identifiers: bpf_cpumask_copy342 343----344 345.. _cpumasks-querying-label:346 3473.2 Querying cpumasks348---------------------349 350In addition to the above kfuncs, there is also a set of read-only kfuncs that351can be used to query the contents of cpumasks.352 353.. kernel-doc:: kernel/bpf/cpumask.c354 :identifiers: bpf_cpumask_first bpf_cpumask_first_zero bpf_cpumask_first_and355 bpf_cpumask_test_cpu bpf_cpumask_weight356 357.. kernel-doc:: kernel/bpf/cpumask.c358 :identifiers: bpf_cpumask_equal bpf_cpumask_intersects bpf_cpumask_subset359 bpf_cpumask_empty bpf_cpumask_full360 361.. kernel-doc:: kernel/bpf/cpumask.c362 :identifiers: bpf_cpumask_any_distribute bpf_cpumask_any_and_distribute363 364----365 366Some example usages of these querying kfuncs were shown above. We will not367replicate those examples here. Note, however, that all of the aforementioned368kfuncs are tested in `tools/testing/selftests/bpf/progs/cpumask_success.c`_, so369please take a look there if you're looking for more examples of how they can be370used.371 372.. _tools/testing/selftests/bpf/progs/cpumask_success.c:373 https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/progs/cpumask_success.c374 375 3764. Adding BPF cpumask kfuncs377============================378 379The set of supported BPF cpumask kfuncs are not (yet) a 1-1 match with the380cpumask operations in include/linux/cpumask.h. Any of those cpumask operations381could easily be encapsulated in a new kfunc if and when required. If you'd like382to support a new cpumask operation, please feel free to submit a patch. If you383do add a new cpumask kfunc, please document it here, and add any relevant384selftest testcases to the cpumask selftest suite.385