bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next v2] libbpf: Print hint about ulimit when getting permission denied error
@ 2019-12-16 18:12 Toke Høiland-Jørgensen
  2019-12-16 22:54 ` Alexei Starovoitov
  2019-12-18 10:47 ` Naresh Kamboju
  0 siblings, 2 replies; 4+ messages in thread
From: Toke Høiland-Jørgensen @ 2019-12-16 18:12 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann
  Cc: Toke Høiland-Jørgensen, netdev, bpf,
	Jesper Dangaard Brouer, Yonghong Song

Probably the single most common error newcomers to XDP are stumped by is
the 'permission denied' error they get when trying to load their program
and 'ulimit -l' is set too low. For examples, see [0], [1].

Since the error code is UAPI, we can't change that. Instead, this patch
adds a few heuristics in libbpf and outputs an additional hint if they are
met: If an EPERM is returned on map create or program load, and geteuid()
shows we are root, and the current RLIMIT_MEMLOCK is not infinity, we
output a hint about raising 'ulimit -l' as an additional log line.

[0] https://marc.info/?l=xdp-newbies&m=157043612505624&w=2
[1] https://github.com/xdp-project/xdp-tutorial/issues/86

Acked-by: Jesper Dangaard Brouer <brouer@redhat.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
---
v2:
  - Format current output as KiB/MiB
  - It's ulimit -l, not ulimit -r
  
 tools/lib/bpf/libbpf.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index a2cc7313763a..3fe42d6b0c2f 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -41,6 +41,7 @@
 #include <sys/types.h>
 #include <sys/vfs.h>
 #include <sys/utsname.h>
+#include <sys/resource.h>
 #include <tools/libc_compat.h>
 #include <libelf.h>
 #include <gelf.h>
@@ -100,6 +101,32 @@ void libbpf_print(enum libbpf_print_level level, const char *format, ...)
 	va_end(args);
 }
 
+static void pr_perm_msg(int err)
+{
+	struct rlimit limit;
+	char buf[100];
+
+	if (err != -EPERM || geteuid() != 0)
+		return;
+
+	err = getrlimit(RLIMIT_MEMLOCK, &limit);
+	if (err)
+		return;
+
+	if (limit.rlim_cur == RLIM_INFINITY)
+		return;
+
+	if (limit.rlim_cur < 1024)
+		snprintf(buf, sizeof(buf), "%lu bytes", limit.rlim_cur);
+	else if (limit.rlim_cur < 1024*1024)
+		snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024);
+	else
+		snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024));
+
+	pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n",
+		buf);
+}
+
 #define STRERR_BUFSIZE  128
 
 /* Copied from tools/perf/util/util.h */
@@ -2983,6 +3010,7 @@ bpf_object__create_maps(struct bpf_object *obj)
 			cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
 			pr_warn("failed to create map (name: '%s'): %s(%d)\n",
 				map->name, cp, err);
+			pr_perm_msg(err);
 			for (j = 0; j < i; j++)
 				zclose(obj->maps[j].fd);
 			return err;
@@ -4381,6 +4409,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
 	ret = -errno;
 	cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
 	pr_warn("load bpf program failed: %s\n", cp);
+	pr_perm_msg(ret);
 
 	if (log_buf && log_buf[0] != '\0') {
 		ret = -LIBBPF_ERRNO__VERIFY;
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next v2] libbpf: Print hint about ulimit when getting permission denied error
  2019-12-16 18:12 [PATCH bpf-next v2] libbpf: Print hint about ulimit when getting permission denied error Toke Høiland-Jørgensen
@ 2019-12-16 22:54 ` Alexei Starovoitov
  2019-12-18 10:47 ` Naresh Kamboju
  1 sibling, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2019-12-16 22:54 UTC (permalink / raw)
  To: Toke Høiland-Jørgensen
  Cc: Alexei Starovoitov, Daniel Borkmann, Network Development, bpf,
	Jesper Dangaard Brouer, Yonghong Song

On Mon, Dec 16, 2019 at 10:12 AM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>
> Probably the single most common error newcomers to XDP are stumped by is
> the 'permission denied' error they get when trying to load their program
> and 'ulimit -l' is set too low. For examples, see [0], [1].
>
> Since the error code is UAPI, we can't change that. Instead, this patch
> adds a few heuristics in libbpf and outputs an additional hint if they are
> met: If an EPERM is returned on map create or program load, and geteuid()
> shows we are root, and the current RLIMIT_MEMLOCK is not infinity, we
> output a hint about raising 'ulimit -l' as an additional log line.
>
> [0] https://marc.info/?l=xdp-newbies&m=157043612505624&w=2
> [1] https://github.com/xdp-project/xdp-tutorial/issues/86
>
> Acked-by: Jesper Dangaard Brouer <brouer@redhat.com>
> Acked-by: Yonghong Song <yhs@fb.com>
> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
> ---
> v2:
>   - Format current output as KiB/MiB
>   - It's ulimit -l, not ulimit -r

Applied. Thanks

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next v2] libbpf: Print hint about ulimit when getting permission denied error
  2019-12-16 18:12 [PATCH bpf-next v2] libbpf: Print hint about ulimit when getting permission denied error Toke Høiland-Jørgensen
  2019-12-16 22:54 ` Alexei Starovoitov
@ 2019-12-18 10:47 ` Naresh Kamboju
  2019-12-18 11:08   ` Toke Høiland-Jørgensen
  1 sibling, 1 reply; 4+ messages in thread
From: Naresh Kamboju @ 2019-12-18 10:47 UTC (permalink / raw)
  To: Toke Høiland-Jørgensen
  Cc: Alexei Starovoitov, Daniel Borkmann, Netdev, bpf,
	Jesper Dangaard Brouer, Yonghong Song, lkft-triage, Leo Yan,
	Daniel Diaz

On Tue, 17 Dec 2019 at 00:00, Toke Høiland-Jørgensen <toke@redhat.com> wrote:
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index a2cc7313763a..3fe42d6b0c2f 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -41,6 +41,7 @@
>  #include <sys/types.h>
>  #include <sys/vfs.h>
>  #include <sys/utsname.h>
> +#include <sys/resource.h>
>  #include <tools/libc_compat.h>
>  #include <libelf.h>
>  #include <gelf.h>
> @@ -100,6 +101,32 @@ void libbpf_print(enum libbpf_print_level level, const char *format, ...)
>         va_end(args);
>  }
>
> +static void pr_perm_msg(int err)
> +{
> +       struct rlimit limit;
> +       char buf[100];
> +
> +       if (err != -EPERM || geteuid() != 0)
> +               return;
> +
> +       err = getrlimit(RLIMIT_MEMLOCK, &limit);
> +       if (err)
> +               return;
> +
> +       if (limit.rlim_cur == RLIM_INFINITY)
> +               return;
> +
> +       if (limit.rlim_cur < 1024)
> +               snprintf(buf, sizeof(buf), "%lu bytes", limit.rlim_cur);

 libbpf.c: In function 'pr_perm_msg':
 libbpf.c:120:33: error: format '%lu' expects argument of type 'long
unsigned int', but argument 4 has type 'rlim_t {aka long long unsigned
int}' [-Werror=format=]
    snprintf(buf, sizeof(buf), "%lu bytes", limit.rlim_cur);
                                ~~^         ~~~~~~~~~~~~~~
                                %llu

Linux next i386 and arm builds failed due to this error.

Full build log link,
https://ci.linaro.org/view/lkft/job/openembedded-lkft-linux-next/DISTRO=lkft,MACHINE=intel-core2-32,label=docker-lkft/672/consoleText
https://ci.linaro.org/view/lkft/job/openembedded-lkft-linux-next/DISTRO=lkft,MACHINE=am57xx-evm,label=docker-lkft/672/consoleText


-- 
Linaro LKFT
https://lkft.linaro.org

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf-next v2] libbpf: Print hint about ulimit when getting permission denied error
  2019-12-18 10:47 ` Naresh Kamboju
@ 2019-12-18 11:08   ` Toke Høiland-Jørgensen
  0 siblings, 0 replies; 4+ messages in thread
From: Toke Høiland-Jørgensen @ 2019-12-18 11:08 UTC (permalink / raw)
  To: Naresh Kamboju
  Cc: Alexei Starovoitov, Daniel Borkmann, Netdev, bpf,
	Jesper Dangaard Brouer, Yonghong Song, lkft-triage, Leo Yan,
	Daniel Diaz

Naresh Kamboju <naresh.kamboju@linaro.org> writes:

> On Tue, 17 Dec 2019 at 00:00, Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> index a2cc7313763a..3fe42d6b0c2f 100644
>> --- a/tools/lib/bpf/libbpf.c
>> +++ b/tools/lib/bpf/libbpf.c
>> @@ -41,6 +41,7 @@
>>  #include <sys/types.h>
>>  #include <sys/vfs.h>
>>  #include <sys/utsname.h>
>> +#include <sys/resource.h>
>>  #include <tools/libc_compat.h>
>>  #include <libelf.h>
>>  #include <gelf.h>
>> @@ -100,6 +101,32 @@ void libbpf_print(enum libbpf_print_level level, const char *format, ...)
>>         va_end(args);
>>  }
>>
>> +static void pr_perm_msg(int err)
>> +{
>> +       struct rlimit limit;
>> +       char buf[100];
>> +
>> +       if (err != -EPERM || geteuid() != 0)
>> +               return;
>> +
>> +       err = getrlimit(RLIMIT_MEMLOCK, &limit);
>> +       if (err)
>> +               return;
>> +
>> +       if (limit.rlim_cur == RLIM_INFINITY)
>> +               return;
>> +
>> +       if (limit.rlim_cur < 1024)
>> +               snprintf(buf, sizeof(buf), "%lu bytes", limit.rlim_cur);
>
>  libbpf.c: In function 'pr_perm_msg':
>  libbpf.c:120:33: error: format '%lu' expects argument of type 'long
> unsigned int', but argument 4 has type 'rlim_t {aka long long unsigned
> int}' [-Werror=format=]
>     snprintf(buf, sizeof(buf), "%lu bytes", limit.rlim_cur);
>                                 ~~^         ~~~~~~~~~~~~~~
>                                 %llu
>

Ah, guess this needs PRIu64. Will send a follow-up, thanks for the
report :)

-Toke


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-12-18 11:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-16 18:12 [PATCH bpf-next v2] libbpf: Print hint about ulimit when getting permission denied error Toke Høiland-Jørgensen
2019-12-16 22:54 ` Alexei Starovoitov
2019-12-18 10:47 ` Naresh Kamboju
2019-12-18 11:08   ` Toke Høiland-Jørgensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).