cpumasks.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. _cpumasks-header-label:
  3. ==================
  4. BPF cpumask kfuncs
  5. ==================
  6. 1. Introduction
  7. ===============
  8. ``struct cpumask`` is a bitmap data structure in the kernel whose indices
  9. reflect the CPUs on the system. Commonly, cpumasks are used to track which CPUs
  10. a task is affinitized to, but they can also be used to e.g. track which cores
  11. are associated with a scheduling domain, which cores on a machine are idle,
  12. etc.
  13. BPF provides programs with a set of :ref:`kfuncs-header-label` that can be
  14. used to allocate, mutate, query, and free cpumasks.
  15. 2. BPF cpumask objects
  16. ======================
  17. There are two different types of cpumasks that can be used by BPF programs.
  18. 2.1 ``struct bpf_cpumask *``
  19. ----------------------------
  20. ``struct bpf_cpumask *`` is a cpumask that is allocated by BPF, on behalf of a
  21. BPF program, and whose lifecycle is entirely controlled by BPF. These cpumasks
  22. are RCU-protected, can be mutated, can be used as kptrs, and can be safely cast
  23. to a ``struct cpumask *``.
  24. 2.1.1 ``struct bpf_cpumask *`` lifecycle
  25. ----------------------------------------
  26. A ``struct bpf_cpumask *`` is allocated, acquired, and released, using the
  27. following functions:
  28. .. kernel-doc:: kernel/bpf/cpumask.c
  29. :identifiers: bpf_cpumask_create
  30. .. kernel-doc:: kernel/bpf/cpumask.c
  31. :identifiers: bpf_cpumask_acquire
  32. .. kernel-doc:: kernel/bpf/cpumask.c
  33. :identifiers: bpf_cpumask_release
  34. For example:
  35. .. code-block:: c
  36. struct cpumask_map_value {
  37. struct bpf_cpumask __kptr * cpumask;
  38. };
  39. struct array_map {
  40. __uint(type, BPF_MAP_TYPE_ARRAY);
  41. __type(key, int);
  42. __type(value, struct cpumask_map_value);
  43. __uint(max_entries, 65536);
  44. } cpumask_map SEC(".maps");
  45. static int cpumask_map_insert(struct bpf_cpumask *mask, u32 pid)
  46. {
  47. struct cpumask_map_value local, *v;
  48. long status;
  49. struct bpf_cpumask *old;
  50. u32 key = pid;
  51. local.cpumask = NULL;
  52. status = bpf_map_update_elem(&cpumask_map, &key, &local, 0);
  53. if (status) {
  54. bpf_cpumask_release(mask);
  55. return status;
  56. }
  57. v = bpf_map_lookup_elem(&cpumask_map, &key);
  58. if (!v) {
  59. bpf_cpumask_release(mask);
  60. return -ENOENT;
  61. }
  62. old = bpf_kptr_xchg(&v->cpumask, mask);
  63. if (old)
  64. bpf_cpumask_release(old);
  65. return 0;
  66. }
  67. /**
  68. * A sample tracepoint showing how a task's cpumask can be queried and
  69. * recorded as a kptr.
  70. */
  71. SEC("tp_btf/task_newtask")
  72. int BPF_PROG(record_task_cpumask, struct task_struct *task, u64 clone_flags)
  73. {
  74. struct bpf_cpumask *cpumask;
  75. int ret;
  76. cpumask = bpf_cpumask_create();
  77. if (!cpumask)
  78. return -ENOMEM;
  79. if (!bpf_cpumask_full(task->cpus_ptr))
  80. bpf_printk("task %s has CPU affinity", task->comm);
  81. bpf_cpumask_copy(cpumask, task->cpus_ptr);
  82. return cpumask_map_insert(cpumask, task->pid);
  83. }
  84. ----
  85. 2.1.1 ``struct bpf_cpumask *`` as kptrs
  86. ---------------------------------------
  87. As mentioned and illustrated above, these ``struct bpf_cpumask *`` objects can
  88. also be stored in a map and used as kptrs. If a ``struct bpf_cpumask *`` is in
  89. a map, the reference can be removed from the map with bpf_kptr_xchg(), or
  90. opportunistically acquired using RCU:
  91. .. code-block:: c
  92. /* struct containing the struct bpf_cpumask kptr which is stored in the map. */
  93. struct cpumasks_kfunc_map_value {
  94. struct bpf_cpumask __kptr * bpf_cpumask;
  95. };
  96. /* The map containing struct cpumasks_kfunc_map_value entries. */
  97. struct {
  98. __uint(type, BPF_MAP_TYPE_ARRAY);
  99. __type(key, int);
  100. __type(value, struct cpumasks_kfunc_map_value);
  101. __uint(max_entries, 1);
  102. } cpumasks_kfunc_map SEC(".maps");
  103. /* ... */
  104. /**
  105. * A simple example tracepoint program showing how a
  106. * struct bpf_cpumask * kptr that is stored in a map can
  107. * be passed to kfuncs using RCU protection.
  108. */
  109. SEC("tp_btf/cgroup_mkdir")
  110. int BPF_PROG(cgrp_ancestor_example, struct cgroup *cgrp, const char *path)
  111. {
  112. struct bpf_cpumask *kptr;
  113. struct cpumasks_kfunc_map_value *v;
  114. u32 key = 0;
  115. /* Assume a bpf_cpumask * kptr was previously stored in the map. */
  116. v = bpf_map_lookup_elem(&cpumasks_kfunc_map, &key);
  117. if (!v)
  118. return -ENOENT;
  119. bpf_rcu_read_lock();
  120. /* Acquire a reference to the bpf_cpumask * kptr that's already stored in the map. */
  121. kptr = v->cpumask;
  122. if (!kptr) {
  123. /* If no bpf_cpumask was present in the map, it's because
  124. * we're racing with another CPU that removed it with
  125. * bpf_kptr_xchg() between the bpf_map_lookup_elem()
  126. * above, and our load of the pointer from the map.
  127. */
  128. bpf_rcu_read_unlock();
  129. return -EBUSY;
  130. }
  131. bpf_cpumask_setall(kptr);
  132. bpf_rcu_read_unlock();
  133. return 0;
  134. }
  135. ----
  136. 2.2 ``struct cpumask``
  137. ----------------------
  138. ``struct cpumask`` is the object that actually contains the cpumask bitmap
  139. being queried, mutated, etc. A ``struct bpf_cpumask`` wraps a ``struct
  140. cpumask``, which is why it's safe to cast it as such (note however that it is
  141. **not** safe to cast a ``struct cpumask *`` to a ``struct bpf_cpumask *``, and
  142. the verifier will reject any program that tries to do so).
  143. As we'll see below, any kfunc that mutates its cpumask argument will take a
  144. ``struct bpf_cpumask *`` as that argument. Any argument that simply queries the
  145. cpumask will instead take a ``struct cpumask *``.
  146. 3. cpumask kfuncs
  147. =================
  148. Above, we described the kfuncs that can be used to allocate, acquire, release,
  149. etc a ``struct bpf_cpumask *``. This section of the document will describe the
  150. kfuncs for mutating and querying cpumasks.
  151. 3.1 Mutating cpumasks
  152. ---------------------
  153. Some cpumask kfuncs are "read-only" in that they don't mutate any of their
  154. arguments, whereas others mutate at least one argument (which means that the
  155. argument must be a ``struct bpf_cpumask *``, as described above).
  156. This section will describe all of the cpumask kfuncs which mutate at least one
  157. argument. :ref:`cpumasks-querying-label` below describes the read-only kfuncs.
  158. 3.1.1 Setting and clearing CPUs
  159. -------------------------------
  160. bpf_cpumask_set_cpu() and bpf_cpumask_clear_cpu() can be used to set and clear
  161. a CPU in a ``struct bpf_cpumask`` respectively:
  162. .. kernel-doc:: kernel/bpf/cpumask.c
  163. :identifiers: bpf_cpumask_set_cpu bpf_cpumask_clear_cpu
  164. These kfuncs are pretty straightforward, and can be used, for example, as
  165. follows:
  166. .. code-block:: c
  167. /**
  168. * A sample tracepoint showing how a cpumask can be queried.
  169. */
  170. SEC("tp_btf/task_newtask")
  171. int BPF_PROG(test_set_clear_cpu, struct task_struct *task, u64 clone_flags)
  172. {
  173. struct bpf_cpumask *cpumask;
  174. cpumask = bpf_cpumask_create();
  175. if (!cpumask)
  176. return -ENOMEM;
  177. bpf_cpumask_set_cpu(0, cpumask);
  178. if (!bpf_cpumask_test_cpu(0, cast(cpumask)))
  179. /* Should never happen. */
  180. goto release_exit;
  181. bpf_cpumask_clear_cpu(0, cpumask);
  182. if (bpf_cpumask_test_cpu(0, cast(cpumask)))
  183. /* Should never happen. */
  184. goto release_exit;
  185. /* struct cpumask * pointers such as task->cpus_ptr can also be queried. */
  186. if (bpf_cpumask_test_cpu(0, task->cpus_ptr))
  187. bpf_printk("task %s can use CPU %d", task->comm, 0);
  188. release_exit:
  189. bpf_cpumask_release(cpumask);
  190. return 0;
  191. }
  192. ----
  193. bpf_cpumask_test_and_set_cpu() and bpf_cpumask_test_and_clear_cpu() are
  194. complementary kfuncs that allow callers to atomically test and set (or clear)
  195. CPUs:
  196. .. kernel-doc:: kernel/bpf/cpumask.c
  197. :identifiers: bpf_cpumask_test_and_set_cpu bpf_cpumask_test_and_clear_cpu
  198. ----
  199. We can also set and clear entire ``struct bpf_cpumask *`` objects in one
  200. operation using bpf_cpumask_setall() and bpf_cpumask_clear():
  201. .. kernel-doc:: kernel/bpf/cpumask.c
  202. :identifiers: bpf_cpumask_setall bpf_cpumask_clear
  203. 3.1.2 Operations between cpumasks
  204. ---------------------------------
  205. In addition to setting and clearing individual CPUs in a single cpumask,
  206. callers can also perform bitwise operations between multiple cpumasks using
  207. bpf_cpumask_and(), bpf_cpumask_or(), and bpf_cpumask_xor():
  208. .. kernel-doc:: kernel/bpf/cpumask.c
  209. :identifiers: bpf_cpumask_and bpf_cpumask_or bpf_cpumask_xor
  210. The following is an example of how they may be used. Note that some of the
  211. kfuncs shown in this example will be covered in more detail below.
  212. .. code-block:: c
  213. /**
  214. * A sample tracepoint showing how a cpumask can be mutated using
  215. bitwise operators (and queried).
  216. */
  217. SEC("tp_btf/task_newtask")
  218. int BPF_PROG(test_and_or_xor, struct task_struct *task, u64 clone_flags)
  219. {
  220. struct bpf_cpumask *mask1, *mask2, *dst1, *dst2;
  221. mask1 = bpf_cpumask_create();
  222. if (!mask1)
  223. return -ENOMEM;
  224. mask2 = bpf_cpumask_create();
  225. if (!mask2) {
  226. bpf_cpumask_release(mask1);
  227. return -ENOMEM;
  228. }
  229. // ...Safely create the other two masks... */
  230. bpf_cpumask_set_cpu(0, mask1);
  231. bpf_cpumask_set_cpu(1, mask2);
  232. bpf_cpumask_and(dst1, (const struct cpumask *)mask1, (const struct cpumask *)mask2);
  233. if (!bpf_cpumask_empty((const struct cpumask *)dst1))
  234. /* Should never happen. */
  235. goto release_exit;
  236. bpf_cpumask_or(dst1, (const struct cpumask *)mask1, (const struct cpumask *)mask2);
  237. if (!bpf_cpumask_test_cpu(0, (const struct cpumask *)dst1))
  238. /* Should never happen. */
  239. goto release_exit;
  240. if (!bpf_cpumask_test_cpu(1, (const struct cpumask *)dst1))
  241. /* Should never happen. */
  242. goto release_exit;
  243. bpf_cpumask_xor(dst2, (const struct cpumask *)mask1, (const struct cpumask *)mask2);
  244. if (!bpf_cpumask_equal((const struct cpumask *)dst1,
  245. (const struct cpumask *)dst2))
  246. /* Should never happen. */
  247. goto release_exit;
  248. release_exit:
  249. bpf_cpumask_release(mask1);
  250. bpf_cpumask_release(mask2);
  251. bpf_cpumask_release(dst1);
  252. bpf_cpumask_release(dst2);
  253. return 0;
  254. }
  255. ----
  256. The contents of an entire cpumask may be copied to another using
  257. bpf_cpumask_copy():
  258. .. kernel-doc:: kernel/bpf/cpumask.c
  259. :identifiers: bpf_cpumask_copy
  260. ----
  261. .. _cpumasks-querying-label:
  262. 3.2 Querying cpumasks
  263. ---------------------
  264. In addition to the above kfuncs, there is also a set of read-only kfuncs that
  265. can be used to query the contents of cpumasks.
  266. .. kernel-doc:: kernel/bpf/cpumask.c
  267. :identifiers: bpf_cpumask_first bpf_cpumask_first_zero bpf_cpumask_first_and
  268. bpf_cpumask_test_cpu bpf_cpumask_weight
  269. .. kernel-doc:: kernel/bpf/cpumask.c
  270. :identifiers: bpf_cpumask_equal bpf_cpumask_intersects bpf_cpumask_subset
  271. bpf_cpumask_empty bpf_cpumask_full
  272. .. kernel-doc:: kernel/bpf/cpumask.c
  273. :identifiers: bpf_cpumask_any_distribute bpf_cpumask_any_and_distribute
  274. ----
  275. Some example usages of these querying kfuncs were shown above. We will not
  276. replicate those examples here. Note, however, that all of the aforementioned
  277. kfuncs are tested in `tools/testing/selftests/bpf/progs/cpumask_success.c`_, so
  278. please take a look there if you're looking for more examples of how they can be
  279. used.
  280. .. _tools/testing/selftests/bpf/progs/cpumask_success.c:
  281. https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/progs/cpumask_success.c
  282. 4. Adding BPF cpumask kfuncs
  283. ============================
  284. The set of supported BPF cpumask kfuncs are not (yet) a 1-1 match with the
  285. cpumask operations in include/linux/cpumask.h. Any of those cpumask operations
  286. could easily be encapsulated in a new kfunc if and when required. If you'd like
  287. to support a new cpumask operation, please feel free to submit a patch. If you
  288. do add a new cpumask kfunc, please document it here, and add any relevant
  289. selftest testcases to the cpumask selftest suite.