Re: [PATCH printk v2 05/12] printk: add macro for console detail messages

From: Petr Mladek
Date: Wed Apr 06 2022 - 09:36:37 EST


On Tue 2022-04-05 15:31:28, John Ogness wrote:
> It is useful to generate log messages that include details about
> the related console. Rather than duplicate the logic to assemble
> the details, put that logic into a macro printk_console_msg().
>
> Once console printers become threaded, this macro will find many
> users.

I really like this idea.

> Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
> ---
> kernel/printk/printk.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index fb9a6eaf54fd..39ca9d758e52 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -2993,6 +2993,10 @@ static void try_enable_default_console(struct console *newcon)
> newcon->flags |= CON_CONSDEV;
> }
>
> +#define printk_console_msg(c, lvl, msg) \
> + printk(lvl pr_fmt("%sconsole [%s%d] " msg "\n"), \
> + (c->flags & CON_BOOT) ? "boot" : "", c->name, c->index)

This might be dangerous because "msg" is handled as printk format.
Any potential %<modifier> might cause unexpected memory access.

Honestly, I am not good at writing macros. I played with it and
the following worked for me:

#define printk_console_msg(c, lvl, fmt, ...) \
printk(lvl pr_fmt("%sconsole [%s%d] " fmt), \
(c->flags & CON_BOOT) ? "boot" : "", \
c->name, c->index, \
##__VA_ARGS__)

Note that it requires adding "\n" back to the callers.

Also, please, call it "console_printk()" or so. It needs extra
arguments and this name will help to distinguish it from
the plain printk() wrappers. From my POV, it is something like,
for example, dev_printk(), snd_printk(), or xfs_printk_ratelimited().

Best Regards,
Petr