Skip to contentSkip to conversation

Rework pthread_setschedprio support

PATCH Superseded Jan Kiszka 1 participant 7 messages Dec 2, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

cover Rework pthread_setschedprio support

It turned out just in time that the __CURRENT_SCHED-approach is broken and not reasonably fixable. This introduces a separate syscall for implementing that posix function.

Jan

Jan Kiszka (5):
cobalt/posix/thread: Factor out thread_lookup_or_shadow cobalt/posix: Add thread_setschedprio
lib/cobalt: Use new thread_setschedprio syscall Revert "cobalt/kernel: Introduce __SCHED_CURRENT policy to
setschedparam_ex"
testsuite/smokey: Add pthread_setschedprio to setsched test case

 include/cobalt/uapi/sched.h                        |  3 -
 include/cobalt/uapi/syscall.h                      |  1 +
 .../arch/x86/include/asm/xenomai/syscall32-table.h |  1 +
 kernel/cobalt/posix/syscall32.c                    |  9 +++
 kernel/cobalt/posix/syscall32.h                    |  6 ++
 kernel/cobalt/posix/thread.c                       | 85 ++++++++++++++++------
 kernel/cobalt/posix/thread.h                       | 11 +++
 kernel/cobalt/trace/cobalt-posix.h                 | 18 ++++-
 lib/cobalt/thread.c                                | 33 ++++++++-
 testsuite/smokey/setsched/setsched.c               | 19 +++++
 10 files changed, 155 insertions(+), 31 deletions(-)
Signature · 1 line
2.16.4
author
#1 lore raw

PATCH 1/5 cobalt/posix/thread: Factor out thread_lookup_or_shadow

From: Jan Kiszka <[email protected]>

This pattern will be reused by cobalt_thread_setschedprio.

---
 kernel/cobalt/posix/thread.c | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)
kernel/cobalt/posix/thread.c +26 -12
index 30c31bb6d9..9f1c802b63 100644
@@ -463,32 +463,46 @@ static inline int pthread_setmode_np(int clrmask, int setmask, int *mode_r)
 	return 0;
 }
 
-int cobalt_thread_setschedparam_ex(unsigned long pth,
-				   int policy,
-				   const struct sched_param_ex *param_ex,
-				   __u32 __user *u_winoff,
-				   int __user *u_promoted)
+static struct cobalt_thread *thread_lookup_or_shadow(unsigned long pth,
+						     __u32 __user *u_winoff,
+						     int *promoted_r)
 {
 	struct cobalt_local_hkey hkey;
 	struct cobalt_thread *thread;
-	int ret, promoted = 0;
+
+	*promoted_r = 0;
 
 	hkey.u_pth = pth;
 	hkey.mm = current->mm;
-	trace_cobalt_pthread_setschedparam(pth, policy, param_ex);
 
 	thread = thread_lookup(&hkey);
 	if (thread == NULL) {
 		if (u_winoff == NULL)
-			return -ESRCH;
+			return ERR_PTR(-ESRCH);
 			
 		thread = cobalt_thread_shadow(&hkey, u_winoff);
-		if (IS_ERR(thread))
-			return PTR_ERR(thread);
-
-		promoted = 1;
+		if (!IS_ERR(thread))
+			*promoted_r = 1;
 	}
 
+	return thread;
+}
+
+int cobalt_thread_setschedparam_ex(unsigned long pth,
+				   int policy,
+				   const struct sched_param_ex *param_ex,
+				   __u32 __user *u_winoff,
+				   int __user *u_promoted)
+{
+	struct cobalt_thread *thread;
+	int ret, promoted;
+
+	trace_cobalt_pthread_setschedparam(pth, policy, param_ex);
+
+	thread = thread_lookup_or_shadow(pth, u_winoff, &promoted);
+	if (IS_ERR(thread))
+		return PTR_ERR(thread);
+
 	ret = __cobalt_thread_setschedparam_ex(thread, policy, param_ex);
 	if (ret)
 		return ret;
Signature · 1 line
2.16.4
author
#2 lore raw

PATCH 2/5 cobalt/posix: Add thread_setschedprio

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

The approach of using thread_setschedparam_ex with __SCHED_CURRENT failed for policies with complex parameter sets. Therefore, introduce a syscall that is fully aware of how to apply only the priority change to the target thread's scheduling settings.

We need a compat path as well because we are passing down pointers.

---
 include/cobalt/uapi/syscall.h                      |  1 +
 .../arch/x86/include/asm/xenomai/syscall32-table.h |  1 +
 kernel/cobalt/posix/syscall32.c                    |  9 ++++++
 kernel/cobalt/posix/syscall32.h                    |  6 ++++
 kernel/cobalt/posix/thread.c                       | 37 ++++++++++++++++++++++
 kernel/cobalt/posix/thread.h                       | 11 +++++++
 kernel/cobalt/trace/cobalt-posix.h                 | 15 +++++++++
 7 files changed, 80 insertions(+)
include/cobalt/uapi/syscall.h +1 -0
index 1cb22a5936..aa3c308d0b 100644
@@ -121,6 +121,7 @@
 #define sc_cobalt_recvmmsg			98
 #define sc_cobalt_sendmmsg			99
 #define sc_cobalt_clock_adjtime			100
+#define sc_cobalt_thread_setschedprio		101
 
 #define __NR_COBALT_SYSCALLS			128 /* Power of 2 */
 
kernel/cobalt/arch/x86/include/asm/xenomai/syscall32-table.h +1 -0
index 7484c3a955..4784377b06 100644
@@ -27,6 +27,7 @@
 __COBALT_CALL32emu_THUNK(thread_create)
 __COBALT_CALL32emu_THUNK(thread_setschedparam_ex)
 __COBALT_CALL32emu_THUNK(thread_getschedparam_ex)
+__COBALT_CALL32emu_THUNK(thread_setschedprio)
 __COBALT_CALL32emu_THUNK(sem_open)
 __COBALT_CALL32x_THUNK(sem_open)
 __COBALT_CALL32emu_THUNK(sem_timedwait)
kernel/cobalt/posix/syscall32.c +9 -0
index a2482b7a00..faa7ef53a6 100644
@@ -88,6 +88,15 @@ COBALT_SYSCALL32emu(thread_getschedparam_ex, current,
 	return ret ?: sys32_put_param_ex(policy, u_param, &param_ex);
 }
 
+COBALT_SYSCALL32emu(thread_setschedprio, conforming,
+		    (compat_ulong_t pth,
+		     int prio,
+		     __u32 __user *u_winoff,
+		     int __user *u_promoted))
+{
+	return cobalt_thread_setschedprio(pth, prio, u_winoff, u_promoted);
+}
+
 static inline int sys32_fetch_timeout(struct timespec *ts,
 				      const void __user *u_ts)
 {
kernel/cobalt/posix/syscall32.h +6 -0
index 5ba6513f38..34904f96bd 100644
@@ -45,6 +45,12 @@ COBALT_SYSCALL32emu_DECL(thread_getschedparam_ex,
 			  int __user *u_policy,
 			  struct compat_sched_param_ex __user *u_param));
 
+COBALT_SYSCALL32emu_DECL(thread_setschedprio,
+			 (compat_ulong_t pth,
+			  int prio,
+			  __u32 __user *u_winoff,
+			  int __user *u_promoted));
+
 COBALT_SYSCALL32emu_DECL(clock_getres,
 			 (clockid_t clock_id,
 			  struct compat_timespec __user *u_ts));
kernel/cobalt/posix/thread.c +37 -0
index 9f1c802b63..c2a9fb46c5 100644
@@ -568,6 +568,43 @@ COBALT_SYSCALL(thread_getschedparam_ex, current,
 	return cobalt_copy_to_user(u_param, &param_ex, sizeof(param_ex));
 }
 
+int cobalt_thread_setschedprio(unsigned long pth,
+			       int prio,
+			       __u32 __user *u_winoff,
+			       int __user *u_promoted)
+{
+	struct sched_param_ex param_ex;
+	struct cobalt_thread *thread;
+	int ret, policy, promoted;
+
+	trace_cobalt_pthread_setschedprio(pth, prio);
+
+	thread = thread_lookup_or_shadow(pth, u_winoff, &promoted);
+	if (IS_ERR(thread))
+		return PTR_ERR(thread);
+
+	ret = __cobalt_thread_getschedparam_ex(thread, &policy, &param_ex);
+	if (ret)
+		return ret;
+
+	param_ex.sched_priority = prio;
+
+	ret = __cobalt_thread_setschedparam_ex(thread, policy, &param_ex);
+	if (ret)
+		return ret;
+
+	return cobalt_copy_to_user(u_promoted, &promoted, sizeof(promoted));
+}
+
+COBALT_SYSCALL(thread_setschedprio, conforming,
+	       (unsigned long pth,
+		int prio,
+		__u32 __user *u_winoff,
+		int __user *u_promoted))
+{
+	return cobalt_thread_setschedprio(pth, prio, u_winoff, u_promoted);
+}
+
 int __cobalt_thread_create(unsigned long pth, int policy,
 			   struct sched_param_ex *param_ex,
 			   int xid, __u32 __user *u_winoff)
kernel/cobalt/posix/thread.h +11 -0
index fec9d17859..51be372a7a 100644
@@ -139,6 +139,11 @@ int __cobalt_thread_getschedparam_ex(struct cobalt_thread *thread,
 				     int *policy_r,
 				     struct sched_param_ex *param_ex);
 
+int cobalt_thread_setschedprio(unsigned long pth,
+			       int prio,
+			       __u32 __user *u_winoff,
+			       int __user *u_promoted);
+
 struct cobalt_thread *cobalt_thread_find(pid_t pid);
 
 struct cobalt_thread *cobalt_thread_find_local(pid_t pid);
@@ -181,6 +186,12 @@ COBALT_SYSCALL_DECL(thread_getschedparam_ex,
 		     int __user *u_policy,
 		     struct sched_param_ex __user *u_param));
 
+COBALT_SYSCALL_DECL(thread_setschedprio,
+		    (unsigned long pth,
+		     int prio,
+		     __u32 __user *u_winoff,
+		     int __user *u_promoted));
+
 void cobalt_thread_map(struct xnthread *curr);
 
 struct xnthread_personality *cobalt_thread_exit(struct xnthread *curr);
kernel/cobalt/trace/cobalt-posix.h +15 -0
index bda7bdd3c8..83ec748586 100644
@@ -70,6 +70,7 @@
 		__cobalt_symbolic_syscall(thread_kill),			\
 		__cobalt_symbolic_syscall(thread_setschedparam_ex),	\
 		__cobalt_symbolic_syscall(thread_getschedparam_ex),	\
+		__cobalt_symbolic_syscall(thread_setschedprio),		\
 		__cobalt_symbolic_syscall(thread_getstat),		\
 		__cobalt_symbolic_syscall(sem_init),			\
 		__cobalt_symbolic_syscall(sem_destroy),			\
@@ -311,6 +312,20 @@ DEFINE_EVENT(cobalt_posix_schedparam, cobalt_pthread_getschedparam,
 	TP_ARGS(pth, policy, param_ex)
 );
 
+TRACE_EVENT(cobalt_pthread_setschedprio,
+	TP_PROTO(unsigned long pth, int prio),
+	TP_ARGS(pth, prio),
+	TP_STRUCT__entry(
+		__field(unsigned long, pth)
+		__field(int, prio)
+	),
+	TP_fast_assign(
+		__entry->pth = pth;
+		__entry->prio = prio;
+	),
+	TP_printk("pth=%p prio=%d", (void *)__entry->pth, __entry->prio)
+);
+
 #define cobalt_print_thread_mode(__mode)			\
 	__print_flags(__mode, "|",				\
 		      {PTHREAD_WARNSW, "warnsw"},		\
Signature · 1 line
2.16.4
author
#3 lore raw

PATCH 3/5 lib/cobalt: Use new thread_setschedprio syscall

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

Switch the implementation of pthread_setschedprio to the new cobalt syscall in order to avoid scheduler setting corruptions for SCHED_QUOTA and similar complex policies.

---
 lib/cobalt/thread.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
lib/cobalt/thread.c +29 -4
index 86db6343c9..bb23cdcb97 100644
@@ -666,11 +666,36 @@ int pthread_setschedparam_ex(pthread_t thread,
 
 COBALT_IMPL(int, pthread_setschedprio, (pthread_t thread, int prio))
 {
-	struct sched_param_ex param_ex = {
-		.sched_priority = prio,
-	};
+	__u32 u_winoff, *u_winoff_ptr;
+	int ret, promoted;
 
-	return pthread_setschedparam_ex(thread, __SCHED_CURRENT, &param_ex);
+	/* See pthread_setschedparam_ex. */
+	if (cobalt_eager_setsched()) {
+		ret = __STD(pthread_setschedprio(thread, prio));
+		if (ret)
+			return ret;
+	}
+
+	/* only request promotion when this targets the current thread */
+	u_winoff_ptr = thread == pthread_self() ? &u_winoff : NULL;
+
+	ret = -XENOMAI_SYSCALL4(sc_cobalt_thread_setschedprio,
+				thread, prio, u_winoff_ptr, &promoted);
+
+	/*
+	 * If the kernel has no reference to the target thread. let glibc
+	 * handle the call.
+	 */
+	if (ret == ESRCH)
+		return __STD(pthread_setschedprio(thread, prio));
+
+	if (ret == 0 && promoted) {
+		cobalt_sigshadow_install_once();
+		cobalt_set_tsd(u_winoff);
+		cobalt_thread_harden();
+	}
+
+	return ret;
 }
 
 /**
Signature · 1 line
2.16.4
author
#4 lore raw

PATCH 4/5 Revert "cobalt/kernel: Introduce __SCHED_CURRENT policy to setschedparam_ex"

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

This reverts commit b3c70f1a470397c915a6c206d07c7c342c164df4.

Was broken by design and is now no longer needed.

---
 include/cobalt/uapi/sched.h        |  3 ---
 kernel/cobalt/posix/thread.c       | 10 ----------
 kernel/cobalt/trace/cobalt-posix.h |  3 +--
 3 files changed, 1 insertion(+), 15 deletions(-)
include/cobalt/uapi/sched.h +0 -3
index 2a1df44b49..b672095c3b 100644
@@ -21,9 +21,6 @@
 #define SCHED_COBALT		42
 #define SCHED_WEAK		43
 
-/* for internal use */
-#define __SCHED_CURRENT		44
-
 #ifndef SCHED_SPORADIC
 #define SCHED_SPORADIC		10
 #define sched_ss_low_priority	sched_u.ss.__sched_low_priority
kernel/cobalt/posix/thread.c +0 -10
index c2a9fb46c5..bf9c082123 100644
@@ -242,7 +242,6 @@ struct xnthread_personality *cobalt_thread_finalize(struct xnthread *zombie)
 int __cobalt_thread_setschedparam_ex(struct cobalt_thread *thread, int policy,
 				     const struct sched_param_ex *param_ex)
 {
-	struct xnthread *base_thread = &thread->threadbase;
 	struct xnsched_class *sched_class;
 	union xnsched_policy_param param;
 	xnticks_t tslice;
@@ -257,15 +256,6 @@ int __cobalt_thread_setschedparam_ex(struct cobalt_thread *thread, int policy,
 		goto out;
 	}
 
-	if (policy == __SCHED_CURRENT) {
-		policy = base_thread->base_class->policy;
-		if (xnthread_base_priority(base_thread) == 0)
-			policy = SCHED_NORMAL;
-		else if (base_thread->base_class == &xnsched_class_rt &&
-			 xnthread_test_state(base_thread, XNRRB))
-			policy = SCHED_RR;
-	}
-
 	tslice = thread->threadbase.rrperiod;
 	sched_class = cobalt_sched_policy_param(&param, policy,
 						param_ex, &tslice);
kernel/cobalt/trace/cobalt-posix.h +1 -2
index 83ec748586..aa78efbc01 100644
@@ -202,8 +202,7 @@ DECLARE_EVENT_CLASS(syscall_exit,
 			 {SCHED_QUOTA, "quota"},		\
 			 {SCHED_SPORADIC, "sporadic"},		\
 			 {SCHED_COBALT, "cobalt"},		\
-			 {SCHED_WEAK, "weak"},			\
-			 {__SCHED_CURRENT, "<current>"})
+			 {SCHED_WEAK, "weak"})
 
 const char *cobalt_trace_parse_sched_params(struct trace_seq *, int,
 					    struct sched_param_ex *);
Signature · 1 line
2.16.4
author
#5 lore raw

PATCH 5/5 testsuite/smokey: Add pthread_setschedprio to setsched test case

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

Now that this path goes via an own syscall, better test it.

At this chance, make sure that the test runs the scheduler changes from primary mode.

---
 testsuite/smokey/setsched/setsched.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
testsuite/smokey/setsched/setsched.c +19 -0
index f227f654cc..111de9f378 100644
@@ -77,6 +77,8 @@ static void *thread_body(void *arg)
 	check_rt_schedparams(SCHED_FIFO, 1);
 	check_linux_schedparams(SCHED_FIFO, 1);
 
+	cobalt_thread_harden();
+
 	if (smokey_check_status(cobalt_thread_stat(thread_pid, &stats)))
 		pthread_exit((void *)(long)-EINVAL);
 	msw = stats.msw;
@@ -94,6 +96,23 @@ static void *thread_body(void *arg)
 
 	check_linux_schedparams(SCHED_FIFO, 2);
 
+	cobalt_thread_harden();
+
+	if (smokey_check_status(cobalt_thread_stat(thread_pid, &stats)))
+		pthread_exit((void *)(long)-EINVAL);
+	msw = stats.msw;
+
+	if (smokey_check_status(pthread_setschedprio(pthread_self(), 3)))
+		pthread_exit((void *)(long)-EINVAL);
+
+	check_rt_schedparams(SCHED_FIFO, 3);
+
+	if (smokey_check_status(cobalt_thread_stat(thread_pid, &stats)) ||
+	    !smokey_assert(stats.msw == msw))
+		pthread_exit((void *)(long)-EINVAL);
+
+	check_linux_schedparams(SCHED_FIFO, 3);
+
 	return (void *)0L;
 }
 
Signature · 1 line
2.16.4
author
#6 lore raw

Re: [PATCH 5/5] testsuite/smokey: Add pthread_setschedprio to setsched test case

Quoted 17 lines · Jan Kiszka via Xenomai

From: Jan Kiszka <[email protected]>

Now that this path goes via an own syscall, better test it.

At this chance, make sure that the test runs the scheduler changes from primary mode.

---
  testsuite/smokey/setsched/setsched.c | 19 +++++++++++++++++++
  1 file changed, 19 insertions(+)
testsuite/smokey/setsched/setsched.c +0 -0
index f227f654cc..111de9f378 100644

testsuite/smokey/setsched/setsched.c static void *thread_body(void *arg) · line 81

Quoted 5 lines
testsuite/smokey/setsched/setsched.c +2 -0
@@ -77,6 +77,8 @@ static void *thread_body(void *arg)
  	check_rt_schedparams(SCHED_FIFO, 1);
  	check_linux_schedparams(SCHED_FIFO, 1);
 
+	cobalt_thread_harden();
+

Seems this causes failures now when not building userspace with --enable-lazy-setsched. Will check if we can catch that case and handle it gracefully.

Jan

Quoted 28 lines
testsuite/smokey/setsched/setsched.c +17 -0
  	if (smokey_check_status(cobalt_thread_stat(thread_pid, &stats)))
  		pthread_exit((void *)(long)-EINVAL);
  	msw = stats.msw;
@@ -94,6 +96,23 @@ static void *thread_body(void *arg)
 
  	check_linux_schedparams(SCHED_FIFO, 2);
 
+	cobalt_thread_harden();
+
+	if (smokey_check_status(cobalt_thread_stat(thread_pid, &stats)))
+		pthread_exit((void *)(long)-EINVAL);
+	msw = stats.msw;
+
+	if (smokey_check_status(pthread_setschedprio(pthread_self(), 3)))
+		pthread_exit((void *)(long)-EINVAL);
+
+	check_rt_schedparams(SCHED_FIFO, 3);
+
+	if (smokey_check_status(cobalt_thread_stat(thread_pid, &stats)) ||
+	    !smokey_assert(stats.msw == msw))
+		pthread_exit((void *)(long)-EINVAL);
+
+	check_linux_schedparams(SCHED_FIFO, 3);
+
  	return (void *)0L;
  }
 
Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux