Commit cc9c4d1b authored by WeiXiong Liao's avatar WeiXiong Liao Committed by Kees Cook

pstore/zone,blk: Add console frontend support

Support backend for console. To enable console backend, just make
console_size be greater than 0 and a multiple of 4096.
Signed-off-by: default avatarWeiXiong Liao <liaoweixiong@allwinnertech.com>
Link: https://lore.kernel.org/lkml/20200511233229.27745-5-keescook@chromium.org/Signed-off-by: default avatarKees Cook <keescook@chromium.org>
parent 0dc06826
...@@ -180,11 +180,11 @@ config PSTORE_BLK_BLKDEV ...@@ -180,11 +180,11 @@ config PSTORE_BLK_BLKDEV
help help
Which block device should be used for pstore/blk. Which block device should be used for pstore/blk.
It accept the following variants: It accepts the following variants:
1) <hex_major><hex_minor> device number in hexadecimal representation, 1) <hex_major><hex_minor> device number in hexadecimal representation,
with no leading 0x, for example b302. with no leading 0x, for example b302.
2) /dev/<disk_name> represents the device number of disk 2) /dev/<disk_name> represents the device name of disk
3) /dev/<disk_name><decimal> represents the device number 3) /dev/<disk_name><decimal> represents the device name and number
of partition - device number of disk plus the partition number of partition - device number of disk plus the partition number
4) /dev/<disk_name>p<decimal> - same as the above, this form is 4) /dev/<disk_name>p<decimal> - same as the above, this form is
used when disk name of partitioned disk ends with a digit. used when disk name of partitioned disk ends with a digit.
...@@ -236,3 +236,15 @@ config PSTORE_BLK_PMSG_SIZE ...@@ -236,3 +236,15 @@ config PSTORE_BLK_PMSG_SIZE
NOTE that, both Kconfig and module parameters can configure NOTE that, both Kconfig and module parameters can configure
pstore/blk, but module parameters have priority over Kconfig. pstore/blk, but module parameters have priority over Kconfig.
config PSTORE_BLK_CONSOLE_SIZE
int "Size in Kbytes of console log to store"
depends on PSTORE_BLK
depends on PSTORE_CONSOLE
default 64
help
This just sets size of console log (console_size) to store via
pstore/blk. The size is in KB and must be a multiple of 4.
NOTE that, both Kconfig and module parameters can configure
pstore/blk, but module parameters have priority over Kconfig.
...@@ -35,6 +35,14 @@ static long pmsg_size = -1; ...@@ -35,6 +35,14 @@ static long pmsg_size = -1;
module_param(pmsg_size, long, 0400); module_param(pmsg_size, long, 0400);
MODULE_PARM_DESC(pmsg_size, "pmsg size in kbytes"); MODULE_PARM_DESC(pmsg_size, "pmsg size in kbytes");
#if IS_ENABLED(CONFIG_PSTORE_CONSOLE)
static long console_size = CONFIG_PSTORE_BLK_CONSOLE_SIZE;
#else
static long console_size = -1;
#endif
module_param(console_size, long, 0400);
MODULE_PARM_DESC(console_size, "console size in kbytes");
/* /*
* blkdev - the block device to use for pstore storage * blkdev - the block device to use for pstore storage
* *
...@@ -91,7 +99,8 @@ struct bdev_info { ...@@ -91,7 +99,8 @@ struct bdev_info {
* whole disk). * whole disk).
* On success, the number of bytes should be returned, others * On success, the number of bytes should be returned, others
* means error. * means error.
* @write: The same as @read. * @write: The same as @read, but the following error number:
* -EBUSY means try to write again later.
* @panic_write:The write operation only used for panic case. It's optional * @panic_write:The write operation only used for panic case. It's optional
* if you do not care panic log. The parameters and return value * if you do not care panic log. The parameters and return value
* are the same as @read. * are the same as @read.
...@@ -142,6 +151,7 @@ static int psblk_register_do(struct pstore_device_info *dev) ...@@ -142,6 +151,7 @@ static int psblk_register_do(struct pstore_device_info *dev)
verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG); verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG);
verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG); verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG);
verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE);
#undef verify_size #undef verify_size
pstore_zone_info->total_size = dev->total_size; pstore_zone_info->total_size = dev->total_size;
......
...@@ -91,10 +91,12 @@ struct pstore_zone { ...@@ -91,10 +91,12 @@ struct pstore_zone {
* *
* @kpszs: kmsg dump storage zones * @kpszs: kmsg dump storage zones
* @ppsz: pmsg storage zone * @ppsz: pmsg storage zone
* @cpsz: console storage zone
* @kmsg_max_cnt: max count of @kpszs * @kmsg_max_cnt: max count of @kpszs
* @kmsg_read_cnt: counter of total read kmsg dumps * @kmsg_read_cnt: counter of total read kmsg dumps
* @kmsg_write_cnt: counter of total kmsg dump writes * @kmsg_write_cnt: counter of total kmsg dump writes
* @pmsg_read_cnt: counter of total read pmsg zone * @pmsg_read_cnt: counter of total read pmsg zone
* @console_read_cnt: counter of total read console zone
* @oops_counter: counter of oops dumps * @oops_counter: counter of oops dumps
* @panic_counter: counter of panic dumps * @panic_counter: counter of panic dumps
* @recovered: whether finished recovering data from storage * @recovered: whether finished recovering data from storage
...@@ -106,10 +108,12 @@ struct pstore_zone { ...@@ -106,10 +108,12 @@ struct pstore_zone {
struct psz_context { struct psz_context {
struct pstore_zone **kpszs; struct pstore_zone **kpszs;
struct pstore_zone *ppsz; struct pstore_zone *ppsz;
struct pstore_zone *cpsz;
unsigned int kmsg_max_cnt; unsigned int kmsg_max_cnt;
unsigned int kmsg_read_cnt; unsigned int kmsg_read_cnt;
unsigned int kmsg_write_cnt; unsigned int kmsg_write_cnt;
unsigned int pmsg_read_cnt; unsigned int pmsg_read_cnt;
unsigned int console_read_cnt;
/* /*
* These counters should be calculated during recovery. * These counters should be calculated during recovery.
* It records the oops/panic times after crashes rather than boots. * It records the oops/panic times after crashes rather than boots.
...@@ -129,6 +133,9 @@ struct psz_context { ...@@ -129,6 +133,9 @@ struct psz_context {
}; };
static struct psz_context pstore_zone_cxt; static struct psz_context pstore_zone_cxt;
static void psz_flush_all_dirty_zones(struct work_struct *);
static DECLARE_DELAYED_WORK(psz_cleaner, psz_flush_all_dirty_zones);
/** /**
* enum psz_flush_mode - flush mode for psz_zone_write() * enum psz_flush_mode - flush mode for psz_zone_write()
* *
...@@ -237,6 +244,9 @@ static int psz_zone_write(struct pstore_zone *zone, ...@@ -237,6 +244,9 @@ static int psz_zone_write(struct pstore_zone *zone,
return 0; return 0;
dirty: dirty:
atomic_set(&zone->dirty, true); atomic_set(&zone->dirty, true);
/* flush dirty zones nicely */
if (wcnt == -EBUSY && !is_on_panic())
schedule_delayed_work(&psz_cleaner, msecs_to_jiffies(500));
return -EBUSY; return -EBUSY;
} }
...@@ -293,6 +303,21 @@ static int psz_move_zone(struct pstore_zone *old, struct pstore_zone *new) ...@@ -293,6 +303,21 @@ static int psz_move_zone(struct pstore_zone *old, struct pstore_zone *new)
return 0; return 0;
} }
static void psz_flush_all_dirty_zones(struct work_struct *work)
{
struct psz_context *cxt = &pstore_zone_cxt;
int ret = 0;
if (cxt->ppsz)
ret |= psz_flush_dirty_zone(cxt->ppsz);
if (cxt->cpsz)
ret |= psz_flush_dirty_zone(cxt->cpsz);
if (cxt->kpszs)
ret |= psz_flush_dirty_zones(cxt->kpszs, cxt->kmsg_max_cnt);
if (ret && cxt->pstore_zone_info)
schedule_delayed_work(&psz_cleaner, msecs_to_jiffies(1000));
}
static int psz_kmsg_recover_data(struct psz_context *cxt) static int psz_kmsg_recover_data(struct psz_context *cxt)
{ {
struct pstore_zone_info *info = cxt->pstore_zone_info; struct pstore_zone_info *info = cxt->pstore_zone_info;
...@@ -545,6 +570,10 @@ static inline int psz_recovery(struct psz_context *cxt) ...@@ -545,6 +570,10 @@ static inline int psz_recovery(struct psz_context *cxt)
goto out; goto out;
ret = psz_recover_zone(cxt, cxt->ppsz); ret = psz_recover_zone(cxt, cxt->ppsz);
if (ret)
goto out;
ret = psz_recover_zone(cxt, cxt->cpsz);
out: out:
if (unlikely(ret)) if (unlikely(ret))
...@@ -562,6 +591,7 @@ static int psz_pstore_open(struct pstore_info *psi) ...@@ -562,6 +591,7 @@ static int psz_pstore_open(struct pstore_info *psi)
cxt->kmsg_read_cnt = 0; cxt->kmsg_read_cnt = 0;
cxt->pmsg_read_cnt = 0; cxt->pmsg_read_cnt = 0;
cxt->console_read_cnt = 0;
return 0; return 0;
} }
...@@ -626,8 +656,9 @@ static int psz_pstore_erase(struct pstore_record *record) ...@@ -626,8 +656,9 @@ static int psz_pstore_erase(struct pstore_record *record)
return psz_kmsg_erase(cxt, cxt->kpszs[record->id], record); return psz_kmsg_erase(cxt, cxt->kpszs[record->id], record);
case PSTORE_TYPE_PMSG: case PSTORE_TYPE_PMSG:
return psz_record_erase(cxt, cxt->ppsz); return psz_record_erase(cxt, cxt->ppsz);
default: case PSTORE_TYPE_CONSOLE:
return -EINVAL; return psz_record_erase(cxt, cxt->cpsz);
default: return -EINVAL;
} }
} }
...@@ -690,9 +721,10 @@ static int notrace psz_kmsg_write(struct psz_context *cxt, ...@@ -690,9 +721,10 @@ static int notrace psz_kmsg_write(struct psz_context *cxt,
return -ENOSPC; return -ENOSPC;
ret = psz_kmsg_write_record(cxt, record); ret = psz_kmsg_write_record(cxt, record);
if (!ret) { if (!ret && is_on_panic()) {
/* ensure all data are flushed to storage when panic */
pr_debug("try to flush other dirty zones\n"); pr_debug("try to flush other dirty zones\n");
psz_flush_dirty_zones(cxt->kpszs, cxt->kmsg_max_cnt); psz_flush_all_dirty_zones(NULL);
} }
/* always return 0 as we had handled it on buffer */ /* always return 0 as we had handled it on buffer */
...@@ -756,9 +788,18 @@ static int notrace psz_pstore_write(struct pstore_record *record) ...@@ -756,9 +788,18 @@ static int notrace psz_pstore_write(struct pstore_record *record)
record->reason == KMSG_DUMP_PANIC) record->reason == KMSG_DUMP_PANIC)
atomic_set(&cxt->on_panic, 1); atomic_set(&cxt->on_panic, 1);
/*
* if on panic, do not write except panic records
* Fix case that panic_write prints log which wakes up console backend.
*/
if (is_on_panic() && record->type != PSTORE_TYPE_DMESG)
return -EBUSY;
switch (record->type) { switch (record->type) {
case PSTORE_TYPE_DMESG: case PSTORE_TYPE_DMESG:
return psz_kmsg_write(cxt, record); return psz_kmsg_write(cxt, record);
case PSTORE_TYPE_CONSOLE:
return psz_record_write(cxt->cpsz, record);
case PSTORE_TYPE_PMSG: case PSTORE_TYPE_PMSG:
return psz_record_write(cxt->ppsz, record); return psz_record_write(cxt->ppsz, record);
default: default:
...@@ -783,6 +824,13 @@ static struct pstore_zone *psz_read_next_zone(struct psz_context *cxt) ...@@ -783,6 +824,13 @@ static struct pstore_zone *psz_read_next_zone(struct psz_context *cxt)
return zone; return zone;
} }
if (cxt->console_read_cnt == 0) {
cxt->console_read_cnt++;
zone = cxt->cpsz;
if (psz_old_ok(zone))
return zone;
}
return NULL; return NULL;
} }
...@@ -893,6 +941,8 @@ static ssize_t psz_pstore_read(struct pstore_record *record) ...@@ -893,6 +941,8 @@ static ssize_t psz_pstore_read(struct pstore_record *record)
readop = psz_kmsg_read; readop = psz_kmsg_read;
record->id = cxt->kmsg_read_cnt - 1; record->id = cxt->kmsg_read_cnt - 1;
break; break;
case PSTORE_TYPE_CONSOLE:
fallthrough;
case PSTORE_TYPE_PMSG: case PSTORE_TYPE_PMSG:
readop = psz_record_read; readop = psz_record_read;
break; break;
...@@ -953,6 +1003,8 @@ static void psz_free_all_zones(struct psz_context *cxt) ...@@ -953,6 +1003,8 @@ static void psz_free_all_zones(struct psz_context *cxt)
psz_free_zones(&cxt->kpszs, &cxt->kmsg_max_cnt); psz_free_zones(&cxt->kpszs, &cxt->kmsg_max_cnt);
if (cxt->ppsz) if (cxt->ppsz)
psz_free_zone(&cxt->ppsz); psz_free_zone(&cxt->ppsz);
if (cxt->cpsz)
psz_free_zone(&cxt->cpsz);
} }
static struct pstore_zone *psz_init_zone(enum pstore_type_id type, static struct pstore_zone *psz_init_zone(enum pstore_type_id type,
...@@ -1054,6 +1106,15 @@ static int psz_alloc_zones(struct psz_context *cxt) ...@@ -1054,6 +1106,15 @@ static int psz_alloc_zones(struct psz_context *cxt)
goto free_out; goto free_out;
} }
off_size += info->console_size;
cxt->cpsz = psz_init_zone(PSTORE_TYPE_CONSOLE, &off,
info->console_size);
if (IS_ERR(cxt->cpsz)) {
err = PTR_ERR(cxt->cpsz);
cxt->cpsz = NULL;
goto free_out;
}
cxt->kpszs = psz_init_zones(PSTORE_TYPE_DMESG, &off, cxt->kpszs = psz_init_zones(PSTORE_TYPE_DMESG, &off,
info->total_size - off_size, info->total_size - off_size,
info->kmsg_size, &cxt->kmsg_max_cnt); info->kmsg_size, &cxt->kmsg_max_cnt);
...@@ -1088,7 +1149,7 @@ int register_pstore_zone(struct pstore_zone_info *info) ...@@ -1088,7 +1149,7 @@ int register_pstore_zone(struct pstore_zone_info *info)
return -EINVAL; return -EINVAL;
} }
if (!info->kmsg_size && !info->pmsg_size) { if (!info->kmsg_size && !info->pmsg_size && !info->console_size) {
pr_warn("at least one record size must be non-zero\n"); pr_warn("at least one record size must be non-zero\n");
return -EINVAL; return -EINVAL;
} }
...@@ -1111,6 +1172,7 @@ int register_pstore_zone(struct pstore_zone_info *info) ...@@ -1111,6 +1172,7 @@ int register_pstore_zone(struct pstore_zone_info *info)
check_size(total_size, 4096); check_size(total_size, 4096);
check_size(kmsg_size, SECTOR_SIZE); check_size(kmsg_size, SECTOR_SIZE);
check_size(pmsg_size, SECTOR_SIZE); check_size(pmsg_size, SECTOR_SIZE);
check_size(console_size, SECTOR_SIZE);
#undef check_size #undef check_size
...@@ -1137,6 +1199,7 @@ int register_pstore_zone(struct pstore_zone_info *info) ...@@ -1137,6 +1199,7 @@ int register_pstore_zone(struct pstore_zone_info *info)
pr_debug("\ttotal size : %ld Bytes\n", info->total_size); pr_debug("\ttotal size : %ld Bytes\n", info->total_size);
pr_debug("\tkmsg size : %ld Bytes\n", info->kmsg_size); pr_debug("\tkmsg size : %ld Bytes\n", info->kmsg_size);
pr_debug("\tpmsg size : %ld Bytes\n", info->pmsg_size); pr_debug("\tpmsg size : %ld Bytes\n", info->pmsg_size);
pr_debug("\tconsole size : %ld Bytes\n", info->console_size);
err = psz_alloc_zones(cxt); err = psz_alloc_zones(cxt);
if (err) { if (err) {
...@@ -1170,6 +1233,10 @@ int register_pstore_zone(struct pstore_zone_info *info) ...@@ -1170,6 +1233,10 @@ int register_pstore_zone(struct pstore_zone_info *info)
cxt->pstore.flags |= PSTORE_FLAGS_PMSG; cxt->pstore.flags |= PSTORE_FLAGS_PMSG;
pr_cont(" pmsg"); pr_cont(" pmsg");
} }
if (info->console_size) {
cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
pr_cont(" console");
}
pr_cont("\n"); pr_cont("\n");
err = pstore_register(&cxt->pstore); err = pstore_register(&cxt->pstore);
...@@ -1211,6 +1278,10 @@ void unregister_pstore_zone(struct pstore_zone_info *info) ...@@ -1211,6 +1278,10 @@ void unregister_pstore_zone(struct pstore_zone_info *info)
/* Stop incoming writes from pstore. */ /* Stop incoming writes from pstore. */
pstore_unregister(&cxt->pstore); pstore_unregister(&cxt->pstore);
/* Flush any pending writes. */
psz_flush_all_dirty_zones(NULL);
flush_delayed_work(&psz_cleaner);
/* Clean up allocations. */ /* Clean up allocations. */
kfree(cxt->pstore.buf); kfree(cxt->pstore.buf);
cxt->pstore.buf = NULL; cxt->pstore.buf = NULL;
......
...@@ -18,11 +18,12 @@ typedef ssize_t (*pstore_zone_write_op)(const char *, size_t, loff_t); ...@@ -18,11 +18,12 @@ typedef ssize_t (*pstore_zone_write_op)(const char *, size_t, loff_t);
* it must be multiple of SECTOR_SIZE(512 Bytes). * it must be multiple of SECTOR_SIZE(512 Bytes).
* @max_reason: Maximum kmsg dump reason to store. * @max_reason: Maximum kmsg dump reason to store.
* @pmsg_size: The size of pmsg zone which is the same as @kmsg_size. * @pmsg_size: The size of pmsg zone which is the same as @kmsg_size.
* @console_size:The size of console zone which is the same as @kmsg_size.
* @read: The general read operation. Both of the function parameters * @read: The general read operation. Both of the function parameters
* @size and @offset are relative value to storage. * @size and @offset are relative value to storage.
* On success, the number of bytes should be returned, others * On success, the number of bytes should be returned, others
* means error. * means error.
* @write: The same as @read. * @write: The same as @read, but -EBUSY means try to write again later.
* @panic_write:The write operation only used for panic case. It's optional * @panic_write:The write operation only used for panic case. It's optional
* if you do not care panic log. The parameters and return value * if you do not care panic log. The parameters and return value
* are the same as @read. * are the same as @read.
...@@ -35,6 +36,7 @@ struct pstore_zone_info { ...@@ -35,6 +36,7 @@ struct pstore_zone_info {
unsigned long kmsg_size; unsigned long kmsg_size;
int max_reason; int max_reason;
unsigned long pmsg_size; unsigned long pmsg_size;
unsigned long console_size;
pstore_zone_read_op read; pstore_zone_read_op read;
pstore_zone_write_op write; pstore_zone_write_op write;
pstore_zone_write_op panic_write; pstore_zone_write_op panic_write;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment