Skip to contentSkip to conversation

JBD-DEBUG /proc/sys entry (again)

PATCH Rusev 2 participants 2 messages Oct 4, 2007 Proc filesystem

lore
Share & tools
lore

Rusev

author
#0 lore raw

All that should be moved to DEBUGFS under /sys/kernel/debug and so on
- that's right, a bit other issue
is of interest for me.

My suggestion is that a few other problems with PROCFS exist:

Quoted 1 line

From my observation there are two major issues are involved:

1. /proc/sys entry has very specific readdir operation (vs. other entries such as /proc/drivers and others)
entries to /proc/sys is likely is to be performed by means of other API. (quick search found thet API explanation for 2.4.xx
http://www.opentech.at/papers/embedded_proc/node33.html,
yet it looks to be a little change in 2.6)

2. function xlate_proc_name behaves not the way it specified in it's header comment:
[1] minor issue is that proc_match wolud likely return "equals"
as result of comparison of "sys" and "sysvipc"
[2] more significant issue is that it can't properly walk long
paths such as /proc/sys/jbd/jbd-debug,
but only paths likes /proc/sys/jbd-debug (just one step
down, path walking is broken).

This way we can't add not only /proc/sys/jbd/jbd-debug but any path likes /proc/aaa/bbb/xxx-debug at one step.
The entry /proc/sys is still specific, because even if fixing xlate_proc_name we can't see /proc/sys/jbd/jbd-debug
in userspace and successfully see /proc/aaa/bbb/jbd-debug.

That's because /proc/sys specific operator readdir blocks such PROCFS entries that they are NOTproperly registersd
with CTL_TABLE.

Yet I think that we have a general problem with adding-long-paths-in-one-step, which is addressed by the following patch:

diff -uprN linux-2.6.21.orig/fs/proc/generic.c
linux-2.6.21/fs/proc/generic.c

fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400 renamed from fs/proc/generic.c 2007-09-13 15:36:07.000000000 +0400 +24 -11
@@ -298,6 +298,7 @@ static int xlate_proc_name(const char *n
        int                     len;
        int                     rtn = 0;
 
+
        spin_lock(&proc_subdir_lock);
        de = &proc_root;
        while (1) {
@@ -305,24 +306,52 @@ static int xlate_proc_name(const char *n
                if (!next)
                        break;
 
-               len = next - cp;
-               for (de = de->subdir; de ; de = de->next) {
-                       if (proc_match(len, cp, de))
-                               break;
-               }
-               if (!de) {
-                       rtn = -ENOENT;
-                       goto out;
-               }
-               cp += len + 1;
-       }
+                ++next;
+
+
+                len = next - cp;
+
+                if(de->subdir == NULL){
+                  /* directory "de" is empty, add myself to it now */
+                  char* my_name = kzalloc( (len - 1)  + 1, GFP_KERNEL);
+                  memcpy(my_name, cp, len - 1);
+                  proc_mkdir(my_name,de);
+                  kfree(my_name);
+                }
+
+
+                struct proc_dir_entry   *parent_de = de;
+                for (de = parent_de->subdir; de ; de = de->next) {
+                  if (proc_match(len - 1, cp, de))
+                                break;
+
+                }
+
+                if(de == NULL){
+                  /* we found no appropriate subdirectory, well create

it now */

+                  char* my_name = kzalloc( (len - 1)  + 1, GFP_KERNEL);
+                  memcpy(my_name, cp, len - 1);
+                  de = proc_mkdir(my_name,parent_de);
+                  kfree(my_name);

+ }
+
+
+
+ if (!de){
+ rtn = -ENOENT;
+ goto out;
+ }
+ cp += len;
+ }

        *residual = cp;
        *ret = de;
 out:
        spin_unlock(&proc_subdir_lock);
        return rtn;

+
}

+
static DEFINE_IDR(proc_inum_idr);
static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */

#1 lore raw
Quoted 24 lines · On Thu, 04 Oct 2007 16:28:07 +0400 Rusev <[email protected]> wrote:

All that should be moved to DEBUGFS under /sys/kernel/debug and so on
- that's right, a bit other issue
is of interest for me.

My suggestion is that a few other problems with PROCFS exist:

From my observation there are two major issues are involved:

1. /proc/sys entry has very specific readdir operation (vs. other entries such as /proc/drivers and others)
entries to /proc/sys is likely is to be performed by means of other API. (quick search found thet API explanation for 2.4.xx
http://www.opentech.at/papers/embedded_proc/node33.html,
yet it looks to be a little change in 2.6)

2. function xlate_proc_name behaves not the way it specified in it's header comment:
[1] minor issue is that proc_match wolud likely return "equals"
as result of comparison of "sys" and "sysvipc"
[2] more significant issue is that it can't properly walk long
paths such as /proc/sys/jbd/jbd-debug,
but only paths likes /proc/sys/jbd-debug (just one step
down, path walking is broken).

Quoted 5 lines

This way we can't add not only /proc/sys/jbd/jbd-debug but any path likes /proc/aaa/bbb/xxx-debug at one step.
The entry /proc/sys is still specific, because even if fixing xlate_proc_name we can't see /proc/sys/jbd/jbd-debug
in userspace and successfully see /proc/aaa/bbb/jbd-debug.

This patch is wrong. xlate_pro_name() is meant to check if a given path is valid, creating new directory entries is something that need to be handle by the code that's creating the entries.

Also note that xlate_pro_name() is also called by remove_proc_entry() so if I call it with a bogus path, this patch will end up creating new directory entries which is not the intended result.

Quoted 6 lines

That's because /proc/sys specific operator readdir blocks such PROCFS entries that they are NOTproperly registersd
with CTL_TABLE.

Yet I think that we have a general problem with adding-long-paths-in-one-step, which is addressed by the following patch:

This should not be done is user one step and for good reason. If you blindly create multiple directory entries in /proc, how are you going to keep track of all the created entries when its time to remove them (module unloading for example)?

If you enter an invalid path the original code is doing the right thing by returning -ENOENT.

fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400 static int xlate_proc_name(const char *n · line 301

Quoted 12 lines

diff -uprN linux-2.6.21.orig/fs/proc/generic.c
linux-2.6.21/fs/proc/generic.c

fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400 renamed from fs/proc/generic.c 2007-09-13 15:36:07.000000000 +0400 +1 -0
@@ -298,6 +298,7 @@ static int xlate_proc_name(const char *n
        int                     len;
        int                     rtn = 0;
 
+

White space damage.

Quoted 16 lines
fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400 renamed from fs/proc/generic.c 2007-09-13 15:36:07.000000000 +0400 +0 -9
        spin_lock(&proc_subdir_lock);
        de = &proc_root;
        while (1) {
@@ -305,24 +306,52 @@ static int xlate_proc_name(const char *n
                if (!next)
                        break;
 
-               len = next - cp;
-               for (de = de->subdir; de ; de = de->next) {
-                       if (proc_match(len, cp, de))
-                               break;
-               }
-               if (!de) {
-                       rtn = -ENOENT;
-                       goto out;
-               }

fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400 static int xlate_proc_name(const char *n · line 316

Quoted 10 lines
fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400 renamed from fs/proc/generic.c 2007-09-13 15:36:07.000000000 +0400 +8 -2
-               cp += len + 1;
-       }
+                ++next;
+
+
+                len = next - cp;
+
+                if(de->subdir == NULL){
+                  /* directory "de" is empty, add myself to it now */
+                  char* my_name = kzalloc( (len - 1)  + 1, GFP_KERNEL);

You did not check if kzalloc was successfully. If the allocation fails, bad things will happen here. Need to check the return status of my_name and return -ENOMEM if the allocation fails. This would of course mean an API change and you would need make sure that all the callers of xlate_proc_name handle the new return code correctly.

Quoted 15 lines
fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400 renamed from fs/proc/generic.c 2007-09-13 15:36:07.000000000 +0400 +15 -0
+                  memcpy(my_name, cp, len - 1);
+                  proc_mkdir(my_name,de);
+                  kfree(my_name);
+                }
+
+
+                struct proc_dir_entry   *parent_de = de;
+                for (de = parent_de->subdir; de ; de = de->next) {
+                  if (proc_match(len - 1, cp, de))
+                                break;
+
+                }
+
+                if(de == NULL){
+                  /* we found no appropriate subdirectory, well create
Quoted 1 line

it now */

1. Email client cut the line. Disable line wrapping.
2. Line too long - Documentation/CodingStyle

Quoted 1 line

+ char* my_name = kzalloc( (len - 1) + 1, GFP_KERNEL);

Again, check for kzalloc return status.

fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400 static int xlate_proc_name(const char *n · line 327

Quoted 7 lines
fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400 renamed from fs/proc/generic.c 2007-09-13 15:36:07.000000000 +0400 +7 -0
+                  memcpy(my_name, cp, len - 1);
+                  de = proc_mkdir(my_name,parent_de);
+                  kfree(my_name);
+                }
+
+
+

White space damage. To many new lines. One is enough.

fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400

Quoted 4 lines
fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400 renamed from fs/proc/generic.c 2007-09-13 15:36:07.000000000 +0400 +4 -0
+                if (!de){
+                        rtn = -ENOENT;
+                        goto out;
+               }

This is basically dead code since you make sure to create a directory entry if one does not exist. The code above makes sure you never hit this condition, which is wrong since you should not create directory entries blindly. Returning -ENOENT on a invalid path is the right thing to do here.

fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400

Quoted 8 lines
fs/proc/generic.c 2007-10-03 22:12:57.000000000 +0400 renamed from fs/proc/generic.c 2007-09-13 15:36:07.000000000 +0400 +3 -0
+                cp += len;
+        }
        *residual = cp;
        *ret = de;
 out:
        spin_unlock(&proc_subdir_lock);
        return rtn;
+

White space damage.

Quoted 3 lines

}

+

White space damage.

Quoted 3 lines

static DEFINE_IDR(proc_inum_idr);
static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */

-JRS