platform.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Persistent Storage - platform driver interface parts.
  3. *
  4. * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/atomic.h>
  20. #include <linux/types.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/kmsg_dump.h>
  24. #include <linux/module.h>
  25. #include <linux/pstore.h>
  26. #include <linux/string.h>
  27. #include <linux/timer.h>
  28. #include <linux/slab.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/hardirq.h>
  31. #include <linux/workqueue.h>
  32. #include "internal.h"
  33. /*
  34. * We defer making "oops" entries appear in pstore - see
  35. * whether the system is actually still running well enough
  36. * to let someone see the entry
  37. */
  38. #define PSTORE_INTERVAL (60 * HZ)
  39. static int pstore_new_entry;
  40. static void pstore_timefunc(unsigned long);
  41. static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
  42. static void pstore_dowork(struct work_struct *);
  43. static DECLARE_WORK(pstore_work, pstore_dowork);
  44. /*
  45. * pstore_lock just protects "psinfo" during
  46. * calls to pstore_register()
  47. */
  48. static DEFINE_SPINLOCK(pstore_lock);
  49. static struct pstore_info *psinfo;
  50. static char *backend;
  51. /* How much of the console log to snapshot */
  52. static unsigned long kmsg_bytes = 10240;
  53. void pstore_set_kmsg_bytes(int bytes)
  54. {
  55. kmsg_bytes = bytes;
  56. }
  57. /* Tag each group of saved records with a sequence number */
  58. static int oopscount;
  59. static const char *get_reason_str(enum kmsg_dump_reason reason)
  60. {
  61. switch (reason) {
  62. case KMSG_DUMP_PANIC:
  63. return "Panic";
  64. case KMSG_DUMP_OOPS:
  65. return "Oops";
  66. case KMSG_DUMP_EMERG:
  67. return "Emergency";
  68. case KMSG_DUMP_RESTART:
  69. return "Restart";
  70. case KMSG_DUMP_HALT:
  71. return "Halt";
  72. case KMSG_DUMP_POWEROFF:
  73. return "Poweroff";
  74. default:
  75. return "Unknown";
  76. }
  77. }
  78. bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
  79. {
  80. /*
  81. * In case of NMI path, pstore shouldn't be blocked
  82. * regardless of reason.
  83. */
  84. if (in_nmi())
  85. return true;
  86. switch (reason) {
  87. /* In panic case, other cpus are stopped by smp_send_stop(). */
  88. case KMSG_DUMP_PANIC:
  89. /* Emergency restart shouldn't be blocked by spin lock. */
  90. case KMSG_DUMP_EMERG:
  91. return true;
  92. default:
  93. return false;
  94. }
  95. }
  96. EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
  97. /*
  98. * callback from kmsg_dump. (s2,l2) has the most recently
  99. * written bytes, older bytes are in (s1,l1). Save as much
  100. * as we can from the end of the buffer.
  101. */
  102. static void pstore_dump(struct kmsg_dumper *dumper,
  103. enum kmsg_dump_reason reason,
  104. const char *s1, unsigned long l1,
  105. const char *s2, unsigned long l2)
  106. {
  107. unsigned long s1_start, s2_start;
  108. unsigned long l1_cpy, l2_cpy;
  109. unsigned long size, total = 0;
  110. char *dst;
  111. const char *why;
  112. u64 id;
  113. int hsize, ret;
  114. unsigned int part = 1;
  115. unsigned long flags = 0;
  116. int is_locked = 0;
  117. why = get_reason_str(reason);
  118. if (pstore_cannot_block_path(reason)) {
  119. is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
  120. if (!is_locked) {
  121. pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
  122. , in_nmi() ? "NMI" : why);
  123. }
  124. } else
  125. spin_lock_irqsave(&psinfo->buf_lock, flags);
  126. oopscount++;
  127. while (total < kmsg_bytes) {
  128. dst = psinfo->buf;
  129. hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
  130. size = psinfo->bufsize - hsize;
  131. dst += hsize;
  132. l2_cpy = min(l2, size);
  133. l1_cpy = min(l1, size - l2_cpy);
  134. if (l1_cpy + l2_cpy == 0)
  135. break;
  136. s2_start = l2 - l2_cpy;
  137. s1_start = l1 - l1_cpy;
  138. memcpy(dst, s1 + s1_start, l1_cpy);
  139. memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
  140. ret = psinfo->write(PSTORE_TYPE_DMESG, reason, &id, part,
  141. hsize + l1_cpy + l2_cpy, psinfo);
  142. if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
  143. pstore_new_entry = 1;
  144. l1 -= l1_cpy;
  145. l2 -= l2_cpy;
  146. total += l1_cpy + l2_cpy;
  147. part++;
  148. }
  149. if (pstore_cannot_block_path(reason)) {
  150. if (is_locked)
  151. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  152. } else
  153. spin_unlock_irqrestore(&psinfo->buf_lock, flags);
  154. }
  155. static struct kmsg_dumper pstore_dumper = {
  156. .dump = pstore_dump,
  157. };
  158. /*
  159. * platform specific persistent storage driver registers with
  160. * us here. If pstore is already mounted, call the platform
  161. * read function right away to populate the file system. If not
  162. * then the pstore mount code will call us later to fill out
  163. * the file system.
  164. *
  165. * Register with kmsg_dump to save last part of console log on panic.
  166. */
  167. int pstore_register(struct pstore_info *psi)
  168. {
  169. struct module *owner = psi->owner;
  170. spin_lock(&pstore_lock);
  171. if (psinfo) {
  172. spin_unlock(&pstore_lock);
  173. return -EBUSY;
  174. }
  175. if (backend && strcmp(backend, psi->name)) {
  176. spin_unlock(&pstore_lock);
  177. return -EINVAL;
  178. }
  179. psinfo = psi;
  180. mutex_init(&psinfo->read_mutex);
  181. spin_unlock(&pstore_lock);
  182. if (owner && !try_module_get(owner)) {
  183. psinfo = NULL;
  184. return -EINVAL;
  185. }
  186. if (pstore_is_mounted())
  187. pstore_get_records(0);
  188. kmsg_dump_register(&pstore_dumper);
  189. pstore_timer.expires = jiffies + PSTORE_INTERVAL;
  190. add_timer(&pstore_timer);
  191. return 0;
  192. }
  193. EXPORT_SYMBOL_GPL(pstore_register);
  194. /*
  195. * Read all the records from the persistent store. Create
  196. * files in our filesystem. Don't warn about -EEXIST errors
  197. * when we are re-scanning the backing store looking to add new
  198. * error records.
  199. */
  200. void pstore_get_records(int quiet)
  201. {
  202. struct pstore_info *psi = psinfo;
  203. char *buf = NULL;
  204. ssize_t size;
  205. u64 id;
  206. enum pstore_type_id type;
  207. struct timespec time;
  208. int failed = 0, rc;
  209. if (!psi)
  210. return;
  211. mutex_lock(&psi->read_mutex);
  212. if (psi->open && psi->open(psi))
  213. goto out;
  214. while ((size = psi->read(&id, &type, &time, &buf, psi)) > 0) {
  215. rc = pstore_mkfile(type, psi->name, id, buf, (size_t)size,
  216. time, psi);
  217. kfree(buf);
  218. buf = NULL;
  219. if (rc && (rc != -EEXIST || !quiet))
  220. failed++;
  221. }
  222. if (psi->close)
  223. psi->close(psi);
  224. out:
  225. mutex_unlock(&psi->read_mutex);
  226. if (failed)
  227. printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
  228. failed, psi->name);
  229. }
  230. static void pstore_dowork(struct work_struct *work)
  231. {
  232. pstore_get_records(1);
  233. }
  234. static void pstore_timefunc(unsigned long dummy)
  235. {
  236. if (pstore_new_entry) {
  237. pstore_new_entry = 0;
  238. schedule_work(&pstore_work);
  239. }
  240. mod_timer(&pstore_timer, jiffies + PSTORE_INTERVAL);
  241. }
  242. module_param(backend, charp, 0444);
  243. MODULE_PARM_DESC(backend, "Pstore backend to use");