Skip to content

Commit 7d4f46c

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd: Move _iommufd_object_alloc helper to a sharable file
The following patch will add a new vIOMMU allocator that will require this _iommufd_object_alloc to be sharable with IOMMU drivers (and iommufd too). Add a new driver.c file that will be built with CONFIG_IOMMUFD_DRIVER_CORE selected by CONFIG_IOMMUFD, and put the CONFIG_DRIVER under that remaining to be selectable for drivers to build the existing iova_bitmap.c file. Link: https://patch.msgid.link/r/2f4f6e116dc49ffb67ff6c5e8a7a8e789ab9e98e.1730836219.git.nicolinc@nvidia.com Suggested-by: Jason Gunthorpe <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Signed-off-by: Nicolin Chen <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent d1b3dad commit 7d4f46c

File tree

6 files changed

+60
-36
lines changed

6 files changed

+60
-36
lines changed

drivers/iommu/iommufd/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# SPDX-License-Identifier: GPL-2.0-only
2+
config IOMMUFD_DRIVER_CORE
3+
tristate
4+
default (IOMMUFD_DRIVER || IOMMUFD) if IOMMUFD!=n
5+
26
config IOMMUFD
37
tristate "IOMMU Userspace API"
48
select INTERVAL_TREE

drivers/iommu/iommufd/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o
1313

1414
obj-$(CONFIG_IOMMUFD) += iommufd.o
1515
obj-$(CONFIG_IOMMUFD_DRIVER) += iova_bitmap.o
16+
17+
iommufd_driver-y := driver.o
18+
obj-$(CONFIG_IOMMUFD_DRIVER_CORE) += iommufd_driver.o

drivers/iommu/iommufd/driver.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES
3+
*/
4+
#include "iommufd_private.h"
5+
6+
struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
7+
size_t size,
8+
enum iommufd_object_type type)
9+
{
10+
struct iommufd_object *obj;
11+
int rc;
12+
13+
obj = kzalloc(size, GFP_KERNEL_ACCOUNT);
14+
if (!obj)
15+
return ERR_PTR(-ENOMEM);
16+
obj->type = type;
17+
/* Starts out bias'd by 1 until it is removed from the xarray */
18+
refcount_set(&obj->shortterm_users, 1);
19+
refcount_set(&obj->users, 1);
20+
21+
/*
22+
* Reserve an ID in the xarray but do not publish the pointer yet since
23+
* the caller hasn't initialized it yet. Once the pointer is published
24+
* in the xarray and visible to other threads we can't reliably destroy
25+
* it anymore, so the caller must complete all errorable operations
26+
* before calling iommufd_object_finalize().
27+
*/
28+
rc = xa_alloc(&ictx->objects, &obj->id, XA_ZERO_ENTRY, xa_limit_31b,
29+
GFP_KERNEL_ACCOUNT);
30+
if (rc)
31+
goto out_free;
32+
return obj;
33+
out_free:
34+
kfree(obj);
35+
return ERR_PTR(rc);
36+
}
37+
EXPORT_SYMBOL_NS_GPL(_iommufd_object_alloc, IOMMUFD);
38+
39+
MODULE_DESCRIPTION("iommufd code shared with builtin modules");
40+
MODULE_LICENSE("GPL");

drivers/iommu/iommufd/iommufd_private.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,6 @@ iommufd_object_put_and_try_destroy(struct iommufd_ctx *ictx,
206206
iommufd_object_remove(ictx, obj, obj->id, 0);
207207
}
208208

209-
struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
210-
size_t size,
211-
enum iommufd_object_type type);
212-
213209
#define __iommufd_object_alloc(ictx, ptr, type, obj) \
214210
container_of(_iommufd_object_alloc( \
215211
ictx, \

drivers/iommu/iommufd/main.c

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,6 @@ struct iommufd_object_ops {
2929
static const struct iommufd_object_ops iommufd_object_ops[];
3030
static struct miscdevice vfio_misc_dev;
3131

32-
struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
33-
size_t size,
34-
enum iommufd_object_type type)
35-
{
36-
struct iommufd_object *obj;
37-
int rc;
38-
39-
obj = kzalloc(size, GFP_KERNEL_ACCOUNT);
40-
if (!obj)
41-
return ERR_PTR(-ENOMEM);
42-
obj->type = type;
43-
/* Starts out bias'd by 1 until it is removed from the xarray */
44-
refcount_set(&obj->shortterm_users, 1);
45-
refcount_set(&obj->users, 1);
46-
47-
/*
48-
* Reserve an ID in the xarray but do not publish the pointer yet since
49-
* the caller hasn't initialized it yet. Once the pointer is published
50-
* in the xarray and visible to other threads we can't reliably destroy
51-
* it anymore, so the caller must complete all errorable operations
52-
* before calling iommufd_object_finalize().
53-
*/
54-
rc = xa_alloc(&ictx->objects, &obj->id, XA_ZERO_ENTRY,
55-
xa_limit_31b, GFP_KERNEL_ACCOUNT);
56-
if (rc)
57-
goto out_free;
58-
return obj;
59-
out_free:
60-
kfree(obj);
61-
return ERR_PTR(rc);
62-
}
63-
6432
/*
6533
* Allow concurrent access to the object.
6634
*

include/linux/iommufd.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,17 @@ static inline int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx)
135135
return -EOPNOTSUPP;
136136
}
137137
#endif /* CONFIG_IOMMUFD */
138+
139+
#if IS_ENABLED(CONFIG_IOMMUFD_DRIVER_CORE)
140+
struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
141+
size_t size,
142+
enum iommufd_object_type type);
143+
#else /* !CONFIG_IOMMUFD_DRIVER_CORE */
144+
static inline struct iommufd_object *
145+
_iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size,
146+
enum iommufd_object_type type)
147+
{
148+
return ERR_PTR(-EOPNOTSUPP);
149+
}
150+
#endif /* CONFIG_IOMMUFD_DRIVER_CORE */
138151
#endif

0 commit comments

Comments
 (0)