mbox series

[v4,bpf-next,0/3] bpf: Add kmem_cache iterator and kfunc

Message ID 20241002180956.1781008-1-namhyung@kernel.org (mailing list archive)
Headers show
Series bpf: Add kmem_cache iterator and kfunc | expand

Message

Namhyung Kim Oct. 2, 2024, 6:09 p.m. UTC
Hello,

I'm proposing a new iterator and a kfunc for the slab memory allocator
to get information of each kmem_cache like in /proc/slabinfo or
/sys/kernel/slab in more flexible way.

v4 changes)

 * skip kmem_cache_destroy() in kmem_cache_iter_seq_stop() if possible  (Vlastimil)
 * fix a bug in the kmem_cache_iter_seq_start() for the last entry
 
v3: https://lore.kernel.org/lkml/20241002065456.1580143-1-namhyung@kernel.org/

 * rework kmem_cache_iter not to hold slab_mutex when running BPF  (Alexei)
 * add virt_addr_valid() check  (Alexei)
 * fix random test failure by running test with the current task  (Hyeonggon)

v2: https://lore.kernel.org/lkml/20240927184133.968283-1-namhyung@kernel.org/

 * rename it to "kmem_cache_iter"
 * fix a build issue
 * add Acked-by's from Roman and Vlastimil (Thanks!)
 * add error codes in the test for debugging

v1: https://lore.kernel.org/lkml/20240925223023.735947-1-namhyung@kernel.org/

My use case is `perf lock contention` tool which shows contended locks
but many of them are not global locks and don't have symbols.  If it
can tranlate the address of the lock in a slab object to the name of
the slab, it'd be much more useful.

I'm not aware of type information in slab yet, but I was told there's
a work to associate BTF ID with it.  It'd be definitely helpful to my
use case.  Probably we need another kfunc to get the start address of
the object or the offset in the object from an address if the type
info is available.  But I want to start with a simple thing first.

The kmem_cache_iter iterates kmem_cache objects under slab_mutex and
will be useful for userspace to prepare some work for specific slabs
like setting up filters in advance.  And the bpf_get_kmem_cache()
kfunc will return a pointer to a slab from the address of a lock.  And
the test code is to read from the iterator and make sure it finds a
slab cache of the task_struct for the current task.

The code is available at 'bpf/slab-iter-v4' branch in
https://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git

Thanks,
Namhyung


Namhyung Kim (3):
  bpf: Add kmem_cache iterator
  mm/bpf: Add bpf_get_kmem_cache() kfunc
  selftests/bpf: Add a test for kmem_cache_iter

 include/linux/btf_ids.h                       |   1 +
 kernel/bpf/Makefile                           |   1 +
 kernel/bpf/helpers.c                          |   1 +
 kernel/bpf/kmem_cache_iter.c                  | 174 ++++++++++++++++++
 mm/slab_common.c                              |  19 ++
 .../bpf/prog_tests/kmem_cache_iter.c          |  64 +++++++
 tools/testing/selftests/bpf/progs/bpf_iter.h  |   7 +
 .../selftests/bpf/progs/kmem_cache_iter.c     |  66 +++++++
 8 files changed, 333 insertions(+)
 create mode 100644 kernel/bpf/kmem_cache_iter.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/kmem_cache_iter.c
 create mode 100644 tools/testing/selftests/bpf/progs/kmem_cache_iter.c


base-commit: 9502a7de5a61bec3bda841a830560c5d6d40ecac

Comments

Daniel Xu Oct. 3, 2024, 1:01 a.m. UTC | #1
Hi Namhyung,

On 10/2/24 17:18, Namhyung Kim wrote:
> Hello,
> 
> On Wed, Oct 2, 2024 at 12:06 PM <bot+bpf-ci@kernel.org> wrote:
>>
>> Dear patch submitter,
>>
>> CI has tested the following submission:
>> Status:     FAILURE
>> Name:       [v4,bpf-next,0/3] bpf: Add kmem_cache iterator and kfunc
>> Patchwork:  https://patchwork.kernel.org/project/netdevbpf/list/?series=894947&state=*
>> Matrix:     https://github.com/kernel-patches/bpf/actions/runs/11149350866
>>
>> Failed jobs:
>> test_progs-aarch64-gcc: https://github.com/kernel-patches/bpf/actions/runs/11149350866/job/30988341564
> 
> I'm not sure if it's because of my change.  It seems to have failed
> on unrelated tests.  Can you please double check?

I ran some queries on the BPF CI dataset (unfortunately not public) and 
I found at least one other instance of this failure [0].

I've also manually triggered a re-run and now it passes.
So it's probably not related to your change.

I'll try to find the right person to debug it further.

Thanks,
Daniel

[0]: 
https://github.com/kernel-patches/bpf/actions/runs/11010557218/job/30573607777
Namhyung Kim Oct. 3, 2024, 5:43 p.m. UTC | #2
Hi Daniel,

On Wed, Oct 2, 2024 at 6:01 PM Daniel Xu <dlxu@meta.com> wrote:
>
> Hi Namhyung,
>
> On 10/2/24 17:18, Namhyung Kim wrote:
> > Hello,
> >
> > On Wed, Oct 2, 2024 at 12:06 PM <bot+bpf-ci@kernel.org> wrote:
> >>
> >> Dear patch submitter,
> >>
> >> CI has tested the following submission:
> >> Status:     FAILURE
> >> Name:       [v4,bpf-next,0/3] bpf: Add kmem_cache iterator and kfunc
> >> Patchwork:  https://patchwork.kernel.org/project/netdevbpf/list/?series=894947&state=*
> >> Matrix:     https://github.com/kernel-patches/bpf/actions/runs/11149350866
> >>
> >> Failed jobs:
> >> test_progs-aarch64-gcc: https://github.com/kernel-patches/bpf/actions/runs/11149350866/job/30988341564
> >
> > I'm not sure if it's because of my change.  It seems to have failed
> > on unrelated tests.  Can you please double check?
>
> I ran some queries on the BPF CI dataset (unfortunately not public) and
> I found at least one other instance of this failure [0].
>
> I've also manually triggered a re-run and now it passes.
> So it's probably not related to your change.
>
> I'll try to find the right person to debug it further.

Thanks a lot for taking care of this!
Namhyung

>
> [0]:
> https://github.com/kernel-patches/bpf/actions/runs/11010557218/job/30573607777
>