From patchwork Mon Feb 5 12:01:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gowans, James" X-Patchwork-Id: 13545356 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8AF82C4828D for ; Mon, 5 Feb 2024 12:04:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4EBBB6B009A; Mon, 5 Feb 2024 07:04:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 49AF56B009B; Mon, 5 Feb 2024 07:04:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 33C2C6B009C; Mon, 5 Feb 2024 07:04:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 174EC6B009B for ; Mon, 5 Feb 2024 07:04:38 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id ED822A0A77 for ; Mon, 5 Feb 2024 12:04:37 +0000 (UTC) X-FDA: 81757618194.08.5725743 Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) by imf18.hostedemail.com (Postfix) with ESMTP id F34BC1C0014 for ; Mon, 5 Feb 2024 12:04:35 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=ZpHiYYS7; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf18.hostedemail.com: domain of "prvs=75897cb1d=jgowans@amazon.com" designates 52.119.213.154 as permitted sender) smtp.mailfrom="prvs=75897cb1d=jgowans@amazon.com" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707134676; a=rsa-sha256; cv=none; b=tCppRAWuoEip17YICTuCgKlYLA6S6p8CcTk1gSIlprPcKNuAgcHlfkFTcHByhOaDSDQm0a BIDO2nxR8+oTJ5AxeA13I2f30L2fN31wVYXglCD/6F3TBLrAa7Z8IsCww0TJ7InjfsXMzD RdgQfOoc2iW/elAFp1XGa5bwFsIo/yU= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=ZpHiYYS7; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf18.hostedemail.com: domain of "prvs=75897cb1d=jgowans@amazon.com" designates 52.119.213.154 as permitted sender) smtp.mailfrom="prvs=75897cb1d=jgowans@amazon.com" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707134676; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=R61AUSOacFWGGPMbVgCPWnIrJ1KmkLbm3r8EEWKzIHg=; b=3QzxeHjeA99jqY3dpMplJR0rffkuE5iU3klZto989uztw37PsmysfiY49SY/g29j9azhHs gX1rI0doJMJTxnY7Hg2bkqjgjH5rHCc3qG1zZH0xElSzudNrXtJ6mBaTxHnOEdMBhtmVIr ulQ20nrl4bjlhBoi8j5RzQnAQMIRucg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1707134676; x=1738670676; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=R61AUSOacFWGGPMbVgCPWnIrJ1KmkLbm3r8EEWKzIHg=; b=ZpHiYYS7bX7Ee49iWVwgn6tXTcpzrQg/OJnRaSUU+v0EuUDGFcIda7LO mY2xzJKofyFUzM1DcCoHChqTwj4RQkUcyhJHjPRSfxKtVcqRRmNS7Y+ay ACpFwNad/ZcuF1gsLqjjysVMrpa1wm5F71PkDijz43Vm0nZt15Jgnizj+ g=; X-IronPort-AV: E=Sophos;i="6.05,245,1701129600"; d="scan'208";a="182633292" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Feb 2024 12:04:31 +0000 Received: from EX19MTAEUA001.ant.amazon.com [10.0.43.254:29990] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.14.129:2525] with esmtp (Farcaster) id 479437ca-efef-4c96-b04c-24208e90e4af; Mon, 5 Feb 2024 12:04:30 +0000 (UTC) X-Farcaster-Flow-ID: 479437ca-efef-4c96-b04c-24208e90e4af Received: from EX19D014EUC004.ant.amazon.com (10.252.51.182) by EX19MTAEUA001.ant.amazon.com (10.252.50.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Mon, 5 Feb 2024 12:04:29 +0000 Received: from dev-dsk-jgowans-1a-a3faec1f.eu-west-1.amazon.com (172.19.112.191) by EX19D014EUC004.ant.amazon.com (10.252.51.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Mon, 5 Feb 2024 12:04:23 +0000 From: James Gowans To: CC: Eric Biederman , , "Joerg Roedel" , Will Deacon , , Alexander Viro , "Christian Brauner" , , Paolo Bonzini , Sean Christopherson , , Andrew Morton , , Alexander Graf , David Woodhouse , "Jan H . Schoenherr" , Usama Arif , Anthony Yznaga , Stanislav Kinsburskii , , , Subject: [RFC 10/18] iommu/intel: zap context table entries on kexec Date: Mon, 5 Feb 2024 12:01:55 +0000 Message-ID: <20240205120203.60312-11-jgowans@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205120203.60312-1-jgowans@amazon.com> References: <20240205120203.60312-1-jgowans@amazon.com> MIME-Version: 1.0 X-Originating-IP: [172.19.112.191] X-ClientProxiedBy: EX19D041UWA001.ant.amazon.com (10.13.139.124) To EX19D014EUC004.ant.amazon.com (10.252.51.182) X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: F34BC1C0014 X-Stat-Signature: ohcy89tjc9kr184nok6i7134hs6kwtuf X-HE-Tag: 1707134675-543707 X-HE-Meta: U2FsdGVkX19oRc0i7yvsfLQrbiGJVL1o6u0U2CUK0ZBwXOGfzPQliDmbw3QGofAEIoYX6C5TWSwP/heE6JpJARJgvdN9pIuE5mYpwqtpvea7YkJmNC1R6TiWCvnEgvh6DQXil6bi6drebytTUGRugYQ34V2OeN2wtU7r1+nEWomjLGPSbMlMwqQwenidLkEzO60rQT959PHtJR8aA6VeHs4OU2+MYDmHC1MEe4xWvRoxJIHp1S2mFxXG6t1Topfu+Wasw81+VYQQfawWjzIYeZ6sFoqMLoFOlhjjB1TA5Jb1eeBGi8PaRjITHUBJdaJZKnZenqocQ42AtNs0qAYjY8buSwbrY+4+oHjCR+Qzn7wDeLgeTri/7R87oXU0Eyfwqlxbu63UqtoMksn+0VZ45kF/pJM4HS6ZXiWXj8+u1wkUwWlYStr7kvH+r5S9+b2BSGmxrIjLhH0OafTWf5N7aIn8y41VO7ETREmino7tVppA/ZCwWBRwD0vG5RIBrV3+9XZUc/ukADq8hNY9BF66C3Pj2+POPQAIfN3i9qtb/TXTVLqfazDzn3MEqqtIOEL3LrYPn29ZPFo9f5NVFoC1tM6yOz/zjReDAgQYy3ZP5U4wFCMagwFaMQ8TQZxpaxF/v4lBnuQ351hmmEg47TpmI1JVeRBw7omD3ePIQtiV+M+FnhI8NBvBa7lAQntb2N4MG8unFwUq7mR+WPXkNN/F941Cjdj7ei9xWwgb5RCeMN/rh6RLN8XTloBMJL3n+5kost6Nb7+WCS+OCcnd09KmUToROXnwkEsSYqRW2pTBhu/n0LIxFDzD79mZH3LhpK+1WLU5JOOo9jsO8LYv50Y2TFH1vfphirl3UR6TkLoEDqJYF6ZOkI6cHDugq8uQySdoQuyb794aa/j1zfTdaW/1wTwexYizgViw0aDdxF+beGRHQa2Q2ZEl7eKhbqF+vZeH1hWEiDgNWDyliBvky7a dIGNftME qkwkEDUhHO5CIXbW89pJc7wG6N1t+DI8+TN+a66f3rQeqfyn0GHa4wJs0CPXpMp1HdX3gicr2pZ4CKL5lGuzSwP62P6cIahNFJ5DI0+VfiuXdK4jgW5zOdUv2mHWm35T7bKSg5n8oL3uZXol/32lYMbtDpBxUIA/xLJ9QaKVoOegkmHXU+6r9Frl3IyZAsvjfo2hxdALNfDAq4cvo3rYKV7ifIGw2OkJ/85EziPKKBtkBXG7cWB3ML4MCIl3cDXnVomtxitK+vey4RnG2UcE6K8ueOtH4BMd+uowtUzrjKxoSVFCMgxRfUzwm+sSHivOJV0YvcD7kFCXvzd8pT+bZszKtkCTUMF/KAbbknq0rNhXBWuftmjAjts4o0xjmrbidYFV3GnutLBklyexDYSsy341oBHvPxAjcWwlXhFLXlT25lOHP2vV7Bqc803QMoNrZlJ6pNpqRuJfzfKvHpwoB/CJRNZQk3cgvRaUaRkdsQvJ45DN5qx/3bhS2Onjz0FTuKJhy1twvNXc+6gzLyXfuHEssobGFvUobXbzfrX9/lodTba2G47USOvh/BQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In the next commit the IOMMU shutdown function will be modified to not actually shut down the IOMMU when doing a kexec. To prevent leaving DMA mappings for non-persistent devices around during kexec we add a function to the kexec flow which iterates though all IOMMU domains and zaps the context entries for the devices belonging to those domain. A list of domains for the IOMMU is added and maintained. --- drivers/iommu/intel/dmar.c | 1 + drivers/iommu/intel/iommu.c | 34 ++++++++++++++++++++++++++++++---- drivers/iommu/intel/iommu.h | 2 ++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c index 23cb80d62a9a..00f69f40a4ac 100644 --- a/drivers/iommu/intel/dmar.c +++ b/drivers/iommu/intel/dmar.c @@ -1097,6 +1097,7 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd) iommu->segment = drhd->segment; iommu->node = NUMA_NO_NODE; + INIT_LIST_HEAD(&iommu->domains); ver = readl(iommu->reg + DMAR_VER_REG); pr_info("%s: reg_base_addr %llx ver %d:%d cap %llx ecap %llx\n", diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 2dd3f055dbce..315c6b7f901c 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -1831,6 +1831,7 @@ static int domain_attach_iommu(struct dmar_domain *domain, goto err_clear; } domain_update_iommu_cap(domain); + list_add(&domain->domains, &iommu->domains); spin_unlock(&iommu->lock); return 0; @@ -3608,6 +3609,33 @@ static void intel_disable_iommus(void) iommu_disable_translation(iommu); } +void zap_context_table_entries(struct intel_iommu *iommu) +{ + struct context_entry *context; + struct dmar_domain *domain; + struct device_domain_info *device; + int bus, devfn; + u16 did_old; + + list_for_each_entry(domain, &iommu->domains, domains) { + list_for_each_entry(device, &domain->devices, link) { + context = iommu_context_addr(iommu, device->bus, device->devfn, 0); + if (!context || !context_present(context)) + continue; + context_domain_id(context); + context_clear_entry(context); + __iommu_flush_cache(iommu, context, sizeof(*context)); + iommu->flush.flush_context(iommu, + did_old, + (((u16)bus) << 8) | devfn, + DMA_CCMD_MASK_NOBIT, + DMA_CCMD_DEVICE_INVL); + iommu->flush.flush_iotlb(iommu, did_old, 0, 0, + DMA_TLB_DSI_FLUSH); + } + } +} + void intel_iommu_shutdown(void) { struct dmar_drhd_unit *drhd; @@ -3620,10 +3648,8 @@ void intel_iommu_shutdown(void) /* Disable PMRs explicitly here. */ for_each_iommu(iommu, drhd) - iommu_disable_protect_mem_regions(iommu); - - /* Make sure the IOMMUs are switched off */ - intel_disable_iommus(); + zap_context_table_entries(iommu); + return up_write(&dmar_global_lock); } diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index a2338e398ba3..4a2f163a86f3 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -600,6 +600,7 @@ struct dmar_domain { spinlock_t lock; /* Protect device tracking lists */ struct list_head devices; /* all devices' list */ struct list_head dev_pasids; /* all attached pasids */ + struct list_head domains; /* all struct dmar_domains on this IOMMU */ struct dma_pte *pgd; /* virtual address */ int gaw; /* max guest address width */ @@ -700,6 +701,7 @@ struct intel_iommu { void *perf_statistic; struct iommu_pmu *pmu; + struct list_head domains; /* all struct dmar_domains on this IOMMU */ }; /* PCI domain-device relationship */