From patchwork Thu Aug 3 22:21:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 1816704 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=MKxtd5E3; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RH3K72dxfz1ybS for ; Fri, 4 Aug 2023 08:23:39 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qRghI-0004Q8-Qr; Thu, 03 Aug 2023 18:21:32 -0400 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 1qRghG-0004Pg-Ts for qemu-devel@nongnu.org; Thu, 03 Aug 2023 18:21:30 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qRghF-0000Va-CT for qemu-devel@nongnu.org; Thu, 03 Aug 2023 18:21:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691101288; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=HIYdHtiBYjd2FsFQPPoQ8yirjasX+58jJpzdJ4zKBQE=; b=MKxtd5E3TfxLrKvkSa2oYu3/v+2wX+1pFK0hRkOVeq20CH3HinFpEgNGhm00Bk/hyHU3c3 OpsNs8njt0LNXxXcVrBfTMbpjFAHHdxkNuBoQFsExEmdkF3he3tVOMjyA0CQX7FE81ovM9 HP71y7KW5Uf5QbkY6Vq8qJYJtuBsbSw= Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-155-SrI1rp7XMD-ZEJa5hzKvdA-1; Thu, 03 Aug 2023 18:21:27 -0400 X-MC-Unique: SrI1rp7XMD-ZEJa5hzKvdA-1 Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-2b9fa64dba8so13893561fa.0 for ; Thu, 03 Aug 2023 15:21:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691101286; x=1691706086; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=HIYdHtiBYjd2FsFQPPoQ8yirjasX+58jJpzdJ4zKBQE=; b=h2SyebTqpHUHgSQdoCp+qrIV3RfMAjwHxZPlj4yNr0PALJd4brjgdIHGaS94nbCmMm 2UUfNrYvIphYZKwFyENo2VeqAhXcf+kZExN0+GoU48ChJuggW4PSfC15pZTuCGjZhZLP p2dnYc4N0gdwNaJNjLHQDX2gRhWtBZgZ9iVOfNRrjgkbF9n+6mZIdWpJIsQO3VfTwI41 zHJWezb2tOZJb8HFBDq8FRCzJneu0UoECDM4vlBT15QSRW1gFx/2TfxpNr4QSVrI6OPK PvjsFJAnBuwWClZ2WUFJc9jjtbJNj0nKSYnANx0wGqyrkNbVobgxIaSg1EwegNaVdrE3 1J0g== X-Gm-Message-State: AOJu0YzAxKh2cE2Q69JO5SwSvi5pPZ8mxNTnS2c8sOsdzNkVfMJSLomv H+HrmkFyr1Th3xVH6zRN3BdOY+zDXQRU/zhS3WTDJyH4/lIaei9qov3oSNEgPh1Oa7MPsbMyhhu WejkVlpO8ovplo70qVx0htOZiANpaLLGmW1C9jARQ6rHCm6aq7zXV/kMhL7gCXaiJaDjO X-Received: by 2002:a2e:b617:0:b0:2b9:ea6b:64f with SMTP id r23-20020a2eb617000000b002b9ea6b064fmr76580ljn.37.1691101285795; Thu, 03 Aug 2023 15:21:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHbmUQ7fU5RzQxo3+v1YjTtMVJK6sQAUiDC/ZJeGk9zFe0eSWKZtC2QkHHygyaEr/LvfbsNjg== X-Received: by 2002:a2e:b617:0:b0:2b9:ea6b:64f with SMTP id r23-20020a2eb617000000b002b9ea6b064fmr76569ljn.37.1691101285504; Thu, 03 Aug 2023 15:21:25 -0700 (PDT) Received: from redhat.com ([2.52.12.104]) by smtp.gmail.com with ESMTPSA id re8-20020a170906d8c800b00992076f4a01sm369306ejb.190.2023.08.03.15.21.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Aug 2023 15:21:25 -0700 (PDT) Date: Thu, 3 Aug 2023 18:21:22 -0400 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Cc: Peter Maydell , Hanna Czenczek , German Maglione Subject: [PULL 12/22] virtio: Fix packed virtqueue used_idx mask Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Mailer: git-send-email 2.27.0.106.g8ac3dc51b1 X-Mutt-Fcc: =sent Received-SPF: pass client-ip=170.10.129.124; envelope-from=mst@redhat.com; helo=us-smtp-delivery-124.mimecast.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, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Hanna Czenczek virtio_queue_packed_set_last_avail_idx() is used by vhost devices to set the internal queue indices to what has been reported by the vhost back-end through GET_VRING_BASE. For packed virtqueues, this 32-bit value is expected to contain both the device's internal avail and used indices, as well as their respective wrap counters. To get the used index, we shift the 32-bit value right by 16, and then apply a mask of 0x7ffff. That seems to be a typo, because it should be 0x7fff; first of all, the virtio specification says that the maximum queue size for packed virt queues is 2^15, so the indices cannot exceed 2^15 - 1 anyway, making 0x7fff the correct mask. Second, the mask clearly is wrong from context, too, given that (A) `idx & 0x70000` must be 0 at this point (`idx` is 32 bit and was shifted to the right by 16 already), (B) `idx & 0x8000` is the used_wrap_counter, so should not be part of the used index, and (C) `vq->used_idx` is a `uint16_t`, so cannot fit the 0x70000 part of the mask anyway. This most likely never produced any guest-visible bugs, though, because for a vhost device, qemu will probably not evaluate the used index outside of virtio_queue_packed_get_last_avail_idx(), where we reconstruct the 32-bit value from avail and used indices and their wrap counters again. There, it does not matter whether the highest bit of the used_idx is the used index wrap counter, because we put the wrap counter exactly in that position anyway. Signed-off-by: Hanna Czenczek Message-Id: <20230721134945.26967-1-hreitz@redhat.com> Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin Reviewed-by: German Maglione --- hw/virtio/virtio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 295a603e58..309038fd46 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -3321,7 +3321,7 @@ static void virtio_queue_packed_set_last_avail_idx(VirtIODevice *vdev, vq->last_avail_wrap_counter = vq->shadow_avail_wrap_counter = !!(idx & 0x8000); idx >>= 16; - vq->used_idx = idx & 0x7ffff; + vq->used_idx = idx & 0x7fff; vq->used_wrap_counter = !!(idx & 0x8000); }