From patchwork Thu Aug 10 23:54:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rong Tao X-Patchwork-Id: 134265 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp787838vqi; Thu, 10 Aug 2023 18:16:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHNGEBhs5NPUCmfXBczYNLNFiWX/vKAixPaHKnppOctszqWGxDbAuFCACRIHowwJ06fLSNM X-Received: by 2002:a17:907:2c41:b0:992:bc8:58e4 with SMTP id hf1-20020a1709072c4100b009920bc858e4mr413608ejc.20.1691716589950; Thu, 10 Aug 2023 18:16:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691716589; cv=none; d=google.com; s=arc-20160816; b=Vnh7ITGcoMhJxYF2uUX/yjTzE5Jl4fQUIZ2rSMINVqdBjqXfAMvrlaZiUkcjSQQ7Sz JJAiHIfiMJoGMFSzq2NZ3zdnCXVJc5z+Dltr7b4f0Z2gu7G9gzBRpYf4OWZzCQ7fvRQ4 qUPTZvDynbdOt8SgM/VQosTkjRoFWURPOyFOFtOc2z3OUibA9YHizxvW2jEfABWdZKdR XaOd/6G6JSupM0QwjGhTCBgpH3n/NrlfFOo1N3OSCfeFLMY29gl2JQ4oI/2MUfIX0vV6 OXEJ7DXN980R3vzbu+xLorCdUFAnDDN2uFUpc3zQOdNfhmXgXG7iIOR+DI9rhPa6q4pj NfsQ== 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:date :subject:cc:to:from:message-id:dkim-signature; bh=AYyH4FOO/VIrSq+SDBQ9ZbyN9SODJ2goYLvhKL3Mw20=; fh=xTGhwt8oF/wfDMYPfzSfsf3cvLArsy7KvHC7NXC/J64=; b=0nUpUUAiO/xiZ1G6dges/zRgGu9Ul7jSyPmMun0SS6cLu0lRtyIGN4Khyrf+Q/DKwM qlA3i52g64mdctSEOWn0fkS+6DKlM3k7NtPgdOuAbHEIK3o1Pkf2P/XG7Ti9cNdqodey qih7qBfJ9G1Iz3qINOy8LySGuW/3JN2FClCliFrzV/I4oM2M1fHzJORK5bC6d1KdbDTW 2MtqC6OlxC4e7hgsKIKQj1d3z6lj1w591MSEVbnXFi5OQFZwJoM5q70YsWy7XzfA3JPt 8o7CNL4z4vbd2qjfA1u4TOeEQtXBkg0wk9OxOyiBihQsmr5j6qD86vl001VqrDemxY4V B/Bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foxmail.com header.s=s201512 header.b=Y0JkMt5X; 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 b8-20020a170906490800b0098e47bbff3csi2454458ejq.297.2023.08.10.18.16.06; Thu, 10 Aug 2023 18:16:29 -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=Y0JkMt5X; 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 S229580AbjHJXyb (ORCPT + 99 others); Thu, 10 Aug 2023 19:54:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232936AbjHJXya (ORCPT ); Thu, 10 Aug 2023 19:54:30 -0400 Received: from out203-205-251-84.mail.qq.com (unknown [203.205.251.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74AA22D48 for ; Thu, 10 Aug 2023 16:54:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1691711665; bh=AYyH4FOO/VIrSq+SDBQ9ZbyN9SODJ2goYLvhKL3Mw20=; h=From:To:Cc:Subject:Date; b=Y0JkMt5XXt8YejOA9RWp9vtfftDs8tw1ETTCTFL+FVej1Vy3pP9LtfujH6HPjji+T +MJMwX5iWsY8boOCW48EQRSUJ5eqXYvM76rRC0kv3dpwhOus+QjPPaSqanVsCUmpjC ZGapSqIs9vf4l5UfvVCMQ3P83pf2zodfCHK/TKDw= Received: from localhost.localdomain ([39.156.73.12]) by newxmesmtplogicsvrszb6-0.qq.com (NewEsmtp) with SMTP id D958B698; Fri, 11 Aug 2023 07:54:21 +0800 X-QQ-mid: xmsmtpt1691711661tkkupw841 Message-ID: X-QQ-XMAILINFO: MFdGPHhuqhNoD+8K/Ve4A6yyZK7J1hWB4CtBFqXpFB5TeNVpIgNKlQIuNp27xS /vdkpaHhuKwzgOwU+GB/1Kb6bqoxOm71kSkgB0wUrohJ8jvigoZ8KNsOUm+nKhyymsW0EkZDGEFS ZpXa7NZRtIT+1znj31GaAc67iWwkl+qD/G4qdKCC6RDhLAE1QwM90qTj2fhwWPQNc1bKFXREAHpc s3fNPwaoFj1pbo/y5/8479JeZMvxq+Q53kQNsJFyG/lQ7wbsMWJxUEB0xk+Kl5ueOe/1TrPdPr6f 93YnjDPFDcIs+SQCfVcpNOvw21iJqPhapDjUnflvv/2/S8naxja5eh3CvSzBriiNNQKh5EpeCcSx RdGK+ujYyC2Zn7N0LoSpZL6sKTdaBpcyL11VP7O9FXx6K+p+gF2/wDRkaI2FTXz8FRlbcbASvcWR eSaIl36y6pYXFLEnB9Vrf1PAQusXvuuTFWxK9rAMw5klbOSOiKBbmv5h3hcHjJlmvZRwPv0BKTGW c/kETO8muw6Y15VL3jakGdsRTwiMLG2HZBsguWE2Ik4EPkSFu9TxB5bepAdSH/9cLzqb69OtEPEa bB9Yk2ppNXpTCT+Kcs7PO3odn4IZb70GzW3yU+rFV1itZ0ZqRs+fYTaqzVOeKq8+gYwnl7xugOyK EPC1EdzxqzIsDmzEjI7rWHLeFy5AFtP01X69luy3yVAUxc1iq8Wzpy1wKSb6NDdfzSVBAitVPZ6x sz3hknQosZ4bot3dXZnzkTZWZMOvnCn1xrP9phvh5rBBt9pPSrTc4GASxjitXIM3eUpSpeeXsG0O qLopTvkl5zMgijMvt+oB/ogbvkXbb0V71SSB9RYtyEUCuB0QIwmHhEH78UKDuU0JKc+u5tOOFu6k CRLl9D22AeHkzGOP3OmA1gQP0t8mR7kSoJuV/jh6/dau5EbOZKOcm8SV+X+71Ubi+OJ1WT1YSqDl eL4RRnwBs+SjhR5o6adlj+y2RVEYCKMkm6W93yYQA= X-QQ-XMRINFO: MSVp+SPm3vtS1Vd6Y4Mggwc= From: Rong Tao To: ast@kernel.org Cc: rongtao@cestc.cn, Andrii Nakryiko , Mykola Lysenko , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , bpf@vger.kernel.org (open list:BPF [SELFTESTS] (Test Runners & Infrastructure)), linux-kselftest@vger.kernel.org (open list:KERNEL SELFTEST FRAMEWORK), linux-kernel@vger.kernel.org (open list) Subject: [PATCH bpf-next v2] selftests/bpf: trace_helpers.c: optimize kallsyms cache Date: Fri, 11 Aug 2023 07:54:19 +0800 X-OQ-MSGID: <20230810235420.297758-1-rtoax@foxmail.com> X-Mailer: git-send-email 2.39.3 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 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: 1773893414771595658 X-GMAIL-MSGID: 1773893414771595658 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 --- v2: 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 | 73 ++++++++++++++------- 1 file changed, 48 insertions(+), 25 deletions(-) diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c index f83d9f65c65b..cda5a2328450 100644 --- a/tools/testing/selftests/bpf/trace_helpers.c +++ b/tools/testing/selftests/bpf/trace_helpers.c @@ -18,9 +18,37 @@ #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 int sym_cnt; +static struct { + struct ksym *syms; + unsigned int sym_cap; + unsigned int sym_cnt; +} ksyms = { + .syms = NULL, + .sym_cap = 1024, + .sym_cnt = 0, +}; + +static int ksyms__add_symbol(const char *name, unsigned long addr) +{ + void *tmp; + unsigned int new_cap; + + if (ksyms.sym_cnt + 1 > ksyms.sym_cap) { + new_cap = ksyms.sym_cap * 4 / 3; + tmp = realloc(ksyms.syms, sizeof(struct ksym) * new_cap); + if (!tmp) + return -ENOMEM; + ksyms.syms = tmp; + ksyms.sym_cap = new_cap; + } + + ksyms.syms[ksyms.sym_cnt].addr = addr; + ksyms.syms[ksyms.sym_cnt].name = strdup(name); + + ksyms.sym_cnt++; + + return 0; +} static int ksym_cmp(const void *p1, const void *p2) { @@ -33,9 +61,10 @@ int load_kallsyms_refresh(void) char func[256], buf[256]; char symbol; void *addr; - int i = 0; - sym_cnt = 0; + ksyms.syms = malloc(sizeof(struct ksym) * ksyms.sym_cap); + if (!ksyms.syms) + return -ENOMEM; f = fopen("/proc/kallsyms", "r"); if (!f) @@ -46,16 +75,10 @@ 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++; + ksyms__add_symbol(func, (unsigned long)addr); } fclose(f); - sym_cnt = i; - qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp); + qsort(ksyms.syms, ksyms.sym_cnt, sizeof(struct ksym), ksym_cmp); return 0; } @@ -65,48 +88,48 @@ int load_kallsyms(void) * This is called/used from multiplace places, * load symbols just once. */ - if (sym_cnt) + if (ksyms.sym_cnt) return 0; return load_kallsyms_refresh(); } struct ksym *ksym_search(long key) { - int start = 0, end = sym_cnt; + int start = 0, end = ksyms.sym_cnt; int result; /* kallsyms not loaded. return NULL */ - if (sym_cnt <= 0) + if (ksyms.sym_cnt <= 0) return NULL; while (start < end) { size_t mid = start + (end - start) / 2; - result = key - syms[mid].addr; + result = key - ksyms.syms[mid].addr; if (result < 0) end = mid; else if (result > 0) start = mid + 1; else - return &syms[mid]; + return &ksyms.syms[mid]; } - if (start >= 1 && syms[start - 1].addr < key && - key < syms[start].addr) + if (start >= 1 && ksyms.syms[start - 1].addr < key && + key < ksyms.syms[start].addr) /* valid ksym */ - return &syms[start - 1]; + return &ksyms.syms[start - 1]; /* out of range. return _stext */ - return &syms[0]; + return &ksyms.syms[0]; } long ksym_get_addr(const char *name) { int i; - for (i = 0; i < sym_cnt; i++) { - if (strcmp(syms[i].name, name) == 0) - return syms[i].addr; + for (i = 0; i < ksyms.sym_cnt; i++) { + if (strcmp(ksyms.syms[i].name, name) == 0) + return ksyms.syms[i].addr; } return 0;