Skip to contentSkip to conversation

Policy switching and XNOTHER maintenance

Xenomai-core Jan Kiszka 3 participants 14 messages Sep 11, 2011 – Sep 18, 2011

Subsystems (2)
lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Looks like we don't support this at all ATM:

do_setsched_event ignores events that enabled SCHED_OTHER, and the XNOTHER flag is only set on xnshadow_map, not updated anywhere else. Fixing this is not straightforward as that flag resides in a state variable that is owned by the thread (ie. updated non-atomically) while do_setsched_event can also be called over different contexts.

Or am I missing something?

In the light of this, I would vote for reverting f9bfab3457 ("only update rescnt for XNOTHER threads") as it only adds conditional branches to the hot paths without solving the issue.

Jan

author
#1 lore raw
Quoted 5 lines · Jan Kiszka

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Quoted 7 lines

Looks like we don't support this at all ATM:

do_setsched_event ignores events that enabled SCHED_OTHER, and the XNOTHER flag is only set on xnshadow_map, not updated anywhere else. Fixing this is not straightforward as that flag resides in a state variable that is owned by the thread (ie. updated non-atomically) while do_setsched_event can also be called over different contexts.

OK, it just takes nklock to update the thread state.

Quoted 2 lines

Or am I missing something?

From __xnpod_set_thread_schedparam:

"A non-real-time shadow may upgrade to real-time FIFO scheduling, but the latter may never downgrade to SCHED_NORMAL Xenomai-wise. In the valid case, we clear XNOTHER to reflect the change."

What prevents us from setting XNOTHER if the priority drops to 0, i.e. the Linux task re-enters SCHED_NORMAL (while Xenomai will still schedule it via its RT class of course)? We cannot deny such a transition on the Linux side anyway, can we?

Jan

#2 lore raw
In reply to Jan Kiszka
Quoted 4 lines · Jan Kiszka

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Doing this while holding a mutex looks invalid. If we do not do it, the current code is valid.

Signature · 1 line
                                                                Gilles.
author
#3 lore raw
Quoted 7 lines · Gilles Chanteperdrix
Quoted 4 lines · Jan Kiszka

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Doing this while holding a mutex looks invalid.

Looking at POSIX e.g., is there anything in the spec that makes this invalid? If the kernel preserves or established proper priority boosting, I do not see what could break in principle.

It is nothing I would design into some app, but we should somehow handle it (doc update or code adjustments).

Quoted 1 line

If we do not do it, the current code is valid.

Except for its dependency on XNOTHER which is not updated on RT->NORMAL transitions.

Jan

#4 lore raw
Quoted 13 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 4 lines · Jan Kiszka

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Doing this while holding a mutex looks invalid.

Looking at POSIX e.g., is there anything in the spec that makes this invalid? If the kernel preserves or established proper priority boosting, I do not see what could break in principle.

Quoted 7 lines

It is nothing I would design into some app, but we should somehow handle it (doc update or code adjustments).

Quoted 1 line

If we do not do it, the current code is valid.

Except for its dependency on XNOTHER which is not updated on RT->NORMAL transitions.

The fact that this update did not take place made the code work. No negative rescnt could happen with that code.

Anyway, here is a patch to allow switching back from RT to NORMAL, but send a SIGDEBUG to a thread attempting to release a mutex while its counter is already 0. We end up avoiding a big chunk of code that would have been useful for a really strange corner case.

examples/native/sigdebug.c +2 -0
index e20714b..7b840f9 100644
@@ -34,6 +34,8 @@ static const char *reason_str[] = {
     [SIGDEBUG_MIGRATE_PRIOINV] = "affected by priority inversion",
     [SIGDEBUG_NOMLOCK] = "missing mlockall",
     [SIGDEBUG_WATCHDOG] = "runaway thread",
+    [SIGDEBUG_SCHED_CHANGE_W_MX] =
+    "switched to SCHED_OTHER policy with mutex held",
 };
 
 void warn_upon_switch(int sig, siginfo_t *si, void *context)
include/asm-generic/syscall.h +1 -0
index a9cdc87..5b6c764 100644
@@ -61,6 +61,7 @@ typedef struct xnsysinfo {
 #define SIGDEBUG_MIGRATE_PRIOINV	4
 #define SIGDEBUG_NOMLOCK		5
 #define SIGDEBUG_WATCHDOG		6
+#define SIGDEBUG_SCHED_CHANGE_W_MX      7
 
 #ifdef __KERNEL__
 
include/nucleus/sched-idle.h +1 -0
index 6399a17..3cc7185 100644
@@ -39,6 +39,7 @@ extern struct xnsched_class xnsched_class_idle;
 static inline void __xnsched_idle_setparam(struct xnthread *thread,
 					   const union xnsched_policy_param *p)
 {
+	xnthread_set_state(thread, XNOTHER);
 	thread->cprio = p->idle.prio;
 }
 
include/nucleus/sched-rt.h +4 -0
index 71f655c..d17b058 100644
@@ -86,6 +86,10 @@ static inline void __xnsched_rt_setparam(struct xnthread *thread,
 					 const union xnsched_policy_param *p)
 {
 	thread->cprio = p->rt.prio;
+	if (thread->cprio)
+		xnthread_clear_state(thread, XNOTHER);
+	else
+		xnthread_set_state(thread, XNOTHER);
 }
 
 static inline void __xnsched_rt_getparam(struct xnthread *thread,
ksrc/nucleus/pod.c +0 -10
index 9a02e80..d19999f 100644
@@ -1896,16 +1896,6 @@ int __xnpod_set_thread_schedparam(struct xnthread *thread,
 		xnsched_putback(thread);
 
 #ifdef CONFIG_XENO_OPT_PERVASIVE
-	/*
-	 * A non-real-time shadow may upgrade to real-time FIFO
-	 * scheduling, but the latter may never downgrade to
-	 * SCHED_NORMAL Xenomai-wise. In the valid case, we clear
-	 * XNOTHER to reflect the change. Note that we keep handling
-	 * non real-time shadow specifics in higher code layers, not
-	 * to pollute the core scheduler with peculiarities.
-	 */
-	if (sched_class == &xnsched_class_rt && sched_param->rt.prio > 0)
-		xnthread_clear_state(thread, XNOTHER);
 	if (propagate) {
 		if (xnthread_test_state(thread, XNRELAX))
 			xnshadow_renice(thread);
ksrc/nucleus/sched-sporadic.c +1 -0
index fd37c21..587be4a 100644
@@ -258,6 +258,7 @@ static void xnsched_sporadic_setparam(struct xnthread *thread,
 		}
 	}
 
+	xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->pss.current_prio;
 }
 
ksrc/nucleus/sched-tp.c +1 -0
index 43a548e..2423e81 100644
@@ -100,6 +100,7 @@ static void xnsched_tp_setparam(struct xnthread *thread,
 {
 	struct xnsched *sched = thread->sched;
 
+	xnthread_clear_state(thread, XNOTHER);
 	thread->tps = &sched->tp.partitions[p->tp.ptid];
 	thread->cprio = p->tp.prio;
 }
ksrc/nucleus/synch.c +7 -3
index b956e46..e3442a4 100644
@@ -684,9 +684,13 @@ xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner)
 
 	XENO_BUGON(NUCLEUS, !testbits(synch->status, XNSYNCH_OWNER));
 
-	if (xnthread_test_state(lastowner, XNOTHER))
-		xnthread_dec_rescnt(lastowner);
-	XENO_BUGON(NUCLEUS, xnthread_get_rescnt(lastowner) < 0);
+	if (xnthread_test_state(lastowner, XNOTHER)) {
+		if (xnthread_get_rescnt(lastowner))
+			xnshadow_send_sig(lastowner, SIGDEBUG,
+					  SIGDEBUG_SCHED_CHANGE_W_MX, 1);
+		else
+			xnthread_dec_rescnt(lastowner);
+	}
 	lastownerh = xnthread_handle(lastowner);
 
 	if (use_fastlock &&
Signature · 1 line
                                                                Gilles.
#5 lore raw
Quoted 22 lines · Gilles Chanteperdrix
Quoted 13 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 4 lines · Jan Kiszka

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Doing this while holding a mutex looks invalid.

Looking at POSIX e.g., is there anything in the spec that makes this invalid? If the kernel preserves or established proper priority boosting, I do not see what could break in principle.

Quoted 7 lines

It is nothing I would design into some app, but we should somehow handle it (doc update or code adjustments).

Quoted 1 line

If we do not do it, the current code is valid.

Except for its dependency on XNOTHER which is not updated on RT->NORMAL transitions.

Quoted 8 lines

The fact that this update did not take place made the code work. No negative rescnt could happen with that code.

Anyway, here is a patch to allow switching back from RT to NORMAL, but send a SIGDEBUG to a thread attempting to release a mutex while its counter is already 0. We end up avoiding a big chunk of code that would have been useful for a really strange corner case.

Here comes version 2:

include/nucleus/sched-idle.h +2 -0
index 6399a17..417170f 100644
@@ -39,6 +39,8 @@ extern struct xnsched_class xnsched_class_idle;
 static inline void __xnsched_idle_setparam(struct xnthread *thread,
 					   const union xnsched_policy_param *p)
 {
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->idle.prio;
 }
 
include/nucleus/sched-rt.h +6 -0
index 71f655c..cc1cefa 100644
@@ -86,6 +86,12 @@ static inline void __xnsched_rt_setparam(struct xnthread *thread,
 					 const union xnsched_policy_param *p)
 {
 	thread->cprio = p->rt.prio;
+	if (xnthread_test_state(thread, XNSHADOW)) {
+		if (thread->cprio)
+			xnthread_clear_state(thread, XNOTHER);
+		else
+			xnthread_set_state(thread, XNOTHER);
+	}
 }
 
 static inline void __xnsched_rt_getparam(struct xnthread *thread,
ksrc/nucleus/pod.c +0 -10
index 9a02e80..d19999f 100644
@@ -1896,16 +1896,6 @@ int __xnpod_set_thread_schedparam(struct xnthread *thread,
 		xnsched_putback(thread);
 
 #ifdef CONFIG_XENO_OPT_PERVASIVE
-	/*
-	 * A non-real-time shadow may upgrade to real-time FIFO
-	 * scheduling, but the latter may never downgrade to
-	 * SCHED_NORMAL Xenomai-wise. In the valid case, we clear
-	 * XNOTHER to reflect the change. Note that we keep handling
-	 * non real-time shadow specifics in higher code layers, not
-	 * to pollute the core scheduler with peculiarities.
-	 */
-	if (sched_class == &xnsched_class_rt && sched_param->rt.prio > 0)
-		xnthread_clear_state(thread, XNOTHER);
 	if (propagate) {
 		if (xnthread_test_state(thread, XNRELAX))
 			xnshadow_renice(thread);
ksrc/nucleus/sched-sporadic.c +2 -0
index fd37c21..ffc9bab 100644
@@ -258,6 +258,8 @@ static void xnsched_sporadic_setparam(struct xnthread *thread,
 		}
 	}
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->pss.current_prio;
 }
 
ksrc/nucleus/sched-tp.c +2 -0
index 43a548e..a2af1d3 100644
@@ -100,6 +100,8 @@ static void xnsched_tp_setparam(struct xnthread *thread,
 {
 	struct xnsched *sched = thread->sched;
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->tps = &sched->tp.partitions[p->tp.ptid];
 	thread->cprio = p->tp.prio;
 }
ksrc/nucleus/synch.c +7 -3
index b956e46..47bc0c5 100644
@@ -684,9 +684,13 @@ xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner)
 
 	XENO_BUGON(NUCLEUS, !testbits(synch->status, XNSYNCH_OWNER));
 
-	if (xnthread_test_state(lastowner, XNOTHER))
-		xnthread_dec_rescnt(lastowner);
-	XENO_BUGON(NUCLEUS, xnthread_get_rescnt(lastowner) < 0);
+	if (xnthread_test_state(lastowner, XNOTHER)) {
+		if (xnthread_get_rescnt(lastowner) == 0)
+			xnshadow_send_sig(lastowner, SIGDEBUG,
+					  SIGDEBUG_MIGRATE_PRIOINV, 1);
+		else
+			xnthread_dec_rescnt(lastowner);
+	}
 	lastownerh = xnthread_handle(lastowner);
 
 	if (use_fastlock &&
Signature · 1 line
                                                                Gilles.
#6 lore raw
Quoted 124 lines · Gilles Chanteperdrix
Quoted 22 lines · Gilles Chanteperdrix
Quoted 13 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 4 lines · Jan Kiszka

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Doing this while holding a mutex looks invalid.

Looking at POSIX e.g., is there anything in the spec that makes this invalid? If the kernel preserves or established proper priority boosting, I do not see what could break in principle.

Quoted 7 lines

It is nothing I would design into some app, but we should somehow handle it (doc update or code adjustments).

Quoted 1 line

If we do not do it, the current code is valid.

Except for its dependency on XNOTHER which is not updated on RT->NORMAL transitions.

Quoted 8 lines

The fact that this update did not take place made the code work. No negative rescnt could happen with that code.

Anyway, here is a patch to allow switching back from RT to NORMAL, but send a SIGDEBUG to a thread attempting to release a mutex while its counter is already 0. We end up avoiding a big chunk of code that would have been useful for a really strange corner case.

Here comes version 2:

include/nucleus/sched-idle.h +2 -0
index 6399a17..417170f 100644
@@ -39,6 +39,8 @@ extern struct xnsched_class xnsched_class_idle;
 static inline void __xnsched_idle_setparam(struct xnthread *thread,
 					   const union xnsched_policy_param *p)
 {
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->idle.prio;
 }
 
include/nucleus/sched-rt.h +6 -0
index 71f655c..cc1cefa 100644
@@ -86,6 +86,12 @@ static inline void __xnsched_rt_setparam(struct xnthread *thread,
 					 const union xnsched_policy_param *p)
 {
 	thread->cprio = p->rt.prio;
+	if (xnthread_test_state(thread, XNSHADOW)) {
+		if (thread->cprio)
+			xnthread_clear_state(thread, XNOTHER);
+		else
+			xnthread_set_state(thread, XNOTHER);
+	}
 }
 
 static inline void __xnsched_rt_getparam(struct xnthread *thread,
ksrc/nucleus/pod.c +0 -10
index 9a02e80..d19999f 100644
@@ -1896,16 +1896,6 @@ int __xnpod_set_thread_schedparam(struct xnthread *thread,
 		xnsched_putback(thread);
 
 #ifdef CONFIG_XENO_OPT_PERVASIVE
-	/*
-	 * A non-real-time shadow may upgrade to real-time FIFO
-	 * scheduling, but the latter may never downgrade to
-	 * SCHED_NORMAL Xenomai-wise. In the valid case, we clear
-	 * XNOTHER to reflect the change. Note that we keep handling
-	 * non real-time shadow specifics in higher code layers, not
-	 * to pollute the core scheduler with peculiarities.
-	 */
-	if (sched_class == &xnsched_class_rt && sched_param->rt.prio > 0)
-		xnthread_clear_state(thread, XNOTHER);
 	if (propagate) {
 		if (xnthread_test_state(thread, XNRELAX))
 			xnshadow_renice(thread);
ksrc/nucleus/sched-sporadic.c +2 -0
index fd37c21..ffc9bab 100644
@@ -258,6 +258,8 @@ static void xnsched_sporadic_setparam(struct xnthread *thread,
 		}
 	}
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->pss.current_prio;
 }
 
ksrc/nucleus/sched-tp.c +2 -0
index 43a548e..a2af1d3 100644
@@ -100,6 +100,8 @@ static void xnsched_tp_setparam(struct xnthread *thread,
 {
 	struct xnsched *sched = thread->sched;
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->tps = &sched->tp.partitions[p->tp.ptid];
 	thread->cprio = p->tp.prio;
 }
ksrc/nucleus/synch.c +0 -3
index b956e46..47bc0c5 100644
@@ -684,9 +684,13 @@ xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner)
 
 	XENO_BUGON(NUCLEUS, !testbits(synch->status, XNSYNCH_OWNER));
 
-	if (xnthread_test_state(lastowner, XNOTHER))
-		xnthread_dec_rescnt(lastowner);
-	XENO_BUGON(NUCLEUS, xnthread_get_rescnt(lastowner) < 0);

ksrc/nucleus/synch.c xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner) · line 696

Quoted 10 lines
ksrc/nucleus/synch.c +7 -0
+	if (xnthread_test_state(lastowner, XNOTHER)) {
+		if (xnthread_get_rescnt(lastowner) == 0)
+			xnshadow_send_sig(lastowner, SIGDEBUG,
+					  SIGDEBUG_MIGRATE_PRIOINV, 1);
+		else
+			xnthread_dec_rescnt(lastowner);
+	}
 	lastownerh = xnthread_handle(lastowner);
 
 	if (use_fastlock &&

Agreed, the logic of this patch sounds right. Switching from -rt to -nrt while holding a -rt mutex is pathological behavior. We don't have to support apps implementing voluntary priority inversion.

Signature · 1 line
Philippe.
author
#7 lore raw
Quoted 131 lines · Philippe Gerum
Quoted 130 lines · Gilles Chanteperdrix
Quoted 22 lines · Gilles Chanteperdrix
Quoted 13 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 4 lines · Jan Kiszka

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Doing this while holding a mutex looks invalid.

Looking at POSIX e.g., is there anything in the spec that makes this invalid? If the kernel preserves or established proper priority boosting, I do not see what could break in principle.

Quoted 7 lines

It is nothing I would design into some app, but we should somehow handle it (doc update or code adjustments).

Quoted 1 line

If we do not do it, the current code is valid.

Except for its dependency on XNOTHER which is not updated on RT->NORMAL transitions.

Quoted 8 lines

The fact that this update did not take place made the code work. No negative rescnt could happen with that code.

Anyway, here is a patch to allow switching back from RT to NORMAL, but send a SIGDEBUG to a thread attempting to release a mutex while its counter is already 0. We end up avoiding a big chunk of code that would have been useful for a really strange corner case.

Here comes version 2:

include/nucleus/sched-idle.h +2 -0
index 6399a17..417170f 100644
@@ -39,6 +39,8 @@ extern struct xnsched_class xnsched_class_idle;
 static inline void __xnsched_idle_setparam(struct xnthread *thread,
 					   const union xnsched_policy_param *p)
 {
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->idle.prio;
 }
 
include/nucleus/sched-rt.h +6 -0
index 71f655c..cc1cefa 100644
@@ -86,6 +86,12 @@ static inline void __xnsched_rt_setparam(struct xnthread *thread,
 					 const union xnsched_policy_param *p)
 {
 	thread->cprio = p->rt.prio;
+	if (xnthread_test_state(thread, XNSHADOW)) {
+		if (thread->cprio)
+			xnthread_clear_state(thread, XNOTHER);
+		else
+			xnthread_set_state(thread, XNOTHER);
+	}
 }
 
 static inline void __xnsched_rt_getparam(struct xnthread *thread,
ksrc/nucleus/pod.c +0 -10
index 9a02e80..d19999f 100644
@@ -1896,16 +1896,6 @@ int __xnpod_set_thread_schedparam(struct xnthread *thread,
 		xnsched_putback(thread);
 
 #ifdef CONFIG_XENO_OPT_PERVASIVE
-	/*
-	 * A non-real-time shadow may upgrade to real-time FIFO
-	 * scheduling, but the latter may never downgrade to
-	 * SCHED_NORMAL Xenomai-wise. In the valid case, we clear
-	 * XNOTHER to reflect the change. Note that we keep handling
-	 * non real-time shadow specifics in higher code layers, not
-	 * to pollute the core scheduler with peculiarities.
-	 */
-	if (sched_class == &xnsched_class_rt && sched_param->rt.prio > 0)
-		xnthread_clear_state(thread, XNOTHER);
 	if (propagate) {
 		if (xnthread_test_state(thread, XNRELAX))
 			xnshadow_renice(thread);
ksrc/nucleus/sched-sporadic.c +2 -0
index fd37c21..ffc9bab 100644
@@ -258,6 +258,8 @@ static void xnsched_sporadic_setparam(struct xnthread *thread,
 		}
 	}
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->pss.current_prio;
 }
 
ksrc/nucleus/sched-tp.c +2 -0
index 43a548e..a2af1d3 100644
@@ -100,6 +100,8 @@ static void xnsched_tp_setparam(struct xnthread *thread,
 {
 	struct xnsched *sched = thread->sched;
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->tps = &sched->tp.partitions[p->tp.ptid];
 	thread->cprio = p->tp.prio;
 }
ksrc/nucleus/synch.c +7 -3
index b956e46..47bc0c5 100644
@@ -684,9 +684,13 @@ xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner)
 
 	XENO_BUGON(NUCLEUS, !testbits(synch->status, XNSYNCH_OWNER));
 
-	if (xnthread_test_state(lastowner, XNOTHER))
-		xnthread_dec_rescnt(lastowner);
-	XENO_BUGON(NUCLEUS, xnthread_get_rescnt(lastowner) < 0);
+	if (xnthread_test_state(lastowner, XNOTHER)) {
+		if (xnthread_get_rescnt(lastowner) == 0)
+			xnshadow_send_sig(lastowner, SIGDEBUG,
+					  SIGDEBUG_MIGRATE_PRIOINV, 1);
+		else
+			xnthread_dec_rescnt(lastowner);
+	}
 	lastownerh = xnthread_handle(lastowner);
 
Quoted 9 lines

ksrc/nucleus/synch.c xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner) · line 696

Quoted 4 lines
ksrc/nucleus/synch.c +0 -0
 	if (use_fastlock &&

Agreed, the logic of this patch sounds right. Switching from -rt to -nrt while holding a -rt mutex is pathological behavior. We don't have to support apps implementing voluntary priority inversion.

No concerns either.

Now we just need

ksrc/nucleus/shadow.c +4 -1
index 21cc191..27bc829 100644
@@ -2756,9 +2756,12 @@ static inline void do_setsched_event(struct task_struct *p, int priority)
 	union xnsched_policy_param param;
 	struct xnsched *sched;
 
-	if (!thread || p->policy != SCHED_FIFO)
+	if (!thread)
 		return;
 
+	if (p->policy == SCHED_FIFO)
+		priority = 0;
+
 	/*
 	 * Linux's priority scale is a subset of the core pod's
 	 * priority scale, so there is no need to bound the priority

to track the policy change when done via Linux.

Jan

#8 lore raw
Quoted 151 lines · Jan Kiszka
Quoted 131 lines · Philippe Gerum
Quoted 130 lines · Gilles Chanteperdrix
Quoted 22 lines · Gilles Chanteperdrix
Quoted 13 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 4 lines · Jan Kiszka

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Doing this while holding a mutex looks invalid.

Looking at POSIX e.g., is there anything in the spec that makes this invalid? If the kernel preserves or established proper priority boosting, I do not see what could break in principle.

Quoted 7 lines

It is nothing I would design into some app, but we should somehow handle it (doc update or code adjustments).

Quoted 1 line

If we do not do it, the current code is valid.

Except for its dependency on XNOTHER which is not updated on RT->NORMAL transitions.

Quoted 8 lines

The fact that this update did not take place made the code work. No negative rescnt could happen with that code.

Anyway, here is a patch to allow switching back from RT to NORMAL, but send a SIGDEBUG to a thread attempting to release a mutex while its counter is already 0. We end up avoiding a big chunk of code that would have been useful for a really strange corner case.

Here comes version 2:

include/nucleus/sched-idle.h +2 -0
index 6399a17..417170f 100644
@@ -39,6 +39,8 @@ extern struct xnsched_class xnsched_class_idle;
 static inline void __xnsched_idle_setparam(struct xnthread *thread,
 					   const union xnsched_policy_param *p)
 {
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->idle.prio;
 }
 
include/nucleus/sched-rt.h +6 -0
index 71f655c..cc1cefa 100644
@@ -86,6 +86,12 @@ static inline void __xnsched_rt_setparam(struct xnthread *thread,
 					 const union xnsched_policy_param *p)
 {
 	thread->cprio = p->rt.prio;
+	if (xnthread_test_state(thread, XNSHADOW)) {
+		if (thread->cprio)
+			xnthread_clear_state(thread, XNOTHER);
+		else
+			xnthread_set_state(thread, XNOTHER);
+	}
 }
 
 static inline void __xnsched_rt_getparam(struct xnthread *thread,
ksrc/nucleus/pod.c +0 -10
index 9a02e80..d19999f 100644
@@ -1896,16 +1896,6 @@ int __xnpod_set_thread_schedparam(struct xnthread *thread,
 		xnsched_putback(thread);
 
 #ifdef CONFIG_XENO_OPT_PERVASIVE
-	/*
-	 * A non-real-time shadow may upgrade to real-time FIFO
-	 * scheduling, but the latter may never downgrade to
-	 * SCHED_NORMAL Xenomai-wise. In the valid case, we clear
-	 * XNOTHER to reflect the change. Note that we keep handling
-	 * non real-time shadow specifics in higher code layers, not
-	 * to pollute the core scheduler with peculiarities.
-	 */
-	if (sched_class == &xnsched_class_rt && sched_param->rt.prio > 0)
-		xnthread_clear_state(thread, XNOTHER);
 	if (propagate) {
 		if (xnthread_test_state(thread, XNRELAX))
 			xnshadow_renice(thread);
ksrc/nucleus/sched-sporadic.c +2 -0
index fd37c21..ffc9bab 100644
@@ -258,6 +258,8 @@ static void xnsched_sporadic_setparam(struct xnthread *thread,
 		}
 	}
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->pss.current_prio;
 }
 
ksrc/nucleus/sched-tp.c +2 -0
index 43a548e..a2af1d3 100644
@@ -100,6 +100,8 @@ static void xnsched_tp_setparam(struct xnthread *thread,
 {
 	struct xnsched *sched = thread->sched;
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->tps = &sched->tp.partitions[p->tp.ptid];
 	thread->cprio = p->tp.prio;
 }
ksrc/nucleus/synch.c +7 -3
index b956e46..47bc0c5 100644
@@ -684,9 +684,13 @@ xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner)
 
 	XENO_BUGON(NUCLEUS, !testbits(synch->status, XNSYNCH_OWNER));
 
-	if (xnthread_test_state(lastowner, XNOTHER))
-		xnthread_dec_rescnt(lastowner);
-	XENO_BUGON(NUCLEUS, xnthread_get_rescnt(lastowner) < 0);
+	if (xnthread_test_state(lastowner, XNOTHER)) {
+		if (xnthread_get_rescnt(lastowner) == 0)
+			xnshadow_send_sig(lastowner, SIGDEBUG,
+					  SIGDEBUG_MIGRATE_PRIOINV, 1);
+		else
+			xnthread_dec_rescnt(lastowner);
+	}
 	lastownerh = xnthread_handle(lastowner);
 
Quoted 9 lines

ksrc/nucleus/synch.c xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner) · line 696

Quoted 4 lines
ksrc/nucleus/synch.c +0 -0
 	if (use_fastlock &&

Agreed, the logic of this patch sounds right. Switching from -rt to -nrt while holding a -rt mutex is pathological behavior. We don't have to support apps implementing voluntary priority inversion.

No concerns either.

Now we just need

ksrc/nucleus/shadow.c +0 -0
index 21cc191..27bc829 100644

ksrc/nucleus/shadow.c static inline void do_setsched_event(struct task_struct *p, int priority) · line 2762

Quoted 8 lines
ksrc/nucleus/shadow.c +2 -1
@@ -2756,9 +2756,12 @@ static inline void do_setsched_event(struct task_struct *p, int priority)
 	union xnsched_policy_param param;
 	struct xnsched *sched;
 
-	if (!thread || p->policy != SCHED_FIFO)
+	if (!thread)
 		return;
 
+	if (p->policy == SCHED_FIFO)
			^^
		inverted?
Quoted 11 lines
ksrc/nucleus/shadow.c +2 -0
+		priority = 0;
+
 	/*
 	 * Linux's priority scale is a subset of the core pod's
 	 * priority scale, so there is no need to bound the priority

to track the policy change when done via Linux.

Jan

Signature · 1 line
Philippe.
author
#9 lore raw
Quoted 155 lines · Philippe Gerum
Quoted 154 lines · Jan Kiszka
Quoted 131 lines · Philippe Gerum
Quoted 130 lines · Gilles Chanteperdrix
Quoted 22 lines · Gilles Chanteperdrix
Quoted 13 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 4 lines · Jan Kiszka

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Doing this while holding a mutex looks invalid.

Looking at POSIX e.g., is there anything in the spec that makes this invalid? If the kernel preserves or established proper priority boosting, I do not see what could break in principle.

Quoted 7 lines

It is nothing I would design into some app, but we should somehow handle it (doc update or code adjustments).

Quoted 1 line

If we do not do it, the current code is valid.

Except for its dependency on XNOTHER which is not updated on RT->NORMAL transitions.

Quoted 8 lines

The fact that this update did not take place made the code work. No negative rescnt could happen with that code.

Anyway, here is a patch to allow switching back from RT to NORMAL, but send a SIGDEBUG to a thread attempting to release a mutex while its counter is already 0. We end up avoiding a big chunk of code that would have been useful for a really strange corner case.

Here comes version 2:

include/nucleus/sched-idle.h +2 -0
index 6399a17..417170f 100644
@@ -39,6 +39,8 @@ extern struct xnsched_class xnsched_class_idle;
 static inline void __xnsched_idle_setparam(struct xnthread *thread,
 					   const union xnsched_policy_param *p)
 {
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->idle.prio;
 }
 
include/nucleus/sched-rt.h +6 -0
index 71f655c..cc1cefa 100644
@@ -86,6 +86,12 @@ static inline void __xnsched_rt_setparam(struct xnthread *thread,
 					 const union xnsched_policy_param *p)
 {
 	thread->cprio = p->rt.prio;
+	if (xnthread_test_state(thread, XNSHADOW)) {
+		if (thread->cprio)
+			xnthread_clear_state(thread, XNOTHER);
+		else
+			xnthread_set_state(thread, XNOTHER);
+	}
 }
 
 static inline void __xnsched_rt_getparam(struct xnthread *thread,
ksrc/nucleus/pod.c +0 -10
index 9a02e80..d19999f 100644
@@ -1896,16 +1896,6 @@ int __xnpod_set_thread_schedparam(struct xnthread *thread,
 		xnsched_putback(thread);
 
 #ifdef CONFIG_XENO_OPT_PERVASIVE
-	/*
-	 * A non-real-time shadow may upgrade to real-time FIFO
-	 * scheduling, but the latter may never downgrade to
-	 * SCHED_NORMAL Xenomai-wise. In the valid case, we clear
-	 * XNOTHER to reflect the change. Note that we keep handling
-	 * non real-time shadow specifics in higher code layers, not
-	 * to pollute the core scheduler with peculiarities.
-	 */
-	if (sched_class == &xnsched_class_rt && sched_param->rt.prio > 0)
-		xnthread_clear_state(thread, XNOTHER);
 	if (propagate) {
 		if (xnthread_test_state(thread, XNRELAX))
 			xnshadow_renice(thread);
ksrc/nucleus/sched-sporadic.c +2 -0
index fd37c21..ffc9bab 100644
@@ -258,6 +258,8 @@ static void xnsched_sporadic_setparam(struct xnthread *thread,
 		}
 	}
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->pss.current_prio;
 }
 
ksrc/nucleus/sched-tp.c +2 -0
index 43a548e..a2af1d3 100644
@@ -100,6 +100,8 @@ static void xnsched_tp_setparam(struct xnthread *thread,
 {
 	struct xnsched *sched = thread->sched;
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->tps = &sched->tp.partitions[p->tp.ptid];
 	thread->cprio = p->tp.prio;
 }
ksrc/nucleus/synch.c +7 -3
index b956e46..47bc0c5 100644
@@ -684,9 +684,13 @@ xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner)
 
 	XENO_BUGON(NUCLEUS, !testbits(synch->status, XNSYNCH_OWNER));
 
-	if (xnthread_test_state(lastowner, XNOTHER))
-		xnthread_dec_rescnt(lastowner);
-	XENO_BUGON(NUCLEUS, xnthread_get_rescnt(lastowner) < 0);
+	if (xnthread_test_state(lastowner, XNOTHER)) {
+		if (xnthread_get_rescnt(lastowner) == 0)
+			xnshadow_send_sig(lastowner, SIGDEBUG,
+					  SIGDEBUG_MIGRATE_PRIOINV, 1);
+		else
+			xnthread_dec_rescnt(lastowner);
+	}
 	lastownerh = xnthread_handle(lastowner);
 
Quoted 9 lines

ksrc/nucleus/synch.c xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner) · line 696

Quoted 4 lines
ksrc/nucleus/synch.c +0 -0
 	if (use_fastlock &&

Agreed, the logic of this patch sounds right. Switching from -rt to -nrt while holding a -rt mutex is pathological behavior. We don't have to support apps implementing voluntary priority inversion.

No concerns either.

Now we just need

ksrc/nucleus/shadow.c +0 -0
index 21cc191..27bc829 100644
@@ -2756,9 +2756,12 @@ static inline void do_setsched_event(struct task_struct *p, int priority)
 	union xnsched_policy_param param;
 	struct xnsched *sched;
 
Quoted 7 lines

ksrc/nucleus/shadow.c static inline void do_setsched_event(struct task_struct *p, int priority) · line 2762

Quoted 5 lines
ksrc/nucleus/shadow.c +2 -1
-	if (!thread || p->policy != SCHED_FIFO)
+	if (!thread)
 		return;
 
+	if (p->policy == SCHED_FIFO)
			^^
		inverted?

Of course.

Jan

author
#10 lore raw
Quoted 160 lines · Jan Kiszka
Quoted 159 lines · Philippe Gerum
Quoted 158 lines · Jan Kiszka
Quoted 131 lines · Philippe Gerum
Quoted 130 lines · Gilles Chanteperdrix
Quoted 22 lines · Gilles Chanteperdrix
Quoted 13 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 4 lines · Jan Kiszka

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Doing this while holding a mutex looks invalid.

Looking at POSIX e.g., is there anything in the spec that makes this invalid? If the kernel preserves or established proper priority boosting, I do not see what could break in principle.

Quoted 7 lines

It is nothing I would design into some app, but we should somehow handle it (doc update or code adjustments).

Quoted 1 line

If we do not do it, the current code is valid.

Except for its dependency on XNOTHER which is not updated on RT->NORMAL transitions.

Quoted 8 lines

The fact that this update did not take place made the code work. No negative rescnt could happen with that code.

Anyway, here is a patch to allow switching back from RT to NORMAL, but send a SIGDEBUG to a thread attempting to release a mutex while its counter is already 0. We end up avoiding a big chunk of code that would have been useful for a really strange corner case.

Here comes version 2:

include/nucleus/sched-idle.h +2 -0
index 6399a17..417170f 100644
@@ -39,6 +39,8 @@ extern struct xnsched_class xnsched_class_idle;
 static inline void __xnsched_idle_setparam(struct xnthread *thread,
 					   const union xnsched_policy_param *p)
 {
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->idle.prio;
 }
 
include/nucleus/sched-rt.h +6 -0
index 71f655c..cc1cefa 100644
@@ -86,6 +86,12 @@ static inline void __xnsched_rt_setparam(struct xnthread *thread,
 					 const union xnsched_policy_param *p)
 {
 	thread->cprio = p->rt.prio;
+	if (xnthread_test_state(thread, XNSHADOW)) {
+		if (thread->cprio)
+			xnthread_clear_state(thread, XNOTHER);
+		else
+			xnthread_set_state(thread, XNOTHER);
+	}
 }
 
 static inline void __xnsched_rt_getparam(struct xnthread *thread,
ksrc/nucleus/pod.c +0 -10
index 9a02e80..d19999f 100644
@@ -1896,16 +1896,6 @@ int __xnpod_set_thread_schedparam(struct xnthread *thread,
 		xnsched_putback(thread);
 
 #ifdef CONFIG_XENO_OPT_PERVASIVE
-	/*
-	 * A non-real-time shadow may upgrade to real-time FIFO
-	 * scheduling, but the latter may never downgrade to
-	 * SCHED_NORMAL Xenomai-wise. In the valid case, we clear
-	 * XNOTHER to reflect the change. Note that we keep handling
-	 * non real-time shadow specifics in higher code layers, not
-	 * to pollute the core scheduler with peculiarities.
-	 */
-	if (sched_class == &xnsched_class_rt && sched_param->rt.prio > 0)
-		xnthread_clear_state(thread, XNOTHER);
 	if (propagate) {
 		if (xnthread_test_state(thread, XNRELAX))
 			xnshadow_renice(thread);
ksrc/nucleus/sched-sporadic.c +2 -0
index fd37c21..ffc9bab 100644
@@ -258,6 +258,8 @@ static void xnsched_sporadic_setparam(struct xnthread *thread,
 		}
 	}
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->pss.current_prio;
 }
 
ksrc/nucleus/sched-tp.c +2 -0
index 43a548e..a2af1d3 100644
@@ -100,6 +100,8 @@ static void xnsched_tp_setparam(struct xnthread *thread,
 {
 	struct xnsched *sched = thread->sched;
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->tps = &sched->tp.partitions[p->tp.ptid];
 	thread->cprio = p->tp.prio;
 }
ksrc/nucleus/synch.c +7 -3
index b956e46..47bc0c5 100644
@@ -684,9 +684,13 @@ xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner)
 
 	XENO_BUGON(NUCLEUS, !testbits(synch->status, XNSYNCH_OWNER));
 
-	if (xnthread_test_state(lastowner, XNOTHER))
-		xnthread_dec_rescnt(lastowner);
-	XENO_BUGON(NUCLEUS, xnthread_get_rescnt(lastowner) < 0);
+	if (xnthread_test_state(lastowner, XNOTHER)) {
+		if (xnthread_get_rescnt(lastowner) == 0)
+			xnshadow_send_sig(lastowner, SIGDEBUG,
+					  SIGDEBUG_MIGRATE_PRIOINV, 1);
+		else
+			xnthread_dec_rescnt(lastowner);
+	}
 	lastownerh = xnthread_handle(lastowner);
 
Quoted 9 lines

ksrc/nucleus/synch.c xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner) · line 696

Quoted 4 lines
ksrc/nucleus/synch.c +0 -0
 	if (use_fastlock &&

Agreed, the logic of this patch sounds right. Switching from -rt to -nrt while holding a -rt mutex is pathological behavior. We don't have to support apps implementing voluntary priority inversion.

No concerns either.

Now we just need

ksrc/nucleus/shadow.c +1 -1
index 21cc191..27bc829 100644
@@ -2756,9 +2756,12 @@ static inline void do_setsched_event(struct task_struct *p, int priority)
 	union xnsched_policy_param param;
 	struct xnsched *sched;
 
-	if (!thread || p->policy != SCHED_FIFO)
+	if (!thread)
 		return;
 
Quoted 5 lines
Quoted 3 lines

ksrc/nucleus/shadow.c static inline void do_setsched_event(struct task_struct *p, int priority) · line 2762

Quoted 1 line
ksrc/nucleus/shadow.c +1 -0
+	if (p->policy == SCHED_FIFO)
			^^
		inverted?

Of course.

Thinking about SCHED_RR or other policies, this still looks fishy and needs some thoughts.

Jan

#11 lore raw
Quoted 161 lines · Jan Kiszka
Quoted 160 lines · Jan Kiszka
Quoted 159 lines · Philippe Gerum
Quoted 158 lines · Jan Kiszka
Quoted 131 lines · Philippe Gerum
Quoted 130 lines · Gilles Chanteperdrix
Quoted 22 lines · Gilles Chanteperdrix
Quoted 13 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 4 lines · Jan Kiszka

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Doing this while holding a mutex looks invalid.

Looking at POSIX e.g., is there anything in the spec that makes this invalid? If the kernel preserves or established proper priority boosting, I do not see what could break in principle.

Quoted 7 lines

It is nothing I would design into some app, but we should somehow handle it (doc update or code adjustments).

Quoted 1 line

If we do not do it, the current code is valid.

Except for its dependency on XNOTHER which is not updated on RT->NORMAL transitions.

Quoted 8 lines

The fact that this update did not take place made the code work. No negative rescnt could happen with that code.

Anyway, here is a patch to allow switching back from RT to NORMAL, but send a SIGDEBUG to a thread attempting to release a mutex while its counter is already 0. We end up avoiding a big chunk of code that would have been useful for a really strange corner case.

Here comes version 2:

include/nucleus/sched-idle.h +2 -0
index 6399a17..417170f 100644
@@ -39,6 +39,8 @@ extern struct xnsched_class xnsched_class_idle;
 static inline void __xnsched_idle_setparam(struct xnthread *thread,
 					   const union xnsched_policy_param *p)
 {
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->idle.prio;
 }
 
include/nucleus/sched-rt.h +6 -0
index 71f655c..cc1cefa 100644
@@ -86,6 +86,12 @@ static inline void __xnsched_rt_setparam(struct xnthread *thread,
 					 const union xnsched_policy_param *p)
 {
 	thread->cprio = p->rt.prio;
+	if (xnthread_test_state(thread, XNSHADOW)) {
+		if (thread->cprio)
+			xnthread_clear_state(thread, XNOTHER);
+		else
+			xnthread_set_state(thread, XNOTHER);
+	}
 }
 
 static inline void __xnsched_rt_getparam(struct xnthread *thread,
ksrc/nucleus/pod.c +0 -10
index 9a02e80..d19999f 100644
@@ -1896,16 +1896,6 @@ int __xnpod_set_thread_schedparam(struct xnthread *thread,
 		xnsched_putback(thread);
 
 #ifdef CONFIG_XENO_OPT_PERVASIVE
-	/*
-	 * A non-real-time shadow may upgrade to real-time FIFO
-	 * scheduling, but the latter may never downgrade to
-	 * SCHED_NORMAL Xenomai-wise. In the valid case, we clear
-	 * XNOTHER to reflect the change. Note that we keep handling
-	 * non real-time shadow specifics in higher code layers, not
-	 * to pollute the core scheduler with peculiarities.
-	 */
-	if (sched_class == &xnsched_class_rt && sched_param->rt.prio > 0)
-		xnthread_clear_state(thread, XNOTHER);
 	if (propagate) {
 		if (xnthread_test_state(thread, XNRELAX))
 			xnshadow_renice(thread);
ksrc/nucleus/sched-sporadic.c +2 -0
index fd37c21..ffc9bab 100644
@@ -258,6 +258,8 @@ static void xnsched_sporadic_setparam(struct xnthread *thread,
 		}
 	}
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->pss.current_prio;
 }
 
ksrc/nucleus/sched-tp.c +2 -0
index 43a548e..a2af1d3 100644
@@ -100,6 +100,8 @@ static void xnsched_tp_setparam(struct xnthread *thread,
 {
 	struct xnsched *sched = thread->sched;
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->tps = &sched->tp.partitions[p->tp.ptid];
 	thread->cprio = p->tp.prio;
 }
ksrc/nucleus/synch.c +7 -3
index b956e46..47bc0c5 100644
@@ -684,9 +684,13 @@ xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner)
 
 	XENO_BUGON(NUCLEUS, !testbits(synch->status, XNSYNCH_OWNER));
 
-	if (xnthread_test_state(lastowner, XNOTHER))
-		xnthread_dec_rescnt(lastowner);
-	XENO_BUGON(NUCLEUS, xnthread_get_rescnt(lastowner) < 0);
+	if (xnthread_test_state(lastowner, XNOTHER)) {
+		if (xnthread_get_rescnt(lastowner) == 0)
+			xnshadow_send_sig(lastowner, SIGDEBUG,
+					  SIGDEBUG_MIGRATE_PRIOINV, 1);
+		else
+			xnthread_dec_rescnt(lastowner);
+	}
 	lastownerh = xnthread_handle(lastowner);
 
Quoted 9 lines

ksrc/nucleus/synch.c xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner) · line 696

Quoted 4 lines
ksrc/nucleus/synch.c +0 -0
 	if (use_fastlock &&

Agreed, the logic of this patch sounds right. Switching from -rt to -nrt while holding a -rt mutex is pathological behavior. We don't have to support apps implementing voluntary priority inversion.

No concerns either.

Now we just need

ksrc/nucleus/shadow.c +1 -1
index 21cc191..27bc829 100644
@@ -2756,9 +2756,12 @@ static inline void do_setsched_event(struct task_struct *p, int priority)
 	union xnsched_policy_param param;
 	struct xnsched *sched;
 
-	if (!thread || p->policy != SCHED_FIFO)
+	if (!thread)
 		return;
 
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

ksrc/nucleus/shadow.c static inline void do_setsched_event(struct task_struct *p, int priority) · line 2762

Quoted 1 line
ksrc/nucleus/shadow.c +1 -0
+	if (p->policy == SCHED_FIFO)
			^^
		inverted?

Of course.

Thinking about SCHED_RR or other policies, this still looks fishy and needs some thoughts.

What about:

ksrc/nucleus/shadow.c +4 -1
index 21cc191..7fe44a1 100644
@@ -2756,9 +2756,12 @@ static inline void do_setsched_event(struct task_struct *p, int priority)
 	union xnsched_policy_param param;
 	struct xnsched *sched;
 
-	if (!thread || p->policy != SCHED_FIFO)
+	if (!thread || (p->policy != SCHED_FIFO && p->policy != SCHED_OTHER))
 		return;
 
+	if (p->policy == SCHED_OTHER)
+		priority = 0;
+
 	/*
 	 * Linux's priority scale is a subset of the core pod's
 	 * priority scale, so there is no need to bound the priority
Signature · 1 line
                                                                Gilles.
author
#12 lore raw
Quoted 181 lines · Gilles Chanteperdrix
Quoted 161 lines · Jan Kiszka
Quoted 160 lines · Jan Kiszka
Quoted 159 lines · Philippe Gerum
Quoted 158 lines · Jan Kiszka
Quoted 131 lines · Philippe Gerum
Quoted 130 lines · Gilles Chanteperdrix
Quoted 22 lines · Gilles Chanteperdrix
Quoted 13 lines · Jan Kiszka
Quoted 7 lines · Gilles Chanteperdrix
Quoted 4 lines · Jan Kiszka

Hi all,

just looked into the hrescnt issue again, specifically the corner case of a shadow thread switching from real-time policy to SCHED_OTHER.

Doing this while holding a mutex looks invalid.

Looking at POSIX e.g., is there anything in the spec that makes this invalid? If the kernel preserves or established proper priority boosting, I do not see what could break in principle.

Quoted 7 lines

It is nothing I would design into some app, but we should somehow handle it (doc update or code adjustments).

Quoted 1 line

If we do not do it, the current code is valid.

Except for its dependency on XNOTHER which is not updated on RT->NORMAL transitions.

Quoted 8 lines

The fact that this update did not take place made the code work. No negative rescnt could happen with that code.

Anyway, here is a patch to allow switching back from RT to NORMAL, but send a SIGDEBUG to a thread attempting to release a mutex while its counter is already 0. We end up avoiding a big chunk of code that would have been useful for a really strange corner case.

Here comes version 2:

include/nucleus/sched-idle.h +2 -0
index 6399a17..417170f 100644
@@ -39,6 +39,8 @@ extern struct xnsched_class xnsched_class_idle;
 static inline void __xnsched_idle_setparam(struct xnthread *thread,
 					   const union xnsched_policy_param *p)
 {
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->idle.prio;
 }
 
include/nucleus/sched-rt.h +6 -0
index 71f655c..cc1cefa 100644
@@ -86,6 +86,12 @@ static inline void __xnsched_rt_setparam(struct xnthread *thread,
 					 const union xnsched_policy_param *p)
 {
 	thread->cprio = p->rt.prio;
+	if (xnthread_test_state(thread, XNSHADOW)) {
+		if (thread->cprio)
+			xnthread_clear_state(thread, XNOTHER);
+		else
+			xnthread_set_state(thread, XNOTHER);
+	}
 }
 
 static inline void __xnsched_rt_getparam(struct xnthread *thread,
ksrc/nucleus/pod.c +0 -10
index 9a02e80..d19999f 100644
@@ -1896,16 +1896,6 @@ int __xnpod_set_thread_schedparam(struct xnthread *thread,
 		xnsched_putback(thread);
 
 #ifdef CONFIG_XENO_OPT_PERVASIVE
-	/*
-	 * A non-real-time shadow may upgrade to real-time FIFO
-	 * scheduling, but the latter may never downgrade to
-	 * SCHED_NORMAL Xenomai-wise. In the valid case, we clear
-	 * XNOTHER to reflect the change. Note that we keep handling
-	 * non real-time shadow specifics in higher code layers, not
-	 * to pollute the core scheduler with peculiarities.
-	 */
-	if (sched_class == &xnsched_class_rt && sched_param->rt.prio > 0)
-		xnthread_clear_state(thread, XNOTHER);
 	if (propagate) {
 		if (xnthread_test_state(thread, XNRELAX))
 			xnshadow_renice(thread);
ksrc/nucleus/sched-sporadic.c +2 -0
index fd37c21..ffc9bab 100644
@@ -258,6 +258,8 @@ static void xnsched_sporadic_setparam(struct xnthread *thread,
 		}
 	}
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->cprio = p->pss.current_prio;
 }
 
ksrc/nucleus/sched-tp.c +2 -0
index 43a548e..a2af1d3 100644
@@ -100,6 +100,8 @@ static void xnsched_tp_setparam(struct xnthread *thread,
 {
 	struct xnsched *sched = thread->sched;
 
+	if (xnthread_test_state(thread, XNSHADOW))
+		xnthread_clear_state(thread, XNOTHER);
 	thread->tps = &sched->tp.partitions[p->tp.ptid];
 	thread->cprio = p->tp.prio;
 }
ksrc/nucleus/synch.c +7 -3
index b956e46..47bc0c5 100644
@@ -684,9 +684,13 @@ xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner)
 
 	XENO_BUGON(NUCLEUS, !testbits(synch->status, XNSYNCH_OWNER));
 
-	if (xnthread_test_state(lastowner, XNOTHER))
-		xnthread_dec_rescnt(lastowner);
-	XENO_BUGON(NUCLEUS, xnthread_get_rescnt(lastowner) < 0);
+	if (xnthread_test_state(lastowner, XNOTHER)) {
+		if (xnthread_get_rescnt(lastowner) == 0)
+			xnshadow_send_sig(lastowner, SIGDEBUG,
+					  SIGDEBUG_MIGRATE_PRIOINV, 1);
+		else
+			xnthread_dec_rescnt(lastowner);
+	}
 	lastownerh = xnthread_handle(lastowner);
 
Quoted 9 lines

ksrc/nucleus/synch.c xnsynch_release_thread(struct xnsynch *synch, struct xnthread *lastowner) · line 696

Quoted 4 lines
ksrc/nucleus/synch.c +0 -0
 	if (use_fastlock &&

Agreed, the logic of this patch sounds right. Switching from -rt to -nrt while holding a -rt mutex is pathological behavior. We don't have to support apps implementing voluntary priority inversion.

No concerns either.

Now we just need

ksrc/nucleus/shadow.c +1 -1
index 21cc191..27bc829 100644
@@ -2756,9 +2756,12 @@ static inline void do_setsched_event(struct task_struct *p, int priority)
 	union xnsched_policy_param param;
 	struct xnsched *sched;
 
-	if (!thread || p->policy != SCHED_FIFO)
+	if (!thread)
 		return;
 
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

ksrc/nucleus/shadow.c static inline void do_setsched_event(struct task_struct *p, int priority) · line 2762

Quoted 1 line
ksrc/nucleus/shadow.c +1 -0
+	if (p->policy == SCHED_FIFO)
			^^
		inverted?

Of course.

Thinking about SCHED_RR or other policies, this still looks fishy and needs some thoughts.

What about:

ksrc/nucleus/shadow.c +0 -0
index 21cc191..7fe44a1 100644
@@ -2756,9 +2756,12 @@ static inline void do_setsched_event(struct task_struct *p, int priority)
 	union xnsched_policy_param param;
 	struct xnsched *sched;
 

ksrc/nucleus/shadow.c static inline void do_setsched_event(struct task_struct *p, int priority) · line 2767

Quoted 10 lines
ksrc/nucleus/shadow.c +4 -1
-	if (!thread || p->policy != SCHED_FIFO)
+	if (!thread || (p->policy != SCHED_FIFO && p->policy != SCHED_OTHER))
 		return;
 
+	if (p->policy == SCHED_OTHER)
+		priority = 0;
+
 	/*
 	 * Linux's priority scale is a subset of the core pod's
 	 * priority scale, so there is no need to bound the priority

No other policies can be issued against the Linux part of a shadow? Then it's OK. I just don't recall the details here ATM.

Jan

#13 lore raw
Quoted 25 lines · Jan Kiszka
Quoted 10 lines · Gilles Chanteperdrix

What about:

ksrc/nucleus/shadow.c +0 -0
index 21cc191..7fe44a1 100644
@@ -2756,9 +2756,12 @@ static inline void do_setsched_event(struct task_struct *p, int priority)
 	union xnsched_policy_param param;
 	struct xnsched *sched;
 

ksrc/nucleus/shadow.c static inline void do_setsched_event(struct task_struct *p, int priority) · line 2767

Quoted 10 lines
ksrc/nucleus/shadow.c +4 -1
-	if (!thread || p->policy != SCHED_FIFO)
+	if (!thread || (p->policy != SCHED_FIFO && p->policy != SCHED_OTHER))
 		return;
 
+	if (p->policy == SCHED_OTHER)
+		priority = 0;
+
 	/*
 	 * Linux's priority scale is a subset of the core pod's
 	 * priority scale, so there is no need to bound the priority

No other policies can be issued against the Linux part of a shadow? Then it's OK. I just don't recall the details here ATM.

Quoted 2 lines

Jan

This should be OK, according to the comment:
* BIG FAT WARNING: Change of scheduling parameters from the
* Linux side are propagated only to threads that belong to
* the Xenomai RT scheduling class. Threads from other classes
* are remain unaffected, since we could not map this
* information 1:1 between Linux and Xenomai.
*/

Signature · 1 line
                                                                Gilles.