From nobody Sun Jul 12 00:33:16 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1783668985; cv=none; d=zohomail.com; s=zohoarc; b=kq1sOEIDBfjYkwgbt6RFGaYaOXn10j8BRC/ixVJtvqQSznx+BVspJSI1Z+NKdO7A3FkNCcqnPV9Hv2nHUMRldNNLJdU0Gp184IXjMN3lHugC+ajj1lFf5v3DWzAI5qR6W2piHrhOt0qxOUOZOiE92D413BDl+Cv2ljykG5/0gz0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783668985; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=tu1jPSlG9trs9p1666Fvux8qmMlQo+f+tnnrgszfoJM=; b=A+kfbFCrp7qIggRLuRu4VzkhDtVJj68C/LFibd66zCKDaXUyvvPY1T7RWmGZnmhpyu19dAWolRU3KziDp3kY5FJUFbRyz159uGg2PA9mrCCsT1cB3E50bV6SSCMODjs91M8PqIJG1yHKoWYGQtxPQclG2N+VMbEPLbb/FS0Iv2E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1783668985096442.25807792793194; Fri, 10 Jul 2026 00:36:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wi5m8-0008LV-Ia; Fri, 10 Jul 2026 03:35:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wi5m6-0008Jz-By for qemu-devel@nongnu.org; Fri, 10 Jul 2026 03:35:54 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1wi5m4-0007OX-Ph for qemu-devel@nongnu.org; Fri, 10 Jul 2026 03:35:54 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-2c6b67d5fa1so10539705ad.2 for ; Fri, 10 Jul 2026 00:35:52 -0700 (PDT) Received: from gmail.com ([114.249.134.218]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ccc9d59e40sm55461315ad.75.2026.07.10.00.35.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jul 2026 00:35:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783668951; x=1784273751; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=tu1jPSlG9trs9p1666Fvux8qmMlQo+f+tnnrgszfoJM=; b=SbyRR00eIBzW3G3UDQ6uPWMN9jKukaJyslDCuE4MiFuqmATvAFy81mpTaaC162AdFY zjYSPT47vAEip56eYkoEq3CaLa+WPSPumK/HCxUjH9GlWsKTdDVx9nsAeMh76cu2l9Uq SJ4tbJQjXzHK7y31xllx6VOgTiqXDd7+0Uw5BZfgcJX71+w6Nd9BhWoLs4UGcCKpQGPi vTYg3YqJWm0q+gkMSCTnxkdIqiKVOD5iDddgK2jbqK0/lQIWcQPSEsn0oFVUFxdqVoxh Ybmfnl5mXASw5a+bLRJFOzEfXHfJoW41qSdbw0MbTqB+1cdz5THb8I+7QAckx6ChHTT4 WN7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783668951; x=1784273751; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=tu1jPSlG9trs9p1666Fvux8qmMlQo+f+tnnrgszfoJM=; b=Dewhsqi8dY6p76LAHyG6q0Gr5dt/VD/qhXRGuno66fU7ugBtPWENjfHMbkl0XdGfFl 8b9TEKEccmEF5mhoDhTbw1YRH0BncCQ6sglVGZyVx611Nt+Q0AEaK3W5eqii5thiYc0d pXF/D0NGYBzAFXXCjW7MGpkHi6cgKEfiA1QZxX+FQ1iOWzpVM4QJUxYGgpBz9HLiyFa2 hdjed6K4RLn2uA/4gdWx0YCFY+RkXI8mxoOyJUMiD4OA1taVTsTc83+eDDsM27eZizwz LfX2+ABrya3524wfJKeUQCoGas8DyI1SO0kOzihNq4tDiEm34bZd7SvAS79Xo8iqauE/ EAvQ== X-Gm-Message-State: AOJu0YxcI48VVrUqKM+usDFRJTnfdIzJZ+nCYAQg8mXItZ/sC+qmlSP+ OQFbo5VzKl42wW0LaLZxroVOrxqPgt+SxUw8aUQPoAY/JIsOvJfvLq/T8vVjMb3h X-Gm-Gg: AfdE7cmu9M+rCpIIDTGLhdyywWOQk+xffwYlgEKlb02VvpaBbhLrNdiwYD4uM235Am+ Wv7lvq4T5BsGMvdrdFGkehdadgwgs67Vu7vmvVVW9MaNky870VrZIq7KxITsd5IYtHgA1I0rxJy 2d1k5eu7gLsu6srk1wToV7ckQvqURST3ob2+FRK214dBJNG8kIZRnY/xq00/bOX3E0A+DyWqabx WhJVGY5kNFJU78CoqO3Or1Z3vHQi94qlO/00HYHB8FV96gnC7Nb47NqxnTWbXm6NE83XF7G6zrL s43M6Xd6RnlNu+KHuNTK4CvqTo+cDetKrWc/SluYB5BlRXeM487ZhIXrkPtqSeqiWdehxlpRVy6 XBnnbFiRKSzF6sQro2MrzN7P2rQAdMHfkFPk/BLlGDUFC0Q/OHf10Oald3Xagc//6wdd0SOnMD+ BjjkZXta+baYjTIA== X-Received: by 2002:a17:902:ef08:b0:2ca:d151:383a with SMTP id d9443c01a7336-2ccea3ca6bdmr109782415ad.20.1783668950661; Fri, 10 Jul 2026 00:35:50 -0700 (PDT) From: Jia Jia To: qemu-devel@nongnu.org Cc: mst@redhat.com Subject: [PATCH v2] virtio: validate split queue head count before popping Date: Fri, 10 Jul 2026 15:35:17 +0800 Message-Id: <20260710073517.13472-1-physicalmtea@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260710022025-mutt-send-email-mst@kernel.org> References: <20260710022025-mutt-send-email-mst@kernel.org> MIME-Version: 1.0 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=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=physicalmtea@gmail.com; helo=mail-pl1-x62c.google.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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783668986549158500 Content-Type: text/plain; charset="utf-8" virtqueue_split_pop() reads the next split avail ring entry after checking that the ring is configured. Call virtqueue_num_heads() before consuming that entry, so an avail index distance larger than the queue size is rejected. virtqueue_num_heads() also reports an empty queue and keeps the read barrier needed before the avail ring entry is read. Drop the now unused virtio_queue_empty_rcu() helper. This prevents an invalid split queue state from being expanded into repeated device command processing. Link: https://gitlab.com/qemu-project/qemu/-/issues/3930 Signed-off-by: Jia Jia --- v2: - Drop virtio_queue_empty_rcu() from the split pop path. - Keep only the existing !vq->vring.avail early exit before virtqueue_num_heads(). - Remove the now unused virtio_queue_empty_rcu() helper. Tested with qemu-system-x86_64 11.0.50, built from origin/master f893c46c3931 plus this patch, configured with: --target-list=3Dx86_64-softmmu --enable-kvm --disable-tcg The original virtio-iommu live-vring qtest reproducer used for the report completed successfully on the patched build without host OOM, confirming that this reproducer is fixed by the patch. hw/virtio/virtio.c | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index f4d86a3655..68ec3f0751 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -716,26 +716,6 @@ static inline bool is_desc_avail(uint16_t flags, bool = wrap_counter) return (avail !=3D used) && (avail =3D=3D wrap_counter); } =20 -/* Fetch avail_idx from VQ memory only when we really need to know if - * guest has added some buffers. - * Called within rcu_read_lock(). */ -static int virtio_queue_empty_rcu(VirtQueue *vq) -{ - if (virtio_device_disabled(vq->vdev)) { - return 1; - } - - if (unlikely(!vq->vring.avail)) { - return 1; - } - - if (vq->shadow_avail_idx !=3D vq->last_avail_idx) { - return 0; - } - - return vring_avail_idx(vq) =3D=3D vq->last_avail_idx; -} - static int virtio_queue_split_empty(VirtQueue *vq) { bool empty; @@ -1748,12 +1728,14 @@ static void *virtqueue_split_pop(VirtQueue *vq, siz= e_t sz) address_space_cache_init_empty(&indirect_desc_cache); =20 RCU_READ_LOCK_GUARD(); - if (virtio_queue_empty_rcu(vq)) { + if (unlikely(!vq->vring.avail)) { + goto done; + } + + rc =3D virtqueue_num_heads(vq, vq->last_avail_idx); + if (rc <=3D 0) { goto done; } - /* Needed after virtio_queue_empty(), see comment in - * virtqueue_num_heads(). */ - smp_rmb(); =20 /* When we start there are none of either input nor output. */ out_num =3D in_num =3D elem_entries =3D 0; --=20 2.34.1