[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAEf4BzaBZyeQNqnDrp5RwMqWKFUvT0LpuXg2bmT8LD6M-9UTMA@mail.gmail.com>
Date: Mon, 16 Mar 2020 22:39:40 -0700
From: Andrii Nakryiko <andrii.nakryiko@...il.com>
To: Martin KaFai Lau <kafai@...com>
Cc: Andrii Nakryiko <andriin@...com>, bpf <bpf@...r.kernel.org>,
Networking <netdev@...r.kernel.org>,
Alexei Starovoitov <ast@...com>,
Daniel Borkmann <daniel@...earbox.net>,
Kernel Team <kernel-team@...com>
Subject: Re: [Potential Spoof] [PATCH bpf-next 3/3] selftests/bpf: reset
process and thread affinity after each test/sub-test
On Mon, Mar 16, 2020 at 10:35 PM Martin KaFai Lau <kafai@...com> wrote:
>
> On Fri, Mar 13, 2020 at 06:39:32PM -0700, Andrii Nakryiko wrote:
> > Some tests and sub-tests are setting "custom" thread/process affinity and
> > don't reset it back. Instead of requiring each test to undo all this, ensure
> > that thread affinity is restored by test_progs test runner itself.
> >
> > Signed-off-by: Andrii Nakryiko <andriin@...com>
> > ---
> > tools/testing/selftests/bpf/test_progs.c | 42 +++++++++++++++++++++++-
> > tools/testing/selftests/bpf/test_progs.h | 1 +
> > 2 files changed, 42 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
> > index c8cb407482c6..b521e0a512b6 100644
> > --- a/tools/testing/selftests/bpf/test_progs.c
> > +++ b/tools/testing/selftests/bpf/test_progs.c
> > @@ -1,12 +1,15 @@
> > // SPDX-License-Identifier: GPL-2.0-only
> > /* Copyright (c) 2017 Facebook
> > */
> > +#define _GNU_SOURCE
> > #include "test_progs.h"
> > #include "cgroup_helpers.h"
> > #include "bpf_rlimit.h"
> > #include <argp.h>
> > -#include <string.h>
> > +#include <pthread.h>
> > +#include <sched.h>
> > #include <signal.h>
> > +#include <string.h>
> > #include <execinfo.h> /* backtrace */
> >
> > /* defined in test_progs.h */
> > @@ -90,6 +93,34 @@ static void skip_account(void)
> > }
> > }
> >
> > +static void stdio_restore(void);
> > +
> > +/* A bunch of tests set custom affinity per-thread and/or per-process. Reset
> > + * it after each test/sub-test.
> > + */
> > +static void reset_affinity() {
> > +
> > + cpu_set_t cpuset;
> > + int i, err;
> > +
> > + CPU_ZERO(&cpuset);
> > + for (i = 0; i < env.nr_cpus; i++)
> > + CPU_SET(i, &cpuset);
> In case the user may run "taskset somemask test_progs",
> is it better to store the inital_cpuset at the beginning
> of main and then restore to inital_cpuset after each run?
Not sure it's worth it (it's test runner, not really a general-purpose
tool), but I can add that for sure.
>
> > +
> > + err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
> > + if (err < 0) {
> > + stdio_restore();
> > + fprintf(stderr, "Failed to reset process affinity: %d!\n", err);
> > + exit(-1);
> > + }
> > + err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
> > + if (err < 0) {
> > + stdio_restore();
> > + fprintf(stderr, "Failed to reset thread affinity: %d!\n", err);
> > + exit(-1);
> > + }
> > +}
Powered by blists - more mailing lists