Skip to content

Commit bd529db

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd: Add a nested HW pagetable object
IOMMU_HWPT_ALLOC already supports iommu_domain allocation for usersapce. But it can only allocate a hw_pagetable that associates to a given IOAS, i.e. only a kernel-managed hw_pagetable of IOMMUFD_OBJ_HWPT_PAGING type. IOMMU drivers can now support user-managed hw_pagetables, for two-stage translation use cases that require user data input from the user space. Add a new IOMMUFD_OBJ_HWPT_NESTED type with its abort/destroy(). Pair it with a new iommufd_hwpt_nested structure and its to_hwpt_nested() helper. Update the to_hwpt_paging() helper, so a NESTED-type hw_pagetable can be handled in the callers, for example iommufd_hw_pagetable_enforce_rr(). Screen the inputs including the parent PAGING-type hw_pagetable that has a need of a new nest_parent flag in the iommufd_hwpt_paging structure. Extend the IOMMU_HWPT_ALLOC ioctl to accept an IOMMU driver specific data input which is tagged by the enum iommu_hwpt_data_type. Also, update the @pt_id to accept hwpt_id too besides an ioas_id. Then, use them to allocate a hw_pagetable of IOMMUFD_OBJ_HWPT_NESTED type using the iommufd_hw_pagetable_alloc_nested() allocator. Link: https://lore.kernel.org/r/20231026043938.63898-8-yi.l.liu@intel.com Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> Co-developed-by: Yi Liu <yi.l.liu@intel.com> Signed-off-by: Yi Liu <yi.l.liu@intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 2bdabb8 commit bd529db

File tree

5 files changed

+159
-16
lines changed

5 files changed

+159
-16
lines changed

drivers/iommu/iommufd/device.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ iommufd_device_auto_get_domain(struct iommufd_device *idev,
588588
}
589589

590590
hwpt_paging = iommufd_hwpt_paging_alloc(idev->ictx, ioas, idev, 0,
591-
immediate_attach);
591+
immediate_attach, NULL);
592592
if (IS_ERR(hwpt_paging)) {
593593
destroy_hwpt = ERR_CAST(hwpt_paging);
594594
goto out_unlock;
@@ -628,6 +628,7 @@ static int iommufd_device_change_pt(struct iommufd_device *idev, u32 *pt_id,
628628
return PTR_ERR(pt_obj);
629629

630630
switch (pt_obj->type) {
631+
case IOMMUFD_OBJ_HWPT_NESTED:
631632
case IOMMUFD_OBJ_HWPT_PAGING: {
632633
struct iommufd_hw_pagetable *hwpt =
633634
container_of(pt_obj, struct iommufd_hw_pagetable, obj);

drivers/iommu/iommufd/hw_pagetable.c

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ void iommufd_hwpt_paging_abort(struct iommufd_object *obj)
4444
iommufd_hwpt_paging_destroy(obj);
4545
}
4646

47+
void iommufd_hwpt_nested_destroy(struct iommufd_object *obj)
48+
{
49+
struct iommufd_hwpt_nested *hwpt_nested =
50+
container_of(obj, struct iommufd_hwpt_nested, common.obj);
51+
52+
if (hwpt_nested->common.domain)
53+
iommu_domain_free(hwpt_nested->common.domain);
54+
55+
refcount_dec(&hwpt_nested->parent->common.obj.users);
56+
}
57+
58+
void iommufd_hwpt_nested_abort(struct iommufd_object *obj)
59+
{
60+
iommufd_hwpt_nested_destroy(obj);
61+
}
62+
4763
static int
4864
iommufd_hwpt_paging_enforce_cc(struct iommufd_hwpt_paging *hwpt_paging)
4965
{
@@ -68,6 +84,8 @@ iommufd_hwpt_paging_enforce_cc(struct iommufd_hwpt_paging *hwpt_paging)
6884
* @idev: Device to get an iommu_domain for
6985
* @flags: Flags from userspace
7086
* @immediate_attach: True if idev should be attached to the hwpt
87+
* @user_data: The user provided driver specific data describing the domain to
88+
* create
7189
*
7290
* Allocate a new iommu_domain and return it as a hw_pagetable. The HWPT
7391
* will be linked to the given ioas and upon return the underlying iommu_domain
@@ -80,7 +98,8 @@ iommufd_hwpt_paging_enforce_cc(struct iommufd_hwpt_paging *hwpt_paging)
8098
struct iommufd_hwpt_paging *
8199
iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
82100
struct iommufd_device *idev, u32 flags,
83-
bool immediate_attach)
101+
bool immediate_attach,
102+
const struct iommu_user_data *user_data)
84103
{
85104
const u32 valid_flags = IOMMU_HWPT_ALLOC_NEST_PARENT |
86105
IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
@@ -91,7 +110,7 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
91110

92111
lockdep_assert_held(&ioas->mutex);
93112

94-
if (flags && !ops->domain_alloc_user)
113+
if ((flags || user_data) && !ops->domain_alloc_user)
95114
return ERR_PTR(-EOPNOTSUPP);
96115
if (flags & ~valid_flags)
97116
return ERR_PTR(-EOPNOTSUPP);
@@ -106,10 +125,11 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
106125
/* Pairs with iommufd_hw_pagetable_destroy() */
107126
refcount_inc(&ioas->obj.users);
108127
hwpt_paging->ioas = ioas;
128+
hwpt_paging->nest_parent = flags & IOMMU_HWPT_ALLOC_NEST_PARENT;
109129

110130
if (ops->domain_alloc_user) {
111-
hwpt->domain =
112-
ops->domain_alloc_user(idev->dev, flags, NULL, NULL);
131+
hwpt->domain = ops->domain_alloc_user(idev->dev, flags, NULL,
132+
user_data);
113133
if (IS_ERR(hwpt->domain)) {
114134
rc = PTR_ERR(hwpt->domain);
115135
hwpt->domain = NULL;
@@ -169,9 +189,70 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
169189
return ERR_PTR(rc);
170190
}
171191

192+
/**
193+
* iommufd_hwpt_nested_alloc() - Get a NESTED iommu_domain for a device
194+
* @ictx: iommufd context
195+
* @parent: Parent PAGING-type hwpt to associate the domain with
196+
* @idev: Device to get an iommu_domain for
197+
* @flags: Flags from userspace
198+
* @user_data: user_data pointer. Must be valid
199+
*
200+
* Allocate a new iommu_domain (must be IOMMU_DOMAIN_NESTED) and return it as
201+
* a NESTED hw_pagetable. The given parent PAGING-type hwpt must be capable of
202+
* being a parent.
203+
*/
204+
static struct iommufd_hwpt_nested *
205+
iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx,
206+
struct iommufd_hwpt_paging *parent,
207+
struct iommufd_device *idev, u32 flags,
208+
const struct iommu_user_data *user_data)
209+
{
210+
const struct iommu_ops *ops = dev_iommu_ops(idev->dev);
211+
struct iommufd_hwpt_nested *hwpt_nested;
212+
struct iommufd_hw_pagetable *hwpt;
213+
int rc;
214+
215+
if (flags || !user_data->len || !ops->domain_alloc_user)
216+
return ERR_PTR(-EOPNOTSUPP);
217+
if (parent->auto_domain || !parent->nest_parent)
218+
return ERR_PTR(-EINVAL);
219+
220+
hwpt_nested = __iommufd_object_alloc(
221+
ictx, hwpt_nested, IOMMUFD_OBJ_HWPT_NESTED, common.obj);
222+
if (IS_ERR(hwpt_nested))
223+
return ERR_CAST(hwpt_nested);
224+
hwpt = &hwpt_nested->common;
225+
226+
refcount_inc(&parent->common.obj.users);
227+
hwpt_nested->parent = parent;
228+
229+
hwpt->domain = ops->domain_alloc_user(idev->dev, flags,
230+
parent->common.domain, user_data);
231+
if (IS_ERR(hwpt->domain)) {
232+
rc = PTR_ERR(hwpt->domain);
233+
hwpt->domain = NULL;
234+
goto out_abort;
235+
}
236+
237+
if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) {
238+
rc = -EINVAL;
239+
goto out_abort;
240+
}
241+
return hwpt_nested;
242+
243+
out_abort:
244+
iommufd_object_abort_and_destroy(ictx, &hwpt->obj);
245+
return ERR_PTR(rc);
246+
}
247+
172248
int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd)
173249
{
174250
struct iommu_hwpt_alloc *cmd = ucmd->cmd;
251+
const struct iommu_user_data user_data = {
252+
.type = cmd->data_type,
253+
.uptr = u64_to_user_ptr(cmd->data_uptr),
254+
.len = cmd->data_len,
255+
};
175256
struct iommufd_hw_pagetable *hwpt;
176257
struct iommufd_ioas *ioas = NULL;
177258
struct iommufd_object *pt_obj;
@@ -180,6 +261,8 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd)
180261

181262
if (cmd->__reserved)
182263
return -EOPNOTSUPP;
264+
if (cmd->data_type == IOMMU_HWPT_DATA_NONE && cmd->data_len)
265+
return -EINVAL;
183266

184267
idev = iommufd_get_device(ucmd, cmd->dev_id);
185268
if (IS_ERR(idev))
@@ -196,13 +279,27 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd)
196279

197280
ioas = container_of(pt_obj, struct iommufd_ioas, obj);
198281
mutex_lock(&ioas->mutex);
199-
hwpt_paging = iommufd_hwpt_paging_alloc(ucmd->ictx, ioas, idev,
200-
cmd->flags, false);
282+
hwpt_paging = iommufd_hwpt_paging_alloc(
283+
ucmd->ictx, ioas, idev, cmd->flags, false,
284+
user_data.len ? &user_data : NULL);
201285
if (IS_ERR(hwpt_paging)) {
202286
rc = PTR_ERR(hwpt_paging);
203287
goto out_unlock;
204288
}
205289
hwpt = &hwpt_paging->common;
290+
} else if (pt_obj->type == IOMMUFD_OBJ_HWPT_PAGING) {
291+
struct iommufd_hwpt_nested *hwpt_nested;
292+
293+
hwpt_nested = iommufd_hwpt_nested_alloc(
294+
ucmd->ictx,
295+
container_of(pt_obj, struct iommufd_hwpt_paging,
296+
common.obj),
297+
idev, cmd->flags, &user_data);
298+
if (IS_ERR(hwpt_nested)) {
299+
rc = PTR_ERR(hwpt_nested);
300+
goto out_unlock;
301+
}
302+
hwpt = &hwpt_nested->common;
206303
} else {
207304
rc = -EINVAL;
208305
goto out_put_pt;

drivers/iommu/iommufd/iommufd_private.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ enum iommufd_object_type {
124124
IOMMUFD_OBJ_ANY = IOMMUFD_OBJ_NONE,
125125
IOMMUFD_OBJ_DEVICE,
126126
IOMMUFD_OBJ_HWPT_PAGING,
127+
IOMMUFD_OBJ_HWPT_NESTED,
127128
IOMMUFD_OBJ_IOAS,
128129
IOMMUFD_OBJ_ACCESS,
129130
#ifdef CONFIG_IOMMUFD_TEST
@@ -255,10 +256,16 @@ struct iommufd_hwpt_paging {
255256
bool auto_domain : 1;
256257
bool enforce_cache_coherency : 1;
257258
bool msi_cookie : 1;
259+
bool nest_parent : 1;
258260
/* Head at iommufd_ioas::hwpt_list */
259261
struct list_head hwpt_item;
260262
};
261263

264+
struct iommufd_hwpt_nested {
265+
struct iommufd_hw_pagetable common;
266+
struct iommufd_hwpt_paging *parent;
267+
};
268+
262269
static inline bool hwpt_is_paging(struct iommufd_hw_pagetable *hwpt)
263270
{
264271
return hwpt->obj.type == IOMMUFD_OBJ_HWPT_PAGING;
@@ -283,25 +290,32 @@ int iommufd_hwpt_get_dirty_bitmap(struct iommufd_ucmd *ucmd);
283290
struct iommufd_hwpt_paging *
284291
iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
285292
struct iommufd_device *idev, u32 flags,
286-
bool immediate_attach);
293+
bool immediate_attach,
294+
const struct iommu_user_data *user_data);
287295
int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
288296
struct iommufd_device *idev);
289297
struct iommufd_hw_pagetable *
290298
iommufd_hw_pagetable_detach(struct iommufd_device *idev);
291299
void iommufd_hwpt_paging_destroy(struct iommufd_object *obj);
292300
void iommufd_hwpt_paging_abort(struct iommufd_object *obj);
301+
void iommufd_hwpt_nested_destroy(struct iommufd_object *obj);
302+
void iommufd_hwpt_nested_abort(struct iommufd_object *obj);
293303
int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd);
294304

295305
static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx,
296306
struct iommufd_hw_pagetable *hwpt)
297307
{
298-
struct iommufd_hwpt_paging *hwpt_paging = to_hwpt_paging(hwpt);
308+
if (hwpt->obj.type == IOMMUFD_OBJ_HWPT_PAGING) {
309+
struct iommufd_hwpt_paging *hwpt_paging = to_hwpt_paging(hwpt);
299310

300-
lockdep_assert_not_held(&hwpt_paging->ioas->mutex);
301-
if (hwpt_paging->auto_domain)
302-
iommufd_object_deref_user(ictx, &hwpt->obj);
303-
else
304-
refcount_dec(&hwpt->obj.users);
311+
lockdep_assert_not_held(&hwpt_paging->ioas->mutex);
312+
313+
if (hwpt_paging->auto_domain) {
314+
iommufd_object_deref_user(ictx, &hwpt->obj);
315+
return;
316+
}
317+
}
318+
refcount_dec(&hwpt->obj.users);
305319
}
306320

307321
struct iommufd_group {

drivers/iommu/iommufd/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ static const struct iommufd_object_ops iommufd_object_ops[] = {
492492
.destroy = iommufd_hwpt_paging_destroy,
493493
.abort = iommufd_hwpt_paging_abort,
494494
},
495+
[IOMMUFD_OBJ_HWPT_NESTED] = {
496+
.destroy = iommufd_hwpt_nested_destroy,
497+
.abort = iommufd_hwpt_nested_abort,
498+
},
495499
#ifdef CONFIG_IOMMUFD_TEST
496500
[IOMMUFD_OBJ_SELFTEST] = {
497501
.destroy = iommufd_selftest_destroy,

include/uapi/linux/iommufd.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,20 +361,44 @@ enum iommufd_hwpt_alloc_flags {
361361
IOMMU_HWPT_ALLOC_DIRTY_TRACKING = 1 << 1,
362362
};
363363

364+
/**
365+
* enum iommu_hwpt_data_type - IOMMU HWPT Data Type
366+
* @IOMMU_HWPT_DATA_NONE: no data
367+
*/
368+
enum iommu_hwpt_data_type {
369+
IOMMU_HWPT_DATA_NONE,
370+
};
371+
364372
/**
365373
* struct iommu_hwpt_alloc - ioctl(IOMMU_HWPT_ALLOC)
366374
* @size: sizeof(struct iommu_hwpt_alloc)
367375
* @flags: Combination of enum iommufd_hwpt_alloc_flags
368376
* @dev_id: The device to allocate this HWPT for
369-
* @pt_id: The IOAS to connect this HWPT to
377+
* @pt_id: The IOAS or HWPT to connect this HWPT to
370378
* @out_hwpt_id: The ID of the new HWPT
371379
* @__reserved: Must be 0
380+
* @data_type: One of enum iommu_hwpt_data_type
381+
* @data_len: Length of the type specific data
382+
* @data_uptr: User pointer to the type specific data
372383
*
373384
* Explicitly allocate a hardware page table object. This is the same object
374385
* type that is returned by iommufd_device_attach() and represents the
375386
* underlying iommu driver's iommu_domain kernel object.
376387
*
377-
* A HWPT will be created with the IOVA mappings from the given IOAS.
388+
* A kernel-managed HWPT will be created with the mappings from the given
389+
* IOAS via the @pt_id. The @data_type for this allocation must be set to
390+
* IOMMU_HWPT_DATA_NONE. The HWPT can be allocated as a parent HWPT for a
391+
* nesting configuration by passing IOMMU_HWPT_ALLOC_NEST_PARENT via @flags.
392+
*
393+
* A user-managed nested HWPT will be created from a given parent HWPT via
394+
* @pt_id, in which the parent HWPT must be allocated previously via the
395+
* same ioctl from a given IOAS (@pt_id). In this case, the @data_type
396+
* must be set to a pre-defined type corresponding to an I/O page table
397+
* type supported by the underlying IOMMU hardware.
398+
*
399+
* If the @data_type is set to IOMMU_HWPT_DATA_NONE, @data_len and
400+
* @data_uptr should be zero. Otherwise, both @data_len and @data_uptr
401+
* must be given.
378402
*/
379403
struct iommu_hwpt_alloc {
380404
__u32 size;
@@ -383,6 +407,9 @@ struct iommu_hwpt_alloc {
383407
__u32 pt_id;
384408
__u32 out_hwpt_id;
385409
__u32 __reserved;
410+
__u32 data_type;
411+
__u32 data_len;
412+
__aligned_u64 data_uptr;
386413
};
387414
#define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC)
388415

0 commit comments

Comments
 (0)