From patchwork Wed Aug 17 19:01:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 12946311 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7721C32772 for ; Wed, 17 Aug 2022 19:03:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A9046B0073; Wed, 17 Aug 2022 15:03:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 855118D0003; Wed, 17 Aug 2022 15:03:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6F44D6B0075; Wed, 17 Aug 2022 15:03:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6062E6B0073 for ; Wed, 17 Aug 2022 15:03:50 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 3CF63A0A9E for ; Wed, 17 Aug 2022 19:03:50 +0000 (UTC) X-FDA: 79810009020.12.C383B4D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf17.hostedemail.com (Postfix) with ESMTP id CD9BB4008B for ; Wed, 17 Aug 2022 19:03:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660763029; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=QjliH6Zbs7SxREI0yAVgUWAGnTR2b2eGLogi48MSCUs=; b=Ywxzj1TMKVS2jUmDDL5F33QiDP9zgwoazNktee7NTcQbYWGZbKNnCbAOjoMRnOlmU7+cyo ZzdOb2g9UVqUG83ewNMVmzQTI36NNUbpkns7xwd5twXJ6O4W/ZwET8LTaLa1FJVyuGiveh x0/JetG6gIjULdrNLOWq/Bxyb+iSpfY= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-301-FKthpWl-NYCrKTMounRGtQ-1; Wed, 17 Aug 2022 15:03:46 -0400 X-MC-Unique: FKthpWl-NYCrKTMounRGtQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 800C93810793; Wed, 17 Aug 2022 19:03:45 +0000 (UTC) Received: from fuller.cnet (ovpn-112-2.gru2.redhat.com [10.97.112.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id ED5751121319; Wed, 17 Aug 2022 19:03:44 +0000 (UTC) Received: by fuller.cnet (Postfix, from userid 1000) id 7DD32416D5E9; Wed, 17 Aug 2022 16:03:26 -0300 (-03) Message-ID: <20220817190221.486745251@redhat.com> User-Agent: quilt/0.66 Date: Wed, 17 Aug 2022 16:01:37 -0300 From: Marcelo Tosatti To: atomlin@redhat.com, frederic@kernel.org Cc: cl@linux.com, tglx@linutronix.de, mingo@kernel.org, peterz@infradead.org, pauld@redhat.com, neelx@redhat.com, oleksandr@natalenko.name, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Marcelo Tosatti Subject: [patch 1/3] mm/vmstat: Use per cpu variable to track a vmstat discrepancy References: <20220817190136.096849758@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660763029; a=rsa-sha256; cv=none; b=e7YJMIgZN1FTrlxN3rfW2BLeu5is+diH/nyO7y8uf6D+V+QQFqqFCTcdE3nb0X5rSwIjER +Ggs2VxbZWLBkhXkjzVdp6KzejC2c5QH/JIapo4m4zUMbR+KoPXZieNyueo4dmne69WRpi IoDOC0HGBCEWLxZ10WTCQJW2HVRE4To= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Ywxzj1TM; spf=pass (imf17.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=mtosatti@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660763029; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:references:dkim-signature; bh=QjliH6Zbs7SxREI0yAVgUWAGnTR2b2eGLogi48MSCUs=; b=TBZGMo18qSdweONsQeocu+fHUmzzUPUoplLU8HFBPVLgjCQqRdxJ0NdMgiVR7JvvRUTPgq KauxqvRs+Yr+61hlEEBSESsqm/LU471wApkOm8QP9T1UA0UEinZynT+4qRTQ9NKYAAWmyQ Je1u9PVKHIa0WG9Zr81FyzBR+TWd+XU= Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Ywxzj1TM; spf=pass (imf17.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=mtosatti@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Stat-Signature: 9jfc1t9oz665txhsxdt8rmi38rbbn5n4 X-Rspamd-Queue-Id: CD9BB4008B X-Rspamd-Server: rspam06 X-HE-Tag: 1660763029-125050 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Aaron Tomlin Add 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 speeds up quiet_vmstat in case no per-CPU differentials exist. Based on https://lore.kernel.org/lkml/20220204173554.763888172@fedora.localdomain/ Signed-off-by: Aaron Tomlin Signed-off-by: Marcelo Tosatti --- mm/vmstat.c | 54 ++++++++++++++++++++---------------------------------- 1 file changed, 20 insertions(+), 34 deletions(-) Index: linux-2.6/mm/vmstat.c =================================================================== --- linux-2.6.orig/mm/vmstat.c +++ linux-2.6/mm/vmstat.c @@ -195,6 +195,12 @@ void fold_vm_numa_events(void) #endif #ifdef CONFIG_SMP +static DEFINE_PER_CPU_ALIGNED(bool, vmstat_dirty); + +static inline void mark_vmstat_dirty(void) +{ + this_cpu_write(vmstat_dirty, true); +} int calculate_pressure_threshold(struct zone *zone) { @@ -367,6 +373,7 @@ void __mod_zone_page_state(struct zone * x = 0; } __this_cpu_write(*p, x); + mark_vmstat_dirty(); if (IS_ENABLED(CONFIG_PREEMPT_RT)) preempt_enable(); @@ -405,6 +412,7 @@ void __mod_node_page_state(struct pglist x = 0; } __this_cpu_write(*p, x); + mark_vmstat_dirty(); if (IS_ENABLED(CONFIG_PREEMPT_RT)) preempt_enable(); @@ -603,6 +611,7 @@ static inline void mod_zone_state(struct if (z) zone_page_state_add(z, zone, item); + mark_vmstat_dirty(); } void mod_zone_page_state(struct zone *zone, enum zone_stat_item item, @@ -671,6 +680,7 @@ static inline void mod_node_state(struct if (z) node_page_state_add(z, pgdat, item); + mark_vmstat_dirty(); } void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item, @@ -825,6 +835,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. + */ + this_cpu_write(vmstat_dirty, false); + for_each_populated_zone(zone) { struct per_cpu_zonestat __percpu *pzstats = zone->per_cpu_zonestats; #ifdef CONFIG_NUMA @@ -1949,35 +1967,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. @@ -1987,10 +1976,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 (!__this_cpu_read(vmstat_dirty)) return; /* @@ -2021,7 +2007,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(); From patchwork Wed Aug 17 19:01:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 12946312 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56D2BC2BB41 for ; Wed, 17 Aug 2022 19:03:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D124F8D0003; Wed, 17 Aug 2022 15:03:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CC2EE8D0005; Wed, 17 Aug 2022 15:03:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B3A718D0003; Wed, 17 Aug 2022 15:03:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 9FD6A8D0005 for ; Wed, 17 Aug 2022 15:03:50 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 7AC721A0A53 for ; Wed, 17 Aug 2022 19:03:50 +0000 (UTC) X-FDA: 79810009020.20.7C8342B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf28.hostedemail.com (Postfix) with ESMTP id ECC33C01B7 for ; Wed, 17 Aug 2022 19:03:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660763029; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=A9mez3un0kb/dY1Y5a1jB6zoEKYBA9P7qIxMNfLm/80=; b=cWOXZDjg+sBCXRdOPvn1nAlzTvZUchN2WS1rZFUZ5HrYSjEYtvth2n1+Bv5rtw/8QSzqJ0 Jin2BoeHh0SLCgqJzLjqi2lpyNFPSy20/FvGoxk8Lj8J6PsC5IJz2vv7rBekzistIDwBSY rg6qbNTFe7EYwVcLDeFFC2kfiPvGFoI= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-329-IV0x1qqsNbC0NScXgRcFVQ-1; Wed, 17 Aug 2022 15:03:46 -0400 X-MC-Unique: IV0x1qqsNbC0NScXgRcFVQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B004A296A612; Wed, 17 Aug 2022 19:03:45 +0000 (UTC) Received: from fuller.cnet (ovpn-112-2.gru2.redhat.com [10.97.112.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EAA1F1121315; Wed, 17 Aug 2022 19:03:44 +0000 (UTC) Received: by fuller.cnet (Postfix, from userid 1000) id 80968416D882; Wed, 17 Aug 2022 16:03:26 -0300 (-03) Message-ID: <20220817190221.546589594@redhat.com> User-Agent: quilt/0.66 Date: Wed, 17 Aug 2022 16:01:38 -0300 From: Marcelo Tosatti To: atomlin@redhat.com, frederic@kernel.org Cc: cl@linux.com, tglx@linutronix.de, mingo@kernel.org, peterz@infradead.org, pauld@redhat.com, neelx@redhat.com, oleksandr@natalenko.name, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Marcelo Tosatti Subject: [patch 2/3] tick/sched: Ensure quiet_vmstat() is called when the idle tick was stopped too References: <20220817190136.096849758@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660763030; a=rsa-sha256; cv=none; b=cWMg44Q2DcjQ1OdxmHsI0hR+EMCLYJYURDiG0GMSdOBcaqe8sIJM2LTLad384lCAivpem7 bYKugVLfTrfq0jN5mfPo0dKMgq+t9bLJCoX1wyHZUYYyU0AmWBJBRsJzhrtpONKrSZgiG5 QEUB4KcN98BXRSDMxvdzymCGl+6P3L4= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cWOXZDjg; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf28.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=mtosatti@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660763030; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:references:dkim-signature; bh=A9mez3un0kb/dY1Y5a1jB6zoEKYBA9P7qIxMNfLm/80=; b=g1cC/yEH5tCHhZrQfPz1nhslVzURz3jlaHTnYCBYuHIEziNfHO+rYJvoqgTAElrteUl5Jv lpHbRg+0+ixzzmx0Zb0Y+jZ1cp6cePLx2yP1wwiJ2aOV9DIxtK5WaF8qGBawrd31h8Ud2D 8CCPL1ohbyBtH5fQe64D1l/vLPNtl/w= X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: ECC33C01B7 X-Rspam-User: Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cWOXZDjg; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf28.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=mtosatti@redhat.com X-Stat-Signature: izspq538aqiqpj3wh79ymr1ihpknikht X-HE-Tag: 1660763029-664765 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Aaron Tomlin 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: 1. CPU Y migrated running task A to CPU X that was in an idle state i.e. waiting for an IRQ - not polling; 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 from CPU Y; 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 Y, indirectly released a few pages (e.g. see __free_one_page()); CPU Y'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 Y'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 with this patch is to ensure refresh_cpu_vm_stats(false) is called, when it is appropriate, on return to the idle loop when the idle tick was previously stopped too. Additionally, in the context of nohz_full, when the scheduling-tick is stopped and before exiting to user-mode, ensure no CPU-specific vmstat differentials remain. Signed-off-by: Aaron Tomlin Signed-off-by: Marcelo Tosatti --- include/linux/tick.h | 5 +++-- kernel/time/tick-sched.c | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) Index: linux-2.6/include/linux/tick.h =================================================================== --- linux-2.6.orig/include/linux/tick.h +++ linux-2.6/include/linux/tick.h @@ -11,7 +11,6 @@ #include #include #include -#include #ifdef CONFIG_GENERIC_CLOCKEVENTS extern void __init tick_init(void); @@ -272,6 +271,7 @@ static inline void tick_dep_clear_signal extern 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_signal static 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_switch static 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 Index: linux-2.6/kernel/time/tick-sched.c =================================================================== --- linux-2.6.orig/kernel/time/tick-sched.c +++ linux-2.6/kernel/time/tick-sched.c @@ -26,6 +26,7 @@ #include #include #include +#include #include @@ -519,6 +520,20 @@ void __tick_nohz_task_switch(void) } } +void __tick_nohz_user_enter_prepare(void) +{ + struct tick_sched *ts; + + if (tick_nohz_full_cpu(smp_processor_id())) { + ts = this_cpu_ptr(&tick_cpu_sched); + + if (ts->tick_stopped) + quiet_vmstat(); + 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) { @@ -890,6 +905,9 @@ static void tick_nohz_stop_tick(struct t ts->do_timer_last = 0; } + /* Attempt to fold when the idle tick is stopped or not */ + quiet_vmstat(); + /* Skip reprogram of event if its not changed */ if (ts->tick_stopped && (expires == ts->next_tick)) { /* Sanity check: make sure clockevent is actually programmed */ @@ -911,7 +929,6 @@ static void tick_nohz_stop_tick(struct t */ if (!ts->tick_stopped) { calc_load_nohz_start(); - quiet_vmstat(); ts->last_tick = hrtimer_get_expires(&ts->sched_timer); ts->tick_stopped = 1; From patchwork Wed Aug 17 19:01:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 12946313 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 946F0C25B08 for ; Wed, 17 Aug 2022 19:03:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B1E68D0006; Wed, 17 Aug 2022 15:03:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2622C8D0005; Wed, 17 Aug 2022 15:03:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 101F78D0006; Wed, 17 Aug 2022 15:03:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 002EA8D0005 for ; Wed, 17 Aug 2022 15:03:57 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C902A1A075D for ; Wed, 17 Aug 2022 19:03:57 +0000 (UTC) X-FDA: 79810009314.28.980D175 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf06.hostedemail.com (Postfix) with ESMTP id 3B07B180049 for ; Wed, 17 Aug 2022 19:03:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660763036; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=n0ItpIZmzr3MlsvmDpXDhuK40hnz/P19I8npZQ9WGtY=; b=csCqWcXACqknuGSznQqiO6pqf+b5zNk5akNrrOsWVuFEAEgyTIMejVBmSGD0ZfmT95Pj64 7Tx5eNEU9SPpfo7Fb7reEHwsOtLprnopcuWS5lWcIVz7SJIuczwdkf28Y1UazzJwfOearT aBBAenvv/Xsa7XCri9ulso/eD88KAoQ= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-606-ck2z4GR1NhmvDYwC8W9CYg-1; Wed, 17 Aug 2022 15:03:46 -0400 X-MC-Unique: ck2z4GR1NhmvDYwC8W9CYg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7D844381078F; Wed, 17 Aug 2022 19:03:45 +0000 (UTC) Received: from fuller.cnet (ovpn-112-2.gru2.redhat.com [10.97.112.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id ED3B818EB5; Wed, 17 Aug 2022 19:03:44 +0000 (UTC) Received: by fuller.cnet (Postfix, from userid 1000) id 85099416D8A2; Wed, 17 Aug 2022 16:03:26 -0300 (-03) Message-ID: <20220817190221.608436609@redhat.com> User-Agent: quilt/0.66 Date: Wed, 17 Aug 2022 16:01:39 -0300 From: Marcelo Tosatti To: atomlin@redhat.com, frederic@kernel.org Cc: cl@linux.com, tglx@linutronix.de, mingo@kernel.org, peterz@infradead.org, pauld@redhat.com, neelx@redhat.com, oleksandr@natalenko.name, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Marcelo Tosatti Subject: [patch 3/3] mm/vmstat: do not queue vmstat_update if tick is stopped References: <20220817190136.096849758@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660763037; a=rsa-sha256; cv=none; b=3Adgk5hpmT66+JtJnl9PPMMHum+3bcUKYZQicofzHPWC5wJIxexINmoyJS/bzSBPNDzIHC PTagfPuCjo7e9eOPKs5LWCOKWjiM3pSfyN1rGMarHATzc8of+xcOfolxBK/KjMA5QXd7kE OAssmJwWypNdQD2B+hdYQMlxxFjJbZs= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=csCqWcXA; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mtosatti@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660763037; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:references:dkim-signature; bh=n0ItpIZmzr3MlsvmDpXDhuK40hnz/P19I8npZQ9WGtY=; b=KaQkzPzynHn1LCgBD8Rc0DgeBHep+r2LR/gZ6u83mZThxT9Hl6hjK4ZqMyZE5zTBXEHVUu Hu36na8ydTPNgZmeP9iB3OusYP98cBg7tP1FIT8PEqQW8NpVuSLq9qJGnPL9yL2Jm+YVgm rZiueCzQb607m/kvv91ToPPEsLeyiSs= X-Stat-Signature: 7d3sr83k1namx61xhijcsxnc3ps79mw1 X-Rspamd-Queue-Id: 3B07B180049 Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=csCqWcXA; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mtosatti@redhat.com X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1660763037-998007 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From the vmstat shepherd, for CPUs that have the tick stopped, do not queue local work to flush the per-CPU vmstats, since in that case the flush is performed on return to userspace or when entering idle. Per-cpu pages can be freed remotely from housekeeping CPUs. Move the quiet_vmstat call after ts->tick_stopped = 1 assignment. Signed-off-by: Marcelo Tosatti --- kernel/time/tick-sched.c | 6 +++--- mm/vmstat.c | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) Index: linux-2.6/mm/vmstat.c =================================================================== --- linux-2.6.orig/mm/vmstat.c +++ linux-2.6/mm/vmstat.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "internal.h" @@ -1973,19 +1974,27 @@ static void vmstat_update(struct work_st */ void quiet_vmstat(void) { + struct delayed_work *dw; + if (system_state != SYSTEM_RUNNING) return; if (!__this_cpu_read(vmstat_dirty)) return; + refresh_cpu_vm_stats(false); + /* - * 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. + * + * Otherwise 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. */ - refresh_cpu_vm_stats(false); + dw = &per_cpu(vmstat_work, smp_processor_id()); + if (delayed_work_pending(dw) && tick_nohz_tick_stopped()) + cancel_delayed_work(dw); } /* @@ -2007,6 +2016,9 @@ static void vmstat_shepherd(struct work_ for_each_online_cpu(cpu) { struct delayed_work *dw = &per_cpu(vmstat_work, cpu); + if (tick_nohz_tick_stopped_cpu(cpu)) + continue; + if (!delayed_work_pending(dw) && per_cpu(vmstat_dirty, cpu)) queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0); Index: linux-2.6/kernel/time/tick-sched.c =================================================================== --- linux-2.6.orig/kernel/time/tick-sched.c +++ linux-2.6/kernel/time/tick-sched.c @@ -905,9 +905,6 @@ static void tick_nohz_stop_tick(struct t ts->do_timer_last = 0; } - /* Attempt to fold when the idle tick is stopped or not */ - quiet_vmstat(); - /* Skip reprogram of event if its not changed */ if (ts->tick_stopped && (expires == ts->next_tick)) { /* Sanity check: make sure clockevent is actually programmed */ @@ -935,6 +932,9 @@ static void tick_nohz_stop_tick(struct t trace_tick_stop(1, TICK_DEP_MASK_NONE); } + /* Attempt to fold when the idle tick is stopped or not */ + quiet_vmstat(); + ts->next_tick = tick; /*