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