Re: [PATCH 4/4] bpftool: clean-up usage of libbpf_get_error()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 11/8/22 6:28 AM, Sahid Orentino Ferdjaoui wrote:
bpftool is now totally compliant with libbpf 1.0 mode and is not
expected to be compiled with pre-1.0, let's clean-up the usage of
libbpf_get_error().

sidenode: We can remove the checks on NULL pointers before calling
btf__free() because that function already does the check.

Signed-off-by: Sahid Orentino Ferdjaoui <sahid.ferdjaoui@xxxxxxxxxxxxxxxxxxxxxxxx>

Ack with a few nits below.

Acked-by: Yonghong Song <yhs@xxxxxx>

---
  tools/bpf/bpftool/btf.c        | 16 +++++++---------
  tools/bpf/bpftool/btf_dumper.c |  2 +-
  tools/bpf/bpftool/gen.c        | 11 ++++-------
  tools/bpf/bpftool/iter.c       |  6 ++----
  tools/bpf/bpftool/main.c       |  7 +++----
  tools/bpf/bpftool/map.c        | 15 +++++++--------
  tools/bpf/bpftool/prog.c       | 10 +++++-----
  tools/bpf/bpftool/struct_ops.c | 15 +++++++--------
  8 files changed, 36 insertions(+), 46 deletions(-)

diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
index 68a70ac03c80..9bcd3be42358 100644
--- a/tools/bpf/bpftool/btf.c
+++ b/tools/bpf/bpftool/btf.c
@@ -467,9 +467,8 @@ static int dump_btf_c(const struct btf *btf,
  	int err = 0, i;

  	d = btf_dump__new(btf, btf_dump_printf, NULL, NULL);
-	err = libbpf_get_error(d);
-	if (err)
-		return err;
+	if (!d)
+		return errno;

Actually there is a difference here.
libbpf_get_error() will return -errno in such case. So the old
way is returning -errno and the new way returning errno.
It looks like this won't impact the functionality as the
caller checks 0/non-0 for success/failure and still use
errno for actual error. But it may still be worthwhile
to mention this in the commit message.

There are some other instances below as well.


  	printf("#ifndef __VMLINUX_H__\n");
  	printf("#define __VMLINUX_H__\n");
@@ -512,10 +511,9 @@ static struct btf *get_vmlinux_btf_from_sysfs(void)
  	struct btf *base;

  	base = btf__parse(sysfs_vmlinux, NULL);
-	if (libbpf_get_error(base)) {
-		p_err("failed to parse vmlinux BTF at '%s': %ld\n",
-		      sysfs_vmlinux, libbpf_get_error(base));
-		base = NULL;
+	if (!base) {
+		p_err("failed to parse vmlinux BTF at '%s': %d\n",
+		      sysfs_vmlinux, errno);
  	}

You can remove the bracket since only one statement in the 'if' body.


  	return base;
@@ -634,8 +632,8 @@ static int do_dump(int argc, char **argv)
  			base = get_vmlinux_btf_from_sysfs();

  		btf = btf__parse_split(*argv, base ?: base_btf);
-		err = libbpf_get_error(btf);
  		if (!btf) {
+			err = errno;
  			p_err("failed to load BTF from %s: %s",
  			      *argv, strerror(errno));
  			goto done;
@@ -681,8 +679,8 @@ static int do_dump(int argc, char **argv)
  		}

  		btf = btf__load_from_kernel_by_id_split(btf_id, base_btf);
-		err = libbpf_get_error(btf);
  		if (!btf) {
+			err = errno;
  			p_err("get btf by id (%u): %s", btf_id, strerror(errno));
  			goto done;
  		}
[...]
@@ -809,14 +809,13 @@ static int get_map_kv_btf(const struct bpf_map_info *info, struct btf **btf)

  static void free_map_kv_btf(struct btf *btf)
  {
-	if (!libbpf_get_error(btf) && btf != btf_vmlinux)
+	if (btf != btf_vmlinux)
  		btf__free(btf);
  }

  static void free_btf_vmlinux(void)
  {
-	if (!libbpf_get_error(btf_vmlinux))
-		btf__free(btf_vmlinux);
+	btf__free(btf_vmlinux);
  }

free_btf_vmlinux() contains a single btf__free() now which can be inlined and this function can be removed.


  static int
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index b6b62b3ef49b..72e1c458dadc 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -322,7 +322,7 @@ static void show_prog_metadata(int fd, __u32 num_maps)
  		return;

  	btf = btf__load_from_kernel_by_id(map_info.btf_id);
-	if (libbpf_get_error(btf))
+	if (!btf)
  		goto out_free;

  	t_datasec = btf__type_by_id(btf, map_info.btf_value_type_id);
[...]



[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux