Skip to contentSkip to conversation

make '%pD' print full path for file

RFC PATCH Superseded Jia He 4 participants 22 messages Jun 11, 2021 – Jun 15, 2021

Subsystems (4)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Jia He

author
#0 lore raw

v3 cover make '%pD' print full path for file

Background
==========
Linus suggested printing full path for file instead of printing the components as '%pd'.

Typically, there is no need for printk specifiers to take any real locks (ie mount_lock or rename_lock). So I introduce a new helper d_path_fast which is similar to d_path except it doesn't take any seqlock/spinlock.

This series is based on Al Viro's d_path cleanup patches [1] which lifted the inner lockless loop into a new helper.

[1] https://lkml.org/lkml/2021/5/18/1260

Test
====
The cases I tested:
1. print '%pD' with full path of ext4 file
2. mount a ext4 filesystem upon a ext4 filesystem, and print the file with '%pD'
5. all test_print selftests, including the new '%14pD' '%-14pD'
4. kasnprintf

After this set, I found many lines containing '%pD[234]' should be changed to '%pD'. I don't want to involve those subsystems in this patch series before the helper is stable enough.

You can get the lines by:
$find fs/ -name \*.[ch] | xargs grep -rn "\%pD[234"

fs/overlayfs/file.c:65: pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",

fs/nfs/direct.c:453:    dfprintk(FILE, "NFS: direct read(%pD2, %zd@%Ld)\n",
fs/nfs/direct.c:908:    dfprintk(FILE, "NFS: direct write(%pD2, %zd@%Ld)\n",
fs/nfs/write.c:1371:    dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
fs/nfs/nfs4file.c:116:  dprintk("NFS: flush(%pD2)\n", file);
fs/nfs/file.c:69:       dprintk("NFS: open file(%pD2)\n", filp);
fs/nfs/file.c:83:       dprintk("NFS: release(%pD2)\n", filp);
fs/nfs/file.c:117:      dprintk("NFS: llseek file(%pD2, %lld, %d)\n",
fs/nfs/file.c:145:      dprintk("NFS: flush(%pD2)\n", file);
fs/nfs/file.c:166:      dprintk("NFS: read(%pD2, %zu@%lu)\n",
fs/nfs/file.c:188:      dprintk("NFS: mmap(%pD2)\n", file);
fs/nfs/file.c:213:      dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
fs/nfs/file.c:328:      dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
fs/nfs/file.c:360:      dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
fs/nfs/file.c:551:      dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
fs/nfs/file.c:621:      dprintk("NFS: write(%pD2, %zu@%Ld)\n",
fs/nfs/file.c:803:      dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
fs/nfs/file.c:841:      dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
fs/nfs/dir.c:111:       dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
fs/nfs/dir.c:456:                                               pr_notice("NFS: directory %pD2 contains a readdir loop."
fs/nfs/dir.c:1084:      dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
fs/nfs/dir.c:1158:      dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
fs/nfs/dir.c:1166:      dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
fs/nfs/dir.c:1208:      dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);

fs/nfsd/nfs4state.c:2439: seq_printf(s, "filename: \"%pD2\"", f->nf_file);
fs/exec.c:817: pr_warn_once("process '%pD4' started with executable stack\n",
fs/iomap/direct-io.c:429: pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s\n",
fs/ioctl.c:81: pr_warn_ratelimited("[%s/%d] FS: %s File: %pD4 would truncate fibmap result\n",
fs/read_write.c:425: "kernel %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
fs/splice.c:754: "splice %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
fs/afs/mntpt.c:64: _enter("%p,%p{%pD2}", inode, file, file);

Changelog:
v3:
- implement new d_path_unsafe to use [buf, end] instead of stack space for filling bytes (by Matthew)
- add new test cases for '%pD'
- drop patch "hmcdrv: remove the redundant directory path" before removing rfc.

v2:
- implement new d_path_fast based on Al Viro's patches
- add check_pointer check (by Petr)
- change the max full path size to 256 in stack space
v1: https://lkml.org/lkml/2021/5/8/122

Jia He (3):
fs: introduce helper d_path_unsafe() lib/vsprintf.c: make %pD print full path for file lib/test_printf: add test cases for '%pD'

 Documentation/core-api/printk-formats.rst |  5 +-
 fs/d_path.c                               | 82 ++++++++++++++++++++++-
 include/linux/dcache.h                    |  1 +
 lib/test_printf.c                         | 26 ++++++-
 lib/vsprintf.c                            | 47 +++++++++++--
 5 files changed, 152 insertions(+), 9 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

RFC PATCH v3 1/3 fs: introduce helper d_path_unsafe()

This helper is similar to d_path except that it doesn't take any seqlock/spinlock. It is typical for debugging purpose. Besides, an additional return value *prenpend_len* is used to get the full path length of the dentry.

prepend_name_with_len() enhances the behavior of prepend_name(). Previously it will skip the loop at once in __prepen_path() when the space is not enough. __prepend_path() gets the full length of dentry together with the parent recusively.

Besides, if someone invokes snprintf with small but positive space, prepend_name_with() needs to move and copy the string partially.

More than that, kasnprintf will pass NULL _buf_ and _end_, hence it returns at the very beginning with false in this case;

---
 fs/d_path.c            | 83 +++++++++++++++++++++++++++++++++++++++++-
 include/linux/dcache.h |  1 +
 2 files changed, 82 insertions(+), 2 deletions(-)
fs/d_path.c +81 -2
index 23a53f7b5c71..4fc224eadf58 100644
@@ -68,9 +68,66 @@ static bool prepend_name(struct prepend_buffer *p, const struct qstr *name)
 	return true;
 }
 
+static bool prepend_name_with_len(struct prepend_buffer *p, const struct qstr *name,
+			 int orig_buflen)
+{
+	const char *dname = smp_load_acquire(&name->name); /* ^^^ */
+	int dlen = READ_ONCE(name->len);
+	char *s;
+	int last_len = p->len;
+
+	p->len -= dlen + 1;
+
+	if (unlikely(!p->buf))
+		return false;
+
+	if (orig_buflen <= 0)
+		return false;
+	/*
+	 * The first time we overflow the buffer. Then fill the string
+	 * partially from the beginning
+	 */
+	if (unlikely(p->len < 0)) {
+		int buflen = strlen(p->buf);
+
+		s = p->buf;
+
+		/* Still have small space to fill partially */
+		if (last_len > 0) {
+			p->buf -= last_len;
+			buflen += last_len;
+		}
+
+		if (buflen > dlen + 1) {
+			/* This dentry name can be fully filled */
+			memmove(p->buf + dlen + 1, s, buflen - dlen - 1);
+			p->buf[0] = '/';
+			memcpy(p->buf + 1, dname, dlen);
+		} else if (buflen > 0) {
+			/* Partially filled, and drop last dentry name */
+			p->buf[0] = '/';
+			memcpy(p->buf + 1, dname, buflen - 1);
+		}
+
+		return false;
+	}
+
+	s = p->buf -= dlen + 1;
+	*s++ = '/';
+	while (dlen--) {
+		char c = *dname++;
+
+		if (!c)
+			break;
+		*s++ = c;
+	}
+	return true;
+}
 static int __prepend_path(const struct dentry *dentry, const struct mount *mnt,
 			  const struct path *root, struct prepend_buffer *p)
 {
+	int orig_buflen = p->len;
+
 	while (dentry != root->dentry || &mnt->mnt != root->mnt) {
 		const struct dentry *parent = READ_ONCE(dentry->d_parent);
 
@@ -97,8 +154,7 @@ static int __prepend_path(const struct dentry *dentry, const struct mount *mnt,
 			return 3;
 
 		prefetch(parent);
-		if (!prepend_name(p, &dentry->d_name))
-			break;
+		prepend_name_with_len(p, &dentry->d_name, orig_buflen);
 		dentry = parent;
 	}
 	return 0;
@@ -263,6 +319,29 @@ char *d_path(const struct path *path, char *buf, int buflen)
 }
 EXPORT_SYMBOL(d_path);
 
+/**
+ * d_path_unsafe - fast return the full path of a dentry without taking
+ * any seqlock/spinlock. This helper is typical for debugging purpose.
+ */
+char *d_path_unsafe(const struct path *path, char *buf, int buflen,
+		    int *prepend_len)
+{
+	struct path root;
+	struct mount *mnt = real_mount(path->mnt);
+	DECLARE_BUFFER(b, buf, buflen);
+
+	rcu_read_lock();
+	get_fs_root_rcu(current->fs, &root);
+
+	prepend(&b, "", 1);
+	__prepend_path(path->dentry, mnt, &root, &b);
+	rcu_read_unlock();
+
+	*prepend_len = b.len;
+
+	return b.buf;
+}
+
 /*
  * Helper function for dentry_operations.d_dname() members
  */
include/linux/dcache.h +1 -0
index 9e23d33bb6f1..ec118b684055 100644
@@ -301,6 +301,7 @@ char *dynamic_dname(struct dentry *, char *, int, const char *, ...);
 extern char *__d_path(const struct path *, const struct path *, char *, int);
 extern char *d_absolute_path(const struct path *, char *, int);
 extern char *d_path(const struct path *, char *, int);
+extern char *d_path_unsafe(const struct path *, char *, int, int*);
 extern char *dentry_path_raw(const struct dentry *, char *, int);
 extern char *dentry_path(const struct dentry *, char *, int);
 
Signature · 1 line
2.17.1
author
#2 lore raw

RFC PATCH v3 2/3 lib/vsprintf.c: make %pD print full path for file

In reply to Jia He

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/

---
 Documentation/core-api/printk-formats.rst |  5 ++-
 lib/vsprintf.c                            | 47 +++++++++++++++++++++--
 2 files changed, 46 insertions(+), 6 deletions(-)
Documentation/core-api/printk-formats.rst +3 -2
index f063a384c7c8..95ba14dc529b 100644
@@ -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.
 
lib/vsprintf.c +43 -4

82 lines not on this page · raw message

Signature · 1 line
2.17.1
#3 lore raw

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

Quoted 59 lines · Jia He

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/

---
 Documentation/core-api/printk-formats.rst |  5 ++-
 lib/vsprintf.c                            | 47 +++++++++++++++++++++--
 2 files changed, 46 insertions(+), 6 deletions(-)
Documentation/core-api/printk-formats.rst +3 -2
index f063a384c7c8..95ba14dc529b 100644
@@ -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.
 
lib/vsprintf.c +1 -0
index f0c35d9b65bf..317b65280252 100644
@@ -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>

lib/vsprintf.c char *widen_string(char *buf, int n, char *end, struct printf_spec spec) · line 610

Quoted 9 lines
lib/vsprintf.c +6 -0
@@ -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,

lib/vsprintf.c char *widen_string(char *buf, int n, char *end, struct printf_spec spec) · line 617

Quoted 7 lines
lib/vsprintf.c +7 -0
+		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).

Quoted 15 lines
lib/vsprintf.c +5 -1
+}
 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;

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 952

Quoted 10 lines
lib/vsprintf.c +6 -1
+
 	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 [*]

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 961

Quoted 8 lines
lib/vsprintf.c +8 -0
+	/* 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.

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 964

Quoted 3 lines
lib/vsprintf.c +3 -0
+
+	/* 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 [*]:

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 968

Quoted 4 lines
lib/vsprintf.c +4 -0
+		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

author
#4 lore raw

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

Hi Rasmus

Quoted 82 lines

-----Original Message-----
From: Rasmus Villemoes <[email protected]>
Sent: Saturday, June 12, 2021 5:28 AM
To: Justin He <[email protected]>; Petr Mladek <[email protected]>; Steven Rostedt <[email protected]>; Sergey Senozhatsky <[email protected]>; Andy Shevchenko <[email protected]>; Jonathan Corbet <[email protected]>; Alexander Viro <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>
Cc: Peter Zijlstra (Intel) <[email protected]>; Eric Biggers <[email protected]>; Ahmed S. Darwish <[email protected]>; linux- [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH RFCv3 2/3] lib/vsprintf.c: make %pD print full path for file

Quoted 11 lines · Jia He

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.

Quoted 7 lines

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-

[email protected]/

Review

Quoted 9 lines
---
 Documentation/core-api/printk-formats.rst |  5 ++-
 lib/vsprintf.c                            | 47 +++++++++++++++++++++--
 2 files changed, 46 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/Documentation/core-api/printk-formats.rst

Documentation/core-api/printk-formats.rst dentry names · line 413

Quoted 11 lines

index f063a384c7c8..95ba14dc529b 100644

Documentation/core-api/printk-formats.rst +1 -1
@@ -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

lib/vsprintf.c char *widen_string(char *buf, int n, char *end,

Quoted 21 lines
Documentation/core-api/printk-formats.rst +2 -1
 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.
 
lib/vsprintf.c +1 -0
index f0c35d9b65bf..317b65280252 100644
@@ -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)

Quoted 2 lines
lib/vsprintf.c +0 -0
 }
 
Quoted 9 lines

lib/vsprintf.c char *widen_string(char *buf, int n, char *end, struct printf_spec spec) · line 610

Quoted 7 lines
lib/vsprintf.c +6 -0
 /* 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

Quoted 43 lines

lib/vsprintf.c char *widen_string(char *buf, int n, char *end, struct printf_spec spec) · line 617

Quoted 8 lines
lib/vsprintf.c +7 -0
+           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).

lib/vsprintf.c char *dentry_name(char *buf, char *end, const

Quoted 5 lines
lib/vsprintf.c +1 -0
+}
 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

Quoted 10 lines
lib/vsprintf.c +4 -1
 }
 
 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;

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 952

Quoted 10 lines
lib/vsprintf.c +6 -1
+
    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 [*]

Quoted 13 lines

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 961

Quoted 8 lines
lib/vsprintf.c +8 -0
+   /* 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.

Quoted 10 lines

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 964

Quoted 4 lines
lib/vsprintf.c +3 -0
+
+   /* 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 [*]:

Quoted 8 lines

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 968

Quoted 4 lines
lib/vsprintf.c +4 -0
+           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.

#5 lore raw

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

Quoted 10 lines · Justin He

Hi Rasmus

Quoted 7 lines

-----Original Message-----
From: Rasmus Villemoes <[email protected]>

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.

Quoted 5 lines

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.

Quoted 11 lines
Quoted 3 lines

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

author
#6 lore raw

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

In reply to Rasmus Villemoes
Quoted 94 lines

-----Original Message-----
From: Rasmus Villemoes <[email protected]>
Sent: Saturday, June 12, 2021 5:28 AM
To: Justin He <[email protected]>; Petr Mladek <[email protected]>; Steven Rostedt <[email protected]>; Sergey Senozhatsky <[email protected]>; Andy Shevchenko <[email protected]>; Jonathan Corbet <[email protected]>; Alexander Viro <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>
Cc: Peter Zijlstra (Intel) <[email protected]>; Eric Biggers <[email protected]>; Ahmed S. Darwish <[email protected]>; linux- [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH RFCv3 2/3] lib/vsprintf.c: make %pD print full path for file

Quoted 11 lines · Jia He

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.

Quoted 7 lines

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-

[email protected]/

Review

Quoted 9 lines
---
 Documentation/core-api/printk-formats.rst |  5 ++-
 lib/vsprintf.c                            | 47 +++++++++++++++++++++--
 2 files changed, 46 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/Documentation/core-api/printk-formats.rst

Documentation/core-api/printk-formats.rst dentry names · line 413

Quoted 11 lines

index f063a384c7c8..95ba14dc529b 100644

Documentation/core-api/printk-formats.rst +1 -1
@@ -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

lib/vsprintf.c char *widen_string(char *buf, int n, char *end,

Quoted 21 lines
Documentation/core-api/printk-formats.rst +2 -1
 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.
 
lib/vsprintf.c +1 -0
index f0c35d9b65bf..317b65280252 100644
@@ -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)

lib/vsprintf.c char *widen_string(char *buf, int n, char *end, struct printf_spec spec) · line 610

Quoted 9 lines
lib/vsprintf.c +6 -0
 }
 
 /* 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,

Quoted 11 lines

lib/vsprintf.c char *widen_string(char *buf, int n, char *end, struct printf_spec spec) · line 617

Quoted 7 lines
lib/vsprintf.c +7 -0
+           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)

Quoted 59 lines

lib/vsprintf.c char *dentry_name(char *buf, char *end, const

Quoted 6 lines
lib/vsprintf.c +1 -0
+}
 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

Quoted 10 lines
lib/vsprintf.c +4 -1
 }
 
 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;

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 952

Quoted 10 lines
lib/vsprintf.c +6 -1
+
    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 [*]

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 961

Quoted 8 lines
lib/vsprintf.c +8 -0
+   /* 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.

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 964

Quoted 3 lines
lib/vsprintf.c +3 -0
+
+   /* 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 [*]:

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 968

Quoted 4 lines
lib/vsprintf.c +4 -0
+           return string_truncate(buf, end, p, dpath_len, spec);
+
+   /* space is enough */
+   return string_nocheck(buf, end, p, spec);
Quoted 5 lines

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.

maintainer
#7 lore raw

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

In reply to Jia He
Quoted 59 lines · Jia He

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/

---
 Documentation/core-api/printk-formats.rst |  5 ++-
 lib/vsprintf.c                            | 47 +++++++++++++++++++++--
 2 files changed, 46 insertions(+), 6 deletions(-)
Documentation/core-api/printk-formats.rst +3 -2
index f063a384c7c8..95ba14dc529b 100644
@@ -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.
 
lib/vsprintf.c +1 -0
index f0c35d9b65bf..317b65280252 100644
@@ -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>

lib/vsprintf.c char *widen_string(char *buf, int n, char *end, struct printf_spec spec) · line 604

Quoted 3 lines
lib/vsprintf.c +0 -0
@@ -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.

Quoted 41 lines
lib/vsprintf.c +27 -2
+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;

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 965

Quoted 10 lines
lib/vsprintf.c +10 -0
+
+	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?

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 969

Quoted 4 lines
lib/vsprintf.c +3 -0
+
+	/* 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

author
#8 lore raw

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

Hi Petr

Quoted 138 lines

-----Original Message-----
From: Petr Mladek <[email protected]>
Sent: Monday, June 14, 2021 11:41 PM
To: Justin He <[email protected]>
Cc: Steven Rostedt <[email protected]>; Sergey Senozhatsky <[email protected]>; Andy Shevchenko <[email protected]>; Rasmus Villemoes <[email protected]>; Jonathan Corbet <[email protected]>; Alexander Viro <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>; Peter Zijlstra (Intel) <[email protected]>; Eric Biggers <[email protected]>; Ahmed S. Darwish <[email protected]>; [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH RFCv3 2/3] lib/vsprintf.c: make %pD print full path for file

Quoted 11 lines · Jia He

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.

Quoted 7 lines

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-

[email protected]/

Review

Quoted 9 lines
---
 Documentation/core-api/printk-formats.rst |  5 ++-
 lib/vsprintf.c                            | 47 +++++++++++++++++++++--
 2 files changed, 46 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/Documentation/core-api/printk-formats.rst

Documentation/core-api/printk-formats.rst dentry names · line 413

Quoted 11 lines

index f063a384c7c8..95ba14dc529b 100644

Documentation/core-api/printk-formats.rst +1 -1
@@ -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

lib/vsprintf.c char *widen_string(char *buf, int n, char *end,

Quoted 21 lines
Documentation/core-api/printk-formats.rst +2 -1
 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.
 
lib/vsprintf.c +1 -0
index f0c35d9b65bf..317b65280252 100644
@@ -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)

lib/vsprintf.c char *widen_string(char *buf, int n, char *end, struct printf_spec spec) · line 604

Quoted 3 lines
lib/vsprintf.c +0 -0
 }
 
 /* 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.

lib/vsprintf.c char *dentry_name(char *buf, char *end, const

Quoted 18 lines
lib/vsprintf.c +14 -0
+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

Quoted 28 lines
lib/vsprintf.c +18 -2
 }
 
 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)
Quoted 8 lines

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 965

Quoted 5 lines
lib/vsprintf.c +5 -0
+           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"

Quoted 9 lines

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 969

Quoted 5 lines
lib/vsprintf.c +3 -0
+
+   /* 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():

Quoted 9 lines
      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

Quoted 44 lines

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;
Quoted 9 lines
      /* 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)

Quoted 6 lines

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.

#9 lore raw

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

Quoted 11 lines · Justin He

Hi Petr

Quoted 8 lines
Quoted 7 lines
lib/vsprintf.c +7 -0
+   /* 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);
Quoted 5 lines
Quoted 2 lines

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

author
#10 lore raw

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

Quoted 41 lines

-----Original Message-----
From: Rasmus Villemoes <[email protected]>
Sent: Tuesday, June 15, 2021 3:15 PM
To: Justin He <[email protected]>; Petr Mladek <[email protected]>
Cc: Steven Rostedt <[email protected]>; Sergey Senozhatsky <[email protected]>; Andy Shevchenko <[email protected]>; Rasmus Villemoes <[email protected]>; Jonathan Corbet <[email protected]>; Alexander Viro <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>; Peter Zijlstra (Intel) <[email protected]>; Eric Biggers <[email protected]>; Ahmed S. Darwish <[email protected]>; [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH RFCv3 2/3] lib/vsprintf.c: make %pD print full path for file

Quoted 11 lines · Justin He

Hi Petr

Quoted 8 lines
Quoted 7 lines
lib/vsprintf.c +7 -0
+   /* 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);
Quoted 5 lines
Quoted 2 lines

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=]

Quoted 8 lines
    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)

Quoted 8 lines

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.

author
#11 lore raw

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

In reply to Petr Mladek

Hi Petr

Quoted 211 lines

-----Original Message-----
From: Petr Mladek <[email protected]>
Sent: Monday, June 14, 2021 11:41 PM
To: Justin He <[email protected]>
Cc: Steven Rostedt <[email protected]>; Sergey Senozhatsky <[email protected]>; Andy Shevchenko <[email protected]>; Rasmus Villemoes <[email protected]>; Jonathan Corbet <[email protected]>; Alexander Viro <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>; Peter Zijlstra (Intel) <[email protected]>; Eric Biggers <[email protected]>; Ahmed S. Darwish <[email protected]>; [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH RFCv3 2/3] lib/vsprintf.c: make %pD print full path for file

Quoted 11 lines · Jia He

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.

Quoted 7 lines

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-

[email protected]/

Review

Quoted 9 lines
---
 Documentation/core-api/printk-formats.rst |  5 ++-
 lib/vsprintf.c                            | 47 +++++++++++++++++++++--
 2 files changed, 46 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/Documentation/core-api/printk-formats.rst

Documentation/core-api/printk-formats.rst dentry names · line 413

Quoted 11 lines

index f063a384c7c8..95ba14dc529b 100644

Documentation/core-api/printk-formats.rst +1 -1
@@ -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

Documentation/core-api/printk-formats.rst dentry names · line 415

Quoted 2 lines
Documentation/core-api/printk-formats.rst +0 -0
 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``

lib/vsprintf.c char *widen_string(char *buf, int n, char *end,

Quoted 19 lines
Documentation/core-api/printk-formats.rst +2 -1
-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.
 
lib/vsprintf.c +1 -0
index f0c35d9b65bf..317b65280252 100644
@@ -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)

lib/vsprintf.c char *widen_string(char *buf, int n, char *end, struct printf_spec spec) · line 604

Quoted 3 lines
lib/vsprintf.c +0 -0
 }
 
 /* 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.

lib/vsprintf.c char *widen_string(char *buf, int n, char *end, struct printf_spec spec)

Quoted 8 lines
lib/vsprintf.c +8 -0
+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));

lib/vsprintf.c char *widen_string(char *buf, int n, char *end, · line 614

Quoted 2 lines
lib/vsprintf.c +2 -0
+
+		return widen_string(buf + full_len, full_len, end - lim,

spec);

lib/vsprintf.c char *dentry_name(char *buf, char *end, const

Quoted 8 lines
lib/vsprintf.c +4 -0
+	}
+
+	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

Quoted 23 lines
lib/vsprintf.c +13 -2
 }
 
 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;

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 965

Quoted 10 lines
lib/vsprintf.c +10 -0
+
+	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?

lib/vsprintf.c char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp · line 969

Quoted 4 lines
lib/vsprintf.c +3 -0
+
+	/* 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;
Quoted 8 lines
	/* 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)

Quoted 7 lines

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

author
#12 lore raw

RFC PATCH v3 3/3 lib/test_printf: add test cases for '%pD'

In reply to Jia He

After the behaviour of specifier '%pD' is changed to print full path of struct file, the related test cases are also updated.

Given the string is prepended from the end of the buffer, the check of "wrote beyond the nul-terminator" should be skipped.

---
 lib/test_printf.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
lib/test_printf.c +25 -1
index ec0d5976bb69..3632bd6cf906 100644
@@ -16,6 +16,7 @@
 
 #include <linux/bitmap.h>
 #include <linux/dcache.h>
+#include <linux/fs.h>
 #include <linux/socket.h>
 #include <linux/in.h>
 
@@ -34,6 +35,7 @@ KSTM_MODULE_GLOBALS();
 
 static char *test_buffer __initdata;
 static char *alloced_buffer __initdata;
+static bool is_prepend_buf;
 
 extern bool no_hash_pointers;
 
@@ -78,7 +80,7 @@ do_test(int bufsize, const char *expect, int elen,
 		return 1;
 	}
 
-	if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
+	if (!is_prepend_buf && memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
 			bufsize, fmt);
 		return 1;
@@ -496,6 +498,27 @@ dentry(void)
 	test("  bravo/alfa|  bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
 }
 
+static struct vfsmount test_vfsmnt = {};
+
+static struct file test_file __initdata = {
+	.f_path = { .dentry = &test_dentry[2],
+		    .mnt = &test_vfsmnt,
+	},
+};
+
+static void __init
+f_d_path(void)
+{
+	test("(null)", "%pD", NULL);
+	test("(efault)", "%pD", PTR_INVALID);
+
+	is_prepend_buf = true;
+	test("/bravo/alfa   |/bravo/alfa   ", "%-14pD|%*pD", &test_file, -14, &test_file);
+	test("   /bravo/alfa|   /bravo/alfa", "%14pD|%*pD", &test_file, 14, &test_file);
+	test("   /bravo/alfa|/bravo/alfa   ", "%14pD|%-14pD", &test_file, &test_file);
+	is_prepend_buf = false;
+}
+
 static void __init
 struct_va_format(void)
 {
@@ -779,6 +802,7 @@ test_pointer(void)
 	ip();
 	uuid();
 	dentry();
+	f_d_path();
 	struct_va_format();
 	time_and_date();
 	struct_clk();
Signature · 1 line
2.17.1
#13 lore raw

Re: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Quoted 5 lines · Jia He

After the behaviour of specifier '%pD' is changed to print full path of struct file, the related test cases are also updated.

Given the string is prepended from the end of the buffer, the check of "wrote beyond the nul-terminator" should be skipped.

Sorry, that is far from enough justification.

I should probably have split the "wrote beyond nul-terminator" check in two:

One that checks whether any memory beyond the buffer given to vsnprintf() was touched (including all the padding, but possibly more for the cases where we pass a known-too-short buffer), symmetric to the "wrote before buffer" check.

And then another that checks the area between the '\0' and the end of the given buffer - I suppose that it's fair game for vsnprintf to use all of that as scratch space, and for that it could be ok to add that boolean knob.

Rasmus

author
#14 lore raw

RE: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Hi Rasmus

Quoted 31 lines

-----Original Message-----
From: Rasmus Villemoes <[email protected]>
Sent: Saturday, June 12, 2021 5:40 AM
To: Justin He <[email protected]>; Petr Mladek <[email protected]>; Steven Rostedt <[email protected]>; Sergey Senozhatsky <[email protected]>; Andy Shevchenko <[email protected]>; Jonathan Corbet <[email protected]>; Alexander Viro <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>
Cc: Peter Zijlstra (Intel) <[email protected]>; Eric Biggers <[email protected]>; Ahmed S. Darwish <[email protected]>; linux- [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Quoted 5 lines · Jia He

After the behaviour of specifier '%pD' is changed to print full path of struct file, the related test cases are also updated.

Given the string is prepended from the end of the buffer, the check of "wrote beyond the nul-terminator" should be skipped.

Sorry, that is far from enough justification.

I should probably have split the "wrote beyond nul-terminator" check in two:

One that checks whether any memory beyond the buffer given to vsnprintf() was touched (including all the padding, but possibly more for the cases where we pass a known-too-short buffer), symmetric to the "wrote before buffer" check.

Quoted 5 lines

And then another that checks the area between the '\0' and the end of the given buffer - I suppose that it's fair game for vsnprintf to use all of that as scratch space, and for that it could be ok to add that boolean knob.

Sorry, I could have thought sth like "write beyond the buffer" had been checked by old test cases, but seems not.
I will split the "wrote beyond nul-terminator" check into 2 parts. One for Non-%pD case, the other for %pD.

For %pD, it needs to check whether the space beyond test_buffer[] is written

--
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.

author
#15 lore raw

RE: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Hi Rasmus

Quoted 54 lines

-----Original Message-----
From: Justin He
Sent: Tuesday, June 15, 2021 3:06 PM
To: Rasmus Villemoes <[email protected]>; Petr Mladek <[email protected]>; Steven Rostedt <[email protected]>; Sergey Senozhatsky <[email protected]>; Andy Shevchenko <[email protected]>; Jonathan Corbet <[email protected]>; Alexander Viro <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>
Cc: Peter Zijlstra (Intel) <[email protected]>; Eric Biggers <[email protected]>; Ahmed S. Darwish <[email protected]>; linux- [email protected]; [email protected]; linux- [email protected]
Subject: RE: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Hi Rasmus

Quoted 19 lines

-----Original Message-----
From: Rasmus Villemoes <[email protected]>
Sent: Saturday, June 12, 2021 5:40 AM
To: Justin He <[email protected]>; Petr Mladek <[email protected]>; Steven Rostedt <[email protected]>; Sergey Senozhatsky <[email protected]>; Andy Shevchenko <[email protected]>; Jonathan Corbet <[email protected]>; Alexander Viro <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>
Cc: Peter Zijlstra (Intel) <[email protected]>; Eric Biggers <[email protected]>; Ahmed S. Darwish <[email protected]>; linux- [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Quoted 3 lines · Jia He

After the behaviour of specifier '%pD' is changed to print full path of struct file, the related test cases are also updated.

Quoted 6 lines
Quoted 2 lines

Given the string is prepended from the end of the buffer, the check of "wrote beyond the nul-terminator" should be skipped.

Sorry, that is far from enough justification.

I should probably have split the "wrote beyond nul-terminator" check in

two:

Quoted 11 lines

One that checks whether any memory beyond the buffer given to vsnprintf() was touched (including all the padding, but possibly more for the cases where we pass a known-too-short buffer), symmetric to the "wrote before buffer" check.

And then another that checks the area between the '\0' and the end of the given buffer - I suppose that it's fair game for vsnprintf to use all of that as scratch space, and for that it could be ok to add that boolean knob.

Quoted 10 lines

Sorry, I could have thought sth like "write beyond the buffer" had been checked by
old test cases, but seems not.
I will split the "wrote beyond nul-terminator" check into 2 parts. One for Non-%pD case, the other for %pD.

For %pD, it needs to check whether the space beyond test_buffer[] is written

Another question is about precision,
Do you think I should add some test cases e.g. "%.10pD" here? I once added some, but the gcc report warning:
warning: precision used with '%p' gnu_printf

What do you think of that?

--
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.

#16 lore raw

Re: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

In reply to Justin He
Quoted 33 lines · Justin He

Hi Rasmus

Quoted 31 lines

-----Original Message-----
From: Rasmus Villemoes <[email protected]>
Sent: Saturday, June 12, 2021 5:40 AM
To: Justin He <[email protected]>; Petr Mladek <[email protected]>; Steven Rostedt <[email protected]>; Sergey Senozhatsky <[email protected]>; Andy Shevchenko <[email protected]>; Jonathan Corbet <[email protected]>; Alexander Viro <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>
Cc: Peter Zijlstra (Intel) <[email protected]>; Eric Biggers <[email protected]>; Ahmed S. Darwish <[email protected]>; linux- [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Quoted 5 lines · Jia He

After the behaviour of specifier '%pD' is changed to print full path of struct file, the related test cases are also updated.

Given the string is prepended from the end of the buffer, the check of "wrote beyond the nul-terminator" should be skipped.

Sorry, that is far from enough justification.

I should probably have split the "wrote beyond nul-terminator" check in two:

One that checks whether any memory beyond the buffer given to vsnprintf() was touched (including all the padding, but possibly more for the cases where we pass a known-too-short buffer), symmetric to the "wrote before buffer" check.

Quoted 7 lines
Quoted 5 lines

And then another that checks the area between the '\0' and the end of the given buffer - I suppose that it's fair game for vsnprintf to use all of that as scratch space, and for that it could be ok to add that boolean knob.

Sorry, I could have thought sth like "write beyond the buffer" had been checked by old test cases, but seems not.

It does. Before each (sub)test, we have (assume PAD_SIZE=4, BUF_SIZE=12)

|    <- alloced_buffer ->    |
|  PAD |  test_buffer | PAD  |
| **** | ************ | **** |

Then after snprintf(buf, 10, "pizza") we have

|    <- alloced_buffer ->    |
|  PAD |  test_buffer | PAD  |
| **** | pizza0****** | **** |
A      B       C   D         E

(with 0 being the nul character). Then

        if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {

checks whether snprint wrote anything between A and B, while

if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE +
PAD_SIZE - (written + 1))) {

checks whether there was a write between C and E.

What I'm saying is that I can see it being reasonable for (some helper inside) snprintf to actually write something beyond C, but certainly never beyond D. So the "wrote beyond" test could be split up, with the first half possibly being allowed to be opt-out for certain test cases.

Quoted 4 lines

I will split the "wrote beyond nul-terminator" check into 2 parts. One for Non-%pD case, the other for %pD.

For %pD, it needs to check whether the space beyond test_buffer[] is written

No, that's not the right way to do this. Let me cook up a patch you can include in your series.

Rasmus

maintainer
#17 lore raw

Re: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

In reply to Jia He
Quoted 36 lines · Jia He

After the behaviour of specifier '%pD' is changed to print full path of struct file, the related test cases are also updated.

Given the string is prepended from the end of the buffer, the check of "wrote beyond the nul-terminator" should be skipped.

---
 lib/test_printf.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
lib/test_printf.c +9 -1
index ec0d5976bb69..3632bd6cf906 100644
@@ -78,7 +80,7 @@ do_test(int bufsize, const char *expect, int elen,
 		return 1;
 	}
 
-	if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
+	if (!is_prepend_buf && memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
 		pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
 			bufsize, fmt);
 		return 1;
@@ -496,6 +498,27 @@ dentry(void)
 	test("  bravo/alfa|  bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
 }
 
+static struct vfsmount test_vfsmnt = {};
+
+static struct file test_file __initdata = {
+	.f_path = { .dentry = &test_dentry[2],
+		    .mnt = &test_vfsmnt,
+	},
+};
+

lib/test_printf.c dentry(void) · line 518

Quoted 10 lines
lib/test_printf.c +10 -0
+static void __init
+f_d_path(void)
+{
+	test("(null)", "%pD", NULL);
+	test("(efault)", "%pD", PTR_INVALID);
+
+	is_prepend_buf = true;
+	test("/bravo/alfa   |/bravo/alfa   ", "%-14pD|%*pD", &test_file, -14, &test_file);
+	test("   /bravo/alfa|   /bravo/alfa", "%14pD|%*pD", &test_file, 14, &test_file);
+	test("   /bravo/alfa|/bravo/alfa   ", "%14pD|%-14pD", &test_file, &test_file);

Please, add more test for scenarios when the path does not fit into the buffer or when there are no limitations, ...

I still have to think about is_prepend_buf hack.

lib/test_printf.c dentry(void) · line 524

Quoted 6 lines
lib/test_printf.c +3 -0
+	is_prepend_buf = false;
+}
+
 static void __init
 struct_va_format(void)
 {

Best Regards,
PEtr

author
#18 lore raw

RE: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Hi Petr

Quoted 65 lines

-----Original Message-----
From: Petr Mladek <[email protected]>
Sent: Monday, June 14, 2021 11:44 PM
To: Justin He <[email protected]>
Cc: Steven Rostedt <[email protected]>; Sergey Senozhatsky <[email protected]>; Andy Shevchenko <[email protected]>; Rasmus Villemoes <[email protected]>; Jonathan Corbet <[email protected]>; Alexander Viro <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>; Peter Zijlstra (Intel) <[email protected]>; Eric Biggers <[email protected]>; Ahmed S. Darwish <[email protected]>; [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Quoted 16 lines · Jia He

After the behaviour of specifier '%pD' is changed to print full path of struct file, the related test cases are also updated.

Given the string is prepended from the end of the buffer, the check of "wrote beyond the nul-terminator" should be skipped.

---
 lib/test_printf.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
lib/test_printf.c +0 -0
index ec0d5976bb69..3632bd6cf906 100644

lib/test_printf.c do_test(int bufsize, const char *expect, int elen, · line 81

Quoted 4 lines
lib/test_printf.c +0 -1
@@ -78,7 +80,7 @@ do_test(int bufsize, const char *expect, int elen,
            return 1;
    }
 
-   if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE +

PAD_SIZE - (written + 1))) {

lib/test_printf.c do_test(int bufsize, const char *expect, int elen, · line 83

Quoted 1 line
lib/test_printf.c +1 -0
+   if (!is_prepend_buf && memchr_inv(test_buffer + written + 1,

FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {

lib/test_printf.c do_test(int bufsize, const char *expect, int elen, · line 84

Quoted 1 line
lib/test_printf.c +0 -0
            pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-

terminator\n",

lib/test_printf.c dentry(void) · line 498

Quoted 4 lines
lib/test_printf.c +0 -0
                    bufsize, fmt);
            return 1;
@@ -496,6 +498,27 @@ dentry(void)
    test("  bravo/alfa|  bravo/alfa", "%12pd2|%*pd2", &test_dentry[2],

12, &test_dentry[2]);

Quoted 8 lines
lib/test_printf.c +6 -0
 }
 
+static struct vfsmount test_vfsmnt = {};
+
+static struct file test_file __initdata = {
+   .f_path = { .dentry = &test_dentry[2],
+               .mnt = &test_vfsmnt,
+   },

lib/test_printf.c dentry(void)

Quoted 10 lines
lib/test_printf.c +10 -0
+};
+
+static void __init
+f_d_path(void)
+{
+   test("(null)", "%pD", NULL);
+   test("(efault)", "%pD", PTR_INVALID);
+
+   is_prepend_buf = true;
+   test("/bravo/alfa   |/bravo/alfa   ", "%-14pD|%*pD", &test_file, -14,

&test_file);

Quoted 7 lines

lib/test_printf.c dentry(void) · line 517

Quoted 1 line
lib/test_printf.c +1 -0
+   test("   /bravo/alfa|   /bravo/alfa", "%14pD|%*pD", &test_file, 14,

&test_file);

lib/test_printf.c dentry(void) · line 518

Quoted 1 line
lib/test_printf.c +1 -0
+   test("   /bravo/alfa|/bravo/alfa   ", "%14pD|%-14pD", &test_file,

&test_file);

Please, add more test for scenarios when the path does not fit into the buffer or when there are no limitations, ...

Indeed, thanks

--
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.

#19 lore raw

Re: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Quoted 13 lines · Justin He

Hi Petr

Quoted 10 lines

lib/test_printf.c dentry(void)

Quoted 8 lines
lib/test_printf.c +8 -0
+static void __init
+f_d_path(void)
+{
+   test("(null)", "%pD", NULL);
+   test("(efault)", "%pD", PTR_INVALID);
+
+   is_prepend_buf = true;
+   test("/bravo/alfa   |/bravo/alfa   ", "%-14pD|%*pD", &test_file, -14,

&test_file);

Quoted 1 line

+ test(" /bravo/alfa| /bravo/alfa", "%14pD|%*pD", &test_file, 14,

Quoted 8 lines
Quoted 6 lines

&test_file);

Quoted 1 line

+ test(" /bravo/alfa|/bravo/alfa ", "%14pD|%-14pD", &test_file,

&test_file);

Please, add more test for scenarios when the path does not fit into the buffer or when there are no limitations, ...

Indeed, thanks

Doesn't the existing test() helper do this for you automatically?

/*
* Every fmt+args is subjected to four tests: Three where we
* tell vsnprintf varying buffer sizes (plenty, not quite
* enough and 0), and then we also test that kvasprintf would
* be able to print it as expected.
*/

I don't see why one would need to do anything special for %pD.

Rasmus

author
#20 lore raw

RE: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Quoted 40 lines

-----Original Message-----
From: Rasmus Villemoes <[email protected]>
Sent: Tuesday, June 15, 2021 3:48 PM
To: Justin He <[email protected]>; Petr Mladek <[email protected]>
Cc: Steven Rostedt <[email protected]>; Sergey Senozhatsky <[email protected]>; Andy Shevchenko <[email protected]>; Jonathan Corbet <[email protected]>; Alexander Viro <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>; Peter Zijlstra (Intel) <[email protected]>; Eric Biggers <[email protected]>; Ahmed S. Darwish <[email protected]>; [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Quoted 11 lines · Justin He

Hi Petr

Quoted 8 lines

lib/test_printf.c dentry(void)

Quoted 8 lines
lib/test_printf.c +8 -0
+static void __init
+f_d_path(void)
+{
+   test("(null)", "%pD", NULL);
+   test("(efault)", "%pD", PTR_INVALID);
+
+   is_prepend_buf = true;
+   test("/bravo/alfa   |/bravo/alfa   ", "%-14pD|%*pD", &test_file, -

14,

Quoted 10 lines
Quoted 8 lines

&test_file);

Quoted 1 line

+ test(" /bravo/alfa| /bravo/alfa", "%14pD|%*pD", &test_file, 14,

&test_file);

Quoted 1 line

+ test(" /bravo/alfa|/bravo/alfa ", "%14pD|%-14pD", &test_file,

&test_file);

Please, add more test for scenarios when the path does not fit into the buffer or when there are no limitations, ...

Indeed, thanks

Quoted 9 lines

Doesn't the existing test() helper do this for you automatically?

/*
* Every fmt+args is subjected to four tests: Three where we
* tell vsnprintf varying buffer sizes (plenty, not quite
* enough and 0), and then we also test that kvasprintf would
* be able to print it as expected.
*/

Yes, it had invoked vsnprintf for 3 times in __test() vsnprintf(buf,256) vsnprintf(buf,random_bytes,...) vsnprintf(buf, 0,...);
seems no need to add more test cases

Quoted 1 line

I don't see why one would need to do anything special for %pD.

Okay, got it, agree

--
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.

#21 lore raw

Re: [PATCH RFCv3 3/3] lib/test_printf: add test cases for '%pD'

Quoted 5 lines · Justin He
Quoted 3 lines

From: Rasmus Villemoes <[email protected]>
Sent: Tuesday, June 15, 2021 3:48 PM
On 15/06/2021 09.07, Justin He wrote:

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.

Just a reminder that you still have this footer that prevents (some) people to respond to your messages.

Signature · 2 lines
With Best Regards,
Andy Shevchenko