qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH v6 03/73] cpu: introduce cpu_mutex_lock/unlock


From: Emilio G. Cota
Subject: Re: [Qemu-devel] [PATCH v6 03/73] cpu: introduce cpu_mutex_lock/unlock
Date: Wed, 6 Feb 2019 15:02:09 -0500
User-agent: Mutt/1.9.4 (2018-02-28)

On Wed, Feb 06, 2019 at 17:21:15 +0000, Alex Bennée wrote:
> Emilio G. Cota <address@hidden> 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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]