From patchwork Thu Apr 19 01:34:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Yan X-Patchwork-Id: 133677 Delivered-To: patch@linaro.org Received: by 10.46.66.142 with SMTP id h14csp88109ljf; Wed, 18 Apr 2018 18:34:38 -0700 (PDT) X-Google-Smtp-Source: AIpwx482D8H9H8jnXa8Hhmy2qqi5LhP547sDzqxMwcJgPJwqDiaCMw4YjpIeLmdg/QMxPHgxXYoo X-Received: by 2002:a17:902:41:: with SMTP id 59-v6mr4182613pla.345.1524101678414; Wed, 18 Apr 2018 18:34:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524101678; cv=none; d=google.com; s=arc-20160816; b=ZvBS1dtEb1OLXwWCpnQDhUVLnzpTNLep0DtwuOQHfLjrm+EMsrX4N7kJh0yAcDyLJk VciZjZIy4w4ZFUeCRN7ExnCT7JYHyRCV6mqIQMINuziPObCjU8i8RoUSH93QS+3qttY0 ocbLU8zzsdopz2um+CxtEieX//c9nF09jPCd9o/8s79ZW9I4czet71WXwexOD0Wx9wUF AVjPt1JDKf5RG+Sl06XnjnajCWH+xHp8geOF2uuAJi1uTBXM+F3W8vbMPvAjRi7QIdX1 wuZLykwd2ON2faVYlfkdp9T2ilNkFnEduz9zUYIVq72eWn9utA/olVy4uyODDKhNsphy 8cNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=xyuB+Mlp2g4Wi5znxx8xbYXUTHS8ghxQ1WZMqEfS+AA=; b=T4EcqSV0fArQqdm7XjhgqiRbrxIsUn0i8uw5oGg3qyW71C3SCk/7Yas25xMMEUFeJp pkJBJkdjRtFYICz2Z5aXjBaTwwVyvojdFsHbXczBZlvxq9EgRNzekuJmQSrScDbh4HFf /SLiR9pEnn4MJOszMGpJNeEF194/rKg9eG6NkNhCnXyD0/irlPeKQzDKiix5Pfrulj4v hM2lKjxgDDr8RldIGK6e2foTAB13NFK+gYBczSghu/YBB3OAtDi4U7HCnSSAS24sqSq6 dkJhCzT+RyDSyXMZUtv2e2nOF6LzHzMqcvZzIGrTf8zXYTat+4ix8KaI0e8+WlhTdwhA k8bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RDMOJXPf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h4-v6si2401335pln.468.2018.04.18.18.34.38; Wed, 18 Apr 2018 18:34:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RDMOJXPf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753206AbeDSBef (ORCPT + 29 others); Wed, 18 Apr 2018 21:34:35 -0400 Received: from mail-ot0-f195.google.com ([74.125.82.195]:47020 "EHLO mail-ot0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752899AbeDSBe3 (ORCPT ); Wed, 18 Apr 2018 21:34:29 -0400 Received: by mail-ot0-f195.google.com with SMTP id v64-v6so4098626otb.13 for ; Wed, 18 Apr 2018 18:34:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=xyuB+Mlp2g4Wi5znxx8xbYXUTHS8ghxQ1WZMqEfS+AA=; b=RDMOJXPfOAdQUsMRhxI3Wnkg/86FFeuKOFD5f/+o1zko9jD74iiDUyaLqubiEjU1vO DIzhqrNMgmcwUknAsvRvWKCjk6JiTa/Ok4+rxI7zLJdp+wclHm6M34meBHn9mX9LRkgU tQSZ7gVjZJkln0bnVmc2T9xppPM+NfiuuMVgQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=xyuB+Mlp2g4Wi5znxx8xbYXUTHS8ghxQ1WZMqEfS+AA=; b=Vqp7MYIV716BtSpqpZUqKdoPXnzfoGEAEZwZOaykGVbjEzZqEZvIP4TqhnsTemfqxm msjhUD5pEXTPOB4wX8ZV/YkEKdROT5iLZnXMsNXShGd3ahdak8FTvFHGU9v4wjyuhjq5 lxLKNU7x3CiQkqiMA7keXFa8vTfcAxTt9nWsfnW2YnpSplFKAjopjMcMgCxuNBPNsVLu r87BnmLnb36S+qLjZy7POY4X4FJTVWBweDDa7DAv/2MhFca1Q3kwhBzaYsq8+67LcoST CA5XXcMn6QQvjMl+yGcjeqmWc7Uv4OHR9y6xTM2oYRGG4vOANlPCA0wQcnhL6Q9ju4pH Mlpw== X-Gm-Message-State: ALQs6tBh7wP3sIE5Tgr2OyR7WtCVmMp2209oXmeITB7AOgNyI9PSIqLH E+nVvYVV90Om/NvpdxDOvGyiuw== X-Received: by 2002:a9d:1920:: with SMTP id j32-v6mr2897841ota.383.1524101669169; Wed, 18 Apr 2018 18:34:29 -0700 (PDT) Received: from localhost.localdomain (li973-238.members.linode.com. [45.33.19.238]) by smtp.gmail.com with ESMTPSA id m84-v6sm1389662oia.1.2018.04.18.18.34.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 18 Apr 2018 18:34:28 -0700 (PDT) From: Leo Yan To: Alexei Starovoitov , Daniel Borkmann , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Leo Yan Subject: [PATCH bpf-next 2/5] samples/bpf: Dynamically allocate structure 'syms' Date: Thu, 19 Apr 2018 09:34:03 +0800 Message-Id: <1524101646-6544-3-git-send-email-leo.yan@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1524101646-6544-1-git-send-email-leo.yan@linaro.org> References: <1524101646-6544-1-git-send-email-leo.yan@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Structure 'syms' is used to store kernel symbol info by reading proc fs node '/proc/kallsyms', this structure is declared with 300000 entries and static linked into bss section. For most case the kernel symbols has less than 300000 entries, so it's safe to define so large array, but the side effect is bss section is big introduced by this structure and it isn't flexible. To fix this, this patch dynamically allocates memory for structure 'syms' based on parsing '/proc/kallsyms' line number at the runtime, which can save elf file required memory significantly. Before: text data bss dec hex filename 18841 1172 5199776 5219789 4fa5cd samples/bpf/sampleip After: text data bss dec hex filename 19101 1188 399792 420081 668f1 samples/bpf/sampleip Signed-off-by: Leo Yan --- samples/bpf/bpf_load.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) -- 1.9.1 diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c index 28e4678..c2bf7ca 100644 --- a/samples/bpf/bpf_load.c +++ b/samples/bpf/bpf_load.c @@ -651,8 +651,7 @@ void read_trace_pipe(void) } } -#define MAX_SYMS 300000 -static struct ksym syms[MAX_SYMS]; +static struct ksym *syms; static int sym_cnt; static int ksym_cmp(const void *p1, const void *p2) @@ -678,12 +677,30 @@ int load_kallsyms(void) break; if (!addr) continue; + sym_cnt++; + } + + syms = calloc(sym_cnt, sizeof(*syms)); + if (!syms) { + fclose(f); + return -ENOMEM; + } + + rewind(f); + while (!feof(f)) { + if (!fgets(buf, sizeof(buf), f)) + break; + if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3) + break; + if (!addr) + continue; syms[i].addr = (long) addr; syms[i].name = strdup(func); i++; } - sym_cnt = i; qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp); + + fclose(f); return 0; }