From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D09BEEB64DA for ; Thu, 20 Jul 2023 14:18:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:To:Subject:Cc: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=PNEX7SvtaLm/fpjwMwOteYpQIAcPrCEUBDoLOXlXOYE=; b=TYS72nJHWOyaoN ibmiYvMLaSyEyUahdz8FyCF6x+G0GDCOJ24iVxPtCjSaKRSRPOBY60yJE9enY68DKZlCrDgrIh+d9 D61Hm0OnQQ2MiUQAMGxuvQcvvIetjKppcuBMRgj+9UJHlJ7S1826ppuLbzTaR7jBqTg70OBwyjaUd hxluKT/+9wdZ/qg3e9XCQY6ieGGd5+9CbnbMg/zkG7d+7dOzNiEQpxYgupYPB36M+EicabngZhMFf eFoCfNXP+3tpjfmvwCP+F/l+BRFbFnRNpA05H1lx9ncFBPt6f8+TvWj0y3sbIUCh+LZT4pOIql/Dd NadWeuMl9R6z5oshj97Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qMUUB-00BMoF-0V; Thu, 20 Jul 2023 14:18:31 +0000 Received: from mga09.intel.com ([134.134.136.24]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qMUU8-00BMnQ-1D; Thu, 20 Jul 2023 14:18:29 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689862708; x=1721398708; h=message-id:date:mime-version:cc:subject:to:references: from:in-reply-to:content-transfer-encoding; bh=BFG+lxSO1zBFfL9o8M5wrEKd1L4tJiPtfHsH/2e/ZoQ=; b=ivdWZPDRfj2LDHbw5IiIckVVrwK+kBM9otGgQnmqOJ9ZtTxV+nYpDHzw tI6mq0xM6eNSkxRxKXafHIOwZbvUheqFD/hIn2Hv8jR6zCo4cKK9tbZjy cWLnQL/Nq9Xcv5a8XHxy3FLtMSzTDMKx5Vbuv6+GqFR1nkmoHMbrKEw8j /z2vqqNrQI9xd6dqZ5DI5vgYKRTZtaq8F0YLWbtWBzJ59mqcndpf+HIE1 uxYUe2rhmzg6E/uTOdc+O5VG6pDinfdxe4vWsIkzkcHAQa7dXzawY9CBf 4q0k2cT/XTvgz2/BBfOrd4NS6STU9By/Ov4OaQxf909WM6LhAwZ0K5Vjo A==; X-IronPort-AV: E=McAfee;i="6600,9927,10777"; a="369414759" X-IronPort-AV: E=Sophos;i="6.01,219,1684825200"; d="scan'208";a="369414759" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jul 2023 07:02:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10777"; a="674703327" X-IronPort-AV: E=Sophos;i="6.01,219,1684825200"; d="scan'208";a="674703327" Received: from hli37-mobl.amr.corp.intel.com (HELO [10.252.191.120]) ([10.252.191.120]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jul 2023 07:01:58 -0700 Message-ID: Date: Thu, 20 Jul 2023 22:01:54 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Cc: baolu.lu@linux.intel.com, Baolin Wang , David Woodhouse , Heiko Stuebner , iommu@lists.linux.dev, Jernej Skrabec , Joerg Roedel , linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-sunxi@lists.linux.dev, Orson Zhai , Robin Murphy , Samuel Holland , Chen-Yu Tsai , Will Deacon , Chunyan Zhang , Alex Williamson Subject: Re: [PATCH 03/10] iommu: Add generic_single_device_group() To: Jason Gunthorpe References: <3-v1-3c8177327a47+256-iommu_group_locking_jgg@nvidia.com> <32eadc5b-bb39-5bb1-f124-44feead97ce9@linux.intel.com> Content-Language: en-US From: Baolu Lu In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230720_071828_548416_B34911CF X-CRM114-Status: GOOD ( 18.95 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 2023/7/20 20:04, Jason Gunthorpe wrote: > On Thu, Jul 20, 2023 at 03:39:27PM +0800, Baolu Lu wrote: >> On 2023/7/19 3:05, Jason Gunthorpe wrote: >>> This implements the common pattern seen in drivers of a single >>> iommu_group for the entire iommu driver. Implement this in core code >>> so the drivers that want this can select it from their ops. >>> >>> Signed-off-by: Jason Gunthorpe >>> --- >>> drivers/iommu/iommu.c | 25 +++++++++++++++++++++++++ >>> include/linux/iommu.h | 3 +++ >>> 2 files changed, 28 insertions(+) >>> >>> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c >>> index 9e41ad4e3219b6..1e0c5d9a0370fb 100644 >>> --- a/drivers/iommu/iommu.c >>> +++ b/drivers/iommu/iommu.c >>> @@ -289,6 +289,9 @@ void iommu_device_unregister(struct iommu_device *iommu) >>> spin_lock(&iommu_device_lock); >>> list_del(&iommu->list); >>> spin_unlock(&iommu_device_lock); >>> + >>> + /* Pairs with the alloc in generic_single_device_group() */ >>> + iommu_group_put(iommu->singleton_group); >>> } >>> EXPORT_SYMBOL_GPL(iommu_device_unregister); >>> @@ -1595,6 +1598,28 @@ struct iommu_group *generic_device_group(struct device *dev) >>> } >>> EXPORT_SYMBOL_GPL(generic_device_group); >>> +/* >>> + * Generic device_group call-back function. It just allocates one >>> + * iommu-group per iommu driver. >>> + */ >>> +struct iommu_group *generic_single_device_group(struct device *dev) >>> +{ >>> + struct iommu_device *iommu = dev->iommu->iommu_dev; >>> + >>> + lockdep_assert_held(&dev_iommu_group_lock); >>> + >>> + if (!iommu->singleton_group) { >>> + struct iommu_group *group; >>> + >>> + group = iommu_group_alloc(); >>> + if (IS_ERR(group)) >>> + return group; >>> + iommu->singleton_group = group; >>> + } >>> + return iommu_group_ref_get(iommu->singleton_group); >>> +} >>> +EXPORT_SYMBOL_GPL(generic_single_device_group); >> When allocating the singleton group for the first time, the group's >> refcount is taken twice. > Yes, that is correct. > > The refcount from alloc belongs to iommu->singleton_group and the > pair'd put is here: > > @@ -289,6 +289,9 @@ void iommu_device_unregister(struct iommu_device *iommu) > spin_lock(&iommu_device_lock); > list_del(&iommu->list); > spin_unlock(&iommu_device_lock); > + > + /* Pairs with the alloc in generic_single_device_group() */ > + iommu_group_put(iommu->singleton_group); > } > > The refcount from iommu_group_ref_get() belongs to the caller and the > caller must have a paired put. Oh, yes! The extra reference counter is paired with above put. Thanks for the explanation. Then, another small comment: iommu->singleton_group will be freed with above put, right? Do you need to set iommu->singleton_group to NULL? Given that iommu_device is not freed here. Best regards, baolu _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel