Skip to contentSkip to conversation

fzsync: revoke thread_b if parent hits accidental break

PATCH New Li Wang 2 participants 2 messages Sep 25, 2019

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH v4 1/1 fzsync: revoke thread_b if parent hits 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. With new wrap_run_b involves enabling thread cancel at the start of the thread B, then do asynchronous cancellation in tst_fzsync_pair_cleanup if pair->exit is not being set to 1.

Workaround: [commit 2e74d996: Check recvmmsg exists before entering fuzzy loop]

Cc Richard Palethorpe <[email protected]>
Cc Cyril Hrubis <[email protected]>
---
 include/tst_fuzzy_sync.h | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
include/tst_fuzzy_sync.h +27 -2
index f9a1947c7..00c74e951 100644
@@ -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,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);
+			pair->thread_b = 0;
+			return;
+		}
 		SAFE_PTHREAD_JOIN(pair->thread_b, NULL);
 		pair->thread_b = 0;
 	}
 }
 
+/**
+ * Wrap run_b for tst_fzsync_pair_reset to enable pthread cancel
+ * at the start of the thread B.
+ */
+static void *wrap_run_b(void *run_b)
+{
+       void *(*real_run_b)(void *) = run_b;
+
+       if (real_run_b) {
+	       pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+	       pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+	       (*real_run_b)(NULL);
+       }
+
+       return NULL;
+}
+
 /**
  * Zero some stat fields
  *
@@ -272,7 +297,7 @@ static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair,
 	pair->b_cntr = 0;
 	pair->exit = 0;
 	if (run_b)
-		SAFE_PTHREAD_CREATE(&pair->thread_b, 0, run_b, 0);
+		SAFE_PTHREAD_CREATE(&pair->thread_b, 0, wrap_run_b, run_b);
 
 	pair->exec_time_start = (float)tst_timeout_remaining();
 }
Signature · 1 line
2.20.1
#1 lore raw

PATCH v4 1/1 fzsync: revoke thread_b if parent hits accidental break

Hello,

Quoted 38 lines · Li Wang

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. With new wrap_run_b involves enabling thread cancel at the start of the thread B, then do asynchronous cancellation in tst_fzsync_pair_cleanup if pair->exit is not being set to 1.

Workaround: [commit 2e74d996: Check recvmmsg exists before entering fuzzy loop]

Cc Richard Palethorpe <[email protected]>
Cc Cyril Hrubis <[email protected]>
---
 include/tst_fuzzy_sync.h | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
include/tst_fuzzy_sync.h +1 -0
index f9a1947c7..00c74e951 100644
@@ -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 227

Quoted 10 lines
include/tst_fuzzy_sync.h +6 -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);
+			pair->thread_b = 0;

We still need to join after using cancel, or?

Quoted 4 lines
include/tst_fuzzy_sync.h +2 -0
+			return;
+		}
 		SAFE_PTHREAD_JOIN(pair->thread_b, NULL);
 		pair->thread_b = 0;

include/tst_fuzzy_sync.h static void tst_fzsync_pair_init(struct tst_fzsync_pair *pair) · line 241

Quoted 10 lines
include/tst_fuzzy_sync.h +7 -0
 	}
 }
 
+/**
+ * Wrap run_b for tst_fzsync_pair_reset to enable pthread cancel
+ * at the start of the thread B.
+ */
+static void *wrap_run_b(void *run_b)
+{
+       void *(*real_run_b)(void *) = run_b;

The C standard doesn't actually allow this, except maybe C11. See my other e-mail to patch V3.

Quoted 14 lines
include/tst_fuzzy_sync.h +10 -0
+
+       if (real_run_b) {
+	       pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+	       pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+	       (*real_run_b)(NULL);
+       }
+
+       return NULL;
+}
+
 /**
  * Zero some stat fields
  *

include/tst_fuzzy_sync.h static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair, · line 303

Quoted 8 lines
include/tst_fuzzy_sync.h +1 -1
@@ -272,7 +297,7 @@ static void tst_fzsync_pair_reset(struct tst_fzsync_pair *pair,
 	pair->b_cntr = 0;
 	pair->exit = 0;
 	if (run_b)
-		SAFE_PTHREAD_CREATE(&pair->thread_b, 0, run_b, 0);
+		SAFE_PTHREAD_CREATE(&pair->thread_b, 0, wrap_run_b, run_b);
 
 	pair->exec_time_start = (float)tst_timeout_remaining();
 }

--
Thank you,
Richard.