Received: by 2002:ab2:2997:0:b0:1ec:cbc4:63fb with SMTP id n23csp323357lqb; Thu, 29 Feb 2024 01:52:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVm0a2umftluOa6NtF4oMFAZlLVaMWXGlhF9P9EIVpNLfEmb/tLO+2hGfjPlFAmwNqH9tCNnHYkX0xt2XSt62VuS0B99gsVHnAnUPJnVQ== X-Google-Smtp-Source: AGHT+IHvqdaRaoykSk6eAl4tv/dnSLIAll2rGhI/Qt8iW1HeIQSs5nlJbFfcFk2a87ByxjmRpT1k X-Received: by 2002:a17:906:6946:b0:a43:4ce5:db5c with SMTP id c6-20020a170906694600b00a434ce5db5cmr1032790ejs.16.1709200353063; Thu, 29 Feb 2024 01:52:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709200353; cv=pass; d=google.com; s=arc-20160816; b=sy4Y++XetUTmD3DdWz9sjggI9Tia/7PUbgo0veXLtnzwzZDY2On+q52BIrfbWCAxSo EJ4nfP1oZPKDHXdG/419gKsrrEs/2mAquXzBFjjov3JUOZKxEzjXgDLKt6ne8WADX0uR z0HzkIBW5/Ut3stOntmG2lEmYTvVOPM4EhGJmXNXFFLvKdpVLv1muk7WyU1tfq5f/Wzw 6DSub0wW8+tT2KqvFQwOfPbJ+DYWDyh/OqiG58miSVBECUDZIBQKOAbCkldrilD46xaz +bCQNjDiKco0dNDL/redeJItzrJT3t2XfmJWVN2KY+TY7uIf2RFcrofGzpKm59rnHR2x TOyw== 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-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86480-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id c13-20020a170906528d00b00a442e29d2cesi418398ejm.898.2024.02.29.01.52.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 01:52:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86480-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86480-linux.lists.archive=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 A27CD1F25729 for ; Thu, 29 Feb 2024 09:52:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 93E96629E1; Thu, 29 Feb 2024 09:52:20 +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 Content-Transfer-Encoding: 8bit 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); -- 2.34.1