From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796288129; b=daiAkadM/x7Et928IX8VHveUETE2NtMbxcbOMTBtat4oKap3TNBL3egor+UWhO3g2hlH RMBkTE6gloqC9+sk0fAEm76dR3WcYeWfPzpeiAeMM4gKSyrBN1YtqER6Yzvs105op+0jP 85aIYTwCfQEqzMW5zxX4dQf0WwnUosJdJmDA/wRxnOMOfovQtltCN/5UaCWhu+Syngh41 /Ur0U7XzfiY/CsSYGKkjzkLPWVtT9zyqr8Ey1LDsuLszWEwj1OccWzwZfr1l1lnzs4nu0 cW6xrwjez7/Wxwc0nwoaoSxjUoQI8bcyptU9q99WSgNsFHDVUchboEwOWMzxhwamHNg== 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=Mk/I2z7IlIFtQVNwbX4Gqnv3SnVFJGcGRBvousZMA9wdObAhLmbzW9ozvSySqpvjg0ij F8jW4YgNfvFlmh3sZHvGjU1lyT7ua1lGao46NLwSiwnHXq+3WBIj6vLx10uk0KgeMMB5u O4CTH/KTiwrh4HtIF4qh4ZPh7VeSTBM6C0OUPm8JRps4iRfHezBVyqZ4GfvXDvjCrXYDh uoCmK3tt/Ep/5303ea8MQGwEFQLu+Xy/LJMuB3P51BgrlDQxiAa6Xjo9KK16OnYbtByee BbVuIWM/ZXlLwp6MA3izqscr8TbArD+pmmGLQpwCR0ydojlFjj991XsDuHfMNnHPJ0w== 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.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 37649606A32C for ; Thu, 3 Dec 2026 08:55:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 37649606A32C 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-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-522-YTJ4jaMzMSyLONrcJR72fg-1; Thu, 3 Dec 2026 08:55:26 +0000 X-MC-Unique: YTJ4jaMzMSyLONrcJR72fg-1 X-Mimecast-MFC-AGG-ID: YTJ4jaMzMSyLONrcJR72fg_1796288125 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4bb8ae35aeaso116540965e9.3 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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lsR9q83BiAEuI72p voQCnIAB2EFNjod/0CEbKI5CTZIyk9sxXYHkOfakwPg== X-Received: by 2002:a05:600c:1d29:b0:4a9:2a92:231f with SMTP id 5b1f17b1804b1-4bb88804d4dmr876337685e9.7.1796288124798; 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: kvm@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 >