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

From: Nicolin Chen
Date: Thu Jan 16 2025 - 20:09:23 EST


On Thu, Jan 16, 2025 at 04:34:06PM -0400, Jason Gunthorpe wrote:
> On Tue, Jan 14, 2025 at 10:56:00PM -0800, Nicolin Chen wrote:
> > @@ -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;
> >
> > 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.

> 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.

> Then you can drop the spinlock and run over the temporary list doing this:
>
> > @@ -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