Skip to content

Commit fef80ea

Browse files
committed
Merge remote-tracking branch 'remotes/ericb/tags/pull-nbd-2021-01-20' into staging
nbd patches for 2021-01-20 - minor resource leak fixes in qemu-nbd - ensure proper aio context when nbd server uses iothreads - iotest refactorings in preparation for rewriting ./check to be more flexible, and preparing for more nbd server reconnect features # gpg: Signature made Thu 21 Jan 2021 02:28:19 GMT # gpg: using RSA key 71C2CC22B1C4602927D2F3AAA7A16B4A2527436A # gpg: Good signature from "Eric Blake <[email protected]>" [full] # gpg: aka "Eric Blake (Free Software Programmer) <[email protected]>" [full] # gpg: aka "[jpeg image of size 6874]" [full] # Primary key fingerprint: 71C2 CC22 B1C4 6029 27D2 F3AA A7A1 6B4A 2527 436A * remotes/ericb/tags/pull-nbd-2021-01-20: iotests.py: qemu_io(): reuse qemu_tool_pipe_and_status() iotests.py: fix qemu_tool_pipe_and_status() iotests/264: fix style iotests: define group in each iotest iotests/294: add shebang line iotests: make tests executable iotests: fix some whitespaces in test output files iotests/303: use dot slash for qcow2.py running iotests/277: use dot slash for nbd-fault-injector.py running nbd/server: Quiesce coroutines on context switch block: Honor blk_set_aio_context() context requirements qemu-nbd: Fix a memleak in nbd_client_thread() qemu-nbd: Fix a memleak in qemu_nbd_client_list() Signed-off-by: Peter Maydell <[email protected]>
2 parents 954b83f + f874e7f commit fef80ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

301 files changed

+452
-72
lines changed

hw/block/dataplane/virtio-blk.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ int virtio_blk_data_plane_start(VirtIODevice *vdev)
172172
VirtIOBlockDataPlane *s = vblk->dataplane;
173173
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vblk)));
174174
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
175+
AioContext *old_context;
175176
unsigned i;
176177
unsigned nvqs = s->conf->num_queues;
177178
Error *local_err = NULL;
@@ -214,7 +215,10 @@ int virtio_blk_data_plane_start(VirtIODevice *vdev)
214215
vblk->dataplane_started = true;
215216
trace_virtio_blk_data_plane_start(s);
216217

218+
old_context = blk_get_aio_context(s->conf->conf.blk);
219+
aio_context_acquire(old_context);
217220
r = blk_set_aio_context(s->conf->conf.blk, s->ctx, &local_err);
221+
aio_context_release(old_context);
218222
if (r < 0) {
219223
error_report_err(local_err);
220224
goto fail_guest_notifiers;

hw/block/dataplane/xen-block.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ void xen_block_dataplane_start(XenBlockDataPlane *dataplane,
725725
{
726726
ERRP_GUARD();
727727
XenDevice *xendev = dataplane->xendev;
728+
AioContext *old_context;
728729
unsigned int ring_size;
729730
unsigned int i;
730731

@@ -808,10 +809,14 @@ void xen_block_dataplane_start(XenBlockDataPlane *dataplane,
808809
goto stop;
809810
}
810811

811-
aio_context_acquire(dataplane->ctx);
812+
old_context = blk_get_aio_context(dataplane->blk);
813+
aio_context_acquire(old_context);
812814
/* If other users keep the BlockBackend in the iothread, that's ok */
813815
blk_set_aio_context(dataplane->blk, dataplane->ctx, NULL);
816+
aio_context_release(old_context);
817+
814818
/* Only reason for failure is a NULL channel */
819+
aio_context_acquire(dataplane->ctx);
815820
xen_device_set_event_channel_context(xendev, dataplane->event_channel,
816821
dataplane->ctx, &error_abort);
817822
aio_context_release(dataplane->ctx);

hw/scsi/virtio-scsi.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,15 +849,17 @@ static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev,
849849
VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
850850
VirtIOSCSI *s = VIRTIO_SCSI(vdev);
851851
SCSIDevice *sd = SCSI_DEVICE(dev);
852+
AioContext *old_context;
852853
int ret;
853854

854855
if (s->ctx && !s->dataplane_fenced) {
855856
if (blk_op_is_blocked(sd->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) {
856857
return;
857858
}
858-
virtio_scsi_acquire(s);
859+
old_context = blk_get_aio_context(sd->conf.blk);
860+
aio_context_acquire(old_context);
859861
ret = blk_set_aio_context(sd->conf.blk, s->ctx, errp);
860-
virtio_scsi_release(s);
862+
aio_context_release(old_context);
861863
if (ret < 0) {
862864
return;
863865
}

nbd/server.c

Lines changed: 106 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ struct NBDClient {
132132
CoMutex send_lock;
133133
Coroutine *send_coroutine;
134134

135+
bool read_yielding;
136+
bool quiescing;
137+
135138
QTAILQ_ENTRY(NBDClient) next;
136139
int nb_requests;
137140
bool closing;
@@ -1352,14 +1355,60 @@ static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
13521355
return 0;
13531356
}
13541357

1355-
static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
1358+
/* nbd_read_eof
1359+
* Tries to read @size bytes from @ioc. This is a local implementation of
1360+
* qio_channel_readv_all_eof. We have it here because we need it to be
1361+
* interruptible and to know when the coroutine is yielding.
1362+
* Returns 1 on success
1363+
* 0 on eof, when no data was read (errp is not set)
1364+
* negative errno on failure (errp is set)
1365+
*/
1366+
static inline int coroutine_fn
1367+
nbd_read_eof(NBDClient *client, void *buffer, size_t size, Error **errp)
1368+
{
1369+
bool partial = false;
1370+
1371+
assert(size);
1372+
while (size > 0) {
1373+
struct iovec iov = { .iov_base = buffer, .iov_len = size };
1374+
ssize_t len;
1375+
1376+
len = qio_channel_readv(client->ioc, &iov, 1, errp);
1377+
if (len == QIO_CHANNEL_ERR_BLOCK) {
1378+
client->read_yielding = true;
1379+
qio_channel_yield(client->ioc, G_IO_IN);
1380+
client->read_yielding = false;
1381+
if (client->quiescing) {
1382+
return -EAGAIN;
1383+
}
1384+
continue;
1385+
} else if (len < 0) {
1386+
return -EIO;
1387+
} else if (len == 0) {
1388+
if (partial) {
1389+
error_setg(errp,
1390+
"Unexpected end-of-file before all bytes were read");
1391+
return -EIO;
1392+
} else {
1393+
return 0;
1394+
}
1395+
}
1396+
1397+
partial = true;
1398+
size -= len;
1399+
buffer = (uint8_t *) buffer + len;
1400+
}
1401+
return 1;
1402+
}
1403+
1404+
static int nbd_receive_request(NBDClient *client, NBDRequest *request,
13561405
Error **errp)
13571406
{
13581407
uint8_t buf[NBD_REQUEST_SIZE];
13591408
uint32_t magic;
13601409
int ret;
13611410

1362-
ret = nbd_read(ioc, buf, sizeof(buf), "request", errp);
1411+
ret = nbd_read_eof(client, buf, sizeof(buf), errp);
13631412
if (ret < 0) {
13641413
return ret;
13651414
}
@@ -1480,25 +1529,48 @@ static void blk_aio_attached(AioContext *ctx, void *opaque)
14801529

14811530
QTAILQ_FOREACH(client, &exp->clients, next) {
14821531
qio_channel_attach_aio_context(client->ioc, ctx);
1532+
1533+
assert(client->recv_coroutine == NULL);
1534+
assert(client->send_coroutine == NULL);
1535+
1536+
if (client->quiescing) {
1537+
client->quiescing = false;
1538+
nbd_client_receive_next_request(client);
1539+
}
1540+
}
1541+
}
1542+
1543+
static void nbd_aio_detach_bh(void *opaque)
1544+
{
1545+
NBDExport *exp = opaque;
1546+
NBDClient *client;
1547+
1548+
QTAILQ_FOREACH(client, &exp->clients, next) {
1549+
qio_channel_detach_aio_context(client->ioc);
1550+
client->quiescing = true;
1551+
14831552
if (client->recv_coroutine) {
1484-
aio_co_schedule(ctx, client->recv_coroutine);
1553+
if (client->read_yielding) {
1554+
qemu_aio_coroutine_enter(exp->common.ctx,
1555+
client->recv_coroutine);
1556+
} else {
1557+
AIO_WAIT_WHILE(exp->common.ctx, client->recv_coroutine != NULL);
1558+
}
14851559
}
1560+
14861561
if (client->send_coroutine) {
1487-
aio_co_schedule(ctx, client->send_coroutine);
1562+
AIO_WAIT_WHILE(exp->common.ctx, client->send_coroutine != NULL);
14881563
}
14891564
}
14901565
}
14911566

14921567
static void blk_aio_detach(void *opaque)
14931568
{
14941569
NBDExport *exp = opaque;
1495-
NBDClient *client;
14961570

14971571
trace_nbd_blk_aio_detach(exp->name, exp->common.ctx);
14981572

1499-
QTAILQ_FOREACH(client, &exp->clients, next) {
1500-
qio_channel_detach_aio_context(client->ioc);
1501-
}
1573+
aio_wait_bh_oneshot(exp->common.ctx, nbd_aio_detach_bh, exp);
15021574

15031575
exp->common.ctx = NULL;
15041576
}
@@ -2151,20 +2223,23 @@ static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle,
21512223

21522224
/* nbd_co_receive_request
21532225
* Collect a client request. Return 0 if request looks valid, -EIO to drop
2154-
* connection right away, and any other negative value to report an error to
2155-
* the client (although the caller may still need to disconnect after reporting
2156-
* the error).
2226+
* connection right away, -EAGAIN to indicate we were interrupted and the
2227+
* channel should be quiesced, and any other negative value to report an error
2228+
* to the client (although the caller may still need to disconnect after
2229+
* reporting the error).
21572230
*/
21582231
static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
21592232
Error **errp)
21602233
{
21612234
NBDClient *client = req->client;
21622235
int valid_flags;
2236+
int ret;
21632237

21642238
g_assert(qemu_in_coroutine());
21652239
assert(client->recv_coroutine == qemu_coroutine_self());
2166-
if (nbd_receive_request(client->ioc, request, errp) < 0) {
2167-
return -EIO;
2240+
ret = nbd_receive_request(client, request, errp);
2241+
if (ret < 0) {
2242+
return ret;
21682243
}
21692244

21702245
trace_nbd_co_receive_request_decode_type(request->handle, request->type,
@@ -2507,6 +2582,17 @@ static coroutine_fn void nbd_trip(void *opaque)
25072582
return;
25082583
}
25092584

2585+
if (client->quiescing) {
2586+
/*
2587+
* We're switching between AIO contexts. Don't attempt to receive a new
2588+
* request and kick the main context which may be waiting for us.
2589+
*/
2590+
nbd_client_put(client);
2591+
client->recv_coroutine = NULL;
2592+
aio_wait_kick();
2593+
return;
2594+
}
2595+
25102596
req = nbd_request_get(client);
25112597
ret = nbd_co_receive_request(req, &request, &local_err);
25122598
client->recv_coroutine = NULL;
@@ -2519,6 +2605,11 @@ static coroutine_fn void nbd_trip(void *opaque)
25192605
goto done;
25202606
}
25212607

2608+
if (ret == -EAGAIN) {
2609+
assert(client->quiescing);
2610+
goto done;
2611+
}
2612+
25222613
nbd_client_receive_next_request(client);
25232614
if (ret == -EIO) {
25242615
goto disconnect;
@@ -2565,7 +2656,8 @@ static coroutine_fn void nbd_trip(void *opaque)
25652656

25662657
static void nbd_client_receive_next_request(NBDClient *client)
25672658
{
2568-
if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) {
2659+
if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS &&
2660+
!client->quiescing) {
25692661
nbd_client_get(client);
25702662
client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
25712663
aio_co_schedule(client->exp->common.ctx, client->recv_coroutine);

qemu-nbd.c

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
181181
sioc = qio_channel_socket_new();
182182
if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
183183
error_report_err(err);
184-
return EXIT_FAILURE;
184+
goto out;
185185
}
186186
rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
187187
&err);
@@ -265,8 +265,8 @@ static void *nbd_client_thread(void *arg)
265265
char *device = arg;
266266
NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
267267
QIOChannelSocket *sioc;
268-
int fd;
269-
int ret;
268+
int fd = -1;
269+
int ret = EXIT_FAILURE;
270270
pthread_t show_parts_thread;
271271
Error *local_error = NULL;
272272

@@ -278,26 +278,24 @@ static void *nbd_client_thread(void *arg)
278278
goto out;
279279
}
280280

281-
ret = nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
282-
NULL, NULL, NULL, &info, &local_error);
283-
if (ret < 0) {
281+
if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
282+
NULL, NULL, NULL, &info, &local_error) < 0) {
284283
if (local_error) {
285284
error_report_err(local_error);
286285
}
287-
goto out_socket;
286+
goto out;
288287
}
289288

290289
fd = open(device, O_RDWR);
291290
if (fd < 0) {
292291
/* Linux-only, we can use %m in printf. */
293292
error_report("Failed to open %s: %m", device);
294-
goto out_socket;
293+
goto out;
295294
}
296295

297-
ret = nbd_init(fd, sioc, &info, &local_error);
298-
if (ret < 0) {
296+
if (nbd_init(fd, sioc, &info, &local_error) < 0) {
299297
error_report_err(local_error);
300-
goto out_fd;
298+
goto out;
301299
}
302300

303301
/* update partition table */
@@ -311,24 +309,20 @@ static void *nbd_client_thread(void *arg)
311309
dup2(STDOUT_FILENO, STDERR_FILENO);
312310
}
313311

314-
ret = nbd_client(fd);
315-
if (ret) {
316-
goto out_fd;
312+
if (nbd_client(fd) < 0) {
313+
goto out;
317314
}
318-
close(fd);
319-
object_unref(OBJECT(sioc));
320-
g_free(info.name);
321-
kill(getpid(), SIGTERM);
322-
return (void *) EXIT_SUCCESS;
323315

324-
out_fd:
325-
close(fd);
326-
out_socket:
316+
ret = EXIT_SUCCESS;
317+
318+
out:
319+
if (fd >= 0) {
320+
close(fd);
321+
}
327322
object_unref(OBJECT(sioc));
328-
out:
329323
g_free(info.name);
330324
kill(getpid(), SIGTERM);
331-
return (void *) EXIT_FAILURE;
325+
return (void *) (intptr_t) ret;
332326
}
333327
#endif /* HAVE_NBD_DEVICE */
334328

tests/qemu-iotests/001

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
2+
# group: rw auto quick
23
#
34
# Test simple read/write using plain bdrv_pread/bdrv_pwrite
45
#

tests/qemu-iotests/002

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
2+
# group: rw auto quick
23
#
34
# Test simple read/write using plain bdrv_pread/bdrv_pwrite
45
#

tests/qemu-iotests/003

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
2+
# group: rw auto
23
#
34
# Test simple read/write using bdrv_aio_readv/bdrv_aio_writev
45
#

tests/qemu-iotests/004

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
2+
# group: rw auto quick
23
#
34
# Make sure we can't read and write outside of the image size.
45
#

tests/qemu-iotests/005

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
2+
# group: img auto quick
23
#
34
# Make sure qemu-img can create 5TB images
45
#

0 commit comments

Comments
 (0)