Re: [PATCH v2 3/4] printk/btrfs: Handle more message headers

From: Geert Uytterhoeven
Date: Tue Dec 13 2016 - 04:21:35 EST


Hi Petr,

On Wed, Nov 9, 2016 at 1:41 PM, Petr Mladek <pmladek@xxxxxxxx> wrote:
> The commit 4bcc595ccd80decb4245096e ("printk: reinstate KERN_CONT for
> printing continuation lines") allows to define more message headers
> for a single message. The motivation is that continuous lines might
> get mixed. Therefore it make sense to define the right log level
> for every piece of a cont line.
>
> The current btrfs_printk() macros do not support continuous lines
> at the moment. But better be prepared for a custom messages and
> avoid potential "lvl" buffer overflow.
>
> This patch iterates over the entire message header. It is interested
> only into the message level like the original code.
>
> This patch also introduces PRINTK_MAX_SINGLE_HEADER_LEN. Three bytes
> are enough for the message level header at the moment. But it used to
> be three, see the commit 04d2c8c83d0e3ac5f ("printk: convert the format
> for KERN_<LEVEL> to a 2 byte pattern").
>
> Also I fixed the default ratelimit level. It looked very strange
> when it was different from the default log level.
>
> Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
> ---
> fs/btrfs/super.c | 26 +++++++++++++++-----------
> include/linux/printk.h | 2 ++
> 2 files changed, 17 insertions(+), 11 deletions(-)
>
> diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
> index 74ed5aae6cea..c083d84eaa32 100644
> --- a/fs/btrfs/super.c
> +++ b/fs/btrfs/super.c
> @@ -202,27 +202,31 @@ void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function
> void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
> {
> struct super_block *sb = fs_info->sb;
> - char lvl[4];
> + char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1];
> struct va_format vaf;
> va_list args;
> - const char *type = logtypes[4];
> + const char *type = NULL;
> int kern_level;
> struct ratelimit_state *ratelimit;

warning: âratelimitâ may be used uninitialized in this function

So this triggered my attention. It seems my gcc (4.1.2) is not smart enough
to notice that ratelimit will be set to a default value if !type.

Still, IMHO the code is too convoluted for the casual reader.

> va_start(args, fmt);
>
> - kern_level = printk_get_level(fmt);
> - if (kern_level) {
> + while ((kern_level = printk_get_level(fmt)) != 0) {
> size_t size = printk_skip_level(fmt) - fmt;
> - memcpy(lvl, fmt, size);
> - lvl[size] = '\0';
> +
> + if (kern_level >= '0' || kern_level <= '7') {
> + memcpy(lvl, fmt, size);
> + lvl[size] = '\0';
> + type = logtypes[kern_level - '0'];
> + ratelimit = &printk_limits[kern_level - '0'];
> + }
> fmt += size;
> - type = logtypes[kern_level - '0'];
> - ratelimit = &printk_limits[kern_level - '0'];
> - } else {
> + }
> +
> + if (!type) {
> *lvl = '\0';
> - /* Default to debug output */
> - ratelimit = &printk_limits[7];
> + type = logtypes[4];
> + ratelimit = &printk_limits[4];
> }
>
> vaf.fmt = fmt;
> diff --git a/include/linux/printk.h b/include/linux/printk.h
> index a0859e169bc3..afe8ccec1672 100644
> --- a/include/linux/printk.h
> +++ b/include/linux/printk.h
> @@ -10,6 +10,8 @@
> extern const char linux_banner[];
> extern const char linux_proc_banner[];
>
> +#define PRINTK_MAX_SINGLE_HEADER_LEN 2

I think you want to use this definition instead of the hardcoded "2" in
printk_skip_level():

| static inline const char *printk_skip_level(const char *buffer)
| {
| if (printk_get_level(buffer))
| return buffer + 2;

return buffer + PRINTK_MAX_SINGLE_HEADER_LEN;

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@xxxxxxxxxxxxxx

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds