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=1796128172; b=QQZi97B+IN99oH10PfBJnIrVaZjxpt6lzcnWNRei3DOULQLo27DJIsRRX3Nb4I4ZUb8r 34GDD0xsj3cGD7AyuCIUMVdO+WdgPAbXhddx2bPqqowEZZEBZHN5S6CxM1FyYbAKsuXBO DI8g9A9CjcK51ndRjPnMoT05ggOk9WJifHaaEOOdB46N2+zlJT/OM+tg4d0qSzj7jMr18 yNA1my45djH/5Xijx/m4+Qi4PAQhf3fhhQZPO9vHFECl9hFzoSRz4QwPYsE6di36CSR/K FBk47E5G1IjXQZaYQ3CkJjzFjB5amdkUJgx28tnOiVkOvkyflCeABq73qZEqf7M9zjw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796128172; 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: Content-Transfer-Encoding:In-Reply-To; bh=+ZeeWTIhVU3jUKLzZiMNUjs/aM4FwClLF831xDf80aA=; b=3K18IBrtNNSLKqNCfhjRe7m619vHimlkjFXn5TGcOXl33ks34aRob+MXLEtk4eIgKt1p cmOLjT6xQa6ZMVp7XVE6m7G/Nti6m+PeCkRzvSCEOFmRrUdAEDIF4TaC6z32dc7oGm47E 28TwiqJ0C29qU3rxHBQJ7SzilDTXqMbdMtpKj8uZAo4ixNjtwD5Ha2YNfaHV2Fdjte2a1 p1aoVJnFNTSq4O2I76GggV0LJf/IzJgja00X7a7nSaKymWnNTZhOY4g68PtE1Z4et0Ixc 4G+LsWx5dCBLJ8MjeATMCtKfjXIy++zh/Jg7XNM0pMbcajP4QzXYJ25FyNKsboP2Z2w== 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=azbkYSkV; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=qOP1KyEb; 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 5A594606A31A for ; Tue, 1 Dec 2026 12:29:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5A594606A31A 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=azbkYSkV; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=qOP1KyEb DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796128172; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+ZeeWTIhVU3jUKLzZiMNUjs/aM4FwClLF831xDf80aA=; b=azbkYSkVhtx43gvSwwm2uHVeW48gocGrRuLX08s+Ds+JzxW7eq4XlSr2d6EvyftuZc9pQJ MDkfb3W+1//6SeBlFNSAUYqkY9R23V8dSvdgGCMGmbQDvEiYadETnmyE2JN+FA6YP98ZuL uLsJSwsiknDzCPsIQXjnCz855kks7L0= 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-119-eO0Cp4KfNxOI4gBFZU9pJw-1; Tue, 1 Dec 2026 12:29:29 +0000 X-MC-Unique: eO0Cp4KfNxOI4gBFZU9pJw-1 X-Mimecast-MFC-AGG-ID: eO0Cp4KfNxOI4gBFZU9pJw_1796128168 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4a17741243eso28791645e9.0 for ; Tue, 01 Dec 2026 04:29:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796128168; x=1796732968; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding: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=+ZeeWTIhVU3jUKLzZiMNUjs/aM4FwClLF831xDf80aA=; b=qOP1KyEb9CAnHMz6TN4VG1sV/6WaM7ReYT9947T0BGbAvDA5bhvvUiTlpu94tz+3lK XY+5WATHvvy2b2D8gsywFTOuTjS0w1y9Iz0snwiFMxrc4YEsH79191eA2cCqmgAn+ET+ X22ABpurv6kiW+0gUzHw3Ad7TIzr/F8Kg/JFzt1Iba/i/Y73HqY0wSeZupBJOcLbx8i7 uxZC3gNNCDFgd9wcuI3aigRDv8QVLpXRPLZ3MIyg9Sgj1/RllKjy3qZhBz8nnfKPy2Xg CHbKHo0iDpIKSSyVdpohysHnTZ9Q0EG4ch8owYURqz8TjzfaRLnnQMzmm6IZGDOPhABy K3xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796128168; x=1796732968; h=in-reply-to:content-transfer-encoding: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=+ZeeWTIhVU3jUKLzZiMNUjs/aM4FwClLF831xDf80aA=; b=G4BVVAQlM+CDAhEoQcHom6NXRmnt07KAiFoO88sP9M+L3T0V7Qekb9oC/DrNtqdHIF f5JBFn0c3GNJm46JaHhgYH0bPD38oHwoxWlp/mLSj0UE2slsW4N2ruVWexaV7bfh+H8q DVZDF1/xtj7upLiATdT4lRmPGieghEHVJ3Kh1ZSsp4bNUA0CvM36srG20s5YCQmwzoMq f6Hf1OQa9ZUSxHt2iJtc0y/7pV0US3hF3N1IWIuIRRPY+6yxonzAYzJvRvw25buk0Z2E Tfe0XrFiXvl4kWv4QmAnf4uJANxqAR4jcJV03Qcx8YH9XAddoLv5buj02NpdlsyYZMCp H2gw== X-Forwarded-Encrypted: i=1; AL+hErF+Rbf5/kOrd4OZrG7ctyu+TZqPQYIrlQfXXjThcgFn3MdwwOLUmPNegsRBEPXspsbEUBc=@vger.kernel.org X-Gm-Message-State: APuU4Z8bT6LqTrOqFvKKb37vIkFWqswW6FUsDLyUM4tnBh/T5u0WPfuv 3xND7xiOraAHdXJaDB9o2sIax162SfBsCYFwZfhO825TjqBTL4/2c/qJ+uwvSudZaF3fHCW67zh li5xVxBg7FsJ5ni9O1gXoOsEWRBcpiOoPi0w/CdpBwkdIQIZUUo4c5JMFub/l9Q== X-Gm-Gg: AZFdTZfJb8/m5/pjgHookQkAHGHooWCInDygsK8gts8tSOPU6nZtQn11uPONgGq2AS6 gjs3wYbBAesRy9qOl25xfdpGeOlE7OHGdwS//iBNaXCwvdK1cCktv58dMNTUP0n0Yd7n0rOL3H1 U0KXf6RDSalqCHzeqcyqaFfyuByc/VIBA43zDODWLaBbaqg3NbIaZAzjCmseB1r4vP1m6IJxbcg gvy9tHS8smUFXZQWnuOM8gFwSXs89StMgIvuT2p/y1i8MeI/2bYgGLaZEai2rlTsoSiyR6oWFAq M8pvSY7eLdleSl5jZLrL5nQR1K5rUD22Vp4JqACbhzG74VjT6QtXtB2l9uM3JaxGRKHyjktx98U EeQu5XH7acjE74SjezLySB3EAz1k/64sURsn/DAoEoA== X-Received: by 2002:a05:600c:1d09:b0:4bb:73d5:ab33 with SMTP id 5b1f17b1804b1-4bb88816acemr807011655e9.22.1796128167975; Tue, 01 Dec 2026 04:29:27 -0800 (PST) X-Received: by 2002:a05:600c:1d09:b0:4bb:73d5:ab33 with SMTP id 5b1f17b1804b1-4bb88816acemr807011065e9.22.1796128167325; Tue, 01 Dec 2026 04:29:27 -0800 (PST) Received: from redhat.com ([2a0d:6fc0:1529:2e00:86ec:bef7:c309:ba1f]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb88442995sm1067631935e9.2.2026.12.01.04.29.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 04:29:26 -0800 (PST) Date: Tue, 1 Dec 2026 07:29:24 -0500 From: "Michael S. Tsirkin" To: Peng Hao Cc: jasowangio@gmail.com, kvm@vger.kernel.org Subject: Re: [PATCH] vhost: wrap the in-order used ring log index with a mask too Message-ID: <20261201072535-mutt-send-email-mst@kernel.org> References: <20261201112733.96626-1-flyingpeng@tencent.com> 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=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20261201112733.96626-1-flyingpeng@tencent.com> On Tue, Dec 01, 2026 at 07:27:33PM +0800, Peng Hao wrote: > Commit e244725e86be ("vhost: fix used ring index wrap in > vhost_add_used_n_in_order") made the loop that writes the used ring > entries wrap start with a mask, because start + nheads[i] can reach > 2 * vq->num, e.g. on the vhost-net TX path where done_idx can reach > VHOST_NET_BATCH (64) with a 32-entry ring. > > The loop that logs those entries for dirty tracking, added just before > by commit bfcde6b64769 ("vhost: fix dirty logging for in-order used ring > entries"), walks the same sequence of positions but still wraps with a > single subtraction: > > next = start + nheads[i]; > if (next >= vq->num) > next -= vq->num; > > so with the same inputs next stays at or above vq->num. The following > run is then logged at vq->used->ring + log_start with log_start past the > end of the ring, and the entries that were actually written after the > wrap are not logged. With migration in progress that leaves stale used > ring entries on the destination, and log_used() can set bits for guest > memory beyond the used ring. > > Use the same mask as the write loop. VHOST_SET_VRING_NUM guarantees > vq->num is a power of two. > > Fixes: bfcde6b64769 ("vhost: fix dirty logging for in-order used ring entries") > Signed-off-by: Peng Hao Yea, this was reported already. But it's broken, isn't it? There's no reason to allow this behaviour? I was testing this patch - does the following fix your issue? Works for you? Testing appreciated. ---> vhost: reject used ring batches larger than the ring A compliant driver never has more than vq->num buffers in flight at once, so a batch accumulated before used->idx advances never exceeds vq->num. Reject used count > vq->num in vhost_add_used_n()/vhost_add_used_n_in_order(). Cc: stable@vger.kernel.org Reported-by: Seohyeon Maeng Reported-by: minki@morningstarsec.dev Reported-by: YanXin Li Reported-by: Peng Hao Fixes: e2b3b35eb989 ("vhost_net: batch used ring update in rx") Fixes: 4afb52c2af44 ("vhost_net: batch update used ring for datacopy TX") Fixes: bfcde6b64769 ("vhost: fix dirty logging for in-order used ring entries") Cc: Jason Wang Cc: Eugenio Pérez Signed-off-by: Michael S. Tsirkin --- diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index d1831f4e09981..a00151932e4e5 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -3181,6 +3181,7 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq, { vring_used_elem_t __user *used; u16 old, new = vq->last_used_idx; + unsigned int total = 0; int start, i; if (!nheads) @@ -3189,6 +3190,12 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq, start = vq->last_used_idx & (vq->num - 1); for (i = 0; i < count; i++) { + total += nheads[i]; + if (unlikely(total > vq->num)) { + vq_err(vq, "Added %u > %u used heads\n", + total, vq->num); + return -EINVAL; + } if (vhost_put_used(vq, &heads[i], start, 1)) { vq_err(vq, "Failed to write used"); return -EFAULT; @@ -3245,6 +3252,11 @@ int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads, bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER); int r; + if (unlikely(count > vq->num)) { + vq_err(vq, "Added %u > %u used entries\n", count, vq->num); + return -EINVAL; + } + if (!in_order || !nheads) r = vhost_add_used_n_ooo(vq, heads, count); else