1 dm-crypt: use per-bio data
3 This patch changes dm-crypt so that it uses auxiliary data allocated with
6 Dm-crypt requires two allocations per request - struct dm_crypt_io and
7 struct ablkcipher_request (with other data appended to it). It used
8 mempool for the allocation.
10 Some requests may require more dm_crypt_ios and ablkcipher_requests,
11 however most requests need just one of each of these two structures to
14 This patch changes it so that the first dm_crypt_io and ablkcipher_request
15 and allocated with the bio (using target per_bio_data_size option). If the
16 request needs additional values, they are allocated from the mempool.
18 Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
21 drivers/md/dm-crypt.c | 40 ++++++++++++++++++++++++++--------------
22 1 file changed, 26 insertions(+), 14 deletions(-)
24 Index: linux-3.14-rc4/drivers/md/dm-crypt.c
25 ===================================================================
26 --- linux-3.14-rc4.orig/drivers/md/dm-crypt.c 2014-02-27 17:48:31.000000000 +0100
27 +++ linux-3.14-rc4/drivers/md/dm-crypt.c 2014-02-27 17:48:31.000000000 +0100
28 @@ -59,7 +59,7 @@ struct dm_crypt_io {
31 struct dm_crypt_io *base_io;
33 +} CRYPTO_MINALIGN_ATTR;
35 struct dm_crypt_request {
36 struct convert_context *ctx;
37 @@ -162,6 +162,8 @@ struct crypt_config {
39 unsigned int dmreq_start;
41 + unsigned int per_bio_data_size;
44 unsigned int key_size;
45 unsigned int key_parts; /* independent parts in key buffer */
46 @@ -895,6 +897,14 @@ static void crypt_alloc_req(struct crypt
47 kcryptd_async_done, dmreq_of_req(cc, ctx->req));
50 +static void crypt_free_req(struct crypt_config *cc,
51 + struct ablkcipher_request *req, struct bio *base_bio)
53 + struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
54 + if ((struct ablkcipher_request *)(io + 1) != req)
55 + mempool_free(req, cc->req_pool);
59 * Encrypt / decrypt data from one bio to another one (can be the same one)
61 @@ -1008,12 +1018,9 @@ static void crypt_free_buffer_pages(stru
65 -static struct dm_crypt_io *crypt_io_alloc(struct crypt_config *cc,
66 - struct bio *bio, sector_t sector)
67 +static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
68 + struct bio *bio, sector_t sector)
70 - struct dm_crypt_io *io;
72 - io = mempool_alloc(cc->io_pool, GFP_NOIO);
76 @@ -1021,8 +1028,6 @@ static struct dm_crypt_io *crypt_io_allo
79 atomic_set(&io->io_pending, 0);
84 static void crypt_inc_pending(struct dm_crypt_io *io)
85 @@ -1046,8 +1051,9 @@ static void crypt_dec_pending(struct dm_
89 - mempool_free(io->ctx.req, cc->req_pool);
90 - mempool_free(io, cc->io_pool);
91 + crypt_free_req(cc, io->ctx.req, base_bio);
92 + if (io != dm_per_bio_data(base_bio, cc->per_bio_data_size))
93 + mempool_free(io, cc->io_pool);
96 bio_endio(base_bio, error);
97 @@ -1255,8 +1261,8 @@ static void kcryptd_crypt_write_convert(
98 * between fragments, so switch to a new dm_crypt_io structure.
100 if (unlikely(!crypt_finished && remaining)) {
101 - new_io = crypt_io_alloc(io->cc, io->base_bio,
103 + new_io = mempool_alloc(cc->io_pool, GFP_NOIO);
104 + crypt_io_init(new_io, io->cc, io->base_bio, sector);
105 crypt_inc_pending(new_io);
106 crypt_convert_init(cc, &new_io->ctx, NULL,
107 io->base_bio, sector);
108 @@ -1325,7 +1331,7 @@ static void kcryptd_async_done(struct cr
112 - mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool);
113 + crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
115 if (!atomic_dec_and_test(&ctx->cc_pending))
117 @@ -1728,6 +1734,10 @@ static int crypt_ctr(struct dm_target *t
121 + cc->per_bio_data_size = ti->per_bio_data_size =
122 + sizeof(struct dm_crypt_io) + cc->dmreq_start +
123 + sizeof(struct dm_crypt_request) + cc->iv_size;
125 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
126 if (!cc->page_pool) {
127 ti->error = "Cannot allocate page mempool";
128 @@ -1824,7 +1834,9 @@ static int crypt_map(struct dm_target *t
129 return DM_MAPIO_REMAPPED;
132 - io = crypt_io_alloc(cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
133 + io = dm_per_bio_data(bio, cc->per_bio_data_size);
134 + crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
135 + io->ctx.req = (struct ablkcipher_request *)(io + 1);
137 if (bio_data_dir(io->base_bio) == READ) {
138 if (kcryptd_io_read(io, GFP_NOWAIT))