1 // SPDX-License-Identifier: GPL-2.0
3 * Provide a pstore intermediate backend, organized into kernel memory
4 * allocated zones that are then mapped and flushed into a single
5 * contiguous region on a storage backend of some kind (block, mtd, etc).
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/mount.h>
14 #include <linux/printk.h>
16 #include <linux/pstore_zone.h>
17 #include <linux/kdev_t.h>
18 #include <linux/device.h>
19 #include <linux/namei.h>
20 #include <linux/fcntl.h>
21 #include <linux/uio.h>
22 #include <linux/writeback.h>
26 * struct psz_buffer - header of zone to flush to storage
28 * @sig: signature to indicate header (PSZ_SIG xor PSZONE-type value)
29 * @datalen: length of data in @data
30 * @start: offset into @data where the beginning of the stored bytes begin
34 #define PSZ_SIG (0x43474244) /* DBGC */
42 * struct psz_kmsg_header - kmsg dump-specific header to flush to storage
44 * @magic: magic num for kmsg dump header
45 * @time: kmsg dump trigger time
46 * @compressed: whether conpressed
47 * @counter: kmsg dump counter
48 * @reason: the kmsg dump reason (e.g. oops, panic, etc)
49 * @data: pointer to log data
51 * This is a sub-header for a kmsg dump, trailing after &psz_buffer.
53 struct psz_kmsg_header {
54 #define PSTORE_KMSG_HEADER_MAGIC 0x4dfc3ae5 /* Just a random number */
56 struct timespec64 time;
59 enum kmsg_dump_reason reason;
64 * struct pstore_zone - single stored buffer
66 * @off: zone offset of storage
67 * @type: front-end type for this zone
68 * @name: front-end name for this zone
69 * @buffer: pointer to data buffer managed by this zone
70 * @oldbuf: pointer to old data buffer
71 * @buffer_size: bytes in @buffer->data
72 * @should_recover: whether this zone should recover from storage
73 * @dirty: whether the data in @buffer dirty
75 * zone structure in memory.
80 enum pstore_type_id type;
82 struct psz_buffer *buffer;
83 struct psz_buffer *oldbuf;
90 * struct psz_context - all about running state of pstore/zone
92 * @kpszs: kmsg dump storage zones
93 * @ppsz: pmsg storage zone
94 * @cpsz: console storage zone
95 * @fpszs: ftrace storage zones
96 * @kmsg_max_cnt: max count of @kpszs
97 * @kmsg_read_cnt: counter of total read kmsg dumps
98 * @kmsg_write_cnt: counter of total kmsg dump writes
99 * @pmsg_read_cnt: counter of total read pmsg zone
100 * @console_read_cnt: counter of total read console zone
101 * @ftrace_max_cnt: max count of @fpszs
102 * @ftrace_read_cnt: counter of max read ftrace zone
103 * @oops_counter: counter of oops dumps
104 * @panic_counter: counter of panic dumps
105 * @recovered: whether finished recovering data from storage
106 * @on_panic: whether panic is happening
107 * @pstore_zone_info_lock: lock to @pstore_zone_info
108 * @pstore_zone_info: information from backend
109 * @pstore: structure for pstore
112 struct pstore_zone **kpszs;
113 struct pstore_zone *ppsz;
114 struct pstore_zone *cpsz;
115 struct pstore_zone **fpszs;
116 unsigned int kmsg_max_cnt;
117 unsigned int kmsg_read_cnt;
118 unsigned int kmsg_write_cnt;
119 unsigned int pmsg_read_cnt;
120 unsigned int console_read_cnt;
121 unsigned int ftrace_max_cnt;
122 unsigned int ftrace_read_cnt;
124 * These counters should be calculated during recovery.
125 * It records the oops/panic times after crashes rather than boots.
127 unsigned int oops_counter;
128 unsigned int panic_counter;
133 * pstore_zone_info_lock protects this entire structure during calls
134 * to register_pstore_zone()/unregister_pstore_zone().
136 struct mutex pstore_zone_info_lock;
137 struct pstore_zone_info *pstore_zone_info;
138 struct pstore_info pstore;
140 static struct psz_context pstore_zone_cxt;
142 static void psz_flush_all_dirty_zones(struct work_struct *);
143 static DECLARE_DELAYED_WORK(psz_cleaner, psz_flush_all_dirty_zones);
146 * enum psz_flush_mode - flush mode for psz_zone_write()
148 * @FLUSH_NONE: do not flush to storage but update data on memory
149 * @FLUSH_PART: just flush part of data including meta data to storage
150 * @FLUSH_META: just flush meta data of zone to storage
151 * @FLUSH_ALL: flush all of zone
153 enum psz_flush_mode {
160 static inline int buffer_datalen(struct pstore_zone *zone)
162 return atomic_read(&zone->buffer->datalen);
165 static inline int buffer_start(struct pstore_zone *zone)
167 return atomic_read(&zone->buffer->start);
170 static inline bool is_on_panic(void)
172 return atomic_read(&pstore_zone_cxt.on_panic);
175 static ssize_t psz_zone_read_buffer(struct pstore_zone *zone, char *buf,
176 size_t len, unsigned long off)
178 if (!buf || !zone || !zone->buffer)
180 if (off > zone->buffer_size)
182 len = min_t(size_t, len, zone->buffer_size - off);
183 memcpy(buf, zone->buffer->data + off, len);
187 static int psz_zone_read_oldbuf(struct pstore_zone *zone, char *buf,
188 size_t len, unsigned long off)
190 if (!buf || !zone || !zone->oldbuf)
192 if (off > zone->buffer_size)
194 len = min_t(size_t, len, zone->buffer_size - off);
195 memcpy(buf, zone->oldbuf->data + off, len);
199 static int psz_zone_write(struct pstore_zone *zone,
200 enum psz_flush_mode flush_mode, const char *buf,
201 size_t len, unsigned long off)
203 struct pstore_zone_info *info = pstore_zone_cxt.pstore_zone_info;
205 ssize_t (*writeop)(const char *buf, size_t bytes, loff_t pos);
208 if (off > zone->buffer_size)
211 wlen = min_t(size_t, len, zone->buffer_size - off);
213 memcpy(zone->buffer->data + off, buf, wlen);
214 atomic_set(&zone->buffer->datalen, wlen + off);
217 /* avoid to damage old records */
218 if (!is_on_panic() && !atomic_read(&pstore_zone_cxt.recovered))
221 writeop = is_on_panic() ? info->panic_write : info->write;
225 switch (flush_mode) {
227 if (unlikely(buf && wlen))
231 wcnt = writeop((const char *)zone->buffer->data + off, wlen,
232 zone->off + sizeof(*zone->buffer) + off);
237 wlen = sizeof(struct psz_buffer);
238 wcnt = writeop((const char *)zone->buffer, wlen, zone->off);
243 wlen = zone->buffer_size + sizeof(*zone->buffer);
244 wcnt = writeop((const char *)zone->buffer, wlen, zone->off);
252 /* no need to mark dirty if going to try next zone */
255 atomic_set(&zone->dirty, true);
256 /* flush dirty zones nicely */
257 if (wcnt == -EBUSY && !is_on_panic())
258 schedule_delayed_work(&psz_cleaner, msecs_to_jiffies(500));
262 static int psz_flush_dirty_zone(struct pstore_zone *zone)
269 if (unlikely(!atomic_read(&pstore_zone_cxt.recovered)))
272 if (!atomic_xchg(&zone->dirty, false))
275 ret = psz_zone_write(zone, FLUSH_ALL, NULL, 0, 0);
277 atomic_set(&zone->dirty, true);
281 static int psz_flush_dirty_zones(struct pstore_zone **zones, unsigned int cnt)
284 struct pstore_zone *zone;
289 for (i = 0; i < cnt; i++) {
293 ret = psz_flush_dirty_zone(zone);
300 static int psz_move_zone(struct pstore_zone *old, struct pstore_zone *new)
302 const char *data = (const char *)old->buffer->data;
305 ret = psz_zone_write(new, FLUSH_ALL, data, buffer_datalen(old), 0);
307 atomic_set(&new->buffer->datalen, 0);
308 atomic_set(&new->dirty, false);
311 atomic_set(&old->buffer->datalen, 0);
315 static void psz_flush_all_dirty_zones(struct work_struct *work)
317 struct psz_context *cxt = &pstore_zone_cxt;
321 ret |= psz_flush_dirty_zone(cxt->ppsz);
323 ret |= psz_flush_dirty_zone(cxt->cpsz);
325 ret |= psz_flush_dirty_zones(cxt->kpszs, cxt->kmsg_max_cnt);
327 ret |= psz_flush_dirty_zones(cxt->fpszs, cxt->ftrace_max_cnt);
328 if (ret && cxt->pstore_zone_info)
329 schedule_delayed_work(&psz_cleaner, msecs_to_jiffies(1000));
332 static int psz_kmsg_recover_data(struct psz_context *cxt)
334 struct pstore_zone_info *info = cxt->pstore_zone_info;
335 struct pstore_zone *zone = NULL;
336 struct psz_buffer *buf;
343 for (i = 0; i < cxt->kmsg_max_cnt; i++) {
344 zone = cxt->kpszs[i];
347 if (atomic_read(&zone->dirty)) {
348 unsigned int wcnt = cxt->kmsg_write_cnt;
349 struct pstore_zone *new = cxt->kpszs[wcnt];
352 ret = psz_move_zone(zone, new);
354 pr_err("move zone from %lu to %d failed\n",
358 cxt->kmsg_write_cnt = (wcnt + 1) % cxt->kmsg_max_cnt;
360 if (!zone->should_recover)
363 rcnt = info->read((char *)buf, zone->buffer_size + sizeof(*buf),
365 if (rcnt != zone->buffer_size + sizeof(*buf))
366 return (int)rcnt < 0 ? (int)rcnt : -EIO;
371 static int psz_kmsg_recover_meta(struct psz_context *cxt)
373 struct pstore_zone_info *info = cxt->pstore_zone_info;
374 struct pstore_zone *zone;
376 struct psz_buffer *buf;
377 struct psz_kmsg_header *hdr;
378 struct timespec64 time = { };
381 * Recover may on panic, we can't allocate any memory by kmalloc.
382 * So, we use local array instead.
384 char buffer_header[sizeof(*buf) + sizeof(*hdr)] = {0};
389 len = sizeof(*buf) + sizeof(*hdr);
390 buf = (struct psz_buffer *)buffer_header;
391 for (i = 0; i < cxt->kmsg_max_cnt; i++) {
392 zone = cxt->kpszs[i];
396 rcnt = info->read((char *)buf, len, zone->off);
397 if (rcnt == -ENOMSG) {
398 pr_debug("%s with id %lu may be broken, skip\n",
401 } else if (rcnt != len) {
402 pr_err("read %s with id %lu failed\n", zone->name, i);
403 return (int)rcnt < 0 ? (int)rcnt : -EIO;
406 if (buf->sig != zone->buffer->sig) {
407 pr_debug("no valid data in kmsg dump zone %lu\n", i);
411 if (zone->buffer_size < atomic_read(&buf->datalen)) {
412 pr_info("found overtop zone: %s: id %lu, off %lld, size %zu\n",
413 zone->name, i, zone->off,
418 hdr = (struct psz_kmsg_header *)buf->data;
419 if (hdr->magic != PSTORE_KMSG_HEADER_MAGIC) {
420 pr_info("found invalid zone: %s: id %lu, off %lld, size %zu\n",
421 zone->name, i, zone->off,
427 * we get the newest zone, and the next one must be the oldest
428 * or unused zone, because we do write one by one like a circle.
430 if (hdr->time.tv_sec >= time.tv_sec) {
431 time.tv_sec = hdr->time.tv_sec;
432 cxt->kmsg_write_cnt = (i + 1) % cxt->kmsg_max_cnt;
435 if (hdr->reason == KMSG_DUMP_OOPS)
437 max(cxt->oops_counter, hdr->counter);
438 else if (hdr->reason == KMSG_DUMP_PANIC)
440 max(cxt->panic_counter, hdr->counter);
442 if (!atomic_read(&buf->datalen)) {
443 pr_debug("found erased zone: %s: id %lu, off %lld, size %zu, datalen %d\n",
444 zone->name, i, zone->off,
446 atomic_read(&buf->datalen));
451 zone->should_recover = true;
452 pr_debug("found nice zone: %s: id %lu, off %lld, size %zu, datalen %d\n",
453 zone->name, i, zone->off,
454 zone->buffer_size, atomic_read(&buf->datalen));
460 static int psz_kmsg_recover(struct psz_context *cxt)
467 ret = psz_kmsg_recover_meta(cxt);
471 ret = psz_kmsg_recover_data(cxt);
477 pr_debug("psz_recover_kmsg failed\n");
481 static int psz_recover_zone(struct psz_context *cxt, struct pstore_zone *zone)
483 struct pstore_zone_info *info = cxt->pstore_zone_info;
484 struct psz_buffer *oldbuf, tmpbuf;
487 ssize_t rcnt, len, start, off;
489 if (!zone || zone->oldbuf)
493 /* save data as much as possible */
494 psz_flush_dirty_zone(zone);
498 if (unlikely(!info->read))
501 len = sizeof(struct psz_buffer);
502 rcnt = info->read((char *)&tmpbuf, len, zone->off);
504 pr_debug("read zone %s failed\n", zone->name);
505 return (int)rcnt < 0 ? (int)rcnt : -EIO;
508 if (tmpbuf.sig != zone->buffer->sig) {
509 pr_debug("no valid data in zone %s\n", zone->name);
513 if (zone->buffer_size < atomic_read(&tmpbuf.datalen) ||
514 zone->buffer_size < atomic_read(&tmpbuf.start)) {
515 pr_info("found overtop zone: %s: off %lld, size %zu\n",
516 zone->name, zone->off, zone->buffer_size);
517 /* just keep going */
521 if (!atomic_read(&tmpbuf.datalen)) {
522 pr_debug("found erased zone: %s: off %lld, size %zu, datalen %d\n",
523 zone->name, zone->off, zone->buffer_size,
524 atomic_read(&tmpbuf.datalen));
528 pr_debug("found nice zone: %s: off %lld, size %zu, datalen %d\n",
529 zone->name, zone->off, zone->buffer_size,
530 atomic_read(&tmpbuf.datalen));
532 len = atomic_read(&tmpbuf.datalen) + sizeof(*oldbuf);
533 oldbuf = kzalloc(len, GFP_KERNEL);
537 memcpy(oldbuf, &tmpbuf, sizeof(*oldbuf));
538 buf = (char *)oldbuf + sizeof(*oldbuf);
539 len = atomic_read(&oldbuf->datalen);
540 start = atomic_read(&oldbuf->start);
541 off = zone->off + sizeof(*oldbuf);
543 /* get part of data */
544 rcnt = info->read(buf, len - start, off + start);
545 if (rcnt != len - start) {
546 pr_err("read zone %s failed\n", zone->name);
547 ret = (int)rcnt < 0 ? (int)rcnt : -EIO;
551 /* get the rest of data */
552 rcnt = info->read(buf + len - start, start, off);
554 pr_err("read zone %s failed\n", zone->name);
555 ret = (int)rcnt < 0 ? (int)rcnt : -EIO;
559 zone->oldbuf = oldbuf;
560 psz_flush_dirty_zone(zone);
568 static int psz_recover_zones(struct psz_context *cxt,
569 struct pstore_zone **zones, unsigned int cnt)
573 struct pstore_zone *zone;
578 for (i = 0; i < cnt; i++) {
582 ret = psz_recover_zone(cxt, zone);
589 pr_debug("recover %s[%u] failed\n", zone->name, i);
594 * psz_recovery() - recover data from storage
595 * @cxt: the context of pstore/zone
597 * recovery means reading data back from storage after rebooting
599 * Return: 0 on success, others on failure.
601 static inline int psz_recovery(struct psz_context *cxt)
605 if (atomic_read(&cxt->recovered))
608 ret = psz_kmsg_recover(cxt);
612 ret = psz_recover_zone(cxt, cxt->ppsz);
616 ret = psz_recover_zone(cxt, cxt->cpsz);
620 ret = psz_recover_zones(cxt, cxt->fpszs, cxt->ftrace_max_cnt);
624 pr_err("recover failed\n");
626 pr_debug("recover end!\n");
627 atomic_set(&cxt->recovered, 1);
632 static int psz_pstore_open(struct pstore_info *psi)
634 struct psz_context *cxt = psi->data;
636 cxt->kmsg_read_cnt = 0;
637 cxt->pmsg_read_cnt = 0;
638 cxt->console_read_cnt = 0;
639 cxt->ftrace_read_cnt = 0;
643 static inline bool psz_old_ok(struct pstore_zone *zone)
645 if (zone && zone->oldbuf && atomic_read(&zone->oldbuf->datalen))
650 static inline bool psz_ok(struct pstore_zone *zone)
652 if (zone && zone->buffer && buffer_datalen(zone))
657 static inline int psz_kmsg_erase(struct psz_context *cxt,
658 struct pstore_zone *zone, struct pstore_record *record)
660 struct psz_buffer *buffer = zone->buffer;
661 struct psz_kmsg_header *hdr =
662 (struct psz_kmsg_header *)buffer->data;
665 if (unlikely(!psz_ok(zone)))
668 /* this zone is already updated, no need to erase */
669 if (record->count != hdr->counter)
672 size = buffer_datalen(zone) + sizeof(*zone->buffer);
673 atomic_set(&zone->buffer->datalen, 0);
674 if (cxt->pstore_zone_info->erase)
675 return cxt->pstore_zone_info->erase(size, zone->off);
677 return psz_zone_write(zone, FLUSH_META, NULL, 0, 0);
680 static inline int psz_record_erase(struct psz_context *cxt,
681 struct pstore_zone *zone)
683 if (unlikely(!psz_old_ok(zone)))
689 * if there are new data in zone buffer, that means the old data
690 * are already invalid. It is no need to flush 0 (erase) to
693 if (!buffer_datalen(zone))
694 return psz_zone_write(zone, FLUSH_META, NULL, 0, 0);
695 psz_flush_dirty_zone(zone);
699 static int psz_pstore_erase(struct pstore_record *record)
701 struct psz_context *cxt = record->psi->data;
703 switch (record->type) {
704 case PSTORE_TYPE_DMESG:
705 if (record->id >= cxt->kmsg_max_cnt)
707 return psz_kmsg_erase(cxt, cxt->kpszs[record->id], record);
708 case PSTORE_TYPE_PMSG:
709 return psz_record_erase(cxt, cxt->ppsz);
710 case PSTORE_TYPE_CONSOLE:
711 return psz_record_erase(cxt, cxt->cpsz);
712 case PSTORE_TYPE_FTRACE:
713 if (record->id >= cxt->ftrace_max_cnt)
715 return psz_record_erase(cxt, cxt->fpszs[record->id]);
716 default: return -EINVAL;
720 static void psz_write_kmsg_hdr(struct pstore_zone *zone,
721 struct pstore_record *record)
723 struct psz_context *cxt = record->psi->data;
724 struct psz_buffer *buffer = zone->buffer;
725 struct psz_kmsg_header *hdr =
726 (struct psz_kmsg_header *)buffer->data;
728 hdr->magic = PSTORE_KMSG_HEADER_MAGIC;
729 hdr->compressed = record->compressed;
730 hdr->time.tv_sec = record->time.tv_sec;
731 hdr->time.tv_nsec = record->time.tv_nsec;
732 hdr->reason = record->reason;
733 if (hdr->reason == KMSG_DUMP_OOPS)
734 hdr->counter = ++cxt->oops_counter;
735 else if (hdr->reason == KMSG_DUMP_PANIC)
736 hdr->counter = ++cxt->panic_counter;
742 * In case zone is broken, which may occur to MTD device, we try each zones,
743 * start at cxt->kmsg_write_cnt.
745 static inline int notrace psz_kmsg_write_record(struct psz_context *cxt,
746 struct pstore_record *record)
749 struct pstore_zone *zone;
752 for (i = 0; i < cxt->kmsg_max_cnt; i++) {
753 unsigned int zonenum, len;
756 zonenum = (cxt->kmsg_write_cnt + i) % cxt->kmsg_max_cnt;
757 zone = cxt->kpszs[zonenum];
761 /* avoid destroying old data, allocate a new one */
762 len = zone->buffer_size + sizeof(*zone->buffer);
763 zone->oldbuf = zone->buffer;
764 zone->buffer = kzalloc(len, GFP_KERNEL);
766 zone->buffer = zone->oldbuf;
769 zone->buffer->sig = zone->oldbuf->sig;
771 pr_debug("write %s to zone id %d\n", zone->name, zonenum);
772 psz_write_kmsg_hdr(zone, record);
773 hlen = sizeof(struct psz_kmsg_header);
774 size = min_t(size_t, record->size, zone->buffer_size - hlen);
775 ret = psz_zone_write(zone, FLUSH_ALL, record->buf, size, hlen);
776 if (likely(!ret || ret != -ENOMSG)) {
777 cxt->kmsg_write_cnt = zonenum + 1;
778 cxt->kmsg_write_cnt %= cxt->kmsg_max_cnt;
779 /* no need to try next zone, free last zone buffer */
785 pr_debug("zone %u may be broken, try next dmesg zone\n",
788 zone->buffer = zone->oldbuf;
795 static int notrace psz_kmsg_write(struct psz_context *cxt,
796 struct pstore_record *record)
801 * Explicitly only take the first part of any new crash.
802 * If our buffer is larger than kmsg_bytes, this can never happen,
803 * and if our buffer is smaller than kmsg_bytes, we don't want the
804 * report split across multiple records.
806 if (record->part != 1)
812 ret = psz_kmsg_write_record(cxt, record);
813 if (!ret && is_on_panic()) {
814 /* ensure all data are flushed to storage when panic */
815 pr_debug("try to flush other dirty zones\n");
816 psz_flush_all_dirty_zones(NULL);
819 /* always return 0 as we had handled it on buffer */
823 static int notrace psz_record_write(struct pstore_zone *zone,
824 struct pstore_record *record)
827 bool is_full_data = false;
831 if (!zone || !record)
834 if (atomic_read(&zone->buffer->datalen) >= zone->buffer_size)
839 if (unlikely(cnt > zone->buffer_size)) {
840 buf += cnt - zone->buffer_size;
841 cnt = zone->buffer_size;
844 start = buffer_start(zone);
845 rem = zone->buffer_size - start;
846 if (unlikely(rem < cnt)) {
847 psz_zone_write(zone, FLUSH_PART, buf, rem, start);
854 atomic_set(&zone->buffer->start, cnt + start);
855 psz_zone_write(zone, FLUSH_PART, buf, cnt, start);
858 * psz_zone_write will set datalen as start + cnt.
859 * It work if actual data length lesser than buffer size.
860 * If data length greater than buffer size, pmsg will rewrite to
861 * beginning of zone, which make buffer->datalen wrongly.
862 * So we should reset datalen as buffer size once actual data length
863 * greater than buffer size.
866 atomic_set(&zone->buffer->datalen, zone->buffer_size);
867 psz_zone_write(zone, FLUSH_META, NULL, 0, 0);
872 static int notrace psz_pstore_write(struct pstore_record *record)
874 struct psz_context *cxt = record->psi->data;
876 if (record->type == PSTORE_TYPE_DMESG &&
877 record->reason == KMSG_DUMP_PANIC)
878 atomic_set(&cxt->on_panic, 1);
881 * if on panic, do not write except panic records
882 * Fix case that panic_write prints log which wakes up console backend.
884 if (is_on_panic() && record->type != PSTORE_TYPE_DMESG)
887 switch (record->type) {
888 case PSTORE_TYPE_DMESG:
889 return psz_kmsg_write(cxt, record);
890 case PSTORE_TYPE_CONSOLE:
891 return psz_record_write(cxt->cpsz, record);
892 case PSTORE_TYPE_PMSG:
893 return psz_record_write(cxt->ppsz, record);
894 case PSTORE_TYPE_FTRACE: {
895 int zonenum = smp_processor_id();
899 return psz_record_write(cxt->fpszs[zonenum], record);
906 static struct pstore_zone *psz_read_next_zone(struct psz_context *cxt)
908 struct pstore_zone *zone = NULL;
910 while (cxt->kmsg_read_cnt < cxt->kmsg_max_cnt) {
911 zone = cxt->kpszs[cxt->kmsg_read_cnt++];
916 if (cxt->ftrace_read_cnt < cxt->ftrace_max_cnt)
918 * No need psz_old_ok(). Let psz_ftrace_read() do so for
919 * combination. psz_ftrace_read() should traverse over
920 * all zones in case of some zone without data.
922 return cxt->fpszs[cxt->ftrace_read_cnt++];
924 if (cxt->pmsg_read_cnt == 0) {
925 cxt->pmsg_read_cnt++;
927 if (psz_old_ok(zone))
931 if (cxt->console_read_cnt == 0) {
932 cxt->console_read_cnt++;
934 if (psz_old_ok(zone))
941 static int psz_kmsg_read_hdr(struct pstore_zone *zone,
942 struct pstore_record *record)
944 struct psz_buffer *buffer = zone->buffer;
945 struct psz_kmsg_header *hdr =
946 (struct psz_kmsg_header *)buffer->data;
948 if (hdr->magic != PSTORE_KMSG_HEADER_MAGIC)
950 record->compressed = hdr->compressed;
951 record->time.tv_sec = hdr->time.tv_sec;
952 record->time.tv_nsec = hdr->time.tv_nsec;
953 record->reason = hdr->reason;
954 record->count = hdr->counter;
958 static ssize_t psz_kmsg_read(struct pstore_zone *zone,
959 struct pstore_record *record)
961 ssize_t size, hlen = 0;
963 size = buffer_datalen(zone);
964 /* Clear and skip this kmsg dump record if it has no valid header */
965 if (psz_kmsg_read_hdr(zone, record)) {
966 atomic_set(&zone->buffer->datalen, 0);
967 atomic_set(&zone->dirty, 0);
970 size -= sizeof(struct psz_kmsg_header);
972 if (!record->compressed) {
973 char *buf = kasprintf(GFP_KERNEL, "%s: Total %d times\n",
974 kmsg_dump_reason_str(record->reason),
977 record->buf = krealloc(buf, hlen + size, GFP_KERNEL);
983 record->buf = kmalloc(size, GFP_KERNEL);
988 size = psz_zone_read_buffer(zone, record->buf + hlen, size,
989 sizeof(struct psz_kmsg_header));
990 if (unlikely(size < 0)) {
998 /* try to combine all ftrace zones */
999 static ssize_t psz_ftrace_read(struct pstore_zone *zone,
1000 struct pstore_record *record)
1002 struct psz_context *cxt;
1003 struct psz_buffer *buf;
1006 if (!zone || !record)
1009 if (!psz_old_ok(zone))
1012 buf = (struct psz_buffer *)zone->oldbuf;
1016 ret = pstore_ftrace_combine_log(&record->buf, &record->size,
1017 (char *)buf->data, atomic_read(&buf->datalen));
1022 cxt = record->psi->data;
1023 if (cxt->ftrace_read_cnt < cxt->ftrace_max_cnt)
1024 /* then, read next ftrace zone */
1027 return record->size ? record->size : -ENOMSG;
1030 static ssize_t psz_record_read(struct pstore_zone *zone,
1031 struct pstore_record *record)
1034 struct psz_buffer *buf;
1036 if (!zone || !record)
1039 buf = (struct psz_buffer *)zone->oldbuf;
1043 len = atomic_read(&buf->datalen);
1044 record->buf = kmalloc(len, GFP_KERNEL);
1048 if (unlikely(psz_zone_read_oldbuf(zone, record->buf, len, 0))) {
1056 static ssize_t psz_pstore_read(struct pstore_record *record)
1058 struct psz_context *cxt = record->psi->data;
1059 ssize_t (*readop)(struct pstore_zone *zone,
1060 struct pstore_record *record);
1061 struct pstore_zone *zone;
1064 /* before read, we must recover from storage */
1065 ret = psz_recovery(cxt);
1070 zone = psz_read_next_zone(cxt);
1074 record->type = zone->type;
1075 switch (record->type) {
1076 case PSTORE_TYPE_DMESG:
1077 readop = psz_kmsg_read;
1078 record->id = cxt->kmsg_read_cnt - 1;
1080 case PSTORE_TYPE_FTRACE:
1081 readop = psz_ftrace_read;
1083 case PSTORE_TYPE_CONSOLE:
1084 case PSTORE_TYPE_PMSG:
1085 readop = psz_record_read;
1091 ret = readop(zone, record);
1097 static struct psz_context pstore_zone_cxt = {
1098 .pstore_zone_info_lock =
1099 __MUTEX_INITIALIZER(pstore_zone_cxt.pstore_zone_info_lock),
1100 .recovered = ATOMIC_INIT(0),
1101 .on_panic = ATOMIC_INIT(0),
1103 .owner = THIS_MODULE,
1104 .open = psz_pstore_open,
1105 .read = psz_pstore_read,
1106 .write = psz_pstore_write,
1107 .erase = psz_pstore_erase,
1111 static void psz_free_zone(struct pstore_zone **pszone)
1113 struct pstore_zone *zone = *pszone;
1118 kfree(zone->buffer);
1123 static void psz_free_zones(struct pstore_zone ***pszones, unsigned int *cnt)
1125 struct pstore_zone **zones = *pszones;
1132 psz_free_zone(&(zones[*cnt]));
1138 static void psz_free_all_zones(struct psz_context *cxt)
1141 psz_free_zones(&cxt->kpszs, &cxt->kmsg_max_cnt);
1143 psz_free_zone(&cxt->ppsz);
1145 psz_free_zone(&cxt->cpsz);
1147 psz_free_zones(&cxt->fpszs, &cxt->ftrace_max_cnt);
1150 static struct pstore_zone *psz_init_zone(enum pstore_type_id type,
1151 loff_t *off, size_t size)
1153 struct pstore_zone_info *info = pstore_zone_cxt.pstore_zone_info;
1154 struct pstore_zone *zone;
1155 const char *name = pstore_type_to_name(type);
1160 if (*off + size > info->total_size) {
1161 pr_err("no room for %s (0x%zx@0x%llx over 0x%lx)\n",
1162 name, size, *off, info->total_size);
1163 return ERR_PTR(-ENOMEM);
1166 zone = kzalloc(sizeof(struct pstore_zone), GFP_KERNEL);
1168 return ERR_PTR(-ENOMEM);
1170 zone->buffer = kmalloc(size, GFP_KERNEL);
1171 if (!zone->buffer) {
1173 return ERR_PTR(-ENOMEM);
1175 memset(zone->buffer, 0xFF, size);
1179 zone->buffer_size = size - sizeof(struct psz_buffer);
1180 zone->buffer->sig = type ^ PSZ_SIG;
1181 zone->oldbuf = NULL;
1182 atomic_set(&zone->dirty, 0);
1183 atomic_set(&zone->buffer->datalen, 0);
1184 atomic_set(&zone->buffer->start, 0);
1188 pr_debug("pszone %s: off 0x%llx, %zu header, %zu data\n", zone->name,
1189 zone->off, sizeof(*zone->buffer), zone->buffer_size);
1193 static struct pstore_zone **psz_init_zones(enum pstore_type_id type,
1194 loff_t *off, size_t total_size, ssize_t record_size,
1197 struct pstore_zone_info *info = pstore_zone_cxt.pstore_zone_info;
1198 struct pstore_zone **zones, *zone;
1199 const char *name = pstore_type_to_name(type);
1203 if (!total_size || !record_size)
1206 if (*off + total_size > info->total_size) {
1207 pr_err("no room for zones %s (0x%zx@0x%llx over 0x%lx)\n",
1208 name, total_size, *off, info->total_size);
1209 return ERR_PTR(-ENOMEM);
1212 c = total_size / record_size;
1213 zones = kcalloc(c, sizeof(*zones), GFP_KERNEL);
1215 pr_err("allocate for zones %s failed\n", name);
1216 return ERR_PTR(-ENOMEM);
1218 memset(zones, 0, c * sizeof(*zones));
1220 for (i = 0; i < c; i++) {
1221 zone = psz_init_zone(type, off, record_size);
1222 if (!zone || IS_ERR(zone)) {
1223 pr_err("initialize zones %s failed\n", name);
1224 psz_free_zones(&zones, &i);
1225 return (void *)zone;
1234 static int psz_alloc_zones(struct psz_context *cxt)
1236 struct pstore_zone_info *info = cxt->pstore_zone_info;
1239 size_t off_size = 0;
1241 off_size += info->pmsg_size;
1242 cxt->ppsz = psz_init_zone(PSTORE_TYPE_PMSG, &off, info->pmsg_size);
1243 if (IS_ERR(cxt->ppsz)) {
1244 err = PTR_ERR(cxt->ppsz);
1249 off_size += info->console_size;
1250 cxt->cpsz = psz_init_zone(PSTORE_TYPE_CONSOLE, &off,
1251 info->console_size);
1252 if (IS_ERR(cxt->cpsz)) {
1253 err = PTR_ERR(cxt->cpsz);
1258 off_size += info->ftrace_size;
1259 cxt->fpszs = psz_init_zones(PSTORE_TYPE_FTRACE, &off,
1261 info->ftrace_size / nr_cpu_ids,
1262 &cxt->ftrace_max_cnt);
1263 if (IS_ERR(cxt->fpszs)) {
1264 err = PTR_ERR(cxt->fpszs);
1269 cxt->kpszs = psz_init_zones(PSTORE_TYPE_DMESG, &off,
1270 info->total_size - off_size,
1271 info->kmsg_size, &cxt->kmsg_max_cnt);
1272 if (IS_ERR(cxt->kpszs)) {
1273 err = PTR_ERR(cxt->kpszs);
1280 psz_free_all_zones(cxt);
1285 * register_pstore_zone() - register to pstore/zone
1287 * @info: back-end driver information. See &struct pstore_zone_info.
1289 * Only one back-end at one time.
1291 * Return: 0 on success, others on failure.
1293 int register_pstore_zone(struct pstore_zone_info *info)
1296 struct psz_context *cxt = &pstore_zone_cxt;
1298 if (info->total_size < 4096) {
1299 pr_warn("total_size must be >= 4096\n");
1302 if (info->total_size > SZ_128M) {
1303 pr_warn("capping size to 128MiB\n");
1304 info->total_size = SZ_128M;
1307 if (!info->kmsg_size && !info->pmsg_size && !info->console_size &&
1308 !info->ftrace_size) {
1309 pr_warn("at least one record size must be non-zero\n");
1313 if (!info->name || !info->name[0])
1316 #define check_size(name, size) { \
1317 if (info->name > 0 && info->name < (size)) { \
1318 pr_err(#name " must be over %d\n", (size)); \
1321 if (info->name & (size - 1)) { \
1322 pr_err(#name " must be a multiple of %d\n", \
1328 check_size(total_size, 4096);
1329 check_size(kmsg_size, SECTOR_SIZE);
1330 check_size(pmsg_size, SECTOR_SIZE);
1331 check_size(console_size, SECTOR_SIZE);
1332 check_size(ftrace_size, SECTOR_SIZE);
1337 * the @read and @write must be applied.
1338 * if no @read, pstore may mount failed.
1339 * if no @write, pstore do not support to remove record file.
1341 if (!info->read || !info->write) {
1342 pr_err("no valid general read/write interface\n");
1346 mutex_lock(&cxt->pstore_zone_info_lock);
1347 if (cxt->pstore_zone_info) {
1348 pr_warn("'%s' already loaded: ignoring '%s'\n",
1349 cxt->pstore_zone_info->name, info->name);
1350 mutex_unlock(&cxt->pstore_zone_info_lock);
1353 cxt->pstore_zone_info = info;
1355 pr_debug("register %s with properties:\n", info->name);
1356 pr_debug("\ttotal size : %ld Bytes\n", info->total_size);
1357 pr_debug("\tkmsg size : %ld Bytes\n", info->kmsg_size);
1358 pr_debug("\tpmsg size : %ld Bytes\n", info->pmsg_size);
1359 pr_debug("\tconsole size : %ld Bytes\n", info->console_size);
1360 pr_debug("\tftrace size : %ld Bytes\n", info->ftrace_size);
1362 err = psz_alloc_zones(cxt);
1364 pr_err("alloc zones failed\n");
1368 if (info->kmsg_size) {
1369 cxt->pstore.bufsize = cxt->kpszs[0]->buffer_size -
1370 sizeof(struct psz_kmsg_header);
1371 cxt->pstore.buf = kzalloc(cxt->pstore.bufsize, GFP_KERNEL);
1372 if (!cxt->pstore.buf) {
1377 cxt->pstore.data = cxt;
1379 pr_info("registered %s as backend for", info->name);
1380 cxt->pstore.max_reason = info->max_reason;
1381 cxt->pstore.name = info->name;
1382 if (info->kmsg_size) {
1383 cxt->pstore.flags |= PSTORE_FLAGS_DMESG;
1385 kmsg_dump_reason_str(cxt->pstore.max_reason));
1386 if (cxt->pstore_zone_info->panic_write)
1387 pr_cont(",panic_write");
1390 if (info->pmsg_size) {
1391 cxt->pstore.flags |= PSTORE_FLAGS_PMSG;
1394 if (info->console_size) {
1395 cxt->pstore.flags |= PSTORE_FLAGS_CONSOLE;
1396 pr_cont(" console");
1398 if (info->ftrace_size) {
1399 cxt->pstore.flags |= PSTORE_FLAGS_FTRACE;
1404 err = pstore_register(&cxt->pstore);
1406 pr_err("registering with pstore failed\n");
1409 mutex_unlock(&pstore_zone_cxt.pstore_zone_info_lock);
1414 kfree(cxt->pstore.buf);
1415 cxt->pstore.buf = NULL;
1416 cxt->pstore.bufsize = 0;
1417 psz_free_all_zones(cxt);
1419 pstore_zone_cxt.pstore_zone_info = NULL;
1420 mutex_unlock(&pstore_zone_cxt.pstore_zone_info_lock);
1423 EXPORT_SYMBOL_GPL(register_pstore_zone);
1426 * unregister_pstore_zone() - unregister to pstore/zone
1428 * @info: back-end driver information. See struct pstore_zone_info.
1430 void unregister_pstore_zone(struct pstore_zone_info *info)
1432 struct psz_context *cxt = &pstore_zone_cxt;
1434 mutex_lock(&cxt->pstore_zone_info_lock);
1435 if (!cxt->pstore_zone_info) {
1436 mutex_unlock(&cxt->pstore_zone_info_lock);
1440 /* Stop incoming writes from pstore. */
1441 pstore_unregister(&cxt->pstore);
1443 /* Flush any pending writes. */
1444 psz_flush_all_dirty_zones(NULL);
1445 flush_delayed_work(&psz_cleaner);
1447 /* Clean up allocations. */
1448 kfree(cxt->pstore.buf);
1449 cxt->pstore.buf = NULL;
1450 cxt->pstore.bufsize = 0;
1451 cxt->pstore_zone_info = NULL;
1453 psz_free_all_zones(cxt);
1455 /* Clear counters and zone state. */
1456 cxt->oops_counter = 0;
1457 cxt->panic_counter = 0;
1458 atomic_set(&cxt->recovered, 0);
1459 atomic_set(&cxt->on_panic, 0);
1461 mutex_unlock(&cxt->pstore_zone_info_lock);
1463 EXPORT_SYMBOL_GPL(unregister_pstore_zone);
1465 MODULE_LICENSE("GPL");
1466 MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
1467 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
1468 MODULE_DESCRIPTION("Storage Manager for pstore/blk");