mtdoops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * MTD Oops/Panic logger
  3. *
  4. * Copyright (C) 2007 Nokia Corporation. All rights reserved.
  5. *
  6. * Author: Richard Purdie <rpurdie@openedhand.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/console.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/sched.h>
  29. #include <linux/wait.h>
  30. #include <linux/delay.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/mtd/mtd.h>
  33. #include <linux/kmsg_dump.h>
  34. /* Maximum MTD partition size */
  35. #define MTDOOPS_MAX_MTD_SIZE (8 * 1024 * 1024)
  36. #define MTDOOPS_KERNMSG_MAGIC 0x5d005d00
  37. #define MTDOOPS_HEADER_SIZE 8
  38. static unsigned long record_size = 4096;
  39. module_param(record_size, ulong, 0400);
  40. MODULE_PARM_DESC(record_size,
  41. "record size for MTD OOPS pages in bytes (default 4096)");
  42. static char mtddev[80];
  43. module_param_string(mtddev, mtddev, 80, 0400);
  44. MODULE_PARM_DESC(mtddev,
  45. "name or index number of the MTD device to use");
  46. static int dump_oops = 1;
  47. module_param(dump_oops, int, 0600);
  48. MODULE_PARM_DESC(dump_oops,
  49. "set to 1 to dump oopses, 0 to only dump panics (default 1)");
  50. static struct mtdoops_context {
  51. struct kmsg_dumper dump;
  52. int mtd_index;
  53. struct work_struct work_erase;
  54. struct work_struct work_write;
  55. struct mtd_info *mtd;
  56. int oops_pages;
  57. int nextpage;
  58. int nextcount;
  59. unsigned long *oops_page_used;
  60. void *oops_buf;
  61. } oops_cxt;
  62. static void mark_page_used(struct mtdoops_context *cxt, int page)
  63. {
  64. set_bit(page, cxt->oops_page_used);
  65. }
  66. static void mark_page_unused(struct mtdoops_context *cxt, int page)
  67. {
  68. clear_bit(page, cxt->oops_page_used);
  69. }
  70. static int page_is_used(struct mtdoops_context *cxt, int page)
  71. {
  72. return test_bit(page, cxt->oops_page_used);
  73. }
  74. static void mtdoops_erase_callback(struct erase_info *done)
  75. {
  76. wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
  77. wake_up(wait_q);
  78. }
  79. static int mtdoops_erase_block(struct mtdoops_context *cxt, int offset)
  80. {
  81. struct mtd_info *mtd = cxt->mtd;
  82. u32 start_page_offset = mtd_div_by_eb(offset, mtd) * mtd->erasesize;
  83. u32 start_page = start_page_offset / record_size;
  84. u32 erase_pages = mtd->erasesize / record_size;
  85. struct erase_info erase;
  86. DECLARE_WAITQUEUE(wait, current);
  87. wait_queue_head_t wait_q;
  88. int ret;
  89. int page;
  90. init_waitqueue_head(&wait_q);
  91. erase.mtd = mtd;
  92. erase.callback = mtdoops_erase_callback;
  93. erase.addr = offset;
  94. erase.len = mtd->erasesize;
  95. erase.priv = (u_long)&wait_q;
  96. set_current_state(TASK_INTERRUPTIBLE);
  97. add_wait_queue(&wait_q, &wait);
  98. ret = mtd->erase(mtd, &erase);
  99. if (ret) {
  100. set_current_state(TASK_RUNNING);
  101. remove_wait_queue(&wait_q, &wait);
  102. printk(KERN_WARNING "mtdoops: erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
  103. (unsigned long long)erase.addr,
  104. (unsigned long long)erase.len, mtddev);
  105. return ret;
  106. }
  107. schedule(); /* Wait for erase to finish. */
  108. remove_wait_queue(&wait_q, &wait);
  109. /* Mark pages as unused */
  110. for (page = start_page; page < start_page + erase_pages; page++)
  111. mark_page_unused(cxt, page);
  112. return 0;
  113. }
  114. static void mtdoops_inc_counter(struct mtdoops_context *cxt)
  115. {
  116. cxt->nextpage++;
  117. if (cxt->nextpage >= cxt->oops_pages)
  118. cxt->nextpage = 0;
  119. cxt->nextcount++;
  120. if (cxt->nextcount == 0xffffffff)
  121. cxt->nextcount = 0;
  122. if (page_is_used(cxt, cxt->nextpage)) {
  123. schedule_work(&cxt->work_erase);
  124. return;
  125. }
  126. printk(KERN_DEBUG "mtdoops: ready %d, %d (no erase)\n",
  127. cxt->nextpage, cxt->nextcount);
  128. }
  129. /* Scheduled work - when we can't proceed without erasing a block */
  130. static void mtdoops_workfunc_erase(struct work_struct *work)
  131. {
  132. struct mtdoops_context *cxt =
  133. container_of(work, struct mtdoops_context, work_erase);
  134. struct mtd_info *mtd = cxt->mtd;
  135. int i = 0, j, ret, mod;
  136. /* We were unregistered */
  137. if (!mtd)
  138. return;
  139. mod = (cxt->nextpage * record_size) % mtd->erasesize;
  140. if (mod != 0) {
  141. cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / record_size);
  142. if (cxt->nextpage >= cxt->oops_pages)
  143. cxt->nextpage = 0;
  144. }
  145. while (mtd->block_isbad) {
  146. ret = mtd->block_isbad(mtd, cxt->nextpage * record_size);
  147. if (!ret)
  148. break;
  149. if (ret < 0) {
  150. printk(KERN_ERR "mtdoops: block_isbad failed, aborting\n");
  151. return;
  152. }
  153. badblock:
  154. printk(KERN_WARNING "mtdoops: bad block at %08lx\n",
  155. cxt->nextpage * record_size);
  156. i++;
  157. cxt->nextpage = cxt->nextpage + (mtd->erasesize / record_size);
  158. if (cxt->nextpage >= cxt->oops_pages)
  159. cxt->nextpage = 0;
  160. if (i == cxt->oops_pages / (mtd->erasesize / record_size)) {
  161. printk(KERN_ERR "mtdoops: all blocks bad!\n");
  162. return;
  163. }
  164. }
  165. for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
  166. ret = mtdoops_erase_block(cxt, cxt->nextpage * record_size);
  167. if (ret >= 0) {
  168. printk(KERN_DEBUG "mtdoops: ready %d, %d\n",
  169. cxt->nextpage, cxt->nextcount);
  170. return;
  171. }
  172. if (mtd->block_markbad && ret == -EIO) {
  173. ret = mtd->block_markbad(mtd, cxt->nextpage * record_size);
  174. if (ret < 0) {
  175. printk(KERN_ERR "mtdoops: block_markbad failed, aborting\n");
  176. return;
  177. }
  178. }
  179. goto badblock;
  180. }
  181. static void mtdoops_write(struct mtdoops_context *cxt, int panic)
  182. {
  183. struct mtd_info *mtd = cxt->mtd;
  184. size_t retlen;
  185. u32 *hdr;
  186. int ret;
  187. /* Add mtdoops header to the buffer */
  188. hdr = cxt->oops_buf;
  189. hdr[0] = cxt->nextcount;
  190. hdr[1] = MTDOOPS_KERNMSG_MAGIC;
  191. if (panic)
  192. ret = mtd->panic_write(mtd, cxt->nextpage * record_size,
  193. record_size, &retlen, cxt->oops_buf);
  194. else
  195. ret = mtd->write(mtd, cxt->nextpage * record_size,
  196. record_size, &retlen, cxt->oops_buf);
  197. if (retlen != record_size || ret < 0)
  198. printk(KERN_ERR "mtdoops: write failure at %ld (%td of %ld written), error %d\n",
  199. cxt->nextpage * record_size, retlen, record_size, ret);
  200. mark_page_used(cxt, cxt->nextpage);
  201. memset(cxt->oops_buf, 0xff, record_size);
  202. mtdoops_inc_counter(cxt);
  203. }
  204. static void mtdoops_workfunc_write(struct work_struct *work)
  205. {
  206. struct mtdoops_context *cxt =
  207. container_of(work, struct mtdoops_context, work_write);
  208. mtdoops_write(cxt, 0);
  209. }
  210. static void find_next_position(struct mtdoops_context *cxt)
  211. {
  212. struct mtd_info *mtd = cxt->mtd;
  213. int ret, page, maxpos = 0;
  214. u32 count[2], maxcount = 0xffffffff;
  215. size_t retlen;
  216. for (page = 0; page < cxt->oops_pages; page++) {
  217. /* Assume the page is used */
  218. mark_page_used(cxt, page);
  219. ret = mtd->read(mtd, page * record_size, MTDOOPS_HEADER_SIZE,
  220. &retlen, (u_char *) &count[0]);
  221. if (retlen != MTDOOPS_HEADER_SIZE ||
  222. (ret < 0 && ret != -EUCLEAN)) {
  223. printk(KERN_ERR "mtdoops: read failure at %ld (%td of %d read), err %d\n",
  224. page * record_size, retlen,
  225. MTDOOPS_HEADER_SIZE, ret);
  226. continue;
  227. }
  228. if (count[0] == 0xffffffff && count[1] == 0xffffffff)
  229. mark_page_unused(cxt, page);
  230. if (count[0] == 0xffffffff)
  231. continue;
  232. if (maxcount == 0xffffffff) {
  233. maxcount = count[0];
  234. maxpos = page;
  235. } else if (count[0] < 0x40000000 && maxcount > 0xc0000000) {
  236. maxcount = count[0];
  237. maxpos = page;
  238. } else if (count[0] > maxcount && count[0] < 0xc0000000) {
  239. maxcount = count[0];
  240. maxpos = page;
  241. } else if (count[0] > maxcount && count[0] > 0xc0000000
  242. && maxcount > 0x80000000) {
  243. maxcount = count[0];
  244. maxpos = page;
  245. }
  246. }
  247. if (maxcount == 0xffffffff) {
  248. cxt->nextpage = 0;
  249. cxt->nextcount = 1;
  250. schedule_work(&cxt->work_erase);
  251. return;
  252. }
  253. cxt->nextpage = maxpos;
  254. cxt->nextcount = maxcount;
  255. mtdoops_inc_counter(cxt);
  256. }
  257. static void mtdoops_do_dump(struct kmsg_dumper *dumper,
  258. enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
  259. const char *s2, unsigned long l2)
  260. {
  261. struct mtdoops_context *cxt = container_of(dumper,
  262. struct mtdoops_context, dump);
  263. unsigned long s1_start, s2_start;
  264. unsigned long l1_cpy, l2_cpy;
  265. char *dst;
  266. /* Only dump oopses if dump_oops is set */
  267. if (reason == KMSG_DUMP_OOPS && !dump_oops)
  268. return;
  269. dst = cxt->oops_buf + MTDOOPS_HEADER_SIZE; /* Skip the header */
  270. l2_cpy = min(l2, record_size - MTDOOPS_HEADER_SIZE);
  271. l1_cpy = min(l1, record_size - MTDOOPS_HEADER_SIZE - l2_cpy);
  272. s2_start = l2 - l2_cpy;
  273. s1_start = l1 - l1_cpy;
  274. memcpy(dst, s1 + s1_start, l1_cpy);
  275. memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
  276. /* Panics must be written immediately */
  277. if (reason == KMSG_DUMP_PANIC) {
  278. if (!cxt->mtd->panic_write)
  279. printk(KERN_ERR "mtdoops: Cannot write from panic without panic_write\n");
  280. else
  281. mtdoops_write(cxt, 1);
  282. return;
  283. }
  284. /* For other cases, schedule work to write it "nicely" */
  285. schedule_work(&cxt->work_write);
  286. }
  287. static void mtdoops_notify_add(struct mtd_info *mtd)
  288. {
  289. struct mtdoops_context *cxt = &oops_cxt;
  290. u64 mtdoops_pages = div_u64(mtd->size, record_size);
  291. int err;
  292. if (!strcmp(mtd->name, mtddev))
  293. cxt->mtd_index = mtd->index;
  294. if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
  295. return;
  296. if (mtd->size < mtd->erasesize * 2) {
  297. printk(KERN_ERR "mtdoops: MTD partition %d not big enough for mtdoops\n",
  298. mtd->index);
  299. return;
  300. }
  301. if (mtd->erasesize < record_size) {
  302. printk(KERN_ERR "mtdoops: eraseblock size of MTD partition %d too small\n",
  303. mtd->index);
  304. return;
  305. }
  306. if (mtd->size > MTDOOPS_MAX_MTD_SIZE) {
  307. printk(KERN_ERR "mtdoops: mtd%d is too large (limit is %d MiB)\n",
  308. mtd->index, MTDOOPS_MAX_MTD_SIZE / 1024 / 1024);
  309. return;
  310. }
  311. /* oops_page_used is a bit field */
  312. cxt->oops_page_used = vmalloc(DIV_ROUND_UP(mtdoops_pages,
  313. BITS_PER_LONG));
  314. if (!cxt->oops_page_used) {
  315. printk(KERN_ERR "mtdoops: could not allocate page array\n");
  316. return;
  317. }
  318. cxt->dump.dump = mtdoops_do_dump;
  319. err = kmsg_dump_register(&cxt->dump);
  320. if (err) {
  321. printk(KERN_ERR "mtdoops: registering kmsg dumper failed, error %d\n", err);
  322. vfree(cxt->oops_page_used);
  323. cxt->oops_page_used = NULL;
  324. return;
  325. }
  326. cxt->mtd = mtd;
  327. cxt->oops_pages = (int)mtd->size / record_size;
  328. find_next_position(cxt);
  329. printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
  330. }
  331. static void mtdoops_notify_remove(struct mtd_info *mtd)
  332. {
  333. struct mtdoops_context *cxt = &oops_cxt;
  334. if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
  335. return;
  336. if (kmsg_dump_unregister(&cxt->dump) < 0)
  337. printk(KERN_WARNING "mtdoops: could not unregister kmsg_dumper\n");
  338. cxt->mtd = NULL;
  339. flush_scheduled_work();
  340. }
  341. static struct mtd_notifier mtdoops_notifier = {
  342. .add = mtdoops_notify_add,
  343. .remove = mtdoops_notify_remove,
  344. };
  345. static int __init mtdoops_init(void)
  346. {
  347. struct mtdoops_context *cxt = &oops_cxt;
  348. int mtd_index;
  349. char *endp;
  350. if (strlen(mtddev) == 0) {
  351. printk(KERN_ERR "mtdoops: mtd device (mtddev=name/number) must be supplied\n");
  352. return -EINVAL;
  353. }
  354. if ((record_size & 4095) != 0) {
  355. printk(KERN_ERR "mtdoops: record_size must be a multiple of 4096\n");
  356. return -EINVAL;
  357. }
  358. if (record_size < 4096) {
  359. printk(KERN_ERR "mtdoops: record_size must be over 4096 bytes\n");
  360. return -EINVAL;
  361. }
  362. /* Setup the MTD device to use */
  363. cxt->mtd_index = -1;
  364. mtd_index = simple_strtoul(mtddev, &endp, 0);
  365. if (*endp == '\0')
  366. cxt->mtd_index = mtd_index;
  367. if (cxt->mtd_index > MAX_MTD_DEVICES) {
  368. printk(KERN_ERR "mtdoops: invalid mtd device number (%u) given\n",
  369. mtd_index);
  370. return -EINVAL;
  371. }
  372. cxt->oops_buf = vmalloc(record_size);
  373. if (!cxt->oops_buf) {
  374. printk(KERN_ERR "mtdoops: failed to allocate buffer workspace\n");
  375. return -ENOMEM;
  376. }
  377. memset(cxt->oops_buf, 0xff, record_size);
  378. INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
  379. INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
  380. register_mtd_user(&mtdoops_notifier);
  381. return 0;
  382. }
  383. static void __exit mtdoops_exit(void)
  384. {
  385. struct mtdoops_context *cxt = &oops_cxt;
  386. unregister_mtd_user(&mtdoops_notifier);
  387. vfree(cxt->oops_buf);
  388. vfree(cxt->oops_page_used);
  389. }
  390. module_init(mtdoops_init);
  391. module_exit(mtdoops_exit);
  392. MODULE_LICENSE("GPL");
  393. MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
  394. MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");