relay.h 8.8 KB

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