printk_ringbuffer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _KERNEL_PRINTK_RINGBUFFER_H
  3. #define _KERNEL_PRINTK_RINGBUFFER_H
  4. #include <linux/atomic.h>
  5. #include <linux/bits.h>
  6. #include <linux/dev_printk.h>
  7. #include <linux/stddef.h>
  8. #include <linux/types.h>
  9. /*
  10. * Meta information about each stored message.
  11. *
  12. * All fields are set by the printk code except for @seq, which is
  13. * set by the ringbuffer code.
  14. */
  15. struct printk_info {
  16. u64 seq; /* sequence number */
  17. u64 ts_nsec; /* timestamp in nanoseconds */
  18. u16 text_len; /* length of text message */
  19. u8 facility; /* syslog facility */
  20. u8 flags:5; /* internal record flags */
  21. u8 level:3; /* syslog level */
  22. u32 caller_id; /* thread id or processor id */
  23. struct dev_printk_info dev_info;
  24. };
  25. /*
  26. * A structure providing the buffers, used by writers and readers.
  27. *
  28. * Writers:
  29. * Using prb_rec_init_wr(), a writer sets @text_buf_size before calling
  30. * prb_reserve(). On success, prb_reserve() sets @info and @text_buf to
  31. * buffers reserved for that writer.
  32. *
  33. * Readers:
  34. * Using prb_rec_init_rd(), a reader sets all fields before calling
  35. * prb_read_valid(). Note that the reader provides the @info and @text_buf,
  36. * buffers. On success, the struct pointed to by @info will be filled and
  37. * the char array pointed to by @text_buf will be filled with text data.
  38. */
  39. struct printk_record {
  40. struct printk_info *info;
  41. char *text_buf;
  42. unsigned int text_buf_size;
  43. };
  44. /* Specifies the logical position and span of a data block. */
  45. struct prb_data_blk_lpos {
  46. unsigned long begin;
  47. unsigned long next;
  48. };
  49. /*
  50. * A descriptor: the complete meta-data for a record.
  51. *
  52. * @state_var: A bitwise combination of descriptor ID and descriptor state.
  53. */
  54. struct prb_desc {
  55. atomic_long_t state_var;
  56. struct prb_data_blk_lpos text_blk_lpos;
  57. };
  58. /* A ringbuffer of "ID + data" elements. */
  59. struct prb_data_ring {
  60. unsigned int size_bits;
  61. char *data;
  62. atomic_long_t head_lpos;
  63. atomic_long_t tail_lpos;
  64. };
  65. /* A ringbuffer of "struct prb_desc" elements. */
  66. struct prb_desc_ring {
  67. unsigned int count_bits;
  68. struct prb_desc *descs;
  69. struct printk_info *infos;
  70. atomic_long_t head_id;
  71. atomic_long_t tail_id;
  72. atomic_long_t last_finalized_seq;
  73. };
  74. /*
  75. * The high level structure representing the printk ringbuffer.
  76. *
  77. * @fail: Count of failed prb_reserve() calls where not even a data-less
  78. * record was created.
  79. */
  80. struct printk_ringbuffer {
  81. struct prb_desc_ring desc_ring;
  82. struct prb_data_ring text_data_ring;
  83. atomic_long_t fail;
  84. };
  85. /*
  86. * Used by writers as a reserve/commit handle.
  87. *
  88. * @rb: Ringbuffer where the entry is reserved.
  89. * @irqflags: Saved irq flags to restore on entry commit.
  90. * @id: ID of the reserved descriptor.
  91. * @text_space: Total occupied buffer space in the text data ring, including
  92. * ID, alignment padding, and wrapping data blocks.
  93. *
  94. * This structure is an opaque handle for writers. Its contents are only
  95. * to be used by the ringbuffer implementation.
  96. */
  97. struct prb_reserved_entry {
  98. struct printk_ringbuffer *rb;
  99. unsigned long irqflags;
  100. unsigned long id;
  101. unsigned int text_space;
  102. };
  103. /* The possible responses of a descriptor state-query. */
  104. enum desc_state {
  105. desc_miss = -1, /* ID mismatch (pseudo state) */
  106. desc_reserved = 0x0, /* reserved, in use by writer */
  107. desc_committed = 0x1, /* committed by writer, could get reopened */
  108. desc_finalized = 0x2, /* committed, no further modification allowed */
  109. desc_reusable = 0x3, /* free, not yet used by any writer */
  110. };
  111. #define _DATA_SIZE(sz_bits) (1UL << (sz_bits))
  112. #define _DESCS_COUNT(ct_bits) (1U << (ct_bits))
  113. #define DESC_SV_BITS BITS_PER_LONG
  114. #define DESC_FLAGS_SHIFT (DESC_SV_BITS - 2)
  115. #define DESC_FLAGS_MASK (3UL << DESC_FLAGS_SHIFT)
  116. #define DESC_STATE(sv) (3UL & (sv >> DESC_FLAGS_SHIFT))
  117. #define DESC_SV(id, state) (((unsigned long)state << DESC_FLAGS_SHIFT) | id)
  118. #define DESC_ID_MASK (~DESC_FLAGS_MASK)
  119. #define DESC_ID(sv) ((sv) & DESC_ID_MASK)
  120. /*
  121. * Special data block logical position values (for fields of
  122. * @prb_desc.text_blk_lpos).
  123. *
  124. * - Bit0 is used to identify if the record has no data block. (Implemented in
  125. * the LPOS_DATALESS() macro.)
  126. *
  127. * - Bit1 specifies the reason for not having a data block.
  128. *
  129. * These special values could never be real lpos values because of the
  130. * meta data and alignment padding of data blocks. (See to_blk_size() for
  131. * details.)
  132. */
  133. #define FAILED_LPOS 0x1
  134. #define EMPTY_LINE_LPOS 0x3
  135. #define FAILED_BLK_LPOS \
  136. { \
  137. .begin = FAILED_LPOS, \
  138. .next = FAILED_LPOS, \
  139. }
  140. /*
  141. * Descriptor Bootstrap
  142. *
  143. * The descriptor array is minimally initialized to allow immediate usage
  144. * by readers and writers. The requirements that the descriptor array
  145. * initialization must satisfy:
  146. *
  147. * Req1
  148. * The tail must point to an existing (committed or reusable) descriptor.
  149. * This is required by the implementation of prb_first_seq().
  150. *
  151. * Req2
  152. * Readers must see that the ringbuffer is initially empty.
  153. *
  154. * Req3
  155. * The first record reserved by a writer is assigned sequence number 0.
  156. *
  157. * To satisfy Req1, the tail initially points to a descriptor that is
  158. * minimally initialized (having no data block, i.e. data-less with the
  159. * data block's lpos @begin and @next values set to FAILED_LPOS).
  160. *
  161. * To satisfy Req2, the initial tail descriptor is initialized to the
  162. * reusable state. Readers recognize reusable descriptors as existing
  163. * records, but skip over them.
  164. *
  165. * To satisfy Req3, the last descriptor in the array is used as the initial
  166. * head (and tail) descriptor. This allows the first record reserved by a
  167. * writer (head + 1) to be the first descriptor in the array. (Only the first
  168. * descriptor in the array could have a valid sequence number of 0.)
  169. *
  170. * The first time a descriptor is reserved, it is assigned a sequence number
  171. * with the value of the array index. A "first time reserved" descriptor can
  172. * be recognized because it has a sequence number of 0 but does not have an
  173. * index of 0. (Only the first descriptor in the array could have a valid
  174. * sequence number of 0.) After the first reservation, all future reservations
  175. * (recycling) simply involve incrementing the sequence number by the array
  176. * count.
  177. *
  178. * Hack #1
  179. * Only the first descriptor in the array is allowed to have the sequence
  180. * number 0. In this case it is not possible to recognize if it is being
  181. * reserved the first time (set to index value) or has been reserved
  182. * previously (increment by the array count). This is handled by _always_
  183. * incrementing the sequence number by the array count when reserving the
  184. * first descriptor in the array. In order to satisfy Req3, the sequence
  185. * number of the first descriptor in the array is initialized to minus
  186. * the array count. Then, upon the first reservation, it is incremented
  187. * to 0, thus satisfying Req3.
  188. *
  189. * Hack #2
  190. * prb_first_seq() can be called at any time by readers to retrieve the
  191. * sequence number of the tail descriptor. However, due to Req2 and Req3,
  192. * initially there are no records to report the sequence number of
  193. * (sequence numbers are u64 and there is nothing less than 0). To handle
  194. * this, the sequence number of the initial tail descriptor is initialized
  195. * to 0. Technically this is incorrect, because there is no record with
  196. * sequence number 0 (yet) and the tail descriptor is not the first
  197. * descriptor in the array. But it allows prb_read_valid() to correctly
  198. * report the existence of a record for _any_ given sequence number at all
  199. * times. Bootstrapping is complete when the tail is pushed the first
  200. * time, thus finally pointing to the first descriptor reserved by a
  201. * writer, which has the assigned sequence number 0.
  202. */
  203. /*
  204. * Initiating Logical Value Overflows
  205. *
  206. * Both logical position (lpos) and ID values can be mapped to array indexes
  207. * but may experience overflows during the lifetime of the system. To ensure
  208. * that printk_ringbuffer can handle the overflows for these types, initial
  209. * values are chosen that map to the correct initial array indexes, but will
  210. * result in overflows soon.
  211. *
  212. * BLK0_LPOS
  213. * The initial @head_lpos and @tail_lpos for data rings. It is at index
  214. * 0 and the lpos value is such that it will overflow on the first wrap.
  215. *
  216. * DESC0_ID
  217. * The initial @head_id and @tail_id for the desc ring. It is at the last
  218. * index of the descriptor array (see Req3 above) and the ID value is such
  219. * that it will overflow on the second wrap.
  220. */
  221. #define BLK0_LPOS(sz_bits) (-(_DATA_SIZE(sz_bits)))
  222. #define DESC0_ID(ct_bits) DESC_ID(-(_DESCS_COUNT(ct_bits) + 1))
  223. #define DESC0_SV(ct_bits) DESC_SV(DESC0_ID(ct_bits), desc_reusable)
  224. /*
  225. * Define a ringbuffer with an external text data buffer. The same as
  226. * DEFINE_PRINTKRB() but requires specifying an external buffer for the
  227. * text data.
  228. *
  229. * Note: The specified external buffer must be of the size:
  230. * 2 ^ (descbits + avgtextbits)
  231. */
  232. #define _DEFINE_PRINTKRB(name, descbits, avgtextbits, text_buf) \
  233. static struct prb_desc _##name##_descs[_DESCS_COUNT(descbits)] = { \
  234. /* the initial head and tail */ \
  235. [_DESCS_COUNT(descbits) - 1] = { \
  236. /* reusable */ \
  237. .state_var = ATOMIC_INIT(DESC0_SV(descbits)), \
  238. /* no associated data block */ \
  239. .text_blk_lpos = FAILED_BLK_LPOS, \
  240. }, \
  241. }; \
  242. static struct printk_info _##name##_infos[_DESCS_COUNT(descbits)] = { \
  243. /* this will be the first record reserved by a writer */ \
  244. [0] = { \
  245. /* will be incremented to 0 on the first reservation */ \
  246. .seq = -(u64)_DESCS_COUNT(descbits), \
  247. }, \
  248. /* the initial head and tail */ \
  249. [_DESCS_COUNT(descbits) - 1] = { \
  250. /* reports the first seq value during the bootstrap phase */ \
  251. .seq = 0, \
  252. }, \
  253. }; \
  254. static struct printk_ringbuffer name = { \
  255. .desc_ring = { \
  256. .count_bits = descbits, \
  257. .descs = &_##name##_descs[0], \
  258. .infos = &_##name##_infos[0], \
  259. .head_id = ATOMIC_INIT(DESC0_ID(descbits)), \
  260. .tail_id = ATOMIC_INIT(DESC0_ID(descbits)), \
  261. .last_finalized_seq = ATOMIC_INIT(0), \
  262. }, \
  263. .text_data_ring = { \
  264. .size_bits = (avgtextbits) + (descbits), \
  265. .data = text_buf, \
  266. .head_lpos = ATOMIC_LONG_INIT(BLK0_LPOS((avgtextbits) + (descbits))), \
  267. .tail_lpos = ATOMIC_LONG_INIT(BLK0_LPOS((avgtextbits) + (descbits))), \
  268. }, \
  269. .fail = ATOMIC_LONG_INIT(0), \
  270. }
  271. /**
  272. * DEFINE_PRINTKRB() - Define a ringbuffer.
  273. *
  274. * @name: The name of the ringbuffer variable.
  275. * @descbits: The number of descriptors as a power-of-2 value.
  276. * @avgtextbits: The average text data size per record as a power-of-2 value.
  277. *
  278. * This is a macro for defining a ringbuffer and all internal structures
  279. * such that it is ready for immediate use. See _DEFINE_PRINTKRB() for a
  280. * variant where the text data buffer can be specified externally.
  281. */
  282. #define DEFINE_PRINTKRB(name, descbits, avgtextbits) \
  283. static char _##name##_text[1U << ((avgtextbits) + (descbits))] \
  284. __aligned(__alignof__(unsigned long)); \
  285. _DEFINE_PRINTKRB(name, descbits, avgtextbits, &_##name##_text[0])
  286. /* Writer Interface */
  287. /**
  288. * prb_rec_init_wr() - Initialize a buffer for writing records.
  289. *
  290. * @r: The record to initialize.
  291. * @text_buf_size: The needed text buffer size.
  292. */
  293. static inline void prb_rec_init_wr(struct printk_record *r,
  294. unsigned int text_buf_size)
  295. {
  296. r->info = NULL;
  297. r->text_buf = NULL;
  298. r->text_buf_size = text_buf_size;
  299. }
  300. bool prb_reserve(struct prb_reserved_entry *e, struct printk_ringbuffer *rb,
  301. struct printk_record *r);
  302. bool prb_reserve_in_last(struct prb_reserved_entry *e, struct printk_ringbuffer *rb,
  303. struct printk_record *r, u32 caller_id, unsigned int max_size);
  304. void prb_commit(struct prb_reserved_entry *e);
  305. void prb_final_commit(struct prb_reserved_entry *e);
  306. void prb_init(struct printk_ringbuffer *rb,
  307. char *text_buf, unsigned int text_buf_size,
  308. struct prb_desc *descs, unsigned int descs_count_bits,
  309. struct printk_info *infos);
  310. unsigned int prb_record_text_space(struct prb_reserved_entry *e);
  311. /* Reader Interface */
  312. /**
  313. * prb_rec_init_rd() - Initialize a buffer for reading records.
  314. *
  315. * @r: The record to initialize.
  316. * @info: A buffer to store record meta-data.
  317. * @text_buf: A buffer to store text data.
  318. * @text_buf_size: The size of @text_buf.
  319. *
  320. * Initialize all the fields that a reader is interested in. All arguments
  321. * (except @r) are optional. Only record data for arguments that are
  322. * non-NULL or non-zero will be read.
  323. */
  324. static inline void prb_rec_init_rd(struct printk_record *r,
  325. struct printk_info *info,
  326. char *text_buf, unsigned int text_buf_size)
  327. {
  328. r->info = info;
  329. r->text_buf = text_buf;
  330. r->text_buf_size = text_buf_size;
  331. }
  332. /**
  333. * prb_for_each_record() - Iterate over the records of a ringbuffer.
  334. *
  335. * @from: The sequence number to begin with.
  336. * @rb: The ringbuffer to iterate over.
  337. * @s: A u64 to store the sequence number on each iteration.
  338. * @r: A printk_record to store the record on each iteration.
  339. *
  340. * This is a macro for conveniently iterating over a ringbuffer.
  341. * Note that @s may not be the sequence number of the record on each
  342. * iteration. For the sequence number, @r->info->seq should be checked.
  343. *
  344. * Context: Any context.
  345. */
  346. #define prb_for_each_record(from, rb, s, r) \
  347. for ((s) = from; prb_read_valid(rb, s, r); (s) = (r)->info->seq + 1)
  348. /**
  349. * prb_for_each_info() - Iterate over the meta data of a ringbuffer.
  350. *
  351. * @from: The sequence number to begin with.
  352. * @rb: The ringbuffer to iterate over.
  353. * @s: A u64 to store the sequence number on each iteration.
  354. * @i: A printk_info to store the record meta data on each iteration.
  355. * @lc: An unsigned int to store the text line count of each record.
  356. *
  357. * This is a macro for conveniently iterating over a ringbuffer.
  358. * Note that @s may not be the sequence number of the record on each
  359. * iteration. For the sequence number, @r->info->seq should be checked.
  360. *
  361. * Context: Any context.
  362. */
  363. #define prb_for_each_info(from, rb, s, i, lc) \
  364. for ((s) = from; prb_read_valid_info(rb, s, i, lc); (s) = (i)->seq + 1)
  365. bool prb_read_valid(struct printk_ringbuffer *rb, u64 seq,
  366. struct printk_record *r);
  367. bool prb_read_valid_info(struct printk_ringbuffer *rb, u64 seq,
  368. struct printk_info *info, unsigned int *line_count);
  369. u64 prb_first_seq(struct printk_ringbuffer *rb);
  370. u64 prb_first_valid_seq(struct printk_ringbuffer *rb);
  371. u64 prb_next_seq(struct printk_ringbuffer *rb);
  372. u64 prb_next_reserve_seq(struct printk_ringbuffer *rb);
  373. #ifdef CONFIG_64BIT
  374. #define __u64seq_to_ulseq(u64seq) (u64seq)
  375. #define __ulseq_to_u64seq(rb, ulseq) (ulseq)
  376. #define ULSEQ_MAX(rb) (-1)
  377. #else /* CONFIG_64BIT */
  378. #define __u64seq_to_ulseq(u64seq) ((u32)u64seq)
  379. #define ULSEQ_MAX(rb) __u64seq_to_ulseq(prb_first_seq(rb) + 0x80000000UL)
  380. static inline u64 __ulseq_to_u64seq(struct printk_ringbuffer *rb, u32 ulseq)
  381. {
  382. u64 rb_first_seq = prb_first_seq(rb);
  383. u64 seq;
  384. /*
  385. * The provided sequence is only the lower 32 bits of the ringbuffer
  386. * sequence. It needs to be expanded to 64bit. Get the first sequence
  387. * number from the ringbuffer and fold it.
  388. *
  389. * Having a 32bit representation in the console is sufficient.
  390. * If a console ever gets more than 2^31 records behind
  391. * the ringbuffer then this is the least of the problems.
  392. *
  393. * Also the access to the ring buffer is always safe.
  394. */
  395. seq = rb_first_seq - (s32)((u32)rb_first_seq - ulseq);
  396. return seq;
  397. }
  398. #endif /* CONFIG_64BIT */
  399. #endif /* _KERNEL_PRINTK_RINGBUFFER_H */