Skip to content

Commit f3ad116

Browse files
committed
Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jlbec/configfs
* 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jlbec/configfs: configfs: Rework configfs_depend_item() locking and make lockdep happy configfs: Silence lockdep on mkdir() and rmdir()
2 parents d645727 + 420118c commit f3ad116

3 files changed

Lines changed: 178 additions & 59 deletions

File tree

‎fs/configfs/configfs_internal.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ struct configfs_dirent {
3939
umode_t s_mode;
4040
struct dentry * s_dentry;
4141
struct iattr * s_iattr;
42+
#ifdef CONFIG_LOCKDEP
43+
int s_depth;
44+
#endif
4245
};
4346

4447
#define CONFIGFS_ROOT 0x0001

‎fs/configfs/dir.c‎

Lines changed: 137 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,97 @@ static const struct dentry_operations configfs_dentry_ops = {
7878
.d_delete = configfs_d_delete,
7979
};
8080

81+
#ifdef CONFIG_LOCKDEP
82+
83+
/*
84+
* Helpers to make lockdep happy with our recursive locking of default groups'
85+
* inodes (see configfs_attach_group() and configfs_detach_group()).
86+
* We put default groups i_mutexes in separate classes according to their depth
87+
* from the youngest non-default group ancestor.
88+
*
89+
* For a non-default group A having default groups A/B, A/C, and A/C/D, default
90+
* groups A/B and A/C will have their inode's mutex in class
91+
* default_group_class[0], and default group A/C/D will be in
92+
* default_group_class[1].
93+
*
94+
* The lock classes are declared and assigned in inode.c, according to the
95+
* s_depth value.
96+
* The s_depth value is initialized to -1, adjusted to >= 0 when attaching
97+
* default groups, and reset to -1 when all default groups are attached. During
98+
* attachment, if configfs_create() sees s_depth > 0, the lock class of the new
99+
* inode's mutex is set to default_group_class[s_depth - 1].
100+
*/
101+
102+
static void configfs_init_dirent_depth(struct configfs_dirent *sd)
103+
{
104+
sd->s_depth = -1;
105+
}
106+
107+
static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
108+
struct configfs_dirent *sd)
109+
{
110+
int parent_depth = parent_sd->s_depth;
111+
112+
if (parent_depth >= 0)
113+
sd->s_depth = parent_depth + 1;
114+
}
115+
116+
static void
117+
configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
118+
{
119+
/*
120+
* item's i_mutex class is already setup, so s_depth is now only
121+
* used to set new sub-directories s_depth, which is always done
122+
* with item's i_mutex locked.
123+
*/
124+
/*
125+
* sd->s_depth == -1 iff we are a non default group.
126+
* else (we are a default group) sd->s_depth > 0 (see
127+
* create_dir()).
128+
*/
129+
if (sd->s_depth == -1)
130+
/*
131+
* We are a non default group and we are going to create
132+
* default groups.
133+
*/
134+
sd->s_depth = 0;
135+
}
136+
137+
static void
138+
configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
139+
{
140+
/* We will not create default groups anymore. */
141+
sd->s_depth = -1;
142+
}
143+
144+
#else /* CONFIG_LOCKDEP */
145+
146+
static void configfs_init_dirent_depth(struct configfs_dirent *sd)
147+
{
148+
}
149+
150+
static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
151+
struct configfs_dirent *sd)
152+
{
153+
}
154+
155+
static void
156+
configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
157+
{
158+
}
159+
160+
static void
161+
configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
162+
{
163+
}
164+
165+
#endif /* CONFIG_LOCKDEP */
166+
81167
/*
82168
* Allocates a new configfs_dirent and links it to the parent configfs_dirent
83169
*/
84-
static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * parent_sd,
85-
void * element)
170+
static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
171+
void *element, int type)
86172
{
87173
struct configfs_dirent * sd;
88174

@@ -94,6 +180,8 @@ static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * pare
94180
INIT_LIST_HEAD(&sd->s_links);
95181
INIT_LIST_HEAD(&sd->s_children);
96182
sd->s_element = element;
183+
sd->s_type = type;
184+
configfs_init_dirent_depth(sd);
97185
spin_lock(&configfs_dirent_lock);
98186
if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
99187
spin_unlock(&configfs_dirent_lock);
@@ -138,12 +226,11 @@ int configfs_make_dirent(struct configfs_dirent * parent_sd,
138226
{
139227
struct configfs_dirent * sd;
140228

141-
sd = configfs_new_dirent(parent_sd, element);
229+
sd = configfs_new_dirent(parent_sd, element, type);
142230
if (IS_ERR(sd))
143231
return PTR_ERR(sd);
144232

145233
sd->s_mode = mode;
146-
sd->s_type = type;
147234
sd->s_dentry = dentry;
148235
if (dentry) {
149236
dentry->d_fsdata = configfs_get(sd);
@@ -187,6 +274,7 @@ static int create_dir(struct config_item * k, struct dentry * p,
187274
error = configfs_make_dirent(p->d_fsdata, d, k, mode,
188275
CONFIGFS_DIR | CONFIGFS_USET_CREATING);
189276
if (!error) {
277+
configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata);
190278
error = configfs_create(d, mode, init_dir);
191279
if (!error) {
192280
inc_nlink(p->d_inode);
@@ -789,11 +877,13 @@ static int configfs_attach_group(struct config_item *parent_item,
789877
* error, as rmdir() would.
790878
*/
791879
mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
880+
configfs_adjust_dir_dirent_depth_before_populate(sd);
792881
ret = populate_groups(to_config_group(item));
793882
if (ret) {
794883
configfs_detach_item(item);
795884
dentry->d_inode->i_flags |= S_DEAD;
796885
}
886+
configfs_adjust_dir_dirent_depth_after_populate(sd);
797887
mutex_unlock(&dentry->d_inode->i_mutex);
798888
if (ret)
799889
d_delete(dentry);
@@ -916,11 +1006,11 @@ static int configfs_dump(struct configfs_dirent *sd, int level)
9161006
* Note, btw, that this can be called at *any* time, even when a configfs
9171007
* subsystem isn't registered, or when configfs is loading or unloading.
9181008
* Just like configfs_register_subsystem(). So we take the same
919-
* precautions. We pin the filesystem. We lock each i_mutex _in_order_
920-
* 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
9211011
* configfs tree, it must be part of the subsystem tree as well, so we
922-
* do not need the subsystem semaphore. Holding the i_mutex chain locks
923-
* 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.
9241014
*/
9251015

9261016
/*
@@ -933,17 +1023,21 @@ static int configfs_dump(struct configfs_dirent *sd, int level)
9331023
* do that so we can unlock it if we find nothing.
9341024
*
9351025
* Here we do a depth-first search of the dentry hierarchy looking for
936-
* our object. We take i_mutex on each step of the way down. IT IS
937-
* ESSENTIAL THAT i_mutex LOCKING IS ORDERED. If we come back up a branch,
938-
* 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.
9391035
*
940-
* If the target is not found, -ENOENT is bubbled up and we have released
941-
* all locks. If the target was found, the locks will be cleared by
942-
* configfs_depend_rollback().
1036+
* If the target is not found, -ENOENT is bubbled up.
9431037
*
9441038
* This adds a requirement that all config_items be unique!
9451039
*
946-
* This is recursive because the locking traversal is tricky. There isn't
1040+
* This is recursive. There isn't
9471041
* much on the stack, though, so folks that need this function - be careful
9481042
* about your stack! Patches will be accepted to make it iterative.
9491043
*/
@@ -955,13 +1049,13 @@ static int configfs_depend_prep(struct dentry *origin,
9551049

9561050
BUG_ON(!origin || !sd);
9571051

958-
/* Lock this guy on the way down */
959-
mutex_lock(&sd->s_dentry->d_inode->i_mutex);
9601052
if (sd->s_element == target) /* Boo-yah */
9611053
goto out;
9621054

9631055
list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
964-
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)) {
9651059
ret = configfs_depend_prep(child_sd->s_dentry,
9661060
target);
9671061
if (!ret)
@@ -970,33 +1064,12 @@ static int configfs_depend_prep(struct dentry *origin,
9701064
}
9711065

9721066
/* We looped all our children and didn't find target */
973-
mutex_unlock(&sd->s_dentry->d_inode->i_mutex);
9741067
ret = -ENOENT;
9751068

9761069
out:
9771070
return ret;
9781071
}
9791072

980-
/*
981-
* This is ONLY called if configfs_depend_prep() did its job. So we can
982-
* trust the entire path from item back up to origin.
983-
*
984-
* We walk backwards from item, unlocking each i_mutex. We finish by
985-
* unlocking origin.
986-
*/
987-
static void configfs_depend_rollback(struct dentry *origin,
988-
struct config_item *item)
989-
{
990-
struct dentry *dentry = item->ci_dentry;
991-
992-
while (dentry != origin) {
993-
mutex_unlock(&dentry->d_inode->i_mutex);
994-
dentry = dentry->d_parent;
995-
}
996-
997-
mutex_unlock(&origin->d_inode->i_mutex);
998-
}
999-
10001073
int configfs_depend_item(struct configfs_subsystem *subsys,
10011074
struct config_item *target)
10021075
{
@@ -1037,17 +1110,21 @@ int configfs_depend_item(struct configfs_subsystem *subsys,
10371110

10381111
/* Ok, now we can trust subsys/s_item */
10391112

1040-
/* 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 */
10411115
ret = configfs_depend_prep(subsys_sd->s_dentry, target);
10421116
if (ret)
1043-
goto out_unlock_fs;
1117+
goto out_unlock_dirent_lock;
10441118

1045-
/* 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+
*/
10461123
p = target->ci_dentry->d_fsdata;
10471124
p->s_dependent_count += 1;
10481125

1049-
configfs_depend_rollback(subsys_sd->s_dentry, target);
1050-
1126+
out_unlock_dirent_lock:
1127+
spin_unlock(&configfs_dirent_lock);
10511128
out_unlock_fs:
10521129
mutex_unlock(&configfs_sb->s_root->d_inode->i_mutex);
10531130

@@ -1072,10 +1149,10 @@ void configfs_undepend_item(struct configfs_subsystem *subsys,
10721149
struct configfs_dirent *sd;
10731150

10741151
/*
1075-
* Since we can trust everything is pinned, we just need i_mutex
1076-
* on the item.
1152+
* Since we can trust everything is pinned, we just need
1153+
* configfs_dirent_lock.
10771154
*/
1078-
mutex_lock(&target->ci_dentry->d_inode->i_mutex);
1155+
spin_lock(&configfs_dirent_lock);
10791156

10801157
sd = target->ci_dentry->d_fsdata;
10811158
BUG_ON(sd->s_dependent_count < 1);
@@ -1086,7 +1163,7 @@ void configfs_undepend_item(struct configfs_subsystem *subsys,
10861163
* After this unlock, we cannot trust the item to stay alive!
10871164
* DO NOT REFERENCE item after this unlock.
10881165
*/
1089-
mutex_unlock(&target->ci_dentry->d_inode->i_mutex);
1166+
spin_unlock(&configfs_dirent_lock);
10901167
}
10911168
EXPORT_SYMBOL(configfs_undepend_item);
10921169

@@ -1286,13 +1363,6 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
12861363
if (sd->s_type & CONFIGFS_USET_DEFAULT)
12871364
return -EPERM;
12881365

1289-
/*
1290-
* Here's where we check for dependents. We're protected by
1291-
* i_mutex.
1292-
*/
1293-
if (sd->s_dependent_count)
1294-
return -EBUSY;
1295-
12961366
/* Get a working ref until we have the child */
12971367
parent_item = configfs_get_config_item(dentry->d_parent);
12981368
subsys = to_config_group(parent_item)->cg_subsys;
@@ -1316,9 +1386,17 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
13161386

13171387
mutex_lock(&configfs_symlink_mutex);
13181388
spin_lock(&configfs_dirent_lock);
1319-
ret = configfs_detach_prep(dentry, &wait_mutex);
1320-
if (ret)
1321-
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+
}
13221400
spin_unlock(&configfs_dirent_lock);
13231401
mutex_unlock(&configfs_symlink_mutex);
13241402

@@ -1429,7 +1507,7 @@ static int configfs_dir_open(struct inode *inode, struct file *file)
14291507
*/
14301508
err = -ENOENT;
14311509
if (configfs_dirent_is_ready(parent_sd)) {
1432-
file->private_data = configfs_new_dirent(parent_sd, NULL);
1510+
file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
14331511
if (IS_ERR(file->private_data))
14341512
err = PTR_ERR(file->private_data);
14351513
else

0 commit comments

Comments
 (0)