From nobody Mon Feb 9 11:04:45 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 9CD88377541 for ; Tue, 3 Feb 2026 22:09:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770156599; cv=none; b=TUrrDCFTOhcv2NL9sPI8y7xSZCiCC2+0+BFAFxCfwoa/RzW7Xe636TM+uiiesrznjRZcFznjqUNfkHSF5Gg+8N+Tcnw5x+OORrxZy+2E+OtvHUXEkBGk715LsAl7lrF7MM5T/EXzWkeZb22RKBccD+q/yhEJQKbJ/Eic3IyGKjs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770156599; c=relaxed/simple; bh=5oimeBpjR9ySAyBwN7duv7K3nB4HOiM5cRVJaWv274o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lQVUnz6KgJF9y6yzqGaPZIUo3YnrxitcZHBZ4zrkl37kF67n9YnQxcwhNbR7ZoMMsFDmsnPLOAlD4a4i6UGkGcUzPTT8rR4fy4vfsrFuV4QCoXVxQIvzHsNw9ay4qpYJ34o8zLKlJoBBdXFpujk63nTWrdfH8ii/pMrJiW86lvM= 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=VodX9BEL; arc=none smtp.client-ip=209.85.214.202 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="VodX9BEL" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2a78c094ad6so62736205ad.1 for ; Tue, 03 Feb 2026 14:09:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770156597; x=1770761397; 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=Lp/+9SAp3L8mfYjwbkh3T/evpw/YswqyzsO+xOxb2eg=; b=VodX9BELBn/e27xV70jzzO9+sOeUoNAPP9z8FQh07P0fTXS5f1q367/em3ZJxhFEaM hKzjkld4V0tYy480msA/MLdJfoIr1g7SOvliskoqU/WsYdPlVNqp2fxlZePMxYL+ucmT gTdKfUf+irKZQ96s5lKfcpVtKOb3uDxJYrsV3pY0qHWIxNMJUx9LP0f4tnmDXakhFFbz 0Kwog7lmmFflb2UJYZf4f27bu9Yx0fHYA0xq72+hl+bxHdnufl2Fy1VbXdj5+ra3sO+Y UIgFx0kYnoW+e+q12h7GNE1OZ45mKIOId0LEhNzxwOubr5937Qf7xqwCVW6sxPwHpU/w KAGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770156597; x=1770761397; 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=Lp/+9SAp3L8mfYjwbkh3T/evpw/YswqyzsO+xOxb2eg=; b=a2qEsVPeq26luOp1Sh+sHouceCGxQygajYJ8ClWgim6CR4BkZGVpmL4+0VBmkCOu/Q Qj9Jskayru4yRYCYq+8ePsfW/9+PjRg5B7+QzmHDhXuBRDG18LBbjkXSpIKpVIqFR9O4 VlSnT0Zc9NOJlW5m0WzFUK0f7GWdZ9qcor5svNRd8jVJHBpF7fmY6lfC4jmUtjDrfI2d SK/NmsdQ0zSAppt8lJBERjsyMIEJNtT2EpsjY0sdgkntgVDk98bsX3cjj/cDtEZ5Flwm LR3tgvWrq652Wyc/+Vpnx2uLTs6uFfq6zaFtNvoPF3/VeT8Ni5jFcjkaBwstHRV1HZuW C/2Q== X-Forwarded-Encrypted: i=1; AJvYcCVeE7F5+fmUR8TAMlgCaxJPCAKSA1NTKntqI8hlNYgkcknJhLZdVsZTatXHB2h9votjBJnsMnXAqDM+vRo=@vger.kernel.org X-Gm-Message-State: AOJu0YzdbTX7kmustyJAFKDWSMYsm67SLu41aMQLzWcb8f7iLvQcVwom 42AAQQoJT/10a7AQDkB5x9vLUeyRhbT+broW5V8ZGoPc5iRKtf3F5x83XlhgSXpBA4lm6hFIe0T aHpcfRu0T+oRa6g== X-Received: from plbmf3.prod.google.com ([2002:a17:902:fc83:b0:2a1:1c0e:70b9]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c411:b0:295:b46f:a6c2 with SMTP id d9443c01a7336-2a933fba80bmr6525215ad.37.1770156597010; Tue, 03 Feb 2026 14:09:57 -0800 (PST) Date: Tue, 3 Feb 2026 22:09:38 +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-5-skhawaja@google.com> Subject: [PATCH 04/14] 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, 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" 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 | 74 +++++++++++++++++++++++++++++++++++++ drivers/iommu/iommu-pages.h | 30 +++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/drivers/iommu/iommu-pages.c b/drivers/iommu/iommu-pages.c index 3bab175d8557..588a8f19b196 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 =20 #define IOPTDESC_MATCH(pg_elm, elm) \ @@ -131,6 +132,79 @@ void iommu_put_pages_list(struct iommu_pages_list *lis= t) } EXPORT_SYMBOL_GPL(iommu_put_pages_list); =20 +#if IS_ENABLED(CONFIG_IOMMU_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) + return; + + /* If less than zero then unpreserve all pages. */ + if (count < 0) + count =3D 0; + + list_for_each_entry(iopt, &list->pages, iopt_freelist_elm) { + kho_unpreserve_folio(ioptdesc_folio(iopt)); + if (count > 0 && --count =3D=3D 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 =3D kho_restore_folio(phys); + BUG_ON(!folio); + + iopt =3D folio_ioptdesc(folio); + + order =3D folio_order(folio); + pgcnt =3D 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 =3D 0; + int ret; + + list_for_each_entry(iopt, &list->pages, iopt_freelist_elm) { + ret =3D 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 oper= ation * @virt: The page to setup diff --git a/drivers/iommu/iommu-pages.h b/drivers/iommu/iommu-pages.h index ae9da4f571f6..bd336fb56b5f 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); =20 +#if IS_ENABLED(CONFIG_IOMMU_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); +#else +static inline int iommu_preserve_page(void *virt) +{ + return -EOPNOTSUPP; +} + +static inline void iommu_unpreserve_page(void *virt) +{ +} + +static inline int iommu_preserve_pages(struct iommu_pages_list *list) +{ + return -EOPNOTSUPP; +} + +static inline void iommu_unpreserve_pages(struct iommu_pages_list *list, i= nt count) +{ +} + +static inline 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 --=20 2.53.0.rc2.204.g2597b5adb4-goog