Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2021, Oracle and/or its affiliates. */
0003 
0004 #include "vmlinux.h"
0005 
0006 #include <bpf/bpf_helpers.h>
0007 #include <bpf/bpf_tracing.h>
0008 #include <bpf/bpf_core_read.h>
0009 
0010 char _license[] SEC("license") = "GPL";
0011 
0012 unsigned int exception_triggered;
0013 int test_pid;
0014 
0015 /* TRACE_EVENT(task_newtask,
0016  *         TP_PROTO(struct task_struct *p, u64 clone_flags)
0017  */
0018 SEC("tp_btf/task_newtask")
0019 int BPF_PROG(trace_task_newtask, struct task_struct *task, u64 clone_flags)
0020 {
0021     int pid = bpf_get_current_pid_tgid() >> 32;
0022     struct callback_head *work;
0023     void *func;
0024 
0025     if (test_pid != pid)
0026         return 0;
0027 
0028     /* To verify we hit an exception we dereference task->task_works->func.
0029      * If task work has been added,
0030      * - task->task_works is non-NULL; and
0031      * - task->task_works->func is non-NULL also (the callback function
0032      *   must be specified for the task work.
0033      *
0034      * However, for a newly-created task, task->task_works is NULLed,
0035      * so we know the exception handler triggered if task_works is
0036      * NULL and func is NULL.
0037      */
0038     work = task->task_works;
0039     func = work->func;
0040     /* Currently verifier will fail for `btf_ptr |= btf_ptr` * instruction.
0041      * To workaround the issue, use barrier_var() and rewrite as below to
0042      * prevent compiler from generating verifier-unfriendly code.
0043      */
0044     barrier_var(work);
0045     if (work)
0046         return 0;
0047     barrier_var(func);
0048     if (func)
0049         return 0;
0050     exception_triggered++;
0051     return 0;
0052 }