Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6200740iob; Tue, 10 May 2022 12:41:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyzLdTfOTmcSnXMl2BkLp6GFBKeDDSMds+igO1guiJc8saxMDI2RxxH43u6RWnNvCWzj1sq X-Received: by 2002:a17:902:9a83:b0:15e:f718:4ced with SMTP id w3-20020a1709029a8300b0015ef7184cedmr15081891plp.69.1652211665641; Tue, 10 May 2022 12:41:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652211665; cv=none; d=google.com; s=arc-20160816; b=fPc9tDzZRdf97qL9/cnHaH1zgLvOg8aQ/mc+kMyIwx1kHiezvUGABbItfi4KUNf5V/ /6sCcpCyNApThlKWwii5YgMjiu+o0sz6+At0hYy7+cJSr3gnxX3NTUY5ZLhbYY4yHWsI AUTZ8y/HG2Si4DQTnQShV7E57xzp3GGOa8x/7RlCyENDTlSqrQH0EiA5a+3OasMTiU1b OtfrNANCQlIEUcFq3hTAECfyJV1p9HY+xqa303Y9ZuvwjoxzLGAT+IIJrWVKcQM/f48Y VhDp592P+4ShMtPSSB8ha2GAB6gyAuhe8MqVX1Tjy5rAvETuNZpIQtS7W/DrKgVXu2zX 0kHw== 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; bh=2o3LNd6t92+KsIQ3e2a4Pi0Ya1WmtsbPxoK8Kf1CQWc=; b=uF0CVnzgvsTI0gvM4ksS2z30TtTohEPtuD09gVoL7gUOwrUWjLLz/3sMRmd+3aOgUw gR1lQeHZS7RrsufE2GYmlZ7NEbm4XJPme6FLmrnf5ss9YuDrqs9RIsJou9aGsqpNe/SZ JvTsao+bi4Gu40fQm65fnmNdHHFCReLHsyY6VsDYKUud8Y2hnE9g7cSdsBuZ3cI6PNdO A90pqXHdJfhPP5iLSvP8DnxqgrauqV1IpTqFU16rYORtlF7+ZNabOfuU8G6a4X0YIp+E +Tz8ycshQVfdPyCwV3QWKuhR1INEiaTjsnv8VxnZ2theTZg15D+MJFPbY4ONKvK88eo7 gTLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HizxagiG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i19-20020a63d453000000b003c22781047esi266512pgj.86.2022.05.10.12.40.48; Tue, 10 May 2022 12:41:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HizxagiG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S241807AbiEJMbV (ORCPT + 99 others); Tue, 10 May 2022 08:31:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241908AbiEJMbL (ORCPT ); Tue, 10 May 2022 08:31:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC83B2A1FDE; Tue, 10 May 2022 05:27:13 -0700 (PDT) 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 499F460BCC; Tue, 10 May 2022 12:27:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6CC7CC385C2; Tue, 10 May 2022 12:27:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1652185632; bh=SeZYGVo+C/Ti73sLThBUppeds9nAxA0/otvgvdLHc5I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HizxagiGVF4VCmSTPu9ZyBCa8JnenDjBuqn44iirNuSEakoHnpf0fs+qbLNZtXZYK OJ9FC0GNdSvxg0fDiWvI8unNvti0owq3m3TvXqkfRfXGPjtwjHEOHa7cWWXgQR8laq hlfFxPoaUKtKSHFFDBJfKycbxXmV1hab2ATi/T9pbtLuOJgNFHOncaYhm0bNaOXFdi IrH0AZjFaDIxCHVdTTHCUFNx6etz2RPDBHne8D5r5WlX+aQvMV8mdg51Wrdh7pi6di IVbkLtY/cRb42vOkyJrPSE6tLTp5yeHjREPTBtqZe52/Fwis8fdZBkw3+0AUPo28vQ 9DNScoGC/n48g== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Christoph Hellwig Subject: [PATCHv6 bpf-next 4/5] bpf: Resolve symbols with ftrace_lookup_symbols for kprobe multi link Date: Tue, 10 May 2022 14:26:15 +0200 Message-Id: <20220510122616.2652285-5-jolsa@kernel.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220510122616.2652285-1-jolsa@kernel.org> References: <20220510122616.2652285-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Using kallsyms_lookup_names function to speed up symbols lookup in kprobe multi link attachment and replacing with it the current kprobe_multi_resolve_syms function. This speeds up bpftrace kprobe attachment: # perf stat -r 5 -e cycles ./src/bpftrace -e 'kprobe:x* { } i:ms:1 { exit(); }' ... 6.5681 +- 0.0225 seconds time elapsed ( +- 0.34% ) After: # perf stat -r 5 -e cycles ./src/bpftrace -e 'kprobe:x* { } i:ms:1 { exit(); }' ... 0.5661 +- 0.0275 seconds time elapsed ( +- 4.85% ) Acked-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- kernel/trace/bpf_trace.c | 112 +++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 46 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index f15b826f9899..7fd11c17558d 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2229,6 +2229,59 @@ struct bpf_kprobe_multi_run_ctx { unsigned long entry_ip; }; +struct user_syms { + const char **syms; + char *buf; +}; + +static int copy_user_syms(struct user_syms *us, unsigned long __user *usyms, u32 cnt) +{ + unsigned long __user usymbol; + const char **syms = NULL; + char *buf = NULL, *p; + int err = -ENOMEM; + unsigned int i; + + syms = kvmalloc(cnt * sizeof(*syms), GFP_KERNEL); + if (!syms) + goto error; + + buf = kvmalloc(cnt * KSYM_NAME_LEN, GFP_KERNEL); + if (!buf) + goto error; + + for (p = buf, i = 0; i < cnt; i++) { + if (__get_user(usymbol, usyms + i)) { + err = -EFAULT; + goto error; + } + err = strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LEN); + if (err == KSYM_NAME_LEN) + err = -E2BIG; + if (err < 0) + goto error; + syms[i] = p; + p += err + 1; + } + + us->syms = syms; + us->buf = buf; + return 0; + +error: + if (err) { + kvfree(syms); + kvfree(buf); + } + return err; +} + +static void free_user_syms(struct user_syms *us) +{ + kvfree(us->syms); + kvfree(us->buf); +} + static void bpf_kprobe_multi_link_release(struct bpf_link *link) { struct bpf_kprobe_multi_link *kmulti_link; @@ -2349,53 +2402,12 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long entry_ip, kprobe_multi_link_prog_run(link, entry_ip, regs); } -static int -kprobe_multi_resolve_syms(const void __user *usyms, u32 cnt, - unsigned long *addrs) +static int symbols_cmp(const void *a, const void *b) { - unsigned long addr, size; - const char __user **syms; - int err = -ENOMEM; - unsigned int i; - char *func; - - size = cnt * sizeof(*syms); - syms = kvzalloc(size, GFP_KERNEL); - if (!syms) - return -ENOMEM; + const char **str_a = (const char **) a; + const char **str_b = (const char **) b; - func = kmalloc(KSYM_NAME_LEN, GFP_KERNEL); - if (!func) - goto error; - - if (copy_from_user(syms, usyms, size)) { - err = -EFAULT; - goto error; - } - - for (i = 0; i < cnt; i++) { - err = strncpy_from_user(func, syms[i], KSYM_NAME_LEN); - if (err == KSYM_NAME_LEN) - err = -E2BIG; - if (err < 0) - goto error; - err = -EINVAL; - addr = kallsyms_lookup_name(func); - if (!addr) - goto error; - if (!kallsyms_lookup_size_offset(addr, &size, NULL)) - goto error; - addr = ftrace_location_range(addr, addr + size - 1); - if (!addr) - goto error; - addrs[i] = addr; - } - - err = 0; -error: - kvfree(syms); - kfree(func); - return err; + return strcmp(*str_a, *str_b); } int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) @@ -2441,7 +2453,15 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr goto error; } } else { - err = kprobe_multi_resolve_syms(usyms, cnt, addrs); + struct user_syms us; + + err = copy_user_syms(&us, usyms, cnt); + if (err) + goto error; + + sort(us.syms, cnt, sizeof(*us.syms), symbols_cmp, NULL); + err = ftrace_lookup_symbols(us.syms, cnt, addrs); + free_user_syms(&us); if (err) goto error; } -- 2.35.3