Skip to contentSkip to conversation

cpusets: fix race in cpuset_add_file()

PATCH Paul Jackson 1 participant 3 messages Sep 17, 2004

lore
Share & tools
lore
#0 lore raw

This patch fixes a missing down()/up() pair in cpuset_add_file().

Without this patch, sometimes it is possible to have two duplicate dentries for a single file of a cpuset, with one of them being invalid, and thus the file is present but cannot be opened...

By holding the cpuset directory inode (after the directory is created, while each file in it is being populated), we prevent a stat(2) or open(2) from creating a second, invalid directory entry for one of the files in a cpuset directory, while cpuset_populate_dir is trying to create the same file.

kernel/cpuset.c +10 -4
Index: 2.6.9-rc2-mm1/kernel/cpuset.c
===================================================================
@@ -991,13 +991,12 @@ static int cpuset_create_dir(struct cpus
 	return error;
 }
 
-/* MUST be called with dir->d_inode->i_sem held */
-
 static int cpuset_add_file(struct dentry *dir, const struct cftype *cft)
 {
 	struct dentry *dentry;
 	int error;
 
+	down(&dir->d_inode->i_sem);
 	dentry = cpuset_get_dentry(dir, cft->name);
 	if (!IS_ERR(dentry)) {
 		error = cpuset_create_file(dentry, 0644 | S_IFREG);
@@ -1006,6 +1005,7 @@ static int cpuset_add_file(struct dentry
 		dput(dentry);
 	} else
 		error = PTR_ERR(dentry);
+	up(&dir->d_inode->i_sem);
 	return error;
 }
 
@@ -1197,7 +1197,6 @@ static struct cftype cft_notify_on_relea
 	.private = FILE_NOTIFY_ON_RELEASE,
 };
 
-/* MUST be called with ->d_inode->i_sem held */
 static int cpuset_populate_dir(struct dentry *cs_dentry)
 {
 	int err;
@@ -1254,9 +1253,16 @@ static long cpuset_create(struct cpuset 
 	err = cpuset_create_dir(cs, name, mode);
 	if (err < 0)
 		goto err;
+
+	/*
+	 * Release cpuset_sem before cpuset_populate_dir() because it
+	 * will down() this new directory's i_sem and if we race with
+	 * another mkdir, we might deadlock.
+	 */
+	up(&cpuset_sem);
+
 	err = cpuset_populate_dir(cs->dentry);
 	/* If err < 0, we have a half-filled directory - oh well ;) */
-	up(&cpuset_sem);
 	return 0;
 err:
 	list_del(&cs->sibling);
Signature · 3 lines
                          I won't rest till it's the best ...
                          Programmer, Linux Scalability
                          Paul Jackson <[email protected]> 1.650.933.1373
#1 lore raw

cpusets: remove more casts

Remove some more casts of (void *) d_fsdata.

In gcc, unlike C++, these serve no good purpose, and can hide valid errors if the type changes in the future.

kernel/cpuset.c +5 -6
Index: 2.6.9-rc2-mm1/kernel/cpuset.c
===================================================================
@@ -215,7 +215,7 @@ static void cpuset_diput(struct dentry *
 {
 	/* is dentry a directory ? if so, kfree() associated cpuset */
 	if (S_ISDIR(inode->i_mode)) {
-		struct cpuset *cs = (struct cpuset *)dentry->d_fsdata;
+		struct cpuset *cs = dentry->d_fsdata;
 		BUG_ON(!(is_removed(cs)));
 		kfree(cs);
 	}
@@ -351,12 +351,12 @@ struct cftype {
 
 static inline struct cpuset *__d_cs(struct dentry *dentry)
 {
-	return (struct cpuset *)dentry->d_fsdata;
+	return dentry->d_fsdata;
 }
 
 static inline struct cftype *__d_cft(struct dentry *dentry)
 {
-	return (struct cftype *)dentry->d_fsdata;
+	return dentry->d_fsdata;
 }
 
 /*
@@ -1273,8 +1273,7 @@ err:
 
 static int cpuset_mkdir(struct inode *dir, struct dentry *dentry, int mode)
 {
-	struct dentry *d_parent = dentry->d_parent;
-	struct cpuset *c_parent = (struct cpuset *)d_parent->d_fsdata;
+	struct cpuset *c_parent = dentry->d_parent->d_fsdata;
 
 	/* the vfs holds inode->i_sem already */
 	return cpuset_create(c_parent, dentry->d_name.name, mode | S_IFDIR);
@@ -1282,7 +1281,7 @@ static int cpuset_mkdir(struct inode *di
 
 static int cpuset_rmdir(struct inode *unused_dir, struct dentry *dentry)
 {
-	struct cpuset *cs = (struct cpuset *)dentry->d_fsdata;
+	struct cpuset *cs = dentry->d_fsdata;
 	struct dentry *d;
 	struct cpuset *parent;
 
Signature · 3 lines
                          I won't rest till it's the best ...
                          Programmer, Linux Scalability
                          Paul Jackson <[email protected]> 1.650.933.1373
#2 lore raw

cpusets: make CONFIG_CPUSETS the default in sn2_defconfig

In reply to Paul Jackson

Make CONFIG_CPUSETS enabled by default in sn2_defconfig.

arch/ia64/configs/sn2_defconfig +1 -0
Index: 2.6.9-rc2-mm1/arch/ia64/configs/sn2_defconfig
===================================================================
@@ -28,6 +28,7 @@ CONFIG_KALLSYMS_ALL=y
 # CONFIG_KALLSYMS_EXTRA_PASS is not set
 CONFIG_FUTEX=y
 CONFIG_EPOLL=y
+CONFIG_CPUSETS=y
 CONFIG_IOSCHED_NOOP=y
 CONFIG_IOSCHED_AS=y
 CONFIG_IOSCHED_DEADLINE=y
Signature · 3 lines
                          I won't rest till it's the best ...
                          Programmer, Linux Scalability
                          Paul Jackson <[email protected]> 1.650.933.1373