^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Persistent Storage - platform driver interface parts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2007-2008 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define pr_fmt(fmt) "pstore: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kmsg_dump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/console.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/pstore.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/lzo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/lz4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/zstd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * We defer making "oops" entries appear in pstore - see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * whether the system is actually still running well enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * to let someone see the entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static int pstore_update_ms = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) module_param_named(update_ms, pstore_update_ms, int, 0600);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) "(default is -1, which means runtime updates are disabled; "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) "enabling this option may not be safe; it may lead to further "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) "corruption on Oopses)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Names should be in the same order as the enum pstore_type_id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static const char * const pstore_type_names[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) "dmesg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "mce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) "console",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) "ftrace",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) "rtas",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) "powerpc-ofw",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) "powerpc-common",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) "pmsg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) "powerpc-opal",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #ifdef CONFIG_PSTORE_BOOT_LOG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) "boot-log",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static int pstore_new_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static void pstore_timefunc(struct timer_list *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static DEFINE_TIMER(pstore_timer, pstore_timefunc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static void pstore_dowork(struct work_struct *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static DECLARE_WORK(pstore_work, pstore_dowork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * psinfo_lock protects "psinfo" during calls to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * pstore_register(), pstore_unregister(), and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * the filesystem mount/unmount routines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static DEFINE_MUTEX(psinfo_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct pstore_info *psinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static char *backend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) module_param(backend, charp, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) MODULE_PARM_DESC(backend, "specific backend to use");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static char *compress =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) CONFIG_PSTORE_COMPRESS_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) module_param(compress, charp, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) MODULE_PARM_DESC(compress, "compression to use");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* Compression parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static struct crypto_comp *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct pstore_zbackend {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int (*zbufsize)(size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static char *big_oops_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static size_t big_oops_buf_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* How much of the console log to snapshot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) void pstore_set_kmsg_bytes(int bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) kmsg_bytes = bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* Tag each group of saved records with a sequence number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int oopscount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) const char *pstore_type_to_name(enum pstore_type_id type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) BUILD_BUG_ON(ARRAY_SIZE(pstore_type_names) != PSTORE_TYPE_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (WARN_ON_ONCE(type >= PSTORE_TYPE_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return pstore_type_names[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) EXPORT_SYMBOL_GPL(pstore_type_to_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) enum pstore_type_id pstore_name_to_type(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) for (i = 0; i < PSTORE_TYPE_MAX; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!strcmp(pstore_type_names[i], name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return PSTORE_TYPE_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) EXPORT_SYMBOL_GPL(pstore_name_to_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static void pstore_timer_kick(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (pstore_update_ms < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * Should pstore_dump() wait for a concurrent pstore_dump()? If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * not, the current pstore_dump() will report a failure to dump
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* In NMI path, pstore shouldn't block regardless of reason. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (in_nmi())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) switch (reason) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* In panic case, other cpus are stopped by smp_send_stop(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) case KMSG_DUMP_PANIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* Emergency restart shouldn't be blocked. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) case KMSG_DUMP_EMERG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static int zbufsize_deflate(size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) size_t cmpr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) switch (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* buffer range for efivars */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) case 1000 ... 2000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) cmpr = 56;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) case 2001 ... 3000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) cmpr = 54;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) case 3001 ... 3999:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) cmpr = 52;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /* buffer range for nvram, erst */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) case 4000 ... 10000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) cmpr = 45;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) cmpr = 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return (size * 100) / cmpr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static int zbufsize_lzo(size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return lzo1x_worst_compress(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static int zbufsize_lz4(size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return LZ4_compressBound(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static int zbufsize_842(size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static int zbufsize_zstd(size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return ZSTD_compressBound(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static const struct pstore_zbackend *zbackend __ro_after_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static const struct pstore_zbackend zbackends[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) .zbufsize = zbufsize_deflate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) .name = "deflate",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) .zbufsize = zbufsize_lzo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) .name = "lzo",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) .zbufsize = zbufsize_lz4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) .name = "lz4",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) .zbufsize = zbufsize_lz4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) .name = "lz4hc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) .zbufsize = zbufsize_842,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) .name = "842",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) .zbufsize = zbufsize_zstd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) .name = "zstd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static int pstore_compress(const void *in, void *out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) unsigned int inlen, unsigned int outlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) ret = crypto_comp_compress(tfm, in, inlen, out, &outlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) pr_err("crypto_comp_compress failed, ret = %d!\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return outlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static void allocate_buf_for_compression(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct crypto_comp *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /* Skip if not built-in or compression backend not selected yet. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /* Skip if no pstore backend yet or compression init already done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (!psinfo || tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!crypto_has_comp(zbackend->name, 0, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) pr_err("Unknown compression: %s\n", zbackend->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) size = zbackend->zbufsize(psinfo->bufsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (size <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) pr_err("Invalid compression size for %s: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) zbackend->name, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) buf = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) pr_err("Failed %d byte compression buffer allocation for: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) size, zbackend->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) ctx = crypto_alloc_comp(zbackend->name, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (IS_ERR_OR_NULL(ctx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) PTR_ERR(ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /* A non-NULL big_oops_buf indicates compression is available. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) tfm = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) big_oops_buf_sz = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) big_oops_buf = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) pr_info("Using crash dump compression: %s\n", zbackend->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static void free_buf_for_compression(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) crypto_free_comp(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) tfm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) kfree(big_oops_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) big_oops_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) big_oops_buf_sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * Called when compression fails, since the printk buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * would be fetched for compression calling it again when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * compression fails would have moved the iterator of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * printk buffer which results in fetching old contents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * Copy the recent messages from big_oops_buf to psinfo->buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static size_t copy_kmsg_to_buffer(int hsize, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) size_t total_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) size_t diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) total_len = hsize + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (total_len > psinfo->bufsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) diff = total_len - psinfo->bufsize + hsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) memcpy(psinfo->buf, big_oops_buf, hsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) memcpy(psinfo->buf + hsize, big_oops_buf + diff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) psinfo->bufsize - hsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) total_len = psinfo->bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) memcpy(psinfo->buf, big_oops_buf, total_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return total_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) void pstore_record_init(struct pstore_record *record,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct pstore_info *psinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) memset(record, 0, sizeof(*record));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) record->psi = psinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* Report zeroed timestamp if called before timekeeping has resumed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) record->time = ns_to_timespec64(ktime_get_real_fast_ns());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * callback from kmsg_dump. Save as much as we can (up to kmsg_bytes) from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * end of the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static void pstore_dump(struct kmsg_dumper *dumper,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) enum kmsg_dump_reason reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) unsigned long total = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) const char *why;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) unsigned int part = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) why = kmsg_dump_reason_str(reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (down_trylock(&psinfo->buf_lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) /* Failed to acquire lock: give up if we cannot wait. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (pstore_cannot_wait(reason)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) pr_err("dump skipped in %s path: may corrupt error record\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) in_nmi() ? "NMI" : why);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (down_interruptible(&psinfo->buf_lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) pr_err("could not grab semaphore?!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) oopscount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) while (total < kmsg_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) char *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) size_t dst_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) int header_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) int zipped_len = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) size_t dump_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) struct pstore_record record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) pstore_record_init(&record, psinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) record.type = PSTORE_TYPE_DMESG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) record.count = oopscount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) record.reason = reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) record.part = part;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) record.buf = psinfo->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) if (big_oops_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) dst = big_oops_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) dst_size = big_oops_buf_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) dst = psinfo->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) dst_size = psinfo->bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) /* Write dump header. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) oopscount, part);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) dst_size -= header_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) /* Write dump contents. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) dst_size, &dump_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (big_oops_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) zipped_len = pstore_compress(dst, psinfo->buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) header_size + dump_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) psinfo->bufsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (zipped_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) record.compressed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) record.size = zipped_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) record.size = copy_kmsg_to_buffer(header_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) dump_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) record.size = header_size + dump_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) ret = psinfo->write(&record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (ret == 0 && reason == KMSG_DUMP_OOPS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) pstore_new_entry = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) pstore_timer_kick();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) total += record.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) part++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) up(&psinfo->buf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static struct kmsg_dumper pstore_dumper = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) .dump = pstore_dump,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * Register with kmsg_dump to save last part of console log on panic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) static void pstore_register_kmsg(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) kmsg_dump_register(&pstore_dumper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) static void pstore_unregister_kmsg(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) kmsg_dump_unregister(&pstore_dumper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) #ifdef CONFIG_PSTORE_CONSOLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static void pstore_console_write(struct console *con, const char *s, unsigned c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) struct pstore_record record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) pstore_record_init(&record, psinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) record.type = PSTORE_TYPE_CONSOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) record.buf = (char *)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) record.size = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) psinfo->write(&record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) static struct console pstore_console = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) .write = pstore_console_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) .index = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) static void pstore_register_console(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /* Show which backend is going to get console writes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) strscpy(pstore_console.name, psinfo->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) sizeof(pstore_console.name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) * Always initialize flags here since prior unregister_console()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) * calls may have changed settings (specifically CON_ENABLED).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) pstore_console.flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) register_console(&pstore_console);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) static void pstore_unregister_console(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) unregister_console(&pstore_console);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) static void pstore_register_console(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static void pstore_unregister_console(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) static int pstore_write_user_compat(struct pstore_record *record,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) const char __user *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (record->buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) record->buf = memdup_user(buf, record->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (IS_ERR(record->buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) ret = PTR_ERR(record->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) ret = record->psi->write(record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) kfree(record->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) record->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return unlikely(ret < 0) ? ret : record->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * platform specific persistent storage driver registers with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * us here. If pstore is already mounted, call the platform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * read function right away to populate the file system. If not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * then the pstore mount code will call us later to fill out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * the file system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) int pstore_register(struct pstore_info *psi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (backend && strcmp(backend, psi->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) pr_warn("ignoring unexpected backend '%s'\n", psi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) /* Sanity check flags. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (!psi->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) pr_warn("backend '%s' must support at least one frontend\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) psi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) /* Check for required functions. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (!psi->read || !psi->write) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) pr_warn("backend '%s' must implement read() and write()\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) psi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) mutex_lock(&psinfo_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (psinfo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) pr_warn("backend '%s' already loaded: ignoring '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) psinfo->name, psi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) mutex_unlock(&psinfo_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) if (!psi->write_user)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) psi->write_user = pstore_write_user_compat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) psinfo = psi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) mutex_init(&psinfo->read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) sema_init(&psinfo->buf_lock, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (psi->flags & PSTORE_FLAGS_DMESG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) allocate_buf_for_compression();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) pstore_get_records(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (psi->flags & PSTORE_FLAGS_DMESG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) pstore_dumper.max_reason = psinfo->max_reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) pstore_register_kmsg();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (psi->flags & PSTORE_FLAGS_CONSOLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) pstore_register_console();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (psi->flags & PSTORE_FLAGS_FTRACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) pstore_register_ftrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (psi->flags & PSTORE_FLAGS_PMSG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) pstore_register_pmsg();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) /* Start watching for new records, if desired. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) pstore_timer_kick();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) * Update the module parameter backend, so it is visible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) * through /sys/module/pstore/parameters/backend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) backend = kstrdup(psi->name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) pr_info("Registered %s as persistent store backend\n", psi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) mutex_unlock(&psinfo_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) EXPORT_SYMBOL_GPL(pstore_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) void pstore_unregister(struct pstore_info *psi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) /* It's okay to unregister nothing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) if (!psi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) mutex_lock(&psinfo_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) /* Only one backend can be registered at a time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (WARN_ON(psi != psinfo)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) mutex_unlock(&psinfo_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) /* Unregister all callbacks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) if (psi->flags & PSTORE_FLAGS_PMSG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) pstore_unregister_pmsg();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) if (psi->flags & PSTORE_FLAGS_FTRACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) pstore_unregister_ftrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (psi->flags & PSTORE_FLAGS_CONSOLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) pstore_unregister_console();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) if (psi->flags & PSTORE_FLAGS_DMESG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) pstore_unregister_kmsg();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) /* Stop timer and make sure all work has finished. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) del_timer_sync(&pstore_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) flush_work(&pstore_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) /* Remove all backend records from filesystem tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) pstore_put_backend_records(psi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) free_buf_for_compression();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) psinfo = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) kfree(backend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) backend = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) mutex_unlock(&psinfo_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) EXPORT_SYMBOL_GPL(pstore_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) static void decompress_record(struct pstore_record *record)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) int unzipped_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) char *unzipped, *workspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !record->compressed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) /* Only PSTORE_TYPE_DMESG support compression. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if (record->type != PSTORE_TYPE_DMESG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) pr_warn("ignored compressed record type %d\n", record->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) /* Missing compression buffer means compression was not initialized. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (!big_oops_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) pr_warn("no decompression method initialized!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) /* Allocate enough space to hold max decompression and ECC. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) unzipped_len = big_oops_buf_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) workspace = kmalloc(unzipped_len + record->ecc_notice_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (!workspace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) /* After decompression "unzipped_len" is almost certainly smaller. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) ret = crypto_comp_decompress(tfm, record->buf, record->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) workspace, &unzipped_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) pr_err("crypto_comp_decompress failed, ret = %d!\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) kfree(workspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) /* Append ECC notice to decompressed buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) memcpy(workspace + unzipped_len, record->buf + record->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) record->ecc_notice_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) /* Copy decompressed contents into an minimum-sized allocation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) unzipped = kmemdup(workspace, unzipped_len + record->ecc_notice_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) kfree(workspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) if (!unzipped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) /* Swap out compressed contents with decompressed contents. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) kfree(record->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) record->buf = unzipped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) record->size = unzipped_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) record->compressed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * Read all the records from one persistent store backend. Create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * files in our filesystem. Don't warn about -EEXIST errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * when we are re-scanning the backing store looking to add new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * error records.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) void pstore_get_backend_records(struct pstore_info *psi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) struct dentry *root, int quiet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) int failed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) unsigned int stop_loop = 65536;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (!psi || !root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) mutex_lock(&psi->read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) if (psi->open && psi->open(psi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) * Backend callback read() allocates record.buf. decompress_record()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) * may reallocate record.buf. On success, pstore_mkfile() will keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) * the record.buf, so free it only on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) for (; stop_loop; stop_loop--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) struct pstore_record *record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) record = kzalloc(sizeof(*record), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (!record) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) pr_err("out of memory creating record\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) pstore_record_init(record, psi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) record->size = psi->read(record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) /* No more records left in backend? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) if (record->size <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) kfree(record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) decompress_record(record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) rc = pstore_mkfile(root, record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) /* pstore_mkfile() did not take record, so free it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) kfree(record->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) kfree(record);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) if (rc != -EEXIST || !quiet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) failed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) if (psi->close)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) psi->close(psi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) mutex_unlock(&psi->read_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if (failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) pr_warn("failed to create %d record(s) from '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) failed, psi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) if (!stop_loop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) pr_err("looping? Too many records seen from '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) psi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) static void pstore_dowork(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) pstore_get_records(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) static void pstore_timefunc(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) if (pstore_new_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) pstore_new_entry = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) schedule_work(&pstore_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) pstore_timer_kick();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) static void __init pstore_choose_compression(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) const struct pstore_zbackend *step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) if (!compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) for (step = zbackends; step->name; step++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (!strcmp(compress, step->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) zbackend = step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) static int __init pstore_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) pstore_choose_compression();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) * Check if any pstore backends registered earlier but did not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) * initialize compression because crypto was not ready. If so,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) * initialize compression now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) allocate_buf_for_compression();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) ret = pstore_init_fs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) free_buf_for_compression();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) late_initcall(pstore_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) static void __exit pstore_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) pstore_exit_fs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) module_exit(pstore_exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) MODULE_LICENSE("GPL");