Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6809676iob; Wed, 11 May 2022 06:00:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJydsuQz0tIIYGeGFrOVKZU9cZ1c3npoja6eVxhgpoCK/eHyuiPHuEWTOFNbo3DLPbEugZWq X-Received: by 2002:a63:191e:0:b0:3db:27cb:6726 with SMTP id z30-20020a63191e000000b003db27cb6726mr2262036pgl.522.1652274019495; Wed, 11 May 2022 06:00:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652274019; cv=none; d=google.com; s=arc-20160816; b=sxuFGo3oRjS1rLyND1RgpTZy2+OKqRNyltxeu9VUoyRg/GCfMjxTc6HzPe9za1s1Mr Ze4E8N9t6KREluotJZlY2fV9Orq9uw4nmqA3BNpYzaZdSocAdv1+nHLep7bXL72SFVv4 IuatftwTKVsEOSZa6QYLZqsTVmjs8yX5iQZwOyNhJ8pC+wYLTQbgXHCC9QRzq8n542bg 3t/KRTu5Q65ZEgVh1xNd2rUN1L7bIaf58gFa2phAoCR6LVdRL5NowxPubnPpxQXrKPU/ dm4UxKlFjuggmtPot+tRKRLQqyfef/OBN48sGsebmFLwhW5a7+pKtTazCtGfwQQLAcZG Bosg== 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=/fUBFqLThKL0/oaZ4altcxEM2SUwyrO5ldQGIQjCN9A=; b=y+isCNiPsZ1AkCXsVCLQaCnJpmJj6G5N1Sm3BqRdcmQx5kHnWdf6mhH2rPP0IokuOp yduosvLjSzxrfHYdlZR2hmXoFDr/QwyLoBd/Tp2XxqAlcGjej1jsLU1BkNqKtuPb/V/8 6J7B03gLeke139RvNgPpVQvfx3tJUkaNSdME8WqPWqW2KB1cNi3UuwT8+feInPXp8mMX rsvH5lYRD+Tr7Yk6USHmAu5pEXQ0W/PxQ9Ok/+w9hskR0y3zRUsKfhT6+mryYaHagKSU EO4V03EdEpvlVH5ndh7Tn4Y7j1nlFWVNmmf74Odyy1XiSAlj0sHf3Zx8y/n1eqBAcw3j 4UtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=eCKuPbS3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s15-20020a65584f000000b003c6b378e37dsi2923735pgr.569.2022.05.11.06.00.07; Wed, 11 May 2022 06:00:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=eCKuPbS3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233919AbiEKJmO (ORCPT + 99 others); Wed, 11 May 2022 05:42:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232963AbiEKJkQ (ORCPT ); Wed, 11 May 2022 05:40:16 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C86B0994EC for ; Wed, 11 May 2022 02:39:45 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id p12so1548312pfn.0 for ; Wed, 11 May 2022 02:39:45 -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=/fUBFqLThKL0/oaZ4altcxEM2SUwyrO5ldQGIQjCN9A=; b=eCKuPbS3/VRMucuQKW3fV3J1Ax7JRz48UTyPqJ9d4QLXXrpHG/NK7WY8d+A+AM8hUp Z4WF3/AniPqFxeK7V7vGG0KrAQOCPyoLla6A1/xTMjxb72DJ+iiSG8IAdYNvzyFhb2zz f9UhYYkuakC1b6i/a/7nPdTssHoc63E0sv6onRn2fTnILoEc7Rs1Sutf09ywHPeXbcqL ErX0tg0HrFzZqIpEd44dcclLS5hYBENT77DOYHQ2l9HGeaSAsor+OqzpHZFKISFSHQ1u EwFScclZw++t3uWQxi2MjTgm82PAnmpAlaEAePYCgZYHtgbGezKLQ7hsXlgLaKg7L30Y m2MQ== 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=/fUBFqLThKL0/oaZ4altcxEM2SUwyrO5ldQGIQjCN9A=; b=xerbrjhqz5bzMKLELAUoqQksw8TXZKk2HYSmz70EqZlA8XnaPjZVQFJoxKp09mglQr 1U9ZwSto8BkR3KYdE4leNVAShWybwEgFYp5Pt6RtUW2nHOcm7XSSgmwsNC+aIvocLdb2 n60KMhpkooGmoMYZ/t4hUFE1CgdKeR6I6hXxUTBe82hevTWYNpbrYYGWIu9U6OTeoMa8 3SdGWGlHquptA5nwu8h9Fvq/LrbODBDc81zbsYhK1pVX/XZbRJO7y81DmqU7r/HNUm4i 6AEaI1kcPxN6hPLleLF32/UJH0Q6MCLPVS/T4iuLbBGiytGssXPFaeBP9cuKW1jVFs48 ZaTQ== X-Gm-Message-State: AOAM5338Ctd2lzQWI8wa27n4dPP2LvpynWBmiYDt5jUv2/KbIi6ez7RF EYVlEGM91A920FoKjai8FJEg6Q== X-Received: by 2002:a63:6904:0:b0:3c6:5a3c:64bd with SMTP id e4-20020a636904000000b003c65a3c64bdmr3032831pgc.371.1652261985323; Wed, 11 May 2022 02:39:45 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.241]) by smtp.gmail.com with ESMTPSA id k4-20020aa790c4000000b0050dc76281cdsm1159834pfk.167.2022.05.11.02.39.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 May 2022 02:39:45 -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 v2 2/2] selftests/bpf: add test case for bpf_map_lookup_percpu_elem Date: Wed, 11 May 2022 17:38:54 +0800 Message-Id: <20220511093854.411-3-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20220511093854.411-1-zhoufeng.zf@bytedance.com> References: <20220511093854.411-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,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 | 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 + +#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; + + buf = (int *)malloc(nr_cpus*sizeof(int)); + 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; + 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"); + + 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 + +#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; + + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.20.1