![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0 */ 0002 0003 #ifndef _KERNEL_PRINTK_RINGBUFFER_H 0004 #define _KERNEL_PRINTK_RINGBUFFER_H 0005 0006 #include <linux/atomic.h> 0007 #include <linux/dev_printk.h> 0008 0009 /* 0010 * Meta information about each stored message. 0011 * 0012 * All fields are set by the printk code except for @seq, which is 0013 * set by the ringbuffer code. 0014 */ 0015 struct printk_info { 0016 u64 seq; /* sequence number */ 0017 u64 ts_nsec; /* timestamp in nanoseconds */ 0018 u16 text_len; /* length of text message */ 0019 u8 facility; /* syslog facility */ 0020 u8 flags:5; /* internal record flags */ 0021 u8 level:3; /* syslog level */ 0022 u32 caller_id; /* thread id or processor id */ 0023 0024 struct dev_printk_info dev_info; 0025 }; 0026 0027 /* 0028 * A structure providing the buffers, used by writers and readers. 0029 * 0030 * Writers: 0031 * Using prb_rec_init_wr(), a writer sets @text_buf_size before calling 0032 * prb_reserve(). On success, prb_reserve() sets @info and @text_buf to 0033 * buffers reserved for that writer. 0034 * 0035 * Readers: 0036 * Using prb_rec_init_rd(), a reader sets all fields before calling 0037 * prb_read_valid(). Note that the reader provides the @info and @text_buf, 0038 * buffers. On success, the struct pointed to by @info will be filled and 0039 * the char array pointed to by @text_buf will be filled with text data. 0040 */ 0041 struct printk_record { 0042 struct printk_info *info; 0043 char *text_buf; 0044 unsigned int text_buf_size; 0045 }; 0046 0047 /* Specifies the logical position and span of a data block. */ 0048 struct prb_data_blk_lpos { 0049 unsigned long begin; 0050 unsigned long next; 0051 }; 0052 0053 /* 0054 * A descriptor: the complete meta-data for a record. 0055 * 0056 * @state_var: A bitwise combination of descriptor ID and descriptor state. 0057 */ 0058 struct prb_desc { 0059 atomic_long_t state_var; 0060 struct prb_data_blk_lpos text_blk_lpos; 0061 }; 0062 0063 /* A ringbuffer of "ID + data" elements. */ 0064 struct prb_data_ring { 0065 unsigned int size_bits; 0066 char *data; 0067 atomic_long_t head_lpos; 0068 atomic_long_t tail_lpos; 0069 }; 0070 0071 /* A ringbuffer of "struct prb_desc" elements. */ 0072 struct prb_desc_ring { 0073 unsigned int count_bits; 0074 struct prb_desc *descs; 0075 struct printk_info *infos; 0076 atomic_long_t head_id; 0077 atomic_long_t tail_id; 0078 atomic_long_t last_finalized_id; 0079 }; 0080 0081 /* 0082 * The high level structure representing the printk ringbuffer. 0083 * 0084 * @fail: Count of failed prb_reserve() calls where not even a data-less 0085 * record was created. 0086 */ 0087 struct printk_ringbuffer { 0088 struct prb_desc_ring desc_ring; 0089 struct prb_data_ring text_data_ring; 0090 atomic_long_t fail; 0091 }; 0092 0093 /* 0094 * Used by writers as a reserve/commit handle. 0095 * 0096 * @rb: Ringbuffer where the entry is reserved. 0097 * @irqflags: Saved irq flags to restore on entry commit. 0098 * @id: ID of the reserved descriptor. 0099 * @text_space: Total occupied buffer space in the text data ring, including 0100 * ID, alignment padding, and wrapping data blocks. 0101 * 0102 * This structure is an opaque handle for writers. Its contents are only 0103 * to be used by the ringbuffer implementation. 0104 */ 0105 struct prb_reserved_entry { 0106 struct printk_ringbuffer *rb; 0107 unsigned long irqflags; 0108 unsigned long id; 0109 unsigned int text_space; 0110 }; 0111 0112 /* The possible responses of a descriptor state-query. */ 0113 enum desc_state { 0114 desc_miss = -1, /* ID mismatch (pseudo state) */ 0115 desc_reserved = 0x0, /* reserved, in use by writer */ 0116 desc_committed = 0x1, /* committed by writer, could get reopened */ 0117 desc_finalized = 0x2, /* committed, no further modification allowed */ 0118 desc_reusable = 0x3, /* free, not yet used by any writer */ 0119 }; 0120 0121 #define _DATA_SIZE(sz_bits) (1UL << (sz_bits)) 0122 #define _DESCS_COUNT(ct_bits) (1U << (ct_bits)) 0123 #define DESC_SV_BITS (sizeof(unsigned long) * 8) 0124 #define DESC_FLAGS_SHIFT (DESC_SV_BITS - 2) 0125 #define DESC_FLAGS_MASK (3UL << DESC_FLAGS_SHIFT) 0126 #define DESC_STATE(sv) (3UL & (sv >> DESC_FLAGS_SHIFT)) 0127 #define DESC_SV(id, state) (((unsigned long)state << DESC_FLAGS_SHIFT) | id) 0128 #define DESC_ID_MASK (~DESC_FLAGS_MASK) 0129 #define DESC_ID(sv) ((sv) & DESC_ID_MASK) 0130 #define FAILED_LPOS 0x1 0131 #define NO_LPOS 0x3 0132 0133 #define FAILED_BLK_LPOS \ 0134 { \ 0135 .begin = FAILED_LPOS, \ 0136 .next = FAILED_LPOS, \ 0137 } 0138 0139 /* 0140 * Descriptor Bootstrap 0141 * 0142 * The descriptor array is minimally initialized to allow immediate usage 0143 * by readers and writers. The requirements that the descriptor array 0144 * initialization must satisfy: 0145 * 0146 * Req1 0147 * The tail must point to an existing (committed or reusable) descriptor. 0148 * This is required by the implementation of prb_first_seq(). 0149 * 0150 * Req2 0151 * Readers must see that the ringbuffer is initially empty. 0152 * 0153 * Req3 0154 * The first record reserved by a writer is assigned sequence number 0. 0155 * 0156 * To satisfy Req1, the tail initially points to a descriptor that is 0157 * minimally initialized (having no data block, i.e. data-less with the 0158 * data block's lpos @begin and @next values set to FAILED_LPOS). 0159 * 0160 * To satisfy Req2, the initial tail descriptor is initialized to the 0161 * reusable state. Readers recognize reusable descriptors as existing 0162 * records, but skip over them. 0163 * 0164 * To satisfy Req3, the last descriptor in the array is used as the initial 0165 * head (and tail) descriptor. This allows the first record reserved by a 0166 * writer (head + 1) to be the first descriptor in the array. (Only the first 0167 * descriptor in the array could have a valid sequence number of 0.) 0168 * 0169 * The first time a descriptor is reserved, it is assigned a sequence number 0170 * with the value of the array index. A "first time reserved" descriptor can 0171 * be recognized because it has a sequence number of 0 but does not have an 0172 * index of 0. (Only the first descriptor in the array could have a valid 0173 * sequence number of 0.) After the first reservation, all future reservations 0174 * (recycling) simply involve incrementing the sequence number by the array 0175 * count. 0176 * 0177 * Hack #1 0178 * Only the first descriptor in the array is allowed to have the sequence 0179 * number 0. In this case it is not possible to recognize if it is being 0180 * reserved the first time (set to index value) or has been reserved 0181 * previously (increment by the array count). This is handled by _always_ 0182 * incrementing the sequence number by the array count when reserving the 0183 * first descriptor in the array. In order to satisfy Req3, the sequence 0184 * number of the first descriptor in the array is initialized to minus 0185 * the array count. Then, upon the first reservation, it is incremented 0186 * to 0, thus satisfying Req3. 0187 * 0188 * Hack #2 0189 * prb_first_seq() can be called at any time by readers to retrieve the 0190 * sequence number of the tail descriptor. However, due to Req2 and Req3, 0191 * initially there are no records to report the sequence number of 0192 * (sequence numbers are u64 and there is nothing less than 0). To handle 0193 * this, the sequence number of the initial tail descriptor is initialized 0194 * to 0. Technically this is incorrect, because there is no record with 0195 * sequence number 0 (yet) and the tail descriptor is not the first 0196 * descriptor in the array. But it allows prb_read_valid() to correctly 0197 * report the existence of a record for _any_ given sequence number at all 0198 * times. Bootstrapping is complete when the tail is pushed the first 0199 * time, thus finally pointing to the first descriptor reserved by a 0200 * writer, which has the assigned sequence number 0. 0201 */ 0202 0203 /* 0204 * Initiating Logical Value Overflows 0205 * 0206 * Both logical position (lpos) and ID values can be mapped to array indexes 0207 * but may experience overflows during the lifetime of the system. To ensure 0208 * that printk_ringbuffer can handle the overflows for these types, initial 0209 * values are chosen that map to the correct initial array indexes, but will 0210 * result in overflows soon. 0211 * 0212 * BLK0_LPOS 0213 * The initial @head_lpos and @tail_lpos for data rings. It is at index 0214 * 0 and the lpos value is such that it will overflow on the first wrap. 0215 * 0216 * DESC0_ID 0217 * The initial @head_id and @tail_id for the desc ring. It is at the last 0218 * index of the descriptor array (see Req3 above) and the ID value is such 0219 * that it will overflow on the second wrap. 0220 */ 0221 #define BLK0_LPOS(sz_bits) (-(_DATA_SIZE(sz_bits))) 0222 #define DESC0_ID(ct_bits) DESC_ID(-(_DESCS_COUNT(ct_bits) + 1)) 0223 #define DESC0_SV(ct_bits) DESC_SV(DESC0_ID(ct_bits), desc_reusable) 0224 0225 /* 0226 * Define a ringbuffer with an external text data buffer. The same as 0227 * DEFINE_PRINTKRB() but requires specifying an external buffer for the 0228 * text data. 0229 * 0230 * Note: The specified external buffer must be of the size: 0231 * 2 ^ (descbits + avgtextbits) 0232 */ 0233 #define _DEFINE_PRINTKRB(name, descbits, avgtextbits, text_buf) \ 0234 static struct prb_desc _##name##_descs[_DESCS_COUNT(descbits)] = { \ 0235 /* the initial head and tail */ \ 0236 [_DESCS_COUNT(descbits) - 1] = { \ 0237 /* reusable */ \ 0238 .state_var = ATOMIC_INIT(DESC0_SV(descbits)), \ 0239 /* no associated data block */ \ 0240 .text_blk_lpos = FAILED_BLK_LPOS, \ 0241 }, \ 0242 }; \ 0243 static struct printk_info _##name##_infos[_DESCS_COUNT(descbits)] = { \ 0244 /* this will be the first record reserved by a writer */ \ 0245 [0] = { \ 0246 /* will be incremented to 0 on the first reservation */ \ 0247 .seq = -(u64)_DESCS_COUNT(descbits), \ 0248 }, \ 0249 /* the initial head and tail */ \ 0250 [_DESCS_COUNT(descbits) - 1] = { \ 0251 /* reports the first seq value during the bootstrap phase */ \ 0252 .seq = 0, \ 0253 }, \ 0254 }; \ 0255 static struct printk_ringbuffer name = { \ 0256 .desc_ring = { \ 0257 .count_bits = descbits, \ 0258 .descs = &_##name##_descs[0], \ 0259 .infos = &_##name##_infos[0], \ 0260 .head_id = ATOMIC_INIT(DESC0_ID(descbits)), \ 0261 .tail_id = ATOMIC_INIT(DESC0_ID(descbits)), \ 0262 .last_finalized_id = ATOMIC_INIT(DESC0_ID(descbits)), \ 0263 }, \ 0264 .text_data_ring = { \ 0265 .size_bits = (avgtextbits) + (descbits), \ 0266 .data = text_buf, \ 0267 .head_lpos = ATOMIC_LONG_INIT(BLK0_LPOS((avgtextbits) + (descbits))), \ 0268 .tail_lpos = ATOMIC_LONG_INIT(BLK0_LPOS((avgtextbits) + (descbits))), \ 0269 }, \ 0270 .fail = ATOMIC_LONG_INIT(0), \ 0271 } 0272 0273 /** 0274 * DEFINE_PRINTKRB() - Define a ringbuffer. 0275 * 0276 * @name: The name of the ringbuffer variable. 0277 * @descbits: The number of descriptors as a power-of-2 value. 0278 * @avgtextbits: The average text data size per record as a power-of-2 value. 0279 * 0280 * This is a macro for defining a ringbuffer and all internal structures 0281 * such that it is ready for immediate use. See _DEFINE_PRINTKRB() for a 0282 * variant where the text data buffer can be specified externally. 0283 */ 0284 #define DEFINE_PRINTKRB(name, descbits, avgtextbits) \ 0285 static char _##name##_text[1U << ((avgtextbits) + (descbits))] \ 0286 __aligned(__alignof__(unsigned long)); \ 0287 _DEFINE_PRINTKRB(name, descbits, avgtextbits, &_##name##_text[0]) 0288 0289 /* Writer Interface */ 0290 0291 /** 0292 * prb_rec_init_wr() - Initialize a buffer for writing records. 0293 * 0294 * @r: The record to initialize. 0295 * @text_buf_size: The needed text buffer size. 0296 */ 0297 static inline void prb_rec_init_wr(struct printk_record *r, 0298 unsigned int text_buf_size) 0299 { 0300 r->info = NULL; 0301 r->text_buf = NULL; 0302 r->text_buf_size = text_buf_size; 0303 } 0304 0305 bool prb_reserve(struct prb_reserved_entry *e, struct printk_ringbuffer *rb, 0306 struct printk_record *r); 0307 bool prb_reserve_in_last(struct prb_reserved_entry *e, struct printk_ringbuffer *rb, 0308 struct printk_record *r, u32 caller_id, unsigned int max_size); 0309 void prb_commit(struct prb_reserved_entry *e); 0310 void prb_final_commit(struct prb_reserved_entry *e); 0311 0312 void prb_init(struct printk_ringbuffer *rb, 0313 char *text_buf, unsigned int text_buf_size, 0314 struct prb_desc *descs, unsigned int descs_count_bits, 0315 struct printk_info *infos); 0316 unsigned int prb_record_text_space(struct prb_reserved_entry *e); 0317 0318 /* Reader Interface */ 0319 0320 /** 0321 * prb_rec_init_rd() - Initialize a buffer for reading records. 0322 * 0323 * @r: The record to initialize. 0324 * @info: A buffer to store record meta-data. 0325 * @text_buf: A buffer to store text data. 0326 * @text_buf_size: The size of @text_buf. 0327 * 0328 * Initialize all the fields that a reader is interested in. All arguments 0329 * (except @r) are optional. Only record data for arguments that are 0330 * non-NULL or non-zero will be read. 0331 */ 0332 static inline void prb_rec_init_rd(struct printk_record *r, 0333 struct printk_info *info, 0334 char *text_buf, unsigned int text_buf_size) 0335 { 0336 r->info = info; 0337 r->text_buf = text_buf; 0338 r->text_buf_size = text_buf_size; 0339 } 0340 0341 /** 0342 * prb_for_each_record() - Iterate over the records of a ringbuffer. 0343 * 0344 * @from: The sequence number to begin with. 0345 * @rb: The ringbuffer to iterate over. 0346 * @s: A u64 to store the sequence number on each iteration. 0347 * @r: A printk_record to store the record on each iteration. 0348 * 0349 * This is a macro for conveniently iterating over a ringbuffer. 0350 * Note that @s may not be the sequence number of the record on each 0351 * iteration. For the sequence number, @r->info->seq should be checked. 0352 * 0353 * Context: Any context. 0354 */ 0355 #define prb_for_each_record(from, rb, s, r) \ 0356 for ((s) = from; prb_read_valid(rb, s, r); (s) = (r)->info->seq + 1) 0357 0358 /** 0359 * prb_for_each_info() - Iterate over the meta data of a ringbuffer. 0360 * 0361 * @from: The sequence number to begin with. 0362 * @rb: The ringbuffer to iterate over. 0363 * @s: A u64 to store the sequence number on each iteration. 0364 * @i: A printk_info to store the record meta data on each iteration. 0365 * @lc: An unsigned int to store the text line count of each record. 0366 * 0367 * This is a macro for conveniently iterating over a ringbuffer. 0368 * Note that @s may not be the sequence number of the record on each 0369 * iteration. For the sequence number, @r->info->seq should be checked. 0370 * 0371 * Context: Any context. 0372 */ 0373 #define prb_for_each_info(from, rb, s, i, lc) \ 0374 for ((s) = from; prb_read_valid_info(rb, s, i, lc); (s) = (i)->seq + 1) 0375 0376 bool prb_read_valid(struct printk_ringbuffer *rb, u64 seq, 0377 struct printk_record *r); 0378 bool prb_read_valid_info(struct printk_ringbuffer *rb, u64 seq, 0379 struct printk_info *info, unsigned int *line_count); 0380 0381 u64 prb_first_valid_seq(struct printk_ringbuffer *rb); 0382 u64 prb_next_seq(struct printk_ringbuffer *rb); 0383 0384 #endif /* _KERNEL_PRINTK_RINGBUFFER_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |