optionally perform deferred actions on return to userspace
Subsystems (5)
cover optionally perform deferred actions on return to userspace
Changelog:
-v2: fix !CONFIG_SMP breakage (kernel robot)
switch option to generic "quiesce_on_exit_to_usermode"
Summary of what was discussed on -v1:
1) The additional hooks to performance sensitive callbacks in mm/vmstat.c are protected by a static key, therefore workloads which do not enable this should not be impacted.
2) People would prefer the prctl() interface, but as noted in the option documentation (patch 1), the code added by this patchset should be reused by the prctl() interface, and the isolcpus option can then be deprecated.
3) Nobody has any other bright ideas for ways to solve this that would make this patch series obsolete.
4) The isolcpus= interface should switch to a cpuset based interface.
---
The logic to disable vmstat worker thread, when entering nohz full, does not cover all scenarios. For example, it is possible for the following to happen:
1) enter nohz_full, which calls refresh_cpu_vm_stats, syncing the stats.
2) app runs mlock, which increases counters for mlock'ed pages.
3) start -RT loop
Since refresh_cpu_vm_stats from nohz_full logic can happen _before_ the mlock, vmstat shepherd can restart vmstat worker thread on the CPU in question.
To fix this, optionally quiesce deferred actions when returning to userspace, controllable by a new "quiesce_on_exit_to_usermode" isolcpus flag (default off).
See individual patches for details.
PATCH 1/5 sched: isolation: introduce quiesce_on_exit_to_usermode isolcpu flags
Add a new isolcpus flag "quiesce_on_exit_to_usermode" to enable quiescing of deferred actions on return to userspace.
include/linux/sched/isolation.h +1 -0
@@ -15,6 +15,7 @@ enum hk_flags {HK_FLAG_WQ = (1 <<6 ),HK_FLAG_MANAGED_IRQ = (1 <<7 ),HK_FLAG_KTHREAD = (1 <<8 ), + HK_FLAG_QUIESCE_URET = (1 << 9), };#ifdef CONFIG_CPU_ISOLATION
kernel/sched/isolation.c +6 -0
@@ -173,6 +173,12 @@ static int __init housekeeping_isolcpus_continue ; } +if (!strncmp (str,"quiesce_on_exit_to_usermode," ,28 )) { + str +=28 ; + flags |=HK_FLAG_QUIESCE_URET ; +continue ; + } +/* * Skip unknown sub-parameter and validate that it is not * containing an invalid character.
Documentation/admin-guide/kernel-parameters.txt +37 -0
@@ -2124,6 +2124,43 @@ The format of <cpu-list> is described above. + quiesce_on_exit_to_usermode + + This flag allows userspace to take preventive measures to + avoid deferred actions and create a OS noise free environment for + the application, by quiescing such activities on + return from syscalls (that is, perform the necessary + background work on return to userspace, rather than allowing + it to happen when userspace is executing, in the form of + an interruption to the application). + + There might be a performance degradation from using this, + on systemcall heavy workloads, for the isolated CPUs. + This option is intended to be used by specialized workloads. + + It should be deprecated in favour of a prctl() interface + to enable this mode (which allows the quiescing to take + place only on select sections of userspace execution, namely + the latency sensitive loops). + + Note: one of the preventive measures this option + enables is the following. + + Page counters are maintained in per-CPU counters to + improve performance. When a CPU modifies a page counter, + this modification is kept in the per-CPU counter. + Certain activities require a global count, which + involves requesting each CPU to flush its local counters + to the global VM counters. + This flush is implemented via a workqueue item, which + requires scheduling the workqueue task on isolated CPUs. + + To avoid this interruption, quiesce_on_exit_to_usermode + syncs the page counters on each return from system calls. + To ensure the application returns to userspace + with no modified per-CPU counters, its necessary to + use mlockall() in addition to this isolcpus flag. + iucv= [HW,NET] ivrs_ioapic [HW,X86-64]
PATCH 2/5 common entry: add hook for isolation to __syscall_exit_to_user_mode_work
In reply to Marcelo TosattiThis hook will be used by the next patch to perform synchronization of per-CPU vmstats.
kernel/entry/common.c +9 -0
@@ -284,9 +284,18 @@ static void syscall_exit_to_user_mode_prsyscall_exit_work (regs, work); } +/* +* Isolaton specific exit to user mode preparation. Runs with interrupts +* enabled. +*/ +static void isolation_exit_to_user_mode_prepare (void ) +{ +} + static __always_inline void __syscall_exit_to_user_mode_work(struct pt_regs *regs) {syscall_exit_to_user_mode_prepare (regs); +isolation_exit_to_user_mode_prepare ();local_irq_disable_exit_to_user ();exit_to_user_mode_prepare (regs); }
PATCH 3/5 mm: vmstat: optionally flush per-CPU vmstat counters on return to userspace
In reply to Marcelo TosattiThe logic to disable vmstat worker thread, when entering nohz full, does not cover all scenarios. For example, it is possible for the following to happen:
1) enter nohz_full, which calls refresh_cpu_vm_stats, syncing the stats.
2) app runs mlock, which increases counters for mlock'ed pages.
3) start -RT loop
Since refresh_cpu_vm_stats from nohz_full logic can happen _before_ the mlock, vmstat shepherd can restart vmstat worker thread on the CPU in question.
To fix this, optionally sync the vmstat counters when returning from userspace, controllable by a new "quiesce_on_exit_to_usermode" isolcpus flags (default off).
kernel/sched/isolation.c +6 -0
@@ -8,6 +8,7 @@* */ #include "sched.h" +#include <linux/vmstat.h> DEFINE_STATIC_KEY_FALSE (housekeeping_overridden);EXPORT_SYMBOL_GPL (housekeeping_overridden);
@@ -129,6 +130,11 @@ static int __init housekeeping_setup(cha } } +#ifdef CONFIG_SMP +if (flags &HK_FLAG_QUIESCE_URET ) +static_branch_enable (&vmstat_sync_enabled); +#endif + housekeeping_flags |= flags;free_bootmem_cpumask_var (non_housekeeping_mask);
include/linux/vmstat.h +17 -0
@@ -21,6 +21,23 @@ int sysctl_vm_numa_stat_handler(struct c void *buffer, size_t *length, loff_t *ppos);#endif +#ifdef CONFIG_SMP +DECLARE_STATIC_KEY_FALSE (vmstat_sync_enabled); + +extern void __sync_vmstat (void ); +static inline void sync_vmstat (void ) +{ +if (static_branch_unlikely (&vmstat_sync_enabled)) +__sync_vmstat (); +} +#else + +static inline void sync_vmstat (void ) +{ +} + +#endif +struct reclaim_stat {unsigned nr_dirty;unsigned nr_unqueued_dirty;
mm/vmstat.c +53 -0
119 lines not on this page · raw message
kernel/entry/common.c +2 -0
@@ -6,6 +6,7 @@#include <linux/livepatch.h> #include <linux/audit.h> #include <linux/tick.h> +#include <linux/vmstat.h> #include "common.h"
@@ -290,6 +291,7 @@ static void syscall_exit_to_user_mode_pr*/ static void isolation_exit_to_user_mode_prepare (void ) { +sync_vmstat (); } static __always_inline void __syscall_exit_to_user_mode_work(struct pt_regs *regs)
Re: [patch 3/5] mm: vmstat: optionally flush per-CPU vmstat counters on return to userspace
mm/vmstat.c void set_pgdat_percpu_threshold(pg_data_ · line 328
Quoted 11 lines · Marcelo Tosatti
mm/vmstat.c +11
-0+ +if (!static_branch_unlikely (&vmstat_sync_enabled)) +return ; + + cpu =smp_processor_id (); + +if (housekeeping_cpu (cpu,HK_FLAG_QUIESCE_URET )) +return ; + +per_cpu (vmstat_dirty,smp_processor_id ()) = true; +}
And you are going to insert this into all the performance critical VM statistics handling. Inline?
And why do you need to do such things as to determine the processor? At mininum do this using this cpu operations like the vmstat functions currently do. And, lucky us, now we also have
more issues why we should disable preemption etc etc while handling vm counters.
Re: [patch 3/5] mm: vmstat: optionally flush per-CPU vmstat counters on return to userspace
Quoted 16 lines · Christoph Lameter
mm/vmstat.c void set_pgdat_percpu_threshold(pg_data_ · line 328
Quoted 11 lines · Marcelo Tosatti
mm/vmstat.c +11
-0+ +if (!static_branch_unlikely (&vmstat_sync_enabled)) +return ; + + cpu =smp_processor_id (); + +if (housekeeping_cpu (cpu,HK_FLAG_QUIESCE_URET )) +return ; + +per_cpu (vmstat_dirty,smp_processor_id ()) = true; +}And you are going to insert this into all the performance critical VM statistics handling. Inline?
Yes, this is what the patch below is supposed to do (maybe it missed some statistics?).
The alternative would be some equivalent of need_update on return to userspace (for all system call returns) (when the HK_FLAG_QUIESCE_URET flag is enabled).
Quoted 3 lines
And why do you need to do such things as to determine the processor? At mininum do this using this cpu operations like the vmstat functions currently do.
OK, will do that and resend.
Quoted 3 lines
And, lucky us, now we also have
more issues why we should disable preemption etc etc while handling vm counters.
Re: [patch 3/5] mm: vmstat: optionally flush per-CPU vmstat counters on return to userspace
In reply to Christoph LameterQuoted 17 lines · Christoph Lameter
Review
Quoted 11 lines · Marcelo Tosatti
(quoted diff) +11
-0+ + if (!static_branch_unlikely(&vmstat_sync_enabled)) + return; + + cpu = smp_processor_id(); + + if (housekeeping_cpu(cpu, HK_FLAG_QUIESCE_URET)) + return; + + per_cpu(vmstat_dirty, smp_processor_id()) = true; +}And you are going to insert this into all the performance critical VM statistics handling. Inline?
Quoted 5 lines
And why do you need to do such things as to determine the processor? At mininum do this using this cpu operations like the vmstat functions currently do. And, lucky us, now we also have
more issues why we should disable preemption etc etc while handling vm counters.
OK, hopefully this is what you mean.
Any other comments?
kernel/sched/isolation.c +6 -0
@@ -8,6 +8,7 @@* */ #include "sched.h" +#include <linux/vmstat.h> DEFINE_STATIC_KEY_FALSE (housekeeping_overridden);EXPORT_SYMBOL_GPL (housekeeping_overridden);
@@ -129,6 +130,11 @@ static int __init housekeeping_setup(cha } } +#ifdef CONFIG_SMP +if (flags &HK_FLAG_QUIESCE_URET ) +static_branch_enable (&vmstat_sync_enabled); +#endif + housekeeping_flags |= flags;free_bootmem_cpumask_var (non_housekeeping_mask);
include/linux/vmstat.h +17 -0
@@ -21,6 +21,23 @@ int sysctl_vm_numa_stat_handler(struct c void *buffer, size_t *length, loff_t *ppos);#endif +#ifdef CONFIG_SMP +DECLARE_STATIC_KEY_FALSE (vmstat_sync_enabled); + +extern void __sync_vmstat (void ); +static inline void sync_vmstat (void ) +{ +if (static_branch_unlikely (&vmstat_sync_enabled)) +__sync_vmstat (); +} +#else + +static inline void sync_vmstat (void ) +{ +} + +#endif +struct reclaim_stat {unsigned nr_dirty;unsigned nr_unqueued_dirty;
mm/vmstat.c +46 -0
112 lines not on this page · raw message
kernel/entry/common.c +2 -0
@@ -6,6 +6,7 @@#include <linux/livepatch.h> #include <linux/audit.h> #include <linux/tick.h> +#include <linux/vmstat.h> #include "common.h"
@@ -290,6 +291,7 @@ static void syscall_exit_to_user_mode_pr*/ static void isolation_exit_to_user_mode_prepare (void ) { +sync_vmstat (); } static __always_inline void __syscall_exit_to_user_mode_work(struct pt_regs *regs)
PATCH 4/5 mm: vmstat: move need_update
In reply to Marcelo TosattiMove need_update() function up in vmstat.c, needed by next patch. No code changes.
mm/vmstat.c +34 -34
@@ -1860,6 +1860,40 @@ static const struct seq_operations vmsta static DEFINE_PER_CPU(struct delayed_work, vmstat_work);int sysctl_stat_interval__read_mostly =HZ ; +/* +* Check if the diffs for a certain cpu indicate that +* an update is needed. +*/ +static bool need_update (int cpu) +{ +pg_data_t *last_pgdat =NULL ; +struct zone *zone; + +for_each_populated_zone (zone) { +struct per_cpu_pageset *p =per_cpu_ptr (zone->pageset, cpu); +struct per_cpu_nodestat *n; +/* +* The fast way of checking if there are any vmstat diffs. +*/ +if (memchr_inv (p->vm_stat_diff,0 ,NR_VM_ZONE_STAT_ITEMS * +sizeof (p->vm_stat_diff[0 ]))) +return true; +#ifdef CONFIG_NUMA +if (memchr_inv (p->vm_numa_stat_diff,0 ,NR_VM_NUMA_STAT_ITEMS * +sizeof (p->vm_numa_stat_diff[0 ]))) +return true; +#endif +if (last_pgdat == zone->zone_pgdat) +continue ; + last_pgdat = zone->zone_pgdat; + n =per_cpu_ptr (zone->zone_pgdat->per_cpu_nodestats, cpu); +if (memchr_inv (n->vm_node_stat_diff,0 ,NR_VM_NODE_STAT_ITEMS * +sizeof (n->vm_node_stat_diff[0 ]))) +return true; + } +return false; +} +#ifdef CONFIG_PROC_FS static void refresh_vm_stats (struct work_struct *work) {
@@ -1945,40 +1979,6 @@ static void vmstat_update(struct work_st* invoked when tick processing is not active. */ /* -* Check if the diffs for a certain cpu indicate that -* an update is needed. -*/ -static bool need_update (int cpu) -{ -pg_data_t *last_pgdat =NULL ; -struct zone *zone; - -for_each_populated_zone (zone) { -struct per_cpu_pageset *p =per_cpu_ptr (zone->pageset, cpu); -struct per_cpu_nodestat *n; -/* -* The fast way of checking if there are any vmstat diffs. -*/ -if (memchr_inv (p->vm_stat_diff,0 ,NR_VM_ZONE_STAT_ITEMS * -sizeof (p->vm_stat_diff[0 ]))) -return true; -#ifdef CONFIG_NUMA -if (memchr_inv (p->vm_numa_stat_diff,0 ,NR_VM_NUMA_STAT_ITEMS * -sizeof (p->vm_numa_stat_diff[0 ]))) -return true; -#endif -if (last_pgdat == zone->zone_pgdat) -continue ; - last_pgdat = zone->zone_pgdat; - n =per_cpu_ptr (zone->zone_pgdat->per_cpu_nodestats, cpu); -if (memchr_inv (n->vm_node_stat_diff,0 ,NR_VM_NODE_STAT_ITEMS * -sizeof (n->vm_node_stat_diff[0 ]))) -return true; - } -return false; -} - -/* * Switch off vmstat processing and then fold all the remaining differentials * until the diffs stay at zero. The function is used by NOHZ and can only be * invoked when tick processing is not active.
PATCH 5/5 mm: vmstat_refresh: avoid queueing work item if cpu stats are clean
In reply to Marcelo TosattiIt is not necessary to queue work item to run refresh_vm_stats on a remote CPU if that CPU has no dirty stats and no per-CPU allocations for remote nodes.
This fixes sosreport hang (which uses vmstat_refresh) with spinning SCHED_FIFO process.
mm/vmstat.c +34 -6
@@ -1895,17 +1895,39 @@ static bool need_update(int cpu) }#ifdef CONFIG_PROC_FS -static void refresh_vm_stats (struct work_struct *work) +static bool need_drain_remote_zones (int cpu) +{ +struct zone *zone; + +for_each_populated_zone (zone) { +struct per_cpu_pageset *p; + + p =per_cpu_ptr (zone->pageset, cpu); + +if (!p->pcp.count) +continue ; +if (!p->expire) +continue ; +if (zone_to_nid (zone) ==cpu_to_node (cpu)) +continue ; + +return true; + } + +return false; +} + +static long refresh_vm_stats (void *arg) {refresh_cpu_vm_stats (true); +return 0 ; }int vmstat_refresh (struct ctl_table *table,int write,void *buffer,size_t *lenp,loff_t *ppos) {long val; -int err; -int i; +int i, cpu;/* * The regular update, every sysctl_stat_interval, may come later
@@ -1919,9 +1941,15 @@ int vmstat_refresh(struct ctl_table *tab* transiently negative values, report an error here if any of * the stats is negative, so we know to go looking for imbalance. */ - err =schedule_on_each_cpu (refresh_vm_stats); -if (err) -return err; +get_online_cpus (); +for_each_online_cpu (cpu) { +if (need_update (cpu) ||need_drain_remote_zones (cpu)) +work_on_cpu (cpu, refresh_vm_stats,NULL ); + +cond_resched (); + } +put_online_cpus (); +for (i =0 ; i <NR_VM_ZONE_STAT_ITEMS ; i++) {/* * Skip checking stats known to go negative occasionally.
kernel test robot
build botRe: [patch 5/5] mm: vmstat_refresh: avoid queueing work item if cpu stats are clean
Thank you for the patch! Yet something to improve:
Hi Marcelo,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on tip/sched/core]
[also build test ERROR on v5.13]
[cannot apply to hnaz-linux-mm/master tip/master linus/master tip/core/entry next-20210709] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Marcelo-Tosatti/optionally-perform-deferred-actions-on-return-to-userspace/20210710-021217 base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 031e3bd8986fffe31e1ddbf5264cccfe30c9abd7 config: arm64-randconfig-r012-20210709 (attached as .config) compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 8d69635ed9ecf36fd0ca85906bfde17949671cbe)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/91934f6f1fd6271ef8af8ec384868558014e371b
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Marcelo-Tosatti/optionally-perform-deferred-actions-on-return-to-userspace/20210710-021217 git checkout 91934f6f1fd6271ef8af8ec384868558014e371b
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross O=build_dir ARCH=arm64 SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
>> mm/vmstat.c:1909:11: error: no member named 'expire' in 'struct per_cpu_pageset'
if (!p->expire)
~ ^1 error generated.
vim +1909 mm/vmstat.c
1896
1897 #ifdef CONFIG_PROC_FS
1898 static bool need_drain_remote_zones(int cpu)
1899 {
1900 struct zone *zone;
1901
1902 for_each_populated_zone(zone) {
1903 struct per_cpu_pageset *p;
1904
1905 p = per_cpu_ptr(zone->pageset, cpu);
1906
1907 if (!p->pcp.count)
1908 continue;
> 1909 if (!p->expire)
1910 continue;
1911 if (zone_to_nid(zone) == cpu_to_node(cpu))
1912 continue;
1913
1914 return true;
1915 }
1916
1917 return false;
1918 }
1919
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org