2020-12-03 12:46:26 -08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* Copyright (c) 2020 Facebook */
|
|
|
|
#include <linux/error-injection.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
2021-01-11 23:55:20 -08:00
|
|
|
#include <linux/percpu-defs.h>
|
2020-12-03 12:46:26 -08:00
|
|
|
#include <linux/sysfs.h>
|
|
|
|
#include <linux/tracepoint.h>
|
|
|
|
#include "bpf_testmod.h"
|
|
|
|
|
|
|
|
#define CREATE_TRACE_POINTS
|
|
|
|
#include "bpf_testmod-events.h"
|
|
|
|
|
2021-01-11 23:55:20 -08:00
|
|
|
DEFINE_PER_CPU(int, bpf_testmod_ksym_percpu) = 123;
|
|
|
|
|
2021-09-10 11:33:52 -07:00
|
|
|
noinline int bpf_testmod_loop_test(int n)
|
|
|
|
{
|
|
|
|
int i, sum = 0;
|
|
|
|
|
|
|
|
/* the primary goal of this test is to test LBR. Create a lot of
|
|
|
|
* branches in the function, so we can catch it easily.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
sum += i;
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2020-12-03 12:46:26 -08:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2021-09-10 11:33:52 -07:00
|
|
|
/* This is always true. Use the check to make sure the compiler
|
|
|
|
* doesn't remove bpf_testmod_loop_test.
|
|
|
|
*/
|
|
|
|
if (bpf_testmod_loop_test(101) > 100)
|
|
|
|
trace_bpf_testmod_test_read(current, &ctx);
|
2020-12-03 12:46:26 -08:00
|
|
|
|
|
|
|
return -EIO; /* always fail */
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(bpf_testmod_test_read);
|
|
|
|
ALLOW_ERROR_INJECTION(bpf_testmod_test_read, ERRNO);
|
|
|
|
|
2021-01-19 12:22:37 +00:00
|
|
|
noinline ssize_t
|
|
|
|
bpf_testmod_test_write(struct file *file, struct kobject *kobj,
|
|
|
|
struct bin_attribute *bin_attr,
|
|
|
|
char *buf, loff_t off, size_t len)
|
|
|
|
{
|
|
|
|
struct bpf_testmod_test_write_ctx ctx = {
|
|
|
|
.buf = buf,
|
|
|
|
.off = off,
|
|
|
|
.len = len,
|
|
|
|
};
|
|
|
|
|
|
|
|
trace_bpf_testmod_test_write_bare(current, &ctx);
|
|
|
|
|
|
|
|
return -EIO; /* always fail */
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(bpf_testmod_test_write);
|
|
|
|
ALLOW_ERROR_INJECTION(bpf_testmod_test_write, ERRNO);
|
|
|
|
|
2020-12-03 12:46:26 -08:00
|
|
|
static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
|
2021-01-19 12:22:37 +00:00
|
|
|
.attr = { .name = "bpf_testmod", .mode = 0666, },
|
2020-12-03 12:46:26 -08:00
|
|
|
.read = bpf_testmod_test_read,
|
2021-01-19 12:22:37 +00:00
|
|
|
.write = bpf_testmod_test_write,
|
2020-12-03 12:46:26 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
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");
|