Skip to content

Commit fc883e7

Browse files
committed
Merge tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/linux
Pull fscrypt update from Eric Biggers: "Improve the performance of opening unencrypted files on filesystems that support fscrypt" * tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/linux: fscrypt: try to avoid refing parent dentry in fscrypt_file_open
2 parents eafb55a + 7f016ed commit fc883e7

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

fs/crypto/hooks.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,41 @@
3030
int fscrypt_file_open(struct inode *inode, struct file *filp)
3131
{
3232
int err;
33-
struct dentry *dir;
33+
struct dentry *dentry, *dentry_parent;
34+
struct inode *inode_parent;
3435

3536
err = fscrypt_require_key(inode);
3637
if (err)
3738
return err;
3839

39-
dir = dget_parent(file_dentry(filp));
40-
if (IS_ENCRYPTED(d_inode(dir)) &&
41-
!fscrypt_has_permitted_context(d_inode(dir), inode)) {
40+
dentry = file_dentry(filp);
41+
42+
/*
43+
* Getting a reference to the parent dentry is needed for the actual
44+
* encryption policy comparison, but it's expensive on multi-core
45+
* systems. Since this function runs on unencrypted files too, start
46+
* with a lightweight RCU-mode check for the parent directory being
47+
* unencrypted (in which case it's fine for the child to be either
48+
* unencrypted, or encrypted with any policy). Only continue on to the
49+
* full policy check if the parent directory is actually encrypted.
50+
*/
51+
rcu_read_lock();
52+
dentry_parent = READ_ONCE(dentry->d_parent);
53+
inode_parent = d_inode_rcu(dentry_parent);
54+
if (inode_parent != NULL && !IS_ENCRYPTED(inode_parent)) {
55+
rcu_read_unlock();
56+
return 0;
57+
}
58+
rcu_read_unlock();
59+
60+
dentry_parent = dget_parent(dentry);
61+
if (!fscrypt_has_permitted_context(d_inode(dentry_parent), inode)) {
4262
fscrypt_warn(inode,
4363
"Inconsistent encryption context (parent directory: %lu)",
44-
d_inode(dir)->i_ino);
64+
d_inode(dentry_parent)->i_ino);
4565
err = -EPERM;
4666
}
47-
dput(dir);
67+
dput(dentry_parent);
4868
return err;
4969
}
5070
EXPORT_SYMBOL_GPL(fscrypt_file_open);

0 commit comments

Comments
 (0)