From patchwork Sat Aug 12 05:57:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rong Tao X-Patchwork-Id: 134826 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp1548736vqi; Fri, 11 Aug 2023 23:21:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGwyL2g8DBUZ3fI3N0EvIpQCZJwWxjrpYOSGb+geXg2HVla2nbf/nle0kno7nnOb6DccyOW X-Received: by 2002:a05:6a00:a0c:b0:64c:4f2f:a235 with SMTP id p12-20020a056a000a0c00b0064c4f2fa235mr4981574pfh.30.1691821294765; Fri, 11 Aug 2023 23:21:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691821294; cv=none; d=google.com; s=arc-20160816; b=x+FN3L/CSrkqGWlsOAEASrTnZkBOuMNsJOflOL7Amf20z0nQJW/FcKhCLojcqTrJOP o13zchT70SIQL2D/OZEVMvD5PLrzHDJvEHITzv+MZOndzAJl5imgYwX4RjZfSSqPpTJP PO2gmPYtxcLKAw/YLTKAQVGhz0c8CaFpxCBLWjjnQd1H1m91W4E7EnUZkAy2KAFKxD3D kyN8Tvv/HthAUVGI5AwigCfap/MbGxp5kqLL+7dQvT9ZmeH5OZE0AdYw73hkmHXCsAt8 qoS8HjLdub8orSD7sXjSwHd3lqtXwbH6IZby55ohOw1OJmCQTQGVmFr3ikse94dfbnro ojyQ== 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:date:subject:cc:to:from:message-id :dkim-signature; bh=6k+2tlcHY6MZU1kfTY6UiFMgENXZRojososrfddZu4I=; fh=rNOdB9FsYK0Ji/UdYrXMmO/FHybMxe5lZPF9JIw2AXA=; b=ehkA/2aSHCzv2y6V48sgQ30cBbzK8pcj2VccwX/6tmHwFPvGf3ljEj8A8ABP1Y/iKI xQG2QcKPKPoxIghCpvrtDmqOYQ0MWw/ecsO001dsB65E/cOk/qGmSeb70CgmDQUzIZ5M BSrL9u0FgIb80JfPGDRM7JobUByLls69MeqCBBexUaiNZ0niACYPWXkVU4HtPCwqNbL1 2KicvARDX6ALidA6P/5qPW8GaedGI6UVniMi6yFE+0VuxqJ0FUCyLOnm3SfSINPdU6er AI1AtbGS9dAZqyUwch+7mtwe1uTb7qetphWCxyT2psrAjucZmmz/LS4CIRvG+WZiad1G 32Bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foxmail.com header.s=s201512 header.b=LPQVLmrs; 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=foxmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y64-20020a638a43000000b00563b0cbe820si4483369pgd.688.2023.08.11.23.21.21; Fri, 11 Aug 2023 23:21:34 -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=@foxmail.com header.s=s201512 header.b=LPQVLmrs; 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=foxmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235976AbjHLF6p (ORCPT + 99 others); Sat, 12 Aug 2023 01:58:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235619AbjHLF6n (ORCPT ); Sat, 12 Aug 2023 01:58:43 -0400 Received: from out203-205-221-233.mail.qq.com (out203-205-221-233.mail.qq.com [203.205.221.233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 602D518F; Fri, 11 Aug 2023 22:58:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1691819919; bh=6k+2tlcHY6MZU1kfTY6UiFMgENXZRojososrfddZu4I=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=LPQVLmrsKYLI9e9HiVnPxJiYj+MVC5PGhSXNXy1gz1VDHtt4myPeM0IAwqVCp/1x0 2yMRQkcsZleN6VpM5sXaT7koNtlFJo/xIpSH5Impt5h8uDI+0/IT2NBcuDBm3n+3/u Gf5enaMUJyvt3MJTzKckMqrW2wPTtsKpmw1necg4= Received: from localhost.localdomain ([183.197.149.136]) by newxmesmtplogicsvrszc2-0.qq.com (NewEsmtp) with SMTP id E97B6647; Sat, 12 Aug 2023 13:58:23 +0800 X-QQ-mid: xmsmtpt1691819907tjhossl2a Message-ID: X-QQ-XMAILINFO: MpO6L0LObisWlSPyv0ox+ZvMrl6iV1FUX3NaeEq6C+iNu2Xfz/4TfRjebT0iwT jRO2L4CZ8senOrM1WgysUkFrCY9MDXEcFoEdjONCfIqk8yzynKkc8DvKvb2/BXLLIWZwA9/KUZXX h7Sw1SckGjvWMulxOgVVEQtTBIwfTpDF80SV+YfkZCaBN0cvEJW8ABUL/FZdJ39pAQ18z8hr8IzV mel8lr/rjN7bS1LFBGdiCpMgcBEGqaoCa1pyPBr7rQWJPvPj43IvehKPTsNfEX9fKJ49FPtfQ/qe IQa8LRQY2Xz0wbdnxH0KVe15p1sWFbLgz5IJwkadS7c947YelRQ9P/x4uSvF62Jkc3mhZTh5ksch XV0kelJvfMZHClTO8bwzFIEYK7U/H4VArDCN5U5oP4lo3hseBlwnt1BOouNaImdDsDCdD2ecC075 6gZ86VxIYxBmpEP7zK7j0fBXRiWbTAU5R20g9578yxK+iKRm7wVAU13e/Pa3lemy9+H4lTQxc5id 3mW4VlPXq3fhnrr03fIzNPU59zrVn8mVcqoFeD9+ijAjIs6GrgnZHj8vjRCtziRK+ZVGQ7sBD9Pb YXLBVS9eGHWdEPAo1gNESRjErU9tUSvYN2SG90NFdNLaiEupzEP5NxiUDv1zf8l7+70m9qvJdmQA 7Dk8potQQIftKVhxBRS/Bz/9KXilmvzDwz/4JVM8OdZvUQEIiYFezP2lTNMaG5LNjQEHU/uwrYVN pNTQCNlRLcfVqReM7i5Ou9yCuAkD5IvShNlaqHrgB4cdwq51kpiHECSt+ddInqdxVkDOFZu5vtlC hR8vR7y83s7aF2kAO2J2x+YiHqglNddCnYlCWH+Vn99f9SVhqiIW6DpSJLSuKUkBK2h4WmGeOi7B lfkINmi6Ep3mvGlUuysnRvuaQ5oWppxA7a6KKoTUrLHRGNy88GWI1IbfdLT9TojIDwp3LvbiWHW6 FmTn3FRfjQJ3MJNC/0asszYu6BfmaJJYLVLsNbj9+bcI84M7piKRtG68OBwJQ0GeWva9KtyfjXoQ rtAxIumjDt1vZVJbleo+iU2j9hgl8auCm/suww8ejBNg0fCeOy X-QQ-XMRINFO: OD9hHCdaPRBwq3WW+NvGbIU= From: Rong Tao To: sdf@google.com, ast@kernel.org Cc: rongtao@cestc.cn, rtoax@foxmail.com, Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org (open list:BPF [GENERAL] (Safe Dynamic Programs and Tools)), linux-kselftest@vger.kernel.org (open list:KERNEL SELFTEST FRAMEWORK), linux-kernel@vger.kernel.org (open list) Subject: [PATCH bpf-next v3] selftests/bpf: trace_helpers.c: optimize kallsyms cache Date: Sat, 12 Aug 2023 13:57:03 +0800 X-OQ-MSGID: <20230812055703.7218-2-rtoax@foxmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230812055703.7218-1-rtoax@foxmail.com> References: <20230812055703.7218-1-rtoax@foxmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, HELO_DYNAMIC_IPADDR,RCVD_IN_DNSWL_BLOCKED,RDNS_DYNAMIC,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no 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: 1774003206245490070 X-GMAIL-MSGID: 1774003206245490070 From: Rong Tao Static ksyms often have problems because the number of symbols exceeds the MAX_SYMS limit. Like changing the MAX_SYMS from 300000 to 400000 in commit e76a014334a6("selftests/bpf: Bump and validate MAX_SYMS") solves the problem somewhat, but it's not the perfect way. This commit uses dynamic memory allocation, which completely solves the problem caused by the limitation of the number of kallsyms. Signed-off-by: Rong Tao --- v3: Do not use structs and judge ksyms__add_symbol function return value. v2: https://lore.kernel.org/lkml/tencent_B655EE5E5D463110D70CD2846AB3262EED09@qq.com/ Do the usual len/capacity scheme here to amortize the cost of realloc, and don't free symbols. v1: https://lore.kernel.org/lkml/tencent_AB461510B10CD484E0B2F62E3754165F2909@qq.com/ --- tools/testing/selftests/bpf/trace_helpers.c | 42 ++++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c index f83d9f65c65b..d8391a2122b4 100644 --- a/tools/testing/selftests/bpf/trace_helpers.c +++ b/tools/testing/selftests/bpf/trace_helpers.c @@ -18,10 +18,32 @@ #define TRACEFS_PIPE "/sys/kernel/tracing/trace_pipe" #define DEBUGFS_PIPE "/sys/kernel/debug/tracing/trace_pipe" -#define MAX_SYMS 400000 -static struct ksym syms[MAX_SYMS]; +static struct ksym *syms; +static int sym_cap; static int sym_cnt; +static int ksyms__add_symbol(const char *name, unsigned long addr) +{ + void *tmp; + unsigned int new_cap; + + if (sym_cnt + 1 > sym_cap) { + new_cap = sym_cap * 4 / 3; + tmp = realloc(syms, sizeof(struct ksym) * new_cap); + if (!tmp) + return -ENOMEM; + syms = tmp; + sym_cap = new_cap; + } + + syms[sym_cnt].addr = addr; + syms[sym_cnt].name = strdup(name); + + sym_cnt++; + + return 0; +} + static int ksym_cmp(const void *p1, const void *p2) { return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr; @@ -33,9 +55,13 @@ int load_kallsyms_refresh(void) char func[256], buf[256]; char symbol; void *addr; - int i = 0; + int ret; + sym_cap = 1024; sym_cnt = 0; + syms = malloc(sizeof(struct ksym) * sym_cap); + if (!syms) + return -ENOMEM; f = fopen("/proc/kallsyms", "r"); if (!f) @@ -46,15 +72,11 @@ int load_kallsyms_refresh(void) break; if (!addr) continue; - if (i >= MAX_SYMS) - return -EFBIG; - - syms[i].addr = (long) addr; - syms[i].name = strdup(func); - i++; + ret = ksyms__add_symbol(func, (unsigned long)addr); + if (ret) + return ret; } fclose(f); - sym_cnt = i; qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp); return 0; }