Skip to content

Commit aa44198

Browse files
committed
Merge tag 'for-linus-iommufd' of git://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd
Pull iommufd updates from Jason Gunthorpe: "No major functionality this cycle: - iommufd part of the domain_alloc_paging_flags() conversion - Move IOMMU_HWPT_FAULT_ID_VALID processing out of drivers - Increase a timeout waiting for other threads to drop transient refcounts that syzkaller was hitting - Fix a UBSAN hit in iova_bitmap due to shift out of bounds - Add missing cleanup of fault events during FD shutdown, fixing a memory leak - Improve the fault delivery flow to have a smaller locking critical region that does not include copy_to_user() - Fix 32 bit ABI breakage due to missed implicit padding, and fix the stack memory leakage" * tag 'for-linus-iommufd' of git://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd: iommufd: Fix struct iommu_hwpt_pgfault init and padding iommufd/fault: Use a separate spinlock to protect fault->deliver list iommufd/fault: Destroy response and mutex in iommufd_fault_destroy() iommufd: Keep OBJ/IOCTL lists in an alphabetical order iommufd/iova_bitmap: Fix shift-out-of-bounds in iova_bitmap_offset_to_index() iommu: iommufd: fix WARNING in iommufd_device_unbind iommufd: Deal with IOMMU_HWPT_FAULT_ID_VALID in iommufd core iommufd/selftest: Remove domain_alloc_paging()
2 parents 47d6573 + e721f61 commit aa44198

File tree

9 files changed

+103
-74
lines changed

9 files changed

+103
-74
lines changed

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,12 @@ arm_vsmmu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
178178
const struct iommu_user_data *user_data)
179179
{
180180
struct arm_vsmmu *vsmmu = container_of(viommu, struct arm_vsmmu, core);
181-
const u32 SUPPORTED_FLAGS = IOMMU_HWPT_FAULT_ID_VALID;
182181
struct arm_smmu_nested_domain *nested_domain;
183182
struct iommu_hwpt_arm_smmuv3 arg;
184183
bool enable_ats = false;
185184
int ret;
186185

187-
/*
188-
* Faults delivered to the nested domain are faults that originated by
189-
* the S1 in the domain. The core code will match all PASIDs when
190-
* delivering the fault due to user_pasid_table
191-
*/
192-
if (flags & ~SUPPORTED_FLAGS)
186+
if (flags)
193187
return ERR_PTR(-EOPNOTSUPP);
194188

195189
ret = iommu_copy_struct_from_user(&arg, user_data,

drivers/iommu/intel/iommu.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3338,8 +3338,7 @@ intel_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags,
33383338
bool first_stage;
33393339

33403340
if (flags &
3341-
(~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING
3342-
| IOMMU_HWPT_FAULT_ID_VALID)))
3341+
(~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING)))
33433342
return ERR_PTR(-EOPNOTSUPP);
33443343
if (nested_parent && !nested_supported(iommu))
33453344
return ERR_PTR(-EOPNOTSUPP);

drivers/iommu/iommufd/fault.c

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,23 @@ static void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt,
103103
{
104104
struct iommufd_fault *fault = hwpt->fault;
105105
struct iopf_group *group, *next;
106+
struct list_head free_list;
106107
unsigned long index;
107108

108109
if (!fault)
109110
return;
111+
INIT_LIST_HEAD(&free_list);
110112

111113
mutex_lock(&fault->mutex);
114+
spin_lock(&fault->lock);
112115
list_for_each_entry_safe(group, next, &fault->deliver, node) {
113116
if (group->attach_handle != &handle->handle)
114117
continue;
118+
list_move(&group->node, &free_list);
119+
}
120+
spin_unlock(&fault->lock);
121+
122+
list_for_each_entry_safe(group, next, &free_list, node) {
115123
list_del(&group->node);
116124
iopf_group_response(group, IOMMU_PAGE_RESP_INVALID);
117125
iopf_free_group(group);
@@ -213,6 +221,7 @@ void iommufd_fault_destroy(struct iommufd_object *obj)
213221
{
214222
struct iommufd_fault *fault = container_of(obj, struct iommufd_fault, obj);
215223
struct iopf_group *group, *next;
224+
unsigned long index;
216225

217226
/*
218227
* The iommufd object's reference count is zero at this point.
@@ -225,6 +234,13 @@ void iommufd_fault_destroy(struct iommufd_object *obj)
225234
iopf_group_response(group, IOMMU_PAGE_RESP_INVALID);
226235
iopf_free_group(group);
227236
}
237+
xa_for_each(&fault->response, index, group) {
238+
xa_erase(&fault->response, index);
239+
iopf_group_response(group, IOMMU_PAGE_RESP_INVALID);
240+
iopf_free_group(group);
241+
}
242+
xa_destroy(&fault->response);
243+
mutex_destroy(&fault->mutex);
228244
}
229245

230246
static void iommufd_compose_fault_message(struct iommu_fault *fault,
@@ -247,7 +263,7 @@ static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *buf,
247263
{
248264
size_t fault_size = sizeof(struct iommu_hwpt_pgfault);
249265
struct iommufd_fault *fault = filep->private_data;
250-
struct iommu_hwpt_pgfault data;
266+
struct iommu_hwpt_pgfault data = {};
251267
struct iommufd_device *idev;
252268
struct iopf_group *group;
253269
struct iopf_fault *iopf;
@@ -258,17 +274,19 @@ static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *buf,
258274
return -ESPIPE;
259275

260276
mutex_lock(&fault->mutex);
261-
while (!list_empty(&fault->deliver) && count > done) {
262-
group = list_first_entry(&fault->deliver,
263-
struct iopf_group, node);
264-
265-
if (group->fault_count * fault_size > count - done)
277+
while ((group = iommufd_fault_deliver_fetch(fault))) {
278+
if (done >= count ||
279+
group->fault_count * fault_size > count - done) {
280+
iommufd_fault_deliver_restore(fault, group);
266281
break;
282+
}
267283

268284
rc = xa_alloc(&fault->response, &group->cookie, group,
269285
xa_limit_32b, GFP_KERNEL);
270-
if (rc)
286+
if (rc) {
287+
iommufd_fault_deliver_restore(fault, group);
271288
break;
289+
}
272290

273291
idev = to_iommufd_handle(group->attach_handle)->idev;
274292
list_for_each_entry(iopf, &group->faults, list) {
@@ -277,13 +295,12 @@ static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *buf,
277295
group->cookie);
278296
if (copy_to_user(buf + done, &data, fault_size)) {
279297
xa_erase(&fault->response, group->cookie);
298+
iommufd_fault_deliver_restore(fault, group);
280299
rc = -EFAULT;
281300
break;
282301
}
283302
done += fault_size;
284303
}
285-
286-
list_del(&group->node);
287304
}
288305
mutex_unlock(&fault->mutex);
289306

@@ -341,10 +358,10 @@ static __poll_t iommufd_fault_fops_poll(struct file *filep,
341358
__poll_t pollflags = EPOLLOUT;
342359

343360
poll_wait(filep, &fault->wait_queue, wait);
344-
mutex_lock(&fault->mutex);
361+
spin_lock(&fault->lock);
345362
if (!list_empty(&fault->deliver))
346363
pollflags |= EPOLLIN | EPOLLRDNORM;
347-
mutex_unlock(&fault->mutex);
364+
spin_unlock(&fault->lock);
348365

349366
return pollflags;
350367
}
@@ -386,6 +403,7 @@ int iommufd_fault_alloc(struct iommufd_ucmd *ucmd)
386403
INIT_LIST_HEAD(&fault->deliver);
387404
xa_init_flags(&fault->response, XA_FLAGS_ALLOC1);
388405
mutex_init(&fault->mutex);
406+
spin_lock_init(&fault->lock);
389407
init_waitqueue_head(&fault->wait_queue);
390408

391409
filep = anon_inode_getfile("[iommufd-pgfault]", &iommufd_fault_fops,
@@ -434,9 +452,9 @@ int iommufd_fault_iopf_handler(struct iopf_group *group)
434452
hwpt = group->attach_handle->domain->fault_data;
435453
fault = hwpt->fault;
436454

437-
mutex_lock(&fault->mutex);
455+
spin_lock(&fault->lock);
438456
list_add_tail(&group->node, &fault->deliver);
439-
mutex_unlock(&fault->mutex);
457+
spin_unlock(&fault->lock);
440458

441459
wake_up_interruptible(&fault->wait_queue);
442460

drivers/iommu/iommufd/hw_pagetable.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
140140
hwpt_paging->nest_parent = flags & IOMMU_HWPT_ALLOC_NEST_PARENT;
141141

142142
if (ops->domain_alloc_paging_flags) {
143-
hwpt->domain = ops->domain_alloc_paging_flags(idev->dev, flags,
144-
user_data);
143+
hwpt->domain = ops->domain_alloc_paging_flags(idev->dev,
144+
flags & ~IOMMU_HWPT_FAULT_ID_VALID, user_data);
145145
if (IS_ERR(hwpt->domain)) {
146146
rc = PTR_ERR(hwpt->domain);
147147
hwpt->domain = NULL;
@@ -280,6 +280,8 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags,
280280
struct iommufd_hw_pagetable *hwpt;
281281
int rc;
282282

283+
if (flags & ~IOMMU_HWPT_FAULT_ID_VALID)
284+
return ERR_PTR(-EOPNOTSUPP);
283285
if (!user_data->len)
284286
return ERR_PTR(-EOPNOTSUPP);
285287
if (!viommu->ops || !viommu->ops->alloc_domain_nested)
@@ -296,7 +298,9 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags,
296298
hwpt_nested->parent = viommu->hwpt;
297299

298300
hwpt->domain =
299-
viommu->ops->alloc_domain_nested(viommu, flags, user_data);
301+
viommu->ops->alloc_domain_nested(viommu,
302+
flags & ~IOMMU_HWPT_FAULT_ID_VALID,
303+
user_data);
300304
if (IS_ERR(hwpt->domain)) {
301305
rc = PTR_ERR(hwpt->domain);
302306
hwpt->domain = NULL;

drivers/iommu/iommufd/iommufd_private.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,39 @@ struct iommufd_fault {
443443
struct iommufd_ctx *ictx;
444444
struct file *filep;
445445

446-
/* The lists of outstanding faults protected by below mutex. */
447-
struct mutex mutex;
446+
spinlock_t lock; /* protects the deliver list */
448447
struct list_head deliver;
448+
struct mutex mutex; /* serializes response flows */
449449
struct xarray response;
450450

451451
struct wait_queue_head wait_queue;
452452
};
453453

454+
/* Fetch the first node out of the fault->deliver list */
455+
static inline struct iopf_group *
456+
iommufd_fault_deliver_fetch(struct iommufd_fault *fault)
457+
{
458+
struct list_head *list = &fault->deliver;
459+
struct iopf_group *group = NULL;
460+
461+
spin_lock(&fault->lock);
462+
if (!list_empty(list)) {
463+
group = list_first_entry(list, struct iopf_group, node);
464+
list_del(&group->node);
465+
}
466+
spin_unlock(&fault->lock);
467+
return group;
468+
}
469+
470+
/* Restore a node back to the head of the fault->deliver list */
471+
static inline void iommufd_fault_deliver_restore(struct iommufd_fault *fault,
472+
struct iopf_group *group)
473+
{
474+
spin_lock(&fault->lock);
475+
list_add(&group->node, &fault->deliver);
476+
spin_unlock(&fault->lock);
477+
}
478+
454479
struct iommufd_attach_handle {
455480
struct iommu_attach_handle handle;
456481
struct iommufd_device *idev;

drivers/iommu/iommufd/iova_bitmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ struct iova_bitmap {
130130
static unsigned long iova_bitmap_offset_to_index(struct iova_bitmap *bitmap,
131131
unsigned long iova)
132132
{
133-
unsigned long pgsize = 1 << bitmap->mapped.pgshift;
133+
unsigned long pgsize = 1UL << bitmap->mapped.pgshift;
134134

135135
return iova / (BITS_PER_TYPE(*bitmap->bitmap) * pgsize);
136136
}

drivers/iommu/iommufd/main.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static int iommufd_object_dec_wait_shortterm(struct iommufd_ctx *ictx,
104104
if (wait_event_timeout(ictx->destroy_wait,
105105
refcount_read(&to_destroy->shortterm_users) ==
106106
0,
107-
msecs_to_jiffies(10000)))
107+
msecs_to_jiffies(60000)))
108108
return 0;
109109

110110
pr_crit("Time out waiting for iommufd object to become free\n");
@@ -307,9 +307,9 @@ union ucmd_buffer {
307307
struct iommu_ioas_map map;
308308
struct iommu_ioas_unmap unmap;
309309
struct iommu_option option;
310+
struct iommu_vdevice_alloc vdev;
310311
struct iommu_vfio_ioas vfio_ioas;
311312
struct iommu_viommu_alloc viommu;
312-
struct iommu_vdevice_alloc vdev;
313313
#ifdef CONFIG_IOMMUFD_TEST
314314
struct iommu_test_cmd test;
315315
#endif
@@ -333,8 +333,8 @@ struct iommufd_ioctl_op {
333333
}
334334
static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
335335
IOCTL_OP(IOMMU_DESTROY, iommufd_destroy, struct iommu_destroy, id),
336-
IOCTL_OP(IOMMU_FAULT_QUEUE_ALLOC, iommufd_fault_alloc, struct iommu_fault_alloc,
337-
out_fault_fd),
336+
IOCTL_OP(IOMMU_FAULT_QUEUE_ALLOC, iommufd_fault_alloc,
337+
struct iommu_fault_alloc, out_fault_fd),
338338
IOCTL_OP(IOMMU_GET_HW_INFO, iommufd_get_hw_info, struct iommu_hw_info,
339339
__reserved),
340340
IOCTL_OP(IOMMU_HWPT_ALLOC, iommufd_hwpt_alloc, struct iommu_hwpt_alloc,
@@ -355,20 +355,18 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
355355
src_iova),
356356
IOCTL_OP(IOMMU_IOAS_IOVA_RANGES, iommufd_ioas_iova_ranges,
357357
struct iommu_ioas_iova_ranges, out_iova_alignment),
358-
IOCTL_OP(IOMMU_IOAS_MAP, iommufd_ioas_map, struct iommu_ioas_map,
359-
iova),
358+
IOCTL_OP(IOMMU_IOAS_MAP, iommufd_ioas_map, struct iommu_ioas_map, iova),
360359
IOCTL_OP(IOMMU_IOAS_MAP_FILE, iommufd_ioas_map_file,
361360
struct iommu_ioas_map_file, iova),
362361
IOCTL_OP(IOMMU_IOAS_UNMAP, iommufd_ioas_unmap, struct iommu_ioas_unmap,
363362
length),
364-
IOCTL_OP(IOMMU_OPTION, iommufd_option, struct iommu_option,
365-
val64),
363+
IOCTL_OP(IOMMU_OPTION, iommufd_option, struct iommu_option, val64),
364+
IOCTL_OP(IOMMU_VDEVICE_ALLOC, iommufd_vdevice_alloc_ioctl,
365+
struct iommu_vdevice_alloc, virt_id),
366366
IOCTL_OP(IOMMU_VFIO_IOAS, iommufd_vfio_ioas, struct iommu_vfio_ioas,
367367
__reserved),
368368
IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl,
369369
struct iommu_viommu_alloc, out_viommu_id),
370-
IOCTL_OP(IOMMU_VDEVICE_ALLOC, iommufd_vdevice_alloc_ioctl,
371-
struct iommu_vdevice_alloc, virt_id),
372370
#ifdef CONFIG_IOMMUFD_TEST
373371
IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
374372
#endif
@@ -490,8 +488,8 @@ static const struct iommufd_object_ops iommufd_object_ops[] = {
490488
[IOMMUFD_OBJ_DEVICE] = {
491489
.destroy = iommufd_device_destroy,
492490
},
493-
[IOMMUFD_OBJ_IOAS] = {
494-
.destroy = iommufd_ioas_destroy,
491+
[IOMMUFD_OBJ_FAULT] = {
492+
.destroy = iommufd_fault_destroy,
495493
},
496494
[IOMMUFD_OBJ_HWPT_PAGING] = {
497495
.destroy = iommufd_hwpt_paging_destroy,
@@ -501,15 +499,15 @@ static const struct iommufd_object_ops iommufd_object_ops[] = {
501499
.destroy = iommufd_hwpt_nested_destroy,
502500
.abort = iommufd_hwpt_nested_abort,
503501
},
504-
[IOMMUFD_OBJ_FAULT] = {
505-
.destroy = iommufd_fault_destroy,
506-
},
507-
[IOMMUFD_OBJ_VIOMMU] = {
508-
.destroy = iommufd_viommu_destroy,
502+
[IOMMUFD_OBJ_IOAS] = {
503+
.destroy = iommufd_ioas_destroy,
509504
},
510505
[IOMMUFD_OBJ_VDEVICE] = {
511506
.destroy = iommufd_vdevice_destroy,
512507
},
508+
[IOMMUFD_OBJ_VIOMMU] = {
509+
.destroy = iommufd_viommu_destroy,
510+
},
513511
#ifdef CONFIG_IOMMUFD_TEST
514512
[IOMMUFD_OBJ_SELFTEST] = {
515513
.destroy = iommufd_selftest_destroy,

0 commit comments

Comments
 (0)