Skip to contentSkip to conversation

backports: add remove_proc_subtree() backport

RFC In review Luis R. Rodriguez 2 participants 5 messages May 13, 2013

lore
Share & tools
lore
#0 lore raw

RFC 1/1 backports: add remove_proc_subtree() backport

From: "Luis R. Rodriguez" <[email protected]>

Intorduced in next-20130429 and Linus has merged it onto v3.10-rc1. Backport this for older kernels.

The new in-kernel implementation relies on internal mechanisms for parent directory parsing and then traversal but these rely on internal procfs locking primatives which we cannot use externally. This limits our implementation with the assumption your procfs implementation is correct.

This implementation uses recursion then if subdirs are found otherwise it treats it as a regular remove_proc_entry()

commit 8ce584c7416d8a85a6f3edc17d1cddefe331e87e

Author: Al Viro <[email protected]>
Date:   Sat Mar 30 20:13:46 2013 -0400

procfs: add proc_remove_subtree()

just what it sounds like; do that only to procfs subtrees you've created - doing that to something shared with another driver is not only antisocial, but might cause interesting races with proc_create() and its ilk.

---

Al, I tried to backport remove_proc_subtree() as best as I could but I am not confident on this solution and would appreciate a review on your part if possible.

 backport/backport-include/linux/proc_fs.h |    7 +++++
 backport/compat/backport-3.10.c           |   48 +++++++++++++++++++++++++++--
 2 files changed, 52 insertions(+), 3 deletions(-)
backport/backport-include/linux/proc_fs.h +7 -0
index 5a1bec1..a85b4fb 100644
@@ -12,6 +12,13 @@ static inline void *PDE_DATA(const struct inode *inode)
 {
 	return PROC_I(inode)->pde->data;
 }
+
+#ifdef CONFIG_PROC_FS
+extern int remove_proc_subtree(const char *name, struct proc_dir_entry *parent);
+#else
+#define remove_proc_subtree(name, parent) do {} while (0)
+#endif /* CONFIG_PROC_FS */
+
 #endif
 
 #endif /* __BACKPORT_PROC_FS_H */
backport/compat/backport-3.10.c +45 -3

67 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 2 lines · Luis R. Rodriguez

This implementation uses recursion then if subdirs are found otherwise it treats it as a regular remove_proc_entry()

Ugh... What for? It's not as if traversing the damn thing had been complicated:
de = root = root of subtree to be killed
unlink de
while true
/* de is already unlinked */
if de has children
child = first child of de
unlink child
de = child
else
/* de can be killed now */
parent = parent of de
kill de
if de == root
return
de = parent
and that's it. Why bother with recursion, chew stack space, etc.? We do depth-first walk through the tree, unlinking the nodes from the lists of children on the way in and freeing them on the way out.

The difference from your variant is that you use the stack to hold pointers to ancestors of the current victim. You get to the parent of said victim by discarding a stack frame. No need, since the victim contained an explicit pointer to its parent...

#2 lore raw
Quoted 6 lines · Al Viro
Quoted 2 lines · Luis R. Rodriguez

This implementation uses recursion then if subdirs are found otherwise it treats it as a regular remove_proc_entry()

Ugh... What for?

I could not figure out a way to traverse the tree safely without being able to use the proc_subdir_lock spinlock. Given that the way we backport is through an external module I had to look at what ways this could be done safely.

Quoted 10 lines

It's not as if traversing the damn thing had
been complicated:
de = root = root of subtree to be killed
unlink de
while true
/* de is already unlinked */
if de has children
child = first child of de
unlink child
de = child

Quoted 8 lines

else
/* de can be killed now */ parent = parent of de
kill de
if de == root
return
de = parent
and that's it. Why bother with recursion, chew stack space, etc.?

I would pick up the same implementation if we could but not having access to the locks and also in the case of NULL being passed as the parent, which some drivers do pass I couldn't see how to easily get access to &proc_root. So this is what I came up with and so I wanted to check if perhaps I missed something.

Quoted 2 lines

We do depth-first walk through the tree, unlinking the nodes from the lists of children on the way in and freeing them on the way out.

OK thanks for the clarification.

Quoted 4 lines

The difference from your variant is that you use the stack to hold pointers to ancestors of the current victim. You get to the parent of said victim by discarding a stack frame. No need, since the victim contained an explicit pointer to its parent...

Except I thought NULL was passed?

Luis

#3 lore raw
Quoted 6 lines · Luis R. Rodriguez
Quoted 4 lines

The difference from your variant is that you use the stack to hold pointers to ancestors of the current victim. You get to the parent of said victim by discarding a stack frame. No need, since the victim contained an explicit pointer to its parent...

Except I thought NULL was passed?

AFAICS, your variant removes _everything_ in the parent. IOW, instead of
rm -rf $ROOT/$RELATIVE_PATH
you do

	test -n $ROOT && rm -rf $ROOT/*
	rm $ROOT/$RELATIVE_PATH

which is not the same thing...

#4 lore raw
Quoted 9 lines · Al Viro
Quoted 6 lines · Luis R. Rodriguez
Quoted 4 lines

The difference from your variant is that you use the stack to hold pointers to ancestors of the current victim. You get to the parent of said victim by discarding a stack frame. No need, since the victim contained an explicit pointer to its parent...

Except I thought NULL was passed?

Quoted 7 lines

AFAICS, your variant removes _everything_ in the parent. IOW, instead of
rm -rf $ROOT/$RELATIVE_PATH
you do

        test -n $ROOT && rm -rf $ROOT/*
        rm $ROOT/$RELATIVE_PATH

which is not the same thing...

Crap, yes, I see now thanks. I can't see how we can safely traverse the tree to find the respective struct proc_dir_entry for the passed name as a modular solution. That is, in order to rm -rf $ROOT/$RELATIVE_PATH I first need the $ROOT/$RELATIVE_PATH struct proc_dir_entry.

Any recommendations?

Luis