From patchwork Fri Dec 16 19:45:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13075350 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 F24A8C4167B for ; Fri, 16 Dec 2022 19:52:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 527BB8E0005; Fri, 16 Dec 2022 14:52:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3765E8E0008; Fri, 16 Dec 2022 14:52:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0DC6B8E0005; Fri, 16 Dec 2022 14:52:42 -0500 (EST) 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 DD7358E0007 for ; Fri, 16 Dec 2022 14:52:42 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B397112083E for ; Fri, 16 Dec 2022 19:52:42 +0000 (UTC) X-FDA: 80249216964.28.DED3434 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf20.hostedemail.com (Postfix) with ESMTP id 2DBEF1C0019 for ; Fri, 16 Dec 2022 19:52:41 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Pc6GAknH; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf20.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=1671220361; 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=kyxh01Z6aQw9/MQYaoI58SSLBFSUwnSJa8fNd5lmbmc=; b=DJJQLBOEnc86ZsvBlsVN/DWNN9XgMABoRz7q6tc9hLHDJO4SlGTbe9cA4HVWwNX/y+EfR8 g61q2e1NN02yMrj1U+ndU4ZwnujvLvHCgx+SJGsCbWo6/Bz3e967Vicy5WtKcswp6JQNfs xUePyS6cIv+GbSTW5EjZAbuiOMGge64= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Pc6GAknH; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf20.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=mtosatti@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671220361; a=rsa-sha256; cv=none; b=eejCk0wQc0RFly9PXTIKT9qfnOyr8YO0YFtQp76EzKfaMwMQN/ZwdSPMu1tlZ657G5M2QW srlYVA0x8dXAeqrL5PsUK1furZckOC0eqzQz+SsVBrtzBIbopm9gZHujSf4/aTO8BHsvKu 4Xfro+K7rskU8nnO3cyP2r4HNZYZunw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671220360; 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=kyxh01Z6aQw9/MQYaoI58SSLBFSUwnSJa8fNd5lmbmc=; b=Pc6GAknHVru0jDDUdiEOUCmXEkySXRTKi+jp3N62g0vB9ktKXLLO4MFXxu7Yff49Hw9bka QW6ae9eZIoEsxPwx4LhFvw9zJKYm8DZ0QIqYANI4QWHwXZkveO90Pv/4XmApCNOzLVTv4G NhvwgDrvRYFhgSiqWQwlUS9M0GwFHaQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-607-kZFRYmw_Ofm-GfhYazO0XA-1; Fri, 16 Dec 2022 14:52:37 -0500 X-MC-Unique: kZFRYmw_Ofm-GfhYazO0XA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2681D85CCE3; Fri, 16 Dec 2022 19:52:36 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-3.gru2.redhat.com [10.97.112.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8CA27492C14; Fri, 16 Dec 2022 19:52:35 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id C48684097957F; Fri, 16 Dec 2022 16:52:10 -0300 (-03) Message-ID: <20221216194904.075275493@redhat.com> User-Agent: quilt/0.66 Date: Fri, 16 Dec 2022 16:45:41 -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 v10 1/6] mm/vmstat: Add CPU-specific variable to track a vmstat discrepancy References: <20221216194540.202752779@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Rspamd-Queue-Id: 2DBEF1C0019 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: jgixz79ccys97t7ka4shzkkoy5w99kqx X-HE-Tag: 1671220361-874837 X-HE-Meta: U2FsdGVkX19appYz8aQwiE1CNEAeI9QD/pmm78NjB3zI/LTD+aIzg5PWFYHyaJ8XsOLXV9HgOsPgQEAuvcvhFJ289CHMcRP2oY6aAJlEA2yy8nQWuLnCjbnUSKeTeozjaQzXmypL1o49zqNR/3V4m1mGnMSJ49LIuPr/8BpFUhaBjhLqvTBHxoQCheEcRNvTIoqDKbtdzOwBf7rSL/YinB+EnSD8i+ZKi6GOFEfseoMZzfk3gFIIb31lKFcmPf4UlTYVkU5yBeheqpZZT8FCr+dxe4tysfVnFwlSF3vHANUtF/SFeQK6GiGePdDo7MQt1f7BFjOiEejezhFrUBASM2paoG0WHFQBppSGW4W7jBDEzih7ZV4mXLjxLcUY1/O1v4DhzNHMEfnRYqP0hYUGfjsgTP5jH20QzdItfiqeuLn6w1aRzopagdxMi0DTWQWvW7nVk8xG/zKvBE7UpOAtOfvqaU87+Es0N8pL37IoSZnqEywaCPRVjtBdSnrUuudRKaBTJLkGWDm9368e9VSBxeRWiL0YJ3sBjSK6kA8xARfmgQPQ2aqSSDkiWBWKEBl4oIswmL+YEoo0/RzkpAzI59MU70JuTikbbkfrVA4MoPu4Ae4nVoKGe9+aNVTBk45Sk3C+Darz5vHZoTZUMZtRqcfW8gk445M3Uj84gHXkyXD2d7kIRo1Ltw88+TOE6FIgDAA4UGwzCgWn9sx6WETwncU2dx999XVHueoqbL/fXZ/g2FZEFrOMIVqWLtc8SUR+pmbUcgqQLd4hU16poBOdKBNjL+K8XWT2suyGD87eZeKnG+UBHx+102Y5fpdtbFvSNcT9sJ+yJU3NfIZ2QJsZKPTJDTSnvOmhhoeofFjpFVEX6JGyRvBB+vGrGA3ul8in 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 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. Signed-off-by: Aaron Tomlin Signed-off-by: Marcelo Tosatti --- mm/vmstat.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) Index: linux-2.6/mm/vmstat.c =================================================================== --- linux-2.6.orig/mm/vmstat.c +++ linux-2.6/mm/vmstat.c @@ -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) { From patchwork Fri Dec 16 19:45:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13075345 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 4E4D2C4167B for ; Fri, 16 Dec 2022 19:52:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C781C8E0003; Fri, 16 Dec 2022 14:52:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C284C8E0001; Fri, 16 Dec 2022 14:52:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AEFF68E0003; Fri, 16 Dec 2022 14:52:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A09D78E0001 for ; Fri, 16 Dec 2022 14:52:41 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 69C5A1404C1 for ; Fri, 16 Dec 2022 19:52:41 +0000 (UTC) X-FDA: 80249216922.10.118C3B1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf04.hostedemail.com (Postfix) with ESMTP id A1F354000B for ; Fri, 16 Dec 2022 19:52:38 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=LVsLHdJm; spf=pass (imf04.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mtosatti@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671220359; a=rsa-sha256; cv=none; b=lTXCew2ltSrblWb2xWav7fiKZDDtuiMZdkrfjFlKHpMBKdqZPxWlEU+aHAss4dQXJpeYMC wbpUxw/ok1JwUjCmZVZ/zZDycrjHURprtF+HLFtCXz0+Hqiv9qGQPuFgnexwMYHwvdggox 3/gxOS3EX6VrBAAZZaPzqkk8XktXt+0= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=LVsLHdJm; spf=pass (imf04.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.133.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=1671220359; 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=9wyWU7q9zcUtfqIAPMNd6wk67vz331U9cm7/N6A7Uqs=; b=FVR0pm/xqYZf9Lj4bDyXlx1VoaIlNlbZBA2PTSrkenLcKWm06v9micjyNfJO+bQP4GrJ66 ysV8NDppKTdSnT+Vlu4pePFV159OYJ5i/hwNjDT4SuZg2hxKSjs1x8eUEKrm3lOZZL52ff flMNxW8/678sGYD1AEMov2B1Z9u0QyI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671220358; 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=9wyWU7q9zcUtfqIAPMNd6wk67vz331U9cm7/N6A7Uqs=; b=LVsLHdJmPCCHQURK/SHNVu4reUstQm6cJ765vUf9BA6Oewl/OT9i5YbO1V9BUv6h7Gcu+1 qffDzImoE28i6M+rv1qMyy25+aiwX6f75aifQllbqmOk0b/glc/dgt+8rdTg/RYdoG22GL pDz5BgcGBZ/7eul6NBnKFeVaLxNCwuU= 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-264-59YoxDBaO_aCqgO6Mf_Ftg-1; Fri, 16 Dec 2022 14:52:36 -0500 X-MC-Unique: 59YoxDBaO_aCqgO6Mf_Ftg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 28D2D2805585; Fri, 16 Dec 2022 19:52:36 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-3.gru2.redhat.com [10.97.112.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6250649BB6A; Fri, 16 Dec 2022 19:52:35 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id C7D304269CA80; Fri, 16 Dec 2022 16:52:10 -0300 (-03) Message-ID: <20221216194904.115519799@redhat.com> User-Agent: quilt/0.66 Date: Fri, 16 Dec 2022 16:45:42 -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 v10 2/6] mm/vmstat: Use vmstat_dirty to track CPU-specific vmstat discrepancies References: <20221216194540.202752779@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: A1F354000B X-Stat-Signature: 8stmosdh9p8pshj68paqc97c1dtnfmyp X-HE-Tag: 1671220358-521742 X-HE-Meta: U2FsdGVkX1/9uUNPvwu6uQIQ0b3mHKXKxjTDw+//FKJi0j1qtnurmamRElK6CsjN2jKdzYp7s6louZLxc2qsg+CFYAzp2/mkUHadwXg/SmtqSWO/5hxu9gjtnV/7aVvcOTfuVch6vuI1e3Wz46R0cDRaYfB0fkqLIf2w7uB9yNThCZbctbVlC9kQHS89g4rohY3qdZ71bAu7k+zREER42BZQ2EGSD7uH65gsO2GSo9F1l+Df6qje1/EgdOZc0S6fIZnf2owPWNfH4s+Vfr3AZM5xQyQ6kUbep0LJVfjCYtbKDCdlZ814HiaVN6tgBfc3MFj4yZsSaRxpIGc6qmE0BDs6DN76fPaMwc3JJ92a+F/GefzbQQoXGidyfO7DsMxLKH+gj5VPY/uGWrGZ1HWr3wwsmA1NIhhzWPM/grGN56Z8QwO9++rX+6tcbqG+PwtArR8Qg0WGiDZon8Rz6ZqYlfb/0gLsF0u4vZ+PiAAb+is5gZJ56NXjo0vaIBuOJ/ex6nZaPCneOuRHcY9qbH2UvA7ZhLFCdigmItE1O4eWliX2jtjJfXMCjZuJyZd0gzMvMBukrwr2A0NIQ3hsfYHhrorAut2tIylm9UA7vTZBRTs6QxFiJKZAhtMGnWAsH0tRAhJJEykPAzN8gZKRlAsHDyJ+05IRfALlK2LnNbq/j/GvO1Um3+PKYIHQtLnjGIOZUrfrCpdrhrsNR+Qan8mCIrBT6Rn4bhSKPwx13NIj7YmpvJTBJ25ChyXJQ+oG/Q539oEb9reylyr4Wvk+7k3a+kEHpvsPaQWL1gKW7FHAlfwb3g6JpN8TaPymWBLBl8HM1E1Ax+9jJSN/Ucdmz/KnPwdFfUfc6HZGztFJis05U4g1FqLvD1sPSrYhNj1vLphWfEKB3GgYma7QInhBaO7rKh8nMmo6tQB4T65VawNzbFs= 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 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/20220204173554.763888172@fedora.localdomain/ Signed-off-by: Aaron Tomlin Signed-off-by: Marcelo Tosatti --- mm/vmstat.c | 48 ++++++++++++++---------------------------------- 1 file changed, 14 insertions(+), 34 deletions(-) Index: linux-2.6/mm/vmstat.c =================================================================== --- linux-2.6.orig/mm/vmstat.c +++ linux-2.6/mm/vmstat.c @@ -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(struct if (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(struct if (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(); From patchwork Fri Dec 16 19:45:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13075348 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 4D74AC4332F for ; Fri, 16 Dec 2022 19:52:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACA3C8E0008; Fri, 16 Dec 2022 14:52:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A7C5E8E0007; Fri, 16 Dec 2022 14:52:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8CCC08E0008; Fri, 16 Dec 2022 14:52:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 7C7C58E0007 for ; Fri, 16 Dec 2022 14:52:43 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 59E1E1C5F7A for ; Fri, 16 Dec 2022 19:52:43 +0000 (UTC) X-FDA: 80249217006.26.1FA983D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf23.hostedemail.com (Postfix) with ESMTP id B3EAE14000E for ; Fri, 16 Dec 2022 19:52:41 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=gXeJEvIz; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.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=1671220361; 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=z8vp/u7LZUrRwGre1Iv9OOlhsfTbo/8utVSvhDfhbTk=; b=0djAY49Bx68lNz4KBrnMQ4546Sw2QfL05f5xm9inrXxJL2wtuai8e7GaiHIlL5B8IJ7Ffw VNVC6JPVpsy4+oTP7ATA1nVx3E7xVXNZOxOM4/jU541191Q6TqbsOEFmbQ7vBo+UobPyBB LKGqXKKLJQOxv22qFu7VLCcYzM5sG/E= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=gXeJEvIz; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mtosatti@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671220361; a=rsa-sha256; cv=none; b=DzVadDmG7dy5CC3bdjUzagDF/FO8/M1aalUvl406J4sU7/7qojuUNB5FZzYBXHwhu80KDI ITpBLkyUYt37vHlxb9f/yTIpV62weIaprtwlJVwIEyyGF3NCQDTsK9mmIPYWMknH5lhrKk Wnxdbj80U/VrKDFWrM5pt/NUpHjGCus= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671220361; 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=z8vp/u7LZUrRwGre1Iv9OOlhsfTbo/8utVSvhDfhbTk=; b=gXeJEvIzvyiRcL/YDTcYwxZ4AcYgkeXGdvRABxBpm6vTtYhsOzTra6kOhUiBkGc3UixUXH Kt9cG1DfLkRkhwhM1J6PhIyUMUQQaFVA0ludnpqYy94ITqugUZ27LCkgCj9r2Y4fGyFuK7 l60clsdO1+VyJRL4Es4KQ1SYWe6h1h0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-202-XXBE8xf3OdemLz0OFcaz-A-1; Fri, 16 Dec 2022 14:52:36 -0500 X-MC-Unique: XXBE8xf3OdemLz0OFcaz-A-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 49BD585A588; Fri, 16 Dec 2022 19:52:36 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-3.gru2.redhat.com [10.97.112.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 64DA753A0; Fri, 16 Dec 2022 19:52:35 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id CAB8E4269CA81; Fri, 16 Dec 2022 16:52:10 -0300 (-03) Message-ID: <20221216194904.155675758@redhat.com> User-Agent: quilt/0.66 Date: Fri, 16 Dec 2022 16:45:43 -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 v10 3/6] mm/vmstat: manage per-CPU stats from CPU context when NOHZ full References: <20221216194540.202752779@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: B3EAE14000E X-Stat-Signature: w9c5hahnhiom9xpy8ohyizqzf613n9t4 X-HE-Tag: 1671220361-265273 X-HE-Meta: U2FsdGVkX18myRAUJrm7nyG3772a6qoYZuTC3UuUYjVRfi7SIAEUil6Jlm3T7V+jWBsfHYBXPbj4NvrdGIy0NHoKMCFPPCDzfR+7QyxdVUZbhsHVHv4tSbmoMkBI+FxruqqE/DZI0vLnd7A4DtHGH3DPQDiBYn675Z5+47SWNdDbhchdVsAIG4jnBI5S9+nBLxpbQgSCV7/AzgIm/FBsd06Q2P2OtMSxpelVv+O8IP0pKAE9iLb3VaXhPKpdf/HvGyReTJ+RO/+f0uZm2jDGf1cEJnybiWxisC3+jO/Ka6ntPjjX0J2U4eQtzKALndbW0MFxOvIXlMm5qWBXzxizw6PjfWwP5ivojuBl1VBrSRSTTIm2QkUzOVFFrTYZBUGrrz6JVifXU8gQYPgFL/JxtIQiDmrb8RqpVYBsoyTBHHyvLOXBgps21H0W5/BhxaQVkCh2K3ZrTLriQ9Yo0cbJha0Re4coILj7sDsysaCXHfPXIvUgybE1xh4fYG+wgEqRJfhBa3XNyp1NYbUdxSt+P4l2M9kRbpYITlROzKGi5c862BUWvZ0gMP602/3f1K70uxB6Y4wKOOtC4CP8y+0+OSoMEau/fcN5iI4/0vzVQ4o5sBvDZSq3oLVSDKxoV+P4dD7FuEDDnzlfBtbun8SG0oyK6JeUuaCaUoWbvDPztsDR/NmmtCP2cpcbhfXLMRUjyp6NjYC0ZTd25nPfgsuQuzwKIopti1Kbk2YKfHYwc/qyCBFGTOWPQUbE9vxesudUS86cXJvIu5w2I0KGAXLCGCMAveAQWXlTngG9dzJA52GbmBHdOhQfH7G6ZEGO3YsTnpU+vdJ4wq48wGiu1Au2w2nqOJg+d6h9XzaA5jRfB0ScqWLXLAZOdJaIRm718YkB 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: 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 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. Signed-off-by: Marcelo Tosatti --- include/linux/vmstat.h | 4 ++-- kernel/time/tick-sched.c | 2 +- mm/vmstat.c | 41 ++++++++++++++++++++++++++++++++--------- 3 files changed, 35 insertions(+), 12 deletions(-) Index: linux-2.6/mm/vmstat.c =================================================================== --- linux-2.6.orig/mm/vmstat.c +++ linux-2.6/mm/vmstat.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "internal.h" @@ -195,9 +196,26 @@ void fold_vm_numa_events(void) #ifdef CONFIG_SMP static DEFINE_PER_CPU_ALIGNED(bool, vmstat_dirty); +static DEFINE_PER_CPU(struct delayed_work, vmstat_work); +int sysctl_stat_interval __read_mostly = HZ; static inline void vmstat_mark_dirty(void) { +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER + int cpu = smp_processor_id(); + + if (tick_nohz_full_cpu(cpu) && !this_cpu_read(vmstat_dirty)) { + struct delayed_work *dw; + + 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 this_cpu_write(vmstat_dirty, true); } @@ -1886,9 +1904,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 +1988,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 +1996,19 @@ 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); + dw = this_cpu_ptr(&vmstat_work); + if (delayed_work_pending(this_cpu_ptr(&vmstat_work))) + cancel_delayed_work(this_cpu_ptr(&vmstat_work)); +#endif } /* @@ -2009,6 +2030,12 @@ static void vmstat_shepherd(struct work_ for_each_online_cpu(cpu) { struct delayed_work *dw = &per_cpu(vmstat_work, 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) && per_cpu(vmstat_dirty, cpu)) queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0); Index: linux-2.6/include/linux/vmstat.h =================================================================== --- linux-2.6.orig/include/linux/vmstat.h +++ linux-2.6/include/linux/vmstat.h @@ -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_state static 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) { } 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 @@ -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; Index: linux-2.6/mm/Kconfig =================================================================== --- linux-2.6.orig/mm/Kconfig +++ linux-2.6/mm/Kconfig @@ -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" From patchwork Fri Dec 16 19:45:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13075346 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 7A329C4332F for ; Fri, 16 Dec 2022 19:52:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DC4408E0006; Fri, 16 Dec 2022 14:52:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D4D718E0005; Fri, 16 Dec 2022 14:52:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB3A58E0005; Fri, 16 Dec 2022 14:52:42 -0500 (EST) 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 AC4E78E0001 for ; Fri, 16 Dec 2022 14:52:42 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 8A6C11A05C8 for ; Fri, 16 Dec 2022 19:52:42 +0000 (UTC) X-FDA: 80249216964.30.D698816 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf15.hostedemail.com (Postfix) with ESMTP id DB1DEA000A for ; Fri, 16 Dec 2022 19:52:40 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YjaXeekc; spf=pass (imf15.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mtosatti@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671220360; a=rsa-sha256; cv=none; b=vATapVfn/nC+mPKvg5dQstyEusdrW+IbleGtw67C5cRkgGMUr1Xl9F/SafGQwpEXn7gBcE rpQNyNVVK1fOfPzTte7mCJHqeR06HX569v8B+/PquxFvkW5SD9PMSoci91/I+4uz6uT7zF DFg2S0mZGuavEHkfCoY1nCnuvkOB5vE= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YjaXeekc; spf=pass (imf15.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.133.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=1671220360; 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=Dj30RymbV1ceM4Dy8ttNJR7SWnDvhwByWmjv10/VX0U=; b=u1oUWKWxuOlap2P9rzQEfOmtUdzds+spYoFwGR3m7pmf9CUpPivVTP2JyS9YeIOghjB+mi jtqHTVrmhcWJESJK/FiVkrfrFbmbMf/dqKuWlGxHfiB2vBZsNI6gJ0GhqeRNbtk7QaCy3I 6vAWj1n9wuR7q6YCWlubdKZlUMWAY48= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671220360; 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=Dj30RymbV1ceM4Dy8ttNJR7SWnDvhwByWmjv10/VX0U=; b=YjaXeekcJTS+v18mECAtDSLFsFwWMRcQmQSiXDBKeOt4DfEuaOwYA2UabYX8VxCMN19Fbn KPEbGHADJdGkr45obqN/eFQN3yfqloliiDRISDJ3VlLNhuVsFAqzUz8Gf/O6BEsyWfup4P JBIeHUulTp80EHM13OxoJ+rfY/TIuPk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-60-JsGNMvx0Nm2XLJKR7vCj_w-1; Fri, 16 Dec 2022 14:52:36 -0500 X-MC-Unique: JsGNMvx0Nm2XLJKR7vCj_w-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 28B64183B3C8; Fri, 16 Dec 2022 19:52:36 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-3.gru2.redhat.com [10.97.112.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7479440C2064; Fri, 16 Dec 2022 19:52:35 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id CD8CF4269CA82; Fri, 16 Dec 2022 16:52:10 -0300 (-03) Message-ID: <20221216194904.194285953@redhat.com> User-Agent: quilt/0.66 Date: Fri, 16 Dec 2022 16:45:44 -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 v10 4/6] tick/nohz_full: Ensure quiet_vmstat() is called on exit to user-mode when the idle tick is stopped References: <20221216194540.202752779@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: DB1DEA000A X-Stat-Signature: b5dhphgw554bwgx4b9kerpo4otzk6ed3 X-HE-Tag: 1671220360-993607 X-HE-Meta: U2FsdGVkX19X1nw6kthCZqqwM/Ny1uIMItC4huFB5FL0kjwnD6G0moxi16i+3zEBzvZywA2EYp2Oc1awJKGYERwgeQzx3n53KkJqII2cTNZr/twuJdYEHq/fRDkbzVd2adJA5tj5DIppgMH3CLaEwrmgdqIO7nHK/b4ZYYzN0ja/JoGKmX2WGRy1KwEmlxHDGqLCQ+HlVMNPgN+YqZgRonHfFp0ckAWR4k6uSjGAcWM8Cv0sAKw4NrcGZ+ajUrwjehp87omFawFVRvyGRMEVoZqIPZDNlbkjLlabpbz+/F3oyegTD1Bpe4uMOckfhWbmx3v/JLjqk1Gj3snJeDbG9JUi+pebWuiXn9Suyt04fbar/qFob0fQu8NuRD5DBuTI0AUanXEjuIo2EB+Xx8Tps4FOx8YFjyxzF+w/+5RYCzyTzwHb/EBR4qJEFRcAUhzMqcVGCsXgSKjWzoJKuZVv/QNEfZK1n/Hh7o6gCGg3SZfDfacRxXiTsGRPcAB55RFsf0q35yjDy2WMJln8CFU5fRXQAZ3o5cAmm0D6heyOHfKolVNTA4lwXodnazgUi29M2lmW4kssOamCeR5XkzTnAuVRzwx7ZNIwtYKtdnI5TXuTyZhvaFJhKPp5tgpoTgs/FEulbzXW//BF/ghgXezGg4BotIiusCFPTWzsgKkles0tkQXv4nHauYGiVo/iW9V2lj7hsf07OqRAfWEDIBMipJd2nqKYEpQ5F07SvGGw39XtxgHeYQIa9rwPa6AbTJruOWs0xNHHP17/Rx6nbwteN7p0u2l1+dzZhjSnksEVVsIv79x3FbDMncF7v304BB4QMbfJB7a9kncSUYss0wMFB4Lxi1dP+f4OCKg6nq0KNdDpUVbsa83/m/u2a6pWA92lG18fVZiezRVtSwYprXDcqXqbW+nHuZVMAQXa0har3V4= 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 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%) Signed-off-by: Aaron Tomlin Signed-off-by: Marcelo Tosatti --- include/linux/tick.h | 5 +++-- kernel/time/tick-sched.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 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,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) { From patchwork Fri Dec 16 19:45:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13075351 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 1408CC4332F for ; Fri, 16 Dec 2022 19:52:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 59C3A8E0007; Fri, 16 Dec 2022 14:52:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 52497900002; Fri, 16 Dec 2022 14:52:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 216C98E000A; Fri, 16 Dec 2022 14:52:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 106AA8E0007 for ; Fri, 16 Dec 2022 14:52:46 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D2C70AAB73 for ; Fri, 16 Dec 2022 19:52:45 +0000 (UTC) X-FDA: 80249217090.02.0D1700A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 3D9C640003 for ; Fri, 16 Dec 2022 19:52:44 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=E7jda8yl; spf=pass (imf11.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.133.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=1671220364; 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=XMdjLmtzP5L6H9Cd/6sMe3g0xhQEAqoO6Fwkx2+y5AA=; b=us3iYmOmsr3lTkqv1jewunSamhDAw9tGBhPyyvWO7GhAMSZCevP415DB3hUgJwcGtjDa8n Vu9DmMgDRAjjnWJxMKAVitAP3WYj+Bz11xaECK73kka8Xq1wikQqAahXHkUZ+fTYsRdhre bp3oxzB7oxSeXIw9wNgrnbv/wKIjl2M= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=E7jda8yl; spf=pass (imf11.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mtosatti@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671220364; a=rsa-sha256; cv=none; b=OW/JAcbOUkWfjcZjP6/QKcB8pEOgqTJIgBO915ZOG2oesvv9gFmgdOzwnUb0wvOIXDd4e6 ACRQ8tNqFx0jUxuTnz7ajXJTVNXzewIJQEoDdc+BVq/JXnfdAOB17rLJiNnmrJMKo5oHvo lCFkMc6shU13wNg0jQF3Ji0jE1efUdM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671220363; 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=XMdjLmtzP5L6H9Cd/6sMe3g0xhQEAqoO6Fwkx2+y5AA=; b=E7jda8yltaa4wxUXe+kSZn/AAvXn5rYS+DJvFai+nbLDdEbzxY2SkgAWQ50ZOq8o6T6xkF xZHlNdwB55JcV7Cc9Sc3xxfPF6gZfbcuUvLyUX/msAoHRbWo4OYD66ca9FR0iSV4oSzfrK z8XrGRzicNBnhtldIjuchB0PluSpmyo= 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-352-T_pEuXCDMzKMDR2dUVU_3A-1; Fri, 16 Dec 2022 14:52:38 -0500 X-MC-Unique: T_pEuXCDMzKMDR2dUVU_3A-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2E5622805583; Fri, 16 Dec 2022 19:52:38 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-3.gru2.redhat.com [10.97.112.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C050814171BE; Fri, 16 Dec 2022 19:52:37 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id D262E4269CA83; Fri, 16 Dec 2022 16:52:10 -0300 (-03) Message-ID: <20221216194904.233941906@redhat.com> User-Agent: quilt/0.66 Date: Fri, 16 Dec 2022 16:45:45 -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 v10 5/6] tick/sched: Ensure quiet_vmstat() is called when the idle tick was stopped too References: <20221216194540.202752779@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 3D9C640003 X-Rspam-User: X-Stat-Signature: eosn7re5nw77tymdgbhjm7mmsmchwkf3 X-HE-Tag: 1671220364-137850 X-HE-Meta: U2FsdGVkX18HGm51Ak4A0fUMmskA0Y6UWwUsEHhiw7bRgg5+8Vu7y8W8IywGgiPQNp2s5YKjZhYD5gqR70/vppoMgX7bC9aNDqTRsJAnfr1FULejnDc5rfbga70ccmwD23Th2u8ykm6PF08XTKBVcXJBS98YSwee43FWx0ts2WhP72EAw2eIB12gWchgnK8RHqN6Q3KYM76cnT2Udg8HFJRCvL8e9du52PqWUkANmveo/clLQUzxDeRd+Nl9E6T9FDf9lkzq/g7VBodqBfiUzp76KaKGW3RyAcpHEwSBegaGXa+LbR9ypd85Lx6lLbkaddEFR0re0v4UNqAJHN8yYMsD0Ip7GVoI56NGwxms5IRVTYnvrrlthKGHQv8qMTOBOKUYGPkzuz1hIXa5SrmYsvdOXpZDKspjxE4/le8QjgsrTH+UW6INb+amHZmYFX7o46vmQmCvO8+v3sBy2WZqOYajWTbH6EnQz/BsnXzY998QKW63DVMTUotLtokvAGIkIM6qFkZ9LFS52cStfXlpZSlop5eSxTzR+mXtkxOoaFKhG0igMDvM+Ds9W3/BYhMmeE9KXcnuakGCSlkvcKfgpd/PDNb75WzXfHff7MzZHFZKmYKibuEAsQd0O3s6/dYcfTorcz8T5FFsk4HBlaNF9p7XHyiPc+57qqK0V8EeuTmcfXO9PSilTZ5RJM7WJr7BwjzBcigCgfgyn0NrSzbagsDCO4m03VPhoXQ0ae7oUeOuukx4eF22i2tRlahfiuOPEgtHS46DyzYG0kgtU8pYzjhdYNczQ84yx9qdy62qYo/NFVye8903CKvp7y6eX2bSsfXrJuKPAmtXR+0NsmQ7mR0BrFNuiWOEO9jQhXbXK2xjaIwRXiMFzsDNLZI9v93oa4UnE/ga1M1wy6/0dEtRuzf5NAMsKLLlxEZ8Lye4tjg= 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: - 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%) Signed-off-by: Aaron Tomlin Signed-off-by: Marcelo Tosatti --- kernel/time/tick-sched.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 @@ -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; /* From patchwork Fri Dec 16 19:45:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13075349 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 CD94DC001B2 for ; Fri, 16 Dec 2022 19:52:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C6458E0009; Fri, 16 Dec 2022 14:52:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3785E8E0007; Fri, 16 Dec 2022 14:52:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F0528E0009; Fri, 16 Dec 2022 14:52:45 -0500 (EST) 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 0BA8D8E0007 for ; Fri, 16 Dec 2022 14:52:45 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id BDDE2AAB73 for ; Fri, 16 Dec 2022 19:52:44 +0000 (UTC) X-FDA: 80249217048.10.C85D03D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 3F5E420004 for ; Fri, 16 Dec 2022 19:52:43 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XRl4Lomw; spf=pass (imf03.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mtosatti@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671220363; a=rsa-sha256; cv=none; b=3GTZSLpttxknn2SGOzOQI/VoCNkStHsvM5ktcFCeWA9hZN8pSk+TSiNQVkOyBBDDf1A7zV ywtoFJfUDiiTfJQfQS0L5oIt7tusZ40leNV/TWwpNAkJjQVBqDtaIxN2iDmgMSxegxjaH7 N5Ig/ArOBPHN9x/1FDLL1Pyd5xiOKKU= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XRl4Lomw; spf=pass (imf03.hostedemail.com: domain of mtosatti@redhat.com designates 170.10.133.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=1671220363; 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=8S7mt7lE8e/IJlnGX6npC3VGrMTwvxTRAvd6yzbBL9I=; b=XSHavczBhR5jLgKyOvxTqUHX+wdZINJfq/oFDRyOeGmDEQr09fQBtwQcfgQ4AutTxXpeu+ cK4tFXOyE28spwCIpuS+9/8BHEzVT02ag8mN1OSnHOaHU/uqlG/BmAz0jQ6DrNRDz/Pv8A sxzMdigwNsukTY9dPAs5J//pbjBXZSQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671220362; 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=8S7mt7lE8e/IJlnGX6npC3VGrMTwvxTRAvd6yzbBL9I=; b=XRl4Lomw6mJ/Kz/oPV7licJtKs30SU3weGJGVPyP7QFuZYQxI5jROcM+x0jtc8/7sE6uWY xBkxAegQEVmGFbOi9vkJugpNuLEuEJhJj4GhDUmPXozf18sQ+/fbFMMQRKltsu4tLz/O4Q o1EzMsQHrHfHpZuEGlkDycbA1fTHb2c= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-322-VaFJA5p6PziY1me1vqiQcQ-1; Fri, 16 Dec 2022 14:52:38 -0500 X-MC-Unique: VaFJA5p6PziY1me1vqiQcQ-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 05030183B3C0; Fri, 16 Dec 2022 19:52:38 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-3.gru2.redhat.com [10.97.112.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C2F491121314; Fri, 16 Dec 2022 19:52:37 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id D7C7C4269CA84; Fri, 16 Dec 2022 16:52:10 -0300 (-03) Message-ID: <20221216194904.272106293@redhat.com> User-Agent: quilt/0.66 Date: Fri, 16 Dec 2022 16:45:46 -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 v10 6/6] mm/vmstat: avoid queueing work item if cpu stats are clean References: <20221216194540.202752779@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Rspam-User: X-Rspamd-Queue-Id: 3F5E420004 X-Rspamd-Server: rspam01 X-Stat-Signature: a7ntb1ypb1u5swo73rdo5ywoia47in3w X-HE-Tag: 1671220363-748800 X-HE-Meta: U2FsdGVkX189GVPuXuNhWL8y4YYDFW9cMVke7dlLE66mbV2tANuWx+60LDYffWWpGuj5GcMXejB8mCJ9dTr/GFdKUsGXzr2TerHYZeJ3vAxiT8po6vxMLSRysJoPNvbEjCPqYVGX4MqSlLA7GQdJgaybQ/J1J6Y3mtxXn6pGZmbhLNe0UWStAn3L/8PjF6Zsriq5bdpZ65S00tSdnqiJFSvN92jSgxR4V0KawZXPzDAU3qRfci+sYkqL/is9nTz0FdL2GDCiZAz7kqq9unWFu9kie9nthJNRUZu4+Lm+HyPCYI695NnCBFY/j9L7a2HRmfU9iwjJgq3aE3n9Y8y4YSkb2p9ZPj01+FNYMWzg9Fe7cOXz907QW17ziwtYb2hMAU3SE4V3xgKxAAJxtwipNgUX+2OxNjlvxY8p3U/Xm+rwQ6iAwdkvU0/0bwA84EeLlscAa8/yq2DBwDaFFeIG9LOUi+kithO8+Ik/uCQrQkRotdsaSKHDqZdtZ4HQrJabZzW54EIPqzTGPEHtWNf8MQERoSe8xvSRHvBIy/dfj/Xo5iTHwAlSs+BzoJ4rLlXnwFyLtxGw0v9XUCmSVx59Y8C7YIuCu8znF5Hq6413bghKKUp3MrcCFKoT3qnB6zis7Y9ppKdeZvQFn+9s9MjrqU/wqPZ2oncupsKrMREGXQ9svY5uwTY65/iBTbxtM4MD1F09LUxWUfjXiUAYsDdEx7YA9Cf2ZDJpMi9puR8TdsqETZmCpQcYnX1X2YMoCk79K5xR8Cwi4H1U0u2PZI59UJlifLXNOpcbCcWaelTavl1bKBhoDqtyys0KiXFzJYgVDLCYCwuT0rPanC5qibbWyjT7hEFq2zn5tSoMXAGBBvdCN9B/fTlxzlRUjyx6wqvt 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: It 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. Signed-off-by: Marcelo Tosatti Index: linux-2.6/mm/vmstat.c =================================================================== --- linux-2.6.orig/mm/vmstat.c +++ linux-2.6/mm/vmstat.c @@ -1905,6 +1905,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); @@ -1914,8 +1939,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 @@ -1929,9 +1958,19 @@ 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); + + INIT_WORK(work, refresh_vm_stats); + if (per_cpu(vmstat_dirty, cpu) || 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.