]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - fs/pstore/platform.c
pstore: Convert buf_lock to semaphore
[mirror_ubuntu-bionic-kernel.git] / fs / pstore / platform.c
index 691032107f8c78776668a93ca4c546dc2a46282a..ca307c62420561b576e5142212f10f7538596c34 100644 (file)
@@ -129,26 +129,27 @@ static const char *get_reason_str(enum kmsg_dump_reason reason)
        }
 }
 
-bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
+/*
+ * Should pstore_dump() wait for a concurrent pstore_dump()? If
+ * not, the current pstore_dump() will report a failure to dump
+ * and return.
+ */
+static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
 {
-       /*
-        * In case of NMI path, pstore shouldn't be blocked
-        * regardless of reason.
-        */
+       /* In NMI path, pstore shouldn't block regardless of reason. */
        if (in_nmi())
                return true;
 
        switch (reason) {
        /* In panic case, other cpus are stopped by smp_send_stop(). */
        case KMSG_DUMP_PANIC:
-       /* Emergency restart shouldn't be blocked by spin lock. */
+       /* Emergency restart shouldn't be blocked. */
        case KMSG_DUMP_EMERG:
                return true;
        default:
                return false;
        }
 }
-EXPORT_SYMBOL_GPL(pstore_cannot_block_path);
 
 #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
 /* Derived from logfs_compress() */
@@ -496,23 +497,23 @@ static void pstore_dump(struct kmsg_dumper *dumper,
        unsigned long   total = 0;
        const char      *why;
        unsigned int    part = 1;
-       unsigned long   flags = 0;
-       int             is_locked;
        int             ret;
 
        why = get_reason_str(reason);
 
-       if (pstore_cannot_block_path(reason)) {
-               is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags);
-               if (!is_locked) {
-                       pr_err("pstore dump routine blocked in %s path, may corrupt error record\n"
-                                      , in_nmi() ? "NMI" : why);
+       if (down_trylock(&psinfo->buf_lock)) {
+               /* Failed to acquire lock: give up if we cannot wait. */
+               if (pstore_cannot_wait(reason)) {
+                       pr_err("dump skipped in %s path: may corrupt error record\n",
+                               in_nmi() ? "NMI" : why);
+                       return;
+               }
+               if (down_interruptible(&psinfo->buf_lock)) {
+                       pr_err("could not grab semaphore?!\n");
                        return;
                }
-       } else {
-               spin_lock_irqsave(&psinfo->buf_lock, flags);
-               is_locked = 1;
        }
+
        oopscount++;
        while (total < kmsg_bytes) {
                char *dst;
@@ -529,7 +530,7 @@ static void pstore_dump(struct kmsg_dumper *dumper,
                record.part = part;
                record.buf = psinfo->buf;
 
-               if (big_oops_buf && is_locked) {
+               if (big_oops_buf) {
                        dst = big_oops_buf;
                        dst_size = big_oops_buf_sz;
                } else {
@@ -547,7 +548,7 @@ static void pstore_dump(struct kmsg_dumper *dumper,
                                          dst_size, &dump_size))
                        break;
 
-               if (big_oops_buf && is_locked) {
+               if (big_oops_buf) {
                        zipped_len = pstore_compress(dst, psinfo->buf,
                                                header_size + dump_size,
                                                psinfo->bufsize);
@@ -570,8 +571,8 @@ static void pstore_dump(struct kmsg_dumper *dumper,
                total += record.size;
                part++;
        }
-       if (is_locked)
-               spin_unlock_irqrestore(&psinfo->buf_lock, flags);
+
+       up(&psinfo->buf_lock);
 }
 
 static struct kmsg_dumper pstore_dumper = {
@@ -594,31 +595,14 @@ static void pstore_unregister_kmsg(void)
 #ifdef CONFIG_PSTORE_CONSOLE
 static void pstore_console_write(struct console *con, const char *s, unsigned c)
 {
-       const char *e = s + c;
-
-       while (s < e) {
-               struct pstore_record record;
-               unsigned long flags;
-
-               pstore_record_init(&record, psinfo);
-               record.type = PSTORE_TYPE_CONSOLE;
+       struct pstore_record record;
 
-               if (c > psinfo->bufsize)
-                       c = psinfo->bufsize;
+       pstore_record_init(&record, psinfo);
+       record.type = PSTORE_TYPE_CONSOLE;
 
-               if (oops_in_progress) {
-                       if (!spin_trylock_irqsave(&psinfo->buf_lock, flags))
-                               break;
-               } else {
-                       spin_lock_irqsave(&psinfo->buf_lock, flags);
-               }
-               record.buf = (char *)s;
-               record.size = c;
-               psinfo->write(&record);
-               spin_unlock_irqrestore(&psinfo->buf_lock, flags);
-               s += c;
-               c = e - s;
-       }
+       record.buf = (char *)s;
+       record.size = c;
+       psinfo->write(&record);
 }
 
 static struct console pstore_console = {
@@ -707,6 +691,7 @@ int pstore_register(struct pstore_info *psi)
                psi->write_user = pstore_write_user_compat;
        psinfo = psi;
        mutex_init(&psinfo->read_mutex);
+       sema_init(&psinfo->buf_lock, 1);
        spin_unlock(&pstore_lock);
 
        if (owner && !try_module_get(owner)) {