From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4A74C169C4 for ; Wed, 6 Feb 2019 05:46:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A5D282080A for ; Wed, 6 Feb 2019 05:46:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jmZ1N3ci" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727579AbfBFFq1 (ORCPT ); Wed, 6 Feb 2019 00:46:27 -0500 Received: from mail-ot1-f65.google.com ([209.85.210.65]:39908 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725897AbfBFFq1 (ORCPT ); Wed, 6 Feb 2019 00:46:27 -0500 Received: by mail-ot1-f65.google.com with SMTP id n8so10026674otl.6 for ; Tue, 05 Feb 2019 21:46:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=GrKTrejmo4+OmlGWm3wn8SVgoiSEhIWbbN/772pYW08=; b=jmZ1N3ci6eBoCwiNKU2AaEEgdpIdQU7h2u5+6FJKXokiBuwvVOjiTskCYVPYg5tjNX fPsG6388V5oYeUY0fLg2nUvPGGPeWQ5jr0t5XPPb779pEeIIxL4EJ6rmNqEN1S2vPmWl pIB9sUHlCJc0IvgUuexjzuizkCMfsBkc20HCAh/ngyuCPX5eITtDHE9QYE1hRRSMjddN M9BJvkVa1KiYPvmVLFZS5DHoyh+rvqyHO31/WqLSa8c68lCPH+e1MfOCmaT55K2yTSN5 ar6ZdydWJ4DeO29EaUGberice6PVi8UjpmBDtQcwVcrZ//yw+UxKWWXKRMATxIG0DkCe 5kuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=GrKTrejmo4+OmlGWm3wn8SVgoiSEhIWbbN/772pYW08=; b=asi/MZM7yEiUfTZ8en0QYKBLQAo/xbyf4r/naql43+wRCl7TgEro3l1LFn+D8yCRZL CxVkLQM8RlNdmEnwauuZqWpzRw4XdDodszNfLLKMMHRn58vpNMn6NMdOcnlqB7CSLFtd JqUfZddzXMaWb4LQiXsgpFOfPMCO0770Wfq9zC5GvaBL0B9s7OIDeoMGBfONzKq4r/as 476xb+LLhOUxQGKt85j7cLI4i2wBLFfqq66LkNO/tBwH4xvTOHH7C5aeCRtenJg+ow9Q LHgCq7X4FhH4gmywn/pQ4KwUcT8ydpJq8ns0H/78WsCsv3wpn5NKxPYE9FXmI/oCrjlF suww== X-Gm-Message-State: AHQUAuZ8rlWCCG+N8zVPfFoAoIN6gaRI8K4Gf4Zh+WwS7SvhktrSzFqM cwMup28K/QN8QtFgRnAkeG8A6XnhjM0IIDIogmbqd4w1r4Q= X-Google-Smtp-Source: AHgI3IaPSyJzcwmSr/rWucmO/TDqETIKSUotHAqzzkvsknNQECHO5d/q7yRxDFeWWv+aeGjomz+U1dBjc4HMWqJ4iqs= X-Received: by 2002:aca:60c1:: with SMTP id u184mr4652675oib.45.1549431986241; Tue, 05 Feb 2019 21:46:26 -0800 (PST) MIME-Version: 1.0 References: <20190206002949.1915237-1-andriin@fb.com> <20190206002949.1915237-3-andriin@fb.com> <20190206030709.hrxaorkkmgaxn5au@ast-mbp> In-Reply-To: <20190206030709.hrxaorkkmgaxn5au@ast-mbp> From: Andrii Nakryiko Date: Tue, 5 Feb 2019 21:46:14 -0800 Message-ID: Subject: Re: [PATCH v2 bpf-next 2/2] btf: expose API to work with raw btf data To: Alexei Starovoitov Cc: Andrii Nakryiko , Song Liu , Yonghong Song , Alexei Starovoitov , Martin Lau , netdev@vger.kernel.org, Daniel Borkmann Content-Type: text/plain; charset="UTF-8" Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On Tue, Feb 5, 2019 at 7:07 PM Alexei Starovoitov wrote: > > On Tue, Feb 05, 2019 at 04:29:49PM -0800, Andrii Nakryiko wrote: > > This patch exposes two new APIs btf__get_raw_data_size() and > > btf__get_raw_data() that allows to get a copy of raw BTF data out of > > struct btf. This is useful for external programs that need to manipulate > > raw data, e.g., pahole using btf__dedup() to deduplicate BTF type info > > and then writing it back to file. > > > > Signed-off-by: Andrii Nakryiko > > Acked-by: Song Liu > > --- > > tools/lib/bpf/btf.c | 10 ++++++++++ > > tools/lib/bpf/btf.h | 2 ++ > > tools/lib/bpf/libbpf.map | 2 ++ > > 3 files changed, 14 insertions(+) > > > > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c > > index 1c2ba7182400..34bfb3641aac 100644 > > --- a/tools/lib/bpf/btf.c > > +++ b/tools/lib/bpf/btf.c > > @@ -437,6 +437,16 @@ int btf__fd(const struct btf *btf) > > return btf->fd; > > } > > > > +__u32 btf__get_raw_data_size(const struct btf *btf) > > +{ > > + return btf->data_size; > > +} > > + > > +void btf__get_raw_data(const struct btf *btf, char *data) > > +{ > > + memcpy(data, btf->data, btf->data_size); > > +} > > I cannot think of any other way to use this api, > but to call btf__get_raw_data_size() first, > then malloc that much memory and then call btf__get_raw_data() > to store btf into it. > > If so, may be api should be single call that allocates, copies, > and returns pointer to new mem and its size? > Probably less error prone? > I don't have strong preference, but providing pointer to allocated memory seems more flexible and allows more clever/optimal use of memory from caller side. E.g., instead of doing two mallocs, you can imagine doing something like: int max_size = max(btf__get_raw_data_size(btf), btf_ext__get_raw_data_size(btf_ext)); char *m = malloc(max_size); btf__get_raw_data(btf, m); dump_btf_section_to_file(m, some_file); btf_ext__get_raw_data(btf_ext, m); dump_btf_ext_section_to_file(m, some_file); free(m); Also, pointer to memory could be mmap()'ed file, for instance. In general, for a library it might be a good thing to not be prescriptive as to how one gets that piece of memory. If those examples are not convincing enough, I'm happy to go with single btf__get_raw_data() call doing allocation and returning pointer.