1 // SPDX-License-Identifier: GPL-2.0
4 #include "lock-contention.h"
11 #include <linux/hash.h>
12 #include <linux/zalloc.h>
14 #define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
15 #define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
17 struct callstack_filter {
18 struct list_head list;
22 static LIST_HEAD(callstack_filters);
23 struct hlist_head *lockhash_table;
25 int parse_call_stack(const struct option *opt __maybe_unused, const char *str,
26 int unset __maybe_unused)
35 for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
36 struct callstack_filter *entry;
38 entry = malloc(sizeof(*entry) + strlen(tok) + 1);
40 pr_err("Memory allocation failure\n");
45 strcpy(entry->name, tok);
46 list_add_tail(&entry->list, &callstack_filters);
53 bool needs_callstack(void)
55 return !list_empty(&callstack_filters);
58 struct lock_stat *lock_stat_find(u64 addr)
60 struct hlist_head *entry = lockhashentry(addr);
61 struct lock_stat *ret;
63 hlist_for_each_entry(ret, entry, hash_entry) {
64 if (ret->addr == addr)
70 struct lock_stat *lock_stat_findnew(u64 addr, const char *name, int flags)
72 struct hlist_head *entry = lockhashentry(addr);
73 struct lock_stat *ret, *new;
75 hlist_for_each_entry(ret, entry, hash_entry) {
76 if (ret->addr == addr)
80 new = zalloc(sizeof(struct lock_stat));
85 new->name = strdup(name);
92 new->wait_time_min = ULLONG_MAX;
94 hlist_add_head(&new->hash_entry, entry);
98 pr_err("memory allocation failed\n");
102 bool match_callstack_filter(struct machine *machine, u64 *callstack, int max_stack_depth)
107 const char *arch = perf_env__arch(machine->env);
109 if (list_empty(&callstack_filters))
112 for (int i = 0; i < max_stack_depth; i++) {
113 struct callstack_filter *filter;
116 * In powerpc, the callchain saved by kernel always includes
117 * first three entries as the NIP (next instruction pointer),
118 * LR (link register), and the contents of LR save area in the
119 * second stack frame. In certain scenarios its possible to have
120 * invalid kernel instruction addresses in either LR or the second
121 * stack frame's LR. In that case, kernel will store that address as
124 * The below check will continue to look into callstack,
125 * incase first or second callstack index entry has 0
126 * address for powerpc.
128 if (!callstack || (!callstack[i] && (strcmp(arch, "powerpc") ||
129 (i != 1 && i != 2))))
133 sym = machine__find_kernel_symbol(machine, ip, &kmap);
137 list_for_each_entry(filter, &callstack_filters, list) {
138 if (strstr(sym->name, filter->name))