diff mbox series

[RFCv3,2/3] lib/vsprintf.c: make %pD print full path for file

Message ID 20210611155953.3010-3-justin.he@arm.com (mailing list archive)
State New, archived
Headers show
Series make '%pD' print full path for file | expand

Commit Message

Justin He June 11, 2021, 3:59 p.m. UTC
We have '%pD' for printing a filename. It may not be perfect (by
default it only prints one component.)

As suggested by Linus at [1]:
A dentry has a parent, but at the same time, a dentry really does
inherently have "one name" (and given just the dentry pointers, you
can't show mount-related parenthood, so in many ways the "show just
one name" makes sense for "%pd" in ways it doesn't necessarily for
"%pD"). But while a dentry arguably has that "one primary component",
a _file_ is certainly not exclusively about that last component.

Hence change the behavior of '%pD' to print full path of that file.

Things become more complicated when spec.precision and spec.field_width
is added in. string_truncate() is to handle the small space case for
'%pD' precision and field_width.

[1] https://lore.kernel.org/lkml/CAHk-=wimsMqGdzik187YWLb-ru+iktb4MYbMQG1rnZ81dXYFVg@mail.gmail.com/

Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Jia He <justin.he@arm.com>
---
 Documentation/core-api/printk-formats.rst |  5 ++-
 lib/vsprintf.c                            | 47 +++++++++++++++++++++--
 2 files changed, 46 insertions(+), 6 deletions(-)

Comments

Rasmus Villemoes June 11, 2021, 9:28 p.m. UTC | #1
On 11/06/2021 17.59, Jia He wrote:
> We have '%pD' for printing a filename. It may not be perfect (by
> default it only prints one component.)
> 
> As suggested by Linus at [1]:
> A dentry has a parent, but at the same time, a dentry really does
> inherently have "one name" (and given just the dentry pointers, you
> can't show mount-related parenthood, so in many ways the "show just
> one name" makes sense for "%pd" in ways it doesn't necessarily for
> "%pD"). But while a dentry arguably has that "one primary component",
> a _file_ is certainly not exclusively about that last component.
> 
> Hence change the behavior of '%pD' to print full path of that file.
> 
> Things become more complicated when spec.precision and spec.field_width
> is added in. string_truncate() is to handle the small space case for
> '%pD' precision and field_width.
> 
> [1] https://lore.kernel.org/lkml/CAHk-=wimsMqGdzik187YWLb-ru+iktb4MYbMQG1rnZ81dXYFVg@mail.gmail.com/
> 
> Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
> Signed-off-by: Jia He <justin.he@arm.com>
> ---
>  Documentation/core-api/printk-formats.rst |  5 ++-
>  lib/vsprintf.c                            | 47 +++++++++++++++++++++--
>  2 files changed, 46 insertions(+), 6 deletions(-)
> 
> diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst
> index f063a384c7c8..95ba14dc529b 100644
> --- a/Documentation/core-api/printk-formats.rst
> +++ b/Documentation/core-api/printk-formats.rst
> @@ -408,12 +408,13 @@ dentry names
>  ::
>  
>  	%pd{,2,3,4}
> -	%pD{,2,3,4}
> +	%pD
>  
>  For printing dentry name; if we race with :c:func:`d_move`, the name might
>  be a mix of old and new ones, but it won't oops.  %pd dentry is a safer
>  equivalent of %s dentry->d_name.name we used to use, %pd<n> prints ``n``
> -last components.  %pD does the same thing for struct file.
> +last components.  %pD prints full file path together with mount-related
> +parenthood.
>  
>  Passed by reference.
>  
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index f0c35d9b65bf..317b65280252 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -27,6 +27,7 @@
>  #include <linux/string.h>
>  #include <linux/ctype.h>
>  #include <linux/kernel.h>
> +#include <linux/dcache.h>
>  #include <linux/kallsyms.h>
>  #include <linux/math64.h>
>  #include <linux/uaccess.h>
> @@ -601,6 +602,20 @@ char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
>  }
>  
>  /* Handle string from a well known address. */
> +static char *string_truncate(char *buf, char *end, const char *s,
> +			     u32 full_len, struct printf_spec spec)
> +{
> +	int lim = 0;
> +
> +	if (buf < end) {

See below, I think the sole caller guarantees this,

> +		if (spec.precision >= 0)
> +			lim = strlen(s) - min_t(int, spec.precision, strlen(s));
> +
> +		return widen_string(buf + full_len, full_len, end - lim, spec);
> +	}
> +
> +	return buf;

which is good because this would almost certainly be wrong (violating
the "always forward buf appropriately regardless of whether you wrote
something" rule).

> +}
>  static char *string_nocheck(char *buf, char *end, const char *s,
>  			    struct printf_spec spec)
>  {
> @@ -920,13 +935,37 @@ char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp
>  }
>  
>  static noinline_for_stack
> -char *file_dentry_name(char *buf, char *end, const struct file *f,
> +char *file_d_path_name(char *buf, char *end, const struct file *f,
>  			struct printf_spec spec, const char *fmt)
>  {
> +	const struct path *path;
> +	char *p;
> +	int prepend_len, reserved_size, dpath_len;
> +
>  	if (check_pointer(&buf, end, f, spec))
>  		return buf;
>  
> -	return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
> +	path = &f->f_path;
> +	if (check_pointer(&buf, end, path, spec))
> +		return buf;
> +
> +	p = d_path_unsafe(path, buf, end - buf, &prepend_len);

If I'm reading this right, you're using buf as scratch space to write
however much of the path fits. Then [*]

> +	/* Minus 1 byte for '\0' */
> +	dpath_len = end - buf - prepend_len - 1;
> +
> +	reserved_size = max_t(int, dpath_len, spec.field_width);
> +
> +	/* no filling space at all */
> +	if (buf >= end || !buf)
> +		return buf + reserved_size;

Why the !buf check? The only way we can have that is the snprintf(NULL,
0, ...) case of asking how much space we'd need to malloc, right? In
which case end would be NULL+0 == NULL, so buf >= end automatically,
regardless of how much have been "printed" before %pD.

> +
> +	/* small space for long name */
> +	if (buf < end && prepend_len < 0)

So if we did an early return for buf >= end, we now know buf < end and
hence the first part here is redundant.

Anyway, as for [*]:

> +		return string_truncate(buf, end, p, dpath_len, spec);
> +
> +	/* space is enough */
> +	return string_nocheck(buf, end, p, spec);

Now you're passing p to string_truncate or string_nocheck, while p
points somewhere into buf itself. I can't convince myself that would be
safe. At the very least, it deserves a couple of comments.

Rasmus
Petr Mladek June 14, 2021, 3:40 p.m. UTC | #2
On Fri 2021-06-11 23:59:52, Jia He wrote:
> We have '%pD' for printing a filename. It may not be perfect (by
> default it only prints one component.)
> 
> As suggested by Linus at [1]:
> A dentry has a parent, but at the same time, a dentry really does
> inherently have "one name" (and given just the dentry pointers, you
> can't show mount-related parenthood, so in many ways the "show just
> one name" makes sense for "%pd" in ways it doesn't necessarily for
> "%pD"). But while a dentry arguably has that "one primary component",
> a _file_ is certainly not exclusively about that last component.
> 
> Hence change the behavior of '%pD' to print full path of that file.
> 
> Things become more complicated when spec.precision and spec.field_width
> is added in. string_truncate() is to handle the small space case for
> '%pD' precision and field_width.
> 
> [1] https://lore.kernel.org/lkml/CAHk-=wimsMqGdzik187YWLb-ru+iktb4MYbMQG1rnZ81dXYFVg@mail.gmail.com/
> 
> Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
> Signed-off-by: Jia He <justin.he@arm.com>
> ---
>  Documentation/core-api/printk-formats.rst |  5 ++-
>  lib/vsprintf.c                            | 47 +++++++++++++++++++++--
>  2 files changed, 46 insertions(+), 6 deletions(-)
> 
> diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst
> index f063a384c7c8..95ba14dc529b 100644
> --- a/Documentation/core-api/printk-formats.rst
> +++ b/Documentation/core-api/printk-formats.rst
> @@ -408,12 +408,13 @@ dentry names
>  ::
>  
>  	%pd{,2,3,4}
> -	%pD{,2,3,4}
> +	%pD
>  
>  For printing dentry name; if we race with :c:func:`d_move`, the name might
>  be a mix of old and new ones, but it won't oops.  %pd dentry is a safer
>  equivalent of %s dentry->d_name.name we used to use, %pd<n> prints ``n``
> -last components.  %pD does the same thing for struct file.
> +last components.  %pD prints full file path together with mount-related
> +parenthood.
>  
>  Passed by reference.
>  
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index f0c35d9b65bf..317b65280252 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -27,6 +27,7 @@
>  #include <linux/string.h>
>  #include <linux/ctype.h>
>  #include <linux/kernel.h>
> +#include <linux/dcache.h>
>  #include <linux/kallsyms.h>
>  #include <linux/math64.h>
>  #include <linux/uaccess.h>
> @@ -601,6 +602,20 @@ char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
>  }
>  
>  /* Handle string from a well known address. */

This comment is for widen_string().

string_truncate() functionality is far from obvious. It would deserve
it's own description, including description of each parammeter.

Well, do we really need it? See below.

> +static char *string_truncate(char *buf, char *end, const char *s,
> +			     u32 full_len, struct printf_spec spec)
> +{
> +	int lim = 0;
> +
> +	if (buf < end) {
> +		if (spec.precision >= 0)
> +			lim = strlen(s) - min_t(int, spec.precision, strlen(s));
> +
> +		return widen_string(buf + full_len, full_len, end - lim, spec);
> +	}
> +
> +	return buf;
> +}
>  static char *string_nocheck(char *buf, char *end, const char *s,
>  			    struct printf_spec spec)
>  {
> @@ -920,13 +935,37 @@ char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp
>  }
>  
>  static noinline_for_stack
> -char *file_dentry_name(char *buf, char *end, const struct file *f,
> +char *file_d_path_name(char *buf, char *end, const struct file *f,
>  			struct printf_spec spec, const char *fmt)
>  {
> +	const struct path *path;
> +	char *p;
> +	int prepend_len, reserved_size, dpath_len;
> +
>  	if (check_pointer(&buf, end, f, spec))
>  		return buf;
>  
> -	return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
> +	path = &f->f_path;
> +	if (check_pointer(&buf, end, path, spec))
> +		return buf;
> +
> +	p = d_path_unsafe(path, buf, end - buf, &prepend_len);
> +
> +	/* Minus 1 byte for '\0' */
> +	dpath_len = end - buf - prepend_len - 1;
> +
> +	reserved_size = max_t(int, dpath_len, spec.field_width);
> +
> +	/* no filling space at all */
> +	if (buf >= end || !buf)
> +		return buf + reserved_size;
> +
> +	/* small space for long name */
> +	if (buf < end && prepend_len < 0)
> +		return string_truncate(buf, end, p, dpath_len, spec);

We need this only because we allowed to write the path behind
spec.field_width. Do I get it right?

> +
> +	/* space is enough */
> +	return string_nocheck(buf, end, p, spec);
>  }

It easy to get lost in all the computations, including the one
in string_truncate():

	dpath_len = end - buf - prepend_len - 1;
	reserved_size = max_t(int, dpath_len, spec.field_width);
and
	lim = strlen(s) - min_t(int, spec.precision, strlen(s));
	return widen_string(buf + full_len, full_len, end - lim, spec);

Please, add comments explaining the meaning of the variables a bit.
They should help to understand why it is done this way.


I tried another approach below. The main trick is that
max_len is limited by spec.field_width and spec.precision before calling
d_path_unsave():


	if (check_pointer(&buf, end, f, spec))
		return buf;

	path = &f->f_path;
	if (check_pointer(&buf, end, path, spec))
		return buf;

	max_len = end - buf;
	if (spec.field_width >= 0 && spec.field_width < max_len)
		max_len = spec.filed_width;
	if (spec.precision >= 0 && spec.precision < max_len)
		max_len = spec.precision;

	p = d_path_unsafe(path, buf, max_len, &prepend_len);

	/*
	 * The path has been printed from the end of the buffer.
	 * Process it like a normal string to handle "precission"
	 * and "width" effects. In the "worst" case, the string
	 * will stay as is.
	 */
	if (buf < end) {
		buf = string_nocheck(buf, end, p, spec);
		/* Return buf when output was limited or did fit in. */
		if (spec.field_width >= 0 || spec.precision >= 0 ||
		    prepend_len >= 0) {
			return buf;
		}
		/* Otherwise, add what was missing. Ignore tail '\0' */
		return buf - prepend_len - 1;
	}

	/*
	 * Nothing has been written to the buffer. Just count the length.
	 * I is fixed when field_with is defined. */
	if (spec.field_width >= 0)
		return buf + spec.field_width;

	/* Otherwise, use the length of the path. */
	dpath_len = max_len - prepend_len - 1;

	/* The path might still get limited by precision number. */
	if (spec.precision >= 0 && spec.precision < dpath_len)
		return buf + spec.precision;

	return buf + dpath_len;


Note that the above code is not even compile tested. There might be
off by one mistakes. Also, it is possible that I missed something.

Best Regards,
Petr
Justin He June 15, 2021, 6:43 a.m. UTC | #3
Hi Rasmus

> -----Original Message-----
> From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> Sent: Saturday, June 12, 2021 5:28 AM
> To: Justin He <Justin.He@arm.com>; Petr Mladek <pmladek@suse.com>; Steven
> Rostedt <rostedt@goodmis.org>; Sergey Senozhatsky
> <senozhatsky@chromium.org>; Andy Shevchenko
> <andriy.shevchenko@linux.intel.com>; Jonathan Corbet <corbet@lwn.net>;
> Alexander Viro <viro@zeniv.linux.org.uk>; Linus Torvalds <torvalds@linux-
> foundation.org>
> Cc: Peter Zijlstra (Intel) <peterz@infradead.org>; Eric Biggers
> <ebiggers@google.com>; Ahmed S. Darwish <a.darwish@linutronix.de>; linux-
> doc@vger.kernel.org; linux-kernel@vger.kernel.org; linux-
> fsdevel@vger.kernel.org
> Subject: Re: [PATCH RFCv3 2/3] lib/vsprintf.c: make %pD print full path for
> file
>
> On 11/06/2021 17.59, Jia He wrote:
> > We have '%pD' for printing a filename. It may not be perfect (by
> > default it only prints one component.)
> >
> > As suggested by Linus at [1]:
> > A dentry has a parent, but at the same time, a dentry really does
> > inherently have "one name" (and given just the dentry pointers, you
> > can't show mount-related parenthood, so in many ways the "show just
> > one name" makes sense for "%pd" in ways it doesn't necessarily for
> > "%pD"). But while a dentry arguably has that "one primary component",
> > a _file_ is certainly not exclusively about that last component.
> >
> > Hence change the behavior of '%pD' to print full path of that file.
> >
> > Things become more complicated when spec.precision and spec.field_width
> > is added in. string_truncate() is to handle the small space case for
> > '%pD' precision and field_width.
> >
> > [1] https://lore.kernel.org/lkml/CAHk-=wimsMqGdzik187YWLb-
> ru+iktb4MYbMQG1rnZ81dXYFVg@mail.gmail.com/
> >
> > Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
> > Signed-off-by: Jia He <justin.he@arm.com>
> > ---
> >  Documentation/core-api/printk-formats.rst |  5 ++-
> >  lib/vsprintf.c                            | 47 +++++++++++++++++++++--
> >  2 files changed, 46 insertions(+), 6 deletions(-)
> >
> > diff --git a/Documentation/core-api/printk-formats.rst
> b/Documentation/core-api/printk-formats.rst
> > index f063a384c7c8..95ba14dc529b 100644
> > --- a/Documentation/core-api/printk-formats.rst
> > +++ b/Documentation/core-api/printk-formats.rst
> > @@ -408,12 +408,13 @@ dentry names
> >  ::
> >
> >     %pd{,2,3,4}
> > -   %pD{,2,3,4}
> > +   %pD
> >
> >  For printing dentry name; if we race with :c:func:`d_move`, the name
> might
> >  be a mix of old and new ones, but it won't oops.  %pd dentry is a safer
> >  equivalent of %s dentry->d_name.name we used to use, %pd<n> prints ``n``
> > -last components.  %pD does the same thing for struct file.
> > +last components.  %pD prints full file path together with mount-related
> > +parenthood.
> >
> >  Passed by reference.
> >
> > diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> > index f0c35d9b65bf..317b65280252 100644
> > --- a/lib/vsprintf.c
> > +++ b/lib/vsprintf.c
> > @@ -27,6 +27,7 @@
> >  #include <linux/string.h>
> >  #include <linux/ctype.h>
> >  #include <linux/kernel.h>
> > +#include <linux/dcache.h>
> >  #include <linux/kallsyms.h>
> >  #include <linux/math64.h>
> >  #include <linux/uaccess.h>
> > @@ -601,6 +602,20 @@ char *widen_string(char *buf, int n, char *end,
> struct printf_spec spec)
> >  }
> >
> >  /* Handle string from a well known address. */
> > +static char *string_truncate(char *buf, char *end, const char *s,
> > +                        u32 full_len, struct printf_spec spec)
> > +{
> > +   int lim = 0;
> > +
> > +   if (buf < end) {
>
> See below, I think the sole caller guarantees this,

Ok, will remove this check statement

>
> > +           if (spec.precision >= 0)
> > +                   lim = strlen(s) - min_t(int, spec.precision, strlen(s));
> > +
> > +           return widen_string(buf + full_len, full_len, end - lim, spec);
> > +   }
> > +
> > +   return buf;
>
> which is good because this would almost certainly be wrong (violating
> the "always forward buf appropriately regardless of whether you wrote
> something" rule).
>
> > +}
> >  static char *string_nocheck(char *buf, char *end, const char *s,
> >                         struct printf_spec spec)
> >  {
> > @@ -920,13 +935,37 @@ char *dentry_name(char *buf, char *end, const
> struct dentry *d, struct printf_sp
> >  }
> >
> >  static noinline_for_stack
> > -char *file_dentry_name(char *buf, char *end, const struct file *f,
> > +char *file_d_path_name(char *buf, char *end, const struct file *f,
> >                     struct printf_spec spec, const char *fmt)
> >  {
> > +   const struct path *path;
> > +   char *p;
> > +   int prepend_len, reserved_size, dpath_len;
> > +
> >     if (check_pointer(&buf, end, f, spec))
> >             return buf;
> >
> > -   return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
> > +   path = &f->f_path;
> > +   if (check_pointer(&buf, end, path, spec))
> > +           return buf;
> > +
> > +   p = d_path_unsafe(path, buf, end - buf, &prepend_len);
>
> If I'm reading this right, you're using buf as scratch space to write
> however much of the path fits. Then [*]
>
> > +   /* Minus 1 byte for '\0' */
> > +   dpath_len = end - buf - prepend_len - 1;
> > +
> > +   reserved_size = max_t(int, dpath_len, spec.field_width);
> > +
> > +   /* no filling space at all */
> > +   if (buf >= end || !buf)
> > +           return buf + reserved_size;
>
> Why the !buf check? The only way we can have that is the snprintf(NULL,
> 0, ...) case of asking how much space we'd need to malloc, right? In
> which case end would be NULL+0 == NULL, so buf >= end automatically,
> regardless of how much have been "printed" before %pD.

My original purpose is to avoid any memory copy/move for kvasprintf->
vsnprintf(NULL, 0,...). But as you said, this can be folded into the case
buf >= end.
Do you think whether following case should be forbidden?:
vsnprintf(NULL, 8,...).
Sorry if it is too verbose. If above invoking is valid, !buf should
still be checked.

>
> > +
> > +   /* small space for long name */
> > +   if (buf < end && prepend_len < 0)
>
> So if we did an early return for buf >= end, we now know buf < end and
> hence the first part here is redundant.
>
> Anyway, as for [*]:
>
> > +           return string_truncate(buf, end, p, dpath_len, spec);
> > +
> > +   /* space is enough */
> > +   return string_nocheck(buf, end, p, spec);
>
> Now you're passing p to string_truncate or string_nocheck, while p
> points somewhere into buf itself. I can't convince myself that would be
> safe. At the very least, it deserves a couple of comments.

When code goes here, the buffer space must be as follows:
|.........|.........|
buf       p         end

So string_nocheck is safe because essential it would byte-to-byte copy p to buf.

But I agree comments are needed here.


--
Cheers,
Justin (Jia He)


IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Justin He June 15, 2021, 6:48 a.m. UTC | #4
Hi Petr

> -----Original Message-----
> From: Petr Mladek <pmladek@suse.com>
> Sent: Monday, June 14, 2021 11:41 PM
> To: Justin He <Justin.He@arm.com>
> Cc: Steven Rostedt <rostedt@goodmis.org>; Sergey Senozhatsky
> <senozhatsky@chromium.org>; Andy Shevchenko
> <andriy.shevchenko@linux.intel.com>; Rasmus Villemoes
> <linux@rasmusvillemoes.dk>; Jonathan Corbet <corbet@lwn.net>; Alexander
> Viro <viro@zeniv.linux.org.uk>; Linus Torvalds <torvalds@linux-
> foundation.org>; Peter Zijlstra (Intel) <peterz@infradead.org>; Eric
> Biggers <ebiggers@google.com>; Ahmed S. Darwish <a.darwish@linutronix.de>;
> linux-doc@vger.kernel.org; linux-kernel@vger.kernel.org; linux-
> fsdevel@vger.kernel.org
> Subject: Re: [PATCH RFCv3 2/3] lib/vsprintf.c: make %pD print full path for
> file
>
> On Fri 2021-06-11 23:59:52, Jia He wrote:
> > We have '%pD' for printing a filename. It may not be perfect (by
> > default it only prints one component.)
> >
> > As suggested by Linus at [1]:
> > A dentry has a parent, but at the same time, a dentry really does
> > inherently have "one name" (and given just the dentry pointers, you
> > can't show mount-related parenthood, so in many ways the "show just
> > one name" makes sense for "%pd" in ways it doesn't necessarily for
> > "%pD"). But while a dentry arguably has that "one primary component",
> > a _file_ is certainly not exclusively about that last component.
> >
> > Hence change the behavior of '%pD' to print full path of that file.
> >
> > Things become more complicated when spec.precision and spec.field_width
> > is added in. string_truncate() is to handle the small space case for
> > '%pD' precision and field_width.
> >
> > [1] https://lore.kernel.org/lkml/CAHk-=wimsMqGdzik187YWLb-
> ru+iktb4MYbMQG1rnZ81dXYFVg@mail.gmail.com/
> >
> > Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
> > Signed-off-by: Jia He <justin.he@arm.com>
> > ---
> >  Documentation/core-api/printk-formats.rst |  5 ++-
> >  lib/vsprintf.c                            | 47 +++++++++++++++++++++--
> >  2 files changed, 46 insertions(+), 6 deletions(-)
> >
> > diff --git a/Documentation/core-api/printk-formats.rst
> b/Documentation/core-api/printk-formats.rst
> > index f063a384c7c8..95ba14dc529b 100644
> > --- a/Documentation/core-api/printk-formats.rst
> > +++ b/Documentation/core-api/printk-formats.rst
> > @@ -408,12 +408,13 @@ dentry names
> >  ::
> >
> >     %pd{,2,3,4}
> > -   %pD{,2,3,4}
> > +   %pD
> >
> >  For printing dentry name; if we race with :c:func:`d_move`, the name
> might
> >  be a mix of old and new ones, but it won't oops.  %pd dentry is a safer
> >  equivalent of %s dentry->d_name.name we used to use, %pd<n> prints ``n``
> > -last components.  %pD does the same thing for struct file.
> > +last components.  %pD prints full file path together with mount-related
> > +parenthood.
> >
> >  Passed by reference.
> >
> > diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> > index f0c35d9b65bf..317b65280252 100644
> > --- a/lib/vsprintf.c
> > +++ b/lib/vsprintf.c
> > @@ -27,6 +27,7 @@
> >  #include <linux/string.h>
> >  #include <linux/ctype.h>
> >  #include <linux/kernel.h>
> > +#include <linux/dcache.h>
> >  #include <linux/kallsyms.h>
> >  #include <linux/math64.h>
> >  #include <linux/uaccess.h>
> > @@ -601,6 +602,20 @@ char *widen_string(char *buf, int n, char *end,
> struct printf_spec spec)
> >  }
> >
> >  /* Handle string from a well known address. */
>
> This comment is for widen_string().
>
> string_truncate() functionality is far from obvious. It would deserve
> it's own description, including description of each parammeter.
>
> Well, do we really need it? See below.
>
> > +static char *string_truncate(char *buf, char *end, const char *s,
> > +                        u32 full_len, struct printf_spec spec)
> > +{
> > +   int lim = 0;
> > +
> > +   if (buf < end) {
> > +           if (spec.precision >= 0)
> > +                   lim = strlen(s) - min_t(int, spec.precision, strlen(s));
> > +
> > +           return widen_string(buf + full_len, full_len, end - lim, spec);
> > +   }
> > +
> > +   return buf;
> > +}
> >  static char *string_nocheck(char *buf, char *end, const char *s,
> >                         struct printf_spec spec)
> >  {
> > @@ -920,13 +935,37 @@ char *dentry_name(char *buf, char *end, const
> struct dentry *d, struct printf_sp
> >  }
> >
> >  static noinline_for_stack
> > -char *file_dentry_name(char *buf, char *end, const struct file *f,
> > +char *file_d_path_name(char *buf, char *end, const struct file *f,
> >                     struct printf_spec spec, const char *fmt)
> >  {
> > +   const struct path *path;
> > +   char *p;
> > +   int prepend_len, reserved_size, dpath_len;
> > +
> >     if (check_pointer(&buf, end, f, spec))
> >             return buf;
> >
> > -   return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
> > +   path = &f->f_path;
> > +   if (check_pointer(&buf, end, path, spec))
> > +           return buf;
> > +
> > +   p = d_path_unsafe(path, buf, end - buf, &prepend_len);
> > +
> > +   /* Minus 1 byte for '\0' */
> > +   dpath_len = end - buf - prepend_len - 1;
> > +
> > +   reserved_size = max_t(int, dpath_len, spec.field_width);
> > +
> > +   /* no filling space at all */
> > +   if (buf >= end || !buf)
> > +           return buf + reserved_size;
> > +
> > +   /* small space for long name */
> > +   if (buf < end && prepend_len < 0)
> > +           return string_truncate(buf, end, p, dpath_len, spec);
>
> We need this only because we allowed to write the path behind
> spec.field_width. Do I get it right?

Both of field_width and precision:
"%.14pD" or "%8.14pD"

>
> > +
> > +   /* space is enough */
> > +   return string_nocheck(buf, end, p, spec);
> >  }
>
> It easy to get lost in all the computations, including the one
> in string_truncate():
>
>       dpath_len = end - buf - prepend_len - 1;
>       reserved_size = max_t(int, dpath_len, spec.field_width);
> and
>       lim = strlen(s) - min_t(int, spec.precision, strlen(s));
>       return widen_string(buf + full_len, full_len, end - lim, spec);
>
> Please, add comments explaining the meaning of the variables a bit.
> They should help to understand why it is done this way.
>
Sure, sorry about that
>
> I tried another approach below. The main trick is that
> max_len is limited by spec.field_width and spec.precision before calling
> d_path_unsave():
>
>
>       if (check_pointer(&buf, end, f, spec))
>               return buf;
>
>       path = &f->f_path;
>       if (check_pointer(&buf, end, path, spec))
>               return buf;
>
>       max_len = end - buf;
>       if (spec.field_width >= 0 && spec.field_width < max_len)
>               max_len = spec.filed_width;
>       if (spec.precision >= 0 && spec.precision < max_len)
>               max_len = spec.precision;
>
>       p = d_path_unsafe(path, buf, max_len, &prepend_len);
>
>       /*
>        * The path has been printed from the end of the buffer.
>        * Process it like a normal string to handle "precission"
>        * and "width" effects. In the "worst" case, the string
>        * will stay as is.
>        */
>       if (buf < end) {
>               buf = string_nocheck(buf, end, p, spec);
>               /* Return buf when output was limited or did fit in. */
>               if (spec.field_width >= 0 || spec.precision >= 0 ||
>                   prepend_len >= 0) {
>                       return buf;
>               }
>               /* Otherwise, add what was missing. Ignore tail '\0' */
>               return buf - prepend_len - 1;
>       }
>
>       /*
>        * Nothing has been written to the buffer. Just count the length.
>        * I is fixed when field_with is defined. */
>       if (spec.field_width >= 0)
>               return buf + spec.field_width;
>
>       /* Otherwise, use the length of the path. */
>       dpath_len = max_len - prepend_len - 1;
>
>       /* The path might still get limited by precision number. */
>       if (spec.precision >= 0 && spec.precision < dpath_len)
>               return buf + spec.precision;
>
>       return buf + dpath_len;
>

Let me check it carefully, thanks for your suggestion.


--
Cheers,
Justin (Jia He)


>
> Note that the above code is not even compile tested. There might be
> off by one mistakes. Also, it is possible that I missed something.
>
> Best Regards,
> Petr
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Rasmus Villemoes June 15, 2021, 7:04 a.m. UTC | #5
On 15/06/2021 08.43, Justin He wrote:
> Hi Rasmus
> 
>> -----Original Message-----
>> From: Rasmus Villemoes <linux@rasmusvillemoes.dk>

>> Why the !buf check? The only way we can have that is the snprintf(NULL,
>> 0, ...) case of asking how much space we'd need to malloc, right? In
>> which case end would be NULL+0 == NULL, so buf >= end automatically,
>> regardless of how much have been "printed" before %pD.
> 
> My original purpose is to avoid any memory copy/move for kvasprintf->
> vsnprintf(NULL, 0,...). But as you said, this can be folded into the case
> buf >= end.
> Do you think whether following case should be forbidden?:
> vsnprintf(NULL, 8,...).

That is an obvious caller bug. The caller tells vsnprintf "here's a
buffer of size 8 at address 0x0". And checking buf for NULL in the guts
of %pD would anyway be completely pointless as it would crash for a fmt of

"x%pD"

or basically anything at all before %pD because those specifiers (or
literal parts) would cause a write to buf - and if that somehow
survived, the buf %pD would be given would now be (void*)1L.

>> Now you're passing p to string_truncate or string_nocheck, while p
>> points somewhere into buf itself. I can't convince myself that would be
>> safe. At the very least, it deserves a couple of comments.
> 
> When code goes here, the buffer space must be as follows:
> |.........|.........|
> buf       p         end
> 
> So string_nocheck is safe because essential it would byte-to-byte copy p to buf.
> 
> But I agree comments are needed here.

Yes, because no matter how string_nocheck happens to be implemented
today, some day somebody might throw in a memcpy() or do something else
that means overlapping "buf" and "s" arguments are suddenly broken in
some configurations or arches.

Rasmus
Rasmus Villemoes June 15, 2021, 7:14 a.m. UTC | #6
On 15/06/2021 08.48, Justin He wrote:
> Hi Petr
> 

>>> +   /* no filling space at all */
>>> +   if (buf >= end || !buf)
>>> +           return buf + reserved_size;
>>> +
>>> +   /* small space for long name */
>>> +   if (buf < end && prepend_len < 0)
>>> +           return string_truncate(buf, end, p, dpath_len, spec);
>>
>> We need this only because we allowed to write the path behind
>> spec.field_width. Do I get it right?
> 
> Both of field_width and precision:
> "%.14pD" or "%8.14pD"

Precision is never gonna be used with %p (or any of its kernel
extensions) because gcc would tell you

foo.c:5:13: warning: precision used with ‘%p’ gnu_printf format [-Wformat=]
    5 |  printf("%.5p\n", foo);

and there's no way -Wformat is going to be turned off to allow that usage.

IOW, there's no need to add complexity to make "%.3pD" of something that
would normally print "/foo/bar" merely print "/fo", similar to what a
precision with %s would mean.

As for field width, I don't know if it's worth honouring, but IIRC the
original %pd and %pD did that (that's where we have widen_string etc. from).

Other %p extensions put the field with to some other use (e.g. the
bitmap and hex string printing), so they obviously cannot simultaneously
use it in the traditional sense.

Rasmus
Justin He June 15, 2021, 7:18 a.m. UTC | #7
> -----Original Message-----
> From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> Sent: Tuesday, June 15, 2021 3:15 PM
> To: Justin He <Justin.He@arm.com>; Petr Mladek <pmladek@suse.com>
> Cc: Steven Rostedt <rostedt@goodmis.org>; Sergey Senozhatsky
> <senozhatsky@chromium.org>; Andy Shevchenko
> <andriy.shevchenko@linux.intel.com>; Rasmus Villemoes
> <linux@rasmusvillemoes.dk>; Jonathan Corbet <corbet@lwn.net>; Alexander
> Viro <viro@zeniv.linux.org.uk>; Linus Torvalds <torvalds@linux-
> foundation.org>; Peter Zijlstra (Intel) <peterz@infradead.org>; Eric
> Biggers <ebiggers@google.com>; Ahmed S. Darwish <a.darwish@linutronix.de>;
> linux-doc@vger.kernel.org; linux-kernel@vger.kernel.org; linux-
> fsdevel@vger.kernel.org
> Subject: Re: [PATCH RFCv3 2/3] lib/vsprintf.c: make %pD print full path for
> file
>
> On 15/06/2021 08.48, Justin He wrote:
> > Hi Petr
> >
>
> >>> +   /* no filling space at all */
> >>> +   if (buf >= end || !buf)
> >>> +           return buf + reserved_size;
> >>> +
> >>> +   /* small space for long name */
> >>> +   if (buf < end && prepend_len < 0)
> >>> +           return string_truncate(buf, end, p, dpath_len, spec);
> >>
> >> We need this only because we allowed to write the path behind
> >> spec.field_width. Do I get it right?
> >
> > Both of field_width and precision:
> > "%.14pD" or "%8.14pD"
>
> Precision is never gonna be used with %p (or any of its kernel
> extensions) because gcc would tell you
>
> foo.c:5:13: warning: precision used with �%p� gnu_printf format [-
> Wformat=]
>     5 |  printf("%.5p\n", foo);
>
> and there's no way -Wformat is going to be turned off to allow that usage.
>
> IOW, there's no need to add complexity to make "%.3pD" of something that
> would normally print "/foo/bar" merely print "/fo", similar to what a
> precision with %s would mean.
>
Aha, this answer my question in last email.
Thank you


--
Cheers,
Justin (Jia He)


> As for field width, I don't know if it's worth honouring, but IIRC the
> original %pd and %pD did that (that's where we have widen_string etc. from).
>
> Other %p extensions put the field with to some other use (e.g. the
> bitmap and hex string printing), so they obviously cannot simultaneously
> use it in the traditional sense.
>
> Rasmus
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Justin He June 15, 2021, 8:32 a.m. UTC | #8
> -----Original Message-----
> From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> Sent: Saturday, June 12, 2021 5:28 AM
> To: Justin He <Justin.He@arm.com>; Petr Mladek <pmladek@suse.com>; Steven
> Rostedt <rostedt@goodmis.org>; Sergey Senozhatsky
> <senozhatsky@chromium.org>; Andy Shevchenko
> <andriy.shevchenko@linux.intel.com>; Jonathan Corbet <corbet@lwn.net>;
> Alexander Viro <viro@zeniv.linux.org.uk>; Linus Torvalds <torvalds@linux-
> foundation.org>
> Cc: Peter Zijlstra (Intel) <peterz@infradead.org>; Eric Biggers
> <ebiggers@google.com>; Ahmed S. Darwish <a.darwish@linutronix.de>; linux-
> doc@vger.kernel.org; linux-kernel@vger.kernel.org; linux-
> fsdevel@vger.kernel.org
> Subject: Re: [PATCH RFCv3 2/3] lib/vsprintf.c: make %pD print full path for
> file
>
> On 11/06/2021 17.59, Jia He wrote:
> > We have '%pD' for printing a filename. It may not be perfect (by
> > default it only prints one component.)
> >
> > As suggested by Linus at [1]:
> > A dentry has a parent, but at the same time, a dentry really does
> > inherently have "one name" (and given just the dentry pointers, you
> > can't show mount-related parenthood, so in many ways the "show just
> > one name" makes sense for "%pd" in ways it doesn't necessarily for
> > "%pD"). But while a dentry arguably has that "one primary component",
> > a _file_ is certainly not exclusively about that last component.
> >
> > Hence change the behavior of '%pD' to print full path of that file.
> >
> > Things become more complicated when spec.precision and spec.field_width
> > is added in. string_truncate() is to handle the small space case for
> > '%pD' precision and field_width.
> >
> > [1] https://lore.kernel.org/lkml/CAHk-=wimsMqGdzik187YWLb-
> ru+iktb4MYbMQG1rnZ81dXYFVg@mail.gmail.com/
> >
> > Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
> > Signed-off-by: Jia He <justin.he@arm.com>
> > ---
> >  Documentation/core-api/printk-formats.rst |  5 ++-
> >  lib/vsprintf.c                            | 47 +++++++++++++++++++++--
> >  2 files changed, 46 insertions(+), 6 deletions(-)
> >
> > diff --git a/Documentation/core-api/printk-formats.rst
> b/Documentation/core-api/printk-formats.rst
> > index f063a384c7c8..95ba14dc529b 100644
> > --- a/Documentation/core-api/printk-formats.rst
> > +++ b/Documentation/core-api/printk-formats.rst
> > @@ -408,12 +408,13 @@ dentry names
> >  ::
> >
> >     %pd{,2,3,4}
> > -   %pD{,2,3,4}
> > +   %pD
> >
> >  For printing dentry name; if we race with :c:func:`d_move`, the name
> might
> >  be a mix of old and new ones, but it won't oops.  %pd dentry is a safer
> >  equivalent of %s dentry->d_name.name we used to use, %pd<n> prints ``n``
> > -last components.  %pD does the same thing for struct file.
> > +last components.  %pD prints full file path together with mount-related
> > +parenthood.
> >
> >  Passed by reference.
> >
> > diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> > index f0c35d9b65bf..317b65280252 100644
> > --- a/lib/vsprintf.c
> > +++ b/lib/vsprintf.c
> > @@ -27,6 +27,7 @@
> >  #include <linux/string.h>
> >  #include <linux/ctype.h>
> >  #include <linux/kernel.h>
> > +#include <linux/dcache.h>
> >  #include <linux/kallsyms.h>
> >  #include <linux/math64.h>
> >  #include <linux/uaccess.h>
> > @@ -601,6 +602,20 @@ char *widen_string(char *buf, int n, char *end,
> struct printf_spec spec)
> >  }
> >
> >  /* Handle string from a well known address. */
> > +static char *string_truncate(char *buf, char *end, const char *s,
> > +                        u32 full_len, struct printf_spec spec)
> > +{
> > +   int lim = 0;
> > +
> > +   if (buf < end) {
>
> See below, I think the sole caller guarantees this,
>
> > +           if (spec.precision >= 0)
> > +                   lim = strlen(s) - min_t(int, spec.precision, strlen(s));
> > +
> > +           return widen_string(buf + full_len, full_len, end - lim, spec);
> > +   }
> > +
> > +   return buf;
>
> which is good because this would almost certainly be wrong (violating
> the "always forward buf appropriately regardless of whether you wrote
> something" rule).

Sorry, I don't quite understand why it violates the rules here.

After removing the precision consideration, the codes should look like:
static char *string_truncate(char *buf, char *end, const char *s,
                                    u32 full_len, struct printf_spec spec)
{
        return widen_string(buf + full_len, full_len, end, spec);
}

Please note that in the case of small space with long string name,
The _buf_ had been filled with full path name:
e.g."/dev/testfile"
But the string might be truncated by the small space size.
e.g. "/dev/testf"
So we can't use the original string_nocheck here

Actually it doesn't backward buf here

--
Cheers,
Justin (Jia He)



>
> > +}
> >  static char *string_nocheck(char *buf, char *end, const char *s,
> >                         struct printf_spec spec)
> >  {
> > @@ -920,13 +935,37 @@ char *dentry_name(char *buf, char *end, const
> struct dentry *d, struct printf_sp
> >  }
> >
> >  static noinline_for_stack
> > -char *file_dentry_name(char *buf, char *end, const struct file *f,
> > +char *file_d_path_name(char *buf, char *end, const struct file *f,
> >                     struct printf_spec spec, const char *fmt)
> >  {
> > +   const struct path *path;
> > +   char *p;
> > +   int prepend_len, reserved_size, dpath_len;
> > +
> >     if (check_pointer(&buf, end, f, spec))
> >             return buf;
> >
> > -   return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
> > +   path = &f->f_path;
> > +   if (check_pointer(&buf, end, path, spec))
> > +           return buf;
> > +
> > +   p = d_path_unsafe(path, buf, end - buf, &prepend_len);
>
> If I'm reading this right, you're using buf as scratch space to write
> however much of the path fits. Then [*]
>
> > +   /* Minus 1 byte for '\0' */
> > +   dpath_len = end - buf - prepend_len - 1;
> > +
> > +   reserved_size = max_t(int, dpath_len, spec.field_width);
> > +
> > +   /* no filling space at all */
> > +   if (buf >= end || !buf)
> > +           return buf + reserved_size;
>
> Why the !buf check? The only way we can have that is the snprintf(NULL,
> 0, ...) case of asking how much space we'd need to malloc, right? In
> which case end would be NULL+0 == NULL, so buf >= end automatically,
> regardless of how much have been "printed" before %pD.
>
> > +
> > +   /* small space for long name */
> > +   if (buf < end && prepend_len < 0)
>
> So if we did an early return for buf >= end, we now know buf < end and
> hence the first part here is redundant.
>
> Anyway, as for [*]:
>
> > +           return string_truncate(buf, end, p, dpath_len, spec);
> > +
> > +   /* space is enough */
> > +   return string_nocheck(buf, end, p, spec);
>
> Now you're passing p to string_truncate or string_nocheck, while p
> points somewhere into buf itself. I can't convince myself that would be
> safe. At the very least, it deserves a couple of comments.
>
> Rasmus
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Justin He June 15, 2021, 2:48 p.m. UTC | #9
Hi Petr

> -----Original Message-----
> From: Petr Mladek <pmladek@suse.com>
> Sent: Monday, June 14, 2021 11:41 PM
> To: Justin He <Justin.He@arm.com>
> Cc: Steven Rostedt <rostedt@goodmis.org>; Sergey Senozhatsky
> <senozhatsky@chromium.org>; Andy Shevchenko
> <andriy.shevchenko@linux.intel.com>; Rasmus Villemoes
> <linux@rasmusvillemoes.dk>; Jonathan Corbet <corbet@lwn.net>; Alexander
> Viro <viro@zeniv.linux.org.uk>; Linus Torvalds <torvalds@linux-
> foundation.org>; Peter Zijlstra (Intel) <peterz@infradead.org>; Eric
> Biggers <ebiggers@google.com>; Ahmed S. Darwish <a.darwish@linutronix.de>;
> linux-doc@vger.kernel.org; linux-kernel@vger.kernel.org; linux-
> fsdevel@vger.kernel.org
> Subject: Re: [PATCH RFCv3 2/3] lib/vsprintf.c: make %pD print full path
> for file
> 
> On Fri 2021-06-11 23:59:52, Jia He wrote:
> > We have '%pD' for printing a filename. It may not be perfect (by
> > default it only prints one component.)
> >
> > As suggested by Linus at [1]:
> > A dentry has a parent, but at the same time, a dentry really does
> > inherently have "one name" (and given just the dentry pointers, you
> > can't show mount-related parenthood, so in many ways the "show just
> > one name" makes sense for "%pd" in ways it doesn't necessarily for
> > "%pD"). But while a dentry arguably has that "one primary component",
> > a _file_ is certainly not exclusively about that last component.
> >
> > Hence change the behavior of '%pD' to print full path of that file.
> >
> > Things become more complicated when spec.precision and spec.field_width
> > is added in. string_truncate() is to handle the small space case for
> > '%pD' precision and field_width.
> >
> > [1] https://lore.kernel.org/lkml/CAHk-=wimsMqGdzik187YWLb-
> ru+iktb4MYbMQG1rnZ81dXYFVg@mail.gmail.com/
> >
> > Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
> > Signed-off-by: Jia He <justin.he@arm.com>
> > ---
> >  Documentation/core-api/printk-formats.rst |  5 ++-
> >  lib/vsprintf.c                            | 47 +++++++++++++++++++++--
> >  2 files changed, 46 insertions(+), 6 deletions(-)
> >
> > diff --git a/Documentation/core-api/printk-formats.rst
> b/Documentation/core-api/printk-formats.rst
> > index f063a384c7c8..95ba14dc529b 100644
> > --- a/Documentation/core-api/printk-formats.rst
> > +++ b/Documentation/core-api/printk-formats.rst
> > @@ -408,12 +408,13 @@ dentry names
> >  ::
> >
> >  	%pd{,2,3,4}
> > -	%pD{,2,3,4}
> > +	%pD
> >
> >  For printing dentry name; if we race with :c:func:`d_move`, the name
> might
> >  be a mix of old and new ones, but it won't oops.  %pd dentry is a safer
> >  equivalent of %s dentry->d_name.name we used to use, %pd<n> prints
> ``n``
> > -last components.  %pD does the same thing for struct file.
> > +last components.  %pD prints full file path together with mount-related
> > +parenthood.
> >
> >  Passed by reference.
> >
> > diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> > index f0c35d9b65bf..317b65280252 100644
> > --- a/lib/vsprintf.c
> > +++ b/lib/vsprintf.c
> > @@ -27,6 +27,7 @@
> >  #include <linux/string.h>
> >  #include <linux/ctype.h>
> >  #include <linux/kernel.h>
> > +#include <linux/dcache.h>
> >  #include <linux/kallsyms.h>
> >  #include <linux/math64.h>
> >  #include <linux/uaccess.h>
> > @@ -601,6 +602,20 @@ char *widen_string(char *buf, int n, char *end,
> struct printf_spec spec)
> >  }
> >
> >  /* Handle string from a well known address. */
> 
> This comment is for widen_string().
> 
> string_truncate() functionality is far from obvious. It would deserve
> it's own description, including description of each parammeter.
> 
> Well, do we really need it? See below.
> 
> > +static char *string_truncate(char *buf, char *end, const char *s,
> > +			     u32 full_len, struct printf_spec spec)
> > +{
> > +	int lim = 0;
> > +
> > +	if (buf < end) {
> > +		if (spec.precision >= 0)
> > +			lim = strlen(s) - min_t(int, spec.precision,
> strlen(s));
> > +
> > +		return widen_string(buf + full_len, full_len, end - lim,
> spec);
> > +	}
> > +
> > +	return buf;
> > +}
> >  static char *string_nocheck(char *buf, char *end, const char *s,
> >  			    struct printf_spec spec)
> >  {
> > @@ -920,13 +935,37 @@ char *dentry_name(char *buf, char *end, const
> struct dentry *d, struct printf_sp
> >  }
> >
> >  static noinline_for_stack
> > -char *file_dentry_name(char *buf, char *end, const struct file *f,
> > +char *file_d_path_name(char *buf, char *end, const struct file *f,
> >  			struct printf_spec spec, const char *fmt)
> >  {
> > +	const struct path *path;
> > +	char *p;
> > +	int prepend_len, reserved_size, dpath_len;
> > +
> >  	if (check_pointer(&buf, end, f, spec))
> >  		return buf;
> >
> > -	return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
> > +	path = &f->f_path;
> > +	if (check_pointer(&buf, end, path, spec))
> > +		return buf;
> > +
> > +	p = d_path_unsafe(path, buf, end - buf, &prepend_len);
> > +
> > +	/* Minus 1 byte for '\0' */
> > +	dpath_len = end - buf - prepend_len - 1;
> > +
> > +	reserved_size = max_t(int, dpath_len, spec.field_width);
> > +
> > +	/* no filling space at all */
> > +	if (buf >= end || !buf)
> > +		return buf + reserved_size;
> > +
> > +	/* small space for long name */
> > +	if (buf < end && prepend_len < 0)
> > +		return string_truncate(buf, end, p, dpath_len, spec);
> 
> We need this only because we allowed to write the path behind
> spec.field_width. Do I get it right?
> 
> > +
> > +	/* space is enough */
> > +	return string_nocheck(buf, end, p, spec);
> >  }
> 
> It easy to get lost in all the computations, including the one
> in string_truncate():
> 
> 	dpath_len = end - buf - prepend_len - 1;
> 	reserved_size = max_t(int, dpath_len, spec.field_width);
> and
> 	lim = strlen(s) - min_t(int, spec.precision, strlen(s));
> 	return widen_string(buf + full_len, full_len, end - lim, spec);
> 
> Please, add comments explaining the meaning of the variables a bit.
> They should help to understand why it is done this way.
> 
> 
> I tried another approach below. The main trick is that
> max_len is limited by spec.field_width and spec.precision before calling
> d_path_unsave():
> 
> 
> 	if (check_pointer(&buf, end, f, spec))
> 		return buf;
> 
> 	path = &f->f_path;
> 	if (check_pointer(&buf, end, path, spec))
> 		return buf;
> 
> 	max_len = end - buf;
> 	if (spec.field_width >= 0 && spec.field_width < max_len)
> 		max_len = spec.filed_width;
> 	if (spec.precision >= 0 && spec.precision < max_len)
> 		max_len = spec.precision;
> 
> 	p = d_path_unsafe(path, buf, max_len, &prepend_len);
> 
> 	/*
> 	 * The path has been printed from the end of the buffer.
> 	 * Process it like a normal string to handle "precission"
> 	 * and "width" effects. In the "worst" case, the string
> 	 * will stay as is.
> 	 */
> 	if (buf < end) {
> 		buf = string_nocheck(buf, end, p, spec);
> 		/* Return buf when output was limited or did fit in. */
> 		if (spec.field_width >= 0 || spec.precision >= 0 ||
> 		    prepend_len >= 0) {
> 			return buf;
> 		}
> 		/* Otherwise, add what was missing. Ignore tail '\0' */
> 		return buf - prepend_len - 1;
> 	}
> 
> 	/*
> 	 * Nothing has been written to the buffer. Just count the length.
> 	 * I is fixed when field_with is defined. */
> 	if (spec.field_width >= 0)
> 		return buf + spec.field_width;
> 
> 	/* Otherwise, use the length of the path. */
> 	dpath_len = max_len - prepend_len - 1;
> 
> 	/* The path might still get limited by precision number. */
> 	if (spec.precision >= 0 && spec.precision < dpath_len)
> 		return buf + spec.precision;
> 
> 	return buf + dpath_len;

As Rasmus confirmed that we needn't consider the spec.precision,
the code can be more concise.
I will send out v4 after testing together with one test_printf patch
from Rasmus.


--
Cheers,
Justin (Jia He)


> 
> 
> Note that the above code is not even compile tested. There might be
> off by one mistakes. Also, it is possible that I missed something.
> 
> Best Regards,
> Petr
diff mbox series

Patch

diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst
index f063a384c7c8..95ba14dc529b 100644
--- a/Documentation/core-api/printk-formats.rst
+++ b/Documentation/core-api/printk-formats.rst
@@ -408,12 +408,13 @@  dentry names
 ::
 
 	%pd{,2,3,4}
-	%pD{,2,3,4}
+	%pD
 
 For printing dentry name; if we race with :c:func:`d_move`, the name might
 be a mix of old and new ones, but it won't oops.  %pd dentry is a safer
 equivalent of %s dentry->d_name.name we used to use, %pd<n> prints ``n``
-last components.  %pD does the same thing for struct file.
+last components.  %pD prints full file path together with mount-related
+parenthood.
 
 Passed by reference.
 
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index f0c35d9b65bf..317b65280252 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -27,6 +27,7 @@ 
 #include <linux/string.h>
 #include <linux/ctype.h>
 #include <linux/kernel.h>
+#include <linux/dcache.h>
 #include <linux/kallsyms.h>
 #include <linux/math64.h>
 #include <linux/uaccess.h>
@@ -601,6 +602,20 @@  char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
 }
 
 /* Handle string from a well known address. */
+static char *string_truncate(char *buf, char *end, const char *s,
+			     u32 full_len, struct printf_spec spec)
+{
+	int lim = 0;
+
+	if (buf < end) {
+		if (spec.precision >= 0)
+			lim = strlen(s) - min_t(int, spec.precision, strlen(s));
+
+		return widen_string(buf + full_len, full_len, end - lim, spec);
+	}
+
+	return buf;
+}
 static char *string_nocheck(char *buf, char *end, const char *s,
 			    struct printf_spec spec)
 {
@@ -920,13 +935,37 @@  char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp
 }
 
 static noinline_for_stack
-char *file_dentry_name(char *buf, char *end, const struct file *f,
+char *file_d_path_name(char *buf, char *end, const struct file *f,
 			struct printf_spec spec, const char *fmt)
 {
+	const struct path *path;
+	char *p;
+	int prepend_len, reserved_size, dpath_len;
+
 	if (check_pointer(&buf, end, f, spec))
 		return buf;
 
-	return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
+	path = &f->f_path;
+	if (check_pointer(&buf, end, path, spec))
+		return buf;
+
+	p = d_path_unsafe(path, buf, end - buf, &prepend_len);
+
+	/* Minus 1 byte for '\0' */
+	dpath_len = end - buf - prepend_len - 1;
+
+	reserved_size = max_t(int, dpath_len, spec.field_width);
+
+	/* no filling space at all */
+	if (buf >= end || !buf)
+		return buf + reserved_size;
+
+	/* small space for long name */
+	if (buf < end && prepend_len < 0)
+		return string_truncate(buf, end, p, dpath_len, spec);
+
+	/* space is enough */
+	return string_nocheck(buf, end, p, spec);
 }
 #ifdef CONFIG_BLOCK
 static noinline_for_stack
@@ -2296,7 +2335,7 @@  early_param("no_hash_pointers", no_hash_pointers_enable);
  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
  *           (default assumed to be phys_addr_t, passed by reference)
  * - 'd[234]' For a dentry name (optionally 2-4 last components)
- * - 'D[234]' Same as 'd' but for a struct file
+ * - 'D' For full path name of a struct file
  * - 'g' For block_device name (gendisk + partition number)
  * - 't[RT][dt][r]' For time and date as represented by:
  *      R    struct rtc_time
@@ -2395,7 +2434,7 @@  char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 	case 'C':
 		return clock(buf, end, ptr, spec, fmt);
 	case 'D':
-		return file_dentry_name(buf, end, ptr, spec, fmt);
+		return file_d_path_name(buf, end, ptr, spec, fmt);
 #ifdef CONFIG_BLOCK
 	case 'g':
 		return bdev_name(buf, end, ptr, spec, fmt);