fzsync: revoke thread_b if parent hits an accidental break
PATCH v3 1/1 fzsync: revoke thread_b if parent hits an accidental break
We shouldn't rely entirely on the pair->exit flag in tst_fzsync_pair_cleanup() since there is possible to call tst_brk() at anyplace of thread_a, that will lead to timeout eventually because of thread_b(tst_fzsync_wait_b) fall into an infinite(no explicit condition to exit) loop.
Thread_a path trace:
tst_brk()
cleanup()
tst_fzsync_pair_cleanup()
SAFE_PTHREAD_JOIN(pair->thread_b, NULL)
#Or pthread_cancel(pair->thread_b)We fix the problem via a way to kill thread_b with pthread_cancel.
Work-around: [commit 2e74d996: Check recvmmsg exists before entering fuzzy loop]
---
Notes:
Patch V2: http://lists.linux.it/pipermail/ltp/2019-January/010489.html Patch V1: http://lists.linux.it/pipermail/ltp/2019-January/010438.html
include/tst_fuzzy_sync.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
include/tst_fuzzy_sync.h +12 -4
@@ -63,6 +63,7 @@#include <time.h> #include <math.h> #include <stdlib.h> +#include <pthread.h> #include "tst_atomic.h" #include "tst_timer.h" #include "tst_safe_pthread.h"
@@ -218,9 +219,13 @@ static void tst_fzsync_pair_init(struct tst_fzsync_pair *pair)static void tst_fzsync_pair_cleanup (struct tst_fzsync_pair *pair) {if (pair->thread_b) { -tst_atomic_store (1 , &pair->exit); -SAFE_PTHREAD_JOIN (pair->thread_b,NULL ); - pair->thread_b =0 ; +if (pair->exit ==1 ) { +SAFE_PTHREAD_JOIN (pair->thread_b,NULL ); + pair->thread_b =0 ; + }else { +pthread_cancel (pair->thread_b); + pair->thread_b =0 ; + } } }
@@ -271,8 +276,11 @@ static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair, pair->a_cntr =0 ; pair->b_cntr =0 ; pair->exit =0 ; -if (run_b) +if (run_b) { +pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS ,NULL ); +pthread_setcancelstate (PTHREAD_CANCEL_ENABLE ,NULL );SAFE_PTHREAD_CREATE (&pair->thread_b,0 , run_b,0 ); + } pair->exec_time_start = (float )tst_timeout_remaining (); }
Signature · 1 line
2.20.1
PATCH v3 1/1 fzsync: revoke thread_b if parent hits an accidental break
Hello,
Quoted 5 lines · Li Wang
We shouldn't rely entirely on the
pair->exitflag intst_fzsync_pair_cleanup()since there is possible to calltst_brk()at anyplace ofthread_a, that will lead to timeout eventually because ofthread_b(tst_fzsync_wait_b)fall into an infinite(no explicit condition to exit) loop.
Quoted 8 lines
Thread_apath trace:
tst_brk()cleanup() tst_fzsync_pair_cleanup() SAFE_PTHREAD_JOIN(pair->thread_b, NULL) #Or pthread_cancel(pair->thread_b)We fix the problem via a way to kill
thread_bwithpthread_cancel.
Good idea, however not an easy one to implement.
Quoted 27 lines
Work-around: [commit
2e74d996: Check recvmmsg exists before entering fuzzy loop]Signed-off-by Li Wang <[email protected]>Cc Richard Palethorpe <[email protected]>Cc Cyril Hrubis <[email protected]>---
Notes:
Patch V2: http://lists.linux.it/pipermail/ltp/2019-January/010489.html Patch V1: http://lists.linux.it/pipermail/ltp/2019-January/010438.htmlinclude/tst_fuzzy_sync.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)include/tst_fuzzy_sync.h +1
-0@@ -63,6 +63,7 @@#include <time.h> #include <math.h> #include <stdlib.h> +#include <pthread.h> #include "tst_atomic.h" #include "tst_timer.h" #include "tst_safe_pthread.h"
include/tst_fuzzy_sync.h static void tst_fzsync_pair_init(struct tst_fzsync_pair *pair) · line 222
Quoted 7 lines
include/tst_fuzzy_sync.h +1
-3@@ -218,9 +219,13 @@ static void tst_fzsync_pair_init(struct tst_fzsync_pair *pair)static void tst_fzsync_pair_cleanup (struct tst_fzsync_pair *pair) {if (pair->thread_b) { -tst_atomic_store (1 , &pair->exit); -SAFE_PTHREAD_JOIN (pair->thread_b,NULL ); - pair->thread_b =0 ; +if (pair->exit ==1 ) {
It can just be
if (!pair->exit) {
...
}
We want to join the thread and set the func pointer to zero regardless of how we exit.
include/tst_fuzzy_sync.h static void tst_fzsync_pair_init(struct tst_fzsync_pair *pair) · line 225
Quoted 3 lines
include/tst_fuzzy_sync.h +3
-0+SAFE_PTHREAD_JOIN (pair->thread_b,NULL ); + pair->thread_b =0 ; + }else {
I suggest still setting pair->exit here and maybe sleeping for 100ms. This gives thread B chance to exit gracefully. It is possible that if thread B is in a spin loop then the thread won't be cancelled as asynchronous cancellation is not guaranteed by POSIX.
Quoted 7 lines
include/tst_fuzzy_sync.h +3
-0+pthread_cancel (pair->thread_b); + pair->thread_b =0 ; + } } }
include/tst_fuzzy_sync.h static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair, · line 281
Quoted 7 lines
include/tst_fuzzy_sync.h +3
-1@@ -271,8 +276,11 @@ static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair, pair->a_cntr =0 ; pair->b_cntr =0 ; pair->exit =0 ; -if (run_b) +if (run_b) { +pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS ,NULL ); +pthread_setcancelstate (PTHREAD_CANCEL_ENABLE ,NULL );
These need to go inside thread B unless I am mistaken. Which means you must wrap the user supplied function. You can create a function which accepts a pointer to some contiguous memory containing the user supplied function pointer and the user supplied arg pointer.
This can then set the threads cancel type before calling the user func with the arg.
include/tst_fuzzy_sync.h static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair, · line 286
Quoted 5 lines
include/tst_fuzzy_sync.h +1
-0SAFE_PTHREAD_CREATE (&pair->thread_b,0 , run_b,0 ); + } pair->exec_time_start = (float )tst_timeout_remaining (); }
--
Thank you,
Richard.
PATCH v3 1/1 fzsync: revoke thread_b if parent hits an accidental break
Hi Richard,
Quoted 10 lines · Richard Palethorpe
...
It can just beif (!pair->exit) { ... }We want to join the thread and set the func pointer to zero regardless of how we exit.
OK.
Quoted 10 lines
include/tst_fuzzy_sync.h static void tst_fzsync_pair_init(struct tst_fzsync_pair *pair) · line 225
Quoted 4 lines
include/tst_fuzzy_sync.h +3
-0+SAFE_PTHREAD_JOIN (pair->thread_b,NULL ); + pair->thread_b =0 ; + }else {I suggest still setting
pair->exithere and maybe sleeping for 100ms. This gives thread B chance to exit gracefully. It is possible that if thread B is in a spin loop then the thread won't be cancelled as asynchronous cancellation is not guaranteed by POSIX.
Good suggestion. That'd be better to give one more time for thread B exiting gracefully.
Quoted 16 lines
include/tst_fuzzy_sync.h static void tst_fzsync_pair_reset(struct
Quoted 7 lines
include/tst_fuzzy_sync.h +3
-0+pthread_cancel (pair->thread_b); + pair->thread_b =0 ; + } } }@@ -271,8 +276,11 @@ static void tst_fzsync_pair_reset(struct
tst_fzsync_pair*pair,include/tst_fuzzy_sync.h static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair, · line 281
Quoted 7 lines
include/tst_fuzzy_sync.h +3
-1pair->a_cntr =0 ; pair->b_cntr =0 ; pair->exit =0 ; -if (run_b) +if (run_b) { +pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS ,NULL ); +pthread_setcancelstate (PTHREAD_CANCEL_ENABLE ,NULL );
Quoted 2 lines
These need to go inside thread B unless I am mistaken. Which means you
Right.
Quoted 5 lines
must wrap the user supplied function. You can create a function which accepts a pointer to some contiguous memory containing the user supplied function
pointer and the user supplied arg pointer.
Since you have fixed the function format of thread B as void *(*run_b)(void *) in tst_fzsync_pair_reset(), which means we have no need to take care of the function arg pointer anymore.
So just like what I did in V2, the wrapper function could steal the real run_b address from pthread_create(..., wrap_run_b, run_b) parameter.
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20190925/162118b3/attachment.htm>
PATCH v3 1/1 fzsync: revoke thread_b if parent hits an accidental break
Hello,
Quoted 62 lines · Li Wang
Hi Richard,
Quoted 10 lines · Richard Palethorpe
...
It can just beif (!pair->exit) { ... }We want to join the thread and set the func pointer to zero regardless of how we exit.
OK.
Quoted 10 lines
include/tst_fuzzy_sync.h static void tst_fzsync_pair_init(struct tst_fzsync_pair *pair) · line 225
Quoted 4 lines
include/tst_fuzzy_sync.h +3
-0+SAFE_PTHREAD_JOIN (pair->thread_b,NULL ); + pair->thread_b =0 ; + }else {I suggest still setting
pair->exithere and maybe sleeping for 100ms. This gives thread B chance to exit gracefully. It is possible that if thread B is in a spin loop then the thread won't be cancelled as asynchronous cancellation is not guaranteed by POSIX.Good suggestion. That'd be better to give one more time for thread B exiting gracefully.
Quoted 16 lines
include/tst_fuzzy_sync.h static void tst_fzsync_pair_reset(struct
Quoted 7 lines
include/tst_fuzzy_sync.h +3
-0+pthread_cancel (pair->thread_b); + pair->thread_b =0 ; + } } }@@ -271,8 +276,11 @@ static void tst_fzsync_pair_reset(struct
tst_fzsync_pair*pair,include/tst_fuzzy_sync.h static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair, · line 281
Quoted 7 lines
include/tst_fuzzy_sync.h +3
-1pair->a_cntr =0 ; pair->b_cntr =0 ; pair->exit =0 ; -if (run_b) +if (run_b) { +pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS ,NULL ); +pthread_setcancelstate (PTHREAD_CANCEL_ENABLE ,NULL );Quoted 2 lines
These need to go inside thread B unless I am mistaken. Which means you
Right.
Quoted 5 lines
must wrap the user supplied function. You can create a function which accepts a pointer to some contiguous memory containing the user supplied function
pointer and the user supplied arg pointer.
Quoted 3 lines
Since you have fixed the function format of thread B as void *(*run_b)(void *) in
tst_fzsync_pair_reset(), which means we have no need to take care of the function arg pointer anymore.
I think the function pointer signature would be 'void *(*run_b)(void)' not 'void *(*run_b)(void *)'.
I doubt any test would need the arg though, because we only use one thread and can store parameters in global variables. So you could remove it and update the tests.
The user might need that arg if they are starting many threads, but for now we don't have explicit support for that in the library.
Quoted 3 lines
So just like what I did in V2, the wrapper function could steal the real
run_baddress frompthread_create(..., wrap_run_b, run_b)parameter.
--
Thank you,
Richard.
PATCH v3 1/1 fzsync: revoke thread_b if parent hits an accidental break
Quoted 6 lines · Richard Palethorpe
Quoted 6 lines
must wrap the user supplied function. You can create a function which
Quoted 4 lines
accepts a pointer to some contiguous memory containing the user supplied function
pointer and the user supplied arg pointer.
Quoted 9 lines
Quoted 1 line
Since you have fixed the function format of thread B as void
*(*run_b)(void
Quoted 1 line
*) in
tst_fzsync_pair_reset(), which means we have no need to take careof
Quoted 1 line
the function arg pointer anymore.
I think the function pointer signature would be 'void *(*run_b)(void)' not 'void *(*run_b)(void *)'.
Hmm, but at least we should respect the pthread_create()? It requires the function prototype is ''void *(*func)(void *)'.
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
We could unuse the arg in thread B, but declare the function prototype with parameter "void *" is no harm.
Quoted 7 lines
I doubt any test would need the arg though, because we only use one thread and can store parameters in global variables. So you could remove it and update the tests.
The user might need that arg if they are starting many threads, but for now we don't have explicit support for that in the library.
Maybe we just need to note that in the lib document.
Quoted 9 lines
Quoted 3 lines
So just like what I did in V2, the wrapper function could steal the real
run_baddress frompthread_create(..., wrap_run_b, run_b)parameter.--
Thank you,
Richard.
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20190925/c33ecf3a/attachment-0001.htm>
PATCH v3 1/1 fzsync: revoke thread_b if parent hits an accidental break
Hello,
Quoted 18 lines · Li Wang
Quoted 6 lines · Richard Palethorpe
Quoted 6 lines
must wrap the user supplied function. You can create a function which
Quoted 4 lines
accepts a pointer to some contiguous memory containing the user supplied function
pointer and the user supplied arg pointer.Quoted 9 lines
Quoted 1 line
Since you have fixed the function format of thread B as void
*(*run_b)(void
Quoted 1 line
*) in
tst_fzsync_pair_reset(), which means we have no need to take careof
Quoted 1 line
the function arg pointer anymore.
I think the function pointer signature would be 'void *(*run_b)(void)' not 'void *(*run_b)(void *)'.
Quoted 8 lines
Hmm, but at least we should respect the
pthread_create()? It requires the function prototype is ''void *(*func)(void *)'.int pthread_create(pthread_t *thread, const
pthread_attr_t*attr,
void *(*start_routine) (void *), void *arg);We could unuse the arg in thread B, but declare the function prototype with parameter "void *" is no harm.
I'm not sure what you are saying. However you could do something like this (I haven't tested it):
struct tst_fzsync_run_thread
{
void *(*run)(void *);
void *arg;
};static void tst_fzsync_thread_wrapper(void *arg)
{
struct tst_fzsync_run_thread t = *(struct tst_fzsync_run_thread *)arg;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE);
t.run(t.arg);
}static void tst_fzsync_pair_reset(..., struct tst_fzsync_run_thread *run_b)
{
...
if (run_b)
SAFE_PTHREAD_fCREATE(..., tst_fzsync_thread_wrapper, (void *)run_b);
...
}Note that in any case you can't reliably cast a function pointer to a void pointer without some magic. I am guessing wrapping it in a struct is the clearest way to do it.
You can remove the arg altogether, but I kept it because we have a struct anyway to wrap the function pointer.
Quoted 6 lines
Quoted 6 lines
I doubt any test would need the arg though, because we only use one thread and can store parameters in global variables. So you could remove it and update the tests.
Quoted 16 lines
Quoted 3 lines
The user might need that arg if they are starting many threads, but for now we don't have explicit support for that in the library.
Maybe we just need to note that in the lib document.
Quoted 9 lines
Quoted 3 lines
So just like what I did in V2, the wrapper function could steal the real
run_baddress frompthread_create(..., wrap_run_b, run_b)parameter.--
Thank you,
Richard.
--
Thank you,
Richard.
PATCH v3 1/1 fzsync: revoke thread_b if parent hits an accidental break
Quoted 4 lines · Richard Palethorpe
...
I'm not sure what you are saying. However you could do something like this (I haven't tested it):
I misunderstood your words in the last mail, sorry about that.
Quoted 12 lines
struct
tst_fzsync_run_thread{ void *(*run)(void *); void *arg; };static void
tst_fzsync_thread_wrapper(void *arg)
{
structtst_fzsync_run_threadt = *(structtst_fzsync_run_thread
*)arg;
Quoted 8 lines
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE); t.run(t.arg); }static void
tst_fzsync_pair_reset(..., struct tst_fzsync_run_thread *run_b)
I'd like to keep the tst_fzync_pair_reset() API no change to user.
The patch v4.1 like below, is there any improper place in usage?
@@ -218,12 +219,36 @@ static void tst_fzsync_pair_init(struct tst_fzsync_pair *pair)
static void tst_fzsync_pair_cleanup(struct tst_fzsync_pair *pair)
{
if (pair->thread_b) {
- tst_atomic_store(1, &pair->exit);
+ /* Revoke thread B if parent hits accidental break */
+ if (!pair->exit) {
+ tst_atomic_store(1, &pair->exit);
+ usleep(100000);
+ pthread_cancel(pair->thread_b);
+ }
SAFE_PTHREAD_JOIN(pair->thread_b, NULL);
pair->thread_b = 0;
}}
+/** To store the run_b pointer and pass to tst_fzsync_thread_wrapper */ +struct tst_fzsync_run_thread {
+ void *(*func)(void *);
+ void *arg;
+};
+
+/**
+ * Wrap run_b for tst_fzsync_pair_reset to enable pthread cancel
+ * at the start of the thread B.
+ */
+static void *tst_fzsync_thread_wrapper(void *run_thread)
+{
+ struct tst_fzsync_run_thread t = *(struct tst_fzsync_run_thread
*)run_thread;
+
+ pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + return t.func(t.arg);
+}
+
/**
* Zero some stat fields
*
@@ -271,8 +296,10 @@ static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair,
pair->a_cntr = 0;
pair->b_cntr = 0;
pair->exit = 0;- if (run_b)
- SAFE_PTHREAD_CREATE(&pair->thread_b, 0, run_b, 0);
+ if (run_b) {
+ struct tst_fzsync_run_thread wrap_run_b = {.func = run_b,
.arg = NULL};
+ SAFE_PTHREAD_CREATE(&pair->thread_b, 0,tst_fzsync_thread_wrapper, &wrap_run_b);
+ }
pair->exec_time_start = (float)tst_timeout_remaining();
}
Quoted 4 lines
Note that in any case you can't reliably cast a function pointer to a void pointer without some magic. I am guessing wrapping it in a struct is the clearest way to do it.
Good to know this, I searched on google and confirmed that the C standard does not allow to cast function pointer to void*, thanks!
Quoted 4 lines
You can remove the arg altogether, but I kept it because we have a struct anyway to wrap the function pointer.
Yes, to keep it make the wrapper struct is clear to understand.
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20190926/36531b52/attachment.htm>
PATCH v3 1/1 fzsync: revoke thread_b if parent hits an accidental break
Quoted 9 lines · Li Wang
Quoted 4 lines · Richard Palethorpe
...
I'm not sure what you are saying. However you could do something like this (I haven't tested it):I misunderstood your words in the last mail, sorry about that.
No problem. This is also my fault.
Quoted 20 lines
Quoted 20 lines
struct
tst_fzsync_run_thread{ void *(*run)(void *); void *arg; };static void
tst_fzsync_thread_wrapper(void *arg)
{
structtst_fzsync_run_threadt = *(structtst_fzsync_run_thread
*)arg;pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE); t.run(t.arg); }
Quoted 6 lines
Quoted 2 lines
static void
tst_fzsync_pair_reset(..., struct tst_fzsync_run_thread *run_b)I'd like to keep the
tst_fzync_pair_reset()API no change to user.The patch v4.1 like below, is there any improper place in usage?
LGTM!
Quoted 74 lines
(quoted diff) +25
-1@@ -218,12 +219,36 @@ static void tst_fzsync_pair_init(struct tst_fzsync_pair *pair) static void tst_fzsync_pair_cleanup(struct tst_fzsync_pair *pair) { if (pair->thread_b) { - tst_atomic_store(1, &pair->exit); + /* Revoke thread B if parent hits accidental break */ + if (!pair->exit) { + tst_atomic_store(1, &pair->exit); + usleep(100000); + pthread_cancel(pair->thread_b); + } SAFE_PTHREAD_JOIN(pair->thread_b, NULL); pair->thread_b = 0; } } +/** To store the run_b pointer and pass to tst_fzsync_thread_wrapper */ +struct tst_fzsync_run_thread { + void *(*func)(void *); + void *arg; +}; + +/** + * Wrap run_b for tst_fzsync_pair_reset to enable pthread cancel + * at the start of the thread B. + */ +static void *tst_fzsync_thread_wrapper(void *run_thread) +{ + struct tst_fzsync_run_thread t = *(struct tst_fzsync_run_thread *)run_thread; + + pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + return t.func(t.arg); +} + /*** Zero some stat fields
*(quoted diff) +4
-2@@ -271,8 +296,10 @@ static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair, pair->a_cntr = 0; pair->b_cntr = 0; pair->exit = 0; - if (run_b) - SAFE_PTHREAD_CREATE(&pair->thread_b, 0, run_b, 0); + if (run_b) { + struct tst_fzsync_run_thread wrap_run_b = {.func = run_b, .arg = NULL}; + SAFE_PTHREAD_CREATE(&pair->thread_b, 0, tst_fzsync_thread_wrapper, &wrap_run_b); + }pair->exec_time_start = (float)tst_timeout_remaining();}
Quoted 4 lines
Note that in any case you can't reliably cast a function pointer to a void pointer without some magic. I am guessing wrapping it in a struct is the clearest way to do it.
Good to know this, I searched on google and confirmed that the C standard does not allow to cast function pointer to void*, thanks!
Quoted 4 lines
You can remove the arg altogether, but I kept it because we have a struct anyway to wrap the function pointer.
Yes, to keep it make the wrapper struct is clear to understand.
Signature · 2 lines
Thank you, Richard.