1ca01d6ddSTony Luck /* 2ca01d6ddSTony Luck * Persistent Storage - platform driver interface parts. 3ca01d6ddSTony Luck * 4f29e5956SAnton Vorontsov * Copyright (C) 2007-2008 Google, Inc. 5ca01d6ddSTony Luck * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com> 6ca01d6ddSTony Luck * 7ca01d6ddSTony Luck * This program is free software; you can redistribute it and/or modify 8ca01d6ddSTony Luck * it under the terms of the GNU General Public License version 2 as 9ca01d6ddSTony Luck * published by the Free Software Foundation. 10ca01d6ddSTony Luck * 11ca01d6ddSTony Luck * This program is distributed in the hope that it will be useful, 12ca01d6ddSTony Luck * but WITHOUT ANY WARRANTY; without even the implied warranty of 13ca01d6ddSTony Luck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14ca01d6ddSTony Luck * GNU General Public License for more details. 15ca01d6ddSTony Luck * 16ca01d6ddSTony Luck * You should have received a copy of the GNU General Public License 17ca01d6ddSTony Luck * along with this program; if not, write to the Free Software 18ca01d6ddSTony Luck * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19ca01d6ddSTony Luck */ 20ca01d6ddSTony Luck 21ca01d6ddSTony Luck #include <linux/atomic.h> 22ca01d6ddSTony Luck #include <linux/types.h> 23ca01d6ddSTony Luck #include <linux/errno.h> 24ca01d6ddSTony Luck #include <linux/init.h> 25ca01d6ddSTony Luck #include <linux/kmsg_dump.h> 26f29e5956SAnton Vorontsov #include <linux/console.h> 27ca01d6ddSTony Luck #include <linux/module.h> 28ca01d6ddSTony Luck #include <linux/pstore.h> 29ca01d6ddSTony Luck #include <linux/string.h> 306dda9266SLuck, Tony #include <linux/timer.h> 31ca01d6ddSTony Luck #include <linux/slab.h> 32ca01d6ddSTony Luck #include <linux/uaccess.h> 33abd4d558SDon Zickus #include <linux/hardirq.h> 34a3f5f075SAnton Vorontsov #include <linux/jiffies.h> 356dda9266SLuck, Tony #include <linux/workqueue.h> 36ca01d6ddSTony Luck 37ca01d6ddSTony Luck #include "internal.h" 38ca01d6ddSTony Luck 39ca01d6ddSTony Luck /* 406dda9266SLuck, Tony * We defer making "oops" entries appear in pstore - see 416dda9266SLuck, Tony * whether the system is actually still running well enough 426dda9266SLuck, Tony * to let someone see the entry 436dda9266SLuck, Tony */ 44521f7288SAnton Vorontsov static int pstore_update_ms = -1; 45a3f5f075SAnton Vorontsov module_param_named(update_ms, pstore_update_ms, int, 0600); 46a3f5f075SAnton Vorontsov MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " 47521f7288SAnton Vorontsov "(default is -1, which means runtime updates are disabled; " 48521f7288SAnton Vorontsov "enabling this option is not safe, it may lead to further " 49521f7288SAnton Vorontsov "corruption on Oopses)"); 506dda9266SLuck, Tony 516dda9266SLuck, Tony static int pstore_new_entry; 526dda9266SLuck, Tony 536dda9266SLuck, Tony static void pstore_timefunc(unsigned long); 546dda9266SLuck, Tony static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0); 556dda9266SLuck, Tony 566dda9266SLuck, Tony static void pstore_dowork(struct work_struct *); 576dda9266SLuck, Tony static DECLARE_WORK(pstore_work, pstore_dowork); 586dda9266SLuck, Tony 596dda9266SLuck, Tony /* 60ca01d6ddSTony Luck * pstore_lock just protects "psinfo" during 61ca01d6ddSTony Luck * calls to pstore_register() 62ca01d6ddSTony Luck */ 63ca01d6ddSTony Luck static DEFINE_SPINLOCK(pstore_lock); 64060287b8SAnton Vorontsov struct pstore_info *psinfo; 65ca01d6ddSTony Luck 66dee28e72SMatthew Garrett static char *backend; 67dee28e72SMatthew Garrett 68366f7e7aSLuck, Tony /* How much of the console log to snapshot */ 69ca01d6ddSTony Luck static unsigned long kmsg_bytes = 10240; 70ca01d6ddSTony Luck 71366f7e7aSLuck, Tony void pstore_set_kmsg_bytes(int bytes) 72ca01d6ddSTony Luck { 73366f7e7aSLuck, Tony kmsg_bytes = bytes; 74ca01d6ddSTony Luck } 75ca01d6ddSTony Luck 76ca01d6ddSTony Luck /* Tag each group of saved records with a sequence number */ 77ca01d6ddSTony Luck static int oopscount; 78ca01d6ddSTony Luck 79381b872cSSeiji Aguchi static const char *get_reason_str(enum kmsg_dump_reason reason) 80381b872cSSeiji Aguchi { 81381b872cSSeiji Aguchi switch (reason) { 82381b872cSSeiji Aguchi case KMSG_DUMP_PANIC: 83381b872cSSeiji Aguchi return "Panic"; 84381b872cSSeiji Aguchi case KMSG_DUMP_OOPS: 85381b872cSSeiji Aguchi return "Oops"; 86381b872cSSeiji Aguchi case KMSG_DUMP_EMERG: 87381b872cSSeiji Aguchi return "Emergency"; 88381b872cSSeiji Aguchi case KMSG_DUMP_RESTART: 89381b872cSSeiji Aguchi return "Restart"; 90381b872cSSeiji Aguchi case KMSG_DUMP_HALT: 91381b872cSSeiji Aguchi return "Halt"; 92381b872cSSeiji Aguchi case KMSG_DUMP_POWEROFF: 93381b872cSSeiji Aguchi return "Poweroff"; 94381b872cSSeiji Aguchi default: 95381b872cSSeiji Aguchi return "Unknown"; 96381b872cSSeiji Aguchi } 97381b872cSSeiji Aguchi } 989f6af27fSTony Luck 999f244e9cSSeiji Aguchi bool pstore_cannot_block_path(enum kmsg_dump_reason reason) 1009f244e9cSSeiji Aguchi { 1019f244e9cSSeiji Aguchi /* 1029f244e9cSSeiji Aguchi * In case of NMI path, pstore shouldn't be blocked 1039f244e9cSSeiji Aguchi * regardless of reason. 1049f244e9cSSeiji Aguchi */ 1059f244e9cSSeiji Aguchi if (in_nmi()) 1069f244e9cSSeiji Aguchi return true; 1079f244e9cSSeiji Aguchi 1089f244e9cSSeiji Aguchi switch (reason) { 1099f244e9cSSeiji Aguchi /* In panic case, other cpus are stopped by smp_send_stop(). */ 1109f244e9cSSeiji Aguchi case KMSG_DUMP_PANIC: 1119f244e9cSSeiji Aguchi /* Emergency restart shouldn't be blocked by spin lock. */ 1129f244e9cSSeiji Aguchi case KMSG_DUMP_EMERG: 1139f244e9cSSeiji Aguchi return true; 1149f244e9cSSeiji Aguchi default: 1159f244e9cSSeiji Aguchi return false; 1169f244e9cSSeiji Aguchi } 1179f244e9cSSeiji Aguchi } 1189f244e9cSSeiji Aguchi EXPORT_SYMBOL_GPL(pstore_cannot_block_path); 1199f244e9cSSeiji Aguchi 120ca01d6ddSTony Luck /* 121ca01d6ddSTony Luck * callback from kmsg_dump. (s2,l2) has the most recently 122ca01d6ddSTony Luck * written bytes, older bytes are in (s1,l1). Save as much 123ca01d6ddSTony Luck * as we can from the end of the buffer. 124ca01d6ddSTony Luck */ 125ca01d6ddSTony Luck static void pstore_dump(struct kmsg_dumper *dumper, 126e2ae715dSKay Sievers enum kmsg_dump_reason reason) 127ca01d6ddSTony Luck { 128e2ae715dSKay Sievers unsigned long total = 0; 129381b872cSSeiji Aguchi const char *why; 130ca01d6ddSTony Luck u64 id; 131b94fdd07SMatthew Garrett unsigned int part = 1; 132abd4d558SDon Zickus unsigned long flags = 0; 133abd4d558SDon Zickus int is_locked = 0; 134e2ae715dSKay Sievers int ret; 135ca01d6ddSTony Luck 136381b872cSSeiji Aguchi why = get_reason_str(reason); 1379f6af27fSTony Luck 1389f244e9cSSeiji Aguchi if (pstore_cannot_block_path(reason)) { 1399f244e9cSSeiji Aguchi is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags); 1409f244e9cSSeiji Aguchi if (!is_locked) { 1419f244e9cSSeiji Aguchi pr_err("pstore dump routine blocked in %s path, may corrupt error record\n" 1429f244e9cSSeiji Aguchi , in_nmi() ? "NMI" : why); 1439f244e9cSSeiji Aguchi } 144abd4d558SDon Zickus } else 145abd4d558SDon Zickus spin_lock_irqsave(&psinfo->buf_lock, flags); 146ca01d6ddSTony Luck oopscount++; 147ca01d6ddSTony Luck while (total < kmsg_bytes) { 148e2ae715dSKay Sievers char *dst; 149e2ae715dSKay Sievers unsigned long size; 150e2ae715dSKay Sievers int hsize; 151e2ae715dSKay Sievers size_t len; 152e2ae715dSKay Sievers 153ca01d6ddSTony Luck dst = psinfo->buf; 15456280682SMatthew Garrett hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part); 155ca01d6ddSTony Luck size = psinfo->bufsize - hsize; 156ca01d6ddSTony Luck dst += hsize; 157ca01d6ddSTony Luck 158e2ae715dSKay Sievers if (!kmsg_dump_get_buffer(dumper, true, dst, size, &len)) 159ca01d6ddSTony Luck break; 160ca01d6ddSTony Luck 1613d6d8d20SKees Cook ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part, 162755d4fe4SSeiji Aguchi oopscount, hsize + len, psinfo); 163b238b8faSChen Gong if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) 1646dda9266SLuck, Tony pstore_new_entry = 1; 165e2ae715dSKay Sievers 166e2ae715dSKay Sievers total += hsize + len; 16756280682SMatthew Garrett part++; 168ca01d6ddSTony Luck } 1699f244e9cSSeiji Aguchi if (pstore_cannot_block_path(reason)) { 170abd4d558SDon Zickus if (is_locked) 1719f244e9cSSeiji Aguchi spin_unlock_irqrestore(&psinfo->buf_lock, flags); 172abd4d558SDon Zickus } else 173abd4d558SDon Zickus spin_unlock_irqrestore(&psinfo->buf_lock, flags); 174ca01d6ddSTony Luck } 175ca01d6ddSTony Luck 176ca01d6ddSTony Luck static struct kmsg_dumper pstore_dumper = { 177ca01d6ddSTony Luck .dump = pstore_dump, 178ca01d6ddSTony Luck }; 179ca01d6ddSTony Luck 180f29e5956SAnton Vorontsov #ifdef CONFIG_PSTORE_CONSOLE 181f29e5956SAnton Vorontsov static void pstore_console_write(struct console *con, const char *s, unsigned c) 182f29e5956SAnton Vorontsov { 183f29e5956SAnton Vorontsov const char *e = s + c; 184f29e5956SAnton Vorontsov 185f29e5956SAnton Vorontsov while (s < e) { 186f29e5956SAnton Vorontsov unsigned long flags; 18770a6f46dSColin Ian King u64 id; 188f29e5956SAnton Vorontsov 189f29e5956SAnton Vorontsov if (c > psinfo->bufsize) 190f29e5956SAnton Vorontsov c = psinfo->bufsize; 19180c9d03cSChuansheng Liu 19280c9d03cSChuansheng Liu if (oops_in_progress) { 19380c9d03cSChuansheng Liu if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) 19480c9d03cSChuansheng Liu break; 19580c9d03cSChuansheng Liu } else { 196f29e5956SAnton Vorontsov spin_lock_irqsave(&psinfo->buf_lock, flags); 19780c9d03cSChuansheng Liu } 198f29e5956SAnton Vorontsov memcpy(psinfo->buf, s, c); 199755d4fe4SSeiji Aguchi psinfo->write(PSTORE_TYPE_CONSOLE, 0, &id, 0, 0, c, psinfo); 200f29e5956SAnton Vorontsov spin_unlock_irqrestore(&psinfo->buf_lock, flags); 201f29e5956SAnton Vorontsov s += c; 202f29e5956SAnton Vorontsov c = e - s; 203f29e5956SAnton Vorontsov } 204f29e5956SAnton Vorontsov } 205f29e5956SAnton Vorontsov 206f29e5956SAnton Vorontsov static struct console pstore_console = { 207f29e5956SAnton Vorontsov .name = "pstore", 208f29e5956SAnton Vorontsov .write = pstore_console_write, 209f29e5956SAnton Vorontsov .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, 210f29e5956SAnton Vorontsov .index = -1, 211f29e5956SAnton Vorontsov }; 212f29e5956SAnton Vorontsov 213f29e5956SAnton Vorontsov static void pstore_register_console(void) 214f29e5956SAnton Vorontsov { 215f29e5956SAnton Vorontsov register_console(&pstore_console); 216f29e5956SAnton Vorontsov } 217f29e5956SAnton Vorontsov #else 218f29e5956SAnton Vorontsov static void pstore_register_console(void) {} 219f29e5956SAnton Vorontsov #endif 220f29e5956SAnton Vorontsov 221897dba02SAnton Vorontsov static int pstore_write_compat(enum pstore_type_id type, 222897dba02SAnton Vorontsov enum kmsg_dump_reason reason, 223755d4fe4SSeiji Aguchi u64 *id, unsigned int part, int count, 224897dba02SAnton Vorontsov size_t size, struct pstore_info *psi) 225897dba02SAnton Vorontsov { 226897dba02SAnton Vorontsov return psi->write_buf(type, reason, id, part, psinfo->buf, size, psi); 227897dba02SAnton Vorontsov } 228897dba02SAnton Vorontsov 229ca01d6ddSTony Luck /* 230ca01d6ddSTony Luck * platform specific persistent storage driver registers with 231ca01d6ddSTony Luck * us here. If pstore is already mounted, call the platform 232ca01d6ddSTony Luck * read function right away to populate the file system. If not 233ca01d6ddSTony Luck * then the pstore mount code will call us later to fill out 234ca01d6ddSTony Luck * the file system. 235ca01d6ddSTony Luck * 236ca01d6ddSTony Luck * Register with kmsg_dump to save last part of console log on panic. 237ca01d6ddSTony Luck */ 238ca01d6ddSTony Luck int pstore_register(struct pstore_info *psi) 239ca01d6ddSTony Luck { 240ca01d6ddSTony Luck struct module *owner = psi->owner; 241ca01d6ddSTony Luck 242*8e48b1a8SLenny Szubowicz if (backend && strcmp(backend, psi->name)) 243*8e48b1a8SLenny Szubowicz return -EPERM; 244*8e48b1a8SLenny Szubowicz 245ca01d6ddSTony Luck spin_lock(&pstore_lock); 246ca01d6ddSTony Luck if (psinfo) { 247ca01d6ddSTony Luck spin_unlock(&pstore_lock); 248ca01d6ddSTony Luck return -EBUSY; 249ca01d6ddSTony Luck } 250dee28e72SMatthew Garrett 251897dba02SAnton Vorontsov if (!psi->write) 252897dba02SAnton Vorontsov psi->write = pstore_write_compat; 253ca01d6ddSTony Luck psinfo = psi; 254f6f82851SKees Cook mutex_init(&psinfo->read_mutex); 255ca01d6ddSTony Luck spin_unlock(&pstore_lock); 256ca01d6ddSTony Luck 257ca01d6ddSTony Luck if (owner && !try_module_get(owner)) { 258ca01d6ddSTony Luck psinfo = NULL; 259ca01d6ddSTony Luck return -EINVAL; 260ca01d6ddSTony Luck } 261ca01d6ddSTony Luck 262ca01d6ddSTony Luck if (pstore_is_mounted()) 2636dda9266SLuck, Tony pstore_get_records(0); 264ca01d6ddSTony Luck 265ca01d6ddSTony Luck kmsg_dump_register(&pstore_dumper); 266f29e5956SAnton Vorontsov pstore_register_console(); 26765f8c95eSAnton Vorontsov pstore_register_ftrace(); 268ca01d6ddSTony Luck 269a3f5f075SAnton Vorontsov if (pstore_update_ms >= 0) { 270a3f5f075SAnton Vorontsov pstore_timer.expires = jiffies + 271a3f5f075SAnton Vorontsov msecs_to_jiffies(pstore_update_ms); 2726dda9266SLuck, Tony add_timer(&pstore_timer); 273a3f5f075SAnton Vorontsov } 2746dda9266SLuck, Tony 275*8e48b1a8SLenny Szubowicz pr_info("pstore: Registered %s as persistent store backend\n", 276*8e48b1a8SLenny Szubowicz psi->name); 277*8e48b1a8SLenny Szubowicz 278ca01d6ddSTony Luck return 0; 279ca01d6ddSTony Luck } 280ca01d6ddSTony Luck EXPORT_SYMBOL_GPL(pstore_register); 281ca01d6ddSTony Luck 282ca01d6ddSTony Luck /* 2836dda9266SLuck, Tony * Read all the records from the persistent store. Create 2846dda9266SLuck, Tony * files in our filesystem. Don't warn about -EEXIST errors 2856dda9266SLuck, Tony * when we are re-scanning the backing store looking to add new 2866dda9266SLuck, Tony * error records. 287ca01d6ddSTony Luck */ 2886dda9266SLuck, Tony void pstore_get_records(int quiet) 289ca01d6ddSTony Luck { 290ca01d6ddSTony Luck struct pstore_info *psi = psinfo; 291f6f82851SKees Cook char *buf = NULL; 2928d38d74bSChen Gong ssize_t size; 293ca01d6ddSTony Luck u64 id; 294755d4fe4SSeiji Aguchi int count; 295ca01d6ddSTony Luck enum pstore_type_id type; 296ca01d6ddSTony Luck struct timespec time; 29706cf91b4SChen Gong int failed = 0, rc; 298ca01d6ddSTony Luck 299ca01d6ddSTony Luck if (!psi) 300ca01d6ddSTony Luck return; 301ca01d6ddSTony Luck 302f6f82851SKees Cook mutex_lock(&psi->read_mutex); 3032174f6dfSKees Cook if (psi->open && psi->open(psi)) 30406cf91b4SChen Gong goto out; 30506cf91b4SChen Gong 306755d4fe4SSeiji Aguchi while ((size = psi->read(&id, &type, &count, &time, &buf, psi)) > 0) { 307755d4fe4SSeiji Aguchi rc = pstore_mkfile(type, psi->name, id, count, buf, 308755d4fe4SSeiji Aguchi (size_t)size, time, psi); 309f6f82851SKees Cook kfree(buf); 310f6f82851SKees Cook buf = NULL; 3116dda9266SLuck, Tony if (rc && (rc != -EEXIST || !quiet)) 312ca01d6ddSTony Luck failed++; 313ca01d6ddSTony Luck } 3142174f6dfSKees Cook if (psi->close) 31506cf91b4SChen Gong psi->close(psi); 31606cf91b4SChen Gong out: 317f6f82851SKees Cook mutex_unlock(&psi->read_mutex); 318ca01d6ddSTony Luck 319ca01d6ddSTony Luck if (failed) 320ca01d6ddSTony Luck printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n", 321ca01d6ddSTony Luck failed, psi->name); 322ca01d6ddSTony Luck } 323ca01d6ddSTony Luck 3246dda9266SLuck, Tony static void pstore_dowork(struct work_struct *work) 3256dda9266SLuck, Tony { 3266dda9266SLuck, Tony pstore_get_records(1); 3276dda9266SLuck, Tony } 3286dda9266SLuck, Tony 3296dda9266SLuck, Tony static void pstore_timefunc(unsigned long dummy) 3306dda9266SLuck, Tony { 3316dda9266SLuck, Tony if (pstore_new_entry) { 3326dda9266SLuck, Tony pstore_new_entry = 0; 3336dda9266SLuck, Tony schedule_work(&pstore_work); 3346dda9266SLuck, Tony } 3356dda9266SLuck, Tony 336a3f5f075SAnton Vorontsov mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms)); 3376dda9266SLuck, Tony } 3386dda9266SLuck, Tony 339dee28e72SMatthew Garrett module_param(backend, charp, 0444); 340dee28e72SMatthew Garrett MODULE_PARM_DESC(backend, "Pstore backend to use"); 341