From patchwork Fri Jan 26 10:53:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 192591 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:e09d:b0:103:945f:af90 with SMTP id gm29csp655458dyb; Fri, 26 Jan 2024 05:25:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IEEu14qs0jvWwQHTYZG/5OuzKz89eB2RhoSMypCqW6FKqQ1D2hqBsCs3DL8RCutAbKSVzX0 X-Received: by 2002:a05:620a:22d3:b0:783:d09e:d000 with SMTP id o19-20020a05620a22d300b00783d09ed000mr604530qki.0.1706275533986; Fri, 26 Jan 2024 05:25:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706275533; cv=pass; d=google.com; s=arc-20160816; b=pECUdAY/Rrq/s2PvTXOiuX2+UE9MP8OzePFIPttH9mWkJJHPYvBDvjA+Jtw9Gfty1N RUu9w3uekUGYTB8IUOFEqUJeRqo08svFXP8OT8yF1TWzo+WGfqS891JTXI3qmc3D1vdT Yl4To8tw6WoNhGSa0FNUy13zhe6OZ3rM1qUpYFFlXp3wM4gc334xGHQhBuKfkCKbwm6Q COg7+2WTeuHcUooxXzJzZDJGLCQoBFcCYYJ/Uip9nnku1MVH+mm5Afkk9F25hy8AVdFM l5rbqs+1iXzuwcBpAfLwTrClk0fX2HM2Se9PKIHRNoeAYuIKEZOQUTH2gSfFHmtIJxFp wP3g== 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=W+HZ0pkBui6FDmU5lSyUQ6KDXCSLMIVvvrQoHTbYST4=; fh=+Q0HmAbA9K+2Zsh7tjb8zIg0VTV8m8JrWwOI0YmM/vM=; b=C8oKVWGN0Ha7vsIG5YoNtKizFwc3OtUNKIIZuafsvXHeVhpnt1qTx1HeXBdMYNXUyB dcyKKZPpd/EcMQYKZrdpCasZoq/Ufp8VBd2XKzZyNNv9YN8P/etXqg06jWbkMNEG4dfH 25idIbAzzD8vO2DiTXBPjsArXC44gxz9ctKNtBfvVFQjg5rmgz2TYiX6FTsKaC3cwai8 ydcObKU/RzBfvPClTfHpehZM/CYkVEzeuHHiCxls1oqyiEwRQNSNiAO/t8fq0nt68zcr CIgLHUfEY0Aie4qVTe0UOubpcLvtv6gc85+CJwlH2vYesF5CRyaHcjGeCQxSPt3pDfIS 2kVw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hn9Odf3J; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-40012-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40012-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 o5-20020a05620a228500b007834e88de07si1219228qkh.396.2024.01.26.05.25.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 05:25:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-40012-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=hn9Odf3J; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-40012-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40012-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 37CC11C27EC9 for ; Fri, 26 Jan 2024 10:59:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 491E317BA3; Fri, 26 Jan 2024 10:59:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hn9Odf3J" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 4FD071427E for ; Fri, 26 Jan 2024 10:59:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706266768; cv=none; b=AwfsHxOco+CmOCT9pXuLyGjATg+ia3NBNIJxFSFtKKyk1xJHybXxT1dOOIKjtJRVoC9rROfrBI0DDQUSI7ZZNJwTwKSIdtRyzPpo5+REuLVHQxms8zeMy1/9HYugSNyxHyAch2aAXYf1cfWTg+yf66lf3EQQGYNXnr3O+nF3Nss= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706266768; c=relaxed/simple; bh=zo5LBMZV0B5NEfxZY5cK3SYRyArtsbTV+MJeKCnjfLM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ez85ImVNVw40oYGFZrbgMOpwRkjYrLAPknJRouDGrKwBDwP6jUMa35Bdf4T5JR+SkQbWzrU0++FkqPXgvxTp7679wtAu1j0DcE1j/II4UhgNdm4xnZ3TmT2Kyjpd4ndd8eTYhwnaZJYo6Ds9CZvhm4uqcvODxAKHmyc1o+UwuqU= 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=hn9Odf3J; arc=none smtp.client-ip=192.198.163.11 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=1706266757; x=1737802757; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zo5LBMZV0B5NEfxZY5cK3SYRyArtsbTV+MJeKCnjfLM=; b=hn9Odf3JaJL5JyZgOnfiFqE7GolbQX2OhDVHCul1hwDBk/cjZl8iD/Si NicqzVz75HWelJtyvE6BdA298vipgvhCFmZfijaPha+W1qgYfa2Ry7Emp NAvHVT0JuAsyGe/7HJwZ/h6JCjx5fopbsqDgu7WZHhjrS3j9VW0XUxdSm QbFOnH49vyKE8qlSbtdg7pU7cJpc36TgwjGvGBVw/UlgtGmYolfRajetN psl3ZxsLcj4DXKTOzjLdWbRqJb6Zq3oKwQgVSR15XP6OPDDvwr14Ontso oyX+y9Vo8XXCLHzl72Kfq2wsFyp1cyDaYJQJ/MUSKN7ZiWuYbot0y3/30 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="9123363" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="9123363" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jan 2024 02:59:17 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="857367842" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="857367842" Received: from allen-box.sh.intel.com ([10.239.159.127]) by fmsmga004.fm.intel.com with ESMTP; 26 Jan 2024 02:59:14 -0800 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v2 1/2] iommu: Use mutex instead of spinlock for iommu_device_list Date: Fri, 26 Jan 2024 18:53:40 +0800 Message-Id: <20240126105341.78086-2-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240126105341.78086-1-baolu.lu@linux.intel.com> References: <20240126105341.78086-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: 1789159574283668630 X-GMAIL-MSGID: 1789159574283668630 The iommu_device_lock spinlock was used to protect the iommu device list. However, there is no requirement to access the iommu device list in interrupt context. Therefore, a mutex is sufficient. This also prepares for the next change, which will iterate the iommu device list and call the probe callback within the locking area. Signed-off-by: Lu Baolu Reviewed-by: Kevin Tian Reviewed-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 68e648b55767..0af0b5544072 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -146,7 +146,7 @@ struct iommu_group_attribute iommu_group_attr_##_name = \ container_of(_kobj, struct iommu_group, kobj) static LIST_HEAD(iommu_device_list); -static DEFINE_SPINLOCK(iommu_device_lock); +static DEFINE_MUTEX(iommu_device_lock); static const struct bus_type * const iommu_buses[] = { &platform_bus_type, @@ -262,9 +262,9 @@ int iommu_device_register(struct iommu_device *iommu, if (hwdev) iommu->fwnode = dev_fwnode(hwdev); - spin_lock(&iommu_device_lock); + mutex_lock(&iommu_device_lock); list_add_tail(&iommu->list, &iommu_device_list); - spin_unlock(&iommu_device_lock); + mutex_unlock(&iommu_device_lock); for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++) err = bus_iommu_probe(iommu_buses[i]); @@ -279,9 +279,9 @@ void iommu_device_unregister(struct iommu_device *iommu) for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) bus_for_each_dev(iommu_buses[i], NULL, iommu, remove_iommu_group); - spin_lock(&iommu_device_lock); + mutex_lock(&iommu_device_lock); list_del(&iommu->list); - spin_unlock(&iommu_device_lock); + mutex_unlock(&iommu_device_lock); /* Pairs with the alloc in generic_single_device_group() */ iommu_group_put(iommu->singleton_group); @@ -316,9 +316,9 @@ int iommu_device_register_bus(struct iommu_device *iommu, if (err) return err; - spin_lock(&iommu_device_lock); + mutex_lock(&iommu_device_lock); list_add_tail(&iommu->list, &iommu_device_list); - spin_unlock(&iommu_device_lock); + mutex_unlock(&iommu_device_lock); err = bus_iommu_probe(bus); if (err) { @@ -2033,9 +2033,9 @@ bool iommu_present(const struct bus_type *bus) for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) { if (iommu_buses[i] == bus) { - spin_lock(&iommu_device_lock); + mutex_lock(&iommu_device_lock); ret = !list_empty(&iommu_device_list); - spin_unlock(&iommu_device_lock); + mutex_unlock(&iommu_device_lock); } } return ret; @@ -2983,13 +2983,13 @@ const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) const struct iommu_ops *ops = NULL; struct iommu_device *iommu; - spin_lock(&iommu_device_lock); + mutex_lock(&iommu_device_lock); list_for_each_entry(iommu, &iommu_device_list, list) if (iommu->fwnode == fwnode) { ops = iommu->ops; break; } - spin_unlock(&iommu_device_lock); + mutex_unlock(&iommu_device_lock); return ops; } From patchwork Fri Jan 26 10:53:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 192606 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:e09d:b0:103:945f:af90 with SMTP id gm29csp679989dyb; Fri, 26 Jan 2024 06:06:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IEuD3P3MF98lz4YnBWDzMy/GPLCgOoxtwyhUj7FpSOnl0pAsaZh44ouCDofQ5dxcdvs15fp X-Received: by 2002:ac8:5e4f:0:b0:42a:5df1:ab23 with SMTP id i15-20020ac85e4f000000b0042a5df1ab23mr2174717qtx.124.1706277983514; Fri, 26 Jan 2024 06:06:23 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706277983; cv=pass; d=google.com; s=arc-20160816; b=oB2r1vqFk8inmKzOrKgFUh4ebVCz6s8YS8W2KI/d8H4Phpn/XRgDvyFOp+EmwLEHWn 5Jj/6GWUkF+I3FsE4bqmDqs3T2Ugw56iBiWctJAnxIx7V9UgiEnNQDRkPyhALIkgGlUy fj4eYNbBL3W8rhEICF2hGNtf5Mr+fIMtZ/0zjWPOXfC0yfnxt52ALP+HsMs4iZiNW5/3 H/0/sTn66xDoWPkGQuwLNIKPkD3ZGR7dZj02t22STCkypFSVVs9A25SlMxlGGlMTrDbS rVEUkru58WpowuaMwpK+7nxqsbExQqxLrHxgZJ1v3COeD/Uv4VN+skUUgDiuJ4oMCZuC VKMw== 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=YjHTlKO47Q6xJAlHvh5/Moahe+11kpNGgwiwZj3d9qw=; fh=+Q0HmAbA9K+2Zsh7tjb8zIg0VTV8m8JrWwOI0YmM/vM=; b=sOkaUI0Z9RvTO7jEowrM8nBV3n7XBs/s30BvKYEM99nVQbZAfPsU53ZD8pRdGwqNeV ril3mYpBE4ZOGwDlak1YyNyJht1xHCLdj84+ewdnoEt4ZPVV3zv7/y43vrh7r27WECBQ RFoiLFG99JRw5QDSVPqVB8zeV0rWcJSMc2ABPfWR3wAQyrRzyiR9S28HCsj8jddihkjd 8kM4vmtv4/JelhZs1u9UvsMw/i2HahT2YrPWie4phmv5mc+zhjtn2bsylZXzVAn1N72H E+/ao5VnX/14Ce8EXuVBRkg50hQ+o0hXruTmV82CKL9zAf4HNpwz9bGMtDRjZa/2n8Wu qeFQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GrsK9Fmj; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-40013-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40013-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 t38-20020a05622a182600b0042a71aa8b24si1260913qtc.241.2024.01.26.06.06.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 06:06:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-40013-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=GrsK9Fmj; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-40013-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40013-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 E65DA1C274C2 for ; Fri, 26 Jan 2024 11:00:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8882A1803E; Fri, 26 Jan 2024 10:59:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="GrsK9Fmj" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 90CA51427A for ; Fri, 26 Jan 2024 10:59:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706266769; cv=none; b=npxYE7XSAMxT19Z80oWaVHcX0E4gOPw1m8wDviyaqLZ9NMv/hvyb39K2l5YbtCizyjFTy2CkZhYq1d84G8nl2+8DxnAoqXuRM1AO2UiguxLvu/ABTKfnjmxLjtdtIX7SPa+U7sqRvir3R0ASDOc1sTeruhGZKhIrI1SFhSsuPJw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706266769; c=relaxed/simple; bh=b4leudU2y75I7gVeqH5B2NhuCYSTwTPov4fkwl1ZnBs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=msKJx9vQY0IoBjmeQd3izeXDLK1Ta/IEY3bcy1WRb7TvWa1rqzo2A3AnSrW0eK9QKWESp+ufZxT31/A0S6QZ7D8VbGhF9GTPlK6yWs5kUedvOvNtYpw8inmmbOsKjInZJXeWIG+rCmtgVcSWNBpNQp36SdhL0CAJl4BT3RjOJ50= 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=GrsK9Fmj; arc=none smtp.client-ip=192.198.163.11 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=1706266760; x=1737802760; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=b4leudU2y75I7gVeqH5B2NhuCYSTwTPov4fkwl1ZnBs=; b=GrsK9FmjLx7P+kQXFxngAyvxH57O275Sq3hEglLu+pz+Vck20YHoKZQj Hx5xVUHQh1mASnpN19hwbm/31X0KLumRatain7w3A0//Z2Qoywu62uB0C CticCQsUkULI7rxv7TJnmfAW++EyemuuPLSaUlQS1LeURcZKrBO4DsC2V wRibZYwxTM/zvPcfbW9JefNM/NWJNlbsdTvlYLFL5reZwNux4DZH29tmG JKLGoKziNtLEfVEvmow/9iHkPreaMFEkcfM88IgFDXlh9JL0nuEkFUAW3 7xgtYQW59KpzUFhI69WzNU18oSa1sx1iRMaIRTkD3nlZZuFXZQ7+fyqWy w==; X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="9123376" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="9123376" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jan 2024 02:59:19 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10964"; a="857367852" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="857367852" Received: from allen-box.sh.intel.com ([10.239.159.127]) by fmsmga004.fm.intel.com with ESMTP; 26 Jan 2024 02:59:16 -0800 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v2 2/2] iommu: Probe right iommu_ops for device Date: Fri, 26 Jan 2024 18:53:41 +0800 Message-Id: <20240126105341.78086-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240126105341.78086-1-baolu.lu@linux.intel.com> References: <20240126105341.78086-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: 1789162142549866470 X-GMAIL-MSGID: 1789162142549866470 Previously, in the iommu probe device path, __iommu_probe_device() gets the iommu_ops for the device from dev->iommu->fwspec if this field has been initialized before probing. Otherwise, it is assumed that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can be present, hence it's feasible to lookup the global iommu device list and use the iommu_ops of the first iommu device which has no dev->iommu->fwspec. The assumption mentioned above is no longer correct with the introduction of the IOMMUFD mock device on x86 platforms. There might be multiple instances of iommu drivers, none of which have the dev->iommu->fwspec field populated. Probe the right iommu_ops for device by iterating over all the global drivers and call their probe function to find a match. Fixes: 17de3f5fdd35 ("iommu: Retire bus ops") Cc: Robin Murphy Signed-off-by: Lu Baolu Reviewed-by: Kevin Tian , with a nit: --- drivers/iommu/iommu.c | 76 +++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 28 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 0af0b5544072..2cdb01e411fa 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -396,30 +396,69 @@ void dev_iommu_priv_set(struct device *dev, void *priv) } EXPORT_SYMBOL_GPL(dev_iommu_priv_set); +static struct iommu_device * +__iommu_do_probe_device(struct device *dev, const struct iommu_ops *ops) +{ + struct iommu_device *iommu_dev; + + if (!try_module_get(ops->owner)) + return ERR_PTR(-EINVAL); + + iommu_dev = ops->probe_device(dev); + if (IS_ERR(iommu_dev)) + module_put(ops->owner); + + return iommu_dev; +} + +static struct iommu_device *iommu_probe_device_ops(struct device *dev) +{ + struct iommu_device *iter, *iommu_dev = ERR_PTR(-ENODEV); + struct iommu_fwspec *fwspec; + + /* + * For FDT-based systems and ACPI IORT/VIOT, drivers register IOMMU + * instances with non-NULL fwnodes, and client devices should have been + * identified with a fwspec by this point. Otherwise, we will iterate + * over all the global drivers and call their probe function to find a + * match. + */ + fwspec = dev_iommu_fwspec_get(dev); + if (fwspec && fwspec->ops) + return __iommu_do_probe_device(dev, fwspec->ops); + + mutex_lock(&iommu_device_lock); + list_for_each_entry(iter, &iommu_device_list, list) { + iommu_dev = __iommu_do_probe_device(dev, iter->ops); + if (!IS_ERR(iommu_dev)) + break; + } + mutex_unlock(&iommu_device_lock); + + return iommu_dev; +} + /* * Init the dev->iommu and dev->iommu_group in the struct device and get the * driver probed */ -static int iommu_init_device(struct device *dev, const struct iommu_ops *ops) +static int iommu_init_device(struct device *dev) { struct iommu_device *iommu_dev; + const struct iommu_ops *ops; struct iommu_group *group; int ret; if (!dev_iommu_get(dev)) return -ENOMEM; - if (!try_module_get(ops->owner)) { - ret = -EINVAL; - goto err_free; - } - - iommu_dev = ops->probe_device(dev); + iommu_dev = iommu_probe_device_ops(dev); if (IS_ERR(iommu_dev)) { ret = PTR_ERR(iommu_dev); - goto err_module_put; + goto err_free; } dev->iommu->iommu_dev = iommu_dev; + ops = dev_iommu_ops(dev); ret = iommu_device_link(iommu_dev, dev); if (ret) @@ -444,7 +483,6 @@ static int iommu_init_device(struct device *dev, const struct iommu_ops *ops) err_release: if (ops->release_device) ops->release_device(dev); -err_module_put: module_put(ops->owner); err_free: dev->iommu->iommu_dev = NULL; @@ -499,28 +537,10 @@ DEFINE_MUTEX(iommu_probe_device_lock); static int __iommu_probe_device(struct device *dev, struct list_head *group_list) { - const struct iommu_ops *ops; - struct iommu_fwspec *fwspec; struct iommu_group *group; struct group_device *gdev; int ret; - /* - * For FDT-based systems and ACPI IORT/VIOT, drivers register IOMMU - * instances with non-NULL fwnodes, and client devices should have been - * identified with a fwspec by this point. Otherwise, we can currently - * assume that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can - * be present, and that any of their registered instances has suitable - * ops for probing, and thus cheekily co-opt the same mechanism. - */ - fwspec = dev_iommu_fwspec_get(dev); - if (fwspec && fwspec->ops) - ops = fwspec->ops; - else - ops = iommu_ops_from_fwnode(NULL); - - if (!ops) - return -ENODEV; /* * Serialise to avoid races between IOMMU drivers registering in * parallel and/or the "replay" calls from ACPI/OF code via client @@ -534,7 +554,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list if (dev->iommu_group) return 0; - ret = iommu_init_device(dev, ops); + ret = iommu_init_device(dev); if (ret) return ret;