During path traversal, the generic_ci_match function may be called
multiple times. The number of memory allocations and releases
in it accounts for a relatively high proportion in the flamegraph.
This patch significantly reduces the number of memory allocations
in generic_ci_match through pre - allocation.

Signed-off-by: Yuwen Chen <ywen.c...@foxmail.com>
---
 fs/ext4/namei.c    |  2 +-
 fs/f2fs/dir.c      |  2 +-
 fs/libfs.c         | 33 ++++++++++++++++++++++++++++++---
 include/linux/fs.h |  8 +++++++-
 4 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index a178ac2294895..f235693bd71aa 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1443,7 +1443,7 @@ static bool ext4_match(struct inode *parent,
 
                return generic_ci_match(parent, fname->usr_fname,
                                        &fname->cf_name, de->name,
-                                       de->name_len) > 0;
+                                       de->name_len, NULL) > 0;
        }
 #endif
 
diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index c36b3b22bfffd..4c6611fbd9574 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -197,7 +197,7 @@ static inline int f2fs_match_name(const struct inode *dir,
        if (fname->cf_name.name)
                return generic_ci_match(dir, fname->usr_fname,
                                        &fname->cf_name,
-                                       de_name, de_name_len);
+                                       de_name, de_name_len, NULL);
 
 #endif
        f.usr_fname = fname->usr_fname;
diff --git a/fs/libfs.c b/fs/libfs.c
index 9ea0ecc325a81..293b605971bbf 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -1863,6 +1863,26 @@ static const struct dentry_operations 
generic_ci_dentry_ops = {
 #endif
 };
 
+#define DECRYPTED_NAME_PREALLOC_MIN_LEN 64
+static inline char *decrypted_name_prealloc_resize(
+               struct decrypted_name_prealloc *prealloc,
+               size_t wantlen)
+{
+       char *retbuf = NULL;
+
+       if (prealloc->name && wantlen <= prealloc->namelen)
+               return prealloc->name;
+
+       retbuf = kmalloc(wantlen + DECRYPTED_NAME_PREALLOC_MIN_LEN, GFP_KERNEL);
+       if (!retbuf)
+               return NULL;
+
+       kfree(prealloc->name);
+       prealloc->name = retbuf;
+       prealloc->namelen = wantlen + DECRYPTED_NAME_PREALLOC_MIN_LEN;
+       return retbuf;
+}
+
 /**
  * generic_ci_match() - Match a name (case-insensitively) with a dirent.
  * This is a filesystem helper for comparison with directory entries.
@@ -1873,6 +1893,7 @@ static const struct dentry_operations 
generic_ci_dentry_ops = {
  * @folded_name: Optional pre-folded name under lookup
  * @de_name: Dirent name.
  * @de_name_len: dirent name length.
+ * @prealloc: decrypted name memory buffer
  *
  * Test whether a case-insensitive directory entry matches the filename
  * being searched.  If @folded_name is provided, it is used instead of
@@ -1884,7 +1905,8 @@ static const struct dentry_operations 
generic_ci_dentry_ops = {
 int generic_ci_match(const struct inode *parent,
                     const struct qstr *name,
                     const struct qstr *folded_name,
-                    const u8 *de_name, u32 de_name_len)
+                    const u8 *de_name, u32 de_name_len,
+                    struct decrypted_name_prealloc *prealloc)
 {
        const struct super_block *sb = parent->i_sb;
        const struct unicode_map *um = sb->s_encoding;
@@ -1899,7 +1921,11 @@ int generic_ci_match(const struct inode *parent,
                if (WARN_ON_ONCE(!fscrypt_has_encryption_key(parent)))
                        return -EINVAL;
 
-               decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
+               if (!prealloc)
+                       decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
+               else
+                       decrypted_name.name = decrypted_name_prealloc_resize(
+                                       prealloc, de_name_len);
                if (!decrypted_name.name)
                        return -ENOMEM;
                res = fscrypt_fname_disk_to_usr(parent, 0, 0, &encrypted_name,
@@ -1928,7 +1954,8 @@ int generic_ci_match(const struct inode *parent,
                res = utf8_strncasecmp(um, name, &dirent);
 
 out:
-       kfree(decrypted_name.name);
+       if (!prealloc)
+               kfree(decrypted_name.name);
        if (res < 0 && sb_has_strict_encoding(sb)) {
                pr_err_ratelimited("Directory contains filename that is invalid 
UTF-8");
                return 0;
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 4ec77da65f144..65307c8c11485 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3651,10 +3651,16 @@ extern int generic_file_fsync(struct file *, loff_t, 
loff_t, int);
 extern int generic_check_addressable(unsigned, u64);
 
 extern void generic_set_sb_d_ops(struct super_block *sb);
+
+struct decrypted_name_prealloc {
+       char *name;
+       size_t namelen;
+};
 extern int generic_ci_match(const struct inode *parent,
                            const struct qstr *name,
                            const struct qstr *folded_name,
-                           const u8 *de_name, u32 de_name_len);
+                           const u8 *de_name, u32 de_name_len,
+                           struct decrypted_name_prealloc *prealloc);
 
 #if IS_ENABLED(CONFIG_UNICODE)
 int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str);
-- 
2.34.1



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Reply via email to