From 5da0bd56210100959332b1731e844c7ed7ece06b Mon Sep 17 00:00:00 2001 From: Sultan Alsawaf Date: Sat, 27 May 2023 15:37:39 -0700 Subject: [PATCH] fscrypt: Avoid dynamic memory allocation during impl selection When there's only one device, use an on-stack buffer to store the device pointers in order to avoid dynamic memory allocation from this hot path. Change-Id: Ib54e0a5de7f0168b6f246a85306a146d1384b39a Signed-off-by: Sultan Alsawaf Signed-off-by: Richard Raya --- fs/crypto/inline_crypt.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/fs/crypto/inline_crypt.c b/fs/crypto/inline_crypt.c index efbb74ca7588..741396ab2a84 100644 --- a/fs/crypto/inline_crypt.c +++ b/fs/crypto/inline_crypt.c @@ -104,6 +104,7 @@ int fscrypt_select_encryption_impl(struct fscrypt_info *ci, enum blk_crypto_mode_num crypto_mode = ci->ci_mode->blk_crypto_mode; unsigned int dun_bytes; struct request_queue **devs; + struct request_queue *devs_onstack; int num_devs; int i; @@ -145,10 +146,13 @@ int fscrypt_select_encryption_impl(struct fscrypt_info *ci, } num_devs = fscrypt_get_num_devices(sb); - devs = kmalloc_array(num_devs, sizeof(*devs), GFP_KERNEL); - if (!devs) - return -ENOMEM; - + if (num_devs == 1) { + devs = &devs_onstack; + } else { + devs = kmalloc_array(num_devs, sizeof(*devs), GFP_KERNEL); + if (!devs) + return -ENOMEM; + } fscrypt_get_devices(sb, num_devs, devs); dun_bytes = fscrypt_get_dun_bytes(ci); @@ -164,7 +168,9 @@ int fscrypt_select_encryption_impl(struct fscrypt_info *ci, ci->ci_inlinecrypt = true; out_free_devs: - kfree(devs); + if (devs != &devs_onstack) + kfree(devs); + return 0; }