Skip to content

Commit ddb9baa

Browse files
sergey-senozhatskypmladek
authored andcommitted
printk: report lost messages in printk safe/nmi contexts
Account lost messages in pritk-safe and printk-safe-nmi contexts and report those numbers during printk_safe_flush(). The patch also moves lost message counter to struct `printk_safe_seq_buf' instead of having dedicated static counters - this simplifies the code. Link: http://lkml.kernel.org/r/[email protected] Cc: Andrew Morton <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: Jan Kara <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Calvin Owens <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Peter Hurley <[email protected]> Cc: [email protected] Signed-off-by: Sergey Senozhatsky <[email protected]> Signed-off-by: Petr Mladek <[email protected]>
1 parent 7acac34 commit ddb9baa

File tree

3 files changed

+28
-37
lines changed

3 files changed

+28
-37
lines changed

kernel/printk/internal.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,6 @@
1616
*/
1717
#include <linux/percpu.h>
1818

19-
#ifdef CONFIG_PRINTK_NMI
20-
21-
extern atomic_t nmi_message_lost;
22-
static inline int get_nmi_message_lost(void)
23-
{
24-
return atomic_xchg(&nmi_message_lost, 0);
25-
}
26-
27-
#else /* CONFIG_PRINTK_NMI */
28-
29-
static inline int get_nmi_message_lost(void)
30-
{
31-
return 0;
32-
}
33-
34-
#endif /* CONFIG_PRINTK_NMI */
35-
3619
#ifdef CONFIG_PRINTK
3720

3821
#define PRINTK_SAFE_CONTEXT_MASK 0x7fffffff

kernel/printk/printk.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,7 +1677,6 @@ asmlinkage int vprintk_emit(int facility, int level,
16771677
unsigned long flags;
16781678
int this_cpu;
16791679
int printed_len = 0;
1680-
int nmi_message_lost;
16811680
bool in_sched = false;
16821681
/* cpu currently holding logbuf_lock in this function */
16831682
static unsigned int logbuf_cpu = UINT_MAX;
@@ -1728,15 +1727,6 @@ asmlinkage int vprintk_emit(int facility, int level,
17281727
strlen(recursion_msg));
17291728
}
17301729

1731-
nmi_message_lost = get_nmi_message_lost();
1732-
if (unlikely(nmi_message_lost)) {
1733-
text_len = scnprintf(textbuf, sizeof(textbuf),
1734-
"BAD LUCK: lost %d message(s) from NMI context!",
1735-
nmi_message_lost);
1736-
printed_len += log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
1737-
NULL, 0, textbuf, text_len);
1738-
}
1739-
17401730
/*
17411731
* The printf needs to come first; we need the syslog
17421732
* prefix which might be passed-in as a parameter.

kernel/printk/printk_safe.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@
4040
* were handled or when IRQs are blocked.
4141
*/
4242
static int printk_safe_irq_ready;
43-
atomic_t nmi_message_lost;
4443

4544
#define SAFE_LOG_BUF_LEN ((1 << CONFIG_PRINTK_SAFE_LOG_BUF_SHIFT) - \
46-
sizeof(atomic_t) - sizeof(struct irq_work))
45+
sizeof(atomic_t) - \
46+
sizeof(atomic_t) - \
47+
sizeof(struct irq_work))
4748

4849
struct printk_safe_seq_buf {
4950
atomic_t len; /* length of written data */
51+
atomic_t message_lost;
5052
struct irq_work work; /* IRQ work that flushes the buffer */
5153
unsigned char buffer[SAFE_LOG_BUF_LEN];
5254
};
@@ -58,6 +60,16 @@ static DEFINE_PER_CPU(int, printk_context);
5860
static DEFINE_PER_CPU(struct printk_safe_seq_buf, nmi_print_seq);
5961
#endif
6062

63+
/* Get flushed in a more safe context. */
64+
static void queue_flush_work(struct printk_safe_seq_buf *s)
65+
{
66+
if (printk_safe_irq_ready) {
67+
/* Make sure that IRQ work is really initialized. */
68+
smp_rmb();
69+
irq_work_queue(&s->work);
70+
}
71+
}
72+
6173
/*
6274
* Add a message to per-CPU context-dependent buffer. NMI and printk-safe
6375
* have dedicated buffers, because otherwise printk-safe preempted by
@@ -79,7 +91,8 @@ static int printk_safe_log_store(struct printk_safe_seq_buf *s,
7991

8092
/* The trailing '\0' is not counted into len. */
8193
if (len >= sizeof(s->buffer) - 1) {
82-
atomic_inc(&nmi_message_lost);
94+
atomic_inc(&s->message_lost);
95+
queue_flush_work(s);
8396
return 0;
8497
}
8598

@@ -91,6 +104,8 @@ static int printk_safe_log_store(struct printk_safe_seq_buf *s,
91104
smp_rmb();
92105

93106
add = vscnprintf(s->buffer + len, sizeof(s->buffer) - len, fmt, args);
107+
if (!add)
108+
return 0;
94109

95110
/*
96111
* Do it once again if the buffer has been flushed in the meantime.
@@ -100,13 +115,7 @@ static int printk_safe_log_store(struct printk_safe_seq_buf *s,
100115
if (atomic_cmpxchg(&s->len, len, len + add) != len)
101116
goto again;
102117

103-
/* Get flushed in a more safe context. */
104-
if (add && printk_safe_irq_ready) {
105-
/* Make sure that IRQ work is really initialized. */
106-
smp_rmb();
107-
irq_work_queue(&s->work);
108-
}
109-
118+
queue_flush_work(s);
110119
return add;
111120
}
112121

@@ -168,6 +177,14 @@ static int printk_safe_flush_buffer(const char *start, size_t len)
168177
return len;
169178
}
170179

180+
static void report_message_lost(struct printk_safe_seq_buf *s)
181+
{
182+
int lost = atomic_xchg(&s->message_lost, 0);
183+
184+
if (lost)
185+
printk_deferred("Lost %d message(s)!\n", lost);
186+
}
187+
171188
/*
172189
* Flush data from the associated per-CPU buffer. The function
173190
* can be called either via IRQ work or independently.
@@ -225,6 +242,7 @@ static void __printk_safe_flush(struct irq_work *work)
225242
goto more;
226243

227244
out:
245+
report_message_lost(s);
228246
raw_spin_unlock_irqrestore(&read_lock, flags);
229247
}
230248

0 commit comments

Comments
 (0)