Skip to contentSkip to conversation

iommufd/fault: Two bug fixes prior to vEVENTQ

PATCH In review Nicolin Chen 2 participants 6 messages Jan 15, 2025 – Jan 17, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover iommufd/fault: Two bug fixes prior to vEVENTQ

It is too late to get the vEVENTQ series merged in this cycle, while some bug fixes for the fault.c file can go in first. These fix two small bugs.

I will send vEVENTQ v6 later to get reviewed. And hopefully that can get merged in the early weeks of the next cycle.

Changelog:
v2
* Add "Reviewed-by" from Jason/Kevin/Baolu
* Move fault->mutex closer to fault->response
* Fix inversed arguments passing at list_add()
* Replace "for" loops with simpler "while" loops
* Update kdoc to reflex all the changes in this version
* Rename iommufd_fault_deliver_extract to iommufd_fault_deliver_fetch
v1
https://lore.kernel.org/all/[email protected]/

Thanks
Nicolin

Nicolin Chen (2):
iommufd/fault: Destroy response and mutex in iommufd_fault_destroy() iommufd/fault: Use a separate spinlock to protect fault->deliver list

 drivers/iommu/iommufd/iommufd_private.h | 29 ++++++++++++-
 drivers/iommu/iommufd/fault.c           | 54 +++++++++++++++----------
 2 files changed, 60 insertions(+), 23 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 iommufd/fault: Destroy response and mutex in iommufd_fault_destroy()

Both were missing in the initial patch.

Fixes 07838f7fd529 ("iommufd: Add iommufd fault object")
---
 drivers/iommu/iommufd/fault.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/iommu/iommufd/fault.c +8 -0
index 1fe804e28a86..685510224d05 100644
@@ -213,6 +213,7 @@ void iommufd_fault_destroy(struct iommufd_object *obj)
 {
 	struct iommufd_fault *fault = container_of(obj, struct iommufd_fault, obj);
 	struct iopf_group *group, *next;
+	unsigned long index;
 
 	/*
 	 * The iommufd object's reference count is zero at this point.
@@ -225,6 +226,13 @@ void iommufd_fault_destroy(struct iommufd_object *obj)
 		iopf_group_response(group, IOMMU_PAGE_RESP_INVALID);
 		iopf_free_group(group);
 	}
+	xa_for_each(&fault->response, index, group) {
+		xa_erase(&fault->response, index);
+		iopf_group_response(group, IOMMU_PAGE_RESP_INVALID);
+		iopf_free_group(group);
+	}
+	xa_destroy(&fault->response);
+	mutex_destroy(&fault->mutex);
 }
 
 static void iommufd_compose_fault_message(struct iommu_fault *fault,
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH rc v2 1/2] iommufd/fault: Destroy response and mutex in iommufd_fault_destroy()

Quoted 11 lines · Nicolin Chen

Both were missing in the initial patch.

Fixes 07838f7fd529 ("iommufd: Add iommufd fault object")
---
 drivers/iommu/iommufd/fault.c | 8 ++++++++
 1 file changed, 8 insertions(+)

I picked this one up

Thanks,
Jason

#3 lore raw

PATCH v2 2/2 iommufd/fault: Use a separate spinlock to protect fault->deliver list

In reply to Nicolin Chen

The fault->mutex was to serialize the fault read()/write() fops and the iommufd_fault_auto_response_faults(), mainly for fault->response. Also, it was conveniently used to fence the fault->deliver in poll() fop and iommufd_fault_iopf_handler().

However, copy_from/to_user() may sleep if pagefaults are enabled. Thus, they could take a long time to wait for user pages to swap in, blocking iommufd_fault_iopf_handler() and its caller that is typically a shared IRQ handler of an IOMMU driver, resulting in a potential global DOS.

Instead of resuing the mutex to protect the fault->deliver list, add a separate spinlock to do the job, so iommufd_fault_iopf_handler() would no longer be blocked by copy_from/to_user().

Provide two list manipulation helpers for fault->deliver:
- Fetch the first iopf_group out of the fault->deliver list
- Restore an iopf_group back to the head of the fault->deliver list
Then, replace list_first_entry and list_for_each with those.

Lastly, move the fault->mutex closer to the fault->response and update its kdoc accordingly.

Fixes 07838f7fd529 ("iommufd: Add iommufd fault object")
---
 drivers/iommu/iommufd/iommufd_private.h | 29 ++++++++++++++--
 drivers/iommu/iommufd/fault.c           | 46 ++++++++++++++-----------
 2 files changed, 52 insertions(+), 23 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +27 -2
index b6d706cf2c66..0b1bafc7fd99 100644
@@ -443,14 +443,39 @@ struct iommufd_fault {
 	struct iommufd_ctx *ictx;
 	struct file *filep;
 
-	/* The lists of outstanding faults protected by below mutex. */
-	struct mutex mutex;
+	spinlock_t lock; /* protects the deliver list */
 	struct list_head deliver;
+	struct mutex mutex; /* serializes response flows */
 	struct xarray response;
 
 	struct wait_queue_head wait_queue;
 };
 
+/* Fetch the first node out of the fault->deliver list */
+static inline struct iopf_group *
+iommufd_fault_deliver_fetch(struct iommufd_fault *fault)
+{
+	struct list_head *list = &fault->deliver;
+	struct iopf_group *group = NULL;
+
+	spin_lock(&fault->lock);
+	if (!list_empty(list)) {
+		group = list_first_entry(list, struct iopf_group, node);
+		list_del(&group->node);
+	}
+	spin_unlock(&fault->lock);
+	return group;
+}
+
+/* Restore a node back to the head of the fault->deliver list */
+static inline void iommufd_fault_deliver_restore(struct iommufd_fault *fault,
+						 struct iopf_group *group)
+{
+	spin_lock(&fault->lock);
+	list_add(&group->node, &fault->deliver);
+	spin_unlock(&fault->lock);
+}
+
 struct iommufd_attach_handle {
 	struct iommu_attach_handle handle;
 	struct iommufd_device *idev;
drivers/iommu/iommufd/fault.c +25 -21

115 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#4 lore raw

Re: [PATCH rc v2 2/2] iommufd/fault: Use a separate spinlock to protect fault->deliver list

Quoted 10 lines · Nicolin Chen
drivers/iommu/iommufd/fault.c +1 -1
@@ -102,17 +102,18 @@ static void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt,
 					 struct iommufd_attach_handle *handle)
 {
 	struct iommufd_fault *fault = hwpt->fault;
-	struct iopf_group *group, *next;
+	struct iopf_group *group;
 	unsigned long index;
 
 	if (!fault)
 		return;

drivers/iommu/iommufd/fault.c static void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt, · line 116

Quoted 10 lines
drivers/iommu/iommufd/fault.c +4 -3
 
 	mutex_lock(&fault->mutex);
-	list_for_each_entry_safe(group, next, &fault->deliver, node) {
-		if (group->attach_handle != &handle->handle)
+	while ((group = iommufd_fault_deliver_fetch(fault))) {
+		if (group->attach_handle != &handle->handle) {
+			iommufd_fault_deliver_restore(fault, group);
 			continue;
-		list_del(&group->node);
+		}

I think this does not work, if we take the 'if attach_handle' leg then restore will put the same entry back into the front and the next fetch will pick it up and then it infinite loops without forward progress.

To make this algorithm work I suggest to do a list_for_each_entry_safe() under the spinlock and list_move each matching entry to a temporary list on the stack.

Then you can drop the spinlock and run over the temporary list doing this:

drivers/iommu/iommufd/fault.c void iommufd_fault_destroy(struct iommufd_object *obj) · line 225

Quoted 6 lines
drivers/iommu/iommufd/fault.c +0 -2
@@ -221,8 +222,7 @@ void iommufd_fault_destroy(struct iommufd_object *obj)
 	 * accessing this pointer. Therefore, acquiring the mutex here
 	 * is unnecessary.
 	 */
-	list_for_each_entry_safe(group, next, &fault->deliver, node) {
-		list_del(&group->node);

The comment above says there is no concurrency so no locking is necessary. I'd leave it alone and just leat it be the efficient list_for_each_entry_safe()

Jason

#5 lore raw

Re: [PATCH rc v2 2/2] iommufd/fault: Use a separate spinlock to protect fault->deliver list

Quoted 12 lines · Jason Gunthorpe
Quoted 11 lines · Nicolin Chen
drivers/iommu/iommufd/fault.c +1 -1
@@ -102,17 +102,18 @@ static void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt,
 					 struct iommufd_attach_handle *handle)
 {
 	struct iommufd_fault *fault = hwpt->fault;
-	struct iopf_group *group, *next;
+	struct iopf_group *group;
 	unsigned long index;
 
 	if (!fault)
 		return;
 
Quoted 13 lines

drivers/iommu/iommufd/fault.c static void iommufd_auto_response_faults(struct iommufd_hw_pagetable *hwpt, · line 116

Quoted 9 lines
drivers/iommu/iommufd/fault.c +4 -3
 	mutex_lock(&fault->mutex);
-	list_for_each_entry_safe(group, next, &fault->deliver, node) {
-		if (group->attach_handle != &handle->handle)
+	while ((group = iommufd_fault_deliver_fetch(fault))) {
+		if (group->attach_handle != &handle->handle) {
+			iommufd_fault_deliver_restore(fault, group);
 			continue;
-		list_del(&group->node);
+		}

I think this does not work, if we take the 'if attach_handle' leg then restore will put the same entry back into the front and the next fetch will pick it up and then it infinite loops without forward progress.

!! Needed more careful thinking. All these continues shouldn't be handled like those breaks.

Quoted 3 lines

To make this algorithm work I suggest to do a list_for_each_entry_safe() under the spinlock and list_move each matching entry to a temporary list on the stack.

Ack. I added a free_list for that.

Quoted 12 lines

Then you can drop the spinlock and run over the temporary list doing this:

drivers/iommu/iommufd/fault.c void iommufd_fault_destroy(struct iommufd_object *obj) · line 225

Quoted 6 lines
drivers/iommu/iommufd/fault.c +0 -2
@@ -221,8 +222,7 @@ void iommufd_fault_destroy(struct iommufd_object *obj)
 	 * accessing this pointer. Therefore, acquiring the mutex here
 	 * is unnecessary.
 	 */
-	list_for_each_entry_safe(group, next, &fault->deliver, node) {
-		list_del(&group->node);

The comment above says there is no concurrency so no locking is necessary. I'd leave it alone and just leat it be the efficient list_for_each_entry_safe()

Ack. Will send a v3.

Thanks
Nicolin