From patchwork Mon Jan 14 15:26:57 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 211812 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id DD6F32C0098 for ; Tue, 15 Jan 2013 02:44:23 +1100 (EST) Received: from localhost ([::1]:58548 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TulxQ-0006Tm-OR for incoming@patchwork.ozlabs.org; Mon, 14 Jan 2013 10:28:16 -0500 Received: from eggs.gnu.org ([208.118.235.92]:32960) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tulwb-0004IN-Cu for qemu-devel@nongnu.org; Mon, 14 Jan 2013 10:27:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TulwU-0002nw-Ln for qemu-devel@nongnu.org; Mon, 14 Jan 2013 10:27:25 -0500 Received: from mail-qa0-f45.google.com ([209.85.216.45]:43341) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TulwU-0002nn-I2 for qemu-devel@nongnu.org; Mon, 14 Jan 2013 10:27:18 -0500 Received: by mail-qa0-f45.google.com with SMTP id j15so1525872qaq.11 for ; Mon, 14 Jan 2013 07:27:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=EtmOMy2FeIJtiAleiRWaIKfoNTcgogzYcX7LjKNTPnk=; b=ALGUFuVPZrd2KcdHBCfEAoGVV2emtnHP9vXVNFLWprlXkjr/KUPuaW+3gbbaOFCWMt UUOumUq3UycJAT7cWLzSl+50uyfwgiSS8UdqIssOg/pm/7fydlXGiVnVqH51AHp1FSiz 4e/jvS8xvSazQ69NffxpTjODooqNzeFhpBXfFb/fpXwZhxyiUFAQLF5QfErjwBk7G7jT 0V4J7+i6SdSYnyyNCF+P7/P/r5tTvCLy/iiZztLJ1lphfMwc68MDpvKYhQTs1TLDP+0m 9HvaCz26bnPR5HWrSUcqKJ8fHigj4gYPTO2iJIz+wxr3bMqq9eExY66oZifynGIJBwj1 BTFQ== X-Received: by 10.49.82.75 with SMTP id g11mr84998378qey.12.1358177238179; Mon, 14 Jan 2013 07:27:18 -0800 (PST) Received: from yakj.usersys.redhat.com (93-34-179-137.ip50.fastwebnet.it. [93.34.179.137]) by mx.google.com with ESMTPS id t2sm6157728qav.1.2013.01.14.07.27.15 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Mon, 14 Jan 2013 07:27:17 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 14 Jan 2013 16:26:57 +0100 Message-Id: <1358177218-16802-7-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.1 In-Reply-To: <1358177218-16802-1-git-send-email-pbonzini@redhat.com> References: <1358177218-16802-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.216.45 Cc: kwolf@redhat.com, stefanha@redhat.com Subject: [Qemu-devel] [PATCH 6/7] ide: issue discard asynchronously but serialize the pieces X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Now that discard can take a long time, make it asynchronous. Each LBA range entry is processed separately because discard can be an expensive operation. Signed-off-by: Paolo Bonzini --- hw/ide/core.c | 79 ++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 25 deletions(-) diff --git a/hw/ide/core.c b/hw/ide/core.c index cb77dfc..14ad079 100644 --- a/hw/ide/core.c +++ b/hw/ide/core.c @@ -325,14 +325,26 @@ typedef struct TrimAIOCB { BlockDriverAIOCB common; QEMUBH *bh; int ret; + QEMUIOVector *qiov; + BlockDriverAIOCB *aiocb; + int i, j; } TrimAIOCB; static void trim_aio_cancel(BlockDriverAIOCB *acb) { TrimAIOCB *iocb = container_of(acb, TrimAIOCB, common); + /* Exit the loop in case bdrv_aio_cancel calls ide_issue_trim_cb again. */ + iocb->j = iocb->qiov->niov - 1; + iocb->i = (iocb->qiov->iov[iocb->j].iov_len / 8) - 1; + + /* Tell ide_issue_trim_cb not to trigger the completion, too. */ qemu_bh_delete(iocb->bh); iocb->bh = NULL; + + if (iocb->aiocb) { + bdrv_aio_cancel(iocb->aiocb); + } qemu_aio_release(iocb); } @@ -349,43 +361,60 @@ static void ide_trim_bh_cb(void *opaque) qemu_bh_delete(iocb->bh); iocb->bh = NULL; - qemu_aio_release(iocb); } +static void ide_issue_trim_cb(void *opaque, int ret) +{ + TrimAIOCB *iocb = opaque; + if (ret >= 0) { + while (iocb->j < iocb->qiov->niov) { + int j = iocb->j; + while (++iocb->i < iocb->qiov->iov[j].iov_len / 8) { + int i = iocb->i; + uint64_t *buffer = iocb->qiov->iov[j].iov_base; + + /* 6-byte LBA + 2-byte range per entry */ + uint64_t entry = le64_to_cpu(buffer[i]); + uint64_t sector = entry & 0x0000ffffffffffffULL; + uint16_t count = entry >> 48; + + if (count == 0) { + continue; + } + + /* Got an entry! Submit and exit. */ + iocb->aiocb = bdrv_aio_discard(iocb->common.bs, sector, count, + ide_issue_trim_cb, opaque); + return; + } + + iocb->j++; + iocb->i = -1; + } + } else { + iocb->ret = ret; + } + + iocb->aiocb = NULL; + if (iocb->bh) { + qemu_bh_schedule(iocb->bh); + } +} + BlockDriverAIOCB *ide_issue_trim(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque) { TrimAIOCB *iocb; - int i, j, ret; iocb = qemu_aio_get(&trim_aiocb_info, bs, cb, opaque); iocb->bh = qemu_bh_new(ide_trim_bh_cb, iocb); iocb->ret = 0; - - for (j = 0; j < qiov->niov; j++) { - uint64_t *buffer = qiov->iov[j].iov_base; - - for (i = 0; i < qiov->iov[j].iov_len / 8; i++) { - /* 6-byte LBA + 2-byte range per entry */ - uint64_t entry = le64_to_cpu(buffer[i]); - uint64_t sector = entry & 0x0000ffffffffffffULL; - uint16_t count = entry >> 48; - - if (count == 0) { - continue; - } - - ret = bdrv_discard(bs, sector, count); - if (!iocb->ret) { - iocb->ret = ret; - } - } - } - - qemu_bh_schedule(iocb->bh); - + iocb->qiov = qiov; + iocb->i = -1; + iocb->j = 0; + ide_issue_trim_cb(iocb, 0); return &iocb->common; }