diff mbox series

[v2,bpf-next,3/3] libbpf: btf typed dump does not need to allocate dump data

Message ID 1626475617-25984-4-git-send-email-alan.maguire@oracle.com (mailing list archive)
State Accepted
Delegated to: BPF
Headers show
Series libbpf: BTF typed dump cleanups | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 22 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Alan Maguire July 16, 2021, 10:46 p.m. UTC
By using the stack for this small structure, we avoid the need
for freeing memory in error paths.

Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
 tools/lib/bpf/btf_dump.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

Andrii Nakryiko July 17, 2021, 12:32 a.m. UTC | #1
On Fri, Jul 16, 2021 at 3:47 PM Alan Maguire <alan.maguire@oracle.com> wrote:
>
> By using the stack for this small structure, we avoid the need
> for freeing memory in error paths.
>
> Suggested-by: Andrii Nakryiko <andrii@kernel.org>
> Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
> ---
>  tools/lib/bpf/btf_dump.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
> index e5fbfb8..bd8e005 100644
> --- a/tools/lib/bpf/btf_dump.c
> +++ b/tools/lib/bpf/btf_dump.c
> @@ -2240,6 +2240,7 @@ int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
>                              const void *data, size_t data_sz,
>                              const struct btf_dump_type_data_opts *opts)
>  {
> +       struct btf_dump_data typed_dump = {};
>         const struct btf_type *t;
>         int ret;
>
> @@ -2250,7 +2251,7 @@ int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
>         if (!t)
>                 return libbpf_err(-ENOENT);
>
> -       d->typed_dump = calloc(1, sizeof(struct btf_dump_data));
> +       d->typed_dump = &typed_dump;
>         if (!d->typed_dump)
>                 return libbpf_err(-ENOMEM);

can't happen, removed, please pay attention to the surrounding code

>
> @@ -2269,7 +2270,5 @@ int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
>
>         ret = btf_dump_dump_type_data(d, NULL, t, id, data, 0, 0);
>
> -       free(d->typed_dump);
> -

added resetting to NULL here, so that we don't have an accidental
dangling pointers

>         return libbpf_err(ret);
>  }

> --
> 1.8.3.1
>
diff mbox series

Patch

diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index e5fbfb8..bd8e005 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -2240,6 +2240,7 @@  int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
 			     const void *data, size_t data_sz,
 			     const struct btf_dump_type_data_opts *opts)
 {
+	struct btf_dump_data typed_dump = {};
 	const struct btf_type *t;
 	int ret;
 
@@ -2250,7 +2251,7 @@  int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
 	if (!t)
 		return libbpf_err(-ENOENT);
 
-	d->typed_dump = calloc(1, sizeof(struct btf_dump_data));
+	d->typed_dump = &typed_dump;
 	if (!d->typed_dump)
 		return libbpf_err(-ENOMEM);
 
@@ -2269,7 +2270,5 @@  int btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
 
 	ret = btf_dump_dump_type_data(d, NULL, t, id, data, 0, 0);
 
-	free(d->typed_dump);
-
 	return libbpf_err(ret);
 }