From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795409395; b=W2r+RA5kO9QC8v+VokUXnoyVN2rTD5W4U8cw+JuMHFiYKdbfCIxNZvIGyuzQJ12/InGb yjd2oNQ8H4qjtuTMf5P+aQgqYutdrWRIunJ31qc14WjnO7rlVYJeOwt864n9cbDl/TweO lzuIX/QYNnV0S5QIkDTvemKz29bLOvICBRk9eIFxJjXRYsTOyIJr5K+o8tv50oero0QyZ MHDloo2+TIPKtk7cnYAA1ZJZu08iO15eLRmeBk29BJ5VPQxYxGiP42MOc4ApheYBdGitQ SBd58NQgyLsK2vwllBKJ1TfQdn/iSRkDz8+Vjbk1765VBGCYhCGyjVYyl6XfkdVYmXw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795409395; 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=bO1KGG2LtYAShW8IvVbHPY+RtbdGqp650yC0DvHa1oc=; b=QbJn3D+3EX23feM0DJSqcty232maxHX6gN7A60cv3RXSbDRh7IpWpweZ602qjMsuwNDk Ntnz3jDug7dDIVtv11bmyWO9+KymW+V4/IyAJiW1IQayqhxAxh/pctaGa2F2uEd0f6FF6 o1sllCAXfajLKTUcPHuYen/HnHaQrC7kjuyogN255Rup2caIOwDX3odjJYtnchzoCwf+a PgtCyRgGiL2Nf+zwetplNWLezAIrJb/o8/5jDdqiCbssjxCfWZoBcEeGDjw6CP4AkV/zw S+R1Y9l9ZBFfy6EZO9hutIUBDEUnb9kPQQ5RvRK8rYkYMLj8Rkk0TuId2IgB0A6YaAw== 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=b4TLQdjs; arc=none smtp.remote-ip=209.85.216.70 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (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 2CCF7606A331 for ; Mon, 23 Nov 2026 04:49:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2CCF7606A331 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=b4TLQdjs Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-3b18165dc53so3377255a91.3 for ; Sun, 22 Nov 2026 20:49:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795409395; x=1796014195; 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=bO1KGG2LtYAShW8IvVbHPY+RtbdGqp650yC0DvHa1oc=; b=b4TLQdjs3ro/rDlvZzpPGwznbl2SdrmBDnu1pygvw4J/JTFP5vwn05bjC9lJ0IbX31 iKjL9Q7SyBshWmI2UmuWto7inowMdzdbDnlO0WpeQiJqQ8YvhIYWFSta17dLpgwMuOY3 hByeujORXmYWJ1X/mjyTRJB5WiOiKUUa8F+sEHWfhdpKmAFimjAUra7113Ne3+/kHvQZ xlh7+rHJnXF/F4PejdKsQc1UIhuJAVur5frWD9NhvVBTvgVPBGPf6mGe0w+JenCO+SCM K53KWp2gE2tCR5l76tSEA+8tJUJFcI8EV8z1ffwppuOaPaVMz9bAZfqG8yT+12nWYBay pz/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795409395; x=1796014195; 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=bO1KGG2LtYAShW8IvVbHPY+RtbdGqp650yC0DvHa1oc=; b=TR7ID4q428dVHw7NcFyENvwcOAIcI1pyOqSGtfcDNsedXMLd9/RW1BDeeeAE9vVvh6 CufVd8hA1tatqo9BGt/VAnCeaU8mtSkTqWld5vr0XIdFW9pqYLopu//7UFZiZh3l4jMk a1yg1S3zzjHXel4YbtIppY5XrMABaZ8JcYu287Ar5aaivw5B4a9MJ3niQIYKKuCyeBk+ edxR7OvFlzcyXb++xaCNsI3OU/WJFjTs1i3pXApLFafB5u0OrqU1cqnDZ8IodP4rsL5z ZmO74nCqFGCdisTJdSl9FMwXvRjYQP5ZFNMzrcM+Wf1t+mMyIDDzeYBM5C4mnfu7Qzel 0lfw== X-Forwarded-Encrypted: i=1; AL+hErGv0HwVZW3SAYPnMDW2bUu2Ok/KSQf6IGmdwM5wGdIVu65HXt+UDHSX3DuBuBTebRje32myWtxONXchTe8=@vger.kernel.org X-Gm-Message-State: APuU4Z/5UoPStQhMubkU6FLZfJmIZJ6uEx+VvBduOLaZNv5vsRLnvMbN Q53ODCxI42C75G0Dr3r/9nJd6wfqYE5jtCPXaOc83/lx35qR1OY4iABrUEdXLAo0ZugiEH+b6uM mkb0K1T3HvCziPg== X-Received: from plup8.prod.google.com ([2002:a17:903:4b28:b0:2eb:337a:cac]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:e70c:b0:3a8:6045:5a19 with SMTP id 98e67ed59e1d1-3b41cafcc05mr9926329a91.37.1795409394285; Sun, 22 Nov 2026 20:49:54 -0800 (PST) Date: Mon, 23 Nov 2026 04:49:34 +0000 In-Reply-To: <20261123044949.3610702-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: <20261123044949.3610702-1-skhawaja@google.com> X-Mailer: git-send-email 2.56.1.809.ga0b61035b5-goog Message-ID: <20261123044949.3610702-4-skhawaja@google.com> Subject: [PATCH v8 03/18] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages From: Samiullah Khawaja To: Joerg Roedel , Will Deacon , Lu Baolu , Jason Gunthorpe , David Woodhouse Cc: Samiullah Khawaja , Pranjal Shrivastava , Ankit Soni , 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 , Nicolin Chen , John Starks Content-Type: text/plain; charset="UTF-8" IOMMU pages are allocated/freed using APIs using struct ioptdesc. For the proper preservation and restoration of ioptdesc add helper functions. Signed-off-by: Samiullah Khawaja Reviewed-by: Pranjal Shrivastava Reviewed-by: Lu Baolu Reviewed-by: Ankit Soni --- drivers/iommu/iommu-pages.c | 109 ++++++++++++++++++++++++++++++++++-- drivers/iommu/iommu-pages.h | 30 ++++++++++ 2 files changed, 135 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommu-pages.c b/drivers/iommu/iommu-pages.c index 3bab175d8557..0aa18c771baa 100644 --- a/drivers/iommu/iommu-pages.c +++ b/drivers/iommu/iommu-pages.c @@ -6,6 +6,7 @@ #include "iommu-pages.h" #include #include +#include #include #define IOPTDESC_MATCH(pg_elm, elm) \ @@ -28,6 +29,13 @@ static inline size_t ioptdesc_mem_size(struct ioptdesc *desc) return 1UL << (folio_order(ioptdesc_folio(desc)) + PAGE_SHIFT); } +static inline void iommu_folio_update_stats(struct folio *folio, + unsigned long nr_pages) +{ + mod_node_page_state(folio_pgdat(folio), NR_IOMMU_PAGES, nr_pages); + lruvec_stat_mod_folio(folio, NR_SECONDARY_PAGETABLE, nr_pages); +} + /** * iommu_alloc_pages_node_sz - Allocate a zeroed page of a given size from * specific NUMA node @@ -80,8 +88,7 @@ void *iommu_alloc_pages_node_sz(int nid, gfp_t gfp, size_t size) * rather large, i.e. multiple gigabytes in size. */ pgcnt = 1UL << order; - mod_node_page_state(folio_pgdat(folio), NR_IOMMU_PAGES, pgcnt); - lruvec_stat_mod_folio(folio, NR_SECONDARY_PAGETABLE, pgcnt); + iommu_folio_update_stats(folio, pgcnt); return folio_address(folio); } @@ -95,8 +102,7 @@ static void __iommu_free_desc(struct ioptdesc *iopt) if (IOMMU_PAGES_USE_DMA_API) WARN_ON_ONCE(iopt->incoherent); - mod_node_page_state(folio_pgdat(folio), NR_IOMMU_PAGES, -pgcnt); - lruvec_stat_mod_folio(folio, NR_SECONDARY_PAGETABLE, -pgcnt); + iommu_folio_update_stats(folio, -pgcnt); folio_put(folio); } @@ -131,6 +137,101 @@ void iommu_put_pages_list(struct iommu_pages_list *list) } EXPORT_SYMBOL_GPL(iommu_put_pages_list); +#if IS_ENABLED(CONFIG_IOMMU_LIVEUPDATE) +/** + * iommu_unpreserve_pages - Unpreserve pages that were preserved in KHO + * @virt: Virtual address of page to be unpreserved + */ +void iommu_unpreserve_pages(void *virt) +{ + kho_unpreserve_folio(ioptdesc_folio(virt_to_ioptdesc(virt))); +} +EXPORT_SYMBOL_GPL(iommu_unpreserve_pages); + +/** + * iommu_preserve_pages - Preserve pages using KHO + * @virt: Virtual address of page to preserve + * + * Returns 0 on success, negative error on failure + */ +int iommu_preserve_pages(void *virt) +{ + return kho_preserve_folio(ioptdesc_folio(virt_to_ioptdesc(virt))); +} +EXPORT_SYMBOL_GPL(iommu_preserve_pages); + +/** + * iommu_unpreserve_pages_list - Unpreserve a list of KHO preserved pages + * @list: List of pages to unpreserve + */ +void iommu_unpreserve_pages_list(struct iommu_pages_list *list) +{ + struct ioptdesc *iopt; + + list_for_each_entry(iopt, &list->pages, iopt_freelist_elm) + kho_unpreserve_folio(ioptdesc_folio(iopt)); +} +EXPORT_SYMBOL_GPL(iommu_unpreserve_pages_list); + +/** + * iommu_restore_pages - Restore pages that were preserved in KHO + * @phys: Physical address of page to restore + */ +void iommu_restore_pages(u64 phys) +{ + struct ioptdesc *iopt; + struct folio *folio; + unsigned long pgcnt; + unsigned int order; + + folio = kho_restore_folio(phys); + if (!folio) + panic("Failed to restore preserved IOMMU page %llx\n", phys); + + iopt = folio_ioptdesc(folio); + + /* + * For the restored pages incoherent is set to false as these are not + * mapped using the DMA_API. The remapping of these pages using DMA_API + * is not needed as these are not going to be written to by the new + * kernel. + */ + iopt->incoherent = false; + + order = folio_order(folio); + pgcnt = 1UL << order; + iommu_folio_update_stats(folio, pgcnt); +} +EXPORT_SYMBOL_GPL(iommu_restore_pages); + +/** + * iommu_preserve_pages_list - Preserve list of pages using KHO + * @list: List of pages to preserve + * + * Returns 0 on success, negative error on failure + */ +int iommu_preserve_pages_list(struct iommu_pages_list *list) +{ + struct ioptdesc *iopt; + int ret; + + list_for_each_entry(iopt, &list->pages, iopt_freelist_elm) { + ret = kho_preserve_folio(ioptdesc_folio(iopt)); + if (ret) + goto err; + } + + return 0; + +err: + list_for_each_entry_continue_reverse(iopt, &list->pages, iopt_freelist_elm) + kho_unpreserve_folio(ioptdesc_folio(iopt)); + + return ret; +} +EXPORT_SYMBOL_GPL(iommu_preserve_pages_list); +#endif + /** * iommu_pages_start_incoherent - Setup the page for cache incoherent operation * @virt: The page to setup diff --git a/drivers/iommu/iommu-pages.h b/drivers/iommu/iommu-pages.h index e9e605b5fa3a..3c409ec3d170 100644 --- a/drivers/iommu/iommu-pages.h +++ b/drivers/iommu/iommu-pages.h @@ -53,6 +53,36 @@ void *iommu_alloc_pages_node_sz(int nid, gfp_t gfp, size_t size); void iommu_free_pages(void *virt); void iommu_put_pages_list(struct iommu_pages_list *list); +#if IS_ENABLED(CONFIG_IOMMU_LIVEUPDATE) +int iommu_preserve_pages(void *virt); +void iommu_unpreserve_pages(void *virt); +int iommu_preserve_pages_list(struct iommu_pages_list *list); +void iommu_unpreserve_pages_list(struct iommu_pages_list *list); +void iommu_restore_pages(u64 phys); +#else +static inline int iommu_preserve_pages(void *virt) +{ + return -EOPNOTSUPP; +} + +static inline void iommu_unpreserve_pages(void *virt) +{ +} + +static inline int iommu_preserve_pages_list(struct iommu_pages_list *list) +{ + return -EOPNOTSUPP; +} + +static inline void iommu_unpreserve_pages_list(struct iommu_pages_list *list) +{ +} + +static inline void iommu_restore_pages(u64 phys) +{ +} +#endif + /** * iommu_pages_list_add - add the page to a iommu_pages_list * @list: List to add the page to -- 2.56.1.809.ga0b61035b5-goog