From f405463404a1ddb2cb8923f7fb4273920fcf0e0c Mon Sep 17 00:00:00 2001 From: roger Date: Mon, 7 Mar 2022 19:25:10 +0800 Subject: [PATCH] blackbox: enable blackbox storaging fault log with pstore/ramoops ohos inclusion category:feature issue: #I4WQEN CVE: NA ------------------------------- 1.enable bbox storaging fault log with pstore/ramoops 2.provide the same ability for pstore/ramoops with pstore/blk Signed-off-by: roger --- drivers/staging/blackbox/blackbox_common.c | 1 + drivers/staging/blackbox/blackbox_core.c | 6 +++ drivers/staging/blackbox/blackbox_storage.c | 37 +++++---------- fs/pstore/blk.c | 4 +- fs/pstore/internal.h | 2 +- fs/pstore/platform.c | 18 +++---- fs/pstore/ram.c | 52 +++++++++++++++++++-- include/linux/blackbox.h | 2 +- include/linux/pstore_ram.h | 1 + 9 files changed, 80 insertions(+), 43 deletions(-) diff --git a/drivers/staging/blackbox/blackbox_common.c b/drivers/staging/blackbox/blackbox_common.c index 7f658f9860ad..8416517eb583 100644 --- a/drivers/staging/blackbox/blackbox_common.c +++ b/drivers/staging/blackbox/blackbox_common.c @@ -178,6 +178,7 @@ void get_timestamp(char *buf, size_t buf_size) tm.tm_hour, tm.tm_min, tm.tm_sec, get_ticks()); buf[buf_size - 1] = '\0'; } +EXPORT_SYMBOL_GPL(get_timestamp); unsigned long long get_ticks(void) { diff --git a/drivers/staging/blackbox/blackbox_core.c b/drivers/staging/blackbox/blackbox_core.c index ee00a80997df..2a1a62781f8c 100644 --- a/drivers/staging/blackbox/blackbox_core.c +++ b/drivers/staging/blackbox/blackbox_core.c @@ -16,6 +16,9 @@ #include #include #include +#ifdef CONFIG_DFX_ZEROHUNG +#include +#endif #include #include @@ -221,6 +224,9 @@ static void save_history_log(const char *log_root_dir, struct error_info *info, get_top_category(info->module, info->event), info->module, get_category(info->module, info->event), info->event, timestamp, need_sys_reset ? "true" : "false", info->error_desc); +#ifdef CONFIG_DFX_ZEROHUNG + zrhung_send_event("KERNEL_VENDOR", "PANIC", info->error_desc); +#endif memset(history_log_path, 0, sizeof(history_log_path)); scnprintf(history_log_path, sizeof(history_log_path) - 1, "%s/%s", log_root_dir, HISTORY_LOG_NAME); diff --git a/drivers/staging/blackbox/blackbox_storage.c b/drivers/staging/blackbox/blackbox_storage.c index 3f11c5f43dec..635e3aa036ab 100644 --- a/drivers/staging/blackbox/blackbox_storage.c +++ b/drivers/staging/blackbox/blackbox_storage.c @@ -61,26 +61,13 @@ static void do_kmsg_dump(struct kmsg_dumper *dumper, } #endif -#if IS_ENABLED(CONFIG_DEF_BLACKBOX_STORAGE_BY_PSTORE_BLK) +#if defined(CONFIG_DEF_BLACKBOX_STORAGE_BY_PSTORE_BLK) || \ + defined(CONFIG_DEF_BLACKBOX_STORAGE_BY_PSTORE_RAM) #define LOG_FILE_WAIT_TIME 1000 /* unit: ms */ #define RETRY_MAX_COUNT 10 #define PSTORE_MOUNT_POINT "/sys/fs/pstore/" #define FILE_LIMIT (0660) -#if __BITS_PER_LONG == 64 -#define sys_lstat sys_newlstat -#else -#define sys_lstat sys_lstat64 -#endif - -struct sys_st { -#if __BITS_PER_LONG == 64 - struct stat __st; -#else - struct stat64 __st; -#endif -}; - static bool is_pstore_part_ready(char *pstore_file) { const char *cur_name = NULL; @@ -88,8 +75,7 @@ static bool is_pstore_part_ready(char *pstore_file) struct dentry *cur_dentry; struct file *filp = NULL; char *full_path = NULL; - struct sys_st st; - int ret = -1; + bool is_ready = false; if (unlikely(!pstore_file)) return -EINVAL; @@ -111,27 +97,26 @@ static bool is_pstore_part_ready(char *pstore_file) memset(full_path, 0, PATH_MAX_LEN); snprintf(full_path, PATH_MAX_LEN - 1, "%s%s", PSTORE_MOUNT_POINT, cur_name); - memset((void *)&st, 0, sizeof(struct sys_st)); - ret = sys_lstat(full_path, &st.__st); - if (!ret && (S_ISREG(st.__st.st_mode)) && - (strncmp(cur_name, "blackbox", strlen("blackbox")) == 0)) { + if (S_ISREG(d_inode(cur_dentry)->i_mode) && !strncmp(cur_name, "blackbox", + strlen("blackbox"))) { + is_ready = true; if (strcmp(full_path, pstore_file) > 0) strncpy(pstore_file, full_path, strlen(full_path)); } } - if (strlen(pstore_file)) + if (is_ready && strlen(pstore_file)) bbox_print_info("get pstore file name %s successfully!\n", pstore_file); __out: file_close(filp); vfree(full_path); - return ret == 0; + return is_ready; } -static int get_log_by_pstore_blk(void *in, unsigned int inlen) +static int get_log_by_pstore(void *in, unsigned int inlen) { char pstore_file[PATH_MAX_LEN]; struct file *filp = NULL; @@ -196,13 +181,15 @@ const struct reboot_crashlog_storage storage_lastwords[] = { #endif #if IS_ENABLED(CONFIG_DEF_BLACKBOX_STORAGE_BY_PSTORE_BLK) { - .get_log = get_log_by_pstore_blk, + .get_log = get_log_by_pstore, .blackbox_dump = pstore_blackbox_dump, .material = "pstore_blk", }, #endif #if IS_ENABLED(CONFIG_DEF_BLACKBOX_STORAGE_BY_PSTORE_RAM) { + .get_log = get_log_by_pstore, + .blackbox_dump = pstore_blackbox_dump, .material = "pstore_ram", }, #endif diff --git a/fs/pstore/blk.c b/fs/pstore/blk.c index b7a290482840..f698b3fad527 100644 --- a/fs/pstore/blk.c +++ b/fs/pstore/blk.c @@ -53,7 +53,7 @@ MODULE_PARM_DESC(ftrace_size, "ftrace size in kbytes"); #if IS_ENABLED(CONFIG_PSTORE_BLACKBOX) static long blackbox_size = CONFIG_PSTORE_BLK_BLACKBOX_SIZE; -bool pstore_blk_ready; +bool pstore_ready; #else static long blackbox_size = -1; #endif @@ -504,7 +504,7 @@ static int __init pstore_blk_init(void) #if IS_ENABLED(CONFIG_PSTORE_BLACKBOX) if (best_effort && blkdev[0]) - pstore_blk_ready = true; + pstore_ready = true; #endif return ret; diff --git a/fs/pstore/internal.h b/fs/pstore/internal.h index dec9d17a5d94..2ef88b2c60c3 100644 --- a/fs/pstore/internal.h +++ b/fs/pstore/internal.h @@ -51,6 +51,6 @@ int __init pstore_init_fs(void); void __exit pstore_exit_fs(void); #ifdef CONFIG_PSTORE_BLACKBOX -extern bool pstore_blk_ready; /* flag which pstore_blk is ready */ +extern bool pstore_ready; /* flag which pstore_blk is ready */ #endif #endif diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c index 577711b621fe..b86a2e486568 100644 --- a/fs/pstore/platform.c +++ b/fs/pstore/platform.c @@ -389,7 +389,6 @@ void pstore_record_init(struct pstore_record *record, #ifdef CONFIG_PSTORE_BLACKBOX #define PSTORE_FLAG "PSTORE" #define CALLSTACK_MAX_ENTRIES 20 -#if defined(CONFIG_PSTORE_BLK) static void dump_stacktrace(char *pbuf, size_t buf_size, bool is_panic) { int i; @@ -428,12 +427,9 @@ static void dump_stacktrace(char *pbuf, size_t buf_size, bool is_panic) memcpy(pbuf + com_len, tmp_buf, stack_len); *(pbuf + buf_size - 1) = '\0'; } -#endif -void pstore_blackbox_dump(struct kmsg_dumper *dumper, - enum kmsg_dump_reason reason) +void pstore_blackbox_dump(struct kmsg_dumper *dumper, enum kmsg_dump_reason reason) { -#if defined(CONFIG_PSTORE_BLK) struct fault_log_info *pfault_log_info; struct pstore_record record; size_t dst_size; @@ -441,8 +437,10 @@ void pstore_blackbox_dump(struct kmsg_dumper *dumper, char *dst; int ret; - if (!pstore_blk_ready) +#if defined(CONFIG_PSTORE_BLK) || defined(CONFIG_PSTORE_RAM) + if (!pstore_ready) return; +#endif why = kmsg_dump_reason_str(reason); @@ -450,7 +448,7 @@ void pstore_blackbox_dump(struct kmsg_dumper *dumper, /* Failed to acquire lock: give up if we cannot wait. */ if (pstore_cannot_wait(reason)) { pr_err("dump skipped in %s path: may corrupt error record\n", - in_nmi() ? "NMI" : why); + in_nmi() ? "NMI" : why); return; } if (down_interruptible(&psinfo->buf_lock)) { @@ -483,15 +481,13 @@ void pstore_blackbox_dump(struct kmsg_dumper *dumper, dst_size -= sizeof(struct fault_log_info); - (void)kmsg_dump_get_buffer(dumper, true, dst + sizeof(struct fault_log_info), - dst_size, &(pfault_log_info->len)); + (void)kmsg_dump_get_buffer(dumper, true, dst + sizeof(struct fault_log_info), dst_size, + &(pfault_log_info->len)); record.size = sizeof(struct fault_log_info) + pfault_log_info->len; ret = psinfo->write(&record); up(&psinfo->buf_lock); - -#endif } EXPORT_SYMBOL_GPL(pstore_blackbox_dump); #endif diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c index ca6d8a867285..35d774e4452a 100644 --- a/fs/pstore/ram.c +++ b/fs/pstore/ram.c @@ -43,6 +43,14 @@ static ulong ramoops_pmsg_size = MIN_MEM_SIZE; module_param_named(pmsg_size, ramoops_pmsg_size, ulong, 0400); MODULE_PARM_DESC(pmsg_size, "size of user space message log"); +static ulong ramoops_blackbox_size = MIN_MEM_SIZE; +module_param_named(blackbox_size, ramoops_blackbox_size, ulong, 0400); +MODULE_PARM_DESC(blackbox_size, "size of blackbox log"); +#if IS_ENABLED(CONFIG_PSTORE_BLACKBOX) +bool pstore_ready; +#endif + + static unsigned long long mem_address; module_param_hw(mem_address, ullong, other, 0400); MODULE_PARM_DESC(mem_address, @@ -80,6 +88,7 @@ struct ramoops_context { struct persistent_ram_zone *cprz; /* Console zone */ struct persistent_ram_zone **fprzs; /* Ftrace zones */ struct persistent_ram_zone *mprz; /* PMSG zone */ + struct persistent_ram_zone *bprz; /* BLACKBOX zone */ phys_addr_t phys_addr; unsigned long size; unsigned int memtype; @@ -87,6 +96,7 @@ struct ramoops_context { size_t console_size; size_t ftrace_size; size_t pmsg_size; + size_t blackbox_size; u32 flags; struct persistent_ram_ecc_info ecc_info; unsigned int max_dump_cnt; @@ -97,6 +107,7 @@ struct ramoops_context { unsigned int max_ftrace_cnt; unsigned int ftrace_read_cnt; unsigned int pmsg_read_cnt; + unsigned int blackbox_read_cnt; struct pstore_info pstore; }; @@ -110,6 +121,7 @@ static int ramoops_pstore_open(struct pstore_info *psi) cxt->console_read_cnt = 0; cxt->ftrace_read_cnt = 0; cxt->pmsg_read_cnt = 0; + cxt->blackbox_read_cnt = 0; return 0; } @@ -213,6 +225,9 @@ static ssize_t ramoops_pstore_read(struct pstore_record *record) if (!prz_ok(prz) && !cxt->pmsg_read_cnt++) prz = ramoops_get_next_prz(&cxt->mprz, 0 /* single */, record); + if (!prz_ok(prz) && !cxt->blackbox_read_cnt++) + prz = ramoops_get_next_prz(&cxt->bprz, 0 /* single */, record); + /* ftrace is last since it may want to dynamically allocate memory. */ if (!prz_ok(prz)) { if (!(cxt->flags & RAMOOPS_FLAG_FTRACE_PER_CPU) && @@ -334,6 +349,11 @@ static int notrace ramoops_pstore_write(struct pstore_record *record) } else if (record->type == PSTORE_TYPE_PMSG) { pr_warn_ratelimited("PMSG shouldn't call %s\n", __func__); return -EINVAL; + } else if (record->type == PSTORE_TYPE_BLACKBOX) { + if (!cxt->bprz) + return -ENOMEM; + persistent_ram_write(cxt->bprz, record->buf, record->size); + return 0; } if (record->type != PSTORE_TYPE_DMESG) @@ -425,6 +445,9 @@ static int ramoops_pstore_erase(struct pstore_record *record) case PSTORE_TYPE_PMSG: prz = cxt->mprz; break; + case PSTORE_TYPE_BLACKBOX: + prz = cxt->bprz; + break; default: return -EINVAL; } @@ -670,6 +693,7 @@ static int ramoops_parse_dt(struct platform_device *pdev, parse_u32("console-size", pdata->console_size, 0); parse_u32("ftrace-size", pdata->ftrace_size, 0); parse_u32("pmsg-size", pdata->pmsg_size, 0); + parse_u32("blackbox-size", pdata->blackbox_size, 0); parse_u32("ecc-size", pdata->ecc_info.ecc_size, 0); parse_u32("flags", pdata->flags, 0); parse_u32("max-reason", pdata->max_reason, pdata->max_reason); @@ -690,9 +714,11 @@ static int ramoops_parse_dt(struct platform_device *pdev, parent_node = of_get_parent(of_node); if (!of_node_name_eq(parent_node, "reserved-memory") && !pdata->console_size && !pdata->ftrace_size && - !pdata->pmsg_size && !pdata->ecc_info.ecc_size) { + !pdata->pmsg_size && !pdata->ecc_info.ecc_size && + !pdata->blackbox_size) { pdata->console_size = pdata->record_size; pdata->pmsg_size = pdata->record_size; + pdata->blackbox_size = pdata->record_size; } of_node_put(parent_node); @@ -734,7 +760,7 @@ static int ramoops_probe(struct platform_device *pdev) } if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size && - !pdata->ftrace_size && !pdata->pmsg_size)) { + !pdata->ftrace_size && !pdata->pmsg_size && !pdata->blackbox_size)) { pr_err("The memory size and the record/console size must be " "non-zero\n"); goto fail_out; @@ -748,6 +774,8 @@ static int ramoops_probe(struct platform_device *pdev) pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size); if (pdata->pmsg_size && !is_power_of_2(pdata->pmsg_size)) pdata->pmsg_size = rounddown_pow_of_two(pdata->pmsg_size); + if (pdata->blackbox_size && !is_power_of_2(pdata->blackbox_size)) + pdata->blackbox_size = rounddown_pow_of_two(pdata->blackbox_size); cxt->size = pdata->mem_size; cxt->phys_addr = pdata->mem_address; @@ -756,13 +784,20 @@ static int ramoops_probe(struct platform_device *pdev) cxt->console_size = pdata->console_size; cxt->ftrace_size = pdata->ftrace_size; cxt->pmsg_size = pdata->pmsg_size; + cxt->blackbox_size = pdata->blackbox_size; cxt->flags = pdata->flags; cxt->ecc_info = pdata->ecc_info; paddr = cxt->phys_addr; dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size - - cxt->pmsg_size; + - cxt->pmsg_size - cxt->blackbox_size; + + err = ramoops_init_prz("blackbox", dev, cxt, &cxt->bprz, &paddr, + cxt->blackbox_size, 0); + if (err) + goto fail_init_bprz; + err = ramoops_init_przs("dmesg", dev, cxt, &cxt->dprzs, &paddr, dump_mem_sz, cxt->record_size, &cxt->max_dump_cnt, 0, 0); @@ -808,6 +843,8 @@ static int ramoops_probe(struct platform_device *pdev) cxt->pstore.flags |= PSTORE_FLAGS_FTRACE; if (cxt->pmsg_size) cxt->pstore.flags |= PSTORE_FLAGS_PMSG; + if (cxt->blackbox_size) + cxt->pstore.flags |= PSTORE_FLAGS_BLACKBOX; /* * Since bufsize is only used for dmesg crash dumps, it @@ -841,6 +878,7 @@ static int ramoops_probe(struct platform_device *pdev) ramoops_console_size = pdata->console_size; ramoops_pmsg_size = pdata->pmsg_size; ramoops_ftrace_size = pdata->ftrace_size; + ramoops_blackbox_size = pdata->blackbox_size; pr_info("using 0x%lx@0x%llx, ecc: %d\n", cxt->size, (unsigned long long)cxt->phys_addr, @@ -856,6 +894,8 @@ static int ramoops_probe(struct platform_device *pdev) fail_init_mprz: fail_init_fprz: persistent_ram_free(cxt->cprz); +fail_init_bprz: + persistent_ram_free(cxt->bprz); fail_init_cprz: ramoops_free_przs(cxt); fail_out: @@ -873,6 +913,7 @@ static int ramoops_remove(struct platform_device *pdev) persistent_ram_free(cxt->mprz); persistent_ram_free(cxt->cprz); + persistent_ram_free(cxt->bprz); ramoops_free_przs(cxt); return 0; @@ -920,6 +961,7 @@ static void __init ramoops_register_dummy(void) pdata.console_size = ramoops_console_size; pdata.ftrace_size = ramoops_ftrace_size; pdata.pmsg_size = ramoops_pmsg_size; + pdata.blackbox_size = ramoops_blackbox_size; /* If "max_reason" is set, its value has priority over "dump_oops". */ if (ramoops_max_reason >= 0) pdata.max_reason = ramoops_max_reason; @@ -955,6 +997,10 @@ static int __init ramoops_init(void) ret = platform_driver_register(&ramoops_driver); if (ret != 0) ramoops_unregister_dummy(); +#if IS_ENABLED(CONFIG_PSTORE_BLACKBOX) + if (!ret) + pstore_ready = true; +#endif return ret; } diff --git a/include/linux/blackbox.h b/include/linux/blackbox.h index 7e1ba44912d2..e6d963264025 100644 --- a/include/linux/blackbox.h +++ b/include/linux/blackbox.h @@ -50,7 +50,7 @@ struct error_info { struct fault_log_info { char flag[8]; /* 8 is the length of the flag */ - int len; /* length of the kernel fault log */ + size_t len; /* length of the kernel fault log */ struct error_info info; }; diff --git a/include/linux/pstore_ram.h b/include/linux/pstore_ram.h index 9f16afec7290..72fa93bc539b 100644 --- a/include/linux/pstore_ram.h +++ b/include/linux/pstore_ram.h @@ -133,6 +133,7 @@ struct ramoops_platform_data { unsigned long console_size; unsigned long ftrace_size; unsigned long pmsg_size; + unsigned long blackbox_size; int max_reason; u32 flags; struct persistent_ram_ecc_info ecc_info; -- Gitee