commit fa747e9f843b ("selftests/bpf: Fix cold_lru producing zero
batch_hash in XDP LB benchmark") claims the addition ensures the
multiplier input is "always >= 1". This invariant does not hold after
wraparound.

batch_gen is __u32. After 2^32 increments it wraps to 0. On CPU 0,
bpf_get_smp_processor_id() returns 0:

    batch_gen = 0  (after u32 wraparound)
    batch_hash = (0 + 0) * KNUTH_HASH_MULT = 0
    *saddr ^= 0  ->  no-op, cold_lru miss counter stays 0

Setting bit 0 before multiplying guarantees a non-zero odd result for
all possible values of batch_gen and cpu_id, including after wraparound:

    (any_value | 1) >= 1  always, since bit 0 is always set

Fixes: fa747e9f843b ("selftests/bpf: Fix cold_lru producing zero batch_hash in 
XDP LB benchmark")
Signed-off-by: Muhammad Bilal <[email protected]>
---
 tools/testing/selftests/bpf/progs/xdp_lb_bench.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/progs/xdp_lb_bench.c 
b/tools/testing/selftests/bpf/progs/xdp_lb_bench.c
index 13777b3dcac8..816880c1cd5e 100644
--- a/tools/testing/selftests/bpf/progs/xdp_lb_bench.c
+++ b/tools/testing/selftests/bpf/progs/xdp_lb_bench.c
@@ -618,7 +618,7 @@ int xdp_lb_bench(struct xdp_md *xdp)
                __u32 *saddr = data + saddr_off;
 
                batch_gen++;
-               batch_hash = (batch_gen + bpf_get_smp_processor_id()) * 
KNUTH_HASH_MULT;
+               batch_hash = ((batch_gen + bpf_get_smp_processor_id()) | 1) * 
KNUTH_HASH_MULT;
                if ((void *)(saddr + 1) <= data_end)
                        *saddr ^= batch_hash;
        }
-- 
2.53.0


Reply via email to