* [LTP] [PATCH v2] fzsync: break inf loop with flag vs pthread_cancel
@ 2022-04-11 15:16 Edward Liaw via ltp
2022-04-12 2:40 ` Li Wang
0 siblings, 1 reply; 3+ messages in thread
From: Edward Liaw via ltp @ 2022-04-11 15:16 UTC (permalink / raw)
To: ltp; +Cc: kernel-team
Hi, I'm working to get fzsync working with the Android kernel, which
does not have pthread_cancel available.
In the absence of pthread_cancel, when thread A exits due to a break,
thread B will get stuck in an infinite loop while waiting for thread A
to progress.
Instead of cancelling thread B, we can use the exit flag to break out of
thread B's loop. This should also remove the need for the wrapper
around the thread.
Signed-off-by: Edward Liaw <edliaw@google.com>
---
include/tst_fuzzy_sync.h | 74 +++++++++++------------------
lib/newlib_tests/tst_fuzzy_sync01.c | 7 +--
lib/newlib_tests/tst_fuzzy_sync02.c | 7 +--
3 files changed, 30 insertions(+), 58 deletions(-)
diff --git a/include/tst_fuzzy_sync.h b/include/tst_fuzzy_sync.h
index bc3450294..455795ebb 100644
--- a/include/tst_fuzzy_sync.h
+++ b/include/tst_fuzzy_sync.h
@@ -60,7 +60,6 @@
*/
#include <math.h>
-#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/time.h>
@@ -234,35 +233,13 @@ static inline void tst_fzsync_pair_cleanup(struct tst_fzsync_pair *pair)
{
if (pair->thread_b) {
/* Revoke thread B if parent hits accidental break */
- if (!pair->exit) {
+ 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 inline 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
*
@@ -311,13 +288,8 @@ static inline void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair,
pair->a_cntr = 0;
pair->b_cntr = 0;
pair->exit = 0;
- if (run_b) {
- static struct tst_fzsync_run_thread wrap_run_b;
-
- wrap_run_b.func = run_b;
- wrap_run_b.arg = NULL;
- SAFE_PTHREAD_CREATE(&pair->thread_b, 0, tst_fzsync_thread_wrapper, &wrap_run_b);
- }
+ if (run_b)
+ SAFE_PTHREAD_CREATE(&pair->thread_b, 0, run_b, 0);
pair->exec_time_start = (float)tst_timeout_remaining();
}
@@ -554,6 +526,7 @@ static inline void tst_fzsync_pair_update(struct tst_fzsync_pair *pair)
* @param our_cntr The counter for the thread we are on
* @param other_cntr The counter for the thread we are synchronising with
* @param spins A pointer to the spin counter or NULL
+ * @param exit Exit flag when we need to break out of the wait loop
*
* Used by tst_fzsync_pair_wait_a(), tst_fzsync_pair_wait_b(),
* tst_fzsync_start_race_a(), etc. If the calling thread is ahead of the other
@@ -566,6 +539,7 @@ static inline void tst_fzsync_pair_update(struct tst_fzsync_pair *pair)
static inline void tst_fzsync_pair_wait(int *our_cntr,
int *other_cntr,
int *spins,
+ int *exit,
bool yield_in_wait)
{
if (tst_atomic_inc(other_cntr) == INT_MAX) {
@@ -578,7 +552,8 @@ static inline void tst_fzsync_pair_wait(int *our_cntr,
*/
if (yield_in_wait) {
while (tst_atomic_load(our_cntr) > 0
- && tst_atomic_load(our_cntr) < INT_MAX) {
+ && tst_atomic_load(our_cntr) < INT_MAX
+ && !tst_atomic_load(exit)) {
if (spins)
(*spins)++;
@@ -586,7 +561,8 @@ static inline void tst_fzsync_pair_wait(int *our_cntr,
}
} else {
while (tst_atomic_load(our_cntr) > 0
- && tst_atomic_load(our_cntr) < INT_MAX) {
+ && tst_atomic_load(our_cntr) < INT_MAX
+ && !tst_atomic_load(exit)) {
if (spins)
(*spins)++;
}
@@ -599,10 +575,12 @@ static inline void tst_fzsync_pair_wait(int *our_cntr,
* is restored and we can continue.
*/
if (yield_in_wait) {
- while (tst_atomic_load(our_cntr) > 1)
+ while (tst_atomic_load(our_cntr) > 1
+ && !tst_atomic_load(exit))
sched_yield();
} else {
- while (tst_atomic_load(our_cntr) > 1)
+ while (tst_atomic_load(our_cntr) > 1
+ && !tst_atomic_load(exit))
;
}
} else {
@@ -612,14 +590,16 @@ static inline void tst_fzsync_pair_wait(int *our_cntr,
*/
if (yield_in_wait) {
while (tst_atomic_load(our_cntr) <
- tst_atomic_load(other_cntr)) {
+ tst_atomic_load(other_cntr)
+ && !tst_atomic_load(exit)) {
if (spins)
(*spins)++;
sched_yield();
}
} else {
while (tst_atomic_load(our_cntr) <
- tst_atomic_load(other_cntr)) {
+ tst_atomic_load(other_cntr)
+ && !tst_atomic_load(exit)) {
if (spins)
(*spins)++;
}
@@ -635,7 +615,8 @@ static inline void tst_fzsync_pair_wait(int *our_cntr,
*/
static inline void tst_fzsync_wait_a(struct tst_fzsync_pair *pair)
{
- tst_fzsync_pair_wait(&pair->a_cntr, &pair->b_cntr, NULL, pair->yield_in_wait);
+ tst_fzsync_pair_wait(&pair->a_cntr, &pair->b_cntr,
+ NULL, &pair->exit, pair->yield_in_wait);
}
/**
@@ -646,7 +627,8 @@ static inline void tst_fzsync_wait_a(struct tst_fzsync_pair *pair)
*/
static inline void tst_fzsync_wait_b(struct tst_fzsync_pair *pair)
{
- tst_fzsync_pair_wait(&pair->b_cntr, &pair->a_cntr, NULL, pair->yield_in_wait);
+ tst_fzsync_pair_wait(&pair->b_cntr, &pair->a_cntr,
+ NULL, &pair->exit, pair->yield_in_wait);
}
/**
@@ -662,7 +644,6 @@ static inline void tst_fzsync_wait_b(struct tst_fzsync_pair *pair)
*/
static inline int tst_fzsync_run_a(struct tst_fzsync_pair *pair)
{
- int exit = 0;
float rem_p = 1 - tst_timeout_remaining() / pair->exec_time_start;
if ((pair->exec_time_p * SAMPLING_SLICE < rem_p)
@@ -677,19 +658,18 @@ static inline int tst_fzsync_run_a(struct tst_fzsync_pair *pair)
if (pair->exec_time_p < rem_p) {
tst_res(TINFO,
"Exceeded execution time, requesting exit");
- exit = 1;
+ tst_atomic_store(1, &pair->exit);
}
if (++pair->exec_loop > pair->exec_loops) {
tst_res(TINFO,
"Exceeded execution loops, requesting exit");
- exit = 1;
+ tst_atomic_store(1, &pair->exit);
}
- tst_atomic_store(exit, &pair->exit);
tst_fzsync_wait_a(pair);
- if (exit) {
+ if (pair->exit) {
tst_fzsync_pair_cleanup(pair);
return 0;
}
@@ -758,7 +738,8 @@ static inline void tst_fzsync_start_race_a(struct tst_fzsync_pair *pair)
static inline void tst_fzsync_end_race_a(struct tst_fzsync_pair *pair)
{
tst_fzsync_time(&pair->a_end);
- tst_fzsync_pair_wait(&pair->a_cntr, &pair->b_cntr, &pair->spins, pair->yield_in_wait);
+ tst_fzsync_pair_wait(&pair->a_cntr, &pair->b_cntr,
+ &pair->spins, &pair->exit, pair->yield_in_wait);
}
/**
@@ -796,7 +777,8 @@ static inline void tst_fzsync_start_race_b(struct tst_fzsync_pair *pair)
static inline void tst_fzsync_end_race_b(struct tst_fzsync_pair *pair)
{
tst_fzsync_time(&pair->b_end);
- tst_fzsync_pair_wait(&pair->b_cntr, &pair->a_cntr, &pair->spins, pair->yield_in_wait);
+ tst_fzsync_pair_wait(&pair->b_cntr, &pair->a_cntr,
+ &pair->spins, &pair->exit, pair->yield_in_wait);
}
/**
diff --git a/lib/newlib_tests/tst_fuzzy_sync01.c b/lib/newlib_tests/tst_fuzzy_sync01.c
index ae3ea4e09..5f23a085b 100644
--- a/lib/newlib_tests/tst_fuzzy_sync01.c
+++ b/lib/newlib_tests/tst_fuzzy_sync01.c
@@ -182,15 +182,10 @@ static void *worker(void *v)
static void run(unsigned int i)
{
const struct window a = races[i].a;
- struct tst_fzsync_run_thread wrap_run_b = {
- .func = worker,
- .arg = &i,
- };
int cs, ct, r, too_early = 0, critical = 0, too_late = 0;
tst_fzsync_pair_reset(&pair, NULL);
- SAFE_PTHREAD_CREATE(&pair.thread_b, 0, tst_fzsync_thread_wrapper,
- &wrap_run_b);
+ SAFE_PTHREAD_CREATE(&pair.thread_b, 0, worker, &i);
while (tst_fzsync_run_a(&pair)) {
diff --git a/lib/newlib_tests/tst_fuzzy_sync02.c b/lib/newlib_tests/tst_fuzzy_sync02.c
index 51075f3c3..c1c2a5327 100644
--- a/lib/newlib_tests/tst_fuzzy_sync02.c
+++ b/lib/newlib_tests/tst_fuzzy_sync02.c
@@ -125,16 +125,11 @@ static void run(unsigned int i)
{
const struct window a = to_abs(races[i].a);
const struct window ad = to_abs(races[i].ad);
- struct tst_fzsync_run_thread wrap_run_b = {
- .func = worker,
- .arg = &i,
- };
int critical = 0;
int now, fin;
tst_fzsync_pair_reset(&pair, NULL);
- SAFE_PTHREAD_CREATE(&pair.thread_b, 0, tst_fzsync_thread_wrapper,
- &wrap_run_b);
+ SAFE_PTHREAD_CREATE(&pair.thread_b, 0, worker, &i);
while (tst_fzsync_run_a(&pair)) {
c = 0;
--
2.35.1.1178.g4f1659d476-goog
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [LTP] [PATCH v2] fzsync: break inf loop with flag vs pthread_cancel
2022-04-11 15:16 [LTP] [PATCH v2] fzsync: break inf loop with flag vs pthread_cancel Edward Liaw via ltp
@ 2022-04-12 2:40 ` Li Wang
2022-04-12 9:40 ` Richard Palethorpe
0 siblings, 1 reply; 3+ messages in thread
From: Li Wang @ 2022-04-12 2:40 UTC (permalink / raw)
To: Edward Liaw; +Cc: kernel-team, LTP List
[-- Attachment #1.1: Type: text/plain, Size: 641 bytes --]
On Mon, Apr 11, 2022 at 11:17 PM Edward Liaw via ltp <ltp@lists.linux.it>
wrote:
Hi, I'm working to get fzsync working with the Android kernel, which
> does not have pthread_cancel available.
>
> In the absence of pthread_cancel, when thread A exits due to a break,
> thread B will get stuck in an infinite loop while waiting for thread A
> to progress.
>
> Instead of cancelling thread B, we can use the exit flag to break out of
> thread B's loop. This should also remove the need for the wrapper
> around the thread.
>
> Signed-off-by: Edward Liaw <edliaw@google.com>
>
Reviewed-by: Li Wang <liwang@redhat.com>
--
Regards,
Li Wang
[-- Attachment #1.2: Type: text/html, Size: 1483 bytes --]
[-- Attachment #2: Type: text/plain, Size: 60 bytes --]
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [LTP] [PATCH v2] fzsync: break inf loop with flag vs pthread_cancel
2022-04-12 2:40 ` Li Wang
@ 2022-04-12 9:40 ` Richard Palethorpe
0 siblings, 0 replies; 3+ messages in thread
From: Richard Palethorpe @ 2022-04-12 9:40 UTC (permalink / raw)
To: Li Wang; +Cc: kernel-team, LTP List
Hello Edward and Li,
Li Wang <liwang@redhat.com> writes:
> On Mon, Apr 11, 2022 at 11:17 PM Edward Liaw via ltp <ltp@lists.linux.it> wrote:
>
> Hi, I'm working to get fzsync working with the Android kernel, which
> does not have pthread_cancel available.
>
> In the absence of pthread_cancel, when thread A exits due to a break,
> thread B will get stuck in an infinite loop while waiting for thread A
> to progress.
>
> Instead of cancelling thread B, we can use the exit flag to break out of
> thread B's loop. This should also remove the need for the wrapper
> around the thread.
>
> Signed-off-by: Edward Liaw <edliaw@google.com>
>
> Reviewed-by: Li Wang <liwang@redhat.com>
Awesome. Merged with some indentation fixes, it seems like you editor
used tab for the first indent then spaces thereafter.
--
Thank you,
Richard.
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-04-12 9:50 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-04-11 15:16 [LTP] [PATCH v2] fzsync: break inf loop with flag vs pthread_cancel Edward Liaw via ltp
2022-04-12 2:40 ` Li Wang
2022-04-12 9:40 ` Richard Palethorpe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox