From patchwork Wed Aug 16 16:35:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Francis Laniel X-Patchwork-Id: 136178 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp2113106vqi; Sat, 19 Aug 2023 09:54:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFsTsxQDWViW6XuyGCYFSOipjgZzNwk312JzK1HffmIRhrWwLx2LvbwjB+Osy2REfnnLMEO X-Received: by 2002:a05:6358:e48e:b0:13a:d865:9c8f with SMTP id by14-20020a056358e48e00b0013ad8659c8fmr1165714rwb.15.1692464040598; Sat, 19 Aug 2023 09:54:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692464040; cv=none; d=google.com; s=arc-20160816; b=rY5JFtWAfDaiBXpmDu1akYSEhuuTtbDyPWliexnHWKs9usov+B/q/1O+fcMlTOogAq /JOXfZ7lb1RZDRtwlAIWF0k3i/48GZO8gwUVJQn5kqpqqrzVpj/hfr9loiAiI5dWsl7k CnBAyi7McntZTlRo/AK2412SUd1TYkjSoXgpaKy2K6UIYQ6zgfT9U+BAVny7BIgtcVgn rYMSNPC3nsyjgJLAth+Kpo7KKBFhbTQTd7SzpSpt9RKuMJlckMZAIjQ+Y6nfGg+rmAxA oICE0XB7UbtcmNVoD//I8yxTXVfJABNMN70CB9KzrrOpAsQTEOz5mq/pEIYanic/xbcF 76fQ== 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=ElFNTE62wAoAri3uxF0HbO1j0BAyiEyFTr89HUlnOwE=; fh=S5i3AjVkOjBGxmY/R1pfznIuGzIi0SgCChJjxP0m/Vw=; b=wmUdKqFyIJXxPFN0lfaXorJg/dnZZCYu1BHUeXGTevHkHpiNTI3G7an5FLlTRLM0iL dcBhoFAPNoilkBuyFnaHiuh1DNDoMCrgS7ZOF5xwgobuZMPctktblUSxALs70OxwiSrR E7Vf9IVF9GjAQG/J99PzLbTrGOi6O9Y1NNW7irKeFEKr5PabbnGBws3wFfHFdn1P/5mm N3upeIJR58jGEMkkZvcK1XAvpDqppxQ4zvud+pjPv0iApSmHYDh9PpU6o3iMxYSGGVxB BKdoqQj7e5iYytHKaCcQQFDQcWXtv59CYwbobDArjs1OH4YbbQe6P+wdjEinLYS/t7Gq 2j+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=HqSc3sFV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id n7-20020a056a00212700b0068711b0164csi3552945pfj.393.2023.08.19.09.54.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Aug 2023 09:54:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=HqSc3sFV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8E2BA11FEF1; Sat, 19 Aug 2023 01:53:10 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344784AbjHPQgA (ORCPT + 99 others); Wed, 16 Aug 2023 12:36:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344752AbjHPQfn (ORCPT ); Wed, 16 Aug 2023 12:35:43 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DAF45199B; Wed, 16 Aug 2023 09:35:41 -0700 (PDT) Received: from pwmachine.numericable.fr (85-170-34-233.rev.numericable.fr [85.170.34.233]) by linux.microsoft.com (Postfix) with ESMTPSA id 4632F211F614; Wed, 16 Aug 2023 09:35:40 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 4632F211F614 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1692203741; bh=ElFNTE62wAoAri3uxF0HbO1j0BAyiEyFTr89HUlnOwE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HqSc3sFVgYM/NnhDRNIVNvOC4OVMb4rZPiLIty2CamJCEjsNQ2viGjE9TaTl86ppz OtJJZxg9FJVnkdbrH8cIAHG93RNUCZm4r0DHafT4l3oCuhMP7/wkWbY0owpKv5QUZY TjtIALQscvAt7Wswq03bv0FlY4kcFGzu3jfHecN4= From: Francis Laniel To: linux-kernel@vger.kernel.org Cc: Francis Laniel , Steven Rostedt , Masami Hiramatsu , linux-trace-kernel@vger.kernel.org Subject: [RFC PATCH v1 1/1] tracing/kprobe: Add multi-probe support for 'perf_kprobe' PMU Date: Wed, 16 Aug 2023 18:35:17 +0200 Message-Id: <20230816163517.112518-2-flaniel@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230816163517.112518-1-flaniel@linux.microsoft.com> References: <20230816163517.112518-1-flaniel@linux.microsoft.com> MIME-Version: 1.0 X-Spam-Status: No, score=-19.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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: INBOX X-GMAIL-THRID: 1774482262998632864 X-GMAIL-MSGID: 1774677173882473612 When using sysfs, it is possible to create kprobe for several kernel functions sharing the same name, but of course with different addresses, by writing their addresses in kprobe_events file. When using PMU, if only the symbol name is given, the event will be created for the first address which matches the symbol, as returned by kallsyms_lookup_name(). The idea here is to search all kernel functions which match this symbol and create a trace_kprobe for each of them. All these trace_kprobes are linked together by sharing the same trace_probe. Signed-off-by: Francis Laniel --- kernel/trace/trace_kprobe.c | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 1b3fa7b854aa..08580f1466c7 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -1682,13 +1682,42 @@ static int unregister_kprobe_event(struct trace_kprobe *tk) } #ifdef CONFIG_PERF_EVENTS + +struct address_array { + unsigned long *addrs; + size_t size; +}; + +static int add_addr(void *data, unsigned long addr) +{ + struct address_array *array = data; + unsigned long *p; + + array->size++; + p = krealloc(array->addrs, + sizeof(*array->addrs) * array->size, + GFP_KERNEL); + if (!p) { + kfree(array->addrs); + return -ENOMEM; + } + + array->addrs = p; + array->addrs[array->size - 1] = addr; + + return 0; +} + /* 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, bool is_return) { enum probe_print_type ptype; + struct address_array array; struct trace_kprobe *tk; + unsigned long func_addr; + unsigned int i; int ret; char *event; @@ -1722,7 +1751,64 @@ create_local_trace_kprobe(char *func, void *addr, unsigned long offs, if (ret < 0) goto error; + array.addrs = NULL; + array.size = 0; + ret = kallsyms_on_each_match_symbol(add_addr, func, &array); + if (ret) + goto error_free; + + if (array.size == 1) + goto end; + + /* + * Below loop allocates a trace_kprobe for each function with the same + * name in kernel source code. + * All this differente trace_kprobes will be linked together through + * append_trace_kprobe(). + * NOTE append_trace_kprobe() is called in register_trace_kprobe() which + * is called when a kprobe is added through sysfs. + */ + func_addr = kallsyms_lookup_name(func); + for (i = 0; i < array.size; i++) { + struct trace_kprobe *tk_same_name; + unsigned long address; + + address = array.addrs[i]; + /* Skip the function address as we already registered it. */ + if (address == func_addr) + continue; + + /* + * alloc_trace_kprobe() first considers symbol name, so we set + * this to NULL to allocate this kprobe on the given address. + */ + tk_same_name = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, + (void *)address, NULL, offs, + 0 /* maxactive */, + 0 /* nargs */, is_return); + + if (IS_ERR(tk_same_name)) { + ret = -ENOMEM; + goto error_free; + } + + init_trace_event_call(tk_same_name); + + if (traceprobe_set_print_fmt(&tk_same_name->tp, ptype) < 0) { + ret = -ENOMEM; + goto error_free; + } + + ret = append_trace_kprobe(tk_same_name, tk); + if (ret) + goto error_free; + } + +end: + kfree(array.addrs); return trace_probe_event_call(&tk->tp); +error_free: + kfree(array.addrs); error: free_trace_kprobe(tk); return ERR_PTR(ret);