1
0
Fork 0
mirror of synced 2025-03-06 20:59:54 +01:00
linux/tools/testing/selftests/bpf/progs/exhandler_kern.c
Andrii Nakryiko f760d05379 libbpf: Provide barrier() and barrier_var() in bpf_helpers.h
Add barrier() and barrier_var() macros into bpf_helpers.h to be used by
end users. While a bit advanced and specialized instruments, they are
sometimes indispensable. Instead of requiring each user to figure out
exact asm volatile incantations for themselves, provide them from
bpf_helpers.h.

Also remove conflicting definitions from selftests. Some tests rely on
barrier_var() definition being nothing, those will still work as libbpf
does the #ifndef/#endif guarding for barrier() and barrier_var(),
allowing users to redefine them, if necessary.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20220509004148.1801791-8-andrii@kernel.org
2022-05-09 17:15:32 +02:00

52 lines
1.4 KiB
C

// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021, Oracle and/or its affiliates. */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
char _license[] SEC("license") = "GPL";
unsigned int exception_triggered;
int test_pid;
/* TRACE_EVENT(task_newtask,
* TP_PROTO(struct task_struct *p, u64 clone_flags)
*/
SEC("tp_btf/task_newtask")
int BPF_PROG(trace_task_newtask, struct task_struct *task, u64 clone_flags)
{
int pid = bpf_get_current_pid_tgid() >> 32;
struct callback_head *work;
void *func;
if (test_pid != pid)
return 0;
/* To verify we hit an exception we dereference task->task_works->func.
* If task work has been added,
* - task->task_works is non-NULL; and
* - task->task_works->func is non-NULL also (the callback function
* must be specified for the task work.
*
* However, for a newly-created task, task->task_works is NULLed,
* so we know the exception handler triggered if task_works is
* NULL and func is NULL.
*/
work = task->task_works;
func = work->func;
/* Currently verifier will fail for `btf_ptr |= btf_ptr` * instruction.
* To workaround the issue, use barrier_var() and rewrite as below to
* prevent compiler from generating verifier-unfriendly code.
*/
barrier_var(work);
if (work)
return 0;
barrier_var(func);
if (func)
return 0;
exception_triggered++;
return 0;
}