Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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