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: 13349988 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2711C001B0 for ; Fri, 11 Aug 2023 00:07:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229503AbjHKAHu (ORCPT ); Thu, 10 Aug 2023 20:07:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229448AbjHKAHu (ORCPT ); Thu, 10 Aug 2023 20:07:50 -0400 Received: from out203-205-221-202.mail.qq.com (out203-205-221-202.mail.qq.com [203.205.221.202]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92504FD; Thu, 10 Aug 2023 17:07:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1691712460; bh=AYyH4FOO/VIrSq+SDBQ9ZbyN9SODJ2goYLvhKL3Mw20=; h=From:To:Cc:Subject:Date; b=QbBIFmm+bV9cRztFMn14LXPJcl5nB+YfibkiBTh7gn5AXlLWQ4G32rvQTKKARoDFS VR+v/z1ig6DpPP0EKaEJLwoIKiIcmUJdxvs5oieA5klk5YkX9tr41eWBvBS/U8pyZK gMXjUfWIW+59siBv9hhdJqrmUkuC3PZPt8PCwue0= 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: MFdGPHhuqhN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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 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org 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;