aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/powerpc/platforms/pseries/nvram.c118
1 files changed, 115 insertions, 3 deletions
diff --git a/arch/powerpc/platforms/pseries/nvram.c b/arch/powerpc/platforms/pseries/nvram.c
index befb41bc4d37..419707b07248 100644
--- a/arch/powerpc/platforms/pseries/nvram.c
+++ b/arch/powerpc/platforms/pseries/nvram.c
@@ -16,6 +16,8 @@
16#include <linux/errno.h> 16#include <linux/errno.h>
17#include <linux/init.h> 17#include <linux/init.h>
18#include <linux/spinlock.h> 18#include <linux/spinlock.h>
19#include <linux/slab.h>
20#include <linux/kmsg_dump.h>
19#include <asm/uaccess.h> 21#include <asm/uaccess.h>
20#include <asm/nvram.h> 22#include <asm/nvram.h>
21#include <asm/rtas.h> 23#include <asm/rtas.h>
@@ -39,7 +41,7 @@ struct nvram_os_partition {
39 const char *name; 41 const char *name;
40 int req_size; /* desired size, in bytes */ 42 int req_size; /* desired size, in bytes */
41 int min_size; /* minimum acceptable size (0 means req_size) */ 43 int min_size; /* minimum acceptable size (0 means req_size) */
42 long size; /* size of data portion of partition */ 44 long size; /* size of data portion (excluding err_log_info) */
43 long index; /* offset of data portion of partition */ 45 long index; /* offset of data portion of partition */
44}; 46};
45 47
@@ -50,11 +52,35 @@ static struct nvram_os_partition rtas_log_partition = {
50 .index = -1 52 .index = -1
51}; 53};
52 54
55static struct nvram_os_partition oops_log_partition = {
56 .name = "lnx,oops-log",
57 .req_size = 4000,
58 .min_size = 2000,
59 .index = -1
60};
61
53static const char *pseries_nvram_os_partitions[] = { 62static const char *pseries_nvram_os_partitions[] = {
54 "ibm,rtas-log", 63 "ibm,rtas-log",
64 "lnx,oops-log",
55 NULL 65 NULL
56}; 66};
57 67
68static void oops_to_nvram(struct kmsg_dumper *dumper,
69 enum kmsg_dump_reason reason,
70 const char *old_msgs, unsigned long old_len,
71 const char *new_msgs, unsigned long new_len);
72
73static struct kmsg_dumper nvram_kmsg_dumper = {
74 .dump = oops_to_nvram
75};
76
77/* See clobbering_unread_rtas_event() */
78#define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */
79static unsigned long last_unread_rtas_event; /* timestamp */
80
81/* We preallocate oops_buf during init to avoid kmalloc during oops/panic. */
82static char *oops_buf;
83
58static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index) 84static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
59{ 85{
60 unsigned int i; 86 unsigned int i;
@@ -214,8 +240,11 @@ int nvram_write_os_partition(struct nvram_os_partition *part, char * buff,
214int nvram_write_error_log(char * buff, int length, 240int nvram_write_error_log(char * buff, int length,
215 unsigned int err_type, unsigned int error_log_cnt) 241 unsigned int err_type, unsigned int error_log_cnt)
216{ 242{
217 return nvram_write_os_partition(&rtas_log_partition, buff, length, 243 int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
218 err_type, error_log_cnt); 244 err_type, error_log_cnt);
245 if (!rc)
246 last_unread_rtas_event = get_seconds();
247 return rc;
219} 248}
220 249
221/* nvram_read_error_log 250/* nvram_read_error_log
@@ -274,6 +303,7 @@ int nvram_clear_error_log(void)
274 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc); 303 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
275 return rc; 304 return rc;
276 } 305 }
306 last_unread_rtas_event = 0;
277 307
278 return 0; 308 return 0;
279} 309}
@@ -342,9 +372,35 @@ static int __init pseries_nvram_init_os_partition(struct nvram_os_partition
342 return 0; 372 return 0;
343} 373}
344 374
375static void __init nvram_init_oops_partition(int rtas_partition_exists)
376{
377 int rc;
378
379 rc = pseries_nvram_init_os_partition(&oops_log_partition);
380 if (rc != 0) {
381 if (!rtas_partition_exists)
382 return;
383 pr_notice("nvram: Using %s partition to log both"
384 " RTAS errors and oops/panic reports\n",
385 rtas_log_partition.name);
386 memcpy(&oops_log_partition, &rtas_log_partition,
387 sizeof(rtas_log_partition));
388 }
389 oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
390 rc = kmsg_dump_register(&nvram_kmsg_dumper);
391 if (rc != 0) {
392 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
393 kfree(oops_buf);
394 return;
395 }
396}
397
345static int __init pseries_nvram_init_log_partitions(void) 398static int __init pseries_nvram_init_log_partitions(void)
346{ 399{
347 (void) pseries_nvram_init_os_partition(&rtas_log_partition); 400 int rc;
401
402 rc = pseries_nvram_init_os_partition(&rtas_log_partition);
403 nvram_init_oops_partition(rc == 0);
348 return 0; 404 return 0;
349} 405}
350machine_arch_initcall(pseries, pseries_nvram_init_log_partitions); 406machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
@@ -378,3 +434,59 @@ int __init pSeries_nvram_init(void)
378 434
379 return 0; 435 return 0;
380} 436}
437
438/*
439 * Try to capture the last capture_len bytes of the printk buffer. Return
440 * the amount actually captured.
441 */
442static size_t capture_last_msgs(const char *old_msgs, size_t old_len,
443 const char *new_msgs, size_t new_len,
444 char *captured, size_t capture_len)
445{
446 if (new_len >= capture_len) {
447 memcpy(captured, new_msgs + (new_len - capture_len),
448 capture_len);
449 return capture_len;
450 } else {
451 /* Grab the end of old_msgs. */
452 size_t old_tail_len = min(old_len, capture_len - new_len);
453 memcpy(captured, old_msgs + (old_len - old_tail_len),
454 old_tail_len);
455 memcpy(captured + old_tail_len, new_msgs, new_len);
456 return old_tail_len + new_len;
457 }
458}
459
460/*
461 * Are we using the ibm,rtas-log for oops/panic reports? And if so,
462 * would logging this oops/panic overwrite an RTAS event that rtas_errd
463 * hasn't had a chance to read and process? Return 1 if so, else 0.
464 *
465 * We assume that if rtas_errd hasn't read the RTAS event in
466 * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.
467 */
468static int clobbering_unread_rtas_event(void)
469{
470 return (oops_log_partition.index == rtas_log_partition.index
471 && last_unread_rtas_event
472 && get_seconds() - last_unread_rtas_event <=
473 NVRAM_RTAS_READ_TIMEOUT);
474}
475
476/* our kmsg_dump callback */
477static void oops_to_nvram(struct kmsg_dumper *dumper,
478 enum kmsg_dump_reason reason,
479 const char *old_msgs, unsigned long old_len,
480 const char *new_msgs, unsigned long new_len)
481{
482 static unsigned int oops_count = 0;
483 size_t text_len;
484
485 if (clobbering_unread_rtas_event())
486 return;
487
488 text_len = capture_last_msgs(old_msgs, old_len, new_msgs, new_len,
489 oops_buf, oops_log_partition.size);
490 (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
491 (int) text_len, ERR_TYPE_KERNEL_PANIC, ++oops_count);
492}