From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.14 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796437162; b=G3jL1bp3RX/QCYHONf0AN5+n6BZqMKmSbR+HTBCn8jmNhl2ByPoFAU2KYzkyHfNtRSsq s1SGr4WcdjJt9pfbm+Hn6IByXcfxVA8vjTBLf4Ydo4ReAdGxf60W64BZcU/BEZ8rSLNh0 lZ3ARYeKDS16jMzaXIki1AV49CVr9MIWyuwuRP5SMSGHCSRjcN8eM9g1lxO9qb2oZOxyi uHCRRkatCziZcwdR43OLYKLgazPByJhiF4yWXqgt5DYyhHdLHqEauU8Ds8wqMDXc1lVHe +omH6yk/WSbGUi+78FXhBtfrtYavklEc7P/vLLnwGscdlYo/c+v3+8e9J/EJi9rex2Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796437162; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Message-ID:Date: MIME-Version:User-Agent:Cc:Subject:To:References:Content-Language: From:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=UY3qG8kqyjzA7t1TzFuHyYfrY+nuIR9b+vDT/giNllA=; b=x2/kvUfughGiXXGcmMgd5JjNHGTZhSDMXlSE4AKB74wFt6Kf+bOt2CHHcCcuc37i4VCm qUtlx4oLi0PCCE5LprB8bhoMXd27F+Nbr7s1IFaxS/d7t9PhJt+DsGVbrevTkVidgTySZ xEi3p/jl5wofP/k57lDI/IJDz3vsx0u+Lmo4Yeu0DEKfy7/Kft2Lhiw2GO+6MhLaTctdk nxEcZu5CMyD8Qfer0FSCwv0ZuxteeJEJXTEhzUkEGsgewv9bo4Luaan0ShsLJa6UcmW1R xUxp721Nnn1vkIkLneXNAqXWBG9gq/TteCWIxk6/8TuruSzQlx76kTNoT1GvSPKqMrw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=XqJYJ7CL; arc=none smtp.remote-ip=192.198.163.14 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.14]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88632606A32F; Sat, 5 Dec 2026 02:19:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 88632606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=XqJYJ7CL DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796437161; x=1827973161; h=message-id:date:mime-version:cc:subject:to:references: from:in-reply-to:content-transfer-encoding; bh=UY3qG8kqyjzA7t1TzFuHyYfrY+nuIR9b+vDT/giNllA=; b=XqJYJ7CLBdexmNz679JygaHKeAaVhhivbFn8q5tnglgZoh+N5l6hACZX cFik9VTDxMBlo5s1bkSI+xTdyKEKXySXpw+fLfvM80asTV3hLaTPeEX43 E5n7ek3AEoHXJi4Powf0O7l/Dp0+5EfJt+55MVClqz7aPom1nPRYPhT5K kk+qkDGnx4ewviTT1o1fj4lWvQ4ImhwJQod8TQYeOWsSJlf4zd8BPUujp Cmh2zNWbzQYuXMxFlmjhiC+W1OhW+qVMZcfVEgb1nyTurenv+OXvXr7EL c+IBfXzqKEh2Z/5WlXDLXdEfL9xTjy2kPU3q7YgKr2ynmvg9KQJrkbHMp g==; X-CSE-ConnectionGUID: GCp75II8Qgya8lB1Ntp/Qw== X-CSE-MsgGUID: ef2rlACVRTqFIetQdarAsA== X-IronPort-AV: E=McAfee;i="6800,10657,11986"; a="5406670" X-IronPort-AV: E=Sophos;i="6.29,79,1794902400"; d="scan'208";a="5406670" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmvoesa108.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 18:19:20 -0800 X-CSE-ConnectionGUID: X0aeOggARhOUy0+2EkbrFQ== X-CSE-MsgGUID: yi5f4g+CR067Hk1QRKni7Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,79,1794902400"; d="scan'208";a="14476874" Received: from blu2-mobl.ccr.corp.intel.com (HELO [10.124.249.52]) ([10.124.249.52]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 18:19:16 -0800 Message-ID: Date: Sat, 5 Dec 2026 10:19:13 +0800 X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Cc: baolu.lu@linux.intel.com, Pranjal Shrivastava , 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 , Vipin Sharma , Ankit Soni , Nicolin Chen , John Starks Subject: Re: [PATCH v8 17/18] iommufd: Add APIs to preserve/unpreserve a vfio cdev To: Samiullah Khawaja , Joerg Roedel , Will Deacon , Jason Gunthorpe , David Woodhouse References: <20261123044949.3610702-1-skhawaja@google.com> <20261123044949.3610702-18-skhawaja@google.com> Content-Language: en-US From: Baolu Lu In-Reply-To: <20261123044949.3610702-18-skhawaja@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 11/23/2026 12:49 PM, Samiullah Khawaja wrote: > Add APIs that can be used to preserve and unpreserve a vfio cdev. Use > the APIs exported by the IOMMU core to preserve/unpreserve device. > > The LUO token of the preserved iommufd is fetched and returned back to > the caller as that can be used during restore to get the restored > iommufd. > > Signed-off-by: Samiullah Khawaja > Reviewed-by: Pranjal Shrivastava > --- > drivers/iommu/iommufd/device.c | 154 ++++++++++++++++++++++++ > drivers/iommu/iommufd/iommufd_private.h | 3 + > drivers/iommu/iommufd/liveupdate.c | 26 ++++ > include/linux/iommufd.h | 27 +++++ > 4 files changed, 210 insertions(+) > > diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c > index 1b0bdf57ac8a..ebfc96445f71 100644 > --- a/drivers/iommu/iommufd/device.c > +++ b/drivers/iommu/iommufd/device.c > @@ -2,6 +2,7 @@ > /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES > */ > #include > +#include > #include > #include > #include > @@ -686,6 +687,10 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, > int rc; > > mutex_lock(&igroup->lock); > + if (iommufd_device_is_preserved(idev)) { > + rc = -EBUSY; > + goto err_unlock; > + } > > attach = xa_cmpxchg(&igroup->pasid_attach, pasid, NULL, > XA_ZERO_ENTRY, GFP_KERNEL); > @@ -869,6 +874,10 @@ iommufd_device_do_replace(struct iommufd_device *idev, ioasid_t pasid, > int rc; > > mutex_lock(&igroup->lock); > + if (iommufd_device_is_preserved(idev)) { > + rc = -EBUSY; > + goto err_unlock; > + } > > attach = xa_load(&igroup->pasid_attach, pasid); > if (!attach) { > @@ -1135,6 +1144,9 @@ void iommufd_device_detach(struct iommufd_device *idev, ioasid_t pasid) > { > struct iommufd_hw_pagetable *hwpt; > > + if (pasid == IOMMU_NO_PASID) > + iommufd_device_unpreserve(idev); Could you add a comment explaining why detaching IOMMU_NO_PASID silently unpreserves the device? > + > hwpt = iommufd_hw_pagetable_detach(idev, pasid); This also races with iommufd_device_preserve(). igroup->lock is dropped between iommufd_device_unpreserve() and iommufd_hw_pagetable_detach(), so a concurrent iommufd_device_preserve() can slip into that window, see the device still attached, and succeed. The device then ends up detached while still marked as preserved. > if (!hwpt) > return; > @@ -1749,3 +1761,145 @@ int iommufd_get_hw_info(struct iommufd_ucmd *ucmd) > iommufd_put_object(ucmd->ictx, &idev->obj); > return rc; > } > + > +#ifdef CONFIG_IOMMU_LIVEUPDATE > +static bool _iommufd_device_has_pasid_attachments(struct iommufd_device *idev) > +{ > + struct iommufd_group *igroup = idev->igroup; > + unsigned long start = IOMMU_NO_PASID; > + > + if (xa_find_after(&igroup->pasid_attach, &start, UINT_MAX, XA_PRESENT)) > + return true; > + > + return false; Nit: this can simply be return xa_find_after(...) != NULL; ? Also, how about dropping the leading underscore. There is no non- underscore version of this helper. > +} > + > +/** > + * iommufd_device_preserve() - Preserve an iommufd device across live update > + * @s: Live update session > + * @idev: Target iommufd device > + * @iommufd_tokenp: Pointer to store outgoing iommufd token > + * > + * Return: 0 on success, or negative error code. > + */ > +int iommufd_device_preserve(struct liveupdate_session *s, > + struct iommufd_device *idev, > + u64 *iommufd_tokenp) > +{ > + struct iommufd_hwpt_paging *hwpt_paging; > + struct iommufd_hw_pagetable *hwpt; > + struct iommufd_attach *attach; > + struct iommufd_group *igroup; > + int ret; > + > + if (!idev) > + return -EINVAL; > + > + if (iommufd_device_is_noiommu(idev)) > + return -EOPNOTSUPP; > + > + igroup = idev->igroup; > + mutex_lock(&igroup->lock); > + if (idev->liveupdate_preserved) { > + ret = -EBUSY; > + goto out; > + } > + > + if (_iommufd_device_has_pasid_attachments(idev)) { > + ret = -EOPNOTSUPP; > + goto out; > + } > + > + attach = xa_load(&igroup->pasid_attach, IOMMU_NO_PASID); > + if (!attach) { > + ret = -ENOENT; > + goto out; > + } > + > + if (!xa_load(&attach->device_array, idev->obj.id)) { > + ret = -ENOENT; > + goto out; > + } > + > + hwpt = attach->hwpt; > + if (!hwpt_is_paging(hwpt)) { > + ret = -EOPNOTSUPP; > + goto out; > + } > + > + hwpt_paging = to_hwpt_paging(hwpt); > + ret = liveupdate_get_token_outgoing(s, idev->ictx->file, iommufd_tokenp); > + if (ret) > + goto out; > + > + if (!hwpt_paging->liveupdate_preserved) { > + ret = -EINVAL; > + goto out; > + } > + > + ret = iommu_preserve_device(hwpt_paging->common.domain, > + idev->dev, > + *iommufd_tokenp); > + > + if (!ret) > + idev->liveupdate_preserved = true; > +out: > + mutex_unlock(&igroup->lock); > + return ret; > +} > +EXPORT_SYMBOL_NS_GPL(iommufd_device_preserve, "IOMMUFD"); > + > +/** > + * iommufd_device_unpreserve() - Unpreserve an iommufd device > + * @idev: Target iommufd device > + */ > +void iommufd_device_unpreserve(struct iommufd_device *idev) > +{ > + struct iommufd_hwpt_paging *hwpt_paging; > + struct iommufd_hw_pagetable *hwpt; > + struct iommufd_attach *attach; > + struct iommufd_group *igroup; > + > + if (!idev) > + return; > + > + igroup = idev->igroup; > + mutex_lock(&igroup->lock); > + if (!idev->liveupdate_preserved) > + goto out; > + > + attach = xa_load(&igroup->pasid_attach, IOMMU_NO_PASID); > + if (!attach) { > + WARN(1, "IOMMU_NO_PASID attachment not found"); > + goto out; > + } > + > + hwpt = attach->hwpt; > + if (WARN(!hwpt_is_paging(hwpt), "Attached HWPT is not a paging HWPT")) > + goto out; > + > + hwpt_paging = to_hwpt_paging(hwpt); > + if (!hwpt_paging->liveupdate_preserved) { > + WARN(1, "Attached domain is not preserved"); > + goto out; > + } > + > + iommu_unpreserve_device(idev->dev); > + idev->liveupdate_preserved = false; > +out: > + mutex_unlock(&igroup->lock); > +} > +EXPORT_SYMBOL_NS_GPL(iommufd_device_unpreserve, "IOMMUFD"); > + > +/** > + * iommufd_device_is_preserved() - Check if an iommufd device is preserved > + * @idev: Target iommufd device > + * > + * Return: true if preserved, false otherwise. > + */ > +bool iommufd_device_is_preserved(struct iommufd_device *idev) > +{ > + return idev && idev->liveupdate_preserved; > +} > +EXPORT_SYMBOL_NS_GPL(iommufd_device_is_preserved, "IOMMUFD"); > +#endif [...] > diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h > index 3087f5b2def8..1ad2a21cfcdf 100644 > --- a/include/linux/iommufd.h > +++ b/include/linux/iommufd.h > @@ -9,6 +9,7 @@ > #include > #include > #include > +#include Nit: Would a forward declaration of "struct liveupdate_session;" be enough here? There seems to be no need to pull liveupdate.h into every user of linux/iommufd.h. > #include > #include > #include > @@ -215,6 +216,14 @@ int iommufd_access_rw(struct iommufd_access *access, unsigned long iova, > int iommufd_vfio_compat_ioas_get_id(struct iommufd_ctx *ictx, u32 *out_ioas_id); > int iommufd_vfio_compat_ioas_create(struct iommufd_ctx *ictx); > int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx); > + > +#ifdef CONFIG_IOMMU_LIVEUPDATE > +int iommufd_device_preserve(struct liveupdate_session *s, > + struct iommufd_device *idev, > + u64 *iommufd_tokenp); > +void iommufd_device_unpreserve(struct iommufd_device *idev); > +bool iommufd_device_is_preserved(struct iommufd_device *idev); > +#endif > #else /* !CONFIG_IOMMUFD */ > static inline struct iommufd_ctx *iommufd_ctx_from_file(struct file *file) > { > @@ -399,4 +408,22 @@ static inline void iommufd_viommu_destroy_mmap(struct iommufd_viommu *viommu, > { > _iommufd_destroy_mmap(viommu->ictx, &viommu->obj, offset); > } > + > +#if !IS_ENABLED(CONFIG_IOMMU_LIVEUPDATE) || !IS_ENABLED(CONFIG_IOMMUFD) > +static inline int iommufd_device_preserve(struct liveupdate_session *s, > + struct iommufd_device *idev, > + u64 *iommufd_tokenp) > +{ > + return -EOPNOTSUPP; > +} > + > +static inline void iommufd_device_unpreserve(struct iommufd_device *idev) > +{ > +} > + > +static inline bool iommufd_device_is_preserved(struct iommufd_device *idev) > +{ > + return false; > +} > +#endif > #endif Thanks, baolu