Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1261392 > unrolled thread

[PATCH 0/5] perf bpf: Improve error code delivering and output

Started byWang Nan <wangnan0@huawei.com>
First post2015-11-03 12:10 +0100
Last post2015-11-04 02:50 +0100
Articles 6 — 4 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH 0/5] perf bpf: Improve error code delivering and output Wang Nan <wangnan0@huawei.com> - 2015-11-03 12:10 +0100
    [PATCH 2/5] perf tools: Mute libbpf when '-v' not set Wang Nan <wangnan0@huawei.com> - 2015-11-03 12:10 +0100
      [tip:perf/urgent] perf bpf: Mute libbpf when '-v' not set tip-bot for Wang Nan <tipbot@zytor.com> - 2015-11-08 08:30 +0100
    [PATCH 4/5] bpf tools: Improve libbpf error reporting Wang Nan <wangnan0@huawei.com> - 2015-11-03 12:10 +0100
    Re: [PATCH 0/5] perf bpf: Improve error code delivering and output Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-11-03 16:50 +0100
      Re: [PATCH 0/5] perf bpf: Improve error code delivering and output "Wangnan (F)" <wangnan0@huawei.com> - 2015-11-04 02:50 +0100

#1261392 — [PATCH 0/5] perf bpf: Improve error code delivering and output

FromWang Nan <wangnan0@huawei.com>
Date2015-11-03 12:10 +0100
Subject[PATCH 0/5] perf bpf: Improve error code delivering and output
Message-ID<qqHtL-1CF-7@gated-at.bofh.it>
This patchset is based on tip/core.

Arnaldo found some confusing error reports when reviewing BPF related
patches. This patch set makes some changes:

 - perf test only print ok, failed or skip. Extra messages can be seen
   by '-v'.

 - libbpf not output anything when called from perf without '-v'.

 - Create libbpf and bpf-loader specific error number group to deliver
   precise error. New strerror framework makes adding new error code
   easier.

 - Improve error messages.

After applying this patch:

When target object not exist:

 $ ls ./foo.o
 ls: cannot access ./foo.o: No such file or directory
 $ ./perf record --event foo.o sleep
 event syntax error: 'foo.o'
                      \___ Failed to load foo.o: No such file or directory

 (add -v to see detail)
 Run 'perf list' for a list of valid events

  Usage: perf record [<options>] [<command>]
     or: perf record [<options>] -- <command> [<options>]

     -e, --event <event>   event selector. use 'perf list' to list available events

When target object format invalid:

 $ cp /etc/passwd ./badbpf.o
 $ ./perf record --event ./badbpf.o sleep
 event syntax error: './badbpf.o'
                      \___ Failed to load ./badbpf.o: BPF object format invalid
 (... skip ...)

When run by normal user and /proc/sys/kernel/kptr_restrict is 1:

 $ cat /proc/sys/kernel/kptr_restrict
 1
 $ ./perf record --event ./bpf-script-example.o sleep
 Failed to init vmlinux path.
 event syntax error: './bpf-script-example.o'
                      \___ You need to be root, and /proc/sys/kernel/kptr_restrict should be 0
 (... skip ...)

After fixing /proc/sys/kernel/kptr_restrict:

 $ sudo -s
 # echo 0 > /proc/sys/kernel/kptr_restrict
 # exit
 $ ./perf record --event ./bpf-script-example.o sleep
 Failed to open kprobe_events: Permission denied
 event syntax error: './bpf-script-example.o'
                      \___ You need to be root
 (... skip ...)

Load an object with no 'version' section:

 # ./perf record --event ./bpf-script-example.o sleep
 event syntax error: './bpf-script-example.o'
                      \___ Failed to load ./bpf-script-example.o: 'version' section incorrect or lost
 (... skip ...)

Load an object with incorrect 'version' section:
 # ./perf record --event ./bpf-script-example.o sleep
 event syntax error: './bpf-script-example.o'
                      \___ Failed to load program: Validate your program and check 'license'/'version' sections in your object
 (... skip ...)

When event name not set:

 # ./perf record --event ./bpf-script-example.o sleep
 event syntax error: './bpf-script-example.o'
                      \___ No event name found in config string
 (... skip ...)

Wang Nan (5):
  perf test: Keep test result clean if '-v' not set
  perf tools: Mute libbpf when '-v' not set
  perf tools: Parsing libbpf return value using err.h
  bpf tools: Improve libbpf error reporting
  perf tools: Improve BPF related error messages output

 tools/lib/bpf/libbpf.c             | 149 +++++++++++++++++++++++++------------
 tools/lib/bpf/libbpf.h             |  12 +++
 tools/perf/tests/attr.c            |   3 +-
 tools/perf/tests/code-reading.c    |   8 +-
 tools/perf/tests/keep-tracking.c   |   4 +-
 tools/perf/tests/llvm.c            |  13 ++--
 tools/perf/tests/switch-tracking.c |   4 +-
 tools/perf/util/bpf-loader.c       | 110 +++++++++++++++++++++++----
 tools/perf/util/bpf-loader.h       |  18 +++++
 tools/perf/util/parse-events.c     |   7 +-
 10 files changed, 243 insertions(+), 85 deletions(-)

-- 
1.8.3.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1261393 — [PATCH 2/5] perf tools: Mute libbpf when '-v' not set

FromWang Nan <wangnan0@huawei.com>
Date2015-11-03 12:10 +0100
Subject[PATCH 2/5] perf tools: Mute libbpf when '-v' not set
Message-ID<qqHtL-1CF-9@gated-at.bofh.it>
In reply to#1261392
According to [1], libbpf should be muted. This patch reset info and
warning message level to ensure libbpf doesn't output anything even
if error happened.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/bpf-loader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index ba6f752..0c5d174 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -26,8 +26,8 @@ static int libbpf_##name(const char *fmt, ...)	\
 	return ret;				\
 }
 
-DEFINE_PRINT_FN(warning, 0)
-DEFINE_PRINT_FN(info, 0)
+DEFINE_PRINT_FN(warning, 1)
+DEFINE_PRINT_FN(info, 1)
 DEFINE_PRINT_FN(debug, 1)
 
 struct bpf_prog_priv {
-- 
1.8.3.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1265009 — [tip:perf/urgent] perf bpf: Mute libbpf when '-v' not set

Fromtip-bot for Wang Nan <tipbot@zytor.com>
Date2015-11-08 08:30 +0100
Subject[tip:perf/urgent] perf bpf: Mute libbpf when '-v' not set
Message-ID<qssAi-5wU-9@gated-at.bofh.it>
In reply to#1261393
Commit-ID:  7a0119468c9c2deff24ef24e1b4d2c1bd1523fd5
Gitweb:     http://git.kernel.org/tip/7a0119468c9c2deff24ef24e1b4d2c1bd1523fd5
Author:     Wang Nan <wangnan0@huawei.com>
AuthorDate: Tue, 3 Nov 2015 10:44:43 +0000
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Tue, 3 Nov 2015 12:06:04 -0300

perf bpf: Mute libbpf when '-v' not set

According to [1], libbpf should be muted. This patch reset info and
warning message level to ensure libbpf doesn't output anything even
if error happened.

[1] http://lkml.kernel.org/r/20151020151255.GF5119@kernel.org

Committer note:

Before:

Testing it with an incompatible kernel version in the .c file that
generated foo.o:

  [root@zoo ~]# perf record -e /tmp/foo.o sleep 1
  libbpf: load bpf program failed: Invalid argument
  libbpf: -- BEGIN DUMP LOG ---
  libbpf:

  libbpf: -- END LOG --
  libbpf: failed to load program 'fork=_do_fork'
  libbpf: failed to load object '/tmp/foo.o'
  event syntax error: '/tmp/foo.o'
                       \___ Invalid argument: Are you root and runing a CONFIG_BPF_SYSCALL kernel?

  (add -v to see detail)
  Run 'perf list' for a list of valid events

   Usage: perf record [<options>] [<command>]
      or: perf record [<options>] -- <command> [<options>]

      -e, --event <event>   event selector. use 'perf list' to list available events
  [root@zoo ~]#

After:

  [root@zoo ~]# perf record -e /tmp/foo.o sleep 1
  event syntax error: '/tmp/foo.o'
                       \___ Invalid argument: Are you root and runing a CONFIG_BPF_SYSCALL kernel?

  (add -v to see detail)
  Run 'perf list' for a list of valid events

   Usage: perf record [<options>] [<command>]
      or: perf record [<options>] -- <command> [<options>]

      -e, --event <event>   event selector. use 'perf list' to list available events
  [root@zoo ~]#

This, BTW, need fixing to emit a proper message by validating the
version in the foo.o "version" ELF section against the running kernel,
warning the user instead of asking the kernel to load a binary that it
will refuse due to unmatching kernel version.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1446547486-229499-3-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/bpf-loader.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index ba6f752..0c5d174 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -26,8 +26,8 @@ static int libbpf_##name(const char *fmt, ...)	\
 	return ret;				\
 }
 
-DEFINE_PRINT_FN(warning, 0)
-DEFINE_PRINT_FN(info, 0)
+DEFINE_PRINT_FN(warning, 1)
+DEFINE_PRINT_FN(info, 1)
 DEFINE_PRINT_FN(debug, 1)
 
 struct bpf_prog_priv {
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1261398 — [PATCH 4/5] bpf tools: Improve libbpf error reporting

FromWang Nan <wangnan0@huawei.com>
Date2015-11-03 12:10 +0100
Subject[PATCH 4/5] bpf tools: Improve libbpf error reporting
Message-ID<qqHDs-1VD-19@gated-at.bofh.it>
In reply to#1261392
In this patch, a series libbpf specific error numbers and
libbpf_strerror() are created to help report error to caller. Functions
are updated to pass correct error number through macro CHECK_ERR().

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/lib/bpf/libbpf.c | 149 +++++++++++++++++++++++++++++++++----------------
 tools/lib/bpf/libbpf.h |  12 ++++
 2 files changed, 113 insertions(+), 48 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 4252fc2..74c64b1 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -61,6 +61,62 @@ void libbpf_set_print(libbpf_print_fn_t warn,
 	__pr_debug = debug;
 }
 
+#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
+#define STRERR_BUFSIZE  128
+
+struct {
+	int code;
+	const char *msg;
+} libbpf_strerror_table[] = {
+	{LIBBPF_ERRNO__ELIBELF, "Something wrong in libelf"},
+	{LIBBPF_ERRNO__EFORMAT, "BPF object format invalid"},
+	{LIBBPF_ERRNO__EKVERSION, "'version' section incorrect or lost"},
+	{LIBBPF_ERRNO__EENDIAN, "Endian missmatch"},
+	{LIBBPF_ERRNO__EINTERNAL, "Internal error in libbpf"},
+	{LIBBPF_ERRNO__ERELOC, "Relocation failed"},
+	{LIBBPF_ERRNO__ELOAD, "Failed to load program"},
+};
+
+int libbpf_strerror(int err, char *buf, size_t size)
+{
+	unsigned int i;
+
+	if (!buf || !size)
+		return -1;
+
+	err = err > 0 ? err : -err;
+
+	if (err < LIBBPF_ERRNO__START) {
+		int ret;
+
+		ret = strerror_r(err, buf, size);
+		buf[size - 1] = '\0';
+		return ret;
+	}
+
+	for (i = 0; i < ARRAY_SIZE(libbpf_strerror_table); i++) {
+		if (libbpf_strerror_table[i].code == err) {
+			const char *msg;
+
+			msg = libbpf_strerror_table[i].msg;
+			snprintf(buf, size, "%s", msg);
+			buf[size - 1] = '\0';
+			return 0;
+		}
+	}
+
+	snprintf(buf, size, "Unknown libbpf error %d", err);
+	buf[size - 1] = '\0';
+	return -1;
+}
+
+#define CHECK_ERR(action, err, out) do {	\
+	err = action;			\
+	if (err)			\
+		goto out;		\
+} while(0)
+
+
 /* Copied from tools/perf/util/util.h */
 #ifndef zfree
 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
@@ -258,7 +314,7 @@ static struct bpf_object *bpf_object__new(const char *path,
 	obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
 	if (!obj) {
 		pr_warning("alloc memory failed for %s\n", path);
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 	}
 
 	strcpy(obj->path, path);
@@ -305,7 +361,7 @@ static int bpf_object__elf_init(struct bpf_object *obj)
 
 	if (obj_elf_valid(obj)) {
 		pr_warning("elf init: internal error\n");
-		return -EEXIST;
+		return -LIBBPF_ERRNO__ELIBELF;
 	}
 
 	if (obj->efile.obj_buf_sz > 0) {
@@ -331,14 +387,14 @@ static int bpf_object__elf_init(struct bpf_object *obj)
 	if (!obj->efile.elf) {
 		pr_warning("failed to open %s as ELF file\n",
 				obj->path);
-		err = -EINVAL;
+		err = -LIBBPF_ERRNO__ELIBELF;
 		goto errout;
 	}
 
 	if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
 		pr_warning("failed to get EHDR from %s\n",
 				obj->path);
-		err = -EINVAL;
+		err = -LIBBPF_ERRNO__EFORMAT;
 		goto errout;
 	}
 	ep = &obj->efile.ehdr;
@@ -346,7 +402,7 @@ static int bpf_object__elf_init(struct bpf_object *obj)
 	if ((ep->e_type != ET_REL) || (ep->e_machine != 0)) {
 		pr_warning("%s is not an eBPF object file\n",
 			obj->path);
-		err = -EINVAL;
+		err = -LIBBPF_ERRNO__EFORMAT;
 		goto errout;
 	}
 
@@ -374,14 +430,14 @@ bpf_object__check_endianness(struct bpf_object *obj)
 			goto mismatch;
 		break;
 	default:
-		return -EINVAL;
+		return -LIBBPF_ERRNO__EENDIAN;
 	}
 
 	return 0;
 
 mismatch:
 	pr_warning("Error: endianness mismatch.\n");
-	return -EINVAL;
+	return -LIBBPF_ERRNO__EENDIAN;
 }
 
 static int
@@ -402,7 +458,7 @@ bpf_object__init_kversion(struct bpf_object *obj,
 
 	if (size != sizeof(kver)) {
 		pr_warning("invalid kver section in %s\n", obj->path);
-		return -EINVAL;
+		return -LIBBPF_ERRNO__EFORMAT;
 	}
 	memcpy(&kver, data, sizeof(kver));
 	obj->kern_version = kver;
@@ -444,7 +500,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 	if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
 		pr_warning("failed to get e_shstrndx from %s\n",
 			   obj->path);
-		return -EINVAL;
+		return -LIBBPF_ERRNO__EFORMAT;
 	}
 
 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
@@ -456,7 +512,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 		if (gelf_getshdr(scn, &sh) != &sh) {
 			pr_warning("failed to get section header from %s\n",
 				   obj->path);
-			err = -EINVAL;
+			err = -LIBBPF_ERRNO__EFORMAT;
 			goto out;
 		}
 
@@ -464,7 +520,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 		if (!name) {
 			pr_warning("failed to get section name from %s\n",
 				   obj->path);
-			err = -EINVAL;
+			err = -LIBBPF_ERRNO__EFORMAT;
 			goto out;
 		}
 
@@ -472,7 +528,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 		if (!data) {
 			pr_warning("failed to get section data from %s(%s)\n",
 				   name, obj->path);
-			err = -EINVAL;
+			err = -LIBBPF_ERRNO__EFORMAT;
 			goto out;
 		}
 		pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
@@ -495,7 +551,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 			if (obj->efile.symbols) {
 				pr_warning("bpf: multiple SYMTAB in %s\n",
 					   obj->path);
-				err = -EEXIST;
+				err = -LIBBPF_ERRNO__EFORMAT;
 			} else
 				obj->efile.symbols = data;
 		} else if ((sh.sh_type == SHT_PROGBITS) &&
@@ -504,7 +560,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 			err = bpf_object__add_program(obj, data->d_buf,
 						      data->d_size, name, idx);
 			if (err) {
-				char errmsg[128];
+				char errmsg[STRERR_BUFSIZE];
+
 				strerror_r(-err, errmsg, sizeof(errmsg));
 				pr_warning("failed to alloc program %s (%s): %s",
 					   name, obj->path, errmsg);
@@ -576,7 +633,7 @@ bpf_program__collect_reloc(struct bpf_program *prog,
 
 		if (!gelf_getrel(data, i, &rel)) {
 			pr_warning("relocation: failed to get %d reloc\n", i);
-			return -EINVAL;
+			return -LIBBPF_ERRNO__EFORMAT;
 		}
 
 		insn_idx = rel.r_offset / sizeof(struct bpf_insn);
@@ -587,20 +644,20 @@ bpf_program__collect_reloc(struct bpf_program *prog,
 				 &sym)) {
 			pr_warning("relocation: symbol %"PRIx64" not found\n",
 				   GELF_R_SYM(rel.r_info));
-			return -EINVAL;
+			return -LIBBPF_ERRNO__EFORMAT;
 		}
 
 		if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
 			pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
 				   insn_idx, insns[insn_idx].code);
-			return -EINVAL;
+			return -LIBBPF_ERRNO__ERELOC;
 		}
 
 		map_idx = sym.st_value / sizeof(struct bpf_map_def);
 		if (map_idx >= nr_maps) {
 			pr_warning("bpf relocation: map_idx %d large than %d\n",
 				   (int)map_idx, (int)nr_maps - 1);
-			return -EINVAL;
+			return -LIBBPF_ERRNO__ERELOC;
 		}
 
 		prog->reloc_desc[i].insn_idx = insn_idx;
@@ -683,7 +740,7 @@ bpf_program__relocate(struct bpf_program *prog, int *map_fds)
 		if (insn_idx >= (int)prog->insns_cnt) {
 			pr_warning("relocation out of range: '%s'\n",
 				   prog->section_name);
-			return -ERANGE;
+			return -LIBBPF_ERRNO__ERELOC;
 		}
 		insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
 		insns[insn_idx].imm = map_fds[map_idx];
@@ -721,7 +778,7 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
 
 	if (!obj_elf_valid(obj)) {
 		pr_warning("Internal error: elf object is closed\n");
-		return -EINVAL;
+		return -LIBBPF_ERRNO__EINTERNAL;
 	}
 
 	for (i = 0; i < obj->efile.nr_reloc; i++) {
@@ -734,21 +791,21 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
 
 		if (shdr->sh_type != SHT_REL) {
 			pr_warning("internal error at %d\n", __LINE__);
-			return -EINVAL;
+			return -LIBBPF_ERRNO__EINTERNAL;
 		}
 
 		prog = bpf_object__find_prog_by_idx(obj, idx);
 		if (!prog) {
 			pr_warning("relocation failed: no %d section\n",
 				   idx);
-			return -ENOENT;
+			return -LIBBPF_ERRNO__ERELOC;
 		}
 
 		err = bpf_program__collect_reloc(prog, nr_maps,
 						 shdr, data,
 						 obj->efile.symbols);
 		if (err)
-			return -EINVAL;
+			return err;
 	}
 	return 0;
 }
@@ -777,7 +834,7 @@ load_program(struct bpf_insn *insns, int insns_cnt,
 		goto out;
 	}
 
-	ret = -EINVAL;
+	ret = -LIBBPF_ERRNO__ELOAD;
 	pr_warning("load bpf program failed: %s\n", strerror(errno));
 
 	if (log_buf) {
@@ -831,7 +888,7 @@ static int bpf_object__validate(struct bpf_object *obj)
 	if (obj->kern_version == 0) {
 		pr_warning("%s doesn't provide kernel version\n",
 			   obj->path);
-		return -EINVAL;
+		return -LIBBPF_ERRNO__EKVERSION;
 	}
 	return 0;
 }
@@ -840,32 +897,28 @@ static struct bpf_object *
 __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
 {
 	struct bpf_object *obj;
+	int err;
 
 	if (elf_version(EV_CURRENT) == EV_NONE) {
 		pr_warning("failed to init libelf for %s\n", path);
-		return NULL;
+		return ERR_PTR(-LIBBPF_ERRNO__ELIBELF);
 	}
 
 	obj = bpf_object__new(path, obj_buf, obj_buf_sz);
-	if (!obj)
-		return NULL;
+	if (IS_ERR(obj))
+		return obj;
 
-	if (bpf_object__elf_init(obj))
-		goto out;
-	if (bpf_object__check_endianness(obj))
-		goto out;
-	if (bpf_object__elf_collect(obj))
-		goto out;
-	if (bpf_object__collect_reloc(obj))
-		goto out;
-	if (bpf_object__validate(obj))
-		goto out;
+	CHECK_ERR(bpf_object__elf_init(obj), err, out);
+	CHECK_ERR(bpf_object__check_endianness(obj), err, out);
+	CHECK_ERR(bpf_object__elf_collect(obj), err, out);
+	CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
+	CHECK_ERR(bpf_object__validate(obj), err, out);
 
 	bpf_object__elf_finish(obj);
 	return obj;
 out:
 	bpf_object__close(obj);
-	return NULL;
+	return ERR_PTR(err);
 }
 
 struct bpf_object *bpf_object__open(const char *path)
@@ -922,6 +975,8 @@ int bpf_object__unload(struct bpf_object *obj)
 
 int bpf_object__load(struct bpf_object *obj)
 {
+	int err;
+
 	if (!obj)
 		return -EINVAL;
 
@@ -931,18 +986,16 @@ int bpf_object__load(struct bpf_object *obj)
 	}
 
 	obj->loaded = true;
-	if (bpf_object__create_maps(obj))
-		goto out;
-	if (bpf_object__relocate(obj))
-		goto out;
-	if (bpf_object__load_progs(obj))
-		goto out;
+
+	CHECK_ERR(bpf_object__create_maps(obj), err, out);
+	CHECK_ERR(bpf_object__relocate(obj), err, out);
+	CHECK_ERR(bpf_object__load_progs(obj), err, out);
 
 	return 0;
 out:
 	bpf_object__unload(obj);
 	pr_warning("failed to load object '%s'\n", obj->path);
-	return -EINVAL;
+	return err;
 }
 
 void bpf_object__close(struct bpf_object *obj)
@@ -990,7 +1043,7 @@ const char *
 bpf_object__get_name(struct bpf_object *obj)
 {
 	if (!obj)
-		return NULL;
+		return ERR_PTR(-EINVAL);
 	return obj->path;
 }
 
@@ -1043,7 +1096,7 @@ const char *bpf_program__title(struct bpf_program *prog, bool dup)
 		title = strdup(title);
 		if (!title) {
 			pr_warning("failed to strdup program title\n");
-			return NULL;
+			return ERR_PTR(-ENOMEM);
 		}
 	}
 
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index f16170c..c2606ae 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -10,6 +10,18 @@
 
 #include <stdio.h>
 #include <stdbool.h>
+#include <linux/err.h>
+
+#define LIBBPF_ERRNO__START	4000
+#define LIBBPF_ERRNO__ELIBELF	4000	/* Something wrong in libelf */
+#define LIBBPF_ERRNO__EFORMAT	4001	/* BPF object format invalid */
+#define LIBBPF_ERRNO__EKVERSION	4002	/* Incorrect or no 'version' section */
+#define LIBBPF_ERRNO__EENDIAN	4003	/* Endian missmatch */
+#define LIBBPF_ERRNO__EINTERNAL	4004	/* Internal error in libbpf */
+#define LIBBPF_ERRNO__ERELOC	4005	/* Relocation failed */
+#define LIBBPF_ERRNO__ELOAD	4006	/* Failed to load program */
+
+int libbpf_strerror(int err, char *buf, size_t size);
 
 /*
  * In include/linux/compiler-gcc.h, __printf is defined. However
-- 
1.8.3.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1261634

FromArnaldo Carvalho de Melo <acme@kernel.org>
Date2015-11-03 16:50 +0100
Message-ID<qqM0p-4Hz-21@gated-at.bofh.it>
In reply to#1261392
Em Tue, Nov 03, 2015 at 10:44:41AM +0000, Wang Nan escreveu:
> This patchset is based on tip/core.
> 
> Arnaldo found some confusing error reports when reviewing BPF related
> patches. This patch set makes some changes:

Thanks for working on this!

I applied the first two and made comments about the conversion to using
err.h, please check,

- Arnaldo
 
>  - perf test only print ok, failed or skip. Extra messages can be seen
>    by '-v'.
> 
>  - libbpf not output anything when called from perf without '-v'.
> 
>  - Create libbpf and bpf-loader specific error number group to deliver
>    precise error. New strerror framework makes adding new error code
>    easier.
> 
>  - Improve error messages.
> 
> After applying this patch:
> 
> When target object not exist:
> 
>  $ ls ./foo.o
>  ls: cannot access ./foo.o: No such file or directory
>  $ ./perf record --event foo.o sleep
>  event syntax error: 'foo.o'
>                       \___ Failed to load foo.o: No such file or directory
> 
>  (add -v to see detail)
>  Run 'perf list' for a list of valid events
> 
>   Usage: perf record [<options>] [<command>]
>      or: perf record [<options>] -- <command> [<options>]
> 
>      -e, --event <event>   event selector. use 'perf list' to list available events
> 
> When target object format invalid:
> 
>  $ cp /etc/passwd ./badbpf.o
>  $ ./perf record --event ./badbpf.o sleep
>  event syntax error: './badbpf.o'
>                       \___ Failed to load ./badbpf.o: BPF object format invalid
>  (... skip ...)
> 
> When run by normal user and /proc/sys/kernel/kptr_restrict is 1:
> 
>  $ cat /proc/sys/kernel/kptr_restrict
>  1
>  $ ./perf record --event ./bpf-script-example.o sleep
>  Failed to init vmlinux path.
>  event syntax error: './bpf-script-example.o'
>                       \___ You need to be root, and /proc/sys/kernel/kptr_restrict should be 0
>  (... skip ...)
> 
> After fixing /proc/sys/kernel/kptr_restrict:
> 
>  $ sudo -s
>  # echo 0 > /proc/sys/kernel/kptr_restrict
>  # exit
>  $ ./perf record --event ./bpf-script-example.o sleep
>  Failed to open kprobe_events: Permission denied
>  event syntax error: './bpf-script-example.o'
>                       \___ You need to be root
>  (... skip ...)
> 
> Load an object with no 'version' section:
> 
>  # ./perf record --event ./bpf-script-example.o sleep
>  event syntax error: './bpf-script-example.o'
>                       \___ Failed to load ./bpf-script-example.o: 'version' section incorrect or lost
>  (... skip ...)
> 
> Load an object with incorrect 'version' section:
>  # ./perf record --event ./bpf-script-example.o sleep
>  event syntax error: './bpf-script-example.o'
>                       \___ Failed to load program: Validate your program and check 'license'/'version' sections in your object
>  (... skip ...)
> 
> When event name not set:
> 
>  # ./perf record --event ./bpf-script-example.o sleep
>  event syntax error: './bpf-script-example.o'
>                       \___ No event name found in config string
>  (... skip ...)
> 
> Wang Nan (5):
>   perf test: Keep test result clean if '-v' not set
>   perf tools: Mute libbpf when '-v' not set
>   perf tools: Parsing libbpf return value using err.h
>   bpf tools: Improve libbpf error reporting
>   perf tools: Improve BPF related error messages output
> 
>  tools/lib/bpf/libbpf.c             | 149 +++++++++++++++++++++++++------------
>  tools/lib/bpf/libbpf.h             |  12 +++
>  tools/perf/tests/attr.c            |   3 +-
>  tools/perf/tests/code-reading.c    |   8 +-
>  tools/perf/tests/keep-tracking.c   |   4 +-
>  tools/perf/tests/llvm.c            |  13 ++--
>  tools/perf/tests/switch-tracking.c |   4 +-
>  tools/perf/util/bpf-loader.c       | 110 +++++++++++++++++++++++----
>  tools/perf/util/bpf-loader.h       |  18 +++++
>  tools/perf/util/parse-events.c     |   7 +-
>  10 files changed, 243 insertions(+), 85 deletions(-)
> 
> -- 
> 1.8.3.4
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1262024

From"Wangnan (F)" <wangnan0@huawei.com>
Date2015-11-04 02:50 +0100
Message-ID<qqVn4-2e0-3@gated-at.bofh.it>
In reply to#1261634

On 2015/11/3 23:43, Arnaldo Carvalho de Melo wrote:
> Em Tue, Nov 03, 2015 at 10:44:41AM +0000, Wang Nan escreveu:
>> This patchset is based on tip/core.
>>
>> Arnaldo found some confusing error reports when reviewing BPF related
>> patches. This patch set makes some changes:
> Thanks for working on this!
>
> I applied the first two and made comments about the conversion to using
> err.h, please check,
>
> - Arnaldo
>   

I can't find them in your git repository at git.kernel.org. Could you
please recheck?

Thank you.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web