Skip to content

Commit 75c4207

Browse files
Mehdi Ben Hadj KhelifaKernel Patches Daemon
authored andcommitted
selftests/bpf: Change variable types for -Wsign-compare
This is a follow up patch for commit 495d2d8("selftests/bpf: Attempt to build BPF programs with -Wsign-compare") from Alexei Starovoitov[1] to be able to enable -Wsign-compare C compilation flag for clang since -Wall doesn't add it and BPF programs are built with clang.This has the benefit to catch problematic comparisons in future tests as quoted from the commit message:" int i = -1; unsigned int j = 1; if (i < j) // this is false. long i = -1; unsigned int j = 1; if (i < j) // this is true. C standard for reference: - If either operand is unsigned long the other shall be converted to unsigned long. - Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int. - Otherwise, if either operand is long, the other shall be converted to long. - Otherwise, if either operand is unsigned, the other shall be converted to unsigned. Unfortunately clang's -Wsign-compare is very noisy. It complains about (s32)a == (u32)b which is safe and doen't have surprising behavior." This specific patch supresses the following warnings when -Wsign-compare is enabled: 1 warning generated. progs/bpf_iter_bpf_percpu_array_map.c:35:16: warning: comparison of integers of different signs: 'int' and 'const volatile __u32' (aka 'const volatile unsigned int') [-Wsign-compare] 35 | for (i = 0; i < num_cpus; i++) { | ~ ^ ~~~~~~~~ 1 warning generated. progs/bpf_qdisc_fifo.c:93:2: warning: comparison of integers of different signs: 'int' and '__u32' (aka 'unsigned int') [-Wsign-compare] 93 | bpf_for(i, 0, sch->q.qlen) { | ^ ~ ~~~~~~~~~~~ Should be noted that many more similar changes are still needed in order to be able to enable the -Wsign-compare flag since -Werror is enabled and would cause compilation of bpf selftests to fail. [1]. Link:torvalds/linux@495d2d8 Signed-off-by: Mehdi Ben Hadj Khelifa <[email protected]>
1 parent d4664e4 commit 75c4207

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

tools/testing/selftests/bpf/progs/bpf_iter_bpf_percpu_array_map.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int dump_bpf_percpu_array_map(struct bpf_iter__bpf_map_elem *ctx)
2424
__u32 *key = ctx->key;
2525
void *pptr = ctx->value;
2626
__u32 step;
27-
int i;
27+
__u32 i;
2828

2929
if (key == (void *)0 || pptr == (void *)0)
3030
return 0;

tools/testing/selftests/bpf/progs/bpf_qdisc_fifo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void BPF_PROG(bpf_fifo_reset, struct Qdisc *sch)
8888
{
8989
struct bpf_list_node *node;
9090
struct skb_node *skbn;
91-
int i;
91+
__u32 i;
9292

9393
bpf_for(i, 0, sch->q.qlen) {
9494
struct sk_buff *skb = NULL;

0 commit comments

Comments
 (0)