From patchwork Tue Jan 23 11:07: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: 13527254 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 629785C911 for ; Tue, 23 Jan 2024 11:08:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008085; cv=none; b=ZkGaAv/4l2VcSeIww5D2xCVM8Jgf7+iG1kyp4VjzIYFgaczdoh7GUYMQp9LJkfp/TqdMn20JJwbXTI2h7zBXD5NQggr9o8a3Qr/pcmUYmYE84sfu0ARWFD7z9ib+urBPXQDS+6o/51aVo3uECrhvccBT5x6J5QR4Zot1TUFTt0Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008085; c=relaxed/simple; bh=OvEEBWdcH3OKo6KDL1Af3FAQxWgGr/9147Iu28ODlPQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=W3s9Q4FZGOPu11SnqgY0gyJTQy8d+S2BjxRYv3AEmSElqgTT64nUXxBLJCIYxhjuHXcbh88YfagGvmnsrshNcYP/qdAKK0qEs1TZBKJQsBG6NL3WxUWFiso7HKt6ADDtmZ46DSq13slIooogDi/hqz9HArGeZeS7MVSoDCEXXyY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=LlxyUMXp; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LlxyUMXp" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5ffc7ce3343so34274467b3.1 for ; Tue, 23 Jan 2024 03:08:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706008083; x=1706612883; darn=vger.kernel.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=GTPmymlTxKhC1ZOw6521BjIEJCf876w90abWnygi6LA=; b=LlxyUMXpJBV3weDVRUHO7AXYO5YGYFA3Cw7Y2otFwa11ZNhurhFUq/RToCG050OWi5 8Bku70Yy4k9DXTh5i3GIszH8PZaJgQm0teIVTVNu+CB6Jikyhq+ZfM1APqn0xT999UH+ W8UW1CPIsZ+8yic7UvgQPDAJjXMFMsY1wA/wXcOT9E9xmyQGfDfCwyEXh++hT7Ucbiwk PiIertEywXOLd4cAFWA3bRhk+iWw99m+HM9JTydC838o88fRcSD+qv4P57T2mna1Dq2Y QhLguAydIYRmo/MhR+blY0mClbHHozAK22yOk6Dy+Ru3iCiE3akb5Kz6I1pMiT8Ffp6a l5LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706008083; x=1706612883; 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=GTPmymlTxKhC1ZOw6521BjIEJCf876w90abWnygi6LA=; b=F8zmW9wN3Cf596nBJIaAkysDFtTKteI2HeDB+yulsczG6w9r9E801c+4EHKsJpyDUt 1628QfFwHXwydjS2w2noAbOadjpL141Bd27+/SHw8WfhQGNexqltSdVoY6WvYQ+AF6cx aq7pGvS1DFnk7MP8DaCqEtlXnnSmEnH7uUgQZMg19bHxffYnmvZU2cVzdyz6AYsEw6pv z1rbToLs4Nx9QVJ3yLH9brwJFXPaNeLvnsFYp+JBPBH2TEpW8CMPlQr1UHIm+fnK7YLc Htkv4EpZ05csLx6hmYCtMD/D1JrT0OoFphJM7w2aUGu9yxOVrnIgSTZCcpWnTvcEud+P hudg== X-Gm-Message-State: AOJu0Yw5h/imuRPfD7XyCgLdZEMMFnz9Nk4UuN8hVEbQiyNDEA5WndrC 6xu4ynewQm1em/NPEpnQ1SNogblY0Gguz6qzY+4Zxv4vwUQPQpFp499jWpq8ZgBaibkLHsa6G2J 4ijzv0Ze4/8sJSugWuA== X-Google-Smtp-Source: AGHT+IFntKP4XZZ43QuR+umsJroitll4qvgERUZmB0oEZ8AsB1XTKr3l5rGp56Z7TXjvTNRwaYCbKG38H3a/DY06 X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:690c:24d:b0:5ff:b6cc:10e with SMTP id ba13-20020a05690c024d00b005ffb6cc010emr1505537ywb.10.1706008083460; Tue, 23 Jan 2024 03:08:03 -0800 (PST) Date: Tue, 23 Jan 2024 11:07:52 +0000 In-Reply-To: <20240123110757.3657908-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240123110757.3657908-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123110757.3657908-2-vdonnefort@google.com> Subject: [PATCH v12 1/6] ring-buffer: Zero ring-buffer sub-buffers 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, Vincent Donnefort In preparation for the ring-buffer memory mapping where each subbuf will be accessible to user-space, zero all the page allocations. Signed-off-by: Vincent Donnefort Reviewed-by: Masami Hiramatsu (Google) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 173d2595ce2d..db73e326fa04 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -1466,7 +1466,8 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, list_add(&bpage->list, pages); - page = alloc_pages_node(cpu_to_node(cpu_buffer->cpu), mflags, + page = alloc_pages_node(cpu_to_node(cpu_buffer->cpu), + mflags | __GFP_ZERO, cpu_buffer->buffer->subbuf_order); if (!page) goto free_pages; @@ -1551,7 +1552,8 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu) cpu_buffer->reader_page = bpage; - page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, cpu_buffer->buffer->subbuf_order); + page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL | __GFP_ZERO, + cpu_buffer->buffer->subbuf_order); if (!page) goto fail_free_reader; bpage->page = page_address(page); @@ -5525,7 +5527,8 @@ ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu) if (bpage->data) goto out; - page = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL | __GFP_NORETRY, + page = alloc_pages_node(cpu_to_node(cpu), + GFP_KERNEL | __GFP_NORETRY | __GFP_ZERO, cpu_buffer->buffer->subbuf_order); if (!page) { kfree(bpage); From patchwork Tue Jan 23 11:07:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 13527262 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A875C5D729 for ; Tue, 23 Jan 2024 11:08:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008088; cv=none; b=jsufRYT0wWEbpXkMIvu96F6g7FurFlBdO3C+Dz8Jap9fKl/WFNPB9N2YOQ8qE1JAkSaRAkNRQuvqhYbOTHhDJUUn2p8S7ItAyueCFOJCvvF2f7vV6fOlhu3/gnnP68ZSF50dZBnpW9DEUxwPS6+ZVbNiHpgrC32ds1ctJPFNU6g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008088; c=relaxed/simple; bh=L7NI50aDDQNa7i7oPwFFzpwUIH5NwtqQCHEB/tffkCk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fD1XkdNZaf3fwBOtE/oMAsZXUZRwL14Te3ghlNh1Uh6ReL2K1NwOy4uYReyLNYJSsE3MfSScFSB3ICEnOyvhRb3gFO1OMkM4mOOO0aMVsjV+ycF/Z0DIWCUaLu3dqDJ2zHGqzpLX3pU1DWVN6fZ7m857jiL4hvGc9OoKCSv5hR0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0r/UmeY1; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0r/UmeY1" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc365b303abso1810773276.3 for ; Tue, 23 Jan 2024 03:08:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706008085; x=1706612885; darn=vger.kernel.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=ERslFwXmtViiX7Q5k4o2C2xcWADYFZJI/gC7YgScktg=; b=0r/UmeY1xhjUpPxVgLMQM1ql6b7NqFKOa9xYuyn2Maysn+AguJ+ivHfyKInkXsQ9bU cxktap6zr2WPfT3nU0esSschNLBV5Sc4McbblzcNB46g+wmE+1ks4FmCoI40ZeONuC0s D5O7t/0Eput9JKY/T6DwD/D7J9xt7as/u+7SnHiNCWlEb7S4LEtMOuHvNpcNgaUHwMYL Yvx8sM97lSyY8JAyvtpWEzitbvp1exA6CqIuEft7eqYkZ/5z6UXd5AQliT94/pR9vbRI JZwTEpHKJfds1aW4p+sFToluVDjy+H5633qp2UKZAHKKzf3qzF2o+s+HuP7l+GmAZaIN v0+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706008085; x=1706612885; 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=ERslFwXmtViiX7Q5k4o2C2xcWADYFZJI/gC7YgScktg=; b=CJe4KI4PzNMpkplgQy+JC6KpzyvYEgRxLgMrGFFaycJ23r2hKZZTYX5w7Nv6sx0hIB sRC+B3c1g/a8LoQBBGz+DcXIhz60Bkh4zwb8TV2H+ZV8EkpwXgXVgrrT5/5EOB+HQRDk E0TYbOVrRLndQm8SEJw2ta01I42fUk+woTYj+yIaTBu1cEI5Reh2I3k5q8uCxPaV5wsY Tu2HLsNqpR0PHsOs1Ca0ndCLzrxhtELgae0xKjvOayMIamkU+fKtRKoOTTn+/lPSCfdp V1xKnNgfAotDaC8SObrzMJaHDZyqAjKiQmvNmVue/vUTl9/ltQHyos5/4mjRhetoSxS8 0oBw== X-Gm-Message-State: AOJu0Yy3Ib1/BETfDzyHuM0pLw4RlbyzIHmEJ41B8fzbsEWegPNp/T93 OGQqwwuxeewTJGOeyrt/ASt7SAZKIukPNhpyM6NPah+gF+UFFwGZcrs7ilM+j3x6mpRhOOWY0x/ iCwW83ATSyogsB8Grqw== X-Google-Smtp-Source: AGHT+IFVdPUBDi9GQdlv4Y48ePe+iFNVloNsMr2rBlX03cxZvnSC38N/JGbe+ffFXEe+ETf0Z3AFA1NTDdf0yK4r X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a25:840d:0:b0:dc2:3fba:40ba with SMTP id u13-20020a25840d000000b00dc23fba40bamr312083ybk.7.1706008085659; Tue, 23 Jan 2024 03:08:05 -0800 (PST) Date: Tue, 23 Jan 2024 11:07:53 +0000 In-Reply-To: <20240123110757.3657908-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240123110757.3657908-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123110757.3657908-3-vdonnefort@google.com> Subject: [PATCH v12 2/6] ring-buffer: Introducing ring-buffer mapping functions 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, Vincent Donnefort In preparation for allowing the user-space to map a ring-buffer, add a set of mapping functions: ring_buffer_{map,unmap}() ring_buffer_map_fault() And controls on the ring-buffer: ring_buffer_map_get_reader() /* swap reader and head */ Mapping the ring-buffer also involves: A unique ID for each subbuf of the ring-buffer, currently they are only identified through their in-kernel VA. A meta-page, where are stored ring-buffer statistics and a description for the current reader The linear mapping exposes the meta-page, and each subbuf of the ring-buffer, ordered following their unique ID, assigned during the first mapping. Once mapped, no subbuf can get in or out of the ring-buffer: the buffer size will remain unmodified and the splice enabling functions will in reality simply memcpy the data instead of swapping subbufs. Signed-off-by: Vincent Donnefort diff --git a/include/linux/ring_buffer.h b/include/linux/ring_buffer.h index fa802db216f9..0841ba8bab14 100644 --- a/include/linux/ring_buffer.h +++ b/include/linux/ring_buffer.h @@ -6,6 +6,8 @@ #include #include +#include + struct trace_buffer; struct ring_buffer_iter; @@ -221,4 +223,9 @@ int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node); #define trace_rb_cpu_prepare NULL #endif +int ring_buffer_map(struct trace_buffer *buffer, int cpu); +int ring_buffer_unmap(struct trace_buffer *buffer, int cpu); +struct page *ring_buffer_map_fault(struct trace_buffer *buffer, int cpu, + unsigned long pgoff); +int ring_buffer_map_get_reader(struct trace_buffer *buffer, int cpu); #endif /* _LINUX_RING_BUFFER_H */ diff --git a/include/uapi/linux/trace_mmap.h b/include/uapi/linux/trace_mmap.h new file mode 100644 index 000000000000..5468afc94be7 --- /dev/null +++ b/include/uapi/linux/trace_mmap.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _TRACE_MMAP_H_ +#define _TRACE_MMAP_H_ + +#include + +/** + * struct trace_buffer_meta - Ring-buffer Meta-page description + * @meta_page_size: Size of this meta-page. + * @meta_struct_len: Size of this structure. + * @subbuf_size: Size of each subbuf, including the header. + * @nr_subbufs: Number of subbfs in the ring-buffer. + * @reader.lost_events: Number of events lost at the time of the reader swap. + * @reader.id: subbuf ID of the current reader. From 0 to @nr_subbufs - 1 + * @reader.read: Number of bytes read on the reader subbuf. + * @entries: Number of entries in the ring-buffer. + * @overrun: Number of entries lost in the ring-buffer. + * @read: Number of entries that have been read. + * @subbufs_touched: Number of subbufs that have been filled. + * @subbufs_lost: Number of subbufs lost to overrun. + * @subbufs_read: Number of subbufs that have been read. + */ +struct trace_buffer_meta { + __u32 meta_page_size; + __u32 meta_struct_len; + + __u32 subbuf_size; + __u32 nr_subbufs; + + struct { + __u64 lost_events; + __u32 id; + __u32 read; + } reader; + + __u64 entries; + __u64 overrun; + __u64 read; + + __u64 subbufs_touched; + __u64 subbufs_lost; +}; + +#endif /* _TRACE_MMAP_H_ */ diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index db73e326fa04..e7b39f7a1c74 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -338,6 +338,7 @@ struct buffer_page { local_t entries; /* entries on this page */ unsigned long real_end; /* real end of data */ unsigned order; /* order of the page */ + u32 id; /* ID for external mapping */ struct buffer_data_page *page; /* Actual data page */ }; @@ -484,6 +485,12 @@ struct ring_buffer_per_cpu { u64 read_stamp; /* pages removed since last reset */ unsigned long pages_removed; + + int mapped; + struct mutex mapping_lock; + unsigned long *subbuf_ids; /* ID to addr */ + struct trace_buffer_meta *meta_page; + /* ring buffer pages to update, > 0 to add, < 0 to remove */ long nr_pages_to_update; struct list_head new_pages; /* new pages to add */ @@ -1542,6 +1549,7 @@ rb_allocate_cpu_buffer(struct trace_buffer *buffer, long nr_pages, int cpu) init_irq_work(&cpu_buffer->irq_work.work, rb_wake_up_waiters); init_waitqueue_head(&cpu_buffer->irq_work.waiters); init_waitqueue_head(&cpu_buffer->irq_work.full_waiters); + mutex_init(&cpu_buffer->mapping_lock); bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), GFP_KERNEL, cpu_to_node(cpu)); @@ -5160,6 +5168,22 @@ static void rb_clear_buffer_page(struct buffer_page *page) page->read = 0; } +static void rb_update_meta_page(struct ring_buffer_per_cpu *cpu_buffer) +{ + struct trace_buffer_meta *meta = cpu_buffer->meta_page; + + WRITE_ONCE(meta->reader.read, cpu_buffer->reader_page->read); + WRITE_ONCE(meta->reader.id, cpu_buffer->reader_page->id); + WRITE_ONCE(meta->reader.lost_events, cpu_buffer->lost_events); + + WRITE_ONCE(meta->entries, local_read(&cpu_buffer->entries)); + WRITE_ONCE(meta->overrun, local_read(&cpu_buffer->overrun)); + WRITE_ONCE(meta->read, cpu_buffer->read); + + WRITE_ONCE(meta->subbufs_touched, local_read(&cpu_buffer->pages_touched)); + WRITE_ONCE(meta->subbufs_lost, local_read(&cpu_buffer->pages_lost)); +} + static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) { @@ -5204,6 +5228,9 @@ rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) cpu_buffer->lost_events = 0; cpu_buffer->last_overrun = 0; + if (cpu_buffer->mapped) + rb_update_meta_page(cpu_buffer); + rb_head_page_activate(cpu_buffer); cpu_buffer->pages_removed = 0; } @@ -5418,6 +5445,11 @@ int ring_buffer_swap_cpu(struct trace_buffer *buffer_a, cpu_buffer_a = buffer_a->buffers[cpu]; cpu_buffer_b = buffer_b->buffers[cpu]; + if (READ_ONCE(cpu_buffer_a->mapped) || READ_ONCE(cpu_buffer_b->mapped)) { + ret = -EBUSY; + goto out; + } + /* At least make sure the two buffers are somewhat the same */ if (cpu_buffer_a->nr_pages != cpu_buffer_b->nr_pages) goto out; @@ -5682,7 +5714,8 @@ int ring_buffer_read_page(struct trace_buffer *buffer, * Otherwise, we can simply swap the page with the one passed in. */ if (read || (len < (commit - read)) || - cpu_buffer->reader_page == cpu_buffer->commit_page) { + cpu_buffer->reader_page == cpu_buffer->commit_page || + cpu_buffer->mapped) { struct buffer_data_page *rpage = cpu_buffer->reader_page->page; unsigned int rpos = read; unsigned int pos = 0; @@ -5901,6 +5934,11 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order) cpu_buffer = buffer->buffers[cpu]; + if (cpu_buffer->mapped) { + err = -EBUSY; + goto error; + } + /* Update the number of pages to match the new size */ nr_pages = old_size * buffer->buffers[cpu]->nr_pages; nr_pages = DIV_ROUND_UP(nr_pages, buffer->subbuf_size); @@ -6002,6 +6040,295 @@ int ring_buffer_subbuf_order_set(struct trace_buffer *buffer, int order) } EXPORT_SYMBOL_GPL(ring_buffer_subbuf_order_set); +#define subbuf_page(off, start) \ + virt_to_page((void *)(start + (off << PAGE_SHIFT))) + +#define foreach_subbuf_page(sub_order, start, page) \ + page = subbuf_page(0, (start)); \ + for (int __off = 0; __off < (1 << (sub_order)); \ + __off++, page = subbuf_page(__off, (start))) + +static inline void subbuf_map_prepare(unsigned long subbuf_start, int order) +{ + struct page *page; + + /* + * When allocating order > 0 pages, only the first struct page has a + * refcount > 1. Increasing the refcount here ensures none of the struct + * page composing the sub-buffer is freeed when the mapping is closed. + */ + foreach_subbuf_page(order, subbuf_start, page) + page_ref_inc(page); +} + +static inline void subbuf_unmap(unsigned long subbuf_start, int order) +{ + struct page *page; + + foreach_subbuf_page(order, subbuf_start, page) { + page_ref_dec(page); + page->mapping = NULL; + } +} + +static void rb_free_subbuf_ids(struct ring_buffer_per_cpu *cpu_buffer) +{ + int sub_id; + + for (sub_id = 0; sub_id < cpu_buffer->nr_pages + 1; sub_id++) + subbuf_unmap(cpu_buffer->subbuf_ids[sub_id], + cpu_buffer->buffer->subbuf_order); + + kfree(cpu_buffer->subbuf_ids); + cpu_buffer->subbuf_ids = NULL; +} + +static int rb_alloc_meta_page(struct ring_buffer_per_cpu *cpu_buffer) +{ + if (cpu_buffer->meta_page) + return 0; + + cpu_buffer->meta_page = page_to_virt(alloc_page(GFP_USER | __GFP_ZERO)); + if (!cpu_buffer->meta_page) + return -ENOMEM; + + return 0; +} + +static void rb_free_meta_page(struct ring_buffer_per_cpu *cpu_buffer) +{ + unsigned long addr = (unsigned long)cpu_buffer->meta_page; + + virt_to_page((void *)addr)->mapping = NULL; + free_page(addr); + cpu_buffer->meta_page = NULL; +} + +static void rb_setup_ids_meta_page(struct ring_buffer_per_cpu *cpu_buffer, + unsigned long *subbuf_ids) +{ + struct trace_buffer_meta *meta = cpu_buffer->meta_page; + unsigned int nr_subbufs = cpu_buffer->nr_pages + 1; + struct buffer_page *first_subbuf, *subbuf; + int id = 0; + + subbuf_ids[id] = (unsigned long)cpu_buffer->reader_page->page; + subbuf_map_prepare(subbuf_ids[id], cpu_buffer->buffer->subbuf_order); + cpu_buffer->reader_page->id = id++; + + first_subbuf = subbuf = rb_set_head_page(cpu_buffer); + do { + if (id >= nr_subbufs) { + WARN_ON(1); + break; + } + + subbuf_ids[id] = (unsigned long)subbuf->page; + subbuf->id = id; + subbuf_map_prepare(subbuf_ids[id], cpu_buffer->buffer->subbuf_order); + + rb_inc_page(&subbuf); + id++; + } while (subbuf != first_subbuf); + + /* install subbuf ID to kern VA translation */ + cpu_buffer->subbuf_ids = subbuf_ids; + + meta->meta_page_size = PAGE_SIZE; + meta->meta_struct_len = sizeof(*meta); + meta->nr_subbufs = nr_subbufs; + meta->subbuf_size = cpu_buffer->buffer->subbuf_size + BUF_PAGE_HDR_SIZE; + + rb_update_meta_page(cpu_buffer); +} + +static inline struct ring_buffer_per_cpu * +rb_get_mapped_buffer(struct trace_buffer *buffer, int cpu) +{ + struct ring_buffer_per_cpu *cpu_buffer; + + if (!cpumask_test_cpu(cpu, buffer->cpumask)) + return ERR_PTR(-EINVAL); + + cpu_buffer = buffer->buffers[cpu]; + + mutex_lock(&cpu_buffer->mapping_lock); + + if (!cpu_buffer->mapped) { + mutex_unlock(&cpu_buffer->mapping_lock); + return ERR_PTR(-ENODEV); + } + + return cpu_buffer; +} + +static inline void rb_put_mapped_buffer(struct ring_buffer_per_cpu *cpu_buffer) +{ + mutex_unlock(&cpu_buffer->mapping_lock); +} + +int ring_buffer_map(struct trace_buffer *buffer, int cpu) +{ + struct ring_buffer_per_cpu *cpu_buffer; + unsigned long flags, *subbuf_ids; + int err = 0; + + if (!cpumask_test_cpu(cpu, buffer->cpumask)) + return -EINVAL; + + cpu_buffer = buffer->buffers[cpu]; + + mutex_lock(&cpu_buffer->mapping_lock); + + if (cpu_buffer->mapped) { + if (cpu_buffer->mapped == INT_MAX) + err = -EBUSY; + else + WRITE_ONCE(cpu_buffer->mapped, cpu_buffer->mapped + 1); + mutex_unlock(&cpu_buffer->mapping_lock); + return err; + } + + /* prevent another thread from changing buffer sizes */ + mutex_lock(&buffer->mutex); + + err = rb_alloc_meta_page(cpu_buffer); + if (err) + goto unlock; + + /* subbuf_ids include the reader while nr_pages does not */ + subbuf_ids = kzalloc(sizeof(*subbuf_ids) * (cpu_buffer->nr_pages + 1), + GFP_KERNEL); + if (!subbuf_ids) { + rb_free_meta_page(cpu_buffer); + err = -ENOMEM; + goto unlock; + } + + atomic_inc(&cpu_buffer->resize_disabled); + + /* + * Lock all readers to block any subbuf swap until the subbuf IDs are + * assigned. + */ + raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); + + rb_setup_ids_meta_page(cpu_buffer, subbuf_ids); + + WRITE_ONCE(cpu_buffer->mapped, 1); + + raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); +unlock: + mutex_unlock(&buffer->mutex); + mutex_unlock(&cpu_buffer->mapping_lock); + + return err; +} + +int ring_buffer_unmap(struct trace_buffer *buffer, int cpu) +{ + struct ring_buffer_per_cpu *cpu_buffer; + int err = 0; + + if (!cpumask_test_cpu(cpu, buffer->cpumask)) + return -EINVAL; + + cpu_buffer = buffer->buffers[cpu]; + + mutex_lock(&cpu_buffer->mapping_lock); + + if (!cpu_buffer->mapped) { + err = -ENODEV; + goto unlock; + } + + WRITE_ONCE(cpu_buffer->mapped, cpu_buffer->mapped - 1); + if (!cpu_buffer->mapped) { + /* Wait for the writer and readers to observe !mapped */ + synchronize_rcu(); + + rb_free_subbuf_ids(cpu_buffer); + rb_free_meta_page(cpu_buffer); + atomic_dec(&cpu_buffer->resize_disabled); + } +unlock: + mutex_unlock(&cpu_buffer->mapping_lock); + + return err; +} + +/* + * +--------------+ pgoff == 0 + * | meta page | + * +--------------+ pgoff == 1 + * | subbuffer 0 | + * +--------------+ pgoff == 1 + (1 << subbuf_order) + * | subbuffer 1 | + * ... + */ +struct page *ring_buffer_map_fault(struct trace_buffer *buffer, int cpu, + unsigned long pgoff) +{ + struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; + unsigned long subbuf_id, subbuf_offset, addr; + struct page *page; + + if (!pgoff) + return virt_to_page((void *)cpu_buffer->meta_page); + + pgoff--; + + subbuf_id = pgoff >> buffer->subbuf_order; + if (subbuf_id > cpu_buffer->nr_pages) + return NULL; + + subbuf_offset = pgoff & ((1UL << buffer->subbuf_order) - 1); + addr = cpu_buffer->subbuf_ids[subbuf_id] + (subbuf_offset * PAGE_SIZE); + page = virt_to_page((void *)addr); + + return page; +} + +int ring_buffer_map_get_reader(struct trace_buffer *buffer, int cpu) +{ + struct ring_buffer_per_cpu *cpu_buffer; + unsigned long reader_size; + unsigned long flags; + + cpu_buffer = rb_get_mapped_buffer(buffer, cpu); + if (IS_ERR(cpu_buffer)) + return (int)PTR_ERR(cpu_buffer); + + raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags); +consume: + if (rb_per_cpu_empty(cpu_buffer)) + goto out; + + reader_size = rb_page_size(cpu_buffer->reader_page); + + /* + * There are data to be read on the current reader page, we can + * return to the caller. But before that, we assume the latter will read + * everything. Let's update the kernel reader accordingly. + */ + if (cpu_buffer->reader_page->read < reader_size) { + while (cpu_buffer->reader_page->read < reader_size) + rb_advance_reader(cpu_buffer); + goto out; + } + + if (WARN_ON(!rb_get_reader_page(cpu_buffer))) + goto out; + + goto consume; +out: + rb_update_meta_page(cpu_buffer); + raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); + rb_put_mapped_buffer(cpu_buffer); + + return 0; +} + /* * We only allocate new buffers, never free them if the CPU goes down. * If we were to free the buffer, then the user would lose any trace that was in From patchwork Tue Jan 23 11:07:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 13527263 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 03BD45D8F5 for ; Tue, 23 Jan 2024 11:08:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008091; cv=none; b=qnMns1/6goiv6ac++Uo3mkU2pnuQPQ+wHZ7lJIYyw4sJtwjQJCrsVgmB7wvcTi7ZblsTJTWDLsgDY2EteUF+0Be88nDKQ56juQltj0/ZIVCZMJ3ue/QQ6N6cbtPCfDQ+YnAdXcBIrTEziV3FCS/oCdblbmoGGd0nVh9pYqbBdiU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008091; c=relaxed/simple; bh=L2LAOFcpz374aq8+MhwwriEORj7D1Fxmja4Be/sRvZQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ozw+8jeLYz19ZxMX2bB0lOraZwX6mtmOf30MSy9Sm5F98I+/KwlCHvNLnJMdagfLYBMY298S0MztMdoZMvt9303XODQq9sn9KFatb8Ekan9YYN6xzLqzTaqx4apOTC4gob7Gy53/6g7NNYzNavuxdb1cFUTOOGVJ3hErZ/8uISs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ghj0L04f; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ghj0L04f" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-40e9d6a366aso42487585e9.3 for ; Tue, 23 Jan 2024 03:08:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706008088; x=1706612888; darn=vger.kernel.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=C10/Kn1e0NHjWL/pUEbGc+DKo3L2nbmZjaNGz+IRIn0=; b=Ghj0L04fB+IXnYlTr3pePiNnxWIVK8qXhXledyGj3StiR+73dXLqwVbANwlQb11QYV Bj8TnG7VcoptP3MDSscf3rQSf1uDap5X36i0doGDg1A/AvcdJuDOlT5Co4LB78cY3tdx Ccj1Nmu+ak3TNAQP3CNlbOk1Uf6cO9u9eabgLjn4EzAXSK3PxnOIxx9bQXRciovdPRP9 Isfi0SuvrFGhKu7mQ7MdX/hOVROHIsdf7+uhx4FHIRP6qqC47MVskbLDmPU4JTH5PU19 3oynR6+WCJAEFyRhuNhxvXw9Qn3b6G+AkSGb3JxIq4q58hXCRvUPEtf0CUCg+yG4DqkE yddQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706008088; x=1706612888; 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=C10/Kn1e0NHjWL/pUEbGc+DKo3L2nbmZjaNGz+IRIn0=; b=NVH5yJkfj9l58bCf9TDo60RetBeOBkEkpyNxxynsNAC8FcvxdYPqY/L03WpO79lcwq 6h1Azt9o2dDwatThP1jc20n+Y7pmlwVvxAEhKJ6BzvLjq0qIE7JpG+a/5SpnZ0D3Drfy MSANzjBzEKTnARJ+3y1979636w0ozLRSKkJm+939PYEwo4EPiNsHjato64HSeiov6pca RoTk9lwhigVtqcuFdzGntvRXL0Nhjr8+HtIC1ZEavZV1M/AVvTrs8HBoGGpL2qkh6ZHa 0rOBymLGJkIUHwOqXYyuGmeJfuKtdpX38A6kFCxOySj8+Vsm+bJm+bBvZt2wby92SA6X Kljg== X-Gm-Message-State: AOJu0YxetqxIevmf8KE9TI7tjU1259xKlZEGb3WS4XJHwL4B/cEh2qLW GfXhlCNSkZQ7kkQusaPG7R9AALmrrQb/ZNltBNhW8D4mc+FF81OckTSj5rAn88YM0qFiEX0LZNu nTpvorT0dj/uXekdzPA== X-Google-Smtp-Source: AGHT+IETut/KeUEf/hyk0DzZNYHBiDsdkOSjSLyD8KgYdpxYYFNiOA07t2gQdW1neiibQsN+R5V32m0jfeL9hJ8y X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:1d90:b0:40e:b31e:7a5b with SMTP id p16-20020a05600c1d9000b0040eb31e7a5bmr30139wms.0.1706008088208; Tue, 23 Jan 2024 03:08:08 -0800 (PST) Date: Tue, 23 Jan 2024 11:07:54 +0000 In-Reply-To: <20240123110757.3657908-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240123110757.3657908-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123110757.3657908-4-vdonnefort@google.com> Subject: [PATCH v12 3/6] tracing: Add snapshot refcount 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, Vincent Donnefort When a ring-buffer is memory mapped by user-space, no trace or ring-buffer swap is possible. This means the snapshot feature is mutually exclusive with the memory mapping. Having a refcount on snapshot users will help to know if a mapping is possible or not. Signed-off-by: Vincent Donnefort diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 46dbe22121e9..ac59321a8d95 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1300,6 +1300,52 @@ static void free_snapshot(struct trace_array *tr) tr->allocated_snapshot = false; } +static int tracing_arm_snapshot_locked(struct trace_array *tr) +{ + int ret; + + lockdep_assert_held(&trace_types_lock); + + if (tr->snapshot == UINT_MAX) + return -EBUSY; + + ret = tracing_alloc_snapshot_instance(tr); + if (ret) + return ret; + + tr->snapshot++; + + return 0; +} + +static void tracing_disarm_snapshot_locked(struct trace_array *tr) +{ + lockdep_assert_held(&trace_types_lock); + + if (WARN_ON(!tr->snapshot)) + return; + + tr->snapshot--; +} + +int tracing_arm_snapshot(struct trace_array *tr) +{ + int ret; + + mutex_lock(&trace_types_lock); + ret = tracing_arm_snapshot_locked(tr); + mutex_unlock(&trace_types_lock); + + return ret; +} + +void tracing_disarm_snapshot(struct trace_array *tr) +{ + mutex_lock(&trace_types_lock); + tracing_disarm_snapshot_locked(tr); + mutex_unlock(&trace_types_lock); +} + /** * tracing_alloc_snapshot - allocate snapshot buffer. * @@ -1373,10 +1419,6 @@ int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, mutex_lock(&trace_types_lock); - ret = tracing_alloc_snapshot_instance(tr); - if (ret) - goto fail_unlock; - if (tr->current_trace->use_max_tr) { ret = -EBUSY; goto fail_unlock; @@ -1395,6 +1437,10 @@ int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, goto fail_unlock; } + ret = tracing_arm_snapshot_locked(tr); + if (ret) + goto fail_unlock; + local_irq_disable(); arch_spin_lock(&tr->max_lock); tr->cond_snapshot = cond_snapshot; @@ -1439,6 +1485,8 @@ int tracing_snapshot_cond_disable(struct trace_array *tr) arch_spin_unlock(&tr->max_lock); local_irq_enable(); + tracing_disarm_snapshot(tr); + return ret; } EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable); @@ -6579,11 +6627,12 @@ int tracing_set_tracer(struct trace_array *tr, const char *buf) */ synchronize_rcu(); free_snapshot(tr); + tracing_disarm_snapshot_locked(tr); } - if (t->use_max_tr && !tr->allocated_snapshot) { - ret = tracing_alloc_snapshot_instance(tr); - if (ret < 0) + if (t->use_max_tr) { + ret = tracing_arm_snapshot_locked(tr); + if (ret) goto out; } #else @@ -6592,8 +6641,11 @@ int tracing_set_tracer(struct trace_array *tr, const char *buf) if (t->init) { ret = tracer_init(t, tr); - if (ret) + if (ret) { + if (t->use_max_tr) + tracing_disarm_snapshot_locked(tr); goto out; + } } tr->current_trace = t; @@ -7695,10 +7747,11 @@ tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt, if (tr->allocated_snapshot) ret = resize_buffer_duplicate_size(&tr->max_buffer, &tr->array_buffer, iter->cpu_file); - else - ret = tracing_alloc_snapshot_instance(tr); - if (ret < 0) + + ret = tracing_arm_snapshot_locked(tr); + if (ret) break; + /* Now, we're going to swap */ if (iter->cpu_file == RING_BUFFER_ALL_CPUS) { local_irq_disable(); @@ -7708,6 +7761,7 @@ tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt, smp_call_function_single(iter->cpu_file, tracing_swap_cpu_buffer, (void *)tr, 1); } + tracing_disarm_snapshot_locked(tr); break; default: if (tr->allocated_snapshot) { @@ -8832,8 +8886,13 @@ ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash, ops = param ? &snapshot_count_probe_ops : &snapshot_probe_ops; - if (glob[0] == '!') - return unregister_ftrace_function_probe_func(glob+1, tr, ops); + if (glob[0] == '!') { + ret = unregister_ftrace_function_probe_func(glob+1, tr, ops); + if (!ret) + tracing_disarm_snapshot(tr); + + return ret; + } if (!param) goto out_reg; @@ -8852,12 +8911,13 @@ ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash, return ret; out_reg: - ret = tracing_alloc_snapshot_instance(tr); + ret = tracing_arm_snapshot(tr); if (ret < 0) goto out; ret = register_ftrace_function_probe(glob, tr, ops, count); - + if (ret < 0) + tracing_disarm_snapshot(tr); out: return ret < 0 ? ret : 0; } diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 00f873910c5d..3aa06bd5e48d 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -334,6 +334,7 @@ struct trace_array { */ struct array_buffer max_buffer; bool allocated_snapshot; + unsigned int snapshot; #endif #ifdef CONFIG_TRACER_MAX_TRACE unsigned long max_latency; @@ -1973,12 +1974,16 @@ static inline void trace_event_eval_update(struct trace_eval_map **map, int len) #ifdef CONFIG_TRACER_SNAPSHOT void tracing_snapshot_instance(struct trace_array *tr); int tracing_alloc_snapshot_instance(struct trace_array *tr); +int tracing_arm_snapshot(struct trace_array *tr); +void tracing_disarm_snapshot(struct trace_array *tr); #else static inline void tracing_snapshot_instance(struct trace_array *tr) { } static inline int tracing_alloc_snapshot_instance(struct trace_array *tr) { return 0; } +static inline int tracing_arm_snapshot(struct trace_array *tr) { return 0; } +static inline void tracing_disarm_snapshot(struct trace_array *tr) { } #endif #ifdef CONFIG_PREEMPT_TRACER diff --git a/kernel/trace/trace_events_trigger.c b/kernel/trace/trace_events_trigger.c index 46439e3bcec4..d41bf64741e2 100644 --- a/kernel/trace/trace_events_trigger.c +++ b/kernel/trace/trace_events_trigger.c @@ -597,20 +597,9 @@ static int register_trigger(char *glob, return ret; } -/** - * unregister_trigger - Generic event_command @unreg implementation - * @glob: The raw string used to register the trigger - * @test: Trigger-specific data used to find the trigger to remove - * @file: The trace_event_file associated with the event - * - * Common implementation for event trigger unregistration. - * - * Usually used directly as the @unreg method in event command - * implementations. - */ -static void unregister_trigger(char *glob, - struct event_trigger_data *test, - struct trace_event_file *file) +static bool __unregister_trigger(char *glob, + struct event_trigger_data *test, + struct trace_event_file *file) { struct event_trigger_data *data = NULL, *iter; @@ -626,8 +615,32 @@ static void unregister_trigger(char *glob, } } - if (data && data->ops->free) - data->ops->free(data); + if (data) { + if (data->ops->free) + data->ops->free(data); + + return true; + } + + return false; +} + +/** + * unregister_trigger - Generic event_command @unreg implementation + * @glob: The raw string used to register the trigger + * @test: Trigger-specific data used to find the trigger to remove + * @file: The trace_event_file associated with the event + * + * Common implementation for event trigger unregistration. + * + * Usually used directly as the @unreg method in event command + * implementations. + */ +static void unregister_trigger(char *glob, + struct event_trigger_data *test, + struct trace_event_file *file) +{ + __unregister_trigger(glob, test, file); } /* @@ -1470,12 +1483,20 @@ register_snapshot_trigger(char *glob, struct event_trigger_data *data, struct trace_event_file *file) { - if (tracing_alloc_snapshot_instance(file->tr) != 0) + if (tracing_arm_snapshot(file->tr)) return 0; return register_trigger(glob, data, file); } +static void unregister_snapshot_trigger(char *glob, + struct event_trigger_data *data, + struct trace_event_file *file) +{ + if (__unregister_trigger(glob, data, file)) + tracing_disarm_snapshot(file->tr); +} + static int snapshot_trigger_print(struct seq_file *m, struct event_trigger_data *data) { @@ -1508,7 +1529,7 @@ static struct event_command trigger_snapshot_cmd = { .trigger_type = ETT_SNAPSHOT, .parse = event_trigger_parse, .reg = register_snapshot_trigger, - .unreg = unregister_trigger, + .unreg = unregister_snapshot_trigger, .get_trigger_ops = snapshot_get_trigger_ops, .set_filter = set_trigger_filter, }; From patchwork Tue Jan 23 11:07:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 13527264 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 676DB5D900 for ; Tue, 23 Jan 2024 11:08:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008093; cv=none; b=b4knoCIFEGU0WwB0gZrDfpiA92jU4rbRsQcpEC2ktAbIFIqT7L1+Jm6CWWL1Q87wwsPd27EYqLf6OfIfv/6iC1Q201UCCA4MNWvYpiIETgyHae+UNeSZQJYYYfL4WwsQeA4bnsAc+XLjHZdKaz2iHhpQNQAPDz2aJ/mh+cAQlTM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008093; c=relaxed/simple; bh=LpOg75oVVjHVZ5zcMj1Cygednq+3XV9tdhxoOBosZSY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=P6KmHUll8tVYvHoHvZm1Tbkl6ZOPPrQDZtL0gIB5QxIMy2b1WAL6oA9d9D26eq8pIk/F6pqxUv9VuveEe3zUNiC/yaAECzi01YntI3QDlboRpgKEiOZmpLRUlUX+BWAaNN53iIkbakisQTMwWwSFO0+axOE8mdYvyV1oSFS/MHc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=j2q7MsmH; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="j2q7MsmH" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc37425c78eso775817276.2 for ; Tue, 23 Jan 2024 03:08:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706008090; x=1706612890; darn=vger.kernel.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=Dr7C1cm+K+0sbZ2jJ1EOt2NrKO81ZGlkB1FGvQN+/xc=; b=j2q7MsmHrELBsmjUcQ82/8B0F4gwBmxh6LZ2io3YbAwEDEAxJVh3YLR/O0BgEwzqRe j8eAZeLjnbYP0HbcMR4ZCRmyFn5SbvaTx+tHAsNqcYAVC+kWuU6IlBavp2eaFIb8uEJa ptEKS01h8OLFKSK2waPcf5PYPJ2JX9UPHc8zPJs05vDnMrXYse9wSsIdwR4dwcYCDk1f Ib7m0sUqr8wLsqpYj7MRm5rm3wjRf9RBRdCuznL9RThnhMOC0EMXDolAC8j6CVBSdHOz 2lbdlFn04VXQeSgAvVv/vC9YHkBxBT/OmIND+wzih1WaiY1t/WexyGBdeC+SQ+bQ7jyM NmjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706008090; x=1706612890; 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=Dr7C1cm+K+0sbZ2jJ1EOt2NrKO81ZGlkB1FGvQN+/xc=; b=XSlxGpxAvwYnP90X4VyuvZrX/BfePDV3GLaDxa7MNhVnnd8hr3uly/V6x1rLK1XjPV 7Me1QsCCje5CaQbf5qjIW9ADCNn9wv6zPYQM3uaIiz5WH4SI2gmZJGufx3HWOG/qvhwB HxbHu9Bz+xhF27+4dd2jqU57IyHDV2lLBNuCCP8EQJoTAAxiGs5X2Y/lBCc+Qnk9NDN1 MG5QJdUZo0HbDn5Nsf5tevTaEvExu7WblxyWUf5nfHo0+T369HoOUQ5oFSz4lt1kI6id kTX2In9l9tOXJHKKlbFrh7nObLeYv5I5d5GxddL5/lFdliQ+d5lR5EKMxUeytEyan5ca 9UMA== X-Gm-Message-State: AOJu0YxvaQNrmVl2ALWIH/bQW9BbCd9cfVadSSHPPpqV+yfYahLjlGBh 6cSk6gZsF2+XxoM86fwkcjNMnPb1XNDMpeN5gdl/W7k/2jdxyXLlstsTEO43bJTKW5ERea9iyBc Y8INeOxUQk35B9q9SRg== X-Google-Smtp-Source: AGHT+IFIyDVggOlHUuSF0i46wk6Y9mpErXwWsnI9RT508ATRbK2lbJzxqsXgHRS4RWrxxOVJKckQ9qtccIT8EXCj X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:6902:1b12:b0:db5:4766:e363 with SMTP id eh18-20020a0569021b1200b00db54766e363mr2514419ybb.6.1706008090555; Tue, 23 Jan 2024 03:08:10 -0800 (PST) Date: Tue, 23 Jan 2024 11:07:55 +0000 In-Reply-To: <20240123110757.3657908-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240123110757.3657908-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123110757.3657908-5-vdonnefort@google.com> Subject: [PATCH v12 4/6] 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, Vincent Donnefort 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. Signed-off-by: Vincent Donnefort diff --git a/include/uapi/linux/trace_mmap.h b/include/uapi/linux/trace_mmap.h index 5468afc94be7..09c4ae6d6e71 100644 --- a/include/uapi/linux/trace_mmap.h +++ b/include/uapi/linux/trace_mmap.h @@ -41,4 +41,6 @@ struct trace_buffer_meta { __u64 subbufs_lost; }; +#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 ac59321a8d95..eb9e829ecc12 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1175,6 +1175,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); @@ -1309,6 +1315,9 @@ static int tracing_arm_snapshot_locked(struct trace_array *tr) if (tr->snapshot == UINT_MAX) return -EBUSY; + if (tr->mapped) + return -EBUSY; + ret = tracing_alloc_snapshot_instance(tr); if (ret) return ret; @@ -6520,7 +6529,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) @@ -8637,15 +8646,31 @@ 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); + 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); iter->wait_index++; @@ -8658,6 +8683,90 @@ static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned return 0; } +static vm_fault_t tracing_buffers_mmap_fault(struct vm_fault *vmf) +{ + struct ftrace_buffer_info *info = vmf->vma->vm_file->private_data; + struct trace_iterator *iter = &info->iter; + vm_fault_t ret = VM_FAULT_SIGBUS; + struct page *page; + + page = ring_buffer_map_fault(iter->array_buffer->buffer, iter->cpu_file, + vmf->pgoff); + if (!page) + return ret; + + get_page(page); + vmf->page = page; + vmf->page->mapping = vmf->vma->vm_file->f_mapping; + vmf->page->index = vmf->pgoff; + + return 0; +} + +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; + + ring_buffer_unmap(iter->array_buffer->buffer, iter->cpu_file); + +#ifdef CONFIG_TRACER_MAX_TRACE + mutex_lock(&trace_types_lock); + if (!WARN_ON(!iter->tr->mapped)) + iter->tr->mapped--; + mutex_unlock(&trace_types_lock); +#endif +} + +static void tracing_buffers_mmap_open(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_map(iter->array_buffer->buffer, iter->cpu_file)); +} + +static const struct vm_operations_struct tracing_buffers_vmops = { + .open = tracing_buffers_mmap_open, + .close = tracing_buffers_mmap_close, + .fault = tracing_buffers_mmap_fault, +}; + +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) + return -EPERM; + + vm_flags_mod(vma, VM_DONTCOPY | VM_DONTDUMP, VM_MAYWRITE); + vma->vm_ops = &tracing_buffers_vmops; + +#ifdef CONFIG_TRACER_MAX_TRACE + mutex_lock(&trace_types_lock); + if (iter->tr->snapshot || iter->tr->mapped == INT_MAX) { + ret = -EBUSY; + goto unlock; + } + + /* Wait for update_max_tr() to observe iter->tr->mapped */ + if (!iter->tr->mapped++) + synchronize_rcu(); +#endif + + ret = ring_buffer_map(iter->array_buffer->buffer, iter->cpu_file); + +#ifdef CONFIG_TRACER_MAX_TRACE + if (ret) + iter->tr->mapped--; +unlock: + mutex_unlock(&trace_types_lock); +#endif + return ret; +} + static const struct file_operations tracing_buffers_fops = { .open = tracing_buffers_open, .read = tracing_buffers_read, @@ -8666,6 +8775,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 3aa06bd5e48d..25ec246d5f1e 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -335,6 +335,7 @@ struct trace_array { struct array_buffer max_buffer; bool allocated_snapshot; unsigned int snapshot; + int mapped; #endif #ifdef CONFIG_TRACER_MAX_TRACE unsigned long max_latency; From patchwork Tue Jan 23 11:07:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 13527265 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 766BA5D72F for ; Tue, 23 Jan 2024 11:08:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008096; cv=none; b=Nntgs5JNs3OOtqGr6YLRCqhqlmrFqvWVw3edgSo/ei+lSuM7RkdjeQNRKbgO4sKKBfObpraWncbSQB5j+ENAXU4e67gCAURdA0mSq4+aRP8OFIzolS23werpJt7TUZZRk38OmyfSj6Cr9waSt6bUvcwuYFjwnWjzGtwZGAnkemI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008096; c=relaxed/simple; bh=2ydE7p4I+jn1AqXuKc/xAoa8ZK8YfR3uygNBDatj3o4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cmwH4qjEXicTLqehB+aqRTcIEIF0dgeHW0bsVt98Agfaiz3INxmrweDu9J8gdcg4u2FjUv5XLCiLQ7spyqrQnZOH40PMlbLo4Y6nf4f7aI6iF7JjaiOflX29Sgwj/8Fe39ufX/4H3nvFqq8GbDR7+7TYRoybbDq+y6p5Bnbxe/I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eDtJL47Z; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eDtJL47Z" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-33926c83a2cso1916129f8f.0 for ; Tue, 23 Jan 2024 03:08:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706008093; x=1706612893; darn=vger.kernel.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=W7oPXmEt7YsbEzKepN7rYv6YN542PMGBpLDCQVs56QI=; b=eDtJL47Z++DAwOfHG1DGRJOoXqG7PzQBIMI8F89Xp6bYqUNkogJ6JuNouzMXS41h+s oDoGX0CziDo+iE8w+TaWUs6smSuwSJueJbcLfai6tJUJtLvJbkxcfbS/Jg4vWDZCsSKi I13ZrZT3xXnQGRrmuDYySQfhQWX45ayitTMvUDXLQ4zvozTFrretALDq//EmInF3WFR5 RdGURbixEtoc8AzLz7JvBl52bB2pnR7LD8ELiOcZgynLgWVcGcBr/magEnVxk4XkV658 17fdV65mZ4LUfc6cnEbo4rINAD5fGtWff1lKKapH9S+mrj0NcUd8okcOZuwXlCT4mGh3 C8Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706008093; x=1706612893; 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=W7oPXmEt7YsbEzKepN7rYv6YN542PMGBpLDCQVs56QI=; b=hc+SV3lLHQ4/VRGlxdK2CxLdex8tvN6V/+mRyqrcllRqBXzIY5wq3aRIH/dO8CCq3L vjblnBwIxmddYvCWWrX/ABMWMMlk0VuN0NFnRko1Ja8cDJCF8nIebWnCEKY+17Y+iP/U S5AnUJcJ25VV4k/dh2GqIW1gjMldirvdcdJN12RHz4XMcezFJ3Q54o3fr5z5WMVViqnb ECCaVVp3LutI9JV1BlZqVkyCSiCW6xLEqtLnZW3z0p4U5P5aVX4TL4tuRW0ynothN/Wo WG4DsJvrR7SfhP5cuM7NszMoAa3qNa76HDY4UdZui7SHwQy1N7+u5R9J0olucktPGZx4 BvzA== X-Gm-Message-State: AOJu0YyI4JMfUpq4GL+8uq21GK25lZG+Yr1SUI6AZq+h1hAJCgyuytbA xOfLJEPavlCAlTWkluk6rymwsJmfTRYYUgtujysbh3KP7aFpmHim7hoHk9MIoz9y805j4Hteyy1 +Xl47ks3bdm5AmiY76w== X-Google-Smtp-Source: AGHT+IHEJGbZrETgw559I9/W3u+W2YGFU+Oe4jSe7q7hW4mNL2TMFi4oaAiUTwtfI2L+rZVdaj4sAlJfb2Z/KvN4 X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:6000:71a:b0:339:22b6:b56f with SMTP id bs26-20020a056000071a00b0033922b6b56fmr23400wrb.0.1706008092731; Tue, 23 Jan 2024 03:08:12 -0800 (PST) Date: Tue, 23 Jan 2024 11:07:56 +0000 In-Reply-To: <20240123110757.3657908-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240123110757.3657908-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123110757.3657908-6-vdonnefort@google.com> Subject: [PATCH v12 5/6] Documentation: tracing: Add ring-buffer mapping 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, Vincent Donnefort It is now possible to mmap() a ring-buffer to stream its content. Add some documentation and a code example. Signed-off-by: Vincent Donnefort diff --git a/Documentation/trace/index.rst b/Documentation/trace/index.rst index 5092d6c13af5..0b300901fd75 100644 --- a/Documentation/trace/index.rst +++ b/Documentation/trace/index.rst @@ -29,6 +29,7 @@ Linux Tracing Technologies timerlat-tracer intel_th ring-buffer-design + ring-buffer-map stm sys-t coresight/index diff --git a/Documentation/trace/ring-buffer-map.rst b/Documentation/trace/ring-buffer-map.rst new file mode 100644 index 000000000000..7d7513180893 --- /dev/null +++ b/Documentation/trace/ring-buffer-map.rst @@ -0,0 +1,106 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================================== +Tracefs ring-buffer memory mapping +================================== + +:Author: Vincent Donnefort + +Overview +======== +Tracefs ring-buffer memory map provides an efficient method to stream data +as no memory copy is necessary. The application mapping the ring-buffer becomes +then a consumer for that ring-buffer, in a similar fashion to trace_pipe. + +Memory mapping setup +==================== +The mapping works with a mmap() of the trace_pipe_raw interface. + +The first system page of the mapping contains ring-buffer statistics and +description. It is referred as the meta-page. One of the most important field of +the meta-page is the reader. It contains the subbuf ID which can be safely read +by the mapper (see ring-buffer-design.rst). + +The meta-page is followed by all the subbuf, ordered by ascendant ID. It is +therefore effortless to know where the reader starts in the mapping: + +.. code-block:: c + + reader_id = meta->reader->id; + reader_offset = meta->meta_page_size + reader_id * meta->subbuf_size; + +When the application is done with the current reader, it can get a new one using +the trace_pipe_raw ioctl() TRACE_MMAP_IOCTL_GET_READER. This ioctl also updates +the meta-page fields. + +Limitations +=========== +When a mapping is in place on a Tracefs ring-buffer, it is not possible to +either resize it (either by increasing the entire size of the ring-buffer or +each subbuf). It is also not possible to use snapshot or splice. + +Concurrent readers (either another application mapping that ring-buffer or the +kernel with trace_pipe) are allowed but not recommended. They will compete for +the ring-buffer and the output is unpredictable. + +Example +======= + +.. code-block:: c + + #include + #include + #include + #include + + #include + + #include + #include + + #define TRACE_PIPE_RAW "/sys/kernel/tracing/per_cpu/cpu0/trace_pipe_raw" + + int main(void) + { + int page_size = getpagesize(), fd, reader_id; + unsigned long meta_len, data_len; + struct trace_buffer_meta *meta; + void *map, *reader, *data; + + fd = open(TRACE_PIPE_RAW, O_RDONLY | O_NONBLOCK); + if (fd < 0) + exit(EXIT_FAILURE); + + map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0); + if (map == MAP_FAILED) + exit(EXIT_FAILURE); + + meta = (struct trace_buffer_meta *)map; + meta_len = meta->meta_page_size; + + printf("entries: %llu\n", meta->entries); + printf("overrun: %llu\n", meta->overrun); + printf("read: %llu\n", meta->read); + printf("subbufs_touched:%llu\n", meta->subbufs_touched); + printf("subbufs_lost: %llu\n", meta->subbufs_lost); + printf("nr_subbufs: %u\n", meta->nr_subbufs); + + data_len = meta->subbuf_size * meta->nr_subbufs; + data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, meta_len); + if (data == MAP_FAILED) + exit(EXIT_FAILURE); + + if (ioctl(fd, TRACE_MMAP_IOCTL_GET_READER) < 0) + exit(EXIT_FAILURE); + + reader_id = meta->reader.id; + reader = data + meta->subbuf_size * reader_id; + + printf("Current reader address: %p\n", reader); + + munmap(data, data_len); + munmap(meta, meta_len); + close (fd); + + return 0; + } From patchwork Tue Jan 23 11:07:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Donnefort X-Patchwork-Id: 13527266 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F10035D8F6 for ; Tue, 23 Jan 2024 11:08:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008098; cv=none; b=D35fkDiPO3quyvtWcP/mNw5z9OE0og8rL9a5cso71zZdQBi2upbGLysHNOaU6LV2eQK4M/sAXsVhbqqX6W4Y8BlnkHTvbGqix8/U37D2fDZu6hF2RSvU5+dmS8F1qwVQVQ9JTocKyLGBLxI/WNwCaljzJEioOx2nfg+Y6LY+UXY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706008098; c=relaxed/simple; bh=jzpwCo0HMVI99saI3KiX/DjK1lxlN166EymbCtXFVp8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DuSS/y3feCn+tc2Bq6ZTq7XeWEZYAoOff3ztLQTahv6HSCXH5EwcEpbuUn2qhilAW8pPrIRv1SSlfOQp1LMViZiwVUSaWoFY0MwIEOJYZOMMMcBQkApzFKmtO+X0ibk2uIYTFEzwk6j5SgxdzgGpv8H1nJZ+uog67RY1pQJuayQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=21Y1a1Hg; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="21Y1a1Hg" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-40e9d6a364eso34663725e9.3 for ; Tue, 23 Jan 2024 03:08:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706008095; x=1706612895; darn=vger.kernel.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=on32GNvkxWDILvYMfSVIpQ/Zr8fKHLMLlVw8UqZ+BYg=; b=21Y1a1HgqV17XDk72W8zaiQUgFOpz+A3umWCn6OacqzZ/FZgortPr4j30Gf+isxEfW +KSZSVeYFt6Ha69275CbVHA6s97JnciucZciv7lyhjfTt4LaXAbfwsj3EVV4r+KWgIiT HTSmNka/4NKIVB5b6vSgF/27Vie6QhFl6xkQtzV81T6iTx0+EthCA3WYGhNReiOHfQAY y1XCNYPukpRu0AI7NvaGLQZjsP8SqXGFF2lq0/BmZIDHmceyE7JwGirmmZAB8kBb4IFX gGN58ITwZB0UAOSlTUxZRG1Zz29S58OdfdFoLAc57ko0dBDjl0KOCTqKRjPgaIK+FjGd f11g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706008095; x=1706612895; 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=on32GNvkxWDILvYMfSVIpQ/Zr8fKHLMLlVw8UqZ+BYg=; b=skiimRs79FABMyFI8G1cByaSK874QYgjxVvWnODu8+laTivgHs7UaKQjUYXrwDUJRw mm/z6z9Y/PLY3LOSzYeWd9jU3XYRQ7J6QxXLcm+tg9/OhMfvHS39dKpjtG+qoPjpOmfx p53HolubipPwr87oCRuoTwGFXIBcJ/b8ijnOSu5rIJGSFkhxDVPt0CKgWCFzUHcb5uFj Yr4bGk4HCjJTPisFeKTMYU1uWvFINUa5CTXAWVvAv3BDoHiHMRpHlWg31oPMDjacEZ2S sb5MvwWG6CuCZwVldgMNt1auzWPlMUCTW4tS3L0fRJR8JudvFYcwYZAYvENQAx4DTmJb mwvQ== X-Gm-Message-State: AOJu0Yzl2yUMRXN+p6+sWQkCdtsFNMuZg20Fq9kyxwUfwPVwLqSrwd8Q RL7tjejbwYQJCzHeWOAVtMIHlxKEDoREg8qLG2L5QPzfvXMmXuSqKUey2507GjuEF1Gr6cWmyjO aondt6aGTSK+UDHqbLA== X-Google-Smtp-Source: AGHT+IFczxSO95adCCP1/OQdMVdXuAbrhocNenUfZEJQ6Y/inr3mrpYKphpQLoisODALT2LTe4UZKmi4jxqxDWFY X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:600c:5104:b0:40d:8587:3399 with SMTP id o4-20020a05600c510400b0040d85873399mr13616wms.6.1706008095318; Tue, 23 Jan 2024 03:08:15 -0800 (PST) Date: Tue, 23 Jan 2024 11:07:57 +0000 In-Reply-To: <20240123110757.3657908-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240123110757.3657908-1-vdonnefort@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123110757.3657908-7-vdonnefort@google.com> Subject: [PATCH v12 6/6] ring-buffer/selftest: Add ring-buffer mapping test 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, Vincent Donnefort , Shuah Khan , Shuah Khan , linux-kselftest@vger.kernel.org This test maps a ring-buffer and validate the meta-page after reset and after emitting few events. Cc: Shuah Khan Cc: Shuah Khan Cc: linux-kselftest@vger.kernel.org Signed-off-by: Vincent Donnefort diff --git a/tools/testing/selftests/ring-buffer/Makefile b/tools/testing/selftests/ring-buffer/Makefile new file mode 100644 index 000000000000..627c5fa6d1ab --- /dev/null +++ b/tools/testing/selftests/ring-buffer/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 +CFLAGS += -Wl,-no-as-needed -Wall +CFLAGS += $(KHDR_INCLUDES) +CFLAGS += -D_GNU_SOURCE + +TEST_GEN_PROGS = map_test + +include ../lib.mk diff --git a/tools/testing/selftests/ring-buffer/config b/tools/testing/selftests/ring-buffer/config new file mode 100644 index 000000000000..ef8214661612 --- /dev/null +++ b/tools/testing/selftests/ring-buffer/config @@ -0,0 +1 @@ +CONFIG_FTRACE=y diff --git a/tools/testing/selftests/ring-buffer/map_test.c b/tools/testing/selftests/ring-buffer/map_test.c new file mode 100644 index 000000000000..ec891e956b83 --- /dev/null +++ b/tools/testing/selftests/ring-buffer/map_test.c @@ -0,0 +1,188 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ring-buffer memory mapping tests + * + * Copyright (c) 2024 Vincent Donnefort + */ +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include "../user_events/user_events_selftests.h" /* share tracefs setup */ +#include "../kselftest_harness.h" + +#define TRACEFS_ROOT "/sys/kernel/tracing" + +static int __tracefs_write(const char *path, const char *value) +{ + FILE *file; + + file = fopen(path, "w"); + if (!file) + return -1; + + fputs(value, file); + fclose(file); + + return 0; +} + +static int __tracefs_write_int(const char *path, int value) +{ + char *str; + int ret; + + if (asprintf(&str, "%d", value) < 0) + return -1; + + ret = __tracefs_write(path, str); + + free(str); + + return ret; +} + +#define tracefs_write_int(path, value) \ + ASSERT_EQ(__tracefs_write_int((path), (value)), 0) + +static int tracefs_reset(void) +{ + if (__tracefs_write_int(TRACEFS_ROOT"/tracing_on", 0)) + return -1; + if (__tracefs_write_int(TRACEFS_ROOT"/trace", 0)) + return -1; + if (__tracefs_write(TRACEFS_ROOT"/set_event", "")) + return -1; + if (__tracefs_write(TRACEFS_ROOT"/current_tracer", "nop")) + return -1; + + return 0; +} + +FIXTURE(map) { + struct trace_buffer_meta *meta; + void *data; + int cpu_fd; + bool umount; +}; + +FIXTURE_VARIANT(map) { + int subbuf_size; +}; + +FIXTURE_VARIANT_ADD(map, subbuf_size_4k) { + .subbuf_size = 4, +}; + +FIXTURE_VARIANT_ADD(map, subbuf_size_8k) { + .subbuf_size = 8, +}; + +FIXTURE_SETUP(map) +{ + int cpu = sched_getcpu(), page_size = getpagesize(); + unsigned long meta_len, data_len; + char *cpu_path, *message; + bool fail, umount; + cpu_set_t cpu_mask; + void *map; + + if (!tracefs_enabled(&message, &fail, &umount)) { + if (fail) { + TH_LOG("Tracefs setup failed: %s", message); + ASSERT_FALSE(fail); + } + SKIP(return, "Skipping: %s", message); + } + + self->umount = umount; + + ASSERT_GE(cpu, 0); + + ASSERT_EQ(tracefs_reset(), 0); + + tracefs_write_int(TRACEFS_ROOT"/buffer_subbuf_size_kb", variant->subbuf_size); + + ASSERT_GE(asprintf(&cpu_path, + TRACEFS_ROOT"/per_cpu/cpu%d/trace_pipe_raw", + cpu), 0); + + self->cpu_fd = open(cpu_path, O_RDONLY | O_NONBLOCK); + ASSERT_GE(self->cpu_fd, 0); + free(cpu_path); + + map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, self->cpu_fd, 0); + ASSERT_NE(map, MAP_FAILED); + self->meta = (struct trace_buffer_meta *)map; + + meta_len = self->meta->meta_page_size; + data_len = self->meta->subbuf_size * self->meta->nr_subbufs; + + map = mmap(NULL, data_len, PROT_READ, MAP_SHARED, self->cpu_fd, meta_len); + ASSERT_NE(map, MAP_FAILED); + self->data = map; + + /* + * Ensure generated events will be found on this very same ring-buffer. + */ + CPU_ZERO(&cpu_mask); + CPU_SET(cpu, &cpu_mask); + ASSERT_EQ(sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask), 0); +} + +FIXTURE_TEARDOWN(map) +{ + tracefs_reset(); + + if (self->umount) + tracefs_unmount(); + + munmap(self->data, self->meta->subbuf_size * self->meta->nr_subbufs); + munmap(self->meta, self->meta->meta_page_size); + close(self->cpu_fd); +} + +TEST_F(map, meta_page_check) +{ + int cnt = 0; + + ASSERT_EQ(self->meta->entries, 0); + ASSERT_EQ(self->meta->overrun, 0); + ASSERT_EQ(self->meta->read, 0); + ASSERT_EQ(self->meta->subbufs_touched, 0); + ASSERT_EQ(self->meta->subbufs_lost, 0); + + ASSERT_EQ(self->meta->reader.id, 0); + ASSERT_EQ(self->meta->reader.read, 0); + + ASSERT_EQ(ioctl(self->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0); + ASSERT_EQ(self->meta->reader.id, 0); + + tracefs_write_int(TRACEFS_ROOT"/tracing_on", 1); + for (int i = 0; i < 16; i++) + tracefs_write_int(TRACEFS_ROOT"/trace_marker", i); +again: + ASSERT_EQ(ioctl(self->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0); + + ASSERT_EQ(self->meta->entries, 16); + ASSERT_EQ(self->meta->overrun, 0); + ASSERT_EQ(self->meta->read, 16); + /* subbufs_touched doesn't take into account the commit page */ + ASSERT_EQ(self->meta->subbufs_touched, 0); + ASSERT_EQ(self->meta->subbufs_lost, 0); + + ASSERT_EQ(self->meta->reader.id, 1); + + if (!(cnt++)) + goto again; +} + +TEST_HARNESS_MAIN