From: Paolo Bonzini <pbonzini@redhat.com>
To: "Alex Bennée" <alex.bennee@linaro.org>
Cc: jan.kiszka@siemens.com, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 5/9] memory: let address_space_rw/ld*/st* run outside the BQL
Date: Thu, 25 Jun 2015 10:13:43 +0200 [thread overview]
Message-ID: <558BB837.6090806@redhat.com> (raw)
In-Reply-To: <87twtxx7om.fsf@linaro.org>
On 24/06/2015 20:50, Alex Bennée wrote:
>
> Paolo Bonzini <pbonzini@redhat.com> writes:
>
>> On 24/06/2015 18:56, Alex Bennée wrote:
>>> This is where I get confused between the advantage of this over however
>>> same pid recursive locking. If you use recursive locking you don't need
>>> to add a bunch of state to remind you of when to release the lock.
>>>
>>> Am I missing something here?
>>
>> The semantics of recursive locking with respect to condition variables
>> are not clear. Either cond_wait releases all locks, and then the mutex
>> can be released when the code doesn't expect it to be, or cond_wait
>> doesn't release all locks and then you have deadlocks.
>>
>> So, recursive locking is okay if you don't have condition variables
>> attached to the lock (and if you cannot do without it), but
>> qemu_global_mutex does have them.
>
> Ahh OK, so I was missing something ;-)
>
>> QEMU has so far tried to use the solution that Stevens outlines here:
>> https://books.google.it/books?id=kCTMFpEcIOwC&pg=PA434
>
> Unfortunately I can't read that link but it sounds like I should get
> myself a copy of the book.
Try following the link from
https://en.wikipedia.org/wiki/Reentrant_mutex#References.
> I take it that approach wouldn't approve of:
>
> static __thread int iothread_lock_count;
>
> void qemu_mutex_lock_iothread(void)
> {
> if (iothread_lock_count == 0) {
> qemu_mutex_lock(&qemu_global_mutex);
> }
> iothread_lock_count++;
> }
>
> void qemu_mutex_unlock_iothread(void)
> {
> iothread_lock_count--;
> if (iothread_lock_count==0) {
> qemu_mutex_unlock(&qemu_global_mutex);
> }
> if (iothread_lock_count < 0) {
> fprintf(stderr,"%s: error, too many unlocks %d\n", __func__,
> iothread_lock_count);
> }
> }
>
> Which should achieve the same "only one lock held" semantics but still
> make the calling code a little less worried about tracking the state.
This is effectively implementing the "other" semantics: cond_wait always
drops the lock.
BTW, fine-grained recursive mutexes are bad for another reason: you can
think of "getting the mutex" as "ensuring all the data structure's
invariant are respected" (At the time you acquire the lock, no other
thread is modifying the state, so any invariant left at unlock time must
still be there). This is not true if you can get the mutex recursively.
But I'm honestly not sure how much of this argument applies for
something as coarse as the iothread lock.
The best argument I have against recursive mutexes is that it's really a
one-way street. Once you've decided to make a mutex recursive, it's
really hard to make it non-recursive.
Paolo
>>
>> Paolo
>
next prev parent reply other threads:[~2015-06-25 8:13 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-18 16:47 [Qemu-devel] [PATCH for-2.4 0/9] KVM: Do I/O outside BQL whenever possible Paolo Bonzini
2015-06-18 16:47 ` [Qemu-devel] [PATCH 1/9] main-loop: use qemu_mutex_lock_iothread consistently Paolo Bonzini
2015-06-23 13:49 ` Frederic Konrad
2015-06-23 13:56 ` Paolo Bonzini
2015-06-23 14:18 ` Frederic Konrad
2015-06-18 16:47 ` [Qemu-devel] [PATCH 2/9] main-loop: introduce qemu_mutex_iothread_locked Paolo Bonzini
2015-06-23 8:48 ` Fam Zheng
2015-06-18 16:47 ` [Qemu-devel] [PATCH 3/9] memory: Add global-locking property to memory regions Paolo Bonzini
2015-06-23 8:51 ` Fam Zheng
2015-06-18 16:47 ` [Qemu-devel] [PATCH 4/9] exec: pull qemu_flush_coalesced_mmio_buffer() into address_space_rw/ld*/st* Paolo Bonzini
2015-06-23 9:05 ` Fam Zheng
2015-06-23 9:12 ` Paolo Bonzini
2015-06-18 16:47 ` [Qemu-devel] [PATCH 5/9] memory: let address_space_rw/ld*/st* run outside the BQL Paolo Bonzini
2015-06-24 16:56 ` Alex Bennée
2015-06-24 17:21 ` Paolo Bonzini
2015-06-24 18:50 ` Alex Bennée
2015-06-25 8:13 ` Paolo Bonzini [this message]
2015-06-18 16:47 ` [Qemu-devel] [PATCH 6/9] kvm: First step to push iothread lock out of inner run loop Paolo Bonzini
2015-06-18 18:19 ` Christian Borntraeger
2015-06-23 9:26 ` Fam Zheng
2015-06-23 9:29 ` Paolo Bonzini
2015-06-23 9:45 ` Fam Zheng
2015-06-23 9:49 ` Paolo Bonzini
2015-06-18 16:47 ` [Qemu-devel] [PATCH 7/9] kvm: Switch to unlocked PIO Paolo Bonzini
2015-06-18 16:47 ` [Qemu-devel] [PATCH 8/9] acpi: mark PMTIMER as unlocked Paolo Bonzini
2015-06-18 16:47 ` [Qemu-devel] [PATCH 9/9] kvm: Switch to unlocked MMIO Paolo Bonzini
-- strict thread matches above, loose matches on Subject: below --
2015-06-24 16:25 [Qemu-devel] [PATCH for-2.4 v2 0/9] KVM: Do I/O outside BQL whenever possible Paolo Bonzini
2015-06-24 16:25 ` [Qemu-devel] [PATCH 5/9] memory: let address_space_rw/ld*/st* run outside the BQL Paolo Bonzini
2015-06-25 5:11 ` Fam Zheng
2015-06-25 7:47 ` Paolo Bonzini
2015-07-02 8:20 [Qemu-devel] [PATCH for-2.4 0/9 v3] KVM: Do I/O outside BQL whenever possible Paolo Bonzini
2015-07-02 8:20 ` [Qemu-devel] [PATCH 5/9] memory: let address_space_rw/ld*/st* run outside the BQL Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=558BB837.6090806@redhat.com \
--to=pbonzini@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=jan.kiszka@siemens.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).