xref: /linux/mm/mmap_lock.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 #define CREATE_TRACE_POINTS
3 #include <trace/events/mmap_lock.h>
4 
5 #include <linux/mm.h>
6 #include <linux/cgroup.h>
7 #include <linux/memcontrol.h>
8 #include <linux/mmap_lock.h>
9 #include <linux/mutex.h>
10 #include <linux/percpu.h>
11 #include <linux/rcupdate.h>
12 #include <linux/smp.h>
13 #include <linux/trace_events.h>
14 #include <linux/local_lock.h>
15 
16 EXPORT_TRACEPOINT_SYMBOL(mmap_lock_start_locking);
17 EXPORT_TRACEPOINT_SYMBOL(mmap_lock_acquire_returned);
18 EXPORT_TRACEPOINT_SYMBOL(mmap_lock_released);
19 
20 #ifdef CONFIG_MEMCG
21 
22 /*
23  * Size of the buffer for memcg path names. Ignoring stack trace support,
24  * trace_events_hist.c uses MAX_FILTER_STR_VAL for this, so we also use it.
25  */
26 #define MEMCG_PATH_BUF_SIZE MAX_FILTER_STR_VAL
27 
28 #define TRACE_MMAP_LOCK_EVENT(type, mm, ...)				\
29 	do {								\
30 		if (trace_mmap_lock_##type##_enabled()) {		\
31 			char buf[MEMCG_PATH_BUF_SIZE];                  \
32 			get_mm_memcg_path(mm, buf, sizeof(buf));        \
33 			trace_mmap_lock_##type(mm, buf, ##__VA_ARGS__); \
34 		}							\
35 	} while (0)
36 
37 #else /* !CONFIG_MEMCG */
38 
39 #define TRACE_MMAP_LOCK_EVENT(type, mm, ...)                                   \
40 	trace_mmap_lock_##type(mm, "", ##__VA_ARGS__)
41 
42 #endif /* CONFIG_MEMCG */
43 
44 #ifdef CONFIG_TRACING
45 #ifdef CONFIG_MEMCG
46 /*
47  * Write the given mm_struct's memcg path to a buffer. If the path cannot be
48  * determined, empty string is written.
49  */
50 static void get_mm_memcg_path(struct mm_struct *mm, char *buf, size_t buflen)
51 {
52 	struct mem_cgroup *memcg;
53 
54 	buf[0] = '\0';
55 	memcg = get_mem_cgroup_from_mm(mm);
56 	if (memcg == NULL)
57 		return;
58 	if (memcg->css.cgroup)
59 		cgroup_path(memcg->css.cgroup, buf, buflen);
60 	css_put(&memcg->css);
61 }
62 
63 #endif /* CONFIG_MEMCG */
64 
65 /*
66  * Trace calls must be in a separate file, as otherwise there's a circular
67  * dependency between linux/mmap_lock.h and trace/events/mmap_lock.h.
68  */
69 
70 void __mmap_lock_do_trace_start_locking(struct mm_struct *mm, bool write)
71 {
72 	TRACE_MMAP_LOCK_EVENT(start_locking, mm, write);
73 }
74 EXPORT_SYMBOL(__mmap_lock_do_trace_start_locking);
75 
76 void __mmap_lock_do_trace_acquire_returned(struct mm_struct *mm, bool write,
77 					   bool success)
78 {
79 	TRACE_MMAP_LOCK_EVENT(acquire_returned, mm, write, success);
80 }
81 EXPORT_SYMBOL(__mmap_lock_do_trace_acquire_returned);
82 
83 void __mmap_lock_do_trace_released(struct mm_struct *mm, bool write)
84 {
85 	TRACE_MMAP_LOCK_EVENT(released, mm, write);
86 }
87 EXPORT_SYMBOL(__mmap_lock_do_trace_released);
88 #endif /* CONFIG_TRACING */
89