cpumask.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. * struct bpf_cpumask - refcounted BPF cpumask wrapper structure
  10. * @cpumask: The actual cpumask embedded in the struct.
  11. * @usage: Object reference counter. When the refcount goes to 0, the
  12. * memory is released back to the BPF allocator, which provides
  13. * RCU safety.
  14. *
  15. * Note that we explicitly embed a cpumask_t rather than a cpumask_var_t. This
  16. * is done to avoid confusing the verifier due to the typedef of cpumask_var_t
  17. * changing depending on whether CONFIG_CPUMASK_OFFSTACK is defined or not. See
  18. * the details in <linux/cpumask.h>. The consequence is that this structure is
  19. * likely a bit larger than it needs to be when CONFIG_CPUMASK_OFFSTACK is
  20. * defined due to embedding the whole NR_CPUS-size bitmap, but the extra memory
  21. * overhead is minimal. For the more typical case of CONFIG_CPUMASK_OFFSTACK
  22. * not being defined, the structure is the same size regardless.
  23. */
  24. struct bpf_cpumask {
  25. cpumask_t cpumask;
  26. refcount_t usage;
  27. };
  28. static struct bpf_mem_alloc bpf_cpumask_ma;
  29. static bool cpu_valid(u32 cpu)
  30. {
  31. return cpu < nr_cpu_ids;
  32. }
  33. __bpf_kfunc_start_defs();
  34. /**
  35. * bpf_cpumask_create() - Create a mutable BPF cpumask.
  36. *
  37. * Allocates a cpumask that can be queried, mutated, acquired, and released by
  38. * a BPF program. The cpumask returned by this function must either be embedded
  39. * in a map as a kptr, or freed with bpf_cpumask_release().
  40. *
  41. * bpf_cpumask_create() allocates memory using the BPF memory allocator, and
  42. * will not block. It may return NULL if no memory is available.
  43. */
  44. __bpf_kfunc struct bpf_cpumask *bpf_cpumask_create(void)
  45. {
  46. struct bpf_cpumask *cpumask;
  47. /* cpumask must be the first element so struct bpf_cpumask be cast to struct cpumask. */
  48. BUILD_BUG_ON(offsetof(struct bpf_cpumask, cpumask) != 0);
  49. cpumask = bpf_mem_cache_alloc(&bpf_cpumask_ma);
  50. if (!cpumask)
  51. return NULL;
  52. memset(cpumask, 0, sizeof(*cpumask));
  53. refcount_set(&cpumask->usage, 1);
  54. return cpumask;
  55. }
  56. /**
  57. * bpf_cpumask_acquire() - Acquire a reference to a BPF cpumask.
  58. * @cpumask: The BPF cpumask being acquired. The cpumask must be a trusted
  59. * pointer.
  60. *
  61. * Acquires a reference to a BPF cpumask. The cpumask returned by this function
  62. * must either be embedded in a map as a kptr, or freed with
  63. * bpf_cpumask_release().
  64. */
  65. __bpf_kfunc struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask)
  66. {
  67. refcount_inc(&cpumask->usage);
  68. return cpumask;
  69. }
  70. /**
  71. * bpf_cpumask_release() - Release a previously acquired BPF cpumask.
  72. * @cpumask: The cpumask being released.
  73. *
  74. * Releases a previously acquired reference to a BPF cpumask. When the final
  75. * reference of the BPF cpumask has been released, it is subsequently freed in
  76. * an RCU callback in the BPF memory allocator.
  77. */
  78. __bpf_kfunc void bpf_cpumask_release(struct bpf_cpumask *cpumask)
  79. {
  80. if (!refcount_dec_and_test(&cpumask->usage))
  81. return;
  82. migrate_disable();
  83. bpf_mem_cache_free_rcu(&bpf_cpumask_ma, cpumask);
  84. migrate_enable();
  85. }
  86. __bpf_kfunc void bpf_cpumask_release_dtor(void *cpumask)
  87. {
  88. bpf_cpumask_release(cpumask);
  89. }
  90. CFI_NOSEAL(bpf_cpumask_release_dtor);
  91. /**
  92. * bpf_cpumask_first() - Get the index of the first nonzero bit in the cpumask.
  93. * @cpumask: The cpumask being queried.
  94. *
  95. * Find the index of the first nonzero bit of the cpumask. A struct bpf_cpumask
  96. * pointer may be safely passed to this function.
  97. */
  98. __bpf_kfunc u32 bpf_cpumask_first(const struct cpumask *cpumask)
  99. {
  100. return cpumask_first(cpumask);
  101. }
  102. /**
  103. * bpf_cpumask_first_zero() - Get the index of the first unset bit in the
  104. * cpumask.
  105. * @cpumask: The cpumask being queried.
  106. *
  107. * Find the index of the first unset bit of the cpumask. A struct bpf_cpumask
  108. * pointer may be safely passed to this function.
  109. */
  110. __bpf_kfunc u32 bpf_cpumask_first_zero(const struct cpumask *cpumask)
  111. {
  112. return cpumask_first_zero(cpumask);
  113. }
  114. /**
  115. * bpf_cpumask_first_and() - Return the index of the first nonzero bit from the
  116. * AND of two cpumasks.
  117. * @src1: The first cpumask.
  118. * @src2: The second cpumask.
  119. *
  120. * Find the index of the first nonzero bit of the AND of two cpumasks.
  121. * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
  122. */
  123. __bpf_kfunc u32 bpf_cpumask_first_and(const struct cpumask *src1,
  124. const struct cpumask *src2)
  125. {
  126. return cpumask_first_and(src1, src2);
  127. }
  128. /**
  129. * bpf_cpumask_set_cpu() - Set a bit for a CPU in a BPF cpumask.
  130. * @cpu: The CPU to be set in the cpumask.
  131. * @cpumask: The BPF cpumask in which a bit is being set.
  132. */
  133. __bpf_kfunc void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask)
  134. {
  135. if (!cpu_valid(cpu))
  136. return;
  137. cpumask_set_cpu(cpu, (struct cpumask *)cpumask);
  138. }
  139. /**
  140. * bpf_cpumask_clear_cpu() - Clear a bit for a CPU in a BPF cpumask.
  141. * @cpu: The CPU to be cleared from the cpumask.
  142. * @cpumask: The BPF cpumask in which a bit is being cleared.
  143. */
  144. __bpf_kfunc void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask)
  145. {
  146. if (!cpu_valid(cpu))
  147. return;
  148. cpumask_clear_cpu(cpu, (struct cpumask *)cpumask);
  149. }
  150. /**
  151. * bpf_cpumask_test_cpu() - Test whether a CPU is set in a cpumask.
  152. * @cpu: The CPU being queried for.
  153. * @cpumask: The cpumask being queried for containing a CPU.
  154. *
  155. * Return:
  156. * * true - @cpu is set in the cpumask
  157. * * false - @cpu was not set in the cpumask, or @cpu is an invalid cpu.
  158. */
  159. __bpf_kfunc bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask)
  160. {
  161. if (!cpu_valid(cpu))
  162. return false;
  163. return cpumask_test_cpu(cpu, (struct cpumask *)cpumask);
  164. }
  165. /**
  166. * bpf_cpumask_test_and_set_cpu() - Atomically test and set a CPU in a BPF cpumask.
  167. * @cpu: The CPU being set and queried for.
  168. * @cpumask: The BPF cpumask being set and queried for containing a CPU.
  169. *
  170. * Return:
  171. * * true - @cpu is set in the cpumask
  172. * * false - @cpu was not set in the cpumask, or @cpu is invalid.
  173. */
  174. __bpf_kfunc bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask)
  175. {
  176. if (!cpu_valid(cpu))
  177. return false;
  178. return cpumask_test_and_set_cpu(cpu, (struct cpumask *)cpumask);
  179. }
  180. /**
  181. * bpf_cpumask_test_and_clear_cpu() - Atomically test and clear a CPU in a BPF
  182. * cpumask.
  183. * @cpu: The CPU being cleared and queried for.
  184. * @cpumask: The BPF cpumask being cleared and queried for containing a CPU.
  185. *
  186. * Return:
  187. * * true - @cpu is set in the cpumask
  188. * * false - @cpu was not set in the cpumask, or @cpu is invalid.
  189. */
  190. __bpf_kfunc bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask)
  191. {
  192. if (!cpu_valid(cpu))
  193. return false;
  194. return cpumask_test_and_clear_cpu(cpu, (struct cpumask *)cpumask);
  195. }
  196. /**
  197. * bpf_cpumask_setall() - Set all of the bits in a BPF cpumask.
  198. * @cpumask: The BPF cpumask having all of its bits set.
  199. */
  200. __bpf_kfunc void bpf_cpumask_setall(struct bpf_cpumask *cpumask)
  201. {
  202. cpumask_setall((struct cpumask *)cpumask);
  203. }
  204. /**
  205. * bpf_cpumask_clear() - Clear all of the bits in a BPF cpumask.
  206. * @cpumask: The BPF cpumask being cleared.
  207. */
  208. __bpf_kfunc void bpf_cpumask_clear(struct bpf_cpumask *cpumask)
  209. {
  210. cpumask_clear((struct cpumask *)cpumask);
  211. }
  212. /**
  213. * bpf_cpumask_and() - AND two cpumasks and store the result.
  214. * @dst: The BPF cpumask where the result is being stored.
  215. * @src1: The first input.
  216. * @src2: The second input.
  217. *
  218. * Return:
  219. * * true - @dst has at least one bit set following the operation
  220. * * false - @dst is empty following the operation
  221. *
  222. * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
  223. */
  224. __bpf_kfunc bool bpf_cpumask_and(struct bpf_cpumask *dst,
  225. const struct cpumask *src1,
  226. const struct cpumask *src2)
  227. {
  228. return cpumask_and((struct cpumask *)dst, src1, src2);
  229. }
  230. /**
  231. * bpf_cpumask_or() - OR two cpumasks and store the result.
  232. * @dst: The BPF cpumask where the result is being stored.
  233. * @src1: The first input.
  234. * @src2: The second input.
  235. *
  236. * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
  237. */
  238. __bpf_kfunc void bpf_cpumask_or(struct bpf_cpumask *dst,
  239. const struct cpumask *src1,
  240. const struct cpumask *src2)
  241. {
  242. cpumask_or((struct cpumask *)dst, src1, src2);
  243. }
  244. /**
  245. * bpf_cpumask_xor() - XOR two cpumasks and store the result.
  246. * @dst: The BPF cpumask where the result is being stored.
  247. * @src1: The first input.
  248. * @src2: The second input.
  249. *
  250. * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
  251. */
  252. __bpf_kfunc void bpf_cpumask_xor(struct bpf_cpumask *dst,
  253. const struct cpumask *src1,
  254. const struct cpumask *src2)
  255. {
  256. cpumask_xor((struct cpumask *)dst, src1, src2);
  257. }
  258. /**
  259. * bpf_cpumask_equal() - Check two cpumasks for equality.
  260. * @src1: The first input.
  261. * @src2: The second input.
  262. *
  263. * Return:
  264. * * true - @src1 and @src2 have the same bits set.
  265. * * false - @src1 and @src2 differ in at least one bit.
  266. *
  267. * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
  268. */
  269. __bpf_kfunc bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2)
  270. {
  271. return cpumask_equal(src1, src2);
  272. }
  273. /**
  274. * bpf_cpumask_intersects() - Check two cpumasks for overlap.
  275. * @src1: The first input.
  276. * @src2: The second input.
  277. *
  278. * Return:
  279. * * true - @src1 and @src2 have at least one of the same bits set.
  280. * * false - @src1 and @src2 don't have any of the same bits set.
  281. *
  282. * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
  283. */
  284. __bpf_kfunc bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2)
  285. {
  286. return cpumask_intersects(src1, src2);
  287. }
  288. /**
  289. * bpf_cpumask_subset() - Check if a cpumask is a subset of another.
  290. * @src1: The first cpumask being checked as a subset.
  291. * @src2: The second cpumask being checked as a superset.
  292. *
  293. * Return:
  294. * * true - All of the bits of @src1 are set in @src2.
  295. * * false - At least one bit in @src1 is not set in @src2.
  296. *
  297. * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
  298. */
  299. __bpf_kfunc bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2)
  300. {
  301. return cpumask_subset(src1, src2);
  302. }
  303. /**
  304. * bpf_cpumask_empty() - Check if a cpumask is empty.
  305. * @cpumask: The cpumask being checked.
  306. *
  307. * Return:
  308. * * true - None of the bits in @cpumask are set.
  309. * * false - At least one bit in @cpumask is set.
  310. *
  311. * A struct bpf_cpumask pointer may be safely passed to @cpumask.
  312. */
  313. __bpf_kfunc bool bpf_cpumask_empty(const struct cpumask *cpumask)
  314. {
  315. return cpumask_empty(cpumask);
  316. }
  317. /**
  318. * bpf_cpumask_full() - Check if a cpumask has all bits set.
  319. * @cpumask: The cpumask being checked.
  320. *
  321. * Return:
  322. * * true - All of the bits in @cpumask are set.
  323. * * false - At least one bit in @cpumask is cleared.
  324. *
  325. * A struct bpf_cpumask pointer may be safely passed to @cpumask.
  326. */
  327. __bpf_kfunc bool bpf_cpumask_full(const struct cpumask *cpumask)
  328. {
  329. return cpumask_full(cpumask);
  330. }
  331. /**
  332. * bpf_cpumask_copy() - Copy the contents of a cpumask into a BPF cpumask.
  333. * @dst: The BPF cpumask being copied into.
  334. * @src: The cpumask being copied.
  335. *
  336. * A struct bpf_cpumask pointer may be safely passed to @src.
  337. */
  338. __bpf_kfunc void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src)
  339. {
  340. cpumask_copy((struct cpumask *)dst, src);
  341. }
  342. /**
  343. * bpf_cpumask_any_distribute() - Return a random set CPU from a cpumask.
  344. * @cpumask: The cpumask being queried.
  345. *
  346. * Return:
  347. * * A random set bit within [0, num_cpus) if at least one bit is set.
  348. * * >= num_cpus if no bit is set.
  349. *
  350. * A struct bpf_cpumask pointer may be safely passed to @src.
  351. */
  352. __bpf_kfunc u32 bpf_cpumask_any_distribute(const struct cpumask *cpumask)
  353. {
  354. return cpumask_any_distribute(cpumask);
  355. }
  356. /**
  357. * bpf_cpumask_any_and_distribute() - Return a random set CPU from the AND of
  358. * two cpumasks.
  359. * @src1: The first cpumask.
  360. * @src2: The second cpumask.
  361. *
  362. * Return:
  363. * * A random set bit within [0, num_cpus) from the AND of two cpumasks, if at
  364. * least one bit is set.
  365. * * >= num_cpus if no bit is set.
  366. *
  367. * struct bpf_cpumask pointers may be safely passed to @src1 and @src2.
  368. */
  369. __bpf_kfunc u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,
  370. const struct cpumask *src2)
  371. {
  372. return cpumask_any_and_distribute(src1, src2);
  373. }
  374. /**
  375. * bpf_cpumask_weight() - Return the number of bits in @cpumask.
  376. * @cpumask: The cpumask being queried.
  377. *
  378. * Count the number of set bits in the given cpumask.
  379. */
  380. __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask)
  381. {
  382. return cpumask_weight(cpumask);
  383. }
  384. __bpf_kfunc_end_defs();
  385. BTF_KFUNCS_START(cpumask_kfunc_btf_ids)
  386. BTF_ID_FLAGS(func, bpf_cpumask_create, KF_ACQUIRE | KF_RET_NULL)
  387. BTF_ID_FLAGS(func, bpf_cpumask_release, KF_RELEASE)
  388. BTF_ID_FLAGS(func, bpf_cpumask_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS)
  389. BTF_ID_FLAGS(func, bpf_cpumask_first, KF_RCU)
  390. BTF_ID_FLAGS(func, bpf_cpumask_first_zero, KF_RCU)
  391. BTF_ID_FLAGS(func, bpf_cpumask_first_and, KF_RCU)
  392. BTF_ID_FLAGS(func, bpf_cpumask_set_cpu, KF_RCU)
  393. BTF_ID_FLAGS(func, bpf_cpumask_clear_cpu, KF_RCU)
  394. BTF_ID_FLAGS(func, bpf_cpumask_test_cpu, KF_RCU)
  395. BTF_ID_FLAGS(func, bpf_cpumask_test_and_set_cpu, KF_RCU)
  396. BTF_ID_FLAGS(func, bpf_cpumask_test_and_clear_cpu, KF_RCU)
  397. BTF_ID_FLAGS(func, bpf_cpumask_setall, KF_RCU)
  398. BTF_ID_FLAGS(func, bpf_cpumask_clear, KF_RCU)
  399. BTF_ID_FLAGS(func, bpf_cpumask_and, KF_RCU)
  400. BTF_ID_FLAGS(func, bpf_cpumask_or, KF_RCU)
  401. BTF_ID_FLAGS(func, bpf_cpumask_xor, KF_RCU)
  402. BTF_ID_FLAGS(func, bpf_cpumask_equal, KF_RCU)
  403. BTF_ID_FLAGS(func, bpf_cpumask_intersects, KF_RCU)
  404. BTF_ID_FLAGS(func, bpf_cpumask_subset, KF_RCU)
  405. BTF_ID_FLAGS(func, bpf_cpumask_empty, KF_RCU)
  406. BTF_ID_FLAGS(func, bpf_cpumask_full, KF_RCU)
  407. BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU)
  408. BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU)
  409. BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU)
  410. BTF_ID_FLAGS(func, bpf_cpumask_weight, KF_RCU)
  411. BTF_KFUNCS_END(cpumask_kfunc_btf_ids)
  412. static const struct btf_kfunc_id_set cpumask_kfunc_set = {
  413. .owner = THIS_MODULE,
  414. .set = &cpumask_kfunc_btf_ids,
  415. };
  416. BTF_ID_LIST(cpumask_dtor_ids)
  417. BTF_ID(struct, bpf_cpumask)
  418. BTF_ID(func, bpf_cpumask_release_dtor)
  419. static int __init cpumask_kfunc_init(void)
  420. {
  421. int ret;
  422. const struct btf_id_dtor_kfunc cpumask_dtors[] = {
  423. {
  424. .btf_id = cpumask_dtor_ids[0],
  425. .kfunc_btf_id = cpumask_dtor_ids[1]
  426. },
  427. };
  428. ret = bpf_mem_alloc_init(&bpf_cpumask_ma, sizeof(struct bpf_cpumask), false);
  429. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &cpumask_kfunc_set);
  430. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &cpumask_kfunc_set);
  431. ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &cpumask_kfunc_set);
  432. return ret ?: register_btf_id_dtor_kfuncs(cpumask_dtors,
  433. ARRAY_SIZE(cpumask_dtors),
  434. THIS_MODULE);
  435. }
  436. late_initcall(cpumask_kfunc_init);