Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp131555iob; Tue, 17 May 2022 21:17:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxH+ceDOZ1Y9dEnvNV+Y5G8eNS/GIKPMfkjthHQnNbuNb59bYzQyuuDC8om2bcHtscTJ6mL X-Received: by 2002:a05:6a00:1ad2:b0:510:72f6:d039 with SMTP id f18-20020a056a001ad200b0051072f6d039mr25826857pfv.2.1652847439797; Tue, 17 May 2022 21:17:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652847439; cv=none; d=google.com; s=arc-20160816; b=fM6sWO1HJlnjKvLysm/09+bm23CmqQaKdyT+nMegbfiArtnP/OwMJASPMI09fL6gTP +RNoBf+S/26kKlsY33/Wlngv6Zxc4SjSE/Glg/afD4IR8xTtrza1YbWD3n6MBFaAJAm0 bCk8vmjRbNnAguk92zva2rPRGyrvloh4AKgSli41zfOIBBwD+NnyvKTZelyJ+Ln7dRVX 6s4OQ7drxtHDF8q1PGMwGtbxq8jVy/c+UmIHl4Ui1Hwt45QnZpT0SLLAyppqjefp/h7/ hg6SSbi1cWjwFMrlyx53gv13DUHNWRv7u3UZSvzoIYbwKRVukIepyxVLKZnj9oY4umFL AHSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=Muu06nupwh+dYqU+u10ilHsVeZYqZAWfb+sLHhJaZYQ=; b=qAgjoCDQS1zhhsofQaPdW+z8OWk6MNo/TUBK9dAeTj4/vQBb7aZbyJwx7YBZtIqvUz aWwtTXBFiEwYfyAD9itYAXkJ5kCQd8BF3s2ysjkIwpBmndahtWYDkbibdWw8fh1dm17H y2kPxBRYtwBGlRwjD5XqIwQTi16d5rTbCiFA6PG2D5yYWZHW4dcb8mGsxyZNkfRUPSFh c+G/xmsEuxuL5nI+ShLdb5hU0ssHDGCK4+HfpjnJN7Qbu9kVd7jxpoUKQ3CmNKCcMlkP 9rR9eMahFtwKowquTnw8LUqPBWToPpnFFFkciC1jCMhlvyb0MYBfjUCBK1YWW3Fg3A+b EgIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=ljs3G+Hi; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id k132-20020a633d8a000000b003ab15789bd8si1262513pga.198.2022.05.17.21.17.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 May 2022 21:17:19 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=ljs3G+Hi; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B489921E06; Tue, 17 May 2022 20:45:43 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234350AbiERCvM (ORCPT + 99 others); Tue, 17 May 2022 22:51:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234384AbiERCvK (ORCPT ); Tue, 17 May 2022 22:51:10 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F5F414D1F for ; Tue, 17 May 2022 19:51:07 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id q4so506640plr.11 for ; Tue, 17 May 2022 19:51:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Muu06nupwh+dYqU+u10ilHsVeZYqZAWfb+sLHhJaZYQ=; b=ljs3G+HiMoygvaVR8Rt7yJ2Nd2WzhNi6OfQtUHYlF+8Pgs/b2GvbZnI7x+Dq+QGqBt Kzb+XR5XQpjqvT76n7BLmu7joPqIHyAgx8e6EtRtvq5ldgL7hlkWuZJbKoYkDIF97rk9 0qhlXzFcES2PbUrlPoflHkEPbrEDy5+i6ITDkCtJOxgNOGWSQ3i9HQ07vJB4NgRoTfUu 4qRr0rBE3lT/S54N1vqnnI4TmDJ8z9p/Vnrq0P04HB/PqiiHNtD6vwEaB8WXmvJgvu9d 2IVXrQgdaMpiLL3vwCsY61R13y6WjVXffD6cQ9uaFhnQAl//Z3joXktTuakmw2pvZlCD rOOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Muu06nupwh+dYqU+u10ilHsVeZYqZAWfb+sLHhJaZYQ=; b=6vjAaBAbXhBVpJj7naKZt4IouHdGCa3JK/2EjtOCQJcOayF58WWrafJy7ZOrXi/yFY 41NET6Ly6qRVg32G8LNB6CiRqwJrG9x3ly5V7ez3sLHk97RjszO/Xh+T9VGBOUFKWzL/ Pl2Z/Wx1A+zmnxg4zBPC0eUT39EHczVI7+LuJCbeYi7ybwDLQYiHaOpgljnqef0MbBvj RVtkHwPC0VHiLmZXhGZyqz00A9h+2B4SKUmcVJU2RnlCQYTIahS2mpknjTfXr0+R+qQz WckHXyqET7gto4EegXti4ZgUxNlYNst5BwfJyf1TLtY4o7XNaDYk1CXYenh2o9ZhJQwX EaGQ== X-Gm-Message-State: AOAM533eg+szHiRwhCslfeeAUSwUKS0JRFtpHfbNGL0nwMHw15NXwV3b AVUp0TAn1KVEnv4rNe6wo6h2WQ== X-Received: by 2002:a17:90b:1b0d:b0:1dc:672e:c8c2 with SMTP id nu13-20020a17090b1b0d00b001dc672ec8c2mr28480250pjb.96.1652842266979; Tue, 17 May 2022 19:51:06 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.225]) by smtp.gmail.com with ESMTPSA id h67-20020a62de46000000b0050dc7628191sm468755pfg.107.2022.05.17.19.50.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 May 2022 19:51:06 -0700 (PDT) From: Feng zhou To: shuah@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, rostedt@goodmis.org, mingo@redhat.com, jolsa@kernel.org, davemarchevsky@fb.com, joannekoong@fb.com, geliang.tang@suse.com Cc: linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, wangdongdong.6@bytedance.com, cong.wang@bytedance.com, zhouchengming@bytedance.com, zhoufeng.zf@bytedance.com, yosryahmed@google.com Subject: [PATCH bpf-next v2] selftests/bpf: fix some bugs in map_lookup_percpu_elem testcase Date: Wed, 18 May 2022 10:50:53 +0800 Message-Id: <20220518025053.20492-1-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Feng Zhou comments from Andrii Nakryiko, details in here: https://lore.kernel.org/lkml/20220511093854.411-1-zhoufeng.zf@bytedance.com/T/ use /* */ instead of // use libbpf_num_possible_cpus() instead of sysconf(_SC_NPROCESSORS_ONLN) use 8 bytes for value size fix memory leak use ASSERT_EQ instead of ASSERT_OK add bpf_loop to fetch values on each possible CPU Fixes: ed7c13776e20c74486b0939a3c1de984c5efb6aa ("selftests/bpf: add test case for bpf_map_lookup_percpu_elem") Signed-off-by: Feng Zhou --- v1->v2: Addressed comments from Yonghong Song. - Adjust the code format more details can be seen from here: https://lore.kernel.org/lkml/80ab09cf-6072-a75a-082d-2721f6f907ef@fb.com/T/ .../bpf/prog_tests/map_lookup_percpu_elem.c | 50 +++++++++------ .../bpf/progs/test_map_lookup_percpu_elem.c | 62 ++++++++++++------- 2 files changed, 71 insertions(+), 41 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c b/tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c index 58b24c2112b0..f987c9278912 100644 --- a/tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c +++ b/tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c @@ -1,30 +1,38 @@ -// SPDX-License-Identifier: GPL-2.0 -// Copyright (c) 2022 Bytedance +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2022 Bytedance */ #include - #include "test_map_lookup_percpu_elem.skel.h" -#define TEST_VALUE 1 - void test_map_lookup_percpu_elem(void) { struct test_map_lookup_percpu_elem *skel; - int key = 0, ret; - int nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); - int *buf; + __u64 key = 0, sum; + int ret, i; + int nr_cpus = libbpf_num_possible_cpus(); + __u64 *buf; - buf = (int *)malloc(nr_cpus*sizeof(int)); + buf = (__u64 *)malloc(nr_cpus*sizeof(__u64)); if (!ASSERT_OK_PTR(buf, "malloc")) return; - memset(buf, 0, nr_cpus*sizeof(int)); - buf[0] = TEST_VALUE; - skel = test_map_lookup_percpu_elem__open_and_load(); - if (!ASSERT_OK_PTR(skel, "test_map_lookup_percpu_elem__open_and_load")) - return; + for (i = 0; i < nr_cpus; i++) + buf[i] = i; + sum = (nr_cpus-1)*nr_cpus/2; + + skel = test_map_lookup_percpu_elem__open(); + if (!ASSERT_OK_PTR(skel, "test_map_lookup_percpu_elem__open")) + goto exit; + + skel->rodata->nr_cpus = nr_cpus; + + ret = test_map_lookup_percpu_elem__load(skel); + if (!ASSERT_OK(ret, "test_map_lookup_percpu_elem__load")) + goto cleanup; + ret = test_map_lookup_percpu_elem__attach(skel); - ASSERT_OK(ret, "test_map_lookup_percpu_elem__attach"); + if (!ASSERT_OK(ret, "test_map_lookup_percpu_elem__attach")) + goto cleanup; ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_array_map), &key, buf, 0); ASSERT_OK(ret, "percpu_array_map update"); @@ -37,10 +45,14 @@ void test_map_lookup_percpu_elem(void) syscall(__NR_getuid); - ret = skel->bss->percpu_array_elem_val == TEST_VALUE && - skel->bss->percpu_hash_elem_val == TEST_VALUE && - skel->bss->percpu_lru_hash_elem_val == TEST_VALUE; - ASSERT_OK(!ret, "bpf_map_lookup_percpu_elem success"); + test_map_lookup_percpu_elem__detach(skel); + + ASSERT_EQ(skel->bss->percpu_array_elem_sum, sum, "percpu_array lookup percpu elem"); + ASSERT_EQ(skel->bss->percpu_hash_elem_sum, sum, "percpu_hash lookup percpu elem"); + ASSERT_EQ(skel->bss->percpu_lru_hash_elem_sum, sum, "percpu_lru_hash lookup percpu elem"); +cleanup: test_map_lookup_percpu_elem__destroy(skel); +exit: + free(buf); } diff --git a/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c b/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c index 5d4ef86cbf48..57e875d6e6e0 100644 --- a/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c +++ b/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c @@ -1,52 +1,70 @@ -// SPDX-License-Identifier: GPL-2.0 -// Copyright (c) 2022 Bytedance +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2022 Bytedance */ #include "vmlinux.h" #include -int percpu_array_elem_val = 0; -int percpu_hash_elem_val = 0; -int percpu_lru_hash_elem_val = 0; +__u64 percpu_array_elem_sum = 0; +__u64 percpu_hash_elem_sum = 0; +__u64 percpu_lru_hash_elem_sum = 0; +const volatile int nr_cpus; struct { __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); __uint(max_entries, 1); __type(key, __u32); - __type(value, __u32); + __type(value, __u64); } percpu_array_map SEC(".maps"); struct { __uint(type, BPF_MAP_TYPE_PERCPU_HASH); __uint(max_entries, 1); - __type(key, __u32); - __type(value, __u32); + __type(key, __u64); + __type(value, __u64); } percpu_hash_map SEC(".maps"); struct { __uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH); __uint(max_entries, 1); - __type(key, __u32); - __type(value, __u32); + __type(key, __u64); + __type(value, __u64); } percpu_lru_hash_map SEC(".maps"); +struct read_percpu_elem_ctx { + void *map; + __u64 sum; +}; + +static int read_percpu_elem_callback(__u32 index, struct read_percpu_elem_ctx *ctx) +{ + __u64 key = 0; + __u64 *value; + + value = bpf_map_lookup_percpu_elem(ctx->map, &key, index); + if (value) + ctx->sum += *value; + return 0; +} + SEC("tp/syscalls/sys_enter_getuid") int sysenter_getuid(const void *ctx) { - __u32 key = 0; - __u32 cpu = 0; - __u32 *value; + struct read_percpu_elem_ctx map_ctx; - value = bpf_map_lookup_percpu_elem(&percpu_array_map, &key, cpu); - if (value) - percpu_array_elem_val = *value; + map_ctx.map = &percpu_array_map; + map_ctx.sum = 0; + bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0); + percpu_array_elem_sum = map_ctx.sum; - value = bpf_map_lookup_percpu_elem(&percpu_hash_map, &key, cpu); - if (value) - percpu_hash_elem_val = *value; + map_ctx.map = &percpu_hash_map; + map_ctx.sum = 0; + bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0); + percpu_hash_elem_sum = map_ctx.sum; - value = bpf_map_lookup_percpu_elem(&percpu_lru_hash_map, &key, cpu); - if (value) - percpu_lru_hash_elem_val = *value; + map_ctx.map = &percpu_lru_hash_map; + map_ctx.sum = 0; + bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0); + percpu_lru_hash_elem_sum = map_ctx.sum; return 0; } -- 2.20.1