1b6cf8b3fSJohn Ogness // SPDX-License-Identifier: GPL-2.0
2b6cf8b3fSJohn Ogness
3b6cf8b3fSJohn Ogness #include <linux/kernel.h>
4b6cf8b3fSJohn Ogness #include <linux/irqflags.h>
5b6cf8b3fSJohn Ogness #include <linux/string.h>
6b6cf8b3fSJohn Ogness #include <linux/errno.h>
7b6cf8b3fSJohn Ogness #include <linux/bug.h>
8b6cf8b3fSJohn Ogness #include "printk_ringbuffer.h"
95f72e52bSJohn Ogness #include "internal.h"
10b6cf8b3fSJohn Ogness
11b6cf8b3fSJohn Ogness /**
12b6cf8b3fSJohn Ogness * DOC: printk_ringbuffer overview
13b6cf8b3fSJohn Ogness *
14b6cf8b3fSJohn Ogness * Data Structure
15b6cf8b3fSJohn Ogness * --------------
16b6cf8b3fSJohn Ogness * The printk_ringbuffer is made up of 3 internal ringbuffers:
17b6cf8b3fSJohn Ogness *
18b6cf8b3fSJohn Ogness * desc_ring
19cfe2790bSJohn Ogness * A ring of descriptors and their meta data (such as sequence number,
20cfe2790bSJohn Ogness * timestamp, loglevel, etc.) as well as internal state information about
21cfe2790bSJohn Ogness * the record and logical positions specifying where in the other
22f35efc78SJohn Ogness * ringbuffer the text strings are located.
23b6cf8b3fSJohn Ogness *
24b6cf8b3fSJohn Ogness * text_data_ring
25b6cf8b3fSJohn Ogness * A ring of data blocks. A data block consists of an unsigned long
26b6cf8b3fSJohn Ogness * integer (ID) that maps to a desc_ring index followed by the text
27b6cf8b3fSJohn Ogness * string of the record.
28b6cf8b3fSJohn Ogness *
29b6cf8b3fSJohn Ogness * The internal state information of a descriptor is the key element to allow
30b6cf8b3fSJohn Ogness * readers and writers to locklessly synchronize access to the data.
31b6cf8b3fSJohn Ogness *
32b6cf8b3fSJohn Ogness * Implementation
33b6cf8b3fSJohn Ogness * --------------
34b6cf8b3fSJohn Ogness *
35b6cf8b3fSJohn Ogness * Descriptor Ring
36b6cf8b3fSJohn Ogness * ~~~~~~~~~~~~~~~
37cfe2790bSJohn Ogness * The descriptor ring is an array of descriptors. A descriptor contains
38cfe2790bSJohn Ogness * essential meta data to track the data of a printk record using
39f35efc78SJohn Ogness * blk_lpos structs pointing to associated text data blocks (see
40f35efc78SJohn Ogness * "Data Rings" below). Each descriptor is assigned an ID that maps
41cfe2790bSJohn Ogness * directly to index values of the descriptor array and has a state. The ID
42cfe2790bSJohn Ogness * and the state are bitwise combined into a single descriptor field named
43cfe2790bSJohn Ogness * @state_var, allowing ID and state to be synchronously and atomically
44cfe2790bSJohn Ogness * updated.
45b6cf8b3fSJohn Ogness *
464cfc7258SJohn Ogness * Descriptors have four states:
47b6cf8b3fSJohn Ogness *
48b6cf8b3fSJohn Ogness * reserved
49b6cf8b3fSJohn Ogness * A writer is modifying the record.
50b6cf8b3fSJohn Ogness *
51b6cf8b3fSJohn Ogness * committed
524cfc7258SJohn Ogness * The record and all its data are written. A writer can reopen the
534cfc7258SJohn Ogness * descriptor (transitioning it back to reserved), but in the committed
544cfc7258SJohn Ogness * state the data is consistent.
554cfc7258SJohn Ogness *
564cfc7258SJohn Ogness * finalized
574cfc7258SJohn Ogness * The record and all its data are complete and available for reading. A
584cfc7258SJohn Ogness * writer cannot reopen the descriptor.
59b6cf8b3fSJohn Ogness *
60b6cf8b3fSJohn Ogness * reusable
61f35efc78SJohn Ogness * The record exists, but its text and/or meta data may no longer be
62f35efc78SJohn Ogness * available.
63b6cf8b3fSJohn Ogness *
64b6cf8b3fSJohn Ogness * Querying the @state_var of a record requires providing the ID of the
654cfc7258SJohn Ogness * descriptor to query. This can yield a possible fifth (pseudo) state:
66b6cf8b3fSJohn Ogness *
67b6cf8b3fSJohn Ogness * miss
68b6cf8b3fSJohn Ogness * The descriptor being queried has an unexpected ID.
69b6cf8b3fSJohn Ogness *
70b6cf8b3fSJohn Ogness * The descriptor ring has a @tail_id that contains the ID of the oldest
71b6cf8b3fSJohn Ogness * descriptor and @head_id that contains the ID of the newest descriptor.
72b6cf8b3fSJohn Ogness *
73b6cf8b3fSJohn Ogness * When a new descriptor should be created (and the ring is full), the tail
74b6cf8b3fSJohn Ogness * descriptor is invalidated by first transitioning to the reusable state and
75b6cf8b3fSJohn Ogness * then invalidating all tail data blocks up to and including the data blocks
76f35efc78SJohn Ogness * associated with the tail descriptor (for the text ring). Then
77b6cf8b3fSJohn Ogness * @tail_id is advanced, followed by advancing @head_id. And finally the
78b6cf8b3fSJohn Ogness * @state_var of the new descriptor is initialized to the new ID and reserved
79b6cf8b3fSJohn Ogness * state.
80b6cf8b3fSJohn Ogness *
81b6cf8b3fSJohn Ogness * The @tail_id can only be advanced if the new @tail_id would be in the
82b6cf8b3fSJohn Ogness * committed or reusable queried state. This makes it possible that a valid
83b6cf8b3fSJohn Ogness * sequence number of the tail is always available.
84b6cf8b3fSJohn Ogness *
854cfc7258SJohn Ogness * Descriptor Finalization
864cfc7258SJohn Ogness * ~~~~~~~~~~~~~~~~~~~~~~~
874cfc7258SJohn Ogness * When a writer calls the commit function prb_commit(), record data is
884cfc7258SJohn Ogness * fully stored and is consistent within the ringbuffer. However, a writer can
894cfc7258SJohn Ogness * reopen that record, claiming exclusive access (as with prb_reserve()), and
904cfc7258SJohn Ogness * modify that record. When finished, the writer must again commit the record.
914cfc7258SJohn Ogness *
924cfc7258SJohn Ogness * In order for a record to be made available to readers (and also become
934cfc7258SJohn Ogness * recyclable for writers), it must be finalized. A finalized record cannot be
944cfc7258SJohn Ogness * reopened and can never become "unfinalized". Record finalization can occur
954cfc7258SJohn Ogness * in three different scenarios:
964cfc7258SJohn Ogness *
974cfc7258SJohn Ogness * 1) A writer can simultaneously commit and finalize its record by calling
984cfc7258SJohn Ogness * prb_final_commit() instead of prb_commit().
994cfc7258SJohn Ogness *
1004cfc7258SJohn Ogness * 2) When a new record is reserved and the previous record has been
1014cfc7258SJohn Ogness * committed via prb_commit(), that previous record is automatically
1024cfc7258SJohn Ogness * finalized.
1034cfc7258SJohn Ogness *
1044cfc7258SJohn Ogness * 3) When a record is committed via prb_commit() and a newer record
1054cfc7258SJohn Ogness * already exists, the record being committed is automatically finalized.
1064cfc7258SJohn Ogness *
107f35efc78SJohn Ogness * Data Ring
108f35efc78SJohn Ogness * ~~~~~~~~~
109f35efc78SJohn Ogness * The text data ring is a byte array composed of data blocks. Data blocks are
110b6cf8b3fSJohn Ogness * referenced by blk_lpos structs that point to the logical position of the
111b6cf8b3fSJohn Ogness * beginning of a data block and the beginning of the next adjacent data
112b6cf8b3fSJohn Ogness * block. Logical positions are mapped directly to index values of the byte
113b6cf8b3fSJohn Ogness * array ringbuffer.
114b6cf8b3fSJohn Ogness *
115b6cf8b3fSJohn Ogness * Each data block consists of an ID followed by the writer data. The ID is
116b6cf8b3fSJohn Ogness * the identifier of a descriptor that is associated with the data block. A
117b6cf8b3fSJohn Ogness * given data block is considered valid if all of the following conditions
118b6cf8b3fSJohn Ogness * are met:
119b6cf8b3fSJohn Ogness *
120b6cf8b3fSJohn Ogness * 1) The descriptor associated with the data block is in the committed
1214cfc7258SJohn Ogness * or finalized queried state.
122b6cf8b3fSJohn Ogness *
123b6cf8b3fSJohn Ogness * 2) The blk_lpos struct within the descriptor associated with the data
124b6cf8b3fSJohn Ogness * block references back to the same data block.
125b6cf8b3fSJohn Ogness *
126b6cf8b3fSJohn Ogness * 3) The data block is within the head/tail logical position range.
127b6cf8b3fSJohn Ogness *
128b6cf8b3fSJohn Ogness * If the writer data of a data block would extend beyond the end of the
129b6cf8b3fSJohn Ogness * byte array, only the ID of the data block is stored at the logical
130b6cf8b3fSJohn Ogness * position and the full data block (ID and writer data) is stored at the
131b6cf8b3fSJohn Ogness * beginning of the byte array. The referencing blk_lpos will point to the
132b6cf8b3fSJohn Ogness * ID before the wrap and the next data block will be at the logical
133b6cf8b3fSJohn Ogness * position adjacent the full data block after the wrap.
134b6cf8b3fSJohn Ogness *
135b6cf8b3fSJohn Ogness * Data rings have a @tail_lpos that points to the beginning of the oldest
136b6cf8b3fSJohn Ogness * data block and a @head_lpos that points to the logical position of the
137b6cf8b3fSJohn Ogness * next (not yet existing) data block.
138b6cf8b3fSJohn Ogness *
139b6cf8b3fSJohn Ogness * When a new data block should be created (and the ring is full), tail data
140b6cf8b3fSJohn Ogness * blocks will first be invalidated by putting their associated descriptors
141b6cf8b3fSJohn Ogness * into the reusable state and then pushing the @tail_lpos forward beyond
142b6cf8b3fSJohn Ogness * them. Then the @head_lpos is pushed forward and is associated with a new
143b6cf8b3fSJohn Ogness * descriptor. If a data block is not valid, the @tail_lpos cannot be
144b6cf8b3fSJohn Ogness * advanced beyond it.
145b6cf8b3fSJohn Ogness *
146cfe2790bSJohn Ogness * Info Array
147cfe2790bSJohn Ogness * ~~~~~~~~~~
148cfe2790bSJohn Ogness * The general meta data of printk records are stored in printk_info structs,
149cfe2790bSJohn Ogness * stored in an array with the same number of elements as the descriptor ring.
150cfe2790bSJohn Ogness * Each info corresponds to the descriptor of the same index in the
151cfe2790bSJohn Ogness * descriptor ring. Info validity is confirmed by evaluating the corresponding
152cfe2790bSJohn Ogness * descriptor before and after loading the info.
153cfe2790bSJohn Ogness *
154b6cf8b3fSJohn Ogness * Usage
155b6cf8b3fSJohn Ogness * -----
156b6cf8b3fSJohn Ogness * Here are some simple examples demonstrating writers and readers. For the
157b6cf8b3fSJohn Ogness * examples a global ringbuffer (test_rb) is available (which is not the
158b6cf8b3fSJohn Ogness * actual ringbuffer used by printk)::
159b6cf8b3fSJohn Ogness *
160f35efc78SJohn Ogness * DEFINE_PRINTKRB(test_rb, 15, 5);
161b6cf8b3fSJohn Ogness *
162b6cf8b3fSJohn Ogness * This ringbuffer allows up to 32768 records (2 ^ 15) and has a size of
163f35efc78SJohn Ogness * 1 MiB (2 ^ (15 + 5)) for text data.
164b6cf8b3fSJohn Ogness *
165b6cf8b3fSJohn Ogness * Sample writer code::
166b6cf8b3fSJohn Ogness *
167b6cf8b3fSJohn Ogness * const char *textstr = "message text";
168b6cf8b3fSJohn Ogness * struct prb_reserved_entry e;
169b6cf8b3fSJohn Ogness * struct printk_record r;
170b6cf8b3fSJohn Ogness *
171b6cf8b3fSJohn Ogness * // specify how much to allocate
172f35efc78SJohn Ogness * prb_rec_init_wr(&r, strlen(textstr) + 1);
173b6cf8b3fSJohn Ogness *
174b6cf8b3fSJohn Ogness * if (prb_reserve(&e, &test_rb, &r)) {
175b6cf8b3fSJohn Ogness * snprintf(r.text_buf, r.text_buf_size, "%s", textstr);
176f35efc78SJohn Ogness *
177cc5c7041SJohn Ogness * r.info->text_len = strlen(textstr);
178b6cf8b3fSJohn Ogness * r.info->ts_nsec = local_clock();
179f35efc78SJohn Ogness * r.info->caller_id = printk_caller_id();
180b6cf8b3fSJohn Ogness *
181f35efc78SJohn Ogness * // commit and finalize the record
1824cfc7258SJohn Ogness * prb_final_commit(&e);
1834cfc7258SJohn Ogness * }
1844cfc7258SJohn Ogness *
1854cfc7258SJohn Ogness * Note that additional writer functions are available to extend a record
1864cfc7258SJohn Ogness * after it has been committed but not yet finalized. This can be done as
1874cfc7258SJohn Ogness * long as no new records have been reserved and the caller is the same.
1884cfc7258SJohn Ogness *
1894cfc7258SJohn Ogness * Sample writer code (record extending)::
1904cfc7258SJohn Ogness *
1914cfc7258SJohn Ogness * // alternate rest of previous example
192f35efc78SJohn Ogness *
1934cfc7258SJohn Ogness * r.info->text_len = strlen(textstr);
194f35efc78SJohn Ogness * r.info->ts_nsec = local_clock();
1954cfc7258SJohn Ogness * r.info->caller_id = printk_caller_id();
1964cfc7258SJohn Ogness *
1974cfc7258SJohn Ogness * // commit the record (but do not finalize yet)
198b6cf8b3fSJohn Ogness * prb_commit(&e);
199b6cf8b3fSJohn Ogness * }
200b6cf8b3fSJohn Ogness *
2014cfc7258SJohn Ogness * ...
2024cfc7258SJohn Ogness *
2034cfc7258SJohn Ogness * // specify additional 5 bytes text space to extend
204f35efc78SJohn Ogness * prb_rec_init_wr(&r, 5);
2054cfc7258SJohn Ogness *
20659f8bccaSJohn Ogness * // try to extend, but only if it does not exceed 32 bytes
2077b0592a2SWang Honghui * if (prb_reserve_in_last(&e, &test_rb, &r, printk_caller_id(), 32)) {
2084cfc7258SJohn Ogness * snprintf(&r.text_buf[r.info->text_len],
2094cfc7258SJohn Ogness * r.text_buf_size - r.info->text_len, "hello");
2104cfc7258SJohn Ogness *
2114cfc7258SJohn Ogness * r.info->text_len += 5;
2124cfc7258SJohn Ogness *
213f35efc78SJohn Ogness * // commit and finalize the record
2144cfc7258SJohn Ogness * prb_final_commit(&e);
2154cfc7258SJohn Ogness * }
2164cfc7258SJohn Ogness *
217b6cf8b3fSJohn Ogness * Sample reader code::
218b6cf8b3fSJohn Ogness *
219b6cf8b3fSJohn Ogness * struct printk_info info;
220b6cf8b3fSJohn Ogness * struct printk_record r;
221b6cf8b3fSJohn Ogness * char text_buf[32];
222b6cf8b3fSJohn Ogness * u64 seq;
223b6cf8b3fSJohn Ogness *
224f35efc78SJohn Ogness * prb_rec_init_rd(&r, &info, &text_buf[0], sizeof(text_buf));
225b6cf8b3fSJohn Ogness *
226b6cf8b3fSJohn Ogness * prb_for_each_record(0, &test_rb, &seq, &r) {
227b6cf8b3fSJohn Ogness * if (info.seq != seq)
228b6cf8b3fSJohn Ogness * pr_warn("lost %llu records\n", info.seq - seq);
229b6cf8b3fSJohn Ogness *
230b6cf8b3fSJohn Ogness * if (info.text_len > r.text_buf_size) {
231b6cf8b3fSJohn Ogness * pr_warn("record %llu text truncated\n", info.seq);
232b6cf8b3fSJohn Ogness * text_buf[r.text_buf_size - 1] = 0;
233b6cf8b3fSJohn Ogness * }
234b6cf8b3fSJohn Ogness *
235f35efc78SJohn Ogness * pr_info("%llu: %llu: %s\n", info.seq, info.ts_nsec,
236f35efc78SJohn Ogness * &text_buf[0]);
237b6cf8b3fSJohn Ogness * }
238b6cf8b3fSJohn Ogness *
239b6cf8b3fSJohn Ogness * Note that additional less convenient reader functions are available to
240b6cf8b3fSJohn Ogness * allow complex record access.
241b6cf8b3fSJohn Ogness *
242b6cf8b3fSJohn Ogness * ABA Issues
243b6cf8b3fSJohn Ogness * ~~~~~~~~~~
244b6cf8b3fSJohn Ogness * To help avoid ABA issues, descriptors are referenced by IDs (array index
245b6cf8b3fSJohn Ogness * values combined with tagged bits counting array wraps) and data blocks are
246b6cf8b3fSJohn Ogness * referenced by logical positions (array index values combined with tagged
247b6cf8b3fSJohn Ogness * bits counting array wraps). However, on 32-bit systems the number of
248b6cf8b3fSJohn Ogness * tagged bits is relatively small such that an ABA incident is (at least
249b6cf8b3fSJohn Ogness * theoretically) possible. For example, if 4 million maximally sized (1KiB)
250b6cf8b3fSJohn Ogness * printk messages were to occur in NMI context on a 32-bit system, the
251b6cf8b3fSJohn Ogness * interrupted context would not be able to recognize that the 32-bit integer
252b6cf8b3fSJohn Ogness * completely wrapped and thus represents a different data block than the one
253b6cf8b3fSJohn Ogness * the interrupted context expects.
254b6cf8b3fSJohn Ogness *
255b6cf8b3fSJohn Ogness * To help combat this possibility, additional state checking is performed
256b6cf8b3fSJohn Ogness * (such as using cmpxchg() even though set() would suffice). These extra
257b6cf8b3fSJohn Ogness * checks are commented as such and will hopefully catch any ABA issue that
258b6cf8b3fSJohn Ogness * a 32-bit system might experience.
259b6cf8b3fSJohn Ogness *
260b6cf8b3fSJohn Ogness * Memory Barriers
261b6cf8b3fSJohn Ogness * ~~~~~~~~~~~~~~~
262b6cf8b3fSJohn Ogness * Multiple memory barriers are used. To simplify proving correctness and
263b6cf8b3fSJohn Ogness * generating litmus tests, lines of code related to memory barriers
264b6cf8b3fSJohn Ogness * (loads, stores, and the associated memory barriers) are labeled::
265b6cf8b3fSJohn Ogness *
266b6cf8b3fSJohn Ogness * LMM(function:letter)
267b6cf8b3fSJohn Ogness *
268b6cf8b3fSJohn Ogness * Comments reference the labels using only the "function:letter" part.
269b6cf8b3fSJohn Ogness *
270b6cf8b3fSJohn Ogness * The memory barrier pairs and their ordering are:
271b6cf8b3fSJohn Ogness *
272b6cf8b3fSJohn Ogness * desc_reserve:D / desc_reserve:B
273b6cf8b3fSJohn Ogness * push descriptor tail (id), then push descriptor head (id)
274b6cf8b3fSJohn Ogness *
275b6cf8b3fSJohn Ogness * desc_reserve:D / data_push_tail:B
276b6cf8b3fSJohn Ogness * push data tail (lpos), then set new descriptor reserved (state)
277b6cf8b3fSJohn Ogness *
278b6cf8b3fSJohn Ogness * desc_reserve:D / desc_push_tail:C
279b6cf8b3fSJohn Ogness * push descriptor tail (id), then set new descriptor reserved (state)
280b6cf8b3fSJohn Ogness *
281b6cf8b3fSJohn Ogness * desc_reserve:D / prb_first_seq:C
282b6cf8b3fSJohn Ogness * push descriptor tail (id), then set new descriptor reserved (state)
283b6cf8b3fSJohn Ogness *
284b6cf8b3fSJohn Ogness * desc_reserve:F / desc_read:D
285b6cf8b3fSJohn Ogness * set new descriptor id and reserved (state), then allow writer changes
286b6cf8b3fSJohn Ogness *
2874cfc7258SJohn Ogness * data_alloc:A (or data_realloc:A) / desc_read:D
288b6cf8b3fSJohn Ogness * set old descriptor reusable (state), then modify new data block area
289b6cf8b3fSJohn Ogness *
2904cfc7258SJohn Ogness * data_alloc:A (or data_realloc:A) / data_push_tail:B
291b6cf8b3fSJohn Ogness * push data tail (lpos), then modify new data block area
292b6cf8b3fSJohn Ogness *
2934cfc7258SJohn Ogness * _prb_commit:B / desc_read:B
294b6cf8b3fSJohn Ogness * store writer changes, then set new descriptor committed (state)
295b6cf8b3fSJohn Ogness *
2964cfc7258SJohn Ogness * desc_reopen_last:A / _prb_commit:B
2974cfc7258SJohn Ogness * set descriptor reserved (state), then read descriptor data
2984cfc7258SJohn Ogness *
2994cfc7258SJohn Ogness * _prb_commit:B / desc_reserve:D
3004cfc7258SJohn Ogness * set new descriptor committed (state), then check descriptor head (id)
3014cfc7258SJohn Ogness *
302b6cf8b3fSJohn Ogness * data_push_tail:D / data_push_tail:A
303b6cf8b3fSJohn Ogness * set descriptor reusable (state), then push data tail (lpos)
304b6cf8b3fSJohn Ogness *
305b6cf8b3fSJohn Ogness * desc_push_tail:B / desc_reserve:D
306b6cf8b3fSJohn Ogness * set descriptor reusable (state), then push descriptor tail (id)
3075f72e52bSJohn Ogness *
3085f72e52bSJohn Ogness * desc_update_last_finalized:A / desc_last_finalized_seq:A
3095f72e52bSJohn Ogness * store finalized record, then set new highest finalized sequence number
310b6cf8b3fSJohn Ogness */
311b6cf8b3fSJohn Ogness
312b6cf8b3fSJohn Ogness #define DATA_SIZE(data_ring) _DATA_SIZE((data_ring)->size_bits)
313b6cf8b3fSJohn Ogness #define DATA_SIZE_MASK(data_ring) (DATA_SIZE(data_ring) - 1)
314b6cf8b3fSJohn Ogness
315b6cf8b3fSJohn Ogness #define DESCS_COUNT(desc_ring) _DESCS_COUNT((desc_ring)->count_bits)
316b6cf8b3fSJohn Ogness #define DESCS_COUNT_MASK(desc_ring) (DESCS_COUNT(desc_ring) - 1)
317b6cf8b3fSJohn Ogness
318b6cf8b3fSJohn Ogness /* Determine the data array index from a logical position. */
319b6cf8b3fSJohn Ogness #define DATA_INDEX(data_ring, lpos) ((lpos) & DATA_SIZE_MASK(data_ring))
320b6cf8b3fSJohn Ogness
321b6cf8b3fSJohn Ogness /* Determine the desc array index from an ID or sequence number. */
322b6cf8b3fSJohn Ogness #define DESC_INDEX(desc_ring, n) ((n) & DESCS_COUNT_MASK(desc_ring))
323b6cf8b3fSJohn Ogness
324b6cf8b3fSJohn Ogness /* Determine how many times the data array has wrapped. */
325b6cf8b3fSJohn Ogness #define DATA_WRAPS(data_ring, lpos) ((lpos) >> (data_ring)->size_bits)
326b6cf8b3fSJohn Ogness
327d397820fSJohn Ogness /* Determine if a logical position refers to a data-less block. */
328d397820fSJohn Ogness #define LPOS_DATALESS(lpos) ((lpos) & 1UL)
329e3bc0401SJohn Ogness #define BLK_DATALESS(blk) (LPOS_DATALESS((blk)->begin) && \
330e3bc0401SJohn Ogness LPOS_DATALESS((blk)->next))
331d397820fSJohn Ogness
332b6cf8b3fSJohn Ogness /* Get the logical position at index 0 of the current wrap. */
333b6cf8b3fSJohn Ogness #define DATA_THIS_WRAP_START_LPOS(data_ring, lpos) \
334b6cf8b3fSJohn Ogness ((lpos) & ~DATA_SIZE_MASK(data_ring))
335b6cf8b3fSJohn Ogness
336b6cf8b3fSJohn Ogness /* Get the ID for the same index of the previous wrap as the given ID. */
337b6cf8b3fSJohn Ogness #define DESC_ID_PREV_WRAP(desc_ring, id) \
338b6cf8b3fSJohn Ogness DESC_ID((id) - DESCS_COUNT(desc_ring))
339b6cf8b3fSJohn Ogness
340b6cf8b3fSJohn Ogness /*
341b6cf8b3fSJohn Ogness * A data block: mapped directly to the beginning of the data block area
342b6cf8b3fSJohn Ogness * specified as a logical position within the data ring.
343b6cf8b3fSJohn Ogness *
344b6cf8b3fSJohn Ogness * @id: the ID of the associated descriptor
345b6cf8b3fSJohn Ogness * @data: the writer data
346b6cf8b3fSJohn Ogness *
347b6cf8b3fSJohn Ogness * Note that the size of a data block is only known by its associated
348b6cf8b3fSJohn Ogness * descriptor.
349b6cf8b3fSJohn Ogness */
350b6cf8b3fSJohn Ogness struct prb_data_block {
351b6cf8b3fSJohn Ogness unsigned long id;
352a38283daSGustavo A. R. Silva char data[];
353b6cf8b3fSJohn Ogness };
354b6cf8b3fSJohn Ogness
355b6cf8b3fSJohn Ogness /*
356b6cf8b3fSJohn Ogness * Return the descriptor associated with @n. @n can be either a
357b6cf8b3fSJohn Ogness * descriptor ID or a sequence number.
358b6cf8b3fSJohn Ogness */
to_desc(struct prb_desc_ring * desc_ring,u64 n)359b6cf8b3fSJohn Ogness static struct prb_desc *to_desc(struct prb_desc_ring *desc_ring, u64 n)
360b6cf8b3fSJohn Ogness {
361b6cf8b3fSJohn Ogness return &desc_ring->descs[DESC_INDEX(desc_ring, n)];
362b6cf8b3fSJohn Ogness }
363b6cf8b3fSJohn Ogness
364cfe2790bSJohn Ogness /*
365cfe2790bSJohn Ogness * Return the printk_info associated with @n. @n can be either a
366cfe2790bSJohn Ogness * descriptor ID or a sequence number.
367cfe2790bSJohn Ogness */
to_info(struct prb_desc_ring * desc_ring,u64 n)368cfe2790bSJohn Ogness static struct printk_info *to_info(struct prb_desc_ring *desc_ring, u64 n)
369cfe2790bSJohn Ogness {
370cfe2790bSJohn Ogness return &desc_ring->infos[DESC_INDEX(desc_ring, n)];
371cfe2790bSJohn Ogness }
372cfe2790bSJohn Ogness
to_block(struct prb_data_ring * data_ring,unsigned long begin_lpos)373b6cf8b3fSJohn Ogness static struct prb_data_block *to_block(struct prb_data_ring *data_ring,
374b6cf8b3fSJohn Ogness unsigned long begin_lpos)
375b6cf8b3fSJohn Ogness {
376b6cf8b3fSJohn Ogness return (void *)&data_ring->data[DATA_INDEX(data_ring, begin_lpos)];
377b6cf8b3fSJohn Ogness }
378b6cf8b3fSJohn Ogness
379b6cf8b3fSJohn Ogness /*
380b6cf8b3fSJohn Ogness * Increase the data size to account for data block meta data plus any
381b6cf8b3fSJohn Ogness * padding so that the adjacent data block is aligned on the ID size.
382b6cf8b3fSJohn Ogness */
to_blk_size(unsigned int size)383b6cf8b3fSJohn Ogness static unsigned int to_blk_size(unsigned int size)
384b6cf8b3fSJohn Ogness {
385b6cf8b3fSJohn Ogness struct prb_data_block *db = NULL;
386b6cf8b3fSJohn Ogness
387b6cf8b3fSJohn Ogness size += sizeof(*db);
388b6cf8b3fSJohn Ogness size = ALIGN(size, sizeof(db->id));
389b6cf8b3fSJohn Ogness return size;
390b6cf8b3fSJohn Ogness }
391b6cf8b3fSJohn Ogness
392b6cf8b3fSJohn Ogness /*
393b6cf8b3fSJohn Ogness * Sanity checker for reserve size. The ringbuffer code assumes that a data
394b6cf8b3fSJohn Ogness * block does not exceed the maximum possible size that could fit within the
395b6cf8b3fSJohn Ogness * ringbuffer. This function provides that basic size check so that the
396b6cf8b3fSJohn Ogness * assumption is safe.
397b6cf8b3fSJohn Ogness */
data_check_size(struct prb_data_ring * data_ring,unsigned int size)398b6cf8b3fSJohn Ogness static bool data_check_size(struct prb_data_ring *data_ring, unsigned int size)
399b6cf8b3fSJohn Ogness {
400b6cf8b3fSJohn Ogness struct prb_data_block *db = NULL;
401b6cf8b3fSJohn Ogness
402b6cf8b3fSJohn Ogness if (size == 0)
403d397820fSJohn Ogness return true;
404b6cf8b3fSJohn Ogness
405b6cf8b3fSJohn Ogness /*
406b6cf8b3fSJohn Ogness * Ensure the alignment padded size could possibly fit in the data
407b6cf8b3fSJohn Ogness * array. The largest possible data block must still leave room for
408b6cf8b3fSJohn Ogness * at least the ID of the next block.
409b6cf8b3fSJohn Ogness */
410b6cf8b3fSJohn Ogness size = to_blk_size(size);
411b6cf8b3fSJohn Ogness if (size > DATA_SIZE(data_ring) - sizeof(db->id))
412b6cf8b3fSJohn Ogness return false;
413b6cf8b3fSJohn Ogness
414b6cf8b3fSJohn Ogness return true;
415b6cf8b3fSJohn Ogness }
416b6cf8b3fSJohn Ogness
417b6cf8b3fSJohn Ogness /* Query the state of a descriptor. */
get_desc_state(unsigned long id,unsigned long state_val)418b6cf8b3fSJohn Ogness static enum desc_state get_desc_state(unsigned long id,
419b6cf8b3fSJohn Ogness unsigned long state_val)
420b6cf8b3fSJohn Ogness {
421b6cf8b3fSJohn Ogness if (id != DESC_ID(state_val))
422b6cf8b3fSJohn Ogness return desc_miss;
423b6cf8b3fSJohn Ogness
42410dcb06dSJohn Ogness return DESC_STATE(state_val);
425b6cf8b3fSJohn Ogness }
426b6cf8b3fSJohn Ogness
427b6cf8b3fSJohn Ogness /*
428ce003d67SJohn Ogness * Get a copy of a specified descriptor and return its queried state. If the
429ce003d67SJohn Ogness * descriptor is in an inconsistent state (miss or reserved), the caller can
430ce003d67SJohn Ogness * only expect the descriptor's @state_var field to be valid.
431cfe2790bSJohn Ogness *
432cfe2790bSJohn Ogness * The sequence number and caller_id can be optionally retrieved. Like all
433cfe2790bSJohn Ogness * non-state_var data, they are only valid if the descriptor is in a
434cfe2790bSJohn Ogness * consistent state.
435b6cf8b3fSJohn Ogness */
desc_read(struct prb_desc_ring * desc_ring,unsigned long id,struct prb_desc * desc_out,u64 * seq_out,u32 * caller_id_out)436b6cf8b3fSJohn Ogness static enum desc_state desc_read(struct prb_desc_ring *desc_ring,
437cfe2790bSJohn Ogness unsigned long id, struct prb_desc *desc_out,
438cfe2790bSJohn Ogness u64 *seq_out, u32 *caller_id_out)
439b6cf8b3fSJohn Ogness {
440cfe2790bSJohn Ogness struct printk_info *info = to_info(desc_ring, id);
441b6cf8b3fSJohn Ogness struct prb_desc *desc = to_desc(desc_ring, id);
442b6cf8b3fSJohn Ogness atomic_long_t *state_var = &desc->state_var;
443b6cf8b3fSJohn Ogness enum desc_state d_state;
444b6cf8b3fSJohn Ogness unsigned long state_val;
445b6cf8b3fSJohn Ogness
446b6cf8b3fSJohn Ogness /* Check the descriptor state. */
447b6cf8b3fSJohn Ogness state_val = atomic_long_read(state_var); /* LMM(desc_read:A) */
448b6cf8b3fSJohn Ogness d_state = get_desc_state(id, state_val);
449ce003d67SJohn Ogness if (d_state == desc_miss || d_state == desc_reserved) {
450ce003d67SJohn Ogness /*
451ce003d67SJohn Ogness * The descriptor is in an inconsistent state. Set at least
452ce003d67SJohn Ogness * @state_var so that the caller can see the details of
453ce003d67SJohn Ogness * the inconsistent state.
454ce003d67SJohn Ogness */
455ce003d67SJohn Ogness goto out;
456ce003d67SJohn Ogness }
457b6cf8b3fSJohn Ogness
458b6cf8b3fSJohn Ogness /*
459b6cf8b3fSJohn Ogness * Guarantee the state is loaded before copying the descriptor
460b6cf8b3fSJohn Ogness * content. This avoids copying obsolete descriptor content that might
4614cfc7258SJohn Ogness * not apply to the descriptor state. This pairs with _prb_commit:B.
462b6cf8b3fSJohn Ogness *
463b6cf8b3fSJohn Ogness * Memory barrier involvement:
464b6cf8b3fSJohn Ogness *
4654cfc7258SJohn Ogness * If desc_read:A reads from _prb_commit:B, then desc_read:C reads
4664cfc7258SJohn Ogness * from _prb_commit:A.
467b6cf8b3fSJohn Ogness *
468b6cf8b3fSJohn Ogness * Relies on:
469b6cf8b3fSJohn Ogness *
4704cfc7258SJohn Ogness * WMB from _prb_commit:A to _prb_commit:B
471b6cf8b3fSJohn Ogness * matching
472b6cf8b3fSJohn Ogness * RMB from desc_read:A to desc_read:C
473b6cf8b3fSJohn Ogness */
474b6cf8b3fSJohn Ogness smp_rmb(); /* LMM(desc_read:B) */
475b6cf8b3fSJohn Ogness
476b6cf8b3fSJohn Ogness /*
477b6cf8b3fSJohn Ogness * Copy the descriptor data. The data is not valid until the
478e7c1fe21SJohn Ogness * state has been re-checked. A memcpy() for all of @desc
479e7c1fe21SJohn Ogness * cannot be used because of the atomic_t @state_var field.
480b6cf8b3fSJohn Ogness */
481f244b4dcSPetr Mladek if (desc_out) {
482e7c1fe21SJohn Ogness memcpy(&desc_out->text_blk_lpos, &desc->text_blk_lpos,
483cfe2790bSJohn Ogness sizeof(desc_out->text_blk_lpos)); /* LMM(desc_read:C) */
484f244b4dcSPetr Mladek }
485cfe2790bSJohn Ogness if (seq_out)
486cfe2790bSJohn Ogness *seq_out = info->seq; /* also part of desc_read:C */
487cfe2790bSJohn Ogness if (caller_id_out)
488cfe2790bSJohn Ogness *caller_id_out = info->caller_id; /* also part of desc_read:C */
489b6cf8b3fSJohn Ogness
490b6cf8b3fSJohn Ogness /*
491b6cf8b3fSJohn Ogness * 1. Guarantee the descriptor content is loaded before re-checking
492b6cf8b3fSJohn Ogness * the state. This avoids reading an obsolete descriptor state
493b6cf8b3fSJohn Ogness * that may not apply to the copied content. This pairs with
494b6cf8b3fSJohn Ogness * desc_reserve:F.
495b6cf8b3fSJohn Ogness *
496b6cf8b3fSJohn Ogness * Memory barrier involvement:
497b6cf8b3fSJohn Ogness *
498b6cf8b3fSJohn Ogness * If desc_read:C reads from desc_reserve:G, then desc_read:E
499b6cf8b3fSJohn Ogness * reads from desc_reserve:F.
500b6cf8b3fSJohn Ogness *
501b6cf8b3fSJohn Ogness * Relies on:
502b6cf8b3fSJohn Ogness *
503b6cf8b3fSJohn Ogness * WMB from desc_reserve:F to desc_reserve:G
504b6cf8b3fSJohn Ogness * matching
505b6cf8b3fSJohn Ogness * RMB from desc_read:C to desc_read:E
506b6cf8b3fSJohn Ogness *
507b6cf8b3fSJohn Ogness * 2. Guarantee the record data is loaded before re-checking the
508b6cf8b3fSJohn Ogness * state. This avoids reading an obsolete descriptor state that may
5094cfc7258SJohn Ogness * not apply to the copied data. This pairs with data_alloc:A and
5104cfc7258SJohn Ogness * data_realloc:A.
511b6cf8b3fSJohn Ogness *
512b6cf8b3fSJohn Ogness * Memory barrier involvement:
513b6cf8b3fSJohn Ogness *
514b6cf8b3fSJohn Ogness * If copy_data:A reads from data_alloc:B, then desc_read:E
515b6cf8b3fSJohn Ogness * reads from desc_make_reusable:A.
516b6cf8b3fSJohn Ogness *
517b6cf8b3fSJohn Ogness * Relies on:
518b6cf8b3fSJohn Ogness *
519b6cf8b3fSJohn Ogness * MB from desc_make_reusable:A to data_alloc:B
520b6cf8b3fSJohn Ogness * matching
521b6cf8b3fSJohn Ogness * RMB from desc_read:C to desc_read:E
522b6cf8b3fSJohn Ogness *
523b6cf8b3fSJohn Ogness * Note: desc_make_reusable:A and data_alloc:B can be different
524b6cf8b3fSJohn Ogness * CPUs. However, the data_alloc:B CPU (which performs the
525b6cf8b3fSJohn Ogness * full memory barrier) must have previously seen
526b6cf8b3fSJohn Ogness * desc_make_reusable:A.
527b6cf8b3fSJohn Ogness */
528b6cf8b3fSJohn Ogness smp_rmb(); /* LMM(desc_read:D) */
529b6cf8b3fSJohn Ogness
530ce003d67SJohn Ogness /*
531ce003d67SJohn Ogness * The data has been copied. Return the current descriptor state,
532ce003d67SJohn Ogness * which may have changed since the load above.
533ce003d67SJohn Ogness */
534b6cf8b3fSJohn Ogness state_val = atomic_long_read(state_var); /* LMM(desc_read:E) */
535ce003d67SJohn Ogness d_state = get_desc_state(id, state_val);
536ce003d67SJohn Ogness out:
537f244b4dcSPetr Mladek if (desc_out)
538ce003d67SJohn Ogness atomic_long_set(&desc_out->state_var, state_val);
539ce003d67SJohn Ogness return d_state;
540b6cf8b3fSJohn Ogness }
541b6cf8b3fSJohn Ogness
542b6cf8b3fSJohn Ogness /*
5434cfc7258SJohn Ogness * Take a specified descriptor out of the finalized state by attempting
5444cfc7258SJohn Ogness * the transition from finalized to reusable. Either this context or some
545b6cf8b3fSJohn Ogness * other context will have been successful.
546b6cf8b3fSJohn Ogness */
desc_make_reusable(struct prb_desc_ring * desc_ring,unsigned long id)547b6cf8b3fSJohn Ogness static void desc_make_reusable(struct prb_desc_ring *desc_ring,
548b6cf8b3fSJohn Ogness unsigned long id)
549b6cf8b3fSJohn Ogness {
5504cfc7258SJohn Ogness unsigned long val_finalized = DESC_SV(id, desc_finalized);
55110dcb06dSJohn Ogness unsigned long val_reusable = DESC_SV(id, desc_reusable);
552b6cf8b3fSJohn Ogness struct prb_desc *desc = to_desc(desc_ring, id);
553b6cf8b3fSJohn Ogness atomic_long_t *state_var = &desc->state_var;
554b6cf8b3fSJohn Ogness
5554cfc7258SJohn Ogness atomic_long_cmpxchg_relaxed(state_var, val_finalized,
556b6cf8b3fSJohn Ogness val_reusable); /* LMM(desc_make_reusable:A) */
557b6cf8b3fSJohn Ogness }
558b6cf8b3fSJohn Ogness
559b6cf8b3fSJohn Ogness /*
560f35efc78SJohn Ogness * Given the text data ring, put the associated descriptor of each
561b6cf8b3fSJohn Ogness * data block from @lpos_begin until @lpos_end into the reusable state.
562b6cf8b3fSJohn Ogness *
563b6cf8b3fSJohn Ogness * If there is any problem making the associated descriptor reusable, either
5644cfc7258SJohn Ogness * the descriptor has not yet been finalized or another writer context has
565b6cf8b3fSJohn Ogness * already pushed the tail lpos past the problematic data block. Regardless,
566b6cf8b3fSJohn Ogness * on error the caller can re-load the tail lpos to determine the situation.
567b6cf8b3fSJohn Ogness */
data_make_reusable(struct printk_ringbuffer * rb,unsigned long lpos_begin,unsigned long lpos_end,unsigned long * lpos_out)568b6cf8b3fSJohn Ogness static bool data_make_reusable(struct printk_ringbuffer *rb,
569b6cf8b3fSJohn Ogness unsigned long lpos_begin,
570b6cf8b3fSJohn Ogness unsigned long lpos_end,
571b6cf8b3fSJohn Ogness unsigned long *lpos_out)
572b6cf8b3fSJohn Ogness {
573584da076SNikolay Borisov
574584da076SNikolay Borisov struct prb_data_ring *data_ring = &rb->text_data_ring;
575b6cf8b3fSJohn Ogness struct prb_desc_ring *desc_ring = &rb->desc_ring;
576b6cf8b3fSJohn Ogness struct prb_data_block *blk;
577b6cf8b3fSJohn Ogness enum desc_state d_state;
578b6cf8b3fSJohn Ogness struct prb_desc desc;
579f35efc78SJohn Ogness struct prb_data_blk_lpos *blk_lpos = &desc.text_blk_lpos;
580b6cf8b3fSJohn Ogness unsigned long id;
581b6cf8b3fSJohn Ogness
582b6cf8b3fSJohn Ogness /* Loop until @lpos_begin has advanced to or beyond @lpos_end. */
583b6cf8b3fSJohn Ogness while ((lpos_end - lpos_begin) - 1 < DATA_SIZE(data_ring)) {
584b6cf8b3fSJohn Ogness blk = to_block(data_ring, lpos_begin);
585b6cf8b3fSJohn Ogness
586b6cf8b3fSJohn Ogness /*
587b6cf8b3fSJohn Ogness * Load the block ID from the data block. This is a data race
588b6cf8b3fSJohn Ogness * against a writer that may have newly reserved this data
589b6cf8b3fSJohn Ogness * area. If the loaded value matches a valid descriptor ID,
590b6cf8b3fSJohn Ogness * the blk_lpos of that descriptor will be checked to make
591b6cf8b3fSJohn Ogness * sure it points back to this data block. If the check fails,
592b6cf8b3fSJohn Ogness * the data area has been recycled by another writer.
593b6cf8b3fSJohn Ogness */
594b6cf8b3fSJohn Ogness id = blk->id; /* LMM(data_make_reusable:A) */
595b6cf8b3fSJohn Ogness
596cfe2790bSJohn Ogness d_state = desc_read(desc_ring, id, &desc,
597cfe2790bSJohn Ogness NULL, NULL); /* LMM(data_make_reusable:B) */
598b6cf8b3fSJohn Ogness
599b6cf8b3fSJohn Ogness switch (d_state) {
600b6cf8b3fSJohn Ogness case desc_miss:
601b6cf8b3fSJohn Ogness case desc_reserved:
602b6cf8b3fSJohn Ogness case desc_committed:
6034cfc7258SJohn Ogness return false;
6044cfc7258SJohn Ogness case desc_finalized:
605b6cf8b3fSJohn Ogness /*
606b6cf8b3fSJohn Ogness * This data block is invalid if the descriptor
607b6cf8b3fSJohn Ogness * does not point back to it.
608b6cf8b3fSJohn Ogness */
609b6cf8b3fSJohn Ogness if (blk_lpos->begin != lpos_begin)
610b6cf8b3fSJohn Ogness return false;
611b6cf8b3fSJohn Ogness desc_make_reusable(desc_ring, id);
612b6cf8b3fSJohn Ogness break;
613b6cf8b3fSJohn Ogness case desc_reusable:
614b6cf8b3fSJohn Ogness /*
615b6cf8b3fSJohn Ogness * This data block is invalid if the descriptor
616b6cf8b3fSJohn Ogness * does not point back to it.
617b6cf8b3fSJohn Ogness */
618b6cf8b3fSJohn Ogness if (blk_lpos->begin != lpos_begin)
619b6cf8b3fSJohn Ogness return false;
620b6cf8b3fSJohn Ogness break;
621b6cf8b3fSJohn Ogness }
622b6cf8b3fSJohn Ogness
623b6cf8b3fSJohn Ogness /* Advance @lpos_begin to the next data block. */
624b6cf8b3fSJohn Ogness lpos_begin = blk_lpos->next;
625b6cf8b3fSJohn Ogness }
626b6cf8b3fSJohn Ogness
627b6cf8b3fSJohn Ogness *lpos_out = lpos_begin;
628b6cf8b3fSJohn Ogness return true;
629b6cf8b3fSJohn Ogness }
630b6cf8b3fSJohn Ogness
631b6cf8b3fSJohn Ogness /*
632b6cf8b3fSJohn Ogness * Advance the data ring tail to at least @lpos. This function puts
633b6cf8b3fSJohn Ogness * descriptors into the reusable state if the tail is pushed beyond
634b6cf8b3fSJohn Ogness * their associated data block.
635b6cf8b3fSJohn Ogness */
data_push_tail(struct printk_ringbuffer * rb,unsigned long lpos)636584da076SNikolay Borisov static bool data_push_tail(struct printk_ringbuffer *rb, unsigned long lpos)
637b6cf8b3fSJohn Ogness {
638584da076SNikolay Borisov struct prb_data_ring *data_ring = &rb->text_data_ring;
639b6cf8b3fSJohn Ogness unsigned long tail_lpos_new;
640b6cf8b3fSJohn Ogness unsigned long tail_lpos;
641b6cf8b3fSJohn Ogness unsigned long next_lpos;
642b6cf8b3fSJohn Ogness
643d397820fSJohn Ogness /* If @lpos is from a data-less block, there is nothing to do. */
644d397820fSJohn Ogness if (LPOS_DATALESS(lpos))
645b6cf8b3fSJohn Ogness return true;
646b6cf8b3fSJohn Ogness
647b6cf8b3fSJohn Ogness /*
648b6cf8b3fSJohn Ogness * Any descriptor states that have transitioned to reusable due to the
649b6cf8b3fSJohn Ogness * data tail being pushed to this loaded value will be visible to this
650b6cf8b3fSJohn Ogness * CPU. This pairs with data_push_tail:D.
651b6cf8b3fSJohn Ogness *
652b6cf8b3fSJohn Ogness * Memory barrier involvement:
653b6cf8b3fSJohn Ogness *
654b6cf8b3fSJohn Ogness * If data_push_tail:A reads from data_push_tail:D, then this CPU can
655b6cf8b3fSJohn Ogness * see desc_make_reusable:A.
656b6cf8b3fSJohn Ogness *
657b6cf8b3fSJohn Ogness * Relies on:
658b6cf8b3fSJohn Ogness *
659b6cf8b3fSJohn Ogness * MB from desc_make_reusable:A to data_push_tail:D
660b6cf8b3fSJohn Ogness * matches
661b6cf8b3fSJohn Ogness * READFROM from data_push_tail:D to data_push_tail:A
662b6cf8b3fSJohn Ogness * thus
663b6cf8b3fSJohn Ogness * READFROM from desc_make_reusable:A to this CPU
664b6cf8b3fSJohn Ogness */
665b6cf8b3fSJohn Ogness tail_lpos = atomic_long_read(&data_ring->tail_lpos); /* LMM(data_push_tail:A) */
666b6cf8b3fSJohn Ogness
667b6cf8b3fSJohn Ogness /*
668b6cf8b3fSJohn Ogness * Loop until the tail lpos is at or beyond @lpos. This condition
669b6cf8b3fSJohn Ogness * may already be satisfied, resulting in no full memory barrier
670b6cf8b3fSJohn Ogness * from data_push_tail:D being performed. However, since this CPU
671b6cf8b3fSJohn Ogness * sees the new tail lpos, any descriptor states that transitioned to
672b6cf8b3fSJohn Ogness * the reusable state must already be visible.
673b6cf8b3fSJohn Ogness */
674b6cf8b3fSJohn Ogness while ((lpos - tail_lpos) - 1 < DATA_SIZE(data_ring)) {
675b6cf8b3fSJohn Ogness /*
676b6cf8b3fSJohn Ogness * Make all descriptors reusable that are associated with
677b6cf8b3fSJohn Ogness * data blocks before @lpos.
678b6cf8b3fSJohn Ogness */
679584da076SNikolay Borisov if (!data_make_reusable(rb, tail_lpos, lpos, &next_lpos)) {
680b6cf8b3fSJohn Ogness /*
681b6cf8b3fSJohn Ogness * 1. Guarantee the block ID loaded in
682b6cf8b3fSJohn Ogness * data_make_reusable() is performed before
683b6cf8b3fSJohn Ogness * reloading the tail lpos. The failed
684b6cf8b3fSJohn Ogness * data_make_reusable() may be due to a newly
685b6cf8b3fSJohn Ogness * recycled data area causing the tail lpos to
686b6cf8b3fSJohn Ogness * have been previously pushed. This pairs with
6874cfc7258SJohn Ogness * data_alloc:A and data_realloc:A.
688b6cf8b3fSJohn Ogness *
689b6cf8b3fSJohn Ogness * Memory barrier involvement:
690b6cf8b3fSJohn Ogness *
691b6cf8b3fSJohn Ogness * If data_make_reusable:A reads from data_alloc:B,
692b6cf8b3fSJohn Ogness * then data_push_tail:C reads from
693b6cf8b3fSJohn Ogness * data_push_tail:D.
694b6cf8b3fSJohn Ogness *
695b6cf8b3fSJohn Ogness * Relies on:
696b6cf8b3fSJohn Ogness *
697b6cf8b3fSJohn Ogness * MB from data_push_tail:D to data_alloc:B
698b6cf8b3fSJohn Ogness * matching
699b6cf8b3fSJohn Ogness * RMB from data_make_reusable:A to
700b6cf8b3fSJohn Ogness * data_push_tail:C
701b6cf8b3fSJohn Ogness *
702b6cf8b3fSJohn Ogness * Note: data_push_tail:D and data_alloc:B can be
703b6cf8b3fSJohn Ogness * different CPUs. However, the data_alloc:B
704b6cf8b3fSJohn Ogness * CPU (which performs the full memory
705b6cf8b3fSJohn Ogness * barrier) must have previously seen
706b6cf8b3fSJohn Ogness * data_push_tail:D.
707b6cf8b3fSJohn Ogness *
708b6cf8b3fSJohn Ogness * 2. Guarantee the descriptor state loaded in
709b6cf8b3fSJohn Ogness * data_make_reusable() is performed before
710b6cf8b3fSJohn Ogness * reloading the tail lpos. The failed
711b6cf8b3fSJohn Ogness * data_make_reusable() may be due to a newly
712b6cf8b3fSJohn Ogness * recycled descriptor causing the tail lpos to
713b6cf8b3fSJohn Ogness * have been previously pushed. This pairs with
714b6cf8b3fSJohn Ogness * desc_reserve:D.
715b6cf8b3fSJohn Ogness *
716b6cf8b3fSJohn Ogness * Memory barrier involvement:
717b6cf8b3fSJohn Ogness *
718b6cf8b3fSJohn Ogness * If data_make_reusable:B reads from
719b6cf8b3fSJohn Ogness * desc_reserve:F, then data_push_tail:C reads
720b6cf8b3fSJohn Ogness * from data_push_tail:D.
721b6cf8b3fSJohn Ogness *
722b6cf8b3fSJohn Ogness * Relies on:
723b6cf8b3fSJohn Ogness *
724b6cf8b3fSJohn Ogness * MB from data_push_tail:D to desc_reserve:F
725b6cf8b3fSJohn Ogness * matching
726b6cf8b3fSJohn Ogness * RMB from data_make_reusable:B to
727b6cf8b3fSJohn Ogness * data_push_tail:C
728b6cf8b3fSJohn Ogness *
729b6cf8b3fSJohn Ogness * Note: data_push_tail:D and desc_reserve:F can
730b6cf8b3fSJohn Ogness * be different CPUs. However, the
731b6cf8b3fSJohn Ogness * desc_reserve:F CPU (which performs the
732b6cf8b3fSJohn Ogness * full memory barrier) must have previously
733b6cf8b3fSJohn Ogness * seen data_push_tail:D.
734b6cf8b3fSJohn Ogness */
735b6cf8b3fSJohn Ogness smp_rmb(); /* LMM(data_push_tail:B) */
736b6cf8b3fSJohn Ogness
737b6cf8b3fSJohn Ogness tail_lpos_new = atomic_long_read(&data_ring->tail_lpos
738b6cf8b3fSJohn Ogness ); /* LMM(data_push_tail:C) */
739b6cf8b3fSJohn Ogness if (tail_lpos_new == tail_lpos)
740b6cf8b3fSJohn Ogness return false;
741b6cf8b3fSJohn Ogness
742b6cf8b3fSJohn Ogness /* Another CPU pushed the tail. Try again. */
743b6cf8b3fSJohn Ogness tail_lpos = tail_lpos_new;
744b6cf8b3fSJohn Ogness continue;
745b6cf8b3fSJohn Ogness }
746b6cf8b3fSJohn Ogness
747b6cf8b3fSJohn Ogness /*
748b6cf8b3fSJohn Ogness * Guarantee any descriptor states that have transitioned to
749b6cf8b3fSJohn Ogness * reusable are stored before pushing the tail lpos. A full
750b6cf8b3fSJohn Ogness * memory barrier is needed since other CPUs may have made
751b6cf8b3fSJohn Ogness * the descriptor states reusable. This pairs with
752b6cf8b3fSJohn Ogness * data_push_tail:A.
753b6cf8b3fSJohn Ogness */
754b6cf8b3fSJohn Ogness if (atomic_long_try_cmpxchg(&data_ring->tail_lpos, &tail_lpos,
755b6cf8b3fSJohn Ogness next_lpos)) { /* LMM(data_push_tail:D) */
756b6cf8b3fSJohn Ogness break;
757b6cf8b3fSJohn Ogness }
758b6cf8b3fSJohn Ogness }
759b6cf8b3fSJohn Ogness
760b6cf8b3fSJohn Ogness return true;
761b6cf8b3fSJohn Ogness }
762b6cf8b3fSJohn Ogness
763b6cf8b3fSJohn Ogness /*
764b6cf8b3fSJohn Ogness * Advance the desc ring tail. This function advances the tail by one
765b6cf8b3fSJohn Ogness * descriptor, thus invalidating the oldest descriptor. Before advancing
766b6cf8b3fSJohn Ogness * the tail, the tail descriptor is made reusable and all data blocks up to
767b6cf8b3fSJohn Ogness * and including the descriptor's data block are invalidated (i.e. the data
768b6cf8b3fSJohn Ogness * ring tail is pushed past the data block of the descriptor being made
769b6cf8b3fSJohn Ogness * reusable).
770b6cf8b3fSJohn Ogness */
desc_push_tail(struct printk_ringbuffer * rb,unsigned long tail_id)771b6cf8b3fSJohn Ogness static bool desc_push_tail(struct printk_ringbuffer *rb,
772b6cf8b3fSJohn Ogness unsigned long tail_id)
773b6cf8b3fSJohn Ogness {
774b6cf8b3fSJohn Ogness struct prb_desc_ring *desc_ring = &rb->desc_ring;
775b6cf8b3fSJohn Ogness enum desc_state d_state;
776b6cf8b3fSJohn Ogness struct prb_desc desc;
777b6cf8b3fSJohn Ogness
778cfe2790bSJohn Ogness d_state = desc_read(desc_ring, tail_id, &desc, NULL, NULL);
779b6cf8b3fSJohn Ogness
780b6cf8b3fSJohn Ogness switch (d_state) {
781b6cf8b3fSJohn Ogness case desc_miss:
782b6cf8b3fSJohn Ogness /*
783b6cf8b3fSJohn Ogness * If the ID is exactly 1 wrap behind the expected, it is
784b6cf8b3fSJohn Ogness * in the process of being reserved by another writer and
785b6cf8b3fSJohn Ogness * must be considered reserved.
786b6cf8b3fSJohn Ogness */
787b6cf8b3fSJohn Ogness if (DESC_ID(atomic_long_read(&desc.state_var)) ==
788b6cf8b3fSJohn Ogness DESC_ID_PREV_WRAP(desc_ring, tail_id)) {
789b6cf8b3fSJohn Ogness return false;
790b6cf8b3fSJohn Ogness }
791b6cf8b3fSJohn Ogness
792b6cf8b3fSJohn Ogness /*
793b6cf8b3fSJohn Ogness * The ID has changed. Another writer must have pushed the
794b6cf8b3fSJohn Ogness * tail and recycled the descriptor already. Success is
795b6cf8b3fSJohn Ogness * returned because the caller is only interested in the
796b6cf8b3fSJohn Ogness * specified tail being pushed, which it was.
797b6cf8b3fSJohn Ogness */
798b6cf8b3fSJohn Ogness return true;
799b6cf8b3fSJohn Ogness case desc_reserved:
800b6cf8b3fSJohn Ogness case desc_committed:
8014cfc7258SJohn Ogness return false;
8024cfc7258SJohn Ogness case desc_finalized:
803b6cf8b3fSJohn Ogness desc_make_reusable(desc_ring, tail_id);
804b6cf8b3fSJohn Ogness break;
805b6cf8b3fSJohn Ogness case desc_reusable:
806b6cf8b3fSJohn Ogness break;
807b6cf8b3fSJohn Ogness }
808b6cf8b3fSJohn Ogness
809b6cf8b3fSJohn Ogness /*
810b6cf8b3fSJohn Ogness * Data blocks must be invalidated before their associated
811b6cf8b3fSJohn Ogness * descriptor can be made available for recycling. Invalidating
812b6cf8b3fSJohn Ogness * them later is not possible because there is no way to trust
813b6cf8b3fSJohn Ogness * data blocks once their associated descriptor is gone.
814b6cf8b3fSJohn Ogness */
815b6cf8b3fSJohn Ogness
816584da076SNikolay Borisov if (!data_push_tail(rb, desc.text_blk_lpos.next))
817b6cf8b3fSJohn Ogness return false;
818b6cf8b3fSJohn Ogness
819b6cf8b3fSJohn Ogness /*
820b6cf8b3fSJohn Ogness * Check the next descriptor after @tail_id before pushing the tail
8214cfc7258SJohn Ogness * to it because the tail must always be in a finalized or reusable
822b6cf8b3fSJohn Ogness * state. The implementation of prb_first_seq() relies on this.
823b6cf8b3fSJohn Ogness *
824b6cf8b3fSJohn Ogness * A successful read implies that the next descriptor is less than or
825b6cf8b3fSJohn Ogness * equal to @head_id so there is no risk of pushing the tail past the
826b6cf8b3fSJohn Ogness * head.
827b6cf8b3fSJohn Ogness */
828cfe2790bSJohn Ogness d_state = desc_read(desc_ring, DESC_ID(tail_id + 1), &desc,
829cfe2790bSJohn Ogness NULL, NULL); /* LMM(desc_push_tail:A) */
830b6cf8b3fSJohn Ogness
8314cfc7258SJohn Ogness if (d_state == desc_finalized || d_state == desc_reusable) {
832b6cf8b3fSJohn Ogness /*
833b6cf8b3fSJohn Ogness * Guarantee any descriptor states that have transitioned to
834b6cf8b3fSJohn Ogness * reusable are stored before pushing the tail ID. This allows
835b6cf8b3fSJohn Ogness * verifying the recycled descriptor state. A full memory
836b6cf8b3fSJohn Ogness * barrier is needed since other CPUs may have made the
837b6cf8b3fSJohn Ogness * descriptor states reusable. This pairs with desc_reserve:D.
838b6cf8b3fSJohn Ogness */
839b6cf8b3fSJohn Ogness atomic_long_cmpxchg(&desc_ring->tail_id, tail_id,
840b6cf8b3fSJohn Ogness DESC_ID(tail_id + 1)); /* LMM(desc_push_tail:B) */
841b6cf8b3fSJohn Ogness } else {
842b6cf8b3fSJohn Ogness /*
843b6cf8b3fSJohn Ogness * Guarantee the last state load from desc_read() is before
844b6cf8b3fSJohn Ogness * reloading @tail_id in order to see a new tail ID in the
845b6cf8b3fSJohn Ogness * case that the descriptor has been recycled. This pairs
846b6cf8b3fSJohn Ogness * with desc_reserve:D.
847b6cf8b3fSJohn Ogness *
848b6cf8b3fSJohn Ogness * Memory barrier involvement:
849b6cf8b3fSJohn Ogness *
850b6cf8b3fSJohn Ogness * If desc_push_tail:A reads from desc_reserve:F, then
851b6cf8b3fSJohn Ogness * desc_push_tail:D reads from desc_push_tail:B.
852b6cf8b3fSJohn Ogness *
853b6cf8b3fSJohn Ogness * Relies on:
854b6cf8b3fSJohn Ogness *
855b6cf8b3fSJohn Ogness * MB from desc_push_tail:B to desc_reserve:F
856b6cf8b3fSJohn Ogness * matching
857b6cf8b3fSJohn Ogness * RMB from desc_push_tail:A to desc_push_tail:D
858b6cf8b3fSJohn Ogness *
859b6cf8b3fSJohn Ogness * Note: desc_push_tail:B and desc_reserve:F can be different
860b6cf8b3fSJohn Ogness * CPUs. However, the desc_reserve:F CPU (which performs
861b6cf8b3fSJohn Ogness * the full memory barrier) must have previously seen
862b6cf8b3fSJohn Ogness * desc_push_tail:B.
863b6cf8b3fSJohn Ogness */
864b6cf8b3fSJohn Ogness smp_rmb(); /* LMM(desc_push_tail:C) */
865b6cf8b3fSJohn Ogness
866b6cf8b3fSJohn Ogness /*
867b6cf8b3fSJohn Ogness * Re-check the tail ID. The descriptor following @tail_id is
868b6cf8b3fSJohn Ogness * not in an allowed tail state. But if the tail has since
869b6cf8b3fSJohn Ogness * been moved by another CPU, then it does not matter.
870b6cf8b3fSJohn Ogness */
871b6cf8b3fSJohn Ogness if (atomic_long_read(&desc_ring->tail_id) == tail_id) /* LMM(desc_push_tail:D) */
872b6cf8b3fSJohn Ogness return false;
873b6cf8b3fSJohn Ogness }
874b6cf8b3fSJohn Ogness
875b6cf8b3fSJohn Ogness return true;
876b6cf8b3fSJohn Ogness }
877b6cf8b3fSJohn Ogness
878b6cf8b3fSJohn Ogness /* Reserve a new descriptor, invalidating the oldest if necessary. */
desc_reserve(struct printk_ringbuffer * rb,unsigned long * id_out)879b6cf8b3fSJohn Ogness static bool desc_reserve(struct printk_ringbuffer *rb, unsigned long *id_out)
880b6cf8b3fSJohn Ogness {
881b6cf8b3fSJohn Ogness struct prb_desc_ring *desc_ring = &rb->desc_ring;
882b6cf8b3fSJohn Ogness unsigned long prev_state_val;
883b6cf8b3fSJohn Ogness unsigned long id_prev_wrap;
884b6cf8b3fSJohn Ogness struct prb_desc *desc;
885b6cf8b3fSJohn Ogness unsigned long head_id;
886b6cf8b3fSJohn Ogness unsigned long id;
887b6cf8b3fSJohn Ogness
888b6cf8b3fSJohn Ogness head_id = atomic_long_read(&desc_ring->head_id); /* LMM(desc_reserve:A) */
889b6cf8b3fSJohn Ogness
890b6cf8b3fSJohn Ogness do {
891b6cf8b3fSJohn Ogness id = DESC_ID(head_id + 1);
892b6cf8b3fSJohn Ogness id_prev_wrap = DESC_ID_PREV_WRAP(desc_ring, id);
893b6cf8b3fSJohn Ogness
894b6cf8b3fSJohn Ogness /*
895b6cf8b3fSJohn Ogness * Guarantee the head ID is read before reading the tail ID.
896b6cf8b3fSJohn Ogness * Since the tail ID is updated before the head ID, this
897b6cf8b3fSJohn Ogness * guarantees that @id_prev_wrap is never ahead of the tail
898b6cf8b3fSJohn Ogness * ID. This pairs with desc_reserve:D.
899b6cf8b3fSJohn Ogness *
900b6cf8b3fSJohn Ogness * Memory barrier involvement:
901b6cf8b3fSJohn Ogness *
902b6cf8b3fSJohn Ogness * If desc_reserve:A reads from desc_reserve:D, then
903b6cf8b3fSJohn Ogness * desc_reserve:C reads from desc_push_tail:B.
904b6cf8b3fSJohn Ogness *
905b6cf8b3fSJohn Ogness * Relies on:
906b6cf8b3fSJohn Ogness *
907b6cf8b3fSJohn Ogness * MB from desc_push_tail:B to desc_reserve:D
908b6cf8b3fSJohn Ogness * matching
909b6cf8b3fSJohn Ogness * RMB from desc_reserve:A to desc_reserve:C
910b6cf8b3fSJohn Ogness *
911b6cf8b3fSJohn Ogness * Note: desc_push_tail:B and desc_reserve:D can be different
912b6cf8b3fSJohn Ogness * CPUs. However, the desc_reserve:D CPU (which performs
913b6cf8b3fSJohn Ogness * the full memory barrier) must have previously seen
914b6cf8b3fSJohn Ogness * desc_push_tail:B.
915b6cf8b3fSJohn Ogness */
916b6cf8b3fSJohn Ogness smp_rmb(); /* LMM(desc_reserve:B) */
917b6cf8b3fSJohn Ogness
918b6cf8b3fSJohn Ogness if (id_prev_wrap == atomic_long_read(&desc_ring->tail_id
919b6cf8b3fSJohn Ogness )) { /* LMM(desc_reserve:C) */
920b6cf8b3fSJohn Ogness /*
921b6cf8b3fSJohn Ogness * Make space for the new descriptor by
922b6cf8b3fSJohn Ogness * advancing the tail.
923b6cf8b3fSJohn Ogness */
924b6cf8b3fSJohn Ogness if (!desc_push_tail(rb, id_prev_wrap))
925b6cf8b3fSJohn Ogness return false;
926b6cf8b3fSJohn Ogness }
927b6cf8b3fSJohn Ogness
928b6cf8b3fSJohn Ogness /*
929b6cf8b3fSJohn Ogness * 1. Guarantee the tail ID is read before validating the
930b6cf8b3fSJohn Ogness * recycled descriptor state. A read memory barrier is
931b6cf8b3fSJohn Ogness * sufficient for this. This pairs with desc_push_tail:B.
932b6cf8b3fSJohn Ogness *
933b6cf8b3fSJohn Ogness * Memory barrier involvement:
934b6cf8b3fSJohn Ogness *
935b6cf8b3fSJohn Ogness * If desc_reserve:C reads from desc_push_tail:B, then
936b6cf8b3fSJohn Ogness * desc_reserve:E reads from desc_make_reusable:A.
937b6cf8b3fSJohn Ogness *
938b6cf8b3fSJohn Ogness * Relies on:
939b6cf8b3fSJohn Ogness *
940b6cf8b3fSJohn Ogness * MB from desc_make_reusable:A to desc_push_tail:B
941b6cf8b3fSJohn Ogness * matching
942b6cf8b3fSJohn Ogness * RMB from desc_reserve:C to desc_reserve:E
943b6cf8b3fSJohn Ogness *
944b6cf8b3fSJohn Ogness * Note: desc_make_reusable:A and desc_push_tail:B can be
945b6cf8b3fSJohn Ogness * different CPUs. However, the desc_push_tail:B CPU
946b6cf8b3fSJohn Ogness * (which performs the full memory barrier) must have
947b6cf8b3fSJohn Ogness * previously seen desc_make_reusable:A.
948b6cf8b3fSJohn Ogness *
949b6cf8b3fSJohn Ogness * 2. Guarantee the tail ID is stored before storing the head
950b6cf8b3fSJohn Ogness * ID. This pairs with desc_reserve:B.
951b6cf8b3fSJohn Ogness *
952b6cf8b3fSJohn Ogness * 3. Guarantee any data ring tail changes are stored before
953b6cf8b3fSJohn Ogness * recycling the descriptor. Data ring tail changes can
954b6cf8b3fSJohn Ogness * happen via desc_push_tail()->data_push_tail(). A full
955b6cf8b3fSJohn Ogness * memory barrier is needed since another CPU may have
956b6cf8b3fSJohn Ogness * pushed the data ring tails. This pairs with
957b6cf8b3fSJohn Ogness * data_push_tail:B.
958b6cf8b3fSJohn Ogness *
959b6cf8b3fSJohn Ogness * 4. Guarantee a new tail ID is stored before recycling the
960b6cf8b3fSJohn Ogness * descriptor. A full memory barrier is needed since
961b6cf8b3fSJohn Ogness * another CPU may have pushed the tail ID. This pairs
962b6cf8b3fSJohn Ogness * with desc_push_tail:C and this also pairs with
963b6cf8b3fSJohn Ogness * prb_first_seq:C.
9644cfc7258SJohn Ogness *
9654cfc7258SJohn Ogness * 5. Guarantee the head ID is stored before trying to
9664cfc7258SJohn Ogness * finalize the previous descriptor. This pairs with
9674cfc7258SJohn Ogness * _prb_commit:B.
968b6cf8b3fSJohn Ogness */
969b6cf8b3fSJohn Ogness } while (!atomic_long_try_cmpxchg(&desc_ring->head_id, &head_id,
970b6cf8b3fSJohn Ogness id)); /* LMM(desc_reserve:D) */
971b6cf8b3fSJohn Ogness
972b6cf8b3fSJohn Ogness desc = to_desc(desc_ring, id);
973b6cf8b3fSJohn Ogness
974b6cf8b3fSJohn Ogness /*
975b6cf8b3fSJohn Ogness * If the descriptor has been recycled, verify the old state val.
976b6cf8b3fSJohn Ogness * See "ABA Issues" about why this verification is performed.
977b6cf8b3fSJohn Ogness */
978b6cf8b3fSJohn Ogness prev_state_val = atomic_long_read(&desc->state_var); /* LMM(desc_reserve:E) */
979b6cf8b3fSJohn Ogness if (prev_state_val &&
98010dcb06dSJohn Ogness get_desc_state(id_prev_wrap, prev_state_val) != desc_reusable) {
981b6cf8b3fSJohn Ogness WARN_ON_ONCE(1);
982b6cf8b3fSJohn Ogness return false;
983b6cf8b3fSJohn Ogness }
984b6cf8b3fSJohn Ogness
985b6cf8b3fSJohn Ogness /*
986b6cf8b3fSJohn Ogness * Assign the descriptor a new ID and set its state to reserved.
987b6cf8b3fSJohn Ogness * See "ABA Issues" about why cmpxchg() instead of set() is used.
988b6cf8b3fSJohn Ogness *
989b6cf8b3fSJohn Ogness * Guarantee the new descriptor ID and state is stored before making
990b6cf8b3fSJohn Ogness * any other changes. A write memory barrier is sufficient for this.
991b6cf8b3fSJohn Ogness * This pairs with desc_read:D.
992b6cf8b3fSJohn Ogness */
993b6cf8b3fSJohn Ogness if (!atomic_long_try_cmpxchg(&desc->state_var, &prev_state_val,
99410dcb06dSJohn Ogness DESC_SV(id, desc_reserved))) { /* LMM(desc_reserve:F) */
995b6cf8b3fSJohn Ogness WARN_ON_ONCE(1);
996b6cf8b3fSJohn Ogness return false;
997b6cf8b3fSJohn Ogness }
998b6cf8b3fSJohn Ogness
999b6cf8b3fSJohn Ogness /* Now data in @desc can be modified: LMM(desc_reserve:G) */
1000b6cf8b3fSJohn Ogness
1001b6cf8b3fSJohn Ogness *id_out = id;
1002b6cf8b3fSJohn Ogness return true;
1003b6cf8b3fSJohn Ogness }
1004b6cf8b3fSJohn Ogness
1005b6cf8b3fSJohn Ogness /* Determine the end of a data block. */
get_next_lpos(struct prb_data_ring * data_ring,unsigned long lpos,unsigned int size)1006b6cf8b3fSJohn Ogness static unsigned long get_next_lpos(struct prb_data_ring *data_ring,
1007b6cf8b3fSJohn Ogness unsigned long lpos, unsigned int size)
1008b6cf8b3fSJohn Ogness {
1009b6cf8b3fSJohn Ogness unsigned long begin_lpos;
1010b6cf8b3fSJohn Ogness unsigned long next_lpos;
1011b6cf8b3fSJohn Ogness
1012b6cf8b3fSJohn Ogness begin_lpos = lpos;
1013b6cf8b3fSJohn Ogness next_lpos = lpos + size;
1014b6cf8b3fSJohn Ogness
1015b6cf8b3fSJohn Ogness /* First check if the data block does not wrap. */
1016b6cf8b3fSJohn Ogness if (DATA_WRAPS(data_ring, begin_lpos) == DATA_WRAPS(data_ring, next_lpos))
1017b6cf8b3fSJohn Ogness return next_lpos;
1018b6cf8b3fSJohn Ogness
1019b6cf8b3fSJohn Ogness /* Wrapping data blocks store their data at the beginning. */
1020b6cf8b3fSJohn Ogness return (DATA_THIS_WRAP_START_LPOS(data_ring, next_lpos) + size);
1021b6cf8b3fSJohn Ogness }
1022b6cf8b3fSJohn Ogness
1023b6cf8b3fSJohn Ogness /*
1024b6cf8b3fSJohn Ogness * Allocate a new data block, invalidating the oldest data block(s)
1025b6cf8b3fSJohn Ogness * if necessary. This function also associates the data block with
1026b6cf8b3fSJohn Ogness * a specified descriptor.
1027b6cf8b3fSJohn Ogness */
data_alloc(struct printk_ringbuffer * rb,unsigned int size,struct prb_data_blk_lpos * blk_lpos,unsigned long id)1028584da076SNikolay Borisov static char *data_alloc(struct printk_ringbuffer *rb, unsigned int size,
1029b6cf8b3fSJohn Ogness struct prb_data_blk_lpos *blk_lpos, unsigned long id)
1030b6cf8b3fSJohn Ogness {
1031584da076SNikolay Borisov struct prb_data_ring *data_ring = &rb->text_data_ring;
1032b6cf8b3fSJohn Ogness struct prb_data_block *blk;
1033b6cf8b3fSJohn Ogness unsigned long begin_lpos;
1034b6cf8b3fSJohn Ogness unsigned long next_lpos;
1035b6cf8b3fSJohn Ogness
1036b6cf8b3fSJohn Ogness if (size == 0) {
10375113cf5fSJohn Ogness /*
10385113cf5fSJohn Ogness * Data blocks are not created for empty lines. Instead, the
10395113cf5fSJohn Ogness * reader will recognize these special lpos values and handle
10405113cf5fSJohn Ogness * it appropriately.
10415113cf5fSJohn Ogness */
10425113cf5fSJohn Ogness blk_lpos->begin = EMPTY_LINE_LPOS;
10435113cf5fSJohn Ogness blk_lpos->next = EMPTY_LINE_LPOS;
1044b6cf8b3fSJohn Ogness return NULL;
1045b6cf8b3fSJohn Ogness }
1046b6cf8b3fSJohn Ogness
1047b6cf8b3fSJohn Ogness size = to_blk_size(size);
1048b6cf8b3fSJohn Ogness
1049b6cf8b3fSJohn Ogness begin_lpos = atomic_long_read(&data_ring->head_lpos);
1050b6cf8b3fSJohn Ogness
1051b6cf8b3fSJohn Ogness do {
1052b6cf8b3fSJohn Ogness next_lpos = get_next_lpos(data_ring, begin_lpos, size);
1053b6cf8b3fSJohn Ogness
1054584da076SNikolay Borisov if (!data_push_tail(rb, next_lpos - DATA_SIZE(data_ring))) {
1055b6cf8b3fSJohn Ogness /* Failed to allocate, specify a data-less block. */
1056d397820fSJohn Ogness blk_lpos->begin = FAILED_LPOS;
1057d397820fSJohn Ogness blk_lpos->next = FAILED_LPOS;
1058b6cf8b3fSJohn Ogness return NULL;
1059b6cf8b3fSJohn Ogness }
1060b6cf8b3fSJohn Ogness
1061b6cf8b3fSJohn Ogness /*
1062b6cf8b3fSJohn Ogness * 1. Guarantee any descriptor states that have transitioned
1063b6cf8b3fSJohn Ogness * to reusable are stored before modifying the newly
1064b6cf8b3fSJohn Ogness * allocated data area. A full memory barrier is needed
1065b6cf8b3fSJohn Ogness * since other CPUs may have made the descriptor states
1066b6cf8b3fSJohn Ogness * reusable. See data_push_tail:A about why the reusable
1067b6cf8b3fSJohn Ogness * states are visible. This pairs with desc_read:D.
1068b6cf8b3fSJohn Ogness *
1069b6cf8b3fSJohn Ogness * 2. Guarantee any updated tail lpos is stored before
1070b6cf8b3fSJohn Ogness * modifying the newly allocated data area. Another CPU may
1071b6cf8b3fSJohn Ogness * be in data_make_reusable() and is reading a block ID
1072b6cf8b3fSJohn Ogness * from this area. data_make_reusable() can handle reading
1073b6cf8b3fSJohn Ogness * a garbage block ID value, but then it must be able to
1074b6cf8b3fSJohn Ogness * load a new tail lpos. A full memory barrier is needed
1075b6cf8b3fSJohn Ogness * since other CPUs may have updated the tail lpos. This
1076b6cf8b3fSJohn Ogness * pairs with data_push_tail:B.
1077b6cf8b3fSJohn Ogness */
1078b6cf8b3fSJohn Ogness } while (!atomic_long_try_cmpxchg(&data_ring->head_lpos, &begin_lpos,
1079b6cf8b3fSJohn Ogness next_lpos)); /* LMM(data_alloc:A) */
1080b6cf8b3fSJohn Ogness
1081b6cf8b3fSJohn Ogness blk = to_block(data_ring, begin_lpos);
1082b6cf8b3fSJohn Ogness blk->id = id; /* LMM(data_alloc:B) */
1083b6cf8b3fSJohn Ogness
1084b6cf8b3fSJohn Ogness if (DATA_WRAPS(data_ring, begin_lpos) != DATA_WRAPS(data_ring, next_lpos)) {
1085b6cf8b3fSJohn Ogness /* Wrapping data blocks store their data at the beginning. */
1086b6cf8b3fSJohn Ogness blk = to_block(data_ring, 0);
1087b6cf8b3fSJohn Ogness
1088b6cf8b3fSJohn Ogness /*
1089b6cf8b3fSJohn Ogness * Store the ID on the wrapped block for consistency.
1090b6cf8b3fSJohn Ogness * The printk_ringbuffer does not actually use it.
1091b6cf8b3fSJohn Ogness */
1092b6cf8b3fSJohn Ogness blk->id = id;
1093b6cf8b3fSJohn Ogness }
1094b6cf8b3fSJohn Ogness
1095b6cf8b3fSJohn Ogness blk_lpos->begin = begin_lpos;
1096b6cf8b3fSJohn Ogness blk_lpos->next = next_lpos;
1097b6cf8b3fSJohn Ogness
1098b6cf8b3fSJohn Ogness return &blk->data[0];
1099b6cf8b3fSJohn Ogness }
1100b6cf8b3fSJohn Ogness
11014cfc7258SJohn Ogness /*
11024cfc7258SJohn Ogness * Try to resize an existing data block associated with the descriptor
11034cfc7258SJohn Ogness * specified by @id. If the resized data block should become wrapped, it
11044cfc7258SJohn Ogness * copies the old data to the new data block. If @size yields a data block
11054cfc7258SJohn Ogness * with the same or less size, the data block is left as is.
11064cfc7258SJohn Ogness *
11074cfc7258SJohn Ogness * Fail if this is not the last allocated data block or if there is not
11084cfc7258SJohn Ogness * enough space or it is not possible make enough space.
11094cfc7258SJohn Ogness *
11104cfc7258SJohn Ogness * Return a pointer to the beginning of the entire data buffer or NULL on
11114cfc7258SJohn Ogness * failure.
11124cfc7258SJohn Ogness */
data_realloc(struct printk_ringbuffer * rb,unsigned int size,struct prb_data_blk_lpos * blk_lpos,unsigned long id)1113584da076SNikolay Borisov static char *data_realloc(struct printk_ringbuffer *rb, unsigned int size,
11144cfc7258SJohn Ogness struct prb_data_blk_lpos *blk_lpos, unsigned long id)
11154cfc7258SJohn Ogness {
1116584da076SNikolay Borisov struct prb_data_ring *data_ring = &rb->text_data_ring;
11174cfc7258SJohn Ogness struct prb_data_block *blk;
11184cfc7258SJohn Ogness unsigned long head_lpos;
11194cfc7258SJohn Ogness unsigned long next_lpos;
11204cfc7258SJohn Ogness bool wrapped;
11214cfc7258SJohn Ogness
11224cfc7258SJohn Ogness /* Reallocation only works if @blk_lpos is the newest data block. */
11234cfc7258SJohn Ogness head_lpos = atomic_long_read(&data_ring->head_lpos);
11244cfc7258SJohn Ogness if (head_lpos != blk_lpos->next)
11254cfc7258SJohn Ogness return NULL;
11264cfc7258SJohn Ogness
11274cfc7258SJohn Ogness /* Keep track if @blk_lpos was a wrapping data block. */
11284cfc7258SJohn Ogness wrapped = (DATA_WRAPS(data_ring, blk_lpos->begin) != DATA_WRAPS(data_ring, blk_lpos->next));
11294cfc7258SJohn Ogness
11304cfc7258SJohn Ogness size = to_blk_size(size);
11314cfc7258SJohn Ogness
11324cfc7258SJohn Ogness next_lpos = get_next_lpos(data_ring, blk_lpos->begin, size);
11334cfc7258SJohn Ogness
11344cfc7258SJohn Ogness /* If the data block does not increase, there is nothing to do. */
11354cfc7258SJohn Ogness if (head_lpos - next_lpos < DATA_SIZE(data_ring)) {
1136eac48eb6SPetr Mladek if (wrapped)
1137eac48eb6SPetr Mladek blk = to_block(data_ring, 0);
1138eac48eb6SPetr Mladek else
11394cfc7258SJohn Ogness blk = to_block(data_ring, blk_lpos->begin);
11404cfc7258SJohn Ogness return &blk->data[0];
11414cfc7258SJohn Ogness }
11424cfc7258SJohn Ogness
1143584da076SNikolay Borisov if (!data_push_tail(rb, next_lpos - DATA_SIZE(data_ring)))
11444cfc7258SJohn Ogness return NULL;
11454cfc7258SJohn Ogness
11464cfc7258SJohn Ogness /* The memory barrier involvement is the same as data_alloc:A. */
11474cfc7258SJohn Ogness if (!atomic_long_try_cmpxchg(&data_ring->head_lpos, &head_lpos,
11484cfc7258SJohn Ogness next_lpos)) { /* LMM(data_realloc:A) */
11494cfc7258SJohn Ogness return NULL;
11504cfc7258SJohn Ogness }
11514cfc7258SJohn Ogness
11524cfc7258SJohn Ogness blk = to_block(data_ring, blk_lpos->begin);
11534cfc7258SJohn Ogness
11544cfc7258SJohn Ogness if (DATA_WRAPS(data_ring, blk_lpos->begin) != DATA_WRAPS(data_ring, next_lpos)) {
11554cfc7258SJohn Ogness struct prb_data_block *old_blk = blk;
11564cfc7258SJohn Ogness
11574cfc7258SJohn Ogness /* Wrapping data blocks store their data at the beginning. */
11584cfc7258SJohn Ogness blk = to_block(data_ring, 0);
11594cfc7258SJohn Ogness
11604cfc7258SJohn Ogness /*
11614cfc7258SJohn Ogness * Store the ID on the wrapped block for consistency.
11624cfc7258SJohn Ogness * The printk_ringbuffer does not actually use it.
11634cfc7258SJohn Ogness */
11644cfc7258SJohn Ogness blk->id = id;
11654cfc7258SJohn Ogness
11664cfc7258SJohn Ogness if (!wrapped) {
11674cfc7258SJohn Ogness /*
11684cfc7258SJohn Ogness * Since the allocated space is now in the newly
11694cfc7258SJohn Ogness * created wrapping data block, copy the content
11704cfc7258SJohn Ogness * from the old data block.
11714cfc7258SJohn Ogness */
11724cfc7258SJohn Ogness memcpy(&blk->data[0], &old_blk->data[0],
11734cfc7258SJohn Ogness (blk_lpos->next - blk_lpos->begin) - sizeof(blk->id));
11744cfc7258SJohn Ogness }
11754cfc7258SJohn Ogness }
11764cfc7258SJohn Ogness
11774cfc7258SJohn Ogness blk_lpos->next = next_lpos;
11784cfc7258SJohn Ogness
11794cfc7258SJohn Ogness return &blk->data[0];
11804cfc7258SJohn Ogness }
11814cfc7258SJohn Ogness
1182b6cf8b3fSJohn Ogness /* Return the number of bytes used by a data block. */
space_used(struct prb_data_ring * data_ring,struct prb_data_blk_lpos * blk_lpos)1183b6cf8b3fSJohn Ogness static unsigned int space_used(struct prb_data_ring *data_ring,
1184b6cf8b3fSJohn Ogness struct prb_data_blk_lpos *blk_lpos)
1185b6cf8b3fSJohn Ogness {
1186d397820fSJohn Ogness /* Data-less blocks take no space. */
1187e3bc0401SJohn Ogness if (BLK_DATALESS(blk_lpos))
1188d397820fSJohn Ogness return 0;
1189d397820fSJohn Ogness
1190b6cf8b3fSJohn Ogness if (DATA_WRAPS(data_ring, blk_lpos->begin) == DATA_WRAPS(data_ring, blk_lpos->next)) {
1191b6cf8b3fSJohn Ogness /* Data block does not wrap. */
1192b6cf8b3fSJohn Ogness return (DATA_INDEX(data_ring, blk_lpos->next) -
1193b6cf8b3fSJohn Ogness DATA_INDEX(data_ring, blk_lpos->begin));
1194b6cf8b3fSJohn Ogness }
1195b6cf8b3fSJohn Ogness
1196b6cf8b3fSJohn Ogness /*
1197b6cf8b3fSJohn Ogness * For wrapping data blocks, the trailing (wasted) space is
1198b6cf8b3fSJohn Ogness * also counted.
1199b6cf8b3fSJohn Ogness */
1200b6cf8b3fSJohn Ogness return (DATA_INDEX(data_ring, blk_lpos->next) +
1201b6cf8b3fSJohn Ogness DATA_SIZE(data_ring) - DATA_INDEX(data_ring, blk_lpos->begin));
1202b6cf8b3fSJohn Ogness }
1203b6cf8b3fSJohn Ogness
12042a7f87edSJohn Ogness /*
12052a7f87edSJohn Ogness * Given @blk_lpos, return a pointer to the writer data from the data block
12062a7f87edSJohn Ogness * and calculate the size of the data part. A NULL pointer is returned if
12072a7f87edSJohn Ogness * @blk_lpos specifies values that could never be legal.
12082a7f87edSJohn Ogness *
12092a7f87edSJohn Ogness * This function (used by readers) performs strict validation on the lpos
12102a7f87edSJohn Ogness * values to possibly detect bugs in the writer code. A WARN_ON_ONCE() is
12112a7f87edSJohn Ogness * triggered if an internal error is detected.
12122a7f87edSJohn Ogness */
get_data(struct prb_data_ring * data_ring,struct prb_data_blk_lpos * blk_lpos,unsigned int * data_size)12132a7f87edSJohn Ogness static const char *get_data(struct prb_data_ring *data_ring,
12142a7f87edSJohn Ogness struct prb_data_blk_lpos *blk_lpos,
12152a7f87edSJohn Ogness unsigned int *data_size)
12162a7f87edSJohn Ogness {
12172a7f87edSJohn Ogness struct prb_data_block *db;
12182a7f87edSJohn Ogness
12192a7f87edSJohn Ogness /* Data-less data block description. */
1220e3bc0401SJohn Ogness if (BLK_DATALESS(blk_lpos)) {
12215113cf5fSJohn Ogness /*
12225113cf5fSJohn Ogness * Records that are just empty lines are also valid, even
12235113cf5fSJohn Ogness * though they do not have a data block. For such records
12245113cf5fSJohn Ogness * explicitly return empty string data to signify success.
12255113cf5fSJohn Ogness */
12265113cf5fSJohn Ogness if (blk_lpos->begin == EMPTY_LINE_LPOS &&
12275113cf5fSJohn Ogness blk_lpos->next == EMPTY_LINE_LPOS) {
12282a7f87edSJohn Ogness *data_size = 0;
12292a7f87edSJohn Ogness return "";
12302a7f87edSJohn Ogness }
12315113cf5fSJohn Ogness
12325113cf5fSJohn Ogness /* Data lost, invalid, or otherwise unavailable. */
12332a7f87edSJohn Ogness return NULL;
12342a7f87edSJohn Ogness }
12352a7f87edSJohn Ogness
12362a7f87edSJohn Ogness /* Regular data block: @begin less than @next and in same wrap. */
12372a7f87edSJohn Ogness if (DATA_WRAPS(data_ring, blk_lpos->begin) == DATA_WRAPS(data_ring, blk_lpos->next) &&
12382a7f87edSJohn Ogness blk_lpos->begin < blk_lpos->next) {
12392a7f87edSJohn Ogness db = to_block(data_ring, blk_lpos->begin);
12402a7f87edSJohn Ogness *data_size = blk_lpos->next - blk_lpos->begin;
12412a7f87edSJohn Ogness
12422a7f87edSJohn Ogness /* Wrapping data block: @begin is one wrap behind @next. */
12432a7f87edSJohn Ogness } else if (DATA_WRAPS(data_ring, blk_lpos->begin + DATA_SIZE(data_ring)) ==
12442a7f87edSJohn Ogness DATA_WRAPS(data_ring, blk_lpos->next)) {
12452a7f87edSJohn Ogness db = to_block(data_ring, 0);
12462a7f87edSJohn Ogness *data_size = DATA_INDEX(data_ring, blk_lpos->next);
12472a7f87edSJohn Ogness
12482a7f87edSJohn Ogness /* Illegal block description. */
12492a7f87edSJohn Ogness } else {
12502a7f87edSJohn Ogness WARN_ON_ONCE(1);
12512a7f87edSJohn Ogness return NULL;
12522a7f87edSJohn Ogness }
12532a7f87edSJohn Ogness
12542a7f87edSJohn Ogness /* A valid data block will always be aligned to the ID size. */
12552a7f87edSJohn Ogness if (WARN_ON_ONCE(blk_lpos->begin != ALIGN(blk_lpos->begin, sizeof(db->id))) ||
12562a7f87edSJohn Ogness WARN_ON_ONCE(blk_lpos->next != ALIGN(blk_lpos->next, sizeof(db->id)))) {
12572a7f87edSJohn Ogness return NULL;
12582a7f87edSJohn Ogness }
12592a7f87edSJohn Ogness
12602a7f87edSJohn Ogness /* A valid data block will always have at least an ID. */
12612a7f87edSJohn Ogness if (WARN_ON_ONCE(*data_size < sizeof(db->id)))
12622a7f87edSJohn Ogness return NULL;
12632a7f87edSJohn Ogness
12642a7f87edSJohn Ogness /* Subtract block ID space from size to reflect data size. */
12652a7f87edSJohn Ogness *data_size -= sizeof(db->id);
12662a7f87edSJohn Ogness
12672a7f87edSJohn Ogness return &db->data[0];
12682a7f87edSJohn Ogness }
12692a7f87edSJohn Ogness
12704cfc7258SJohn Ogness /*
12714cfc7258SJohn Ogness * Attempt to transition the newest descriptor from committed back to reserved
12724cfc7258SJohn Ogness * so that the record can be modified by a writer again. This is only possible
12734cfc7258SJohn Ogness * if the descriptor is not yet finalized and the provided @caller_id matches.
12744cfc7258SJohn Ogness */
desc_reopen_last(struct prb_desc_ring * desc_ring,u32 caller_id,unsigned long * id_out)12754cfc7258SJohn Ogness static struct prb_desc *desc_reopen_last(struct prb_desc_ring *desc_ring,
12764cfc7258SJohn Ogness u32 caller_id, unsigned long *id_out)
12774cfc7258SJohn Ogness {
12784cfc7258SJohn Ogness unsigned long prev_state_val;
12794cfc7258SJohn Ogness enum desc_state d_state;
12804cfc7258SJohn Ogness struct prb_desc desc;
12814cfc7258SJohn Ogness struct prb_desc *d;
12824cfc7258SJohn Ogness unsigned long id;
1283cfe2790bSJohn Ogness u32 cid;
12844cfc7258SJohn Ogness
12854cfc7258SJohn Ogness id = atomic_long_read(&desc_ring->head_id);
12864cfc7258SJohn Ogness
12874cfc7258SJohn Ogness /*
12884cfc7258SJohn Ogness * To reduce unnecessarily reopening, first check if the descriptor
12894cfc7258SJohn Ogness * state and caller ID are correct.
12904cfc7258SJohn Ogness */
1291cfe2790bSJohn Ogness d_state = desc_read(desc_ring, id, &desc, NULL, &cid);
1292cfe2790bSJohn Ogness if (d_state != desc_committed || cid != caller_id)
12934cfc7258SJohn Ogness return NULL;
12944cfc7258SJohn Ogness
12954cfc7258SJohn Ogness d = to_desc(desc_ring, id);
12964cfc7258SJohn Ogness
12974cfc7258SJohn Ogness prev_state_val = DESC_SV(id, desc_committed);
12984cfc7258SJohn Ogness
12994cfc7258SJohn Ogness /*
13004cfc7258SJohn Ogness * Guarantee the reserved state is stored before reading any
13014cfc7258SJohn Ogness * record data. A full memory barrier is needed because @state_var
13024cfc7258SJohn Ogness * modification is followed by reading. This pairs with _prb_commit:B.
13034cfc7258SJohn Ogness *
13044cfc7258SJohn Ogness * Memory barrier involvement:
13054cfc7258SJohn Ogness *
13064cfc7258SJohn Ogness * If desc_reopen_last:A reads from _prb_commit:B, then
13074cfc7258SJohn Ogness * prb_reserve_in_last:A reads from _prb_commit:A.
13084cfc7258SJohn Ogness *
13094cfc7258SJohn Ogness * Relies on:
13104cfc7258SJohn Ogness *
13114cfc7258SJohn Ogness * WMB from _prb_commit:A to _prb_commit:B
13124cfc7258SJohn Ogness * matching
13134cfc7258SJohn Ogness * MB If desc_reopen_last:A to prb_reserve_in_last:A
13144cfc7258SJohn Ogness */
13154cfc7258SJohn Ogness if (!atomic_long_try_cmpxchg(&d->state_var, &prev_state_val,
13164cfc7258SJohn Ogness DESC_SV(id, desc_reserved))) { /* LMM(desc_reopen_last:A) */
13174cfc7258SJohn Ogness return NULL;
13184cfc7258SJohn Ogness }
13194cfc7258SJohn Ogness
13204cfc7258SJohn Ogness *id_out = id;
13214cfc7258SJohn Ogness return d;
13224cfc7258SJohn Ogness }
13234cfc7258SJohn Ogness
13244cfc7258SJohn Ogness /**
13254cfc7258SJohn Ogness * prb_reserve_in_last() - Re-reserve and extend the space in the ringbuffer
13264cfc7258SJohn Ogness * used by the newest record.
13274cfc7258SJohn Ogness *
13284cfc7258SJohn Ogness * @e: The entry structure to setup.
13294cfc7258SJohn Ogness * @rb: The ringbuffer to re-reserve and extend data in.
13304cfc7258SJohn Ogness * @r: The record structure to allocate buffers for.
13314cfc7258SJohn Ogness * @caller_id: The caller ID of the caller (reserving writer).
133259f8bccaSJohn Ogness * @max_size: Fail if the extended size would be greater than this.
13334cfc7258SJohn Ogness *
13344cfc7258SJohn Ogness * This is the public function available to writers to re-reserve and extend
13354cfc7258SJohn Ogness * data.
13364cfc7258SJohn Ogness *
13374cfc7258SJohn Ogness * The writer specifies the text size to extend (not the new total size) by
1338f35efc78SJohn Ogness * setting the @text_buf_size field of @r. To ensure proper initialization
1339f35efc78SJohn Ogness * of @r, prb_rec_init_wr() should be used.
13404cfc7258SJohn Ogness *
13414cfc7258SJohn Ogness * This function will fail if @caller_id does not match the caller ID of the
13424cfc7258SJohn Ogness * newest record. In that case the caller must reserve new data using
13434cfc7258SJohn Ogness * prb_reserve().
13444cfc7258SJohn Ogness *
13454cfc7258SJohn Ogness * Context: Any context. Disables local interrupts on success.
13464cfc7258SJohn Ogness * Return: true if text data could be extended, otherwise false.
13474cfc7258SJohn Ogness *
13484cfc7258SJohn Ogness * On success:
13494cfc7258SJohn Ogness *
13504cfc7258SJohn Ogness * - @r->text_buf points to the beginning of the entire text buffer.
13514cfc7258SJohn Ogness *
13524cfc7258SJohn Ogness * - @r->text_buf_size is set to the new total size of the buffer.
13534cfc7258SJohn Ogness *
13544cfc7258SJohn Ogness * - @r->info is not touched so that @r->info->text_len could be used
13554cfc7258SJohn Ogness * to append the text.
13564cfc7258SJohn Ogness *
13574cfc7258SJohn Ogness * - prb_record_text_space() can be used on @e to query the new
13584cfc7258SJohn Ogness * actually used space.
13594cfc7258SJohn Ogness *
13604cfc7258SJohn Ogness * Important: All @r->info fields will already be set with the current values
13614cfc7258SJohn Ogness * for the record. I.e. @r->info->text_len will be less than
1362f35efc78SJohn Ogness * @text_buf_size. Writers can use @r->info->text_len to know
13634cfc7258SJohn Ogness * where concatenation begins and writers should update
13644cfc7258SJohn Ogness * @r->info->text_len after concatenating.
13654cfc7258SJohn Ogness */
prb_reserve_in_last(struct prb_reserved_entry * e,struct printk_ringbuffer * rb,struct printk_record * r,u32 caller_id,unsigned int max_size)13664cfc7258SJohn Ogness bool prb_reserve_in_last(struct prb_reserved_entry *e, struct printk_ringbuffer *rb,
136759f8bccaSJohn Ogness struct printk_record *r, u32 caller_id, unsigned int max_size)
13684cfc7258SJohn Ogness {
1369cfe2790bSJohn Ogness struct prb_desc_ring *desc_ring = &rb->desc_ring;
1370cfe2790bSJohn Ogness struct printk_info *info;
13714cfc7258SJohn Ogness unsigned int data_size;
13724cfc7258SJohn Ogness struct prb_desc *d;
13734cfc7258SJohn Ogness unsigned long id;
13744cfc7258SJohn Ogness
13754cfc7258SJohn Ogness local_irq_save(e->irqflags);
13764cfc7258SJohn Ogness
13774cfc7258SJohn Ogness /* Transition the newest descriptor back to the reserved state. */
1378cfe2790bSJohn Ogness d = desc_reopen_last(desc_ring, caller_id, &id);
13794cfc7258SJohn Ogness if (!d) {
13804cfc7258SJohn Ogness local_irq_restore(e->irqflags);
13814cfc7258SJohn Ogness goto fail_reopen;
13824cfc7258SJohn Ogness }
13834cfc7258SJohn Ogness
13844cfc7258SJohn Ogness /* Now the writer has exclusive access: LMM(prb_reserve_in_last:A) */
13854cfc7258SJohn Ogness
1386cfe2790bSJohn Ogness info = to_info(desc_ring, id);
1387cfe2790bSJohn Ogness
13884cfc7258SJohn Ogness /*
13894cfc7258SJohn Ogness * Set the @e fields here so that prb_commit() can be used if
13904cfc7258SJohn Ogness * anything fails from now on.
13914cfc7258SJohn Ogness */
13924cfc7258SJohn Ogness e->rb = rb;
13934cfc7258SJohn Ogness e->id = id;
13944cfc7258SJohn Ogness
13954cfc7258SJohn Ogness /*
13964cfc7258SJohn Ogness * desc_reopen_last() checked the caller_id, but there was no
13974cfc7258SJohn Ogness * exclusive access at that point. The descriptor may have
13984cfc7258SJohn Ogness * changed since then.
13994cfc7258SJohn Ogness */
1400cfe2790bSJohn Ogness if (caller_id != info->caller_id)
14014cfc7258SJohn Ogness goto fail;
14024cfc7258SJohn Ogness
14034cfc7258SJohn Ogness if (BLK_DATALESS(&d->text_blk_lpos)) {
1404cfe2790bSJohn Ogness if (WARN_ON_ONCE(info->text_len != 0)) {
14054cfc7258SJohn Ogness pr_warn_once("wrong text_len value (%hu, expecting 0)\n",
1406cfe2790bSJohn Ogness info->text_len);
1407cfe2790bSJohn Ogness info->text_len = 0;
14084cfc7258SJohn Ogness }
14094cfc7258SJohn Ogness
14104cfc7258SJohn Ogness if (!data_check_size(&rb->text_data_ring, r->text_buf_size))
14114cfc7258SJohn Ogness goto fail;
14124cfc7258SJohn Ogness
141359f8bccaSJohn Ogness if (r->text_buf_size > max_size)
141459f8bccaSJohn Ogness goto fail;
141559f8bccaSJohn Ogness
1416584da076SNikolay Borisov r->text_buf = data_alloc(rb, r->text_buf_size,
14174cfc7258SJohn Ogness &d->text_blk_lpos, id);
14184cfc7258SJohn Ogness } else {
14194cfc7258SJohn Ogness if (!get_data(&rb->text_data_ring, &d->text_blk_lpos, &data_size))
14204cfc7258SJohn Ogness goto fail;
14214cfc7258SJohn Ogness
14224cfc7258SJohn Ogness /*
14234cfc7258SJohn Ogness * Increase the buffer size to include the original size. If
14244cfc7258SJohn Ogness * the meta data (@text_len) is not sane, use the full data
14254cfc7258SJohn Ogness * block size.
14264cfc7258SJohn Ogness */
1427cfe2790bSJohn Ogness if (WARN_ON_ONCE(info->text_len > data_size)) {
14284cfc7258SJohn Ogness pr_warn_once("wrong text_len value (%hu, expecting <=%u)\n",
1429cfe2790bSJohn Ogness info->text_len, data_size);
1430cfe2790bSJohn Ogness info->text_len = data_size;
14314cfc7258SJohn Ogness }
1432cfe2790bSJohn Ogness r->text_buf_size += info->text_len;
14334cfc7258SJohn Ogness
14344cfc7258SJohn Ogness if (!data_check_size(&rb->text_data_ring, r->text_buf_size))
14354cfc7258SJohn Ogness goto fail;
14364cfc7258SJohn Ogness
143759f8bccaSJohn Ogness if (r->text_buf_size > max_size)
143859f8bccaSJohn Ogness goto fail;
143959f8bccaSJohn Ogness
1440584da076SNikolay Borisov r->text_buf = data_realloc(rb, r->text_buf_size,
14414cfc7258SJohn Ogness &d->text_blk_lpos, id);
14424cfc7258SJohn Ogness }
14434cfc7258SJohn Ogness if (r->text_buf_size && !r->text_buf)
14444cfc7258SJohn Ogness goto fail;
14454cfc7258SJohn Ogness
1446cfe2790bSJohn Ogness r->info = info;
14474cfc7258SJohn Ogness
14484cfc7258SJohn Ogness e->text_space = space_used(&rb->text_data_ring, &d->text_blk_lpos);
14494cfc7258SJohn Ogness
14504cfc7258SJohn Ogness return true;
14514cfc7258SJohn Ogness fail:
14524cfc7258SJohn Ogness prb_commit(e);
14534cfc7258SJohn Ogness /* prb_commit() re-enabled interrupts. */
14544cfc7258SJohn Ogness fail_reopen:
14554cfc7258SJohn Ogness /* Make it clear to the caller that the re-reserve failed. */
14564cfc7258SJohn Ogness memset(r, 0, sizeof(*r));
14574cfc7258SJohn Ogness return false;
14584cfc7258SJohn Ogness }
14594cfc7258SJohn Ogness
14604cfc7258SJohn Ogness /*
14615f72e52bSJohn Ogness * @last_finalized_seq value guarantees that all records up to and including
14625f72e52bSJohn Ogness * this sequence number are finalized and can be read. The only exception are
14635f72e52bSJohn Ogness * too old records which have already been overwritten.
14645f72e52bSJohn Ogness *
14655f72e52bSJohn Ogness * It is also guaranteed that @last_finalized_seq only increases.
14665f72e52bSJohn Ogness *
14675f72e52bSJohn Ogness * Be aware that finalized records following non-finalized records are not
14685f72e52bSJohn Ogness * reported because they are not yet available to the reader. For example,
14695f72e52bSJohn Ogness * a new record stored via printk() will not be available to a printer if
14705f72e52bSJohn Ogness * it follows a record that has not been finalized yet. However, once that
14715f72e52bSJohn Ogness * non-finalized record becomes finalized, @last_finalized_seq will be
14725f72e52bSJohn Ogness * appropriately updated and the full set of finalized records will be
14735f72e52bSJohn Ogness * available to the printer. And since each printk() caller will either
14745f72e52bSJohn Ogness * directly print or trigger deferred printing of all available unprinted
14755f72e52bSJohn Ogness * records, all printk() messages will get printed.
14765f72e52bSJohn Ogness */
desc_last_finalized_seq(struct printk_ringbuffer * rb)14775f72e52bSJohn Ogness static u64 desc_last_finalized_seq(struct printk_ringbuffer *rb)
14785f72e52bSJohn Ogness {
14795f72e52bSJohn Ogness struct prb_desc_ring *desc_ring = &rb->desc_ring;
14805f72e52bSJohn Ogness unsigned long ulseq;
14815f72e52bSJohn Ogness
14825f72e52bSJohn Ogness /*
14835f72e52bSJohn Ogness * Guarantee the sequence number is loaded before loading the
14845f72e52bSJohn Ogness * associated record in order to guarantee that the record can be
14855f72e52bSJohn Ogness * seen by this CPU. This pairs with desc_update_last_finalized:A.
14865f72e52bSJohn Ogness */
14875f72e52bSJohn Ogness ulseq = atomic_long_read_acquire(&desc_ring->last_finalized_seq
14885f72e52bSJohn Ogness ); /* LMM(desc_last_finalized_seq:A) */
14895f72e52bSJohn Ogness
14905f72e52bSJohn Ogness return __ulseq_to_u64seq(rb, ulseq);
14915f72e52bSJohn Ogness }
14925f72e52bSJohn Ogness
14935f72e52bSJohn Ogness static bool _prb_read_valid(struct printk_ringbuffer *rb, u64 *seq,
14945f72e52bSJohn Ogness struct printk_record *r, unsigned int *line_count);
14955f72e52bSJohn Ogness
14965f72e52bSJohn Ogness /*
14975f72e52bSJohn Ogness * Check if there are records directly following @last_finalized_seq that are
14985f72e52bSJohn Ogness * finalized. If so, update @last_finalized_seq to the latest of these
14995f72e52bSJohn Ogness * records. It is not allowed to skip over records that are not yet finalized.
15005f72e52bSJohn Ogness */
desc_update_last_finalized(struct printk_ringbuffer * rb)15015f72e52bSJohn Ogness static void desc_update_last_finalized(struct printk_ringbuffer *rb)
15025f72e52bSJohn Ogness {
15035f72e52bSJohn Ogness struct prb_desc_ring *desc_ring = &rb->desc_ring;
15045f72e52bSJohn Ogness u64 old_seq = desc_last_finalized_seq(rb);
15055f72e52bSJohn Ogness unsigned long oldval;
15065f72e52bSJohn Ogness unsigned long newval;
15075f72e52bSJohn Ogness u64 finalized_seq;
15085f72e52bSJohn Ogness u64 try_seq;
15095f72e52bSJohn Ogness
15105f72e52bSJohn Ogness try_again:
15115f72e52bSJohn Ogness finalized_seq = old_seq;
15125f72e52bSJohn Ogness try_seq = finalized_seq + 1;
15135f72e52bSJohn Ogness
15145f72e52bSJohn Ogness /* Try to find later finalized records. */
15155f72e52bSJohn Ogness while (_prb_read_valid(rb, &try_seq, NULL, NULL)) {
15165f72e52bSJohn Ogness finalized_seq = try_seq;
15175f72e52bSJohn Ogness try_seq++;
15185f72e52bSJohn Ogness }
15195f72e52bSJohn Ogness
15205f72e52bSJohn Ogness /* No update needed if no later finalized record was found. */
15215f72e52bSJohn Ogness if (finalized_seq == old_seq)
15225f72e52bSJohn Ogness return;
15235f72e52bSJohn Ogness
15245f72e52bSJohn Ogness oldval = __u64seq_to_ulseq(old_seq);
15255f72e52bSJohn Ogness newval = __u64seq_to_ulseq(finalized_seq);
15265f72e52bSJohn Ogness
15275f72e52bSJohn Ogness /*
15285f72e52bSJohn Ogness * Set the sequence number of a later finalized record that has been
15295f72e52bSJohn Ogness * seen.
15305f72e52bSJohn Ogness *
15315f72e52bSJohn Ogness * Guarantee the record data is visible to other CPUs before storing
15325f72e52bSJohn Ogness * its sequence number. This pairs with desc_last_finalized_seq:A.
15335f72e52bSJohn Ogness *
15345f72e52bSJohn Ogness * Memory barrier involvement:
15355f72e52bSJohn Ogness *
15365f72e52bSJohn Ogness * If desc_last_finalized_seq:A reads from
15375f72e52bSJohn Ogness * desc_update_last_finalized:A, then desc_read:A reads from
15385f72e52bSJohn Ogness * _prb_commit:B.
15395f72e52bSJohn Ogness *
15405f72e52bSJohn Ogness * Relies on:
15415f72e52bSJohn Ogness *
15425f72e52bSJohn Ogness * RELEASE from _prb_commit:B to desc_update_last_finalized:A
15435f72e52bSJohn Ogness * matching
15445f72e52bSJohn Ogness * ACQUIRE from desc_last_finalized_seq:A to desc_read:A
15455f72e52bSJohn Ogness *
15465f72e52bSJohn Ogness * Note: _prb_commit:B and desc_update_last_finalized:A can be
15475f72e52bSJohn Ogness * different CPUs. However, the desc_update_last_finalized:A
15485f72e52bSJohn Ogness * CPU (which performs the release) must have previously seen
15495f72e52bSJohn Ogness * _prb_commit:B.
15505f72e52bSJohn Ogness */
15515f72e52bSJohn Ogness if (!atomic_long_try_cmpxchg_release(&desc_ring->last_finalized_seq,
15525f72e52bSJohn Ogness &oldval, newval)) { /* LMM(desc_update_last_finalized:A) */
15535f72e52bSJohn Ogness old_seq = __ulseq_to_u64seq(rb, oldval);
15545f72e52bSJohn Ogness goto try_again;
15555f72e52bSJohn Ogness }
15565f72e52bSJohn Ogness }
15575f72e52bSJohn Ogness
15585f72e52bSJohn Ogness /*
15594cfc7258SJohn Ogness * Attempt to finalize a specified descriptor. If this fails, the descriptor
15604cfc7258SJohn Ogness * is either already final or it will finalize itself when the writer commits.
15614cfc7258SJohn Ogness */
desc_make_final(struct printk_ringbuffer * rb,unsigned long id)15625f72e52bSJohn Ogness static void desc_make_final(struct printk_ringbuffer *rb, unsigned long id)
15634cfc7258SJohn Ogness {
15645f72e52bSJohn Ogness struct prb_desc_ring *desc_ring = &rb->desc_ring;
15654cfc7258SJohn Ogness unsigned long prev_state_val = DESC_SV(id, desc_committed);
15664cfc7258SJohn Ogness struct prb_desc *d = to_desc(desc_ring, id);
15674cfc7258SJohn Ogness
15685f72e52bSJohn Ogness if (atomic_long_try_cmpxchg_relaxed(&d->state_var, &prev_state_val,
15695f72e52bSJohn Ogness DESC_SV(id, desc_finalized))) { /* LMM(desc_make_final:A) */
15705f72e52bSJohn Ogness desc_update_last_finalized(rb);
15715f72e52bSJohn Ogness }
15724cfc7258SJohn Ogness }
15734cfc7258SJohn Ogness
1574b6cf8b3fSJohn Ogness /**
1575b6cf8b3fSJohn Ogness * prb_reserve() - Reserve space in the ringbuffer.
1576b6cf8b3fSJohn Ogness *
1577b6cf8b3fSJohn Ogness * @e: The entry structure to setup.
1578b6cf8b3fSJohn Ogness * @rb: The ringbuffer to reserve data in.
1579b6cf8b3fSJohn Ogness * @r: The record structure to allocate buffers for.
1580b6cf8b3fSJohn Ogness *
1581b6cf8b3fSJohn Ogness * This is the public function available to writers to reserve data.
1582b6cf8b3fSJohn Ogness *
1583f35efc78SJohn Ogness * The writer specifies the text size to reserve by setting the
1584f35efc78SJohn Ogness * @text_buf_size field of @r. To ensure proper initialization of @r,
1585f35efc78SJohn Ogness * prb_rec_init_wr() should be used.
1586b6cf8b3fSJohn Ogness *
1587b6cf8b3fSJohn Ogness * Context: Any context. Disables local interrupts on success.
1588b6cf8b3fSJohn Ogness * Return: true if at least text data could be allocated, otherwise false.
1589b6cf8b3fSJohn Ogness *
1590f35efc78SJohn Ogness * On success, the fields @info and @text_buf of @r will be set by this
1591f35efc78SJohn Ogness * function and should be filled in by the writer before committing. Also
1592b6cf8b3fSJohn Ogness * on success, prb_record_text_space() can be used on @e to query the actual
1593b6cf8b3fSJohn Ogness * space used for the text data block.
1594b6cf8b3fSJohn Ogness *
1595f35efc78SJohn Ogness * Important: @info->text_len needs to be set correctly by the writer in
1596f35efc78SJohn Ogness * order for data to be readable and/or extended. Its value
1597f35efc78SJohn Ogness * is initialized to 0.
1598b6cf8b3fSJohn Ogness */
prb_reserve(struct prb_reserved_entry * e,struct printk_ringbuffer * rb,struct printk_record * r)1599b6cf8b3fSJohn Ogness bool prb_reserve(struct prb_reserved_entry *e, struct printk_ringbuffer *rb,
1600b6cf8b3fSJohn Ogness struct printk_record *r)
1601b6cf8b3fSJohn Ogness {
1602b6cf8b3fSJohn Ogness struct prb_desc_ring *desc_ring = &rb->desc_ring;
1603cfe2790bSJohn Ogness struct printk_info *info;
1604b6cf8b3fSJohn Ogness struct prb_desc *d;
1605b6cf8b3fSJohn Ogness unsigned long id;
1606cc5c7041SJohn Ogness u64 seq;
1607b6cf8b3fSJohn Ogness
1608b6cf8b3fSJohn Ogness if (!data_check_size(&rb->text_data_ring, r->text_buf_size))
1609b6cf8b3fSJohn Ogness goto fail;
1610b6cf8b3fSJohn Ogness
1611b6cf8b3fSJohn Ogness /*
1612b6cf8b3fSJohn Ogness * Descriptors in the reserved state act as blockers to all further
1613b6cf8b3fSJohn Ogness * reservations once the desc_ring has fully wrapped. Disable
1614b6cf8b3fSJohn Ogness * interrupts during the reserve/commit window in order to minimize
1615b6cf8b3fSJohn Ogness * the likelihood of this happening.
1616b6cf8b3fSJohn Ogness */
1617b6cf8b3fSJohn Ogness local_irq_save(e->irqflags);
1618b6cf8b3fSJohn Ogness
1619b6cf8b3fSJohn Ogness if (!desc_reserve(rb, &id)) {
1620b6cf8b3fSJohn Ogness /* Descriptor reservation failures are tracked. */
1621b6cf8b3fSJohn Ogness atomic_long_inc(&rb->fail);
1622b6cf8b3fSJohn Ogness local_irq_restore(e->irqflags);
1623b6cf8b3fSJohn Ogness goto fail;
1624b6cf8b3fSJohn Ogness }
1625b6cf8b3fSJohn Ogness
1626b6cf8b3fSJohn Ogness d = to_desc(desc_ring, id);
1627cfe2790bSJohn Ogness info = to_info(desc_ring, id);
1628b6cf8b3fSJohn Ogness
1629b6cf8b3fSJohn Ogness /*
1630cc5c7041SJohn Ogness * All @info fields (except @seq) are cleared and must be filled in
1631cc5c7041SJohn Ogness * by the writer. Save @seq before clearing because it is used to
1632cc5c7041SJohn Ogness * determine the new sequence number.
1633cc5c7041SJohn Ogness */
1634cfe2790bSJohn Ogness seq = info->seq;
1635cfe2790bSJohn Ogness memset(info, 0, sizeof(*info));
1636cc5c7041SJohn Ogness
1637cc5c7041SJohn Ogness /*
1638b6cf8b3fSJohn Ogness * Set the @e fields here so that prb_commit() can be used if
1639b6cf8b3fSJohn Ogness * text data allocation fails.
1640b6cf8b3fSJohn Ogness */
1641b6cf8b3fSJohn Ogness e->rb = rb;
1642b6cf8b3fSJohn Ogness e->id = id;
1643b6cf8b3fSJohn Ogness
1644b6cf8b3fSJohn Ogness /*
1645b6cf8b3fSJohn Ogness * Initialize the sequence number if it has "never been set".
1646b6cf8b3fSJohn Ogness * Otherwise just increment it by a full wrap.
1647b6cf8b3fSJohn Ogness *
1648b6cf8b3fSJohn Ogness * @seq is considered "never been set" if it has a value of 0,
1649cfe2790bSJohn Ogness * _except_ for @infos[0], which was specially setup by the ringbuffer
1650b6cf8b3fSJohn Ogness * initializer and therefore is always considered as set.
1651b6cf8b3fSJohn Ogness *
1652b6cf8b3fSJohn Ogness * See the "Bootstrap" comment block in printk_ringbuffer.h for
1653b6cf8b3fSJohn Ogness * details about how the initializer bootstraps the descriptors.
1654b6cf8b3fSJohn Ogness */
1655cc5c7041SJohn Ogness if (seq == 0 && DESC_INDEX(desc_ring, id) != 0)
1656cfe2790bSJohn Ogness info->seq = DESC_INDEX(desc_ring, id);
1657b6cf8b3fSJohn Ogness else
1658cfe2790bSJohn Ogness info->seq = seq + DESCS_COUNT(desc_ring);
1659b6cf8b3fSJohn Ogness
16604cfc7258SJohn Ogness /*
16614cfc7258SJohn Ogness * New data is about to be reserved. Once that happens, previous
16624cfc7258SJohn Ogness * descriptors are no longer able to be extended. Finalize the
16634cfc7258SJohn Ogness * previous descriptor now so that it can be made available to
16644cfc7258SJohn Ogness * readers. (For seq==0 there is no previous descriptor.)
16654cfc7258SJohn Ogness */
1666cfe2790bSJohn Ogness if (info->seq > 0)
16675f72e52bSJohn Ogness desc_make_final(rb, DESC_ID(id - 1));
16684cfc7258SJohn Ogness
1669584da076SNikolay Borisov r->text_buf = data_alloc(rb, r->text_buf_size, &d->text_blk_lpos, id);
1670b6cf8b3fSJohn Ogness /* If text data allocation fails, a data-less record is committed. */
1671b6cf8b3fSJohn Ogness if (r->text_buf_size && !r->text_buf) {
1672b6cf8b3fSJohn Ogness prb_commit(e);
1673b6cf8b3fSJohn Ogness /* prb_commit() re-enabled interrupts. */
1674b6cf8b3fSJohn Ogness goto fail;
1675b6cf8b3fSJohn Ogness }
1676b6cf8b3fSJohn Ogness
1677cfe2790bSJohn Ogness r->info = info;
1678b6cf8b3fSJohn Ogness
1679b6cf8b3fSJohn Ogness /* Record full text space used by record. */
1680b6cf8b3fSJohn Ogness e->text_space = space_used(&rb->text_data_ring, &d->text_blk_lpos);
1681b6cf8b3fSJohn Ogness
1682b6cf8b3fSJohn Ogness return true;
1683b6cf8b3fSJohn Ogness fail:
1684b6cf8b3fSJohn Ogness /* Make it clear to the caller that the reserve failed. */
1685b6cf8b3fSJohn Ogness memset(r, 0, sizeof(*r));
1686b6cf8b3fSJohn Ogness return false;
1687b6cf8b3fSJohn Ogness }
1688b6cf8b3fSJohn Ogness
16894cfc7258SJohn Ogness /* Commit the data (possibly finalizing it) and restore interrupts. */
_prb_commit(struct prb_reserved_entry * e,unsigned long state_val)16904cfc7258SJohn Ogness static void _prb_commit(struct prb_reserved_entry *e, unsigned long state_val)
16914cfc7258SJohn Ogness {
16924cfc7258SJohn Ogness struct prb_desc_ring *desc_ring = &e->rb->desc_ring;
16934cfc7258SJohn Ogness struct prb_desc *d = to_desc(desc_ring, e->id);
16944cfc7258SJohn Ogness unsigned long prev_state_val = DESC_SV(e->id, desc_reserved);
16954cfc7258SJohn Ogness
16964cfc7258SJohn Ogness /* Now the writer has finished all writing: LMM(_prb_commit:A) */
16974cfc7258SJohn Ogness
16984cfc7258SJohn Ogness /*
16994cfc7258SJohn Ogness * Set the descriptor as committed. See "ABA Issues" about why
17004cfc7258SJohn Ogness * cmpxchg() instead of set() is used.
17014cfc7258SJohn Ogness *
17024cfc7258SJohn Ogness * 1 Guarantee all record data is stored before the descriptor state
17034cfc7258SJohn Ogness * is stored as committed. A write memory barrier is sufficient
17044cfc7258SJohn Ogness * for this. This pairs with desc_read:B and desc_reopen_last:A.
17054cfc7258SJohn Ogness *
17064cfc7258SJohn Ogness * 2. Guarantee the descriptor state is stored as committed before
17074cfc7258SJohn Ogness * re-checking the head ID in order to possibly finalize this
17084cfc7258SJohn Ogness * descriptor. This pairs with desc_reserve:D.
17094cfc7258SJohn Ogness *
17104cfc7258SJohn Ogness * Memory barrier involvement:
17114cfc7258SJohn Ogness *
17124cfc7258SJohn Ogness * If prb_commit:A reads from desc_reserve:D, then
17134cfc7258SJohn Ogness * desc_make_final:A reads from _prb_commit:B.
17144cfc7258SJohn Ogness *
17154cfc7258SJohn Ogness * Relies on:
17164cfc7258SJohn Ogness *
17174cfc7258SJohn Ogness * MB _prb_commit:B to prb_commit:A
17184cfc7258SJohn Ogness * matching
17194cfc7258SJohn Ogness * MB desc_reserve:D to desc_make_final:A
17204cfc7258SJohn Ogness */
17214cfc7258SJohn Ogness if (!atomic_long_try_cmpxchg(&d->state_var, &prev_state_val,
17224cfc7258SJohn Ogness DESC_SV(e->id, state_val))) { /* LMM(_prb_commit:B) */
17234cfc7258SJohn Ogness WARN_ON_ONCE(1);
17244cfc7258SJohn Ogness }
17254cfc7258SJohn Ogness
17264cfc7258SJohn Ogness /* Restore interrupts, the reserve/commit window is finished. */
17274cfc7258SJohn Ogness local_irq_restore(e->irqflags);
17284cfc7258SJohn Ogness }
17294cfc7258SJohn Ogness
1730b6cf8b3fSJohn Ogness /**
1731b6cf8b3fSJohn Ogness * prb_commit() - Commit (previously reserved) data to the ringbuffer.
1732b6cf8b3fSJohn Ogness *
1733b6cf8b3fSJohn Ogness * @e: The entry containing the reserved data information.
1734b6cf8b3fSJohn Ogness *
1735b6cf8b3fSJohn Ogness * This is the public function available to writers to commit data.
1736b6cf8b3fSJohn Ogness *
17374cfc7258SJohn Ogness * Note that the data is not yet available to readers until it is finalized.
17384cfc7258SJohn Ogness * Finalizing happens automatically when space for the next record is
17394cfc7258SJohn Ogness * reserved.
17404cfc7258SJohn Ogness *
17414cfc7258SJohn Ogness * See prb_final_commit() for a version of this function that finalizes
17424cfc7258SJohn Ogness * immediately.
17434cfc7258SJohn Ogness *
1744b6cf8b3fSJohn Ogness * Context: Any context. Enables local interrupts.
1745b6cf8b3fSJohn Ogness */
prb_commit(struct prb_reserved_entry * e)1746b6cf8b3fSJohn Ogness void prb_commit(struct prb_reserved_entry *e)
1747b6cf8b3fSJohn Ogness {
1748b6cf8b3fSJohn Ogness struct prb_desc_ring *desc_ring = &e->rb->desc_ring;
17494cfc7258SJohn Ogness unsigned long head_id;
1750b6cf8b3fSJohn Ogness
17514cfc7258SJohn Ogness _prb_commit(e, desc_committed);
1752b6cf8b3fSJohn Ogness
1753b6cf8b3fSJohn Ogness /*
17544cfc7258SJohn Ogness * If this descriptor is no longer the head (i.e. a new record has
17554cfc7258SJohn Ogness * been allocated), extending the data for this record is no longer
17564cfc7258SJohn Ogness * allowed and therefore it must be finalized.
1757b6cf8b3fSJohn Ogness */
17584cfc7258SJohn Ogness head_id = atomic_long_read(&desc_ring->head_id); /* LMM(prb_commit:A) */
17594cfc7258SJohn Ogness if (head_id != e->id)
17605f72e52bSJohn Ogness desc_make_final(e->rb, e->id);
1761b6cf8b3fSJohn Ogness }
1762b6cf8b3fSJohn Ogness
17634cfc7258SJohn Ogness /**
17644cfc7258SJohn Ogness * prb_final_commit() - Commit and finalize (previously reserved) data to
17654cfc7258SJohn Ogness * the ringbuffer.
17664cfc7258SJohn Ogness *
17674cfc7258SJohn Ogness * @e: The entry containing the reserved data information.
17684cfc7258SJohn Ogness *
17694cfc7258SJohn Ogness * This is the public function available to writers to commit+finalize data.
17704cfc7258SJohn Ogness *
17714cfc7258SJohn Ogness * By finalizing, the data is made immediately available to readers.
17724cfc7258SJohn Ogness *
17734cfc7258SJohn Ogness * This function should only be used if there are no intentions of extending
17744cfc7258SJohn Ogness * this data using prb_reserve_in_last().
17754cfc7258SJohn Ogness *
17764cfc7258SJohn Ogness * Context: Any context. Enables local interrupts.
17774cfc7258SJohn Ogness */
prb_final_commit(struct prb_reserved_entry * e)17784cfc7258SJohn Ogness void prb_final_commit(struct prb_reserved_entry *e)
17794cfc7258SJohn Ogness {
17804cfc7258SJohn Ogness _prb_commit(e, desc_finalized);
1781f244b4dcSPetr Mladek
17825f72e52bSJohn Ogness desc_update_last_finalized(e->rb);
1783b6cf8b3fSJohn Ogness }
1784b6cf8b3fSJohn Ogness
1785b6cf8b3fSJohn Ogness /*
1786b6cf8b3fSJohn Ogness * Count the number of lines in provided text. All text has at least 1 line
1787b6cf8b3fSJohn Ogness * (even if @text_size is 0). Each '\n' processed is counted as an additional
1788b6cf8b3fSJohn Ogness * line.
1789b6cf8b3fSJohn Ogness */
count_lines(const char * text,unsigned int text_size)1790d397820fSJohn Ogness static unsigned int count_lines(const char *text, unsigned int text_size)
1791b6cf8b3fSJohn Ogness {
1792b6cf8b3fSJohn Ogness unsigned int next_size = text_size;
1793b6cf8b3fSJohn Ogness unsigned int line_count = 1;
1794d397820fSJohn Ogness const char *next = text;
1795b6cf8b3fSJohn Ogness
1796b6cf8b3fSJohn Ogness while (next_size) {
1797b6cf8b3fSJohn Ogness next = memchr(next, '\n', next_size);
1798b6cf8b3fSJohn Ogness if (!next)
1799b6cf8b3fSJohn Ogness break;
1800b6cf8b3fSJohn Ogness line_count++;
1801b6cf8b3fSJohn Ogness next++;
1802b6cf8b3fSJohn Ogness next_size = text_size - (next - text);
1803b6cf8b3fSJohn Ogness }
1804b6cf8b3fSJohn Ogness
1805b6cf8b3fSJohn Ogness return line_count;
1806b6cf8b3fSJohn Ogness }
1807b6cf8b3fSJohn Ogness
1808b6cf8b3fSJohn Ogness /*
1809b6cf8b3fSJohn Ogness * Given @blk_lpos, copy an expected @len of data into the provided buffer.
1810b6cf8b3fSJohn Ogness * If @line_count is provided, count the number of lines in the data.
1811b6cf8b3fSJohn Ogness *
1812b6cf8b3fSJohn Ogness * This function (used by readers) performs strict validation on the data
1813b6cf8b3fSJohn Ogness * size to possibly detect bugs in the writer code. A WARN_ON_ONCE() is
1814b6cf8b3fSJohn Ogness * triggered if an internal error is detected.
1815b6cf8b3fSJohn Ogness */
copy_data(struct prb_data_ring * data_ring,struct prb_data_blk_lpos * blk_lpos,u16 len,char * buf,unsigned int buf_size,unsigned int * line_count)1816b6cf8b3fSJohn Ogness static bool copy_data(struct prb_data_ring *data_ring,
1817b6cf8b3fSJohn Ogness struct prb_data_blk_lpos *blk_lpos, u16 len, char *buf,
1818b6cf8b3fSJohn Ogness unsigned int buf_size, unsigned int *line_count)
1819b6cf8b3fSJohn Ogness {
1820b6cf8b3fSJohn Ogness unsigned int data_size;
1821d397820fSJohn Ogness const char *data;
1822b6cf8b3fSJohn Ogness
1823b6cf8b3fSJohn Ogness /* Caller might not want any data. */
1824b6cf8b3fSJohn Ogness if ((!buf || !buf_size) && !line_count)
1825b6cf8b3fSJohn Ogness return true;
1826b6cf8b3fSJohn Ogness
1827b6cf8b3fSJohn Ogness data = get_data(data_ring, blk_lpos, &data_size);
1828b6cf8b3fSJohn Ogness if (!data)
1829b6cf8b3fSJohn Ogness return false;
1830b6cf8b3fSJohn Ogness
1831b6cf8b3fSJohn Ogness /*
1832b6cf8b3fSJohn Ogness * Actual cannot be less than expected. It can be more than expected
1833b6cf8b3fSJohn Ogness * because of the trailing alignment padding.
1834cfe2790bSJohn Ogness *
1835cfe2790bSJohn Ogness * Note that invalid @len values can occur because the caller loads
1836cfe2790bSJohn Ogness * the value during an allowed data race.
1837b6cf8b3fSJohn Ogness */
1838cfe2790bSJohn Ogness if (data_size < (unsigned int)len)
1839b6cf8b3fSJohn Ogness return false;
1840b6cf8b3fSJohn Ogness
1841b6cf8b3fSJohn Ogness /* Caller interested in the line count? */
1842b6cf8b3fSJohn Ogness if (line_count)
1843668af87fSJohn Ogness *line_count = count_lines(data, len);
1844b6cf8b3fSJohn Ogness
1845b6cf8b3fSJohn Ogness /* Caller interested in the data content? */
1846b6cf8b3fSJohn Ogness if (!buf || !buf_size)
1847b6cf8b3fSJohn Ogness return true;
1848b6cf8b3fSJohn Ogness
184953e9e33eSKees Cook data_size = min_t(unsigned int, buf_size, len);
1850b6cf8b3fSJohn Ogness
1851b6cf8b3fSJohn Ogness memcpy(&buf[0], data, data_size); /* LMM(copy_data:A) */
1852b6cf8b3fSJohn Ogness return true;
1853b6cf8b3fSJohn Ogness }
1854b6cf8b3fSJohn Ogness
1855b6cf8b3fSJohn Ogness /*
1856b6cf8b3fSJohn Ogness * This is an extended version of desc_read(). It gets a copy of a specified
18574cfc7258SJohn Ogness * descriptor. However, it also verifies that the record is finalized and has
1858b6cf8b3fSJohn Ogness * the sequence number @seq. On success, 0 is returned.
1859b6cf8b3fSJohn Ogness *
1860b6cf8b3fSJohn Ogness * Error return values:
18614cfc7258SJohn Ogness * -EINVAL: A finalized record with sequence number @seq does not exist.
18624cfc7258SJohn Ogness * -ENOENT: A finalized record with sequence number @seq exists, but its data
1863b6cf8b3fSJohn Ogness * is not available. This is a valid record, so readers should
1864b6cf8b3fSJohn Ogness * continue with the next record.
1865b6cf8b3fSJohn Ogness */
desc_read_finalized_seq(struct prb_desc_ring * desc_ring,unsigned long id,u64 seq,struct prb_desc * desc_out)18664cfc7258SJohn Ogness static int desc_read_finalized_seq(struct prb_desc_ring *desc_ring,
1867b6cf8b3fSJohn Ogness unsigned long id, u64 seq,
1868b6cf8b3fSJohn Ogness struct prb_desc *desc_out)
1869b6cf8b3fSJohn Ogness {
1870b6cf8b3fSJohn Ogness struct prb_data_blk_lpos *blk_lpos = &desc_out->text_blk_lpos;
1871b6cf8b3fSJohn Ogness enum desc_state d_state;
1872cfe2790bSJohn Ogness u64 s;
1873b6cf8b3fSJohn Ogness
1874cfe2790bSJohn Ogness d_state = desc_read(desc_ring, id, desc_out, &s, NULL);
1875b6cf8b3fSJohn Ogness
1876b6cf8b3fSJohn Ogness /*
1877b6cf8b3fSJohn Ogness * An unexpected @id (desc_miss) or @seq mismatch means the record
18784cfc7258SJohn Ogness * does not exist. A descriptor in the reserved or committed state
18794cfc7258SJohn Ogness * means the record does not yet exist for the reader.
1880b6cf8b3fSJohn Ogness */
1881b6cf8b3fSJohn Ogness if (d_state == desc_miss ||
1882b6cf8b3fSJohn Ogness d_state == desc_reserved ||
18834cfc7258SJohn Ogness d_state == desc_committed ||
1884cfe2790bSJohn Ogness s != seq) {
1885b6cf8b3fSJohn Ogness return -EINVAL;
1886b6cf8b3fSJohn Ogness }
1887b6cf8b3fSJohn Ogness
1888b6cf8b3fSJohn Ogness /*
1889b6cf8b3fSJohn Ogness * A descriptor in the reusable state may no longer have its data
1890d397820fSJohn Ogness * available; report it as existing but with lost data. Or the record
1891d397820fSJohn Ogness * may actually be a record with lost data.
1892b6cf8b3fSJohn Ogness */
1893b6cf8b3fSJohn Ogness if (d_state == desc_reusable ||
1894d397820fSJohn Ogness (blk_lpos->begin == FAILED_LPOS && blk_lpos->next == FAILED_LPOS)) {
1895b6cf8b3fSJohn Ogness return -ENOENT;
1896b6cf8b3fSJohn Ogness }
1897b6cf8b3fSJohn Ogness
1898b6cf8b3fSJohn Ogness return 0;
1899b6cf8b3fSJohn Ogness }
1900b6cf8b3fSJohn Ogness
1901b6cf8b3fSJohn Ogness /*
1902b6cf8b3fSJohn Ogness * Copy the ringbuffer data from the record with @seq to the provided
1903b6cf8b3fSJohn Ogness * @r buffer. On success, 0 is returned.
1904b6cf8b3fSJohn Ogness *
19054cfc7258SJohn Ogness * See desc_read_finalized_seq() for error return values.
1906b6cf8b3fSJohn Ogness */
prb_read(struct printk_ringbuffer * rb,u64 seq,struct printk_record * r,unsigned int * line_count)1907b6cf8b3fSJohn Ogness static int prb_read(struct printk_ringbuffer *rb, u64 seq,
1908b6cf8b3fSJohn Ogness struct printk_record *r, unsigned int *line_count)
1909b6cf8b3fSJohn Ogness {
1910b6cf8b3fSJohn Ogness struct prb_desc_ring *desc_ring = &rb->desc_ring;
1911cfe2790bSJohn Ogness struct printk_info *info = to_info(desc_ring, seq);
1912b6cf8b3fSJohn Ogness struct prb_desc *rdesc = to_desc(desc_ring, seq);
1913b6cf8b3fSJohn Ogness atomic_long_t *state_var = &rdesc->state_var;
1914b6cf8b3fSJohn Ogness struct prb_desc desc;
1915b6cf8b3fSJohn Ogness unsigned long id;
1916b6cf8b3fSJohn Ogness int err;
1917b6cf8b3fSJohn Ogness
1918b6cf8b3fSJohn Ogness /* Extract the ID, used to specify the descriptor to read. */
1919b6cf8b3fSJohn Ogness id = DESC_ID(atomic_long_read(state_var));
1920b6cf8b3fSJohn Ogness
1921b6cf8b3fSJohn Ogness /* Get a local copy of the correct descriptor (if available). */
19224cfc7258SJohn Ogness err = desc_read_finalized_seq(desc_ring, id, seq, &desc);
1923b6cf8b3fSJohn Ogness
1924b6cf8b3fSJohn Ogness /*
1925b6cf8b3fSJohn Ogness * If @r is NULL, the caller is only interested in the availability
1926b6cf8b3fSJohn Ogness * of the record.
1927b6cf8b3fSJohn Ogness */
1928b6cf8b3fSJohn Ogness if (err || !r)
1929b6cf8b3fSJohn Ogness return err;
1930b6cf8b3fSJohn Ogness
1931b6cf8b3fSJohn Ogness /* If requested, copy meta data. */
1932b6cf8b3fSJohn Ogness if (r->info)
1933cfe2790bSJohn Ogness memcpy(r->info, info, sizeof(*(r->info)));
1934b6cf8b3fSJohn Ogness
1935b6cf8b3fSJohn Ogness /* Copy text data. If it fails, this is a data-less record. */
1936cfe2790bSJohn Ogness if (!copy_data(&rb->text_data_ring, &desc.text_blk_lpos, info->text_len,
1937b6cf8b3fSJohn Ogness r->text_buf, r->text_buf_size, line_count)) {
1938b6cf8b3fSJohn Ogness return -ENOENT;
1939b6cf8b3fSJohn Ogness }
1940b6cf8b3fSJohn Ogness
19414cfc7258SJohn Ogness /* Ensure the record is still finalized and has the same @seq. */
19424cfc7258SJohn Ogness return desc_read_finalized_seq(desc_ring, id, seq, &desc);
1943b6cf8b3fSJohn Ogness }
1944b6cf8b3fSJohn Ogness
1945b6cf8b3fSJohn Ogness /* Get the sequence number of the tail descriptor. */
prb_first_seq(struct printk_ringbuffer * rb)194690ad525cSJohn Ogness u64 prb_first_seq(struct printk_ringbuffer *rb)
1947b6cf8b3fSJohn Ogness {
1948b6cf8b3fSJohn Ogness struct prb_desc_ring *desc_ring = &rb->desc_ring;
1949b6cf8b3fSJohn Ogness enum desc_state d_state;
1950b6cf8b3fSJohn Ogness struct prb_desc desc;
1951b6cf8b3fSJohn Ogness unsigned long id;
1952cfe2790bSJohn Ogness u64 seq;
1953b6cf8b3fSJohn Ogness
1954b6cf8b3fSJohn Ogness for (;;) {
1955b6cf8b3fSJohn Ogness id = atomic_long_read(&rb->desc_ring.tail_id); /* LMM(prb_first_seq:A) */
1956b6cf8b3fSJohn Ogness
1957cfe2790bSJohn Ogness d_state = desc_read(desc_ring, id, &desc, &seq, NULL); /* LMM(prb_first_seq:B) */
1958b6cf8b3fSJohn Ogness
1959b6cf8b3fSJohn Ogness /*
1960b6cf8b3fSJohn Ogness * This loop will not be infinite because the tail is
19614cfc7258SJohn Ogness * _always_ in the finalized or reusable state.
1962b6cf8b3fSJohn Ogness */
19634cfc7258SJohn Ogness if (d_state == desc_finalized || d_state == desc_reusable)
1964b6cf8b3fSJohn Ogness break;
1965b6cf8b3fSJohn Ogness
1966b6cf8b3fSJohn Ogness /*
1967b6cf8b3fSJohn Ogness * Guarantee the last state load from desc_read() is before
1968b6cf8b3fSJohn Ogness * reloading @tail_id in order to see a new tail in the case
1969b6cf8b3fSJohn Ogness * that the descriptor has been recycled. This pairs with
1970b6cf8b3fSJohn Ogness * desc_reserve:D.
1971b6cf8b3fSJohn Ogness *
1972b6cf8b3fSJohn Ogness * Memory barrier involvement:
1973b6cf8b3fSJohn Ogness *
1974b6cf8b3fSJohn Ogness * If prb_first_seq:B reads from desc_reserve:F, then
1975b6cf8b3fSJohn Ogness * prb_first_seq:A reads from desc_push_tail:B.
1976b6cf8b3fSJohn Ogness *
1977b6cf8b3fSJohn Ogness * Relies on:
1978b6cf8b3fSJohn Ogness *
1979b6cf8b3fSJohn Ogness * MB from desc_push_tail:B to desc_reserve:F
1980b6cf8b3fSJohn Ogness * matching
1981b6cf8b3fSJohn Ogness * RMB prb_first_seq:B to prb_first_seq:A
1982b6cf8b3fSJohn Ogness */
1983b6cf8b3fSJohn Ogness smp_rmb(); /* LMM(prb_first_seq:C) */
1984b6cf8b3fSJohn Ogness }
1985b6cf8b3fSJohn Ogness
1986cfe2790bSJohn Ogness return seq;
1987b6cf8b3fSJohn Ogness }
1988b6cf8b3fSJohn Ogness
1989ac7d7844SJohn Ogness /**
1990ac7d7844SJohn Ogness * prb_next_reserve_seq() - Get the sequence number after the most recently
1991ac7d7844SJohn Ogness * reserved record.
1992ac7d7844SJohn Ogness *
1993ac7d7844SJohn Ogness * @rb: The ringbuffer to get the sequence number from.
1994ac7d7844SJohn Ogness *
1995ac7d7844SJohn Ogness * This is the public function available to readers to see what sequence
1996ac7d7844SJohn Ogness * number will be assigned to the next reserved record.
1997ac7d7844SJohn Ogness *
1998ac7d7844SJohn Ogness * Note that depending on the situation, this value can be equal to or
1999ac7d7844SJohn Ogness * higher than the sequence number returned by prb_next_seq().
2000ac7d7844SJohn Ogness *
2001ac7d7844SJohn Ogness * Context: Any context.
2002ac7d7844SJohn Ogness * Return: The sequence number that will be assigned to the next record
2003ac7d7844SJohn Ogness * reserved.
2004ac7d7844SJohn Ogness */
prb_next_reserve_seq(struct printk_ringbuffer * rb)2005ac7d7844SJohn Ogness u64 prb_next_reserve_seq(struct printk_ringbuffer *rb)
2006ac7d7844SJohn Ogness {
2007ac7d7844SJohn Ogness struct prb_desc_ring *desc_ring = &rb->desc_ring;
2008ac7d7844SJohn Ogness unsigned long last_finalized_id;
2009ac7d7844SJohn Ogness atomic_long_t *state_var;
2010ac7d7844SJohn Ogness u64 last_finalized_seq;
2011ac7d7844SJohn Ogness unsigned long head_id;
2012ac7d7844SJohn Ogness struct prb_desc desc;
2013ac7d7844SJohn Ogness unsigned long diff;
2014ac7d7844SJohn Ogness struct prb_desc *d;
2015ac7d7844SJohn Ogness int err;
2016ac7d7844SJohn Ogness
2017ac7d7844SJohn Ogness /*
2018ac7d7844SJohn Ogness * It may not be possible to read a sequence number for @head_id.
2019ac7d7844SJohn Ogness * So the ID of @last_finailzed_seq is used to calculate what the
2020ac7d7844SJohn Ogness * sequence number of @head_id will be.
2021ac7d7844SJohn Ogness */
2022ac7d7844SJohn Ogness
2023ac7d7844SJohn Ogness try_again:
2024ac7d7844SJohn Ogness last_finalized_seq = desc_last_finalized_seq(rb);
2025ac7d7844SJohn Ogness
2026ac7d7844SJohn Ogness /*
2027ac7d7844SJohn Ogness * @head_id is loaded after @last_finalized_seq to ensure that
2028ac7d7844SJohn Ogness * it points to the record with @last_finalized_seq or newer.
2029ac7d7844SJohn Ogness *
2030ac7d7844SJohn Ogness * Memory barrier involvement:
2031ac7d7844SJohn Ogness *
2032ac7d7844SJohn Ogness * If desc_last_finalized_seq:A reads from
2033ac7d7844SJohn Ogness * desc_update_last_finalized:A, then
2034ac7d7844SJohn Ogness * prb_next_reserve_seq:A reads from desc_reserve:D.
2035ac7d7844SJohn Ogness *
2036ac7d7844SJohn Ogness * Relies on:
2037ac7d7844SJohn Ogness *
2038ac7d7844SJohn Ogness * RELEASE from desc_reserve:D to desc_update_last_finalized:A
2039ac7d7844SJohn Ogness * matching
2040ac7d7844SJohn Ogness * ACQUIRE from desc_last_finalized_seq:A to prb_next_reserve_seq:A
2041ac7d7844SJohn Ogness *
2042ac7d7844SJohn Ogness * Note: desc_reserve:D and desc_update_last_finalized:A can be
2043ac7d7844SJohn Ogness * different CPUs. However, the desc_update_last_finalized:A CPU
2044ac7d7844SJohn Ogness * (which performs the release) must have previously seen
2045ac7d7844SJohn Ogness * desc_read:C, which implies desc_reserve:D can be seen.
2046ac7d7844SJohn Ogness */
2047ac7d7844SJohn Ogness head_id = atomic_long_read(&desc_ring->head_id); /* LMM(prb_next_reserve_seq:A) */
2048ac7d7844SJohn Ogness
2049ac7d7844SJohn Ogness d = to_desc(desc_ring, last_finalized_seq);
2050ac7d7844SJohn Ogness state_var = &d->state_var;
2051ac7d7844SJohn Ogness
2052ac7d7844SJohn Ogness /* Extract the ID, used to specify the descriptor to read. */
2053ac7d7844SJohn Ogness last_finalized_id = DESC_ID(atomic_long_read(state_var));
2054ac7d7844SJohn Ogness
2055ac7d7844SJohn Ogness /* Ensure @last_finalized_id is correct. */
2056ac7d7844SJohn Ogness err = desc_read_finalized_seq(desc_ring, last_finalized_id, last_finalized_seq, &desc);
2057ac7d7844SJohn Ogness
2058ac7d7844SJohn Ogness if (err == -EINVAL) {
2059ac7d7844SJohn Ogness if (last_finalized_seq == 0) {
2060ac7d7844SJohn Ogness /*
2061ac7d7844SJohn Ogness * No record has been finalized or even reserved yet.
2062ac7d7844SJohn Ogness *
2063ac7d7844SJohn Ogness * The @head_id is initialized such that the first
2064ac7d7844SJohn Ogness * increment will yield the first record (seq=0).
2065ac7d7844SJohn Ogness * Handle it separately to avoid a negative @diff
2066ac7d7844SJohn Ogness * below.
2067ac7d7844SJohn Ogness */
2068ac7d7844SJohn Ogness if (head_id == DESC0_ID(desc_ring->count_bits))
2069ac7d7844SJohn Ogness return 0;
2070ac7d7844SJohn Ogness
2071ac7d7844SJohn Ogness /*
2072ac7d7844SJohn Ogness * One or more descriptors are already reserved. Use
2073ac7d7844SJohn Ogness * the descriptor ID of the first one (@seq=0) for
2074ac7d7844SJohn Ogness * the @diff below.
2075ac7d7844SJohn Ogness */
2076ac7d7844SJohn Ogness last_finalized_id = DESC0_ID(desc_ring->count_bits) + 1;
2077ac7d7844SJohn Ogness } else {
2078ac7d7844SJohn Ogness /* Record must have been overwritten. Try again. */
2079ac7d7844SJohn Ogness goto try_again;
2080ac7d7844SJohn Ogness }
2081ac7d7844SJohn Ogness }
2082ac7d7844SJohn Ogness
2083ac7d7844SJohn Ogness /* Diff of known descriptor IDs to compute related sequence numbers. */
2084ac7d7844SJohn Ogness diff = head_id - last_finalized_id;
2085ac7d7844SJohn Ogness
2086ac7d7844SJohn Ogness /*
2087ac7d7844SJohn Ogness * @head_id points to the most recently reserved record, but this
2088ac7d7844SJohn Ogness * function returns the sequence number that will be assigned to the
2089ac7d7844SJohn Ogness * next (not yet reserved) record. Thus +1 is needed.
2090ac7d7844SJohn Ogness */
2091ac7d7844SJohn Ogness return (last_finalized_seq + diff + 1);
2092ac7d7844SJohn Ogness }
2093ac7d7844SJohn Ogness
2094b6cf8b3fSJohn Ogness /*
2095584528d6SJohn Ogness * Non-blocking read of a record.
2096b6cf8b3fSJohn Ogness *
2097584528d6SJohn Ogness * On success @seq is updated to the record that was read and (if provided)
2098584528d6SJohn Ogness * @r and @line_count will contain the read/calculated data.
2099584528d6SJohn Ogness *
2100584528d6SJohn Ogness * On failure @seq is updated to a record that is not yet available to the
2101584528d6SJohn Ogness * reader, but it will be the next record available to the reader.
2102b1c4c67aSJohn Ogness *
2103b1c4c67aSJohn Ogness * Note: When the current CPU is in panic, this function will skip over any
2104b1c4c67aSJohn Ogness * non-existent/non-finalized records in order to allow the panic CPU
2105b1c4c67aSJohn Ogness * to print any and all records that have been finalized.
2106b6cf8b3fSJohn Ogness */
_prb_read_valid(struct printk_ringbuffer * rb,u64 * seq,struct printk_record * r,unsigned int * line_count)2107b6cf8b3fSJohn Ogness static bool _prb_read_valid(struct printk_ringbuffer *rb, u64 *seq,
2108b6cf8b3fSJohn Ogness struct printk_record *r, unsigned int *line_count)
2109b6cf8b3fSJohn Ogness {
2110b6cf8b3fSJohn Ogness u64 tail_seq;
2111b6cf8b3fSJohn Ogness int err;
2112b6cf8b3fSJohn Ogness
2113b6cf8b3fSJohn Ogness while ((err = prb_read(rb, *seq, r, line_count))) {
2114b6cf8b3fSJohn Ogness tail_seq = prb_first_seq(rb);
2115b6cf8b3fSJohn Ogness
2116b6cf8b3fSJohn Ogness if (*seq < tail_seq) {
2117b6cf8b3fSJohn Ogness /*
2118b6cf8b3fSJohn Ogness * Behind the tail. Catch up and try again. This
2119b6cf8b3fSJohn Ogness * can happen for -ENOENT and -EINVAL cases.
2120b6cf8b3fSJohn Ogness */
2121b6cf8b3fSJohn Ogness *seq = tail_seq;
2122b6cf8b3fSJohn Ogness
2123b6cf8b3fSJohn Ogness } else if (err == -ENOENT) {
2124584528d6SJohn Ogness /* Record exists, but the data was lost. Skip. */
2125b6cf8b3fSJohn Ogness (*seq)++;
2126b6cf8b3fSJohn Ogness
2127b6cf8b3fSJohn Ogness } else {
2128b1c4c67aSJohn Ogness /*
2129b1c4c67aSJohn Ogness * Non-existent/non-finalized record. Must stop.
2130b1c4c67aSJohn Ogness *
2131b1c4c67aSJohn Ogness * For panic situations it cannot be expected that
2132b1c4c67aSJohn Ogness * non-finalized records will become finalized. But
2133b1c4c67aSJohn Ogness * there may be other finalized records beyond that
2134b1c4c67aSJohn Ogness * need to be printed for a panic situation. If this
2135b1c4c67aSJohn Ogness * is the panic CPU, skip this
2136*c1aa3daaSDonghyeok Choe * non-existent/non-finalized record unless non-panic
2137*c1aa3daaSDonghyeok Choe * CPUs are still running and their debugging is
2138*c1aa3daaSDonghyeok Choe * explicitly enabled.
2139b1c4c67aSJohn Ogness *
2140b1c4c67aSJohn Ogness * Note that new messages printed on panic CPU are
2141b1c4c67aSJohn Ogness * finalized when we are here. The only exception
2142b1c4c67aSJohn Ogness * might be the last message without trailing newline.
2143b1c4c67aSJohn Ogness * But it would have the sequence number returned
2144b1c4c67aSJohn Ogness * by "prb_next_reserve_seq() - 1".
2145b1c4c67aSJohn Ogness */
2146*c1aa3daaSDonghyeok Choe if (this_cpu_in_panic() &&
2147*c1aa3daaSDonghyeok Choe (!debug_non_panic_cpus || legacy_allow_panic_sync) &&
2148*c1aa3daaSDonghyeok Choe ((*seq + 1) < prb_next_reserve_seq(rb))) {
2149b1c4c67aSJohn Ogness (*seq)++;
2150*c1aa3daaSDonghyeok Choe } else {
2151b6cf8b3fSJohn Ogness return false;
2152b6cf8b3fSJohn Ogness }
2153b6cf8b3fSJohn Ogness }
2154*c1aa3daaSDonghyeok Choe }
2155b6cf8b3fSJohn Ogness
2156b6cf8b3fSJohn Ogness return true;
2157b6cf8b3fSJohn Ogness }
2158b6cf8b3fSJohn Ogness
2159b6cf8b3fSJohn Ogness /**
2160b6cf8b3fSJohn Ogness * prb_read_valid() - Non-blocking read of a requested record or (if gone)
2161b6cf8b3fSJohn Ogness * the next available record.
2162b6cf8b3fSJohn Ogness *
2163b6cf8b3fSJohn Ogness * @rb: The ringbuffer to read from.
2164b6cf8b3fSJohn Ogness * @seq: The sequence number of the record to read.
2165b6cf8b3fSJohn Ogness * @r: A record data buffer to store the read record to.
2166b6cf8b3fSJohn Ogness *
2167b6cf8b3fSJohn Ogness * This is the public function available to readers to read a record.
2168b6cf8b3fSJohn Ogness *
2169f35efc78SJohn Ogness * The reader provides the @info and @text_buf buffers of @r to be
2170b6cf8b3fSJohn Ogness * filled in. Any of the buffer pointers can be set to NULL if the reader
2171b6cf8b3fSJohn Ogness * is not interested in that data. To ensure proper initialization of @r,
2172b6cf8b3fSJohn Ogness * prb_rec_init_rd() should be used.
2173b6cf8b3fSJohn Ogness *
2174b6cf8b3fSJohn Ogness * Context: Any context.
2175b6cf8b3fSJohn Ogness * Return: true if a record was read, otherwise false.
2176b6cf8b3fSJohn Ogness *
2177b6cf8b3fSJohn Ogness * On success, the reader must check r->info.seq to see which record was
2178b6cf8b3fSJohn Ogness * actually read. This allows the reader to detect dropped records.
2179b6cf8b3fSJohn Ogness *
2180584528d6SJohn Ogness * Failure means @seq refers to a record not yet available to the reader.
2181b6cf8b3fSJohn Ogness */
prb_read_valid(struct printk_ringbuffer * rb,u64 seq,struct printk_record * r)2182b6cf8b3fSJohn Ogness bool prb_read_valid(struct printk_ringbuffer *rb, u64 seq,
2183b6cf8b3fSJohn Ogness struct printk_record *r)
2184b6cf8b3fSJohn Ogness {
2185b6cf8b3fSJohn Ogness return _prb_read_valid(rb, &seq, r, NULL);
2186b6cf8b3fSJohn Ogness }
2187b6cf8b3fSJohn Ogness
2188b6cf8b3fSJohn Ogness /**
2189b6cf8b3fSJohn Ogness * prb_read_valid_info() - Non-blocking read of meta data for a requested
2190b6cf8b3fSJohn Ogness * record or (if gone) the next available record.
2191b6cf8b3fSJohn Ogness *
2192b6cf8b3fSJohn Ogness * @rb: The ringbuffer to read from.
2193b6cf8b3fSJohn Ogness * @seq: The sequence number of the record to read.
2194b6cf8b3fSJohn Ogness * @info: A buffer to store the read record meta data to.
2195b6cf8b3fSJohn Ogness * @line_count: A buffer to store the number of lines in the record text.
2196b6cf8b3fSJohn Ogness *
2197b6cf8b3fSJohn Ogness * This is the public function available to readers to read only the
2198b6cf8b3fSJohn Ogness * meta data of a record.
2199b6cf8b3fSJohn Ogness *
2200b6cf8b3fSJohn Ogness * The reader provides the @info, @line_count buffers to be filled in.
2201b6cf8b3fSJohn Ogness * Either of the buffer pointers can be set to NULL if the reader is not
2202b6cf8b3fSJohn Ogness * interested in that data.
2203b6cf8b3fSJohn Ogness *
2204b6cf8b3fSJohn Ogness * Context: Any context.
2205b6cf8b3fSJohn Ogness * Return: true if a record's meta data was read, otherwise false.
2206b6cf8b3fSJohn Ogness *
2207b6cf8b3fSJohn Ogness * On success, the reader must check info->seq to see which record meta data
2208b6cf8b3fSJohn Ogness * was actually read. This allows the reader to detect dropped records.
2209b6cf8b3fSJohn Ogness *
2210584528d6SJohn Ogness * Failure means @seq refers to a record not yet available to the reader.
2211b6cf8b3fSJohn Ogness */
prb_read_valid_info(struct printk_ringbuffer * rb,u64 seq,struct printk_info * info,unsigned int * line_count)2212b6cf8b3fSJohn Ogness bool prb_read_valid_info(struct printk_ringbuffer *rb, u64 seq,
2213b6cf8b3fSJohn Ogness struct printk_info *info, unsigned int *line_count)
2214b6cf8b3fSJohn Ogness {
2215b6cf8b3fSJohn Ogness struct printk_record r;
2216b6cf8b3fSJohn Ogness
2217f35efc78SJohn Ogness prb_rec_init_rd(&r, info, NULL, 0);
2218b6cf8b3fSJohn Ogness
2219b6cf8b3fSJohn Ogness return _prb_read_valid(rb, &seq, &r, line_count);
2220b6cf8b3fSJohn Ogness }
2221b6cf8b3fSJohn Ogness
2222b6cf8b3fSJohn Ogness /**
2223b6cf8b3fSJohn Ogness * prb_first_valid_seq() - Get the sequence number of the oldest available
2224b6cf8b3fSJohn Ogness * record.
2225b6cf8b3fSJohn Ogness *
2226b6cf8b3fSJohn Ogness * @rb: The ringbuffer to get the sequence number from.
2227b6cf8b3fSJohn Ogness *
2228b6cf8b3fSJohn Ogness * This is the public function available to readers to see what the
2229b6cf8b3fSJohn Ogness * first/oldest valid sequence number is.
2230b6cf8b3fSJohn Ogness *
2231b6cf8b3fSJohn Ogness * This provides readers a starting point to begin iterating the ringbuffer.
2232b6cf8b3fSJohn Ogness *
2233b6cf8b3fSJohn Ogness * Context: Any context.
2234b6cf8b3fSJohn Ogness * Return: The sequence number of the first/oldest record or, if the
2235b6cf8b3fSJohn Ogness * ringbuffer is empty, 0 is returned.
2236b6cf8b3fSJohn Ogness */
prb_first_valid_seq(struct printk_ringbuffer * rb)2237b6cf8b3fSJohn Ogness u64 prb_first_valid_seq(struct printk_ringbuffer *rb)
2238b6cf8b3fSJohn Ogness {
2239b6cf8b3fSJohn Ogness u64 seq = 0;
2240b6cf8b3fSJohn Ogness
2241b6cf8b3fSJohn Ogness if (!_prb_read_valid(rb, &seq, NULL, NULL))
2242b6cf8b3fSJohn Ogness return 0;
2243b6cf8b3fSJohn Ogness
2244b6cf8b3fSJohn Ogness return seq;
2245b6cf8b3fSJohn Ogness }
2246b6cf8b3fSJohn Ogness
2247b6cf8b3fSJohn Ogness /**
2248b6cf8b3fSJohn Ogness * prb_next_seq() - Get the sequence number after the last available record.
2249b6cf8b3fSJohn Ogness *
2250b6cf8b3fSJohn Ogness * @rb: The ringbuffer to get the sequence number from.
2251b6cf8b3fSJohn Ogness *
2252b6cf8b3fSJohn Ogness * This is the public function available to readers to see what the next
2253b6cf8b3fSJohn Ogness * newest sequence number available to readers will be.
2254b6cf8b3fSJohn Ogness *
2255b6cf8b3fSJohn Ogness * This provides readers a sequence number to jump to if all currently
22565f72e52bSJohn Ogness * available records should be skipped. It is guaranteed that all records
22575f72e52bSJohn Ogness * previous to the returned value have been finalized and are (or were)
22585f72e52bSJohn Ogness * available to the reader.
2259b6cf8b3fSJohn Ogness *
2260b6cf8b3fSJohn Ogness * Context: Any context.
2261b6cf8b3fSJohn Ogness * Return: The sequence number of the next newest (not yet available) record
2262b6cf8b3fSJohn Ogness * for readers.
2263b6cf8b3fSJohn Ogness */
prb_next_seq(struct printk_ringbuffer * rb)2264b6cf8b3fSJohn Ogness u64 prb_next_seq(struct printk_ringbuffer *rb)
2265b6cf8b3fSJohn Ogness {
2266f244b4dcSPetr Mladek u64 seq;
2267b6cf8b3fSJohn Ogness
22685f72e52bSJohn Ogness seq = desc_last_finalized_seq(rb);
2269f244b4dcSPetr Mladek
2270f244b4dcSPetr Mladek /*
2271f244b4dcSPetr Mladek * Begin searching after the last finalized record.
2272f244b4dcSPetr Mladek *
2273f244b4dcSPetr Mladek * On 0, the search must begin at 0 because of hack#2
2274f244b4dcSPetr Mladek * of the bootstrapping phase it is not known if a
2275f244b4dcSPetr Mladek * record at index 0 exists.
2276f244b4dcSPetr Mladek */
2277f244b4dcSPetr Mladek if (seq != 0)
2278f244b4dcSPetr Mladek seq++;
2279f244b4dcSPetr Mladek
2280f244b4dcSPetr Mladek /*
2281f244b4dcSPetr Mladek * The information about the last finalized @seq might be inaccurate.
2282f244b4dcSPetr Mladek * Search forward to find the current one.
2283f244b4dcSPetr Mladek */
2284b6cf8b3fSJohn Ogness while (_prb_read_valid(rb, &seq, NULL, NULL))
2285b6cf8b3fSJohn Ogness seq++;
2286b6cf8b3fSJohn Ogness
2287b6cf8b3fSJohn Ogness return seq;
2288b6cf8b3fSJohn Ogness }
2289b6cf8b3fSJohn Ogness
2290b6cf8b3fSJohn Ogness /**
2291b6cf8b3fSJohn Ogness * prb_init() - Initialize a ringbuffer to use provided external buffers.
2292b6cf8b3fSJohn Ogness *
2293b6cf8b3fSJohn Ogness * @rb: The ringbuffer to initialize.
2294b6cf8b3fSJohn Ogness * @text_buf: The data buffer for text data.
2295b6cf8b3fSJohn Ogness * @textbits: The size of @text_buf as a power-of-2 value.
2296b6cf8b3fSJohn Ogness * @descs: The descriptor buffer for ringbuffer records.
2297b6cf8b3fSJohn Ogness * @descbits: The count of @descs items as a power-of-2 value.
2298cfe2790bSJohn Ogness * @infos: The printk_info buffer for ringbuffer records.
2299b6cf8b3fSJohn Ogness *
2300b6cf8b3fSJohn Ogness * This is the public function available to writers to setup a ringbuffer
2301b6cf8b3fSJohn Ogness * during runtime using provided buffers.
2302b6cf8b3fSJohn Ogness *
2303b6cf8b3fSJohn Ogness * This must match the initialization of DEFINE_PRINTKRB().
2304b6cf8b3fSJohn Ogness *
2305b6cf8b3fSJohn Ogness * Context: Any context.
2306b6cf8b3fSJohn Ogness */
prb_init(struct printk_ringbuffer * rb,char * text_buf,unsigned int textbits,struct prb_desc * descs,unsigned int descbits,struct printk_info * infos)2307b6cf8b3fSJohn Ogness void prb_init(struct printk_ringbuffer *rb,
2308b6cf8b3fSJohn Ogness char *text_buf, unsigned int textbits,
2309cfe2790bSJohn Ogness struct prb_desc *descs, unsigned int descbits,
2310cfe2790bSJohn Ogness struct printk_info *infos)
2311b6cf8b3fSJohn Ogness {
2312b6cf8b3fSJohn Ogness memset(descs, 0, _DESCS_COUNT(descbits) * sizeof(descs[0]));
2313cfe2790bSJohn Ogness memset(infos, 0, _DESCS_COUNT(descbits) * sizeof(infos[0]));
2314b6cf8b3fSJohn Ogness
2315b6cf8b3fSJohn Ogness rb->desc_ring.count_bits = descbits;
2316b6cf8b3fSJohn Ogness rb->desc_ring.descs = descs;
2317cfe2790bSJohn Ogness rb->desc_ring.infos = infos;
2318b6cf8b3fSJohn Ogness atomic_long_set(&rb->desc_ring.head_id, DESC0_ID(descbits));
2319b6cf8b3fSJohn Ogness atomic_long_set(&rb->desc_ring.tail_id, DESC0_ID(descbits));
23205f72e52bSJohn Ogness atomic_long_set(&rb->desc_ring.last_finalized_seq, 0);
2321b6cf8b3fSJohn Ogness
2322b6cf8b3fSJohn Ogness rb->text_data_ring.size_bits = textbits;
2323b6cf8b3fSJohn Ogness rb->text_data_ring.data = text_buf;
2324b6cf8b3fSJohn Ogness atomic_long_set(&rb->text_data_ring.head_lpos, BLK0_LPOS(textbits));
2325b6cf8b3fSJohn Ogness atomic_long_set(&rb->text_data_ring.tail_lpos, BLK0_LPOS(textbits));
2326b6cf8b3fSJohn Ogness
2327b6cf8b3fSJohn Ogness atomic_long_set(&rb->fail, 0);
2328b6cf8b3fSJohn Ogness
2329b6cf8b3fSJohn Ogness atomic_long_set(&(descs[_DESCS_COUNT(descbits) - 1].state_var), DESC0_SV(descbits));
2330d397820fSJohn Ogness descs[_DESCS_COUNT(descbits) - 1].text_blk_lpos.begin = FAILED_LPOS;
2331d397820fSJohn Ogness descs[_DESCS_COUNT(descbits) - 1].text_blk_lpos.next = FAILED_LPOS;
2332cfe2790bSJohn Ogness
2333cfe2790bSJohn Ogness infos[0].seq = -(u64)_DESCS_COUNT(descbits);
2334cfe2790bSJohn Ogness infos[_DESCS_COUNT(descbits) - 1].seq = 0;
2335b6cf8b3fSJohn Ogness }
2336b6cf8b3fSJohn Ogness
2337b6cf8b3fSJohn Ogness /**
2338b6cf8b3fSJohn Ogness * prb_record_text_space() - Query the full actual used ringbuffer space for
2339b6cf8b3fSJohn Ogness * the text data of a reserved entry.
2340b6cf8b3fSJohn Ogness *
2341b6cf8b3fSJohn Ogness * @e: The successfully reserved entry to query.
2342b6cf8b3fSJohn Ogness *
2343b6cf8b3fSJohn Ogness * This is the public function available to writers to see how much actual
2344b6cf8b3fSJohn Ogness * space is used in the ringbuffer to store the text data of the specified
2345b6cf8b3fSJohn Ogness * entry.
2346b6cf8b3fSJohn Ogness *
2347b6cf8b3fSJohn Ogness * This function is only valid if @e has been successfully reserved using
2348b6cf8b3fSJohn Ogness * prb_reserve().
2349b6cf8b3fSJohn Ogness *
2350b6cf8b3fSJohn Ogness * Context: Any context.
2351b6cf8b3fSJohn Ogness * Return: The size in bytes used by the text data of the associated record.
2352b6cf8b3fSJohn Ogness */
prb_record_text_space(struct prb_reserved_entry * e)2353b6cf8b3fSJohn Ogness unsigned int prb_record_text_space(struct prb_reserved_entry *e)
2354b6cf8b3fSJohn Ogness {
2355b6cf8b3fSJohn Ogness return e->text_space;
2356b6cf8b3fSJohn Ogness }
2357