perf lock contention: Allow 'mmap_lock' in -L/--lock-filter
Namhyung Kim
maintainerPATCH v2 1/1 perf lock contention: Allow 'mmap_lock' in -L/--lock-filter
The -L/--lock-filter option is to specify target locks by name or address. It's basically for global locks where name or address is known and fixed. But 'mmap_lock' is a per-process lock so it cannot be used for the -L option.
$ sudo perf lock con -ab -L mmap_lock ignore unknown symbol: mmap_lock libbpf: map 'addr_filter': failed to create: -EINVAL libbpf: failed to load BPF skeleton 'lock_contention_bpf': -EINVAL Failed to load lock-contention BPF skeleton lock contention BPF setup failed
However, it's still a common source of contention especially in a large process so we want to use it for the -L/--lock-filter option. As there is check_lock_type() to check mmap_lock at runtime, let's used it to filter mmap_locks as a special case.
Of course, this only works with -b/--use-bpf option.
$ sudo perf lock con -b -L mmap_lock -- perf bench mem mmap -f demand -t 2 # Running 'mem/mmap' benchmark: # function 'demand' (Demand loaded mmap()) # Copying 1MB bytes ...
2.679184 GB/sec/thread ( +- 1.78% )
contended total wait max wait avg wait type caller
1 15.22 us 15.22 us 15.22 us rwsem:W __vm_munmap+0x7e
1 7.72 us 7.72 us 7.72 us rwsem:R lock_mm_and_find_vma+0x97---
v2)
* handle a bad lock name for -L option (Sashiko)
* handle mmap_lock and other lock together (Sashiko)
tools/perf/tests/shell/lock_contention.sh | 11 +++++++++ tools/perf/util/bpf_lock_contention.c | 9 +++++++- .../perf/util/bpf_skel/lock_contention.bpf.c | 23 +++++++++++++++++-- 3 files changed, 40 insertions(+), 3 deletions(-)
tools/perf/tests/shell/lock_contention.sh +11 -0
@@ -208,6 +208,17 @@ test_lock_filter() err=1 exit fi + + perf lock con -b -L mmap_lock -q -- perf bench mem mmap -t 2 -l 10 > /dev/null 2> ${result} + + # find out the type of mmap_lock + test_lock_filter_type=$(head -1 "${result}" | awk '{ print $8 }' | sed -e 's/:.*//') + + if [ "$(grep -c -v "${test_lock_filter_type}" "${result}")" != "0" ]; then + echo "[Fail] BPF result should not have non-${test_lock_filter_type} locks:" "$(cat "${result}")" + err=1 + exit + fi } test_stack_filter()
tools/perf/util/bpf_lock_contention.c +8 -1
@@ -186,6 +186,7 @@ int lock_contention_prepare(struct lock_contention *con)int ncpus =1 , ntasks =1 , ntypes =1 , naddrs =1 , ncgrps =1 , nslabs =1 ;struct evlist *evlist = con->evlist;struct target *target = con->target; +bool has_mmap_lock = false;/* make sure it loads the kernel map before lookup */ map__load (machine__kernel_map (con->machine));
@@ -244,6 +245,11 @@ int lock_contention_prepare(struct lock_contention *con)unsigned long *addrs;for (i =0 ; i < con->filters->nr_syms; i++) { +if (!strcmp (con->filters->syms[i],"mmap_lock" )) { + has_mmap_lock = true; +continue ; + } + sym =machine__find_kernel_symbol_by_name (con->machine, con->filters->syms[i], &kmap);
@@ -263,7 +269,7 @@ int lock_contention_prepare(struct lock_contention *con) addrs[con->filters->nr_addrs++] =map__unmap_ip (kmap, sym->start); con->filters->addrs = addrs; } - naddrs = con->filters->nr_addrs; + naddrs = con->filters->nr_addrs ?: has_mmap_lock; skel->rodata->has_addr =1 ; }
@@ -298,6 +304,7 @@ int lock_contention_prepare(struct lock_contention *con) skel->rodata->aggr_mode = con->aggr_mode; skel->rodata->needs_callstack = con->save_callstack; skel->rodata->lock_owner = con->owner; + skel->rodata->has_mmap_lock = has_mmap_lock;if (con->aggr_mode ==LOCK_AGGR_CGROUP || con->filters->nr_cgrps) {if (cgroup_is_v2 ("perf_event" ))
tools/perf/util/bpf_skel/lock_contention.bpf.c +21 -2
@@ -184,6 +184,7 @@ const volatile int has_type;const volatile int has_addr;const volatile int has_cgroup;const volatile int has_slab; +const volatile int has_mmap_lock;const volatile int needs_callstack;const volatile int stack_skip;const volatile int lock_owner;
@@ -214,6 +215,8 @@ int data_map_full;struct task_struct *bpf_task_from_pid (s32 pid) __ksym__weak ; void bpf_task_release(struct task_struct *p) __ksym __weak; +static inline __u32 check_lock_type (__u64 lock,__u32 flags); +static inline __u64 get_current_cgroup_id (void ) {struct task_struct *task;
@@ -239,6 +242,8 @@ static inline __u64 get_current_cgroup_id(void)static inline int can_record (u64 *ctx) { +bool is_addr_ok = false; +if (has_cpu) {__u32 cpu =bpf_get_smp_processor_id ();__u8 *ok;
@@ -271,8 +276,10 @@ static inline int can_record(u64 *ctx)__u64 addr = ctx[0 ]; ok =bpf_map_lookup_elem (&addr_filter, &addr); -if (!ok && !has_slab) +if (!ok && !has_slab && !has_mmap_lock)return 0 ; + + is_addr_ok = !!ok; }if (has_cgroup) {
@@ -284,6 +291,10 @@ static inline int can_record(u64 *ctx)return 0 ; } +if (is_addr_ok) +return 1 ; + +/* slab and mmap_lock are part of the addr_filter */ if (has_slab && bpf_get_kmem_cache) {__u8 *ok;__u64 addr = ctx[0 ];
@@ -291,7 +302,15 @@ static inline int can_record(u64 *ctx) kmem_cache_addr = (long )bpf_get_kmem_cache (addr); ok =bpf_map_lookup_elem (&slab_filter, &kmem_cache_addr); -if (!ok) +if (!ok && !has_mmap_lock) +return 0 ; + } + +if (has_mmap_lock) { +__u64 lock = ctx[0 ]; +__u32 flag = ctx[1 ]; + +if (check_lock_type (lock, flag) !=LCD_F_MMAP_LOCK )return 0 ; }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Combining a slab cache filter with the 'mmap_lock' filter silently drops all valid slab lock…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Combining a slab cache filter with the 'mmap_lock' filter silently drops all valid slab lock events. --
commit b47198e2987a248476bc916b47bb7722966773ea
Author: Namhyung Kim <[email protected]>
perf lock contention: Allow 'mmap_lock' in -L/--lock-filter
This commit allows filtering for the per-process mmap_lock using the -L/--lock-filter option in perf lock contention by adding a special case to check the lock type via BPF.
Quoted 4 lines
tools/perf/util/bpf_skel/lock_contention.bpf.c +0
-0
[ ... ]
Quoted 6 lines
tools/perf/util/bpf_skel/lock_contention.bpf.c +1
-1@@ -291,7 +302,15 @@ static inline int can_record(u64 *ctx) kmem_cache_addr = (long )bpf_get_kmem_cache (addr); ok =bpf_map_lookup_elem (&slab_filter, &kmem_cache_addr); -if (!ok) +if (!ok && !has_mmap_lock)
tools/perf/util/bpf_skel/lock_contention.bpf.c static inline int can_record(u64 *ctx) · line 315
Quoted 10 lines
tools/perf/util/bpf_skel/lock_contention.bpf.c +8
-0+return 0 ; + } + +if (has_mmap_lock) { +__u64 lock = ctx[0 ]; +__u32 flag = ctx[1 ]; + +if (check_lock_type (lock, flag) !=LCD_F_MMAP_LOCK )return 0 ; }
If a user specifies both a slab cache filter and the mmap_lock filter, will valid slab lock events be silently dropped here?
When a lock event successfully matches the slab_filter, ok is non-null. However, there is no early return like there is for is_addr_ok above.
Execution falls through to the has_mmap_lock block. Since a slab lock is not an mmap_lock, check_lock_type() will not equal LCD_F_MMAP_LOCK, and the function returns 0, filtering out the valid slab event.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Arnaldo Carvalho de Melo
maintainerQuoted 11 lines · Namhyung Kim
The -L/--lock-filter option is to specify target locks by name or address. It's basically for global locks where name or address is known and fixed. But '
mmap_lock' is a per-process lock so it cannot be used for the -L option.$ sudo perf lock con -ab -L mmap_lock ignore unknown symbol: mmap_lock libbpf: map 'addr_filter': failed to create: -EINVAL libbpf: failed to load BPF skeleton 'lock_contention_bpf': -EINVAL Failed to load lock-contention BPF skeleton lock contention BPF setup failed
Hi,
Did this fell thru the cracks? :-)
- Arnaldo
Quoted 164 lines
However, it's still a common source of contention especially in a large process so we want to use it for the -L/--lock-filter option. As there is
check_lock_type()to checkmmap_lockat runtime, let's used it to filtermmap_locksas a special case.Of course, this only works with -b/--use-bpf option.
$ sudo perf lock con -b -L mmap_lock -- perf bench mem mmap -f demand -t 2 # Running 'mem/mmap' benchmark: # function 'demand' (Demand loaded mmap()) # Copying 1MB bytes ...2.679184 GB/sec/thread ( +- 1.78% )contended total wait max wait avg wait type caller
1 15.22 us 15.22 us 15.22 us rwsem:W __vm_munmap+0x7e 1 7.72 us 7.72 us 7.72 us rwsem:R lock_mm_and_find_vma+0x97Signed-off-by Namhyung Kim <[email protected]>---
v2)
* handle a bad lock name for -L option (Sashiko)
* handlemmap_lockand other lock together (Sashiko)tools/perf/tests/shell/lock_contention.sh | 11 +++++++++ tools/perf/util/bpf_lock_contention.c | 9 +++++++- .../perf/util/bpf_skel/lock_contention.bpf.c | 23 +++++++++++++++++-- 3 files changed, 40 insertions(+), 3 deletions(-)tools/perf/tests/shell/lock_contention.sh +11
-0@@ -208,6 +208,17 @@ test_lock_filter() err=1 exit fi + + perf lock con -b -L mmap_lock -q -- perf bench mem mmap -t 2 -l 10 > /dev/null 2> ${result} + + # find out the type of mmap_lock + test_lock_filter_type=$(head -1 "${result}" | awk '{ print $8 }' | sed -e 's/:.*//') + + if [ "$(grep -c -v "${test_lock_filter_type}" "${result}")" != "0" ]; then + echo "[Fail] BPF result should not have non-${test_lock_filter_type} locks:" "$(cat "${result}")" + err=1 + exit + fi } test_stack_filter()tools/perf/util/bpf_lock_contention.c +8
-1@@ -186,6 +186,7 @@ int lock_contention_prepare(struct lock_contention *con)int ncpus =1 , ntasks =1 , ntypes =1 , naddrs =1 , ncgrps =1 , nslabs =1 ;struct evlist *evlist = con->evlist;struct target *target = con->target; +bool has_mmap_lock = false;/* make sure it loads the kernel map before lookup */ map__load (machine__kernel_map (con->machine));@@ -244,6 +245,11 @@ int lock_contention_prepare(struct lock_contention *con)unsigned long *addrs;for (i =0 ; i < con->filters->nr_syms; i++) { +if (!strcmp (con->filters->syms[i],"mmap_lock" )) { + has_mmap_lock = true; +continue ; + } + sym =machine__find_kernel_symbol_by_name (con->machine, con->filters->syms[i], &kmap);@@ -263,7 +269,7 @@ int lock_contention_prepare(struct lock_contention *con) addrs[con->filters->nr_addrs++] =map__unmap_ip (kmap, sym->start); con->filters->addrs = addrs; } - naddrs = con->filters->nr_addrs; + naddrs = con->filters->nr_addrs ?: has_mmap_lock; skel->rodata->has_addr =1 ; }@@ -298,6 +304,7 @@ int lock_contention_prepare(struct lock_contention *con) skel->rodata->aggr_mode = con->aggr_mode; skel->rodata->needs_callstack = con->save_callstack; skel->rodata->lock_owner = con->owner; + skel->rodata->has_mmap_lock = has_mmap_lock;if (con->aggr_mode ==LOCK_AGGR_CGROUP || con->filters->nr_cgrps) {if (cgroup_is_v2 ("perf_event" ))tools/perf/util/bpf_skel/lock_contention.bpf.c +21
-2@@ -184,6 +184,7 @@ const volatile int has_type;const volatile int has_addr;const volatile int has_cgroup;const volatile int has_slab; +const volatile int has_mmap_lock;const volatile int needs_callstack;const volatile int stack_skip;const volatile int lock_owner;@@ -214,6 +215,8 @@ int data_map_full;struct task_struct *bpf_task_from_pid (s32 pid) __ksym__weak ; void bpf_task_release(struct task_struct *p) __ksym __weak; +static inline __u32 check_lock_type (__u64 lock,__u32 flags); +static inline __u64 get_current_cgroup_id (void ) {struct task_struct *task;@@ -239,6 +242,8 @@ static inline __u64 get_current_cgroup_id(void)static inline int can_record (u64 *ctx) { +bool is_addr_ok = false; +if (has_cpu) {__u32 cpu =bpf_get_smp_processor_id ();__u8 *ok;@@ -271,8 +276,10 @@ static inline int can_record(u64 *ctx)__u64 addr = ctx[0 ]; ok =bpf_map_lookup_elem (&addr_filter, &addr); -if (!ok && !has_slab) +if (!ok && !has_slab && !has_mmap_lock)return 0 ; + + is_addr_ok = !!ok; }if (has_cgroup) {@@ -284,6 +291,10 @@ static inline int can_record(u64 *ctx)return 0 ; } +if (is_addr_ok) +return 1 ; + +/* slab and mmap_lock are part of the addr_filter */ if (has_slab && bpf_get_kmem_cache) {__u8 *ok;__u64 addr = ctx[0 ];@@ -291,7 +302,15 @@ static inline int can_record(u64 *ctx) kmem_cache_addr = (long )bpf_get_kmem_cache (addr); ok =bpf_map_lookup_elem (&slab_filter, &kmem_cache_addr); -if (!ok) +if (!ok && !has_mmap_lock) +return 0 ; + } + +if (has_mmap_lock) { +__u64 lock = ctx[0 ]; +__u32 flag = ctx[1 ]; + +if (check_lock_type (lock, flag) !=LCD_F_MMAP_LOCK )return 0 ; }Signature · 1 line
2.54.0.545.g6539524ca2-goog
Namhyung Kim
maintainerQuoted 13 lines · Arnaldo Carvalho de Melo
Quoted 11 lines · Namhyung Kim
The -L/--lock-filter option is to specify target locks by name or address. It's basically for global locks where name or address is known and fixed. But '
mmap_lock' is a per-process lock so it cannot be used for the -L option.$ sudo perf lock con -ab -L mmap_lock ignore unknown symbol: mmap_lock libbpf: map 'addr_filter': failed to create: -EINVAL libbpf: failed to load BPF skeleton 'lock_contention_bpf': -EINVAL Failed to load lock-contention BPF skeleton lock contention BPF setup failed
Quoted 3 lines
Hi,
Did this fell thru the cracks? :-)
Will check and send v3 soon.
Thanks,
Namhyung