cpu isolation: infra to block interference to select CPUs
Subsystems (5)
RFC PATCH 3/7 introduce _fail variants of stop_machine functions
In reply to Marcelo TosattiIntroduce stop_machine_fail and stop_machine_cpuslocked_fail, which check if any online CPU in the system is tagged as a block interference CPU.
If so, returns an error.
include/linux/stop_machine.h +6 -0
@@ -113,6 +113,9 @@ static inline void print_stop_info(const*/ int stop_machine (cpu_stop_fn_t fn,void *data,const struct cpumask *cpus); + +int stop_machine_fail (cpu_stop_fn_t fn,void *data,const struct cpumask *cpus); +/** * stop_machine_cpuslocked: freeze the machine on all CPUs and run this function * @fn: the function to run
@@ -124,6 +127,9 @@ int stop_machine(cpu_stop_fn_t fn, void*/ int stop_machine_cpuslocked (cpu_stop_fn_t fn,void *data,const struct cpumask *cpus); + +int stop_machine_cpuslocked_fail (cpu_stop_fn_t fn,void *data,const struct cpumask *cpus); +/** * stop_core_cpuslocked: - stop all threads on just one core * @cpu: any cpu in the targeted core
kernel/stop_machine.c +25 -0
@@ -22,6 +22,7 @@#include <linux/atomic.h> #include <linux/nmi.h> #include <linux/sched/wake_q.h> +#include <linux/sched/isolation.h> /* * Structure to determine completion condition and record errors. May
@@ -619,6 +620,17 @@ int stop_machine_cpuslocked(cpu_stop_fn_return stop_cpus (cpu_online_mask, multi_cpu_stop, &msdata); } +int stop_machine_cpuslocked_fail (cpu_stop_fn_t fn,void *data, +const struct cpumask *cpus) +{ +block_interf_assert_held (); + +if (cpumask_intersects (block_interf_cpumask, cpu_online_mask)) +return -EPERM ; + +return stop_machine_cpuslocked (fn, data, cpus); +} +int stop_machine (cpu_stop_fn_t fn,void *data,const struct cpumask *cpus) {int ret;
@@ -631,6 +643,19 @@ int stop_machine(cpu_stop_fn_t fn, void }EXPORT_SYMBOL_GPL (stop_machine); +int stop_machine_fail (cpu_stop_fn_t fn,void *data,const struct cpumask *cpus) +{ +int ret; + +/* No CPUs can come up or down during this. */ +cpus_read_lock (); + ret =stop_machine_cpuslocked_fail (fn, data, cpus); +cpus_read_unlock (); +return ret; +} +EXPORT_SYMBOL_GPL (stop_machine_fail); + +#ifdef CONFIG_SCHED_SMT int stop_core_cpuslocked (unsigned int cpu,cpu_stop_fn_t fn,void *data) {
RFC PATCH 4/7 clockevent unbind: use smp_call_func_single_fail
In reply to Marcelo TosattiConvert clockevents_unbind from smp_call_function_single to smp_call_func_single_fail, which will fail in case the target CPU is tagged as block interference CPU.
kernel/time/clockevents.c +7 -1
@@ -13,6 +13,7 @@#include <linux/module.h> #include <linux/smp.h> #include <linux/device.h> +#include <linux/sched/isolation.h> #include "tick-internal.h"
@@ -416,9 +417,14 @@ static void __clockevents_unbind(void *a*/ static int clockevents_unbind (struct clock_event_device *ced,int cpu) { +int ret;struct ce_unbind cu = { .ce = ced, .res = -ENODEV }; -smp_call_function_single (cpu, __clockevents_unbind, &cu,1 ); +block_interf_read_lock (); + ret =smp_call_func_single_fail (cpu, __clockevents_unbind, &cu,1 ); +block_interf_read_unlock (); +if (ret) +return ret;return cu.res; }
RFC PATCH 2/7 introduce smp_call_func_single_fail
In reply to Marcelo TosattiIntroduce smp_call_func_single_fail, which checks if the target CPU is tagged as a "block interference" CPU, and returns an error if so.
include/linux/smp.h +3 -0
@@ -50,6 +50,9 @@ extern unsigned int total_cpus;int smp_call_function_single (int cpuid,smp_call_func_t func,void *info,int wait); +int smp_call_func_single_fail (int cpuid,smp_call_func_t func,void *info, +int wait); +void on_each_cpu_cond_mask (smp_cond_func_t cond_func,smp_call_func_t func,void *info,bool wait,const struct cpumask *mask);
kernel/smp.c +25 -0
@@ -25,6 +25,7 @@#include <linux/nmi.h> #include <linux/sched/debug.h> #include <linux/jump_label.h> +#include <linux/sched/isolation.h> #include "smpboot.h" #include "sched/smp.h"
@@ -782,6 +783,30 @@ int smp_call_function_single(int cpu, sm }EXPORT_SYMBOL (smp_call_function_single); +/* +* smp_call_func_single_fail - Run a function on a specific CPU, +* failing if any target CPU is marked as "no ipi". +* @func: The function to run. This must be fast and non-blocking. +* @info: An arbitrary pointer to pass to the function. +* @wait: If true, wait until function has completed on other CPUs. +* +* Returns 0 on success, else a negative status code. +*/ +int smp_call_func_single_fail (int cpu,smp_call_func_t func,void *info, +int wait) +{ +int err; + +block_interf_assert_held (); +if (block_interf_cpu (cpu)) +return -EPERM ; + + err =smp_call_function_single (cpu, func, info, wait); + +return err; +} +EXPORT_SYMBOL (smp_call_func_single_fail); +/** * smp_call_function_single_async() - Run an asynchronous function on a * specific CPU.
RFC PATCH 7/7 mtrr_add_page/mtrr_del_page: check for block interference CPUs
In reply to Marcelo TosattiCheck if any online CPU in the system is tagged as a block interference CPU, and if so return an error to mtrr_add_page/mtrr_del_page.
This can avoid interference to such CPUs (while allowing userspace to handle the failures).
arch/x86/kernel/cpu/mtrr/mtrr.c +15 -0
@@ -45,6 +45,7 @@#include <linux/smp.h> #include <linux/syscore_ops.h> #include <linux/rcupdate.h> +#include <linux/sched/isolation.h> #include <asm/cpufeature.h> #include <asm/e820/api.h>
@@ -335,6 +336,13 @@ int mtrr_add_page(unsigned long base, un error = -EINVAL ; replace =-1 ; +block_interf_read_lock (); + +if (cpumask_intersects (block_interf_cpumask, cpu_online_mask)) { +block_interf_read_unlock (); +return -EPERM ; + } +/* No CPU hotplug when we change MTRR entries */ cpus_read_lock ();
@@ -399,6 +407,7 @@ int mtrr_add_page(unsigned long base, un out:mutex_unlock (&mtrr_mutex);cpus_read_unlock (); +block_interf_read_unlock ();return error; }
@@ -484,6 +493,11 @@ int mtrr_del_page(int reg, unsigned longreturn -ENODEV ; max = num_var_ranges; +block_interf_read_lock (); +if (cpumask_intersects (block_interf_cpumask, cpu_online_mask)) { +block_interf_read_unlock (); +return -EPERM ; + }/* No CPU hotplug when we change MTRR entries */ cpus_read_lock ();mutex_lock (&mtrr_mutex);
@@ -521,6 +535,7 @@ int mtrr_del_page(int reg, unsigned long out:mutex_unlock (&mtrr_mutex);cpus_read_unlock (); +block_interf_read_unlock ();return error; }
RFC PATCH 6/7 perf_event_open: check for block interference CPUs
In reply to Marcelo TosattiWhen creating perf events, return an error rather than interfering with CPUs tagged as block interference.
Note: this patch is incomplete, installation of perf context on block interference CPUs via task context is not performed.
kernel/events/core.c +25 -0
@@ -54,6 +54,7 @@#include <linux/highmem.h> #include <linux/pgtable.h> #include <linux/buildid.h> +#include <linux/sched/isolation.h> #include "internal.h"
@@ -12391,6 +12392,26 @@ not_move_group:WARN_ON_ONCE (ctx->parent_ctx); +block_interf_read_lock (); +if (!task) { +if (move_group) { +for_each_sibling_event (sibling, group_leader) { +if (block_interf_cpu (sibling->cpu)) { + err = -EPERM ; + goto err_block_interf; + } + } +if (block_interf_cpu (group_leader->cpu)) { + err = -EPERM ; + goto err_block_interf; + } + } +if (block_interf_cpu (event->cpu)) { + err = -EPERM ; + goto err_block_interf; + } + } +/* * This is the point on no return; we cannot fail hereafter. This is * where we start modifying current state.
@@ -12464,6 +12485,8 @@ not_move_group:put_task_struct (task); } +block_interf_read_unlock (); +mutex_lock (¤t->perf_event_mutex);list_add_tail (&event->owner_entry, ¤t->perf_event_list);mutex_unlock (¤t->perf_event_mutex);
@@ -12478,6 +12501,8 @@ not_move_group:fd_install (event_fd, event_file);return event_fd; +err_block_interf: +block_interf_read_unlock (); err_locked:if (move_group)perf_event_ctx_unlock (group_leader, gctx);
RFC PATCH 1/7 cpu isolation: basic block interference infrastructure
In reply to Marcelo TosattiThere are a number of codepaths in the kernel that interrupt code execution in remote CPUs. A subset of such codepaths are triggered from userspace and can therefore return errors.
Introduce a cpumask named "block interference", writable from userspace.
This cpumask (and associated helpers) can be used by code that executes code on remote CPUs to optionally return an error.
include/linux/sched/isolation.h +29 -0
@@ -58,4 +58,33 @@ static inline bool housekeeping_cpu(int return true; } +#ifdef CONFIG_CPU_ISOLATION +extern cpumask_var_t block_interf_cpumask; +extern bool block_interf_cpumask_active; + +void block_interf_read_lock (void ); +void block_interf_read_unlock (void ); + +void block_interf_write_lock (void ); +void block_interf_write_unlock (void ); + +void block_interf_assert_held (void ); + +#else +static inline void block_interf_read_lock (void ) { } +static inline void block_interf_read_unlock (void ) { } +static inline void block_interf_write_lock (void ) { } +static inline void block_interf_write_unlock (void ) { } +static inline void block_interf_assert_held (void ) { } +#endif + +static inline bool block_interf_cpu (int cpu) +{ +#ifdef CONFIG_CPU_ISOLATION +if (block_interf_cpumask_active) +return cpumask_test_cpu (cpu, block_interf_cpumask); +#endif +return false; +} +#endif /* _LINUX_SCHED_ISOLATION_H */
kernel/sched/isolation.c +113 -0
116 lines not on this page · raw message
RFC PATCH 5/7 timekeeping_notify: use stop_machine_fail when appropriate
In reply to Marcelo TosattiChange timekeeping_notify to use stop_machine_fail when appropriate, which will fail in case the target CPU is tagged as block interference CPU.
include/linux/clocksource.h +1 -1
@@ -267,7 +267,7 @@ extern void clocksource_arch_init(structstatic inline void clocksource_arch_init (struct clocksource *cs) { }#endif -extern int timekeeping_notify (struct clocksource *clock); +extern int timekeeping_notify (struct clocksource *clock,bool fail);extern u64 clocksource_mmio_readl_up (struct clocksource *);extern u64 clocksource_mmio_readl_down (struct clocksource *);
kernel/time/clocksource.c +26 -16
124 lines not on this page · raw message
kernel/time/timekeeping.c +14 -2
@@ -13,6 +13,7 @@#include <linux/sched.h> #include <linux/sched/loadavg.h> #include <linux/sched/clock.h> +#include <linux/sched/isolation.h> #include <linux/syscore_ops.h> #include <linux/clocksource.h> #include <linux/jiffies.h>
@@ -1497,13 +1498,24 @@ static int change_clocksource(void *data* This function is called from clocksource.c after a new, better clock * source has been registered. The caller holds the clocksource_mutex. */ -int timekeeping_notify (struct clocksource *clock) +int timekeeping_notify (struct clocksource *clock,bool fail) {struct timekeeper *tk = &tk_core.timekeeper;if (tk->tkr_mono.clock == clock)return 0 ; -stop_machine (change_clocksource, clock,NULL ); + +if (!fail) +stop_machine (change_clocksource, clock,NULL ); +else { +int ret; + +block_interf_read_lock (); + ret =stop_machine_fail (change_clocksource, clock,NULL ); +block_interf_read_unlock (); +if (ret) +return ret; + }tick_clock_notify ();return tk->tkr_mono.clock == clock ?0 :-1 ; }
cover cpu isolation: infra to block interference to select CPUs
There are a number of codepaths in the kernel that interrupt code execution in remote CPUs. A subset of such codepaths are triggered from userspace and can therefore return errors.
Introduce a cpumask named "block interference", writable from userspace.
This cpumask (and associated helpers) can be used by code that executes code on remote CPUs to optionally return an error.
Note: the word "interference" has been chosen since "interruption" is often confused with "device interrupt".
To protect readers VS writers of this cpumask, a per-CPU read-write semaphore is used. This is acceptable since the codepaths which trigger such interferences are not (or should not be) hot.
What is proposed is to incrementally modify code that can return errors in two ways:
1) Introduction of fail variants of the functions that generate code execution on remote CPUs. This way the modified code should look like:
block_interf_read_lock();
ret = smp_call_func_single_fail() / stop_machine_fail() / ...
block_interf_read_unlock();This is grep friendly (so one can search for smp_call_function_* variants) and re-uses code.
2) Usage of block interference CPU mask helpers. For certain users of smp_call_func_*, stop_machine_* functions it is natural to check for block interference CPUs before calling the functions for remote code execution.
For example if its not desirable to perform error handling at smp_call_func_* time, or if performing the error handling requires unjustified complexity. Then:
block_interf_read_lock();
if target cpumask intersects with block interference cpumask {
block_interf_read_unlock();
return error
}
...
ret = smp_call_function_single / stop_machine() / ...
...
block_interf_read_unlock();Regarding housekeeping flags, it is usually the case that initialization might require code execution on interference blocked CPUs (for example MTRR initialization, resctrlfs initialization, MSR writes, ...). Therefore tagging the CPUs after system initialization is necessary, which is not possible with current housekeeping flags infrastructure.
This patchset converts clockevent and clocksource unbind, perf_event_open system call, and /proc/mtrr writes, but there are several more users to convert (eg: MSR reads/writes, resctrlfs, etc).
Sending this as an RFC to know whether folks think this is the right direction.