From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 8B40439099E for ; Sun, 14 Jun 2026 23:37:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781480266; cv=none; b=Zeob7g5yfDD6OZezAyaVRtmawNhVVDmGkwUYTT7iwLenXpwF6GQqfLakK65xzfCSV0x7eIhK36101WuZj5ht+QKmEuXhZB6+KGVcpS0sE7DXylBZlgxqQ3WGUH9snqHzMyBYsSAdgSBiuVt6Daf2yBKMSZ1o+BeiBcThXLjHyxU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781480266; c=relaxed/simple; bh=sHmdBPgUy64h0nomxRMYZgtzGsV7LcN1Y2jthOewPfQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fNIBNu4f/Jvq1Y/f0Bor3RhYMYxO+yGhgpSBjxXvKP2AZl956OiFPU3+tLCKEgdODKJ1w1j8BIo+YM/q7DTc9OO7kQx/C9YT1x6Be5cvBjkkMKFOSZMmzg60JVVWQnovTqu8t7ccBfdk5OfVSdc9tqmTrJ5NGFwUG8rT3cjbITw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=atCqeRik; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="atCqeRik" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-8423f1fe39eso3412454b3a.1 for ; Sun, 14 Jun 2026 16:37:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781480264; x=1782085064; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4W8djzLKUJavR0ZWbxIExIzqg4vveKv4bLWIrwQxGcQ=; b=atCqeRikhx7DpDmvH+++xBrAOcokvPNwFdv2ujGoJfKJ2ClYwTm+af1Gg6sxwSOD/R z61vPz8nh++iCrNZQ1em5UFKB2OUtYhkfOGAAw8EeAW3co35FdHvDqUDRpVTdCzQHa8+ kavO75ccqXvEYVHm/gBmE3tM8NyZpgJ58Zdf8v3kU/NRVK19bfK1K1VqHmfplXioDUwX Eg+Vn6cV+Lz9AHfY6p/XulrfZzgGXhElxMusF2vwiSNn+mYREOojzfo/ymYl+Oshy6C1 lV4PIDi+eEfmtlHSj+ZagnSMC4ZYN5t9b/FvtXqxjF7YeG+p/rZsq2QraPSE3yfu/zx4 UXXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781480264; x=1782085064; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4W8djzLKUJavR0ZWbxIExIzqg4vveKv4bLWIrwQxGcQ=; b=S4ys9V1tSv6Nb7smYAuxAsH2dDmXzZpoPIWU0ZNXqZUtTPGMPA6lrgg4g+7l0QND7Y 4Wjm4/3tfXwKO+207KleNRpn4Xk0J9sOvct+ZMonOiG5/eRgQNBrPlcQkgbsuz1YPUdR Hoi82aXJDm2o0/3ZBdGxSzqeMTuLH/hp02qN/sAVPtOlqKf94ZoonUtnVi2DTYOWbtJX ApqQmjrKm81q9lLI+yKLrgshPr7tPx/aWxhD1xNthhpFJXUH5vDQWtgXp009rIU6tOos K1T1NMgZoy88LMqH7qMnLst0OuGR5IFass1Q2DMBafQKuHF2WMIKQjsMFWjZXFnbDh35 rIfQ== X-Forwarded-Encrypted: i=1; AFNElJ/uDsgdl50CkPpugqHDJrxKQmZdM8innS+rjcijN6RkXF42vLlvfEUSnT+hH8Fn28Q2QOrvHJDwPsuTaSo=@vger.kernel.org X-Gm-Message-State: AOJu0YwKTYxAIGkgEpweLJUnNv/YjLRtbCgnBwPrYEfKWtU0mMmIlu2L OL9HUfESwNYOUdECnXJ3s8fhx6c/Yi39M0S0fRgR+nQBB6mdxzfV+A1nF3BloSHU63qmbTPmejy HWst5o53er07/fg== X-Received: from pfbfm11.prod.google.com ([2002:a05:6a00:2f8b:b0:83e:b5cc:11c3]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2d17:b0:842:6004:3fce with SMTP id d2e1a72fcca58-8434cdeeedbmr12308751b3a.26.1781480263617; Sun, 14 Jun 2026 16:37:43 -0700 (PDT) Date: Sun, 14 Jun 2026 23:37:27 +0000 In-Reply-To: <20260614233728.2212104-1-skhawaja@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260614233728.2212104-1-skhawaja@google.com> X-Mailer: git-send-email 2.54.0.1136.gdb2ca164c4-goog Message-ID: <20260614233728.2212104-18-skhawaja@google.com> Subject: [PATCH v3 17/18] vfio/pci: Preserve the iommufd state of the vfio cdev From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Jason Gunthorpe Cc: Samiullah Khawaja , Robin Murphy , Kevin Tian , Alex Williamson , Shuah Khan , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Pratyush Yadav , Pasha Tatashin , David Matlack , Andrew Morton , Pranjal Shrivastava , Vipin Sharma Content-Type: text/plain; charset="UTF-8" If the vfio cdev is attached to an iommufd, preserve the state of the attached iommufd also. Basically preserve the iommu specific state of the device and also the attach iommu HW unit. Once the device and its iommufd attachment is preserved, it cannot be detached or attached to another IOAS until it is unpreserved. Reviewed-by: Pranjal Shrivastava Signed-off-by: Samiullah Khawaja --- drivers/vfio/device_cdev.c | 10 +++++++ drivers/vfio/pci/vfio_pci_liveupdate.c | 37 +++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c index 0050f0fe456b..844f1473d875 100644 --- a/drivers/vfio/device_cdev.c +++ b/drivers/vfio/device_cdev.c @@ -275,6 +275,11 @@ int vfio_df_ioctl_attach_pt(struct vfio_device_file *df, } mutex_lock(&device->dev_set->lock); + if (iommufd_device_is_preserved(device->iommufd_device)) { + ret = -EBUSY; + goto out_unlock; + } + if (attach.flags & VFIO_DEVICE_ATTACH_PASID) ret = device->ops->pasid_attach_ioas(device, attach.pasid, @@ -333,6 +338,11 @@ int vfio_df_ioctl_detach_pt(struct vfio_device_file *df, } mutex_lock(&device->dev_set->lock); + if (iommufd_device_is_preserved(device->iommufd_device)) { + mutex_unlock(&device->dev_set->lock); + return -EBUSY; + } + if (detach.flags & VFIO_DEVICE_DETACH_PASID) device->ops->pasid_detach_ioas(device, detach.pasid); else diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio_pci_liveupdate.c index 7b841a1e7fcc..37d327f82d5a 100644 --- a/drivers/vfio/pci/vfio_pci_liveupdate.c +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -106,6 +106,7 @@ #include #include +#include #include #include #include @@ -114,6 +115,8 @@ #include "vfio_pci_priv.h" +MODULE_IMPORT_NS("IOMMUFD"); + static bool vfio_pci_liveupdate_can_preserve(struct liveupdate_file_handler *handler, struct file *file) { @@ -155,9 +158,26 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) vdev = container_of(device, struct vfio_pci_core_device, vdev); pdev = vdev->pdev; +#ifdef CONFIG_IOMMU_LIVEUPDATE + /* If iommufd is attached, preserve the underlying domain */ + mutex_lock(&device->dev_set->lock); + if (device->iommufd_attached) { + u64 token, preserved_state; + + ret = iommufd_device_preserve(args->session, + device->iommufd_device, + &token, &preserved_state); + if (ret) { + mutex_unlock(&device->dev_set->lock); + return ret; + } + } + mutex_unlock(&device->dev_set->lock); +#endif + ret = pci_liveupdate_preserve(pdev); if (ret) - return ret; + goto err_iommufd_unpreserve; ser = kho_alloc_preserve(sizeof(*ser)); if (IS_ERR(ser)) { @@ -173,12 +193,27 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) err_unpreserve: pci_liveupdate_unpreserve(pdev); + +err_iommufd_unpreserve: + mutex_lock(&device->dev_set->lock); + if (device->iommufd_attached) + iommufd_device_unpreserve(args->session, + device->iommufd_device); + mutex_unlock(&device->dev_set->lock); return ret; } static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args *args) { struct vfio_device *device = vfio_device_from_file(args->file); + struct vfio_pci_core_device_ser *ser; + + ser = phys_to_virt(args->serialized_data); + mutex_lock(&device->dev_set->lock); + if (device->iommufd_attached) + iommufd_device_unpreserve(args->session, + device->iommufd_device); + mutex_unlock(&device->dev_set->lock); pci_liveupdate_unpreserve(to_pci_dev(device->dev)); kho_unpreserve_free(phys_to_virt(args->serialized_data)); -- 2.54.0.1136.gdb2ca164c4-goog