Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5654558rwl; Tue, 21 Mar 2023 23:52:43 -0700 (PDT) X-Google-Smtp-Source: AK7set8ct54zBtU8ehpBihr7iPhWYJs5f2wgEhLxBv+xEglaizPkDT6hlaMU0KjtFWHXOMW2mdWe X-Received: by 2002:a17:906:4a0a:b0:91f:5f9c:5db6 with SMTP id w10-20020a1709064a0a00b0091f5f9c5db6mr6968431eju.52.1679467963038; Tue, 21 Mar 2023 23:52:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679467963; cv=none; d=google.com; s=arc-20160816; b=DTzcfBY6XpY5juI38smJO/8clGuccDT11IO8FvJpo+f1YWkAkUVH6SZbiZxCWp930s c53QzvV2WHv5kDyzT66UxsMCVq53dMMeoRns42M6lrRkBV3rPkJDyzBRoTbFWxj6lZIj uhrNz5iCSAuMcbPal/cOShIokCbGP5GKem/La/cyXFU/lTijdVJmTXg3m5RlcFAV5j/y gn/H63WvvoboTW2YfQbJTn0M9TTaQHL8gvCXRhoSYYiRtHnxy/G4dDboWT9+wXSUjP6+ 2tufrCtEl0tRpxZ7ABYKmh0oQ31lz70w/aihD1JZGM13tqbwiquHGFQUZ054SLWiVczu 6/+w== 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=K4178DzENXLaBrIKmy4InE+U+uc99Oq6WGMV/2rh7l4=; b=wmqnIScxIbYqh2JI5ytttlQVW8akN5mmyNubdY7HRw4+Ka5adA2Wtn3U1qBRGaNGxp nliQeSK5amtq+stxQCch2wfas76XUDqSxk9Cllue01+uyteh6NN5by312sS5dKJIU7Ec xSGOKCWy2/wEpf0XUKGcyiAxNFwF/kbNuVZ1AHjDQlcrB71zSQVHnbrqBHRZioHJZShc PUdauMxUP+VZ9BqGK6bKaGX+10S+KBQl9euGj/Kle3teZrnoAfZq+B73Fds6AaR5v6sc nwBlOxRlm0R614hs0xTVCN/QeourLcvuoO/QIprnIAJ0mu6aceAkRrcvsIrCRCd+Xpp3 x2Jg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nsUX9bYD; 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 lh6-20020a170906f8c600b0092419a141fbsi493392ejb.726.2023.03.21.23.52.18; Tue, 21 Mar 2023 23:52:43 -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=nsUX9bYD; 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 S229912AbjCVGtx (ORCPT + 99 others); Wed, 22 Mar 2023 02:49:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229917AbjCVGts (ORCPT ); Wed, 22 Mar 2023 02:49:48 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E61425E16 for ; Tue, 21 Mar 2023 23:49:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679467782; x=1711003782; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uNsJ5iL/12Ebvb4ULP39bZOjdt78s0sC7JK9D/nWeI8=; b=nsUX9bYD2DpX7olHGN04k+04liza5iIcQfq0BRWVcWWhhCq2CmWOXP16 OM2cFxwEihXAl5nRufdgKlSoKYqYQ5UiW3C7IiUKD3EMpQR6wcTCoxB+x 8QTCvOQEQrJf4COt5f3zQmjgDrdWPYriI1cQULiGLcwtvj7IMjK956871 ZKP8be7Ixiqi5o237sdILGvQWIfzRcvPj7udQr/l6jgZwzKVRK+SRC1mr 81MNRIZqXjOuoF+NIqSosgyo+siLs9kaBW452Z1R4iknzyAkWIpNpscFz hEOYLOE9KNH65T3Nz7Q27EgOzezOrQ0GH8/8vdB/nbsMzNbA/L46V3vii Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10656"; a="337866739" X-IronPort-AV: E=Sophos;i="5.98,281,1673942400"; d="scan'208";a="337866739" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2023 23:49:41 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10656"; a="659080394" X-IronPort-AV: E=Sophos;i="5.98,281,1673942400"; d="scan'208";a="659080394" Received: from allen-box.sh.intel.com ([10.239.159.48]) by orsmga006.jf.intel.com with ESMTP; 21 Mar 2023 23:49:39 -0700 From: Lu Baolu To: Joerg Roedel Cc: Jason Gunthorpe , Robin Murphy , Christoph Hellwig , Kevin Tian , Will Deacon , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v4 2/6] iommu: Split iommu_group_remove_device() into helpers Date: Wed, 22 Mar 2023 14:49:52 +0800 Message-Id: <20230322064956.263419-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230322064956.263419-1-baolu.lu@linux.intel.com> References: <20230322064956.263419-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE, URIBL_BLOCKED autolearn=unavailable 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 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 10db680acaed..43db48323370 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -453,6 +453,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; @@ -1068,7 +1108,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; @@ -1076,27 +1116,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); -- 2.34.1