dm io: fix a race condition in the wake up code for sync_io
authorJoe Thornber <thornber@redhat.com>
Fri, 27 Jun 2014 19:29:04 +0000 (15:29 -0400)
committerBen Hutchings <ben@decadent.org.uk>
Wed, 6 Aug 2014 17:07:37 +0000 (18:07 +0100)
commit3b9185076b63d1e24cd43209a7a9c26e78b659bf
treeae3b3521d80b797c302d3bda3f4f36e1c37a6050
parent6df6ecaf8904028b819f6a3115218029667dccbc
dm io: fix a race condition in the wake up code for sync_io

commit 10f1d5d111e8aed46a0f1179faf9a3cf422f689e upstream.

There's a race condition between the atomic_dec_and_test(&io->count)
in dec_count() and the waking of the sync_io() thread.  If the thread
is spuriously woken immediately after the decrement it may exit,
making the on stack io struct invalid, yet the dec_count could still
be using it.

Fix this race by using a completion in sync_io() and dec_count().

Reported-by: Minfei Huang <huangminfei@ucloud.cn>
Signed-off-by: Joe Thornber <thornber@redhat.com>
Signed-off-by: Mike Snitzer <snitzer@redhat.com>
Acked-by: Mikulas Patocka <mpatocka@redhat.com>
[bwh: Backported to 3.2: use wait_for_completion() as wait_for_completion_io()
 is not available]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
drivers/md/dm-io.c