From nobody Wed Oct 1 21:24:19 2025 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 8B51525B1E0 for ; Sun, 28 Sep 2025 19:07:43 +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=1759086465; cv=none; b=I+cY/6yMCDSkka7jv2fZ7LXzXntzQhzmW/JluNwraDt97PGEbhF1wxg2td3g7nv/qwP1upxtpJd/KyOLwe/yTSDelS8JbHcJU7th0gMb1+vQV7dcNseKhciCENsfEL7+w3qELMMIG/0GzqvSqFH7JpEacqhsrx6OS8gQX+X+8BI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086465; c=relaxed/simple; bh=K5fHpHWFykCx8GSNl+bRBubKB+z9YJLFyN3qkeQz+gs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=B5mST3ipSnkLfgA4gZkPlGsyC2DTT5bC3fT+nOaVSkblyjd08x9aMBuUFPEdwDEHvdHvFjss+Tsl47a24QcZm9lRwWaH0+16b2/bHDnHRxW1XgVLKuzOyycPuXyM9Q+NC4Yxf8KYWBdfFns7cdBmrcV+Lg2WKk/xsWuh9ioT3lI= 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=GV3GfVZm; 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="GV3GfVZm" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2711a55da20so31851565ad.1 for ; Sun, 28 Sep 2025 12:07:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086463; x=1759691263; 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=uCuO86qV7dZm/aG2Dppf5kSqYkYatsl7iuzcTQdCTDQ=; b=GV3GfVZmiepgSw60vYBkuo+4Jn3eNhSgmv3tBMkqRH2UVbzxuSlGsuNAtxwQ8tNWWE 6i1GxecoVWL3geLV6e+5CmfpgrcyaRE1xqXm9zmnXVaVDFsC3agyJ1bicltByYQSMglM YR8b1jk17xvmdnysYEBB1ZCwxzHCbYXECXcE2UM5hKW2bOpIAyRMKvCIPF3nH/V9UlcF E0RjZxk4bxGplmtba2ASH1bPc+rySRLRzJTerh/r6N/NbcZW1cDm58y3/mc90q/VWtF3 l0C5YSp2wosgbQyqfkioCt7UyLBT2jL6HbICwjDd5tCD7dYKVK00fAQjcLQTywbwAHwU 2NQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086463; x=1759691263; 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=uCuO86qV7dZm/aG2Dppf5kSqYkYatsl7iuzcTQdCTDQ=; b=aRLsf9aPFs4KxrrLcwFE7sRYph0FTWn3A1l7aYsQW0jH++bZEgnrvbDe9vgsUj9oDh AXOg3jUPd+Udn90aQQ3OxPTf8FGX6x+39dF/jcPXicPjOkkq5Duy6/K3dCHrWi8BaVRL BvqCIIhA9NeDsXpZl3TgO5SMGdDnAhRtuanW48voSG4yElvpu8Cjl7j5lDr2hfTVczHZ nqA0W2G3SInnZJ68eBtdMe2O6MJLzvH74cxpaXxNvmaI86uHFdTw/1j6CBREj2IPeiHl KfS/o2uuUq7spx/GmiPGbNkBd5RqGQGo1Qjh2bojfbbK4o6/73RlVsrMvzi6u6Ic46Dh 3kJg== X-Forwarded-Encrypted: i=1; AJvYcCWBPX8DqVepg/E1J22EODHF85hiOUaPzqtOaurWgz+IPqEeDXl4nRHDiC47U74cy0IzXQ4n0UiyMOIMFpM=@vger.kernel.org X-Gm-Message-State: AOJu0YyRGBIEVilDUVg3TU23nD8dPQyKDpDtbZGs8TmYYElAlDBsIpdT sIeUBRFEL3WtBIg5TGol8mouMtHhVtErrDFaOHQZ0Fxyk5+eb7VKQzgz6KItx8zwFy/TxFCCc5b 7PoH5LTRa8ue61g== X-Google-Smtp-Source: AGHT+IGpJ49K/7HkT1GORRMDUMXXC7GDu2XGPfU5JdrnHAHFnDeqzWXWkVU/StxzhY59C7N/ChZl6seiFoS9WQ== X-Received: from pjbaz14.prod.google.com ([2002:a17:90b:28e:b0:32d:e264:a78e]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d2d0:b0:25c:b6fb:778 with SMTP id d9443c01a7336-27ed49fbbd6mr146672005ad.14.1759086462824; Sun, 28 Sep 2025 12:07:42 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:09 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-2-skhawaja@google.com> Subject: [RFC PATCH 01/15] iommu/vt-d: Register with Live Update Orchestrator 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 , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, zhuyifei@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Register Intel IOMMU driver with live update orchestrator as subsystem. Add stub implementation of the prepare, cancel and finish callbacks. Signed-off-by: Samiullah Khawaja --- MAINTAINERS | 2 ++ drivers/iommu/intel/Makefile | 1 + drivers/iommu/intel/liveupdate.c | 45 ++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 drivers/iommu/intel/liveupdate.c diff --git a/MAINTAINERS b/MAINTAINERS index baeda8a526aa..e038cdd6aa41 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14228,6 +14228,7 @@ F: tools/testing/selftests/livepatch/ LIVE UPDATE M: Pasha Tatashin R: Pratyush Yadav +R: Samiullah Khawaja L: linux-kernel@vger.kernel.org S: Maintained F: Documentation/ABI/testing/sysfs-kernel-liveupdate @@ -14235,6 +14236,7 @@ F: Documentation/admin-guide/liveupdate.rst F: Documentation/core-api/liveupdate.rst F: Documentation/mm/memfd_preservation.rst F: Documentation/userspace-api/liveupdate.rst +F: drivers/iommu/intel/liveupdate.c F: include/linux/liveupdate.h F: include/uapi/linux/liveupdate.h F: kernel/liveupdate/ diff --git a/drivers/iommu/intel/Makefile b/drivers/iommu/intel/Makefile index ada651c4a01b..58922d580c79 100644 --- a/drivers/iommu/intel/Makefile +++ b/drivers/iommu/intel/Makefile @@ -6,3 +6,4 @@ obj-$(CONFIG_INTEL_IOMMU_DEBUGFS) +=3D debugfs.o obj-$(CONFIG_INTEL_IOMMU_SVM) +=3D svm.o obj-$(CONFIG_IRQ_REMAP) +=3D irq_remapping.o obj-$(CONFIG_INTEL_IOMMU_PERF_EVENTS) +=3D perfmon.o +obj-$(CONFIG_LIVEUPDATE) +=3D liveupdate.o diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupd= ate.c new file mode 100644 index 000000000000..d73d780d7e19 --- /dev/null +++ b/drivers/iommu/intel/liveupdate.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025, Google LLC + * Author: Samiullah Khawaja + */ + +#define pr_fmt(fmt) "iommu: liveupdate: " fmt + +#include +#include + +static int intel_liveupdate_prepare(struct liveupdate_subsystem *handle, u= 64 *data) +{ + pr_warn("Not implemented\n"); + return 0; +} + +static void intel_liveupdate_cancel(struct liveupdate_subsystem *handle, u= 64 data) +{ + pr_warn("Not implemented\n"); +} + +static void intel_liveupdate_finish(struct liveupdate_subsystem *handle, u= 64 data) +{ + pr_warn("Not implemented\n"); +} + +static struct liveupdate_subsystem_ops intel_liveupdate_subsystem_ops =3D { + .prepare =3D intel_liveupdate_prepare, + .finish =3D intel_liveupdate_finish, + .cancel =3D intel_liveupdate_cancel, +}; + +static struct liveupdate_subsystem intel_liveupdate_subsystem =3D { + .name =3D "intel-iommu", + .ops =3D &intel_liveupdate_subsystem_ops, +}; + +static int __init intel_liveupdate_init(void) +{ + WARN_ON_ONCE(liveupdate_register_subsystem(&intel_liveupdate_subsystem)); + return 0; +} + +late_initcall(intel_liveupdate_init); --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 1C05725C816 for ; Sun, 28 Sep 2025 19:07:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086466; cv=none; b=acyEj+lX4I09z6IQMv1u9UsX4VswlhdMZXR61uufeR1Iu9wXZ2jfHw64P87P5dNyQMDZQLFScyhWle3ugr+uh5Y/lhd8uSALyIpkXwmJ0kdoBbQDwOdeZ4R5d2ABDyKWmmVgUgrvBgKkvmxh+OVSsVyRmVwqtT+NZAmcsRxKwms= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086466; c=relaxed/simple; bh=TwkDwodBUCdAT08Dx1368hcmJsMEk9iOzMuO3YTeHgw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pgFpJtv0kzmh7Xg5wgbgJCXYl5aLXY+/SJNCIYtcLyI1QuPNYwlIKAjWv3xH+kxPF1+UXKX+V6MbhPDR87t8gC8tFuE7wt5LoSHi11nPbuk3xU4LrJ5qWMWj1mfe58h1o1gSOpu+Xz3g6rCuHN+GqA4ODv1pzu0o1hpdJbdygwU= 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=Ac23Ridm; arc=none smtp.client-ip=209.85.210.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="Ac23Ridm" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-78102ba5966so3465590b3a.2 for ; Sun, 28 Sep 2025 12:07:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086464; x=1759691264; 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=IKcQOPAuV/AfXb11Bbc+ExvF6Mw54uI2kbWY/7dTIhc=; b=Ac23RidmF81xVkhnxmFW2pn+gdL+1B3q0PPRCI0CzCBnSiw3mJj1hvrmx2hIqHBvBO TeL9ApJj9ybfMEczgBrbkCO6iGbvlNjcYeMMaMA8sFRC081C+xETeOxaL2oLQHNXOn3L +XzyAobU0CPNO9tMmeRV4UqfrdNi5AouGwbheh12TK+wZekzBlGNLjVtb+1nuWmcHs2z 5uXBjStQK1ohZpHlUkQlzu+jkl3ZqGYJYthE6PChQqaFX6ML2ARMetY4CkJeyH58tmHj 3hsvvg+/1orvX1EkdwdMtih5fMQal8UYaNROwPpF9pShJQAx6lzFV6SbwLP2guILfdOv AjEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086464; x=1759691264; 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=IKcQOPAuV/AfXb11Bbc+ExvF6Mw54uI2kbWY/7dTIhc=; b=b9Xsd4rVPZlB/miGG4svI+yWaJlv24smVszcsP/7enEV8/febf0isHHbUImGi0tQHk doJXsGB+GmVnb0wZFtVTpkhZml4c4tKyih9bJt53vmrFfNrSeFhjrKE28lBuifA08dAp MPo+XJlHtnulwaR2osgDLX9ezBALuRDPNXCzZHr6wpB2qyAAzKBwq3a3hZjx2noxfzaK 90V0aaZAIMCQHGpdgZNarhXVdxclycskA3Iqda+oV9OsP9GiNrozWhra1wTQSFTwFRSN 868U0qXJ9YwyDQg8gKvLTcmJDArLtSnJnMvu0Dr6YdYhhXxQauRjYhhhrDlFa0CEfDmk 5uhw== X-Forwarded-Encrypted: i=1; AJvYcCUZ86r+/CRnmrh073+dJLqG51ph1smxE5y3yBZLLzPMiSCU5abRS0dT/EE68w5lhNX5jEhpZ2AoRgLsrVI=@vger.kernel.org X-Gm-Message-State: AOJu0Yx+F6nxWbig5J4RITbeW31PNTRX819uAWCTEqjYEiw6zDEwIHME yMYH+B39gyg16LnE0syVqLY6zGLizRb+tSheiMegBANn49Yv75BY1uDR3FPX5KMpBtwPfzAaW1O AP2OCPA+w0FuSPw== X-Google-Smtp-Source: AGHT+IHDYriJ2CM6vbXXafroWapgFfQBlAXQ04VVe+gXkI/tWSYoUN/rx+IsvTB3qwxKgNIrgENnlZqvrAEbqw== X-Received: from pfwz13.prod.google.com ([2002:a05:6a00:1d8d:b0:781:2040:d72b]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:8cb:b0:77f:efd:829b with SMTP id d2e1a72fcca58-780fceba2ffmr14102660b3a.22.1759086464308; Sun, 28 Sep 2025 12:07:44 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:10 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-3-skhawaja@google.com> Subject: [RFC PATCH 02/15] iommu: Add rw_semaphore to serialize live update state 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 , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, zhuyifei@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a rw_semaphore to protect the IOMMU live update state. When a live update operation (prepare, cancel, etc.) is in progress, the underlying state of the IOMMU subsystem (e.g., the set of active hardware units, the state of preserved domains) must not be changed by concurrent events. This semaphore acts as a subsystem-wide lock to serialize the LUO callbacks against any other code path that might modify this state, such as IOMMU hardware hotplug. The LUO callbacks take a write lock, as they modify the live update state. Other code paths should take a read lock to check if a live update is in progress. Signed-off-by: Samiullah Khawaja --- drivers/iommu/iommu.c | 4 ++++ include/linux/iommu.h | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 060ebe330ee1..bfa7c8653720 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2052,6 +2052,10 @@ struct iommu_domain *iommu_paging_domain_alloc_flags= (struct device *dev, } EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc_flags); =20 +#ifdef CONFIG_LIVEUPDATE +DECLARE_RWSEM(liveupdate_state_rwsem); +#endif + void iommu_domain_free(struct iommu_domain *domain) { switch (domain->cookie_type) { diff --git a/include/linux/iommu.h b/include/linux/iommu.h index c30d12e16473..d23d078f7c18 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -598,6 +598,15 @@ __iommu_copy_struct_to_user(const struct iommu_user_da= ta *dst_data, __iommu_copy_struct_to_user(user_data, ksrc, data_type, sizeof(*ksrc), \ offsetofend(typeof(*ksrc), min_last)) =20 +#ifdef CONFIG_LIVEUPDATE +extern struct rw_semaphore liveupdate_state_rwsem; +#define guard_liveupdate_state_read() guard(rwsem_read)(&liveupdate_state_= rwsem) +#define guard_liveupdate_state_write() guard(rwsem_write)(&liveupdate_stat= e_rwsem) +#else +#define guard_liveupdate_state_read() +#define guard_liveupdate_state_write() +#endif /* CONFIG_LIVEUPDATE */ + /** * struct iommu_ops - iommu ops and capabilities * @capable: check capability --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 8AE3C25D1E9 for ; Sun, 28 Sep 2025 19:07:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086468; cv=none; b=T0kXRyJelbQUIWuuuhE3fdO34Q3LRlQ0pim2gFEoLaooxoArkVET7WQ2jnZixufcr/7Ohmjd/O1QKP+jHajPktrCXPmfNw1BPuHgDnWs7yyFWhRSao7FIAf545jsgkn26vHgdRjZGrrVRb7Ic/ZJJeijukJirqwk3NltLpBn9wk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086468; c=relaxed/simple; bh=8L0DFfHrZlNZX0VjY4FjLPOo4H3abf6EW6LzlAWKOaY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mZ9bVozzAH/Grzxcq2TE0CwJ1WIYPcibPvzdrh1DmfJpuy+D4bnCb1yQe7ulpdUT1jniYmZaCVZg331f1tr8gzAyPlbcG3s8QEBLaySTnMYmxOszJOekHn0hl1KmP10Jd8GmAXg6YhEOHg40CsaJDNayU9f8JP/SHHs3i4mWfDE= 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=ybyFXCPb; arc=none smtp.client-ip=209.85.210.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="ybyFXCPb" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7847cc45409so317497b3a.3 for ; Sun, 28 Sep 2025 12:07:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086466; x=1759691266; 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=unF6bUsLM9O1vzcXGUHanadtXOZ35cPd5k0W1K58MJE=; b=ybyFXCPbynpfm2KlLj+2TEFZoa4PM4pDiN2K5OaujpQi/GCmCuyrd26fJQP7h4HSy+ 0bcbBXvdWAUOEqqNELznZ+x9ePrKljX4EtTFy4hIej9oT7o4U/zZB4cPFSdx7IY5967s K5DAOsw6ruBauAugfepZebJT2WcG/E5H/RwYOUfA5TTF4K7lx0gQleANyN3Uwgw+xL7H cvtP9jo2LppP7PIF3gtB+v0f8KLse6tJVjyhMlAf76HFH7veRXHd61Sc5vRpw1CnCzRx BaH8Rb42GPxK6hKeAelqo41duQDTVSwUPHPmcb8d9i4mgnZUFnSwDAtMHsWKbQITtvED kkew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086466; x=1759691266; 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=unF6bUsLM9O1vzcXGUHanadtXOZ35cPd5k0W1K58MJE=; b=boLilNUiiJb3Wef5qq2thjmG///n+ZQRYTj8Wz15zB1y5goHgiw9ZKZu9E3CaLyXj/ 2+prW9W8Zau3LmIa1PvMbN2HhqS1Q8MlKOQEkcHbTLG3waMTyP1YePSQ7SObYQj0VlRx YWQiP2iBqmOYAjxy5tSz5IYhZPILqGeS9z2T+pvglk4jIDwYpsnd5TdbmgACxZJw6bKf E+6Bd6GuJTYhjKRyWITaFGA/D84VAp8gyn1BdPXvHWiXeYbecITwZhqUtj3ppEzjbcZI JPJGUxGjq4RhrD1GV8DTU8eJcAa88B7i+00OYA7fp8RhHHkkqpoeLQwgo21y3YbirJby 9baQ== X-Forwarded-Encrypted: i=1; AJvYcCU3HnFUFbct/Wwxf2T5+Ic/K58qo5ILJzQ2YkHoJWr3epTk1B4B9Z7YXxgc1vBBqn7nhZlup9C+Vg8jj/U=@vger.kernel.org X-Gm-Message-State: AOJu0YwmUraZaNrkU2AJ7cgfkrChqgRNe+LjVwTyCYuysYAD5DUm2Cfa BGUtiWnWV7eCiZzMvV5UDFZNhVx7LO2qMbrhbZuncjcgJq/DXwU2VcJsQri9oqr1YI9cs44x52O 2CBNNtgrf9uvowg== X-Google-Smtp-Source: AGHT+IFHeLeByvDx3H7Fv/UY9IPnM3xJSFbkXHDcYCobgRG472vqOkunFnZPCYmzKNVjAjQUoI+lXVkr/Z2l0Q== X-Received: from pfht2.prod.google.com ([2002:a62:ea02:0:b0:776:2281:3189]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:6a26:b0:2e3:a914:aacd with SMTP id adf61e73a8af0-2e7be0668c2mr18263550637.2.1759086465857; Sun, 28 Sep 2025 12:07:45 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:11 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-4-skhawaja@google.com> Subject: [RFC PATCH 03/15] iommu/vt-d: Prevent hotplugs when live update state is not normal 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 , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, zhuyifei@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hotplugs should not be allowed when the live update state is not normal. This means either we have preserved the state of IOMMU hardware units or restoring the preserved state. The live update semaphore read lock should be taken before checking the live update state. Signed-off-by: Samiullah Khawaja --- drivers/iommu/intel/dmar.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c index ec975c73cfe6..248bc7e9b035 100644 --- a/drivers/iommu/intel/dmar.c +++ b/drivers/iommu/intel/dmar.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -2357,6 +2358,10 @@ static int dmar_device_hotplug(acpi_handle handle, b= ool insert) if (tmp =3D=3D NULL) return 0; =20 + guard_liveupdate_state_read(); + if (!liveupdate_state_normal()) + return -EBUSY; + down_write(&dmar_global_lock); if (insert) ret =3D dmar_hotplug_insert(tmp); --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 310152609EE for ; Sun, 28 Sep 2025 19:07:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086470; cv=none; b=XEInyw7oGQ7HkddBP5XyGt3oUXR2VkPIkuk9dZ6XT8/aDQVCaF/uFShP2WyBvfuvdoUZuapyvq9QnF6Li9wyIdzaVQ9Dq3HCWDF86GVTc/vm0Z1LWi8DQA6xmHZZkjVyHwpBWpj99NSVxAmW8ivcMJtzpo/sPM48nChw+cHH3To= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086470; c=relaxed/simple; bh=zDH31s7rsPYxIzn24EVtvl9OZhYo2h1GDjEToyCXHhU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FQi70l3TN1ePj/NbbsWjfhVhk34bGaUClLfXuXIvF2pCruZx3O4TdduIB0C06x8+kHkZeB3cmL4sqBAmQ2GpqJ7+3gXUMxKj1VyzKhBbjSorpNiFR5J0/Cgu2N4DWz0yHaK8ZB7oH22aLv2uFKZLsphxriPucRqTkXsZRfbs7po= 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=mnFTZE0v; arc=none smtp.client-ip=209.85.210.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="mnFTZE0v" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-78117fbda6eso2468186b3a.3 for ; Sun, 28 Sep 2025 12:07:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086467; x=1759691267; 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=XKD+6rR+ZCf718VijhXNO1kt7/wJrzJ8IPqx+tgskHE=; b=mnFTZE0vR2GT6QLf/UIMxM3z8PRaxZF4tJcVT/jnT5DWr3ZzQAANG4VPoLkhRceR+N cvfYHwD4bUnOhNvg0jk35wnhOn7BWragZaAJsFMypKxh0D7UXyzlHY5TtjVaaQ4Z0YXw ycuffM4v5ZShQ2oc5cR93xJaPrZJCQ5++bil+6Pij6T1km4QZZODtwipINNJlUfRQbn5 a/MUHI0naypMsa930t4jVmpzZDf55ujfiH/58LukhamCEBTpu0J6vAIZpp+orJGNjgRH tA21RaagswYxDbFdezP33lqfU8ogm/4Y3D4C8DubSxDasj8R2c1u+sBAOfsF3Xt1l+6c Kscw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086467; x=1759691267; 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=XKD+6rR+ZCf718VijhXNO1kt7/wJrzJ8IPqx+tgskHE=; b=mvft9Ud8h7P1fZxmK7e0ghsGgrHNju1oOYENP5pMr2+2+MvFPV++xpzpYGXqq2d0pa d2EkwONCQ5gOyOHtUk7M6ReCFPpLrudP8psKn1EPO3qr6g/vZLJ+ifDdmqmfZugfppPJ Z2SN+OLkSMF2HXQZ/W8Ka7lI1LTWOChm7Qzpg3Q1ydmPxjoHh+A9D7T5AKfQa4ClU2Ve hIiaTJox+zKGMzyAEiaYJ3Z9nJKq3Y/ztGzAtpC1ighyD20vKwfSPvt/Z7cLA0OWuLFp sq2/gxwFdVDkl1S8wGEEONlf4fQHmI3117SV5jCNHTuMfzOPTUhP4YACvinW80Q9wYaq 1HVA== X-Forwarded-Encrypted: i=1; AJvYcCUnV5Amu/Zi2cWXLEPAM6lkLOZUAQdRzwfmy/SRIt0dNKXrKYnlYeB9jCGV+72gkBnCN+pLiJ9zwnyAAec=@vger.kernel.org X-Gm-Message-State: AOJu0YwjTgCzD3Rr08+FZ2vjyuILI7FGYoujzyuM/ezkmOY/i5b+vy6G g2Eu4/lYZWe8GXkd+OyI2aAZbda10xqCVNXVPyueRGEtRblQwtxFEjKZJ1nL0UpBeG3Ig4q9qmU jYvDj0UGc9xyjpA== X-Google-Smtp-Source: AGHT+IGttSbQUUKNc7Fj+s9+oJWDjLUfJmgHpOgznyqZWeNNRCg9RbZUUOUsFhC6DOgwPPznvL3k5goTDcPGbA== X-Received: from pfbfp10.prod.google.com ([2002:a05:6a00:608a:b0:76b:651e:a69c]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3c8b:b0:781:2757:1b4f with SMTP id d2e1a72fcca58-78127571ef2mr4881787b3a.7.1759086467484; Sun, 28 Sep 2025 12:07:47 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:12 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-5-skhawaja@google.com> Subject: [RFC PATCH 04/15] iommu: Add preserve iommu_domain op 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 , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, zhuyifei@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an optional preserve iommu_domain op that can be implemented by the iommu drivers to preserve the iommu domain. Signed-off-by: Samiullah Khawaja --- include/linux/iommu.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index d23d078f7c18..40801d8eac61 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -758,6 +758,8 @@ struct iommu_ops { * specific mechanisms. * @set_pgtable_quirks: Set io page table quirks (IO_PGTABLE_QUIRK_*) * @free: Release the domain after use. + * @preserve: Preserve the iommu domain for liveupdate. + * Returns 0 on success, a negative errno on failure. */ struct iommu_domain_ops { int (*attach_dev)(struct iommu_domain *domain, struct device *dev); @@ -787,6 +789,7 @@ struct iommu_domain_ops { unsigned long quirks); =20 void (*free)(struct iommu_domain *domain); + int (*preserve)(struct iommu_domain *domain); }; =20 /** --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 72202261573 for ; Sun, 28 Sep 2025 19:07:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086470; cv=none; b=Mly2GemyqFmHeLD1/Y0/J1iCNw2ngAH4LpFV9VochtdgU3PjGV8d6x0ejHdZ1jyI7Tb84bQ89mYMXxJYIo2RORL8s2BlIBlefZyeFWSBg/7PkwKujEHlTuTB0Eilz+bHp3Lzr7njDUV1ZM5wBGU/6+SRXe6ddXgfgaHceG86rj0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086470; c=relaxed/simple; bh=MuX3jZ3sSbwfMCn0UPvl+BEUskcLBHNXaIPs4MYUTto=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UvJGUDcsmvlplLHul08lJ2rc+U5eREwABGAT6OdIsWlveMpa4gLiCgNFB4h0mpVb75Lihtim64imbNdvKne30S1B1oeG5M6k7lWHQ4Vs05FUEE1+7UrcaL27JUOc8+zsNt9Gy2O0gzYl2LRgxk0Mh/2y3wpqepY1HgGgyKcbzPc= 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=DbY5qTnW; arc=none smtp.client-ip=209.85.210.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="DbY5qTnW" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7847cc45409so317516b3a.3 for ; Sun, 28 Sep 2025 12:07:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086469; x=1759691269; 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=rdi2PyKiCCjIQ8wy03XR3Gwap8/Sh7/Zx1Xvfs/uPkQ=; b=DbY5qTnWSS+/nluO92jyrGiov/dbTYh9OkTEX0TykWM9igSFQMvHxSlTsGV/ukO9Kz ikdzO4Qvlns4qzi2QYSKCKtoRoGHQx2suMKFYx2HwB3NVKYtDNrtWiyYNBHIvTHcmGpN EQKKW/h8LoM1W91Rbbmxm72wAJx9sRORIWDhF5VnzePvn4EzyHqXBn3OSMzz5lQpEo1G a19TGWHRjEifQjbt+D72jnklKyzr0xrT9vkg1LW/31ROSkeEn77z51TD+MztwUJb9KE2 i/xBicRUkh4mvvITSQdwVJOMrJ3JZ+6UPb306lOCs/wHxyZTjmVGLJr6tTqJpvdChH3j AhqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086469; x=1759691269; 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=rdi2PyKiCCjIQ8wy03XR3Gwap8/Sh7/Zx1Xvfs/uPkQ=; b=p47y+BFIEKJw3+UCTz1ICw3j4w4lnL7IT4CsVBPwvvQsTgNT03bVC7NMviPFMyovXT cNZ3tHo1q/odUtIJN0hhxCBhGg1QEiMTI9FjZHjlxHDb39tDSZVaVfM0jzNnfZKzDAhJ mNpxEd4DhOozfqb9DDv4MEKyMlj23KKs5+lDdKR+CFn/87xSh/UU1Xa8ejxHAATcQaek LhonmgZnZTNWdt5YgG5RxcREL26ICRKlQs+2wP6XLhDxz+WxriW96wVZ/IYQAf8wjHoE quko6As/J89ZfeEN1rMIdY0lqNQslw9Deod8AlSsMqi55ono1fGX+8bK2YCxn2/e7noY 4Zig== X-Forwarded-Encrypted: i=1; AJvYcCU5qRFShzOx4tt1z+7EukN+m3hxy5Hytyc7JV+T7bwIbvhfAWwMOYYy1hZYJZo0++Gz3GbCbP9xzN75snI=@vger.kernel.org X-Gm-Message-State: AOJu0Yyt+PWc8YkdUP9wgxi289fgB402MxgXA9VIRbvB5gJP4HDGV1yZ UEoi8BLA1HWpdlG8A70PsyBqTbkjLQoRr0bRgq5DbiFPJ2pZ9bxx18M2Bi58G71I+SwqEJimb30 rsWnuJlLToDkSSQ== X-Google-Smtp-Source: AGHT+IE9CnlJcLdiotMu3CPWOFGa4Qbbs3VcUOyfssl5nXIo/nwxXTHS8424T9CoJW4l01vDjDvbImmW+vK32A== X-Received: from pfbde1.prod.google.com ([2002:a05:6a00:4681:b0:77e:468d:c50d]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:80ec:b0:77e:325b:f614 with SMTP id d2e1a72fcca58-780fce1fe2bmr12364767b3a.12.1759086468856; Sun, 28 Sep 2025 12:07:48 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:13 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-6-skhawaja@google.com> Subject: [RFC PATCH 05/15] iommu: Introduce API to preserve iommu domain 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 , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, zhuyifei@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an API that can be called by the iommu users to preserve iommu domain. Currently it only marks the iommu_domain as preserved. Signed-off-by: Samiullah Khawaja --- drivers/iommu/iommu.c | 20 ++++++++++++++++++++ include/linux/iommu.h | 10 ++++++++++ 2 files changed, 30 insertions(+) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index bfa7c8653720..2e6e9c3f26ec 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2002,6 +2002,10 @@ static void iommu_domain_init(struct iommu_domain *d= omain, unsigned int type, domain->owner =3D ops; if (!domain->ops) domain->ops =3D ops->default_domain_ops; + +#ifdef CONFIG_LIVEUPDATE + atomic_set(&domain->preserved, 0); +#endif } =20 static struct iommu_domain * @@ -2054,6 +2058,22 @@ EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc_flags); =20 #ifdef CONFIG_LIVEUPDATE DECLARE_RWSEM(liveupdate_state_rwsem); + +int iommu_domain_preserve(struct iommu_domain *domain) +{ + int ret; + + lockdep_assert_held(&liveupdate_state_rwsem); + if (!domain->ops->preserve) + return -EOPNOTSUPP; + + ret =3D domain->ops->preserve(domain); + if (!ret) + atomic_set(&domain->preserved, 1); + + return ret; +} +EXPORT_SYMBOL_GPL(iommu_domain_preserve); #endif =20 void iommu_domain_free(struct iommu_domain *domain) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 40801d8eac61..aafd06134f5c 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -14,6 +14,7 @@ #include #include #include +#include #include =20 #define IOMMU_READ (1 << 0) @@ -248,6 +249,10 @@ struct iommu_domain { struct list_head next; }; }; + +#ifdef CONFIG_LIVEUPDATE + atomic_t preserved; +#endif }; =20 static inline bool iommu_is_dma_domain(struct iommu_domain *domain) @@ -915,6 +920,11 @@ static inline struct iommu_domain *iommu_paging_domain= _alloc(struct device *dev) { return iommu_paging_domain_alloc_flags(dev, 0); } + +#ifdef CONFIG_LIVEUPDATE +int iommu_domain_preserve(struct iommu_domain *domain); +#endif + extern void iommu_domain_free(struct iommu_domain *domain); extern int iommu_attach_device(struct iommu_domain *domain, struct device *dev); --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 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 E8FD925A2C7 for ; Sun, 28 Sep 2025 19:07:50 +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=1759086472; cv=none; b=LdAV7TqGLdRvEMUlYr2s2CRBuLhEYU1DyRnDerIECDHMXypnewI5oBXo2Oq3VBT1DTAV5iUchglqN6CbVtPRrvI6TlRZ6NUpulSLdf27Y46J2A4lZ6FqrVdk95LaHpUUFTDZ8q4T+bW6TfGl4wG30j8L0Kx3E3XiQC4yblkKNts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086472; c=relaxed/simple; bh=EeovTi/1CEJ8oPAob1Hj6uM2MPZDtSjBccEqrgz0EFQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=e3lGF03bFV+/Xb7PACrX2EDnIBFs6vHaLmdaFpVg5E3YoI7eW63wp19p2WbQZqFwcv1Q8Y/2/+gbgPkt1c6LorCeM9vAU8j3S7N2tMbL3P2Meum0iIZHBUxdtSr9PEAy9cNoDTjk7Gg3f07MwLZ4DnaDKvUYiA18FeO4qlnVQQw= 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=F5/OONwZ; 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="F5/OONwZ" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-27356178876so29150225ad.1 for ; Sun, 28 Sep 2025 12:07:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086470; x=1759691270; 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=TH/9Jtoynp6nkNgBt5pZU0cN3mVwJHyFDfFaXLb+GfA=; b=F5/OONwZi/9UhhytjzF4xJx3mtwD7NjYfk+hGIjoafLd7ygfRmHj0dUWDcjfTYNv95 JTLH1jej1thhAyOpGqGrslK3y5jIkZS+HRCrm5PP/xqSlqLW5TEZP4YFMi6UyhwwtzMs /XLVH778YzfFFVR6vQTE6Tt2at9E/g4OTKrT9prMV4Va0ebenH9kFiMBrepIIRxEL4LM LuaZ39j/feavWPjnqj7lV8tjOMcZ10mBioxPaZtzdOsSkOXm/c5WNTgT5+4NSWdyxWFj Dz0NVph/xMFevhUKMpoghibtbq3g+Hh8r/h8eTO3FnbMCvOcxyA6IMPeIKcqDPD4j2b+ h/cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086470; x=1759691270; 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=TH/9Jtoynp6nkNgBt5pZU0cN3mVwJHyFDfFaXLb+GfA=; b=PFzDndlSiF4aeSGvIsMC11bIoICl28kiY0aLRrmugDfXTxnpo/j0Ahtgz05JYvjtYt mYR5xvFK/JsrEtyeL6oTto60k0LDVN7BrtmfXrfXmYusZIhx+56TXRk0Xvl1mZGyLQq2 7rNlNY9PfOp91K+0QdBGinZgs3ZXxcc31dmSTn4vLM9KQjKoahkUt7dMJEqT906uYuHW KAJh826OubH5c4O3D/uKRHyMeP1lOBeIWMT+VUiO0w8iXktPVSW6SeO2SvG51xR9Ppry lfX0bUb1tI+00El0SkA+uvpVszXDC3BWnhr8fY5y7VkZohvr3zjILaUu8lWBZaaVr4Cv LPkA== X-Forwarded-Encrypted: i=1; AJvYcCUlAakFSmjLQ5S14cywvaRXZBEglvAll3LIvkPSnzc20DiSyVS6HiU7MGw2JjGNx326M8DAHJ20VAbsDwU=@vger.kernel.org X-Gm-Message-State: AOJu0YzXLSW9u6PJ7AIJfNOJtUhPPXx9pecHVhCYG1eh6L8wU4d/5a/K +xg9yF8TOVzL4w/n8d+bWw8Us3Z5nMJ2Dx8E7snkKU/FKn7kdcUigXTx4J+Af1Weh8Eca7foWBm JTg5s2brHkHEjdQ== X-Google-Smtp-Source: AGHT+IEDySm4AN2rcWd3tpaQUgR+ObHMHdJfAIbGRY3+AD4vHP4FnsnrIni5cOLhrFkNB0nMmOkCgtANf3ycPg== X-Received: from pjbjx1.prod.google.com ([2002:a17:90b:46c1:b0:32d:df7e:66c2]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f786:b0:240:3b9e:dd65 with SMTP id d9443c01a7336-27ed4aab57bmr154999485ad.38.1759086470179; Sun, 28 Sep 2025 12:07:50 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:14 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-7-skhawaja@google.com> Subject: [RFC PATCH 06/15] iommu/vt-d: Add stub intel iommu domain preserve op 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 , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, zhuyifei@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a stub implementation of iommu domain preserve for intel iommu driver. This is required so that the iommu_domain is marked as preserved. Signed-off-by: Samiullah Khawaja --- drivers/iommu/intel/iommu.c | 6 ++++++ drivers/iommu/intel/iommu.h | 4 ++++ drivers/iommu/intel/liveupdate.c | 9 +++++++++ 3 files changed, 19 insertions(+) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index e236c7ec221f..7035ffca020f 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4472,6 +4472,9 @@ const struct iommu_domain_ops intel_fs_paging_domain_= ops =3D { .iova_to_phys =3D intel_iommu_iova_to_phys, .free =3D intel_iommu_domain_free, .enforce_cache_coherency =3D intel_iommu_enforce_cache_coherency_fs, +#ifdef CONFIG_LIVEUPDATE + .preserve =3D intel_iommu_domain_liveupdate_preserve, +#endif }; =20 const struct iommu_domain_ops intel_ss_paging_domain_ops =3D { @@ -4485,6 +4488,9 @@ const struct iommu_domain_ops intel_ss_paging_domain_= ops =3D { .iova_to_phys =3D intel_iommu_iova_to_phys, .free =3D intel_iommu_domain_free, .enforce_cache_coherency =3D intel_iommu_enforce_cache_coherency_ss, +#ifdef CONFIG_LIVEUPDATE + .preserve =3D intel_iommu_domain_liveupdate_preserve, +#endif }; =20 const struct iommu_ops intel_iommu_ops =3D { diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 3056583d7f56..6b69232efffd 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -1345,6 +1345,10 @@ static inline int iopf_for_domain_replace(struct iom= mu_domain *new, return 0; } =20 +#ifdef CONFIG_LIVEUPDATE +int intel_iommu_domain_liveupdate_preserve(struct iommu_domain *domain); +#endif + #ifdef CONFIG_INTEL_IOMMU_SVM void intel_svm_check(struct intel_iommu *iommu); struct iommu_domain *intel_svm_domain_alloc(struct device *dev, diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupd= ate.c index d73d780d7e19..a15feef4d9ca 100644 --- a/drivers/iommu/intel/liveupdate.c +++ b/drivers/iommu/intel/liveupdate.c @@ -9,6 +9,14 @@ #include #include =20 +#include "iommu.h" + +int intel_iommu_domain_liveupdate_preserve(struct iommu_domain *domain) +{ + pr_warn("Not implemented\n"); + return 0; +} + static int intel_liveupdate_prepare(struct liveupdate_subsystem *handle, u= 64 *data) { pr_warn("Not implemented\n"); --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 4B8452652B4 for ; Sun, 28 Sep 2025 19:07:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086473; cv=none; b=o1NR/fUmiQmAHuMHf37V/AB2EbJiPUYJIIi+c7h0LlmCGL/Of5HuaM5TBWY82KCDYdZwfsRozTizgQaBOpkLCDUbcz4h6iGYYBpU5Pmfit9jsP4daT3KJd3t5zYFA8p50pjnPDm/BPSXcToT9uF5wRMvMr1Kc93Nzz4NUcnVyGM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086473; c=relaxed/simple; bh=5afXYIp78DkALWn3sB5Jmp4qKGsGM5Fm7+lS4mnhcaA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qtI2I9PTIGZ+UyqChoCKKV8Hg8wlmBOSPXbmnyYe3I4QVF0CZXO8rPnzPD71p5NWFG6dGxoXmIZVV1Lvw1VbFW3NVXyDtAL6vqHQ909fMaPV4ImWyN4lVbvANw97mK6ci/3eSXULhcg61umc988eFBqea1bFXuvEFTI7WLQxDcA= 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=NM9vtxBT; arc=none smtp.client-ip=209.85.215.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="NM9vtxBT" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b55443b4114so2583695a12.2 for ; Sun, 28 Sep 2025 12:07:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086472; x=1759691272; 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=vsU7Kqlniap8aOZDcuZtGEaTcQ9taaF5KqlzYc+vBSw=; b=NM9vtxBTRIiCTVTmD8vMWdvEgg94ehiga03k+AWltTWXb4/mHfpZWoTvb91PulV7Vz hbU+K9kwEbIIZY5v3wejyDn1jB37TWHPJ1EIxIkSwxcxM4vqTfH5Z2pddSuLIfbxcRhD H62IOOmeytSdH3fExu8sITvGnAveSI+zkus795PcobwbyhHnoa6Sinbw+iL/Z1e+dPlq BWr4gkLvJ1XPzw7F1Ikz0fTZ0FCMBMcWMJ3UVu4OY0UINe3mY0G/C9UW+dKaSowlPMk7 1zIHkmSpy1h22KRPz0wg1VbB/GgwucGm9DFz76lWUuVoxLk5gld2Fl0/j5QVn0WVhaoh A2GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086472; x=1759691272; 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=vsU7Kqlniap8aOZDcuZtGEaTcQ9taaF5KqlzYc+vBSw=; b=EM3m6tteCMdvPxeiH2btY95Mn+y96lVo7KVk4Wf1/ndmu/vLBK/UBzEinQGgVZjHfF IsczXgqft9qGnsnzxES3aNPhWqos0cbhiLAeAd2qj1E5XVTkmLsrNfxbAxhPVuJtKZtQ 75THdq+o/9G4rqp8ooCfyltFatsy+7KLhRKftS6MRebH9paQfjjpvr1oam2esWLiyEEd +TtvA/OTDNmKzK3zKyIma7DfDkJlt8YZYxpcule/Hvd0xxw65ICJloUpq9DjNiXb/lI7 vl7dJw7HRTJ+lX+oACGHOhDFeOoS4KvURKsKBsGcctfXA+yX8MU4Oic59eugK9GWPQO6 q5hQ== X-Forwarded-Encrypted: i=1; AJvYcCXHxYqX8CgEvBvrCcZE31eWy0DkhZdsQMktTzJ7HwyFrYGnw/9H0yQin0zvJPz6RtzdOUkZwuhwnAISiT8=@vger.kernel.org X-Gm-Message-State: AOJu0YyJwNBxYSgCZJ7bUqzUc1+gMr8geugJMQ4XV8U6JiEbBH0ujaiV s3/jshYT7HJGBfc+v6WfUiu021iFLy4x3icR8CptUDxXJxbrhmRuAuG4be3daKA7iXovsMfkwwi YlWLxETc+JeLWIw== X-Google-Smtp-Source: AGHT+IHfdirlJBqh06AiPWU9gZRM34sOyCYlhqLwriEV1slCgLDHGPkpGuA/w9oi9uxQsxmHSL03/Is8uy3WGQ== X-Received: from pfrb29.prod.google.com ([2002:aa7:8edd:0:b0:772:749b:de38]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6300:6d1a:b0:2f9:dc8d:d2b3 with SMTP id adf61e73a8af0-2f9dc8dd5e4mr5325467637.22.1759086471611; Sun, 28 Sep 2025 12:07:51 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:15 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-8-skhawaja@google.com> Subject: [RFC PATCH 07/15] iommu/vt-d: Add implementation of live update prepare callback 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 , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, zhuyifei@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Liveupdate prepare callback preserves the iommus of the devices that are attached to a preserved iommu domain. It does this for only pcie devices by iterating through all the pcie devices and checking whether the attached iommu domain is preserved. Only stub implementation of the iommu and device preservation are added with this commit. Signed-off-by: Samiullah Khawaja --- drivers/iommu/intel/dmar.c | 4 + drivers/iommu/intel/iommu.h | 3 + drivers/iommu/intel/liveupdate.c | 150 ++++++++++++++++++++++++++++++- 3 files changed, 156 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c index 248bc7e9b035..cd6ce519c1da 100644 --- a/drivers/iommu/intel/dmar.c +++ b/drivers/iommu/intel/dmar.c @@ -1101,6 +1101,10 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd) ida_init(&iommu->domain_ida); mutex_init(&iommu->did_lock); =20 +#ifdef CONFIG_LIVEUPDATE + atomic_set(&iommu->preserved, 0); +#endif + ver =3D readl(iommu->reg + DMAR_VER_REG); pr_info("%s: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n", iommu->name, diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 6b69232efffd..93ac55eb49f0 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -758,6 +758,9 @@ struct intel_iommu { void *perf_statistic; =20 struct iommu_pmu *pmu; +#ifdef CONFIG_LIVEUPDATE + atomic_t preserved; +#endif }; =20 /* PCI domain-device relationship */ diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupd= ate.c index a15feef4d9ca..94aabf025a60 100644 --- a/drivers/iommu/intel/liveupdate.c +++ b/drivers/iommu/intel/liveupdate.c @@ -6,24 +6,172 @@ =20 #define pr_fmt(fmt) "iommu: liveupdate: " fmt =20 +#include #include #include +#include =20 #include "iommu.h" =20 +struct iommu_unit_ser { + u64 phys_addr; + u64 root_table; +}; + +struct device_ser { + u64 bdf; + u64 pasid_table; + u64 pasid_order; + u64 iommu_phys; +}; + +struct iommu_ser { + u64 nr_iommus; + u64 nr_devices; + + union { + u64 iommu_units_phys; + struct iommu_unit_ser *iommu_units; + }; + + union { + u64 devices_phys; + struct device_ser *devices; + }; +}; + int intel_iommu_domain_liveupdate_preserve(struct iommu_domain *domain) { pr_warn("Not implemented\n"); return 0; +} + +static bool is_device_domain_preserved(struct device *dev) +{ + struct device_domain_info *info =3D dev_iommu_priv_get(dev); =20 + return atomic_read(&info->domain->domain.preserved) =3D=3D 1; } =20 -static int intel_liveupdate_prepare(struct liveupdate_subsystem *handle, u= 64 *data) +static int preserve_device_state(struct pci_dev *dev, struct device_ser *s= er) { pr_warn("Not implemented\n"); return 0; } =20 +static int preserve_iommu_state(struct intel_iommu *iommu, + struct iommu_unit_ser *ser) +{ + pr_warn("Not implemented\n"); + return 0; +} + +static void unpreserve_state(struct iommu_ser *ser) +{ + pr_warn("Not implemented\n"); +} + +static int preserve_state(struct iommu_ser *ser) +{ + struct device_domain_info *info; + struct pci_dev *pdev =3D NULL; + struct dmar_drhd_unit *drhd; + struct intel_iommu *iommu; + int ret =3D 0; + + for_each_pci_dev(pdev) { + if (!is_device_domain_preserved(&pdev->dev)) + continue; + + info =3D dev_iommu_priv_get(&pdev->dev); + if (!info) + return -EINVAL; + + if (ser->devices) + ret =3D preserve_device_state(pdev, &ser->devices[ser->nr_devices]); + + if (ret) + return ret; + + atomic_set(&info->iommu->preserved, 1); + ser->nr_devices++; + } + + for_each_iommu(iommu, drhd) { + if (!atomic_read(&iommu->preserved)) + continue; + + atomic_set(&iommu->preserved, 0); + if (ser->iommu_units) + ret =3D preserve_iommu_state(iommu, &ser->iommu_units[ser->nr_iommus]); + + if (ret) + return ret; + + ser->nr_iommus++; + } + + return 0; +} + +static struct iommu_ser *alloc_preserve_state_mem(void) +{ + struct iommu_ser *ser_ptr; + struct iommu_ser ser; + struct folio *folio; + size_t sz; + int ret; + + memset(&ser, 0, sizeof(ser)); + ret =3D preserve_state(&ser); + if (ret) + goto error; + + sz =3D sizeof(struct iommu_ser) + + (ser.nr_iommus * sizeof(struct iommu_unit_ser)) + + (ser.nr_devices * sizeof(struct device_ser)); + + folio =3D folio_alloc(GFP_KERNEL, get_order(sz)); + if (!folio) + return ERR_PTR(-ENOMEM); + + ret =3D kho_preserve_folio(folio); + if (ret) + goto error_preserve; + + ser_ptr =3D folio_address(folio); + memset(ser_ptr, 0, sz); + ser_ptr->iommu_units =3D (void *)(ser_ptr + 1); + ser_ptr->devices =3D (void *)(ser_ptr->iommu_units + ser.nr_iommus); + + return ser_ptr; + +error_preserve: + folio_put(folio); +error: + return ERR_PTR(ret); +} + +static int intel_liveupdate_prepare(struct liveupdate_subsystem *handle, u= 64 *data) +{ + struct iommu_ser *ser; + int ret; + + guard_liveupdate_state_write(); + ser =3D alloc_preserve_state_mem(); + if (IS_ERR(ser)) + return PTR_ERR(ser); + + ret =3D preserve_state(ser); + if (ret) + unpreserve_state(ser); + + if (!ret) + *data =3D __pa(ser); + + return ret; +} + static void intel_liveupdate_cancel(struct liveupdate_subsystem *handle, u= 64 data) { pr_warn("Not implemented\n"); --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 E47DF269D06 for ; Sun, 28 Sep 2025 19:07:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086475; cv=none; b=OFnT1E7xRubExUlvEt0u363mNQ7RKfj7Rdpx4ScLaLoyjL+Rcvh7hhoJWmtLSCiQJhObdbNw3QzVcHGWjmruohUn7jqspdwDPlp9qD7b8q2dxwYpIykLmc9H6xwalFxUd6KqXZfx0DOc0UKrx4R9JdU9UBcEXNbH9wDUr/hg87U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086475; c=relaxed/simple; bh=QR2Gs/iwXVTCtcLMACOzt1mygqy+KIwEpFthZZnCaC8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tN0RsExJl608HvwFJGZlxh2LQkTzj/JkSYTDPIsLgXgrQ34PMU/K/KBN++DLasnwCl5S3aEWH4S518gP3LRqOvw0jJdVbv4AJt8Spc0+UWZjTdDLDCR82JnzvxNWxo3XSUNcWbVr1Bq5cRf36QFkRKArn4DlP8GfpAnmtHuB6LQ= 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=MOWhe5tY; arc=none smtp.client-ip=209.85.210.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="MOWhe5tY" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-780fb254938so3219005b3a.0 for ; Sun, 28 Sep 2025 12:07:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086473; x=1759691273; 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=QcKsX07BcsLgJUGWNvmQKTHOi+pK9yuzTPJl0W1HDJo=; b=MOWhe5tYuT3LEn+MDfpHj3DlqZTURsmJpmWt+ql9+jF2Tdu6b8jiPDFC+8lwBUap8c o62kPy+97VRcWCkYYgd96Dqt2Mu0NV5nECHJ7+xodthXd4sQ9otWvDimUi75XjHP2mo+ I5goS+20xwZhjpb16MgNW4DlUHnocw+JrZ9+Gkk3RKeLM24vk/4NmiOqSHhqi7fjLHb9 bycdxLtasIE8Xy6ifArmEiGNAlVR5NoOkoWt/ygOvGewpj0yA1oFMVRAE+Jqbdtt1fyp F9vBv+isApQioa4bJ/Bu1I3kZ9yLUXk0rmwWiDc33UuzwIBdWPbh2lNLTu7LgDY2iBU0 fI/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086473; x=1759691273; 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=QcKsX07BcsLgJUGWNvmQKTHOi+pK9yuzTPJl0W1HDJo=; b=X6enHpH1D0vKcUCILGZh5QFmJ8x/hIXSi9Wle3tfk/TE0D8t/gCLSaWv2/7OKWg+84 Xj5TgNDwf5swEgngkJIidLf3iAikWOUBpwoFFhJzK4NVTd2ueOBD0kuiCSUEYfGU+lt8 haVqrvY7BV4Wrq/h4GouF6WSlsh0/b1yLHuOcom5I6YVHE94ia3NeWDvxTuVWn/3LgtT t89Ul34ibEiaEEnc2Ww0q7uTWgaE4pLRA8c+Ywn1jwOqG5JmcqYnZ/cijKYh+HJ+6EP6 7pYO1Vwg+w5AnOSOIDrxpcm2ET8PS4ScgfmMkZizxfbfBrUJu3Vai863xQ0Nzp4F+XJ8 y+vw== X-Forwarded-Encrypted: i=1; AJvYcCUZnTwGlzLAm7BE4FdXxAn7EvoML9KFi7h9VXb8vusgEWjbEqqW5JPeW0uP5IeOvPcSyO9ZZTBNwc26JlQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yyle0ol4S9hK7yHKBxyb3KjdBLtMV3D8NuNZtZu9vE67cq9J5tJ EkBf1z56icFbQKAQTR11z/HuxlswVBhuVJ4XcJIKzZB7sUyRjMgetIEuet/j8V51hqEDD+zlemL seS3lvjK9UkrKnQ== X-Google-Smtp-Source: AGHT+IGFAKSNmw8iYSKRrK9GJvSB49WtExb49RjLckPU3ZxW1Xt1iEJUBMJZeDOD3UW4tcj2DZSh10gOcwSWPA== X-Received: from pfbf6.prod.google.com ([2002:a05:6a00:ad86:b0:77c:37c1:4bef]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:6d5a:20b0:782:ec0f:d279 with SMTP id d2e1a72fcca58-782ec0fd804mr2724979b3a.10.1759086473198; Sun, 28 Sep 2025 12:07:53 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:16 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-9-skhawaja@google.com> Subject: [RFC PATCH 08/15] iommu/vt-d: Implement live update preserve_iommu_context 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 , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, zhuyifei@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add implementation of preserve_iommu_context to preserve the root_table and the associated context tables. Also mark the iommu unit as preserved. Signed-off-by: Samiullah Khawaja --- drivers/iommu/intel/iommu.c | 2 - drivers/iommu/intel/iommu.h | 1 + drivers/iommu/intel/liveupdate.c | 80 +++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 7035ffca020f..caac4fd9a51e 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -67,8 +67,6 @@ static int force_on =3D 0; static int intel_iommu_tboot_noforce; static int no_platform_optin; =20 -#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry)) - /* * Take a root_entry and return the Lower Context Table Pointer (LCTP) * if marked present. diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 93ac55eb49f0..273d40812d09 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -556,6 +556,8 @@ struct root_entry { u64 lo; u64 hi; }; + +#define ROOT_ENTRY_NR (VTD_PAGE_SIZE / sizeof(struct root_entry)) =20 /* * low 64 bits: diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupd= ate.c index 94aabf025a60..fb214736aa3c 100644 --- a/drivers/iommu/intel/liveupdate.c +++ b/drivers/iommu/intel/liveupdate.c @@ -59,11 +59,87 @@ static int preserve_device_state(struct pci_dev *dev, s= truct device_ser *ser) return 0; } =20 +static int unpreserve_iommu_context(struct intel_iommu *iommu, int end) +{ + struct context_entry *context; + int i; + + if (end < 0) + end =3D ROOT_ENTRY_NR; + + for (i =3D 0; i < end; i++) { + context =3D iommu_context_addr(iommu, i, 0, 0); + if (context) + WARN_ON_ONCE(kho_unpreserve_folio(virt_to_folio(context))); + + if (!sm_supported(iommu)) + continue; + + context =3D iommu_context_addr(iommu, i, 0x80, 0); + if (context) + WARN_ON_ONCE(kho_unpreserve_folio(virt_to_folio(context))); + } + + return 0; +} + +static int preserve_iommu_context(struct intel_iommu *iommu) +{ + struct context_entry *context; + int ret; + int i; + + for (i =3D 0; i < ROOT_ENTRY_NR; i++) { + context =3D iommu_context_addr(iommu, i, 0, 0); + if (context) { + ret =3D kho_preserve_folio(virt_to_folio(context)); + if (ret) + goto error; + } + + if (!sm_supported(iommu)) + continue; + + context =3D iommu_context_addr(iommu, i, 0x80, 0); + if (context) { + ret =3D kho_preserve_folio(virt_to_folio(context)); + if (ret) + goto error_sm; + } + } + + return 0; + +error_sm: + context =3D iommu_context_addr(iommu, i, 0, 0); + WARN_ON_ONCE(kho_unpreserve_folio(virt_to_folio(context))); +error: + WARN_ON_ONCE(unpreserve_iommu_context(iommu, i)); + return ret; +} + static int preserve_iommu_state(struct intel_iommu *iommu, struct iommu_unit_ser *ser) { - pr_warn("Not implemented\n"); - return 0; + int ret; + + spin_lock(&iommu->lock); + ret =3D preserve_iommu_context(iommu); + if (ret) + goto error; + + ret =3D kho_preserve_folio(virt_to_folio(iommu->root_entry)); + if (ret) { + unpreserve_iommu_context(iommu, -1); + goto error; + } + + ser->phys_addr =3D iommu->reg_phys; + ser->root_table =3D __pa(iommu->root_entry); + atomic_set(&iommu->preserved, 1); +error: + spin_unlock(&iommu->lock); + return ret; } =20 static void unpreserve_state(struct iommu_ser *ser) --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 8A94026C39B for ; Sun, 28 Sep 2025 19:07:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086477; cv=none; b=bd26PYgh4up4mFBdXT42ea8TTiAun+ugawCxZRyFkVmqQA3vo6IhD+isxrVyKvHJdIOxGyDe/5/eagp7h+q/tptPSAHXsySH0ZnNPYIHEZGskllhASf+xt/1H05CIc4N/fi0FJ5vt+YQmbZTD4Uaf3Us9Hvmz/eFVcupjo20vgw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086477; c=relaxed/simple; bh=jkmwgnL7BhvDTNRmHeg8Vg8AsLanY9NZX4iIKwYAEYk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qxxTmiHONPOj78/zPuHBCKIccipkOE3a0DMkFN87oQMEOKSUuHAKnG7nY3NExzLi9jn5YQwm6ersWKVGIJ1N6avpt722Ysq6Z4Iy032Jlo760sn7RpdtMFPQ7RvM59Jggca+xdic9bsXDGcQHhulq3cKsixi6+fPiazDJHIrC2g= 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=Xx9LuwPF; arc=none smtp.client-ip=209.85.210.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="Xx9LuwPF" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-77f2466eeb5so3163480b3a.2 for ; Sun, 28 Sep 2025 12:07:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086475; x=1759691275; 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=ucXCpHZHap8YpvFOrTnapJK/l5FI9kkW9aB4eroyCmw=; b=Xx9LuwPFaKX0yyMZ+zf91gKq7rJuYCKk5S8f5LV7Q9JP3M2rH5mqYs6s6KpkoUpCnf 0TZJg85adPtuYPGaOt4t8RNx2dqkEyQcJ+vY/yF6PhvOioD6JA4HIPM38RZjN8reFkIf voq+j4fq5jEYk5UlEehUDBl26ThH05NL34cMUCrPtjBagWvyv41HUjHg1r7TIdZJ4xB7 EomL/kRlyQdb5/kFk/dBguhOH/bi7NJONFaryKUWSL60i4NvKYkzxTW54GXJzLzIrj1S Hc4DjUt7WL+TLc40x9x/fbpo+nOdFMJHPkPv6BQeQsY3D9xoNa5IO08quFXmzvMoElIa BbSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086475; x=1759691275; 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=ucXCpHZHap8YpvFOrTnapJK/l5FI9kkW9aB4eroyCmw=; b=HS6uEgGjw2wKjjCDxZThNnSNfEV9REAnISng6ehjQ8zqBqVFLZP0PemZJeFZNlaQDW Cg4JVz4L2I87ypy81ZdQq38XpcNxWIaDJpFpwMQr7tqVb2wW8L3J7oxx7VAlO/tCIsPr os3KZF/+Mm1TWEVJwO3nllrgCLA7ZUe1HBeXCDe01KfGswMXtZTp1TNivVmlHHJxrsqK cpa54wFhoSmj313iKFegfnNMdmKeMYsTVGAmjZnJZL+hM0eLcyB7nCkF93F9Y4TzuELe lwbsvw+sJSH+T9aF/9AWLbufQkmkgMT/CYcUWcpAviL/QrHisAL8UOAgL7dNaX2Ib+4N JqUQ== X-Forwarded-Encrypted: i=1; AJvYcCWeeTq67xpy8oX9iXpovtXKLTS5ESW8CgtTgg1dSJzzm09DT/670hpLpNCuI2BJP2eBlHDT0z/3TxP6eQM=@vger.kernel.org X-Gm-Message-State: AOJu0YzQmdMaNKHPMGz9PqCY1a+IMqv8M/NanWXFiroNNXS3NUptYT1k yXs9z3FGpxhPjghmQrvWEGsN9pjRziaNLCsZ9W54aMTO9DVQygkZIsgWu8kh3PH/Lta1eUBnadQ SxUkvpbBhZsRGnA== X-Google-Smtp-Source: AGHT+IHCtCjCjURftUd42BI8Xn94s+m6l5b6UgXQA8KkQXHy6gXqsx21JjwsoB3Xl8hB9g8pCQfv/F4XvBfXyg== X-Received: from pfbhk3.prod.google.com ([2002:a05:6a00:8783:b0:781:65e:cee5]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:240d:b0:76e:885a:c32c with SMTP id d2e1a72fcca58-780fceda55bmr14907781b3a.26.1759086474819; Sun, 28 Sep 2025 12:07:54 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:17 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-10-skhawaja@google.com> Subject: [RFC PATCH 09/15] iommu/vt-d: Add live update freeze callback 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 , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, zhuyifei@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The iommu_ser needs to be updated during freeze to set the physical address of the iommu_units and devices array as the virtual addresses will not be valid after kexec in the next kernel. Signed-off-by: Samiullah Khawaja --- drivers/iommu/intel/liveupdate.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupd= ate.c index fb214736aa3c..a7d9b07aaada 100644 --- a/drivers/iommu/intel/liveupdate.c +++ b/drivers/iommu/intel/liveupdate.c @@ -258,10 +258,21 @@ static void intel_liveupdate_finish(struct liveupdate= _subsystem *handle, u64 dat pr_warn("Not implemented\n"); } =20 +static int intel_liveupdate_freeze(struct liveupdate_subsystem *handle, u6= 4 *data) +{ + struct iommu_ser *ser =3D __va(*data); + + ser->iommu_units_phys =3D __pa(ser->iommu_units); + ser->devices_phys =3D __pa(ser->devices); + + return 0; +} + static struct liveupdate_subsystem_ops intel_liveupdate_subsystem_ops =3D { .prepare =3D intel_liveupdate_prepare, .finish =3D intel_liveupdate_finish, .cancel =3D intel_liveupdate_cancel, + .freeze =3D intel_liveupdate_freeze, }; =20 static struct liveupdate_subsystem intel_liveupdate_subsystem =3D { --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 2EFA326E17A for ; Sun, 28 Sep 2025 19:07:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086478; cv=none; b=O4OR9yU/CQt8ZTJ5ZOAOdB+mr+rQ4Ar/truwFpy0bNUQTqcXrNDBJ7eMxTps4CMiuaUGl+dYj3cWV2H3PgGvSoxyFCzCFKQszJBYv1SnnqnQiP4sEoKjUrbqtzFKVHrVZqO2BxxQcBofK/uMb5VI1V02vXij1V2o9J6mk3LqIFk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086478; c=relaxed/simple; bh=cUNulnp9gxtOtkG7ryVsF75CYtxVqmeKGRtBY6UQkRE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iO4D8+/h2/a6ngNWmcTsUzQYekuIKLDjUyd2amoGctPcIRWiTTKZ7AsZFKrYrxqggutVLrGtK4k3ApuZ9tfmI080FUSn8BtoJ3NnsncfqN/IKpG39T89ZZXN249ofd3iP24BXquhtukHhsjM/iBQv1+v+bZjIgUwC93wtjtnNMQ= 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=bCb+BYW5; arc=none smtp.client-ip=209.85.210.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="bCb+BYW5" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7811a602576so3398812b3a.0 for ; Sun, 28 Sep 2025 12:07:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086476; x=1759691276; 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=DoORliUGK7lTSGlg4cqnmqfBZFAdVY0fHhO98K40Ca0=; b=bCb+BYW5dM93jC5Kc8VWJr0MyXKxaX+m/AFsTOcPn5dcz58vt5QlN7wLO9CyPxDHm5 MLI7pG0MGf5lNe6HOfjTEQ2ad5NORHB6K74jOWfcAzV0t6X0P2WV3/cpC79FYWaSfef9 A4BDv5TvmWdrnd/HMf4eB0ra+QsLM+Z2DZaWvhxmcK2myaOmfwO88G8mlppE2XkOYI1t gNI/B+QJDlKpe24+oWCSAIvXgpXP4xq+e15xeQpRdU1p66HYXlYDXsHN3lqNmHSPgWlV qONHCyjOwjCL760IewZw3rxmVlfUqWYUzGEJU59DiG8jOgLNZIFvHh+qHcyts40sZaMn u+nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086476; x=1759691276; 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=DoORliUGK7lTSGlg4cqnmqfBZFAdVY0fHhO98K40Ca0=; b=KYbwUZR89NUFnSXaWRBzkYebjcbP+F2OuRrAgDeScgfB/7/OEVAPWNSu6crAjhZbEo 3r9YJCpysiJ0A+Jz16QnsERyWe7hV2IzTVXmZiDD5SgNegJGhsYJJXeBgUjY2iB0N5OL zeD8v2K/a6MkNrK6VUYas+DDemA0wVjOMXv47JApbGePmwryFyKzrvZMh6LphvxGXNsq tZUPus9/jysrUlkAwIKF+j1O3POOzBT9kK+Wea3tYBRMZvj/73FVEi4tjRjq2AuBatCK K7VAPRaEY8TPueODfItazurXPYLB5JtMDQMm6Aqq7kjmRur9JwnuxCfrGr4Of48vRRjR PhPw== X-Forwarded-Encrypted: i=1; AJvYcCXquyJUEgOYzTNXzd5D977gOeiPBaypTbz4gEdbCeEelLY89x6upO1FNeQyeDVyZlZHv9k4e8fokISGTZc=@vger.kernel.org X-Gm-Message-State: AOJu0Yx+LmjSYih1n4bKIrFooYAfgpao1PGNNsyEzCIRmrUXt7OVsOrI JiwJ4UsaSwL8k3kgsmtm9OW15jxyYa4JvgC/ha/OTBoNgHZwnW02jL/NnjhlKhfszqhQEbtu0MP QxE7okdkOjU7lxw== X-Google-Smtp-Source: AGHT+IFLhnnoCDxarwczs9atmxqzu3Xs3uCyHPAxyqgbhg5fCT67/boDgPD+SbYpcL4XGVurMCxpUaxVyD6jzw== X-Received: from pfux37.prod.google.com ([2002:a05:6a00:be5:b0:77f:24f4:40f7]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3906:b0:77f:40ce:9c4 with SMTP id d2e1a72fcca58-780fcf12f65mr17541869b3a.32.1759086476439; Sun, 28 Sep 2025 12:07:56 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:18 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-11-skhawaja@google.com> Subject: [RFC PATCH 10/15] iommu/vt-d: Restore iommu root_table and context on live update 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 , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, zhuyifei@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" During boot if the live update state is updated then the iommu live update state needs to be checked to see if the state of any iommu hardware unit was persisted before live update. If there is preserved state available for an iommu hardware unit then restore the root_table and the iommu context from preserved state. Signed-off-by: Samiullah Khawaja --- drivers/iommu/intel/iommu.c | 7 +++ drivers/iommu/intel/iommu.h | 1 + drivers/iommu/intel/liveupdate.c | 92 +++++++++++++++++++++++++++++++- 3 files changed, 99 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index caac4fd9a51e..245316db3650 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -984,6 +984,13 @@ static int iommu_alloc_root_entry(struct intel_iommu *= iommu) { struct root_entry *root; =20 +#ifdef CONFIG_LIVEUPDATE + if (!intel_iommu_liveupdate_restore_root_table(iommu) && + iommu->root_entry) { + __iommu_flush_cache(iommu, iommu->root_entry, ROOT_SIZE); + return 0; + } +#endif root =3D iommu_alloc_pages_node_sz(iommu->node, GFP_ATOMIC, SZ_4K); if (!root) { pr_err("Allocating root entry for %s failed\n", diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 273d40812d09..6119a638c530 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -1351,6 +1351,7 @@ static inline int iopf_for_domain_replace(struct iomm= u_domain *new, =20 #ifdef CONFIG_LIVEUPDATE int intel_iommu_domain_liveupdate_preserve(struct iommu_domain *domain); +int intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu); #endif =20 #ifdef CONFIG_INTEL_IOMMU_SVM diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupd= ate.c index a7d9b07aaada..755325a5225c 100644 --- a/drivers/iommu/intel/liveupdate.c +++ b/drivers/iommu/intel/liveupdate.c @@ -253,9 +253,11 @@ static void intel_liveupdate_cancel(struct liveupdate_= subsystem *handle, u64 dat pr_warn("Not implemented\n"); } =20 +static struct iommu_ser *serialized_state; + static void intel_liveupdate_finish(struct liveupdate_subsystem *handle, u= 64 data) { - pr_warn("Not implemented\n"); + serialized_state =3D NULL; } =20 static int intel_liveupdate_freeze(struct liveupdate_subsystem *handle, u6= 4 *data) @@ -280,6 +282,94 @@ static struct liveupdate_subsystem intel_liveupdate_su= bsystem =3D { .ops =3D &intel_liveupdate_subsystem_ops, }; =20 +static struct iommu_ser *get_liveupdate_state(void) +{ + struct iommu_ser *ser; + u64 data; + int ret; + + if (serialized_state) + return serialized_state; + + ret =3D liveupdate_get_subsystem_data(&intel_liveupdate_subsystem, &data); + if (WARN_ON_ONCE(ret)) + return NULL; + + if (!kho_restore_folio(data)) + return NULL; + + ser =3D __va(data); + ser->iommu_units =3D __va(ser->iommu_units_phys); + ser->devices =3D __va(ser->devices_phys); + serialized_state =3D ser; + + return ser; +} + +static int restore_iommu_context(struct intel_iommu *iommu) +{ + struct context_entry *context; + int i, ret =3D 0; + + 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))); + } + + return ret; +} + +static struct iommu_unit_ser *get_iommu_unit_state(struct iommu_ser *ser, = u64 reg_phys) +{ + int i; + + for (i =3D 0; i < ser->nr_iommus; ++i) { + if (ser->iommu_units[i].phys_addr =3D=3D reg_phys) + return &ser->iommu_units[i]; + } + + return NULL; +} + +int intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu) +{ + struct iommu_unit_ser *iser; + struct iommu_ser *ser; + int ret; + + if (!liveupdate_state_updated()) + return -EINVAL; + + ser =3D get_liveupdate_state(); + if (!ser) + return -EINVAL; + + iser =3D get_iommu_unit_state(ser, iommu->reg_phys); + if (!iser) + return -EINVAL; + + iommu->root_entry =3D __va(iser->root_table); + + ret =3D restore_iommu_context(iommu); + if (ret) { + WARN_ONCE(ret, "Cannot restore iommu [%llx] root context\n", iommu->reg_= phys); + folio_put(virt_to_folio(iommu->root_entry)); + iommu->root_entry =3D NULL; + } + pr_info("Restored IOMMU[0x%llx] Root Table at: 0x%llx\n", + iommu->reg_phys, iser->root_table); + + return ret; +} + static int __init intel_liveupdate_init(void) { WARN_ON_ONCE(liveupdate_register_subsystem(&intel_liveupdate_subsystem)); --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 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 7064B2701B6 for ; Sun, 28 Sep 2025 19:07:58 +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=1759086480; cv=none; b=d61tez+BshGEeiWQGyQW6DRmLitIDom6jNSapezoGTl8MQKpWXLbpdf8wWbPpESOCycH6wAl1FDsUjd9AYK7zDSz0oNQQ36f2XrBDPi5NWQ8EeOMAxbrFe4sW2us1kHU3dRn5cxx3vml2KjME7qdgCet79pFpLul0znWlqZC29M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086480; c=relaxed/simple; bh=dUFxCLs7p9f1eI8l4b/ZFNt/1ECQtCPnVCtAG5CC0cA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ELnJMUS47mItWaiRZZCfRLiGYwxm95ed+gsibEPwKjioNX4SZ8g58tXRSy36304o6vs49sKmbQL3axH834Fw3TvmOlh1ADnj07UtEht8uZccNygQga68gRH9e1+gjSNuJ8Sxshu5UH90k+v7lfSWnsfgu9PkG67ePrSV9V00o/U= 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=dS12Kdhx; 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="dS12Kdhx" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-286a252bfbfso21150495ad.3 for ; Sun, 28 Sep 2025 12:07:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086478; x=1759691278; 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=oIOurj+2nVVUDk10T5jpgRKE6q2CDkbGgR0n/0hZqjQ=; b=dS12KdhxGofTY5YH1pkQLUoDKjU2tbeb0Ogr0id4+GN1u9k7jMNCgvJz8UVXQ2OVY4 fSQFY7jmeP1s8NH5CMQBKnLGlRdtRksefkeRrkkwqzVeiFuOVYRxOhmsaz0G4WlCjYFI xQsq6f9DrwzQ340PzbVu8GSIfUO3Xx9R8y+9u8Ze2Dd0h7z0XS6i6PQIejLsyQhTtV4F 3pFBzG8oGMA3KRSRDMQjbxUUKjJjcsKpoSM6WYTtqf5yE3bNH5PSNJG+0Ij6EDpEMwKr UIWKxaU9phaxUHU+ERGZ7zpRW8mvSw2qJ/OaYOCxPpL/OnyW5MwyNS/0FaHQRePB6wpw dCqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086478; x=1759691278; 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=oIOurj+2nVVUDk10T5jpgRKE6q2CDkbGgR0n/0hZqjQ=; b=Ot4d9Y1Ck1aeUKSL6zdple0KhuDgg9fu5vYbFLml7IY8kBMdUvnmJFD6c7ihLEngh5 5yTyfI8GwFTpBPKn9zsacGcyAUdQyqiBDdTru5woX/bf6PsB1JnL+dLm0yIX3KDmCOUC jxhFt09dSmwTojwt9+WIdqGrWbIMPRm+C3CD9ph9lDguv59h/G7O3JaIPNbdlGkUBrAb Xoo31Krz4S41s6oh04kBZ1Pk4vac0yjWZrCdYG6mhyAXRKgKINtW8n+2aWmGFFz1DHBo Pmo7QQPxbVQSfUGrCKai6GxXvTwuPeoemEsIUfRI9LY7wEleE4iysL5lq3+f85HD+/G4 scHw== X-Forwarded-Encrypted: i=1; AJvYcCU6qea3AbOFaP7Vlc10qY62S/si2Uq8ZHaIGzDe0UAh2As1cLEQxqfTUEJmIuRvXazrisF3hYAONrdAnGQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxE/Nl7ZAgWWduyU4xjsHRSbiZ1scqIsjti25gNopcmsL12fXnU xmfaaphAVpyberoVm2AmTYd7920VoDeOyr2MJIUauulcFM6IyJ42rfMmPuvX+GTG8/RQbkamA+f MuW1mNAyPkRCNqA== X-Google-Smtp-Source: AGHT+IElf39/DKJfyLea14v/9U1aXLe5jJdoyOqxRTz9WC5PCBwphXq5PQNOB03FmfvrzghM771xyTnwDScHMw== X-Received: from pjbpg12.prod.google.com ([2002:a17:90b:1e0c:b0:32b:50cb:b92f]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:27cc:b0:269:63ea:6d4e with SMTP id d9443c01a7336-27ed4a3de18mr105082665ad.37.1759086477777; Sun, 28 Sep 2025 12:07:57 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:19 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-12-skhawaja@google.com> Subject: [RFC PATCH 11/15] iommufd: Add basic skeleton based on liveupdate_file_handle From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Pasha Tatashin , Jason Gunthorpe , iommu@lists.linux.dev Cc: YiFei Zhu , Samiullah Khawaja , Robin Murphy , Pratyush Yadav , Kevin Tian , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: YiFei Zhu No functionality is implemented in this commit. Just registering and unregistering of the struct liveupdate_file_handle for iommufd. All operations are stubs returning either error or no-op. Signed-off-by: YiFei Zhu Signed-off-by: Samiullah Khawaja --- drivers/iommu/iommufd/Makefile | 1 + drivers/iommu/iommufd/iommufd_private.h | 15 ++++++ drivers/iommu/iommufd/liveupdate.c | 68 +++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 14 ++++- 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/iommufd/liveupdate.c diff --git a/drivers/iommu/iommufd/Makefile b/drivers/iommu/iommufd/Makefile index 71d692c9a8f4..f37830ff7229 100644 --- a/drivers/iommu/iommufd/Makefile +++ b/drivers/iommu/iommufd/Makefile @@ -17,3 +17,4 @@ obj-$(CONFIG_IOMMUFD_DRIVER) +=3D iova_bitmap.o =20 iommufd_driver-y :=3D driver.o obj-$(CONFIG_IOMMUFD_DRIVER_CORE) +=3D iommufd_driver.o +obj-$(CONFIG_LIVEUPDATE) +=3D liveupdate.o diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommuf= d/iommufd_private.h index 0da2a81eedfa..faf48ca9e555 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -702,6 +702,21 @@ iommufd_get_vdevice(struct iommufd_ctx *ictx, u32 id) struct iommufd_vdevice, obj); } =20 +#ifdef CONFIG_LIVEUPDATE +int iommufd_liveupdate_register_lufs(void); +int iommufd_liveupdate_unregister_lufs(void); +#else +static inline int iommufd_liveupdate_register_lufs(void) +{ + return 0; +} + +static inline int iommufd_liveupdate_unregister_lufs(void) +{ + return 0; +} +#endif + #ifdef CONFIG_IOMMUFD_TEST int iommufd_test(struct iommufd_ucmd *ucmd); void iommufd_selftest_destroy(struct iommufd_object *obj); diff --git a/drivers/iommu/iommufd/liveupdate.c b/drivers/iommu/iommufd/liv= eupdate.c new file mode 100644 index 000000000000..6d2a64966335 --- /dev/null +++ b/drivers/iommu/iommufd/liveupdate.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#define pr_fmt(fmt) "iommufd: " fmt + +#include +#include +#include + +#include "iommufd_private.h" + +static int iommufd_liveupdate_prepare(struct liveupdate_file_handler *hand= ler, + struct file *file, u64 *data) +{ + return -EOPNOTSUPP; +} + +static int iommufd_liveupdate_freeze(struct liveupdate_file_handler *handl= er, + struct file *file, u64 *data) +{ + /* No-Op; everything should be made read-only */ + return 0; +} + +static void iommufd_liveupdate_cancel(struct liveupdate_file_handler *hand= ler, + struct file *file, u64 data) +{ +} + +static void iommufd_liveupdate_finish(struct liveupdate_file_handler *hand= ler, + struct file *file, u64 data, bool reclaimed) +{ +} + +static int iommufd_liveupdate_retrieve(struct liveupdate_file_handler *han= dler, + u64 data, struct file **file_p) +{ + return -EOPNOTSUPP; +} + +static bool iommufd_liveupdate_can_preserve(struct liveupdate_file_handler= *handler, + struct file *file) +{ + return false; +} + +static struct liveupdate_file_ops iommufd_lu_file_ops =3D { + .prepare =3D iommufd_liveupdate_prepare, + .freeze =3D iommufd_liveupdate_freeze, + .cancel =3D iommufd_liveupdate_cancel, + .finish =3D iommufd_liveupdate_finish, + .retrieve =3D iommufd_liveupdate_retrieve, + .can_preserve =3D iommufd_liveupdate_can_preserve, +}; + +static struct liveupdate_file_handler iommufd_lu_handler =3D { + .compatible =3D "iommufd-v1", + .ops =3D &iommufd_lu_file_ops, +}; + +int iommufd_liveupdate_register_lufs(void) +{ + return liveupdate_register_file_handler(&iommufd_lu_handler); +} + +int iommufd_liveupdate_unregister_lufs(void) +{ + return liveupdate_unregister_file_handler(&iommufd_lu_handler); +} diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 15af7ced0501..b3bf65bc8da4 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -723,11 +723,21 @@ static int __init iommufd_init(void) if (ret) goto err_misc; } + + if (IS_ENABLED(CONFIG_LIVEUPDATE)) { + ret =3D iommufd_liveupdate_register_lufs(); + if (ret) + goto err_vfio_misc; + } + ret =3D iommufd_test_init(); if (ret) - goto err_vfio_misc; + goto err_lufs; return 0; =20 +err_lufs: + if (IS_ENABLED(CONFIG_LIVEUPDATE)) + iommufd_liveupdate_unregister_lufs(); err_vfio_misc: if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)) misc_deregister(&vfio_misc_dev); @@ -739,6 +749,8 @@ static int __init iommufd_init(void) static void __exit iommufd_exit(void) { iommufd_test_exit(); + if (IS_ENABLED(CONFIG_LIVEUPDATE)) + iommufd_liveupdate_unregister_lufs(); if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)) misc_deregister(&vfio_misc_dev); misc_deregister(&iommu_misc_dev); --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 8BAC2257858 for ; Sun, 28 Sep 2025 19:07:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086481; cv=none; b=Fyr4JDPYbgpU1DybnZqKtEMYkxJa68mpIfgRvGK797ulZSGbkgSe83LvWxZG/ej2lLMd/IhT/DDOLxZ5kaEoZ/wfSAKNpavYuUXnvDkgUWGl59YBVl7mWfnoXLmeJ9zlhPfXgBZf7fQJcCf9GzN6aPKPGYsvSTzxSZ4jatg7rNM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086481; c=relaxed/simple; bh=Hz08krsvo2uu8r8XXqocMeGrlRetPXduZzadaTl1c1g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=F2up0pKoUb/a5KdJziLywhXQSfcadK7g64F8G1ilcq4wyqye81jQKF6Tz5wphB4LZcdqzHXXY1Oy2GctS+TkrOiVzn9eD5c0rDEOBSTl/Rx855HFNUJ8yp18L305xGwEAeUnFXt63elGoBy9gfFqOvgnzEmtkUIS0obKxsNl9FU= 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=DOtNlOrY; arc=none smtp.client-ip=209.85.210.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="DOtNlOrY" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7810289cd5eso5458519b3a.1 for ; Sun, 28 Sep 2025 12:07:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086479; x=1759691279; 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=wMP1AXZR5K2DaLRM+QM+TDjG2L4fBux6TVzeNYZkXlc=; b=DOtNlOrY4PVX8Mc3NrL+O8hAPU9HugQruCvzdMxRWOsTb1LeHuBCQECMyetdvQhgp2 jNCNYQVsKp3UxH9cgUPorWTbFnUosO2fqqt/T8KPWb8GXwyigsaj8qcV3EFz6ojpS6gw 1ugd7reuzeafLLfRFzt982d3GpbFKZJLrjn/8IdanYdpyqmHL6/3ipA3vOt7U7uxJfOM X+EjBo/fUebgZUeYco5ZIYF5BceVtv5D1f3JmFiDtCTipicvdhnLMtcqMj8rAHe233NC zyUgWFfIdAUjdMFkU+jE9ZOOG2O88hfTUa7rSi5Fdw49K6oVP/Va/7ZrvcjXr6oqybN8 sf2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086479; x=1759691279; 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=wMP1AXZR5K2DaLRM+QM+TDjG2L4fBux6TVzeNYZkXlc=; b=Dp2u87K0mzsP0jyo52qCdf6upKkMVnwYOwPY9+6vs2flajnBOVz7oAyPZjy800AbL5 LmoNjcZdBIWP4r7p5eGO7kl/JEo4QiZoqX9qUq6a0ewMkunfmcmcaMukcU90shcYUfOn X2QJNLu+znOW5JTFh8+3SE7umuyIuFPNwHol8me9RfzgNurUOfvpykv4fUeQd08pn53W HS/7JyjcUj8WLkMzjBK8Ljc1RVF2KvW87uaRC5r4eyAQdKC1Bwer/k6fmVSeyiUTcUc7 kdalCbtx8mpNwTkfTBEjQHHzHoTewquTGgxNOb58BivvXFQkCBwHDYFVq5yaOuL8Ldls xmXg== X-Forwarded-Encrypted: i=1; AJvYcCX7Dn0Rb9OJHGiLv2Sf5uNAr+BKVnqJBlDXjDAzhx9rjfcrLQKUkfFwpd7iK2lbMF+AvAHDIBrtIrNaMNQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzDw/13iEfsVaRL7u8f7ainyVeW5+Wymsi74X/4ALO8g5QOSbp3 FuCVEC8QGNjmKONtOCedAyHcjLqTILlcViTSxgI8fEdgcVMICOGoRocXq6s9TB9AC1d/xTC1xJ3 BwMPzGyejAs4PZQ== X-Google-Smtp-Source: AGHT+IERI+oLKd9q7ro7oO/YEhndfr7WxtS0ghCxPXAfP/xH9RrAUds1QILDN/YJJkQWFDM4kRukWJPJlkXBxQ== X-Received: from pfbct10.prod.google.com ([2002:a05:6a00:f8a:b0:77f:6432:dc09]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:189a:b0:776:20c2:d58b with SMTP id d2e1a72fcca58-780fced5d67mr17812026b3a.24.1759086478987; Sun, 28 Sep 2025 12:07:58 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:20 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-13-skhawaja@google.com> Subject: [RFC PATCH 12/15] iommufd-luo: Implement basic prepare/cancel/finish/retrieve using folios From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Pasha Tatashin , Jason Gunthorpe , iommu@lists.linux.dev Cc: YiFei Zhu , Samiullah Khawaja , Robin Murphy , Pratyush Yadav , Kevin Tian , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: YiFei Zhu The actual serialization and de-serialization is implemented in follow up commits. - On prepare, a single folio is created and preserved to store all the structs. - On cancel, the folio is unpreserved and freed. - On retrieve, the folio is restored, then an fd with anon_inode is created, with data pointing to the folio. - On finish, the folio is freed. Signed-off-by: YiFei Zhu Signed-off-by: Samiullah Khawaja --- drivers/iommu/iommufd/iommufd_private.h | 12 +++ drivers/iommu/iommufd/liveupdate.c | 127 +++++++++++++++++++++++- drivers/iommu/iommufd/main.c | 2 +- 3 files changed, 137 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommuf= d/iommufd_private.h index faf48ca9e555..dfa17bfc9933 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -19,6 +19,9 @@ struct iommu_domain; struct iommu_group; struct iommu_option; struct iommufd_device; +struct iommufd_lu; + +extern const struct file_operations iommufd_fops; =20 struct iommufd_sw_msi_map { struct list_head sw_msi_item; @@ -55,6 +58,10 @@ struct iommufd_ctx { /* Compatibility with VFIO no iommu */ u8 no_iommu_mode; struct iommufd_ioas *vfio_ioas; + +#ifdef CONFIG_LIVEUPDATE + struct iommufd_lu *lu; +#endif }; =20 /* Entry for iommufd_ctx::mt_mmap */ @@ -703,6 +710,11 @@ iommufd_get_vdevice(struct iommufd_ctx *ictx, u32 id) } =20 #ifdef CONFIG_LIVEUPDATE +struct iommufd_lu { + /* Only valid in restore, for lifetime purposes */ + struct folio *folio_lu; +}; + int iommufd_liveupdate_register_lufs(void); int iommufd_liveupdate_unregister_lufs(void); #else diff --git a/drivers/iommu/iommufd/liveupdate.c b/drivers/iommu/iommufd/liv= eupdate.c index 6d2a64966335..1bdd5a82af90 100644 --- a/drivers/iommu/iommufd/liveupdate.c +++ b/drivers/iommu/iommufd/liveupdate.c @@ -2,16 +2,52 @@ =20 #define pr_fmt(fmt) "iommufd: " fmt =20 +#include #include #include +#include #include +#include =20 #include "iommufd_private.h" =20 static int iommufd_liveupdate_prepare(struct liveupdate_file_handler *hand= ler, struct file *file, u64 *data) { - return -EOPNOTSUPP; + struct iommufd_ctx *ictx =3D iommufd_ctx_from_file(file); + struct iommufd_lu *iommufd_lu; + struct folio *folio_lu; + size_t serial_size; + int rc; + + if (IS_ERR(ictx)) + return PTR_ERR(ictx); + + serial_size =3D sizeof(*iommufd_lu); + + folio_lu =3D folio_alloc(GFP_KERNEL, get_order(serial_size)); + if (!folio_lu) { + rc =3D -ENOMEM; + goto err_ctx_put; + } + + iommufd_lu =3D folio_address(folio_lu); + + rc =3D kho_preserve_folio(folio_lu); + if (rc) + goto err_folio_put; + + *data =3D virt_to_phys(iommufd_lu); + + iommufd_ctx_put(ictx); + return 0; + +err_folio_put: + folio_put(folio_lu); + +err_ctx_put: + iommufd_ctx_put(ictx); + return rc; } =20 static int iommufd_liveupdate_freeze(struct liveupdate_file_handler *handl= er, @@ -24,23 +60,108 @@ static int iommufd_liveupdate_freeze(struct liveupdate= _file_handler *handler, static void iommufd_liveupdate_cancel(struct liveupdate_file_handler *hand= ler, struct file *file, u64 data) { + struct iommufd_ctx *ictx =3D iommufd_ctx_from_file(file); + struct folio *folio_lu; + + if (WARN_ON(IS_ERR(ictx))) + return; + + folio_lu =3D pfn_folio(PHYS_PFN(data)); + WARN_ON(kho_unpreserve_folio(folio_lu)); + folio_put(folio_lu); + + iommufd_ctx_put(ictx); } =20 static void iommufd_liveupdate_finish(struct liveupdate_file_handler *hand= ler, struct file *file, u64 data, bool reclaimed) { + struct iommufd_lu *iommufd_lu; + struct iommufd_ctx *ictx; + struct folio *folio_lu; + + if (!reclaimed || !file) { + pr_warn("%s: fd not reclaimed\n", __func__); + + folio_lu =3D kho_restore_folio(data); + if (WARN_ON_ONCE(IS_ERR_OR_NULL(folio_lu))) + return; + + iommufd_lu =3D folio_address(folio_lu); + } else { + ictx =3D iommufd_ctx_from_file(file); + iommufd_lu =3D ictx->lu; + ictx->lu =3D NULL; + iommufd_ctx_put(ictx); + } + + folio_put(iommufd_lu->folio_lu); } =20 static int iommufd_liveupdate_retrieve(struct liveupdate_file_handler *han= dler, u64 data, struct file **file_p) { - return -EOPNOTSUPP; + struct iommufd_lu *iommufd_lu; + struct iommufd_ctx *ictx; + struct folio *folio_lu; + struct file *file; + int rc; + + folio_lu =3D kho_restore_folio(data); + if (IS_ERR_OR_NULL(folio_lu)) + return -EFAULT; + + iommufd_lu =3D folio_address(folio_lu); + iommufd_lu->folio_lu =3D folio_lu; + + file =3D anon_inode_create_getfile("iommufd", &iommufd_fops, + NULL, O_RDWR, NULL); + if (IS_ERR(file)) { + rc =3D PTR_ERR(file); + goto err_folio_put; + } + + rc =3D iommufd_fops.open(file->f_inode, file); + if (rc) + goto err_fput; + + ictx =3D iommufd_ctx_from_file(file); + if (WARN_ON(IS_ERR(ictx))) { + rc =3D PTR_ERR(ictx); + goto err_fput; + } + + if (WARN_ON(ictx->lu)) { + rc =3D -EEXIST; + goto err_ctx_put; + } + ictx->lu =3D iommufd_lu; + + iommufd_ctx_put(ictx); + + *file_p =3D file; + + return 0; + +err_ctx_put: + iommufd_ctx_put(ictx); +err_fput: + fput(file); +err_folio_put: + folio_put(folio_lu); + return rc; } =20 static bool iommufd_liveupdate_can_preserve(struct liveupdate_file_handler= *handler, struct file *file) { - return false; + struct iommufd_ctx *ictx =3D iommufd_ctx_from_file(file); + + if (IS_ERR(ictx)) + return false; + + iommufd_ctx_put(ictx); + return true; } =20 static struct liveupdate_file_ops iommufd_lu_file_ops =3D { diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index b3bf65bc8da4..a8b6daaca11f 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -577,7 +577,7 @@ static int iommufd_fops_mmap(struct file *filp, struct = vm_area_struct *vma) return rc; } =20 -static const struct file_operations iommufd_fops =3D { +const struct file_operations iommufd_fops =3D { .owner =3D THIS_MODULE, .open =3D iommufd_fops_open, .release =3D iommufd_fops_release, --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 4F6D92737F9 for ; Sun, 28 Sep 2025 19:08:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086482; cv=none; b=nduTLFwVTlFXukAtdgR0oOjFWUHvTwGxCmSmMabp1N4LyBZIN5GFXCkzPayOasodf+1QYTXd6bUfCmb/yupIbT6PtNhxTyaK0way4gHELyh6AxzMwJuxWxBwg19wuVNBwry6/7T7/Te3d9C/uFp5hxT3l7yBJ73gDkiIWLh5xXo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086482; c=relaxed/simple; bh=eE4YtWNaCXed6BZ7yxa8qvhRb0ZMJlu8yifPEfR7bgo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nNAQPv+tNz8y9m+uG+PLANcGnRoIIOkAiPrg/IBvGG9Ki8JQV7y2q5fXw0ISyXVPCL59U2RiwTt/K/SzyJaqtrKvxuxIp6Zp6dUezkkWPCjqkct5fV05EzW5g5BeZVLzMKk3sQFlVgk4fWO3qi/tqymmgWg7xcFOvX5zvTmXNws= 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=UKX4EYyd; arc=none smtp.client-ip=209.85.215.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="UKX4EYyd" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b5509ed1854so2521036a12.0 for ; Sun, 28 Sep 2025 12:08:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086480; x=1759691280; 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=Pi5xSv0ip63020Fps6lPWkegEnm0TGf/4Ss482K5u6o=; b=UKX4EYyd9VPPT9Kt8iDe2VvQFcLlWY8yHS9y2ZWX7mc9iFYikmS2XqXe+XxBAAcvjN z/nm8B+tau5vnJO2ccZ33+7O/JzFw8kiCORKfHJmVXMAQNeSHI8iaZE6yIZINhVTpJxS 6wimAFRcBX4FHGH1Lf7GBBfyA9Q67CrUSVXUY/DaEYrPCFb9b+ympfzfuLnY/gaNkuY9 Xx5/SyJvUj3HApMoNqn4rypZQ64akkBg+GkynMnPFrdkdPjgeQnG/U8L/pOlUH4uZwaH qTolVWRVaydrY8DSEE3PmY+ntOxptqhhrsUd0kUa3U6o53pINb+5SsVvsnu97Hw3q24r 5alw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086480; x=1759691280; 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=Pi5xSv0ip63020Fps6lPWkegEnm0TGf/4Ss482K5u6o=; b=OLBsZpsH1fvTNwDxTV4DWmTpXG7u3hIqW4TmoHX5lNeAtLQbNn+2VEa7NqchY2M1WC pfQKGYfe1+n4FDoWwxzoYPwNPrRHC2NZCaPhWzpJUgiDonkqRt3e/QMqf13SuAL0szN+ 6B5fF+GeEUQ+9Kiz1/JfOj/Jwyl9MST+/Z58a2WM5QSydj5rE24g9ZkeK7LmL6vjvK3K FVdB1sT2RFJ57yhmYPrkzBkfInVKJgct4HFcilfppPPK8/bLZ8uM/diCaX8owzU0EtTu NLPivg6wiRZSnJn2AP1JI2b5V0tuXrjYH7Vi9fP4E46JnuZuwspe13LVKxdSnN3sDAGe 3wsg== X-Forwarded-Encrypted: i=1; AJvYcCW2EAcpHkoQJ+4ioPcmsfN3SGHFM+5/gjEAXjTW0f8zZZe+KtHWM6BWEeSohr1CaKfBw/Q6N6rhEmexTho=@vger.kernel.org X-Gm-Message-State: AOJu0YzUBOypGhEi8F4XCSWx+Dl0WxgMK3L7N0BzvQFIghrBO9nWp7dZ UTYRJWSdYB4rt4Rcxx8i0YbakoeFdRi/j9DrysGgsE8pBzZGSd4jSRwiWtvHERm0Qk4B9OPNyYt 4H+KdzoI+6xmvUQ== X-Google-Smtp-Source: AGHT+IG0pDvpM9G54/3TjfbvcFpmO/TqiGthSy0imZbx93Glz7wAQ23TYddREy6lbjR0p5lw+B7srvLFeianPg== X-Received: from pjjk3.prod.google.com ([2002:a17:90a:6583:b0:32d:a0b1:2b14]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4d10:b0:330:6d5e:f178 with SMTP id 98e67ed59e1d1-3342a30c47amr3373951a91.26.1759086480539; Sun, 28 Sep 2025 12:08:00 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:21 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-14-skhawaja@google.com> Subject: [RFC PATCH 13/15] iommufd: Persist iommu domains for live update From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Pasha Tatashin , Jason Gunthorpe , iommu@lists.linux.dev Cc: YiFei Zhu , Samiullah Khawaja , Robin Murphy , Pratyush Yadav , Kevin Tian , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: YiFei Zhu Iterate through all the IOAS objects and the underlying hwpt_paging objects. Persist each iommu domain using API iommu_domain_preserve. This is temporary as only the domains attached to the persisted devices need to preserved. Signed-off-by: YiFei Zhu Signed-off-by: Samiullah Khawaja --- drivers/iommu/iommufd/liveupdate.c | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/drivers/iommu/iommufd/liveupdate.c b/drivers/iommu/iommufd/liv= eupdate.c index 1bdd5a82af90..0af0c6fadff1 100644 --- a/drivers/iommu/iommufd/liveupdate.c +++ b/drivers/iommu/iommufd/liveupdate.c @@ -8,9 +8,52 @@ #include #include #include +#include =20 #include "iommufd_private.h" =20 +static int iommufd_save_ioas(struct iommufd_ctx *ictx, + struct iommufd_lu *iommufd_lu) +{ + struct iommufd_hwpt_paging *hwpt_paging; + struct iommufd_ioas *ioas =3D NULL; + struct iommufd_object *obj; + unsigned long index; + int rc; + + /* Iterate each ioas. */ + xa_for_each(&ictx->objects, index, obj) { + if (obj->type !=3D IOMMUFD_OBJ_IOAS) + continue; + + ioas =3D (struct iommufd_ioas *)obj; + mutex_lock(&ioas->mutex); + + /* + * TODO: Iterate over each device of this iommufd and only save + * hwpt/domain if the device is persisted. + */ + list_for_each_entry(hwpt_paging, &ioas->hwpt_list, hwpt_item) { + if (!hwpt_paging->common.domain) + continue; + + rc =3D iommu_domain_preserve(hwpt_paging->common.domain); + if (rc) + goto err; + } + + mutex_unlock(&ioas->mutex); + ioas =3D NULL; + } + + return 0; + +err: + if (ioas) + mutex_unlock(&ioas->mutex); + return rc; +} + static int iommufd_liveupdate_prepare(struct liveupdate_file_handler *hand= ler, struct file *file, u64 *data) { @@ -33,6 +76,10 @@ static int iommufd_liveupdate_prepare(struct liveupdate_= file_handler *handler, =20 iommufd_lu =3D folio_address(folio_lu); =20 + rc =3D iommufd_save_ioas(ictx, iommufd_lu); + if (rc) + goto err_folio_put; + rc =3D kho_preserve_folio(folio_lu); if (rc) goto err_folio_put; --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 C10662749ED for ; Sun, 28 Sep 2025 19:08:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086484; cv=none; b=hXoer4q/glTq0bnHOPL44npZvLETnREkyFVQH1zNyBNHquqSUT/Vy95fCyJFTmwhUXJRWUvmLphLx0kigp7TIf50qElvGKmzbL76vPZ8QPtnxX3bVoZUt2vxbmISC5o21xmeHglHNyarMNSx81twPp6BQXHmgs3/KSp4G+QDA/I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086484; c=relaxed/simple; bh=iu8bF1+1izo/N9nO598vTqm7TLEKTMTdaWt9KfjORTs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DQwdZ2rsQDBq/YpnT/CKH8tWg/rzwOk00J6itxif2uMkOPkhwdDQm04LLRUB+pe6B1bsPjCYrsqqdJpc5KlX4EgGhietC4/CJCQLKT7vgHGmTeIPDWoCbEzJZnc1VyCpDbQNTUnNyEOzag07TP2QEO1w1jhhW/dRASJiXA7tSY4= 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=HprrTUwg; arc=none smtp.client-ip=209.85.210.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="HprrTUwg" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7810912fc31so3027687b3a.3 for ; Sun, 28 Sep 2025 12:08:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086482; x=1759691282; 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=r6rIkXRuvh9+tWEFzDbEPwiqaPEAKampFFEauxRboPc=; b=HprrTUwgn0RuYRn7h2IPOAqgFlbeMq4vzxXmBONe1ESEeNmdtfqtdZAc7kgFz/7rcW XWstQrxoyMQ9jgt+SwQ5kDcFl0t8cvQu5n03N4zuZmEeFkNQ5u9NEVLMFFWMcyCf5/JG BaU0T8b35xKOx64vcboD6oHKa77eKabkTA3sp4wdoqC7iEzCiAQeKW12fV6EtXVDYtzz zVupgYRXAiHFr0opipvsGZOXE2/Hz+VHThkwR+u9+PTft2JbDORB0cQ6J6s2KkN63oQQ F8XsNeT7g0g8n2vm3aGmYkQae+GyfQ+MzSaPFXYFlUTNsCfZk9FTGioJ64XYoWudxhRP otiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086482; x=1759691282; 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=r6rIkXRuvh9+tWEFzDbEPwiqaPEAKampFFEauxRboPc=; b=HWf0ezCaltjqbc3KDwl4Qu/mH8Ly5Df/7VgbSmYNYg1WIwv/6gnJSskT1IvIB3Q0+d mBdveLzbS2kcOW9toaYMdehavZcgpyuN90f5MeHSsm4s+/KkB7PzvwM7NesgteVzvQSR IGBPFzYMCkfpuntcj3K58YFpggL5KNgSU0vO2535+Ivyea+Y4KXWJudyGUoWwSmn+fT+ 4rYCCq3xxM3t7iEYIWyyxEs/G1KmGBpSBu4kR0PLWeDoud6mN4GUlg6pkbUBfYnLv8lN Yx4Ylg/SQVkK4fBzkKeTEFcOiHJfzm0Sia91EXab8zQaVAturHRAyUMGlbDKWYAT1auZ PR7g== X-Forwarded-Encrypted: i=1; AJvYcCUd3rKUb5+qnS60uGS4foujKpCtbg1EBbClY5l+4UW0ysSqQ1CkZ0c2EFRFsHDFMcQ8gY8xzz/mVHepwvc=@vger.kernel.org X-Gm-Message-State: AOJu0YxS5nhjRY94e9pGKsMIU8J6/mc/pMoFKGXrYPCTsaeuPC2QIIKr FJyhzyAJ2vGWd9EkZEcorCihM1HyuKmM0KxiUT7/deKFxvl+pbuiDDJHv2spub/HwG1ITX1hwdS Q/J/AgjMB9BRpkQ== X-Google-Smtp-Source: AGHT+IG3DYQZH5AWOVC5jjKnDvm3R5bOqt3Kna9JyjjCIT0P03vRmnYOpx62s2gMEvWQZHaWnGO1XY6oqMnV7Q== X-Received: from pfbhm9.prod.google.com ([2002:a05:6a00:6709:b0:77f:456b:23da]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:178f:b0:781:1e0d:a181 with SMTP id d2e1a72fcca58-7811e0da5bfmr8289263b3a.14.1759086482049; Sun, 28 Sep 2025 12:08:02 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:22 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-15-skhawaja@google.com> Subject: [RFC PATCH 14/15] iommu/vt-d: sanitize restored root table and iommu contexts 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 , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, zhuyifei@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The persisted root table will contain context entries from the previous kernel. Sanitize the root table entries by setting them all to zero. This is temporary, the context entries for the persisted devices need to be kept intact. Signed-off-by: Samiullah Khawaja --- drivers/iommu/intel/liveupdate.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupd= ate.c index 755325a5225c..3783632cf634 100644 --- a/drivers/iommu/intel/liveupdate.c +++ b/drivers/iommu/intel/liveupdate.c @@ -306,6 +306,26 @@ static struct iommu_ser *get_liveupdate_state(void) return ser; } =20 +static void sanitize_iommu_context(struct intel_iommu *iommu) +{ + struct context_entry *context; + int i; + + /* TODO: Keep the context entries for the preserved devices. */ + for (i =3D 0; i < ROOT_ENTRY_NR; i++) { + context =3D iommu_context_addr(iommu, i, 0, 0); + if (context) + memset(context, 0, PAGE_SIZE); + + if (!sm_supported(iommu)) + continue; + + context =3D iommu_context_addr(iommu, i, 0x80, 0); + if (context) + memset(context, 0, PAGE_SIZE); + } +} + static int restore_iommu_context(struct intel_iommu *iommu) { struct context_entry *context; @@ -324,6 +344,8 @@ static int restore_iommu_context(struct intel_iommu *io= mmu) BUG_ON(!kho_restore_folio(virt_to_phys(context))); } =20 + sanitize_iommu_context(iommu); + return ret; } =20 --=20 2.51.0.536.g15c5d4f767-goog From nobody Wed Oct 1 21:24:19 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 7353A275AF4 for ; Sun, 28 Sep 2025 19:08:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086486; cv=none; b=DDmxqsD39HRATqNNVPK8rysPDtlL/oj2TT7Bd9x6p3ijRnwp9PllUXr9BAO93qgl/sHygHkc0VeLOw7lNKw+JkN5lIyAEaPPEkTuPvmzrHfp9iBidhAqThBZAzSZL/WirODKDRttvKC11FgySivwx7gEBqIGZYu21Yrm83uzBWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759086486; c=relaxed/simple; bh=bp7Q5MfQ7BUhrxdif0RdrYgnTPagGMPXrfDPEix32cA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VI8IZKT4J+FMcvpNRpnla53f8tgb5CBiVXdpClJjWi62Ps2p7HTAi/jTuzjrVybFinILpvSiBuQZSUbdpQBe4uM8mH34+ftxLW7WIEhGDKI0yW6cEGJX9rXd7pGrxuFdzqGHXWjjNeUxZsoaOuvez6b4JdMbF+wwFhuLq92Gb+k= 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=lwB8M8US; arc=none smtp.client-ip=209.85.210.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="lwB8M8US" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-77b73bddbdcso3232511b3a.1 for ; Sun, 28 Sep 2025 12:08:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1759086484; x=1759691284; 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=RJxUEkhR4xzbVVm1hzIqWlYBXGWxXyVuvzNZ0+K6GBc=; b=lwB8M8USC4MsZpOV6rXGPf99LmzaBDPL4k7rhBcrCTsUGgWW9xNQ2BpB6Rc2npS5vr lV+wLEiSQszkb/isLVMPea1ycNz7TDSPteRGdDTpQUPIBr5K06xRBlaegmneLqSD/DHr KoqlNCljMeykVKk1heJ3jWjy34kGyRw6AQmLffCCNQmLioiwRFFSXojTA0DL4mU/OCL8 HbawFx/MhOO3GH+1lAb2m+yDY5apdruMCGukAVHwb8+bkHg5bwPbtPQJpU0tD8qOAO6d ii80AmOaejskNnyRIb4dAiWOOCnSeILmQJB+hlFYVfUo82Ke5T2wrUTl3GGMtT/kn+U/ qdZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759086484; x=1759691284; 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=RJxUEkhR4xzbVVm1hzIqWlYBXGWxXyVuvzNZ0+K6GBc=; b=sXIYgnObW4KY9nrj2s1nrhUOwT5jMu9Vd8nHI0HVmxxN0nmaEJz3cnD8uuH0D9jr+0 l9rQDEtG5ylNK2wq2uoqqEUjfA8U1RiLOYEwxrnIBSf8lqgt2s71me5u6+lK5/6K59gU tMNhNl6uHrL4x+RXzk+gLENzs9TDMJkm6RzBq1xO8jY7Pdc5f+mkbVYZ7Woqyys6IqeV MQFxpDuIkcwVr56yBrmtU3gERUnlkoIjyUBnAtErNdOGQvDp0jlJmw7ytZ7c+t4Wa+1e U+m4mWhWxi50q5U7MlwdXLrf3+yf8rvLbVYhK52A2XiHSyGPSmusgi/aRldAz2OE3Jpr 083g== X-Forwarded-Encrypted: i=1; AJvYcCWy92ulQeK/BC8aRdW7FFDKn77d0MPLfuxF/JqG3+ETlySSHjnA057zFgz9c0ipxKpWkEZnAgskvMwlP9Q=@vger.kernel.org X-Gm-Message-State: AOJu0Ywrtl0NR2mhs4+BuOvR0r7h3qq1XJ0TzXtOCwaWuGGnHtb53oZX RF8WOWEPc7rxEvj0YUQO/iw4BqSRrqwC7s0ZGUaX/lM+LHT1E2qS9j4HEufv6hvyYXlYgkxHdXl WXcM71sDe4bxLVQ== X-Google-Smtp-Source: AGHT+IFMTxLoSANT4uRqLmmBpB6B54qHvJVSAjDeqXG+t6yE8ph3la8TEAGcK8rMwkormXLy9e2ya2SG2UiFcQ== X-Received: from pfog16.prod.google.com ([2002:aa7:8750:0:b0:77f:1f29:5399]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2395:b0:772:397b:b270 with SMTP id d2e1a72fcca58-78100fcf632mr12637028b3a.10.1759086483707; Sun, 28 Sep 2025 12:08:03 -0700 (PDT) Date: Sun, 28 Sep 2025 19:06:23 +0000 In-Reply-To: <20250928190624.3735830-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: <20250928190624.3735830-1-skhawaja@google.com> X-Mailer: git-send-email 2.51.0.536.g15c5d4f767-goog Message-ID: <20250928190624.3735830-16-skhawaja@google.com> Subject: [RFC PATCH 15/15] iommufd/selftest: Add test to verify iommufd preservation 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 , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, zhuyifei@google.com, Chris Li , praan@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Test iommufd preservation by setting up an iommufd and preserve it across live update. Test takes VFIO cdev path of a device bound to vfio-pci driver and binds it to an iommufd being preserved. Note that the helper functions setup_cdev, open_iommufd, and setup_iommufd will be replaced with VFIO selftest library. Similarly the helper function defined to open and interface with Live Update Orchestrator device will be replaced with a common helper library. Signed-off-by: Samiullah Khawaja --- tools/testing/selftests/iommu/Makefile | 1 + .../selftests/iommu/iommufd_liveupdate.c | 196 ++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 tools/testing/selftests/iommu/iommufd_liveupdate.c diff --git a/tools/testing/selftests/iommu/Makefile b/tools/testing/selftes= ts/iommu/Makefile index 84abeb2f0949..42c962c5e612 100644 --- a/tools/testing/selftests/iommu/Makefile +++ b/tools/testing/selftests/iommu/Makefile @@ -6,5 +6,6 @@ LDLIBS +=3D -lcap TEST_GEN_PROGS :=3D TEST_GEN_PROGS +=3D iommufd TEST_GEN_PROGS +=3D iommufd_fail_nth +TEST_GEN_PROGS +=3D iommufd_liveupdate =20 include ../lib.mk diff --git a/tools/testing/selftests/iommu/iommufd_liveupdate.c b/tools/tes= ting/selftests/iommu/iommufd_liveupdate.c new file mode 100644 index 000000000000..1003d0cf2cae --- /dev/null +++ b/tools/testing/selftests/iommu/iommufd_liveupdate.c @@ -0,0 +1,196 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Copyright (c) 2025, Google LLC. + * Samiullah Khawaja + */ + +#include +#include +#include +#include + +#define __EXPORTED_HEADERS__ +#include +#include +#include +#include + +#include "../kselftest.h" + +#define ksft_assert(condition) \ + do { if (!(condition)) \ + ksft_exit_fail_msg("Failed: %s at %s %d\n", \ + #condition, __FILE__, __LINE__); } while (0) + +int setup_cdev(const char *vfio_cdev_path) +{ + int cdev_fd; + + cdev_fd =3D open(vfio_cdev_path, O_RDWR); + if (cdev_fd < 0) + ksft_exit_skip("Failed to open VFIO cdev: %s\n", vfio_cdev_path); + + return cdev_fd; +} + +int open_iommufd(void) +{ + int iommufd; + + iommufd =3D open("/dev/iommu", O_RDWR); + if (iommufd < 0) + ksft_exit_skip("Failed to open /dev/iommu. IOMMUFD support not enabled.\= n"); + + return iommufd; +} + +int setup_iommufd(int iommufd, int cdev_fd) +{ + int ret; + + struct vfio_device_bind_iommufd bind =3D { + .argsz =3D sizeof(bind), + .flags =3D 0, + }; + struct iommu_ioas_alloc alloc_data =3D { + .size =3D sizeof(alloc_data), + .flags =3D 0, + }; + struct vfio_device_attach_iommufd_pt attach_data =3D { + .argsz =3D sizeof(attach_data), + .flags =3D 0, + }; + + bind.iommufd =3D iommufd; + ret =3D ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind); + ksft_assert(!ret); + + ret =3D ioctl(iommufd, IOMMU_IOAS_ALLOC, &alloc_data); + ksft_assert(!ret); + + attach_data.pt_id =3D alloc_data.out_ioas_id; + ret =3D ioctl(cdev_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data); + ksft_assert(!ret); + + return ret; +} + +int open_liveupdate_orchestrator(void) +{ + int luo; + + luo =3D open("/dev/liveupdate", O_RDWR); + ksft_assert(luo > 0); + + return luo; +} + +__u32 liveupdate_get_state(int luo) +{ + struct liveupdate_ioctl_get_state state; + int ret; + + state.size =3D sizeof(state); + ret =3D ioctl(luo, LIVEUPDATE_IOCTL_GET_STATE, &state); + ksft_assert(!ret); + + return state.state; +} + +bool liveupdate_state_normal(int luo) +{ + return liveupdate_get_state(luo) =3D=3D LIVEUPDATE_STATE_NORMAL; +} + +bool liveupdate_state_updated(int luo) +{ + return liveupdate_get_state(luo) =3D=3D LIVEUPDATE_STATE_UPDATED; +} + +int liveupdate_set_event(int luo, enum liveupdate_event ev) +{ + struct liveupdate_ioctl_set_event event; + int ret; + + event.event =3D ev; + event.size =3D sizeof(event); + + ret =3D ioctl(luo, LIVEUPDATE_IOCTL_SET_EVENT, &event); + ksft_assert(!ret); + + return ret; +} + +int liveupdate_preserve_iommufd(int luo, int iommufd, int token) +{ + struct liveupdate_ioctl_fd_preserve preserve; + int ret; + + preserve.fd =3D iommufd; + preserve.token =3D token; + preserve.size =3D sizeof(preserve); + + ret =3D ioctl(luo, LIVEUPDATE_IOCTL_FD_PRESERVE, &preserve); + ksft_assert(!ret); + + return ret; +} + +int liveupdate_restore_iommufd(int luo, int token) +{ + struct liveupdate_ioctl_fd_restore restore; + int ret; + + restore.token =3D token; + restore.size =3D sizeof(restore); + + ret =3D ioctl(luo, LIVEUPDATE_IOCTL_FD_RESTORE, &restore); + ksft_assert(!ret); + ksft_assert(restore.fd > 0); + + return restore.fd; +} + +int main(int argc, char *argv[]) +{ + int iommufd, cdev_fd, luo, ret; + const int token =3D 0x123456; + + if (argc < 2) { + printf("Usage: ./iommufd_liveupdate \n"); + return 1; + } + + cdev_fd =3D setup_cdev(argv[1]); + + luo =3D open_liveupdate_orchestrator(); + ksft_assert(luo > 0); + + if (liveupdate_state_normal(luo)) + iommufd =3D open_iommufd(); + else if (liveupdate_state_updated(luo)) + iommufd =3D liveupdate_restore_iommufd(luo, token); + else + ksft_exit_fail_msg("Test can only run when LUO state is normal or update= d"); + + ret =3D setup_iommufd(iommufd, cdev_fd); + ksft_assert(!ret); + + if (liveupdate_state_normal(luo)) { + ret =3D liveupdate_preserve_iommufd(luo, iommufd, token); + ksft_assert(!ret); + + ret =3D liveupdate_set_event(luo, LIVEUPDATE_PREPARE); + ksft_assert(!ret); + + while (1) + sleep(5); + } else { + ret =3D liveupdate_set_event(luo, LIVEUPDATE_FINISH); + ksft_assert(!ret); + } + + return 0; +} + --=20 2.51.0.536.g15c5d4f767-goog