dm crypt: fix ctx pending

Fix regression in dm-crypt introduced in commit
3a7f6c990ad04e6f576a159876c602d14d6f7fef ("dm crypt: use async crypto").

If write requests need to be split into pieces, the code must not process them
in parallel because the crypto context cannot be shared. So there can be
parallel crypto operations on one part of the write, but only one write bio
can be processed at a time.

This is not optimal and the workqueue code needs to be optimized for parallel
processing, but for now it solves the problem without affecting the
performance of synchronous crypto operation (most of current dm-crypt users).

http://bugzilla.kernel.org/show_bug.cgi?id=10242
http://bugzilla.kernel.org/show_bug.cgi?id=10207

Signed-off-by: Milan Broz <mbroz@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
Cc: "Rafael J. Wysocki" <rjw@sisk.pl>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by Milan Broz and committed by Linus Torvalds 3f1e9070 ab473a52

+31 -29
+31 -29
drivers/md/dm-crypt.c
··· 1 1 /* 2 2 * Copyright (C) 2003 Christophe Saout <christophe@saout.de> 3 3 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org> 4 - * Copyright (C) 2006-2007 Red Hat, Inc. All rights reserved. 4 + * Copyright (C) 2006-2008 Red Hat, Inc. All rights reserved. 5 5 * 6 6 * This file is released under the GPL. 7 7 */ ··· 93 93 94 94 struct workqueue_struct *io_queue; 95 95 struct workqueue_struct *crypt_queue; 96 + wait_queue_head_t writeq; 97 + 96 98 /* 97 99 * crypto related data 98 100 */ ··· 333 331 ctx->idx_out = bio_out ? bio_out->bi_idx : 0; 334 332 ctx->sector = sector + cc->iv_offset; 335 333 init_completion(&ctx->restart); 336 - /* 337 - * Crypto operation can be asynchronous, 338 - * ctx->pending is increased after request submission. 339 - * We need to ensure that we don't call the crypt finish 340 - * operation before pending got incremented 341 - * (dependent on crypt submission return code). 342 - */ 343 - atomic_set(&ctx->pending, 2); 334 + atomic_set(&ctx->pending, 1); 344 335 } 345 336 346 337 static int crypt_convert_block(struct crypt_config *cc, ··· 406 411 static int crypt_convert(struct crypt_config *cc, 407 412 struct convert_context *ctx) 408 413 { 409 - int r = 0; 414 + int r; 410 415 411 416 while(ctx->idx_in < ctx->bio_in->bi_vcnt && 412 417 ctx->idx_out < ctx->bio_out->bi_vcnt) { 413 418 414 419 crypt_alloc_req(cc, ctx); 415 420 421 + atomic_inc(&ctx->pending); 422 + 416 423 r = crypt_convert_block(cc, ctx, cc->req); 417 424 418 425 switch (r) { 426 + /* async */ 419 427 case -EBUSY: 420 428 wait_for_completion(&ctx->restart); 421 429 INIT_COMPLETION(ctx->restart); 422 430 /* fall through*/ 423 431 case -EINPROGRESS: 424 - atomic_inc(&ctx->pending); 425 432 cc->req = NULL; 426 - r = 0; 427 - /* fall through*/ 428 - case 0: 429 433 ctx->sector++; 430 434 continue; 431 - } 432 435 433 - break; 436 + /* sync */ 437 + case 0: 438 + atomic_dec(&ctx->pending); 439 + ctx->sector++; 440 + continue; 441 + 442 + /* error */ 443 + default: 444 + atomic_dec(&ctx->pending); 445 + return r; 446 + } 434 447 } 435 448 436 - /* 437 - * If there are pending crypto operation run async 438 - * code. Otherwise process return code synchronously. 439 - * The step of 2 ensures that async finish doesn't 440 - * call crypto finish too early. 441 - */ 442 - if (atomic_sub_return(2, &ctx->pending)) 443 - return -EINPROGRESS; 444 - 445 - return r; 449 + return 0; 446 450 } 447 451 448 452 static void dm_crypt_bio_destructor(struct bio *bio) ··· 618 624 static void kcryptd_io_write(struct dm_crypt_io *io) 619 625 { 620 626 struct bio *clone = io->ctx.bio_out; 627 + struct crypt_config *cc = io->target->private; 621 628 622 629 generic_make_request(clone); 630 + wake_up(&cc->writeq); 623 631 } 624 632 625 633 static void kcryptd_io(struct work_struct *work) ··· 694 698 695 699 r = crypt_convert(cc, &io->ctx); 696 700 697 - if (r != -EINPROGRESS) { 701 + if (atomic_dec_and_test(&io->ctx.pending)) { 702 + /* processed, no running async crypto */ 698 703 kcryptd_crypt_write_io_submit(io, r, 0); 699 704 if (unlikely(r < 0)) 700 705 return; ··· 703 706 atomic_inc(&io->pending); 704 707 705 708 /* out of memory -> run queues */ 706 - if (unlikely(remaining)) 709 + if (unlikely(remaining)) { 710 + /* wait for async crypto then reinitialize pending */ 711 + wait_event(cc->writeq, !atomic_read(&io->ctx.pending)); 712 + atomic_set(&io->ctx.pending, 1); 707 713 congestion_wait(WRITE, HZ/100); 714 + } 708 715 } 709 716 } 710 717 ··· 747 746 748 747 r = crypt_convert(cc, &io->ctx); 749 748 750 - if (r != -EINPROGRESS) 749 + if (atomic_dec_and_test(&io->ctx.pending)) 751 750 kcryptd_crypt_read_done(io, r); 752 751 753 752 crypt_dec_pending(io); ··· 1048 1047 goto bad_crypt_queue; 1049 1048 } 1050 1049 1050 + init_waitqueue_head(&cc->writeq); 1051 1051 ti->private = cc; 1052 1052 return 0; 1053 1053