From nobody Thu Oct 9 06:31:51 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=amsat.org ARC-Seal: i=1; a=rsa-sha256; t=1595006436; cv=none; d=zohomail.com; s=zohoarc; b=SYiMihCWwCnIKUNfrFR7j3GVjphKBHEeb0xNL8k2UELieSEiR6Ol9U51715AFXbWJTbdoONbvnVlsVcZEC/bnmH8j2N6PKQOc6zqNPTmvDXWgJq3HDUbMceuUWMCjKDOgkNQqC/BVyqeiBkN5SEjDdRxyrdGA7F3yzSxQwo7xE4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595006436; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=d18oQfmzx1Zi6yKmQ2ydWRnM5m1FMy+yxBjSGhi73ik=; b=eV1d3AC4shGuIkdi7BizpcnfJOYzzeaHl7IuUGF7nalhj8ZGfli+Pm/PRm2i6eH/rJ33EwlreFWwv55icTh6cl/S/HQ6KswTY/n567H+PUzp2GoEGl5Ip3NMjV1eIC7n702AIsRrfeQV6SQTEIrtDIE4kLW//3O3dqm9BrEceL8= 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 159500643651059.30334038274066; Fri, 17 Jul 2020 10:20:36 -0700 (PDT) Received: from localhost ([::1]:53290 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jwU2D-0006e2-TS for importer@patchew.org; Fri, 17 Jul 2020 13:20:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50368) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jwU1S-0005wX-4F; Fri, 17 Jul 2020 13:19:46 -0400 Received: from mail-ej1-x644.google.com ([2a00:1450:4864:20::644]:40471) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jwU1P-0005r6-Sm; Fri, 17 Jul 2020 13:19:45 -0400 Received: by mail-ej1-x644.google.com with SMTP id o18so11647710eje.7; Fri, 17 Jul 2020 10:19:42 -0700 (PDT) Received: from x1w.redhat.com (138.red-83-57-170.dynamicip.rima-tde.net. [83.57.170.138]) by smtp.gmail.com with ESMTPSA id v25sm8818378edr.74.2020.07.17.10.19.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2020 10:19:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=d18oQfmzx1Zi6yKmQ2ydWRnM5m1FMy+yxBjSGhi73ik=; b=Ie58hXXPG4OEHN8L1fW+TWsaKIdj75mHEVpYkwSITnBMTqUTLsr5cKt0xsVZ0Hm2+R Fh6UWsYZIFzTIGAMqYdi06rhcVoH06nt1qTd0/O36G0lNjIb0Afzfi/fU+Dm9HOsRzjr 1RsW7dyRurSAMH4UZPGv4DnEcoBJlh4pmOxYvDbQ3RnVrnbEAS3Vm1c+t+URvAP/JsMK vwLxX54b+56viTdIqK7yQTITA5o8pm/g7zBzQsSYEn46LypTKRlOgxjDBsDaxPLIfkwp VIjCekrsVgrFZ92IXjvUY1fnQPHC7igde0MtBbNWdYVvYiydEIsPFQrLAX2IPxQnSTm8 pGow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=d18oQfmzx1Zi6yKmQ2ydWRnM5m1FMy+yxBjSGhi73ik=; b=OQH6GCNfowV3rgORdLcxauQ7+PbPG+8qTLhEpI++buO/sRnQJbe/Hc+nEwYN9Z8oMt 9ihelCYH18/qBnxSfwm8ohqoNOYNagjpKKcUx+S6jPFolzcwqxK0F5tvgxqIdnrog2Z8 mrbEQcqxvvOj0/BGI5GiBOXhX2++Rs/ulMC/y03WRWiWm90pBeq/OiAQx/kaTYxXK/GG nrPPpOxfcEhelZsL9RUawWgTV5Zhyy0NOUG7fi1HrI1qa1PByW1d2F2YbZtaSF5t0T+X 5bg26G1M/kbSIBrw69rEprUNDmlV1FDy7CEvFlx4wk/J9Mfwdu61VAj3iqkPJuOH8e8g ACeQ== X-Gm-Message-State: AOAM533rmfToR+TNcDEyJicf2RP7VP9B+0066S/S5MoXoywz/woOquQ7 fUNNQMpo4WT++JO0HVBOrbXFkoZC3tQ= X-Google-Smtp-Source: ABdhPJyjCjsbv+k0pA6OGRcH25A2uZUtJMMdn7wWnPVUXIh5fnpGCLePdVAUTnMEZtcipXNCPUE0wQ== X-Received: by 2002:a17:906:c943:: with SMTP id fw3mr9284676ejb.55.1595006381352; Fri, 17 Jul 2020 10:19:41 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Subject: [RFC PATCH-for-5.1] hw/ide: Do not block for AIO while resetting a drive Date: Fri, 17 Jul 2020 19:19:38 +0200 Message-Id: <20200717171938.1249-1-f4bug@amsat.org> X-Mailer: git-send-email 2.21.3 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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::644; envelope-from=philippe.mathieu.daude@gmail.com; helo=mail-ej1-x644.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: 0 X-Spam_score: 0.0 X-Spam_bar: / X-Spam_report: (0.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=1, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no 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: Stefan Hajnoczi , Alexander Bulekov , John Snow , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) libFuzzer triggered the following assertion: cat << EOF | qemu-system-i386 -M pc-q35-5.0 \ -nographic -monitor none -serial none \ -qtest stdio -trace ide\* outl 0xcf8 0x8000fa24 outl 0xcfc 0xe106c000 outl 0xcf8 0x8000fa04 outw 0xcfc 0x7 outl 0xcf8 0x8000fb20 write 0x0 0x3 0x2780e7 write 0xe106c22c 0xd 0x1130c218021130c218021130c2 write 0xe106c218 0x15 0x110010110010110010110010110010110010110010 EOF ide_exec_cmd IDE exec cmd: bus 0x56170a77a2b8; state 0x56170a77a340; cmd = 0xe7 ide_reset IDEstate 0x56170a77a340 Aborted (core dumped) (gdb) bt #1 0x00007ffff4f93895 in abort () at /lib64/libc.so.6 #2 0x0000555555dc6c00 in bdrv_aio_cancel (acb=3D0x555556765550) at block= /io.c:2745 #3 0x0000555555dac202 in blk_aio_cancel (acb=3D0x555556765550) at block/= block-backend.c:1546 #4 0x0000555555b1bd74 in ide_reset (s=3D0x555557213340) at hw/ide/core.c= :1318 #5 0x0000555555b1e3a1 in ide_bus_reset (bus=3D0x5555572132b8) at hw/ide/= core.c:2422 #6 0x0000555555b2aa27 in ahci_reset_port (s=3D0x55555720eb50, port=3D2) = at hw/ide/ahci.c:650 #7 0x0000555555b29fd7 in ahci_port_write (s=3D0x55555720eb50, port=3D2, = offset=3D44, val=3D16) at hw/ide/ahci.c:360 #8 0x0000555555b2a564 in ahci_mem_write (opaque=3D0x55555720eb50, addr= =3D556, val=3D16, size=3D1) at hw/ide/ahci.c:513 #9 0x000055555598415b in memory_region_write_accessor (mr=3D0x55555720eb= 80, addr=3D556, value=3D0x7fffffffb838, size=3D1, shift=3D0, mask=3D255, at= trs=3D...) at softmmu/memory.c:483 Looking at bdrv_aio_cancel: 2728 /* async I/Os */ 2729 2730 void bdrv_aio_cancel(BlockAIOCB *acb) 2731 { 2732 qemu_aio_ref(acb); 2733 bdrv_aio_cancel_async(acb); 2734 while (acb->refcnt > 1) { 2735 if (acb->aiocb_info->get_aio_context) { 2736 aio_poll(acb->aiocb_info->get_aio_context(acb), true); 2737 } else if (acb->bs) { 2738 /* qemu_aio_ref and qemu_aio_unref are not thread-safe, so 2739 * assert that we're not using an I/O thread. Thread-safe 2740 * code should use bdrv_aio_cancel_async exclusively. 2741 */ 2742 assert(bdrv_get_aio_context(acb->bs) =3D=3D qemu_get_aio_c= ontext()); 2743 aio_poll(bdrv_get_aio_context(acb->bs), true); 2744 } else { 2745 abort(); <=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D 2746 } 2747 } 2748 qemu_aio_unref(acb); 2749 } Our context is already referenced but we don't have a getter, neither a block driver state. Maybe because we are called from a vCPU context? Avoid this case by calling the pending callback directly. In this case this is WIN_FLUSH_CACHE. I'm not sure for the other READ/WRITE commands. Reported-by: Alexander Bulekov Fixes: bef0fd5958 ("ide: convert ide_sector_read() to asynchronous I/O") BugLink: https://bugs.launchpad.net/qemu/+bug/1878255 Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- RFC because I don't understand AIO operations well. After RFC Cc: qemu-stable@nongnu.org ide_is_pio_out() verifies a PIO OUT checking: s->end_transfer_func =3D=3D ide_dummy_transfer_stop An alternative might be: if (s->pio_aiocb && s->end_transfer_func =3D=3D ide_dummy_transfer_stop= ) { /* If there is a pending AIO callback, invoke it now. */ blk_aio_cancel_async(s->pio_aiocb); s->pio_aiocb =3D NULL; } Or if we want to limit to WIN_FLUSH_CACHE: if (s->pio_aiocb && s->bus->error_status & IDE_RETRY_FLUSH) { /* If there is a pending AIO callback, invoke it now. */ blk_aio_cancel_async(s->pio_aiocb); s->pio_aiocb =3D NULL; } Cc: Stefan Hajnoczi Last minute chat: 19:01 f4bug: use bdrv_aio_cancel_async() if possible because it = won't block the current thread. 19:02 f4bug: For example, in device emulation code where the gue= st has requested to cancel an I/O request it's often possible to use the as= ync version. 19:02 f4bug: But in synchronous code like device reset it may be= necessary to use the synchronous (blocking) bdrv_aio_cancel() API instead.= :( 19:14 f4bug: The way to decide is: will the current function ret= urn to the event loop and is there someone who will handle the request comp= letion callback when cancel finishes? 19:14 f4bug: If the next line of code requires the request to fi= nished then async cancel cannot be used. 19:15 f4bug: On the other hand, if the function can return and i= t's okay for the request to cancel at some future time then you can use asy= nc. So I'll revisit this patch :) --- hw/ide/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hw/ide/core.c b/hw/ide/core.c index d997a78e47..e3a9ce7d25 100644 --- a/hw/ide/core.c +++ b/hw/ide/core.c @@ -1315,7 +1315,8 @@ static void ide_reset(IDEState *s) trace_ide_reset(s); =20 if (s->pio_aiocb) { - blk_aio_cancel(s->pio_aiocb); + /* If there is a pending AIO callback, invoke it now. */ + blk_aio_cancel_async(s->pio_aiocb); s->pio_aiocb =3D NULL; } =20 --=20 2.21.3