diff mbox series

[bpf-next,v2,1/9] bpf: selftests: Use libbpf 1.0 API mode in bpf constructor

Message ID 20220403144300.6707-2-laoar.shao@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: RLIMIT_MEMLOCK cleanups | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline fail Detected static functions without inline keyword in header files: 1
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 2 maintainers not CCed: linux-kselftest@vger.kernel.org shuah@kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 31 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Yafang Shao April 3, 2022, 2:42 p.m. UTC
In libbpf 1.0 API mode, it will bump rlimit automatically if there's no
memcg-basaed accounting, so we can use libbpf 1.0 API mode instead in case
we want to run it in an old kernel.

The constructor is renamed to bpf_strict_all_ctor().

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 tools/testing/selftests/bpf/bpf_rlimit.h | 26 +++---------------------
 1 file changed, 3 insertions(+), 23 deletions(-)

Comments

Andrii Nakryiko April 4, 2022, 1:24 a.m. UTC | #1
On Sun, Apr 3, 2022 at 7:43 AM Yafang Shao <laoar.shao@gmail.com> wrote:
>
> In libbpf 1.0 API mode, it will bump rlimit automatically if there's no
> memcg-basaed accounting, so we can use libbpf 1.0 API mode instead in case
> we want to run it in an old kernel.
>
> The constructor is renamed to bpf_strict_all_ctor().
>
> Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> ---
>  tools/testing/selftests/bpf/bpf_rlimit.h | 26 +++---------------------
>  1 file changed, 3 insertions(+), 23 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/bpf_rlimit.h b/tools/testing/selftests/bpf/bpf_rlimit.h
> index 9dac9b30f8ef..d050f7d0bb5c 100644
> --- a/tools/testing/selftests/bpf/bpf_rlimit.h
> +++ b/tools/testing/selftests/bpf/bpf_rlimit.h
> @@ -1,28 +1,8 @@
>  #include <sys/resource.h>
>  #include <stdio.h>
>
> -static  __attribute__((constructor)) void bpf_rlimit_ctor(void)
> +static  __attribute__((constructor)) void bpf_strict_all_ctor(void)

well, no, let's get rid of bpf_rlimit.h altogether. There is no need
for constructor magic when you can have an explicit
libbpf_set_strict_mode(LIBBPF_STRICT_ALL).

>  {
> -       struct rlimit rlim_old, rlim_new = {
> -               .rlim_cur       = RLIM_INFINITY,
> -               .rlim_max       = RLIM_INFINITY,
> -       };
> -
> -       getrlimit(RLIMIT_MEMLOCK, &rlim_old);
> -       /* For the sake of running the test cases, we temporarily
> -        * set rlimit to infinity in order for kernel to focus on
> -        * errors from actual test cases and not getting noise
> -        * from hitting memlock limits. The limit is on per-process
> -        * basis and not a global one, hence destructor not really
> -        * needed here.
> -        */
> -       if (setrlimit(RLIMIT_MEMLOCK, &rlim_new) < 0) {
> -               perror("Unable to lift memlock rlimit");
> -               /* Trying out lower limit, but expect potential test
> -                * case failures from this!
> -                */
> -               rlim_new.rlim_cur = rlim_old.rlim_cur + (1UL << 20);
> -               rlim_new.rlim_max = rlim_old.rlim_max + (1UL << 20);
> -               setrlimit(RLIMIT_MEMLOCK, &rlim_new);
> -       }
> +       /* Use libbpf 1.0 API mode */
> +       libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
>  }
> --
> 2.17.1
>
Andrii Nakryiko April 4, 2022, 1:26 a.m. UTC | #2
On Sun, Apr 3, 2022 at 6:24 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Sun, Apr 3, 2022 at 7:43 AM Yafang Shao <laoar.shao@gmail.com> wrote:
> >
> > In libbpf 1.0 API mode, it will bump rlimit automatically if there's no
> > memcg-basaed accounting, so we can use libbpf 1.0 API mode instead in case

also very eye catching typo: basaed -> based

> > we want to run it in an old kernel.
> >
> > The constructor is renamed to bpf_strict_all_ctor().
> >
> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> > ---
> >  tools/testing/selftests/bpf/bpf_rlimit.h | 26 +++---------------------
> >  1 file changed, 3 insertions(+), 23 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/bpf_rlimit.h b/tools/testing/selftests/bpf/bpf_rlimit.h
> > index 9dac9b30f8ef..d050f7d0bb5c 100644
> > --- a/tools/testing/selftests/bpf/bpf_rlimit.h
> > +++ b/tools/testing/selftests/bpf/bpf_rlimit.h
> > @@ -1,28 +1,8 @@
> >  #include <sys/resource.h>
> >  #include <stdio.h>
> >
> > -static  __attribute__((constructor)) void bpf_rlimit_ctor(void)
> > +static  __attribute__((constructor)) void bpf_strict_all_ctor(void)
>
> well, no, let's get rid of bpf_rlimit.h altogether. There is no need
> for constructor magic when you can have an explicit
> libbpf_set_strict_mode(LIBBPF_STRICT_ALL).
>
> >  {
> > -       struct rlimit rlim_old, rlim_new = {
> > -               .rlim_cur       = RLIM_INFINITY,
> > -               .rlim_max       = RLIM_INFINITY,
> > -       };
> > -
> > -       getrlimit(RLIMIT_MEMLOCK, &rlim_old);
> > -       /* For the sake of running the test cases, we temporarily
> > -        * set rlimit to infinity in order for kernel to focus on
> > -        * errors from actual test cases and not getting noise
> > -        * from hitting memlock limits. The limit is on per-process
> > -        * basis and not a global one, hence destructor not really
> > -        * needed here.
> > -        */
> > -       if (setrlimit(RLIMIT_MEMLOCK, &rlim_new) < 0) {
> > -               perror("Unable to lift memlock rlimit");
> > -               /* Trying out lower limit, but expect potential test
> > -                * case failures from this!
> > -                */
> > -               rlim_new.rlim_cur = rlim_old.rlim_cur + (1UL << 20);
> > -               rlim_new.rlim_max = rlim_old.rlim_max + (1UL << 20);
> > -               setrlimit(RLIMIT_MEMLOCK, &rlim_new);
> > -       }
> > +       /* Use libbpf 1.0 API mode */
> > +       libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
> >  }
> > --
> > 2.17.1
> >
Yafang Shao April 4, 2022, 6:44 a.m. UTC | #3
On Mon, Apr 4, 2022 at 9:24 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Sun, Apr 3, 2022 at 7:43 AM Yafang Shao <laoar.shao@gmail.com> wrote:
> >
> > In libbpf 1.0 API mode, it will bump rlimit automatically if there's no
> > memcg-basaed accounting, so we can use libbpf 1.0 API mode instead in case
> > we want to run it in an old kernel.
> >
> > The constructor is renamed to bpf_strict_all_ctor().
> >
> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> > ---
> >  tools/testing/selftests/bpf/bpf_rlimit.h | 26 +++---------------------
> >  1 file changed, 3 insertions(+), 23 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/bpf_rlimit.h b/tools/testing/selftests/bpf/bpf_rlimit.h
> > index 9dac9b30f8ef..d050f7d0bb5c 100644
> > --- a/tools/testing/selftests/bpf/bpf_rlimit.h
> > +++ b/tools/testing/selftests/bpf/bpf_rlimit.h
> > @@ -1,28 +1,8 @@
> >  #include <sys/resource.h>
> >  #include <stdio.h>
> >
> > -static  __attribute__((constructor)) void bpf_rlimit_ctor(void)
> > +static  __attribute__((constructor)) void bpf_strict_all_ctor(void)
>
> well, no, let's get rid of bpf_rlimit.h altogether. There is no need
> for constructor magic when you can have an explicit
> libbpf_set_strict_mode(LIBBPF_STRICT_ALL).
>

Sure, I will do it.

> >  {
> > -       struct rlimit rlim_old, rlim_new = {
> > -               .rlim_cur       = RLIM_INFINITY,
> > -               .rlim_max       = RLIM_INFINITY,
> > -       };
> > -
> > -       getrlimit(RLIMIT_MEMLOCK, &rlim_old);
> > -       /* For the sake of running the test cases, we temporarily
> > -        * set rlimit to infinity in order for kernel to focus on
> > -        * errors from actual test cases and not getting noise
> > -        * from hitting memlock limits. The limit is on per-process
> > -        * basis and not a global one, hence destructor not really
> > -        * needed here.
> > -        */
> > -       if (setrlimit(RLIMIT_MEMLOCK, &rlim_new) < 0) {
> > -               perror("Unable to lift memlock rlimit");
> > -               /* Trying out lower limit, but expect potential test
> > -                * case failures from this!
> > -                */
> > -               rlim_new.rlim_cur = rlim_old.rlim_cur + (1UL << 20);
> > -               rlim_new.rlim_max = rlim_old.rlim_max + (1UL << 20);
> > -               setrlimit(RLIMIT_MEMLOCK, &rlim_new);
> > -       }
> > +       /* Use libbpf 1.0 API mode */
> > +       libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
> >  }
> > --
> > 2.17.1
> >
Yafang Shao April 4, 2022, 6:45 a.m. UTC | #4
On Mon, Apr 4, 2022 at 9:27 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Sun, Apr 3, 2022 at 6:24 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
> >
> > On Sun, Apr 3, 2022 at 7:43 AM Yafang Shao <laoar.shao@gmail.com> wrote:
> > >
> > > In libbpf 1.0 API mode, it will bump rlimit automatically if there's no
> > > memcg-basaed accounting, so we can use libbpf 1.0 API mode instead in case
>
> also very eye catching typo: basaed -> based

Thanks for pointing this out. I will be more careful.
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/bpf_rlimit.h b/tools/testing/selftests/bpf/bpf_rlimit.h
index 9dac9b30f8ef..d050f7d0bb5c 100644
--- a/tools/testing/selftests/bpf/bpf_rlimit.h
+++ b/tools/testing/selftests/bpf/bpf_rlimit.h
@@ -1,28 +1,8 @@ 
 #include <sys/resource.h>
 #include <stdio.h>
 
-static  __attribute__((constructor)) void bpf_rlimit_ctor(void)
+static  __attribute__((constructor)) void bpf_strict_all_ctor(void)
 {
-	struct rlimit rlim_old, rlim_new = {
-		.rlim_cur	= RLIM_INFINITY,
-		.rlim_max	= RLIM_INFINITY,
-	};
-
-	getrlimit(RLIMIT_MEMLOCK, &rlim_old);
-	/* For the sake of running the test cases, we temporarily
-	 * set rlimit to infinity in order for kernel to focus on
-	 * errors from actual test cases and not getting noise
-	 * from hitting memlock limits. The limit is on per-process
-	 * basis and not a global one, hence destructor not really
-	 * needed here.
-	 */
-	if (setrlimit(RLIMIT_MEMLOCK, &rlim_new) < 0) {
-		perror("Unable to lift memlock rlimit");
-		/* Trying out lower limit, but expect potential test
-		 * case failures from this!
-		 */
-		rlim_new.rlim_cur = rlim_old.rlim_cur + (1UL << 20);
-		rlim_new.rlim_max = rlim_old.rlim_max + (1UL << 20);
-		setrlimit(RLIMIT_MEMLOCK, &rlim_new);
-	}
+	/* Use libbpf 1.0 API mode */
+	libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
 }