2 * Copyright (c) 2018 Citrix Systems Inc.
3 * (c) Gerd Hoffmann <kraxel@redhat.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; under version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, see <http://www.gnu.org/licenses/>.
17 * Contributions after 2012-01-13 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
21 #include "qemu/osdep.h"
22 #include "qemu/defer-call.h"
23 #include "qemu/error-report.h"
24 #include "qemu/main-loop.h"
25 #include "qemu/memalign.h"
26 #include "qapi/error.h"
27 #include "hw/xen/xen.h"
28 #include "hw/block/xen_blkif.h"
29 #include "hw/xen/interface/io/ring.h"
30 #include "sysemu/block-backend.h"
31 #include "sysemu/iothread.h"
32 #include "xen-block.h"
34 typedef struct XenBlockRequest
{
44 XenBlockDataPlane
*dataplane
;
45 QLIST_ENTRY(XenBlockRequest
) list
;
49 struct XenBlockDataPlane
{
51 XenEventChannel
*event_channel
;
52 unsigned int *ring_ref
;
53 unsigned int nr_ring_ref
;
56 blkif_back_rings_t rings
;
58 QLIST_HEAD(inflight_head
, XenBlockRequest
) inflight
;
59 QLIST_HEAD(freelist_head
, XenBlockRequest
) freelist
;
61 int requests_inflight
;
62 unsigned int max_requests
;
64 unsigned int sector_size
;
70 static int xen_block_send_response(XenBlockRequest
*request
);
72 static void reset_request(XenBlockRequest
*request
)
74 memset(&request
->req
, 0, sizeof(request
->req
));
80 request
->aio_inflight
= 0;
81 request
->aio_errors
= 0;
83 request
->dataplane
= NULL
;
84 memset(&request
->list
, 0, sizeof(request
->list
));
85 memset(&request
->acct
, 0, sizeof(request
->acct
));
87 qemu_iovec_reset(&request
->v
);
90 static XenBlockRequest
*xen_block_start_request(XenBlockDataPlane
*dataplane
)
92 XenBlockRequest
*request
= NULL
;
94 if (QLIST_EMPTY(&dataplane
->freelist
)) {
95 if (dataplane
->requests_total
>= dataplane
->max_requests
) {
98 /* allocate new struct */
99 request
= g_malloc0(sizeof(*request
));
100 request
->dataplane
= dataplane
;
102 * We cannot need more pages per requests than this, and since we
103 * re-use requests, allocate the memory once here. It will be freed
104 * xen_block_dataplane_destroy() when the request list is freed.
106 request
->buf
= qemu_memalign(XEN_PAGE_SIZE
,
107 BLKIF_MAX_SEGMENTS_PER_REQUEST
*
109 dataplane
->requests_total
++;
110 qemu_iovec_init(&request
->v
, 1);
112 /* get one from freelist */
113 request
= QLIST_FIRST(&dataplane
->freelist
);
114 QLIST_REMOVE(request
, list
);
116 QLIST_INSERT_HEAD(&dataplane
->inflight
, request
, list
);
117 dataplane
->requests_inflight
++;
123 static void xen_block_complete_request(XenBlockRequest
*request
)
125 XenBlockDataPlane
*dataplane
= request
->dataplane
;
127 if (xen_block_send_response(request
)) {
128 Error
*local_err
= NULL
;
130 xen_device_notify_event_channel(dataplane
->xendev
,
131 dataplane
->event_channel
,
134 error_report_err(local_err
);
138 QLIST_REMOVE(request
, list
);
139 dataplane
->requests_inflight
--;
140 reset_request(request
);
141 request
->dataplane
= dataplane
;
142 QLIST_INSERT_HEAD(&dataplane
->freelist
, request
, list
);
146 * translate request into iovec + start offset
147 * do sanity checks along the way
149 static int xen_block_parse_request(XenBlockRequest
*request
)
151 XenBlockDataPlane
*dataplane
= request
->dataplane
;
155 switch (request
->req
.operation
) {
158 case BLKIF_OP_FLUSH_DISKCACHE
:
159 request
->presync
= 1;
160 if (!request
->req
.nr_segments
) {
166 case BLKIF_OP_DISCARD
:
169 error_report("error: unknown operation (%d)", request
->req
.operation
);
173 if (request
->req
.operation
!= BLKIF_OP_READ
&&
174 !blk_is_writable(dataplane
->blk
)) {
175 error_report("error: write req for ro device");
179 request
->start
= request
->req
.sector_number
* dataplane
->sector_size
;
180 for (i
= 0; i
< request
->req
.nr_segments
; i
++) {
181 if (i
== BLKIF_MAX_SEGMENTS_PER_REQUEST
) {
182 error_report("error: nr_segments too big");
185 if (request
->req
.seg
[i
].first_sect
> request
->req
.seg
[i
].last_sect
) {
186 error_report("error: first > last sector");
189 if (request
->req
.seg
[i
].last_sect
* dataplane
->sector_size
>=
191 error_report("error: page crossing");
195 len
= (request
->req
.seg
[i
].last_sect
-
196 request
->req
.seg
[i
].first_sect
+ 1) * dataplane
->sector_size
;
197 request
->size
+= len
;
199 if (request
->start
+ request
->size
> blk_getlength(dataplane
->blk
)) {
200 error_report("error: access beyond end of file");
206 request
->status
= BLKIF_RSP_ERROR
;
210 static int xen_block_copy_request(XenBlockRequest
*request
)
212 XenBlockDataPlane
*dataplane
= request
->dataplane
;
213 XenDevice
*xendev
= dataplane
->xendev
;
214 XenDeviceGrantCopySegment segs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
216 bool to_domain
= (request
->req
.operation
== BLKIF_OP_READ
);
217 void *virt
= request
->buf
;
218 Error
*local_err
= NULL
;
220 if (request
->req
.nr_segments
== 0) {
224 count
= request
->req
.nr_segments
;
226 for (i
= 0; i
< count
; i
++) {
228 segs
[i
].dest
.foreign
.ref
= request
->req
.seg
[i
].gref
;
229 segs
[i
].dest
.foreign
.offset
= request
->req
.seg
[i
].first_sect
*
230 dataplane
->sector_size
;
231 segs
[i
].source
.virt
= virt
;
233 segs
[i
].source
.foreign
.ref
= request
->req
.seg
[i
].gref
;
234 segs
[i
].source
.foreign
.offset
= request
->req
.seg
[i
].first_sect
*
235 dataplane
->sector_size
;
236 segs
[i
].dest
.virt
= virt
;
238 segs
[i
].len
= (request
->req
.seg
[i
].last_sect
-
239 request
->req
.seg
[i
].first_sect
+ 1) *
240 dataplane
->sector_size
;
244 xen_device_copy_grant_refs(xendev
, to_domain
, segs
, count
, &local_err
);
247 error_reportf_err(local_err
, "failed to copy data: ");
249 request
->aio_errors
++;
256 static int xen_block_do_aio(XenBlockRequest
*request
);
258 static void xen_block_complete_aio(void *opaque
, int ret
)
260 XenBlockRequest
*request
= opaque
;
261 XenBlockDataPlane
*dataplane
= request
->dataplane
;
263 aio_context_acquire(dataplane
->ctx
);
266 error_report("%s I/O error",
267 request
->req
.operation
== BLKIF_OP_READ
?
269 request
->aio_errors
++;
272 request
->aio_inflight
--;
273 if (request
->presync
) {
274 request
->presync
= 0;
275 xen_block_do_aio(request
);
278 if (request
->aio_inflight
> 0) {
282 switch (request
->req
.operation
) {
284 /* in case of failure request->aio_errors is increased */
286 xen_block_copy_request(request
);
290 case BLKIF_OP_FLUSH_DISKCACHE
:
295 request
->status
= request
->aio_errors
? BLKIF_RSP_ERROR
: BLKIF_RSP_OKAY
;
297 switch (request
->req
.operation
) {
299 case BLKIF_OP_FLUSH_DISKCACHE
:
300 if (!request
->req
.nr_segments
) {
305 if (request
->status
== BLKIF_RSP_OKAY
) {
306 block_acct_done(blk_get_stats(dataplane
->blk
), &request
->acct
);
308 block_acct_failed(blk_get_stats(dataplane
->blk
), &request
->acct
);
311 case BLKIF_OP_DISCARD
:
316 xen_block_complete_request(request
);
318 if (dataplane
->more_work
) {
319 qemu_bh_schedule(dataplane
->bh
);
323 aio_context_release(dataplane
->ctx
);
326 static bool xen_block_split_discard(XenBlockRequest
*request
,
327 blkif_sector_t sector_number
,
330 XenBlockDataPlane
*dataplane
= request
->dataplane
;
333 uint64_t byte_remaining
;
334 uint64_t sec_start
= sector_number
;
335 uint64_t sec_count
= nr_sectors
;
337 /* Wrap around, or overflowing byte limit? */
338 if (sec_start
+ sec_count
< sec_count
||
339 sec_start
+ sec_count
> INT64_MAX
/ dataplane
->sector_size
) {
343 byte_offset
= sec_start
* dataplane
->sector_size
;
344 byte_remaining
= sec_count
* dataplane
->sector_size
;
347 byte_chunk
= byte_remaining
> BDRV_REQUEST_MAX_BYTES
?
348 BDRV_REQUEST_MAX_BYTES
: byte_remaining
;
349 request
->aio_inflight
++;
350 blk_aio_pdiscard(dataplane
->blk
, byte_offset
, byte_chunk
,
351 xen_block_complete_aio
, request
);
352 byte_remaining
-= byte_chunk
;
353 byte_offset
+= byte_chunk
;
354 } while (byte_remaining
> 0);
359 static int xen_block_do_aio(XenBlockRequest
*request
)
361 XenBlockDataPlane
*dataplane
= request
->dataplane
;
363 if (request
->req
.nr_segments
&&
364 (request
->req
.operation
== BLKIF_OP_WRITE
||
365 request
->req
.operation
== BLKIF_OP_FLUSH_DISKCACHE
) &&
366 xen_block_copy_request(request
)) {
370 request
->aio_inflight
++;
371 if (request
->presync
) {
372 blk_aio_flush(request
->dataplane
->blk
, xen_block_complete_aio
,
377 switch (request
->req
.operation
) {
379 qemu_iovec_add(&request
->v
, request
->buf
, request
->size
);
380 block_acct_start(blk_get_stats(dataplane
->blk
), &request
->acct
,
381 request
->v
.size
, BLOCK_ACCT_READ
);
382 request
->aio_inflight
++;
383 blk_aio_preadv(dataplane
->blk
, request
->start
, &request
->v
, 0,
384 xen_block_complete_aio
, request
);
387 case BLKIF_OP_FLUSH_DISKCACHE
:
388 if (!request
->req
.nr_segments
) {
392 qemu_iovec_add(&request
->v
, request
->buf
, request
->size
);
393 block_acct_start(blk_get_stats(dataplane
->blk
), &request
->acct
,
395 request
->req
.operation
== BLKIF_OP_WRITE
?
396 BLOCK_ACCT_WRITE
: BLOCK_ACCT_FLUSH
);
397 request
->aio_inflight
++;
398 blk_aio_pwritev(dataplane
->blk
, request
->start
, &request
->v
, 0,
399 xen_block_complete_aio
, request
);
401 case BLKIF_OP_DISCARD
:
403 struct blkif_request_discard
*req
= (void *)&request
->req
;
404 if (!xen_block_split_discard(request
, req
->sector_number
,
411 /* unknown operation (shouldn't happen -- parse catches this) */
415 xen_block_complete_aio(request
, 0);
420 request
->status
= BLKIF_RSP_ERROR
;
421 xen_block_complete_request(request
);
425 static int xen_block_send_response(XenBlockRequest
*request
)
427 XenBlockDataPlane
*dataplane
= request
->dataplane
;
429 int have_requests
= 0;
430 blkif_response_t
*resp
;
432 /* Place on the response ring for the relevant domain. */
433 switch (dataplane
->protocol
) {
434 case BLKIF_PROTOCOL_NATIVE
:
435 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
436 &dataplane
->rings
.native
,
437 dataplane
->rings
.native
.rsp_prod_pvt
);
439 case BLKIF_PROTOCOL_X86_32
:
440 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
441 &dataplane
->rings
.x86_32_part
,
442 dataplane
->rings
.x86_32_part
.rsp_prod_pvt
);
444 case BLKIF_PROTOCOL_X86_64
:
445 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
446 &dataplane
->rings
.x86_64_part
,
447 dataplane
->rings
.x86_64_part
.rsp_prod_pvt
);
453 resp
->id
= request
->req
.id
;
454 resp
->operation
= request
->req
.operation
;
455 resp
->status
= request
->status
;
457 dataplane
->rings
.common
.rsp_prod_pvt
++;
459 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&dataplane
->rings
.common
,
461 if (dataplane
->rings
.common
.rsp_prod_pvt
==
462 dataplane
->rings
.common
.req_cons
) {
464 * Tail check for pending requests. Allows frontend to avoid
465 * notifications if requests are already in flight (lower
466 * overheads and promotes batching).
468 RING_FINAL_CHECK_FOR_REQUESTS(&dataplane
->rings
.common
,
470 } else if (RING_HAS_UNCONSUMED_REQUESTS(&dataplane
->rings
.common
)) {
475 dataplane
->more_work
++;
480 static int xen_block_get_request(XenBlockDataPlane
*dataplane
,
481 XenBlockRequest
*request
, RING_IDX rc
)
483 switch (dataplane
->protocol
) {
484 case BLKIF_PROTOCOL_NATIVE
: {
485 blkif_request_t
*req
=
486 RING_GET_REQUEST(&dataplane
->rings
.native
, rc
);
488 memcpy(&request
->req
, req
, sizeof(request
->req
));
491 case BLKIF_PROTOCOL_X86_32
: {
492 blkif_x86_32_request_t
*req
=
493 RING_GET_REQUEST(&dataplane
->rings
.x86_32_part
, rc
);
495 blkif_get_x86_32_req(&request
->req
, req
);
498 case BLKIF_PROTOCOL_X86_64
: {
499 blkif_x86_64_request_t
*req
=
500 RING_GET_REQUEST(&dataplane
->rings
.x86_64_part
, rc
);
502 blkif_get_x86_64_req(&request
->req
, req
);
506 /* Prevent the compiler from accessing the on-ring fields instead. */
512 * Threshold of in-flight requests above which we will start using
513 * defer_call_begin()/defer_call_end() to batch requests.
515 #define IO_PLUG_THRESHOLD 1
517 static bool xen_block_handle_requests(XenBlockDataPlane
*dataplane
)
520 XenBlockRequest
*request
;
521 int inflight_atstart
= dataplane
->requests_inflight
;
523 bool done_something
= false;
525 dataplane
->more_work
= 0;
527 rc
= dataplane
->rings
.common
.req_cons
;
528 rp
= dataplane
->rings
.common
.sring
->req_prod
;
529 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
532 * If there was more than IO_PLUG_THRESHOLD requests in flight
533 * when we got here, this is an indication that there the bottleneck
534 * is below us, so it's worth beginning to batch up I/O requests
535 * rather than submitting them immediately. The maximum number
536 * of requests we're willing to batch is the number already in
537 * flight, so it can grow up to max_requests when the bottleneck
540 if (inflight_atstart
> IO_PLUG_THRESHOLD
) {
544 /* pull request from ring */
545 if (RING_REQUEST_CONS_OVERFLOW(&dataplane
->rings
.common
, rc
)) {
548 request
= xen_block_start_request(dataplane
);
549 if (request
== NULL
) {
550 dataplane
->more_work
++;
553 xen_block_get_request(dataplane
, request
, rc
);
554 dataplane
->rings
.common
.req_cons
= ++rc
;
555 done_something
= true;
558 if (xen_block_parse_request(request
) != 0) {
559 switch (request
->req
.operation
) {
561 block_acct_invalid(blk_get_stats(dataplane
->blk
),
565 block_acct_invalid(blk_get_stats(dataplane
->blk
),
568 case BLKIF_OP_FLUSH_DISKCACHE
:
569 block_acct_invalid(blk_get_stats(dataplane
->blk
),
575 xen_block_complete_request(request
);
579 if (inflight_atstart
> IO_PLUG_THRESHOLD
&&
580 batched
>= inflight_atstart
) {
583 xen_block_do_aio(request
);
584 if (inflight_atstart
> IO_PLUG_THRESHOLD
) {
585 if (batched
>= inflight_atstart
) {
593 if (inflight_atstart
> IO_PLUG_THRESHOLD
) {
597 return done_something
;
600 static void xen_block_dataplane_bh(void *opaque
)
602 XenBlockDataPlane
*dataplane
= opaque
;
604 aio_context_acquire(dataplane
->ctx
);
605 xen_block_handle_requests(dataplane
);
606 aio_context_release(dataplane
->ctx
);
609 static bool xen_block_dataplane_event(void *opaque
)
611 XenBlockDataPlane
*dataplane
= opaque
;
613 return xen_block_handle_requests(dataplane
);
616 XenBlockDataPlane
*xen_block_dataplane_create(XenDevice
*xendev
,
618 unsigned int sector_size
,
621 XenBlockDataPlane
*dataplane
= g_new0(XenBlockDataPlane
, 1);
623 dataplane
->xendev
= xendev
;
624 dataplane
->blk
= blk
;
625 dataplane
->sector_size
= sector_size
;
627 QLIST_INIT(&dataplane
->inflight
);
628 QLIST_INIT(&dataplane
->freelist
);
631 dataplane
->iothread
= iothread
;
632 object_ref(OBJECT(dataplane
->iothread
));
633 dataplane
->ctx
= iothread_get_aio_context(dataplane
->iothread
);
635 dataplane
->ctx
= qemu_get_aio_context();
637 dataplane
->bh
= aio_bh_new_guarded(dataplane
->ctx
, xen_block_dataplane_bh
,
639 &DEVICE(xendev
)->mem_reentrancy_guard
);
644 void xen_block_dataplane_destroy(XenBlockDataPlane
*dataplane
)
646 XenBlockRequest
*request
;
652 while (!QLIST_EMPTY(&dataplane
->freelist
)) {
653 request
= QLIST_FIRST(&dataplane
->freelist
);
654 QLIST_REMOVE(request
, list
);
655 qemu_iovec_destroy(&request
->v
);
656 qemu_vfree(request
->buf
);
660 qemu_bh_delete(dataplane
->bh
);
661 if (dataplane
->iothread
) {
662 object_unref(OBJECT(dataplane
->iothread
));
668 void xen_block_dataplane_detach(XenBlockDataPlane
*dataplane
)
670 if (!dataplane
|| !dataplane
->event_channel
) {
674 /* Only reason for failure is a NULL channel */
675 xen_device_set_event_channel_context(dataplane
->xendev
,
676 dataplane
->event_channel
,
680 void xen_block_dataplane_attach(XenBlockDataPlane
*dataplane
)
682 if (!dataplane
|| !dataplane
->event_channel
) {
686 /* Only reason for failure is a NULL channel */
687 xen_device_set_event_channel_context(dataplane
->xendev
,
688 dataplane
->event_channel
,
689 dataplane
->ctx
, &error_abort
);
692 void xen_block_dataplane_stop(XenBlockDataPlane
*dataplane
)
700 xendev
= dataplane
->xendev
;
702 if (!blk_in_drain(dataplane
->blk
)) {
703 xen_block_dataplane_detach(dataplane
);
706 aio_context_acquire(dataplane
->ctx
);
707 /* Xen doesn't have multiple users for nodes, so this can't fail */
708 blk_set_aio_context(dataplane
->blk
, qemu_get_aio_context(), &error_abort
);
709 aio_context_release(dataplane
->ctx
);
712 * Now that the context has been moved onto the main thread, cancel
713 * further processing.
715 qemu_bh_cancel(dataplane
->bh
);
717 if (dataplane
->event_channel
) {
718 Error
*local_err
= NULL
;
720 xen_device_unbind_event_channel(xendev
, dataplane
->event_channel
,
722 dataplane
->event_channel
= NULL
;
725 error_report_err(local_err
);
729 if (dataplane
->sring
) {
730 Error
*local_err
= NULL
;
732 xen_device_unmap_grant_refs(xendev
, dataplane
->sring
,
734 dataplane
->nr_ring_ref
, &local_err
);
735 dataplane
->sring
= NULL
;
738 error_report_err(local_err
);
742 g_free(dataplane
->ring_ref
);
743 dataplane
->ring_ref
= NULL
;
746 void xen_block_dataplane_start(XenBlockDataPlane
*dataplane
,
747 const unsigned int ring_ref
[],
748 unsigned int nr_ring_ref
,
749 unsigned int event_channel
,
750 unsigned int protocol
,
754 XenDevice
*xendev
= dataplane
->xendev
;
755 AioContext
*old_context
;
756 unsigned int ring_size
;
759 dataplane
->nr_ring_ref
= nr_ring_ref
;
760 dataplane
->ring_ref
= g_new(unsigned int, nr_ring_ref
);
762 for (i
= 0; i
< nr_ring_ref
; i
++) {
763 dataplane
->ring_ref
[i
] = ring_ref
[i
];
766 dataplane
->protocol
= protocol
;
768 ring_size
= XEN_PAGE_SIZE
* dataplane
->nr_ring_ref
;
769 switch (dataplane
->protocol
) {
770 case BLKIF_PROTOCOL_NATIVE
:
772 dataplane
->max_requests
= __CONST_RING_SIZE(blkif
, ring_size
);
775 case BLKIF_PROTOCOL_X86_32
:
777 dataplane
->max_requests
= __CONST_RING_SIZE(blkif_x86_32
, ring_size
);
780 case BLKIF_PROTOCOL_X86_64
:
782 dataplane
->max_requests
= __CONST_RING_SIZE(blkif_x86_64
, ring_size
);
786 error_setg(errp
, "unknown protocol %u", dataplane
->protocol
);
790 xen_device_set_max_grant_refs(xendev
, dataplane
->nr_ring_ref
,
796 dataplane
->sring
= xen_device_map_grant_refs(xendev
,
798 dataplane
->nr_ring_ref
,
799 PROT_READ
| PROT_WRITE
,
805 switch (dataplane
->protocol
) {
806 case BLKIF_PROTOCOL_NATIVE
:
808 blkif_sring_t
*sring_native
= dataplane
->sring
;
810 BACK_RING_INIT(&dataplane
->rings
.native
, sring_native
, ring_size
);
813 case BLKIF_PROTOCOL_X86_32
:
815 blkif_x86_32_sring_t
*sring_x86_32
= dataplane
->sring
;
817 BACK_RING_INIT(&dataplane
->rings
.x86_32_part
, sring_x86_32
,
821 case BLKIF_PROTOCOL_X86_64
:
823 blkif_x86_64_sring_t
*sring_x86_64
= dataplane
->sring
;
825 BACK_RING_INIT(&dataplane
->rings
.x86_64_part
, sring_x86_64
,
831 dataplane
->event_channel
=
832 xen_device_bind_event_channel(xendev
, event_channel
,
833 xen_block_dataplane_event
, dataplane
,
839 old_context
= blk_get_aio_context(dataplane
->blk
);
840 aio_context_acquire(old_context
);
841 /* If other users keep the BlockBackend in the iothread, that's ok */
842 blk_set_aio_context(dataplane
->blk
, dataplane
->ctx
, NULL
);
843 aio_context_release(old_context
);
845 if (!blk_in_drain(dataplane
->blk
)) {
846 xen_block_dataplane_attach(dataplane
);
852 xen_block_dataplane_stop(dataplane
);