Ensure quiet_vmstat() is called when returning to userpace and when idle tick is stopped
Subsystems (3)
PATCH v11 5/6 tick/sched: Ensure quiet_vmstat() is called when the idle tick was stopped too
In reply to Marcelo TosattiFrom: Aaron Tomlin <[email protected]>
In the context of the idle task and an adaptive-tick mode/or a nohz_full CPU, quiet_vmstat() can be called: before stopping the idle tick, entering an idle state and on exit. In particular, for the latter case, when the idle task is required to reschedule, the idle tick can remain stopped and the timer expiration time endless i.e., KTIME_MAX. Now, indeed before a nohz_full CPU enters an idle state, CPU-specific vmstat counters should be processed to ensure the respective values have been reset and folded into the zone specific 'vm_stat[]'. That being said, it can only occur when: the idle tick was previously stopped, and reprogramming of the timer is not required.
A customer provided some evidence which indicates that the idle tick was stopped; albeit, CPU-specific vmstat counters still remained populated. Thus one can only assume quiet_vmstat() was not invoked on return to the idle loop.
If I understand correctly, I suspect this divergence might erroneously prevent a reclaim attempt by kswapd. If the number of zone specific free pages are below their per-cpu drift value then zone_page_state_snapshot() is used to compute a more accurate view of the aforementioned statistic. Thus any task blocked on the NUMA node specific pfmemalloc_wait queue will be unable to make significant progress via direct reclaim unless it is killed after being woken up by kswapd (see throttle_direct_reclaim()).
Consider the following theoretical scenario:
- Note: CPU X is part of 'tick_nohz_full_mask'
1. CPU Y migrated running task A to CPU X that
was in an idle state i.e. waiting for an IRQ; marked the current task on CPU X to need/or require a reschedule i.e., set TIF_NEED_RESCHED and invoked a reschedule IPI to CPU X (see sched_move_task())
2. CPU X acknowledged the reschedule IPI. Generic
idle loop code noticed the TIF_NEED_RESCHED flag against the idle task and attempts to exit of the loop and calls the main scheduler function i.e. __schedule().
Since the idle tick was previously stopped no scheduling-clock tick would occur. So, no deferred timers would be handled
3. Post transition to kernel execution Task A
running on CPU X, indirectly released a few pages (e.g. see __free_one_page()); CPU X's 'vm_stat_diff[NR_FREE_PAGES]' was updated and zone specific 'vm_stat[]' update was deferred as per the CPU-specific stat threshold
4. Task A does invoke exit(2) and the kernel does
remove the task from the run-queue; the idle task was selected to execute next since there are no other runnable tasks assigned to the given CPU (see pick_next_task() and pick_next_task_idle())
5. On return to the idle loop since the idle tick
was already stopped and can remain so (see [1] below) e.g. no pending soft IRQs, no attempt is made to zero and fold CPU X's vmstat counters since reprogramming of the scheduling-clock tick is not required/or needed (see [2])
...
do_idle
{
__current_set_polling()
tick_nohz_idle_enter()
while (!need_resched()) {
local_irq_disable()
...
/* No polling or broadcast event */ cpuidle_idle_call()
{
if (cpuidle_not_available(drv, dev)) {
tick_nohz_idle_stop_tick()
__tick_nohz_idle_stop_tick(this_cpu_ptr(&tick_cpu_sched)) {
int cpu = smp_processor_id()
if (ts->timer_expires_base)
expires = ts->timer_expires
else if (can_stop_idle_tick(cpu, ts))
(1) -------> expires = tick_nohz_next_event(ts, cpu)
else
return
ts->idle_calls++
if (expires > 0LL) {
tick_nohz_stop_tick(ts, cpu) {
if (ts->tick_stopped && (expires == ts->next_tick)) {
(2) -------> if (tick == KTIME_MAX || ts->next_tick ==
hrtimer_get_expires(&ts->sched_timer)) return
}
...
}
So, the idea of this patch is to ensure refresh_cpu_vm_stats(false) is called, when it is appropriate, on return to the idle loop if the idle tick was previously stopped too.
A trivial test program was used to determine the impact of the proposed changes and under vanilla. The nanosleep(2) system call was used several times to suspend execution for a period of time to approximately compute the number of CPU-cycles in the idle code path. The following is an average count of CPU-cycles:
Vanilla Modified
Cycles per idle loop 151858 153258 (+1.0%)
--- kernel/time/tick-sched.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/time/tick-sched.c +2 -1
@@ -928,13 +928,14 @@ static void tick_nohz_stop_tick(struct t*/ if (!ts->tick_stopped) {calc_load_nohz_start (); -quiet_vmstat (false); ts->last_tick =hrtimer_get_expires (&ts->sched_timer); ts->tick_stopped =1 ;trace_tick_stop (1 ,TICK_DEP_MASK_NONE ); } +/* Attempt to fold when the idle tick is stopped or not */ +quiet_vmstat (false); ts->next_tick = tick;/*
PATCH v11 3/6 mm/vmstat: manage per-CPU stats from CPU context when NOHZ full
In reply to Marcelo TosattiFor nohz full CPUs, we'd like the per-CPU vm statistics to be synchronized when userspace is executing. Otherwise, the vmstat_shepherd might queue a work item to synchronize them, which is undesired intereference for isolated CPUs.
This means that its necessary to check for, and possibly sync, the statistics when returning to userspace. This means that there are now two execution contexes, on different CPUs, which require awareness about each other: context switch and vmstat shepherd kernel threadr.
To avoid the shared variables between these two contexes (which would require atomic accesses), delegate the responsability of statistics synchronization from vmstat_shepherd to local CPU context, for nohz_full CPUs.
Do that by queueing a delayed work when marking per-CPU vmstat dirty.
When returning to userspace, fold the stats and cancel the delayed work.
When entering idle, only fold the stats.
--- include/linux/vmstat.h | 4 ++-- kernel/time/tick-sched.c | 2 +- mm/vmstat.c | 41 ++++++++++++++++++++++++++++++++--------- 3 files changed, 35 insertions(+), 12 deletions(-)
mm/vmstat.c +79 -14
@@ -28,6 +28,7 @@#include <linux/mm_inline.h> #include <linux/page_ext.h> #include <linux/page_owner.h> +#include <linux/tick.h> #include "internal.h"
@@ -194,21 +195,50 @@ void fold_vm_numa_events(void)#endif #ifdef CONFIG_SMP -static DEFINE_PER_CPU_ALIGNED(bool, vmstat_dirty); + +struct vmstat_dirty { +bool dirty; +bool cpuhotplug; +}; + +static DEFINE_PER_CPU_ALIGNED(struct vmstat_dirty, vmstat_dirty_pcpu); +static DEFINE_PER_CPU(struct delayed_work, vmstat_work); +int sysctl_stat_interval__read_mostly =HZ ;static inline void vmstat_mark_dirty (void ) { -this_cpu_write (vmstat_dirty, true); +struct vmstat_dirty *vms =this_cpu_ptr (&vmstat_dirty_pcpu); + +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER +int cpu =smp_processor_id (); + +if (tick_nohz_full_cpu (cpu) && !vms->dirty) { +struct delayed_work *dw; + + dw =this_cpu_ptr (&vmstat_work); +if (!delayed_work_pending (dw) && !vms->cpuhotplug) { +unsigned long delay; + + delay =round_jiffies_relative (sysctl_stat_interval); +queue_delayed_work_on (cpu, mm_percpu_wq, dw, delay); + } + } +#endif + vms->dirty = true; }static inline void vmstat_clear_dirty (void ) { -this_cpu_write (vmstat_dirty, false); +struct vmstat_dirty *vms =this_cpu_ptr (&vmstat_dirty_pcpu); + + vms->dirty = false; }static inline bool is_vmstat_dirty (void ) { -return this_cpu_read (vmstat_dirty); +struct vmstat_dirty *vms =this_cpu_ptr (&vmstat_dirty_pcpu); + +return vms->dirty; }int calculate_pressure_threshold (struct zone *zone)
@@ -1886,9 +1916,6 @@ static const struct seq_operations vmsta#endif /* CONFIG_PROC_FS */ #ifdef CONFIG_SMP -static DEFINE_PER_CPU(struct delayed_work, vmstat_work); -int sysctl_stat_interval __read_mostly = HZ; - #ifdef CONFIG_PROC_FS static void refresh_vm_stats(struct work_struct *work) {
@@ -1973,7 +2000,7 @@ static void vmstat_update(struct work_st* until the diffs stay at zero. The function is used by NOHZ and can only be * invoked when tick processing is not active. */ -void quiet_vmstat (void ) +void quiet_vmstat (bool user) {if (system_state !=SYSTEM_RUNNING )return ;
@@ -1981,13 +2008,18 @@ void quiet_vmstat(void)if (!is_vmstat_dirty ())return ; +refresh_cpu_vm_stats (false); + +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER +if (!user) +return ;/* -* Just refresh counters and do not care about the pending delayed -* vmstat_update. It doesn't fire that often to matter and canceling -* it would be too expensive from this path. -* vmstat_shepherd will take care about that for us. +* If the tick is stopped, cancel any delayed work to avoid +* interruptions to this CPU in the future. */ -refresh_cpu_vm_stats (false); +if (delayed_work_pending (this_cpu_ptr (&vmstat_work))) +cancel_delayed_work (this_cpu_ptr (&vmstat_work)); +#endif }/*
@@ -2008,8 +2040,15 @@ static void vmstat_shepherd(struct work_/* Check processors whose vmstat worker threads have been disabled */ for_each_online_cpu (cpu) {struct delayed_work *dw = &per_cpu (vmstat_work, cpu); +struct vmstat_dirty *vms =per_cpu_ptr (&vmstat_dirty_pcpu, cpu); -if (!delayed_work_pending (dw) &&per_cpu (vmstat_dirty, cpu)) +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER +/* NOHZ full CPUs manage their own vmstat flushing */ +if (tick_nohz_full_cpu (cpu)) +continue ; +#endif + +if (!delayed_work_pending (dw) && vms->dirty)queue_delayed_work_on (cpu, mm_percpu_wq, dw,0 );cond_resched ();
@@ -2044,6 +2083,25 @@ static void __init init_cpu_node_state(vstatic int vmstat_cpu_online (unsigned int cpu) { +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER +struct vmstat_dirty *vms =per_cpu_ptr (&vmstat_dirty_pcpu, cpu); + +if (tick_nohz_full_cpu (cpu)) { +struct delayed_work *dw; + + vms->cpuhotplug = false; + vms->dirty = true; + + dw =this_cpu_ptr (&vmstat_work); +if (!delayed_work_pending (dw)) { +unsigned long delay; + + delay =round_jiffies_relative (sysctl_stat_interval); +queue_delayed_work_on (cpu, mm_percpu_wq, dw, delay); + } + } +#endif +refresh_zone_stat_thresholds ();if (!node_state (cpu_to_node (cpu),N_CPU )) {
@@ -2053,8 +2111,15 @@ static int vmstat_cpu_online(unsigned inreturn 0 ; } +/* +* ONLINE: The callbacks are invoked on the hotplugged CPU from the per CPU +* hotplug thread with interrupts and preemption enabled. +*/ static int vmstat_cpu_down_prep (unsigned int cpu) { +struct vmstat_dirty *vms =per_cpu_ptr (&vmstat_dirty_pcpu, cpu); + + vms->cpuhotplug = true;cancel_delayed_work_sync (&per_cpu (vmstat_work, cpu));return 0 ; }
include/linux/vmstat.h +2 -2
@@ -290,7 +290,7 @@ extern void dec_zone_state(struct zone *extern void __dec_zone_state (struct zone *,enum zone_stat_item );extern void __dec_node_state (struct pglist_data *,enum node_stat_item ); -void quiet_vmstat (void ); +void quiet_vmstat (bool user);void cpu_vm_stats_fold (int cpu);void refresh_zone_stat_thresholds (void );
@@ -403,7 +403,7 @@ static inline void __dec_node_page_statestatic inline void refresh_zone_stat_thresholds (void ) { }static inline void cpu_vm_stats_fold (int cpu) { } -static inline void quiet_vmstat (void ) { } +static inline void quiet_vmstat (bool user) { }static inline void drain_zonestat (struct zone *zone,struct per_cpu_zonestat *pzstats) { }
kernel/time/tick-sched.c +1 -1
@@ -911,7 +911,7 @@ static void tick_nohz_stop_tick(struct t*/ if (!ts->tick_stopped) {calc_load_nohz_start (); -quiet_vmstat (); +quiet_vmstat (false); ts->last_tick =hrtimer_get_expires (&ts->sched_timer); ts->tick_stopped =1 ;
mm/Kconfig +13 -0
@@ -1124,6 +1124,19 @@ config PTE_MARKER_UFFD_WP purposes. It is required to enable userfaultfd write protection on file-backed memory types like shmem and hugetlbfs. +config FLUSH_WORK_ON_RESUME_USER + bool "Flush per-CPU vmstats on user return (for nohz full CPUs)" + depends on NO_HZ_FULL + default y + + help + By default, nohz full CPUs flush per-CPU vm statistics on return + to userspace (to avoid additional interferences when executing + userspace code). This has a small but measurable impact on + system call performance. You can disable this to improve system call + performance, at the expense of potential interferences to userspace + execution. + # multi-gen LRU { config LRU_GEN bool "Multi-Gen LRU"
PATCH v11 4/6 tick/nohz_full: Ensure quiet_vmstat() is called on exit to user-mode when the idle tick is stopped
In reply to Marcelo TosattiFrom: Aaron Tomlin <[email protected]>
For nohz full CPUs, we'd like the per-CPU vm statistics to be synchronized when userspace is executing. Otherwise, the vmstat_shepherd might queue a work item to synchronize them, which is undesired intereference for isolated CPUs.
This patch syncs CPU-specific vmstat differentials, on return to userspace, if CONFIG_FLUSH_WORK_ON_RESUME_USER is enabled and the tick is stopped.
A trivial test program was used to determine the impact of the proposed changes and under vanilla. The mlock(2) and munlock(2) system calls was used solely to modify vmstat item 'NR_MLOCK'. The following is an average count of CPU-cycles across the aforementioned system calls:
Vanilla Modified
Cycles per syscall 8461 8690 (+2.6%)
--- include/linux/tick.h | 5 +++-- kernel/time/tick-sched.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-)
include/linux/tick.h +3 -2
@@ -11,7 +11,6 @@#include <linux/context_tracking_state.h> #include <linux/cpumask.h> #include <linux/sched.h> -#include <linux/rcupdate.h> #ifdef CONFIG_GENERIC_CLOCKEVENTS extern void __init tick_init (void );
@@ -272,6 +271,7 @@ static inline void tick_dep_clear_signalextern void tick_nohz_full_kick_cpu (int cpu);extern void __tick_nohz_task_switch (void ); +void __tick_nohz_user_enter_prepare (void );extern void __init tick_nohz_full_setup (cpumask_var_t cpumask);#else static inline bool tick_nohz_full_enabled (void ) {return false; }
@@ -296,6 +296,7 @@ static inline void tick_dep_clear_signalstatic inline void tick_nohz_full_kick_cpu (int cpu) { }static inline void __tick_nohz_task_switch (void ) { } +static inline void __tick_nohz_user_enter_prepare (void ) { }static inline void tick_nohz_full_setup (cpumask_var_t cpumask) { }#endif
@@ -308,7 +309,7 @@ static inline void tick_nohz_task_switchstatic inline void tick_nohz_user_enter_prepare (void ) {if (tick_nohz_full_cpu (smp_processor_id ())) -rcu_nocb_flush_deferred_wakeup (); +__tick_nohz_user_enter_prepare (); }#endif
kernel/time/tick-sched.c +17 -0
@@ -26,6 +26,7 @@#include <linux/posix-timers.h> #include <linux/context_tracking.h> #include <linux/mm.h> +#include <linux/rcupdate.h> #include <asm/irq_regs.h>
@@ -519,6 +520,22 @@ void __tick_nohz_task_switch(void) } } +void __tick_nohz_user_enter_prepare (void ) +{ +if (tick_nohz_full_cpu (smp_processor_id ())) { +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER +struct tick_sched *ts; + + ts =this_cpu_ptr (&tick_cpu_sched); + +if (ts->tick_stopped) +quiet_vmstat (true); +#endif +rcu_nocb_flush_deferred_wakeup (); + } +} +EXPORT_SYMBOL_GPL (__tick_nohz_user_enter_prepare); +/* Get the boot-time nohz CPU list from the kernel parameters. */ void __init tick_nohz_full_setup (cpumask_var_t cpumask) {
PATCH v11 2/6 mm/vmstat: Use vmstat_dirty to track CPU-specific vmstat discrepancies
In reply to Marcelo TosattiFrom: Aaron Tomlin <[email protected]>
This patch will now use the previously introduced CPU-specific variable namely vmstat_dirty to indicate if a vmstat differential/or imbalance is present for a given CPU. So, at the appropriate time, vmstat processing can be initiated. The hope is that this particular approach is "cheaper" when compared to need_update(). The idea is based on Marcelo's patch [1].
[1]: https://lore.kernel.org/lkml/[email protected]/
--- mm/vmstat.c | 48 ++++++++++++++---------------------------------- 1 file changed, 14 insertions(+), 34 deletions(-)
mm/vmstat.c +14 -34
@@ -381,6 +381,7 @@ void __mod_zone_page_state(struct zone * x =0 ; }__this_cpu_write (*p, x); +vmstat_mark_dirty ();preempt_enable_nested (); }
@@ -417,6 +418,7 @@ void __mod_node_page_state(struct pglist x =0 ; }__this_cpu_write (*p, x); +vmstat_mark_dirty ();preempt_enable_nested (); }
@@ -606,6 +608,7 @@ static inline void mod_zone_state(structif (z)zone_page_state_add (z, zone, item); +vmstat_mark_dirty (); } void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
@@ -674,6 +677,7 @@ static inline void mod_node_state(structif (z)node_page_state_add (z, pgdat, item); +vmstat_mark_dirty (); } void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item,
@@ -828,6 +832,14 @@ static int refresh_cpu_vm_stats(bool do_int global_node_diff[NR_VM_NODE_STAT_ITEMS ] = {0 , };int changes =0 ; +/* +* Clear vmstat_dirty before clearing the percpu vmstats. +* If interrupts are enabled, it is possible that an interrupt +* or another task modifies a percpu vmstat, which will +* set vmstat_dirty to true. +*/ +vmstat_clear_dirty (); +for_each_populated_zone (zone) {struct per_cpu_zonestat __percpu *pzstats = zone->per_cpu_zonestats;#ifdef CONFIG_NUMA
@@ -1957,35 +1969,6 @@ static void vmstat_update(struct work_st }/* -* 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_zonestat *pzstats =per_cpu_ptr (zone->per_cpu_zonestats, cpu); -struct per_cpu_nodestat *n; - -/* -* The fast way of checking if there are any vmstat diffs. -*/ -if (memchr_inv (pzstats->vm_stat_diff,0 ,sizeof (pzstats->vm_stat_diff))) -return true; - -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 ,sizeof (n->vm_node_stat_diff))) -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.
@@ -1995,10 +1978,7 @@ void quiet_vmstat(void)if (system_state !=SYSTEM_RUNNING )return ; -if (!delayed_work_pending (this_cpu_ptr (&vmstat_work))) -return ; - -if (!need_update (smp_processor_id ())) +if (!is_vmstat_dirty ())return ;/*
@@ -2029,7 +2009,7 @@ static void vmstat_shepherd(struct work_for_each_online_cpu (cpu) {struct delayed_work *dw = &per_cpu (vmstat_work, cpu); -if (!delayed_work_pending (dw) &&need_update (cpu)) +if (!delayed_work_pending (dw) &&per_cpu (vmstat_dirty, cpu))queue_delayed_work_on (cpu, mm_percpu_wq, dw,0 );cond_resched ();
PATCH v11 6/6 mm/vmstat: 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 +46 -5
@@ -1917,6 +1917,31 @@ static const struct seq_operations vmsta #ifdef CONFIG_SMP #ifdef CONFIG_PROC_FS +static bool need_drain_remote_zones(int cpu) +{ +#ifdef CONFIG_NUMA + struct zone *zone; + + for_each_populated_zone(zone) { + struct per_cpu_pages *pcp; + + pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu); + if (!pcp->count) + continue; + + if (!pcp->expire) + continue; + + if (zone_to_nid(zone) == cpu_to_node(cpu)) + continue; + + return true; + } +#endif + + return false; +} + static void refresh_vm_stats(struct work_struct *work) { refresh_cpu_vm_stats(true);
@@ -1926,8 +1951,12 @@ int vmstat_refresh(struct ctl_table *tab void *buffer, size_t *lenp, loff_t *ppos) { long val; -int err; -int i; +int i, cpu; +struct work_struct __percpu *works; + + works = alloc_percpu(struct work_struct); +if (!works) +return -ENOMEM ;/* * The regular update, every sysctl_stat_interval, may come later
@@ -1941,9 +1970,21 @@ 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; +cpus_read_lock (); +for_each_online_cpu (cpu) { +struct work_struct *work =per_cpu_ptr (works, cpu); +struct vmstat_dirty *vms =per_cpu_ptr (&vmstat_dirty_pcpu, cpu); + +INIT_WORK (work, refresh_vm_stats); + +if (vms->dirty ||need_drain_remote_zones (cpu)) +schedule_work_on (cpu, work); + } +for_each_online_cpu (cpu) +flush_work (per_cpu_ptr (works, cpu)); +cpus_read_unlock (); +free_percpu (works); +for (i =0 ; i <NR_VM_ZONE_STAT_ITEMS ; i++) {/* * Skip checking stats known to go negative occasionally.
v11 cover Ensure quiet_vmstat() is called when returning to userpace and when idle tick is stopped
This patch series addresses the following two problems:
1. A customer provided some evidence which indicates that the idle tick was stopped; albeit, CPU-specific vmstat counters still remained populated.
Thus one can only assume quiet_vmstat() was not invoked on return to the idle loop. If I understand correctly, I suspect this divergence might erroneously prevent a reclaim attempt by kswapd. If the number of zone specific free pages are below their per-cpu drift value then zone_page_state_snapshot() is used to compute a more accurate view of the aforementioned statistic. Thus any task blocked on the NUMA node specific pfmemalloc_wait queue will be unable to make significant progress via direct reclaim unless it is killed after being woken up by kswapd
(see throttle_direct_reclaim())
2. With a SCHED_FIFO task that busy loops on a given CPU, and kworker for that CPU at SCHED_OTHER priority, queuing work to sync per-vmstats will either cause that work to never execute, or stalld (i.e. stall daemon) boosts kworker priority which causes a latency violation
As seen previously, the trivial test program (i.e. attached at the end of this cover letter) executed inside a KVM VM, was used to determine the somewhat impact under vanilla and with the proposed changes. Firstly, the mlock(2) and munlock(2) system calls was used solely to modify vmstat item 'NR_MLOCK'. In another scenario, the nanosleep(2) system call was used several times to suspend execution for a period of time to approximately compute the number of CPU-cycles in the idle code path. The following is an average count of CPU-cycles across the aforementioned system calls and the idle loop, respectively. I believe these results are negligible:
Vanilla Modified
Cycles per idle loop 151858 153258 (+1.0%) Cycles per syscall 8461 8690 (+2.6%)
Any feedback would be appreciated. Thanks.
Changes since v10 [1]:
- Close cpu hotplug race with nohz_full CPUs
(Frederic Weisbecker)
Changes since v9 [2]:
- Add config to enable/disable syncing when returning to userspace (Frederic Weisbecker)
- Add missing signed-off-by
(Frederic Weisbecker)
- Use proper CPU value when skipping nohz_full CPUs
(Frederic Weisbecker)
- Use this_cpu_ptr when appropriate
(Frederic Weisbecker)
- Improve changelogs
(Frederic Weisbecker)
- For stat_refresh sysfs file: avoid queueing work on CPU if stats are clean
Changes since v8 [3]:
- For nohz_full CPUs, manage per-CPU vmstat flushing from CPU context (Frederic Weisbecker)
Changes since v7 [4]:
- Added trivial helpers for modification and testing (Andrew Morton)
- Modified comment since we do now cancel any delayed work if the tick is stopped in quiet_vmstat()
- Moved check to ensure vmstat differentials do not remain if the tick is stopped on exiting to user-mode into a separate patch
(Frederic Weisbecker)
Changes since v6 [5]:
- Clean vmstat_dirty before differential sync loop
- Cancel pending work if tick stopped
- Do not queue work to remote CPU if tick stopped
Changes since v5 [6]:
- Introduced __tick_nohz_user_enter_prepare()
- Switched to EXPORT_SYMBOL_GPL()
Changes since v4 [7]:
- Moved vmstat_dirty specific changes into a separate patch (Marcelo Tosatti)
Changes since v3 [8]:
- Used EXPORT_SYMBOL() on tick_nohz_user_enter_prepare()
- Replaced need_update()
- Introduced CPU-specific variable namely vmstat_dirty and mark_vmstat_dirty()
[1]: https://lore.kernel.org/linux-mm/[email protected]/T/
[2]: https://lore.kernel.org/lkml/20221214131839.GE1930067@lothringen/t/
[3]: https://lore.kernel.org/linux-mm/[email protected]/
[4]: https://lore.kernel.org/lkml/[email protected]/
[5]: https://lore.kernel.org/linux-mm/[email protected]/
[6]: https://lore.kernel.org/lkml/[email protected]/
[7]: https://lore.kernel.org/lkml/[email protected]/
[8]: https://lore.kernel.org/lkml/[email protected]/
Aaron Tomlin (4):
mm/vmstat: Add CPU-specific variable to track a vmstat discrepancy mm/vmstat: Use vmstat_dirty to track CPU-specific vmstat discrepancies tick/nohz_full: Ensure quiet_vmstat() is called on exit to user-mode
when the idle tick is stopped
tick/sched: Ensure quiet_vmstat() is called when the idle tick was
stopped too
Marcelo Tosatti (2):
mm/vmstat: Do not queue vmstat_update if tick is stopped mm/vmstat: avoid queueing work item if cpu stats are clean
include/linux/tick.h | 5 +- include/linux/vmstat.h | 4 +- kernel/time/tick-sched.c | 20 ++++++++++- mm/Kconfig | 13 +++++++ mm/vmstat.c | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------- 5 files changed, 186 insertions(+), 52 deletions(-)
PATCH v11 1/6 mm/vmstat: Add CPU-specific variable to track a vmstat discrepancy
From: Aaron Tomlin <[email protected]>
Introduce a CPU-specific variable namely vmstat_dirty to indicate if a vmstat imbalance is present for a given CPU. Therefore, at the appropriate time, we can fold all the remaining differentials. This patch also provides trivial helpers for modification and testing.
--- mm/vmstat.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
mm/vmstat.c +16 -0
@@ -194,6 +194,22 @@ void fold_vm_numa_events(void)#endif #ifdef CONFIG_SMP +static DEFINE_PER_CPU_ALIGNED(bool, vmstat_dirty); + +static inline void vmstat_mark_dirty (void ) +{ +this_cpu_write (vmstat_dirty, true); +} + +static inline void vmstat_clear_dirty (void ) +{ +this_cpu_write (vmstat_dirty, false); +} + +static inline bool is_vmstat_dirty (void ) +{ +return this_cpu_read (vmstat_dirty); +}int calculate_pressure_threshold (struct zone *zone) {
Frederic Weisbecker
maintainerRe: [PATCH v11 3/6] mm/vmstat: manage per-CPU stats from CPU context when NOHZ full
In reply to Marcelo Tosattimm/vmstat.c void fold_vm_numa_events(void) · line 201
Quoted 9 lines · Marcelo Tosatti
mm/vmstat.c +4
-1@@ -194,21 +195,50 @@ void fold_vm_numa_events(void)#endif #ifdef CONFIG_SMP -static DEFINE_PER_CPU_ALIGNED(bool, vmstat_dirty); + +struct vmstat_dirty { + bool dirty; + bool cpuhotplug;
May be call it "online" for clarity. Also should it depend on CONFIG_FLUSH_WORK_ON_RESUME_USER?
mm/vmstat.c void fold_vm_numa_events(void) · line 212
Quoted 12 lines
mm/vmstat.c +8
-1+}; + +static DEFINE_PER_CPU_ALIGNED(struct vmstat_dirty, vmstat_dirty_pcpu); +static DEFINE_PER_CPU(struct delayed_work, vmstat_work); +int sysctl_stat_interval__read_mostly =HZ ;static inline void vmstat_mark_dirty (void ) { -this_cpu_write (vmstat_dirty, true); +struct vmstat_dirty *vms =this_cpu_ptr (&vmstat_dirty_pcpu); + +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER
Please avoid ifdeffery in the middle of a function when possible. This block could be in a different function or use IS_ENABLED() for example.
Quoted 13 lines
mm/vmstat.c +13
-0+int cpu =smp_processor_id (); + +if (tick_nohz_full_cpu (cpu) && !vms->dirty) { +struct delayed_work *dw; + + dw =this_cpu_ptr (&vmstat_work); +if (!delayed_work_pending (dw) && !vms->cpuhotplug) { +unsigned long delay; + + delay =round_jiffies_relative (sysctl_stat_interval); +queue_delayed_work_on (cpu, mm_percpu_wq, dw, delay); + } + }
mm/vmstat.c void fold_vm_numa_events(void) · line 234
Quoted 10 lines
mm/vmstat.c +5
-1+#endif + vms->dirty = true; }static inline void vmstat_clear_dirty (void ) { -this_cpu_write (vmstat_dirty, false); +struct vmstat_dirty *vms =this_cpu_ptr (&vmstat_dirty_pcpu); + + vms->dirty = false;
You could keep this_cpu_write(vmstat_dirty.dirty, false)
mm/vmstat.c void fold_vm_numa_events(void) · line 241
Quoted 8 lines
mm/vmstat.c +3
-1}static inline bool is_vmstat_dirty (void ) { -return this_cpu_read (vmstat_dirty); +struct vmstat_dirty *vms =this_cpu_ptr (&vmstat_dirty_pcpu); + +return vms->dirty;
Ditto with this_cpu_read()?
mm/vmstat.c void quiet_vmstat(void) · line 2013
Quoted 10 lines
mm/vmstat.c +3
-0}int calculate_pressure_threshold (struct zone *zone)@@ -1981,13 +2008,18 @@ void quiet_vmstat(void)if (!is_vmstat_dirty ())return ; +refresh_cpu_vm_stats (false); + +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER
This can use IS_ENABLED()
Quoted 18 lines
mm/vmstat.c +7
-5+if (!user) +return ;/* -* Just refresh counters and do not care about the pending delayed -* vmstat_update. It doesn't fire that often to matter and canceling -* it would be too expensive from this path. -* vmstat_shepherd will take care about that for us. +* If the tick is stopped, cancel any delayed work to avoid +* interruptions to this CPU in the future. */ -refresh_cpu_vm_stats (false); +if (delayed_work_pending (this_cpu_ptr (&vmstat_work))) +cancel_delayed_work (this_cpu_ptr (&vmstat_work)); +#endif }/*
mm/vmstat.c static void vmstat_shepherd(struct work_ · line 2045
Quoted 7 lines
mm/vmstat.c +2
-1@@ -2008,8 +2040,15 @@ static void vmstat_shepherd(struct work_/* Check processors whose vmstat worker threads have been disabled */ for_each_online_cpu (cpu) {struct delayed_work *dw = &per_cpu (vmstat_work, cpu); +struct vmstat_dirty *vms =per_cpu_ptr (&vmstat_dirty_pcpu, cpu); -if (!delayed_work_pending (dw) &&per_cpu (vmstat_dirty, cpu)) +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER
Same here.
Quoted 10 lines
mm/vmstat.c +6
-0+/* NOHZ full CPUs manage their own vmstat flushing */ +if (tick_nohz_full_cpu (cpu)) +continue ; +#endif + +if (!delayed_work_pending (dw) && vms->dirty)queue_delayed_work_on (cpu, mm_percpu_wq, dw,0 );cond_resched ();
mm/vmstat.c static int vmstat_cpu_online(unsigned in · line 2116
Quoted 6 lines
mm/vmstat.c +3
-0@@ -2053,8 +2111,15 @@ static int vmstat_cpu_online(unsigned inreturn 0 ; } +/* +* ONLINE: The callbacks are invoked on the hotplugged CPU from the per CPU +* hotplug thread with interrupts and preemption enabled.
This is OFFLINE and the reason behind that comment is confusing.
mm/vmstat.c static int vmstat_cpu_online(unsigned in · line 2122
Quoted 6 lines
mm/vmstat.c +4
-0+*/ static int vmstat_cpu_down_prep (unsigned int cpu) { +struct vmstat_dirty *vms =per_cpu_ptr (&vmstat_dirty_pcpu, cpu); + + vms->cpuhotplug = true;
this_cpu_write() ?
Quoted 5 lines
mm/Kconfig +2
-0cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu)); return 0; } +config FLUSH_WORK_ON_RESUME_USER + bool "Flush per-CPU vmstats on user return (for nohz full CPUs)"
mm/Kconfig config PTE_MARKER_UFFD_WP · line 1138
Quoted 10 lines
mm/Kconfig +10
-0+ depends on NO_HZ_FULL + default y + + help + By default, nohz full CPUs flush per-CPU vm statistics on return + to userspace (to avoid additional interferences when executing + userspace code). This has a small but measurable impact on + system call performance. You can disable this to improve system call + performance, at the expense of potential interferences to userspace + execution.
Can you move that below config CPU_ISOLATION ?
Thanks!
Quoted 6 lines
mm/Kconfig +1
-0+ # multi-gen LRU { config LRU_GEN bool "Multi-Gen LRU"