From patchwork Wed Oct 18 14:40:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Francis Laniel X-Patchwork-Id: 154907 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp4839203vqb; Wed, 18 Oct 2023 07:41:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHX9Dxlw2cOdp+sTKKBp2bjKSyr+Io/b9kDQAqq7i7x8jciKA0GotUlDnmafrpsiIlibnOv X-Received: by 2002:a17:902:d04c:b0:1b8:2ba0:c9a8 with SMTP id l12-20020a170902d04c00b001b82ba0c9a8mr5349739pll.2.1697640072421; Wed, 18 Oct 2023 07:41:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697640072; cv=none; d=google.com; s=arc-20160816; b=O2gHGa3UliWHWA5Fj+Zkqm+mvgse9Eyt2B+qdaD80vrjoP/EcfB4ZGxpDNPtAapoie QaswFkaSvnsOO5/vOOJ44goHqPIB0Xeg0OBjzQyAUgtX51ZfD16ShQUnt7mDjOolghBp VRGpRqQ07NbKuyT6bqHIUUpoiXRGTOQ3sqqvg9AxRx4H9G1lD52o5baXdEoj2rhaSWYB mNwAIQVMrm4H2Hg0PdiqLfusmyEhF5KyOtQI6zsyJHXAeJWINZqJ7xvMsP7i784mq7mp 3QbF90841zdmLpsUbDaP7y8OuSI9rMeQwvnaf0SbbCYL8bwsa6LPt96yLXePiz7hxSE4 6MHA== 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:dkim-filter; bh=ZyhmO3X6jBWaHqcLJasVL9JKLb1Dgh5tpNMbuRYa2ik=; fh=NSAbnaDam6sUkll6gF7SlPBrZXaf+dda4tr8h/bsVHs=; b=hodBRm5K0Uj2MMc+uEcDaqBW1Fgp+u2fbnLaU2F3mCCMgowvyUelm5OT6BkjA8HAWt mfa6h9goEC03ZLBz9t4UKaFWPdeqDOtmLmJQasRL43wuhMX+hSh1KC6TVV8z971bWIoO jXncOUzf8oP9prrWvzg9FY5+lMq5nk/0lwPR4anqXNsD6B9Tw/eQ1Lfuxh8CteROYM1R 8LRHYSb7RlkLn0PL9yJK10+Pq6GBX726Fim+VA+GUv/ydxnpibXyBPJ3WGR5uLcCQZrM FvUQJg8E6Qr5pv5hzKmqMdAQQMlkzLBMWBpw/0z1r2VmjQsaCvAqG53/dtKfD6PU/91/ ePJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=FQMjO0kL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id j1-20020a170902690100b001c9d7d70d7bsi15999plk.4.2023.10.18.07.41.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 07:41:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=FQMjO0kL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 24D438226F07; Wed, 18 Oct 2023 07:41:10 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344891AbjJROlC (ORCPT + 23 others); Wed, 18 Oct 2023 10:41:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231970AbjJROlA (ORCPT ); Wed, 18 Oct 2023 10:41:00 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B893210D; Wed, 18 Oct 2023 07:40:58 -0700 (PDT) Received: from pwmachine.numericable.fr (unknown [188.24.154.80]) by linux.microsoft.com (Postfix) with ESMTPSA id 5688720B74C1; Wed, 18 Oct 2023 07:40:56 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 5688720B74C1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1697640058; bh=ZyhmO3X6jBWaHqcLJasVL9JKLb1Dgh5tpNMbuRYa2ik=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FQMjO0kLaNOHH1rthm3YX7spa7A90Q8QVHXqVikjkfWN+6bcWWrma6ygpS5FtXO79 MrxIIywW4yN/Hn+3nD0VDMsUDoGUoFrv/ckDcJMog+PT5FoIr5Ss+A8SszB8VUi6Aw h4aLTyOwkk6tJ+q0JmtogLYfJAOdiLdjbkt6lbaQ= From: Francis Laniel To: linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , stable@vger.kernel.org, Francis Laniel , Steven Rostedt , linux-kernel@vger.kernel.org Subject: [PATCH v5 1/2] tracing/kprobes: Return EADDRNOTAVAIL when func matches several symbols Date: Wed, 18 Oct 2023 17:40:29 +0300 Message-Id: <20231018144030.86885-2-flaniel@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231018144030.86885-1-flaniel@linux.microsoft.com> References: <20231018144030.86885-1-flaniel@linux.microsoft.com> MIME-Version: 1.0 X-Spam-Status: No, score=-8.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 18 Oct 2023 07:41:10 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780104636685040785 X-GMAIL-MSGID: 1780104636685040785 Previously to this commit, if func matches several symbols, a kprobe, being either sysfs or PMU, would only be installed for the first matching address. This could lead to some misunderstanding when some BPF code was never called because it was attached to a function which was indeed not called, because the effectively called one has no kprobes attached. So, this commit returns EADDRNOTAVAIL when func matches several symbols. This way, user needs to use address to remove the ambiguity. Suggested-by: Masami Hiramatsu Signed-off-by: Francis Laniel Link: https://lore.kernel.org/lkml/20230819101105.b0c104ae4494a7d1f2eea742@kernel.org/ --- kernel/trace/trace_kprobe.c | 63 +++++++++++++++++++++++++++++++++++++ kernel/trace/trace_probe.h | 1 + 2 files changed, 64 insertions(+) -- 2.34.1 diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 3d7a180a8427..a8fef6ab0872 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -705,6 +705,25 @@ static struct notifier_block trace_kprobe_module_nb = { .priority = 1 /* Invoked after kprobe module callback */ }; +static int count_symbols(void *data, unsigned long unused) +{ + unsigned int *count = data; + + (*count)++; + + return 0; +} + +static unsigned int number_of_same_symbols(char *func_name) +{ + unsigned int count; + + count = 0; + kallsyms_on_each_match_symbol(count_symbols, func_name, &count); + + return count; +} + static int __trace_kprobe_create(int argc, const char *argv[]) { /* @@ -836,6 +855,31 @@ static int __trace_kprobe_create(int argc, const char *argv[]) } } + if (symbol && !strchr(symbol, ':')) { + unsigned int count; + + count = number_of_same_symbols(symbol); + if (count > 1) { + /* + * Users should use ADDR to remove the ambiguity of + * using KSYM only. + */ + trace_probe_log_err(0, NON_UNIQ_SYMBOL); + ret = -EADDRNOTAVAIL; + + goto error; + } else if (count == 0) { + /* + * We can return ENOENT earlier than when register the + * kprobe. + */ + trace_probe_log_err(0, BAD_PROBE_ADDR); + ret = -ENOENT; + + goto error; + } + } + trace_probe_log_set_index(0); if (event) { ret = traceprobe_parse_event_name(&event, &group, gbuf, @@ -1695,6 +1739,7 @@ static int unregister_kprobe_event(struct trace_kprobe *tk) } #ifdef CONFIG_PERF_EVENTS + /* create a trace_kprobe, but don't add it to global lists */ struct trace_event_call * create_local_trace_kprobe(char *func, void *addr, unsigned long offs, @@ -1705,6 +1750,24 @@ create_local_trace_kprobe(char *func, void *addr, unsigned long offs, int ret; char *event; + if (func) { + unsigned int count; + + count = number_of_same_symbols(func); + if (count > 1) + /* + * Users should use addr to remove the ambiguity of + * using func only. + */ + return ERR_PTR(-EADDRNOTAVAIL); + else if (count == 0) + /* + * We can return ENOENT earlier than when register the + * kprobe. + */ + return ERR_PTR(-ENOENT); + } + /* * local trace_kprobes are not added to dyn_event, so they are never * searched in find_trace_kprobe(). Therefore, there is no concern of diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h index 02b432ae7513..850d9ecb6765 100644 --- a/kernel/trace/trace_probe.h +++ b/kernel/trace/trace_probe.h @@ -450,6 +450,7 @@ extern int traceprobe_define_arg_fields(struct trace_event_call *event_call, C(BAD_MAXACT, "Invalid maxactive number"), \ C(MAXACT_TOO_BIG, "Maxactive is too big"), \ C(BAD_PROBE_ADDR, "Invalid probed address or symbol"), \ + C(NON_UNIQ_SYMBOL, "The symbol is not unique"), \ C(BAD_RETPROBE, "Retprobe address must be an function entry"), \ C(NO_TRACEPOINT, "Tracepoint is not found"), \ C(BAD_ADDR_SUFFIX, "Invalid probed address suffix"), \