Skip to content

Commit 5467a68

Browse files
author
Al Viro
committed
dcache: sort the freeing-without-RCU-delay mess for good.
For lockless accesses to dentries we don't have pinned we rely (among other things) upon having an RCU delay between dropping the last reference and actually freeing the memory. On the other hand, for things like pipes and sockets we neither do that kind of lockless access, nor want to deal with the overhead of an RCU delay every time a socket gets closed. So delay was made optional - setting DCACHE_RCUACCESS in ->d_flags made sure it would happen. We tried to avoid setting it unless we knew we need it. Unfortunately, that had led to recurring class of bugs, in which we missed the need to set it. We only really need it for dentries that are created by d_alloc_pseudo(), so let's not bother with trying to be smart - just make having an RCU delay the default. The ones that do *not* get it set the replacement flag (DCACHE_NORCU) and we'd better use that sparingly. d_alloc_pseudo() is the only such user right now. FWIW, the race that finally prompted that switch had been between __lock_parent() of immediate subdirectory of what's currently the root of a disconnected tree (e.g. from open-by-handle in progress) racing with d_splice_alias() elsewhere picking another alias for the same inode, either on outright corrupted fs image, or (in case of open-by-handle on NFS) that subdirectory having been just moved on server. It's not easy to hit, so the sky is not falling, but that's not the first race on similar missed cases and the logics for settinf DCACHE_RCUACCESS has gotten ridiculously convoluted. Cc: stable@vger.kernel.org Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 9419a31 commit 5467a68

4 files changed

Lines changed: 20 additions & 14 deletions

File tree

‎Documentation/filesystems/porting‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,8 @@ in your dentry operations instead.
638638
inode to d_splice_alias() will also do the right thing (equivalent of
639639
d_add(dentry, NULL); return NULL;), so that kind of special cases
640640
also doesn't need a separate treatment.
641+
--
642+
[mandatory]
643+
DCACHE_RCUACCESS is gone; having an RCU delay on dentry freeing is the
644+
default. DCACHE_NORCU opts out, and only d_alloc_pseudo() has any
645+
business doing so.

‎fs/dcache.c‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ static void dentry_free(struct dentry *dentry)
344344
}
345345
}
346346
/* if dentry was never visible to RCU, immediate free is OK */
347-
if (!(dentry->d_flags & DCACHE_RCUACCESS))
347+
if (dentry->d_flags & DCACHE_NORCU)
348348
__d_free(&dentry->d_u.d_rcu);
349349
else
350350
call_rcu(&dentry->d_u.d_rcu, __d_free);
@@ -1701,7 +1701,6 @@ struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
17011701
struct dentry *dentry = __d_alloc(parent->d_sb, name);
17021702
if (!dentry)
17031703
return NULL;
1704-
dentry->d_flags |= DCACHE_RCUACCESS;
17051704
spin_lock(&parent->d_lock);
17061705
/*
17071706
* don't need child lock because it is not subject
@@ -1726,7 +1725,7 @@ struct dentry *d_alloc_cursor(struct dentry * parent)
17261725
{
17271726
struct dentry *dentry = d_alloc_anon(parent->d_sb);
17281727
if (dentry) {
1729-
dentry->d_flags |= DCACHE_RCUACCESS | DCACHE_DENTRY_CURSOR;
1728+
dentry->d_flags |= DCACHE_DENTRY_CURSOR;
17301729
dentry->d_parent = dget(parent);
17311730
}
17321731
return dentry;
@@ -1739,10 +1738,17 @@ struct dentry *d_alloc_cursor(struct dentry * parent)
17391738
*
17401739
* For a filesystem that just pins its dentries in memory and never
17411740
* performs lookups at all, return an unhashed IS_ROOT dentry.
1741+
* This is used for pipes, sockets et.al. - the stuff that should
1742+
* never be anyone's children or parents. Unlike all other
1743+
* dentries, these will not have RCU delay between dropping the
1744+
* last reference and freeing them.
17421745
*/
17431746
struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
17441747
{
1745-
return __d_alloc(sb, name);
1748+
struct dentry *dentry = __d_alloc(sb, name);
1749+
if (likely(dentry))
1750+
dentry->d_flags |= DCACHE_NORCU;
1751+
return dentry;
17461752
}
17471753
EXPORT_SYMBOL(d_alloc_pseudo);
17481754

@@ -1911,12 +1917,10 @@ struct dentry *d_make_root(struct inode *root_inode)
19111917

19121918
if (root_inode) {
19131919
res = d_alloc_anon(root_inode->i_sb);
1914-
if (res) {
1915-
res->d_flags |= DCACHE_RCUACCESS;
1920+
if (res)
19161921
d_instantiate(res, root_inode);
1917-
} else {
1922+
else
19181923
iput(root_inode);
1919-
}
19201924
}
19211925
return res;
19221926
}
@@ -2781,9 +2785,7 @@ static void __d_move(struct dentry *dentry, struct dentry *target,
27812785
copy_name(dentry, target);
27822786
target->d_hash.pprev = NULL;
27832787
dentry->d_parent->d_lockref.count++;
2784-
if (dentry == old_parent)
2785-
dentry->d_flags |= DCACHE_RCUACCESS;
2786-
else
2788+
if (dentry != old_parent) /* wasn't IS_ROOT */
27872789
WARN_ON(!--old_parent->d_lockref.count);
27882790
} else {
27892791
target->d_parent = old_parent;

‎fs/nsfs.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,12 @@ static void *__ns_get_path(struct path *path, struct ns_common *ns)
8585
inode->i_fop = &ns_file_operations;
8686
inode->i_private = ns;
8787

88-
dentry = d_alloc_pseudo(mnt->mnt_sb, &empty_name);
88+
dentry = d_alloc_anon(mnt->mnt_sb);
8989
if (!dentry) {
9090
iput(inode);
9191
return ERR_PTR(-ENOMEM);
9292
}
9393
d_instantiate(dentry, inode);
94-
dentry->d_flags |= DCACHE_RCUACCESS;
9594
dentry->d_fsdata = (void *)ns->ops;
9695
d = atomic_long_cmpxchg(&ns->stashed, 0, (unsigned long)dentry);
9796
if (d) {

‎include/linux/dcache.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ struct dentry_operations {
176176
* typically using d_splice_alias. */
177177

178178
#define DCACHE_REFERENCED 0x00000040 /* Recently used, don't discard. */
179-
#define DCACHE_RCUACCESS 0x00000080 /* Entry has ever been RCU-visible */
180179

181180
#define DCACHE_CANT_MOUNT 0x00000100
182181
#define DCACHE_GENOCIDE 0x00000200
@@ -217,6 +216,7 @@ struct dentry_operations {
217216

218217
#define DCACHE_PAR_LOOKUP 0x10000000 /* being looked up (with parent locked shared) */
219218
#define DCACHE_DENTRY_CURSOR 0x20000000
219+
#define DCACHE_NORCU 0x40000000 /* No RCU delay for freeing */
220220

221221
extern seqlock_t rename_lock;
222222

0 commit comments

Comments
 (0)