Skip to content

Commit ad56ebd

Browse files
KAGA-KOKOpmladek
authored andcommitted
printk: nbcon: Add sequence handling
Add an atomic_long_t field @nbcon_seq to the console struct to store the sequence number for nbcon consoles. For nbcon consoles this will be used instead of the non-atomic @seq field. The new field allows for safe atomic sequence number updates without requiring any locking. On 64bit systems the new field stores the full sequence number. On 32bit systems the new field stores the lower 32 bits of the sequence number, which are expanded to 64bit as needed by folding the values based on the sequence numbers available in the ringbuffer. For 32bit systems, having a 32bit representation in the console is sufficient. If a console ever gets more than 2^31 records behind the ringbuffer then this is the least of the problems. Co-developed-by: John Ogness <john.ogness@linutronix.de> Signed-off-by: John Ogness <john.ogness@linutronix.de> Signed-off-by: Thomas Gleixner (Intel) <tglx@linutronix.de> Signed-off-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20230916192007.608398-7-john.ogness@linutronix.de
1 parent 4b08d9e commit ad56ebd

File tree

4 files changed

+136
-7
lines changed

4 files changed

+136
-7
lines changed

include/linux/console.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ struct printk_buffers;
243243
* might cause a system freeze when the console
244244
* is used later.
245245
* @pbufs: Pointer to the text buffer for this context
246+
* @seq: The sequence number to print for this context
246247
*/
247248
struct nbcon_context {
248249
/* members set by caller */
@@ -253,6 +254,7 @@ struct nbcon_context {
253254

254255
/* members set by acquire */
255256
struct printk_buffers *pbufs;
257+
u64 seq;
256258
};
257259

258260
/**
@@ -276,6 +278,7 @@ struct nbcon_context {
276278
* @node: hlist node for the console list
277279
*
278280
* @nbcon_state: State for nbcon consoles
281+
* @nbcon_seq: Sequence number of the next record for nbcon to print
279282
* @pbufs: Pointer to nbcon private buffer
280283
*/
281284
struct console {
@@ -299,6 +302,7 @@ struct console {
299302

300303
/* nbcon console specific members */
301304
atomic_t __private nbcon_state;
305+
atomic_long_t __private nbcon_seq;
302306
struct printk_buffers *pbufs;
303307
};
304308

kernel/printk/internal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
#include <linux/percpu.h>
66
#include <linux/console.h>
7+
#include "printk_ringbuffer.h"
78

89
#if defined(CONFIG_PRINTK) && defined(CONFIG_SYSCTL)
910
void __init printk_sysctl_init(void);
@@ -42,6 +43,8 @@ enum printk_info_flags {
4243
LOG_CONT = 8, /* text is a fragment of a continuation line */
4344
};
4445

46+
extern struct printk_ringbuffer *prb;
47+
4548
__printf(4, 0)
4649
int vprintk_store(int facility, int level,
4750
const struct dev_printk_info *dev_info,
@@ -69,6 +72,8 @@ void defer_console_output(void);
6972
u16 printk_parse_prefix(const char *text, int *level,
7073
enum printk_info_flags *flags);
7174

75+
u64 nbcon_seq_read(struct console *con);
76+
void nbcon_seq_force(struct console *con, u64 seq);
7277
bool nbcon_alloc(struct console *con);
7378
void nbcon_init(struct console *con);
7479
void nbcon_free(struct console *con);
@@ -88,6 +93,8 @@ void nbcon_free(struct console *con);
8893
#define printk_safe_exit_irqrestore(flags) local_irq_restore(flags)
8994

9095
static inline bool printk_percpu_data_ready(void) { return false; }
96+
static inline u64 nbcon_seq_read(struct console *con) { return 0; }
97+
static inline void nbcon_seq_force(struct console *con, u64 seq) { }
9198
static inline bool nbcon_alloc(struct console *con) { return false; }
9299
static inline void nbcon_init(struct console *con) { }
93100
static inline void nbcon_free(struct console *con) { }

kernel/printk/nbcon.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,101 @@ static inline bool nbcon_state_try_cmpxchg(struct console *con, struct nbcon_sta
140140
return atomic_try_cmpxchg(&ACCESS_PRIVATE(con, nbcon_state), &cur->atom, new->atom);
141141
}
142142

143+
#ifdef CONFIG_64BIT
144+
145+
#define __seq_to_nbcon_seq(seq) (seq)
146+
#define __nbcon_seq_to_seq(seq) (seq)
147+
148+
#else /* CONFIG_64BIT */
149+
150+
#define __seq_to_nbcon_seq(seq) ((u32)seq)
151+
152+
static inline u64 __nbcon_seq_to_seq(u32 nbcon_seq)
153+
{
154+
u64 seq;
155+
u64 rb_next_seq;
156+
157+
/*
158+
* The provided sequence is only the lower 32 bits of the ringbuffer
159+
* sequence. It needs to be expanded to 64bit. Get the next sequence
160+
* number from the ringbuffer and fold it.
161+
*
162+
* Having a 32bit representation in the console is sufficient.
163+
* If a console ever gets more than 2^31 records behind
164+
* the ringbuffer then this is the least of the problems.
165+
*
166+
* Also the access to the ring buffer is always safe.
167+
*/
168+
rb_next_seq = prb_next_seq(prb);
169+
seq = rb_next_seq - ((u32)rb_next_seq - nbcon_seq);
170+
171+
return seq;
172+
}
173+
174+
#endif /* CONFIG_64BIT */
175+
176+
/**
177+
* nbcon_seq_read - Read the current console sequence
178+
* @con: Console to read the sequence of
179+
*
180+
* Return: Sequence number of the next record to print on @con.
181+
*/
182+
u64 nbcon_seq_read(struct console *con)
183+
{
184+
unsigned long nbcon_seq = atomic_long_read(&ACCESS_PRIVATE(con, nbcon_seq));
185+
186+
return __nbcon_seq_to_seq(nbcon_seq);
187+
}
188+
189+
/**
190+
* nbcon_seq_force - Force console sequence to a specific value
191+
* @con: Console to work on
192+
* @seq: Sequence number value to set
193+
*
194+
* Only to be used during init (before registration) or in extreme situations
195+
* (such as panic with CONSOLE_REPLAY_ALL).
196+
*/
197+
void nbcon_seq_force(struct console *con, u64 seq)
198+
{
199+
/*
200+
* If the specified record no longer exists, the oldest available record
201+
* is chosen. This is especially important on 32bit systems because only
202+
* the lower 32 bits of the sequence number are stored. The upper 32 bits
203+
* are derived from the sequence numbers available in the ringbuffer.
204+
*/
205+
u64 valid_seq = max_t(u64, seq, prb_first_valid_seq(prb));
206+
207+
atomic_long_set(&ACCESS_PRIVATE(con, nbcon_seq), __seq_to_nbcon_seq(valid_seq));
208+
209+
/* Clear con->seq since nbcon consoles use con->nbcon_seq instead. */
210+
con->seq = 0;
211+
}
212+
213+
/**
214+
* nbcon_seq_try_update - Try to update the console sequence number
215+
* @ctxt: Pointer to an acquire context that contains
216+
* all information about the acquire mode
217+
* @new_seq: The new sequence number to set
218+
*
219+
* @ctxt->seq is updated to the new value of @con::nbcon_seq (expanded to
220+
* the 64bit value). This could be a different value than @new_seq if
221+
* nbcon_seq_force() was used or the current context no longer owns the
222+
* console. In the later case, it will stop printing anyway.
223+
*/
224+
__maybe_unused
225+
static void nbcon_seq_try_update(struct nbcon_context *ctxt, u64 new_seq)
226+
{
227+
unsigned long nbcon_seq = __seq_to_nbcon_seq(ctxt->seq);
228+
struct console *con = ctxt->console;
229+
230+
if (atomic_long_try_cmpxchg(&ACCESS_PRIVATE(con, nbcon_seq), &nbcon_seq,
231+
__seq_to_nbcon_seq(new_seq))) {
232+
ctxt->seq = new_seq;
233+
} else {
234+
ctxt->seq = nbcon_seq_read(con);
235+
}
236+
}
237+
143238
/**
144239
* nbcon_context_try_acquire_direct - Try to acquire directly
145240
* @ctxt: The context of the caller
@@ -510,6 +605,9 @@ static bool nbcon_context_try_acquire(struct nbcon_context *ctxt)
510605
else
511606
ctxt->pbufs = con->pbufs;
512607

608+
/* Set the record sequence for this context to print. */
609+
ctxt->seq = nbcon_seq_read(ctxt->console);
610+
513611
return true;
514612
}
515613

@@ -722,6 +820,8 @@ bool nbcon_alloc(struct console *con)
722820
*
723821
* nbcon_alloc() *must* be called and succeed before this function
724822
* is called.
823+
*
824+
* This function expects that the legacy @con->seq has been set.
725825
*/
726826
void nbcon_init(struct console *con)
727827
{
@@ -730,6 +830,7 @@ void nbcon_init(struct console *con)
730830
/* nbcon_alloc() must have been called and successful! */
731831
BUG_ON(!con->pbufs);
732832

833+
nbcon_seq_force(con, con->seq);
733834
nbcon_state_set(con, &state);
734835
}
735836

kernel/printk/printk.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ _DEFINE_PRINTKRB(printk_rb_static, CONFIG_LOG_BUF_SHIFT - PRB_AVGBITS,
494494

495495
static struct printk_ringbuffer printk_rb_dynamic;
496496

497-
static struct printk_ringbuffer *prb = &printk_rb_static;
497+
struct printk_ringbuffer *prb = &printk_rb_static;
498498

499499
/*
500500
* We cannot access per-CPU data (e.g. per-CPU flush irq_work) before
@@ -3168,18 +3168,25 @@ void console_flush_on_panic(enum con_flush_mode mode)
31683168

31693169
if (mode == CONSOLE_REPLAY_ALL) {
31703170
struct console *c;
3171+
short flags;
31713172
int cookie;
31723173
u64 seq;
31733174

31743175
seq = prb_first_valid_seq(prb);
31753176

31763177
cookie = console_srcu_read_lock();
31773178
for_each_console_srcu(c) {
3178-
/*
3179-
* This is an unsynchronized assignment, but the
3180-
* kernel is in "hope and pray" mode anyway.
3181-
*/
3182-
c->seq = seq;
3179+
flags = console_srcu_read_flags(c);
3180+
3181+
if (flags & CON_NBCON) {
3182+
nbcon_seq_force(c, seq);
3183+
} else {
3184+
/*
3185+
* This is an unsynchronized assignment. On
3186+
* panic legacy consoles are only best effort.
3187+
*/
3188+
c->seq = seq;
3189+
}
31833190
}
31843191
console_srcu_read_unlock(cookie);
31853192
}
@@ -3750,6 +3757,7 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
37503757
struct console *c;
37513758
u64 last_diff = 0;
37523759
u64 printk_seq;
3760+
short flags;
37533761
int cookie;
37543762
u64 diff;
37553763
u64 seq;
@@ -3771,14 +3779,23 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
37713779
for_each_console_srcu(c) {
37723780
if (con && con != c)
37733781
continue;
3782+
3783+
flags = console_srcu_read_flags(c);
3784+
37743785
/*
37753786
* If consoles are not usable, it cannot be expected
37763787
* that they make forward progress, so only increment
37773788
* @diff for usable consoles.
37783789
*/
37793790
if (!console_is_usable(c))
37803791
continue;
3781-
printk_seq = c->seq;
3792+
3793+
if (flags & CON_NBCON) {
3794+
printk_seq = nbcon_seq_read(c);
3795+
} else {
3796+
printk_seq = c->seq;
3797+
}
3798+
37823799
if (printk_seq < seq)
37833800
diff += seq - printk_seq;
37843801
}

0 commit comments

Comments
 (0)