1 dm-crypt: don't allocate pages for a partial request.
3 This patch changes crypt_alloc_buffer so that it always allocates pages for
6 This change enables further simplification and removing of one refcounts
9 Note: the next patch is needed to fix a theoretical deadlock
11 Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
14 drivers/md/dm-crypt.c | 133 +++++++++-----------------------------------------
15 1 file changed, 25 insertions(+), 108 deletions(-)
17 Index: linux-3.9.2-fast/drivers/md/dm-crypt.c
18 ===================================================================
19 --- linux-3.9.2-fast.orig/drivers/md/dm-crypt.c 2013-05-15 21:47:30.000000000 +0200
20 +++ linux-3.9.2-fast/drivers/md/dm-crypt.c 2013-05-15 22:49:13.000000000 +0200
21 @@ -59,7 +59,6 @@ struct dm_crypt_io {
25 - struct dm_crypt_io *base_io;
28 struct dm_crypt_request {
29 @@ -162,7 +161,6 @@ struct crypt_config {
33 -#define MIN_POOL_PAGES 32
35 static struct kmem_cache *_crypt_io_pool;
37 @@ -777,14 +775,13 @@ static int crypt_convert(struct crypt_co
41 +static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
44 * Generate a new unfragmented bio with the given size
45 * This should never violate the device limitations
46 - * May return a smaller bio when running out of pages, indicated by
47 - * *out_of_pages set to 1.
49 -static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
50 - unsigned *out_of_pages)
51 +static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
53 struct crypt_config *cc = io->cc;
55 @@ -798,37 +795,22 @@ static struct bio *crypt_alloc_buffer(st
58 clone_init(io, clone);
61 for (i = 0; i < nr_iovecs; i++) {
62 page = mempool_alloc(cc->page_pool, gfp_mask);
69 - * If additional pages cannot be allocated without waiting,
70 - * return a partially-allocated bio. The caller will then try
71 - * to allocate more bios while submitting this partial bio.
73 - gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
75 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
77 if (!bio_add_page(clone, page, len, 0)) {
78 mempool_free(page, cc->page_pool);
80 + crypt_free_buffer_pages(cc, clone);
88 - if (!clone->bi_size) {
96 @@ -855,7 +837,6 @@ static struct dm_crypt_io *crypt_io_allo
100 - io->base_io = NULL;
102 atomic_set(&io->io_pending, 0);
104 @@ -870,13 +851,11 @@ static void crypt_inc_pending(struct dm_
106 * One of the bios was finished. Check for completion of
107 * the whole request and correctly clean up the buffer.
108 - * If base_io is set, wait for the last fragment to complete.
110 static void crypt_dec_pending(struct dm_crypt_io *io)
112 struct crypt_config *cc = io->cc;
113 struct bio *base_bio = io->base_bio;
114 - struct dm_crypt_io *base_io = io->base_io;
115 int error = io->error;
117 if (!atomic_dec_and_test(&io->io_pending))
118 @@ -886,13 +865,7 @@ static void crypt_dec_pending(struct dm_
119 mempool_free(io->ctx.req, cc->req_pool);
120 mempool_free(io, cc->io_pool);
122 - if (likely(!base_io))
123 - bio_endio(base_bio, error);
125 - if (error && !base_io->error)
126 - base_io->error = error;
127 - crypt_dec_pending(base_io);
129 + bio_endio(base_bio, error);
133 @@ -1030,10 +1003,7 @@ static void kcryptd_crypt_write_convert(
135 struct crypt_config *cc = io->cc;
137 - struct dm_crypt_io *new_io;
139 - unsigned out_of_pages = 0;
140 - unsigned remaining = io->base_bio->bi_size;
141 sector_t sector = io->sector;
144 @@ -1043,81 +1013,28 @@ static void kcryptd_crypt_write_convert(
145 crypt_inc_pending(io);
146 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
149 - * The allocated buffers can be smaller than the whole bio,
150 - * so repeat the whole process until all the data can be handled.
152 - while (remaining) {
153 - clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
154 - if (unlikely(!clone)) {
155 - io->error = -ENOMEM;
159 - io->ctx.bio_out = clone;
160 - io->ctx.idx_out = 0;
162 - remaining -= clone->bi_size;
163 - sector += bio_sectors(clone);
165 - crypt_inc_pending(io);
167 - r = crypt_convert(cc, &io->ctx);
171 - crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
173 - /* Encryption was already finished, submit io now */
174 - if (crypt_finished) {
175 - kcryptd_crypt_write_io_submit(io, 0);
178 - * If there was an error, do not try next fragments.
179 - * For async, error is processed in async handler.
181 - if (unlikely(r < 0))
183 + clone = crypt_alloc_buffer(io, io->base_bio->bi_size);
184 + if (unlikely(!clone)) {
185 + io->error = -ENOMEM;
189 - io->sector = sector;
191 + io->ctx.bio_out = clone;
192 + io->ctx.idx_out = 0;
195 - * Out of memory -> run queues
196 - * But don't wait if split was due to the io size restriction
198 - if (unlikely(out_of_pages))
199 - congestion_wait(BLK_RW_ASYNC, HZ/100);
200 + sector += bio_sectors(clone);
203 - * With async crypto it is unsafe to share the crypto context
204 - * between fragments, so switch to a new dm_crypt_io structure.
206 - if (unlikely(!crypt_finished && remaining)) {
207 - new_io = crypt_io_alloc(io->cc, io->base_bio,
209 - crypt_inc_pending(new_io);
210 - crypt_convert_init(cc, &new_io->ctx, NULL,
211 - io->base_bio, sector);
212 - new_io->ctx.idx_in = io->ctx.idx_in;
213 - new_io->ctx.offset_in = io->ctx.offset_in;
216 - * Fragments after the first use the base_io
220 - new_io->base_io = io;
222 - new_io->base_io = io->base_io;
223 - crypt_inc_pending(io->base_io);
224 - crypt_dec_pending(io);
226 + crypt_inc_pending(io);
227 + r = crypt_convert(cc, &io->ctx);
230 + crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
235 + /* Encryption was already finished, submit io now */
236 + if (crypt_finished)
237 + kcryptd_crypt_write_io_submit(io, 0);
240 crypt_dec_pending(io);
243 @@ -1556,7 +1473,7 @@ static int crypt_ctr(struct dm_target *t
247 - cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
248 + cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0);
249 if (!cc->page_pool) {
250 ti->error = "Cannot allocate page mempool";