From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 66F07478870 for ; Fri, 14 Aug 2026 14:46:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786718793; cv=none; b=Qt5yRxrh/mCVw5UXYcII1MGad7EGnciuVnWil98VIg2hQw8cFgJm4u3dmpEAW4zDopC5kMDBc3IPMpwBNqFWvhj0/2WgyA62cVZOknGMQ8gZQomjIWDKNyuDn5U6/9q9P2Y7EIZIyIT3w8vIFhIGQFLzTTHWo4SREOyg3RPDEGU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786718793; c=relaxed/simple; bh=Xa0VSs5Wgy2s+5Y5cGxNKlGb+hYYsFQLTHT2zK+trE4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EwvjiBGwqqa1A7VZBRQjnYVEJBBJ4wmPb9kqwgPlfTATuownK+yl9ypnBHJ36w3lgTeefQ+Wv4XigF1a+WoznRhyfoM7iXmoCWOZz++EXcf+1/+Y0DDbMeSWHC+yF9SSmMWVsY8Npg3yCXBMLuIYNhUKP0WFZuzg16X4Qmz9TMM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RCT4IIvj; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RCT4IIvj" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2d032846c95so16504225ad.1 for ; Fri, 14 Aug 2026 07:46:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786718792; x=1787323592; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=YY8hyvFF7qJQztCQvpK0cOJf2H3tyq11sUcoS+GLPLI=; b=RCT4IIvjxKxRVinQZg8+kzUjoKTXew9akP25hUKcFmz0VDj+ehHv5Pk0eC3BJn7UQ0 C89osGKBCelOZ+oqqpCf5PTTQxm1xnek/vczrvAX/x344z3F62pCqcS2TTuqwiXzqPfM k67NxC0TjE9+3naVqFaJQ/scr2HIYhLnhqV8Ei7CPFjmqYF54Tamy9LFtjiG1FDLxgkd wQT6Ns7bwUybdvcxvrANS3Qd5LrFZA5KoWwK9meVmp+aLEMMn0UJ1pcLCMDaiRERf85Y CiyGFQbFjI1UrlnstfLsbUYQYsrGvIe/DGRcRiUVx5ryBhMduDF6UbYk4ll/wFwBgyIa ULlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786718792; x=1787323592; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=YY8hyvFF7qJQztCQvpK0cOJf2H3tyq11sUcoS+GLPLI=; b=AG4Ww5Pcr+uohxDQBV+cth/TosB7MHtT5JFErcC4PUqYlS0Z0DJDdgl+QKRg6iMSfk iAHoLuCPapW/EuCYXH0Tz7KVYz2N7GiGzFAiM+1BQPf39JTIyBC4BzpB9N3NXkBNf1sV Tx5ghvqPqOK6fSuZJxfQkYZqtxnzgER+EthcTaTxqqEKBGCQKQsxeT18uG4GJThF3V1d H9kNau+k9Rwn5QLzv3Vj4Nc6hiZHi+XaibFJjv9svWdbcsC8d6KHIPMcHbgdQqVBFi3y OulhXY+stD0b51SY7OLnnSWADExqVIYhV/21cjIbEH9yd0LE2xtwHYeW/HWn5lpovlKt /yiA== X-Forwarded-Encrypted: i=1; AHgh+Rp49BJ0fvajBoXLGoOXqqW4/zouhuzRkz+y7Z8AMBQlVPterMYlORUvsxmWcse/sxiGPO8/0ba2gDAkw4I=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5QXxJ7OE2HGAQ7d36HVpT2wAQH7pRsyYq/OY7hPuZbjJL+C7b rQl3cxX7geumDOk/KtaLbxsfAETojh5IxdpwCk1wxJj8xNJGne35AObw X-Gm-Gg: AR+sD13zJKkuIm71+Krx4eKpMWYN0bWHGBewwnRbm5gPpCkLdqOt0v8V/bO7nZMkyk/ vz72RrLhPMmj5vPTaT97g4A6IsffZ8b7u943bzKSZEoj9R3ooWtsbWE9xWkcakCtLOWoy4pEJGW VIMVmdUrs4e98Ja4bgiNl27Rj0QadlR5eC3C/Ik7uLDnNmWNGwnaGigOCemPmirDo/GFW498Nd8 qVxxz/56/TdOKciJFo1HuM8TcHI7HjaT4IsipFUUVoSFcrhZCB2+Q5rXrs8u6a02/wGMJoJSQSu yRWkZ3094evYdDkdpXDF+pdDlut4oidACgF4b4XI+5Kqd9omWZSD8YIkZMN0Q+gOTkPDKD56Jxj oLwUoTUy8dTT3h4flcYC3P1t0TGI8iECrMqxcinqoN1+1sh/l6FlhiwRHwph5ENyEcdqpxEpgVD /NghFH1wqJEQSVBBS0dPU+frwKi5lonRwMc9itulwAyJTRaKqg4WCb0cFbwPOvC5g= X-Received: by 2002:a17:903:230d:b0:2cc:aa38:4df3 with SMTP id d9443c01a7336-2d3b0c47f94mr59329875ad.13.1786718791165; Fri, 14 Aug 2026 07:46:31 -0700 (PDT) Received: from [127.0.1.1] ([188.253.12.32]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-320e9df03e1sm5158782eec.16.2026.08.14.07.46.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Aug 2026 07:46:30 -0700 (PDT) From: Jia Jia To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowangio@gmail.com Cc: eperezma@redhat.com, kvm@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 1/3] vhost: add helpers for device IOTLB lifecycle Date: Fri, 14 Aug 2026 22:45:56 +0800 Message-Id: <20260814144558.134314-2-physicalmtea@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260814144558.134314-1-physicalmtea@gmail.com> References: <20260814144558.134314-1-physicalmtea@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit vhost_init_device_iotlb() currently replaces an existing device IOTLB with a new empty table. A later VHOST_SET_FEATURES update, such as a logging change while ACCESS_PLATFORM remains enabled, can therefore discard valid translations. Make device IOTLB initialization idempotent and add a common teardown helper for the inverse transition. The helper detaches the table from all virtqueues, resets their metadata caches, clears queued IOTLB miss messages, and frees the old table after the virtqueue handoff. It is a no-op when no device IOTLB is installed, so callers do not need to inspect that internal state. Callers must hold the device mutex. The helper does not update acknowledged features; backend-specific code continues to do that. Fixes: 6b1e6cc7855b ("vhost: new device IOTLB API") Signed-off-by: Jia Jia --- drivers/vhost/vhost.c | 30 ++++++++++++++++++++++++++++++ drivers/vhost/vhost.h | 1 + 2 files changed, 31 insertions(+) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index cda5c350d9be..61676987ad58 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -2451,6 +2451,32 @@ 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) +{ + struct vhost_iotlb *iotlb; + int i; + + iotlb = d->iotlb; + if (!iotlb) + return; + 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_meta_reset(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); + static bool vhost_retry_iotlb_misses(struct vhost_dev *d) { bool wake = false; @@ -2475,12 +2501,16 @@ static bool vhost_retry_iotlb_misses(struct vhost_dev *d) return wake; } +/* Caller must hold the device mutex. */ int vhost_init_device_iotlb(struct vhost_dev *d) { struct vhost_iotlb *niotlb, *oiotlb; bool wake; int i; + if (d->iotlb) + return 0; + niotlb = iotlb_alloc(); if (!niotlb) return -ENOMEM; diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index 50fccc85d594..a3c598a79251 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -283,6 +283,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,