aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIlya Dryomov2016-12-02 09:35:08 -0600
committerGreg Kroah-Hartman2017-01-26 01:24:46 -0600
commitf77ef5348d4bfb498062930bd9af828f3ef2947e (patch)
tree3791a83093db585a63fd06169e74a18509e48372
parent5b482bf5886855be44e89653f88297aab423dee6 (diff)
downloadkernel-omap-f77ef5348d4bfb498062930bd9af828f3ef2947e.tar.gz
kernel-omap-f77ef5348d4bfb498062930bd9af828f3ef2947e.tar.xz
kernel-omap-f77ef5348d4bfb498062930bd9af828f3ef2947e.zip
libceph: stop allocating a new cipher on every crypto request
commit 7af3ea189a9a13f090de51c97f676215dabc1205 upstream. This is useless and more importantly not allowed on the writeback path, because crypto_alloc_skcipher() allocates memory with GFP_KERNEL, which can recurse back into the filesystem: kworker/9:3 D ffff92303f318180 0 20732 2 0x00000080 Workqueue: ceph-msgr ceph_con_workfn [libceph] ffff923035dd4480 ffff923038f8a0c0 0000000000000001 000000009eb27318 ffff92269eb28000 ffff92269eb27338 ffff923036b145ac ffff923035dd4480 00000000ffffffff ffff923036b145b0 ffffffff951eb4e1 ffff923036b145a8 Call Trace: [<ffffffff951eb4e1>] ? schedule+0x31/0x80 [<ffffffff951eb77a>] ? schedule_preempt_disabled+0xa/0x10 [<ffffffff951ed1f4>] ? __mutex_lock_slowpath+0xb4/0x130 [<ffffffff951ed28b>] ? mutex_lock+0x1b/0x30 [<ffffffffc0a974b3>] ? xfs_reclaim_inodes_ag+0x233/0x2d0 [xfs] [<ffffffff94d92ba5>] ? move_active_pages_to_lru+0x125/0x270 [<ffffffff94f2b985>] ? radix_tree_gang_lookup_tag+0xc5/0x1c0 [<ffffffff94dad0f3>] ? __list_lru_walk_one.isra.3+0x33/0x120 [<ffffffffc0a98331>] ? xfs_reclaim_inodes_nr+0x31/0x40 [xfs] [<ffffffff94e05bfe>] ? super_cache_scan+0x17e/0x190 [<ffffffff94d919f3>] ? shrink_slab.part.38+0x1e3/0x3d0 [<ffffffff94d9616a>] ? shrink_node+0x10a/0x320 [<ffffffff94d96474>] ? do_try_to_free_pages+0xf4/0x350 [<ffffffff94d967ba>] ? try_to_free_pages+0xea/0x1b0 [<ffffffff94d863bd>] ? __alloc_pages_nodemask+0x61d/0xe60 [<ffffffff94ddf42d>] ? cache_grow_begin+0x9d/0x560 [<ffffffff94ddfb88>] ? fallback_alloc+0x148/0x1c0 [<ffffffff94ed84e7>] ? __crypto_alloc_tfm+0x37/0x130 [<ffffffff94de09db>] ? __kmalloc+0x1eb/0x580 [<ffffffffc09fe2db>] ? crush_choose_firstn+0x3eb/0x470 [libceph] [<ffffffff94ed84e7>] ? __crypto_alloc_tfm+0x37/0x130 [<ffffffff94ed9c19>] ? crypto_spawn_tfm+0x39/0x60 [<ffffffffc08b30a3>] ? crypto_cbc_init_tfm+0x23/0x40 [cbc] [<ffffffff94ed857c>] ? __crypto_alloc_tfm+0xcc/0x130 [<ffffffff94edcc23>] ? crypto_skcipher_init_tfm+0x113/0x180 [<ffffffff94ed7cc3>] ? crypto_create_tfm+0x43/0xb0 [<ffffffff94ed83b0>] ? crypto_larval_lookup+0x150/0x150 [<ffffffff94ed7da2>] ? crypto_alloc_tfm+0x72/0x120 [<ffffffffc0a01dd7>] ? ceph_aes_encrypt2+0x67/0x400 [libceph] [<ffffffffc09fd264>] ? ceph_pg_to_up_acting_osds+0x84/0x5b0 [libceph] [<ffffffff950d40a0>] ? release_sock+0x40/0x90 [<ffffffff95139f94>] ? tcp_recvmsg+0x4b4/0xae0 [<ffffffffc0a02714>] ? ceph_encrypt2+0x54/0xc0 [libceph] [<ffffffffc0a02b4d>] ? ceph_x_encrypt+0x5d/0x90 [libceph] [<ffffffffc0a02bdf>] ? calcu_signature+0x5f/0x90 [libceph] [<ffffffffc0a02ef5>] ? ceph_x_sign_message+0x35/0x50 [libceph] [<ffffffffc09e948c>] ? prepare_write_message_footer+0x5c/0xa0 [libceph] [<ffffffffc09ecd18>] ? ceph_con_workfn+0x2258/0x2dd0 [libceph] [<ffffffffc09e9903>] ? queue_con_delay+0x33/0xd0 [libceph] [<ffffffffc09f68ed>] ? __submit_request+0x20d/0x2f0 [libceph] [<ffffffffc09f6ef8>] ? ceph_osdc_start_request+0x28/0x30 [libceph] [<ffffffffc0b52603>] ? rbd_queue_workfn+0x2f3/0x350 [rbd] [<ffffffff94c94ec0>] ? process_one_work+0x160/0x410 [<ffffffff94c951bd>] ? worker_thread+0x4d/0x480 [<ffffffff94c95170>] ? process_one_work+0x410/0x410 [<ffffffff94c9af8d>] ? kthread+0xcd/0xf0 [<ffffffff951efb2f>] ? ret_from_fork+0x1f/0x40 [<ffffffff94c9aec0>] ? kthread_create_on_node+0x190/0x190 Allocating the cipher along with the key fixes the issue - as long the key doesn't change, a single cipher context can be used concurrently in multiple requests. We still can't take that GFP_KERNEL allocation though. Both ceph_crypto_key_clone() and ceph_crypto_key_decode() are called from GFP_NOFS context, so resort to memalloc_noio_{save,restore}() here. Reported-by: Lucas Stach <l.stach@pengutronix.de> Signed-off-by: Ilya Dryomov <idryomov@gmail.com> Reviewed-by: Sage Weil <sage@redhat.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--net/ceph/crypto.c85
-rw-r--r--net/ceph/crypto.h1
2 files changed, 61 insertions, 25 deletions
diff --git a/net/ceph/crypto.c b/net/ceph/crypto.c
index f19f31eeeef9..292e33bd916e 100644
--- a/net/ceph/crypto.c
+++ b/net/ceph/crypto.c
@@ -13,14 +13,60 @@
13#include <linux/ceph/decode.h> 13#include <linux/ceph/decode.h>
14#include "crypto.h" 14#include "crypto.h"
15 15
16/*
17 * Set ->key and ->tfm. The rest of the key should be filled in before
18 * this function is called.
19 */
20static int set_secret(struct ceph_crypto_key *key, void *buf)
21{
22 unsigned int noio_flag;
23 int ret;
24
25 key->key = NULL;
26 key->tfm = NULL;
27
28 switch (key->type) {
29 case CEPH_CRYPTO_NONE:
30 return 0; /* nothing to do */
31 case CEPH_CRYPTO_AES:
32 break;
33 default:
34 return -ENOTSUPP;
35 }
36
37 WARN_ON(!key->len);
38 key->key = kmemdup(buf, key->len, GFP_NOIO);
39 if (!key->key) {
40 ret = -ENOMEM;
41 goto fail;
42 }
43
44 /* crypto_alloc_skcipher() allocates with GFP_KERNEL */
45 noio_flag = memalloc_noio_save();
46 key->tfm = crypto_alloc_skcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
47 memalloc_noio_restore(noio_flag);
48 if (IS_ERR(key->tfm)) {
49 ret = PTR_ERR(key->tfm);
50 key->tfm = NULL;
51 goto fail;
52 }
53
54 ret = crypto_skcipher_setkey(key->tfm, key->key, key->len);
55 if (ret)
56 goto fail;
57
58 return 0;
59
60fail:
61 ceph_crypto_key_destroy(key);
62 return ret;
63}
64
16int ceph_crypto_key_clone(struct ceph_crypto_key *dst, 65int ceph_crypto_key_clone(struct ceph_crypto_key *dst,
17 const struct ceph_crypto_key *src) 66 const struct ceph_crypto_key *src)
18{ 67{
19 memcpy(dst, src, sizeof(struct ceph_crypto_key)); 68 memcpy(dst, src, sizeof(struct ceph_crypto_key));
20 dst->key = kmemdup(src->key, src->len, GFP_NOFS); 69 return set_secret(dst, src->key);
21 if (!dst->key)
22 return -ENOMEM;
23 return 0;
24} 70}
25 71
26int ceph_crypto_key_encode(struct ceph_crypto_key *key, void **p, void *end) 72int ceph_crypto_key_encode(struct ceph_crypto_key *key, void **p, void *end)
@@ -37,16 +83,16 @@ int ceph_crypto_key_encode(struct ceph_crypto_key *key, void **p, void *end)
37 83
38int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end) 84int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end)
39{ 85{
86 int ret;
87
40 ceph_decode_need(p, end, 2*sizeof(u16) + sizeof(key->created), bad); 88 ceph_decode_need(p, end, 2*sizeof(u16) + sizeof(key->created), bad);
41 key->type = ceph_decode_16(p); 89 key->type = ceph_decode_16(p);
42 ceph_decode_copy(p, &key->created, sizeof(key->created)); 90 ceph_decode_copy(p, &key->created, sizeof(key->created));
43 key->len = ceph_decode_16(p); 91 key->len = ceph_decode_16(p);
44 ceph_decode_need(p, end, key->len, bad); 92 ceph_decode_need(p, end, key->len, bad);
45 key->key = kmalloc(key->len, GFP_NOFS); 93 ret = set_secret(key, *p);
46 if (!key->key) 94 *p += key->len;
47 return -ENOMEM; 95 return ret;
48 ceph_decode_copy(p, key->key, key->len);
49 return 0;
50 96
51bad: 97bad:
52 dout("failed to decode crypto key\n"); 98 dout("failed to decode crypto key\n");
@@ -85,14 +131,11 @@ void ceph_crypto_key_destroy(struct ceph_crypto_key *key)
85 if (key) { 131 if (key) {
86 kfree(key->key); 132 kfree(key->key);
87 key->key = NULL; 133 key->key = NULL;
134 crypto_free_skcipher(key->tfm);
135 key->tfm = NULL;
88 } 136 }
89} 137}
90 138
91static struct crypto_skcipher *ceph_crypto_alloc_cipher(void)
92{
93 return crypto_alloc_skcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
94}
95
96static const u8 *aes_iv = (u8 *)CEPH_AES_IV; 139static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
97 140
98/* 141/*
@@ -168,8 +211,7 @@ static void teardown_sgtable(struct sg_table *sgt)
168static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt, 211static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
169 void *buf, int buf_len, int in_len, int *pout_len) 212 void *buf, int buf_len, int in_len, int *pout_len)
170{ 213{
171 struct crypto_skcipher *tfm = ceph_crypto_alloc_cipher(); 214 SKCIPHER_REQUEST_ON_STACK(req, key->tfm);
172 SKCIPHER_REQUEST_ON_STACK(req, tfm);
173 struct sg_table sgt; 215 struct sg_table sgt;
174 struct scatterlist prealloc_sg; 216 struct scatterlist prealloc_sg;
175 char iv[AES_BLOCK_SIZE] __aligned(8); 217 char iv[AES_BLOCK_SIZE] __aligned(8);
@@ -177,20 +219,15 @@ static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
177 int crypt_len = encrypt ? in_len + pad_byte : in_len; 219 int crypt_len = encrypt ? in_len + pad_byte : in_len;
178 int ret; 220 int ret;
179 221
180 if (IS_ERR(tfm))
181 return PTR_ERR(tfm);
182
183 WARN_ON(crypt_len > buf_len); 222 WARN_ON(crypt_len > buf_len);
184 if (encrypt) 223 if (encrypt)
185 memset(buf + in_len, pad_byte, pad_byte); 224 memset(buf + in_len, pad_byte, pad_byte);
186 ret = setup_sgtable(&sgt, &prealloc_sg, buf, crypt_len); 225 ret = setup_sgtable(&sgt, &prealloc_sg, buf, crypt_len);
187 if (ret) 226 if (ret)
188 goto out_tfm; 227 return ret;
189 228
190 crypto_skcipher_setkey((void *)tfm, key->key, key->len);
191 memcpy(iv, aes_iv, AES_BLOCK_SIZE); 229 memcpy(iv, aes_iv, AES_BLOCK_SIZE);
192 230 skcipher_request_set_tfm(req, key->tfm);
193 skcipher_request_set_tfm(req, tfm);
194 skcipher_request_set_callback(req, 0, NULL, NULL); 231 skcipher_request_set_callback(req, 0, NULL, NULL);
195 skcipher_request_set_crypt(req, sgt.sgl, sgt.sgl, crypt_len, iv); 232 skcipher_request_set_crypt(req, sgt.sgl, sgt.sgl, crypt_len, iv);
196 233
@@ -232,8 +269,6 @@ static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
232 269
233out_sgt: 270out_sgt:
234 teardown_sgtable(&sgt); 271 teardown_sgtable(&sgt);
235out_tfm:
236 crypto_free_skcipher(tfm);
237 return ret; 272 return ret;
238} 273}
239 274
diff --git a/net/ceph/crypto.h b/net/ceph/crypto.h
index c4211590b721..58d83aa7740f 100644
--- a/net/ceph/crypto.h
+++ b/net/ceph/crypto.h
@@ -12,6 +12,7 @@ struct ceph_crypto_key {
12 struct ceph_timespec created; 12 struct ceph_timespec created;
13 int len; 13 int len;
14 void *key; 14 void *key;
15 struct crypto_skcipher *tfm;
15}; 16};
16 17
17int ceph_crypto_key_clone(struct ceph_crypto_key *dst, 18int ceph_crypto_key_clone(struct ceph_crypto_key *dst,