From: lemonlime51@gmail.com (Matthias Bonne)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Question on mutex code
Date: Sun, 15 Mar 2015 01:08:59 +0200 [thread overview]
Message-ID: <5504BF8B.1090207@gmail.com> (raw)
In-Reply-To: <65060.1425999557@turing-police.cc.vt.edu>
On 03/10/15 16:59, Valdis.Kletnieks at vt.edu wrote:
> On Tue, 10 Mar 2015 14:03:59 +0100, Yann Droneaud said:
>
>>> Consider the following sequence of events:
>>>
>>> 0. Suppose a mutex is locked by task A and has no waiters.
>>>
>>> 1. Task B calls mutex_trylock().
>>>
>>> 2. mutex_trylock() calls the architecture-specific
>>> __mutex_fastpath_trylock(), with __mutex_trylock_slowpath() as
>>> fail_fn.
>>>
>>> 3. According to the description of __mutex_fastpath_trylock() (for
>>> example in include/asm-generic/mutex-dec.h), "if the architecture
>>> has no effective trylock variant, it should call the fail_fn
>>> spinlock-based trylock variant unconditionally". So
>>> __mutex_fastpath_trylock() may now call __mutex_trylock_slowpath().
>>>
>>> 4. Task A releases the mutex.
>>>
>>> 5. Task B, in __mutex_trylock_slowpath, executes:
>>>
>>> /* No need to trylock if the mutex is locked. */
>>> if (mutex_is_locked(lock))
>>> return 0;
>>>
>>> Since the mutex is no longer locked, the function continues.
>>>
>>> 6. Task C, which runs on a different cpu than task B, locks the mutex
>>> again.
>>>
>>> 7. Task B, in __mutex_trylock_slowpath(), continues:
>>>
>>> spin_lock_mutex(&lock->wait_lock, flags);
>
> B will spin here until C releases the lock.
>
> When that spin exits, C no longer holds the lock. Re-do the analysis
> from this point.
>
Thank you for the review.
I don't think B waits for C here - C holds the mutex (lock), not the
internal spinlock (lock->wait_lock). I might be wrong though.
WARNING: multiple messages have this Message-ID (diff)
From: Matthias Bonne <lemonlime51@gmail.com>
To: Valdis.Kletnieks@vt.edu
Cc: Yann Droneaud <ydroneaud@opteya.com>,
kernelnewbies@kernelnewbies.org, linux-kernel@vger.kernel.org,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>
Subject: Re: Question on mutex code
Date: Sun, 15 Mar 2015 01:08:59 +0200 [thread overview]
Message-ID: <5504BF8B.1090207@gmail.com> (raw)
In-Reply-To: <65060.1425999557@turing-police.cc.vt.edu>
On 03/10/15 16:59, Valdis.Kletnieks@vt.edu wrote:
> On Tue, 10 Mar 2015 14:03:59 +0100, Yann Droneaud said:
>
>>> Consider the following sequence of events:
>>>
>>> 0. Suppose a mutex is locked by task A and has no waiters.
>>>
>>> 1. Task B calls mutex_trylock().
>>>
>>> 2. mutex_trylock() calls the architecture-specific
>>> __mutex_fastpath_trylock(), with __mutex_trylock_slowpath() as
>>> fail_fn.
>>>
>>> 3. According to the description of __mutex_fastpath_trylock() (for
>>> example in include/asm-generic/mutex-dec.h), "if the architecture
>>> has no effective trylock variant, it should call the fail_fn
>>> spinlock-based trylock variant unconditionally". So
>>> __mutex_fastpath_trylock() may now call __mutex_trylock_slowpath().
>>>
>>> 4. Task A releases the mutex.
>>>
>>> 5. Task B, in __mutex_trylock_slowpath, executes:
>>>
>>> /* No need to trylock if the mutex is locked. */
>>> if (mutex_is_locked(lock))
>>> return 0;
>>>
>>> Since the mutex is no longer locked, the function continues.
>>>
>>> 6. Task C, which runs on a different cpu than task B, locks the mutex
>>> again.
>>>
>>> 7. Task B, in __mutex_trylock_slowpath(), continues:
>>>
>>> spin_lock_mutex(&lock->wait_lock, flags);
>
> B will spin here until C releases the lock.
>
> When that spin exits, C no longer holds the lock. Re-do the analysis
> from this point.
>
Thank you for the review.
I don't think B waits for C here - C holds the mutex (lock), not the
internal spinlock (lock->wait_lock). I might be wrong though.
next prev parent reply other threads:[~2015-03-14 23:08 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-03-04 0:13 Question on mutex code Matthias Bonne
2015-03-10 13:03 ` Yann Droneaud
2015-03-10 13:03 ` Yann Droneaud
2015-03-10 14:59 ` Valdis.Kletnieks at vt.edu
2015-03-10 14:59 ` Valdis.Kletnieks
2015-03-14 23:08 ` Matthias Bonne [this message]
2015-03-14 23:08 ` Matthias Bonne
2015-03-14 23:05 ` Matthias Bonne
2015-03-14 23:05 ` Matthias Bonne
2015-03-15 1:03 ` Davidlohr Bueso
2015-03-15 1:04 ` Davidlohr Bueso
2015-03-15 1:09 ` Davidlohr Bueso
2015-03-15 1:10 ` Davidlohr Bueso
2015-03-15 21:49 ` Matthias Bonne
2015-03-15 21:49 ` Matthias Bonne
2015-03-15 22:10 ` Rabin Vincent
2015-03-15 22:11 ` Rabin Vincent
2015-03-16 3:40 ` Matthias Bonne
2015-03-16 3:40 ` Matthias Bonne
2015-03-15 22:18 ` Davidlohr Bueso
2015-03-15 22:19 ` Davidlohr Bueso
2015-03-15 22:23 ` Davidlohr Bueso
2015-03-15 22:24 ` Davidlohr Bueso
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=5504BF8B.1090207@gmail.com \
--to=lemonlime51@gmail.com \
--cc=kernelnewbies@lists.kernelnewbies.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.