AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/11/12 22:39 flow patch-triage
10m Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "DEBUG_INFO_BTF",
    "CGROUPS"
  ],
  "FocusSymbols": [
    "bpf_task_park",
    "bpf_task_wake",
    "bpf_kthread_create",
    "bpf_kthread_start",
    "bpf_kthread_bind",
    "bpf_kthread_attach_cgroup",
    "bpf_kthread_stop",
    "bpf_kthread_should_stop",
    "bpf_prog_free_kthreads",
    "cgroup_attach_kthread"
  ],
  "KMSANReasoning": "The patch series introduces BPF kthreads (`bpf_kthread_create`, `bpf_kthread_start`, `bpf_kthread_bind`, `bpf_kthread_attach_cgroup`, `bpf_kthread_stop`) and task park/wake mechanisms (`bpf_task_park`, `bpf_task_wake`), alongside verifier enhancements to track sleepable, parkable, and migrating subprograms and invalidate borrowed references across park calls.\n\nKMSAN vs KASAN Applicability:\n1. Uninitialized memory / info-leak analysis:\n   - No kernel data structures or buffers are copied to user space (no `copy_to_user`, `put_user`, socket options, netlink attributes, or ioctl output structs).\n   - All newly introduced heap objects (e.g. `struct bpf_kthread` via `kzalloc_obj`) and struct fields (e.g. `bpf_wake_pending` and `bpf_wake_node` in `struct task_struct`, initialized in `dup_task_struct()`; `kthreads` and `kthread_seq` in `struct bpf_prog_aux`, allocated with `GFP_ZERO`; verifier state flags `parked`, `in_kthread_fn`, `might_park` in zero-allocated structures) are fully initialized before use.\n   - All local variables and branching conditions across `task_park.c`, `kthread.c`, `cgroup.c`, and `verifier.c` are properly initialized. There is no risk of uninitialized memory reads or information leakage to userspace.\n\n2. Memory safety and concurrency risks (KASAN / LOCKDEP):\n   - The primary risks of this patch involve task and program lifetime management, reference counting (`get_task_struct`, `put_task_struct`, `bpf_prog_inc_not_zero`, `kthread_stop_put`), race conditions between parking and waking, and synchronization during kthread shutdown or migration. These risks (such as Use-After-Free, double frees, or invalid dereferences) are directly covered by KASAN.\n   - Potential deadlocks (such as cyclic stopping chains or interactions between raw spinlocks, cgroup mutexes, and scheduler wait states) are covered by LOCKDEP.\n\nBecause there are no uninitialized memory risks or kernel info-leaks, a dedicated KMSAN fuzzing session is not justified.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch introduces BPF kthreads and task parking mechanisms (bpf_task_park, bpf_task_wake, and bpf_kthread_* kfuncs), along with cgroup kthread attachment and significant verifier extensions for tracking migrating kfuncs, register/stack object invalidation across sleeps, and lifecycle teardown. These changes touch core BPF, scheduler, and cgroup interactions, and are fully reachable from userspace via the BPF syscall. Fuzzing is warranted to test for concurrency issues, lifetime/teardown races, and verifier soundness regressions.",
  "WorthFuzzing": true
}

1/1 2026/11/12 22:39 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 3f9f9198c5e11a731f7218111346acc0acb7780f\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Thu Nov 12 22:39:54 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/include/linux/bpf.h b/include/linux/bpf.h\nindex 520fd72e21fd07..7c15593cb18880 100644\n--- a/include/linux/bpf.h\n+++ b/include/linux/bpf.h\n@@ -1943,6 +1943,8 @@ struct bpf_prog_aux {\n \tbool changes_pkt_data;\n \tbool has_prologue;\n \tbool might_sleep;\n+\t/* a run may leave on another CPU than it started on, see bpf_task_park() */\n+\tbool may_migrate;\n \tbool can_unwind;\n \t/* frames of recursive subprogs can be on the stack when it is called */\n \tbool in_recursion;\n@@ -2044,6 +2046,8 @@ struct bpf_prog_aux {\n \tstruct bpf_stream *stream[2];\n \tspinlock_t st_ops_assoc_lock; /* protects st_ops_assoc */\n \tstruct bpf_map __rcu *st_ops_assoc;\n+\tstruct list_head kthreads; /* BPF kthreads of the prog, see kernel/bpf/kthread.c */\n+\tatomic_t kthread_seq; /* index of the prog's next BPF kthread, in its name */\n };\n \n #define BPF_NR_CONTEXTS        4       /* normal, softirq, hardirq, NMI */\n@@ -2968,6 +2972,7 @@ struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);\n void bpf_prog_put(struct bpf_prog *prog);\n \n void bpf_prog_free_id(struct bpf_prog *prog);\n+void bpf_prog_free_kthreads(struct bpf_prog_aux *aux);\n void bpf_map_free_id(struct bpf_map *map);\n \n struct btf_field *btf_record_find(const struct btf_record *rec,\ndiff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h\nindex 4771c55d8f41f6..7097bb7033e8fc 100644\n--- a/include/linux/bpf_verifier.h\n+++ b/include/linux/bpf_verifier.h\n@@ -469,6 +469,12 @@ struct bpf_func_state {\n \tbool in_callback_fn;\n \tbool in_async_callback_fn;\n \tbool in_exception_callback_fn;\n+\t/*\n+\t * The body of a BPF kthread: an async callback whose kernel caller,\n+\t * bpf_kthread_fn(), keeps nothing across it that the sleep in\n+\t * bpf_task_park() could invalidate, so the park is allowed in it.\n+\t */\n+\tbool in_kthread_fn;\n \tbool no_stack_arg_load;\n \t/*\n \t * The helper invokes this callback under rcu_read_lock(), so the\n@@ -559,6 +565,12 @@ struct bpf_verifier_state {\n \t * and their packet pointers are not in the state, see bpf_flow_return().\n \t */\n \tbool changed_pkt_data;\n+\t/*\n+\t * A function that is verified on its own parked the task. Its callers,\n+\t * what the sleep takes from them and whether one of them is a callback,\n+\t * are not in the state, see bpf_flow_return().\n+\t */\n+\tbool parked;\n \t/*\n \t * Not zero in a function that is verified on its own: the call that the\n \t * state at its entry came from, see bpf_flow_call().\n@@ -1056,6 +1068,8 @@ struct bpf_subprog_info {\n \tbool keep_fastcall_stack: 1;\n \tbool changes_pkt_data: 1;\n \tbool might_sleep: 1;\n+\t/* some call reachable from this subprog may be bpf_task_park() */\n+\tbool might_park: 1;\n \tbool uses_arena_sp: 1;\n \t/* a callback that takes a stack in arena, like the main program does */\n \tbool arena_stack_entry: 1;\n@@ -2033,6 +2047,7 @@ static inline bool bpf_is_kfunc_sleepable(struct bpf_call_arg_meta *meta)\n }\n bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta);\n bool bpf_is_cast_kfunc(const struct bpf_call_arg_meta *meta);\n+bool bpf_is_task_park_kfunc(struct bpf_call_arg_meta *meta);\n struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem);\n int bpf_copy_insn_array_uniq(struct bpf_map *map, u32 start, u32 end, u32 *off);\n bool bpf_insn_is_cond_jump(u8 code);\ndiff --git a/include/linux/cgroup.h b/include/linux/cgroup.h\nindex 2afb4cb2bb4fa4..f649973801f4c0 100644\n--- a/include/linux/cgroup.h\n+++ b/include/linux/cgroup.h\n@@ -158,6 +158,7 @@ struct cgroup *cgroup_v1v2_get_from_fd(int fd);\n bool cgroup1_ssid_disabled(int ssid);\n \n int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);\n+int cgroup_attach_kthread(struct cgroup *dst_cgrp, struct task_struct *task);\n int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);\n \n int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);\n@@ -804,6 +805,8 @@ static inline void cgroup_lock(void) {}\n static inline void cgroup_unlock(void) {}\n static inline int cgroup_attach_task_all(struct task_struct *from,\n \t\t\t\t\t struct task_struct *t) { return 0; }\n+static inline int cgroup_attach_kthread(struct cgroup *dst_cgrp,\n+\t\t\t\t\tstruct task_struct *task) { return -ENODEV; }\n static inline int cgroupstats_build(struct cgroupstats *stats,\n \t\t\t\t    struct dentry *dentry) { return -EINVAL; }\n \ndiff --git a/include/linux/filter.h b/include/linux/filter.h\nindex 61448be53f8da6..8a99b9aacfb057 100644\n--- a/include/linux/filter.h\n+++ b/include/linux/filter.h\n@@ -883,6 +883,13 @@ static __always_inline u32 bpf_prog_run(const struct bpf_prog *prog, const void\n  * This uses migrate_disable/enable() explicitly to document that the\n  * invocation of a BPF program does not require reentrancy protection\n  * against a BPF program which is invoked from a preempting task.\n+ *\n+ * A syscall program may call bpf_task_park(), or a BPF kthread kfunc that\n+ * waits for another task, which lift the pin for the duration of the sleep\n+ * or wait and restore it before returning to the program, so such a program\n+ * may resume on another CPU. Nothing on this path keeps per-CPU state\n+ * across the program: __bpf_prog_run() samples its stats only after the\n+ * program returns.\n  */\n static inline u32 bpf_prog_run_pin_on_cpu(const struct bpf_prog *prog,\n \t\t\t\t\t  const void *ctx)\ndiff --git a/include/linux/sched.h b/include/linux/sched.h\nindex 1c93da75226e32..0fdd3917443602 100644\n--- a/include/linux/sched.h\n+++ b/include/linux/sched.h\n@@ -1630,6 +1630,10 @@ struct task_struct {\n \tstruct bpf_local_storage __rcu\t*bpf_storage;\n \t/* Used for BPF run context */\n \tstruct bpf_run_ctx\t\t*bpf_ctx;\n+\t/* Set by bpf_task_wake(), consumed by bpf_task_park() */\n+\tint\t\t\t\tbpf_wake_pending;\n+\t/* On a CPU's queue of deferred bpf_task_wake() calls when not NULL */\n+\tstruct wake_q_node\t\tbpf_wake_node;\n #endif\n \t/* Used by BPF for per-TASK xdp storage */\n \tstruct bpf_net_context\t\t*bpf_net_context;\ndiff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile\nindex d26c62326e588f..a754fbefae39a9 100644\n--- a/kernel/bpf/Makefile\n+++ b/kernel/bpf/Makefile\n@@ -15,7 +15,7 @@ obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o check_btf.o flow.o join.o\n obj-${CONFIG_BPF_LSM}\t  += bpf_inode_storage.o\n obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o\n obj-$(CONFIG_BPF_JIT) += trampoline.o\n-obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o\n+obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o task_park.o kthread.o\n ifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy)\n obj-$(CONFIG_BPF_SYSCALL) += arena.o range_tree.o\n endif\ndiff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c\nindex 133f189bf6d270..541f0830cf79a6 100644\n--- a/kernel/bpf/arraymap.c\n+++ b/kernel/bpf/arraymap.c\n@@ -1028,7 +1028,12 @@ static void *prog_fd_array_get_ptr(struct bpf_map *map,\n \tif (IS_ERR(prog))\n \t\treturn prog;\n \n-\tif (prog-\u003etype == BPF_PROG_TYPE_EXT ||\n+\t/*\n+\t * A tail call runs the program on the caller's Tasks Trace RCU read\n+\t * section alone, and a program that parks leaves that section for its\n+\t * sleep, see bpf_task_park().\n+\t */\n+\tif (prog-\u003etype == BPF_PROG_TYPE_EXT || prog-\u003eaux-\u003emay_migrate ||\n \t    !bpf_prog_map_compatible(map, prog)) {\n \t\tbpf_prog_put(prog);\n \t\treturn ERR_PTR(-EINVAL);\ndiff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c\nindex bbcf42eae9d921..0102df246633f0 100644\n--- a/kernel/bpf/cfg.c\n+++ b/kernel/bpf/cfg.c\n@@ -67,6 +67,14 @@ static void mark_subprog_might_sleep(struct bpf_verifier_env *env, int off)\n \tsubprog-\u003emight_sleep = true;\n }\n \n+static void mark_subprog_might_park(struct bpf_verifier_env *env, int off)\n+{\n+\tstruct bpf_subprog_info *subprog;\n+\n+\tsubprog = bpf_find_containing_subprog(env, off);\n+\tsubprog-\u003emight_park = true;\n+}\n+\n static void mark_subprog_might_throw(struct bpf_verifier_env *env, int off)\n {\n \tstruct bpf_subprog_info *subprog;\n@@ -86,15 +94,28 @@ static void mark_subprog_might_throw(struct bpf_verifier_env *env, int off)\n static bool merge_callee_effects(struct bpf_verifier_env *env, int t, int w)\n {\n \tstruct bpf_subprog_info *caller, *callee;\n+\tbool might_park;\n \n \tcaller = bpf_find_containing_subprog(env, t);\n \tcallee = bpf_find_containing_subprog(env, w);\n+\t/*\n+\t * A park in a callback does not mark the function that only loads\n+\t * the callback's address: a synchronous callback is refused at the\n+\t * park with the caller's frames in view, and an asynchronous one is a\n+\t * root of its own. The address may also be called through callx,\n+\t * which is a call like any other, so the mark follows it when the\n+\t * program has callx at all.\n+\t */\n+\tmight_park = callee-\u003emight_park \u0026\u0026\n+\t\t     (bpf_pseudo_call(\u0026env-\u003eprog-\u003einsnsi[t]) || env-\u003ehas_callx);\n \tif (caller-\u003echanges_pkt_data \u003e= callee-\u003echanges_pkt_data \u0026\u0026\n \t    caller-\u003emight_sleep \u003e= callee-\u003emight_sleep \u0026\u0026\n+\t    caller-\u003emight_park \u003e= might_park \u0026\u0026\n \t    caller-\u003emight_throw \u003e= callee-\u003emight_throw)\n \t\treturn false;\n \tcaller-\u003echanges_pkt_data |= callee-\u003echanges_pkt_data;\n \tcaller-\u003emight_sleep |= callee-\u003emight_sleep;\n+\tcaller-\u003emight_park |= might_park;\n \tcaller-\u003emight_throw |= callee-\u003emight_throw;\n \treturn true;\n }\n@@ -618,6 +639,8 @@ static int visit_insn(int t, struct bpf_verifier_env *env)\n \t\t\t */\n \t\t\tif (ret == 0 \u0026\u0026 bpf_is_kfunc_sleepable(\u0026meta))\n \t\t\t\tmark_subprog_might_sleep(env, t);\n+\t\t\tif (ret == 0 \u0026\u0026 bpf_is_task_park_kfunc(\u0026meta))\n+\t\t\t\tmark_subprog_might_park(env, t);\n \t\t\tif (ret == 0 \u0026\u0026 bpf_is_kfunc_pkt_changing(\u0026meta))\n \t\t\t\tmark_subprog_changes_pkt_data(env, t);\n \t\t\tif (ret == 0 \u0026\u0026 bpf_is_throw_kfunc(insn))\ndiff --git a/kernel/bpf/core.c b/kernel/bpf/core.c\nindex f88a1c041a7a69..95f4be8ed40537 100644\n--- a/kernel/bpf/core.c\n+++ b/kernel/bpf/core.c\n@@ -138,6 +138,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag\n #ifdef CONFIG_FINEIBT\n \tINIT_LIST_HEAD_RCU(\u0026fp-\u003eaux-\u003eksym_prefix.lnode);\n #endif\n+\tINIT_LIST_HEAD(\u0026fp-\u003eaux-\u003ekthreads);\n \tmutex_init(\u0026fp-\u003eaux-\u003eused_maps_mutex);\n \tmutex_init(\u0026fp-\u003eaux-\u003eext_mutex);\n \tmutex_init(\u0026fp-\u003eaux-\u003edst_mutex);\ndiff --git a/kernel/bpf/diagnostics.c b/kernel/bpf/diagnostics.c\nindex 3de3e1486bbdef..33788d6f615580 100644\n--- a/kernel/bpf/diagnostics.c\n+++ b/kernel/bpf/diagnostics.c\n@@ -2231,6 +2231,9 @@ static void diag_print_mod(struct bpf_verifier_env *env, const struct bpf_diag_h\n \tcase BPF_DIAG_MOD_NON_OWN_REF:\n \t\treason = \"leaving the protected region invalidated this borrowed pointer\";\n \t\tbreak;\n+\tcase BPF_DIAG_MOD_TASK_PARK:\n+\t\treason = \"the sleep in bpf_task_park() invalidated this pointer\";\n+\t\tbreak;\n \tcase BPF_DIAG_MOD_CALLER_SAVED:\n \t\treason = target-\u003ekind == BPF_DIAG_MOD_TARGET_STACK_ARG ?\n \t\t\t \"call invalidated this outgoing stack argument\" :\ndiff --git a/kernel/bpf/diagnostics.h b/kernel/bpf/diagnostics.h\nindex e155d2588a51c6..656c0430f013e4 100644\n--- a/kernel/bpf/diagnostics.h\n+++ b/kernel/bpf/diagnostics.h\n@@ -26,6 +26,7 @@ enum bpf_diag_mod_reason {\n \tBPF_DIAG_MOD_SLICE_BUF,\n \tBPF_DIAG_MOD_NON_OWN_REF,\n \tBPF_DIAG_MOD_CALLER_SAVED,\n+\tBPF_DIAG_MOD_TASK_PARK,\n };\n \n enum bpf_diag_context_kind {\ndiff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c\nindex 1d33a78d937390..df5784a132d754 100644\n--- a/kernel/bpf/fixups.c\n+++ b/kernel/bpf/fixups.c\n@@ -2862,6 +2862,12 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)\n \t\t\tu16 live = env-\u003einsn_aux_data[i].live_regs_before;\n \t\t\tint reg, save_off = 0;\n \n+\t\t\t/* The count and the time are kept per cpu, see bpf_timed_may_goto_save(). */\n+\t\t\tif (prog-\u003eaux-\u003emay_migrate) {\n+\t\t\t\tverbose(env, \"may_goto cannot be used in a program that may migrate, also not in a loop that gets one\\n\");\n+\t\t\t\treturn -EINVAL;\n+\t\t\t}\n+\n \t\t\t/* a register that is dead here holds the address of the count */\n \t\t\tfor (reg = BPF_REG_0; reg \u003c= BPF_REG_9; reg++)\n \t\t\t\tif (!(live \u0026 BIT(reg)))\ndiff --git a/kernel/bpf/flow.c b/kernel/bpf/flow.c\nindex c9458b3ad114a5..47ff2fb5f11a50 100644\n--- a/kernel/bpf/flow.c\n+++ b/kernel/bpf/flow.c\n@@ -2309,7 +2309,8 @@ static void flow_shape_ctx(const struct bpf_verifier_state *st, struct flow_shap\n \n \tctx = jhash_3words(st-\u003ein_sleepable | st-\u003ecallers_unknown \u003c\u003c 1 |\n \t\t\t   st-\u003eunwound \u003c\u003c 2 | st-\u003echanged_pkt_data \u003c\u003c 3 |\n-\t\t\t   (st-\u003eactive_irq_id != 0) \u003c\u003c 4 | (st-\u003eactive_lock_id != 0) \u003c\u003c 5,\n+\t\t\t   (st-\u003eactive_irq_id != 0) \u003c\u003c 4 | (st-\u003eactive_lock_id != 0) \u003c\u003c 5 |\n+\t\t\t   st-\u003eparked \u003c\u003c 6,\n \t\t\t   st-\u003eacquired_refs, st-\u003eactive_locks, 0);\n \tctx = jhash_2words(st-\u003eactive_preempt_locks, st-\u003eactive_rcu_locks, ctx);\n \tctx = jhash_2words(lower_32_bits((unsigned long)st-\u003eactive_lock_ptr),\ndiff --git a/kernel/bpf/join.c b/kernel/bpf/join.c\nindex cc4494aef18733..b63f99c663080e 100644\n--- a/kernel/bpf/join.c\n+++ b/kernel/bpf/join.c\n@@ -452,6 +452,7 @@ static bool join_func(struct bpf_join_state *js, struct bpf_func_state *fd,\n \t\t\treturn false;\n \t\tif (fa-\u003ein_callback_fn != fb-\u003ein_callback_fn ||\n \t\t    fa-\u003ein_async_callback_fn != fb-\u003ein_async_callback_fn ||\n+\t\t    fa-\u003ein_kthread_fn != fb-\u003ein_kthread_fn ||\n \t\t    fa-\u003ein_exception_callback_fn != fb-\u003ein_exception_callback_fn ||\n \t\t    fa-\u003ein_rcu_callback_fn != fb-\u003ein_rcu_callback_fn ||\n \t\t    fa-\u003easync_entry_cnt != fb-\u003easync_entry_cnt ||\n@@ -532,7 +533,8 @@ static bool join_check_state(const struct bpf_verifier_state *a,\n \t\treturn false;\n \tif (a-\u003especulative != b-\u003especulative || a-\u003ein_sleepable != b-\u003ein_sleepable ||\n \t    a-\u003ecallers_unknown != b-\u003ecallers_unknown ||\n-\t    a-\u003eunwound != b-\u003eunwound || a-\u003echanged_pkt_data != b-\u003echanged_pkt_data)\n+\t    a-\u003eunwound != b-\u003eunwound || a-\u003echanged_pkt_data != b-\u003echanged_pkt_data ||\n+\t    a-\u003eparked != b-\u003eparked)\n \t\treturn false;\n \tif (a-\u003eactive_locks != b-\u003eactive_locks ||\n \t    a-\u003eactive_preempt_locks != b-\u003eactive_preempt_locks ||\ndiff --git a/kernel/bpf/kthread.c b/kernel/bpf/kthread.c\nnew file mode 100644\nindex 00000000000000..d4b8110545f09d\n--- /dev/null\n+++ b/kernel/bpf/kthread.c\n@@ -0,0 +1,572 @@\n+// SPDX-License-Identifier: GPL-2.0-only\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \u003clinux/bpf.h\u003e\n+#include \u003clinux/btf.h\u003e\n+#include \u003clinux/btf_ids.h\u003e\n+#include \u003clinux/cgroup.h\u003e\n+#include \u003clinux/cpumask.h\u003e\n+#include \u003clinux/kthread.h\u003e\n+#include \u003clinux/rcupdate_trace.h\u003e\n+#include \u003clinux/sched.h\u003e\n+#include \u003clinux/sched/signal.h\u003e\n+#include \u003clinux/sched/task.h\u003e\n+\n+/*\n+ * A BPF kthread is a kernel thread whose thread function is one callback of\n+ * the program that created it, run once. The callback is the body of the\n+ * thread: it loops, sleeps in bpf_task_park() and returns on its own, and its\n+ * return ends the thread. The kernel adds nothing in between.\n+ *\n+ * The thread is known to the program by its task_struct. bpf_kthread_create()\n+ * returns it as a referenced pointer, and the control kfuncs take it back, so\n+ * the program keeps it wherever it keeps task pointers: a kptr in a map value,\n+ * a graph node, or a register until\n+ * bpf_task_release(). Nothing else is handed out, and nothing else needs a\n+ * lifetime of its own.\n+ *\n+ * Every thread is on one global list, which is how a control kfunc tells a\n+ * BPF kthread from any other task it is given, and how the thread's object is\n+ * found without reading through a task that may have exited. Whoever takes a\n+ * thread off the list under the lock owns its teardown.\n+ *\n+ * A thread does not hold a reference on its program. If it did, a program\n+ * whose user dropped every file descriptor would live on through its own\n+ * threads, with nothing left that could stop them. Instead the release of the\n+ * program's last reference stops the threads it still has, before anything\n+ * their callback could be using is released, and creation fails once the\n+ * program has no reference left. A program's threads thus end with its last\n+ * user, the way a timer or a work item ends with the last user of its map.\n+ * The one exception is a thread that bpf_kthread_stop() has taken off the\n+ * lists but that has not exited yet: its stopper holds the program for it.\n+ *\n+ * Four of the kfuncs wait for another task. Creation waits for kthreadd,\n+ * bpf_kthread_stop() for the thread's exit, and bpf_kthread_bind() and\n+ * bpf_kthread_attach_cgroup() for a running thread's next migrate_enable(),\n+ * which is what set_cpus_allowed_ptr() waits for on any migration-disabled\n+ * task. The kernel assumes that a task which has disabled migration enables\n+ * it again soon, and waits for that while holding locks: cpuset attach under\n+ * cgroup_mutex, cpus_read_lock() and the threadgroup rwsem, a cpuset.cpus\n+ * write under cpuset_mutex, CPU offline in balance_hotplug_wait(). kthreadd's\n+ * fork read-locks the global threadgroup rwsem as well, which a cgroup move\n+ * holds in the default lock mode; under favordynmods the move holds the moved\n+ * process's own, but a subtree_control write still takes the global one. A\n+ * sleepable program is migration-disabled for its whole run and a thread body\n+ * for its life outside the park, so a wait made while pinned can close a cycle\n+ * through any of those: a task moved into a cpuset while it creates a thread,\n+ * a thread blocked in a stop being bound by the thread it waits for, a thread\n+ * on a CPU going offline taking cpus_read_lock(). Each of the four waits\n+ * therefore runs with migration enabled and the Tasks Trace read section kept.\n+ * Nothing the read section protects depends on the CPU: the program, its maps\n+ * and its context are held as before, per-CPU memory stays valid memory, and\n+ * neither runner keeps per-CPU state across the program. A program that keeps\n+ * such state of its own, a stack in arena or the time slice of may_goto, is\n+ * refused these kfuncs as it is refused the park. The caller may return on\n+ * another CPU, the contract bpf_task_park() already carries, so a per-CPU map\n+ * value pointer or a CPU id obtained before such a call names the CPU of the\n+ * lookup. Unlike the park, the waits keep the program's protection, so the\n+ * verifier has nothing to invalidate across them.\n+ */\n+\n+struct bpf_kthread {\n+\tstruct list_head node;\t\t/* on bpf_kthreads */\n+\tstruct list_head prog_node;\t/* on aux-\u003ekthreads */\n+\tstruct task_struct *task;\t/* holds one reference for this object */\n+\tstruct bpf_prog_aux *aux;\t/* the program whose callback this is */\n+\tstruct bpf_kthread *waits_for;\t/* the thread this one is stopping, under the lock */\n+\tvoid *callback_fn;\n+\tu64 arg;\n+};\n+\n+/*\n+ * Both lists, and every object on them, are under this lock. It is a raw lock\n+ * taken with IRQs disabled because bpf_kthread_start() is not sleepable: a\n+ * timer callback calls it from softirq, and a program may call it with IRQs\n+ * or preemption disabled.\n+ */\n+static LIST_HEAD(bpf_kthreads);\n+static DEFINE_RAW_SPINLOCK(bpf_kthreads_lock);\n+\n+static int bpf_kthread_fn(void *data)\n+{\n+\tstruct bpf_kthread *kthread = data;\n+\tbpf_callback_t callback_fn = kthread-\u003ecallback_fn;\n+\tunsigned int pflags;\n+\tint ret;\n+\n+\t/* Stopped before it was started: the callback does not run. */\n+\tif (kthread_should_stop())\n+\t\treturn -EINTR;\n+\n+\t/*\n+\t * The callback runs as the body of a sleepable program does, with\n+\t * migration disabled under a Tasks Trace RCU read section, except\n+\t * while it sleeps in bpf_task_park(), which drops both for the sleep\n+\t * and takes them again before it returns, and while it waits in one\n+\t * of the kfuncs the comment at the top names, which drop migration\n+\t * disable alone. That is\n+\t * safe here for the reason it is safe in a syscall program: this\n+\t * function keeps nothing across the callback, and the program, with\n+\t * its maps and code, is held until the thread has exited, since the\n+\t * release of its last reference stops the thread first. The verifier\n+\t * checks the callback as a sleepable callback in which the park is\n+\t * allowed.\n+\t */\n+\t/*\n+\t * A task work queued for this thread with TWA_SIGNAL sets\n+\t * TIF_NOTIFY_SIGNAL, which a kernel thread never gets to clear, since\n+\t * the work runs on the return to user mode or at exit. Left alone it\n+\t * would end every sleep of the body at once from then on.\n+\t */\n+\tpflags = no_notify_signal_save();\n+\trcu_read_lock_trace();\n+\tmigrate_disable();\n+\tret = callback_fn(kthread-\u003earg, 0, 0, 0, 0);\n+\tmigrate_enable();\n+\trcu_read_unlock_trace();\n+\tno_notify_signal_restore(pflags);\n+\treturn ret;\n+}\n+\n+/* The BPF kthread whose task is @task, if any. Call with bpf_kthreads_lock held. */\n+static struct bpf_kthread *bpf_kthread_find(struct task_struct *task)\n+{\n+\tstruct bpf_kthread *kthread;\n+\n+\tlockdep_assert_held(\u0026bpf_kthreads_lock);\n+\tlist_for_each_entry(kthread, \u0026bpf_kthreads, node)\n+\t\tif (kthread-\u003etask == task)\n+\t\t\treturn kthread;\n+\treturn NULL;\n+}\n+\n+/* Take a thread off both lists. Call with bpf_kthreads_lock held. */\n+static void bpf_kthread_unlink(struct bpf_kthread *kthread)\n+{\n+\tlockdep_assert_held(\u0026bpf_kthreads_lock);\n+\tlist_del(\u0026kthread-\u003enode);\n+\tlist_del(\u0026kthread-\u003eprog_node);\n+}\n+\n+/*\n+ * The object of the calling thread, if it is a BPF kthread that has not begun\n+ * to exit. An exiting thread still runs its task work, which may be a program's\n+ * callback, and its object is freed as soon as whoever stops it has seen it\n+ * exit, which is before that. Nothing waits for such a thread any more, so it\n+ * needs no record of what it waits for.\n+ */\n+static struct bpf_kthread *bpf_kthread_self(void)\n+{\n+\tif (kthread_func(current) != bpf_kthread_fn || (current-\u003eflags \u0026 PF_EXITING))\n+\t\treturn NULL;\n+\treturn kthread_data(current);\n+}\n+\n+/*\n+ * Whether a stop of @kthread would wait, through the chain of threads each of\n+ * them is stopping, for the calling thread. Call with bpf_kthreads_lock held.\n+ * Every object on the chain is alive: a thread clears waits_for under the lock\n+ * before it frees the object it points to.\n+ */\n+static bool bpf_kthread_waits_for_current(struct bpf_kthread *kthread)\n+{\n+\tlockdep_assert_held(\u0026bpf_kthreads_lock);\n+\tfor (; kthread; kthread = kthread-\u003ewaits_for)\n+\t\tif (kthread-\u003etask == current)\n+\t\t\treturn true;\n+\treturn false;\n+}\n+\n+/*\n+ * Stop a thread that has been taken off the lists and free its object. Waits\n+ * for the thread to exit, and returns what its callback returned, or -EINTR if\n+ * the callback never ran. Returns at once for a thread that has exited.\n+ */\n+static int bpf_kthread_stop_and_free(struct bpf_kthread *kthread)\n+{\n+\tint ret;\n+\n+\tret = kthread_stop_put(kthread-\u003etask);\n+\tkfree(kthread);\n+\treturn ret;\n+}\n+\n+/*\n+ * Stop every thread of @aux. Called when the program's last reference goes,\n+ * before its code, maps and BTF are released, and before the Tasks Trace RCU\n+ * grace period that release waits for, which a thread still inside its\n+ * callback would hold up. May block.\n+ */\n+void bpf_prog_free_kthreads(struct bpf_prog_aux *aux)\n+{\n+\tstruct bpf_kthread *kthread, *tmp;\n+\tunsigned long irqflags;\n+\tLIST_HEAD(kthreads);\n+\n+\t/*\n+\t * Nothing joins a program's list once its last reference is gone, so\n+\t * an empty list stays empty. The release path takes the lock only for\n+\t * a program with threads, from a work item, never from the direct\n+\t * path it may take in softirq context for a program without.\n+\t */\n+\tif (list_empty(\u0026aux-\u003ekthreads))\n+\t\treturn;\n+\n+\traw_spin_lock_irqsave(\u0026bpf_kthreads_lock, irqflags);\n+\tlist_for_each_entry_safe(kthread, tmp, \u0026aux-\u003ekthreads, prog_node) {\n+\t\tlist_del(\u0026kthread-\u003enode);\n+\t\tlist_move_tail(\u0026kthread-\u003eprog_node, \u0026kthreads);\n+\t}\n+\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\n+\n+\tlist_for_each_entry_safe(kthread, tmp, \u0026kthreads, prog_node)\n+\t\tbpf_kthread_stop_and_free(kthread);\n+}\n+\n+__bpf_kfunc_start_defs();\n+\n+/**\n+ * bpf_kthread_create - Create a kernel thread that runs a BPF callback\n+ * @callback_fn: the thread's body, run once; its return ends the thread\n+ * @arg: passed to @callback_fn as its only argument\n+ * @flags: must be 0\n+ * @aux: the calling program, set by the verifier\n+ *\n+ * Creates a kernel thread named bpfk/\u003cprogram id\u003e:\u003cindex\u003e without starting it,\n+ * the index counting the program's creations so that its threads are told\n+ * apart in ps, top and traces; the prefix is short so that both numbers fit\n+ * the comm that tracepoints and perf report as long as they have nine digits\n+ * between them, where /proc shows the full name in any case. bpf_kthread_start()\n+ * runs @callback_fn on it with @arg. The thread ends when the callback returns,\n+ * when bpf_kthread_stop() stops it, or with the last reference on the program\n+ * that created it. A thread whose callback has returned keeps its return value\n+ * until bpf_kthread_stop() collects it.\n+ *\n+ * The creation waits for kthreadd with migration enabled, so the caller may\n+ * return on another CPU.\n+ *\n+ * Return: the thread's task with a reference the program must release, or NULL\n+ * if @flags is not 0, the thread could not be created, or the program is being\n+ * freed.\n+ */\n+__bpf_kfunc struct task_struct *bpf_kthread_create(int (callback_fn)(u64 arg), u64 arg,\n+\t\t\t\t\t\t   u64 flags, struct bpf_prog_aux *aux)\n+{\n+\tstruct bpf_kthread *kthread;\n+\tunsigned long irqflags;\n+\tstruct task_struct *task;\n+\tstruct bpf_prog *prog;\n+\n+\tif (flags)\n+\t\treturn NULL;\n+\n+\tkthread = kzalloc_obj(*kthread, GFP_KERNEL_ACCOUNT);\n+\tif (!kthread)\n+\t\treturn NULL;\n+\tkthread-\u003eaux = aux;\n+\tkthread-\u003ecallback_fn = callback_fn;\n+\tkthread-\u003earg = arg;\n+\n+\t/*\n+\t * The creation waits for kthreadd, and kthreadd's fork takes the\n+\t * threadgroup rwsem that a cgroup move of this task holds while it\n+\t * waits for this task to enable migration: see the comment at the top.\n+\t */\n+\tmigrate_enable();\n+\ttask = kthread_create(bpf_kthread_fn, kthread, \"bpfk/%u:%u\", aux-\u003eid,\n+\t\t\t      atomic_fetch_inc(\u0026aux-\u003ekthread_seq));\n+\tif (IS_ERR(task)) {\n+\t\tmigrate_disable();\n+\t\tkfree(kthread);\n+\t\treturn NULL;\n+\t}\n+\tkthread-\u003etask = get_task_struct(task);\n+\t/*\n+\t * The program's reference is the kind bpf_task_acquire() takes and\n+\t * bpf_task_release() drops; the object's own, above, is the kind\n+\t * kthread_stop_put() drops. Take it before the thread is published:\n+\t * from then on any program may stop it, and a stopped thread's exit\n+\t * drops the last reference the task had of its own.\n+\t */\n+\trefcount_inc(\u0026task-\u003ercu_users);\n+\n+\t/*\n+\t * Run the thread up to its park. The generic kthread prologue, which\n+\t * runs first, resets the affinity to the default for the thread's\n+\t * node, so that an affinity set from now on sticks.\n+\t */\n+\tkthread_park(task);\n+\tmigrate_disable();\n+\n+\t/*\n+\t * Publish the thread while the program has a reference left to take.\n+\t * Once it has none, the release of its last reference is pending or\n+\t * under way, and a thread published after that release looked would\n+\t * survive the program. The reference taken here is dropped again at\n+\t * once: the release cannot start before it is, and the thread is on\n+\t * the lists by then.\n+\t */\n+\traw_spin_lock_irqsave(\u0026bpf_kthreads_lock, irqflags);\n+\tprog = bpf_prog_inc_not_zero(aux-\u003eprog);\n+\tif (IS_ERR(prog)) {\n+\t\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\n+\t\tmigrate_enable();\n+\t\tbpf_kthread_stop_and_free(kthread);\n+\t\tmigrate_disable();\n+\t\tput_task_struct_rcu_user(task);\n+\t\treturn NULL;\n+\t}\n+\tlist_add_tail(\u0026kthread-\u003enode, \u0026bpf_kthreads);\n+\tlist_add_tail(\u0026kthread-\u003eprog_node, \u0026aux-\u003ekthreads);\n+\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\n+\tbpf_prog_put(prog);\n+\treturn task;\n+}\n+\n+/**\n+ * bpf_kthread_start - Run the callback of a BPF kthread\n+ * @task: a thread created by bpf_kthread_create()\n+ * @flags: must be 0\n+ *\n+ * Starts the thread, which then runs its callback. Does nothing for a thread\n+ * that has been started already.\n+ *\n+ * Return: 0 on success, -EINVAL if @flags is not 0, -ESRCH if @task is not a\n+ * BPF kthread or has been stopped.\n+ */\n+__bpf_kfunc int bpf_kthread_start(struct task_struct *task, u64 flags)\n+{\n+\tunsigned long irqflags;\n+\tbool found;\n+\n+\tif (flags)\n+\t\treturn -EINVAL;\n+\n+\traw_spin_lock_irqsave(\u0026bpf_kthreads_lock, irqflags);\n+\tfound = !!bpf_kthread_find(task);\n+\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\n+\tif (!found)\n+\t\treturn -ESRCH;\n+\n+\tkthread_unpark(task);\n+\treturn 0;\n+}\n+\n+/**\n+ * bpf_kthread_bind - Restrict a BPF kthread to one CPU\n+ * @task: a thread created by bpf_kthread_create()\n+ * @cpu: the CPU the thread is to run on\n+ * @flags: must be 0\n+ *\n+ * Sets the thread's affinity to @cpu. A thread that has not been started wakes\n+ * on @cpu; one that has moves there at its next park or return, since its\n+ * callback runs with migration disabled, and the call waits for that with\n+ * migration enabled, so the caller may return on another CPU and a thread may\n+ * bind itself. Unlike kthread_bind(), this leaves the affinity changeable from\n+ * user space and subject to cpusets, like that of any other task. The binding\n+ * is lost when @cpu goes offline or when the cpuset of the thread's cgroup\n+ * changes its CPUs: the thread is moved, or placed elsewhere at its next\n+ * wakeup, and its affinity reset to the cpuset's CPUs, as a user task's is\n+ * after an offline. It is also lost when the housekeeping CPUs change, which\n+ * resets the affinity of every kernel thread that is not bound for good. A\n+ * program that needs the binding re-applies it.\n+ *\n+ * Return: 0 on success, -EINVAL if @flags is not 0 or @cpu is not a possible\n+ * CPU or is offline, -ESRCH if @task is not a BPF kthread or has been stopped.\n+ */\n+__bpf_kfunc int bpf_kthread_bind(struct task_struct *task, u32 cpu, u64 flags)\n+{\n+\tunsigned long irqflags;\n+\tbool found;\n+\tint ret;\n+\n+\tif (flags)\n+\t\treturn -EINVAL;\n+\tif (cpu \u003e= nr_cpu_ids || !cpu_possible(cpu))\n+\t\treturn -EINVAL;\n+\n+\traw_spin_lock_irqsave(\u0026bpf_kthreads_lock, irqflags);\n+\tfound = !!bpf_kthread_find(task);\n+\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\n+\tif (!found)\n+\t\treturn -ESRCH;\n+\n+\t/* A running thread moves at its next park: see the comment at the top. */\n+\tmigrate_enable();\n+\tret = set_cpus_allowed_ptr(task, cpumask_of(cpu));\n+\tmigrate_disable();\n+\treturn ret;\n+}\n+\n+#ifdef CONFIG_CGROUPS\n+/**\n+ * bpf_kthread_attach_cgroup - Move a BPF kthread into a cgroup\n+ * @task: a thread created by bpf_kthread_create()\n+ * @cgrp: the cgroup to move it into\n+ * @flags: must be 0\n+ *\n+ * Moves the thread into @cgrp, started or not, so that its CPU time and the\n+ * pressure it causes are accounted to @cgrp and its affinity follows @cgrp's\n+ * cpuset. Until then it runs in the root cgroup, as a kernel thread does.\n+ * Kernel memory the thread allocates is still not charged to @cgrp, since\n+ * the allocations of a kernel thread are charged to no memory cgroup. The\n+ * thread is a member of @cgrp like any process: @cgrp cannot be removed until\n+ * the thread leaves or exits, and @cgrp has to be able to host processes, so\n+ * on cgroup v2 it may not have controllers enabled in its children. Like any\n+ * kernel thread it is not frozen with @cgrp, which then never reports frozen.\n+ * A cpuset applies its CPUs to the thread as it joins, so bind the\n+ * thread with bpf_kthread_bind() afterwards, not before. A running thread\n+ * joins a cpuset at its next park, and the move waits for that with\n+ * migration enabled, holding cgroup_mutex meanwhile, so the caller may return\n+ * on another CPU and a thread may move itself. A thread whose callback has\n+ * returned but that has not been stopped is left where it is and the call\n+ * reports success, as cgroup.procs does for an exiting task.\n+ *\n+ * Return: 0 on success, -EINVAL if @flags is not 0, -ESRCH if @task is not a\n+ * BPF kthread or has been stopped, -ENODEV if @cgrp is being removed, -EBUSY\n+ * or -EOPNOTSUPP if @cgrp cannot host processes, or an error of the move.\n+ */\n+__bpf_kfunc int bpf_kthread_attach_cgroup(struct task_struct *task, struct cgroup *cgrp,\n+\t\t\t\t\t  u64 flags)\n+{\n+\tunsigned long irqflags;\n+\tbool found;\n+\tint ret;\n+\n+\tif (flags)\n+\t\treturn -EINVAL;\n+\n+\traw_spin_lock_irqsave(\u0026bpf_kthreads_lock, irqflags);\n+\tfound = !!bpf_kthread_find(task);\n+\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\n+\tif (!found)\n+\t\treturn -ESRCH;\n+\n+\t/* A running thread joins at its next park: see the comment at the top. */\n+\tmigrate_enable();\n+\tret = cgroup_attach_kthread(cgrp, task);\n+\tmigrate_disable();\n+\treturn ret;\n+}\n+#endif\n+\n+/**\n+ * bpf_kthread_stop - Stop a BPF kthread and wait for it to exit\n+ * @task: a thread created by bpf_kthread_create()\n+ * @flags: must be 0\n+ *\n+ * Makes bpf_kthread_should_stop() return true in the thread, ends the sleep it\n+ * is in, and waits for its callback to return. A thread that has not been\n+ * started exits without running its callback. The thread is gone afterwards;\n+ * the reference the program holds on @task stays its own to release.\n+ *\n+ * The wait ends when the callback returns, which the callback decides by\n+ * polling bpf_kthread_should_stop(); the kernel cannot end it sooner. It is\n+ * made with migration enabled, so the caller may return on another CPU, but\n+ * under the Tasks Trace read section, which every Tasks Trace grace period\n+ * then waits for, so a thread should stop threads that return promptly, as\n+ * one that sleeps in bpf_task_park() does. Two threads stopping each other, or\n+ * a longer ring of them, would wait forever; the stop that would close such a\n+ * ring is refused instead.\n+ *\n+ * Return: what the thread's callback returned, or -EINTR if it never ran.\n+ * -EINVAL if @flags is not 0, -EDEADLK if @task is the calling thread or is,\n+ * through the threads it is itself stopping, waiting for the calling thread,\n+ * -ESRCH if @task is not a BPF kthread, has been stopped already, or is being\n+ * stopped by the release of its program.\n+ */\n+__bpf_kfunc int bpf_kthread_stop(struct task_struct *task, u64 flags)\n+{\n+\tstruct bpf_kthread *kthread, *self;\n+\tunsigned long irqflags;\n+\tstruct bpf_prog *prog;\n+\tint ret;\n+\n+\tif (flags)\n+\t\treturn -EINVAL;\n+\tif (task == current)\n+\t\treturn -EDEADLK;\n+\n+\traw_spin_lock_irqsave(\u0026bpf_kthreads_lock, irqflags);\n+\tkthread = bpf_kthread_find(task);\n+\tif (!kthread) {\n+\t\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\n+\t\treturn -ESRCH;\n+\t}\n+\tif (bpf_kthread_waits_for_current(kthread)) {\n+\t\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\n+\t\treturn -EDEADLK;\n+\t}\n+\t/*\n+\t * Hold the program while the thread is off the lists and not yet\n+\t * gone. The release of the program's last reference stops only the\n+\t * threads it finds on its list, and this one may be asleep in\n+\t * bpf_task_park(), outside the read section that otherwise keeps the\n+\t * program for it, so without the reference it could wake into a freed\n+\t * program. A program with no reference left is being released, and\n+\t * that release is stopping the thread.\n+\t */\n+\tprog = bpf_prog_inc_not_zero(kthread-\u003eaux-\u003eprog);\n+\tif (IS_ERR(prog)) {\n+\t\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\n+\t\treturn -ESRCH;\n+\t}\n+\tbpf_kthread_unlink(kthread);\n+\tself = bpf_kthread_self();\n+\tif (self)\n+\t\tself-\u003ewaits_for = kthread;\n+\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\n+\n+\t/* The wait for the exit: see the comment at the top. */\n+\tmigrate_enable();\n+\tret = kthread_stop_put(kthread-\u003etask);\n+\tmigrate_disable();\n+\n+\tif (self) {\n+\t\traw_spin_lock_irqsave(\u0026bpf_kthreads_lock, irqflags);\n+\t\tself-\u003ewaits_for = NULL;\n+\t\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\n+\t}\n+\tkfree(kthread);\n+\tbpf_prog_put(prog);\n+\treturn ret;\n+}\n+\n+/**\n+ * bpf_kthread_should_stop - Whether the calling BPF kthread was asked to stop\n+ *\n+ * The callback of a BPF kthread checks this in its loop and returns when it is\n+ * set, as a kernel thread function does; bpf_kthread_stop() waits for that.\n+ *\n+ * Return: true in a kernel thread that was asked to stop, by bpf_kthread_stop()\n+ * or by the release of its program, false otherwise, including for every\n+ * thread of a user process.\n+ */\n+__bpf_kfunc bool bpf_kthread_should_stop(void)\n+{\n+\treturn (current-\u003eflags \u0026 PF_KTHREAD) \u0026\u0026 kthread_should_stop();\n+}\n+\n+__bpf_kfunc_end_defs();\n+\n+BTF_KFUNCS_START(bpf_kthread_kfunc_ids)\n+BTF_ID_FLAGS(func, bpf_kthread_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE | KF_IMPLICIT_ARGS)\n+BTF_ID_FLAGS(func, bpf_kthread_start, KF_RCU)\n+BTF_ID_FLAGS(func, bpf_kthread_bind, KF_SLEEPABLE)\n+#ifdef CONFIG_CGROUPS\n+BTF_ID_FLAGS(func, bpf_kthread_attach_cgroup, KF_SLEEPABLE)\n+#endif\n+BTF_ID_FLAGS(func, bpf_kthread_stop, KF_SLEEPABLE)\n+BTF_ID_FLAGS(func, bpf_kthread_should_stop)\n+BTF_KFUNCS_END(bpf_kthread_kfunc_ids)\n+\n+static const struct btf_kfunc_id_set bpf_kthread_kfunc_set = {\n+\t.owner = THIS_MODULE,\n+\t.set = \u0026bpf_kthread_kfunc_ids,\n+};\n+\n+static int __init bpf_kthread_kfunc_init(void)\n+{\n+\treturn register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, \u0026bpf_kthread_kfunc_set);\n+}\n+late_initcall(bpf_kthread_kfunc_init);\ndiff --git a/kernel/bpf/states.c b/kernel/bpf/states.c\nindex efc0870b85c2a3..d0db8074928a0e 100644\n--- a/kernel/bpf/states.c\n+++ b/kernel/bpf/states.c\n@@ -745,7 +745,8 @@ bool bpf_state_subsumes(struct bpf_verifier_env *env, struct bpf_verifier_state\n \tif (old-\u003ein_sleepable != cur-\u003ein_sleepable)\n \t\treturn false;\n \n-\tif (old-\u003eunwound != cur-\u003eunwound || old-\u003echanged_pkt_data != cur-\u003echanged_pkt_data)\n+\tif (old-\u003eunwound != cur-\u003eunwound || old-\u003echanged_pkt_data != cur-\u003echanged_pkt_data ||\n+\t    old-\u003eparked != cur-\u003eparked)\n \t\treturn false;\n \n \tif (!refsafe(old, cur, \u0026env-\u003eidmap_scratch))\n@@ -758,6 +759,13 @@ bool bpf_state_subsumes(struct bpf_verifier_env *env, struct bpf_verifier_state\n \t\tinsn_idx = bpf_frame_insn_idx(old, i);\n \t\tif (old-\u003eframe[i]-\u003ecallsite != cur-\u003eframe[i]-\u003ecallsite)\n \t\t\treturn false;\n+\t\t/*\n+\t\t * The body of a BPF kthread may park where another async\n+\t\t * callback of the same subprog may not, so a state verified\n+\t\t * in one role must not prune the other.\n+\t\t */\n+\t\tif (old-\u003eframe[i]-\u003ein_kthread_fn != cur-\u003eframe[i]-\u003ein_kthread_fn)\n+\t\t\treturn false;\n \t\tif (!func_states_equal(env, old-\u003eframe[i], cur-\u003eframe[i], insn_idx))\n \t\t\treturn false;\n \t}\ndiff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c\nindex 9004f2673ccf96..9ea8c755651efe 100644\n--- a/kernel/bpf/syscall.c\n+++ b/kernel/bpf/syscall.c\n@@ -2512,6 +2512,12 @@ static void bpf_prog_put_deferred(struct work_struct *work)\n \n \taux = container_of(work, struct bpf_prog_aux, work);\n \tprog = aux-\u003eprog;\n+\t/*\n+\t * The threads of the prog still run its code, each inside a Tasks\n+\t * Trace RCU read section that the grace period below would wait for.\n+\t * Stop them first; this waits for each of them to exit.\n+\t */\n+\tbpf_prog_free_kthreads(aux);\n \tperf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);\n \tbpf_audit_prog(prog, BPF_AUDIT_UNLOAD);\n \tbpf_prog_free_id(prog);\n@@ -2523,7 +2529,8 @@ static void __bpf_prog_put(struct bpf_prog *prog)\n \tstruct bpf_prog_aux *aux = prog-\u003eaux;\n \n \tif (atomic64_dec_and_test(\u0026aux-\u003erefcnt)) {\n-\t\tif (in_hardirq() || irqs_disabled()) {\n+\t\t/* Stopping the prog's threads blocks, which the direct path may not. */\n+\t\tif (in_hardirq() || irqs_disabled() || !list_empty(\u0026aux-\u003ekthreads)) {\n \t\t\tINIT_WORK(\u0026aux-\u003ework, bpf_prog_put_deferred);\n \t\t\tschedule_work(\u0026aux-\u003ework);\n \t\t} else {\n@@ -6661,8 +6668,9 @@ int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)\n \t\tif (IS_ERR(prog))\n \t\t\treturn PTR_ERR(prog);\n \n+\t\t/* The run is counted per CPU, which a program that migrates would unbalance. */\n \t\tif (attr-\u003etest.ctx_size_in \u003c prog-\u003eaux-\u003emax_ctx_offset ||\n-\t\t    attr-\u003etest.ctx_size_in \u003e U16_MAX) {\n+\t\t    attr-\u003etest.ctx_size_in \u003e U16_MAX || prog-\u003eaux-\u003emay_migrate) {\n \t\t\tbpf_prog_put(prog);\n \t\t\treturn -EINVAL;\n \t\t}\ndiff --git a/kernel/bpf/task_park.c b/kernel/bpf/task_park.c\nnew file mode 100644\nindex 00000000000000..b6980c1cc1bf0b\n--- /dev/null\n+++ b/kernel/bpf/task_park.c\n@@ -0,0 +1,311 @@\n+// SPDX-License-Identifier: GPL-2.0-only\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \u003clinux/bpf.h\u003e\n+#include \u003clinux/btf.h\u003e\n+#include \u003clinux/btf_ids.h\u003e\n+#include \u003clinux/hrtimer.h\u003e\n+#include \u003clinux/irq_work.h\u003e\n+#include \u003clinux/kthread.h\u003e\n+#include \u003clinux/rcupdate_trace.h\u003e\n+#include \u003clinux/sched.h\u003e\n+#include \u003clinux/sched/signal.h\u003e\n+#include \u003clinux/sched/wake_q.h\u003e\n+\n+/*\n+ * A BPF program that blocks for an event keeps its own queue of waiters and\n+ * its own notion of when the event has arrived. The kernel contributes two\n+ * operations over the task: bpf_task_park() puts the current task to sleep\n+ * and bpf_task_wake() makes a task runnable. The only shared state is one\n+ * word in the task, bpf_wake_pending, which lets the program check its\n+ * condition outside the kernel without losing a wake that lands between\n+ * the check and the sleep: the waker sets the word before it wakes the\n+ * task, and a park that finds it set returns instead of sleeping.\n+ *\n+ * The word also carries the ordering the program relies on. A waker\n+ * publishes its condition and then calls bpf_task_wake(); a waiter reads\n+ * the condition, finds it unmet, calls bpf_task_park(), and reads the\n+ * condition again after the return. The second read must see the waker's\n+ * condition whenever the park returned because of that wake, so the store\n+ * of the word and the load that consumes it are both an xchg(): the store\n+ * is fully ordered after the waker's condition stores, the load before the\n+ * waiter's later condition loads. A release would not do for the store. Of\n+ * two wakers' stores one overwrites the other, the waiter observes only the\n+ * later one, and nothing would order the earlier waker's condition before\n+ * the waiter's read of it. The xchg() also makes the consume atomic: a wake\n+ * that lands between the waiter's observation of the word and its reset is\n+ * not erased but stays pending for the next park.\n+ */\n+\n+/*\n+ * A wake asked for with IRQs disabled cannot be issued there: the caller may\n+ * have interrupted the scheduler with the task's pi_lock or a runqueue lock\n+ * held, or may run in NMI context, where try_to_wake_up() may take neither.\n+ * Such wakes are queued on the CPU and issued from irq_work once IRQs are\n+ * enabled again.\n+ *\n+ * The queue goes through a node of its own in the task, under the rule the\n+ * scheduler's wake queues follow for theirs: a task is on at most one queue,\n+ * whoever claimed the node wakes the task, and a waker that finds it claimed\n+ * leaves the wake to the claimant. The pending word is set before the claim\n+ * is tried, so a wake left this way still reaches a parked task through the\n+ * claimant's wake, and any other task through its next park.\n+ *\n+ * The task's wake_q node would save the field, but whoever claims that node\n+ * may turn away a futex, rwsem or semaphore wakeup that then relies on the\n+ * claimant, and so has to wake with TASK_NORMAL. A program could then aim a\n+ * wake of an uninterruptible sleep at any task it names, and not every such\n+ * sleep has a loop around it: a new kthread sleeps once until its creator\n+ * wakes it. With its own node the drain wakes interruptible sleeps only, as\n+ * the direct wake does.\n+ *\n+ * The queue is a stack with a per-CPU head: pushed with cmpxchg() from\n+ * whatever context asks, emptied with one xchg() by the irq_work, so a push\n+ * that interrupts the drain lands on an empty stack and queues the work\n+ * again.\n+ */\n+static DEFINE_PER_CPU(struct wake_q_node *, bpf_task_wake_queue);\n+\n+static void bpf_task_wake_deferred(struct irq_work *work)\n+{\n+\tstruct wake_q_node *node = xchg(this_cpu_ptr(\u0026bpf_task_wake_queue), NULL);\n+\n+\twhile (node) {\n+\t\tstruct task_struct *task = container_of(node, struct task_struct, bpf_wake_node);\n+\n+\t\tnode = node-\u003enext == WAKE_Q_TAIL ? NULL : node-\u003enext;\n+\t\t/* The task may be queued again from here on. */\n+\t\tWRITE_ONCE(task-\u003ebpf_wake_node.next, NULL);\n+\t\twake_up_state(task, TASK_INTERRUPTIBLE);\n+\t\tput_task_struct(task);\n+\t}\n+}\n+\n+static DEFINE_PER_CPU(struct irq_work, bpf_task_wake_work) =\n+\tIRQ_WORK_INIT_HARD(bpf_task_wake_deferred);\n+\n+static void bpf_task_wake_defer(struct task_struct *task)\n+{\n+\tstruct wake_q_node *node = \u0026task-\u003ebpf_wake_node, *head;\n+\n+\t/*\n+\t * The claim, as in __wake_q_add(): NULL is free, anything else is\n+\t * queued. The xchg() that stored the pending word is a full barrier\n+\t * between that store and the claim, so a claim lost to another holder\n+\t * is still covered: the holder's wake follows its own reset of the\n+\t * node, which this claim did not observe, so the task it wakes\n+\t * observes the word.\n+\t */\n+\tif (cmpxchg_relaxed(\u0026node-\u003enext, NULL, WAKE_Q_TAIL))\n+\t\treturn;\n+\tget_task_struct(task);\n+\thead = this_cpu_read(bpf_task_wake_queue);\n+\tdo {\n+\t\tWRITE_ONCE(node-\u003enext, head ?: WAKE_Q_TAIL);\n+\t} while (!try_cmpxchg(this_cpu_ptr(\u0026bpf_task_wake_queue), \u0026head, node));\n+\tirq_work_queue(this_cpu_ptr(\u0026bpf_task_wake_work));\n+}\n+\n+/*\n+ * What ends a park besides a wake and the timeout: a stop or park request for\n+ * a kthread, or a signal. Both are checked for every caller, since a kthread\n+ * that allowed signals has them too, and the kthread checks are false for a\n+ * user task. The request is reported apart from a signal because nothing\n+ * clears it while the thread's function runs: every later park that finds no\n+ * wake pending fails the same way, so the caller has to return.\n+ */\n+static int bpf_task_park_interrupted(void)\n+{\n+\tif (kthread_should_stop_or_park())\n+\t\treturn -ESHUTDOWN;\n+\tif (signal_pending(current))\n+\t\treturn -EINTR;\n+\treturn 0;\n+}\n+\n+__bpf_kfunc_start_defs();\n+\n+/**\n+ * bpf_task_park - Sleep until bpf_task_wake() is called for the current task\n+ * @timeout_ns: relative timeout, or U64_MAX for none\n+ * @flags: must be 0\n+ *\n+ * Returns at once if a wake is pending from a bpf_task_wake() that found the\n+ * task running. Otherwise sleeps interruptibly until a wake, the timeout, a\n+ * signal, or a stop or park request for a kthread. Every return past the\n+ * argument check consumes the pending wake, and a wake takes precedence over\n+ * a timeout, a signal or a stop request noticed at the same time, since the\n+ * waker may already have accounted for it. Any return hands control back to\n+ * the program's own check of its condition, and spurious returns are\n+ * possible. A @timeout_ns of 0 polls: it reports a pending wake,\n+ * then a stop or park request as -ESHUTDOWN, a pending signal as -EINTR, and\n+ * otherwise -ETIMEDOUT, without sleeping.\n+ *\n+ * Return: 0 when a wake was consumed or the sleep ended for no recorded\n+ * reason, -ETIMEDOUT when the timeout passed, -ESHUTDOWN when a stop or park\n+ * request for a kthread is pending and -EINTR when a signal is, whether it\n+ * was found before the sleep or ended it, even if the timeout passed\n+ * meanwhile, -EINVAL when @flags is not 0. Nothing clears a stop or park\n+ * request while the thread's function runs, so once a call has returned\n+ * -ESHUTDOWN, every later one that finds no wake pending does too.\n+ */\n+__bpf_kfunc int bpf_task_park(u64 timeout_ns, u64 flags)\n+{\n+\tktime_t expires = min_t(u64, timeout_ns, KTIME_MAX);\n+\tbool expired;\n+\tint ret = 0;\n+\n+\tif (flags)\n+\t\treturn -EINVAL;\n+\tif (READ_ONCE(current-\u003ebpf_wake_pending))\n+\t\tgoto out;\n+\n+\t/*\n+\t * set_current_state() is a full barrier that pairs with the one in\n+\t * try_to_wake_up() after the waker's store of bpf_wake_pending:\n+\t * either this task observes the pending wake below or the waker\n+\t * observes it sleeping and wakes it.\n+\t */\n+\tset_current_state(TASK_INTERRUPTIBLE);\n+\tif (READ_ONCE(current-\u003ebpf_wake_pending)) {\n+\t\t__set_current_state(TASK_RUNNING);\n+\t\tgoto out;\n+\t}\n+\t/*\n+\t * kthread_stop(), kthread_park() and signal delivery set their flag\n+\t * and wake the task in the same way, so the same ordering covers\n+\t * them.\n+\t */\n+\tret = bpf_task_park_interrupted();\n+\tif (ret) {\n+\t\t__set_current_state(TASK_RUNNING);\n+\t\tgoto out;\n+\t}\n+\n+\t/*\n+\t * Sleepable programs run with migration disabled under a Tasks Trace\n+\t * RCU read section. Neither may be held across a sleep of unbounded\n+\t * length: the read section would stall every Tasks Trace grace period\n+\t * in the system, and a migration-disabled sleeper keeps rq-\u003enr_pinned\n+\t * raised, which blocks CPU offline in balance_hotplug_wait() and pins\n+\t * the wakeup to the CPU it slept on.\n+\t *\n+\t * Dropping both is correct only where the caller's runner holds the\n+\t * program, and with it its maps and context, for the whole\n+\t * invocation, so that the program's own memory needs no grace period,\n+\t * and where nothing on the run path keeps per-CPU state across the\n+\t * program. A program that keeps such state itself, a stack in arena or\n+\t * the time slice of may_goto, is refused the call, and one that calls\n+\t * it is kept out of prog arrays, where the caller of a tail call holds\n+\t * it through this very read section. The verifier confines the call to\n+\t * such contexts: the main body of a syscall program or of a BPF kthread\n+\t * and the functions either calls directly, never any other callback,\n+\t * and never a region that pins the CPU, a lock, a saved IRQ state or an\n+\t * RCU read section. It also invalidates every pointer the program holds\n+\t * across the call except its stack, its context, maps it references\n+\t * directly and the values of its arrays, arena memory and\n+\t * verifier-tracked references. What remains is that the program may\n+\t * resume on another CPU, which is part of the contract.\n+\t */\n+\tmigrate_enable();\n+\trcu_read_unlock_trace();\n+\t/*\n+\t * A NULL expiry sleeps without a timer; a zero one returns at once.\n+\t * schedule_hrtimeout() tells an expiry from an early return, but not\n+\t * a wake from a signal or a stop or park request, so read the flags\n+\t * again, as before the sleep. They win over an expiry that fired at\n+\t * the same time, since the request stays pending and the timeout\n+\t * does not; a wake, consumed below, wins over both.\n+\t */\n+\texpired = !schedule_hrtimeout(timeout_ns == U64_MAX ? NULL : \u0026expires, HRTIMER_MODE_REL);\n+\tret = bpf_task_park_interrupted();\n+\tif (!ret \u0026\u0026 expired)\n+\t\tret = -ETIMEDOUT;\n+\trcu_read_lock_trace();\n+\tmigrate_disable();\n+out:\n+\t/*\n+\t * Consume the wake. xchg() is fully ordered, so the condition loads\n+\t * the program makes after the return see the stores the waker made\n+\t * before its bpf_task_wake(). A wake that raced with the timeout, the\n+\t * signal or the stop request wins: the waker may have accounted for it.\n+\t */\n+\tif (xchg(\u0026current-\u003ebpf_wake_pending, 0))\n+\t\tret = 0;\n+\treturn ret;\n+}\n+\n+/**\n+ * bpf_task_wake - Wake a task parked in bpf_task_park()\n+ * @task: task to wake\n+ * @flags: must be 0\n+ *\n+ * Marks a wake pending for @task and wakes it if it sleeps interruptibly.\n+ * A task that is not parked keeps the pending wake until its next\n+ * bpf_task_park(), which then returns at once.\n+ *\n+ * The wake takes the task's pi_lock and a runqueue lock, which the kernel\n+ * code a program runs in or interrupts may already hold, and it may not be\n+ * issued in NMI context at all. Such code and NMI context both run with IRQs\n+ * disabled, so a call with IRQs disabled marks the wake pending and queues it\n+ * for irq_work on the calling CPU, which issues it once IRQs are enabled\n+ * again. Such a call cannot tell whether the task was asleep and returns 2.\n+ *\n+ * Return: 1 when @task was asleep and is now runnable, 0 when it was not, 2\n+ * when the wake was deferred, -EINVAL when @flags is not 0.\n+ */\n+__bpf_kfunc int bpf_task_wake(struct task_struct *task, u64 flags)\n+{\n+\tif (flags)\n+\t\treturn -EINVAL;\n+\n+\t/*\n+\t * xchg() is a full barrier. It orders the program's stores before this\n+\t * call, its condition, before the word, so a park that consumes the\n+\t * word sees them, also when what it consumes is another waker's store\n+\t * that overwrote this one. try_to_wake_up() then orders the store\n+\t * before its load of the task state, pairing with set_current_state()\n+\t * in bpf_task_park().\n+\t */\n+\txchg(\u0026task-\u003ebpf_wake_pending, 1);\n+\tif (unlikely(irqs_disabled())) {\n+\t\tbpf_task_wake_defer(task);\n+\t\treturn 2;\n+\t}\n+\treturn wake_up_state(task, TASK_INTERRUPTIBLE);\n+}\n+\n+__bpf_kfunc_end_defs();\n+\n+BTF_KFUNCS_START(bpf_task_park_kfunc_ids)\n+BTF_ID_FLAGS(func, bpf_task_park, KF_SLEEPABLE)\n+BTF_KFUNCS_END(bpf_task_park_kfunc_ids)\n+\n+BTF_KFUNCS_START(bpf_task_wake_kfunc_ids)\n+BTF_ID_FLAGS(func, bpf_task_wake, KF_RCU)\n+BTF_KFUNCS_END(bpf_task_wake_kfunc_ids)\n+\n+static const struct btf_kfunc_id_set bpf_task_park_kfunc_set = {\n+\t.owner = THIS_MODULE,\n+\t.set = \u0026bpf_task_park_kfunc_ids,\n+};\n+\n+static const struct btf_kfunc_id_set bpf_task_wake_kfunc_set = {\n+\t.owner = THIS_MODULE,\n+\t.set = \u0026bpf_task_wake_kfunc_ids,\n+};\n+\n+/*\n+ * The park stays with syscall programs, whose runner holds the program across\n+ * the sleep. The wake touches only the task and defers itself where it cannot\n+ * be issued, so every program type has it, to wake a parked task from the\n+ * kernel event that makes it worth waking.\n+ */\n+static int __init bpf_task_park_kfunc_init(void)\n+{\n+\tint ret;\n+\n+\tret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, \u0026bpf_task_park_kfunc_set);\n+\treturn ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026bpf_task_wake_kfunc_set);\n+}\n+late_initcall(bpf_task_park_kfunc_init);\ndiff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c\nindex 20b1b9ff574c7d..e7769999e8c377 100644\n--- a/kernel/bpf/verifier.c\n+++ b/kernel/bpf/verifier.c\n@@ -537,6 +537,7 @@ static bool is_call_rcu_kfunc(u32 btf_id);\n static bool is_callback_calling_kfunc(u32 btf_id);\n \n static bool is_bpf_wq_set_callback_kfunc(u32 btf_id);\n+static bool is_bpf_kthread_create_kfunc(u32 btf_id);\n static bool is_task_work_add_kfunc(u32 func_id);\n \n static bool is_sync_callback_calling_function(enum bpf_func_id func_id)\n@@ -580,9 +581,10 @@ static bool is_async_cb_sleepable(struct bpf_verifier_env *env, struct bpf_insn\n \tif (bpf_pseudo_kfunc_call(insn) \u0026\u0026 insn-\u003eoff == 0 \u0026\u0026 is_call_rcu_kfunc(insn-\u003eimm))\n \t\treturn false;\n \n-\t/* bpf_wq and bpf_task_work callbacks are always sleepable. */\n+\t/* bpf_wq, bpf_task_work and bpf_kthread callbacks are always sleepable. */\n \tif (bpf_pseudo_kfunc_call(insn) \u0026\u0026 insn-\u003eoff == 0 \u0026\u0026\n-\t    (is_bpf_wq_set_callback_kfunc(insn-\u003eimm) || is_task_work_add_kfunc(insn-\u003eimm)))\n+\t    (is_bpf_wq_set_callback_kfunc(insn-\u003eimm) || is_task_work_add_kfunc(insn-\u003eimm) ||\n+\t     is_bpf_kthread_create_kfunc(insn-\u003eimm)))\n \t\treturn true;\n \n \tverifier_bug(env, \"unhandled async callback in is_async_cb_sleepable\");\n@@ -1792,6 +1794,7 @@ int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,\n \tdst_state-\u003ecallers_unknown = src-\u003ecallers_unknown;\n \tdst_state-\u003eunwound = src-\u003eunwound;\n \tdst_state-\u003echanged_pkt_data = src-\u003echanged_pkt_data;\n+\tdst_state-\u003eparked = src-\u003eparked;\n \tdst_state-\u003eflow_entry = src-\u003eflow_entry;\n \tdst_state-\u003einsn_idx = src-\u003einsn_idx;\n \tdst_state-\u003ecurframe = src-\u003ecurframe;\n@@ -11222,6 +11225,136 @@ static void invalidate_non_owning_refs(struct bpf_verifier_env *env)\n \t}));\n }\n \n+/*\n+ * bpf_task_park() is confined to the main body of the program, the body of a\n+ * BPF kthread, and the functions either calls directly. Any other callback\n+ * runs from kernel code that holds state of its own around the call, an\n+ * iteration position, a work item or timer that owns the program only through\n+ * Tasks Trace RCU, an exiting task, none of it audited against a sleep that\n+ * drops the program's protection. Refuse the park in any such callback frame,\n+ * synchronous, asynchronous or exception. Asynchronous callbacks are verified\n+ * as a state of their own with the callback as frame 0, so the walk over the\n+ * frames sees them too.\n+ *\n+ * The body of a BPF kthread is the exception because its caller,\n+ * bpf_kthread_fn(), is the whole of the thread: it brackets the callback the\n+ * way the runner of a syscall program brackets the program, keeps no state\n+ * across it, and the program's release stops the thread and waits for it\n+ * before anything the callback uses is freed. Callbacks nested under the\n+ * body are refused as anywhere else.\n+ */\n+static bool in_callback_frames(struct bpf_verifier_env *env)\n+{\n+\tstruct bpf_verifier_state *vstate = env-\u003ecur_state;\n+\tint i;\n+\n+\tfor (i = 0; i \u003c= vstate-\u003ecurframe; i++) {\n+\t\tstruct bpf_func_state *frame = vstate-\u003eframe[i];\n+\n+\t\tif (frame-\u003ein_kthread_fn)\n+\t\t\tcontinue;\n+\t\tif (frame-\u003ein_callback_fn || frame-\u003ein_async_callback_fn ||\n+\t\t    frame-\u003ein_exception_callback_fn)\n+\t\t\treturn true;\n+\t}\n+\treturn false;\n+}\n+\n+/*\n+ * bpf_task_park() sleeps without the Tasks Trace RCU read section and the\n+ * disabled migration that a sleepable program otherwise runs under. A dynptr\n+ * or an iterator on the stack cannot simply be invalidated across such a\n+ * sleep: the program must still release or destroy it, and an object that\n+ * stays usable may point at memory the sleep lets go. Refuse the park while\n+ * any is live, except a bpf_iter_num, whose state lives entirely on the stack\n+ * and whose next and destroy touch nothing else.\n+ */\n+static int check_park_stack_objects(struct bpf_verifier_env *env, int insn_idx)\n+{\n+\tstruct bpf_verifier_state *vstate = env-\u003ecur_state;\n+\tint i, spi;\n+\n+\tfor (i = 0; i \u003c= vstate-\u003ecurframe; i++) {\n+\t\tstruct bpf_func_state *frame = vstate-\u003eframe[i];\n+\n+\t\tfor (spi = 0; spi \u003c bpf_stack_nr_slots(frame); spi++) {\n+\t\t\tstruct bpf_stack_state *slot = bpf_stack_slot(frame, spi);\n+\t\t\tstruct bpf_reg_state *st = \u0026slot-\u003espilled_ptr;\n+\n+\t\t\tswitch (slot-\u003eslot_type[BPF_REG_SIZE - 1]) {\n+\t\t\tcase STACK_DYNPTR:\n+\t\t\t\t/* Report the slot the program named, not the second half. */\n+\t\t\t\tif (!st-\u003edynptr.first_slot)\n+\t\t\t\t\tcontinue;\n+\t\t\t\tverbose(env, \"bpf_task_park() with a live dynptr at frame %d fp%d\\n\",\n+\t\t\t\t\ti, -(spi + 1) * BPF_REG_SIZE);\n+\t\t\t\tbreak;\n+\t\t\tcase STACK_ITER:\n+\t\t\t\tif (!st-\u003eid)\n+\t\t\t\t\tcontinue;\n+\t\t\t\tif (st-\u003eiter.btf == btf_vmlinux \u0026\u0026\n+\t\t\t\t    !strcmp(iter_type_str(st-\u003eiter.btf, st-\u003eiter.btf_id), \"num\"))\n+\t\t\t\t\tcontinue;\n+\t\t\t\tverbose(env, \"bpf_task_park() with a live %s iterator at frame %d fp%d\\n\",\n+\t\t\t\t\titer_type_str(st-\u003eiter.btf, st-\u003eiter.btf_id), i,\n+\t\t\t\t\t-(spi + 1) * BPF_REG_SIZE);\n+\t\t\t\tbreak;\n+\t\t\tdefault:\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tbpf_diag_res(env, insn_idx, \"live stack object across bpf_task_park()\",\n+\t\t\t\t     \"A dynptr or iterator on the stack may refer to memory that the sleep in bpf_task_park() lets go, and the verifier cannot invalidate it without losing the path that releases its reference.\",\n+\t\t\t\t     \"Release or destroy the dynptr or iterator before calling bpf_task_park(), or move the park out of the loop that uses it.\");\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t}\n+\treturn 0;\n+}\n+\n+/*\n+ * Invalidate what the sleep in bpf_task_park() may free. Keep only pointers\n+ * whose lifetime does not rest on the dropped protection: the stack and\n+ * instruction metadata; the context and the maps the program references\n+ * directly, which the invocation holds; the values of its array maps, which\n+ * the map allocates and frees with itself, and which hold the program's\n+ * global variables; arena memory, which the arena map holds and whose\n+ * accesses tolerate faults; and pointers backed by an explicit\n+ * verifier-tracked reference, found through their own id or the id of their\n+ * parent. An inner map found through a map-in-map lookup is not kept, nor\n+ * are its values: the outer map's element alone holds it, and its deletion\n+ * frees it after a grace period the sleeping program no longer delays.\n+ * Everything else, values of maps that free elements on delete or on their\n+ * owner's teardown, RCU and trusted pointers without a reference, per-CPU\n+ * and local storage pointers, is gone after the call.\n+ */\n+static void invalidate_regs_across_park(struct bpf_verifier_env *env)\n+{\n+\tstruct bpf_stack_state *stack;\n+\tstruct bpf_func_state *state;\n+\tstruct bpf_reg_state *reg;\n+\n+\tbpf_for_each_reg_in_vstate_mask(env-\u003ecur_state, state, reg, stack, 1 \u003c\u003c STACK_SPILL, ({\n+\t\tenum bpf_reg_type type = base_type(reg-\u003etype);\n+\n+\t\tif (!is_pointer_regtype(reg-\u003etype))\n+\t\t\tcontinue;\n+\t\tif ((reg-\u003eid \u0026\u0026 find_reference_state(env-\u003ecur_state, reg-\u003eid)) ||\n+\t\t    (reg-\u003eparent_id \u0026\u0026 find_reference_state(env-\u003ecur_state, reg-\u003eparent_id)))\n+\t\t\tcontinue;\n+\t\tif ((type == CONST_PTR_TO_MAP || type == PTR_TO_MAP_VALUE) \u0026\u0026 !reg-\u003emap_uid \u0026\u0026\n+\t\t    (type == CONST_PTR_TO_MAP || reg-\u003emap_ptr-\u003emap_type == BPF_MAP_TYPE_ARRAY))\n+\t\t\tcontinue;\n+\t\tif (type == PTR_TO_STACK || type == PTR_TO_FUNC || type == PTR_TO_INSN ||\n+\t\t    type == PTR_TO_CTX || type == PTR_TO_ARENA)\n+\t\t\tcontinue;\n+\t\tbpf_diag_record_scrub(env, reg, BPF_DIAG_MOD_TASK_PARK);\n+\t\tmark_reg_invalid(env, reg);\n+\t}));\n+\t/* the frames of the callers are looked at when the function returns to them */\n+\tif (env-\u003ecur_state-\u003eflow_entry)\n+\t\tenv-\u003ecur_state-\u003eparked = true;\n+}\n+\n static void invalidate_rcu_protected_refs(struct bpf_verifier_env *env)\n {\n \tstruct bpf_stack_state *stack;\n@@ -11640,6 +11773,9 @@ static bool func_is_on_its_own(struct bpf_verifier_env *env, struct bpf_verifier\n \tif (sub-\u003echanges_pkt_data || sub-\u003ehas_tail_call || sub-\u003ehas_ld_abs ||\n \t    sub-\u003eis_cb || sub-\u003eis_async_cb || sub-\u003eis_exception_cb)\n \t\treturn false;\n+\t/* a park looks at every frame, and takes from each what the sleep may free */\n+\tif (sub-\u003emight_park)\n+\t\treturn false;\n \tif (state-\u003especulative)\n \t\treturn false;\n \tif (sub-\u003ereleases_inherited \u0026\u0026\n@@ -11819,6 +11955,15 @@ int bpf_flow_return(struct bpf_verifier_env *env, struct bpf_verifier_state *cal\n \t\tenv-\u003esubprog_info[callee-\u003esubprogno].changes_pkt_data = true;\n \t\treturn 1;\n \t}\n+\t/*\n+\t * Likewise for a park behind such an address. It was checked against the frames\n+\t * of the function only: which caller is a callback, and what the sleep takes\n+\t * from each, is found out in the context of the caller.\n+\t */\n+\tif (exit-\u003eparked) {\n+\t\tenv-\u003esubprog_info[callee-\u003esubprogno].might_park = true;\n+\t\treturn 1;\n+\t}\n \n \tfor (i = 0; i \u003c CALLER_SAVED_REGS; i++)\n \t\tbpf_mark_reg_not_init(env, \u0026frame-\u003eregs[caller_saved[i]]);\n@@ -12012,6 +12157,26 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t\treturn err;\n \t\t}\n \n+\t\t/*\n+\t\t * A park inside the callee is checked when the callee is verified,\n+\t\t * but only against the callee's own frame. Apply the rules for the\n+\t\t * frames the callee cannot see here, at the call.\n+\t\t */\n+\t\tif (env-\u003esubprog_info[subprog].might_park) {\n+\t\t\tif (in_callback_frames(env)) {\n+\t\t\t\tverbose(env, \"global function %s() may call bpf_task_park(), which is not allowed in a callback\\n\",\n+\t\t\t\t\tsub_name);\n+\t\t\t\toperation = bpf_diag_fmt(env, \"global function %s() that may call bpf_task_park()\",\n+\t\t\t\t\t\t\t sub_name);\n+\t\t\t\tbpf_diag_policy(env, *insn_idx, operation, \"the call is in a callback\",\n+\t\t\t\t\t\t\"Call the function from the main program, from the body of a BPF kthread, or from a function either calls directly, not from any other callback.\");\n+\t\t\t\treturn -EINVAL;\n+\t\t\t}\n+\t\t\terr = check_park_stack_objects(env, *insn_idx);\n+\t\t\tif (err)\n+\t\t\t\treturn err;\n+\t\t}\n+\n \t\tif (env-\u003elog.level \u0026 BPF_LOG_LEVEL)\n \t\t\tverbose(env, \"Func#%d ('%s') is global and assumed valid.\\n\",\n \t\t\t\tsubprog, sub_name);\n@@ -12024,6 +12189,8 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\treturns_void = subprog_returns_void(env, subprog);\n \t\tif (env-\u003esubprog_info[subprog].changes_pkt_data)\n \t\t\tclear_all_pkt_pointers(env);\n+\t\tif (env-\u003esubprog_info[subprog].might_park)\n+\t\t\tinvalidate_regs_across_park(env);\n \t\tif (returns_void)\n \t\t\tbpf_diag_record_scrub(env, \u0026caller-\u003eregs[BPF_REG_0], BPF_DIAG_MOD_CALLER_SAVED);\n \t\telse\n@@ -12487,6 +12654,33 @@ static int set_task_work_schedule_callback_state(struct bpf_verifier_env *env,\n \treturn 0;\n }\n \n+static int set_kthread_callback_state(struct bpf_verifier_env *env,\n+\t\t\t\t      struct bpf_func_state *caller,\n+\t\t\t\t      struct bpf_func_state *callee,\n+\t\t\t\t      int insn_idx)\n+{\n+\t/*\n+\t * bpf_kthread_create(int (callback_fn)(u64 arg), u64 arg, u64 flags);\n+\t * callback_fn(u64 arg);\n+\t */\n+\tbpf_mark_reg_unknown(\u0026callee-\u003eregs[BPF_REG_1]);\n+\n+\t/* unused */\n+\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_2]);\n+\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_3]);\n+\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_4]);\n+\tbpf_mark_reg_not_init(env, \u0026callee-\u003eregs[BPF_REG_5]);\n+\tcallee-\u003ein_async_callback_fn = true;\n+\tcallee-\u003ein_kthread_fn = true;\n+\t/*\n+\t * The callback returns an int, which comes back in the low 32 bits of\n+\t * R0: compare those, as for a global function.\n+\t */\n+\tcallee-\u003ecallback_ret_range = retval_range(S32_MIN, S32_MAX);\n+\tcallee-\u003ecallback_ret_range.return_32bit = true;\n+\treturn 0;\n+}\n+\n static int set_rcu_callback_state(struct bpf_verifier_env *env,\n \t\t\t\t  struct bpf_func_state *caller,\n \t\t\t\t  struct bpf_func_state *callee,\n@@ -14170,9 +14364,14 @@ enum special_kfunc_type {\n \tKF_bpf_task_work_schedule_resume,\n \tKF_bpf_call_rcu,\n \tKF_bpf_call_rcu_tasks_trace,\n+\tKF_bpf_kthread_create,\n+\tKF_bpf_kthread_stop,\n+\tKF_bpf_kthread_bind,\n+\tKF_bpf_kthread_attach_cgroup,\n \tKF_bpf_arena_alloc_pages,\n \tKF_bpf_arena_free_pages,\n \tKF_bpf_session_is_return,\n+\tKF_bpf_task_park,\n };\n \n BTF_ID_LIST(special_kfunc_list)\n@@ -14262,6 +14461,14 @@ BTF_ID(func, bpf_task_work_schedule_signal)\n BTF_ID(func, bpf_task_work_schedule_resume)\n BTF_ID(func, bpf_call_rcu)\n BTF_ID(func, bpf_call_rcu_tasks_trace)\n+BTF_ID(func, bpf_kthread_create)\n+BTF_ID(func, bpf_kthread_stop)\n+BTF_ID(func, bpf_kthread_bind)\n+#ifdef CONFIG_CGROUPS\n+BTF_ID(func, bpf_kthread_attach_cgroup)\n+#else\n+BTF_ID_UNUSED\n+#endif\n #if defined(CONFIG_MMU) \u0026\u0026 defined(CONFIG_64BIT)\n BTF_ID(func, bpf_arena_alloc_pages)\n BTF_ID(func, bpf_arena_free_pages)\n@@ -14274,6 +14481,7 @@ BTF_ID(func, bpf_session_is_return)\n #else\n BTF_ID_UNUSED\n #endif\n+BTF_ID(func, bpf_task_park)\n \n static bool is_bpf_cast_to_kern_ctx_kfunc(const struct bpf_call_arg_meta *meta)\n {\n@@ -14297,6 +14505,21 @@ static bool is_bpf_iter_css_task_new_kfunc(const struct bpf_call_arg_meta *meta)\n \treturn is_kfunc_call(meta, special_kfunc_list[KF_bpf_iter_css_task_new]);\n }\n \n+static bool is_bpf_task_park_kfunc(u32 btf_id)\n+{\n+\treturn btf_id == special_kfunc_list[KF_bpf_task_park];\n+}\n+\n+/* The kfuncs that sleep or wait with migration enabled. */\n+static bool is_migrating_kfunc(u32 btf_id)\n+{\n+\treturn btf_id == special_kfunc_list[KF_bpf_task_park] ||\n+\t       btf_id == special_kfunc_list[KF_bpf_kthread_create] ||\n+\t       btf_id == special_kfunc_list[KF_bpf_kthread_stop] ||\n+\t       btf_id == special_kfunc_list[KF_bpf_kthread_bind] ||\n+\t       btf_id == special_kfunc_list[KF_bpf_kthread_attach_cgroup];\n+}\n+\n static bool is_bpf_obj_new_kfunc(u32 func_id)\n {\n \treturn func_id == special_kfunc_list[KF_bpf_obj_new] ||\n@@ -14414,6 +14637,11 @@ bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta)\n \t       is_kfunc_dynptr_write(meta);\n }\n \n+bool bpf_is_task_park_kfunc(struct bpf_call_arg_meta *meta)\n+{\n+\treturn is_kfunc_call(meta, special_kfunc_list[KF_bpf_task_park]);\n+}\n+\n static u32 kfunc_abi_slots(const struct btf_func_model *fm)\n {\n \tconst struct bpf_jit_arg_abi *abi = bpf_jit_arg_abi();\n@@ -15140,6 +15368,7 @@ static bool is_sync_callback_calling_kfunc(u32 btf_id)\n static bool is_async_callback_calling_kfunc(u32 btf_id)\n {\n \treturn is_bpf_wq_set_callback_kfunc(btf_id) ||\n+\t       is_bpf_kthread_create_kfunc(btf_id) ||\n \t       is_task_work_add_kfunc(btf_id) ||\n \t       is_call_rcu_kfunc(btf_id);\n }\n@@ -15155,6 +15384,11 @@ static bool is_bpf_wq_set_callback_kfunc(u32 btf_id)\n \treturn btf_id == special_kfunc_list[KF_bpf_wq_set_callback];\n }\n \n+static bool is_bpf_kthread_create_kfunc(u32 btf_id)\n+{\n+\treturn btf_id == special_kfunc_list[KF_bpf_kthread_create];\n+}\n+\n static bool is_callback_calling_kfunc(u32 btf_id)\n {\n \treturn is_sync_callback_calling_kfunc(btf_id) ||\n@@ -16095,6 +16329,24 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \tif (!in_sleepable_context(env))\n \t\tinsn_aux-\u003enon_sleepable = true;\n \n+\t/*\n+\t * The kfunc may return on another CPU. What a run takes on the CPU it\n+\t * starts on and gives back on the CPU it ends on does not survive that.\n+\t * The stack in arena is such a thing, see bpf_arena_stack_put(), and\n+\t * the time slice of may_goto, which bpf_do_misc_fixups() refuses. For\n+\t * the same reason the program is kept out of prog arrays, where it would\n+\t * also run without a reference of its own, and away from runners that\n+\t * count a run per CPU.\n+\t */\n+\tif (is_migrating_kfunc(meta.func_id)) {\n+\t\tif (env-\u003euses_arena_sp) {\n+\t\t\tverbose(env, \"%s() cannot be called in a program that uses R%d\\n\",\n+\t\t\t\tfunc_name, BPF_REG_ARENA_SP);\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t\tenv-\u003eprog-\u003eaux-\u003emay_migrate = true;\n+\t}\n+\n \t/* Check the arguments */\n \terr = check_func_args(env, \u0026meta, insn_idx);\n \tif (err \u003c 0)\n@@ -16159,6 +16411,16 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t}\n \t}\n \n+\tif (is_bpf_kthread_create_kfunc(meta.func_id)) {\n+\t\terr = push_callback_call(env, insn, insn_idx, meta.subprogno,\n+\t\t\t\t\t set_kthread_callback_state);\n+\t\tif (err) {\n+\t\t\tverbose(env, \"kfunc %s#%d failed callback verification\\n\",\n+\t\t\t\tfunc_name, meta.func_id);\n+\t\t\treturn err;\n+\t\t}\n+\t}\n+\n \trcu_lock = is_kfunc_bpf_rcu_read_lock(\u0026meta);\n \trcu_unlock = is_kfunc_bpf_rcu_read_unlock(\u0026meta);\n \n@@ -16266,6 +16528,19 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,\n \t\t}\n \t}\n \n+\tif (is_bpf_task_park_kfunc(meta.func_id)) {\n+\t\tif (in_callback_frames(env)) {\n+\t\t\tverbose(env, \"bpf_task_park() is not allowed in a callback\\n\");\n+\t\t\tbpf_diag_policy(env, insn_idx, \"kfunc bpf_task_park\", \"the call is in a callback\",\n+\t\t\t\t\t\"Call bpf_task_park() from the main program, from the body of a BPF kthread, or from a function either calls directly, not from any other callback.\");\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t\terr = check_park_stack_objects(env, insn_idx);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t\tinvalidate_regs_across_park(env);\n+\t}\n+\n \tbpf_diag_record_caller_saved(env, regs);\n \tbpf_diag_mod_begin(env, \u0026regs[BPF_REG_0], NULL, BPF_DIAG_MOD_WRITE);\n \tfor (i = 0; i \u003c CALLER_SAVED_REGS; i++) {\ndiff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c\nindex a284225f8dc95b..41adae216adaa4 100644\n--- a/kernel/cgroup/cgroup.c\n+++ b/kernel/cgroup/cgroup.c\n@@ -3124,6 +3124,51 @@ int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,\n \treturn ret;\n }\n \n+/**\n+ * cgroup_attach_kthread - move a kernel thread into a cgroup\n+ * @dst_cgrp: the cgroup to move it into\n+ * @task: the kernel thread\n+ *\n+ * For kernel threads the kernel creates on behalf of a user and accounts to a\n+ * cgroup of the user's choosing rather than, as cgroup_attach_task_all() does,\n+ * to the creator's own. @dst_cgrp is checked as a destination of cgroup.procs\n+ * is, except for the permission check, which the caller owes: it must not be\n+ * dying and, on the default hierarchy, must be able to host processes, and\n+ * @task must be one the cgroup core may migrate.\n+ *\n+ * Return: 0 on success, -ENODEV if @dst_cgrp is being removed, -EBUSY or\n+ * -EOPNOTSUPP if it cannot host processes, -EINVAL if @task may not be\n+ * migrated, or the error of the migration.\n+ */\n+int cgroup_attach_kthread(struct cgroup *dst_cgrp, struct task_struct *task)\n+{\n+\tenum cgroup_attach_lock_mode lock_mode;\n+\tint ret;\n+\n+\tcgroup_lock();\n+\tif (cgroup_is_dead(dst_cgrp)) {\n+\t\tret = -ENODEV;\n+\t\tgoto out_unlock;\n+\t}\n+\t/* As cgroup_procs_write_start() refuses them: cpusets cannot move these. */\n+\tif (task-\u003eno_cgroup_migration || (task-\u003eflags \u0026 PF_NO_SETAFFINITY)) {\n+\t\tret = -EINVAL;\n+\t\tgoto out_unlock;\n+\t}\n+\tret = cgroup_migrate_vet_dst(dst_cgrp);\n+\tif (ret)\n+\t\tgoto out_unlock;\n+\t/* The moved process's own rwsem under favordynmods, as cgroup.procs takes. */\n+\tlock_mode = cgroup_enable_per_threadgroup_rwsem ? CGRP_ATTACH_LOCK_PER_THREADGROUP :\n+\t\t\t\t\t\t\t   CGRP_ATTACH_LOCK_GLOBAL;\n+\tcgroup_attach_lock(lock_mode, task);\n+\tret = cgroup_attach_task(dst_cgrp, task, true);\n+\tcgroup_attach_unlock(lock_mode, task);\n+out_unlock:\n+\tcgroup_unlock();\n+\treturn ret;\n+}\n+\n struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,\n \t\t\t\t\t     enum cgroup_attach_lock_mode *lock_mode)\n {\ndiff --git a/kernel/fork.c b/kernel/fork.c\nindex 8b108ed371529c..a272d11c6cafcc 100644\n--- a/kernel/fork.c\n+++ b/kernel/fork.c\n@@ -1019,6 +1019,8 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)\n #ifdef CONFIG_BPF_SYSCALL\n \tRCU_INIT_POINTER(tsk-\u003ebpf_storage, NULL);\n \ttsk-\u003ebpf_ctx = NULL;\n+\ttsk-\u003ebpf_wake_pending = 0;\n+\ttsk-\u003ebpf_wake_node.next = NULL;\n #endif\n \treturn tsk;\n \ndiff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h\nindex 2893bf06ff25d4..49d7a45f970716 100644\n--- a/tools/testing/selftests/bpf/bpf_experimental.h\n+++ b/tools/testing/selftests/bpf/bpf_experimental.h\n@@ -351,6 +351,18 @@ extern void bpf_iter_css_destroy(struct bpf_iter_css *it) __weak __ksym;\n extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;\n extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;\n \n+extern int bpf_task_park(__u64 timeout_ns, __u64 flags) __weak __ksym;\n+extern int bpf_task_wake(struct task_struct *task, __u64 flags) __weak __ksym;\n+\n+extern struct task_struct *bpf_kthread_create(int (callback_fn)(__u64 arg), __u64 arg,\n+\t\t\t\t\t      __u64 flags) __weak __ksym;\n+extern int bpf_kthread_start(struct task_struct *task, __u64 flags) __weak __ksym;\n+extern int bpf_kthread_bind(struct task_struct *task, __u32 cpu, __u64 flags) __weak __ksym;\n+extern int bpf_kthread_attach_cgroup(struct task_struct *task, struct cgroup *cgrp,\n+\t\t\t\t     __u64 flags) __weak __ksym;\n+extern int bpf_kthread_stop(struct task_struct *task, __u64 flags) __weak __ksym;\n+extern bool bpf_kthread_should_stop(void) __weak __ksym;\n+\n struct bpf_iter_kmem_cache;\n extern int bpf_iter_kmem_cache_new(struct bpf_iter_kmem_cache *it) __weak __ksym;\n extern struct kmem_cache *bpf_iter_kmem_cache_next(struct bpf_iter_kmem_cache *it) __weak __ksym;\ndiff --git a/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c b/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c\nnew file mode 100644\nindex 00000000000000..fabd6b9e524269\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c\n@@ -0,0 +1,641 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \u003ctest_progs.h\u003e\n+#include \u003csched.h\u003e\n+#include \"cgroup_helpers.h\"\n+#include \"bpf_kthread.skel.h\"\n+#include \"verifier_bpf_kthread.skel.h\"\n+\n+/* The context of the syscall programs, as the programs declare it. */\n+struct kthread_args {\n+\t__u32 idx;\n+\t__u32 start;\n+\t__u32 body;\n+\t__u32 bind;\n+\t__u32 cpu;\n+\t__u64 cgroup_id;\n+};\n+\n+enum body {\n+\tBODY_WORKER,\n+\tBODY_SPAWNER,\n+\tBODY_SELF_STOPPER,\n+\tBODY_RING,\n+\tBODY_STOP_CHECKER,\n+\tBODY_SELF_BINDER,\n+};\n+\n+static int run_prog(struct bpf_program *prog, struct kthread_args *args, int *retval)\n+{\n+\tLIBBPF_OPTS(bpf_test_run_opts, opts,\n+\t\t.ctx_in = args,\n+\t\t.ctx_size_in = args ? sizeof(*args) : 0,\n+\t);\n+\tint err;\n+\n+\terr = bpf_prog_test_run_opts(bpf_program__fd(prog), \u0026opts);\n+\tif (!err)\n+\t\t*retval = opts.retval;\n+\treturn err;\n+}\n+\n+static int create_thread_on(struct bpf_kthread *skel, __u32 idx, bool start, enum body body,\n+\t\t\t    int cpu)\n+{\n+\tstruct kthread_args args = {\n+\t\t.idx = idx, .start = start, .body = body, .bind = cpu \u003e= 0, .cpu = cpu,\n+\t};\n+\tint err, retval;\n+\n+\terr = run_prog(skel-\u003eprogs.create_thread, \u0026args, \u0026retval);\n+\tif (!ASSERT_OK(err, \"create_thread\") || !ASSERT_EQ(retval, 0, \"create_ret\"))\n+\t\treturn -1;\n+\treturn 0;\n+}\n+\n+static int create_thread(struct bpf_kthread *skel, __u32 idx, bool start, enum body body)\n+{\n+\treturn create_thread_on(skel, idx, start, body, -1);\n+}\n+\n+static int bind_thread(struct bpf_kthread *skel, __u32 idx, __u32 cpu)\n+{\n+\tstruct kthread_args args = { .idx = idx, .cpu = cpu };\n+\tint retval;\n+\n+\tif (!ASSERT_OK(run_prog(skel-\u003eprogs.bind_thread, \u0026args, \u0026retval), \"bind_thread\"))\n+\t\treturn -EFAULT;\n+\treturn retval;\n+}\n+\n+static int attach_cgroup(struct bpf_kthread *skel, __u32 idx, __u64 cgroup_id)\n+{\n+\tstruct kthread_args args = { .idx = idx, .cgroup_id = cgroup_id };\n+\tint retval;\n+\n+\tif (!ASSERT_OK(run_prog(skel-\u003eprogs.attach_cgroup, \u0026args, \u0026retval), \"attach_cgroup\"))\n+\t\treturn -EFAULT;\n+\treturn retval;\n+}\n+\n+/* Returns what the program returned, which for stop_thread is what the stop returned. */\n+static int control_thread(struct bpf_program *prog, __u32 idx)\n+{\n+\tstruct kthread_args args = { .idx = idx };\n+\tint retval;\n+\n+\tif (!ASSERT_OK(run_prog(prog, \u0026args, \u0026retval), bpf_program__name(prog)))\n+\t\treturn -EFAULT;\n+\treturn retval;\n+}\n+\n+#define start_thread(skel, idx) control_thread((skel)-\u003eprogs.start_thread, idx)\n+#define wake_thread(skel, idx) control_thread((skel)-\u003eprogs.wake_thread, idx)\n+#define stop_thread(skel, idx) control_thread((skel)-\u003eprogs.stop_thread, idx)\n+\n+/* Poll until *p is nonzero, for up to two seconds. */\n+static bool wait_for(__u32 *p)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c 2000; i++) {\n+\t\tif (__atomic_load_n(p, __ATOMIC_ACQUIRE))\n+\t\t\treturn true;\n+\t\tusleep(1000);\n+\t}\n+\treturn false;\n+}\n+\n+/* Poll until *p exceeds @seen, for up to two seconds: the thread has looped again. */\n+static bool wait_past(__u32 *p, __u32 seen)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c 2000; i++) {\n+\t\tif (__atomic_load_n(p, __ATOMIC_ACQUIRE) \u003e seen)\n+\t\t\treturn true;\n+\t\tusleep(1000);\n+\t}\n+\treturn false;\n+}\n+\n+/* Whether the thread sleeps: state 'S' in /proc/\u003ctid\u003e/stat, read past the parenthesised comm. */\n+static bool thread_sleeping(__u32 tid)\n+{\n+\tchar path[64], buf[512], *p;\n+\tint fd, n;\n+\n+\tsnprintf(path, sizeof(path), \"/proc/%u/stat\", tid);\n+\tfd = open(path, O_RDONLY);\n+\tif (fd \u003c 0)\n+\t\treturn false;\n+\tn = read(fd, buf, sizeof(buf) - 1);\n+\tclose(fd);\n+\tif (n \u003c= 0)\n+\t\treturn false;\n+\tbuf[n] = 0;\n+\tp = strrchr(buf, ')');\n+\treturn p \u0026\u0026 p[1] == ' ' \u0026\u0026 p[2] == 'S';\n+}\n+\n+/* Poll until the thread sleeps, for up to two seconds. */\n+static bool wait_sleeping(__u32 tid)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c 2000; i++) {\n+\t\tif (thread_sleeping(tid))\n+\t\t\treturn true;\n+\t\tusleep(1000);\n+\t}\n+\treturn false;\n+}\n+\n+/* The lowest and highest CPUs the test may run on; a bound thread has to run there. */\n+static int allowed_cpus(int *lo, int *hi)\n+{\n+\tcpu_set_t set;\n+\tint cpu;\n+\n+\tif (sched_getaffinity(0, sizeof(set), \u0026set))\n+\t\treturn -1;\n+\t*lo = *hi = -1;\n+\tfor (cpu = 0; cpu \u003c CPU_SETSIZE; cpu++) {\n+\t\tif (!CPU_ISSET(cpu, \u0026set))\n+\t\t\tcontinue;\n+\t\tif (*lo \u003c 0)\n+\t\t\t*lo = cpu;\n+\t\t*hi = cpu;\n+\t}\n+\treturn *lo \u003c 0 ? -1 : 0;\n+}\n+\n+static bool task_exists(__u32 tid)\n+{\n+\tchar path[64];\n+\n+\tsnprintf(path, sizeof(path), \"/proc/%u\", tid);\n+\treturn !access(path, F_OK);\n+}\n+\n+/* Poll until the task has left /proc, for up to two seconds. */\n+static bool wait_gone(__u32 tid)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c 2000; i++) {\n+\t\tif (!task_exists(tid))\n+\t\t\treturn true;\n+\t\tusleep(1000);\n+\t}\n+\treturn false;\n+}\n+\n+static int read_file(const char *path, char *buf, size_t size)\n+{\n+\tint fd, n;\n+\n+\tfd = open(path, O_RDONLY);\n+\tif (fd \u003c 0)\n+\t\treturn -errno;\n+\tn = read(fd, buf, size - 1);\n+\tclose(fd);\n+\tif (n \u003c 0)\n+\t\treturn -errno;\n+\tbuf[n] = 0;\n+\treturn n;\n+}\n+\n+/*\n+ * The thread is named after the program that created it and its index among\n+ * the program's threads. /proc shows a kernel thread's full name whatever its\n+ * length; the comm the thread reads for itself is what tracepoints and perf\n+ * report, and the name is short enough to fit it.\n+ */\n+static void check_comm(struct bpf_kthread *skel, __u32 tid, __u32 idx)\n+{\n+\tstruct bpf_prog_info info = {};\n+\t__u32 len = sizeof(info);\n+\tchar path[64], buf[512], want[64];\n+\n+\tif (!ASSERT_OK(bpf_prog_get_info_by_fd(bpf_program__fd(skel-\u003eprogs.create_thread),\n+\t\t\t\t\t       \u0026info, \u0026len), \"prog_info\"))\n+\t\treturn;\n+\tsnprintf(want, sizeof(want), \"bpfk/%u:%u\", info.id, idx);\n+\n+\tsnprintf(path, sizeof(path), \"/proc/%u/comm\", tid);\n+\tif (!ASSERT_GT(read_file(path, buf, sizeof(buf)), 0, \"read comm\"))\n+\t\treturn;\n+\tif (buf[strlen(buf) - 1] == '\\n')\n+\t\tbuf[strlen(buf) - 1] = 0;\n+\tASSERT_STREQ(buf, want, \"comm\");\n+\tASSERT_STREQ(skel-\u003ebss-\u003ecomms[idx], want, \"task comm\");\n+}\n+\n+/*\n+ * A started thread runs its callback, which parks until woken; its return ends\n+ * the thread and is what the stop reports.\n+ */\n+static void test_start_exit(void)\n+{\n+\tstruct bpf_kthread *skel;\n+\t__u32 tid;\n+\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (create_thread(skel, 0, true, BODY_WORKER))\n+\t\tgoto out;\n+\tif (!ASSERT_TRUE(wait_for(\u0026skel-\u003ebss-\u003eruns[0]), \"ran\"))\n+\t\tgoto out;\n+\ttid = skel-\u003ebss-\u003etids[0];\n+\tASSERT_TRUE(task_exists(tid), \"alive\");\n+\tASSERT_TRUE(wait_sleeping(tid), \"parked\");\n+\tcheck_comm(skel, tid, 0);\n+\n+\t__atomic_store_n(\u0026skel-\u003ebss-\u003eexit_now[0], 1, __ATOMIC_RELEASE);\n+\tASSERT_GE(wake_thread(skel, 0), 0, \"wake ret\");\n+\tASSERT_TRUE(wait_gone(tid), \"exited\");\n+\tASSERT_EQ(stop_thread(skel, 0), 1, \"exit code\");\n+\tASSERT_EQ(skel-\u003ebss-\u003eruns[0], 1, \"runs\");\n+\tASSERT_EQ(stop_thread(skel, 0), -ESRCH, \"slot empty\");\n+out:\n+\tbpf_kthread__destroy(skel);\n+}\n+\n+/* A stop ends a running callback through bpf_kthread_should_stop(). */\n+static void test_stop_running(void)\n+{\n+\tstruct bpf_kthread *skel;\n+\t__u32 tid;\n+\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (create_thread(skel, 1, true, BODY_WORKER))\n+\t\tgoto out;\n+\tif (!ASSERT_TRUE(wait_for(\u0026skel-\u003ebss-\u003eruns[1]), \"ran\"))\n+\t\tgoto out;\n+\ttid = skel-\u003ebss-\u003etids[1];\n+\tASSERT_EQ(stop_thread(skel, 1), 0, \"stop ret\");\n+\tASSERT_TRUE(wait_gone(tid), \"exited\");\n+out:\n+\tbpf_kthread__destroy(skel);\n+}\n+\n+/* A stop makes every park return -ESHUTDOWN, except one that has a wake to consume. */\n+static void test_stop_park_rets(void)\n+{\n+\tstruct bpf_kthread *skel;\n+\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (create_thread(skel, 0, true, BODY_STOP_CHECKER))\n+\t\tgoto out;\n+\tif (!ASSERT_TRUE(wait_for(\u0026skel-\u003ebss-\u003eruns[0]), \"ran\"))\n+\t\tgoto out;\n+\tASSERT_EQ(stop_thread(skel, 0), 1, \"stop ret\");\n+\tASSERT_EQ(skel-\u003ebss-\u003estop_park_rets[0], -ESHUTDOWN, \"park the stop ended\");\n+\tASSERT_EQ(skel-\u003ebss-\u003estop_park_rets[1], -ESHUTDOWN, \"poll after the stop\");\n+\tASSERT_EQ(skel-\u003ebss-\u003estop_park_rets[2], 0, \"park with a wake pending\");\n+\tASSERT_EQ(skel-\u003ebss-\u003estop_park_rets[3], -ESHUTDOWN, \"park after the wake\");\n+out:\n+\tbpf_kthread__destroy(skel);\n+}\n+\n+/* A thread stopped before it is started never runs its callback. */\n+static void test_stop_parked(void)\n+{\n+\tstruct bpf_kthread *skel;\n+\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (create_thread(skel, 2, false, BODY_WORKER))\n+\t\tgoto out;\n+\tASSERT_EQ(stop_thread(skel, 2), -EINTR, \"stop ret\");\n+\tASSERT_EQ(skel-\u003ebss-\u003eruns[2], 0, \"runs\");\n+out:\n+\tbpf_kthread__destroy(skel);\n+}\n+\n+/* A parked thread starts through an RCU-protected pointer; a second start is a no-op. */\n+static void test_start_later(void)\n+{\n+\tstruct bpf_kthread *skel;\n+\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (create_thread(skel, 2, false, BODY_WORKER))\n+\t\tgoto out;\n+\tASSERT_EQ(start_thread(skel, 2), 0, \"start ret\");\n+\tif (!ASSERT_TRUE(wait_for(\u0026skel-\u003ebss-\u003eruns[2]), \"ran\"))\n+\t\tgoto out;\n+\tASSERT_EQ(start_thread(skel, 2), 0, \"second start ret\");\n+\tASSERT_EQ(stop_thread(skel, 2), 0, \"stop ret\");\n+\tASSERT_EQ(skel-\u003ebss-\u003eruns[2], 1, \"runs\");\n+out:\n+\tbpf_kthread__destroy(skel);\n+}\n+\n+/* A thread cannot stop itself. */\n+static void test_self_stop(void)\n+{\n+\tstruct bpf_kthread *skel;\n+\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (create_thread(skel, 3, true, BODY_SELF_STOPPER))\n+\t\tgoto out;\n+\tif (!ASSERT_TRUE(wait_for(\u0026skel-\u003ebss-\u003eruns[3]), \"ran\"))\n+\t\tgoto out;\n+\tASSERT_EQ(skel-\u003ebss-\u003eself_stop_ret, -EDEADLK, \"self stop ret\");\n+\tASSERT_EQ(stop_thread(skel, 3), 0, \"stop ret\");\n+out:\n+\tbpf_kthread__destroy(skel);\n+}\n+\n+/* A thread creates and starts another thread from its callback. */\n+static void test_spawn(void)\n+{\n+\tstruct bpf_kthread *skel;\n+\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (create_thread(skel, 0, true, BODY_SPAWNER))\n+\t\tgoto out;\n+\tif (!ASSERT_TRUE(wait_for(\u0026skel-\u003ebss-\u003eruns[1]), \"child ran\") ||\n+\t    !ASSERT_TRUE(wait_for(\u0026skel-\u003ebss-\u003eruns[0]), \"parent ran\"))\n+\t\tgoto out;\n+\tASSERT_EQ(skel-\u003ebss-\u003espawn_ret, 0, \"spawn ret\");\n+\tASSERT_NEQ(skel-\u003ebss-\u003etids[0], skel-\u003ebss-\u003etids[1], \"distinct threads\");\n+\tcheck_comm(skel, skel-\u003ebss-\u003etids[0], 0);\n+\tcheck_comm(skel, skel-\u003ebss-\u003etids[1], 1);\n+\tASSERT_EQ(stop_thread(skel, 1), 0, \"stop child\");\n+\tASSERT_EQ(stop_thread(skel, 0), 0, \"stop parent\");\n+out:\n+\tbpf_kthread__destroy(skel);\n+}\n+\n+/* Tasks that are no BPF kthreads, nonzero flags, and a stop before the start. */\n+static void test_misuse(void)\n+{\n+\tstruct bpf_kthread *skel;\n+\tint retval;\n+\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (ASSERT_OK(run_prog(skel-\u003eprogs.control_foreign_task, NULL, \u0026retval), \"foreign\")) {\n+\t\tASSERT_EQ(retval, 0, \"foreign ret\");\n+\t\tASSERT_EQ(skel-\u003ebss-\u003eforeign_start_ret, -ESRCH, \"foreign start ret\");\n+\t\tASSERT_EQ(skel-\u003ebss-\u003eforeign_stop_ret, -ESRCH, \"foreign stop ret\");\n+\t\tASSERT_EQ(skel-\u003ebss-\u003eforeign_attach_ret, -ESRCH, \"foreign attach ret\");\n+\t}\n+\tif (ASSERT_OK(run_prog(skel-\u003eprogs.misuse, NULL, \u0026retval), \"misuse\"))\n+\t\tASSERT_EQ(retval, 0, \"misuse ret\");\n+\tbpf_kthread__destroy(skel);\n+}\n+\n+/* The program's release stops the threads it still has. */\n+static void test_program_free(void)\n+{\n+\tstruct bpf_kthread *skel;\n+\t__u32 tid;\n+\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (create_thread(skel, 0, true, BODY_WORKER))\n+\t\tgoto out;\n+\tif (!ASSERT_TRUE(wait_for(\u0026skel-\u003ebss-\u003eruns[0]), \"ran\"))\n+\t\tgoto out;\n+\ttid = skel-\u003ebss-\u003etids[0];\n+\tbpf_kthread__destroy(skel);\n+\tASSERT_TRUE(wait_gone(tid), \"exited\");\n+\treturn;\n+out:\n+\tbpf_kthread__destroy(skel);\n+}\n+\n+/*\n+ * Threads stopping each other in a ring. The stop that would close the ring of\n+ * waits is refused with -EDEADLK, the others return their successor's exit\n+ * code as the ring unwinds, and every thread exits. The thread the refused\n+ * stop was aimed at is the one left in its slot; the others were taken by the\n+ * threads that stopped them.\n+ */\n+static void test_stop_ring(__u32 size)\n+{\n+\tstruct bpf_kthread *skel;\n+\tint refused = 0, left = 0, ret;\n+\t__u32 i, peer;\n+\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tskel-\u003ebss-\u003ering_size = size;\n+\tfor (i = 0; i \u003c size; i++)\n+\t\tif (create_thread(skel, i, false, BODY_RING))\n+\t\t\tgoto out;\n+\tfor (i = 0; i \u003c size; i++)\n+\t\tif (!ASSERT_EQ(start_thread(skel, i), 0, \"start ret\"))\n+\t\t\tgoto out;\n+\tfor (i = 0; i \u003c size; i++) {\n+\t\tif (!ASSERT_TRUE(wait_for(\u0026skel-\u003ebss-\u003eruns[i]), \"ran\") ||\n+\t\t    !ASSERT_TRUE(wait_gone(skel-\u003ebss-\u003etids[i]), \"exited\"))\n+\t\t\tgoto out;\n+\t}\n+\tfor (i = 0; i \u003c size; i++) {\n+\t\tpeer = i + 1 \u003c size ? i + 1 : 0;\n+\t\tret = skel-\u003ebss-\u003ering_stop_ret[i];\n+\t\tif (ret == -EDEADLK)\n+\t\t\trefused++;\n+\t\telse\n+\t\t\tASSERT_EQ(ret, peer + 1, \"ring stop ret\");\n+\t}\n+\tASSERT_EQ(refused, 1, \"one stop refused\");\n+\tfor (i = 0; i \u003c size; i++) {\n+\t\tret = stop_thread(skel, i);\n+\t\tif (ret == -ESRCH)\n+\t\t\tcontinue;\n+\t\tASSERT_EQ(ret, i + 1, \"exit code\");\n+\t\tleft++;\n+\t}\n+\tASSERT_EQ(left, 1, \"one thread left in its slot\");\n+out:\n+\tbpf_kthread__destroy(skel);\n+}\n+\n+/*\n+ * A thread bound before its start runs on that CPU, and the last bind wins. A\n+ * thread bound while it runs moves at its next sleep, which for a body that\n+ * parks is at once, since the park sleeps with migration enabled; its next\n+ * iteration runs on the new CPU.\n+ */\n+static void test_bind(void)\n+{\n+\tstruct bpf_kthread *skel;\n+\tint lo, hi;\n+\t__u32 loops;\n+\n+\tif (!ASSERT_OK(allowed_cpus(\u0026lo, \u0026hi), \"allowed_cpus\"))\n+\t\treturn;\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (create_thread_on(skel, 0, false, BODY_WORKER, lo))\n+\t\tgoto out;\n+\tASSERT_EQ(bind_thread(skel, 0, -1), -EINVAL, \"bind impossible cpu\");\n+\tASSERT_EQ(bind_thread(skel, 0, hi), 0, \"bind ret\");\n+\tASSERT_EQ(start_thread(skel, 0), 0, \"start ret\");\n+\tif (!ASSERT_TRUE(wait_past(\u0026skel-\u003ebss-\u003eloops[0], 0), \"ran\"))\n+\t\tgoto out;\n+\tASSERT_EQ(skel-\u003ebss-\u003ecpus[0], hi, \"cpu\");\n+\tASSERT_EQ(bind_thread(skel, 0, lo), 0, \"bind running ret\");\n+\tloops = skel-\u003ebss-\u003eloops[0];\n+\tASSERT_GE(wake_thread(skel, 0), 0, \"wake ret\");\n+\tif (ASSERT_TRUE(wait_past(\u0026skel-\u003ebss-\u003eloops[0], loops), \"looped\"))\n+\t\tASSERT_EQ(skel-\u003ebss-\u003ecpus[0], lo, \"cpu after bind\");\n+\tASSERT_EQ(stop_thread(skel, 0), 0, \"stop ret\");\n+out:\n+\tbpf_kthread__destroy(skel);\n+}\n+\n+/*\n+ * A thread binds itself: it is migratable during the call, so the bind takes\n+ * effect before it returns, and the thread's first iteration runs on the CPU.\n+ */\n+static void test_self_bind(void)\n+{\n+\tstruct bpf_kthread *skel;\n+\tint lo, hi;\n+\n+\tif (!ASSERT_OK(allowed_cpus(\u0026lo, \u0026hi), \"allowed_cpus\"))\n+\t\treturn;\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tskel-\u003ebss-\u003eself_bind_cpu = lo;\n+\tif (create_thread(skel, 0, true, BODY_SELF_BINDER))\n+\t\tgoto out;\n+\tif (!ASSERT_TRUE(wait_past(\u0026skel-\u003ebss-\u003eloops[0], 0), \"ran\"))\n+\t\tgoto out;\n+\tASSERT_EQ(skel-\u003ebss-\u003eself_bind_ret, 0, \"self bind ret\");\n+\tASSERT_EQ(skel-\u003ebss-\u003ecpus[0], lo, \"cpu\");\n+\tASSERT_EQ(stop_thread(skel, 0), 0, \"stop ret\");\n+out:\n+\tbpf_kthread__destroy(skel);\n+}\n+\n+/* Whether cgroup.events of the cgroup reports it populated, polling until it says @populated. */\n+static bool wait_populated(const char *path, bool populated)\n+{\n+\tconst char *want = populated ? \"populated 1\" : \"populated 0\";\n+\tchar buf[256];\n+\tint i;\n+\n+\tfor (i = 0; i \u003c 2000; i++) {\n+\t\tif (!read_cgroup_file(path, \"cgroup.events\", buf, sizeof(buf)) \u0026\u0026 strstr(buf, want))\n+\t\t\treturn true;\n+\t\tusleep(1000);\n+\t}\n+\treturn false;\n+}\n+\n+/*\n+ * A thread moved into a cgroup, parked or running, runs there from its next\n+ * iteration on and populates the cgroup until it exits. The test's work\n+ * directory has controllers enabled for its children, so it cannot take the\n+ * thread itself.\n+ */\n+static void test_attach_cgroup(void)\n+{\n+\tstruct bpf_kthread *skel = NULL;\n+\tint fd1 = -1, fd2 = -1;\n+\tchar subtree[256] = {};\n+\t__u64 cg1, cg2;\n+\t__u32 loops;\n+\n+\tif (!ASSERT_OK(setup_cgroup_environment(), \"setup_cgroup_environment\"))\n+\t\treturn;\n+\tfd1 = create_and_get_cgroup(\"/kthread1\");\n+\tfd2 = create_and_get_cgroup(\"/kthread2\");\n+\tif (!ASSERT_GE(fd1, 0, \"create kthread1\") || !ASSERT_GE(fd2, 0, \"create kthread2\"))\n+\t\tgoto out;\n+\tcg1 = get_cgroup_id(\"/kthread1\");\n+\tcg2 = get_cgroup_id(\"/kthread2\");\n+\tskel = bpf_kthread__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\tgoto out;\n+\tif (create_thread(skel, 0, false, BODY_WORKER))\n+\t\tgoto out;\n+\t/* Only a domain controller in the children makes the parent refuse processes. */\n+\tif (!read_cgroup_file(\"\", \"cgroup.subtree_control\", subtree, sizeof(subtree)) \u0026\u0026\n+\t    (strstr(subtree, \"memory\") || strstr(subtree, \"io\")))\n+\t\tASSERT_EQ(attach_cgroup(skel, 0, get_cgroup_id(\"\")), -EBUSY,\n+\t\t\t  \"attach to inner cgroup\");\n+\tASSERT_EQ(attach_cgroup(skel, 0, cg1), 0, \"attach parked ret\");\n+\tASSERT_EQ(start_thread(skel, 0), 0, \"start ret\");\n+\tif (!ASSERT_TRUE(wait_past(\u0026skel-\u003ebss-\u003eloops[0], 0), \"ran\"))\n+\t\tgoto out;\n+\tASSERT_EQ(skel-\u003ebss-\u003ecgids[0], cg1, \"cgroup after start\");\n+\tASSERT_TRUE(wait_populated(\"/kthread1\", true), \"kthread1 populated\");\n+\n+\tASSERT_EQ(attach_cgroup(skel, 0, cg2), 0, \"attach running ret\");\n+\tloops = skel-\u003ebss-\u003eloops[0];\n+\tASSERT_GE(wake_thread(skel, 0), 0, \"wake ret\");\n+\tif (ASSERT_TRUE(wait_past(\u0026skel-\u003ebss-\u003eloops[0], loops), \"looped\"))\n+\t\tASSERT_EQ(skel-\u003ebss-\u003ecgids[0], cg2, \"cgroup after move\");\n+\tASSERT_TRUE(wait_populated(\"/kthread1\", false), \"kthread1 empty\");\n+\tASSERT_TRUE(wait_populated(\"/kthread2\", true), \"kthread2 populated\");\n+\n+\tASSERT_EQ(stop_thread(skel, 0), 0, \"stop ret\");\n+\tASSERT_TRUE(wait_populated(\"/kthread2\", false), \"kthread2 empty\");\n+out:\n+\tbpf_kthread__destroy(skel);\n+\tif (fd1 \u003e= 0)\n+\t\tclose(fd1);\n+\tif (fd2 \u003e= 0)\n+\t\tclose(fd2);\n+\tcleanup_cgroup_environment();\n+}\n+\n+void test_bpf_kthread(void)\n+{\n+\tRUN_TESTS(verifier_bpf_kthread);\n+\n+\tif (test__start_subtest(\"start_exit\"))\n+\t\ttest_start_exit();\n+\tif (test__start_subtest(\"stop_running\"))\n+\t\ttest_stop_running();\n+\tif (test__start_subtest(\"stop_park_rets\"))\n+\t\ttest_stop_park_rets();\n+\tif (test__start_subtest(\"stop_parked\"))\n+\t\ttest_stop_parked();\n+\tif (test__start_subtest(\"start_later\"))\n+\t\ttest_start_later();\n+\tif (test__start_subtest(\"self_stop\"))\n+\t\ttest_self_stop();\n+\tif (test__start_subtest(\"spawn\"))\n+\t\ttest_spawn();\n+\tif (test__start_subtest(\"misuse\"))\n+\t\ttest_misuse();\n+\tif (test__start_subtest(\"program_free\"))\n+\t\ttest_program_free();\n+\tif (test__start_subtest(\"stop_ring2\"))\n+\t\ttest_stop_ring(2);\n+\tif (test__start_subtest(\"stop_ring3\"))\n+\t\ttest_stop_ring(3);\n+\tif (test__start_subtest(\"bind\"))\n+\t\ttest_bind();\n+\tif (test__start_subtest(\"self_bind\"))\n+\t\ttest_self_bind();\n+\tif (test__start_subtest(\"attach_cgroup\"))\n+\t\ttest_attach_cgroup();\n+}\ndiff --git a/tools/testing/selftests/bpf/prog_tests/task_park.c b/tools/testing/selftests/bpf/prog_tests/task_park.c\nnew file mode 100644\nindex 00000000000000..a479a183c69683\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/prog_tests/task_park.c\n@@ -0,0 +1,311 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \u003ctest_progs.h\u003e\n+#include \u003cpthread.h\u003e\n+#include \u003csignal.h\u003e\n+#include \u003csys/prctl.h\u003e\n+#include \u003csys/syscall.h\u003e\n+#include \"task_park.skel.h\"\n+#include \"verifier_task_park.skel.h\"\n+#include \"verifier_task_park_frames.skel.h\"\n+\n+static int run_syscall_prog(struct bpf_program *prog, __u32 *retval)\n+{\n+\tLIBBPF_OPTS(bpf_test_run_opts, opts);\n+\tint err;\n+\n+\terr = bpf_prog_test_run_opts(bpf_program__fd(prog), \u0026opts);\n+\tif (!err)\n+\t\t*retval = opts.retval;\n+\treturn err;\n+}\n+\n+/* A thread blocked in a syscall program that parks. */\n+struct waiter {\n+\tstruct bpf_program *prog;\n+\tpthread_t thread;\n+\tpid_t tid;\n+\t__u32 retval;\n+\tint err;\n+\tint done;\n+};\n+\n+static void *waiter_fn(void *arg)\n+{\n+\tstruct waiter *w = arg;\n+\n+\t__atomic_store_n(\u0026w-\u003etid, syscall(SYS_gettid), __ATOMIC_RELEASE);\n+\tw-\u003eerr = run_syscall_prog(w-\u003eprog, \u0026w-\u003eretval);\n+\t__atomic_store_n(\u0026w-\u003edone, 1, __ATOMIC_RELEASE);\n+\treturn NULL;\n+}\n+\n+static bool waiter_done(struct waiter *w)\n+{\n+\treturn __atomic_load_n(\u0026w-\u003edone, __ATOMIC_ACQUIRE);\n+}\n+\n+static int start_waiter(struct waiter *w, struct bpf_program *prog)\n+{\n+\tmemset(w, 0, sizeof(*w));\n+\tw-\u003eprog = prog;\n+\treturn pthread_create(\u0026w-\u003ethread, NULL, waiter_fn, w);\n+}\n+\n+/*\n+ * Whether the waiter thread sleeps in the kernel, which for this thread can\n+ * only be the park: state 'S' in /proc/self/task/\u003ctid\u003e/stat, read past the\n+ * parenthesised comm.\n+ */\n+static bool waiter_sleeping(struct waiter *w)\n+{\n+\tpid_t tid = __atomic_load_n(\u0026w-\u003etid, __ATOMIC_ACQUIRE);\n+\tchar path[64], buf[512], *p;\n+\tint fd, n;\n+\n+\tif (!tid)\n+\t\treturn false;\n+\tsnprintf(path, sizeof(path), \"/proc/self/task/%d/stat\", tid);\n+\tfd = open(path, O_RDONLY);\n+\tif (fd \u003c 0)\n+\t\treturn false;\n+\tn = read(fd, buf, sizeof(buf) - 1);\n+\tclose(fd);\n+\tif (n \u003c= 0)\n+\t\treturn false;\n+\tbuf[n] = 0;\n+\tp = strrchr(buf, ')');\n+\treturn p \u0026\u0026 p[1] == ' ' \u0026\u0026 p[2] == 'S';\n+}\n+\n+/*\n+ * Wake the waiter until it has returned. A wake before the waiter published\n+ * itself finds no task and sets nothing; one that finds the task sets the\n+ * condition, so the waiter returns 0 only after a wake run that found it,\n+ * and such a run returns 0 or 1. Returns the last result that found a task,\n+ * or -ESRCH if none did.\n+ */\n+static int wake_until_done(struct task_park *skel, struct waiter *w)\n+{\n+\tint i, last = -ESRCH;\n+\t__u32 retval;\n+\n+\tfor (i = 0; i \u003c 5000 \u0026\u0026 !waiter_done(w); i++) {\n+\t\tif (run_syscall_prog(skel-\u003eprogs.wake_waiter, \u0026retval))\n+\t\t\tbreak;\n+\t\tif ((__s32)retval \u003e= 0)\n+\t\t\tlast = retval;\n+\t\tusleep(1000);\n+\t}\n+\treturn last;\n+}\n+\n+static void test_timeout(struct task_park *skel)\n+{\n+\t__u32 retval;\n+\n+\tif (ASSERT_OK(run_syscall_prog(skel-\u003eprogs.park_timeout, \u0026retval), \"park_timeout\"))\n+\t\tASSERT_EQ((__s32)retval, -ETIMEDOUT, \"timeout_ret\");\n+}\n+\n+static void test_flags(struct task_park *skel)\n+{\n+\t__u32 retval;\n+\n+\tif (ASSERT_OK(run_syscall_prog(skel-\u003eprogs.bad_flags, \u0026retval), \"bad_flags\"))\n+\t\tASSERT_EQ(retval, 0, \"flags_ret\");\n+}\n+\n+static void test_pending(struct task_park *skel)\n+{\n+\t__u32 retval;\n+\n+\tif (ASSERT_OK(run_syscall_prog(skel-\u003eprogs.park_pending, \u0026retval), \"park_pending\"))\n+\t\tASSERT_EQ((__s32)retval, -ETIMEDOUT, \"pending_ret\");\n+}\n+\n+static void test_wake(struct task_park *skel)\n+{\n+\tstruct waiter w;\n+\tint last;\n+\n+\tif (!ASSERT_OK(start_waiter(\u0026w, skel-\u003eprogs.park_until_woken), \"pthread_create\"))\n+\t\treturn;\n+\tlast = wake_until_done(skel, \u0026w);\n+\tpthread_join(w.thread, NULL);\n+\tASSERT_GE(last, 0, \"wake_found_waiter\");\n+\tASSERT_OK(w.err, \"park_until_woken\");\n+\tASSERT_EQ((__s32)w.retval, 0, \"wake_park_ret\");\n+}\n+\n+/*\n+ * Wake a waiter that is known to be asleep, without a timer: once the thread\n+ * shows as sleeping, the one wake that follows must report that it found the\n+ * task asleep.\n+ */\n+static void test_wake_asleep(struct task_park *skel)\n+{\n+\t__u64 timeout = skel-\u003edata-\u003epark_timeout_ns;\n+\tstruct waiter w;\n+\t__u32 retval;\n+\tint i;\n+\n+\tskel-\u003edata-\u003epark_timeout_ns = ~0ULL;\n+\tif (!ASSERT_OK(start_waiter(\u0026w, skel-\u003eprogs.park_until_woken), \"pthread_create\"))\n+\t\tgoto out;\n+\tfor (i = 0; i \u003c 5000 \u0026\u0026 !waiter_sleeping(\u0026w); i++)\n+\t\tusleep(1000);\n+\tif (ASSERT_TRUE(waiter_sleeping(\u0026w), \"waiter_sleeping\")) {\n+\t\tif (ASSERT_OK(run_syscall_prog(skel-\u003eprogs.wake_waiter, \u0026retval), \"wake_waiter\"))\n+\t\t\tASSERT_EQ((__s32)retval, 1, \"wake_found_asleep\");\n+\t}\n+\t/* Bound any further park before draining, so a lost wake cannot hang the join. */\n+\tskel-\u003edata-\u003epark_timeout_ns = timeout;\n+\twake_until_done(skel, \u0026w);\n+\tpthread_join(w.thread, NULL);\n+\tASSERT_OK(w.err, \"park_until_woken\");\n+\tASSERT_EQ((__s32)w.retval, 0, \"wake_park_ret\");\n+out:\n+\tskel-\u003edata-\u003epark_timeout_ns = timeout;\n+}\n+\n+/*\n+ * Wait until the waiter sleeps, attach the tracing program @prog, let @fire\n+ * raise its event for this thread, and check that the program's wake, not the\n+ * syscall drain, ended the park and returned @expected_ret. The program\n+ * attaches only once the waiter sleeps, since the polling sleeps of this\n+ * thread are events of its own, and the drain runs only if the event failed.\n+ */\n+static void test_wake_tracing(struct task_park *skel, struct bpf_program *prog,\n+\t\t\t      int (*fire)(void), int expected_ret)\n+{\n+\t__u64 timeout = skel-\u003edata-\u003epark_timeout_ns;\n+\tstruct bpf_link *link = NULL;\n+\tstruct waiter w;\n+\tbool woken;\n+\tint i;\n+\n+\tskel-\u003edata-\u003epark_timeout_ns = ~0ULL;\n+\tskel-\u003ebss-\u003ewaker_tid = syscall(SYS_gettid);\n+\tskel-\u003edata-\u003etracing_wake_ret = -1;\n+\tif (!ASSERT_OK(start_waiter(\u0026w, skel-\u003eprogs.park_until_woken), \"pthread_create\"))\n+\t\tgoto out;\n+\tfor (i = 0; i \u003c 5000 \u0026\u0026 !waiter_sleeping(\u0026w); i++)\n+\t\tusleep(1000);\n+\tif (ASSERT_TRUE(waiter_sleeping(\u0026w), \"waiter_sleeping\")) {\n+\t\tlink = bpf_program__attach(prog);\n+\t\tif (ASSERT_OK_PTR(link, \"attach\"))\n+\t\t\tASSERT_OK(fire(), \"fire\");\n+\t}\n+\tfor (i = 0; i \u003c 5000 \u0026\u0026 !waiter_done(\u0026w); i++)\n+\t\tusleep(1000);\n+\twoken = waiter_done(\u0026w);\n+\tbpf_link__destroy(link);\n+\t/* Bound any further park before draining, so a lost wake cannot hang the join. */\n+\tskel-\u003edata-\u003epark_timeout_ns = timeout;\n+\tif (!woken)\n+\t\twake_until_done(skel, \u0026w);\n+\tpthread_join(w.thread, NULL);\n+\tASSERT_TRUE(woken, \"woken_by_tracing_prog\");\n+\tASSERT_EQ(skel-\u003edata-\u003etracing_wake_ret, expected_ret, \"tracing_wake_ret\");\n+\tASSERT_OK(w.err, \"park_until_woken\");\n+\tASSERT_EQ((__s32)w.retval, 0, \"wake_park_ret\");\n+out:\n+\tskel-\u003edata-\u003epark_timeout_ns = timeout;\n+}\n+\n+/* Renaming the thread to its own name still fires task_rename. */\n+static int rename_self(void)\n+{\n+\tchar comm[16];\n+\n+\tif (prctl(PR_GET_NAME, comm))\n+\t\treturn -errno;\n+\treturn prctl(PR_SET_NAME, comm) ? -errno : 0;\n+}\n+\n+/* The timer that ends this sleep wakes the thread, which fires sched_waking. */\n+static int sleep_self(void)\n+{\n+\treturn usleep(1000) ? -errno : 0;\n+}\n+\n+static void sig_noop(int sig)\n+{\n+}\n+\n+/*\n+ * A program that parks cannot be the target of a tail call: a prog array takes\n+ * a syscall program that wakes and refuses one that parks.\n+ */\n+static void test_prog_array(struct task_park *skel)\n+{\n+\tint map_fd = bpf_map__fd(skel-\u003emaps.jmp_table);\n+\t__u32 key = 0;\n+\tint fd;\n+\n+\tfd = bpf_program__fd(skel-\u003eprogs.wake_waiter);\n+\tASSERT_OK(bpf_map_update_elem(map_fd, \u0026key, \u0026fd, BPF_ANY), \"member_that_wakes\");\n+\tfd = bpf_program__fd(skel-\u003eprogs.park_timeout);\n+\tASSERT_EQ(bpf_map_update_elem(map_fd, \u0026key, \u0026fd, BPF_ANY), -EINVAL, \"member_that_parks\");\n+}\n+\n+/* A signal ends the sleep, and the park reports it as -EINTR. */\n+static void test_signal(struct task_park *skel)\n+{\n+\tstruct sigaction sa = { .sa_handler = sig_noop }, old_sa;\n+\tstruct waiter w;\n+\tbool interrupted;\n+\tint i;\n+\n+\tif (!ASSERT_OK(sigaction(SIGUSR2, \u0026sa, \u0026old_sa), \"sigaction\"))\n+\t\treturn;\n+\tif (!ASSERT_OK(start_waiter(\u0026w, skel-\u003eprogs.park_until_woken), \"pthread_create\"))\n+\t\tgoto out;\n+\t/* A signal handled before the thread enters its syscall ends nothing: resend. */\n+\tfor (i = 0; i \u003c 5000 \u0026\u0026 !waiter_done(\u0026w); i++) {\n+\t\tpthread_kill(w.thread, SIGUSR2);\n+\t\tusleep(1000);\n+\t}\n+\tinterrupted = waiter_done(\u0026w);\n+\tif (!interrupted)\n+\t\twake_until_done(skel, \u0026w);\n+\tpthread_join(w.thread, NULL);\n+\tASSERT_TRUE(interrupted, \"interrupted\");\n+\tASSERT_OK(w.err, \"park_until_woken\");\n+\tASSERT_EQ((__s32)w.retval, -EINTR, \"signal_park_ret\");\n+out:\n+\tsigaction(SIGUSR2, \u0026old_sa, NULL);\n+}\n+\n+void test_task_park(void)\n+{\n+\tstruct task_park *skel;\n+\n+\tRUN_TESTS(verifier_task_park);\n+\tRUN_TESTS(verifier_task_park_frames);\n+\n+\tskel = task_park__open_and_load();\n+\tif (!ASSERT_OK_PTR(skel, \"open_and_load\"))\n+\t\treturn;\n+\tif (test__start_subtest(\"timeout\"))\n+\t\ttest_timeout(skel);\n+\tif (test__start_subtest(\"flags\"))\n+\t\ttest_flags(skel);\n+\tif (test__start_subtest(\"pending\"))\n+\t\ttest_pending(skel);\n+\tif (test__start_subtest(\"wake\"))\n+\t\ttest_wake(skel);\n+\tif (test__start_subtest(\"wake_asleep\"))\n+\t\ttest_wake_asleep(skel);\n+\tif (test__start_subtest(\"wake_tracing\"))\n+\t\ttest_wake_tracing(skel, skel-\u003eprogs.wake_on_rename, rename_self, 1);\n+\tif (test__start_subtest(\"wake_tracing_deferred\"))\n+\t\ttest_wake_tracing(skel, skel-\u003eprogs.wake_on_waking, sleep_self, 2);\n+\tif (test__start_subtest(\"prog_array\"))\n+\t\ttest_prog_array(skel);\n+\tif (test__start_subtest(\"signal\"))\n+\t\ttest_signal(skel);\n+\ttask_park__destroy(skel);\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/bpf_kthread.c b/tools/testing/selftests/bpf/progs/bpf_kthread.c\nnew file mode 100644\nindex 00000000000000..3eac7366514fa0\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/bpf_kthread.c\n@@ -0,0 +1,414 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \"bpf_experimental.h\"\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cerrno.h\u003e\n+#include \"bpf_misc.h\"\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+#define NR_THREADS 4\n+#define LOOP_BOUND (1 \u003c\u003c 23)\t/* BPF_MAX_LOOPS */\n+#define PARK_TIMEOUT_NS (10ULL * 1000 * 1000 * 1000)\n+\n+struct thread_slot {\n+\tstruct task_struct __kptr *task;\n+};\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, NR_THREADS);\n+\t__type(key, __u32);\n+\t__type(value, struct thread_slot);\n+} threads SEC(\".maps\");\n+\n+/* The context of the syscall programs below. */\n+struct kthread_args {\n+\t__u32 idx;\t\t/* the thread's slot, and the argument of its callback */\n+\t__u32 start;\t\t/* create: start the thread */\n+\t__u32 body;\t\t/* create: which callback, see enum body */\n+\t__u32 bind;\t\t/* create: bind the thread to cpu before starting it */\n+\t__u32 cpu;\t\t/* create and bind: the CPU */\n+\t__u64 cgroup_id;\t/* attach_cgroup: the cgroup to move the thread into */\n+};\n+\n+enum body {\n+\tBODY_WORKER,\t\t/* loop until told to return or asked to stop */\n+\tBODY_SPAWNER,\t\t/* create and start thread 1 first, then as above */\n+\tBODY_SELF_STOPPER,\t/* try to stop itself first, then as above */\n+\tBODY_RING,\t\t/* stop the next thread of a ring, then return idx + 1 */\n+\tBODY_STOP_CHECKER,\t/* park until stopped, then record what later parks return */\n+\tBODY_SELF_BINDER,\t/* bind itself to self_bind_cpu first, then as worker */\n+};\n+\n+__u32 runs[NR_THREADS];\t\t/* callback entries, per slot */\n+__u32 tids[NR_THREADS];\t\t/* the thread's PID, per slot */\n+char comms[NR_THREADS][16];\t/* the comm the thread read for itself, per slot */\n+__u32 cpus[NR_THREADS];\t\t/* the CPU the thread last ran on, per slot */\n+__u32 loops[NR_THREADS];\t/* iterations of the body's loop, per slot */\n+__u64 cgids[NR_THREADS];\t/* the cgroup the thread last ran in, per slot */\n+__u32 exit_now[NR_THREADS];\t/* set by the test: the callback returns idx + 1 */\n+int self_stop_ret;\t\t/* bpf_kthread_stop() on the calling thread */\n+int spawn_ret;\t\t\t/* bpf_kthread_start() by the spawner */\n+int foreign_start_ret;\t\t/* bpf_kthread_start() on a task that is no BPF kthread */\n+int foreign_stop_ret;\t\t/* bpf_kthread_stop() on the same */\n+int foreign_attach_ret;\t\t/* bpf_kthread_attach_cgroup() on the same */\n+__u32 ring_size;\t\t/* set by the test: threads in the ring */\n+__u32 ring_ready;\t\t/* ring threads about to stop their successor */\n+int ring_stop_ret[NR_THREADS];\t/* what stopping the successor returned, per slot */\n+int stop_park_rets[4];\t\t/* what the park returned once a stop was pending */\n+__u32 self_bind_cpu;\t\t/* set by the test: where the self binder binds itself */\n+int self_bind_ret;\t\t/* what that bind returned */\n+\n+static __u32 load(__u32 *p)\n+{\n+\treturn *(volatile __u32 *)p;\n+}\n+\n+/*\n+ * The body of a thread: record who runs it, then sleep in bpf_task_park()\n+ * until the test wakes it to return or bpf_kthread_stop() asks it to stop. The\n+ * park's timeout is ten seconds, well past what a test waits, so that a lost\n+ * wake fails the test instead of passing on the timeout. The bounded loop\n+ * outlasts any test; a callback cannot loop without a bound.\n+ */\n+static int worker(__u64 arg)\n+{\n+\t__u64 idx = arg;\n+\n+\tif (idx \u003e= NR_THREADS)\n+\t\treturn -EINVAL;\n+\ttids[idx] = bpf_get_current_pid_tgid();\n+\tbpf_get_current_comm(comms[idx], sizeof(comms[idx]));\n+\t__sync_fetch_and_add(\u0026runs[idx], 1);\n+\n+\tbpf_repeat(LOOP_BOUND) {\n+\t\tcpus[idx] = bpf_get_smp_processor_id();\n+\t\tcgids[idx] = bpf_get_current_cgroup_id();\n+\t\t__sync_fetch_and_add(\u0026loops[idx], 1);\n+\t\tif (bpf_kthread_should_stop())\n+\t\t\treturn 0;\n+\t\tif (load(\u0026exit_now[idx]))\n+\t\t\treturn idx + 1;\n+\t\tbpf_task_park(PARK_TIMEOUT_NS, 0);\n+\t}\n+\treturn -ETIME;\n+}\n+\n+/*\n+ * What bpf_task_park() returns to a thread that is being stopped: -ESHUTDOWN\n+ * from the park that the stop ends or finds, and from every later one, a poll\n+ * included, except one that finds a wake pending, which wins as it does over a\n+ * signal. The stop is pending for good, so none of this depends on where the\n+ * thread was when it arrived.\n+ */\n+static int stop_checker(__u64 arg)\n+{\n+\t__u64 idx = arg;\n+\tint ret = 0;\n+\n+\tif (idx \u003e= NR_THREADS)\n+\t\treturn -EINVAL;\n+\t__sync_fetch_and_add(\u0026runs[idx], 1);\n+\n+\tbpf_repeat(LOOP_BOUND) {\n+\t\tret = bpf_task_park(PARK_TIMEOUT_NS, 0);\n+\t\tif (ret == -ESHUTDOWN)\n+\t\t\tbreak;\n+\t}\n+\tstop_park_rets[0] = ret;\n+\tstop_park_rets[1] = bpf_task_park(0, 0);\n+\tbpf_task_wake(bpf_get_current_task_btf(), 0);\n+\tstop_park_rets[2] = bpf_task_park(PARK_TIMEOUT_NS, 0);\n+\tstop_park_rets[3] = bpf_task_park(PARK_TIMEOUT_NS, 0);\n+\treturn idx + 1;\n+}\n+\n+/* Publish @task in slot @idx, releasing whatever the slot held. */\n+static void publish(__u32 idx, struct task_struct *task)\n+{\n+\tstruct thread_slot *slot;\n+\n+\tslot = bpf_map_lookup_elem(\u0026threads, \u0026idx);\n+\tif (!slot) {\n+\t\tbpf_task_release(task);\n+\t\treturn;\n+\t}\n+\ttask = bpf_kptr_xchg(\u0026slot-\u003etask, task);\n+\tif (task)\n+\t\tbpf_task_release(task);\n+}\n+\n+/* Thread 0 of the spawn case: it creates and starts thread 1, then runs as worker 0. */\n+static int spawner(__u64 arg)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(worker, 1, 0);\n+\tif (!task) {\n+\t\tspawn_ret = -ENOMEM;\n+\t\treturn -ENOMEM;\n+\t}\n+\tspawn_ret = bpf_kthread_start(task, 0);\n+\tpublish(1, task);\n+\treturn worker(arg);\n+}\n+\n+static int self_stopper(__u64 arg)\n+{\n+\tself_stop_ret = bpf_kthread_stop(bpf_get_current_task_btf(), 0);\n+\treturn worker(arg);\n+}\n+\n+/*\n+ * Thread @idx of a ring of ring_size threads stops thread idx + 1, the last\n+ * one thread 0. Each waits until all of them are about to stop, so that the\n+ * stops form a ring of waits: the stop that would close it is refused with\n+ * -EDEADLK and the others complete as the ring unwinds. A thread returns\n+ * idx + 1 once its stop has returned, whichever way it did.\n+ */\n+static int ring_stopper(__u64 arg)\n+{\n+\t__u64 idx = arg, size = load(\u0026ring_size);\n+\tstruct task_struct *task = NULL, *peer_task;\n+\tstruct thread_slot *slot;\n+\t__u32 peer;\n+\n+\tif (idx \u003e= NR_THREADS || !size)\n+\t\treturn -EINVAL;\n+\ttids[idx] = bpf_get_current_pid_tgid();\n+\t__sync_fetch_and_add(\u0026runs[idx], 1);\n+\n+\tpeer = idx + 1 \u003c size ? idx + 1 : 0;\n+\tslot = bpf_map_lookup_elem(\u0026threads, \u0026peer);\n+\tif (!slot)\n+\t\treturn -ENOENT;\n+\tbpf_rcu_read_lock();\n+\tpeer_task = slot-\u003etask;\n+\tif (peer_task)\n+\t\ttask = bpf_task_acquire(peer_task);\n+\tbpf_rcu_read_unlock();\n+\tif (!task)\n+\t\treturn -ESRCH;\n+\n+\t/* A stop can only arrive once every thread is at the barrier: leave it then too. */\n+\t__sync_fetch_and_add(\u0026ring_ready, 1);\n+\tbpf_repeat(LOOP_BOUND) {\n+\t\tif (load(\u0026ring_ready) \u003e= size || bpf_kthread_should_stop())\n+\t\t\tbreak;\n+\t\tbpf_task_park(1000000, 0);\n+\t}\n+\tring_stop_ret[idx] = bpf_kthread_stop(task, 0);\n+\tbpf_task_release(task);\n+\treturn idx + 1;\n+}\n+\n+/* Bind itself to self_bind_cpu, then run as a worker, which records its CPU. */\n+static int self_binder(__u64 arg)\n+{\n+\tself_bind_ret = bpf_kthread_bind(bpf_get_current_task_btf(), load(\u0026self_bind_cpu), 0);\n+\treturn worker(arg);\n+}\n+\n+/* Take the thread out of slot @idx; the caller releases it. */\n+static struct task_struct *take(__u32 idx)\n+{\n+\tstruct thread_slot *slot;\n+\n+\tslot = bpf_map_lookup_elem(\u0026threads, \u0026idx);\n+\tif (!slot)\n+\t\treturn NULL;\n+\treturn bpf_kptr_xchg(\u0026slot-\u003etask, NULL);\n+}\n+\n+SEC(\"syscall\")\n+int create_thread(struct kthread_args *args)\n+{\n+\tstruct task_struct *task;\n+\tint ret;\n+\n+\tif (args-\u003ebody == BODY_SPAWNER)\n+\t\ttask = bpf_kthread_create(spawner, args-\u003eidx, 0);\n+\telse if (args-\u003ebody == BODY_SELF_STOPPER)\n+\t\ttask = bpf_kthread_create(self_stopper, args-\u003eidx, 0);\n+\telse if (args-\u003ebody == BODY_RING)\n+\t\ttask = bpf_kthread_create(ring_stopper, args-\u003eidx, 0);\n+\telse if (args-\u003ebody == BODY_STOP_CHECKER)\n+\t\ttask = bpf_kthread_create(stop_checker, args-\u003eidx, 0);\n+\telse if (args-\u003ebody == BODY_SELF_BINDER)\n+\t\ttask = bpf_kthread_create(self_binder, args-\u003eidx, 0);\n+\telse\n+\t\ttask = bpf_kthread_create(worker, args-\u003eidx, 0);\n+\tif (!task)\n+\t\treturn -EAGAIN;\n+\tif (args-\u003ebind) {\n+\t\tret = bpf_kthread_bind(task, args-\u003ecpu, 0);\n+\t\tif (ret)\n+\t\t\tgoto stop;\n+\t}\n+\tif (args-\u003estart) {\n+\t\tret = bpf_kthread_start(task, 0);\n+\t\tif (ret)\n+\t\t\tgoto stop;\n+\t}\n+\tpublish(args-\u003eidx, task);\n+\treturn 0;\n+stop:\n+\tbpf_kthread_stop(task, 0);\n+\tbpf_task_release(task);\n+\treturn ret;\n+}\n+\n+/* Start the thread of slot idx through the RCU-protected pointer in its slot. */\n+SEC(\"syscall\")\n+int start_thread(struct kthread_args *args)\n+{\n+\tstruct thread_slot *slot;\n+\tstruct task_struct *task;\n+\t__u32 idx = args-\u003eidx;\n+\tint ret = -ESRCH;\n+\n+\tslot = bpf_map_lookup_elem(\u0026threads, \u0026idx);\n+\tif (!slot)\n+\t\treturn -ENOENT;\n+\tbpf_rcu_read_lock();\n+\ttask = slot-\u003etask;\n+\tif (task)\n+\t\tret = bpf_kthread_start(task, 0);\n+\tbpf_rcu_read_unlock();\n+\treturn ret;\n+}\n+\n+/* Wake the thread of slot idx from its park, through the RCU-protected pointer in its slot. */\n+SEC(\"syscall\")\n+int wake_thread(struct kthread_args *args)\n+{\n+\tstruct thread_slot *slot;\n+\tstruct task_struct *task;\n+\t__u32 idx = args-\u003eidx;\n+\tint ret = -ESRCH;\n+\n+\tslot = bpf_map_lookup_elem(\u0026threads, \u0026idx);\n+\tif (!slot)\n+\t\treturn -ENOENT;\n+\tbpf_rcu_read_lock();\n+\ttask = slot-\u003etask;\n+\tif (task)\n+\t\tret = bpf_task_wake(task, 0);\n+\tbpf_rcu_read_unlock();\n+\treturn ret;\n+}\n+\n+/* Bind the thread of slot idx to args-\u003ecpu, running or not. */\n+SEC(\"syscall\")\n+int bind_thread(struct kthread_args *args)\n+{\n+\tstruct task_struct *task;\n+\tint ret;\n+\n+\ttask = take(args-\u003eidx);\n+\tif (!task)\n+\t\treturn -ESRCH;\n+\tret = bpf_kthread_bind(task, args-\u003ecpu, 0);\n+\tpublish(args-\u003eidx, task);\n+\treturn ret;\n+}\n+\n+/* Move the thread of slot idx into the cgroup args-\u003ecgroup_id, running or not. */\n+SEC(\"syscall\")\n+int attach_cgroup(struct kthread_args *args)\n+{\n+\tstruct task_struct *task;\n+\tstruct cgroup *cgrp;\n+\tint ret = -ENOENT;\n+\n+\ttask = take(args-\u003eidx);\n+\tif (!task)\n+\t\treturn -ESRCH;\n+\tcgrp = bpf_cgroup_from_id(args-\u003ecgroup_id);\n+\tif (cgrp) {\n+\t\tret = bpf_kthread_attach_cgroup(task, cgrp, 0);\n+\t\tbpf_cgroup_release(cgrp);\n+\t}\n+\tpublish(args-\u003eidx, task);\n+\treturn ret;\n+}\n+\n+/* bpf_kthread_attach_cgroup() on the root cgroup with @flags. */\n+static int attach_root_cgroup(struct task_struct *task, __u64 flags)\n+{\n+\tstruct cgroup *cgrp;\n+\tint ret;\n+\n+\tcgrp = bpf_cgroup_from_id(1);\n+\tif (!cgrp)\n+\t\treturn -ENOENT;\n+\tret = bpf_kthread_attach_cgroup(task, cgrp, flags);\n+\tbpf_cgroup_release(cgrp);\n+\treturn ret;\n+}\n+\n+/* Stop the thread of slot idx and empty the slot; returns what the stop returned. */\n+SEC(\"syscall\")\n+int stop_thread(struct kthread_args *args)\n+{\n+\tstruct task_struct *task;\n+\tint ret;\n+\n+\ttask = take(args-\u003eidx);\n+\tif (!task)\n+\t\treturn -ESRCH;\n+\tret = bpf_kthread_stop(task, 0);\n+\tbpf_task_release(task);\n+\treturn ret;\n+}\n+\n+/* The control kfuncs refuse a task that is no BPF kthread, init here. */\n+SEC(\"syscall\")\n+int control_foreign_task(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_task_from_pid(1);\n+\tif (!task)\n+\t\treturn -ESRCH;\n+\tforeign_start_ret = bpf_kthread_start(task, 0);\n+\tforeign_stop_ret = bpf_kthread_stop(task, 0);\n+\tforeign_attach_ret = attach_root_cgroup(task, 0);\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+/*\n+ * A thread of a user process is never asked to stop, nonzero flags are refused\n+ * everywhere, and a thread stopped before it was started reports -EINTR.\n+ */\n+SEC(\"syscall\")\n+int misuse(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\tint ret;\n+\n+\tif (bpf_kthread_should_stop())\n+\t\treturn 1;\n+\ttask = bpf_kthread_create(worker, 0, 1);\n+\tif (task) {\n+\t\tbpf_kthread_stop(task, 0);\n+\t\tbpf_task_release(task);\n+\t\treturn 2;\n+\t}\n+\ttask = bpf_kthread_create(worker, 0, 0);\n+\tif (!task)\n+\t\treturn 3;\n+\tif (bpf_kthread_start(task, 1) != -EINVAL)\n+\t\tret = 4;\n+\telse if (bpf_kthread_stop(task, 1) != -EINVAL)\n+\t\tret = 5;\n+\telse if (attach_root_cgroup(task, 1) != -EINVAL)\n+\t\tret = 6;\n+\telse\n+\t\tret = bpf_kthread_stop(task, 0) == -EINTR ? 0 : 7;\n+\tbpf_task_release(task);\n+\treturn ret;\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/task_park.c b/tools/testing/selftests/bpf/progs/task_park.c\nnew file mode 100644\nindex 00000000000000..2320ede02cc9d4\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/task_park.c\n@@ -0,0 +1,218 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \"bpf_experimental.h\"\n+#include \u003cerrno.h\u003e\n+\n+/* Exercise bpf_task_park() from syscall programs, bpf_task_wake() from those and tracing ones. */\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+extern struct task_struct *bpf_task_acquire(struct task_struct *task) __ksym;\n+extern void bpf_task_release(struct task_struct *task) __ksym;\n+extern void bpf_rcu_read_lock(void) __ksym;\n+extern void bpf_rcu_read_unlock(void) __ksym;\n+\n+#define READ_ONCE(x) (*(volatile typeof(x) *)\u0026(x))\n+#define WRITE_ONCE(x, v) (*(volatile typeof(x) *)\u0026(x) = (v))\n+\n+enum { WAITING, WOKEN };\n+\n+/* The waiter publishes itself here; the waker sets the condition and wakes it. */\n+struct park_elem {\n+\tstruct task_struct __kptr *task;\n+\t__u32 state;\n+};\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, struct park_elem);\n+} park_map SEC(\".maps\");\n+\n+/* A tail call would run its target under the caller's protection, which a park leaves. */\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_PROG_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__uint(key_size, sizeof(__u32));\n+\t__uint(value_size, sizeof(__u32));\n+} jmp_table SEC(\".maps\");\n+\n+/* Bound on each sleep of the waiter, so that a lost wake fails a test instead of hanging it. */\n+__u64 park_timeout_ns = 10ULL * 1000 * 1000 * 1000;\n+\n+static __always_inline struct park_elem *lookup_elem(void)\n+{\n+\t__u32 key = 0;\n+\n+\treturn bpf_map_lookup_elem(\u0026park_map, \u0026key);\n+}\n+\n+/* Reset the condition, then publish the task: a wake between the two sets it again. */\n+static int publish_waiter(void)\n+{\n+\tstruct park_elem *elem = lookup_elem();\n+\tstruct task_struct *task;\n+\n+\tif (!elem)\n+\t\treturn -ENOENT;\n+\tWRITE_ONCE(elem-\u003estate, WAITING);\n+\ttask = bpf_task_acquire(bpf_get_current_task_btf());\n+\tif (!task)\n+\t\treturn -ESRCH;\n+\ttask = bpf_kptr_xchg(\u0026elem-\u003etask, task);\n+\tif (task)\n+\t\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+static void withdraw_waiter(void)\n+{\n+\tstruct park_elem *elem = lookup_elem();\n+\tstruct task_struct *task;\n+\n+\tif (!elem)\n+\t\treturn;\n+\ttask = bpf_kptr_xchg(\u0026elem-\u003etask, NULL);\n+\tif (task)\n+\t\tbpf_task_release(task);\n+}\n+\n+/*\n+ * Sleep until wake_waiter() has set the condition. The element is a value of\n+ * an array map, which the program holds, so its pointer survives the park and\n+ * the loop keeps it; a value of a hash map would have to be looked up again\n+ * on every turn. A park that returns 0 before the condition is set is a\n+ * spurious wakeup that just goes round once more.\n+ */\n+SEC(\"syscall\")\n+int park_until_woken(void *ctx)\n+{\n+\tstruct park_elem *elem;\n+\tint i, ret;\n+\n+\tret = publish_waiter();\n+\tif (ret)\n+\t\treturn ret;\n+\telem = lookup_elem();\n+\tif (!elem)\n+\t\treturn -ENOENT;\n+\tret = -EAGAIN;\n+\tfor (i = 0; i \u003c 100; i++) {\n+\t\tif (READ_ONCE(elem-\u003estate) == WOKEN) {\n+\t\t\tret = 0;\n+\t\t\tbreak;\n+\t\t}\n+\t\tret = bpf_task_park(park_timeout_ns, 0);\n+\t\tif (ret)\n+\t\t\tbreak;\n+\t\tret = -EAGAIN;\n+\t}\n+\twithdraw_waiter();\n+\treturn ret;\n+}\n+\n+/*\n+ * Wake whoever published itself, without taking a reference. The condition is\n+ * set only once a task is found: a waiter between its reset of the condition\n+ * and its publication must not find it set, or it would return without any\n+ * wake having reached it. Setting the condition before the wake is the order\n+ * the kfunc's release store pairs with, so a park that consumes this wake\n+ * sees the condition on its next look.\n+ */\n+SEC(\"syscall\")\n+int wake_waiter(void *ctx)\n+{\n+\tstruct park_elem *elem = lookup_elem();\n+\tstruct task_struct *task;\n+\tint ret = -ESRCH;\n+\n+\tif (!elem)\n+\t\treturn -ENOENT;\n+\tbpf_rcu_read_lock();\n+\ttask = elem-\u003etask;\n+\tif (task) {\n+\t\tWRITE_ONCE(elem-\u003estate, WOKEN);\n+\t\tret = bpf_task_wake(task, 0);\n+\t}\n+\tbpf_rcu_read_unlock();\n+\treturn ret;\n+}\n+\n+/* Set by the test: the thread whose events wake the waiter. */\n+__u32 waker_tid;\n+/* What the last wake from a tracing program returned. */\n+int tracing_wake_ret = -1;\n+\n+/*\n+ * The same wake from a tracing program, on an event of the thread the test\n+ * names. The task kptr read under bpf_rcu_read_lock() is what bpf_task_wake()\n+ * accepts, so the event takes no reference.\n+ */\n+static __always_inline void wake_waiter_on_event(struct task_struct *task)\n+{\n+\tstruct park_elem *elem = lookup_elem();\n+\tstruct task_struct *waiter;\n+\n+\tif (!elem || task-\u003epid != waker_tid)\n+\t\treturn;\n+\tbpf_rcu_read_lock();\n+\twaiter = elem-\u003etask;\n+\tif (waiter) {\n+\t\tWRITE_ONCE(elem-\u003estate, WOKEN);\n+\t\ttracing_wake_ret = bpf_task_wake(waiter, 0);\n+\t}\n+\tbpf_rcu_read_unlock();\n+}\n+\n+/* The wake from process context: task_rename fires from prctl(). */\n+SEC(\"tp_btf/task_rename\")\n+int BPF_PROG(wake_on_rename, struct task_struct *task, const char *comm)\n+{\n+\twake_waiter_on_event(task);\n+\treturn 0;\n+}\n+\n+/*\n+ * The wake from under the scheduler's locks: sched_waking fires inside\n+ * try_to_wake_up() with the woken task's pi_lock held and IRQs disabled,\n+ * where the wake cannot be issued, so it is queued for irq_work and returns 2.\n+ */\n+SEC(\"tp_btf/sched_waking\")\n+int BPF_PROG(wake_on_waking, struct task_struct *p)\n+{\n+\twake_waiter_on_event(p);\n+\treturn 0;\n+}\n+\n+/* Nothing wakes this task: the park sleeps 1 ms and times out. */\n+SEC(\"syscall\")\n+int park_timeout(void *ctx)\n+{\n+\treturn bpf_task_park(1000000, 0);\n+}\n+\n+/*\n+ * A wake that finds its task running is kept for the next park, which returns\n+ * at once even as a poll. That park consumes it, so a second poll finds\n+ * nothing and reports the timeout.\n+ */\n+SEC(\"syscall\")\n+int park_pending(void *ctx)\n+{\n+\tif (bpf_task_wake(bpf_get_current_task_btf(), 0))\n+\t\treturn -EBUSY;\n+\tif (bpf_task_park(0, 0))\n+\t\treturn -EAGAIN;\n+\treturn bpf_task_park(0, 0);\n+}\n+\n+SEC(\"syscall\")\n+int bad_flags(void *ctx)\n+{\n+\tif (bpf_task_park(0, 1) != -EINVAL)\n+\t\treturn 1;\n+\tif (bpf_task_wake(bpf_get_current_task_btf(), 1) != -EINVAL)\n+\t\treturn 2;\n+\treturn 0;\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_arena_stack.c b/tools/testing/selftests/bpf/progs/verifier_arena_stack.c\nindex b46e30a9c67f64..e09a9ce101876b 100644\n--- a/tools/testing/selftests/bpf/progs/verifier_arena_stack.c\n+++ b/tools/testing/selftests/bpf/progs/verifier_arena_stack.c\n@@ -1307,6 +1307,33 @@ __naked void packet_write_prologue(void)\n \t: __clobber_all);\n }\n \n+extern int bpf_task_park(__u64 timeout_ns, __u64 flags) __ksym;\n+\n+/* the call is in asm, which gives the kfunc no BTF */\n+void __park_btf_root(void)\n+{\n+\tbpf_task_park(0, 0);\n+}\n+\n+/* The stack goes back to the cpu that the run ends on, and a park may change that cpu. */\n+SEC(\"syscall\")\n+__description(\"arena stack: a program that parks\")\n+__failure __msg(\"bpf_task_park() cannot be called in a program that uses R12\")\n+__naked void parks(void)\n+{\n+\tasm volatile (\n+\t\".8byte %[adj];\"\n+\t\"r1 = 0;\"\n+\t\"r2 = 0;\"\n+\t\"call %[bpf_task_park];\"\n+\t\"r0 = 0;\"\n+\t\"exit;\"\n+\t:\n+\t: __imm_insn(adj, ADJ(16)),\n+\t  __imm(bpf_task_park)\n+\t: __clobber_all);\n+}\n+\n #endif\n \n char _license[] SEC(\"license\") = \"GPL\";\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_bpf_kthread.c b/tools/testing/selftests/bpf/progs/verifier_bpf_kthread.c\nnew file mode 100644\nindex 00000000000000..26f73270269892\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/verifier_bpf_kthread.c\n@@ -0,0 +1,398 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \"bpf_experimental.h\"\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \"bpf_misc.h\"\n+\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+struct slot {\n+\tstruct task_struct __kptr *task;\n+\tstruct task_struct __kptr_untrusted *untrusted_task;\n+};\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, struct slot);\n+} slots SEC(\".maps\");\n+\n+static struct slot *lookup_slot(void)\n+{\n+\t__u32 key = 0;\n+\n+\treturn bpf_map_lookup_elem(\u0026slots, \u0026key);\n+}\n+\n+static int body(__u64 arg)\n+{\n+\treturn 0;\n+}\n+\n+static int body_negative(__u64 arg)\n+{\n+\treturn -1;\n+}\n+\n+static int body_deref_arg(__u64 arg)\n+{\n+\treturn *(int *)arg;\n+}\n+\n+static int body_spawns(__u64 arg)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(body, arg, 0);\n+\tif (!task)\n+\t\treturn -1;\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+static int body_parks(__u64 arg)\n+{\n+\treturn bpf_task_park(0, 0);\n+}\n+\n+/* A global function that parks is admitted from the thread body as from the main program. */\n+__noinline int kthread_park_global(void)\n+{\n+\treturn bpf_task_park(0, 0);\n+}\n+\n+static int body_parks_via_global(__u64 arg)\n+{\n+\treturn kthread_park_global();\n+}\n+\n+static int park_cb(__u32 index, void *data)\n+{\n+\tbpf_task_park(0, 0);\n+\treturn 1;\n+}\n+\n+static int body_parks_in_callback(__u64 arg)\n+{\n+\tbpf_loop(1, park_cb, NULL, 0);\n+\treturn 0;\n+}\n+\n+static int park_global_cb(__u32 index, void *data)\n+{\n+\tkthread_park_global();\n+\treturn 1;\n+}\n+\n+static int body_parks_via_global_in_callback(__u64 arg)\n+{\n+\tbpf_loop(1, park_global_cb, NULL, 0);\n+\treturn 0;\n+}\n+\n+/* The task bpf_kthread_create() returns carries a reference the program must release. */\n+SEC(\"syscall\")\n+__failure __msg(\"Unreleased reference\")\n+int create_leaks_task(void *ctx)\n+{\n+\treturn bpf_kthread_create(body, 0, 0) != NULL;\n+}\n+\n+SEC(\"syscall\")\n+__success\n+int create_releases_task(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(body, 0, 0);\n+\tif (!task)\n+\t\treturn 0;\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+/* The callback returns an int and is checked as one: a negative return is fine. */\n+SEC(\"syscall\")\n+__success\n+int callback_returns_negative(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(body_negative, 0, 0);\n+\tif (!task)\n+\t\treturn 0;\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+/* The callback's argument is a number, whatever the creator passed. */\n+SEC(\"syscall\")\n+__failure __msg(\"R1 invalid mem access 'scalar'\")\n+int callback_dereferences_arg(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(body_deref_arg, 0, 0);\n+\tif (!task)\n+\t\treturn 0;\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+/* A thread may create threads. */\n+SEC(\"syscall\")\n+__success\n+int callback_creates_thread(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(body_spawns, 0, 0);\n+\tif (!task)\n+\t\treturn 0;\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+/* The thread body may sleep in bpf_task_park(), directly or through a global function. */\n+SEC(\"syscall\")\n+__success\n+int callback_parks(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(body_parks, 0, 0);\n+\tif (!task)\n+\t\treturn 0;\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__success\n+int callback_parks_via_global(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(body_parks_via_global, 0, 0);\n+\tif (!task)\n+\t\treturn 0;\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+/* A callback under the thread body is a callback like any other: no park there. */\n+SEC(\"syscall\")\n+__failure __msg(\"bpf_task_park() is not allowed in a callback\")\n+int callback_parks_in_nested_callback(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(body_parks_in_callback, 0, 0);\n+\tif (!task)\n+\t\treturn 0;\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"global function kthread_park_global() may call bpf_task_park(), \"\n+\t\t\"which is not allowed in a callback\")\n+int callback_parks_via_global_in_nested_callback(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(body_parks_via_global_in_callback, 0, 0);\n+\tif (!task)\n+\t\treturn 0;\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+__u32 indexed[4];\n+\n+/* The argument is an unknown scalar: the body bounds it before it indexes with it. */\n+static int body_indexes_arg(__u64 arg)\n+{\n+\tif (arg \u003c 4)\n+\t\treturn indexed[arg];\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__success\n+int callback_indexes_with_arg(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(body_indexes_arg, 1, 0);\n+\tif (!task)\n+\t\treturn 0;\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+/*\n+ * The creation waits for kthreadd with migration enabled, so the program may\n+ * go on on another CPU, where the time slice of may_goto is another run's.\n+ */\n+SEC(\"syscall\")\n+__failure __msg(\"may_goto cannot be used in a program that may migrate\")\n+int create_with_may_goto(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\tint i;\n+\n+\tfor (i = 0; i \u003c 4 \u0026\u0026 can_loop; i++) {\n+\t\ttask = bpf_kthread_create(body_indexes_arg, i, 0);\n+\t\tif (task)\n+\t\t\tbpf_task_release(task);\n+\t}\n+\treturn 0;\n+}\n+\n+struct wq_elem {\n+\tstruct bpf_wq wq;\n+};\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, struct wq_elem);\n+} wq_map SEC(\".maps\");\n+\n+/* Taking the address of a body that parks does not make this function park. */\n+__noinline int spawn_parking_thread(void)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(body_parks, 0, 0);\n+\tif (!task)\n+\t\treturn -1;\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+static int spawn_wq_cb(void *map, int *key, void *value)\n+{\n+\tspawn_parking_thread();\n+\treturn 0;\n+}\n+\n+/* So a global function that creates such a thread may be called from a callback. */\n+SEC(\"syscall\")\n+__success\n+int global_spawner_in_wq_callback(void *ctx)\n+{\n+\tstruct wq_elem *elem;\n+\t__u32 key = 0;\n+\n+\telem = bpf_map_lookup_elem(\u0026wq_map, \u0026key);\n+\tif (!elem)\n+\t\treturn 0;\n+\tif (bpf_wq_init(\u0026elem-\u003ewq, \u0026wq_map, 0))\n+\t\treturn 0;\n+\treturn bpf_wq_set_callback(\u0026elem-\u003ewq, spawn_wq_cb, 0);\n+}\n+\n+/*\n+ * bpf_kthread_stop() takes a referenced or trusted task; bpf_kthread_start()\n+ * also an RCU-protected one that is not NULL.\n+ */\n+SEC(\"syscall\")\n+__failure __msg(\"R1 must be referenced or trusted\")\n+int stop_untrusted_task(void *ctx)\n+{\n+\tstruct slot *slot = lookup_slot();\n+\tstruct task_struct *task;\n+\n+\tif (!slot)\n+\t\treturn 0;\n+\ttask = slot-\u003euntrusted_task;\n+\tif (!task)\n+\t\treturn 0;\n+\treturn bpf_kthread_stop(task, 0);\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"R1 must be a rcu pointer\")\n+int start_untrusted_task(void *ctx)\n+{\n+\tstruct slot *slot = lookup_slot();\n+\tstruct task_struct *task;\n+\n+\tif (!slot)\n+\t\treturn 0;\n+\ttask = slot-\u003euntrusted_task;\n+\tif (!task)\n+\t\treturn 0;\n+\treturn bpf_kthread_start(task, 0);\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"Possibly NULL pointer passed to trusted R1\")\n+int start_maybe_null_task(void *ctx)\n+{\n+\tstruct slot *slot = lookup_slot();\n+\tstruct task_struct *task;\n+\tint ret;\n+\n+\tif (!slot)\n+\t\treturn 0;\n+\tbpf_rcu_read_lock();\n+\ttask = slot-\u003etask;\n+\tret = bpf_kthread_start(task, 0);\n+\tbpf_rcu_read_unlock();\n+\treturn ret;\n+}\n+\n+SEC(\"syscall\")\n+__success\n+int start_rcu_task(void *ctx)\n+{\n+\tstruct slot *slot = lookup_slot();\n+\tstruct task_struct *task;\n+\tint ret = 0;\n+\n+\tif (!slot)\n+\t\treturn 0;\n+\tbpf_rcu_read_lock();\n+\ttask = slot-\u003etask;\n+\tif (task)\n+\t\tret = bpf_kthread_start(task, 0);\n+\tbpf_rcu_read_unlock();\n+\treturn ret;\n+}\n+\n+/* bpf_kthread_stop() sleeps, so a pointer that only an RCU section protects will not do. */\n+SEC(\"syscall\")\n+__failure __msg(\"kernel func bpf_kthread_stop is sleepable within rcu_read_lock region\")\n+int stop_rcu_task(void *ctx)\n+{\n+\tstruct slot *slot = lookup_slot();\n+\tstruct task_struct *task;\n+\tint ret = 0;\n+\n+\tif (!slot)\n+\t\treturn 0;\n+\tbpf_rcu_read_lock();\n+\ttask = slot-\u003etask;\n+\tif (task)\n+\t\tret = bpf_kthread_stop(task, 0);\n+\tbpf_rcu_read_unlock();\n+\treturn ret;\n+}\n+\n+SEC(\"tp/syscalls/sys_enter_nanosleep\")\n+__failure __msg(\"calling kernel function bpf_kthread_create is not allowed\")\n+int create_from_tracepoint(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_kthread_create(body, 0, 0);\n+\tif (task)\n+\t\tbpf_task_release(task);\n+\treturn 0;\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_task_park.c b/tools/testing/selftests/bpf/progs/verifier_task_park.c\nnew file mode 100644\nindex 00000000000000..33bc29f0805871\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/verifier_task_park.c\n@@ -0,0 +1,550 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#define BPF_NO_KFUNC_PROTOTYPES\n+#include \u003cvmlinux.h\u003e\n+#include \u003cerrno.h\u003e\n+#include \u003cbpf/bpf_helpers.h\u003e\n+#include \u003cbpf/bpf_tracing.h\u003e\n+#include \"bpf_misc.h\"\n+#include \"bpf_experimental.h\"\n+#include \u003cbpf_arena_common.h\u003e\n+\n+/* What survives a bpf_task_park(), what does not, and where the park is refused. */\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+extern struct task_struct *bpf_task_acquire(struct task_struct *task) __ksym;\n+extern void bpf_task_release(struct task_struct *task) __ksym;\n+extern void bpf_rcu_read_lock(void) __ksym;\n+extern void bpf_rcu_read_unlock(void) __ksym;\n+extern void bpf_local_irq_save(unsigned long *flags) __weak __ksym;\n+extern void bpf_local_irq_restore(unsigned long *flags) __weak __ksym;\n+extern int bpf_res_spin_lock(struct bpf_res_spin_lock *lock) __weak __ksym;\n+extern void bpf_res_spin_unlock(struct bpf_res_spin_lock *lock) __weak __ksym;\n+extern int bpf_iter_bits_new(struct bpf_iter_bits *it, const u64 *unsafe_ptr__ign,\n+\t\t\t     u32 nr_words) __weak __ksym;\n+extern void bpf_iter_bits_destroy(struct bpf_iter_bits *it) __weak __ksym;\n+\n+struct park_elem {\n+\tstruct bpf_spin_lock lock;\n+\tstruct task_struct __kptr *task;\n+\tstruct task_struct __kptr_untrusted *untrusted_task;\n+\t__u32 state;\n+};\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, struct park_elem);\n+} park_map SEC(\".maps\");\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_HASH);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, struct park_elem);\n+} hash_map SEC(\".maps\");\n+\n+struct rlock_elem {\n+\tstruct bpf_res_spin_lock lock;\n+\t__u32 state;\n+};\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, struct rlock_elem);\n+} rlock_map SEC(\".maps\");\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, __u32);\n+\t__array(values, struct {\n+\t\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t\t__uint(max_entries, 1);\n+\t\t__type(key, __u32);\n+\t\t__type(value, __u32);\n+\t});\n+} outer_map SEC(\".maps\");\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_RINGBUF);\n+\t__uint(max_entries, 4096);\n+} ringbuf SEC(\".maps\");\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARENA);\n+\t__uint(map_flags, BPF_F_MMAPABLE);\n+\t__uint(max_entries, 2);\n+} arena SEC(\".maps\");\n+\n+static __always_inline struct park_elem *lookup_elem(void)\n+{\n+\t__u32 key = 0;\n+\n+\treturn bpf_map_lookup_elem(\u0026park_map, \u0026key);\n+}\n+\n+/*\n+ * A hash map frees a deleted element after a grace period the sleeper no\n+ * longer delays: its value is gone after a park, in a register or spilled.\n+ */\n+SEC(\"syscall\")\n+__failure __msg(\"invalid mem access 'scalar'\")\n+int hash_value_after_park(void *ctx)\n+{\n+\tstruct park_elem *elem;\n+\t__u32 key = 0;\n+\n+\telem = bpf_map_lookup_elem(\u0026hash_map, \u0026key);\n+\tif (!elem)\n+\t\treturn 0;\n+\tbpf_task_park(0, 0);\n+\treturn elem-\u003estate;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"invalid mem access 'scalar'\")\n+__naked int spilled_hash_value_after_park(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\t*(u32 *)(r10 - 4) = r1;\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -4;\t\t\t\t\t\\\n+\tr1 = %[hash_map] ll;\t\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\t\t\t\\\n+\tif r0 == 0 goto l0_%=;\t\t\t\t\\\n+\t*(u64 *)(r10 - 16) = r0;\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tcall bpf_task_park;\t\t\t\t\\\n+\tr6 = *(u64 *)(r10 - 16);\t\t\t\\\n+\tr0 = *(u32 *)(r6 + %[state_off]);\t\t\\\n+l0_%=:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_map_lookup_elem),\n+\t  __imm_addr(hash_map),\n+\t  __imm_const(state_off, offsetof(struct park_elem, state))\n+\t: __clobber_all);\n+}\n+\n+/* An array allocates its values with the map, which the program holds: they survive. */\n+SEC(\"syscall\")\n+__success __retval(0)\n+int array_value_survives_park(void *ctx)\n+{\n+\tstruct park_elem *elem = lookup_elem();\n+\n+\tif (!elem)\n+\t\treturn 1;\n+\tbpf_task_park(0, 0);\n+\treturn elem-\u003estate;\n+}\n+\n+/*\n+ * Global variables are array values too, and the compiler hoists their address\n+ * out of loops; whether it does is its choice, so the hoisted form is pinned in\n+ * assembly as well.\n+ */\n+__u32 rounds = 2;\n+\n+SEC(\"syscall\")\n+__success __retval(0)\n+int global_survives_park_in_loop(void *ctx)\n+{\n+\t__u32 i, n = 0;\n+\n+\tfor (i = 0; i \u003c 4 \u0026\u0026 i \u003c rounds; i++) {\n+\t\tbpf_task_park(0, 0);\n+\t\tn++;\n+\t}\n+\treturn n != 2;\n+}\n+\n+SEC(\"syscall\")\n+__success __retval(2)\n+__naked int global_pointer_survives_park(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr6 = %[rounds] ll;\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tcall bpf_task_park;\t\t\t\t\\\n+\tr0 = *(u32 *)(r6 + 0);\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(rounds)\n+\t: __clobber_all);\n+}\n+\n+/* A per-CPU value is gone: the task may resume on another CPU. */\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, __u32);\n+} percpu_map SEC(\".maps\");\n+\n+SEC(\"syscall\")\n+__failure __msg(\"invalid mem access 'scalar'\")\n+int percpu_value_after_park(void *ctx)\n+{\n+\t__u32 key = 0, *val;\n+\n+\tval = bpf_map_lookup_elem(\u0026percpu_map, \u0026key);\n+\tif (!val)\n+\t\treturn 0;\n+\tbpf_task_park(0, 0);\n+\treturn *val;\n+}\n+\n+/* A trusted but unreferenced pointer is gone after a park. */\n+SEC(\"syscall\")\n+__failure __msg(\"invalid mem access 'scalar'\")\n+int task_pointer_after_park(void *ctx)\n+{\n+\tstruct task_struct *task = bpf_get_current_task_btf();\n+\n+\tbpf_task_park(0, 0);\n+\treturn task-\u003epid;\n+}\n+\n+/* A referenced pointer survives a park. */\n+SEC(\"syscall\")\n+__success __retval(0)\n+int referenced_task_survives_park(void *ctx)\n+{\n+\tstruct task_struct *task;\n+\n+\ttask = bpf_task_acquire(bpf_get_current_task_btf());\n+\tif (!task)\n+\t\treturn 1;\n+\tbpf_task_park(0, 0);\n+\tbpf_task_release(task);\n+\treturn 0;\n+}\n+\n+/* The stack and the context belong to the invocation: both survive a park. */\n+SEC(\"syscall\")\n+__success __retval(7)\n+int stack_survives_park(void *ctx)\n+{\n+\tvolatile __u32 word = 7;\n+\n+\tbpf_task_park(0, 0);\n+\treturn word;\n+}\n+\n+/*\n+ * Load only: the runner refuses a context smaller than the program reads, and\n+ * the loader passes none.\n+ */\n+SEC(\"syscall\")\n+__success\n+int ctx_survives_park(__u32 *ctx)\n+{\n+\tbpf_task_park(0, 0);\n+\treturn *ctx;\n+}\n+\n+/* Arena memory is held by the arena map, which the program holds: it survives a park. */\n+SEC(\"syscall\")\n+__success __retval(0)\n+int arena_pointer_survives_park(void *ctx)\n+{\n+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)\n+\tvolatile int __arena *page;\n+\n+\tpage = bpf_arena_alloc_pages(\u0026arena, NULL, 1, NUMA_NO_NODE, 0);\n+\tif (!page)\n+\t\treturn 1;\n+\t*page = 7;\n+\tbpf_task_park(0, 0);\n+\treturn *page != 7;\n+#else\n+\t/* Nothing to test without arena casts; the program must still load. */\n+\treturn 0;\n+#endif\n+}\n+\n+/* A map the program references directly is held by the invocation: its pointer survives. */\n+SEC(\"syscall\")\n+__success __retval(0)\n+__naked int map_pointer_survives_park(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr6 = %[park_map] ll;\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\tr2 = 0;\t\t\t\t\t\t\\\n+\tcall bpf_task_park;\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\t*(u32 *)(r10 - 4) = r1;\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -4;\t\t\t\t\t\\\n+\tr1 = r6;\t\t\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_map_lookup_elem),\n+\t  __imm_addr(park_map)\n+\t: __clobber_all);\n+}\n+\n+/*\n+ * An inner map is held only by the outer map's element: neither its pointer\n+ * nor its values survive, even though the inner map is an array.\n+ */\n+SEC(\"syscall\")\n+__failure __msg(\"expected=map_ptr\")\n+int inner_map_pointer_after_park(void *ctx)\n+{\n+\t__u32 key = 0;\n+\tvoid *inner;\n+\n+\tinner = bpf_map_lookup_elem(\u0026outer_map, \u0026key);\n+\tif (!inner)\n+\t\treturn 0;\n+\tbpf_task_park(0, 0);\n+\treturn bpf_map_lookup_elem(inner, \u0026key) != NULL;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"invalid mem access 'scalar'\")\n+int inner_array_value_after_park(void *ctx)\n+{\n+\t__u32 key = 0, *val;\n+\tvoid *inner;\n+\n+\tinner = bpf_map_lookup_elem(\u0026outer_map, \u0026key);\n+\tif (!inner)\n+\t\treturn 0;\n+\tval = bpf_map_lookup_elem(inner, \u0026key);\n+\tif (!val)\n+\t\treturn 0;\n+\tbpf_task_park(0, 0);\n+\treturn *val;\n+}\n+\n+/* A dynptr on the stack is refused across a park, referenced or not. */\n+char dynptr_buf[8];\n+\n+SEC(\"syscall\")\n+__failure __msg(\"bpf_task_park() with a live dynptr at frame 0\")\n+int dynptr_across_park(void *ctx)\n+{\n+\tstruct bpf_dynptr ptr;\n+\n+\tbpf_dynptr_from_mem(dynptr_buf, sizeof(dynptr_buf), 0, \u0026ptr);\n+\tbpf_task_park(0, 0);\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"bpf_task_park() with a live dynptr at frame 0\")\n+int ringbuf_dynptr_across_park(void *ctx)\n+{\n+\tstruct bpf_dynptr ptr;\n+\n+\tbpf_ringbuf_reserve_dynptr(\u0026ringbuf, 8, 0, \u0026ptr);\n+\tbpf_task_park(0, 0);\n+\tbpf_ringbuf_discard_dynptr(\u0026ptr, 0);\n+\treturn 0;\n+}\n+\n+/* A number iterator lives entirely on the stack: a park inside the loop is fine. */\n+SEC(\"syscall\")\n+__success __retval(0)\n+int num_iterator_survives_park(void *ctx)\n+{\n+\tint i;\n+\n+\tbpf_for(i, 0, 3)\n+\t\tbpf_task_park(0, 0);\n+\treturn 0;\n+}\n+\n+/* Any other iterator is refused across a park. */\n+SEC(\"syscall\")\n+__failure __msg(\"bpf_task_park() with a live bits iterator at frame 0\")\n+int bits_iterator_across_park(void *ctx)\n+{\n+\tstruct bpf_iter_bits it;\n+\t__u64 word = 1;\n+\n+\tbpf_iter_bits_new(\u0026it, \u0026word, 1);\n+\tbpf_task_park(0, 0);\n+\tbpf_iter_bits_destroy(\u0026it);\n+\treturn 0;\n+}\n+\n+/* Regions that pin the CPU or hold a lock refuse a sleepable call, the park included. */\n+SEC(\"syscall\")\n+__failure __msg(\"function calls are not allowed while holding a lock\")\n+int park_while_locked(void *ctx)\n+{\n+\tstruct park_elem *elem = lookup_elem();\n+\n+\tif (!elem)\n+\t\treturn 0;\n+\tbpf_spin_lock(\u0026elem-\u003elock);\n+\tbpf_task_park(0, 0);\n+\tbpf_spin_unlock(\u0026elem-\u003elock);\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"function calls are not allowed while holding a lock\")\n+int park_while_res_locked(void *ctx)\n+{\n+\tstruct rlock_elem *elem;\n+\t__u32 key = 0;\n+\n+\telem = bpf_map_lookup_elem(\u0026rlock_map, \u0026key);\n+\tif (!elem)\n+\t\treturn 0;\n+\tif (bpf_res_spin_lock(\u0026elem-\u003elock))\n+\t\treturn 0;\n+\tbpf_task_park(0, 0);\n+\tbpf_res_spin_unlock(\u0026elem-\u003elock);\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"kernel func bpf_task_park is sleepable within rcu_read_lock region\")\n+int park_in_rcu_region(void *ctx)\n+{\n+\tbpf_rcu_read_lock();\n+\tbpf_task_park(0, 0);\n+\tbpf_rcu_read_unlock();\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"kernel func bpf_task_park is sleepable within non-preemptible region\")\n+int park_in_preempt_disabled_region(void *ctx)\n+{\n+\tbpf_preempt_disable();\n+\tbpf_task_park(0, 0);\n+\tbpf_preempt_enable();\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"kernel func bpf_task_park is sleepable within IRQ-disabled region\")\n+int park_in_irq_disabled_region(void *ctx)\n+{\n+\tunsigned long flags;\n+\n+\tbpf_local_irq_save(\u0026flags);\n+\tbpf_task_park(0, 0);\n+\tbpf_local_irq_restore(\u0026flags);\n+\treturn 0;\n+}\n+\n+/*\n+ * The time slice of may_goto is kept per CPU, and a program that parks may go\n+ * on on another one. That holds for a may_goto the program asks for and for one\n+ * the verifier adds to a loop it cannot bound.\n+ */\n+SEC(\"syscall\")\n+__failure __msg(\"may_goto cannot be used in a program that may migrate\")\n+int park_with_may_goto(void *ctx)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i \u003c 4 \u0026\u0026 can_loop; i++)\n+\t\tbpf_task_park(0, 0);\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"may_goto cannot be used in a program that may migrate\")\n+int park_in_unbounded_loop(void *ctx)\n+{\n+\twhile (*(volatile __u32 *)\u0026rounds)\n+\t\tbpf_task_park(0, 0);\n+\treturn 0;\n+}\n+\n+/* The park is registered for syscall programs only. */\n+SEC(\"tp/syscalls/sys_enter_nanosleep\")\n+__failure __msg(\"calling kernel function bpf_task_park is not allowed\")\n+int park_from_non_syscall(void *ctx)\n+{\n+\treturn bpf_task_park(0, 0);\n+}\n+\n+/* The wake is registered for every program type. */\n+SEC(\"xdp\")\n+__success\n+int wake_from_xdp(void *ctx)\n+{\n+\tstruct park_elem *elem = lookup_elem();\n+\tstruct task_struct *task;\n+\n+\tif (!elem)\n+\t\treturn XDP_PASS;\n+\tbpf_rcu_read_lock();\n+\ttask = elem-\u003etask;\n+\tif (task)\n+\t\tbpf_task_wake(task, 0);\n+\tbpf_rcu_read_unlock();\n+\treturn XDP_PASS;\n+}\n+\n+/* bpf_task_wake() takes a referenced, trusted or RCU-protected task that is not NULL. */\n+SEC(\"syscall\")\n+__failure __msg(\"R1 must be a rcu pointer\")\n+int wake_untrusted_task(void *ctx)\n+{\n+\tstruct park_elem *elem = lookup_elem();\n+\tstruct task_struct *task;\n+\n+\tif (!elem)\n+\t\treturn 0;\n+\ttask = elem-\u003euntrusted_task;\n+\tif (!task)\n+\t\treturn 0;\n+\treturn bpf_task_wake(task, 0);\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"Possibly NULL pointer passed to trusted R1\")\n+int wake_maybe_null_task(void *ctx)\n+{\n+\tstruct park_elem *elem = lookup_elem();\n+\tstruct task_struct *task;\n+\tint ret;\n+\n+\tif (!elem)\n+\t\treturn 0;\n+\tbpf_rcu_read_lock();\n+\ttask = elem-\u003etask;\n+\tret = bpf_task_wake(task, 0);\n+\tbpf_rcu_read_unlock();\n+\treturn ret;\n+}\n+\n+SEC(\"syscall\")\n+__success\n+int wake_rcu_task(void *ctx)\n+{\n+\tstruct park_elem *elem = lookup_elem();\n+\tstruct task_struct *task;\n+\tint ret = 0;\n+\n+\tif (!elem)\n+\t\treturn 0;\n+\tbpf_rcu_read_lock();\n+\ttask = elem-\u003etask;\n+\tif (task)\n+\t\tret = bpf_task_wake(task, 0);\n+\tbpf_rcu_read_unlock();\n+\treturn ret;\n+}\ndiff --git a/tools/testing/selftests/bpf/progs/verifier_task_park_frames.c b/tools/testing/selftests/bpf/progs/verifier_task_park_frames.c\nnew file mode 100644\nindex 00000000000000..f374e9051fe5e1\n--- /dev/null\n+++ b/tools/testing/selftests/bpf/progs/verifier_task_park_frames.c\n@@ -0,0 +1,393 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */\n+\n+#include \"bpf_experimental.h\"\n+#include \u003cerrno.h\u003e\n+#include \"bpf_misc.h\"\n+\n+/* Where a bpf_task_park() may sit in the call graph, and what its callers lose. */\n+char _license[] SEC(\"license\") = \"GPL\";\n+\n+struct park_elem {\n+\t__u32 state;\n+};\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, struct park_elem);\n+} park_map SEC(\".maps\");\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_HASH);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, struct park_elem);\n+} hash_map SEC(\".maps\");\n+\n+struct wq_elem {\n+\tstruct bpf_wq wq;\n+};\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, struct wq_elem);\n+} wq_map SEC(\".maps\");\n+\n+struct tw_elem {\n+\tstruct bpf_task_work tw;\n+};\n+\n+struct {\n+\t__uint(type, BPF_MAP_TYPE_ARRAY);\n+\t__uint(max_entries, 1);\n+\t__type(key, __u32);\n+\t__type(value, struct tw_elem);\n+} tw_map SEC(\".maps\");\n+\n+static __always_inline struct park_elem *lookup_hash_elem(void)\n+{\n+\t__u32 key = 0;\n+\n+\treturn bpf_map_lookup_elem(\u0026hash_map, \u0026key);\n+}\n+\n+/* A static function that parks is verified in its caller's state: the park sees every frame. */\n+static __noinline int park_static(void)\n+{\n+\treturn bpf_task_park(0, 0);\n+}\n+\n+/* A global function is verified alone: its callers rely on the might_park mark. */\n+__noinline int park_global(void)\n+{\n+\treturn bpf_task_park(0, 0);\n+}\n+\n+/* The mark reaches callers through further static and global functions. */\n+static __noinline int park_via_static(void)\n+{\n+\treturn park_global();\n+}\n+\n+__noinline int park_via_global(void)\n+{\n+\treturn park_global();\n+}\n+\n+/* The mark reaches a caller that sleeps on its own account as well. */\n+__noinline int park_and_sleep_global(void)\n+{\n+\tchar buf[8];\n+\n+\tpark_static();\n+\treturn bpf_copy_from_user(buf, sizeof(buf), NULL);\n+}\n+\n+/* A park in a function called directly from the main program is allowed. */\n+SEC(\"syscall\")\n+__success __retval(0)\n+int park_in_static_subprog(void *ctx)\n+{\n+\treturn park_static() != -ETIMEDOUT;\n+}\n+\n+SEC(\"syscall\")\n+__success __retval(0)\n+int park_in_global_subprog(void *ctx)\n+{\n+\treturn park_global() != -ETIMEDOUT;\n+}\n+\n+/* The caller's hash map value does not survive a park in its callee, static or global. */\n+SEC(\"syscall\")\n+__failure __msg(\"invalid mem access 'scalar'\")\n+int hash_value_after_static_subprog_park(void *ctx)\n+{\n+\tstruct park_elem *elem = lookup_hash_elem();\n+\n+\tif (!elem)\n+\t\treturn 0;\n+\tpark_static();\n+\treturn elem-\u003estate;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"invalid mem access 'scalar'\")\n+int hash_value_after_global_subprog_park(void *ctx)\n+{\n+\tstruct park_elem *elem = lookup_hash_elem();\n+\n+\tif (!elem)\n+\t\treturn 0;\n+\tpark_global();\n+\treturn elem-\u003estate;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"invalid mem access 'scalar'\")\n+int hash_value_after_nested_subprog_park(void *ctx)\n+{\n+\tstruct park_elem *elem = lookup_hash_elem();\n+\n+\tif (!elem)\n+\t\treturn 0;\n+\tpark_via_static();\n+\treturn elem-\u003estate;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"invalid mem access 'scalar'\")\n+int hash_value_after_sleeping_subprog_park(void *ctx)\n+{\n+\tstruct park_elem *elem = lookup_hash_elem();\n+\n+\tif (!elem)\n+\t\treturn 0;\n+\tpark_and_sleep_global();\n+\treturn elem-\u003estate;\n+}\n+\n+/* A dynptr held by the caller is as live across a global call that parks as across the park. */\n+char dynptr_buf[8];\n+\n+SEC(\"syscall\")\n+__failure __msg(\"bpf_task_park() with a live dynptr at frame 0\")\n+int dynptr_across_global_subprog_park(void *ctx)\n+{\n+\tstruct bpf_dynptr ptr;\n+\n+\tbpf_dynptr_from_mem(dynptr_buf, sizeof(dynptr_buf), 0, \u0026ptr);\n+\tpark_global();\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"bpf_task_park() with a live bits iterator at frame 0\")\n+int bits_iterator_across_global_subprog_park(void *ctx)\n+{\n+\tstruct bpf_iter_bits it;\n+\t__u64 word = 1;\n+\n+\tbpf_iter_bits_new(\u0026it, \u0026word, 1);\n+\tpark_global();\n+\tbpf_iter_bits_destroy(\u0026it);\n+\treturn 0;\n+}\n+\n+/*\n+ * No callback other than the body of a BPF kthread may park: not directly, not\n+ * through a static or a global function.\n+ */\n+static int park_cb(__u32 index, void *data)\n+{\n+\tbpf_task_park(0, 0);\n+\treturn 1;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"bpf_task_park() is not allowed in a callback\")\n+int park_in_loop_callback(void *ctx)\n+{\n+\tbpf_loop(1, park_cb, NULL, 0);\n+\treturn 0;\n+}\n+\n+static int park_static_cb(__u32 index, void *data)\n+{\n+\tpark_static();\n+\treturn 1;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"bpf_task_park() is not allowed in a callback\")\n+int static_subprog_park_in_callback(void *ctx)\n+{\n+\tbpf_loop(1, park_static_cb, NULL, 0);\n+\treturn 0;\n+}\n+\n+static int park_global_cb(__u32 index, void *data)\n+{\n+\tpark_global();\n+\treturn 1;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"global function park_global() may call bpf_task_park(), \"\n+\t\t\"which is not allowed in a callback\")\n+int global_subprog_park_in_callback(void *ctx)\n+{\n+\tbpf_loop(1, park_global_cb, NULL, 0);\n+\treturn 0;\n+}\n+\n+static int park_via_global_cb(__u32 index, void *data)\n+{\n+\tpark_via_global();\n+\treturn 1;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"global function park_via_global() may call bpf_task_park(), \"\n+\t\t\"which is not allowed in a callback\")\n+int nested_global_subprog_park_in_callback(void *ctx)\n+{\n+\tbpf_loop(1, park_via_global_cb, NULL, 0);\n+\treturn 0;\n+}\n+\n+static int park_for_each_cb(struct bpf_map *map, __u32 *key, struct park_elem *val, void *data)\n+{\n+\tbpf_task_park(0, 0);\n+\treturn 1;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"bpf_task_park() is not allowed in a callback\")\n+int park_in_for_each_callback(void *ctx)\n+{\n+\tbpf_for_each_map_elem(\u0026park_map, park_for_each_cb, NULL, 0);\n+\treturn 0;\n+}\n+\n+#ifdef CAN_USE_CALLX\n+\n+/* calls_arg(fn) { return fn(); } */\n+__naked __noinline __used\n+static unsigned long calls_arg(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tcallx r1;\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t::: __clobber_all);\n+}\n+\n+/*\n+ * The program loads the address of the function that parks, calls_arg() calls\n+ * it. Nothing marks calls_arg(), so it is verified on its own, without its\n+ * callers, until its exit says that it parked. The hash map value of the\n+ * program is gone after the call all the same.\n+ */\n+SEC(\"syscall\")\n+__failure __msg(\"invalid mem access 'scalar'\")\n+__naked int hash_value_after_callx_park(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 0;\t\t\t\t\t\t\\\n+\t*(u32 *)(r10 - 4) = r1;\t\t\t\t\\\n+\tr2 = r10;\t\t\t\t\t\\\n+\tr2 += -4;\t\t\t\t\t\\\n+\tr1 = %[hash_map] ll;\t\t\t\t\\\n+\tcall %[bpf_map_lookup_elem];\t\t\t\\\n+\tif r0 == 0 goto l0_%=;\t\t\t\t\\\n+\tr6 = r0;\t\t\t\t\t\\\n+\tr1 = %[park_static] ll;\t\t\t\t\\\n+\tcall calls_arg;\t\t\t\t\t\\\n+\tr0 = *(u32 *)(r6 + %[state_off]);\t\t\\\n+l0_%=:\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_map_lookup_elem),\n+\t  __imm_addr(hash_map),\n+\t  __imm_addr(park_static),\n+\t  __imm_const(state_off, offsetof(struct park_elem, state))\n+\t: __clobber_all);\n+}\n+\n+/* Nor does the callback get to park behind calls_arg(). */\n+__naked __noinline __used\n+static unsigned long park_callx_cb(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = %[park_static] ll;\t\t\t\t\\\n+\tcall calls_arg;\t\t\t\t\t\\\n+\tr0 = 1;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm_addr(park_static)\n+\t: __clobber_all);\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"bpf_task_park() is not allowed in a callback\")\n+__naked int callx_park_in_callback(void)\n+{\n+\tasm volatile (\"\t\t\t\t\t\\\n+\tr1 = 1;\t\t\t\t\t\t\\\n+\tr2 = %[park_callx_cb] ll;\t\t\t\\\n+\tr3 = 0;\t\t\t\t\t\t\\\n+\tr4 = 0;\t\t\t\t\t\t\\\n+\tcall %[bpf_loop];\t\t\t\t\\\n+\tr0 = 0;\t\t\t\t\t\t\\\n+\texit;\t\t\t\t\t\t\\\n+\"\t:\n+\t: __imm(bpf_loop),\n+\t  __imm_addr(park_callx_cb)\n+\t: __clobber_all);\n+}\n+\n+#endif /* CAN_USE_CALLX */\n+\n+/* An asynchronous callback is verified as its own state, with the callback as frame 0. */\n+static int park_wq_cb(void *map, int *key, void *value)\n+{\n+\tbpf_task_park(0, 0);\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"bpf_task_park() is not allowed in a callback\")\n+int park_in_wq_callback(void *ctx)\n+{\n+\tstruct wq_elem *elem;\n+\t__u32 key = 0;\n+\n+\telem = bpf_map_lookup_elem(\u0026wq_map, \u0026key);\n+\tif (!elem)\n+\t\treturn 0;\n+\tif (bpf_wq_init(\u0026elem-\u003ewq, \u0026wq_map, 0))\n+\t\treturn 0;\n+\tif (bpf_wq_set_callback(\u0026elem-\u003ewq, park_wq_cb, 0))\n+\t\treturn 0;\n+\treturn bpf_wq_start(\u0026elem-\u003ewq, 0);\n+}\n+\n+static int park_task_work_cb(struct bpf_map *map, void *key, void *value)\n+{\n+\tbpf_task_park(0, 0);\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__failure __msg(\"bpf_task_park() is not allowed in a callback\")\n+int park_in_task_work_callback(void *ctx)\n+{\n+\tstruct task_struct *task = bpf_get_current_task_btf();\n+\tstruct tw_elem *elem;\n+\t__u32 key = 0;\n+\n+\telem = bpf_map_lookup_elem(\u0026tw_map, \u0026key);\n+\tif (!elem)\n+\t\treturn 0;\n+\treturn bpf_task_work_schedule_signal(task, \u0026elem-\u003etw, \u0026tw_map, park_task_work_cb);\n+}\n+\n+/* The exception callback unwinds the program after bpf_throw(): no park there either. */\n+__noinline int park_exception_cb(u64 cookie)\n+{\n+\tbpf_task_park(0, 0);\n+\treturn 0;\n+}\n+\n+SEC(\"syscall\")\n+__exception_cb(park_exception_cb)\n+__arch_x86_64 __arch_arm64 __arch_s390x\n+__failure __msg(\"bpf_task_park() is not allowed in a callback\")\n+int park_in_exception_callback(void *ctx)\n+{\n+\tbpf_throw(0);\n+\treturn 0;\n+}\n"
}

2/1 2026/11/12 22:39 agent patch-evaluator
5m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "DEBUG_INFO_BTF",
    "CGROUPS"
  ],
  "FocusSymbols": [
    "bpf_task_park",
    "bpf_task_wake",
    "bpf_kthread_create",
    "bpf_kthread_start",
    "bpf_kthread_bind",
    "bpf_kthread_attach_cgroup",
    "bpf_kthread_stop",
    "bpf_kthread_should_stop",
    "bpf_prog_free_kthreads",
    "cgroup_attach_kthread"
  ],
  "Reasoning": "The patch introduces BPF kthreads and task parking mechanisms (bpf_task_park, bpf_task_wake, and bpf_kthread_* kfuncs), along with cgroup kthread attachment and significant verifier extensions for tracking migrating kfuncs, register/stack object invalidation across sleeps, and lifecycle teardown. These changes touch core BPF, scheduler, and cgroup interactions, and are fully reachable from userspace via the BPF syscall. Fuzzing is warranted to test for concurrency issues, lifetime/teardown races, and verifier soundness regressions.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.

================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.

- REACHABILITY IS THE PRIMARY GATE:
  Fuzzing can only discover bugs in code that can actually execute in standard virtualized
  environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
  If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
  regardless of whether it adds assertions or complex logic.

- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
  Patch authors routinely label changes as "cleanups", "refactorings", or state
  "No functional change intended". Do NOT take these claims at face value.
  Code refactorings that rearrange logic, introduce helper functions, or alter state management
  in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
  If reachable executable code is modified or refactored, it MUST be fuzzed.

- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
  When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
  BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
  Even if the author believes the invariant always holds, fuzzing is essential to verify whether
  an unusual sequence of operations can violate it.

================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:

- Non-kernel and non-executable changes:
  * Modifications to Documentation/, comments, or spelling fixes.
  * User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
    that do not affect the compiled kernel image (vmlinux) or kernel modules.
  * Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
    that do not alter control flow or data structures.
  * Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
  * Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
    ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
  * Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
    physical PCI hot-unplug or manual sysfs driver unbinding.
  * Code paths exclusive to architectures other than the target architecture.

================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
  * Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
    BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
  * Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
  * Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
  * Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).

================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:

- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
  Do NOT list generic, ubiquitous functions called by almost every program in the corpus
  (including, but not limited to: general memory allocators and deallocators, page fault
  and trap handlers, or core synchronization primitives; this is not an exhaustive list).
  Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
  which severely dilutes fuzzing effort away from the actual changes.

- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
  List functions that specifically implement the logic being added or altered, or direct API entrypoints
  for the subsystem feature under review.

- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
  Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
  distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
  If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
  functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).

================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 3f9f9198c5e11a731f7218111346acc0acb7780f
Author: syz-cluster <triage@syzkaller.com>
Date:   Thu Nov 12 22:39:54 2026 +0000

    syz-cluster: applied patch under review

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 520fd72e21fd07..7c15593cb18880 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1943,6 +1943,8 @@ struct bpf_prog_aux {
 	bool changes_pkt_data;
 	bool has_prologue;
 	bool might_sleep;
+	/* a run may leave on another CPU than it started on, see bpf_task_park() */
+	bool may_migrate;
 	bool can_unwind;
 	/* frames of recursive subprogs can be on the stack when it is called */
 	bool in_recursion;
@@ -2044,6 +2046,8 @@ struct bpf_prog_aux {
 	struct bpf_stream *stream[2];
 	spinlock_t st_ops_assoc_lock; /* protects st_ops_assoc */
 	struct bpf_map __rcu *st_ops_assoc;
+	struct list_head kthreads; /* BPF kthreads of the prog, see kernel/bpf/kthread.c */
+	atomic_t kthread_seq; /* index of the prog's next BPF kthread, in its name */
 };
 
 #define BPF_NR_CONTEXTS        4       /* normal, softirq, hardirq, NMI */
@@ -2968,6 +2972,7 @@ struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
 void bpf_prog_put(struct bpf_prog *prog);
 
 void bpf_prog_free_id(struct bpf_prog *prog);
+void bpf_prog_free_kthreads(struct bpf_prog_aux *aux);
 void bpf_map_free_id(struct bpf_map *map);
 
 struct btf_field *btf_record_find(const struct btf_record *rec,
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 4771c55d8f41f6..7097bb7033e8fc 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -469,6 +469,12 @@ struct bpf_func_state {
 	bool in_callback_fn;
 	bool in_async_callback_fn;
 	bool in_exception_callback_fn;
+	/*
+	 * The body of a BPF kthread: an async callback whose kernel caller,
+	 * bpf_kthread_fn(), keeps nothing across it that the sleep in
+	 * bpf_task_park() could invalidate, so the park is allowed in it.
+	 */
+	bool in_kthread_fn;
 	bool no_stack_arg_load;
 	/*
 	 * The helper invokes this callback under rcu_read_lock(), so the
@@ -559,6 +565,12 @@ struct bpf_verifier_state {
 	 * and their packet pointers are not in the state, see bpf_flow_return().
 	 */
 	bool changed_pkt_data;
+	/*
+	 * A function that is verified on its own parked the task. Its callers,
+	 * what the sleep takes from them and whether one of them is a callback,
+	 * are not in the state, see bpf_flow_return().
+	 */
+	bool parked;
 	/*
 	 * Not zero in a function that is verified on its own: the call that the
 	 * state at its entry came from, see bpf_flow_call().
@@ -1056,6 +1068,8 @@ struct bpf_subprog_info {
 	bool keep_fastcall_stack: 1;
 	bool changes_pkt_data: 1;
 	bool might_sleep: 1;
+	/* some call reachable from this subprog may be bpf_task_park() */
+	bool might_park: 1;
 	bool uses_arena_sp: 1;
 	/* a callback that takes a stack in arena, like the main program does */
 	bool arena_stack_entry: 1;
@@ -2033,6 +2047,7 @@ static inline bool bpf_is_kfunc_sleepable(struct bpf_call_arg_meta *meta)
 }
 bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta);
 bool bpf_is_cast_kfunc(const struct bpf_call_arg_meta *meta);
+bool bpf_is_task_park_kfunc(struct bpf_call_arg_meta *meta);
 struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem);
 int bpf_copy_insn_array_uniq(struct bpf_map *map, u32 start, u32 end, u32 *off);
 bool bpf_insn_is_cond_jump(u8 code);
diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 2afb4cb2bb4fa4..f649973801f4c0 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -158,6 +158,7 @@ struct cgroup *cgroup_v1v2_get_from_fd(int fd);
 bool cgroup1_ssid_disabled(int ssid);
 
 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
+int cgroup_attach_kthread(struct cgroup *dst_cgrp, struct task_struct *task);
 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
 
 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
@@ -804,6 +805,8 @@ static inline void cgroup_lock(void) {}
 static inline void cgroup_unlock(void) {}
 static inline int cgroup_attach_task_all(struct task_struct *from,
 					 struct task_struct *t) { return 0; }
+static inline int cgroup_attach_kthread(struct cgroup *dst_cgrp,
+					struct task_struct *task) { return -ENODEV; }
 static inline int cgroupstats_build(struct cgroupstats *stats,
 				    struct dentry *dentry) { return -EINVAL; }
 
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 61448be53f8da6..8a99b9aacfb057 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -883,6 +883,13 @@ static __always_inline u32 bpf_prog_run(const struct bpf_prog *prog, const void
  * This uses migrate_disable/enable() explicitly to document that the
  * invocation of a BPF program does not require reentrancy protection
  * against a BPF program which is invoked from a preempting task.
+ *
+ * A syscall program may call bpf_task_park(), or a BPF kthread kfunc that
+ * waits for another task, which lift the pin for the duration of the sleep
+ * or wait and restore it before returning to the program, so such a program
+ * may resume on another CPU. Nothing on this path keeps per-CPU state
+ * across the program: __bpf_prog_run() samples its stats only after the
+ * program returns.
  */
 static inline u32 bpf_prog_run_pin_on_cpu(const struct bpf_prog *prog,
 					  const void *ctx)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 1c93da75226e32..0fdd3917443602 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1630,6 +1630,10 @@ struct task_struct {
 	struct bpf_local_storage __rcu	*bpf_storage;
 	/* Used for BPF run context */
 	struct bpf_run_ctx		*bpf_ctx;
+	/* Set by bpf_task_wake(), consumed by bpf_task_park() */
+	int				bpf_wake_pending;
+	/* On a CPU's queue of deferred bpf_task_wake() calls when not NULL */
+	struct wake_q_node		bpf_wake_node;
 #endif
 	/* Used by BPF for per-TASK xdp storage */
 	struct bpf_net_context		*bpf_net_context;
diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile
index d26c62326e588f..a754fbefae39a9 100644
--- a/kernel/bpf/Makefile
+++ b/kernel/bpf/Makefile
@@ -15,7 +15,7 @@ obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o check_btf.o flow.o join.o
 obj-${CONFIG_BPF_LSM}	  += bpf_inode_storage.o
 obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o
 obj-$(CONFIG_BPF_JIT) += trampoline.o
-obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o
+obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o task_park.o kthread.o
 ifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy)
 obj-$(CONFIG_BPF_SYSCALL) += arena.o range_tree.o
 endif
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index 133f189bf6d270..541f0830cf79a6 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -1028,7 +1028,12 @@ static void *prog_fd_array_get_ptr(struct bpf_map *map,
 	if (IS_ERR(prog))
 		return prog;
 
-	if (prog->type == BPF_PROG_TYPE_EXT ||
+	/*
+	 * A tail call runs the program on the caller's Tasks Trace RCU read
+	 * section alone, and a program that parks leaves that section for its
+	 * sleep, see bpf_task_park().
+	 */
+	if (prog->type == BPF_PROG_TYPE_EXT || prog->aux->may_migrate ||
 	    !bpf_prog_map_compatible(map, prog)) {
 		bpf_prog_put(prog);
 		return ERR_PTR(-EINVAL);
diff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c
index bbcf42eae9d921..0102df246633f0 100644
--- a/kernel/bpf/cfg.c
+++ b/kernel/bpf/cfg.c
@@ -67,6 +67,14 @@ static void mark_subprog_might_sleep(struct bpf_verifier_env *env, int off)
 	subprog->might_sleep = true;
 }
 
+static void mark_subprog_might_park(struct bpf_verifier_env *env, int off)
+{
+	struct bpf_subprog_info *subprog;
+
+	subprog = bpf_find_containing_subprog(env, off);
+	subprog->might_park = true;
+}
+
 static void mark_subprog_might_throw(struct bpf_verifier_env *env, int off)
 {
 	struct bpf_subprog_info *subprog;
@@ -86,15 +94,28 @@ static void mark_subprog_might_throw(struct bpf_verifier_env *env, int off)
 static bool merge_callee_effects(struct bpf_verifier_env *env, int t, int w)
 {
 	struct bpf_subprog_info *caller, *callee;
+	bool might_park;
 
 	caller = bpf_find_containing_subprog(env, t);
 	callee = bpf_find_containing_subprog(env, w);
+	/*
+	 * A park in a callback does not mark the function that only loads
+	 * the callback's address: a synchronous callback is refused at the
+	 * park with the caller's frames in view, and an asynchronous one is a
+	 * root of its own. The address may also be called through callx,
+	 * which is a call like any other, so the mark follows it when the
+	 * program has callx at all.
+	 */
+	might_park = callee->might_park &&
+		     (bpf_pseudo_call(&env->prog->insnsi[t]) || env->has_callx);
 	if (caller->changes_pkt_data >= callee->changes_pkt_data &&
 	    caller->might_sleep >= callee->might_sleep &&
+	    caller->might_park >= might_park &&
 	    caller->might_throw >= callee->might_throw)
 		return false;
 	caller->changes_pkt_data |= callee->changes_pkt_data;
 	caller->might_sleep |= callee->might_sleep;
+	caller->might_park |= might_park;
 	caller->might_throw |= callee->might_throw;
 	return true;
 }
@@ -618,6 +639,8 @@ static int visit_insn(int t, struct bpf_verifier_env *env)
 			 */
 			if (ret == 0 && bpf_is_kfunc_sleepable(&meta))
 				mark_subprog_might_sleep(env, t);
+			if (ret == 0 && bpf_is_task_park_kfunc(&meta))
+				mark_subprog_might_park(env, t);
 			if (ret == 0 && bpf_is_kfunc_pkt_changing(&meta))
 				mark_subprog_changes_pkt_data(env, t);
 			if (ret == 0 && bpf_is_throw_kfunc(insn))
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index f88a1c041a7a69..95f4be8ed40537 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -138,6 +138,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag
 #ifdef CONFIG_FINEIBT
 	INIT_LIST_HEAD_RCU(&fp->aux->ksym_prefix.lnode);
 #endif
+	INIT_LIST_HEAD(&fp->aux->kthreads);
 	mutex_init(&fp->aux->used_maps_mutex);
 	mutex_init(&fp->aux->ext_mutex);
 	mutex_init(&fp->aux->dst_mutex);
diff --git a/kernel/bpf/diagnostics.c b/kernel/bpf/diagnostics.c
index 3de3e1486bbdef..33788d6f615580 100644
--- a/kernel/bpf/diagnostics.c
+++ b/kernel/bpf/diagnostics.c
@@ -2231,6 +2231,9 @@ static void diag_print_mod(struct bpf_verifier_env *env, const struct bpf_diag_h
 	case BPF_DIAG_MOD_NON_OWN_REF:
 		reason = "leaving the protected region invalidated this borrowed pointer";
 		break;
+	case BPF_DIAG_MOD_TASK_PARK:
+		reason = "the sleep in bpf_task_park() invalidated this pointer";
+		break;
 	case BPF_DIAG_MOD_CALLER_SAVED:
 		reason = target->kind == BPF_DIAG_MOD_TARGET_STACK_ARG ?
 			 "call invalidated this outgoing stack argument" :
diff --git a/kernel/bpf/diagnostics.h b/kernel/bpf/diagnostics.h
index e155d2588a51c6..656c0430f013e4 100644
--- a/kernel/bpf/diagnostics.h
+++ b/kernel/bpf/diagnostics.h
@@ -26,6 +26,7 @@ enum bpf_diag_mod_reason {
 	BPF_DIAG_MOD_SLICE_BUF,
 	BPF_DIAG_MOD_NON_OWN_REF,
 	BPF_DIAG_MOD_CALLER_SAVED,
+	BPF_DIAG_MOD_TASK_PARK,
 };
 
 enum bpf_diag_context_kind {
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 1d33a78d937390..df5784a132d754 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -2862,6 +2862,12 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
 			u16 live = env->insn_aux_data[i].live_regs_before;
 			int reg, save_off = 0;
 
+			/* The count and the time are kept per cpu, see bpf_timed_may_goto_save(). */
+			if (prog->aux->may_migrate) {
+				verbose(env, "may_goto cannot be used in a program that may migrate, also not in a loop that gets one\n");
+				return -EINVAL;
+			}
+
 			/* a register that is dead here holds the address of the count */
 			for (reg = BPF_REG_0; reg <= BPF_REG_9; reg++)
 				if (!(live & BIT(reg)))
diff --git a/kernel/bpf/flow.c b/kernel/bpf/flow.c
index c9458b3ad114a5..47ff2fb5f11a50 100644
--- a/kernel/bpf/flow.c
+++ b/kernel/bpf/flow.c
@@ -2309,7 +2309,8 @@ static void flow_shape_ctx(const struct bpf_verifier_state *st, struct flow_shap
 
 	ctx = jhash_3words(st->in_sleepable | st->callers_unknown << 1 |
 			   st->unwound << 2 | st->changed_pkt_data << 3 |
-			   (st->active_irq_id != 0) << 4 | (st->active_lock_id != 0) << 5,
+			   (st->active_irq_id != 0) << 4 | (st->active_lock_id != 0) << 5 |
+			   st->parked << 6,
 			   st->acquired_refs, st->active_locks, 0);
 	ctx = jhash_2words(st->active_preempt_locks, st->active_rcu_locks, ctx);
 	ctx = jhash_2words(lower_32_bits((unsigned long)st->active_lock_ptr),
diff --git a/kernel/bpf/join.c b/kernel/bpf/join.c
index cc4494aef18733..b63f99c663080e 100644
--- a/kernel/bpf/join.c
+++ b/kernel/bpf/join.c
@@ -452,6 +452,7 @@ static bool join_func(struct bpf_join_state *js, struct bpf_func_state *fd,
 			return false;
 		if (fa->in_callback_fn != fb->in_callback_fn ||
 		    fa->in_async_callback_fn != fb->in_async_callback_fn ||
+		    fa->in_kthread_fn != fb->in_kthread_fn ||
 		    fa->in_exception_callback_fn != fb->in_exception_callback_fn ||
 		    fa->in_rcu_callback_fn != fb->in_rcu_callback_fn ||
 		    fa->async_entry_cnt != fb->async_entry_cnt ||
@@ -532,7 +533,8 @@ static bool join_check_state(const struct bpf_verifier_state *a,
 		return false;
 	if (a->speculative != b->speculative || a->in_sleepable != b->in_sleepable ||
 	    a->callers_unknown != b->callers_unknown ||
-	    a->unwound != b->unwound || a->changed_pkt_data != b->changed_pkt_data)
+	    a->unwound != b->unwound || a->changed_pkt_data != b->changed_pkt_data ||
+	    a->parked != b->parked)
 		return false;
 	if (a->active_locks != b->active_locks ||
 	    a->active_preempt_locks != b->active_preempt_locks ||
diff --git a/kernel/bpf/kthread.c b/kernel/bpf/kthread.c
new file mode 100644
index 00000000000000..d4b8110545f09d
--- /dev/null
+++ b/kernel/bpf/kthread.c
@@ -0,0 +1,572 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
+#include <linux/cgroup.h>
+#include <linux/cpumask.h>
+#include <linux/kthread.h>
+#include <linux/rcupdate_trace.h>
+#include <linux/sched.h>
+#include <linux/sched/signal.h>
+#include <linux/sched/task.h>
+
+/*
+ * A BPF kthread is a kernel thread whose thread function is one callback of
+ * the program that created it, run once. The callback is the body of the
+ * thread: it loops, sleeps in bpf_task_park() and returns on its own, and its
+ * return ends the thread. The kernel adds nothing in between.
+ *
+ * The thread is known to the program by its task_struct. bpf_kthread_create()
+ * returns it as a referenced pointer, and the control kfuncs take it back, so
+ * the program keeps it wherever it keeps task pointers: a kptr in a map value,
+ * a graph node, or a register until
+ * bpf_task_release(). Nothing else is handed out, and nothing else needs a
+ * lifetime of its own.
+ *
+ * Every thread is on one global list, which is how a control kfunc tells a
+ * BPF kthread from any other task it is given, and how the thread's object is
+ * found without reading through a task that may have exited. Whoever takes a
+ * thread off the list under the lock owns its teardown.
+ *
+ * A thread does not hold a reference on its program. If it did, a program
+ * whose user dropped every file descriptor would live on through its own
+ * threads, with nothing left that could stop them. Instead the release of the
+ * program's last reference stops the threads it still has, before anything
+ * their callback could be using is released, and creation fails once the
+ * program has no reference left. A program's threads thus end with its last
+ * user, the way a timer or a work item ends with the last user of its map.
+ * The one exception is a thread that bpf_kthread_stop() has taken off the
+ * lists but that has not exited yet: its stopper holds the program for it.
+ *
+ * Four of the kfuncs wait for another task. Creation waits for kthreadd,
+ * bpf_kthread_stop() for the thread's exit, and bpf_kthread_bind() and
+ * bpf_kthread_attach_cgroup() for a running thread's next migrate_enable(),
+ * which is what set_cpus_allowed_ptr() waits for on any migration-disabled
+ * task. The kernel assumes that a task which has disabled migration enables
+ * it again soon, and waits for that while holding locks: cpuset attach under
+ * cgroup_mutex, cpus_read_lock() and the threadgroup rwsem, a cpuset.cpus
+ * write under cpuset_mutex, CPU offline in balance_hotplug_wait(). kthreadd's
+ * fork read-locks the global threadgroup rwsem as well, which a cgroup move
+ * holds in the default lock mode; under favordynmods the move holds the moved
+ * process's own, but a subtree_control write still takes the global one. A
+ * sleepable program is migration-disabled for its whole run and a thread body
+ * for its life outside the park, so a wait made while pinned can close a cycle
+ * through any of those: a task moved into a cpuset while it creates a thread,
+ * a thread blocked in a stop being bound by the thread it waits for, a thread
+ * on a CPU going offline taking cpus_read_lock(). Each of the four waits
+ * therefore runs with migration enabled and the Tasks Trace read section kept.
+ * Nothing the read section protects depends on the CPU: the program, its maps
+ * and its context are held as before, per-CPU memory stays valid memory, and
+ * neither runner keeps per-CPU state across the program. A program that keeps
+ * such state of its own, a stack in arena or the time slice of may_goto, is
+ * refused these kfuncs as it is refused the park. The caller may return on
+ * another CPU, the contract bpf_task_park() already carries, so a per-CPU map
+ * value pointer or a CPU id obtained before such a call names the CPU of the
+ * lookup. Unlike the park, the waits keep the program's protection, so the
+ * verifier has nothing to invalidate across them.
+ */
+
+struct bpf_kthread {
+	struct list_head node;		/* on bpf_kthreads */
+	struct list_head prog_node;	/* on aux->kthreads */
+	struct task_struct *task;	/* holds one reference for this object */
+	struct bpf_prog_aux *aux;	/* the program whose callback this is */
+	struct bpf_kthread *waits_for;	/* the thread this one is stopping, under the lock */
+	void *callback_fn;
+	u64 arg;
+};
+
+/*
+ * Both lists, and every object on them, are under this lock. It is a raw lock
+ * taken with IRQs disabled because bpf_kthread_start() is not sleepable: a
+ * timer callback calls it from softirq, and a program may call it with IRQs
+ * or preemption disabled.
+ */
+static LIST_HEAD(bpf_kthreads);
+static DEFINE_RAW_SPINLOCK(bpf_kthreads_lock);
+
+static int bpf_kthread_fn(void *data)
+{
+	struct bpf_kthread *kthread = data;
+	bpf_callback_t callback_fn = kthread->callback_fn;
+	unsigned int pflags;
+	int ret;
+
+	/* Stopped before it was started: the callback does not run. */
+	if (kthread_should_stop())
+		return -EINTR;
+
+	/*
+	 * The callback runs as the body of a sleepable program does, with
+	 * migration disabled under a Tasks Trace RCU read section, except
+	 * while it sleeps in bpf_task_park(), which drops both for the sleep
+	 * and takes them again before it returns, and while it waits in one
+	 * of the kfuncs the comment at the top names, which drop migration
+	 * disable alone. That is
+	 * safe here for the reason it is safe in a syscall program: this
+	 * function keeps nothing across the callback, and the program, with
+	 * its maps and code, is held until the thread has exited, since the
+	 * release of its last reference stops the thread first. The verifier
+	 * checks the callback as a sleepable callback in which the park is
+	 * allowed.
+	 */
+	/*
+	 * A task work queued for this thread with TWA_SIGNAL sets
+	 * TIF_NOTIFY_SIGNAL, which a kernel thread never gets to clear, since
+	 * the work runs on the return to user mode or at exit. Left alone it
+	 * would end every sleep of the body at once from then on.
+	 */
+	pflags = no_notify_signal_save();
+	rcu_read_lock_trace();
+	migrate_disable();
+	ret = callback_fn(kthread->arg, 0, 0, 0, 0);
+	migrate_enable();
+	rcu_read_unlock_trace();
+	no_notify_signal_restore(pflags);
+	return ret;
+}
+
+/* The BPF kthread whose task is @task, if any. Call with bpf_kthreads_lock held. */
+static struct bpf_kthread *bpf_kthread_find(struct task_struct *task)
+{
+	struct bpf_kthread *kthread;
+
+	lockdep_assert_held(&bpf_kthreads_lock);
+	list_for_each_entry(kthread, &bpf_kthreads, node)
+		if (kthread->task == task)
+			return kthread;
+	return NULL;
+}
+
+/* Take a thread off both lists. Call with bpf_kthreads_lock held. */
+static void bpf_kthread_unlink(struct bpf_kthread *kthread)
+{
+	lockdep_assert_held(&bpf_kthreads_lock);
+	list_del(&kthread->node);
+	list_del(&kthread->prog_node);
+}
+
+/*
+ * The object of the calling thread, if it is a BPF kthread that has not begun
+ * to exit. An exiting thread still runs its task work, which may be a program's
+ * callback, and its object is freed as soon as whoever stops it has seen it
+ * exit, which is before that. Nothing waits for such a thread any more, so it
+ * needs no record of what it waits for.
+ */
+static struct bpf_kthread *bpf_kthread_self(void)
+{
+	if (kthread_func(current) != bpf_kthread_fn || (current->flags & PF_EXITING))
+		return NULL;
+	return kthread_data(current);
+}
+
+/*
+ * Whether a stop of @kthread would wait, through the chain of threads each of
+ * them is stopping, for the calling thread. Call with bpf_kthreads_lock held.
+ * Every object on the chain is alive: a thread clears waits_for under the lock
+ * before it frees the object it points to.
+ */
+static bool bpf_kthread_waits_for_current(struct bpf_kthread *kthread)
+{
+	lockdep_assert_held(&bpf_kthreads_lock);
+	for (; kthread; kthread = kthread->waits_for)
+		if (kthread->task == current)
+			return true;
+	return false;
+}
+
+/*
+ * Stop a thread that has been taken off the lists and free its object. Waits
+ * for the thread to exit, and returns what its callback returned, or -EINTR if
+ * the callback never ran. Returns at once for a thread that has exited.
+ */
+static int bpf_kthread_stop_and_free(struct bpf_kthread *kthread)
+{
+	int ret;
+
+	ret = kthread_stop_put(kthread->task);
+	kfree(kthread);
+	return ret;
+}
+
+/*
+ * Stop every thread of @aux. Called when the program's last reference goes,
+ * before its code, maps and BTF are released, and before the Tasks Trace RCU
+ * grace period that release waits for, which a thread still inside its
+ * callback would hold up. May block.
+ */
+void bpf_prog_free_kthreads(struct bpf_prog_aux *aux)
+{
+	struct bpf_kthread *kthread, *tmp;
+	unsigned long irqflags;
+	LIST_HEAD(kthreads);
+
+	/*
+	 * Nothing joins a program's list once its last reference is gone, so
+	 * an empty list stays empty. The release path takes the lock only for
+	 * a program with threads, from a work item, never from the direct
+	 * path it may take in softirq context for a program without.
+	 */
+	if (list_empty(&aux->kthreads))
+		return;
+
+	raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+	list_for_each_entry_safe(kthread, tmp, &aux->kthreads, prog_node) {
+		list_del(&kthread->node);
+		list_move_tail(&kthread->prog_node, &kthreads);
+	}
+	raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+
+	list_for_each_entry_safe(kthread, tmp, &kthreads, prog_node)
+		bpf_kthread_stop_and_free(kthread);
+}
+
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_kthread_create - Create a kernel thread that runs a BPF callback
+ * @callback_fn: the thread's body, run once; its return ends the thread
+ * @arg: passed to @callback_fn as its only argument
+ * @flags: must be 0
+ * @aux: the calling program, set by the verifier
+ *
+ * Creates a kernel thread named bpfk/<program id>:<index> without starting it,
+ * the index counting the program's creations so that its threads are told
+ * apart in ps, top and traces; the prefix is short so that both numbers fit
+ * the comm that tracepoints and perf report as long as they have nine digits
+ * between them, where /proc shows the full name in any case. bpf_kthread_start()
+ * runs @callback_fn on it with @arg. The thread ends when the callback returns,
+ * when bpf_kthread_stop() stops it, or with the last reference on the program
+ * that created it. A thread whose callback has returned keeps its return value
+ * until bpf_kthread_stop() collects it.
+ *
+ * The creation waits for kthreadd with migration enabled, so the caller may
+ * return on another CPU.
+ *
+ * Return: the thread's task with a reference the program must release, or NULL
+ * if @flags is not 0, the thread could not be created, or the program is being
+ * freed.
+ */
+__bpf_kfunc struct task_struct *bpf_kthread_create(int (callback_fn)(u64 arg), u64 arg,
+						   u64 flags, struct bpf_prog_aux *aux)
+{
+	struct bpf_kthread *kthread;
+	unsigned long irqflags;
+	struct task_struct *task;
+	struct bpf_prog *prog;
+
+	if (flags)
+		return NULL;
+
+	kthread = kzalloc_obj(*kthread, GFP_KERNEL_ACCOUNT);
+	if (!kthread)
+		return NULL;
+	kthread->aux = aux;
+	kthread->callback_fn = callback_fn;
+	kthread->arg = arg;
+
+	/*
+	 * The creation waits for kthreadd, and kthreadd's fork takes the
+	 * threadgroup rwsem that a cgroup move of this task holds while it
+	 * waits for this task to enable migration: see the comment at the top.
+	 */
+	migrate_enable();
+	task = kthread_create(bpf_kthread_fn, kthread, "bpfk/%u:%u", aux->id,
+			      atomic_fetch_inc(&aux->kthread_seq));
+	if (IS_ERR(task)) {
+		migrate_disable();
+		kfree(kthread);
+		return NULL;
+	}
+	kthread->task = get_task_struct(task);
+	/*
+	 * The program's reference is the kind bpf_task_acquire() takes and
+	 * bpf_task_release() drops; the object's own, above, is the kind
+	 * kthread_stop_put() drops. Take it before the thread is published:
+	 * from then on any program may stop it, and a stopped thread's exit
+	 * drops the last reference the task had of its own.
+	 */
+	refcount_inc(&task->rcu_users);
+
+	/*
+	 * Run the thread up to its park. The generic kthread prologue, which
+	 * runs first, resets the affinity to the default for the thread's
+	 * node, so that an affinity set from now on sticks.
+	 */
+	kthread_park(task);
+	migrate_disable();
+
+	/*
+	 * Publish the thread while the program has a reference left to take.
+	 * Once it has none, the release of its last reference is pending or
+	 * under way, and a thread published after that release looked would
+	 * survive the program. The reference taken here is dropped again at
+	 * once: the release cannot start before it is, and the thread is on
+	 * the lists by then.
+	 */
+	raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+	prog = bpf_prog_inc_not_zero(aux->prog);
+	if (IS_ERR(prog)) {
+		raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+		migrate_enable();
+		bpf_kthread_stop_and_free(kthread);
+		migrate_disable();
+		put_task_struct_rcu_user(task);
+		return NULL;
+	}
+	list_add_tail(&kthread->node, &bpf_kthreads);
+	list_add_tail(&kthread->prog_node, &aux->kthreads);
+	raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+	bpf_prog_put(prog);
+	return task;
+}
+
+/**
+ * bpf_kthread_start - Run the callback of a BPF kthread
+ * @task: a thread created by bpf_kthread_create()
+ * @flags: must be 0
+ *
+ * Starts the thread, which then runs its callback. Does nothing for a thread
+ * that has been started already.
+ *
+ * Return: 0 on success, -EINVAL if @flags is not 0, -ESRCH if @task is not a
+ * BPF kthread or has been stopped.
+ */
+__bpf_kfunc int bpf_kthread_start(struct task_struct *task, u64 flags)
+{
+	unsigned long irqflags;
+	bool found;
+
+	if (flags)
+		return -EINVAL;
+
+	raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+	found = !!bpf_kthread_find(task);
+	raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+	if (!found)
+		return -ESRCH;
+
+	kthread_unpark(task);
+	return 0;
+}
+
+/**
+ * bpf_kthread_bind - Restrict a BPF kthread to one CPU
+ * @task: a thread created by bpf_kthread_create()
+ * @cpu: the CPU the thread is to run on
+ * @flags: must be 0
+ *
+ * Sets the thread's affinity to @cpu. A thread that has not been started wakes
+ * on @cpu; one that has moves there at its next park or return, since its
+ * callback runs with migration disabled, and the call waits for that with
+ * migration enabled, so the caller may return on another CPU and a thread may
+ * bind itself. Unlike kthread_bind(), this leaves the affinity changeable from
+ * user space and subject to cpusets, like that of any other task. The binding
+ * is lost when @cpu goes offline or when the cpuset of the thread's cgroup
+ * changes its CPUs: the thread is moved, or placed elsewhere at its next
+ * wakeup, and its affinity reset to the cpuset's CPUs, as a user task's is
+ * after an offline. It is also lost when the housekeeping CPUs change, which
+ * resets the affinity of every kernel thread that is not bound for good. A
+ * program that needs the binding re-applies it.
+ *
+ * Return: 0 on success, -EINVAL if @flags is not 0 or @cpu is not a possible
+ * CPU or is offline, -ESRCH if @task is not a BPF kthread or has been stopped.
+ */
+__bpf_kfunc int bpf_kthread_bind(struct task_struct *task, u32 cpu, u64 flags)
+{
+	unsigned long irqflags;
+	bool found;
+	int ret;
+
+	if (flags)
+		return -EINVAL;
+	if (cpu >= nr_cpu_ids || !cpu_possible(cpu))
+		return -EINVAL;
+
+	raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+	found = !!bpf_kthread_find(task);
+	raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+	if (!found)
+		return -ESRCH;
+
+	/* A running thread moves at its next park: see the comment at the top. */
+	migrate_enable();
+	ret = set_cpus_allowed_ptr(task, cpumask_of(cpu));
+	migrate_disable();
+	return ret;
+}
+
+#ifdef CONFIG_CGROUPS
+/**
+ * bpf_kthread_attach_cgroup - Move a BPF kthread into a cgroup
+ * @task: a thread created by bpf_kthread_create()
+ * @cgrp: the cgroup to move it into
+ * @flags: must be 0
+ *
+ * Moves the thread into @cgrp, started or not, so that its CPU time and the
+ * pressure it causes are accounted to @cgrp and its affinity follows @cgrp's
+ * cpuset. Until then it runs in the root cgroup, as a kernel thread does.
+ * Kernel memory the thread allocates is still not charged to @cgrp, since
+ * the allocations of a kernel thread are charged to no memory cgroup. The
+ * thread is a member of @cgrp like any process: @cgrp cannot be removed until
+ * the thread leaves or exits, and @cgrp has to be able to host processes, so
+ * on cgroup v2 it may not have controllers enabled in its children. Like any
+ * kernel thread it is not frozen with @cgrp, which then never reports frozen.
+ * A cpuset applies its CPUs to the thread as it joins, so bind the
+ * thread with bpf_kthread_bind() afterwards, not before. A running thread
+ * joins a cpuset at its next park, and the move waits for that with
+ * migration enabled, holding cgroup_mutex meanwhile, so the caller may return
+ * on another CPU and a thread may move itself. A thread whose callback has
+ * returned but that has not been stopped is left where it is and the call
+ * reports success, as cgroup.procs does for an exiting task.
+ *
+ * Return: 0 on success, -EINVAL if @flags is not 0, -ESRCH if @task is not a
+ * BPF kthread or has been stopped, -ENODEV if @cgrp is being removed, -EBUSY
+ * or -EOPNOTSUPP if @cgrp cannot host processes, or an error of the move.
+ */
+__bpf_kfunc int bpf_kthread_attach_cgroup(struct task_struct *task, struct cgroup *cgrp,
+					  u64 flags)
+{
+	unsigned long irqflags;
+	bool found;
+	int ret;
+
+	if (flags)
+		return -EINVAL;
+
+	raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+	found = !!bpf_kthread_find(task);
+	raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+	if (!found)
+		return -ESRCH;
+
+	/* A running thread joins at its next park: see the comment at the top. */
+	migrate_enable();
+	ret = cgroup_attach_kthread(cgrp, task);
+	migrate_disable();
+	return ret;
+}
+#endif
+
+/**
+ * bpf_kthread_stop - Stop a BPF kthread and wait for it to exit
+ * @task: a thread created by bpf_kthread_create()
+ * @flags: must be 0
+ *
+ * Makes bpf_kthread_should_stop() return true in the thread, ends the sleep it
+ * is in, and waits for its callback to return. A thread that has not been
+ * started exits without running its callback. The thread is gone afterwards;
+ * the reference the program holds on @task stays its own to release.
+ *
+ * The wait ends when the callback returns, which the callback decides by
+ * polling bpf_kthread_should_stop(); the kernel cannot end it sooner. It is
+ * made with migration enabled, so the caller may return on another CPU, but
+ * under the Tasks Trace read section, which every Tasks Trace grace period
+ * then waits for, so a thread should stop threads that return promptly, as
+ * one that sleeps in bpf_task_park() does. Two threads stopping each other, or
+ * a longer ring of them, would wait forever; the stop that would close such a
+ * ring is refused instead.
+ *
+ * Return: what the thread's callback returned, or -EINTR if it never ran.
+ * -EINVAL if @flags is not 0, -EDEADLK if @task is the calling thread or is,
+ * through the threads it is itself stopping, waiting for the calling thread,
+ * -ESRCH if @task is not a BPF kthread, has been stopped already, or is being
+ * stopped by the release of its program.
+ */
+__bpf_kfunc int bpf_kthread_stop(struct task_struct *task, u64 flags)
+{
+	struct bpf_kthread *kthread, *self;
+	unsigned long irqflags;
+	struct bpf_prog *prog;
+	int ret;
+
+	if (flags)
+		return -EINVAL;
+	if (task == current)
+		return -EDEADLK;
+
+	raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+	kthread = bpf_kthread_find(task);
+	if (!kthread) {
+		raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+		return -ESRCH;
+	}
+	if (bpf_kthread_waits_for_current(kthread)) {
+		raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+		return -EDEADLK;
+	}
+	/*
+	 * Hold the program while the thread is off the lists and not yet
+	 * gone. The release of the program's last reference stops only the
+	 * threads it finds on its list, and this one may be asleep in
+	 * bpf_task_park(), outside the read section that otherwise keeps the
+	 * program for it, so without the reference it could wake into a freed
+	 * program. A program with no reference left is being released, and
+	 * that release is stopping the thread.
+	 */
+	prog = bpf_prog_inc_not_zero(kthread->aux->prog);
+	if (IS_ERR(prog)) {
+		raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+		return -ESRCH;
+	}
+	bpf_kthread_unlink(kthread);
+	self = bpf_kthread_self();
+	if (self)
+		self->waits_for = kthread;
+	raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+
+	/* The wait for the exit: see the comment at the top. */
+	migrate_enable();
+	ret = kthread_stop_put(kthread->task);
+	migrate_disable();
+
+	if (self) {
+		raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+		self->waits_for = NULL;
+		raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+	}
+	kfree(kthread);
+	bpf_prog_put(prog);
+	return ret;
+}
+
+/**
+ * bpf_kthread_should_stop - Whether the calling BPF kthread was asked to stop
+ *
+ * The callback of a BPF kthread checks this in its loop and returns when it is
+ * set, as a kernel thread function does; bpf_kthread_stop() waits for that.
+ *
+ * Return: true in a kernel thread that was asked to stop, by bpf_kthread_stop()
+ * or by the release of its program, false otherwise, including for every
+ * thread of a user process.
+ */
+__bpf_kfunc bool bpf_kthread_should_stop(void)
+{
+	return (current->flags & PF_KTHREAD) && kthread_should_stop();
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_kthread_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_kthread_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE | KF_IMPLICIT_ARGS)
+BTF_ID_FLAGS(func, bpf_kthread_start, KF_RCU)
+BTF_ID_FLAGS(func, bpf_kthread_bind, KF_SLEEPABLE)
+#ifdef CONFIG_CGROUPS
+BTF_ID_FLAGS(func, bpf_kthread_attach_cgroup, KF_SLEEPABLE)
+#endif
+BTF_ID_FLAGS(func, bpf_kthread_stop, KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_kthread_should_stop)
+BTF_KFUNCS_END(bpf_kthread_kfunc_ids)
+
+static const struct btf_kfunc_id_set bpf_kthread_kfunc_set = {
+	.owner = THIS_MODULE,
+	.set = &bpf_kthread_kfunc_ids,
+};
+
+static int __init bpf_kthread_kfunc_init(void)
+{
+	return register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_kthread_kfunc_set);
+}
+late_initcall(bpf_kthread_kfunc_init);
diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c
index efc0870b85c2a3..d0db8074928a0e 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -745,7 +745,8 @@ bool bpf_state_subsumes(struct bpf_verifier_env *env, struct bpf_verifier_state
 	if (old->in_sleepable != cur->in_sleepable)
 		return false;
 
-	if (old->unwound != cur->unwound || old->changed_pkt_data != cur->changed_pkt_data)
+	if (old->unwound != cur->unwound || old->changed_pkt_data != cur->changed_pkt_data ||
+	    old->parked != cur->parked)
 		return false;
 
 	if (!refsafe(old, cur, &env->idmap_scratch))
@@ -758,6 +759,13 @@ bool bpf_state_subsumes(struct bpf_verifier_env *env, struct bpf_verifier_state
 		insn_idx = bpf_frame_insn_idx(old, i);
 		if (old->frame[i]->callsite != cur->frame[i]->callsite)
 			return false;
+		/*
+		 * The body of a BPF kthread may park where another async
+		 * callback of the same subprog may not, so a state verified
+		 * in one role must not prune the other.
+		 */
+		if (old->frame[i]->in_kthread_fn != cur->frame[i]->in_kthread_fn)
+			return false;
 		if (!func_states_equal(env, old->frame[i], cur->frame[i], insn_idx))
 			return false;
 	}
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 9004f2673ccf96..9ea8c755651efe 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2512,6 +2512,12 @@ static void bpf_prog_put_deferred(struct work_struct *work)
 
 	aux = container_of(work, struct bpf_prog_aux, work);
 	prog = aux->prog;
+	/*
+	 * The threads of the prog still run its code, each inside a Tasks
+	 * Trace RCU read section that the grace period below would wait for.
+	 * Stop them first; this waits for each of them to exit.
+	 */
+	bpf_prog_free_kthreads(aux);
 	perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
 	bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
 	bpf_prog_free_id(prog);
@@ -2523,7 +2529,8 @@ static void __bpf_prog_put(struct bpf_prog *prog)
 	struct bpf_prog_aux *aux = prog->aux;
 
 	if (atomic64_dec_and_test(&aux->refcnt)) {
-		if (in_hardirq() || irqs_disabled()) {
+		/* Stopping the prog's threads blocks, which the direct path may not. */
+		if (in_hardirq() || irqs_disabled() || !list_empty(&aux->kthreads)) {
 			INIT_WORK(&aux->work, bpf_prog_put_deferred);
 			schedule_work(&aux->work);
 		} else {
@@ -6661,8 +6668,9 @@ int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
 		if (IS_ERR(prog))
 			return PTR_ERR(prog);
 
+		/* The run is counted per CPU, which a program that migrates would unbalance. */
 		if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
-		    attr->test.ctx_size_in > U16_MAX) {
+		    attr->test.ctx_size_in > U16_MAX || prog->aux->may_migrate) {
 			bpf_prog_put(prog);
 			return -EINVAL;
 		}
diff --git a/kernel/bpf/task_park.c b/kernel/bpf/task_park.c
new file mode 100644
index 00000000000000..b6980c1cc1bf0b
--- /dev/null
+++ b/kernel/bpf/task_park.c
@@ -0,0 +1,311 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
+#include <linux/hrtimer.h>
+#include <linux/irq_work.h>
+#include <linux/kthread.h>
+#include <linux/rcupdate_trace.h>
+#include <linux/sched.h>
+#include <linux/sched/signal.h>
+#include <linux/sched/wake_q.h>
+
+/*
+ * A BPF program that blocks for an event keeps its own queue of waiters and
+ * its own notion of when the event has arrived. The kernel contributes two
+ * operations over the task: bpf_task_park() puts the current task to sleep
+ * and bpf_task_wake() makes a task runnable. The only shared state is one
+ * word in the task, bpf_wake_pending, which lets the program check its
+ * condition outside the kernel without losing a wake that lands between
+ * the check and the sleep: the waker sets the word before it wakes the
+ * task, and a park that finds it set returns instead of sleeping.
+ *
+ * The word also carries the ordering the program relies on. A waker
+ * publishes its condition and then calls bpf_task_wake(); a waiter reads
+ * the condition, finds it unmet, calls bpf_task_park(), and reads the
+ * condition again after the return. The second read must see the waker's
+ * condition whenever the park returned because of that wake, so the store
+ * of the word and the load that consumes it are both an xchg(): the store
+ * is fully ordered after the waker's condition stores, the load before the
+ * waiter's later condition loads. A release would not do for the store. Of
+ * two wakers' stores one overwrites the other, the waiter observes only the
+ * later one, and nothing would order the earlier waker's condition before
+ * the waiter's read of it. The xchg() also makes the consume atomic: a wake
+ * that lands between the waiter's observation of the word and its reset is
+ * not erased but stays pending for the next park.
+ */
+
+/*
+ * A wake asked for with IRQs disabled cannot be issued there: the caller may
+ * have interrupted the scheduler with the task's pi_lock or a runqueue lock
+ * held, or may run in NMI context, where try_to_wake_up() may take neither.
+ * Such wakes are queued on the CPU and issued from irq_work once IRQs are
+ * enabled again.
+ *
+ * The queue goes through a node of its own in the task, under the rule the
+ * scheduler's wake queues follow for theirs: a task is on at most one queue,
+ * whoever claimed the node wakes the task, and a waker that finds it claimed
+ * leaves the wake to the claimant. The pending word is set before the claim
+ * is tried, so a wake left this way still reaches a parked task through the
+ * claimant's wake, and any other task through its next park.
+ *
+ * The task's wake_q node would save the field, but whoever claims that node
+ * may turn away a futex, rwsem or semaphore wakeup that then relies on the
+ * claimant, and so has to wake with TASK_NORMAL. A program could then aim a
+ * wake of an uninterruptible sleep at any task it names, and not every such
+ * sleep has a loop around it: a new kthread sleeps once until its creator
+ * wakes it. With its own node the drain wakes interruptible sleeps only, as
+ * the direct wake does.
+ *
+ * The queue is a stack with a per-CPU head: pushed with cmpxchg() from
+ * whatever context asks, emptied with one xchg() by the irq_work, so a push
+ * that interrupts the drain lands on an empty stack and queues the work
+ * again.
+ */
+static DEFINE_PER_CPU(struct wake_q_node *, bpf_task_wake_queue);
+
+static void bpf_task_wake_deferred(struct irq_work *work)
+{
+	struct wake_q_node *node = xchg(this_cpu_ptr(&bpf_task_wake_queue), NULL);
+
+	while (node) {
+		struct task_struct *task = container_of(node, struct task_struct, bpf_wake_node);
+
+		node = node->next == WAKE_Q_TAIL ? NULL : node->next;
+		/* The task may be queued again from here on. */
+		WRITE_ONCE(task->bpf_wake_node.next, NULL);
+		wake_up_state(task, TASK_INTERRUPTIBLE);
+		put_task_struct(task);
+	}
+}
+
+static DEFINE_PER_CPU(struct irq_work, bpf_task_wake_work) =
+	IRQ_WORK_INIT_HARD(bpf_task_wake_deferred);
+
+static void bpf_task_wake_defer(struct task_struct *task)
+{
+	struct wake_q_node *node = &task->bpf_wake_node, *head;
+
+	/*
+	 * The claim, as in __wake_q_add(): NULL is free, anything else is
+	 * queued. The xchg() that stored the pending word is a full barrier
+	 * between that store and the claim, so a claim lost to another holder
+	 * is still covered: the holder's wake follows its own reset of the
+	 * node, which this claim did not observe, so the task it wakes
+	 * observes the word.
+	 */
+	if (cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL))
+		return;
+	get_task_struct(task);
+	head = this_cpu_read(bpf_task_wake_queue);
+	do {
+		WRITE_ONCE(node->next, head ?: WAKE_Q_TAIL);
+	} while (!try_cmpxchg(this_cpu_ptr(&bpf_task_wake_queue), &head, node));
+	irq_work_queue(this_cpu_ptr(&bpf_task_wake_work));
+}
+
+/*
+ * What ends a park besides a wake and the timeout: a stop or park request for
+ * a kthread, or a signal. Both are checked for every caller, since a kthread
+ * that allowed signals has them too, and the kthread checks are false for a
+ * user task. The request is reported apart from a signal because nothing
+ * clears it while the thread's function runs: every later park that finds no
+ * wake pending fails the same way, so the caller has to return.
+ */
+static int bpf_task_park_interrupted(void)
+{
+	if (kthread_should_stop_or_park())
+		return -ESHUTDOWN;
+	if (signal_pending(current))
+		return -EINTR;
+	return 0;
+}
+
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_task_park - Sleep until bpf_task_wake() is called for the current task
+ * @timeout_ns: relative timeout, or U64_MAX for none
+ * @flags: must be 0
+ *
+ * Returns at once if a wake is pending from a bpf_task_wake() that found the
+ * task running. Otherwise sleeps interruptibly until a wake, the timeout, a
+ * signal, or a stop or park request for a kthread. Every return past the
+ * argument check consumes the pending wake, and a wake takes precedence over
+ * a timeout, a signal or a stop request noticed at the same time, since the
+ * waker may already have accounted for it. Any return hands control back to
+ * the program's own check of its condition, and spurious returns are
+ * possible. A @timeout_ns of 0 polls: it reports a pending wake,
+ * then a stop or park request as -ESHUTDOWN, a pending signal as -EINTR, and
+ * otherwise -ETIMEDOUT, without sleeping.
+ *
+ * Return: 0 when a wake was consumed or the sleep ended for no recorded
+ * reason, -ETIMEDOUT when the timeout passed, -ESHUTDOWN when a stop or park
+ * request for a kthread is pending and -EINTR when a signal is, whether it
+ * was found before the sleep or ended it, even if the timeout passed
+ * meanwhile, -EINVAL when @flags is not 0. Nothing clears a stop or park
+ * request while the thread's function runs, so once a call has returned
+ * -ESHUTDOWN, every later one that finds no wake pending does too.
+ */
+__bpf_kfunc int bpf_task_park(u64 timeout_ns, u64 flags)
+{
+	ktime_t expires = min_t(u64, timeout_ns, KTIME_MAX);
+	bool expired;
+	int ret = 0;
+
+	if (flags)
+		return -EINVAL;
+	if (READ_ONCE(current->bpf_wake_pending))
+		goto out;
+
+	/*
+	 * set_current_state() is a full barrier that pairs with the one in
+	 * try_to_wake_up() after the waker's store of bpf_wake_pending:
+	 * either this task observes the pending wake below or the waker
+	 * observes it sleeping and wakes it.
+	 */
+	set_current_state(TASK_INTERRUPTIBLE);
+	if (READ_ONCE(current->bpf_wake_pending)) {
+		__set_current_state(TASK_RUNNING);
+		goto out;
+	}
+	/*
+	 * kthread_stop(), kthread_park() and signal delivery set their flag
+	 * and wake the task in the same way, so the same ordering covers
+	 * them.
+	 */
+	ret = bpf_task_park_interrupted();
+	if (ret) {
+		__set_current_state(TASK_RUNNING);
+		goto out;
+	}
+
+	/*
+	 * Sleepable programs run with migration disabled under a Tasks Trace
+	 * RCU read section. Neither may be held across a sleep of unbounded
+	 * length: the read section would stall every Tasks Trace grace period
+	 * in the system, and a migration-disabled sleeper keeps rq->nr_pinned
+	 * raised, which blocks CPU offline in balance_hotplug_wait() and pins
+	 * the wakeup to the CPU it slept on.
+	 *
+	 * Dropping both is correct only where the caller's runner holds the
+	 * program, and with it its maps and context, for the whole
+	 * invocation, so that the program's own memory needs no grace period,
+	 * and where nothing on the run path keeps per-CPU state across the
+	 * program. A program that keeps such state itself, a stack in arena or
+	 * the time slice of may_goto, is refused the call, and one that calls
+	 * it is kept out of prog arrays, where the caller of a tail call holds
+	 * it through this very read section. The verifier confines the call to
+	 * such contexts: the main body of a syscall program or of a BPF kthread
+	 * and the functions either calls directly, never any other callback,
+	 * and never a region that pins the CPU, a lock, a saved IRQ state or an
+	 * RCU read section. It also invalidates every pointer the program holds
+	 * across the call except its stack, its context, maps it references
+	 * directly and the values of its arrays, arena memory and
+	 * verifier-tracked references. What remains is that the program may
+	 * resume on another CPU, which is part of the contract.
+	 */
+	migrate_enable();
+	rcu_read_unlock_trace();
+	/*
+	 * A NULL expiry sleeps without a timer; a zero one returns at once.
+	 * schedule_hrtimeout() tells an expiry from an early return, but not
+	 * a wake from a signal or a stop or park request, so read the flags
+	 * again, as before the sleep. They win over an expiry that fired at
+	 * the same time, since the request stays pending and the timeout
+	 * does not; a wake, consumed below, wins over both.
+	 */
+	expired = !schedule_hrtimeout(timeout_ns == U64_MAX ? NULL : &expires, HRTIMER_MODE_REL);
+	ret = bpf_task_park_interrupted();
+	if (!ret && expired)
+		ret = -ETIMEDOUT;
+	rcu_read_lock_trace();
+	migrate_disable();
+out:
+	/*
+	 * Consume the wake. xchg() is fully ordered, so the condition loads
+	 * the program makes after the return see the stores the waker made
+	 * before its bpf_task_wake(). A wake that raced with the timeout, the
+	 * signal or the stop request wins: the waker may have accounted for it.
+	 */
+	if (xchg(&current->bpf_wake_pending, 0))
+		ret = 0;
+	return ret;
+}
+
+/**
+ * bpf_task_wake - Wake a task parked in bpf_task_park()
+ * @task: task to wake
+ * @flags: must be 0
+ *
+ * Marks a wake pending for @task and wakes it if it sleeps interruptibly.
+ * A task that is not parked keeps the pending wake until its next
+ * bpf_task_park(), which then returns at once.
+ *
+ * The wake takes the task's pi_lock and a runqueue lock, which the kernel
+ * code a program runs in or interrupts may already hold, and it may not be
+ * issued in NMI context at all. Such code and NMI context both run with IRQs
+ * disabled, so a call with IRQs disabled marks the wake pending and queues it
+ * for irq_work on the calling CPU, which issues it once IRQs are enabled
+ * again. Such a call cannot tell whether the task was asleep and returns 2.
+ *
+ * Return: 1 when @task was asleep and is now runnable, 0 when it was not, 2
+ * when the wake was deferred, -EINVAL when @flags is not 0.
+ */
+__bpf_kfunc int bpf_task_wake(struct task_struct *task, u64 flags)
+{
+	if (flags)
+		return -EINVAL;
+
+	/*
+	 * xchg() is a full barrier. It orders the program's stores before this
+	 * call, its condition, before the word, so a park that consumes the
+	 * word sees them, also when what it consumes is another waker's store
+	 * that overwrote this one. try_to_wake_up() then orders the store
+	 * before its load of the task state, pairing with set_current_state()
+	 * in bpf_task_park().
+	 */
+	xchg(&task->bpf_wake_pending, 1);
+	if (unlikely(irqs_disabled())) {
+		bpf_task_wake_defer(task);
+		return 2;
+	}
+	return wake_up_state(task, TASK_INTERRUPTIBLE);
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_task_park_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_task_park, KF_SLEEPABLE)
+BTF_KFUNCS_END(bpf_task_park_kfunc_ids)
+
+BTF_KFUNCS_START(bpf_task_wake_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_task_wake, KF_RCU)
+BTF_KFUNCS_END(bpf_task_wake_kfunc_ids)
+
+static const struct btf_kfunc_id_set bpf_task_park_kfunc_set = {
+	.owner = THIS_MODULE,
+	.set = &bpf_task_park_kfunc_ids,
+};
+
+static const struct btf_kfunc_id_set bpf_task_wake_kfunc_set = {
+	.owner = THIS_MODULE,
+	.set = &bpf_task_wake_kfunc_ids,
+};
+
+/*
+ * The park stays with syscall programs, whose runner holds the program across
+ * the sleep. The wake touches only the task and defers itself where it cannot
+ * be issued, so every program type has it, to wake a parked task from the
+ * kernel event that makes it worth waking.
+ */
+static int __init bpf_task_park_kfunc_init(void)
+{
+	int ret;
+
+	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_task_park_kfunc_set);
+	return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &bpf_task_wake_kfunc_set);
+}
+late_initcall(bpf_task_park_kfunc_init);
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 20b1b9ff574c7d..e7769999e8c377 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -537,6 +537,7 @@ static bool is_call_rcu_kfunc(u32 btf_id);
 static bool is_callback_calling_kfunc(u32 btf_id);
 
 static bool is_bpf_wq_set_callback_kfunc(u32 btf_id);
+static bool is_bpf_kthread_create_kfunc(u32 btf_id);
 static bool is_task_work_add_kfunc(u32 func_id);
 
 static bool is_sync_callback_calling_function(enum bpf_func_id func_id)
@@ -580,9 +581,10 @@ static bool is_async_cb_sleepable(struct bpf_verifier_env *env, struct bpf_insn
 	if (bpf_pseudo_kfunc_call(insn) && insn->off == 0 && is_call_rcu_kfunc(insn->imm))
 		return false;
 
-	/* bpf_wq and bpf_task_work callbacks are always sleepable. */
+	/* bpf_wq, bpf_task_work and bpf_kthread callbacks are always sleepable. */
 	if (bpf_pseudo_kfunc_call(insn) && insn->off == 0 &&
-	    (is_bpf_wq_set_callback_kfunc(insn->imm) || is_task_work_add_kfunc(insn->imm)))
+	    (is_bpf_wq_set_callback_kfunc(insn->imm) || is_task_work_add_kfunc(insn->imm) ||
+	     is_bpf_kthread_create_kfunc(insn->imm)))
 		return true;
 
 	verifier_bug(env, "unhandled async callback in is_async_cb_sleepable");
@@ -1792,6 +1794,7 @@ int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,
 	dst_state->callers_unknown = src->callers_unknown;
 	dst_state->unwound = src->unwound;
 	dst_state->changed_pkt_data = src->changed_pkt_data;
+	dst_state->parked = src->parked;
 	dst_state->flow_entry = src->flow_entry;
 	dst_state->insn_idx = src->insn_idx;
 	dst_state->curframe = src->curframe;
@@ -11222,6 +11225,136 @@ static void invalidate_non_owning_refs(struct bpf_verifier_env *env)
 	}));
 }
 
+/*
+ * bpf_task_park() is confined to the main body of the program, the body of a
+ * BPF kthread, and the functions either calls directly. Any other callback
+ * runs from kernel code that holds state of its own around the call, an
+ * iteration position, a work item or timer that owns the program only through
+ * Tasks Trace RCU, an exiting task, none of it audited against a sleep that
+ * drops the program's protection. Refuse the park in any such callback frame,
+ * synchronous, asynchronous or exception. Asynchronous callbacks are verified
+ * as a state of their own with the callback as frame 0, so the walk over the
+ * frames sees them too.
+ *
+ * The body of a BPF kthread is the exception because its caller,
+ * bpf_kthread_fn(), is the whole of the thread: it brackets the callback the
+ * way the runner of a syscall program brackets the program, keeps no state
+ * across it, and the program's release stops the thread and waits for it
+ * before anything the callback uses is freed. Callbacks nested under the
+ * body are refused as anywhere else.
+ */
+static bool in_callback_frames(struct bpf_verifier_env *env)
+{
+	struct bpf_verifier_state *vstate = env->cur_state;
+	int i;
+
+	for (i = 0; i <= vstate->curframe; i++) {
+		struct bpf_func_state *frame = vstate->frame[i];
+
+		if (frame->in_kthread_fn)
+			continue;
+		if (frame->in_callback_fn || frame->in_async_callback_fn ||
+		    frame->in_exception_callback_fn)
+			return true;
+	}
+	return false;
+}
+
+/*
+ * bpf_task_park() sleeps without the Tasks Trace RCU read section and the
+ * disabled migration that a sleepable program otherwise runs under. A dynptr
+ * or an iterator on the stack cannot simply be invalidated across such a
+ * sleep: the program must still release or destroy it, and an object that
+ * stays usable may point at memory the sleep lets go. Refuse the park while
+ * any is live, except a bpf_iter_num, whose state lives entirely on the stack
+ * and whose next and destroy touch nothing else.
+ */
+static int check_park_stack_objects(struct bpf_verifier_env *env, int insn_idx)
+{
+	struct bpf_verifier_state *vstate = env->cur_state;
+	int i, spi;
+
+	for (i = 0; i <= vstate->curframe; i++) {
+		struct bpf_func_state *frame = vstate->frame[i];
+
+		for (spi = 0; spi < bpf_stack_nr_slots(frame); spi++) {
+			struct bpf_stack_state *slot = bpf_stack_slot(frame, spi);
+			struct bpf_reg_state *st = &slot->spilled_ptr;
+
+			switch (slot->slot_type[BPF_REG_SIZE - 1]) {
+			case STACK_DYNPTR:
+				/* Report the slot the program named, not the second half. */
+				if (!st->dynptr.first_slot)
+					continue;
+				verbose(env, "bpf_task_park() with a live dynptr at frame %d fp%d\n",
+					i, -(spi + 1) * BPF_REG_SIZE);
+				break;
+			case STACK_ITER:
+				if (!st->id)
+					continue;
+				if (st->iter.btf == btf_vmlinux &&
+				    !strcmp(iter_type_str(st->iter.btf, st->iter.btf_id), "num"))
+					continue;
+				verbose(env, "bpf_task_park() with a live %s iterator at frame %d fp%d\n",
+					iter_type_str(st->iter.btf, st->iter.btf_id), i,
+					-(spi + 1) * BPF_REG_SIZE);
+				break;
+			default:
+				continue;
+			}
+			bpf_diag_res(env, insn_idx, "live stack object across bpf_task_park()",
+				     "A dynptr or iterator on the stack may refer to memory that the sleep in bpf_task_park() lets go, and the verifier cannot invalidate it without losing the path that releases its reference.",
+				     "Release or destroy the dynptr or iterator before calling bpf_task_park(), or move the park out of the loop that uses it.");
+			return -EINVAL;
+		}
+	}
+	return 0;
+}
+
+/*
+ * Invalidate what the sleep in bpf_task_park() may free. Keep only pointers
+ * whose lifetime does not rest on the dropped protection: the stack and
+ * instruction metadata; the context and the maps the program references
+ * directly, which the invocation holds; the values of its array maps, which
+ * the map allocates and frees with itself, and which hold the program's
+ * global variables; arena memory, which the arena map holds and whose
+ * accesses tolerate faults; and pointers backed by an explicit
+ * verifier-tracked reference, found through their own id or the id of their
+ * parent. An inner map found through a map-in-map lookup is not kept, nor
+ * are its values: the outer map's element alone holds it, and its deletion
+ * frees it after a grace period the sleeping program no longer delays.
+ * Everything else, values of maps that free elements on delete or on their
+ * owner's teardown, RCU and trusted pointers without a reference, per-CPU
+ * and local storage pointers, is gone after the call.
+ */
+static void invalidate_regs_across_park(struct bpf_verifier_env *env)
+{
+	struct bpf_stack_state *stack;
+	struct bpf_func_state *state;
+	struct bpf_reg_state *reg;
+
+	bpf_for_each_reg_in_vstate_mask(env->cur_state, state, reg, stack, 1 << STACK_SPILL, ({
+		enum bpf_reg_type type = base_type(reg->type);
+
+		if (!is_pointer_regtype(reg->type))
+			continue;
+		if ((reg->id && find_reference_state(env->cur_state, reg->id)) ||
+		    (reg->parent_id && find_reference_state(env->cur_state, reg->parent_id)))
+			continue;
+		if ((type == CONST_PTR_TO_MAP || type == PTR_TO_MAP_VALUE) && !reg->map_uid &&
+		    (type == CONST_PTR_TO_MAP || reg->map_ptr->map_type == BPF_MAP_TYPE_ARRAY))
+			continue;
+		if (type == PTR_TO_STACK || type == PTR_TO_FUNC || type == PTR_TO_INSN ||
+		    type == PTR_TO_CTX || type == PTR_TO_ARENA)
+			continue;
+		bpf_diag_record_scrub(env, reg, BPF_DIAG_MOD_TASK_PARK);
+		mark_reg_invalid(env, reg);
+	}));
+	/* the frames of the callers are looked at when the function returns to them */
+	if (env->cur_state->flow_entry)
+		env->cur_state->parked = true;
+}
+
 static void invalidate_rcu_protected_refs(struct bpf_verifier_env *env)
 {
 	struct bpf_stack_state *stack;
@@ -11640,6 +11773,9 @@ static bool func_is_on_its_own(struct bpf_verifier_env *env, struct bpf_verifier
 	if (sub->changes_pkt_data || sub->has_tail_call || sub->has_ld_abs ||
 	    sub->is_cb || sub->is_async_cb || sub->is_exception_cb)
 		return false;
+	/* a park looks at every frame, and takes from each what the sleep may free */
+	if (sub->might_park)
+		return false;
 	if (state->speculative)
 		return false;
 	if (sub->releases_inherited &&
@@ -11819,6 +11955,15 @@ int bpf_flow_return(struct bpf_verifier_env *env, struct bpf_verifier_state *cal
 		env->subprog_info[callee->subprogno].changes_pkt_data = true;
 		return 1;
 	}
+	/*
+	 * Likewise for a park behind such an address. It was checked against the frames
+	 * of the function only: which caller is a callback, and what the sleep takes
+	 * from each, is found out in the context of the caller.
+	 */
+	if (exit->parked) {
+		env->subprog_info[callee->subprogno].might_park = true;
+		return 1;
+	}
 
 	for (i = 0; i < CALLER_SAVED_REGS; i++)
 		bpf_mark_reg_not_init(env, &frame->regs[caller_saved[i]]);
@@ -12012,6 +12157,26 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 			return err;
 		}
 
+		/*
+		 * A park inside the callee is checked when the callee is verified,
+		 * but only against the callee's own frame. Apply the rules for the
+		 * frames the callee cannot see here, at the call.
+		 */
+		if (env->subprog_info[subprog].might_park) {
+			if (in_callback_frames(env)) {
+				verbose(env, "global function %s() may call bpf_task_park(), which is not allowed in a callback\n",
+					sub_name);
+				operation = bpf_diag_fmt(env, "global function %s() that may call bpf_task_park()",
+							 sub_name);
+				bpf_diag_policy(env, *insn_idx, operation, "the call is in a callback",
+						"Call the function from the main program, from the body of a BPF kthread, or from a function either calls directly, not from any other callback.");
+				return -EINVAL;
+			}
+			err = check_park_stack_objects(env, *insn_idx);
+			if (err)
+				return err;
+		}
+
 		if (env->log.level & BPF_LOG_LEVEL)
 			verbose(env, "Func#%d ('%s') is global and assumed valid.\n",
 				subprog, sub_name);
@@ -12024,6 +12189,8 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		returns_void = subprog_returns_void(env, subprog);
 		if (env->subprog_info[subprog].changes_pkt_data)
 			clear_all_pkt_pointers(env);
+		if (env->subprog_info[subprog].might_park)
+			invalidate_regs_across_park(env);
 		if (returns_void)
 			bpf_diag_record_scrub(env, &caller->regs[BPF_REG_0], BPF_DIAG_MOD_CALLER_SAVED);
 		else
@@ -12487,6 +12654,33 @@ static int set_task_work_schedule_callback_state(struct bpf_verifier_env *env,
 	return 0;
 }
 
+static int set_kthread_callback_state(struct bpf_verifier_env *env,
+				      struct bpf_func_state *caller,
+				      struct bpf_func_state *callee,
+				      int insn_idx)
+{
+	/*
+	 * bpf_kthread_create(int (callback_fn)(u64 arg), u64 arg, u64 flags);
+	 * callback_fn(u64 arg);
+	 */
+	bpf_mark_reg_unknown(&callee->regs[BPF_REG_1]);
+
+	/* unused */
+	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_2]);
+	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_3]);
+	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
+	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
+	callee->in_async_callback_fn = true;
+	callee->in_kthread_fn = true;
+	/*
+	 * The callback returns an int, which comes back in the low 32 bits of
+	 * R0: compare those, as for a global function.
+	 */
+	callee->callback_ret_range = retval_range(S32_MIN, S32_MAX);
+	callee->callback_ret_range.return_32bit = true;
+	return 0;
+}
+
 static int set_rcu_callback_state(struct bpf_verifier_env *env,
 				  struct bpf_func_state *caller,
 				  struct bpf_func_state *callee,
@@ -14170,9 +14364,14 @@ enum special_kfunc_type {
 	KF_bpf_task_work_schedule_resume,
 	KF_bpf_call_rcu,
 	KF_bpf_call_rcu_tasks_trace,
+	KF_bpf_kthread_create,
+	KF_bpf_kthread_stop,
+	KF_bpf_kthread_bind,
+	KF_bpf_kthread_attach_cgroup,
 	KF_bpf_arena_alloc_pages,
 	KF_bpf_arena_free_pages,
 	KF_bpf_session_is_return,
+	KF_bpf_task_park,
 };
 
 BTF_ID_LIST(special_kfunc_list)
@@ -14262,6 +14461,14 @@ BTF_ID(func, bpf_task_work_schedule_signal)
 BTF_ID(func, bpf_task_work_schedule_resume)
 BTF_ID(func, bpf_call_rcu)
 BTF_ID(func, bpf_call_rcu_tasks_trace)
+BTF_ID(func, bpf_kthread_create)
+BTF_ID(func, bpf_kthread_stop)
+BTF_ID(func, bpf_kthread_bind)
+#ifdef CONFIG_CGROUPS
+BTF_ID(func, bpf_kthread_attach_cgroup)
+#else
+BTF_ID_UNUSED
+#endif
 #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
 BTF_ID(func, bpf_arena_alloc_pages)
 BTF_ID(func, bpf_arena_free_pages)
@@ -14274,6 +14481,7 @@ BTF_ID(func, bpf_session_is_return)
 #else
 BTF_ID_UNUSED
 #endif
+BTF_ID(func, bpf_task_park)
 
 static bool is_bpf_cast_to_kern_ctx_kfunc(const struct bpf_call_arg_meta *meta)
 {
@@ -14297,6 +14505,21 @@ static bool is_bpf_iter_css_task_new_kfunc(const struct bpf_call_arg_meta *meta)
 	return is_kfunc_call(meta, special_kfunc_list[KF_bpf_iter_css_task_new]);
 }
 
+static bool is_bpf_task_park_kfunc(u32 btf_id)
+{
+	return btf_id == special_kfunc_list[KF_bpf_task_park];
+}
+
+/* The kfuncs that sleep or wait with migration enabled. */
+static bool is_migrating_kfunc(u32 btf_id)
+{
+	return btf_id == special_kfunc_list[KF_bpf_task_park] ||
+	       btf_id == special_kfunc_list[KF_bpf_kthread_create] ||
+	       btf_id == special_kfunc_list[KF_bpf_kthread_stop] ||
+	       btf_id == special_kfunc_list[KF_bpf_kthread_bind] ||
+	       btf_id == special_kfunc_list[KF_bpf_kthread_attach_cgroup];
+}
+
 static bool is_bpf_obj_new_kfunc(u32 func_id)
 {
 	return func_id == special_kfunc_list[KF_bpf_obj_new] ||
@@ -14414,6 +14637,11 @@ bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta)
 	       is_kfunc_dynptr_write(meta);
 }
 
+bool bpf_is_task_park_kfunc(struct bpf_call_arg_meta *meta)
+{
+	return is_kfunc_call(meta, special_kfunc_list[KF_bpf_task_park]);
+}
+
 static u32 kfunc_abi_slots(const struct btf_func_model *fm)
 {
 	const struct bpf_jit_arg_abi *abi = bpf_jit_arg_abi();
@@ -15140,6 +15368,7 @@ static bool is_sync_callback_calling_kfunc(u32 btf_id)
 static bool is_async_callback_calling_kfunc(u32 btf_id)
 {
 	return is_bpf_wq_set_callback_kfunc(btf_id) ||
+	       is_bpf_kthread_create_kfunc(btf_id) ||
 	       is_task_work_add_kfunc(btf_id) ||
 	       is_call_rcu_kfunc(btf_id);
 }
@@ -15155,6 +15384,11 @@ static bool is_bpf_wq_set_callback_kfunc(u32 btf_id)
 	return btf_id == special_kfunc_list[KF_bpf_wq_set_callback];
 }
 
+static bool is_bpf_kthread_create_kfunc(u32 btf_id)
+{
+	return btf_id == special_kfunc_list[KF_bpf_kthread_create];
+}
+
 static bool is_callback_calling_kfunc(u32 btf_id)
 {
 	return is_sync_callback_calling_kfunc(btf_id) ||
@@ -16095,6 +16329,24 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	if (!in_sleepable_context(env))
 		insn_aux->non_sleepable = true;
 
+	/*
+	 * The kfunc may return on another CPU. What a run takes on the CPU it
+	 * starts on and gives back on the CPU it ends on does not survive that.
+	 * The stack in arena is such a thing, see bpf_arena_stack_put(), and
+	 * the time slice of may_goto, which bpf_do_misc_fixups() refuses. For
+	 * the same reason the program is kept out of prog arrays, where it would
+	 * also run without a reference of its own, and away from runners that
+	 * count a run per CPU.
+	 */
+	if (is_migrating_kfunc(meta.func_id)) {
+		if (env->uses_arena_sp) {
+			verbose(env, "%s() cannot be called in a program that uses R%d\n",
+				func_name, BPF_REG_ARENA_SP);
+			return -EINVAL;
+		}
+		env->prog->aux->may_migrate = true;
+	}
+
 	/* Check the arguments */
 	err = check_func_args(env, &meta, insn_idx);
 	if (err < 0)
@@ -16159,6 +16411,16 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		}
 	}
 
+	if (is_bpf_kthread_create_kfunc(meta.func_id)) {
+		err = push_callback_call(env, insn, insn_idx, meta.subprogno,
+					 set_kthread_callback_state);
+		if (err) {
+			verbose(env, "kfunc %s#%d failed callback verification\n",
+				func_name, meta.func_id);
+			return err;
+		}
+	}
+
 	rcu_lock = is_kfunc_bpf_rcu_read_lock(&meta);
 	rcu_unlock = is_kfunc_bpf_rcu_read_unlock(&meta);
 
@@ -16266,6 +16528,19 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		}
 	}
 
+	if (is_bpf_task_park_kfunc(meta.func_id)) {
+		if (in_callback_frames(env)) {
+			verbose(env, "bpf_task_park() is not allowed in a callback\n");
+			bpf_diag_policy(env, insn_idx, "kfunc bpf_task_park", "the call is in a callback",
+					"Call bpf_task_park() from the main program, from the body of a BPF kthread, or from a function either calls directly, not from any other callback.");
+			return -EINVAL;
+		}
+		err = check_park_stack_objects(env, insn_idx);
+		if (err)
+			return err;
+		invalidate_regs_across_park(env);
+	}
+
 	bpf_diag_record_caller_saved(env, regs);
 	bpf_diag_mod_begin(env, &regs[BPF_REG_0], NULL, BPF_DIAG_MOD_WRITE);
 	for (i = 0; i < CALLER_SAVED_REGS; i++) {
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index a284225f8dc95b..41adae216adaa4 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -3124,6 +3124,51 @@ int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
 	return ret;
 }
 
+/**
+ * cgroup_attach_kthread - move a kernel thread into a cgroup
+ * @dst_cgrp: the cgroup to move it into
+ * @task: the kernel thread
+ *
+ * For kernel threads the kernel creates on behalf of a user and accounts to a
+ * cgroup of the user's choosing rather than, as cgroup_attach_task_all() does,
+ * to the creator's own. @dst_cgrp is checked as a destination of cgroup.procs
+ * is, except for the permission check, which the caller owes: it must not be
+ * dying and, on the default hierarchy, must be able to host processes, and
+ * @task must be one the cgroup core may migrate.
+ *
+ * Return: 0 on success, -ENODEV if @dst_cgrp is being removed, -EBUSY or
+ * -EOPNOTSUPP if it cannot host processes, -EINVAL if @task may not be
+ * migrated, or the error of the migration.
+ */
+int cgroup_attach_kthread(struct cgroup *dst_cgrp, struct task_struct *task)
+{
+	enum cgroup_attach_lock_mode lock_mode;
+	int ret;
+
+	cgroup_lock();
+	if (cgroup_is_dead(dst_cgrp)) {
+		ret = -ENODEV;
+		goto out_unlock;
+	}
+	/* As cgroup_procs_write_start() refuses them: cpusets cannot move these. */
+	if (task->no_cgroup_migration || (task->flags & PF_NO_SETAFFINITY)) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+	ret = cgroup_migrate_vet_dst(dst_cgrp);
+	if (ret)
+		goto out_unlock;
+	/* The moved process's own rwsem under favordynmods, as cgroup.procs takes. */
+	lock_mode = cgroup_enable_per_threadgroup_rwsem ? CGRP_ATTACH_LOCK_PER_THREADGROUP :
+							   CGRP_ATTACH_LOCK_GLOBAL;
+	cgroup_attach_lock(lock_mode, task);
+	ret = cgroup_attach_task(dst_cgrp, task, true);
+	cgroup_attach_unlock(lock_mode, task);
+out_unlock:
+	cgroup_unlock();
+	return ret;
+}
+
 struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,
 					     enum cgroup_attach_lock_mode *lock_mode)
 {
diff --git a/kernel/fork.c b/kernel/fork.c
index 8b108ed371529c..a272d11c6cafcc 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1019,6 +1019,8 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
 #ifdef CONFIG_BPF_SYSCALL
 	RCU_INIT_POINTER(tsk->bpf_storage, NULL);
 	tsk->bpf_ctx = NULL;
+	tsk->bpf_wake_pending = 0;
+	tsk->bpf_wake_node.next = NULL;
 #endif
 	return tsk;
 
diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h
index 2893bf06ff25d4..49d7a45f970716 100644
--- a/tools/testing/selftests/bpf/bpf_experimental.h
+++ b/tools/testing/selftests/bpf/bpf_experimental.h
@@ -351,6 +351,18 @@ extern void bpf_iter_css_destroy(struct bpf_iter_css *it) __weak __ksym;
 extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;
 extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;
 
+extern int bpf_task_park(__u64 timeout_ns, __u64 flags) __weak __ksym;
+extern int bpf_task_wake(struct task_struct *task, __u64 flags) __weak __ksym;
+
+extern struct task_struct *bpf_kthread_create(int (callback_fn)(__u64 arg), __u64 arg,
+					      __u64 flags) __weak __ksym;
+extern int bpf_kthread_start(struct task_struct *task, __u64 flags) __weak __ksym;
+extern int bpf_kthread_bind(struct task_struct *task, __u32 cpu, __u64 flags) __weak __ksym;
+extern int bpf_kthread_attach_cgroup(struct task_struct *task, struct cgroup *cgrp,
+				     __u64 flags) __weak __ksym;
+extern int bpf_kthread_stop(struct task_struct *task, __u64 flags) __weak __ksym;
+extern bool bpf_kthread_should_stop(void) __weak __ksym;
+
 struct bpf_iter_kmem_cache;
 extern int bpf_iter_kmem_cache_new(struct bpf_iter_kmem_cache *it) __weak __ksym;
 extern struct kmem_cache *bpf_iter_kmem_cache_next(struct bpf_iter_kmem_cache *it) __weak __ksym;
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c b/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c
new file mode 100644
index 00000000000000..fabd6b9e524269
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c
@@ -0,0 +1,641 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <test_progs.h>
+#include <sched.h>
+#include "cgroup_helpers.h"
+#include "bpf_kthread.skel.h"
+#include "verifier_bpf_kthread.skel.h"
+
+/* The context of the syscall programs, as the programs declare it. */
+struct kthread_args {
+	__u32 idx;
+	__u32 start;
+	__u32 body;
+	__u32 bind;
+	__u32 cpu;
+	__u64 cgroup_id;
+};
+
+enum body {
+	BODY_WORKER,
+	BODY_SPAWNER,
+	BODY_SELF_STOPPER,
+	BODY_RING,
+	BODY_STOP_CHECKER,
+	BODY_SELF_BINDER,
+};
+
+static int run_prog(struct bpf_program *prog, struct kthread_args *args, int *retval)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts,
+		.ctx_in = args,
+		.ctx_size_in = args ? sizeof(*args) : 0,
+	);
+	int err;
+
+	err = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts);
+	if (!err)
+		*retval = opts.retval;
+	return err;
+}
+
+static int create_thread_on(struct bpf_kthread *skel, __u32 idx, bool start, enum body body,
+			    int cpu)
+{
+	struct kthread_args args = {
+		.idx = idx, .start = start, .body = body, .bind = cpu >= 0, .cpu = cpu,
+	};
+	int err, retval;
+
+	err = run_prog(skel->progs.create_thread, &args, &retval);
+	if (!ASSERT_OK(err, "create_thread") || !ASSERT_EQ(retval, 0, "create_ret"))
+		return -1;
+	return 0;
+}
+
+static int create_thread(struct bpf_kthread *skel, __u32 idx, bool start, enum body body)
+{
+	return create_thread_on(skel, idx, start, body, -1);
+}
+
+static int bind_thread(struct bpf_kthread *skel, __u32 idx, __u32 cpu)
+{
+	struct kthread_args args = { .idx = idx, .cpu = cpu };
+	int retval;
+
+	if (!ASSERT_OK(run_prog(skel->progs.bind_thread, &args, &retval), "bind_thread"))
+		return -EFAULT;
+	return retval;
+}
+
+static int attach_cgroup(struct bpf_kthread *skel, __u32 idx, __u64 cgroup_id)
+{
+	struct kthread_args args = { .idx = idx, .cgroup_id = cgroup_id };
+	int retval;
+
+	if (!ASSERT_OK(run_prog(skel->progs.attach_cgroup, &args, &retval), "attach_cgroup"))
+		return -EFAULT;
+	return retval;
+}
+
+/* Returns what the program returned, which for stop_thread is what the stop returned. */
+static int control_thread(struct bpf_program *prog, __u32 idx)
+{
+	struct kthread_args args = { .idx = idx };
+	int retval;
+
+	if (!ASSERT_OK(run_prog(prog, &args, &retval), bpf_program__name(prog)))
+		return -EFAULT;
+	return retval;
+}
+
+#define start_thread(skel, idx) control_thread((skel)->progs.start_thread, idx)
+#define wake_thread(skel, idx) control_thread((skel)->progs.wake_thread, idx)
+#define stop_thread(skel, idx) control_thread((skel)->progs.stop_thread, idx)
+
+/* Poll until *p is nonzero, for up to two seconds. */
+static bool wait_for(__u32 *p)
+{
+	int i;
+
+	for (i = 0; i < 2000; i++) {
+		if (__atomic_load_n(p, __ATOMIC_ACQUIRE))
+			return true;
+		usleep(1000);
+	}
+	return false;
+}
+
+/* Poll until *p exceeds @seen, for up to two seconds: the thread has looped again. */
+static bool wait_past(__u32 *p, __u32 seen)
+{
+	int i;
+
+	for (i = 0; i < 2000; i++) {
+		if (__atomic_load_n(p, __ATOMIC_ACQUIRE) > seen)
+			return true;
+		usleep(1000);
+	}
+	return false;
+}
+
+/* Whether the thread sleeps: state 'S' in /proc/<tid>/stat, read past the parenthesised comm. */
+static bool thread_sleeping(__u32 tid)
+{
+	char path[64], buf[512], *p;
+	int fd, n;
+
+	snprintf(path, sizeof(path), "/proc/%u/stat", tid);
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return false;
+	n = read(fd, buf, sizeof(buf) - 1);
+	close(fd);
+	if (n <= 0)
+		return false;
+	buf[n] = 0;
+	p = strrchr(buf, ')');
+	return p && p[1] == ' ' && p[2] == 'S';
+}
+
+/* Poll until the thread sleeps, for up to two seconds. */
+static bool wait_sleeping(__u32 tid)
+{
+	int i;
+
+	for (i = 0; i < 2000; i++) {
+		if (thread_sleeping(tid))
+			return true;
+		usleep(1000);
+	}
+	return false;
+}
+
+/* The lowest and highest CPUs the test may run on; a bound thread has to run there. */
+static int allowed_cpus(int *lo, int *hi)
+{
+	cpu_set_t set;
+	int cpu;
+
+	if (sched_getaffinity(0, sizeof(set), &set))
+		return -1;
+	*lo = *hi = -1;
+	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
+		if (!CPU_ISSET(cpu, &set))
+			continue;
+		if (*lo < 0)
+			*lo = cpu;
+		*hi = cpu;
+	}
+	return *lo < 0 ? -1 : 0;
+}
+
+static bool task_exists(__u32 tid)
+{
+	char path[64];
+
+	snprintf(path, sizeof(path), "/proc/%u", tid);
+	return !access(path, F_OK);
+}
+
+/* Poll until the task has left /proc, for up to two seconds. */
+static bool wait_gone(__u32 tid)
+{
+	int i;
+
+	for (i = 0; i < 2000; i++) {
+		if (!task_exists(tid))
+			return true;
+		usleep(1000);
+	}
+	return false;
+}
+
+static int read_file(const char *path, char *buf, size_t size)
+{
+	int fd, n;
+
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return -errno;
+	n = read(fd, buf, size - 1);
+	close(fd);
+	if (n < 0)
+		return -errno;
+	buf[n] = 0;
+	return n;
+}
+
+/*
+ * The thread is named after the program that created it and its index among
+ * the program's threads. /proc shows a kernel thread's full name whatever its
+ * length; the comm the thread reads for itself is what tracepoints and perf
+ * report, and the name is short enough to fit it.
+ */
+static void check_comm(struct bpf_kthread *skel, __u32 tid, __u32 idx)
+{
+	struct bpf_prog_info info = {};
+	__u32 len = sizeof(info);
+	char path[64], buf[512], want[64];
+
+	if (!ASSERT_OK(bpf_prog_get_info_by_fd(bpf_program__fd(skel->progs.create_thread),
+					       &info, &len), "prog_info"))
+		return;
+	snprintf(want, sizeof(want), "bpfk/%u:%u", info.id, idx);
+
+	snprintf(path, sizeof(path), "/proc/%u/comm", tid);
+	if (!ASSERT_GT(read_file(path, buf, sizeof(buf)), 0, "read comm"))
+		return;
+	if (buf[strlen(buf) - 1] == '\n')
+		buf[strlen(buf) - 1] = 0;
+	ASSERT_STREQ(buf, want, "comm");
+	ASSERT_STREQ(skel->bss->comms[idx], want, "task comm");
+}
+
+/*
+ * A started thread runs its callback, which parks until woken; its return ends
+ * the thread and is what the stop reports.
+ */
+static void test_start_exit(void)
+{
+	struct bpf_kthread *skel;
+	__u32 tid;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 0, true, BODY_WORKER))
+		goto out;
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[0]), "ran"))
+		goto out;
+	tid = skel->bss->tids[0];
+	ASSERT_TRUE(task_exists(tid), "alive");
+	ASSERT_TRUE(wait_sleeping(tid), "parked");
+	check_comm(skel, tid, 0);
+
+	__atomic_store_n(&skel->bss->exit_now[0], 1, __ATOMIC_RELEASE);
+	ASSERT_GE(wake_thread(skel, 0), 0, "wake ret");
+	ASSERT_TRUE(wait_gone(tid), "exited");
+	ASSERT_EQ(stop_thread(skel, 0), 1, "exit code");
+	ASSERT_EQ(skel->bss->runs[0], 1, "runs");
+	ASSERT_EQ(stop_thread(skel, 0), -ESRCH, "slot empty");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* A stop ends a running callback through bpf_kthread_should_stop(). */
+static void test_stop_running(void)
+{
+	struct bpf_kthread *skel;
+	__u32 tid;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 1, true, BODY_WORKER))
+		goto out;
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[1]), "ran"))
+		goto out;
+	tid = skel->bss->tids[1];
+	ASSERT_EQ(stop_thread(skel, 1), 0, "stop ret");
+	ASSERT_TRUE(wait_gone(tid), "exited");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* A stop makes every park return -ESHUTDOWN, except one that has a wake to consume. */
+static void test_stop_park_rets(void)
+{
+	struct bpf_kthread *skel;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 0, true, BODY_STOP_CHECKER))
+		goto out;
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[0]), "ran"))
+		goto out;
+	ASSERT_EQ(stop_thread(skel, 0), 1, "stop ret");
+	ASSERT_EQ(skel->bss->stop_park_rets[0], -ESHUTDOWN, "park the stop ended");
+	ASSERT_EQ(skel->bss->stop_park_rets[1], -ESHUTDOWN, "poll after the stop");
+	ASSERT_EQ(skel->bss->stop_park_rets[2], 0, "park with a wake pending");
+	ASSERT_EQ(skel->bss->stop_park_rets[3], -ESHUTDOWN, "park after the wake");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* A thread stopped before it is started never runs its callback. */
+static void test_stop_parked(void)
+{
+	struct bpf_kthread *skel;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 2, false, BODY_WORKER))
+		goto out;
+	ASSERT_EQ(stop_thread(skel, 2), -EINTR, "stop ret");
+	ASSERT_EQ(skel->bss->runs[2], 0, "runs");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* A parked thread starts through an RCU-protected pointer; a second start is a no-op. */
+static void test_start_later(void)
+{
+	struct bpf_kthread *skel;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 2, false, BODY_WORKER))
+		goto out;
+	ASSERT_EQ(start_thread(skel, 2), 0, "start ret");
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[2]), "ran"))
+		goto out;
+	ASSERT_EQ(start_thread(skel, 2), 0, "second start ret");
+	ASSERT_EQ(stop_thread(skel, 2), 0, "stop ret");
+	ASSERT_EQ(skel->bss->runs[2], 1, "runs");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* A thread cannot stop itself. */
+static void test_self_stop(void)
+{
+	struct bpf_kthread *skel;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 3, true, BODY_SELF_STOPPER))
+		goto out;
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[3]), "ran"))
+		goto out;
+	ASSERT_EQ(skel->bss->self_stop_ret, -EDEADLK, "self stop ret");
+	ASSERT_EQ(stop_thread(skel, 3), 0, "stop ret");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* A thread creates and starts another thread from its callback. */
+static void test_spawn(void)
+{
+	struct bpf_kthread *skel;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 0, true, BODY_SPAWNER))
+		goto out;
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[1]), "child ran") ||
+	    !ASSERT_TRUE(wait_for(&skel->bss->runs[0]), "parent ran"))
+		goto out;
+	ASSERT_EQ(skel->bss->spawn_ret, 0, "spawn ret");
+	ASSERT_NEQ(skel->bss->tids[0], skel->bss->tids[1], "distinct threads");
+	check_comm(skel, skel->bss->tids[0], 0);
+	check_comm(skel, skel->bss->tids[1], 1);
+	ASSERT_EQ(stop_thread(skel, 1), 0, "stop child");
+	ASSERT_EQ(stop_thread(skel, 0), 0, "stop parent");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* Tasks that are no BPF kthreads, nonzero flags, and a stop before the start. */
+static void test_misuse(void)
+{
+	struct bpf_kthread *skel;
+	int retval;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (ASSERT_OK(run_prog(skel->progs.control_foreign_task, NULL, &retval), "foreign")) {
+		ASSERT_EQ(retval, 0, "foreign ret");
+		ASSERT_EQ(skel->bss->foreign_start_ret, -ESRCH, "foreign start ret");
+		ASSERT_EQ(skel->bss->foreign_stop_ret, -ESRCH, "foreign stop ret");
+		ASSERT_EQ(skel->bss->foreign_attach_ret, -ESRCH, "foreign attach ret");
+	}
+	if (ASSERT_OK(run_prog(skel->progs.misuse, NULL, &retval), "misuse"))
+		ASSERT_EQ(retval, 0, "misuse ret");
+	bpf_kthread__destroy(skel);
+}
+
+/* The program's release stops the threads it still has. */
+static void test_program_free(void)
+{
+	struct bpf_kthread *skel;
+	__u32 tid;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 0, true, BODY_WORKER))
+		goto out;
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[0]), "ran"))
+		goto out;
+	tid = skel->bss->tids[0];
+	bpf_kthread__destroy(skel);
+	ASSERT_TRUE(wait_gone(tid), "exited");
+	return;
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/*
+ * Threads stopping each other in a ring. The stop that would close the ring of
+ * waits is refused with -EDEADLK, the others return their successor's exit
+ * code as the ring unwinds, and every thread exits. The thread the refused
+ * stop was aimed at is the one left in its slot; the others were taken by the
+ * threads that stopped them.
+ */
+static void test_stop_ring(__u32 size)
+{
+	struct bpf_kthread *skel;
+	int refused = 0, left = 0, ret;
+	__u32 i, peer;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	skel->bss->ring_size = size;
+	for (i = 0; i < size; i++)
+		if (create_thread(skel, i, false, BODY_RING))
+			goto out;
+	for (i = 0; i < size; i++)
+		if (!ASSERT_EQ(start_thread(skel, i), 0, "start ret"))
+			goto out;
+	for (i = 0; i < size; i++) {
+		if (!ASSERT_TRUE(wait_for(&skel->bss->runs[i]), "ran") ||
+		    !ASSERT_TRUE(wait_gone(skel->bss->tids[i]), "exited"))
+			goto out;
+	}
+	for (i = 0; i < size; i++) {
+		peer = i + 1 < size ? i + 1 : 0;
+		ret = skel->bss->ring_stop_ret[i];
+		if (ret == -EDEADLK)
+			refused++;
+		else
+			ASSERT_EQ(ret, peer + 1, "ring stop ret");
+	}
+	ASSERT_EQ(refused, 1, "one stop refused");
+	for (i = 0; i < size; i++) {
+		ret = stop_thread(skel, i);
+		if (ret == -ESRCH)
+			continue;
+		ASSERT_EQ(ret, i + 1, "exit code");
+		left++;
+	}
+	ASSERT_EQ(left, 1, "one thread left in its slot");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/*
+ * A thread bound before its start runs on that CPU, and the last bind wins. A
+ * thread bound while it runs moves at its next sleep, which for a body that
+ * parks is at once, since the park sleeps with migration enabled; its next
+ * iteration runs on the new CPU.
+ */
+static void test_bind(void)
+{
+	struct bpf_kthread *skel;
+	int lo, hi;
+	__u32 loops;
+
+	if (!ASSERT_OK(allowed_cpus(&lo, &hi), "allowed_cpus"))
+		return;
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread_on(skel, 0, false, BODY_WORKER, lo))
+		goto out;
+	ASSERT_EQ(bind_thread(skel, 0, -1), -EINVAL, "bind impossible cpu");
+	ASSERT_EQ(bind_thread(skel, 0, hi), 0, "bind ret");
+	ASSERT_EQ(start_thread(skel, 0), 0, "start ret");
+	if (!ASSERT_TRUE(wait_past(&skel->bss->loops[0], 0), "ran"))
+		goto out;
+	ASSERT_EQ(skel->bss->cpus[0], hi, "cpu");
+	ASSERT_EQ(bind_thread(skel, 0, lo), 0, "bind running ret");
+	loops = skel->bss->loops[0];
+	ASSERT_GE(wake_thread(skel, 0), 0, "wake ret");
+	if (ASSERT_TRUE(wait_past(&skel->bss->loops[0], loops), "looped"))
+		ASSERT_EQ(skel->bss->cpus[0], lo, "cpu after bind");
+	ASSERT_EQ(stop_thread(skel, 0), 0, "stop ret");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/*
+ * A thread binds itself: it is migratable during the call, so the bind takes
+ * effect before it returns, and the thread's first iteration runs on the CPU.
+ */
+static void test_self_bind(void)
+{
+	struct bpf_kthread *skel;
+	int lo, hi;
+
+	if (!ASSERT_OK(allowed_cpus(&lo, &hi), "allowed_cpus"))
+		return;
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	skel->bss->self_bind_cpu = lo;
+	if (create_thread(skel, 0, true, BODY_SELF_BINDER))
+		goto out;
+	if (!ASSERT_TRUE(wait_past(&skel->bss->loops[0], 0), "ran"))
+		goto out;
+	ASSERT_EQ(skel->bss->self_bind_ret, 0, "self bind ret");
+	ASSERT_EQ(skel->bss->cpus[0], lo, "cpu");
+	ASSERT_EQ(stop_thread(skel, 0), 0, "stop ret");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* Whether cgroup.events of the cgroup reports it populated, polling until it says @populated. */
+static bool wait_populated(const char *path, bool populated)
+{
+	const char *want = populated ? "populated 1" : "populated 0";
+	char buf[256];
+	int i;
+
+	for (i = 0; i < 2000; i++) {
+		if (!read_cgroup_file(path, "cgroup.events", buf, sizeof(buf)) && strstr(buf, want))
+			return true;
+		usleep(1000);
+	}
+	return false;
+}
+
+/*
+ * A thread moved into a cgroup, parked or running, runs there from its next
+ * iteration on and populates the cgroup until it exits. The test's work
+ * directory has controllers enabled for its children, so it cannot take the
+ * thread itself.
+ */
+static void test_attach_cgroup(void)
+{
+	struct bpf_kthread *skel = NULL;
+	int fd1 = -1, fd2 = -1;
+	char subtree[256] = {};
+	__u64 cg1, cg2;
+	__u32 loops;
+
+	if (!ASSERT_OK(setup_cgroup_environment(), "setup_cgroup_environment"))
+		return;
+	fd1 = create_and_get_cgroup("/kthread1");
+	fd2 = create_and_get_cgroup("/kthread2");
+	if (!ASSERT_GE(fd1, 0, "create kthread1") || !ASSERT_GE(fd2, 0, "create kthread2"))
+		goto out;
+	cg1 = get_cgroup_id("/kthread1");
+	cg2 = get_cgroup_id("/kthread2");
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		goto out;
+	if (create_thread(skel, 0, false, BODY_WORKER))
+		goto out;
+	/* Only a domain controller in the children makes the parent refuse processes. */
+	if (!read_cgroup_file("", "cgroup.subtree_control", subtree, sizeof(subtree)) &&
+	    (strstr(subtree, "memory") || strstr(subtree, "io")))
+		ASSERT_EQ(attach_cgroup(skel, 0, get_cgroup_id("")), -EBUSY,
+			  "attach to inner cgroup");
+	ASSERT_EQ(attach_cgroup(skel, 0, cg1), 0, "attach parked ret");
+	ASSERT_EQ(start_thread(skel, 0), 0, "start ret");
+	if (!ASSERT_TRUE(wait_past(&skel->bss->loops[0], 0), "ran"))
+		goto out;
+	ASSERT_EQ(skel->bss->cgids[0], cg1, "cgroup after start");
+	ASSERT_TRUE(wait_populated("/kthread1", true), "kthread1 populated");
+
+	ASSERT_EQ(attach_cgroup(skel, 0, cg2), 0, "attach running ret");
+	loops = skel->bss->loops[0];
+	ASSERT_GE(wake_thread(skel, 0), 0, "wake ret");
+	if (ASSERT_TRUE(wait_past(&skel->bss->loops[0], loops), "looped"))
+		ASSERT_EQ(skel->bss->cgids[0], cg2, "cgroup after move");
+	ASSERT_TRUE(wait_populated("/kthread1", false), "kthread1 empty");
+	ASSERT_TRUE(wait_populated("/kthread2", true), "kthread2 populated");
+
+	ASSERT_EQ(stop_thread(skel, 0), 0, "stop ret");
+	ASSERT_TRUE(wait_populated("/kthread2", false), "kthread2 empty");
+out:
+	bpf_kthread__destroy(skel);
+	if (fd1 >= 0)
+		close(fd1);
+	if (fd2 >= 0)
+		close(fd2);
+	cleanup_cgroup_environment();
+}
+
+void test_bpf_kthread(void)
+{
+	RUN_TESTS(verifier_bpf_kthread);
+
+	if (test__start_subtest("start_exit"))
+		test_start_exit();
+	if (test__start_subtest("stop_running"))
+		test_stop_running();
+	if (test__start_subtest("stop_park_rets"))
+		test_stop_park_rets();
+	if (test__start_subtest("stop_parked"))
+		test_stop_parked();
+	if (test__start_subtest("start_later"))
+		test_start_later();
+	if (test__start_subtest("self_stop"))
+		test_self_stop();
+	if (test__start_subtest("spawn"))
+		test_spawn();
+	if (test__start_subtest("misuse"))
+		test_misuse();
+	if (test__start_subtest("program_free"))
+		test_program_free();
+	if (test__start_subtest("stop_ring2"))
+		test_stop_ring(2);
+	if (test__start_subtest("stop_ring3"))
+		test_stop_ring(3);
+	if (test__start_subtest("bind"))
+		test_bind();
+	if (test__start_subtest("self_bind"))
+		test_self_bind();
+	if (test__start_subtest("attach_cgroup"))
+		test_attach_cgroup();
+}
diff --git a/tools/testing/selftests/bpf/prog_tests/task_park.c b/tools/testing/selftests/bpf/prog_tests/task_park.c
new file mode 100644
index 00000000000000..a479a183c69683
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/task_park.c
@@ -0,0 +1,311 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <test_progs.h>
+#include <pthread.h>
+#include <signal.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include "task_park.skel.h"
+#include "verifier_task_park.skel.h"
+#include "verifier_task_park_frames.skel.h"
+
+static int run_syscall_prog(struct bpf_program *prog, __u32 *retval)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+	int err;
+
+	err = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts);
+	if (!err)
+		*retval = opts.retval;
+	return err;
+}
+
+/* A thread blocked in a syscall program that parks. */
+struct waiter {
+	struct bpf_program *prog;
+	pthread_t thread;
+	pid_t tid;
+	__u32 retval;
+	int err;
+	int done;
+};
+
+static void *waiter_fn(void *arg)
+{
+	struct waiter *w = arg;
+
+	__atomic_store_n(&w->tid, syscall(SYS_gettid), __ATOMIC_RELEASE);
+	w->err = run_syscall_prog(w->prog, &w->retval);
+	__atomic_store_n(&w->done, 1, __ATOMIC_RELEASE);
+	return NULL;
+}
+
+static bool waiter_done(struct waiter *w)
+{
+	return __atomic_load_n(&w->done, __ATOMIC_ACQUIRE);
+}
+
+static int start_waiter(struct waiter *w, struct bpf_program *prog)
+{
+	memset(w, 0, sizeof(*w));
+	w->prog = prog;
+	return pthread_create(&w->thread, NULL, waiter_fn, w);
+}
+
+/*
+ * Whether the waiter thread sleeps in the kernel, which for this thread can
+ * only be the park: state 'S' in /proc/self/task/<tid>/stat, read past the
+ * parenthesised comm.
+ */
+static bool waiter_sleeping(struct waiter *w)
+{
+	pid_t tid = __atomic_load_n(&w->tid, __ATOMIC_ACQUIRE);
+	char path[64], buf[512], *p;
+	int fd, n;
+
+	if (!tid)
+		return false;
+	snprintf(path, sizeof(path), "/proc/self/task/%d/stat", tid);
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return false;
+	n = read(fd, buf, sizeof(buf) - 1);
+	close(fd);
+	if (n <= 0)
+		return false;
+	buf[n] = 0;
+	p = strrchr(buf, ')');
+	return p && p[1] == ' ' && p[2] == 'S';
+}
+
+/*
+ * Wake the waiter until it has returned. A wake before the waiter published
+ * itself finds no task and sets nothing; one that finds the task sets the
+ * condition, so the waiter returns 0 only after a wake run that found it,
+ * and such a run returns 0 or 1. Returns the last result that found a task,
+ * or -ESRCH if none did.
+ */
+static int wake_until_done(struct task_park *skel, struct waiter *w)
+{
+	int i, last = -ESRCH;
+	__u32 retval;
+
+	for (i = 0; i < 5000 && !waiter_done(w); i++) {
+		if (run_syscall_prog(skel->progs.wake_waiter, &retval))
+			break;
+		if ((__s32)retval >= 0)
+			last = retval;
+		usleep(1000);
+	}
+	return last;
+}
+
+static void test_timeout(struct task_park *skel)
+{
+	__u32 retval;
+
+	if (ASSERT_OK(run_syscall_prog(skel->progs.park_timeout, &retval), "park_timeout"))
+		ASSERT_EQ((__s32)retval, -ETIMEDOUT, "timeout_ret");
+}
+
+static void test_flags(struct task_park *skel)
+{
+	__u32 retval;
+
+	if (ASSERT_OK(run_syscall_prog(skel->progs.bad_flags, &retval), "bad_flags"))
+		ASSERT_EQ(retval, 0, "flags_ret");
+}
+
+static void test_pending(struct task_park *skel)
+{
+	__u32 retval;
+
+	if (ASSERT_OK(run_syscall_prog(skel->progs.park_pending, &retval), "park_pending"))
+		ASSERT_EQ((__s32)retval, -ETIMEDOUT, "pending_ret");
+}
+
+static void test_wake(struct task_park *skel)
+{
+	struct waiter w;
+	int last;
+
+	if (!ASSERT_OK(start_waiter(&w, skel->progs.park_until_woken), "pthread_create"))
+		return;
+	last = wake_until_done(skel, &w);
+	pthread_join(w.thread, NULL);
+	ASSERT_GE(last, 0, "wake_found_waiter");
+	ASSERT_OK(w.err, "park_until_woken");
+	ASSERT_EQ((__s32)w.retval, 0, "wake_park_ret");
+}
+
+/*
+ * Wake a waiter that is known to be asleep, without a timer: once the thread
+ * shows as sleeping, the one wake that follows must report that it found the
+ * task asleep.
+ */
+static void test_wake_asleep(struct task_park *skel)
+{
+	__u64 timeout = skel->data->park_timeout_ns;
+	struct waiter w;
+	__u32 retval;
+	int i;
+
+	skel->data->park_timeout_ns = ~0ULL;
+	if (!ASSERT_OK(start_waiter(&w, skel->progs.park_until_woken), "pthread_create"))
+		goto out;
+	for (i = 0; i < 5000 && !waiter_sleeping(&w); i++)
+		usleep(1000);
+	if (ASSERT_TRUE(waiter_sleeping(&w), "waiter_sleeping")) {
+		if (ASSERT_OK(run_syscall_prog(skel->progs.wake_waiter, &retval), "wake_waiter"))
+			ASSERT_EQ((__s32)retval, 1, "wake_found_asleep");
+	}
+	/* Bound any further park before draining, so a lost wake cannot hang the join. */
+	skel->data->park_timeout_ns = timeout;
+	wake_until_done(skel, &w);
+	pthread_join(w.thread, NULL);
+	ASSERT_OK(w.err, "park_until_woken");
+	ASSERT_EQ((__s32)w.retval, 0, "wake_park_ret");
+out:
+	skel->data->park_timeout_ns = timeout;
+}
+
+/*
+ * Wait until the waiter sleeps, attach the tracing program @prog, let @fire
+ * raise its event for this thread, and check that the program's wake, not the
+ * syscall drain, ended the park and returned @expected_ret. The program
+ * attaches only once the waiter sleeps, since the polling sleeps of this
+ * thread are events of its own, and the drain runs only if the event failed.
+ */
+static void test_wake_tracing(struct task_park *skel, struct bpf_program *prog,
+			      int (*fire)(void), int expected_ret)
+{
+	__u64 timeout = skel->data->park_timeout_ns;
+	struct bpf_link *link = NULL;
+	struct waiter w;
+	bool woken;
+	int i;
+
+	skel->data->park_timeout_ns = ~0ULL;
+	skel->bss->waker_tid = syscall(SYS_gettid);
+	skel->data->tracing_wake_ret = -1;
+	if (!ASSERT_OK(start_waiter(&w, skel->progs.park_until_woken), "pthread_create"))
+		goto out;
+	for (i = 0; i < 5000 && !waiter_sleeping(&w); i++)
+		usleep(1000);
+	if (ASSERT_TRUE(waiter_sleeping(&w), "waiter_sleeping")) {
+		link = bpf_program__attach(prog);
+		if (ASSERT_OK_PTR(link, "attach"))
+			ASSERT_OK(fire(), "fire");
+	}
+	for (i = 0; i < 5000 && !waiter_done(&w); i++)
+		usleep(1000);
+	woken = waiter_done(&w);
+	bpf_link__destroy(link);
+	/* Bound any further park before draining, so a lost wake cannot hang the join. */
+	skel->data->park_timeout_ns = timeout;
+	if (!woken)
+		wake_until_done(skel, &w);
+	pthread_join(w.thread, NULL);
+	ASSERT_TRUE(woken, "woken_by_tracing_prog");
+	ASSERT_EQ(skel->data->tracing_wake_ret, expected_ret, "tracing_wake_ret");
+	ASSERT_OK(w.err, "park_until_woken");
+	ASSERT_EQ((__s32)w.retval, 0, "wake_park_ret");
+out:
+	skel->data->park_timeout_ns = timeout;
+}
+
+/* Renaming the thread to its own name still fires task_rename. */
+static int rename_self(void)
+{
+	char comm[16];
+
+	if (prctl(PR_GET_NAME, comm))
+		return -errno;
+	return prctl(PR_SET_NAME, comm) ? -errno : 0;
+}
+
+/* The timer that ends this sleep wakes the thread, which fires sched_waking. */
+static int sleep_self(void)
+{
+	return usleep(1000) ? -errno : 0;
+}
+
+static void sig_noop(int sig)
+{
+}
+
+/*
+ * A program that parks cannot be the target of a tail call: a prog array takes
+ * a syscall program that wakes and refuses one that parks.
+ */
+static void test_prog_array(struct task_park *skel)
+{
+	int map_fd = bpf_map__fd(skel->maps.jmp_table);
+	__u32 key = 0;
+	int fd;
+
+	fd = bpf_program__fd(skel->progs.wake_waiter);
+	ASSERT_OK(bpf_map_update_elem(map_fd, &key, &fd, BPF_ANY), "member_that_wakes");
+	fd = bpf_program__fd(skel->progs.park_timeout);
+	ASSERT_EQ(bpf_map_update_elem(map_fd, &key, &fd, BPF_ANY), -EINVAL, "member_that_parks");
+}
+
+/* A signal ends the sleep, and the park reports it as -EINTR. */
+static void test_signal(struct task_park *skel)
+{
+	struct sigaction sa = { .sa_handler = sig_noop }, old_sa;
+	struct waiter w;
+	bool interrupted;
+	int i;
+
+	if (!ASSERT_OK(sigaction(SIGUSR2, &sa, &old_sa), "sigaction"))
+		return;
+	if (!ASSERT_OK(start_waiter(&w, skel->progs.park_until_woken), "pthread_create"))
+		goto out;
+	/* A signal handled before the thread enters its syscall ends nothing: resend. */
+	for (i = 0; i < 5000 && !waiter_done(&w); i++) {
+		pthread_kill(w.thread, SIGUSR2);
+		usleep(1000);
+	}
+	interrupted = waiter_done(&w);
+	if (!interrupted)
+		wake_until_done(skel, &w);
+	pthread_join(w.thread, NULL);
+	ASSERT_TRUE(interrupted, "interrupted");
+	ASSERT_OK(w.err, "park_until_woken");
+	ASSERT_EQ((__s32)w.retval, -EINTR, "signal_park_ret");
+out:
+	sigaction(SIGUSR2, &old_sa, NULL);
+}
+
+void test_task_park(void)
+{
+	struct task_park *skel;
+
+	RUN_TESTS(verifier_task_park);
+	RUN_TESTS(verifier_task_park_frames);
+
+	skel = task_park__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (test__start_subtest("timeout"))
+		test_timeout(skel);
+	if (test__start_subtest("flags"))
+		test_flags(skel);
+	if (test__start_subtest("pending"))
+		test_pending(skel);
+	if (test__start_subtest("wake"))
+		test_wake(skel);
+	if (test__start_subtest("wake_asleep"))
+		test_wake_asleep(skel);
+	if (test__start_subtest("wake_tracing"))
+		test_wake_tracing(skel, skel->progs.wake_on_rename, rename_self, 1);
+	if (test__start_subtest("wake_tracing_deferred"))
+		test_wake_tracing(skel, skel->progs.wake_on_waking, sleep_self, 2);
+	if (test__start_subtest("prog_array"))
+		test_prog_array(skel);
+	if (test__start_subtest("signal"))
+		test_signal(skel);
+	task_park__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/bpf_kthread.c b/tools/testing/selftests/bpf/progs/bpf_kthread.c
new file mode 100644
index 00000000000000..3eac7366514fa0
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_kthread.c
@@ -0,0 +1,414 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "bpf_experimental.h"
+#include <bpf/bpf_helpers.h>
+#include <errno.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+#define NR_THREADS 4
+#define LOOP_BOUND (1 << 23)	/* BPF_MAX_LOOPS */
+#define PARK_TIMEOUT_NS (10ULL * 1000 * 1000 * 1000)
+
+struct thread_slot {
+	struct task_struct __kptr *task;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, NR_THREADS);
+	__type(key, __u32);
+	__type(value, struct thread_slot);
+} threads SEC(".maps");
+
+/* The context of the syscall programs below. */
+struct kthread_args {
+	__u32 idx;		/* the thread's slot, and the argument of its callback */
+	__u32 start;		/* create: start the thread */
+	__u32 body;		/* create: which callback, see enum body */
+	__u32 bind;		/* create: bind the thread to cpu before starting it */
+	__u32 cpu;		/* create and bind: the CPU */
+	__u64 cgroup_id;	/* attach_cgroup: the cgroup to move the thread into */
+};
+
+enum body {
+	BODY_WORKER,		/* loop until told to return or asked to stop */
+	BODY_SPAWNER,		/* create and start thread 1 first, then as above */
+	BODY_SELF_STOPPER,	/* try to stop itself first, then as above */
+	BODY_RING,		/* stop the next thread of a ring, then return idx + 1 */
+	BODY_STOP_CHECKER,	/* park until stopped, then record what later parks return */
+	BODY_SELF_BINDER,	/* bind itself to self_bind_cpu first, then as worker */
+};
+
+__u32 runs[NR_THREADS];		/* callback entries, per slot */
+__u32 tids[NR_THREADS];		/* the thread's PID, per slot */
+char comms[NR_THREADS][16];	/* the comm the thread read for itself, per slot */
+__u32 cpus[NR_THREADS];		/* the CPU the thread last ran on, per slot */
+__u32 loops[NR_THREADS];	/* iterations of the body's loop, per slot */
+__u64 cgids[NR_THREADS];	/* the cgroup the thread last ran in, per slot */
+__u32 exit_now[NR_THREADS];	/* set by the test: the callback returns idx + 1 */
+int self_stop_ret;		/* bpf_kthread_stop() on the calling thread */
+int spawn_ret;			/* bpf_kthread_start() by the spawner */
+int foreign_start_ret;		/* bpf_kthread_start() on a task that is no BPF kthread */
+int foreign_stop_ret;		/* bpf_kthread_stop() on the same */
+int foreign_attach_ret;		/* bpf_kthread_attach_cgroup() on the same */
+__u32 ring_size;		/* set by the test: threads in the ring */
+__u32 ring_ready;		/* ring threads about to stop their successor */
+int ring_stop_ret[NR_THREADS];	/* what stopping the successor returned, per slot */
+int stop_park_rets[4];		/* what the park returned once a stop was pending */
+__u32 self_bind_cpu;		/* set by the test: where the self binder binds itself */
+int self_bind_ret;		/* what that bind returned */
+
+static __u32 load(__u32 *p)
+{
+	return *(volatile __u32 *)p;
+}
+
+/*
+ * The body of a thread: record who runs it, then sleep in bpf_task_park()
+ * until the test wakes it to return or bpf_kthread_stop() asks it to stop. The
+ * park's timeout is ten seconds, well past what a test waits, so that a lost
+ * wake fails the test instead of passing on the timeout. The bounded loop
+ * outlasts any test; a callback cannot loop without a bound.
+ */
+static int worker(__u64 arg)
+{
+	__u64 idx = arg;
+
+	if (idx >= NR_THREADS)
+		return -EINVAL;
+	tids[idx] = bpf_get_current_pid_tgid();
+	bpf_get_current_comm(comms[idx], sizeof(comms[idx]));
+	__sync_fetch_and_add(&runs[idx], 1);
+
+	bpf_repeat(LOOP_BOUND) {
+		cpus[idx] = bpf_get_smp_processor_id();
+		cgids[idx] = bpf_get_current_cgroup_id();
+		__sync_fetch_and_add(&loops[idx], 1);
+		if (bpf_kthread_should_stop())
+			return 0;
+		if (load(&exit_now[idx]))
+			return idx + 1;
+		bpf_task_park(PARK_TIMEOUT_NS, 0);
+	}
+	return -ETIME;
+}
+
+/*
+ * What bpf_task_park() returns to a thread that is being stopped: -ESHUTDOWN
+ * from the park that the stop ends or finds, and from every later one, a poll
+ * included, except one that finds a wake pending, which wins as it does over a
+ * signal. The stop is pending for good, so none of this depends on where the
+ * thread was when it arrived.
+ */
+static int stop_checker(__u64 arg)
+{
+	__u64 idx = arg;
+	int ret = 0;
+
+	if (idx >= NR_THREADS)
+		return -EINVAL;
+	__sync_fetch_and_add(&runs[idx], 1);
+
+	bpf_repeat(LOOP_BOUND) {
+		ret = bpf_task_park(PARK_TIMEOUT_NS, 0);
+		if (ret == -ESHUTDOWN)
+			break;
+	}
+	stop_park_rets[0] = ret;
+	stop_park_rets[1] = bpf_task_park(0, 0);
+	bpf_task_wake(bpf_get_current_task_btf(), 0);
+	stop_park_rets[2] = bpf_task_park(PARK_TIMEOUT_NS, 0);
+	stop_park_rets[3] = bpf_task_park(PARK_TIMEOUT_NS, 0);
+	return idx + 1;
+}
+
+/* Publish @task in slot @idx, releasing whatever the slot held. */
+static void publish(__u32 idx, struct task_struct *task)
+{
+	struct thread_slot *slot;
+
+	slot = bpf_map_lookup_elem(&threads, &idx);
+	if (!slot) {
+		bpf_task_release(task);
+		return;
+	}
+	task = bpf_kptr_xchg(&slot->task, task);
+	if (task)
+		bpf_task_release(task);
+}
+
+/* Thread 0 of the spawn case: it creates and starts thread 1, then runs as worker 0. */
+static int spawner(__u64 arg)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(worker, 1, 0);
+	if (!task) {
+		spawn_ret = -ENOMEM;
+		return -ENOMEM;
+	}
+	spawn_ret = bpf_kthread_start(task, 0);
+	publish(1, task);
+	return worker(arg);
+}
+
+static int self_stopper(__u64 arg)
+{
+	self_stop_ret = bpf_kthread_stop(bpf_get_current_task_btf(), 0);
+	return worker(arg);
+}
+
+/*
+ * Thread @idx of a ring of ring_size threads stops thread idx + 1, the last
+ * one thread 0. Each waits until all of them are about to stop, so that the
+ * stops form a ring of waits: the stop that would close it is refused with
+ * -EDEADLK and the others complete as the ring unwinds. A thread returns
+ * idx + 1 once its stop has returned, whichever way it did.
+ */
+static int ring_stopper(__u64 arg)
+{
+	__u64 idx = arg, size = load(&ring_size);
+	struct task_struct *task = NULL, *peer_task;
+	struct thread_slot *slot;
+	__u32 peer;
+
+	if (idx >= NR_THREADS || !size)
+		return -EINVAL;
+	tids[idx] = bpf_get_current_pid_tgid();
+	__sync_fetch_and_add(&runs[idx], 1);
+
+	peer = idx + 1 < size ? idx + 1 : 0;
+	slot = bpf_map_lookup_elem(&threads, &peer);
+	if (!slot)
+		return -ENOENT;
+	bpf_rcu_read_lock();
+	peer_task = slot->task;
+	if (peer_task)
+		task = bpf_task_acquire(peer_task);
+	bpf_rcu_read_unlock();
+	if (!task)
+		return -ESRCH;
+
+	/* A stop can only arrive once every thread is at the barrier: leave it then too. */
+	__sync_fetch_and_add(&ring_ready, 1);
+	bpf_repeat(LOOP_BOUND) {
+		if (load(&ring_ready) >= size || bpf_kthread_should_stop())
+			break;
+		bpf_task_park(1000000, 0);
+	}
+	ring_stop_ret[idx] = bpf_kthread_stop(task, 0);
+	bpf_task_release(task);
+	return idx + 1;
+}
+
+/* Bind itself to self_bind_cpu, then run as a worker, which records its CPU. */
+static int self_binder(__u64 arg)
+{
+	self_bind_ret = bpf_kthread_bind(bpf_get_current_task_btf(), load(&self_bind_cpu), 0);
+	return worker(arg);
+}
+
+/* Take the thread out of slot @idx; the caller releases it. */
+static struct task_struct *take(__u32 idx)
+{
+	struct thread_slot *slot;
+
+	slot = bpf_map_lookup_elem(&threads, &idx);
+	if (!slot)
+		return NULL;
+	return bpf_kptr_xchg(&slot->task, NULL);
+}
+
+SEC("syscall")
+int create_thread(struct kthread_args *args)
+{
+	struct task_struct *task;
+	int ret;
+
+	if (args->body == BODY_SPAWNER)
+		task = bpf_kthread_create(spawner, args->idx, 0);
+	else if (args->body == BODY_SELF_STOPPER)
+		task = bpf_kthread_create(self_stopper, args->idx, 0);
+	else if (args->body == BODY_RING)
+		task = bpf_kthread_create(ring_stopper, args->idx, 0);
+	else if (args->body == BODY_STOP_CHECKER)
+		task = bpf_kthread_create(stop_checker, args->idx, 0);
+	else if (args->body == BODY_SELF_BINDER)
+		task = bpf_kthread_create(self_binder, args->idx, 0);
+	else
+		task = bpf_kthread_create(worker, args->idx, 0);
+	if (!task)
+		return -EAGAIN;
+	if (args->bind) {
+		ret = bpf_kthread_bind(task, args->cpu, 0);
+		if (ret)
+			goto stop;
+	}
+	if (args->start) {
+		ret = bpf_kthread_start(task, 0);
+		if (ret)
+			goto stop;
+	}
+	publish(args->idx, task);
+	return 0;
+stop:
+	bpf_kthread_stop(task, 0);
+	bpf_task_release(task);
+	return ret;
+}
+
+/* Start the thread of slot idx through the RCU-protected pointer in its slot. */
+SEC("syscall")
+int start_thread(struct kthread_args *args)
+{
+	struct thread_slot *slot;
+	struct task_struct *task;
+	__u32 idx = args->idx;
+	int ret = -ESRCH;
+
+	slot = bpf_map_lookup_elem(&threads, &idx);
+	if (!slot)
+		return -ENOENT;
+	bpf_rcu_read_lock();
+	task = slot->task;
+	if (task)
+		ret = bpf_kthread_start(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+/* Wake the thread of slot idx from its park, through the RCU-protected pointer in its slot. */
+SEC("syscall")
+int wake_thread(struct kthread_args *args)
+{
+	struct thread_slot *slot;
+	struct task_struct *task;
+	__u32 idx = args->idx;
+	int ret = -ESRCH;
+
+	slot = bpf_map_lookup_elem(&threads, &idx);
+	if (!slot)
+		return -ENOENT;
+	bpf_rcu_read_lock();
+	task = slot->task;
+	if (task)
+		ret = bpf_task_wake(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+/* Bind the thread of slot idx to args->cpu, running or not. */
+SEC("syscall")
+int bind_thread(struct kthread_args *args)
+{
+	struct task_struct *task;
+	int ret;
+
+	task = take(args->idx);
+	if (!task)
+		return -ESRCH;
+	ret = bpf_kthread_bind(task, args->cpu, 0);
+	publish(args->idx, task);
+	return ret;
+}
+
+/* Move the thread of slot idx into the cgroup args->cgroup_id, running or not. */
+SEC("syscall")
+int attach_cgroup(struct kthread_args *args)
+{
+	struct task_struct *task;
+	struct cgroup *cgrp;
+	int ret = -ENOENT;
+
+	task = take(args->idx);
+	if (!task)
+		return -ESRCH;
+	cgrp = bpf_cgroup_from_id(args->cgroup_id);
+	if (cgrp) {
+		ret = bpf_kthread_attach_cgroup(task, cgrp, 0);
+		bpf_cgroup_release(cgrp);
+	}
+	publish(args->idx, task);
+	return ret;
+}
+
+/* bpf_kthread_attach_cgroup() on the root cgroup with @flags. */
+static int attach_root_cgroup(struct task_struct *task, __u64 flags)
+{
+	struct cgroup *cgrp;
+	int ret;
+
+	cgrp = bpf_cgroup_from_id(1);
+	if (!cgrp)
+		return -ENOENT;
+	ret = bpf_kthread_attach_cgroup(task, cgrp, flags);
+	bpf_cgroup_release(cgrp);
+	return ret;
+}
+
+/* Stop the thread of slot idx and empty the slot; returns what the stop returned. */
+SEC("syscall")
+int stop_thread(struct kthread_args *args)
+{
+	struct task_struct *task;
+	int ret;
+
+	task = take(args->idx);
+	if (!task)
+		return -ESRCH;
+	ret = bpf_kthread_stop(task, 0);
+	bpf_task_release(task);
+	return ret;
+}
+
+/* The control kfuncs refuse a task that is no BPF kthread, init here. */
+SEC("syscall")
+int control_foreign_task(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_task_from_pid(1);
+	if (!task)
+		return -ESRCH;
+	foreign_start_ret = bpf_kthread_start(task, 0);
+	foreign_stop_ret = bpf_kthread_stop(task, 0);
+	foreign_attach_ret = attach_root_cgroup(task, 0);
+	bpf_task_release(task);
+	return 0;
+}
+
+/*
+ * A thread of a user process is never asked to stop, nonzero flags are refused
+ * everywhere, and a thread stopped before it was started reports -EINTR.
+ */
+SEC("syscall")
+int misuse(void *ctx)
+{
+	struct task_struct *task;
+	int ret;
+
+	if (bpf_kthread_should_stop())
+		return 1;
+	task = bpf_kthread_create(worker, 0, 1);
+	if (task) {
+		bpf_kthread_stop(task, 0);
+		bpf_task_release(task);
+		return 2;
+	}
+	task = bpf_kthread_create(worker, 0, 0);
+	if (!task)
+		return 3;
+	if (bpf_kthread_start(task, 1) != -EINVAL)
+		ret = 4;
+	else if (bpf_kthread_stop(task, 1) != -EINVAL)
+		ret = 5;
+	else if (attach_root_cgroup(task, 1) != -EINVAL)
+		ret = 6;
+	else
+		ret = bpf_kthread_stop(task, 0) == -EINTR ? 0 : 7;
+	bpf_task_release(task);
+	return ret;
+}
diff --git a/tools/testing/selftests/bpf/progs/task_park.c b/tools/testing/selftests/bpf/progs/task_park.c
new file mode 100644
index 00000000000000..2320ede02cc9d4
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/task_park.c
@@ -0,0 +1,218 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "bpf_experimental.h"
+#include <errno.h>
+
+/* Exercise bpf_task_park() from syscall programs, bpf_task_wake() from those and tracing ones. */
+char _license[] SEC("license") = "GPL";
+
+extern struct task_struct *bpf_task_acquire(struct task_struct *task) __ksym;
+extern void bpf_task_release(struct task_struct *task) __ksym;
+extern void bpf_rcu_read_lock(void) __ksym;
+extern void bpf_rcu_read_unlock(void) __ksym;
+
+#define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
+#define WRITE_ONCE(x, v) (*(volatile typeof(x) *)&(x) = (v))
+
+enum { WAITING, WOKEN };
+
+/* The waiter publishes itself here; the waker sets the condition and wakes it. */
+struct park_elem {
+	struct task_struct __kptr *task;
+	__u32 state;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct park_elem);
+} park_map SEC(".maps");
+
+/* A tail call would run its target under the caller's protection, which a park leaves. */
+struct {
+	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+	__uint(max_entries, 1);
+	__uint(key_size, sizeof(__u32));
+	__uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+/* Bound on each sleep of the waiter, so that a lost wake fails a test instead of hanging it. */
+__u64 park_timeout_ns = 10ULL * 1000 * 1000 * 1000;
+
+static __always_inline struct park_elem *lookup_elem(void)
+{
+	__u32 key = 0;
+
+	return bpf_map_lookup_elem(&park_map, &key);
+}
+
+/* Reset the condition, then publish the task: a wake between the two sets it again. */
+static int publish_waiter(void)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+
+	if (!elem)
+		return -ENOENT;
+	WRITE_ONCE(elem->state, WAITING);
+	task = bpf_task_acquire(bpf_get_current_task_btf());
+	if (!task)
+		return -ESRCH;
+	task = bpf_kptr_xchg(&elem->task, task);
+	if (task)
+		bpf_task_release(task);
+	return 0;
+}
+
+static void withdraw_waiter(void)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+
+	if (!elem)
+		return;
+	task = bpf_kptr_xchg(&elem->task, NULL);
+	if (task)
+		bpf_task_release(task);
+}
+
+/*
+ * Sleep until wake_waiter() has set the condition. The element is a value of
+ * an array map, which the program holds, so its pointer survives the park and
+ * the loop keeps it; a value of a hash map would have to be looked up again
+ * on every turn. A park that returns 0 before the condition is set is a
+ * spurious wakeup that just goes round once more.
+ */
+SEC("syscall")
+int park_until_woken(void *ctx)
+{
+	struct park_elem *elem;
+	int i, ret;
+
+	ret = publish_waiter();
+	if (ret)
+		return ret;
+	elem = lookup_elem();
+	if (!elem)
+		return -ENOENT;
+	ret = -EAGAIN;
+	for (i = 0; i < 100; i++) {
+		if (READ_ONCE(elem->state) == WOKEN) {
+			ret = 0;
+			break;
+		}
+		ret = bpf_task_park(park_timeout_ns, 0);
+		if (ret)
+			break;
+		ret = -EAGAIN;
+	}
+	withdraw_waiter();
+	return ret;
+}
+
+/*
+ * Wake whoever published itself, without taking a reference. The condition is
+ * set only once a task is found: a waiter between its reset of the condition
+ * and its publication must not find it set, or it would return without any
+ * wake having reached it. Setting the condition before the wake is the order
+ * the kfunc's release store pairs with, so a park that consumes this wake
+ * sees the condition on its next look.
+ */
+SEC("syscall")
+int wake_waiter(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+	int ret = -ESRCH;
+
+	if (!elem)
+		return -ENOENT;
+	bpf_rcu_read_lock();
+	task = elem->task;
+	if (task) {
+		WRITE_ONCE(elem->state, WOKEN);
+		ret = bpf_task_wake(task, 0);
+	}
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+/* Set by the test: the thread whose events wake the waiter. */
+__u32 waker_tid;
+/* What the last wake from a tracing program returned. */
+int tracing_wake_ret = -1;
+
+/*
+ * The same wake from a tracing program, on an event of the thread the test
+ * names. The task kptr read under bpf_rcu_read_lock() is what bpf_task_wake()
+ * accepts, so the event takes no reference.
+ */
+static __always_inline void wake_waiter_on_event(struct task_struct *task)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *waiter;
+
+	if (!elem || task->pid != waker_tid)
+		return;
+	bpf_rcu_read_lock();
+	waiter = elem->task;
+	if (waiter) {
+		WRITE_ONCE(elem->state, WOKEN);
+		tracing_wake_ret = bpf_task_wake(waiter, 0);
+	}
+	bpf_rcu_read_unlock();
+}
+
+/* The wake from process context: task_rename fires from prctl(). */
+SEC("tp_btf/task_rename")
+int BPF_PROG(wake_on_rename, struct task_struct *task, const char *comm)
+{
+	wake_waiter_on_event(task);
+	return 0;
+}
+
+/*
+ * The wake from under the scheduler's locks: sched_waking fires inside
+ * try_to_wake_up() with the woken task's pi_lock held and IRQs disabled,
+ * where the wake cannot be issued, so it is queued for irq_work and returns 2.
+ */
+SEC("tp_btf/sched_waking")
+int BPF_PROG(wake_on_waking, struct task_struct *p)
+{
+	wake_waiter_on_event(p);
+	return 0;
+}
+
+/* Nothing wakes this task: the park sleeps 1 ms and times out. */
+SEC("syscall")
+int park_timeout(void *ctx)
+{
+	return bpf_task_park(1000000, 0);
+}
+
+/*
+ * A wake that finds its task running is kept for the next park, which returns
+ * at once even as a poll. That park consumes it, so a second poll finds
+ * nothing and reports the timeout.
+ */
+SEC("syscall")
+int park_pending(void *ctx)
+{
+	if (bpf_task_wake(bpf_get_current_task_btf(), 0))
+		return -EBUSY;
+	if (bpf_task_park(0, 0))
+		return -EAGAIN;
+	return bpf_task_park(0, 0);
+}
+
+SEC("syscall")
+int bad_flags(void *ctx)
+{
+	if (bpf_task_park(0, 1) != -EINVAL)
+		return 1;
+	if (bpf_task_wake(bpf_get_current_task_btf(), 1) != -EINVAL)
+		return 2;
+	return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_arena_stack.c b/tools/testing/selftests/bpf/progs/verifier_arena_stack.c
index b46e30a9c67f64..e09a9ce101876b 100644
--- a/tools/testing/selftests/bpf/progs/verifier_arena_stack.c
+++ b/tools/testing/selftests/bpf/progs/verifier_arena_stack.c
@@ -1307,6 +1307,33 @@ __naked void packet_write_prologue(void)
 	: __clobber_all);
 }
 
+extern int bpf_task_park(__u64 timeout_ns, __u64 flags) __ksym;
+
+/* the call is in asm, which gives the kfunc no BTF */
+void __park_btf_root(void)
+{
+	bpf_task_park(0, 0);
+}
+
+/* The stack goes back to the cpu that the run ends on, and a park may change that cpu. */
+SEC("syscall")
+__description("arena stack: a program that parks")
+__failure __msg("bpf_task_park() cannot be called in a program that uses R12")
+__naked void parks(void)
+{
+	asm volatile (
+	".8byte %[adj];"
+	"r1 = 0;"
+	"r2 = 0;"
+	"call %[bpf_task_park];"
+	"r0 = 0;"
+	"exit;"
+	:
+	: __imm_insn(adj, ADJ(16)),
+	  __imm(bpf_task_park)
+	: __clobber_all);
+}
+
 #endif
 
 char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_bpf_kthread.c b/tools/testing/selftests/bpf/progs/verifier_bpf_kthread.c
new file mode 100644
index 00000000000000..26f73270269892
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_bpf_kthread.c
@@ -0,0 +1,398 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "bpf_experimental.h"
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+struct slot {
+	struct task_struct __kptr *task;
+	struct task_struct __kptr_untrusted *untrusted_task;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct slot);
+} slots SEC(".maps");
+
+static struct slot *lookup_slot(void)
+{
+	__u32 key = 0;
+
+	return bpf_map_lookup_elem(&slots, &key);
+}
+
+static int body(__u64 arg)
+{
+	return 0;
+}
+
+static int body_negative(__u64 arg)
+{
+	return -1;
+}
+
+static int body_deref_arg(__u64 arg)
+{
+	return *(int *)arg;
+}
+
+static int body_spawns(__u64 arg)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body, arg, 0);
+	if (!task)
+		return -1;
+	bpf_task_release(task);
+	return 0;
+}
+
+static int body_parks(__u64 arg)
+{
+	return bpf_task_park(0, 0);
+}
+
+/* A global function that parks is admitted from the thread body as from the main program. */
+__noinline int kthread_park_global(void)
+{
+	return bpf_task_park(0, 0);
+}
+
+static int body_parks_via_global(__u64 arg)
+{
+	return kthread_park_global();
+}
+
+static int park_cb(__u32 index, void *data)
+{
+	bpf_task_park(0, 0);
+	return 1;
+}
+
+static int body_parks_in_callback(__u64 arg)
+{
+	bpf_loop(1, park_cb, NULL, 0);
+	return 0;
+}
+
+static int park_global_cb(__u32 index, void *data)
+{
+	kthread_park_global();
+	return 1;
+}
+
+static int body_parks_via_global_in_callback(__u64 arg)
+{
+	bpf_loop(1, park_global_cb, NULL, 0);
+	return 0;
+}
+
+/* The task bpf_kthread_create() returns carries a reference the program must release. */
+SEC("syscall")
+__failure __msg("Unreleased reference")
+int create_leaks_task(void *ctx)
+{
+	return bpf_kthread_create(body, 0, 0) != NULL;
+}
+
+SEC("syscall")
+__success
+int create_releases_task(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+/* The callback returns an int and is checked as one: a negative return is fine. */
+SEC("syscall")
+__success
+int callback_returns_negative(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_negative, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+/* The callback's argument is a number, whatever the creator passed. */
+SEC("syscall")
+__failure __msg("R1 invalid mem access 'scalar'")
+int callback_dereferences_arg(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_deref_arg, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+/* A thread may create threads. */
+SEC("syscall")
+__success
+int callback_creates_thread(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_spawns, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+/* The thread body may sleep in bpf_task_park(), directly or through a global function. */
+SEC("syscall")
+__success
+int callback_parks(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_parks, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+SEC("syscall")
+__success
+int callback_parks_via_global(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_parks_via_global, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+/* A callback under the thread body is a callback like any other: no park there. */
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int callback_parks_in_nested_callback(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_parks_in_callback, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("global function kthread_park_global() may call bpf_task_park(), "
+		"which is not allowed in a callback")
+int callback_parks_via_global_in_nested_callback(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_parks_via_global_in_callback, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+__u32 indexed[4];
+
+/* The argument is an unknown scalar: the body bounds it before it indexes with it. */
+static int body_indexes_arg(__u64 arg)
+{
+	if (arg < 4)
+		return indexed[arg];
+	return 0;
+}
+
+SEC("syscall")
+__success
+int callback_indexes_with_arg(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_indexes_arg, 1, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+/*
+ * The creation waits for kthreadd with migration enabled, so the program may
+ * go on on another CPU, where the time slice of may_goto is another run's.
+ */
+SEC("syscall")
+__failure __msg("may_goto cannot be used in a program that may migrate")
+int create_with_may_goto(void *ctx)
+{
+	struct task_struct *task;
+	int i;
+
+	for (i = 0; i < 4 && can_loop; i++) {
+		task = bpf_kthread_create(body_indexes_arg, i, 0);
+		if (task)
+			bpf_task_release(task);
+	}
+	return 0;
+}
+
+struct wq_elem {
+	struct bpf_wq wq;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct wq_elem);
+} wq_map SEC(".maps");
+
+/* Taking the address of a body that parks does not make this function park. */
+__noinline int spawn_parking_thread(void)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_parks, 0, 0);
+	if (!task)
+		return -1;
+	bpf_task_release(task);
+	return 0;
+}
+
+static int spawn_wq_cb(void *map, int *key, void *value)
+{
+	spawn_parking_thread();
+	return 0;
+}
+
+/* So a global function that creates such a thread may be called from a callback. */
+SEC("syscall")
+__success
+int global_spawner_in_wq_callback(void *ctx)
+{
+	struct wq_elem *elem;
+	__u32 key = 0;
+
+	elem = bpf_map_lookup_elem(&wq_map, &key);
+	if (!elem)
+		return 0;
+	if (bpf_wq_init(&elem->wq, &wq_map, 0))
+		return 0;
+	return bpf_wq_set_callback(&elem->wq, spawn_wq_cb, 0);
+}
+
+/*
+ * bpf_kthread_stop() takes a referenced or trusted task; bpf_kthread_start()
+ * also an RCU-protected one that is not NULL.
+ */
+SEC("syscall")
+__failure __msg("R1 must be referenced or trusted")
+int stop_untrusted_task(void *ctx)
+{
+	struct slot *slot = lookup_slot();
+	struct task_struct *task;
+
+	if (!slot)
+		return 0;
+	task = slot->untrusted_task;
+	if (!task)
+		return 0;
+	return bpf_kthread_stop(task, 0);
+}
+
+SEC("syscall")
+__failure __msg("R1 must be a rcu pointer")
+int start_untrusted_task(void *ctx)
+{
+	struct slot *slot = lookup_slot();
+	struct task_struct *task;
+
+	if (!slot)
+		return 0;
+	task = slot->untrusted_task;
+	if (!task)
+		return 0;
+	return bpf_kthread_start(task, 0);
+}
+
+SEC("syscall")
+__failure __msg("Possibly NULL pointer passed to trusted R1")
+int start_maybe_null_task(void *ctx)
+{
+	struct slot *slot = lookup_slot();
+	struct task_struct *task;
+	int ret;
+
+	if (!slot)
+		return 0;
+	bpf_rcu_read_lock();
+	task = slot->task;
+	ret = bpf_kthread_start(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+SEC("syscall")
+__success
+int start_rcu_task(void *ctx)
+{
+	struct slot *slot = lookup_slot();
+	struct task_struct *task;
+	int ret = 0;
+
+	if (!slot)
+		return 0;
+	bpf_rcu_read_lock();
+	task = slot->task;
+	if (task)
+		ret = bpf_kthread_start(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+/* bpf_kthread_stop() sleeps, so a pointer that only an RCU section protects will not do. */
+SEC("syscall")
+__failure __msg("kernel func bpf_kthread_stop is sleepable within rcu_read_lock region")
+int stop_rcu_task(void *ctx)
+{
+	struct slot *slot = lookup_slot();
+	struct task_struct *task;
+	int ret = 0;
+
+	if (!slot)
+		return 0;
+	bpf_rcu_read_lock();
+	task = slot->task;
+	if (task)
+		ret = bpf_kthread_stop(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+SEC("tp/syscalls/sys_enter_nanosleep")
+__failure __msg("calling kernel function bpf_kthread_create is not allowed")
+int create_from_tracepoint(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body, 0, 0);
+	if (task)
+		bpf_task_release(task);
+	return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_task_park.c b/tools/testing/selftests/bpf/progs/verifier_task_park.c
new file mode 100644
index 00000000000000..33bc29f0805871
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_task_park.c
@@ -0,0 +1,550 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#define BPF_NO_KFUNC_PROTOTYPES
+#include <vmlinux.h>
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+#include <bpf_arena_common.h>
+
+/* What survives a bpf_task_park(), what does not, and where the park is refused. */
+char _license[] SEC("license") = "GPL";
+
+extern struct task_struct *bpf_task_acquire(struct task_struct *task) __ksym;
+extern void bpf_task_release(struct task_struct *task) __ksym;
+extern void bpf_rcu_read_lock(void) __ksym;
+extern void bpf_rcu_read_unlock(void) __ksym;
+extern void bpf_local_irq_save(unsigned long *flags) __weak __ksym;
+extern void bpf_local_irq_restore(unsigned long *flags) __weak __ksym;
+extern int bpf_res_spin_lock(struct bpf_res_spin_lock *lock) __weak __ksym;
+extern void bpf_res_spin_unlock(struct bpf_res_spin_lock *lock) __weak __ksym;
+extern int bpf_iter_bits_new(struct bpf_iter_bits *it, const u64 *unsafe_ptr__ign,
+			     u32 nr_words) __weak __ksym;
+extern void bpf_iter_bits_destroy(struct bpf_iter_bits *it) __weak __ksym;
+
+struct park_elem {
+	struct bpf_spin_lock lock;
+	struct task_struct __kptr *task;
+	struct task_struct __kptr_untrusted *untrusted_task;
+	__u32 state;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct park_elem);
+} park_map SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct park_elem);
+} hash_map SEC(".maps");
+
+struct rlock_elem {
+	struct bpf_res_spin_lock lock;
+	__u32 state;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct rlock_elem);
+} rlock_map SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u32);
+	__array(values, struct {
+		__uint(type, BPF_MAP_TYPE_ARRAY);
+		__uint(max_entries, 1);
+		__type(key, __u32);
+		__type(value, __u32);
+	});
+} outer_map SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_RINGBUF);
+	__uint(max_entries, 4096);
+} ringbuf SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARENA);
+	__uint(map_flags, BPF_F_MMAPABLE);
+	__uint(max_entries, 2);
+} arena SEC(".maps");
+
+static __always_inline struct park_elem *lookup_elem(void)
+{
+	__u32 key = 0;
+
+	return bpf_map_lookup_elem(&park_map, &key);
+}
+
+/*
+ * A hash map frees a deleted element after a grace period the sleeper no
+ * longer delays: its value is gone after a park, in a register or spilled.
+ */
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int hash_value_after_park(void *ctx)
+{
+	struct park_elem *elem;
+	__u32 key = 0;
+
+	elem = bpf_map_lookup_elem(&hash_map, &key);
+	if (!elem)
+		return 0;
+	bpf_task_park(0, 0);
+	return elem->state;
+}
+
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+__naked int spilled_hash_value_after_park(void)
+{
+	asm volatile ("					\
+	r1 = 0;						\
+	*(u32 *)(r10 - 4) = r1;				\
+	r2 = r10;					\
+	r2 += -4;					\
+	r1 = %[hash_map] ll;				\
+	call %[bpf_map_lookup_elem];			\
+	if r0 == 0 goto l0_%=;				\
+	*(u64 *)(r10 - 16) = r0;			\
+	r1 = 0;						\
+	r2 = 0;						\
+	call bpf_task_park;				\
+	r6 = *(u64 *)(r10 - 16);			\
+	r0 = *(u32 *)(r6 + %[state_off]);		\
+l0_%=:	exit;						\
+"	:
+	: __imm(bpf_map_lookup_elem),
+	  __imm_addr(hash_map),
+	  __imm_const(state_off, offsetof(struct park_elem, state))
+	: __clobber_all);
+}
+
+/* An array allocates its values with the map, which the program holds: they survive. */
+SEC("syscall")
+__success __retval(0)
+int array_value_survives_park(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+
+	if (!elem)
+		return 1;
+	bpf_task_park(0, 0);
+	return elem->state;
+}
+
+/*
+ * Global variables are array values too, and the compiler hoists their address
+ * out of loops; whether it does is its choice, so the hoisted form is pinned in
+ * assembly as well.
+ */
+__u32 rounds = 2;
+
+SEC("syscall")
+__success __retval(0)
+int global_survives_park_in_loop(void *ctx)
+{
+	__u32 i, n = 0;
+
+	for (i = 0; i < 4 && i < rounds; i++) {
+		bpf_task_park(0, 0);
+		n++;
+	}
+	return n != 2;
+}
+
+SEC("syscall")
+__success __retval(2)
+__naked int global_pointer_survives_park(void)
+{
+	asm volatile ("					\
+	r6 = %[rounds] ll;				\
+	r1 = 0;						\
+	r2 = 0;						\
+	call bpf_task_park;				\
+	r0 = *(u32 *)(r6 + 0);				\
+	exit;						\
+"	:
+	: __imm_addr(rounds)
+	: __clobber_all);
+}
+
+/* A per-CPU value is gone: the task may resume on another CPU. */
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u32);
+} percpu_map SEC(".maps");
+
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int percpu_value_after_park(void *ctx)
+{
+	__u32 key = 0, *val;
+
+	val = bpf_map_lookup_elem(&percpu_map, &key);
+	if (!val)
+		return 0;
+	bpf_task_park(0, 0);
+	return *val;
+}
+
+/* A trusted but unreferenced pointer is gone after a park. */
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int task_pointer_after_park(void *ctx)
+{
+	struct task_struct *task = bpf_get_current_task_btf();
+
+	bpf_task_park(0, 0);
+	return task->pid;
+}
+
+/* A referenced pointer survives a park. */
+SEC("syscall")
+__success __retval(0)
+int referenced_task_survives_park(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_task_acquire(bpf_get_current_task_btf());
+	if (!task)
+		return 1;
+	bpf_task_park(0, 0);
+	bpf_task_release(task);
+	return 0;
+}
+
+/* The stack and the context belong to the invocation: both survive a park. */
+SEC("syscall")
+__success __retval(7)
+int stack_survives_park(void *ctx)
+{
+	volatile __u32 word = 7;
+
+	bpf_task_park(0, 0);
+	return word;
+}
+
+/*
+ * Load only: the runner refuses a context smaller than the program reads, and
+ * the loader passes none.
+ */
+SEC("syscall")
+__success
+int ctx_survives_park(__u32 *ctx)
+{
+	bpf_task_park(0, 0);
+	return *ctx;
+}
+
+/* Arena memory is held by the arena map, which the program holds: it survives a park. */
+SEC("syscall")
+__success __retval(0)
+int arena_pointer_survives_park(void *ctx)
+{
+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
+	volatile int __arena *page;
+
+	page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+	if (!page)
+		return 1;
+	*page = 7;
+	bpf_task_park(0, 0);
+	return *page != 7;
+#else
+	/* Nothing to test without arena casts; the program must still load. */
+	return 0;
+#endif
+}
+
+/* A map the program references directly is held by the invocation: its pointer survives. */
+SEC("syscall")
+__success __retval(0)
+__naked int map_pointer_survives_park(void)
+{
+	asm volatile ("					\
+	r6 = %[park_map] ll;				\
+	r1 = 0;						\
+	r2 = 0;						\
+	call bpf_task_park;				\
+	r1 = 0;						\
+	*(u32 *)(r10 - 4) = r1;				\
+	r2 = r10;					\
+	r2 += -4;					\
+	r1 = r6;					\
+	call %[bpf_map_lookup_elem];			\
+	r0 = 0;						\
+	exit;						\
+"	:
+	: __imm(bpf_map_lookup_elem),
+	  __imm_addr(park_map)
+	: __clobber_all);
+}
+
+/*
+ * An inner map is held only by the outer map's element: neither its pointer
+ * nor its values survive, even though the inner map is an array.
+ */
+SEC("syscall")
+__failure __msg("expected=map_ptr")
+int inner_map_pointer_after_park(void *ctx)
+{
+	__u32 key = 0;
+	void *inner;
+
+	inner = bpf_map_lookup_elem(&outer_map, &key);
+	if (!inner)
+		return 0;
+	bpf_task_park(0, 0);
+	return bpf_map_lookup_elem(inner, &key) != NULL;
+}
+
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int inner_array_value_after_park(void *ctx)
+{
+	__u32 key = 0, *val;
+	void *inner;
+
+	inner = bpf_map_lookup_elem(&outer_map, &key);
+	if (!inner)
+		return 0;
+	val = bpf_map_lookup_elem(inner, &key);
+	if (!val)
+		return 0;
+	bpf_task_park(0, 0);
+	return *val;
+}
+
+/* A dynptr on the stack is refused across a park, referenced or not. */
+char dynptr_buf[8];
+
+SEC("syscall")
+__failure __msg("bpf_task_park() with a live dynptr at frame 0")
+int dynptr_across_park(void *ctx)
+{
+	struct bpf_dynptr ptr;
+
+	bpf_dynptr_from_mem(dynptr_buf, sizeof(dynptr_buf), 0, &ptr);
+	bpf_task_park(0, 0);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() with a live dynptr at frame 0")
+int ringbuf_dynptr_across_park(void *ctx)
+{
+	struct bpf_dynptr ptr;
+
+	bpf_ringbuf_reserve_dynptr(&ringbuf, 8, 0, &ptr);
+	bpf_task_park(0, 0);
+	bpf_ringbuf_discard_dynptr(&ptr, 0);
+	return 0;
+}
+
+/* A number iterator lives entirely on the stack: a park inside the loop is fine. */
+SEC("syscall")
+__success __retval(0)
+int num_iterator_survives_park(void *ctx)
+{
+	int i;
+
+	bpf_for(i, 0, 3)
+		bpf_task_park(0, 0);
+	return 0;
+}
+
+/* Any other iterator is refused across a park. */
+SEC("syscall")
+__failure __msg("bpf_task_park() with a live bits iterator at frame 0")
+int bits_iterator_across_park(void *ctx)
+{
+	struct bpf_iter_bits it;
+	__u64 word = 1;
+
+	bpf_iter_bits_new(&it, &word, 1);
+	bpf_task_park(0, 0);
+	bpf_iter_bits_destroy(&it);
+	return 0;
+}
+
+/* Regions that pin the CPU or hold a lock refuse a sleepable call, the park included. */
+SEC("syscall")
+__failure __msg("function calls are not allowed while holding a lock")
+int park_while_locked(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+
+	if (!elem)
+		return 0;
+	bpf_spin_lock(&elem->lock);
+	bpf_task_park(0, 0);
+	bpf_spin_unlock(&elem->lock);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("function calls are not allowed while holding a lock")
+int park_while_res_locked(void *ctx)
+{
+	struct rlock_elem *elem;
+	__u32 key = 0;
+
+	elem = bpf_map_lookup_elem(&rlock_map, &key);
+	if (!elem)
+		return 0;
+	if (bpf_res_spin_lock(&elem->lock))
+		return 0;
+	bpf_task_park(0, 0);
+	bpf_res_spin_unlock(&elem->lock);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("kernel func bpf_task_park is sleepable within rcu_read_lock region")
+int park_in_rcu_region(void *ctx)
+{
+	bpf_rcu_read_lock();
+	bpf_task_park(0, 0);
+	bpf_rcu_read_unlock();
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("kernel func bpf_task_park is sleepable within non-preemptible region")
+int park_in_preempt_disabled_region(void *ctx)
+{
+	bpf_preempt_disable();
+	bpf_task_park(0, 0);
+	bpf_preempt_enable();
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("kernel func bpf_task_park is sleepable within IRQ-disabled region")
+int park_in_irq_disabled_region(void *ctx)
+{
+	unsigned long flags;
+
+	bpf_local_irq_save(&flags);
+	bpf_task_park(0, 0);
+	bpf_local_irq_restore(&flags);
+	return 0;
+}
+
+/*
+ * The time slice of may_goto is kept per CPU, and a program that parks may go
+ * on on another one. That holds for a may_goto the program asks for and for one
+ * the verifier adds to a loop it cannot bound.
+ */
+SEC("syscall")
+__failure __msg("may_goto cannot be used in a program that may migrate")
+int park_with_may_goto(void *ctx)
+{
+	int i;
+
+	for (i = 0; i < 4 && can_loop; i++)
+		bpf_task_park(0, 0);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("may_goto cannot be used in a program that may migrate")
+int park_in_unbounded_loop(void *ctx)
+{
+	while (*(volatile __u32 *)&rounds)
+		bpf_task_park(0, 0);
+	return 0;
+}
+
+/* The park is registered for syscall programs only. */
+SEC("tp/syscalls/sys_enter_nanosleep")
+__failure __msg("calling kernel function bpf_task_park is not allowed")
+int park_from_non_syscall(void *ctx)
+{
+	return bpf_task_park(0, 0);
+}
+
+/* The wake is registered for every program type. */
+SEC("xdp")
+__success
+int wake_from_xdp(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+
+	if (!elem)
+		return XDP_PASS;
+	bpf_rcu_read_lock();
+	task = elem->task;
+	if (task)
+		bpf_task_wake(task, 0);
+	bpf_rcu_read_unlock();
+	return XDP_PASS;
+}
+
+/* bpf_task_wake() takes a referenced, trusted or RCU-protected task that is not NULL. */
+SEC("syscall")
+__failure __msg("R1 must be a rcu pointer")
+int wake_untrusted_task(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+
+	if (!elem)
+		return 0;
+	task = elem->untrusted_task;
+	if (!task)
+		return 0;
+	return bpf_task_wake(task, 0);
+}
+
+SEC("syscall")
+__failure __msg("Possibly NULL pointer passed to trusted R1")
+int wake_maybe_null_task(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+	int ret;
+
+	if (!elem)
+		return 0;
+	bpf_rcu_read_lock();
+	task = elem->task;
+	ret = bpf_task_wake(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+SEC("syscall")
+__success
+int wake_rcu_task(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+	int ret = 0;
+
+	if (!elem)
+		return 0;
+	bpf_rcu_read_lock();
+	task = elem->task;
+	if (task)
+		ret = bpf_task_wake(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_task_park_frames.c b/tools/testing/selftests/bpf/progs/verifier_task_park_frames.c
new file mode 100644
index 00000000000000..f374e9051fe5e1
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_task_park_frames.c
@@ -0,0 +1,393 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "bpf_experimental.h"
+#include <errno.h>
+#include "bpf_misc.h"
+
+/* Where a bpf_task_park() may sit in the call graph, and what its callers lose. */
+char _license[] SEC("license") = "GPL";
+
+struct park_elem {
+	__u32 state;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct park_elem);
+} park_map SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct park_elem);
+} hash_map SEC(".maps");
+
+struct wq_elem {
+	struct bpf_wq wq;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct wq_elem);
+} wq_map SEC(".maps");
+
+struct tw_elem {
+	struct bpf_task_work tw;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct tw_elem);
+} tw_map SEC(".maps");
+
+static __always_inline struct park_elem *lookup_hash_elem(void)
+{
+	__u32 key = 0;
+
+	return bpf_map_lookup_elem(&hash_map, &key);
+}
+
+/* A static function that parks is verified in its caller's state: the park sees every frame. */
+static __noinline int park_static(void)
+{
+	return bpf_task_park(0, 0);
+}
+
+/* A global function is verified alone: its callers rely on the might_park mark. */
+__noinline int park_global(void)
+{
+	return bpf_task_park(0, 0);
+}
+
+/* The mark reaches callers through further static and global functions. */
+static __noinline int park_via_static(void)
+{
+	return park_global();
+}
+
+__noinline int park_via_global(void)
+{
+	return park_global();
+}
+
+/* The mark reaches a caller that sleeps on its own account as well. */
+__noinline int park_and_sleep_global(void)
+{
+	char buf[8];
+
+	park_static();
+	return bpf_copy_from_user(buf, sizeof(buf), NULL);
+}
+
+/* A park in a function called directly from the main program is allowed. */
+SEC("syscall")
+__success __retval(0)
+int park_in_static_subprog(void *ctx)
+{
+	return park_static() != -ETIMEDOUT;
+}
+
+SEC("syscall")
+__success __retval(0)
+int park_in_global_subprog(void *ctx)
+{
+	return park_global() != -ETIMEDOUT;
+}
+
+/* The caller's hash map value does not survive a park in its callee, static or global. */
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int hash_value_after_static_subprog_park(void *ctx)
+{
+	struct park_elem *elem = lookup_hash_elem();
+
+	if (!elem)
+		return 0;
+	park_static();
+	return elem->state;
+}
+
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int hash_value_after_global_subprog_park(void *ctx)
+{
+	struct park_elem *elem = lookup_hash_elem();
+
+	if (!elem)
+		return 0;
+	park_global();
+	return elem->state;
+}
+
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int hash_value_after_nested_subprog_park(void *ctx)
+{
+	struct park_elem *elem = lookup_hash_elem();
+
+	if (!elem)
+		return 0;
+	park_via_static();
+	return elem->state;
+}
+
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int hash_value_after_sleeping_subprog_park(void *ctx)
+{
+	struct park_elem *elem = lookup_hash_elem();
+
+	if (!elem)
+		return 0;
+	park_and_sleep_global();
+	return elem->state;
+}
+
+/* A dynptr held by the caller is as live across a global call that parks as across the park. */
+char dynptr_buf[8];
+
+SEC("syscall")
+__failure __msg("bpf_task_park() with a live dynptr at frame 0")
+int dynptr_across_global_subprog_park(void *ctx)
+{
+	struct bpf_dynptr ptr;
+
+	bpf_dynptr_from_mem(dynptr_buf, sizeof(dynptr_buf), 0, &ptr);
+	park_global();
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() with a live bits iterator at frame 0")
+int bits_iterator_across_global_subprog_park(void *ctx)
+{
+	struct bpf_iter_bits it;
+	__u64 word = 1;
+
+	bpf_iter_bits_new(&it, &word, 1);
+	park_global();
+	bpf_iter_bits_destroy(&it);
+	return 0;
+}
+
+/*
+ * No callback other than the body of a BPF kthread may park: not directly, not
+ * through a static or a global function.
+ */
+static int park_cb(__u32 index, void *data)
+{
+	bpf_task_park(0, 0);
+	return 1;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int park_in_loop_callback(void *ctx)
+{
+	bpf_loop(1, park_cb, NULL, 0);
+	return 0;
+}
+
+static int park_static_cb(__u32 index, void *data)
+{
+	park_static();
+	return 1;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int static_subprog_park_in_callback(void *ctx)
+{
+	bpf_loop(1, park_static_cb, NULL, 0);
+	return 0;
+}
+
+static int park_global_cb(__u32 index, void *data)
+{
+	park_global();
+	return 1;
+}
+
+SEC("syscall")
+__failure __msg("global function park_global() may call bpf_task_park(), "
+		"which is not allowed in a callback")
+int global_subprog_park_in_callback(void *ctx)
+{
+	bpf_loop(1, park_global_cb, NULL, 0);
+	return 0;
+}
+
+static int park_via_global_cb(__u32 index, void *data)
+{
+	park_via_global();
+	return 1;
+}
+
+SEC("syscall")
+__failure __msg("global function park_via_global() may call bpf_task_park(), "
+		"which is not allowed in a callback")
+int nested_global_subprog_park_in_callback(void *ctx)
+{
+	bpf_loop(1, park_via_global_cb, NULL, 0);
+	return 0;
+}
+
+static int park_for_each_cb(struct bpf_map *map, __u32 *key, struct park_elem *val, void *data)
+{
+	bpf_task_park(0, 0);
+	return 1;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int park_in_for_each_callback(void *ctx)
+{
+	bpf_for_each_map_elem(&park_map, park_for_each_cb, NULL, 0);
+	return 0;
+}
+
+#ifdef CAN_USE_CALLX
+
+/* calls_arg(fn) { return fn(); } */
+__naked __noinline __used
+static unsigned long calls_arg(void)
+{
+	asm volatile ("					\
+	callx r1;					\
+	exit;						\
+"	::: __clobber_all);
+}
+
+/*
+ * The program loads the address of the function that parks, calls_arg() calls
+ * it. Nothing marks calls_arg(), so it is verified on its own, without its
+ * callers, until its exit says that it parked. The hash map value of the
+ * program is gone after the call all the same.
+ */
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+__naked int hash_value_after_callx_park(void)
+{
+	asm volatile ("					\
+	r1 = 0;						\
+	*(u32 *)(r10 - 4) = r1;				\
+	r2 = r10;					\
+	r2 += -4;					\
+	r1 = %[hash_map] ll;				\
+	call %[bpf_map_lookup_elem];			\
+	if r0 == 0 goto l0_%=;				\
+	r6 = r0;					\
+	r1 = %[park_static] ll;				\
+	call calls_arg;					\
+	r0 = *(u32 *)(r6 + %[state_off]);		\
+l0_%=:	exit;						\
+"	:
+	: __imm(bpf_map_lookup_elem),
+	  __imm_addr(hash_map),
+	  __imm_addr(park_static),
+	  __imm_const(state_off, offsetof(struct park_elem, state))
+	: __clobber_all);
+}
+
+/* Nor does the callback get to park behind calls_arg(). */
+__naked __noinline __used
+static unsigned long park_callx_cb(void)
+{
+	asm volatile ("					\
+	r1 = %[park_static] ll;				\
+	call calls_arg;					\
+	r0 = 1;						\
+	exit;						\
+"	:
+	: __imm_addr(park_static)
+	: __clobber_all);
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+__naked int callx_park_in_callback(void)
+{
+	asm volatile ("					\
+	r1 = 1;						\
+	r2 = %[park_callx_cb] ll;			\
+	r3 = 0;						\
+	r4 = 0;						\
+	call %[bpf_loop];				\
+	r0 = 0;						\
+	exit;						\
+"	:
+	: __imm(bpf_loop),
+	  __imm_addr(park_callx_cb)
+	: __clobber_all);
+}
+
+#endif /* CAN_USE_CALLX */
+
+/* An asynchronous callback is verified as its own state, with the callback as frame 0. */
+static int park_wq_cb(void *map, int *key, void *value)
+{
+	bpf_task_park(0, 0);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int park_in_wq_callback(void *ctx)
+{
+	struct wq_elem *elem;
+	__u32 key = 0;
+
+	elem = bpf_map_lookup_elem(&wq_map, &key);
+	if (!elem)
+		return 0;
+	if (bpf_wq_init(&elem->wq, &wq_map, 0))
+		return 0;
+	if (bpf_wq_set_callback(&elem->wq, park_wq_cb, 0))
+		return 0;
+	return bpf_wq_start(&elem->wq, 0);
+}
+
+static int park_task_work_cb(struct bpf_map *map, void *key, void *value)
+{
+	bpf_task_park(0, 0);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int park_in_task_work_callback(void *ctx)
+{
+	struct task_struct *task = bpf_get_current_task_btf();
+	struct tw_elem *elem;
+	__u32 key = 0;
+
+	elem = bpf_map_lookup_elem(&tw_map, &key);
+	if (!elem)
+		return 0;
+	return bpf_task_work_schedule_signal(task, &elem->tw, &tw_map, park_task_work_cb);
+}
+
+/* The exception callback unwinds the program after bpf_throw(): no park there either. */
+__noinline int park_exception_cb(u64 cookie)
+{
+	bpf_task_park(0, 0);
+	return 0;
+}
+
+SEC("syscall")
+__exception_cb(park_exception_cb)
+__arch_x86_64 __arch_arm64 __arch_s390x
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int park_in_exception_callback(void *ctx)
+{
+	bpf_throw(0);
+	return 0;
+}

3/2 2026/11/12 22:39 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 62646
							output: 38
							thoughts: 334
						

Thoughts:
**My Initial Assessment of the BPF Patch**

Okay, so this patch is introducing some significant new BPF functionality. Let's break it down: First, we have `bpf_task_park` and `bpf_task_wake` kfuncs. Then, full BPF support for kernel threads with functions like `bpf_kthread_create`, `bpf_kthread_start`, etc. The patch also modifies `cgroup_attach_kthread`, and, crucially, a lot of verifier logic in various files, including `verifier.c`, `cfg.c`, etc. I see changes in the core BPF code, syscall interfaces, the scheduler, `fork`, and `filter.h`.

This is a deep dive into core BPF subsystems. It's touching the verifier, scheduling/task interactions, and cgroups, all of which are critical. And, of course, userspace can access this through the `bpf(2)` syscall, specifically `BPF_PROG_LOAD` and `BPF_PROG_RUN`. 

Given the broad scope and the user-space interface, I'm thinking this is definitely `WorthFuzzing=true`. The potential for unexpected behavior or vulnerabilities introduced by new features or interactions with existing code is high.

Now, I need to make sure we're properly configured for fuzzing this. I need to nail down the appropriate `EnableConfigs` and `FocusSymbols`. Let me dig into the codebase; I'll need to search the repository or directly check the files mentioned to confirm specific config options and identify relevant symbol names.



4/2 2026/11/12 22:40 tool grepper
0m Args:
{
  "Expression": "bpf_task_park|bpf_kthread",
  "PathPrefixes": [
    "kernel/bpf/"
  ]
}

Results:
{
  "Output": "\nFull output is too long, showing 200 out of 501 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (5 files in total):\nkernel/bpf/arraymap.c:1\nkernel/bpf/diagnostics.c:1\nkernel/bpf/kthread.c:100\nkernel/bpf/task_park.c:18\nkernel/bpf/verifier.c:41\n\nkernel/bpf/arraymap.c=1022=static void *prog_fd_array_get_ptr(struct bpf_map *map,\n--\nkernel/bpf/arraymap.c-1033-\t * section alone, and a program that parks leaves that section for its\nkernel/bpf/arraymap.c:1034:\t * sleep, see bpf_task_park().\nkernel/bpf/arraymap.c-1035-\t */\n--\nkernel/bpf/diagnostics.c=2200=static void diag_print_mod(struct bpf_verifier_env *env, const struct bpf_diag_history_event *event)\n--\nkernel/bpf/diagnostics.c-2234-\tcase BPF_DIAG_MOD_TASK_PARK:\nkernel/bpf/diagnostics.c:2235:\t\treason = \"the sleep in bpf_task_park() invalidated this pointer\";\nkernel/bpf/diagnostics.c-2236-\t\tbreak;\n--\nkernel/bpf/kthread.c-17- * the program that created it, run once. The callback is the body of the\nkernel/bpf/kthread.c:18: * thread: it loops, sleeps in bpf_task_park() and returns on its own, and its\nkernel/bpf/kthread.c-19- * return ends the thread. The kernel adds nothing in between.\nkernel/bpf/kthread.c-20- *\nkernel/bpf/kthread.c:21: * The thread is known to the program by its task_struct. bpf_kthread_create()\nkernel/bpf/kthread.c-22- * returns it as a referenced pointer, and the control kfuncs take it back, so\n--\nkernel/bpf/kthread.c-39- * user, the way a timer or a work item ends with the last user of its map.\nkernel/bpf/kthread.c:40: * The one exception is a thread that bpf_kthread_stop() has taken off the\nkernel/bpf/kthread.c-41- * lists but that has not exited yet: its stopper holds the program for it.\n--\nkernel/bpf/kthread.c-43- * Four of the kfuncs wait for another task. Creation waits for kthreadd,\nkernel/bpf/kthread.c:44: * bpf_kthread_stop() for the thread's exit, and bpf_kthread_bind() and\nkernel/bpf/kthread.c:45: * bpf_kthread_attach_cgroup() for a running thread's next migrate_enable(),\nkernel/bpf/kthread.c-46- * which is what set_cpus_allowed_ptr() waits for on any migration-disabled\n--\nkernel/bpf/kthread.c-64- * refused these kfuncs as it is refused the park. The caller may return on\nkernel/bpf/kthread.c:65: * another CPU, the contract bpf_task_park() already carries, so a per-CPU map\nkernel/bpf/kthread.c-66- * value pointer or a CPU id obtained before such a call names the CPU of the\n--\nkernel/bpf/kthread.c-70-\nkernel/bpf/kthread.c:71:struct bpf_kthread {\nkernel/bpf/kthread.c:72:\tstruct list_head node;\t\t/* on bpf_kthreads */\nkernel/bpf/kthread.c-73-\tstruct list_head prog_node;\t/* on aux-\u003ekthreads */\n--\nkernel/bpf/kthread.c-75-\tstruct bpf_prog_aux *aux;\t/* the program whose callback this is */\nkernel/bpf/kthread.c:76:\tstruct bpf_kthread *waits_for;\t/* the thread this one is stopping, under the lock */\nkernel/bpf/kthread.c-77-\tvoid *callback_fn;\n--\nkernel/bpf/kthread.c-82- * Both lists, and every object on them, are under this lock. It is a raw lock\nkernel/bpf/kthread.c:83: * taken with IRQs disabled because bpf_kthread_start() is not sleepable: a\nkernel/bpf/kthread.c-84- * timer callback calls it from softirq, and a program may call it with IRQs\n--\nkernel/bpf/kthread.c-86- */\nkernel/bpf/kthread.c:87:static LIST_HEAD(bpf_kthreads);\nkernel/bpf/kthread.c:88:static DEFINE_RAW_SPINLOCK(bpf_kthreads_lock);\nkernel/bpf/kthread.c-89-\nkernel/bpf/kthread.c:90:static int bpf_kthread_fn(void *data)\nkernel/bpf/kthread.c-91-{\nkernel/bpf/kthread.c:92:\tstruct bpf_kthread *kthread = data;\nkernel/bpf/kthread.c-93-\tbpf_callback_t callback_fn = kthread-\u003ecallback_fn;\n--\nkernel/bpf/kthread.c-103-\t * migration disabled under a Tasks Trace RCU read section, except\nkernel/bpf/kthread.c:104:\t * while it sleeps in bpf_task_park(), which drops both for the sleep\nkernel/bpf/kthread.c-105-\t * and takes them again before it returns, and while it waits in one\n--\nkernel/bpf/kthread.c-130-\nkernel/bpf/kthread.c:131:/* The BPF kthread whose task is @task, if any. Call with bpf_kthreads_lock held. */\nkernel/bpf/kthread.c:132:static struct bpf_kthread *bpf_kthread_find(struct task_struct *task)\nkernel/bpf/kthread.c-133-{\nkernel/bpf/kthread.c:134:\tstruct bpf_kthread *kthread;\nkernel/bpf/kthread.c-135-\nkernel/bpf/kthread.c:136:\tlockdep_assert_held(\u0026bpf_kthreads_lock);\nkernel/bpf/kthread.c:137:\tlist_for_each_entry(kthread, \u0026bpf_kthreads, node)\nkernel/bpf/kthread.c-138-\t\tif (kthread-\u003etask == task)\n--\nkernel/bpf/kthread.c-142-\nkernel/bpf/kthread.c:143:/* Take a thread off both lists. Call with bpf_kthreads_lock held. */\nkernel/bpf/kthread.c:144:static void bpf_kthread_unlink(struct bpf_kthread *kthread)\nkernel/bpf/kthread.c-145-{\nkernel/bpf/kthread.c:146:\tlockdep_assert_held(\u0026bpf_kthreads_lock);\nkernel/bpf/kthread.c-147-\tlist_del(\u0026kthread-\u003enode);\n--\nkernel/bpf/kthread.c-157- */\nkernel/bpf/kthread.c:158:static struct bpf_kthread *bpf_kthread_self(void)\nkernel/bpf/kthread.c-159-{\nkernel/bpf/kthread.c:160:\tif (kthread_func(current) != bpf_kthread_fn || (current-\u003eflags \u0026 PF_EXITING))\nkernel/bpf/kthread.c-161-\t\treturn NULL;\n--\nkernel/bpf/kthread.c-166- * Whether a stop of @kthread would wait, through the chain of threads each of\nkernel/bpf/kthread.c:167: * them is stopping, for the calling thread. Call with bpf_kthreads_lock held.\nkernel/bpf/kthread.c-168- * Every object on the chain is alive: a thread clears waits_for under the lock\n--\nkernel/bpf/kthread.c-170- */\nkernel/bpf/kthread.c:171:static bool bpf_kthread_waits_for_current(struct bpf_kthread *kthread)\nkernel/bpf/kthread.c-172-{\nkernel/bpf/kthread.c:173:\tlockdep_assert_held(\u0026bpf_kthreads_lock);\nkernel/bpf/kthread.c-174-\tfor (; kthread; kthread = kthread-\u003ewaits_for)\n--\nkernel/bpf/kthread.c-184- */\nkernel/bpf/kthread.c:185:static int bpf_kthread_stop_and_free(struct bpf_kthread *kthread)\nkernel/bpf/kthread.c-186-{\n--\nkernel/bpf/kthread.c=200=void bpf_prog_free_kthreads(struct bpf_prog_aux *aux)\nkernel/bpf/kthread.c-201-{\nkernel/bpf/kthread.c:202:\tstruct bpf_kthread *kthread, *tmp;\nkernel/bpf/kthread.c-203-\tunsigned long irqflags;\n--\nkernel/bpf/kthread.c-214-\nkernel/bpf/kthread.c:215:\traw_spin_lock_irqsave(\u0026bpf_kthreads_lock, irqflags);\nkernel/bpf/kthread.c-216-\tlist_for_each_entry_safe(kthread, tmp, \u0026aux-\u003ekthreads, prog_node) {\n--\nkernel/bpf/kthread.c-219-\t}\nkernel/bpf/kthread.c:220:\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\nkernel/bpf/kthread.c-221-\nkernel/bpf/kthread.c-222-\tlist_for_each_entry_safe(kthread, tmp, \u0026kthreads, prog_node)\nkernel/bpf/kthread.c:223:\t\tbpf_kthread_stop_and_free(kthread);\nkernel/bpf/kthread.c-224-}\n--\nkernel/bpf/kthread.c=226=__bpf_kfunc_start_defs();\n--\nkernel/bpf/kthread.c-228-/**\nkernel/bpf/kthread.c:229: * bpf_kthread_create - Create a kernel thread that runs a BPF callback\nkernel/bpf/kthread.c-230- * @callback_fn: the thread's body, run once; its return ends the thread\n--\nkernel/bpf/kthread.c-238- * the comm that tracepoints and perf report as long as they have nine digits\nkernel/bpf/kthread.c:239: * between them, where /proc shows the full name in any case. bpf_kthread_start()\nkernel/bpf/kthread.c-240- * runs @callback_fn on it with @arg. The thread ends when the callback returns,\nkernel/bpf/kthread.c:241: * when bpf_kthread_stop() stops it, or with the last reference on the program\nkernel/bpf/kthread.c-242- * that created it. A thread whose callback has returned keeps its return value\nkernel/bpf/kthread.c:243: * until bpf_kthread_stop() collects it.\nkernel/bpf/kthread.c-244- *\n--\nkernel/bpf/kthread.c-251- */\nkernel/bpf/kthread.c:252:__bpf_kfunc struct task_struct *bpf_kthread_create(int (callback_fn)(u64 arg), u64 arg,\nkernel/bpf/kthread.c-253-\t\t\t\t\t\t   u64 flags, struct bpf_prog_aux *aux)\nkernel/bpf/kthread.c-254-{\nkernel/bpf/kthread.c:255:\tstruct bpf_kthread *kthread;\nkernel/bpf/kthread.c-256-\tunsigned long irqflags;\n--\nkernel/bpf/kthread.c-275-\tmigrate_enable();\nkernel/bpf/kthread.c:276:\ttask = kthread_create(bpf_kthread_fn, kthread, \"bpfk/%u:%u\", aux-\u003eid,\nkernel/bpf/kthread.c-277-\t\t\t      atomic_fetch_inc(\u0026aux-\u003ekthread_seq));\n--\nkernel/bpf/kthread.c-308-\t */\nkernel/bpf/kthread.c:309:\traw_spin_lock_irqsave(\u0026bpf_kthreads_lock, irqflags);\nkernel/bpf/kthread.c-310-\tprog = bpf_prog_inc_not_zero(aux-\u003eprog);\nkernel/bpf/kthread.c-311-\tif (IS_ERR(prog)) {\nkernel/bpf/kthread.c:312:\t\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\nkernel/bpf/kthread.c-313-\t\tmigrate_enable();\nkernel/bpf/kthread.c:314:\t\tbpf_kthread_stop_and_free(kthread);\nkernel/bpf/kthread.c-315-\t\tmigrate_disable();\n--\nkernel/bpf/kthread.c-318-\t}\nkernel/bpf/kthread.c:319:\tlist_add_tail(\u0026kthread-\u003enode, \u0026bpf_kthreads);\nkernel/bpf/kthread.c-320-\tlist_add_tail(\u0026kthread-\u003eprog_node, \u0026aux-\u003ekthreads);\nkernel/bpf/kthread.c:321:\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\nkernel/bpf/kthread.c-322-\tbpf_prog_put(prog);\n--\nkernel/bpf/kthread.c-326-/**\nkernel/bpf/kthread.c:327: * bpf_kthread_start - Run the callback of a BPF kthread\nkernel/bpf/kthread.c:328: * @task: a thread created by bpf_kthread_create()\nkernel/bpf/kthread.c-329- * @flags: must be 0\n--\nkernel/bpf/kthread.c-336- */\nkernel/bpf/kthread.c:337:__bpf_kfunc int bpf_kthread_start(struct task_struct *task, u64 flags)\nkernel/bpf/kthread.c-338-{\n--\nkernel/bpf/kthread.c-344-\nkernel/bpf/kthread.c:345:\traw_spin_lock_irqsave(\u0026bpf_kthreads_lock, irqflags);\nkernel/bpf/kthread.c:346:\tfound = !!bpf_kthread_find(task);\nkernel/bpf/kthread.c:347:\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\nkernel/bpf/kthread.c-348-\tif (!found)\n--\nkernel/bpf/kthread.c-355-/**\nkernel/bpf/kthread.c:356: * bpf_kthread_bind - Restrict a BPF kthread to one CPU\nkernel/bpf/kthread.c:357: * @task: a thread created by bpf_kthread_create()\nkernel/bpf/kthread.c-358- * @cpu: the CPU the thread is to run on\n--\nkernel/bpf/kthread.c-376- */\nkernel/bpf/kthread.c:377:__bpf_kfunc int bpf_kthread_bind(struct task_struct *task, u32 cpu, u64 flags)\nkernel/bpf/kthread.c-378-{\n--\nkernel/bpf/kthread.c-387-\nkernel/bpf/kthread.c:388:\traw_spin_lock_irqsave(\u0026bpf_kthreads_lock, irqflags);\nkernel/bpf/kthread.c:389:\tfound = !!bpf_kthread_find(task);\nkernel/bpf/kthread.c:390:\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\nkernel/bpf/kthread.c-391-\tif (!found)\n--\nkernel/bpf/kthread.c-402-/**\nkernel/bpf/kthread.c:403: * bpf_kthread_attach_cgroup - Move a BPF kthread into a cgroup\nkernel/bpf/kthread.c:404: * @task: a thread created by bpf_kthread_create()\nkernel/bpf/kthread.c-405- * @cgrp: the cgroup to move it into\n--\nkernel/bpf/kthread.c-417- * A cpuset applies its CPUs to the thread as it joins, so bind the\nkernel/bpf/kthread.c:418: * thread with bpf_kthread_bind() afterwards, not before. A running thread\nkernel/bpf/kthread.c-419- * joins a cpuset at its next park, and the move waits for that with\n--\nkernel/bpf/kthread.c-428- */\nkernel/bpf/kthread.c:429:__bpf_kfunc int bpf_kthread_attach_cgroup(struct task_struct *task, struct cgroup *cgrp,\nkernel/bpf/kthread.c-430-\t\t\t\t\t  u64 flags)\n--\nkernel/bpf/kthread.c-438-\nkernel/bpf/kthread.c:439:\traw_spin_lock_irqsave(\u0026bpf_kthreads_lock, irqflags);\nkernel/bpf/kthread.c:440:\tfound = !!bpf_kthread_find(task);\nkernel/bpf/kthread.c:441:\traw_spin_unlock_irqrestore(\u0026bpf_kthreads_lock, irqflags);\nkernel/bpf/kthread.c-442-\tif (!found)\n--\n\n"
}

5/2 2026/11/12 22:40 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 68111
							output: 37
							thoughts: 248
						

Thoughts:
**My Kernel Compilation Investigations**

Okay, let's dive into this. First, I need to verify which configurations control the compilation of `task_park.o` and `kthread.o` within the BPF subsystem. I'll start with a glance at `kernel/bpf/Makefile`. Ah, yes, there it is: `obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o task_park.o kthread.o`. So, it's definitively `CONFIG_BPF_SYSCALL` that governs their inclusion. Confirmed.

Next, I should account for cgroups since that's another common dependency with BPF. Let's see how `kthread.c` handles `CONFIG_CGROUPS`. I see it's guarding `bpf_kthread_attach_cgroup` with `#ifdef CONFIG_CGROUPS`. Makes sense. I'll need to remember that `cgroup_attach_kthread` is within `kernel/cgroup/cgroup.c` if I need to trace that as well.

Now, BTF, that's crucial for kfuncs. I recall it's involved in registering kfuncs or using them, and that's usually tied to `CONFIG_DEBUG_INFO_BTF`. I need to solidify that link; is it standard, or is another flag needed? Time to grep the codebase. I'll look for instances of `register_btf_kfunc_id_set` within `kernel/bpf/` to ascertain its connection to the configuration options.



6/2 2026/11/12 22:41 tool grepper
0m Args:
{
  "Expression": "register_btf_kfunc_id_set",
  "PathPrefixes": [
    "kernel/bpf/"
  ]
}

Results:
{
  "Output": "kernel/bpf/arena.c=1333=static int __init kfunc_init(void)\nkernel/bpf/arena.c-1334-{\nkernel/bpf/arena.c:1335:\treturn register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026common_kfunc_set);\nkernel/bpf/arena.c-1336-}\n--\nkernel/bpf/btf.c=9861=static int btf_populate_kfunc_set(struct btf *btf, enum btf_kfunc_hook hook,\n--\nkernel/bpf/btf.c-9907-\tset = tab-\u003esets[hook];\nkernel/bpf/btf.c:9908:\t/* Warn when register_btf_kfunc_id_set is called twice for the same hook\nkernel/bpf/btf.c-9909-\t * for module sets.\n--\nkernel/bpf/btf.c=10132=u32 *btf_kfunc_is_modify_return(const struct btf *btf, u32 kfunc_btf_id,\n--\nkernel/bpf/btf.c-10140-\nkernel/bpf/btf.c:10141:static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook,\nkernel/bpf/btf.c-10142-\t\t\t\t       const struct btf_kfunc_id_set *kset)\n--\nkernel/bpf/btf.c-10167-/* This function must be invoked only from initcalls/module init functions */\nkernel/bpf/btf.c:10168:int register_btf_kfunc_id_set(enum bpf_prog_type prog_type,\nkernel/bpf/btf.c-10169-\t\t\t      const struct btf_kfunc_id_set *kset)\n--\nkernel/bpf/btf.c-10181-\thook = bpf_prog_type_to_kfunc_hook(prog_type);\nkernel/bpf/btf.c:10182:\treturn __register_btf_kfunc_id_set(hook, kset);\nkernel/bpf/btf.c-10183-}\nkernel/bpf/btf.c:10184:EXPORT_SYMBOL_GPL(register_btf_kfunc_id_set);\nkernel/bpf/btf.c-10185-\n--\nkernel/bpf/btf.c=10187=int register_btf_fmodret_id_set(const struct btf_kfunc_id_set *kset)\nkernel/bpf/btf.c-10188-{\nkernel/bpf/btf.c:10189:\treturn __register_btf_kfunc_id_set(BTF_KFUNC_HOOK_FMODRET, kset);\nkernel/bpf/btf.c-10190-}\n--\nkernel/bpf/cpumask.c=515=static int __init cpumask_kfunc_init(void)\n--\nkernel/bpf/cpumask.c-525-\tret = bpf_mem_alloc_init(\u0026bpf_cpumask_ma, sizeof(struct bpf_cpumask), false);\nkernel/bpf/cpumask.c:526:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, \u0026cpumask_kfunc_set);\nkernel/bpf/cpumask.c:527:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, \u0026cpumask_kfunc_set);\nkernel/bpf/cpumask.c:528:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, \u0026cpumask_kfunc_set);\nkernel/bpf/cpumask.c-529-\treturn  ret ?: register_btf_id_dtor_kfuncs(cpumask_dtors,\n--\nkernel/bpf/crypto.c=340=static int __init crypto_kfunc_init(void)\n--\nkernel/bpf/crypto.c-349-\nkernel/bpf/crypto.c:350:\tret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, \u0026crypt_kfunc_set);\nkernel/bpf/crypto.c:351:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT, \u0026crypt_kfunc_set);\nkernel/bpf/crypto.c:352:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, \u0026crypt_kfunc_set);\nkernel/bpf/crypto.c:353:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,\nkernel/bpf/crypto.c-354-\t\t\t\t\t       \u0026crypt_init_kfunc_set);\n--\nkernel/bpf/helpers.c=5394=static int __init kfunc_init(void)\n--\nkernel/bpf/helpers.c-5409-\nkernel/bpf/helpers.c:5410:\tret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5411:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, \u0026user_mem_kfunc_set);\nkernel/bpf/helpers.c:5412:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5413:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5414:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5415:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c:5416:\tret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, \u0026generic_kfunc_set);\nkernel/bpf/helpers.c-5417-\tret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors,\n--\nkernel/bpf/helpers.c-5419-\t\t\t\t\t\t  THIS_MODULE);\nkernel/bpf/helpers.c:5420:\treturn ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026common_kfunc_set);\nkernel/bpf/helpers.c-5421-}\n--\nkernel/bpf/kthread.c=568=static int __init bpf_kthread_kfunc_init(void)\nkernel/bpf/kthread.c-569-{\nkernel/bpf/kthread.c:570:\treturn register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, \u0026bpf_kthread_kfunc_set);\nkernel/bpf/kthread.c-571-}\n--\nkernel/bpf/map_iter.c=271=static int init_subsystem(void)\nkernel/bpf/map_iter.c-272-{\nkernel/bpf/map_iter.c:273:\treturn register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026bpf_map_iter_kfunc_set);\nkernel/bpf/map_iter.c-274-}\n--\nkernel/bpf/rqspinlock.c=759=static __init int rqspinlock_register_kfuncs(void)\nkernel/bpf/rqspinlock.c-760-{\nkernel/bpf/rqspinlock.c:761:\treturn register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026rqspinlock_kfunc_set);\nkernel/bpf/rqspinlock.c-762-}\n--\nkernel/bpf/task_park.c=304=static int __init bpf_task_park_kfunc_init(void)\n--\nkernel/bpf/task_park.c-307-\nkernel/bpf/task_park.c:308:\tret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, \u0026bpf_task_park_kfunc_set);\nkernel/bpf/task_park.c:309:\treturn ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, \u0026bpf_task_wake_kfunc_set);\nkernel/bpf/task_park.c-310-}\n"
}

7/2 2026/11/12 22:41 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 71142
							output: 42
							thoughts: 14
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience and presented in the first person:

**Checking Kconfig for BPF_SYSCALL**

Alright, let's get down to brass tacks. I need to make sure `BPF_SYSCALL` is properly configured, or at least understand its state within the kernel configuration. It's a fundamental piece for BPF syscall hooking, and I need to confirm its availability or potential dependencies. So, I'm diving into the Kconfig files. Time to see if it's enabled, disabled, or maybe hidden behind some other feature flag. This is the starting point for any in-depth analysis or modification related to BPF syscall usage, so let's get this initial check out of the way.



8/2 2026/11/12 22:42 tool grepper
0m Args:
{
  "Expression": "config BPF_SYSCALL",
  "PathPrefixes": [
    "init/Kconfig",
    "kernel/bpf/Kconfig"
  ]
}

Results:
{
  "Output": "kernel/bpf/Kconfig=34=menu \"BPF subsystem\"\nkernel/bpf/Kconfig-35-\nkernel/bpf/Kconfig:36:config BPF_SYSCALL\nkernel/bpf/Kconfig-37-\tbool \"Enable bpf() system call\"\n"
}

9/2 2026/11/12 22:42 llm patch-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 71447
							output: 34
							thoughts: 19
						

Thoughts:
Here's my take on those thoughts, tailored for an expert audience:

**Diving into the BPF Configuration**

Okay, so I'm thinking, let's zero in on the `kernel/bpf/Kconfig` file. I'm specifically targeting around line 36. My gut feeling is that something interesting, likely related to a core BPF feature or perhaps a conditional compilation setting, is in that vicinity. Given the depth of the BPF subsystem, a quick scan of that area might give me a key to unlock the current mental puzzle I'm grappling with in the wider context. This is likely due to my deep understanding that the `Kconfig` files are usually where the meat of the configuration options, which in turn reflect the underlying design and dependencies, are documented and defined. Therefore, that specific area might shed some light on the issue I am trying to understand.



10/2 2026/11/12 22:43 tool read-file
0m Args:
{
  "File": "kernel/bpf/Kconfig",
  "FirstLine": 34,
  "LineCount": 40
}

Results:
{
  "Contents": "  34:\tmenu \"BPF subsystem\"\n  35:\t\n  36:\tconfig BPF_SYSCALL\n  37:\t\tbool \"Enable bpf() system call\"\n  38:\t\tselect BPF\n  39:\t\tselect IRQ_WORK\n  40:\t\tselect NEED_TASKS_RCU\n  41:\t\tselect TASKS_TRACE_RCU\n  42:\t\tselect BINARY_PRINTF\n  43:\t\tselect NET_SOCK_MSG if NET\n  44:\t\tselect NET_XGRESS if NET\n  45:\t\tselect PAGE_POOL if NET\n  46:\t\tdefault n\n  47:\t\thelp\n  48:\t\t  Enable the bpf() system call that allows to manipulate BPF programs\n  49:\t\t  and maps via file descriptors.\n  50:\t\n  51:\tconfig BPF_JIT\n  52:\t\tbool \"Enable BPF Just In Time compiler\"\n  53:\t\tdepends on BPF\n  54:\t\tdepends on HAVE_CBPF_JIT || HAVE_EBPF_JIT\n  55:\t\tselect EXECMEM\n  56:\t\thelp\n  57:\t\t  BPF programs are normally handled by a BPF interpreter. This option\n  58:\t\t  allows the kernel to generate native code when a program is loaded\n  59:\t\t  into the kernel. This will significantly speed-up processing of BPF\n  60:\t\t  programs.\n  61:\t\n  62:\t\t  Note, an admin should enable this feature changing:\n  63:\t\t  /proc/sys/net/core/bpf_jit_enable\n  64:\t\t  /proc/sys/net/core/bpf_jit_harden   (optional)\n  65:\t\t  /proc/sys/net/core/bpf_jit_kallsyms (optional)\n  66:\t\n  67:\tconfig BPF_JIT_ALWAYS_ON\n  68:\t\tbool \"Permanently enable BPF JIT and remove BPF interpreter\" if !ARCH_WANT_BPF_JIT_ALWAYS_ON\n  69:\t\tdefault ARCH_WANT_BPF_JIT_ALWAYS_ON\n  70:\t\tdepends on BPF_SYSCALL \u0026\u0026 HAVE_EBPF_JIT \u0026\u0026 BPF_JIT\n  71:\t\thelp\n  72:\t\t  Enables BPF JIT and removes BPF interpreter to avoid speculative\n  73:\t\t  execution of BPF instructions by the interpreter.\n"
}

11/2 2026/11/12 22:43 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 72301
							output: 41
							thoughts: 25
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience:

**Investigating BTF Dependencies**

Alright, I'm just running through my mental checklist on this BTF situation. I know we've already looked at the obvious stuff, but I want to make *absolutely* sure we haven't missed anything crucial. My gut feeling is something isn't quite right. Given the reliance on BTF for BTF ID sets, I need to double-check `DEBUG_INFO_BTF` in `Kconfig`. It's easy to overlook a dependency like that, and a misconfiguration there could explain the odd behavior we're seeing. It's a fundamental piece of the puzzle, and a quick double-check always helps prevent a wild goose chase later.



12/2 2026/11/12 22:44 tool grepper
0m Args:
{
  "Expression": "DEBUG_INFO_BTF",
  "PathPrefixes": [
    "kernel/bpf/",
    "lib/Kconfig.debug"
  ]
}

Results:
{
  "Output": "kernel/bpf/Makefile=43=ifeq ($(CONFIG_SYSFS),y)\nkernel/bpf/Makefile:44:obj-$(CONFIG_DEBUG_INFO_BTF) += sysfs_btf.o\nkernel/bpf/Makefile:45:obj-$(CONFIG_DEBUG_INFO_BTF_INLINE) += btf_vmlinux_inline.o\nkernel/bpf/Makefile-46-endif\n--\nkernel/bpf/btf.c=6865=static struct btf *btf_parse_base(struct btf_verifier_env *env, const char *name,\n--\nkernel/bpf/btf.c-6870-\nkernel/bpf/btf.c:6871:\tif (!IS_ENABLED(CONFIG_DEBUG_INFO_BTF))\nkernel/bpf/btf.c-6872-\t\treturn ERR_PTR(-ENOENT);\n--\nkernel/bpf/btf.c=6951=__u32 btf_relocate_id(const struct btf *btf, __u32 id)\n--\nkernel/bpf/btf.c-6957-\nkernel/bpf/btf.c:6958:#ifdef CONFIG_DEBUG_INFO_BTF_MODULES\nkernel/bpf/btf.c-6959-\n--\nkernel/bpf/btf.c=7076=static struct btf *btf_parse_module(const char *module_name, const void *data,\n--\nkernel/bpf/btf.c-7178-\nkernel/bpf/btf.c:7179:#endif /* CONFIG_DEBUG_INFO_BTF_MODULES */\nkernel/bpf/btf.c-7180-\n--\nkernel/bpf/btf.c=9305=enum {\n--\nkernel/bpf/btf.c-9308-\nkernel/bpf/btf.c:9309:#ifdef CONFIG_DEBUG_INFO_BTF_MODULES\nkernel/bpf/btf.c-9310-struct btf_module {\n--\nkernel/bpf/btf.c=9323=static void purge_cand_cache(struct btf *btf);\nkernel/bpf/btf.c-9324-\nkernel/bpf/btf.c:9325:#if IS_MODULE(CONFIG_DEBUG_INFO_BTF_INLINE)\nkernel/bpf/btf.c-9326-extern char __start_BTF_inline_link[];\n--\nkernel/bpf/btf.c=9423=static void btf_vmlinux_inline_lazy_init(void)\n--\nkernel/bpf/btf.c-9426-\nkernel/bpf/btf.c:9427:#endif /* IS_MODULE(CONFIG_DEBUG_INFO_BTF_INLINE) */\nkernel/bpf/btf.c-9428-\nkernel/bpf/btf.c=9429=static int btf_module_notify(struct notifier_block *nb, unsigned long op,\n--\nkernel/bpf/btf.c-9450-\nkernel/bpf/btf.c:9451:#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF_INLINE)\nkernel/bpf/btf.c-9452-\t\tif (mod-\u003ebtf_inline_data_size) {\n--\nkernel/bpf/btf.c=9574=fs_initcall(btf_module_init);\nkernel/bpf/btf.c:9575:#endif /* CONFIG_DEBUG_INFO_BTF_MODULES */\nkernel/bpf/btf.c-9576-\nkernel/bpf/btf.c=9577=struct module *btf_try_get_module(const struct btf *btf)\n--\nkernel/bpf/btf.c-9579-\tstruct module *res = NULL;\nkernel/bpf/btf.c:9580:#ifdef CONFIG_DEBUG_INFO_BTF_MODULES\nkernel/bpf/btf.c-9581-\tstruct btf_module *btf_mod, *tmp;\n--\nkernel/bpf/btf.c=9607=struct btf *btf_get_module_btf(const struct module *module)\nkernel/bpf/btf.c-9608-{\nkernel/bpf/btf.c:9609:#ifdef CONFIG_DEBUG_INFO_BTF_MODULES\nkernel/bpf/btf.c-9610-\tstruct btf_module *btf_mod, *tmp;\n--\nkernel/bpf/btf.c-9620-\nkernel/bpf/btf.c:9621:#ifdef CONFIG_DEBUG_INFO_BTF_MODULES\nkernel/bpf/btf.c-9622-\tmutex_lock(\u0026btf_module_mutex);\n--\nkernel/bpf/btf.c=9637=static int check_btf_kconfigs(const struct module *module, const char *feature)\nkernel/bpf/btf.c-9638-{\nkernel/bpf/btf.c:9639:\tif (!module \u0026\u0026 IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) {\nkernel/bpf/btf.c-9640-\t\tpr_err(\"missing vmlinux BTF, cannot register %s\\n\", feature);\n--\nkernel/bpf/btf.c-9642-\t}\nkernel/bpf/btf.c:9643:\tif (module \u0026\u0026 IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))\nkernel/bpf/btf.c-9644-\t\tpr_warn(\"missing module BTF, cannot register %s\\n\", feature);\n--\nkernel/bpf/btf.c=9795=static int btf_check_kfunc_name(struct btf *btf, const char *func_name, u32 kind)\nkernel/bpf/btf.c-9796-{\nkernel/bpf/btf.c:9797:#ifdef CONFIG_DEBUG_INFO_BTF_MODULES\nkernel/bpf/btf.c-9798-\tstruct btf_module *btf_mod, *tmp;\n--\nkernel/bpf/btf.c-9811-\nkernel/bpf/btf.c:9812:#ifdef CONFIG_DEBUG_INFO_BTF_MODULES\nkernel/bpf/btf.c-9813-\tguard(mutex)(\u0026btf_module_mutex);\n--\nkernel/bpf/btf.c=10461=static struct bpf_cand_cache *populate_cand_cache(struct bpf_cand_cache *cands,\n--\nkernel/bpf/btf.c-10488-\nkernel/bpf/btf.c:10489:#ifdef CONFIG_DEBUG_INFO_BTF_MODULES\nkernel/bpf/btf.c-10490-static void __purge_cand_cache(struct btf *btf, struct bpf_cand_cache **cache,\n--\nkernel/bpf/btf_vmlinux_inline.c-10-\nkernel/bpf/btf_vmlinux_inline.c:11:#if IS_BUILTIN(CONFIG_DEBUG_INFO_BTF_INLINE)\nkernel/bpf/btf_vmlinux_inline.c-12-extern char __start_BTF_inline[];\n--\nkernel/bpf/btf_vmlinux_inline.c=16=static int __init btf_vmlinux_inline_init(void)\nkernel/bpf/btf_vmlinux_inline.c-17-{\nkernel/bpf/btf_vmlinux_inline.c:18:#if IS_BUILTIN(CONFIG_DEBUG_INFO_BTF_INLINE)\nkernel/bpf/btf_vmlinux_inline.c-19-\tsize_t data_size = __stop_BTF_inline - __start_BTF_inline;\n--\nkernel/bpf/verifier.c=2967=int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset)\n--\nkernel/bpf/verifier.c-2983-\t\tif (!btf_vmlinux) {\nkernel/bpf/verifier.c:2984:\t\t\tverbose(env, \"calling kernel function is not supported without CONFIG_DEBUG_INFO_BTF\\n\");\nkernel/bpf/verifier.c-2985-\t\t\treturn -ENOTSUPP;\n--\nkernel/bpf/verifier.c=7007=static int check_ptr_to_map_access(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-7021-\tif (!btf_vmlinux) {\nkernel/bpf/verifier.c:7022:\t\tverbose(env, \"map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\\n\");\nkernel/bpf/verifier.c-7023-\t\treturn -ENOTSUPP;\n--\nkernel/bpf/verifier.c=22508=static int check_pseudo_btf_id(struct bpf_verifier_env *env,\n--\nkernel/bpf/verifier.c-22524-\t\tif (!btf_vmlinux) {\nkernel/bpf/verifier.c:22525:\t\t\tverbose(env, \"kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\\n\");\nkernel/bpf/verifier.c-22526-\t\t\treturn -EINVAL;\n--\nkernel/bpf/verifier.c=24907=struct btf *bpf_get_btf_vmlinux(void)\n--\nkernel/bpf/verifier.c-24911-\nkernel/bpf/verifier.c:24912:\tif (!btf \u0026\u0026 IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) {\nkernel/bpf/verifier.c-24913-\t\tmutex_lock(\u0026btf_vmlinux_lock);\n--\nlib/Kconfig.debug=378=config DEBUG_INFO_SPLIT\n--\nlib/Kconfig.debug-397-\nlib/Kconfig.debug:398:config DEBUG_INFO_BTF\nlib/Kconfig.debug-399-\tbool \"Generate BTF type information\"\n--\nlib/Kconfig.debug=419=config PAHOLE_HAS_LANG_EXCLUDE\n--\nlib/Kconfig.debug-425-\t  otherwise it would emit malformed kernel and module binaries when\nlib/Kconfig.debug:426:\t  using DEBUG_INFO_BTF_MODULES.\nlib/Kconfig.debug-427-\nlib/Kconfig.debug=428=config PAHOLE_HAS_INLINE\n--\nlib/Kconfig.debug-433-\nlib/Kconfig.debug:434:config DEBUG_INFO_BTF_MODULES\nlib/Kconfig.debug-435-\tbool \"Generate BTF type information for kernel modules\"\nlib/Kconfig.debug-436-\tdefault y\nlib/Kconfig.debug:437:\tdepends on DEBUG_INFO_BTF \u0026\u0026 MODULES\nlib/Kconfig.debug-438-\thelp\n--\nlib/Kconfig.debug-440-\nlib/Kconfig.debug:441:config DEBUG_INFO_BTF_INLINE\nlib/Kconfig.debug-442-\ttristate \"Provide information about inline sites in BTF\"\nlib/Kconfig.debug-443-\tdefault n\nlib/Kconfig.debug:444:\tdepends on DEBUG_INFO_BTF \u0026\u0026 PAHOLE_HAS_INLINE \u0026\u0026 SYSFS\nlib/Kconfig.debug:445:\tselect DEBUG_INFO_BTF_MODULES if MODULES\nlib/Kconfig.debug-446-\thelp\n--\nlib/Kconfig.debug=453=config MODULE_ALLOW_BTF_MISMATCH\nlib/Kconfig.debug-454-\tbool \"Allow loading modules with non-matching BTF type info\"\nlib/Kconfig.debug:455:\tdepends on DEBUG_INFO_BTF_MODULES\nlib/Kconfig.debug-456-\thelp\n"
}

13/2 2026/11/12 22:44 llm patch-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 75632
							output: 31
							thoughts: 1356
						

Thoughts:
**Analyzing BPF Kfunc and Kthread Integration for Fuzzing Focus**

Okay, so I need to figure out the right symbols to focus on for fuzzing this patch, keeping in mind the kernel's fuzzing guidelines. First, I see that this patch is all about integrating BPF with kernel threads and the task park mechanism.  I know that using kernel functions (kfuncs) in BPF requires `DEBUG_INFO_BTF`, `BPF_SYSCALL`, and `CGROUPS` to be enabled, which isn't news, but worth remembering.

Alright, let's break down the functions modified and added. I see several functions related to task parking and waking: `bpf_task_wake_deferred`, `bpf_task_wake_defer`, `bpf_task_park_interrupted`, `bpf_task_park`, and `bpf_task_wake` in `kernel/bpf/task_park.c`.  Then there's a whole suite of kthread-related functions in `kernel/bpf/kthread.c`: `bpf_kthread_fn`, `bpf_kthread_find`, `bpf_kthread_unlink`, `bpf_kthread_self`, `bpf_kthread_waits_for_current`, `bpf_kthread_stop_and_free`, `bpf_prog_free_kthreads`, `bpf_kthread_create`, `bpf_kthread_start`, `bpf_kthread_bind`, `bpf_kthread_attach_cgroup`, `bpf_kthread_stop`, and `bpf_kthread_should_stop`.  `cgroup_attach_kthread` is also present. The verifier has several updates, too, including `in_callback_frames`, `check_park_stack_objects`, `invalidate_regs_across_park`, `set_kthread_callback_state`, `is_bpf_task_park_kfunc`, `is_migrating_kfunc`, `bpf_is_task_park_kfunc`, and `is_bpf_kthread_create_kfunc`. And then the syscall functions: `bpf_prog_put_deferred`, `__bpf_prog_put`, and `kern_sys_bpf`. Finally, in `cfg.c`, there are `mark_subprog_might_park` and `merge_callee_effects`.

Now, according to the fuzzing guidelines, I need to avoid ubiquitous functions and instead focus on the core logic and entry points.  I have to be very careful to exclude things like memory allocators, core sync primitives, etc.  So, I need to look at what's specific to this new functionality and what's essentially infrastructure.

Based on that, I'm thinking the following functions are likely candidates for the `FocusSymbols`: `bpf_task_park`, `bpf_task_wake`, `bpf_kthread_create`, `bpf_kthread_start`, `bpf_kthread_bind`, `bpf_kthread_attach_cgroup`, `bpf_kthread_stop`, `bpf_kthread_should_stop`, `bpf_prog_free_kthreads`, and `cgroup_attach_kthread`. These seem like the key entry points and core logic for the new kthread and task park integration.

Next, I need to check whether these are *visible* symbols or static/inlined, because that is important for fuzzing. Looking at `kernel/bpf/kthread.c` I see that `__bpf_kfunc struct task_struct *bpf_kthread_create(...)`, `__bpf_kfunc int bpf_kthread_start(...)`, `__bpf_kfunc int bpf_kthread_bind(...)`, `__bpf_kfunc int bpf_kthread_attach_cgroup(...)`, `__bpf_kfunc int bpf_kthread_stop(...)`, and `__bpf_kfunc bool bpf_kthread_should_stop(...)` are *not* static; they are kfuncs.  `void bpf_prog_free_kthreads(struct bpf_prog_aux *aux)` is also visible. However, `int bpf_kthread_fn(void *data)` is static, so I will need to think about how to tackle that if relevant. In `kernel/bpf/task_park.c` I have `__bpf_kfunc int bpf_task_park(...)` and `__bpf_kfunc int bpf_task_wake(...)`. `int


14/2 2026/11/12 22:45 tool grepper
0m Args:
{
  "Expression": "#define __bpf_kfunc",
  "PathPrefixes": [
    "include/"
  ]
}

Results:
{
  "Output": "include/linux/btf.h-92- */\ninclude/linux/btf.h:93:#define __bpf_kfunc __used __retain __noclone noinline\ninclude/linux/btf.h-94-\ninclude/linux/btf.h:95:#define __bpf_kfunc_start_defs()\t\t\t\t\t       \\\ninclude/linux/btf.h-96-\t__diag_push();\t\t\t\t\t\t\t       \\\n--\ninclude/linux/btf.h-101-\ninclude/linux/btf.h:102:#define __bpf_kfunc_end_defs() __diag_pop()\ninclude/linux/btf.h-103-#define __bpf_hook_start() __bpf_kfunc_start_defs()\n"
}

15/2 2026/11/12 22:45 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 78244
							output: 277
							thoughts: 1076
						

Thoughts:
**Triage: BPF Kthread Implementation - Worthy of Fuzzing**

Okay, so looking at this kthread BPF implementation, I see a clear and distinct set of kfuncs thanks to the `__bpf_kfunc __used __retain __noclone noinline` attributes. That means `bpf_task_park`, `bpf_task_wake`, `bpf_kthread_create`, `bpf_kthread_start`, `bpf_kthread_bind`, `bpf_kthread_attach_cgroup`, `bpf_kthread_stop`, `bpf_kthread_should_stop`, `bpf_prog_free_kthreads`, and `cgroup_attach_kthread` are all unique symbols in vmlinux, not inlined, and readily available for analysis.

I also need to check the configurations. The conditional compilation of `bpf_kthread_attach_cgroup` under `CONFIG_CGROUPS` is important. Without it, we get a stub, a no-op that returns -ENODEV. So, `CGROUPS` is definitely a key config here, alongside `BPF_SYSCALL` for `task_park.o` and `kthread.o` to allow BPF syscall programs to be built and `DEBUG_INFO_BTF` is essential for BTF and the kfuncs themselves.

Now, let's break down why this is ripe for fuzzing. This patch introduces significant complexity. It touches core subsystems: memory management, the scheduler, synchronization primitives, BPF, and cgroups. We're talking about intricate synchronization mechanisms, life cycle management, and potential race conditions. There's thread creation, parking, waking, stopping, binding, task migration across sleeps/waits, deadlock detection, deferred wakes, verifier state tracking with concepts like `in_kthread_fn`, `parked`, and register invalidation across parks, plus destruction handling when programs unload.

This functionality is accessible from userspace via `sys_bpf` system calls – `BPF_PROG_LOAD`, `BPF_PROG_RUN`, `BPF_PROG_TEST_RUN`, all that. Given the complexity, the direct userland interface, and the potential for subtle synchronization issues, this is absolutely worth fuzzing. The patch is complex. The synchronization and memory management are complex. There are many touch points with userland.

So, for `set-results`, I'd set `WorthFuzzing` to `true`, and `EnableConfigs` to `["BPF_SYSCALL", "DEBUG_INFO_BTF", "CGROUPS"]`. Then, `FocusSymbols` should be set to: `["bpf_task_park", "bpf_task_wake", "bpf_kthread_create", "bpf_kthread_start", "bpf_kthread_bind", "bpf_kthread_attach_cgroup", "bpf_kthread_stop", "bpf_kthread_should_stop", "bpf_prog_free_kthreads", "cgroup_attach_kthread"]`. Finally, the `Reasoning` should succinctly summarize the patch's complexity, core subsystem involvement, and userland accessibility. I'm confident in calling `set-results` with these values.



16/2 2026/11/12 22:45 tool set-results
0m Args:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "DEBUG_INFO_BTF",
    "CGROUPS"
  ],
  "FocusSymbols": [
    "bpf_task_park",
    "bpf_task_wake",
    "bpf_kthread_create",
    "bpf_kthread_start",
    "bpf_kthread_bind",
    "bpf_kthread_attach_cgroup",
    "bpf_kthread_stop",
    "bpf_kthread_should_stop",
    "bpf_prog_free_kthreads",
    "cgroup_attach_kthread"
  ],
  "Reasoning": "The patch introduces BPF kthreads and task parking mechanisms (bpf_task_park, bpf_task_wake, and bpf_kthread_* kfuncs), along with cgroup kthread attachment and significant verifier extensions for tracking migrating kfuncs, register/stack object invalidation across sleeps, and lifecycle teardown. These changes touch core BPF, scheduler, and cgroup interactions, and are fully reachable from userspace via the BPF syscall. Fuzzing is warranted to test for concurrency issues, lifetime/teardown races, and verifier soundness regressions.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "BPF_SYSCALL",
    "DEBUG_INFO_BTF",
    "CGROUPS"
  ],
  "FocusSymbols": [
    "bpf_task_park",
    "bpf_task_wake",
    "bpf_kthread_create",
    "bpf_kthread_start",
    "bpf_kthread_bind",
    "bpf_kthread_attach_cgroup",
    "bpf_kthread_stop",
    "bpf_kthread_should_stop",
    "bpf_prog_free_kthreads",
    "cgroup_attach_kthread"
  ],
  "Reasoning": "The patch introduces BPF kthreads and task parking mechanisms (bpf_task_park, bpf_task_wake, and bpf_kthread_* kfuncs), along with cgroup kthread attachment and significant verifier extensions for tracking migrating kfuncs, register/stack object invalidation across sleeps, and lifecycle teardown. These changes touch core BPF, scheduler, and cgroup interactions, and are fully reachable from userspace via the BPF syscall. Fuzzing is warranted to test for concurrency issues, lifetime/teardown races, and verifier soundness regressions.",
  "WorthFuzzing": true
}

17/1 2026/11/12 22:45 action If
4m Args:
{
  "WorthFuzzing": true
}

Results:
null

18/2 2026/11/12 22:45 agent kmsan-evaluator
4m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series introduces BPF kthreads (`bpf_kthread_create`, `bpf_kthread_start`, `bpf_kthread_bind`, `bpf_kthread_attach_cgroup`, `bpf_kthread_stop`) and task park/wake mechanisms (`bpf_task_park`, `bpf_task_wake`), alongside verifier enhancements to track sleepable, parkable, and migrating subprograms and invalidate borrowed references across park calls.\n\nKMSAN vs KASAN Applicability:\n1. Uninitialized memory / info-leak analysis:\n   - No kernel data structures or buffers are copied to user space (no `copy_to_user`, `put_user`, socket options, netlink attributes, or ioctl output structs).\n   - All newly introduced heap objects (e.g. `struct bpf_kthread` via `kzalloc_obj`) and struct fields (e.g. `bpf_wake_pending` and `bpf_wake_node` in `struct task_struct`, initialized in `dup_task_struct()`; `kthreads` and `kthread_seq` in `struct bpf_prog_aux`, allocated with `GFP_ZERO`; verifier state flags `parked`, `in_kthread_fn`, `might_park` in zero-allocated structures) are fully initialized before use.\n   - All local variables and branching conditions across `task_park.c`, `kthread.c`, `cgroup.c`, and `verifier.c` are properly initialized. There is no risk of uninitialized memory reads or information leakage to userspace.\n\n2. Memory safety and concurrency risks (KASAN / LOCKDEP):\n   - The primary risks of this patch involve task and program lifetime management, reference counting (`get_task_struct`, `put_task_struct`, `bpf_prog_inc_not_zero`, `kthread_stop_put`), race conditions between parking and waking, and synchronization during kthread shutdown or migration. These risks (such as Use-After-Free, double frees, or invalid dereferences) are directly covered by KASAN.\n   - Potential deadlocks (such as cyclic stopping chains or interactions between raw spinlocks, cgroup mutexes, and scheduler wait states) are covered by LOCKDEP.\n\nBecause there are no uninitialized memory risks or kernel info-leaks, a dedicated KMSAN fuzzing session is not justified.",
  "NeedsKMSAN": false
}

Instruction:
You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.

CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
  a comprehensive suite of debugging tools and sanitizers, including KASAN
  (out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
  (locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
  or page allocations) and kernel-to-user memory info-leaks.

Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.

Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.

Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
   attributes, ioctl output arguments, socket options, or BPF buffers) where fields
   or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
   or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
   could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
   uninitialized bytes of existing buffers.

Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
  dereferences, locking deadlocks, or use-after-free bugs (these are already caught
  by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
  or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.

Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 3f9f9198c5e11a731f7218111346acc0acb7780f
Author: syz-cluster <triage@syzkaller.com>
Date:   Thu Nov 12 22:39:54 2026 +0000

    syz-cluster: applied patch under review

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 520fd72e21fd07..7c15593cb18880 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -1943,6 +1943,8 @@ struct bpf_prog_aux {
 	bool changes_pkt_data;
 	bool has_prologue;
 	bool might_sleep;
+	/* a run may leave on another CPU than it started on, see bpf_task_park() */
+	bool may_migrate;
 	bool can_unwind;
 	/* frames of recursive subprogs can be on the stack when it is called */
 	bool in_recursion;
@@ -2044,6 +2046,8 @@ struct bpf_prog_aux {
 	struct bpf_stream *stream[2];
 	spinlock_t st_ops_assoc_lock; /* protects st_ops_assoc */
 	struct bpf_map __rcu *st_ops_assoc;
+	struct list_head kthreads; /* BPF kthreads of the prog, see kernel/bpf/kthread.c */
+	atomic_t kthread_seq; /* index of the prog's next BPF kthread, in its name */
 };
 
 #define BPF_NR_CONTEXTS        4       /* normal, softirq, hardirq, NMI */
@@ -2968,6 +2972,7 @@ struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
 void bpf_prog_put(struct bpf_prog *prog);
 
 void bpf_prog_free_id(struct bpf_prog *prog);
+void bpf_prog_free_kthreads(struct bpf_prog_aux *aux);
 void bpf_map_free_id(struct bpf_map *map);
 
 struct btf_field *btf_record_find(const struct btf_record *rec,
diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
index 4771c55d8f41f6..7097bb7033e8fc 100644
--- a/include/linux/bpf_verifier.h
+++ b/include/linux/bpf_verifier.h
@@ -469,6 +469,12 @@ struct bpf_func_state {
 	bool in_callback_fn;
 	bool in_async_callback_fn;
 	bool in_exception_callback_fn;
+	/*
+	 * The body of a BPF kthread: an async callback whose kernel caller,
+	 * bpf_kthread_fn(), keeps nothing across it that the sleep in
+	 * bpf_task_park() could invalidate, so the park is allowed in it.
+	 */
+	bool in_kthread_fn;
 	bool no_stack_arg_load;
 	/*
 	 * The helper invokes this callback under rcu_read_lock(), so the
@@ -559,6 +565,12 @@ struct bpf_verifier_state {
 	 * and their packet pointers are not in the state, see bpf_flow_return().
 	 */
 	bool changed_pkt_data;
+	/*
+	 * A function that is verified on its own parked the task. Its callers,
+	 * what the sleep takes from them and whether one of them is a callback,
+	 * are not in the state, see bpf_flow_return().
+	 */
+	bool parked;
 	/*
 	 * Not zero in a function that is verified on its own: the call that the
 	 * state at its entry came from, see bpf_flow_call().
@@ -1056,6 +1068,8 @@ struct bpf_subprog_info {
 	bool keep_fastcall_stack: 1;
 	bool changes_pkt_data: 1;
 	bool might_sleep: 1;
+	/* some call reachable from this subprog may be bpf_task_park() */
+	bool might_park: 1;
 	bool uses_arena_sp: 1;
 	/* a callback that takes a stack in arena, like the main program does */
 	bool arena_stack_entry: 1;
@@ -2033,6 +2047,7 @@ static inline bool bpf_is_kfunc_sleepable(struct bpf_call_arg_meta *meta)
 }
 bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta);
 bool bpf_is_cast_kfunc(const struct bpf_call_arg_meta *meta);
+bool bpf_is_task_park_kfunc(struct bpf_call_arg_meta *meta);
 struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem);
 int bpf_copy_insn_array_uniq(struct bpf_map *map, u32 start, u32 end, u32 *off);
 bool bpf_insn_is_cond_jump(u8 code);
diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 2afb4cb2bb4fa4..f649973801f4c0 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -158,6 +158,7 @@ struct cgroup *cgroup_v1v2_get_from_fd(int fd);
 bool cgroup1_ssid_disabled(int ssid);
 
 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
+int cgroup_attach_kthread(struct cgroup *dst_cgrp, struct task_struct *task);
 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
 
 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
@@ -804,6 +805,8 @@ static inline void cgroup_lock(void) {}
 static inline void cgroup_unlock(void) {}
 static inline int cgroup_attach_task_all(struct task_struct *from,
 					 struct task_struct *t) { return 0; }
+static inline int cgroup_attach_kthread(struct cgroup *dst_cgrp,
+					struct task_struct *task) { return -ENODEV; }
 static inline int cgroupstats_build(struct cgroupstats *stats,
 				    struct dentry *dentry) { return -EINVAL; }
 
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 61448be53f8da6..8a99b9aacfb057 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -883,6 +883,13 @@ static __always_inline u32 bpf_prog_run(const struct bpf_prog *prog, const void
  * This uses migrate_disable/enable() explicitly to document that the
  * invocation of a BPF program does not require reentrancy protection
  * against a BPF program which is invoked from a preempting task.
+ *
+ * A syscall program may call bpf_task_park(), or a BPF kthread kfunc that
+ * waits for another task, which lift the pin for the duration of the sleep
+ * or wait and restore it before returning to the program, so such a program
+ * may resume on another CPU. Nothing on this path keeps per-CPU state
+ * across the program: __bpf_prog_run() samples its stats only after the
+ * program returns.
  */
 static inline u32 bpf_prog_run_pin_on_cpu(const struct bpf_prog *prog,
 					  const void *ctx)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 1c93da75226e32..0fdd3917443602 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1630,6 +1630,10 @@ struct task_struct {
 	struct bpf_local_storage __rcu	*bpf_storage;
 	/* Used for BPF run context */
 	struct bpf_run_ctx		*bpf_ctx;
+	/* Set by bpf_task_wake(), consumed by bpf_task_park() */
+	int				bpf_wake_pending;
+	/* On a CPU's queue of deferred bpf_task_wake() calls when not NULL */
+	struct wake_q_node		bpf_wake_node;
 #endif
 	/* Used by BPF for per-TASK xdp storage */
 	struct bpf_net_context		*bpf_net_context;
diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile
index d26c62326e588f..a754fbefae39a9 100644
--- a/kernel/bpf/Makefile
+++ b/kernel/bpf/Makefile
@@ -15,7 +15,7 @@ obj-$(CONFIG_BPF_SYSCALL) += fixups.o cfg.o states.o check_btf.o flow.o join.o
 obj-${CONFIG_BPF_LSM}	  += bpf_inode_storage.o
 obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o
 obj-$(CONFIG_BPF_JIT) += trampoline.o
-obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o
+obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o task_park.o kthread.o
 ifeq ($(CONFIG_MMU)$(CONFIG_64BIT),yy)
 obj-$(CONFIG_BPF_SYSCALL) += arena.o range_tree.o
 endif
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c
index 133f189bf6d270..541f0830cf79a6 100644
--- a/kernel/bpf/arraymap.c
+++ b/kernel/bpf/arraymap.c
@@ -1028,7 +1028,12 @@ static void *prog_fd_array_get_ptr(struct bpf_map *map,
 	if (IS_ERR(prog))
 		return prog;
 
-	if (prog->type == BPF_PROG_TYPE_EXT ||
+	/*
+	 * A tail call runs the program on the caller's Tasks Trace RCU read
+	 * section alone, and a program that parks leaves that section for its
+	 * sleep, see bpf_task_park().
+	 */
+	if (prog->type == BPF_PROG_TYPE_EXT || prog->aux->may_migrate ||
 	    !bpf_prog_map_compatible(map, prog)) {
 		bpf_prog_put(prog);
 		return ERR_PTR(-EINVAL);
diff --git a/kernel/bpf/cfg.c b/kernel/bpf/cfg.c
index bbcf42eae9d921..0102df246633f0 100644
--- a/kernel/bpf/cfg.c
+++ b/kernel/bpf/cfg.c
@@ -67,6 +67,14 @@ static void mark_subprog_might_sleep(struct bpf_verifier_env *env, int off)
 	subprog->might_sleep = true;
 }
 
+static void mark_subprog_might_park(struct bpf_verifier_env *env, int off)
+{
+	struct bpf_subprog_info *subprog;
+
+	subprog = bpf_find_containing_subprog(env, off);
+	subprog->might_park = true;
+}
+
 static void mark_subprog_might_throw(struct bpf_verifier_env *env, int off)
 {
 	struct bpf_subprog_info *subprog;
@@ -86,15 +94,28 @@ static void mark_subprog_might_throw(struct bpf_verifier_env *env, int off)
 static bool merge_callee_effects(struct bpf_verifier_env *env, int t, int w)
 {
 	struct bpf_subprog_info *caller, *callee;
+	bool might_park;
 
 	caller = bpf_find_containing_subprog(env, t);
 	callee = bpf_find_containing_subprog(env, w);
+	/*
+	 * A park in a callback does not mark the function that only loads
+	 * the callback's address: a synchronous callback is refused at the
+	 * park with the caller's frames in view, and an asynchronous one is a
+	 * root of its own. The address may also be called through callx,
+	 * which is a call like any other, so the mark follows it when the
+	 * program has callx at all.
+	 */
+	might_park = callee->might_park &&
+		     (bpf_pseudo_call(&env->prog->insnsi[t]) || env->has_callx);
 	if (caller->changes_pkt_data >= callee->changes_pkt_data &&
 	    caller->might_sleep >= callee->might_sleep &&
+	    caller->might_park >= might_park &&
 	    caller->might_throw >= callee->might_throw)
 		return false;
 	caller->changes_pkt_data |= callee->changes_pkt_data;
 	caller->might_sleep |= callee->might_sleep;
+	caller->might_park |= might_park;
 	caller->might_throw |= callee->might_throw;
 	return true;
 }
@@ -618,6 +639,8 @@ static int visit_insn(int t, struct bpf_verifier_env *env)
 			 */
 			if (ret == 0 && bpf_is_kfunc_sleepable(&meta))
 				mark_subprog_might_sleep(env, t);
+			if (ret == 0 && bpf_is_task_park_kfunc(&meta))
+				mark_subprog_might_park(env, t);
 			if (ret == 0 && bpf_is_kfunc_pkt_changing(&meta))
 				mark_subprog_changes_pkt_data(env, t);
 			if (ret == 0 && bpf_is_throw_kfunc(insn))
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index f88a1c041a7a69..95f4be8ed40537 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -138,6 +138,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag
 #ifdef CONFIG_FINEIBT
 	INIT_LIST_HEAD_RCU(&fp->aux->ksym_prefix.lnode);
 #endif
+	INIT_LIST_HEAD(&fp->aux->kthreads);
 	mutex_init(&fp->aux->used_maps_mutex);
 	mutex_init(&fp->aux->ext_mutex);
 	mutex_init(&fp->aux->dst_mutex);
diff --git a/kernel/bpf/diagnostics.c b/kernel/bpf/diagnostics.c
index 3de3e1486bbdef..33788d6f615580 100644
--- a/kernel/bpf/diagnostics.c
+++ b/kernel/bpf/diagnostics.c
@@ -2231,6 +2231,9 @@ static void diag_print_mod(struct bpf_verifier_env *env, const struct bpf_diag_h
 	case BPF_DIAG_MOD_NON_OWN_REF:
 		reason = "leaving the protected region invalidated this borrowed pointer";
 		break;
+	case BPF_DIAG_MOD_TASK_PARK:
+		reason = "the sleep in bpf_task_park() invalidated this pointer";
+		break;
 	case BPF_DIAG_MOD_CALLER_SAVED:
 		reason = target->kind == BPF_DIAG_MOD_TARGET_STACK_ARG ?
 			 "call invalidated this outgoing stack argument" :
diff --git a/kernel/bpf/diagnostics.h b/kernel/bpf/diagnostics.h
index e155d2588a51c6..656c0430f013e4 100644
--- a/kernel/bpf/diagnostics.h
+++ b/kernel/bpf/diagnostics.h
@@ -26,6 +26,7 @@ enum bpf_diag_mod_reason {
 	BPF_DIAG_MOD_SLICE_BUF,
 	BPF_DIAG_MOD_NON_OWN_REF,
 	BPF_DIAG_MOD_CALLER_SAVED,
+	BPF_DIAG_MOD_TASK_PARK,
 };
 
 enum bpf_diag_context_kind {
diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
index 1d33a78d937390..df5784a132d754 100644
--- a/kernel/bpf/fixups.c
+++ b/kernel/bpf/fixups.c
@@ -2862,6 +2862,12 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
 			u16 live = env->insn_aux_data[i].live_regs_before;
 			int reg, save_off = 0;
 
+			/* The count and the time are kept per cpu, see bpf_timed_may_goto_save(). */
+			if (prog->aux->may_migrate) {
+				verbose(env, "may_goto cannot be used in a program that may migrate, also not in a loop that gets one\n");
+				return -EINVAL;
+			}
+
 			/* a register that is dead here holds the address of the count */
 			for (reg = BPF_REG_0; reg <= BPF_REG_9; reg++)
 				if (!(live & BIT(reg)))
diff --git a/kernel/bpf/flow.c b/kernel/bpf/flow.c
index c9458b3ad114a5..47ff2fb5f11a50 100644
--- a/kernel/bpf/flow.c
+++ b/kernel/bpf/flow.c
@@ -2309,7 +2309,8 @@ static void flow_shape_ctx(const struct bpf_verifier_state *st, struct flow_shap
 
 	ctx = jhash_3words(st->in_sleepable | st->callers_unknown << 1 |
 			   st->unwound << 2 | st->changed_pkt_data << 3 |
-			   (st->active_irq_id != 0) << 4 | (st->active_lock_id != 0) << 5,
+			   (st->active_irq_id != 0) << 4 | (st->active_lock_id != 0) << 5 |
+			   st->parked << 6,
 			   st->acquired_refs, st->active_locks, 0);
 	ctx = jhash_2words(st->active_preempt_locks, st->active_rcu_locks, ctx);
 	ctx = jhash_2words(lower_32_bits((unsigned long)st->active_lock_ptr),
diff --git a/kernel/bpf/join.c b/kernel/bpf/join.c
index cc4494aef18733..b63f99c663080e 100644
--- a/kernel/bpf/join.c
+++ b/kernel/bpf/join.c
@@ -452,6 +452,7 @@ static bool join_func(struct bpf_join_state *js, struct bpf_func_state *fd,
 			return false;
 		if (fa->in_callback_fn != fb->in_callback_fn ||
 		    fa->in_async_callback_fn != fb->in_async_callback_fn ||
+		    fa->in_kthread_fn != fb->in_kthread_fn ||
 		    fa->in_exception_callback_fn != fb->in_exception_callback_fn ||
 		    fa->in_rcu_callback_fn != fb->in_rcu_callback_fn ||
 		    fa->async_entry_cnt != fb->async_entry_cnt ||
@@ -532,7 +533,8 @@ static bool join_check_state(const struct bpf_verifier_state *a,
 		return false;
 	if (a->speculative != b->speculative || a->in_sleepable != b->in_sleepable ||
 	    a->callers_unknown != b->callers_unknown ||
-	    a->unwound != b->unwound || a->changed_pkt_data != b->changed_pkt_data)
+	    a->unwound != b->unwound || a->changed_pkt_data != b->changed_pkt_data ||
+	    a->parked != b->parked)
 		return false;
 	if (a->active_locks != b->active_locks ||
 	    a->active_preempt_locks != b->active_preempt_locks ||
diff --git a/kernel/bpf/kthread.c b/kernel/bpf/kthread.c
new file mode 100644
index 00000000000000..d4b8110545f09d
--- /dev/null
+++ b/kernel/bpf/kthread.c
@@ -0,0 +1,572 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
+#include <linux/cgroup.h>
+#include <linux/cpumask.h>
+#include <linux/kthread.h>
+#include <linux/rcupdate_trace.h>
+#include <linux/sched.h>
+#include <linux/sched/signal.h>
+#include <linux/sched/task.h>
+
+/*
+ * A BPF kthread is a kernel thread whose thread function is one callback of
+ * the program that created it, run once. The callback is the body of the
+ * thread: it loops, sleeps in bpf_task_park() and returns on its own, and its
+ * return ends the thread. The kernel adds nothing in between.
+ *
+ * The thread is known to the program by its task_struct. bpf_kthread_create()
+ * returns it as a referenced pointer, and the control kfuncs take it back, so
+ * the program keeps it wherever it keeps task pointers: a kptr in a map value,
+ * a graph node, or a register until
+ * bpf_task_release(). Nothing else is handed out, and nothing else needs a
+ * lifetime of its own.
+ *
+ * Every thread is on one global list, which is how a control kfunc tells a
+ * BPF kthread from any other task it is given, and how the thread's object is
+ * found without reading through a task that may have exited. Whoever takes a
+ * thread off the list under the lock owns its teardown.
+ *
+ * A thread does not hold a reference on its program. If it did, a program
+ * whose user dropped every file descriptor would live on through its own
+ * threads, with nothing left that could stop them. Instead the release of the
+ * program's last reference stops the threads it still has, before anything
+ * their callback could be using is released, and creation fails once the
+ * program has no reference left. A program's threads thus end with its last
+ * user, the way a timer or a work item ends with the last user of its map.
+ * The one exception is a thread that bpf_kthread_stop() has taken off the
+ * lists but that has not exited yet: its stopper holds the program for it.
+ *
+ * Four of the kfuncs wait for another task. Creation waits for kthreadd,
+ * bpf_kthread_stop() for the thread's exit, and bpf_kthread_bind() and
+ * bpf_kthread_attach_cgroup() for a running thread's next migrate_enable(),
+ * which is what set_cpus_allowed_ptr() waits for on any migration-disabled
+ * task. The kernel assumes that a task which has disabled migration enables
+ * it again soon, and waits for that while holding locks: cpuset attach under
+ * cgroup_mutex, cpus_read_lock() and the threadgroup rwsem, a cpuset.cpus
+ * write under cpuset_mutex, CPU offline in balance_hotplug_wait(). kthreadd's
+ * fork read-locks the global threadgroup rwsem as well, which a cgroup move
+ * holds in the default lock mode; under favordynmods the move holds the moved
+ * process's own, but a subtree_control write still takes the global one. A
+ * sleepable program is migration-disabled for its whole run and a thread body
+ * for its life outside the park, so a wait made while pinned can close a cycle
+ * through any of those: a task moved into a cpuset while it creates a thread,
+ * a thread blocked in a stop being bound by the thread it waits for, a thread
+ * on a CPU going offline taking cpus_read_lock(). Each of the four waits
+ * therefore runs with migration enabled and the Tasks Trace read section kept.
+ * Nothing the read section protects depends on the CPU: the program, its maps
+ * and its context are held as before, per-CPU memory stays valid memory, and
+ * neither runner keeps per-CPU state across the program. A program that keeps
+ * such state of its own, a stack in arena or the time slice of may_goto, is
+ * refused these kfuncs as it is refused the park. The caller may return on
+ * another CPU, the contract bpf_task_park() already carries, so a per-CPU map
+ * value pointer or a CPU id obtained before such a call names the CPU of the
+ * lookup. Unlike the park, the waits keep the program's protection, so the
+ * verifier has nothing to invalidate across them.
+ */
+
+struct bpf_kthread {
+	struct list_head node;		/* on bpf_kthreads */
+	struct list_head prog_node;	/* on aux->kthreads */
+	struct task_struct *task;	/* holds one reference for this object */
+	struct bpf_prog_aux *aux;	/* the program whose callback this is */
+	struct bpf_kthread *waits_for;	/* the thread this one is stopping, under the lock */
+	void *callback_fn;
+	u64 arg;
+};
+
+/*
+ * Both lists, and every object on them, are under this lock. It is a raw lock
+ * taken with IRQs disabled because bpf_kthread_start() is not sleepable: a
+ * timer callback calls it from softirq, and a program may call it with IRQs
+ * or preemption disabled.
+ */
+static LIST_HEAD(bpf_kthreads);
+static DEFINE_RAW_SPINLOCK(bpf_kthreads_lock);
+
+static int bpf_kthread_fn(void *data)
+{
+	struct bpf_kthread *kthread = data;
+	bpf_callback_t callback_fn = kthread->callback_fn;
+	unsigned int pflags;
+	int ret;
+
+	/* Stopped before it was started: the callback does not run. */
+	if (kthread_should_stop())
+		return -EINTR;
+
+	/*
+	 * The callback runs as the body of a sleepable program does, with
+	 * migration disabled under a Tasks Trace RCU read section, except
+	 * while it sleeps in bpf_task_park(), which drops both for the sleep
+	 * and takes them again before it returns, and while it waits in one
+	 * of the kfuncs the comment at the top names, which drop migration
+	 * disable alone. That is
+	 * safe here for the reason it is safe in a syscall program: this
+	 * function keeps nothing across the callback, and the program, with
+	 * its maps and code, is held until the thread has exited, since the
+	 * release of its last reference stops the thread first. The verifier
+	 * checks the callback as a sleepable callback in which the park is
+	 * allowed.
+	 */
+	/*
+	 * A task work queued for this thread with TWA_SIGNAL sets
+	 * TIF_NOTIFY_SIGNAL, which a kernel thread never gets to clear, since
+	 * the work runs on the return to user mode or at exit. Left alone it
+	 * would end every sleep of the body at once from then on.
+	 */
+	pflags = no_notify_signal_save();
+	rcu_read_lock_trace();
+	migrate_disable();
+	ret = callback_fn(kthread->arg, 0, 0, 0, 0);
+	migrate_enable();
+	rcu_read_unlock_trace();
+	no_notify_signal_restore(pflags);
+	return ret;
+}
+
+/* The BPF kthread whose task is @task, if any. Call with bpf_kthreads_lock held. */
+static struct bpf_kthread *bpf_kthread_find(struct task_struct *task)
+{
+	struct bpf_kthread *kthread;
+
+	lockdep_assert_held(&bpf_kthreads_lock);
+	list_for_each_entry(kthread, &bpf_kthreads, node)
+		if (kthread->task == task)
+			return kthread;
+	return NULL;
+}
+
+/* Take a thread off both lists. Call with bpf_kthreads_lock held. */
+static void bpf_kthread_unlink(struct bpf_kthread *kthread)
+{
+	lockdep_assert_held(&bpf_kthreads_lock);
+	list_del(&kthread->node);
+	list_del(&kthread->prog_node);
+}
+
+/*
+ * The object of the calling thread, if it is a BPF kthread that has not begun
+ * to exit. An exiting thread still runs its task work, which may be a program's
+ * callback, and its object is freed as soon as whoever stops it has seen it
+ * exit, which is before that. Nothing waits for such a thread any more, so it
+ * needs no record of what it waits for.
+ */
+static struct bpf_kthread *bpf_kthread_self(void)
+{
+	if (kthread_func(current) != bpf_kthread_fn || (current->flags & PF_EXITING))
+		return NULL;
+	return kthread_data(current);
+}
+
+/*
+ * Whether a stop of @kthread would wait, through the chain of threads each of
+ * them is stopping, for the calling thread. Call with bpf_kthreads_lock held.
+ * Every object on the chain is alive: a thread clears waits_for under the lock
+ * before it frees the object it points to.
+ */
+static bool bpf_kthread_waits_for_current(struct bpf_kthread *kthread)
+{
+	lockdep_assert_held(&bpf_kthreads_lock);
+	for (; kthread; kthread = kthread->waits_for)
+		if (kthread->task == current)
+			return true;
+	return false;
+}
+
+/*
+ * Stop a thread that has been taken off the lists and free its object. Waits
+ * for the thread to exit, and returns what its callback returned, or -EINTR if
+ * the callback never ran. Returns at once for a thread that has exited.
+ */
+static int bpf_kthread_stop_and_free(struct bpf_kthread *kthread)
+{
+	int ret;
+
+	ret = kthread_stop_put(kthread->task);
+	kfree(kthread);
+	return ret;
+}
+
+/*
+ * Stop every thread of @aux. Called when the program's last reference goes,
+ * before its code, maps and BTF are released, and before the Tasks Trace RCU
+ * grace period that release waits for, which a thread still inside its
+ * callback would hold up. May block.
+ */
+void bpf_prog_free_kthreads(struct bpf_prog_aux *aux)
+{
+	struct bpf_kthread *kthread, *tmp;
+	unsigned long irqflags;
+	LIST_HEAD(kthreads);
+
+	/*
+	 * Nothing joins a program's list once its last reference is gone, so
+	 * an empty list stays empty. The release path takes the lock only for
+	 * a program with threads, from a work item, never from the direct
+	 * path it may take in softirq context for a program without.
+	 */
+	if (list_empty(&aux->kthreads))
+		return;
+
+	raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+	list_for_each_entry_safe(kthread, tmp, &aux->kthreads, prog_node) {
+		list_del(&kthread->node);
+		list_move_tail(&kthread->prog_node, &kthreads);
+	}
+	raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+
+	list_for_each_entry_safe(kthread, tmp, &kthreads, prog_node)
+		bpf_kthread_stop_and_free(kthread);
+}
+
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_kthread_create - Create a kernel thread that runs a BPF callback
+ * @callback_fn: the thread's body, run once; its return ends the thread
+ * @arg: passed to @callback_fn as its only argument
+ * @flags: must be 0
+ * @aux: the calling program, set by the verifier
+ *
+ * Creates a kernel thread named bpfk/<program id>:<index> without starting it,
+ * the index counting the program's creations so that its threads are told
+ * apart in ps, top and traces; the prefix is short so that both numbers fit
+ * the comm that tracepoints and perf report as long as they have nine digits
+ * between them, where /proc shows the full name in any case. bpf_kthread_start()
+ * runs @callback_fn on it with @arg. The thread ends when the callback returns,
+ * when bpf_kthread_stop() stops it, or with the last reference on the program
+ * that created it. A thread whose callback has returned keeps its return value
+ * until bpf_kthread_stop() collects it.
+ *
+ * The creation waits for kthreadd with migration enabled, so the caller may
+ * return on another CPU.
+ *
+ * Return: the thread's task with a reference the program must release, or NULL
+ * if @flags is not 0, the thread could not be created, or the program is being
+ * freed.
+ */
+__bpf_kfunc struct task_struct *bpf_kthread_create(int (callback_fn)(u64 arg), u64 arg,
+						   u64 flags, struct bpf_prog_aux *aux)
+{
+	struct bpf_kthread *kthread;
+	unsigned long irqflags;
+	struct task_struct *task;
+	struct bpf_prog *prog;
+
+	if (flags)
+		return NULL;
+
+	kthread = kzalloc_obj(*kthread, GFP_KERNEL_ACCOUNT);
+	if (!kthread)
+		return NULL;
+	kthread->aux = aux;
+	kthread->callback_fn = callback_fn;
+	kthread->arg = arg;
+
+	/*
+	 * The creation waits for kthreadd, and kthreadd's fork takes the
+	 * threadgroup rwsem that a cgroup move of this task holds while it
+	 * waits for this task to enable migration: see the comment at the top.
+	 */
+	migrate_enable();
+	task = kthread_create(bpf_kthread_fn, kthread, "bpfk/%u:%u", aux->id,
+			      atomic_fetch_inc(&aux->kthread_seq));
+	if (IS_ERR(task)) {
+		migrate_disable();
+		kfree(kthread);
+		return NULL;
+	}
+	kthread->task = get_task_struct(task);
+	/*
+	 * The program's reference is the kind bpf_task_acquire() takes and
+	 * bpf_task_release() drops; the object's own, above, is the kind
+	 * kthread_stop_put() drops. Take it before the thread is published:
+	 * from then on any program may stop it, and a stopped thread's exit
+	 * drops the last reference the task had of its own.
+	 */
+	refcount_inc(&task->rcu_users);
+
+	/*
+	 * Run the thread up to its park. The generic kthread prologue, which
+	 * runs first, resets the affinity to the default for the thread's
+	 * node, so that an affinity set from now on sticks.
+	 */
+	kthread_park(task);
+	migrate_disable();
+
+	/*
+	 * Publish the thread while the program has a reference left to take.
+	 * Once it has none, the release of its last reference is pending or
+	 * under way, and a thread published after that release looked would
+	 * survive the program. The reference taken here is dropped again at
+	 * once: the release cannot start before it is, and the thread is on
+	 * the lists by then.
+	 */
+	raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+	prog = bpf_prog_inc_not_zero(aux->prog);
+	if (IS_ERR(prog)) {
+		raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+		migrate_enable();
+		bpf_kthread_stop_and_free(kthread);
+		migrate_disable();
+		put_task_struct_rcu_user(task);
+		return NULL;
+	}
+	list_add_tail(&kthread->node, &bpf_kthreads);
+	list_add_tail(&kthread->prog_node, &aux->kthreads);
+	raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+	bpf_prog_put(prog);
+	return task;
+}
+
+/**
+ * bpf_kthread_start - Run the callback of a BPF kthread
+ * @task: a thread created by bpf_kthread_create()
+ * @flags: must be 0
+ *
+ * Starts the thread, which then runs its callback. Does nothing for a thread
+ * that has been started already.
+ *
+ * Return: 0 on success, -EINVAL if @flags is not 0, -ESRCH if @task is not a
+ * BPF kthread or has been stopped.
+ */
+__bpf_kfunc int bpf_kthread_start(struct task_struct *task, u64 flags)
+{
+	unsigned long irqflags;
+	bool found;
+
+	if (flags)
+		return -EINVAL;
+
+	raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+	found = !!bpf_kthread_find(task);
+	raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+	if (!found)
+		return -ESRCH;
+
+	kthread_unpark(task);
+	return 0;
+}
+
+/**
+ * bpf_kthread_bind - Restrict a BPF kthread to one CPU
+ * @task: a thread created by bpf_kthread_create()
+ * @cpu: the CPU the thread is to run on
+ * @flags: must be 0
+ *
+ * Sets the thread's affinity to @cpu. A thread that has not been started wakes
+ * on @cpu; one that has moves there at its next park or return, since its
+ * callback runs with migration disabled, and the call waits for that with
+ * migration enabled, so the caller may return on another CPU and a thread may
+ * bind itself. Unlike kthread_bind(), this leaves the affinity changeable from
+ * user space and subject to cpusets, like that of any other task. The binding
+ * is lost when @cpu goes offline or when the cpuset of the thread's cgroup
+ * changes its CPUs: the thread is moved, or placed elsewhere at its next
+ * wakeup, and its affinity reset to the cpuset's CPUs, as a user task's is
+ * after an offline. It is also lost when the housekeeping CPUs change, which
+ * resets the affinity of every kernel thread that is not bound for good. A
+ * program that needs the binding re-applies it.
+ *
+ * Return: 0 on success, -EINVAL if @flags is not 0 or @cpu is not a possible
+ * CPU or is offline, -ESRCH if @task is not a BPF kthread or has been stopped.
+ */
+__bpf_kfunc int bpf_kthread_bind(struct task_struct *task, u32 cpu, u64 flags)
+{
+	unsigned long irqflags;
+	bool found;
+	int ret;
+
+	if (flags)
+		return -EINVAL;
+	if (cpu >= nr_cpu_ids || !cpu_possible(cpu))
+		return -EINVAL;
+
+	raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+	found = !!bpf_kthread_find(task);
+	raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+	if (!found)
+		return -ESRCH;
+
+	/* A running thread moves at its next park: see the comment at the top. */
+	migrate_enable();
+	ret = set_cpus_allowed_ptr(task, cpumask_of(cpu));
+	migrate_disable();
+	return ret;
+}
+
+#ifdef CONFIG_CGROUPS
+/**
+ * bpf_kthread_attach_cgroup - Move a BPF kthread into a cgroup
+ * @task: a thread created by bpf_kthread_create()
+ * @cgrp: the cgroup to move it into
+ * @flags: must be 0
+ *
+ * Moves the thread into @cgrp, started or not, so that its CPU time and the
+ * pressure it causes are accounted to @cgrp and its affinity follows @cgrp's
+ * cpuset. Until then it runs in the root cgroup, as a kernel thread does.
+ * Kernel memory the thread allocates is still not charged to @cgrp, since
+ * the allocations of a kernel thread are charged to no memory cgroup. The
+ * thread is a member of @cgrp like any process: @cgrp cannot be removed until
+ * the thread leaves or exits, and @cgrp has to be able to host processes, so
+ * on cgroup v2 it may not have controllers enabled in its children. Like any
+ * kernel thread it is not frozen with @cgrp, which then never reports frozen.
+ * A cpuset applies its CPUs to the thread as it joins, so bind the
+ * thread with bpf_kthread_bind() afterwards, not before. A running thread
+ * joins a cpuset at its next park, and the move waits for that with
+ * migration enabled, holding cgroup_mutex meanwhile, so the caller may return
+ * on another CPU and a thread may move itself. A thread whose callback has
+ * returned but that has not been stopped is left where it is and the call
+ * reports success, as cgroup.procs does for an exiting task.
+ *
+ * Return: 0 on success, -EINVAL if @flags is not 0, -ESRCH if @task is not a
+ * BPF kthread or has been stopped, -ENODEV if @cgrp is being removed, -EBUSY
+ * or -EOPNOTSUPP if @cgrp cannot host processes, or an error of the move.
+ */
+__bpf_kfunc int bpf_kthread_attach_cgroup(struct task_struct *task, struct cgroup *cgrp,
+					  u64 flags)
+{
+	unsigned long irqflags;
+	bool found;
+	int ret;
+
+	if (flags)
+		return -EINVAL;
+
+	raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+	found = !!bpf_kthread_find(task);
+	raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+	if (!found)
+		return -ESRCH;
+
+	/* A running thread joins at its next park: see the comment at the top. */
+	migrate_enable();
+	ret = cgroup_attach_kthread(cgrp, task);
+	migrate_disable();
+	return ret;
+}
+#endif
+
+/**
+ * bpf_kthread_stop - Stop a BPF kthread and wait for it to exit
+ * @task: a thread created by bpf_kthread_create()
+ * @flags: must be 0
+ *
+ * Makes bpf_kthread_should_stop() return true in the thread, ends the sleep it
+ * is in, and waits for its callback to return. A thread that has not been
+ * started exits without running its callback. The thread is gone afterwards;
+ * the reference the program holds on @task stays its own to release.
+ *
+ * The wait ends when the callback returns, which the callback decides by
+ * polling bpf_kthread_should_stop(); the kernel cannot end it sooner. It is
+ * made with migration enabled, so the caller may return on another CPU, but
+ * under the Tasks Trace read section, which every Tasks Trace grace period
+ * then waits for, so a thread should stop threads that return promptly, as
+ * one that sleeps in bpf_task_park() does. Two threads stopping each other, or
+ * a longer ring of them, would wait forever; the stop that would close such a
+ * ring is refused instead.
+ *
+ * Return: what the thread's callback returned, or -EINTR if it never ran.
+ * -EINVAL if @flags is not 0, -EDEADLK if @task is the calling thread or is,
+ * through the threads it is itself stopping, waiting for the calling thread,
+ * -ESRCH if @task is not a BPF kthread, has been stopped already, or is being
+ * stopped by the release of its program.
+ */
+__bpf_kfunc int bpf_kthread_stop(struct task_struct *task, u64 flags)
+{
+	struct bpf_kthread *kthread, *self;
+	unsigned long irqflags;
+	struct bpf_prog *prog;
+	int ret;
+
+	if (flags)
+		return -EINVAL;
+	if (task == current)
+		return -EDEADLK;
+
+	raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+	kthread = bpf_kthread_find(task);
+	if (!kthread) {
+		raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+		return -ESRCH;
+	}
+	if (bpf_kthread_waits_for_current(kthread)) {
+		raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+		return -EDEADLK;
+	}
+	/*
+	 * Hold the program while the thread is off the lists and not yet
+	 * gone. The release of the program's last reference stops only the
+	 * threads it finds on its list, and this one may be asleep in
+	 * bpf_task_park(), outside the read section that otherwise keeps the
+	 * program for it, so without the reference it could wake into a freed
+	 * program. A program with no reference left is being released, and
+	 * that release is stopping the thread.
+	 */
+	prog = bpf_prog_inc_not_zero(kthread->aux->prog);
+	if (IS_ERR(prog)) {
+		raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+		return -ESRCH;
+	}
+	bpf_kthread_unlink(kthread);
+	self = bpf_kthread_self();
+	if (self)
+		self->waits_for = kthread;
+	raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+
+	/* The wait for the exit: see the comment at the top. */
+	migrate_enable();
+	ret = kthread_stop_put(kthread->task);
+	migrate_disable();
+
+	if (self) {
+		raw_spin_lock_irqsave(&bpf_kthreads_lock, irqflags);
+		self->waits_for = NULL;
+		raw_spin_unlock_irqrestore(&bpf_kthreads_lock, irqflags);
+	}
+	kfree(kthread);
+	bpf_prog_put(prog);
+	return ret;
+}
+
+/**
+ * bpf_kthread_should_stop - Whether the calling BPF kthread was asked to stop
+ *
+ * The callback of a BPF kthread checks this in its loop and returns when it is
+ * set, as a kernel thread function does; bpf_kthread_stop() waits for that.
+ *
+ * Return: true in a kernel thread that was asked to stop, by bpf_kthread_stop()
+ * or by the release of its program, false otherwise, including for every
+ * thread of a user process.
+ */
+__bpf_kfunc bool bpf_kthread_should_stop(void)
+{
+	return (current->flags & PF_KTHREAD) && kthread_should_stop();
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_kthread_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_kthread_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE | KF_IMPLICIT_ARGS)
+BTF_ID_FLAGS(func, bpf_kthread_start, KF_RCU)
+BTF_ID_FLAGS(func, bpf_kthread_bind, KF_SLEEPABLE)
+#ifdef CONFIG_CGROUPS
+BTF_ID_FLAGS(func, bpf_kthread_attach_cgroup, KF_SLEEPABLE)
+#endif
+BTF_ID_FLAGS(func, bpf_kthread_stop, KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_kthread_should_stop)
+BTF_KFUNCS_END(bpf_kthread_kfunc_ids)
+
+static const struct btf_kfunc_id_set bpf_kthread_kfunc_set = {
+	.owner = THIS_MODULE,
+	.set = &bpf_kthread_kfunc_ids,
+};
+
+static int __init bpf_kthread_kfunc_init(void)
+{
+	return register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_kthread_kfunc_set);
+}
+late_initcall(bpf_kthread_kfunc_init);
diff --git a/kernel/bpf/states.c b/kernel/bpf/states.c
index efc0870b85c2a3..d0db8074928a0e 100644
--- a/kernel/bpf/states.c
+++ b/kernel/bpf/states.c
@@ -745,7 +745,8 @@ bool bpf_state_subsumes(struct bpf_verifier_env *env, struct bpf_verifier_state
 	if (old->in_sleepable != cur->in_sleepable)
 		return false;
 
-	if (old->unwound != cur->unwound || old->changed_pkt_data != cur->changed_pkt_data)
+	if (old->unwound != cur->unwound || old->changed_pkt_data != cur->changed_pkt_data ||
+	    old->parked != cur->parked)
 		return false;
 
 	if (!refsafe(old, cur, &env->idmap_scratch))
@@ -758,6 +759,13 @@ bool bpf_state_subsumes(struct bpf_verifier_env *env, struct bpf_verifier_state
 		insn_idx = bpf_frame_insn_idx(old, i);
 		if (old->frame[i]->callsite != cur->frame[i]->callsite)
 			return false;
+		/*
+		 * The body of a BPF kthread may park where another async
+		 * callback of the same subprog may not, so a state verified
+		 * in one role must not prune the other.
+		 */
+		if (old->frame[i]->in_kthread_fn != cur->frame[i]->in_kthread_fn)
+			return false;
 		if (!func_states_equal(env, old->frame[i], cur->frame[i], insn_idx))
 			return false;
 	}
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 9004f2673ccf96..9ea8c755651efe 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2512,6 +2512,12 @@ static void bpf_prog_put_deferred(struct work_struct *work)
 
 	aux = container_of(work, struct bpf_prog_aux, work);
 	prog = aux->prog;
+	/*
+	 * The threads of the prog still run its code, each inside a Tasks
+	 * Trace RCU read section that the grace period below would wait for.
+	 * Stop them first; this waits for each of them to exit.
+	 */
+	bpf_prog_free_kthreads(aux);
 	perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
 	bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
 	bpf_prog_free_id(prog);
@@ -2523,7 +2529,8 @@ static void __bpf_prog_put(struct bpf_prog *prog)
 	struct bpf_prog_aux *aux = prog->aux;
 
 	if (atomic64_dec_and_test(&aux->refcnt)) {
-		if (in_hardirq() || irqs_disabled()) {
+		/* Stopping the prog's threads blocks, which the direct path may not. */
+		if (in_hardirq() || irqs_disabled() || !list_empty(&aux->kthreads)) {
 			INIT_WORK(&aux->work, bpf_prog_put_deferred);
 			schedule_work(&aux->work);
 		} else {
@@ -6661,8 +6668,9 @@ int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
 		if (IS_ERR(prog))
 			return PTR_ERR(prog);
 
+		/* The run is counted per CPU, which a program that migrates would unbalance. */
 		if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
-		    attr->test.ctx_size_in > U16_MAX) {
+		    attr->test.ctx_size_in > U16_MAX || prog->aux->may_migrate) {
 			bpf_prog_put(prog);
 			return -EINVAL;
 		}
diff --git a/kernel/bpf/task_park.c b/kernel/bpf/task_park.c
new file mode 100644
index 00000000000000..b6980c1cc1bf0b
--- /dev/null
+++ b/kernel/bpf/task_park.c
@@ -0,0 +1,311 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <linux/bpf.h>
+#include <linux/btf.h>
+#include <linux/btf_ids.h>
+#include <linux/hrtimer.h>
+#include <linux/irq_work.h>
+#include <linux/kthread.h>
+#include <linux/rcupdate_trace.h>
+#include <linux/sched.h>
+#include <linux/sched/signal.h>
+#include <linux/sched/wake_q.h>
+
+/*
+ * A BPF program that blocks for an event keeps its own queue of waiters and
+ * its own notion of when the event has arrived. The kernel contributes two
+ * operations over the task: bpf_task_park() puts the current task to sleep
+ * and bpf_task_wake() makes a task runnable. The only shared state is one
+ * word in the task, bpf_wake_pending, which lets the program check its
+ * condition outside the kernel without losing a wake that lands between
+ * the check and the sleep: the waker sets the word before it wakes the
+ * task, and a park that finds it set returns instead of sleeping.
+ *
+ * The word also carries the ordering the program relies on. A waker
+ * publishes its condition and then calls bpf_task_wake(); a waiter reads
+ * the condition, finds it unmet, calls bpf_task_park(), and reads the
+ * condition again after the return. The second read must see the waker's
+ * condition whenever the park returned because of that wake, so the store
+ * of the word and the load that consumes it are both an xchg(): the store
+ * is fully ordered after the waker's condition stores, the load before the
+ * waiter's later condition loads. A release would not do for the store. Of
+ * two wakers' stores one overwrites the other, the waiter observes only the
+ * later one, and nothing would order the earlier waker's condition before
+ * the waiter's read of it. The xchg() also makes the consume atomic: a wake
+ * that lands between the waiter's observation of the word and its reset is
+ * not erased but stays pending for the next park.
+ */
+
+/*
+ * A wake asked for with IRQs disabled cannot be issued there: the caller may
+ * have interrupted the scheduler with the task's pi_lock or a runqueue lock
+ * held, or may run in NMI context, where try_to_wake_up() may take neither.
+ * Such wakes are queued on the CPU and issued from irq_work once IRQs are
+ * enabled again.
+ *
+ * The queue goes through a node of its own in the task, under the rule the
+ * scheduler's wake queues follow for theirs: a task is on at most one queue,
+ * whoever claimed the node wakes the task, and a waker that finds it claimed
+ * leaves the wake to the claimant. The pending word is set before the claim
+ * is tried, so a wake left this way still reaches a parked task through the
+ * claimant's wake, and any other task through its next park.
+ *
+ * The task's wake_q node would save the field, but whoever claims that node
+ * may turn away a futex, rwsem or semaphore wakeup that then relies on the
+ * claimant, and so has to wake with TASK_NORMAL. A program could then aim a
+ * wake of an uninterruptible sleep at any task it names, and not every such
+ * sleep has a loop around it: a new kthread sleeps once until its creator
+ * wakes it. With its own node the drain wakes interruptible sleeps only, as
+ * the direct wake does.
+ *
+ * The queue is a stack with a per-CPU head: pushed with cmpxchg() from
+ * whatever context asks, emptied with one xchg() by the irq_work, so a push
+ * that interrupts the drain lands on an empty stack and queues the work
+ * again.
+ */
+static DEFINE_PER_CPU(struct wake_q_node *, bpf_task_wake_queue);
+
+static void bpf_task_wake_deferred(struct irq_work *work)
+{
+	struct wake_q_node *node = xchg(this_cpu_ptr(&bpf_task_wake_queue), NULL);
+
+	while (node) {
+		struct task_struct *task = container_of(node, struct task_struct, bpf_wake_node);
+
+		node = node->next == WAKE_Q_TAIL ? NULL : node->next;
+		/* The task may be queued again from here on. */
+		WRITE_ONCE(task->bpf_wake_node.next, NULL);
+		wake_up_state(task, TASK_INTERRUPTIBLE);
+		put_task_struct(task);
+	}
+}
+
+static DEFINE_PER_CPU(struct irq_work, bpf_task_wake_work) =
+	IRQ_WORK_INIT_HARD(bpf_task_wake_deferred);
+
+static void bpf_task_wake_defer(struct task_struct *task)
+{
+	struct wake_q_node *node = &task->bpf_wake_node, *head;
+
+	/*
+	 * The claim, as in __wake_q_add(): NULL is free, anything else is
+	 * queued. The xchg() that stored the pending word is a full barrier
+	 * between that store and the claim, so a claim lost to another holder
+	 * is still covered: the holder's wake follows its own reset of the
+	 * node, which this claim did not observe, so the task it wakes
+	 * observes the word.
+	 */
+	if (cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL))
+		return;
+	get_task_struct(task);
+	head = this_cpu_read(bpf_task_wake_queue);
+	do {
+		WRITE_ONCE(node->next, head ?: WAKE_Q_TAIL);
+	} while (!try_cmpxchg(this_cpu_ptr(&bpf_task_wake_queue), &head, node));
+	irq_work_queue(this_cpu_ptr(&bpf_task_wake_work));
+}
+
+/*
+ * What ends a park besides a wake and the timeout: a stop or park request for
+ * a kthread, or a signal. Both are checked for every caller, since a kthread
+ * that allowed signals has them too, and the kthread checks are false for a
+ * user task. The request is reported apart from a signal because nothing
+ * clears it while the thread's function runs: every later park that finds no
+ * wake pending fails the same way, so the caller has to return.
+ */
+static int bpf_task_park_interrupted(void)
+{
+	if (kthread_should_stop_or_park())
+		return -ESHUTDOWN;
+	if (signal_pending(current))
+		return -EINTR;
+	return 0;
+}
+
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_task_park - Sleep until bpf_task_wake() is called for the current task
+ * @timeout_ns: relative timeout, or U64_MAX for none
+ * @flags: must be 0
+ *
+ * Returns at once if a wake is pending from a bpf_task_wake() that found the
+ * task running. Otherwise sleeps interruptibly until a wake, the timeout, a
+ * signal, or a stop or park request for a kthread. Every return past the
+ * argument check consumes the pending wake, and a wake takes precedence over
+ * a timeout, a signal or a stop request noticed at the same time, since the
+ * waker may already have accounted for it. Any return hands control back to
+ * the program's own check of its condition, and spurious returns are
+ * possible. A @timeout_ns of 0 polls: it reports a pending wake,
+ * then a stop or park request as -ESHUTDOWN, a pending signal as -EINTR, and
+ * otherwise -ETIMEDOUT, without sleeping.
+ *
+ * Return: 0 when a wake was consumed or the sleep ended for no recorded
+ * reason, -ETIMEDOUT when the timeout passed, -ESHUTDOWN when a stop or park
+ * request for a kthread is pending and -EINTR when a signal is, whether it
+ * was found before the sleep or ended it, even if the timeout passed
+ * meanwhile, -EINVAL when @flags is not 0. Nothing clears a stop or park
+ * request while the thread's function runs, so once a call has returned
+ * -ESHUTDOWN, every later one that finds no wake pending does too.
+ */
+__bpf_kfunc int bpf_task_park(u64 timeout_ns, u64 flags)
+{
+	ktime_t expires = min_t(u64, timeout_ns, KTIME_MAX);
+	bool expired;
+	int ret = 0;
+
+	if (flags)
+		return -EINVAL;
+	if (READ_ONCE(current->bpf_wake_pending))
+		goto out;
+
+	/*
+	 * set_current_state() is a full barrier that pairs with the one in
+	 * try_to_wake_up() after the waker's store of bpf_wake_pending:
+	 * either this task observes the pending wake below or the waker
+	 * observes it sleeping and wakes it.
+	 */
+	set_current_state(TASK_INTERRUPTIBLE);
+	if (READ_ONCE(current->bpf_wake_pending)) {
+		__set_current_state(TASK_RUNNING);
+		goto out;
+	}
+	/*
+	 * kthread_stop(), kthread_park() and signal delivery set their flag
+	 * and wake the task in the same way, so the same ordering covers
+	 * them.
+	 */
+	ret = bpf_task_park_interrupted();
+	if (ret) {
+		__set_current_state(TASK_RUNNING);
+		goto out;
+	}
+
+	/*
+	 * Sleepable programs run with migration disabled under a Tasks Trace
+	 * RCU read section. Neither may be held across a sleep of unbounded
+	 * length: the read section would stall every Tasks Trace grace period
+	 * in the system, and a migration-disabled sleeper keeps rq->nr_pinned
+	 * raised, which blocks CPU offline in balance_hotplug_wait() and pins
+	 * the wakeup to the CPU it slept on.
+	 *
+	 * Dropping both is correct only where the caller's runner holds the
+	 * program, and with it its maps and context, for the whole
+	 * invocation, so that the program's own memory needs no grace period,
+	 * and where nothing on the run path keeps per-CPU state across the
+	 * program. A program that keeps such state itself, a stack in arena or
+	 * the time slice of may_goto, is refused the call, and one that calls
+	 * it is kept out of prog arrays, where the caller of a tail call holds
+	 * it through this very read section. The verifier confines the call to
+	 * such contexts: the main body of a syscall program or of a BPF kthread
+	 * and the functions either calls directly, never any other callback,
+	 * and never a region that pins the CPU, a lock, a saved IRQ state or an
+	 * RCU read section. It also invalidates every pointer the program holds
+	 * across the call except its stack, its context, maps it references
+	 * directly and the values of its arrays, arena memory and
+	 * verifier-tracked references. What remains is that the program may
+	 * resume on another CPU, which is part of the contract.
+	 */
+	migrate_enable();
+	rcu_read_unlock_trace();
+	/*
+	 * A NULL expiry sleeps without a timer; a zero one returns at once.
+	 * schedule_hrtimeout() tells an expiry from an early return, but not
+	 * a wake from a signal or a stop or park request, so read the flags
+	 * again, as before the sleep. They win over an expiry that fired at
+	 * the same time, since the request stays pending and the timeout
+	 * does not; a wake, consumed below, wins over both.
+	 */
+	expired = !schedule_hrtimeout(timeout_ns == U64_MAX ? NULL : &expires, HRTIMER_MODE_REL);
+	ret = bpf_task_park_interrupted();
+	if (!ret && expired)
+		ret = -ETIMEDOUT;
+	rcu_read_lock_trace();
+	migrate_disable();
+out:
+	/*
+	 * Consume the wake. xchg() is fully ordered, so the condition loads
+	 * the program makes after the return see the stores the waker made
+	 * before its bpf_task_wake(). A wake that raced with the timeout, the
+	 * signal or the stop request wins: the waker may have accounted for it.
+	 */
+	if (xchg(&current->bpf_wake_pending, 0))
+		ret = 0;
+	return ret;
+}
+
+/**
+ * bpf_task_wake - Wake a task parked in bpf_task_park()
+ * @task: task to wake
+ * @flags: must be 0
+ *
+ * Marks a wake pending for @task and wakes it if it sleeps interruptibly.
+ * A task that is not parked keeps the pending wake until its next
+ * bpf_task_park(), which then returns at once.
+ *
+ * The wake takes the task's pi_lock and a runqueue lock, which the kernel
+ * code a program runs in or interrupts may already hold, and it may not be
+ * issued in NMI context at all. Such code and NMI context both run with IRQs
+ * disabled, so a call with IRQs disabled marks the wake pending and queues it
+ * for irq_work on the calling CPU, which issues it once IRQs are enabled
+ * again. Such a call cannot tell whether the task was asleep and returns 2.
+ *
+ * Return: 1 when @task was asleep and is now runnable, 0 when it was not, 2
+ * when the wake was deferred, -EINVAL when @flags is not 0.
+ */
+__bpf_kfunc int bpf_task_wake(struct task_struct *task, u64 flags)
+{
+	if (flags)
+		return -EINVAL;
+
+	/*
+	 * xchg() is a full barrier. It orders the program's stores before this
+	 * call, its condition, before the word, so a park that consumes the
+	 * word sees them, also when what it consumes is another waker's store
+	 * that overwrote this one. try_to_wake_up() then orders the store
+	 * before its load of the task state, pairing with set_current_state()
+	 * in bpf_task_park().
+	 */
+	xchg(&task->bpf_wake_pending, 1);
+	if (unlikely(irqs_disabled())) {
+		bpf_task_wake_defer(task);
+		return 2;
+	}
+	return wake_up_state(task, TASK_INTERRUPTIBLE);
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_task_park_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_task_park, KF_SLEEPABLE)
+BTF_KFUNCS_END(bpf_task_park_kfunc_ids)
+
+BTF_KFUNCS_START(bpf_task_wake_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_task_wake, KF_RCU)
+BTF_KFUNCS_END(bpf_task_wake_kfunc_ids)
+
+static const struct btf_kfunc_id_set bpf_task_park_kfunc_set = {
+	.owner = THIS_MODULE,
+	.set = &bpf_task_park_kfunc_ids,
+};
+
+static const struct btf_kfunc_id_set bpf_task_wake_kfunc_set = {
+	.owner = THIS_MODULE,
+	.set = &bpf_task_wake_kfunc_ids,
+};
+
+/*
+ * The park stays with syscall programs, whose runner holds the program across
+ * the sleep. The wake touches only the task and defers itself where it cannot
+ * be issued, so every program type has it, to wake a parked task from the
+ * kernel event that makes it worth waking.
+ */
+static int __init bpf_task_park_kfunc_init(void)
+{
+	int ret;
+
+	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_task_park_kfunc_set);
+	return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &bpf_task_wake_kfunc_set);
+}
+late_initcall(bpf_task_park_kfunc_init);
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 20b1b9ff574c7d..e7769999e8c377 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -537,6 +537,7 @@ static bool is_call_rcu_kfunc(u32 btf_id);
 static bool is_callback_calling_kfunc(u32 btf_id);
 
 static bool is_bpf_wq_set_callback_kfunc(u32 btf_id);
+static bool is_bpf_kthread_create_kfunc(u32 btf_id);
 static bool is_task_work_add_kfunc(u32 func_id);
 
 static bool is_sync_callback_calling_function(enum bpf_func_id func_id)
@@ -580,9 +581,10 @@ static bool is_async_cb_sleepable(struct bpf_verifier_env *env, struct bpf_insn
 	if (bpf_pseudo_kfunc_call(insn) && insn->off == 0 && is_call_rcu_kfunc(insn->imm))
 		return false;
 
-	/* bpf_wq and bpf_task_work callbacks are always sleepable. */
+	/* bpf_wq, bpf_task_work and bpf_kthread callbacks are always sleepable. */
 	if (bpf_pseudo_kfunc_call(insn) && insn->off == 0 &&
-	    (is_bpf_wq_set_callback_kfunc(insn->imm) || is_task_work_add_kfunc(insn->imm)))
+	    (is_bpf_wq_set_callback_kfunc(insn->imm) || is_task_work_add_kfunc(insn->imm) ||
+	     is_bpf_kthread_create_kfunc(insn->imm)))
 		return true;
 
 	verifier_bug(env, "unhandled async callback in is_async_cb_sleepable");
@@ -1792,6 +1794,7 @@ int bpf_copy_verifier_state(struct bpf_verifier_state *dst_state,
 	dst_state->callers_unknown = src->callers_unknown;
 	dst_state->unwound = src->unwound;
 	dst_state->changed_pkt_data = src->changed_pkt_data;
+	dst_state->parked = src->parked;
 	dst_state->flow_entry = src->flow_entry;
 	dst_state->insn_idx = src->insn_idx;
 	dst_state->curframe = src->curframe;
@@ -11222,6 +11225,136 @@ static void invalidate_non_owning_refs(struct bpf_verifier_env *env)
 	}));
 }
 
+/*
+ * bpf_task_park() is confined to the main body of the program, the body of a
+ * BPF kthread, and the functions either calls directly. Any other callback
+ * runs from kernel code that holds state of its own around the call, an
+ * iteration position, a work item or timer that owns the program only through
+ * Tasks Trace RCU, an exiting task, none of it audited against a sleep that
+ * drops the program's protection. Refuse the park in any such callback frame,
+ * synchronous, asynchronous or exception. Asynchronous callbacks are verified
+ * as a state of their own with the callback as frame 0, so the walk over the
+ * frames sees them too.
+ *
+ * The body of a BPF kthread is the exception because its caller,
+ * bpf_kthread_fn(), is the whole of the thread: it brackets the callback the
+ * way the runner of a syscall program brackets the program, keeps no state
+ * across it, and the program's release stops the thread and waits for it
+ * before anything the callback uses is freed. Callbacks nested under the
+ * body are refused as anywhere else.
+ */
+static bool in_callback_frames(struct bpf_verifier_env *env)
+{
+	struct bpf_verifier_state *vstate = env->cur_state;
+	int i;
+
+	for (i = 0; i <= vstate->curframe; i++) {
+		struct bpf_func_state *frame = vstate->frame[i];
+
+		if (frame->in_kthread_fn)
+			continue;
+		if (frame->in_callback_fn || frame->in_async_callback_fn ||
+		    frame->in_exception_callback_fn)
+			return true;
+	}
+	return false;
+}
+
+/*
+ * bpf_task_park() sleeps without the Tasks Trace RCU read section and the
+ * disabled migration that a sleepable program otherwise runs under. A dynptr
+ * or an iterator on the stack cannot simply be invalidated across such a
+ * sleep: the program must still release or destroy it, and an object that
+ * stays usable may point at memory the sleep lets go. Refuse the park while
+ * any is live, except a bpf_iter_num, whose state lives entirely on the stack
+ * and whose next and destroy touch nothing else.
+ */
+static int check_park_stack_objects(struct bpf_verifier_env *env, int insn_idx)
+{
+	struct bpf_verifier_state *vstate = env->cur_state;
+	int i, spi;
+
+	for (i = 0; i <= vstate->curframe; i++) {
+		struct bpf_func_state *frame = vstate->frame[i];
+
+		for (spi = 0; spi < bpf_stack_nr_slots(frame); spi++) {
+			struct bpf_stack_state *slot = bpf_stack_slot(frame, spi);
+			struct bpf_reg_state *st = &slot->spilled_ptr;
+
+			switch (slot->slot_type[BPF_REG_SIZE - 1]) {
+			case STACK_DYNPTR:
+				/* Report the slot the program named, not the second half. */
+				if (!st->dynptr.first_slot)
+					continue;
+				verbose(env, "bpf_task_park() with a live dynptr at frame %d fp%d\n",
+					i, -(spi + 1) * BPF_REG_SIZE);
+				break;
+			case STACK_ITER:
+				if (!st->id)
+					continue;
+				if (st->iter.btf == btf_vmlinux &&
+				    !strcmp(iter_type_str(st->iter.btf, st->iter.btf_id), "num"))
+					continue;
+				verbose(env, "bpf_task_park() with a live %s iterator at frame %d fp%d\n",
+					iter_type_str(st->iter.btf, st->iter.btf_id), i,
+					-(spi + 1) * BPF_REG_SIZE);
+				break;
+			default:
+				continue;
+			}
+			bpf_diag_res(env, insn_idx, "live stack object across bpf_task_park()",
+				     "A dynptr or iterator on the stack may refer to memory that the sleep in bpf_task_park() lets go, and the verifier cannot invalidate it without losing the path that releases its reference.",
+				     "Release or destroy the dynptr or iterator before calling bpf_task_park(), or move the park out of the loop that uses it.");
+			return -EINVAL;
+		}
+	}
+	return 0;
+}
+
+/*
+ * Invalidate what the sleep in bpf_task_park() may free. Keep only pointers
+ * whose lifetime does not rest on the dropped protection: the stack and
+ * instruction metadata; the context and the maps the program references
+ * directly, which the invocation holds; the values of its array maps, which
+ * the map allocates and frees with itself, and which hold the program's
+ * global variables; arena memory, which the arena map holds and whose
+ * accesses tolerate faults; and pointers backed by an explicit
+ * verifier-tracked reference, found through their own id or the id of their
+ * parent. An inner map found through a map-in-map lookup is not kept, nor
+ * are its values: the outer map's element alone holds it, and its deletion
+ * frees it after a grace period the sleeping program no longer delays.
+ * Everything else, values of maps that free elements on delete or on their
+ * owner's teardown, RCU and trusted pointers without a reference, per-CPU
+ * and local storage pointers, is gone after the call.
+ */
+static void invalidate_regs_across_park(struct bpf_verifier_env *env)
+{
+	struct bpf_stack_state *stack;
+	struct bpf_func_state *state;
+	struct bpf_reg_state *reg;
+
+	bpf_for_each_reg_in_vstate_mask(env->cur_state, state, reg, stack, 1 << STACK_SPILL, ({
+		enum bpf_reg_type type = base_type(reg->type);
+
+		if (!is_pointer_regtype(reg->type))
+			continue;
+		if ((reg->id && find_reference_state(env->cur_state, reg->id)) ||
+		    (reg->parent_id && find_reference_state(env->cur_state, reg->parent_id)))
+			continue;
+		if ((type == CONST_PTR_TO_MAP || type == PTR_TO_MAP_VALUE) && !reg->map_uid &&
+		    (type == CONST_PTR_TO_MAP || reg->map_ptr->map_type == BPF_MAP_TYPE_ARRAY))
+			continue;
+		if (type == PTR_TO_STACK || type == PTR_TO_FUNC || type == PTR_TO_INSN ||
+		    type == PTR_TO_CTX || type == PTR_TO_ARENA)
+			continue;
+		bpf_diag_record_scrub(env, reg, BPF_DIAG_MOD_TASK_PARK);
+		mark_reg_invalid(env, reg);
+	}));
+	/* the frames of the callers are looked at when the function returns to them */
+	if (env->cur_state->flow_entry)
+		env->cur_state->parked = true;
+}
+
 static void invalidate_rcu_protected_refs(struct bpf_verifier_env *env)
 {
 	struct bpf_stack_state *stack;
@@ -11640,6 +11773,9 @@ static bool func_is_on_its_own(struct bpf_verifier_env *env, struct bpf_verifier
 	if (sub->changes_pkt_data || sub->has_tail_call || sub->has_ld_abs ||
 	    sub->is_cb || sub->is_async_cb || sub->is_exception_cb)
 		return false;
+	/* a park looks at every frame, and takes from each what the sleep may free */
+	if (sub->might_park)
+		return false;
 	if (state->speculative)
 		return false;
 	if (sub->releases_inherited &&
@@ -11819,6 +11955,15 @@ int bpf_flow_return(struct bpf_verifier_env *env, struct bpf_verifier_state *cal
 		env->subprog_info[callee->subprogno].changes_pkt_data = true;
 		return 1;
 	}
+	/*
+	 * Likewise for a park behind such an address. It was checked against the frames
+	 * of the function only: which caller is a callback, and what the sleep takes
+	 * from each, is found out in the context of the caller.
+	 */
+	if (exit->parked) {
+		env->subprog_info[callee->subprogno].might_park = true;
+		return 1;
+	}
 
 	for (i = 0; i < CALLER_SAVED_REGS; i++)
 		bpf_mark_reg_not_init(env, &frame->regs[caller_saved[i]]);
@@ -12012,6 +12157,26 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 			return err;
 		}
 
+		/*
+		 * A park inside the callee is checked when the callee is verified,
+		 * but only against the callee's own frame. Apply the rules for the
+		 * frames the callee cannot see here, at the call.
+		 */
+		if (env->subprog_info[subprog].might_park) {
+			if (in_callback_frames(env)) {
+				verbose(env, "global function %s() may call bpf_task_park(), which is not allowed in a callback\n",
+					sub_name);
+				operation = bpf_diag_fmt(env, "global function %s() that may call bpf_task_park()",
+							 sub_name);
+				bpf_diag_policy(env, *insn_idx, operation, "the call is in a callback",
+						"Call the function from the main program, from the body of a BPF kthread, or from a function either calls directly, not from any other callback.");
+				return -EINVAL;
+			}
+			err = check_park_stack_objects(env, *insn_idx);
+			if (err)
+				return err;
+		}
+
 		if (env->log.level & BPF_LOG_LEVEL)
 			verbose(env, "Func#%d ('%s') is global and assumed valid.\n",
 				subprog, sub_name);
@@ -12024,6 +12189,8 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		returns_void = subprog_returns_void(env, subprog);
 		if (env->subprog_info[subprog].changes_pkt_data)
 			clear_all_pkt_pointers(env);
+		if (env->subprog_info[subprog].might_park)
+			invalidate_regs_across_park(env);
 		if (returns_void)
 			bpf_diag_record_scrub(env, &caller->regs[BPF_REG_0], BPF_DIAG_MOD_CALLER_SAVED);
 		else
@@ -12487,6 +12654,33 @@ static int set_task_work_schedule_callback_state(struct bpf_verifier_env *env,
 	return 0;
 }
 
+static int set_kthread_callback_state(struct bpf_verifier_env *env,
+				      struct bpf_func_state *caller,
+				      struct bpf_func_state *callee,
+				      int insn_idx)
+{
+	/*
+	 * bpf_kthread_create(int (callback_fn)(u64 arg), u64 arg, u64 flags);
+	 * callback_fn(u64 arg);
+	 */
+	bpf_mark_reg_unknown(&callee->regs[BPF_REG_1]);
+
+	/* unused */
+	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_2]);
+	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_3]);
+	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_4]);
+	bpf_mark_reg_not_init(env, &callee->regs[BPF_REG_5]);
+	callee->in_async_callback_fn = true;
+	callee->in_kthread_fn = true;
+	/*
+	 * The callback returns an int, which comes back in the low 32 bits of
+	 * R0: compare those, as for a global function.
+	 */
+	callee->callback_ret_range = retval_range(S32_MIN, S32_MAX);
+	callee->callback_ret_range.return_32bit = true;
+	return 0;
+}
+
 static int set_rcu_callback_state(struct bpf_verifier_env *env,
 				  struct bpf_func_state *caller,
 				  struct bpf_func_state *callee,
@@ -14170,9 +14364,14 @@ enum special_kfunc_type {
 	KF_bpf_task_work_schedule_resume,
 	KF_bpf_call_rcu,
 	KF_bpf_call_rcu_tasks_trace,
+	KF_bpf_kthread_create,
+	KF_bpf_kthread_stop,
+	KF_bpf_kthread_bind,
+	KF_bpf_kthread_attach_cgroup,
 	KF_bpf_arena_alloc_pages,
 	KF_bpf_arena_free_pages,
 	KF_bpf_session_is_return,
+	KF_bpf_task_park,
 };
 
 BTF_ID_LIST(special_kfunc_list)
@@ -14262,6 +14461,14 @@ BTF_ID(func, bpf_task_work_schedule_signal)
 BTF_ID(func, bpf_task_work_schedule_resume)
 BTF_ID(func, bpf_call_rcu)
 BTF_ID(func, bpf_call_rcu_tasks_trace)
+BTF_ID(func, bpf_kthread_create)
+BTF_ID(func, bpf_kthread_stop)
+BTF_ID(func, bpf_kthread_bind)
+#ifdef CONFIG_CGROUPS
+BTF_ID(func, bpf_kthread_attach_cgroup)
+#else
+BTF_ID_UNUSED
+#endif
 #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
 BTF_ID(func, bpf_arena_alloc_pages)
 BTF_ID(func, bpf_arena_free_pages)
@@ -14274,6 +14481,7 @@ BTF_ID(func, bpf_session_is_return)
 #else
 BTF_ID_UNUSED
 #endif
+BTF_ID(func, bpf_task_park)
 
 static bool is_bpf_cast_to_kern_ctx_kfunc(const struct bpf_call_arg_meta *meta)
 {
@@ -14297,6 +14505,21 @@ static bool is_bpf_iter_css_task_new_kfunc(const struct bpf_call_arg_meta *meta)
 	return is_kfunc_call(meta, special_kfunc_list[KF_bpf_iter_css_task_new]);
 }
 
+static bool is_bpf_task_park_kfunc(u32 btf_id)
+{
+	return btf_id == special_kfunc_list[KF_bpf_task_park];
+}
+
+/* The kfuncs that sleep or wait with migration enabled. */
+static bool is_migrating_kfunc(u32 btf_id)
+{
+	return btf_id == special_kfunc_list[KF_bpf_task_park] ||
+	       btf_id == special_kfunc_list[KF_bpf_kthread_create] ||
+	       btf_id == special_kfunc_list[KF_bpf_kthread_stop] ||
+	       btf_id == special_kfunc_list[KF_bpf_kthread_bind] ||
+	       btf_id == special_kfunc_list[KF_bpf_kthread_attach_cgroup];
+}
+
 static bool is_bpf_obj_new_kfunc(u32 func_id)
 {
 	return func_id == special_kfunc_list[KF_bpf_obj_new] ||
@@ -14414,6 +14637,11 @@ bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta)
 	       is_kfunc_dynptr_write(meta);
 }
 
+bool bpf_is_task_park_kfunc(struct bpf_call_arg_meta *meta)
+{
+	return is_kfunc_call(meta, special_kfunc_list[KF_bpf_task_park]);
+}
+
 static u32 kfunc_abi_slots(const struct btf_func_model *fm)
 {
 	const struct bpf_jit_arg_abi *abi = bpf_jit_arg_abi();
@@ -15140,6 +15368,7 @@ static bool is_sync_callback_calling_kfunc(u32 btf_id)
 static bool is_async_callback_calling_kfunc(u32 btf_id)
 {
 	return is_bpf_wq_set_callback_kfunc(btf_id) ||
+	       is_bpf_kthread_create_kfunc(btf_id) ||
 	       is_task_work_add_kfunc(btf_id) ||
 	       is_call_rcu_kfunc(btf_id);
 }
@@ -15155,6 +15384,11 @@ static bool is_bpf_wq_set_callback_kfunc(u32 btf_id)
 	return btf_id == special_kfunc_list[KF_bpf_wq_set_callback];
 }
 
+static bool is_bpf_kthread_create_kfunc(u32 btf_id)
+{
+	return btf_id == special_kfunc_list[KF_bpf_kthread_create];
+}
+
 static bool is_callback_calling_kfunc(u32 btf_id)
 {
 	return is_sync_callback_calling_kfunc(btf_id) ||
@@ -16095,6 +16329,24 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	if (!in_sleepable_context(env))
 		insn_aux->non_sleepable = true;
 
+	/*
+	 * The kfunc may return on another CPU. What a run takes on the CPU it
+	 * starts on and gives back on the CPU it ends on does not survive that.
+	 * The stack in arena is such a thing, see bpf_arena_stack_put(), and
+	 * the time slice of may_goto, which bpf_do_misc_fixups() refuses. For
+	 * the same reason the program is kept out of prog arrays, where it would
+	 * also run without a reference of its own, and away from runners that
+	 * count a run per CPU.
+	 */
+	if (is_migrating_kfunc(meta.func_id)) {
+		if (env->uses_arena_sp) {
+			verbose(env, "%s() cannot be called in a program that uses R%d\n",
+				func_name, BPF_REG_ARENA_SP);
+			return -EINVAL;
+		}
+		env->prog->aux->may_migrate = true;
+	}
+
 	/* Check the arguments */
 	err = check_func_args(env, &meta, insn_idx);
 	if (err < 0)
@@ -16159,6 +16411,16 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		}
 	}
 
+	if (is_bpf_kthread_create_kfunc(meta.func_id)) {
+		err = push_callback_call(env, insn, insn_idx, meta.subprogno,
+					 set_kthread_callback_state);
+		if (err) {
+			verbose(env, "kfunc %s#%d failed callback verification\n",
+				func_name, meta.func_id);
+			return err;
+		}
+	}
+
 	rcu_lock = is_kfunc_bpf_rcu_read_lock(&meta);
 	rcu_unlock = is_kfunc_bpf_rcu_read_unlock(&meta);
 
@@ -16266,6 +16528,19 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		}
 	}
 
+	if (is_bpf_task_park_kfunc(meta.func_id)) {
+		if (in_callback_frames(env)) {
+			verbose(env, "bpf_task_park() is not allowed in a callback\n");
+			bpf_diag_policy(env, insn_idx, "kfunc bpf_task_park", "the call is in a callback",
+					"Call bpf_task_park() from the main program, from the body of a BPF kthread, or from a function either calls directly, not from any other callback.");
+			return -EINVAL;
+		}
+		err = check_park_stack_objects(env, insn_idx);
+		if (err)
+			return err;
+		invalidate_regs_across_park(env);
+	}
+
 	bpf_diag_record_caller_saved(env, regs);
 	bpf_diag_mod_begin(env, &regs[BPF_REG_0], NULL, BPF_DIAG_MOD_WRITE);
 	for (i = 0; i < CALLER_SAVED_REGS; i++) {
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index a284225f8dc95b..41adae216adaa4 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -3124,6 +3124,51 @@ int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
 	return ret;
 }
 
+/**
+ * cgroup_attach_kthread - move a kernel thread into a cgroup
+ * @dst_cgrp: the cgroup to move it into
+ * @task: the kernel thread
+ *
+ * For kernel threads the kernel creates on behalf of a user and accounts to a
+ * cgroup of the user's choosing rather than, as cgroup_attach_task_all() does,
+ * to the creator's own. @dst_cgrp is checked as a destination of cgroup.procs
+ * is, except for the permission check, which the caller owes: it must not be
+ * dying and, on the default hierarchy, must be able to host processes, and
+ * @task must be one the cgroup core may migrate.
+ *
+ * Return: 0 on success, -ENODEV if @dst_cgrp is being removed, -EBUSY or
+ * -EOPNOTSUPP if it cannot host processes, -EINVAL if @task may not be
+ * migrated, or the error of the migration.
+ */
+int cgroup_attach_kthread(struct cgroup *dst_cgrp, struct task_struct *task)
+{
+	enum cgroup_attach_lock_mode lock_mode;
+	int ret;
+
+	cgroup_lock();
+	if (cgroup_is_dead(dst_cgrp)) {
+		ret = -ENODEV;
+		goto out_unlock;
+	}
+	/* As cgroup_procs_write_start() refuses them: cpusets cannot move these. */
+	if (task->no_cgroup_migration || (task->flags & PF_NO_SETAFFINITY)) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+	ret = cgroup_migrate_vet_dst(dst_cgrp);
+	if (ret)
+		goto out_unlock;
+	/* The moved process's own rwsem under favordynmods, as cgroup.procs takes. */
+	lock_mode = cgroup_enable_per_threadgroup_rwsem ? CGRP_ATTACH_LOCK_PER_THREADGROUP :
+							   CGRP_ATTACH_LOCK_GLOBAL;
+	cgroup_attach_lock(lock_mode, task);
+	ret = cgroup_attach_task(dst_cgrp, task, true);
+	cgroup_attach_unlock(lock_mode, task);
+out_unlock:
+	cgroup_unlock();
+	return ret;
+}
+
 struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,
 					     enum cgroup_attach_lock_mode *lock_mode)
 {
diff --git a/kernel/fork.c b/kernel/fork.c
index 8b108ed371529c..a272d11c6cafcc 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1019,6 +1019,8 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
 #ifdef CONFIG_BPF_SYSCALL
 	RCU_INIT_POINTER(tsk->bpf_storage, NULL);
 	tsk->bpf_ctx = NULL;
+	tsk->bpf_wake_pending = 0;
+	tsk->bpf_wake_node.next = NULL;
 #endif
 	return tsk;
 
diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h
index 2893bf06ff25d4..49d7a45f970716 100644
--- a/tools/testing/selftests/bpf/bpf_experimental.h
+++ b/tools/testing/selftests/bpf/bpf_experimental.h
@@ -351,6 +351,18 @@ extern void bpf_iter_css_destroy(struct bpf_iter_css *it) __weak __ksym;
 extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;
 extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;
 
+extern int bpf_task_park(__u64 timeout_ns, __u64 flags) __weak __ksym;
+extern int bpf_task_wake(struct task_struct *task, __u64 flags) __weak __ksym;
+
+extern struct task_struct *bpf_kthread_create(int (callback_fn)(__u64 arg), __u64 arg,
+					      __u64 flags) __weak __ksym;
+extern int bpf_kthread_start(struct task_struct *task, __u64 flags) __weak __ksym;
+extern int bpf_kthread_bind(struct task_struct *task, __u32 cpu, __u64 flags) __weak __ksym;
+extern int bpf_kthread_attach_cgroup(struct task_struct *task, struct cgroup *cgrp,
+				     __u64 flags) __weak __ksym;
+extern int bpf_kthread_stop(struct task_struct *task, __u64 flags) __weak __ksym;
+extern bool bpf_kthread_should_stop(void) __weak __ksym;
+
 struct bpf_iter_kmem_cache;
 extern int bpf_iter_kmem_cache_new(struct bpf_iter_kmem_cache *it) __weak __ksym;
 extern struct kmem_cache *bpf_iter_kmem_cache_next(struct bpf_iter_kmem_cache *it) __weak __ksym;
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c b/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c
new file mode 100644
index 00000000000000..fabd6b9e524269
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_kthread.c
@@ -0,0 +1,641 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <test_progs.h>
+#include <sched.h>
+#include "cgroup_helpers.h"
+#include "bpf_kthread.skel.h"
+#include "verifier_bpf_kthread.skel.h"
+
+/* The context of the syscall programs, as the programs declare it. */
+struct kthread_args {
+	__u32 idx;
+	__u32 start;
+	__u32 body;
+	__u32 bind;
+	__u32 cpu;
+	__u64 cgroup_id;
+};
+
+enum body {
+	BODY_WORKER,
+	BODY_SPAWNER,
+	BODY_SELF_STOPPER,
+	BODY_RING,
+	BODY_STOP_CHECKER,
+	BODY_SELF_BINDER,
+};
+
+static int run_prog(struct bpf_program *prog, struct kthread_args *args, int *retval)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts,
+		.ctx_in = args,
+		.ctx_size_in = args ? sizeof(*args) : 0,
+	);
+	int err;
+
+	err = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts);
+	if (!err)
+		*retval = opts.retval;
+	return err;
+}
+
+static int create_thread_on(struct bpf_kthread *skel, __u32 idx, bool start, enum body body,
+			    int cpu)
+{
+	struct kthread_args args = {
+		.idx = idx, .start = start, .body = body, .bind = cpu >= 0, .cpu = cpu,
+	};
+	int err, retval;
+
+	err = run_prog(skel->progs.create_thread, &args, &retval);
+	if (!ASSERT_OK(err, "create_thread") || !ASSERT_EQ(retval, 0, "create_ret"))
+		return -1;
+	return 0;
+}
+
+static int create_thread(struct bpf_kthread *skel, __u32 idx, bool start, enum body body)
+{
+	return create_thread_on(skel, idx, start, body, -1);
+}
+
+static int bind_thread(struct bpf_kthread *skel, __u32 idx, __u32 cpu)
+{
+	struct kthread_args args = { .idx = idx, .cpu = cpu };
+	int retval;
+
+	if (!ASSERT_OK(run_prog(skel->progs.bind_thread, &args, &retval), "bind_thread"))
+		return -EFAULT;
+	return retval;
+}
+
+static int attach_cgroup(struct bpf_kthread *skel, __u32 idx, __u64 cgroup_id)
+{
+	struct kthread_args args = { .idx = idx, .cgroup_id = cgroup_id };
+	int retval;
+
+	if (!ASSERT_OK(run_prog(skel->progs.attach_cgroup, &args, &retval), "attach_cgroup"))
+		return -EFAULT;
+	return retval;
+}
+
+/* Returns what the program returned, which for stop_thread is what the stop returned. */
+static int control_thread(struct bpf_program *prog, __u32 idx)
+{
+	struct kthread_args args = { .idx = idx };
+	int retval;
+
+	if (!ASSERT_OK(run_prog(prog, &args, &retval), bpf_program__name(prog)))
+		return -EFAULT;
+	return retval;
+}
+
+#define start_thread(skel, idx) control_thread((skel)->progs.start_thread, idx)
+#define wake_thread(skel, idx) control_thread((skel)->progs.wake_thread, idx)
+#define stop_thread(skel, idx) control_thread((skel)->progs.stop_thread, idx)
+
+/* Poll until *p is nonzero, for up to two seconds. */
+static bool wait_for(__u32 *p)
+{
+	int i;
+
+	for (i = 0; i < 2000; i++) {
+		if (__atomic_load_n(p, __ATOMIC_ACQUIRE))
+			return true;
+		usleep(1000);
+	}
+	return false;
+}
+
+/* Poll until *p exceeds @seen, for up to two seconds: the thread has looped again. */
+static bool wait_past(__u32 *p, __u32 seen)
+{
+	int i;
+
+	for (i = 0; i < 2000; i++) {
+		if (__atomic_load_n(p, __ATOMIC_ACQUIRE) > seen)
+			return true;
+		usleep(1000);
+	}
+	return false;
+}
+
+/* Whether the thread sleeps: state 'S' in /proc/<tid>/stat, read past the parenthesised comm. */
+static bool thread_sleeping(__u32 tid)
+{
+	char path[64], buf[512], *p;
+	int fd, n;
+
+	snprintf(path, sizeof(path), "/proc/%u/stat", tid);
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return false;
+	n = read(fd, buf, sizeof(buf) - 1);
+	close(fd);
+	if (n <= 0)
+		return false;
+	buf[n] = 0;
+	p = strrchr(buf, ')');
+	return p && p[1] == ' ' && p[2] == 'S';
+}
+
+/* Poll until the thread sleeps, for up to two seconds. */
+static bool wait_sleeping(__u32 tid)
+{
+	int i;
+
+	for (i = 0; i < 2000; i++) {
+		if (thread_sleeping(tid))
+			return true;
+		usleep(1000);
+	}
+	return false;
+}
+
+/* The lowest and highest CPUs the test may run on; a bound thread has to run there. */
+static int allowed_cpus(int *lo, int *hi)
+{
+	cpu_set_t set;
+	int cpu;
+
+	if (sched_getaffinity(0, sizeof(set), &set))
+		return -1;
+	*lo = *hi = -1;
+	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
+		if (!CPU_ISSET(cpu, &set))
+			continue;
+		if (*lo < 0)
+			*lo = cpu;
+		*hi = cpu;
+	}
+	return *lo < 0 ? -1 : 0;
+}
+
+static bool task_exists(__u32 tid)
+{
+	char path[64];
+
+	snprintf(path, sizeof(path), "/proc/%u", tid);
+	return !access(path, F_OK);
+}
+
+/* Poll until the task has left /proc, for up to two seconds. */
+static bool wait_gone(__u32 tid)
+{
+	int i;
+
+	for (i = 0; i < 2000; i++) {
+		if (!task_exists(tid))
+			return true;
+		usleep(1000);
+	}
+	return false;
+}
+
+static int read_file(const char *path, char *buf, size_t size)
+{
+	int fd, n;
+
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return -errno;
+	n = read(fd, buf, size - 1);
+	close(fd);
+	if (n < 0)
+		return -errno;
+	buf[n] = 0;
+	return n;
+}
+
+/*
+ * The thread is named after the program that created it and its index among
+ * the program's threads. /proc shows a kernel thread's full name whatever its
+ * length; the comm the thread reads for itself is what tracepoints and perf
+ * report, and the name is short enough to fit it.
+ */
+static void check_comm(struct bpf_kthread *skel, __u32 tid, __u32 idx)
+{
+	struct bpf_prog_info info = {};
+	__u32 len = sizeof(info);
+	char path[64], buf[512], want[64];
+
+	if (!ASSERT_OK(bpf_prog_get_info_by_fd(bpf_program__fd(skel->progs.create_thread),
+					       &info, &len), "prog_info"))
+		return;
+	snprintf(want, sizeof(want), "bpfk/%u:%u", info.id, idx);
+
+	snprintf(path, sizeof(path), "/proc/%u/comm", tid);
+	if (!ASSERT_GT(read_file(path, buf, sizeof(buf)), 0, "read comm"))
+		return;
+	if (buf[strlen(buf) - 1] == '\n')
+		buf[strlen(buf) - 1] = 0;
+	ASSERT_STREQ(buf, want, "comm");
+	ASSERT_STREQ(skel->bss->comms[idx], want, "task comm");
+}
+
+/*
+ * A started thread runs its callback, which parks until woken; its return ends
+ * the thread and is what the stop reports.
+ */
+static void test_start_exit(void)
+{
+	struct bpf_kthread *skel;
+	__u32 tid;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 0, true, BODY_WORKER))
+		goto out;
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[0]), "ran"))
+		goto out;
+	tid = skel->bss->tids[0];
+	ASSERT_TRUE(task_exists(tid), "alive");
+	ASSERT_TRUE(wait_sleeping(tid), "parked");
+	check_comm(skel, tid, 0);
+
+	__atomic_store_n(&skel->bss->exit_now[0], 1, __ATOMIC_RELEASE);
+	ASSERT_GE(wake_thread(skel, 0), 0, "wake ret");
+	ASSERT_TRUE(wait_gone(tid), "exited");
+	ASSERT_EQ(stop_thread(skel, 0), 1, "exit code");
+	ASSERT_EQ(skel->bss->runs[0], 1, "runs");
+	ASSERT_EQ(stop_thread(skel, 0), -ESRCH, "slot empty");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* A stop ends a running callback through bpf_kthread_should_stop(). */
+static void test_stop_running(void)
+{
+	struct bpf_kthread *skel;
+	__u32 tid;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 1, true, BODY_WORKER))
+		goto out;
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[1]), "ran"))
+		goto out;
+	tid = skel->bss->tids[1];
+	ASSERT_EQ(stop_thread(skel, 1), 0, "stop ret");
+	ASSERT_TRUE(wait_gone(tid), "exited");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* A stop makes every park return -ESHUTDOWN, except one that has a wake to consume. */
+static void test_stop_park_rets(void)
+{
+	struct bpf_kthread *skel;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 0, true, BODY_STOP_CHECKER))
+		goto out;
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[0]), "ran"))
+		goto out;
+	ASSERT_EQ(stop_thread(skel, 0), 1, "stop ret");
+	ASSERT_EQ(skel->bss->stop_park_rets[0], -ESHUTDOWN, "park the stop ended");
+	ASSERT_EQ(skel->bss->stop_park_rets[1], -ESHUTDOWN, "poll after the stop");
+	ASSERT_EQ(skel->bss->stop_park_rets[2], 0, "park with a wake pending");
+	ASSERT_EQ(skel->bss->stop_park_rets[3], -ESHUTDOWN, "park after the wake");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* A thread stopped before it is started never runs its callback. */
+static void test_stop_parked(void)
+{
+	struct bpf_kthread *skel;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 2, false, BODY_WORKER))
+		goto out;
+	ASSERT_EQ(stop_thread(skel, 2), -EINTR, "stop ret");
+	ASSERT_EQ(skel->bss->runs[2], 0, "runs");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* A parked thread starts through an RCU-protected pointer; a second start is a no-op. */
+static void test_start_later(void)
+{
+	struct bpf_kthread *skel;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 2, false, BODY_WORKER))
+		goto out;
+	ASSERT_EQ(start_thread(skel, 2), 0, "start ret");
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[2]), "ran"))
+		goto out;
+	ASSERT_EQ(start_thread(skel, 2), 0, "second start ret");
+	ASSERT_EQ(stop_thread(skel, 2), 0, "stop ret");
+	ASSERT_EQ(skel->bss->runs[2], 1, "runs");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* A thread cannot stop itself. */
+static void test_self_stop(void)
+{
+	struct bpf_kthread *skel;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 3, true, BODY_SELF_STOPPER))
+		goto out;
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[3]), "ran"))
+		goto out;
+	ASSERT_EQ(skel->bss->self_stop_ret, -EDEADLK, "self stop ret");
+	ASSERT_EQ(stop_thread(skel, 3), 0, "stop ret");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* A thread creates and starts another thread from its callback. */
+static void test_spawn(void)
+{
+	struct bpf_kthread *skel;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 0, true, BODY_SPAWNER))
+		goto out;
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[1]), "child ran") ||
+	    !ASSERT_TRUE(wait_for(&skel->bss->runs[0]), "parent ran"))
+		goto out;
+	ASSERT_EQ(skel->bss->spawn_ret, 0, "spawn ret");
+	ASSERT_NEQ(skel->bss->tids[0], skel->bss->tids[1], "distinct threads");
+	check_comm(skel, skel->bss->tids[0], 0);
+	check_comm(skel, skel->bss->tids[1], 1);
+	ASSERT_EQ(stop_thread(skel, 1), 0, "stop child");
+	ASSERT_EQ(stop_thread(skel, 0), 0, "stop parent");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* Tasks that are no BPF kthreads, nonzero flags, and a stop before the start. */
+static void test_misuse(void)
+{
+	struct bpf_kthread *skel;
+	int retval;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (ASSERT_OK(run_prog(skel->progs.control_foreign_task, NULL, &retval), "foreign")) {
+		ASSERT_EQ(retval, 0, "foreign ret");
+		ASSERT_EQ(skel->bss->foreign_start_ret, -ESRCH, "foreign start ret");
+		ASSERT_EQ(skel->bss->foreign_stop_ret, -ESRCH, "foreign stop ret");
+		ASSERT_EQ(skel->bss->foreign_attach_ret, -ESRCH, "foreign attach ret");
+	}
+	if (ASSERT_OK(run_prog(skel->progs.misuse, NULL, &retval), "misuse"))
+		ASSERT_EQ(retval, 0, "misuse ret");
+	bpf_kthread__destroy(skel);
+}
+
+/* The program's release stops the threads it still has. */
+static void test_program_free(void)
+{
+	struct bpf_kthread *skel;
+	__u32 tid;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread(skel, 0, true, BODY_WORKER))
+		goto out;
+	if (!ASSERT_TRUE(wait_for(&skel->bss->runs[0]), "ran"))
+		goto out;
+	tid = skel->bss->tids[0];
+	bpf_kthread__destroy(skel);
+	ASSERT_TRUE(wait_gone(tid), "exited");
+	return;
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/*
+ * Threads stopping each other in a ring. The stop that would close the ring of
+ * waits is refused with -EDEADLK, the others return their successor's exit
+ * code as the ring unwinds, and every thread exits. The thread the refused
+ * stop was aimed at is the one left in its slot; the others were taken by the
+ * threads that stopped them.
+ */
+static void test_stop_ring(__u32 size)
+{
+	struct bpf_kthread *skel;
+	int refused = 0, left = 0, ret;
+	__u32 i, peer;
+
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	skel->bss->ring_size = size;
+	for (i = 0; i < size; i++)
+		if (create_thread(skel, i, false, BODY_RING))
+			goto out;
+	for (i = 0; i < size; i++)
+		if (!ASSERT_EQ(start_thread(skel, i), 0, "start ret"))
+			goto out;
+	for (i = 0; i < size; i++) {
+		if (!ASSERT_TRUE(wait_for(&skel->bss->runs[i]), "ran") ||
+		    !ASSERT_TRUE(wait_gone(skel->bss->tids[i]), "exited"))
+			goto out;
+	}
+	for (i = 0; i < size; i++) {
+		peer = i + 1 < size ? i + 1 : 0;
+		ret = skel->bss->ring_stop_ret[i];
+		if (ret == -EDEADLK)
+			refused++;
+		else
+			ASSERT_EQ(ret, peer + 1, "ring stop ret");
+	}
+	ASSERT_EQ(refused, 1, "one stop refused");
+	for (i = 0; i < size; i++) {
+		ret = stop_thread(skel, i);
+		if (ret == -ESRCH)
+			continue;
+		ASSERT_EQ(ret, i + 1, "exit code");
+		left++;
+	}
+	ASSERT_EQ(left, 1, "one thread left in its slot");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/*
+ * A thread bound before its start runs on that CPU, and the last bind wins. A
+ * thread bound while it runs moves at its next sleep, which for a body that
+ * parks is at once, since the park sleeps with migration enabled; its next
+ * iteration runs on the new CPU.
+ */
+static void test_bind(void)
+{
+	struct bpf_kthread *skel;
+	int lo, hi;
+	__u32 loops;
+
+	if (!ASSERT_OK(allowed_cpus(&lo, &hi), "allowed_cpus"))
+		return;
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (create_thread_on(skel, 0, false, BODY_WORKER, lo))
+		goto out;
+	ASSERT_EQ(bind_thread(skel, 0, -1), -EINVAL, "bind impossible cpu");
+	ASSERT_EQ(bind_thread(skel, 0, hi), 0, "bind ret");
+	ASSERT_EQ(start_thread(skel, 0), 0, "start ret");
+	if (!ASSERT_TRUE(wait_past(&skel->bss->loops[0], 0), "ran"))
+		goto out;
+	ASSERT_EQ(skel->bss->cpus[0], hi, "cpu");
+	ASSERT_EQ(bind_thread(skel, 0, lo), 0, "bind running ret");
+	loops = skel->bss->loops[0];
+	ASSERT_GE(wake_thread(skel, 0), 0, "wake ret");
+	if (ASSERT_TRUE(wait_past(&skel->bss->loops[0], loops), "looped"))
+		ASSERT_EQ(skel->bss->cpus[0], lo, "cpu after bind");
+	ASSERT_EQ(stop_thread(skel, 0), 0, "stop ret");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/*
+ * A thread binds itself: it is migratable during the call, so the bind takes
+ * effect before it returns, and the thread's first iteration runs on the CPU.
+ */
+static void test_self_bind(void)
+{
+	struct bpf_kthread *skel;
+	int lo, hi;
+
+	if (!ASSERT_OK(allowed_cpus(&lo, &hi), "allowed_cpus"))
+		return;
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	skel->bss->self_bind_cpu = lo;
+	if (create_thread(skel, 0, true, BODY_SELF_BINDER))
+		goto out;
+	if (!ASSERT_TRUE(wait_past(&skel->bss->loops[0], 0), "ran"))
+		goto out;
+	ASSERT_EQ(skel->bss->self_bind_ret, 0, "self bind ret");
+	ASSERT_EQ(skel->bss->cpus[0], lo, "cpu");
+	ASSERT_EQ(stop_thread(skel, 0), 0, "stop ret");
+out:
+	bpf_kthread__destroy(skel);
+}
+
+/* Whether cgroup.events of the cgroup reports it populated, polling until it says @populated. */
+static bool wait_populated(const char *path, bool populated)
+{
+	const char *want = populated ? "populated 1" : "populated 0";
+	char buf[256];
+	int i;
+
+	for (i = 0; i < 2000; i++) {
+		if (!read_cgroup_file(path, "cgroup.events", buf, sizeof(buf)) && strstr(buf, want))
+			return true;
+		usleep(1000);
+	}
+	return false;
+}
+
+/*
+ * A thread moved into a cgroup, parked or running, runs there from its next
+ * iteration on and populates the cgroup until it exits. The test's work
+ * directory has controllers enabled for its children, so it cannot take the
+ * thread itself.
+ */
+static void test_attach_cgroup(void)
+{
+	struct bpf_kthread *skel = NULL;
+	int fd1 = -1, fd2 = -1;
+	char subtree[256] = {};
+	__u64 cg1, cg2;
+	__u32 loops;
+
+	if (!ASSERT_OK(setup_cgroup_environment(), "setup_cgroup_environment"))
+		return;
+	fd1 = create_and_get_cgroup("/kthread1");
+	fd2 = create_and_get_cgroup("/kthread2");
+	if (!ASSERT_GE(fd1, 0, "create kthread1") || !ASSERT_GE(fd2, 0, "create kthread2"))
+		goto out;
+	cg1 = get_cgroup_id("/kthread1");
+	cg2 = get_cgroup_id("/kthread2");
+	skel = bpf_kthread__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		goto out;
+	if (create_thread(skel, 0, false, BODY_WORKER))
+		goto out;
+	/* Only a domain controller in the children makes the parent refuse processes. */
+	if (!read_cgroup_file("", "cgroup.subtree_control", subtree, sizeof(subtree)) &&
+	    (strstr(subtree, "memory") || strstr(subtree, "io")))
+		ASSERT_EQ(attach_cgroup(skel, 0, get_cgroup_id("")), -EBUSY,
+			  "attach to inner cgroup");
+	ASSERT_EQ(attach_cgroup(skel, 0, cg1), 0, "attach parked ret");
+	ASSERT_EQ(start_thread(skel, 0), 0, "start ret");
+	if (!ASSERT_TRUE(wait_past(&skel->bss->loops[0], 0), "ran"))
+		goto out;
+	ASSERT_EQ(skel->bss->cgids[0], cg1, "cgroup after start");
+	ASSERT_TRUE(wait_populated("/kthread1", true), "kthread1 populated");
+
+	ASSERT_EQ(attach_cgroup(skel, 0, cg2), 0, "attach running ret");
+	loops = skel->bss->loops[0];
+	ASSERT_GE(wake_thread(skel, 0), 0, "wake ret");
+	if (ASSERT_TRUE(wait_past(&skel->bss->loops[0], loops), "looped"))
+		ASSERT_EQ(skel->bss->cgids[0], cg2, "cgroup after move");
+	ASSERT_TRUE(wait_populated("/kthread1", false), "kthread1 empty");
+	ASSERT_TRUE(wait_populated("/kthread2", true), "kthread2 populated");
+
+	ASSERT_EQ(stop_thread(skel, 0), 0, "stop ret");
+	ASSERT_TRUE(wait_populated("/kthread2", false), "kthread2 empty");
+out:
+	bpf_kthread__destroy(skel);
+	if (fd1 >= 0)
+		close(fd1);
+	if (fd2 >= 0)
+		close(fd2);
+	cleanup_cgroup_environment();
+}
+
+void test_bpf_kthread(void)
+{
+	RUN_TESTS(verifier_bpf_kthread);
+
+	if (test__start_subtest("start_exit"))
+		test_start_exit();
+	if (test__start_subtest("stop_running"))
+		test_stop_running();
+	if (test__start_subtest("stop_park_rets"))
+		test_stop_park_rets();
+	if (test__start_subtest("stop_parked"))
+		test_stop_parked();
+	if (test__start_subtest("start_later"))
+		test_start_later();
+	if (test__start_subtest("self_stop"))
+		test_self_stop();
+	if (test__start_subtest("spawn"))
+		test_spawn();
+	if (test__start_subtest("misuse"))
+		test_misuse();
+	if (test__start_subtest("program_free"))
+		test_program_free();
+	if (test__start_subtest("stop_ring2"))
+		test_stop_ring(2);
+	if (test__start_subtest("stop_ring3"))
+		test_stop_ring(3);
+	if (test__start_subtest("bind"))
+		test_bind();
+	if (test__start_subtest("self_bind"))
+		test_self_bind();
+	if (test__start_subtest("attach_cgroup"))
+		test_attach_cgroup();
+}
diff --git a/tools/testing/selftests/bpf/prog_tests/task_park.c b/tools/testing/selftests/bpf/prog_tests/task_park.c
new file mode 100644
index 00000000000000..a479a183c69683
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/task_park.c
@@ -0,0 +1,311 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <test_progs.h>
+#include <pthread.h>
+#include <signal.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include "task_park.skel.h"
+#include "verifier_task_park.skel.h"
+#include "verifier_task_park_frames.skel.h"
+
+static int run_syscall_prog(struct bpf_program *prog, __u32 *retval)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+	int err;
+
+	err = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts);
+	if (!err)
+		*retval = opts.retval;
+	return err;
+}
+
+/* A thread blocked in a syscall program that parks. */
+struct waiter {
+	struct bpf_program *prog;
+	pthread_t thread;
+	pid_t tid;
+	__u32 retval;
+	int err;
+	int done;
+};
+
+static void *waiter_fn(void *arg)
+{
+	struct waiter *w = arg;
+
+	__atomic_store_n(&w->tid, syscall(SYS_gettid), __ATOMIC_RELEASE);
+	w->err = run_syscall_prog(w->prog, &w->retval);
+	__atomic_store_n(&w->done, 1, __ATOMIC_RELEASE);
+	return NULL;
+}
+
+static bool waiter_done(struct waiter *w)
+{
+	return __atomic_load_n(&w->done, __ATOMIC_ACQUIRE);
+}
+
+static int start_waiter(struct waiter *w, struct bpf_program *prog)
+{
+	memset(w, 0, sizeof(*w));
+	w->prog = prog;
+	return pthread_create(&w->thread, NULL, waiter_fn, w);
+}
+
+/*
+ * Whether the waiter thread sleeps in the kernel, which for this thread can
+ * only be the park: state 'S' in /proc/self/task/<tid>/stat, read past the
+ * parenthesised comm.
+ */
+static bool waiter_sleeping(struct waiter *w)
+{
+	pid_t tid = __atomic_load_n(&w->tid, __ATOMIC_ACQUIRE);
+	char path[64], buf[512], *p;
+	int fd, n;
+
+	if (!tid)
+		return false;
+	snprintf(path, sizeof(path), "/proc/self/task/%d/stat", tid);
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return false;
+	n = read(fd, buf, sizeof(buf) - 1);
+	close(fd);
+	if (n <= 0)
+		return false;
+	buf[n] = 0;
+	p = strrchr(buf, ')');
+	return p && p[1] == ' ' && p[2] == 'S';
+}
+
+/*
+ * Wake the waiter until it has returned. A wake before the waiter published
+ * itself finds no task and sets nothing; one that finds the task sets the
+ * condition, so the waiter returns 0 only after a wake run that found it,
+ * and such a run returns 0 or 1. Returns the last result that found a task,
+ * or -ESRCH if none did.
+ */
+static int wake_until_done(struct task_park *skel, struct waiter *w)
+{
+	int i, last = -ESRCH;
+	__u32 retval;
+
+	for (i = 0; i < 5000 && !waiter_done(w); i++) {
+		if (run_syscall_prog(skel->progs.wake_waiter, &retval))
+			break;
+		if ((__s32)retval >= 0)
+			last = retval;
+		usleep(1000);
+	}
+	return last;
+}
+
+static void test_timeout(struct task_park *skel)
+{
+	__u32 retval;
+
+	if (ASSERT_OK(run_syscall_prog(skel->progs.park_timeout, &retval), "park_timeout"))
+		ASSERT_EQ((__s32)retval, -ETIMEDOUT, "timeout_ret");
+}
+
+static void test_flags(struct task_park *skel)
+{
+	__u32 retval;
+
+	if (ASSERT_OK(run_syscall_prog(skel->progs.bad_flags, &retval), "bad_flags"))
+		ASSERT_EQ(retval, 0, "flags_ret");
+}
+
+static void test_pending(struct task_park *skel)
+{
+	__u32 retval;
+
+	if (ASSERT_OK(run_syscall_prog(skel->progs.park_pending, &retval), "park_pending"))
+		ASSERT_EQ((__s32)retval, -ETIMEDOUT, "pending_ret");
+}
+
+static void test_wake(struct task_park *skel)
+{
+	struct waiter w;
+	int last;
+
+	if (!ASSERT_OK(start_waiter(&w, skel->progs.park_until_woken), "pthread_create"))
+		return;
+	last = wake_until_done(skel, &w);
+	pthread_join(w.thread, NULL);
+	ASSERT_GE(last, 0, "wake_found_waiter");
+	ASSERT_OK(w.err, "park_until_woken");
+	ASSERT_EQ((__s32)w.retval, 0, "wake_park_ret");
+}
+
+/*
+ * Wake a waiter that is known to be asleep, without a timer: once the thread
+ * shows as sleeping, the one wake that follows must report that it found the
+ * task asleep.
+ */
+static void test_wake_asleep(struct task_park *skel)
+{
+	__u64 timeout = skel->data->park_timeout_ns;
+	struct waiter w;
+	__u32 retval;
+	int i;
+
+	skel->data->park_timeout_ns = ~0ULL;
+	if (!ASSERT_OK(start_waiter(&w, skel->progs.park_until_woken), "pthread_create"))
+		goto out;
+	for (i = 0; i < 5000 && !waiter_sleeping(&w); i++)
+		usleep(1000);
+	if (ASSERT_TRUE(waiter_sleeping(&w), "waiter_sleeping")) {
+		if (ASSERT_OK(run_syscall_prog(skel->progs.wake_waiter, &retval), "wake_waiter"))
+			ASSERT_EQ((__s32)retval, 1, "wake_found_asleep");
+	}
+	/* Bound any further park before draining, so a lost wake cannot hang the join. */
+	skel->data->park_timeout_ns = timeout;
+	wake_until_done(skel, &w);
+	pthread_join(w.thread, NULL);
+	ASSERT_OK(w.err, "park_until_woken");
+	ASSERT_EQ((__s32)w.retval, 0, "wake_park_ret");
+out:
+	skel->data->park_timeout_ns = timeout;
+}
+
+/*
+ * Wait until the waiter sleeps, attach the tracing program @prog, let @fire
+ * raise its event for this thread, and check that the program's wake, not the
+ * syscall drain, ended the park and returned @expected_ret. The program
+ * attaches only once the waiter sleeps, since the polling sleeps of this
+ * thread are events of its own, and the drain runs only if the event failed.
+ */
+static void test_wake_tracing(struct task_park *skel, struct bpf_program *prog,
+			      int (*fire)(void), int expected_ret)
+{
+	__u64 timeout = skel->data->park_timeout_ns;
+	struct bpf_link *link = NULL;
+	struct waiter w;
+	bool woken;
+	int i;
+
+	skel->data->park_timeout_ns = ~0ULL;
+	skel->bss->waker_tid = syscall(SYS_gettid);
+	skel->data->tracing_wake_ret = -1;
+	if (!ASSERT_OK(start_waiter(&w, skel->progs.park_until_woken), "pthread_create"))
+		goto out;
+	for (i = 0; i < 5000 && !waiter_sleeping(&w); i++)
+		usleep(1000);
+	if (ASSERT_TRUE(waiter_sleeping(&w), "waiter_sleeping")) {
+		link = bpf_program__attach(prog);
+		if (ASSERT_OK_PTR(link, "attach"))
+			ASSERT_OK(fire(), "fire");
+	}
+	for (i = 0; i < 5000 && !waiter_done(&w); i++)
+		usleep(1000);
+	woken = waiter_done(&w);
+	bpf_link__destroy(link);
+	/* Bound any further park before draining, so a lost wake cannot hang the join. */
+	skel->data->park_timeout_ns = timeout;
+	if (!woken)
+		wake_until_done(skel, &w);
+	pthread_join(w.thread, NULL);
+	ASSERT_TRUE(woken, "woken_by_tracing_prog");
+	ASSERT_EQ(skel->data->tracing_wake_ret, expected_ret, "tracing_wake_ret");
+	ASSERT_OK(w.err, "park_until_woken");
+	ASSERT_EQ((__s32)w.retval, 0, "wake_park_ret");
+out:
+	skel->data->park_timeout_ns = timeout;
+}
+
+/* Renaming the thread to its own name still fires task_rename. */
+static int rename_self(void)
+{
+	char comm[16];
+
+	if (prctl(PR_GET_NAME, comm))
+		return -errno;
+	return prctl(PR_SET_NAME, comm) ? -errno : 0;
+}
+
+/* The timer that ends this sleep wakes the thread, which fires sched_waking. */
+static int sleep_self(void)
+{
+	return usleep(1000) ? -errno : 0;
+}
+
+static void sig_noop(int sig)
+{
+}
+
+/*
+ * A program that parks cannot be the target of a tail call: a prog array takes
+ * a syscall program that wakes and refuses one that parks.
+ */
+static void test_prog_array(struct task_park *skel)
+{
+	int map_fd = bpf_map__fd(skel->maps.jmp_table);
+	__u32 key = 0;
+	int fd;
+
+	fd = bpf_program__fd(skel->progs.wake_waiter);
+	ASSERT_OK(bpf_map_update_elem(map_fd, &key, &fd, BPF_ANY), "member_that_wakes");
+	fd = bpf_program__fd(skel->progs.park_timeout);
+	ASSERT_EQ(bpf_map_update_elem(map_fd, &key, &fd, BPF_ANY), -EINVAL, "member_that_parks");
+}
+
+/* A signal ends the sleep, and the park reports it as -EINTR. */
+static void test_signal(struct task_park *skel)
+{
+	struct sigaction sa = { .sa_handler = sig_noop }, old_sa;
+	struct waiter w;
+	bool interrupted;
+	int i;
+
+	if (!ASSERT_OK(sigaction(SIGUSR2, &sa, &old_sa), "sigaction"))
+		return;
+	if (!ASSERT_OK(start_waiter(&w, skel->progs.park_until_woken), "pthread_create"))
+		goto out;
+	/* A signal handled before the thread enters its syscall ends nothing: resend. */
+	for (i = 0; i < 5000 && !waiter_done(&w); i++) {
+		pthread_kill(w.thread, SIGUSR2);
+		usleep(1000);
+	}
+	interrupted = waiter_done(&w);
+	if (!interrupted)
+		wake_until_done(skel, &w);
+	pthread_join(w.thread, NULL);
+	ASSERT_TRUE(interrupted, "interrupted");
+	ASSERT_OK(w.err, "park_until_woken");
+	ASSERT_EQ((__s32)w.retval, -EINTR, "signal_park_ret");
+out:
+	sigaction(SIGUSR2, &old_sa, NULL);
+}
+
+void test_task_park(void)
+{
+	struct task_park *skel;
+
+	RUN_TESTS(verifier_task_park);
+	RUN_TESTS(verifier_task_park_frames);
+
+	skel = task_park__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (test__start_subtest("timeout"))
+		test_timeout(skel);
+	if (test__start_subtest("flags"))
+		test_flags(skel);
+	if (test__start_subtest("pending"))
+		test_pending(skel);
+	if (test__start_subtest("wake"))
+		test_wake(skel);
+	if (test__start_subtest("wake_asleep"))
+		test_wake_asleep(skel);
+	if (test__start_subtest("wake_tracing"))
+		test_wake_tracing(skel, skel->progs.wake_on_rename, rename_self, 1);
+	if (test__start_subtest("wake_tracing_deferred"))
+		test_wake_tracing(skel, skel->progs.wake_on_waking, sleep_self, 2);
+	if (test__start_subtest("prog_array"))
+		test_prog_array(skel);
+	if (test__start_subtest("signal"))
+		test_signal(skel);
+	task_park__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/bpf_kthread.c b/tools/testing/selftests/bpf/progs/bpf_kthread.c
new file mode 100644
index 00000000000000..3eac7366514fa0
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_kthread.c
@@ -0,0 +1,414 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "bpf_experimental.h"
+#include <bpf/bpf_helpers.h>
+#include <errno.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+#define NR_THREADS 4
+#define LOOP_BOUND (1 << 23)	/* BPF_MAX_LOOPS */
+#define PARK_TIMEOUT_NS (10ULL * 1000 * 1000 * 1000)
+
+struct thread_slot {
+	struct task_struct __kptr *task;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, NR_THREADS);
+	__type(key, __u32);
+	__type(value, struct thread_slot);
+} threads SEC(".maps");
+
+/* The context of the syscall programs below. */
+struct kthread_args {
+	__u32 idx;		/* the thread's slot, and the argument of its callback */
+	__u32 start;		/* create: start the thread */
+	__u32 body;		/* create: which callback, see enum body */
+	__u32 bind;		/* create: bind the thread to cpu before starting it */
+	__u32 cpu;		/* create and bind: the CPU */
+	__u64 cgroup_id;	/* attach_cgroup: the cgroup to move the thread into */
+};
+
+enum body {
+	BODY_WORKER,		/* loop until told to return or asked to stop */
+	BODY_SPAWNER,		/* create and start thread 1 first, then as above */
+	BODY_SELF_STOPPER,	/* try to stop itself first, then as above */
+	BODY_RING,		/* stop the next thread of a ring, then return idx + 1 */
+	BODY_STOP_CHECKER,	/* park until stopped, then record what later parks return */
+	BODY_SELF_BINDER,	/* bind itself to self_bind_cpu first, then as worker */
+};
+
+__u32 runs[NR_THREADS];		/* callback entries, per slot */
+__u32 tids[NR_THREADS];		/* the thread's PID, per slot */
+char comms[NR_THREADS][16];	/* the comm the thread read for itself, per slot */
+__u32 cpus[NR_THREADS];		/* the CPU the thread last ran on, per slot */
+__u32 loops[NR_THREADS];	/* iterations of the body's loop, per slot */
+__u64 cgids[NR_THREADS];	/* the cgroup the thread last ran in, per slot */
+__u32 exit_now[NR_THREADS];	/* set by the test: the callback returns idx + 1 */
+int self_stop_ret;		/* bpf_kthread_stop() on the calling thread */
+int spawn_ret;			/* bpf_kthread_start() by the spawner */
+int foreign_start_ret;		/* bpf_kthread_start() on a task that is no BPF kthread */
+int foreign_stop_ret;		/* bpf_kthread_stop() on the same */
+int foreign_attach_ret;		/* bpf_kthread_attach_cgroup() on the same */
+__u32 ring_size;		/* set by the test: threads in the ring */
+__u32 ring_ready;		/* ring threads about to stop their successor */
+int ring_stop_ret[NR_THREADS];	/* what stopping the successor returned, per slot */
+int stop_park_rets[4];		/* what the park returned once a stop was pending */
+__u32 self_bind_cpu;		/* set by the test: where the self binder binds itself */
+int self_bind_ret;		/* what that bind returned */
+
+static __u32 load(__u32 *p)
+{
+	return *(volatile __u32 *)p;
+}
+
+/*
+ * The body of a thread: record who runs it, then sleep in bpf_task_park()
+ * until the test wakes it to return or bpf_kthread_stop() asks it to stop. The
+ * park's timeout is ten seconds, well past what a test waits, so that a lost
+ * wake fails the test instead of passing on the timeout. The bounded loop
+ * outlasts any test; a callback cannot loop without a bound.
+ */
+static int worker(__u64 arg)
+{
+	__u64 idx = arg;
+
+	if (idx >= NR_THREADS)
+		return -EINVAL;
+	tids[idx] = bpf_get_current_pid_tgid();
+	bpf_get_current_comm(comms[idx], sizeof(comms[idx]));
+	__sync_fetch_and_add(&runs[idx], 1);
+
+	bpf_repeat(LOOP_BOUND) {
+		cpus[idx] = bpf_get_smp_processor_id();
+		cgids[idx] = bpf_get_current_cgroup_id();
+		__sync_fetch_and_add(&loops[idx], 1);
+		if (bpf_kthread_should_stop())
+			return 0;
+		if (load(&exit_now[idx]))
+			return idx + 1;
+		bpf_task_park(PARK_TIMEOUT_NS, 0);
+	}
+	return -ETIME;
+}
+
+/*
+ * What bpf_task_park() returns to a thread that is being stopped: -ESHUTDOWN
+ * from the park that the stop ends or finds, and from every later one, a poll
+ * included, except one that finds a wake pending, which wins as it does over a
+ * signal. The stop is pending for good, so none of this depends on where the
+ * thread was when it arrived.
+ */
+static int stop_checker(__u64 arg)
+{
+	__u64 idx = arg;
+	int ret = 0;
+
+	if (idx >= NR_THREADS)
+		return -EINVAL;
+	__sync_fetch_and_add(&runs[idx], 1);
+
+	bpf_repeat(LOOP_BOUND) {
+		ret = bpf_task_park(PARK_TIMEOUT_NS, 0);
+		if (ret == -ESHUTDOWN)
+			break;
+	}
+	stop_park_rets[0] = ret;
+	stop_park_rets[1] = bpf_task_park(0, 0);
+	bpf_task_wake(bpf_get_current_task_btf(), 0);
+	stop_park_rets[2] = bpf_task_park(PARK_TIMEOUT_NS, 0);
+	stop_park_rets[3] = bpf_task_park(PARK_TIMEOUT_NS, 0);
+	return idx + 1;
+}
+
+/* Publish @task in slot @idx, releasing whatever the slot held. */
+static void publish(__u32 idx, struct task_struct *task)
+{
+	struct thread_slot *slot;
+
+	slot = bpf_map_lookup_elem(&threads, &idx);
+	if (!slot) {
+		bpf_task_release(task);
+		return;
+	}
+	task = bpf_kptr_xchg(&slot->task, task);
+	if (task)
+		bpf_task_release(task);
+}
+
+/* Thread 0 of the spawn case: it creates and starts thread 1, then runs as worker 0. */
+static int spawner(__u64 arg)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(worker, 1, 0);
+	if (!task) {
+		spawn_ret = -ENOMEM;
+		return -ENOMEM;
+	}
+	spawn_ret = bpf_kthread_start(task, 0);
+	publish(1, task);
+	return worker(arg);
+}
+
+static int self_stopper(__u64 arg)
+{
+	self_stop_ret = bpf_kthread_stop(bpf_get_current_task_btf(), 0);
+	return worker(arg);
+}
+
+/*
+ * Thread @idx of a ring of ring_size threads stops thread idx + 1, the last
+ * one thread 0. Each waits until all of them are about to stop, so that the
+ * stops form a ring of waits: the stop that would close it is refused with
+ * -EDEADLK and the others complete as the ring unwinds. A thread returns
+ * idx + 1 once its stop has returned, whichever way it did.
+ */
+static int ring_stopper(__u64 arg)
+{
+	__u64 idx = arg, size = load(&ring_size);
+	struct task_struct *task = NULL, *peer_task;
+	struct thread_slot *slot;
+	__u32 peer;
+
+	if (idx >= NR_THREADS || !size)
+		return -EINVAL;
+	tids[idx] = bpf_get_current_pid_tgid();
+	__sync_fetch_and_add(&runs[idx], 1);
+
+	peer = idx + 1 < size ? idx + 1 : 0;
+	slot = bpf_map_lookup_elem(&threads, &peer);
+	if (!slot)
+		return -ENOENT;
+	bpf_rcu_read_lock();
+	peer_task = slot->task;
+	if (peer_task)
+		task = bpf_task_acquire(peer_task);
+	bpf_rcu_read_unlock();
+	if (!task)
+		return -ESRCH;
+
+	/* A stop can only arrive once every thread is at the barrier: leave it then too. */
+	__sync_fetch_and_add(&ring_ready, 1);
+	bpf_repeat(LOOP_BOUND) {
+		if (load(&ring_ready) >= size || bpf_kthread_should_stop())
+			break;
+		bpf_task_park(1000000, 0);
+	}
+	ring_stop_ret[idx] = bpf_kthread_stop(task, 0);
+	bpf_task_release(task);
+	return idx + 1;
+}
+
+/* Bind itself to self_bind_cpu, then run as a worker, which records its CPU. */
+static int self_binder(__u64 arg)
+{
+	self_bind_ret = bpf_kthread_bind(bpf_get_current_task_btf(), load(&self_bind_cpu), 0);
+	return worker(arg);
+}
+
+/* Take the thread out of slot @idx; the caller releases it. */
+static struct task_struct *take(__u32 idx)
+{
+	struct thread_slot *slot;
+
+	slot = bpf_map_lookup_elem(&threads, &idx);
+	if (!slot)
+		return NULL;
+	return bpf_kptr_xchg(&slot->task, NULL);
+}
+
+SEC("syscall")
+int create_thread(struct kthread_args *args)
+{
+	struct task_struct *task;
+	int ret;
+
+	if (args->body == BODY_SPAWNER)
+		task = bpf_kthread_create(spawner, args->idx, 0);
+	else if (args->body == BODY_SELF_STOPPER)
+		task = bpf_kthread_create(self_stopper, args->idx, 0);
+	else if (args->body == BODY_RING)
+		task = bpf_kthread_create(ring_stopper, args->idx, 0);
+	else if (args->body == BODY_STOP_CHECKER)
+		task = bpf_kthread_create(stop_checker, args->idx, 0);
+	else if (args->body == BODY_SELF_BINDER)
+		task = bpf_kthread_create(self_binder, args->idx, 0);
+	else
+		task = bpf_kthread_create(worker, args->idx, 0);
+	if (!task)
+		return -EAGAIN;
+	if (args->bind) {
+		ret = bpf_kthread_bind(task, args->cpu, 0);
+		if (ret)
+			goto stop;
+	}
+	if (args->start) {
+		ret = bpf_kthread_start(task, 0);
+		if (ret)
+			goto stop;
+	}
+	publish(args->idx, task);
+	return 0;
+stop:
+	bpf_kthread_stop(task, 0);
+	bpf_task_release(task);
+	return ret;
+}
+
+/* Start the thread of slot idx through the RCU-protected pointer in its slot. */
+SEC("syscall")
+int start_thread(struct kthread_args *args)
+{
+	struct thread_slot *slot;
+	struct task_struct *task;
+	__u32 idx = args->idx;
+	int ret = -ESRCH;
+
+	slot = bpf_map_lookup_elem(&threads, &idx);
+	if (!slot)
+		return -ENOENT;
+	bpf_rcu_read_lock();
+	task = slot->task;
+	if (task)
+		ret = bpf_kthread_start(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+/* Wake the thread of slot idx from its park, through the RCU-protected pointer in its slot. */
+SEC("syscall")
+int wake_thread(struct kthread_args *args)
+{
+	struct thread_slot *slot;
+	struct task_struct *task;
+	__u32 idx = args->idx;
+	int ret = -ESRCH;
+
+	slot = bpf_map_lookup_elem(&threads, &idx);
+	if (!slot)
+		return -ENOENT;
+	bpf_rcu_read_lock();
+	task = slot->task;
+	if (task)
+		ret = bpf_task_wake(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+/* Bind the thread of slot idx to args->cpu, running or not. */
+SEC("syscall")
+int bind_thread(struct kthread_args *args)
+{
+	struct task_struct *task;
+	int ret;
+
+	task = take(args->idx);
+	if (!task)
+		return -ESRCH;
+	ret = bpf_kthread_bind(task, args->cpu, 0);
+	publish(args->idx, task);
+	return ret;
+}
+
+/* Move the thread of slot idx into the cgroup args->cgroup_id, running or not. */
+SEC("syscall")
+int attach_cgroup(struct kthread_args *args)
+{
+	struct task_struct *task;
+	struct cgroup *cgrp;
+	int ret = -ENOENT;
+
+	task = take(args->idx);
+	if (!task)
+		return -ESRCH;
+	cgrp = bpf_cgroup_from_id(args->cgroup_id);
+	if (cgrp) {
+		ret = bpf_kthread_attach_cgroup(task, cgrp, 0);
+		bpf_cgroup_release(cgrp);
+	}
+	publish(args->idx, task);
+	return ret;
+}
+
+/* bpf_kthread_attach_cgroup() on the root cgroup with @flags. */
+static int attach_root_cgroup(struct task_struct *task, __u64 flags)
+{
+	struct cgroup *cgrp;
+	int ret;
+
+	cgrp = bpf_cgroup_from_id(1);
+	if (!cgrp)
+		return -ENOENT;
+	ret = bpf_kthread_attach_cgroup(task, cgrp, flags);
+	bpf_cgroup_release(cgrp);
+	return ret;
+}
+
+/* Stop the thread of slot idx and empty the slot; returns what the stop returned. */
+SEC("syscall")
+int stop_thread(struct kthread_args *args)
+{
+	struct task_struct *task;
+	int ret;
+
+	task = take(args->idx);
+	if (!task)
+		return -ESRCH;
+	ret = bpf_kthread_stop(task, 0);
+	bpf_task_release(task);
+	return ret;
+}
+
+/* The control kfuncs refuse a task that is no BPF kthread, init here. */
+SEC("syscall")
+int control_foreign_task(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_task_from_pid(1);
+	if (!task)
+		return -ESRCH;
+	foreign_start_ret = bpf_kthread_start(task, 0);
+	foreign_stop_ret = bpf_kthread_stop(task, 0);
+	foreign_attach_ret = attach_root_cgroup(task, 0);
+	bpf_task_release(task);
+	return 0;
+}
+
+/*
+ * A thread of a user process is never asked to stop, nonzero flags are refused
+ * everywhere, and a thread stopped before it was started reports -EINTR.
+ */
+SEC("syscall")
+int misuse(void *ctx)
+{
+	struct task_struct *task;
+	int ret;
+
+	if (bpf_kthread_should_stop())
+		return 1;
+	task = bpf_kthread_create(worker, 0, 1);
+	if (task) {
+		bpf_kthread_stop(task, 0);
+		bpf_task_release(task);
+		return 2;
+	}
+	task = bpf_kthread_create(worker, 0, 0);
+	if (!task)
+		return 3;
+	if (bpf_kthread_start(task, 1) != -EINVAL)
+		ret = 4;
+	else if (bpf_kthread_stop(task, 1) != -EINVAL)
+		ret = 5;
+	else if (attach_root_cgroup(task, 1) != -EINVAL)
+		ret = 6;
+	else
+		ret = bpf_kthread_stop(task, 0) == -EINTR ? 0 : 7;
+	bpf_task_release(task);
+	return ret;
+}
diff --git a/tools/testing/selftests/bpf/progs/task_park.c b/tools/testing/selftests/bpf/progs/task_park.c
new file mode 100644
index 00000000000000..2320ede02cc9d4
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/task_park.c
@@ -0,0 +1,218 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "bpf_experimental.h"
+#include <errno.h>
+
+/* Exercise bpf_task_park() from syscall programs, bpf_task_wake() from those and tracing ones. */
+char _license[] SEC("license") = "GPL";
+
+extern struct task_struct *bpf_task_acquire(struct task_struct *task) __ksym;
+extern void bpf_task_release(struct task_struct *task) __ksym;
+extern void bpf_rcu_read_lock(void) __ksym;
+extern void bpf_rcu_read_unlock(void) __ksym;
+
+#define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
+#define WRITE_ONCE(x, v) (*(volatile typeof(x) *)&(x) = (v))
+
+enum { WAITING, WOKEN };
+
+/* The waiter publishes itself here; the waker sets the condition and wakes it. */
+struct park_elem {
+	struct task_struct __kptr *task;
+	__u32 state;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct park_elem);
+} park_map SEC(".maps");
+
+/* A tail call would run its target under the caller's protection, which a park leaves. */
+struct {
+	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+	__uint(max_entries, 1);
+	__uint(key_size, sizeof(__u32));
+	__uint(value_size, sizeof(__u32));
+} jmp_table SEC(".maps");
+
+/* Bound on each sleep of the waiter, so that a lost wake fails a test instead of hanging it. */
+__u64 park_timeout_ns = 10ULL * 1000 * 1000 * 1000;
+
+static __always_inline struct park_elem *lookup_elem(void)
+{
+	__u32 key = 0;
+
+	return bpf_map_lookup_elem(&park_map, &key);
+}
+
+/* Reset the condition, then publish the task: a wake between the two sets it again. */
+static int publish_waiter(void)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+
+	if (!elem)
+		return -ENOENT;
+	WRITE_ONCE(elem->state, WAITING);
+	task = bpf_task_acquire(bpf_get_current_task_btf());
+	if (!task)
+		return -ESRCH;
+	task = bpf_kptr_xchg(&elem->task, task);
+	if (task)
+		bpf_task_release(task);
+	return 0;
+}
+
+static void withdraw_waiter(void)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+
+	if (!elem)
+		return;
+	task = bpf_kptr_xchg(&elem->task, NULL);
+	if (task)
+		bpf_task_release(task);
+}
+
+/*
+ * Sleep until wake_waiter() has set the condition. The element is a value of
+ * an array map, which the program holds, so its pointer survives the park and
+ * the loop keeps it; a value of a hash map would have to be looked up again
+ * on every turn. A park that returns 0 before the condition is set is a
+ * spurious wakeup that just goes round once more.
+ */
+SEC("syscall")
+int park_until_woken(void *ctx)
+{
+	struct park_elem *elem;
+	int i, ret;
+
+	ret = publish_waiter();
+	if (ret)
+		return ret;
+	elem = lookup_elem();
+	if (!elem)
+		return -ENOENT;
+	ret = -EAGAIN;
+	for (i = 0; i < 100; i++) {
+		if (READ_ONCE(elem->state) == WOKEN) {
+			ret = 0;
+			break;
+		}
+		ret = bpf_task_park(park_timeout_ns, 0);
+		if (ret)
+			break;
+		ret = -EAGAIN;
+	}
+	withdraw_waiter();
+	return ret;
+}
+
+/*
+ * Wake whoever published itself, without taking a reference. The condition is
+ * set only once a task is found: a waiter between its reset of the condition
+ * and its publication must not find it set, or it would return without any
+ * wake having reached it. Setting the condition before the wake is the order
+ * the kfunc's release store pairs with, so a park that consumes this wake
+ * sees the condition on its next look.
+ */
+SEC("syscall")
+int wake_waiter(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+	int ret = -ESRCH;
+
+	if (!elem)
+		return -ENOENT;
+	bpf_rcu_read_lock();
+	task = elem->task;
+	if (task) {
+		WRITE_ONCE(elem->state, WOKEN);
+		ret = bpf_task_wake(task, 0);
+	}
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+/* Set by the test: the thread whose events wake the waiter. */
+__u32 waker_tid;
+/* What the last wake from a tracing program returned. */
+int tracing_wake_ret = -1;
+
+/*
+ * The same wake from a tracing program, on an event of the thread the test
+ * names. The task kptr read under bpf_rcu_read_lock() is what bpf_task_wake()
+ * accepts, so the event takes no reference.
+ */
+static __always_inline void wake_waiter_on_event(struct task_struct *task)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *waiter;
+
+	if (!elem || task->pid != waker_tid)
+		return;
+	bpf_rcu_read_lock();
+	waiter = elem->task;
+	if (waiter) {
+		WRITE_ONCE(elem->state, WOKEN);
+		tracing_wake_ret = bpf_task_wake(waiter, 0);
+	}
+	bpf_rcu_read_unlock();
+}
+
+/* The wake from process context: task_rename fires from prctl(). */
+SEC("tp_btf/task_rename")
+int BPF_PROG(wake_on_rename, struct task_struct *task, const char *comm)
+{
+	wake_waiter_on_event(task);
+	return 0;
+}
+
+/*
+ * The wake from under the scheduler's locks: sched_waking fires inside
+ * try_to_wake_up() with the woken task's pi_lock held and IRQs disabled,
+ * where the wake cannot be issued, so it is queued for irq_work and returns 2.
+ */
+SEC("tp_btf/sched_waking")
+int BPF_PROG(wake_on_waking, struct task_struct *p)
+{
+	wake_waiter_on_event(p);
+	return 0;
+}
+
+/* Nothing wakes this task: the park sleeps 1 ms and times out. */
+SEC("syscall")
+int park_timeout(void *ctx)
+{
+	return bpf_task_park(1000000, 0);
+}
+
+/*
+ * A wake that finds its task running is kept for the next park, which returns
+ * at once even as a poll. That park consumes it, so a second poll finds
+ * nothing and reports the timeout.
+ */
+SEC("syscall")
+int park_pending(void *ctx)
+{
+	if (bpf_task_wake(bpf_get_current_task_btf(), 0))
+		return -EBUSY;
+	if (bpf_task_park(0, 0))
+		return -EAGAIN;
+	return bpf_task_park(0, 0);
+}
+
+SEC("syscall")
+int bad_flags(void *ctx)
+{
+	if (bpf_task_park(0, 1) != -EINVAL)
+		return 1;
+	if (bpf_task_wake(bpf_get_current_task_btf(), 1) != -EINVAL)
+		return 2;
+	return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_arena_stack.c b/tools/testing/selftests/bpf/progs/verifier_arena_stack.c
index b46e30a9c67f64..e09a9ce101876b 100644
--- a/tools/testing/selftests/bpf/progs/verifier_arena_stack.c
+++ b/tools/testing/selftests/bpf/progs/verifier_arena_stack.c
@@ -1307,6 +1307,33 @@ __naked void packet_write_prologue(void)
 	: __clobber_all);
 }
 
+extern int bpf_task_park(__u64 timeout_ns, __u64 flags) __ksym;
+
+/* the call is in asm, which gives the kfunc no BTF */
+void __park_btf_root(void)
+{
+	bpf_task_park(0, 0);
+}
+
+/* The stack goes back to the cpu that the run ends on, and a park may change that cpu. */
+SEC("syscall")
+__description("arena stack: a program that parks")
+__failure __msg("bpf_task_park() cannot be called in a program that uses R12")
+__naked void parks(void)
+{
+	asm volatile (
+	".8byte %[adj];"
+	"r1 = 0;"
+	"r2 = 0;"
+	"call %[bpf_task_park];"
+	"r0 = 0;"
+	"exit;"
+	:
+	: __imm_insn(adj, ADJ(16)),
+	  __imm(bpf_task_park)
+	: __clobber_all);
+}
+
 #endif
 
 char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_bpf_kthread.c b/tools/testing/selftests/bpf/progs/verifier_bpf_kthread.c
new file mode 100644
index 00000000000000..26f73270269892
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_bpf_kthread.c
@@ -0,0 +1,398 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "bpf_experimental.h"
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+struct slot {
+	struct task_struct __kptr *task;
+	struct task_struct __kptr_untrusted *untrusted_task;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct slot);
+} slots SEC(".maps");
+
+static struct slot *lookup_slot(void)
+{
+	__u32 key = 0;
+
+	return bpf_map_lookup_elem(&slots, &key);
+}
+
+static int body(__u64 arg)
+{
+	return 0;
+}
+
+static int body_negative(__u64 arg)
+{
+	return -1;
+}
+
+static int body_deref_arg(__u64 arg)
+{
+	return *(int *)arg;
+}
+
+static int body_spawns(__u64 arg)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body, arg, 0);
+	if (!task)
+		return -1;
+	bpf_task_release(task);
+	return 0;
+}
+
+static int body_parks(__u64 arg)
+{
+	return bpf_task_park(0, 0);
+}
+
+/* A global function that parks is admitted from the thread body as from the main program. */
+__noinline int kthread_park_global(void)
+{
+	return bpf_task_park(0, 0);
+}
+
+static int body_parks_via_global(__u64 arg)
+{
+	return kthread_park_global();
+}
+
+static int park_cb(__u32 index, void *data)
+{
+	bpf_task_park(0, 0);
+	return 1;
+}
+
+static int body_parks_in_callback(__u64 arg)
+{
+	bpf_loop(1, park_cb, NULL, 0);
+	return 0;
+}
+
+static int park_global_cb(__u32 index, void *data)
+{
+	kthread_park_global();
+	return 1;
+}
+
+static int body_parks_via_global_in_callback(__u64 arg)
+{
+	bpf_loop(1, park_global_cb, NULL, 0);
+	return 0;
+}
+
+/* The task bpf_kthread_create() returns carries a reference the program must release. */
+SEC("syscall")
+__failure __msg("Unreleased reference")
+int create_leaks_task(void *ctx)
+{
+	return bpf_kthread_create(body, 0, 0) != NULL;
+}
+
+SEC("syscall")
+__success
+int create_releases_task(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+/* The callback returns an int and is checked as one: a negative return is fine. */
+SEC("syscall")
+__success
+int callback_returns_negative(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_negative, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+/* The callback's argument is a number, whatever the creator passed. */
+SEC("syscall")
+__failure __msg("R1 invalid mem access 'scalar'")
+int callback_dereferences_arg(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_deref_arg, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+/* A thread may create threads. */
+SEC("syscall")
+__success
+int callback_creates_thread(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_spawns, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+/* The thread body may sleep in bpf_task_park(), directly or through a global function. */
+SEC("syscall")
+__success
+int callback_parks(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_parks, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+SEC("syscall")
+__success
+int callback_parks_via_global(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_parks_via_global, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+/* A callback under the thread body is a callback like any other: no park there. */
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int callback_parks_in_nested_callback(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_parks_in_callback, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("global function kthread_park_global() may call bpf_task_park(), "
+		"which is not allowed in a callback")
+int callback_parks_via_global_in_nested_callback(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_parks_via_global_in_callback, 0, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+__u32 indexed[4];
+
+/* The argument is an unknown scalar: the body bounds it before it indexes with it. */
+static int body_indexes_arg(__u64 arg)
+{
+	if (arg < 4)
+		return indexed[arg];
+	return 0;
+}
+
+SEC("syscall")
+__success
+int callback_indexes_with_arg(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_indexes_arg, 1, 0);
+	if (!task)
+		return 0;
+	bpf_task_release(task);
+	return 0;
+}
+
+/*
+ * The creation waits for kthreadd with migration enabled, so the program may
+ * go on on another CPU, where the time slice of may_goto is another run's.
+ */
+SEC("syscall")
+__failure __msg("may_goto cannot be used in a program that may migrate")
+int create_with_may_goto(void *ctx)
+{
+	struct task_struct *task;
+	int i;
+
+	for (i = 0; i < 4 && can_loop; i++) {
+		task = bpf_kthread_create(body_indexes_arg, i, 0);
+		if (task)
+			bpf_task_release(task);
+	}
+	return 0;
+}
+
+struct wq_elem {
+	struct bpf_wq wq;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct wq_elem);
+} wq_map SEC(".maps");
+
+/* Taking the address of a body that parks does not make this function park. */
+__noinline int spawn_parking_thread(void)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body_parks, 0, 0);
+	if (!task)
+		return -1;
+	bpf_task_release(task);
+	return 0;
+}
+
+static int spawn_wq_cb(void *map, int *key, void *value)
+{
+	spawn_parking_thread();
+	return 0;
+}
+
+/* So a global function that creates such a thread may be called from a callback. */
+SEC("syscall")
+__success
+int global_spawner_in_wq_callback(void *ctx)
+{
+	struct wq_elem *elem;
+	__u32 key = 0;
+
+	elem = bpf_map_lookup_elem(&wq_map, &key);
+	if (!elem)
+		return 0;
+	if (bpf_wq_init(&elem->wq, &wq_map, 0))
+		return 0;
+	return bpf_wq_set_callback(&elem->wq, spawn_wq_cb, 0);
+}
+
+/*
+ * bpf_kthread_stop() takes a referenced or trusted task; bpf_kthread_start()
+ * also an RCU-protected one that is not NULL.
+ */
+SEC("syscall")
+__failure __msg("R1 must be referenced or trusted")
+int stop_untrusted_task(void *ctx)
+{
+	struct slot *slot = lookup_slot();
+	struct task_struct *task;
+
+	if (!slot)
+		return 0;
+	task = slot->untrusted_task;
+	if (!task)
+		return 0;
+	return bpf_kthread_stop(task, 0);
+}
+
+SEC("syscall")
+__failure __msg("R1 must be a rcu pointer")
+int start_untrusted_task(void *ctx)
+{
+	struct slot *slot = lookup_slot();
+	struct task_struct *task;
+
+	if (!slot)
+		return 0;
+	task = slot->untrusted_task;
+	if (!task)
+		return 0;
+	return bpf_kthread_start(task, 0);
+}
+
+SEC("syscall")
+__failure __msg("Possibly NULL pointer passed to trusted R1")
+int start_maybe_null_task(void *ctx)
+{
+	struct slot *slot = lookup_slot();
+	struct task_struct *task;
+	int ret;
+
+	if (!slot)
+		return 0;
+	bpf_rcu_read_lock();
+	task = slot->task;
+	ret = bpf_kthread_start(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+SEC("syscall")
+__success
+int start_rcu_task(void *ctx)
+{
+	struct slot *slot = lookup_slot();
+	struct task_struct *task;
+	int ret = 0;
+
+	if (!slot)
+		return 0;
+	bpf_rcu_read_lock();
+	task = slot->task;
+	if (task)
+		ret = bpf_kthread_start(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+/* bpf_kthread_stop() sleeps, so a pointer that only an RCU section protects will not do. */
+SEC("syscall")
+__failure __msg("kernel func bpf_kthread_stop is sleepable within rcu_read_lock region")
+int stop_rcu_task(void *ctx)
+{
+	struct slot *slot = lookup_slot();
+	struct task_struct *task;
+	int ret = 0;
+
+	if (!slot)
+		return 0;
+	bpf_rcu_read_lock();
+	task = slot->task;
+	if (task)
+		ret = bpf_kthread_stop(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+SEC("tp/syscalls/sys_enter_nanosleep")
+__failure __msg("calling kernel function bpf_kthread_create is not allowed")
+int create_from_tracepoint(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_kthread_create(body, 0, 0);
+	if (task)
+		bpf_task_release(task);
+	return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_task_park.c b/tools/testing/selftests/bpf/progs/verifier_task_park.c
new file mode 100644
index 00000000000000..33bc29f0805871
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_task_park.c
@@ -0,0 +1,550 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#define BPF_NO_KFUNC_PROTOTYPES
+#include <vmlinux.h>
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+#include <bpf_arena_common.h>
+
+/* What survives a bpf_task_park(), what does not, and where the park is refused. */
+char _license[] SEC("license") = "GPL";
+
+extern struct task_struct *bpf_task_acquire(struct task_struct *task) __ksym;
+extern void bpf_task_release(struct task_struct *task) __ksym;
+extern void bpf_rcu_read_lock(void) __ksym;
+extern void bpf_rcu_read_unlock(void) __ksym;
+extern void bpf_local_irq_save(unsigned long *flags) __weak __ksym;
+extern void bpf_local_irq_restore(unsigned long *flags) __weak __ksym;
+extern int bpf_res_spin_lock(struct bpf_res_spin_lock *lock) __weak __ksym;
+extern void bpf_res_spin_unlock(struct bpf_res_spin_lock *lock) __weak __ksym;
+extern int bpf_iter_bits_new(struct bpf_iter_bits *it, const u64 *unsafe_ptr__ign,
+			     u32 nr_words) __weak __ksym;
+extern void bpf_iter_bits_destroy(struct bpf_iter_bits *it) __weak __ksym;
+
+struct park_elem {
+	struct bpf_spin_lock lock;
+	struct task_struct __kptr *task;
+	struct task_struct __kptr_untrusted *untrusted_task;
+	__u32 state;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct park_elem);
+} park_map SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct park_elem);
+} hash_map SEC(".maps");
+
+struct rlock_elem {
+	struct bpf_res_spin_lock lock;
+	__u32 state;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct rlock_elem);
+} rlock_map SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u32);
+	__array(values, struct {
+		__uint(type, BPF_MAP_TYPE_ARRAY);
+		__uint(max_entries, 1);
+		__type(key, __u32);
+		__type(value, __u32);
+	});
+} outer_map SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_RINGBUF);
+	__uint(max_entries, 4096);
+} ringbuf SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARENA);
+	__uint(map_flags, BPF_F_MMAPABLE);
+	__uint(max_entries, 2);
+} arena SEC(".maps");
+
+static __always_inline struct park_elem *lookup_elem(void)
+{
+	__u32 key = 0;
+
+	return bpf_map_lookup_elem(&park_map, &key);
+}
+
+/*
+ * A hash map frees a deleted element after a grace period the sleeper no
+ * longer delays: its value is gone after a park, in a register or spilled.
+ */
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int hash_value_after_park(void *ctx)
+{
+	struct park_elem *elem;
+	__u32 key = 0;
+
+	elem = bpf_map_lookup_elem(&hash_map, &key);
+	if (!elem)
+		return 0;
+	bpf_task_park(0, 0);
+	return elem->state;
+}
+
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+__naked int spilled_hash_value_after_park(void)
+{
+	asm volatile ("					\
+	r1 = 0;						\
+	*(u32 *)(r10 - 4) = r1;				\
+	r2 = r10;					\
+	r2 += -4;					\
+	r1 = %[hash_map] ll;				\
+	call %[bpf_map_lookup_elem];			\
+	if r0 == 0 goto l0_%=;				\
+	*(u64 *)(r10 - 16) = r0;			\
+	r1 = 0;						\
+	r2 = 0;						\
+	call bpf_task_park;				\
+	r6 = *(u64 *)(r10 - 16);			\
+	r0 = *(u32 *)(r6 + %[state_off]);		\
+l0_%=:	exit;						\
+"	:
+	: __imm(bpf_map_lookup_elem),
+	  __imm_addr(hash_map),
+	  __imm_const(state_off, offsetof(struct park_elem, state))
+	: __clobber_all);
+}
+
+/* An array allocates its values with the map, which the program holds: they survive. */
+SEC("syscall")
+__success __retval(0)
+int array_value_survives_park(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+
+	if (!elem)
+		return 1;
+	bpf_task_park(0, 0);
+	return elem->state;
+}
+
+/*
+ * Global variables are array values too, and the compiler hoists their address
+ * out of loops; whether it does is its choice, so the hoisted form is pinned in
+ * assembly as well.
+ */
+__u32 rounds = 2;
+
+SEC("syscall")
+__success __retval(0)
+int global_survives_park_in_loop(void *ctx)
+{
+	__u32 i, n = 0;
+
+	for (i = 0; i < 4 && i < rounds; i++) {
+		bpf_task_park(0, 0);
+		n++;
+	}
+	return n != 2;
+}
+
+SEC("syscall")
+__success __retval(2)
+__naked int global_pointer_survives_park(void)
+{
+	asm volatile ("					\
+	r6 = %[rounds] ll;				\
+	r1 = 0;						\
+	r2 = 0;						\
+	call bpf_task_park;				\
+	r0 = *(u32 *)(r6 + 0);				\
+	exit;						\
+"	:
+	: __imm_addr(rounds)
+	: __clobber_all);
+}
+
+/* A per-CPU value is gone: the task may resume on another CPU. */
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u32);
+} percpu_map SEC(".maps");
+
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int percpu_value_after_park(void *ctx)
+{
+	__u32 key = 0, *val;
+
+	val = bpf_map_lookup_elem(&percpu_map, &key);
+	if (!val)
+		return 0;
+	bpf_task_park(0, 0);
+	return *val;
+}
+
+/* A trusted but unreferenced pointer is gone after a park. */
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int task_pointer_after_park(void *ctx)
+{
+	struct task_struct *task = bpf_get_current_task_btf();
+
+	bpf_task_park(0, 0);
+	return task->pid;
+}
+
+/* A referenced pointer survives a park. */
+SEC("syscall")
+__success __retval(0)
+int referenced_task_survives_park(void *ctx)
+{
+	struct task_struct *task;
+
+	task = bpf_task_acquire(bpf_get_current_task_btf());
+	if (!task)
+		return 1;
+	bpf_task_park(0, 0);
+	bpf_task_release(task);
+	return 0;
+}
+
+/* The stack and the context belong to the invocation: both survive a park. */
+SEC("syscall")
+__success __retval(7)
+int stack_survives_park(void *ctx)
+{
+	volatile __u32 word = 7;
+
+	bpf_task_park(0, 0);
+	return word;
+}
+
+/*
+ * Load only: the runner refuses a context smaller than the program reads, and
+ * the loader passes none.
+ */
+SEC("syscall")
+__success
+int ctx_survives_park(__u32 *ctx)
+{
+	bpf_task_park(0, 0);
+	return *ctx;
+}
+
+/* Arena memory is held by the arena map, which the program holds: it survives a park. */
+SEC("syscall")
+__success __retval(0)
+int arena_pointer_survives_park(void *ctx)
+{
+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
+	volatile int __arena *page;
+
+	page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+	if (!page)
+		return 1;
+	*page = 7;
+	bpf_task_park(0, 0);
+	return *page != 7;
+#else
+	/* Nothing to test without arena casts; the program must still load. */
+	return 0;
+#endif
+}
+
+/* A map the program references directly is held by the invocation: its pointer survives. */
+SEC("syscall")
+__success __retval(0)
+__naked int map_pointer_survives_park(void)
+{
+	asm volatile ("					\
+	r6 = %[park_map] ll;				\
+	r1 = 0;						\
+	r2 = 0;						\
+	call bpf_task_park;				\
+	r1 = 0;						\
+	*(u32 *)(r10 - 4) = r1;				\
+	r2 = r10;					\
+	r2 += -4;					\
+	r1 = r6;					\
+	call %[bpf_map_lookup_elem];			\
+	r0 = 0;						\
+	exit;						\
+"	:
+	: __imm(bpf_map_lookup_elem),
+	  __imm_addr(park_map)
+	: __clobber_all);
+}
+
+/*
+ * An inner map is held only by the outer map's element: neither its pointer
+ * nor its values survive, even though the inner map is an array.
+ */
+SEC("syscall")
+__failure __msg("expected=map_ptr")
+int inner_map_pointer_after_park(void *ctx)
+{
+	__u32 key = 0;
+	void *inner;
+
+	inner = bpf_map_lookup_elem(&outer_map, &key);
+	if (!inner)
+		return 0;
+	bpf_task_park(0, 0);
+	return bpf_map_lookup_elem(inner, &key) != NULL;
+}
+
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int inner_array_value_after_park(void *ctx)
+{
+	__u32 key = 0, *val;
+	void *inner;
+
+	inner = bpf_map_lookup_elem(&outer_map, &key);
+	if (!inner)
+		return 0;
+	val = bpf_map_lookup_elem(inner, &key);
+	if (!val)
+		return 0;
+	bpf_task_park(0, 0);
+	return *val;
+}
+
+/* A dynptr on the stack is refused across a park, referenced or not. */
+char dynptr_buf[8];
+
+SEC("syscall")
+__failure __msg("bpf_task_park() with a live dynptr at frame 0")
+int dynptr_across_park(void *ctx)
+{
+	struct bpf_dynptr ptr;
+
+	bpf_dynptr_from_mem(dynptr_buf, sizeof(dynptr_buf), 0, &ptr);
+	bpf_task_park(0, 0);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() with a live dynptr at frame 0")
+int ringbuf_dynptr_across_park(void *ctx)
+{
+	struct bpf_dynptr ptr;
+
+	bpf_ringbuf_reserve_dynptr(&ringbuf, 8, 0, &ptr);
+	bpf_task_park(0, 0);
+	bpf_ringbuf_discard_dynptr(&ptr, 0);
+	return 0;
+}
+
+/* A number iterator lives entirely on the stack: a park inside the loop is fine. */
+SEC("syscall")
+__success __retval(0)
+int num_iterator_survives_park(void *ctx)
+{
+	int i;
+
+	bpf_for(i, 0, 3)
+		bpf_task_park(0, 0);
+	return 0;
+}
+
+/* Any other iterator is refused across a park. */
+SEC("syscall")
+__failure __msg("bpf_task_park() with a live bits iterator at frame 0")
+int bits_iterator_across_park(void *ctx)
+{
+	struct bpf_iter_bits it;
+	__u64 word = 1;
+
+	bpf_iter_bits_new(&it, &word, 1);
+	bpf_task_park(0, 0);
+	bpf_iter_bits_destroy(&it);
+	return 0;
+}
+
+/* Regions that pin the CPU or hold a lock refuse a sleepable call, the park included. */
+SEC("syscall")
+__failure __msg("function calls are not allowed while holding a lock")
+int park_while_locked(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+
+	if (!elem)
+		return 0;
+	bpf_spin_lock(&elem->lock);
+	bpf_task_park(0, 0);
+	bpf_spin_unlock(&elem->lock);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("function calls are not allowed while holding a lock")
+int park_while_res_locked(void *ctx)
+{
+	struct rlock_elem *elem;
+	__u32 key = 0;
+
+	elem = bpf_map_lookup_elem(&rlock_map, &key);
+	if (!elem)
+		return 0;
+	if (bpf_res_spin_lock(&elem->lock))
+		return 0;
+	bpf_task_park(0, 0);
+	bpf_res_spin_unlock(&elem->lock);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("kernel func bpf_task_park is sleepable within rcu_read_lock region")
+int park_in_rcu_region(void *ctx)
+{
+	bpf_rcu_read_lock();
+	bpf_task_park(0, 0);
+	bpf_rcu_read_unlock();
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("kernel func bpf_task_park is sleepable within non-preemptible region")
+int park_in_preempt_disabled_region(void *ctx)
+{
+	bpf_preempt_disable();
+	bpf_task_park(0, 0);
+	bpf_preempt_enable();
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("kernel func bpf_task_park is sleepable within IRQ-disabled region")
+int park_in_irq_disabled_region(void *ctx)
+{
+	unsigned long flags;
+
+	bpf_local_irq_save(&flags);
+	bpf_task_park(0, 0);
+	bpf_local_irq_restore(&flags);
+	return 0;
+}
+
+/*
+ * The time slice of may_goto is kept per CPU, and a program that parks may go
+ * on on another one. That holds for a may_goto the program asks for and for one
+ * the verifier adds to a loop it cannot bound.
+ */
+SEC("syscall")
+__failure __msg("may_goto cannot be used in a program that may migrate")
+int park_with_may_goto(void *ctx)
+{
+	int i;
+
+	for (i = 0; i < 4 && can_loop; i++)
+		bpf_task_park(0, 0);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("may_goto cannot be used in a program that may migrate")
+int park_in_unbounded_loop(void *ctx)
+{
+	while (*(volatile __u32 *)&rounds)
+		bpf_task_park(0, 0);
+	return 0;
+}
+
+/* The park is registered for syscall programs only. */
+SEC("tp/syscalls/sys_enter_nanosleep")
+__failure __msg("calling kernel function bpf_task_park is not allowed")
+int park_from_non_syscall(void *ctx)
+{
+	return bpf_task_park(0, 0);
+}
+
+/* The wake is registered for every program type. */
+SEC("xdp")
+__success
+int wake_from_xdp(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+
+	if (!elem)
+		return XDP_PASS;
+	bpf_rcu_read_lock();
+	task = elem->task;
+	if (task)
+		bpf_task_wake(task, 0);
+	bpf_rcu_read_unlock();
+	return XDP_PASS;
+}
+
+/* bpf_task_wake() takes a referenced, trusted or RCU-protected task that is not NULL. */
+SEC("syscall")
+__failure __msg("R1 must be a rcu pointer")
+int wake_untrusted_task(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+
+	if (!elem)
+		return 0;
+	task = elem->untrusted_task;
+	if (!task)
+		return 0;
+	return bpf_task_wake(task, 0);
+}
+
+SEC("syscall")
+__failure __msg("Possibly NULL pointer passed to trusted R1")
+int wake_maybe_null_task(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+	int ret;
+
+	if (!elem)
+		return 0;
+	bpf_rcu_read_lock();
+	task = elem->task;
+	ret = bpf_task_wake(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
+
+SEC("syscall")
+__success
+int wake_rcu_task(void *ctx)
+{
+	struct park_elem *elem = lookup_elem();
+	struct task_struct *task;
+	int ret = 0;
+
+	if (!elem)
+		return 0;
+	bpf_rcu_read_lock();
+	task = elem->task;
+	if (task)
+		ret = bpf_task_wake(task, 0);
+	bpf_rcu_read_unlock();
+	return ret;
+}
diff --git a/tools/testing/selftests/bpf/progs/verifier_task_park_frames.c b/tools/testing/selftests/bpf/progs/verifier_task_park_frames.c
new file mode 100644
index 00000000000000..f374e9051fe5e1
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_task_park_frames.c
@@ -0,0 +1,393 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include "bpf_experimental.h"
+#include <errno.h>
+#include "bpf_misc.h"
+
+/* Where a bpf_task_park() may sit in the call graph, and what its callers lose. */
+char _license[] SEC("license") = "GPL";
+
+struct park_elem {
+	__u32 state;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct park_elem);
+} park_map SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct park_elem);
+} hash_map SEC(".maps");
+
+struct wq_elem {
+	struct bpf_wq wq;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct wq_elem);
+} wq_map SEC(".maps");
+
+struct tw_elem {
+	struct bpf_task_work tw;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, struct tw_elem);
+} tw_map SEC(".maps");
+
+static __always_inline struct park_elem *lookup_hash_elem(void)
+{
+	__u32 key = 0;
+
+	return bpf_map_lookup_elem(&hash_map, &key);
+}
+
+/* A static function that parks is verified in its caller's state: the park sees every frame. */
+static __noinline int park_static(void)
+{
+	return bpf_task_park(0, 0);
+}
+
+/* A global function is verified alone: its callers rely on the might_park mark. */
+__noinline int park_global(void)
+{
+	return bpf_task_park(0, 0);
+}
+
+/* The mark reaches callers through further static and global functions. */
+static __noinline int park_via_static(void)
+{
+	return park_global();
+}
+
+__noinline int park_via_global(void)
+{
+	return park_global();
+}
+
+/* The mark reaches a caller that sleeps on its own account as well. */
+__noinline int park_and_sleep_global(void)
+{
+	char buf[8];
+
+	park_static();
+	return bpf_copy_from_user(buf, sizeof(buf), NULL);
+}
+
+/* A park in a function called directly from the main program is allowed. */
+SEC("syscall")
+__success __retval(0)
+int park_in_static_subprog(void *ctx)
+{
+	return park_static() != -ETIMEDOUT;
+}
+
+SEC("syscall")
+__success __retval(0)
+int park_in_global_subprog(void *ctx)
+{
+	return park_global() != -ETIMEDOUT;
+}
+
+/* The caller's hash map value does not survive a park in its callee, static or global. */
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int hash_value_after_static_subprog_park(void *ctx)
+{
+	struct park_elem *elem = lookup_hash_elem();
+
+	if (!elem)
+		return 0;
+	park_static();
+	return elem->state;
+}
+
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int hash_value_after_global_subprog_park(void *ctx)
+{
+	struct park_elem *elem = lookup_hash_elem();
+
+	if (!elem)
+		return 0;
+	park_global();
+	return elem->state;
+}
+
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int hash_value_after_nested_subprog_park(void *ctx)
+{
+	struct park_elem *elem = lookup_hash_elem();
+
+	if (!elem)
+		return 0;
+	park_via_static();
+	return elem->state;
+}
+
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+int hash_value_after_sleeping_subprog_park(void *ctx)
+{
+	struct park_elem *elem = lookup_hash_elem();
+
+	if (!elem)
+		return 0;
+	park_and_sleep_global();
+	return elem->state;
+}
+
+/* A dynptr held by the caller is as live across a global call that parks as across the park. */
+char dynptr_buf[8];
+
+SEC("syscall")
+__failure __msg("bpf_task_park() with a live dynptr at frame 0")
+int dynptr_across_global_subprog_park(void *ctx)
+{
+	struct bpf_dynptr ptr;
+
+	bpf_dynptr_from_mem(dynptr_buf, sizeof(dynptr_buf), 0, &ptr);
+	park_global();
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() with a live bits iterator at frame 0")
+int bits_iterator_across_global_subprog_park(void *ctx)
+{
+	struct bpf_iter_bits it;
+	__u64 word = 1;
+
+	bpf_iter_bits_new(&it, &word, 1);
+	park_global();
+	bpf_iter_bits_destroy(&it);
+	return 0;
+}
+
+/*
+ * No callback other than the body of a BPF kthread may park: not directly, not
+ * through a static or a global function.
+ */
+static int park_cb(__u32 index, void *data)
+{
+	bpf_task_park(0, 0);
+	return 1;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int park_in_loop_callback(void *ctx)
+{
+	bpf_loop(1, park_cb, NULL, 0);
+	return 0;
+}
+
+static int park_static_cb(__u32 index, void *data)
+{
+	park_static();
+	return 1;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int static_subprog_park_in_callback(void *ctx)
+{
+	bpf_loop(1, park_static_cb, NULL, 0);
+	return 0;
+}
+
+static int park_global_cb(__u32 index, void *data)
+{
+	park_global();
+	return 1;
+}
+
+SEC("syscall")
+__failure __msg("global function park_global() may call bpf_task_park(), "
+		"which is not allowed in a callback")
+int global_subprog_park_in_callback(void *ctx)
+{
+	bpf_loop(1, park_global_cb, NULL, 0);
+	return 0;
+}
+
+static int park_via_global_cb(__u32 index, void *data)
+{
+	park_via_global();
+	return 1;
+}
+
+SEC("syscall")
+__failure __msg("global function park_via_global() may call bpf_task_park(), "
+		"which is not allowed in a callback")
+int nested_global_subprog_park_in_callback(void *ctx)
+{
+	bpf_loop(1, park_via_global_cb, NULL, 0);
+	return 0;
+}
+
+static int park_for_each_cb(struct bpf_map *map, __u32 *key, struct park_elem *val, void *data)
+{
+	bpf_task_park(0, 0);
+	return 1;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int park_in_for_each_callback(void *ctx)
+{
+	bpf_for_each_map_elem(&park_map, park_for_each_cb, NULL, 0);
+	return 0;
+}
+
+#ifdef CAN_USE_CALLX
+
+/* calls_arg(fn) { return fn(); } */
+__naked __noinline __used
+static unsigned long calls_arg(void)
+{
+	asm volatile ("					\
+	callx r1;					\
+	exit;						\
+"	::: __clobber_all);
+}
+
+/*
+ * The program loads the address of the function that parks, calls_arg() calls
+ * it. Nothing marks calls_arg(), so it is verified on its own, without its
+ * callers, until its exit says that it parked. The hash map value of the
+ * program is gone after the call all the same.
+ */
+SEC("syscall")
+__failure __msg("invalid mem access 'scalar'")
+__naked int hash_value_after_callx_park(void)
+{
+	asm volatile ("					\
+	r1 = 0;						\
+	*(u32 *)(r10 - 4) = r1;				\
+	r2 = r10;					\
+	r2 += -4;					\
+	r1 = %[hash_map] ll;				\
+	call %[bpf_map_lookup_elem];			\
+	if r0 == 0 goto l0_%=;				\
+	r6 = r0;					\
+	r1 = %[park_static] ll;				\
+	call calls_arg;					\
+	r0 = *(u32 *)(r6 + %[state_off]);		\
+l0_%=:	exit;						\
+"	:
+	: __imm(bpf_map_lookup_elem),
+	  __imm_addr(hash_map),
+	  __imm_addr(park_static),
+	  __imm_const(state_off, offsetof(struct park_elem, state))
+	: __clobber_all);
+}
+
+/* Nor does the callback get to park behind calls_arg(). */
+__naked __noinline __used
+static unsigned long park_callx_cb(void)
+{
+	asm volatile ("					\
+	r1 = %[park_static] ll;				\
+	call calls_arg;					\
+	r0 = 1;						\
+	exit;						\
+"	:
+	: __imm_addr(park_static)
+	: __clobber_all);
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+__naked int callx_park_in_callback(void)
+{
+	asm volatile ("					\
+	r1 = 1;						\
+	r2 = %[park_callx_cb] ll;			\
+	r3 = 0;						\
+	r4 = 0;						\
+	call %[bpf_loop];				\
+	r0 = 0;						\
+	exit;						\
+"	:
+	: __imm(bpf_loop),
+	  __imm_addr(park_callx_cb)
+	: __clobber_all);
+}
+
+#endif /* CAN_USE_CALLX */
+
+/* An asynchronous callback is verified as its own state, with the callback as frame 0. */
+static int park_wq_cb(void *map, int *key, void *value)
+{
+	bpf_task_park(0, 0);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int park_in_wq_callback(void *ctx)
+{
+	struct wq_elem *elem;
+	__u32 key = 0;
+
+	elem = bpf_map_lookup_elem(&wq_map, &key);
+	if (!elem)
+		return 0;
+	if (bpf_wq_init(&elem->wq, &wq_map, 0))
+		return 0;
+	if (bpf_wq_set_callback(&elem->wq, park_wq_cb, 0))
+		return 0;
+	return bpf_wq_start(&elem->wq, 0);
+}
+
+static int park_task_work_cb(struct bpf_map *map, void *key, void *value)
+{
+	bpf_task_park(0, 0);
+	return 0;
+}
+
+SEC("syscall")
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int park_in_task_work_callback(void *ctx)
+{
+	struct task_struct *task = bpf_get_current_task_btf();
+	struct tw_elem *elem;
+	__u32 key = 0;
+
+	elem = bpf_map_lookup_elem(&tw_map, &key);
+	if (!elem)
+		return 0;
+	return bpf_task_work_schedule_signal(task, &elem->tw, &tw_map, park_task_work_cb);
+}
+
+/* The exception callback unwinds the program after bpf_throw(): no park there either. */
+__noinline int park_exception_cb(u64 cookie)
+{
+	bpf_task_park(0, 0);
+	return 0;
+}
+
+SEC("syscall")
+__exception_cb(park_exception_cb)
+__arch_x86_64 __arch_arm64 __arch_s390x
+__failure __msg("bpf_task_park() is not allowed in a callback")
+int park_in_exception_callback(void *ctx)
+{
+	bpf_throw(0);
+	return 0;
+}

19/3 2026/11/12 22:45 llm kmsan-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 61858
							output: 35
							thoughts: 3513
						

Thoughts:
**KMSAN Evaluation for BPF Kthreads Patch Series**

Okay, so the task is to determine if this patch series necessitates a dedicated KMSAN fuzzing run. My immediate thought is, "Let's be efficient and only flag `NeedsKMSAN=true` if it's *absolutely* required." The core of the issue hinges on potential uninitialized memory risks that *only* KMSAN would catch. I need to be certain that no other existing checks (KASAN, LOCKDEP, standard bug detectors) could identify issues introduced here.

First, I need to understand what this patch series is even doing. From the overview, it seems to be adding BPF kthreads and associated task park/wake mechanisms, along with verifier changes and cgroup integration. It touches a significant number of files, from core BPF logic to the verifier, fork and syscall handling.

My approach is to methodically go through the code, focusing on the rules given. The crucial question is: Does this patch introduce or modify anything that could lead to *uninitialized* memory being read, used, or leaked to userspace?

I'm starting by meticulously checking for kernel structures that are sent to user space via `copy_to_user`, `put_user`, netlink, etc. There's nothing obviously exposed here. The `kzalloc` calls I'm seeing everywhere for `bpf_kthread` are zero-initializing the structs. So that looks good. The task struct additions are *initialized*.

Next, I need to consider conditional logic. Does the new code have conditional execution paths depending on potentially uninitialized variables? I went through all the code and found no such places.

I'm paying close attention to allocations and initialization. Specifically, I'm verifying that all data structures are properly initialized before being read or used. Any complex structures, especially the ones touched by the verifier, need extra scrutiny. Looking through the verifier changes, there's the `set_kthread_callback_state` change where the registers R2-R5 are marked *not init* and verifier ensures R1 is scalar. I see nothing immediately that looks like an issue. `bpf_task_park` is also fully initialized.

Now, I'm trying to determine if anything is a data leak. There's nothing exposed to user space.

I'm also considering concurrency and synchronization. Are there any races, deadlocks or use-after-free bugs that could occur? My assessment is these types of errors are already caught by KASAN and lockdep.

Finally, I'm looking for any places where the patch introduces something that relies on a value being initialized to zero. I don't see anything.

In conclusion, after a very thorough review of the code, I'm confident that these changes *do not* introduce a risk of uninitialized memory usage or data leaks that would *only* be caught by KMSAN. All introduced structures are zero-initialized. I see no kernel structures copied to user space, so no uninitialized padding or field issues. While the patch introduces a lot of changes, the fundamental risks it touches (UAF, deadlocks, out-of-bounds access) are already well-covered by existing tools. Therefore, I believe `NeedsKMSAN=false`.



20/3 2026/11/12 22:47 tool grepper
0m Args:
{
  "Expression": "bpf_prog_aux",
  "PathPrefixes": [
    "kernel/bpf/core.c"
  ]
}

Results:
{
  "Output": "kernel/bpf/core.c=102=struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags)\n--\nkernel/bpf/core.c-104-\tgfp_t gfp_flags = bpf_memcg_flags(GFP_KERNEL | __GFP_ZERO | gfp_extra_flags);\nkernel/bpf/core.c:105:\tstruct bpf_prog_aux *aux;\nkernel/bpf/core.c-106-\tstruct bpf_prog *fp;\n--\nkernel/bpf/core.c=972=struct bpf_prog *bpf_prog_ksym_find(unsigned long addr)\n--\nkernel/bpf/core.c-979-\treturn ksym \u0026\u0026 ksym-\u003eprog ?\nkernel/bpf/core.c:980:\t       container_of(ksym, struct bpf_prog_aux, ksym)-\u003eprog :\nkernel/bpf/core.c-981-\t       NULL;\n--\nkernel/bpf/core.c=1768=void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)\nkernel/bpf/core.c-1769-{\nkernel/bpf/core.c:1770:\tstruct bpf_prog_aux *aux = fp-\u003eaux;\nkernel/bpf/core.c-1771-\tu32 i;\n--\nkernel/bpf/core.c=2828=static bool __bpf_prog_map_compatible(struct bpf_map *map,\n--\nkernel/bpf/core.c-2831-\tenum bpf_prog_type prog_type = resolve_prog_type(fp);\nkernel/bpf/core.c:2832:\tstruct bpf_prog_aux *aux = fp-\u003eaux;\nkernel/bpf/core.c-2833-\tenum bpf_cgroup_storage_type i;\n--\nkernel/bpf/core.c=2907=static int bpf_check_tail_call(const struct bpf_prog *fp)\nkernel/bpf/core.c-2908-{\nkernel/bpf/core.c:2909:\tstruct bpf_prog_aux *aux = fp-\u003eaux;\nkernel/bpf/core.c-2910-\tint i, ret = 0;\n--\nkernel/bpf/core.c=3387=int bpf_prog_array_copy_info(struct bpf_prog_array *array,\n--\nkernel/bpf/core.c-3406-\nkernel/bpf/core.c:3407:void __bpf_free_used_maps(struct bpf_prog_aux *aux,\nkernel/bpf/core.c-3408-\t\t\t  struct bpf_map **used_maps, u32 len)\n--\nkernel/bpf/core.c-3429-\nkernel/bpf/core.c:3430:static void bpf_free_used_maps(struct bpf_prog_aux *aux)\nkernel/bpf/core.c-3431-{\n--\nkernel/bpf/core.c=3436=void __bpf_free_used_btfs(struct btf_mod_pair *used_btfs, u32 len)\n--\nkernel/bpf/core.c-3450-\nkernel/bpf/core.c:3451:static void bpf_free_used_btfs(struct bpf_prog_aux *aux)\nkernel/bpf/core.c-3452-{\n--\nkernel/bpf/core.c=3457=static void bpf_prog_free_images(struct work_struct *work)\nkernel/bpf/core.c-3458-{\nkernel/bpf/core.c:3459:\tstruct bpf_prog_aux *aux;\nkernel/bpf/core.c-3460-\tint i;\nkernel/bpf/core.c-3461-\nkernel/bpf/core.c:3462:\taux = container_of(to_rcu_work(work), struct bpf_prog_aux, rwork);\nkernel/bpf/core.c-3463-\tfor (i = 0; i \u003c aux-\u003ereal_func_cnt; i++) {\n--\nkernel/bpf/core.c=3479=static void bpf_prog_free_deferred(struct work_struct *work)\nkernel/bpf/core.c-3480-{\nkernel/bpf/core.c:3481:\tstruct bpf_prog_aux *aux;\nkernel/bpf/core.c-3482-\tbool ksyms;\nkernel/bpf/core.c-3483-\nkernel/bpf/core.c:3484:\taux = container_of(work, struct bpf_prog_aux, work);\nkernel/bpf/core.c-3485-\t/*\n--\nkernel/bpf/core.c=3517=void bpf_prog_free(struct bpf_prog *fp)\nkernel/bpf/core.c-3518-{\nkernel/bpf/core.c:3519:\tstruct bpf_prog_aux *aux = fp-\u003eaux;\nkernel/bpf/core.c-3520-\n--\nkernel/bpf/core.c=4045=__weak void __percpu *bpf_arena_stack_percpu(struct bpf_arena *arena)\n--\nkernel/bpf/core.c-4049-\nkernel/bpf/core.c:4050:__weak u64 bpf_arena_stack_get(struct bpf_prog_aux *aux, u64 busy)\nkernel/bpf/core.c-4051-{\n--\nkernel/bpf/core.c-4054-\nkernel/bpf/core.c:4055:__weak void bpf_arena_stack_put(struct bpf_prog_aux *aux, u32 base)\nkernel/bpf/core.c-4056-{\n"
}

21/3 2026/11/12 22:47 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 67668
							output: 36
							thoughts: 104
						

Thoughts:
Okay, let's dive into this. Here's how I'd approach this code snippet, thinking out loud as it were:

**Deconstructing Memory Allocation in BPF Core**

Right, so I see a potential area for investigation relating to memory allocation within the BPF core. Specifically, I need to understand how the `gfp_flags` are being set in `kernel/bpf/core.c`, around line 104. The function `bpf_memcg_flags()` is immediately interesting; I'll need to figure out exactly how the combination of `GFP_KERNEL`, `__GFP_ZERO`, and any `gfp_extra_flags` are impacting the behavior. This is crucial because memory allocation is always a potential point of resource contention and can have serious consequences if mishandled. I need to understand what this `gfp_flags` variable is contributing and what it means for how memory is allocated.

Then, there's the `kzalloc` call, and the use of `GFP_USER | __GFP_ZERO`. Seeing `GFP_USER` makes me think about user-space interaction and potential vulnerability vectors here. I need to see how the allocation relates to potential user-space data and if it has any security implications in that context. Given this, I should probably also review the surrounding context, from lines 100-145 to gain a comprehensive understanding of the allocation strategy and its purpose. I'll need to understand what `aux` is used for and if the allocation size is safe. I'll need to think about potential race conditions and other issues.



22/3 2026/11/12 22:48 tool read-file
0m Args:
{
  "File": "kernel/bpf/core.c",
  "FirstLine": 100,
  "LineCount": 45
}

Results:
{
  "Contents": " 100:\t};\n 101:\t\n 102:\tstruct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags)\n 103:\t{\n 104:\t\tgfp_t gfp_flags = bpf_memcg_flags(GFP_KERNEL | __GFP_ZERO | gfp_extra_flags);\n 105:\t\tstruct bpf_prog_aux *aux;\n 106:\t\tstruct bpf_prog *fp;\n 107:\t\n 108:\t\tsize = round_up(size, __PAGE_SIZE);\n 109:\t\tfp = __vmalloc(size, gfp_flags);\n 110:\t\tif (fp == NULL)\n 111:\t\t\treturn NULL;\n 112:\t\n 113:\t\taux = kzalloc_obj(*aux, bpf_memcg_flags(GFP_KERNEL | gfp_extra_flags));\n 114:\t\tif (aux == NULL) {\n 115:\t\t\tvfree(fp);\n 116:\t\t\treturn NULL;\n 117:\t\t}\n 118:\t\tfp-\u003eactive = __alloc_percpu_gfp(sizeof(u8[BPF_NR_CONTEXTS]), 4,\n 119:\t\t\t\t\t\tbpf_memcg_flags(GFP_KERNEL | gfp_extra_flags));\n 120:\t\tif (!fp-\u003eactive) {\n 121:\t\t\tvfree(fp);\n 122:\t\t\tkfree(aux);\n 123:\t\t\treturn NULL;\n 124:\t\t}\n 125:\t\n 126:\t\tfp-\u003epages = size / PAGE_SIZE;\n 127:\t\tfp-\u003eaux = aux;\n 128:\t\tfp-\u003eaux-\u003emain_prog_aux = aux;\n 129:\t\tfp-\u003eaux-\u003eprog = fp;\n 130:\t\tfp-\u003ejit_requested = ebpf_jit_enabled();\n 131:\t\tfp-\u003ejit_required = IS_ENABLED(CONFIG_BPF_JIT_ALWAYS_ON);\n 132:\t\tfp-\u003eblinding_requested = bpf_jit_blinding_enabled(fp);\n 133:\t#ifdef CONFIG_CGROUP_BPF\n 134:\t\taux-\u003ecgroup_atype = CGROUP_BPF_ATTACH_TYPE_INVALID;\n 135:\t#endif\n 136:\t\n 137:\t\tINIT_LIST_HEAD_RCU(\u0026fp-\u003eaux-\u003eksym.lnode);\n 138:\t#ifdef CONFIG_FINEIBT\n 139:\t\tINIT_LIST_HEAD_RCU(\u0026fp-\u003eaux-\u003eksym_prefix.lnode);\n 140:\t#endif\n 141:\t\tINIT_LIST_HEAD(\u0026fp-\u003eaux-\u003ekthreads);\n 142:\t\tmutex_init(\u0026fp-\u003eaux-\u003eused_maps_mutex);\n 143:\t\tmutex_init(\u0026fp-\u003eaux-\u003eext_mutex);\n 144:\t\tmutex_init(\u0026fp-\u003eaux-\u003edst_mutex);\n"
}

23/3 2026/11/12 22:48 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 68930
							output: 45
							thoughts: 121
						

Thoughts:
**Examining the BPF Verifier State Copying**

Okay, so I see that `aux = kzalloc_obj(*aux, ...);` has been called. That means `aux` is now completely zero-initialized. I need to understand how the `bpf_verifier_state` and `bpf_func_state` structures are being handled, especially since this zero-initialization just happened. Let's delve into the `kernel/bpf/verifier.c` file. I need to trace this data.

Ah, here it is: `dst_state->parked = src->parked;`. This is where `dst_state` is being updated based on `src`.  So, we've got a copy or assignment happening there. I need to be sure the allocation and copying process makes sense in the broader context. I'm wondering what actually allocates these structures in the first place, specifically `bpf_verifier_state` and `bpf_func_state`. It's time to go back to `kernel/bpf/verifier.c` to trace this and confirm the memory management. I need to see the complete allocation and copy chain to confirm the integrity of the data.



24/3 2026/11/12 22:48 tool grepper
0m Args:
{
  "Expression": "kzalloc.*bpf_verifier_state|kzalloc.*bpf_func_state",
  "PathPrefixes": [
    "kernel/bpf/"
  ]
}

Results:
{
  "Output": "kernel/bpf/verifier.c=23445=static int do_check_common(struct bpf_verifier_env *env, int subprog,\n--\nkernel/bpf/verifier.c-23459-\nkernel/bpf/verifier.c:23460:\tstate = kzalloc_obj(struct bpf_verifier_state, GFP_KERNEL_ACCOUNT);\nkernel/bpf/verifier.c-23461-\tif (!state)\n--\nkernel/bpf/verifier.c-23468-\tenv-\u003eno_rcu_read_lock = context == BPF_FUNC_CTX_NO_RCU_LOCK;\nkernel/bpf/verifier.c:23469:\tstate-\u003eframe[0] = kzalloc_obj(struct bpf_func_state, GFP_KERNEL_ACCOUNT);\nkernel/bpf/verifier.c-23470-\tif (!state-\u003eframe[0]) {\n"
}

25/3 2026/11/12 22:48 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 69595
							output: 30
							thoughts: 185
						

Thoughts:
**Analyzing BPF State and Parking Mechanisms**

Okay, so I'm looking at how the BPF verifier handles state related to potential parking of BPF programs, focusing on `bpf_verifier_state` and `bpf_func_state`. It looks like both of these structures are allocated using `kzalloc_obj`, which is important because it means all the boolean flags, like `in_kthread_fn` and `parked`, are initialized to zero. That's a good starting point for ensuring a clean state.

Now, digging deeper, I see that functions like `join_func` and `join_check_state` are comparing both `in_kthread_fn` and `parked`. Also, the `bpf_state_subsumes` function seems to have the same check, ensuring state comparisons incorporate these parking related flags. And, interestingly, `parked` also plays a role in hashing within `flow_shape_ctx`, which suggests that parking status contributes to how a particular BPF program's flow is identified.

Next, I need to check how the verifier handles `subprog_info`. It looks like `subprog->might_park` is a bitfield, so I need to understand its behavior fully. How is `subprog_info` allocated and cleared? This is important. I need to trace the allocation and clearing of this data structure to see how it's handled, to fully grasp its effects in parking. I need to check `cfg.c` and `verifier.c` to look for clues. This could be where the parking behavior actually originates.



26/3 2026/11/12 22:48 tool grepper
0m Args:
{
  "Expression": "subprog_info",
  "PathPrefixes": [
    "kernel/bpf/"
  ]
}

Results:
{
  "Output": "\nFull output is too long, showing 200 out of 928 lines.\nUse more precise expression or PathPrefixes if possible.\n\nNumber of matching lines per file (11 files in total):\nkernel/bpf/btf.c:1\nkernel/bpf/cfg.c:18\nkernel/bpf/check_btf.c:9\nkernel/bpf/const_fold.c:3\nkernel/bpf/core.c:2\nkernel/bpf/diagnostics.c:3\nkernel/bpf/fixups.c:36\nkernel/bpf/flow.c:13\nkernel/bpf/liveness.c:26\nkernel/bpf/loops.c:2\nkernel/bpf/verifier.c:94\n\nkernel/bpf/btf.c=8769=int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)\n--\nkernel/bpf/btf.c-8771-\tbool is_global = subprog_aux(env, subprog)-\u003elinkage == BTF_FUNC_GLOBAL;\nkernel/bpf/btf.c:8772:\tstruct bpf_subprog_info *sub = subprog_info(env, subprog);\nkernel/bpf/btf.c-8773-\tstruct bpf_verifier_log *log = \u0026env-\u003elog;\n--\nkernel/bpf/cfg.c=54=static void mark_subprog_changes_pkt_data(struct bpf_verifier_env *env, int off)\nkernel/bpf/cfg.c-55-{\nkernel/bpf/cfg.c:56:\tstruct bpf_subprog_info *subprog;\nkernel/bpf/cfg.c-57-\n--\nkernel/bpf/cfg.c=62=static void mark_subprog_might_sleep(struct bpf_verifier_env *env, int off)\nkernel/bpf/cfg.c-63-{\nkernel/bpf/cfg.c:64:\tstruct bpf_subprog_info *subprog;\nkernel/bpf/cfg.c-65-\n--\nkernel/bpf/cfg.c=70=static void mark_subprog_might_park(struct bpf_verifier_env *env, int off)\nkernel/bpf/cfg.c-71-{\nkernel/bpf/cfg.c:72:\tstruct bpf_subprog_info *subprog;\nkernel/bpf/cfg.c-73-\n--\nkernel/bpf/cfg.c=78=static void mark_subprog_might_throw(struct bpf_verifier_env *env, int off)\nkernel/bpf/cfg.c-79-{\nkernel/bpf/cfg.c:80:\tstruct bpf_subprog_info *subprog;\nkernel/bpf/cfg.c-81-\n--\nkernel/bpf/cfg.c=94=static bool merge_callee_effects(struct bpf_verifier_env *env, int t, int w)\nkernel/bpf/cfg.c-95-{\nkernel/bpf/cfg.c:96:\tstruct bpf_subprog_info *caller, *callee;\nkernel/bpf/cfg.c-97-\tbool might_park;\n--\nkernel/bpf/cfg.c=313=static int __compute_subprog_jts(struct bpf_verifier_env *env)\nkernel/bpf/cfg.c-314-{\nkernel/bpf/cfg.c:315:\tstruct bpf_subprog_info *subprog;\nkernel/bpf/cfg.c-316-\tstruct bpf_iarray *jt, *jt_cur;\n--\nkernel/bpf/cfg.c-363-\tfor (i = 0; i \u003c env-\u003esubprog_cnt; i++) {\nkernel/bpf/cfg.c:364:\t\tjt = env-\u003esubprog_info[i].jt;\nkernel/bpf/cfg.c-365-\t\tif (jt)\n--\nkernel/bpf/cfg.c=384=void bpf_free_subprog_jts(struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-387-\nkernel/bpf/cfg.c:388:\tfor (i = 0; i \u003c ARRAY_SIZE(env-\u003esubprog_info); i++) {\nkernel/bpf/cfg.c:389:\t\tkvfree(env-\u003esubprog_info[i].jt);\nkernel/bpf/cfg.c:390:\t\tenv-\u003esubprog_info[i].jt = NULL;\nkernel/bpf/cfg.c-391-\t}\n--\nkernel/bpf/cfg.c=396=subprog_jt(int t, struct bpf_verifier_env *env)\nkernel/bpf/cfg.c-397-{\nkernel/bpf/cfg.c:398:\tstruct bpf_subprog_info *subprog;\nkernel/bpf/cfg.c-399-\tint subprog_start, err;\n--\nkernel/bpf/cfg.c=682=int bpf_check_cfg(struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-700-\tex_insn_beg = env-\u003eexception_callback_subprog\nkernel/bpf/cfg.c:701:\t\t      ? env-\u003esubprog_info[env-\u003eexception_callback_subprog].start\nkernel/bpf/cfg.c-702-\t\t      : 0;\n--\nkernel/bpf/cfg.c-769-\tclose_callee_effects(env);\nkernel/bpf/cfg.c:770:\tenv-\u003eprog-\u003eaux-\u003echanges_pkt_data = env-\u003esubprog_info[0].changes_pkt_data;\nkernel/bpf/cfg.c:771:\tenv-\u003eprog-\u003eaux-\u003emight_sleep = env-\u003esubprog_info[0].might_sleep;\nkernel/bpf/cfg.c-772-\n--\nkernel/bpf/cfg.c=780=struct dfs_state {\n--\nkernel/bpf/cfg.c-786- * For each subprogram 'i' fill array env-\u003ecfg.insn_subprogram sub-range\nkernel/bpf/cfg.c:787: * [env-\u003esubprog_info[i].postorder_start, env-\u003esubprog_info[i+1].postorder_start)\nkernel/bpf/cfg.c-788- * with indices of 'i' instructions in postorder.\n--\nkernel/bpf/cfg.c=790=int bpf_compute_postorder(struct bpf_verifier_env *env)\n--\nkernel/bpf/cfg.c-809-\tfor (subprog_idx = 0; subprog_idx \u003c env-\u003esubprog_cnt; subprog_idx++) {\nkernel/bpf/cfg.c:810:\t\tstart = env-\u003esubprog_info[subprog_idx].start;\nkernel/bpf/cfg.c:811:\t\tenv-\u003esubprog_info[subprog_idx].postorder_start = cur_postorder;\nkernel/bpf/cfg.c-812-\t\tstack[0] = start;\n--\nkernel/bpf/cfg.c-835-\t}\nkernel/bpf/cfg.c:836:\tenv-\u003esubprog_info[subprog_idx].postorder_start = cur_postorder;\nkernel/bpf/cfg.c-837-\tenv-\u003ecfg.postorder_nums = postorder_nums;\n--\nkernel/bpf/check_btf.c=10=static int check_abnormal_return(struct bpf_verifier_env *env)\n--\nkernel/bpf/check_btf.c-14-\tfor (i = 1; i \u003c env-\u003esubprog_cnt; i++) {\nkernel/bpf/check_btf.c:15:\t\tif (env-\u003esubprog_info[i].has_ld_abs) {\nkernel/bpf/check_btf.c-16-\t\t\tverbose(env, \"LD_ABS is not allowed in subprogs without BTF\\n\");\n--\nkernel/bpf/check_btf.c-18-\t\t}\nkernel/bpf/check_btf.c:19:\t\tif (env-\u003esubprog_info[i].has_tail_call) {\nkernel/bpf/check_btf.c-20-\t\t\tverbose(env, \"tail_call is not allowed in subprogs without BTF\\n\");\n--\nkernel/bpf/check_btf.c=133=static int check_btf_func(struct bpf_verifier_env *env,\n--\nkernel/bpf/check_btf.c-174-\nkernel/bpf/check_btf.c:175:\t\tif (env-\u003esubprog_info[i].start != krecord[i].insn_off) {\nkernel/bpf/check_btf.c-176-\t\t\tverbose(env, \"func_info BTF section doesn't match subprog layout in BPF program\\n\");\n--\nkernel/bpf/check_btf.c-192-\t\t\tbtf_type_is_small_int(ret_type) || btf_is_any_enum(ret_type);\nkernel/bpf/check_btf.c:193:\t\tif (i \u0026\u0026 !scalar_return \u0026\u0026 env-\u003esubprog_info[i].has_ld_abs) {\nkernel/bpf/check_btf.c-194-\t\t\tverbose(env, \"LD_ABS is only allowed in functions that return 'int' or 'void'.\\n\");\n--\nkernel/bpf/check_btf.c-196-\t\t}\nkernel/bpf/check_btf.c:197:\t\tif (i \u0026\u0026 !scalar_return \u0026\u0026 env-\u003esubprog_info[i].has_tail_call) {\nkernel/bpf/check_btf.c-198-\t\t\tverbose(env, \"tail_call is only allowed in functions that return 'int' or 'void'.\\n\");\n--\nkernel/bpf/check_btf.c-201-\nkernel/bpf/check_btf.c:202:\t\tenv-\u003esubprog_info[i].name = btf_name_by_offset(btf, type-\u003ename_off);\nkernel/bpf/check_btf.c-203-\t\tbpfptr_add(\u0026urecord, urec_size);\n--\nkernel/bpf/check_btf.c=217=static int check_btf_line(struct bpf_verifier_env *env,\n--\nkernel/bpf/check_btf.c-221-\tu32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0;\nkernel/bpf/check_btf.c:222:\tstruct bpf_subprog_info *sub;\nkernel/bpf/check_btf.c-223-\tstruct bpf_line_info *linfo;\n--\nkernel/bpf/check_btf.c-252-\ts = 0;\nkernel/bpf/check_btf.c:253:\tsub = env-\u003esubprog_info;\nkernel/bpf/check_btf.c-254-\tulinfo = make_bpfptr(attr-\u003eline_info, uattr.is_kernel);\n--\nkernel/bpf/check_btf.c-283-\t\t * first sub also and the first sub must have\nkernel/bpf/check_btf.c:284:\t\t * subprog_info[0].start == 0.\nkernel/bpf/check_btf.c-285-\t\t */\n--\nkernel/bpf/const_fold.c=243=int bpf_compute_const_regs(struct bpf_verifier_env *env)\n--\nkernel/bpf/const_fold.c-261-\tfor (i = 0; i \u003c env-\u003esubprog_cnt; i++) {\nkernel/bpf/const_fold.c:262:\t\tint start = env-\u003esubprog_info[i].start;\nkernel/bpf/const_fold.c-263-\n--\nkernel/bpf/const_fold.c-269-\t\t */\nkernel/bpf/const_fold.c:270:\t\tfor (r = 0; r \u003c MAX_BPF_REG \u0026\u0026 env-\u003esubprog_info[i].bail_insn \u003e= 0; r++)\nkernel/bpf/const_fold.c:271:\t\t\tci_in[env-\u003esubprog_info[i].bail_insn][r] = unknown;\nkernel/bpf/const_fold.c-272-\t}\n--\nkernel/bpf/core.c=1936=u16 bpf_out_stack_arg_cnt(const struct bpf_verifier_env *env, const struct bpf_prog *prog)\nkernel/bpf/core.c-1937-{\nkernel/bpf/core.c:1938:\tconst struct bpf_subprog_info *sub;\nkernel/bpf/core.c-1939-\n--\nkernel/bpf/core.c-1941-\t\treturn 0;\nkernel/bpf/core.c:1942:\tsub = \u0026env-\u003esubprog_info[prog-\u003eaux-\u003efunc_idx];\nkernel/bpf/core.c-1943-\treturn sub-\u003estack_arg_cnt - bpf_in_stack_arg_cnt(sub);\n--\nkernel/bpf/diagnostics.c=786=static __printf(4, 5) void bpf_diag_source(struct bpf_verifier_env *env,\n--\nkernel/bpf/diagnostics.c-795-\tconst struct bpf_line_info *linfo;\nkernel/bpf/diagnostics.c:796:\tconst struct bpf_subprog_info *subprog;\nkernel/bpf/diagnostics.c-797-\tstruct btf *btf = env-\u003eprog-\u003eaux-\u003ebtf;\n--\nkernel/bpf/diagnostics.c-831-\tsubprog = bpf_find_containing_subprog(env, insn_idx);\nkernel/bpf/diagnostics.c:832:\tsubprogno = subprog ? subprog - env-\u003esubprog_info : -ENOENT;\nkernel/bpf/diagnostics.c-833-\tfunc = subprogno \u003e= 0 ? bpf_subprog_name(env, subprogno) : NULL;\n--\nkernel/bpf/diagnostics.c-850-\tlinfo_end = subprogno \u003e= 0 \u0026\u0026 subprogno + 1 \u003c env-\u003esubprog_cnt ?\nkernel/bpf/diagnostics.c:851:\t\t    env-\u003esubprog_info[subprogno + 1].linfo_idx : env-\u003eprog-\u003eaux-\u003enr_linfo;\nkernel/bpf/diagnostics.c-852-\tfor (i = linfo_start; i \u003c linfo_end; i++) {\n--\nkernel/bpf/fixups.c=156=static int get_callee_stack_depth(struct bpf_verifier_env *env,\n--\nkernel/bpf/fixups.c-163-\t\treturn -EFAULT;\nkernel/bpf/fixups.c:164:\treturn env-\u003esubprog_info[subprog].stack_depth;\nkernel/bpf/fixups.c-165-}\n--\nkernel/bpf/fixups.c=343=static void patch_list_remap_aux(struct bpf_verifier_env *env,\n--\nkernel/bpf/fixups.c-373- */\nkernel/bpf/fixups.c:374:static bool patch_list_subprog_gone(const struct bpf_subprog_info *info, u32 cnt,\nkernel/bpf/fixups.c-375-\t\t\t\t    const u32 *new_off, u32 off)\n--\nkernel/bpf/fixups.c=395=static void patch_list_del_subprogs(struct bpf_verifier_env *env,\n--\nkernel/bpf/fixups.c-397-{\nkernel/bpf/fixups.c:398:\tstruct bpf_subprog_info *info = env-\u003esubprog_info;\nkernel/bpf/fixups.c-399-\tstruct bpf_prog_aux *aux = env-\u003eprog-\u003eaux;\n--\nkernel/bpf/fixups.c=436=static void patch_list_remap_subprogs(struct bpf_verifier_env *env,\n--\nkernel/bpf/fixups.c-438-{\nkernel/bpf/fixups.c:439:\tstruct bpf_subprog_info *info = env-\u003esubprog_info;\nkernel/bpf/fixups.c-440-\tu32 i;\n--\nkernel/bpf/fixups.c=590=static bool jumps_to_itself(const struct bpf_insn *insn)\n--\nkernel/bpf/fixups.c-598-/* How a subprog leaves when it is out of budget, see push_budget_exit() */\nkernel/bpf/fixups.c:599:static struct bpf_insn budget_exit_insn(struct bpf_verifier_env *env, struct bpf_subprog_info *sub)\nkernel/bpf/fixups.c-600-{\n--\nkernel/bpf/fixups.c=623=static int append_budget_exit(struct bpf_verifier_env *env, int s)\nkernel/bpf/fixups.c-624-{\nkernel/bpf/fixups.c:625:\tstruct bpf_subprog_info *sub = \u0026env-\u003esubprog_info[s];\nkernel/bpf/fixups.c-626-\tint last = (sub + 1)-\u003estart - 1, n = 0, i;\n--\nkernel/bpf/fixups.c=648=int bpf_insert_loop_guards(struct bpf_verifier_env *env)\n--\nkernel/bpf/fixups.c-689-\t\t\tcontinue;\nkernel/bpf/fixups.c:690:\t\ti = env-\u003esubprog_info[s + 1].start;\nkernel/bpf/fixups.c-691-\t\tleave[s] = i - leave[s];\n--\nkernel/bpf/fixups.c-713-\tfor (i = 0; i \u003c len; i++) {\nkernel/bpf/fixups.c:714:\t\ts = bpf_find_containing_subprog(env, i) - env-\u003esubprog_info;\n\n"
}

27/3 2026/11/12 22:48 llm kmsan-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 74846
							output: 551
							thoughts: 2254
						

Thoughts:
**KMSAN Applicability Analysis for BPF Kthread and Task Park/Wake Primitives**

Okay, let's break this down. First, I see we're allocating the `bpf_verifier_env` with `kzalloc`, meaning the `subprog_info` is zeroed from the start. That's good.

Now, a deep dive into this patch series: It adds `bpf_task_park()` and `bpf_task_wake()` and a suite of `bpf_kthread_*` functions for BPF-driven kernel thread management. The verifier now tracks `might_park`, `parked`, and `in_kthread_fn` to handle registers and potential memory issues correctly across sleep/wake boundaries. It also adds logic around cgroup attachment for these kthreads.

The critical question is, does this expose uninitialized memory risks ONLY detectable by KMSAN? Let's check the four KMSAN conditions:

1.  **Kernel structures to userspace?** Nope. The only kernel-user communication here is syscall return codes. No data structures are copied to user space. Good.

2.  **Conditional logic on uninitialized variables?** I'm meticulously checking every new variable or struct field. `tsk->bpf_wake_pending`, `tsk->bpf_wake_node`, `prog->aux->may_migrate`, `prog->aux->kthreads`, `prog->aux->kthread_seq`, `subprog->might_park`, `vstate->parked`, `frame->in_kthread_fn`, `expires`, `expired`, `ret` in various functions, and `lock_mode` in `cgroup_attach_kthread` are all initialized either during allocation or before use. Nothing I'm seeing is uninitialized at any point.

3.  **Complex data structures with uninitialized fields?** Not that I can see. We're using `kzalloc` for `bpf_kthread`, and new `task_struct` fields are explicitly initialized in `dup_task_struct`.

4.  **Bounds checks exposing uninitialized memory?** No. No changes here.

So, it seems we're good on that front.

Now, what kinds of *other* bugs could this introduce? Well, concurrency problems are the obvious ones: races between `bpf_task_park()` and `bpf_task_wake()`, issues with stopping kthreads, or even the cgroup interactions. Locking issues like deadlocks are also likely, given the introduction of new synchronization primitives. Also, use-after-free or refcounting errors with the `task_struct`, `bpf_kthread`, or `bpf_prog_aux` lifecycles could be potential. Verifier bugs could allow invalid pointer dereferences, stack clobbering or reference leaks. Memory corruption or out-of-bounds access could be an issue too.

Here's the key: None of these scenarios *require* or *benefit* from KMSAN. KMSAN is for detecting the use of uninitialized memory reads and information leaks, which is not what this patch introduces. This patch introduces bugs such as concurrency, synchronization, task lifecycle / reference counting (UAF, double frees), and potential deadlocks when stopping threads or attaching to cgroups. All of these are best caught by other tools.

We'd catch these with KASAN, LOCKDEP, and potentially other analysis tools. KASAN will flag memory corruption and UAF issues. LOCKDEP will catch locking inversions and deadlocks. KMSAN would be useless here.

Therefore, `NeedsKMSAN = false`.

I've reviewed the patch deeply, and this feels solid. KMSAN is not applicable. The potential risks are more related to concurrency, locking, and memory safety issues that are better addressed by KASAN and LOCKDEP. No uninitialized memory is read. No kernel data is leaked. Let's call `set-results`.



28/3 2026/11/12 22:50 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series introduces BPF kthreads (`bpf_kthread_create`, `bpf_kthread_start`, `bpf_kthread_bind`, `bpf_kthread_attach_cgroup`, `bpf_kthread_stop`) and task park/wake mechanisms (`bpf_task_park`, `bpf_task_wake`), alongside verifier enhancements to track sleepable, parkable, and migrating subprograms and invalidate borrowed references across park calls.\n\nKMSAN vs KASAN Applicability:\n1. Uninitialized memory / info-leak analysis:\n   - No kernel data structures or buffers are copied to user space (no `copy_to_user`, `put_user`, socket options, netlink attributes, or ioctl output structs).\n   - All newly introduced heap objects (e.g. `struct bpf_kthread` via `kzalloc_obj`) and struct fields (e.g. `bpf_wake_pending` and `bpf_wake_node` in `struct task_struct`, initialized in `dup_task_struct()`; `kthreads` and `kthread_seq` in `struct bpf_prog_aux`, allocated with `GFP_ZERO`; verifier state flags `parked`, `in_kthread_fn`, `might_park` in zero-allocated structures) are fully initialized before use.\n   - All local variables and branching conditions across `task_park.c`, `kthread.c`, `cgroup.c`, and `verifier.c` are properly initialized. There is no risk of uninitialized memory reads or information leakage to userspace.\n\n2. Memory safety and concurrency risks (KASAN / LOCKDEP):\n   - The primary risks of this patch involve task and program lifetime management, reference counting (`get_task_struct`, `put_task_struct`, `bpf_prog_inc_not_zero`, `kthread_stop_put`), race conditions between parking and waking, and synchronization during kthread shutdown or migration. These risks (such as Use-After-Free, double frees, or invalid dereferences) are directly covered by KASAN.\n   - Potential deadlocks (such as cyclic stopping chains or interactions between raw spinlocks, cgroup mutexes, and scheduler wait states) are covered by LOCKDEP.\n\nBecause there are no uninitialized memory risks or kernel info-leaks, a dedicated KMSAN fuzzing session is not justified.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series introduces BPF kthreads (`bpf_kthread_create`, `bpf_kthread_start`, `bpf_kthread_bind`, `bpf_kthread_attach_cgroup`, `bpf_kthread_stop`) and task park/wake mechanisms (`bpf_task_park`, `bpf_task_wake`), alongside verifier enhancements to track sleepable, parkable, and migrating subprograms and invalidate borrowed references across park calls.\n\nKMSAN vs KASAN Applicability:\n1. Uninitialized memory / info-leak analysis:\n   - No kernel data structures or buffers are copied to user space (no `copy_to_user`, `put_user`, socket options, netlink attributes, or ioctl output structs).\n   - All newly introduced heap objects (e.g. `struct bpf_kthread` via `kzalloc_obj`) and struct fields (e.g. `bpf_wake_pending` and `bpf_wake_node` in `struct task_struct`, initialized in `dup_task_struct()`; `kthreads` and `kthread_seq` in `struct bpf_prog_aux`, allocated with `GFP_ZERO`; verifier state flags `parked`, `in_kthread_fn`, `might_park` in zero-allocated structures) are fully initialized before use.\n   - All local variables and branching conditions across `task_park.c`, `kthread.c`, `cgroup.c`, and `verifier.c` are properly initialized. There is no risk of uninitialized memory reads or information leakage to userspace.\n\n2. Memory safety and concurrency risks (KASAN / LOCKDEP):\n   - The primary risks of this patch involve task and program lifetime management, reference counting (`get_task_struct`, `put_task_struct`, `bpf_prog_inc_not_zero`, `kthread_stop_put`), race conditions between parking and waking, and synchronization during kthread shutdown or migration. These risks (such as Use-After-Free, double frees, or invalid dereferences) are directly covered by KASAN.\n   - Potential deadlocks (such as cyclic stopping chains or interactions between raw spinlocks, cgroup mutexes, and scheduler wait states) are covered by LOCKDEP.\n\nBecause there are no uninitialized memory risks or kernel info-leaks, a dedicated KMSAN fuzzing session is not justified.",
  "NeedsKMSAN": false
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)