On 8/15/25 23:00, Jiri Olsa wrote:
On Fri, Aug 15, 2025 at 08:21:55PM +0100, Mykyta Yatsenko wrote:
SNIP
void bpf_task_work_cancel_and_free(void *val)
{
+ struct bpf_task_work *tw = val;
+ struct bpf_task_work_context *ctx;
+ enum bpf_task_work_state state;
+
+ /* No need do rcu_read_lock as no other codepath can reset this pointer */
+ ctx = unrcu_pointer(xchg((struct bpf_task_work_context __force __rcu **)&tw->ctx, NULL));
+ if (!ctx)
+ return;
+ state = xchg(&ctx->state, BPF_TW_FREED);
+
+ switch (state) {
+ case BPF_TW_SCHEDULED:
+ /* If we can't cancel task work, rely on task work callback to free the context */
+ if (!task_work_cancel_match(ctx->task, task_work_match, ctx))
+ break;
+ bpf_task_work_context_reset(ctx);
+ fallthrough;
+ case BPF_TW_STANDBY:
+ call_rcu_tasks_trace(&ctx->rcu, bpf_task_work_context_free);
+ break;
+ /* In all below cases scheduling logic should detect context state change and cleanup */
+ case BPF_TW_SCHEDULING:
+ case BPF_TW_PENDING:
+ case BPF_TW_RUNNING:
+ default:
+ break;
+ }
}
BTF_KFUNCS_START(generic_btf_ids)
@@ -3769,6 +4017,8 @@ BTF_ID_FLAGS(func, bpf_rbtree_first, KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_rbtree_root, KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_rbtree_left, KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_rbtree_right, KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_TRUSTED_ARGS)
hi,
I'd like to use that with uprobes, could we add it to common_kfunc_set?
I tried it with uprobe and it seems to work nicely
thanks,
jirka
I'll move these kfuncs into common_kfunc_set, in the next version.
----
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 346ae8fd3ada..b5d52168ba77 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -4129,6 +4129,8 @@ BTF_ID_FLAGS(func, bpf_strnstr);
BTF_ID_FLAGS(func, bpf_cgroup_read_xattr, KF_RCU)
#endif
BTF_ID_FLAGS(func, bpf_stream_vprintk, KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_task_work_schedule_signal, KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_task_work_schedule_resume, KF_TRUSTED_ARGS)
BTF_KFUNCS_END(common_btf_ids)
static const struct btf_kfunc_id_set common_kfunc_set = {