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

Mateusz Guzik posted 3 patches 10 months, 1 week ago
There is a newer version of this series
[PATCH v2 1/3] vfs: add initial support for CONFIG_VFS_DEBUG
Posted by Mateusz Guzik 10 months, 1 week ago
Small collection of macros taken from mmdebug.h

Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
---
 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

diff --git a/include/linux/fs.h b/include/linux/fs.h
index 1437a3323731..034745af9702 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -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>
diff --git a/include/linux/vfsdebug.h b/include/linux/vfsdebug.h
new file mode 100644
index 000000000000..c96dc589fa01
--- /dev/null
+++ b/include/linux/vfsdebug.h
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef LINUX_VFS_DEBUG_H
+#define LINUX_VFS_DEBUG_H 1
+
+#include <linux/bug.h>
+
+struct inode;
+
+#ifdef CONFIG_DEBUG_VFS
+/*
+ * 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);
+}
+#define VFS_BUG_ON(cond) BUG_ON(cond)
+#define VFS_WARN_ON(cond) (void)WARN_ON(cond)
+#define VFS_WARN_ON_ONCE(cond) (void)WARN_ON_ONCE(cond)
+#define VFS_WARN_ONCE(cond, format...) (void)WARN_ONCE(cond, format)
+#define VFS_WARN(cond, format...) (void)WARN(cond, format)
+
+#define VFS_BUG_ON_INODE(cond, inode)		({			\
+	if (unlikely(!!(cond))) {					\
+		dump_inode(inode, "VFS_BUG_ON_INODE(" #cond")");\
+		BUG_ON(1);						\
+	}								\
+})
+
+#define VFS_WARN_ON_INODE(cond, inode)		({			\
+	int __ret_warn = !!(cond);					\
+									\
+	if (unlikely(__ret_warn)) {					\
+		dump_inode(inode, "VFS_WARN_ON_INODE(" #cond")");\
+		WARN_ON(1);						\
+	}								\
+	unlikely(__ret_warn);						\
+})
+#else
+#define VFS_BUG_ON(cond) BUILD_BUG_ON_INVALID(cond)
+#define VFS_WARN_ON(cond) BUILD_BUG_ON_INVALID(cond)
+#define VFS_WARN_ON_ONCE(cond) BUILD_BUG_ON_INVALID(cond)
+#define VFS_WARN_ONCE(cond, format...) BUILD_BUG_ON_INVALID(cond)
+#define VFS_WARN(cond, format...) BUILD_BUG_ON_INVALID(cond)
+
+#define VFS_BUG_ON_INODE(cond, inode) VFS_BUG_ON(cond)
+#define VFS_WARN_ON_INODE(cond, inode)  BUILD_BUG_ON_INVALID(cond)
+#endif /* CONFIG_DEBUG_VFS */
+
+#endif
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 1af972a92d06..c08ce985c482 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -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
 
-- 
2.43.0
Re: [PATCH v2 1/3] vfs: add initial support for CONFIG_VFS_DEBUG
Posted by Jan Kara 10 months, 1 week ago
On Thu 06-02-25 18:03:05, Mateusz Guzik wrote:
> Small collection of macros taken from mmdebug.h
> 
> Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>

For start this looks good! Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

BTW:

> +/*
> + * 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

-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR
Re: [PATCH v2 1/3] vfs: add initial support for CONFIG_VFS_DEBUG
Posted by Mateusz Guzik 10 months, 1 week ago
On Fri, Feb 7, 2025 at 2:15 PM Jan Kara <jack@suse.cz> wrote:
>
> On Thu 06-02-25 18:03:05, Mateusz Guzik wrote:
> > Small collection of macros taken from mmdebug.h
> >
> > Signed-off-by: Mateusz Guzik <mjguzik@gmail.com>
>
> For start this looks good! Feel free to add:
>
> Reviewed-by: Jan Kara <jack@suse.cz>
>
> BTW:
>
> > +/*
> > + * 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.
-- 
Mateusz Guzik <mjguzik gmail.com>