From patchwork Fri Nov 4 13:07:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wang <00107082@163.com> X-Patchwork-Id: 15506 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp389878wru; Fri, 4 Nov 2022 06:08:37 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6cE+dHUuZLnd7UmvvUAZqlibFdy2C4Syx4sYLuGN8i1Qxxlpx8FlOwxJTGOqEKIhELKnms X-Received: by 2002:a17:90b:4c07:b0:213:44f5:6073 with SMTP id na7-20020a17090b4c0700b0021344f56073mr36660027pjb.184.1667567317362; Fri, 04 Nov 2022 06:08:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667567317; cv=none; d=google.com; s=arc-20160816; b=JnlaY1bN28G9M7otxnwaRxdvn/Sjlmbk5b9uJpxLBYcRRC5jauXIDtnIM17sMDCFLH Hf92cyYZ+R2HM3XjCe3361L7YFXfSFf2SC+b5rlbFu3jut9aUO5GXZXTnFQF8kymAKvA /LotC74SkFUPOXcM/Pjyu1xDRBS6aaXOdLcYgwUjy1TcJMPlN75U1jR7VE0+aMByxCyG 0QE0DnZLmVVAAsk4JJqJ9FEtVPDXs5YrCM+sYOAkd5b/gA4mvGtNhU9sRHQ0wvhDROQ7 ABw1BZcsLMMFOJoIqV6/2rMH1vX7dR/I9PMZ8oM47PNjtYW8yEkYTmM8LMBoyTV/0q9C IGdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:mime-version :content-transfer-encoding:subject:to:from:date:dkim-signature; bh=E+XzMKU05SBCjZ1akHyN80VngofkywOG1jO+qLwFMRQ=; b=u3XfbabYqWpdQFOlqUecXd8+lQbmqbS65RNyoYPh78yZA8cBrB/wBWdjea+JtrPlXc Y2QiM1dUH4R6cOkIkGJw5YppO6gYwNLdK1RBKYhAxIfDvleXj6+gfun1PNcK6NWg2PLa i7K0jyGE3khJDpSZvcbBD5FN3JYUz7UZebvhrp0JE+9jm8jh8bHFgsB6E4wS9rUEIzvJ 2kulSAOr3+mW0lyqLh4GF5Mf35XUED6a+/NRyLpzvZtC9OqCgJtxD2d2iJfEqwZzZl8A 4BVuraTfGhM+zUGRZfqqEDdXTHMlwk0yKIMjKqOLmAzOUTAPDVqMkOghp4/K7qF/lZAO CIJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@163.com header.s=s110527 header.b="Yp/+ETeK"; 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=163.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f15-20020a63f10f000000b0045ce0eba520si904202pgi.751.2022.11.04.06.08.18; Fri, 04 Nov 2022 06:08:37 -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=neutral (body hash did not verify) header.i=@163.com header.s=s110527 header.b="Yp/+ETeK"; 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=163.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230205AbiKDNHl (ORCPT + 99 others); Fri, 4 Nov 2022 09:07:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230021AbiKDNHj (ORCPT ); Fri, 4 Nov 2022 09:07:39 -0400 Received: from m13134.mail.163.com (m13134.mail.163.com [220.181.13.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 89F142E6B9 for ; Fri, 4 Nov 2022 06:07:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=Date:From:Subject:MIME-Version:Message-ID; bh=M9PbT Gakjv/3zqqgp2j3iH+TiftOFmtoxz74GES9Wdo=; b=Yp/+ETeK6T2LXqYxNALmY CjF/byF4HURdXx4RLah+6zhP35r5VkVwOboxj1jrufD3Huwl3mLjk2nmx5qKIukL jH4Wm9IVgMZ82BCTNoxEw7M2dmVa305eNgdRcpXK/+l17Guqna9gmuaoq4IT3hmJ RsPUcQbRcv+LsrJ3c2keeM= Received: from 00107082$163.com ( [222.64.157.165] ) by ajax-webmail-wmsvr134 (Coremail) ; Fri, 4 Nov 2022 21:07:35 +0800 (CST) X-Originating-IP: [222.64.157.165] Date: Fri, 4 Nov 2022 21:07:35 +0800 (CST) From: "David Wang" <00107082@163.com> To: "linux-kernel@vger.kernel.org" Subject: [PATCH] trace/trace_uprobe: Only invoke uprobe ebpf handler when event matches. X-Priority: 3 X-Mailer: Coremail Webmail Server Version XT5.0.13 build 20220113(9671e152) Copyright (c) 2002-2022 www.mailtech.cn 163com X-NTES-SC: AL_QuydBfmduEou4yWfZekZnEoUjug3W8K5v/kk3Y9VOp80iivC1Q4adlVPLEL4686kOQeHmjKpQSRf6PtgR4pKcqx5q1y7eiLUqlPJsufzuHT5 MIME-Version: 1.0 Message-ID: <41828b90.5798.18442c10017.Coremail.00107082@163.com> X-Coremail-Locale: zh_CN X-CM-TRANSID: hsGowACH_t2XDmVjmj5cAA--.49235W X-CM-SenderInfo: qqqrilqqysqiywtou0bp/1tbiEAivql8YLKr7GgABs8 X-Coremail-Antispam: 1U5529EdanIXcx71UUUUU7vcSsGvfC2KfnxnUU== X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748571067213768119?= X-GMAIL-MSGID: =?utf-8?q?1748571067213768119?= Only invoke uprobe ebpf handler when event matches. uprobe ebpf handler would be called even the event dose not match any registered perf event, following steps would be used to generate a unregistered perf event. 1. register a uprobe event on a specified pid 2. invokes syscall `clone` (via pthread_create), new process generated. (Maybe it is a bug here, the uprobe breakpoint is inherited from 3. invokes the function which is uprobed in step 1. 4. perf event generated... Ebpf handler would be invoked even the event happened on , but the default perf event handler make further check and ignore the event because no registered perf event match on . The patch means to fix the inconsistent behavior between ebpf and the default. Before invoke uprobe ebpf handler, make sure current event match. Signed-off-by: David Wang <00107082@163.com> Reported-by: kernel test robot Signed-off-by: David Wang <00107082@163.com> --- -- diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c index fb58e86dd117..6f13163c0c0f 100644 --- a/kernel/trace/trace_uprobe.c +++ b/kernel/trace/trace_uprobe.c @@ -1346,27 +1346,20 @@ static void __uprobe_perf_func(struct trace_uprobe *tu, void *data; int size, esize; int rctx; + bool trace_event_match = true; -#ifdef CONFIG_BPF_EVENTS - if (bpf_prog_array_valid(call)) { - u32 ret; - ret = bpf_prog_run_array_sleepable(call->prog_array, regs, bpf_prog_run); - if (!ret) - return; + preempt_disable(); + head = this_cpu_ptr(call->perf_events); + if (hlist_empty(head)) { + trace_event_match = false; + goto out; } -#endif /* CONFIG_BPF_EVENTS */ esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); - size = esize + tu->tp.size + dsize; size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32); if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough")) - return; - - preempt_disable(); - head = this_cpu_ptr(call->perf_events); - if (hlist_empty(head)) goto out; entry = perf_trace_buf_alloc(size, NULL, &rctx); @@ -1389,11 +1382,21 @@ static void __uprobe_perf_func(struct trace_uprobe *tu, memset(data + len, 0, size - esize - len); } - perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, head, NULL); out: preempt_enable(); + +#ifdef CONFIG_BPF_EVENTS + if (trace_event_match && bpf_prog_array_valid(call)) { + u32 ret; + + ret = bpf_prog_run_array_sleepable(call->prog_array, regs, bpf_prog_run); + if (!ret) + return; + } +#endif /* CONFIG_BPF_EVENTS */ + } /* uprobe profile handler */