All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] Introducing trace buffer mapping by user-space
@ 2023-03-17 14:33 Vincent Donnefort
  2023-03-17 14:33 ` [PATCH 1/2] ring-buffer: Introducing ring-buffer mapping functions Vincent Donnefort
  2023-03-17 14:33 ` [PATCH 2/2] tracing: Allow user-space mapping of the ring-buffer Vincent Donnefort
  0 siblings, 2 replies; 11+ messages in thread
From: Vincent Donnefort @ 2023-03-17 14:33 UTC (permalink / raw)
  To: rostedt, mhiramat, linux-kernel, linux-trace-kernel
  Cc: kernel-team, Vincent Donnefort

The tracing ring-buffers can be stored on disk or sent to network without any
copy via splice. However the later doesn't allow real time processing of the
traces. A solution is to give access to userspace to the ring-buffer pages
directly via a mapping. A piece of software can now become a reader of the
ring-buffer, and drive a consuming or non-consuming read in a similar fashion to
what trace and trace_pipe offer.

Attached to this cover letter an example of consuming read for a ring-buffer,
using libtracefs.

Vincent

--

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <tracefs.h>
#include <kbuffer.h>
#include <event-parse.h>

#include <asm/types.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#define TRACE_MMAP_IOCTL_GET_READER_PAGE	_IO('T', 0x1)

struct ring_buffer_meta_page {
	__u64		entries;
	__u64		overrun;
	__u32		pages_touched;
	__u32		reader_page;
	__u32		nr_data_pages;
	__u32		data_page_head;
	__u32		data_pages[];
};

/* Need to access private struct to save counters */
struct kbuffer {
	unsigned long long 	timestamp;
	long long		lost_events;
	unsigned long		flags;
	void			*subbuffer;
	void			*data;
	unsigned int		index;
	unsigned int		curr;
	unsigned int		next;
	unsigned int		size;
	unsigned int		start;
	unsigned int		first;

	unsigned int (*read_4)(void *ptr);
	unsigned long long (*read_8)(void *ptr);
	unsigned long long (*read_long)(struct kbuffer *kbuf, void *ptr);
	int (*next_event)(struct kbuffer *kbuf);
};

static char *argv0;
static bool need_exit;

static char *get_this_name(void)
{
	static char *this_name;
	char *arg;
	char *p;

	if (this_name)
		return this_name;

	arg = argv0;
	p = arg+strlen(arg);

	while (p >= arg && *p != '/')
		p--;
	p++;

	this_name = p;
	return p;
}

static void __vdie(const char *fmt, va_list ap, int err)
{
	int ret = errno;
	char *p = get_this_name();

	if (err && errno)
		perror(p);
	else
		ret = -1;

	fprintf(stderr, "  ");
	vfprintf(stderr, fmt, ap);

	fprintf(stderr, "\n");
	exit(ret);
}

void pdie(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	__vdie(fmt, ap, 1);
	va_end(ap);
}

static void read_page(struct tep_handle *tep, struct kbuffer *kbuf,
		      void *data, int page)
{
	static struct trace_seq seq;
	struct tep_record record;

	if (seq.buffer)
		trace_seq_reset(&seq);
	else
		trace_seq_init(&seq);

	while ((record.data = kbuffer_read_event(kbuf, &record.ts))) {
		kbuffer_next_event(kbuf, NULL);
		tep_print_event(tep, &seq, &record,
				"%s-%d %9d\t%s\n", TEP_PRINT_COMM,
				TEP_PRINT_PID, TEP_PRINT_TIME, TEP_PRINT_NAME);
		trace_seq_do_printf(&seq);
		trace_seq_reset(&seq);
	}
}

static int next_reader_page(int fd, struct ring_buffer_meta_page *meta,
			    struct kbuffer *kbuf)
{
	int prev_reader_page = meta->reader_page;

	if (ioctl(fd, TRACE_MMAP_IOCTL_GET_READER_PAGE) < 0)
		pdie("ioctl");

	return meta->reader_page;
}

static void signal_handler(int unused)
{
	printf("Exit!\n");
	need_exit = true;
}

int main(int argc, char **argv)
{
	int page_size, data_len, page, fd, start = -1;
	struct ring_buffer_meta_page *map;
	struct kbuffer *kbuf, prev_kbuf;
	struct tep_handle *tep;
	__u64 prev_entries;
	void *meta, *data;
	char *buf, path[32];
	int cpu;

	argv0 = argv[0];
	cpu = atoi(argv[1]);
	snprintf(path, 32, "per_cpu/cpu%d/trace_pipe_raw", cpu);

	tep = tracefs_local_events(NULL);
	kbuf = tep_kbuffer(tep);
	page_size = getpagesize();

	fd = tracefs_instance_file_open(NULL, path, O_RDONLY);
	if (fd < 0)
		pdie("raw");

	meta = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0);
	if (meta == MAP_FAILED)
		pdie("mmap");
	map = meta;

	printf("entries:	%llu\n", map->entries);
	printf("overrun:	%llu\n", map->overrun);
	printf("pages_touched:	%u\n", map->pages_touched);
	printf("reader_page:	%u\n", map->reader_page);
	printf("nr_data_pages:	%u\n\n", map->nr_data_pages);

	data_len = page_size * (map->nr_data_pages + 1);

	data = mmap(NULL, data_len, PROT_READ, MAP_SHARED, fd, page_size);
	if (data == MAP_FAILED)
		pdie("mmap data");

	signal(SIGINT, signal_handler);

	page = ((struct ring_buffer_meta_page*)meta)->reader_page;
again:
	do {
		kbuffer_load_subbuffer(kbuf, data + page_size * page);

		if (page != start) {
			printf("READER PAGE: %d\n", map->reader_page);
		} else {
			kbuf->curr = prev_kbuf.curr;
			kbuf->index = prev_kbuf.index;
			kbuf->next = prev_kbuf.next;
			kbuf->timestamp = prev_kbuf.timestamp;
			kbuffer_next_event(kbuf, NULL);
		}

		prev_entries = map->entries;
		start = page;

		read_page(tep, kbuf, data, page);
	} while ((page = next_reader_page(fd, meta, kbuf)) != start);

	prev_kbuf.curr = kbuf->curr;
	prev_kbuf.index = kbuf->index;
	prev_kbuf.next = kbuf->next;
	prev_kbuf.timestamp = kbuf->timestamp;

	while (prev_entries == *(volatile __u64 *)&map->entries && !need_exit)
		usleep(100000);

	if (!need_exit)
		goto again;

	munmap(data, data_len);
	munmap(meta, page_size);
	close(fd);

	return 0;
}

Vincent Donnefort (2):
  ring-buffer: Introducing ring-buffer mapping functions
  tracing: Allow user-space mapping of the ring-buffer

 include/linux/ring_buffer.h     |   8 +
 include/uapi/linux/trace_mmap.h |  27 +++
 kernel/trace/ring_buffer.c      | 334 +++++++++++++++++++++++++++++++-
 kernel/trace/trace.c            |  73 ++++++-
 4 files changed, 436 insertions(+), 6 deletions(-)
 create mode 100644 include/uapi/linux/trace_mmap.h

-- 
2.40.0.rc1.284.g88254d51c5-goog


^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: [PATCH 1/2] ring-buffer: Introducing ring-buffer mapping functions
@ 2023-03-18  3:00 kernel test robot
  0 siblings, 0 replies; 11+ messages in thread
From: kernel test robot @ 2023-03-18  3:00 UTC (permalink / raw)
  To: oe-kbuild; +Cc: lkp, Dan Carpenter

BCC: lkp@intel.com
CC: oe-kbuild-all@lists.linux.dev
In-Reply-To: <20230317143310.1604700-2-vdonnefort@google.com>
References: <20230317143310.1604700-2-vdonnefort@google.com>
TO: Vincent Donnefort <vdonnefort@google.com>
TO: rostedt@goodmis.org
TO: mhiramat@kernel.org
TO: linux-kernel@vger.kernel.org
TO: linux-trace-kernel@vger.kernel.org
CC: kernel-team@android.com
CC: Vincent Donnefort <vdonnefort@google.com>

Hi Vincent,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on rostedt-trace/for-next v6.3-rc2 next-20230317]
[cannot apply to rostedt-trace/for-next-urgent]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Vincent-Donnefort/ring-buffer-Introducing-ring-buffer-mapping-functions/20230317-223437
patch link:    https://lore.kernel.org/r/20230317143310.1604700-2-vdonnefort%40google.com
patch subject: [PATCH 1/2] ring-buffer: Introducing ring-buffer mapping functions
:::::: branch date: 12 hours ago
:::::: commit date: 12 hours ago
config: i386-randconfig-m021 (https://download.01.org/0day-ci/archive/20230318/202303181001.bu7VCBhF-lkp@intel.com/config)
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>
| Reported-by: Dan Carpenter <error27@gmail.com>
| Link: https://lore.kernel.org/r/202303181001.bu7VCBhF-lkp@intel.com/

smatch warnings:
kernel/trace/ring_buffer.c:6137 ring_buffer_get_reader_page() warn: passing a valid pointer to 'PTR_ERR'
kernel/trace/ring_buffer.c:5996 rb_get_mapped_buffer() warn: inconsistent returns '&cpu_buffer->mapping_lock'.
kernel/trace/ring_buffer.c:6158 ring_buffer_update_meta_page() warn: passing a valid pointer to 'PTR_ERR'

vim +/PTR_ERR +6137 kernel/trace/ring_buffer.c

e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5978  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5979  static inline struct ring_buffer_per_cpu *
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5980  rb_get_mapped_buffer(struct trace_buffer *buffer, int cpu)
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5981  {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5982  	struct ring_buffer_per_cpu *cpu_buffer;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5983  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5984  	if (!cpumask_test_cpu(cpu, buffer->cpumask))
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5985  		return ERR_PTR(-EINVAL);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5986  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5987  	cpu_buffer = buffer->buffers[cpu];
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5988  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5989  	mutex_lock(&cpu_buffer->mapping_lock);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5990  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5991  	if (!cpu_buffer->mapped) {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5992  		mutex_unlock(&cpu_buffer->mapping_lock);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5993  		return ERR_PTR(-ENODEV);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5994  	}
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5995  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17 @5996  	return cpu_buffer;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5997  }
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5998  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  5999  static inline void rb_put_mapped_buffer(struct ring_buffer_per_cpu *cpu_buffer)
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6000  {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6001  	mutex_unlock(&cpu_buffer->mapping_lock);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6002  }
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6003  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6004  int ring_buffer_map(struct trace_buffer *buffer, int cpu)
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6005  {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6006  	struct ring_buffer_per_cpu *cpu_buffer;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6007  	unsigned long flags, *page_ids;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6008  	int err = 0;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6009  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6010  	if (!cpumask_test_cpu(cpu, buffer->cpumask))
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6011  		return -EINVAL;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6012  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6013  	cpu_buffer = buffer->buffers[cpu];
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6014  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6015  	mutex_lock(&cpu_buffer->mapping_lock);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6016  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6017  	if (cpu_buffer->mapped) {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6018  		WRITE_ONCE(cpu_buffer->mapped, cpu_buffer->mapped + 1);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6019  		goto unlock;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6020  	}
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6021  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6022  	/* prevent another thread from changing buffer sizes */
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6023  	mutex_lock(&buffer->mutex);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6024  	atomic_inc(&cpu_buffer->resize_disabled);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6025  	mutex_unlock(&buffer->mutex);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6026  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6027  	err = rb_alloc_meta_page(cpu_buffer);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6028  	if (err) {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6029  		atomic_dec(&cpu_buffer->resize_disabled);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6030  		goto unlock;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6031  	}
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6032  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6033  	/* page_ids include the reader page while nr_pages does not */
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6034  	page_ids = kzalloc(sizeof(*page_ids) * (cpu_buffer->nr_pages + 1),
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6035  			   GFP_KERNEL);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6036  	if (!page_ids) {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6037  		rb_free_meta_page(cpu_buffer);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6038  		atomic_dec(&cpu_buffer->resize_disabled);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6039  		err = -ENOMEM;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6040  		goto unlock;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6041  	}
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6042  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6043  	/*
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6044  	 * Lock all readers to block any page swap until the page IDs are
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6045  	 * assigned.
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6046  	 */
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6047  	raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6048  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6049  	rb_setup_ids_meta_page(cpu_buffer, page_ids);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6050  	/*
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6051  	 * Ensure the writer will observe the meta-page before
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6052  	 * cpu_buffer->mapped.
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6053  	 */
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6054  	smp_wmb();
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6055  	WRITE_ONCE(cpu_buffer->mapped, 1);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6056  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6057  	/* Init meta_page values unless the writer did it already */
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6058  	cmpxchg(&cpu_buffer->meta_page->entries, 0,
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6059  		local_read(&cpu_buffer->entries));
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6060  	cmpxchg(&cpu_buffer->meta_page->overrun, 0,
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6061  		local_read(&cpu_buffer->overrun));
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6062  	cmpxchg(&cpu_buffer->meta_page->pages_touched, 0,
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6063  		local_read(&cpu_buffer->pages_touched));
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6064  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6065  	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6066  unlock:
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6067  	mutex_unlock(&cpu_buffer->mapping_lock);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6068  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6069  	return err;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6070  }
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6071  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6072  int ring_buffer_unmap(struct trace_buffer *buffer, int cpu)
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6073  {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6074  	struct ring_buffer_per_cpu *cpu_buffer;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6075  	int err = 0;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6076  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6077  	if (!cpumask_test_cpu(cpu, buffer->cpumask))
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6078  		return -EINVAL;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6079  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6080  	cpu_buffer = buffer->buffers[cpu];
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6081  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6082  	mutex_lock(&cpu_buffer->mapping_lock);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6083  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6084  	if (!cpu_buffer->mapped) {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6085  		err = -ENODEV;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6086  		goto unlock;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6087  	}
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6088  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6089  	WRITE_ONCE(cpu_buffer->mapped, cpu_buffer->mapped - 1);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6090  	if (!cpu_buffer->mapped) {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6091  		/* Wait the writer and readers to observe !mapped */
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6092  		synchronize_rcu();
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6093  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6094  		rb_free_page_ids(cpu_buffer);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6095  		rb_free_meta_page(cpu_buffer);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6096  		atomic_dec(&cpu_buffer->resize_disabled);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6097  	}
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6098  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6099  unlock:
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6100  	mutex_unlock(&cpu_buffer->mapping_lock);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6101  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6102  	return err;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6103  }
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6104  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6105  /*
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6106   *   +--------------+
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6107   *   |   meta page  |  pgoff=0
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6108   *   +--------------+
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6109   *   |  data page1  |  pgoff=1 page_ids=0
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6110   *   +--------------+
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6111   *   |  data page2  |  pgoff=2 page_ids=1
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6112   *         ...
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6113   */
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6114  struct page *ring_buffer_map_fault(struct trace_buffer *buffer, int cpu,
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6115  				   unsigned long pgoff)
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6116  {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6117  	struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu];
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6118  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6119  	if (!pgoff)
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6120  		return virt_to_page(cpu_buffer->meta_page);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6121  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6122  	pgoff--;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6123  	if (pgoff > cpu_buffer->nr_pages)
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6124  		return NULL;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6125  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6126  	return virt_to_page(cpu_buffer->page_ids[pgoff]);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6127  }
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6128  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6129  int ring_buffer_get_reader_page(struct trace_buffer *buffer, int cpu)
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6130  {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6131  	struct ring_buffer_per_cpu *cpu_buffer;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6132  	struct buffer_page *reader;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6133  	unsigned long flags;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6134  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6135  	cpu_buffer = rb_get_mapped_buffer(buffer, cpu);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6136  	if (IS_ERR(cpu_buffer))
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17 @6137  		return (int)PTR_ERR(cpu_buffer);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6138  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6139  	raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6140  	reader = cpu_buffer->reader_page;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6141  	reader->read = rb_page_size(reader);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6142  	if (!rb_per_cpu_empty(cpu_buffer))
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6143  		WARN_ON(!rb_get_reader_page(cpu_buffer));
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6144  	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6145  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6146  	rb_put_mapped_buffer(cpu_buffer);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6147  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6148  	return 0;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6149  }
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6150  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6151  int ring_buffer_update_meta_page(struct trace_buffer *buffer, int cpu)
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6152  {
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6153  	struct ring_buffer_per_cpu *cpu_buffer;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6154  	unsigned long flags;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6155  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6156  	cpu_buffer = rb_get_mapped_buffer(buffer, cpu);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6157  	if (IS_ERR(cpu_buffer))
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17 @6158  		return PTR_ERR(cpu_buffer);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6159  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6160  	/* Update the head page if the writer moved it */
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6161  	raw_spin_lock_irqsave(&cpu_buffer->reader_lock, flags);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6162  	rb_set_head_page(cpu_buffer);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6163  	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6164  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6165  	rb_put_mapped_buffer(cpu_buffer);
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6166  
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6167  	return 0;
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6168  }
e6f6ebfdb93b0a Vincent Donnefort 2023-03-17  6169  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-03-21 16:52 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-17 14:33 [PATCH 0/2] Introducing trace buffer mapping by user-space Vincent Donnefort
2023-03-17 14:33 ` [PATCH 1/2] ring-buffer: Introducing ring-buffer mapping functions Vincent Donnefort
2023-03-21  1:45   ` Steven Rostedt
2023-03-21 15:17     ` Vincent Donnefort
2023-03-21 15:40       ` Steven Rostedt
2023-03-21 16:20         ` Vincent Donnefort
2023-03-21 16:51           ` Steven Rostedt
2023-03-21 16:44         ` Steven Rostedt
2023-03-21 16:50           ` Vincent Donnefort
2023-03-17 14:33 ` [PATCH 2/2] tracing: Allow user-space mapping of the ring-buffer Vincent Donnefort
  -- strict thread matches above, loose matches on Subject: below --
2023-03-18  3:00 [PATCH 1/2] ring-buffer: Introducing ring-buffer mapping functions kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.