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=1796288131; b=rd32BnAAG2Vbpo4W/eZgshVW3syLvyl2qMXAfkuN7Na3/Sbkt23TvmDNVmf3qM/2mu/g rxOSikEnmq2NSHnbeJSIVX0CwScuragvWLgvioadsJrQlepf32D6vY2kLwI6R63S3TM4O cve9ffYPJr/ouKw7DsxZON0L1Zth8Xv51vH9/I3ZbNY63PShlIGwREF1PXE9BqxuGESnt nYoQLGr2LMfdNxP/8QsoCx7HF/UDvt8VtRJFM57m8BTnK7xEdHUDEoRBhdkjkuKJ14ob+ IPtCPF0mXb+pbuhOvJ5r1xD3/92pcK4Mh9jQaAXwWCEIHaL9XxSyRF/815IDQJZAC/A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796288131; 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=hiJz26A6csouwu27ac21Z84q8X40pUJYC9ZphxsDKmpiaa0yMQJzCBDvEUG/3ErMob5W BQ5mGYkbzavUHlAzx4iH73FkNzfwWQ+qB7FeY/0BLu1KH9xlkI1Oi9UuvoSi/81pzs8Es M+9AJtdsrFA3U7HJx4QKOBRnEaXtViHapmUnwXjFHfM5Iye8eoXVY1ld6FET+RoDofwU2 LLuEywkJbY/UBX36DrqMcIfuVkH+noquYf47oLOYveWqf2RYHPt+WVE+XQ8mKyCsw38Mz poxwmZFFGqHszXB4IsWSX95mmKDv8rgUX8rvmyXGbCXcS59vDBHetcG4PubhZ4xwp0A== 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="V7+zz+H/"; 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 0D70B606A338 for ; Thu, 3 Dec 2026 08:55:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0D70B606A338 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=V7+zz+H/; 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=1796288130; 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=V7+zz+H/RuIj/Qe6FIHG8m74Y9vL7w05eN17Hx4Rrx+y1G09dq82rPMzDYMAMLE8xhsGWT xQ6dclih+ZV9zXw9Pa+isrY7s74U5FPjrgsIJ4XZLakuM+FbFsha7BP2gFvima0dabRKWa JRq/0Net3S2AA4TJN6d27Xa19tfglGw= 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-655-qxZ3dFC9MOq7NV3Ac8IZdg-1; Thu, 3 Dec 2026 08:55:26 +0000 X-MC-Unique: qxZ3dFC9MOq7NV3Ac8IZdg-1 X-Mimecast-MFC-AGG-ID: qxZ3dFC9MOq7NV3Ac8IZdg_1796288125 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4a359c7f47bso118073675e9.2 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=ZHrdYwdUMtUZtosfEB5WuKEYmjV16eY1G6qpK8Ze6rofKPBB/tO1cOA5luvPaUbZy8 RU+gXsWphJvpBIEuhY9a4uzuLtuHHEiXNHkJPhF/wegciRi6+5qqHHAe334atVLiQOG/ Io1sEFZkWUWfl+9DIHe/dcIAsofs6+gcxOZlRlSLtNynYwzqzEkQTRi/10/wt+3LvAq0 QfU7RvjLQgrBUTaSU0D23w/yY4UKvfG0WDqcPsi4cHTi9myxaeRuxF2r6PxvOPfu5Egs tweqoU7IaYh4VrgnOTgDn3/0CgNCmdwH0GyONQEccFuPuByljovPRj+ooPiiRaAFjJuT Rt4A== X-Forwarded-Encrypted: i=1; AL+hErEDOW9pByLLYRzsalZssDmxHHGx5HksDRk+VMFvfjsCaOj7PUUw5bHXJopDU6YYfklKDIJxzeo=@vger.kernel.org X-Gm-Message-State: APuU4Z+JuGBLT1XBDh9wpO6jZYg9bGkCYqQxAscWlgENkRWrd1N+bZBH wfeSCrTze/T1aq4/DEA6NOzuJThN4sI2EWu5t+I3cENltjRDW5TNmOsQ/HEFdOqMJz4uV2nGaWs Zl798YRyRNdt+t4oUdTd+1gyIjXrjMWL1Zm2jwmkhufe1V3ml26xF+MdRUA== X-Gm-Gg: AZFdTZcYwZwpRaqtHqo4XSCTtB5HqYLIqD9GQ/jAiAFVq/5MU5ns8HFxiU1w79kq+/I iaU+zzeohnX4LQ9yl0JQX4XOtnCP6bg8iWw8uvZt2dUbWqEgrO4kpY/0QPwzc4yd1RwknCQesOF ISc5WT8ZlhexLui/gXR66regzJ9kPXrYEvmxCslt+v4yTfFg8a0QUMYds4bDunRfYanCRFBzjlI AponhKqdzc+sG0Cq8+0PuHIHCKDI2VCSaAn5tIkzZh4epwAFUrHME6u8w8xS+IsPJbs24LQkhlt vx8eezS5y3ESzIuRTpTQUF6ll/3NrGTGkLdAOrr0GMgwidw3zjlheCmRxmg+7tKIdMcd2dO3ZmL pzDEJG0widTPNii7+4xPoM0O0ZtVz1E09k5ttojWRIA== X-Received: by 2002:a05:600c:1d29:b0:4a9:2a92:231f with SMTP id 5b1f17b1804b1-4bb88804d4dmr876337745e9.7.1796288124827; 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: stable@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 >