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);