Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1049892iob; Fri, 13 May 2022 21:06:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw/oRQeHHsvaFN0zKaUO35/XnERaKFY5q0q2YR6WstbVy6+ScEKZ11PrUaD6IPCfm0qB5Rc X-Received: by 2002:a5d:6d0d:0:b0:20c:530c:1683 with SMTP id e13-20020a5d6d0d000000b0020c530c1683mr6322074wrq.109.1652501190267; Fri, 13 May 2022 21:06:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652501190; cv=none; d=google.com; s=arc-20160816; b=gqa6SywRvjTw5gMzSTF5sNoNbXxJ6Lat9OqWYm+Mb0ZnUFOhDZB6PM4S5DpaQ0AB50 YfpTMFm2fNplhm5w2oIXHOW0GUMhVwLWs6zNgn8dOo7qT3R+f1ldK1NeuEHMi6RvrIAa w4y6P8NDxbT1eb3nFLRgnVknlaD9JwBDCJW9psKXCZM0XYrD9qOLD14Tt8VdwbxdiKEa 3LBFJhKhRQEokpV1wbv9NrE1YnxuFxNi8LYK1fTwzZd1usdP5est3n92jZCTosUhGbBD ihnOXdaB1g7OWpuTOa6u2qauiRqlN/BedNXKdfLO6mEAZCBxuBdODl1+0tLazng2VajY XPlQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gEB3NbHEFaXkY+7O5LinimDmPz0C+lpCHDDyO2s1SG0=; b=J8jh4j9kMR+CETkTa89/4HfYxcTry8S6b9B1ahnHul5fpbvwoZ39RFRXX2oXJQ7HAS GKQmNDLGlTyqBwvU4Ujc8sx76v5w3tlS9n/GXk8z7AXhFSOwLPL3Be1+hWd6jqDgFZbP F+f0+TdRVFrqcytM5ALitQrRfzAXzMLffmM7QD2jFVYVQCtYNm9vGUOAvw0YyoZxYab+ jk67f9Z6q0abcj3fpXiQFNn/WSiM7kqQlMrpYEoKd3q7ggt2VC4ef9o8rymfXr533VGL Up8AcabpOT7f0hKWrIfTMAWHEAYuJ3JtkjcfjEE2izFCG9/u8NzHZVMdTxAEKaNl3zic QKow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=j9XBSBxJ; 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=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id k8-20020adff288000000b0020a7955c896si3485752wro.754.2022.05.13.21.06.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 21:06:30 -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=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=j9XBSBxJ; 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=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 9307A466F00; Fri, 13 May 2022 17:38:01 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377421AbiEMGka (ORCPT + 99 others); Fri, 13 May 2022 02:40:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377417AbiEMGk2 (ORCPT ); Fri, 13 May 2022 02:40:28 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96DF62A28D1 for ; Thu, 12 May 2022 23:40:23 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id 31so6649893pgp.8 for ; Thu, 12 May 2022 23:40:23 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=gEB3NbHEFaXkY+7O5LinimDmPz0C+lpCHDDyO2s1SG0=; b=j9XBSBxJAHXR/iQiKohExEY1bxn8g015nWIKUkNnn+CWX0u7t/ULIFexyBCN0iYoiV 1wcBiIUMECDQSY1AzqnU4H5qRenJI8E5o4F845ZL2WkkOXzUXnF2eBMMZlFq86dp6a8G hlFexIB7iFK4UTWYLukjtZt2s65cU+W3n968vljMcj6XzO5dUOopEZeoBEoZKqwZ+tYB f04kE2YYKNrXZeApMNRSnyEeNXeM0NWvJp4POhoG2O+FN2dFRwI4NffXdpoUA9afxWIO AhbOR2HMFdhkvcBPDgVGmeW6FJIwQG35fl4lWZRUwUtkis3EGSofHKJLnvW/BMdE+9B4 GlIQ== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=gEB3NbHEFaXkY+7O5LinimDmPz0C+lpCHDDyO2s1SG0=; b=5SIiELCfTc+v5JdzX9FdjFQrwRCM8zZDPRRTIa88ZLAL0ptNaXMCb4XqZcNN8NFI6Y smImVb7MfVEMYyaJnXT7nJAZNOhEWcv7q3Ath8eGtBkAEBXFYjEDLhEOHNbtpo8nd97X IxPExedgBuHFsf4Ell6ffaNWDWivD6hCabrSA2WkIr7TDyqyEvq9acse1Oopo3EUtb05 Ii9WwdTQgCR3EqJYpAANU4oQvfI8Hri/LHEU9dABbzpcThKu86nNCxe35OLA5bnJatFa jygCqPAoNl4wnxZly8rbrW60SfKdiH+jQosG1h/FZCjtKKt5FNrnFXJ8852MSbvX0wxf JRuQ== X-Gm-Message-State: AOAM532edLcBLodl/h/UhGi9jyd3dF61o2uNKb4ARXO/22VwNWCt+17k SgqqmSEwlS6h6X71tvKUOvSZHg== X-Received: by 2002:a63:2c4a:0:b0:3c1:df82:cf0e with SMTP id s71-20020a632c4a000000b003c1df82cf0emr2831567pgs.474.1652424022912; Thu, 12 May 2022 23:40:22 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.225]) by smtp.gmail.com with ESMTPSA id 185-20020a6204c2000000b0050dc76281d8sm934990pfe.178.2022.05.12.23.40.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 May 2022 23:40:22 -0700 (PDT) From: Feng zhou To: 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: 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 v3 2/2] selftests/bpf: add test case for bpf_map_lookup_percpu_elem Date: Fri, 13 May 2022 14:39:52 +0800 Message-Id: <20220513063952.41794-3-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20220513063952.41794-1-zhoufeng.zf@bytedance.com> References: <20220513063952.41794-1-zhoufeng.zf@bytedance.com> 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 test_progs: Tests new ebpf helpers bpf_map_lookup_percpu_elem. Signed-off-by: Feng Zhou --- .../bpf/prog_tests/map_lookup_percpu_elem.c | 59 +++++++++++++++ .../bpf/progs/test_map_lookup_percpu_elem.c | 71 +++++++++++++++++++ 2 files changed, 130 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..89ca170f1c25 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/map_lookup_percpu_elem.c @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2022 Bytedance */ + +#include + +#include "test_map_lookup_percpu_elem.skel.h" + +void test_map_lookup_percpu_elem(void) +{ + struct test_map_lookup_percpu_elem *skel; + __u64 key = 0, sum; + int ret, i; + int nr_cpus = libbpf_num_possible_cpus(); + __u64 *buf; + + buf = (__u64 *)malloc(nr_cpus*sizeof(__u64)); + if (!ASSERT_OK_PTR(buf, "malloc")) + return; + + for (i=0; irodata->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); + 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"); + + 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); + + 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 new file mode 100644 index 000000000000..75479180571f --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c @@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2022 Bytedance */ + +#include "vmlinux.h" +#include + +__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, __u64); +} percpu_array_map SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_HASH); + __uint(max_entries, 1); + __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, __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) +{ + struct read_percpu_elem_ctx map_ctx; + + 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; + + 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; + + 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; +} + +char _license[] SEC("license") = "GPL"; -- 2.20.1