From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796288129; b=kNzA89+TxEyDnOudK+MtdlyCtjEybVrfoA8tUNSwomaK3ZHTkHIdh7Ss5jYEEXMPGiwW DjZBShjtjnp4/0ucvn7tB63z2WpmwK4XhnmMuK8oVwzEpMaVUIS1uU3A5suo75NUlzOcE IPUu/O3H4u3Qb39KhgasNz9bkRpsOqNlcrdxxiqJ3um0KPg5VDqacjH4DDsQinOHKVc8t 9WiDRyi3LwjPCjC2iHwy7TackGqNaZkyR5rOR+9PPCwP5FQDH9BHPTgAXmTsIH6Ve95Ex XClBbyHGeAg43DTyQpBnw+FTHUcMeJHGyzSOKkyG9FgCTr2zoqjsNPjczD0dH78QsTg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796288129; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received: X-Received:Received:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=VsNZBYvp21b5gbEc+htPk0AgCqld86jaQop2cXYxmaQ=; b=lR/md0GamKCdHFwmsxJcfq8oKtfx+g5hOjCOQKTDGNhd5VRtnRz6c6AMJ5IsNYiiTXAF A0g5VCwItY5Msxu+1+whfa2FRiRwrQY/cb5K7mLHhJTZiS7wNxSWWvhhtwoqbmsV/teve 6NEoU7MJUZ719f0FUrYizVI7y+vw5T+yYfpUMJ3/w+4i7xP8/ze9p26+g44WSe5GXvhur 6MTLp3RnC/Ki7fA9yfMJv1jCkfAjeKTwG5TjfzwR7L3pxsGx7ch67IlqSPjpM79PL7PKs XkSCoRh9ODg3UJERog8PTOH222ypNlYMaP75nGJ9VmnHcmfhyDPWJCC9Cv5gDFOHI/g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DKBvnK8s; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=TczpILft; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74C86606A333 for ; Thu, 3 Dec 2026 08:55:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 74C86606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DKBvnK8s; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=TczpILft DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796288128; 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=VsNZBYvp21b5gbEc+htPk0AgCqld86jaQop2cXYxmaQ=; b=DKBvnK8sN3n31mWLlPpYTkY/OWKimP7ngF2K8lg2iyfLWBbt0+Iy30uL0iH8gTrsb05K6T kRyaNJQcs0txB/TINB5MC7+DemwjHpyCg+UfXhhNU4fP/SiLZha+QzL5sdhgPBvwWHy4gq kC/qTpqxvCCBwfmYDmuaHfnPtD8Ue5Q= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-655-WUdOIjPvM9e_HhgpAw8CIw-1; Thu, 3 Dec 2026 08:55:26 +0000 X-MC-Unique: WUdOIjPvM9e_HhgpAw8CIw-1 X-Mimecast-MFC-AGG-ID: WUdOIjPvM9e_HhgpAw8CIw_1796288125 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4a1f03f2e8aso124889035e9.1 for ; Thu, 03 Dec 2026 00:55:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796288125; x=1796892925; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=VsNZBYvp21b5gbEc+htPk0AgCqld86jaQop2cXYxmaQ=; b=TczpILft6sJAKh4ElDSBEs5OLQziv35uy0He1WQEts3kGcvz64e9Av4bvy8aS4am4J o+Jv8FmE7l53P22JpRHO8hFzI1RXB1rHDJBwFGLrna9+OR95OuHdoI6h8jj8+WGdYfUX vtzOgRIwKfFWvkD8T3JTGKN/mUz420qk3akHUu1EXfsYKU5BL2qM2wT24CILJXP7Fayp PzuBSwO2i+0mGin8zo8g/thVMEDEnZYH8yMgD/3GdYYgWI7oT+59uPuSavc2P4lGajnP 5tZhkigOJ5lBQgAkqRKj8BdnnCODcR6c6x51vUNbX61rIhvM5NPL1j9X/yXgWfe2B4u+ 2IOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796288125; x=1796892925; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=VsNZBYvp21b5gbEc+htPk0AgCqld86jaQop2cXYxmaQ=; b=YgOX0BrfHnHIce5qF2kpTYsWAU3ds6ApwTRNyD3YpBbruksS2rtsa7YPZUU/aPoW8Q pphqyQDggSVMYrSezXX8+wcgCyUgyl76Pgp8iI1+LN7mdm8d7PWIsZZL63IFZkVmuTUI 52P0mdzBHgOExgfsBoWA/+cGGx3PNtmMHQsceHvj0WE9cfsA2DAMog+1fi6h6KOlpYlQ zLStzh8ng+QuxKwAcWdtBynG0ALiA1JbUQjMQy1QRJ1f2Mk1uUkY09d6/ffvmX/r0oBP 9vB7ZWhhhY9LgF+78oA85/DW/ExQzEwjNsJRlRgr9cb7DstcI8/nIZpPRyfTGccbZtOu EG0Q== X-Forwarded-Encrypted: i=1; AL+hErFKTdn+6xbrlYo0k4RZBzLZ6wGO25sINDuAmhO9NPspQV2WnPi/9BHa2kAhTFESBerJUyXqjAA=@vger.kernel.org X-Gm-Message-State: APuU4Z/Q+Cip8Tmu+ZxBjDovwamj6oWX5fMFLse2IP5vwVFO7uPQFnjN GvSWmyoTrcylbkyFMKU3BE4trQU93UrbEX2RmTsif1KHxn1dEpCEbIwGZ14rzG5s/5c7eNDkJa6 RiwSi7nyJddZZ9SsyVZ9x3Cza8mhTu0zu7lSWhA8HjWCVi7l8n/CDGpD3ug== X-Gm-Gg: AZFdTZcdRQaiKVSq9AKqVEHTX3rvpUdUKvPfE+IYNVmwWBK84h2Z2uCuMmP1xw12NNR LkQilaqPxJTAd+4FncWMogEb5lgTj5DXmtcaKLF9YMx+vkfXaA4FG4JlvReSzCQ/fcPeX3/240A cMf/gZ+XCAa3dqqSnwtcs6Q/wx6VQk4kBVLyWBlNQVElRXkG5+VUpVhv9YxkbGMfTlW44JE5yr5 otkGlKYnjRarpSDsHE/xm8FYIiRb5r1BGdUQY78YMMoqePqhMzXIB2aTZvuzWXnAOm1LAtTJyTR VjZ1mS8fuvC0Kcp8qW3MccI450ytNmS92AN62V01EM47bxkQsvUy/W8LALN0Yd3VNvzpBjYlEqC bzallPu4/0hLkC1S+gpLxNIcN05vjETBwKAxaSYYC+A== X-Received: by 2002:a05:600c:1d29:b0:4a9:2a92:231f with SMTP id 5b1f17b1804b1-4bb88804d4dmr876337675e9.7.1796288124800; Thu, 03 Dec 2026 00:55:24 -0800 (PST) X-Received: by 2002:a05:600c:1d29:b0:4a9:2a92:231f with SMTP id 5b1f17b1804b1-4bb88804d4dmr876337155e9.7.1796288124265; Thu, 03 Dec 2026 00:55:24 -0800 (PST) Received: from redhat.com ([2a0d:6fc0:153a:4c00:6468:7acf:febf:a046]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6a67fe48sm76380506f8f.23.2026.12.03.00.55.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 00:55:23 -0800 (PST) Date: Thu, 3 Dec 2026 03:55:20 -0500 From: "Michael S. Tsirkin" To: fadouse@pm.me Cc: Jason Wang , Eugenio =?iso-8859-1?Q?P=E9rez?= , Linfeng Sun , Fang Xieyan , kvm@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] vhost: mask the in-order used-ring log cursor Message-ID: <20261203035348-mutt-send-email-mst@kernel.org> References: <20261122-b4-06-vhost-in-order-log-oob-v1-35efdba8fbab-v1-1-fc1253d60351@pm.me> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261122-b4-06-vhost-in-order-log-oob-v1-35efdba8fbab-v1-1-fc1253d60351@pm.me> On Sun, Nov 22, 2026 at 09:19:07PM +0800, YanXin Li via B4 Relay wrote: > From: YanXin Li > > vhost_add_used_n_in_order() plays the batch of used-ring writes back a > second time for dirty logging. Commit e244725e86be ("vhost: fix used > ring index wrap in vhost_add_used_n_in_order") masked the index in the > descriptor-publishing loop. The dirty-logging loop introduced by > bfcde6b64769 still uses the single-subtraction form > > next = start + nheads[i]; > if (next >= vq->num) > next -= vq->num; > > If the guest refills the available ring while get_rx_bufs() collects > one mergeable-RX packet, that packet can consume more heads than > vq->num. get_rx_bufs() permits up to UIO_MAXIOV = 1024 descriptor > chains in one call, so a single subtraction is not enough. For > vq->num = 32, last_used_idx = 0 and nheads[0] = 64, next becomes > 64 - 32 = 32, which is still equal to vq->num and out of bounds. The > next iteration then logs > > used = vq->used->ring + 32; > log_used(vq, 4 + 32 * 8, 8); > > which starts past the end of the 260-byte ring body (4 + 32 * 8). > For a used ring placed inside page 7 at guest address 0x7ef8 (so the > entire 262-byte ring occupies [0x7ef8, 0x7ffe) and > vq_log_used_access_ok() validates only log_base[0]), log_used() > marks [0x7ffc, 0x8004) as dirty. log_write() then sets a bit in > log_base[1] for page 8, past the single validated byte. On a > vhost-net RX queue configured with VIRTIO_F_IN_ORDER, > VIRTIO_NET_F_MRG_RXBUF, > VHOST_F_LOG_ALL, VHOST_VRING_F_LOG, vq->num = 32, and > log_guest_addr = 0x7ef8, delivering one 8 KiB frame consuming 64 > mergeable buffers followed by one 1-buffer frame in the same > count = 2 flush writes > > log_base[1] = 0x01 > > where log_base[0] = 0x80 is the only byte the kernel was entitled to > touch. A guest attached to a vhost-net backend with dirty-page > logging enabled (for example during live migration) and a small RX > ring can therefore corrupt dirty-log bitmap memory that the VMM owns > outside the region registered for this ring. > > Wrap the logging loop index the same way the first loop does: > > next = (start + nheads[i]) & (vq->num - 1); > > vhost_vring_set_num() already enforces that vq->num is a nonzero > power of two, so this is well defined for every accepted ring size. > The coalescing invariant is preserved: if a run is not flushed, > next == start + 1. When start == vq->num - 1, start + 1 == vq->num, > whereas the masked next is always less than vq->num. The run is > therefore flushed before the cursor wraps. > With this change the same vhost-net configuration publishes > used->idx = 65 and confines the dirty log to log_base[0] = 0x80, > leaving log_base[1] = 0x00. The mask keeps next in [0, vq->num) for > every accepted power-of-two ring size. As a bounded cross-check of > the index arithmetic, a Z3 model over ring sizes {1, 2, 4, 8, 16, 32} > and buffer counts in [1, 64] finds an out-of-range result for the old > subtraction (vq->num = 16, start = 15, nheads = 17) and none for the > mask within those bounds. > > Runtime verification and fix validation were performed in an x86-64 > QEMU/KVM guest running mainline after v7.4-rc3, with Generic KASAN, > UBSAN, lockdep, and DEBUG_OBJECTS. > > Fixes: bfcde6b64769 ("vhost: fix dirty logging for in-order used ring entries") > Cc: stable@vger.kernel.org > Signed-off-by: YanXin Li vhost: reject used ring batches larger than the ring is in stable now, and seems to be the most comprehensive way to address this class of issues. Could you pls confirm it addresses this problem? > --- > drivers/vhost/vhost.c | 4 +--- > 1 file changed, 1 insertion(+), 3 deletions(-) > > diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c > index dc973287567..80b887e1511 100644 > --- a/drivers/vhost/vhost.c > +++ b/drivers/vhost/vhost.c > @@ -3206,9 +3206,7 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq, > log_start = start; > log_count++; > > - next = start + nheads[i]; > - if (next >= vq->num) > - next -= vq->num; > + next = (start + nheads[i]) & (vq->num - 1); > if (i + 1 == count || next != start + 1) { > used = vq->used->ring + log_start; > log_used(vq, > > --- > base-commit: c44104f3642da720164042623e4832836aefdd8e > change-id: 20261122-b4-06-vhost-in-order-log-oob-v1-35efdba8fbab-43734ad10f0a > > Best regards, > -- > YanXin Li >