Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3845386rdg; Wed, 18 Oct 2023 07:41:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGrf/JixhdBFjyYt+VSvPVEcI98Gbr4AExYYDdOxVgC6uZn/DaEsnhvThbfIU8YWR1SOH7Q X-Received: by 2002:a17:90a:bf85:b0:27d:e73:3077 with SMTP id d5-20020a17090abf8500b0027d0e733077mr5265904pjs.1.1697640087467; Wed, 18 Oct 2023 07:41:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697640087; cv=none; d=google.com; s=arc-20160816; b=EPfhIwDmJ75A5KzUmPybFxj0hFA7VgGBKWi8qe6m5soDfNwr5Z/PpacuBTrJtinbpc Jhk44e9yU4pNMD/Hikfkz5Im190vs0hRTW/7cTyfocbVr5r8EqVhbJhjfF+masZ9X1zP DPZVDQr8F3aTOcck3z9RJJhCvAypW0sGNjarvhQtWgVNhx0ADxzwEGICoVVBL+Dqpq9U hEBWorBrpByAraDmbNnv1y1kmOL24axSEaqYQeRtbxuZjsORNAPxMhBxf4BAIWPB0HiG N/uyGc7G0jaUCf4gVHgSgwMYYtnYAK5yghVJ8ohGnDc729tnem+WHJBGGbXw0m7kNlVN +Low== 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 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id 66-20020a17090a09c800b00273f1cd73b2si8253pjo.75.2023.10.18.07.41.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 07:41:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; 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 2620:137:e000::3:5 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 groat.vger.email (Postfix) with ESMTP id 7160580293F5; Wed, 18 Oct 2023 07:41:24 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344811AbjJROlC (ORCPT + 99 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 Content-Transfer-Encoding: 8bit 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 groat.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 (groat.vger.email [0.0.0.0]); Wed, 18 Oct 2023 07:41:24 -0700 (PDT) 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(+) 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"), \ -- 2.34.1