mtdoops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * MTD Oops/Panic logger
  3. *
  4. * Copyright © 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 ((ret = mtd_block_isbad(mtd, cxt->nextpage * record_size)) > 0) {
  146. badblock:
  147. printk(KERN_WARNING "mtdoops: bad block at %08lx\n",
  148. cxt->nextpage * record_size);
  149. i++;
  150. cxt->nextpage = cxt->nextpage + (mtd->erasesize / record_size);
  151. if (cxt->nextpage >= cxt->oops_pages)
  152. cxt->nextpage = 0;
  153. if (i == cxt->oops_pages / (mtd->erasesize / record_size)) {
  154. printk(KERN_ERR "mtdoops: all blocks bad!\n");
  155. return;
  156. }
  157. }
  158. if (ret < 0) {
  159. printk(KERN_ERR "mtdoops: mtd_block_isbad failed, aborting\n");
  160. return;
  161. }
  162. for (j = 0, ret = -1; (j < 3) && (ret < 0); j++)
  163. ret = mtdoops_erase_block(cxt, cxt->nextpage * record_size);
  164. if (ret >= 0) {
  165. printk(KERN_DEBUG "mtdoops: ready %d, %d\n",
  166. cxt->nextpage, cxt->nextcount);
  167. return;
  168. }
  169. if (ret == -EIO) {
  170. ret = mtd_block_markbad(mtd, cxt->nextpage * record_size);
  171. if (ret < 0 && ret != -EOPNOTSUPP) {
  172. printk(KERN_ERR "mtdoops: block_markbad failed, aborting\n");
  173. return;
  174. }
  175. }
  176. goto badblock;
  177. }
  178. static void mtdoops_write(struct mtdoops_context *cxt, int panic)
  179. {
  180. struct mtd_info *mtd = cxt->mtd;
  181. size_t retlen;
  182. u32 *hdr;
  183. int ret;
  184. /* Add mtdoops header to the buffer */
  185. hdr = cxt->oops_buf;
  186. hdr[0] = cxt->nextcount;
  187. hdr[1] = MTDOOPS_KERNMSG_MAGIC;
  188. if (panic) {
  189. ret = mtd_panic_write(mtd, cxt->nextpage * record_size,
  190. record_size, &retlen, cxt->oops_buf);
  191. if (ret == -EOPNOTSUPP) {
  192. printk(KERN_ERR "mtdoops: Cannot write from panic without panic_write\n");
  193. return;
  194. }
  195. } else
  196. ret = mtd_write(mtd, cxt->nextpage * record_size,
  197. record_size, &retlen, cxt->oops_buf);
  198. if (retlen != record_size || ret < 0)
  199. printk(KERN_ERR "mtdoops: write failure at %ld (%td of %ld written), error %d\n",
  200. cxt->nextpage * record_size, retlen, record_size, ret);
  201. mark_page_used(cxt, cxt->nextpage);
  202. memset(cxt->oops_buf, 0xff, record_size);
  203. mtdoops_inc_counter(cxt);
  204. }
  205. static void mtdoops_workfunc_write(struct work_struct *work)
  206. {
  207. struct mtdoops_context *cxt =
  208. container_of(work, struct mtdoops_context, work_write);
  209. mtdoops_write(cxt, 0);
  210. }
  211. static void find_next_position(struct mtdoops_context *cxt)
  212. {
  213. struct mtd_info *mtd = cxt->mtd;
  214. int ret, page, maxpos = 0;
  215. u32 count[2], maxcount = 0xffffffff;
  216. size_t retlen;
  217. for (page = 0; page < cxt->oops_pages; page++) {
  218. if (mtd_block_isbad(mtd, page * record_size))
  219. continue;
  220. /* Assume the page is used */
  221. mark_page_used(cxt, page);
  222. ret = mtd_read(mtd, page * record_size, MTDOOPS_HEADER_SIZE,
  223. &retlen, (u_char *)&count[0]);
  224. if (retlen != MTDOOPS_HEADER_SIZE ||
  225. (ret < 0 && !mtd_is_bitflip(ret))) {
  226. printk(KERN_ERR "mtdoops: read failure at %ld (%td of %d read), err %d\n",
  227. page * record_size, retlen,
  228. MTDOOPS_HEADER_SIZE, ret);
  229. continue;
  230. }
  231. if (count[0] == 0xffffffff && count[1] == 0xffffffff)
  232. mark_page_unused(cxt, page);
  233. if (count[0] == 0xffffffff || count[1] != MTDOOPS_KERNMSG_MAGIC)
  234. continue;
  235. if (maxcount == 0xffffffff) {
  236. maxcount = count[0];
  237. maxpos = page;
  238. } else if (count[0] < 0x40000000 && maxcount > 0xc0000000) {
  239. maxcount = count[0];
  240. maxpos = page;
  241. } else if (count[0] > maxcount && count[0] < 0xc0000000) {
  242. maxcount = count[0];
  243. maxpos = page;
  244. } else if (count[0] > maxcount && count[0] > 0xc0000000
  245. && maxcount > 0x80000000) {
  246. maxcount = count[0];
  247. maxpos = page;
  248. }
  249. }
  250. if (maxcount == 0xffffffff) {
  251. cxt->nextpage = cxt->oops_pages - 1;
  252. cxt->nextcount = 0;
  253. }
  254. else {
  255. cxt->nextpage = maxpos;
  256. cxt->nextcount = maxcount;
  257. }
  258. mtdoops_inc_counter(cxt);
  259. }
  260. static void mtdoops_do_dump(struct kmsg_dumper *dumper,
  261. enum kmsg_dump_reason reason)
  262. {
  263. struct mtdoops_context *cxt = container_of(dumper,
  264. struct mtdoops_context, dump);
  265. /* Only dump oopses if dump_oops is set */
  266. if (reason == KMSG_DUMP_OOPS && !dump_oops)
  267. return;
  268. kmsg_dump_get_buffer(dumper, true, cxt->oops_buf + MTDOOPS_HEADER_SIZE,
  269. record_size - MTDOOPS_HEADER_SIZE, NULL);
  270. /* Panics must be written immediately */
  271. if (reason != KMSG_DUMP_OOPS)
  272. mtdoops_write(cxt, 1);
  273. /* For other cases, schedule work to write it "nicely" */
  274. schedule_work(&cxt->work_write);
  275. }
  276. static void mtdoops_notify_add(struct mtd_info *mtd)
  277. {
  278. struct mtdoops_context *cxt = &oops_cxt;
  279. u64 mtdoops_pages = div_u64(mtd->size, record_size);
  280. int err;
  281. if (!strcmp(mtd->name, mtddev))
  282. cxt->mtd_index = mtd->index;
  283. if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
  284. return;
  285. if (mtd->size < mtd->erasesize * 2) {
  286. printk(KERN_ERR "mtdoops: MTD partition %d not big enough for mtdoops\n",
  287. mtd->index);
  288. return;
  289. }
  290. if (mtd->erasesize < record_size) {
  291. printk(KERN_ERR "mtdoops: eraseblock size of MTD partition %d too small\n",
  292. mtd->index);
  293. return;
  294. }
  295. if (mtd->size > MTDOOPS_MAX_MTD_SIZE) {
  296. printk(KERN_ERR "mtdoops: mtd%d is too large (limit is %d MiB)\n",
  297. mtd->index, MTDOOPS_MAX_MTD_SIZE / 1024 / 1024);
  298. return;
  299. }
  300. /* oops_page_used is a bit field */
  301. cxt->oops_page_used = vmalloc(DIV_ROUND_UP(mtdoops_pages,
  302. BITS_PER_LONG) * sizeof(unsigned long));
  303. if (!cxt->oops_page_used) {
  304. printk(KERN_ERR "mtdoops: could not allocate page array\n");
  305. return;
  306. }
  307. cxt->dump.max_reason = KMSG_DUMP_OOPS;
  308. cxt->dump.dump = mtdoops_do_dump;
  309. err = kmsg_dump_register(&cxt->dump);
  310. if (err) {
  311. printk(KERN_ERR "mtdoops: registering kmsg dumper failed, error %d\n", err);
  312. vfree(cxt->oops_page_used);
  313. cxt->oops_page_used = NULL;
  314. return;
  315. }
  316. cxt->mtd = mtd;
  317. cxt->oops_pages = (int)mtd->size / record_size;
  318. find_next_position(cxt);
  319. printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index);
  320. }
  321. static void mtdoops_notify_remove(struct mtd_info *mtd)
  322. {
  323. struct mtdoops_context *cxt = &oops_cxt;
  324. if (mtd->index != cxt->mtd_index || cxt->mtd_index < 0)
  325. return;
  326. if (kmsg_dump_unregister(&cxt->dump) < 0)
  327. printk(KERN_WARNING "mtdoops: could not unregister kmsg_dumper\n");
  328. cxt->mtd = NULL;
  329. flush_work(&cxt->work_erase);
  330. flush_work(&cxt->work_write);
  331. }
  332. static struct mtd_notifier mtdoops_notifier = {
  333. .add = mtdoops_notify_add,
  334. .remove = mtdoops_notify_remove,
  335. };
  336. static int __init mtdoops_init(void)
  337. {
  338. struct mtdoops_context *cxt = &oops_cxt;
  339. int mtd_index;
  340. char *endp;
  341. if (strlen(mtddev) == 0) {
  342. printk(KERN_ERR "mtdoops: mtd device (mtddev=name/number) must be supplied\n");
  343. return -EINVAL;
  344. }
  345. if ((record_size & 4095) != 0) {
  346. printk(KERN_ERR "mtdoops: record_size must be a multiple of 4096\n");
  347. return -EINVAL;
  348. }
  349. if (record_size < 4096) {
  350. printk(KERN_ERR "mtdoops: record_size must be over 4096 bytes\n");
  351. return -EINVAL;
  352. }
  353. /* Setup the MTD device to use */
  354. cxt->mtd_index = -1;
  355. mtd_index = simple_strtoul(mtddev, &endp, 0);
  356. if (*endp == '\0')
  357. cxt->mtd_index = mtd_index;
  358. cxt->oops_buf = vmalloc(record_size);
  359. if (!cxt->oops_buf) {
  360. printk(KERN_ERR "mtdoops: failed to allocate buffer workspace\n");
  361. return -ENOMEM;
  362. }
  363. memset(cxt->oops_buf, 0xff, record_size);
  364. INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase);
  365. INIT_WORK(&cxt->work_write, mtdoops_workfunc_write);
  366. register_mtd_user(&mtdoops_notifier);
  367. return 0;
  368. }
  369. static void __exit mtdoops_exit(void)
  370. {
  371. struct mtdoops_context *cxt = &oops_cxt;
  372. unregister_mtd_user(&mtdoops_notifier);
  373. vfree(cxt->oops_buf);
  374. vfree(cxt->oops_page_used);
  375. }
  376. module_init(mtdoops_init);
  377. module_exit(mtdoops_exit);
  378. MODULE_LICENSE("GPL");
  379. MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>");
  380. MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver");