]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/pstore/ram_core.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / fs / pstore / ram_core.c
CommitLineData
c672528a
CC
1/*
2 * Copyright (C) 2012 Google, Inc.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
ef748853
FF
15#define pr_fmt(fmt) "persistent_ram: " fmt
16
404a6043
CC
17#include <linux/device.h>
18#include <linux/err.h>
c672528a 19#include <linux/errno.h>
c672528a
CC
20#include <linux/init.h>
21#include <linux/io.h>
5bf6d1b9 22#include <linux/kernel.h>
404a6043
CC
23#include <linux/list.h>
24#include <linux/memblock.h>
5bf6d1b9 25#include <linux/pstore_ram.h>
9cc05ad9 26#include <linux/rslib.h>
c672528a 27#include <linux/slab.h>
5bf6d1b9 28#include <linux/uaccess.h>
404a6043 29#include <linux/vmalloc.h>
24c3d2f3 30#include <asm/page.h>
c672528a 31
c672528a
CC
32struct persistent_ram_buffer {
33 uint32_t sig;
808d0387
CC
34 atomic_t start;
35 atomic_t size;
c672528a
CC
36 uint8_t data[0];
37};
38
39#define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */
40
808d0387
CC
41static inline size_t buffer_size(struct persistent_ram_zone *prz)
42{
43 return atomic_read(&prz->buffer->size);
44}
45
46static inline size_t buffer_start(struct persistent_ram_zone *prz)
47{
48 return atomic_read(&prz->buffer->start);
49}
50
0405a5ce 51/* increase and wrap the start pointer, returning the old value */
d5a9bf0b 52static size_t buffer_start_add(struct persistent_ram_zone *prz, size_t a)
0405a5ce
RH
53{
54 int old;
55 int new;
663deb47 56 unsigned long flags = 0;
0405a5ce 57
663deb47
JF
58 if (!(prz->flags & PRZ_FLAG_NO_LOCK))
59 raw_spin_lock_irqsave(&prz->buffer_lock, flags);
0405a5ce
RH
60
61 old = atomic_read(&prz->buffer->start);
62 new = old + a;
017321cf 63 while (unlikely(new >= prz->buffer_size))
0405a5ce
RH
64 new -= prz->buffer_size;
65 atomic_set(&prz->buffer->start, new);
66
663deb47
JF
67 if (!(prz->flags & PRZ_FLAG_NO_LOCK))
68 raw_spin_unlock_irqrestore(&prz->buffer_lock, flags);
0405a5ce
RH
69
70 return old;
71}
72
73/* increase the size counter until it hits the max size */
d5a9bf0b 74static void buffer_size_add(struct persistent_ram_zone *prz, size_t a)
0405a5ce
RH
75{
76 size_t old;
77 size_t new;
663deb47 78 unsigned long flags = 0;
0405a5ce 79
663deb47
JF
80 if (!(prz->flags & PRZ_FLAG_NO_LOCK))
81 raw_spin_lock_irqsave(&prz->buffer_lock, flags);
0405a5ce
RH
82
83 old = atomic_read(&prz->buffer->size);
84 if (old == prz->buffer_size)
85 goto exit;
86
87 new = old + a;
88 if (new > prz->buffer_size)
89 new = prz->buffer_size;
90 atomic_set(&prz->buffer->size, new);
91
92exit:
663deb47
JF
93 if (!(prz->flags & PRZ_FLAG_NO_LOCK))
94 raw_spin_unlock_irqrestore(&prz->buffer_lock, flags);
0405a5ce
RH
95}
96
a15d0b36 97static void notrace persistent_ram_encode_rs8(struct persistent_ram_zone *prz,
c672528a
CC
98 uint8_t *data, size_t len, uint8_t *ecc)
99{
100 int i;
c31ad081 101 uint16_t par[prz->ecc_info.ecc_size];
9cc05ad9 102
c672528a
CC
103 /* Initialize the parity buffer */
104 memset(par, 0, sizeof(par));
105 encode_rs8(prz->rs_decoder, data, len, par, 0);
c31ad081 106 for (i = 0; i < prz->ecc_info.ecc_size; i++)
c672528a
CC
107 ecc[i] = par[i];
108}
109
110static int persistent_ram_decode_rs8(struct persistent_ram_zone *prz,
111 void *data, size_t len, uint8_t *ecc)
112{
113 int i;
c31ad081 114 uint16_t par[prz->ecc_info.ecc_size];
9cc05ad9 115
c31ad081 116 for (i = 0; i < prz->ecc_info.ecc_size; i++)
c672528a
CC
117 par[i] = ecc[i];
118 return decode_rs8(prz->rs_decoder, data, par, len,
119 NULL, 0, NULL, 0, NULL);
120}
c672528a 121
a15d0b36 122static void notrace persistent_ram_update_ecc(struct persistent_ram_zone *prz,
808d0387 123 unsigned int start, unsigned int count)
c672528a
CC
124{
125 struct persistent_ram_buffer *buffer = prz->buffer;
c672528a
CC
126 uint8_t *buffer_end = buffer->data + prz->buffer_size;
127 uint8_t *block;
128 uint8_t *par;
c31ad081
AH
129 int ecc_block_size = prz->ecc_info.block_size;
130 int ecc_size = prz->ecc_info.ecc_size;
131 int size = ecc_block_size;
9cc05ad9 132
c31ad081 133 if (!ecc_size)
9cc05ad9
CC
134 return;
135
808d0387 136 block = buffer->data + (start & ~(ecc_block_size - 1));
c31ad081 137 par = prz->par_buffer + (start / ecc_block_size) * ecc_size;
808d0387 138
c672528a 139 do {
9cc05ad9 140 if (block + ecc_block_size > buffer_end)
c672528a
CC
141 size = buffer_end - block;
142 persistent_ram_encode_rs8(prz, block, size, par);
9cc05ad9
CC
143 block += ecc_block_size;
144 par += ecc_size;
808d0387 145 } while (block < buffer->data + start + count);
c672528a
CC
146}
147
9cc05ad9 148static void persistent_ram_update_header_ecc(struct persistent_ram_zone *prz)
c672528a 149{
c672528a
CC
150 struct persistent_ram_buffer *buffer = prz->buffer;
151
c31ad081 152 if (!prz->ecc_info.ecc_size)
9cc05ad9
CC
153 return;
154
c672528a
CC
155 persistent_ram_encode_rs8(prz, (uint8_t *)buffer, sizeof(*buffer),
156 prz->par_header);
c672528a
CC
157}
158
9cc05ad9 159static void persistent_ram_ecc_old(struct persistent_ram_zone *prz)
c672528a
CC
160{
161 struct persistent_ram_buffer *buffer = prz->buffer;
c672528a
CC
162 uint8_t *block;
163 uint8_t *par;
164
c31ad081 165 if (!prz->ecc_info.ecc_size)
9cc05ad9
CC
166 return;
167
c672528a
CC
168 block = buffer->data;
169 par = prz->par_buffer;
808d0387 170 while (block < buffer->data + buffer_size(prz)) {
c672528a 171 int numerr;
c31ad081 172 int size = prz->ecc_info.block_size;
c672528a
CC
173 if (block + size > buffer->data + prz->buffer_size)
174 size = buffer->data + prz->buffer_size - block;
175 numerr = persistent_ram_decode_rs8(prz, block, size, par);
176 if (numerr > 0) {
ef748853 177 pr_devel("error in block %p, %d\n", block, numerr);
c672528a
CC
178 prz->corrected_bytes += numerr;
179 } else if (numerr < 0) {
ef748853 180 pr_devel("uncorrectable error in block %p\n", block);
c672528a
CC
181 prz->bad_blocks++;
182 }
c31ad081
AH
183 block += prz->ecc_info.block_size;
184 par += prz->ecc_info.ecc_size;
9cc05ad9
CC
185 }
186}
187
5ca5d4e6 188static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,
c31ad081 189 struct persistent_ram_ecc_info *ecc_info)
9cc05ad9
CC
190{
191 int numerr;
192 struct persistent_ram_buffer *buffer = prz->buffer;
193 int ecc_blocks;
1e6a9e56 194 size_t ecc_total;
9cc05ad9 195
c31ad081 196 if (!ecc_info || !ecc_info->ecc_size)
9cc05ad9
CC
197 return 0;
198
c31ad081
AH
199 prz->ecc_info.block_size = ecc_info->block_size ?: 128;
200 prz->ecc_info.ecc_size = ecc_info->ecc_size ?: 16;
201 prz->ecc_info.symsize = ecc_info->symsize ?: 8;
202 prz->ecc_info.poly = ecc_info->poly ?: 0x11d;
9cc05ad9 203
c31ad081
AH
204 ecc_blocks = DIV_ROUND_UP(prz->buffer_size - prz->ecc_info.ecc_size,
205 prz->ecc_info.block_size +
206 prz->ecc_info.ecc_size);
207 ecc_total = (ecc_blocks + 1) * prz->ecc_info.ecc_size;
1e6a9e56
AV
208 if (ecc_total >= prz->buffer_size) {
209 pr_err("%s: invalid ecc_size %u (total %zu, buffer size %zu)\n",
c31ad081
AH
210 __func__, prz->ecc_info.ecc_size,
211 ecc_total, prz->buffer_size);
9cc05ad9
CC
212 return -EINVAL;
213 }
214
1e6a9e56 215 prz->buffer_size -= ecc_total;
9cc05ad9 216 prz->par_buffer = buffer->data + prz->buffer_size;
c31ad081
AH
217 prz->par_header = prz->par_buffer +
218 ecc_blocks * prz->ecc_info.ecc_size;
9cc05ad9
CC
219
220 /*
221 * first consecutive root is 0
222 * primitive element to generate roots = 1
223 */
c31ad081
AH
224 prz->rs_decoder = init_rs(prz->ecc_info.symsize, prz->ecc_info.poly,
225 0, 1, prz->ecc_info.ecc_size);
9cc05ad9 226 if (prz->rs_decoder == NULL) {
ef748853 227 pr_info("init_rs failed\n");
9cc05ad9 228 return -EINVAL;
c672528a 229 }
9cc05ad9
CC
230
231 prz->corrected_bytes = 0;
232 prz->bad_blocks = 0;
233
234 numerr = persistent_ram_decode_rs8(prz, buffer, sizeof(*buffer),
235 prz->par_header);
236 if (numerr > 0) {
ef748853 237 pr_info("error in header, %d\n", numerr);
9cc05ad9
CC
238 prz->corrected_bytes += numerr;
239 } else if (numerr < 0) {
ef748853 240 pr_info("uncorrectable error in header\n");
9cc05ad9
CC
241 prz->bad_blocks++;
242 }
243
244 return 0;
245}
246
247ssize_t persistent_ram_ecc_string(struct persistent_ram_zone *prz,
248 char *str, size_t len)
249{
250 ssize_t ret;
251
bd08ec33
AH
252 if (!prz->ecc_info.ecc_size)
253 return 0;
254
9cc05ad9
CC
255 if (prz->corrected_bytes || prz->bad_blocks)
256 ret = snprintf(str, len, ""
257 "\n%d Corrected bytes, %d unrecoverable blocks\n",
258 prz->corrected_bytes, prz->bad_blocks);
259 else
260 ret = snprintf(str, len, "\nNo errors detected\n");
261
262 return ret;
263}
264
a15d0b36 265static void notrace persistent_ram_update(struct persistent_ram_zone *prz,
808d0387 266 const void *s, unsigned int start, unsigned int count)
9cc05ad9
CC
267{
268 struct persistent_ram_buffer *buffer = prz->buffer;
7e75678d 269 memcpy_toio(buffer->data + start, s, count);
808d0387 270 persistent_ram_update_ecc(prz, start, count);
9cc05ad9
CC
271}
272
5bf6d1b9
MS
273static int notrace persistent_ram_update_user(struct persistent_ram_zone *prz,
274 const void __user *s, unsigned int start, unsigned int count)
275{
276 struct persistent_ram_buffer *buffer = prz->buffer;
277 int ret = unlikely(__copy_from_user(buffer->data + start, s, count)) ?
278 -EFAULT : 0;
279 persistent_ram_update_ecc(prz, start, count);
280 return ret;
281}
282
201e4aca 283void persistent_ram_save_old(struct persistent_ram_zone *prz)
9cc05ad9
CC
284{
285 struct persistent_ram_buffer *buffer = prz->buffer;
808d0387
CC
286 size_t size = buffer_size(prz);
287 size_t start = buffer_start(prz);
9cc05ad9 288
201e4aca
AV
289 if (!size)
290 return;
c672528a 291
201e4aca
AV
292 if (!prz->old_log) {
293 persistent_ram_ecc_old(prz);
294 prz->old_log = kmalloc(size, GFP_KERNEL);
295 }
296 if (!prz->old_log) {
ef748853 297 pr_err("failed to allocate buffer\n");
c672528a
CC
298 return;
299 }
300
808d0387 301 prz->old_log_size = size;
d771fdf9
AB
302 memcpy_fromio(prz->old_log, &buffer->data[start], size - start);
303 memcpy_fromio(prz->old_log + size - start, &buffer->data[0], start);
c672528a
CC
304}
305
a15d0b36 306int notrace persistent_ram_write(struct persistent_ram_zone *prz,
c672528a
CC
307 const void *s, unsigned int count)
308{
309 int rem;
310 int c = count;
808d0387 311 size_t start;
c672528a 312
808d0387 313 if (unlikely(c > prz->buffer_size)) {
c672528a
CC
314 s += c - prz->buffer_size;
315 c = prz->buffer_size;
316 }
808d0387 317
484dd30e 318 buffer_size_add(prz, c);
808d0387
CC
319
320 start = buffer_start_add(prz, c);
321
322 rem = prz->buffer_size - start;
323 if (unlikely(rem < c)) {
324 persistent_ram_update(prz, s, start, rem);
c672528a
CC
325 s += rem;
326 c -= rem;
808d0387 327 start = 0;
c672528a 328 }
808d0387 329 persistent_ram_update(prz, s, start, c);
c672528a 330
9cc05ad9 331 persistent_ram_update_header_ecc(prz);
c672528a
CC
332
333 return count;
334}
335
5bf6d1b9
MS
336int notrace persistent_ram_write_user(struct persistent_ram_zone *prz,
337 const void __user *s, unsigned int count)
338{
339 int rem, ret = 0, c = count;
340 size_t start;
341
342 if (unlikely(!access_ok(VERIFY_READ, s, count)))
343 return -EFAULT;
344 if (unlikely(c > prz->buffer_size)) {
345 s += c - prz->buffer_size;
346 c = prz->buffer_size;
347 }
348
349 buffer_size_add(prz, c);
350
351 start = buffer_start_add(prz, c);
352
353 rem = prz->buffer_size - start;
354 if (unlikely(rem < c)) {
355 ret = persistent_ram_update_user(prz, s, start, rem);
356 s += rem;
357 c -= rem;
358 start = 0;
359 }
360 if (likely(!ret))
361 ret = persistent_ram_update_user(prz, s, start, c);
362
363 persistent_ram_update_header_ecc(prz);
364
365 return unlikely(ret) ? ret : count;
366}
367
c672528a
CC
368size_t persistent_ram_old_size(struct persistent_ram_zone *prz)
369{
370 return prz->old_log_size;
371}
372
373void *persistent_ram_old(struct persistent_ram_zone *prz)
374{
375 return prz->old_log;
376}
377
378void persistent_ram_free_old(struct persistent_ram_zone *prz)
379{
380 kfree(prz->old_log);
381 prz->old_log = NULL;
382 prz->old_log_size = 0;
383}
384
fce39793
AV
385void persistent_ram_zap(struct persistent_ram_zone *prz)
386{
387 atomic_set(&prz->buffer->start, 0);
388 atomic_set(&prz->buffer->size, 0);
389 persistent_ram_update_header_ecc(prz);
390}
391
027bc8b0
TL
392static void *persistent_ram_vmap(phys_addr_t start, size_t size,
393 unsigned int memtype)
c672528a 394{
404a6043
CC
395 struct page **pages;
396 phys_addr_t page_start;
397 unsigned int page_count;
398 pgprot_t prot;
399 unsigned int i;
2b1321e4 400 void *vaddr;
404a6043
CC
401
402 page_start = start - offset_in_page(start);
403 page_count = DIV_ROUND_UP(size + offset_in_page(start), PAGE_SIZE);
404
027bc8b0
TL
405 if (memtype)
406 prot = pgprot_noncached(PAGE_KERNEL);
407 else
408 prot = pgprot_writecombine(PAGE_KERNEL);
404a6043 409
b8f52d89 410 pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL);
404a6043 411 if (!pages) {
ef748853
FF
412 pr_err("%s: Failed to allocate array for %u pages\n",
413 __func__, page_count);
2b1321e4 414 return NULL;
404a6043
CC
415 }
416
417 for (i = 0; i < page_count; i++) {
418 phys_addr_t addr = page_start + i * PAGE_SIZE;
419 pages[i] = pfn_to_page(addr >> PAGE_SHIFT);
420 }
2b1321e4 421 vaddr = vmap(pages, page_count, VM_MAP, prot);
404a6043 422 kfree(pages);
2b1321e4 423
4871620c
BY
424 /*
425 * Since vmap() uses page granularity, we must add the offset
426 * into the page here, to get the byte granularity address
427 * into the mapping to represent the actual "start" location.
428 */
429 return vaddr + offset_in_page(start);
2b1321e4
AV
430}
431
027bc8b0
TL
432static void *persistent_ram_iomap(phys_addr_t start, size_t size,
433 unsigned int memtype)
24c3d2f3 434{
027bc8b0
TL
435 void *va;
436
24c3d2f3
AV
437 if (!request_mem_region(start, size, "persistent_ram")) {
438 pr_err("request mem region (0x%llx@0x%llx) failed\n",
439 (unsigned long long)size, (unsigned long long)start);
440 return NULL;
441 }
442
027bc8b0
TL
443 if (memtype)
444 va = ioremap(start, size);
445 else
446 va = ioremap_wc(start, size);
447
4871620c
BY
448 /*
449 * Since request_mem_region() and ioremap() are byte-granularity
450 * there is no need handle anything special like we do when the
451 * vmap() case in persistent_ram_vmap() above.
452 */
027bc8b0 453 return va;
24c3d2f3
AV
454}
455
2b1321e4 456static int persistent_ram_buffer_map(phys_addr_t start, phys_addr_t size,
027bc8b0 457 struct persistent_ram_zone *prz, int memtype)
2b1321e4 458{
d3b48769
AV
459 prz->paddr = start;
460 prz->size = size;
461
24c3d2f3 462 if (pfn_valid(start >> PAGE_SHIFT))
027bc8b0 463 prz->vaddr = persistent_ram_vmap(start, size, memtype);
24c3d2f3 464 else
027bc8b0 465 prz->vaddr = persistent_ram_iomap(start, size, memtype);
24c3d2f3 466
404a6043 467 if (!prz->vaddr) {
2b1321e4
AV
468 pr_err("%s: Failed to map 0x%llx pages at 0x%llx\n", __func__,
469 (unsigned long long)size, (unsigned long long)start);
404a6043
CC
470 return -ENOMEM;
471 }
472
4871620c 473 prz->buffer = prz->vaddr;
404a6043
CC
474 prz->buffer_size = size - sizeof(struct persistent_ram_buffer);
475
476 return 0;
477}
478
f568f6ca 479static int persistent_ram_post_init(struct persistent_ram_zone *prz, u32 sig,
76d5692a 480 struct persistent_ram_ecc_info *ecc_info)
404a6043 481{
bb4206f2 482 int ret;
c672528a 483
c31ad081 484 ret = persistent_ram_init_ecc(prz, ecc_info);
9cc05ad9 485 if (ret)
bb4206f2 486 return ret;
c672528a 487
cbe7cbf5
AV
488 sig ^= PERSISTENT_RAM_SIG;
489
490 if (prz->buffer->sig == sig) {
02e9bdd8
JFG
491 if (buffer_size(prz) == 0) {
492 pr_debug("found existing empty buffer\n");
493 return 0;
494 }
495
808d0387
CC
496 if (buffer_size(prz) > prz->buffer_size ||
497 buffer_start(prz) > buffer_size(prz))
ef748853
FF
498 pr_info("found existing invalid buffer, size %zu, start %zu\n",
499 buffer_size(prz), buffer_start(prz));
c672528a 500 else {
ef748853
FF
501 pr_debug("found existing buffer, size %zu, start %zu\n",
502 buffer_size(prz), buffer_start(prz));
c672528a 503 persistent_ram_save_old(prz);
25b63da6 504 return 0;
c672528a
CC
505 }
506 } else {
ef748853
FF
507 pr_debug("no valid data in buffer (sig = 0x%08x)\n",
508 prz->buffer->sig);
c672528a
CC
509 }
510
76d5692a 511 /* Rewind missing or invalid memory area. */
cbe7cbf5 512 prz->buffer->sig = sig;
fce39793 513 persistent_ram_zap(prz);
c672528a 514
bb4206f2
AV
515 return 0;
516}
517
d3b48769
AV
518void persistent_ram_free(struct persistent_ram_zone *prz)
519{
beeb9432
AV
520 if (!prz)
521 return;
522
523 if (prz->vaddr) {
524 if (pfn_valid(prz->paddr >> PAGE_SHIFT)) {
4871620c
BY
525 /* We must vunmap() at page-granularity. */
526 vunmap(prz->vaddr - offset_in_page(prz->paddr));
beeb9432
AV
527 } else {
528 iounmap(prz->vaddr);
529 release_mem_region(prz->paddr, prz->size);
530 }
531 prz->vaddr = NULL;
d3b48769
AV
532 }
533 persistent_ram_free_old(prz);
534 kfree(prz);
535}
536
f568f6ca 537struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
027bc8b0 538 u32 sig, struct persistent_ram_ecc_info *ecc_info,
663deb47 539 unsigned int memtype, u32 flags)
8cf5aff8
AV
540{
541 struct persistent_ram_zone *prz;
542 int ret = -ENOMEM;
543
544 prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
545 if (!prz) {
ef748853 546 pr_err("failed to allocate persistent ram zone\n");
8cf5aff8
AV
547 goto err;
548 }
549
76d5692a 550 /* Initialize general buffer state. */
e9a330c4 551 raw_spin_lock_init(&prz->buffer_lock);
76d5692a
KC
552 prz->flags = flags;
553
027bc8b0 554 ret = persistent_ram_buffer_map(start, size, prz, memtype);
8cf5aff8
AV
555 if (ret)
556 goto err;
557
76d5692a 558 ret = persistent_ram_post_init(prz, sig, ecc_info);
beeb9432
AV
559 if (ret)
560 goto err;
8cf5aff8
AV
561
562 return prz;
563err:
beeb9432 564 persistent_ram_free(prz);
8cf5aff8
AV
565 return ERR_PTR(ret);
566}