Skip to content

Commit e2ae715

Browse files
kaysieversgregkh
authored andcommitted
kmsg - kmsg_dump() use iterator to receive log buffer content
Provide an iterator to receive the log buffer content, and convert all kmsg_dump() users to it. The structured data in the kmsg buffer now contains binary data, which should no longer be copied verbatim to the kmsg_dump() users. The iterator should provide reliable access to the buffer data, and also supports proper log line-aware chunking of data while iterating. Signed-off-by: Kay Sievers <kay@vrfy.org> Tested-by: Tony Luck <tony.luck@intel.com> Reported-by: Anton Vorontsov <anton.vorontsov@linaro.org> Tested-by: Anton Vorontsov <anton.vorontsov@linaro.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 1bd289d commit e2ae715

6 files changed

Lines changed: 258 additions & 137 deletions

File tree

‎arch/powerpc/platforms/pseries/nvram.c‎

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ static const char *pseries_nvram_os_partitions[] = {
6868
};
6969

7070
static void oops_to_nvram(struct kmsg_dumper *dumper,
71-
enum kmsg_dump_reason reason,
72-
const char *old_msgs, unsigned long old_len,
73-
const char *new_msgs, unsigned long new_len);
71+
enum kmsg_dump_reason reason);
7472

7573
static struct kmsg_dumper nvram_kmsg_dumper = {
7674
.dump = oops_to_nvram
@@ -503,28 +501,6 @@ int __init pSeries_nvram_init(void)
503501
return 0;
504502
}
505503

506-
/*
507-
* Try to capture the last capture_len bytes of the printk buffer. Return
508-
* the amount actually captured.
509-
*/
510-
static size_t capture_last_msgs(const char *old_msgs, size_t old_len,
511-
const char *new_msgs, size_t new_len,
512-
char *captured, size_t capture_len)
513-
{
514-
if (new_len >= capture_len) {
515-
memcpy(captured, new_msgs + (new_len - capture_len),
516-
capture_len);
517-
return capture_len;
518-
} else {
519-
/* Grab the end of old_msgs. */
520-
size_t old_tail_len = min(old_len, capture_len - new_len);
521-
memcpy(captured, old_msgs + (old_len - old_tail_len),
522-
old_tail_len);
523-
memcpy(captured + old_tail_len, new_msgs, new_len);
524-
return old_tail_len + new_len;
525-
}
526-
}
527-
528504
/*
529505
* Are we using the ibm,rtas-log for oops/panic reports? And if so,
530506
* would logging this oops/panic overwrite an RTAS event that rtas_errd
@@ -541,27 +517,6 @@ static int clobbering_unread_rtas_event(void)
541517
NVRAM_RTAS_READ_TIMEOUT);
542518
}
543519

544-
/* Squeeze out each line's <n> severity prefix. */
545-
static size_t elide_severities(char *buf, size_t len)
546-
{
547-
char *in, *out, *buf_end = buf + len;
548-
/* Assume a <n> at the very beginning marks the start of a line. */
549-
int newline = 1;
550-
551-
in = out = buf;
552-
while (in < buf_end) {
553-
if (newline && in+3 <= buf_end &&
554-
*in == '<' && isdigit(in[1]) && in[2] == '>') {
555-
in += 3;
556-
newline = 0;
557-
} else {
558-
newline = (*in == '\n');
559-
*out++ = *in++;
560-
}
561-
}
562-
return out - buf;
563-
}
564-
565520
/* Derived from logfs_compress() */
566521
static int nvram_compress(const void *in, void *out, size_t inlen,
567522
size_t outlen)
@@ -619,9 +574,7 @@ static int zip_oops(size_t text_len)
619574
* partition. If that's too much, go back and capture uncompressed text.
620575
*/
621576
static void oops_to_nvram(struct kmsg_dumper *dumper,
622-
enum kmsg_dump_reason reason,
623-
const char *old_msgs, unsigned long old_len,
624-
const char *new_msgs, unsigned long new_len)
577+
enum kmsg_dump_reason reason)
625578
{
626579
static unsigned int oops_count = 0;
627580
static bool panicking = false;
@@ -660,14 +613,14 @@ static void oops_to_nvram(struct kmsg_dumper *dumper,
660613
return;
661614

662615
if (big_oops_buf) {
663-
text_len = capture_last_msgs(old_msgs, old_len,
664-
new_msgs, new_len, big_oops_buf, big_oops_buf_sz);
665-
text_len = elide_severities(big_oops_buf, text_len);
616+
kmsg_dump_get_buffer(dumper, false,
617+
big_oops_buf, big_oops_buf_sz, &text_len);
666618
rc = zip_oops(text_len);
667619
}
668620
if (rc != 0) {
669-
text_len = capture_last_msgs(old_msgs, old_len,
670-
new_msgs, new_len, oops_data, oops_data_sz);
621+
kmsg_dump_rewind(dumper);
622+
kmsg_dump_get_buffer(dumper, true,
623+
oops_data, oops_data_sz, &text_len);
671624
err_type = ERR_TYPE_KERNEL_PANIC;
672625
*oops_len = (u16) text_len;
673626
}

‎arch/x86/platform/mrst/early_printk_mrst.c‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,16 @@ static struct kmsg_dumper dw_dumper;
110110
static int dumper_registered;
111111

112112
static void dw_kmsg_dump(struct kmsg_dumper *dumper,
113-
enum kmsg_dump_reason reason,
114-
const char *s1, unsigned long l1,
115-
const char *s2, unsigned long l2)
113+
enum kmsg_dump_reason reason)
116114
{
117-
int i;
115+
static char line[1024];
116+
size_t len;
118117

119118
/* When run to this, we'd better re-init the HW */
120119
mrst_early_console_init();
121120

122-
for (i = 0; i < l1; i++)
123-
early_mrst_console.write(&early_mrst_console, s1 + i, 1);
124-
for (i = 0; i < l2; i++)
125-
early_mrst_console.write(&early_mrst_console, s2 + i, 1);
121+
while (kmsg_dump_get_line(dumper, true, line, sizeof(line), &len))
122+
early_mrst_console.write(&early_mrst_console, line, len);
126123
}
127124

128125
/* Set the ratio rate to 115200, 8n1, IRQ disabled */

‎drivers/mtd/mtdoops.c‎

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -304,32 +304,17 @@ static void find_next_position(struct mtdoops_context *cxt)
304304
}
305305

306306
static void mtdoops_do_dump(struct kmsg_dumper *dumper,
307-
enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
308-
const char *s2, unsigned long l2)
307+
enum kmsg_dump_reason reason)
309308
{
310309
struct mtdoops_context *cxt = container_of(dumper,
311310
struct mtdoops_context, dump);
312-
unsigned long s1_start, s2_start;
313-
unsigned long l1_cpy, l2_cpy;
314-
char *dst;
315-
316-
if (reason != KMSG_DUMP_OOPS &&
317-
reason != KMSG_DUMP_PANIC)
318-
return;
319311

320312
/* Only dump oopses if dump_oops is set */
321313
if (reason == KMSG_DUMP_OOPS && !dump_oops)
322314
return;
323315

324-
dst = cxt->oops_buf + MTDOOPS_HEADER_SIZE; /* Skip the header */
325-
l2_cpy = min(l2, record_size - MTDOOPS_HEADER_SIZE);
326-
l1_cpy = min(l1, record_size - MTDOOPS_HEADER_SIZE - l2_cpy);
327-
328-
s2_start = l2 - l2_cpy;
329-
s1_start = l1 - l1_cpy;
330-
331-
memcpy(dst, s1 + s1_start, l1_cpy);
332-
memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
316+
kmsg_dump_get_buffer(dumper, true, cxt->oops_buf + MTDOOPS_HEADER_SIZE,
317+
record_size - MTDOOPS_HEADER_SIZE, NULL);
333318

334319
/* Panics must be written immediately */
335320
if (reason != KMSG_DUMP_OOPS)
@@ -375,6 +360,7 @@ static void mtdoops_notify_add(struct mtd_info *mtd)
375360
return;
376361
}
377362

363+
cxt->dump.max_reason = KMSG_DUMP_OOPS;
378364
cxt->dump.dump = mtdoops_do_dump;
379365
err = kmsg_dump_register(&cxt->dump);
380366
if (err) {

‎fs/pstore/platform.c‎

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,15 @@ static const char *get_reason_str(enum kmsg_dump_reason reason)
9494
* as we can from the end of the buffer.
9595
*/
9696
static void pstore_dump(struct kmsg_dumper *dumper,
97-
enum kmsg_dump_reason reason,
98-
const char *s1, unsigned long l1,
99-
const char *s2, unsigned long l2)
97+
enum kmsg_dump_reason reason)
10098
{
101-
unsigned long s1_start, s2_start;
102-
unsigned long l1_cpy, l2_cpy;
103-
unsigned long size, total = 0;
104-
char *dst;
99+
unsigned long total = 0;
105100
const char *why;
106101
u64 id;
107-
int hsize, ret;
108102
unsigned int part = 1;
109103
unsigned long flags = 0;
110104
int is_locked = 0;
105+
int ret;
111106

112107
why = get_reason_str(reason);
113108

@@ -119,30 +114,25 @@ static void pstore_dump(struct kmsg_dumper *dumper,
119114
spin_lock_irqsave(&psinfo->buf_lock, flags);
120115
oopscount++;
121116
while (total < kmsg_bytes) {
117+
char *dst;
118+
unsigned long size;
119+
int hsize;
120+
size_t len;
121+
122122
dst = psinfo->buf;
123123
hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
124124
size = psinfo->bufsize - hsize;
125125
dst += hsize;
126126

127-
l2_cpy = min(l2, size);
128-
l1_cpy = min(l1, size - l2_cpy);
129-
130-
if (l1_cpy + l2_cpy == 0)
127+
if (!kmsg_dump_get_buffer(dumper, true, dst, size, &len))
131128
break;
132129

133-
s2_start = l2 - l2_cpy;
134-
s1_start = l1 - l1_cpy;
135-
136-
memcpy(dst, s1 + s1_start, l1_cpy);
137-
memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
138-
139130
ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
140-
hsize + l1_cpy + l2_cpy, psinfo);
131+
hsize + len, psinfo);
141132
if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
142133
pstore_new_entry = 1;
143-
l1 -= l1_cpy;
144-
l2 -= l2_cpy;
145-
total += l1_cpy + l2_cpy;
134+
135+
total += hsize + len;
146136
part++;
147137
}
148138
if (in_nmi()) {

‎include/linux/kmsg_dump.h‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* is passed to the kernel.
2222
*/
2323
enum kmsg_dump_reason {
24+
KMSG_DUMP_UNDEF,
2425
KMSG_DUMP_PANIC,
2526
KMSG_DUMP_OOPS,
2627
KMSG_DUMP_EMERG,
@@ -31,23 +32,37 @@ enum kmsg_dump_reason {
3132

3233
/**
3334
* struct kmsg_dumper - kernel crash message dumper structure
34-
* @dump: The callback which gets called on crashes. The buffer is passed
35-
* as two sections, where s1 (length l1) contains the older
36-
* messages and s2 (length l2) contains the newer.
3735
* @list: Entry in the dumper list (private)
36+
* @dump: Call into dumping code which will retrieve the data with
37+
* through the record iterator
38+
* @max_reason: filter for highest reason number that should be dumped
3839
* @registered: Flag that specifies if this is already registered
3940
*/
4041
struct kmsg_dumper {
41-
void (*dump)(struct kmsg_dumper *dumper, enum kmsg_dump_reason reason,
42-
const char *s1, unsigned long l1,
43-
const char *s2, unsigned long l2);
4442
struct list_head list;
45-
int registered;
43+
void (*dump)(struct kmsg_dumper *dumper, enum kmsg_dump_reason reason);
44+
enum kmsg_dump_reason max_reason;
45+
bool active;
46+
bool registered;
47+
48+
/* private state of the kmsg iterator */
49+
u32 cur_idx;
50+
u32 next_idx;
51+
u64 cur_seq;
52+
u64 next_seq;
4653
};
4754

4855
#ifdef CONFIG_PRINTK
4956
void kmsg_dump(enum kmsg_dump_reason reason);
5057

58+
bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
59+
char *line, size_t size, size_t *len);
60+
61+
bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
62+
char *buf, size_t size, size_t *len);
63+
64+
void kmsg_dump_rewind(struct kmsg_dumper *dumper);
65+
5166
int kmsg_dump_register(struct kmsg_dumper *dumper);
5267

5368
int kmsg_dump_unregister(struct kmsg_dumper *dumper);
@@ -56,6 +71,22 @@ static inline void kmsg_dump(enum kmsg_dump_reason reason)
5671
{
5772
}
5873

74+
bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
75+
const char *line, size_t size, size_t *len)
76+
{
77+
return false;
78+
}
79+
80+
bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
81+
char *buf, size_t size, size_t *len)
82+
{
83+
return false;
84+
}
85+
86+
void kmsg_dump_rewind(struct kmsg_dumper *dumper)
87+
{
88+
}
89+
5990
static inline int kmsg_dump_register(struct kmsg_dumper *dumper)
6091
{
6192
return -EINVAL;

0 commit comments

Comments
 (0)