Skip to content

Commit 84208b8

Browse files
committed
Merge patch series "symlink length caching"
Mateusz Guzik <[email protected]> says: quote: When utilized it dodges strlen() in vfs_readlink(), giving about 1.5% speed up when issuing readlink on /initrd.img on ext4. The size is stored in a union with i_devices, which is never looked at unless the inode is for a device. ext4 and tmpfs are patched, other filesystems can also get there with some more work. benchmark: plug into will-it-scale into tests/readlink1.c: char *testcase_description = "readlink /initrd.img"; void testcase(unsigned long long *iterations, unsigned long nr) { char *tmplink = "/initrd.img"; char buf[1024]; while (1) { int error = readlink(tmplink, buf, sizeof(buf)); assert(error > 0); (*iterations)++; } } * patches from https://lore.kernel.org/r/[email protected]: tmpfs: use inode_set_cached_link() ext4: use inode_set_cached_link() vfs: support caching symlink lengths in inodes Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
2 parents 135ec43 + 657e726 commit 84208b8

File tree

7 files changed

+43
-23
lines changed

7 files changed

+43
-23
lines changed

fs/ext4/inode.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5006,10 +5006,11 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
50065006
if (IS_ENCRYPTED(inode)) {
50075007
inode->i_op = &ext4_encrypted_symlink_inode_operations;
50085008
} else if (ext4_inode_is_fast_symlink(inode)) {
5009-
inode->i_link = (char *)ei->i_data;
50105009
inode->i_op = &ext4_fast_symlink_inode_operations;
50115010
nd_terminate_link(ei->i_data, inode->i_size,
50125011
sizeof(ei->i_data) - 1);
5012+
inode_set_cached_link(inode, (char *)ei->i_data,
5013+
inode->i_size);
50135014
} else {
50145015
inode->i_op = &ext4_symlink_inode_operations;
50155016
}

fs/ext4/namei.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3418,7 +3418,6 @@ static int ext4_symlink(struct mnt_idmap *idmap, struct inode *dir,
34183418
inode->i_op = &ext4_symlink_inode_operations;
34193419
} else {
34203420
inode->i_op = &ext4_fast_symlink_inode_operations;
3421-
inode->i_link = (char *)&EXT4_I(inode)->i_data;
34223421
}
34233422
}
34243423

@@ -3434,6 +3433,9 @@ static int ext4_symlink(struct mnt_idmap *idmap, struct inode *dir,
34343433
disk_link.len);
34353434
inode->i_size = disk_link.len - 1;
34363435
EXT4_I(inode)->i_disksize = inode->i_size;
3436+
if (!IS_ENCRYPTED(inode))
3437+
inode_set_cached_link(inode, (char *)&EXT4_I(inode)->i_data,
3438+
inode->i_size);
34373439
}
34383440
err = ext4_add_nondir(handle, dentry, &inode);
34393441
if (handle)

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 *);

mm/shmem.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3914,6 +3914,7 @@ static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir,
39143914
int len;
39153915
struct inode *inode;
39163916
struct folio *folio;
3917+
char *link;
39173918

39183919
len = strlen(symname) + 1;
39193920
if (len > PAGE_SIZE)
@@ -3935,12 +3936,13 @@ static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir,
39353936

39363937
inode->i_size = len-1;
39373938
if (len <= SHORT_SYMLINK_LEN) {
3938-
inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3939-
if (!inode->i_link) {
3939+
link = kmemdup(symname, len, GFP_KERNEL);
3940+
if (!link) {
39403941
error = -ENOMEM;
39413942
goto out_remove_offset;
39423943
}
39433944
inode->i_op = &shmem_short_symlink_operations;
3945+
inode_set_cached_link(inode, link, len - 1);
39443946
} else {
39453947
inode_nohighmem(inode);
39463948
inode->i_mapping->a_ops = &shmem_aops;

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)