On Wed, Feb 06, 2019 at 17:21:15 +0000, Alex Bennée wrote: > Emilio G. Cota <c...@braap.org> writes: > > +/* > > + * Note: we index the bitmap with cpu->cpu_index + 1 so that the logic > > + * also works during early CPU initialization, when cpu->cpu_index is set > > to > > + * UNASSIGNED_CPU_INDEX == -1. > > + */ > > +static __thread DECLARE_BITMAP(cpu_lock_bitmap, > > CPU_LOCK_BITMAP_SIZE); > > I'm a little confused by this __thread bitmap. So by being a __thread > this is a per thread record (like __thread bool iothread_locked) of the > lock. However the test bellow: > > > + > > +bool no_cpu_mutex_locked(void) > > +{ > > + return bitmap_empty(cpu_lock_bitmap, CPU_LOCK_BITMAP_SIZE); > > +} > > which is used for asserts implies the only case we care about is > ensuring one thread doesn't take multiple cpu locks (which seems > reasonable). The only other test is used by cpu_mutex_locked to see if > the current thread has locked a given CPU index. > > Given that a thread can only be in two conditions: > > - no lock held > - holding lock for cpu->index > > Why the bitmap?
(snip) > If I've missed something I think the doc comment needs to be a bit more > explicit about our locking rules. The missing bit is that the bitmap is not only used for asserts; by the end of the series, we sometimes acquire all cpu locks (in CPU_FOREACH order to avoid deadlock), e.g. in pause_all_vcpus(). Given that this happens in patch 70, I think your comment here is reasonable. I'll update the commit message to explain why we add now the bitmap, even if it in this patch it isn't needed yet. > <snip> > > > > + /* prevent deadlock with CPU mutex */ > > + g_assert(no_cpu_mutex_locked()); > > + > > Technically asserts don't prevent this - they are just enforcing the > locking rules otherwise we would deadlock. Agreed. With that comment I mean "make sure we're following the locking order". Will fix. Thanks, E.