From patchwork Sat Apr 6 17:36:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 13619893 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 28634CD1284 for ; Sat, 6 Apr 2024 17:37:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B94D6B0083; Sat, 6 Apr 2024 13:37:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1421F6B0085; Sat, 6 Apr 2024 13:37:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EFD386B0087; Sat, 6 Apr 2024 13:37:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D2DAC6B0083 for ; Sat, 6 Apr 2024 13:37:18 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 89E7A80308 for ; Sat, 6 Apr 2024 17:37:18 +0000 (UTC) X-FDA: 81979813356.26.C327FEC Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf14.hostedemail.com (Postfix) with ESMTP id AFDAD100011 for ; Sat, 6 Apr 2024 17:37:16 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Zb6geIP8; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 3S4gRZgoKCNgP7IHH89ILNAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--vdonnefort.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3S4gRZgoKCNgP7IHH89ILNAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--vdonnefort.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712425036; a=rsa-sha256; cv=none; b=jX5kxUYCHGdtrVO70LRrw1cicxmUsUo7IojM1ROhSpuNButWI9+2zwC+7jkLfNWAZBOJ80 9/0zFchNnL8WJzO0LyQZqobTdzXiCv2WTPojWAveX+kNw5juaQvxHDFnQeDylAiAV4e5Lf bhZulHTGOe8dMuFHBBNllvPVmh7bt+I= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Zb6geIP8; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 3S4gRZgoKCNgP7IHH89ILNAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--vdonnefort.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3S4gRZgoKCNgP7IHH89ILNAIIAF8.6IGFCHOR-GGEP46E.ILA@flex--vdonnefort.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712425036; 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=72Zhe8jfvgxef0iyuPAsGeg/MBc9zR+e0H1uunA/IEs=; b=ReHZOXRFjCgAn+vduDyoEjhkQueTTaTR2SexNugX3YyihwEc0jjUAjxiKOBO3qb9/5hGPW RvUfSooq3AWgwX6qX7oNxZzK1tkWk56f/EOKILdmspdhP+abFSGEFF3K3QcOnxcziPuHg7 vGX0fg/GoTdxqy5iKB1A2hn4KEPodx0= Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-343d02b62ddso1378079f8f.1 for ; Sat, 06 Apr 2024 10:37:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712425035; x=1713029835; 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=72Zhe8jfvgxef0iyuPAsGeg/MBc9zR+e0H1uunA/IEs=; b=Zb6geIP874BIqZRhCu0hUejhopYZL/5wetbwqoAp9xLFkIbtU6Oav5ZZgYcixUCJA1 Dqj0XTSfJaGrUgk9IcowCFrOXnjl/2vDzFZ5OVg+UBQF9gIiefJyUP3JyvmgqytJdzwA CitDtNBUJbRd+tViSnnANJbeuBekbXwzdX8eHitm3RnWO5oHxi43oDszBLskpxEB9Kbl Ev1xFSIgO0B0NVrFONM78Pm/gwCahLnBIfvzG2V7H9zDCPO2nkR3VW2NpPmbeXW1J0+T Hm3wqS+KRA00vtFct/P17BjcwXB+IOISGFZNchEUv4irvRQrDU1uuorYUtlW3ApLFsnL 0EeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712425035; x=1713029835; 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=72Zhe8jfvgxef0iyuPAsGeg/MBc9zR+e0H1uunA/IEs=; b=NE9vO7FKwbW0diV0MdPHG7jXcCYYehmBJA6npTq+XzkHSfGt5eP7TF0rYW8EenmKzr 1vj1sLvzIsrx87wM9Leve/9EHgcWlKWNL+tinZaZDGfyLIad+B/jJxJBz5JTmv3J40HV Od3KVdn8QFuGEji5826fiZIZ4Mae6boqkJrhHUxxyzjnVq57L7jjtn8J6R8/qKmwQj23 9gyoNBFYjZpFQy1cG98hHcl5pQolNJMW5Sm32xMNtV++92Vl6n9QXB1Opa24UkL/bosi 5+pDi3Bq0q4mX7G/UHmFS6zWlIkb9K34tDT6PCi0+a6yFlSyPr3cS9mbvfIrB+kw1aiI LfzA== X-Forwarded-Encrypted: i=1; AJvYcCVw/vZuO/uXc59jk1J8eHpDuH96qkKDEwVGdwQqmPEtJNl2QszQH2eOgDayDCcLsBH9C18JBhBYiF3AF5eR4zycI9M= X-Gm-Message-State: AOJu0Yyzsh0bL9B5DsiL1jBpQpYQkZfG+myYCm6aXt07TBRaUoUXWpDC bH5uJ1vD17MIk4bG0pZyk6/e1NZ5/IF1Djv8whxPIghwubnr6RfvPtal2RziaB8XCPQorujlSqm 0zmdO/9wUVRgpTXtLbA== X-Google-Smtp-Source: AGHT+IGiDvc+/BkaShTc5qPfjfkPWDIrx/Pw/cQ9puA5MwbRRuMEO4JPSTNTgVUJ8vLGJlfy/dHi3mkpuxYFwMrp X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:adf:f04a:0:b0:343:ebd3:c444 with SMTP id t10-20020adff04a000000b00343ebd3c444mr8807wro.5.1712425035369; Sat, 06 Apr 2024 10:37:15 -0700 (PDT) Date: Sat, 6 Apr 2024 18:36:47 +0100 In-Reply-To: <20240406173649.3210836-1-vdonnefort@google.com> Mime-Version: 1.0 References: <20240406173649.3210836-1-vdonnefort@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240406173649.3210836-4-vdonnefort@google.com> Subject: [PATCH v20 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, Vincent Donnefort , linux-mm@kvack.org X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: AFDAD100011 X-Stat-Signature: 9b1aefhixswwq3ycsotjatyti81epzkp X-HE-Tag: 1712425036-29051 X-HE-Meta: U2FsdGVkX19pqyocaf/SKDdAZfrQ9RSGZlyukog7ZtagQdqfBWNEy09bgbYJIKmCt3mVmfHwuDiCdq/pp860y2eL11bbQdJU5RN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T1kBq2yBD8PGTHW2A+Blc94SPI7b9L5xm9UEgWIWGE/iT6UdDpFMyXWS9krjUUM9LrHejgKPJ5v5jP6kLARD8tTDS9gkcl6VWYOYq0Hp8x3nINPu/2e9xdSaLBFnY1Lv6858MCkYsKtAO+2Epd4WYPFUMdZNkSfU/Rt33u5wtltQ7LnoxzP9eF4lgCEyZ4h1osVjzlym9Xkv2vx/bvYV0PFnY3nvqFbmuO6lPWsoZG33dZlgZI+eNsERO3G2mSFB47a6fys6hqYtfiH2Z9zMUWFdXHY9dKIW0TKEU07P4ih4B9b2P5dqcfp+06h2n9i4F2VrN4kf/7Wq98rtbAHSDYvFQ2bBxz5w22TNuT/y7TPd0ykjniws5hdXH+L5hFQWYBtcHyNX6MdFapmBtuucSMgrW6rAa7U+jZXJy9OCB8= 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 ffcd8dfcaa4f..d25b9d504a7c 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..83194bf7b1df 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) - return -ENOIOCTLCMD; + 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; + } + 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,85 @@ 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; + + if (vma->vm_flags & VM_WRITE || vma->vm_flags & VM_EXEC || + !(vma->vm_flags & VM_MAYSHARE)) + return -EPERM; + + vm_flags_mod(vma, + VM_MIXEDMAP | VM_PFNMAP | + VM_DONTCOPY | VM_DONTDUMP | VM_DONTEXPAND | VM_IO, + VM_MAYWRITE); + + vma->vm_ops = &tracing_buffers_vmops; + + 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); + + return ret; +} + static const struct file_operations tracing_buffers_fops = { .open = tracing_buffers_open, .read = tracing_buffers_read, @@ -8223,6 +8325,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;