From patchwork Sat Aug 5 14:58:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 131498 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp531391vqr; Sat, 5 Aug 2023 09:13:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGXXptOt1k6ihBycPRaL1iT6yr5kpRcoXlI6E6qk7L0xvHOCRL2fxgv5a4y7QZVc6TzTj3T X-Received: by 2002:a17:906:300d:b0:99c:56d1:79fd with SMTP id 13-20020a170906300d00b0099c56d179fdmr4054692ejz.51.1691252016126; Sat, 05 Aug 2023 09:13:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691252016; cv=none; d=google.com; s=arc-20160816; b=LTsdQHq/8QQRzmQdRYIzNxKvN/LQ1dgz53DK0gCbD/es+OElmi8HrRx+JFy8hieOOn oqj6u0m2pMKO64WN+Y2NzEGJ5kVSW7G2aK5lgAEv4pGeOdBgW3aVPYErD8XqMz5/KTqq KaYuXD8w7j9+ZB10S7rCfUScrVrlAWqctDocLzFlKNyqizZvkbz+vr3LryMmA+1B6VJ6 fGrdsgA52EbX11JhHL4gakoRY4iJuMinPaFXatqz/kP08/GZaAvdSJE1Hyu5/T3EGmc3 RcNOm49SH5yR49edLPInr2f7ExdxZbVSdd6Zruqk6CSxd8U3aEf7UPMsdzqlIhRNt/3q DmqA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wJAFM/KxDG3NWUGMYaBWFKu80BnXO+X0VTkUu90CSEY=; fh=nNLeqPmm07c4DrdDej5z+HdJpmx5SqFFnpeka2nrMVM=; b=UTsvXJ9EzoWZbVIFdNPJgbTeFMzay5AyTD/kUz7nYH+04/V5Nf1u9/wGoM1CzM860e /w4rTLGYT1CSAuqeY59kUzRHxBred42LJwaFwKWAL1MAxfFUcFZ1iZ4FdWtBJe++mN67 7cjCcj7VCXvv5V3Fs/doVsgkcazw4iQlHpupv7UWF3O0yUwPnb+PXpq6B+JE2JOag1Pl LRVHIddNdlzeiFEFrbb8mWVV1tbhsR7dy1+eW/9K8vCFRzoxLSS5653gnBjy+5XooZdF ik4WF2rCJDeUnGNH6i1nwi5NXR7eLdyRpH4uIiIqg8PrhiaC1ZNHvHt6rcUq3RJqLtDU CEVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dtQWDnhA; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n5-20020a170906840500b0099cb49c2dd9si249009ejx.445.2023.08.05.09.13.10; Sat, 05 Aug 2023 09:13:36 -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=@kernel.org header.s=k20201202 header.b=dtQWDnhA; 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 S229906AbjHEO7N (ORCPT + 99 others); Sat, 5 Aug 2023 10:59:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229884AbjHEO7L (ORCPT ); Sat, 5 Aug 2023 10:59:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA70F44A1; Sat, 5 Aug 2023 07:58:54 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4D2EF601D0; Sat, 5 Aug 2023 14:58:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1491FC433C8; Sat, 5 Aug 2023 14:58:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1691247533; bh=zlG4XdXdEpC54rcZoPhBS/p5nrcyfvD1Z3WKRvALoqk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dtQWDnhA/NNtISReu5UUc99txh1LiTHylfhpoJVKtscA39myvE3AjoY5vdFp+9saC xnoUS1YK5XUGkOlYCC3UmM/pNaW783FDsz0WtBtCcNpu5wLwQRVG1V4wpTcOVHEvOE pKg3I9bPvV1xchqwNA9yzgoV9vtzu1pxTMTt7i2dIYBftfJ0RGmTKntCAMu42N6/gW AOGDiWEbqU/e8yEtS5oU2VEAjpL2CKhpLNOAry8SW56hqnZzQOC5iGYzcRpNQTHxZu HjaIeks7HQPRENMJKOesbKGi3o1P+7vfUFcvCDrtyz7tbkHdqWE9gEVOIL2038uDX2 VxsWES0gtZKug== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Sven Schnelle , Alexei Starovoitov , Jiri Olsa , Arnaldo Carvalho de Melo , Daniel Borkmann , Alan Maguire , Mark Rutland , Peter Zijlstra , Thomas Gleixner Subject: [RFC PATCH 5/5] bpf: Enable kprobe_multi feature if CONFIG_FPROBE is enabled Date: Sat, 5 Aug 2023 23:58:47 +0900 Message-Id: <169124752752.186149.5928887430024942007.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <169124746774.186149.2326708176801468806.stgit@devnote2> References: <169124746774.186149.2326708176801468806.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773406273597760318 X-GMAIL-MSGID: 1773406273597760318 From: Masami Hiramatsu (Google) Enable kprobe_multi feature if CONFIG_FPROBE is enabled. The pt_regs is converted from ftrace_regs by ftrace_partial_regs(), thus some registers may always returns 0. But it should be enough for function entry (access arguments) and exit (access return value). Signed-off-by: Masami Hiramatsu (Google) --- kernel/trace/bpf_trace.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 99c5f95360f9..0725272a3de2 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2460,7 +2460,7 @@ static int __init bpf_event_init(void) fs_initcall(bpf_event_init); #endif /* CONFIG_MODULES */ -#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS +#ifdef CONFIG_FPROBE struct bpf_kprobe_multi_link { struct bpf_link link; struct fprobe fp; @@ -2482,6 +2482,8 @@ struct user_syms { char *buf; }; +static DEFINE_PER_CPU(struct pt_regs, bpf_kprobe_multi_pt_regs); + static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt) { unsigned long __user usymbol; @@ -2623,13 +2625,14 @@ static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) static int kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, - unsigned long entry_ip, struct pt_regs *regs) + unsigned long entry_ip, struct ftrace_regs *fregs) { struct bpf_kprobe_multi_run_ctx run_ctx = { .link = link, .entry_ip = entry_ip, }; struct bpf_run_ctx *old_run_ctx; + struct pt_regs *regs; int err; if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) { @@ -2639,6 +2642,7 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, migrate_disable(); rcu_read_lock(); + regs = ftrace_partial_regs(fregs, this_cpu_ptr(&bpf_kprobe_multi_pt_regs)); old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); err = bpf_prog_run(link->link.prog, regs); bpf_reset_run_ctx(old_run_ctx); @@ -2656,13 +2660,9 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip, void *data) { struct bpf_kprobe_multi_link *link; - struct pt_regs *regs = ftrace_get_regs(fregs); - - if (!regs) - return 0; link = container_of(fp, struct bpf_kprobe_multi_link, fp); - kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs); + kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), fregs); return 0; } @@ -2672,13 +2672,9 @@ kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip, void *data) { struct bpf_kprobe_multi_link *link; - struct pt_regs *regs = ftrace_get_regs(fregs); - - if (!regs) - return; link = container_of(fp, struct bpf_kprobe_multi_link, fp); - kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), regs); + kprobe_multi_link_prog_run(link, get_entry_ip(fentry_ip), fregs); } static int symbols_cmp_r(const void *a, const void *b, const void *priv) @@ -2918,7 +2914,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr kvfree(cookies); return err; } -#else /* !CONFIG_DYNAMIC_FTRACE_WITH_REGS */ +#else /* !CONFIG_FPROBE */ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) { return -EOPNOTSUPP;