Skip to content

Commit 2b5067a

Browse files
CmdrMoozytorvalds
authored andcommitted
mm: mmap_lock: add tracepoints around lock acquisition
The goal of these tracepoints is to be able to debug lock contention issues. This lock is acquired on most (all?) mmap / munmap / page fault operations, so a multi-threaded process which does a lot of these can experience significant contention. We trace just before we start acquisition, when the acquisition returns (whether it succeeded or not), and when the lock is released (or downgraded). The events are broken out by lock type (read / write). The events are also broken out by memcg path. For container-based workloads, users often think of several processes in a memcg as a single logical "task", so collecting statistics at this level is useful. The end goal is to get latency information. This isn't directly included in the trace events. Instead, users are expected to compute the time between "start locking" and "acquire returned", using e.g. synthetic events or BPF. The benefit we get from this is simpler code. Because we use tracepoint_enabled() to decide whether or not to trace, this patch has effectively no overhead unless tracepoints are enabled at runtime. If tracepoints are enabled, there is a performance impact, but how much depends on exactly what e.g. the BPF program does. [axelrasmussen@google.com: fix use-after-free race and css ref leak in tracepoints] Link: https://lkml.kernel.org/r/20201130233504.3725241-1-axelrasmussen@google.com [axelrasmussen@google.com: v3] Link: https://lkml.kernel.org/r/20201207213358.573750-1-axelrasmussen@google.com [rostedt@goodmis.org: in-depth examples of tracepoint_enabled() usage, and per-cpu-per-context buffer design] Link: https://lkml.kernel.org/r/20201105211739.568279-2-axelrasmussen@google.com Signed-off-by: Axel Rasmussen <axelrasmussen@google.com> Acked-by: Vlastimil Babka <vbabka@suse.cz> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Michel Lespinasse <walken@google.com> Cc: Daniel Jordan <daniel.m.jordan@oracle.com> Cc: Jann Horn <jannh@google.com> Cc: Chinwen Chang <chinwen.chang@mediatek.com> Cc: Davidlohr Bueso <dbueso@suse.de> Cc: David Rientjes <rientjes@google.com> Cc: Laurent Dufour <ldufour@linux.ibm.com> Cc: Yafang Shao <laoar.shao@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 777f303 commit 2b5067a

4 files changed

Lines changed: 427 additions & 6 deletions

File tree

‎include/linux/mmap_lock.h‎

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,152 @@
11
#ifndef _LINUX_MMAP_LOCK_H
22
#define _LINUX_MMAP_LOCK_H
33

4+
#include <linux/lockdep.h>
5+
#include <linux/mm_types.h>
46
#include <linux/mmdebug.h>
7+
#include <linux/rwsem.h>
8+
#include <linux/tracepoint-defs.h>
9+
#include <linux/types.h>
510

611
#define MMAP_LOCK_INITIALIZER(name) \
712
.mmap_lock = __RWSEM_INITIALIZER((name).mmap_lock),
813

14+
DECLARE_TRACEPOINT(mmap_lock_start_locking);
15+
DECLARE_TRACEPOINT(mmap_lock_acquire_returned);
16+
DECLARE_TRACEPOINT(mmap_lock_released);
17+
18+
#ifdef CONFIG_TRACING
19+
20+
void __mmap_lock_do_trace_start_locking(struct mm_struct *mm, bool write);
21+
void __mmap_lock_do_trace_acquire_returned(struct mm_struct *mm, bool write,
22+
bool success);
23+
void __mmap_lock_do_trace_released(struct mm_struct *mm, bool write);
24+
25+
static inline void __mmap_lock_trace_start_locking(struct mm_struct *mm,
26+
bool write)
27+
{
28+
if (tracepoint_enabled(mmap_lock_start_locking))
29+
__mmap_lock_do_trace_start_locking(mm, write);
30+
}
31+
32+
static inline void __mmap_lock_trace_acquire_returned(struct mm_struct *mm,
33+
bool write, bool success)
34+
{
35+
if (tracepoint_enabled(mmap_lock_acquire_returned))
36+
__mmap_lock_do_trace_acquire_returned(mm, write, success);
37+
}
38+
39+
static inline void __mmap_lock_trace_released(struct mm_struct *mm, bool write)
40+
{
41+
if (tracepoint_enabled(mmap_lock_released))
42+
__mmap_lock_do_trace_released(mm, write);
43+
}
44+
45+
#else /* !CONFIG_TRACING */
46+
47+
static inline void __mmap_lock_trace_start_locking(struct mm_struct *mm,
48+
bool write)
49+
{
50+
}
51+
52+
static inline void __mmap_lock_trace_acquire_returned(struct mm_struct *mm,
53+
bool write, bool success)
54+
{
55+
}
56+
57+
static inline void __mmap_lock_trace_released(struct mm_struct *mm, bool write)
58+
{
59+
}
60+
61+
#endif /* CONFIG_TRACING */
62+
963
static inline void mmap_init_lock(struct mm_struct *mm)
1064
{
1165
init_rwsem(&mm->mmap_lock);
1266
}
1367

1468
static inline void mmap_write_lock(struct mm_struct *mm)
1569
{
70+
__mmap_lock_trace_start_locking(mm, true);
1671
down_write(&mm->mmap_lock);
72+
__mmap_lock_trace_acquire_returned(mm, true, true);
1773
}
1874

1975
static inline void mmap_write_lock_nested(struct mm_struct *mm, int subclass)
2076
{
77+
__mmap_lock_trace_start_locking(mm, true);
2178
down_write_nested(&mm->mmap_lock, subclass);
79+
__mmap_lock_trace_acquire_returned(mm, true, true);
2280
}
2381

2482
static inline int mmap_write_lock_killable(struct mm_struct *mm)
2583
{
26-
return down_write_killable(&mm->mmap_lock);
84+
int ret;
85+
86+
__mmap_lock_trace_start_locking(mm, true);
87+
ret = down_write_killable(&mm->mmap_lock);
88+
__mmap_lock_trace_acquire_returned(mm, true, ret == 0);
89+
return ret;
2790
}
2891

2992
static inline bool mmap_write_trylock(struct mm_struct *mm)
3093
{
31-
return down_write_trylock(&mm->mmap_lock) != 0;
94+
bool ret;
95+
96+
__mmap_lock_trace_start_locking(mm, true);
97+
ret = down_write_trylock(&mm->mmap_lock) != 0;
98+
__mmap_lock_trace_acquire_returned(mm, true, ret);
99+
return ret;
32100
}
33101

34102
static inline void mmap_write_unlock(struct mm_struct *mm)
35103
{
36104
up_write(&mm->mmap_lock);
105+
__mmap_lock_trace_released(mm, true);
37106
}
38107

39108
static inline void mmap_write_downgrade(struct mm_struct *mm)
40109
{
41110
downgrade_write(&mm->mmap_lock);
111+
__mmap_lock_trace_acquire_returned(mm, false, true);
42112
}
43113

44114
static inline void mmap_read_lock(struct mm_struct *mm)
45115
{
116+
__mmap_lock_trace_start_locking(mm, false);
46117
down_read(&mm->mmap_lock);
118+
__mmap_lock_trace_acquire_returned(mm, false, true);
47119
}
48120

49121
static inline int mmap_read_lock_killable(struct mm_struct *mm)
50122
{
51-
return down_read_killable(&mm->mmap_lock);
123+
int ret;
124+
125+
__mmap_lock_trace_start_locking(mm, false);
126+
ret = down_read_killable(&mm->mmap_lock);
127+
__mmap_lock_trace_acquire_returned(mm, false, ret == 0);
128+
return ret;
52129
}
53130

54131
static inline bool mmap_read_trylock(struct mm_struct *mm)
55132
{
56-
return down_read_trylock(&mm->mmap_lock) != 0;
133+
bool ret;
134+
135+
__mmap_lock_trace_start_locking(mm, false);
136+
ret = down_read_trylock(&mm->mmap_lock) != 0;
137+
__mmap_lock_trace_acquire_returned(mm, false, ret);
138+
return ret;
57139
}
58140

59141
static inline void mmap_read_unlock(struct mm_struct *mm)
60142
{
61143
up_read(&mm->mmap_lock);
144+
__mmap_lock_trace_released(mm, false);
62145
}
63146

64147
static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm)
65148
{
66-
if (down_read_trylock(&mm->mmap_lock)) {
149+
if (mmap_read_trylock(mm)) {
67150
rwsem_release(&mm->mmap_lock.dep_map, _RET_IP_);
68151
return true;
69152
}
@@ -73,6 +156,7 @@ static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm)
73156
static inline void mmap_read_unlock_non_owner(struct mm_struct *mm)
74157
{
75158
up_read_non_owner(&mm->mmap_lock);
159+
__mmap_lock_trace_released(mm, false);
76160
}
77161

78162
static inline void mmap_assert_locked(struct mm_struct *mm)

‎include/trace/events/mmap_lock.h‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#undef TRACE_SYSTEM
3+
#define TRACE_SYSTEM mmap_lock
4+
5+
#if !defined(_TRACE_MMAP_LOCK_H) || defined(TRACE_HEADER_MULTI_READ)
6+
#define _TRACE_MMAP_LOCK_H
7+
8+
#include <linux/tracepoint.h>
9+
#include <linux/types.h>
10+
11+
struct mm_struct;
12+
13+
extern int trace_mmap_lock_reg(void);
14+
extern void trace_mmap_lock_unreg(void);
15+
16+
TRACE_EVENT_FN(mmap_lock_start_locking,
17+
18+
TP_PROTO(struct mm_struct *mm, const char *memcg_path, bool write),
19+
20+
TP_ARGS(mm, memcg_path, write),
21+
22+
TP_STRUCT__entry(
23+
__field(struct mm_struct *, mm)
24+
__string(memcg_path, memcg_path)
25+
__field(bool, write)
26+
),
27+
28+
TP_fast_assign(
29+
__entry->mm = mm;
30+
__assign_str(memcg_path, memcg_path);
31+
__entry->write = write;
32+
),
33+
34+
TP_printk(
35+
"mm=%p memcg_path=%s write=%s\n",
36+
__entry->mm,
37+
__get_str(memcg_path),
38+
__entry->write ? "true" : "false"
39+
),
40+
41+
trace_mmap_lock_reg, trace_mmap_lock_unreg
42+
);
43+
44+
TRACE_EVENT_FN(mmap_lock_acquire_returned,
45+
46+
TP_PROTO(struct mm_struct *mm, const char *memcg_path, bool write,
47+
bool success),
48+
49+
TP_ARGS(mm, memcg_path, write, success),
50+
51+
TP_STRUCT__entry(
52+
__field(struct mm_struct *, mm)
53+
__string(memcg_path, memcg_path)
54+
__field(bool, write)
55+
__field(bool, success)
56+
),
57+
58+
TP_fast_assign(
59+
__entry->mm = mm;
60+
__assign_str(memcg_path, memcg_path);
61+
__entry->write = write;
62+
__entry->success = success;
63+
),
64+
65+
TP_printk(
66+
"mm=%p memcg_path=%s write=%s success=%s\n",
67+
__entry->mm,
68+
__get_str(memcg_path),
69+
__entry->write ? "true" : "false",
70+
__entry->success ? "true" : "false"
71+
),
72+
73+
trace_mmap_lock_reg, trace_mmap_lock_unreg
74+
);
75+
76+
TRACE_EVENT_FN(mmap_lock_released,
77+
78+
TP_PROTO(struct mm_struct *mm, const char *memcg_path, bool write),
79+
80+
TP_ARGS(mm, memcg_path, write),
81+
82+
TP_STRUCT__entry(
83+
__field(struct mm_struct *, mm)
84+
__string(memcg_path, memcg_path)
85+
__field(bool, write)
86+
),
87+
88+
TP_fast_assign(
89+
__entry->mm = mm;
90+
__assign_str(memcg_path, memcg_path);
91+
__entry->write = write;
92+
),
93+
94+
TP_printk(
95+
"mm=%p memcg_path=%s write=%s\n",
96+
__entry->mm,
97+
__get_str(memcg_path),
98+
__entry->write ? "true" : "false"
99+
),
100+
101+
trace_mmap_lock_reg, trace_mmap_lock_unreg
102+
);
103+
104+
#endif /* _TRACE_MMAP_LOCK_H */
105+
106+
/* This part must be outside protection */
107+
#include <trace/define_trace.h>

‎mm/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \
5252
mm_init.o percpu.o slab_common.o \
5353
compaction.o vmacache.o \
5454
interval_tree.o list_lru.o workingset.o \
55-
debug.o gup.o $(mmu-y)
55+
debug.o gup.o mmap_lock.o $(mmu-y)
5656

5757
# Give 'page_alloc' its own module-parameter namespace
5858
page-alloc-y := page_alloc.o

0 commit comments

Comments
 (0)