From nobody Mon Feb 9 23:15:54 2026 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 315D437BE9C for ; Tue, 3 Feb 2026 22:10:02 +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=1770156603; cv=none; b=XIAqLY/LnM/2wR24WGMvaI6RSXoUwuPuZ8AT/2msuhmHn8jArO9RI40ibPPWdDT9KpyOd2/77c/0iSzxT6W3RheELOzop/x1fwQUURawZjlW0L9o8kjUl1CDwUqUZrgP17Q+iDmbQyZLXrcRusBgB9gX2dTeSTlZkpPLx9E+NP8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770156603; c=relaxed/simple; bh=qPSLNvG5EvI+RYNmb6EVFUAJ74gpknCj2QGMG3t3XLU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OVS1NqAoZZ1dTAgwCh26fwmsAKOoPnr8yqkpmSDVqTjdB1O60QIk/J9Uua2al2OXq1sgsauzKYgjwynJzYdGuOe39tBvrRjqrjXWVQKUq6WQdtAIkMwRF3LoIUq1JY+23yuSdtn3WVkRkf0vJLYCMkTVO6S0vMQPLzm/0pECBis= 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=qV5463o/; 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="qV5463o/" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2a8c54bbe46so125909965ad.2 for ; Tue, 03 Feb 2026 14:10:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770156602; x=1770761402; 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=laeArOoHMmZZpQbmwN1GpGAXEElz6gu2HEz9aEbOa+M=; b=qV5463o/EF5ElAbpmg2OjciKn9xU3F2cSxfAE2emSGhKMBR/knegJgib142oyIDnCY BMQf6G5Dbdjls0/OvsRzZmvxLQbTnnYvu6mGrYdOQkwVNh6leJSn8uc03nIK1Md8InLp 8IZS8rJaisEUggCPIPYw5gAUqVx2JigzO80UXyMrpARUPC+3r8vTG9UZeEqSNUjwk8iO wTN3hketVxLpEasgOEAVbkWVADnmLEsu5Rt5vlS//K0kYm8mGFoRvrBVZDTYHAFtqmtW fwaop70yKDSOzV+YN1sA65BxC6uufJENzs0Lw6148FM9KgENxYd4QL658MVoO1QJG5FT 2Mfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770156602; x=1770761402; 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=laeArOoHMmZZpQbmwN1GpGAXEElz6gu2HEz9aEbOa+M=; b=KR2lfcvd7Zu0bpYVLqMbfL1acYfjmrP4T4fSz6PKWufmpph41FSEy67dYaMpcMUKpg wi3VyaX+4O5VZs9BbV/mmQZSPhqyHznRfUqV3/kqYiqC84In3QM+Xq7HrHYUyFeBHSrB rT8gjwWCtJF/RhUUtTooho/u9+jih9T5YUws7XbVEv26IknWeuIoUgqp0Fmu0Qqn8Fyv XdgDY6OnpBUNeKkGvKjbhC70QHgcnLKdd+dia9Hl8g//5rjwP+coesGwsgANkC9c7496 4LgXKBPY9TEPZ2fQUCoJHn9NRqm1UiYaBE1azhUUc2ov6a55//n5qTfYjrhyv7aP6GIW JP3Q== X-Forwarded-Encrypted: i=1; AJvYcCXIja6VOqLL2MLm4+9PBFYtXOxM0kE01tfexaqMp5bEkeULhoE31E98GqALdFTFhI81EdIXLwYnNO0VEu0=@vger.kernel.org X-Gm-Message-State: AOJu0YxrE5SkeMlyXUmq3cCooAaJSLxi22w1cWZqmeNTOPtBFgjbaiFy lupaVhRsR4HRfQuWO3Ui4qakNNKLe9Q0lD1L64dHs2KXUamGHNEUpGUlnEap4+uQseyLaWMLCel Bwi6xJ/gKiVSgUw== X-Received: from plbmh14.prod.google.com ([2002:a17:903:9ce:b0:2a0:a0e0:a9c3]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2448:b0:2a8:2c4a:3570 with SMTP id d9443c01a7336-2a933febfb8mr7195225ad.49.1770156601503; Tue, 03 Feb 2026 14:10:01 -0800 (PST) Date: Tue, 3 Feb 2026 22:09:41 +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-8-skhawaja@google.com> Subject: [PATCH 07/14] iommu/vt-d: Restore IOMMU state and reclaimed domain ids 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" During boot fetch the preserved state of IOMMU unit and if found then restore the state. - Reuse the root_table that was preserved in the previous kernel. - Reclaim the domain ids of the preserved domains for each preserved devices so these are not acquired by another domain. Signed-off-by: Samiullah Khawaja --- drivers/iommu/intel/iommu.c | 26 +++++++++++++++------ drivers/iommu/intel/iommu.h | 7 ++++++ drivers/iommu/intel/liveupdate.c | 40 ++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index c95de93fb72f..8acb7f8a7627 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -222,12 +222,12 @@ static void clear_translation_pre_enabled(struct inte= l_iommu *iommu) iommu->flags &=3D ~VTD_FLAG_TRANS_PRE_ENABLED; } =20 -static void init_translation_status(struct intel_iommu *iommu) +static void init_translation_status(struct intel_iommu *iommu, bool restor= ing) { u32 gsts; =20 gsts =3D readl(iommu->reg + DMAR_GSTS_REG); - if (gsts & DMA_GSTS_TES) + if (!restoring && (gsts & DMA_GSTS_TES)) iommu->flags |=3D VTD_FLAG_TRANS_PRE_ENABLED; } =20 @@ -670,10 +670,16 @@ void dmar_fault_dump_ptes(struct intel_iommu *iommu, = u16 source_id, #endif =20 /* iommu handling */ -static int iommu_alloc_root_entry(struct intel_iommu *iommu) +static int iommu_alloc_root_entry(struct intel_iommu *iommu, struct iommu_= ser *restored_state) { struct root_entry *root; =20 + if (restored_state) { + intel_iommu_liveupdate_restore_root_table(iommu, restored_state); + __iommu_flush_cache(iommu, iommu->root_entry, ROOT_SIZE); + return 0; + } + root =3D iommu_alloc_pages_node_sz(iommu->node, GFP_ATOMIC, SZ_4K); if (!root) { pr_err("Allocating root entry for %s failed\n", @@ -1614,6 +1620,7 @@ static int copy_translation_tables(struct intel_iommu= *iommu) =20 static int __init init_dmars(void) { + struct iommu_ser *iommu_ser =3D NULL; struct dmar_drhd_unit *drhd; struct intel_iommu *iommu; int ret; @@ -1636,8 +1643,10 @@ static int __init init_dmars(void) intel_pasid_max_id); } =20 + iommu_ser =3D iommu_get_preserved_data(iommu->reg_phys, IOMMU_INTEL); + intel_iommu_init_qi(iommu); - init_translation_status(iommu); + init_translation_status(iommu, !!iommu_ser); =20 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) { iommu_disable_translation(iommu); @@ -1651,7 +1660,7 @@ static int __init init_dmars(void) * we could share the same root & context tables * among all IOMMU's. Need to Split it later. */ - ret =3D iommu_alloc_root_entry(iommu); + ret =3D iommu_alloc_root_entry(iommu, iommu_ser); if (ret) goto free_iommu; =20 @@ -2110,15 +2119,18 @@ int dmar_parse_one_satc(struct acpi_dmar_header *hd= r, void *arg) static int intel_iommu_add(struct dmar_drhd_unit *dmaru) { struct intel_iommu *iommu =3D dmaru->iommu; + struct iommu_ser *iommu_ser =3D NULL; int ret; =20 + iommu_ser =3D iommu_get_preserved_data(iommu->reg_phys, IOMMU_INTEL); + /* * Disable translation if already enabled prior to OS handover. */ - if (iommu->gcmd & DMA_GCMD_TE) + if (!iommu_ser && iommu->gcmd & DMA_GCMD_TE) iommu_disable_translation(iommu); =20 - ret =3D iommu_alloc_root_entry(iommu); + ret =3D iommu_alloc_root_entry(iommu, iommu_ser); if (ret) goto out; =20 diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 70032e86437d..d7bf63aff17d 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -1283,6 +1283,8 @@ int intel_iommu_preserve_device(struct device *dev, s= truct device_ser *device_se void intel_iommu_unpreserve_device(struct device *dev, struct device_ser *= device_ser); int intel_iommu_preserve(struct iommu_device *iommu, struct iommu_ser *iom= mu_ser); void intel_iommu_unpreserve(struct iommu_device *iommu, struct iommu_ser *= iommu_ser); +void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu, + struct iommu_ser *iommu_ser); #else static inline int intel_iommu_preserve_device(struct device *dev, struct d= evice_ser *device_ser) { @@ -1301,6 +1303,11 @@ static inline int intel_iommu_preserve(struct iommu_= device *iommu, struct iommu_ static inline void intel_iommu_unpreserve(struct iommu_device *iommu, stru= ct iommu_ser *iommu_ser) { } + +static inline void intel_iommu_liveupdate_restore_root_table(struct intel_= iommu *iommu, + struct iommu_ser *iommu_ser) +{ +} #endif =20 #ifdef CONFIG_INTEL_IOMMU_SVM diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupd= ate.c index 82ba1daf1711..6dcb5783d1db 100644 --- a/drivers/iommu/intel/liveupdate.c +++ b/drivers/iommu/intel/liveupdate.c @@ -73,6 +73,46 @@ static int preserve_iommu_context(struct intel_iommu *io= mmu) return ret; } =20 +static void restore_iommu_context(struct intel_iommu *iommu) +{ + struct context_entry *context; + int i; + + for (i =3D 0; i < ROOT_ENTRY_NR; i++) { + context =3D iommu_context_addr(iommu, i, 0, 0); + if (context) + BUG_ON(!kho_restore_folio(virt_to_phys(context))); + + if (!sm_supported(iommu)) + continue; + + context =3D iommu_context_addr(iommu, i, 0x80, 0); + if (context) + BUG_ON(!kho_restore_folio(virt_to_phys(context))); + } +} + +static int __restore_used_domain_ids(struct device_ser *ser, void *arg) +{ + int id =3D ser->domain_iommu_ser.did; + struct intel_iommu *iommu =3D arg; + + ida_alloc_range(&iommu->domain_ida, id, id, GFP_ATOMIC); + return 0; +} + +void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu, + struct iommu_ser *iommu_ser) +{ + BUG_ON(!kho_restore_folio(iommu_ser->intel.root_table)); + iommu->root_entry =3D __va(iommu_ser->intel.root_table); + + restore_iommu_context(iommu); + iommu_for_each_preserved_device(__restore_used_domain_ids, iommu); + pr_info("Restored IOMMU[0x%llx] Root Table at: 0x%llx\n", + iommu->reg_phys, iommu_ser->intel.root_table); +} + int intel_iommu_preserve_device(struct device *dev, struct device_ser *dev= ice_ser) { struct device_domain_info *info =3D dev_iommu_priv_get(dev); --=20 2.53.0.rc2.204.g2597b5adb4-goog