* [PATCH] printk: Remove print_prefix() calls with NULL buffer.
@ 2018-12-11 9:49 Tetsuo Handa
2018-12-11 15:24 ` Petr Mladek
2018-12-12 2:20 ` Sergey Senozhatsky
0 siblings, 2 replies; 5+ messages in thread
From: Tetsuo Handa @ 2018-12-11 9:49 UTC (permalink / raw)
To: Petr Mladek, Sergey Senozhatsky, Steven Rostedt
Cc: linux-kernel, Tetsuo Handa
We can save lines/size by removing print_prefix() with buf == NULL.
This patch makes no functional change.
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
kernel/printk/printk.c | 39 ++++++++++++++-------------------------
1 file changed, 14 insertions(+), 25 deletions(-)
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 49bcf0e..1306fe0 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -1227,13 +1227,15 @@ static inline void boot_delay_msec(int level)
static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME);
module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
+static size_t print_syslog(unsigned int level, char *buf)
+{
+ return sprintf(buf, "<%u>", level);
+}
+
static size_t print_time(u64 ts, char *buf)
{
unsigned long rem_nsec = do_div(ts, 1000000000);
- if (!buf)
- return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);
-
return sprintf(buf, "[%5lu.%06lu] ",
(unsigned long)ts, rem_nsec / 1000);
}
@@ -1242,24 +1244,11 @@ static size_t print_prefix(const struct printk_log *msg, bool syslog,
bool time, char *buf)
{
size_t len = 0;
- unsigned int prefix = (msg->facility << 3) | msg->level;
-
- if (syslog) {
- if (buf) {
- len += sprintf(buf, "<%u>", prefix);
- } else {
- len += 3;
- if (prefix > 999)
- len += 3;
- else if (prefix > 99)
- len += 2;
- else if (prefix > 9)
- len++;
- }
- }
+ if (syslog)
+ len = print_syslog((msg->facility << 3) | msg->level, buf);
if (time)
- len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
+ len += print_time(msg->ts_nsec, buf + len);
return len;
}
@@ -1269,6 +1258,8 @@ static size_t msg_print_text(const struct printk_log *msg, bool syslog,
const char *text = log_text(msg);
size_t text_size = msg->text_len;
size_t len = 0;
+ char prefix[PREFIX_MAX];
+ const size_t prefix_len = print_prefix(msg, syslog, time, prefix);
do {
const char *next = memchr(text, '\n', text_size);
@@ -1283,19 +1274,17 @@ static size_t msg_print_text(const struct printk_log *msg, bool syslog,
}
if (buf) {
- if (print_prefix(msg, syslog, time, NULL) +
- text_len + 1 >= size - len)
+ if (prefix_len + text_len + 1 >= size - len)
break;
- len += print_prefix(msg, syslog, time, buf + len);
+ memcpy(buf + len, prefix, prefix_len);
+ len += prefix_len;
memcpy(buf + len, text, text_len);
len += text_len;
buf[len++] = '\n';
} else {
/* SYSLOG_ACTION_* buffer size only calculation */
- len += print_prefix(msg, syslog, time, NULL);
- len += text_len;
- len++;
+ len += prefix_len + text_len + 1;
}
text = next;
--
1.8.3.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] printk: Remove print_prefix() calls with NULL buffer.
2018-12-11 9:49 [PATCH] printk: Remove print_prefix() calls with NULL buffer Tetsuo Handa
@ 2018-12-11 15:24 ` Petr Mladek
2018-12-12 2:20 ` Sergey Senozhatsky
1 sibling, 0 replies; 5+ messages in thread
From: Petr Mladek @ 2018-12-11 15:24 UTC (permalink / raw)
To: Tetsuo Handa; +Cc: Sergey Senozhatsky, Steven Rostedt, linux-kernel
On Tue 2018-12-11 18:49:05, Tetsuo Handa wrote:
> We can save lines/size by removing print_prefix() with buf == NULL.
> This patch makes no functional change.
>
> Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Nice clean up. It makes the code better readable.
Acked-by: Petr Mladek <pmladek@suse.com>
Best Regards,
Petr
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] printk: Remove print_prefix() calls with NULL buffer.
2018-12-11 9:49 [PATCH] printk: Remove print_prefix() calls with NULL buffer Tetsuo Handa
2018-12-11 15:24 ` Petr Mladek
@ 2018-12-12 2:20 ` Sergey Senozhatsky
2018-12-12 13:16 ` Petr Mladek
1 sibling, 1 reply; 5+ messages in thread
From: Sergey Senozhatsky @ 2018-12-12 2:20 UTC (permalink / raw)
To: Tetsuo Handa
Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, linux-kernel
On (12/11/18 18:49), Tetsuo Handa wrote:
>
> We can save lines/size by removing print_prefix() with buf == NULL.
> This patch makes no functional change.
>
> Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Looks good to me,
Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Shouldn't this also have "Suggested-by: Petr Mladek" ?
-ss
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] printk: Remove print_prefix() calls with NULL buffer.
2018-12-12 2:20 ` Sergey Senozhatsky
@ 2018-12-12 13:16 ` Petr Mladek
2018-12-12 13:20 ` Sergey Senozhatsky
0 siblings, 1 reply; 5+ messages in thread
From: Petr Mladek @ 2018-12-12 13:16 UTC (permalink / raw)
To: Sergey Senozhatsky
Cc: Tetsuo Handa, Sergey Senozhatsky, Steven Rostedt, linux-kernel
On Wed 2018-12-12 11:20:29, Sergey Senozhatsky wrote:
> On (12/11/18 18:49), Tetsuo Handa wrote:
> >
> > We can save lines/size by removing print_prefix() with buf == NULL.
> > This patch makes no functional change.
> >
> > Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
>
> Looks good to me,
> Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Thanks. I have pushed the patch into printk.git, for-4.21 branch.
> Shouldn't this also have "Suggested-by: Petr Mladek" ?
I did not add this tag. This idea comes from some other Tetsuo's patch.
Best Regards,
Petr
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] printk: Remove print_prefix() calls with NULL buffer.
2018-12-12 13:16 ` Petr Mladek
@ 2018-12-12 13:20 ` Sergey Senozhatsky
0 siblings, 0 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2018-12-12 13:20 UTC (permalink / raw)
To: Petr Mladek, Tetsuo Handa
Cc: Sergey Senozhatsky, Sergey Senozhatsky, Steven Rostedt, linux-kernel
On (12/12/18 14:16), Petr Mladek wrote:
> On Wed 2018-12-12 11:20:29, Sergey Senozhatsky wrote:
> > On (12/11/18 18:49), Tetsuo Handa wrote:
> > >
> > > We can save lines/size by removing print_prefix() with buf == NULL.
> > > This patch makes no functional change.
> > >
> > > Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
> >
> > Looks good to me,
> > Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
>
> Thanks. I have pushed the patch into printk.git, for-4.21 branch.
Thanks.
> > Shouldn't this also have "Suggested-by: Petr Mladek" ?
>
> I did not add this tag. This idea comes from some other Tetsuo's patch.
Oh, OK. Sorry then, seems I was under wrong impression.
-ss
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2018-12-12 13:20 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-11 9:49 [PATCH] printk: Remove print_prefix() calls with NULL buffer Tetsuo Handa
2018-12-11 15:24 ` Petr Mladek
2018-12-12 2:20 ` Sergey Senozhatsky
2018-12-12 13:16 ` Petr Mladek
2018-12-12 13:20 ` Sergey Senozhatsky
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).