From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 4F2BB308F39 for ; Tue, 2 Dec 2025 23:03:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716619; cv=none; b=FlyIGkDfRF8tbPhNxdCsb2rkjTtelAe3NBYqyfKD5AQ1qIXDfjfBO2yFRGCCqITuZZWcmmlk+3R/jkGjwGgh/WXpqfTJ5/5s87CpEQ4FVLHTUY0ymWDUXb8USyQz//7ixaiJBhNFJEmwUKpjMMSYc47vo43IrO+91QcD12SrPek= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716619; c=relaxed/simple; bh=yUZI/irVfwiaP4PFSwpXr/fxPmv+UTawXdvsT/zLHjk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cVQU1yo+KeEbqZQFENkqlvw5NQM6e0OQCJVRg5ACDIrcpF7QX36chqMZzmd4T39oDnnVpIzou3ujK5PFqnpAKSDD7ZXg/WdLqXyXlCgwYOESNwOOSV0ovwt+OCtWhk8yvdooa7vIEInyQAipZ/dX+iGrN0HDC/FfpMdXByrayWA= 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=AiCeUkF3; arc=none smtp.client-ip=209.85.216.74 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="AiCeUkF3" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-343bf6ded5cso9596169a91.0 for ; Tue, 02 Dec 2025 15:03:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764716617; x=1765321417; 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=4mZ6L2jBPpsYrbVWvwV7OWcG6bK6uqvVq9Q2DCACrdk=; b=AiCeUkF3mQrLKxJjnnS59X7vjTCNpmywH1Ro4vKiB3rhI5I5HcubXgtBB95Z6Im/es qhnEybiU0BWN3zQvMk5s0hm+sJrZwippAIfu2gGOPVopaYIo3nZ0WOkahMux818spAn6 1zU0G1ywTYuRhJQUA1Joky5c5Nrm+W8i2GNqRDRMflkQU2kEG25KzwgNFV3pWv0H2VGE kEiFWRRYhrgcCpIIDexj/mHIDjJRCPHhGWK/pDYOq8xTrJi/GpbcrjTd+CjXIpxw5hBD rFPhfqLBNPuhWYjddXB0F7LJcJR+cl+70ayqrNSb7tKSgZLeFsPL7hx1/QH6UAHo7NAL GwHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764716617; x=1765321417; 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=4mZ6L2jBPpsYrbVWvwV7OWcG6bK6uqvVq9Q2DCACrdk=; b=tkGLsNxCYIFZclndD92eA/YIH50YB1CltHwGioUhLj0I0uzlmBAfRdjEZyLC0gFLy4 cJmNXnLlfvu6PisEtwx2OTi065JodvKA1PwysScEfv44TOXDX7j5Zdy2IfmPlzOU0h5b GDTsqnGe4xLb/GlYV2e8OK1DVPbI8FwPLbglCRsVDDS3UVT/omGXVC2uiXL0iszK/5xf fs60fl8EbqTYhXW0nNlw91SV4vBV/BjuxeaeKZ2c3qFLMYaBtzoWWXvJgVexCxSiuZuU 6304rEIirLW2ar09QE64oqpQ8TBS9WgQT+T6rLJqlidLMj7aW6D++FvlCzuvlgqpI93C e8aw== X-Forwarded-Encrypted: i=1; AJvYcCVNkwE78YBuGaSGh7k8LiFuTh+BO3zmihXpnRLS6owq+r3bYGShqx+Oo0SqOr/N4eO5rCINuTUSN81cUQ0=@vger.kernel.org X-Gm-Message-State: AOJu0YyoZUOmBbYDHVAqPZ+DA95j7ec0Tn5qFYmn/Fkt+T4yf/U4VAfK F98Or6jpTU1H0A2sFjERwQ9nOtvANrhyWJz+H4CXRTYR/qmWtfHK6Iet4RvX94FgCxi/A+UsNiq 2ELlMuccPl24ClQ== X-Google-Smtp-Source: AGHT+IEYhoU7ADyVksV3xKCXL4U6ohVJDbeio3wZoom6OShVYzd0zuWruj0oybl/9ze7UEg+jWKn0RqmwKZRBQ== X-Received: from pjbca18.prod.google.com ([2002:a17:90a:f312:b0:32b:ae4c:196c]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4a8b:b0:341:194:5e7a with SMTP id 98e67ed59e1d1-349127fdfd5mr335129a91.29.1764716616667; Tue, 02 Dec 2025 15:03:36 -0800 (PST) Date: Tue, 2 Dec 2025 23:02:51 +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-22-skhawaja@google.com> Subject: [RFC PATCH v2 21/32] iommu/vt-d: Clean the context entries of unpreserved devices 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 normal shutdown the iommu translation is disabled. Since the root table is preserved during live update, it needs to be cleaned up and the context entries of the unpreserved devices need to be cleared. Signed-off-by: Samiullah Khawaja --- drivers/iommu/intel/iommu.c | 33 ++++++++++++++++++++++++++++++-- drivers/iommu/intel/iommu.h | 1 + drivers/iommu/intel/liveupdate.c | 1 + 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 3f69a073b2d8..84fef81ecf4d 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,10 @@ static int rwbf_quirk; #define rwbf_required(iommu) (rwbf_quirk || cap_rwbf((iommu)->cap)) +#ifdef CONFIG_LIVEUPDATE +static void __clean_unpreserved_context_entries(struct intel_iommu *iommu); +#endif + /* * set to 1 to panic kernel if can't successfully enable VT-d * (used when kernel is launched w/ TXT) @@ -2376,8 +2381,12 @@ void intel_iommu_shutdown(void) /* Disable PMRs explicitly here. */ iommu_disable_protect_mem_regions(iommu); - /* Make sure the IOMMUs are switched off */ - iommu_disable_translation(iommu); + if (iommu->iommu.outgoing_preserved_state) { + __clean_unpreserved_context_entries(iommu); + } else { + /* Make sure the IOMMUs are switched off */ + iommu_disable_translation(iommu); + } } } @@ -2884,6 +2893,26 @@ static const struct iommu_dirty_ops intel_second_stage_dirty_ops = { .set_dirty_tracking = intel_iommu_set_dirty_tracking, }; +static void __clean_unpreserved_context_entries(struct intel_iommu *iommu) +{ + struct device_domain_info *info; + struct pci_dev *pdev = NULL; + + for_each_pci_dev(pdev) { + info = dev_iommu_priv_get(&pdev->dev); + if (!info) + continue; + + if (info->iommu != iommu) + continue; + + if (dev_iommu_preserved_state(&pdev->dev)) + continue; + + domain_context_clear(info); + } +} + static struct iommu_domain * intel_iommu_domain_alloc_second_stage(struct device *dev, struct intel_iommu *iommu, u32 flags) diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index ea88c86030bb..1eb60ce1300f 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -1283,6 +1283,7 @@ int intel_iommu_preserve_device(struct device *dev, struct device_ser *device_se void intel_iommu_unpreserve_device(struct device *dev, struct device_ser *device_ser); int intel_iommu_preserve(struct iommu_device *iommu, struct iommu_ser *iommu_ser); void intel_iommu_unpreserve(struct iommu_device *iommu, struct iommu_ser *iommu_ser); +bool intel_iommu_liveupdate_clear_context_entries(struct intel_iommu *iommu); #endif #ifdef CONFIG_INTEL_IOMMU_SVM diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupdate.c index 491075802e4b..3f8c7f15bc36 100644 --- a/drivers/iommu/intel/liveupdate.c +++ b/drivers/iommu/intel/liveupdate.c @@ -9,6 +9,7 @@ #include #include +#include #include #include -- 2.52.0.158.g65b55ccf14-goog