Re: [RFC PATCH] vsprintf: Add %pv extension replacement for print_vma_addr

From: Sergey Senozhatsky
Date: Sat Aug 15 2020 - 17:54:27 EST


Cc-ing John

On (20/08/14 10:53), Joe Perches wrote:
[..]

In general, the idea looks nice.

> +static noinline_for_stack
> +char *vma_addr(char *buf, char *end, void *ip,
> + struct printf_spec spec, const char *fmt)
> +{
> +#ifdef CONFIG_MMU
> + struct mm_struct *mm = current->mm;
> + struct vm_area_struct *vma;
> +
> + /*
> + * we might be running from an atomic context so we cannot sleep
> + */
> + if (!mmap_read_trylock(mm))
> + return buf;
> +
> + vma = find_vma(mm, (unsigned long)ip);
> + if (vma && vma->vm_file) {
> + char *p;
> + struct file *f = vma->vm_file;
> + char *page = (char *)__get_free_page(GFP_NOWAIT);

Hmm, this is huge. For the time being this is going to introduce lock
inversion chains:

PRINTK -> printk_locks -> MM -> mm_locks

vs
MM -> mm_locks -> PRINTK -> printk_locks

Another thing to mention is

PRINTK -> printk_locks -> MM (WANR_ON/etc) -> PRINTK

we are in printk_safe, currently, but that's going to change.

We might not be ready to take this as of now, but things can change
once we drop printk_locks.

> + if (page) {
> + p = file_path(f, page, PAGE_SIZE);
> + if (IS_ERR(p))
> + p = "?";
> + buf = string(buf, end, kbasename(p), default_str_spec);
> + buf = string_nocheck(buf, end, "[", default_str_spec);
> + buf = pointer_string(buf, end,
> + (void *)vma->vm_start,
> + default_hex_spec);
> + buf = string_nocheck(buf, end, "+", default_str_spec);
> + buf = pointer_string(buf, end,
> + (void *)(vma->vm_end - vma->vm_start),
> + default_hex_spec);
> + buf = string_nocheck(buf, end, "]", default_str_spec);
> + free_page((unsigned long)page);
> + }
> + }
> + mmap_read_unlock(mm);
> +#else
> + buf = string_nocheck(buf, end, "CONFIG_MMU=n", default_str_spec);

Hmm, we don't usually do that CONFIG_FOO=n thing, I think we just need
to skip %pv and print nothing. Otherwise on !CONFIG_MMU systems the logbuf
may contain a number of CONFIG_MMU=n messages, which are hardly useful.

> +#endif
> + return buf;
> +}
> +
> /*
> * Show a '%p' thing. A kernel extension is that the '%p' is followed
> * by an extra set of alphanumeric characters that are extended format
> @@ -2254,6 +2304,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
> return uuid_string(buf, end, ptr, spec, fmt);
> case 'V':
> return va_format(buf, end, ptr, spec, fmt);

+ #ifdef CONFIG_MMU
> + case 'v':
> + return vma_addr(buf, end, ptr, spec, fmt);
+ #endif

> case 'K':
> return restricted_pointer(buf, end, ptr, spec);
> case 'N':

-ss