Re: [PATCH v5 bpf-next 4/4] selftests/bpf: Add selftests for cpumask iter

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

 



On Wed, Jan 31, 2024 at 6:55 AM Yafang Shao <laoar.shao@xxxxxxxxx> wrote:
>
> Add selftests for the newly added cpumask iter.
> - cpumask_iter_success
>   - The number of CPUs should be expected when iterating over the cpumask
>   - percpu data extracted from the percpu struct should be expected
>   - It can work in both non-sleepable and sleepable prog
>   - RCU lock is only required by bpf_iter_cpumask_new()
>   - It is fine without calling bpf_iter_cpumask_next()
>
> - cpumask_iter_failure
>   - RCU lock is required in sleepable prog
>   - The cpumask to be iterated over can't be NULL
>   - bpf_iter_cpumask_destroy() is required after calling
>     bpf_iter_cpumask_new()
>   - bpf_iter_cpumask_destroy() can only destroy an initilialized iter
>   - bpf_iter_cpumask_next() must use an initilialized iter

typos: initialized

>
> The result as follows,
>
>   #64/37   cpumask/test_cpumask_iter:OK
>   #64/38   cpumask/test_cpumask_iter_sleepable:OK
>   #64/39   cpumask/test_cpumask_iter_sleepable:OK
>   #64/40   cpumask/test_cpumask_iter_next_no_rcu:OK
>   #64/41   cpumask/test_cpumask_iter_no_next:OK
>   #64/42   cpumask/test_cpumask_iter:OK
>   #64/43   cpumask/test_cpumask_iter_no_rcu:OK
>   #64/44   cpumask/test_cpumask_iter_no_destroy:OK
>   #64/45   cpumask/test_cpumask_iter_null_pointer:OK
>   #64/46   cpumask/test_cpumask_iter_next_uninit:OK
>   #64/47   cpumask/test_cpumask_iter_destroy_uninit:OK
>   #64      cpumask:OK
>
> Signed-off-by: Yafang Shao <laoar.shao@xxxxxxxxx>
> ---
>  tools/testing/selftests/bpf/config            |   1 +
>  .../selftests/bpf/prog_tests/cpumask.c        | 152 ++++++++++++++++++
>  .../selftests/bpf/progs/cpumask_common.h      |   3 +
>  .../bpf/progs/cpumask_iter_failure.c          |  99 ++++++++++++
>  .../bpf/progs/cpumask_iter_success.c          | 126 +++++++++++++++
>  5 files changed, 381 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/progs/cpumask_iter_failure.c
>  create mode 100644 tools/testing/selftests/bpf/progs/cpumask_iter_success.c
>

LGTM overall, except for seemingly unnecessary use of a big macro

> diff --git a/tools/testing/selftests/bpf/progs/cpumask_common.h b/tools/testing/selftests/bpf/progs/cpumask_common.h
> index 0cd4aebb97cf..cdb9dc95e9d9 100644
> --- a/tools/testing/selftests/bpf/progs/cpumask_common.h
> +++ b/tools/testing/selftests/bpf/progs/cpumask_common.h
> @@ -55,6 +55,9 @@ void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym
>  u32 bpf_cpumask_any_distribute(const struct cpumask *src) __ksym;
>  u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1, const struct cpumask *src2) __ksym;
>  u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym;
> +int bpf_iter_cpumask_new(struct bpf_iter_cpumask *it, const struct cpumask *mask) __ksym;
> +int *bpf_iter_cpumask_next(struct bpf_iter_cpumask *it) __ksym;
> +void bpf_iter_cpumask_destroy(struct bpf_iter_cpumask *it) __ksym;

let's mark them __weak so they don't conflict with definitions that
will eventually come from vmlinux.h (that applies to all the kfunc
definitions we currently have and we'll need to clean all that up, but
let's not add non-weak kfuncs going forward)

>
>  void bpf_rcu_read_lock(void) __ksym;
>  void bpf_rcu_read_unlock(void) __ksym;

[...]

> diff --git a/tools/testing/selftests/bpf/progs/cpumask_iter_success.c b/tools/testing/selftests/bpf/progs/cpumask_iter_success.c
> new file mode 100644
> index 000000000000..4ce14ef98451
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/cpumask_iter_success.c
> @@ -0,0 +1,126 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/* Copyright (c) 2024 Yafang Shao <laoar.shao@xxxxxxxxx> */
> +
> +#include "vmlinux.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +#include "task_kfunc_common.h"
> +#include "cpumask_common.h"
> +
> +char _license[] SEC("license") = "GPL";
> +
> +extern const struct psi_group_cpu system_group_pcpu __ksym __weak;
> +extern const struct rq runqueues __ksym __weak;
> +
> +int pid;
> +
> +#define READ_PERCPU_DATA(meta, cgrp, mask)                                                     \
> +{                                                                                              \
> +       u32 nr_running = 0, psi_nr_running = 0, nr_cpus = 0;                                    \
> +       struct psi_group_cpu *groupc;                                                           \
> +       struct rq *rq;                                                                          \
> +       int *cpu;                                                                               \
> +                                                                                               \
> +       bpf_for_each(cpumask, cpu, mask) {                                                      \
> +               rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, *cpu);                            \
> +               if (!rq) {                                                                      \
> +                       err += 1;                                                               \
> +                       continue;                                                               \
> +               }                                                                               \
> +               nr_running += rq->nr_running;                                                   \
> +               nr_cpus += 1;                                                                   \
> +                                                                                               \
> +               groupc = (struct psi_group_cpu *)bpf_per_cpu_ptr(&system_group_pcpu, *cpu);     \
> +               if (!groupc) {                                                                  \
> +                       err += 1;                                                               \
> +                       continue;                                                               \
> +               }                                                                               \
> +               psi_nr_running += groupc->tasks[NR_RUNNING];                                    \
> +       }                                                                                       \
> +       BPF_SEQ_PRINTF(meta->seq, "nr_running %u nr_cpus %u psi_running %u\n",                  \
> +                      nr_running, nr_cpus, psi_nr_running);                                    \
> +}
> +

Does this have to be a gigantic macro? Why can't it be just a function?

> +SEC("iter.s/cgroup")
> +int BPF_PROG(test_cpumask_iter_sleepable, struct bpf_iter_meta *meta, struct cgroup *cgrp)
> +{
> +       struct task_struct *p;
> +
> +       /* epilogue */
> +       if (!cgrp)
> +               return 0;
> +
> +       bpf_rcu_read_lock();
> +       p = bpf_task_from_pid(pid);
> +       if (!p) {
> +               bpf_rcu_read_unlock();
> +               return 1;
> +       }
> +
> +       READ_PERCPU_DATA(meta, cgrp, p->cpus_ptr);
> +       bpf_task_release(p);
> +       bpf_rcu_read_unlock();
> +       return 0;
> +}
> +

[...]





[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