From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 7176A3074B2 for ; Tue, 2 Dec 2025 23:03:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716615; cv=none; b=t9CGyT2r1HQqO9/IEEcYJo0REZVkSMKvN1eShs+w2PpGwDz+ZQPuqeHLP2OAKmIR5cZ9hq+kYBtAnu1QhX+WdlNFvOjafzjQfK9c89Yj5n+AsAe8NguleCB8jGIFSUd+aBGCTruNhXnahcJJaBUZUmJ1uCYxvNhqbBwEU0FjZN4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716615; c=relaxed/simple; bh=17TLyZ7NAJhMejOsqwXU6VTVIJcq0EySjDEiRvPt3yo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qKpIJbaM4Q0UK5yLML23kG/nC52LLwVIa7XRbmQ731UBehK/CHraKyfa0RPl3R1vjg1Qo2gheLfbQM440GaJeK3V7xyC7bE6EOP+7Iiv3RkfxBk7qwpfZ9YEMml5D+5aWfKCiT1ApTtvJWiADcU2LPrS+uq3izFw5FjN+uRL0tQ= 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=rEivj8Gj; arc=none smtp.client-ip=209.85.210.202 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="rEivj8Gj" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7dd05696910so1981588b3a.2 for ; Tue, 02 Dec 2025 15:03:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764716614; x=1765321414; 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=7SOT8K49LtwTsG7RfVvoMzCrFv50BV70/2oRvQ12P5c=; b=rEivj8Gj542J3USJ2D1wOZ6+zJwdrrPUKNoIhzoFNoSdPthzS05M/tip+sZKlyVgRE Bx7Xl5MIOlT/+VHEJJAwUqNkMx9b9gtkAWHIfckm38p6MV31Tggqb8SCOZq5QmZGomA7 9/aCA3druxx1kZ4C5vcmdIjaHa8G7C056qIBAUMnlAb/TmGhXKuZuJuCI+9lloHEzMLW CaLb10zE1DJIPG3AdAR0bFAR9XSTqPjz71P73z+QAACZG4g/1e/ou3KPf7SOcGDm6Ltl vKuR2GHHxn7yPM13T2QxX8rSGwXeY0FJDuC30k4ybLtIxEd0OZRpb36/SyF83vX7HK0e 8X7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764716614; x=1765321414; 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=7SOT8K49LtwTsG7RfVvoMzCrFv50BV70/2oRvQ12P5c=; b=RlqIt6TDSeSCL0gQ/LIIVWtZXCFjDGiyD1GK2dSVyRJzHBRTSZslGESVQ/R15xR5q5 VQFevvXSy+TRwpKmXvZ7wFGrLq7eITuir5kakrgnBg87cgP5ORno6DBNlNLUeNKH3jDc dB11nm4BvhbVPaEuDDd6d9u7iBp5sHQTnJEcb/kuVj1pNfI1Uwk0xxe0LWTEfdP20x7x 8jFanvvsGgD4g+D+hizVjbfD69LLRgDjK8dHfDGhMhxQmWdwxLn9xhERxUo44hJakvuD y9nVHLHYs3d2Zt93KmCPhqAg9hMln03MdDUlVOYxw9MfcBbzIAnir+q4mprBO5WhOTDG yTLg== X-Forwarded-Encrypted: i=1; AJvYcCVifb6IF6Pm0aunVYQR7hMfBSjTRf9L+PwXXzYTOFVrN65osoNvWzmGvkFgq+YWu3c+AzItPqxfR8wZ7RY=@vger.kernel.org X-Gm-Message-State: AOJu0YxJO4kBvxrDWQefbLx2Ukc5JOV3uBo67Z61HVY5HfAReHvrxO54 WynHmxtnbSLj7aLi0dPHP3IXAK6Q2GtvXs24tt8mkVAymXem2pZNOqAI6bZiHwqTLFz/Vkp4ZxW iUOO3rT+rxEscXQ== X-Google-Smtp-Source: AGHT+IEcuP7p0pMPfljCS082qx09HbScKKr0PxJnv71nQzkwGoiDBsND32iUweTwZ1Nrc3sZw3DhZl6u27Ra3Q== X-Received: from pfwo10.prod.google.com ([2002:a05:6a00:1bca:b0:7cf:2dad:ff88]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:a1a:b0:7aa:9e4d:b693 with SMTP id d2e1a72fcca58-7e00f321535mr190120b3a.17.1764716613746; Tue, 02 Dec 2025 15:03:33 -0800 (PST) Date: Tue, 2 Dec 2025 23:02:49 +0000 In-Reply-To: <20251202230303.1017519-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: <20251202230303.1017519-1-skhawaja@google.com> X-Mailer: git-send-email 2.52.0.158.g65b55ccf14-goog Message-ID: <20251202230303.1017519-20-skhawaja@google.com> Subject: [RFC PATCH v2 19/32] vfio/pci: Preserve the iommufd state of the vfio cdev From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Pasha Tatashin , Jason Gunthorpe , iommu@lists.linux.dev Cc: Samiullah Khawaja , Robin Murphy , Pratyush Yadav , Kevin Tian , Alex Williamson , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, YiFei Zhu , Chris Li , praan@google.com 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 state of the device and also the attached domain. The token returned by the preservation API will be used to restore/rebind to the iommufd state. Signed-off-by: Samiullah Khawaja --- drivers/vfio/pci/vfio_pci_liveupdate.c | 23 +++++++++++++++++++---- include/linux/kho/abi/vfio_pci.h | 10 ++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio_pci_liveupdate.c index bcaf9de8a823..b721080599d5 100644 --- a/drivers/vfio/pci/vfio_pci_liveupdate.c +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "vfio_pci_priv.h" @@ -38,9 +39,9 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) struct vfio_device *device = vfio_device_from_file(args->file); struct vfio_pci_core_device_ser *ser; struct vfio_pci_core_device *vdev; + int err, iommufd_token; struct pci_dev *pdev; struct folio *folio; - int err; vdev = container_of(device, struct vfio_pci_core_device, vdev); pdev = vdev->pdev; @@ -51,15 +52,26 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) if (vfio_pci_is_intel_display(pdev)) return -EINVAL; + /* If iommufd is attached, preserve the underlying domain */ + if (device->iommufd_attached) { + iommufd_token = iommufd_device_preserve(device->iommufd_device, + IOMMU_NO_PASID); + if (iommufd_token < 0) + return iommufd_token; + } + folio = folio_alloc(GFP_KERNEL | __GFP_ZERO, get_order(sizeof(*ser))); - if (!folio) - return -ENOMEM; + if (!folio) { + err = -ENOMEM; + goto error_folio; + } ser = folio_address(folio); ser->bdf = pci_dev_id(pdev); ser->domain = pci_domain_nr(pdev->bus); ser->reset_works = vdev->reset_works; + ser->iommufd_ser.token = iommufd_token; err = kho_preserve_folio(folio); if (err) @@ -69,8 +81,11 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) args->serialized_data = virt_to_phys(ser); return 0; -error: +error_folio: folio_put(folio); +error: + if (device->iommufd_attached) + iommufd_device_unpreserve(device->iommufd_device); return err; } diff --git a/include/linux/kho/abi/vfio_pci.h b/include/linux/kho/abi/vfio_pci.h index 6c3d3c6dfc09..28d6eac5fd65 100644 --- a/include/linux/kho/abi/vfio_pci.h +++ b/include/linux/kho/abi/vfio_pci.h @@ -28,6 +28,15 @@ #define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1" +/** + * struct vfio_iommufd_ser - Serialized state relevant attached iommufd. + * + * @token: The token of the bound iommufd state. + */ +struct vfio_iommufd_ser { + u32 token; +} __packed; + /** * struct vfio_pci_core_device_ser - Serialized state of a single VFIO PCI * device. @@ -40,6 +49,7 @@ struct vfio_pci_core_device_ser { u16 bdf; u16 domain; u8 reset_works; + struct vfio_iommufd_ser iommufd_ser; } __packed; #endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */ -- 2.52.0.158.g65b55ccf14-goog