mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] lib/string: add memrchr function
@ 2019-02-14  3:48 Xiang Xiao
  2019-02-14  3:48 ` [PATCH 2/2] rpmsg: add syslog redirection driver Xiang Xiao
  2019-02-14  4:08 ` [PATCH 1/2] lib/string: add memrchr function Randy Dunlap
  0 siblings, 2 replies; 4+ messages in thread
From: Xiang Xiao @ 2019-02-14  3:48 UTC (permalink / raw)
  To: gregkh, alexander.shishkin, andriy.shevchenko, ohad,
	bjorn.andersson, wendy.liang, arnaud.pouliquen, kumar.gala,
	linux-remoteproc, linux-kernel
  Cc: Xiang Xiao

Here is the detailed description for memrchr:

void *memrchr(const void *s, int c, size_t n);

The memrchr() function is like the memchr() function, except
that it searches backward from the end of the n bytes pointed
to by s instead of forward from the beginning.

The memrchr() functions return a pointer to the matching byte
or NULL if the character does not occur in the given memory
area.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
---
 include/linux/string.h |  1 +
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 7927b87..f380f4b 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -167,6 +167,7 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
 	memcpy(dst, src, cnt);
 }
 #endif
+void *memrchr(const void *s, int c, size_t n);
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
 
diff --git a/lib/string.c b/lib/string.c
index 38e4ca0..92914f6 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -964,6 +964,27 @@ void *memchr(const void *s, int c, size_t n)
 EXPORT_SYMBOL(memchr);
 #endif
 
+/**
+ * memrchr - Find a character in an area of memory.
+ * @s: The memory area
+ * @c: The byte to search for
+ * @n: The size of the area.
+ *
+ * returns the address of the last occurrence of @c, or %NULL
+ * if @c is not found
+ */
+void *memrchr(const void *s, int c, size_t n)
+{
+	const unsigned char *p = s + n;
+
+	while (n-- != 0) {
+		if ((unsigned char)c == *--p)
+			return (void *)p;
+	}
+	return NULL;
+}
+EXPORT_SYMBOL(memrchr);
+
 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
 {
 	while (bytes) {
-- 
2.7.4


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] rpmsg: add syslog redirection driver
  2019-02-14  3:48 [PATCH 1/2] lib/string: add memrchr function Xiang Xiao
@ 2019-02-14  3:48 ` Xiang Xiao
  2019-02-14  4:08   ` Randy Dunlap
  2019-02-14  4:08 ` [PATCH 1/2] lib/string: add memrchr function Randy Dunlap
  1 sibling, 1 reply; 4+ messages in thread
From: Xiang Xiao @ 2019-02-14  3:48 UTC (permalink / raw)
  To: gregkh, alexander.shishkin, andriy.shevchenko, ohad,
	bjorn.andersson, wendy.liang, arnaud.pouliquen, kumar.gala,
	linux-remoteproc, linux-kernel
  Cc: Guiding Li

From: Guiding Li <liguiding@pinecone.net>

This driver allows the remote processor redirect the output of
syslog/printf into the kernel log, which is very useful to see
what happen in the remote side.

Signed-off-by: Guiding Li <liguiding@pinecone.net>
---
 drivers/rpmsg/Kconfig        |  12 ++++
 drivers/rpmsg/Makefile       |   1 +
 drivers/rpmsg/rpmsg_syslog.c | 163 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 176 insertions(+)
 create mode 100644 drivers/rpmsg/rpmsg_syslog.c

diff --git a/drivers/rpmsg/Kconfig b/drivers/rpmsg/Kconfig
index d0322b4..d701614 100644
--- a/drivers/rpmsg/Kconfig
+++ b/drivers/rpmsg/Kconfig
@@ -15,6 +15,18 @@ config RPMSG_CHAR
 	  in /dev. They make it possible for user-space programs to send and
 	  receive rpmsg packets.
 
+config RPMSG_SYSLOG
+	tristate "RPMSG syslog redirection"
+	depends on RPMSG
+	help
+	  Say Y here to redirect the syslog/printf from remote processor into
+	  the kernel log which is very useful to see what happen in the remote
+	  side.
+
+	  If the remote processor hang during bootup or panic at the runtime,
+	  We can even cat /sys/kernel/debug/remoteproc/remoteprocX/trace0 to
+	  get the last log which doesn't output yet.
+
 config RPMSG_QCOM_GLINK_NATIVE
 	tristate
 	select RPMSG
diff --git a/drivers/rpmsg/Makefile b/drivers/rpmsg/Makefile
index 9aa8595..bfd22df 100644
--- a/drivers/rpmsg/Makefile
+++ b/drivers/rpmsg/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_RPMSG)		+= rpmsg_core.o
 obj-$(CONFIG_RPMSG_CHAR)	+= rpmsg_char.o
+obj-$(CONFIG_RPMSG_SYSLOG)	+= rpmsg_syslog.o
 obj-$(CONFIG_RPMSG_QCOM_GLINK_RPM) += qcom_glink_rpm.o
 obj-$(CONFIG_RPMSG_QCOM_GLINK_NATIVE) += qcom_glink_native.o
 obj-$(CONFIG_RPMSG_QCOM_GLINK_SMEM) += qcom_glink_smem.o
diff --git a/drivers/rpmsg/rpmsg_syslog.c b/drivers/rpmsg/rpmsg_syslog.c
new file mode 100644
index 0000000..b7a0d27
--- /dev/null
+++ b/drivers/rpmsg/rpmsg_syslog.c
@@ -0,0 +1,163 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2017 Pinecone Inc.
+ *
+ * redirect syslog/printf from remote to the kernel.
+ */
+
+#include <linux/module.h>
+#include <linux/rpmsg.h>
+#include <linux/slab.h>
+
+#define RPMSG_SYSLOG_TRANSFER		0
+#define RPMSG_SYSLOG_TRANSFER_DONE	1
+#define RPMSG_SYSLOG_SUSPEND		2
+#define RPMSG_SYSLOG_RESUME		3
+
+struct rpmsg_syslog_header {
+	u32				command;
+	s32				result;
+} __packed;
+
+struct rpmsg_syslog_transfer {
+	struct rpmsg_syslog_header	header;
+	u32				count;
+	char				data[0];
+} __packed;
+
+#define rpmsg_syslog_suspend		rpmsg_syslog_header
+#define rpmsg_syslog_resume		rpmsg_syslog_header
+#define rpmsg_syslog_transfer_done	rpmsg_syslog_header
+
+struct rpmsg_syslog {
+	char				*buf;
+	unsigned int			next;
+	unsigned int			size;
+};
+
+static int rpmsg_syslog_callback(struct rpmsg_device *rpdev,
+				 void *data, int len, void *priv_, u32 src)
+{
+	struct rpmsg_syslog *priv = dev_get_drvdata(&rpdev->dev);
+	struct rpmsg_syslog_transfer *msg = data;
+	struct rpmsg_syslog_transfer_done done;
+	unsigned int copied = msg->count;
+	unsigned int printed = 0;
+	const char *nl;
+
+	if (msg->header.command != RPMSG_SYSLOG_TRANSFER)
+		return -EINVAL;
+
+	/* output the message before '\n' to the kernel log */
+	nl = memrchr(msg->data, '\n', msg->count);
+	if (nl) {
+		printed = nl + 1 - msg->data;
+		copied = msg->count - printed;
+
+		if (priv->next) {
+			pr_info("%.*s%.*s", priv->next,
+				priv->buf, printed, msg->data);
+			priv->next = 0;
+		} else {
+			pr_info("%.*s", printed, msg->data);
+		}
+	}
+
+	/* append the message after '\n' to the buffer */
+	if (copied != 0) {
+		unsigned int newsize = priv->next + copied;
+
+		if (newsize > priv->size) {
+			char *newbuf;
+
+			newbuf = krealloc(priv->buf, newsize, GFP_KERNEL);
+			if (newbuf) {
+				priv->buf  = newbuf;
+				priv->size = newsize;
+			} else {
+				copied = priv->size - priv->next;
+			}
+		}
+
+		strncpy(priv->buf + priv->next, msg->data + printed, copied);
+		priv->next += copied;
+	}
+
+	done.command = RPMSG_SYSLOG_TRANSFER_DONE;
+	done.result  = printed + copied;
+	return rpmsg_send(rpdev->ept, &done, sizeof(done));
+}
+
+static int rpmsg_syslog_probe(struct rpmsg_device *rpdev)
+{
+	struct rpmsg_syslog *priv;
+
+	priv = devm_kzalloc(&rpdev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	dev_set_drvdata(&rpdev->dev, priv);
+	return 0;
+}
+
+static void rpmsg_syslog_remove(struct rpmsg_device *rpdev)
+{
+	struct rpmsg_syslog *priv = dev_get_drvdata(&rpdev->dev);
+
+	/* flush the buffered log if need */
+	if (priv->next)
+		pr_info("%.*s\n", priv->next, priv->buf);
+	kfree(priv->buf);
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int rpmsg_syslog_dev_suspend(struct device *dev)
+{
+	struct rpmsg_device *rpdev = dev_get_drvdata(dev);
+	struct rpmsg_syslog_suspend msg = {
+		.command = RPMSG_SYSLOG_SUSPEND,
+	};
+
+	return rpmsg_send(rpdev->ept, &msg, sizeof(msg));
+}
+
+static int rpmsg_syslog_dev_resume(struct device *dev)
+{
+	struct rpmsg_device *rpdev = dev_get_drvdata(dev);
+	struct rpmsg_syslog_resume msg = {
+		.command = RPMSG_SYSLOG_RESUME,
+	};
+
+	return rpmsg_send(rpdev->ept, &msg, sizeof(msg));
+}
+#endif
+
+static SIMPLE_DEV_PM_OPS(rpmsg_syslog_pm,
+			rpmsg_syslog_dev_suspend,
+			rpmsg_syslog_dev_resume);
+
+static const struct rpmsg_device_id rpmsg_syslog_id_table[] = {
+	{ .name = "rpmsg-syslog" },
+	{ },
+};
+MODULE_DEVICE_TABLE(rpmsg, rpmsg_syslog_id_table);
+
+static struct rpmsg_driver rpmsg_syslog_driver = {
+	.drv = {
+		.name  = "rpmsg_syslog",
+		.owner = THIS_MODULE,
+		.pm    = &rpmsg_syslog_pm,
+	},
+
+	.id_table = rpmsg_syslog_id_table,
+	.probe    = rpmsg_syslog_probe,
+	.callback = rpmsg_syslog_callback,
+	.remove   = rpmsg_syslog_remove,
+};
+
+module_rpmsg_driver(rpmsg_syslog_driver);
+
+MODULE_ALIAS("rpmsg:rpmsg_syslog");
+MODULE_AUTHOR("Guiding Li <liguiding@pinecone.net>");
+MODULE_DESCRIPTION("rpmsg syslog redirection driver");
+MODULE_LICENSE("GPL v2");
-- 
2.7.4


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] lib/string: add memrchr function
  2019-02-14  3:48 [PATCH 1/2] lib/string: add memrchr function Xiang Xiao
  2019-02-14  3:48 ` [PATCH 2/2] rpmsg: add syslog redirection driver Xiang Xiao
@ 2019-02-14  4:08 ` Randy Dunlap
  1 sibling, 0 replies; 4+ messages in thread
From: Randy Dunlap @ 2019-02-14  4:08 UTC (permalink / raw)
  To: Xiang Xiao, gregkh, alexander.shishkin, andriy.shevchenko, ohad,
	bjorn.andersson, wendy.liang, arnaud.pouliquen, kumar.gala,
	linux-remoteproc, linux-kernel
  Cc: Xiang Xiao

On 2/13/19 7:48 PM, Xiang Xiao wrote:
> Here is the detailed description for memrchr:
> 
> void *memrchr(const void *s, int c, size_t n);
> 
> The memrchr() function is like the memchr() function, except
> that it searches backward from the end of the n bytes pointed
> to by s instead of forward from the beginning.
> 
> The memrchr() functions return a pointer to the matching byte
> or NULL if the character does not occur in the given memory
> area.
> 
> Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
> ---
>  include/linux/string.h |  1 +
>  lib/string.c           | 21 +++++++++++++++++++++
>  2 files changed, 22 insertions(+)
> 
> diff --git a/include/linux/string.h b/include/linux/string.h
> index 7927b87..f380f4b 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -167,6 +167,7 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
>  	memcpy(dst, src, cnt);
>  }
>  #endif
> +void *memrchr(const void *s, int c, size_t n);
>  void *memchr_inv(const void *s, int c, size_t n);
>  char *strreplace(char *s, char old, char new);
>  
> diff --git a/lib/string.c b/lib/string.c
> index 38e4ca0..92914f6 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -964,6 +964,27 @@ void *memchr(const void *s, int c, size_t n)
>  EXPORT_SYMBOL(memchr);
>  #endif
>  
> +/**
> + * memrchr - Find a character in an area of memory.

This should say up front that this is a reverse search, from the end of
the memory area backwards.

> + * @s: The memory area
> + * @c: The byte to search for
> + * @n: The size of the area.
> + *
> + * returns the address of the last occurrence of @c, or %NULL

    * Return: the address of the last occurrence of @c, or %NULL

> + * if @c is not found
> + */
> +void *memrchr(const void *s, int c, size_t n)
> +{
> +	const unsigned char *p = s + n;
> +
> +	while (n-- != 0) {
> +		if ((unsigned char)c == *--p)
> +			return (void *)p;
> +	}
> +	return NULL;
> +}
> +EXPORT_SYMBOL(memrchr);
> +
>  static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
>  {
>  	while (bytes) {
> 


-- 
~Randy

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] rpmsg: add syslog redirection driver
  2019-02-14  3:48 ` [PATCH 2/2] rpmsg: add syslog redirection driver Xiang Xiao
@ 2019-02-14  4:08   ` Randy Dunlap
  0 siblings, 0 replies; 4+ messages in thread
From: Randy Dunlap @ 2019-02-14  4:08 UTC (permalink / raw)
  To: Xiang Xiao, gregkh, alexander.shishkin, andriy.shevchenko, ohad,
	bjorn.andersson, wendy.liang, arnaud.pouliquen, kumar.gala,
	linux-remoteproc, linux-kernel
  Cc: Guiding Li

On 2/13/19 7:48 PM, Xiang Xiao wrote:
> From: Guiding Li <liguiding@pinecone.net>
> 
> This driver allows the remote processor redirect the output of

                                processor to redirect

> syslog/printf into the kernel log, which is very useful to see
> what happen in the remote side.
> 
> Signed-off-by: Guiding Li <liguiding@pinecone.net>
> ---
>  drivers/rpmsg/Kconfig        |  12 ++++
>  drivers/rpmsg/Makefile       |   1 +
>  drivers/rpmsg/rpmsg_syslog.c | 163 +++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 176 insertions(+)
>  create mode 100644 drivers/rpmsg/rpmsg_syslog.c
> 
> diff --git a/drivers/rpmsg/Kconfig b/drivers/rpmsg/Kconfig
> index d0322b4..d701614 100644
> --- a/drivers/rpmsg/Kconfig
> +++ b/drivers/rpmsg/Kconfig
> @@ -15,6 +15,18 @@ config RPMSG_CHAR
>  	  in /dev. They make it possible for user-space programs to send and
>  	  receive rpmsg packets.
>  
> +config RPMSG_SYSLOG
> +	tristate "RPMSG syslog redirection"
> +	depends on RPMSG
> +	help
> +	  Say Y here to redirect the syslog/printf from remote processor into
> +	  the kernel log which is very useful to see what happen in the remote

	                                         see what happens
	or                                       see what happened

> +	  side.
> +
> +	  If the remote processor hang during bootup or panic at the runtime,

	                          hangs during bootup or panics during runtime,


> +	  We can even cat /sys/kernel/debug/remoteproc/remoteprocX/trace0 to

	  we

> +	  get the last log which doesn't output yet.

	  get the last log which hasn't been output yet.

> +
>  config RPMSG_QCOM_GLINK_NATIVE
>  	tristate
>  	select RPMSG

-- 
~Randy

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-02-14  4:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-14  3:48 [PATCH 1/2] lib/string: add memrchr function Xiang Xiao
2019-02-14  3:48 ` [PATCH 2/2] rpmsg: add syslog redirection driver Xiang Xiao
2019-02-14  4:08   ` Randy Dunlap
2019-02-14  4:08 ` [PATCH 1/2] lib/string: add memrchr function Randy Dunlap

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®