Skip to contentSkip to conversation

linux-user: fixes for sched_ syscalls

PATCH Superseded Tonis Tiigi 2 participants 5 messages Dec 11, 2021 – Dec 19, 2021

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover linux-user: fixes for sched_ syscalls

This patchset improves support for sched_* syscalls under user emulation. The first commit adds support for sched_g/setattr that was previously not implemented. There is no equivalent for these syscalls in libc, so I needed to redefine the struct from kernel. It can't be included directly because of conflict with libc sched headers.

The second commit fixes sched_g/setscheduler and sched_g/setparam, when QEMU is built with musl. Musl does not implement these due to conflict between what these functions should do in syscalls and libc https://git.musl-libc.org/cgit/musl/commit/?id=1e21e78bf7a5c24c217446d8760be7b7188711c2 . I've changed it to call syscall directly what should always be the expected behavior for the user.

Via https://github.com/tonistiigi/binfmt/pull/70 with additional tests.

Tonis Tiigi (2):
linux-user: add sched_getattr support
linux-user: call set/getscheduler set/getparam directly

 linux-user/syscall.c      | 71 ++++++++++++++++++++++++++++++++++++---
 linux-user/syscall_defs.h | 15 +++++++++
 2 files changed, 82 insertions(+), 4 deletions(-)
Signature · 1 line
2.30.1 (Apple Git-130)
#1 lore raw

PATCH 1/2 linux-user: add sched_getattr support

---
 linux-user/syscall.c      | 55 +++++++++++++++++++++++++++++++++++++++
 linux-user/syscall_defs.h | 15 +++++++++++
 2 files changed, 70 insertions(+)
linux-user/syscall.c +55 -0
index f1cfcc8104..670b61b2ef 100644
@@ -339,6 +339,10 @@ _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
 #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
 _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
           unsigned long *, user_mask_ptr);
+#define __NR_sys_sched_getattr __NR_sched_getattr
+_syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr, unsigned int, size, unsigned int, flags);
+#define __NR_sys_sched_setattr __NR_sched_setattr
+_syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr, unsigned int, flags);
 #define __NR_sys_getcpu __NR_getcpu
 _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
 _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
@@ -10593,6 +10597,57 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         }
     case TARGET_NR_sched_getscheduler:
         return get_errno(sched_getscheduler(arg1));
+    case TARGET_NR_sched_getattr:
+        {
+            struct target_sched_attr *target_scha;
+            struct target_sched_attr scha;
+            if (arg2 == 0) {
+                return -TARGET_EINVAL;
+            }
+            ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4));
+            if (!is_error(ret)) {
+                if (!lock_user_struct(VERIFY_WRITE, target_scha, arg2, 0))
+                    return -TARGET_EFAULT;
+                target_scha->size = tswap32(scha.size);
+                target_scha->sched_policy = tswap32(scha.sched_policy);
+                target_scha->sched_flags = tswap64(scha.sched_policy);
+                target_scha->sched_nice = tswap32(scha.sched_nice);
+                target_scha->sched_priority = tswap32(scha.sched_priority);
+                target_scha->sched_runtime = tswap64(scha.sched_runtime);
+                target_scha->sched_deadline = tswap64(scha.sched_deadline);
+                target_scha->sched_period = tswap64(scha.sched_period);
+                if (scha.size >= 0x38) {
+                    target_scha->sched_util_min = tswap32(scha.sched_util_min);
+                    target_scha->sched_util_max = tswap32(scha.sched_util_max);
+                }
+                unlock_user_struct(target_scha, arg2, 1);
+            }
+            return ret;
+        }
+    case TARGET_NR_sched_setattr:
+        {
+            struct target_sched_attr *target_scha;
+            struct target_sched_attr scha;
+            if (arg2 == 0) {
+                return -TARGET_EINVAL;
+            }
+            if (!lock_user_struct(VERIFY_READ, target_scha, arg2, 1))
+                return -TARGET_EFAULT;
+            scha.size = tswap32(target_scha->size);
+            scha.sched_policy = tswap32(target_scha->sched_policy);
+            scha.sched_flags = tswap64(target_scha->sched_flags);
+            scha.sched_nice = tswap32(target_scha->sched_nice);
+            scha.sched_priority = tswap32(target_scha->sched_priority);
+            scha.sched_runtime = tswap64(target_scha->sched_runtime);
+            scha.sched_deadline = tswap64(target_scha->sched_deadline);
+            scha.sched_period = tswap64(target_scha->sched_period);
+            if (scha.size >= 0x38) {
+                scha.sched_util_min = tswap32(target_scha->sched_util_min);
+                scha.sched_util_max = tswap32(target_scha->sched_util_max);
+            }
+            unlock_user_struct(target_scha, arg2, 0);
+            return get_errno(sys_sched_setattr(arg1, &scha, arg3));
+        }
     case TARGET_NR_sched_yield:
         return get_errno(sched_yield());
     case TARGET_NR_sched_get_priority_max:
linux-user/syscall_defs.h +15 -0
index 0b13975937..715ec75462 100644
@@ -2914,4 +2914,19 @@ struct target_statx {
    /* 0x100 */
 };
 
+/* from kernel's include/linux/sched/types.h */
+struct target_sched_attr {
+    uint32_t size;
+    uint32_t sched_policy;
+    uint64_t sched_flags;
+    int32_t sched_nice;
+    uint32_t sched_priority;
+    uint64_t sched_runtime;
+    uint64_t sched_deadline;
+    uint64_t sched_period;
+    // 0x30
+    uint32_t sched_util_min;
+    uint32_t sched_util_max;
+};
+
 #endif
Signature · 1 line
2.30.1 (Apple Git-130)
#2 lore raw

Re: [PATCH 1/2] linux-user: add sched_getattr support

Le 11/12/2021 à 03:27, Tonis Tiigi a écrit :

Quoted 27 lines
---
  linux-user/syscall.c      | 55 +++++++++++++++++++++++++++++++++++++++
  linux-user/syscall_defs.h | 15 +++++++++++
  2 files changed, 70 insertions(+)
linux-user/syscall.c +6 -0
index f1cfcc8104..670b61b2ef 100644
@@ -339,6 +339,10 @@ _syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
  #define __NR_sys_sched_setaffinity __NR_sched_setaffinity
  _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
            unsigned long *, user_mask_ptr);
+#define __NR_sys_sched_getattr __NR_sched_getattr
+_syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr, unsigned int, size, unsigned int, flags);
+#define __NR_sys_sched_setattr __NR_sched_setattr
+_syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr, unsigned int, flags);
  #define __NR_sys_getcpu __NR_getcpu
  _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
  _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
@@ -10593,6 +10597,57 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
          }
      case TARGET_NR_sched_getscheduler:
          return get_errno(sched_getscheduler(arg1));
+    case TARGET_NR_sched_getattr:
+        {

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 10611

Quoted 10 lines
linux-user/syscall.c +8 -0
+            struct target_sched_attr *target_scha;
+            struct target_sched_attr scha;
+            if (arg2 == 0) {
+                return -TARGET_EINVAL;
+            }
 
 
+            ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4));
+            if (!is_error(ret)) {
+                if (!lock_user_struct(VERIFY_WRITE, target_scha, arg2, 0))

As sizeof(target_scha) can be greater than arg3, you should user lock_user() with arg3 as length.

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 10612

Quoted 1 line
linux-user/syscall.c +1 -0
+                    return -TARGET_EFAULT;

QEMU style uses braces {} even for one line statement.

You can run scripts/checkpath.pl to check the style

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 10619

Quoted 9 lines
linux-user/syscall.c +9 -0
+                target_scha->size = tswap32(scha.size);
+                target_scha->sched_policy = tswap32(scha.sched_policy);
+                target_scha->sched_flags = tswap64(scha.sched_policy);
+                target_scha->sched_nice = tswap32(scha.sched_nice);
+                target_scha->sched_priority = tswap32(scha.sched_priority);
+                target_scha->sched_runtime = tswap64(scha.sched_runtime);
+                target_scha->sched_deadline = tswap64(scha.sched_deadline);
+                target_scha->sched_period = tswap64(scha.sched_period);
+                if (scha.size >= 0x38) {

You should use offsetot(target_sched_attr, sched_util_min) rather than 0x38.

Quoted 6 lines
linux-user/syscall.c +6 -0
+                    target_scha->sched_util_min = tswap32(scha.sched_util_min);
+                    target_scha->sched_util_max = tswap32(scha.sched_util_max);
+                }
+                unlock_user_struct(target_scha, arg2, 1);
+            }
+            return ret;

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 10635

Quoted 10 lines
linux-user/syscall.c +10 -0
+        }
+    case TARGET_NR_sched_setattr:
+        {
+            struct target_sched_attr *target_scha;
+            struct target_sched_attr scha;
+            if (arg2 == 0) {
+                return -TARGET_EINVAL;
+            }
+            if (!lock_user_struct(VERIFY_READ, target_scha, arg2, 1))
+                return -TARGET_EFAULT;

same comment

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 10644

Quoted 9 lines
linux-user/syscall.c +9 -0
+            scha.size = tswap32(target_scha->size);
+            scha.sched_policy = tswap32(target_scha->sched_policy);
+            scha.sched_flags = tswap64(target_scha->sched_flags);
+            scha.sched_nice = tswap32(target_scha->sched_nice);
+            scha.sched_priority = tswap32(target_scha->sched_priority);
+            scha.sched_runtime = tswap64(target_scha->sched_runtime);
+            scha.sched_deadline = tswap64(target_scha->sched_deadline);
+            scha.sched_period = tswap64(target_scha->sched_period);
+            if (scha.size >= 0x38) {

same comment

Quoted 18 lines
linux-user/syscall.c +6 -0
+                scha.sched_util_min = tswap32(target_scha->sched_util_min);
+                scha.sched_util_max = tswap32(target_scha->sched_util_max);
+            }
+            unlock_user_struct(target_scha, arg2, 0);
+            return get_errno(sys_sched_setattr(arg1, &scha, arg3));
+        }
      case TARGET_NR_sched_yield:
          return get_errno(sched_yield());
      case TARGET_NR_sched_get_priority_max:
linux-user/syscall_defs.h +1 -0
index 0b13975937..715ec75462 100644
@@ -2914,4 +2914,19 @@ struct target_statx {
     /* 0x100 */
  };
 
+/* from kernel's include/linux/sched/types.h */

linux-user/syscall_defs.h struct target_statx { · line 2927

Quoted 10 lines
linux-user/syscall_defs.h +10 -0
+struct target_sched_attr {
+    uint32_t size;
+    uint32_t sched_policy;
+    uint64_t sched_flags;
+    int32_t sched_nice;
+    uint32_t sched_priority;
+    uint64_t sched_runtime;
+    uint64_t sched_deadline;
+    uint64_t sched_period;
+    // 0x30

Remove this comment

linux-user/syscall_defs.h struct target_statx { · line 2931

Quoted 4 lines
linux-user/syscall_defs.h +4 -0
+    uint32_t sched_util_min;
+    uint32_t sched_util_max;
+};
+

Please, use abi_uint, abit_int, abi_ullong rather than uint32_t, int32_t and uint64_t. These types forces alignment according to the target type.

Thanks,
Laurent

#3 lore raw

PATCH 2/2 linux-user: call set/getscheduler set/getparam directly

In reply to Tonis Tiigi

There seems to be difference in syscall and libc definition of these methods and therefore musl does not implement them (1e21e78bf7). Call syscall directly to ensure the behavior of the libc of user application, not the libc that was used to build QEMU.

---
 linux-user/syscall.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
linux-user/syscall.c +12 -4
index 670b61b2ef..263d7fac5e 100644
@@ -343,6 +343,14 @@ _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
 _syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr, unsigned int, size, unsigned int, flags);
 #define __NR_sys_sched_setattr __NR_sched_setattr
 _syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr, unsigned int, flags);
+#define __NR_sys_sched_getscheduler __NR_sched_getscheduler
+_syscall1(int, sys_sched_getscheduler, pid_t, pid);
+#define __NR_sys_sched_setscheduler __NR_sched_setscheduler
+_syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy, const struct sched_param *, param);
+#define __NR_sys_sched_getparam __NR_sched_getparam
+_syscall2(int, sys_sched_getparam, pid_t, pid, struct sched_param *, param);
+#define __NR_sys_sched_setparam __NR_sched_setparam
+_syscall2(int, sys_sched_setparam, pid_t, pid, const struct sched_param *, param);
 #define __NR_sys_getcpu __NR_getcpu
 _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
 _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
@@ -10563,7 +10571,7 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
                 return -TARGET_EFAULT;
             schp.sched_priority = tswap32(target_schp->sched_priority);
             unlock_user_struct(target_schp, arg2, 0);
-            return get_errno(sched_setparam(arg1, &schp));
+            return get_errno(sys_sched_setparam(arg1, &schp));
         }
     case TARGET_NR_sched_getparam:
         {
@@ -10573,7 +10581,7 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
             if (arg2 == 0) {
                 return -TARGET_EINVAL;
             }
-            ret = get_errno(sched_getparam(arg1, &schp));
+            ret = get_errno(sys_sched_getparam(arg1, &schp));
             if (!is_error(ret)) {
                 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
                     return -TARGET_EFAULT;
@@ -10593,10 +10601,10 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
                 return -TARGET_EFAULT;
             schp.sched_priority = tswap32(target_schp->sched_priority);
             unlock_user_struct(target_schp, arg3, 0);
-            return get_errno(sched_setscheduler(arg1, arg2, &schp));
+            return get_errno(sys_sched_setscheduler(arg1, arg2, &schp));
         }
     case TARGET_NR_sched_getscheduler:
-        return get_errno(sched_getscheduler(arg1));
+        return get_errno(sys_sched_getscheduler(arg1));
     case TARGET_NR_sched_getattr:
         {
             struct target_sched_attr *target_scha;
Signature · 1 line
2.30.1 (Apple Git-130)
#4 lore raw

Re: [PATCH 2/2] linux-user: call set/getscheduler set/getparam directly

Le 11/12/2021 à 03:27, Tonis Tiigi a écrit :

Quoted 51 lines

There seems to be difference in syscall and libc definition of these methods and therefore musl does not implement them (1e21e78bf7). Call syscall directly to ensure the behavior of the libc of user application, not the libc that was used to build QEMU.

---
  linux-user/syscall.c | 16 ++++++++++++----
  1 file changed, 12 insertions(+), 4 deletions(-)
linux-user/syscall.c +10 -2
index 670b61b2ef..263d7fac5e 100644
@@ -343,6 +343,14 @@ _syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
  _syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr, unsigned int, size, unsigned int, flags);
  #define __NR_sys_sched_setattr __NR_sched_setattr
  _syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr, unsigned int, flags);
+#define __NR_sys_sched_getscheduler __NR_sched_getscheduler
+_syscall1(int, sys_sched_getscheduler, pid_t, pid);
+#define __NR_sys_sched_setscheduler __NR_sched_setscheduler
+_syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy, const struct sched_param *, param);
+#define __NR_sys_sched_getparam __NR_sched_getparam
+_syscall2(int, sys_sched_getparam, pid_t, pid, struct sched_param *, param);
+#define __NR_sys_sched_setparam __NR_sched_setparam
+_syscall2(int, sys_sched_setparam, pid_t, pid, const struct sched_param *, param);
  #define __NR_sys_getcpu __NR_getcpu
  _syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
  _syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
@@ -10563,7 +10571,7 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
                  return -TARGET_EFAULT;
              schp.sched_priority = tswap32(target_schp->sched_priority);
              unlock_user_struct(target_schp, arg2, 0);
-            return get_errno(sched_setparam(arg1, &schp));
+            return get_errno(sys_sched_setparam(arg1, &schp));
          }
      case TARGET_NR_sched_getparam:
          {
@@ -10573,7 +10581,7 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
              if (arg2 == 0) {
                  return -TARGET_EINVAL;
              }
-            ret = get_errno(sched_getparam(arg1, &schp));
+            ret = get_errno(sys_sched_getparam(arg1, &schp));
              if (!is_error(ret)) {
                  if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
                      return -TARGET_EFAULT;
@@ -10593,10 +10601,10 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
                  return -TARGET_EFAULT;
              schp.sched_priority = tswap32(target_schp->sched_priority);

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 10610

Quoted 10 lines
linux-user/syscall.c +2 -2
              unlock_user_struct(target_schp, arg3, 0);
-            return get_errno(sched_setscheduler(arg1, arg2, &schp));
+            return get_errno(sys_sched_setscheduler(arg1, arg2, &schp));
          }
      case TARGET_NR_sched_getscheduler:
-        return get_errno(sched_getscheduler(arg1));
+        return get_errno(sys_sched_getscheduler(arg1));
      case TARGET_NR_sched_getattr:
          {
              struct target_sched_attr *target_scha;

It looks good, but you need to introduce also target_sched_param using abi_int.

Thanks,
Laurent