From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794283730; b=SbN31FWRy9mUo+pk8NJGc9Pw8KpOhIB0BFoRf83UEpx2xxnTo4xGAZbarspiGW2c6GzF NIQ2Et/aXgIAqn8+zkiBGw2W5Yw8Fo5WW60QmrlTLvOLcu+9wxZzmYhAb4aB8Aqe0vspy ZV6rXH8W99RnvCF66FQmHEA/1U9JHurC+VIDDvt1YCsnvp8xf/ksGeFFlib1MnChst6Us lW/8Qfa7YptjOZC2L+WnG5mLOLFns95czqS/eSjgv/G5Wy/l5OFUrDXxkPWLuppZtRygH 1UaUimKcWaVMtgXvVJSXeUnL/ApOSlwxXK4Es2pDr5SPYai5VzO3xruJG/2T1vN5OYA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794283730; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=ORcr20MtpC9CS4uldIm6EOxjXnmzk1t05YkaOp2xyik=; b=LfAwlkf0ik/SnMRVIX1oz0ZZs4OI28Xk2+5RHfcaecff2R/3RwMOLb0EIk/OL68x1W7K YJRMASL7zujFCqaxhf6Hiq0sPybhTLhPPtJ0IB2PFKBeX9ypgCmDs2vd8jL5hp+NNHl+N 3B44+B5AG+eH4OOkX+ZfE1rJAcq4IgzFaAJfxCu690+QpMTJ6WfgF+TrALgx/Shfl96Bk zwzq5ywy86ClXb0ygEhZihwTQ1EFDntnf7kH5Wp4CF4sfx1cO6+058KxPRog2heSGwEOt jU/9VA79YrC4H4R2YuHrG+sXbVIErmjwk66BzW4+IS47zg5mnE5Nm457C9hKKO15Gfg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=WNYOjyOH; arc=none smtp.remote-ip=209.85.167.200 Received: from mail-oi1-f200.google.com (mail-oi1-f200.google.com [209.85.167.200]) (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 9448C606B7DD for ; Tue, 10 Nov 2026 04:08:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9448C606B7DD 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.a=rsa-sha256 header.s=20260707 header.b=WNYOjyOH Received: by mail-oi1-f200.google.com with SMTP id 5614622812f47-567ac848d83so2053578b6e.3 for ; Mon, 09 Nov 2026 20:08:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1794283728; x=1794888528; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ORcr20MtpC9CS4uldIm6EOxjXnmzk1t05YkaOp2xyik=; b=WNYOjyOHZP7wevBEv6/i2TGnCUq9hQpLU8ojATWnUvSggBZAybkMPZk648qrwJhnHz R7EDq3bhLOs5VUa46GRmkLhwsXCEBY/XL9cMgUQL8AWdaPThJRdfTKkn5PTtKJa438ka HiE1SA1nZtFhPulKKura9bMIz4rwnDzv8NFwYBcfJE8yy/VwyvxYIH8XwtOpuQQH6bnw gS+D/ZM+AIjtDCZ2B3akgdRa8v5i1WmiYK7ZnxTUcAZJzTUZ8l8b22dEWbWxcV3XxEWZ /NBBumqC3MEoVrEAdWsg+dtWj3qL6VuoLJBbQu1kNkhcfXC5Xc9SKcsn7XLDThQi4OGR kjTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794283728; x=1794888528; h=content-type: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:content-type; bh=ORcr20MtpC9CS4uldIm6EOxjXnmzk1t05YkaOp2xyik=; b=FkbZpvN2DLGUqZyWSQ/jhITvJg/WJG0purYSmilWYKScFE2QnZ2BSZk/G/EWJwGKjL zVWkKGSuHERWkp4zKD1lowH1wnrsUT4xGD0fz2qWg6FWRHREFw0BKLVe0eunXsHAEMz4 iYb808kb2k2IbP4fhOd+h2b4jrUWF+Fane5oEPBj3U/1vkVI5uRxpujoHNxJAAPgZRux S8R5Jy3W8PEaq0J3pAXMM/vfDHVG0V7x6S82paJFOdRspWyCbsLTQmFL4cu22CS3NlX2 iYy2/ZsU61SoZxid32M73gXqSsPAI/gzkmKdvvMmT1+T7VA+XKSa1F25Vf72Cuu7ELhr 6Gug== X-Forwarded-Encrypted: i=1; AL+hErEKejZgUFv1FLA7b87+5jvhQxQs4QfGJ7xbxPpwAaYjnJhHesiYRy+yFpAQhUSPhJ9fWVTQzuRkzmhg+Qg=@vger.kernel.org X-Gm-Message-State: APuU4Z8O66ZpolHO6P4cRMr8y6OClGu4Dg2JsaUtelIaQXQlz8t2ykY8 9bFkg9MirAF1y4T9rmnyakpTmkd+OMrCj5VeDxwrdlUuuE9HKWk9Rylgt1Q5Mc7lPTvc1Ahbzo8 zmLg5NT4g64EoGQ== X-Received: from iocow14-n2.prod.google.com ([2002:a05:6602:c5ce:20b0:9c4:1f4b:e117]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:308a:b0:4c5:d5b0:4062 with SMTP id 5614622812f47-5683cbf007dmr1307568b6e.21.1794283728166; Mon, 09 Nov 2026 20:08:48 -0800 (PST) Date: Tue, 10 Nov 2026 04:08:21 +0000 In-Reply-To: <20261110040828.1774343-1-skhawaja@google.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 References: <20261110040828.1774343-1-skhawaja@google.com> X-Mailer: git-send-email 2.56.0.506.g506a7eabc5-goog Message-ID: <20261110040828.1774343-16-skhawaja@google.com> Subject: [PATCH v7 15/18] iommufd: Persist iommu hardware pagetables for live update From: Samiullah Khawaja To: Joerg Roedel , Will Deacon , Lu Baolu , Jason Gunthorpe , David Woodhouse Cc: YiFei Zhu , 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 , Ankit Soni , Nicolin Chen , John Starks Content-Type: text/plain; charset="UTF-8" From: YiFei Zhu Register iommufd with the LUO framework and implement the preserve and unpreserve ops to save marked HWPTs. To make sure mappings do not change during preserved state, add a liveupdate_immutable flag to IOAS. When an HWPT is preserved, its IOAS is marked immutable and any map/unmap attempts will fail with -EBUSY. This is synchronized using the domains_rwsem to prevent races with concurrent mapping operations. The preserve callback iterates over the marked HWPTs, verifies that the backing memory pages are preserved, and calls iommu_preserve_domain() to preserve the associated IOMMU domain. Signed-off-by: YiFei Zhu Signed-off-by: Samiullah Khawaja --- MAINTAINERS | 1 + drivers/iommu/iommufd/io_pagetable.c | 24 ++ drivers/iommu/iommufd/iommufd_private.h | 16 ++ drivers/iommu/iommufd/liveupdate.c | 344 ++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 10 +- include/linux/kho/abi/iommufd.h | 56 ++++ 6 files changed, 450 insertions(+), 1 deletion(-) create mode 100644 include/linux/kho/abi/iommufd.h diff --git a/MAINTAINERS b/MAINTAINERS index 336567b09855..a54c7689f1a8 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14037,6 +14037,7 @@ F: drivers/iommu/iommufd/liveupdate.c F: drivers/iommu/liveupdate.c F: include/linux/iommu-liveupdate.h F: include/linux/kho/abi/iommu.h +F: include/linux/kho/abi/iommufd.h IOMMUFD M: Jason Gunthorpe diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index 5ac736cf5bb5..01d0613ad617 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -30,6 +30,19 @@ struct iopt_pages_list { unsigned long length; }; +static inline bool iopt_liveupdate_immutable(const struct io_pagetable *iopt) +{ + bool immutable = false; + + lockdep_assert_held(&iopt->domains_rwsem); + +#ifdef CONFIG_IOMMU_LIVEUPDATE + /* iopt becomes immutable once liveupdate preservation starts */ + immutable = iopt->num_preserved_domains; +#endif + return immutable; +} + struct iopt_area *iopt_area_contig_init(struct iopt_area_contig_iter *iter, struct io_pagetable *iopt, unsigned long iova, @@ -384,6 +397,11 @@ int iopt_map_pages(struct io_pagetable *iopt, struct list_head *pages_list, return rc; down_read(&iopt->domains_rwsem); + if (iopt_liveupdate_immutable(iopt)) { + rc = -EBUSY; + goto out_unlock_domains; + } + rc = iopt_fill_domains_pages(pages_list); if (rc) goto out_unlock_domains; @@ -758,6 +776,12 @@ static int iopt_unmap_iova_range(struct io_pagetable *iopt, unsigned long start, again: down_read(&iopt->domains_rwsem); down_write(&iopt->iova_rwsem); + + if (iopt_liveupdate_immutable(iopt)) { + rc = -EBUSY; + goto out_unlock_iova; + } + while ((area = iopt_area_iter_first(iopt, start, last))) { unsigned long area_last = iopt_area_last_iova(area); unsigned long area_first = iopt_area_iova(area); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index a33b32708afa..69d902b69b82 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -98,6 +98,9 @@ struct io_pagetable { /* IOVA that cannot be allocated, struct iopt_reserved */ struct rb_root_cached reserved_itree; u8 disable_large_pages; +#ifdef CONFIG_IOMMU_LIVEUPDATE + u32 num_preserved_domains; +#endif unsigned long iova_alignment; }; @@ -398,6 +401,7 @@ struct iommufd_hwpt_paging { bool enforce_cache_coherency : 1; bool nest_parent : 1; #ifdef CONFIG_IOMMU_LIVEUPDATE + bool liveupdate_preserved; u64 liveupdate_token; #endif /* Head at iommufd_ioas::hwpt_list */ @@ -738,8 +742,20 @@ int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd); void iommufd_hw_queue_destroy(struct iommufd_object *obj); #ifdef CONFIG_IOMMU_LIVEUPDATE +int iommufd_liveupdate_register(void); +void iommufd_liveupdate_unregister(void); + int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd); #else +static inline int iommufd_liveupdate_register(void) +{ + return 0; +} + +static inline void iommufd_liveupdate_unregister(void) +{ +} + static inline int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd) { return -ENOTTY; diff --git a/drivers/iommu/iommufd/liveupdate.c b/drivers/iommu/iommufd/liveupdate.c index 9d73917157cd..21a6efafb960 100644 --- a/drivers/iommu/iommufd/liveupdate.c +++ b/drivers/iommu/iommufd/liveupdate.c @@ -9,9 +9,28 @@ #include #include +#include +#include #include +#include +#include +#include #include "iommufd_private.h" +#include "io_pagetable.h" + +static u32 iopt_inc_num_preserved_domains(struct io_pagetable *iopt) +{ + guard(rwsem_write)(&iopt->domains_rwsem); + return ++iopt->num_preserved_domains; +} + +static void iopt_dec_num_preserved_domains(struct io_pagetable *iopt) +{ + guard(rwsem_write)(&iopt->domains_rwsem); + if (!WARN_ON(!iopt->num_preserved_domains)) + iopt->num_preserved_domains--; +} int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd) { @@ -40,6 +59,12 @@ int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd) goto out_unlock; } + /* The token of a preserved HWPT can't change. */ + if (hwpt->liveupdate_preserved) { + rc = -EBUSY; + goto out_unlock; + } + xa_for_each_marked(&ictx->objects, index, obj, IOMMUFD_OBJ_LIVEUPDATE_MARK) { struct iommufd_hwpt_paging *hwpt_marked; @@ -73,3 +98,322 @@ int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd) iommufd_put_object(ictx, &hwpt->common.obj); return rc; } + +static int iopt_area_validate_file_pages(struct iopt_area *area) +{ + unsigned long last_index = iopt_area_last_index(area); + unsigned long index = iopt_area_index(area); + unsigned int page_offset = area->page_offset; + struct iopt_pages *pages = area->pages; + unsigned long iova = iopt_area_iova(area); + + guard(mutex)(&pages->mutex); + if (WARN_ON(!area->storage_domain)) + return -EINVAL; + + /* + * Walk the domain one page at a time instead of jumping ahead by the + * folio size. When filling a domain, iommufd reuses pages that are + * already pinned (by an access or another domain), so if the memfd + * was punched in between, an old page from a different folio can sit + * in the middle of a folio range. batch_from_domain() walks the domain + * the same way. + */ + while (index <= last_index) { + phys_addr_t phys = iommu_iova_to_phys(area->storage_domain, iova); + struct folio *folio; + + if (!phys) + return -EINVAL; + + folio = page_folio(pfn_to_page(PHYS_PFN(phys - page_offset))); + if (folio->mapping != pages->file->f_mapping) + return -EINVAL; + + iova += PAGE_SIZE - page_offset; + page_offset = 0; + index++; + } + + return 0; +} + +static int iopt_validate_preserved_memory(struct io_pagetable *iopt, + struct liveupdate_session *s) +{ + struct iopt_area *area; + int ret = 0; + + down_read(&iopt->iova_rwsem); + for (area = iopt_area_iter_first(iopt, 0, ULONG_MAX); area; + area = iopt_area_iter_next(area, 0, ULONG_MAX)) { + struct iopt_pages *pages = area->pages; + + if (!pages) + continue; + + /* Only allow file based mapping */ + if (pages->type != IOPT_ADDRESS_FILE) { + ret = -EINVAL; + break; + } + + /* Make sure that the file was preserved. */ + ret = liveupdate_get_token_outgoing(s, pages->file, NULL); + if (ret) + break; + + /* + * Make sure the mapped pages are still associated with the + * preserved memfd. + */ + ret = iopt_area_validate_file_pages(area); + if (ret) + break; + } + up_read(&iopt->iova_rwsem); + + return ret; +} + +static int iommufd_preserve_hwpt(struct iommufd_hwpt_paging *hwpt, + struct iommufd_hwpt_ser *hwpt_ser, + struct liveupdate_session *session) +{ + struct iommu_domain_ser *domain_ser; + int rc; + + /* + * Make IOAS immutable so the DMA mappings do not change while + * the HWPT is preserved. Since one IOAS can have multiple + * HWPTs, if an error occurs this call needs to make the IOAS + * mutable again if it was the one that made it immutable. + */ + if (iopt_inc_num_preserved_domains(&hwpt->ioas->iopt) == 1) { + rc = iopt_validate_preserved_memory(&hwpt->ioas->iopt, session); + if (rc) + goto err; + } + + hwpt_ser->size = sizeof(*hwpt_ser); + hwpt_ser->token = hwpt->liveupdate_token; + + rc = iommu_preserve_domain(hwpt->common.domain, &domain_ser); + if (rc < 0) + goto err; + + hwpt_ser->domain_ser_phys = virt_to_phys(domain_ser); + return 0; + +err: + iopt_dec_num_preserved_domains(&hwpt->ioas->iopt); + return rc; +} + +static void _iommufd_unpreserve(struct iommufd_ctx *ictx, + struct iommufd_ser *ser) +{ + struct iommufd_hwpt_paging *hwpt; + struct iommufd_object *obj; + unsigned long index; + + xa_lock(&ictx->objects); + xa_for_each_marked(&ictx->objects, index, obj, IOMMUFD_OBJ_LIVEUPDATE_MARK) { + if (obj->type != IOMMUFD_OBJ_HWPT_PAGING) + continue; + + hwpt = to_hwpt_paging(container_of(obj, struct iommufd_hw_pagetable, obj)); + if (!hwpt->liveupdate_preserved) + continue; + + xa_unlock(&ictx->objects); + + iommu_unpreserve_domain(hwpt->common.domain); + iopt_dec_num_preserved_domains(&hwpt->ioas->iopt); + + hwpt->liveupdate_preserved = false; + iommufd_put_object(ictx, obj); + + xa_lock(&ictx->objects); + } + xa_unlock(&ictx->objects); + + kho_unpreserve_free(ser); +} + +static int iommufd_liveupdate_preserve(struct liveupdate_file_op_args *args) +{ + struct iommufd_ctx *ictx; + struct iommufd_hwpt_paging *hwpt; + struct iommufd_ser *iommufd_ser; + struct iommufd_object *obj; + unsigned int num_hwpts; + unsigned long index; + unsigned int i; + void *mem; + int rc; + + ictx = iommufd_ctx_from_file(args->file); + if (IS_ERR(ictx)) + return PTR_ERR(ictx); + + mutex_lock(&ictx->liveupdate_mutex); + + /* Count the number of HWPTs to preserve */ + num_hwpts = 0; + xa_lock(&ictx->objects); + xa_for_each_marked(&ictx->objects, index, obj, IOMMUFD_OBJ_LIVEUPDATE_MARK) { + if (obj->type != IOMMUFD_OBJ_HWPT_PAGING) + continue; + + hwpt = to_hwpt_paging(container_of(obj, struct iommufd_hw_pagetable, obj)); + if (!hwpt->common.domain) { + rc = -EINVAL; + xa_unlock(&ictx->objects); + goto out_unlock; + } + num_hwpts++; + } + xa_unlock(&ictx->objects); + + mem = kho_alloc_preserve(struct_size(iommufd_ser, + hwpt_array, num_hwpts)); + if (IS_ERR(mem)) { + rc = PTR_ERR(mem); + goto out_unlock; + } + + iommufd_ser = mem; + iommufd_ser->size = sizeof(*iommufd_ser); + + /* Preserve HWPTs */ + i = 0; + xa_lock(&ictx->objects); + xa_for_each_marked(&ictx->objects, index, obj, IOMMUFD_OBJ_LIVEUPDATE_MARK) { + if (obj->type != IOMMUFD_OBJ_HWPT_PAGING) + continue; + + if (!iommufd_lock_obj(obj)) { + rc = -ENOENT; + xa_unlock(&ictx->objects); + goto out_unpreserve; + } + + /* + * HWPT is locked so it will not be destroyed. The xarray lock + * can be released here before preserving the HWPT. + */ + xa_unlock(&ictx->objects); + hwpt = to_hwpt_paging(container_of(obj, struct iommufd_hw_pagetable, obj)); + rc = iommufd_preserve_hwpt(hwpt, &iommufd_ser->hwpt_array[i++], args->session); + if (rc) { + iommufd_put_object(ictx, obj); + goto out_unpreserve; + } + + /* + * Mark the HWPT as successfully preserved. This is distinct + * from IOMMUFD_OBJ_LIVEUPDATE_MARK, which only indicates the + * userspace intent to preserve. + */ + hwpt->liveupdate_preserved = true; + xa_lock(&ictx->objects); + } + xa_unlock(&ictx->objects); + + /* Store the actual number of HWPTs that are preserved */ + iommufd_ser->num_hwpts = i; + + args->serialized_data = virt_to_phys(iommufd_ser); + mutex_unlock(&ictx->liveupdate_mutex); + iommufd_ctx_put(ictx); + return 0; + +out_unpreserve: + _iommufd_unpreserve(ictx, iommufd_ser); +out_unlock: + mutex_unlock(&ictx->liveupdate_mutex); + iommufd_ctx_put(ictx); + return rc; +} + +static void iommufd_liveupdate_unpreserve(struct liveupdate_file_op_args *args) +{ + struct iommufd_ctx *ictx; + + ictx = iommufd_ctx_from_file(args->file); + if (WARN_ON(IS_ERR(ictx))) + return; + + mutex_lock(&ictx->liveupdate_mutex); + _iommufd_unpreserve(ictx, phys_to_virt(args->serialized_data)); + mutex_unlock(&ictx->liveupdate_mutex); + + iommufd_ctx_put(ictx); +} + +static int iommufd_liveupdate_retrieve(struct liveupdate_file_op_args *args) +{ + return -EOPNOTSUPP; +} + +static bool iommufd_liveupdate_can_finish(struct liveupdate_file_op_args *args) +{ + return false; +} + +static void iommufd_liveupdate_finish(struct liveupdate_file_op_args *args) +{ +} + +static bool iommufd_liveupdate_can_preserve(struct liveupdate_file_handler *handler, + struct file *file) +{ + struct iommufd_ctx *ictx = iommufd_ctx_from_file(file); + + if (IS_ERR(ictx)) + return false; + + iommufd_ctx_put(ictx); + return true; +} + +static struct liveupdate_file_ops iommufd_ser_file_ops = { + .can_preserve = iommufd_liveupdate_can_preserve, + .preserve = iommufd_liveupdate_preserve, + .unpreserve = iommufd_liveupdate_unpreserve, + .retrieve = iommufd_liveupdate_retrieve, + .can_finish = iommufd_liveupdate_can_finish, + .finish = iommufd_liveupdate_finish, + .owner = THIS_MODULE, +}; + +static struct liveupdate_file_handler iommufd_ser_handler = { + .compatible = IOMMUFD_LUO_COMPATIBLE, + .ops = &iommufd_ser_file_ops, +}; + +int iommufd_liveupdate_register(void) +{ + int ret; + + ret = liveupdate_register_file_handler(&iommufd_ser_handler); + if (ret) + goto out; + + ret = iommu_liveupdate_register_flb(&iommufd_ser_handler); + if (ret) + liveupdate_unregister_file_handler(&iommufd_ser_handler); + +out: + if (ret == -EOPNOTSUPP) + return 0; + + return ret; +} + +void iommufd_liveupdate_unregister(void) +{ + iommu_liveupdate_unregister_flb(&iommufd_ser_handler); + liveupdate_unregister_file_handler(&iommufd_ser_handler); +} diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index fe8610fd58e3..86a578fb8e18 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -805,11 +805,18 @@ static int __init iommufd_init(void) if (ret) goto err_misc; } - ret = iommufd_test_init(); + + ret = iommufd_liveupdate_register(); if (ret) goto err_vfio_misc; + + ret = iommufd_test_init(); + if (ret) + goto err_liveupdate; return 0; +err_liveupdate: + iommufd_liveupdate_unregister(); err_vfio_misc: if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)) misc_deregister(&vfio_misc_dev); @@ -821,6 +828,7 @@ static int __init iommufd_init(void) static void __exit iommufd_exit(void) { iommufd_test_exit(); + iommufd_liveupdate_unregister(); if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)) misc_deregister(&vfio_misc_dev); misc_deregister(&iommu_misc_dev); diff --git a/include/linux/kho/abi/iommufd.h b/include/linux/kho/abi/iommufd.h new file mode 100644 index 000000000000..442c12b9bdad --- /dev/null +++ b/include/linux/kho/abi/iommufd.h @@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright (C) 2026, Google LLC + * Author: Samiullah Khawaja + */ + +#ifndef _LINUX_KHO_ABI_IOMMUFD_H +#define _LINUX_KHO_ABI_IOMMUFD_H + +#include +#include +#include + +/** + * DOC: IOMMUFD Live Update ABI + * + * This header defines the ABI for preserving the state of an IOMMUFD file + * across a kexec reboot using LUO. + * + * This interface is a contract. Any modification to any of the serialization + * structs defined here constitutes a breaking change. Such changes require + * incrementing the version number in the IOMMUFD_LUO_COMPATIBLE string. + */ + +#define IOMMUFD_LUO_COMPATIBLE "iommufd-v1" + +/** + * struct iommufd_hwpt_ser - IOMMUFD HWPT serialized state + * @size: Size of this struct + * @padding: Padding + * @domain_ser_phys: Physical address of the serialized state of associated domain + * @token: User provided token + */ +struct iommufd_hwpt_ser { + u32 size; + u32 padding; + u64 domain_ser_phys; + u64 token; +} __packed; + +/** + * struct iommufd_ser - IOMMUFD serialized state + * @size: Size of this struct + * @features: Bitmask of iommufd specific features used by the preserved state + * @num_hwpts: Number of preserved HWPTs + * @hwpt_array: Array of serialized state of preserved HWPTs + */ +struct iommufd_ser { + u32 size; + u32 features; + u64 num_hwpts; + struct iommufd_hwpt_ser hwpt_array[]; +} __packed; + +#endif /* _LINUX_KHO_ABI_IOMMUFD_H */ -- 2.56.0.506.g506a7eabc5-goog