Skip to content

Commit 83e57e4

Browse files
committed
fscrypt: optimize fscrypt_initialize()
fscrypt_initialize() is a "one-time init" function that is called whenever the key is set up for any inode on any filesystem. Make it implement "one-time init" more efficiently by not taking a global mutex in the "already initialized case" and doing fewer pointer dereferences. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent 41b2ad8 commit 83e57e4

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

fs/crypto/crypto.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,31 +308,36 @@ EXPORT_SYMBOL(fscrypt_decrypt_block_inplace);
308308

309309
/**
310310
* fscrypt_initialize() - allocate major buffers for fs encryption.
311-
* @cop_flags: fscrypt operations flags
311+
* @sb: the filesystem superblock
312312
*
313313
* We only call this when we start accessing encrypted files, since it
314314
* results in memory getting allocated that wouldn't otherwise be used.
315315
*
316316
* Return: 0 on success; -errno on failure
317317
*/
318-
int fscrypt_initialize(unsigned int cop_flags)
318+
int fscrypt_initialize(struct super_block *sb)
319319
{
320320
int err = 0;
321+
mempool_t *pool;
322+
323+
/* pairs with smp_store_release() below */
324+
if (likely(smp_load_acquire(&fscrypt_bounce_page_pool)))
325+
return 0;
321326

322327
/* No need to allocate a bounce page pool if this FS won't use it. */
323-
if (cop_flags & FS_CFLG_OWN_PAGES)
328+
if (sb->s_cop->flags & FS_CFLG_OWN_PAGES)
324329
return 0;
325330

326331
mutex_lock(&fscrypt_init_mutex);
327332
if (fscrypt_bounce_page_pool)
328333
goto out_unlock;
329334

330335
err = -ENOMEM;
331-
fscrypt_bounce_page_pool =
332-
mempool_create_page_pool(num_prealloc_crypto_pages, 0);
333-
if (!fscrypt_bounce_page_pool)
336+
pool = mempool_create_page_pool(num_prealloc_crypto_pages, 0);
337+
if (!pool)
334338
goto out_unlock;
335-
339+
/* pairs with smp_load_acquire() above */
340+
smp_store_release(&fscrypt_bounce_page_pool, pool);
336341
err = 0;
337342
out_unlock:
338343
mutex_unlock(&fscrypt_init_mutex);

fs/crypto/fscrypt_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ typedef enum {
264264

265265
/* crypto.c */
266266
extern struct kmem_cache *fscrypt_info_cachep;
267-
int fscrypt_initialize(unsigned int cop_flags);
267+
int fscrypt_initialize(struct super_block *sb);
268268
int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw,
269269
u64 lblk_num, struct page *src_page,
270270
struct page *dest_page, unsigned int len,

fs/crypto/keysetup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ fscrypt_setup_encryption_info(struct inode *inode,
560560
struct fscrypt_master_key *mk = NULL;
561561
int res;
562562

563-
res = fscrypt_initialize(inode->i_sb->s_cop->flags);
563+
res = fscrypt_initialize(inode->i_sb);
564564
if (res)
565565
return res;
566566

0 commit comments

Comments
 (0)