pstore/rom: new support logger for block devices
v4 cover pstore/rom: new support logger for block devices
Why should we need pstore_rom?
1. Most embedded intelligent equipment have no persistent ram, which increases costs. We perfer to cheaper solutions, like block devices. In fast, there is already a sample for block device logger in driver MTD (drivers/mtd/mtdoops.c).
2. Do not any equipment have battery, which means that it lost all data on general ram if power failure. Pstore has little to do for these equipments.
[PATCH v4]
On patch 1:
1. Fix always true condition '(--i >= 0) => (0-u32max >= 0)' in function romz_init_zones by defining variable i to 'int' rahter than 'unsigned int'.
2. To make codes more easily to read, we use macro READ_NEXT_ZONE for return value of romz_dmesg_read if it need to read next zone. Moveover, we assign READ_NEXT_ZONE -1024 rather than 0.
3. Add 'FLUSH_META' to 'enum romz_flush_mode' and rename 'NOT_FLUSH' to 'FLUSH_NONE'
4. Function romz_zone_write work badly with FLUSH_PART mode as badly address and offset to write.
On patch 3:
NEW SUPPORT psmg for pstore_rom.
[PATCH v3]
On patch 1:
Fix build as module error for undefined 'vfs_read' and 'vfs_write' Both of 'vfs_read' and 'vfs_write' haven't be exproted yet, so we use 'kernel_read' and 'kernel_write' instead.
[PATCH v2]
On patch 1:
Fix build as module error for redefinition of 'romz_unregister' and 'romz_register'
[PATCH v1]
On patch 1:
Core codes of pstore_rom, which works well on allwinner(sunxi) platform. On patch 2:
A sample for pstore_rom, using general ram rather than block device.
liaoweixiong (3):
pstore/rom: new support logger for block devices pstore/rom: add sample for pstore_rom pstore/rom: support pmsg for pstore_rom
fs/pstore/Kconfig | 16 + fs/pstore/Makefile | 5 + fs/pstore/rombuf.c | 46 ++ fs/pstore/romzone.c | 1138 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/pstore_rom.h | 62 +++ 5 files changed, 1267 insertions(+) create mode 100644 fs/pstore/rombuf.c create mode 100644 fs/pstore/romzone.c create mode 100644 include/linux/pstore_rom.h
Signature · 1 line
1.9.1
RFC v4 1/3 pstore/rom: new support logger for block devices
pstore_rom is similar to pstore_ram, but dump log to block devices rather than persistent ram.
Why should we need pstore_rom?
1. Most embedded intelligent equipment have no persistent ram, which increases costs. We perfer to cheaper solutions, like block devices. In fast, there is already a sample for block device logger in driver MTD (drivers/mtd/mtdoops.c).
2. Do not any equipment have battery, which means that it lost all data on general ram if power failure. Pstore has little to do for these equipments.
pstore_rom can only dump Oops/Panic log to block devices. It only supports dmesg now. To make pstore_rom work, the block driver should provide the path of a block partition device (/dev/XXXX) and the read/write apis when on panic.
pstore_rom begins at 'romz_register', by witch block device can register a block partition to pstore_rom. Then pstore_rom divide and manage the partition as zones, which is similar to pstore_ram.
Recommend that, block driver register pstore_rom after block device is ready.
pstore_rom works well on allwinner(sunxi) platform.
--- fs/pstore/Kconfig | 7 + fs/pstore/Makefile | 3 + fs/pstore/romzone.c | 961 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/pstore_rom.h | 61 +++ 4 files changed, 1032 insertions(+) create mode 100644 fs/pstore/romzone.c create mode 100644 include/linux/pstore_rom.h
fs/pstore/Kconfig +7 -0
@@ -152,3 +152,10 @@ config PSTORE_RAM "ramoops.ko". For more information, see Documentation/admin-guide/ramoops.rst. + +config PSTORE_ROM + tristate "Log panic/oops to a block device" + depends on PSTORE + help + This enables panic and oops message to be logged to a block dev + where it can be read back at some later point.
fs/pstore/Makefile +3 -0
@@ -12,3 +12,6 @@ pstore-$(CONFIG_PSTORE_PMSG) += pmsg.o ramoops-objs += ram.o ram_core.o obj-$(CONFIG_PSTORE_RAM) += ramoops.o + +obj-$(CONFIG_PSTORE_ROM) += romoops.o +romoops-y += romzone.o
fs/pstore/romzone.c new file +961 -0
961 lines not on this page · raw message
include/linux/pstore_rom.h new file +61 -0
61 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC v4 2/3 pstore/rom: add sample for pstore_rom
In reply to liaoweixiongIt is a sample for pstore_rom, using general ram rather than block device. According to pstore_rom, the data will be saved to ram buffer if not register device path and apis for panic. So, it can only used to dump Oops and some things will not reboot.
--- fs/pstore/Kconfig | 9 +++++++++ fs/pstore/Makefile | 2 ++ fs/pstore/rombuf.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 fs/pstore/rombuf.c
fs/pstore/Kconfig +9 -0
@@ -159,3 +159,12 @@ config PSTORE_ROM help This enables panic and oops message to be logged to a block dev where it can be read back at some later point. + +config PSTORE_ROMBUF + tristate "romoop buffer sample" + depends on PSTORE_ROM + help + This is a sample for pstore rom, but do NOT have a block dev. + According to pstore_rom, the data will be saved to ram buffer and + dropped when reboot. So, it can only used to dump Oops and some + things will not reboot.
fs/pstore/Makefile +2 -0
@@ -15,3 +15,5 @@ obj-$(CONFIG_PSTORE_RAM) += ramoops.o obj-$(CONFIG_PSTORE_ROM) += romoops.o romoops-y += romzone.o + +obj-$(CONFIG_PSTORE_ROMBUF) += rombuf.o
fs/pstore/rombuf.c new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
1.9.1
RFC v4 3/3 pstore/rom: support pmsg for pstore_rom
In reply to liaoweixiongTo enable pmsg, just set pmsg_size when block device register romzone. The pmsg have a single-boot lifetime, which means that zone of pmsg gets wiped after its contents get copied out after boot.
--- fs/pstore/romzone.c | 194 +++++++++++++++++++++++++++++++++++++++++++-- include/linux/pstore_rom.h | 1 + 2 files changed, 187 insertions(+), 8 deletions(-)
fs/pstore/romzone.c +186 -8
@@ -31,12 +31,14 @@* * @sig: signature to indicate header (ROM_SIG xor ROMZONE-type value) * @datalen: length of data in @data +* @start: offset into @data where the beginning of the stored bytes begin * @data: zone data. */ struct romz_buffer {#define ROM_SIG (0x43474244)/* DBGC */ uint32_t sig;atomic_t datalen; +atomic_t start;uint8_t data[0 ]; };
@@ -69,6 +71,10 @@ struct romz_dmesg_header {* frontent name for this zone * @buffer: * pointer to data buffer managed by this zone +* @oldbuf: +* pointer to old data buffer. It is used for zone who have a single-boot +* lifetime, which means that this zone gets wiped after its contents get +* copied out after boot. * @buffer_size: * bytes in @buffer->data * @should_recover:
@@ -82,6 +88,7 @@ struct romz_zone {enum pstore_type_id type;struct romz_buffer *buffer; +struct romz_buffer *oldbuf;size_t buffer_size;bool should_recover;atomic_t dirty;
@@ -89,8 +96,10 @@ struct romz_zone {struct romoops_context {struct romz_zone **drzs;/* Oops dump zones */ +struct romz_zone *prz;/* Pmsg dump zones */ unsigned int dmesg_max_cnt;unsigned int dmesg_read_cnt; +unsigned int pmsg_read_cnt;unsigned int dmesg_write_cnt;/** * the counter should be recovered when do recovery
@@ -124,6 +133,11 @@ static inline int buffer_datalen(struct romz_zone *zone)return atomic_read (&zone->buffer->datalen); } +static inline int buffer_start (struct romz_zone *zone) +{ +return atomic_read (&zone->buffer->start); +} +static inline bool is_on_panic (void ) {struct romoops_context *cxt = &romz_cxt;
@@ -398,6 +412,65 @@ static int romz_recover_dmesg(struct romoops_context *cxt)return ret; } +static int romz_recover_pmsg (struct romoops_context *cxt) +{ +struct romz_info *info = cxt->rzinfo; +struct romz_buffer *oldbuf; +struct romz_zone *zone =NULL ; +ssize_t (*readop)(char *buf,size_t bytes,loff_t pos); +int ret =0 ; +ssize_t rcnt, len; + + zone = cxt->prz; +if (!zone) +return -EINVAL ; + +if (zone->oldbuf) +return 0 ; + + readop = info->read; +if (is_on_panic ()) + readop = info->panic_read; +if (!readop) +return -EINVAL ; + + len = zone->buffer_size +sizeof (*oldbuf); + oldbuf =kzalloc (len,GFP_KERNEL ); +if (!oldbuf) +return -ENOMEM ; + + rcnt =readop ((char *)oldbuf, len, zone->off); +if (rcnt != len) { +pr_debug ("recovery pmsg failed\n" ); + ret = -EIO ; + goto free_oldbuf; + } + +if (oldbuf->sig != zone->buffer->sig) { +pr_debug ("no valid data in zone %s\n" , zone->name); + goto free_oldbuf; + } + +if (zone->buffer_size <atomic_read (&oldbuf->datalen) || + zone->buffer_size <atomic_read (&oldbuf->start)) { +pr_info ("found overtop zone: %s: off %lu, size %zu\n" , + zone->name, zone->off, zone->buffer_size); + goto free_oldbuf; + } + +if (atomic_read (&zone->dirty)) +romz_zone_write (zone,FLUSH_ALL ,NULL ,buffer_datalen (zone),0 ); +else +romz_zone_write (zone,FLUSH_META ,NULL ,0 ,0 ); + + zone->oldbuf = oldbuf; +return 0 ; + +free_oldbuf: +kfree (oldbuf); +return ret; +} +static inline int romz_recovery (struct romoops_context *cxt) {int ret = -EBUSY ;
@@ -412,6 +485,10 @@ static inline int romz_recovery(struct romoops_context *cxt)if (ret) goto recover_fail; + ret =romz_recover_pmsg (cxt); +if (ret) + goto recover_fail; +atomic_set (&cxt->recovery,1 );pr_debug ("recover end!\n" );return 0 ;
@@ -520,6 +597,55 @@ static int notrace romz_dmesg_write(struct romoops_context *cxt,return 0 ; } +static int notrace romz_pmsg_write(struct romoops_context *cxt, +struct pstore_record *record) +{ +struct romz_zone *zone; +size_t start, rem; +int cnt = record->size; +bool is_full_data = false; +char *buf = record->buf; + + zone = cxt->prz; +if (!zone) +return -ENOSPC ; + +if (atomic_read (&zone->buffer->datalen) >= zone->buffer_size) + is_full_data = true; + +if (unlikely (cnt > zone->buffer_size)) { + buf += cnt - zone->buffer_size; + cnt = zone->buffer_size; + } + + start =buffer_start (zone); + rem = zone->buffer_size - start; +if (unlikely (rem < cnt)) { +romz_zone_write (zone,FLUSH_PART , buf, rem, start); + buf += rem; + cnt -= rem; + start =0 ; + is_full_data = true; + } + +atomic_set (&zone->buffer->start, cnt + start); +romz_zone_write (zone,FLUSH_PART , buf, cnt, start); + +/** +* romz_zone_write will set datalen as start + cnt. +* It work if actual data length lesser than buffer size. +* If data length greater than buffer size, pmsg will rewrite to +* beginning of zone, which make buffer->datalen wrongly. +* So we should reset datalen as buffer size once actual data length +* greater than buffer size. +*/ +if (is_full_data) { +atomic_set (&zone->buffer->datalen, zone->buffer_size); +romz_zone_write (zone,FLUSH_META ,NULL ,0 ,0 ); + } +return 0 ; +} + static int notrace romoops_pstore_write(struct pstore_record *record) {struct romoops_context *cxt = record->psi->data;
@@ -537,6 +663,8 @@ static int notrace romoops_pstore_write(struct pstore_record *record)switch (record->type) {case PSTORE_TYPE_DMESG :return romz_dmesg_write (cxt, record); +case PSTORE_TYPE_PMSG : +return romz_pmsg_write (cxt, record);default :return -EINVAL ; }
@@ -544,9 +672,11 @@ static int notrace romoops_pstore_write(struct pstore_record *record)static inline bool romz_ok (struct romz_zone *zone) { -if (!zone || !zone->buffer || !buffer_datalen (zone)) -return false; -return true; +if (zone && zone->oldbuf &&atomic_read (&zone->oldbuf->datalen)) +return true; +if (zone && zone->buffer &&buffer_datalen (zone)) +return true; +return false; }#define READ_NEXT_ZONE ((ssize_t)(-1024))
@@ -560,6 +690,13 @@ static struct romz_zone *romz_read_next_zone(struct romoops_context *cxt)return zone; } +if (cxt->pmsg_read_cnt ==0 ) { + cxt->pmsg_read_cnt++; + zone = cxt->prz; +if (romz_ok (zone)) +return zone; + } +return NULL ; }
@@ -620,6 +757,29 @@ static ssize_t romz_dmesg_read(struct romz_zone *zone,return size + hlen; } +static ssize_t romz_pmsg_read (struct romz_zone *zone, +struct pstore_record *record) +{ +size_t size, start; +struct romz_buffer *buf; + + buf = (struct romz_buffer *)zone->oldbuf; +if (!buf) +return READ_NEXT_ZONE ; + + size =atomic_read (&buf->datalen); + start =atomic_read (&buf->start); + + record->buf =kmalloc (size,GFP_KERNEL ); +if (!record->buf) +return -ENOMEM ; + +memcpy (record->buf, buf->data + start, size - start); +memcpy (record->buf + size - start, buf->data, start); + +return size; +} +static ssize_t romoops_pstore_read (struct pstore_record *record) {struct romoops_context *cxt = record->psi->data;
@@ -651,6 +811,9 @@ static ssize_t romoops_pstore_read(struct pstore_record *record) romz_read = romz_dmesg_read; record->id = cxt->dmesg_read_cnt -1 ;break ; +case PSTORE_TYPE_PMSG : + romz_read = romz_pmsg_read; +break ;default : goto next_zone; }
@@ -757,8 +920,10 @@ static struct romz_zone *romz_init_zone(enum pstore_type_id type, zone->type = type; zone->buffer_size = size -sizeof (struct romz_buffer ); zone->buffer->sig = type ^ROM_SIG ; + zone->oldbuf =NULL ;atomic_set (&zone->dirty,0 );atomic_set (&zone->buffer->datalen,0 ); +atomic_set (&zone->buffer->start,0 ); *off += size;
@@ -840,7 +1005,7 @@ static int romz_cut_zones(struct romoops_context *cxt)int err;size_t size; - size = info->part_size; + size = info->part_size - info->pmsg_size; cxt->drzs =romz_init_zones (PSTORE_TYPE_DMESG , &off, size, info->dmesg_size, &cxt->dmesg_max_cnt);if (IS_ERR (cxt->drzs)) {
@@ -848,7 +1013,16 @@ static int romz_cut_zones(struct romoops_context *cxt) goto fail_out; } + size = info->pmsg_size; + cxt->prz =romz_init_zone (PSTORE_TYPE_PMSG , &off, size); +if (IS_ERR (cxt->prz)) { + err =PTR_ERR (cxt->prz); + goto free_dmesg_zones; + } +return 0 ; +free_dmesg_zones: +romz_free_zones (&cxt->drzs, &cxt->dmesg_max_cnt); fail_out:return err; }
@@ -859,7 +1033,7 @@ int romz_register(struct romz_info *info)struct romoops_context *cxt = &romz_cxt;struct module *owner = info->owner; -if (!info->part_size || !info->dmesg_size) { +if (!info->part_size || (!info->dmesg_size && !info->pmsg_size)) {pr_warn ("The memory size and the dmesg size must be non-zero\n" );return -EINVAL ; }
@@ -879,6 +1053,7 @@ int romz_register(struct romz_info *info)check_size (part_size,4096 -1 );check_size (dmesg_size,SECTOR_SIZE -1 ); +check_size (pmsg_size,SECTOR_SIZE -1 );#undef check_size
@@ -915,6 +1090,8 @@ int romz_register(struct romz_info *info) } cxt->pstore.data = cxt; cxt->pstore.flags =PSTORE_FLAGS_DMESG ; +if (info->pmsg_size) + cxt->pstore.flags |=PSTORE_FLAGS_PMSG ; err =pstore_register (&cxt->pstore);if (err) {
@@ -922,9 +1099,10 @@ int romz_register(struct romz_info *info) goto free_pstore_buf; } -pr_info ("Registered %s as romzone backend for %s%s\n" , info->name, +pr_info ("Registered %s as romzone backend for %s%s%s\n" , info->name, cxt->drzs && cxt->rzinfo->dump_oops ?"Oops " :"" , - cxt->drzs ?"Panic " :"" ); + cxt->drzs ?"Panic " :"" , + cxt->prz ?"Pmsg" :"" );module_put (owner);return 0 ;
@@ -952,7 +1130,7 @@ void romz_unregister(struct romz_info *info)spin_unlock (&cxt->rzinfo_lock);romz_free_zones (&cxt->drzs, &cxt->dmesg_max_cnt); - +romz_free_zone (&cxt->prz); }EXPORT_SYMBOL_GPL (romz_unregister);
include/linux/pstore_rom.h +1 -0
@@ -48,6 +48,7 @@ struct romz_info {const char *part_path;unsigned long part_size;unsigned long dmesg_size; +unsigned long pmsg_size;int dump_oops;ssize_t (*read)(char *buf,size_t bytes,loff_t pos);ssize_t (*write)(const char *buf,size_t bytes,loff_t pos);
Signature · 1 line
1.9.1
Luck, Tony
reviewerI'm curious why you call this "pstore/rom" rather than the more descriptive "pstore/block".
It looks to be a really good idea.
I think you need to document how the "write" function for the block device must be written. Since pstore calls this at "panic" time, the write path:
+ Cannot allocate any memory
+ Must be polled, not interrupt driven
+ Cannot take any locks that may be held by regular code
+ ... perhaps other restrictions that I can't think of right now
The memory allocation restriction is likely easy to get around. Just allocate anything you need at pstore "init" time rather than waiting until the panic.
-Tony
hi Tony:
Quoted 1 line · Tony
I'm curious why you call this "pstore/rom" rather than the more descriptive "pstore/block".
Because there is "pstore/ram", so i name it as "pstore/rom". It's nice to rename it "pstore/block", i will change it in next version of patch.
Quoted 2 lines
It looks to be a really good idea.
Should i add "Acked-by" in next version of patch? I have little experience in sending patch to upstream linux.
Quoted 14 lines
I think you need to document how the "write" function for the block device must be written. Since pstore calls this at "panic" time, the write path:
+ Cannot allocate any memory
+ Must be polled, not interrupt driven
+ Cannot take any locks that may be held by regular code
+ ... perhaps other restrictions that I can't think of right nowThe memory allocation restriction is likely easy to get around. Just allocate anything you need at pstore "init" time rather than waiting until the panic.
I will provide a documemt for it in next version of patch.
-Tony
Luck, Tony
reviewerQuoted 2 lines
Quoted 1 line
It looks to be a really good idea.
Should i add "Acked-by" in next version of patch?
I've only looked at the concept, not at the code yet. So too early to commit to an "Acked-by"
Quoted 1 line
I have little experience in sending patch to upstream linux.
You are doing fine so far.
-Tony