2019-01-08 17:48:53 +03:00
|
|
|
/*
|
2019-01-08 17:48:54 +03:00
|
|
|
* Copyright (c) 2018 Citrix Systems Inc.
|
|
|
|
* (c) Gerd Hoffmann <kraxel@redhat.com>
|
2019-01-08 17:48:53 +03:00
|
|
|
*
|
2019-01-08 17:48:54 +03:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; under version 2 of the License.
|
2019-01-08 17:48:53 +03:00
|
|
|
*
|
2019-01-08 17:48:54 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2019-01-08 17:48:53 +03:00
|
|
|
*
|
2019-01-08 17:48:54 +03:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
2019-01-08 17:48:53 +03:00
|
|
|
*
|
2019-01-08 17:48:54 +03:00
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2019-01-08 17:48:53 +03:00
|
|
|
*/
|
|
|
|
|
2019-01-08 17:48:55 +03:00
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu/error-report.h"
|
Include qemu/main-loop.h less
In my "build everything" tree, changing qemu/main-loop.h triggers a
recompile of some 5600 out of 6600 objects (not counting tests and
objects that don't depend on qemu/osdep.h). It includes block/aio.h,
which in turn includes qemu/event_notifier.h, qemu/notify.h,
qemu/processor.h, qemu/qsp.h, qemu/queue.h, qemu/thread-posix.h,
qemu/thread.h, qemu/timer.h, and a few more.
Include qemu/main-loop.h only where it's needed. Touching it now
recompiles only some 1700 objects. For block/aio.h and
qemu/event_notifier.h, these numbers drop from 5600 to 2800. For the
others, they shrink only slightly.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20190812052359.30071-21-armbru@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
2019-08-12 08:23:50 +03:00
|
|
|
#include "qemu/main-loop.h"
|
2019-01-08 17:48:55 +03:00
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "hw/xen/xen_common.h"
|
|
|
|
#include "hw/block/xen_blkif.h"
|
|
|
|
#include "sysemu/block-backend.h"
|
|
|
|
#include "sysemu/iothread.h"
|
|
|
|
#include "xen-block.h"
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
typedef struct XenBlockRequest {
|
2019-01-08 17:48:55 +03:00
|
|
|
blkif_request_t req;
|
|
|
|
int16_t status;
|
|
|
|
off_t start;
|
|
|
|
QEMUIOVector v;
|
|
|
|
void *buf;
|
|
|
|
size_t size;
|
|
|
|
int presync;
|
|
|
|
int aio_inflight;
|
|
|
|
int aio_errors;
|
2019-01-08 17:48:56 +03:00
|
|
|
XenBlockDataPlane *dataplane;
|
2019-01-08 17:48:57 +03:00
|
|
|
QLIST_ENTRY(XenBlockRequest) list;
|
2019-01-08 17:48:55 +03:00
|
|
|
BlockAcctCookie acct;
|
2019-01-08 17:48:57 +03:00
|
|
|
} XenBlockRequest;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
struct XenBlockDataPlane {
|
2019-01-08 17:48:55 +03:00
|
|
|
XenDevice *xendev;
|
|
|
|
XenEventChannel *event_channel;
|
|
|
|
unsigned int *ring_ref;
|
|
|
|
unsigned int nr_ring_ref;
|
|
|
|
void *sring;
|
|
|
|
int protocol;
|
|
|
|
blkif_back_rings_t rings;
|
|
|
|
int more_work;
|
2019-01-08 17:48:57 +03:00
|
|
|
QLIST_HEAD(inflight_head, XenBlockRequest) inflight;
|
|
|
|
QLIST_HEAD(freelist_head, XenBlockRequest) freelist;
|
2019-01-08 17:48:55 +03:00
|
|
|
int requests_total;
|
|
|
|
int requests_inflight;
|
|
|
|
unsigned int max_requests;
|
|
|
|
BlockBackend *blk;
|
2019-04-09 19:40:38 +03:00
|
|
|
unsigned int sector_size;
|
2019-01-08 17:48:55 +03:00
|
|
|
QEMUBH *bh;
|
|
|
|
IOThread *iothread;
|
|
|
|
AioContext *ctx;
|
2019-01-08 17:48:53 +03:00
|
|
|
};
|
|
|
|
|
xen-block: Fix double qlist remove and request leak
Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on
remove") revealed that a request was removed twice from a list, once
in xen_block_finish_request() and a second time in
xen_block_release_request() when both function are called from
xen_block_complete_aio(). But also, the `requests_inflight' counter is
decreased twice, and thus became negative.
This is a bug that was introduced in bfd0d6366043 ("xen-block: improve
response latency"), where a `finished' list was removed.
That commit also introduced a leak of request in xen_block_do_aio().
That function calls xen_block_finish_request() but the request is
never released after that.
To fix both issue, we do two changes:
- we squash finish_request() and release_request() together as we want
to remove a request from 'inflight' list to add it to 'freelist'.
- before releasing a request, we need to let the other end know the
result, thus we should call xen_block_send_response() before
releasing a request.
The first change fixes the double QLIST_REMOVE() as we remove the extra
call. The second change makes the leak go away because if we want to
call finish_request(), we need to call a function that does all of
finish, send response, and release.
Fixes: bfd0d6366043 ("xen-block: improve response latency")
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Message-Id: <20200406140217.1441858-1-anthony.perard@citrix.com>
Reviewed-by: Paul Durrant <paul@xen.org>
[mreitz: Amended commit message as per Paul's suggestions]
Signed-off-by: Max Reitz <mreitz@redhat.com>
2020-04-06 17:02:17 +03:00
|
|
|
static int xen_block_send_response(XenBlockRequest *request);
|
|
|
|
|
2019-01-08 17:48:58 +03:00
|
|
|
static void reset_request(XenBlockRequest *request)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:57 +03:00
|
|
|
memset(&request->req, 0, sizeof(request->req));
|
|
|
|
request->status = 0;
|
|
|
|
request->start = 0;
|
|
|
|
request->size = 0;
|
|
|
|
request->presync = 0;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
request->aio_inflight = 0;
|
|
|
|
request->aio_errors = 0;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
request->dataplane = NULL;
|
|
|
|
memset(&request->list, 0, sizeof(request->list));
|
|
|
|
memset(&request->acct, 0, sizeof(request->acct));
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
qemu_iovec_reset(&request->v);
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:58 +03:00
|
|
|
static XenBlockRequest *xen_block_start_request(XenBlockDataPlane *dataplane)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:57 +03:00
|
|
|
XenBlockRequest *request = NULL;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
if (QLIST_EMPTY(&dataplane->freelist)) {
|
|
|
|
if (dataplane->requests_total >= dataplane->max_requests) {
|
2019-01-08 17:48:53 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* allocate new struct */
|
2019-01-08 17:48:57 +03:00
|
|
|
request = g_malloc0(sizeof(*request));
|
|
|
|
request->dataplane = dataplane;
|
2018-12-12 14:16:26 +03:00
|
|
|
/*
|
|
|
|
* We cannot need more pages per requests than this, and since we
|
|
|
|
* re-use requests, allocate the memory once here. It will be freed
|
|
|
|
* xen_block_dataplane_destroy() when the request list is freed.
|
|
|
|
*/
|
|
|
|
request->buf = qemu_memalign(XC_PAGE_SIZE,
|
|
|
|
BLKIF_MAX_SEGMENTS_PER_REQUEST *
|
|
|
|
XC_PAGE_SIZE);
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->requests_total++;
|
2019-01-08 17:48:57 +03:00
|
|
|
qemu_iovec_init(&request->v, 1);
|
2019-01-08 17:48:53 +03:00
|
|
|
} else {
|
|
|
|
/* get one from freelist */
|
2019-01-08 17:48:57 +03:00
|
|
|
request = QLIST_FIRST(&dataplane->freelist);
|
|
|
|
QLIST_REMOVE(request, list);
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
2019-01-08 17:48:57 +03:00
|
|
|
QLIST_INSERT_HEAD(&dataplane->inflight, request, list);
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->requests_inflight++;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
|
|
|
out:
|
2019-01-08 17:48:57 +03:00
|
|
|
return request;
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
|
xen-block: Fix double qlist remove and request leak
Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on
remove") revealed that a request was removed twice from a list, once
in xen_block_finish_request() and a second time in
xen_block_release_request() when both function are called from
xen_block_complete_aio(). But also, the `requests_inflight' counter is
decreased twice, and thus became negative.
This is a bug that was introduced in bfd0d6366043 ("xen-block: improve
response latency"), where a `finished' list was removed.
That commit also introduced a leak of request in xen_block_do_aio().
That function calls xen_block_finish_request() but the request is
never released after that.
To fix both issue, we do two changes:
- we squash finish_request() and release_request() together as we want
to remove a request from 'inflight' list to add it to 'freelist'.
- before releasing a request, we need to let the other end know the
result, thus we should call xen_block_send_response() before
releasing a request.
The first change fixes the double QLIST_REMOVE() as we remove the extra
call. The second change makes the leak go away because if we want to
call finish_request(), we need to call a function that does all of
finish, send response, and release.
Fixes: bfd0d6366043 ("xen-block: improve response latency")
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Message-Id: <20200406140217.1441858-1-anthony.perard@citrix.com>
Reviewed-by: Paul Durrant <paul@xen.org>
[mreitz: Amended commit message as per Paul's suggestions]
Signed-off-by: Max Reitz <mreitz@redhat.com>
2020-04-06 17:02:17 +03:00
|
|
|
static void xen_block_complete_request(XenBlockRequest *request)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:57 +03:00
|
|
|
XenBlockDataPlane *dataplane = request->dataplane;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
xen-block: Fix double qlist remove and request leak
Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on
remove") revealed that a request was removed twice from a list, once
in xen_block_finish_request() and a second time in
xen_block_release_request() when both function are called from
xen_block_complete_aio(). But also, the `requests_inflight' counter is
decreased twice, and thus became negative.
This is a bug that was introduced in bfd0d6366043 ("xen-block: improve
response latency"), where a `finished' list was removed.
That commit also introduced a leak of request in xen_block_do_aio().
That function calls xen_block_finish_request() but the request is
never released after that.
To fix both issue, we do two changes:
- we squash finish_request() and release_request() together as we want
to remove a request from 'inflight' list to add it to 'freelist'.
- before releasing a request, we need to let the other end know the
result, thus we should call xen_block_send_response() before
releasing a request.
The first change fixes the double QLIST_REMOVE() as we remove the extra
call. The second change makes the leak go away because if we want to
call finish_request(), we need to call a function that does all of
finish, send response, and release.
Fixes: bfd0d6366043 ("xen-block: improve response latency")
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Message-Id: <20200406140217.1441858-1-anthony.perard@citrix.com>
Reviewed-by: Paul Durrant <paul@xen.org>
[mreitz: Amended commit message as per Paul's suggestions]
Signed-off-by: Max Reitz <mreitz@redhat.com>
2020-04-06 17:02:17 +03:00
|
|
|
if (xen_block_send_response(request)) {
|
|
|
|
Error *local_err = NULL;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
xen-block: Fix double qlist remove and request leak
Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on
remove") revealed that a request was removed twice from a list, once
in xen_block_finish_request() and a second time in
xen_block_release_request() when both function are called from
xen_block_complete_aio(). But also, the `requests_inflight' counter is
decreased twice, and thus became negative.
This is a bug that was introduced in bfd0d6366043 ("xen-block: improve
response latency"), where a `finished' list was removed.
That commit also introduced a leak of request in xen_block_do_aio().
That function calls xen_block_finish_request() but the request is
never released after that.
To fix both issue, we do two changes:
- we squash finish_request() and release_request() together as we want
to remove a request from 'inflight' list to add it to 'freelist'.
- before releasing a request, we need to let the other end know the
result, thus we should call xen_block_send_response() before
releasing a request.
The first change fixes the double QLIST_REMOVE() as we remove the extra
call. The second change makes the leak go away because if we want to
call finish_request(), we need to call a function that does all of
finish, send response, and release.
Fixes: bfd0d6366043 ("xen-block: improve response latency")
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Message-Id: <20200406140217.1441858-1-anthony.perard@citrix.com>
Reviewed-by: Paul Durrant <paul@xen.org>
[mreitz: Amended commit message as per Paul's suggestions]
Signed-off-by: Max Reitz <mreitz@redhat.com>
2020-04-06 17:02:17 +03:00
|
|
|
xen_device_notify_event_channel(dataplane->xendev,
|
|
|
|
dataplane->event_channel,
|
|
|
|
&local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_report_err(local_err);
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
QLIST_REMOVE(request, list);
|
xen-block: Fix double qlist remove and request leak
Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on
remove") revealed that a request was removed twice from a list, once
in xen_block_finish_request() and a second time in
xen_block_release_request() when both function are called from
xen_block_complete_aio(). But also, the `requests_inflight' counter is
decreased twice, and thus became negative.
This is a bug that was introduced in bfd0d6366043 ("xen-block: improve
response latency"), where a `finished' list was removed.
That commit also introduced a leak of request in xen_block_do_aio().
That function calls xen_block_finish_request() but the request is
never released after that.
To fix both issue, we do two changes:
- we squash finish_request() and release_request() together as we want
to remove a request from 'inflight' list to add it to 'freelist'.
- before releasing a request, we need to let the other end know the
result, thus we should call xen_block_send_response() before
releasing a request.
The first change fixes the double QLIST_REMOVE() as we remove the extra
call. The second change makes the leak go away because if we want to
call finish_request(), we need to call a function that does all of
finish, send response, and release.
Fixes: bfd0d6366043 ("xen-block: improve response latency")
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Message-Id: <20200406140217.1441858-1-anthony.perard@citrix.com>
Reviewed-by: Paul Durrant <paul@xen.org>
[mreitz: Amended commit message as per Paul's suggestions]
Signed-off-by: Max Reitz <mreitz@redhat.com>
2020-04-06 17:02:17 +03:00
|
|
|
dataplane->requests_inflight--;
|
2019-01-08 17:48:58 +03:00
|
|
|
reset_request(request);
|
2019-01-08 17:48:57 +03:00
|
|
|
request->dataplane = dataplane;
|
|
|
|
QLIST_INSERT_HEAD(&dataplane->freelist, request, list);
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* translate request into iovec + start offset
|
|
|
|
* do sanity checks along the way
|
|
|
|
*/
|
2019-01-08 17:48:58 +03:00
|
|
|
static int xen_block_parse_request(XenBlockRequest *request)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:57 +03:00
|
|
|
XenBlockDataPlane *dataplane = request->dataplane;
|
2019-01-08 17:48:53 +03:00
|
|
|
size_t len;
|
|
|
|
int i;
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
switch (request->req.operation) {
|
2019-01-08 17:48:53 +03:00
|
|
|
case BLKIF_OP_READ:
|
|
|
|
break;
|
|
|
|
case BLKIF_OP_FLUSH_DISKCACHE:
|
2019-01-08 17:48:57 +03:00
|
|
|
request->presync = 1;
|
|
|
|
if (!request->req.nr_segments) {
|
2019-01-08 17:48:53 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case BLKIF_OP_WRITE:
|
|
|
|
break;
|
|
|
|
case BLKIF_OP_DISCARD:
|
|
|
|
return 0;
|
|
|
|
default:
|
2019-01-08 17:48:57 +03:00
|
|
|
error_report("error: unknown operation (%d)", request->req.operation);
|
2019-01-08 17:48:53 +03:00
|
|
|
goto err;
|
|
|
|
};
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
if (request->req.operation != BLKIF_OP_READ &&
|
2021-01-18 15:34:47 +03:00
|
|
|
!blk_is_writable(dataplane->blk)) {
|
2019-01-08 17:48:54 +03:00
|
|
|
error_report("error: write req for ro device");
|
2019-01-08 17:48:53 +03:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-04-09 19:40:38 +03:00
|
|
|
request->start = request->req.sector_number * dataplane->sector_size;
|
2019-01-08 17:48:57 +03:00
|
|
|
for (i = 0; i < request->req.nr_segments; i++) {
|
2019-01-08 17:48:53 +03:00
|
|
|
if (i == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
|
2019-01-08 17:48:54 +03:00
|
|
|
error_report("error: nr_segments too big");
|
2019-01-08 17:48:53 +03:00
|
|
|
goto err;
|
|
|
|
}
|
2019-01-08 17:48:57 +03:00
|
|
|
if (request->req.seg[i].first_sect > request->req.seg[i].last_sect) {
|
2019-01-08 17:48:54 +03:00
|
|
|
error_report("error: first > last sector");
|
2019-01-08 17:48:53 +03:00
|
|
|
goto err;
|
|
|
|
}
|
2019-04-09 19:40:38 +03:00
|
|
|
if (request->req.seg[i].last_sect * dataplane->sector_size >=
|
2019-01-08 17:48:57 +03:00
|
|
|
XC_PAGE_SIZE) {
|
2019-01-08 17:48:54 +03:00
|
|
|
error_report("error: page crossing");
|
2019-01-08 17:48:53 +03:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
len = (request->req.seg[i].last_sect -
|
2019-04-09 19:40:38 +03:00
|
|
|
request->req.seg[i].first_sect + 1) * dataplane->sector_size;
|
2019-01-08 17:48:57 +03:00
|
|
|
request->size += len;
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
2019-01-31 18:33:16 +03:00
|
|
|
if (request->start + request->size > blk_getlength(dataplane->blk)) {
|
2019-01-08 17:48:54 +03:00
|
|
|
error_report("error: access beyond end of file");
|
2019-01-08 17:48:53 +03:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2019-01-08 17:48:57 +03:00
|
|
|
request->status = BLKIF_RSP_ERROR;
|
2019-01-08 17:48:53 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:58 +03:00
|
|
|
static int xen_block_copy_request(XenBlockRequest *request)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:57 +03:00
|
|
|
XenBlockDataPlane *dataplane = request->dataplane;
|
2019-01-08 17:48:56 +03:00
|
|
|
XenDevice *xendev = dataplane->xendev;
|
2019-01-08 17:48:55 +03:00
|
|
|
XenDeviceGrantCopySegment segs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
|
|
|
|
int i, count;
|
2019-01-08 17:48:57 +03:00
|
|
|
bool to_domain = (request->req.operation == BLKIF_OP_READ);
|
|
|
|
void *virt = request->buf;
|
2019-01-08 17:48:55 +03:00
|
|
|
Error *local_err = NULL;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
if (request->req.nr_segments == 0) {
|
2019-01-08 17:48:53 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
count = request->req.nr_segments;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
if (to_domain) {
|
2019-01-08 17:48:57 +03:00
|
|
|
segs[i].dest.foreign.ref = request->req.seg[i].gref;
|
|
|
|
segs[i].dest.foreign.offset = request->req.seg[i].first_sect *
|
2019-04-09 19:40:38 +03:00
|
|
|
dataplane->sector_size;
|
2019-01-08 17:48:53 +03:00
|
|
|
segs[i].source.virt = virt;
|
|
|
|
} else {
|
2019-01-08 17:48:57 +03:00
|
|
|
segs[i].source.foreign.ref = request->req.seg[i].gref;
|
|
|
|
segs[i].source.foreign.offset = request->req.seg[i].first_sect *
|
2019-04-09 19:40:38 +03:00
|
|
|
dataplane->sector_size;
|
2019-01-08 17:48:53 +03:00
|
|
|
segs[i].dest.virt = virt;
|
|
|
|
}
|
2019-01-08 17:48:57 +03:00
|
|
|
segs[i].len = (request->req.seg[i].last_sect -
|
2019-04-01 15:17:19 +03:00
|
|
|
request->req.seg[i].first_sect + 1) *
|
2019-04-09 19:40:38 +03:00
|
|
|
dataplane->sector_size;
|
2019-01-08 17:48:53 +03:00
|
|
|
virt += segs[i].len;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:55 +03:00
|
|
|
xen_device_copy_grant_refs(xendev, to_domain, segs, count, &local_err);
|
|
|
|
|
|
|
|
if (local_err) {
|
|
|
|
error_reportf_err(local_err, "failed to copy data: ");
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
request->aio_errors++;
|
2019-01-08 17:48:53 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:55 +03:00
|
|
|
return 0;
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:58 +03:00
|
|
|
static int xen_block_do_aio(XenBlockRequest *request);
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:58 +03:00
|
|
|
static void xen_block_complete_aio(void *opaque, int ret)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:57 +03:00
|
|
|
XenBlockRequest *request = opaque;
|
|
|
|
XenBlockDataPlane *dataplane = request->dataplane;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
aio_context_acquire(dataplane->ctx);
|
2019-01-08 17:48:53 +03:00
|
|
|
|
|
|
|
if (ret != 0) {
|
2019-01-08 17:48:54 +03:00
|
|
|
error_report("%s I/O error",
|
2019-01-08 17:48:57 +03:00
|
|
|
request->req.operation == BLKIF_OP_READ ?
|
2019-01-08 17:48:54 +03:00
|
|
|
"read" : "write");
|
2019-01-08 17:48:57 +03:00
|
|
|
request->aio_errors++;
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
request->aio_inflight--;
|
|
|
|
if (request->presync) {
|
|
|
|
request->presync = 0;
|
2019-01-08 17:48:58 +03:00
|
|
|
xen_block_do_aio(request);
|
2019-01-08 17:48:53 +03:00
|
|
|
goto done;
|
|
|
|
}
|
2019-01-08 17:48:57 +03:00
|
|
|
if (request->aio_inflight > 0) {
|
2019-01-08 17:48:53 +03:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
switch (request->req.operation) {
|
2019-01-08 17:48:53 +03:00
|
|
|
case BLKIF_OP_READ:
|
2019-01-08 17:48:57 +03:00
|
|
|
/* in case of failure request->aio_errors is increased */
|
2019-01-08 17:48:53 +03:00
|
|
|
if (ret == 0) {
|
2019-01-08 17:48:58 +03:00
|
|
|
xen_block_copy_request(request);
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BLKIF_OP_WRITE:
|
|
|
|
case BLKIF_OP_FLUSH_DISKCACHE:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
request->status = request->aio_errors ? BLKIF_RSP_ERROR : BLKIF_RSP_OKAY;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
switch (request->req.operation) {
|
2019-01-08 17:48:53 +03:00
|
|
|
case BLKIF_OP_WRITE:
|
|
|
|
case BLKIF_OP_FLUSH_DISKCACHE:
|
2019-01-08 17:48:57 +03:00
|
|
|
if (!request->req.nr_segments) {
|
2019-01-08 17:48:53 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-02-15 19:25:31 +03:00
|
|
|
/* fall through */
|
2019-01-08 17:48:53 +03:00
|
|
|
case BLKIF_OP_READ:
|
2019-01-08 17:48:57 +03:00
|
|
|
if (request->status == BLKIF_RSP_OKAY) {
|
|
|
|
block_acct_done(blk_get_stats(dataplane->blk), &request->acct);
|
2019-01-08 17:48:53 +03:00
|
|
|
} else {
|
2019-01-08 17:48:57 +03:00
|
|
|
block_acct_failed(blk_get_stats(dataplane->blk), &request->acct);
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BLKIF_OP_DISCARD:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-12-12 14:16:25 +03:00
|
|
|
|
xen-block: Fix double qlist remove and request leak
Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on
remove") revealed that a request was removed twice from a list, once
in xen_block_finish_request() and a second time in
xen_block_release_request() when both function are called from
xen_block_complete_aio(). But also, the `requests_inflight' counter is
decreased twice, and thus became negative.
This is a bug that was introduced in bfd0d6366043 ("xen-block: improve
response latency"), where a `finished' list was removed.
That commit also introduced a leak of request in xen_block_do_aio().
That function calls xen_block_finish_request() but the request is
never released after that.
To fix both issue, we do two changes:
- we squash finish_request() and release_request() together as we want
to remove a request from 'inflight' list to add it to 'freelist'.
- before releasing a request, we need to let the other end know the
result, thus we should call xen_block_send_response() before
releasing a request.
The first change fixes the double QLIST_REMOVE() as we remove the extra
call. The second change makes the leak go away because if we want to
call finish_request(), we need to call a function that does all of
finish, send response, and release.
Fixes: bfd0d6366043 ("xen-block: improve response latency")
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Message-Id: <20200406140217.1441858-1-anthony.perard@citrix.com>
Reviewed-by: Paul Durrant <paul@xen.org>
[mreitz: Amended commit message as per Paul's suggestions]
Signed-off-by: Max Reitz <mreitz@redhat.com>
2020-04-06 17:02:17 +03:00
|
|
|
xen_block_complete_request(request);
|
2018-12-12 14:16:25 +03:00
|
|
|
|
2019-04-08 18:16:17 +03:00
|
|
|
if (dataplane->more_work) {
|
|
|
|
qemu_bh_schedule(dataplane->bh);
|
|
|
|
}
|
2019-01-08 17:48:53 +03:00
|
|
|
|
|
|
|
done:
|
2019-01-08 17:48:56 +03:00
|
|
|
aio_context_release(dataplane->ctx);
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:58 +03:00
|
|
|
static bool xen_block_split_discard(XenBlockRequest *request,
|
|
|
|
blkif_sector_t sector_number,
|
|
|
|
uint64_t nr_sectors)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:57 +03:00
|
|
|
XenBlockDataPlane *dataplane = request->dataplane;
|
2019-01-08 17:48:53 +03:00
|
|
|
int64_t byte_offset;
|
|
|
|
int byte_chunk;
|
2019-04-01 15:17:19 +03:00
|
|
|
uint64_t byte_remaining;
|
2019-01-08 17:48:53 +03:00
|
|
|
uint64_t sec_start = sector_number;
|
|
|
|
uint64_t sec_count = nr_sectors;
|
|
|
|
|
|
|
|
/* Wrap around, or overflowing byte limit? */
|
|
|
|
if (sec_start + sec_count < sec_count ||
|
2019-04-09 19:40:38 +03:00
|
|
|
sec_start + sec_count > INT64_MAX / dataplane->sector_size) {
|
2019-01-08 17:48:53 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-09 19:40:38 +03:00
|
|
|
byte_offset = sec_start * dataplane->sector_size;
|
|
|
|
byte_remaining = sec_count * dataplane->sector_size;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
|
|
|
do {
|
2019-04-01 15:17:19 +03:00
|
|
|
byte_chunk = byte_remaining > BDRV_REQUEST_MAX_BYTES ?
|
|
|
|
BDRV_REQUEST_MAX_BYTES : byte_remaining;
|
2019-01-08 17:48:57 +03:00
|
|
|
request->aio_inflight++;
|
2019-01-08 17:48:56 +03:00
|
|
|
blk_aio_pdiscard(dataplane->blk, byte_offset, byte_chunk,
|
2019-01-08 17:48:58 +03:00
|
|
|
xen_block_complete_aio, request);
|
2019-01-08 17:48:53 +03:00
|
|
|
byte_remaining -= byte_chunk;
|
|
|
|
byte_offset += byte_chunk;
|
|
|
|
} while (byte_remaining > 0);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:58 +03:00
|
|
|
static int xen_block_do_aio(XenBlockRequest *request)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:57 +03:00
|
|
|
XenBlockDataPlane *dataplane = request->dataplane;
|
|
|
|
|
|
|
|
if (request->req.nr_segments &&
|
|
|
|
(request->req.operation == BLKIF_OP_WRITE ||
|
|
|
|
request->req.operation == BLKIF_OP_FLUSH_DISKCACHE) &&
|
2019-01-08 17:48:58 +03:00
|
|
|
xen_block_copy_request(request)) {
|
2019-01-08 17:48:53 +03:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
request->aio_inflight++;
|
|
|
|
if (request->presync) {
|
2019-01-08 17:48:58 +03:00
|
|
|
blk_aio_flush(request->dataplane->blk, xen_block_complete_aio,
|
|
|
|
request);
|
2019-01-08 17:48:53 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
switch (request->req.operation) {
|
2019-01-08 17:48:53 +03:00
|
|
|
case BLKIF_OP_READ:
|
2019-01-08 17:48:57 +03:00
|
|
|
qemu_iovec_add(&request->v, request->buf, request->size);
|
|
|
|
block_acct_start(blk_get_stats(dataplane->blk), &request->acct,
|
|
|
|
request->v.size, BLOCK_ACCT_READ);
|
|
|
|
request->aio_inflight++;
|
|
|
|
blk_aio_preadv(dataplane->blk, request->start, &request->v, 0,
|
2019-01-08 17:48:58 +03:00
|
|
|
xen_block_complete_aio, request);
|
2019-01-08 17:48:53 +03:00
|
|
|
break;
|
|
|
|
case BLKIF_OP_WRITE:
|
|
|
|
case BLKIF_OP_FLUSH_DISKCACHE:
|
2019-01-08 17:48:57 +03:00
|
|
|
if (!request->req.nr_segments) {
|
2019-01-08 17:48:53 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
qemu_iovec_add(&request->v, request->buf, request->size);
|
|
|
|
block_acct_start(blk_get_stats(dataplane->blk), &request->acct,
|
|
|
|
request->v.size,
|
|
|
|
request->req.operation == BLKIF_OP_WRITE ?
|
2019-01-08 17:48:53 +03:00
|
|
|
BLOCK_ACCT_WRITE : BLOCK_ACCT_FLUSH);
|
2019-01-08 17:48:57 +03:00
|
|
|
request->aio_inflight++;
|
|
|
|
blk_aio_pwritev(dataplane->blk, request->start, &request->v, 0,
|
2019-01-08 17:48:58 +03:00
|
|
|
xen_block_complete_aio, request);
|
2019-01-08 17:48:53 +03:00
|
|
|
break;
|
|
|
|
case BLKIF_OP_DISCARD:
|
|
|
|
{
|
2019-01-08 17:48:57 +03:00
|
|
|
struct blkif_request_discard *req = (void *)&request->req;
|
2019-01-08 17:48:58 +03:00
|
|
|
if (!xen_block_split_discard(request, req->sector_number,
|
|
|
|
req->nr_sectors)) {
|
2019-01-08 17:48:53 +03:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
/* unknown operation (shouldn't happen -- parse catches this) */
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:58 +03:00
|
|
|
xen_block_complete_aio(request, 0);
|
2019-01-08 17:48:53 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2019-01-08 17:48:57 +03:00
|
|
|
request->status = BLKIF_RSP_ERROR;
|
xen-block: Fix double qlist remove and request leak
Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on
remove") revealed that a request was removed twice from a list, once
in xen_block_finish_request() and a second time in
xen_block_release_request() when both function are called from
xen_block_complete_aio(). But also, the `requests_inflight' counter is
decreased twice, and thus became negative.
This is a bug that was introduced in bfd0d6366043 ("xen-block: improve
response latency"), where a `finished' list was removed.
That commit also introduced a leak of request in xen_block_do_aio().
That function calls xen_block_finish_request() but the request is
never released after that.
To fix both issue, we do two changes:
- we squash finish_request() and release_request() together as we want
to remove a request from 'inflight' list to add it to 'freelist'.
- before releasing a request, we need to let the other end know the
result, thus we should call xen_block_send_response() before
releasing a request.
The first change fixes the double QLIST_REMOVE() as we remove the extra
call. The second change makes the leak go away because if we want to
call finish_request(), we need to call a function that does all of
finish, send response, and release.
Fixes: bfd0d6366043 ("xen-block: improve response latency")
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Message-Id: <20200406140217.1441858-1-anthony.perard@citrix.com>
Reviewed-by: Paul Durrant <paul@xen.org>
[mreitz: Amended commit message as per Paul's suggestions]
Signed-off-by: Max Reitz <mreitz@redhat.com>
2020-04-06 17:02:17 +03:00
|
|
|
xen_block_complete_request(request);
|
2019-01-08 17:48:53 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-12 14:16:25 +03:00
|
|
|
static int xen_block_send_response(XenBlockRequest *request)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:57 +03:00
|
|
|
XenBlockDataPlane *dataplane = request->dataplane;
|
2019-01-08 17:48:55 +03:00
|
|
|
int send_notify = 0;
|
|
|
|
int have_requests = 0;
|
|
|
|
blkif_response_t *resp;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
|
|
|
/* Place on the response ring for the relevant domain. */
|
2019-01-08 17:48:56 +03:00
|
|
|
switch (dataplane->protocol) {
|
2019-01-08 17:48:53 +03:00
|
|
|
case BLKIF_PROTOCOL_NATIVE:
|
|
|
|
resp = (blkif_response_t *)RING_GET_RESPONSE(
|
2019-01-08 17:48:56 +03:00
|
|
|
&dataplane->rings.native,
|
|
|
|
dataplane->rings.native.rsp_prod_pvt);
|
2019-01-08 17:48:53 +03:00
|
|
|
break;
|
|
|
|
case BLKIF_PROTOCOL_X86_32:
|
|
|
|
resp = (blkif_response_t *)RING_GET_RESPONSE(
|
2019-01-08 17:48:56 +03:00
|
|
|
&dataplane->rings.x86_32_part,
|
|
|
|
dataplane->rings.x86_32_part.rsp_prod_pvt);
|
2019-01-08 17:48:53 +03:00
|
|
|
break;
|
|
|
|
case BLKIF_PROTOCOL_X86_64:
|
|
|
|
resp = (blkif_response_t *)RING_GET_RESPONSE(
|
2019-01-08 17:48:56 +03:00
|
|
|
&dataplane->rings.x86_64_part,
|
|
|
|
dataplane->rings.x86_64_part.rsp_prod_pvt);
|
2019-01-08 17:48:53 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
resp->id = request->req.id;
|
|
|
|
resp->operation = request->req.operation;
|
|
|
|
resp->status = request->status;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->rings.common.rsp_prod_pvt++;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&dataplane->rings.common,
|
|
|
|
send_notify);
|
|
|
|
if (dataplane->rings.common.rsp_prod_pvt ==
|
|
|
|
dataplane->rings.common.req_cons) {
|
2019-01-08 17:48:53 +03:00
|
|
|
/*
|
|
|
|
* Tail check for pending requests. Allows frontend to avoid
|
|
|
|
* notifications if requests are already in flight (lower
|
|
|
|
* overheads and promotes batching).
|
|
|
|
*/
|
2019-01-08 17:48:56 +03:00
|
|
|
RING_FINAL_CHECK_FOR_REQUESTS(&dataplane->rings.common,
|
|
|
|
have_requests);
|
|
|
|
} else if (RING_HAS_UNCONSUMED_REQUESTS(&dataplane->rings.common)) {
|
2019-01-08 17:48:53 +03:00
|
|
|
have_requests = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_requests) {
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->more_work++;
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
return send_notify;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:58 +03:00
|
|
|
static int xen_block_get_request(XenBlockDataPlane *dataplane,
|
|
|
|
XenBlockRequest *request, RING_IDX rc)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:56 +03:00
|
|
|
switch (dataplane->protocol) {
|
|
|
|
case BLKIF_PROTOCOL_NATIVE: {
|
|
|
|
blkif_request_t *req =
|
|
|
|
RING_GET_REQUEST(&dataplane->rings.native, rc);
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
memcpy(&request->req, req, sizeof(request->req));
|
2019-01-08 17:48:53 +03:00
|
|
|
break;
|
2019-01-08 17:48:56 +03:00
|
|
|
}
|
|
|
|
case BLKIF_PROTOCOL_X86_32: {
|
|
|
|
blkif_x86_32_request_t *req =
|
|
|
|
RING_GET_REQUEST(&dataplane->rings.x86_32_part, rc);
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
blkif_get_x86_32_req(&request->req, req);
|
2019-01-08 17:48:53 +03:00
|
|
|
break;
|
2019-01-08 17:48:56 +03:00
|
|
|
}
|
|
|
|
case BLKIF_PROTOCOL_X86_64: {
|
|
|
|
blkif_x86_64_request_t *req =
|
|
|
|
RING_GET_REQUEST(&dataplane->rings.x86_64_part, rc);
|
|
|
|
|
2019-01-08 17:48:57 +03:00
|
|
|
blkif_get_x86_64_req(&request->req, req);
|
2019-01-08 17:48:53 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-01-08 17:48:56 +03:00
|
|
|
}
|
2019-01-08 17:48:53 +03:00
|
|
|
/* Prevent the compiler from accessing the on-ring fields instead. */
|
|
|
|
barrier();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-12 14:16:24 +03:00
|
|
|
/*
|
|
|
|
* Threshold of in-flight requests above which we will start using
|
|
|
|
* blk_io_plug()/blk_io_unplug() to batch requests.
|
|
|
|
*/
|
|
|
|
#define IO_PLUG_THRESHOLD 1
|
|
|
|
|
2019-04-08 18:16:17 +03:00
|
|
|
static bool xen_block_handle_requests(XenBlockDataPlane *dataplane)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
|
|
|
RING_IDX rc, rp;
|
2019-01-08 17:48:57 +03:00
|
|
|
XenBlockRequest *request;
|
2018-12-12 14:16:24 +03:00
|
|
|
int inflight_atstart = dataplane->requests_inflight;
|
|
|
|
int batched = 0;
|
2019-04-08 18:16:17 +03:00
|
|
|
bool done_something = false;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->more_work = 0;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
rc = dataplane->rings.common.req_cons;
|
|
|
|
rp = dataplane->rings.common.sring->req_prod;
|
2019-01-08 17:48:53 +03:00
|
|
|
xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
|
|
|
|
|
2018-12-12 14:16:24 +03:00
|
|
|
/*
|
|
|
|
* If there was more than IO_PLUG_THRESHOLD requests in flight
|
|
|
|
* when we got here, this is an indication that there the bottleneck
|
|
|
|
* is below us, so it's worth beginning to batch up I/O requests
|
|
|
|
* rather than submitting them immediately. The maximum number
|
|
|
|
* of requests we're willing to batch is the number already in
|
|
|
|
* flight, so it can grow up to max_requests when the bottleneck
|
|
|
|
* is below us.
|
|
|
|
*/
|
|
|
|
if (inflight_atstart > IO_PLUG_THRESHOLD) {
|
|
|
|
blk_io_plug(dataplane->blk);
|
|
|
|
}
|
2019-01-08 17:48:53 +03:00
|
|
|
while (rc != rp) {
|
|
|
|
/* pull request from ring */
|
2019-01-08 17:48:56 +03:00
|
|
|
if (RING_REQUEST_CONS_OVERFLOW(&dataplane->rings.common, rc)) {
|
2019-01-08 17:48:53 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-01-08 17:48:58 +03:00
|
|
|
request = xen_block_start_request(dataplane);
|
2019-01-08 17:48:57 +03:00
|
|
|
if (request == NULL) {
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->more_work++;
|
2019-01-08 17:48:53 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-01-08 17:48:58 +03:00
|
|
|
xen_block_get_request(dataplane, request, rc);
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->rings.common.req_cons = ++rc;
|
2019-04-08 18:16:17 +03:00
|
|
|
done_something = true;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
|
|
|
/* parse them */
|
2019-01-08 17:48:58 +03:00
|
|
|
if (xen_block_parse_request(request) != 0) {
|
2019-01-08 17:48:57 +03:00
|
|
|
switch (request->req.operation) {
|
2019-01-08 17:48:53 +03:00
|
|
|
case BLKIF_OP_READ:
|
2019-01-08 17:48:56 +03:00
|
|
|
block_acct_invalid(blk_get_stats(dataplane->blk),
|
2019-01-08 17:48:53 +03:00
|
|
|
BLOCK_ACCT_READ);
|
|
|
|
break;
|
|
|
|
case BLKIF_OP_WRITE:
|
2019-01-08 17:48:56 +03:00
|
|
|
block_acct_invalid(blk_get_stats(dataplane->blk),
|
2019-01-08 17:48:53 +03:00
|
|
|
BLOCK_ACCT_WRITE);
|
|
|
|
break;
|
|
|
|
case BLKIF_OP_FLUSH_DISKCACHE:
|
2019-01-08 17:48:56 +03:00
|
|
|
block_acct_invalid(blk_get_stats(dataplane->blk),
|
2019-01-08 17:48:53 +03:00
|
|
|
BLOCK_ACCT_FLUSH);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
xen-block: Fix double qlist remove and request leak
Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on
remove") revealed that a request was removed twice from a list, once
in xen_block_finish_request() and a second time in
xen_block_release_request() when both function are called from
xen_block_complete_aio(). But also, the `requests_inflight' counter is
decreased twice, and thus became negative.
This is a bug that was introduced in bfd0d6366043 ("xen-block: improve
response latency"), where a `finished' list was removed.
That commit also introduced a leak of request in xen_block_do_aio().
That function calls xen_block_finish_request() but the request is
never released after that.
To fix both issue, we do two changes:
- we squash finish_request() and release_request() together as we want
to remove a request from 'inflight' list to add it to 'freelist'.
- before releasing a request, we need to let the other end know the
result, thus we should call xen_block_send_response() before
releasing a request.
The first change fixes the double QLIST_REMOVE() as we remove the extra
call. The second change makes the leak go away because if we want to
call finish_request(), we need to call a function that does all of
finish, send response, and release.
Fixes: bfd0d6366043 ("xen-block: improve response latency")
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Message-Id: <20200406140217.1441858-1-anthony.perard@citrix.com>
Reviewed-by: Paul Durrant <paul@xen.org>
[mreitz: Amended commit message as per Paul's suggestions]
Signed-off-by: Max Reitz <mreitz@redhat.com>
2020-04-06 17:02:17 +03:00
|
|
|
xen_block_complete_request(request);
|
2019-01-08 17:48:53 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-12 14:16:24 +03:00
|
|
|
if (inflight_atstart > IO_PLUG_THRESHOLD &&
|
|
|
|
batched >= inflight_atstart) {
|
|
|
|
blk_io_unplug(dataplane->blk);
|
|
|
|
}
|
2019-01-08 17:48:58 +03:00
|
|
|
xen_block_do_aio(request);
|
2018-12-12 14:16:24 +03:00
|
|
|
if (inflight_atstart > IO_PLUG_THRESHOLD) {
|
|
|
|
if (batched >= inflight_atstart) {
|
|
|
|
blk_io_plug(dataplane->blk);
|
|
|
|
batched = 0;
|
|
|
|
} else {
|
|
|
|
batched++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (inflight_atstart > IO_PLUG_THRESHOLD) {
|
|
|
|
blk_io_unplug(dataplane->blk);
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
|
2019-04-08 18:16:17 +03:00
|
|
|
return done_something;
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:58 +03:00
|
|
|
static void xen_block_dataplane_bh(void *opaque)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:56 +03:00
|
|
|
XenBlockDataPlane *dataplane = opaque;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
aio_context_acquire(dataplane->ctx);
|
2019-01-08 17:48:58 +03:00
|
|
|
xen_block_handle_requests(dataplane);
|
2019-01-08 17:48:56 +03:00
|
|
|
aio_context_release(dataplane->ctx);
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
|
2019-04-08 18:16:17 +03:00
|
|
|
static bool xen_block_dataplane_event(void *opaque)
|
2019-01-08 17:48:55 +03:00
|
|
|
{
|
2019-01-08 17:48:56 +03:00
|
|
|
XenBlockDataPlane *dataplane = opaque;
|
2019-01-08 17:48:55 +03:00
|
|
|
|
2019-04-08 18:16:17 +03:00
|
|
|
return xen_block_handle_requests(dataplane);
|
2019-01-08 17:48:55 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
XenBlockDataPlane *xen_block_dataplane_create(XenDevice *xendev,
|
2019-04-09 19:40:38 +03:00
|
|
|
BlockBackend *blk,
|
|
|
|
unsigned int sector_size,
|
2019-01-08 17:48:56 +03:00
|
|
|
IOThread *iothread)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:56 +03:00
|
|
|
XenBlockDataPlane *dataplane = g_new0(XenBlockDataPlane, 1);
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->xendev = xendev;
|
2019-04-09 19:40:38 +03:00
|
|
|
dataplane->blk = blk;
|
|
|
|
dataplane->sector_size = sector_size;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
QLIST_INIT(&dataplane->inflight);
|
|
|
|
QLIST_INIT(&dataplane->freelist);
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:55 +03:00
|
|
|
if (iothread) {
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->iothread = iothread;
|
|
|
|
object_ref(OBJECT(dataplane->iothread));
|
|
|
|
dataplane->ctx = iothread_get_aio_context(dataplane->iothread);
|
2019-01-08 17:48:55 +03:00
|
|
|
} else {
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->ctx = qemu_get_aio_context();
|
2019-01-08 17:48:55 +03:00
|
|
|
}
|
2019-01-08 17:48:58 +03:00
|
|
|
dataplane->bh = aio_bh_new(dataplane->ctx, xen_block_dataplane_bh,
|
|
|
|
dataplane);
|
2019-01-08 17:48:55 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
return dataplane;
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
void xen_block_dataplane_destroy(XenBlockDataPlane *dataplane)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:57 +03:00
|
|
|
XenBlockRequest *request;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
if (!dataplane) {
|
2019-01-08 17:48:55 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
while (!QLIST_EMPTY(&dataplane->freelist)) {
|
2019-01-08 17:48:57 +03:00
|
|
|
request = QLIST_FIRST(&dataplane->freelist);
|
|
|
|
QLIST_REMOVE(request, list);
|
|
|
|
qemu_iovec_destroy(&request->v);
|
2018-12-12 14:16:26 +03:00
|
|
|
qemu_vfree(request->buf);
|
2019-01-08 17:48:57 +03:00
|
|
|
g_free(request);
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
qemu_bh_delete(dataplane->bh);
|
|
|
|
if (dataplane->iothread) {
|
|
|
|
object_unref(OBJECT(dataplane->iothread));
|
2019-01-08 17:48:55 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
g_free(dataplane);
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
void xen_block_dataplane_stop(XenBlockDataPlane *dataplane)
|
2019-01-08 17:48:53 +03:00
|
|
|
{
|
2019-01-08 17:48:55 +03:00
|
|
|
XenDevice *xendev;
|
2019-01-08 17:48:53 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
if (!dataplane) {
|
2019-01-08 17:48:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:34:51 +03:00
|
|
|
xendev = dataplane->xendev;
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
aio_context_acquire(dataplane->ctx);
|
2019-12-16 17:34:51 +03:00
|
|
|
if (dataplane->event_channel) {
|
|
|
|
/* Only reason for failure is a NULL channel */
|
|
|
|
xen_device_set_event_channel_context(xendev, dataplane->event_channel,
|
|
|
|
qemu_get_aio_context(),
|
|
|
|
&error_abort);
|
|
|
|
}
|
2019-05-02 12:10:59 +03:00
|
|
|
/* Xen doesn't have multiple users for nodes, so this can't fail */
|
|
|
|
blk_set_aio_context(dataplane->blk, qemu_get_aio_context(), &error_abort);
|
2019-01-08 17:48:56 +03:00
|
|
|
aio_context_release(dataplane->ctx);
|
2019-01-08 17:48:55 +03:00
|
|
|
|
2019-12-16 17:34:51 +03:00
|
|
|
/*
|
|
|
|
* Now that the context has been moved onto the main thread, cancel
|
|
|
|
* further processing.
|
|
|
|
*/
|
|
|
|
qemu_bh_cancel(dataplane->bh);
|
2019-01-08 17:48:55 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
if (dataplane->event_channel) {
|
2019-01-08 17:48:55 +03:00
|
|
|
Error *local_err = NULL;
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
xen_device_unbind_event_channel(xendev, dataplane->event_channel,
|
2019-01-08 17:48:55 +03:00
|
|
|
&local_err);
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->event_channel = NULL;
|
2019-01-08 17:48:55 +03:00
|
|
|
|
|
|
|
if (local_err) {
|
|
|
|
error_report_err(local_err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
if (dataplane->sring) {
|
2019-01-08 17:48:55 +03:00
|
|
|
Error *local_err = NULL;
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
xen_device_unmap_grant_refs(xendev, dataplane->sring,
|
|
|
|
dataplane->nr_ring_ref, &local_err);
|
|
|
|
dataplane->sring = NULL;
|
2019-01-08 17:48:55 +03:00
|
|
|
|
|
|
|
if (local_err) {
|
|
|
|
error_report_err(local_err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
g_free(dataplane->ring_ref);
|
|
|
|
dataplane->ring_ref = NULL;
|
2019-01-08 17:48:55 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
void xen_block_dataplane_start(XenBlockDataPlane *dataplane,
|
2019-01-08 17:48:55 +03:00
|
|
|
const unsigned int ring_ref[],
|
|
|
|
unsigned int nr_ring_ref,
|
|
|
|
unsigned int event_channel,
|
|
|
|
unsigned int protocol,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2020-07-07 19:50:37 +03:00
|
|
|
ERRP_GUARD();
|
2019-01-08 17:48:56 +03:00
|
|
|
XenDevice *xendev = dataplane->xendev;
|
2020-12-14 20:05:16 +03:00
|
|
|
AioContext *old_context;
|
2019-01-08 17:48:55 +03:00
|
|
|
unsigned int ring_size;
|
|
|
|
unsigned int i;
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->nr_ring_ref = nr_ring_ref;
|
|
|
|
dataplane->ring_ref = g_new(unsigned int, nr_ring_ref);
|
2019-01-08 17:48:55 +03:00
|
|
|
|
|
|
|
for (i = 0; i < nr_ring_ref; i++) {
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->ring_ref[i] = ring_ref[i];
|
2019-01-08 17:48:55 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->protocol = protocol;
|
2019-01-08 17:48:55 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
ring_size = XC_PAGE_SIZE * dataplane->nr_ring_ref;
|
|
|
|
switch (dataplane->protocol) {
|
2019-01-08 17:48:55 +03:00
|
|
|
case BLKIF_PROTOCOL_NATIVE:
|
|
|
|
{
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->max_requests = __CONST_RING_SIZE(blkif, ring_size);
|
2019-01-08 17:48:55 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BLKIF_PROTOCOL_X86_32:
|
|
|
|
{
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->max_requests = __CONST_RING_SIZE(blkif_x86_32, ring_size);
|
2019-01-08 17:48:55 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BLKIF_PROTOCOL_X86_64:
|
|
|
|
{
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->max_requests = __CONST_RING_SIZE(blkif_x86_64, ring_size);
|
2019-01-08 17:48:55 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2019-01-08 17:48:56 +03:00
|
|
|
error_setg(errp, "unknown protocol %u", dataplane->protocol);
|
2019-01-08 17:48:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
xen_device_set_max_grant_refs(xendev, dataplane->nr_ring_ref,
|
2020-07-07 19:50:37 +03:00
|
|
|
errp);
|
|
|
|
if (*errp) {
|
2019-01-08 17:48:55 +03:00
|
|
|
goto stop;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->sring = xen_device_map_grant_refs(xendev,
|
|
|
|
dataplane->ring_ref,
|
|
|
|
dataplane->nr_ring_ref,
|
2019-01-08 17:48:55 +03:00
|
|
|
PROT_READ | PROT_WRITE,
|
2020-07-07 19:50:37 +03:00
|
|
|
errp);
|
|
|
|
if (*errp) {
|
2019-01-08 17:48:55 +03:00
|
|
|
goto stop;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
switch (dataplane->protocol) {
|
2019-01-08 17:48:55 +03:00
|
|
|
case BLKIF_PROTOCOL_NATIVE:
|
|
|
|
{
|
2019-01-08 17:48:56 +03:00
|
|
|
blkif_sring_t *sring_native = dataplane->sring;
|
2019-01-08 17:48:55 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
BACK_RING_INIT(&dataplane->rings.native, sring_native, ring_size);
|
2019-01-08 17:48:55 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BLKIF_PROTOCOL_X86_32:
|
|
|
|
{
|
2019-01-08 17:48:56 +03:00
|
|
|
blkif_x86_32_sring_t *sring_x86_32 = dataplane->sring;
|
2019-01-08 17:48:55 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
BACK_RING_INIT(&dataplane->rings.x86_32_part, sring_x86_32,
|
2019-01-08 17:48:55 +03:00
|
|
|
ring_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BLKIF_PROTOCOL_X86_64:
|
|
|
|
{
|
2019-01-08 17:48:56 +03:00
|
|
|
blkif_x86_64_sring_t *sring_x86_64 = dataplane->sring;
|
2019-01-08 17:48:55 +03:00
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
BACK_RING_INIT(&dataplane->rings.x86_64_part, sring_x86_64,
|
2019-01-08 17:48:55 +03:00
|
|
|
ring_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:56 +03:00
|
|
|
dataplane->event_channel =
|
2019-12-16 17:34:51 +03:00
|
|
|
xen_device_bind_event_channel(xendev, event_channel,
|
2019-01-08 17:48:58 +03:00
|
|
|
xen_block_dataplane_event, dataplane,
|
2020-07-07 19:50:37 +03:00
|
|
|
errp);
|
|
|
|
if (*errp) {
|
2019-01-08 17:48:55 +03:00
|
|
|
goto stop;
|
|
|
|
}
|
|
|
|
|
2020-12-14 20:05:16 +03:00
|
|
|
old_context = blk_get_aio_context(dataplane->blk);
|
|
|
|
aio_context_acquire(old_context);
|
2019-05-02 12:10:59 +03:00
|
|
|
/* If other users keep the BlockBackend in the iothread, that's ok */
|
|
|
|
blk_set_aio_context(dataplane->blk, dataplane->ctx, NULL);
|
2020-12-14 20:05:16 +03:00
|
|
|
aio_context_release(old_context);
|
|
|
|
|
2019-12-16 17:34:51 +03:00
|
|
|
/* Only reason for failure is a NULL channel */
|
2020-12-14 20:05:16 +03:00
|
|
|
aio_context_acquire(dataplane->ctx);
|
2019-12-16 17:34:51 +03:00
|
|
|
xen_device_set_event_channel_context(xendev, dataplane->event_channel,
|
|
|
|
dataplane->ctx, &error_abort);
|
2019-01-08 17:48:56 +03:00
|
|
|
aio_context_release(dataplane->ctx);
|
2019-12-16 17:34:51 +03:00
|
|
|
|
2019-01-08 17:48:55 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
stop:
|
2019-01-08 17:48:56 +03:00
|
|
|
xen_block_dataplane_stop(dataplane);
|
2019-01-08 17:48:53 +03:00
|
|
|
}
|