Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1170073pxb; Fri, 21 Jan 2022 11:21:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJzr23/EhOhE248g1PGg5gLmyXfPurAw2ojoIAzb4vvOVn11HDOyyti5G0zVq5G4JcqFcggK X-Received: by 2002:a17:90a:4407:: with SMTP id s7mr1155141pjg.126.1642792868109; Fri, 21 Jan 2022 11:21:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642792868; cv=none; d=google.com; s=arc-20160816; b=GsU+07N7DyO5mXKznP5etCfj7NEv0ruyDBB2Q9f4jfmAXp7IejDGxK5MlpkeZzS34C S7yNHcw1toejBFD2HADYlWJEHXx9vbscqmu9PJuqG72iUkz6kyt2sX22m/UZ1tnGH5g9 R6lDLev1MTwfrEfklhUTn2p2/zkTARsIKUyneKhAA8s01iJ0mYSOSggbty1PDBYfqp3B vFPAmEr2PbAT2MjfVHOYzgIFEJ7um3Er9TJZjPyEJDmVTOS6yBIQJZ60zN3hMGDksmqL 7WgxyxEGayxl3HxJtuu6CXv0hCc9FtV+HAd4MlYWNlJG5UzTrLYa/7FNBgj6YSYhzikl Z0zA== 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=AkYexpUlktm0c98fCKoBBYNyMAnVlNtGFKOec6WB2Qk=; b=WKJ9U+y7aHllOJX+PXBsQLBKFFuCLd9gbd2bkd9BWOLh/nscxq+NdY5lymo+36s7Jw j/rKCM++76da9rvUL94jAqMbs62WF3OJ7PwZs40TpjEbzsDVsPY0U/ePY+5q6uuM87hm ufIxtSXn40axbKnIXw81rXPfrkdUWoWEAXBCla6nqdHJXnBXYY/5dTUMz8Q99z3bGfFQ IEuGZvnIFlI4lb6TtFBzHGDbuwJ/7SjzGgoiWuEBQCIJDF06/JJaP6VcGILAqr+Bwsg9 Qd5kWeMtmZ/EHDTF308w7zWxyBdNOabFY94wFTU9+U5y3pMssDsTbgbn1GV8egF3kLyr Y1YA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="a/xGqStV"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q4si8457240pgj.233.2022.01.21.11.20.56; Fri, 21 Jan 2022 11:21:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="a/xGqStV"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1355547AbiASO6i (ORCPT + 99 others); Wed, 19 Jan 2022 09:58:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355402AbiASO6X (ORCPT ); Wed, 19 Jan 2022 09:58:23 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 779CBC06175A; Wed, 19 Jan 2022 06:58:07 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 140DD61480; Wed, 19 Jan 2022 14:58:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7DA47C004E1; Wed, 19 Jan 2022 14:58:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1642604286; bh=AGdswND/skwO7CuRhjnhUztIarwXZ50Sy0S/WnqRJXo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a/xGqStVrRjwbChx7k7KjoMlZ5585T4m3hETH31hebk6CMwbmTilYZPWcMTazweVz uFJMiR6cD5+TorSOIjn57YiIu4uaxefKelGGgSGX0jOzOOw4J1E/ejHue3GU5PCaZi IZ28fyZndiQoOuqLIQjuIx+3q7rYMdkmWtzAu7uR8QknQNvNNfhov+7g46zZRw9SRD n7vKE3nWUypic+YMF7fdL0xpn0O59vUKLXFc5rqwZK6jvZ+9QstvWE7iUoi1lrn1pP e9/njftMYUiVcOnf8M1HOxdoTdwOqFBRaGLvMyonn5WVXSjafbsPDl0ejMioWuvun4 5VobgVkjE7cpA== From: Masami Hiramatsu To: Jiri Olsa , Alexei Starovoitov Cc: Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu , netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , "Naveen N . Rao" , Anil S Keshavamurthy , "David S . Miller" Subject: [RFC PATCH v3 8/9] [DO NOT MERGE] Out-of-tree: Support wildcard symbol option to sample Date: Wed, 19 Jan 2022 23:58:01 +0900 Message-Id: <164260428108.657731.16447173618084051958.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <164260419349.657731.13913104835063027148.stgit@devnote2> References: <164260419349.657731.13913104835063027148.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is not intended to be merged to upstream code (since this expose some kernel internal functions just for an example.) But this is good to show how the fprobe is time-efficient for registering a probe on thousands of functions. # time insmod fprobe_example.ko symbol='btrfs_*' [ 36.130947] fprobe_init: 1028 symbols found [ 36.177901] fprobe_init: Planted fprobe at btrfs_* real 0m 0.08s user 0m 0.00s sys 0m 0.07s Signed-off-by: Masami Hiramatsu --- kernel/kallsyms.c | 1 + kernel/trace/ftrace.c | 1 + samples/fprobe/fprobe_example.c | 69 ++++++++++++++++++++++++++++++++++----- 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 3011bc33a5ba..d0c4073acbfd 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -246,6 +246,7 @@ int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, } return 0; } +EXPORT_SYMBOL_GPL(kallsyms_on_each_symbol); #endif /* CONFIG_LIVEPATCH */ static unsigned long get_symbol_pos(unsigned long addr, diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 39350aa38649..7ce604bc9529 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -1580,6 +1580,7 @@ unsigned long ftrace_location(unsigned long ip) { return ftrace_location_range(ip, ip); } +EXPORT_SYMBOL_GPL(ftrace_location); /** * ftrace_text_reserved - return true if range contains an ftrace location diff --git a/samples/fprobe/fprobe_example.c b/samples/fprobe/fprobe_example.c index c28320537f98..df034e00661e 100644 --- a/samples/fprobe/fprobe_example.c +++ b/samples/fprobe/fprobe_example.c @@ -12,6 +12,7 @@ #define pr_fmt(fmt) "%s: " fmt, __func__ +#include #include #include #include @@ -37,16 +38,51 @@ static void sample_exit_handler(struct fprobe *fp, unsigned long ip, struct pt_r static char *symbuf; +struct sym_search_param { + unsigned long *addrs; + const char *pat; + int cnt; +}; + +#define MAX_FPROBE_ENTS (16 * 1024) + +static int wildcard_match(void *data, const char *symbol, struct module *mod, + unsigned long addr) +{ + struct sym_search_param *param = (struct sym_search_param *)data; + + if (glob_match(param->pat, symbol)) { + if (!ftrace_location(addr)) + return 0; + + if (param->addrs) + param->addrs[param->cnt] = addr; + param->cnt++; + if (param->cnt >= MAX_FPROBE_ENTS) + return -E2BIG; + } + return 0; +} + static int __init fprobe_init(void) { - const char **syms; + struct sym_search_param param = {.pat = symbol, .addrs = NULL, .cnt = 0}; + unsigned long *addrs = NULL; + const char **syms = NULL; char *p; int ret, count, i; + bool wildcard = false; sample_probe.entry_handler = sample_entry_handler; sample_probe.exit_handler = sample_exit_handler; - if (strchr(symbol, ',')) { + if (strchr(symbol, '*')) { + kallsyms_on_each_symbol(wildcard_match, ¶m); + count = param.cnt; + if (!count) + return -ENOENT; + wildcard = true; + } else if (strchr(symbol, ',')) { symbuf = kstrdup(symbol, GFP_KERNEL); if (!symbuf) return -ENOMEM; @@ -58,19 +94,31 @@ static int __init fprobe_init(void) count = 1; symbuf = symbol; } - pr_info("%d symbols found\n", count); - syms = kcalloc(count, sizeof(char *), GFP_KERNEL); - if (!syms) { + if (wildcard) + addrs = kcalloc(count, sizeof(unsigned long), GFP_KERNEL); + else + syms = kcalloc(count, sizeof(char *), GFP_KERNEL); + if (!syms && !addrs) { ret = -ENOMEM; goto error; } - p = symbuf; - for (i = 0; i < count; i++) - syms[i] = strsep(&p, ","); + if (wildcard) { + param.addrs = addrs; + param.cnt = 0; + + kallsyms_on_each_symbol(wildcard_match, ¶m); + count = param.cnt; + sample_probe.addrs = addrs; + } else { + p = symbuf; + for (i = 0; i < count; i++) + syms[i] = strsep(&p, ","); + sample_probe.syms = syms; + } + pr_info("%d symbols found\n", count); - sample_probe.syms = syms; sample_probe.nentry = count; ret = register_fprobe(&sample_probe); @@ -82,6 +130,8 @@ static int __init fprobe_init(void) return 0; error: + kfree(addrs); + kfree(syms); if (symbuf != symbol) kfree(symbuf); return ret; @@ -92,6 +142,7 @@ static void __exit fprobe_exit(void) unregister_fprobe(&sample_probe); kfree(sample_probe.syms); + kfree(sample_probe.addrs); if (symbuf != symbol) kfree(symbuf);