Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp958367rdb; Tue, 23 Jan 2024 23:41:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IE8Jz+yVRbavoJgF2eSdy+5egr6md4LuwdjhlO1bMDqMrknQG/f4cCh2Y49bPZ1fcIPVhSh X-Received: by 2002:a17:906:b06:b0:a30:33bd:7c3e with SMTP id u6-20020a1709060b0600b00a3033bd7c3emr393759ejg.44.1706082096375; Tue, 23 Jan 2024 23:41:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706082096; cv=pass; d=google.com; s=arc-20160816; b=bl2H3sLIcWcNh85FP/yu532RXp7y+Dc6ZHS2zRPDeeJ15BFR7tgd7ft3ObdSMqFIdq dNBZupOPHtyHuH40e3mR+p/acg05MpdBR253upjpVBA74KRDrZ0VBXWVtpCxhMJDlSpE BLxjsjmlmjUOdwFbYe1OK/sYttHTPZMqaZa1cP9GVSns+Dm2VbouqbnqclUf4TmTkw5l DKKv2rF1fmA/r5yaikUCtquQDPWEfdilDOnSgvBbLOMlouCX2v+dl0nyG7DpXq/EBRCS IcgCU1hSwx4HpUzjBSfOs4LRvRwIU+q1/kWQ0g0QrHacO9uL/pqsArDZFR5GfxARQee+ pzcw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=U8Bfuvk8oKuA+3Tc2xTLQDMirdliyrSB9iEQK+0MXno=; fh=MDft5w/4F41L3Ue5lVyKx4Wmg6BvQ6NDDRm/BeFCnXM=; b=MmD9mqj25Hzm2RN/8rrx2GbaMKWaaFbsX8sq4MZkFpWuC7u49hmoCbcls9VETnHlV+ 4w9UTEzmPWHSUNYZXvG5/1i4kHH7+t6Q6SGcSCf4goRZfa2Bka8EiF/WHY13Hq2QlIxb puMGnL4x27woYTO/g9pUZAk5MNWVL1/DpIEz2Uvwn70g3R/gDXAD1l59hFc5LSpRHXut 9cE6ufd1zVbFtpKXTJjAOzsbA2miizkpwmlTRWXqTppjiIGQTQYnVMVeKeo4/oMaF4hJ 92O1aAgFJCw4Ts7hL27kb64oUlEcuoVUqA3RMjk9CXmEgez3wNN1qCgyxGQD7ce2yewZ KqhA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ldsm0IO0; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-36570-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36570-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id t12-20020a17090605cc00b00a281246153bsi12556901ejt.481.2024.01.23.23.41.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 23:41:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-36570-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ldsm0IO0; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-36570-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36570-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id E9DE21F25A8D for ; Wed, 24 Jan 2024 07:41:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B1F16175AD; Wed, 24 Jan 2024 07:39:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ldsm0IO0" Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F04517552; Wed, 24 Jan 2024 07:39:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706081955; cv=none; b=JyYiHuqfVH4Oy3vKw3oDAnC0L48YQCafbRs1uHFPZwRN74m8+P6TMCftJ/aOI9tDT74AxjxjyTLQKpuUb5ZY281Vtw5XseIlAxEb8ylpfoZFgAU4Qnu+IFQj8AO/nsxeh1UeOZwo+7cm7c3u9Z5M9AWXcOzE2WmyduPbcGERH3s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706081955; c=relaxed/simple; bh=oyPADhSHwEU24RtFpATiv+amECQBJiGyb2+3W+A/YO4=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=SJYWy4s98hvsGcdaWhs5gRfrvxXwqBCEdWylI1rzgQxshoP0yg5Tr9p9PwWuOsx+z++/lThIg97Cmr5UhqyR2G0DeJDXuWvzya+Tm2h52JgnCSVBh7c7Wy8VWqps2c9/mutDXwCfFGVwxQZbAffTvNXvdeK/ToWf37ehb6L91yY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ldsm0IO0; arc=none smtp.client-ip=209.85.167.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-f54.google.com with SMTP id 2adb3069b0e04-5100893015fso1723655e87.1; Tue, 23 Jan 2024 23:39:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706081952; x=1706686752; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=U8Bfuvk8oKuA+3Tc2xTLQDMirdliyrSB9iEQK+0MXno=; b=ldsm0IO01bItcOWQbrOElpDDpUI6HLt6y+onQRzAa1+TskX+ULsafoP7jQ8ktHW1vv W6VXYuD5U+pCOpBO9Qe3bPGRYjB1uJ7IC6gwKKMLZ3dgiEwaWAM//9KevYkKIhmoJvos Ao+sB+jQOgocZ4DXBTXK+Rxai34/c8h3vD51n5fP5c3Hp3OlUUl8WfntdFE5bhWDMhAQ YqDUOicMFmWeRsK8sDwGlYEHoICix16lEwPLrB5nv8IDkx6860ch1kxK4u7eR3imZp+h yuU5IHV//55PFkbsDYBA2h2Z30NJvEzdrXMjk1TDk7+qwxnEvpF04VAD+3UeClabVm7o J3jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706081952; x=1706686752; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=U8Bfuvk8oKuA+3Tc2xTLQDMirdliyrSB9iEQK+0MXno=; b=PEw/blxYL4cGrrsMm1LU2+lKPTariGZVgZt81TakmnaPD5gLU/18OaodJxqMoFxCw5 CRoYGG2MUE/kVB3gcS8+6uWsxNliIsGLAgbDcV5v/c7Xmr001F2WrKMNE1CYyKOHzPyQ WYu3T/WfK9mpEW2oO+f/BbiN96ITsczz/5WeP1SHRwgCi3ErwhzqwcwxmOszoRtac9wz FFnyuI2Qfr7rOciOmTNbZno7kWNi/YtRkJSJtMRuLINwAucsW9wPO7CcM0tQh1TlcTgk ggxpIoQA7ZWqCAZoFgYIGP/YE3kCqPlwywL5jpMZaMhlpfUGQcGS+nvb8atyfytIwTs+ 7KeA== X-Gm-Message-State: AOJu0YzA453m6Pv+N0l48JHG6CeXithoiD2fUVNldb8p5K9rvhULdXd1 uRSbZHGRxHQOrf19xd0PfR08m1Kboywv8X0yAXxznw3ZQV5Q+HYNa2ZkhdRP2iwQDXOf8pKDkzd jczZsRZ+kf/S+YWf5vAzRRys9qyo= X-Received: by 2002:ac2:5b4b:0:b0:510:1439:2bee with SMTP id i11-20020ac25b4b000000b0051014392beemr45089lfp.191.1706081951853; Tue, 23 Jan 2024 23:39:11 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240122110722.690223-1-yi.sun@unisoc.com> <20240122110722.690223-3-yi.sun@unisoc.com> <20240122154255.GA389442@fedora> <20240123150924.GD484337@fedora> In-Reply-To: <20240123150924.GD484337@fedora> From: yi sun Date: Wed, 24 Jan 2024 15:38:35 +0800 Message-ID: Subject: Re: [PATCH 2/2] virtio-blk: Ensure no requests in virtqueues before deleting vqs. To: Stefan Hajnoczi Cc: mst@redhat.com, Yi Sun , axboe@kernel.dk, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, pbonzini@redhat.com, virtualization@lists.linux.dev, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, zhiguo.niu@unisoc.com, hongyu.jin@unisoc.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Yes, I'm using virtio-mmio. On Tue, Jan 23, 2024 at 11:09=E2=80=AFPM Stefan Hajnoczi wrote: > > Hi Michael, > This could potentially affect other VIRTIO drivers too. Please see > below. > > On Tue, Jan 23, 2024 at 11:27:40AM +0800, yi sun wrote: > > On Mon, Jan 22, 2024 at 11:43=E2=80=AFPM Stefan Hajnoczi wrote: > > > > > > On Mon, Jan 22, 2024 at 07:07:22PM +0800, Yi Sun wrote: > > > > Ensure no remaining requests in virtqueues before resetting vdev an= d > > > > deleting virtqueues. Otherwise these requests will never be complet= ed. > > > > It may cause the system to become unresponsive. So it is better to = place > > > > blk_mq_quiesce_queue() in front of virtio_reset_device(). > > > > > > QEMU's virtio-blk device implementation completes all requests during > > > device reset. Most device implementations have to do the same to avoi= d > > > leaving dangling requests in flight across device reset. > > > > > > Which device implementation are you using and why is it safe for the > > > device is simply drop requests across device reset? > > > > > > Stefan > > > > Virtio-blk device implementation completes all requests during device r= eset, but > > this can only ensure that the device has finished using virtqueue. We s= hould > > also consider the driver's use of virtqueue. > > > > I caught such an example. Before del_vqs, the request had been processe= d by > > the device, but it had not been processed by the driver. Although I am > > using kernel5.4, > > I think this problem may also occur with the latest version of kernel. > > > > The debug code I added is as follows: > > virtblk_freeze() > > { > > vdev reset(); > > quiesce queue(); > > if (virtqueue->num_free !=3D 1024) //1024 is the init value. > > BUG_ON(1); > > vdev del_vqs(); > > } > > > > BUG_ON triggered the dump, the analysis is as follows: > > > > There is one request left in request_queue. > > crash_arm64> struct request ffffff81f0560000 | grep -e state -e __data_= len > > __data_len =3D 20480, > > state =3D MQ_RQ_IN_FLIGHT, > > > > crash_arm64> vring_virtqueue.packed,last_used_idx,broken,vq 0xffffff808= 6f92900 | > > grep -e num -e used_wrap_counter -e last_used_idx -e broken -e > > num_free -e desc_state -e "desc =3D" > > num =3D 1024, > > desc =3D 0xffffff8085ff8000, > > used_wrap_counter =3D false, > > desc_state =3D 0xffffff8085610000, > > last_used_idx =3D 487, > > broken =3D false, > > num_free =3D 1017, > > > > Find desc based on last_used_idx. Through flags, we can know that the r= equest > > has been processed by the device, but it is still in flight state > > because it has not > > had time to run virtblk_done(). > > crash_arm> vring_packed_desc ffffff8085ff9e70 > > struct vring_packed_desc { > > addr =3D 10474619192, > > len =3D 20481, > > id =3D 667, > > flags =3D 2 > > } > > > > I'm using a closed source virtual machine, so I can't see the source > > code for it, > > but I'm guessing it's similar to qemu. > > > > After the device completes the request, we must also ensure that the dr= iver can > > complete the request in virtblk_done(). > > > > Okay, I think your approach of waiting for requests before > virtio_device_reset() makes sense. blk_mq_complete_request() is async > (might be deferred to an IPI or softirq) so it's not enough for > virtblk_done() to run before virtio_device_reset() returns. There is no > guarantee that virtblk_request_done() runs before virtio_device_reset() > returns. > > A separate issue about virtio_device_reset(): > > Are you using virtio-mmio? virtio-mmio's vm_reset() doesn't offer the > same guarantees as virtio-pci's reset functions. virtio-pci guarantees > that irqs sent by the device during the reset operation complete and the > irq handlers run before virtio_device_reset() returns. virtio-mmio does > not. > > (On top of this, virtio_device_reset() now has > CONFIG_VIRTIO_HARDEN_NOTIFICATION which changes the semantics. Drivers > cannot expect to complete any in-flight requests in > virtio_device_reset() when complied with this config option.) > > Other drivers may be affected by these inconsistent > virtio_device_reset() semantics. I haven't audited the code. > > Stefan