ram_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Google, Inc.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/device.h>
  7. #include <linux/err.h>
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/list.h>
  13. #include <linux/memblock.h>
  14. #include <linux/rslib.h>
  15. #include <linux/slab.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/mm.h>
  19. #include <asm/page.h>
  20. #include "ram_internal.h"
  21. /**
  22. * struct persistent_ram_buffer - persistent circular RAM buffer
  23. *
  24. * @sig: Signature to indicate header (PERSISTENT_RAM_SIG xor PRZ-type value)
  25. * @start: First valid byte in the buffer.
  26. * @size: Number of valid bytes in the buffer.
  27. * @data: The contents of the buffer.
  28. */
  29. struct persistent_ram_buffer {
  30. uint32_t sig;
  31. atomic_t start;
  32. atomic_t size;
  33. uint8_t data[];
  34. };
  35. #define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */
  36. static inline size_t buffer_size(struct persistent_ram_zone *prz)
  37. {
  38. return atomic_read(&prz->buffer->size);
  39. }
  40. static inline size_t buffer_start(struct persistent_ram_zone *prz)
  41. {
  42. return atomic_read(&prz->buffer->start);
  43. }
  44. /* increase and wrap the start pointer, returning the old value */
  45. static size_t buffer_start_add(struct persistent_ram_zone *prz, size_t a)
  46. {
  47. int old;
  48. int new;
  49. unsigned long flags = 0;
  50. if (!(prz->flags & PRZ_FLAG_NO_LOCK))
  51. raw_spin_lock_irqsave(&prz->buffer_lock, flags);
  52. old = atomic_read(&prz->buffer->start);
  53. new = old + a;
  54. while (unlikely(new >= prz->buffer_size))
  55. new -= prz->buffer_size;
  56. atomic_set(&prz->buffer->start, new);
  57. if (!(prz->flags & PRZ_FLAG_NO_LOCK))
  58. raw_spin_unlock_irqrestore(&prz->buffer_lock, flags);
  59. return old;
  60. }
  61. /* increase the size counter until it hits the max size */
  62. static void buffer_size_add(struct persistent_ram_zone *prz, size_t a)
  63. {
  64. size_t old;
  65. size_t new;
  66. unsigned long flags = 0;
  67. if (!(prz->flags & PRZ_FLAG_NO_LOCK))
  68. raw_spin_lock_irqsave(&prz->buffer_lock, flags);
  69. old = atomic_read(&prz->buffer->size);
  70. if (old == prz->buffer_size)
  71. goto exit;
  72. new = old + a;
  73. if (new > prz->buffer_size)
  74. new = prz->buffer_size;
  75. atomic_set(&prz->buffer->size, new);
  76. exit:
  77. if (!(prz->flags & PRZ_FLAG_NO_LOCK))
  78. raw_spin_unlock_irqrestore(&prz->buffer_lock, flags);
  79. }
  80. static void notrace persistent_ram_encode_rs8(struct persistent_ram_zone *prz,
  81. uint8_t *data, size_t len, uint8_t *ecc)
  82. {
  83. int i;
  84. /* Initialize the parity buffer */
  85. memset(prz->ecc_info.par, 0,
  86. prz->ecc_info.ecc_size * sizeof(prz->ecc_info.par[0]));
  87. encode_rs8(prz->rs_decoder, data, len, prz->ecc_info.par, 0);
  88. for (i = 0; i < prz->ecc_info.ecc_size; i++)
  89. ecc[i] = prz->ecc_info.par[i];
  90. }
  91. static int persistent_ram_decode_rs8(struct persistent_ram_zone *prz,
  92. void *data, size_t len, uint8_t *ecc)
  93. {
  94. int i;
  95. for (i = 0; i < prz->ecc_info.ecc_size; i++)
  96. prz->ecc_info.par[i] = ecc[i];
  97. return decode_rs8(prz->rs_decoder, data, prz->ecc_info.par, len,
  98. NULL, 0, NULL, 0, NULL);
  99. }
  100. static void notrace persistent_ram_update_ecc(struct persistent_ram_zone *prz,
  101. unsigned int start, unsigned int count)
  102. {
  103. struct persistent_ram_buffer *buffer = prz->buffer;
  104. uint8_t *buffer_end = buffer->data + prz->buffer_size;
  105. uint8_t *block;
  106. uint8_t *par;
  107. int ecc_block_size = prz->ecc_info.block_size;
  108. int ecc_size = prz->ecc_info.ecc_size;
  109. int size = ecc_block_size;
  110. if (!ecc_size)
  111. return;
  112. block = buffer->data + (start & ~(ecc_block_size - 1));
  113. par = prz->par_buffer + (start / ecc_block_size) * ecc_size;
  114. do {
  115. if (block + ecc_block_size > buffer_end)
  116. size = buffer_end - block;
  117. persistent_ram_encode_rs8(prz, block, size, par);
  118. block += ecc_block_size;
  119. par += ecc_size;
  120. } while (block < buffer->data + start + count);
  121. }
  122. static void persistent_ram_update_header_ecc(struct persistent_ram_zone *prz)
  123. {
  124. struct persistent_ram_buffer *buffer = prz->buffer;
  125. if (!prz->ecc_info.ecc_size)
  126. return;
  127. persistent_ram_encode_rs8(prz, (uint8_t *)buffer, sizeof(*buffer),
  128. prz->par_header);
  129. }
  130. static void persistent_ram_ecc_old(struct persistent_ram_zone *prz)
  131. {
  132. struct persistent_ram_buffer *buffer = prz->buffer;
  133. uint8_t *block;
  134. uint8_t *par;
  135. if (!prz->ecc_info.ecc_size)
  136. return;
  137. block = buffer->data;
  138. par = prz->par_buffer;
  139. while (block < buffer->data + buffer_size(prz)) {
  140. int numerr;
  141. int size = prz->ecc_info.block_size;
  142. if (block + size > buffer->data + prz->buffer_size)
  143. size = buffer->data + prz->buffer_size - block;
  144. numerr = persistent_ram_decode_rs8(prz, block, size, par);
  145. if (numerr > 0) {
  146. pr_devel("error in block %p, %d\n", block, numerr);
  147. prz->corrected_bytes += numerr;
  148. } else if (numerr < 0) {
  149. pr_devel("uncorrectable error in block %p\n", block);
  150. prz->bad_blocks++;
  151. }
  152. block += prz->ecc_info.block_size;
  153. par += prz->ecc_info.ecc_size;
  154. }
  155. }
  156. static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,
  157. struct persistent_ram_ecc_info *ecc_info)
  158. {
  159. int numerr;
  160. struct persistent_ram_buffer *buffer = prz->buffer;
  161. size_t ecc_blocks;
  162. size_t ecc_total;
  163. if (!ecc_info || !ecc_info->ecc_size)
  164. return 0;
  165. prz->ecc_info.block_size = ecc_info->block_size ?: 128;
  166. prz->ecc_info.ecc_size = ecc_info->ecc_size ?: 16;
  167. prz->ecc_info.symsize = ecc_info->symsize ?: 8;
  168. prz->ecc_info.poly = ecc_info->poly ?: 0x11d;
  169. ecc_blocks = DIV_ROUND_UP(prz->buffer_size - prz->ecc_info.ecc_size,
  170. prz->ecc_info.block_size +
  171. prz->ecc_info.ecc_size);
  172. ecc_total = (ecc_blocks + 1) * prz->ecc_info.ecc_size;
  173. if (ecc_total >= prz->buffer_size) {
  174. pr_err("%s: invalid ecc_size %u (total %zu, buffer size %zu)\n",
  175. __func__, prz->ecc_info.ecc_size,
  176. ecc_total, prz->buffer_size);
  177. return -EINVAL;
  178. }
  179. prz->buffer_size -= ecc_total;
  180. prz->par_buffer = buffer->data + prz->buffer_size;
  181. prz->par_header = prz->par_buffer +
  182. ecc_blocks * prz->ecc_info.ecc_size;
  183. /*
  184. * first consecutive root is 0
  185. * primitive element to generate roots = 1
  186. */
  187. prz->rs_decoder = init_rs(prz->ecc_info.symsize, prz->ecc_info.poly,
  188. 0, 1, prz->ecc_info.ecc_size);
  189. if (prz->rs_decoder == NULL) {
  190. pr_info("init_rs failed\n");
  191. return -EINVAL;
  192. }
  193. /* allocate workspace instead of using stack VLA */
  194. prz->ecc_info.par = kmalloc_objs(*prz->ecc_info.par,
  195. prz->ecc_info.ecc_size);
  196. if (!prz->ecc_info.par) {
  197. pr_err("cannot allocate ECC parity workspace\n");
  198. return -ENOMEM;
  199. }
  200. prz->corrected_bytes = 0;
  201. prz->bad_blocks = 0;
  202. numerr = persistent_ram_decode_rs8(prz, buffer, sizeof(*buffer),
  203. prz->par_header);
  204. if (numerr > 0) {
  205. pr_info("error in header, %d\n", numerr);
  206. prz->corrected_bytes += numerr;
  207. } else if (numerr < 0) {
  208. pr_info_ratelimited("uncorrectable error in header\n");
  209. prz->bad_blocks++;
  210. }
  211. return 0;
  212. }
  213. ssize_t persistent_ram_ecc_string(struct persistent_ram_zone *prz,
  214. char *str, size_t len)
  215. {
  216. ssize_t ret;
  217. if (!prz->ecc_info.ecc_size)
  218. return 0;
  219. if (prz->corrected_bytes || prz->bad_blocks)
  220. ret = snprintf(str, len, ""
  221. "\nECC: %d Corrected bytes, %d unrecoverable blocks\n",
  222. prz->corrected_bytes, prz->bad_blocks);
  223. else
  224. ret = snprintf(str, len, "\nECC: No errors detected\n");
  225. return ret;
  226. }
  227. static void notrace persistent_ram_update(struct persistent_ram_zone *prz,
  228. const void *s, unsigned int start, unsigned int count)
  229. {
  230. struct persistent_ram_buffer *buffer = prz->buffer;
  231. memcpy_toio(buffer->data + start, s, count);
  232. persistent_ram_update_ecc(prz, start, count);
  233. }
  234. static int notrace persistent_ram_update_user(struct persistent_ram_zone *prz,
  235. const void __user *s, unsigned int start, unsigned int count)
  236. {
  237. struct persistent_ram_buffer *buffer = prz->buffer;
  238. int ret = unlikely(copy_from_user(buffer->data + start, s, count)) ?
  239. -EFAULT : 0;
  240. persistent_ram_update_ecc(prz, start, count);
  241. return ret;
  242. }
  243. void persistent_ram_save_old(struct persistent_ram_zone *prz)
  244. {
  245. struct persistent_ram_buffer *buffer = prz->buffer;
  246. size_t size = buffer_size(prz);
  247. size_t start = buffer_start(prz);
  248. if (!size)
  249. return;
  250. /*
  251. * If the existing buffer is differently sized, free it so a new
  252. * one is allocated. This can happen when persistent_ram_save_old()
  253. * is called early in boot and later for a timer-triggered
  254. * survivable crash when the crash dumps don't match in size
  255. * (which would be extremely unlikely given kmsg buffers usually
  256. * exceed prz buffer sizes).
  257. */
  258. if (prz->old_log && prz->old_log_size != size)
  259. persistent_ram_free_old(prz);
  260. if (!prz->old_log) {
  261. persistent_ram_ecc_old(prz);
  262. prz->old_log = kvzalloc(size, GFP_KERNEL);
  263. }
  264. if (!prz->old_log) {
  265. pr_err("failed to allocate buffer\n");
  266. return;
  267. }
  268. prz->old_log_size = size;
  269. memcpy_fromio(prz->old_log, &buffer->data[start], size - start);
  270. memcpy_fromio(prz->old_log + size - start, &buffer->data[0], start);
  271. }
  272. int notrace persistent_ram_write(struct persistent_ram_zone *prz,
  273. const void *s, unsigned int count)
  274. {
  275. int rem;
  276. int c = count;
  277. size_t start;
  278. if (unlikely(c > prz->buffer_size)) {
  279. s += c - prz->buffer_size;
  280. c = prz->buffer_size;
  281. }
  282. buffer_size_add(prz, c);
  283. start = buffer_start_add(prz, c);
  284. rem = prz->buffer_size - start;
  285. if (unlikely(rem < c)) {
  286. persistent_ram_update(prz, s, start, rem);
  287. s += rem;
  288. c -= rem;
  289. start = 0;
  290. }
  291. persistent_ram_update(prz, s, start, c);
  292. persistent_ram_update_header_ecc(prz);
  293. return count;
  294. }
  295. int notrace persistent_ram_write_user(struct persistent_ram_zone *prz,
  296. const void __user *s, unsigned int count)
  297. {
  298. int rem, ret = 0, c = count;
  299. size_t start;
  300. if (unlikely(c > prz->buffer_size)) {
  301. s += c - prz->buffer_size;
  302. c = prz->buffer_size;
  303. }
  304. buffer_size_add(prz, c);
  305. start = buffer_start_add(prz, c);
  306. rem = prz->buffer_size - start;
  307. if (unlikely(rem < c)) {
  308. ret = persistent_ram_update_user(prz, s, start, rem);
  309. s += rem;
  310. c -= rem;
  311. start = 0;
  312. }
  313. if (likely(!ret))
  314. ret = persistent_ram_update_user(prz, s, start, c);
  315. persistent_ram_update_header_ecc(prz);
  316. return unlikely(ret) ? ret : count;
  317. }
  318. size_t persistent_ram_old_size(struct persistent_ram_zone *prz)
  319. {
  320. return prz->old_log_size;
  321. }
  322. void *persistent_ram_old(struct persistent_ram_zone *prz)
  323. {
  324. return prz->old_log;
  325. }
  326. void persistent_ram_free_old(struct persistent_ram_zone *prz)
  327. {
  328. kvfree(prz->old_log);
  329. prz->old_log = NULL;
  330. prz->old_log_size = 0;
  331. }
  332. void persistent_ram_zap(struct persistent_ram_zone *prz)
  333. {
  334. atomic_set(&prz->buffer->start, 0);
  335. atomic_set(&prz->buffer->size, 0);
  336. persistent_ram_update_header_ecc(prz);
  337. }
  338. #define MEM_TYPE_WCOMBINE 0
  339. #define MEM_TYPE_NONCACHED 1
  340. #define MEM_TYPE_NORMAL 2
  341. static void *persistent_ram_vmap(phys_addr_t start, size_t size,
  342. unsigned int memtype)
  343. {
  344. struct page **pages;
  345. phys_addr_t page_start;
  346. unsigned int page_count;
  347. pgprot_t prot;
  348. unsigned int i;
  349. void *vaddr;
  350. page_start = start - offset_in_page(start);
  351. page_count = DIV_ROUND_UP(size + offset_in_page(start), PAGE_SIZE);
  352. switch (memtype) {
  353. case MEM_TYPE_NORMAL:
  354. prot = PAGE_KERNEL;
  355. break;
  356. case MEM_TYPE_NONCACHED:
  357. prot = pgprot_noncached(PAGE_KERNEL);
  358. break;
  359. case MEM_TYPE_WCOMBINE:
  360. prot = pgprot_writecombine(PAGE_KERNEL);
  361. break;
  362. default:
  363. pr_err("invalid mem_type=%d\n", memtype);
  364. return NULL;
  365. }
  366. pages = kmalloc_objs(struct page *, page_count);
  367. if (!pages) {
  368. pr_err("%s: Failed to allocate array for %u pages\n",
  369. __func__, page_count);
  370. return NULL;
  371. }
  372. for (i = 0; i < page_count; i++) {
  373. phys_addr_t addr = page_start + i * PAGE_SIZE;
  374. pages[i] = pfn_to_page(addr >> PAGE_SHIFT);
  375. }
  376. /*
  377. * VM_IOREMAP used here to bypass this region during vread()
  378. * and kmap_atomic() (i.e. kcore) to avoid __va() failures.
  379. */
  380. vaddr = vmap(pages, page_count, VM_MAP | VM_IOREMAP, prot);
  381. kfree(pages);
  382. /*
  383. * vmap() may fail and return NULL. Do not add the offset in this
  384. * case, otherwise a NULL mapping would appear successful.
  385. */
  386. if (!vaddr)
  387. return NULL;
  388. /*
  389. * Since vmap() uses page granularity, we must add the offset
  390. * into the page here, to get the byte granularity address
  391. * into the mapping to represent the actual "start" location.
  392. */
  393. return vaddr + offset_in_page(start);
  394. }
  395. static void *persistent_ram_iomap(phys_addr_t start, size_t size,
  396. unsigned int memtype, char *label)
  397. {
  398. void *va;
  399. if (!request_mem_region(start, size, label ?: "ramoops")) {
  400. pr_err("request mem region (%s 0x%llx@0x%llx) failed\n",
  401. label ?: "ramoops",
  402. (unsigned long long)size, (unsigned long long)start);
  403. return NULL;
  404. }
  405. if (memtype)
  406. va = ioremap(start, size);
  407. else
  408. va = ioremap_wc(start, size);
  409. /*
  410. * Since request_mem_region() and ioremap() are byte-granularity
  411. * there is no need handle anything special like we do when the
  412. * vmap() case in persistent_ram_vmap() above.
  413. */
  414. return va;
  415. }
  416. static int persistent_ram_buffer_map(phys_addr_t start, phys_addr_t size,
  417. struct persistent_ram_zone *prz, int memtype)
  418. {
  419. prz->paddr = start;
  420. prz->size = size;
  421. if (pfn_valid(start >> PAGE_SHIFT))
  422. prz->vaddr = persistent_ram_vmap(start, size, memtype);
  423. else
  424. prz->vaddr = persistent_ram_iomap(start, size, memtype,
  425. prz->label);
  426. if (!prz->vaddr) {
  427. pr_err("%s: Failed to map 0x%llx pages at 0x%llx\n", __func__,
  428. (unsigned long long)size, (unsigned long long)start);
  429. return -ENOMEM;
  430. }
  431. prz->buffer = prz->vaddr;
  432. prz->buffer_size = size - sizeof(struct persistent_ram_buffer);
  433. return 0;
  434. }
  435. static int persistent_ram_post_init(struct persistent_ram_zone *prz, u32 sig,
  436. struct persistent_ram_ecc_info *ecc_info)
  437. {
  438. int ret;
  439. bool zap = !!(prz->flags & PRZ_FLAG_ZAP_OLD);
  440. ret = persistent_ram_init_ecc(prz, ecc_info);
  441. if (ret) {
  442. pr_warn("ECC failed %s\n", prz->label);
  443. return ret;
  444. }
  445. sig ^= PERSISTENT_RAM_SIG;
  446. if (prz->buffer->sig == sig) {
  447. if (buffer_size(prz) == 0 && buffer_start(prz) == 0) {
  448. pr_debug("found existing empty buffer\n");
  449. return 0;
  450. }
  451. if (buffer_size(prz) > prz->buffer_size ||
  452. buffer_start(prz) > buffer_size(prz)) {
  453. pr_info("found existing invalid buffer, size %zu, start %zu\n",
  454. buffer_size(prz), buffer_start(prz));
  455. zap = true;
  456. } else {
  457. pr_debug("found existing buffer, size %zu, start %zu\n",
  458. buffer_size(prz), buffer_start(prz));
  459. persistent_ram_save_old(prz);
  460. }
  461. } else {
  462. pr_debug("no valid data in buffer (sig = 0x%08x)\n",
  463. prz->buffer->sig);
  464. prz->buffer->sig = sig;
  465. zap = true;
  466. }
  467. /* Reset missing, invalid, or single-use memory area. */
  468. if (zap)
  469. persistent_ram_zap(prz);
  470. return 0;
  471. }
  472. void persistent_ram_free(struct persistent_ram_zone **_prz)
  473. {
  474. struct persistent_ram_zone *prz;
  475. if (!_prz)
  476. return;
  477. prz = *_prz;
  478. if (!prz)
  479. return;
  480. if (prz->vaddr) {
  481. if (pfn_valid(prz->paddr >> PAGE_SHIFT)) {
  482. /* We must vunmap() at page-granularity. */
  483. vunmap(prz->vaddr - offset_in_page(prz->paddr));
  484. } else {
  485. iounmap(prz->vaddr);
  486. release_mem_region(prz->paddr, prz->size);
  487. }
  488. prz->vaddr = NULL;
  489. }
  490. if (prz->rs_decoder) {
  491. free_rs(prz->rs_decoder);
  492. prz->rs_decoder = NULL;
  493. }
  494. kfree(prz->ecc_info.par);
  495. prz->ecc_info.par = NULL;
  496. persistent_ram_free_old(prz);
  497. kfree(prz->label);
  498. kfree(prz);
  499. *_prz = NULL;
  500. }
  501. struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
  502. u32 sig, struct persistent_ram_ecc_info *ecc_info,
  503. unsigned int memtype, u32 flags, char *label)
  504. {
  505. struct persistent_ram_zone *prz;
  506. int ret = -ENOMEM;
  507. prz = kzalloc_obj(struct persistent_ram_zone);
  508. if (!prz) {
  509. pr_err("failed to allocate persistent ram zone\n");
  510. goto err;
  511. }
  512. /* Initialize general buffer state. */
  513. raw_spin_lock_init(&prz->buffer_lock);
  514. prz->flags = flags;
  515. prz->label = kstrdup(label, GFP_KERNEL);
  516. if (!prz->label)
  517. goto err;
  518. ret = persistent_ram_buffer_map(start, size, prz, memtype);
  519. if (ret)
  520. goto err;
  521. ret = persistent_ram_post_init(prz, sig, ecc_info);
  522. if (ret)
  523. goto err;
  524. pr_debug("attached %s 0x%zx@0x%llx: %zu header, %zu data, %zu ecc (%d/%d)\n",
  525. prz->label, prz->size, (unsigned long long)prz->paddr,
  526. sizeof(*prz->buffer), prz->buffer_size,
  527. prz->size - sizeof(*prz->buffer) - prz->buffer_size,
  528. prz->ecc_info.ecc_size, prz->ecc_info.block_size);
  529. return prz;
  530. err:
  531. persistent_ram_free(&prz);
  532. return ERR_PTR(ret);
  533. }