From patchwork Mon Mar 6 02:57:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 64396 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1631597wrd; Sun, 5 Mar 2023 19:04:53 -0800 (PST) X-Google-Smtp-Source: AK7set+H4O28I13JoQFcBVfsWBUkNYNnG8yUZdub+orRpqWFvHtXjRq/H9LmPI4d52xHgfXEdGyo X-Received: by 2002:a17:907:80e:b0:885:6a2e:f941 with SMTP id wv14-20020a170907080e00b008856a2ef941mr11840685ejb.54.1678071893315; Sun, 05 Mar 2023 19:04:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678071893; cv=none; d=google.com; s=arc-20160816; b=U9u0JIzA12lKoTGxFyq4honnlMtn/MhzhkcX9R9dOekt1pgx1FStwHXT89LFmyoIF2 MXS076PUOgog/6Flsh5/KW9rBzXTLpl3wBe43/pAtuGASrelNKKf9/3LBxaMmNFa7+HJ lf75WHSZTRD6P48scse5EN8uqDmYdj5RH+efSm2nxiKvjkmblMiGGVr/gX/GnqPag7ND rBI0JHvXf8KwLbeqcH4ba3uDzO5AhqgUaDR9MbpqJ9T11oOZXSNtYUVs/dn1igHSF/uR Al1ahd1lHsk8sUL5jv5UR0OlIz1X97eONmQegpKxOm8ACnQODi9PP2To/U6muIXFR64U WFHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=KFk22u3Ue13+BtokiWtJBeLbdvy8QJsGgG64cjwN58g=; b=IbgIfMbJmzbmBTtukrRO38UWf+vSJdAJyR9f3WOdNlX/Vbj4aOIS/569YLEFEXbJsf 8tpH3iOyx5TAf2m9+jb0KH0ePN1i/VIHhVnrcn1sXGcpH19J130dSYCeNZs/rJWoCjDp FVVeU9hvMPK6omXTwrImehGcqOcCxu8phQ+cUGM8YB1RJszk1q0qjwQ1wQZ1FIop0Uw+ 9PHrrkUsNnDSrnxsd/ipAnue8tCmHA0iPuMd22+yFo5wjiHHCjLmuR7W0JIJZ3/5O/fR MYJMOEnTZpuc+tdp5MHTQh2B/dzsvqqC9c1HLcExXGdPgoYuo4evGTlXvB4YUo3gQyJU uPJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=loQ2l8wt; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id be6-20020a0564021a2600b004add56ba930si9239115edb.461.2023.03.05.19.04.29; Sun, 05 Mar 2023 19:04:53 -0800 (PST) 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=loQ2l8wt; 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 S229628AbjCFC7V (ORCPT + 99 others); Sun, 5 Mar 2023 21:59:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229579AbjCFC7S (ORCPT ); Sun, 5 Mar 2023 21:59:18 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E479110419 for ; Sun, 5 Mar 2023 18:59:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678071557; x=1709607557; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ubDKJPz7LI3eEIbVA/X1FG8UptdiqBziq+folsggKew=; b=loQ2l8wtHeRqOZ1dTE+oFp22bw+gX9/iTOAgkcgjMiaF8pNTaoUYsDel hDTUXE7udma+Tyvvz+QcKxlXPxtEclqVyYkfVPNBqU9FzXcuFYtt10XYn nW+y8keBRUZ5vf1ftEVNACWjspMg1qynw/gVQCu9DYDqAjCaKQcXD1TUL /wSZPYbcbbJrG/K1bAQByK3eniEP3rmDtb6j1EqXnlStfx9Cux/ioQ8Jm 8CyfUr2KrYHKB9tU2pZE4ml7akDFPJH2Hkk2AJhgxrih3V0vDCed5+agm vj+lNzY6fIqKTPvbiGzT+z2QmI2wooaC9/scGtpdWS6RbJgpTHT+FVHBc w==; X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="363071480" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="363071480" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2023 18:59:17 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="765072469" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="765072469" Received: from allen-box.sh.intel.com ([10.239.159.48]) by FMSMGA003.fm.intel.com with ESMTP; 05 Mar 2023 18:59:15 -0800 From: Lu Baolu To: iommu@lists.linux.dev Cc: Joerg Roedel , Jason Gunthorpe , Christoph Hellwig , Kevin Tian , Will Deacon , Robin Murphy , linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v3 1/6] ARM/dma-mapping: Add arm_iommu_release_device() Date: Mon, 6 Mar 2023 10:57:59 +0800 Message-Id: <20230306025804.13912-2-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230306025804.13912-1-baolu.lu@linux.intel.com> References: <20230306025804.13912-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759585913626481287?= X-GMAIL-MSGID: =?utf-8?q?1759585913626481287?= It is like arm_iommu_detach_device() except it handles the special case of being called under an iommu driver's release operation. In this case the driver must have already detached the device from any attached domain before calling this function. Replace arm_iommu_detach_device() with arm_iommu_release_device() in the release path of the ipmmu-vmsa driver. The bonus is that it also removes a obstacle of arm_iommu_detach_device() re-entering the iommu core during release_device. With this removed, the iommu core code could be simplified a lot. Signed-off-by: Jason Gunthorpe Signed-off-by: Lu Baolu Reviewed-by: Jason Gunthorpe --- arch/arm/include/asm/dma-iommu.h | 1 + arch/arm/mm/dma-mapping.c | 25 +++++++++++++++++++++++++ drivers/iommu/ipmmu-vmsa.c | 15 +++++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/arch/arm/include/asm/dma-iommu.h b/arch/arm/include/asm/dma-iommu.h index fe9ef6f79e9c..ea7198a17861 100644 --- a/arch/arm/include/asm/dma-iommu.h +++ b/arch/arm/include/asm/dma-iommu.h @@ -31,6 +31,7 @@ void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping); int arm_iommu_attach_device(struct device *dev, struct dma_iommu_mapping *mapping); void arm_iommu_detach_device(struct device *dev); +void arm_iommu_release_device(struct device *dev); #endif /* __KERNEL__ */ #endif diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c index 8bc01071474a..96fa27f4a164 100644 --- a/arch/arm/mm/dma-mapping.c +++ b/arch/arm/mm/dma-mapping.c @@ -1682,6 +1682,31 @@ int arm_iommu_attach_device(struct device *dev, } EXPORT_SYMBOL_GPL(arm_iommu_attach_device); +/** + * arm_iommu_release_device + * @dev: valid struct device pointer + * + * This is like arm_iommu_detach_device() except it handles the special + * case of being called under an iommu driver's release operation. In this + * case the driver must have already detached the device from any attached + * domain before calling this function. + */ +void arm_iommu_release_device(struct device *dev) +{ + struct dma_iommu_mapping *mapping; + + mapping = to_dma_iommu_mapping(dev); + if (!mapping) { + dev_warn(dev, "Not attached\n"); + return; + } + + kref_put(&mapping->kref, release_iommu_mapping); + to_dma_iommu_mapping(dev) = NULL; + set_dma_ops(dev, NULL); +} +EXPORT_SYMBOL_GPL(arm_iommu_release_device); + /** * arm_iommu_detach_device * @dev: valid struct device pointer diff --git a/drivers/iommu/ipmmu-vmsa.c b/drivers/iommu/ipmmu-vmsa.c index bdf1a4e5eae0..de9c74cf61a4 100644 --- a/drivers/iommu/ipmmu-vmsa.c +++ b/drivers/iommu/ipmmu-vmsa.c @@ -30,7 +30,7 @@ #define arm_iommu_create_mapping(...) NULL #define arm_iommu_attach_device(...) -ENODEV #define arm_iommu_release_mapping(...) do {} while (0) -#define arm_iommu_detach_device(...) do {} while (0) +#define arm_iommu_release_device(...) do {} while (0) #endif #define IPMMU_CTX_MAX 16U @@ -820,7 +820,18 @@ static void ipmmu_probe_finalize(struct device *dev) static void ipmmu_release_device(struct device *dev) { - arm_iommu_detach_device(dev); + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); + struct ipmmu_vmsa_device *mmu = to_ipmmu(dev); + unsigned int i; + + for (i = 0; i < fwspec->num_ids; ++i) { + unsigned int utlb = fwspec->ids[i]; + + ipmmu_imuctr_write(mmu, utlb, 0); + mmu->utlb_ctx[utlb] = IPMMU_CTX_INVALID; + } + + arm_iommu_release_device(dev); } static struct iommu_group *ipmmu_find_group(struct device *dev) From patchwork Mon Mar 6 02:58:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 64395 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1631331wrd; Sun, 5 Mar 2023 19:04:05 -0800 (PST) X-Google-Smtp-Source: AK7set87wU5x/1NfPZrQoFRJ6pmjskoHw9QupwCkksAr2E9s4SuiV15XkUNbugw+Nra/uZhoaPIR X-Received: by 2002:a17:906:175b:b0:888:db6b:5fa9 with SMTP id d27-20020a170906175b00b00888db6b5fa9mr9653452eje.67.1678071845562; Sun, 05 Mar 2023 19:04:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678071845; cv=none; d=google.com; s=arc-20160816; b=PCbLMSBPaxePyhE8uC09vqd+pbMNUyEcH6AHEbLIQ0QE7Udz+CXWwVT+raxt8QsZdu z9f9ScBgk0X5zm85KB1wTvWP+LExBrYf9hI4+3A7oDwOXU9FuKj3zRPtvB1Bo+Cu0VWH JlXe5qgB5XqTugMUucE7X1KVOq7I72sOz7iXKY5bwKK5D+iWTV8MVGYanmpl6lqLNdEk ITY2XBz4TkM9EWFop7tXm1oMnAxBIQ/M7RrZ65wB8l7tfFCskS3D6TLAOogYJoFa6uLw hwI/tOx/zOlEZ9GWsRFu4peNbToxNFP3pkueaZK5shTg07lToAWpOwV/MbrzFa0lqvAR 6aoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oZSe5PlHo4OAV8Zfw2VQNdcwC9YPMgZ4U9CIZDxGowc=; b=wLCiTR/wfZ9ywlbcQq9W6jKxcJZE2Pgtu9AUAY/JPrHOH5h56nq4uuMJD9RGXT+jbx pWnXmiu7R9g+p9y3C0DACalKDuPI1dDnlD5hn066b+crVcE2gVudZv2cwygzA9Kt2wAk EE1QHXOT7db7ATm4Ld75XmwVrpof8bDusUqZtCEEEVsH6ViiRX2LYBUp2i4t8qwyvSit 2fjF1p6a+HHx2n4KChfWUvor/N6ulspKNY/S1tTo1xqy1AFznTfQy0YQDSWFsP5XT6u/ 22JtL3VBhkwkIdBVGiWc6BouB+4zC935q43lg8S14PG3iH/E7fhxcclIF9PotjKIAey5 4qXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=X72YTi6z; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j17-20020aa7c0d1000000b004d198125108si6915076edp.358.2023.03.05.19.03.39; Sun, 05 Mar 2023 19:04:05 -0800 (PST) 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=X72YTi6z; 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 S229658AbjCFC7Z (ORCPT + 99 others); Sun, 5 Mar 2023 21:59:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229600AbjCFC7V (ORCPT ); Sun, 5 Mar 2023 21:59:21 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 617C81165B for ; Sun, 5 Mar 2023 18:59:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678071560; x=1709607560; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=r1yKdk6UOO9yO9Ew/Jiy3oqCAhgo4LhD3eUDU5kx0jM=; b=X72YTi6zFCO9TMth5sEwh5m4Np/LDHk3q9Mq3BvHlJo++2+mKA5p00r4 n0jIXkZn9kEkYC25nhWkVbLirvf1bfK/Bs8D3h4MHsg8r4WCvLdCD+bZe RBHI6HYI2lJ40eUwjfgaF7ATBWl/th3ShkqLbzE9we9+WRKMFbQXslPg8 BK5O8v1/NSyOZ0qqdlnlMq0FTXvR7APccetXZ6li24yl3Xq93Q+V1aDz8 2r5TXP0FxcpLbgTtQajPhAPShU+rqdSpHzSVEJHJKtq9Wv1zmUTskm81v T5M3PYQckj5S6MtTAnDH8Vv0nVDteJ63XGWLDl2aXnpeGSt3mBkVNHKO4 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="363071488" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="363071488" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2023 18:59:20 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="765072474" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="765072474" Received: from allen-box.sh.intel.com ([10.239.159.48]) by FMSMGA003.fm.intel.com with ESMTP; 05 Mar 2023 18:59:17 -0800 From: Lu Baolu To: iommu@lists.linux.dev Cc: Joerg Roedel , Jason Gunthorpe , Christoph Hellwig , Kevin Tian , Will Deacon , Robin Murphy , linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v3 2/6] iommu: Split iommu_group_remove_device() into helpers Date: Mon, 6 Mar 2023 10:58:00 +0800 Message-Id: <20230306025804.13912-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230306025804.13912-1-baolu.lu@linux.intel.com> References: <20230306025804.13912-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759585863753584767?= X-GMAIL-MSGID: =?utf-8?q?1759585863753584767?= So that code could be re-used by iommu_release_device() in the subsequent change. No intention for functionality change. Signed-off-by: Jason Gunthorpe Signed-off-by: Lu Baolu Reviewed-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 64 +++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 80211bf08c0d..bd9b293e07a8 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -465,6 +465,46 @@ int iommu_probe_device(struct device *dev) } +/* + * Remove a device from a group's device list and return the group device + * if successful. + */ +static struct group_device * +__iommu_group_remove_device(struct iommu_group *group, struct device *dev) +{ + struct group_device *device; + + lockdep_assert_held(&group->mutex); + list_for_each_entry(device, &group->devices, list) { + if (device->dev == dev) { + list_del(&device->list); + return device; + } + } + + return NULL; +} + +/* + * Release a device from its group and decrements the iommu group reference + * count. + */ +static void __iommu_group_release_device(struct iommu_group *group, + struct group_device *grp_dev) +{ + struct device *dev = grp_dev->dev; + + sysfs_remove_link(group->devices_kobj, grp_dev->name); + sysfs_remove_link(&dev->kobj, "iommu_group"); + + trace_remove_device_from_group(group->id, dev); + + kfree(grp_dev->name); + kfree(grp_dev); + dev->iommu_group = NULL; + kobject_put(group->devices_kobj); +} + void iommu_release_device(struct device *dev) { const struct iommu_ops *ops; @@ -1080,7 +1120,7 @@ EXPORT_SYMBOL_GPL(iommu_group_add_device); void iommu_group_remove_device(struct device *dev) { struct iommu_group *group = dev->iommu_group; - struct group_device *tmp_device, *device = NULL; + struct group_device *device; if (!group) return; @@ -1088,27 +1128,11 @@ void iommu_group_remove_device(struct device *dev) dev_info(dev, "Removing from iommu group %d\n", group->id); mutex_lock(&group->mutex); - list_for_each_entry(tmp_device, &group->devices, list) { - if (tmp_device->dev == dev) { - device = tmp_device; - list_del(&device->list); - break; - } - } + device = __iommu_group_remove_device(group, dev); mutex_unlock(&group->mutex); - if (!device) - return; - - sysfs_remove_link(group->devices_kobj, device->name); - sysfs_remove_link(&dev->kobj, "iommu_group"); - - trace_remove_device_from_group(group->id, dev); - - kfree(device->name); - kfree(device); - dev->iommu_group = NULL; - kobject_put(group->devices_kobj); + if (device) + __iommu_group_release_device(group, device); } EXPORT_SYMBOL_GPL(iommu_group_remove_device); From patchwork Mon Mar 6 02:58:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 64394 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1629766wrd; Sun, 5 Mar 2023 19:00:44 -0800 (PST) X-Google-Smtp-Source: AK7set94tU7oxvm7QWH06zyVAnhhdrqtlpGrsOPPZ96VWLCEvcOzZvCkzMe7Z+IxbYKbFoU+DVI/ X-Received: by 2002:a05:6a20:8f09:b0:cb:cfb1:5009 with SMTP id b9-20020a056a208f0900b000cbcfb15009mr13017790pzk.34.1678071644649; Sun, 05 Mar 2023 19:00:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678071644; cv=none; d=google.com; s=arc-20160816; b=z4SYODx4m0C6yErU6jm/oOUhcsk9WLO8ggXfclJVUFx8udxpRA2OzSwgiNktCiKpc7 dk2bpl/WneQmZvYaEIOogAzXVuax2oP+y5PXQAdDmuwIInqQq3Tds9mS/ezvnSElOJiQ oKdrC11w+y4B512ZNLE8EYHD7QwGNUMVcd1u06ySQ+z2KnZG55mQ/8AhDJqBhAGV9wNh UE2bst8uHAdLaflW0Z07hGA2O6nz6xjgxRWoXm32bVUXWLmtnP7UPNh6PLcsevsmtN2Q 9XMSCeh+97OrkumM1Je35ASrVRPfaMrbzi92R7DSkacWTEt1oarrxHk8NydoGMT6B+ob sZ0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=E2/CaviEcysBW/Jp4/aLR57bA/iZCyi+RXEmnBIQSqM=; b=wfbgVlIplGtp4lcO6A8c9dXqS7h3WnVt9bJoRvhhwsWLFs5xyMEyaL2IfvApoeFY5z EeUkOArzCvxLa/y6Qrt5FB+RWntmikX8rvSYm7jjs4R3H9FF+vV3HOCCBIScJN5e+kT4 nGasYfGX/1dQjB2fAE/54xt/5d67OQZLEODuoejOjGCwWNc2iC2J/UiYM8jYMSJvRZKa AxnRMrSuwHJqS5VFJuWMV6SH9EWO6AwirlrrQh94hDTGyd1dLSEGmaRweOzbsHowkair q33XmNwTgZc9LrdOb77KPPLeFRR7sx3G7cAzRtsLD5kKMFPg8wAfQffceGOL1NLgOr7c wJBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=J0+weIw6; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h129-20020a636c87000000b004fbd23110d4si7694374pgc.773.2023.03.05.19.00.32; Sun, 05 Mar 2023 19:00:44 -0800 (PST) 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=J0+weIw6; 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 S229653AbjCFC72 (ORCPT + 99 others); Sun, 5 Mar 2023 21:59:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229591AbjCFC7X (ORCPT ); Sun, 5 Mar 2023 21:59:23 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2F871114C for ; Sun, 5 Mar 2023 18:59:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678071562; x=1709607562; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=23OLppNDX/QUue37RTrJRL+8G3hSt1KtGJZMXHrLeEU=; b=J0+weIw6Mci7I/ufJQBR/7s3vJ+jZYM32NCgzjSl6TN2jE8mvQgZ8Qpj LHh6V6c8K+v1mVXZH8yb8VXofw/V8NKv/7virFc+avbZq50m8g3Q34awR o2xwiZkyaM90FTKipEr5xW33gt0h91yFLXK+GNs2DBsCXunNt9re710ub a+0DY+18bsXsivsI/AZQeLq0/nJWqqCQnpYI8thvO3ngeS0nCvV2gYsC8 xpxC91gFvThvZQyGyLKQeYDss8e8/55lAjIIRNVqbuZd9KQH3N7YQkUzF ya/M/u8qLUwPKPIJ05jKp1YO9jwggI8zJ5RL+wHNYifV3DoJ/2aCMOPbh g==; X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="363071513" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="363071513" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2023 18:59:22 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="765072478" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="765072478" Received: from allen-box.sh.intel.com ([10.239.159.48]) by FMSMGA003.fm.intel.com with ESMTP; 05 Mar 2023 18:59:20 -0800 From: Lu Baolu To: iommu@lists.linux.dev Cc: Joerg Roedel , Jason Gunthorpe , Christoph Hellwig , Kevin Tian , Will Deacon , Robin Murphy , linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v3 3/6] iommu: Same critical region for device release and removal Date: Mon, 6 Mar 2023 10:58:01 +0800 Message-Id: <20230306025804.13912-4-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230306025804.13912-1-baolu.lu@linux.intel.com> References: <20230306025804.13912-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759585652476580887?= X-GMAIL-MSGID: =?utf-8?q?1759585652476580887?= In a non-driver context, it is crucial to ensure the consistency of a device's iommu ops. Otherwise, it may result in a situation where a device is released but it's iommu ops are still used. Put the ops->release_device and __iommu_group_remove_device() in a some group->mutext critical region, so that, as long as group->mutex is held and the device is in its group's device list, its iommu ops are always consistent. Add check of group ownership if the released device is the last one. Signed-off-by: Jason Gunthorpe Signed-off-by: Lu Baolu Reviewed-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index bd9b293e07a8..0bcd9625090d 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -507,18 +507,44 @@ static void __iommu_group_release_device(struct iommu_group *group, void iommu_release_device(struct device *dev) { + struct iommu_group *group = dev->iommu_group; + struct group_device *device; const struct iommu_ops *ops; - if (!dev->iommu) + if (!dev->iommu || !group) return; iommu_device_unlink(dev->iommu->iommu_dev, dev); + mutex_lock(&group->mutex); + device = __iommu_group_remove_device(group, dev); + + /* + * If the group has become empty then ownership must have been released, + * and the current domain must be set back to NULL or the default + * domain. + */ + if (list_empty(&group->devices)) + WARN_ON(group->owner_cnt || + group->domain != group->default_domain); + + /* + * release_device() must stop using any attached domain on the device. + * If there are still other devices in the group they are not effected + * by this callback. + * + * The IOMMU driver must set the device to either an identity or + * blocking translation and stop using any domain pointer, as it is + * going to be freed. + */ ops = dev_iommu_ops(dev); if (ops->release_device) ops->release_device(dev); + mutex_unlock(&group->mutex); + + if (device) + __iommu_group_release_device(group, device); - iommu_group_remove_device(dev); module_put(ops->owner); dev_iommu_free(dev); } From patchwork Mon Mar 6 02:58:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 64404 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1635374wrd; Sun, 5 Mar 2023 19:16:33 -0800 (PST) X-Google-Smtp-Source: AK7set+liW2l/5dx5rvh8rVG9wS0/eeIauQK0KXFFKSbRomrilLY5mp3UELj2Uy22nM/davDBvzp X-Received: by 2002:a62:4e06:0:b0:5a8:aca5:817d with SMTP id c6-20020a624e06000000b005a8aca5817dmr8855581pfb.5.1678072593012; Sun, 05 Mar 2023 19:16:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678072592; cv=none; d=google.com; s=arc-20160816; b=iqL5peeEj2f9ZD5w1XqscnsovwuPpvqPiQdtArUO/TNRYY6901Io8b+LfUzwhysuZ2 URePiawIyRAU+8Bq+fb4ps3PatcEcNhA3biYGdtpyfxSwtb9RKIqbDMBc0X8VFfm8IOj iP1xzoeJ42eigPC77YpxzNUuEOsJkcjvdtY16p7iYsf56655Z1VevfqW/CVXB/FF/vEh 6J7ivRuKHQ9c0+H6SG6K4wQmTZlPq+YkI7urda1A4+XyIEw9vHqSFO+EPBm4YkoD2J2B XYTU83VXHqo3LT+Yg0lawzZChtxjSZCBd9c2U1ZAl5izJzmDBvTxRbnVW8yYZid2UGr0 pdqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=HCksJop8pcHSLTajudJpz6wThpWXMZn1RUUw5XVz04A=; b=ORCJ+uqlW4TxBfG5SnkyOIs6pLinGSqXGt3vmDMluVSppj8W5pS2n06O0SlSEw0OIy ZSNfTTccWqHUOhLAX8ZzWgEZJo73TuejiGSWYmLq9uogECwptswPnBC+DAdGgx+oy0NQ WgQYpOGc9/HjaEWl50RogQF5zbUV/uj7SOJadwNNvih5GNAjO10FU5dX91e2TL5HXHhk vK5pVU5eheMFhs8xIKkY124HQ046Nuw7Mp6givimku1Ss8wJ2Y06a2Qn8rQ7FPiFn8WH V2KpHUi83Sfb5MujTZVHTYLAv142+XKnGcz3CiKuJGgNjz+mAZzBE+gByCkkqNa08LhD S4Ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JFVSLHH5; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a21-20020a631a55000000b0050318fe2b1bsi7928717pgm.147.2023.03.05.19.16.20; Sun, 05 Mar 2023 19:16:32 -0800 (PST) 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=JFVSLHH5; 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 S229695AbjCFC7d (ORCPT + 99 others); Sun, 5 Mar 2023 21:59:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229676AbjCFC71 (ORCPT ); Sun, 5 Mar 2023 21:59:27 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4548A11668 for ; Sun, 5 Mar 2023 18:59:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678071565; x=1709607565; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MdWuluD3W8nfCHmXoPEsni78ipVq2xDF2ne8PYskGyM=; b=JFVSLHH5v9MCBzXy40dN2rMYzu/Po924qnmOkJOUJmXpGxL/K/3otyVN EIM7f3bMDZzicpjwbAtb1VJaS8NhA+Op6ZAP5lEh5/TVQuCv/HJRFScYv IM6NK+SYMk9uujPtksR652+MSMZbI/AnaNHELWqAodHE5eeLhD8bD3gha KBqHxtgrtyXhv+OPO6OlA45aqyfE+HBL9M+2Dma9JdzUE845toBd1sp33 23b0VGQ2U+ku+w7nEfIv3+hxWNLJw778Pm0YPHqRh92MZBw8BCI86Xvfj 9uXixdOK3ba0pcnUBBgSu2iToKgtVpZzIOYH18w6L0FEpNKX/7hj6pDaB Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="363071524" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="363071524" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2023 18:59:25 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="765072484" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="765072484" Received: from allen-box.sh.intel.com ([10.239.159.48]) by FMSMGA003.fm.intel.com with ESMTP; 05 Mar 2023 18:59:22 -0800 From: Lu Baolu To: iommu@lists.linux.dev Cc: Joerg Roedel , Jason Gunthorpe , Christoph Hellwig , Kevin Tian , Will Deacon , Robin Murphy , linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v3 4/6] iommu: Move lock from iommu_change_dev_def_domain() to its caller Date: Mon, 6 Mar 2023 10:58:02 +0800 Message-Id: <20230306025804.13912-5-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230306025804.13912-1-baolu.lu@linux.intel.com> References: <20230306025804.13912-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759586647374441766?= X-GMAIL-MSGID: =?utf-8?q?1759586647374441766?= The intention is to make it possible to put group ownership check and default domain change in a same critical region protected by the group's mutex lock. No intentional functional change. Signed-off-by: Lu Baolu Reviewed-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 0bcd9625090d..f8f400548a10 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2945,7 +2945,7 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, int ret, dev_def_dom; struct device *dev; - mutex_lock(&group->mutex); + lockdep_assert_held(&group->mutex); if (group->default_domain != group->domain) { dev_err_ratelimited(prev_dev, "Group not assigned to default domain\n"); @@ -3033,28 +3033,15 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, goto free_new_domain; group->domain = group->default_domain; - - /* - * Release the mutex here because ops->probe_finalize() call-back of - * some vendor IOMMU drivers calls arm_iommu_attach_device() which - * in-turn might call back into IOMMU core code, where it tries to take - * group->mutex, resulting in a deadlock. - */ - mutex_unlock(&group->mutex); - - /* Make sure dma_ops is appropriatley set */ - iommu_group_do_probe_finalize(dev, group->default_domain); iommu_domain_free(prev_dom); + return 0; free_new_domain: iommu_domain_free(group->default_domain); group->default_domain = prev_dom; group->domain = prev_dom; - out: - mutex_unlock(&group->mutex); - return ret; } @@ -3142,7 +3129,19 @@ static ssize_t iommu_group_store_type(struct iommu_group *group, goto out; } + mutex_lock(&group->mutex); ret = iommu_change_dev_def_domain(group, dev, req_type); + /* + * Release the mutex here because ops->probe_finalize() call-back of + * some vendor IOMMU drivers calls arm_iommu_attach_device() which + * in-turn might call back into IOMMU core code, where it tries to take + * group->mutex, resulting in a deadlock. + */ + mutex_unlock(&group->mutex); + + /* Make sure dma_ops is appropriatley set */ + if (!ret) + iommu_group_do_probe_finalize(dev, group->default_domain); ret = ret ?: count; out: From patchwork Mon Mar 6 02:58:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 64421 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1647082wrd; Sun, 5 Mar 2023 20:01:54 -0800 (PST) X-Google-Smtp-Source: AK7set+VzGWdwCh1P82EwMPcw4hZapr54GGB8PAyILETNAP3qit2QsBM7fqLc6bAUPBnSIzV+TaL X-Received: by 2002:a05:6a20:2448:b0:cc:c3f7:916b with SMTP id t8-20020a056a20244800b000ccc3f7916bmr9429613pzc.0.1678075314421; Sun, 05 Mar 2023 20:01:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678075314; cv=none; d=google.com; s=arc-20160816; b=Bhnh6zA8PLPQstaftROBvEXB+L1xaWekeHfo6eQrCGuW1rspYWRHc6ttZfg9fkwYFF I3mABkxkA3KLrHyf9AOxKzvDRAghQ1/9CDcOJfzJCxnFqneMEVRb3yusIP8nkDpgvrcR DCld4ak6UMLLs/URC+6Ja0wl96Katg7UlWzmCS/oH/7ZHBLderemCYWWUoQy/mbRr2j6 1PIrIt8bTxZAaa38pKDZBQxihq68C5CQZNej2xv7m6eaLibkOAyWn0eyj8pQJIol/2nN fWZsO2uH0TGqCbMU9ZoN+N/YZSLVZAOP+4+ZFKIfaN6xHq46EFhCyBCMyZaoegKf9SK0 PQmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=vSjGnt7Qy4rn2LVpJdBg4suKULDmIncVi1Duk8jaY4g=; b=FXy4i/iIg5bPzGGjKkBXiwQrXUsqh32yVGZQDNuDsZHUgCAR0kiHD/eO27FAB2BBvn yvANptrw+mQAB7FHiVvY4thCqg9oVgPGM4oMHAKqUscLVgrpEsu87dfzPumDCjdqF9qi eagAH6I2jIziHawcP3ew3ojHXTZdIEI6f7wJ/enulPJ7xdhDQQWeCKeDTzYyy1qsUwsR AKgIn4boyb6G/LOi45q+9NVTjiP03F9pYDky1pXNNpeirmm+4QKKdFgEwwlY+z50i2bz Sqf6hVub2cQ9fO6bR3JHDxBQCBzzH9Z8Q8HuOoBgfwTAhGZXTFclTZ6GsgphDGSjdCzm AtYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=aIMH4XtB; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r20-20020aa79634000000b0059242a657d9si8453085pfg.140.2023.03.05.20.01.42; Sun, 05 Mar 2023 20:01:54 -0800 (PST) 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=aIMH4XtB; 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 S229618AbjCFC7l (ORCPT + 99 others); Sun, 5 Mar 2023 21:59:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229687AbjCFC7a (ORCPT ); Sun, 5 Mar 2023 21:59:30 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD4C31286F for ; Sun, 5 Mar 2023 18:59:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678071567; x=1709607567; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=e5onQTPUwerLr2g9cgW3cFZ0Fra9mu+whj+aXFchN2A=; b=aIMH4XtB2Vjg52Vbj7Ee5xXusDUd3/4vGa96f+q9JC2HtgbaDszM9bn7 yDnT2hSXX7Ov0AoONFg8hFd4c9FdcL/K31BlnGEyTv/pMXJaqKINFussi fNdul/oNjGA9ZbQRxA4ieKiX4wQ/6UF1lgkcfOLtObynEwBwjJ2p/WcUm PV9MX/1ZfGAI2npdltIsM1JmeMlw61/4SKUdtFLBR6lY8AFv1b2oysd1s hyTUlG1XuwKDUjNuZlVkQ71h/xo3aFupGLxpS+XpjezChmHMVRqE7T9l1 lIPSxu5KCtFb+Y+7PXyXuihs6QNM4Ly60LXTF9FCYvRtkuLZXxRdQJOSB A==; X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="363071534" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="363071534" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2023 18:59:27 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="765072487" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="765072487" Received: from allen-box.sh.intel.com ([10.239.159.48]) by FMSMGA003.fm.intel.com with ESMTP; 05 Mar 2023 18:59:25 -0800 From: Lu Baolu To: iommu@lists.linux.dev Cc: Joerg Roedel , Jason Gunthorpe , Christoph Hellwig , Kevin Tian , Will Deacon , Robin Murphy , linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v3 5/6] iommu: Replace device_lock() with group->mutex Date: Mon, 6 Mar 2023 10:58:03 +0800 Message-Id: <20230306025804.13912-6-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230306025804.13912-1-baolu.lu@linux.intel.com> References: <20230306025804.13912-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759589500839731805?= X-GMAIL-MSGID: =?utf-8?q?1759589500839731805?= device_lock() was used in iommu_group_store_type() to prevent the devices in an iommu group from being attached by any device driver. On the other hand, in order to avoid lock race between group->mutex and device_lock(), it limited the usage scenario to the singleton groups. We already have the DMA ownership scheme to avoid driver attachment and group->mutex ensures that device ops are always valid, there's no need for device_lock() anymore. Remove device_lock() and the singleton group limitation. Signed-off-by: Lu Baolu Reviewed-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 78 +++++++++---------------------------------- 1 file changed, 16 insertions(+), 62 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index f8f400548a10..9fe6d149f281 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3011,14 +3011,6 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, goto out; } - /* We can bring up a flush queue without tearing down the domain */ - if (type == IOMMU_DOMAIN_DMA_FQ && prev_dom->type == IOMMU_DOMAIN_DMA) { - ret = iommu_dma_init_fq(prev_dom); - if (!ret) - prev_dom->type = IOMMU_DOMAIN_DMA_FQ; - goto out; - } - /* Sets group->default_domain to the newly allocated domain */ ret = iommu_group_alloc_default_domain(group, dev, type); if (ret) @@ -3051,7 +3043,7 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, * transition. Return failure if this isn't met. * * We need to consider the race between this and the device release path. - * device_lock(dev) is used here to guarantee that the device release path + * group->mutex is used here to guarantee that the device release path * will not be entered at the same time. */ static ssize_t iommu_group_store_type(struct iommu_group *group, @@ -3077,60 +3069,27 @@ static ssize_t iommu_group_store_type(struct iommu_group *group, else return -EINVAL; - /* - * Lock/Unlock the group mutex here before device lock to - * 1. Make sure that the iommu group has only one device (this is a - * prerequisite for step 2) - * 2. Get struct *dev which is needed to lock device - */ mutex_lock(&group->mutex); - if (iommu_group_device_count(group) != 1) { + /* We can bring up a flush queue without tearing down the domain. */ + if (req_type == IOMMU_DOMAIN_DMA_FQ && + group->default_domain->type == IOMMU_DOMAIN_DMA) { + ret = iommu_dma_init_fq(group->default_domain); + if (!ret) + group->default_domain->type = IOMMU_DOMAIN_DMA_FQ; mutex_unlock(&group->mutex); - pr_err_ratelimited("Cannot change default domain: Group has more than one device\n"); - return -EINVAL; + + return ret ?: count; } - /* Since group has only one device */ + /* Otherwise, ensure that device exists and no driver is bound. */ dev = iommu_group_first_dev(group); - get_device(dev); - - /* - * Don't hold the group mutex because taking group mutex first and then - * the device lock could potentially cause a deadlock as below. Assume - * two threads T1 and T2. T1 is trying to change default domain of an - * iommu group and T2 is trying to hot unplug a device or release [1] VF - * of a PCIe device which is in the same iommu group. T1 takes group - * mutex and before it could take device lock assume T2 has taken device - * lock and is yet to take group mutex. Now, both the threads will be - * waiting for the other thread to release lock. Below, lock order was - * suggested. - * device_lock(dev); - * mutex_lock(&group->mutex); - * iommu_change_dev_def_domain(); - * mutex_unlock(&group->mutex); - * device_unlock(dev); - * - * [1] Typical device release path - * device_lock() from device/driver core code - * -> bus_notifier() - * -> iommu_bus_notifier() - * -> iommu_release_device() - * -> ops->release_device() vendor driver calls back iommu core code - * -> mutex_lock() from iommu core code - */ - mutex_unlock(&group->mutex); - - /* Check if the device in the group still has a driver bound to it */ - device_lock(dev); - if (device_is_bound(dev) && !(req_type == IOMMU_DOMAIN_DMA_FQ && - group->default_domain->type == IOMMU_DOMAIN_DMA)) { - pr_err_ratelimited("Device is still bound to driver\n"); - ret = -EBUSY; - goto out; + if (!dev || group->owner_cnt) { + mutex_unlock(&group->mutex); + return -EPERM; } - mutex_lock(&group->mutex); ret = iommu_change_dev_def_domain(group, dev, req_type); + /* * Release the mutex here because ops->probe_finalize() call-back of * some vendor IOMMU drivers calls arm_iommu_attach_device() which @@ -3141,14 +3100,9 @@ static ssize_t iommu_group_store_type(struct iommu_group *group, /* Make sure dma_ops is appropriatley set */ if (!ret) - iommu_group_do_probe_finalize(dev, group->default_domain); - ret = ret ?: count; - -out: - device_unlock(dev); - put_device(dev); + __iommu_group_dma_finalize(group); - return ret; + return ret ?: count; } static bool iommu_is_default_domain(struct iommu_group *group) From patchwork Mon Mar 6 02:58:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 64397 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1631677wrd; Sun, 5 Mar 2023 19:05:04 -0800 (PST) X-Google-Smtp-Source: AK7set/gYVxoEHep9KDpDk+OOw0zq+/+c8Cr5rAFQjPIohlzmXrAX5Y8O3uuY6r2xsQZK0/fi0TL X-Received: by 2002:aa7:d8d0:0:b0:4af:5a0f:ba34 with SMTP id k16-20020aa7d8d0000000b004af5a0fba34mr8884338eds.26.1678071904657; Sun, 05 Mar 2023 19:05:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678071904; cv=none; d=google.com; s=arc-20160816; b=dXcjEgZrjbfM6p/sgobqHk+HIM6/OIHGPzXr7VwSK5mnNxwdSy4T3p+XBNeyLAl60e 1mJRiNixTYp+RzqRIQn0zwJ+PPvJTTYPu5WKfB0WqVk49ec/zgX+eXroMnBubonkH+Be FveAtjOKGVBOdu7gs6k4Uu0hyrJGe8k+RPwQKkLh/GX4KOGFSc6rYKvt4R0kqfKI3nPk LonA2k5xlBtMZid+b2os8PsEvJVwKw/gKTx+nFDtb3IxybXvnYTcgr1sOUcIvXiHftx9 /wayJUxNjOXdWh/q/YhTIqEAdxItbsQQOm627kUkimIpqYNdMdOo9Gu4t/7tFRRpuubZ frBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=6/g5Jmo/QemLuLylzLku5DD6RxzsKCZrNdEpJD3JqIs=; b=euGtu7m3AeRgLReViBNCbgQggVcevkbwb29d8Enh0WLd6o+t8Su/VZp/kInqMxqH2F qGY/7t4Wu6f1ruQMR0SOLF7pwU1YgfVY2yECWl0R+cmK4CRjKZMViZiZhXtlOBagKtw8 RTEfrI+LiMkrb9JIyVZDji/hy7CxaPXWaMy5Nf5Ryiny6ndsncTwFnYmbFfeyo658m5h wMYFOf3f39XV8gc8B3+HS5NeRO1nQNM3TVxQ0dPVX4zVdXbnzG+yvsO02IYUJ4ZrNNbA WdF447JRXnx5jlQ98ICRACw179HvsszKP9dHaGLmkUQJyg5wuTklzfa3hqitsFaU7s4E ToCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mAtgoytf; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z22-20020aa7c656000000b004c0da1cf02dsi9556927edr.105.2023.03.05.19.04.41; Sun, 05 Mar 2023 19:05:04 -0800 (PST) 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=mAtgoytf; 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 S229667AbjCFC7w (ORCPT + 99 others); Sun, 5 Mar 2023 21:59:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229709AbjCFC7j (ORCPT ); Sun, 5 Mar 2023 21:59:39 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AEE912069 for ; Sun, 5 Mar 2023 18:59:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678071570; x=1709607570; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Taiw2o2chkLGjTGNeAmtwqU/UhEE9L3oW9AzQ9w2d3I=; b=mAtgoytfVraoykZ6qVCf+TvF0IPuLGw/txjzRvN9GMVa0YVXwtQIM2AB X1XvLgvuS/oEwOgEMgOoruQeV+Duc7D34CTZWZHbPCPSH52d+Mndgt/vt jjcpLXFNK4I9Hf2JVqOpOLqJiJHdTlimGljngkM2EkpK/G8WUNE1u3zHW wbrTT8rRdDQR3NZNSog324A1qMp4j5LH+Q4XBzhEBVpxT+4u7xQcmjh6r qCcHsd5yJ/COq0y+dku6r3isu0SIym40aUAJcyqZnoBtukpDjmyUV/xjX G6vc0rutLwDFTF+RN44LRKWx/T6OpnLmnpHwkr1EL/JrbIjIH2DjaAwSr g==; X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="363071543" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="363071543" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2023 18:59:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10640"; a="765072490" X-IronPort-AV: E=Sophos;i="5.98,236,1673942400"; d="scan'208";a="765072490" Received: from allen-box.sh.intel.com ([10.239.159.48]) by FMSMGA003.fm.intel.com with ESMTP; 05 Mar 2023 18:59:27 -0800 From: Lu Baolu To: iommu@lists.linux.dev Cc: Joerg Roedel , Jason Gunthorpe , Christoph Hellwig , Kevin Tian , Will Deacon , Robin Murphy , linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v3 6/6] iommu: Cleanup iommu_change_dev_def_domain() Date: Mon, 6 Mar 2023 10:58:04 +0800 Message-Id: <20230306025804.13912-7-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230306025804.13912-1-baolu.lu@linux.intel.com> References: <20230306025804.13912-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759585925545124408?= X-GMAIL-MSGID: =?utf-8?q?1759585925545124408?= As the singleton group limitation has been removed, cleanup the code in iommu_change_dev_def_domain() accordingly. Documentation is also updated. Signed-off-by: Lu Baolu Reviewed-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 81 +++++-------------- .../ABI/testing/sysfs-kernel-iommu_groups | 1 - 2 files changed, 21 insertions(+), 61 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 9fe6d149f281..735554ccc04b 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2924,11 +2924,10 @@ int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat) EXPORT_SYMBOL_GPL(iommu_dev_disable_feature); /* - * Changes the default domain of an iommu group that has *only* one device + * Changes the default domain of an iommu group * * @group: The group for which the default domain should be changed - * @prev_dev: The device in the group (this is used to make sure that the device - * hasn't changed after the caller has called this function) + * @dev: The first device in the group * @type: The type of the new default domain that gets associated with the group * * Returns 0 on success and error code on failure @@ -2939,101 +2938,63 @@ EXPORT_SYMBOL_GPL(iommu_dev_disable_feature); * Please take a closer look if intended to use for other purposes. */ static int iommu_change_dev_def_domain(struct iommu_group *group, - struct device *prev_dev, int type) + struct device *dev, int type) { + struct __group_domain_type gtype = {NULL, 0}; struct iommu_domain *prev_dom; - int ret, dev_def_dom; - struct device *dev; + int ret; lockdep_assert_held(&group->mutex); - if (group->default_domain != group->domain) { - dev_err_ratelimited(prev_dev, "Group not assigned to default domain\n"); - ret = -EBUSY; - goto out; - } - - /* - * iommu group wasn't locked while acquiring device lock in - * iommu_group_store_type(). So, make sure that the device count hasn't - * changed while acquiring device lock. - * - * Changing default domain of an iommu group with two or more devices - * isn't supported because there could be a potential deadlock. Consider - * the following scenario. T1 is trying to acquire device locks of all - * the devices in the group and before it could acquire all of them, - * there could be another thread T2 (from different sub-system and use - * case) that has already acquired some of the device locks and might be - * waiting for T1 to release other device locks. - */ - if (iommu_group_device_count(group) != 1) { - dev_err_ratelimited(prev_dev, "Cannot change default domain: Group has more than one device\n"); - ret = -EINVAL; - goto out; - } - - /* Since group has only one device */ - dev = iommu_group_first_dev(group); - - if (prev_dev != dev) { - dev_err_ratelimited(prev_dev, "Cannot change default domain: Device has been changed\n"); - ret = -EBUSY; - goto out; - } - prev_dom = group->default_domain; - if (!prev_dom) { - ret = -EINVAL; - goto out; - } - - dev_def_dom = iommu_get_def_domain_type(dev); + __iommu_group_for_each_dev(group, >ype, + probe_get_default_domain_type); if (!type) { /* * If the user hasn't requested any specific type of domain and * if the device supports both the domains, then default to the * domain the device was booted with */ - type = dev_def_dom ? : iommu_def_domain_type; - } else if (dev_def_dom && type != dev_def_dom) { - dev_err_ratelimited(prev_dev, "Device cannot be in %s domain\n", + type = gtype.type ? : iommu_def_domain_type; + } else if (gtype.type && type != gtype.type) { + dev_err_ratelimited(dev, "Device cannot be in %s domain\n", iommu_domain_type_str(type)); - ret = -EINVAL; - goto out; + return -EINVAL; } /* * Switch to a new domain only if the requested domain type is different * from the existing default domain type */ - if (prev_dom->type == type) { - ret = 0; - goto out; - } + if (prev_dom->type == type) + return 0; + + group->default_domain = NULL; + group->domain = NULL; /* Sets group->default_domain to the newly allocated domain */ ret = iommu_group_alloc_default_domain(group, dev, type); if (ret) - goto out; + goto restore_old_domain; - ret = iommu_create_device_direct_mappings(group, dev); + ret = iommu_group_create_direct_mappings(group); if (ret) goto free_new_domain; - ret = __iommu_attach_device(group->default_domain, dev); + ret = __iommu_attach_group(group->default_domain, group); if (ret) goto free_new_domain; - group->domain = group->default_domain; iommu_domain_free(prev_dom); return 0; free_new_domain: iommu_domain_free(group->default_domain); +restore_old_domain: group->default_domain = prev_dom; group->domain = prev_dom; -out: + return ret; } diff --git a/Documentation/ABI/testing/sysfs-kernel-iommu_groups b/Documentation/ABI/testing/sysfs-kernel-iommu_groups index b15af6a5bc08..a42d4383d999 100644 --- a/Documentation/ABI/testing/sysfs-kernel-iommu_groups +++ b/Documentation/ABI/testing/sysfs-kernel-iommu_groups @@ -53,7 +53,6 @@ Description: /sys/kernel/iommu_groups//type shows the type of default The default domain type of a group may be modified only when - - The group has only one device. - The device in the group is not bound to any device driver. So, the users must unbind the appropriate driver before changing the default domain type.