Skip to contentSkip to conversation

CONFIG_VFS_DEBUG at last

PATCH Superseded Mateusz Guzik 2 participants 9 messages Feb 6, 2025 – Feb 8, 2025

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover CONFIG_VFS_DEBUG at last

This adds a super basic version just to get the mechanism going and adds sample usage.

The macro set is incomplete (e.g., lack of locking macros) and dump_inode routine fails to dump any state yet, to be implemented(tm).

I think despite the primitive state this is complete enough to start sprinkling warns as necessary.

v2:
- correct may_open
- fixed up condition reporting:
before:
VFS_WARN_ON_INODE(__builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(__builtin_strlen(link)) * 0l)) : (int *)8))), __builtin_strlen(link), __fortify_strlen(link)) != linklen) failed for inode ff32f7c350c8aec8
after:
VFS_WARN_ON_INODE(strlen(link) != linklen) failed for inode ff2b81ddca13f338

Mateusz Guzik (3):
vfs: add initial support for CONFIG_VFS_DEBUG
vfs: catch invalid modes in may_open()
vfs: use the new debug macros in inode_set_cached_link()

 fs/namei.c               |  2 ++
 include/linux/fs.h       | 16 +++----------
 include/linux/vfsdebug.h | 49 ++++++++++++++++++++++++++++++++++++++++
 lib/Kconfig.debug        |  9 ++++++++
 4 files changed, 63 insertions(+), 13 deletions(-)
 create mode 100644 include/linux/vfsdebug.h
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 vfs: add initial support for CONFIG_VFS_DEBUG

Small collection of macros taken from mmdebug.h

---
 include/linux/fs.h       |  1 +
 include/linux/vfsdebug.h | 49 ++++++++++++++++++++++++++++++++++++++++
 lib/Kconfig.debug        |  9 ++++++++
 3 files changed, 59 insertions(+)
 create mode 100644 include/linux/vfsdebug.h
include/linux/fs.h +1 -0
index 1437a3323731..034745af9702 100644
@@ -2,6 +2,7 @@
 #ifndef _LINUX_FS_H
 #define _LINUX_FS_H
 
+#include <linux/vfsdebug.h>
 #include <linux/linkage.h>
 #include <linux/wait_bit.h>
 #include <linux/kdev_t.h>
include/linux/vfsdebug.h new file +49 -0

49 lines not on this page · raw message

lib/Kconfig.debug +9 -0
index 1af972a92d06..c08ce985c482 100644
@@ -808,6 +808,15 @@ config ARCH_HAS_DEBUG_VM_PGTABLE
 	  An architecture should select this when it can successfully
 	  build and run DEBUG_VM_PGTABLE.
 
+config DEBUG_VFS
+	bool "Debug VFS"
+	depends on DEBUG_KERNEL
+	help
+	  Enable this to turn on extended checks in the VFS layer that may impact
+	  performance.
+
+	  If unsure, say N.
+
 config DEBUG_VM_IRQSOFF
 	def_bool DEBUG_VM && !PREEMPT_RT
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 2/3 vfs: catch invalid modes in may_open()

In reply to Mateusz Guzik
---
 fs/namei.c | 2 ++
 1 file changed, 2 insertions(+)
fs/namei.c +2 -0
index 3ab9440c5b93..21630a0f8e30 100644
@@ -3415,6 +3415,8 @@ static int may_open(struct mnt_idmap *idmap, const struct path *path,
 		if ((acc_mode & MAY_EXEC) && path_noexec(path))
 			return -EACCES;
 		break;
+	default:
+		VFS_BUG_ON_INODE(1, inode);
 	}
 
 	error = inode_permission(idmap, inode, MAY_OPEN | acc_mode);
Signature · 1 line
2.43.0
#6 lore raw

PATCH v2 3/3 vfs: use the new debug macros in inode_set_cached_link()

In reply to Mateusz Guzik
---
 include/linux/fs.h | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
include/linux/fs.h +2 -13
index 034745af9702..e71d58c7f59c 100644
@@ -792,19 +792,8 @@ struct inode {
 
 static inline void inode_set_cached_link(struct inode *inode, char *link, int linklen)
 {
-	int testlen;
-
-	/*
-	 * TODO: patch it into a debug-only check if relevant macros show up.
-	 * In the meantime, since we are suffering strlen even on production kernels
-	 * to find the right length, do a fixup if the wrong value got passed.
-	 */
-	testlen = strlen(link);
-	if (testlen != linklen) {
-		WARN_ONCE(1, "bad length passed for symlink [%s] (got %d, expected %d)",
-			  link, linklen, testlen);
-		linklen = testlen;
-	}
+	VFS_WARN_ON_INODE(strlen(link) != linklen, inode);
+	VFS_WARN_ON_INODE(inode->i_opflags & IOP_CACHED_LINK, inode);
 	inode->i_link = link;
 	inode->i_linklen = linklen;
 	inode->i_opflags |= IOP_CACHED_LINK;
Signature · 1 line
2.43.0
#8 lore raw
In reply to Mateusz Guzik
Quoted 10 lines · Mateusz Guzik

This adds a super basic version just to get the mechanism going and adds sample usage.

The macro set is incomplete (e.g., lack of locking macros) and dump_inode routine fails to dump any state yet, to be implemented(tm).

I think despite the primitive state this is complete enough to start sprinkling warns as necessary.

Quoted 11 lines

v2:
- correct may_open
- fixed up condition reporting:
before:
VFS_WARN_ON_INODE(__builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(__builtin_strlen(link)) * 0l)) : (int *)8))), __builtin_strlen(link), __fortify_strlen(link)) != linklen) failed for inode ff32f7c350c8aec8
after:
VFS_WARN_ON_INODE(strlen(link) != linklen) failed for inode ff2b81ddca13f338

welp, the BSD asserts fire when the condition is false, while Linux when it is true, so the *text* is backwards. should:
VFS_WARN_ON_INODE(strlen(link) != linklen) encountered for inode ff2b81ddca13f338

modulo this bit I think this is fine for now

Quoted 15 lines

Mateusz Guzik (3):
vfs: add initial support for CONFIG_VFS_DEBUG
vfs: catch invalid modes in may_open()
vfs: use the new debug macros in inode_set_cached_link()

 fs/namei.c               |  2 ++
 include/linux/fs.h       | 16 +++----------
 include/linux/vfsdebug.h | 49 ++++++++++++++++++++++++++++++++++++++++
 lib/Kconfig.debug        |  9 ++++++++
 4 files changed, 63 insertions(+), 13 deletions(-)
 create mode 100644 include/linux/vfsdebug.h

--
2.43.0

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
reviewer Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] vfs: add initial support for CONFIG_VFS_DEBUG

In reply to Mateusz Guzik
Quoted 3 lines · Mateusz Guzik

Small collection of macros taken from mmdebug.h

For start this looks good! Feel free to add:

BTW:

include/linux/vfsdebug.h line 15

Quoted 6 lines
include/linux/vfsdebug.h new file +6 -0
+/*
+ * TODO: add a proper inode dumping routine, this is a stub to get debug off the ground
+ */
+static inline void dump_inode(struct inode *inode, const char *reason) {
+	pr_crit("%s failed for inode %px", reason, inode);
+}

fs/inode.c:dump_mapping() already has quite a bit of what you'd want here so just refactoring dump_mapping() so it can be used in the new asserts would get you 90% there I'd think.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
reviewer Reviewed-by on v2 3/3
#7 lore raw

Re: [PATCH v2 3/3] vfs: use the new debug macros in inode_set_cached_link()

In reply to Mateusz Guzik
Quoted 1 line · Mateusz Guzik

Looks good. Feel free to add:

Honza

Quoted 33 lines
---
 include/linux/fs.h | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
include/linux/fs.h +2 -13
index 034745af9702..e71d58c7f59c 100644
@@ -792,19 +792,8 @@ struct inode {
 
 static inline void inode_set_cached_link(struct inode *inode, char *link, int linklen)
 {
-	int testlen;
-
-	/*
-	 * TODO: patch it into a debug-only check if relevant macros show up.
-	 * In the meantime, since we are suffering strlen even on production kernels
-	 * to find the right length, do a fixup if the wrong value got passed.
-	 */
-	testlen = strlen(link);
-	if (testlen != linklen) {
-		WARN_ONCE(1, "bad length passed for symlink [%s] (got %d, expected %d)",
-			  link, linklen, testlen);
-		linklen = testlen;
-	}
+	VFS_WARN_ON_INODE(strlen(link) != linklen, inode);
+	VFS_WARN_ON_INODE(inode->i_opflags & IOP_CACHED_LINK, inode);
 	inode->i_link = link;
 	inode->i_linklen = linklen;
 	inode->i_opflags |= IOP_CACHED_LINK;
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
reviewer Reviewed-by on v2 2/3
#5 lore raw

Re: [PATCH v2 2/3] vfs: catch invalid modes in may_open()

In reply to Mateusz Guzik
Quoted 1 line · Mateusz Guzik

Looks good. Feel free to add:

Honza

Quoted 20 lines
---
 fs/namei.c | 2 ++
 1 file changed, 2 insertions(+)
fs/namei.c +2 -0
index 3ab9440c5b93..21630a0f8e30 100644
@@ -3415,6 +3415,8 @@ static int may_open(struct mnt_idmap *idmap, const struct path *path,
 		if ((acc_mode & MAY_EXEC) && path_noexec(path))
 			return -EACCES;
 		break;
+	default:
+		VFS_BUG_ON_INODE(1, inode);
 	}
 
 	error = inode_permission(idmap, inode, MAY_OPEN | acc_mode);
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw

Re: [PATCH v2 1/3] vfs: add initial support for CONFIG_VFS_DEBUG

In reply to Jan Kara
Quoted 12 lines · Jan Kara
Quoted 3 lines · Mateusz Guzik

Small collection of macros taken from mmdebug.h

For start this looks good! Feel free to add:

BTW:

Quoted 11 lines

include/linux/vfsdebug.h line 15

Quoted 6 lines
include/linux/vfsdebug.h new file +6 -0
+/*
+ * TODO: add a proper inode dumping routine, this is a stub to get debug off the ground
+ */
+static inline void dump_inode(struct inode *inode, const char *reason) {
+     pr_crit("%s failed for inode %px", reason, inode);
+}

fs/inode.c:dump_mapping() already has quite a bit of what you'd want here so just refactoring dump_mapping() so it can be used in the new asserts would get you 90% there I'd think.

It looks rather underwhelming.

I was thinking about an equivalent of vn_printf like here:
https://cgit.freebsd.org/src/tree/sys/kern/vfs_subr.c#n4533

Dumps all fields, with spelled out flag names and so on. Also there is a hook for fs-specific dump routine.

Very useful, but also quite a chore to fully implement and future-proof.

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>