1 ext4 crypto: use slab caches
3 Use slab caches the ext4_crypto_ctx and ext4_crypt_info structures for
4 slighly better memory efficiency and debuggability.
6 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
8 fs/ext4/crypto.c | 60 +++++++++++++++++++++++++++++-------------------------------
9 fs/ext4/crypto_key.c | 12 +++++++++---
11 3 files changed, 39 insertions(+), 34 deletions(-)
13 diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
14 index 3a25aa4..1c34f0e 100644
15 --- a/fs/ext4/crypto.c
16 +++ b/fs/ext4/crypto.c
17 @@ -55,6 +55,9 @@ static mempool_t *ext4_bounce_page_pool;
18 static LIST_HEAD(ext4_free_crypto_ctxs);
19 static DEFINE_SPINLOCK(ext4_crypto_ctx_lock);
21 +static struct kmem_cache *ext4_crypto_ctx_cachep;
22 +struct kmem_cache *ext4_crypt_info_cachep;
25 * ext4_release_crypto_ctx() - Releases an encryption context
26 * @ctx: The encryption context to release.
27 @@ -79,7 +82,7 @@ void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx)
28 if (ctx->flags & EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL) {
30 crypto_free_tfm(ctx->tfm);
32 + kmem_cache_free(ext4_crypto_ctx_cachep, ctx);
34 spin_lock_irqsave(&ext4_crypto_ctx_lock, flags);
35 list_add(&ctx->free_list, &ext4_free_crypto_ctxs);
36 @@ -88,23 +91,6 @@ void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx)
40 - * ext4_alloc_and_init_crypto_ctx() - Allocates and inits an encryption context
41 - * @mask: The allocation mask.
43 - * Return: An allocated and initialized encryption context on success. An error
44 - * value or NULL otherwise.
46 -static struct ext4_crypto_ctx *ext4_alloc_and_init_crypto_ctx(gfp_t mask)
48 - struct ext4_crypto_ctx *ctx = kzalloc(sizeof(struct ext4_crypto_ctx),
52 - return ERR_PTR(-ENOMEM);
57 * ext4_get_crypto_ctx() - Gets an encryption context
58 * @inode: The inode for which we are doing the crypto
60 @@ -121,8 +107,6 @@ struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode)
61 struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
64 - if (!ext4_read_workqueue)
68 * We first try getting the ctx from a free list because in
69 @@ -141,9 +125,9 @@ struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode)
70 list_del(&ctx->free_list);
71 spin_unlock_irqrestore(&ext4_crypto_ctx_lock, flags);
73 - ctx = ext4_alloc_and_init_crypto_ctx(GFP_NOFS);
76 + ctx = kmem_cache_zalloc(ext4_crypto_ctx_cachep, GFP_NOFS);
81 ctx->flags |= EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
82 @@ -217,7 +201,7 @@ void ext4_exit_crypto(void)
85 crypto_free_tfm(pos->tfm);
87 + kmem_cache_free(ext4_crypto_ctx_cachep, pos);
89 INIT_LIST_HEAD(&ext4_free_crypto_ctxs);
90 if (ext4_bounce_page_pool)
91 @@ -226,6 +210,12 @@ void ext4_exit_crypto(void)
92 if (ext4_read_workqueue)
93 destroy_workqueue(ext4_read_workqueue);
94 ext4_read_workqueue = NULL;
95 + if (ext4_crypto_ctx_cachep)
96 + kmem_cache_destroy(ext4_crypto_ctx_cachep);
97 + ext4_crypto_ctx_cachep = NULL;
98 + if (ext4_crypt_info_cachep)
99 + kmem_cache_destroy(ext4_crypt_info_cachep);
100 + ext4_crypt_info_cachep = NULL;
104 @@ -238,23 +228,31 @@ void ext4_exit_crypto(void)
106 int ext4_init_crypto(void)
109 + int i, res = -ENOMEM;
111 mutex_lock(&crypto_init);
112 if (ext4_read_workqueue)
113 goto already_initialized;
114 ext4_read_workqueue = alloc_workqueue("ext4_crypto", WQ_HIGHPRI, 0);
115 - if (!ext4_read_workqueue) {
117 + if (!ext4_read_workqueue)
120 + ext4_crypto_ctx_cachep = KMEM_CACHE(ext4_crypto_ctx,
121 + SLAB_RECLAIM_ACCOUNT);
122 + if (!ext4_crypto_ctx_cachep)
125 + ext4_crypt_info_cachep = KMEM_CACHE(ext4_crypt_info,
126 + SLAB_RECLAIM_ACCOUNT);
127 + if (!ext4_crypt_info_cachep)
131 for (i = 0; i < num_prealloc_crypto_ctxs; i++) {
132 struct ext4_crypto_ctx *ctx;
134 - ctx = ext4_alloc_and_init_crypto_ctx(GFP_KERNEL);
136 - res = PTR_ERR(ctx);
137 + ctx = kmem_cache_zalloc(ext4_crypto_ctx_cachep, GFP_NOFS);
142 list_add(&ctx->free_list, &ext4_free_crypto_ctxs);
143 diff --git a/fs/ext4/crypto_key.c b/fs/ext4/crypto_key.c
144 index 0075e43..d6abe46 100644
145 --- a/fs/ext4/crypto_key.c
146 +++ b/fs/ext4/crypto_key.c
147 @@ -96,7 +96,7 @@ void ext4_free_encryption_info(struct inode *inode)
148 key_put(ci->ci_keyring_key);
149 crypto_free_ablkcipher(ci->ci_ctfm);
150 memzero_explicit(&ci->ci_raw, sizeof(ci->ci_raw));
152 + kmem_cache_free(ext4_crypt_info_cachep, ci);
153 ei->i_crypt_info = NULL;
156 @@ -113,6 +113,12 @@ int _ext4_get_encryption_info(struct inode *inode)
157 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
160 + if (!ext4_read_workqueue) {
161 + res = ext4_init_crypto();
166 if (ei->i_crypt_info) {
167 if (!ei->i_crypt_info->ci_keyring_key ||
168 key_validate(ei->i_crypt_info->ci_keyring_key) == 0)
169 @@ -134,7 +140,7 @@ int _ext4_get_encryption_info(struct inode *inode)
173 - crypt_info = kmalloc(sizeof(struct ext4_crypt_info), GFP_KERNEL);
174 + crypt_info = kmem_cache_alloc(ext4_crypt_info_cachep, GFP_KERNEL);
178 @@ -188,7 +194,7 @@ out:
183 + kmem_cache_free(ext4_crypt_info_cachep, crypt_info);
185 ei->i_crypt_info = crypt_info;
186 crypt_info->ci_keyring_key = keyring_key;
187 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
188 index 4b589fe..9c2b48cd 100644
191 @@ -2059,6 +2059,7 @@ int ext4_get_policy(struct inode *inode,
192 struct ext4_encryption_policy *policy);
195 +extern struct kmem_cache *ext4_crypt_info_cachep;
196 bool ext4_valid_contents_enc_mode(uint32_t mode);
197 uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size);
198 extern struct workqueue_struct *ext4_read_workqueue;