Skip to contentSkip to conversation

Add kfunc for doing pid -> task lookup

PATCH Applied David Vernet 2 participants 4 messages Nov 22, 2022 – Nov 24, 2022

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

cover Add kfunc for doing pid -> task lookup

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(+)
Signature · 1 line
2.38.1
#1 lore raw

PATCH 1/2 bpf: Add bpf_task_from_pid() kfunc

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.

---
 kernel/bpf/helpers.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
kernel/bpf/helpers.c +20 -0
index 9e8a5557ea8d..d5a849d44a35 100644
@@ -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 = {
Signature · 1 line
2.38.1
#2 lore raw

PATCH 2/2 selftests/bpf: Add selftests for bpf_task_from_pid()

In reply to David Vernet

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

---
 .../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(+)
tools/testing/selftests/bpf/prog_tests/task_kfunc.c +4 -0
index 4994fe6092cc..ffd8ef4303c8 100644
@@ -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)
tools/testing/selftests/bpf/progs/task_kfunc_common.h +1 -0
index 160d6dde00be..c0ffd171743e 100644
@@ -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)
 {
tools/testing/selftests/bpf/progs/task_kfunc_failure.c +13 -0
index 93e934ddfcb6..e310473190d5 100644
@@ -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;
+}
tools/testing/selftests/bpf/progs/task_kfunc_success.c +73 -0

76 lines not on this page · raw message

Signature · 1 line
2.38.1
#3 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 10 lines · 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!

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