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"), \ From patchwork Wed Oct 18 14:40:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Francis Laniel X-Patchwork-Id: 154908 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp4839405vqb; Wed, 18 Oct 2023 07:41:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGP1nvyIgdu6giYboif2zEOugd/BXX77lPQo2A1ppaiWePA6icmgplH2aFrXgrewjrryAdM X-Received: by 2002:a05:6359:320a:b0:166:d975:8dab with SMTP id rj10-20020a056359320a00b00166d9758dabmr4864541rwb.1.1697640091672; Wed, 18 Oct 2023 07:41:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697640091; cv=none; d=google.com; s=arc-20160816; b=rbTOJylm9x9cqurD2KOi1qEJoCA1KZgkZkx8rwlbS30USodqILyH6KMRlpWMDAFBQ0 cWw9XZKVjM9SqLaYPhjxceCQfhjMr3pgXkZURAeFMdPU1PKn8s/LUjNSyPOUYP3QMlf/ iNw1aObqvxJTJuL/EctDBZUp64K1tepES9nKdf8fhT02IHdYN6ruNOdFfXc6xhJQTp4P J1zW3GbVlj+yCCpfK5QfwyUI+wAcBmZqkU+tm2WG3fs8tlLIZ9Z1dhoTfkr423WF7U5h +Xwu5bFWtd9mz5z5+HprNndM8ZGAojjvf6wPdOrqAtVRK2xsMryICcAYLxAOteAxNZ3V FYFA== 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=xfU7t1LgNcdblzWvppUIKK8lYqVIT5V5QuCHhVdi4XA=; fh=C+HpNNXT2Lb0cuD1vcC5eXxufB72DUxrdImbb4uYsfw=; b=YakW9C0QI9x581KMx0u6PrT6WvF/uvO3C8wttlY8RP0zJWRs01En0WJGG+4wZIKdkT JKWLC8UQU+8zpAoIywQQd43IWNxE6c/q0E+8zrdpl7vRgXyeHDI7lqDvErmeR4RQiQr+ unqK057tZr0yFmyczQYgn1oUAeHJyPDfsRLqQxgkk2S+jN9REPUvTyBy2tcwHNDrsp69 0QxWrADcMXExRDOLrZ2yb8XmiWAbPX7/cJBUdo/nM7qAOi/DSkAyoELX2rmvzf32u4zu rye+HwPXYBOuPmLtUvShMIBORkVwLqE4rF5bS3eLTWcQd36NjkMOovtHT3g4gU4DdgtT mx7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=jj8G1UuN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id x4-20020a056a00188400b006be30cdc3d8si4222296pfh.163.2023.10.18.07.41.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 07:41:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=jj8G1UuN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (Postfix) with ESMTP id 155C18022C3C; Wed, 18 Oct 2023 07:41:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235242AbjJROlL (ORCPT + 23 others); Wed, 18 Oct 2023 10:41:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231984AbjJROlJ (ORCPT ); Wed, 18 Oct 2023 10:41:09 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C4679112; Wed, 18 Oct 2023 07:41:04 -0700 (PDT) Received: from pwmachine.numericable.fr (unknown [188.24.154.80]) by linux.microsoft.com (Postfix) with ESMTPSA id E6C8E20B74C2; Wed, 18 Oct 2023 07:41:01 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com E6C8E20B74C2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1697640064; bh=xfU7t1LgNcdblzWvppUIKK8lYqVIT5V5QuCHhVdi4XA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jj8G1UuN9Bk/jt7qcdNl+3JbxLcchuZl5ENkdpTuNUHmsmpsB+5UyK3Da+JmSQxRE iByd2j7wE4UOQvR9Pj58rqHZZb03r+jez6TUt9GqPMe+z43DeK3+J5EEri0uGVFWcw IO4XORpsoyWdS8ndO6+MKMW5RVrWhs8Z3mrBTyWo= From: Francis Laniel To: linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , stable@vger.kernel.org, Francis Laniel , Steven Rostedt , Shuah Khan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v5 2/2] selftests/ftrace: Add new test case which checks non unique symbol Date: Wed, 18 Oct 2023 17:40:30 +0300 Message-Id: <20231018144030.86885-3-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 pete.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 (pete.vger.email [0.0.0.0]); Wed, 18 Oct 2023 07:41:29 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780104656397817885 X-GMAIL-MSGID: 1780104656397817885 If name_show() is non unique, this test will try to install a kprobe on this function which should fail returning EADDRNOTAVAIL. On kernel where name_show() is not unique, this test is skipped. Signed-off-by: Francis Laniel --- .../ftrace/test.d/kprobe/kprobe_non_uniq_symbol.tc | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 tools/testing/selftests/ftrace/test.d/kprobe/kprobe_non_uniq_symbol.tc -- 2.34.1 diff --git a/tools/testing/selftests/ftrace/test.d/kprobe/kprobe_non_uniq_symbol.tc b/tools/testing/selftests/ftrace/test.d/kprobe/kprobe_non_uniq_symbol.tc new file mode 100644 index 000000000000..bc9514428dba --- /dev/null +++ b/tools/testing/selftests/ftrace/test.d/kprobe/kprobe_non_uniq_symbol.tc @@ -0,0 +1,13 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Test failure of registering kprobe on non unique symbol +# requires: kprobe_events + +SYMBOL='name_show' + +# We skip this test on kernel where SYMBOL is unique or does not exist. +if [ "$(grep -c -E "[[:alnum:]]+ t ${SYMBOL}" /proc/kallsyms)" -le '1' ]; then + exit_unsupported +fi + +! echo "p:test_non_unique ${SYMBOL}" > kprobe_events