From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) (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 8D4B22EDD58 for ; Sat, 8 Aug 2026 02:27:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786156049; cv=none; b=Ch8E+9FNV6O5bFRWtoWg5JAR9BZXk00s0XLHEcynp5QFaSc/TQLomM68qFJYiZLWws9kV0zau3d+TK9xmmM7gWJquDcLMOt2dtxr7mqNLVhEEt9aa2Jh/W3lUkpMbFMYVdE9wkhbX54ooLZxZgoiJWv5+vnbmK4HSkgGhabtNm0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786156049; c=relaxed/simple; bh=hvc4sChU1zYRyCYy56LSYGC/4J5BHZmxu3246c51Qwk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=m3n0y7hqPR5tv3z5NrgSj2qTdvESP0La/IV07XYyVQwBnCfdOEA4JdkXKhHTmdjpKvBzB06YS3NdhqA6kR+xVm9dvwDN+P3WYKlNj1SKF0ghIz1THmoEoW7fRaGYQoior+AGSddJTagoToZGoSBCdop5jac/bQGYJ3nYqirEATA= 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=cZ0pyDjt; arc=none smtp.client-ip=209.85.210.197 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="cZ0pyDjt" Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-8485d853b08so276868b3a.1 for ; Fri, 07 Aug 2026 19:27:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786156047; x=1786760847; 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=RLrpkCzlvvEgnlzC1Ye64k2MeVsxQQxVHyL6u9eNslk=; b=cZ0pyDjthOHK4sRWU3I8zcMO4Gbgyx+rzmAV6Q7MNk856tif0BSZMhuQACumZn/Dah w4RsQtaS0ojc3sWqV08+q5jKCOPKxdhubgINFZhcbvbDi4m6DuAITr6fIq6QZ5BoQNkC 6B8MPz+hjY3sOeb+bLJX+GnDGlfmRTnlAx6lkGmke8/2nDH08N5ZdKAHGI/seNioXhOE MzYIex4SECzPwbUJh7krZP9slAzdPqjaV6cmD4VSg/+znyT/ncl5x71HKYnBPlNvfnSy 8gSaiq6z+0u4oi3QomFy41kXKRWiDR5Bb2z2YICU4Rb/bv5d7x1rBxebucoxa20VzTDL 5mQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786156047; x=1786760847; 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=RLrpkCzlvvEgnlzC1Ye64k2MeVsxQQxVHyL6u9eNslk=; b=RMQnR8yoYx70zNRnP5RZJZmkhXCH8Tmx/yL1nmdgiXaK7Wbf7G9e2DaBj8A//bnHMb jSh3q/KRTPi6nHyvb5ogcyvjZu+/FAuDcIKRXcq2bIESG+JdIegvZL4UnSvs1kfCLKpS InBvO68byVleK11VnzLhF13RT6BPYsrSBPqPzQa6q4Sk6/B5+avVecHFYZ91AcEdNC3Z 5srDe1pAUbQGMCxUqxy1YEu2lE7wqIk3oVW/Tud7nuRF7zG0ZqewA5LBRaSaAqCqCewq GL3vOsSTcEy2pEyub7EyD0nHlUQi+ea6VyLk9ueqD3OkCejoxBZXVBeZmyDbcqjWamN3 wymA== X-Forwarded-Encrypted: i=1; AHgh+Rp1Xkq1Ydu8AwyJBEr0CL29tWLVMIuOTRPLeFcEkvlcQ0wWnp3M4CftoZWggDnLnB7gaJA=@vger.kernel.org X-Gm-Message-State: AOJu0Yy2+cehwIw92kEUOmT9/xr9xEFN8YsSXOInUSRNGdijKL9XQzRM yZKYlohvMH5BmrbgsWkPFDqIdZ83Tco4sX27tEYDLWEl23dV1aC83RHDWP2aD2fj2h+sSbRkYLv MPxM+EGwHINkXxA== X-Received: from pfwy32.prod.google.com ([2002:a05:6a00:1ca0:b0:84c:586:a7ea]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1388:b0:82f:3a1e:5618 with SMTP id d2e1a72fcca58-84f2e017478mr29152470b3a.22.1786156046606; Fri, 07 Aug 2026 19:27:26 -0700 (PDT) Date: Sat, 8 Aug 2026 02:27:08 +0000 In-Reply-To: <20260808022723.3893618-1-skhawaja@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260808022723.3893618-1-skhawaja@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260808022723.3893618-4-skhawaja@google.com> Subject: [PATCH v4 03/18] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages 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, Pratyush Yadav , Pasha Tatashin , David Matlack , Andrew Morton , Pranjal Shrivastava , Vipin Sharma 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. Reviewed-by: Pranjal Shrivastava Signed-off-by: Samiullah Khawaja --- drivers/iommu/iommu-pages.c | 108 ++++++++++++++++++++++++++++++++++-- drivers/iommu/iommu-pages.h | 30 ++++++++++ 2 files changed, 134 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommu-pages.c b/drivers/iommu/iommu-pages.c index 3bab175d8557..96cc935fb4e1 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,100 @@ 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); + BUG_ON(!folio); + + 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.55.0.679.g6767b8d81c-goog