From patchwork Tue Apr 30 11:13:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 13648770 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE9AFC19F53 for ; Tue, 30 Apr 2024 11:14:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 285F66B0088; Tue, 30 Apr 2024 07:14:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 235586B0089; Tue, 30 Apr 2024 07:14:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0AF366B008A; Tue, 30 Apr 2024 07:14:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id DF2B66B0088 for ; Tue, 30 Apr 2024 07:14:09 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 932891604D3 for ; Tue, 30 Apr 2024 11:14:09 +0000 (UTC) X-FDA: 82065939018.10.EB8B300 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf30.hostedemail.com (Postfix) with ESMTP id BD9C180018 for ; Tue, 30 Apr 2024 11:14:07 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Saa4jiZm; spf=pass (imf30.hostedemail.com: domain of 3ftIwZgoKCB8Q8JII9AJMOBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--vdonnefort.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3ftIwZgoKCB8Q8JII9AJMOBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--vdonnefort.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714475647; a=rsa-sha256; cv=none; b=rAGSM01ySLVM00VZ6fPwVZlNHsmmjEU58VUeCRmSvulfTzD7gKHXRVTkNsyNOixyQ+JTt4 xnGmcoCCU5uipPBWp7bV7kepkz/TWbBT/Jl+tmoxnEPCEp1hZ//USYg59gLSQY5gspN044 sWS4Un3cnk0pt3LtR3sm/op2Tm2N6t8= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Saa4jiZm; spf=pass (imf30.hostedemail.com: domain of 3ftIwZgoKCB8Q8JII9AJMOBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--vdonnefort.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3ftIwZgoKCB8Q8JII9AJMOBJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--vdonnefort.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714475647; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=kaFaT9yjXtresVS5HFOg4DBcxHZeTWd1pZVR5Za+Mxc=; b=oy8E2iDrZQ8ukl325O8sLtNUzObrJtYE6XwHYfh0dTte2AKVypjZwu7I/w3tfTVKrYa3jq VCixYKDJanyEy09VP6bhTfC3xruIDcU43EHc5i+96XDInFZXVwbGSd5qZteWvQY6vcgCeg g1/YhxVopqzoxRBtjMbUqvk9aihq420= Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-349cafdc8f0so3335836f8f.1 for ; Tue, 30 Apr 2024 04:14:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1714475646; x=1715080446; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kaFaT9yjXtresVS5HFOg4DBcxHZeTWd1pZVR5Za+Mxc=; b=Saa4jiZmdJxUOQFcBXKGEosC6c87Yywq3KjWjxBbxrivXR29/T4CBpVDOKbHGK9vQ/ eTZUgU0Z/9xcyY4xPDrWUB9bZuMLm6sPSz2IqKWJLDNix9XcOqApM0neDGhsZH7+jDZC Uix1G0Ijb3tCIUX54UvjJrXIoDAeJ7ZM9rdZD8EIyHQpE2SSnpAHX/kLY9OG7y/IWTrA cwkJ0CGW0mkYtUqiSMUWuNRPvs56RYmF/eq2wHJpt20taKIObnWMePn0j9mHtwLEzj9s Nlg0C/bzQu0aNDpw70LuxDLYwMmCsM2wDu2a9w/rHWVYodRjYxT4cMp0TwmKGeRtH6kp 4P7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714475646; x=1715080446; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kaFaT9yjXtresVS5HFOg4DBcxHZeTWd1pZVR5Za+Mxc=; b=HGUtPZ+qiiY0hvTtwARHCMINlQtgQHLrWipd0P9iTr1/VVpSfmMdEqnmNKFXxeNxyQ aK3Qv0J7ar0V/ofghJjysLkZFvvgkEX79kuUojPm/3zRsIeWJMjZvi0GqwEHpx25iHTC 69PD6n+o73hTSPr/ljuO3F+nWLh4mxxgK12KrWVV1i5blz7cvsjs3AEWGmPXyBF0ntPa qyIsL575+S6jWjqwaIlXYtuBBrnVOCV18wj+it7Bdq0S0iNdFl2oy3AINPLNNAsEBGpE Ddt06zUQeAX93QwiYshdW1bkC9FymmdX1bShvTD1MYLWARE4B8Oj8V7rQD8Lc/XrEXUs 4/Ng== X-Forwarded-Encrypted: i=1; AJvYcCUrdgzSkxB5i1KZF9tu1FpeenjJJ2EuevEg9zhN4K2hnkO28Etsd6sQ5baYJwjENgko18wgm9o5PEFpjBWtqucMxJo= X-Gm-Message-State: AOJu0YxF+hb1yeA3WNMvRsDTdgZEP34Vph8etuD64jlLKJuE6W+t27gi vg8wMXi4rQPZTGGsF5hOxSpoalEDv0FH/5yiLbNAqylfUoC6WotBqxtTl/4RMJ01dwYvbfR7PIO GHrqajsAPe+KrKFnrAw== X-Google-Smtp-Source: AGHT+IEO2pWfjDCnEIaI11osu0vpD76LPQlxnP+vniPkBaaF4f948j34AMtRFw57ax5xp982MlQPZiRKqJXvorKB X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a5d:5850:0:b0:34d:17e1:7eed with SMTP id i16-20020a5d5850000000b0034d17e17eedmr22593wrf.3.1714475646019; Tue, 30 Apr 2024 04:14:06 -0700 (PDT) Date: Tue, 30 Apr 2024 12:13:52 +0100 In-Reply-To: <20240430111354.637356-1-vdonnefort@google.com> Mime-Version: 1.0 References: <20240430111354.637356-1-vdonnefort@google.com> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Message-ID: <20240430111354.637356-4-vdonnefort@google.com> Subject: [PATCH v22 3/5] tracing: Allow user-space mapping of the ring-buffer From: Vincent Donnefort To: rostedt@goodmis.org, mhiramat@kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: mathieu.desnoyers@efficios.com, kernel-team@android.com, rdunlap@infradead.org, rppt@kernel.org, david@redhat.com, Vincent Donnefort , linux-mm@kvack.org X-Stat-Signature: cyhnaamfkdgxgbzdkqm4o9nb8gph57po X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: BD9C180018 X-HE-Tag: 1714475647-684778 X-HE-Meta: U2FsdGVkX1/mgAfMufpeLO/YrKjabyeD2cG6FAFe5kGDE1gqYgDubMl/MG51udqtiGSXqXt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6V8jpVvqoN3+pVMrOQVQc6h//okFenNJUdYQN8CASBZtSbcM+rO28V4NZ2S8LkihStN1nBWFKX3ZcDUH1XfhfzphSm1xZb8vOoxumd5ARmwX9ymCLVp/MyxXm4BHqg0BCiJ56JGUFIyi/yw4bUPpGI6DnwYIyE3Y+aoh/z8+vRfulsucWPbY6UBmYlzkb6A0plPtiwwT/qB5rp7Y+1efKoZXmAYcP+0fYtkH62M5wh4Fya5pE7SWD64yZA+rA3hfjvvWGhmFJ1h37lLQG9tEBd+ahlOtP03wGpcJspN4rSjuGg3/t+8v3KYtwgZDQsFxOkB2Pd/GAoie/PmHYWKpGEQTLQ387hOap++5RX5Vx5qxHDLyydw0oebYrGeYoW5n/gV8seu8bZx9ncYx+C7Zx6iYbC7NzTuG/Uux+cmFBVEvX7O8ULZU7HVu+WNCFHtbtsg8CBdCSU= 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: List-Subscribe: List-Unsubscribe: Currently, user-space extracts data from the ring-buffer via splice, which is handy for storage or network sharing. However, due to splice limitations, it is imposible to do real-time analysis without a copy. A solution for that problem is to let the user-space map the ring-buffer directly. The mapping is exposed via the per-CPU file trace_pipe_raw. The first element of the mapping is the meta-page. It is followed by each subbuffer constituting the ring-buffer, ordered by their unique page ID: * Meta-page -- include/uapi/linux/trace_mmap.h for a description * Subbuf ID 0 * Subbuf ID 1 ... It is therefore easy to translate a subbuf ID into an offset in the mapping: reader_id = meta->reader->id; reader_offset = meta->meta_page_size + reader_id * meta->subbuf_size; When new data is available, the mapper must call a newly introduced ioctl: TRACE_MMAP_IOCTL_GET_READER. This will update the Meta-page reader ID to point to the next reader containing unread data. Mapping will prevent snapshot and buffer size modifications. CC: Signed-off-by: Vincent Donnefort diff --git a/include/uapi/linux/trace_mmap.h b/include/uapi/linux/trace_mmap.h index b682e9925539..bd1066754220 100644 --- a/include/uapi/linux/trace_mmap.h +++ b/include/uapi/linux/trace_mmap.h @@ -43,4 +43,6 @@ struct trace_buffer_meta { __u64 Reserved2; }; +#define TRACE_MMAP_IOCTL_GET_READER _IO('T', 0x1) + #endif /* _TRACE_MMAP_H_ */ diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 233d1af39fff..a35e7f598233 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1191,6 +1191,12 @@ static void tracing_snapshot_instance_cond(struct trace_array *tr, return; } + if (tr->mapped) { + trace_array_puts(tr, "*** BUFFER MEMORY MAPPED ***\n"); + trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n"); + return; + } + local_irq_save(flags); update_max_tr(tr, current, smp_processor_id(), cond_data); local_irq_restore(flags); @@ -1323,7 +1329,7 @@ static int tracing_arm_snapshot_locked(struct trace_array *tr) lockdep_assert_held(&trace_types_lock); spin_lock(&tr->snapshot_trigger_lock); - if (tr->snapshot == UINT_MAX) { + if (tr->snapshot == UINT_MAX || tr->mapped) { spin_unlock(&tr->snapshot_trigger_lock); return -EBUSY; } @@ -6068,7 +6074,7 @@ static void tracing_set_nop(struct trace_array *tr) { if (tr->current_trace == &nop_trace) return; - + tr->current_trace->enabled--; if (tr->current_trace->reset) @@ -8194,15 +8200,32 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos, return ret; } -/* An ioctl call with cmd 0 to the ring buffer file will wake up all waiters */ static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct ftrace_buffer_info *info = file->private_data; struct trace_iterator *iter = &info->iter; + int err; + + if (cmd == TRACE_MMAP_IOCTL_GET_READER) { + if (!(file->f_flags & O_NONBLOCK)) { + err = ring_buffer_wait(iter->array_buffer->buffer, + iter->cpu_file, + iter->tr->buffer_percent, + NULL, NULL); + if (err) + return err; + } - if (cmd) - return -ENOIOCTLCMD; + return ring_buffer_map_get_reader(iter->array_buffer->buffer, + iter->cpu_file); + } else if (cmd) { + return -ENOTTY; + } + /* + * An ioctl call with cmd 0 to the ring buffer file will wake up all + * waiters + */ mutex_lock(&trace_types_lock); /* Make sure the waiters see the new wait_index */ @@ -8214,6 +8237,76 @@ static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned return 0; } +#ifdef CONFIG_TRACER_MAX_TRACE +static int get_snapshot_map(struct trace_array *tr) +{ + int err = 0; + + /* + * Called with mmap_lock held. lockdep would be unhappy if we would now + * take trace_types_lock. Instead use the specific + * snapshot_trigger_lock. + */ + spin_lock(&tr->snapshot_trigger_lock); + + if (tr->snapshot || tr->mapped == UINT_MAX) + err = -EBUSY; + else + tr->mapped++; + + spin_unlock(&tr->snapshot_trigger_lock); + + /* Wait for update_max_tr() to observe iter->tr->mapped */ + if (tr->mapped == 1) + synchronize_rcu(); + + return err; + +} +static void put_snapshot_map(struct trace_array *tr) +{ + spin_lock(&tr->snapshot_trigger_lock); + if (!WARN_ON(!tr->mapped)) + tr->mapped--; + spin_unlock(&tr->snapshot_trigger_lock); +} +#else +static inline int get_snapshot_map(struct trace_array *tr) { return 0; } +static inline void put_snapshot_map(struct trace_array *tr) { } +#endif + +static void tracing_buffers_mmap_close(struct vm_area_struct *vma) +{ + struct ftrace_buffer_info *info = vma->vm_file->private_data; + struct trace_iterator *iter = &info->iter; + + WARN_ON(ring_buffer_unmap(iter->array_buffer->buffer, iter->cpu_file)); + put_snapshot_map(iter->tr); +} + +static const struct vm_operations_struct tracing_buffers_vmops = { + .close = tracing_buffers_mmap_close, +}; + +static int tracing_buffers_mmap(struct file *filp, struct vm_area_struct *vma) +{ + struct ftrace_buffer_info *info = filp->private_data; + struct trace_iterator *iter = &info->iter; + int ret = 0; + + ret = get_snapshot_map(iter->tr); + if (ret) + return ret; + + ret = ring_buffer_map(iter->array_buffer->buffer, iter->cpu_file, vma); + if (ret) + put_snapshot_map(iter->tr); + + vma->vm_ops = &tracing_buffers_vmops; + + return ret; +} + static const struct file_operations tracing_buffers_fops = { .open = tracing_buffers_open, .read = tracing_buffers_read, @@ -8223,6 +8316,7 @@ static const struct file_operations tracing_buffers_fops = { .splice_read = tracing_buffers_splice_read, .unlocked_ioctl = tracing_buffers_ioctl, .llseek = no_llseek, + .mmap = tracing_buffers_mmap, }; static ssize_t diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 64450615ca0c..749a182dab48 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -336,6 +336,7 @@ struct trace_array { bool allocated_snapshot; spinlock_t snapshot_trigger_lock; unsigned int snapshot; + unsigned int mapped; unsigned long max_latency; #ifdef CONFIG_FSNOTIFY struct dentry *d_max_latency;