Skip to content

Commit

Permalink
selftests/bpf: fix lots of silly mistakes pointed out by compiler
Browse files Browse the repository at this point in the history
Once we enable -Wall for BPF sources, compiler will complain about lots
of unused variables, variables that are set but never read, etc.

Fix all these issues first before enabling -Wall in Makefile.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20230309054015.4068562-4-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Andrii Nakryiko authored and Alexei Starovoitov committed Mar 10, 2023
1 parent 713461b commit c8ed668
Show file tree
Hide file tree
Showing 81 changed files with 90 additions and 187 deletions.
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/progs/bpf_iter_ksym.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ int dump_ksym(struct bpf_iter__ksym *ctx)
__u32 seq_num = ctx->meta->seq_num;
unsigned long value;
char type;
int ret;

if (!iter)
return 0;
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/progs/bpf_iter_setsockopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ int change_tcp_cc(struct bpf_iter__tcp *ctx)
char cur_cc[TCP_CA_NAME_MAX];
struct tcp_sock *tp;
struct sock *sk;
int ret;

if (!bpf_tcp_sk(ctx->sk_common))
return 0;
Expand Down
2 changes: 0 additions & 2 deletions tools/testing/selftests/bpf/progs/bpf_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ static int callback_set_0f(int i, void *ctx)
SEC("fentry/" SYS_PREFIX "sys_nanosleep")
int prog_non_constant_callback(void *ctx)
{
struct callback_ctx data = {};

if (bpf_get_current_pid_tgid() >> 32 != pid)
return 0;

Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/progs/cb_refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ int leak_prog(void *ctx)
{
struct prog_test_ref_kfunc *p;
struct map_value *v;
unsigned long sl;

v = bpf_map_lookup_elem(&array_map, &(int){0});
if (!v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ static inline int is_allowed_peer_cg(struct __sk_buff *skb,
SEC("cgroup_skb/ingress")
int ingress_lookup(struct __sk_buff *skb)
{
__u32 serv_port_key = 0;
struct ipv6hdr ip6h;
struct tcphdr tcph;

Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/bpf/progs/cgrp_kfunc_failure.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ int BPF_PROG(cgrp_kfunc_acquire_unreleased, struct cgroup *cgrp, const char *pat
acquired = bpf_cgroup_acquire(cgrp);

/* Acquired cgroup is never released. */
__sink(acquired);

return 0;
}
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/progs/cgrp_ls_attach_cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ int BPF_PROG(update_cookie_tracing, struct socket *sock,
struct sockaddr *uaddr, int addr_len, int flags)
{
struct socket_cookie *p;
struct tcp_sock *tcp_sk;

if (uaddr->sa_family != AF_INET6)
return 0;
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/progs/cgrp_ls_sleepable.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ void bpf_rcu_read_unlock(void) __ksym;
SEC("?iter.s/cgroup")
int cgroup_iter(struct bpf_iter__cgroup *ctx)
{
struct seq_file *seq = ctx->meta->seq;
struct cgroup *cgrp = ctx->cgroup;
long *ptr;

Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/core_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int balancer_ingress(struct __sk_buff *ctx)
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
void *ptr;
int ret = 0, nh_off, i = 0;
int nh_off, i = 0;

nh_off = 14;

Expand Down
3 changes: 3 additions & 0 deletions tools/testing/selftests/bpf/progs/cpumask_failure.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ int BPF_PROG(test_alloc_no_release, struct task_struct *task, u64 clone_flags)
struct bpf_cpumask *cpumask;

cpumask = create_cpumask();
__sink(cpumask);

/* cpumask is never released. */
return 0;
Expand Down Expand Up @@ -51,6 +52,7 @@ int BPF_PROG(test_acquire_wrong_cpumask, struct task_struct *task, u64 clone_fla

/* Can't acquire a non-struct bpf_cpumask. */
cpumask = bpf_cpumask_acquire((struct bpf_cpumask *)task->cpus_ptr);
__sink(cpumask);

return 0;
}
Expand All @@ -63,6 +65,7 @@ int BPF_PROG(test_mutate_cpumask, struct task_struct *task, u64 clone_flags)

/* Can't set the CPU of a non-struct bpf_cpumask. */
bpf_cpumask_set_cpu(0, (struct bpf_cpumask *)task->cpus_ptr);
__sink(cpumask);

return 0;
}
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/progs/cpumask_success.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ SEC("tp_btf/task_newtask")
int BPF_PROG(test_insert_leave, struct task_struct *task, u64 clone_flags)
{
struct bpf_cpumask *cpumask;
struct __cpumask_map_value *v;

cpumask = create_cpumask();
if (!cpumask)
Expand Down
5 changes: 3 additions & 2 deletions tools/testing/selftests/bpf/progs/dynptr_fail.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ SEC("?raw_tp")
__failure __msg("value is outside of the allowed memory range")
int data_slice_out_of_bounds_map_value(void *ctx)
{
__u32 key = 0, map_val;
__u32 map_val;
struct bpf_dynptr ptr;
void *data;

Expand Down Expand Up @@ -388,7 +388,6 @@ int data_slice_missing_null_check2(void *ctx)
/* this should fail */
*data2 = 3;

done:
bpf_ringbuf_discard_dynptr(&ptr, 0);
return 0;
}
Expand Down Expand Up @@ -440,6 +439,7 @@ int invalid_write1(void *ctx)

/* this should fail */
data = bpf_dynptr_data(&ptr, 0, 1);
__sink(data);

return 0;
}
Expand Down Expand Up @@ -1374,6 +1374,7 @@ int invalid_slice_rdwr_rdonly(struct __sk_buff *skb)
* changing packet data
*/
hdr = bpf_dynptr_slice_rdwr(&ptr, 0, buffer, sizeof(buffer));
__sink(hdr);

return 0;
}
5 changes: 1 addition & 4 deletions tools/testing/selftests/bpf/progs/dynptr_success.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ SEC("?tp/syscalls/sys_enter_nanosleep")
int test_read_write(void *ctx)
{
char write_data[64] = "hello there, world!!";
char read_data[64] = {}, buf[64] = {};
char read_data[64] = {};
struct bpf_dynptr ptr;
int i;

Expand Down Expand Up @@ -170,7 +170,6 @@ int test_skb_readonly(struct __sk_buff *skb)
{
__u8 write_data[2] = {1, 2};
struct bpf_dynptr ptr;
__u64 *data;
int ret;

if (bpf_dynptr_from_skb(skb, 0, &ptr)) {
Expand All @@ -191,10 +190,8 @@ int test_skb_readonly(struct __sk_buff *skb)
SEC("?cgroup_skb/egress")
int test_dynptr_skb_data(struct __sk_buff *skb)
{
__u8 write_data[2] = {1, 2};
struct bpf_dynptr ptr;
__u64 *data;
int ret;

if (bpf_dynptr_from_skb(skb, 0, &ptr)) {
err = 1;
Expand Down
2 changes: 0 additions & 2 deletions tools/testing/selftests/bpf/progs/fexit_bpf2bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ int new_get_skb_ifindex(int val, struct __sk_buff *skb, int var)
void *data = (void *)(long)skb->data;
struct ipv6hdr ip6, *ip6p;
int ifindex = skb->ifindex;
__u32 eth_proto;
__u32 nh_off;

/* check that BPF extension can read packet via direct packet access */
if (data + 14 + sizeof(ip6) > data_end)
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/freplace_attach_probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct {
SEC("freplace/handle_kprobe")
int new_handle_kprobe(struct pt_regs *ctx)
{
struct hmap_elem zero = {}, *val;
struct hmap_elem *val;
int key = 0;

val = bpf_map_lookup_elem(&hash_map, &key);
Expand Down
11 changes: 5 additions & 6 deletions tools/testing/selftests/bpf/progs/iters.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ __failure __msg("unbounded memory access")
int iter_err_unsafe_asm_loop(const void *ctx)
{
struct bpf_iter_num it;
int *v, i = 0;

MY_PID_GUARD();

Expand Down Expand Up @@ -88,7 +87,7 @@ __success
int iter_while_loop(const void *ctx)
{
struct bpf_iter_num it;
int *v, i;
int *v;

MY_PID_GUARD();

Expand All @@ -106,7 +105,7 @@ __success
int iter_while_loop_auto_cleanup(const void *ctx)
{
__attribute__((cleanup(bpf_iter_num_destroy))) struct bpf_iter_num it;
int *v, i;
int *v;

MY_PID_GUARD();

Expand All @@ -124,7 +123,7 @@ __success
int iter_for_loop(const void *ctx)
{
struct bpf_iter_num it;
int *v, i;
int *v;

MY_PID_GUARD();

Expand Down Expand Up @@ -192,7 +191,7 @@ __success
int iter_manual_unroll_loop(const void *ctx)
{
struct bpf_iter_num it;
int *v, i;
int *v;

MY_PID_GUARD();

Expand Down Expand Up @@ -621,7 +620,7 @@ __success
int iter_stack_array_loop(const void *ctx)
{
long arr1[16], arr2[16], sum = 0;
int *v, i;
int i;

MY_PID_GUARD();

Expand Down
3 changes: 3 additions & 0 deletions tools/testing/selftests/bpf/progs/linked_funcs1.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "bpf_misc.h"

/* weak and shared between two files */
const volatile int my_tid __weak;
Expand Down Expand Up @@ -51,6 +52,7 @@ __weak int set_output_weak(int x)
* cause problems for BPF static linker
*/
whatever = bpf_core_type_size(struct task_struct);
__sink(whatever);

output_weak1 = x;
return x;
Expand All @@ -71,6 +73,7 @@ int BPF_PROG(handler1, struct pt_regs *regs, long id)

/* make sure we have CO-RE relocations in main program */
whatever = bpf_core_type_size(struct task_struct);
__sink(whatever);

set_output_val2(1000);
set_output_ctx2(ctx); /* ctx definition is hidden in BPF_PROG macro */
Expand Down
3 changes: 3 additions & 0 deletions tools/testing/selftests/bpf/progs/linked_funcs2.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "bpf_misc.h"

/* weak and shared between both files */
const volatile int my_tid __weak;
Expand Down Expand Up @@ -51,6 +52,7 @@ __weak int set_output_weak(int x)
* cause problems for BPF static linker
*/
whatever = 2 * bpf_core_type_size(struct task_struct);
__sink(whatever);

output_weak2 = x;
return 2 * x;
Expand All @@ -71,6 +73,7 @@ int BPF_PROG(handler2, struct pt_regs *regs, long id)

/* make sure we have CO-RE relocations in main program */
whatever = bpf_core_type_size(struct task_struct);
__sink(whatever);

set_output_val1(2000);
set_output_ctx1(ctx); /* ctx definition is hidden in BPF_PROG macro */
Expand Down
4 changes: 0 additions & 4 deletions tools/testing/selftests/bpf/progs/linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ SEC("tc")
int map_list_push_pop_multiple(void *ctx)
{
struct map_value *v;
int ret;

v = bpf_map_lookup_elem(&array_map, &(int){0});
if (!v)
Expand All @@ -326,7 +325,6 @@ int inner_map_list_push_pop_multiple(void *ctx)
{
struct map_value *v;
void *map;
int ret;

map = bpf_map_lookup_elem(&map_of_maps, &(int){0});
if (!map)
Expand All @@ -352,7 +350,6 @@ SEC("tc")
int map_list_in_list(void *ctx)
{
struct map_value *v;
int ret;

v = bpf_map_lookup_elem(&array_map, &(int){0});
if (!v)
Expand All @@ -365,7 +362,6 @@ int inner_map_list_in_list(void *ctx)
{
struct map_value *v;
void *map;
int ret;

map = bpf_map_lookup_elem(&map_of_maps, &(int){0});
if (!map)
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/progs/linked_list_fail.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ SEC("?tc")
int incorrect_head_off2(void *ctx)
{
struct foo *f;
struct bar *b;

f = bpf_obj_new(typeof(*f));
if (!f)
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/progs/local_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ int BPF_PROG(inode_rename, struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry,
unsigned int flags)
{
__u32 pid = bpf_get_current_pid_tgid() >> 32;
struct local_storage *storage;
int err;

Expand Down
3 changes: 0 additions & 3 deletions tools/testing/selftests/bpf/progs/map_kptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ int test_ls_map_kptr_ref1(void *ctx)
{
struct task_struct *current;
struct map_value *v;
int ret;

current = bpf_get_current_task_btf();
if (!current)
Expand All @@ -534,7 +533,6 @@ int test_ls_map_kptr_ref2(void *ctx)
{
struct task_struct *current;
struct map_value *v;
int ret;

current = bpf_get_current_task_btf();
if (!current)
Expand All @@ -550,7 +548,6 @@ int test_ls_map_kptr_ref_del(void *ctx)
{
struct task_struct *current;
struct map_value *v;
int ret;

current = bpf_get_current_task_btf();
if (!current)
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/progs/netcnt_prog.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ SEC("cgroup/skb")
int bpf_nextcnt(struct __sk_buff *skb)
{
union percpu_net_cnt *percpu_cnt;
char fmt[] = "%d %llu %llu\n";
union net_cnt *cnt;
__u64 ts, dt;
int ret;
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/progs/netif_receive_skb.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ static int __strncmp(const void *m1, const void *m2, size_t len)
do { \
static const char _expectedval[EXPECTED_STRSIZE] = \
_expected; \
static const char _ptrtype[64] = #_type; \
__u64 _hflags = _flags | BTF_F_COMPACT; \
static _type _ptrdata = __VA_ARGS__; \
static struct btf_ptr _ptr = { }; \
Expand Down
1 change: 0 additions & 1 deletion tools/testing/selftests/bpf/progs/perfbuf_bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ long dropped __attribute__((aligned(128))) = 0;
SEC("fentry/" SYS_PREFIX "sys_getpgid")
int bench_perfbuf(void *ctx)
{
__u64 *sample;
int i;

for (i = 0; i < batch_cnt; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/pyperf.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ int __on_event(struct bpf_raw_tracepoint_args *ctx)
SEC("raw_tracepoint/kfree_skb")
int on_event(struct bpf_raw_tracepoint_args* ctx)
{
int i, ret = 0;
int ret = 0;
ret |= __on_event(ctx);
ret |= __on_event(ctx);
ret |= __on_event(ctx);
Expand Down
Loading

0 comments on commit c8ed668

Please sign in to comment.