On Sat, 2012-05-19 at 11:46 +0200, Joel Reardon wrote: > > Take the work_sem at the beginning. Release at the very end. > > > > Then you can do something like this: > > > > int found = 1; > > > > while (found) { > > found = 0; > > > > spin_lock(&ubi->wl_lock); > > list_for_each_entry(wrk, tmp, &ubi->works, list) { > > if (wrk->lnum == lnum) { > > list_del(&wrk->list); > > ubi->works_count -= 1; > > ubi_assert(ubi->works_count >= 0); > > spin_unlock(&ubi->wl_lock); > > > > err = wrk->func(ubi, wrk, 0); > > if (err) > > return err; > > > > spin_lock(&ubi->wl_lock); > > found = 1; > > break; > > } > > spin_unlock(&ubi->wl_lock); > > } > > > > > If I use list_for_each_entry_safe(), it protects against deleting as it > iterates. Yes, but do not be confused, it does not protect against concurrent deleting. I would not use word "protect" at all - it is just a version of list_for_each_entry() which we use when we delete list elements inside the loop. It requires another temporary variable. > If I take the work_sem first, is it okay to do a simple > traversal instead of one traversal per removed item? No, work_sem does not protect the list. It is just a bit unusual way of syncing with all the processes which are performing UBI works. Take a closer look - there is only one single place where we take it for writing - ubi_wl_flush(). Everywhere else we take it for reading which means that many processes may enter the critical section, and may concurrently add/remove elements to/from the list. The list is protected by the spinlock. > Even if another > thread adds new work for the same vol_id/lnum, its okay, because the > caller of this function only cares about vol_id/lnums erasures that > it knows are currently on the worklist. If you walk the list and read pointers, and someone else modifies them, you may be in trouble. You cannot traverse the list without the spinlock. And once you dropped the spinlock - you have to start over because your 'wrk' pointer may point to a non-existing object, because this object might have been already freed. This is why I added 2 loops. -- Best Regards, Artem Bityutskiy