Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp1926862imi; Sun, 24 Jul 2022 00:54:31 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sygQ7cHqj0MCCl8o8NLXHiKYVm/Qg7YkTJJWBGPTwFsyWRdQ0iT3m703IXm+Y68AEABunR X-Received: by 2002:a17:902:d58c:b0:16c:fdb7:86e9 with SMTP id k12-20020a170902d58c00b0016cfdb786e9mr7411167plh.74.1658649271187; Sun, 24 Jul 2022 00:54:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658649271; cv=none; d=google.com; s=arc-20160816; b=WTXTqjsOC4EdRCln5T0zhNWXbKMvZILMejGAOM82Sc8f2BkESH9EA8zG5JCmYwtBk4 HmNCXkCcudRJ9V/MK3BadtTMK6wdBzdVSZiMdlcQq5CtRZHvEwieuG82APhL7y284VBK S3I7SFTbD/egJU0OFRyctv1fPVFoj9m8GOgO52BBjv7Tdiia9tlmTiFelyZzhCQeBPI+ Thg+YCcs4wLYBIO7rzEZDeheAkQUU7pR4DyzgxVUFVz1vIyYwf3m2YxCZyJ1TZ/71xv0 8A9G+gRNKrbBDIzo1Wuk1kPnmRa9+CSth4YYzyDA4pH7RAiOFjm8yAKaVLxtzwDYclh7 QEog== 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=39uAJ0t7J+B9fzr8rsqZW2hZuvCLCPALoEm3tgvTqd8=; b=pw3Yhhq9coP7zFreLxQly634dfAQWXEfbiqA8qaSKEDoE71mHbALSpd44EE2ea9Mw8 JvkzuARps3Ijl3fFcNTxhUegzTU/2B77HkTHDS/XupKCaZmSNgKWS90xBMDlImnZWwCX Nc0V7IF7CkNcbiyN41K+5hTjRlkbk1Se1bTZox3ohvyqBMEJKGtPLPOlj10mgBO9Srg+ vdJbXgoz6Aea1M/JSTEvz6YeKSjinRs11tyFqmAA9hgSmYMkwMwTiSRUtrhsuWuRyI5m OchvnBDamhq+WivAxURTj2P8vN0VlbAPKxc/GNNWMJaOASGY8I/UInAUMPspuRz4IKU0 rpqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=kFKeqBRD; 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 f6-20020a6547c6000000b0041255acce87si10473298pgs.92.2022.07.24.00.54.16; Sun, 24 Jul 2022 00:54:31 -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=kFKeqBRD; 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 S239360AbiGXHX2 (ORCPT + 99 others); Sun, 24 Jul 2022 03:23:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232718AbiGXHX1 (ORCPT ); Sun, 24 Jul 2022 03:23:27 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C89A413D6D for ; Sun, 24 Jul 2022 00:23:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1658647405; x=1690183405; h=message-id:date:mime-version:cc:subject:to:references: from:in-reply-to:content-transfer-encoding; bh=W/ZfiGZet5U/LoLWUKDc9YKyt+TrQHvH31tE+Q6WVVY=; b=kFKeqBRDQSDL7ZfBiQDMgxWCrQaNfF0g201rfRp0FU9PZeMoF5mroC7a YolRkeGG0aU7eNKDsD/kWYT+fG+0atyc+yEX6kWW5htHhTnDDwcbezhru fOeLt306MtY+IatTaS3kS5a9y3fS/ocjJmWhIP0KZ1xIb+IllzR5gs1VF q7YHoQdu/suKGb/GlNmbPX4qxpd5gbbjfjxcmU90xakpfhisWqKx6WoUc rhNcfIYDvxRi8kmT8MvaOya32ghNHcCeXBqRHaMOljxHAqEBk65ExISuy 115K6cN1Z6/KUC9ALQPUg/A1KesVrEgFrNHPdZWqgOSX14NW0UQlWZDvy g==; X-IronPort-AV: E=McAfee;i="6400,9594,10417"; a="313263959" X-IronPort-AV: E=Sophos;i="5.93,190,1654585200"; d="scan'208";a="313263959" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2022 00:23:25 -0700 X-IronPort-AV: E=Sophos;i="5.93,190,1654585200"; d="scan'208";a="657737876" Received: from zjiang1-mobl.ccr.corp.intel.com (HELO [10.249.170.155]) ([10.249.170.155]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2022 00:23:21 -0700 Message-ID: <5f845a90-97ba-e994-c8b9-91fc6f3ea1c1@linux.intel.com> Date: Sun, 24 Jul 2022 15:23:19 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Cc: baolu.lu@linux.intel.com, Joerg Roedel , Christoph Hellwig , Kevin Tian , Ashok Raj , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Dave Jiang , Vinod Koul , Eric Auger , Liu Yi L , Jacob jun Pan , Zhangfei Gao , Zhu Tony , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Jean-Philippe Brucker Subject: Re: [PATCH v10 04/12] iommu: Add attach/detach_dev_pasid iommu interface Content-Language: en-US To: Jason Gunthorpe References: <20220705050710.2887204-1-baolu.lu@linux.intel.com> <20220705050710.2887204-5-baolu.lu@linux.intel.com> <20220723141118.GD79279@nvidia.com> From: Baolu Lu In-Reply-To: <20220723141118.GD79279@nvidia.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE 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/7/23 22:11, Jason Gunthorpe wrote: >> + >> + /* >> + * Otherwise, the device came from DT/ACPI, assume it is static and >> + * then singleton can know from the device count in the group. >> + */ >> + return true; >> +} > I would be happer if probe was changed to refuse to add a device to a > group if the group's pasid xarray is not empty, as a protective > measure. Agreed. I will add below code. diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 047898666b9f..e43cb6776087 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -895,6 +895,14 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev) int ret, i = 0; struct group_device *device; + /* + * The iommu_attach_device_pasid() requires a singleton group. + * Refuse to add a device into it if this assumption has been + * made. + */ + if (!xa_empty(group->pasid_array)) + return -EBUSY; + device = kzalloc(sizeof(*device), GFP_KERNEL); if (!device) return -ENOMEM; > >> +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 || !iommu_group_immutable_singleton(group, dev)) { >> + iommu_group_put(group); >> + return -EINVAL; > goto error below > >> + } >> + >> + mutex_lock(&group->mutex); > Just hold the group->mutex a few lines above and don't put locking in > iommu_group_immutable_singleton(), it is clearer Above two comments agreed. iommu_attach_device_pasid() looks like below after update. int iommu_attach_device_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t pasid) { struct iommu_group *group; int ret = -EINVAL; void *curr; if (!domain->ops->set_dev_pasid) return -EOPNOTSUPP; group = iommu_group_get(dev); if (!group) return -ENODEV; mutex_lock(&group->mutex); if (!iommu_group_immutable_singleton(group, dev)) goto out_unlock; curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL); if (curr) { ret = xa_err(curr) ? : -EBUSY; goto out_unlock; } ret = domain->ops->set_dev_pasid(domain, dev, pasid); if (ret) xa_erase(&group->pasid_array, pasid); out_unlock: mutex_unlock(&group->mutex); iommu_group_put(group); return ret; } Best regards, baolu