Skip to content

Commit c05f3e3

Browse files
roygerkonradwilk
authored andcommitted
xen-blkback: fix shutdown race
Introduce a new variable to keep track of the number of in-flight requests. We need to make sure that when xen_blkif_put is called the request has already been freed and we can safely free xen_blkif, which was not the case before. Signed-off-by: Roger Pau Monné <roger.pau@citrix.com> Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Cc: David Vrabel <david.vrabel@citrix.com> Reviewed-by: Boris Ostrovsky <boris.ostrovsky@oracle.com> Tested-by: Matt Rushton <mrushton@amazon.com> Reviewed-by: Matt Rushton <mrushton@amazon.com> Cc: Matt Wilson <msw@amazon.com> Cc: Ian Campbell <Ian.Campbell@citrix.com> Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
1 parent ef75341 commit c05f3e3

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

‎drivers/block/xen-blkback/blkback.c‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,7 @@ static void xen_blk_drain_io(struct xen_blkif *blkif)
943943
{
944944
atomic_set(&blkif->drain, 1);
945945
do {
946-
/* The initial value is one, and one refcnt taken at the
947-
* start of the xen_blkif_schedule thread. */
948-
if (atomic_read(&blkif->refcnt) <= 2)
946+
if (atomic_read(&blkif->inflight) == 0)
949947
break;
950948
wait_for_completion_interruptible_timeout(
951949
&blkif->drain_complete, HZ);
@@ -985,17 +983,30 @@ static void __end_block_io_op(struct pending_req *pending_req, int error)
985983
* the proper response on the ring.
986984
*/
987985
if (atomic_dec_and_test(&pending_req->pendcnt)) {
988-
xen_blkbk_unmap(pending_req->blkif,
986+
struct xen_blkif *blkif = pending_req->blkif;
987+
988+
xen_blkbk_unmap(blkif,
989989
pending_req->segments,
990990
pending_req->nr_pages);
991-
make_response(pending_req->blkif, pending_req->id,
991+
make_response(blkif, pending_req->id,
992992
pending_req->operation, pending_req->status);
993-
xen_blkif_put(pending_req->blkif);
994-
if (atomic_read(&pending_req->blkif->refcnt) <= 2) {
995-
if (atomic_read(&pending_req->blkif->drain))
996-
complete(&pending_req->blkif->drain_complete);
993+
free_req(blkif, pending_req);
994+
/*
995+
* Make sure the request is freed before releasing blkif,
996+
* or there could be a race between free_req and the
997+
* cleanup done in xen_blkif_free during shutdown.
998+
*
999+
* NB: The fact that we might try to wake up pending_free_wq
1000+
* before drain_complete (in case there's a drain going on)
1001+
* it's not a problem with our current implementation
1002+
* because we can assure there's no thread waiting on
1003+
* pending_free_wq if there's a drain going on, but it has
1004+
* to be taken into account if the current model is changed.
1005+
*/
1006+
if (atomic_dec_and_test(&blkif->inflight) && atomic_read(&blkif->drain)) {
1007+
complete(&blkif->drain_complete);
9971008
}
998-
free_req(pending_req->blkif, pending_req);
1009+
xen_blkif_put(blkif);
9991010
}
10001011
}
10011012

@@ -1249,6 +1260,7 @@ static int dispatch_rw_block_io(struct xen_blkif *blkif,
12491260
* below (in "!bio") if we are handling a BLKIF_OP_DISCARD.
12501261
*/
12511262
xen_blkif_get(blkif);
1263+
atomic_inc(&blkif->inflight);
12521264

12531265
for (i = 0; i < nseg; i++) {
12541266
while ((bio == NULL) ||

‎drivers/block/xen-blkback/common.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ struct xen_blkif {
278278
/* for barrier (drain) requests */
279279
struct completion drain_complete;
280280
atomic_t drain;
281+
atomic_t inflight;
281282
/* One thread per one blkif. */
282283
struct task_struct *xenblkd;
283284
unsigned int waiting_reqs;

‎drivers/block/xen-blkback/xenbus.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ static struct xen_blkif *xen_blkif_alloc(domid_t domid)
128128
INIT_LIST_HEAD(&blkif->persistent_purge_list);
129129
blkif->free_pages_num = 0;
130130
atomic_set(&blkif->persistent_gnt_in_use, 0);
131+
atomic_set(&blkif->inflight, 0);
131132

132133
INIT_LIST_HEAD(&blkif->pending_free);
133134

0 commit comments

Comments
 (0)