CodeSamples/defer: Fix hazard torture
PATCH v2 1/1 CodeSamples/defer: Fix hazard torture
Because hp_try_record() must check for concurrent modifications, we should not dereference the pointer. The arg of hazptr_read_stress_test is always NULL, which is designated at time when create the thread, therefore, all threads will store their hazard pointer in the same slot. Also, hazptr_thread_exit will clear their own hazard pointer from smp_thread_id() * K.
--- CodeSamples/defer/hazptr.h | 2 +- CodeSamples/defer/hazptrtorture.h | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-)
CodeSamples/defer/hazptr.h +1 -1
@@ -87,7 +87,7 @@ static inline void *hp_record(void **p, //\lnlbl{hr:b}void *tmp;do { - tmp =hp_try_record (*p, hp); + tmp =hp_try_record (p, hp); }while (tmp == (void *)HAZPTR_POISON );return tmp; }//\lnlbl{hr:e}
CodeSamples/defer/hazptrtorture.h +2 -3
@@ -99,7 +99,7 @@ void *hazptr_read_perf_test(void *arg) {int i;int me = (long )arg; -int base = me *K ; +int base =smp_thread_id () *K ;long long n_reads_local =0 ;hazptr_head_t hh;hazptr_head_t *hhp = &hh;
@@ -241,8 +241,7 @@ DEFINE_PER_THREAD(long long [HAZPTR_STRESS_PIPE_LEN + 1], hazptr_stress_count);void *hazptr_read_stress_test (void *arg) { -int me = (int )(long )arg; -int base = me *K ; +int base =smp_thread_id () *K ;struct hazptr_stress *p;int pc;
Signature · 1 line
2.34.1
Hi,
I have squashed the two commits , because they are somewhat related.
Thanks,
Alan
Quoted 54 lines
2023年4月22日 18:34,Alan Huang <[email protected]> 写道:
Because
hp_try_record()must check for concurrent modifications, we should not dereference the pointer. The arg ofhazptr_read_stress_testis always NULL, which is designated at time when create the thread, therefore, all threads will store their hazard pointer in the same slot. Also,hazptr_thread_exitwill clear their own hazard pointer fromsmp_thread_id()* K.Signed-off-by Alan Huang <[email protected]>---
CodeSamples/defer/hazptr.h| 2 +-CodeSamples/defer/hazptrtorture.h| 5 ++---2 files changed, 3 insertions(+), 4 deletions(-)CodeSamples/defer/hazptr.h +1
-1@@ -87,7 +87,7 @@ static inline void *hp_record(void **p, //\lnlbl{hr:b}void *tmp;do { - tmp =hp_try_record (*p, hp); + tmp =hp_try_record (p, hp); }while (tmp == (void *)HAZPTR_POISON );return tmp; }//\lnlbl{hr:e} CodeSamples/defer/hazptrtorture.h +2
-3@@ -99,7 +99,7 @@ void *hazptr_read_perf_test(void *arg) {int i;int me = (long )arg; -int base = me *K ; +int base =smp_thread_id () *K ;long long n_reads_local =0 ;hazptr_head_t hh;hazptr_head_t *hhp = &hh;@@ -241,8 +241,7 @@ DEFINE_PER_THREAD(long long [HAZPTR_STRESS_PIPE_LEN + 1], hazptr_stress_count);void *hazptr_read_stress_test (void *arg) { -int me = (int )(long )arg; -int base = me *K ; +int base =smp_thread_id () *K ;struct hazptr_stress *p;int pc;Signature · 1 line
2.34.1
Quoted 8 lines · Alan Huang
Because
hp_try_record()must check for concurrent modifications, we should not dereference the pointer. The arg ofhazptr_read_stress_testis always NULL, which is designated at time when create the thread, therefore, all threads will store their hazard pointer in the same slot. Also,hazptr_thread_exitwill clear their own hazard pointer fromsmp_thread_id()* K.Signed-off-by Alan Huang <[email protected]>
Now I see all your points. Thank you for your thorough review of the code!
As I mentioned earlier, the deref of p was introduced by commit a082aba85ce0 ("defer/hazptr: Checkpoint for text/code update"), while the uses of arg in hazptr_read_stress_test() and hazptr_read_perf_test() were there since the addition of this code by commit c672535d475a ("Convert Tom Hart's hazard-pointer implementation to perfbook"), that is, if I'm reading the code correctly.
So,
Thanks, Akira
Quoted 41 lines
--- CodeSamples/defer/hazptr.h | 2 +- CodeSamples/defer/hazptrtorture.h | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-)CodeSamples/defer/hazptr.h +1
-1@@ -87,7 +87,7 @@ static inline void *hp_record(void **p, //\lnlbl{hr:b}void *tmp;do { - tmp =hp_try_record (*p, hp); + tmp =hp_try_record (p, hp); }while (tmp == (void *)HAZPTR_POISON );return tmp; }//\lnlbl{hr:e} CodeSamples/defer/hazptrtorture.h +2
-3@@ -99,7 +99,7 @@ void *hazptr_read_perf_test(void *arg) {int i;int me = (long )arg; -int base = me *K ; +int base =smp_thread_id () *K ;long long n_reads_local =0 ;hazptr_head_t hh;hazptr_head_t *hhp = &hh;@@ -241,8 +241,7 @@ DEFINE_PER_THREAD(long long [HAZPTR_STRESS_PIPE_LEN + 1], hazptr_stress_count);void *hazptr_read_stress_test (void *arg) { -int me = (int )(long )arg; -int base = me *K ; +int base =smp_thread_id () *K ;struct hazptr_stress *p;int pc;
Quoted 21 lines · Akira Yokosawa
Quoted 8 lines · Alan Huang
Because
hp_try_record()must check for concurrent modifications, we should not dereference the pointer. The arg ofhazptr_read_stress_testis always NULL, which is designated at time when create the thread, therefore, all threads will store their hazard pointer in the same slot. Also,hazptr_thread_exitwill clear their own hazard pointer fromsmp_thread_id()* K.Signed-off-by Alan Huang <[email protected]>Now I see all your points. Thank you for your thorough review of the code!
As I mentioned earlier, the deref of p was introduced by commit
a082aba85ce0("defer/hazptr: Checkpoint for text/code update"), while the uses of arg inhazptr_read_stress_test()andhazptr_read_perf_test()were there since the addition of this code by commitc672535d475a("Convert Tom Hart's hazard-pointer implementation to perfbook"), that is, if I'm reading the code correctly.
Quoted 5 lines
So,
Reviewed-by Akira Yokosawa <[email protected]>Fixes c672535d475a ("Convert Tom Hart's hazard-pointer implementation to perfbook")Fixes a082aba85ce0 ("defer/hazptr: Checkpoint for text/code update")
Now -that- is what I call an embarrassing bug!
Thank you both for tracking it down and for the fix!!!
Queued and pushed.
Thanx, Paul
Quoted 43 lines
Thanks, Akira
Quoted 41 lines
--- CodeSamples/defer/hazptr.h | 2 +- CodeSamples/defer/hazptrtorture.h | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-)CodeSamples/defer/hazptr.h +1
-1@@ -87,7 +87,7 @@ static inline void *hp_record(void **p, //\lnlbl{hr:b}void *tmp;do { - tmp =hp_try_record (*p, hp); + tmp =hp_try_record (p, hp); }while (tmp == (void *)HAZPTR_POISON );return tmp; }//\lnlbl{hr:e} CodeSamples/defer/hazptrtorture.h +2
-3@@ -99,7 +99,7 @@ void *hazptr_read_perf_test(void *arg) {int i;int me = (long )arg; -int base = me *K ; +int base =smp_thread_id () *K ;long long n_reads_local =0 ;hazptr_head_t hh;hazptr_head_t *hhp = &hh;@@ -241,8 +241,7 @@ DEFINE_PER_THREAD(long long [HAZPTR_STRESS_PIPE_LEN + 1], hazptr_stress_count);void *hazptr_read_stress_test (void *arg) { -int me = (int )(long )arg; -int base = me *K ; +int base =smp_thread_id () *K ;struct hazptr_stress *p;int pc;