From nobody Wed Nov 27 11:54:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1699270317; cv=none; d=zohomail.com; s=zohoarc; b=PbEWVM656eWwY8i+rPsZj/1ALbEENRgaxqZY+kwHVkz1vWKJkdhe35yeepHwdhc4ir1hjocarpGYVUqZOxCdBG2G4CsA+ypKYEPsxg/DyBGFSsAgecCNpYXgmigpgO6CbtRu0pM9bIPoQuNIFZhgxGgxY6KZIkqiU14ScWJ6wpc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699270317; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=l0/vWnFHt+cyNHMvzu2dssx7xYyn6IEpyeIo7MQjzWQ=; b=TyYnAPeYV6/g3jrHygOTpVQNaMF1nrlkddu/Kdl72nCKEs3HynVzFw99MfBOxrSkbKBS6D9OlugyO9PtgkkdzLmFUBbxfYE3jTddSfbW8BKIbVgjjjAyUxNlffLn2iuhRHC08uiQdTxVnj07CAtbria/jL0EXHoGKcDJ9MajD40= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1699270317795445.043520390318; Mon, 6 Nov 2023 03:31:57 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qzxTe-0007Yr-It; Mon, 06 Nov 2023 06:09:06 -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 1qzxTd-0007Wq-2y for qemu-devel@nongnu.org; Mon, 06 Nov 2023 06:09:05 -0500 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qzxTZ-0005JB-Rm for qemu-devel@nongnu.org; Mon, 06 Nov 2023 06:09:04 -0500 Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-32d849cc152so3001776f8f.1 for ; Mon, 06 Nov 2023 03:09:01 -0800 (PST) Received: from m1x-phil.lan (176-131-220-199.abo.bbox.fr. [176.131.220.199]) by smtp.gmail.com with ESMTPSA id z18-20020adfec92000000b0032da8fb0d05sm9132494wrn.110.2023.11.06.03.08.58 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 06 Nov 2023 03:08:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1699268940; x=1699873740; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=l0/vWnFHt+cyNHMvzu2dssx7xYyn6IEpyeIo7MQjzWQ=; b=ATOHgHNsCkKb89JaOnwl8QH72EF8YZw+TFI1xlV8vgO5T92YyWphdlcIsoMHqk8+TU Vc8e/AXE44m4xrOGH8R0XBa4X29NorWbvjte/FwV44YiBOH26UxPjIl3yr+E8fvbhl5T 4TdP6raxusiQSOxyVH4A+XuZgoMWD2bqPV0o0W1KTCAn/bhZX7v6O9rXyB7xkjFlDRt8 iMP5QdazlChzlSDHosx9ur1yL3CVSXshPF/tPiFjLneC3UAax7BGhijhlzeiPx/w8Vmy TtAF6/94PMk1ZnD63cDtm9CLAtAB6p6QaUHEBgmZifi//6pe/lMdnWN0AnO7gJoCMLJi Bhyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699268940; x=1699873740; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=l0/vWnFHt+cyNHMvzu2dssx7xYyn6IEpyeIo7MQjzWQ=; b=UTqFyT71i4IJofVvsltHWMsi4PCYPele0UkW8bi8VmR9hkA4k/5pzYIQND/qLSfJQ7 rlYOZEGqsrnM+MUsyrND5aE8UxIAwyA4ZoizGmG4v+Zoc6MHCABZOPIDG28VqnEzjMW7 EYjTW/KwX6AG8Fmdu9mSzuRlWXe6ByFn0rw+gTBQDbTkDaIvQ+U3Xr2PHO71loY4phjL UMKuqBrKRp5mpM24gXUqSuzLth36mUAJLcA1Jq0yBuNUiDS8fJkK/11VBj9r2Ff6SWno vMoLrbSgwe7tYkol1NQ+YXsksGnaJg3KFftYJgiwiVsQ0FCPJ67iHiTXXOMRTNs19eZg Uxkw== X-Gm-Message-State: AOJu0YwLGSbqsl0NSZiAeKpTitj7W6WyJ0qMdPj16dwGcQToJuqMsUd5 c0cnry6cNbzNjA2vQHgd00MPIeEBJgIfhQtBG2A= X-Google-Smtp-Source: AGHT+IFsbitm3bJThRshzHVNkwv0m7bE7yeAZoRxHUIurMBc1Abgxn0QnxImhW8tuDAE+j3RuaAyCQ== X-Received: by 2002:a05:6000:1361:b0:32d:9b80:e2c6 with SMTP id q1-20020a056000136100b0032d9b80e2c6mr22417154wrz.26.1699268940275; Mon, 06 Nov 2023 03:09:00 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: kvm@vger.kernel.org, qemu-s390x@nongnu.org, qemu-block@nongnu.org, qemu-riscv@nongnu.org, qemu-ppc@nongnu.org, qemu-arm@nongnu.org, Fiona Ebner , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , simon.rowe@nutanix.com, John Snow Subject: [PULL 47/60] hw/ide: reset: cancel async DMA operation before resetting state Date: Mon, 6 Nov 2023 12:03:19 +0100 Message-ID: <20231106110336.358-48-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231106110336.358-1-philmd@linaro.org> References: <20231106110336.358-1-philmd@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=2a00:1450:4864:20::42e; envelope-from=philmd@linaro.org; helo=mail-wr1-x42e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1699270319712100007 From: Fiona Ebner If there is a pending DMA operation during ide_bus_reset(), the fact that the IDEState is already reset before the operation is canceled can be problematic. In particular, ide_dma_cb() might be called and then use the reset IDEState which contains the signature after the reset. When used to construct the IO operation this leads to ide_get_sector() returning 0 and nsector being 1. This is particularly bad, because a write command will thus destroy the first sector which often contains a partition table or similar. Traces showing the unsolicited write happening with IDEState 0x5595af6949d0 being used after reset: > ahci_port_write ahci(0x5595af6923f0)[0]: port write [reg:PxSCTL] @ 0x2c: = 0x00000300 > ahci_reset_port ahci(0x5595af6923f0)[0]: reset port > ide_reset IDEstate 0x5595af6949d0 > ide_reset IDEstate 0x5595af694da8 > ide_bus_reset_aio aio_cancel > dma_aio_cancel dbs=3D0x7f64600089a0 > dma_blk_cb dbs=3D0x7f64600089a0 ret=3D0 > dma_complete dbs=3D0x7f64600089a0 ret=3D0 cb=3D0x5595acd40b30 > ahci_populate_sglist ahci(0x5595af6923f0)[0] > ahci_dma_prepare_buf ahci(0x5595af6923f0)[0]: prepare buf limit=3D512 pre= pared=3D512 > ide_dma_cb IDEState 0x5595af6949d0; sector_num=3D0 n=3D1 cmd=3DDMA WRITE > dma_blk_io dbs=3D0x7f6420802010 bs=3D0x5595ae2c6c30 offset=3D0 to_dev=3D1 > dma_blk_cb dbs=3D0x7f6420802010 ret=3D0 > (gdb) p *qiov > $11 =3D {iov =3D 0x7f647c76d840, niov =3D 1, {{nalloc =3D 1, local_iov = =3D {iov_base =3D 0x0, > iov_len =3D 512}}, {__pad =3D "\001\000\000\000\000\000\000\000\000= \000\000", > size =3D 512}}} > (gdb) bt > #0 blk_aio_pwritev (blk=3D0x5595ae2c6c30, offset=3D0, qiov=3D0x7f6420802= 070, flags=3D0, > cb=3D0x5595ace6f0b0 , opaque=3D0x7f6420802010) > at ../block/block-backend.c:1682 > #1 0x00005595ace6f185 in dma_blk_cb (opaque=3D0x7f6420802010, ret=3D) > at ../softmmu/dma-helpers.c:179 > #2 0x00005595ace6f778 in dma_blk_io (ctx=3D0x5595ae0609f0, > sg=3Dsg@entry=3D0x5595af694d00, offset=3Doffset@entry=3D0, align=3Dal= ign@entry=3D512, > io_func=3Dio_func@entry=3D0x5595ace6ee30 , > io_func_opaque=3Dio_func_opaque@entry=3D0x5595ae2c6c30, > cb=3D0x5595acd40b30 , opaque=3D0x5595af6949d0, > dir=3DDMA_DIRECTION_TO_DEVICE) at ../softmmu/dma-helpers.c:244 > #3 0x00005595ace6f90a in dma_blk_write (blk=3D0x5595ae2c6c30, > sg=3Dsg@entry=3D0x5595af694d00, offset=3Doffset@entry=3D0, align=3Dal= ign@entry=3D512, > cb=3Dcb@entry=3D0x5595acd40b30 , opaque=3Dopaque@entry=3D= 0x5595af6949d0) > at ../softmmu/dma-helpers.c:280 > #4 0x00005595acd40e18 in ide_dma_cb (opaque=3D0x5595af6949d0, ret=3D) > at ../hw/ide/core.c:953 > #5 0x00005595ace6f319 in dma_complete (ret=3D0, dbs=3D0x7f64600089a0) > at ../softmmu/dma-helpers.c:107 > #6 dma_blk_cb (opaque=3D0x7f64600089a0, ret=3D0) at ../softmmu/dma-helpe= rs.c:127 > #7 0x00005595ad12227d in blk_aio_complete (acb=3D0x7f6460005b10) > at ../block/block-backend.c:1527 > #8 blk_aio_complete (acb=3D0x7f6460005b10) at ../block/block-backend.c:1= 524 > #9 blk_aio_write_entry (opaque=3D0x7f6460005b10) at ../block/block-backe= nd.c:1594 > #10 0x00005595ad258cfb in coroutine_trampoline (i0=3D, > i1=3D) at ../util/coroutine-ucontext.c:177 Signed-off-by: Fiona Ebner Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: simon.rowe@nutanix.com Message-ID: <20230906130922.142845-1-f.ebner@proxmox.com> Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- hw/ide/core.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/hw/ide/core.c b/hw/ide/core.c index b5e0dcd29b..63ba665f3d 100644 --- a/hw/ide/core.c +++ b/hw/ide/core.c @@ -2515,19 +2515,19 @@ static void ide_dummy_transfer_stop(IDEState *s) =20 void ide_bus_reset(IDEBus *bus) { - bus->unit =3D 0; - bus->cmd =3D 0; - ide_reset(&bus->ifs[0]); - ide_reset(&bus->ifs[1]); - ide_clear_hob(bus); - - /* pending async DMA */ + /* pending async DMA - needs the IDEState before it is reset */ if (bus->dma->aiocb) { trace_ide_bus_reset_aio(); blk_aio_cancel(bus->dma->aiocb); bus->dma->aiocb =3D NULL; } =20 + bus->unit =3D 0; + bus->cmd =3D 0; + ide_reset(&bus->ifs[0]); + ide_reset(&bus->ifs[1]); + ide_clear_hob(bus); + /* reset dma provider too */ if (bus->dma->ops->reset) { bus->dma->ops->reset(bus->dma); --=20 2.41.0