Skip to content

Commit ea38219

Browse files
mjguzikbrauner
authored andcommitted
vfs: support caching symlink lengths in inodes
When utilized it dodges strlen() in vfs_readlink(), giving about 1.5% speed up when issuing readlink on /initrd.img on ext4. Filesystems opt in by calling inode_set_cached_link() when creating an inode. The size is stored in a new union utilizing the same space as i_devices, thus avoiding growing the struct or taking up any more space. Churn-wise the current readlink_copy() helper is patched to accept the size instead of calculating it. Signed-off-by: Mateusz Guzik <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 135ec43 commit ea38219

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

fs/namei.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5272,19 +5272,16 @@ SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newna
52725272
getname(newname), 0);
52735273
}
52745274

5275-
int readlink_copy(char __user *buffer, int buflen, const char *link)
5275+
int readlink_copy(char __user *buffer, int buflen, const char *link, int linklen)
52765276
{
5277-
int len = PTR_ERR(link);
5278-
if (IS_ERR(link))
5279-
goto out;
5277+
int copylen;
52805278

5281-
len = strlen(link);
5282-
if (len > (unsigned) buflen)
5283-
len = buflen;
5284-
if (copy_to_user(buffer, link, len))
5285-
len = -EFAULT;
5286-
out:
5287-
return len;
5279+
copylen = linklen;
5280+
if (unlikely(copylen > (unsigned) buflen))
5281+
copylen = buflen;
5282+
if (copy_to_user(buffer, link, copylen))
5283+
copylen = -EFAULT;
5284+
return copylen;
52885285
}
52895286

52905287
/**
@@ -5304,6 +5301,9 @@ int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
53045301
const char *link;
53055302
int res;
53065303

5304+
if (inode->i_opflags & IOP_CACHED_LINK)
5305+
return readlink_copy(buffer, buflen, inode->i_link, inode->i_linklen);
5306+
53075307
if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
53085308
if (unlikely(inode->i_op->readlink))
53095309
return inode->i_op->readlink(dentry, buffer, buflen);
@@ -5322,7 +5322,7 @@ int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
53225322
if (IS_ERR(link))
53235323
return PTR_ERR(link);
53245324
}
5325-
res = readlink_copy(buffer, buflen, link);
5325+
res = readlink_copy(buffer, buflen, link, strlen(link));
53265326
do_delayed_call(&done);
53275327
return res;
53285328
}
@@ -5391,10 +5391,14 @@ EXPORT_SYMBOL(page_put_link);
53915391

53925392
int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
53935393
{
5394+
const char *link;
5395+
int res;
5396+
53945397
DEFINE_DELAYED_CALL(done);
5395-
int res = readlink_copy(buffer, buflen,
5396-
page_get_link(dentry, d_inode(dentry),
5397-
&done));
5398+
link = page_get_link(dentry, d_inode(dentry), &done);
5399+
res = PTR_ERR(link);
5400+
if (!IS_ERR(link))
5401+
res = readlink_copy(buffer, buflen, link, strlen(link));
53985402
do_delayed_call(&done);
53995403
return res;
54005404
}

fs/proc/namespaces.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int bufl
8383
if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
8484
res = ns_get_name(name, sizeof(name), task, ns_ops);
8585
if (res >= 0)
86-
res = readlink_copy(buffer, buflen, name);
86+
res = readlink_copy(buffer, buflen, name, strlen(name));
8787
}
8888
put_task_struct(task);
8989
return res;

include/linux/fs.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ is_uncached_acl(struct posix_acl *acl)
626626
#define IOP_XATTR 0x0008
627627
#define IOP_DEFAULT_READLINK 0x0010
628628
#define IOP_MGTIME 0x0020
629+
#define IOP_CACHED_LINK 0x0040
629630

630631
/*
631632
* Keep mostly read-only and often accessed (especially for
@@ -723,7 +724,10 @@ struct inode {
723724
};
724725
struct file_lock_context *i_flctx;
725726
struct address_space i_data;
726-
struct list_head i_devices;
727+
union {
728+
struct list_head i_devices;
729+
int i_linklen;
730+
};
727731
union {
728732
struct pipe_inode_info *i_pipe;
729733
struct cdev *i_cdev;
@@ -749,6 +753,13 @@ struct inode {
749753
void *i_private; /* fs or device private pointer */
750754
} __randomize_layout;
751755

756+
static inline void inode_set_cached_link(struct inode *inode, char *link, int linklen)
757+
{
758+
inode->i_link = link;
759+
inode->i_linklen = linklen;
760+
inode->i_opflags |= IOP_CACHED_LINK;
761+
}
762+
752763
/*
753764
* Get bit address from inode->i_state to use with wait_var_event()
754765
* infrastructre.
@@ -3351,7 +3362,7 @@ extern const struct file_operations generic_ro_fops;
33513362

33523363
#define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
33533364

3354-
extern int readlink_copy(char __user *, int, const char *);
3365+
extern int readlink_copy(char __user *, int, const char *, int);
33553366
extern int page_readlink(struct dentry *, char __user *, int);
33563367
extern const char *page_get_link(struct dentry *, struct inode *,
33573368
struct delayed_call *);

security/apparmor/apparmorfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,7 @@ static int policy_readlink(struct dentry *dentry, char __user *buffer,
26122612
res = snprintf(name, sizeof(name), "%s:[%lu]", AAFS_NAME,
26132613
d_inode(dentry)->i_ino);
26142614
if (res > 0 && res < sizeof(name))
2615-
res = readlink_copy(buffer, buflen, name);
2615+
res = readlink_copy(buffer, buflen, name, strlen(name));
26162616
else
26172617
res = -ENOENT;
26182618

0 commit comments

Comments
 (0)