futex: replace bare barrier() with more lightweight READ_ONCE()
PATCH 1/1 futex: replace bare barrier() with more lightweight READ_ONCE()
Commit e91467ecd1ef ("bug in futex unqueue_me") introduces a barrier() in unqueue_me(), to address below problem.
The scenario is like this:
====================
original code:
retry:
lock_ptr = q->lock_ptr;
if (lock_ptr != 0) {
spin_lock(lock_ptr)
if (unlikely(lock_ptr != q->lock_ptr)) {
spin_unlock(lock_ptr);
goto retry;
}
...
}====================
It was observed that compiler generates code that is equivalent to:
retry:
if (q->lock_ptr != 0) {
spin_lock(q->lock_ptr)
if (unlikely(lock_ptr != q->lock_ptr)) {
spin_unlock(lock_ptr);
goto retry;
}
...
}since q->lock_ptr might change between the test of non-nullness and spin_lock(), the double load will cause trouble. So that commit uses a barrier() to prevent this.
This patch replaces this bare barrier() with a READ_ONCE().
The reasons are:
1) READ_ONCE() is a more weak form of barrier() that affect only the specific accesses, while barrier() is a more general compiler level memroy barrier. READ_ONCE() was not available at that time when that patch was written.
2) READ_ONCE() which could be more informative by its name, while a bare barrier() without comment leads to quite a bit of perplexity.
Assembly code before(barrier version) and after this patch(READ_ONCE version) are the same:
====================
Before(barrier version):
unqueue_me():linux/kernel/futex.c:1930
1df6: 4c 8b bd 28 ff ff ff mov -0xd8(%rbp),%r15
linux/kernel/futex.c:1932
1dfd: 4d 85 ff test %r15,%r15
1e00: 0f 84 5c 01 00 00 je 1f62 <futex_wait+0x292>
spin_lock():
linux/include/linux/spinlock.h:302
1e06: 4c 89 ff mov %r15,%rdi
1e09: e8 00 00 00 00 callq 1e0e <futex_wait+0x13e>====================After(READ_ONCE version):
__read_once_size():linux/include/linux/compiler.h:218
1df6: 4c 8b bd 28 ff ff ff mov -0xd8(%rbp),%r15
unqueue_me():
linux/kernel/futex.c:1935
1dfd: 4d 85 ff test %r15,%r15
1e00: 0f 84 5c 01 00 00 je 1f62 <futex_wait+0x292>
spin_lock():
linux/include/linux/spinlock.h:302
1e06: 4c 89 ff mov %r15,%rdi
1e09: e8 00 00 00 00 callq 1e0e <futex_wait+0x13e>Code size is also the same.
--- kernel/futex.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/futex.c +5 -2
@@ -1927,8 +1927,11 @@ static int unqueue_me(struct futex_q *q)/* In the common case we don't take the spinlock, which is nice. */ retry: - lock_ptr = q->lock_ptr; -barrier (); +/* +* Prevent the compiler to read q->lock_ptr twice (if and spin_lock), +* or that would cause trouble since q->lock_ptr can change in between. +*/ + lock_ptr =READ_ONCE (q->lock_ptr);if (lock_ptr !=NULL ) {spin_lock (lock_ptr);/*
Signature · 1 line
2.4.3
Quoted 77 lines · Jianyu Zhan
Commit
e91467ecd1ef("bug in futexunqueue_me") introduces abarrier()inunqueue_me(), to address below problem.The scenario is like this:
====================
original code:retry:
lock_ptr = q->lock_ptr; if (lock_ptr != 0) { spin_lock(lock_ptr) if (unlikely(lock_ptr != q->lock_ptr)) { spin_unlock(lock_ptr); goto retry; } ... }====================
It was observed that compiler generates code that is equivalent to:retry:
if (q->lock_ptr != 0) { spin_lock(q->lock_ptr) if (unlikely(lock_ptr != q->lock_ptr)) { spin_unlock(lock_ptr); goto retry; } ... }since
q->lock_ptrmight change between the test of non-nullness andspin_lock(), the double load will cause trouble. So that commit uses abarrier()to prevent this.This patch replaces this bare
barrier()with aREAD_ONCE().The reasons are:
1)
READ_ONCE()is a more weak form ofbarrier()that affect only the specific accesses, whilebarrier()is a more general compiler level memroy barrier.READ_ONCE()was not available at that time when that patch was written.2)
READ_ONCE()which could be more informative by its name, while a barebarrier()without comment leads to quite a bit of perplexity.Assembly code before(barrier version) and after this
patch(READ_ONCE version)are the same:====================
Before(barrier version):
unqueue_me():linux/kernel/futex.c:19301df6: 4c 8b bd 28 ff ff ff mov -0xd8(%rbp),%r15 linux/kernel/futex.c:1932 1dfd: 4d 85 ff test %r15,%r15 1e00: 0f 84 5c 01 00 00 je 1f62 <futex_wait+0x292> spin_lock(): linux/include/linux/spinlock.h:302 1e06: 4c 89 ff mov %r15,%rdi 1e09: e8 00 00 00 00 callq 1e0e <futex_wait+0x13e>====================
After(READ_ONCE version):
__read_once_size():linux/include/linux/compiler.h:2181df6: 4c 8b bd 28 ff ff ff mov -0xd8(%rbp),%r15 unqueue_me(): linux/kernel/futex.c:1935 1dfd: 4d 85 ff test %r15,%r15 1e00: 0f 84 5c 01 00 00 je 1f62 <futex_wait+0x292> spin_lock(): linux/include/linux/spinlock.h:302 1e06: 4c 89 ff mov %r15,%rdi 1e09: e8 00 00 00 00 callq 1e0e <futex_wait+0x13e>
Quoted 3 lines
Code size is also the same.
Suggested-by Darren Hart <[email protected]>
A simple suggested-by may cause people to think this patch was my idea, which would not be accurate. You can credit people for recommendations in the patch changelog ("Since v1: ..." below the --- line).
Quoted 11 lines
Signed-off-by Jianyu Zhan <[email protected]>--- kernel/futex.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)kernel/futex.c +0
-0
kernel/futex.c static int unqueue_me(struct futex_q *q) · line 1933
Quoted 9 lines
kernel/futex.c +4
-2@@ -1927,8 +1927,11 @@ static int unqueue_me(struct futex_q *q)/* In the common case we don't take the spinlock, which is nice. */ retry: - lock_ptr = q->lock_ptr; -barrier (); +/* +* Prevent the compiler to read q->lock_ptr twice (if and spin_lock), +* or that would cause trouble since q->lock_ptr can change in between. +*/
I thought I provided a corrected comment block.... maybe I didn't. We have been working on improving the futex documentation, so we're paying close attention to terminology as well as grammar. This one needs a couple minor tweaks. I suggest:
/*
* Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and
* optimizing lock_ptr out of the logic below.
*/
The bit about q->lock_ptr possibly changing is already covered by the large comment block below the spin_lock(lock_ptr) call.
With the above change, I'll add my Reviewed-by.
Thanks,
Signature · 2 lines
Darren Hart Intel Open Source Technology Center
Quoted 11 lines · Darren Hart
I thought I provided a corrected comment block.... maybe I didn't. We have been working on improving the futex documentation, so we're paying close attention to terminology as well as grammar. This one needs a couple minor tweaks. I suggest:
/*
* UseREAD_ONCEto forbid the compiler from reloadingq->lock_ptrand
* optimizinglock_ptrout of the logic below.
*/The bit about
q->lock_ptrpossibly changing is already covered by the large comment block below thespin_lock(lock_ptr)call.
The large comment block is explaining the why the retry logic is required. To achieve this semantic requirement, the READ_ONCE is needed to prevent compiler optimizing it by doing double loads.
So I think the comment above should explain this tricky part.
Quoted 1 line
/* Use
READ_ONCEto forbid the compiler from reloadingq->lock_ptrinspin_lock()*/
And as for preventing from optimizing the lock_ptr out of the retry code block, I have consult
Paul Mckenney, he suggests one more READ_ONCE should be added here:
if (unlikely(lock_ptr != READ_ONCE(q->lock_ptr))) { <------------------------------
spin_unlock(lock_ptr);
goto retry;}
And I think this are two problem, and should be separated into two patches?
Regards,
Jianyu Zhan
Quoted 10 lines · Jianyu Zhan
Quoted 9 lines · Darren Hart
I thought I provided a corrected comment block.... maybe I didn't. We have been working on improving the futex documentation, so we're paying close attention to terminology as well as grammar. This one needs a couple minor tweaks. I suggest:
/*
* UseREAD_ONCEto forbid the compiler from reloadingq->lock_ptrand
* optimizinglock_ptrout of the logic below.
*/
Quoted 8 lines
Quoted 2 lines
The bit about
q->lock_ptrpossibly changing is already covered by the large comment block below thespin_lock(lock_ptr)call.The large comment block is explaining the why the retry logic is required. To achieve this semantic requirement, the
READ_ONCEis needed to prevent compiler optimizing it by doing double loads.So I think the comment above should explain this tricky part.
Fair point. Consider:
/*
* q->lock_ptr can change between this read and the following spin_lock.
* Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and
* optimizing lock_ptr out of the logic below.
*/
Quoted 6 lines
Quoted 2 lines
/* Use
READ_ONCEto forbid the compiler from reloadingq->lock_ptrinspin_lock()*/And as for preventing from optimizing the
lock_ptrout of the retry code block, I have consult
Paul Mckenney, he suggests one moreREAD_ONCEshould be added here:
Let's keep this discussion together so we have a record of the justification.
+Paul McKenney
Paul, my understanding was that spin_lock was a CPU memory barrier, which in turn is an implicit compiler barrier (aka barrier()), of which READ_ONCE is described as a weaker form. Reviewing this, I realize the scope of barrier() wasn't clear to me. It seems while barrier() ensures ordering, it does not offer the same guarantee regarding reloading that READ_ONCE offers. So READ_ONCE is not strictly a weaker form of barrier() as I had gathered from a spotty reading of memory-barriers.txt, but it also offers guarantees regarding memory references that barrier() does not.
Correct?
Quoted 8 lines
if (
unlikely(lock_ptr != READ_ONCE(q->lock_ptr))) { <------------------------------spin_unlock(lock_ptr); goto retry;}
And I think this are two problem, and should be separated into two patches?
Yes (pending results of the conversation above).
Signature · 2 lines
Darren Hart Intel Open Source Technology Center
Quoted 51 lines · Darren Hart
Quoted 10 lines · Jianyu Zhan
Quoted 9 lines · Darren Hart
I thought I provided a corrected comment block.... maybe I didn't. We have been working on improving the futex documentation, so we're paying close attention to terminology as well as grammar. This one needs a couple minor tweaks. I suggest:
/*
* UseREAD_ONCEto forbid the compiler from reloadingq->lock_ptrand
* optimizinglock_ptrout of the logic below.
*/Quoted 8 lines
Quoted 2 lines
The bit about
q->lock_ptrpossibly changing is already covered by the large comment block below thespin_lock(lock_ptr)call.The large comment block is explaining the why the retry logic is required. To achieve this semantic requirement, the
READ_ONCEis needed to prevent compiler optimizing it by doing double loads.So I think the comment above should explain this tricky part.
Fair point. Consider:
/*
*q->lock_ptrcan change between this read and the followingspin_lock.
* UseREAD_ONCEto forbid the compiler from reloadingq->lock_ptrand
* optimizinglock_ptrout of the logic below.
*/Quoted 6 lines
Quoted 2 lines
/* Use
READ_ONCEto forbid the compiler from reloadingq->lock_ptrinspin_lock()*/And as for preventing from optimizing the
lock_ptrout of the retry code block, I have consult
Paul Mckenney, he suggests one moreREAD_ONCEshould be added here:Let's keep this discussion together so we have a record of the justification.
+Paul McKenney
Paul, my understanding was that
spin_lockwas a CPU memory barrier, which in turn is an implicit compiler barrier (akabarrier()), of whichREAD_ONCEis described as a weaker form. Reviewing this, I realize the scope ofbarrier()wasn't clear to me. It seems whilebarrier()ensures ordering, it does not offer the same guarantee regarding reloading thatREAD_ONCEoffers. SoREAD_ONCEis not strictly a weaker form ofbarrier()as I had gathered from a spotty reading of memory-barriers.txt, but it also offers guarantees regarding memory references thatbarrier()does not.
Quoted 1 line
Correct?
If q->lock_ptr is never changed except under that lock, then there is indeed no reason for the ACCESS_ONCE().
So, is q->lock_ptr ever changed while the lock is -not- held? If so, I suggest that you put an ACCESS_ONCE() there.
Thanx, Paul
Quoted 14 lines
Quoted 7 lines
if (
unlikely(lock_ptr != READ_ONCE(q->lock_ptr))) { <------------------------------spin_unlock(lock_ptr); goto retry;}
And I think this are two problem, and should be separated into two patches?
Yes (pending results of the conversation above).
Signature · 2 lines
Darren Hart Intel Open Source Technology Center
Quoted 52 lines · Paul McKenney
Quoted 51 lines · Darren Hart
Quoted 10 lines · Jianyu Zhan
Quoted 9 lines · Darren Hart
I thought I provided a corrected comment block.... maybe I didn't. We have been working on improving the futex documentation, so we're paying close attention to terminology as well as grammar. This one needs a couple minor tweaks. I suggest:
/*
* UseREAD_ONCEto forbid the compiler from reloadingq->lock_ptrand
* optimizinglock_ptrout of the logic below.
*/Quoted 8 lines
Quoted 2 lines
The bit about
q->lock_ptrpossibly changing is already covered by the large comment block below thespin_lock(lock_ptr)call.The large comment block is explaining the why the retry logic is required. To achieve this semantic requirement, the
READ_ONCEis needed to prevent compiler optimizing it by doing double loads.So I think the comment above should explain this tricky part.
Fair point. Consider:
/*
*q->lock_ptrcan change between this read and the followingspin_lock.
* UseREAD_ONCEto forbid the compiler from reloadingq->lock_ptrand
* optimizinglock_ptrout of the logic below.
*/Quoted 6 lines
Quoted 2 lines
/* Use
READ_ONCEto forbid the compiler from reloadingq->lock_ptrinspin_lock()*/And as for preventing from optimizing the
lock_ptrout of the retry code block, I have consult
Paul Mckenney, he suggests one moreREAD_ONCEshould be added here:Let's keep this discussion together so we have a record of the justification.
+Paul McKenney
Paul, my understanding was that
spin_lockwas a CPU memory barrier, which in turn is an implicit compiler barrier (akabarrier()), of whichREAD_ONCEis described as a weaker form. Reviewing this, I realize the scope ofbarrier()wasn't clear to me. It seems whilebarrier()ensures ordering, it does not offer the same guarantee regarding reloading thatREAD_ONCEoffers. SoREAD_ONCEis not strictly a weaker form ofbarrier()as I had gathered from a spotty reading of memory-barriers.txt, but it also offers guarantees regarding memory references thatbarrier()does not.
Quoted 4 lines
Quoted 1 line
Correct?
If
q->lock_ptris never changed except under that lock, then there is indeed no reason for theACCESS_ONCE().
The only location where a q->lock_ptr is updated without that lock being held is in queue_lock(). This is safe as the futex_q is not yet queued onto an hb until after the lock is held (so unqueue_me() cannot race with queue_lock()).
Quoted 2 lines
So, is
q->lock_ptrever changed while the lock is -not- held? If so, I suggest that you put anACCESS_ONCE()there.
It is not.
Signature · 2 lines
Darren Hart Intel Open Source Technology Center
Quoted 58 lines · Darren Hart
Quoted 52 lines · Paul McKenney
Quoted 51 lines · Darren Hart
Quoted 10 lines · Jianyu Zhan
Quoted 9 lines · Darren Hart
I thought I provided a corrected comment block.... maybe I didn't. We have been working on improving the futex documentation, so we're paying close attention to terminology as well as grammar. This one needs a couple minor tweaks. I suggest:
/*
* UseREAD_ONCEto forbid the compiler from reloadingq->lock_ptrand
* optimizinglock_ptrout of the logic below.
*/Quoted 8 lines
Quoted 2 lines
The bit about
q->lock_ptrpossibly changing is already covered by the large comment block below thespin_lock(lock_ptr)call.The large comment block is explaining the why the retry logic is required. To achieve this semantic requirement, the
READ_ONCEis needed to prevent compiler optimizing it by doing double loads.So I think the comment above should explain this tricky part.
Fair point. Consider:
/*
*q->lock_ptrcan change between this read and the followingspin_lock.
* UseREAD_ONCEto forbid the compiler from reloadingq->lock_ptrand
* optimizinglock_ptrout of the logic below.
*/Quoted 6 lines
Quoted 2 lines
/* Use
READ_ONCEto forbid the compiler from reloadingq->lock_ptrinspin_lock()*/And as for preventing from optimizing the
lock_ptrout of the retry code block, I have consult
Paul Mckenney, he suggests one moreREAD_ONCEshould be added here:Let's keep this discussion together so we have a record of the justification.
+Paul McKenney
Paul, my understanding was that
spin_lockwas a CPU memory barrier, which in turn is an implicit compiler barrier (akabarrier()), of whichREAD_ONCEis described as a weaker form. Reviewing this, I realize the scope ofbarrier()wasn't clear to me. It seems whilebarrier()ensures ordering, it does not offer the same guarantee regarding reloading thatREAD_ONCEoffers. SoREAD_ONCEis not strictly a weaker form ofbarrier()as I had gathered from a spotty reading of memory-barriers.txt, but it also offers guarantees regarding memory references thatbarrier()does not.Quoted 4 lines
Quoted 1 line
Correct?
If
q->lock_ptris never changed except under that lock, then there is indeed no reason for theACCESS_ONCE().
Quoted 8 lines
The only location where a
q->lock_ptris updated without that lock being held is inqueue_lock(). This is safe as thefutex_qis not yet queued onto an hb until after the lock is held (sounqueue_me()cannot race withqueue_lock()).Quoted 2 lines
So, is
q->lock_ptrever changed while the lock is -not- held? If so, I suggest that you put anACCESS_ONCE()there.It is not.
If I followed that correctly, then I agree that you don't need an ACCESS_ONCE() in this case.
Thanx, Paul
Quoted 63 lines · Paul McKenney
Quoted 62 lines · Darren Hart
Quoted 52 lines · Paul McKenney
Quoted 51 lines · Darren Hart
Quoted 10 lines · Jianyu Zhan
Quoted 9 lines · Darren Hart
I thought I provided a corrected comment block.... maybe I didn't. We have been working on improving the futex documentation, so we're paying close attention to terminology as well as grammar. This one needs a couple minor tweaks. I suggest:
/*
* UseREAD_ONCEto forbid the compiler from reloadingq->lock_ptrand
* optimizinglock_ptrout of the logic below.
*/Quoted 8 lines
Quoted 2 lines
The bit about
q->lock_ptrpossibly changing is already covered by the large comment block below thespin_lock(lock_ptr)call.The large comment block is explaining the why the retry logic is required. To achieve this semantic requirement, the
READ_ONCEis needed to prevent compiler optimizing it by doing double loads.So I think the comment above should explain this tricky part.
Fair point. Consider:
/*
*q->lock_ptrcan change between this read and the followingspin_lock.
* UseREAD_ONCEto forbid the compiler from reloadingq->lock_ptrand
* optimizinglock_ptrout of the logic below.
*/Quoted 6 lines
Quoted 2 lines
/* Use
READ_ONCEto forbid the compiler from reloadingq->lock_ptrinspin_lock()*/And as for preventing from optimizing the
lock_ptrout of the retry code block, I have consult
Paul Mckenney, he suggests one moreREAD_ONCEshould be added here:Let's keep this discussion together so we have a record of the justification.
+Paul McKenney
Paul, my understanding was that
spin_lockwas a CPU memory barrier, which in turn is an implicit compiler barrier (akabarrier()), of whichREAD_ONCEis described as a weaker form. Reviewing this, I realize the scope ofbarrier()wasn't clear to me. It seems whilebarrier()ensures ordering, it does not offer the same guarantee regarding reloading thatREAD_ONCEoffers. SoREAD_ONCEis not strictly a weaker form ofbarrier()as I had gathered from a spotty reading of memory-barriers.txt, but it also offers guarantees regarding memory references thatbarrier()does not.Quoted 4 lines
Quoted 1 line
Correct?
If
q->lock_ptris never changed except under that lock, then there is indeed no reason for theACCESS_ONCE().The only location where a
q->lock_ptris updated without that lock being held is inqueue_lock(). This is safe as thefutex_qis not yet queued onto an hb until after the lock is held (sounqueue_me()cannot race withqueue_lock()).
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
So, is
q->lock_ptrever changed while the lock is -not- held? If so, I suggest that you put anACCESS_ONCE()there.It is not.
If I followed that correctly, then I agree that you don't need an
ACCESS_ONCE()in this case.
Thanks for offering your time and expertise Paul. It's a major effort every time I open memory-barriers.txt :-)
Signature · 2 lines
Darren Hart Intel Open Source Technology Center
PATCH v3 1/1 futex: replace bare barrier() with more lightweight READ_ONCE()
In reply to Darren HartCommit e91467ecd1ef ("bug in futex unqueue_me") introduces a barrier() in unqueue_me(), to address below problem.
The scenario is like this:
====================
original code:
retry:
lock_ptr = q->lock_ptr;
if (lock_ptr != 0) {
spin_lock(lock_ptr)
if (unlikely(lock_ptr != q->lock_ptr)) {
spin_unlock(lock_ptr);
goto retry;
}
...
}====================
It was observed that compiler generates code that is equivalent to:
retry:
if (q->lock_ptr != 0) {
spin_lock(q->lock_ptr)
if (unlikely(lock_ptr != q->lock_ptr)) {
spin_unlock(lock_ptr);
goto retry;
}
...
}since q->lock_ptr might change between the test of non-nullness and spin_lock(), the double load will cause trouble. So that commit uses a barrier() to prevent this.
This patch replaces this bare barrier() with a READ_ONCE().
The reasons are:
1) READ_ONCE() is a more weak form of barrier() that affect only the specific accesses, while barrier() is a more general compiler level memroy barrier. READ_ONCE() was not available at that time when that patch was written.
2) READ_ONCE() which could be more informative by its name, while a bare barrier() without comment leads to quite a bit of perplexity.
Assembly code before(barrier version) and after this patch(READ_ONCE version) are the same:
====================
Before(barrier version):
unqueue_me():linux/kernel/futex.c:1930
1df6: 4c 8b bd 28 ff ff ff mov -0xd8(%rbp),%r15
linux/kernel/futex.c:1932
1dfd: 4d 85 ff test %r15,%r15
1e00: 0f 84 5c 01 00 00 je 1f62 <futex_wait+0x292>
spin_lock():
linux/include/linux/spinlock.h:302
1e06: 4c 89 ff mov %r15,%rdi
1e09: e8 00 00 00 00 callq 1e0e <futex_wait+0x13e>====================After(READ_ONCE version):
__read_once_size():linux/include/linux/compiler.h:218
1df6: 4c 8b bd 28 ff ff ff mov -0xd8(%rbp),%r15
unqueue_me():
linux/kernel/futex.c:1935
1dfd: 4d 85 ff test %r15,%r15
1e00: 0f 84 5c 01 00 00 je 1f62 <futex_wait+0x292>
spin_lock():
linux/include/linux/spinlock.h:302
1e06: 4c 89 ff mov %r15,%rdi
1e09: e8 00 00 00 00 callq 1e0e <futex_wait+0x13e>Code size is also the same.
Many thanks to Darren Hart <[email protected]> for reviewing and suggestion.
--- kernel/futex.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/futex.c +6 -2
@@ -1927,8 +1927,12 @@ static int unqueue_me(struct futex_q *q)/* In the common case we don't take the spinlock, which is nice. */ retry: - lock_ptr = q->lock_ptr; -barrier (); +/* +* q->lock_ptr can change between this read and the following spin_lock. +* Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and +* optimizing lock_ptr out of the logic below. +*/ + lock_ptr =READ_ONCE (q->lock_ptr);if (lock_ptr !=NULL ) {spin_lock (lock_ptr);/*
Signature · 1 line
2.4.3
Quoted 77 lines · Jianyu Zhan
Commit
e91467ecd1ef("bug in futexunqueue_me") introduces abarrier()inunqueue_me(), to address below problem.The scenario is like this:
====================
original code:retry:
lock_ptr = q->lock_ptr; if (lock_ptr != 0) { spin_lock(lock_ptr) if (unlikely(lock_ptr != q->lock_ptr)) { spin_unlock(lock_ptr); goto retry; } ... }====================
It was observed that compiler generates code that is equivalent to:retry:
if (q->lock_ptr != 0) { spin_lock(q->lock_ptr) if (unlikely(lock_ptr != q->lock_ptr)) { spin_unlock(lock_ptr); goto retry; } ... }since
q->lock_ptrmight change between the test of non-nullness andspin_lock(), the double load will cause trouble. So that commit uses abarrier()to prevent this.This patch replaces this bare
barrier()with aREAD_ONCE().The reasons are:
1)
READ_ONCE()is a more weak form ofbarrier()that affect only the specific accesses, whilebarrier()is a more general compiler level memroy barrier.READ_ONCE()was not available at that time when that patch was written.2)
READ_ONCE()which could be more informative by its name, while a barebarrier()without comment leads to quite a bit of perplexity.Assembly code before(barrier version) and after this
patch(READ_ONCE version)are the same:====================
Before(barrier version):
unqueue_me():linux/kernel/futex.c:19301df6: 4c 8b bd 28 ff ff ff mov -0xd8(%rbp),%r15 linux/kernel/futex.c:1932 1dfd: 4d 85 ff test %r15,%r15 1e00: 0f 84 5c 01 00 00 je 1f62 <futex_wait+0x292> spin_lock(): linux/include/linux/spinlock.h:302 1e06: 4c 89 ff mov %r15,%rdi 1e09: e8 00 00 00 00 callq 1e0e <futex_wait+0x13e>====================
After(READ_ONCE version):
__read_once_size():linux/include/linux/compiler.h:2181df6: 4c 8b bd 28 ff ff ff mov -0xd8(%rbp),%r15 unqueue_me(): linux/kernel/futex.c:1935 1dfd: 4d 85 ff test %r15,%r15 1e00: 0f 84 5c 01 00 00 je 1f62 <futex_wait+0x292> spin_lock(): linux/include/linux/spinlock.h:302 1e06: 4c 89 ff mov %r15,%rdi 1e09: e8 00 00 00 00 callq 1e0e <futex_wait+0x13e>
Quoted 7 lines
Code size is also the same.
Many thanks to Darren Hart <[email protected]> for reviewing and suggestion.
Signed-off-by Jianyu Zhan <[email protected]>
Thanks for sticking with this Jianyu.
Quoted 27 lines
--- kernel/futex.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)kernel/futex.c +6
-2@@ -1927,8 +1927,12 @@ static int unqueue_me(struct futex_q *q)/* In the common case we don't take the spinlock, which is nice. */ retry: - lock_ptr = q->lock_ptr; -barrier (); +/* +* q->lock_ptr can change between this read and the following spin_lock. +* Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and +* optimizing lock_ptr out of the logic below. +*/ + lock_ptr =READ_ONCE (q->lock_ptr);if (lock_ptr !=NULL ) {spin_lock (lock_ptr);/* Signature · 1 line
2.4.3
Signature · 2 lines
Darren Hart Intel Open Source Technology Center
tip-bot for Jianyu Zhan
merge bot[tip:locking/core] futex: Replace barrier() in unqueue_me() with READ_ONCE()
In reply to Jianyu Zhanfutex: Replace barrier() in unqueue_me() with READ_ONCE()
Commit-ID: 29b75eb2d56a714190a93d7be4525e617591077a Gitweb: http://git.kernel.org/tip/29b75eb2d56a714190a93d7be4525e617591077a Author: Jianyu Zhan <[email protected]> AuthorDate: Mon, 7 Mar 2016 09:32:24 +0800 Committer: Thomas Gleixner <[email protected]> CommitDate: Tue, 8 Mar 2016 17:04:02 +0100
futex: Replace barrier() in unqueue_me() with READ_ONCE()
Commit e91467ecd1ef ("bug in futex unqueue_me") introduced a barrier() in unqueue_me() to prevent the compiler from rereading the lock pointer which might change after a check for NULL.
Replace the barrier() with a READ_ONCE() for the following reasons:
1) READ_ONCE() is a weaker form of barrier() that affects only the specific load operation, while barrier() is a general compiler level memory barrier. READ_ONCE() was not available at the time when the barrier was added.
2) Aside of that READ_ONCE() is descriptive and self explainatory while a barrier without comment is not clear to the casual reader.
No functional change.
[ tglx: Massaged changelog ]
--- kernel/futex.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/futex.c +6 -2
@@ -2010,8 +2010,12 @@ static int unqueue_me(struct futex_q *q)/* In the common case we don't take the spinlock, which is nice. */ retry: - lock_ptr = q->lock_ptr; -barrier (); +/* +* q->lock_ptr can change between this read and the following spin_lock. +* Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and +* optimizing lock_ptr out of the logic below. +*/ + lock_ptr =READ_ONCE (q->lock_ptr);if (lock_ptr !=NULL ) {spin_lock (lock_ptr);/*