On Tue, Dec 05, 2017 at 09:03:02PM -0200, Thiago Rafael Becker wrote: > On Tue, 5 Dec 2017, Matthew Wilcox wrote: > > It must be relatively common to sort an already-sorted array. I wonder > > if something like this patch would be worthwhile? > > The bug happens when two threads enter sort_groups for the same group info > in parallel, and one thread starts overwriting values that another thread > may already have "heapified" or sorted. > > Thread A Thread B > Enter groups_sort > Enter groups_sort > . > . > . > Return from groups_sort > . > . > . > Return from groups_sort > > Wouldn't this patch just make both threads see the structure as unsorted and > sort them? I'm sorry, I wasn't clear. I wasn't commenting on the original bug (and I believe your analysis to be correct unless there's some locking which prevents two calls to group_sort from happening at the same time). I was wondering about whether our sort() implementation is the best that it could possibly be, and whether having the trait of not modifying an already-sorted array is worthwhile (eg it would not cause cachelines to enter the dirty state if they were already clean).