Skip to contentSkip to conversation

DEBUGFS: Automatically create parents for debugfs files v2

PATCH In review Andi Kleen 6 participants 13 messages Dec 13, 2011

Subsystems (2)
lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/3 DEBUGFS: Automatically create parents for debugfs files v2

From: Andi Kleen <[email protected]>

Allow passing path names to debugfs_create* and automatically create the parents. This makes it much simpler for the caller to create hierarchies.

The way the reference counts are handled is admittedly a bit ugly.

There is no way to clean them up currently other than to delete the tree, but that doesn't seem like a big problem for debugfs to leave behind a few empty directories.

v2: Comment fixes based on review comments.
v3: satisfy the kernel community's white space obsession.

---
 fs/debugfs/inode.c |   62 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 60 insertions(+), 2 deletions(-)
fs/debugfs/inode.c +60 -2
index f3a257d..0081ac0 100644
@@ -146,6 +146,55 @@ static struct file_system_type debug_fs_type = {
 	.kill_sb =	kill_litter_super,
 };
 
+/*
+ * Created parents stay around later. Sorry, but it shouldn't be a big issue.
+ */
+static const char *create_parents(const char *name, struct dentry *parent,
+				  struct dentry **pp, int *ref)
+{
+	char fn[strlen(name) + 1];
+	char *tail;
+	char *s, *p;
+	struct dentry *new_parent, *dentry, *old_parent = NULL;
+
+	strcpy(fn, name);
+	tail = strrchr(fn, '/');
+	if (!tail)
+		return name;
+	tail++;
+
+	s = fn;
+	while ((p = strsep(&s, "/")) != NULL && p != tail) {
+		/*
+		 * When the parent already exists, this will error out.
+		 * Ignore that error. We'll just look the dentry up below.
+		 */
+		dentry = debugfs_create_dir(p, parent);
+
+		/* Is it there now? */
+		mutex_lock(&parent->d_inode->i_mutex);
+		new_parent = lookup_one_len(p, parent, strlen(p));
+		mutex_unlock(&parent->d_inode->i_mutex);
+
+		if (!PTR_ERR(dentry))
+			dput(dentry);
+		if (IS_ERR(new_parent))
+			return NULL;
+		if (old_parent)
+			dput(old_parent);
+		if (parent != *pp)
+			old_parent = parent;
+		parent = new_parent;
+	}
+	if (parent) {
+		*ref = 1;
+		*pp = parent;
+	}
+	if (old_parent)
+		dput(old_parent);
+	return strrchr(name, '/') + 1;
+}
+
 static int debugfs_create_by_name(const char *name, mode_t mode,
 				  struct dentry *parent,
 				  struct dentry **dentry,
@@ -153,6 +202,7 @@ static int debugfs_create_by_name(const char *name, mode_t mode,
 				  const struct file_operations *fops)
 {
 	int error = 0;
+	int ref = 0;
 
 	/* If the parent is not specified, we create it in the root.
 	 * We need the root dentry to do this, which is in the super 
@@ -161,6 +211,7 @@ static int debugfs_create_by_name(const char *name, mode_t mode,
 	 */
 	if (!parent)
 		parent = debugfs_mount->mnt_sb->s_root;
+	name = create_parents(name, parent, &parent, &ref);
 
 	*dentry = NULL;
 	mutex_lock(&parent->d_inode->i_mutex);
@@ -185,12 +236,17 @@ static int debugfs_create_by_name(const char *name, mode_t mode,
 		error = PTR_ERR(*dentry);
 	mutex_unlock(&parent->d_inode->i_mutex);
 
+	if (ref)
+		dput(parent);
+
 	return error;
 }
 
 /**
  * debugfs_create_file - create a file in the debugfs filesystem
  * @name: a pointer to a string containing the name of the file to create.
+ *        This can also contain directories (dir/file), in this case missing
+ *        directories will be automatically created.
  * @mode: the permission that the file should have.
  * @parent: a pointer to the parent dentry for this file.  This should be a
  *          directory dentry if set.  If this paramater is NULL, then the
@@ -243,7 +299,8 @@ EXPORT_SYMBOL_GPL(debugfs_create_file);
 /**
  * debugfs_create_dir - create a directory in the debugfs filesystem
  * @name: a pointer to a string containing the name of the directory to
- *        create.
+ *        create. This can also contain directories (dir/file), in this case
+ *        missing directories will be automatically created.
  * @parent: a pointer to the parent dentry for this file.  This should be a
  *          directory dentry if set.  If this paramater is NULL, then the
  *          directory will be created in the root of the debugfs filesystem.
@@ -269,7 +326,8 @@ EXPORT_SYMBOL_GPL(debugfs_create_dir);
 /**
  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
  * @name: a pointer to a string containing the name of the symbolic link to
- *        create.
+ *        create.  This can also contain directories (dir/file), in this case
+ *        missing directories will be automatically created
  * @parent: a pointer to the parent dentry for this symbolic link.  This
  *          should be a directory dentry if set.  If this paramater is NULL,
  *          then the symbolic link will be created in the root of the debugfs
Signature · 1 line
1.7.7.3
author
#1 lore raw

PATCH 2/3 DEBUGFS: Add per cpu counters

From: Andi Kleen <[email protected]>

Add a simple way to export per cpu counters in debugfs.

This is done using a section, so that they can be declared using a simple macro with minimal typing.

OPEN: modules are not supported yet.

---
 fs/debugfs/Makefile               |    2 +-
 fs/debugfs/counter.c              |   73 +++++++++++++++++++++++++++++++++++++
 include/asm-generic/vmlinux.lds.h |    6 +++
 include/linux/debugfs.h           |   27 ++++++++++++++
 4 files changed, 107 insertions(+), 1 deletions(-)
 create mode 100644 fs/debugfs/counter.c
fs/debugfs/Makefile +1 -1
index 840c456..21be8d3 100644
@@ -1,4 +1,4 @@
-debugfs-objs	:= inode.o file.o
+debugfs-objs	:= inode.o file.o counter.o
 
 obj-$(CONFIG_DEBUG_FS)	+= debugfs.o
 
fs/debugfs/counter.c new file +73 -0

73 lines not on this page · raw message

include/asm-generic/vmlinux.lds.h +6 -0
index b5e2e4c..7a54092 100644
@@ -272,6 +272,12 @@
 									\
 	TRACEDATA							\
 									\
+	__debugfs	: AT(ADDR(__debugfs) - LOAD_OFFSET) {		\
+		VMLINUX_SYMBOL(__start___debugfs) = .;			\
+		*(__debugfs)						\
+		VMLINUX_SYMBOL(__stop___debugfs) = .;			\
+	}								\
+									\
 	/* Kernel symbol table: Normal symbols */			\
 	__ksymtab         : AT(ADDR(__ksymtab) - LOAD_OFFSET) {		\
 		VMLINUX_SYMBOL(__start___ksymtab) = .;			\
include/linux/debugfs.h +27 -0
index e7d9b20..a243b05 100644
@@ -18,6 +18,7 @@
 #include <linux/fs.h>
 
 #include <linux/types.h>
+#include <linux/percpu.h>
 
 struct file_operations;
 
@@ -76,6 +77,24 @@ struct dentry *debugfs_create_blob(const char *name, mode_t mode,
 
 bool debugfs_initialized(void);
 
+struct debugfs_counter {
+	unsigned __percpu *ptr;
+	const char *fn;
+	const char *name;
+} __attribute__((aligned(sizeof(char *))));
+
+/* Note: static doesn't work unlike DEFINE_PERCPU. Sorry. */
+#define DEFINE_DEBUGFS_COUNTER(name_, file)				\
+	DEFINE_PER_CPU(unsigned, name_ ## _counter);			\
+	struct debugfs_counter name_ ## _pcpu_counter __used		\
+	__attribute__((aligned(sizeof(char *)),section("__debugfs"),unused)) \
+	= { .ptr = &name_ ## _counter, .fn = file, .name = #name_ }; \
+
+#define DECLARE_DEBUGFS_COUNTER(name)			\
+	struct debugfs_counter name ## _pcpu_counter
+
+#define debugfs_counter_inc(name) this_cpu_inc(name ## _counter)
+
 #else
 
 #include <linux/err.h>
@@ -193,6 +212,14 @@ static inline bool debugfs_initialized(void)
 	return false;
 }
 
+
+struct debugfs_counter {
+};
+
+#define DEFINE_DEBUGFS_COUNTER(name, file)
+#define DECLARE_DEBUGFS_COUNTER(name)
+#define debugfs_counter_inc(name) do {} while(0)
+
 #endif
 
 #endif
Signature · 1 line
1.7.7.3
#2 lore raw

Re: [PATCH 2/3] DEBUGFS: Add per cpu counters

Quoted 7 lines · Andi Kleen
include/asm-generic/vmlinux.lds.h +6 -0
 									\
+	__debugfs	: AT(ADDR(__debugfs) - LOAD_OFFSET) {		\
+		VMLINUX_SYMBOL(__start___debugfs) = .;			\
+		*(__debugfs)						\
+		VMLINUX_SYMBOL(__stop___debugfs) = .;			\
+	}								\
+									\

....

include/linux/debugfs.h struct dentry *debugfs_create_blob(const char *name, mode_t mode, · line 91

Quoted 12 lines
include/linux/debugfs.h +12 -0
+struct debugfs_counter {
+	unsigned __percpu *ptr;
+	const char *fn;
+	const char *name;
+} __attribute__((aligned(sizeof(char *))));
+
+/* Note: static doesn't work unlike DEFINE_PERCPU. Sorry. */
+#define DEFINE_DEBUGFS_COUNTER(name_, file)				\
+	DEFINE_PER_CPU(unsigned, name_ ## _counter);			\
+	struct debugfs_counter name_ ## _pcpu_counter __used		\
+	__attribute__((aligned(sizeof(char *)),section("__debugfs"),unused)) \
+	= { .ptr = &name_ ## _counter, .fn = file, .name = #name_ }; \

Sigh, we had that section forms an array problem more than once already. Why do you invent another variant and think that it will not explode?

Your alignment magic does not guarantee at all that the structs will form an array. The "aligned" attribute guarantees only the _MINIMUM_ alignment for a structure, but the compiler and the linker are free to align on larger multiples.

See commit 654986462 for details. That has been discussed to death and your solution wont work either across all the various compiler incarnations we have and will have.

Thanks,

tglx

#3 lore raw

Re: [PATCH 2/3] DEBUGFS: Add per cpu counters

Quoted 11 lines · Thomas Gleixner
Quoted 7 lines · Andi Kleen
include/asm-generic/vmlinux.lds.h +6 -0
 									\
+	__debugfs	: AT(ADDR(__debugfs) - LOAD_OFFSET) {		\
+		VMLINUX_SYMBOL(__start___debugfs) = .;			\
+		*(__debugfs)						\
+		VMLINUX_SYMBOL(__stop___debugfs) = .;			\
+	}								\
+									\

....

Quoted 16 lines

include/linux/debugfs.h struct dentry *debugfs_create_blob(const char *name, mode_t mode, · line 91

Quoted 12 lines
include/linux/debugfs.h +12 -0
+struct debugfs_counter {
+	unsigned __percpu *ptr;
+	const char *fn;
+	const char *name;
+} __attribute__((aligned(sizeof(char *))));
+
+/* Note: static doesn't work unlike DEFINE_PERCPU. Sorry. */
+#define DEFINE_DEBUGFS_COUNTER(name_, file)				\
+	DEFINE_PER_CPU(unsigned, name_ ## _counter);			\
+	struct debugfs_counter name_ ## _pcpu_counter __used		\
+	__attribute__((aligned(sizeof(char *)),section("__debugfs"),unused)) \
+	= { .ptr = &name_ ## _counter, .fn = file, .name = #name_ }; \

Sigh, we had that section forms an array problem more than once already. Why do you invent another variant and think that it will not explode?

I did three or four different sections like this in the past and as far as I know none of them has exploded so far in production use.

Can you be more specific? Where exactly do you think this will not work?

Quoted 7 lines

Your alignment magic does not guarantee at all that the structs will form an array. The "aligned" attribute guarantees only the _MINIMUM_ alignment for a structure, but the compiler and the linker are free to align on larger multiples.

See commit 654986462 for details.

Doesn't give a lot of details actually. Which target?

Note that my structure only has pointers, so there is not a lot of potential for "evil" alignment.

-Andi

#4 lore raw

Re: [PATCH 2/3] DEBUGFS: Add per cpu counters

Quoted 11 lines · Andi Kleen
Quoted 10 lines

include/linux/debugfs.h struct dentry *debugfs_create_blob(const char *name, mode_t mode, · line 85

Quoted 7 lines
include/linux/debugfs.h +6 -0
+struct debugfs_counter {
+	unsigned __percpu *ptr;
+	const char *fn;
+	const char *name;
+} __attribute__((aligned(sizeof(char *))));
+

See commit 654986462 for details.

Quoted 4 lines

Doesn't give a lot of details actually. Which target?

Note that my structure only has pointers, so there is not a lot of potential for "evil" alignment.

We hit this with trace events as well, and your structure does have an evil alignment, it's 3 pointers, which is not a base 2 number. Thus, there were some compilers that liked to add padding to make the alignment a power of 2. That is, between two sections of files we ended up with something like:

.section file1
.ptra1
.ptra2
.ptra3
.ptrb1
.ptrb2
.ptrb3
.ptrc1
.ptrc2
.ptrc3
.ptrd1
.ptrd2
.ptrd3
.ptre1
.ptre2
.ptre3
<space>
.section file2
.ptrf1
.ptrf2
.ptrf3
[...]

It didn't happen often, heck, trace events and tracepoints were like this for sometime before this blew up in our faces.

Quoted 1 line

From what I've seen is that 1 or 2 longs will pack nicely, but anything

else (except for maybe 4, 8, 16, etc) will run a risk of crashing.

-- Steve

#5 lore raw

Re: [PATCH 2/3] DEBUGFS: Add per cpu counters

Quoted 49 lines · * Steven Rostedt ([email protected]) wrote:
Quoted 11 lines · Andi Kleen
Quoted 10 lines

include/linux/debugfs.h struct dentry *debugfs_create_blob(const char *name, mode_t mode, · line 85

Quoted 7 lines
include/linux/debugfs.h +6 -0
+struct debugfs_counter {
+	unsigned __percpu *ptr;
+	const char *fn;
+	const char *name;
+} __attribute__((aligned(sizeof(char *))));
+

See commit 654986462 for details.

Quoted 4 lines

Doesn't give a lot of details actually. Which target?

Note that my structure only has pointers, so there is not a lot of potential for "evil" alignment.

We hit this with trace events as well, and your structure does have an evil alignment, it's 3 pointers, which is not a base 2 number. Thus, there were some compilers that liked to add padding to make the alignment a power of 2. That is, between two sections of files we ended up with something like:

.section file1
.ptra1
.ptra2
.ptra3
.ptrb1
.ptrb2
.ptrb3
.ptrc1
.ptrc2
.ptrc3
.ptrd1
.ptrd2
.ptrd3
.ptre1
.ptre2
.ptre3
<space>
.section file2
.ptrf1
.ptrf2
.ptrf3
[...]

Quoted 5 lines

It didn't happen often, heck, trace events and tracepoints were like this for sometime before this blew up in our faces.

From what I've seen is that 1 or 2 longs will pack nicely, but anything else (except for maybe 4, 8, 16, etc) will run a risk of crashing.

Yep. We changed our implementations to use an array of pointers to the elements rather than creating a section of these elements per se to make sure we would not trigger this problem anymore. Unless my memory fails me, it started blowing up in our faces with new gcc versions. The thing is that gcc is within its right to happily over-align those structures. __attribute__((aligned())) is just a hint providing a minimum alignment to respect, but the compiler can choose a higher alignment value as it sees fit.

Mathieu

Quoted 4 lines

-- Steve

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#6 lore raw

Re: [PATCH 2/3] DEBUGFS: Add per cpu counters

In reply to Steven Rostedt
Quoted 1 line · Steven Rostedt

We hit this with trace events as well,

Here's the commit if you were interested:

e4a9ea5ee7c8812a7bf0c3fb725ceeaa3d4c2fcc

-- Steve

#7 lore raw

Re: [PATCH 2/3] DEBUGFS: Add per cpu counters

In reply to Andi Kleen
Quoted 29 lines · Andi Kleen
Quoted 11 lines · Thomas Gleixner
Quoted 7 lines · Andi Kleen
include/asm-generic/vmlinux.lds.h +6 -0
 									\
+	__debugfs	: AT(ADDR(__debugfs) - LOAD_OFFSET) {		\
+		VMLINUX_SYMBOL(__start___debugfs) = .;			\
+		*(__debugfs)						\
+		VMLINUX_SYMBOL(__stop___debugfs) = .;			\
+	}								\
+									\

....

Quoted 16 lines

include/linux/debugfs.h struct dentry *debugfs_create_blob(const char *name, mode_t mode, · line 91

Quoted 12 lines
include/linux/debugfs.h +12 -0
+struct debugfs_counter {
+	unsigned __percpu *ptr;
+	const char *fn;
+	const char *name;
+} __attribute__((aligned(sizeof(char *))));
+
+/* Note: static doesn't work unlike DEFINE_PERCPU. Sorry. */
+#define DEFINE_DEBUGFS_COUNTER(name_, file)				\
+	DEFINE_PER_CPU(unsigned, name_ ## _counter);			\
+	struct debugfs_counter name_ ## _pcpu_counter __used		\
+	__attribute__((aligned(sizeof(char *)),section("__debugfs"),unused)) \
+	= { .ptr = &name_ ## _counter, .fn = file, .name = #name_ }; \

Sigh, we had that section forms an array problem more than once already. Why do you invent another variant and think that it will not explode?

Quoted 5 lines

I did three or four different sections like this in the past and as far as I know none of them has exploded so far in production use.

Can you be more specific? Where exactly do you think this will not work?

Can you actually read and understand what I wrote below?

Quoted 9 lines
Quoted 7 lines

Your alignment magic does not guarantee at all that the structs will form an array. The "aligned" attribute guarantees only the _MINIMUM_ alignment for a structure, but the compiler and the linker are free to align on larger multiples.

See commit 654986462 for details.

Doesn't give a lot of details actually.

There are enough details to figure it out. http://www.jfgit.com might help.

Quoted 1 line

Which target?

The target is irrelevant.

Quoted 2 lines

Note that my structure only has pointers, so there is not a lot of potential for "evil" alignment.

That does NOT matter at ALL. Also whether you did that in the past or not is completely irrelevant. Its also totally irrelevant whether your structure only has pointers or not.

There is NO GUARANTEE that this will end up in a linear array with a stride of the struct size. Period.

Just get it.

Thanks,

tglx

#8 lore raw
In reply to Andi Kleen
Quoted 8 lines · Andi Kleen
fs/debugfs/inode.c +7 -0
+/*
+ * Created parents stay around later. Sorry, but it shouldn't be a big issue.
+ */
+static const char *create_parents(const char *name, struct dentry *parent,
+				  struct dentry **pp, int *ref)
+{
+	char fn[strlen(name) + 1];

fs/debugfs/inode.c static struct file_system_type debug_fs_type = { · line 165

Quoted 10 lines
fs/debugfs/inode.c +10 -0
+	char *tail;
+	char *s, *p;
+	struct dentry *new_parent, *dentry, *old_parent = NULL;
+
+	strcpy(fn, name);
+	tail = strrchr(fn, '/');
+	if (!tail)
+		return name;
+	tail++;
+

Hi Andi, is it possible to do say kstrdup or something instead of variable-size array in fn[strlen(name) + 1] ? As far as I remember we already had some warnings with them at least in perf land (or trace land). Or I miss something?

Cyrill

#9 lore raw
Quoted 1 line

Hi Andi, is it possible to do say kstrdup or something instead of

It's possible, just would add complexity with an additional error path.

Quoted 3 lines

variable-size array in fn[strlen(name) + 1] ? As far as I remember we already had some warnings with them at least in perf land (or trace land). Or I miss something?

Not aware of any problems with tracing or perf. I like them at least.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
#10 lore raw
Quoted 10 lines · Andi Kleen
Quoted 1 line

Hi Andi, is it possible to do say kstrdup or something instead of

It's possible, just would add complexity with an additional error path.

Quoted 3 lines

variable-size array in fn[strlen(name) + 1] ? As far as I remember we already had some warnings with them at least in perf land (or trace land). Or I miss something?

Not aware of any problems with tracing or perf. I like them at least.

That does not make them better. We had wreckage before and you can just consult commit a84a79e4d3 and then find a good explanation why your usage is safe.

Thanks,

tglx

#11 lore raw
Quoted 11 lines · Thomas Gleixner
Quoted 9 lines · Andi Kleen
Quoted 1 line

Hi Andi, is it possible to do say kstrdup or something instead of

It's possible, just would add complexity with an additional error path.

Quoted 3 lines

variable-size array in fn[strlen(name) + 1] ? As far as I remember we already had some warnings with them at least in perf land (or trace land). Or I miss something?

Quoted 5 lines
Quoted 1 line

Not aware of any problems with tracing or perf. I like them at least.

That does not make them better. We had wreckage before and you can just consult commit a84a79e4d3 and then find a good explanation why your usage is safe.

Ok. I must admit I sometimes forget with what substandard toolchains some of the ports are afflicted with.

But fair enough. I'll just make the code longer, more complex, more error prone and slower.

Should probably find some way to let gcc warn about them, I'm sure I'm not the only one who likes them.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
#12 lore raw
Quoted 18 lines · Andi Kleen
Quoted 11 lines · Thomas Gleixner
Quoted 9 lines · Andi Kleen
Quoted 1 line

Hi Andi, is it possible to do say kstrdup or something instead of

It's possible, just would add complexity with an additional error path.

Quoted 3 lines

variable-size array in fn[strlen(name) + 1] ? As far as I remember we already had some warnings with them at least in perf land (or trace land). Or I miss something?

Quoted 5 lines
Quoted 1 line

Not aware of any problems with tracing or perf. I like them at least.

That does not make them better. We had wreckage before and you can just consult commit a84a79e4d3 and then find a good explanation why your usage is safe.

Quoted 5 lines

Ok. I must admit I sometimes forget with what substandard toolchains some of the ports are afflicted with.

But fair enough. I'll just make the code longer, more complex, more error prone and slower.

Oh, yes it's so complex to implement an error path correctly. And of course it's so fcking relevant whether that code takes a few more cycles more or not.

Thanks,

tglx