Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp933934iob; Fri, 13 May 2022 17:00:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxUsrwBqEITGtvnmhAINtWv9QSQidpXZQdlqxFITgKDr0piJE9BGcznq0z01/r+cFO9OGFV X-Received: by 2002:adf:f64b:0:b0:20a:c685:89ee with SMTP id x11-20020adff64b000000b0020ac68589eemr5437497wrp.366.1652486443501; Fri, 13 May 2022 17:00:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652486443; cv=none; d=google.com; s=arc-20160816; b=q599e9MWq73yMPAIg3IfPgcMsof2+tYY+GxI2DaXuEk5QxUXWqjoIX71oyG0dp4v35 RzIjHaS8iB9MJKgoIQYAHVVHVyht7cjYg3L/fldSVNfi8VHBeFOo5AVLV3T37FAI5ksn yFnD2ygnDV7W2aaPJa/uIsZa1V3JKMhUfYXPX4dfnWLhPzDsfXCq1ffVAr9ENJ43OrnZ WNf7FxVi/337/qPMb+JGMHcgoEGfwEvrNzj07syw946PCnZJhu3wru8ggkmul29xfIHc /7rt6ndDzrDE5ckewfoZt6sZQjsAdN5eLiZEqLAmhgjFmNqT3KhfMfADgmRwFUyLt8Nn kQ3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=ynf0e/virvZjVizU2QRUFjeoIWjt7fbHUUoT414b2mI=; b=a0FRCE9N/i7Y4UN5c7IB4UKP59ihkcGmleShwaznJaYAPYHmSlI7xd4hVwi4eESmX6 /To+eX0ZrgUYWk5D+SgGBDj9Gtc6nrSLAfv6/KLISiPKUgAZFJMBUjQUD/IpytJb0pcE fTKNzksK2iWvZsJbAB0vmIRWuWTAd/45k73iGqVg+msO/uFyo1Pf+uLd9blrWbtLMm3p XPODduwxx6zY/7Iqqpl9A901zi/KGtU9ro7HdDdRo8McbfgN+WFergAAV/ovGsYJ0YP1 O8qbZgFGnwHBwJHa4sqjrGt9+vOvCOII8iEmxf5510Yu/7HuB4AzUbcxjpIgq6QVUWyC IRVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=o71rtoZS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id a6-20020a056000188600b0020ad935a228si3401053wri.817.2022.05.13.17.00.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 17:00:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=o71rtoZS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 528AE8FD67; Fri, 13 May 2022 16:02:44 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344396AbiELDeh (ORCPT + 99 others); Wed, 11 May 2022 23:34:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344338AbiELDee (ORCPT ); Wed, 11 May 2022 23:34:34 -0400 Received: from mail-io1-xd31.google.com (mail-io1-xd31.google.com [IPv6:2607:f8b0:4864:20::d31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BEA66FD11; Wed, 11 May 2022 20:34:32 -0700 (PDT) Received: by mail-io1-xd31.google.com with SMTP id e194so4033750iof.11; Wed, 11 May 2022 20:34:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ynf0e/virvZjVizU2QRUFjeoIWjt7fbHUUoT414b2mI=; b=o71rtoZS1hWQgY+blTY49Fz1ge2cFxrR3Q3Djr44ei+5uqxh787BoRPdLH3/pXMqaf Bueuci8imJrDQOZXlZU5OWsX3P9NDq51ZSz0awKmdBS6j9JXdhQucobr8HOrKdz3fdGV rkm0YWlx4p+bRCEa7y8/v6VGdFMY3tfjCknHVZRxcSaX0QCh0eYJqPrpxInjT5NVyV96 jl7N9q30zbFwL4HXGrBEHXu8H51OAh2dk57hSkuMxuyOiY8tnIlXtq7YGurdLKfKFAeK VigX23GFtenA9s2iaKO+yr25hUT7+eBcXU+TQnNsNm/hHkH2ZuQY7lWrGeRdJ3ELXmRA zbSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ynf0e/virvZjVizU2QRUFjeoIWjt7fbHUUoT414b2mI=; b=Mojp6YOmsdrTbiF4+XfW8YlYJ65ad7cANWiZdyIoG55rZ5gFRveC9rsrRmxHuUNjIF r/FqiLBy4gNJymXaSkHj656Q/NYpSnSyML3beP7RWT3G4Xj7SApJEEeWETFUGMZAJZan +hHk6GTTtW692jHWj0JFl+L3qMWNTpGYuvt3l8ncVn87ioUWp/lJhk5fu/Ga8zWi6uJr jXvBdv7Iu1OOdVuEdFmxpjZ1wLbpDIqlbc/xnvSjLt7P5wfd9W+8C4wuYKT1XqisljFV HQ/5+Wz79uAyrf+vkjFZRaupznUaxWcVgSqIZ0NRq+VmmrJJAu2Ypq8eCKQIZ6fDucgS 5f1g== X-Gm-Message-State: AOAM531u7xTxNbZ4t3RIu3fiNVEvUOr/txm8ioFNM8sZ48TTFDcschVo efKo86AQJRZcr7D09KTxMxJzrW45vwVsu30yMfk= X-Received: by 2002:a5e:8e42:0:b0:657:bc82:64e5 with SMTP id r2-20020a5e8e42000000b00657bc8264e5mr11982398ioo.112.1652326471691; Wed, 11 May 2022 20:34:31 -0700 (PDT) MIME-Version: 1.0 References: <20220511093854.411-1-zhoufeng.zf@bytedance.com> <20220511093854.411-3-zhoufeng.zf@bytedance.com> In-Reply-To: <20220511093854.411-3-zhoufeng.zf@bytedance.com> From: Andrii Nakryiko Date: Wed, 11 May 2022 20:34:20 -0700 Message-ID: Subject: Re: [PATCH bpf-next v2 2/2] selftests/bpf: add test case for bpf_map_lookup_percpu_elem To: Feng zhou Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin Lau , Song Liu , Yonghong Song , john fastabend , KP Singh , Steven Rostedt , Ingo Molnar , Jiri Olsa , Dave Marchevsky , Joanne Koong , Geliang Tang , Networking , bpf , open list , duanxiongchun@bytedance.com, Muchun Song , Dongdong Wang , Cong Wang , zhouchengming@bytedance.com, yosryahmed@google.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 On Wed, May 11, 2022 at 2:39 AM Feng zhou wrote: > > From: Feng Zhou > > test_progs: > Tests new ebpf helpers bpf_map_lookup_percpu_elem. > > Signed-off-by: Feng Zhou > --- > .../bpf/prog_tests/map_lookup_percpu_elem.c | 46 ++++++++++++++++ > .../bpf/progs/test_map_lookup_percpu_elem.c | 54 +++++++++++++++++++ > 2 files changed, 100 insertions(+) > create mode 100644 tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c > create mode 100644 tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c > > 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 > new file mode 100644 > index 000000000000..58b24c2112b0 > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c > @@ -0,0 +1,46 @@ > +// SPDX-License-Identifier: GPL-2.0 > +// Copyright (c) 2022 Bytedance /* */ instead of // > + > +#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); I think this is actually wrong and will break selftests on systems with offline CPUs. Please use libbpf_num_possible_cpus() instead. > + int *buf; > + > + buf = (int *)malloc(nr_cpus*sizeof(int)); > + if (!ASSERT_OK_PTR(buf, "malloc")) > + return; > + memset(buf, 0, nr_cpus*sizeof(int)); this is wrong, kernel expects to have roundup(sz, 8) per each CPU, while you have just 4 bytes per each element please also have spaces around multiplication operator here and above > + 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; buf leaking here > + ret = test_map_lookup_percpu_elem__attach(skel); > + ASSERT_OK(ret, "test_map_lookup_percpu_elem__attach"); > + > + ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_array_map), &key, buf, 0); > + ASSERT_OK(ret, "percpu_array_map update"); > + > + ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_hash_map), &key, buf, 0); > + ASSERT_OK(ret, "percpu_hash_map update"); > + > + ret = bpf_map_update_elem(bpf_map__fd(skel->maps.percpu_lru_hash_map), &key, buf, 0); > + ASSERT_OK(ret, "percpu_lru_hash_map update"); > + > + 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"); this would be better done as three separate ASSERT_EQ(), combining into opaque true/false isn't helpful if something breaks > + > + test_map_lookup_percpu_elem__destroy(skel); > +} > 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 > new file mode 100644 > index 000000000000..5d4ef86cbf48 > --- /dev/null > +++ b/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c > @@ -0,0 +1,54 @@ > +// SPDX-License-Identifier: GPL-2.0 > +// Copyright (c) 2022 Bytedance /* */ instead of // > + > +#include "vmlinux.h" > +#include > + > +int percpu_array_elem_val = 0; > +int percpu_hash_elem_val = 0; > +int percpu_lru_hash_elem_val = 0; > + > +struct { > + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); > + __uint(max_entries, 1); > + __type(key, __u32); > + __type(value, __u32); > +} percpu_array_map SEC(".maps"); > + > +struct { > + __uint(type, BPF_MAP_TYPE_PERCPU_HASH); > + __uint(max_entries, 1); > + __type(key, __u32); > + __type(value, __u32); > +} percpu_hash_map SEC(".maps"); > + > +struct { > + __uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH); > + __uint(max_entries, 1); > + __type(key, __u32); > + __type(value, __u32); > +} percpu_lru_hash_map SEC(".maps"); > + > +SEC("tp/syscalls/sys_enter_getuid") > +int sysenter_getuid(const void *ctx) > +{ > + __u32 key = 0; > + __u32 cpu = 0; > + __u32 *value; > + > + value = bpf_map_lookup_percpu_elem(&percpu_array_map, &key, cpu); > + if (value) > + percpu_array_elem_val = *value; > + > + value = bpf_map_lookup_percpu_elem(&percpu_hash_map, &key, cpu); > + if (value) > + percpu_hash_elem_val = *value; > + > + value = bpf_map_lookup_percpu_elem(&percpu_lru_hash_map, &key, cpu); > + if (value) > + percpu_lru_hash_elem_val = *value; > + if the test happens to run on CPU 0 then the test doesn't really test much. It would be more interesting to have a bpf_loop() iteration that would fetch values on each possible CPU instead and do something with it. > + return 0; > +} > + > +char _license[] SEC("license") = "GPL"; > -- > 2.20.1 >