From: "Eric W. Biederman" <ebiederm@xmission.com>
To: Greg Kroah-Hartman <gregkh@suse.de>
Cc: Kay Sievers <kay.sievers@vrfy.org>, Greg KH <greg@kroah.com>,
linux-kernel@vger.kernel.org, Tejun Heo <tj@kernel.org>,
Cornelia Huck <cornelia.huck@de.ibm.com>,
linux-fsdevel@vger.kernel.org,
Eric Dumazet <eric.dumazet@gmail.com>,
Benjamin LaHaise <bcrl@lhnet.ca>, Serge Hallyn <serue@us.ibm.com>,
"Eric W. Biederman" <ebiederm@xmission.com>,
"Eric W. Biederman" <ebiederm@aristanetworks.com>
Subject: [PATCH 03/15] sysfs: Use dentry_ops instead of directly playing with the dcache
Date: Sat, 7 Nov 2009 23:27:01 -0800 [thread overview]
Message-ID: <1257665233-12468-3-git-send-email-ebiederm@xmission.com> (raw)
In-Reply-To: <m1pr7tmqb4.fsf@fess.ebiederm.org>
From: Eric W. Biederman <ebiederm@xmission.com>
Calling d_drop unconditionally when a sysfs_dirent is deleted has
the potential to leak mounts, so instead implement dentry delete
and revalidate operations that cause sysfs dentries to be removed
at the appropriate time.
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
---
fs/sysfs/dir.c | 73 +++++++++++++++++++++++++++++++++++--------------------
1 files changed, 46 insertions(+), 27 deletions(-)
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index 130dfc3..b5e8499 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -298,6 +298,46 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
goto repeat;
}
+static int sysfs_dentry_delete(struct dentry *dentry)
+{
+ struct sysfs_dirent *sd = dentry->d_fsdata;
+ return !!(sd->s_flags & SYSFS_FLAG_REMOVED);
+}
+
+static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd)
+{
+ struct sysfs_dirent *sd = dentry->d_fsdata;
+ int is_dir;
+
+ mutex_lock(&sysfs_mutex);
+
+ /* The sysfs dirent has been deleted */
+ if (sd->s_flags & SYSFS_FLAG_REMOVED)
+ goto out_bad;
+
+ mutex_unlock(&sysfs_mutex);
+out_valid:
+ return 1;
+out_bad:
+ /* Remove the dentry from the dcache hashes.
+ * If this is a deleted dentry we use d_drop instead of d_delete
+ * so sysfs doesn't need to cope with negative dentries.
+ */
+ is_dir = (sysfs_type(sd) == SYSFS_DIR);
+ mutex_unlock(&sysfs_mutex);
+ if (is_dir) {
+ /* If we have submounts we must allow the vfs caches
+ * to lie about the state of the filesystem to prevent
+ * leaks and other nasty things.
+ */
+ if (have_submounts(dentry))
+ goto out_valid;
+ shrink_dcache_parent(dentry);
+ }
+ d_drop(dentry);
+ return 0;
+}
+
static void sysfs_dentry_iput(struct dentry * dentry, struct inode * inode)
{
struct sysfs_dirent * sd = dentry->d_fsdata;
@@ -307,6 +347,8 @@ static void sysfs_dentry_iput(struct dentry * dentry, struct inode * inode)
}
static const struct dentry_operations sysfs_dentry_ops = {
+ .d_revalidate = sysfs_dentry_revalidate,
+ .d_delete = sysfs_dentry_delete,
.d_iput = sysfs_dentry_iput,
};
@@ -527,44 +569,21 @@ void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
}
/**
- * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent
+ * sysfs_dec_nlink - Decrement link count for the specified sysfs_dirent
* @sd: target sysfs_dirent
*
- * Drop dentry for @sd. @sd must have been unlinked from its
+ * Decrement nlink for @sd. @sd must have been unlinked from its
* parent on entry to this function such that it can't be looked
* up anymore.
*/
-static void sysfs_drop_dentry(struct sysfs_dirent *sd)
+static void sysfs_dec_nlink(struct sysfs_dirent *sd)
{
struct inode *inode;
- struct dentry *dentry;
inode = ilookup(sysfs_sb, sd->s_ino);
if (!inode)
return;
- /* Drop any existing dentries associated with sd.
- *
- * For the dentry to be properly freed we need to grab a
- * reference to the dentry under the dcache lock, unhash it,
- * and then put it. The playing with the dentry count allows
- * dput to immediately free the dentry if it is not in use.
- */
-repeat:
- spin_lock(&dcache_lock);
- list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
- if (d_unhashed(dentry))
- continue;
- dget_locked(dentry);
- spin_lock(&dentry->d_lock);
- __d_drop(dentry);
- spin_unlock(&dentry->d_lock);
- spin_unlock(&dcache_lock);
- dput(dentry);
- goto repeat;
- }
- spin_unlock(&dcache_lock);
-
/* adjust nlink and update timestamp */
mutex_lock(&inode->i_mutex);
@@ -611,7 +630,7 @@ void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
acxt->removed = sd->s_sibling;
sd->s_sibling = NULL;
- sysfs_drop_dentry(sd);
+ sysfs_dec_nlink(sd);
sysfs_deactivate(sd);
unmap_bin_file(sd);
sysfs_put(sd);
--
1.6.5.2.143.g8cc62
next prev parent reply other threads:[~2009-11-08 7:27 UTC|newest]
Thread overview: 92+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-03 11:53 [PATCH 0/13] sysfs lazification Eric W. Biederman
2009-11-03 11:56 ` [PATCH 01/13] sysfs: Update sysfs_setxattr so it updates secdata under the sysfs_mutex Eric W. Biederman
2009-11-03 13:48 ` Serge E. Hallyn
2009-11-03 21:09 ` Eric W. Biederman
2009-11-03 21:31 ` Serge E. Hallyn
2009-11-03 22:13 ` Eric W. Biederman
2009-11-07 2:27 ` Tejun Heo
2009-11-03 11:56 ` [PATCH 02/13] sysfs: Rename sysfs_d_iput to sysfs_dentry_iput Eric W. Biederman
2009-11-04 4:33 ` Serge E. Hallyn
2009-11-03 11:56 ` [PATCH 03/13] sysfs: Use dentry_ops instead of directly playing with the dcache Eric W. Biederman
2009-11-03 22:27 ` Serge E. Hallyn
2009-11-03 11:57 ` [PATCH 04/13] sysfs: Simplify sysfs_chmod_file semantics Eric W. Biederman
2009-11-04 2:43 ` Serge E. Hallyn
2009-11-04 3:42 ` Eric W. Biederman
2009-11-04 4:55 ` Serge E. Hallyn
2009-11-03 11:57 ` [PATCH 05/13] sysfs: Simplify iattr time assignments Eric W. Biederman
2009-11-04 2:57 ` Serge E. Hallyn
2009-11-03 11:57 ` [PATCH 06/13] sysfs: Fix locking and factor out sysfs_sd_setattr Eric W. Biederman
2009-11-04 3:16 ` Serge E. Hallyn
2009-11-04 3:49 ` Eric W. Biederman
2009-11-04 12:56 ` Eric W. Biederman
2009-11-03 11:57 ` [PATCH 07/13] sysfs: Update s_iattr on link and unlink Eric W. Biederman
2009-11-04 3:54 ` Serge E. Hallyn
2009-11-04 4:11 ` Eric W. Biederman
2009-11-04 4:58 ` Serge E. Hallyn
2009-11-03 11:57 ` [PATCH 08/13] sysfs: Nicely indent sysfs_symlink_inode_operations Eric W. Biederman
2009-11-04 4:33 ` Serge E. Hallyn
2009-11-03 11:57 ` [PATCH 09/13] sysfs: Implement sysfs_getattr & sysfs_permission Eric W. Biederman
2009-11-04 4:20 ` Serge E. Hallyn
2009-11-04 5:50 ` Eric W. Biederman
2009-11-04 14:24 ` Serge E. Hallyn
2009-11-07 2:06 ` Tejun Heo
2009-11-08 6:04 ` Eric W. Biederman
2009-11-03 11:57 ` [PATCH 10/13] sysfs: In sysfs_chmod_file lazily propagate the mode change Eric W. Biederman
2009-11-04 4:23 ` Serge E. Hallyn
2009-11-03 11:57 ` [PATCH 11/13] sysfs: Gut sysfs_addrm_start and sysfs_addrm_finish Eric W. Biederman
2009-11-04 4:32 ` Serge E. Hallyn
2009-11-07 2:08 ` Tejun Heo
2009-11-03 11:57 ` [PATCH 12/13] sysfs: Propagate renames to the vfs on demand Eric W. Biederman
2009-11-04 21:49 ` Serge E. Hallyn
2009-11-04 21:59 ` Eric W. Biederman
2009-11-07 2:11 ` Tejun Heo
2009-11-07 2:16 ` Eric W. Biederman
2009-11-07 2:20 ` Tejun Heo
2009-11-07 2:34 ` Eric W. Biederman
2009-11-07 11:12 ` Miklos Szeredi
2009-11-07 11:57 ` Eric W. Biederman
2009-11-09 14:14 ` Serge E. Hallyn
2009-11-09 22:34 ` Eric Biederman
2009-11-03 11:57 ` [PATCH 13/13] sysfs: Factor out sysfs_rename from sysfs_rename_dir and sysfs_move_dir Eric W. Biederman
2009-11-04 22:19 ` Serge E. Hallyn
2009-11-04 22:23 ` Eric W. Biederman
2009-11-04 22:37 ` Serge E. Hallyn
2009-11-05 4:51 ` Eric W. Biederman
2009-11-04 12:04 ` [PATCH 14/13] sysfs: sysfs_setattr remove unnecessary permission check Eric W. Biederman
2009-11-04 14:25 ` Serge E. Hallyn
2009-11-07 2:18 ` Tejun Heo
2009-11-04 12:05 ` [PATCH 15/13] sysfs: Protect sysfs_refresh_inode with inode mutex Eric W. Biederman
2009-11-04 14:27 ` Serge E. Hallyn
2009-11-04 20:51 ` Eric W. Biederman
2009-11-07 2:26 ` Tejun Heo
2009-11-08 7:04 ` Eric W. Biederman
2009-11-06 22:48 ` [PATCH 0/13] sysfs lazification Greg KH
2009-11-08 7:06 ` Eric W. Biederman
2009-11-17 9:11 ` Eric W. Biederman
2009-11-17 15:41 ` Greg KH
2009-11-17 15:56 ` Eric W. Biederman
2009-11-08 7:25 ` [PATCH 0/15] sysfs lazification final Eric W. Biederman
2009-11-08 7:26 ` [PATCH 01/15] sysfs: Update sysfs_setxattr so it updates secdata under the sysfs_mutex Eric W. Biederman
2009-11-08 7:27 ` [PATCH 02/15] sysfs: Rename sysfs_d_iput to sysfs_dentry_iput Eric W. Biederman
2009-11-08 7:27 ` Eric W. Biederman [this message]
2009-11-08 7:27 ` [PATCH 04/15] sysfs: Simplify sysfs_chmod_file semantics Eric W. Biederman
2009-11-08 7:27 ` [PATCH 05/15] sysfs: Simplify iattr time assignments Eric W. Biederman
2009-11-08 7:27 ` [PATCH 06/15] sysfs: Fix locking and factor out sysfs_sd_setattr Eric W. Biederman
2009-11-09 14:26 ` Serge E. Hallyn
2009-11-20 20:13 ` Greg KH
2009-11-20 21:39 ` Eric W. Biederman
2009-11-21 0:07 ` Eric W. Biederman
2009-11-21 5:12 ` Greg KH
2009-11-08 7:27 ` [PATCH 07/15] sysfs: Update s_iattr on link and unlink Eric W. Biederman
2009-11-08 7:27 ` [PATCH 08/15] sysfs: Nicely indent sysfs_symlink_inode_operations Eric W. Biederman
2009-11-08 7:27 ` [PATCH 09/15] sysfs: Implement sysfs_getattr & sysfs_permission Eric W. Biederman
2009-11-08 7:27 ` [PATCH 10/15] sysfs: In sysfs_chmod_file lazily propagate the mode change Eric W. Biederman
2009-11-08 7:27 ` [PATCH 11/15] sysfs: Gut sysfs_addrm_start and sysfs_addrm_finish Eric W. Biederman
2009-11-08 7:27 ` [PATCH 12/15] sysfs: Propagate renames to the vfs on demand Eric W. Biederman
2009-11-08 7:27 ` [PATCH 13/15] sysfs: Factor out sysfs_rename from sysfs_rename_dir and sysfs_move_dir Eric W. Biederman
2009-11-08 7:27 ` [PATCH 14/15] sysfs: sysfs_setattr remove unnecessary permission check Eric W. Biederman
2009-11-08 7:27 ` [PATCH 15/15] sysfs: Protect sysfs_refresh_inode with inode mutex Eric W. Biederman
2009-11-09 3:57 ` [PATCH 0/15] sysfs lazification final Tejun Heo
2009-11-30 21:33 ` Greg KH
2009-11-30 21:53 ` Greg KH
2009-12-01 0:12 ` Eric W. Biederman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1257665233-12468-3-git-send-email-ebiederm@xmission.com \
--to=ebiederm@xmission.com \
--cc=bcrl@lhnet.ca \
--cc=cornelia.huck@de.ibm.com \
--cc=ebiederm@aristanetworks.com \
--cc=eric.dumazet@gmail.com \
--cc=greg@kroah.com \
--cc=gregkh@suse.de \
--cc=kay.sievers@vrfy.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=serue@us.ibm.com \
--cc=tj@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).