Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1862169pxp; Thu, 10 Mar 2022 13:43:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJxkB8SB6B9mE/fO/csKg8Ps6I3grRLMabuq97QjRHdvmGbdmCo+R+KkIBY1LwKtffpeyBno X-Received: by 2002:a17:906:7954:b0:6da:9ee0:2e54 with SMTP id l20-20020a170906795400b006da9ee02e54mr6232109ejo.630.1646948580048; Thu, 10 Mar 2022 13:43:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646948580; cv=none; d=google.com; s=arc-20160816; b=FrSHsBAp3AMqbmaPQh59guSfY+F9FWfb7+vk5db7xhF69IBXOcvg2Omqx1maZqtK5m qs9Srs0NWCekANq/b/ThjQ+LObHHwN9yTUaFHq97c9SMPNezfz3WL4RqFSGtNC6k74wc uB4QM+8GUmqVpbY36VEUYZtLamSzrUt4WlrYQ1LcKLZ1HPOEx0GEarWTsyzLdQAHnAd9 2BGt8XEm9ivzpt/nOQTgxeX2u8iMCiqKg23lc+KeBpVsuWx4SPZ93JoGgXhUsPIjDlIw k3DrVPvudbG/lulaM1hdw+JxjcMp/N3nCYDFzZQT/iLSxcTdu93rVkq2InqtgdmZg399 jZnA== 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:subject:cc:to:from:date :dkim-signature; bh=RU+zWSFVVcSVPnsaguqk+2yiFY/0cqjwh6QeUiO+RN4=; b=EFLJrRusrK7SJ5ubn5fROJIsoZ3AND+Z5GPp2xlZG2B4u8KW4ewU6vioFbzgnhTILJ zKvdXG2yZz2UjpavErRTc9pQh7u/mfWBEJ0o6t8o2vRKc3+LXlW/qDDWzXRHtKJatYJf aR22GNlPaXt9IPhkWfhGxAa9d0U5PlrNNSaQGuoAaOk4skYRWlpeGVJOBGSOW7vaDhYh CEuXZWrEl7N9ePM/NoPYCYBZvO6XszQ9nfjDHxQptSfNufCJkgYTjuaYTxB622JRFosY g/81GhVE+VoSfAFHqzpzj2+oCeyMz5nr82LtbYcgPMhyjq1uTx2pEEwSAMVLG9amBmRA P0Gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LDiSDCl9; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q7-20020a17090676c700b006b42e81bba6si3927886ejn.731.2022.03.10.13.42.36; Thu, 10 Mar 2022 13:43:00 -0800 (PST) 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=@kernel.org header.s=k20201202 header.b=LDiSDCl9; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244837AbiCJPOe (ORCPT + 99 others); Thu, 10 Mar 2022 10:14:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345927AbiCJPNZ (ORCPT ); Thu, 10 Mar 2022 10:13:25 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88F5818CC16; Thu, 10 Mar 2022 07:11:11 -0800 (PST) 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 ams.source.kernel.org (Postfix) with ESMTPS id 43A9CB826A7; Thu, 10 Mar 2022 15:11:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8E2AC340E8; Thu, 10 Mar 2022 15:11:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1646925069; bh=JvrfE5TvRk4CMy3XwYhop6gLRKNZsMIKrNNEdCW1/oo=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=LDiSDCl9M7475v50zFK02U0ry3YwyAFVxWIMbEVqbDFFK1nqR8XT9B/+KGxXhoAQz zVLRHRIZBWVaNFZjU4bNS6RdPQNFQUyA6Av767HI8u4K0EHPyLjRNa/AuMxvveKzOo lBixxntNbWXiuuc0OGO2hSSNBUkIw4scx96KIpf0Bg5EQ83VcVyyUWtyfkyOd6atoM ERWZbv8EN5SG0Pn5cMx1Hv6Xc9CZwg+53HUVP3Tmdh1F1GcUZm52p1fhpc+EWHwSxz MH6X4Npq8BfenoG9l/DCVBXfdE0i3qLW57Xr8VBzDsbB2Gn9o74HPwTGWc2uo8Qp3X cAx9MePe2ux5g== Date: Fri, 11 Mar 2022 00:11:03 +0900 From: Masami Hiramatsu To: Andrii Nakryiko Cc: Jiri Olsa , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Networking , bpf , lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , "Naveen N . Rao" , Anil S Keshavamurthy , "David S . Miller" Subject: Re: [PATCH v10 12/12] fprobe: Add a selftest for fprobe Message-Id: <20220311001103.6d3f30c80175b0d169e3f4f6@kernel.org> In-Reply-To: References: <164673771096.1984170.8155877393151850116.stgit@devnote2> <164673784786.1984170.244480726272055433.stgit@devnote2> <20220310091745.73580bd6314803cfbf21312d@kernel.org> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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, 9 Mar 2022 16:40:00 -0800 Andrii Nakryiko wrote: > On Wed, Mar 9, 2022 at 4:17 PM Masami Hiramatsu wrote: > > > > Hi, > > > > On Tue, 8 Mar 2022 20:10:48 +0900 > > Masami Hiramatsu wrote: > > > > > Add a KUnit based selftest for fprobe interface. > > > > > > Signed-off-by: Masami Hiramatsu > > > --- > > > Changes in v9: > > > - Rename fprobe_target* to fprobe_selftest_target*. > > > - Find the correct expected ip by ftrace_location_range(). > > > - Since the ftrace_location_range() is not exposed to module, make > > > this test only for embedded. > > > - Add entry only test. > > > - Reset the fprobe structure before reuse it. > > > --- > > > lib/Kconfig.debug | 12 ++++ > > > lib/Makefile | 2 + > > > lib/test_fprobe.c | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++ > > > 3 files changed, 188 insertions(+) > > > create mode 100644 lib/test_fprobe.c > > > > > > diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug > > > index 14b89aa37c5c..ffc469a12afc 100644 > > > --- a/lib/Kconfig.debug > > > +++ b/lib/Kconfig.debug > > > @@ -2100,6 +2100,18 @@ config KPROBES_SANITY_TEST > > > > > > Say N if you are unsure. > > > > > > +config FPROBE_SANITY_TEST > > > + bool "Self test for fprobe" > > > + depends on DEBUG_KERNEL > > > + depends on FPROBE > > > + depends on KUNIT > > > > Hmm, this caused a build error with allmodconfig because KUNIT=m but FPROBE_SANITY_TEST=y. > > Let me fix this issue. > > Please base on top of bpf-next and add [PATCH v11 bpf-next] to subject. OK, let me rebase on it. There are master and for-next branch, which one is better to use? Thank you, > > > > > Thank you, > > > > > + help > > > + This option will enable testing the fprobe when the system boot. > > > + A series of tests are made to verify that the fprobe is functioning > > > + properly. > > > + > > > + Say N if you are unsure. > > > + > > > config BACKTRACE_SELF_TEST > > > tristate "Self test for the backtrace code" > > > depends on DEBUG_KERNEL > > > diff --git a/lib/Makefile b/lib/Makefile > > > index 300f569c626b..154008764b16 100644 > > > --- a/lib/Makefile > > > +++ b/lib/Makefile > > > @@ -103,6 +103,8 @@ obj-$(CONFIG_TEST_HMM) += test_hmm.o > > > obj-$(CONFIG_TEST_FREE_PAGES) += test_free_pages.o > > > obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o > > > obj-$(CONFIG_TEST_REF_TRACKER) += test_ref_tracker.o > > > +CFLAGS_test_fprobe.o += $(CC_FLAGS_FTRACE) > > > +obj-$(CONFIG_FPROBE_SANITY_TEST) += test_fprobe.o > > > # > > > # CFLAGS for compiling floating point code inside the kernel. x86/Makefile turns > > > # off the generation of FPU/SSE* instructions for kernel proper but FPU_FLAGS > > > diff --git a/lib/test_fprobe.c b/lib/test_fprobe.c > > > new file mode 100644 > > > index 000000000000..ed70637a2ffa > > > --- /dev/null > > > +++ b/lib/test_fprobe.c > > > @@ -0,0 +1,174 @@ > > > +// SPDX-License-Identifier: GPL-2.0-or-later > > > +/* > > > + * test_fprobe.c - simple sanity test for fprobe > > > + */ > > > + > > > +#include > > > +#include > > > +#include > > > +#include > > > + > > > +#define div_factor 3 > > > + > > > +static struct kunit *current_test; > > > + > > > +static u32 rand1, entry_val, exit_val; > > > + > > > +/* Use indirect calls to avoid inlining the target functions */ > > > +static u32 (*target)(u32 value); > > > +static u32 (*target2)(u32 value); > > > +static unsigned long target_ip; > > > +static unsigned long target2_ip; > > > + > > > +static noinline u32 fprobe_selftest_target(u32 value) > > > +{ > > > + return (value / div_factor); > > > +} > > > + > > > +static noinline u32 fprobe_selftest_target2(u32 value) > > > +{ > > > + return (value / div_factor) + 1; > > > +} > > > + > > > +static notrace void fp_entry_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs) > > > +{ > > > + KUNIT_EXPECT_FALSE(current_test, preemptible()); > > > + /* This can be called on the fprobe_selftest_target and the fprobe_selftest_target2 */ > > > + if (ip != target_ip) > > > + KUNIT_EXPECT_EQ(current_test, ip, target2_ip); > > > + entry_val = (rand1 / div_factor); > > > +} > > > + > > > +static notrace void fp_exit_handler(struct fprobe *fp, unsigned long ip, struct pt_regs *regs) > > > +{ > > > + unsigned long ret = regs_return_value(regs); > > > + > > > + KUNIT_EXPECT_FALSE(current_test, preemptible()); > > > + if (ip != target_ip) { > > > + KUNIT_EXPECT_EQ(current_test, ip, target2_ip); > > > + KUNIT_EXPECT_EQ(current_test, ret, (rand1 / div_factor) + 1); > > > + } else > > > + KUNIT_EXPECT_EQ(current_test, ret, (rand1 / div_factor)); > > > + KUNIT_EXPECT_EQ(current_test, entry_val, (rand1 / div_factor)); > > > + exit_val = entry_val + div_factor; > > > +} > > > + > > > +/* Test entry only (no rethook) */ > > > +static void test_fprobe_entry(struct kunit *test) > > > +{ > > > + struct fprobe fp_entry = { > > > + .entry_handler = fp_entry_handler, > > > + }; > > > + > > > + current_test = test; > > > + > > > + /* Before register, unregister should be failed. */ > > > + KUNIT_EXPECT_NE(test, 0, unregister_fprobe(&fp_entry)); > > > + KUNIT_EXPECT_EQ(test, 0, register_fprobe(&fp_entry, "fprobe_selftest_target*", NULL)); > > > + > > > + entry_val = 0; > > > + exit_val = 0; > > > + target(rand1); > > > + KUNIT_EXPECT_NE(test, 0, entry_val); > > > + KUNIT_EXPECT_EQ(test, 0, exit_val); > > > + > > > + entry_val = 0; > > > + exit_val = 0; > > > + target2(rand1); > > > + KUNIT_EXPECT_NE(test, 0, entry_val); > > > + KUNIT_EXPECT_EQ(test, 0, exit_val); > > > + > > > + KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp_entry)); > > > +} > > > + > > > +static void test_fprobe(struct kunit *test) > > > +{ > > > + struct fprobe fp = { > > > + .entry_handler = fp_entry_handler, > > > + .exit_handler = fp_exit_handler, > > > + }; > > > + > > > + current_test = test; > > > + KUNIT_EXPECT_EQ(test, 0, register_fprobe(&fp, "fprobe_selftest_target*", NULL)); > > > + > > > + entry_val = 0; > > > + exit_val = 0; > > > + target(rand1); > > > + KUNIT_EXPECT_NE(test, 0, entry_val); > > > + KUNIT_EXPECT_EQ(test, entry_val + div_factor, exit_val); > > > + > > > + entry_val = 0; > > > + exit_val = 0; > > > + target2(rand1); > > > + KUNIT_EXPECT_NE(test, 0, entry_val); > > > + KUNIT_EXPECT_EQ(test, entry_val + div_factor, exit_val); > > > + > > > + KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp)); > > > +} > > > + > > > +static void test_fprobe_syms(struct kunit *test) > > > +{ > > > + static const char *syms[] = {"fprobe_selftest_target", "fprobe_selftest_target2"}; > > > + struct fprobe fp = { > > > + .entry_handler = fp_entry_handler, > > > + .exit_handler = fp_exit_handler, > > > + }; > > > + > > > + current_test = test; > > > + KUNIT_EXPECT_EQ(test, 0, register_fprobe_syms(&fp, syms, 2)); > > > + > > > + entry_val = 0; > > > + exit_val = 0; > > > + target(rand1); > > > + KUNIT_EXPECT_NE(test, 0, entry_val); > > > + KUNIT_EXPECT_EQ(test, entry_val + div_factor, exit_val); > > > + > > > + entry_val = 0; > > > + exit_val = 0; > > > + target2(rand1); > > > + KUNIT_EXPECT_NE(test, 0, entry_val); > > > + KUNIT_EXPECT_EQ(test, entry_val + div_factor, exit_val); > > > + > > > + KUNIT_EXPECT_EQ(test, 0, unregister_fprobe(&fp)); > > > +} > > > + > > > +static unsigned long get_ftrace_location(void *func) > > > +{ > > > + unsigned long size, addr = (unsigned long)func; > > > + > > > + if (!kallsyms_lookup_size_offset(addr, &size, NULL) || !size) > > > + return 0; > > > + > > > + return ftrace_location_range(addr, addr + size - 1); > > > +} > > > + > > > +static int fprobe_test_init(struct kunit *test) > > > +{ > > > + do { > > > + rand1 = prandom_u32(); > > > + } while (rand1 <= div_factor); > > > + > > > + target = fprobe_selftest_target; > > > + target2 = fprobe_selftest_target2; > > > + target_ip = get_ftrace_location(target); > > > + target2_ip = get_ftrace_location(target2); > > > + > > > + return 0; > > > +} > > > + > > > +static struct kunit_case fprobe_testcases[] = { > > > + KUNIT_CASE(test_fprobe_entry), > > > + KUNIT_CASE(test_fprobe), > > > + KUNIT_CASE(test_fprobe_syms), > > > + {} > > > +}; > > > + > > > +static struct kunit_suite fprobe_test_suite = { > > > + .name = "fprobe_test", > > > + .init = fprobe_test_init, > > > + .test_cases = fprobe_testcases, > > > +}; > > > + > > > +kunit_test_suites(&fprobe_test_suite); > > > + > > > +MODULE_LICENSE("GPL"); > > > > > > > > > -- > > Masami Hiramatsu -- Masami Hiramatsu