ram.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * RAM Oops/Panic logger
  3. *
  4. * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
  5. * Copyright (C) 2011 Kees Cook <keescook@chromium.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/kernel.h>
  24. #include <linux/err.h>
  25. #include <linux/module.h>
  26. #include <linux/version.h>
  27. #include <linux/pstore.h>
  28. #include <linux/time.h>
  29. #include <linux/io.h>
  30. #include <linux/ioport.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/slab.h>
  33. #include <linux/compiler.h>
  34. #include <linux/pstore_ram.h>
  35. #define RAMOOPS_KERNMSG_HDR "===="
  36. #define MIN_MEM_SIZE 4096UL
  37. static ulong record_size = MIN_MEM_SIZE;
  38. module_param(record_size, ulong, 0400);
  39. MODULE_PARM_DESC(record_size,
  40. "size of each dump done on oops/panic");
  41. static ulong ramoops_console_size = MIN_MEM_SIZE;
  42. module_param_named(console_size, ramoops_console_size, ulong, 0400);
  43. MODULE_PARM_DESC(console_size, "size of kernel console log");
  44. static ulong ramoops_ftrace_size = MIN_MEM_SIZE;
  45. module_param_named(ftrace_size, ramoops_ftrace_size, ulong, 0400);
  46. MODULE_PARM_DESC(ftrace_size, "size of ftrace log");
  47. static ulong mem_address;
  48. module_param(mem_address, ulong, 0400);
  49. MODULE_PARM_DESC(mem_address,
  50. "start of reserved RAM used to store oops/panic logs");
  51. static ulong mem_size;
  52. module_param(mem_size, ulong, 0400);
  53. MODULE_PARM_DESC(mem_size,
  54. "size of reserved RAM used to store oops/panic logs");
  55. static unsigned int mem_type;
  56. module_param(mem_type, uint, 0600);
  57. MODULE_PARM_DESC(mem_type,
  58. "set to 1 to try to use unbuffered memory (default 0)");
  59. static int dump_oops = 1;
  60. module_param(dump_oops, int, 0600);
  61. MODULE_PARM_DESC(dump_oops,
  62. "set to 1 to dump oopses, 0 to only dump panics (default 1)");
  63. static int ramoops_ecc;
  64. module_param_named(ecc, ramoops_ecc, int, 0600);
  65. MODULE_PARM_DESC(ramoops_ecc,
  66. "if non-zero, the option enables ECC support and specifies "
  67. "ECC buffer size in bytes (1 is a special value, means 16 "
  68. "bytes ECC)");
  69. struct ramoops_context {
  70. struct persistent_ram_zone **przs;
  71. struct persistent_ram_zone *cprz;
  72. struct persistent_ram_zone *fprz;
  73. phys_addr_t phys_addr;
  74. unsigned long size;
  75. unsigned int memtype;
  76. size_t record_size;
  77. size_t console_size;
  78. size_t ftrace_size;
  79. int dump_oops;
  80. struct persistent_ram_ecc_info ecc_info;
  81. unsigned int max_dump_cnt;
  82. unsigned int dump_write_cnt;
  83. /* _read_cnt need clear on ramoops_pstore_open */
  84. unsigned int dump_read_cnt;
  85. unsigned int console_read_cnt;
  86. unsigned int ftrace_read_cnt;
  87. struct pstore_info pstore;
  88. };
  89. static struct platform_device *dummy;
  90. static struct ramoops_platform_data *dummy_data;
  91. static int ramoops_pstore_open(struct pstore_info *psi)
  92. {
  93. struct ramoops_context *cxt = psi->data;
  94. cxt->dump_read_cnt = 0;
  95. cxt->console_read_cnt = 0;
  96. cxt->ftrace_read_cnt = 0;
  97. return 0;
  98. }
  99. static struct persistent_ram_zone *
  100. ramoops_get_next_prz(struct persistent_ram_zone *przs[], uint *c, uint max,
  101. u64 *id,
  102. enum pstore_type_id *typep, enum pstore_type_id type,
  103. bool update)
  104. {
  105. struct persistent_ram_zone *prz;
  106. int i = (*c)++;
  107. if (i >= max)
  108. return NULL;
  109. prz = przs[i];
  110. if (!prz)
  111. return NULL;
  112. /* Update old/shadowed buffer. */
  113. if (update)
  114. persistent_ram_save_old(prz);
  115. if (!persistent_ram_old_size(prz))
  116. return NULL;
  117. *typep = type;
  118. *id = i;
  119. return prz;
  120. }
  121. static int ramoops_read_kmsg_hdr(char *buffer, struct timespec *time,
  122. bool *compressed)
  123. {
  124. char data_type;
  125. int header_length = 0;
  126. if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n%n", &time->tv_sec,
  127. &time->tv_nsec, &data_type, &header_length) == 3) {
  128. if (data_type == 'C')
  129. *compressed = true;
  130. else
  131. *compressed = false;
  132. } else if (sscanf(buffer, RAMOOPS_KERNMSG_HDR "%lu.%lu\n%n",
  133. &time->tv_sec, &time->tv_nsec, &header_length) == 2) {
  134. *compressed = false;
  135. } else {
  136. time->tv_sec = 0;
  137. time->tv_nsec = 0;
  138. *compressed = false;
  139. }
  140. return header_length;
  141. }
  142. static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
  143. int *count, struct timespec *time,
  144. char **buf, bool *compressed,
  145. struct pstore_info *psi)
  146. {
  147. ssize_t size;
  148. ssize_t ecc_notice_size;
  149. struct ramoops_context *cxt = psi->data;
  150. struct persistent_ram_zone *prz;
  151. int header_length;
  152. prz = ramoops_get_next_prz(cxt->przs, &cxt->dump_read_cnt,
  153. cxt->max_dump_cnt, id, type,
  154. PSTORE_TYPE_DMESG, 1);
  155. if (!prz)
  156. prz = ramoops_get_next_prz(&cxt->cprz, &cxt->console_read_cnt,
  157. 1, id, type, PSTORE_TYPE_CONSOLE, 0);
  158. if (!prz)
  159. prz = ramoops_get_next_prz(&cxt->fprz, &cxt->ftrace_read_cnt,
  160. 1, id, type, PSTORE_TYPE_FTRACE, 0);
  161. if (!prz)
  162. return 0;
  163. if (!persistent_ram_old(prz))
  164. return 0;
  165. size = persistent_ram_old_size(prz);
  166. header_length = ramoops_read_kmsg_hdr(persistent_ram_old(prz), time,
  167. compressed);
  168. size -= header_length;
  169. /* ECC correction notice */
  170. ecc_notice_size = persistent_ram_ecc_string(prz, NULL, 0);
  171. *buf = kmalloc(size + ecc_notice_size + 1, GFP_KERNEL);
  172. if (*buf == NULL)
  173. return -ENOMEM;
  174. memcpy(*buf, (char *)persistent_ram_old(prz) + header_length, size);
  175. persistent_ram_ecc_string(prz, *buf + size, ecc_notice_size + 1);
  176. return size + ecc_notice_size;
  177. }
  178. static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz,
  179. bool compressed)
  180. {
  181. char *hdr;
  182. struct timespec timestamp;
  183. size_t len;
  184. /* Report zeroed timestamp if called before timekeeping has resumed. */
  185. if (__getnstimeofday(&timestamp)) {
  186. timestamp.tv_sec = 0;
  187. timestamp.tv_nsec = 0;
  188. }
  189. hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu-%c\n",
  190. (long)timestamp.tv_sec, (long)(timestamp.tv_nsec / 1000),
  191. compressed ? 'C' : 'D');
  192. WARN_ON_ONCE(!hdr);
  193. len = hdr ? strlen(hdr) : 0;
  194. persistent_ram_write(prz, hdr, len);
  195. kfree(hdr);
  196. return len;
  197. }
  198. static int notrace ramoops_pstore_write_buf(enum pstore_type_id type,
  199. enum kmsg_dump_reason reason,
  200. u64 *id, unsigned int part,
  201. const char *buf,
  202. bool compressed, size_t size,
  203. struct pstore_info *psi)
  204. {
  205. struct ramoops_context *cxt = psi->data;
  206. struct persistent_ram_zone *prz;
  207. size_t hlen;
  208. if (type == PSTORE_TYPE_CONSOLE) {
  209. if (!cxt->cprz)
  210. return -ENOMEM;
  211. persistent_ram_write(cxt->cprz, buf, size);
  212. return 0;
  213. } else if (type == PSTORE_TYPE_FTRACE) {
  214. if (!cxt->fprz)
  215. return -ENOMEM;
  216. persistent_ram_write(cxt->fprz, buf, size);
  217. return 0;
  218. }
  219. if (type != PSTORE_TYPE_DMESG)
  220. return -EINVAL;
  221. /* Out of the various dmesg dump types, ramoops is currently designed
  222. * to only store crash logs, rather than storing general kernel logs.
  223. */
  224. if (reason != KMSG_DUMP_OOPS &&
  225. reason != KMSG_DUMP_PANIC)
  226. return -EINVAL;
  227. /* Skip Oopes when configured to do so. */
  228. if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
  229. return -EINVAL;
  230. /* Explicitly only take the first part of any new crash.
  231. * If our buffer is larger than kmsg_bytes, this can never happen,
  232. * and if our buffer is smaller than kmsg_bytes, we don't want the
  233. * report split across multiple records.
  234. */
  235. if (part != 1)
  236. return -ENOSPC;
  237. if (!cxt->przs)
  238. return -ENOSPC;
  239. prz = cxt->przs[cxt->dump_write_cnt];
  240. hlen = ramoops_write_kmsg_hdr(prz, compressed);
  241. if (size + hlen > prz->buffer_size)
  242. size = prz->buffer_size - hlen;
  243. persistent_ram_write(prz, buf, size);
  244. cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
  245. return 0;
  246. }
  247. static int ramoops_pstore_erase(enum pstore_type_id type, u64 id, int count,
  248. struct timespec time, struct pstore_info *psi)
  249. {
  250. struct ramoops_context *cxt = psi->data;
  251. struct persistent_ram_zone *prz;
  252. switch (type) {
  253. case PSTORE_TYPE_DMESG:
  254. if (id >= cxt->max_dump_cnt)
  255. return -EINVAL;
  256. prz = cxt->przs[id];
  257. break;
  258. case PSTORE_TYPE_CONSOLE:
  259. prz = cxt->cprz;
  260. break;
  261. case PSTORE_TYPE_FTRACE:
  262. prz = cxt->fprz;
  263. break;
  264. default:
  265. return -EINVAL;
  266. }
  267. persistent_ram_free_old(prz);
  268. persistent_ram_zap(prz);
  269. return 0;
  270. }
  271. static struct ramoops_context oops_cxt = {
  272. .pstore = {
  273. .owner = THIS_MODULE,
  274. .name = "ramoops",
  275. .open = ramoops_pstore_open,
  276. .read = ramoops_pstore_read,
  277. .write_buf = ramoops_pstore_write_buf,
  278. .erase = ramoops_pstore_erase,
  279. },
  280. };
  281. static void ramoops_free_przs(struct ramoops_context *cxt)
  282. {
  283. int i;
  284. cxt->max_dump_cnt = 0;
  285. if (!cxt->przs)
  286. return;
  287. for (i = 0; !IS_ERR_OR_NULL(cxt->przs[i]); i++)
  288. persistent_ram_free(cxt->przs[i]);
  289. kfree(cxt->przs);
  290. }
  291. static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
  292. phys_addr_t *paddr, size_t dump_mem_sz)
  293. {
  294. int err = -ENOMEM;
  295. int i;
  296. if (!cxt->record_size)
  297. return 0;
  298. if (*paddr + dump_mem_sz - cxt->phys_addr > cxt->size) {
  299. dev_err(dev, "no room for dumps\n");
  300. return -ENOMEM;
  301. }
  302. cxt->max_dump_cnt = dump_mem_sz / cxt->record_size;
  303. if (!cxt->max_dump_cnt)
  304. return -ENOMEM;
  305. cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt,
  306. GFP_KERNEL);
  307. if (!cxt->przs) {
  308. dev_err(dev, "failed to initialize a prz array for dumps\n");
  309. goto fail_prz;
  310. }
  311. for (i = 0; i < cxt->max_dump_cnt; i++) {
  312. size_t sz = cxt->record_size;
  313. cxt->przs[i] = persistent_ram_new(*paddr, sz, 0,
  314. &cxt->ecc_info,
  315. cxt->memtype);
  316. if (IS_ERR(cxt->przs[i])) {
  317. err = PTR_ERR(cxt->przs[i]);
  318. dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
  319. sz, (unsigned long long)*paddr, err);
  320. goto fail_prz;
  321. }
  322. *paddr += sz;
  323. }
  324. return 0;
  325. fail_prz:
  326. ramoops_free_przs(cxt);
  327. return err;
  328. }
  329. static int ramoops_init_prz(struct device *dev, struct ramoops_context *cxt,
  330. struct persistent_ram_zone **prz,
  331. phys_addr_t *paddr, size_t sz, u32 sig)
  332. {
  333. if (!sz)
  334. return 0;
  335. if (*paddr + sz - cxt->phys_addr > cxt->size) {
  336. dev_err(dev, "no room for mem region (0x%zx@0x%llx) in (0x%lx@0x%llx)\n",
  337. sz, (unsigned long long)*paddr,
  338. cxt->size, (unsigned long long)cxt->phys_addr);
  339. return -ENOMEM;
  340. }
  341. *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info, cxt->memtype);
  342. if (IS_ERR(*prz)) {
  343. int err = PTR_ERR(*prz);
  344. dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
  345. sz, (unsigned long long)*paddr, err);
  346. return err;
  347. }
  348. persistent_ram_zap(*prz);
  349. *paddr += sz;
  350. return 0;
  351. }
  352. static int ramoops_probe(struct platform_device *pdev)
  353. {
  354. struct device *dev = &pdev->dev;
  355. struct ramoops_platform_data *pdata = pdev->dev.platform_data;
  356. struct ramoops_context *cxt = &oops_cxt;
  357. size_t dump_mem_sz;
  358. phys_addr_t paddr;
  359. int err = -EINVAL;
  360. /* Only a single ramoops area allowed at a time, so fail extra
  361. * probes.
  362. */
  363. if (cxt->max_dump_cnt)
  364. goto fail_out;
  365. if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size &&
  366. !pdata->ftrace_size)) {
  367. pr_err("The memory size and the record/console size must be "
  368. "non-zero\n");
  369. goto fail_out;
  370. }
  371. if (pdata->record_size && !is_power_of_2(pdata->record_size))
  372. pdata->record_size = rounddown_pow_of_two(pdata->record_size);
  373. if (pdata->console_size && !is_power_of_2(pdata->console_size))
  374. pdata->console_size = rounddown_pow_of_two(pdata->console_size);
  375. if (pdata->ftrace_size && !is_power_of_2(pdata->ftrace_size))
  376. pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size);
  377. cxt->size = pdata->mem_size;
  378. cxt->phys_addr = pdata->mem_address;
  379. cxt->memtype = pdata->mem_type;
  380. cxt->record_size = pdata->record_size;
  381. cxt->console_size = pdata->console_size;
  382. cxt->ftrace_size = pdata->ftrace_size;
  383. cxt->dump_oops = pdata->dump_oops;
  384. cxt->ecc_info = pdata->ecc_info;
  385. paddr = cxt->phys_addr;
  386. dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size;
  387. err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz);
  388. if (err)
  389. goto fail_out;
  390. err = ramoops_init_prz(dev, cxt, &cxt->cprz, &paddr,
  391. cxt->console_size, 0);
  392. if (err)
  393. goto fail_init_cprz;
  394. err = ramoops_init_prz(dev, cxt, &cxt->fprz, &paddr, cxt->ftrace_size,
  395. LINUX_VERSION_CODE);
  396. if (err)
  397. goto fail_init_fprz;
  398. if (!cxt->przs && !cxt->cprz && !cxt->fprz) {
  399. pr_err("memory size too small, minimum is %zu\n",
  400. cxt->console_size + cxt->record_size +
  401. cxt->ftrace_size);
  402. err = -EINVAL;
  403. goto fail_cnt;
  404. }
  405. cxt->pstore.data = cxt;
  406. /*
  407. * Console can handle any buffer size, so prefer LOG_LINE_MAX. If we
  408. * have to handle dumps, we must have at least record_size buffer. And
  409. * for ftrace, bufsize is irrelevant (if bufsize is 0, buf will be
  410. * ZERO_SIZE_PTR).
  411. */
  412. if (cxt->console_size)
  413. cxt->pstore.bufsize = 1024; /* LOG_LINE_MAX */
  414. cxt->pstore.bufsize = max(cxt->record_size, cxt->pstore.bufsize);
  415. cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
  416. spin_lock_init(&cxt->pstore.buf_lock);
  417. if (!cxt->pstore.buf) {
  418. pr_err("cannot allocate pstore buffer\n");
  419. err = -ENOMEM;
  420. goto fail_clear;
  421. }
  422. err = pstore_register(&cxt->pstore);
  423. if (err) {
  424. pr_err("registering with pstore failed\n");
  425. goto fail_buf;
  426. }
  427. /*
  428. * Update the module parameter variables as well so they are visible
  429. * through /sys/module/ramoops/parameters/
  430. */
  431. mem_size = pdata->mem_size;
  432. mem_address = pdata->mem_address;
  433. record_size = pdata->record_size;
  434. dump_oops = pdata->dump_oops;
  435. pr_info("attached 0x%lx@0x%llx, ecc: %d/%d\n",
  436. cxt->size, (unsigned long long)cxt->phys_addr,
  437. cxt->ecc_info.ecc_size, cxt->ecc_info.block_size);
  438. return 0;
  439. fail_buf:
  440. kfree(cxt->pstore.buf);
  441. fail_clear:
  442. cxt->pstore.bufsize = 0;
  443. fail_cnt:
  444. kfree(cxt->fprz);
  445. fail_init_fprz:
  446. kfree(cxt->cprz);
  447. fail_init_cprz:
  448. ramoops_free_przs(cxt);
  449. fail_out:
  450. return err;
  451. }
  452. static int __exit ramoops_remove(struct platform_device *pdev)
  453. {
  454. #if 0
  455. /* TODO(kees): We cannot unload ramoops since pstore doesn't support
  456. * unregistering yet.
  457. */
  458. struct ramoops_context *cxt = &oops_cxt;
  459. iounmap(cxt->virt_addr);
  460. release_mem_region(cxt->phys_addr, cxt->size);
  461. cxt->max_dump_cnt = 0;
  462. /* TODO(kees): When pstore supports unregistering, call it here. */
  463. kfree(cxt->pstore.buf);
  464. cxt->pstore.bufsize = 0;
  465. return 0;
  466. #endif
  467. return -EBUSY;
  468. }
  469. static struct platform_driver ramoops_driver = {
  470. .probe = ramoops_probe,
  471. .remove = __exit_p(ramoops_remove),
  472. .driver = {
  473. .name = "ramoops",
  474. },
  475. };
  476. static void ramoops_register_dummy(void)
  477. {
  478. if (!mem_size)
  479. return;
  480. pr_info("using module parameters\n");
  481. dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL);
  482. if (!dummy_data) {
  483. pr_info("could not allocate pdata\n");
  484. return;
  485. }
  486. dummy_data->mem_size = mem_size;
  487. dummy_data->mem_address = mem_address;
  488. dummy_data->mem_type = 0;
  489. dummy_data->record_size = record_size;
  490. dummy_data->console_size = ramoops_console_size;
  491. dummy_data->ftrace_size = ramoops_ftrace_size;
  492. dummy_data->dump_oops = dump_oops;
  493. /*
  494. * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
  495. * (using 1 byte for ECC isn't much of use anyway).
  496. */
  497. dummy_data->ecc_info.ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
  498. dummy = platform_device_register_data(NULL, "ramoops", -1,
  499. dummy_data, sizeof(struct ramoops_platform_data));
  500. if (IS_ERR(dummy)) {
  501. pr_info("could not create platform device: %ld\n",
  502. PTR_ERR(dummy));
  503. }
  504. }
  505. static int __init ramoops_init(void)
  506. {
  507. ramoops_register_dummy();
  508. return platform_driver_register(&ramoops_driver);
  509. }
  510. postcore_initcall(ramoops_init);
  511. static void __exit ramoops_exit(void)
  512. {
  513. platform_driver_unregister(&ramoops_driver);
  514. platform_device_unregister(dummy);
  515. kfree(dummy_data);
  516. }
  517. module_exit(ramoops_exit);
  518. MODULE_LICENSE("GPL");
  519. MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
  520. MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");