From patchwork Thu Feb 29 09:46:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 208348 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp357669dyb; Thu, 29 Feb 2024 04:31:51 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU16G+pXt+mUe7631fTGjYotiRqhShLVHYvQGpKCq7k9IHl2DzOIZ4kBFozYtCN8l4AYVJJrN+3C/X3amCEcHG3z8CJFQ== X-Google-Smtp-Source: AGHT+IGWSZsCTMnSdkqy8SA+EJ0N/UfSrHic7BOZZp+eLUygUZw9mjwwS/WlAuLybO19dnwc0zJb X-Received: by 2002:a17:906:b43:b0:a44:1cb4:2e7f with SMTP id v3-20020a1709060b4300b00a441cb42e7fmr1643115ejg.20.1709209911067; Thu, 29 Feb 2024 04:31:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709209911; cv=pass; d=google.com; s=arc-20160816; b=aTcS/Kmea/M3IINQW+QInTyacnNK/JGYKD1dtKN5Jji56zi4X4/ONry4OImXLSGF0E esYyxQHCkMb5FtriMYkfq/uUnvYMcZPoxwcp+hcCx7AJFl/NAI1eVk5WPtXTey1sEBq8 n9s1xbpS3fUBFSAN198U7rixOGv7b/VHtGpm0C25pMSI4sECPluclPqRR4zRc4BN7nl/ QrzsqgtgR0jRH8lQawZvSKcpDfzKiXRLFywcjNKzrt2Ak4/HQXlAibod0FM45hbAyzxo eNozy63jle2MengUlcce9+3pKKHIMMpROLdKVZ0McsMEiZrxVn67NBcVSMKVh79ZDtwO 18OA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=1A6ObPg1Lcehpzm+MrTJzw/Ld73lndJAqssn//PqZVY=; fh=uyI/4ttAQPfURKZyGpapvNeqD+HuuY0cyBg8vJ5ndgc=; b=YCzeNkTw1DKUDENAq3X9NCv1nXGrHm24OhuHH4Zxg8I+lOceDfGvTBhYk36i0MvvFT 0NDuG3gS5WygsW613Pde/WY6BTZy0mZ4WYQazTPcdgmtfE+BiWEl7m1n2xAaIWLpkvwY AKipBUrDbnA93eNLlsCGJIOvRZwVfhA/alKuFYdHTcd0OW4dGiCNh/YNNqmbb4/0gCe4 n7HMcIDJfyOisBBzyPlxTzvt67EkTyk0MaJ/6+NIBVNp3s+UrzgqB6yC1euNi+/GD8f7 oONVKgJ6xjrneV6axxe/SY41FUFDgwWmu0uS86R2QZx80QWukwODp/fa9KMtPcuRDJbC qYVw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=R1tHi1kJ; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-86480-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86480-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id bh10-20020a170906a0ca00b00a3f80081696si539071ejb.241.2024.02.29.04.31.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 04:31:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86480-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=R1tHi1kJ; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-86480-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86480-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 440191F25A47 for ; Thu, 29 Feb 2024 09:52:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 899E5629FD; Thu, 29 Feb 2024 09:52:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="R1tHi1kJ" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 44F10627F8 for ; Thu, 29 Feb 2024 09:52:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709200339; cv=none; b=me++rMs+ky2+h3JK6JvD1ZtsGSrQ5xNPM7RdmqbZ8IprFLOp1XVKTpDBN4lgoRkKlhSPUobtN2wEGV/DFcShlAgEo7ODu8W3zYiQWaga166l2gghEHl1VtvGhs8HUSMLEuFHjbgv3ALHk9JSFnbKCXkOwKqYnfVlm2epsLzkVyc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709200339; c=relaxed/simple; bh=vaA/zQDUtXU1RUde31FgXuYCnR5c2pHYcZwv74k8mCM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ETZwVTHr26quik+nQwuoxRC/GO2bmbmJx0shj7E9Bq5toABqMu7WcF1UePjIxwmkO97Qde1orkgVccqelS+LcOS5d5bss+yDMI0cT4QqD/8xU1ft4MnYzFTKLrE7BKLktNvtGYs810fGfh7GZe1vHQpI+JBuNDlwUa+WdaCS9t4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=R1tHi1kJ; arc=none smtp.client-ip=192.198.163.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709200338; x=1740736338; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vaA/zQDUtXU1RUde31FgXuYCnR5c2pHYcZwv74k8mCM=; b=R1tHi1kJDWdno7jaKfqZ1QLyO6AHTMH7TcQ1gaK/FAxDNRaPsed+IoKX wRvdVaySzsNWW45/WnLSUqq+m/g+pD2EmhPO2AaIK6NUlap28lQg4Pyqu ffAiz4vh48rkHJnzjLFYUWW6rECY4siExIIUCBSkaBtHWVkk91t723Cuf uSn/crYc1upc2xDPmOnFtCLpslc8dGcgNiatZX5kmLpFpQwKSLDHVQqj+ AEHVuoj1IF3VsLdoLK85PU7g2YlcBfMxogi4vmtQZwxRwMLl3e8p9bj7Y 1eoskERi9XUMEXoR3Z0TOIVYaRM62EQhJiVwRkUfLTs4kYGs+shhwNScd g==; X-IronPort-AV: E=McAfee;i="6600,9927,10998"; a="14366670" X-IronPort-AV: E=Sophos;i="6.06,194,1705392000"; d="scan'208";a="14366670" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Feb 2024 01:52:17 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,194,1705392000"; d="scan'208";a="38811017" Received: from allen-box.sh.intel.com ([10.239.159.127]) by fmviesa001.fm.intel.com with ESMTP; 29 Feb 2024 01:52:15 -0800 From: Lu Baolu To: Joerg Roedel , Will Deacon , Robin Murphy , Jason Gunthorpe , Kevin Tian , Eric Badger Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Lu Baolu , Jason Gunthorpe Subject: [PATCH v2 1/2] iommu: Add static iommu_ops->release_domain Date: Thu, 29 Feb 2024 17:46:12 +0800 Message-Id: <20240229094613.121575-2-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240229094613.121575-1-baolu.lu@linux.intel.com> References: <20240229094613.121575-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792236491523066191 X-GMAIL-MSGID: 1792236491523066191 The current device_release callback for individual iommu drivers does the following: 1) Silent IOMMU DMA translation: It detaches any existing domain from the device and puts it into a blocking state (some drivers might use the identity state). 2) Resource release: It releases resources allocated during the device_probe callback and restores the device to its pre-probe state. Step 1 is challenging for individual iommu drivers because each must check if a domain is already attached to the device. Additionally, if a deferred attach never occurred, the device_release should avoid modifying hardware configuration regardless of the reason for its call. To simplify this process, introduce a static release_domain within the iommu_ops structure. It can be either a blocking or identity domain depending on the iommu hardware. The iommu core will decide whether to attach this domain before the device_release callback, eliminating the need for repetitive code in various drivers. Consequently, the device_release callback can focus solely on the opposite operations of device_probe, including releasing all resources allocated during that callback. Co-developed-by: Jason Gunthorpe Signed-off-by: Jason Gunthorpe Signed-off-by: Lu Baolu Reviewed-by: Kevin Tian --- include/linux/iommu.h | 1 + drivers/iommu/iommu.c | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index f08e6aa32657..2d44d4f01cc2 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -585,6 +585,7 @@ struct iommu_ops { struct module *owner; struct iommu_domain *identity_domain; struct iommu_domain *blocked_domain; + struct iommu_domain *release_domain; struct iommu_domain *default_domain; }; diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 210dc7b4c8cf..7158aa3d38af 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -461,13 +461,24 @@ static void iommu_deinit_device(struct device *dev) /* * release_device() must stop using any attached domain on the device. - * If there are still other devices in the group they are not effected + * If there are still other devices in the group, they are not affected * by this callback. * - * The IOMMU driver must set the device to either an identity or - * blocking translation and stop using any domain pointer, as it is - * going to be freed. + * If the iommu driver provides release_domain, the core code ensures + * that domain is attached prior to calling release_device. Drivers can + * use this to enforce a translation on the idle iommu. Typically, the + * global static blocked_domain is a good choice. + * + * Otherwise, the iommu driver must set the device to either an identity + * or a blocking translation in release_device() and stop using any + * domain pointer, as it is going to be freed. + * + * Regardless, if a delayed attach never occurred, then the release + * should still avoid touching any hardware configuration either. */ + if (!dev->iommu->attach_deferred && ops->release_domain) + ops->release_domain->ops->attach_dev(ops->release_domain, dev); + if (ops->release_device) ops->release_device(dev); From patchwork Thu Feb 29 09:46:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 208308 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp317565dyb; Thu, 29 Feb 2024 03:10:37 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW+qKBbYtNE2tgTQMhe5EkkymuLaVKOC1ZjAMAdZR1KruTM9qESGFMDkBF1D3UnIa8etb6ZPgpekuQGAokwPbDo6XuHCA== X-Google-Smtp-Source: AGHT+IEsIM3i3lADmet41P2YvTVOa4X3YZFk9+MxemsnKRxLgPsCtQztV6tOsLNql0/oMarE7H47 X-Received: by 2002:ac8:4b42:0:b0:42e:b936:78b1 with SMTP id e2-20020ac84b42000000b0042eb93678b1mr1286279qts.58.1709205037556; Thu, 29 Feb 2024 03:10:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709205037; cv=pass; d=google.com; s=arc-20160816; b=i+KY6rtOU/krIJHVe9WN3Ec743tcRDbayZhdvxuczYeMm3eKfK/PcHo2IX3LGgZTQS unLj2VIMdifHm96C1wMKp5wGfc9uP8O67FmtyfygPYK17KHnoUGRPpYuPe1UQpoqVtFE vtk3eoyX0e+ZYZW78wbGJYqw2BtKNJet6MNUaVihbyrtLwWvW/Rebw9/Bm8Vqfk6Axgq Ohge/nQ+voJKCrKW4IMe14NJukmZFb1USp2S1JDLtfRLXDyEL2hp8gq7Vsy96akoP+Mo e+G3ssPXgdODvXWsuGaHH2T+/zwJEZLOODuYKyasl/c+t/ZW9R8SLh4QjBQhyKSlQ5uZ 5TQQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=z0drPAoXhU0Zm24QUqMhGwjjIUnhJFEMILq1FpcVLIA=; fh=q/aNRRwbk+AHb42I2d6wYfBCRE+T0txaX+dEwQcyGW0=; b=n/pu9IlJ4A8qF013l33jiKv/aRnWo2CT3UeMvHGI5lABI/HBIwe8vxDQj+2TQ+VEax eUnoFefpyaLsSoAS3wAq7y/HQ8sGMpYtPx2g2+Lv92Gkd18kf3Bn7+KJT2yxDzzrGEx0 4VdzX0qyAvV9vV7+I/1EyrFSnuUo6T/fQfOKa/pjF3iHtHckav7iGGm4BbAsfUkb7V6Y SEHi9L7oboRqR9G86Z6h+IKviVI/Lqz/nRprxaAk6MXip+8O3a2Ssm+TxrXL2S3I7bwn 9dKPu7AsH+vSYT8GLwk4Ri4Cs8oqTTQU34ZASu52bKjYkLRsjDTieRw894kHsjqVMzAk z8NA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TJMoyXAj; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-86481-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86481-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id y12-20020ac8524c000000b0042e797890b0si1120158qtn.561.2024.02.29.03.10.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 03:10:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86481-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TJMoyXAj; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-86481-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86481-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D77E51C239C1 for ; Thu, 29 Feb 2024 09:52:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 72B4E6280A; Thu, 29 Feb 2024 09:52:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="TJMoyXAj" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 847766281E for ; Thu, 29 Feb 2024 09:52:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709200342; cv=none; b=AYFwk+IHX/yO4SaOdGqZqn+IBajip1KPffx+V5IgYCXmCZGRy7z0QtUAN8oNf9l64GQTYGz1OLupzhRJiZjbhnV0S8nt1n7Yeq2Ai5rOL8LViaB/sfJrSbmb/KdpTQ7rLzkPjyNHhdpdTEw1PxfTkIbob8zWajKJk52MTQgf4fs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709200342; c=relaxed/simple; bh=lKzo68tQ2/euX957y2t0ouNXm48fmjaBIyuo8kUBY4I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=t0o4E14zosOSfQDwYmbdvZRBGZJLFLmMTQ/d9gHSAxlPyLaBVTDxJz1XtlabZHg+3zKDaRZY+AnvXYJVN30bvWf6R6qRLoPBQBvfFKt9I2tumTJ7LjrNeWha+2gIIFXJh2Lxzj5fcIJs+AeXvR2Hvip7zx31bOoGGQlDWL9e5Qk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=TJMoyXAj; arc=none smtp.client-ip=192.198.163.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709200340; x=1740736340; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=lKzo68tQ2/euX957y2t0ouNXm48fmjaBIyuo8kUBY4I=; b=TJMoyXAjNVvmZcCJ75BAr0bqcobLu4D7JWO63zrSSqMoQRh9vsbtZ3j6 H84JgMoHL+KKWdfwlObweU1MsEmw6YvorBqolMukqeqIYaIr/2Zu7HUdR veumDIGofq5WOFydaoHoJF/027VTcnmpE6a6wD3zfPvWdNXf6ea0YJnBm JLp8L4uvNvoGhaYkL/c95ZJnjG42p6FRk9ajTiIV2Ea9gPn6Nt14rpYgE oscgwJtq65WdtrJUJVA4R6Q+T7EjW4ZHeaJEdlxOzelSiL1m8xXwWAgK+ BpnMrNyGxVpBqWjYFAumg8I2tmZ/5cBMrohWo6UAWfxLrMWOClnhSvMZy Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10998"; a="14366693" X-IronPort-AV: E=Sophos;i="6.06,194,1705392000"; d="scan'208";a="14366693" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Feb 2024 01:52:19 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,194,1705392000"; d="scan'208";a="38811022" Received: from allen-box.sh.intel.com ([10.239.159.127]) by fmviesa001.fm.intel.com with ESMTP; 29 Feb 2024 01:52:18 -0800 From: Lu Baolu To: Joerg Roedel , Will Deacon , Robin Murphy , Jason Gunthorpe , Kevin Tian , Eric Badger Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v2 2/2] iommu/vt-d: Fix NULL domain on device release Date: Thu, 29 Feb 2024 17:46:13 +0800 Message-Id: <20240229094613.121575-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240229094613.121575-1-baolu.lu@linux.intel.com> References: <20240229094613.121575-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792231381364733662 X-GMAIL-MSGID: 1792231381364733662 In the kdump kernel, the IOMMU operates in deferred_attach mode. In this mode, info->domain may not yet be assigned by the time the release_device function is called. It leads to the following crash in the crash kernel: BUG: kernel NULL pointer dereference, address: 000000000000003c ... RIP: 0010:do_raw_spin_lock+0xa/0xa0 ... _raw_spin_lock_irqsave+0x1b/0x30 intel_iommu_release_device+0x96/0x170 iommu_deinit_device+0x39/0xf0 __iommu_group_remove_device+0xa0/0xd0 iommu_bus_notifier+0x55/0xb0 notifier_call_chain+0x5a/0xd0 blocking_notifier_call_chain+0x41/0x60 bus_notify+0x34/0x50 device_del+0x269/0x3d0 pci_remove_bus_device+0x77/0x100 p2sb_bar+0xae/0x1d0 ... i801_probe+0x423/0x740 Use the release_domain mechanism to fix it. The scalable mode context entry which is not part of release_domain should be cleared in release_device(). Fixes: 586081d3f6b1 ("iommu/vt-d: Remove DEFER_DEVICE_DOMAIN_INFO") Reported-by: Eric Badger Closes: https://lore.kernel.org/r/20240113181713.1817855-1-ebadger@purestorage.com Signed-off-by: Lu Baolu --- drivers/iommu/intel/pasid.h | 1 + drivers/iommu/intel/iommu.c | 31 +++----------- drivers/iommu/intel/pasid.c | 83 +++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 25 deletions(-) diff --git a/drivers/iommu/intel/pasid.h b/drivers/iommu/intel/pasid.h index 487ede039bdd..42fda97fd851 100644 --- a/drivers/iommu/intel/pasid.h +++ b/drivers/iommu/intel/pasid.h @@ -318,4 +318,5 @@ void intel_pasid_tear_down_entry(struct intel_iommu *iommu, bool fault_ignore); void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu, struct device *dev, u32 pasid); +void intel_pasid_teardown_sm_context(struct device *dev); #endif /* __INTEL_PASID_H */ diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index cc3994efd362..f74d42d3258f 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -3869,30 +3869,6 @@ static void domain_context_clear(struct device_domain_info *info) &domain_context_clear_one_cb, info); } -static void dmar_remove_one_dev_info(struct device *dev) -{ - struct device_domain_info *info = dev_iommu_priv_get(dev); - struct dmar_domain *domain = info->domain; - struct intel_iommu *iommu = info->iommu; - unsigned long flags; - - if (!dev_is_real_dma_subdevice(info->dev)) { - if (dev_is_pci(info->dev) && sm_supported(iommu)) - intel_pasid_tear_down_entry(iommu, info->dev, - IOMMU_NO_PASID, false); - - iommu_disable_pci_caps(info); - domain_context_clear(info); - } - - spin_lock_irqsave(&domain->lock, flags); - list_del(&info->link); - spin_unlock_irqrestore(&domain->lock, flags); - - domain_detach_iommu(domain, iommu); - info->domain = NULL; -} - /* * Clear the page table pointer in context or pasid table entries so that * all DMA requests without PASID from the device are blocked. If the page @@ -4431,7 +4407,11 @@ static void intel_iommu_release_device(struct device *dev) mutex_lock(&iommu->iopf_lock); device_rbtree_remove(info); mutex_unlock(&iommu->iopf_lock); - dmar_remove_one_dev_info(dev); + + if (sm_supported(iommu) && !dev_is_real_dma_subdevice(dev) && + !context_copied(iommu, info->bus, info->devfn)) + intel_pasid_teardown_sm_context(dev); + intel_pasid_free_table(dev); intel_iommu_debugfs_remove_dev(info); kfree(info); @@ -4922,6 +4902,7 @@ static const struct iommu_dirty_ops intel_dirty_ops = { const struct iommu_ops intel_iommu_ops = { .blocked_domain = &blocking_domain, + .release_domain = &blocking_domain, .capable = intel_iommu_capable, .hw_info = intel_iommu_hw_info, .domain_alloc = intel_iommu_domain_alloc, diff --git a/drivers/iommu/intel/pasid.c b/drivers/iommu/intel/pasid.c index 108158e2b907..52068cf52fe2 100644 --- a/drivers/iommu/intel/pasid.c +++ b/drivers/iommu/intel/pasid.c @@ -667,3 +667,86 @@ int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev, return 0; } + +/* + * Interfaces to setup or teardown a pasid table to the scalable-mode + * context table entry: + */ + +/* + * Cache invalidation for changes to a scalable-mode context table + * entry. + * + * Section 6.5.3.3 of the VT-d spec: + * - Device-selective context-cache invalidation; + * - Domain-selective PASID-cache invalidation to affected domains + * (can be skipped if all PASID entries were not-present); + * - Domain-selective IOTLB invalidation to affected domains; + * - Global Device-TLB invalidation to affected functions. + * + * Note that RWBF (Required Write-Buffer Flushing) capability has + * been deprecated for scable mode. Section 11.4.2 of the VT-d spec: + * + * HRWBF: Hardware implementations reporting Scalable Mode Translation + * Support (SMTS) as Set also report this field as Clear. + */ +static void sm_context_flush_caches(struct device *dev) +{ + struct device_domain_info *info = dev_iommu_priv_get(dev); + struct intel_iommu *iommu = info->iommu; + + iommu->flush.flush_context(iommu, 0, PCI_DEVID(info->bus, info->devfn), + DMA_CCMD_MASK_NOBIT, DMA_CCMD_DEVICE_INVL); + qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0); + iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); + devtlb_invalidation_with_pasid(iommu, dev, IOMMU_NO_PASID); +} + +static void context_entry_teardown_pasid_table(struct intel_iommu *iommu, + struct context_entry *context) +{ + context_clear_entry(context); + if (!ecap_coherent(iommu->ecap)) + clflush_cache_range(context, sizeof(*context)); +} + +static void device_pasid_table_teardown(struct device *dev, u8 bus, u8 devfn) +{ + struct device_domain_info *info = dev_iommu_priv_get(dev); + struct intel_iommu *iommu = info->iommu; + struct context_entry *context; + + spin_lock(&iommu->lock); + context = iommu_context_addr(iommu, bus, devfn, false); + if (!context) { + spin_unlock(&iommu->lock); + return; + } + + context_entry_teardown_pasid_table(iommu, context); + spin_unlock(&iommu->lock); + + sm_context_flush_caches(dev); +} + +static int pci_pasid_table_teardown(struct pci_dev *pdev, u16 alias, void *data) +{ + struct device *dev = data; + + if (dev == &pdev->dev) + device_pasid_table_teardown(dev, PCI_BUS_NUM(alias), alias & 0xff); + + return 0; +} + +void intel_pasid_teardown_sm_context(struct device *dev) +{ + struct device_domain_info *info = dev_iommu_priv_get(dev); + + if (!dev_is_pci(dev)) { + device_pasid_table_teardown(dev, info->bus, info->devfn); + return; + } + + pci_for_each_dma_alias(to_pci_dev(dev), pci_pasid_table_teardown, dev); +}