persistent store
Subsystems (2)
Luck, Tony
reviewerRFC 1/1 persistent store
Here's a patch based on some discussions I had with Thomas Gleixner at plumbers conference that implements a generic layer for persistent storage usable to pass tens or hundreds of kilobytes of data from the dying breath of a crashing kernel to its successor.
The usage model I'm envisioning is that a platform driver will register with this code to provide the actual storage. I've tried to make this interface general, but I'm working from a sample of one (the ACPI ERST code), so if anyone else has some persistent store that can't be handled by this code, speak up and we can put in the necessary tweaks.
My assumptions are that the data that Linux cares about will be wrapped in some error record structure with a header, and possibly a footer that the device code needs. So the driver specifies how much padding to put around a buffer to make life easy for it. It also specifies the maximum number of bytes that can be saved in one record.
There are three callback functions from the generic code to the driver:
"reader" which iterates over all records currently in the store - returning type, size and a record identifier as well as the actual data.
"writer" which writes a record with a type to the persistent store
"eraser" which takes a record identifier, and clears that item from the store.
The Linux user visible interface is via /sys (similar to the "efivars" interface)
# ls -l /sys/firmware/pstore total 0 -r--r--r-- 1 root root 0 2010-11-20 11:03 dmesg-0 --w------- 1 root root 0 2010-11-20 11:03 erase
The "type" of error record I mentioned earlier is used to name the files ... saved console logs from kmsg_dmp() are named with a "dmesg" prefix as shown above.
Once an error record has been viewed, analysed, saved. The user can request it to be cleared by writing its name to the "erase" file:
# echo "dmesg-0" > erase
Answers to a few questions that I think you might ask:
1) "Why do you only allow one platform driver to register?"
I only have one such driver. Adding more is easy from the "read" side (just collect all the records from all devices and remember where they came from so you can call the correct "eraser" function). But the "write" side opens up questions that I don't have good answers for:
- Which device(s) should error records be written to?
All of them? Start with one and move on when it is
full? Write some types of records to one device?
If someone has a machine with multiple persistent storage devices - then we can talk about how to answer these questions.
2) "Why do you read in all the data from the device when it registers and save it in memory? Couldn't you just get the list of records and pick up the data from the device when the user reads the file?"
I don't think this is going to be very much data, just a few hundred kilobytes (i.e. less that $0.01 worth of memory, even expensive server memory). The memory is freed when the record is erased ... which is likely to be soon after boot.
3) "/sys/firmware/pstore is the wrong pathname for this".
You are probably right. I put it under "firmware" because that's where the "efivars" driver put its top level directory. In my case the ERST back end is firmware, so there is some vague logic to it ... but better suggestions are welcome. Perhaps /sys/devices/platform/pstore?
4) "/sys is the wrong place for this."
Perhaps. I definitely want to use some sort of filesystem interface (so each record shows up as a file to the user). This seems a lot cleaner than trying to map the semantics of actual persistent storage devices onto a character device. The "sysfs_create_bin_file()" API seems very well designed for this usage. If not /sys, then where? "debugfs" would work - but not everyone mounts debugfs. Creating a whole new filesystem for this seems like overkill.
5) "Why is the record identifier type 'u64'?"
This is one place where I knowingly let the ERST implementation bleed all the way up to the top - it uses 64-bit record numbers. It would be possible to map these to something smaller like "int" ... but the code to do so would be far larger than the memory saved. The most common usage case is likely to be a software crash with just one "dmesg" record.
6) "Is this widely useful? How many systems have persistent storage?" Although ERST was only added to the ACPI spec earlier this year, it merely documents existing functionality required for WHEA (Windows Hardware Error Architecture). So most modern server systems should have it (my test system has it, and it has a BIOS written in mid 2008). Sorry desktops & laptops - no love for you here.
No-sign-off-yet-this-is-just-RFC
-Tony
---
drivers/firmware/Kconfig +12 -0
@@ -134,4 +134,16 @@ config ISCSI_IBFT detect iSCSI boot parameters dynamically during system boot, say Y. Otherwise, say N. +config PSTORE + tristate "Persistant store support via /sys" + default n + help + This option enables generic access to platform level persistent + storage via /sys/firmware/pstore. Only useful if you have a + platform level driver that registers with pstore to provide + the data, so you probably should just go say "Y" (or "M") to + a platform specific persistent store driver (e.g. ACPI_APEI on + X86) which will select this for you. If you don't have a platform + persistent store driver, say N. + endmenu
drivers/firmware/Makefile +1 -0
@@ -11,3 +11,4 @@ obj-$(CONFIG_DMIID) += dmi-id.o obj-$(CONFIG_ISCSI_IBFT_FIND) += iscsi_ibft_find.o obj-$(CONFIG_ISCSI_IBFT) += iscsi_ibft.o obj-$(CONFIG_FIRMWARE_MEMMAP) += memmap.o +obj-$(CONFIG_PSTORE) += pstore.o
drivers/firmware/pstore.c new file +313 -0
313 lines not on this page · raw message
include/linux/pstore.h new file +54 -0
54 lines not on this page · raw message
Hi Tony,
this is actually very cool.
See below for minor nitpicks:
Quoted 53 lines · Tony
Here's a patch based on some discussions I had with Thomas Gleixner at plumbers conference that implements a generic layer for persistent storage usable to pass tens or hundreds of kilobytes of data from the dying breath of a crashing kernel to its successor.
The usage model I'm envisioning is that a platform driver will register with this code to provide the actual storage. I've tried to make this interface general, but I'm working from a sample of one (the ACPI ERST code), so if anyone else has some persistent store that can't be handled by this code, speak up and we can put in the necessary tweaks.
My assumptions are that the data that Linux cares about will be wrapped in some error record structure with a header, and possibly a footer that the device code needs. So the driver specifies how much padding to put around a buffer to make life easy for it. It also specifies the maximum number of bytes that can be saved in one record.
There are three callback functions from the generic code to the driver:
"reader" which iterates over all records currently in the store - returning type, size and a record identifier as well as the actual data.
"writer" which writes a record with a type to the persistent store
"eraser" which takes a record identifier, and clears that item from the store.
The Linux user visible interface is via /sys (similar to the "efivars" interface)
# ls -l /sys/firmware/pstore total 0 -r--r--r-- 1 root root 0 2010-11-20 11:03 dmesg-0 --w------- 1 root root 0 2010-11-20 11:03 eraseThe "type" of error record I mentioned earlier is used to name the files ... saved console logs from
kmsg_dmp()are named with a "dmesg" prefix as shown above.Once an error record has been viewed, analysed, saved. The user can request it to be cleared by writing its name to the "erase" file:
# echo "dmesg-0" > eraseAnswers to a few questions that I think you might ask:
Quoted 8 lines
1) "Why do you only allow one platform driver to register?"
I only have one such driver. Adding more is easy from the "read" side (just collect all the records from all devices and remember where they came from so you can call the correct "eraser" function). But the "write" side opens up questions that I don't have good answers for:
- Which device(s) should error records be written to?
All of them? Start with one and move on when it is
full? Write some types of records to one device?
Maybe based on the error type? We definitely need one device which should contain all the records, something like main pstore device.
Quoted 11 lines
If someone has a machine with multiple persistent storage devices - then we can talk about how to answer these questions.
2) "Why do you read in all the data from the device when it registers and save it in memory? Couldn't you just get the list of records and pick up the data from the device when the user reads the file?"
I don't think this is going to be very much data, just a few hundred kilobytes (i.e. less that $0.01 worth of memory, even expensive server memory). The memory is freed when the record is erased ... which is likely to be soon after boot.
This is actually a nice idea from the aspect that when those files appear in sysfs on boot, a userspace daemon can check for their existence and tell the user that she has valid error records from the last crash and she could report them to lkml/hw vendor/...
Quoted 6 lines
3) "
/sys/firmware/pstoreis the wrong pathname for this".
You are probably right. I put it under "firmware" because that's where the "efivars" driver put its top level directory. In my case the ERST back end is firmware, so there is some vague logic to it ... but better suggestions are welcome. Perhaps/sys/devices/platform/pstore?
Quoted 8 lines
4) "/sys is the wrong place for this."
Perhaps. I definitely want to use some sort of filesystem interface (so each record shows up as a file to the user). This seems a lot cleaner than trying to map the semantics of actual persistent storage devices onto a character device. The "sysfs_create_bin_file()" API seems very well designed for this usage. If not /sys, then where? "debugfs" would work - but not everyone mounts debugfs. Creating a whole new filesystem for this seems like overkill.
No, I think /sys is the right place for it being always present and all. Besides, for example, all the ACPI tables are there anyway (/sys/firmware/acpi/tables/) so pstore won't be the first blob there.
/sys/firmware might not be all that sensible if someone comes up with persistent storage type which is the network but we'll change that then.
Quoted 153 lines
5) "Why is the record identifier type 'u64'?"
This is one place where I knowingly let the ERST implementation bleed all the way up to the top - it uses 64-bit record numbers. It would be possible to map these to something smaller like "int" ... but the code to do so would be far larger than the memory saved. The most common usage case is likely to be a software crash with just one "dmesg" record.6) "Is this widely useful? How many systems have persistent storage?" Although ERST was only added to the ACPI spec earlier this year, it merely documents existing functionality required for WHEA (Windows Hardware Error Architecture). So most modern server systems should have it (my test system has it, and it has a BIOS written in mid 2008). Sorry desktops & laptops - no love for you here.
No-sign-off-yet-this-is-just-RFC
(quoted diff) +0
-2-Tony ---drivers/firmware/Kconfig +12
-0@@ -134,4 +134,16 @@ config ISCSI_IBFT detect iSCSI boot parameters dynamically during system boot, say Y. Otherwise, say N. +config PSTORE + tristate "Persistant store support via /sys" + default n + help + This option enables generic access to platform level persistent + storage via /sys/firmware/pstore. Only useful if you have a + platform level driver that registers with pstore to provide + the data, so you probably should just go say "Y" (or "M") to + a platform specific persistent store driver (e.g. ACPI_APEI on + X86) which will select this for you. If you don't have a platform + persistent store driver, say N. + endmenudrivers/firmware/Makefile +1
-0@@ -11,3 +11,4 @@ obj-$(CONFIG_DMIID) += dmi-id.o obj-$(CONFIG_ISCSI_IBFT_FIND) += iscsi_ibft_find.o obj-$(CONFIG_ISCSI_IBFT) += iscsi_ibft.o obj-$(CONFIG_FIRMWARE_MEMMAP) += memmap.o +obj-$(CONFIG_PSTORE) += pstore.odrivers/firmware/pstore.c new file +98
-0@@ -0,0 +1,313 @@ +/* +* Persistent Storage - pstore.c +* +* Copyright (C) 2010 Intel Corporation <[email protected]> +* +* This code is the generic layer to export data records from platform +* level persistent storage via sysfs. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <linux/atomic.h> +#include <linux/types.h> +#include <linux/errno.h> +#include <linux/init.h> +#include <linux/kmsg_dump.h> +#include <linux/module.h> +#include <linux/pstore.h> +#include <linux/string.h> +#include <linux/sysfs.h> +#include <linux/slab.h> +#include <linux/uaccess.h> + +MODULE_AUTHOR ("Tony Luck <[email protected]>" ); +MODULE_DESCRIPTION ("sysfs interface to persistent storage" ); +MODULE_LICENSE ("GPL" ); + +static DEFINE_SPINLOCK (pstore_lock); +static LIST_HEAD (pstore_list); +static struct kset *pstore_kset; + +#define PSTORE_NAMELEN 16 + +struct pstore_entry { +struct bin_attribute attr; +char name[PSTORE_NAMELEN ]; +u64 id; +int type; +int size; +struct list_head list; +char data[]; +}; + +static int pstore_create_sysfs_entry (struct pstore_entry *new_pstore); + +static struct pstore_info *psinfo; + +static char *pstore_buf; + +/* +* callback from kmsg_dump. (s2,l2) has the most recently +* written bytes, older bytes are in (s1,l1). Save as much +* as we can from the end of the buffer. +*/ +static void +pstore_dump (struct kmsg_dumper *dumper,enum kmsg_dump_reason reason, +const char *s1,unsigned long l1, +const char *s2,unsigned long l2) +{ +unsigned long s1_start, s2_start; +unsigned long l1_cpy, l2_cpy; +char *dst = pstore_buf + psinfo->header_size; + +/* Don't dump oopses to persistent store */ +if (reason ==KMSG_DUMP_OOPS ) +return ; + + l2_cpy =min (l2, psinfo->data_size); + l1_cpy =min (l1, psinfo->data_size - l2_cpy); + + s2_start = l2 - l2_cpy; + s1_start = l1 - l1_cpy; + +memcpy (dst, s1 + s1_start, l1_cpy); +memcpy (dst + l1_cpy, s2 + s2_start, l2_cpy); + + psinfo->writer (PSTORE_DMESG , pstore_buf, l1_cpy + l2_cpy); +} + +static struct kmsg_dumper pstore_dumper = { + .dump = pstore_dump, +}; + +/* +* platform specific persistent storage driver registers with +* us here. Read out all the records right away and install +* them in /sys. Register with kmsg_dump to save last part
drivers/firmware/pstore.c line 108
Quoted 10 lines
drivers/firmware/pstore.c new file +10
-0+* of console log on panic. +*/ +int +pstore_register (struct pstore_info *psi) +{ +struct pstore_entry *new_pstore; +int rc =0 , type; +unsigned long size; +u64 id; +unsigned long ps_maxsize;
Alignment here? Maybe something like this:
struct pstore_entry *new_pstore; unsigned long ps_maxsize; int rc = 0, type;
drivers/firmware/pstore.c line 116
Quoted 8 lines
drivers/firmware/pstore.c new file +8
-0+ +spin_lock (&pstore_lock); +if (psinfo) { +spin_unlock (&pstore_lock); +return -EBUSY ; + } + psinfo = psi; +spin_unlock (&pstore_lock);
Maybe make this lockless with cmpxchg? OTOH, the spinlock would be easier when you have multiple persistent storage devices...
drivers/firmware/pstore.c line 121
Quoted 4 lines
drivers/firmware/pstore.c new file +4
-0+ ps_maxsize = psi->header_size + psi->data_size + psi->footer_size; + pstore_buf =kzalloc (ps_maxsize,GFP_KERNEL ); +if (!pstore_buf) +return -ENOMEM ;
newline
Quoted 23 lines
drivers/firmware/pstore.c new file +23
-0+for (;;) { +if (psi->reader (&id, &type, pstore_buf, &size) <=0 ) +break ; + new_pstore =kzalloc (sizeof (struct pstore_entry ) + size, +GFP_KERNEL ); +if (!new_pstore) { + rc = -ENOMEM ; +break ; + } + new_pstore->id = id; + new_pstore->type = type; + new_pstore->size = size; +memcpy (new_pstore->data, pstore_buf + psi->header_size, size); +if (pstore_create_sysfs_entry (new_pstore)) { +kfree (new_pstore); + rc = -EINVAL ; +break ; + } + } + +kobject_uevent (&pstore_kset->kobj,KOBJ_ADD ); + +kmsg_dump_register (&pstore_dumper);
drivers/firmware/pstore.c line 154
Quoted 10 lines
drivers/firmware/pstore.c new file +10
-0+ +return rc; +} +EXPORT_SYMBOL_GPL (pstore_register); + +int +pstore_write (int type,char *buf,unsigned long size) +{ +if (!psinfo->writer) +return -ENODEV ;
I think you should move this check to the pstore_register() above. We don't want persistent storage backends which do not implement ->write anyway since the whole point of them becomes moot, no?
Quoted 57 lines
drivers/firmware/pstore.c new file +57
-0+if (size > psinfo->data_size) +return -EFBIG ; + +memcpy (pstore_buf + psinfo->header_size, buf, size); +return psinfo->writer (type, pstore_buf, size); +} +EXPORT_SYMBOL_GPL (pstore_write); + +#define to_pstore_entry (obj) container_of(obj, struct pstore_entry, attr) + +/* +* "read" function for files containing persistent store records +*/ +static ssize_t pstore_show (struct file *filp,struct kobject *kobj, +struct bin_attribute *bin_attr,char *buf, +loff_t offset,size_t count) +{ +struct pstore_entry *ps =to_pstore_entry (bin_attr); + +return memory_read_from_buffer (buf, count, &offset, + ps->data, ps->size); +} + +/* +* Erase records by writing their filename to the "erase" file. E.g. +* # echo "dmesg-0" > erase +*/ +static ssize_t pstore_erase (struct file *filp,struct kobject *kobj, +struct bin_attribute *bin_attr, +char *buf,loff_t pos,size_t count) +{ +struct pstore_entry *search_pstore, *n; +int len1, len2, found =0 ; + + len1 = count; +if (buf[len1 -1 ] =='\n' ) + len1--; + +spin_lock (&pstore_lock); + +/* +* Find this record +*/ +list_for_each_entry_safe (search_pstore, n, &pstore_list, list) { + len2 =strlen (search_pstore->name); +if (len1 == len2 &&memcmp (buf, search_pstore->name, + len1) ==0 ) { + found =1 ; +break ; + } + } +if (!found) { +spin_unlock (&pstore_lock); +return -EINVAL ; + } + +if (psinfo->eraser)
drivers/firmware/pstore.c line 221
Quoted 10 lines
drivers/firmware/pstore.c new file +10
-0+if (psinfo->eraser (search_pstore->id)) { +spin_unlock (&pstore_lock); +return -EIO ; + } + +list_del (&search_pstore->list); + +spin_unlock (&pstore_lock); + +sysfs_remove_bin_file (&pstore_kset->kobj, &search_pstore->attr);
AFAICT, you might want to remove the sysfs file _before_ you remove it from the pstore list/erase its contents, otherwise concurrent accesses to it from userspace readers might make us go boom.
Quoted 74 lines
drivers/firmware/pstore.c new file +74
-0+ +return count; +} + +static struct bin_attribute attr_erase = { + .attr = {.name ="erase" , .mode =0200 }, + .write = pstore_erase, +}; + +static int +pstore_create_sysfs_entry (struct pstore_entry *new_pstore) +{ +static atomic_t next; +int error, seq; + + seq =atomic_add_return (1 , &next); + +switch (new_pstore->type) { +case PSTORE_DMESG : +sprintf (new_pstore->name,"dmesg-%d" , seq); +break ; +case PSTORE_MCE : +sprintf (new_pstore->name,"mce-%d" , seq); +break ; +default : +sprintf (new_pstore->name,"type%d-%d" , new_pstore->type, seq); +break ; + } + +sysfs_attr_init (&new_pstore->attr.attr); + new_pstore->attr.size =0 ; + new_pstore->attr.read = pstore_show; + new_pstore->attr.attr.name = new_pstore->name; + new_pstore->attr.attr.mode =0444 ; + error =sysfs_create_bin_file (&pstore_kset->kobj, &new_pstore->attr); +if (!error) { +spin_lock (&pstore_lock); +list_add (&new_pstore->list, &pstore_list); +spin_unlock (&pstore_lock); + } +return error; +} + +static int __init +pstore_init (void ) +{ +int error =0 ; + +/* Register the pstore directory at /sys/firmware/pstore */ + pstore_kset =kset_create_and_add ("pstore" ,NULL , firmware_kobj); +if (!pstore_kset) { +printk (KERN_ERR "pstore: Subsystem registration failed.\n" ); +return -ENOMEM ; + } + +/* +* Add attribute to allow records to be erased from persistent store +*/ + error =sysfs_create_bin_file (&pstore_kset->kobj, + &attr_erase); +if (error) { +printk (KERN_ERR "pstore: unable to create 'erase' sysfs file" +" due to error %d\n" , error); +kset_unregister (pstore_kset); + } + +return error; +} + +static void __exit +pstore_exit (void ) +{ +struct pstore_entry *entry, *n; +
drivers/firmware/pstore.c line 305
Quoted 10 lines
drivers/firmware/pstore.c new file +10
-0+if (psinfo) +kmsg_dump_unregister (&pstore_dumper); + +list_for_each_entry_safe (entry, n, &pstore_list, list) { +spin_lock (&pstore_lock); +list_del (&entry->list); +spin_unlock (&pstore_lock); +sysfs_remove_bin_file (&pstore_kset->kobj, &entry->attr); + } +sysfs_remove_bin_file (&pstore_kset->kobj, &attr_erase);
ditto.
Quoted 31 lines
drivers/firmware/pstore.c new file +8
-0+ +kset_unregister (pstore_kset); + +kfree (pstore_buf); +} + +module_init (pstore_init); +module_exit (pstore_exit);include/linux/pstore.h new file +17
-0@@ -0,0 +1,54 @@ +/* +* Persistent Storage - pstore.h +* +* Copyright (C) 2010 Intel Corporation <[email protected]> +* +* This code is the generic layer to export data records from platform +* level persistent storage via sysfs. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*
include/linux/pstore.h line 27
Quoted 10 lines
include/linux/pstore.h new file +10
-0+* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +#ifndef _LINUX_PSTORE_H +#define _LINUX_PSTORE_H + +/* types */ +#define PSTORE_DMESG 0 +#define PSTORE_MCE 1
maybe PSTORE_TYPE_DMESG/PSTORE_TYPE_MCE ?
Quoted 18 lines
include/linux/pstore.h new file +18
-0+ +struct pstore_info { +unsigned long header_size; +unsigned long data_size; +unsigned long footer_size; +int (*reader)(u64 *id,int *type,char *buf,unsigned long *size); +int (*writer)(int type,char *buf,unsigned long size); +int (*eraser)(u64 id); +}; + +#if defined(CONFIG_PSTORE ) || defined(CONFIG_PSTORE_MODULE ) +extern int pstore_register (struct pstore_info *); +extern int pstore_write (int type,char *buf,unsigned long size); +#else +static inline int +pstore_register (struct pstore_info *psi) +{ +return -ENODEV ;
include/linux/pstore.h
Quoted 10 lines
include/linux/pstore.h new file +9
-1+} +static inline int +pstore_write (int type,char *buf,unsigned long size) +{ +return -ENODEV ; +} +#endif + +#endif /*_LINUX_PSTORE_H*/ --
Thanks.
Signature · 2 lines
Regards/Gruss,
Boris.From: "Luck, Tony" <[email protected]>
Date: Sat, 20 Nov 2010 15:48:19 -0800
Quoted 7 lines
The usage model I'm envisioning is that a platform driver will register with this code to provide the actual storage. I've tried to make this interface general, but I'm working from a sample of one (the ACPI ERST code), so if anyone else has some persistent store that can't be handled by this code, speak up and we can put in the necessary tweaks.
Quoted 6 lines
My assumptions are that the data that Linux cares about will be wrapped in some error record structure with a header, and possibly a footer that the device code needs. So the driver specifies how much padding to put around a buffer to make life easy for it. It also specifies the maximum number of bytes that can be saved in one record.
Thanks for doing this work Tony.
On sparc64 I can mark an arbitrary region of physical memory as persistent across soft reboots. So I'll likely use that to implement these interfaces.
Quoted 1 line · Borislav Petkov
this is actually very cool.
Thanks for looking at it (and more thanks for this endorsement!)
Quoted 11 lines
Quoted 8 lines
1) "Why do you only allow one platform driver to register?"
I only have one such driver. Adding more is easy from the "read" side (just collect all the records from all devices and remember where they came from so you can call the correct "eraser" function). But the "write" side opens up questions that I don't have good answers for:
- Which device(s) should error records be written to?
All of them? Start with one and move on when it is
full? Write some types of records to one device?Maybe based on the error type? We definitely need one device which should contain all the records, something like main pstore device.
But who decides which records go where? And which device gets to be the "main" one? I don't think that we can usefully do this in the registration mechanism (how does a driver know that other drivers even exist?). I continue to want to defer this until someone with two or more devices on one machine steps forward.
Quoted 15 lines
Quoted 6 lines
3) "
/sys/firmware/pstoreis the wrong pathname for this".
You are probably right. I put it under "firmware" because that's where the "efivars" driver put its top level directory. In my case the ERST back end is firmware, so there is some vague logic to it ... but better suggestions are welcome. Perhaps/sys/devices/platform/pstore?Quoted 8 lines
4) "/sys is the wrong place for this."
Perhaps. I definitely want to use some sort of filesystem interface (so each record shows up as a file to the user). This seems a lot cleaner than trying to map the semantics of actual persistent storage devices onto a character device. The "sysfs_create_bin_file()" API seems very well designed for this usage. If not /sys, then where? "debugfs" would work - but not everyone mounts debugfs. Creating a whole new filesystem for this seems like overkill.
Quoted 3 lines
No, I think /sys is the right place for it being always present and all. Besides, for example, all the ACPI tables are there anyway (
/sys/firmware/acpi/tables/) so pstore won't be the first blob there.
Heh! The acpi tables code is where I found out how easy it was to handle blobs bigger than PAGE_SIZE using memory_read_from_buffer().
Quoted 3 lines
/sys/firmwaremight not be all that sensible if someone comes up with persistent storage type which is the network but we'll change that then.
I'd like to get the right place first time - change means having to update any applications that coded in the pathname.
Quoted 11 lines
drivers/firmware/pstore.c line 108
Quoted 8 lines
drivers/firmware/pstore.c new file +8
-0+int +pstore_register (struct pstore_info *psi) +{ + struct pstore_entry *new_pstore; + int rc = 0, type; + unsigned long size; + u64 id; + unsigned long ps_maxsize;Alignment here? Maybe something like this:
Quoted 3 lines
struct
pstore_entry*new_pstore;
unsigned longps_maxsize;
int rc = 0, type;
Are you talking about textual alignment of the declarations? Yours does look prettier.
Quoted 11 lines
Quoted 8 lines
+
+spin_lock(&pstore_lock);
+ if (psinfo) {
+spin_unlock(&pstore_lock);
+ return-EBUSY;
+ }
+ psinfo = psi;
+spin_unlock(&pstore_lock);Maybe make this lockless with cmpxchg? OTOH, the spinlock would be easier when you have multiple persistent storage devices...
cmpxchg would make the code shorter - I'll try recoding and see if I like the way it looks.
Quoted 6 lines
Quoted 4 lines
+
ps_maxsize=psi->header_size+psi->data_size+psi->footer_size;
+pstore_buf=kzalloc(ps_maxsize, GFP_KERNEL);
+ if (!pstore_buf)
+ return-ENOMEM;newline
Yup. Will fix.
Quoted 9 lines
drivers/firmware/pstore.c line 154
Quoted 5 lines
drivers/firmware/pstore.c new file +5
-0+int +pstore_write (int type,char *buf,unsigned long size) +{ + if (!psinfo->writer) + return -ENODEV;I think you should move this check to the
pstore_register()above. We don't want persistent storage backends which do not implement ->write anyway since the whole point of them becomes moot, no?
Doh! Yes, of course. Will fix.
Quoted 9 lines
drivers/firmware/pstore.c line 221
Quoted 5 lines
+
list_del(&search_pstore->list);drivers/firmware/pstore.c new file +4
-0+ + spin_unlock(&pstore_lock); + + sysfs_remove_bin_file(&pstore_kset->kobj, &search_pstore->attr);AFAICT, you might want to remove the sysfs file _before_ you remove it from the pstore list/erase its contents, otherwise concurrent accesses to it from userspace readers might make us go boom.
I'll think about the ordering. I have three things to do here:
1) Remove from the pstore_list
2) Call platform driver to erase from store
3) Call sysfs to remove the binfile.
Potentially the erase could fail ... and I should probably notice that and do something.
Quoted 5 lines
include/linux/pstore.h line 27
Quoted 3 lines
include/linux/pstore.h new file +3
-0+/* types */ +#define PSTORE_DMESG 0 +#define PSTORE_MCE 1maybe PSTORE_TYPE_DMESG/PSTORE_TYPE_MCE ?
Much better (I suck at naming things). Will fix.
-Tony
Hi, Tony,
Quoted 13 lines · Tony
Here's a patch based on some discussions I had with Thomas Gleixner at plumbers conference that implements a generic layer for persistent storage usable to pass tens or hundreds of kilobytes of data from the dying breath of a crashing kernel to its successor.
The usage model I'm envisioning is that a platform driver will register with this code to provide the actual storage. I've tried to make this interface general, but I'm working from a sample of one (the ACPI ERST code), so if anyone else has some persistent store that can't be handled by this code, speak up and we can put in the necessary tweaks.
Quoted 6 lines
My assumptions are that the data that Linux cares about will be wrapped in some error record structure with a header, and possibly a footer that the device code needs. So the driver specifies how much padding to put around a buffer to make life easy for it. It also specifies the maximum number of bytes that can be saved in one record.
This patch provides a general "read" interface for kmsg_dumper and some other persistent storage users. Another possible choice is to just extend the original interface to add persistent store support. For example, we can add a "read" function in kmsg_dumper, and output the content of persistent store via extend /dev/kmsg via prefix every line comes from persistent store or adding some "ioctl" to do that. (But it seems that nobody likes "ioctl").
Quoted 8 lines
There are three callback functions from the generic code to the driver:
"reader" which iterates over all records currently in the store - returning type, size and a record identifier as well as the actual data.
"writer" which writes a record with a type to the persistent store
I think it is necessary to require this to be NMI safe (in comments?), because hardware error handler may need to write to persistent storage in NMI context. Or we can add a "flag" field to let storage provider advocate its capability of NMI safe.
Quoted 24 lines
"eraser" which takes a record identifier, and clears that item from the store.
The Linux user visible interface is via /sys (similar to the "efivars" interface)
# ls -l /sys/firmware/pstore total 0 -r--r--r-- 1 root root 0 2010-11-20 11:03 dmesg-0 --w------- 1 root root 0 2010-11-20 11:03 eraseThe "type" of error record I mentioned earlier is used to name the files ... saved console logs from
kmsg_dmp()are named with a "dmesg" prefix as shown above.Once an error record has been viewed, analysed, saved. The user can request it to be cleared by writing its name to the "erase" file:
# echo "dmesg-0" > eraseAnswers to a few questions that I think you might ask:
Quoted 8 lines
1) "Why do you only allow one platform driver to register?"
I only have one such driver. Adding more is easy from the "read" side (just collect all the records from all devices and remember where they came from so you can call the correct "eraser" function). But the "write" side opens up questions that I don't have good answers for:
- Which device(s) should error records be written to?
All of them? Start with one and move on when it is
full? Write some types of records to one device?
The persistent storage may be full, and the writing may fail. So I think we can just try to write one by one, until the first success writing.
[...]
Quoted 33 lines
+ * Erase records by writing their filename to the "erase" file. E.g.
+ * # echo "dmesg-0" > erase
+ */drivers/firmware/pstore.c new file +30
-0+static ssize_t pstore_erase (struct file *filp,struct kobject *kobj, +struct bin_attribute *bin_attr, +char *buf,loff_t pos,size_t count) +{ +struct pstore_entry *search_pstore, *n; +int len1, len2, found =0 ; + + len1 = count; +if (buf[len1 -1 ] =='\n' ) + len1--; + +spin_lock (&pstore_lock); + +/* +* Find this record +*/ +list_for_each_entry_safe (search_pstore, n, &pstore_list, list) { + len2 =strlen (search_pstore->name); +if (len1 == len2 &&memcmp (buf, search_pstore->name, + len1) ==0 ) { + found =1 ; +break ; + } + } +if (!found) { +spin_unlock (&pstore_lock); +return -EINVAL ; + } + +if (psinfo->eraser)
drivers/firmware/pstore.c line 221
Quoted 10 lines
drivers/firmware/pstore.c new file +10
-0+if (psinfo->eraser (search_pstore->id)) { +spin_unlock (&pstore_lock); +return -EIO ; + } + +list_del (&search_pstore->list); + +spin_unlock (&pstore_lock); + +sysfs_remove_bin_file (&pstore_kset->kobj, &search_pstore->attr);
It seems that the corresponding memory is not freed after erasing.
Best Regards,
Huang Ying
Quoted 9 lines · Tony Luck
Quoted 9 lines
Quoted 8 lines
1) "Why do you only allow one platform driver to register?"
I only have one such driver. Adding more is easy from the "read" side (just collect all the records from all devices and remember where they came from so you can call the correct "eraser" function). But the "write" side opens up questions that I don't have good answers for:
- Which device(s) should error records be written to?
All of them? Start with one and move on when it is
full? Write some types of records to one device?
Quoted 8 lines
Quoted 2 lines
Maybe based on the error type? We definitely need one device which should contain all the records, something like main pstore device.
But who decides which records go where? And which device gets to be the "main" one? I don't think that we can usefully do this in the registration mechanism (how does a driver know that other drivers even exist?). I continue to want to defer this until someone with two or more devices on one machine steps forward.
Yeah, let's wait and see.
[..]
Quoted 5 lines
Quoted 2 lines
/sys/firmwaremight not be all that sensible if someone comes up with persistent storage type which is the network but we'll change that then.I'd like to get the right place first time - change means having to update any applications that coded in the pathname.
True, true.
Quoted 11 lines
Quoted 11 lines
drivers/firmware/pstore.c line 108
Quoted 8 lines
drivers/firmware/pstore.c new file +8
-0+int +pstore_register (struct pstore_info *psi) +{ + struct pstore_entry *new_pstore; + int rc = 0, type; + unsigned long size; + u64 id; + unsigned long ps_maxsize;Alignment here? Maybe something like this:
Quoted 6 lines
Quoted 3 lines
struct
pstore_entry*new_pstore;
unsigned longps_maxsize;
int rc = 0, type;Are you talking about textual alignment of the declarations? Yours does look prettier.
Yep, textual alignment.
[..]
Quoted 10 lines
Quoted 9 lines
drivers/firmware/pstore.c line 221
Quoted 5 lines
+
list_del(&search_pstore->list);drivers/firmware/pstore.c new file +4
-0+ + spin_unlock(&pstore_lock); + + sysfs_remove_bin_file(&pstore_kset->kobj, &search_pstore->attr);AFAICT, you might want to remove the sysfs file _before_ you remove it from the pstore list/erase its contents, otherwise concurrent accesses to it from userspace readers might make us go boom.
Quoted 7 lines
I'll think about the ordering. I have three things to do here:
1) Remove from thepstore_list
2) Call platform driver to erase from store
3) Call sysfs to remove the binfile.Potentially the erase could fail ... and I should probably notice that and do something.
Right, and I was also wondering what might happen if userspace accesses the bin attribute before you've removed it from the sysfs hierarchy but after erasing its backing storage in the firmware?
AFAICT, it could be that those ps->data and ps->size members which are accessed in pstore_show() (which is called when the bin attribute is read from /sysfs) after the block of data is erased by platform driver, might contain crap data and memory_read_from_buffer() could do some illegal accesses to some kernel memory if not cleared properly by the ->erase() routine.
OTOH, you might want to do the clearing here and leave the platform driver as dumb as possible by having a lower level __pstore_erase() wrapper or similar.
Do you see my point? (I could very well be completely offcourse too :))
Thanks.
Signature · 2 lines
Regards/Gruss,
Boris.Quoted 1 line · Borislav Petkov
Do you see my point? (I could very well be completely offcourse too :))
Yes I was :)
ps->data still points to the corresponding kernel buffer. And you keep it around until the driver is removed/machine shutdown.
Signature · 2 lines
Regards/Gruss,
Boris.Quoted 10 lines
Quoted 5 lines
"reader" which iterates over all records currently in the store - returning type, size and a record identifier as well as the actual data.
"writer" which writes a record with a type to the persistent store
I think it is necessary to require this to be NMI safe (in comments?), because hardware error handler may need to write to persistent storage in NMI context. Or we can add a "flag" field to let storage provider advocate its capability of NMI safe.
One thing we need for some of the driver code sitting in the pending pile is support for hardware assisted logging, where record numberes are handed out by a device register access.
Quoted 7 lines
Quoted 7 lines
The "type" of error record I mentioned earlier is used to name the files ... saved console logs from
kmsg_dmp()are named with a "dmesg" prefix as shown above.Once an error record has been viewed, analysed, saved. The user can request it to be cleared by writing its name to the "erase" file:
How will fragmentation be handled ?
Quoted 2 lines
The persistent storage may be full, and the writing may fail. So I think we can just try to write one by one, until the first success writing.
If you intend to treat it basically like an fs why not just make it a file system. No more weird echo erase into magic sysfs file node you can just use "rm".
The logging off an interrupt isn't a big problem either as you can have a pool of blocks which are preallocated for the log files and more can be stitched into the files asynchronously after the interrupt logging events. For that matter you can keep it as a circular log just easily but present it as a file system.
Quoted 3 lines
[...]
Quoted 2 lines
+ * Erase records by writing their filename to the "erase" file. E.g.
+ * # echo "dmesg-0" > erase
rm dmesg-0
We have a model for this and filesystem indirected via sysfs commands is in the daft category.
Tony Luck
reviewerQuoted 7 lines · Huang Ying
This patch provides a general "read" interface for
kmsg_dumperand some other persistent storage users. Another possible choice is to just extend the original interface to add persistent store support. For example, we can add a "read" function inkmsg_dumper, and output the content of persistent store via extend/dev/kmsgvia prefix every line comes from persistent store or adding some "ioctl" to do that. (But it seems that nobody likes "ioctl").
In Linux (and Unix before it) "everything is a file" ... but this doesn't work very well if the file has internal structure (e.g. is made of records that can be individually changed or deleted). A filesystem seems a much better model.
Quoted 6 lines
Quoted 1 line
"writer" which writes a record with a type to the persistent store
I think it is necessary to require this to be NMI safe (in comments?), because hardware error handler may need to write to persistent storage in NMI context. Or we can add a "flag" field to let storage provider advocate its capability of NMI safe.
I can add a comment to pstore.h to document the NMI-safe requirement for the "writer" function.
Quoted 6 lines
Quoted 3 lines
- Which device(s) should error records be written to? All of them? Start with one and move on when it is full? Write some types of records to one device?
The persistent storage may be full, and the writing may fail. So I think we can just try to write one by one, until the first success writing.
A good option - if we ever find someone luck enough to have more than one persistent store device.
Quoted 3 lines
Quoted 1 line
+
sysfs_remove_bin_file(&pstore_kset->kobj, &search_pstore->attr);It seems that the corresponding memory is not freed after erasing.
Ouch! Good catch. Will add a kfree()
-Tony
Tony Luck
reviewerQuoted 10 lines · Alan Cox
Quoted 6 lines
Quoted 1 line
"writer" which writes a record with a type to the persistent store
I think it is necessary to require this to be NMI safe (in comments?), because hardware error handler may need to write to persistent storage in NMI context. Or we can add a "flag" field to let storage provider advocate its capability of NMI safe.
One thing we need for some of the driver code sitting in the pending pile is support for hardware assisted logging, where record numberes are handed out by a device register access.
I don't follow you here ... perhaps we have a different idea of what "record numbers" are? I'm thinking they are purely an internal implementation detail of a platform level pstore driver ... but the generic code needs to use them as an opaque object holding them from the result of a read and using them when requesting an erase. What usage are you thinking of that needs this device register access?
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Once an error record has been viewed, analysed, saved. The user can request it to be cleared by writing its name to the "erase" file:
How will fragmentation be handled ?
This is left as an exercise for the platform level diver (or in the case of ERST, it is handled by the firmware that implements ERST). In general this shouldn't be a hard problem because I think the usual use case will be that just after boot we will run some processes that look at, analyse, save and clear all the error records. Different types of records may be handled by different processes, so there can be some short term fragmentation when one type of record has been erased, but the remainder have not yet been processed.
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
+ * Erase records by writing their filename to the "erase" file. E.g.
+ * # echo "dmesg-0" > eraserm dmesg-0
We have a model for this and filesystem indirected via sysfs commands is in the daft category.
Yup - totally mad. My sysfs-fu isn't up to figuring out how to get a notification on unlink(2). Is that possible? It would be a much superior metaphor.
-Tony
Quoted 9 lines · Tony Luck
Quoted 7 lines · Huang Ying
This patch provides a general "read" interface for
kmsg_dumperand some other persistent storage users. Another possible choice is to just extend the original interface to add persistent store support. For example, we can add a "read" function inkmsg_dumper, and output the content of persistent store via extend/dev/kmsgvia prefix every line comes from persistent store or adding some "ioctl" to do that. (But it seems that nobody likes "ioctl").
Quoted 4 lines
In Linux (and Unix before it) "everything is a file" ... but this doesn't work very well if the file has internal structure (e.g. is made of records that can be individually changed or deleted). A filesystem seems a much better model.
Any tangents with "pramfs: persistent and protected RAM filesystem" on linux-embedded?
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 2 lines · Geert Uytterhoeven
Any tangents with "pramfs: persistent and protected RAM filesystem" on linux-embedded?
That looks nice for building on top on a sane h/w design (a block of non-volatile RAM). My pstore code works with uglier things. The ACPI/ERST interface provides ways to read/write/clear "records" from what might be some NVRAM, but might also be a channel to a service processor ... and ERST is what I'd like to see made usable (without subjecting users to the implementation details).
-Tony