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 4A66137E308 for ; Tue, 14 Apr 2026 21:14:16 +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=1776201257; cv=none; b=hhIXemHXYuaO+Vjl0enKlG85lE+xbskjqNKG2rZWMRJ0Ud0mq3qWU7KONg/wuhp+6xtkiF0sTeNkPOKDE8B43jJv8h6HDcEa6IlzzyGON8BkQoXEg+Lmxo0SaIIzgpoPBfNqaN16iM/WId+5fWqDrRhKwd5Tl6hU2s2M8phmJJ0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776201257; c=relaxed/simple; bh=P82U9b1A4HStqPYDnOm9GD+DzgbwLf1lAwUYP1ER5wg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iD70DfvC0vJzGcYgunJAvyY4/HVndwf7v+UgKbKfmKRvHDwwIMKkYFJYWv30VXHjXnUZ+c++acRIZzgF2729tjuDZeZLwPejF28Y+5O9fM+PRKey+ng7ZcrgLNfu/sIv3KN9vSq/ExqKsI+3bpuKmbN1x7YeoC/FRVO2oJYeIKs= 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=PwbZfXha; 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="PwbZfXha" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [20.191.74.188]) by linux.microsoft.com (Postfix) with ESMTPSA id 9BFF020B6F0C; Tue, 14 Apr 2026 14:14:15 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 9BFF020B6F0C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1776201256; bh=iwqcK02WLd+CWksCwPDzhz12+1Xn7Wqds38ku7rZIyY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PwbZfXhaN1XS5bMzuyOFODVYXTjUVG1I+xEKccvMMmPZk2EYf/F0OSfvNIUktJGDn d9UoQJvHwVENsblzHqWvH8ZONqcg8B3mKx/04KipD5jukXMBZic5xuozH1MavuM0Dg RY8bWcDZ7VfIW+uRXWFlgd4OEKlwJ8bMDLPOgTmc= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , Mostafa Saleh , David Matlack , Robin Murphy , Nicolin Chen , "Tian, Kevin" , Yi Liu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Will Deacon , Jacob Pan , Baolu Lu Subject: [PATCH V4 02/10] iommufd: Move igroup allocation to a function Date: Tue, 14 Apr 2026 14:14:04 -0700 Message-Id: <20260414211412.2729-3-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260414211412.2729-1-jacob.pan@linux.microsoft.com> References: <20260414211412.2729-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. Reviewed-by: Samiullah Khawaja Signed-off-by: Jason Gunthorpe Signed-off-by: Jacob Pan --- v3: - Moved null group check out to the next patch (Mostafa) --- drivers/iommu/iommufd/device.c | 43 +++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 344d620cdecc..2f30ea069f66 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -56,6 +56,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 +111,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