1c23551c9SAlan Maguire // SPDX-License-Identifier: GPL-2.0
2c23551c9SAlan Maguire /* Copyright (c) 2021, Oracle and/or its affiliates. */
3c23551c9SAlan Maguire
4c23551c9SAlan Maguire #include "vmlinux.h"
5c23551c9SAlan Maguire
6c23551c9SAlan Maguire #include <bpf/bpf_helpers.h>
7c23551c9SAlan Maguire #include <bpf/bpf_tracing.h>
8c23551c9SAlan Maguire #include <bpf/bpf_core_read.h>
9c23551c9SAlan Maguire
10c23551c9SAlan Maguire char _license[] SEC("license") = "GPL";
11c23551c9SAlan Maguire
12c23551c9SAlan Maguire unsigned int exception_triggered;
13c23551c9SAlan Maguire int test_pid;
14c23551c9SAlan Maguire
15c23551c9SAlan Maguire /* TRACE_EVENT(task_newtask,
16c23551c9SAlan Maguire * TP_PROTO(struct task_struct *p, u64 clone_flags)
17c23551c9SAlan Maguire */
18c23551c9SAlan Maguire SEC("tp_btf/task_newtask")
BPF_PROG(trace_task_newtask,struct task_struct * task,u64 clone_flags)19c23551c9SAlan Maguire int BPF_PROG(trace_task_newtask, struct task_struct *task, u64 clone_flags)
20c23551c9SAlan Maguire {
21c23551c9SAlan Maguire int pid = bpf_get_current_pid_tgid() >> 32;
22c23551c9SAlan Maguire struct callback_head *work;
23c23551c9SAlan Maguire void *func;
24c23551c9SAlan Maguire
25c23551c9SAlan Maguire if (test_pid != pid)
26c23551c9SAlan Maguire return 0;
27c23551c9SAlan Maguire
28c23551c9SAlan Maguire /* To verify we hit an exception we dereference task->task_works->func.
29c23551c9SAlan Maguire * If task work has been added,
30c23551c9SAlan Maguire * - task->task_works is non-NULL; and
31c23551c9SAlan Maguire * - task->task_works->func is non-NULL also (the callback function
32c23551c9SAlan Maguire * must be specified for the task work.
33c23551c9SAlan Maguire *
34c23551c9SAlan Maguire * However, for a newly-created task, task->task_works is NULLed,
35c23551c9SAlan Maguire * so we know the exception handler triggered if task_works is
36c23551c9SAlan Maguire * NULL and func is NULL.
37c23551c9SAlan Maguire */
38c23551c9SAlan Maguire work = task->task_works;
39c23551c9SAlan Maguire func = work->func;
40*44df171aSYonghong Song /* Currently verifier will fail for `btf_ptr |= btf_ptr` * instruction.
41*44df171aSYonghong Song * To workaround the issue, use barrier_var() and rewrite as below to
42*44df171aSYonghong Song * prevent compiler from generating verifier-unfriendly code.
43*44df171aSYonghong Song */
44*44df171aSYonghong Song barrier_var(work);
45*44df171aSYonghong Song if (work)
46*44df171aSYonghong Song return 0;
47*44df171aSYonghong Song barrier_var(func);
48*44df171aSYonghong Song if (func)
49*44df171aSYonghong Song return 0;
50c23551c9SAlan Maguire exception_triggered++;
51c23551c9SAlan Maguire return 0;
52c23551c9SAlan Maguire }
53