bpf: Add kmem_cache iterator and kfunc
Subsystems (7)
v5 cover bpf: Add kmem_cache iterator and kfunc
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.
v5 changes)
* set PTR_UNTRUSTED for return value of bpf_get_kmem_cache() (Alexei)
* add KF_RCU_PROTECTED to bpf_get_kmem_cache(). See below. (Song)
* add WARN_ON_ONCE and comment in kmem_cache_iter_seq_next() (Song)
* change kmem_cache_iter_seq functions not to call BPF on intermediate stop
* add a subtest to compare the kmem cache info with /proc/slabinfo (Alexei)
v4: https://lore.kernel.org/lkml/[email protected]
* 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/[email protected]/
* 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/[email protected]/
* 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/[email protected]/
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.
Actualy I'm not sure about the RCU lock - IIUC it doesn't protect the kmem_cache itself but kmem_cache_destroy() calls some RCU barrier functions, so having RCU read lock would protect the object from going away by kfree_rcu() or something and then kmem_cache. But please correct me if I'm wrong.
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-v5' 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 | 175 ++++++++++++++++++ kernel/bpf/verifier.c | 5 + mm/slab_common.c | 19 ++ .../bpf/prog_tests/kmem_cache_iter.c | 115 ++++++++++++ tools/testing/selftests/bpf/progs/bpf_iter.h | 7 + .../selftests/bpf/progs/kmem_cache_iter.c | 95 ++++++++++ 9 files changed, 419 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: 5bd48a3a14df4b3ee1be0757efcc0f40d4f57b35
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
PATCH v5 1/3 bpf: Add kmem_cache iterator
The new "kmem_cache" iterator will traverse the list of slab caches and call attached BPF programs for each entry. It should check the argument (ctx.s) if it's NULL before using it.
Now the iteration grabs the slab_mutex only if it traverse the list and releases the mutex when it runs the BPF program. The kmem_cache entry is protected by a refcount during the execution.
It includes the internal "mm/slab.h" header to access kmem_cache, slab_caches and slab_mutex. Hope it's ok to mm folks.
--- include/linux/btf_ids.h | 1 + kernel/bpf/Makefile | 1 + kernel/bpf/kmem_cache_iter.c | 175 +++++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 kernel/bpf/kmem_cache_iter.c
include/linux/btf_ids.h +1 -0
@@ -283,5 +283,6 @@ extern u32 btf_tracing_ids[];extern u32 bpf_cgroup_btf_id[];extern u32 bpf_local_storage_map_btf_id[];extern u32 btf_bpf_map_id[]; +extern u32 bpf_kmem_cache_btf_id[];#endif
kernel/bpf/Makefile +1 -0
@@ -52,3 +52,4 @@ obj-$(CONFIG_BPF_PRELOAD) += preload/ obj-$(CONFIG_BPF_SYSCALL) += relo_core.o obj-$(CONFIG_BPF_SYSCALL) += btf_iter.o obj-$(CONFIG_BPF_SYSCALL) += btf_relocate.o +obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o
kernel/bpf/kmem_cache_iter.c new file +175 -0
175 lines not on this page · raw message
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
Re: [PATCH v5 bpf-next 1/3] bpf: Add kmem_cache iterator
Quoted 6 lines · Namhyung Kim
The new "
kmem_cache" iterator will traverse the list of slab caches and call attached BPF programs for each entry. It should check the argument (ctx.s) if it's NULL before using it.Now the iteration grabs the
slab_mutexonly if it traverse the list and
traverses
Quoted 5 lines
releases the mutex when it runs the BPF program. The
kmem_cacheentry is protected by a refcount during the execution.It includes the internal "
mm/slab.h" header to accesskmem_cache,slab_cachesandslab_mutex. Hope it's ok to mm folks.
What was the reason you dropped Vlastimil's and Roman's acks from this patch while keeping them in patch 2 ?
Folks pls Ack again if it looks ok.
I'm ready to apply, but would like the acks first.
Also I'd like to remove the above paragraph from mm/slab.h from the commit log.
It was good to ask during v1, but looks odd at v5.
Re: [PATCH v5 bpf-next 1/3] bpf: Add kmem_cache iterator
Hello Alexei,
Quoted 10 lines · Alexei Starovoitov
Quoted 6 lines · Namhyung Kim
The new "
kmem_cache" iterator will traverse the list of slab caches and call attached BPF programs for each entry. It should check the argument (ctx.s) if it's NULL before using it.Now the iteration grabs the
slab_mutexonly if it traverse the list andtraverses
Quoted 8 lines
Quoted 5 lines
releases the mutex when it runs the BPF program. The
kmem_cacheentry is protected by a refcount during the execution.It includes the internal "
mm/slab.h" header to accesskmem_cache,slab_cachesandslab_mutex. Hope it's ok to mm folks.What was the reason you dropped Vlastimil's and Roman's acks from this patch while keeping them in patch 2 ?
I wanted to make sure the slab maintainers agree with the refcounting and the locking logic changes. But I forgot to add back Vlastimil's Acked for the v4 which is the same in this regard.
Quoted 8 lines
Folks pls Ack again if it looks ok.
I'm ready to apply, but would like the acks first.
Also I'd like to remove the above paragraph from
mm/slab.hfrom the commit log.
It was good to ask during v1, but looks odd at v5.
Sure, feel free to make any changes.
Thanks,
Namhyung
Re: [PATCH v5 bpf-next 1/3] bpf: Add kmem_cache iterator
In reply to Namhyung Kimkernel/bpf/kmem_cache_iter.c line 14
Quoted 5 lines · Namhyung Kim
kernel/bpf/kmem_cache_iter.c new file +4
-0+struct bpf_iter__kmem_cache { + __bpf_md_ptr(struct bpf_iter_meta *, meta); + __bpf_md_ptr(struct kmem_cache *, s); +};
Just noticed this.
Not your fault. You're copy pasting from bpf_iter__*. It looks like tech debt.
Andrii, Song,
do you remember why all iters are using this?__bpf_md_ptr() wrap was necessary in uapi/bpf.h,
but this is kernel iters that go into vmlinux.h
It should be fine to remove them all and
progs wouldn't need to do the ugly dance of:
#define bpf_iter__ksym bpf_iter__ksym___not_used
#include "vmlinux.h"
#undef bpf_iter__ksym
Re: [PATCH v5 bpf-next 1/3] bpf: Add kmem_cache iterator
Quoted 12 lines · Alexei Starovoitov
kernel/bpf/kmem_cache_iter.c line 14
Quoted 5 lines · Namhyung Kim
kernel/bpf/kmem_cache_iter.c new file +4
-0+struct bpf_iter__kmem_cache { + __bpf_md_ptr(struct bpf_iter_meta *, meta); + __bpf_md_ptr(struct kmem_cache *, s); +};Just noticed this.
Not your fault. You're copy pasting from bpf_iter__*. It looks like tech debt.
Quoted 3 lines
Andrii, Song,
do you remember why all iters are using this?
I don't *know*, but I suspect we are doing this because of 32-bit host architecture. BPF-side is always 64-bit, so to make memory layout inside the kernel and in BPF programs compatible we have to do this for pointers, no?
Quoted 8 lines
__bpf_md_ptr()wrap was necessary inuapi/bpf.h,
but this is kernel iters that go intovmlinux.h
It should be fine to remove them all and
progs wouldn't need to do the ugly dance of:#define
bpf_iter__ksymbpf_iter__ksym___not_used
#include "vmlinux.h"
#undefbpf_iter__ksym
I don't think __bpf_md_ptr is why we are doing this ___not_used dance. At some point we probably didn't want to rely on having the very latest vmlinux.h available in BPF selftests, so we chose to define local versions of all relevant context types.
I think we can drop all that ___not_used dance regardless (and remove local definitions in progs/bpf_iter.h).
Re: [PATCH v5 bpf-next 1/3] bpf: Add kmem_cache iterator
Quoted 10 lines · Andrii Nakryiko
Quoted 7 lines · Alexei Starovoitov
kernel/bpf/kmem_cache_iter.c line 14
Quoted 5 lines · Namhyung Kim
kernel/bpf/kmem_cache_iter.c new file +4
-0+struct bpf_iter__kmem_cache { + __bpf_md_ptr(struct bpf_iter_meta *, meta); + __bpf_md_ptr(struct kmem_cache *, s); +};
BTW, do we want/need to define an open-coded iterator version of this, so that this iteration can be done from other BPF programs? Seems like it has to be a sleepable BPF program, but that's probably fine?
Quoted 30 lines
Quoted 8 lines
Just noticed this.
Not your fault. You're copy pasting from bpf_iter__*. It looks like tech debt.Andrii, Song,
do you remember why all iters are using this?
I don't *know*, but I suspect we are doing this because of 32-bit host architecture. BPF-side is always 64-bit, so to make memory layout inside the kernel and in BPF programs compatible we have to do this for pointers, no?
Quoted 8 lines
__bpf_md_ptr()wrap was necessary inuapi/bpf.h,
but this is kernel iters that go intovmlinux.h
It should be fine to remove them all and
progs wouldn't need to do the ugly dance of:#define
bpf_iter__ksymbpf_iter__ksym___not_used
#include "vmlinux.h"
#undefbpf_iter__ksymI don't think
__bpf_md_ptris why we are doing this___not_useddance. At some point we probably didn't want to rely on having the very latestvmlinux.havailable in BPF selftests, so we chose to define local versions of all relevant context types.I think we can drop all that
___not_useddance regardless (and remove local definitions inprogs/bpf_iter.h).
Re: [PATCH v5 bpf-next 1/3] bpf: Add kmem_cache iterator
In reply to Namhyung KimQuoted 10 lines · Namhyung Kim
The new "
kmem_cache" iterator will traverse the list of slab caches and call attached BPF programs for each entry. It should check the argument (ctx.s) if it's NULL before using it.Now the iteration grabs the
slab_mutexonly if it traverse the list and releases the mutex when it runs the BPF program. Thekmem_cacheentry is protected by a refcount during the execution.It includes the internal "
mm/slab.h" header to accesskmem_cache,slab_cachesandslab_mutex. Hope it's ok to mm folks.
Yeah this paragraph can be dropped.
Quoted 1 line
Signed-off-by Namhyung Kim <[email protected]>
PATCH v5 2/3 mm/bpf: Add bpf_get_kmem_cache() kfunc
In reply to Namhyung KimThe bpf_get_kmem_cache() is to get a slab cache information from a virtual address like virt_to_cache(). If the address is a pointer to a slab object, it'd return a valid kmem_cache pointer, otherwise NULL is returned.
It doesn't grab a reference count of the kmem_cache so the caller is responsible to manage the access. The returned point is marked as PTR_UNTRUSTED. And the kfunc has KF_RCU_PROTECTED as the slab object might be protected by RCU.
The intended use case for now is to symbolize locks in slab objects from the lock contention tracepoints.
--- kernel/bpf/helpers.c | 1 + kernel/bpf/verifier.c | 5 +++++ mm/slab_common.c | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+)
kernel/bpf/helpers.c +1 -0
@@ -3090,6 +3090,7 @@ BTF_ID_FLAGS(func, bpf_iter_bits_new, KF_ITER_NEW) BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY) BTF_ID_FLAGS(func, bpf_copy_from_user_str, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_get_kmem_cache, KF_RCU_PROTECTED)BTF_KFUNCS_END (common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/verifier.c +5 -0
@@ -11259,6 +11259,7 @@ enum special_kfunc_type { KF_bpf_preempt_enable, KF_bpf_iter_css_task_new, KF_bpf_session_cookie, + KF_bpf_get_kmem_cache, };BTF_SET_START (special_kfunc_set)
@@ -11324,6 +11325,7 @@ BTF_ID(func, bpf_session_cookie)#else BTF_ID_UNUSED #endif +BTF_ID(func, bpf_get_kmem_cache) static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta) {
@@ -12834,6 +12836,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, regs[BPF_REG_0 ].type =PTR_TO_BTF_ID ; regs[BPF_REG_0 ].btf_id = ptr_type_id; +if (meta.func_id == special_kfunc_list[KF_bpf_get_kmem_cache]) + regs[BPF_REG_0 ].type |=PTR_UNTRUSTED ; +if (is_iter_next_kfunc (&meta)) {struct bpf_reg_state *cur_iter;
mm/slab_common.c +19 -0
@@ -1322,6 +1322,25 @@ size_t ksize(const void *objp) }EXPORT_SYMBOL (ksize); +#ifdef CONFIG_BPF_SYSCALL +#include <linux/btf.h> + +__bpf_kfunc_start_defs (); + +__bpf_kfunc struct kmem_cache *bpf_get_kmem_cache(u64 addr) +{ +struct slab *slab; + +if (!virt_addr_valid (addr)) +return NULL ; + + slab =virt_to_slab ((void *)(long )addr); +return slab ? slab->slab_cache :NULL ; +} + +__bpf_kfunc_end_defs (); +#endif /* CONFIG_BPF_SYSCALL */ +/* Tracepoints definitions. */ EXPORT_TRACEPOINT_SYMBOL (kmalloc);EXPORT_TRACEPOINT_SYMBOL (kmem_cache_alloc);
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
Re: [PATCH v5 bpf-next 2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc
Quoted 10 lines · Namhyung Kim
The
bpf_get_kmem_cache()is to get a slab cache information from a virtual address likevirt_to_cache(). If the address is a pointer to a slab object, it'd return a validkmem_cachepointer, otherwise NULL is returned.It doesn't grab a reference count of the
kmem_cacheso the caller is responsible to manage the access. The returned point is marked asPTR_UNTRUSTED. And the kfunc hasKF_RCU_PROTECTEDas the slab object might be protected by RCU.
...
Quoted 1 line
+BTF_ID_FLAGS(func,
bpf_get_kmem_cache,KF_RCU_PROTECTED)
This flag is unnecessary. PTR_UNTRUSTED can point to absolutely any memory. In this case it likely points to a valid kmem_cache, but
the verifier will guard all accesses with probe_read anyway.
I can remove this flag while applying.
Re: [PATCH v5 bpf-next 2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc
Quoted 12 lines · Alexei Starovoitov
Quoted 10 lines · Namhyung Kim
The
bpf_get_kmem_cache()is to get a slab cache information from a virtual address likevirt_to_cache(). If the address is a pointer to a slab object, it'd return a validkmem_cachepointer, otherwise NULL is returned.It doesn't grab a reference count of the
kmem_cacheso the caller is responsible to manage the access. The returned point is marked asPTR_UNTRUSTED. And the kfunc hasKF_RCU_PROTECTEDas the slab object might be protected by RCU.
Quoted 8 lines
...
Quoted 1 line
+BTF_ID_FLAGS(func,
bpf_get_kmem_cache,KF_RCU_PROTECTED)This flag is unnecessary.
PTR_UNTRUSTEDcan point to absolutely any memory. In this case it likely points to a validkmem_cache, but
the verifier will guard all accesses withprobe_readanyway.I can remove this flag while applying.
Ok, I'd be happy if you would remove it.
Thanks,
Namhyung
Re: [PATCH v5 bpf-next 2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc
Hi Alexei,
Quoted 16 lines · Namhyung Kim
Quoted 15 lines · Alexei Starovoitov
Quoted 10 lines · Namhyung Kim
The
bpf_get_kmem_cache()is to get a slab cache information from a virtual address likevirt_to_cache(). If the address is a pointer to a slab object, it'd return a validkmem_cachepointer, otherwise NULL is returned.It doesn't grab a reference count of the
kmem_cacheso the caller is responsible to manage the access. The returned point is marked asPTR_UNTRUSTED. And the kfunc hasKF_RCU_PROTECTEDas the slab object might be protected by RCU....
Quoted 1 line
+BTF_ID_FLAGS(func,
bpf_get_kmem_cache,KF_RCU_PROTECTED)
Quoted 7 lines
Quoted 5 lines
This flag is unnecessary.
PTR_UNTRUSTEDcan point to absolutely any memory. In this case it likely points to a validkmem_cache, but
the verifier will guard all accesses withprobe_readanyway.I can remove this flag while applying.
Ok, I'd be happy if you would remove it.
You will need to update the bpf_rcu_read_lock/unlock() in the test code (patch 3). I can send v6 with that and Vlastimil's Ack if you want.
Thanks,
Namhyung
Re: [PATCH v5 bpf-next 2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc
Quoted 24 lines · Namhyung Kim
Hi Alexei,
Quoted 20 lines · Namhyung Kim
Quoted 19 lines · Alexei Starovoitov
Quoted 10 lines · Namhyung Kim
The
bpf_get_kmem_cache()is to get a slab cache information from a virtual address likevirt_to_cache(). If the address is a pointer to a slab object, it'd return a validkmem_cachepointer, otherwise NULL is returned.It doesn't grab a reference count of the
kmem_cacheso the caller is responsible to manage the access. The returned point is marked asPTR_UNTRUSTED. And the kfunc hasKF_RCU_PROTECTEDas the slab object might be protected by RCU....
Quoted 1 line
+BTF_ID_FLAGS(func,
bpf_get_kmem_cache,KF_RCU_PROTECTED)This flag is unnecessary.
PTR_UNTRUSTEDcan point to absolutely any memory. In this case it likely points to a validkmem_cache, but
the verifier will guard all accesses withprobe_readanyway.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
I can remove this flag while applying.
Ok, I'd be happy if you would remove it.
You will need to update the bpf_rcu_read_lock/unlock() in the test code (patch 3). I can send v6 with that and Vlastimil's Ack if you want.
Fixed all that while applying.
Could you please follow up with an open-coded iterator version of the same slab iterator ?
So that progs can iterate slabs as a normal for/while loop ?
Re: [PATCH v5 bpf-next 2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc
Quoted 29 lines · Alexei Starovoitov
Quoted 28 lines · Namhyung Kim
Hi Alexei,
Quoted 23 lines · Namhyung Kim
Quoted 12 lines · Alexei Starovoitov
Quoted 10 lines · Namhyung Kim
The
bpf_get_kmem_cache()is to get a slab cache information from a virtual address likevirt_to_cache(). If the address is a pointer to a slab object, it'd return a validkmem_cachepointer, otherwise NULL is returned.It doesn't grab a reference count of the
kmem_cacheso the caller is responsible to manage the access. The returned point is marked asPTR_UNTRUSTED. And the kfunc hasKF_RCU_PROTECTEDas the slab object might be protected by RCU.Quoted 8 lines
...
Quoted 1 line
+BTF_ID_FLAGS(func,
bpf_get_kmem_cache,KF_RCU_PROTECTED)This flag is unnecessary.
PTR_UNTRUSTEDcan point to absolutely any memory. In this case it likely points to a validkmem_cache, but
the verifier will guard all accesses withprobe_readanyway.I can remove this flag while applying.
Ok, I'd be happy if you would remove it.
Quoted 8 lines
Quoted 2 lines
You will need to update the bpf_rcu_read_lock/unlock() in the test code (patch 3). I can send v6 with that and Vlastimil's Ack if you want.
Fixed all that while applying.
Could you please follow up with an open-coded iterator version of the same slab iterator ?
So that progs can iterate slabs as a normal for/while loop ?
I'm not sure I'm following. Do you want a new test program to iterate kmem_caches by reading list pointers manually? How can I grab the slab_mutex then?
Thanks,
Namhyung
Re: [PATCH v5 bpf-next 2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc
Quoted 36 lines · Namhyung Kim
Quoted 34 lines · Alexei Starovoitov
Quoted 24 lines · Namhyung Kim
Hi Alexei,
Quoted 20 lines · Namhyung Kim
Quoted 19 lines · Alexei Starovoitov
Quoted 10 lines · Namhyung Kim
The
bpf_get_kmem_cache()is to get a slab cache information from a virtual address likevirt_to_cache(). If the address is a pointer to a slab object, it'd return a validkmem_cachepointer, otherwise NULL is returned.It doesn't grab a reference count of the
kmem_cacheso the caller is responsible to manage the access. The returned point is marked asPTR_UNTRUSTED. And the kfunc hasKF_RCU_PROTECTEDas the slab object might be protected by RCU....
Quoted 1 line
+BTF_ID_FLAGS(func,
bpf_get_kmem_cache,KF_RCU_PROTECTED)This flag is unnecessary.
PTR_UNTRUSTEDcan point to absolutely any memory. In this case it likely points to a validkmem_cache, but
the verifier will guard all accesses withprobe_readanyway.Quoted 6 lines
Quoted 3 lines
Quoted 1 line
I can remove this flag while applying.
Ok, I'd be happy if you would remove it.
You will need to update the bpf_rcu_read_lock/unlock() in the test code (patch 3). I can send v6 with that and Vlastimil's Ack if you want.
Fixed all that while applying.
Quoted 7 lines
Quoted 3 lines
Could you please follow up with an open-coded iterator version of the same slab iterator ?
So that progs can iterate slabs as a normal for/while loop ?I'm not sure I'm following. Do you want a new test program to iterate
kmem_cachesby reading list pointers manually? How can I grab theslab_mutexthen?
No.
See bpf_iter_task_new/_next/_destroy kfuncs and
commit c68a78ffe2cb ("bpf: Introduce task open coded iterator kfuncs").
Re: [PATCH v5 bpf-next 2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc
Quoted 41 lines · Alexei Starovoitov
Quoted 40 lines · Namhyung Kim
Quoted 37 lines · Alexei Starovoitov
Quoted 24 lines · Namhyung Kim
Hi Alexei,
Quoted 20 lines · Namhyung Kim
Quoted 19 lines · Alexei Starovoitov
Quoted 10 lines · Namhyung Kim
The
bpf_get_kmem_cache()is to get a slab cache information from a virtual address likevirt_to_cache(). If the address is a pointer to a slab object, it'd return a validkmem_cachepointer, otherwise NULL is returned.It doesn't grab a reference count of the
kmem_cacheso the caller is responsible to manage the access. The returned point is marked asPTR_UNTRUSTED. And the kfunc hasKF_RCU_PROTECTEDas the slab object might be protected by RCU....
Quoted 1 line
+BTF_ID_FLAGS(func,
bpf_get_kmem_cache,KF_RCU_PROTECTED)This flag is unnecessary.
PTR_UNTRUSTEDcan point to absolutely any memory. In this case it likely points to a validkmem_cache, but
the verifier will guard all accesses withprobe_readanyway.Quoted 6 lines
Quoted 3 lines
Quoted 1 line
I can remove this flag while applying.
Ok, I'd be happy if you would remove it.
You will need to update the bpf_rcu_read_lock/unlock() in the test code (patch 3). I can send v6 with that and Vlastimil's Ack if you want.
Fixed all that while applying.
Could you please follow up with an open-coded iterator version of the same slab iterator ?
So that progs can iterate slabs as a normal for/while loop ?
Quoted 7 lines
Quoted 3 lines
I'm not sure I'm following. Do you want a new test program to iterate
kmem_cachesby reading list pointers manually? How can I grab theslab_mutexthen?No.
See bpf_iter_task_new/_next/_destroy kfuncs and
commitc68a78ffe2cb("bpf: Introduce task open coded iterator kfuncs").
Oh, ok. Thanks for the pointer, I'll take a look and add the open code version.
Thanks,
Namhyung
PATCH v5 3/3 selftests/bpf: Add a test for kmem_cache_iter
In reply to Namhyung KimThe test traverses all slab caches using the kmem_cache_iter and save the data into slab_result array map. And check if current task's pointer is from "task_struct" slab cache using bpf_get_kmem_cache().
Also compare the result array with /proc/slabinfo if available (when CONFIG_SLUB_DEBUG is on). Note that many of the fields in the slabinfo are transient, so it only compares the name and objsize fields.
--- .../bpf/prog_tests/kmem_cache_iter.c | 115 ++++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_iter.h | 7 ++ .../selftests/bpf/progs/kmem_cache_iter.c | 95 +++++++++++++++ 3 files changed, 217 insertions(+) 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
tools/testing/selftests/bpf/prog_tests/kmem_cache_iter.c new file +115 -0
@@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Google */ + +#include <test_progs.h> +#include <bpf/libbpf.h> +#include <bpf/btf.h> +#include "kmem_cache_iter.skel.h" + +#define SLAB_NAME_MAX 32 + +struct kmem_cache_result { +char name[SLAB_NAME_MAX ]; +long obj_size; +}; + +static void subtest_kmem_cache_iter_check_task_struct (struct kmem_cache_iter *skel) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts, + .flags = 0, /* Run it with the current task */ + ); +int prog_fd =bpf_program__fd (skel->progs.check_task_struct); + +/* Get task_struct and check it if's from a slab cache */ +ASSERT_OK (bpf_prog_test_run_opts (prog_fd, &opts),"prog_test_run" ); + +/* The BPF program should set 'found' variable */ +ASSERT_EQ (skel->bss->task_struct_found,1 ,"task_struct_found" ); +} + +static void subtest_kmem_cache_iter_check_slabinfo (struct kmem_cache_iter *skel) +{ +FILE *fp; +int map_fd; +char name[SLAB_NAME_MAX ]; +unsigned long objsize; +char rest_of_line[1000 ]; +struct kmem_cache_result r; +int seen =0 ; + + fp =fopen ("/proc/slabinfo" ,"r" ); +if (fp ==NULL ) { +/* CONFIG_SLUB_DEBUG is not enabled */ +return ; + } + + map_fd =bpf_map__fd (skel->maps.slab_result); + +/* Ignore first two lines for header */ +fscanf (fp,"slabinfo - version: %*d.%*d\n" ); +fscanf (fp,"# %*s %*s %*s %*s %*s %*s : %[^\n]\n" , rest_of_line); + +/* Compare name and objsize only - others can be changes frequently */ +while (fscanf (fp,"%s %*u %*u %lu %*u %*u : %[^\n]\n" , + name, &objsize, rest_of_line) ==3 ) { +int ret =bpf_map_lookup_elem (map_fd, &seen, &r); + +if (!ASSERT_OK (ret,"kmem_cache_lookup" )) +break ; + +ASSERT_STREQ (r.name, name,"kmem_cache_name" ); +ASSERT_EQ (r.obj_size, objsize,"kmem_cache_objsize" ); + + seen++; + } + +ASSERT_EQ (skel->bss->kmem_cache_seen, seen,"kmem_cache_seen_eq" ); + +fclose (fp); +} + +void test_kmem_cache_iter (void ) +{ +DECLARE_LIBBPF_OPTS (bpf_iter_attach_opts, opts); +struct kmem_cache_iter *skel =NULL ; +union bpf_iter_link_info linfo = {}; +struct bpf_link *link; +char buf[256 ]; +int iter_fd; + + skel =kmem_cache_iter__open_and_load (); +if (!ASSERT_OK_PTR (skel,"kmem_cache_iter__open_and_load" )) +return ; + + opts.link_info = &linfo; + opts.link_info_len =sizeof (linfo); + + link =bpf_program__attach_iter (skel->progs.slab_info_collector, &opts); +if (!ASSERT_OK_PTR (link,"attach_iter" )) + goto destroy; + + iter_fd =bpf_iter_create (bpf_link__fd (link)); +if (!ASSERT_GE (iter_fd,0 ,"iter_create" )) + goto free_link; + +memset (buf,0 ,sizeof (buf)); +while (read (iter_fd, buf,sizeof (buf) >0 )) { +/* Read out all contents */ +printf ("%s" , buf); + } + +/* Next reads should return 0 */ +ASSERT_EQ (read (iter_fd, buf,sizeof (buf)),0 ,"read" ); + +if (test__start_subtest ("check_task_struct" )) +subtest_kmem_cache_iter_check_task_struct (skel); +if (test__start_subtest ("check_slabinfo" )) +subtest_kmem_cache_iter_check_slabinfo (skel); + +close (iter_fd); + +free_link: +bpf_link__destroy (link); +destroy: +kmem_cache_iter__destroy (skel); +}
tools/testing/selftests/bpf/progs/bpf_iter.h +7 -0
@@ -24,6 +24,7 @@#define BTF_F_PTR_RAW BTF_F_PTR_RAW___not_used#define BTF_F_ZERO BTF_F_ZERO___not_used#define bpf_iter__ksym bpf_iter__ksym___not_used +#define bpf_iter__kmem_cache bpf_iter__kmem_cache___not_used#include "vmlinux.h" #undef bpf_iter_meta#undef bpf_iter__bpf_map
@@ -48,6 +49,7 @@#undef BTF_F_PTR_RAW#undef BTF_F_ZERO#undef bpf_iter__ksym +#undef bpf_iter__kmem_cachestruct bpf_iter_meta {struct seq_file *seq;
@@ -165,3 +167,8 @@ struct bpf_iter__ksym {struct bpf_iter_meta *meta;struct kallsym_iter *ksym; }; + +struct bpf_iter__kmem_cache { +struct bpf_iter_meta *meta; +struct kmem_cache *s; +} __attribute__((preserve_access_index));
tools/testing/selftests/bpf/progs/kmem_cache_iter.c new file +95 -0
95 lines not on this page · raw message
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Thu, 10 Oct 2024 16:25:02 -0700 you wrote:
Hello,
I'm proposing a new iterator and a kfunc for the slab memory allocator to get information of each
kmem_cachelike in/proc/slabinfoor/sys/kernel/slabin more flexible way.v5 changes)
[...]
Here is the summary with links:
- [v5,bpf-next,1/3] bpf: Add kmem_cache iterator
https://git.kernel.org/bpf/bpf-next/c/4971266e1595 - [v5,bpf-next,2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc https://git.kernel.org/bpf/bpf-next/c/04b069ff0181 - [v5,bpf-next,3/3] selftests/bpf: Add a test for kmem_cache_iter https://git.kernel.org/bpf/bpf-next/c/73bb7e74d181
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html