sched: Introduce CPU soft affinity for processes
Subsystems (6)
cover sched: Introduce CPU soft affinity for processes
For multi-tenancy currently there are mechanisms to share the system CPUs by time-sharing (e.g: CFS) and by dividing up the system in 'rigid' containers by using system calls like sched_setaffinity. There is no existing way in the linux kernel today, for flexible workloads where there is a need to give the whole system while still maintaining a notion of preference to CPUs.
This patch introduces a new CPU mask, 'cpus_preferred' within the task_struct structure and allows applications a way to specify a set of CPUs which the application would like to run on. The scheduler will try to honor the applications' request the best it can, however if the scheduler finds that there are no idle CPUs within the preferred list, it shall run the application anywhere within the system.
This can be used to design soft containers which allows a tenant to use more capacity than he is entitled to when others aren't fully using theirs. The advantage of space sharing the system as opposed to time sharing is that you maintain more cache locality when the soft containers are being utilized.
Since this behavior is observed on every scheduling decision, the application gets to run on its preferred CPUs as long as the application does not overuse its specified resources.
The design of soft containers still needs more user-space code however, this is what is needed from the kernel.
FAQs:
Q) What if I set "hard" affinity after I set a preference by using soft affinity?
A: Hard affinity will over-ride any previous soft affinity.
Q) What if my application had already specified a "hard" affinity? Can I still provide a set of CPUs for soft affinity?
A: Yes, it will work as long as the new soft affinity is a subset of the "hard" affinity.
Q) Can I have mutually exclusive hard and soft affinities?
A: No, soft affinity is always a subset of hard affinity.
Note:
Ignore the kernel/sched/tick-sched.c change. It is just fixing a build error on Peter's tree.
Rohit Jain (2):
sched: Introduce new flags to sched_setaffinity to support soft
affinity.
sched: Actual changes after adding SCHED_SOFT_AFFINITY to make it work
with the scheduler
arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/init_task.h | 1 + include/linux/sched.h | 4 +- include/linux/syscalls.h | 3 + include/uapi/asm-generic/unistd.h | 4 +- include/uapi/linux/sched.h | 3 + kernel/compat.c | 2 +- kernel/sched/core.c | 167 ++++++++++++++++++++++++++++----- kernel/sched/cpudeadline.c | 4 +- kernel/sched/cpupri.c | 4 +- kernel/sched/fair.c | 116 +++++++++++++++++------ kernel/time/tick-sched.c | 1 + 12 files changed, 250 insertions(+), 60 deletions(-)
Signature · 1 line
2.7.4
PATCH 1/2 sched: Introduce new flags to sched_setaffinity to support soft affinity.
These are the changes for supporting the system call and set the cpus_preferred mask as the application wants. This patch does not make the cpus_preferred take any action.
--- arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/init_task.h | 1 + include/linux/sched.h | 4 +- include/linux/syscalls.h | 3 + include/uapi/asm-generic/unistd.h | 4 +- include/uapi/linux/sched.h | 3 + kernel/compat.c | 2 +- kernel/sched/core.c | 167 ++++++++++++++++++++++++++++----- kernel/time/tick-sched.c | 1 + 9 files changed, 159 insertions(+), 27 deletions(-)
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -339,6 +339,7 @@ 330 common pkey_alloc sys_pkey_alloc 331 common pkey_free sys_pkey_free 332 common statx sys_statx +333 common sched_setaffinity_flags sys_sched_setaffinity_flags # # x32-specific system call numbers start at 512 to avoid cache impact
include/linux/init_task.h +1 -0
@@ -235,6 +235,7 @@ extern struct cred init_cred; .normal_prio = MAX_PRIO-20, \ .policy = SCHED_NORMAL, \ .cpus_allowed = CPU_MASK_ALL, \ + .cpus_preferred = CPU_MASK_ALL, \ .nr_cpus_allowed= NR_CPUS, \ .mm = NULL, \ .active_mm = &init_mm, \
include/linux/sched.h +3 -1
@@ -582,6 +582,7 @@ struct task_struct {unsigned int policy;int nr_cpus_allowed;cpumask_t cpus_allowed; +cpumask_t cpus_preferred;#ifdef CONFIG_PREEMPT_RCU int rcu_read_lock_nesting;
@@ -1647,7 +1648,8 @@ static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)# define vcpu_is_preempted (cpu) false#endif -extern long sched_setaffinity (pid_t pid,const struct cpumask *new_mask); +extern long sched_setaffinity (pid_t pid,const struct cpumask *new_mask, +int flags);extern long sched_getaffinity (pid_t pid,struct cpumask *mask);#ifndef TASK_SIZE_OF
include/linux/syscalls.h +3 -0
@@ -326,6 +326,9 @@ asmlinkage long sys_sched_get_priority_max(int policy);asmlinkage long sys_sched_get_priority_min (int policy);asmlinkage long sys_sched_rr_get_interval (pid_t pid,struct timespec __user *interval); +asmlinkage long sys_sched_setaffinity_flags (pid_t pid,unsigned int len, +unsigned long __user *user_mask_ptr, +int flags);asmlinkage long sys_setpriority (int which,int who,int niceval);asmlinkage long sys_getpriority (int which,int who);
include/uapi/asm-generic/unistd.h +3 -1
@@ -376,6 +376,8 @@ __SYSCALL(__NR_sched_getparam, sys_sched_getparam)#define __NR_sched_setaffinity 122__SC_COMP (__NR_sched_setaffinity, sys_sched_setaffinity, \ compat_sys_sched_setaffinity) +#define __NR_sched_setaffinity_flags 293 +__SYSCALL(__NR_sched_setaffinity_flags, sys_sched_setaffinity_flags)#define __NR_sched_getaffinity 123__SC_COMP (__NR_sched_getaffinity, sys_sched_getaffinity, \ compat_sys_sched_getaffinity)
@@ -733,7 +735,7 @@ __SYSCALL(__NR_pkey_free, sys_pkey_free) __SYSCALL(__NR_statx, sys_statx)#undef __NR_syscalls -#define __NR_syscalls 292 +#define __NR_syscalls 293/* * All syscalls below here should go away really,
include/uapi/linux/sched.h +3 -0
@@ -49,4 +49,7 @@#define SCHED_FLAG_RESET_ON_FORK 0x01#define SCHED_FLAG_RECLAIM 0x02 +#define SCHED_HARD_AFFINITY 0 +#define SCHED_SOFT_AFFINITY 1 +#endif /* _UAPI_LINUX_SCHED_H */
kernel/compat.c +1 -1
@@ -356,7 +356,7 @@ COMPAT_SYSCALL_DEFINE3(sched_setaffinity, compat_pid_t, pid,if (retval) goto out; - retval =sched_setaffinity (pid, new_mask); + retval =sched_setaffinity (pid, new_mask,SCHED_HARD_AFFINITY ); out:free_cpumask_var (new_mask);return retval;
kernel/sched/core.c +143 -24
217 lines not on this page · raw message
kernel/time/tick-sched.c +1 -0
@@ -19,6 +19,7 @@#include <linux/percpu.h> #include <linux/nmi.h> #include <linux/profile.h> +#include <linux/vmstat.h> #include <linux/sched/signal.h> #include <linux/sched/clock.h> #include <linux/sched/stat.h>
Signature · 1 line
2.7.4
kbuild test robot
build botRe: [PATCH 1/2] sched: Introduce new flags to sched_setaffinity to support soft affinity.
url: https://github.com/0day-ci/linux/commits/Rohit-Jain/sched-Introduce-new-flags-to-sched_setaffinity-to-support-soft-affinity/20170921-140313 config: i386-randconfig-x018-201738 (attached as…
Hi Rohit,
[auto build test ERROR on tip/sched/core]
[also build test ERROR on v4.14-rc1 next-20170920]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Rohit-Jain/sched-Introduce-new-flags-to-sched_setaffinity-to-support-soft-affinity/20170921-140313
config: i386-randconfig-x018-201738 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:
# save the attached .config to linux build tree
make ARCH=i386 All errors (new ones prefixed by >>):
kernel//trace/trace_hwlat.c: In function 'move_to_next_cpu':
>> kernel//trace/trace_hwlat.c:299:2: error: too few arguments to function 'sched_setaffinity'
sched_setaffinity(0, current_mask);
^~~~~~~~~~~~~~~~~
In file included from include/linux/kthread.h:5:0,
from kernel//trace/trace_hwlat.c:42:
include/linux/sched.h:1636:13: note: declared here
extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask,
^~~~~~~~~~~~~~~~~
kernel//trace/trace_hwlat.c: In function 'start_kthread':
kernel//trace/trace_hwlat.c:372:2: error: too few arguments to function 'sched_setaffinity'
sched_setaffinity(kthread->pid, current_mask);
^~~~~~~~~~~~~~~~~ In file included from include/linux/kthread.h:5:0,
from kernel//trace/trace_hwlat.c:42:
include/linux/sched.h:1636:13: note: declared here
extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask,
^~~~~~~~~~~~~~~~~
vim +/sched_setaffinity +299 kernel//trace/trace_hwlat.c
0330f7aa Steven Rostedt (Red Hat 2016-07-15 269)
f447c196 Steven Rostedt (VMware 2017-01-31 270) static void move_to_next_cpu(void)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 271) {
f447c196 Steven Rostedt (VMware 2017-01-31 272) struct cpumask *current_mask = &save_cpumask;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 273) int next_cpu;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 274)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 275) if (disable_migrate)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 276) return;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 277) /*
0330f7aa Steven Rostedt (Red Hat 2016-07-15 278) * If for some reason the user modifies the CPU affinity
0330f7aa Steven Rostedt (Red Hat 2016-07-15 279) * of this thread, than stop migrating for the duration
0330f7aa Steven Rostedt (Red Hat 2016-07-15 280) * of the current test.
0330f7aa Steven Rostedt (Red Hat 2016-07-15 281) */
0330f7aa Steven Rostedt (Red Hat 2016-07-15 282) if (!cpumask_equal(current_mask, ¤t->cpus_allowed))
0330f7aa Steven Rostedt (Red Hat 2016-07-15 283) goto disable;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 284)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 285) get_online_cpus();
0330f7aa Steven Rostedt (Red Hat 2016-07-15 286) cpumask_and(current_mask, cpu_online_mask, tracing_buffer_mask);
0330f7aa Steven Rostedt (Red Hat 2016-07-15 287) next_cpu = cpumask_next(smp_processor_id(), current_mask);
0330f7aa Steven Rostedt (Red Hat 2016-07-15 288) put_online_cpus();
0330f7aa Steven Rostedt (Red Hat 2016-07-15 289)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 290) if (next_cpu >= nr_cpu_ids)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 291) next_cpu = cpumask_first(current_mask);
0330f7aa Steven Rostedt (Red Hat 2016-07-15 292)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 293) if (next_cpu >= nr_cpu_ids) /* Shouldn't happen! */
0330f7aa Steven Rostedt (Red Hat 2016-07-15 294) goto disable;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 295)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 296) cpumask_clear(current_mask);
0330f7aa Steven Rostedt (Red Hat 2016-07-15 297) cpumask_set_cpu(next_cpu, current_mask);
0330f7aa Steven Rostedt (Red Hat 2016-07-15 298)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 @299) sched_setaffinity(0, current_mask);
0330f7aa Steven Rostedt (Red Hat 2016-07-15 300) return;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 301)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 302) disable:
0330f7aa Steven Rostedt (Red Hat 2016-07-15 303) disable_migrate = true;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 304) }
0330f7aa Steven Rostedt (Red Hat 2016-07-15 305) :::::: The code at line 299 was first introduced by commit
:::::: 0330f7aa8ee63d0c435c0cb4e47ea06235ee4b7f tracing: Have hwlat trace migrate across tracing_cpumask CPUs
:::::: TO: Steven Rostedt (Red Hat) <[email protected]> :::::: CC: Steven Rostedt <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
kbuild test robot
build botRe: [PATCH 1/2] sched: Introduce new flags to sched_setaffinity to support soft affinity.
In reply to Rohit Jainurl: https://github.com/0day-ci/linux/commits/Rohit-Jain/sched-Introduce-new-flags-to-sched_setaffinity-to-support-soft-affinity/20170921-140313 reproduce:
Hi Rohit,
[auto build test WARNING on tip/sched/core]
[also build test WARNING on v4.14-rc1 next-20170921]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
# apt-get install sparse
make ARCH=x86_64 allmodconfig
make C=1 CF=-D__CHECK_ENDIAN__sparse warnings: (new ones prefixed by >>)
vim +299 kernel/trace/trace_hwlat.c
0330f7aa Steven Rostedt (Red Hat 2016-07-15 269)
f447c196 Steven Rostedt (VMware 2017-01-31 270) static void move_to_next_cpu(void)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 271) {
f447c196 Steven Rostedt (VMware 2017-01-31 272) struct cpumask *current_mask = &save_cpumask;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 273) int next_cpu;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 274)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 275) if (disable_migrate)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 276) return;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 277) /*
0330f7aa Steven Rostedt (Red Hat 2016-07-15 278) * If for some reason the user modifies the CPU affinity
0330f7aa Steven Rostedt (Red Hat 2016-07-15 279) * of this thread, than stop migrating for the duration
0330f7aa Steven Rostedt (Red Hat 2016-07-15 280) * of the current test.
0330f7aa Steven Rostedt (Red Hat 2016-07-15 281) */
0330f7aa Steven Rostedt (Red Hat 2016-07-15 282) if (!cpumask_equal(current_mask, ¤t->cpus_allowed))
0330f7aa Steven Rostedt (Red Hat 2016-07-15 283) goto disable;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 284)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 285) get_online_cpus();
0330f7aa Steven Rostedt (Red Hat 2016-07-15 286) cpumask_and(current_mask, cpu_online_mask, tracing_buffer_mask);
0330f7aa Steven Rostedt (Red Hat 2016-07-15 287) next_cpu = cpumask_next(smp_processor_id(), current_mask);
0330f7aa Steven Rostedt (Red Hat 2016-07-15 288) put_online_cpus();
0330f7aa Steven Rostedt (Red Hat 2016-07-15 289)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 290) if (next_cpu >= nr_cpu_ids)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 291) next_cpu = cpumask_first(current_mask);
0330f7aa Steven Rostedt (Red Hat 2016-07-15 292)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 293) if (next_cpu >= nr_cpu_ids) /* Shouldn't happen! */
0330f7aa Steven Rostedt (Red Hat 2016-07-15 294) goto disable;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 295)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 296) cpumask_clear(current_mask);
0330f7aa Steven Rostedt (Red Hat 2016-07-15 297) cpumask_set_cpu(next_cpu, current_mask);
0330f7aa Steven Rostedt (Red Hat 2016-07-15 298)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 @299) sched_setaffinity(0, current_mask);
0330f7aa Steven Rostedt (Red Hat 2016-07-15 300) return;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 301)
0330f7aa Steven Rostedt (Red Hat 2016-07-15 302) disable:
0330f7aa Steven Rostedt (Red Hat 2016-07-15 303) disable_migrate = true;
0330f7aa Steven Rostedt (Red Hat 2016-07-15 304) }
0330f7aa Steven Rostedt (Red Hat 2016-07-15 305) :::::: The code at line 299 was first introduced by commit
:::::: 0330f7aa8ee63d0c435c0cb4e47ea06235ee4b7f tracing: Have hwlat trace migrate across tracing_cpumask CPUs
:::::: TO: Steven Rostedt (Red Hat) <[email protected]> :::::: CC: Steven Rostedt <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
PATCH 2/2 sched: Actual changes after adding SCHED_SOFT_AFFINITY to make it work with the scheduler
In reply to Rohit JainThese are the changes which change the scheduler behavior based on the cpus_preferred mask. Keep in mind that when the system call changes cpus_allowed mask, cpus_preferred and cpus_allowed become the same.
--- kernel/sched/cpudeadline.c | 4 +- kernel/sched/cpupri.c | 4 +- kernel/sched/fair.c | 116 +++++++++++++++++++++++++++++++++------------ 3 files changed, 91 insertions(+), 33 deletions(-)
kernel/sched/cpudeadline.c +2 -2
@@ -127,13 +127,13 @@ int cpudl_find(struct cpudl *cp, struct task_struct *p,const struct sched_dl_entity *dl_se = &p->dl;if (later_mask && -cpumask_and (later_mask, cp->free_cpus, &p->cpus_allowed)) { +cpumask_and (later_mask, cp->free_cpus, &p->cpus_preferred)) {return 1 ; }else {int best_cpu =cpudl_maximum (cp);WARN_ON (best_cpu !=-1 && !cpu_present (best_cpu)); -if (cpumask_test_cpu (best_cpu, &p->cpus_allowed) && +if (cpumask_test_cpu (best_cpu, &p->cpus_preferred) &&dl_time_before (dl_se->deadline, cp->elements[0 ].dl)) {if (later_mask)cpumask_set_cpu (best_cpu, later_mask);
kernel/sched/cpupri.c +2 -2
@@ -103,11 +103,11 @@ int cpupri_find(struct cpupri *cp, struct task_struct *p,if (skip)continue ; -if (cpumask_any_and (&p->cpus_allowed, vec->mask) >= nr_cpu_ids) +if (cpumask_any_and (&p->cpus_preferred, vec->mask) >= nr_cpu_ids)continue ;if (lowest_mask) { -cpumask_and (lowest_mask, &p->cpus_allowed, vec->mask); +cpumask_and (lowest_mask, &p->cpus_preferred, vec->mask);/* * We have to ensure that we have at least one bit
kernel/sched/fair.c +87 -29
192 lines not on this page · raw message
Signature · 1 line
2.7.4
Peter Zijlstra
maintainerRe: [PATCH 2/2] sched: Actual changes after adding SCHED_SOFT_AFFINITY to make it work with the scheduler
kernel/sched/fair.c void __update_idle_core(struct rq *rq) · line 6043
Quoted 5 lines · Rohit Jain
kernel/sched/fair.c +1
-0@@ -6019,7 +6040,8 @@ void __update_idle_core(struct rq *rq)static int select_idle_core (struct task_struct *p,struct sched_domain *sd,int target) {struct cpumask *cpus =this_cpu_cpumask_var_ptr (select_idle_mask); +struct cpumask *pcpus =this_cpu_cpumask_var_ptr (select_idle_mask);
This is broken... they're the exact _same_ variable.
Quoted 6 lines
kernel/sched/fair.c +1
-0+int core;if (!static_branch_likely (&sched_smt_present))return -1 ;@@ -6028,20 +6050,21 @@ static int select_idle_core(struct task_struct *p, struct sched_domain *sd, intreturn -1 ;
kernel/sched/fair.c static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int · line 6060
Quoted 10 lines
kernel/sched/fair.c +6
-0cpumask_and (cpus,sched_domain_span (sd), &p->cpus_allowed); +cpumask_and (pcpus, cpus, &p->cpus_preferred); + core =scan_cpu_mask_for_idle_cores (pcpus, target); +if (core >=0 ) +return core; +if (cpumask_equal (cpus, pcpus)) + goto out;
Therefore this _must_ be true.
Also, you're touching one of the hottest paths in the whole scheduler for this half arsed feature, not going to happen.
You further failed to teach the actual load-balancer of this new mask, so it will still happily move tasks around.
Re: [PATCH 2/2] sched: Actual changes after adding SCHED_SOFT_AFFINITY to make it work with the scheduler
Quoted 19 lines · Peter Zijlstra
kernel/sched/fair.c void __update_idle_core(struct rq *rq) · line 6043
Quoted 5 lines · Rohit Jain
kernel/sched/fair.c +1
-0@@ -6019,7 +6040,8 @@ void __update_idle_core(struct rq *rq)static int select_idle_core (struct task_struct *p,struct sched_domain *sd,int target) {struct cpumask *cpus =this_cpu_cpumask_var_ptr (select_idle_mask); +struct cpumask *pcpus =this_cpu_cpumask_var_ptr (select_idle_mask);This is broken... they're the exact _same_ variable.
Quoted 11 lines
kernel/sched/fair.c +3
-0+int core;if (!static_branch_likely (&sched_smt_present))return -1 ;@@ -6028,20 +6050,21 @@ static int select_idle_core(struct task_struct *p, struct sched_domain *sd, intreturn -1 ;cpumask_and (cpus,sched_domain_span (sd), &p->cpus_allowed); +cpumask_and (pcpus, cpus, &p->cpus_preferred); + core =scan_cpu_mask_for_idle_cores (pcpus, target);
Quoted 6 lines
kernel/sched/fair.c static int select_idle_core(struct task_struct *p, struct sched_domain *sd, int · line 6060
Quoted 5 lines
kernel/sched/fair.c +4
-0+if (core >=0 ) +return core; +if (cpumask_equal (cpus, pcpus)) + goto out;Therefore this _must_ be true.
You are right, totally screwed it up here :(
Quoted 3 lines
Also, you're touching one of the hottest paths in the whole scheduler
Yes, I am touching that code path but the number of CPUs scanned will still remain the same (by intent at least). Because we will scan the preferred first and then the allowed mask. I just added an 'ordering' in searching. Please correct me if you meant something else entirely.
Quoted 3 lines
You further failed to teach the actual load-balancer of this new mask, so it will still happily move tasks around.
We were thinking because on every wakeup in idle CPU search we 'force' it to search the preferred CPUs first may be its OK to let it be stolen in idle CPU load balancing because eventually it will come back to the preferred CPUs? Again, may be my understanding is not right.