Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp5871646ioo; Wed, 1 Jun 2022 14:35:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyEtZLRtn/AKX2nCm6vyWFT7vbJUtrQ4tohrEmqef5qQyDJ71SxowYYZFJ09hBVOmBVQD+2 X-Received: by 2002:a17:902:ce8f:b0:163:cc85:ba89 with SMTP id f15-20020a170902ce8f00b00163cc85ba89mr1382885plg.79.1654119316040; Wed, 01 Jun 2022 14:35:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654119316; cv=none; d=google.com; s=arc-20160816; b=NRzeHRnO6iyG69Y0X9anvXg3Rm02hgpx8YlsAwecJtTw+/lRGoFk0WtiEqM4dzOnlf C1lTDQHd+RDS43PWTTjvAReTA72ojEu+CnLWiyTh8XxbqcyAfeo+62L8/F0Z8TYfIJU+ HbHfHf/+T3vlV8yTwLYT3fJqMrRohjzE+cMf/kkDVVNXn9VNt4PQGIT9+2mlpkvlVjYm f1/hKP9V4KrN+WovDzWDwCLCOWMXp/4EXlEjxewLkipnjyrDyM96kjCMHUJ3YnlWVLMi TEjuHNYf39tT7ZeZO3mc48E9W0jMeRoI8uji+rRrF22c87WWMOcu1KHEKR0Zfoy8EzHs 8SKA== 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=/jeuanqa4QSFF72ecQdWIsYZTnsdhrNbdxEpbiyYgr0=; b=xOaF++EKbV38RrSNdVQWgjqpb3379eHU10QJ3NFNrdSULQ6MulZ3ut6kwHQDXbzKAF uAA9c7Dw7MCc3TD7sVlPylbbkg987fzSeV2msxM1bw/6R6fP9iBYaThB4byb60IFTx1q TJMPpaQUyT3tKeuGzMarx23mPPuKiJiNoY85OPtn8ErH9gWIS/irlgEvVR5kQOqNW01g UjV1qjBhsTURy0+P3voLnS7j6gcuuOd7i1dnhYepayobArvQx4jg1catMDH2AngcFAmq Y1rbpcOKsX9mxDj4AFEGJBdEq3RHgOJy0glk9ea9eQmR4lywN1KQa44Mxwi+dJlqULP4 PMHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=a1kBtGxe; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id lx12-20020a17090b4b0c00b001e67c8309a5si963192pjb.9.2022.06.01.14.35.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 14:35:16 -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=@kernel.org header.s=k20201202 header.b=a1kBtGxe; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B053F1A8810; Wed, 1 Jun 2022 13:42:43 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346446AbiEaRLh (ORCPT + 99 others); Tue, 31 May 2022 13:11:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236090AbiEaRLf (ORCPT ); Tue, 31 May 2022 13:11:35 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 777D58BD1E; Tue, 31 May 2022 10:11:34 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id EC64860A54; Tue, 31 May 2022 17:11:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 631D5C3411D; Tue, 31 May 2022 17:11:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1654017093; bh=PiJTFDNVbEqwodufEmKeOFpsiPkT1+3psU+PLhFiVvU=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=a1kBtGxeowAhq9hi8kGTWlPnjN6B174ifLpjM0UytOV0o/RNsLoF97qVr5j31gMHo 8UQ9FmXxVkOntl7m1kJNMvlMQahstqASa/9BC5oh2XcmhM2C5GTJYbgZLcq1wAXYtz a1hRp9RsIGzu7Va8u8+dvr4Zu9T57Osn1ip4rNLIiwZmb2OGzY5KGvMlWldrqDv1sf 8rBaurh9N9UnJ7QJuSG8E7kbrQXxJTsGoJ9KnbxocUfWHqomOLPmqDeqS2mcDiMwMW SPr73kr0YRSgFvuA5fgcuz3gekf3c19qRq+l/abo4ZmhT0PFeiDssuavc4h1hDXPHA owNDiCUGP40tA== Received: by mail-yb1-f169.google.com with SMTP id i11so24997869ybq.9; Tue, 31 May 2022 10:11:33 -0700 (PDT) X-Gm-Message-State: AOAM532IHZ3o/Zo1ERSBqel7rb6JQ+Zj1jKFHLGfd7C7PGnRQh33Eym9 phGv4HSgqSiBHuLlQKPidHpzoif8jr7Z0sXgh5s= X-Received: by 2002:a25:4705:0:b0:65d:43f8:5652 with SMTP id u5-20020a254705000000b0065d43f85652mr3648473yba.389.1654017092431; Tue, 31 May 2022 10:11:32 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Song Liu Date: Tue, 31 May 2022 10:11:21 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH bpf-next 2/2] selftests/bpf: Add PROG_TEST_RUN selftest for BPF_PROG_TYPE_KPROBE To: Daniel Xu Cc: bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , open list Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,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 On Sun, May 29, 2022 at 3:06 PM Daniel Xu wrote: > > This commit adds a selftest to test that we can both PROG_TEST_RUN a > kprobe prog and set its context. nit: per Documentation/process/submitting-patches.rst: Describe your changes in imperative mood, e.g. "make xyzzy do frotz" instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy to do frotz", as if you are giving orders to the codebase to change its behaviour. > > Signed-off-by: Daniel Xu Other than that, Acked-by: Song Liu > --- > .../selftests/bpf/prog_tests/kprobe_ctx.c | 57 +++++++++++++++++++ > .../testing/selftests/bpf/progs/kprobe_ctx.c | 33 +++++++++++ > 2 files changed, 90 insertions(+) > create mode 100644 tools/testing/selftests/bpf/prog_tests/kprobe_ctx.c > create mode 100644 tools/testing/selftests/bpf/progs/kprobe_ctx.c > > diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_ctx.c b/tools/testing/selftests/bpf/prog_tests/kprobe_ctx.c > new file mode 100644 > index 000000000000..260966fd4506 > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/kprobe_ctx.c > @@ -0,0 +1,57 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#include > +#include > +#include "kprobe_ctx.skel.h" > + > +/* > + * x86_64 happens to be one of the architectures that exports the > + * kernel `struct pt_regs` to userspace ABI. For the architectures > + * that don't, users will have to extract `struct pt_regs` from vmlinux > + * BTF in order to use BPF_PROG_TYPE_KPROBE's BPF_PROG_RUN functionality. > + * > + * We choose to only test x86 here to keep the test simple. > + */ > +void test_kprobe_ctx(void) > +{ > +#ifdef __x86_64__ > + struct pt_regs regs = { > + .rdi = 1, > + .rsi = 2, > + .rdx = 3, > + .rcx = 4, > + .r8 = 5, > + }; > + > + LIBBPF_OPTS(bpf_test_run_opts, tattr, > + .ctx_in = ®s, > + .ctx_size_in = sizeof(regs), > + ); > + > + struct kprobe_ctx *skel = NULL; > + int prog_fd; > + int err; > + > + skel = kprobe_ctx__open_and_load(); > + if (!ASSERT_OK_PTR(skel, "skel_open")) > + return; > + > + skel->bss->expected_p1 = (void *)1; > + skel->bss->expected_p2 = (void *)2; > + skel->bss->expected_p3 = (void *)3; > + skel->bss->expected_p4 = (void *)4; > + skel->bss->expected_p5 = (void *)5; > + > + prog_fd = bpf_program__fd(skel->progs.prog); > + err = bpf_prog_test_run_opts(prog_fd, &tattr); > + if (!ASSERT_OK(err, "bpf_prog_test_run")) > + goto cleanup; > + > + if (!ASSERT_TRUE(skel->bss->ret, "ret")) > + goto cleanup; > + > + if (!ASSERT_GT(tattr.duration, 0, "duration")) > + goto cleanup; > +cleanup: > + kprobe_ctx__destroy(skel); > +#endif > +} > diff --git a/tools/testing/selftests/bpf/progs/kprobe_ctx.c b/tools/testing/selftests/bpf/progs/kprobe_ctx.c > new file mode 100644 > index 000000000000..98063c549930 > --- /dev/null > +++ b/tools/testing/selftests/bpf/progs/kprobe_ctx.c > @@ -0,0 +1,33 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +#include "vmlinux.h" > +#include > +#include > + > +volatile void *expected_p1; > +volatile void *expected_p2; > +volatile void *expected_p3; > +volatile void *expected_p4; > +volatile void *expected_p5; > +volatile bool ret = false; > + > +SEC("kprobe/this_function_does_not_exist") > +int prog(struct pt_regs *ctx) > +{ > + void *p1, *p2, *p3, *p4, *p5; > + > + p1 = (void *)PT_REGS_PARM1(ctx); > + p2 = (void *)PT_REGS_PARM2(ctx); > + p3 = (void *)PT_REGS_PARM3(ctx); > + p4 = (void *)PT_REGS_PARM4(ctx); > + p5 = (void *)PT_REGS_PARM5(ctx); > + > + if (p1 != expected_p1 || p2 != expected_p2 || p3 != expected_p3 || > + p4 != expected_p4 || p5 != expected_p5) > + return 0; > + > + ret = true; > + return 0; > +} > + > +char _license[] SEC("license") = "GPL"; > -- > 2.36.1 >