forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Add bpf_testmod kernel module for testing
Add bpf_testmod module, which is conceptually out-of-tree module and provides ways for selftests/bpf to test various kernel module-related functionality: raw tracepoint, fentry/fexit/fmod_ret, etc. This module will be auto-loaded by test_progs test runner and expected by some of selftests to be present and loaded. Pahole currently isn't able to generate BTF for static functions in kernel modules, so make sure traced function is global. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
- Loading branch information
Showing
9 changed files
with
198 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,4 @@ test_cpp | |
/tools | ||
/runqslower | ||
/bench | ||
*.ko |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
*.mod | ||
*.mod.c | ||
*.o | ||
.ko | ||
/Module.symvers | ||
/modules.order |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
BPF_TESTMOD_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) | ||
KDIR ?= $(abspath $(BPF_TESTMOD_DIR)/../../../../..) | ||
|
||
ifeq ($(V),1) | ||
Q = | ||
else | ||
Q = @ | ||
endif | ||
|
||
MODULES = bpf_testmod.ko | ||
|
||
obj-m += bpf_testmod.o | ||
CFLAGS_bpf_testmod.o = -I$(src) | ||
|
||
all: | ||
+$(Q)make -C $(KDIR) M=$(BPF_TESTMOD_DIR) modules | ||
|
||
clean: | ||
+$(Q)make -C $(KDIR) M=$(BPF_TESTMOD_DIR) clean | ||
|
36 changes: 36 additions & 0 deletions
36
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod-events.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
/* Copyright (c) 2020 Facebook */ | ||
#undef TRACE_SYSTEM | ||
#define TRACE_SYSTEM bpf_testmod | ||
|
||
#if !defined(_BPF_TESTMOD_EVENTS_H) || defined(TRACE_HEADER_MULTI_READ) | ||
#define _BPF_TESTMOD_EVENTS_H | ||
|
||
#include <linux/tracepoint.h> | ||
#include "bpf_testmod.h" | ||
|
||
TRACE_EVENT(bpf_testmod_test_read, | ||
TP_PROTO(struct task_struct *task, struct bpf_testmod_test_read_ctx *ctx), | ||
TP_ARGS(task, ctx), | ||
TP_STRUCT__entry( | ||
__field(pid_t, pid) | ||
__array(char, comm, TASK_COMM_LEN) | ||
__field(loff_t, off) | ||
__field(size_t, len) | ||
), | ||
TP_fast_assign( | ||
__entry->pid = task->pid; | ||
memcpy(__entry->comm, task->comm, TASK_COMM_LEN); | ||
__entry->off = ctx->off; | ||
__entry->len = ctx->len; | ||
), | ||
TP_printk("pid=%d comm=%s off=%llu len=%zu", | ||
__entry->pid, __entry->comm, __entry->off, __entry->len) | ||
); | ||
|
||
#endif /* _BPF_TESTMOD_EVENTS_H */ | ||
|
||
#undef TRACE_INCLUDE_PATH | ||
#define TRACE_INCLUDE_PATH . | ||
#define TRACE_INCLUDE_FILE bpf_testmod-events | ||
#include <trace/define_trace.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2020 Facebook */ | ||
#include <linux/error-injection.h> | ||
#include <linux/init.h> | ||
#include <linux/module.h> | ||
#include <linux/sysfs.h> | ||
#include <linux/tracepoint.h> | ||
#include "bpf_testmod.h" | ||
|
||
#define CREATE_TRACE_POINTS | ||
#include "bpf_testmod-events.h" | ||
|
||
noinline ssize_t | ||
bpf_testmod_test_read(struct file *file, struct kobject *kobj, | ||
struct bin_attribute *bin_attr, | ||
char *buf, loff_t off, size_t len) | ||
{ | ||
struct bpf_testmod_test_read_ctx ctx = { | ||
.buf = buf, | ||
.off = off, | ||
.len = len, | ||
}; | ||
|
||
trace_bpf_testmod_test_read(current, &ctx); | ||
|
||
return -EIO; /* always fail */ | ||
} | ||
EXPORT_SYMBOL(bpf_testmod_test_read); | ||
ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO); | ||
|
||
static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = { | ||
.attr = { .name = "bpf_testmod", .mode = 0444, }, | ||
.read = bpf_testmod_test_read, | ||
}; | ||
|
||
static int bpf_testmod_init(void) | ||
{ | ||
return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file); | ||
} | ||
|
||
static void bpf_testmod_exit(void) | ||
{ | ||
return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file); | ||
} | ||
|
||
module_init(bpf_testmod_init); | ||
module_exit(bpf_testmod_exit); | ||
|
||
MODULE_AUTHOR("Andrii Nakryiko"); | ||
MODULE_DESCRIPTION("BPF selftests module"); | ||
MODULE_LICENSE("Dual BSD/GPL"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
/* Copyright (c) 2020 Facebook */ | ||
#ifndef _BPF_TESTMOD_H | ||
#define _BPF_TESTMOD_H | ||
|
||
#include <linux/types.h> | ||
|
||
struct bpf_testmod_test_read_ctx { | ||
char *buf; | ||
loff_t off; | ||
size_t len; | ||
}; | ||
|
||
#endif /* _BPF_TESTMOD_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters