From: Milan Broz <[email-masked]>

The pending reference count must be incremented *before* the async work is
queued to another thread, not after.  Otherwise there's a race if the
work completes and decrements the reference count before it gets incremented.

Signed-off-by: Milan Broz <[email-masked]>
Signed-off-by: Alasdair G Kergon <[email-masked]>

---
 drivers/md/dm-crypt.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)

Index: linux-2.6.27/drivers/md/dm-crypt.c
===================================================================
--- linux-2.6.27.orig/drivers/md/dm-crypt.c	2008-10-10 12:21:34.000000000 +0100
+++ linux-2.6.27/drivers/md/dm-crypt.c	2008-10-10 12:21:34.000000000 +0100
@@ -719,16 +719,15 @@ static void kcryptd_crypt_write_convert(
 
 		remaining -= clone->bi_size;
 
+		crypt_inc_pending(io);
 		r = crypt_convert(cc, &io->ctx);
 
 		if (atomic_dec_and_test(&io->ctx.pending)) {
 			/* processed, no running async crypto  */
-			crypt_inc_pending(io);
 			kcryptd_crypt_write_io_submit(io, r, 0);
 			if (unlikely(r < 0))
 				break;
-		} else
-			crypt_inc_pending(io);
+		}
 
 		/* out of memory -> run queues */
 		if (unlikely(remaining)) {