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/error-report.h"
23 #include "qemu/main-loop.h"
24 #include "qemu/memalign.h"
25 #include "qapi/error.h"
26 #include "hw/xen/xen.h"
27 #include "hw/block/xen_blkif.h"
28 #include "hw/xen/interface/io/ring.h"
29 #include "sysemu/block-backend.h"
30 #include "sysemu/iothread.h"
31 #include "xen-block.h"
33 typedef struct XenBlockRequest
{
43 XenBlockDataPlane
*dataplane
;
44 QLIST_ENTRY(XenBlockRequest
) list
;
48 struct XenBlockDataPlane
{
50 XenEventChannel
*event_channel
;
51 unsigned int *ring_ref
;
52 unsigned int nr_ring_ref
;
55 blkif_back_rings_t rings
;
57 QLIST_HEAD(inflight_head
, XenBlockRequest
) inflight
;
58 QLIST_HEAD(freelist_head
, XenBlockRequest
) freelist
;
60 int requests_inflight
;
61 unsigned int max_requests
;
63 unsigned int sector_size
;
69 static int xen_block_send_response(XenBlockRequest
*request
);
71 static void reset_request(XenBlockRequest
*request
)
73 memset(&request
->req
, 0, sizeof(request
->req
));
79 request
->aio_inflight
= 0;
80 request
->aio_errors
= 0;
82 request
->dataplane
= NULL
;
83 memset(&request
->list
, 0, sizeof(request
->list
));
84 memset(&request
->acct
, 0, sizeof(request
->acct
));
86 qemu_iovec_reset(&request
->v
);
89 static XenBlockRequest
*xen_block_start_request(XenBlockDataPlane
*dataplane
)
91 XenBlockRequest
*request
= NULL
;
93 if (QLIST_EMPTY(&dataplane
->freelist
)) {
94 if (dataplane
->requests_total
>= dataplane
->max_requests
) {
97 /* allocate new struct */
98 request
= g_malloc0(sizeof(*request
));
99 request
->dataplane
= dataplane
;
101 * We cannot need more pages per requests than this, and since we
102 * re-use requests, allocate the memory once here. It will be freed
103 * xen_block_dataplane_destroy() when the request list is freed.
105 request
->buf
= qemu_memalign(XEN_PAGE_SIZE
,
106 BLKIF_MAX_SEGMENTS_PER_REQUEST
*
108 dataplane
->requests_total
++;
109 qemu_iovec_init(&request
->v
, 1);
111 /* get one from freelist */
112 request
= QLIST_FIRST(&dataplane
->freelist
);
113 QLIST_REMOVE(request
, list
);
115 QLIST_INSERT_HEAD(&dataplane
->inflight
, request
, list
);
116 dataplane
->requests_inflight
++;
122 static void xen_block_complete_request(XenBlockRequest
*request
)
124 XenBlockDataPlane
*dataplane
= request
->dataplane
;
126 if (xen_block_send_response(request
)) {
127 Error
*local_err
= NULL
;
129 xen_device_notify_event_channel(dataplane
->xendev
,
130 dataplane
->event_channel
,
133 error_report_err(local_err
);
137 QLIST_REMOVE(request
, list
);
138 dataplane
->requests_inflight
--;
139 reset_request(request
);
140 request
->dataplane
= dataplane
;
141 QLIST_INSERT_HEAD(&dataplane
->freelist
, request
, list
);
145 * translate request into iovec + start offset
146 * do sanity checks along the way
148 static int xen_block_parse_request(XenBlockRequest
*request
)
150 XenBlockDataPlane
*dataplane
= request
->dataplane
;
154 switch (request
->req
.operation
) {
157 case BLKIF_OP_FLUSH_DISKCACHE
:
158 request
->presync
= 1;
159 if (!request
->req
.nr_segments
) {
165 case BLKIF_OP_DISCARD
:
168 error_report("error: unknown operation (%d)", request
->req
.operation
);
172 if (request
->req
.operation
!= BLKIF_OP_READ
&&
173 !blk_is_writable(dataplane
->blk
)) {
174 error_report("error: write req for ro device");
178 request
->start
= request
->req
.sector_number
* dataplane
->sector_size
;
179 for (i
= 0; i
< request
->req
.nr_segments
; i
++) {
180 if (i
== BLKIF_MAX_SEGMENTS_PER_REQUEST
) {
181 error_report("error: nr_segments too big");
184 if (request
->req
.seg
[i
].first_sect
> request
->req
.seg
[i
].last_sect
) {
185 error_report("error: first > last sector");
188 if (request
->req
.seg
[i
].last_sect
* dataplane
->sector_size
>=
190 error_report("error: page crossing");
194 len
= (request
->req
.seg
[i
].last_sect
-
195 request
->req
.seg
[i
].first_sect
+ 1) * dataplane
->sector_size
;
196 request
->size
+= len
;
198 if (request
->start
+ request
->size
> blk_getlength(dataplane
->blk
)) {
199 error_report("error: access beyond end of file");
205 request
->status
= BLKIF_RSP_ERROR
;
209 static int xen_block_copy_request(XenBlockRequest
*request
)
211 XenBlockDataPlane
*dataplane
= request
->dataplane
;
212 XenDevice
*xendev
= dataplane
->xendev
;
213 XenDeviceGrantCopySegment segs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
215 bool to_domain
= (request
->req
.operation
== BLKIF_OP_READ
);
216 void *virt
= request
->buf
;
217 Error
*local_err
= NULL
;
219 if (request
->req
.nr_segments
== 0) {
223 count
= request
->req
.nr_segments
;
225 for (i
= 0; i
< count
; i
++) {
227 segs
[i
].dest
.foreign
.ref
= request
->req
.seg
[i
].gref
;
228 segs
[i
].dest
.foreign
.offset
= request
->req
.seg
[i
].first_sect
*
229 dataplane
->sector_size
;
230 segs
[i
].source
.virt
= virt
;
232 segs
[i
].source
.foreign
.ref
= request
->req
.seg
[i
].gref
;
233 segs
[i
].source
.foreign
.offset
= request
->req
.seg
[i
].first_sect
*
234 dataplane
->sector_size
;
235 segs
[i
].dest
.virt
= virt
;
237 segs
[i
].len
= (request
->req
.seg
[i
].last_sect
-
238 request
->req
.seg
[i
].first_sect
+ 1) *
239 dataplane
->sector_size
;
243 xen_device_copy_grant_refs(xendev
, to_domain
, segs
, count
, &local_err
);
246 error_reportf_err(local_err
, "failed to copy data: ");
248 request
->aio_errors
++;
255 static int xen_block_do_aio(XenBlockRequest
*request
);
257 static void xen_block_complete_aio(void *opaque
, int ret
)
259 XenBlockRequest
*request
= opaque
;
260 XenBlockDataPlane
*dataplane
= request
->dataplane
;
262 aio_context_acquire(dataplane
->ctx
);
265 error_report("%s I/O error",
266 request
->req
.operation
== BLKIF_OP_READ
?
268 request
->aio_errors
++;
271 request
->aio_inflight
--;
272 if (request
->presync
) {
273 request
->presync
= 0;
274 xen_block_do_aio(request
);
277 if (request
->aio_inflight
> 0) {
281 switch (request
->req
.operation
) {
283 /* in case of failure request->aio_errors is increased */
285 xen_block_copy_request(request
);
289 case BLKIF_OP_FLUSH_DISKCACHE
:
294 request
->status
= request
->aio_errors
? BLKIF_RSP_ERROR
: BLKIF_RSP_OKAY
;
296 switch (request
->req
.operation
) {
298 case BLKIF_OP_FLUSH_DISKCACHE
:
299 if (!request
->req
.nr_segments
) {
304 if (request
->status
== BLKIF_RSP_OKAY
) {
305 block_acct_done(blk_get_stats(dataplane
->blk
), &request
->acct
);
307 block_acct_failed(blk_get_stats(dataplane
->blk
), &request
->acct
);
310 case BLKIF_OP_DISCARD
:
315 xen_block_complete_request(request
);
317 if (dataplane
->more_work
) {
318 qemu_bh_schedule(dataplane
->bh
);
322 aio_context_release(dataplane
->ctx
);
325 static bool xen_block_split_discard(XenBlockRequest
*request
,
326 blkif_sector_t sector_number
,
329 XenBlockDataPlane
*dataplane
= request
->dataplane
;
332 uint64_t byte_remaining
;
333 uint64_t sec_start
= sector_number
;
334 uint64_t sec_count
= nr_sectors
;
336 /* Wrap around, or overflowing byte limit? */
337 if (sec_start
+ sec_count
< sec_count
||
338 sec_start
+ sec_count
> INT64_MAX
/ dataplane
->sector_size
) {
342 byte_offset
= sec_start
* dataplane
->sector_size
;
343 byte_remaining
= sec_count
* dataplane
->sector_size
;
346 byte_chunk
= byte_remaining
> BDRV_REQUEST_MAX_BYTES
?
347 BDRV_REQUEST_MAX_BYTES
: byte_remaining
;
348 request
->aio_inflight
++;
349 blk_aio_pdiscard(dataplane
->blk
, byte_offset
, byte_chunk
,
350 xen_block_complete_aio
, request
);
351 byte_remaining
-= byte_chunk
;
352 byte_offset
+= byte_chunk
;
353 } while (byte_remaining
> 0);
358 static int xen_block_do_aio(XenBlockRequest
*request
)
360 XenBlockDataPlane
*dataplane
= request
->dataplane
;
362 if (request
->req
.nr_segments
&&
363 (request
->req
.operation
== BLKIF_OP_WRITE
||
364 request
->req
.operation
== BLKIF_OP_FLUSH_DISKCACHE
) &&
365 xen_block_copy_request(request
)) {
369 request
->aio_inflight
++;
370 if (request
->presync
) {
371 blk_aio_flush(request
->dataplane
->blk
, xen_block_complete_aio
,
376 switch (request
->req
.operation
) {
378 qemu_iovec_add(&request
->v
, request
->buf
, request
->size
);
379 block_acct_start(blk_get_stats(dataplane
->blk
), &request
->acct
,
380 request
->v
.size
, BLOCK_ACCT_READ
);
381 request
->aio_inflight
++;
382 blk_aio_preadv(dataplane
->blk
, request
->start
, &request
->v
, 0,
383 xen_block_complete_aio
, request
);
386 case BLKIF_OP_FLUSH_DISKCACHE
:
387 if (!request
->req
.nr_segments
) {
391 qemu_iovec_add(&request
->v
, request
->buf
, request
->size
);
392 block_acct_start(blk_get_stats(dataplane
->blk
), &request
->acct
,
394 request
->req
.operation
== BLKIF_OP_WRITE
?
395 BLOCK_ACCT_WRITE
: BLOCK_ACCT_FLUSH
);
396 request
->aio_inflight
++;
397 blk_aio_pwritev(dataplane
->blk
, request
->start
, &request
->v
, 0,
398 xen_block_complete_aio
, request
);
400 case BLKIF_OP_DISCARD
:
402 struct blkif_request_discard
*req
= (void *)&request
->req
;
403 if (!xen_block_split_discard(request
, req
->sector_number
,
410 /* unknown operation (shouldn't happen -- parse catches this) */
414 xen_block_complete_aio(request
, 0);
419 request
->status
= BLKIF_RSP_ERROR
;
420 xen_block_complete_request(request
);
424 static int xen_block_send_response(XenBlockRequest
*request
)
426 XenBlockDataPlane
*dataplane
= request
->dataplane
;
428 int have_requests
= 0;
429 blkif_response_t
*resp
;
431 /* Place on the response ring for the relevant domain. */
432 switch (dataplane
->protocol
) {
433 case BLKIF_PROTOCOL_NATIVE
:
434 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
435 &dataplane
->rings
.native
,
436 dataplane
->rings
.native
.rsp_prod_pvt
);
438 case BLKIF_PROTOCOL_X86_32
:
439 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
440 &dataplane
->rings
.x86_32_part
,
441 dataplane
->rings
.x86_32_part
.rsp_prod_pvt
);
443 case BLKIF_PROTOCOL_X86_64
:
444 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
445 &dataplane
->rings
.x86_64_part
,
446 dataplane
->rings
.x86_64_part
.rsp_prod_pvt
);
452 resp
->id
= request
->req
.id
;
453 resp
->operation
= request
->req
.operation
;
454 resp
->status
= request
->status
;
456 dataplane
->rings
.common
.rsp_prod_pvt
++;
458 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&dataplane
->rings
.common
,
460 if (dataplane
->rings
.common
.rsp_prod_pvt
==
461 dataplane
->rings
.common
.req_cons
) {
463 * Tail check for pending requests. Allows frontend to avoid
464 * notifications if requests are already in flight (lower
465 * overheads and promotes batching).
467 RING_FINAL_CHECK_FOR_REQUESTS(&dataplane
->rings
.common
,
469 } else if (RING_HAS_UNCONSUMED_REQUESTS(&dataplane
->rings
.common
)) {
474 dataplane
->more_work
++;
479 static int xen_block_get_request(XenBlockDataPlane
*dataplane
,
480 XenBlockRequest
*request
, RING_IDX rc
)
482 switch (dataplane
->protocol
) {
483 case BLKIF_PROTOCOL_NATIVE
: {
484 blkif_request_t
*req
=
485 RING_GET_REQUEST(&dataplane
->rings
.native
, rc
);
487 memcpy(&request
->req
, req
, sizeof(request
->req
));
490 case BLKIF_PROTOCOL_X86_32
: {
491 blkif_x86_32_request_t
*req
=
492 RING_GET_REQUEST(&dataplane
->rings
.x86_32_part
, rc
);
494 blkif_get_x86_32_req(&request
->req
, req
);
497 case BLKIF_PROTOCOL_X86_64
: {
498 blkif_x86_64_request_t
*req
=
499 RING_GET_REQUEST(&dataplane
->rings
.x86_64_part
, rc
);
501 blkif_get_x86_64_req(&request
->req
, req
);
505 /* Prevent the compiler from accessing the on-ring fields instead. */
511 * Threshold of in-flight requests above which we will start using
512 * defer_call_begin()/defer_call_end() to batch requests.
514 #define IO_PLUG_THRESHOLD 1
516 static bool xen_block_handle_requests(XenBlockDataPlane
*dataplane
)
519 XenBlockRequest
*request
;
520 int inflight_atstart
= dataplane
->requests_inflight
;
522 bool done_something
= false;
524 dataplane
->more_work
= 0;
526 rc
= dataplane
->rings
.common
.req_cons
;
527 rp
= dataplane
->rings
.common
.sring
->req_prod
;
528 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
531 * If there was more than IO_PLUG_THRESHOLD requests in flight
532 * when we got here, this is an indication that there the bottleneck
533 * is below us, so it's worth beginning to batch up I/O requests
534 * rather than submitting them immediately. The maximum number
535 * of requests we're willing to batch is the number already in
536 * flight, so it can grow up to max_requests when the bottleneck
539 if (inflight_atstart
> IO_PLUG_THRESHOLD
) {
543 /* pull request from ring */
544 if (RING_REQUEST_CONS_OVERFLOW(&dataplane
->rings
.common
, rc
)) {
547 request
= xen_block_start_request(dataplane
);
548 if (request
== NULL
) {
549 dataplane
->more_work
++;
552 xen_block_get_request(dataplane
, request
, rc
);
553 dataplane
->rings
.common
.req_cons
= ++rc
;
554 done_something
= true;
557 if (xen_block_parse_request(request
) != 0) {
558 switch (request
->req
.operation
) {
560 block_acct_invalid(blk_get_stats(dataplane
->blk
),
564 block_acct_invalid(blk_get_stats(dataplane
->blk
),
567 case BLKIF_OP_FLUSH_DISKCACHE
:
568 block_acct_invalid(blk_get_stats(dataplane
->blk
),
574 xen_block_complete_request(request
);
578 if (inflight_atstart
> IO_PLUG_THRESHOLD
&&
579 batched
>= inflight_atstart
) {
582 xen_block_do_aio(request
);
583 if (inflight_atstart
> IO_PLUG_THRESHOLD
) {
584 if (batched
>= inflight_atstart
) {
592 if (inflight_atstart
> IO_PLUG_THRESHOLD
) {
596 return done_something
;
599 static void xen_block_dataplane_bh(void *opaque
)
601 XenBlockDataPlane
*dataplane
= opaque
;
603 aio_context_acquire(dataplane
->ctx
);
604 xen_block_handle_requests(dataplane
);
605 aio_context_release(dataplane
->ctx
);
608 static bool xen_block_dataplane_event(void *opaque
)
610 XenBlockDataPlane
*dataplane
= opaque
;
612 return xen_block_handle_requests(dataplane
);
615 XenBlockDataPlane
*xen_block_dataplane_create(XenDevice
*xendev
,
617 unsigned int sector_size
,
620 XenBlockDataPlane
*dataplane
= g_new0(XenBlockDataPlane
, 1);
622 dataplane
->xendev
= xendev
;
623 dataplane
->blk
= blk
;
624 dataplane
->sector_size
= sector_size
;
626 QLIST_INIT(&dataplane
->inflight
);
627 QLIST_INIT(&dataplane
->freelist
);
630 dataplane
->iothread
= iothread
;
631 object_ref(OBJECT(dataplane
->iothread
));
632 dataplane
->ctx
= iothread_get_aio_context(dataplane
->iothread
);
634 dataplane
->ctx
= qemu_get_aio_context();
636 dataplane
->bh
= aio_bh_new_guarded(dataplane
->ctx
, xen_block_dataplane_bh
,
638 &DEVICE(xendev
)->mem_reentrancy_guard
);
643 void xen_block_dataplane_destroy(XenBlockDataPlane
*dataplane
)
645 XenBlockRequest
*request
;
651 while (!QLIST_EMPTY(&dataplane
->freelist
)) {
652 request
= QLIST_FIRST(&dataplane
->freelist
);
653 QLIST_REMOVE(request
, list
);
654 qemu_iovec_destroy(&request
->v
);
655 qemu_vfree(request
->buf
);
659 qemu_bh_delete(dataplane
->bh
);
660 if (dataplane
->iothread
) {
661 object_unref(OBJECT(dataplane
->iothread
));
667 void xen_block_dataplane_detach(XenBlockDataPlane
*dataplane
)
669 if (!dataplane
|| !dataplane
->event_channel
) {
673 /* Only reason for failure is a NULL channel */
674 xen_device_set_event_channel_context(dataplane
->xendev
,
675 dataplane
->event_channel
,
679 void xen_block_dataplane_attach(XenBlockDataPlane
*dataplane
)
681 if (!dataplane
|| !dataplane
->event_channel
) {
685 /* Only reason for failure is a NULL channel */
686 xen_device_set_event_channel_context(dataplane
->xendev
,
687 dataplane
->event_channel
,
688 dataplane
->ctx
, &error_abort
);
691 void xen_block_dataplane_stop(XenBlockDataPlane
*dataplane
)
699 xendev
= dataplane
->xendev
;
701 if (!blk_in_drain(dataplane
->blk
)) {
702 xen_block_dataplane_detach(dataplane
);
705 aio_context_acquire(dataplane
->ctx
);
706 /* Xen doesn't have multiple users for nodes, so this can't fail */
707 blk_set_aio_context(dataplane
->blk
, qemu_get_aio_context(), &error_abort
);
708 aio_context_release(dataplane
->ctx
);
711 * Now that the context has been moved onto the main thread, cancel
712 * further processing.
714 qemu_bh_cancel(dataplane
->bh
);
716 if (dataplane
->event_channel
) {
717 Error
*local_err
= NULL
;
719 xen_device_unbind_event_channel(xendev
, dataplane
->event_channel
,
721 dataplane
->event_channel
= NULL
;
724 error_report_err(local_err
);
728 if (dataplane
->sring
) {
729 Error
*local_err
= NULL
;
731 xen_device_unmap_grant_refs(xendev
, dataplane
->sring
,
733 dataplane
->nr_ring_ref
, &local_err
);
734 dataplane
->sring
= NULL
;
737 error_report_err(local_err
);
741 g_free(dataplane
->ring_ref
);
742 dataplane
->ring_ref
= NULL
;
745 void xen_block_dataplane_start(XenBlockDataPlane
*dataplane
,
746 const unsigned int ring_ref
[],
747 unsigned int nr_ring_ref
,
748 unsigned int event_channel
,
749 unsigned int protocol
,
753 XenDevice
*xendev
= dataplane
->xendev
;
754 AioContext
*old_context
;
755 unsigned int ring_size
;
758 dataplane
->nr_ring_ref
= nr_ring_ref
;
759 dataplane
->ring_ref
= g_new(unsigned int, nr_ring_ref
);
761 for (i
= 0; i
< nr_ring_ref
; i
++) {
762 dataplane
->ring_ref
[i
] = ring_ref
[i
];
765 dataplane
->protocol
= protocol
;
767 ring_size
= XEN_PAGE_SIZE
* dataplane
->nr_ring_ref
;
768 switch (dataplane
->protocol
) {
769 case BLKIF_PROTOCOL_NATIVE
:
771 dataplane
->max_requests
= __CONST_RING_SIZE(blkif
, ring_size
);
774 case BLKIF_PROTOCOL_X86_32
:
776 dataplane
->max_requests
= __CONST_RING_SIZE(blkif_x86_32
, ring_size
);
779 case BLKIF_PROTOCOL_X86_64
:
781 dataplane
->max_requests
= __CONST_RING_SIZE(blkif_x86_64
, ring_size
);
785 error_setg(errp
, "unknown protocol %u", dataplane
->protocol
);
789 xen_device_set_max_grant_refs(xendev
, dataplane
->nr_ring_ref
,
795 dataplane
->sring
= xen_device_map_grant_refs(xendev
,
797 dataplane
->nr_ring_ref
,
798 PROT_READ
| PROT_WRITE
,
804 switch (dataplane
->protocol
) {
805 case BLKIF_PROTOCOL_NATIVE
:
807 blkif_sring_t
*sring_native
= dataplane
->sring
;
809 BACK_RING_INIT(&dataplane
->rings
.native
, sring_native
, ring_size
);
812 case BLKIF_PROTOCOL_X86_32
:
814 blkif_x86_32_sring_t
*sring_x86_32
= dataplane
->sring
;
816 BACK_RING_INIT(&dataplane
->rings
.x86_32_part
, sring_x86_32
,
820 case BLKIF_PROTOCOL_X86_64
:
822 blkif_x86_64_sring_t
*sring_x86_64
= dataplane
->sring
;
824 BACK_RING_INIT(&dataplane
->rings
.x86_64_part
, sring_x86_64
,
830 dataplane
->event_channel
=
831 xen_device_bind_event_channel(xendev
, event_channel
,
832 xen_block_dataplane_event
, dataplane
,
838 old_context
= blk_get_aio_context(dataplane
->blk
);
839 aio_context_acquire(old_context
);
840 /* If other users keep the BlockBackend in the iothread, that's ok */
841 blk_set_aio_context(dataplane
->blk
, dataplane
->ctx
, NULL
);
842 aio_context_release(old_context
);
844 if (!blk_in_drain(dataplane
->blk
)) {
845 xen_block_dataplane_attach(dataplane
);
851 xen_block_dataplane_stop(dataplane
);