Skip to content

Commit 3c2d518

Browse files
stefanhaRHebblake
authored andcommitted
nbd-client: avoid read_reply_co entry if send failed
The following segfault is encountered if the NBD server closes the UNIX domain socket immediately after negotiation: Program terminated with signal SIGSEGV, Segmentation fault. #0 aio_co_schedule (ctx=0x0, co=0xd3c0ff2ef0) at util/async.c:441 441 QSLIST_INSERT_HEAD_ATOMIC(&ctx->scheduled_coroutines, (gdb) bt #0 0x000000d3c01a50f8 in aio_co_schedule (ctx=0x0, co=0xd3c0ff2ef0) at util/async.c:441 #1 0x000000d3c012fa90 in nbd_coroutine_end (bs=bs@entry=0xd3c0fec650, request=<optimized out>) at block/nbd-client.c:207 #2 0x000000d3c012fb58 in nbd_client_co_preadv (bs=0xd3c0fec650, offset=0, bytes=<optimized out>, qiov=0x7ffc10a91b20, flags=0) at block/nbd-client.c:237 qemu#3 0x000000d3c0128e63 in bdrv_driver_preadv (bs=bs@entry=0xd3c0fec650, offset=offset@entry=0, bytes=bytes@entry=512, qiov=qiov@entry=0x7ffc10a91b20, flags=0) at block/io.c:836 qemu#4 0x000000d3c012c3e0 in bdrv_aligned_preadv (child=child@entry=0xd3c0ff51d0, req=req@entry=0x7f31885d6e90, offset=offset@entry=0, bytes=bytes@entry=512, align=align@entry=1, qiov=qiov@entry=0x7ffc10a91b20, f +lags=0) at block/io.c:1086 qemu#5 0x000000d3c012c6b8 in bdrv_co_preadv (child=0xd3c0ff51d0, offset=offset@entry=0, bytes=bytes@entry=512, qiov=qiov@entry=0x7ffc10a91b20, flags=flags@entry=0) at block/io.c:1182 qemu#6 0x000000d3c011cc17 in blk_co_preadv (blk=0xd3c0ff4f80, offset=0, bytes=512, qiov=0x7ffc10a91b20, flags=0) at block/block-backend.c:1032 qemu#7 0x000000d3c011ccec in blk_read_entry (opaque=0x7ffc10a91b40) at block/block-backend.c:1079 qemu#8 0x000000d3c01bbb96 in coroutine_trampoline (i0=<optimized out>, i1=<optimized out>) at util/coroutine-ucontext.c:79 qemu#9 0x00007f3196cb8600 in __start_context () at /lib64/libc.so.6 The problem is that nbd_client_init() uses nbd_client_attach_aio_context() -> aio_co_schedule(new_context, client->read_reply_co). Execution of read_reply_co is deferred to a BH which doesn't run until later. In the mean time blk_co_preadv() can be called and nbd_coroutine_end() calls aio_wake() on read_reply_co. At this point in time read_reply_co's ctx isn't set because it has never been entered yet. This patch simplifies the nbd_co_send_request() -> nbd_co_receive_reply() -> nbd_coroutine_end() lifecycle to just nbd_co_send_request() -> nbd_co_receive_reply(). The request is "ended" if an error occurs at any point. Callers no longer have to invoke nbd_coroutine_end(). This cleanup also eliminates the segfault because we don't call aio_co_schedule() to wake up s->read_reply_co if sending the request failed. It is only necessary to wake up s->read_reply_co if a reply was received. Note this only happens with UNIX domain sockets on Linux. It doesn't seem possible to reproduce this with TCP sockets. Suggested-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Message-Id: <20170829122745.14309-2-stefanha@redhat.com> Signed-off-by: Eric Blake <eblake@redhat.com>
1 parent 2c94e27 commit 3c2d518

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

block/nbd-client.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ static int nbd_co_send_request(BlockDriverState *bs,
144144
request->handle = INDEX_TO_HANDLE(s, i);
145145

146146
if (s->quit) {
147-
qemu_co_mutex_unlock(&s->send_mutex);
148-
return -EIO;
147+
rc = -EIO;
148+
goto err;
149149
}
150150
if (!s->ioc) {
151-
qemu_co_mutex_unlock(&s->send_mutex);
152-
return -EPIPE;
151+
rc = -EPIPE;
152+
goto err;
153153
}
154154

155155
if (qiov) {
@@ -166,8 +166,13 @@ static int nbd_co_send_request(BlockDriverState *bs,
166166
} else {
167167
rc = nbd_send_request(s->ioc, request);
168168
}
169+
170+
err:
169171
if (rc < 0) {
170172
s->quit = true;
173+
s->requests[i].coroutine = NULL;
174+
s->in_flight--;
175+
qemu_co_queue_next(&s->free_sema);
171176
}
172177
qemu_co_mutex_unlock(&s->send_mutex);
173178
return rc;
@@ -201,13 +206,6 @@ static void nbd_co_receive_reply(NBDClientSession *s,
201206
/* Tell the read handler to read another header. */
202207
s->reply.handle = 0;
203208
}
204-
}
205-
206-
static void nbd_coroutine_end(BlockDriverState *bs,
207-
NBDRequest *request)
208-
{
209-
NBDClientSession *s = nbd_get_client_session(bs);
210-
int i = HANDLE_TO_INDEX(s, request->handle);
211209

212210
s->requests[i].coroutine = NULL;
213211

@@ -243,7 +241,6 @@ int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset,
243241
} else {
244242
nbd_co_receive_reply(client, &request, &reply, qiov);
245243
}
246-
nbd_coroutine_end(bs, &request);
247244
return -reply.error;
248245
}
249246

@@ -272,7 +269,6 @@ int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset,
272269
} else {
273270
nbd_co_receive_reply(client, &request, &reply, NULL);
274271
}
275-
nbd_coroutine_end(bs, &request);
276272
return -reply.error;
277273
}
278274

@@ -306,7 +302,6 @@ int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
306302
} else {
307303
nbd_co_receive_reply(client, &request, &reply, NULL);
308304
}
309-
nbd_coroutine_end(bs, &request);
310305
return -reply.error;
311306
}
312307

@@ -330,7 +325,6 @@ int nbd_client_co_flush(BlockDriverState *bs)
330325
} else {
331326
nbd_co_receive_reply(client, &request, &reply, NULL);
332327
}
333-
nbd_coroutine_end(bs, &request);
334328
return -reply.error;
335329
}
336330

@@ -355,7 +349,6 @@ int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes)
355349
} else {
356350
nbd_co_receive_reply(client, &request, &reply, NULL);
357351
}
358-
nbd_coroutine_end(bs, &request);
359352
return -reply.error;
360353

361354
}

0 commit comments

Comments
 (0)