diff mbox series

[bpf-next,3/4] bpf: Add new kfunc bpf_cpumask_set_from_pid

Message ID 20231222113102.4148-4-laoar.shao@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf: Add bpf_iter_cpumask | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test
bpf/vmtest-bpf-next-VM_Test-9 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-8 fail Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for s390x-gcc / test
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-12 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-13 fail Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-15 success Logs for x86_64-gcc / test
bpf/vmtest-bpf-next-VM_Test-16 success Logs for x86_64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-17 fail Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-18 fail Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-llvm-17 / test
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-21 fail Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-llvm-18 / test
bpf/vmtest-bpf-next-VM_Test-22 fail Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-llvm-18 / veristat
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/ynl success SINGLE THREAD; Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 1146 this patch: 1147
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/build_clang success Errors and warnings before: 1143 this patch: 1143
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 1173 this patch: 1174
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 29 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Yafang Shao Dec. 22, 2023, 11:31 a.m. UTC
Introducing a new kfunc: bpf_cpumask_set_from_pid. This function serves the
purpose of retrieving the cpumask associated with a specific PID. Its
utility is particularly evident within container environments. For
instance, it allows for extracting the cpuset of a container using the
init task within it.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 kernel/bpf/cpumask.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

Comments

Tejun Heo Dec. 22, 2023, 5:51 p.m. UTC | #1
Hello,

On Fri, Dec 22, 2023 at 11:31:01AM +0000, Yafang Shao wrote:
> Introducing a new kfunc: bpf_cpumask_set_from_pid. This function serves the
> purpose of retrieving the cpumask associated with a specific PID. Its
> utility is particularly evident within container environments. For
> instance, it allows for extracting the cpuset of a container using the
> init task within it.
> 
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
...
> +__bpf_kfunc bool bpf_cpumask_set_from_pid(struct cpumask *cpumask, u32 pid)
> +{
> +	struct task_struct *task;
> +
> +	if (!cpumask)
> +		return false;
> +
> +	task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
> +	if (!task)
> +		return false;
> +
> +	cpumask_copy(cpumask, task->cpus_ptr);
> +	put_task_struct(task);
> +	return true;
> +}

This seems awfully specific. Why is this necessary? Shouldn't the BPF prog
get the task and bpf_cpumask_copy() its ->cpus_ptr instead?

Thanks.
Yafang Shao Dec. 24, 2023, 3:05 a.m. UTC | #2
On Sat, Dec 23, 2023 at 1:51 AM Tejun Heo <tj@kernel.org> wrote:
>
> Hello,
>
> On Fri, Dec 22, 2023 at 11:31:01AM +0000, Yafang Shao wrote:
> > Introducing a new kfunc: bpf_cpumask_set_from_pid. This function serves the
> > purpose of retrieving the cpumask associated with a specific PID. Its
> > utility is particularly evident within container environments. For
> > instance, it allows for extracting the cpuset of a container using the
> > init task within it.
> >
> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> ...
> > +__bpf_kfunc bool bpf_cpumask_set_from_pid(struct cpumask *cpumask, u32 pid)
> > +{
> > +     struct task_struct *task;
> > +
> > +     if (!cpumask)
> > +             return false;
> > +
> > +     task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
> > +     if (!task)
> > +             return false;
> > +
> > +     cpumask_copy(cpumask, task->cpus_ptr);
> > +     put_task_struct(task);
> > +     return true;
> > +}
>
> This seems awfully specific. Why is this necessary? Shouldn't the BPF prog
> get the task and bpf_cpumask_copy() its ->cpus_ptr instead?

Good point. I missed the bpf_cpumask_copy().  Will use it instead.
diff mbox series

Patch

diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c
index 4ae07a4..5755bb6 100644
--- a/kernel/bpf/cpumask.c
+++ b/kernel/bpf/cpumask.c
@@ -467,6 +467,22 @@  __bpf_kfunc void bpf_iter_cpumask_destroy(struct bpf_iter_cpumask *it)
 	bpf_mem_free(&bpf_global_ma, kit->cpu);
 }
 
+__bpf_kfunc bool bpf_cpumask_set_from_pid(struct cpumask *cpumask, u32 pid)
+{
+	struct task_struct *task;
+
+	if (!cpumask)
+		return false;
+
+	task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
+	if (!task)
+		return false;
+
+	cpumask_copy(cpumask, task->cpus_ptr);
+	put_task_struct(task);
+	return true;
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_SET8_START(cpumask_kfunc_btf_ids)
@@ -498,6 +514,7 @@  __bpf_kfunc void bpf_iter_cpumask_destroy(struct bpf_iter_cpumask *it)
 BTF_ID_FLAGS(func, bpf_iter_cpumask_new, KF_ITER_NEW | KF_RCU)
 BTF_ID_FLAGS(func, bpf_iter_cpumask_next, KF_ITER_NEXT | KF_RET_NULL | KF_RCU)
 BTF_ID_FLAGS(func, bpf_iter_cpumask_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_cpumask_set_from_pid, KF_RCU)
 BTF_SET8_END(cpumask_kfunc_btf_ids)
 
 static const struct btf_kfunc_id_set cpumask_kfunc_set = {