Skip to contentSkip to conversation

crypto/af_alg02: use pthread_tryjoin_np() instead of pthread_kill()

PATCH New Alexey Kodanev 3 participants 8 messages Jul 15, 2021 – Jul 16, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto/af_alg02: use pthread_tryjoin_np() instead of pthread_kill()

musl doesn't return ESRCH for pthread_kill() if thread id is not found.

POSIX only recommends to return ESRCH, and also says that pthread_kill() produces undefined behavior if tid lifetime has ended [1].

[1]: https://man7.org/linux/man-pages/man3/pthread_kill.3.html

---
 testcases/kernel/crypto/af_alg02.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/kernel/crypto/af_alg02.c +1 -1
index 31d30777c..0f5793c16 100644
@@ -60,7 +60,7 @@ static void run(void)
 
 	TST_CHECKPOINT_WAIT(0);
 
-	while (pthread_kill(thr, 0) != ESRCH) {
+	while (pthread_tryjoin_np(thr, NULL) == EBUSY) {
 		if (tst_timeout_remaining() <= 10) {
 			pthread_cancel(thr);
 			tst_brk(TBROK,
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 crypto/af_alg02: use pthread_tryjoin_np() instead of pthread_kill()

Hi Alexey,

Quoted 18 lines · Alexey Kodanev

musl doesn't return ESRCH for pthread_kill() if thread id is not found.

POSIX only recommends to return ESRCH, and also says that pthread_kill() produces undefined behavior if tid lifetime has ended [1].

[1]: https://man7.org/linux/man-pages/man3/pthread_kill.3.html

---
 testcases/kernel/crypto/af_alg02.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/kernel/crypto/af_alg02.c +0 -0
index 31d30777c..0f5793c16 100644

testcases/kernel/crypto/af_alg02.c static void run(void) · line 64

Quoted 6 lines
testcases/kernel/crypto/af_alg02.c +1 -1
@@ -60,7 +60,7 @@ static void run(void)
 
        TST_CHECKPOINT_WAIT(0);
 
-       while (pthread_kill(thr, 0) != ESRCH) {
+       while (pthread_tryjoin_np(thr, NULL) == EBUSY) {
 

I'm not sure if safe enough to use because it is nonstandard GNU extensions and the "_np" means nonportable.

Maybe another workaround is to define a volatile flag 'thread_complete', initialize it to '0' when thread_B starts and reset to '1' while exit, and just
do a value check in the while loop of thread_A should acquire thread_B status.
Is this way a bit better?

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210716/67185a7b/attachment.htm>
#2 lore raw

PATCH 1/1 crypto/af_alg02: use pthread_tryjoin_np() instead of pthread_kill()

Quoted 6 lines

Hi Alexey,

Quoted 1 line · Alexey Kodanev

musl doesn't return ESRCH for pthread_kill() if thread id is not found.

Maybe ask on MUSL mailing list?

Quoted 17 lines
Quoted 16 lines

POSIX only recommends to return ESRCH, and also says that pthread_kill() produces undefined behavior if tid lifetime has ended [1].

[1]: https://man7.org/linux/man-pages/man3/pthread_kill.3.html

---
 testcases/kernel/crypto/af_alg02.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/kernel/crypto/af_alg02.c +0 -0
index 31d30777c..0f5793c16 100644
@@ -60,7 +60,7 @@ static void run(void)
Quoted 8 lines

testcases/kernel/crypto/af_alg02.c static void run(void) · line 63

Quoted 4 lines
testcases/kernel/crypto/af_alg02.c +1 -1
        TST_CHECKPOINT_WAIT(0);
 
-       while (pthread_kill(thr, 0) != ESRCH) {
+       while (pthread_tryjoin_np(thr, NULL) == EBUSY) {

I'm not sure if safe enough to use because it is nonstandard GNU extensions and the "_np" means nonportable.

Others please double check, but pthread_tryjoin_np() seems to be in uclibc-ng and musl (+ of course in glibc). It's only missing in bionic (it looks like people would like to have it [1]).

Quoted 6 lines

Maybe another workaround is to define a volatile flag 'thread_complete', initialize it to '0' when thread_B starts and reset to '1' while exit, and just
do a value check in the while loop of thread_A should acquire thread_B status.
Is this way a bit better?

Sounds as reasonable workaround for me.

Kind regards,
Petr

[1] https://github.com/kito-cheng/android-checkpoint/blob/master/bionic/0003-bionic-Implement-pthread_tryjoin_np.patch

#3 lore raw

PATCH 1/1 crypto/af_alg02: use pthread_tryjoin_np() instead of pthread_kill()

Hi Petr,

Quoted 7 lines · Petr Vorel
Quoted 6 lines

Hi Alexey,

Quoted 1 line · Alexey Kodanev

musl doesn't return ESRCH for pthread_kill() if thread id is not found.

Maybe ask on MUSL mailing list?

It's not a musl issue, but I was going to send a few improvements (including this) when time permits.

Quoted 24 lines
Quoted 22 lines
Quoted 22 lines

POSIX only recommends to return ESRCH, and also says that pthread_kill() produces undefined behavior if tid lifetime has ended [1].

[1]: https://man7.org/linux/man-pages/man3/pthread_kill.3.html

---
 testcases/kernel/crypto/af_alg02.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/kernel/crypto/af_alg02.c +1 -1
index 31d30777c..0f5793c16 100644
@@ -60,7 +60,7 @@ static void run(void)
 
        TST_CHECKPOINT_WAIT(0);
 
-       while (pthread_kill(thr, 0) != ESRCH) {
+       while (pthread_tryjoin_np(thr, NULL) == EBUSY) {
Quoted 6 lines
Quoted 2 lines

I'm not sure if safe enough to use because it is nonstandard GNU extensions and the "_np" means nonportable.

Others please double check, but pthread_tryjoin_np() seems to be in uclibc-ng and musl (+ of course in glibc). It's only missing in bionic (it looks like people would like to have it [1]).

Yeah, I think it's quite useful.

Quoted 13 lines
Quoted 6 lines

Maybe another workaround is to define a volatile flag 'thread_complete', initialize it to '0' when thread_B starts and reset to '1' while exit, and just
do a value check in the while loop of thread_A should acquire thread_B status.
Is this way a bit better?

Sounds as reasonable workaround for me.

Kind regards,
Petr

[1] https://github.com/kito-cheng/android-checkpoint/blob/master/bionic/0003-bionic-Implement-pthread_tryjoin_np.patch

#4 lore raw

PATCH 1/1 crypto/af_alg02: use pthread_tryjoin_np() instead of pthread_kill()

In reply to Li Wang

Hi Li,

Quoted 28 lines · Li Wang

Hi Alexey,

On Thu, Jul 15, 2021 at 6:29 PM Alexey Kodanev <[email protected] <mailto:[email protected]>> wrote:

    musl doesn't return ESRCH for pthread_kill() if thread id is not found.

    POSIX only recommends to return ESRCH, and also says that pthread_kill()
    produces undefined behavior if tid lifetime has ended [1].

    [1]: https://man7.org/linux/man-pages/man3/pthread_kill.3.html <https://man7.org/linux/man-pages/man3/pthread_kill.3.html>

    Signed-off-by: Alexey Kodanev <[email protected] <mailto:[email protected]>>
    ---
    ?testcases/kernel/crypto/af_alg02.c | 2 +-
    ?1 file changed, 1 insertion(+), 1 deletion(-)

    diff --git a/testcases/kernel/crypto/af_alg02.c b/testcases/kernel/crypto/af_alg02.c
    index 31d30777c..0f5793c16 100644
    --- a/testcases/kernel/crypto/af_alg02.c
    +++ b/testcases/kernel/crypto/af_alg02.c
    @@ -60,7 +60,7 @@ static void run(void)

    ? ? ? ? TST_CHECKPOINT_WAIT(0);

    -? ? ? ?while (pthread_kill(thr, 0) != ESRCH) {
    +? ? ? ?while (pthread_tryjoin_np(thr, NULL) == EBUSY) {
Quoted 7 lines

I'm not sure ifsafeenough to use because it is nonstandard GNU extensions and the "_np" means nonportable.

Maybe another workaround is to define a volatile?flag 'thread_complete',? initialize it to '0' when thread_B starts and reset to '1' while exit, and just do a value check in the while loop of thread_A should acquire?thread_B status. Is this way a bit better?

OK, why not, so something like this:

testcases/kernel/crypto/af_alg02.c +5 -1
index 0f5793c16..1fe0f3bf0 100644
@@ -18,11 +18,13 @@
 #include "tst_test.h"
 #include "tst_af_alg.h"
 #include "tst_safe_pthread.h"
+#include "tst_atomic.h"
 #include <pthread.h>
 #include <errno.h>
 
 #define SALSA20_IV_SIZE       8
 #define SALSA20_MIN_KEY_SIZE  16
+static int completed;
 
 static void *verify_encrypt(void *arg)
 {
@@ -48,6 +50,8 @@ static void *verify_encrypt(void *arg)
                tst_res(TPASS, "Successfully \"encrypted\" an empty message");
        else
                tst_res(TFAIL, "read() didn't return 0");
+
+       tst_atomic_store(1, &completed);
        return arg;
 }
 
@@ -60,7 +64,7 @@ static void run(void)
 
        TST_CHECKPOINT_WAIT(0);
 
-       while (pthread_tryjoin_np(thr, NULL) == EBUSY) {
+       while (!tst_atomic_load(&completed)) {
                if (tst_timeout_remaining() <= 10) {
                        pthread_cancel(thr);
                        tst_brk(TBROK,
#5 lore raw

PATCH 1/1 crypto/af_alg02: use pthread_tryjoin_np() instead of pthread_kill()

Quoted 40 lines · Alexey Kodanev
Quoted 3 lines

Maybe another workaround is to define a volatile flag 'thread_complete', initialize it to '0' when thread_B starts and reset to '1' while exit,

and just

Quoted 1 line

do a value check in the while loop of thread_A should acquire thread_B

status.

Quoted 1 line

Is this way a bit better?

OK, why not, so something like this:

testcases/kernel/crypto/af_alg02.c +4 -0
index 0f5793c16..1fe0f3bf0 100644
@@ -18,11 +18,13 @@
 #include "tst_test.h"
 #include "tst_af_alg.h"
 #include "tst_safe_pthread.h"
+#include "tst_atomic.h"
 #include <pthread.h>
 #include <errno.h>
 
 #define SALSA20_IV_SIZE       8
 #define SALSA20_MIN_KEY_SIZE  16
+static int completed;
 
 static void *verify_encrypt(void *arg)
 {
@@ -48,6 +50,8 @@ static void *verify_encrypt(void *arg)
                tst_res(TPASS, "Successfully \"encrypted\" an empty
 message");
        else
                tst_res(TFAIL, "read() didn't return 0");
+
+       tst_atomic_store(1, &completed);
        return arg;
 }

testcases/kernel/crypto/af_alg02.c static void run(void) · line 68

Quoted 6 lines
testcases/kernel/crypto/af_alg02.c +1 -1
@@ -60,7 +64,7 @@ static void run(void)
 
        TST_CHECKPOINT_WAIT(0);
 
-       while (pthread_tryjoin_np(thr, NULL) == EBUSY) {
+       while (!tst_atomic_load(&completed)) {
 

+1
The atomic method is quite awesome!

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210716/e43fce96/attachment.htm>
#6 lore raw

PATCH 1/1 crypto/af_alg02: use pthread_tryjoin_np() instead of pthread_kill()

Quoted 8 lines

#define SALSA20_IV_SIZE 8

Quoted 6 lines

#define SALSA20_MIN_KEY_SIZE 16
+static int completed;

static void *verify_encrypt(void *arg)
{

But we still need to initialize '0' at the start of thread_B, in case of test running with '-i xx' parameter. Isn't it?

     tst_atomic_store(0, &completed);
Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210716/96e883bb/attachment.htm>
#7 lore raw

PATCH 1/1 crypto/af_alg02: use pthread_tryjoin_np() instead of pthread_kill()

Quoted 9 lines · Li Wang
        ?#define SALSA20_IV_SIZE? ? ? ?8
        ?#define SALSA20_MIN_KEY_SIZE? 16
        +static int completed;

        ?static void *verify_encrypt(void *arg)
        ?{
Quoted 5 lines

But we still need to initialize?'0' at the start of thread_B, in case of test?running with '-i xx' parameter. Isn't it?

? ???tst_atomic_store(0, &completed);

Yeah, right, and that's another reason to use pthread_tryjoin_np() :)

Because otherwise the thread resources not released... anyway we could add pthread_join in the end of the run()...

Quoted 3 lines
Signature · 2 lines
Regards,
Li Wang