From nobody Mon Feb 9 06:48:26 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 5DC073815C4 for ; Tue, 3 Feb 2026 22:10:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770156612; cv=none; b=gNmzK4iBWdLwwOYxS0EtM3H8CrHBnY5ZSkRV3KAn45SOgd+eJ20AmAOeYEx7YAHl+sZ1oU5cw0lN/6eq0gXlG9FGniE41daZtbLAuoGlQds7JJZ0dMffSD8BnNXsX+GU/q+wa0HUvV0f5sGyrOoJg4cpYoA9iJEfQhGbeqJ+RtA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770156612; c=relaxed/simple; bh=kib+TGW+Sn0wIp4wEuCVzIXogMh8yQTFjwVFCEPYhmY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=i0AE3hWbQhUInavUqKVMjk7dhPnAMUVG2r4LZRMEgMe/pubVpcinNKWzBFy2lCuzcMkpMB2e1pctNWzqn8YASvDzOKbrn9M2/tssLo+Oy6911l/OS6EaCx++LE8D0wGbjbWYjzSK+yW/l+yMcUgRRY6ExgudCF8XvBaOTNMKyww= 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=yiWuZSL9; arc=none smtp.client-ip=209.85.216.73 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="yiWuZSL9" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3545cc84ab1so7281153a91.0 for ; Tue, 03 Feb 2026 14:10:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770156609; x=1770761409; 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=cqqC2kaW5ecj2f4ECaQ1IWiF4359d2X2a1SnW9XYDSk=; b=yiWuZSL9Ap91iA5bMFdovEO9BPEwpndAd8N/1aB+qK5EDBFG/4Y/T0yxQQkB/QYekm 4nY+W2kGtDRy9X6kaOKPM4nhZo0qyARf3Esk8D6AXESAc2AEtsf0rzcWxDFBj8/g0Vb/ 8amiBC5LlhHUuhehZYUiGHyqu/DvItGh+9gVddAzhc0fSuWDYKyEyYh1vMOCYzKt+IyQ QLB+Xln7Fa0JukUiXpA037Yc4jfW4qtF7RRQgcrdg9DnARBEd4RhOWIBafbjT3yfEWQd SOr+1PwaXJJrUOd943ykhUWWPnLvNKkA12E+jTmZuXCPtAptL0pKBGXi9cJJ/blwF4RS dCbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770156609; x=1770761409; 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=cqqC2kaW5ecj2f4ECaQ1IWiF4359d2X2a1SnW9XYDSk=; b=KlBWnHMeWhQOJwFnrBb1tzCj631JWPrynU+HmyJuh52LO2WsyEJd/N0gkxRIx/WQc1 0M7v5qc8yXgtoBYmHn5m+l+pfQTiN+owo0rQZltSrymQuJocJjofs+2S0+q2uWO0+DJ3 y/1ub8zII3mtEfRFqaQ8ZqRxLWP7EM858vB2HVif8hXBM/V2oHuGa2h8qsqgVZAxmMx5 odZOWghKor22IZhsfBvOit7P2Pn00/dTAZ1acYeMcWXXIjEkzZTBRGQ33AiHg+rnmGdJ +2mY05uXAtEqg9KhEDt6lai2wBvM5slRNJpR6ZvFCZmvA+Ovbxy7jfd21hXzEbBldnAX aaqw== X-Forwarded-Encrypted: i=1; AJvYcCUoSsrsYaucxGkk9RFAhKTuP4bE2PNx9vmSLW1WmvSdCsoXwu3/19B+GA9WAZl7yCTnRG8d/eqzsUPDecc=@vger.kernel.org X-Gm-Message-State: AOJu0YxL8nzFLMLFybFvKv63xC/vZwPUbrBV4SQatzhckLmH0CY9FA63 6dDs5l9IJ0jN/a1c1e8H7NgKferL766MZ1GWSPApv1DxQgGepZqNhPVzBWjHEJV4Qjm0LYipiag LIHNgdvgIJ9UAsQ== X-Received: from pjpo9.prod.google.com ([2002:a17:90a:9f89:b0:34c:f8b8:349b]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:e70d:b0:34c:a35d:de19 with SMTP id 98e67ed59e1d1-354871ea85fmr658805a91.33.1770156609486; Tue, 03 Feb 2026 14:10:09 -0800 (PST) Date: Tue, 3 Feb 2026 22:09:46 +0000 In-Reply-To: <20260203220948.2176157-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: <20260203220948.2176157-1-skhawaja@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260203220948.2176157-13-skhawaja@google.com> Subject: [PATCH 12/14] iommufd: Add APIs to preserve/unpreserve a 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, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Pratyush Yadav , Pasha Tatashin , David Matlack , Andrew Morton , Chris Li , Pranjal Shrivastava , Vipin Sharma , YiFei Zhu Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Pass the LUO preservation token of the attached iommufd into IOMMU preserve device API. This establishes the ownership of the device with the preserved iommufd. Signed-off-by: Samiullah Khawaja --- drivers/iommu/iommufd/device.c | 69 ++++++++++++++++++++++++++++++++++ include/linux/iommufd.h | 23 ++++++++++++ 2 files changed, 92 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 4c842368289f..30cb5218093b 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 @@ -1661,3 +1662,71 @@ int iommufd_get_hw_info(struct iommufd_ucmd *ucmd) iommufd_put_object(ucmd->ictx, &idev->obj); return rc; } + +#ifdef CONFIG_IOMMU_LIVEUPDATE +int iommufd_device_preserve(struct liveupdate_session *s, + struct iommufd_device *idev, + u64 *tokenp) +{ + struct iommufd_group *igroup =3D idev->igroup; + struct iommufd_hwpt_paging *hwpt_paging; + struct iommufd_hw_pagetable *hwpt; + struct iommufd_attach *attach; + int ret; + + mutex_lock(&igroup->lock); + attach =3D xa_load(&igroup->pasid_attach, IOMMU_NO_PASID); + if (!attach) { + ret =3D -ENOENT; + goto out; + } + + hwpt =3D attach->hwpt; + hwpt_paging =3D find_hwpt_paging(hwpt); + if (!hwpt_paging || !hwpt_paging->lu_preserve) { + ret =3D -EINVAL; + goto out; + } + + ret =3D liveupdate_get_token_outgoing(s, idev->ictx->file, tokenp); + if (ret) + goto out; + + ret =3D iommu_preserve_device(hwpt_paging->common.domain, + idev->dev, + *tokenp); +out: + mutex_unlock(&igroup->lock); + return ret; +} +EXPORT_SYMBOL_NS_GPL(iommufd_device_preserve, "IOMMUFD"); + +void iommufd_device_unpreserve(struct liveupdate_session *s, + struct iommufd_device *idev, + u64 token) +{ + struct iommufd_group *igroup =3D idev->igroup; + struct iommufd_hwpt_paging *hwpt_paging; + struct iommufd_hw_pagetable *hwpt; + struct iommufd_attach *attach; + + mutex_lock(&igroup->lock); + attach =3D xa_load(&igroup->pasid_attach, IOMMU_NO_PASID); + if (!attach) { + WARN_ON(-ENOENT); + goto out; + } + + hwpt =3D attach->hwpt; + hwpt_paging =3D find_hwpt_paging(hwpt); + if (!hwpt_paging || !hwpt_paging->lu_preserve) { + WARN_ON(-EINVAL); + goto out; + } + + iommu_unpreserve_device(hwpt_paging->common.domain, idev->dev); +out: + mutex_unlock(&igroup->lock); +} +EXPORT_SYMBOL_NS_GPL(iommufd_device_unpreserve, "IOMMUFD"); +#endif diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 6e7efe83bc5d..c4b3ed5b518c 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -71,6 +72,28 @@ void iommufd_device_detach(struct iommufd_device *idev, = ioasid_t pasid); struct iommufd_ctx *iommufd_device_to_ictx(struct iommufd_device *idev); u32 iommufd_device_to_id(struct iommufd_device *idev); =20 +#ifdef CONFIG_IOMMU_LIVEUPDATE +int iommufd_device_preserve(struct liveupdate_session *s, + struct iommufd_device *idev, + u64 *tokenp); +void iommufd_device_unpreserve(struct liveupdate_session *s, + struct iommufd_device *idev, + u64 token); +#else +static inline int iommufd_device_preserve(struct liveupdate_session *s, + struct iommufd_device *idev, + u64 *tokenp) +{ + return -EOPNOTSUPP; +} + +static inline void iommufd_device_unpreserve(struct liveupdate_session *s, + struct iommufd_device *idev, + u64 token) +{ +} +#endif + struct iommufd_access_ops { u8 needs_pin_pages : 1; void (*unmap)(void *data, unsigned long iova, unsigned long length); --=20 2.53.0.rc2.204.g2597b5adb4-goog