[PATCH] fscrypt: improve filename encryption and decryption performance
From: Yuwen Chen
Date: Thu Jul 03 2025 - 02:28:42 EST
With the CONFIG_UNICODE configuration enabled, the fname_decrypt
and fscrypt_fname_encrypt functions may be called very frequently.
Since filenames are generally short, the frequent invocation of
memory allocation and release operations by these two functions
will lead to very poor performance.
Signed-off-by: Yuwen Chen <ywen.chen@xxxxxxxxxxx>
---
fs/crypto/fname.c | 11 +++--------
include/crypto/skcipher.h | 9 +++++++++
2 files changed, 12 insertions(+), 8 deletions(-)
diff --git a/fs/crypto/fname.c b/fs/crypto/fname.c
index 010f9c0a4c2f1..a3cc30ff3586b 100644
--- a/fs/crypto/fname.c
+++ b/fs/crypto/fname.c
@@ -77,6 +77,7 @@ static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
return is_dot_dotdot(str->name, str->len);
}
+#define MAX_SKCIPHER_REQSIZE (384)
/**
* fscrypt_fname_encrypt() - encrypt a filename
* @inode: inode of the parent directory (for regular filenames)
@@ -92,10 +93,10 @@ static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
u8 *out, unsigned int olen)
{
- struct skcipher_request *req = NULL;
DECLARE_CRYPTO_WAIT(wait);
const struct fscrypt_inode_info *ci = inode->i_crypt_info;
struct crypto_skcipher *tfm = ci->ci_enc_key.tfm;
+ SKCIPHER_REQUEST_ON_STACK(req, tfm, MAX_SKCIPHER_REQSIZE);
union fscrypt_iv iv;
struct scatterlist sg;
int res;
@@ -124,7 +125,6 @@ int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
/* Do the encryption */
res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
- skcipher_request_free(req);
if (res < 0) {
fscrypt_err(inode, "Filename encryption failed: %d", res);
return res;
@@ -148,18 +148,14 @@ static int fname_decrypt(const struct inode *inode,
const struct fscrypt_str *iname,
struct fscrypt_str *oname)
{
- struct skcipher_request *req = NULL;
DECLARE_CRYPTO_WAIT(wait);
struct scatterlist src_sg, dst_sg;
const struct fscrypt_inode_info *ci = inode->i_crypt_info;
struct crypto_skcipher *tfm = ci->ci_enc_key.tfm;
+ SKCIPHER_REQUEST_ON_STACK(req, tfm, MAX_SKCIPHER_REQSIZE);
union fscrypt_iv iv;
int res;
- /* Allocate request */
- req = skcipher_request_alloc(tfm, GFP_NOFS);
- if (!req)
- return -ENOMEM;
skcipher_request_set_callback(req,
CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
crypto_req_done, &wait);
@@ -172,7 +168,6 @@ static int fname_decrypt(const struct inode *inode,
sg_init_one(&dst_sg, oname->name, oname->len);
skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, &iv);
res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
- skcipher_request_free(req);
if (res < 0) {
fscrypt_err(inode, "Filename decryption failed: %d", res);
return res;
diff --git a/include/crypto/skcipher.h b/include/crypto/skcipher.h
index 9e5853464345b..ff2c5ac9252ff 100644
--- a/include/crypto/skcipher.h
+++ b/include/crypto/skcipher.h
@@ -226,6 +226,15 @@ struct lskcipher_alg {
crypto_sync_skcipher_tfm((_tfm)), \
(void *)__##name##_desc)
+
+#define SKCIPHER_REQUEST_ON_STACK(name, _tfm, reqsize) \
+ char __##name##_desc[sizeof(struct skcipher_request) + reqsize \
+ ] CRYPTO_MINALIGN_ATTR; \
+ struct skcipher_request *name = \
+ (((struct skcipher_request *)__##name##_desc)->base.tfm = \
+ crypto_skcipher_tfm((_tfm)), \
+ (void *)__##name##_desc)
+
/**
* DOC: Symmetric Key Cipher API
*
--
2.34.1