From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796429953; b=cPujWzOBBti1UHwyVnuYOzBd4lIYlQH1iE9pWojen5czvC0LfZfCUT+7x9V8v/g5S95c K2drmu0RMhhiH99CoMMASJ1eddoTFS/Hb0xX66/rmFBnPMLutm7l5k3suHdYKiMLy/k9M gdrXUR6XyVwxI5/nc/KmNn73NnsCyovNmqAJtbZjnp9h+eFa4ZrQXrDXftNBcKzXCiMzY aJ9FTm7VA//2Gn/tvDMAimZZ91GHLub7sOUr4G3rvP5fW4UvDqMJcNKjMt3R1rffn4IAX 0glrlDalDgYKqqDXyJRmFhOjILKXpxBlkCeJKPuVYls7b5F549I2fY0o5F2Svu79VYQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796429953; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; bh=odP/ahjIJp2WH5yWMUELgBqJwvvWYawooSkWlt+WLKU=; b=IbNfgcMSPvmHsnXmh5+QqkHiYaX8oOLtMfi44LBuQ2AmKF1+FHBYlD1UlPKCKOM9IniL JP7OesknCokKcqrZWuHNVu3rfzEFOarh+s60Z3No7+Qp4Lk7hrdkrkITWiSxoRvBFv4Z2 0TjmeebjLGFeyrASNqa0ayhQ9m+Nzlm8zbZL5aeZg4bkAxCcKNdc+tOTHSnDt6bFMkeAr WOkFDV3bYtmW/lcPCByQb5a1+Byxt0lorcTNz2EgUjxN9LqRQx7vu57F1Nx51t98n6DkJ qp1jnX7MfS7AVfvDNu20kow2onD5eNuWk9iEcawEVrHD20nc2Am8BSZbcUyh+pAlKhg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=E8u+6dd+; arc=none smtp.remote-ip=209.85.214.173 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AAD6A606A32C for ; Sat, 5 Dec 2026 00:19:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AAD6A606A32C 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=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=E8u+6dd+ Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2f0e8bbc7a4so11065ad.1 for ; Fri, 04 Dec 2026 16:19:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796429952; x=1797034752; 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=odP/ahjIJp2WH5yWMUELgBqJwvvWYawooSkWlt+WLKU=; b=E8u+6dd+m4ZOXucqStd000cAMSrDfZXAXHHdOKJ3wxyiwHHALLY09MiaWF6hGzEGKQ ccF57Madbels7ij+DQ5HVX/XFhSkMYeZ1WPnTwDuz9r8JbNm+cLUEYb9u63exNDQlurf S171n3Pr6XVTYVAahHgXUjEk+6E0L/HN7Wg9Vu8i6XVCtAmQzRfyCYJQijCAFdgpzKoC x//BHP83flOh4btQKVNdfJwTHxSP45/TJLx7TwsXYP2B2YZqMt3qsBnGfpHNMTewIWPa qmwnAh+3B6+nLKp35A5KaRet+yTQ1nJH0AEkzpKjZpg10PI1EaprY4Lq0bJFWy6Cau1D ZEvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796429952; x=1797034752; 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=odP/ahjIJp2WH5yWMUELgBqJwvvWYawooSkWlt+WLKU=; b=ECNOKC4MX0VZeBlxUSR1VkSvw+SIHXRbMu/GJ/pXiWdslRzFp4Aid/fYRKFRPo/k/K FhdMVVRttvatiYwHg4wU4oAPVxuSGLkaPpqQou1bsT+B1HaSYV8U3OEVFlP7jGzrdG8i uEaGCvj5ZTphRcg0qrJVtSDPFMQavnUQMnkrnfl35xsxGX8VlwTIMRloCsUNZSf2XTGF ZJvaxg6+bHPezve2W/yOyqCMzHfcbytp+aZ3DC1ASr8j4FBVRCZIFQBqBX6yRL4GUz0N +WBTJgY58P0XVD+btHWaNL8kI2fhhYjdCQNyN0L6RbxJOexui5HYMC1jELypnGg6wAzb 4daA== X-Forwarded-Encrypted: i=1; AL+hErGZ06BGxZVmvdY+AeCwk+Y2tb128P1x2rOLD3IZhI9YTUZIoLAMcVlgooOH89N7iB50iGCDdkUS6RtHtok=@vger.kernel.org X-Gm-Message-State: AB4q+uHCRXVCSavMxufeu3xMUalBHmBBReUZSfkxgjt5QA7OqCQNA4rd pxjnxk8mLWCM55XHQnIGn38+De1aXh1llAbhCnGif8YgXSm1LstuH3dobjBy1orU7Q== X-Gm-Gg: AZFdTZezqp88MB+CLxI/6lo9IqBRKlnnkFSaiqo6YU97nUQUtueWCbHIzkuNvWEmBMx zfhvuojghw0w7grUReEqgOz9g5w+jAyv2j19vEJPqmRzkoczhI5crpFvA05d5F0saheNg9Hw0rR EDzK7PllAX51i3HsRKZjSJgC1rwB0aQOXFrGMa1HNye7yPI71g8w8wTzAHH5ZYM3s2G5sM3aQic u70bmczYIQ1qGegCpiyrjLchAOQwgcCrevObRYhVmeDh9XIkc4Rm8Mo3Nr056ZcmbZysi+nzLE2 I6u1CcjvoZUNxk3vMj9y1tqDnxWMMkefThfPKL6Pk5ZL64fBXcXYdDMg/ItFQiqZhEl3r2WND9n SrhML7+b1ORJA2KdoCqpkcqkvja8YBeqJ2GhySVX4abyCcldoSdARrrHRdhaa8w4xHrn5JXwhMl 6ZHr45Az4+cKdEHhr2T/tNeNK23EkaCOqbQdHXmi8yqa+B3ZxRaIVORGx+fSVYN2VDzaW77DLDM Fpq9HMdKyxqZwaf4Kf59q/nDvKOjvUwQZ+GweTx7MEe0zj8pQTGN53STr5NnikHN/rSQW3h8Y+9 z3YIEFJDCiKua6gkdh0vJMPv98+OlnBm6WBWokpnu2Lbx6VqXPZR2kTBCIeq8NqG4yZ4cjeXI+s K2zTszoyVMZa/MidcrjqN X-Received: by 2002:a17:902:fc48:b0:2f1:b8a0:222e with SMTP id d9443c01a7336-2f1f4d47876mr538375ad.26.1796429951334; Fri, 04 Dec 2026 16:19:11 -0800 (PST) Received: from google.com (25.75.145.34.bc.googleusercontent.com. [34.145.75.25]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8de120fee00sm2513853b3a.18.2026.12.04.16.19.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 16:19:10 -0800 (PST) Date: Sat, 5 Dec 2026 00:19:07 +0000 From: Samiullah Khawaja To: Baolu Lu Cc: Joerg Roedel , Will Deacon , Jason Gunthorpe , David Woodhouse , 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 , Ankit Soni , Nicolin Chen , John Starks Subject: Re: [PATCH v8 06/18] iommu: Implement device and IOMMU HW preservation Message-ID: References: <20261123044949.3610702-1-skhawaja@google.com> <20261123044949.3610702-7-skhawaja@google.com> <9fbbfe18-485d-418d-87c4-57f6d4b2d582@linux.intel.com> X-Mailing-List: linux-kernel@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=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <9fbbfe18-485d-418d-87c4-57f6d4b2d582@linux.intel.com> On Fri, Dec 04, 2026 at 02:14:30PM +0800, Baolu Lu wrote: >On 11/23/26 12:49, Samiullah Khawaja wrote: >>Add IOMMU ops to preserve/unpreserve the device state handled by the >>IOMMU driver. This is IOMMU specific state of the device that needs to >>be preserved for liveupdate. These ops can be implemented by the IOMMU >>drivers that support preservation of devices that have their IOMMU >>domains preserved. During device preservation the state of the >>associated IOMMU is also preserved as dependency. >> >>Signed-off-by: Samiullah Khawaja > >[...] > >>diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c >>index 8da858154949..f8837ab1b526 100644 >>--- a/drivers/iommu/iommu.c >>+++ b/drivers/iommu/iommu.c >>@@ -18,6 +18,7 @@ >> #include >> #include >> #include >>+#include >> #include >> #include >> #include >>@@ -71,6 +72,10 @@ struct iommu_group { >> */ >> unsigned int recovery_cnt; >> void *owner; >>+#ifdef CONFIG_IOMMU_LIVEUPDATE >>+ /* Group is reserved for a device with live update state */ >>+ bool liveupdate_reserved; >>+#endif >> }; >> struct group_device { >>@@ -624,6 +629,19 @@ static struct iommu_domain *pasid_array_entry_to_domain(void *entry) >> DEFINE_MUTEX(iommu_probe_device_lock); >>+#ifdef CONFIG_IOMMU_LIVEUPDATE >>+static bool iommu_group_liveupdate_reserved(struct iommu_group *group) >>+{ >>+ lockdep_assert_held(&group->mutex); >>+ return group->liveupdate_reserved; >>+} >>+#else >>+static inline bool iommu_group_liveupdate_reserved(struct iommu_group *group) >>+{ >>+ return false; >>+} >>+#endif >>+ >> static int __iommu_probe_device(struct device *dev, struct list_head *group_list) >> { >> struct iommu_group *group; >>@@ -661,6 +679,11 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list >> goto err_put_group; >> } >>+ if (iommu_group_liveupdate_reserved(group)) { >>+ ret = -EBUSY; >>+ goto err_free_gdev; >>+ } >>+ >> /* >> * The gdev must be in the list before calling >> * iommu_setup_default_domain() >>@@ -697,6 +720,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list >> err_remove_gdev: >> list_del(&gdev->list); >>+err_free_gdev: >> __iommu_group_free_device(group, gdev); >> err_put_group: >> iommu_deinit_device(dev); >>@@ -3545,6 +3569,40 @@ bool iommu_group_dma_owner_claimed(struct iommu_group *group) >> } >> EXPORT_SYMBOL_GPL(iommu_group_dma_owner_claimed); >>+#ifdef CONFIG_IOMMU_LIVEUPDATE >>+/** >>+ * iommu_group_reserve_liveupdate() - Reserve a group for live update >>+ * @group: The group. >>+ * >>+ * No other device can be added to a reserved group. >>+ * >>+ * Return: 0 on success, -EBUSY if the group is already reserved, or >>+ * -EOPNOTSUPP if the group doesn't have exactly one device. >>+ */ >>+int iommu_group_reserve_liveupdate(struct iommu_group *group) >>+{ >>+ guard(mutex)(&group->mutex); >>+ if (group->liveupdate_reserved) >>+ return -EBUSY; >>+ >>+ if (!list_is_singular(&group->devices)) >>+ return -EOPNOTSUPP; >>+ >>+ group->liveupdate_reserved = true; >>+ return 0; >>+} > >The preservation only blocks new devices from joining the group. What >happens if the preserved device is released, for example by a surprise >hot-unplug? It appears that the device is gone but its device_ser entry >stays live, the iommu_ser refcount is leaked. > >Is this guaranteed not to happen because vfio keeps the device open? If >so, perhaps add a WARN_ON() in the release path when the device >still has preserved state? Yes, the device is not removed as vfio keeps the device open. I will add a WARN_ON() in the release path. > >>+ >>+/** >>+ * iommu_group_unreserve_liveupdate() - Drop the live update reservation >>+ * @group: The group. >>+ */ >>+void iommu_group_unreserve_liveupdate(struct iommu_group *group) >>+{ >>+ guard(mutex)(&group->mutex); >>+ group->liveupdate_reserved = false; >>+} >>+#endif >>+ >> static void iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid, >> struct iommu_domain *domain) >> { >>diff --git a/drivers/iommu/liveupdate.c b/drivers/iommu/liveupdate.c >>index a98f16d11727..06234f9c604b 100644 >>--- a/drivers/iommu/liveupdate.c >>+++ b/drivers/iommu/liveupdate.c >>@@ -43,6 +43,9 @@ >> #include >> #include >> #include >>+#include >>+ >>+#include "iommu-priv.h" >> /** >> * struct iommu_ser_set - Set of serialized objects of one type >>@@ -358,3 +361,185 @@ void iommu_unpreserve_domain(struct iommu_domain *domain) >> liveupdate_flb_put_outgoing(&iommu_flb); >> } >> EXPORT_SYMBOL_GPL(iommu_unpreserve_domain); >>+ >>+static int iommu_preserve_locked(struct iommu_device *iommu, >>+ struct iommu_flb_obj *flb_obj) >>+{ >>+ struct iommu_hw_ser *iommu_hw_ser; >>+ int ret; >>+ >>+ if (!iommu->ops->preserve || !iommu->ops->unpreserve) >>+ return -EOPNOTSUPP; > > >nit: The only caller has already checked these ops. The same for >WARN_ON_ONCE() in iommu_unpreserve_locked(). Both can be dropped. Will do. > >>+ >>+ lockdep_assert_held(&flb_obj->lock); >>+ if (iommu->iommu_ser) { >>+ iommu->iommu_ser->hdr.ref_count++; >>+ return 0; >>+ } >>+ >>+ iommu_hw_ser = iommu_ser_set_alloc(&flb_obj->iommu_set); >>+ if (IS_ERR(iommu_hw_ser)) >>+ return PTR_ERR(iommu_hw_ser); >>+ >>+ ret = iommu->ops->preserve(iommu, iommu_hw_ser); >>+ if (ret) { >>+ iommu_hw_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED; >>+ return ret; >>+ } >>+ >>+ iommu->iommu_ser = iommu_hw_ser; >>+ return ret; >>+} >>+ >>+static void iommu_unpreserve_locked(struct iommu_device *iommu, >>+ struct iommu_flb_obj *flb_obj) >>+{ >>+ struct iommu_hw_ser *iommu_hw_ser = iommu->iommu_ser; >>+ >>+ lockdep_assert_held(&flb_obj->lock); >>+ if (WARN_ON(!iommu_hw_ser)) >>+ return; >>+ >>+ if (WARN_ON_ONCE(!iommu->ops->preserve || >>+ !iommu->ops->unpreserve)) >>+ return; >>+ >>+ iommu_hw_ser->hdr.ref_count--; >>+ if (iommu_hw_ser->hdr.ref_count) >>+ return; >>+ >>+ iommu->iommu_ser = NULL; >>+ iommu->ops->unpreserve(iommu, iommu_hw_ser); >>+ iommu_hw_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED; >>+} >>+ >>+/** >>+ * iommu_preserve_device() - Preserve device state across live update >>+ * @domain: Associated IOMMU domain >>+ * @dev: Device to preserve >>+ * @dma_owner_token: Token to identify DMA owner of this device >>+ * >>+ * Return: 0 on success, or negative error code. >>+ */ >>+int iommu_preserve_device(struct iommu_domain *domain, >>+ struct device *dev, u64 dma_owner_token) >>+{ >>+ struct iommu_device_ser *device_ser; >>+ struct iommu_flb_obj *flb_obj; >>+ struct dev_iommu *iommu; >>+ struct pci_dev *pdev; >>+ int ret; >>+ >>+ if (!dev_is_pci(dev)) >>+ return -EOPNOTSUPP; >>+ >>+ if (!iommu_group_dma_owner_claimed(dev->iommu_group)) >>+ return -EINVAL; >>+ >>+ if (iommu_get_domain_for_dev(dev) != domain) >>+ return -EINVAL; > >It's better to check dma ownership and domain with group->mutex locked. >Perhaps move them to iommu_group_reserve_liveupdate()? Agreed. Will move this to the reserve_liveupdate(). > >>+ >>+ pdev = to_pci_dev(dev); >>+ iommu = dev->iommu; >>+ if (!iommu->iommu_dev->ops->preserve_device || >>+ !iommu->iommu_dev->ops->unpreserve_device || >>+ !iommu->iommu_dev->ops->preserve || >>+ !iommu->iommu_dev->ops->unpreserve) >>+ return -EOPNOTSUPP; > >nit: how aobut > > const struct iommu_ops *ops = dev_iommu_ops(dev); > if (!ops->preserve_device || !ops->unpreserve_device || > !ops->preserve || !ops->unpreserve) > return -EOPNOTSUPP; > >? Looks great. I will update this. > >>+ >>+ ret = iommu_group_reserve_liveupdate(dev->iommu_group); >>+ if (ret) >>+ return ret; >>+ >>+ ret = liveupdate_flb_get_outgoing(&iommu_flb, (void **)&flb_obj); >>+ if (ret) >>+ goto out_unreserve; >>+ >>+ mutex_lock(&flb_obj->lock); >>+ if (!domain->domain_ser) { >>+ ret = -EINVAL; >>+ goto out_unlock; >>+ } >>+ >>+ device_ser = iommu_ser_set_alloc(&flb_obj->device_set); >>+ if (IS_ERR(device_ser)) { >>+ ret = PTR_ERR(device_ser); >>+ goto out_unlock; >>+ } >>+ >>+ ret = iommu_preserve_locked(iommu->iommu_dev, flb_obj); >>+ if (ret) { >>+ device_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED; >>+ goto out_unlock; >>+ } >>+ >>+ device_ser->attachment.domain_phys = virt_to_phys(domain->domain_ser); >>+ device_ser->iommu_phys = virt_to_phys(iommu->iommu_dev->iommu_ser); >>+ device_ser->devid = pci_dev_id(pdev); >>+ device_ser->pci_domain_nr = pci_domain_nr(pdev->bus); >>+ device_ser->dma_owner_token = dma_owner_token; >>+ >>+ ret = iommu->iommu_dev->ops->preserve_device(dev, device_ser); >>+ if (ret) { >>+ device_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED; >>+ iommu_unpreserve_locked(iommu->iommu_dev, flb_obj); >>+ goto out_unlock; >>+ } >>+ >>+ WRITE_ONCE(dev->iommu->device_ser, device_ser); >>+ >>+out_unlock: >>+ mutex_unlock(&flb_obj->lock); >>+ liveupdate_flb_put_outgoing(&iommu_flb); >>+out_unreserve: >>+ if (ret) >>+ iommu_group_unreserve_liveupdate(dev->iommu_group); >>+ return ret; >>+} >>+EXPORT_SYMBOL_GPL(iommu_preserve_device); >>+ >>+/** >>+ * iommu_unpreserve_device() - Unpreserve device state >>+ * @dev: Device to unpreserve >>+ */ >>+void iommu_unpreserve_device(struct device *dev) >>+{ >>+ struct iommu_device_ser *iommu_device_ser; >>+ struct iommu_flb_obj *flb_obj; >>+ struct dev_iommu *iommu; >>+ int ret; >>+ >>+ if (!dev_is_pci(dev)) >>+ return; >>+ >>+ if (!iommu_group_dma_owner_claimed(dev->iommu_group)) >>+ return; > >Nit: iommu_unpreserve_device() is supposed to be called in >preserve_device failure path or after a successful perserve_device(). > >How could above checks be true? If these ever trigger, the preserved >state leaks silently and the group stays reserved forever.Perhaps drop >these checks, or at least make it a WARN_ON()? All your points are valid and yes I will make it a WARN_ON(). > >>+ >>+ iommu = dev->iommu; >>+ if (WARN_ON(!iommu->iommu_dev->ops->unpreserve_device || >>+ !iommu->iommu_dev->ops->unpreserve)) >>+ return; >>+ >>+ ret = liveupdate_flb_get_outgoing(&iommu_flb, (void **)&flb_obj); >>+ if (WARN_ON(ret)) >>+ return; >>+ >>+ mutex_lock(&flb_obj->lock); >>+ iommu_device_ser = dev_iommu_preserved_state(dev); >>+ if (WARN_ON(!iommu_device_ser)) { >>+ ret = -ENOENT; >>+ goto out_unlock; >>+ } >>+ >>+ iommu_device_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED; >>+ iommu->iommu_dev->ops->unpreserve_device(dev, iommu_device_ser); > >Nit: the driver callback gets an entry that is already marked DELETED. >Call ->unpreserve_device() first and then set the flag, the same order >as in iommu_unpreserve_locked(). Agreed. Will update. > >>+ WRITE_ONCE(dev->iommu->device_ser, NULL); >>+ >>+ iommu_unpreserve_locked(iommu->iommu_dev, flb_obj); >>+out_unlock: >>+ mutex_unlock(&flb_obj->lock); >>+ liveupdate_flb_put_outgoing(&iommu_flb); >>+ if (!ret) >>+ iommu_group_unreserve_liveupdate(dev->iommu_group); >>+} >>+EXPORT_SYMBOL_GPL(iommu_unpreserve_device); >>diff --git a/include/linux/iommu-liveupdate.h b/include/linux/iommu-liveupdate.h >>index caa9778eee2d..2d49885b9fcd 100644 >>--- a/include/linux/iommu-liveupdate.h >>+++ b/include/linux/iommu-liveupdate.h >>@@ -8,6 +8,7 @@ >> #ifndef _LINUX_IOMMU_LIVEUPDATE_H >> #define _LINUX_IOMMU_LIVEUPDATE_H >>+#include >> #include >> #include >> #include >>@@ -15,8 +16,58 @@ >> #ifdef CONFIG_IOMMU_LIVEUPDATE >> int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler); >> void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler); >>+ >>+/** >>+ * dev_iommu_has_preserved_state() - Check if a device has live update state >>+ * @dev: Target device >>+ * >>+ * Unlike dev_iommu_preserved_state() and dev_iommu_restored_state(), this does >>+ * not dereference the serialized state, so it is safe to call without holding >>+ * a reference on the live update FLB. >>+ * >>+ * Return: true if @dev has preserved (outgoing) or restored (incoming) state. >>+ */ >>+static inline bool dev_iommu_has_preserved_state(struct device *dev) >>+{ >>+ return dev->iommu && READ_ONCE(dev->iommu->device_ser); >>+} > >Is this used anywhere? I couldn't find a caller in the tree. Clean it >up? I will remove this. > >>+ >>+/** >>+ * dev_iommu_preserved_state() - Get preserved state of a device >>+ * @dev: Target device >>+ * >>+ * Return: Pointer to preserved device state, or NULL if not preserved. >>+ */ >>+static inline void *dev_iommu_preserved_state(struct device *dev) >>+{ >>+ struct iommu_device_ser *ser; >>+ >>+ if (!dev->iommu) >>+ return NULL; >>+ >>+ ser = READ_ONCE(dev->iommu->device_ser); >>+ if (ser && !(ser->hdr.flags & IOMMU_SER_FLAG_INCOMING)) >>+ return ser; >>+ >>+ return NULL; >>+} > >nit: why not returning "struct iommu_device_ser *" instead of "void *"? I will update this and return struct iommu_device_ser *. > >>+ >> int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser); >> void iommu_unpreserve_domain(struct iommu_domain *domain); >>+int iommu_preserve_device(struct iommu_domain *domain, >>+ struct device *dev, u64 dma_owner_token); >>+void iommu_unpreserve_device(struct device *dev); >>+ >>+/** >>+ * iommu_preserved_state() - Get preserved state of an IOMMU instance >>+ * @iommu: IOMMU hardware instance >>+ * >>+ * Return: Pointer to preserved state, or NULL if not preserved. >>+ */ >>+static inline void *iommu_preserved_state(struct iommu_device *iommu) >>+{ >>+ return iommu->iommu_ser; >>+} > >READ_ONCE()/WRITE_ONCE() used for dev->iommu->device_ser, but not here. >Please handle it consistently. Will update. > >> #else >> static inline int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler) >> { >>@@ -27,6 +78,16 @@ static inline void iommu_liveupdate_unregister_flb(struct liveupdate_file_handle >> { >> } >>+static inline bool dev_iommu_has_preserved_state(struct device *dev) >>+{ >>+ return false; >>+} >>+ >>+static inline void *dev_iommu_preserved_state(struct device *dev) >>+{ >>+ return NULL; >>+} >>+ >> static inline int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser) >> { >> return -EOPNOTSUPP; >>@@ -35,5 +96,20 @@ static inline int iommu_preserve_domain(struct iommu_domain *domain, struct iomm >> static inline void iommu_unpreserve_domain(struct iommu_domain *domain) >> { >> } >>+ >>+static inline int iommu_preserve_device(struct iommu_domain *domain, >>+ struct device *dev, u64 dma_owner_token) >>+{ >>+ return -EOPNOTSUPP; >>+} >>+ >>+static inline void iommu_unpreserve_device(struct device *dev) >>+{ >>+} >>+ >>+static inline void *iommu_preserved_state(struct iommu_device *iommu) >>+{ >>+ return NULL; >>+} >> #endif >> #endif /* _LINUX_IOMMU_LIVEUPDATE_H */ >>diff --git a/include/linux/iommu.h b/include/linux/iommu.h >>index 77f78c88c627..c44f7d1c8498 100644 >>--- a/include/linux/iommu.h >>+++ b/include/linux/iommu.h >>@@ -684,6 +684,10 @@ __iommu_copy_struct_to_user(const struct iommu_user_data *dst_data, >> * resources shared/passed to user space IOMMU instance. Associate >> * it with a nesting @parent_domain. It is required for driver to >> * set @viommu->ops pointing to its own viommu_ops >>+ * @preserve_device: Preserve state of a device for liveupdate. >>+ * @unpreserve_device: Unpreserve state that was preserved earlier. >>+ * @preserve: Preserve state of iommu translation hardware for liveupdate. >>+ * @unpreserve: Unpreserve state of iommu that was preserved earlier. >> * @owner: Driver module providing these ops >> * @identity_domain: An always available, always attachable identity >> * translation. >>@@ -740,6 +744,13 @@ struct iommu_ops { >> struct iommu_domain *parent_domain, >> const struct iommu_user_data *user_data); >>+#ifdef CONFIG_IOMMU_LIVEUPDATE >>+ int (*preserve_device)(struct device *dev, struct iommu_device_ser *device_ser); >>+ void (*unpreserve_device)(struct device *dev, struct iommu_device_ser *device_ser); >>+ int (*preserve)(struct iommu_device *iommu, struct iommu_hw_ser *iommu_ser); >>+ void (*unpreserve)(struct iommu_device *iommu, struct iommu_hw_ser *iommu_ser); >>+#endif > >nit: How about preserve_iommu/unpreserve_iommu, to match >preserve_device/unpreserve_device? Yes that looks good. I will update this. > >>+ >> const struct iommu_domain_ops *default_domain_ops; >> struct module *owner; >> struct iommu_domain *identity_domain; >>@@ -829,6 +840,7 @@ struct iommu_domain_ops { >> * @singleton_group: Used internally for drivers that have only one group >> * @max_pasids: number of supported PASIDs >> * @ready: set once iommu_device_register() has completed successfully >>+ * @iommu_ser: preserved iommu state of outgoing kernel for liveupdate. >> */ >> struct iommu_device { >> struct list_head list; >>@@ -838,6 +850,10 @@ struct iommu_device { >> struct iommu_group *singleton_group; >> u32 max_pasids; >> bool ready; >>+ >>+#ifdef CONFIG_IOMMU_LIVEUPDATE >>+ struct iommu_hw_ser *iommu_ser; >>+#endif >> }; >> /** >>@@ -877,6 +893,8 @@ struct iommu_fault_param { >> * @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs >> * @require_direct: device requires IOMMU_RESV_DIRECT regions >> * @shadow_on_flush: IOTLB flushes are used to sync shadow tables >>+ * @device_ser: preserved (outgoing) or restored (incoming) liveupdate state >>+ * of this device >> * >> * TODO: migrate other per device data pointers under iommu_dev_data, e.g. >> * struct iommu_group *iommu_group; >>@@ -892,6 +910,9 @@ struct dev_iommu { >> u32 pci_32bit_workaround:1; >> u32 require_direct:1; >> u32 shadow_on_flush:1; >>+#ifdef CONFIG_IOMMU_LIVEUPDATE >>+ struct iommu_device_ser *device_ser; >>+#endif >> }; >> int iommu_device_register(struct iommu_device *iommu, > >Thanks, >baolu Thanks, Sami