Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp4566937pxu; Tue, 20 Oct 2020 22:45:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxng/ly7ToBNODLgHl+jbzrK6bntOYwlCb9Ji1Hm1tuUEsgEIrl1vGjCu8lx6uM6hIFtAvc X-Received: by 2002:a50:8f61:: with SMTP id 88mr289839edy.175.1603259135864; Tue, 20 Oct 2020 22:45:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603259135; cv=none; d=google.com; s=arc-20160816; b=DhWXlE7yoYPbbTdu/93/QDQs6cgomqR+bGaAztZ1OvjjjgPIdvcB9iJZ1BrxQygDTs PpFq8KZUioWeEwkLLvYd6EC9R367jTa27rqaPZJi3KZCE3QVXvu8glWgHQ/9DvLeN2PU tC5faDAfKZEW5ehWCkFKcc2p0tCRAzKE/OWjOFEUAY+rxDfKU8SPI/xXipX4g+muF6HF zti2wPAFYnvvAGGWU6IM0ZzTtYLqbBI8rs7n9PjSvgi+FHuLXVxgjB+2Q1jxrjX/mQ4h vjs1YSJSmj+IJtigxMZLIeMvZMEPBy19WD/d0KZHYnW12SgxNMMwTyJEWsNn9Uin2mk4 zK0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:subject:from :references:cc:to; bh=/8MQGbkVdUE/qLCYOt9OimHQxNKQV9cs6zu0YHVZnSg=; b=rR0HFO8oKIKkyGd/h1w7OWZ6R14UsD0IyIGvrQpuyMgt+UQ6LRcDBcYlcOFBka/N2G mOQLCoKbqwwyEqijT0cI0Gu/cPU1UbYdB0yRw8U9cAZzD8A75XNQ/QrURLRgz1/rbUBJ UNtIYslruOC1RH35S0o57zCEFeZV/i/xWZzcrJP5akK5fsHwHT3Ov68RYm+EiDUYsyHa omC4m8ieXrwWI9W0pdS0SRznnYiu4V3EqV8iOiVA5CMZFGsfZpNQ2RuD3C7pD1/MALNo 3LS/VyvhskGAGQxZ2xjs1G2CrXr/H7if5XwlSzKKFHOSd1S4OK9syu2G4/H+65eCNSlJ wNzw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v16si601691ejg.54.2020.10.20.22.45.13; Tue, 20 Oct 2020 22:45:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2408415AbgJTOud (ORCPT + 99 others); Tue, 20 Oct 2020 10:50:33 -0400 Received: from mx2.suse.de ([195.135.220.15]:33048 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2408369AbgJTOuc (ORCPT ); Tue, 20 Oct 2020 10:50:32 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 47EE6AE5C; Tue, 20 Oct 2020 14:50:31 +0000 (UTC) To: Axel Rasmussen , Steven Rostedt , Ingo Molnar , Andrew Morton , Michel Lespinasse , Daniel Jordan , Laurent Dufour , Jann Horn , Chinwen Chang Cc: Yafang Shao , linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <20201009220524.485102-1-axelrasmussen@google.com> <20201009220524.485102-3-axelrasmussen@google.com> From: Vlastimil Babka Subject: Re: [PATCH v3 2/2] mmap_lock: add tracepoints around lock acquisition Message-ID: <1b9238b7-17f2-6c1e-b37e-cf65424f504b@suse.cz> Date: Tue, 20 Oct 2020 16:50:30 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.3.2 MIME-Version: 1.0 In-Reply-To: <20201009220524.485102-3-axelrasmussen@google.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/10/20 12:05 AM, Axel Rasmussen wrote: > 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 Yeah I agree with this approach that follows the page ref one. ... > 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. I don't think this argument works in the kernel community. I would just remove this comment. > + */ > +EXPORT_SYMBOL(__tracepoint_mmap_lock_start_locking); > +EXPORT_SYMBOL(__tracepoint_mmap_lock_acquire_returned); > +EXPORT_SYMBOL(__tracepoint_mmap_lock_released); You can use EXPORT_TRACEPOINT_SYMBOL() here. > +#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()) { \ Is this check really needed? We only got called from the functions inlined in the .h file because tracepoint_enabled() was true in the first place, so this seems redundant. > + 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); Seems wasteful to have an always-true success field here. Yeah, not reusing the same event class for all three tracepoints means more code, but for tracing efficiency it's worth it, IMHO. > +} > +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); Ditto. > +} > +EXPORT_SYMBOL(__mmap_lock_do_trace_released); >