public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/2] Add kfunc for doing pid -> task lookup
@ 2022-11-22 14:52 David Vernet
  2022-11-22 14:52 ` [PATCH bpf-next 1/2] bpf: Add bpf_task_from_pid() kfunc David Vernet
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: David Vernet @ 2022-11-22 14:52 UTC (permalink / raw)
  To: bpf
  Cc: ast, andrii, daniel, martin.lau, yhs, song, sdf, john.fastabend,
	kpsingh, jolsa, haoluo, tj, kernel-team, linux-kernel

A series of prior patches added support for storing struct task_struct *
objects as kptrs. This patch set proposes extending this with another
kfunc called bpf_task_from_pid() which performs a lookup of a task from
its pid, from the root pid namespace idr.

This allows BPF programs that don't have a kptr to a task, to instead
perform a lookup by pid. This will be useful for programs that are
tracking pids and want, e.g., to do a lookup to find task->comm.

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

 kernel/bpf/helpers.c                          | 20 +++++
 .../selftests/bpf/prog_tests/task_kfunc.c     |  3 +
 .../selftests/bpf/progs/task_kfunc_common.h   |  1 +
 .../selftests/bpf/progs/task_kfunc_success.c  | 73 +++++++++++++++++++
 4 files changed, 97 insertions(+)

-- 
2.38.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf-next 1/2] bpf: Add bpf_task_from_pid() kfunc
  2022-11-22 14:52 [PATCH bpf-next 0/2] Add kfunc for doing pid -> task lookup David Vernet
@ 2022-11-22 14:52 ` David Vernet
  2022-11-22 14:53 ` [PATCH bpf-next 2/2] selftests/bpf: Add selftests for bpf_task_from_pid() David Vernet
  2022-11-24  2:10 ` [PATCH bpf-next 0/2] Add kfunc for doing pid -> task lookup patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: David Vernet @ 2022-11-22 14:52 UTC (permalink / raw)
  To: bpf
  Cc: ast, andrii, daniel, martin.lau, yhs, song, sdf, john.fastabend,
	kpsingh, jolsa, haoluo, tj, kernel-team, linux-kernel

Callers can currently store tasks as kptrs using bpf_task_acquire(),
bpf_task_kptr_get(), and bpf_task_release(). These are useful if a
caller already has a struct task_struct *, but there may be some callers
who only have a pid, and want to look up the associated struct
task_struct * from that to e.g. find task->comm.

This patch therefore adds a new bpf_task_from_pid() kfunc which allows
BPF programs to get a struct task_struct * kptr from a pid.

Signed-off-by: David Vernet <void@manifault.com>
---
 kernel/bpf/helpers.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 9e8a5557ea8d..d5a849d44a35 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1879,6 +1879,25 @@ void bpf_task_release(struct task_struct *p)
 	put_task_struct_rcu_user(p);
 }
 
+/**
+ * bpf_task_from_pid - Find a struct task_struct from its pid by looking it up
+ * in the root pid namespace idr. If a task is returned, it must either be
+ * stored in a map, or released with bpf_task_release().
+ * @pid: The pid of the task being looked up.
+ */
+struct task_struct *bpf_task_from_pid(s32 pid)
+{
+	struct task_struct *p;
+
+	rcu_read_lock();
+	p = find_task_by_pid_ns(pid, &init_pid_ns);
+	if (p)
+		bpf_task_acquire(p);
+	rcu_read_unlock();
+
+	return p;
+}
+
 void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -1904,6 +1923,7 @@ BTF_ID_FLAGS(func, bpf_list_pop_back, KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS)
 BTF_ID_FLAGS(func, bpf_task_kptr_get, KF_ACQUIRE | KF_KPTR_GET | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_task_release, KF_RELEASE)
+BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
 BTF_SET8_END(generic_btf_ids)
 
 static const struct btf_kfunc_id_set generic_kfunc_set = {
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH bpf-next 2/2] selftests/bpf: Add selftests for bpf_task_from_pid()
  2022-11-22 14:52 [PATCH bpf-next 0/2] Add kfunc for doing pid -> task lookup David Vernet
  2022-11-22 14:52 ` [PATCH bpf-next 1/2] bpf: Add bpf_task_from_pid() kfunc David Vernet
@ 2022-11-22 14:53 ` David Vernet
  2022-11-24  2:10 ` [PATCH bpf-next 0/2] Add kfunc for doing pid -> task lookup patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: David Vernet @ 2022-11-22 14:53 UTC (permalink / raw)
  To: bpf
  Cc: ast, andrii, daniel, martin.lau, yhs, song, sdf, john.fastabend,
	kpsingh, jolsa, haoluo, tj, kernel-team, linux-kernel

Add some selftest testcases that validate the expected behavior of the
bpf_task_from_pid() kfunc that was added in the prior patch.

Signed-off-by: David Vernet <void@manifault.com>
---
 .../selftests/bpf/prog_tests/task_kfunc.c     |  4 +
 .../selftests/bpf/progs/task_kfunc_common.h   |  1 +
 .../selftests/bpf/progs/task_kfunc_failure.c  | 13 ++++
 .../selftests/bpf/progs/task_kfunc_success.c  | 73 +++++++++++++++++++
 4 files changed, 91 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/task_kfunc.c b/tools/testing/selftests/bpf/prog_tests/task_kfunc.c
index 4994fe6092cc..ffd8ef4303c8 100644
--- a/tools/testing/selftests/bpf/prog_tests/task_kfunc.c
+++ b/tools/testing/selftests/bpf/prog_tests/task_kfunc.c
@@ -78,6 +78,9 @@ static const char * const success_tests[] = {
 	"test_task_xchg_release",
 	"test_task_get_release",
 	"test_task_current_acquire_release",
+	"test_task_from_pid_arg",
+	"test_task_from_pid_current",
+	"test_task_from_pid_invalid",
 };
 
 static struct {
@@ -99,6 +102,7 @@ static struct {
 	{"task_kfunc_release_fp", "arg#0 pointer type STRUCT task_struct must point"},
 	{"task_kfunc_release_null", "arg#0 is ptr_or_null_ expected ptr_ or socket"},
 	{"task_kfunc_release_unacquired", "release kernel function bpf_task_release expects"},
+	{"task_kfunc_from_pid_no_null_check", "arg#0 is ptr_or_null_ expected ptr_ or socket"},
 };
 
 static void verify_fail(const char *prog_name, const char *expected_err_msg)
diff --git a/tools/testing/selftests/bpf/progs/task_kfunc_common.h b/tools/testing/selftests/bpf/progs/task_kfunc_common.h
index 160d6dde00be..c0ffd171743e 100644
--- a/tools/testing/selftests/bpf/progs/task_kfunc_common.h
+++ b/tools/testing/selftests/bpf/progs/task_kfunc_common.h
@@ -23,6 +23,7 @@ struct hash_map {
 struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
 struct task_struct *bpf_task_kptr_get(struct task_struct **pp) __ksym;
 void bpf_task_release(struct task_struct *p) __ksym;
+struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
 
 static inline struct __tasks_kfunc_map_value *tasks_kfunc_map_value_lookup(struct task_struct *p)
 {
diff --git a/tools/testing/selftests/bpf/progs/task_kfunc_failure.c b/tools/testing/selftests/bpf/progs/task_kfunc_failure.c
index 93e934ddfcb6..e310473190d5 100644
--- a/tools/testing/selftests/bpf/progs/task_kfunc_failure.c
+++ b/tools/testing/selftests/bpf/progs/task_kfunc_failure.c
@@ -258,3 +258,16 @@ int BPF_PROG(task_kfunc_release_unacquired, struct task_struct *task, u64 clone_
 
 	return 0;
 }
+
+SEC("tp_btf/task_newtask")
+int BPF_PROG(task_kfunc_from_pid_no_null_check, struct task_struct *task, u64 clone_flags)
+{
+	struct task_struct *acquired;
+
+	acquired = bpf_task_from_pid(task->pid);
+
+	/* Releasing bpf_task_from_pid() lookup without a NULL check. */
+	bpf_task_release(acquired);
+
+	return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/task_kfunc_success.c b/tools/testing/selftests/bpf/progs/task_kfunc_success.c
index be4534b5ba2e..60c7ead41cfc 100644
--- a/tools/testing/selftests/bpf/progs/task_kfunc_success.c
+++ b/tools/testing/selftests/bpf/progs/task_kfunc_success.c
@@ -147,3 +147,76 @@ int BPF_PROG(test_task_current_acquire_release, struct task_struct *task, u64 cl
 
 	return 0;
 }
+
+static void lookup_compare_pid(const struct task_struct *p)
+{
+	struct task_struct *acquired;
+
+	acquired = bpf_task_from_pid(p->pid);
+	if (!acquired) {
+		err = 1;
+		return;
+	}
+
+	if (acquired->pid != p->pid)
+		err = 2;
+	bpf_task_release(acquired);
+}
+
+SEC("tp_btf/task_newtask")
+int BPF_PROG(test_task_from_pid_arg, struct task_struct *task, u64 clone_flags)
+{
+	struct task_struct *acquired;
+
+	if (!is_test_kfunc_task())
+		return 0;
+
+	lookup_compare_pid(task);
+	return 0;
+}
+
+SEC("tp_btf/task_newtask")
+int BPF_PROG(test_task_from_pid_current, struct task_struct *task, u64 clone_flags)
+{
+	struct task_struct *current, *acquired;
+
+	if (!is_test_kfunc_task())
+		return 0;
+
+	lookup_compare_pid(bpf_get_current_task_btf());
+	return 0;
+}
+
+static int is_pid_lookup_valid(s32 pid)
+{
+	struct task_struct *acquired;
+
+	acquired = bpf_task_from_pid(pid);
+	if (acquired) {
+		bpf_task_release(acquired);
+		return 1;
+	}
+
+	return 0;
+}
+
+SEC("tp_btf/task_newtask")
+int BPF_PROG(test_task_from_pid_invalid, struct task_struct *task, u64 clone_flags)
+{
+	struct task_struct *acquired;
+
+	if (!is_test_kfunc_task())
+		return 0;
+
+	if (is_pid_lookup_valid(-1)) {
+		err = 1;
+		return 0;
+	}
+
+	if (is_pid_lookup_valid(0xcafef00d)) {
+		err = 2;
+		return 0;
+	}
+
+	return 0;
+}
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next 0/2] Add kfunc for doing pid -> task lookup
  2022-11-22 14:52 [PATCH bpf-next 0/2] Add kfunc for doing pid -> task lookup David Vernet
  2022-11-22 14:52 ` [PATCH bpf-next 1/2] bpf: Add bpf_task_from_pid() kfunc David Vernet
  2022-11-22 14:53 ` [PATCH bpf-next 2/2] selftests/bpf: Add selftests for bpf_task_from_pid() David Vernet
@ 2022-11-24  2:10 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-11-24  2:10 UTC (permalink / raw)
  To: David Vernet
  Cc: bpf, ast, andrii, daniel, martin.lau, yhs, song, sdf,
	john.fastabend, kpsingh, jolsa, haoluo, tj, kernel-team,
	linux-kernel

Hello:

This series was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Tue, 22 Nov 2022 08:52:58 -0600 you wrote:
> A series of prior patches added support for storing struct task_struct *
> objects as kptrs. This patch set proposes extending this with another
> kfunc called bpf_task_from_pid() which performs a lookup of a task from
> its pid, from the root pid namespace idr.
> 
> This allows BPF programs that don't have a kptr to a task, to instead
> perform a lookup by pid. This will be useful for programs that are
> tracking pids and want, e.g., to do a lookup to find task->comm.
> 
> [...]

Here is the summary with links:
  - [bpf-next,1/2] bpf: Add bpf_task_from_pid() kfunc
    https://git.kernel.org/bpf/bpf-next/c/3f0e6f2b41d3
  - [bpf-next,2/2] selftests/bpf: Add selftests for bpf_task_from_pid()
    https://git.kernel.org/bpf/bpf-next/c/f471748b7fe5

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-11-24  2:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-22 14:52 [PATCH bpf-next 0/2] Add kfunc for doing pid -> task lookup David Vernet
2022-11-22 14:52 ` [PATCH bpf-next 1/2] bpf: Add bpf_task_from_pid() kfunc David Vernet
2022-11-22 14:53 ` [PATCH bpf-next 2/2] selftests/bpf: Add selftests for bpf_task_from_pid() David Vernet
2022-11-24  2:10 ` [PATCH bpf-next 0/2] Add kfunc for doing pid -> task lookup patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox