printk: Remove unnecessary kmalloc() from syslog during clear
authorNamit Gupta <gupta.namit@samsung.com>
Wed, 20 Jun 2018 13:56:19 +0000 (19:26 +0530)
committerPetr Mladek <pmladek@suse.com>
Wed, 27 Jun 2018 14:16:28 +0000 (16:16 +0200)
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@epcas5p3.samsung.com
Cc: linux-kernel@vger.kernel.org
Cc: pankaj.m@samsung.com
Cc: a.sahrawat@samsung.com
Signed-off-by: Namit Gupta <gupta.namit@samsung.com>
Signed-off-by: Himanshu Maithani <himanshu.m@samsung.com>
Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Signed-off-by: Petr Mladek <pmladek@suse.com>
kernel/printk/printk.c

index 3999c295d6f71de2ce5c219cd3aeeb6aafc34137..16b02cc51a14cb4b5a59c3e3966d71e0409f4fe3 100644 (file)
@@ -1352,71 +1352,78 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
 {
        char *text;
        int len = 0;
+       u64 next_seq;
+       u64 seq;
+       u32 idx;
+
+       if (!buf) {
+               if (clear) {
+                       logbuf_lock_irq();
+                       clear_seq = log_next_seq;
+                       clear_idx = log_next_idx;
+                       logbuf_unlock_irq();
+               }
+               return 0;
+       }
 
        text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
        if (!text)
                return -ENOMEM;
 
        logbuf_lock_irq();
-       if (buf) {
-               u64 next_seq;
-               u64 seq;
-               u32 idx;
+       /*
+        * Find first record that fits, including all following records,
+        * into the user-provided buffer for this dump.
+        */
+       seq = clear_seq;
+       idx = clear_idx;
+       while (seq < log_next_seq) {
+               struct printk_log *msg = log_from_idx(idx);
 
-               /*
-                * Find first record that fits, including all following records,
-                * into the user-provided buffer for this dump.
-                */
-               seq = clear_seq;
-               idx = clear_idx;
-               while (seq < log_next_seq) {
-                       struct printk_log *msg = log_from_idx(idx);
-
-                       len += msg_print_text(msg, true, NULL, 0);
-                       idx = log_next(idx);
-                       seq++;
-               }
+               len += msg_print_text(msg, true, NULL, 0);
+               idx = log_next(idx);
+               seq++;
+       }
 
-               /* move first record forward until length fits into the buffer */
-               seq = clear_seq;
-               idx = clear_idx;
-               while (len > size && seq < log_next_seq) {
-                       struct printk_log *msg = log_from_idx(idx);
+       /* move first record forward until length fits into the buffer */
+       seq = clear_seq;
+       idx = clear_idx;
+       while (len > size && seq < log_next_seq) {
+               struct printk_log *msg = log_from_idx(idx);
 
-                       len -= msg_print_text(msg, true, NULL, 0);
-                       idx = log_next(idx);
-                       seq++;
-               }
+               len -= msg_print_text(msg, true, NULL, 0);
+               idx = log_next(idx);
+               seq++;
+       }
 
-               /* last message fitting into this dump */
-               next_seq = log_next_seq;
+       /* last message fitting into this dump */
+       next_seq = log_next_seq;
 
-               len = 0;
-               while (len >= 0 && seq < next_seq) {
-                       struct printk_log *msg = log_from_idx(idx);
-                       int textlen;
+       len = 0;
+       while (len >= 0 && seq < next_seq) {
+               struct printk_log *msg = log_from_idx(idx);
+               int textlen;
 
-                       textlen = msg_print_text(msg, true, text,
-                                                LOG_LINE_MAX + PREFIX_MAX);
-                       if (textlen < 0) {
-                               len = textlen;
-                               break;
-                       }
-                       idx = log_next(idx);
-                       seq++;
+               textlen = msg_print_text(msg, true, text,
+                                        LOG_LINE_MAX + PREFIX_MAX);
+               if (textlen < 0) {
+                       len = textlen;
+                       break;
+               }
+               idx = log_next(idx);
+               seq++;
 
-                       logbuf_unlock_irq();
-                       if (copy_to_user(buf + len, text, textlen))
-                               len = -EFAULT;
-                       else
-                               len += textlen;
-                       logbuf_lock_irq();
+               logbuf_unlock_irq();
+               if (copy_to_user(buf + len, text, textlen))
+                       len = -EFAULT;
+               else
+                       len += textlen;
+               logbuf_lock_irq();
 
-                       if (seq < log_first_seq) {
-                               /* messages are gone, move to next one */
-                               seq = log_first_seq;
-                               idx = log_first_idx;
-                       }
+               if (seq < log_first_seq) {
+                       /* messages are gone, move to next one */
+                       seq = log_first_seq;
+                       idx = log_first_idx;
                }
        }