From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90FEF3D9544 for ; Thu, 20 Aug 2026 09:11:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787217090; cv=none; b=Cti2dY9oxf7UaZCko/klt0d5G6tIYzY7jmN8J2xIiw422Thb3nIa4udnqJrYT5zF39Kz6hMSTNP7JlW/U14AREUcBTaNwRea4qjGMPVTHjhCF8d/C5wcjn9KO7ou8IroHUpIwfBoAG52EloZbw04m1paW9ekbcS6FoRxmRGqynM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787217090; c=relaxed/simple; bh=Pqhe1/gUDvPffZ3kxx5/azyvzD1TsrTgTISNWYyFtgk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Wjj4ED8k2KtugSfnltzhzRMvq2Ew7H6taIlcgATAD2ivsPc7itjawtUNEDrad75jVHm8YSUR3NaAqbTaFpUpXZiNatHAUUGdxbTQRmfGEwBSgQQv88W/j6a4zUDX6fqyjodvFvbHx9NPEhXnX/bUDu8a8ykgqgs0/2qtnWEnOjw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=XxOwevNh; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=VN5EgLDD; arc=none smtp.client-ip=170.10.129.124 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.b="XxOwevNh"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="VN5EgLDD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1787217087; 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=V3/buro2KkWnv5c3F843d2AiVyWVMTzzCYjAgiddcY8=; b=XxOwevNhPXOGD097ZBJkwsXXqg31MPeCNzoiEiNOuiQASN6JOSHref5Zl/Gl2Ffn4ae2uk bUGZh65ZUN+tbbrw/H3q2BXyQe5+aV+q2ooCXI2F6ix8NTZoVuoyB7PDN6sNOZDuyC2Q1Z rbsunANHPWVDr/W0vOdk73JzCURxYfY= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-421-_1YxW7SCOTezI4IoN-zvLg-1; Thu, 20 Aug 2026 05:11:24 -0400 X-MC-Unique: _1YxW7SCOTezI4IoN-zvLg-1 X-Mimecast-MFC-AGG-ID: _1YxW7SCOTezI4IoN-zvLg_1787217083 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-4815b878101so893473f8f.1 for ; Thu, 20 Aug 2026 02:11:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1787217083; x=1787821883; 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=V3/buro2KkWnv5c3F843d2AiVyWVMTzzCYjAgiddcY8=; b=VN5EgLDDFX2vOytakAjfysoBoH2G+XB+v+dLjx7hQ9SuzELKhvlrU95sVs/wgWEl6F mNYr4/CcLohk+0tlermMoZp2iTWeCfKMDoOdEBBdtTPkrFJnq3MiayRR1pfaI+vXxnMv G+acpWborldEikVGcmBsnirIFmuXwHMWI2eaYDGOyDcObzZxJ8LJRXLHoQKtDKwoN1RY +ft8MBJ0KT4FL7mhPCZFH2GDegRV7oU2uO/qkmjC7lmGlnQYoN+znKpP+V9/62yMip+F y0FC0jqrBJdKLphMxY+kfDiyX1b8E+juGldkSpCdngM66Kmz6IB8gW+3e398iEiKqZPQ L9Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787217083; x=1787821883; 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=V3/buro2KkWnv5c3F843d2AiVyWVMTzzCYjAgiddcY8=; b=HK5SGrcYoFnfEdiZhq/nXuNQqZPAOjqQdfACFHg3yIlNaY+jpf3xFrgr5EilcZMKxC L6ZxcBZ/51Ye41qCqmsIsp6/I6vKqio91urz0mXLWakyfIRlvjKcro3zYAjZnkeg+gY2 FoS9xFrKxd+g8BpPKNqw7l9k5oTK+X9MrOAUHB9Z8aTwRYCgRu7+JKzs7anPHeUOxIIr djMofmVIrR/7h0lCO+i97ebWpLV8kqQOyN7AUq15gzN7J/TFGIz5Cedu2LQgUWrTKB8C qGUL9+P61iUi7fF0HHcRamgHbdtPY2P55KvPQ+ay5nCTUw45x1sjc7m1yWzm6ydB/oeM MDHw== X-Forwarded-Encrypted: i=1; AHgh+Rr5n3eS/m8U1jIZuyCAlbxNN2sO4AA3nVnrtDMoGUpfzzI7LmBG8uyVrVkVO5CapW563R3RKKeUF7Cqjl4=@vger.kernel.org X-Gm-Message-State: AFuF++n7q0On3itmMavR16z9bLm1VOKopaV8XJ4MkthyBuBFIa1hXLhl xmS4w3kpnMAh0OTTtCERk+nZEr+NbI/LhuMSjnbHbMKfcUdxNvdqDdVLSZQwNUAHcW7I4eJpbP0 i4/CixshcM2bpcZFmG4GnUlbd6UQd8GXJFnc8RKBETq2ZIhk6jMtIbODMhhx8lioSfw== X-Gm-Gg: AR+sD12cQ3TpqvPHDEOIeWsRBzwujnJhN7Nvq5NyU9BjmHHqzbuTA0xCdnoJP9/aj9x 7m4zwvUSrWdNOkbRlcDzonkrwvw5MFlLWiaY9icLhH6nUTg7ClMfNVc5Mn64eUHrtW74nB4zyfO 9jQug01BbteIEl+ZEelsF/O1sfNqRdFtgZa1lMmwM4vY6eff+LmiAa4CVjLoG/JVj0olSSb6qb4 MolxkmJt4qGxRHxaPu1HNXxhhn1Ztt7TpZYrH/AaKnm/FkUi8Yif8xRYvSLcdqTUn4PEsKd/0Pg txo6BiZdEguT+YXSQSiz6XLaTHJbVz4RxLESFfvZkKulQSnfIXCSPq/9KpP5TDYfgLoCDpUjgVa uyShdkxd2Z6kBwbbcCs0V/GAJ5x2bJ2ixS2/cpuQMetAqd3K7IAXb X-Received: by 2002:a05:6000:310f:b0:480:ca53:280f with SMTP id ffacd0b85a97d-482b1e844cfmr18067500f8f.2.1787217082995; Thu, 20 Aug 2026 02:11:22 -0700 (PDT) X-Received: by 2002:a05:6000:310f:b0:480:ca53:280f with SMTP id ffacd0b85a97d-482b1e844cfmr18067399f8f.2.1787217082530; Thu, 20 Aug 2026 02:11:22 -0700 (PDT) Received: from sgarzare-redhat (host-82-53-135-154.retail.telecomitalia.it. [82.53.135.154]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-482b14b8038sm10520293f8f.20.2026.08.20.02.11.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Aug 2026 02:11:21 -0700 (PDT) Date: Thu, 20 Aug 2026 11:11:16 +0200 From: Stefano Garzarella To: Jia Jia Cc: stefanha@redhat.com, mst@redhat.com, jasowangio@gmail.com, eperezma@redhat.com, weiyj.lk@gmail.com, kvm@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v7 1/3] vhost: invalidate vring access on IOTLB transitions Message-ID: References: <20260820080332.313933-1-physicalmtea@gmail.com> <20260820080332.313933-2-physicalmtea@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20260820080332.313933-2-physicalmtea@gmail.com> On Thu, Aug 20, 2026 at 04:03:30PM +0800, Jia Jia wrote: >When ACCESS_PLATFORM changes, the addresses cached in desc, avail, and >used change meaning with the address space. Clear the cached vring access >state when the device IOTLB is installed or removed so stale IOVAs cannot >be reused as direct userspace addresses. > >Keep device IOTLB initialization idempotent and apply the mode change even >when a virtqueue backend is attached. Drop the device-wide IOTLB first, >then clear each VQ state under its own mutex, and keep the old table alive >until every VQ has completed the handoff. > >A successful live mode change leaves the backend attached but invalidates >the cached vring addresses. Userspace must configure the vring addresses >for the new address mode before data processing can resume. > >Fixes: 6b1e6cc7855b ("vhost: new device IOTLB API") >Signed-off-by: Jia Jia >--- > drivers/vhost/vhost.c | 53 ++++++++++++++++++++++++++++++++++++++++++- > drivers/vhost/vhost.h | 1 + > 2 files changed, 53 insertions(+), 1 deletion(-) > >diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c >index 14637cff0bd4..31fff9800045 100644 >--- a/drivers/vhost/vhost.c >+++ b/drivers/vhost/vhost.c >@@ -344,6 +344,17 @@ static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq) > vq->meta_iotlb[j] = NULL; > } > >+/* Caller must hold the virtqueue mutex. */ >+static void vhost_vq_invalidate_access(struct vhost_virtqueue *vq) >+{ >+ vq->desc = NULL; >+ vq->avail = NULL; >+ vq->used = NULL; >+ vq->log_used = false; >+ vq->log_addr = -1ull; >+ __vhost_vq_meta_reset(vq); >+} >+ > static void vhost_vq_meta_reset(struct vhost_dev *d) > { > int i; >@@ -1918,6 +1929,9 @@ int vq_meta_prefetch(struct vhost_virtqueue *vq) > { > unsigned int num = vq->num; > >+ if (!vq->desc || !vq->avail || !vq->used) >+ return 0; >+ > if (!vq->iotlb) > return 1; > >@@ -2287,11 +2301,48 @@ long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *arg > } > EXPORT_SYMBOL_GPL(vhost_vring_ioctl); > >+/* Caller must hold the device mutex. */ >+void vhost_clear_device_iotlb(struct vhost_dev *d) Is this the right patch where introduce this function? IMO should be introduced when we use it. About that I'm not sure if it is better to squash the other 2 patches with this one, otherwise will be this bisectable? I mean with just this patch applied (and without the other 2) who is going to free the old iotlb? >+{ >+ struct vhost_iotlb *iotlb; >+ int i; >+ >+ iotlb = d->iotlb; >+ if (!iotlb) >+ return; >+ >+ /* >+ * Drop the device-wide view first. Each VQ then drops its >+ * per-VQ view and its cached ring access under its own mutex. >+ * Keep the old table alive until every VQ has completed this >+ * handoff, since a worker may still be using it while waiting >+ * for its VQ mutex. >+ */ >+ d->iotlb = NULL; >+ >+ for (i = 0; i < d->nvqs; ++i) { >+ struct vhost_virtqueue *vq = d->vqs[i]; >+ >+ mutex_lock(&vq->mutex); >+ vq->iotlb = NULL; >+ vhost_vq_invalidate_access(vq); >+ mutex_unlock(&vq->mutex); >+ } >+ >+ vhost_clear_msg(d); >+ vhost_iotlb_free(iotlb); >+ wake_up_interruptible_poll(&d->wait, EPOLLIN | EPOLLRDNORM); >+} >+EXPORT_SYMBOL_GPL(vhost_clear_device_iotlb); >+ > int vhost_init_device_iotlb(struct vhost_dev *d) > { > struct vhost_iotlb *niotlb, *oiotlb; > int i; > >+ if (d->iotlb) >+ return 0; >+ IIUC after this patch `oiotlb` is always NULL, can we remove it? Thanks, Stefano > if (max_iotlb_entries <= 0) > return -EINVAL; > >@@ -2307,7 +2358,7 @@ int vhost_init_device_iotlb(struct vhost_dev *d) > > mutex_lock(&vq->mutex); > vq->iotlb = niotlb; >- __vhost_vq_meta_reset(vq); >+ vhost_vq_invalidate_access(vq); > mutex_unlock(&vq->mutex); > } > >diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h >index 0192ade6e749..3c75e8089373 100644 >--- a/drivers/vhost/vhost.h >+++ b/drivers/vhost/vhost.h >@@ -277,6 +277,7 @@ ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to, > int noblock); > ssize_t vhost_chr_write_iter(struct vhost_dev *dev, > struct iov_iter *from); >+void vhost_clear_device_iotlb(struct vhost_dev *d); > int vhost_init_device_iotlb(struct vhost_dev *d); > > void vhost_iotlb_map_free(struct vhost_iotlb *iotlb, >-- >2.34.1 >