relay.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * linux/include/linux/relay.h
  4. *
  5. * Copyright (C) 2002, 2003 - Tom Zanussi ([email protected]), IBM Corp
  6. * Copyright (C) 1999, 2000, 2001, 2002 - Karim Yaghmour ([email protected])
  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. const 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. * This callback is optional.
  87. *
  88. * NOTE: subbuf_start will also be invoked when the buffer is
  89. * created, so that the first sub-buffer can be initialized
  90. * if necessary. In this case, prev_subbuf will be NULL.
  91. *
  92. * NOTE: the client can reserve bytes at the beginning of the new
  93. * sub-buffer by calling subbuf_start_reserve() in this callback.
  94. */
  95. int (*subbuf_start) (struct rchan_buf *buf,
  96. void *subbuf,
  97. void *prev_subbuf,
  98. size_t prev_padding);
  99. /*
  100. * create_buf_file - create file to represent a relay channel buffer
  101. * @filename: the name of the file to create
  102. * @parent: the parent of the file to create
  103. * @mode: the mode of the file to create
  104. * @buf: the channel buffer
  105. * @is_global: outparam - set non-zero if the buffer should be global
  106. *
  107. * Called during relay_open(), once for each per-cpu buffer,
  108. * to allow the client to create a file to be used to
  109. * represent the corresponding channel buffer. If the file is
  110. * created outside of relay, the parent must also exist in
  111. * that filesystem.
  112. *
  113. * The callback should return the dentry of the file created
  114. * to represent the relay buffer.
  115. *
  116. * Setting the is_global outparam to a non-zero value will
  117. * cause relay_open() to create a single global buffer rather
  118. * than the default set of per-cpu buffers.
  119. *
  120. * This callback is mandatory.
  121. *
  122. * See Documentation/filesystems/relay.rst for more info.
  123. */
  124. struct dentry *(*create_buf_file)(const char *filename,
  125. struct dentry *parent,
  126. umode_t mode,
  127. struct rchan_buf *buf,
  128. int *is_global);
  129. /*
  130. * remove_buf_file - remove file representing a relay channel buffer
  131. * @dentry: the dentry of the file to remove
  132. *
  133. * Called during relay_close(), once for each per-cpu buffer,
  134. * to allow the client to remove a file used to represent a
  135. * channel buffer.
  136. *
  137. * The callback should return 0 if successful, negative if not.
  138. *
  139. * This callback is mandatory.
  140. */
  141. int (*remove_buf_file)(struct dentry *dentry);
  142. };
  143. /*
  144. * CONFIG_RELAY kernel API, kernel/relay.c
  145. */
  146. struct rchan *relay_open(const char *base_filename,
  147. struct dentry *parent,
  148. size_t subbuf_size,
  149. size_t n_subbufs,
  150. const struct rchan_callbacks *cb,
  151. void *private_data);
  152. extern int relay_late_setup_files(struct rchan *chan,
  153. const char *base_filename,
  154. struct dentry *parent);
  155. extern void relay_close(struct rchan *chan);
  156. extern void relay_flush(struct rchan *chan);
  157. extern void relay_subbufs_consumed(struct rchan *chan,
  158. unsigned int cpu,
  159. size_t consumed);
  160. extern void relay_reset(struct rchan *chan);
  161. extern int relay_buf_full(struct rchan_buf *buf);
  162. extern size_t relay_switch_subbuf(struct rchan_buf *buf,
  163. size_t length);
  164. /**
  165. * relay_write - write data into the channel
  166. * @chan: relay channel
  167. * @data: data to be written
  168. * @length: number of bytes to write
  169. *
  170. * Writes data into the current cpu's channel buffer.
  171. *
  172. * Protects the buffer by disabling interrupts. Use this
  173. * if you might be logging from interrupt context. Try
  174. * __relay_write() if you know you won't be logging from
  175. * interrupt context.
  176. */
  177. static inline void relay_write(struct rchan *chan,
  178. const void *data,
  179. size_t length)
  180. {
  181. unsigned long flags;
  182. struct rchan_buf *buf;
  183. local_irq_save(flags);
  184. buf = *this_cpu_ptr(chan->buf);
  185. if (unlikely(buf->offset + length > chan->subbuf_size))
  186. length = relay_switch_subbuf(buf, length);
  187. memcpy(buf->data + buf->offset, data, length);
  188. buf->offset += length;
  189. local_irq_restore(flags);
  190. }
  191. /**
  192. * __relay_write - write data into the channel
  193. * @chan: relay channel
  194. * @data: data to be written
  195. * @length: number of bytes to write
  196. *
  197. * Writes data into the current cpu's channel buffer.
  198. *
  199. * Protects the buffer by disabling preemption. Use
  200. * relay_write() if you might be logging from interrupt
  201. * context.
  202. */
  203. static inline void __relay_write(struct rchan *chan,
  204. const void *data,
  205. size_t length)
  206. {
  207. struct rchan_buf *buf;
  208. buf = *get_cpu_ptr(chan->buf);
  209. if (unlikely(buf->offset + length > buf->chan->subbuf_size))
  210. length = relay_switch_subbuf(buf, length);
  211. memcpy(buf->data + buf->offset, data, length);
  212. buf->offset += length;
  213. put_cpu_ptr(chan->buf);
  214. }
  215. /**
  216. * relay_reserve - reserve slot in channel buffer
  217. * @chan: relay channel
  218. * @length: number of bytes to reserve
  219. *
  220. * Returns pointer to reserved slot, NULL if full.
  221. *
  222. * Reserves a slot in the current cpu's channel buffer.
  223. * Does not protect the buffer at all - caller must provide
  224. * appropriate synchronization.
  225. */
  226. static inline void *relay_reserve(struct rchan *chan, size_t length)
  227. {
  228. void *reserved = NULL;
  229. struct rchan_buf *buf = *get_cpu_ptr(chan->buf);
  230. if (unlikely(buf->offset + length > buf->chan->subbuf_size)) {
  231. length = relay_switch_subbuf(buf, length);
  232. if (!length)
  233. goto end;
  234. }
  235. reserved = buf->data + buf->offset;
  236. buf->offset += length;
  237. end:
  238. put_cpu_ptr(chan->buf);
  239. return reserved;
  240. }
  241. /**
  242. * subbuf_start_reserve - reserve bytes at the start of a sub-buffer
  243. * @buf: relay channel buffer
  244. * @length: number of bytes to reserve
  245. *
  246. * Helper function used to reserve bytes at the beginning of
  247. * a sub-buffer in the subbuf_start() callback.
  248. */
  249. static inline void subbuf_start_reserve(struct rchan_buf *buf,
  250. size_t length)
  251. {
  252. BUG_ON(length >= buf->chan->subbuf_size - 1);
  253. buf->offset = length;
  254. }
  255. /*
  256. * exported relay file operations, kernel/relay.c
  257. */
  258. extern const struct file_operations relay_file_operations;
  259. #ifdef CONFIG_RELAY
  260. int relay_prepare_cpu(unsigned int cpu);
  261. #else
  262. #define relay_prepare_cpu NULL
  263. #endif
  264. #endif /* _LINUX_RELAY_H */