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_common.h"
27 #include "hw/block/xen_blkif.h"
28 #include "sysemu/block-backend.h"
29 #include "sysemu/iothread.h"
30 #include "xen-block.h"
32 typedef struct XenBlockRequest
{
42 XenBlockDataPlane
*dataplane
;
43 QLIST_ENTRY(XenBlockRequest
) list
;
47 struct XenBlockDataPlane
{
49 XenEventChannel
*event_channel
;
50 unsigned int *ring_ref
;
51 unsigned int nr_ring_ref
;
54 blkif_back_rings_t rings
;
56 QLIST_HEAD(inflight_head
, XenBlockRequest
) inflight
;
57 QLIST_HEAD(freelist_head
, XenBlockRequest
) freelist
;
59 int requests_inflight
;
60 unsigned int max_requests
;
62 unsigned int sector_size
;
68 static int xen_block_send_response(XenBlockRequest
*request
);
70 static void reset_request(XenBlockRequest
*request
)
72 memset(&request
->req
, 0, sizeof(request
->req
));
78 request
->aio_inflight
= 0;
79 request
->aio_errors
= 0;
81 request
->dataplane
= NULL
;
82 memset(&request
->list
, 0, sizeof(request
->list
));
83 memset(&request
->acct
, 0, sizeof(request
->acct
));
85 qemu_iovec_reset(&request
->v
);
88 static XenBlockRequest
*xen_block_start_request(XenBlockDataPlane
*dataplane
)
90 XenBlockRequest
*request
= NULL
;
92 if (QLIST_EMPTY(&dataplane
->freelist
)) {
93 if (dataplane
->requests_total
>= dataplane
->max_requests
) {
96 /* allocate new struct */
97 request
= g_malloc0(sizeof(*request
));
98 request
->dataplane
= dataplane
;
100 * We cannot need more pages per requests than this, and since we
101 * re-use requests, allocate the memory once here. It will be freed
102 * xen_block_dataplane_destroy() when the request list is freed.
104 request
->buf
= qemu_memalign(XC_PAGE_SIZE
,
105 BLKIF_MAX_SEGMENTS_PER_REQUEST
*
107 dataplane
->requests_total
++;
108 qemu_iovec_init(&request
->v
, 1);
110 /* get one from freelist */
111 request
= QLIST_FIRST(&dataplane
->freelist
);
112 QLIST_REMOVE(request
, list
);
114 QLIST_INSERT_HEAD(&dataplane
->inflight
, request
, list
);
115 dataplane
->requests_inflight
++;
121 static void xen_block_complete_request(XenBlockRequest
*request
)
123 XenBlockDataPlane
*dataplane
= request
->dataplane
;
125 if (xen_block_send_response(request
)) {
126 Error
*local_err
= NULL
;
128 xen_device_notify_event_channel(dataplane
->xendev
,
129 dataplane
->event_channel
,
132 error_report_err(local_err
);
136 QLIST_REMOVE(request
, list
);
137 dataplane
->requests_inflight
--;
138 reset_request(request
);
139 request
->dataplane
= dataplane
;
140 QLIST_INSERT_HEAD(&dataplane
->freelist
, request
, list
);
144 * translate request into iovec + start offset
145 * do sanity checks along the way
147 static int xen_block_parse_request(XenBlockRequest
*request
)
149 XenBlockDataPlane
*dataplane
= request
->dataplane
;
153 switch (request
->req
.operation
) {
156 case BLKIF_OP_FLUSH_DISKCACHE
:
157 request
->presync
= 1;
158 if (!request
->req
.nr_segments
) {
164 case BLKIF_OP_DISCARD
:
167 error_report("error: unknown operation (%d)", request
->req
.operation
);
171 if (request
->req
.operation
!= BLKIF_OP_READ
&&
172 !blk_is_writable(dataplane
->blk
)) {
173 error_report("error: write req for ro device");
177 request
->start
= request
->req
.sector_number
* dataplane
->sector_size
;
178 for (i
= 0; i
< request
->req
.nr_segments
; i
++) {
179 if (i
== BLKIF_MAX_SEGMENTS_PER_REQUEST
) {
180 error_report("error: nr_segments too big");
183 if (request
->req
.seg
[i
].first_sect
> request
->req
.seg
[i
].last_sect
) {
184 error_report("error: first > last sector");
187 if (request
->req
.seg
[i
].last_sect
* dataplane
->sector_size
>=
189 error_report("error: page crossing");
193 len
= (request
->req
.seg
[i
].last_sect
-
194 request
->req
.seg
[i
].first_sect
+ 1) * dataplane
->sector_size
;
195 request
->size
+= len
;
197 if (request
->start
+ request
->size
> blk_getlength(dataplane
->blk
)) {
198 error_report("error: access beyond end of file");
204 request
->status
= BLKIF_RSP_ERROR
;
208 static int xen_block_copy_request(XenBlockRequest
*request
)
210 XenBlockDataPlane
*dataplane
= request
->dataplane
;
211 XenDevice
*xendev
= dataplane
->xendev
;
212 XenDeviceGrantCopySegment segs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
214 bool to_domain
= (request
->req
.operation
== BLKIF_OP_READ
);
215 void *virt
= request
->buf
;
216 Error
*local_err
= NULL
;
218 if (request
->req
.nr_segments
== 0) {
222 count
= request
->req
.nr_segments
;
224 for (i
= 0; i
< count
; i
++) {
226 segs
[i
].dest
.foreign
.ref
= request
->req
.seg
[i
].gref
;
227 segs
[i
].dest
.foreign
.offset
= request
->req
.seg
[i
].first_sect
*
228 dataplane
->sector_size
;
229 segs
[i
].source
.virt
= virt
;
231 segs
[i
].source
.foreign
.ref
= request
->req
.seg
[i
].gref
;
232 segs
[i
].source
.foreign
.offset
= request
->req
.seg
[i
].first_sect
*
233 dataplane
->sector_size
;
234 segs
[i
].dest
.virt
= virt
;
236 segs
[i
].len
= (request
->req
.seg
[i
].last_sect
-
237 request
->req
.seg
[i
].first_sect
+ 1) *
238 dataplane
->sector_size
;
242 xen_device_copy_grant_refs(xendev
, to_domain
, segs
, count
, &local_err
);
245 error_reportf_err(local_err
, "failed to copy data: ");
247 request
->aio_errors
++;
254 static int xen_block_do_aio(XenBlockRequest
*request
);
256 static void xen_block_complete_aio(void *opaque
, int ret
)
258 XenBlockRequest
*request
= opaque
;
259 XenBlockDataPlane
*dataplane
= request
->dataplane
;
261 aio_context_acquire(dataplane
->ctx
);
264 error_report("%s I/O error",
265 request
->req
.operation
== BLKIF_OP_READ
?
267 request
->aio_errors
++;
270 request
->aio_inflight
--;
271 if (request
->presync
) {
272 request
->presync
= 0;
273 xen_block_do_aio(request
);
276 if (request
->aio_inflight
> 0) {
280 switch (request
->req
.operation
) {
282 /* in case of failure request->aio_errors is increased */
284 xen_block_copy_request(request
);
288 case BLKIF_OP_FLUSH_DISKCACHE
:
293 request
->status
= request
->aio_errors
? BLKIF_RSP_ERROR
: BLKIF_RSP_OKAY
;
295 switch (request
->req
.operation
) {
297 case BLKIF_OP_FLUSH_DISKCACHE
:
298 if (!request
->req
.nr_segments
) {
303 if (request
->status
== BLKIF_RSP_OKAY
) {
304 block_acct_done(blk_get_stats(dataplane
->blk
), &request
->acct
);
306 block_acct_failed(blk_get_stats(dataplane
->blk
), &request
->acct
);
309 case BLKIF_OP_DISCARD
:
314 xen_block_complete_request(request
);
316 if (dataplane
->more_work
) {
317 qemu_bh_schedule(dataplane
->bh
);
321 aio_context_release(dataplane
->ctx
);
324 static bool xen_block_split_discard(XenBlockRequest
*request
,
325 blkif_sector_t sector_number
,
328 XenBlockDataPlane
*dataplane
= request
->dataplane
;
331 uint64_t byte_remaining
;
332 uint64_t sec_start
= sector_number
;
333 uint64_t sec_count
= nr_sectors
;
335 /* Wrap around, or overflowing byte limit? */
336 if (sec_start
+ sec_count
< sec_count
||
337 sec_start
+ sec_count
> INT64_MAX
/ dataplane
->sector_size
) {
341 byte_offset
= sec_start
* dataplane
->sector_size
;
342 byte_remaining
= sec_count
* dataplane
->sector_size
;
345 byte_chunk
= byte_remaining
> BDRV_REQUEST_MAX_BYTES
?
346 BDRV_REQUEST_MAX_BYTES
: byte_remaining
;
347 request
->aio_inflight
++;
348 blk_aio_pdiscard(dataplane
->blk
, byte_offset
, byte_chunk
,
349 xen_block_complete_aio
, request
);
350 byte_remaining
-= byte_chunk
;
351 byte_offset
+= byte_chunk
;
352 } while (byte_remaining
> 0);
357 static int xen_block_do_aio(XenBlockRequest
*request
)
359 XenBlockDataPlane
*dataplane
= request
->dataplane
;
361 if (request
->req
.nr_segments
&&
362 (request
->req
.operation
== BLKIF_OP_WRITE
||
363 request
->req
.operation
== BLKIF_OP_FLUSH_DISKCACHE
) &&
364 xen_block_copy_request(request
)) {
368 request
->aio_inflight
++;
369 if (request
->presync
) {
370 blk_aio_flush(request
->dataplane
->blk
, xen_block_complete_aio
,
375 switch (request
->req
.operation
) {
377 qemu_iovec_add(&request
->v
, request
->buf
, request
->size
);
378 block_acct_start(blk_get_stats(dataplane
->blk
), &request
->acct
,
379 request
->v
.size
, BLOCK_ACCT_READ
);
380 request
->aio_inflight
++;
381 blk_aio_preadv(dataplane
->blk
, request
->start
, &request
->v
, 0,
382 xen_block_complete_aio
, request
);
385 case BLKIF_OP_FLUSH_DISKCACHE
:
386 if (!request
->req
.nr_segments
) {
390 qemu_iovec_add(&request
->v
, request
->buf
, request
->size
);
391 block_acct_start(blk_get_stats(dataplane
->blk
), &request
->acct
,
393 request
->req
.operation
== BLKIF_OP_WRITE
?
394 BLOCK_ACCT_WRITE
: BLOCK_ACCT_FLUSH
);
395 request
->aio_inflight
++;
396 blk_aio_pwritev(dataplane
->blk
, request
->start
, &request
->v
, 0,
397 xen_block_complete_aio
, request
);
399 case BLKIF_OP_DISCARD
:
401 struct blkif_request_discard
*req
= (void *)&request
->req
;
402 if (!xen_block_split_discard(request
, req
->sector_number
,
409 /* unknown operation (shouldn't happen -- parse catches this) */
413 xen_block_complete_aio(request
, 0);
418 request
->status
= BLKIF_RSP_ERROR
;
419 xen_block_complete_request(request
);
423 static int xen_block_send_response(XenBlockRequest
*request
)
425 XenBlockDataPlane
*dataplane
= request
->dataplane
;
427 int have_requests
= 0;
428 blkif_response_t
*resp
;
430 /* Place on the response ring for the relevant domain. */
431 switch (dataplane
->protocol
) {
432 case BLKIF_PROTOCOL_NATIVE
:
433 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
434 &dataplane
->rings
.native
,
435 dataplane
->rings
.native
.rsp_prod_pvt
);
437 case BLKIF_PROTOCOL_X86_32
:
438 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
439 &dataplane
->rings
.x86_32_part
,
440 dataplane
->rings
.x86_32_part
.rsp_prod_pvt
);
442 case BLKIF_PROTOCOL_X86_64
:
443 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
444 &dataplane
->rings
.x86_64_part
,
445 dataplane
->rings
.x86_64_part
.rsp_prod_pvt
);
451 resp
->id
= request
->req
.id
;
452 resp
->operation
= request
->req
.operation
;
453 resp
->status
= request
->status
;
455 dataplane
->rings
.common
.rsp_prod_pvt
++;
457 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&dataplane
->rings
.common
,
459 if (dataplane
->rings
.common
.rsp_prod_pvt
==
460 dataplane
->rings
.common
.req_cons
) {
462 * Tail check for pending requests. Allows frontend to avoid
463 * notifications if requests are already in flight (lower
464 * overheads and promotes batching).
466 RING_FINAL_CHECK_FOR_REQUESTS(&dataplane
->rings
.common
,
468 } else if (RING_HAS_UNCONSUMED_REQUESTS(&dataplane
->rings
.common
)) {
473 dataplane
->more_work
++;
478 static int xen_block_get_request(XenBlockDataPlane
*dataplane
,
479 XenBlockRequest
*request
, RING_IDX rc
)
481 switch (dataplane
->protocol
) {
482 case BLKIF_PROTOCOL_NATIVE
: {
483 blkif_request_t
*req
=
484 RING_GET_REQUEST(&dataplane
->rings
.native
, rc
);
486 memcpy(&request
->req
, req
, sizeof(request
->req
));
489 case BLKIF_PROTOCOL_X86_32
: {
490 blkif_x86_32_request_t
*req
=
491 RING_GET_REQUEST(&dataplane
->rings
.x86_32_part
, rc
);
493 blkif_get_x86_32_req(&request
->req
, req
);
496 case BLKIF_PROTOCOL_X86_64
: {
497 blkif_x86_64_request_t
*req
=
498 RING_GET_REQUEST(&dataplane
->rings
.x86_64_part
, rc
);
500 blkif_get_x86_64_req(&request
->req
, req
);
504 /* Prevent the compiler from accessing the on-ring fields instead. */
510 * Threshold of in-flight requests above which we will start using
511 * blk_io_plug()/blk_io_unplug() to batch requests.
513 #define IO_PLUG_THRESHOLD 1
515 static bool xen_block_handle_requests(XenBlockDataPlane
*dataplane
)
518 XenBlockRequest
*request
;
519 int inflight_atstart
= dataplane
->requests_inflight
;
521 bool done_something
= false;
523 dataplane
->more_work
= 0;
525 rc
= dataplane
->rings
.common
.req_cons
;
526 rp
= dataplane
->rings
.common
.sring
->req_prod
;
527 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
530 * If there was more than IO_PLUG_THRESHOLD requests in flight
531 * when we got here, this is an indication that there the bottleneck
532 * is below us, so it's worth beginning to batch up I/O requests
533 * rather than submitting them immediately. The maximum number
534 * of requests we're willing to batch is the number already in
535 * flight, so it can grow up to max_requests when the bottleneck
538 if (inflight_atstart
> IO_PLUG_THRESHOLD
) {
539 blk_io_plug(dataplane
->blk
);
542 /* pull request from ring */
543 if (RING_REQUEST_CONS_OVERFLOW(&dataplane
->rings
.common
, rc
)) {
546 request
= xen_block_start_request(dataplane
);
547 if (request
== NULL
) {
548 dataplane
->more_work
++;
551 xen_block_get_request(dataplane
, request
, rc
);
552 dataplane
->rings
.common
.req_cons
= ++rc
;
553 done_something
= true;
556 if (xen_block_parse_request(request
) != 0) {
557 switch (request
->req
.operation
) {
559 block_acct_invalid(blk_get_stats(dataplane
->blk
),
563 block_acct_invalid(blk_get_stats(dataplane
->blk
),
566 case BLKIF_OP_FLUSH_DISKCACHE
:
567 block_acct_invalid(blk_get_stats(dataplane
->blk
),
573 xen_block_complete_request(request
);
577 if (inflight_atstart
> IO_PLUG_THRESHOLD
&&
578 batched
>= inflight_atstart
) {
579 blk_io_unplug(dataplane
->blk
);
581 xen_block_do_aio(request
);
582 if (inflight_atstart
> IO_PLUG_THRESHOLD
) {
583 if (batched
>= inflight_atstart
) {
584 blk_io_plug(dataplane
->blk
);
591 if (inflight_atstart
> IO_PLUG_THRESHOLD
) {
592 blk_io_unplug(dataplane
->blk
);
595 return done_something
;
598 static void xen_block_dataplane_bh(void *opaque
)
600 XenBlockDataPlane
*dataplane
= opaque
;
602 aio_context_acquire(dataplane
->ctx
);
603 xen_block_handle_requests(dataplane
);
604 aio_context_release(dataplane
->ctx
);
607 static bool xen_block_dataplane_event(void *opaque
)
609 XenBlockDataPlane
*dataplane
= opaque
;
611 return xen_block_handle_requests(dataplane
);
614 XenBlockDataPlane
*xen_block_dataplane_create(XenDevice
*xendev
,
616 unsigned int sector_size
,
619 XenBlockDataPlane
*dataplane
= g_new0(XenBlockDataPlane
, 1);
621 dataplane
->xendev
= xendev
;
622 dataplane
->blk
= blk
;
623 dataplane
->sector_size
= sector_size
;
625 QLIST_INIT(&dataplane
->inflight
);
626 QLIST_INIT(&dataplane
->freelist
);
629 dataplane
->iothread
= iothread
;
630 object_ref(OBJECT(dataplane
->iothread
));
631 dataplane
->ctx
= iothread_get_aio_context(dataplane
->iothread
);
633 dataplane
->ctx
= qemu_get_aio_context();
635 dataplane
->bh
= aio_bh_new(dataplane
->ctx
, xen_block_dataplane_bh
,
641 void xen_block_dataplane_destroy(XenBlockDataPlane
*dataplane
)
643 XenBlockRequest
*request
;
649 while (!QLIST_EMPTY(&dataplane
->freelist
)) {
650 request
= QLIST_FIRST(&dataplane
->freelist
);
651 QLIST_REMOVE(request
, list
);
652 qemu_iovec_destroy(&request
->v
);
653 qemu_vfree(request
->buf
);
657 qemu_bh_delete(dataplane
->bh
);
658 if (dataplane
->iothread
) {
659 object_unref(OBJECT(dataplane
->iothread
));
665 void xen_block_dataplane_stop(XenBlockDataPlane
*dataplane
)
673 xendev
= dataplane
->xendev
;
675 aio_context_acquire(dataplane
->ctx
);
676 if (dataplane
->event_channel
) {
677 /* Only reason for failure is a NULL channel */
678 xen_device_set_event_channel_context(xendev
, dataplane
->event_channel
,
679 qemu_get_aio_context(),
682 /* Xen doesn't have multiple users for nodes, so this can't fail */
683 blk_set_aio_context(dataplane
->blk
, qemu_get_aio_context(), &error_abort
);
684 aio_context_release(dataplane
->ctx
);
687 * Now that the context has been moved onto the main thread, cancel
688 * further processing.
690 qemu_bh_cancel(dataplane
->bh
);
692 if (dataplane
->event_channel
) {
693 Error
*local_err
= NULL
;
695 xen_device_unbind_event_channel(xendev
, dataplane
->event_channel
,
697 dataplane
->event_channel
= NULL
;
700 error_report_err(local_err
);
704 if (dataplane
->sring
) {
705 Error
*local_err
= NULL
;
707 xen_device_unmap_grant_refs(xendev
, dataplane
->sring
,
708 dataplane
->nr_ring_ref
, &local_err
);
709 dataplane
->sring
= NULL
;
712 error_report_err(local_err
);
716 g_free(dataplane
->ring_ref
);
717 dataplane
->ring_ref
= NULL
;
720 void xen_block_dataplane_start(XenBlockDataPlane
*dataplane
,
721 const unsigned int ring_ref
[],
722 unsigned int nr_ring_ref
,
723 unsigned int event_channel
,
724 unsigned int protocol
,
728 XenDevice
*xendev
= dataplane
->xendev
;
729 AioContext
*old_context
;
730 unsigned int ring_size
;
733 dataplane
->nr_ring_ref
= nr_ring_ref
;
734 dataplane
->ring_ref
= g_new(unsigned int, nr_ring_ref
);
736 for (i
= 0; i
< nr_ring_ref
; i
++) {
737 dataplane
->ring_ref
[i
] = ring_ref
[i
];
740 dataplane
->protocol
= protocol
;
742 ring_size
= XC_PAGE_SIZE
* dataplane
->nr_ring_ref
;
743 switch (dataplane
->protocol
) {
744 case BLKIF_PROTOCOL_NATIVE
:
746 dataplane
->max_requests
= __CONST_RING_SIZE(blkif
, ring_size
);
749 case BLKIF_PROTOCOL_X86_32
:
751 dataplane
->max_requests
= __CONST_RING_SIZE(blkif_x86_32
, ring_size
);
754 case BLKIF_PROTOCOL_X86_64
:
756 dataplane
->max_requests
= __CONST_RING_SIZE(blkif_x86_64
, ring_size
);
760 error_setg(errp
, "unknown protocol %u", dataplane
->protocol
);
764 xen_device_set_max_grant_refs(xendev
, dataplane
->nr_ring_ref
,
770 dataplane
->sring
= xen_device_map_grant_refs(xendev
,
772 dataplane
->nr_ring_ref
,
773 PROT_READ
| PROT_WRITE
,
779 switch (dataplane
->protocol
) {
780 case BLKIF_PROTOCOL_NATIVE
:
782 blkif_sring_t
*sring_native
= dataplane
->sring
;
784 BACK_RING_INIT(&dataplane
->rings
.native
, sring_native
, ring_size
);
787 case BLKIF_PROTOCOL_X86_32
:
789 blkif_x86_32_sring_t
*sring_x86_32
= dataplane
->sring
;
791 BACK_RING_INIT(&dataplane
->rings
.x86_32_part
, sring_x86_32
,
795 case BLKIF_PROTOCOL_X86_64
:
797 blkif_x86_64_sring_t
*sring_x86_64
= dataplane
->sring
;
799 BACK_RING_INIT(&dataplane
->rings
.x86_64_part
, sring_x86_64
,
805 dataplane
->event_channel
=
806 xen_device_bind_event_channel(xendev
, event_channel
,
807 xen_block_dataplane_event
, dataplane
,
813 old_context
= blk_get_aio_context(dataplane
->blk
);
814 aio_context_acquire(old_context
);
815 /* If other users keep the BlockBackend in the iothread, that's ok */
816 blk_set_aio_context(dataplane
->blk
, dataplane
->ctx
, NULL
);
817 aio_context_release(old_context
);
819 /* Only reason for failure is a NULL channel */
820 aio_context_acquire(dataplane
->ctx
);
821 xen_device_set_event_channel_context(xendev
, dataplane
->event_channel
,
822 dataplane
->ctx
, &error_abort
);
823 aio_context_release(dataplane
->ctx
);
828 xen_block_dataplane_stop(dataplane
);