Skip to content

Commit 420118c

Browse files
Louis RillingJoel Becker
authored andcommitted
configfs: Rework configfs_depend_item() locking and make lockdep happy
configfs_depend_item() recursively locks all inodes mutex from configfs root to the target item, which makes lockdep unhappy. The purpose of this recursive locking is to ensure that the item tree can be safely parsed and that the target item, if found, is not about to leave. This patch reworks configfs_depend_item() locking using configfs_dirent_lock. Since configfs_dirent_lock protects all changes to the configfs_dirent tree, and protects tagging of items to be removed, this lock can be used instead of the inodes mutex lock chain. This needs that the check for dependents be done atomically with CONFIGFS_USET_DROPPING tagging. Now lockdep looks happy with configfs. [ Lifted the setting of s_type into configfs_new_dirent() to satisfy the atomic setting of CONFIGFS_USET_CREATING -- Joel ] Signed-off-by: Louis Rilling <louis.rilling@kerlabs.com> Signed-off-by: Joel Becker <joel.becker@oracle.com>
1 parent e74cc06 commit 420118c

1 file changed

Lines changed: 47 additions & 59 deletions

File tree

‎fs/configfs/dir.c‎

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
167167
/*
168168
* Allocates a new configfs_dirent and links it to the parent configfs_dirent
169169
*/
170-
static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd,
171-
void * element)
170+
static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
171+
void *element, int type)
172172
{
173173
struct configfs_dirent * sd;
174174

@@ -180,6 +180,7 @@ static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * pare
180180
INIT_LIST_HEAD(&sd->s_links);
181181
INIT_LIST_HEAD(&sd->s_children);
182182
sd->s_element = element;
183+
sd->s_type = type;
183184
configfs_init_dirent_depth(sd);
184185
spin_lock(&configfs_dirent_lock);
185186
if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
@@ -225,12 +226,11 @@ int configfs_make_dirent(struct configfs_dirent * parent_sd,
225226
{
226227
struct configfs_dirent * sd;
227228

228-
sd = configfs_new_dirent(parent_sd, element);
229+
sd = configfs_new_dirent(parent_sd, element, type);
229230
if (IS_ERR(sd))
230231
return PTR_ERR(sd);
231232

232233
sd->s_mode = mode;
233-
sd->s_type = type;
234234
sd->s_dentry = dentry;
235235
if (dentry) {
236236
dentry->d_fsdata = configfs_get(sd);
@@ -1006,11 +1006,11 @@ static int configfs_dump(struct configfs_dirent *sd, int level)
10061006
* Note, btw, that this can be called at *any* time, even when a configfs
10071007
* subsystem isn't registered, or when configfs is loading or unloading.
10081008
* Just like configfs_register_subsystem(). So we take the same
1009-
* precautions. We pin the filesystem. We lock each i_mutex _in_order_
1010-
* on our way down the tree. If we can find the target item in the
1009+
* precautions. We pin the filesystem. We lock configfs_dirent_lock.
1010+
* If we can find the target item in the
10111011
* configfs tree, it must be part of the subsystem tree as well, so we
1012-
* do not need the subsystem semaphore. Holding the i_mutex chain locks
1013-
* out mkdir() and rmdir(), who might be racing us.
1012+
* do not need the subsystem semaphore. Holding configfs_dirent_lock helps
1013+
* locking out mkdir() and rmdir(), who might be racing us.
10141014
*/
10151015

10161016
/*
@@ -1023,17 +1023,21 @@ static int configfs_dump(struct configfs_dirent *sd, int level)
10231023
* do that so we can unlock it if we find nothing.
10241024
*
10251025
* Here we do a depth-first search of the dentry hierarchy looking for
1026-
* our object. We take i_mutex on each step of the way down. IT IS
1027-
* ESSENTIAL THAT i_mutex LOCKING IS ORDERED. If we come back up a branch,
1028-
* we'll drop the i_mutex.
1026+
* our object.
1027+
* We deliberately ignore items tagged as dropping since they are virtually
1028+
* dead, as well as items in the middle of attachment since they virtually
1029+
* do not exist yet. This completes the locking out of racing mkdir() and
1030+
* rmdir().
1031+
* Note: subdirectories in the middle of attachment start with s_type =
1032+
* CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When
1033+
* CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of
1034+
* s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
10291035
*
1030-
* If the target is not found, -ENOENT is bubbled up and we have released
1031-
* all locks. If the target was found, the locks will be cleared by
1032-
* configfs_depend_rollback().
1036+
* If the target is not found, -ENOENT is bubbled up.
10331037
*
10341038
* This adds a requirement that all config_items be unique!
10351039
*
1036-
* This is recursive because the locking traversal is tricky. There isn't
1040+
* This is recursive. There isn't
10371041
* much on the stack, though, so folks that need this function - be careful
10381042
* about your stack! Patches will be accepted to make it iterative.
10391043
*/
@@ -1045,13 +1049,13 @@ static int configfs_depend_prep(struct dentry *origin,
10451049

10461050
BUG_ON(!origin || !sd);
10471051

1048-
/* Lock this guy on the way down */
1049-
mutex_lock(&sd->s_dentry->d_inode->i_mutex);
10501052
if (sd->s_element == target) /* Boo-yah */
10511053
goto out;
10521054

10531055
list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1054-
if (child_sd->s_type & CONFIGFS_DIR) {
1056+
if ((child_sd->s_type & CONFIGFS_DIR) &&
1057+
!(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1058+
!(child_sd->s_type & CONFIGFS_USET_CREATING)) {
10551059
ret = configfs_depend_prep(child_sd->s_dentry,
10561060
target);
10571061
if (!ret)
@@ -1060,33 +1064,12 @@ static int configfs_depend_prep(struct dentry *origin,
10601064
}
10611065

10621066
/* We looped all our children and didn't find target */
1063-
mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
10641067
ret = -ENOENT;
10651068

10661069
out:
10671070
return ret;
10681071
}
10691072

1070-
/*
1071-
* This is ONLY called if configfs_depend_prep() did its job. So we can
1072-
* trust the entire path from item back up to origin.
1073-
*
1074-
* We walk backwards from item, unlocking each i_mutex. We finish by
1075-
* unlocking origin.
1076-
*/
1077-
static void configfs_depend_rollback(struct dentry *origin,
1078-
struct config_item *item)
1079-
{
1080-
struct dentry *dentry = item->ci_dentry;
1081-
1082-
while (dentry != origin) {
1083-
mutex_unlock(&dentry->d_inode->i_mutex);
1084-
dentry = dentry->d_parent;
1085-
}
1086-
1087-
mutex_unlock(&origin->d_inode->i_mutex);
1088-
}
1089-
10901073
int configfs_depend_item(struct configfs_subsystem *subsys,
10911074
struct config_item *target)
10921075
{
@@ -1127,17 +1110,21 @@ int configfs_depend_item(struct configfs_subsystem *subsys,
11271110

11281111
/* Ok, now we can trust subsys/s_item */
11291112

1130-
/* Scan the tree, locking i_mutex recursively, return 0 if found */
1113+
spin_lock(&configfs_dirent_lock);
1114+
/* Scan the tree, return 0 if found */
11311115
ret = configfs_depend_prep(subsys_sd->s_dentry, target);
11321116
if (ret)
1133-
goto out_unlock_fs;
1117+
goto out_unlock_dirent_lock;
11341118

1135-
/* We hold all i_mutexes from the subsystem down to the target */
1119+
/*
1120+
* We are sure that the item is not about to be removed by rmdir(), and
1121+
* not in the middle of attachment by mkdir().
1122+
*/
11361123
p = target->ci_dentry->d_fsdata;
11371124
p->s_dependent_count += 1;
11381125

1139-
configfs_depend_rollback(subsys_sd->s_dentry, target);
1140-
1126+
out_unlock_dirent_lock:
1127+
spin_unlock(&configfs_dirent_lock);
11411128
out_unlock_fs:
11421129
mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
11431130

@@ -1162,10 +1149,10 @@ void configfs_undepend_item(struct configfs_subsystem *subsys,
11621149
struct configfs_dirent *sd;
11631150

11641151
/*
1165-
* Since we can trust everything is pinned, we just need i_mutex
1166-
* on the item.
1152+
* Since we can trust everything is pinned, we just need
1153+
* configfs_dirent_lock.
11671154
*/
1168-
mutex_lock(&target->ci_dentry->d_inode->i_mutex);
1155+
spin_lock(&configfs_dirent_lock);
11691156

11701157
sd = target->ci_dentry->d_fsdata;
11711158
BUG_ON(sd->s_dependent_count < 1);
@@ -1176,7 +1163,7 @@ void configfs_undepend_item(struct configfs_subsystem *subsys,
11761163
* After this unlock, we cannot trust the item to stay alive!
11771164
* DO NOT REFERENCE item after this unlock.
11781165
*/
1179-
mutex_unlock(&target->ci_dentry->d_inode->i_mutex);
1166+
spin_unlock(&configfs_dirent_lock);
11801167
}
11811168
EXPORT_SYMBOL(configfs_undepend_item);
11821169

@@ -1376,13 +1363,6 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
13761363
if (sd->s_type & CONFIGFS_USET_DEFAULT)
13771364
return -EPERM;
13781365

1379-
/*
1380-
* Here's where we check for dependents. We're protected by
1381-
* i_mutex.
1382-
*/
1383-
if (sd->s_dependent_count)
1384-
return -EBUSY;
1385-
13861366
/* Get a working ref until we have the child */
13871367
parent_item = configfs_get_config_item(dentry->d_parent);
13881368
subsys = to_config_group(parent_item)->cg_subsys;
@@ -1406,9 +1386,17 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
14061386

14071387
mutex_lock(&configfs_symlink_mutex);
14081388
spin_lock(&configfs_dirent_lock);
1409-
ret = configfs_detach_prep(dentry, &wait_mutex);
1410-
if (ret)
1411-
configfs_detach_rollback(dentry);
1389+
/*
1390+
* Here's where we check for dependents. We're protected by
1391+
* configfs_dirent_lock.
1392+
* If no dependent, atomically tag the item as dropping.
1393+
*/
1394+
ret = sd->s_dependent_count ? -EBUSY : 0;
1395+
if (!ret) {
1396+
ret = configfs_detach_prep(dentry, &wait_mutex);
1397+
if (ret)
1398+
configfs_detach_rollback(dentry);
1399+
}
14121400
spin_unlock(&configfs_dirent_lock);
14131401
mutex_unlock(&configfs_symlink_mutex);
14141402

@@ -1519,7 +1507,7 @@ static int configfs_dir_open(struct inode *inode, struct file *file)
15191507
*/
15201508
err = -ENOENT;
15211509
if (configfs_dirent_is_ready(parent_sd)) {
1522-
file->private_data = configfs_new_dirent(parent_sd, NULL);
1510+
file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
15231511
if (IS_ERR(file->private_data))
15241512
err = PTR_ERR(file->private_data);
15251513
else

0 commit comments

Comments
 (0)