Skip to contentSkip to conversation

Add new bpf_cpumask_weight() kfunc

PATCH Applied David Vernet 3 participants 6 messages Dec 7, 2023 – Dec 10, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover Add new bpf_cpumask_weight() kfunc

It can be useful to query how many bits are set in a cpumask. For example, if you want to perform special logic for the last remaining core that's set in a mask. This logic is already exposed through the main kernel's cpumask header as cpumask_weight(), so it would be useful to add a new bpf_cpumask_weight() kfunc which wraps it and does the same.

This patch series was built and tested on top of commit 2146f7fe6e02 ("Merge branch 'allocate-bpf-trampoline-on-bpf_prog_pack'").

David Vernet (2):
bpf: Add bpf_cpumask_weight() kfunc selftests/bpf: Add test for bpf_cpumask_weight() kfunc

 Documentation/bpf/cpumasks.rst                |  2 +-
 kernel/bpf/cpumask.c                          | 12 ++++++
 .../selftests/bpf/prog_tests/cpumask.c        |  1 +
 .../selftests/bpf/progs/cpumask_common.h      |  1 +
 .../selftests/bpf/progs/cpumask_success.c     | 43 +++++++++++++++++++
 5 files changed, 58 insertions(+), 1 deletion(-)
Signature · 1 line
2.42.1
#1 lore raw

PATCH 1/2 bpf: Add bpf_cpumask_weight() kfunc

It can be useful to query how many bits are set in a cpumask. For example, if you want to perform special logic for the last remaining core that's set in a mask. Let's therefore add a new bpf_cpumask_weight() kfunc which checks how many bits are set in a mask.

---
 Documentation/bpf/cpumasks.rst |  2 +-
 kernel/bpf/cpumask.c           | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
Documentation/bpf/cpumasks.rst +1 -1
index a22b6ad105fb..b5d47a04da5d 100644
@@ -352,7 +352,7 @@ can be used to query the contents of cpumasks.
 
 .. kernel-doc:: kernel/bpf/cpumask.c
    :identifiers: bpf_cpumask_first bpf_cpumask_first_zero bpf_cpumask_first_and
-                 bpf_cpumask_test_cpu
+                 bpf_cpumask_test_cpu bpf_cpumask_weight
 
 .. kernel-doc:: kernel/bpf/cpumask.c
    :identifiers: bpf_cpumask_equal bpf_cpumask_intersects bpf_cpumask_subset
kernel/bpf/cpumask.c +12 -0
index e01c741e54e7..7499b7d8c06f 100644
@@ -405,6 +405,17 @@ __bpf_kfunc u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1,
 	return cpumask_any_and_distribute(src1, src2);
 }
 
+/**
+ * bpf_cpumask_weight() - Return the number of bits in @cpumask.
+ * @cpumask: The cpumask being queried.
+ *
+ * Count the number of set bits in the given cpumask.
+ */
+__bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask)
+{
+	return cpumask_weight(cpumask);
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_SET8_START(cpumask_kfunc_btf_ids)
@@ -432,6 +443,7 @@ BTF_ID_FLAGS(func, bpf_cpumask_full, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU)
 BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU)
+BTF_ID_FLAGS(func, bpf_cpumask_weight, KF_RCU)
 BTF_SET8_END(cpumask_kfunc_btf_ids)
 
 static const struct btf_kfunc_id_set cpumask_kfunc_set = {
Signature · 1 line
2.42.1
reviewer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH bpf-next 1/2] bpf: Add bpf_cpumask_weight() kfunc

Quoted 6 lines · David Vernet

It can be useful to query how many bits are set in a cpumask. For example, if you want to perform special logic for the last remaining core that's set in a mask. Let's therefore add a new bpf_cpumask_weight() kfunc which checks how many bits are set in a mask.

#3 lore raw

PATCH 2/2 selftests/bpf: Add test for bpf_cpumask_weight() kfunc

In reply to David Vernet

The new bpf_cpumask_weight() kfunc can be used to count the number of bits that are set in a struct cpumask* kptr. Let's add a selftest to verify its behavior.

---
 .../selftests/bpf/prog_tests/cpumask.c        |  1 +
 .../selftests/bpf/progs/cpumask_common.h      |  1 +
 .../selftests/bpf/progs/cpumask_success.c     | 43 +++++++++++++++++++
 3 files changed, 45 insertions(+)
tools/testing/selftests/bpf/prog_tests/cpumask.c +1 -0
index 756ea8b590b6..c2e886399e3c 100644
@@ -18,6 +18,7 @@ static const char * const cpumask_success_testcases[] = {
 	"test_insert_leave",
 	"test_insert_remove_release",
 	"test_global_mask_rcu",
+	"test_cpumask_weight",
 };
 
 static void verify_success(const char *prog_name)
tools/testing/selftests/bpf/progs/cpumask_common.h +1 -0
index b15c588ace15..0cd4aebb97cf 100644
@@ -54,6 +54,7 @@ bool bpf_cpumask_full(const struct cpumask *cpumask) __ksym;
 void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym;
 u32 bpf_cpumask_any_distribute(const struct cpumask *src) __ksym;
 u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1, const struct cpumask *src2) __ksym;
+u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym;
 
 void bpf_rcu_read_lock(void) __ksym;
 void bpf_rcu_read_unlock(void) __ksym;
tools/testing/selftests/bpf/progs/cpumask_success.c +43 -0

49 lines not on this page · raw message

Signature · 1 line
2.42.1
#5 lore raw
In reply to David Vernet
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 8 lines · On Thu, 7 Dec 2023 15:08:41 -0600 you wrote:

It can be useful to query how many bits are set in a cpumask. For example, if you want to perform special logic for the last remaining core that's set in a mask. This logic is already exposed through the main kernel's cpumask header as cpumask_weight(), so it would be useful to add a new bpf_cpumask_weight() kfunc which wraps it and does the same.

[...]

Here is the summary with links:
- [bpf-next,1/2] bpf: Add bpf_cpumask_weight() kfunc

    https://git.kernel.org/bpf/bpf-next/c/a6de18f310a5
  - [bpf-next,2/2] selftests/bpf: Add test for bpf_cpumask_weight() kfunc
    https://git.kernel.org/bpf/bpf-next/c/88f6047191e6

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html