Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp1772342lqs; Sun, 16 Jun 2024 04:11:54 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXdnVywkJlbAVxkeLD2wr3iChTGUVMAB4G0yjbKzv7FcCEWZANoll2G3Mb7Uzw2YX70/zsxX9EOzsre5i3Cjxgj+9E0gRRw4l2GKlHmpQ== X-Google-Smtp-Source: AGHT+IHM2LJx+66C0InyeUZhK0KueLcOJv1EJtcYP5EnEpARJsNzBfBx+yNsREMJ3fu11B8pZ+kd X-Received: by 2002:a17:903:950:b0:1f7:38a2:f1e8 with SMTP id d9443c01a7336-1f8627e422bmr98260715ad.41.1718536314322; Sun, 16 Jun 2024 04:11:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718536314; cv=pass; d=google.com; s=arc-20160816; b=rckzcTaMcs4ComRAhgXGnD6t3u4N1HbuBCMS9rMTFLU1F2PiFvOUwukcYoMyk1Rqzp LUgm+QLZ3zESFgnibX4KAHFgeJMiVrwmUMzm0BEHjOY313gO7CfOwFs/u6HMc/sqKsEJ ry089xj8seJwxXbhXY3FAmB262a9a11nFV5gnrVnyg/PNIieNlv2ve2N45H2H5OCdzJk oCll8xVN8txauRzFR4gHUERyRwjwGvGP3sc8udPqH+XFkqVwsZiB9wPwi5nIO7kuW74e rTGSJHCgi0RzwmVlNmY3nCPqAv48ZqjRp2OhL8Xm+ZbxRwzfFxvUgQpVSMmWPEAFCBNr 5qmg== 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=uJ3N8A4nXwKYk9FL5xEDxBjwh0ih1SZjVQIk+z4AuqM=; fh=tXslIEC/iII+0o9YvlUtRXapl6zSPusBr1yEidN1Q7w=; b=DEVOc99N6cKtN9fZWmuhOMjijZ3pB0E8busw4uPzfvZnz2n/Ygs9RCSesESwj09R3a CEoFaW3VDzt2YYXM4gR/SX09E61ke9OlV1WO2eXmxg0wGzjnGAy1WH7pj6gMiY/AiUd1 xWbFqm5keQqGMjO2NtyQJVcL7IJff58GMks5w8xlUhcUeUolOXFp279ntRL6HCn4ZjPm 1z5KFAOCN3AwBQk3xkknCylfTPmCwaMEoKfeloKvTzwq4wnEseacz5a6VmEj917EPlKg F2oeDGFdpLcQrNta/8pdltAu6ODjGDiIRTfLxCXOWWLhYPn8hC0kGomoxsQ7zeHObytT X90g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZcaQFP09; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-216133-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-216133-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d9443c01a7336-1f855f0b5d2si73410155ad.538.2024.06.16.04.11.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Jun 2024 04:11:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-216133-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZcaQFP09; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-216133-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-216133-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 0E473B21C7E for ; Sun, 16 Jun 2024 06:15:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 508C7178CC9; Sun, 16 Jun 2024 06:14:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ZcaQFP09" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) (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 B796D16FF41 for ; Sun, 16 Jun 2024 06:14:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.13 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718518471; cv=none; b=qQ60ZHbYs4YlQsjbB7g+sEqhG+D9HUj4YmCHiaSryJl4gcohkBsB1QxmPbzUL1/ZG0ghK3XuMqKl5CDYGJj4KZnIWF01wVuHofvYVSk5fAvZIosLYVxbh85/VTdPNAfs0zGjPIDNEuWQCChi97QSGsqXDys6EpGF9PCLOAGqyhI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718518471; c=relaxed/simple; bh=qodzswP5GQ9SEGf4F0B5i+MCeMzNe8O2apiCZ+ZU4II=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XpFg0ijMm7r5RNx6NrtpJqHKoUiA7apLBPRXurWfvPepGPQkUIq7uqPq62+afWaXjvZ+9tr3hgDn6U8oRvQtqRCPWNjvZ89TxCp8VqvLj6eU6SOQGmige8CSrlQNh3Hgrh1tOcGIEmJFh9LigbCHjMV9jmaEzAB5Uy/rx38xIdU= 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=ZcaQFP09; arc=none smtp.client-ip=192.198.163.13 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=1718518470; x=1750054470; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qodzswP5GQ9SEGf4F0B5i+MCeMzNe8O2apiCZ+ZU4II=; b=ZcaQFP093Kw3BlagdBDSHcbpivW71RnR5G2Ii7s+n0o/XmoqJvQAFN5M /tgXv1YhfKSDpU54HRmvs/yGRPkFwg+i2D+9w2G4PI1J1I3FaX6yJyAOb 3MmE7VvQ3sCXeY2YHKmwksrpXEEi0H28fdcDpjdRGWvsSnzcgTKLaj5wz 8IuHmENzVrePs1iu0c3hqIiP9TDaRSjASTKhlNVYFv1ABwa0YmiXiKn9Y ytQgcoJHnI4yTqUGQT++5a/rlq1ynC/9V7txYUwZe65fpvFVij5ROvDFP SGSzbiPhJcnxvdbKeKnocTxYQX0xWXYI3nurt4J109KH3oAjudECUl4qC w==; X-CSE-ConnectionGUID: ZLVK5hR3R4OuruAOu3gNBQ== X-CSE-MsgGUID: glYz065lQAyjwXoYi4PCmw== X-IronPort-AV: E=McAfee;i="6700,10204,11104"; a="18289991" X-IronPort-AV: E=Sophos;i="6.08,241,1712646000"; d="scan'208";a="18289991" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jun 2024 23:14:29 -0700 X-CSE-ConnectionGUID: HMjnR5+oTqSGa9jGb63OFw== X-CSE-MsgGUID: pfpQSHiGQmO9jWT/kbnnwg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,241,1712646000"; d="scan'208";a="40748144" Received: from unknown (HELO allen-box.sh.intel.com) ([10.239.159.127]) by fmviesa007.fm.intel.com with ESMTP; 15 Jun 2024 23:14:25 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados Cc: iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v7 02/10] iommu: Remove sva handle list Date: Sun, 16 Jun 2024 14:11:47 +0800 Message-Id: <20240616061155.169343-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240616061155.169343-1-baolu.lu@linux.intel.com> References: <20240616061155.169343-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 struct sva_iommu represents an association between an SVA domain and a PASID of a device. It's stored in the iommu group's pasid array and also tracked by a list in the per-mm data structure. Removes duplicate tracking of sva_iommu by eliminating the list. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 2 -- drivers/iommu/iommu-priv.h | 3 +++ drivers/iommu/iommu-sva.c | 30 ++++++++++++++++++++---------- drivers/iommu/iommu.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index afc5af0069bb..87ebcc29020e 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -1005,14 +1005,12 @@ struct iommu_attach_handle { struct iommu_sva { struct iommu_attach_handle handle; struct device *dev; - struct list_head handle_item; refcount_t users; }; struct iommu_mm_data { u32 pasid; struct list_head sva_domains; - struct list_head sva_handles; }; int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode, diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h index 5f731d994803..f1536a5ebb0d 100644 --- a/drivers/iommu/iommu-priv.h +++ b/drivers/iommu/iommu-priv.h @@ -28,4 +28,7 @@ void iommu_device_unregister_bus(struct iommu_device *iommu, const struct bus_type *bus, struct notifier_block *nb); +struct iommu_attach_handle *iommu_attach_handle_get(struct iommu_group *group, + ioasid_t pasid, + unsigned int type); #endif /* __LINUX_IOMMU_PRIV_H */ diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index 0fb923254062..9b7f62517419 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -41,7 +41,6 @@ static struct iommu_mm_data *iommu_alloc_mm_data(struct mm_struct *mm, struct de } iommu_mm->pasid = pasid; INIT_LIST_HEAD(&iommu_mm->sva_domains); - INIT_LIST_HEAD(&iommu_mm->sva_handles); /* * Make sure the write to mm->iommu_mm is not reordered in front of * initialization to iommu_mm fields. If it does, readers may see a @@ -69,11 +68,16 @@ static struct iommu_mm_data *iommu_alloc_mm_data(struct mm_struct *mm, struct de */ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm) { + struct iommu_group *group = dev->iommu_group; + struct iommu_attach_handle *attach_handle; struct iommu_mm_data *iommu_mm; struct iommu_domain *domain; struct iommu_sva *handle; int ret; + if (!group) + return ERR_PTR(-ENODEV); + mutex_lock(&iommu_sva_lock); /* Allocate mm->pasid if necessary. */ @@ -83,12 +87,22 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm goto out_unlock; } - list_for_each_entry(handle, &mm->iommu_mm->sva_handles, handle_item) { - if (handle->dev == dev) { - refcount_inc(&handle->users); - mutex_unlock(&iommu_sva_lock); - return handle; + /* A bond already exists, just take a reference`. */ + attach_handle = iommu_attach_handle_get(group, iommu_mm->pasid, IOMMU_DOMAIN_SVA); + if (!IS_ERR(attach_handle)) { + handle = container_of(attach_handle, struct iommu_sva, handle); + if (attach_handle->domain->mm != mm) { + ret = -EBUSY; + goto out_unlock; } + refcount_inc(&handle->users); + mutex_unlock(&iommu_sva_lock); + return handle; + } + + if (PTR_ERR(attach_handle) != -ENOENT) { + ret = PTR_ERR(attach_handle); + goto out_unlock; } handle = kzalloc(sizeof(*handle), GFP_KERNEL); @@ -99,7 +113,6 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm /* Search for an existing domain. */ list_for_each_entry(domain, &mm->iommu_mm->sva_domains, next) { - handle->handle.domain = domain; ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid, &handle->handle); if (!ret) { @@ -115,7 +128,6 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm goto out_free_handle; } - handle->handle.domain = domain; ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid, &handle->handle); if (ret) @@ -125,7 +137,6 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm out: refcount_set(&handle->users, 1); - list_add(&handle->handle_item, &mm->iommu_mm->sva_handles); mutex_unlock(&iommu_sva_lock); handle->dev = dev; return handle; @@ -159,7 +170,6 @@ void iommu_sva_unbind_device(struct iommu_sva *handle) mutex_unlock(&iommu_sva_lock); return; } - list_del(&handle->handle_item); iommu_detach_device_pasid(domain, dev, iommu_mm->pasid); if (--domain->users == 0) { diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index a712b0cc3a1d..7890bd21dff6 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3486,3 +3486,34 @@ void iommu_free_global_pasid(ioasid_t pasid) ida_free(&iommu_global_pasid_ida, pasid); } EXPORT_SYMBOL_GPL(iommu_free_global_pasid); + +/** + * iommu_attach_handle_get - Return the attach handle + * @group: the iommu group that domain was attached to + * @pasid: the pasid within the group + * @type: matched domain type, 0 for any match + * + * Return handle or ERR_PTR(-ENOENT) on none, ERR_PTR(-EBUSY) on mismatch. + * + * Return the attach handle to the caller. The life cycle of an iommu attach + * handle is from the time when the domain is attached to the time when the + * domain is detached. Callers are required to synchronize the call of + * iommu_attach_handle_get() with domain attachment and detachment. The attach + * handle can only be used during its life cycle. + */ +struct iommu_attach_handle * +iommu_attach_handle_get(struct iommu_group *group, ioasid_t pasid, unsigned int type) +{ + struct iommu_attach_handle *handle; + + xa_lock(&group->pasid_array); + handle = xa_load(&group->pasid_array, pasid); + if (!handle) + handle = ERR_PTR(-ENOENT); + else if (type && handle->domain->type != type) + handle = ERR_PTR(-EBUSY); + xa_unlock(&group->pasid_array); + + return handle; +} +EXPORT_SYMBOL_NS_GPL(iommu_attach_handle_get, IOMMUFD_INTERNAL); -- 2.34.1