Skip to content

Commit e74cc06

Browse files
Louis RillingJoel Becker
authored andcommitted
configfs: Silence lockdep on mkdir() and rmdir()
When attaching default groups (subdirs) of a new group (in mkdir() or in configfs_register()), configfs recursively takes inode's mutexes along the path from the parent of the new group to the default subdirs. This is needed to ensure that the VFS will not race with operations on these sub-dirs. This is safe for the following reasons: - the VFS allows one to lock first an inode and second one of its children (The lock subclasses for this pattern are respectively I_MUTEX_PARENT and I_MUTEX_CHILD); - from this rule any inode path can be recursively locked in descending order as long as it stays under a single mountpoint and does not follow symlinks. Unfortunately lockdep does not know (yet?) how to handle such recursion. I've tried to use Peter Zijlstra's lock_set_subclass() helper to upgrade i_mutexes from I_MUTEX_CHILD to I_MUTEX_PARENT when we know that we might recursively lock some of their descendant, but this usage does not seem to fit the purpose of lock_set_subclass() because it leads to several i_mutex locked with subclass I_MUTEX_PARENT by the same task. >From inside configfs it is not possible to serialize those recursive locking with a top-level one, because mkdir() and rmdir() are already called with inodes locked by the VFS. So using some mutex_lock_nest_lock() is not an option. I am proposing two solutions: 1) one that wraps recursive mutex_lock()s with lockdep_off()/lockdep_on(). 2) (as suggested earlier by Peter Zijlstra) one that puts the i_mutexes recursively locked in different classes based on their depth from the top-level config_group created. This induces an arbitrary limit (MAX_LOCK_DEPTH - 2 == 46) on the nesting of configfs default groups whenever lockdep is activated but this limit looks reasonably high. Unfortunately, this also isolates VFS operations on configfs default groups from the others and thus lowers the chances to detect locking issues. Nobody likes solution 1), which I can understand. This patch implements solution 2). However lockdep is still not happy with configfs_depend_item(). Next patch reworks the locking of configfs_depend_item() and finally makes lockdep happy. [ Note: This hides a few locking interactions with the VFS from lockdep. That was my big concern, because we like lockdep's protection. However, the current state always dumps a spurious warning. The locking is correct, so I tell people to ignore the warning and that we'll keep our eyes on the locking to make sure it stays correct. With this patch, we eliminate the warning. We do lose some of the lockdep protections, but this only means that we still have to keep our eyes on the locking. We're going to do that anyway. -- Joel ] Signed-off-by: Louis Rilling <louis.rilling@kerlabs.com> Signed-off-by: Joel Becker <joel.becker@oracle.com>
1 parent 3c48f23 commit e74cc06

3 files changed

Lines changed: 131 additions & 0 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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,92 @@ 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
*/
@@ -94,6 +180,7 @@ 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+
configfs_init_dirent_depth(sd);
97184
spin_lock(&configfs_dirent_lock);
98185
if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
99186
spin_unlock(&configfs_dirent_lock);
@@ -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);

‎fs/configfs/inode.c‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@
3333
#include <linux/backing-dev.h>
3434
#include <linux/capability.h>
3535
#include <linux/sched.h>
36+
#include <linux/lockdep.h>
3637

3738
#include <linux/configfs.h>
3839
#include "configfs_internal.h"
3940

41+
#ifdef CONFIG_LOCKDEP
42+
static struct lock_class_key default_group_class[MAX_LOCK_DEPTH];
43+
#endif
44+
4045
extern struct super_block * configfs_sb;
4146

4247
static const struct address_space_operations configfs_aops = {
@@ -150,6 +155,38 @@ struct inode * configfs_new_inode(mode_t mode, struct configfs_dirent * sd)
150155
return inode;
151156
}
152157

158+
#ifdef CONFIG_LOCKDEP
159+
160+
static void configfs_set_inode_lock_class(struct configfs_dirent *sd,
161+
struct inode *inode)
162+
{
163+
int depth = sd->s_depth;
164+
165+
if (depth > 0) {
166+
if (depth <= ARRAY_SIZE(default_group_class)) {
167+
lockdep_set_class(&inode->i_mutex,
168+
&default_group_class[depth - 1]);
169+
} else {
170+
/*
171+
* In practice the maximum level of locking depth is
172+
* already reached. Just inform about possible reasons.
173+
*/
174+
printk(KERN_INFO "configfs: Too many levels of inodes"
175+
" for the locking correctness validator.\n");
176+
printk(KERN_INFO "Spurious warnings may appear.\n");
177+
}
178+
}
179+
}
180+
181+
#else /* CONFIG_LOCKDEP */
182+
183+
static void configfs_set_inode_lock_class(struct configfs_dirent *sd,
184+
struct inode *inode)
185+
{
186+
}
187+
188+
#endif /* CONFIG_LOCKDEP */
189+
153190
int configfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
154191
{
155192
int error = 0;
@@ -162,6 +199,7 @@ int configfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *
162199
struct inode *p_inode = dentry->d_parent->d_inode;
163200
p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
164201
}
202+
configfs_set_inode_lock_class(sd, inode);
165203
goto Proceed;
166204
}
167205
else

0 commit comments

Comments
 (0)