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 "qapi/error.h"
25 #include "hw/xen/xen_common.h"
26 #include "hw/block/xen_blkif.h"
27 #include "sysemu/block-backend.h"
28 #include "sysemu/iothread.h"
29 #include "xen-block.h"
31 typedef struct XenBlockRequest
{
41 XenBlockDataPlane
*dataplane
;
42 QLIST_ENTRY(XenBlockRequest
) list
;
46 struct XenBlockDataPlane
{
48 XenEventChannel
*event_channel
;
49 unsigned int *ring_ref
;
50 unsigned int nr_ring_ref
;
53 blkif_back_rings_t rings
;
55 QLIST_HEAD(inflight_head
, XenBlockRequest
) inflight
;
56 QLIST_HEAD(freelist_head
, XenBlockRequest
) freelist
;
58 int requests_inflight
;
59 unsigned int max_requests
;
61 unsigned int sector_size
;
67 static int xen_block_send_response(XenBlockRequest
*request
);
69 static void reset_request(XenBlockRequest
*request
)
71 memset(&request
->req
, 0, sizeof(request
->req
));
77 request
->aio_inflight
= 0;
78 request
->aio_errors
= 0;
80 request
->dataplane
= NULL
;
81 memset(&request
->list
, 0, sizeof(request
->list
));
82 memset(&request
->acct
, 0, sizeof(request
->acct
));
84 qemu_iovec_reset(&request
->v
);
87 static XenBlockRequest
*xen_block_start_request(XenBlockDataPlane
*dataplane
)
89 XenBlockRequest
*request
= NULL
;
91 if (QLIST_EMPTY(&dataplane
->freelist
)) {
92 if (dataplane
->requests_total
>= dataplane
->max_requests
) {
95 /* allocate new struct */
96 request
= g_malloc0(sizeof(*request
));
97 request
->dataplane
= dataplane
;
99 * We cannot need more pages per requests than this, and since we
100 * re-use requests, allocate the memory once here. It will be freed
101 * xen_block_dataplane_destroy() when the request list is freed.
103 request
->buf
= qemu_memalign(XC_PAGE_SIZE
,
104 BLKIF_MAX_SEGMENTS_PER_REQUEST
*
106 dataplane
->requests_total
++;
107 qemu_iovec_init(&request
->v
, 1);
109 /* get one from freelist */
110 request
= QLIST_FIRST(&dataplane
->freelist
);
111 QLIST_REMOVE(request
, list
);
113 QLIST_INSERT_HEAD(&dataplane
->inflight
, request
, list
);
114 dataplane
->requests_inflight
++;
120 static void xen_block_complete_request(XenBlockRequest
*request
)
122 XenBlockDataPlane
*dataplane
= request
->dataplane
;
124 if (xen_block_send_response(request
)) {
125 Error
*local_err
= NULL
;
127 xen_device_notify_event_channel(dataplane
->xendev
,
128 dataplane
->event_channel
,
131 error_report_err(local_err
);
135 QLIST_REMOVE(request
, list
);
136 dataplane
->requests_inflight
--;
137 reset_request(request
);
138 request
->dataplane
= dataplane
;
139 QLIST_INSERT_HEAD(&dataplane
->freelist
, request
, list
);
143 * translate request into iovec + start offset
144 * do sanity checks along the way
146 static int xen_block_parse_request(XenBlockRequest
*request
)
148 XenBlockDataPlane
*dataplane
= request
->dataplane
;
152 switch (request
->req
.operation
) {
155 case BLKIF_OP_FLUSH_DISKCACHE
:
156 request
->presync
= 1;
157 if (!request
->req
.nr_segments
) {
163 case BLKIF_OP_DISCARD
:
166 error_report("error: unknown operation (%d)", request
->req
.operation
);
170 if (request
->req
.operation
!= BLKIF_OP_READ
&&
171 blk_is_read_only(dataplane
->blk
)) {
172 error_report("error: write req for ro device");
176 request
->start
= request
->req
.sector_number
* dataplane
->sector_size
;
177 for (i
= 0; i
< request
->req
.nr_segments
; i
++) {
178 if (i
== BLKIF_MAX_SEGMENTS_PER_REQUEST
) {
179 error_report("error: nr_segments too big");
182 if (request
->req
.seg
[i
].first_sect
> request
->req
.seg
[i
].last_sect
) {
183 error_report("error: first > last sector");
186 if (request
->req
.seg
[i
].last_sect
* dataplane
->sector_size
>=
188 error_report("error: page crossing");
192 len
= (request
->req
.seg
[i
].last_sect
-
193 request
->req
.seg
[i
].first_sect
+ 1) * dataplane
->sector_size
;
194 request
->size
+= len
;
196 if (request
->start
+ request
->size
> blk_getlength(dataplane
->blk
)) {
197 error_report("error: access beyond end of file");
203 request
->status
= BLKIF_RSP_ERROR
;
207 static int xen_block_copy_request(XenBlockRequest
*request
)
209 XenBlockDataPlane
*dataplane
= request
->dataplane
;
210 XenDevice
*xendev
= dataplane
->xendev
;
211 XenDeviceGrantCopySegment segs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
213 bool to_domain
= (request
->req
.operation
== BLKIF_OP_READ
);
214 void *virt
= request
->buf
;
215 Error
*local_err
= NULL
;
217 if (request
->req
.nr_segments
== 0) {
221 count
= request
->req
.nr_segments
;
223 for (i
= 0; i
< count
; i
++) {
225 segs
[i
].dest
.foreign
.ref
= request
->req
.seg
[i
].gref
;
226 segs
[i
].dest
.foreign
.offset
= request
->req
.seg
[i
].first_sect
*
227 dataplane
->sector_size
;
228 segs
[i
].source
.virt
= virt
;
230 segs
[i
].source
.foreign
.ref
= request
->req
.seg
[i
].gref
;
231 segs
[i
].source
.foreign
.offset
= request
->req
.seg
[i
].first_sect
*
232 dataplane
->sector_size
;
233 segs
[i
].dest
.virt
= virt
;
235 segs
[i
].len
= (request
->req
.seg
[i
].last_sect
-
236 request
->req
.seg
[i
].first_sect
+ 1) *
237 dataplane
->sector_size
;
241 xen_device_copy_grant_refs(xendev
, to_domain
, segs
, count
, &local_err
);
244 error_reportf_err(local_err
, "failed to copy data: ");
246 request
->aio_errors
++;
253 static int xen_block_do_aio(XenBlockRequest
*request
);
255 static void xen_block_complete_aio(void *opaque
, int ret
)
257 XenBlockRequest
*request
= opaque
;
258 XenBlockDataPlane
*dataplane
= request
->dataplane
;
260 aio_context_acquire(dataplane
->ctx
);
263 error_report("%s I/O error",
264 request
->req
.operation
== BLKIF_OP_READ
?
266 request
->aio_errors
++;
269 request
->aio_inflight
--;
270 if (request
->presync
) {
271 request
->presync
= 0;
272 xen_block_do_aio(request
);
275 if (request
->aio_inflight
> 0) {
279 switch (request
->req
.operation
) {
281 /* in case of failure request->aio_errors is increased */
283 xen_block_copy_request(request
);
287 case BLKIF_OP_FLUSH_DISKCACHE
:
292 request
->status
= request
->aio_errors
? BLKIF_RSP_ERROR
: BLKIF_RSP_OKAY
;
294 switch (request
->req
.operation
) {
296 case BLKIF_OP_FLUSH_DISKCACHE
:
297 if (!request
->req
.nr_segments
) {
302 if (request
->status
== BLKIF_RSP_OKAY
) {
303 block_acct_done(blk_get_stats(dataplane
->blk
), &request
->acct
);
305 block_acct_failed(blk_get_stats(dataplane
->blk
), &request
->acct
);
308 case BLKIF_OP_DISCARD
:
313 xen_block_complete_request(request
);
315 if (dataplane
->more_work
) {
316 qemu_bh_schedule(dataplane
->bh
);
320 aio_context_release(dataplane
->ctx
);
323 static bool xen_block_split_discard(XenBlockRequest
*request
,
324 blkif_sector_t sector_number
,
327 XenBlockDataPlane
*dataplane
= request
->dataplane
;
330 uint64_t byte_remaining
;
331 uint64_t sec_start
= sector_number
;
332 uint64_t sec_count
= nr_sectors
;
334 /* Wrap around, or overflowing byte limit? */
335 if (sec_start
+ sec_count
< sec_count
||
336 sec_start
+ sec_count
> INT64_MAX
/ dataplane
->sector_size
) {
340 byte_offset
= sec_start
* dataplane
->sector_size
;
341 byte_remaining
= sec_count
* dataplane
->sector_size
;
344 byte_chunk
= byte_remaining
> BDRV_REQUEST_MAX_BYTES
?
345 BDRV_REQUEST_MAX_BYTES
: byte_remaining
;
346 request
->aio_inflight
++;
347 blk_aio_pdiscard(dataplane
->blk
, byte_offset
, byte_chunk
,
348 xen_block_complete_aio
, request
);
349 byte_remaining
-= byte_chunk
;
350 byte_offset
+= byte_chunk
;
351 } while (byte_remaining
> 0);
356 static int xen_block_do_aio(XenBlockRequest
*request
)
358 XenBlockDataPlane
*dataplane
= request
->dataplane
;
360 if (request
->req
.nr_segments
&&
361 (request
->req
.operation
== BLKIF_OP_WRITE
||
362 request
->req
.operation
== BLKIF_OP_FLUSH_DISKCACHE
) &&
363 xen_block_copy_request(request
)) {
367 request
->aio_inflight
++;
368 if (request
->presync
) {
369 blk_aio_flush(request
->dataplane
->blk
, xen_block_complete_aio
,
374 switch (request
->req
.operation
) {
376 qemu_iovec_add(&request
->v
, request
->buf
, request
->size
);
377 block_acct_start(blk_get_stats(dataplane
->blk
), &request
->acct
,
378 request
->v
.size
, BLOCK_ACCT_READ
);
379 request
->aio_inflight
++;
380 blk_aio_preadv(dataplane
->blk
, request
->start
, &request
->v
, 0,
381 xen_block_complete_aio
, request
);
384 case BLKIF_OP_FLUSH_DISKCACHE
:
385 if (!request
->req
.nr_segments
) {
389 qemu_iovec_add(&request
->v
, request
->buf
, request
->size
);
390 block_acct_start(blk_get_stats(dataplane
->blk
), &request
->acct
,
392 request
->req
.operation
== BLKIF_OP_WRITE
?
393 BLOCK_ACCT_WRITE
: BLOCK_ACCT_FLUSH
);
394 request
->aio_inflight
++;
395 blk_aio_pwritev(dataplane
->blk
, request
->start
, &request
->v
, 0,
396 xen_block_complete_aio
, request
);
398 case BLKIF_OP_DISCARD
:
400 struct blkif_request_discard
*req
= (void *)&request
->req
;
401 if (!xen_block_split_discard(request
, req
->sector_number
,
408 /* unknown operation (shouldn't happen -- parse catches this) */
412 xen_block_complete_aio(request
, 0);
417 request
->status
= BLKIF_RSP_ERROR
;
418 xen_block_complete_request(request
);
422 static int xen_block_send_response(XenBlockRequest
*request
)
424 XenBlockDataPlane
*dataplane
= request
->dataplane
;
426 int have_requests
= 0;
427 blkif_response_t
*resp
;
429 /* Place on the response ring for the relevant domain. */
430 switch (dataplane
->protocol
) {
431 case BLKIF_PROTOCOL_NATIVE
:
432 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
433 &dataplane
->rings
.native
,
434 dataplane
->rings
.native
.rsp_prod_pvt
);
436 case BLKIF_PROTOCOL_X86_32
:
437 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
438 &dataplane
->rings
.x86_32_part
,
439 dataplane
->rings
.x86_32_part
.rsp_prod_pvt
);
441 case BLKIF_PROTOCOL_X86_64
:
442 resp
= (blkif_response_t
*)RING_GET_RESPONSE(
443 &dataplane
->rings
.x86_64_part
,
444 dataplane
->rings
.x86_64_part
.rsp_prod_pvt
);
450 resp
->id
= request
->req
.id
;
451 resp
->operation
= request
->req
.operation
;
452 resp
->status
= request
->status
;
454 dataplane
->rings
.common
.rsp_prod_pvt
++;
456 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&dataplane
->rings
.common
,
458 if (dataplane
->rings
.common
.rsp_prod_pvt
==
459 dataplane
->rings
.common
.req_cons
) {
461 * Tail check for pending requests. Allows frontend to avoid
462 * notifications if requests are already in flight (lower
463 * overheads and promotes batching).
465 RING_FINAL_CHECK_FOR_REQUESTS(&dataplane
->rings
.common
,
467 } else if (RING_HAS_UNCONSUMED_REQUESTS(&dataplane
->rings
.common
)) {
472 dataplane
->more_work
++;
477 static int xen_block_get_request(XenBlockDataPlane
*dataplane
,
478 XenBlockRequest
*request
, RING_IDX rc
)
480 switch (dataplane
->protocol
) {
481 case BLKIF_PROTOCOL_NATIVE
: {
482 blkif_request_t
*req
=
483 RING_GET_REQUEST(&dataplane
->rings
.native
, rc
);
485 memcpy(&request
->req
, req
, sizeof(request
->req
));
488 case BLKIF_PROTOCOL_X86_32
: {
489 blkif_x86_32_request_t
*req
=
490 RING_GET_REQUEST(&dataplane
->rings
.x86_32_part
, rc
);
492 blkif_get_x86_32_req(&request
->req
, req
);
495 case BLKIF_PROTOCOL_X86_64
: {
496 blkif_x86_64_request_t
*req
=
497 RING_GET_REQUEST(&dataplane
->rings
.x86_64_part
, rc
);
499 blkif_get_x86_64_req(&request
->req
, req
);
503 /* Prevent the compiler from accessing the on-ring fields instead. */
509 * Threshold of in-flight requests above which we will start using
510 * blk_io_plug()/blk_io_unplug() to batch requests.
512 #define IO_PLUG_THRESHOLD 1
514 static bool xen_block_handle_requests(XenBlockDataPlane
*dataplane
)
517 XenBlockRequest
*request
;
518 int inflight_atstart
= dataplane
->requests_inflight
;
520 bool done_something
= false;
522 dataplane
->more_work
= 0;
524 rc
= dataplane
->rings
.common
.req_cons
;
525 rp
= dataplane
->rings
.common
.sring
->req_prod
;
526 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
529 * If there was more than IO_PLUG_THRESHOLD requests in flight
530 * when we got here, this is an indication that there the bottleneck
531 * is below us, so it's worth beginning to batch up I/O requests
532 * rather than submitting them immediately. The maximum number
533 * of requests we're willing to batch is the number already in
534 * flight, so it can grow up to max_requests when the bottleneck
537 if (inflight_atstart
> IO_PLUG_THRESHOLD
) {
538 blk_io_plug(dataplane
->blk
);
541 /* pull request from ring */
542 if (RING_REQUEST_CONS_OVERFLOW(&dataplane
->rings
.common
, rc
)) {
545 request
= xen_block_start_request(dataplane
);
546 if (request
== NULL
) {
547 dataplane
->more_work
++;
550 xen_block_get_request(dataplane
, request
, rc
);
551 dataplane
->rings
.common
.req_cons
= ++rc
;
552 done_something
= true;
555 if (xen_block_parse_request(request
) != 0) {
556 switch (request
->req
.operation
) {
558 block_acct_invalid(blk_get_stats(dataplane
->blk
),
562 block_acct_invalid(blk_get_stats(dataplane
->blk
),
565 case BLKIF_OP_FLUSH_DISKCACHE
:
566 block_acct_invalid(blk_get_stats(dataplane
->blk
),
572 xen_block_complete_request(request
);
576 if (inflight_atstart
> IO_PLUG_THRESHOLD
&&
577 batched
>= inflight_atstart
) {
578 blk_io_unplug(dataplane
->blk
);
580 xen_block_do_aio(request
);
581 if (inflight_atstart
> IO_PLUG_THRESHOLD
) {
582 if (batched
>= inflight_atstart
) {
583 blk_io_plug(dataplane
->blk
);
590 if (inflight_atstart
> IO_PLUG_THRESHOLD
) {
591 blk_io_unplug(dataplane
->blk
);
594 return done_something
;
597 static void xen_block_dataplane_bh(void *opaque
)
599 XenBlockDataPlane
*dataplane
= opaque
;
601 aio_context_acquire(dataplane
->ctx
);
602 xen_block_handle_requests(dataplane
);
603 aio_context_release(dataplane
->ctx
);
606 static bool xen_block_dataplane_event(void *opaque
)
608 XenBlockDataPlane
*dataplane
= opaque
;
610 return xen_block_handle_requests(dataplane
);
613 XenBlockDataPlane
*xen_block_dataplane_create(XenDevice
*xendev
,
615 unsigned int sector_size
,
618 XenBlockDataPlane
*dataplane
= g_new0(XenBlockDataPlane
, 1);
620 dataplane
->xendev
= xendev
;
621 dataplane
->blk
= blk
;
622 dataplane
->sector_size
= sector_size
;
624 QLIST_INIT(&dataplane
->inflight
);
625 QLIST_INIT(&dataplane
->freelist
);
628 dataplane
->iothread
= iothread
;
629 object_ref(OBJECT(dataplane
->iothread
));
630 dataplane
->ctx
= iothread_get_aio_context(dataplane
->iothread
);
632 dataplane
->ctx
= qemu_get_aio_context();
634 dataplane
->bh
= aio_bh_new(dataplane
->ctx
, xen_block_dataplane_bh
,
640 void xen_block_dataplane_destroy(XenBlockDataPlane
*dataplane
)
642 XenBlockRequest
*request
;
648 while (!QLIST_EMPTY(&dataplane
->freelist
)) {
649 request
= QLIST_FIRST(&dataplane
->freelist
);
650 QLIST_REMOVE(request
, list
);
651 qemu_iovec_destroy(&request
->v
);
652 qemu_vfree(request
->buf
);
656 qemu_bh_delete(dataplane
->bh
);
657 if (dataplane
->iothread
) {
658 object_unref(OBJECT(dataplane
->iothread
));
664 void xen_block_dataplane_stop(XenBlockDataPlane
*dataplane
)
672 xendev
= dataplane
->xendev
;
674 aio_context_acquire(dataplane
->ctx
);
675 if (dataplane
->event_channel
) {
676 /* Only reason for failure is a NULL channel */
677 xen_device_set_event_channel_context(xendev
, dataplane
->event_channel
,
678 qemu_get_aio_context(),
681 /* Xen doesn't have multiple users for nodes, so this can't fail */
682 blk_set_aio_context(dataplane
->blk
, qemu_get_aio_context(), &error_abort
);
683 aio_context_release(dataplane
->ctx
);
686 * Now that the context has been moved onto the main thread, cancel
687 * further processing.
689 qemu_bh_cancel(dataplane
->bh
);
691 if (dataplane
->event_channel
) {
692 Error
*local_err
= NULL
;
694 xen_device_unbind_event_channel(xendev
, dataplane
->event_channel
,
696 dataplane
->event_channel
= NULL
;
699 error_report_err(local_err
);
703 if (dataplane
->sring
) {
704 Error
*local_err
= NULL
;
706 xen_device_unmap_grant_refs(xendev
, dataplane
->sring
,
707 dataplane
->nr_ring_ref
, &local_err
);
708 dataplane
->sring
= NULL
;
711 error_report_err(local_err
);
715 g_free(dataplane
->ring_ref
);
716 dataplane
->ring_ref
= NULL
;
719 void xen_block_dataplane_start(XenBlockDataPlane
*dataplane
,
720 const unsigned int ring_ref
[],
721 unsigned int nr_ring_ref
,
722 unsigned int event_channel
,
723 unsigned int protocol
,
727 XenDevice
*xendev
= dataplane
->xendev
;
728 unsigned int ring_size
;
731 dataplane
->nr_ring_ref
= nr_ring_ref
;
732 dataplane
->ring_ref
= g_new(unsigned int, nr_ring_ref
);
734 for (i
= 0; i
< nr_ring_ref
; i
++) {
735 dataplane
->ring_ref
[i
] = ring_ref
[i
];
738 dataplane
->protocol
= protocol
;
740 ring_size
= XC_PAGE_SIZE
* dataplane
->nr_ring_ref
;
741 switch (dataplane
->protocol
) {
742 case BLKIF_PROTOCOL_NATIVE
:
744 dataplane
->max_requests
= __CONST_RING_SIZE(blkif
, ring_size
);
747 case BLKIF_PROTOCOL_X86_32
:
749 dataplane
->max_requests
= __CONST_RING_SIZE(blkif_x86_32
, ring_size
);
752 case BLKIF_PROTOCOL_X86_64
:
754 dataplane
->max_requests
= __CONST_RING_SIZE(blkif_x86_64
, ring_size
);
758 error_setg(errp
, "unknown protocol %u", dataplane
->protocol
);
762 xen_device_set_max_grant_refs(xendev
, dataplane
->nr_ring_ref
,
768 dataplane
->sring
= xen_device_map_grant_refs(xendev
,
770 dataplane
->nr_ring_ref
,
771 PROT_READ
| PROT_WRITE
,
777 switch (dataplane
->protocol
) {
778 case BLKIF_PROTOCOL_NATIVE
:
780 blkif_sring_t
*sring_native
= dataplane
->sring
;
782 BACK_RING_INIT(&dataplane
->rings
.native
, sring_native
, ring_size
);
785 case BLKIF_PROTOCOL_X86_32
:
787 blkif_x86_32_sring_t
*sring_x86_32
= dataplane
->sring
;
789 BACK_RING_INIT(&dataplane
->rings
.x86_32_part
, sring_x86_32
,
793 case BLKIF_PROTOCOL_X86_64
:
795 blkif_x86_64_sring_t
*sring_x86_64
= dataplane
->sring
;
797 BACK_RING_INIT(&dataplane
->rings
.x86_64_part
, sring_x86_64
,
803 dataplane
->event_channel
=
804 xen_device_bind_event_channel(xendev
, event_channel
,
805 xen_block_dataplane_event
, dataplane
,
811 aio_context_acquire(dataplane
->ctx
);
812 /* If other users keep the BlockBackend in the iothread, that's ok */
813 blk_set_aio_context(dataplane
->blk
, dataplane
->ctx
, NULL
);
814 /* Only reason for failure is a NULL channel */
815 xen_device_set_event_channel_context(xendev
, dataplane
->event_channel
,
816 dataplane
->ctx
, &error_abort
);
817 aio_context_release(dataplane
->ctx
);
822 xen_block_dataplane_stop(dataplane
);