From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 83191362136 for ; Fri, 27 Feb 2026 17:52:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772214772; cv=none; b=GmPcNLrO97NrXfCG38j3HODtqdylYu90t5E58A67CmB1lwTWxyNuGczRl7ONbNwPhM271DUz4KPtCwCJaMOtN99mx0ABNTp/e39yO4KJDAKhY4ePIGI5cFtKy8H+HyrS+Dwm8/SYEeFF6w4bRMYO+HiEK8V2qHG7UkZFgkwBwa8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772214772; c=relaxed/simple; bh=2pSNNjCMJCzi4E9NiX/bZFyADwrRK7bee1k67bm0SZQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JQnBAW2YMpuZWg3HXN6bkWJfC0+l71O2Edf5IvghuGXY17ZZzPQD+VMvl8LzTYbK3hh5egyQhHftG4y/r8oOANETMgjG7zHRIF92laZaQlDvTGOsVyhBJZTKPhLgmN06wW6KizjPvuHlsdM+UhFxSEvicL56jypD6mcUE0lsHH4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=C4j+7E+x; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="C4j+7E+x" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [20.236.11.69]) by linux.microsoft.com (Postfix) with ESMTPSA id 96BB620B6F03; Fri, 27 Feb 2026 09:52:50 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 96BB620B6F03 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1772214771; bh=4uSNyWj8lWw77lOFEo+XTvKl1no/K1YZKJTwbDlGzmY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C4j+7E+xIfTGQq2J5TMwdviUb431sfmpEQDf0xVndqzGGmajfHMgEQbUWsmHQ6zLJ OqH+vxZyCpvZaAwq33tY1NXreRqCQk0+0MkEd7BM4XGAMqFBwwYLHipNtkvx1AJb6M 0mSUDinxPVHsdzI5pNFhqPuar1Yv5GlVf4hH6CSk= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , David Matlack , Nicolin Chen , "Tian, Kevin" , Yi Liu , Baolu Lu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Jacob Pan , Jean Philippe-Brucker , Robin Murphy Subject: [PATCH 02/11] iommufd: Move igroup allocation to a function Date: Fri, 27 Feb 2026 09:52:38 -0800 Message-Id: <20260227175247.26103-3-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260227175247.26103-1-jacob.pan@linux.microsoft.com> References: <20260227175247.26103-1-jacob.pan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Jason Gunthorpe So it can be reused in the next patch which allows binding to noiommu device. Signed-off-by: Jason Gunthorpe Signed-off-by: Jacob Pan --- drivers/iommu/iommufd/device.c | 48 +++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 344d620cdecc..54d73016468f 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -30,8 +30,9 @@ static void iommufd_group_release(struct kref *kref) WARN_ON(!xa_empty(&igroup->pasid_attach)); - xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, - NULL, GFP_KERNEL); + if (igroup->group) + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), + igroup, NULL, GFP_KERNEL); iommu_group_put(igroup->group); mutex_destroy(&igroup->lock); kfree(igroup); @@ -56,6 +57,30 @@ static bool iommufd_group_try_get(struct iommufd_group *igroup, return kref_get_unless_zero(&igroup->ref); } +static struct iommufd_group *iommufd_alloc_group(struct iommufd_ctx *ictx, + struct iommu_group *group) +{ + struct iommufd_group *new_igroup; + + new_igroup = kzalloc(sizeof(*new_igroup), GFP_KERNEL); + if (!new_igroup) + return ERR_PTR(-ENOMEM); + + kref_init(&new_igroup->ref); + mutex_init(&new_igroup->lock); + xa_init(&new_igroup->pasid_attach); + new_igroup->sw_msi_start = PHYS_ADDR_MAX; + /* group reference moves into new_igroup */ + new_igroup->group = group; + + /* + * The ictx is not additionally refcounted here becase all objects using + * an igroup must put it before their destroy completes. + */ + new_igroup->ictx = ictx; + return new_igroup; +} + /* * iommufd needs to store some more data for each iommu_group, we keep a * parallel xarray indexed by iommu_group id to hold this instead of putting it @@ -87,25 +112,12 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, } xa_unlock(&ictx->groups); - new_igroup = kzalloc_obj(*new_igroup); - if (!new_igroup) { + new_igroup = iommufd_alloc_group(ictx, group); + if (IS_ERR(new_igroup)) { iommu_group_put(group); - return ERR_PTR(-ENOMEM); + return new_igroup; } - kref_init(&new_igroup->ref); - mutex_init(&new_igroup->lock); - xa_init(&new_igroup->pasid_attach); - new_igroup->sw_msi_start = PHYS_ADDR_MAX; - /* group reference moves into new_igroup */ - new_igroup->group = group; - - /* - * The ictx is not additionally refcounted here becase all objects using - * an igroup must put it before their destroy completes. - */ - new_igroup->ictx = ictx; - /* * We dropped the lock so igroup is invalid. NULL is a safe and likely * value to assume for the xa_cmpxchg algorithm. -- 2.34.1