Wake the parked waiter from a tracing program in both of the contexts the wake distinguishes. Attached to task_rename, the program runs in process context when the test thread renames itself, so its wake is direct and reports that it found the task asleep. Attached to sched_waking, it runs when the test thread's own sleep ends, inside try_to_wake_up() with the thread's pi_lock held and IRQs disabled, so its wake is deferred to irq_work and returns 2, and the park ends when the irq_work runs. In each case the test checks that the waiter returned before the syscall drain was tried, which runs only as a fallback so that a lost wake fails the test instead of hanging it. The program is attached only once the waiter sleeps, since the thread's polling sleeps are wakeups of its own. Signed-off-by: Kumar Kartikeya Dwivedi --- .../selftests/bpf/prog_tests/task_park.c | 66 +++++++++++++++++++ tools/testing/selftests/bpf/progs/task_park.c | 48 +++++++++++++- 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/task_park.c b/tools/testing/selftests/bpf/prog_tests/task_park.c index 43514132e6e9..a479a183c696 100644 --- a/tools/testing/selftests/bpf/prog_tests/task_park.c +++ b/tools/testing/selftests/bpf/prog_tests/task_park.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "task_park.skel.h" #include "verifier_task_park.skel.h" @@ -169,6 +170,67 @@ static void test_wake_asleep(struct task_park *skel) 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) { } @@ -237,6 +299,10 @@ void test_task_park(void) 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")) diff --git a/tools/testing/selftests/bpf/progs/task_park.c b/tools/testing/selftests/bpf/progs/task_park.c index c777cc787dab..2320ede02cc9 100644 --- a/tools/testing/selftests/bpf/progs/task_park.c +++ b/tools/testing/selftests/bpf/progs/task_park.c @@ -4,7 +4,7 @@ #include "bpf_experimental.h" #include -/* Exercise bpf_task_park() and bpf_task_wake() from syscall programs. */ +/* 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; @@ -139,6 +139,52 @@ int wake_waiter(void *ctx) 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) -- 2.53.0