mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: dragos.tatulea@intel.com
To: ccross@android.com, keescook@chromium.org, tony.luck@intel.com,
	cbouatmailru@gmail.com, linux-kernel@vger.kernel.org
Cc: adrian.hunter@intel.com, octavian.purdila@intel.com
Subject: [PATCH v2 3/8] pstore: add support for external writers
Date: Thu, 18 Oct 2012 11:28:23 +0300	[thread overview]
Message-ID: <1350548908-24407-4-git-send-email-dragos.tatulea@intel.com> (raw)
In-Reply-To: <1350548908-24407-1-git-send-email-dragos.tatulea@intel.com>

From: Adrian Hunter <adrian.hunter@intel.com>

Other modules that may wish to write to persistent storage
are supported by adding a notifier and write function.

The notifier has 3 events: PSTORE_BEGIN, PSTORE_DUMP and
PSTORE_END.  External writers use the PSTORE_DUMP event
whereas the PSTORE_BEGIN and PSTORE_END can be used by
platform code to ensure the back end is powered up.

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
---
 fs/pstore/platform.c   |   88 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/pstore.h |   43 +++++++++++++++++++----
 2 files changed, 124 insertions(+), 7 deletions(-)

diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c
index 9d65723..63ff377 100644
--- a/fs/pstore/platform.c
+++ b/fs/pstore/platform.c
@@ -34,6 +34,7 @@
 #include <linux/jiffies.h>
 #include <linux/workqueue.h>
 #include <linux/debugfs.h>
+#include <linux/notifier.h>
 
 #include "internal.h"
 
@@ -74,6 +75,20 @@ void pstore_set_kmsg_bytes(int bytes)
 	kmsg_bytes = bytes;
 }
 
+static ATOMIC_NOTIFIER_HEAD(pstore_notifiers);
+
+int pstore_notifier_register(struct notifier_block *n)
+{
+	return atomic_notifier_chain_register(&pstore_notifiers, n);
+}
+EXPORT_SYMBOL_GPL(pstore_notifier_register);
+
+int pstore_notifier_unregister(struct notifier_block *n)
+{
+	return atomic_notifier_chain_unregister(&pstore_notifiers, n);
+}
+EXPORT_SYMBOL_GPL(pstore_notifier_unregister);
+
 /* Tag each group of saved records with a sequence number */
 static int	oopscount;
 
@@ -97,6 +112,26 @@ static const char *get_reason_str(enum kmsg_dump_reason reason)
 	}
 }
 
+static int pstore_ext_flush(void)
+{
+	int ret;
+
+	if (!psinfo->ext_len)
+		return 0;
+
+	ret = psinfo->write(psinfo->ext_type, psinfo->ext_reason,
+			    &psinfo->ext_id, psinfo->ext_part++,
+			    psinfo->ext_len, psinfo);
+
+	if (ret == 0 && psinfo->ext_reason == KMSG_DUMP_OOPS &&
+	    pstore_is_mounted())
+		pstore_new_entry = 1;
+
+	psinfo->ext_len = 0;
+
+	return ret;
+}
+
 /*
  * callback from kmsg_dump. (s2,l2) has the most recently
  * written bytes, older bytes are in (s1,l1). Save as much
@@ -122,6 +157,15 @@ static void pstore_dump(struct kmsg_dumper *dumper,
 	} else
 		spin_lock_irqsave(&psinfo->buf_lock, flags);
 	oopscount++;
+
+	psinfo->ext_id = 0;
+	psinfo->ext_len = 0;
+	psinfo->ext_part = 0;
+	psinfo->ext_type = PSTORE_TYPE_UNKNOWN;
+	psinfo->ext_reason = reason;
+
+	atomic_notifier_call_chain(&pstore_notifiers, PSTORE_BEGIN, psinfo);
+
 	while (total < kmsg_bytes) {
 		char *dst;
 		unsigned long size;
@@ -148,6 +192,13 @@ static void pstore_dump(struct kmsg_dumper *dumper,
 		total += hsize + len;
 		part++;
 	}
+
+	atomic_notifier_call_chain(&pstore_notifiers, PSTORE_DUMP, psinfo);
+
+	pstore_ext_flush();
+
+	atomic_notifier_call_chain(&pstore_notifiers, PSTORE_END, psinfo);
+
 	if (in_nmi()) {
 		if (is_locked)
 			spin_unlock(&psinfo->buf_lock);
@@ -368,5 +419,42 @@ static void pstore_timefunc(unsigned long dummy)
 	mod_timer(&pstore_timer, jiffies + msecs_to_jiffies(pstore_update_ms));
 }
 
+/* pstore_write must only be called from PSTORE_DUMP notifier callbacks */
+int pstore_write(enum pstore_type_id type, const char *buf, size_t size)
+{
+	size_t len;
+	int err = 0, err2;
+
+	if (!psinfo)
+		return -ENODEV;
+
+	/*
+	 * No locking is needed because pstore_write is called only from
+	 * PSTORE_DUMP notifier callbacks.
+	 */
+
+	if (type != psinfo->ext_type) {
+		err = pstore_ext_flush();
+		psinfo->ext_type = type;
+		psinfo->ext_part = 1;
+	}
+
+	while (size) {
+		len = min(size, psinfo->bufsize - psinfo->ext_len);
+		memcpy(psinfo->buf + psinfo->ext_len, buf, len);
+		psinfo->ext_len += len;
+		buf += len;
+		size -= len;
+		if (psinfo->ext_len == psinfo->bufsize) {
+			err2 = pstore_ext_flush();
+			if (err2 && !err)
+				err = err2;
+		}
+	}
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(pstore_write);
+
 module_param(backend, charp, 0444);
 MODULE_PARM_DESC(backend, "Pstore backend to use");
diff --git a/include/linux/pstore.h b/include/linux/pstore.h
index 55ab23f..1bf7f4b 100644
--- a/include/linux/pstore.h
+++ b/include/linux/pstore.h
@@ -40,17 +40,27 @@ enum pstore_type_id {
 
 struct module;
 
+/* Notifier events */
+#define PSTORE_BEGIN		1
+#define PSTORE_DUMP		2
+#define PSTORE_END		3
+
 #define PSTORE_NO_HEADINGS	BIT(0)
 #define PSTORE_MAX_KMSG_BYTES	BIT(1)
 
 struct pstore_info {
-	struct module	*owner;
-	char		*name;
-	unsigned int	flags;
-	spinlock_t	buf_lock;	/* serialize access to 'buf' */
-	char		*buf;
-	size_t		bufsize;
-	struct mutex	read_mutex;	/* serialize open/read/close */
+	struct module		*owner;
+	char			*name;
+	unsigned int		flags;
+	spinlock_t		buf_lock;	/* serialize access to 'buf' */
+	char			*buf;
+	size_t			bufsize;
+	struct mutex		read_mutex;	/* serialize open/read/close */
+	u64			ext_id;
+	size_t			ext_len;
+	unsigned int		ext_part;
+	enum pstore_type_id	ext_type;
+	enum kmsg_dump_reason	ext_reason;
 	int		(*open)(struct pstore_info *psi);
 	int		(*close)(struct pstore_info *psi);
 	ssize_t		(*read)(u64 *id, enum pstore_type_id *type,
@@ -70,12 +80,31 @@ struct pstore_info {
 
 #ifdef CONFIG_PSTORE
 extern int pstore_register(struct pstore_info *);
+extern int pstore_notifier_register(struct notifier_block *n);
+extern int pstore_notifier_unregister(struct notifier_block *n);
+/* pstore_write must only be called from PSTORE_DUMP notifier callbacks */
+extern int pstore_write(enum pstore_type_id type, const char *buf, size_t size);
 #else
 static inline int
 pstore_register(struct pstore_info *psi)
 {
 	return -ENODEV;
 }
+static inline int
+pstore_notifier_register(struct notifier_block *n)
+{
+	return 0;
+}
+static inline int
+pstore_notifier_unregister(struct notifier_block *n)
+{
+	return 0;
+}
+static inline int
+pstore_write(enum pstore_type_id type, const char *buf, size_t size)
+{
+	return 0;
+}
 #endif
 
 #endif /*_LINUX_PSTORE_H*/
-- 
1.7.9.5


  parent reply	other threads:[~2012-10-18  8:28 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-18  8:28 [PATCH v2 0/8] pstore: add interface for dumpers and add task list dumper dragos.tatulea
2012-10-18  8:28 ` [PATCH v2 1/8] pstore: add flags dragos.tatulea
2012-10-18  8:28 ` [PATCH v2 2/8] pstore: add debugfs support for causing dumps and panics dragos.tatulea
2012-10-18  8:28 ` dragos.tatulea [this message]
2012-10-18  8:28 ` [PATCH v2 4/8] pstore: allow storing different type id's in ram backend dragos.tatulea
2012-10-18  8:28 ` [PATCH v2 5/8] pstore: add task list dumper dragos.tatulea
2012-10-18  8:28 ` [PATCH v2 6/8] pstore: do not run timer while pstore is not mounted dragos.tatulea
2012-10-18  8:28 ` [PATCH v2 7/8] pstore: make sure pstore_write exists on flush error dragos.tatulea
2012-10-18  8:28 ` [PATCH v2 8/8] pstore: max out console log level during sysfs dump switch dragos.tatulea

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1350548908-24407-4-git-send-email-dragos.tatulea@intel.com \
    --to=dragos.tatulea@intel.com \
    --cc=adrian.hunter@intel.com \
    --cc=cbouatmailru@gmail.com \
    --cc=ccross@android.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=octavian.purdila@intel.com \
    --cc=tony.luck@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®