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 <sultan@kerneltoast.com>
Signed-off-by: Richard Raya <rdxzv.dev@gmail.com>
This commit is contained in:
Sultan Alsawaf 2023-05-27 15:37:39 -07:00 committed by Richard Raya
parent 3b491a454b
commit 5da0bd5621

View File

@ -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;
}