From patchwork Mon Jun 13 09:43:56 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paulina Szubarczyk X-Patchwork-Id: 9172467 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A01D060573 for ; Mon, 13 Jun 2016 09:47:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8E07B200E7 for ; Mon, 13 Jun 2016 09:47:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 82AD6265B9; Mon, 13 Jun 2016 09:47:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id EA995200E7 for ; Mon, 13 Jun 2016 09:47:31 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bCORA-0003Gy-2a; Mon, 13 Jun 2016 09:45:40 +0000 Received: from mail6.bemta6.messagelabs.com ([85.158.143.247]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bCOR9-0003Go-Gi for xen-devel@lists.xenproject.org; Mon, 13 Jun 2016 09:45:39 +0000 Received: from [85.158.143.35] by server-3.bemta-6.messagelabs.com id EF/A5-25713-2C08E575; Mon, 13 Jun 2016 09:45:38 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrEIsWRWlGSWpSXmKPExsVyMfS6o+6hhrh wgw+7uCy+b5nM5MDocfjDFZYAxijWzLyk/IoE1oz5626xFTQ6Vlx6tpapgfG+fhcjF4eQwHRG ievTDrOBOCwCL1kkds3/wATiSAj0s0p8/DCfsYuRE8iJkTi3+iCUXSlx9dI9ZhBbSEBL4tLKF SwQo74zSnx+1s8OkmATMJOYOfknC4gtImApcaLzGdhUZoHTjBJn7lwAKxIW8JFYemIBWBGLgK rEyTcfge7g4OAV8JZYcUsKYpmcxMljk1lBbE6g8uO9pxkhFntL9N2+xjqBUWABI8MqRvXi1KK y1CJdY72kosz0jJLcxMwcXUMDM73c1OLixPTUnMSkYr3k/NxNjMDQYgCCHYwd/5wOMUpyMCmJ 8kr4xIUL8SXlp1RmJBZnxBeV5qQWH2LU4OAQ6Fuz+gKjFEtefl6qkgSvWT1QnWBRanpqRVpmD jD4YUolOHiURHiDQNK8xQWJucWZ6RCpU4zGHFt+X1vLxLFt6r21TEJgk6TEeb/XAZUKgJRmlO bBDYJF5SVGWSlhXkagM4V4ClKLcjNLUOVfMYpzMCoJ89qBLOTJzCuB2/cK6BQmoFM49kWDnFK SiJCSamBsnJS0cFZEZsJjdkmm4ovfX6/2Vwq0krSq4HC/unqq4amjmmyhajciLsn8Tv/9/+65 avFGG6drfr49zTrTQ5/kmNluktP7cWdfW8n2Pcf2zajW3flx/7pTiZcsZlTNnc66UD7snviJ2 avLzy/Z2qR2Y8nTpaeWTfyy1NPAcfX9YyePWPYqac2VUGIpzkg01GIuKk4EAFV0X0TFAgAA X-Env-Sender: paulinaszubarczyk@gmail.com X-Msg-Ref: server-2.tower-21.messagelabs.com!1465811137!4746966!1 X-Originating-IP: [209.85.215.65] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.46; banners=-,-,- X-VirusChecked: Checked Received: (qmail 30658 invoked from network); 13 Jun 2016 09:45:38 -0000 Received: from mail-lf0-f65.google.com (HELO mail-lf0-f65.google.com) (209.85.215.65) by server-2.tower-21.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 13 Jun 2016 09:45:38 -0000 Received: by mail-lf0-f65.google.com with SMTP id l184so939293lfl.1 for ; Mon, 13 Jun 2016 02:45:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=CX/vcQ+wPGWBMz2kPLQrylMX+WxYnJ5MYZjAHXfZrzc=; b=eTTyn/GIgwapwHFohEdpgrYIqyS99s2y4iAHZmh+2oILLkpzlxxhxnyUoTs+Q8z3VC 8elG0eMpY6djfJtN3KoLLoaFpQgB7iGxoRt0OxIFbNAf5Cx0t8UfoJTk4fM02JOz05/X Vc+e7FBuanbONQ5ht08zDvVPxnb8LYkY76JuSArylb/YyGHQrD7ohsK0gXFCkncTUviG E2VfmtZW2b/py+SMnCfpOrS1lhouDrw6GB6klceti4yl4RcUCCT+qjEKBROyjC+f2UNG 10ToMa7IDmX2/5Re5TBUMiowE61ssPEMA0t60QtdZLfxVfSAq/u8wY/Q/yXBUOJ+YCkq kN2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=CX/vcQ+wPGWBMz2kPLQrylMX+WxYnJ5MYZjAHXfZrzc=; b=gN1j4v1A3klpORspd15LrHndebbn3NZIBFHu3mKsJsmdRbQ3r7HhRq22yr9aqtlPKb uJWskK2Ck2lXCbCkuyae8iATt+sBx9TOTgPOp221RT5CAC0QSldQXyU1P7BM6J0N2df+ vd5X0lakC08E0wYtYCyjpZ+QI+eyayNAslXQM9FU0CUk5CCW94GuG+DbXWqtAO5Tt9hg IiOIsQI4vmQcZIJCzKzsUwfVSvDz9ze/7ogc65qyCu+oIkWVqGmkq/DS6OKmf5kOh6hg XicJKnVjBdT5xiI6I5M433NdiEZC+mM0KIPmO8mkIMizkQMZXbHtBeVlT9j1wpjE2KtD wQ1A== X-Gm-Message-State: ALyK8tIIMnGX5Ta/fDxrzNpxnQTTJlvd7TGezeSQ5Y+uSnICrU71u78nYfGMp7MPbPMTPg== X-Received: by 10.25.216.104 with SMTP id p101mr49053lfg.198.1465811137283; Mon, 13 Jun 2016 02:45:37 -0700 (PDT) Received: from localhost.localdomain ([193.0.108.42]) by smtp.gmail.com with ESMTPSA id e36sm2738753lji.31.2016.06.13.02.45.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 13 Jun 2016 02:45:36 -0700 (PDT) From: Paulina Szubarczyk To: xen-devel@lists.xenproject.org, roger.pau@citrix.com Date: Mon, 13 Jun 2016 11:43:56 +0200 Message-Id: <1465811036-17026-3-git-send-email-paulinaszubarczyk@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1465811036-17026-1-git-send-email-paulinaszubarczyk@gmail.com> References: <1465811036-17026-1-git-send-email-paulinaszubarczyk@gmail.com> Cc: sstabellini@kernel.org, wei.liu2@citrix.com, Paulina Szubarczyk , ian.jackson@eu.citrix.com, P.Gawkowski@ii.pw.edu.pl, dvrabel@cantab.net, anthony.perard@citrix.com Subject: [Xen-devel] [PATCH v2 2/2] qdisk - hw/block/xen_disk: grant copy implementation X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Copy data operated on during request from/to local buffers to/from the grant references. Before grant copy operation local buffers must be allocated what is done by calling ioreq_init_copy_buffers. For the 'read' operation, first, the qemu device invokes the read operation on local buffers and on the completion grant copy is called and buffers are freed. For the 'write' operation grant copy is performed before invoking write by qemu device. A new value 'feature_grant_copy' is added to recognize when the grant copy operation is supported by a guest. The body of the function 'ioreq_runio_qemu_aio' is moved to 'ioreq_runio_qemu_aio_blk' and in the 'ioreq_runio_qemu_aio' depending on the support for grant copy according checks, initialization, grant operation are made, then the 'ioreq_runio_qemu_aio_blk' function is called. Signed-off-by: Paulina Szubarczyk --- Changes since v1: - removed the 'ioreq_write','ioreq_read_init','ioreq_read' functions - implemented 'ioreq_init_copy_buffers', 'ioreq_copy' - reverted the removal of grant map and introduced conditional invoking grant copy or grant map - resigned from caching the local buffers on behalf of allocating the required amount of pages at once. The cached structure would require to have an lock guard and I suppose that the performance improvement would degraded. hw/block/xen_disk.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 163 insertions(+), 12 deletions(-) diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c index 37e14d1..af6b8c7 100644 --- a/hw/block/xen_disk.c +++ b/hw/block/xen_disk.c @@ -131,6 +131,9 @@ struct XenBlkDev { unsigned int persistent_gnt_count; unsigned int max_grants; + /* Grant copy */ + gboolean feature_grant_copy; + /* qemu block driver */ DriveInfo *dinfo; BlockBackend *blk; @@ -500,6 +503,100 @@ static int ioreq_map(struct ioreq *ioreq) return 0; } +static void* get_buffer(int count) +{ + return xc_memalign(xen_xc, XC_PAGE_SIZE, count*XC_PAGE_SIZE); +} + +static void free_buffers(struct ioreq *ioreq) +{ + int i; + + for (i = 0; i < ioreq->v.niov; i++) { + ioreq->page[i] = NULL; + } + + free(ioreq->pages); +} + +static int ioreq_init_copy_buffers(struct ioreq *ioreq) { + int i; + + if (ioreq->v.niov == 0) { + return 0; + } + + ioreq->pages = get_buffer(ioreq->v.niov); + if (!ioreq->pages) { + return -1; + } + + for (i = 0; i < ioreq->v.niov; i++) { + ioreq->page[i] = ioreq->pages + i*XC_PAGE_SIZE; + ioreq->v.iov[i].iov_base += (uintptr_t)ioreq->page[i]; + } + + return 0; +} + +static int ioreq_copy(struct ioreq *ioreq) +{ + XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev; + xc_gnttab_grant_copy_segment_t segs[BLKIF_MAX_SEGMENTS_PER_REQUEST]; + int i, count = 0, r, rc; + int64_t file_blk = ioreq->blkdev->file_blk; + + if (ioreq->v.niov == 0) { + r = 0; goto out; + } + + count = ioreq->v.niov; + + for (i = 0; i < count; i++) { + + xc_gnttab_grant_copy_ptr_t *from, *to; + + if (ioreq->req.operation == BLKIF_OP_READ) { + segs[i].flags = GNTCOPY_dest_gref; + from = &(segs[i].dest); + to = &(segs[i].source); + } else { + segs[i].flags = GNTCOPY_source_gref; + from = &(segs[i].source); + to = &(segs[i].dest); + } + segs[i].len = (ioreq->req.seg[i].last_sect + - ioreq->req.seg[i].first_sect + 1) * file_blk; + from->foreign.ref = ioreq->refs[i]; + from->foreign.domid = ioreq->domids[i]; + from->foreign.offset = ioreq->req.seg[i].first_sect * file_blk; + to->virt = ioreq->v.iov[i].iov_base; + } + + rc = xc_gnttab_grant_copy(gnt, count, segs); + + if (rc) { + xen_be_printf(&ioreq->blkdev->xendev, 0, + "failed to copy data %d \n", rc); + ioreq->aio_errors++; + r = -1; goto out; + } else { + r = 0; + } + + for (i = 0; i < count; i++) { + if (segs[i].status != GNTST_okay) { + xen_be_printf(&ioreq->blkdev->xendev, 0, + "failed to copy data %d for gref %d, domid %d\n", rc, + ioreq->refs[i], ioreq->domids[i]); + ioreq->aio_errors++; + r = -1; + } + } +out: + return r; +} + static int ioreq_runio_qemu_aio(struct ioreq *ioreq); static void qemu_aio_complete(void *opaque, int ret) @@ -521,6 +618,7 @@ static void qemu_aio_complete(void *opaque, int ret) if (ioreq->aio_inflight > 0) { return; } + if (ioreq->postsync) { ioreq->postsync = 0; ioreq->aio_inflight++; @@ -528,8 +626,32 @@ static void qemu_aio_complete(void *opaque, int ret) return; } + if (ioreq->blkdev->feature_grant_copy) { + switch (ioreq->req.operation) { + case BLKIF_OP_READ: + /* in case of failure ioreq->aio_errors is increased + * and it is logged */ + ioreq_copy(ioreq); + free_buffers(ioreq); + break; + case BLKIF_OP_WRITE: + case BLKIF_OP_FLUSH_DISKCACHE: + if (!ioreq->req.nr_segments) { + break; + } + free_buffers(ioreq); + break; + default: + break; + } + } + ioreq->status = ioreq->aio_errors ? BLKIF_RSP_ERROR : BLKIF_RSP_OKAY; - ioreq_unmap(ioreq); + + if (!ioreq->blkdev->feature_grant_copy) { + ioreq_unmap(ioreq); + } + ioreq_finish(ioreq); switch (ioreq->req.operation) { case BLKIF_OP_WRITE: @@ -547,14 +669,42 @@ static void qemu_aio_complete(void *opaque, int ret) qemu_bh_schedule(ioreq->blkdev->bh); } +static int ioreq_runio_qemu_aio_blk(struct ioreq *ioreq); + static int ioreq_runio_qemu_aio(struct ioreq *ioreq) { - struct XenBlkDev *blkdev = ioreq->blkdev; + if (ioreq->blkdev->feature_grant_copy) { + + ioreq_init_copy_buffers(ioreq); + if (ioreq->req.nr_segments && (ioreq->req.operation == BLKIF_OP_WRITE || + ioreq->req.operation == BLKIF_OP_FLUSH_DISKCACHE)) { + if (ioreq_copy(ioreq)) { + free_buffers(ioreq); + goto err; + } + } + if (ioreq_runio_qemu_aio_blk(ioreq)) goto err; - if (ioreq->req.nr_segments && ioreq_map(ioreq) == -1) { - goto err_no_map; + } else { + + if (ioreq->req.nr_segments && ioreq_map(ioreq)) goto err; + if (ioreq_runio_qemu_aio_blk(ioreq)) { + ioreq_unmap(ioreq); + goto err; + } } + return 0; +err: + ioreq_finish(ioreq); + ioreq->status = BLKIF_RSP_ERROR; + return -1; +} + +static int ioreq_runio_qemu_aio_blk(struct ioreq *ioreq) +{ + struct XenBlkDev *blkdev = ioreq->blkdev; + ioreq->aio_inflight++; if (ioreq->presync) { blk_aio_flush(ioreq->blkdev->blk, qemu_aio_complete, ioreq); @@ -594,19 +744,12 @@ static int ioreq_runio_qemu_aio(struct ioreq *ioreq) } default: /* unknown operation (shouldn't happen -- parse catches this) */ - goto err; + return -1; } qemu_aio_complete(ioreq, 0); return 0; - -err: - ioreq_unmap(ioreq); -err_no_map: - ioreq_finish(ioreq); - ioreq->status = BLKIF_RSP_ERROR; - return -1; } static int blk_send_response_one(struct ioreq *ioreq) @@ -1020,10 +1163,18 @@ static int blk_connect(struct XenDevice *xendev) xen_be_bind_evtchn(&blkdev->xendev); + xc_gnttab_grant_copy_segment_t seg; + blkdev->feature_grant_copy = + (xc_gnttab_grant_copy(blkdev->xendev.gnttabdev, 0, &seg) == 0); + + xen_be_printf(&blkdev->xendev, 3, "GRANT COPY %s\n", + blkdev->feature_grant_copy ? "ENABLED" : "DISABLED"); + xen_be_printf(&blkdev->xendev, 1, "ok: proto %s, ring-ref %d, " "remote port %d, local port %d\n", blkdev->xendev.protocol, blkdev->ring_ref, blkdev->xendev.remote_port, blkdev->xendev.local_port); + return 0; }