mmap_lock.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define CREATE_TRACE_POINTS
  3. #include <trace/events/mmap_lock.h>
  4. #include <linux/mm.h>
  5. #include <linux/cgroup.h>
  6. #include <linux/memcontrol.h>
  7. #include <linux/mmap_lock.h>
  8. #include <linux/mutex.h>
  9. #include <linux/percpu.h>
  10. #include <linux/rcupdate.h>
  11. #include <linux/smp.h>
  12. #include <linux/trace_events.h>
  13. #include <linux/local_lock.h>
  14. EXPORT_TRACEPOINT_SYMBOL(mmap_lock_start_locking);
  15. EXPORT_TRACEPOINT_SYMBOL(mmap_lock_acquire_returned);
  16. EXPORT_TRACEPOINT_SYMBOL(mmap_lock_released);
  17. #ifdef CONFIG_MEMCG
  18. /*
  19. * Our various events all share the same buffer (because we don't want or need
  20. * to allocate a set of buffers *per event type*), so we need to protect against
  21. * concurrent _reg() and _unreg() calls, and count how many _reg() calls have
  22. * been made.
  23. */
  24. static DEFINE_MUTEX(reg_lock);
  25. static int reg_refcount; /* Protected by reg_lock. */
  26. /*
  27. * Size of the buffer for memcg path names. Ignoring stack trace support,
  28. * trace_events_hist.c uses MAX_FILTER_STR_VAL for this, so we also use it.
  29. */
  30. #define MEMCG_PATH_BUF_SIZE MAX_FILTER_STR_VAL
  31. /*
  32. * How many contexts our trace events might be called in: normal, softirq, irq,
  33. * and NMI.
  34. */
  35. #define CONTEXT_COUNT 4
  36. struct memcg_path {
  37. local_lock_t lock;
  38. char __rcu *buf;
  39. local_t buf_idx;
  40. };
  41. static DEFINE_PER_CPU(struct memcg_path, memcg_paths) = {
  42. .lock = INIT_LOCAL_LOCK(lock),
  43. .buf_idx = LOCAL_INIT(0),
  44. };
  45. static char **tmp_bufs;
  46. /* Called with reg_lock held. */
  47. static void free_memcg_path_bufs(void)
  48. {
  49. struct memcg_path *memcg_path;
  50. int cpu;
  51. char **old = tmp_bufs;
  52. for_each_possible_cpu(cpu) {
  53. memcg_path = per_cpu_ptr(&memcg_paths, cpu);
  54. *(old++) = rcu_dereference_protected(memcg_path->buf,
  55. lockdep_is_held(&reg_lock));
  56. rcu_assign_pointer(memcg_path->buf, NULL);
  57. }
  58. /* Wait for inflight memcg_path_buf users to finish. */
  59. synchronize_rcu();
  60. old = tmp_bufs;
  61. for_each_possible_cpu(cpu) {
  62. kfree(*(old++));
  63. }
  64. kfree(tmp_bufs);
  65. tmp_bufs = NULL;
  66. }
  67. int trace_mmap_lock_reg(void)
  68. {
  69. int cpu;
  70. char *new;
  71. mutex_lock(&reg_lock);
  72. /* If the refcount is going 0->1, proceed with allocating buffers. */
  73. if (reg_refcount++)
  74. goto out;
  75. tmp_bufs = kmalloc_array(num_possible_cpus(), sizeof(*tmp_bufs),
  76. GFP_KERNEL);
  77. if (tmp_bufs == NULL)
  78. goto out_fail;
  79. for_each_possible_cpu(cpu) {
  80. new = kmalloc(MEMCG_PATH_BUF_SIZE * CONTEXT_COUNT, GFP_KERNEL);
  81. if (new == NULL)
  82. goto out_fail_free;
  83. rcu_assign_pointer(per_cpu_ptr(&memcg_paths, cpu)->buf, new);
  84. /* Don't need to wait for inflights, they'd have gotten NULL. */
  85. }
  86. out:
  87. mutex_unlock(&reg_lock);
  88. return 0;
  89. out_fail_free:
  90. free_memcg_path_bufs();
  91. out_fail:
  92. /* Since we failed, undo the earlier ref increment. */
  93. --reg_refcount;
  94. mutex_unlock(&reg_lock);
  95. return -ENOMEM;
  96. }
  97. void trace_mmap_lock_unreg(void)
  98. {
  99. mutex_lock(&reg_lock);
  100. /* If the refcount is going 1->0, proceed with freeing buffers. */
  101. if (--reg_refcount)
  102. goto out;
  103. free_memcg_path_bufs();
  104. out:
  105. mutex_unlock(&reg_lock);
  106. }
  107. static inline char *get_memcg_path_buf(void)
  108. {
  109. struct memcg_path *memcg_path = this_cpu_ptr(&memcg_paths);
  110. char *buf;
  111. int idx;
  112. rcu_read_lock();
  113. buf = rcu_dereference(memcg_path->buf);
  114. if (buf == NULL) {
  115. rcu_read_unlock();
  116. return NULL;
  117. }
  118. idx = local_add_return(MEMCG_PATH_BUF_SIZE, &memcg_path->buf_idx) -
  119. MEMCG_PATH_BUF_SIZE;
  120. return &buf[idx];
  121. }
  122. static inline void put_memcg_path_buf(void)
  123. {
  124. local_sub(MEMCG_PATH_BUF_SIZE, &this_cpu_ptr(&memcg_paths)->buf_idx);
  125. rcu_read_unlock();
  126. }
  127. #define TRACE_MMAP_LOCK_EVENT(type, mm, ...) \
  128. do { \
  129. const char *memcg_path; \
  130. local_lock(&memcg_paths.lock); \
  131. memcg_path = get_mm_memcg_path(mm); \
  132. trace_mmap_lock_##type(mm, \
  133. memcg_path != NULL ? memcg_path : "", \
  134. ##__VA_ARGS__); \
  135. if (likely(memcg_path != NULL)) \
  136. put_memcg_path_buf(); \
  137. local_unlock(&memcg_paths.lock); \
  138. } while (0)
  139. #else /* !CONFIG_MEMCG */
  140. int trace_mmap_lock_reg(void)
  141. {
  142. return 0;
  143. }
  144. void trace_mmap_lock_unreg(void)
  145. {
  146. }
  147. #define TRACE_MMAP_LOCK_EVENT(type, mm, ...) \
  148. trace_mmap_lock_##type(mm, "", ##__VA_ARGS__)
  149. #endif /* CONFIG_MEMCG */
  150. #ifdef CONFIG_TRACING
  151. #ifdef CONFIG_MEMCG
  152. /*
  153. * Write the given mm_struct's memcg path to a percpu buffer, and return a
  154. * pointer to it. If the path cannot be determined, or no buffer was available
  155. * (because the trace event is being unregistered), NULL is returned.
  156. *
  157. * Note: buffers are allocated per-cpu to avoid locking, so preemption must be
  158. * disabled by the caller before calling us, and re-enabled only after the
  159. * caller is done with the pointer.
  160. *
  161. * The caller must call put_memcg_path_buf() once the buffer is no longer
  162. * needed. This must be done while preemption is still disabled.
  163. */
  164. static const char *get_mm_memcg_path(struct mm_struct *mm)
  165. {
  166. char *buf = NULL;
  167. struct mem_cgroup *memcg = get_mem_cgroup_from_mm(mm);
  168. if (memcg == NULL)
  169. goto out;
  170. if (unlikely(memcg->css.cgroup == NULL))
  171. goto out_put;
  172. buf = get_memcg_path_buf();
  173. if (buf == NULL)
  174. goto out_put;
  175. cgroup_path(memcg->css.cgroup, buf, MEMCG_PATH_BUF_SIZE);
  176. out_put:
  177. css_put(&memcg->css);
  178. out:
  179. return buf;
  180. }
  181. #endif /* CONFIG_MEMCG */
  182. /*
  183. * Trace calls must be in a separate file, as otherwise there's a circular
  184. * dependency between linux/mmap_lock.h and trace/events/mmap_lock.h.
  185. */
  186. void __mmap_lock_do_trace_start_locking(struct mm_struct *mm, bool write)
  187. {
  188. TRACE_MMAP_LOCK_EVENT(start_locking, mm, write);
  189. }
  190. EXPORT_SYMBOL(__mmap_lock_do_trace_start_locking);
  191. void __mmap_lock_do_trace_acquire_returned(struct mm_struct *mm, bool write,
  192. bool success)
  193. {
  194. TRACE_MMAP_LOCK_EVENT(acquire_returned, mm, write, success);
  195. }
  196. EXPORT_SYMBOL(__mmap_lock_do_trace_acquire_returned);
  197. void __mmap_lock_do_trace_released(struct mm_struct *mm, bool write)
  198. {
  199. TRACE_MMAP_LOCK_EVENT(released, mm, write);
  200. }
  201. EXPORT_SYMBOL(__mmap_lock_do_trace_released);
  202. #endif /* CONFIG_TRACING */