Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4846629rwb; Tue, 8 Aug 2023 15:07:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGkFcsgdMNg4N2ST2xYBiOLXkRFQMAnBpUSdrEKkrDEo2LSAao7Iej7yZOhxTW21LMuat+0 X-Received: by 2002:a05:6358:4186:b0:135:4003:784d with SMTP id w6-20020a056358418600b001354003784dmr758074rwc.20.1691532469097; Tue, 08 Aug 2023 15:07:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691532469; cv=none; d=google.com; s=arc-20160816; b=utNMmr6t4Oaxi3lNMIukXamqi/iU0XEwXAE87xb4VUULfZxfxv1uCI/Kc7BKiReEB9 fwhMIphyQbGySNRshChJmXZ+rdFOjnhm8yABvP5p/BeRcel+CQMJDQ28+dNCG0QBTWYE hl5uHW6qVDYnU5Jzi4R+CW3Hqh7pK5IVCXJYgECJLX52NOxLikObnQkSu4DYSR4PXXzS USNo5Rt0YR4sHR3z7VCNns8FrTwf8+8pRbr+mQWWmat90qJCZNdQgyQJCP2XLwr+TWV3 ZpH9BTuM0hJH70ZDIfxirbVDAFyFT5IC2mNQ7MV9bmWhTIwHK2CNggEajn1WFOkce4lZ 8HBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=7fE5Alv+EJyDTeeiRz1KQXY25Fs9lEK25v4C7UY4dNc=; fh=cZy9HjNAXZ/7ekHeGfo7H7BOXw9+5OwCpF2oxNSlWzc=; b=vFNZmBYl9DvgOzbdG0BImoyPEUUSo4sKa/J1tZW5RN88zwC4GsTYFYp8pxQKhA9Be1 /lZRMwep8s2eWZF6pwgTsrx12zDliXS/+lkHlzhPx9wKHMkQ7Yfa4wBJz6eg5DrnoHM3 xR5QjVHD62mpZzLS/BB/Ti3x+X+RGcinvZLrkwNdXyt1fr4N19Ig6B3iBdWW+1sHtOjc oi2GHswPWG9tbFYfsqNouunxSV/Qn7UQy5g4B9giABuUyd+1BDoHn6tdnJw61W2Wj1oN eQE+m/irUQjp1BkqaCOHVg/WtMHAo7YXNgGt5E/Cc+a4Wtp6rQiHuenmRqqTMXPRVCCl odCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HsUT7hfP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y22-20020a63e256000000b00563de641cf1si7455589pgj.724.2023.08.08.15.07.37; Tue, 08 Aug 2023 15:07:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HsUT7hfP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232432AbjHHTRr (ORCPT + 99 others); Tue, 8 Aug 2023 15:17:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230463AbjHHTQz (ORCPT ); Tue, 8 Aug 2023 15:16:55 -0400 Received: from mail-vs1-xe2d.google.com (mail-vs1-xe2d.google.com [IPv6:2607:f8b0:4864:20::e2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 708A53A682 for ; Tue, 8 Aug 2023 09:39:21 -0700 (PDT) Received: by mail-vs1-xe2d.google.com with SMTP id ada2fe7eead31-44781abd5a8so2436570137.1 for ; Tue, 08 Aug 2023 09:39:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1691512759; x=1692117559; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=7fE5Alv+EJyDTeeiRz1KQXY25Fs9lEK25v4C7UY4dNc=; b=HsUT7hfPGZ2ruM0UEWI1kfUl5gPNIrXUy+235IBog+Y/bjjpcs9xUhx3/BczgwtCSk dTW/x8DMglxWvv9hvZq/UM505nkEmJQYC7Al0DRcxnWbDn2NZDkDVEtnupFDCExxnCrO YJKGklvKSqowqdHz/TxNSaZq/sDEQ3xa+vb+I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691512759; x=1692117559; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7fE5Alv+EJyDTeeiRz1KQXY25Fs9lEK25v4C7UY4dNc=; b=JDpVZ0zywFD/+I6HDLtxiVr8MIOuogHLH+ARdNWEGylSGPS+4SgwE9s1A4RaYMiCxT 0GXuvOSEtRaFnX2drfwULUDvXaqTMkcdWlKIc/MVBKM+FfIaH8nwYIHyQw7QvjL+iOJ3 8WTclhw+ZKoQ3eUT1iVS/fZubwhejQUy7wRQmgdB3OViiTKOylhlH5gYjynR2kEFfANw ft/N3pLUetKA9dYdfPoVti0h1pELYnvmIaT7fQaVpZR9QK9ani+M5AYfq7V5+wDbwSK9 FnpF93Xs0ZeVubFPmQeGrmtKQu3Tba59Xs4ttil7San/Q6/PpVPkhetwemJ2i/k50xAf sbZw== X-Gm-Message-State: AOJu0YzfCeBhreKu8EIxmPL4z/H2OE4IiWcbcSu0h0PYTHYqSfEeZx35 DGEsekCvxBzE8mQQ1C4Be1F/FlvR3h0J0+J2k39q4Jg= X-Received: by 2002:a05:6a21:3391:b0:13f:b028:789c with SMTP id yy17-20020a056a21339100b0013fb028789cmr12352855pzb.5.1691471492751; Mon, 07 Aug 2023 22:11:32 -0700 (PDT) Received: from yuanyao.c.googlers.com.com (0.223.81.34.bc.googleusercontent.com. [34.81.223.0]) by smtp.gmail.com with ESMTPSA id jd7-20020a170903260700b001bba27d1b65sm7881189plb.85.2023.08.07.22.11.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 22:11:32 -0700 (PDT) From: Yuan Yao To: LKML Cc: Keiichi Watanabe , Daniel Verkamp , Takaya Saeki , Junichi Uekawa , Yuan Yao , "David S. Miller" , Jason Wang , "Michael S. Tsirkin" , Tiwei Bie , Xuan Zhuo , virtualization@lists.linux-foundation.org Subject: [PATCH] virtio_ring: fix avail_wrap_counter in virtqueue_add_packed Date: Tue, 8 Aug 2023 05:10:59 +0000 Message-ID: <20230808051110.3492693-1-yuanyaogoog@chromium.org> X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In current packed virtqueue implementation, the avail_wrap_counter won't flip, in the case when the driver supplies a descriptor chain with a length equals to the queue size; total_sg == vq->packed.vring.num. Let’s assume the following situation: vq->packed.vring.num=4 vq->packed.next_avail_idx: 1 vq->packed.avail_wrap_counter: 0 Then the driver adds a descriptor chain containing 4 descriptors. We expect the following result with avail_wrap_counter flipped: vq->packed.next_avail_idx: 1 vq->packed.avail_wrap_counter: 1 But, the current implementation gives the following result: vq->packed.next_avail_idx: 1 vq->packed.avail_wrap_counter: 0 To reproduce the bug, you can set a packed queue size as small as possible, so that the driver is more likely to provide a descriptor chain with a length equal to the packed queue size. For example, in qemu run following commands: sudo qemu-system-x86_64 \ -enable-kvm \ -nographic \ -kernel "path/to/kernel_image" \ -m 1G \ -drive file="path/to/rootfs",if=none,id=disk \ -device virtio-blk,drive=disk \ -drive file="path/to/disk_image",if=none,id=rwdisk \ -device virtio-blk,drive=rwdisk,packed=on,queue-size=4,\ indirect_desc=off \ -append "console=ttyS0 root=/dev/vda rw init=/bin/bash" Inside the VM, create a directory and mount the rwdisk device on it. The rwdisk will hang and mount operation will not complete. This commit fixes the wrap counter error by flipping the packed.avail_wrap_counter, when start of descriptor chain equals to the end of descriptor chain (head == i). Fixes: 1ce9e6055fa0 ("virtio_ring: introduce packed ring support") Signed-off-by: Yuan Yao --- drivers/virtio/virtio_ring.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index c5310eaf8b46..da1150d127c2 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -1461,7 +1461,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, } } - if (i < head) + if (i <= head) vq->packed.avail_wrap_counter ^= 1; /* We're using some buffers from the free list. */ -- 2.41.0.640.ga95def55d0-goog