From patchwork Wed Oct 7 18:44:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 11821593 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6A730109B for ; Wed, 7 Oct 2020 18:44:17 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EFA2021775 for ; Wed, 7 Oct 2020 18:44:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="u7KStS15" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EFA2021775 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id F3C226B006C; Wed, 7 Oct 2020 14:44:15 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E9D0E8E0001; Wed, 7 Oct 2020 14:44:15 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3B896B006E; Wed, 7 Oct 2020 14:44:15 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0066.hostedemail.com [216.40.44.66]) by kanga.kvack.org (Postfix) with ESMTP id A0F0A6B0068 for ; Wed, 7 Oct 2020 14:44:15 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 3C6128249980 for ; Wed, 7 Oct 2020 18:44:15 +0000 (UTC) X-FDA: 77346004470.10.floor15_120171f271d1 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin10.hostedemail.com (Postfix) with ESMTP id 08CE316A4AB for ; Wed, 7 Oct 2020 18:44:15 +0000 (UTC) X-Spam-Summary: 1,0,0,3e93dc4e417ded0b,d41d8cd98f00b204,3fqx-xw0kcjauhy5buc6eccy708805y.w86527eh-664fuw4.8b0@flex--axelrasmussen.bounces.google.com,,RULES_HIT:4:41:152:355:379:541:800:960:973:988:989:1260:1277:1313:1314:1345:1359:1431:1434:1437:1516:1518:1593:1594:1605:1730:1747:1777:1792:1801:2393:2559:2562:2639:2693:2892:2897:3138:3139:3140:3141:3142:3152:3865:3866:3867:3868:3870:3871:3872:4250:4321:4605:5007:6119:6261:6653:7903:9149:9969:10004:10946:11026:11473:11658:11914:12043:12048:12291:12296:12297:12438:12555:12683:12895:12986:13137:13150:13161:13229:13230:13231:14096:14097:14394:14659:21080:21444:21451:21627:21740:21966:21987:21990:30012:30029:30054:30055:30070:30075,0,RBL:209.85.214.201:@flex--axelrasmussen.bounces.google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100;04yrea8ms34hye7pd6r86rygd9j3cyct6x4ms4f91hig18fqm38iu6bbbn3qsdr.kp1n8ee8q3qnba5q7fgq18hc6gxyp4he4816wi6b14cebgi1pww1rm6cohn44u7.g-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0. 5,Netche X-HE-Tag: floor15_120171f271d1 X-Filterd-Recvd-Size: 16261 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Wed, 7 Oct 2020 18:44:14 +0000 (UTC) Received: by mail-pl1-f201.google.com with SMTP id b5so1731878plk.2 for ; Wed, 07 Oct 2020 11:44:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=roCb7/YhJ1J9g1aL6y8hue0669Lq+IzoGlC5brUIAWQ=; b=u7KStS15kIm+Heu8DVgCSUWwiOwbb1arGa4w4IVRbQx1Znu6WEW3O9tUx7qGoTPKsy rQ0LX8s5CLEX2ZsLPnHGVIIQlU/sF5B19ZotJU7r2DTWDYqgactGeZZd/K6+Ed4qiLc2 WGLOzuZqJDj1UISpOoIDsZS6HT1SmNgyaAhKX4VIPnISX00fTySNm2RMueJks7aIZXIq tTaavH1ABlJbDBnIEbbegVyasP+rogJ2YLaD9Y3AtnghZXgpUfpE2gHm5iTVDFB/VF0M WJJq9vlZ2kaIsSxvtqCvP1Tg30yKnauOo07kUvSG7yBg/vsKBJLWBjP0DKKM8X7C0OP9 jzEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=roCb7/YhJ1J9g1aL6y8hue0669Lq+IzoGlC5brUIAWQ=; b=FlgpZcUGK9yz/dDE4ofOr4rRlG15K9xKqRVwP/hN8UbhdQK+IW/OJGlVIsgGvIPbB2 /MyoHBt0rk/vC+/tMWMkTTqTChfKEiFrvZie7vmwncm8Wv6wththy1zJgIF1sRkI+jQa ZjuzLr2vRgDndhkoiC3vaahftypxtK3MXi70Sqq6K+T3u3DdGzfhZXls0AbK2Gy2RfEd cM8xkw+EMxLpNH2sSd+vpSVuxvHjzJxufiDrXM/xKEKg/qTi6Zt4pJUprzPs6kc+aXsL okXsaMakTPdr0CmLKo0aJYHYXqebdKsacrjrxooVxz7oi4UNSuLJPIt8GMbjOQc8F+XM hiBw== X-Gm-Message-State: AOAM5327R+BDUMDwoZ7uNPUDX/9v/gcuBw55p/HjXFD52wHMTBXE35sN vwX3VA+K9TsK6e1a+xmSNWVrvkWXeoXE9GaICd5P X-Google-Smtp-Source: ABdhPJx5aKBy8JW83gwhazdhKNAj7El6GejmjweU2s6gx5eGR1ZXqaBm2/WEVsZ0e4q78ESgpWbIDTzGPIpKAvVEh/iz X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:f693:9fff:feef:c8f8]) (user=axelrasmussen job=sendgmr) by 2002:a63:2547:: with SMTP id l68mr4078696pgl.241.1602096253163; Wed, 07 Oct 2020 11:44:13 -0700 (PDT) Date: Wed, 7 Oct 2020 11:44:03 -0700 In-Reply-To: <20201007184403.1902111-1-axelrasmussen@google.com> Message-Id: <20201007184403.1902111-3-axelrasmussen@google.com> Mime-Version: 1.0 References: <20201007184403.1902111-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.28.0.806.g8561365e88-goog Subject: [PATCH v2 2/2] mmap_lock: add tracepoints around lock acquisition From: Axel Rasmussen To: Steven Rostedt , Ingo Molnar , Andrew Morton , Michel Lespinasse , Vlastimil Babka , Daniel Jordan , Laurent Dufour , Axel Rasmussen , Jann Horn , Chinwen Chang Cc: Yafang Shao , linux-kernel@vger.kernel.org, linux-mm@kvack.org X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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. Signed-off-by: Axel Rasmussen --- include/linux/mmap_lock.h | 112 ++++++++++++++++++++++++++++--- include/trace/events/mmap_lock.h | 70 +++++++++++++++++++ mm/Makefile | 2 +- mm/mmap_lock.c | 87 ++++++++++++++++++++++++ 4 files changed, 261 insertions(+), 10 deletions(-) create mode 100644 include/trace/events/mmap_lock.h create mode 100644 mm/mmap_lock.c diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h index e1afb420fc94..aa3d64f99d17 100644 --- a/include/linux/mmap_lock.h +++ b/include/linux/mmap_lock.h @@ -1,11 +1,95 @@ #ifndef _LINUX_MMAP_LOCK_H #define _LINUX_MMAP_LOCK_H +#include +#include #include +#include +#include +#include #define MMAP_LOCK_INITIALIZER(name) \ .mmap_lock = __RWSEM_INITIALIZER((name).mmap_lock), +DECLARE_TRACEPOINT(mmap_lock_start_locking); +DECLARE_TRACEPOINT(mmap_lock_acquire_returned); +DECLARE_TRACEPOINT(mmap_lock_released); + +#ifdef CONFIG_TRACING + +void __mmap_lock_do_trace_start_locking(struct mm_struct *mm, bool write); +void __mmap_lock_do_trace_acquire_returned(struct mm_struct *mm, bool write, + bool success); +void __mmap_lock_do_trace_released(struct mm_struct *mm, bool write); + +#else /* !COFNIG_TRACING */ + +static inline void __mmap_lock_do_trace_start_locking(struct mm_struct *mm, + bool write) +{ +} + +static inline void __mmap_lock_do_trace_acquire_returned(struct mm_struct *mm, + bool write, + bool success) +{ +} + +static inline void __mmap_lock_do_trace_released(struct mm_struct *mm, + bool write) +{ +} + +#endif /* CONFIG_TRACING */ + +static inline bool +__mmap_lock_traced_trylock(struct mm_struct *mm, + int (*trylock)(struct rw_semaphore *), bool write) +{ + bool ret; + + if (tracepoint_enabled(mmap_lock_start_locking)) + __mmap_lock_do_trace_start_locking(mm, write); + ret = trylock(&mm->mmap_lock) != 0; + if (tracepoint_enabled(mmap_lock_acquire_returned)) + __mmap_lock_do_trace_acquire_returned(mm, write, ret); + return ret; +} + +static inline void __mmap_lock_traced_lock(struct mm_struct *mm, + void (*lock)(struct rw_semaphore *), + bool write) +{ + if (tracepoint_enabled(mmap_lock_start_locking)) + __mmap_lock_do_trace_start_locking(mm, write); + lock(&mm->mmap_lock); + if (tracepoint_enabled(mmap_lock_acquire_returned)) + __mmap_lock_do_trace_acquire_returned(mm, write, true); +} + +static inline int +__mmap_lock_traced_lock_return(struct mm_struct *mm, + int (*lock)(struct rw_semaphore *), bool write) +{ + int ret; + + if (tracepoint_enabled(mmap_lock_start_locking)) + __mmap_lock_do_trace_start_locking(mm, write); + ret = lock(&mm->mmap_lock); + if (tracepoint_enabled(mmap_lock_acquire_returned)) + __mmap_lock_do_trace_acquire_returned(mm, write, ret == 0); + return ret; +} + +static inline void +__mmap_lock_traced_unlock(struct mm_struct *mm, + void (*unlock)(struct rw_semaphore *), bool write) +{ + unlock(&mm->mmap_lock); + if (tracepoint_enabled(mmap_lock_released)) + __mmap_lock_do_trace_released(mm, write); +} + static inline void mmap_init_lock(struct mm_struct *mm) { init_rwsem(&mm->mmap_lock); @@ -13,58 +97,66 @@ static inline void mmap_init_lock(struct mm_struct *mm) static inline void mmap_write_lock(struct mm_struct *mm) { - down_write(&mm->mmap_lock); + __mmap_lock_traced_lock(mm, down_write, true); } static inline void mmap_write_lock_nested(struct mm_struct *mm, int subclass) { + if (tracepoint_enabled(mmap_lock_start_locking)) + __mmap_lock_do_trace_start_locking(mm, true); down_write_nested(&mm->mmap_lock, subclass); + if (tracepoint_enabled(mmap_lock_acquire_returned)) + __mmap_lock_do_trace_acquire_returned(mm, true, true); } static inline int mmap_write_lock_killable(struct mm_struct *mm) { - return down_write_killable(&mm->mmap_lock); + return __mmap_lock_traced_lock_return(mm, down_write_killable, true); } static inline bool mmap_write_trylock(struct mm_struct *mm) { - return down_write_trylock(&mm->mmap_lock) != 0; + return __mmap_lock_traced_trylock(mm, down_write_trylock, true); } static inline void mmap_write_unlock(struct mm_struct *mm) { - up_write(&mm->mmap_lock); + __mmap_lock_traced_unlock(mm, up_write, true); } static inline void mmap_write_downgrade(struct mm_struct *mm) { downgrade_write(&mm->mmap_lock); + if (tracepoint_enabled(mmap_lock_acquire_returned)) + __mmap_lock_do_trace_acquire_returned(mm, false, true); } static inline void mmap_read_lock(struct mm_struct *mm) { - down_read(&mm->mmap_lock); + __mmap_lock_traced_lock(mm, down_read, false); } static inline int mmap_read_lock_killable(struct mm_struct *mm) { - return down_read_killable(&mm->mmap_lock); + return __mmap_lock_traced_lock_return(mm, down_read_killable, false); } static inline bool mmap_read_trylock(struct mm_struct *mm) { - return down_read_trylock(&mm->mmap_lock) != 0; + return __mmap_lock_traced_trylock(mm, down_read_trylock, false); } static inline void mmap_read_unlock(struct mm_struct *mm) { - up_read(&mm->mmap_lock); + __mmap_lock_traced_unlock(mm, up_read, false); } static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm) { - if (down_read_trylock(&mm->mmap_lock)) { + if (mmap_read_trylock(mm)) { rwsem_release(&mm->mmap_lock.dep_map, _RET_IP_); + if (tracepoint_enabled(mmap_lock_released)) + __mmap_lock_do_trace_released(mm, false); return true; } return false; @@ -73,6 +165,8 @@ static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm) static inline void mmap_read_unlock_non_owner(struct mm_struct *mm) { up_read_non_owner(&mm->mmap_lock); + if (tracepoint_enabled(mmap_lock_released)) + __mmap_lock_do_trace_released(mm, false); } static inline void mmap_assert_locked(struct mm_struct *mm) diff --git a/include/trace/events/mmap_lock.h b/include/trace/events/mmap_lock.h new file mode 100644 index 000000000000..ca652b52510e --- /dev/null +++ b/include/trace/events/mmap_lock.h @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM mmap_lock + +#if !defined(_TRACE_MMAP_LOCK_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_MMAP_LOCK_H + +#include +#include + +struct mm_struct; + +DECLARE_EVENT_CLASS( + mmap_lock_template, + + TP_PROTO(struct mm_struct *mm, const char *memcg_path, bool write, + bool success), + + TP_ARGS(mm, memcg_path, write, success), + + TP_STRUCT__entry( + __field(struct mm_struct *, mm) + __string(memcg_path, memcg_path) + __field(bool, write) + __field(bool, success) + ), + + TP_fast_assign( + __entry->mm = mm; + __assign_str(memcg_path, memcg_path); + __entry->write = write; + __entry->success = success; + ), + + TP_printk( + "mm=%p memcg_path=%s write=%s success=%s\n", + __entry->mm, + __get_str(memcg_path), + __entry->write ? "true" : "false", + __entry->success ? "true" : "false") + ); + +DEFINE_EVENT(mmap_lock_template, mmap_lock_start_locking, + + TP_PROTO(struct mm_struct *mm, const char *memcg_path, bool write, + bool success), + + TP_ARGS(mm, memcg_path, write, success) +); + +DEFINE_EVENT(mmap_lock_template, mmap_lock_acquire_returned, + + TP_PROTO(struct mm_struct *mm, const char *memcg_path, bool write, + bool success), + + TP_ARGS(mm, memcg_path, write, success) +); + +DEFINE_EVENT(mmap_lock_template, mmap_lock_released, + + TP_PROTO(struct mm_struct *mm, const char *memcg_path, bool write, + bool success), + + TP_ARGS(mm, memcg_path, write, success) +); + +#endif /* _TRACE_MMAP_LOCK_H */ + +/* This part must be outside protection */ +#include diff --git a/mm/Makefile b/mm/Makefile index 069f216e109e..b6cd2fffa492 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -52,7 +52,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \ mm_init.o percpu.o slab_common.o \ compaction.o vmacache.o \ interval_tree.o list_lru.o workingset.o \ - debug.o gup.o $(mmu-y) + debug.o gup.o mmap_lock.o $(mmu-y) # Give 'page_alloc' its own module-parameter namespace page-alloc-y := page_alloc.o diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c new file mode 100644 index 000000000000..b849287bd12a --- /dev/null +++ b/mm/mmap_lock.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 +#define CREATE_TRACE_POINTS +#include + +#include +#include +#include +#include +#include +#include +#include + +/* + * We have to export these, as drivers use mmap_lock, and our inline functions + * in the header check if the tracepoint is enabled. They can't be GPL, as e.g. + * the nvidia driver is an existing caller of this code. + */ +EXPORT_SYMBOL(__tracepoint_mmap_lock_start_locking); +EXPORT_SYMBOL(__tracepoint_mmap_lock_acquire_returned); +EXPORT_SYMBOL(__tracepoint_mmap_lock_released); + +#ifdef CONFIG_MEMCG + +DEFINE_PER_CPU(char[MAX_FILTER_STR_VAL], trace_memcg_path); + +/* + * Write the given mm_struct's memcg path to a percpu buffer, and return a + * pointer to it. If the path cannot be determined, the buffer will contain the + * empty string. + * + * Note: buffers are allocated per-cpu to avoid locking, so preemption must be + * disabled by the caller before calling us, and re-enabled only after the + * caller is done with the pointer. + */ +static const char *get_mm_memcg_path(struct mm_struct *mm) +{ + struct mem_cgroup *memcg = get_mem_cgroup_from_mm(mm); + + if (memcg != NULL && likely(memcg->css.cgroup != NULL)) { + char *buf = this_cpu_ptr(trace_memcg_path); + + cgroup_path(memcg->css.cgroup, buf, MAX_FILTER_STR_VAL); + return buf; + } + return ""; +} + +#define TRACE_MMAP_LOCK_EVENT(type, mm, ...) \ + do { \ + if (trace_mmap_lock_##type##_enabled()) { \ + get_cpu(); \ + trace_mmap_lock_##type(mm, get_mm_memcg_path(mm), \ + ##__VA_ARGS__); \ + put_cpu(); \ + } \ + } while (0) + +#else /* !CONFIG_MEMCG */ + +#define TRACE_MMAP_LOCK_EVENT(type, mm, ...) \ + trace_mmap_lock_##type(mm, "", ##__VA_ARGS__) + +#endif /* CONFIG_MEMCG */ + +/* + * Trace calls must be in a separate file, as otherwise there's a circular + * dependency between linux/mmap_lock.h and trace/events/mmap_lock.h. + */ + +void __mmap_lock_do_trace_start_locking(struct mm_struct *mm, bool write) +{ + TRACE_MMAP_LOCK_EVENT(start_locking, mm, write, true); +} +EXPORT_SYMBOL(__mmap_lock_do_trace_start_locking); + +void __mmap_lock_do_trace_acquire_returned(struct mm_struct *mm, bool write, + bool success) +{ + TRACE_MMAP_LOCK_EVENT(acquire_returned, mm, write, success); +} +EXPORT_SYMBOL(__mmap_lock_do_trace_acquire_returned); + +void __mmap_lock_do_trace_released(struct mm_struct *mm, bool write) +{ + TRACE_MMAP_LOCK_EVENT(released, mm, write, true); +} +EXPORT_SYMBOL(__mmap_lock_do_trace_released);