From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 751F031354F for ; Tue, 2 Dec 2025 23:03:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716628; cv=none; b=aoP9K7OmIaquQN3QClO7skiAo8wKca18mrshrwIXTweuToHpUwucSjgP0ERUwsOpkrOjugDFJ4N0t/G5W0YOvSaul9H0DOVR4ZmEDAoBZSxWvuHyOuaj3GjV8wgR+ykGVR1Kb2HIhtRcn0szsUXr4KTGeN7flG0Bjt2tIgtwHtI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716628; c=relaxed/simple; bh=trC+TNdN6VLVn3UOdV3aInOJKLZeApPzbmZ2TRA5JhM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eoNPSBHuOcUKx+9LMdA5LyjIL93Tw5hKDNALgtZfi9g6qTaFoUOyMNQYvJfuduGr5qp2FGOjkGRhXMuRKhYC5WEs4E5OrHrY/H95fTlZsrU5Wa+7tuFaFq36cYDbYRx/vJSUuuuG8cA2nyD5xGXlX5OQU7OE0YLUaNQ8oprlbD4= 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=3no2LTn9; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3no2LTn9" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-297d50cd8c4so163158155ad.0 for ; Tue, 02 Dec 2025 15:03:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764716626; x=1765321426; 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=F3UibQVWg0fC/MfVYBeY3d9nYlOGX58qpOdLisvdcHU=; b=3no2LTn96xgNu9jhi3SsCeM6QWV3DmocSPxSy5oWm6FwnrYaQfb545ONCzNeUa+gHe ilLsumS0Ny0YaIPjHp8Qt7ooGLLU8vdXuV1wmrC1dLtRJxRJlVYj0zdXdWKwIw/o+hGk gsA2HR1UKrEMCakt6C2lUxhdJ9rPg2bXM07UTOohfsC52q8Y+oQiZsvQ9AGTHU6pSml6 OXgL42hfUsmg4llkSLeMLQvrJ9hiyEvCSQwP7GbTw2t11Ri8wtTwgLkM5sGI3xK7Rm56 8fdGptkAc/ezFnLUpQdai07ViWoEIhAlqmBLHa9iyuv614fihAoLHUwo82AXBG2K7kAF poQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764716626; x=1765321426; 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=F3UibQVWg0fC/MfVYBeY3d9nYlOGX58qpOdLisvdcHU=; b=qsJlJLs+DXLS6oAjvvBTNTEhyc1kRRVMloX5RyNx8oq+MKxrxSZ/zF3ZsjgXKu6Alc TKceloeKgYee1jKyYWJ0AZgh4kMn2eeBMkHrcGs9sHB4HAPH1Qo7gFrfoBZJKgMHT2hU s6ntPX6pBdgFB/VK9En5utT0NUPVpXmbqt3iz044jzWUN9POEdF2Na7XAYHPaCfiKPvP l6NXCb+7tyQhB7FuxWK9Ozn38OYswuPfHXF2BbUdzf0Orif/6ZZQ9MugU7PLVaNi5KwC OZcTSmgNULDFiu8Nbnf+m4zRHIhY0o7mIAz+9Dh5F5PD4CTdk4AFOkZH4gy0pPBrjo8A x5Og== X-Forwarded-Encrypted: i=1; AJvYcCXJU/GITW3UtRFIhpmGKxfC9Xwjh48abLVBM3h+o68DP/J1bpnw7bnQs7RxZYBiv7wCvAz+OJ05DkKXJw4=@vger.kernel.org X-Gm-Message-State: AOJu0YzMATY1t077fggNVmNwdhfbsGVeRhhGyVDH19PPh8+5Xi2ypJJM wJYtROzNCUtmezCaBU2TmC1gc0/PZKTcuZidX9oHjeEaeXscYPS2OiK/jfwsrb1jzLwhrpG07Vu cGs7zdcBpW39M1g== X-Google-Smtp-Source: AGHT+IEauqgmLCM3DCqI746jCchiGjXOAJpZy4Pe1Qioq8h3ZTfr1kGTnyJn9UdaGWBB6xpAm2BwUwSWhDnM+w== X-Received: from plj12.prod.google.com ([2002:a17:902:c14c:b0:295:64f4:dde2]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d482:b0:295:3d5d:fe37 with SMTP id d9443c01a7336-29d6842ccf2mr2744665ad.41.1764716625733; Tue, 02 Dec 2025 15:03:45 -0800 (PST) Date: Tue, 2 Dec 2025 23:02:57 +0000 In-Reply-To: <20251202230303.1017519-1-skhawaja@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251202230303.1017519-1-skhawaja@google.com> X-Mailer: git-send-email 2.52.0.158.g65b55ccf14-goog Message-ID: <20251202230303.1017519-28-skhawaja@google.com> Subject: [RFC PATCH v2 27/32] iommu: restore preserved domain and reattach From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Pasha Tatashin , Jason Gunthorpe , iommu@lists.linux.dev Cc: Samiullah Khawaja , Robin Murphy , Pratyush Yadav , Kevin Tian , Alex Williamson , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, YiFei Zhu , Chris Li , praan@google.com Content-Type: text/plain; charset="UTF-8" During boot the preserved iommu domains need to be recreated and reattached to the preserved devices. Once the device is reattached to the preserved domain, the preserved_state set in the dev_iommu can be unset. Signed-off-by: Samiullah Khawaja --- drivers/iommu/iommu.c | 39 ++++++++++++++++++++++++++++++++++++-- drivers/iommu/liveupdate.c | 31 ++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 1 + 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 3feb440de40a..07453611cf8e 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -489,6 +490,10 @@ static int iommu_init_device(struct device *dev) } dev->iommu->iommu_dev = iommu_dev; +#ifdef CONFIG_LIVEUPDATE + dev->iommu->device_ser = iommu_get_device_preserved_data(dev); +#endif + ret = iommu_device_link(iommu_dev, dev); if (ret) goto err_release; @@ -2161,6 +2166,12 @@ static int __iommu_attach_device(struct iommu_domain *domain, if (old) atomic_dec(&old->attach_count); +#ifdef CONFIG_LIVEUPDATE + /* The associated state can be unset once restored. */ + if (dev_iommu_restored_state(dev)) + WRITE_ONCE(dev->iommu->device_ser, NULL); +#endif + dev->iommu->attach_deferred = 0; trace_attach_device_to_domain(dev); return 0; @@ -3006,6 +3017,27 @@ int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids) } EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); +static struct iommu_domain *__iommu_group_maybe_restore_domain(struct iommu_group *group) +{ + struct device_ser *device_ser; + struct iommu_domain *domain; + struct device *dev; + + dev = iommu_group_first_dev(group); + if (!dev_is_pci(dev)) + return NULL; + + device_ser = dev_iommu_restored_state(dev); + if (!device_ser) + return NULL; + + domain = iommu_restore_domain(dev, device_ser); + if (WARN_ON(IS_ERR(domain))) + return NULL; + + return domain; +} + /** * iommu_setup_default_domain - Set the default_domain for the group * @group: Group to change @@ -3020,8 +3052,8 @@ static int iommu_setup_default_domain(struct iommu_group *group, int target_type) { struct iommu_domain *old_dom = group->default_domain; + struct iommu_domain *dom, *restored_domain; struct group_device *gdev; - struct iommu_domain *dom; bool direct_failed; int req_type; int ret; @@ -3065,6 +3097,9 @@ static int iommu_setup_default_domain(struct iommu_group *group, /* We must set default_domain early for __iommu_device_set_domain */ group->default_domain = dom; if (!group->domain) { + restored_domain = __iommu_group_maybe_restore_domain(group); + if (!restored_domain) + restored_domain = dom; /* * Drivers are not allowed to fail the first domain attach. * The only way to recover from this is to fail attaching the @@ -3072,7 +3107,7 @@ static int iommu_setup_default_domain(struct iommu_group *group, * in group->default_domain so it is freed after. */ ret = __iommu_group_set_domain_internal( - group, dom, IOMMU_SET_DOMAIN_MUST_SUCCEED); + group, restored_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED); if (WARN_ON(ret)) goto out_free_old; } else { diff --git a/drivers/iommu/liveupdate.c b/drivers/iommu/liveupdate.c index 1ca97612c501..735b7d9417c7 100644 --- a/drivers/iommu/liveupdate.c +++ b/drivers/iommu/liveupdate.c @@ -467,3 +467,34 @@ int iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev) { return -EOPNOTSUPP; } + +struct iommu_domain *iommu_restore_domain(struct device *dev, struct device_ser *ser) +{ + struct iommu_domain_ser *domain_ser; + struct iommu_lu_flb_obj *flb_obj; + struct iommu_domain *domain; + int ret; + + domain_ser = __va(ser->domain_iommu_ser.domain_phys); + + ret = liveupdate_flb_get_incoming(&iommu_flb, (void **)&flb_obj); + if (ret) + return ERR_PTR(ret); + + guard(mutex)(&flb_obj->lock); + if (domain_ser->restored_domain) + return domain_ser->restored_domain; + + domain_ser->obj.incoming = true; + domain = iommu_paging_domain_alloc(dev); + if (IS_ERR(domain)) + return domain; + + ret = domain->ops->restore(domain, domain_ser); + if (ret) + return ERR_PTR(ret); + + domain->preserved_state = domain_ser; + domain_ser->restored_domain = domain; + return domain; +} diff --git a/include/linux/iommu-lu.h b/include/linux/iommu-lu.h index d0226ec19b2f..1f90f4fb0328 100644 --- a/include/linux/iommu-lu.h +++ b/include/linux/iommu-lu.h @@ -78,6 +78,7 @@ static inline void *iommu_domain_restored_state(struct iommu_domain *domain) } #endif +struct iommu_domain *iommu_restore_domain(struct device *dev, struct device_ser *ser); int iommu_for_each_preserved_device(int (*fn)(struct device_ser *ser, void *arg), void *arg); struct device_ser *iommu_get_device_preserved_data(struct device *dev); struct iommu_ser *iommu_get_preserved_data(u64 token, enum iommu_lu_type type); -- 2.52.0.158.g65b55ccf14-goog