mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00

The selftests build four kernel modules which use copy-pasted Makefile targets. This is a bit messy, and doesn't scale so well when we add more modules, so let's consolidate these rules into a single rule generated for each module name, and move the module sources into a single directory. To avoid parallel builds of the different modules stepping on each other's toes during the 'modpost' phase of the Kbuild 'make modules', the module files should really be a grouped target. However, make only added explicit support for grouped targets in version 4.3, which is newer than the minimum version supported by the kernel. However, make implicitly treats pattern matching rules with multiple targets as a grouped target, so we can work around this by turning the rule into a pattern matching target. We do this by replacing '.ko' with '%ko' in the targets with subst(). Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Viktor Malik <vmalik@redhat.com> Link: https://lore.kernel.org/bpf/20241204-bpf-selftests-mod-compile-v5-1-b96231134a49@redhat.com
114 lines
2.3 KiB
C
114 lines
2.3 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <vmlinux.h>
|
|
#include <bpf/bpf_tracing.h>
|
|
#include <bpf/bpf_helpers.h>
|
|
#include "../test_kmods/bpf_testmod_kfunc.h"
|
|
|
|
struct map_value {
|
|
struct prog_test_ref_kfunc __kptr *ptr;
|
|
};
|
|
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_ARRAY);
|
|
__type(key, int);
|
|
__type(value, struct map_value);
|
|
__uint(max_entries, 16);
|
|
} array_map SEC(".maps");
|
|
|
|
static __noinline int cb1(void *map, void *key, void *value, void *ctx)
|
|
{
|
|
void *p = *(void **)ctx;
|
|
bpf_kfunc_call_test_release(p);
|
|
/* Without the fix this would cause underflow */
|
|
return 0;
|
|
}
|
|
|
|
SEC("?tc")
|
|
int underflow_prog(void *ctx)
|
|
{
|
|
struct prog_test_ref_kfunc *p;
|
|
unsigned long sl = 0;
|
|
|
|
p = bpf_kfunc_call_test_acquire(&sl);
|
|
if (!p)
|
|
return 0;
|
|
bpf_for_each_map_elem(&array_map, cb1, &p, 0);
|
|
bpf_kfunc_call_test_release(p);
|
|
return 0;
|
|
}
|
|
|
|
static __always_inline int cb2(void *map, void *key, void *value, void *ctx)
|
|
{
|
|
unsigned long sl = 0;
|
|
|
|
*(void **)ctx = bpf_kfunc_call_test_acquire(&sl);
|
|
/* Without the fix this would leak memory */
|
|
return 0;
|
|
}
|
|
|
|
SEC("?tc")
|
|
int leak_prog(void *ctx)
|
|
{
|
|
struct prog_test_ref_kfunc *p;
|
|
struct map_value *v;
|
|
|
|
v = bpf_map_lookup_elem(&array_map, &(int){0});
|
|
if (!v)
|
|
return 0;
|
|
|
|
p = NULL;
|
|
bpf_for_each_map_elem(&array_map, cb2, &p, 0);
|
|
p = bpf_kptr_xchg(&v->ptr, p);
|
|
if (p)
|
|
bpf_kfunc_call_test_release(p);
|
|
return 0;
|
|
}
|
|
|
|
static __always_inline int cb(void *map, void *key, void *value, void *ctx)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
static __always_inline int cb3(void *map, void *key, void *value, void *ctx)
|
|
{
|
|
unsigned long sl = 0;
|
|
void *p;
|
|
|
|
bpf_kfunc_call_test_acquire(&sl);
|
|
bpf_for_each_map_elem(&array_map, cb, &p, 0);
|
|
/* It should only complain here, not in cb. This is why we need
|
|
* callback_ref to be set to frameno.
|
|
*/
|
|
return 0;
|
|
}
|
|
|
|
SEC("?tc")
|
|
int nested_cb(void *ctx)
|
|
{
|
|
struct prog_test_ref_kfunc *p;
|
|
unsigned long sl = 0;
|
|
int sp = 0;
|
|
|
|
p = bpf_kfunc_call_test_acquire(&sl);
|
|
if (!p)
|
|
return 0;
|
|
bpf_for_each_map_elem(&array_map, cb3, &sp, 0);
|
|
bpf_kfunc_call_test_release(p);
|
|
return 0;
|
|
}
|
|
|
|
SEC("?tc")
|
|
int non_cb_transfer_ref(void *ctx)
|
|
{
|
|
struct prog_test_ref_kfunc *p;
|
|
unsigned long sl = 0;
|
|
|
|
p = bpf_kfunc_call_test_acquire(&sl);
|
|
if (!p)
|
|
return 0;
|
|
cb1(NULL, NULL, NULL, &p);
|
|
bpf_kfunc_call_test_acquire(&sl);
|
|
return 0;
|
|
}
|
|
|
|
char _license[] SEC("license") = "GPL";
|