From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 42C752FFFB4 for ; Tue, 2 Dec 2025 23:03:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716604; cv=none; b=QNMoPnHmHlS/t61CFeoRnUMkolg7pUT9BYsjYCuC2idIn9OSJD5RFzYBjthOX6aa2us5D6X8YOXDC0iTf8IXUp/UGwxLsMVFwG0TPQoPxQ+UZGcdSvxrHc/PvEozg0EhTcZ9xyJKKW3nWpC/KK3lf3KnOYyXL5NEo105Mr2NOGk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716604; c=relaxed/simple; bh=wbS80YRzP6HQ1mk9Zgm1QNuvC4z9OIAWk5rpB+SP67s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iBnFfdIP6s4Nho0oTK9bATlHkWxyQ6NX32rawrvbQ3A/S6HvsKEqPC933OSgwHzvxs3IcGtEaMg6QPPbKSZTe9QkxDGuS+DvT/BCkz6tIpmhJoHccCc9tn3oKkpL8gq2CGO+kBVP67baATyul1di1zrB2U5rL6su7fNGBp407js= 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=Evr+SLJ2; arc=none smtp.client-ip=209.85.214.201 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="Evr+SLJ2" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2982dec5ccbso105558815ad.3 for ; Tue, 02 Dec 2025 15:03:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764716602; x=1765321402; 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=pdgzhYNfjIdTL/eG3cF5Z6AQsPCrOwVXSPRWTTBl/Rw=; b=Evr+SLJ2BzDcWafKjR1B0nMhE6oODcgGPNSJEYQTHL5wkpcE0+y6S4tcAOmKhWxAQo MD51jkRE/dwVYWAMXCPthAztJhQe+/1irFy6cVoXU9ovS7/U8ikgBqzKxJQH60TCXCcH K08CJhWuxNFrLAJfQCSd3unMq9LqAjoDExwrKDicBsdCr8ZoTrdJiqaqYn3Ov2uzVTOv kPXeiW6nCLw4r2IbS1ntyCB8pF/s9cBTVhF5cZ4VfDxhQ8dHIp/4Ny3W23RosUeypt6T 92xQAxzcIxROdbBuzc6uvjebDWNUINKtq1GCXT5TwlxynWoSMtDFBwvRqyDq1cnP6z08 aU0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764716602; x=1765321402; 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=pdgzhYNfjIdTL/eG3cF5Z6AQsPCrOwVXSPRWTTBl/Rw=; b=bfA/Sr7unsX1CRcOPy25eboFApvCGqAEnRNaUGJ6p40FdQNz9ja33vjaIHhdX4/b2r aSJm1xUcBG4c0eDhIVy+WDA5tGAfeToZ9DhzFsQ2he73hGuJHJ+ekdWLFeNXRZ7I+bcO 1xJm7YgNv0Me0dsdHhX7/sjjLOjmMq15d6VcLUHmXV1uvu/M0y/rlhDqKMbCIiqoDTiP DwZnBtzXW4dRaZE3TucqAVQA4XHiqMk8epGsLpM7f4oG1TvHOwHRrt4UomloixXiSB/0 EPccDOrcP+zfRgiHFp26C1xlnJ73Qo8MbeUHks0jEu6ZHjnAVjqABHbsDENTrC5dGLNN goRw== X-Forwarded-Encrypted: i=1; AJvYcCVskeAiHFGipUZrXAp7bieOfTzUz0VApsFeixaGke9JFLWy81FvtaZhRPuyW8iBpzy4LMqTU0szRVCpHVs=@vger.kernel.org X-Gm-Message-State: AOJu0YzNk18Esq5BX18LbHXguHaiq3EknooBmYm6/0jgEkT32VFhjg+M AIgblWprwJtXlqeVaRi8thqEZxyIfklxOUrJdVwU8mhhfDviCiIdlVSkVB466nWqhkVfGMh7UtR Yke7boRbpB1FyTw== X-Google-Smtp-Source: AGHT+IHBhqwUWNFFs/zSxpUxLtaFweT97baBO05WXbNjXDrV/S+Y3+rnz+5xR7EyyF3jIBOeQtRiauGmHWkASg== X-Received: from plbje22.prod.google.com ([2002:a17:903:2656:b0:29d:5afa:2c2]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ef01:b0:295:425a:350e with SMTP id d9443c01a7336-29d682a9d76mr2117075ad.8.1764716601632; Tue, 02 Dec 2025 15:03:21 -0800 (PST) Date: Tue, 2 Dec 2025 23:02:41 +0000 In-Reply-To: <20251202230303.1017519-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: <20251202230303.1017519-1-skhawaja@google.com> X-Mailer: git-send-email 2.52.0.158.g65b55ccf14-goog Message-ID: <20251202230303.1017519-12-skhawaja@google.com> Subject: [RFC PATCH v2 11/32] iommu/pages: Add APIs to preserve/unpreserve/restore iommu pages From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Pasha Tatashin , Jason Gunthorpe , iommu@lists.linux.dev Cc: Samiullah Khawaja , Robin Murphy , Pratyush Yadav , Kevin Tian , Alex Williamson , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, YiFei Zhu , Chris Li , praan@google.com 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 --- drivers/iommu/iommu-pages.c | 70 +++++++++++++++++++++++++++++++++++++ drivers/iommu/iommu-pages.h | 8 +++++ 2 files changed, 78 insertions(+) diff --git a/drivers/iommu/iommu-pages.c b/drivers/iommu/iommu-pages.c index 3bab175d8557..14669b0f498f 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) \ @@ -131,6 +132,75 @@ void iommu_put_pages_list(struct iommu_pages_list *list) } EXPORT_SYMBOL_GPL(iommu_put_pages_list); +#if IS_ENABLED(CONFIG_LIVEUPDATE) +void iommu_unpreserve_page(void *virt) +{ + kho_unpreserve_folio(ioptdesc_folio(virt_to_ioptdesc(virt))); +} +EXPORT_SYMBOL_GPL(iommu_unpreserve_page); + +int iommu_preserve_page(void *virt) +{ + return kho_preserve_folio(ioptdesc_folio(virt_to_ioptdesc(virt))); +} +EXPORT_SYMBOL_GPL(iommu_preserve_page); + +void iommu_unpreserve_pages(struct iommu_pages_list *list, int count) +{ + struct ioptdesc *iopt; + + if (count < 0) + count = 0; + + list_for_each_entry(iopt, &list->pages, iopt_freelist_elm) { + kho_unpreserve_folio(ioptdesc_folio(iopt)); + if (count > 0 && --count == 0) + break; + } +} +EXPORT_SYMBOL_GPL(iommu_unpreserve_pages); + +void iommu_restore_page(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); + + order = folio_order(folio); + pgcnt = 1UL << order; + mod_node_page_state(folio_pgdat(folio), NR_IOMMU_PAGES, pgcnt); + lruvec_stat_mod_folio(folio, NR_SECONDARY_PAGETABLE, pgcnt); +} +EXPORT_SYMBOL_GPL(iommu_restore_page); + +int iommu_preserve_pages(struct iommu_pages_list *list) +{ + struct ioptdesc *iopt; + int count = 0; + int ret; + + list_for_each_entry(iopt, &list->pages, iopt_freelist_elm) { + ret = kho_preserve_folio(ioptdesc_folio(iopt)); + if (ret) { + iommu_unpreserve_pages(list, count); + return ret; + } + + ++count; + } + + return 0; +} +EXPORT_SYMBOL_GPL(iommu_preserve_pages); + +#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 ae9da4f571f6..ec1787776edd 100644 --- a/drivers/iommu/iommu-pages.h +++ b/drivers/iommu/iommu-pages.h @@ -53,6 +53,14 @@ 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_LIVEUPDATE) +int iommu_preserve_page(void *virt); +void iommu_unpreserve_page(void *virt); +int iommu_preserve_pages(struct iommu_pages_list *list); +void iommu_unpreserve_pages(struct iommu_pages_list *list, int count); +void iommu_restore_page(u64 phys); +#endif + /** * iommu_pages_list_add - add the page to a iommu_pages_list * @list: List to add the page to -- 2.52.0.158.g65b55ccf14-goog