]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
perf lock contention: Run BPF slab cache iterator
authorNamhyung Kim <namhyung@kernel.org>
Fri, 20 Dec 2024 06:00:07 +0000 (22:00 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 23 Dec 2024 16:52:03 +0000 (13:52 -0300)
Recently the kernel got the kmem_cache iterator to traverse metadata of
slab objects.  This can be used to symbolize dynamic locks in a slab.

The new slab_caches hash map will have the pointer of the kmem_cache as
a key and save the name and a id.  The id will be saved in the flags
part of the lock.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Ian Rogers <irogers@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Chun-Tse Shao <ctshao@google.com>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Kees Cook <kees@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Song Liu <song@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Link: https://lore.kernel.org/r/20241220060009.507297-3-namhyung@kernel.org
[ Added change from Namhyung addressing review from Alexei: ]
Link: https://lore.kernel.org/r/Z2dVdH3o5iF-KrWj@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/bpf_lock_contention.c
tools/perf/util/bpf_skel/lock_contention.bpf.c
tools/perf/util/bpf_skel/lock_data.h
tools/perf/util/bpf_skel/vmlinux/vmlinux.h

index 37e17c56f1064e60f5bc8676dabd630d437dcfa3..169531d1865264be14e6e7a324ce9cc851512cec 100644 (file)
 #include <linux/zalloc.h>
 #include <linux/string.h>
 #include <bpf/bpf.h>
+#include <bpf/btf.h>
 #include <inttypes.h>
 
 #include "bpf_skel/lock_contention.skel.h"
 #include "bpf_skel/lock_data.h"
 
 static struct lock_contention_bpf *skel;
+static bool has_slab_iter;
+
+static void check_slab_cache_iter(struct lock_contention *con)
+{
+       struct btf *btf = btf__load_vmlinux_btf();
+       s32 ret;
+
+       if (btf == NULL) {
+               pr_debug("BTF loading failed: %s\n", strerror(errno));
+               return;
+       }
+
+       ret = btf__find_by_name_kind(btf, "bpf_iter__kmem_cache", BTF_KIND_STRUCT);
+       if (ret < 0) {
+               bpf_program__set_autoload(skel->progs.slab_cache_iter, false);
+               pr_debug("slab cache iterator is not available: %d\n", ret);
+               goto out;
+       }
+
+       has_slab_iter = true;
+
+       bpf_map__set_max_entries(skel->maps.slab_caches, con->map_nr_entries);
+out:
+       btf__free(btf);
+}
+
+static void run_slab_cache_iter(void)
+{
+       int fd;
+       char buf[256];
+
+       if (!has_slab_iter)
+               return;
+
+       fd = bpf_iter_create(bpf_link__fd(skel->links.slab_cache_iter));
+       if (fd < 0) {
+               pr_debug("cannot create slab cache iter: %d\n", fd);
+               return;
+       }
+
+       /* This will run the bpf program */
+       while (read(fd, buf, sizeof(buf)) > 0)
+               continue;
+
+       close(fd);
+}
 
 int lock_contention_prepare(struct lock_contention *con)
 {
@@ -109,6 +156,8 @@ int lock_contention_prepare(struct lock_contention *con)
                        skel->rodata->use_cgroup_v2 = 1;
        }
 
+       check_slab_cache_iter(con);
+
        if (lock_contention_bpf__load(skel) < 0) {
                pr_err("Failed to load lock-contention BPF skeleton\n");
                return -1;
@@ -304,6 +353,7 @@ next:
 
 int lock_contention_start(void)
 {
+       run_slab_cache_iter();
        skel->bss->enabled = 1;
        return 0;
 }
index 1069bda5d733887fa290b5aba99999a7e47b1f93..364ce10078f88251e03119e1917b75f3cdfc70a2 100644 (file)
@@ -100,6 +100,13 @@ struct {
        __uint(max_entries, 1);
 } cgroup_filter SEC(".maps");
 
+struct {
+       __uint(type, BPF_MAP_TYPE_HASH);
+       __uint(key_size, sizeof(long));
+       __uint(value_size, sizeof(struct slab_cache_data));
+       __uint(max_entries, 1);
+} slab_caches SEC(".maps");
+
 struct rw_semaphore___old {
        struct task_struct *owner;
 } __attribute__((preserve_access_index));
@@ -136,6 +143,8 @@ int perf_subsys_id = -1;
 
 __u64 end_ts;
 
+__u32 slab_cache_id;
+
 /* error stat */
 int task_fail;
 int stack_fail;
@@ -563,4 +572,43 @@ int BPF_PROG(end_timestamp)
        return 0;
 }
 
+/*
+ * bpf_iter__kmem_cache added recently so old kernels don't have it in the
+ * vmlinux.h.  But we cannot add it here since it will cause a compiler error
+ * due to redefinition of the struct on later kernels.
+ *
+ * So it uses a CO-RE trick to access the member only if it has the type.
+ * This will support both old and new kernels without compiler errors.
+ */
+struct bpf_iter__kmem_cache___new {
+       struct kmem_cache *s;
+} __attribute__((preserve_access_index));
+
+SEC("iter/kmem_cache")
+int slab_cache_iter(void *ctx)
+{
+       struct kmem_cache *s = NULL;
+       struct slab_cache_data d;
+       const char *nameptr;
+
+       if (bpf_core_type_exists(struct bpf_iter__kmem_cache)) {
+               struct bpf_iter__kmem_cache___new *iter = ctx;
+
+               s = iter->s;
+       }
+
+       if (s == NULL)
+               return 0;
+
+       nameptr = s->name;
+       bpf_probe_read_kernel_str(d.name, sizeof(d.name), nameptr);
+
+       d.id = ++slab_cache_id << LCB_F_SLAB_ID_SHIFT;
+       if (d.id >= LCB_F_SLAB_ID_END)
+               return 0;
+
+       bpf_map_update_elem(&slab_caches, &s, &d, BPF_NOEXIST);
+       return 0;
+}
+
 char LICENSE[] SEC("license") = "Dual BSD/GPL";
index 4f0aae5483745dfafcef3741a88e383b8650cafc..c15f734d7fc4aecb518eada554ef2330f5bf2abe 100644 (file)
@@ -32,9 +32,16 @@ struct contention_task_data {
 #define LCD_F_MMAP_LOCK                (1U << 31)
 #define LCD_F_SIGHAND_LOCK     (1U << 30)
 
+#define LCB_F_SLAB_ID_SHIFT    16
+#define LCB_F_SLAB_ID_START    (1U << 16)
+#define LCB_F_SLAB_ID_END      (1U << 26)
+#define LCB_F_SLAB_ID_MASK     0x03FF0000U
+
 #define LCB_F_TYPE_MAX         (1U << 7)
 #define LCB_F_TYPE_MASK                0x0000007FU
 
+#define SLAB_NAME_MAX  28
+
 struct contention_data {
        u64 total_time;
        u64 min_time;
@@ -55,4 +62,9 @@ enum lock_class_sym {
        LOCK_CLASS_RQLOCK,
 };
 
+struct slab_cache_data {
+       u32 id;
+       char name[SLAB_NAME_MAX];
+};
+
 #endif /* UTIL_BPF_SKEL_LOCK_DATA_H */
index 4dcad7b682bdee9c0d4f30389a227b97fc2648c2..7b81d3173917fdb558d9acb24b7bb2ef5e4e6565 100644 (file)
@@ -195,4 +195,12 @@ struct bpf_perf_event_data_kern {
  */
 struct rq {};
 
+struct kmem_cache {
+       const char *name;
+} __attribute__((preserve_access_index));
+
+struct bpf_iter__kmem_cache {
+       struct kmem_cache *s;
+} __attribute__((preserve_access_index));
+
 #endif // __VMLINUX_H