add patch fix-ocfs2-corrupt-when-updating-journal-superblock-fails
[ext4-patch-queue.git] / crypto-use-slab-caches
blob75f354f061f3b00914d79d5a8331a6a391f88fc8
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>
7 ---
8  fs/ext4/crypto.c     | 60 +++++++++++++++++++++++++++++-------------------------------
9  fs/ext4/crypto_key.c | 12 +++++++++---
10  fs/ext4/ext4.h       |  1 +
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;
24  /**
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) {
29                 if (ctx->tfm)
30                         crypto_free_tfm(ctx->tfm);
31 -               kfree(ctx);
32 +               kmem_cache_free(ext4_crypto_ctx_cachep, ctx);
33         } else {
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)
37  }
39  /**
40 - * ext4_alloc_and_init_crypto_ctx() - Allocates and inits an encryption context
41 - * @mask: The allocation mask.
42 - *
43 - * Return: An allocated and initialized encryption context on success. An error
44 - * value or NULL otherwise.
45 - */
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),
49 -                                             mask);
51 -       if (!ctx)
52 -               return ERR_PTR(-ENOMEM);
53 -       return ctx;
56 -/**
57   * ext4_get_crypto_ctx() - Gets an encryption context
58   * @inode:       The inode for which we are doing the crypto
59   *
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;
63         BUG_ON(ci == NULL);
64 -       if (!ext4_read_workqueue)
65 -               ext4_init_crypto();
67         /*
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);
72         if (!ctx) {
73 -               ctx = ext4_alloc_and_init_crypto_ctx(GFP_NOFS);
74 -               if (IS_ERR(ctx)) {
75 -                       res = PTR_ERR(ctx);
76 +               ctx = kmem_cache_zalloc(ext4_crypto_ctx_cachep, GFP_NOFS);
77 +               if (!ctx) {
78 +                       res = -ENOMEM;
79                         goto out;
80                 }
81                 ctx->flags |= EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
82 @@ -217,7 +201,7 @@ void ext4_exit_crypto(void)
83                 }
84                 if (pos->tfm)
85                         crypto_free_tfm(pos->tfm);
86 -               kfree(pos);
87 +               kmem_cache_free(ext4_crypto_ctx_cachep, pos);
88         }
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;
103  /**
104 @@ -238,23 +228,31 @@ void ext4_exit_crypto(void)
105   */
106  int ext4_init_crypto(void)
108 -       int i, res;
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) {
116 -               res = -ENOMEM;
117 +       if (!ext4_read_workqueue)
118 +               goto fail;
120 +       ext4_crypto_ctx_cachep = KMEM_CACHE(ext4_crypto_ctx,
121 +                                           SLAB_RECLAIM_ACCOUNT);
122 +       if (!ext4_crypto_ctx_cachep)
123 +               goto fail;
125 +       ext4_crypt_info_cachep = KMEM_CACHE(ext4_crypt_info,
126 +                                           SLAB_RECLAIM_ACCOUNT);
127 +       if (!ext4_crypt_info_cachep)
128                 goto fail;
129 -       }
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);
135 -               if (IS_ERR(ctx)) {
136 -                       res = PTR_ERR(ctx);
137 +               ctx = kmem_cache_zalloc(ext4_crypto_ctx_cachep, GFP_NOFS);
138 +               if (!ctx) {
139 +                       res = -ENOMEM;
140                         goto fail;
141                 }
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));
151 -       kfree(ci);
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);
158         int res;
160 +       if (!ext4_read_workqueue) {
161 +               res = ext4_init_crypto();
162 +               if (res)
163 +                       return res;
164 +       }
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)
170                 return -EINVAL;
171         res = 0;
173 -       crypt_info = kmalloc(sizeof(struct ext4_crypt_info), GFP_KERNEL);
174 +       crypt_info = kmem_cache_alloc(ext4_crypt_info_cachep, GFP_KERNEL);
175         if (!crypt_info)
176                 return -ENOMEM;
178 @@ -188,7 +194,7 @@ out:
179         if (res < 0) {
180                 if (res == -ENOKEY)
181                         res = 0;
182 -               kfree(crypt_info);
183 +               kmem_cache_free(ext4_crypt_info_cachep, crypt_info);
184         } else {
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
189 --- a/fs/ext4/ext4.h
190 +++ b/fs/ext4/ext4.h
191 @@ -2059,6 +2059,7 @@ int ext4_get_policy(struct inode *inode,
192                     struct ext4_encryption_policy *policy);
194  /* crypto.c */
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;