Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp2115128rwl; Fri, 6 Jan 2023 02:01:54 -0800 (PST) X-Google-Smtp-Source: AMrXdXsN2R2KU4E3+MNBxMMKptwLmAyVUjMQ3YVBgmpcS7dqw+CgLxx6n3RDGPsow3nWHn2UTBbc X-Received: by 2002:a17:90a:62cc:b0:223:7910:95f8 with SMTP id k12-20020a17090a62cc00b00223791095f8mr66443598pjs.36.1672999313757; Fri, 06 Jan 2023 02:01:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672999313; cv=none; d=google.com; s=arc-20160816; b=IkNB6TVQzFQ8uxbBGa0LntlD6LxKEQ+qg0Vq43NV7Vd3KzP/Zdxb9fLKVVkFzr8vWO M79xepUWtwGJqp8z9uSJ8OscBm6RaLeQ6imV5mpPkWQprcTyq8xNO0v/GjKev7lQZRQ1 k4uxCYV9pqelBDFx6ifStTs6nZt+DupWt2AoSiFxLi4IoblWybbxWOZn5e74eGwxHkjG AgOAelJqJbkqfCWm4El/7tBzRrA32TUE4L7Z99r8BgXNZ/6ldmsoeKrYJIH6QiKeDYhc DlZQDCZ9qcU/WSjHKDVCzhd+SXsy5u/jb+qBsmAUgj6rc02rJnDA8rR3OjYKs9mblcFq u6wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:date:from:dkim-signature; bh=PN8//UfAoVwKaLm31eaWicz8hNpiTUFBXEnuGYY6upA=; b=ataG5apVKAtkD5gchPCkflhcECnaEESYu52crLJgzyksd0mEQwGxunXlP42Bn2BUDg /lp2d+9GmGYZgPQGfS7OiN6rhn4LL7T2uYAo31O3bSYhSF41a+Fzn4uokm8GIGxcRTbv AOZ1k0u+z0VDbGEVE0QYEPARzVVHQ3FYsnxAwDmE8dByZotxAtP/SkpbjbdDoIWBaHxU R+46pb0dzKXncKEexE2KQeuTc7Dmb1QvM4tlWzxvf1f1TG2HncTlre5v/pXj7K1C/4YG PlOXIxIgjArooTG55VEmBwOpGW6Ueo2+6NtXwZOjjxBUf3vjoA6BiYVlgUmzvG3C51HR kcTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=FlwDESQy; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o17-20020a639211000000b004a3492595dcsi1079234pgd.248.2023.01.06.02.01.46; Fri, 06 Jan 2023 02:01:53 -0800 (PST) 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=@gmail.com header.s=20210112 header.b=FlwDESQy; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233270AbjAFJrR (ORCPT + 54 others); Fri, 6 Jan 2023 04:47:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233742AbjAFJqT (ORCPT ); Fri, 6 Jan 2023 04:46:19 -0500 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 698D869B3B; Fri, 6 Jan 2023 01:45:45 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id c65-20020a1c3544000000b003cfffd00fc0so3167204wma.1; Fri, 06 Jan 2023 01:45:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:from:to:cc:subject:date:message-id:reply-to; bh=PN8//UfAoVwKaLm31eaWicz8hNpiTUFBXEnuGYY6upA=; b=FlwDESQyeNbwEOF9phOaSdZuMcQArQruAlYMTIIZ6+wSGyki259+Z+Sbg2rCI+BVQt sn9X3mES4FYL0nwejFeAt+UGU5KL6LpEanaT9TjhL8UDHNtgsndEgp/y8K1EIX3vxA3n Z4RlkGCMmQa9J5f+NxM8mSeSlbHjY6+DRna3iodggUIwdycTIXd5f1/VV/VOMKpN4dh3 4HAHYShdm7RrVrqqnLISKJ9Mp+f0ysYmVu0Q5RJ39oG+0QOQMCFeTmNbH5ljcGBn7WTx nOER6ELX9ikWuNZjskCtbq5SEpXtds8xMJ05K3DiU/V5OZy7YAob2+UaBkXAWzBuQVux bA2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=PN8//UfAoVwKaLm31eaWicz8hNpiTUFBXEnuGYY6upA=; b=QRL2oqrjWWXNKZxBt/9+IJ0LvXHs4UYKnoysqqykq+rxLGfbVQGzr7BPZ6NYSWWE4u VvAnLgyrRDrHOlS4derW8ACDjB+1wIAXKM+GgeJWwSLmTpAIzGQNmy1o76R8gN37MW8Q pBgaQkH1qC5NQ6bSoEFuA8HDusjq5tAq8tt/ntqUzIGETfT6c0N4k/HQufEiRjSmYHq6 u1tNyHV08dJm67xxCcpBmWno7j2Pih1lRqo4u8DmK8XMbPm/N6PindTP2XmZs0y8KJ91 eDa5tSidOIwNXr/ZVSUiFzTeaAtFyGMSW6IIqjDe/t/n4xHcsU1Trl0LGhx6SLMT5nED psdQ== X-Gm-Message-State: AFqh2krhoYSkvJPFBC7fQkrVWEKWZQ0THZ3q1l0K7quawcv+Jbp2He6u QgAAS8p6mqZr0IgscFfrMw0= X-Received: by 2002:a05:600c:3ca2:b0:3d9:a5a2:65fa with SMTP id bg34-20020a05600c3ca200b003d9a5a265famr17725332wmb.7.1672998343858; Fri, 06 Jan 2023 01:45:43 -0800 (PST) Received: from krava (2001-1ae9-1c2-4c00-726e-c10f-8833-ff22.ip6.tmcz.cz. [2001:1ae9:1c2:4c00:726e:c10f:8833:ff22]) by smtp.gmail.com with ESMTPSA id bg24-20020a05600c3c9800b003cfa3a12660sm13739987wmb.1.2023.01.06.01.45.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Jan 2023 01:45:43 -0800 (PST) From: Jiri Olsa X-Google-Original-From: Jiri Olsa Date: Fri, 6 Jan 2023 10:45:40 +0100 To: Jiri Olsa Cc: Zhen Lei , Petr Mladek , Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Joe Lawrence , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Steven Rostedt , Masami Hiramatsu , Mark Rutland , bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Luis Chamberlain , linux-modules@vger.kernel.org Subject: Re: [PATCH 2/3] bpf: Optimize get_modules_for_addrs() Message-ID: References: <20221230112729.351-1-thunder.leizhen@huawei.com> <20221230112729.351-3-thunder.leizhen@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 On Thu, Jan 05, 2023 at 10:31:12PM +0100, Jiri Olsa wrote: > On Wed, Jan 04, 2023 at 05:25:08PM +0100, Petr Mladek wrote: > > On Fri 2022-12-30 19:27:28, Zhen Lei wrote: > > > Function __module_address() can quickly return the pointer of the module > > > to which an address belongs. We do not need to traverse the symbols of all > > > modules to check whether each address in addrs[] is the start address of > > > the corresponding symbol, because register_fprobe_ips() will do this check > > > later. > > hum, for some reason I can see only replies to this patch and > not the actual patch.. I'll dig it out of the lore I guess > > > > > > > Assuming that there are m modules, each module has n symbols on average, > > > and the number of addresses 'addrs_cnt' is abbreviated as K. Then the time > > > complexity of the original method is O(K * log(K)) + O(m * n * log(K)), > > > and the time complexity of current method is O(K * (log(m) + M)), M <= m. > > > (m * n * log(K)) / (K * m) ==> n / log2(K). Even if n is 10 and K is 128, > > > the ratio is still greater than 1. Therefore, the new method will > > > generally have better performance. > > could you try to benchmark that? I tried something similar but was not > able to get better performance hm looks like I tried the smilar thing (below) like you did, but wasn't able to get better performace I guess your goal is to get rid of the module arg in module_kallsyms_on_each_symbol callback that we use? I'm ok with the change if the performace is not worse jirka --- diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 5b9008bc597b..3280c22009f1 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2692,23 +2692,16 @@ struct module_addr_args { int mods_cap; }; -static int module_callback(void *data, const char *name, - struct module *mod, unsigned long addr) +static int add_module(struct module_addr_args *args, struct module *mod) { - struct module_addr_args *args = data; struct module **mods; - /* We iterate all modules symbols and for each we: - * - search for it in provided addresses array - * - if found we check if we already have the module pointer stored - * (we iterate modules sequentially, so we can check just the last - * module pointer) + /* We iterate sorted addresses and for each within module we: + * - check if we already have the module pointer stored for it + * (we iterate sorted addresses sequentially, so we can check + * just the last module pointer) * - take module reference and store it */ - if (!bsearch(&addr, args->addrs, args->addrs_cnt, sizeof(addr), - bpf_kprobe_multi_addrs_cmp)) - return 0; - if (args->mods && args->mods[args->mods_cnt - 1] == mod) return 0; @@ -2734,10 +2727,24 @@ static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u3 .addrs = addrs, .addrs_cnt = addrs_cnt, }; - int err; + u32 i, err = 0; + + for (i = 0; !err && i < addrs_cnt; i++) { + struct module *mod; + bool found = false; + + preempt_disable(); + mod = __module_text_address(addrs[i]); + found = mod && try_module_get(mod); + preempt_enable(); + + if (found) { + err = add_module(&args, mod); + module_put(mod); + } + } /* We return either err < 0 in case of error, ... */ - err = module_kallsyms_on_each_symbol(module_callback, &args); if (err) { kprobe_multi_put_modules(args.mods, args.mods_cnt); kfree(args.mods); @@ -2862,7 +2869,8 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr } else { /* * We need to sort addrs array even if there are no cookies - * provided, to allow bsearch in get_modules_for_addrs. + * provided, to allow sequential address walk in + * get_modules_for_addrs. */ sort(addrs, cnt, sizeof(*addrs), bpf_kprobe_multi_addrs_cmp, NULL);