From mboxrd@z Thu Jan 1 00:00:00 1970 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E06CECAAD1 for ; Thu, 1 Sep 2022 10:44:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233088AbiIAKog (ORCPT ); Thu, 1 Sep 2022 06:44:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233364AbiIAKoc (ORCPT ); Thu, 1 Sep 2022 06:44:32 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4C93A50E2; Thu, 1 Sep 2022 03:44:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662029070; x=1693565070; h=message-id:date:mime-version:cc:subject:to:references: from:in-reply-to:content-transfer-encoding; bh=z0nHhpyQNt/z52vG9scDUVjFGYLIq163jnyWrsCmkKM=; b=E3arJ6qadWEYD0diCEPq6xV64YIKNgV2JiG1RU8ewLRtCzryM01OPcGg MIZx52ld0q0dDOP5eLa5Op82Ejlnz1YL3ClRY9qfR0ECE++qEJ4vr9KlN QkqgGUzdaupzBZm/q5HCYe+CtlQoFZNygLugkWt9xyjIePebV8Y4GTwKE eUZYDLZGb53NPc2e3+jbpbvNAnb53jT0b9Xl3RmK5NQCaisRFPUxnyYWp 9YYFpwLscm/ZatzJMt8fs1JFG5Fuj9qZEAHpjqa1yqBykrHnEk5Tdz8x9 cPpGHVpZAQRVYJ3QRJS1vOYM/+csFDGX87PAgCXcU1JtbWgbF3wPu8lM9 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10456"; a="278698819" X-IronPort-AV: E=Sophos;i="5.93,280,1654585200"; d="scan'208";a="278698819" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Sep 2022 03:44:30 -0700 X-IronPort-AV: E=Sophos;i="5.93,280,1654585200"; d="scan'208";a="673795597" Received: from luli3-mobl.ccr.corp.intel.com (HELO [10.254.215.169]) ([10.254.215.169]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Sep 2022 03:44:25 -0700 Message-ID: <53837119-9348-f9c9-1f49-cad639fc6ecd@linux.intel.com> Date: Thu, 1 Sep 2022 18:44:10 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Cc: baolu.lu@linux.intel.com, Joerg Roedel , Christoph Hellwig , Bjorn Helgaas , Kevin Tian , Ashok Raj , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Dave Jiang , Fenghua Yu , Vinod Koul , Eric Auger , Liu Yi L , Jacob jun Pan , Zhangfei Gao , Zhu Tony , iommu@lists.linux.dev, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v12 07/17] iommu: Try to allocate blocking domain when probing device Content-Language: en-US To: Jason Gunthorpe References: <20220826121141.50743-1-baolu.lu@linux.intel.com> <20220826121141.50743-8-baolu.lu@linux.intel.com> <316f6575-59ea-08e3-aaaf-bc1e4f42a574@linux.intel.com> From: Baolu Lu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022/8/31 22:10, Jason Gunthorpe wrote: > On Wed, Aug 31, 2022 at 09:49:44AM +0800, Baolu Lu wrote: >>> Maybe all of this is just the good reason to go to a simple >>> device->ops->remove_dev_pasid() callback and forget about blocking >>> domain here. >> >> Do you mean rolling back to what we did in v10? > > Yeah, but it shouldn't be a domain_op, removing a pasid is a device op > > Just > > remove_dev_pasid(struct device *dev, ioasid_t pasid) It's clear now. Thanks! How about below iommu_attach/detach_device_pasid() code? By the way, how about naming it "block_dev_pasid(dev, pasid)"? +static int __iommu_set_group_pasid(struct iommu_domain *domain, + struct iommu_group *group, ioasid_t pasid) +{ + struct group_device *device; + int ret = 0; + + list_for_each_entry(device, &group->devices, list) { + ret = domain->ops->set_dev_pasid(domain, device->dev, pasid); + if (ret) + break; + } + + return ret; +} + +static void __iommu_remove_group_pasid(struct iommu_group *group, + ioasid_t pasid) +{ + struct group_device *device; + const struct iommu_ops *ops; + + list_for_each_entry(device, &group->devices, list) { + ops = dev_iommu_ops(device->dev); + ops->remove_dev_pasid(device->dev, pasid); + } +} + +/* + * iommu_attach_device_pasid() - Attach a domain to pasid of device + * @domain: the iommu domain. + * @dev: the attached device. + * @pasid: the pasid of the device. + * + * Return: 0 on success, or an error. + */ +int iommu_attach_device_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid) +{ + struct iommu_group *group; + void *curr; + int ret; + + if (!domain->ops->set_dev_pasid) + return -EOPNOTSUPP; + + group = iommu_group_get(dev); + if (!group) + return -ENODEV; + + mutex_lock(&group->mutex); + curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL); + if (curr) { + ret = xa_err(curr) ? : -EBUSY; + goto out_unlock; + } + + ret = __iommu_set_group_pasid(domain, group, pasid); + if (ret) { + __iommu_remove_group_pasid(group, pasid); + xa_erase(&group->pasid_array, pasid); + } +out_unlock: + mutex_unlock(&group->mutex); + iommu_group_put(group); + + return ret; +} +EXPORT_SYMBOL_GPL(iommu_attach_device_pasid); + +/* + * iommu_detach_device_pasid() - Detach the domain from pasid of device + * @domain: the iommu domain. + * @dev: the attached device. + * @pasid: the pasid of the device. + * + * The @domain must have been attached to @pasid of the @dev with + * iommu_attach_device_pasid(). + */ +void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev, + ioasid_t pasid) +{ + struct iommu_group *group = iommu_group_get(dev); + + mutex_lock(&group->mutex); + __iommu_remove_group_pasid(group, pasid); + WARN_ON(xa_erase(&group->pasid_array, pasid) != domain); + mutex_unlock(&group->mutex); + + iommu_group_put(group); +} +EXPORT_SYMBOL_GPL(iommu_detach_device_pasid); + * @remove_dev_pasid: Remove any translation configurations of a specific + * pasid, so that any DMA transactions with this pasid + * will be blocked by the hardware. * @pgsize_bitmap: bitmap of all possible supported page sizes * @owner: Driver module providing these ops */ @@ -256,6 +259,7 @@ struct iommu_ops { struct iommu_page_response *msg); int (*def_domain_type)(struct device *dev); + void (*remove_dev_pasid)(struct device *dev, ioasid_t pasid); Best regards, baolu