Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4667982rwb; Tue, 8 Aug 2023 11:48:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHTfrGGUDABsHU/MtaxiRNEcCPXl13Zeu7Q9w1AxzUKa8XO8BckGRQ0gBMdXLXOD9TI7dms X-Received: by 2002:a05:6a20:320a:b0:132:833b:961 with SMTP id hl10-20020a056a20320a00b00132833b0961mr372267pzc.36.1691520507687; Tue, 08 Aug 2023 11:48:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691520507; cv=none; d=google.com; s=arc-20160816; b=vzodyXRnybDXS/ZxxUBXDYfJgwNj92zZuIGSa2Vd2moAPU7BXW3A9SK2Emd8Fhb3ni CzXjtwLj2552BrPr5wajkn7o3u7U9u86mX/9ew/lazHsLeYW2a8W53AraT0uOIWHtuRs B34sPkz19Py9pJ3WMDfnItANbPklz9NXZveM7aZnLExMJTtoqKD8Totw+U2aLa26GylT xAy0+CSNumr1vehKSyyk7T5IYSu0KsfOd5xYBoy+qnRJMV/sWo6yRj5nw5tKbsqJCIKS S5d9hEqE4AdE1w5A9uw+3EasTfaXZrsewWMPnt79nCsk29m9C8kFMnojtZ3ctA7Wfnh6 BEug== 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=UMc/79F+2XKuxYwXqrQoYwy2p4KTqDsJfOhydisLsjE=; fh=27fvB7HhX56tKdkCQiizTgGDzhJyOkEQUf4s2tIT0jA=; b=ivqMVt94IKcRk2VcBCJTIB3DF5SSB+lLVrOBvuln40zYpLdHaQkj9dkXTHmvs4XadT LJFolDbxEmcdkb/2TtayYgZvmt4aohcrCyqnSNmwmVFVIIWxPpE4Q3GDs6GDwrt6Nti4 bXTcunem1AWvXO++/BDvLID7wbhWecWojTPg0AxwYrrjOSS54egAgKHtE4t+8aootcDF UYI1YlUlobvpBaWHs2BHg+UTvUBJBtcKgXpTFJ7grvV6ug/CWWM9AF/Z+qy8CRUvXB7k bYInI/PUjjk+FKCLLelSB12WpCah5wMuX70joX5YyuAKAleYWlhzrAzVkvVPIofyBSqu /Sdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=aAW8PoHq; 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 eg21-20020a056a00801500b006872b49c1aesi7650712pfb.133.2023.08.08.11.48.15; Tue, 08 Aug 2023 11:48:27 -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=aAW8PoHq; 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 S229871AbjHHQYM (ORCPT + 99 others); Tue, 8 Aug 2023 12:24:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229840AbjHHQWm (ORCPT ); Tue, 8 Aug 2023 12:22:42 -0400 Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB4E7A25A for ; Tue, 8 Aug 2023 08:49:28 -0700 (PDT) Received: by mail-ot1-x335.google.com with SMTP id 46e09a7af769-6bcae8c4072so3899849a34.1 for ; Tue, 08 Aug 2023 08:49:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1691509762; x=1692114562; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=UMc/79F+2XKuxYwXqrQoYwy2p4KTqDsJfOhydisLsjE=; b=aAW8PoHqmlsuVoxEg4DCItGWrWcOs5LP+gQlE3x2RMXc8huziD8LDKmCptwS3vtaXG R1fND4xLHokMhy/le+hHFoqY/RE+zWkl5Y7DYB1fufC2fExdYHf+9Hwx/o1dPRBRz4tF gU2YlfstPleFEE1fXtlhvphgT+EjuPtcsx1KM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691509762; x=1692114562; 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=UMc/79F+2XKuxYwXqrQoYwy2p4KTqDsJfOhydisLsjE=; b=DKrpUoGjwH41noq8vJ3Vi414A3gtm/UMmaxbUH9Vnp7TDiXo6ipgEHJ9cPirwY5EzH 74yW42oHCG5aXQJ84nnpgm4hmzv8MECDYqIF9Ro9cKEGAvEbbgW5LaXy6Nueu2bTtlZD d40nnOoXa08QpBtu8vQ4HFn1yKcvqIOBmtAfGLfDnjkl8ViMEN/VgDqThsdtidstDkny HUx1xMf34iRpTxPrhlsa9KlKMRPmowGoLcAOSW+WT3eQE9MX+s3KZDBOtsX6ycx0l+Qw yBUPyDa7w2uZswzNyuxbTFNzmz9u6oefAyPbG+bHpf4aJVWUtB9Ik7tXS+XK4CBMzIKl Ld+w== X-Gm-Message-State: AOJu0YzXtbdzDgRAxxnu68nioBYLf1axziQhEk5TFJKOHeDTzs/iSnTH ziWqvjhG+HkHFArPC6q+FZnKAB0Zr261wSSuZXRN X-Received: by 2002:a17:902:7245:b0:1b5:2fdf:5bd8 with SMTP id c5-20020a170902724500b001b52fdf5bd8mr12580530pll.8.1691483595962; Tue, 08 Aug 2023 01:33:15 -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 b1-20020a170903228100b001b7fd27144dsm8340168plh.40.2023.08.08.01.33.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 01:33:15 -0700 (PDT) From: Yuan Yao To: LKML Cc: Junichi Uekawa , Daniel Verkamp , Takaya Saeki , Keiichi Watanabe , Yuan Yao , Jason Wang , "David S. Miller" , "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 08:32:27 +0000 Message-ID: <20230808083257.3777012-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_BLOCKED,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 Acked-by: Jason Wang --- 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