xref: /openbmc/linux/include/linux/relay.h (revision 023542f4)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2b86ff981SJens Axboe /*
3b86ff981SJens Axboe  * linux/include/linux/relay.h
4b86ff981SJens Axboe  *
5b86ff981SJens Axboe  * Copyright (C) 2002, 2003 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
6b86ff981SJens Axboe  * Copyright (C) 1999, 2000, 2001, 2002 - Karim Yaghmour (karim@opersys.com)
7b86ff981SJens Axboe  *
8b86ff981SJens Axboe  * CONFIG_RELAY definitions and declarations
9b86ff981SJens Axboe  */
10b86ff981SJens Axboe 
11b86ff981SJens Axboe #ifndef _LINUX_RELAY_H
12b86ff981SJens Axboe #define _LINUX_RELAY_H
13b86ff981SJens Axboe 
14b86ff981SJens Axboe #include <linux/types.h>
15b86ff981SJens Axboe #include <linux/sched.h>
167c9cb383STom Zanussi #include <linux/timer.h>
17b86ff981SJens Axboe #include <linux/wait.h>
18b86ff981SJens Axboe #include <linux/list.h>
1926b5679eSPeter Zijlstra #include <linux/irq_work.h>
20187f1882SPaul Gortmaker #include <linux/bug.h>
21b86ff981SJens Axboe #include <linux/fs.h>
22b86ff981SJens Axboe #include <linux/poll.h>
23b86ff981SJens Axboe #include <linux/kref.h>
24017c59c0SAkash Goel #include <linux/percpu.h>
25b86ff981SJens Axboe 
26b86ff981SJens Axboe /*
27b86ff981SJens Axboe  * Tracks changes to rchan/rchan_buf structs
28b86ff981SJens Axboe  */
2923c88752SMathieu Desnoyers #define RELAYFS_CHANNEL_VERSION		7
30b86ff981SJens Axboe 
31b86ff981SJens Axboe /*
32b86ff981SJens Axboe  * Per-cpu relay channel buffer
33b86ff981SJens Axboe  */
34b86ff981SJens Axboe struct rchan_buf
35b86ff981SJens Axboe {
36b86ff981SJens Axboe 	void *start;			/* start of channel buffer */
37b86ff981SJens Axboe 	void *data;			/* start of current sub-buffer */
38b86ff981SJens Axboe 	size_t offset;			/* current offset into sub-buffer */
39b86ff981SJens Axboe 	size_t subbufs_produced;	/* count of sub-buffers produced */
40b86ff981SJens Axboe 	size_t subbufs_consumed;	/* count of sub-buffers consumed */
41b86ff981SJens Axboe 	struct rchan *chan;		/* associated channel */
42b86ff981SJens Axboe 	wait_queue_head_t read_wait;	/* reader wait queue */
4326b5679eSPeter Zijlstra 	struct irq_work wakeup_work;	/* reader wakeup */
44b86ff981SJens Axboe 	struct dentry *dentry;		/* channel file dentry */
45b86ff981SJens Axboe 	struct kref kref;		/* channel buffer refcount */
46b86ff981SJens Axboe 	struct page **page_array;	/* array of current buffer pages */
47b86ff981SJens Axboe 	unsigned int page_count;	/* number of current buffer pages */
48b86ff981SJens Axboe 	unsigned int finalized;		/* buffer has been finalized */
49b86ff981SJens Axboe 	size_t *padding;		/* padding counts per sub-buffer */
50b86ff981SJens Axboe 	size_t prev_padding;		/* temporary variable */
51b86ff981SJens Axboe 	size_t bytes_consumed;		/* bytes consumed in cur read subbuf */
5220d8b67cSEduard - Gabriel Munteanu 	size_t early_bytes;		/* bytes consumed before VFS inited */
53b86ff981SJens Axboe 	unsigned int cpu;		/* this buf's cpu */
54b86ff981SJens Axboe } ____cacheline_aligned;
55b86ff981SJens Axboe 
56b86ff981SJens Axboe /*
57b86ff981SJens Axboe  * Relay channel data structure
58b86ff981SJens Axboe  */
59b86ff981SJens Axboe struct rchan
60b86ff981SJens Axboe {
61b86ff981SJens Axboe 	u32 version;			/* the version of this struct */
62b86ff981SJens Axboe 	size_t subbuf_size;		/* sub-buffer size */
63b86ff981SJens Axboe 	size_t n_subbufs;		/* number of sub-buffers per buffer */
64b86ff981SJens Axboe 	size_t alloc_size;		/* total buffer size allocated */
65*023542f4SJani Nikula 	const struct rchan_callbacks *cb; /* client callbacks */
66b86ff981SJens Axboe 	struct kref kref;		/* channel refcount */
67b86ff981SJens Axboe 	void *private_data;		/* for user-defined data */
68b86ff981SJens Axboe 	size_t last_toobig;		/* tried to log event > subbuf size */
6962461ac2SLuc Van Oostenryck 	struct rchan_buf * __percpu *buf; /* per-cpu channel buffers */
7023c88752SMathieu Desnoyers 	int is_global;			/* One global buffer ? */
7123c88752SMathieu Desnoyers 	struct list_head list;		/* for channel list */
7223c88752SMathieu Desnoyers 	struct dentry *parent;		/* parent dentry passed to open */
7320d8b67cSEduard - Gabriel Munteanu 	int has_base_filename;		/* has a filename associated? */
7423c88752SMathieu Desnoyers 	char base_filename[NAME_MAX];	/* saved base filename */
75b86ff981SJens Axboe };
76b86ff981SJens Axboe 
77b86ff981SJens Axboe /*
78b86ff981SJens Axboe  * Relay channel client callbacks
79b86ff981SJens Axboe  */
80b86ff981SJens Axboe struct rchan_callbacks
81b86ff981SJens Axboe {
82b86ff981SJens Axboe 	/*
83b86ff981SJens Axboe 	 * subbuf_start - called on buffer-switch to a new sub-buffer
84b86ff981SJens Axboe 	 * @buf: the channel buffer containing the new sub-buffer
85b86ff981SJens Axboe 	 * @subbuf: the start of the new sub-buffer
86b86ff981SJens Axboe 	 * @prev_subbuf: the start of the previous sub-buffer
87b86ff981SJens Axboe 	 * @prev_padding: unused space at the end of previous sub-buffer
88b86ff981SJens Axboe 	 *
89b86ff981SJens Axboe 	 * The client should return 1 to continue logging, 0 to stop
90b86ff981SJens Axboe 	 * logging.
91b86ff981SJens Axboe 	 *
92371e0388SJani Nikula 	 * This callback is optional.
93371e0388SJani Nikula 	 *
94b86ff981SJens Axboe 	 * NOTE: subbuf_start will also be invoked when the buffer is
95b86ff981SJens Axboe 	 *       created, so that the first sub-buffer can be initialized
96b86ff981SJens Axboe 	 *       if necessary.  In this case, prev_subbuf will be NULL.
97b86ff981SJens Axboe 	 *
98b86ff981SJens Axboe 	 * NOTE: the client can reserve bytes at the beginning of the new
99b86ff981SJens Axboe 	 *       sub-buffer by calling subbuf_start_reserve() in this callback.
100b86ff981SJens Axboe 	 */
101b86ff981SJens Axboe 	int (*subbuf_start) (struct rchan_buf *buf,
102b86ff981SJens Axboe 			     void *subbuf,
103b86ff981SJens Axboe 			     void *prev_subbuf,
104b86ff981SJens Axboe 			     size_t prev_padding);
105b86ff981SJens Axboe 
106b86ff981SJens Axboe 	/*
107b86ff981SJens Axboe 	 * create_buf_file - create file to represent a relay channel buffer
108b86ff981SJens Axboe 	 * @filename: the name of the file to create
109b86ff981SJens Axboe 	 * @parent: the parent of the file to create
110b86ff981SJens Axboe 	 * @mode: the mode of the file to create
111b86ff981SJens Axboe 	 * @buf: the channel buffer
112b86ff981SJens Axboe 	 * @is_global: outparam - set non-zero if the buffer should be global
113b86ff981SJens Axboe 	 *
114b86ff981SJens Axboe 	 * Called during relay_open(), once for each per-cpu buffer,
115b86ff981SJens Axboe 	 * to allow the client to create a file to be used to
116b86ff981SJens Axboe 	 * represent the corresponding channel buffer.  If the file is
117b86ff981SJens Axboe 	 * created outside of relay, the parent must also exist in
118b86ff981SJens Axboe 	 * that filesystem.
119b86ff981SJens Axboe 	 *
120b86ff981SJens Axboe 	 * The callback should return the dentry of the file created
121b86ff981SJens Axboe 	 * to represent the relay buffer.
122b86ff981SJens Axboe 	 *
123b86ff981SJens Axboe 	 * Setting the is_global outparam to a non-zero value will
124b86ff981SJens Axboe 	 * cause relay_open() to create a single global buffer rather
125b86ff981SJens Axboe 	 * than the default set of per-cpu buffers.
126b86ff981SJens Axboe 	 *
127371e0388SJani Nikula 	 * This callback is mandatory.
128371e0388SJani Nikula 	 *
1290c1bc6b8SMauro Carvalho Chehab 	 * See Documentation/filesystems/relay.rst for more info.
130b86ff981SJens Axboe 	 */
131b86ff981SJens Axboe 	struct dentry *(*create_buf_file)(const char *filename,
132b86ff981SJens Axboe 					  struct dentry *parent,
133f4ae40a6SAl Viro 					  umode_t mode,
134b86ff981SJens Axboe 					  struct rchan_buf *buf,
135b86ff981SJens Axboe 					  int *is_global);
136b86ff981SJens Axboe 
137b86ff981SJens Axboe 	/*
138b86ff981SJens Axboe 	 * remove_buf_file - remove file representing a relay channel buffer
139b86ff981SJens Axboe 	 * @dentry: the dentry of the file to remove
140b86ff981SJens Axboe 	 *
141b86ff981SJens Axboe 	 * Called during relay_close(), once for each per-cpu buffer,
142b86ff981SJens Axboe 	 * to allow the client to remove a file used to represent a
143b86ff981SJens Axboe 	 * channel buffer.
144b86ff981SJens Axboe 	 *
145b86ff981SJens Axboe 	 * The callback should return 0 if successful, negative if not.
146371e0388SJani Nikula 	 *
147371e0388SJani Nikula 	 * This callback is mandatory.
148b86ff981SJens Axboe 	 */
149b86ff981SJens Axboe 	int (*remove_buf_file)(struct dentry *dentry);
150b86ff981SJens Axboe };
151b86ff981SJens Axboe 
152b86ff981SJens Axboe /*
153b86ff981SJens Axboe  * CONFIG_RELAY kernel API, kernel/relay.c
154b86ff981SJens Axboe  */
155b86ff981SJens Axboe 
156b86ff981SJens Axboe struct rchan *relay_open(const char *base_filename,
157b86ff981SJens Axboe 			 struct dentry *parent,
158b86ff981SJens Axboe 			 size_t subbuf_size,
159b86ff981SJens Axboe 			 size_t n_subbufs,
160*023542f4SJani Nikula 			 const struct rchan_callbacks *cb,
16123c88752SMathieu Desnoyers 			 void *private_data);
16220d8b67cSEduard - Gabriel Munteanu extern int relay_late_setup_files(struct rchan *chan,
16320d8b67cSEduard - Gabriel Munteanu 				  const char *base_filename,
16420d8b67cSEduard - Gabriel Munteanu 				  struct dentry *parent);
165b86ff981SJens Axboe extern void relay_close(struct rchan *chan);
166b86ff981SJens Axboe extern void relay_flush(struct rchan *chan);
167b86ff981SJens Axboe extern void relay_subbufs_consumed(struct rchan *chan,
168b86ff981SJens Axboe 				   unsigned int cpu,
169b86ff981SJens Axboe 				   size_t consumed);
170b86ff981SJens Axboe extern void relay_reset(struct rchan *chan);
171b86ff981SJens Axboe extern int relay_buf_full(struct rchan_buf *buf);
172b86ff981SJens Axboe 
173b86ff981SJens Axboe extern size_t relay_switch_subbuf(struct rchan_buf *buf,
174b86ff981SJens Axboe 				  size_t length);
175b86ff981SJens Axboe 
176b86ff981SJens Axboe /**
177b86ff981SJens Axboe  *	relay_write - write data into the channel
178b86ff981SJens Axboe  *	@chan: relay channel
179b86ff981SJens Axboe  *	@data: data to be written
180b86ff981SJens Axboe  *	@length: number of bytes to write
181b86ff981SJens Axboe  *
182b86ff981SJens Axboe  *	Writes data into the current cpu's channel buffer.
183b86ff981SJens Axboe  *
184b86ff981SJens Axboe  *	Protects the buffer by disabling interrupts.  Use this
185b86ff981SJens Axboe  *	if you might be logging from interrupt context.  Try
186b86ff981SJens Axboe  *	__relay_write() if you know you	won't be logging from
187b86ff981SJens Axboe  *	interrupt context.
188b86ff981SJens Axboe  */
relay_write(struct rchan * chan,const void * data,size_t length)189b86ff981SJens Axboe static inline void relay_write(struct rchan *chan,
190b86ff981SJens Axboe 			       const void *data,
191b86ff981SJens Axboe 			       size_t length)
192b86ff981SJens Axboe {
193b86ff981SJens Axboe 	unsigned long flags;
194b86ff981SJens Axboe 	struct rchan_buf *buf;
195b86ff981SJens Axboe 
196b86ff981SJens Axboe 	local_irq_save(flags);
197017c59c0SAkash Goel 	buf = *this_cpu_ptr(chan->buf);
198b86ff981SJens Axboe 	if (unlikely(buf->offset + length > chan->subbuf_size))
199b86ff981SJens Axboe 		length = relay_switch_subbuf(buf, length);
200b86ff981SJens Axboe 	memcpy(buf->data + buf->offset, data, length);
201b86ff981SJens Axboe 	buf->offset += length;
202b86ff981SJens Axboe 	local_irq_restore(flags);
203b86ff981SJens Axboe }
204b86ff981SJens Axboe 
205b86ff981SJens Axboe /**
206b86ff981SJens Axboe  *	__relay_write - write data into the channel
207b86ff981SJens Axboe  *	@chan: relay channel
208b86ff981SJens Axboe  *	@data: data to be written
209b86ff981SJens Axboe  *	@length: number of bytes to write
210b86ff981SJens Axboe  *
211b86ff981SJens Axboe  *	Writes data into the current cpu's channel buffer.
212b86ff981SJens Axboe  *
213b86ff981SJens Axboe  *	Protects the buffer by disabling preemption.  Use
214b86ff981SJens Axboe  *	relay_write() if you might be logging from interrupt
215b86ff981SJens Axboe  *	context.
216b86ff981SJens Axboe  */
__relay_write(struct rchan * chan,const void * data,size_t length)217b86ff981SJens Axboe static inline void __relay_write(struct rchan *chan,
218b86ff981SJens Axboe 				 const void *data,
219b86ff981SJens Axboe 				 size_t length)
220b86ff981SJens Axboe {
221b86ff981SJens Axboe 	struct rchan_buf *buf;
222b86ff981SJens Axboe 
223017c59c0SAkash Goel 	buf = *get_cpu_ptr(chan->buf);
224b86ff981SJens Axboe 	if (unlikely(buf->offset + length > buf->chan->subbuf_size))
225b86ff981SJens Axboe 		length = relay_switch_subbuf(buf, length);
226b86ff981SJens Axboe 	memcpy(buf->data + buf->offset, data, length);
227b86ff981SJens Axboe 	buf->offset += length;
228017c59c0SAkash Goel 	put_cpu_ptr(chan->buf);
229b86ff981SJens Axboe }
230b86ff981SJens Axboe 
231b86ff981SJens Axboe /**
232b86ff981SJens Axboe  *	relay_reserve - reserve slot in channel buffer
233b86ff981SJens Axboe  *	@chan: relay channel
234b86ff981SJens Axboe  *	@length: number of bytes to reserve
235b86ff981SJens Axboe  *
236b86ff981SJens Axboe  *	Returns pointer to reserved slot, NULL if full.
237b86ff981SJens Axboe  *
238b86ff981SJens Axboe  *	Reserves a slot in the current cpu's channel buffer.
239b86ff981SJens Axboe  *	Does not protect the buffer at all - caller must provide
240b86ff981SJens Axboe  *	appropriate synchronization.
241b86ff981SJens Axboe  */
relay_reserve(struct rchan * chan,size_t length)242b86ff981SJens Axboe static inline void *relay_reserve(struct rchan *chan, size_t length)
243b86ff981SJens Axboe {
244017c59c0SAkash Goel 	void *reserved = NULL;
245017c59c0SAkash Goel 	struct rchan_buf *buf = *get_cpu_ptr(chan->buf);
246b86ff981SJens Axboe 
247b86ff981SJens Axboe 	if (unlikely(buf->offset + length > buf->chan->subbuf_size)) {
248b86ff981SJens Axboe 		length = relay_switch_subbuf(buf, length);
249b86ff981SJens Axboe 		if (!length)
250017c59c0SAkash Goel 			goto end;
251b86ff981SJens Axboe 	}
252b86ff981SJens Axboe 	reserved = buf->data + buf->offset;
253b86ff981SJens Axboe 	buf->offset += length;
254b86ff981SJens Axboe 
255017c59c0SAkash Goel end:
256017c59c0SAkash Goel 	put_cpu_ptr(chan->buf);
257b86ff981SJens Axboe 	return reserved;
258b86ff981SJens Axboe }
259b86ff981SJens Axboe 
260b86ff981SJens Axboe /**
261b86ff981SJens Axboe  *	subbuf_start_reserve - reserve bytes at the start of a sub-buffer
262b86ff981SJens Axboe  *	@buf: relay channel buffer
263b86ff981SJens Axboe  *	@length: number of bytes to reserve
264b86ff981SJens Axboe  *
265b86ff981SJens Axboe  *	Helper function used to reserve bytes at the beginning of
266b86ff981SJens Axboe  *	a sub-buffer in the subbuf_start() callback.
267b86ff981SJens Axboe  */
subbuf_start_reserve(struct rchan_buf * buf,size_t length)268b86ff981SJens Axboe static inline void subbuf_start_reserve(struct rchan_buf *buf,
269b86ff981SJens Axboe 					size_t length)
270b86ff981SJens Axboe {
271b86ff981SJens Axboe 	BUG_ON(length >= buf->chan->subbuf_size - 1);
272b86ff981SJens Axboe 	buf->offset = length;
273b86ff981SJens Axboe }
274b86ff981SJens Axboe 
275b86ff981SJens Axboe /*
276b86ff981SJens Axboe  * exported relay file operations, kernel/relay.c
277b86ff981SJens Axboe  */
27815ad7cdcSHelge Deller extern const struct file_operations relay_file_operations;
279b86ff981SJens Axboe 
280e6d4989aSRichard Weinberger #ifdef CONFIG_RELAY
281e6d4989aSRichard Weinberger int relay_prepare_cpu(unsigned int cpu);
282e6d4989aSRichard Weinberger #else
283e6d4989aSRichard Weinberger #define relay_prepare_cpu     NULL
284e6d4989aSRichard Weinberger #endif
285e6d4989aSRichard Weinberger 
286b86ff981SJens Axboe #endif /* _LINUX_RELAY_H */
287b86ff981SJens Axboe 
288