From nobody Fri Nov 14 20:43:46 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1589908649; cv=none; d=zohomail.com; s=zohoarc; b=C02V/NIfv1pDEqA10rf4xpyWc0TEdG2efQahnKlC3evQz6/dn6lSGzfSJA0dCGgTSVDxlY1E8Etyca9gkywIwOd4qCeTeKaSd+/jEJ5m1MptX8/3B+PR5eFOt39KWm1F5jaYNpPU7sC8zrRwNXL0smG1EvKrsFxrs9BHdpGT5GA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1589908649; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=8GcfOHK0z+CHgyGSWamqDzJ/eK8uFIiE2Aef6qeLdEk=; b=BxijXDebK/Kyq+Rlf9XR/vKoU6dFVJDoMGr4ZvezaAvXyZhGydd57TtuEOeB2wddfne3C6liZmtEFFE9S7ldKLGSzZjr0Ff/6uQFFzhQPFXhr0kvDPgPSX1Plt5wdm7uqA77lE0aNaXenbNxSMfEa2yE2o+MAW79VrxBoiYNX4M= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1589908649016537.0999638046852; Tue, 19 May 2020 10:17:29 -0700 (PDT) Received: from localhost ([::1]:57872 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jb5rq-00020Y-DD for importer@patchew.org; Tue, 19 May 2020 13:17:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34354) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jb5mn-0003Jc-KI for qemu-devel@nongnu.org; Tue, 19 May 2020 13:12:13 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:27222 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jb5mm-0002eK-Bf for qemu-devel@nongnu.org; Tue, 19 May 2020 13:12:13 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-305-IuwUGN--P_2c15tnn_p_pg-1; Tue, 19 May 2020 13:12:09 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C12C218FF660; Tue, 19 May 2020 17:12:08 +0000 (UTC) Received: from localhost (ovpn-114-215.ams2.redhat.com [10.36.114.215]) by smtp.corp.redhat.com (Postfix) with ESMTP id A4F6D10013D9; Tue, 19 May 2020 17:12:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1589908331; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8GcfOHK0z+CHgyGSWamqDzJ/eK8uFIiE2Aef6qeLdEk=; b=Ro+v/m6GKbYDryuQ6loRoqotnTVmKIJsG3HYRmqQjIQk4mTCH9jyXnCCrOi40yzh0laK17 W3CsT3vNyf5wYR1lg0RAjniYVa9X+OWGC8Xc5e6hHAyTMCSs9/gM5012QPiGIyz6OSS2MK WDVd39LcQ3gjBW6XiRe0mHTi7S30cnI= X-MC-Unique: IuwUGN--P_2c15tnn_p_pg-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 7/7] block/nvme: support nested aio_poll() Date: Tue, 19 May 2020 18:11:38 +0100 Message-Id: <20200519171138.201667-8-stefanha@redhat.com> In-Reply-To: <20200519171138.201667-1-stefanha@redhat.com> References: <20200519171138.201667-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/19 00:34:39 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -3 X-Spam_score: -0.4 X-Spam_bar: / X-Spam_report: (-0.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" QEMU block drivers are supposed to support aio_poll() from I/O completion callback functions. This means completion processing must be re-entrant. The standard approach is to schedule a BH during completion processing and cancel it at the end of processing. If aio_poll() is invoked by a callback function then the BH will run. The BH continues the suspended completion processing. All of this means that request A's cb() can synchronously wait for request B to complete. Previously the nvme block driver would hang because it didn't process completions from nested aio_poll(). Signed-off-by: Stefan Hajnoczi Reviewed-by: Sergio Lopez --- block/nvme.c | 67 ++++++++++++++++++++++++++++++++++++++++------ block/trace-events | 2 +- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/block/nvme.c b/block/nvme.c index 5b2f6e9ffc..ce908235ae 100644 --- a/block/nvme.c +++ b/block/nvme.c @@ -74,9 +74,11 @@ typedef struct { int cq_phase; int free_req_head; NVMeRequest reqs[NVME_NUM_REQS]; - bool busy; int need_kick; int inflight; + + /* Thread-safe, no lock necessary */ + QEMUBH *completion_bh; } NVMeQueuePair; =20 /* Memory mapped registers */ @@ -140,6 +142,8 @@ struct BDRVNVMeState { #define NVME_BLOCK_OPT_DEVICE "device" #define NVME_BLOCK_OPT_NAMESPACE "namespace" =20 +static void nvme_process_completion_bh(void *opaque); + static QemuOptsList runtime_opts =3D { .name =3D "nvme", .head =3D QTAILQ_HEAD_INITIALIZER(runtime_opts.head), @@ -181,6 +185,9 @@ static void nvme_init_queue(BlockDriverState *bs, NVMeQ= ueue *q, =20 static void nvme_free_queue_pair(NVMeQueuePair *q) { + if (q->completion_bh) { + qemu_bh_delete(q->completion_bh); + } qemu_vfree(q->prp_list_pages); qemu_vfree(q->sq.queue); qemu_vfree(q->cq.queue); @@ -214,6 +221,8 @@ static NVMeQueuePair *nvme_create_queue_pair(BlockDrive= rState *bs, q->index =3D idx; qemu_co_queue_init(&q->free_req_queue); q->prp_list_pages =3D qemu_blockalign0(bs, s->page_size * NVME_NUM_REQ= S); + q->completion_bh =3D aio_bh_new(bdrv_get_aio_context(bs), + nvme_process_completion_bh, q); r =3D qemu_vfio_dma_map(s->vfio, q->prp_list_pages, s->page_size * NVME_NUM_REQS, false, &prp_list_iova); @@ -352,11 +361,21 @@ static bool nvme_process_completion(NVMeQueuePair *q) NvmeCqe *c; =20 trace_nvme_process_completion(s, q->index, q->inflight); - if (q->busy || s->plugged) { - trace_nvme_process_completion_queue_busy(s, q->index); + if (s->plugged) { + trace_nvme_process_completion_queue_plugged(s, q->index); return false; } - q->busy =3D true; + + /* + * Support re-entrancy when a request cb() function invokes aio_poll(). + * Pending completions must be visible to aio_poll() so that a cb() + * function can wait for the completion of another request. + * + * The aio_poll() loop will execute our BH and we'll resume completion + * processing there. + */ + qemu_bh_schedule(q->completion_bh); + assert(q->inflight >=3D 0); while (q->inflight) { int ret; @@ -384,10 +403,10 @@ static bool nvme_process_completion(NVMeQueuePair *q) assert(req.cb); nvme_put_free_req_locked(q, preq); preq->cb =3D preq->opaque =3D NULL; - qemu_mutex_unlock(&q->lock); - req.cb(req.opaque, ret); - qemu_mutex_lock(&q->lock); q->inflight--; + qemu_mutex_unlock(&q->lock); + req.cb(req.opaque, ret); + qemu_mutex_lock(&q->lock); progress =3D true; } if (progress) { @@ -396,10 +415,28 @@ static bool nvme_process_completion(NVMeQueuePair *q) *q->cq.doorbell =3D cpu_to_le32(q->cq.head); nvme_wake_free_req_locked(q); } - q->busy =3D false; + + qemu_bh_cancel(q->completion_bh); + return progress; } =20 +static void nvme_process_completion_bh(void *opaque) +{ + NVMeQueuePair *q =3D opaque; + + /* + * We're being invoked because a nvme_process_completion() cb() functi= on + * called aio_poll(). The callback may be waiting for further completi= ons + * so notify the device that it has space to fill in more completions = now. + */ + smp_mb_release(); + *q->cq.doorbell =3D cpu_to_le32(q->cq.head); + nvme_wake_free_req_locked(q); + + nvme_process_completion(q); +} + static void nvme_trace_command(const NvmeCmd *cmd) { int i; @@ -1309,6 +1346,13 @@ static void nvme_detach_aio_context(BlockDriverState= *bs) { BDRVNVMeState *s =3D bs->opaque; =20 + for (int i =3D 0; i < s->nr_queues; i++) { + NVMeQueuePair *q =3D s->queues[i]; + + qemu_bh_delete(q->completion_bh); + q->completion_bh =3D NULL; + } + aio_set_event_notifier(bdrv_get_aio_context(bs), &s->irq_notifier, false, NULL, NULL); } @@ -1321,6 +1365,13 @@ static void nvme_attach_aio_context(BlockDriverState= *bs, s->aio_context =3D new_context; aio_set_event_notifier(new_context, &s->irq_notifier, false, nvme_handle_event, nvme_poll_cb); + + for (int i =3D 0; i < s->nr_queues; i++) { + NVMeQueuePair *q =3D s->queues[i]; + + q->completion_bh =3D + aio_bh_new(new_context, nvme_process_completion_bh, q); + } } =20 static void nvme_aio_plug(BlockDriverState *bs) diff --git a/block/trace-events b/block/trace-events index 29dff8881c..dbe76a7613 100644 --- a/block/trace-events +++ b/block/trace-events @@ -158,7 +158,7 @@ nvme_kick(void *s, int queue) "s %p queue %d" nvme_dma_flush_queue_wait(void *s) "s %p" nvme_error(int cmd_specific, int sq_head, int sqid, int cid, int status) "= cmd_specific %d sq_head %d sqid %d cid %d status 0x%x" nvme_process_completion(void *s, int index, int inflight) "s %p queue %d i= nflight %d" -nvme_process_completion_queue_busy(void *s, int index) "s %p queue %d" +nvme_process_completion_queue_plugged(void *s, int index) "s %p queue %d" nvme_complete_command(void *s, int index, int cid) "s %p queue %d cid %d" nvme_submit_command(void *s, int index, int cid) "s %p queue %d cid %d" nvme_submit_command_raw(int c0, int c1, int c2, int c3, int c4, int c5, in= t c6, int c7) "%02x %02x %02x %02x %02x %02x %02x %02x" --=20 2.25.3