From patchwork Tue Nov 22 08:13:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Klaus Jensen X-Patchwork-Id: 1707682 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=irrelevant.dk header.i=@irrelevant.dk header.a=rsa-sha256 header.s=fm3 header.b=P740Y1+c; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm1 header.b=izvod/za; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NGcY12Fpbz23nk for ; Tue, 22 Nov 2022 19:16:37 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oxOQ2-0002Um-T8; Tue, 22 Nov 2022 03:14:14 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oxOPw-0002Ro-Kx; Tue, 22 Nov 2022 03:14:08 -0500 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oxOPq-00032D-7O; Tue, 22 Nov 2022 03:14:08 -0500 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 6385E320024A; Tue, 22 Nov 2022 03:13:58 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Tue, 22 Nov 2022 03:13:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irrelevant.dk; h=cc:cc:content-transfer-encoding:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1669104837; x= 1669191237; bh=DlHke+ZGmp4Kp0kN5AHVCi+UWrUQwSr8Y4wjV05omHQ=; b=P 740Y1+co/BnaHNOXuurLcSe8n+eYlrmCwKaB+Um18SnuSH9WKQ0xl1i7QTUN6n7Q 35XLwcAmMmx0Tm4GEr7JeHPN+Vorx9L5mWaMX6l0shyeGtDeKB/cfaz1v8DS+5gx HsdQ1UtrO5NIQTEXYsahL2tsTU/K47Mlj79o3hh6QOtThxs9xywnJcmJvD2scgLW Ojc+/TlsECDAt+wWW9KdqO8J7ZDomRZ8ZUv1YGkMaLXShVv1h+RAjZkZqyapp9io 80BrqOlr0miAw4KGGVffE+N1HdW740kCCD3phSoGhdgeWyVIe8pi952Hzi/C5+pV knRPEwQ1LtdzY23nD+RxA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; t=1669104837; x=1669191237; bh=DlHke+ZGmp4Kp 0kN5AHVCi+UWrUQwSr8Y4wjV05omHQ=; b=izvod/zasqJn/wee9mnJ4202GCg9q STW9prEY1KXX/czfsFm3oO+EsGCaexyoZhHJcUvUGZ53l7LY6UEL1mXSSkXc5gZO PSWxQl/8sGyRqhF5bH3vTNB19zsqxq5EZXaqFtoYcKe80MMt8fOmyyxgbJ/gBbXF Pm3O+Q+513y8E+5FgcKSX6nsGry7lfNbhlt32x82B/SJPZaRt0dWj3os/npwzQ5H JsltYF7cdeMmPCUz/MZU7XcIvrEkRg961hhNksezyBoadcI2z1I+PNXU4QBrVW4J oSX43IUzicXddU+EG14TmOfCGnuCipojeoI36Bg++tp/tZvQ/NEoKuNmA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvgedrheejgdduudelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepmfhlrghu shculfgvnhhsvghnuceoihhtshesihhrrhgvlhgvvhgrnhhtrdgukheqnecuggftrfgrth htvghrnhepjefgieelgfeiveehkeeuveehheekfeevgeeigfehfefgjeejhefffeegudej udegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepih htshesihhrrhgvlhgvvhgrnhhtrdgukh X-ME-Proxy: Feedback-ID: idc91472f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 22 Nov 2022 03:13:56 -0500 (EST) From: Klaus Jensen To: qemu-devel@nongnu.org Cc: Keith Busch , Klaus Jensen , qemu-block@nongnu.org, Klaus Jensen Subject: [PATCH for-7.2 2/5] hw/nvme: fix aio cancel in flush Date: Tue, 22 Nov 2022 09:13:45 +0100 Message-Id: <20221122081348.49963-3-its@irrelevant.dk> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221122081348.49963-1-its@irrelevant.dk> References: <20221122081348.49963-1-its@irrelevant.dk> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2550; i=k.jensen@samsung.com; h=from:subject; bh=z21NZQE0tPSmiRSuuRtkPPRRr3BOvzupV5f9QANkoS4=; b=owJ4nAFtAZL+kA0DAAoBTeGvMW1PDekByyZiAGN8hLvNlduUWPQEm7WVuKb5rIL2pNL5WRuLp5Ff Dt+qBGbnIYkBMwQAAQoAHRYhBFIoM6p14tzmokdmwE3hrzFtTw3pBQJjfIS7AAoJEE3hrzFtTw3p15 gH/RTvZ/ex1DzYef2+9OfFWL0lB826lKQZzzi8pAA1wfhxFSTH6pL6Sto+Pz/Sn7PuMeWLon2ldEG1 guem1kXQifS77PeSrkRCp9xfLjRiw+nljDbI/eC14jz/M3ViODHqM/H+4idWGs32+BIKn5PZ7gBw5M 8twpQIC8Cb4vdbxzqGdcM+XVGlrZVsSFsm5kAcLyFT18THBTfGsWy+7ozlEe4aa8IOX5sfp1iLIwxE a0bIp2U0HQkY9ulDvM1U3K/q4sAyLPjaEa8br/kNwpPStgac5BrqzAO4hEY+ss/mwNbQHPrx10GF61 Ta9RETIfJW1lZb+KoXLcEEYJ4SBnWbB7rY+7gm X-Developer-Key: i=k.jensen@samsung.com; a=openpgp; fpr=DDCA4D9C9EF931CC3468427263D56FC5E55DA838 Received-SPF: pass client-ip=64.147.123.19; envelope-from=its@irrelevant.dk; helo=wout3-smtp.messagingengine.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 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 From: Klaus Jensen Make sure that iocb->aiocb is NULL'ed when cancelling. Fix a potential use-after-free by removing the bottom half and enqueuing the completion directly. Fixes: 38f4ac65ac88 ("hw/nvme: reimplement flush to allow cancellation") Signed-off-by: Klaus Jensen --- hw/nvme/ctrl.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c index 26b53469328f..fc129b8d1a93 100644 --- a/hw/nvme/ctrl.c +++ b/hw/nvme/ctrl.c @@ -3160,7 +3160,6 @@ typedef struct NvmeFlushAIOCB { BlockAIOCB common; BlockAIOCB *aiocb; NvmeRequest *req; - QEMUBH *bh; int ret; NvmeNamespace *ns; @@ -3176,6 +3175,7 @@ static void nvme_flush_cancel(BlockAIOCB *acb) if (iocb->aiocb) { blk_aio_cancel_async(iocb->aiocb); + iocb->aiocb = NULL; } } @@ -3185,6 +3185,8 @@ static const AIOCBInfo nvme_flush_aiocb_info = { .get_aio_context = nvme_get_aio_context, }; +static void nvme_do_flush(NvmeFlushAIOCB *iocb); + static void nvme_flush_ns_cb(void *opaque, int ret) { NvmeFlushAIOCB *iocb = opaque; @@ -3206,13 +3208,11 @@ static void nvme_flush_ns_cb(void *opaque, int ret) } out: - iocb->aiocb = NULL; - qemu_bh_schedule(iocb->bh); + nvme_do_flush(iocb); } -static void nvme_flush_bh(void *opaque) +static void nvme_do_flush(NvmeFlushAIOCB *iocb) { - NvmeFlushAIOCB *iocb = opaque; NvmeRequest *req = iocb->req; NvmeCtrl *n = nvme_ctrl(req); int i; @@ -3239,14 +3239,8 @@ static void nvme_flush_bh(void *opaque) return; done: - qemu_bh_delete(iocb->bh); - iocb->bh = NULL; - iocb->common.cb(iocb->common.opaque, iocb->ret); - qemu_aio_unref(iocb); - - return; } static uint16_t nvme_flush(NvmeCtrl *n, NvmeRequest *req) @@ -3258,7 +3252,6 @@ static uint16_t nvme_flush(NvmeCtrl *n, NvmeRequest *req) iocb = qemu_aio_get(&nvme_flush_aiocb_info, NULL, nvme_misc_cb, req); iocb->req = req; - iocb->bh = qemu_bh_new(nvme_flush_bh, iocb); iocb->ret = 0; iocb->ns = NULL; iocb->nsid = 0; @@ -3280,13 +3273,11 @@ static uint16_t nvme_flush(NvmeCtrl *n, NvmeRequest *req) } req->aiocb = &iocb->common; - qemu_bh_schedule(iocb->bh); + nvme_do_flush(iocb); return NVME_NO_COMPLETE; out: - qemu_bh_delete(iocb->bh); - iocb->bh = NULL; qemu_aio_unref(iocb); return status;