linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* printk: /dev/kmsg - properly support writev() to avoid interleaved printk() lines
@ 2011-04-07  2:29 Kay Sievers
  2011-04-12 23:13 ` Andrew Morton
  0 siblings, 1 reply; 2+ messages in thread
From: Kay Sievers @ 2011-04-07  2:29 UTC (permalink / raw)
  To: Greg KH; +Cc: linux-kernel, Lennart Poettering

From: Kay Sievers <kay.sievers@vrfy.org>

printk: /dev/kmsg - properly support writev() to avoid interleaved printk lines

We should avoid calling printk() in a loop, when we pass a single
string to /dev/kmsg with writev(). 

Cc: Lennart Poettering <lennart@poettering.net>
Signed-off-by: Kay Sievers <kay.sievers@vrfy.org>
---
 mem.c |   41 ++++++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index 436a990..78923a9 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -806,29 +806,40 @@ static const struct file_operations oldmem_fops = {
 };
 #endif
 
-static ssize_t kmsg_write(struct file *file, const char __user *buf,
-			  size_t count, loff_t *ppos)
+static ssize_t kmsg_writev(struct kiocb *iocb, const struct iovec *iv,
+			   unsigned long count, loff_t pos)
 {
-	char *tmp;
-	ssize_t ret;
+	char *line, *p;
+	int len, i;
+	ssize_t ret = -EFAULT;
 
-	tmp = kmalloc(count + 1, GFP_KERNEL);
-	if (tmp == NULL)
+	len = iov_length(iv, count);
+	line = p = kmalloc(len + 1, GFP_KERNEL);
+	if (line == NULL)
 		return -ENOMEM;
-	ret = -EFAULT;
-	if (!copy_from_user(tmp, buf, count)) {
-		tmp[count] = 0;
-		ret = printk("%s", tmp);
-		if (ret > count)
-			/* printk can add a prefix */
-			ret = count;
+
+	/*
+	 * copy all vectors into a single string, to ensure we do
+	 * not interleave our log line with other printk calls
+	 */
+	for (i = 0; i < count; i++) {
+		if (copy_from_user(p, iv[i].iov_base, iv[i].iov_len))
+			goto out;
+		p += iv[i].iov_len;
 	}
-	kfree(tmp);
+	p[0] = '\0';
+
+	ret = printk("%s", line);
+	/* printk can add a prefix */
+	if (ret > len)
+		ret = len;
+out:
+	kfree(line);
 	return ret;
 }
 
 static const struct file_operations kmsg_fops = {
-	.write = kmsg_write,
+	.aio_write = kmsg_writev,
 	.llseek = noop_llseek,
 };
 


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

* Re: printk: /dev/kmsg - properly support writev() to avoid interleaved printk() lines
  2011-04-07  2:29 printk: /dev/kmsg - properly support writev() to avoid interleaved printk() lines Kay Sievers
@ 2011-04-12 23:13 ` Andrew Morton
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2011-04-12 23:13 UTC (permalink / raw)
  To: Kay Sievers; +Cc: Greg KH, linux-kernel, Lennart Poettering

On Thu, 07 Apr 2011 04:29:20 +0200
Kay Sievers <kay.sievers@vrfy.org> wrote:

> From: Kay Sievers <kay.sievers@vrfy.org>
> 
> printk: /dev/kmsg - properly support writev() to avoid interleaved printk lines
> 
> We should avoid calling printk() in a loop, when we pass a single
> string to /dev/kmsg with writev(). 
> 
> ...
> --- a/drivers/char/mem.c
> +++ b/drivers/char/mem.c
> @@ -806,29 +806,40 @@ static const struct file_operations oldmem_fops = {
>  };
>  #endif
>  
> -static ssize_t kmsg_write(struct file *file, const char __user *buf,
> -			  size_t count, loff_t *ppos)
> +static ssize_t kmsg_writev(struct kiocb *iocb, const struct iovec *iv,
> +			   unsigned long count, loff_t pos)
>  {
> -	char *tmp;
> -	ssize_t ret;
> +	char *line, *p;
> +	int len, i;
> +	ssize_t ret = -EFAULT;
>  
> -	tmp = kmalloc(count + 1, GFP_KERNEL);
> -	if (tmp == NULL)
> +	len = iov_length(iv, count);
> +	line = p = kmalloc(len + 1, GFP_KERNEL);
> +	if (line == NULL)
>  		return -ENOMEM;
> -	ret = -EFAULT;
> -	if (!copy_from_user(tmp, buf, count)) {
> -		tmp[count] = 0;
> -		ret = printk("%s", tmp);
> -		if (ret > count)
> -			/* printk can add a prefix */
> -			ret = count;
> +
> +	/*
> +	 * copy all vectors into a single string, to ensure we do
> +	 * not interleave our log line with other printk calls
> +	 */
> +	for (i = 0; i < count; i++) {
> +		if (copy_from_user(p, iv[i].iov_base, iv[i].iov_len))
> +			goto out;
> +		p += iv[i].iov_len;
>  	}
> -	kfree(tmp);
> +	p[0] = '\0';
> +
> +	ret = printk("%s", line);
> +	/* printk can add a prefix */
> +	if (ret > len)
> +		ret = len;
> +out:
> +	kfree(line);
>  	return ret;
>  }
>  
>  static const struct file_operations kmsg_fops = {
> -	.write = kmsg_write,
> +	.aio_write = kmsg_writev,
>  	.llseek = noop_llseek,
>  };
>  

LGTM.  Nitpicks:

From: Andrew Morton <akpm@linux-foundation.org>

make `len' size_t, avoid multiple-assignments.

Cc: Greg KH <gregkh@suse.de>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Lennart Poettering <lennart@poettering.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 drivers/char/mem.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff -puN drivers/char/mem.c~dev-kmsg-properly-support-writev-to-avoid-interleaved-printk-lines-fix drivers/char/mem.c
--- a/drivers/char/mem.c~dev-kmsg-properly-support-writev-to-avoid-interleaved-printk-lines-fix
+++ a/drivers/char/mem.c
@@ -810,11 +810,11 @@ static ssize_t kmsg_writev(struct kiocb 
 			   unsigned long count, loff_t pos)
 {
 	char *line, *p;
-	int len, i;
+	int i;
 	ssize_t ret = -EFAULT;
+	size_t len = iov_length(iv, count);
 
-	len = iov_length(iv, count);
-	line = p = kmalloc(len + 1, GFP_KERNEL);
+	line = kmalloc(len + 1, GFP_KERNEL);
 	if (line == NULL)
 		return -ENOMEM;
 
@@ -822,6 +822,7 @@ static ssize_t kmsg_writev(struct kiocb 
 	 * copy all vectors into a single string, to ensure we do
 	 * not interleave our log line with other printk calls
 	 */
+	p = line;
 	for (i = 0; i < count; i++) {
 		if (copy_from_user(p, iv[i].iov_base, iv[i].iov_len))
 			goto out;
_




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

end of thread, other threads:[~2011-04-12 23:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-07  2:29 printk: /dev/kmsg - properly support writev() to avoid interleaved printk() lines Kay Sievers
2011-04-12 23:13 ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).