Enable RCU semantics for task kptrs
Subsystems (5)
cover Enable RCU semantics for task kptrs
In commit 22df776a9a86 ("tasks: Extract rcu_users out of union"), the 'refcount_t rcu_users' field was extracted out of a union with the 'struct rcu_head rcu' field. This allows us to use the field for refcounting struct task_struct with RCU protection, as the RCU callback no longer flips rcu_users to be nonzero after the callback is scheduled.
This patch set leverages this to do a few things:
1. Marks struct task_struct as RCU safe in the verifier, allowing referenced kptr tasks stored in maps to be accessed in an RCU read region without acquiring a reference (with just a NULL check).
2. Makes bpf_task_acquire() a KF_ACQUIRE | KF_RCU | KF_RET_NULL kfunc.
3. Removes bpf_task_kptr_get() and bpf_task_acquire_not_zero(), as they're now redundant with the above two changes.
4. Updates selftests and documentation accordingly.
David Vernet (3):
bpf: Make struct task_struct an RCU-safe type
bpf: Remove now-defunct task kfuncs
bpf,docs: Update documentation to reflect new task kfuncs
Documentation/bpf/kfuncs.rst | 49 ++++++- kernel/bpf/helpers.c | 78 +---------- kernel/bpf/verifier.c | 1 + .../selftests/bpf/prog_tests/task_kfunc.c | 4 +- .../selftests/bpf/progs/rcu_read_lock.c | 9 +- .../selftests/bpf/progs/task_kfunc_common.h | 6 +- .../selftests/bpf/progs/task_kfunc_failure.c | 126 ++++++++---------- .../selftests/bpf/progs/task_kfunc_success.c | 76 +++++++++-- 8 files changed, 174 insertions(+), 175 deletions(-)
Signature · 1 line
2.39.0
PATCH 1/3 bpf: Make struct task_struct an RCU-safe type
struct task_struct objects are a bit interesting in terms of how their lifetime is protected by refcounts. task structs have two refcount fields:
1. refcount_t usage: Protects the memory backing the task struct. When this refcount drops to 0, the task is immediately freed, without waiting for an RCU grace period to elapse. This is the field that most callers in the kernel currently use to ensure that a task remains valid while it's being referenced, and is what's currently tracked with bpf_task_acquire() and bpf_task_release().
2. refcount_t rcu_users: A refcount field which, when it drops to 0, schedules an RCU callback that drops a reference held on the 'usage' field above (which is acquired when the task is first created). This field therefore provides a form of RCU protection on the task by ensuring that at least one 'usage' refcount will be held until an RCU grace period has elapsed. The qualifier "a form of" is important here, as a task can remain valid after task->rcu_users has dropped to 0 and the subsequent RCU gp has elapsed.
In terms of BPF, we want to use task->rcu_users to protect tasks that function as referenced kptrs, and to allow tasks stored as referenced kptrs in maps to be accessed with RCU protection.
Let's first determine whether we can safely use task->rcu_users to protect tasks stored in maps. All of the bpf_task* kfuncs can only be called from tracepoint, struct_ops, or BPF_PROG_TYPE_SCHED_CLS, program types. For tracepoint and struct_ops programs, the struct task_struct passed to a program handler will always be trusted, so it will always be safe to call bpf_task_acquire() with any task passed to a program. Note, however, that we must update bpf_task_acquire() to be KF_RET_NULL, as it is possible that the task has exited by the time the program is invoked, even if the pointer is still currently valid because the main kernel holds a task->usage refcount. For BPF_PROG_TYPE_SCHED_CLS, tasks should never be passed as an argument to the any program handlers, so it should not be relevant.
The second question is whether it's safe to use RCU to access a task that was acquired with bpf_task_acquire(), and stored in a map. Because bpf_task_acquire() now uses task->rcu_users, it follows that if the task is present in the map, that it must have had at least one task->rcu_users refcount by the time the current RCU cs was started. Therefore, it's safe to access that task until the end of the current RCU cs.
With all that said, this patch makes struct task_struct is an RCU-protected object. In doing so, we also change bpf_task_acquire() to be KF_ACQUIRE | KF_RCU | KF_RET_NULL, and adjust any selftests as necessary. A subsequent patch will remove bpf_task_kptr_get(), and bpf_task_acquire_not_zero() respectively.
--- kernel/bpf/helpers.c | 11 ++- kernel/bpf/verifier.c | 1 + .../selftests/bpf/prog_tests/task_kfunc.c | 2 + .../selftests/bpf/progs/task_kfunc_common.h | 5 + .../selftests/bpf/progs/task_kfunc_failure.c | 98 +++++++++++++++++-- .../selftests/bpf/progs/task_kfunc_success.c | 52 +++++++++- 6 files changed, 153 insertions(+), 16 deletions(-)
kernel/bpf/helpers.c +7 -4
@@ -18,6 +18,7 @@#include <linux/pid_namespace.h> #include <linux/poison.h> #include <linux/proc_ns.h> +#include <linux/sched/task.h> #include <linux/security.h> #include <linux/btf_ids.h> #include <linux/bpf_mem_alloc.h>
@@ -2013,7 +2014,9 @@ __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root)*/ __bpf_kfunc struct task_struct *bpf_task_acquire(struct task_struct *p) { -return get_task_struct (p); +if (refcount_inc_not_zero (&p->rcu_users)) +return p; +return NULL ; }/**
@@ -2089,7 +2092,7 @@ __bpf_kfunc struct task_struct *bpf_task_kptr_get(struct task_struct **pp)*/ __bpf_kfunc void bpf_task_release(struct task_struct *p) { -put_task_struct (p); +put_task_struct_rcu_user (p); }#ifdef CONFIG_CGROUPS
@@ -2199,7 +2202,7 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)rcu_read_lock (); p =find_task_by_pid_ns (pid, &init_pid_ns);if (p) -bpf_task_acquire (p); + p =bpf_task_acquire (p);rcu_read_unlock ();return p;
@@ -2371,7 +2374,7 @@ BTF_ID_FLAGS(func, bpf_list_push_front) BTF_ID_FLAGS(func, bpf_list_push_back) BTF_ID_FLAGS(func, bpf_list_pop_front, KF_ACQUIRE | KF_RET_NULL) 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_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_task_acquire_not_zero, KF_ACQUIRE | KF_RCU | KF_RET_NULL) 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 )
kernel/bpf/verifier.c +1 -0
@@ -4600,6 +4600,7 @@ BTF_SET_START(rcu_protected_types) BTF_ID(struct, prog_test_ref_kfunc) BTF_ID(struct, cgroup) BTF_ID(struct, bpf_cpumask) +BTF_ID(struct, task_struct)BTF_SET_END (rcu_protected_types) static bool rcu_protected_object(const struct btf *btf, u32 btf_id)
tools/testing/selftests/bpf/prog_tests/task_kfunc.c +2 -0
@@ -78,6 +78,8 @@ static const char * const success_tests[] = {"test_task_from_pid_arg" ,"test_task_from_pid_current" ,"test_task_from_pid_invalid" , +"task_kfunc_acquire_trusted_walked" , + "task_kfunc_acquire_untrusted_walked_null_check", };void test_task_kfunc (void )
tools/testing/selftests/bpf/progs/task_kfunc_common.h +5 -0
@@ -24,6 +24,8 @@ 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; +void bpf_rcu_read_lock(void) __ksym; +void bpf_rcu_read_unlock(void) __ksym;static inline struct __tasks_kfunc_map_value *tasks_kfunc_map_value_lookup (struct task_struct *p) {
@@ -60,6 +62,9 @@ static inline int tasks_kfunc_map_insert(struct task_struct *p) } acquired =bpf_task_acquire (p); +if (!acquired) +return -ENOENT ; + old =bpf_kptr_xchg (&v->task, acquired);if (old) {bpf_task_release (old);
tools/testing/selftests/bpf/progs/task_kfunc_failure.c +88 -10
165 lines not on this page · raw message
tools/testing/selftests/bpf/progs/task_kfunc_success.c +50 -2
67 lines not on this page · raw message
Signature · 1 line
2.39.0
Re: [PATCH bpf-next 1/3] bpf: Make struct task_struct an RCU-safe type
Quoted 7 lines · David Vernet
kernel/bpf/helpers.c | 11 ++- kernel/bpf/verifier.c | 1 + .../selftests/bpf/prog_tests/task_kfunc.c | 2 + .../selftests/bpf/progs/task_kfunc_common.h | 5 + .../selftests/bpf/progs/task_kfunc_failure.c | 98 +++++++++++++++++-- .../selftests/bpf/progs/task_kfunc_success.c | 52 +++++++++- 6 files changed, 153 insertions(+), 16 deletions(-)
See CI failures on gcc compiled kernel:
https://github.com/kernel-patches/bpf/actions/runs/4570493668/jobs/8068004031
kernel/bpf/helpers.c __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root) · line 2020
Quoted 7 lines
__bpf_kfuncstructtask_struct*bpf_task_acquire(structtask_struct*p) {kernel/bpf/helpers.c +3
-1-return get_task_struct (p); +if (refcount_inc_not_zero (&p->rcu_users)) +return p; +return NULL ; }
I wonder whether we should add a bit of safety net here. Like do not allow acquire of tasks with PF_KTHREAD | PF_EXITING or at least is_idle_task ?
Re: [PATCH bpf-next 1/3] bpf: Make struct task_struct an RCU-safe type
Quoted 11 lines · Alexei Starovoitov
Quoted 7 lines · David Vernet
kernel/bpf/helpers.c | 11 ++- kernel/bpf/verifier.c | 1 + .../selftests/bpf/prog_tests/task_kfunc.c | 2 + .../selftests/bpf/progs/task_kfunc_common.h | 5 + .../selftests/bpf/progs/task_kfunc_failure.c | 98 +++++++++++++++++-- .../selftests/bpf/progs/task_kfunc_success.c | 52 +++++++++- 6 files changed, 153 insertions(+), 16 deletions(-)See CI failures on gcc compiled kernel:
https://github.com/kernel-patches/bpf/actions/runs/4570493668/jobs/8068004031
Thanks for the heads up, I'll take a look and resubmit v2 with fixes for gcc. In general it seems like a good idea to test both gcc and clang selftest builds; I'll do that from now on.
Quoted 10 lines
kernel/bpf/helpers.c __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root) · line 2020
Quoted 7 lines
__bpf_kfuncstructtask_struct*bpf_task_acquire(structtask_struct*p) {kernel/bpf/helpers.c +3
-1-return get_task_struct (p); +if (refcount_inc_not_zero (&p->rcu_users)) +return p; +return NULL ; }I wonder whether we should add a bit of safety net here. Like do not allow acquire of tasks with
PF_KTHREAD|PF_EXITING
That's certainly an option, though I don't think it buys us much. It doesn't prevent the task from being pinned if it's acquired a bit earlier, and others in the kernel can acquire a task with get_task_struct() regardless of whether it's PF_EXITING (or an idle task, etc). IMO it's a better UX to provide a complementary API to get_task_struct(), but with RCU protection. On the other hand, it's already KF_RET_NULL, and I doubt needing to acquire a task that's PF_EXITING would be a common occurrence. We could always go the more restrictive route, and then loosen it if there's a valid use case? My only concern is that this safety net arguably doesn't really protect us from anything (given that you can just acquire the task before it's exiting), but maybe I'm wrong about that.
Quoted 1 line
or at least
is_idle_task?
Hmm, this one I'm really not sure about. On the one hand I can't think of a reason why anyone would need to acquire a reference to an idle task. On the other hand, it seems pretty benign to pin an idle task. I guess my sentiment is the same as above. I'm fine with adding a restriction and then loosening it later if there's a valid reason (and I can add a comment explaining this).
Re: [PATCH bpf-next 1/3] bpf: Make struct task_struct an RCU-safe type
Quoted 42 lines · David Vernet
Quoted 11 lines · Alexei Starovoitov
Quoted 7 lines · David Vernet
kernel/bpf/helpers.c | 11 ++- kernel/bpf/verifier.c | 1 + .../selftests/bpf/prog_tests/task_kfunc.c | 2 + .../selftests/bpf/progs/task_kfunc_common.h | 5 + .../selftests/bpf/progs/task_kfunc_failure.c | 98 +++++++++++++++++-- .../selftests/bpf/progs/task_kfunc_success.c | 52 +++++++++- 6 files changed, 153 insertions(+), 16 deletions(-)See CI failures on gcc compiled kernel:
https://github.com/kernel-patches/bpf/actions/runs/4570493668/jobs/8068004031Thanks for the heads up, I'll take a look and resubmit v2 with fixes for gcc. In general it seems like a good idea to test both gcc and clang selftest builds; I'll do that from now on.
Quoted 10 lines
kernel/bpf/helpers.c __bpf_kfunc struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root) · line 2020
Quoted 7 lines
__bpf_kfuncstructtask_struct*bpf_task_acquire(structtask_struct*p) {kernel/bpf/helpers.c +3
-1-return get_task_struct (p); +if (refcount_inc_not_zero (&p->rcu_users)) +return p; +return NULL ; }I wonder whether we should add a bit of safety net here. Like do not allow acquire of tasks with
PF_KTHREAD|PF_EXITINGThat's certainly an option, though I don't think it buys us much. It doesn't prevent the task from being pinned if it's acquired a bit earlier, and others in the kernel can acquire a task with
get_task_struct()regardless of whether it'sPF_EXITING(or an idle task, etc). IMO it's a better UX to provide a complementary API toget_task_struct(), but with RCU protection. On the other hand, it's alreadyKF_RET_NULL, and I doubt needing to acquire a task that'sPF_EXITINGwould be a common occurrence. We could always go the more restrictive route, and then loosen it if there's a valid use case? My only concern is that this safety net arguably doesn't really protect us from anything (given that you can just acquire the task before it's exiting), but maybe I'm wrong about that.
Quoted 8 lines
Quoted 1 line
or at least
is_idle_task?Hmm, this one I'm really not sure about. On the one hand I can't think of a reason why anyone would need to acquire a reference to an idle task. On the other hand, it seems pretty benign to pin an idle task. I guess my sentiment is the same as above. I'm fine with adding a restriction and then loosening it later if there's a valid reason (and I can add a comment explaining this).
Good point about pinning earlier. Let's keep it as-is then. bpf prog can do such checks on its own if it needs to.
PATCH 2/3 bpf: Remove now-defunct task kfuncs
In reply to David VernetIn commit 22df776a9a86 ("tasks: Extract rcu_users out of union"), the 'refcount_t rcu_users' field was extracted out of a union with the 'struct rcu_head rcu' field. This allows us to safely perform a refcount_inc_not_zero() on task->rcu_users when acquiring a reference on a task struct. A prior patch leveraged this by making struct task_struct an RCU-protected object in the verifier, and by bpf_task_acquire() to use the task->rcu_users field for synchronization.
Now that we can use RCU to protect tasks, we no longer need bpf_task_kptr_get(), or bpf_task_acquire_not_zero(). bpf_task_kptr_get() is truly completely unnecessary, as we can just use RCU to get the object. bpf_task_acquire_not_zero() is now equivalent to bpf_task_acquire().
In addition to these changes, this patch also updates the associated selftests to no longer use these kfuncs.
--- kernel/bpf/helpers.c | 69 ------------------ .../selftests/bpf/prog_tests/task_kfunc.c | 2 +- .../selftests/bpf/progs/rcu_read_lock.c | 9 +-- .../selftests/bpf/progs/task_kfunc_common.h | 1 - .../selftests/bpf/progs/task_kfunc_failure.c | 73 ------------------- .../selftests/bpf/progs/task_kfunc_success.c | 22 +++--- 6 files changed, 14 insertions(+), 162 deletions(-)
kernel/bpf/helpers.c +0 -69
@@ -2019,73 +2019,6 @@ __bpf_kfunc struct task_struct *bpf_task_acquire(struct task_struct *p)return NULL ; } -/** -* bpf_task_acquire_not_zero - Acquire a reference to a rcu task object. A task -* acquired by this kfunc which is not stored in a map as a kptr, must be -* released by calling bpf_task_release(). -* @p: The task on which a reference is being acquired. -*/ -__bpf_kfunc struct task_struct *bpf_task_acquire_not_zero(struct task_struct *p) -{ -/* For the time being this function returns NULL, as it's not currently -* possible to safely acquire a reference to a task with RCU protection -* using get_task_struct() and put_task_struct(). This is due to the -* slightly odd mechanics of p->rcu_users, and how task RCU protection -* works. -* -* A struct task_struct is refcounted by two different refcount_t -* fields: -* -* 1. p->usage: The "true" refcount field which tracks a task's -* lifetime. The task is freed as soon as this -* refcount drops to 0. -* -* 2. p->rcu_users: An "RCU users" refcount field which is statically -* initialized to 2, and is co-located in a union with -* a struct rcu_head field (p->rcu). p->rcu_users -* essentially encapsulates a single p->usage -* refcount, and when p->rcu_users goes to 0, an RCU -* callback is scheduled on the struct rcu_head which -* decrements the p->usage refcount. -* -* There are two important implications to this task refcounting logic -* described above. The first is that -* refcount_inc_not_zero(&p->rcu_users) cannot be used anywhere, as -* after the refcount goes to 0, the RCU callback being scheduled will -* cause the memory backing the refcount to again be nonzero due to the -* fields sharing a union. The other is that we can't rely on RCU to -* guarantee that a task is valid in a BPF program. This is because a -* task could have already transitioned to being in the TASK_DEAD -* state, had its rcu_users refcount go to 0, and its rcu callback -* invoked in which it drops its single p->usage reference. At this -* point the task will be freed as soon as the last p->usage reference -* goes to 0, without waiting for another RCU gp to elapse. The only -* way that a BPF program can guarantee that a task is valid is in this -* scenario is to hold a p->usage refcount itself. -* -* Until we're able to resolve this issue, either by pulling -* p->rcu_users and p->rcu out of the union, or by getting rid of -* p->usage and just using p->rcu_users for refcounting, we'll just -* return NULL here. -*/ -return NULL ; -} - -/** -* bpf_task_kptr_get - Acquire a reference on a struct task_struct kptr. A task -* kptr acquired by this kfunc which is not subsequently stored in a map, must -* be released by calling bpf_task_release(). -* @pp: A pointer to a task kptr on which a reference is being acquired. -*/ -__bpf_kfunc struct task_struct *bpf_task_kptr_get(struct task_struct **pp) -{ -/* We must return NULL here until we have clarity on how to properly -* leverage RCU for ensuring a task's lifetime. See the comment above -* in bpf_task_acquire_not_zero() for more details. -*/ -return NULL ; -} -/** * bpf_task_release - Release the reference acquired on a task. * @p: The task on which a reference is being released.
@@ -2375,8 +2308,6 @@ BTF_ID_FLAGS(func, bpf_list_push_back) BTF_ID_FLAGS(func, bpf_list_pop_front, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_list_pop_back, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) -BTF_ID_FLAGS(func, bpf_task_acquire_not_zero, KF_ACQUIRE | KF_RCU | KF_RET_NULL) -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_rbtree_remove, KF_ACQUIRE)BTF_ID_FLAGS (func, bpf_rbtree_add)
tools/testing/selftests/bpf/prog_tests/task_kfunc.c +1 -1
@@ -73,7 +73,7 @@ static const char * const success_tests[] = {"test_task_acquire_release_current" ,"test_task_acquire_leave_in_map" ,"test_task_xchg_release" , -"test_task_get_release" , +"test_task_map_acquire_release" ,"test_task_current_acquire_release" ,"test_task_from_pid_arg" , "test_task_from_pid_current",
tools/testing/selftests/bpf/progs/rcu_read_lock.c +2 -7
@@ -23,7 +23,7 @@ struct bpf_key *bpf_lookup_user_key(__u32 serial, __u64 flags) __ksym; void bpf_key_put(struct bpf_key *key) __ksym; void bpf_rcu_read_lock(void) __ksym; void bpf_rcu_read_unlock(void) __ksym; -struct task_struct *bpf_task_acquire_not_zero (struct task_struct *p) __ksym; +struct task_struct *bpf_task_acquire (struct task_struct *p) __ksym; void bpf_task_release(struct task_struct *p) __ksym;SEC ("?fentry.s/" SYS_PREFIX "sys_getpgid" )
@@ -159,13 +159,8 @@ int task_acquire(void *ctx) goto out;/* acquire a reference which can be used outside rcu read lock region */ - gparent =bpf_task_acquire_not_zero (gparent); + gparent =bpf_task_acquire (gparent);if (!gparent) -/* Until we resolve the issues with using task->rcu_users, we -* expect bpf_task_acquire_not_zero() to return a NULL task. -* See the comment at the definition of -* bpf_task_acquire_not_zero() for more details. -*/ goto out; (void )bpf_task_storage_get (&map_a, gparent,0 ,0 );
tools/testing/selftests/bpf/progs/task_kfunc_common.h +0 -1
@@ -21,7 +21,6 @@ struct hash_map { } __tasks_kfunc_map SEC(".maps");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; void bpf_rcu_read_lock(void) __ksym;
tools/testing/selftests/bpf/progs/task_kfunc_failure.c +0 -73
85 lines not on this page · raw message
tools/testing/selftests/bpf/progs/task_kfunc_success.c +11 -11
@@ -122,7 +122,7 @@ int BPF_PROG(test_task_xchg_release, struct task_struct *task, u64 clone_flags) } SEC("tp_btf/task_newtask") -int BPF_PROG (test_task_get_release ,struct task_struct *task,u64 clone_flags) +int BPF_PROG (test_task_map_acquire_release ,struct task_struct *task,u64 clone_flags) {struct task_struct *kptr;struct __tasks_kfunc_map_value *v;
@@ -143,18 +143,18 @@ int BPF_PROG(test_task_get_release, struct task_struct *task, u64 clone_flags)return 0 ; } - kptr =bpf_task_kptr_get (&v->task); -if (kptr) { -/* Until we resolve the issues with using task->rcu_users, we -* expect bpf_task_kptr_get() to return a NULL task. See the -* comment at the definition of bpf_task_acquire_not_zero() for -* more details. -*/ -bpf_task_release (kptr); +bpf_rcu_read_lock (); + kptr = v->task; +if (!kptr) { err =3 ; -return 0 ; + }else { + kptr =bpf_task_acquire (kptr); +if (!kptr) + err =4 ; +else +bpf_task_release (kptr); } - +bpf_rcu_read_unlock ();return 0 ; }
Signature · 1 line
2.39.0
PATCH 3/3 bpf,docs: Update documentation to reflect new task kfuncs
In reply to David VernetNow that struct task_struct objects are RCU safe, and bpf_task_acquire() can return NULL, we should update the BPF task kfunc documentation to reflect the current state of the API.
--- Documentation/bpf/kfuncs.rst | 49 +++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 6 deletions(-)
Documentation/bpf/kfuncs.rst +43 -6
@@ -471,13 +471,50 @@ struct_ops callback arg. For example: struct task_struct *acquired; acquired = bpf_task_acquire(task); + if (acquired) + /* + * In a typical program you'd do something like store + * the task in a map, and the map will automatically + * release it later. Here, we release it manually. + */ + bpf_task_release(acquired); + return 0; + } + + +References acquired on ``struct task_struct *`` objects are RCU protected. +Therefore, when in an RCU read region, you can obtain a pointer to a task +embedded in a map value without having to acquire a reference: + +.. code-block:: c + + #define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8))) + private(TASK) static struct task_struct *global; + + /** + * A trivial example showing how to access a task stored + * in a map using RCU. + */ + SEC("tp_btf/task_newtask") + int BPF_PROG(task_rcu_read_example, struct task_struct *task, u64 clone_flags) + { + struct task_struct *local_copy; + + bpf_rcu_read_lock(); + local_copy = global; + if (local_copy) + /* + * We could also pass local_copy to kfuncs or helper functions here, + * as we're guaranteed that local_copy will be valid until we exit + * the RCU read region below. + */ + bpf_printk("Global task %s is valid", local_copy->comm); + else + bpf_printk("No global task found"); + bpf_rcu_read_unlock(); + + /* At this point we can no longer reference local_copy. */ - /* - * In a typical program you'd do something like store - * the task in a map, and the map will automatically - * release it later. Here, we release it manually. - */ - bpf_task_release(acquired); return 0; }
Signature · 1 line
2.39.0