[PATCH] printk: remove unnecessary kmalloc() from syslog during

From: Namit Gupta
Date: Mon Jun 25 2018 - 08:58:05 EST


When the request is only for clearing logs, there is no need for
allocation/deallocation. Only the indexes need to be reset and returned.
Rest of the patch is mostly made up of changes because of indention.

Link: http://lkml.kernel.org/r/20180620135951epcas5p3bd2a8f25ec689ca333bce861b527dba2~54wyKcT0_3155531555epcas5p3y@xxxxxxxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Cc: pankaj.m@xxxxxxxxxxx
Cc: a.sahrawat@xxxxxxxxxxx
Signed-off-by: Namit Gupta <gupta.namit@xxxxxxxxxxx>
Signed-off-by: Himanshu Maithani <himanshu.m@xxxxxxxxxxx>
Reviewed-by: Steven Rostedt (VMware) <rostedt@xxxxxxxxxxx>
Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@xxxxxxxxx>
[pmladek@xxxxxxxx: Removed code duplication.]
Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
kernel/printk/printk.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 247808333ba4..0fa2ca6fd8f9 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -1350,12 +1350,14 @@ static int syslog_print(char __user *buf, int size)

static int syslog_print_all(char __user *buf, int size, bool clear)
{
- char *text;
+ char *text = NULL;
int len = 0;

- text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
- if (!text)
- return -ENOMEM;
+ if (buf) {
+ text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
+ if (!text)
+ return -ENOMEM;
+ }

logbuf_lock_irq();
if (buf) {
@@ -1426,7 +1428,8 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
}
logbuf_unlock_irq();

- kfree(text);
+ if (text)
+ kfree(text);
return len;
}

--
2.13.7