From nobody Thu Sep 11 18:24:15 2025 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 35406C05027 for ; Fri, 17 Feb 2023 09:56:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230223AbjBQJ4X (ORCPT ); Fri, 17 Feb 2023 04:56:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229950AbjBQJ4P (ORCPT ); Fri, 17 Feb 2023 04:56:15 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 761785F26D for ; Fri, 17 Feb 2023 01:56:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676627774; x=1708163774; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=d4ud28FUnLJpp84jaUtvJV6QcvsnVTvJ83pUv/Icsko=; b=l/a/eLMRDOuF+Z2hrozWDRCd33t83agHh7KXITgWApZvsZOtabGJ2UTp PFAloOlXW2j8RkqnPb56Wrzh35vQq9u0GpmV09O2sJpsJc2IitsBvN85H uUUxHdiKdFXcFVg1PRgFBtlmRWwG+ravDLV08vw17MepQUCIlz7JmGVcU yp64vg/30m5S6RkDQRribMUh1l/w+Gffxa2c9xAdXwlP0kjKolPsSapCw RJ7BU00SMM8MyDgtfPPx80Jb56jMgZyZ+tpNuLlJhotQB9KdTAJOOLXJX vI/WbT/tiOmrUV3rM+VDAc+IRu8eAP8R3ME5JqX5T0fINSvmGbNYNu/1b A==; X-IronPort-AV: E=McAfee;i="6500,9779,10623"; a="331955202" X-IronPort-AV: E=Sophos;i="5.97,304,1669104000"; d="scan'208";a="331955202" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Feb 2023 01:56:13 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10623"; a="999391218" X-IronPort-AV: E=Sophos;i="5.97,304,1669104000"; d="scan'208";a="999391218" Received: from allen-box.sh.intel.com ([10.239.159.48]) by fmsmga005.fm.intel.com with ESMTP; 17 Feb 2023 01:56:10 -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 v2 2/6] iommu: Split iommu_group_remove_device() into helpers Date: Fri, 17 Feb 2023 17:47:32 +0800 Message-Id: <20230217094736.159005-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230217094736.159005-1-baolu.lu@linux.intel.com> References: <20230217094736.159005-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" So that code could be re-used by iommu_release_device() in the subsequent change. No intention for functionality change. Signed-off-by: Lu Baolu --- 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 28c6b088aedd..6247883991e2 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1069,6 +1069,46 @@ int iommu_group_add_device(struct iommu_group *group= , struct device *dev) } EXPORT_SYMBOL_GPL(iommu_group_add_device); =20 +/* + * 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 =3D=3D 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 =3D 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 =3D NULL; + kobject_put(group->devices_kobj); +} + /** * iommu_group_remove_device - remove a device from it's current group * @dev: device to be removed @@ -1079,7 +1119,7 @@ EXPORT_SYMBOL_GPL(iommu_group_add_device); void iommu_group_remove_device(struct device *dev) { struct iommu_group *group =3D dev->iommu_group; - struct group_device *tmp_device, *device =3D NULL; + struct group_device *device; =20 if (!group) return; @@ -1087,27 +1127,11 @@ void iommu_group_remove_device(struct device *dev) dev_info(dev, "Removing from iommu group %d\n", group->id); =20 mutex_lock(&group->mutex); - list_for_each_entry(tmp_device, &group->devices, list) { - if (tmp_device->dev =3D=3D dev) { - device =3D tmp_device; - list_del(&device->list); - break; - } - } + device =3D __iommu_group_remove_device(group, dev); mutex_unlock(&group->mutex); =20 - 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 =3D NULL; - kobject_put(group->devices_kobj); + if (device) + __iommu_group_release_device(group, device); } EXPORT_SYMBOL_GPL(iommu_group_remove_device); =20 --=20 2.34.1