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=hftCGtjj8pt+mxenWc/8nRRIujGd6kOuebc/Xfn5jDC6NtlhasRUTWbLoeZ4VPJhdoAp Ai2bDf1fhIF67UvxIZ416Srz0DNhgo/hEh1ErQBkJbIcZTjtlMK1TfqFly7QD0F8DFRaK weiBalMOUHy9Y4M/O7w+DVPFkCZGlAd/yWo7dAPyiyHTh9RpjqM5UaXEYRdJbb+qGjRU2 3OFW8otfuAdnFlk4Er+30TkPRn12308oIDuBOlBua/I3WqWEP1xg/F7EgBxnIVPZCIydh k37IMjEf3FQ8D7P25z8pZRlKHuws25V9idLv0V0pUOTY41l4VE5Yr9CthFQBqOe8zUw== 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=LQhB/iNpAm3Gt8fQwvXhWdmgkMcqRGu/jAuyExSweY30J9sHRLIsC/MiTKjhrHPIxepZ mvhiPlBPds3Ed0rSnX0R47oWZUp1Pxi0IvHKAT+k3VfVHHgsxeYy5CGl8aZ19qn8qIw4m Pxhgdi69aCYBOUdj69TInyf4X+8NjsLeTPoVf5b8XiNgHPiTyJ3nmk2ln4uuXxjPuVg09 3KqqsWRnGEqFmmIGoBp6fJs90uDzpJLxOn0bUkH+2+AaPZL8gikXF+AjkJ2+x1u5Vv1jS Mf8H6hxnmaDTK6+raLtNCd8PO5A3UIbK1OXk6YQBe6RPIcYn2DEAQKPz+jfAQoeRM+Q== 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 CD821606A322 for ; Thu, 3 Dec 2026 08:55:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CD821606A322 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-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-363-ZMWl61qeO0S8C0e-4YMDVA-1; Thu, 3 Dec 2026 08:55:26 +0000 X-MC-Unique: ZMWl61qeO0S8C0e-4YMDVA-1 X-Mimecast-MFC-AGG-ID: ZMWl61qeO0S8C0e-4YMDVA_1796288125 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4a1f03f2e8aso124889055e9.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=udW9awbK6bgmGegDwoL0gFtxP26kvlEGkMHHe50tbFqrHPfYoCDaWoUJIvoJrNedIY mart6QUR+9zsofhwV6oaawd5rl9ZG5dx5pDL5NP4LdLqp4c+Z+k1rjW6U1oMz4iEi8L/ AfKW/pp37Ra2YwzdKlFOh0vX4SpzzM2Yw+ecgSZrvjdbaB4+BeBa+CkFeJJRBMdOv9nr rvkFTIYlwRU9uajcqCWUm3jv0icwCp2x07vAE+nN1Nw/slTC/UGVyNhMg+tWtr5WCS9s 3QMHoqSt1NiZ4IvJtum1UWE+QpJ5LGoPLyAD2i1Wr9QAwEUEU3Q1ELufqTyhSgSRtxo1 t3+g== X-Forwarded-Encrypted: i=1; AL+hErFOwfuZzSyn5OmakeQmnnWoUa0nV9sV6123cAppiu4Q9HT0MCt6EQ9Afjb7NaTX3INUjgQQXUV/Duyl2gc=@vger.kernel.org X-Gm-Message-State: APuU4Z+zqTyT0tT7XH6BLgryE7YmAA4aB4Lfc+tNAtdTphe4KwpbfpIq ErVzw0rN5PinrO3F55sM8P5Jo3t2Ef0KFNLVYa33b7vcUoZzt4E3apOOPE/r6UaFGlY54z2Y9S6 k1GfdSK1SdoKK70lUT29TlHQTbue2YvaNAhCrP66uk8ieIV8hgkZl7zGZylQEb2Q7ug== X-Gm-Gg: AZFdTZeFsNSMdTa2LJRfkrt7/7y/vR8Kb0gYjntCj8m496eWq3w96udcagFTDihSHdo 0m1q6lxbPZ7fmmA81dVVO/kuFvvEetg6Lgv2+8BxtWPwU0xsqeVXooBOgLsBujcqh4JhRSkuGMZ IfDsb5Qc2Hb5c1CPB93CPq2A1mDOwQrjUYriUrEzIkAgotlMOu7q2HyQDU9pA/7687C5QiXeItj r145kNFZqGe+71vY1je64Ahhd0hxIeumDAguVIg3E1dZbT+d67BBtr2UKbr58jFq72mFsgeDKYJ vZjKxtjuF4UZ0cstaDq/MCyZKhasyVHQAtyLhFM/wjTrcffCjRNR0F/JuGNifhJDdxb8vgrEXuM /J9zLb119P1IBNbhvvHGcHq+Z3cDL20p8adGld3vQKw== X-Received: by 2002:a05:600c:1d29:b0:4a9:2a92:231f with SMTP id 5b1f17b1804b1-4bb88804d4dmr876337735e9.7.1796288124822; 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: linux-kernel@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 >