Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp533444rwe; Thu, 1 Sep 2022 04:00:19 -0700 (PDT) X-Google-Smtp-Source: AA6agR4EVgkl9ykVSclJhWIZpIslRNEKRKNBalIz4uzoAMdxF1fAljYV+yBvQUJfj8khUBz4aslf X-Received: by 2002:a17:90b:4a02:b0:1fe:1391:314d with SMTP id kk2-20020a17090b4a0200b001fe1391314dmr8166033pjb.216.1662030018758; Thu, 01 Sep 2022 04:00:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662030018; cv=none; d=google.com; s=arc-20160816; b=ncAJSkOXnrAqF1aQ+D048w+OlHCLX/bkgPW1qXBbCEzApefeJhD/ThalW+3e/RVRgd n4H4HVxmJOneYSDIuNI0iAH2BZCa/UX72JnzY4yPrg6MYmzZQ2+AtajPUgss6SVvcYOs 2Y1XGhd9r8l7gtNhvsPJ+DdtpllkCIwIRKsmx9rawMPiCRtPschnAgjm2UHBIoCskibp A0+LJodxVA2ONFbz4t0dfLQUsAoQUAxx/4cHpJKDt8CC/1jlmnFSgzUOieSATkkTzcG6 zzLtSsNr8jHdEELnuKEZkoM1+r6BaOu0OLP6uZbGcGHxm1RH3h7XzHcDPRNgcuV5NK7a ptWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:to:content-language:subject:cc:user-agent:mime-version :date:message-id:dkim-signature; bh=Mf00MLE52s52R0U810iZms/FdrZP0cC3x19q4whNcU8=; b=RfqJgl9rdAGpSOKCR6ibr8BNlig5LkfMqcb9EJNQHs6K1640BiK9fndYPQMaVxe67L +vIgeV3+iBSBcMBueo9hAWGvjz+ZSlzRZLj1ZJZzk9ErCQjOyJ9tH+XTKxNgPJ6m6/Hw qtkkY+06GhZvynjVEDR2C6PRyNY/oMPcgPEc1g9hwDgV+g418n4x9vsLwVyFGw+x++FB tIP8jjf9NydDp618T3dMm2SAa4I+NhxCaEA5qCaOkWHOTZUGzr34R8XI1enfE4YE623Z ga9kfJH1XXbP9KDVIGMw8lipxm5Jb9ITBrqf4ODDgCCZLZEXD6k4aAnJasBvaIse3VIp GQqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=E3arJ6qa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n3-20020a170902d2c300b001728964e6eesi10876244plc.622.2022.09.01.04.00.02; Thu, 01 Sep 2022 04:00:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=E3arJ6qa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231802AbiIAKof (ORCPT + 99 others); Thu, 1 Sep 2022 06:44:35 -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 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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