Skip to contentSkip to conversation

Fix rt_task_unblock for RT_MUTEX

PATCH Applied Richard Weinberger 2 participants 8 messages May 24, 2024 – May 29, 2024

lore
Share & tools
lore
#0 lore raw

v2 cover Fix rt_task_unblock for RT_MUTEX

Changes since v1 [0]:
- Better names for EINTR capable functions
- Remove want_eintr flag and loop around __pthread_mutex_lock()
- Correctly integrate mutex-2 test into smokey test framework

[0]: https://lore.kernel.org/xenomai/[email protected]/T/#mf76a46eaa5ec033bacdfc63ca219af3bb84356b3

Richard Weinberger (2):
Alchemy: Fix rt_task_unblock() for RT_MUTEX
alchemytests: Add test case for RT_MUTEX and -EINTR

 include/cobalt/pthread.h                     |   5 +
 lib/alchemy/mutex.c                          |   8 ++
 lib/cobalt/mutex.c                           |  44 ++++++--
 testsuite/smokey/alchemytests/Makefile.am    |   5 +
 testsuite/smokey/alchemytests/alchemytests.c |   1 +
 testsuite/smokey/alchemytests/mutex-2.c      | 111 +++++++++++++++++++
 6 files changed, 164 insertions(+), 10 deletions(-)
 create mode 100644 testsuite/smokey/alchemytests/mutex-2.c
Signature · 1 line
2.35.3
#1 lore raw

PATCH v2 1/2 Alchemy: Fix rt_task_unblock() for RT_MUTEX

Starting with Xenomai 3, RT_MUTEX is based on libcobalt's pthread mutex implementation.
POSIX requires that pthread_mutex_lock() and pthread_mutex_timedlocks shall not return EINTR, this requirement breaks rt_task_unblock() if a RT_TASK blocks on a RT_MUTEX.

To restore the functionality provide a new flag to __pthread_mutex_lock() and __pthread_mutex_timedlock()
It can get interrupted and will return EINTR to the caller.

---
 include/cobalt/pthread.h |  5 +++++
 lib/alchemy/mutex.c      |  8 ++++++++
 lib/cobalt/mutex.c       | 44 +++++++++++++++++++++++++++++++---------
 3 files changed, 47 insertions(+), 10 deletions(-)
include/cobalt/pthread.h +5 -0
index 1a8991f5c..fed7dd2d8 100644
@@ -170,6 +170,11 @@ int pthread_attr_getpersonality_ex(const pthread_attr_ex_t *attr_ex,
 
 int pthread_attr_setpersonality_ex(pthread_attr_ex_t *attr_ex,
 				   int personality);
+
+int pthread_mutex_lock_interruptible_np(pthread_mutex_t *mutex);
+
+int pthread_timedmutex_lock_interruptible_np(pthread_mutex_t *mutex, const struct timespec *to);
+
 #ifdef __cplusplus
 }
 #endif
lib/alchemy/mutex.c +8 -0
index 9681e124e..6bec2d9bb 100644
@@ -327,7 +327,11 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 
 	/* Slow path. */
 	if (abs_timeout == NULL) {
+#ifdef CONFIG_XENO_MERCURY
 		ret = -__RT(pthread_mutex_lock(&mcb->lock));
+#else
+		ret = -pthread_mutex_lock_interruptible_np(&mcb->lock);
+#endif
 		goto done;
 	}
 
@@ -338,7 +342,11 @@ int rt_mutex_acquire_timed(RT_MUTEX *mutex,
 	 * the user timeout into something POSIX understands.
 	 */
 	clockobj_convert_clocks(&alchemy_clock, abs_timeout, CLOCK_REALTIME, &ts);
+#ifdef CONFIG_XENO_MERCURY
 	ret = -__RT(pthread_mutex_timedlock(&mcb->lock, &ts));
+#else
+	ret = -pthread_timedmutex_lock_interruptible_np(&mcb->lock, &ts);
+#endif
 done:
 	switch (ret) {
 	case -ENOTRECOVERABLE:
lib/cobalt/mutex.c +34 -10

83 lines not on this page · raw message

Signature · 1 line
2.35.3
#2 lore raw

PATCH v2 2/2 alchemytests: Add test case for RT_MUTEX and -EINTR

In reply to Richard Weinberger

Make sure rt_task_unblock() can unblock a mutex waiter.

---
 testsuite/smokey/alchemytests/Makefile.am    |   5 +
 testsuite/smokey/alchemytests/alchemytests.c |   1 +
 testsuite/smokey/alchemytests/mutex-2.c      | 111 +++++++++++++++++++
 3 files changed, 117 insertions(+)
 create mode 100644 testsuite/smokey/alchemytests/mutex-2.c
testsuite/smokey/alchemytests/Makefile.am +5 -0
index 3525dfa09..eba82f8ec 100644
@@ -20,6 +20,7 @@ test_PROGRAMS =				\
 		alchemytests_mq2	\
 		alchemytests_mq3	\
 		alchemytests_mutex1	\
+		alchemytests_mutex2	\
 		alchemytests_pipe1	\
 		alchemytests_sem1	\
 		alchemytests_sem2	\
@@ -69,6 +70,10 @@ alchemytests_mutex1_SOURCES = mutex-1.c
 alchemytests_mutex1_CPPFLAGS = $(alchemycppflags)
 alchemytests_mutex1_LDADD = $(alchemyldadd)
 alchemytests_mutex1_LDFLAGS = @XENO_AUTOINIT_LDFLAGS@
+alchemytests_mutex2_SOURCES = mutex-2.c
+alchemytests_mutex2_CPPFLAGS = $(alchemycppflags)
+alchemytests_mutex2_LDADD = $(alchemyldadd)
+alchemytests_mutex2_LDFLAGS = @XENO_AUTOINIT_LDFLAGS@
 alchemytests_pipe1_SOURCES = pipe-1.c
 alchemytests_pipe1_CPPFLAGS = $(alchemycppflags)
 alchemytests_pipe1_LDADD = $(alchemyldadd)
testsuite/smokey/alchemytests/alchemytests.c +1 -0
index 9a937f3a0..947fa37b9 100644
@@ -12,6 +12,7 @@ static const char * const tests[] = {
 	"alchemytests_mq2",
 	"alchemytests_mq3",
 	"alchemytests_mutex1",
+	"alchemytests_mutex2",
 	"alchemytests_pipe1",
 	"alchemytests_sem1",
 	"alchemytests_sem2",
testsuite/smokey/alchemytests/mutex-2.c new file +111 -0

111 lines not on this page · raw message

Signature · 1 line
2.35.3
Applied
#3 lore raw
In reply to Richard Weinberger
Quoted 11 lines · Richard Weinberger

Changes since v1 [0]:
- Better names for EINTR capable functions
- Remove want_eintr flag and loop around __pthread_mutex_lock()
- Correctly integrate mutex-2 test into smokey test framework

[0]: https://lore.kernel.org/xenomai/[email protected]/T/#mf76a46eaa5ec033bacdfc63ca219af3bb84356b3

Richard Weinberger (2):
Alchemy: Fix rt_task_unblock() for RT_MUTEX
alchemytests: Add test case for RT_MUTEX and -EINTR

Quoted 9 lines
 include/cobalt/pthread.h                     |   5 +
 lib/alchemy/mutex.c                          |   8 ++
 lib/cobalt/mutex.c                           |  44 ++++++--
 testsuite/smokey/alchemytests/Makefile.am    |   5 +
 testsuite/smokey/alchemytests/alchemytests.c |   1 +
 testsuite/smokey/alchemytests/mutex-2.c      | 111 +++++++++++++++++++
 6 files changed, 164 insertions(+), 10 deletions(-)
 create mode 100644 testsuite/smokey/alchemytests/mutex-2.c

Thanks, applied (just not yet pushed - our lab has some hick-up).

Jan

Signature · 2 lines
Siemens AG, Technology
Linux Expert Center
#4 lore raw
Quoted 22 lines · Jan Kiszka
Quoted 11 lines · Richard Weinberger

Changes since v1 [0]:
- Better names for EINTR capable functions
- Remove want_eintr flag and loop around __pthread_mutex_lock()
- Correctly integrate mutex-2 test into smokey test framework

[0]: https://lore.kernel.org/xenomai/[email protected]/T/#mf76a46eaa5ec033bacdfc63ca219af3bb84356b3

Richard Weinberger (2):
Alchemy: Fix rt_task_unblock() for RT_MUTEX
alchemytests: Add test case for RT_MUTEX and -EINTR

Quoted 9 lines
 include/cobalt/pthread.h                     |   5 +
 lib/alchemy/mutex.c                          |   8 ++
 lib/cobalt/mutex.c                           |  44 ++++++--
 testsuite/smokey/alchemytests/Makefile.am    |   5 +
 testsuite/smokey/alchemytests/alchemytests.c |   1 +
 testsuite/smokey/alchemytests/mutex-2.c      | 111 +++++++++++++++++++
 6 files changed, 164 insertions(+), 10 deletions(-)
 create mode 100644 testsuite/smokey/alchemytests/mutex-2.c
Quoted 2 lines

Thanks, applied (just not yet pushed - our lab has some hick-up).

Now done - all red:

2024-05-28T11:51:59 FAILURE rt_thread_body:117: pthread_mutex_lock

returned 4 instead of 0 - Interrupted system call

Please have a look.

Jan

Signature · 2 lines
Siemens AG, Technology
Linux Expert Center
#5 lore raw

----- Ursprüngliche Mail -----

Quoted 30 lines

Von: "Jan Kiszka" <[email protected]>
An: "richard" <[email protected]>, "xenomai" <[email protected]>
CC: [email protected]
Gesendet: Dienstag, 28. Mai 2024 15:36:14
Betreff: Re: [PATCH v2 0/2] Fix rt_task_unblock for RT_MUTEX

Quoted 23 lines · Jan Kiszka
Quoted 21 lines · Richard Weinberger

Changes since v1 [0]:
- Better names for EINTR capable functions
- Remove want_eintr flag and loop around __pthread_mutex_lock()
- Correctly integrate mutex-2 test into smokey test framework

[0]:
https://lore.kernel.org/xenomai/[email protected]/T/#mf76a46eaa5ec033bacdfc63ca219af3bb84356b3

Richard Weinberger (2):
Alchemy: Fix rt_task_unblock() for RT_MUTEX
alchemytests: Add test case for RT_MUTEX and -EINTR

 include/cobalt/pthread.h                     |   5 +
 lib/alchemy/mutex.c                          |   8 ++
 lib/cobalt/mutex.c                           |  44 ++++++--
 testsuite/smokey/alchemytests/Makefile.am    |   5 +
 testsuite/smokey/alchemytests/alchemytests.c |   1 +
 testsuite/smokey/alchemytests/mutex-2.c      | 111 +++++++++++++++++++
 6 files changed, 164 insertions(+), 10 deletions(-)
 create mode 100644 testsuite/smokey/alchemytests/mutex-2.c
Quoted 4 lines
Quoted 2 lines

Thanks, applied (just not yet pushed - our lab has some hick-up).

Now done - all red:

*grrr* sorry for that.

Quoted 2 lines
2024-05-28T11:51:59 FAILURE rt_thread_body:117: pthread_mutex_lock

returned 4 instead of 0 - Interrupted system call

I'm pretty sure the problem is:

... while (ret == -EINTR);

Should be ret == EINTR because -ret is returned.

Thanks,
//richard

#6 lore raw
Quoted 38 lines · Richard Weinberger

----- Ursprüngliche Mail -----

Quoted 30 lines

Von: "Jan Kiszka" <[email protected]>
An: "richard" <[email protected]>, "xenomai" <[email protected]>
CC: [email protected]
Gesendet: Dienstag, 28. Mai 2024 15:36:14
Betreff: Re: [PATCH v2 0/2] Fix rt_task_unblock for RT_MUTEX

Quoted 23 lines · Jan Kiszka
Quoted 21 lines · Richard Weinberger

Changes since v1 [0]:
- Better names for EINTR capable functions
- Remove want_eintr flag and loop around __pthread_mutex_lock()
- Correctly integrate mutex-2 test into smokey test framework

[0]:
https://lore.kernel.org/xenomai/[email protected]/T/#mf76a46eaa5ec033bacdfc63ca219af3bb84356b3

Richard Weinberger (2):
Alchemy: Fix rt_task_unblock() for RT_MUTEX
alchemytests: Add test case for RT_MUTEX and -EINTR

 include/cobalt/pthread.h                     |   5 +
 lib/alchemy/mutex.c                          |   8 ++
 lib/cobalt/mutex.c                           |  44 ++++++--
 testsuite/smokey/alchemytests/Makefile.am    |   5 +
 testsuite/smokey/alchemytests/alchemytests.c |   1 +
 testsuite/smokey/alchemytests/mutex-2.c      | 111 +++++++++++++++++++
 6 files changed, 164 insertions(+), 10 deletions(-)
 create mode 100644 testsuite/smokey/alchemytests/mutex-2.c
Quoted 4 lines
Quoted 2 lines

Thanks, applied (just not yet pushed - our lab has some hick-up).

Now done - all red:

*grrr* sorry for that.

Quoted 9 lines
Quoted 2 lines
2024-05-28T11:51:59 FAILURE rt_thread_body:117: pthread_mutex_lock

returned 4 instead of 0 - Interrupted system call

I'm pretty sure the problem is:

... while (ret == -EINTR);

Should be ret == EINTR because -ret is returned.

Seems that was it, fixed up now in next.

Jan

Signature · 2 lines
Siemens AG, Technology
Linux Expert Center
#7 lore raw

----- Ursprüngliche Mail -----

Quoted 11 lines

Von: "Jan Kiszka" <[email protected]>

Quoted 10 lines
Quoted 4 lines
Quoted 2 lines

Thanks, applied (just not yet pushed - our lab has some hick-up).

Now done - all red:

*grrr* sorry for that.

Quoted 2 lines
2024-05-28T11:51:59 FAILURE rt_thread_body:117: pthread_mutex_lock

returned 4 instead of 0 - Interrupted system call

Quoted 8 lines
Quoted 6 lines

I'm pretty sure the problem is:

... while (ret == -EINTR);

Should be ret == EINTR because -ret is returned.

Seems that was it, fixed up now in next.

Thanks for the fixup, the resolution looks good!

--
Thanks,
//richard