From patchwork Thu Jan 5 12:52:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13089802 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 5CDEFC3DA7A for ; Thu, 5 Jan 2023 12:56:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7DECC8E0002; Thu, 5 Jan 2023 07:56:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 790538E0001; Thu, 5 Jan 2023 07:56:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 656F18E0002; Thu, 5 Jan 2023 07:56:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 5676E8E0001 for ; Thu, 5 Jan 2023 07:56:45 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 17CA0120289 for ; Thu, 5 Jan 2023 12:56:45 +0000 (UTC) X-FDA: 80320744770.18.8BD72E2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf22.hostedemail.com (Postfix) with ESMTP id 0F99FC0007 for ; Thu, 5 Jan 2023 12:56:42 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QO9hWfDO; spf=pass (imf22.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=1672923403; 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=A8ptA+mtJpfx4WDFilLLKvFD1fvtEmfC0YGkopBqCFA=; b=O+BWm0WbUsJFc+hBlgUceZ9iy6IScpknBXtrIDAHI/11Ci8yAvTr/Sa+OfKo/XfrmjavLm zMXqDOASD+Wq0O2F3ZD1g05yToqYl6JqIRQ1W7I8lSQqDPgLoSA+3PLUXORFOnUIeb7W7Z AvC69gzOTcZ1CjpfWdBOJWkMCyb6lGU= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QO9hWfDO; spf=pass (imf22.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672923403; a=rsa-sha256; cv=none; b=RaLl1tFGWIm31jVo3c3qT1W8kWZDfYBmNKxSMVYPryp1WIUqo/Vss7jnfS6z0j2R4JG8jG HmzVlmfIX9X53jkfHTuOBPufbAJLyj7iTO8BI+0jOPSFt5guafLSH+XVjbMRu/ShiXcvTi +cBuRCbAIQPKFBh9J5bAKei+YhlPPxI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672923402; 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=A8ptA+mtJpfx4WDFilLLKvFD1fvtEmfC0YGkopBqCFA=; b=QO9hWfDOW9DrhTT+wMQN7/rZOBEhPnTfAjcEFxRLDNa+zVNB074oDuFvJfWlH4S4Xbsvd9 720Og5Ux3c6d2d6LkuqcKpsXs/wEAcXbUktYqPx2Hlg5ZuK7abi1bcZ3w4yrVZaTiRLfqa TlY1rgsg7SO6DcRz2kY2i3UBK13FOtQ= 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-115-ihzkEaxQNQebANYux16DrQ-1; Thu, 05 Jan 2023 07:56:41 -0500 X-MC-Unique: ihzkEaxQNQebANYux16DrQ-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 9D881101A52E; Thu, 5 Jan 2023 12:56:40 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-2.gru2.redhat.com [10.97.112.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2F20349BB6A; Thu, 5 Jan 2023 12:56:40 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id 9695340502F36; Thu, 5 Jan 2023 09:54:47 -0300 (-03) Message-ID: <20230105125248.772766288@redhat.com> User-Agent: quilt/0.66 Date: Thu, 05 Jan 2023 09:52:19 -0300 From: Marcelo Tosatti To: atomlin@atomlin.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 v13 1/6] mm/vmstat: Add CPU-specific variable to track a vmstat discrepancy References: <20230105125218.031928326@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Rspamd-Queue-Id: 0F99FC0007 X-Stat-Signature: tu1d9z6sdky6zigjgmnpf79gqg7p7hqo X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1672923402-411423 X-HE-Meta: U2FsdGVkX19yQgqx41GX/L/qoL6rhtbnQWDn+o24dEnC/w1zqOzoMsvjJIa5sY0GxV9RJF/DWgazJxkCpqtneqeEabEDegnRClU8SHCMFSf0M6+LGWUJ52sXU89h2EVYDNXMqcogGYWX6mcDpBOYbW70s/EUQ+MJ7tDf5TgManFB7pZLMpDhYMORNqMs/1TsknD8drxQ0bBut5FNhr0mK8TkzRGxK+c0KB3q3DMLWZjQLn01AmwbtPpzzkD16Ow2YuK/LbxIGd9zIkm0Dzcar4RnzZZc68ot6HxXfzYzRRUa8wBMav80fCgQmhd8Yz3cSPX1FmGhMmeeAVmn86eVEYLpoMC3cbNzaSME6Pr2sVGX6wNMBg79nFGjLYyNbt81VoxIuu59jHtP1UAh/EFvS8S6G1it5cdqCLGSXSswBSV6zECbR1OcGos1ocjXgNLOtFsHrrUAD++gIN6YlrrR3DBtHtd37XDttcMCxpkMJDUA3pd67eTbLPR2vaFJTSWyRBqmGmqXz5IeUh06gbX8w2wKjVBJUjmEoEVmG5u8Qlfrnq2SgkI7w6AzGVPTDl8ybD95uiEKNrBf5qjl4OJAdmPgiVMlb0OZCrKVsynnikNGG112bF6ANawAho0ncHJ2PfQCmJpdClDCFdu1cIjJ8e8q5s0Fs6FGOIYhBmUyqCeeFksUn/jJ59fpuf7nd5yoOsxyVjhU39N+cZ1y/Elr+Uk1J1yFkKemt7ABIdx2kWauGdpqDn6p6KYpZVnNLD3lReooaRgSon1P1Me8jhMIZr5EwI6O8MofYzAv24T9Ui2kjiFiO5Mn8Q2ER1lgtpPTXvWGVS0CeCu0m7paA7IJLk4of6bYiEDI0D56bpcO5yVsrlp+cTi9AApKOXBSvEW6 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 Thu Jan 5 12:52:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13089807 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 3861DC3DA7D for ; Thu, 5 Jan 2023 12:56:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 25332900003; Thu, 5 Jan 2023 07:56:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BD0B900007; Thu, 5 Jan 2023 07:56:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D325B900003; Thu, 5 Jan 2023 07:56:48 -0500 (EST) 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 BA930900005 for ; Thu, 5 Jan 2023 07:56:48 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 7EEF71A0D28 for ; Thu, 5 Jan 2023 12:56:48 +0000 (UTC) X-FDA: 80320744896.30.AA4A621 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf01.hostedemail.com (Postfix) with ESMTP id E4C9640004 for ; Thu, 5 Jan 2023 12:56:46 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=e0KcPP9w; spf=pass (imf01.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=1672923406; 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=AWWNy27PnDV2BzrRi18CQGy2pR4xt8zt+F15YzdMHas=; b=B2h20QVETkFrkIMGOpSM/tESFfftutBkipV1sWztM+ZmA/hbyfdDFr3v8prd6enKH3tAlT vjsB81bcmsPC3TMozR1jhXXp7a3LOiYKkoTbhg/XgGS4g9ZUwE3BTOHPGvsLBSAK0K5WQk 9mrc6vBNTOghgngcdYGrFgMcKpqgxDc= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=e0KcPP9w; spf=pass (imf01.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672923406; a=rsa-sha256; cv=none; b=o5te24S/3gwuhn1NBkuWjMwoikGZ19y+1RVrbiRrN0AtyosEfYi8QQ9PjoWxiWKQWiRP4T eGOlbSWjBJ+cxc0L8f3C83bCmKgY0wXM2SFvY5W7Ztqc0nd7vr+vxePcbWUZyTMdxgM2oQ XHQgn+dtWXDEYPAf+OeyMRsVvUOE/zI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672923406; 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=AWWNy27PnDV2BzrRi18CQGy2pR4xt8zt+F15YzdMHas=; b=e0KcPP9wqxdWVV0zbR0iSSuB6efDhXeY/iNAAryZCN7Ikg4x5q2t7g7rCJSS++XRJVX6JE UIAnpXmkoPH2vqJ3I2lZSHaCBUnEMysbNioGxwxFzU1U5DfMSJ4X3V/9lPlC+YQpO6rD68 45Ptab/DoQ1UKXnF+ZLrWuH2efBEo7Q= 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-191--cpSVXu9NI2oKV4L1YUfng-1; Thu, 05 Jan 2023 07:56:43 -0500 X-MC-Unique: -cpSVXu9NI2oKV4L1YUfng-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 AD5AD80234E; Thu, 5 Jan 2023 12:56:42 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-2.gru2.redhat.com [10.97.112.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4916153A0; Thu, 5 Jan 2023 12:56:42 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id 98FF240502F3A; Thu, 5 Jan 2023 09:54:47 -0300 (-03) Message-ID: <20230105125248.813825852@redhat.com> User-Agent: quilt/0.66 Date: Thu, 05 Jan 2023 09:52:20 -0300 From: Marcelo Tosatti To: atomlin@atomlin.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 v13 2/6] mm/vmstat: Use vmstat_dirty to track CPU-specific vmstat discrepancies References: <20230105125218.031928326@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspamd-Queue-Id: E4C9640004 X-Stat-Signature: mw5xnt5b8mpaq4iqz9o8x3njxmteigk4 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1672923406-389981 X-HE-Meta: U2FsdGVkX18aofFnymHD2a4W3GEeY/V5e4vvkmvJUA4jec6r1lYLf5IUzxkljJaXqqPRbUTtNlER6KRpY2zhXybFDUQNsgcRJUhRSzJB+m+5/W8/206UgtOFzf4yTTqNSBrHLjKYoG6WE/UQhyUS7uGpCMP74saH46S30KvyJsf6VQRWQHjZ6dxxpwysYrhEEyZk1wdQczS0aomTrl7vZkDroV0lXtlYHnxAM7pqbbPDqdlqlpQqr0SCB/mdHxo9pFxt8/K0TlkC+GlGUDm3zZ38pduJIq5y3DUdLJ3jdjHx1SkKyye/GxtdNQADcwodqpRMVqNdZW2C50P3P05WbBKtKP1U06LgkXJ7fFD6k2Y3jWTYdb5Ar7WIsv+J9+Xc0FMrt/WnWhyOkL55EI+h9vzbLcPy3TtnCvNdWB1cLd1niIx8j3EFnlplOGkOuJsi9haxNEWP6CUQo0K6gUq3f6SOFoTk9iuYSZtfYD31mVeqUCnnosqwX8GD93Ip/ZD2ZQfjFtQybv6YZEFyv6WFlEk9J8AgPFaL0fv2k1byIL1NxN7/sAgWrv45g9+QhhlmY8D1cMFvkYCJ2NFqkpMoc9qHlxSO6jhvVAylRe8391FbKN5PLBUe1drmNKlqJChtopYbkqOjrLpc3zsJmipphjzy3ehMu15xyQvp7aHGxXwT7p1DZ1UCM2rVwt7bpxAXzVp+e/hUwtSWCx7HaIwITXs8GpJ6DsD5OWnZ+w6FcHCzOO/ypkkQ6lmJLP3+nPl5LfThB0jnPEG5pKYsjHF4GK7XN9z1brZTqjE5BOpZ3z0qdquKGHCAchWMHBc7P2Hey3/eJhjlMAOmemN6L1Tf9DeBpXOOL+pxNCwfbi5iZwVslkxqNWq18FvgVhRxxqddmUeM8EvISbaPTYorzx+xYA42Lxqm86l5kZn3QygHqRg= 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(); } @@ -577,6 +579,9 @@ static inline void mod_zone_state(struct s8 __percpu *p = pcp->vm_stat_diff + item; long o, n, t, z; + /* cmpxchg and vmstat_mark_dirty should happen on the same CPU */ + preempt_disable(); + do { z = 0; /* overflow to zone counters */ @@ -606,6 +611,8 @@ static inline void mod_zone_state(struct if (z) zone_page_state_add(z, zone, item); + vmstat_mark_dirty(); + preempt_enable(); } void mod_zone_page_state(struct zone *zone, enum zone_stat_item item, @@ -645,6 +652,8 @@ static inline void mod_node_state(struct delta >>= PAGE_SHIFT; } + /* cmpxchg and vmstat_mark_dirty should happen on the same CPU */ + preempt_disable(); do { z = 0; /* overflow to node counters */ @@ -674,6 +683,8 @@ static inline void mod_node_state(struct if (z) node_page_state_add(z, pgdat, item); + vmstat_mark_dirty(); + preempt_enable(); } void mod_node_page_state(struct pglist_data *pgdat, enum node_stat_item item, @@ -828,6 +839,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 +1976,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 +1985,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 +2016,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 Thu Jan 5 12:52:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13089808 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 25772C53210 for ; Thu, 5 Jan 2023 12:56:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 91A55900007; Thu, 5 Jan 2023 07:56:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8C89A900005; Thu, 5 Jan 2023 07:56:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6F4AD900007; Thu, 5 Jan 2023 07:56:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4B26F900005 for ; Thu, 5 Jan 2023 07:56:54 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2BA63C0D68 for ; Thu, 5 Jan 2023 12:56:54 +0000 (UTC) X-FDA: 80320745148.28.DDCBF47 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf21.hostedemail.com (Postfix) with ESMTP id 8CB2A1C0007 for ; Thu, 5 Jan 2023 12:56:52 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZKG+LHlK; spf=pass (imf21.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=1672923412; 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=WjySYZGPU70OrR1lv/LMrVt9W5iPl/r1manuHwTbfNg=; b=kyeQYGSEJzGeuWbKMNiYNKbl1gCEtQL56kHWtC6FQIjfB6GG+9PQSvjzq9vnaWYa9LM5TJ u1SyB4Grs4GM8psJYFy60yoqJzMT99Brbg8IQ6wEIQlgpkWkbWR2uV1CeRfgt3ssLfatRq M58lU868cOrI76mhga0E2vynL2soCac= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZKG+LHlK; spf=pass (imf21.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672923412; a=rsa-sha256; cv=none; b=LHC3AlR3Goy/eh3oQIC12skStNTcpa2eA3Nx40XuA9PJp8BACBLpcz7mfkTtviCiwiNDCr +oJm3+4vHWBy6jdCCOerohOcYGffIh/YonPrWXnK74J0EDQKtCpbtz/u885XF3xAdNjJ3Z AEVICH3W/VqAgO4+DcaYvlBAqDuuf1k= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672923412; 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=WjySYZGPU70OrR1lv/LMrVt9W5iPl/r1manuHwTbfNg=; b=ZKG+LHlK5NbWfyAl6GsmeSFcJ6FVoxHHs0l1+X5ucX2ws57LG4aMMT+diBaxkjeCAvJfPR J7edsvIyJCGn7jS10m1HJRLcSKTm/25HOQhoy7rhaWu0bY7IntRaZ78lYNy+d3qQzBnYSi aDz3PDMr5duppkxe2h8cF8DyZ6uss6M= 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-669-kZwppUtgOjWtuR5rl6C4SQ-1; Thu, 05 Jan 2023 07:56:43 -0500 X-MC-Unique: kZwppUtgOjWtuR5rl6C4SQ-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 ACFB78588E1; Thu, 5 Jan 2023 12:56:42 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-2.gru2.redhat.com [10.97.112.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 45C36492D8B; Thu, 5 Jan 2023 12:56:42 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id 9B65340502F3C; Thu, 5 Jan 2023 09:54:47 -0300 (-03) Message-ID: <20230105125248.853465707@redhat.com> User-Agent: quilt/0.66 Date: Thu, 05 Jan 2023 09:52:21 -0300 From: Marcelo Tosatti To: atomlin@atomlin.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 v13 3/6] mm/vmstat: manage per-CPU stats from CPU context when NOHZ full References: <20230105125218.031928326@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8CB2A1C0007 X-Stat-Signature: 5ufdue75qy7yo6j43w5j14cwifx3dkkp X-Rspam-User: X-HE-Tag: 1672923412-987361 X-HE-Meta: U2FsdGVkX1+0x/gdtUY5uKBfmXy+mfcaQooQkNW3mptdcxwkKZFuRf6mcsQT4rVFBUIMPJxRXlRdX6V0QzWeJKqLCtJhz+lz9wE2rW45aqeeFeW3tJcHcnwk1JXutOBgbtDQ4wcDowusoC/p5xM2cXv1NVDWrSGwzSIGW7yIoh1mqN3N48VGuXv4fIxLaIS10BtmHsq8wCrxem0RmY3RxAu8/JBleGzfxNnRYMW1O7txHthqBVljxCUXYv3cj09ay/bTIeGTRKLxxuwm2BzBWGqnlrgKYa1hw+9G09kNxmUGm8EtrtyKc2olPG/PmuJ+jh4s/zdgRnbwaVVmptmYQ/yUuG+5njL8Bj5RlU/L+SI4CtR6cNm9T1vZc0AJcJEl3mOgXEKtLWyxjRSWYd/jvDFKwKZ3D7gHnjYXA+OvEyRDIjgjeeaUh+rMKjoRIXuy2fLQ0tuS1F6v7+AEF8/r7jwaFzvCPxhyhDrAtE1UsHHtPfWFfz42KkU0rgyCxIVNeCfI39plX3/fWpaY+t0sxMxVWt/jMS1sPZSXhbNWD34D/ycz89J4Rw8khmsaa4jngXjWtinPjSk1NWZv0sWqDh6bZyk4iU2Uqyc6FvcwYhoueb1f86MdRHIJW37dmKfA7Uun1CjXqxozGn7jom7fWe8hto6bTArmm9uUlVZSx0yYA1uxaU1gMK4ReYoWSj2JIi97EZvJpSkaRVEXQx29PKSWeySAlnrANcXEp7RTP6C80jLCyVyGa5pT3wXdbKNAsfuQuGAjPbJvMZPPi8rHa1BSms/5/RsvCxD0DrwH+CJb12e/f6RNQ2QmKBZyCKL1Ypbb9AzZ4X18/n4Clf7tEnaLUVbwiY0FIrw/mow1LsLK7EHx8oqa6tUid/Pl+U5w 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" @@ -194,21 +195,57 @@ void fold_vm_numa_events(void) #endif #ifdef CONFIG_SMP -static DEFINE_PER_CPU_ALIGNED(bool, vmstat_dirty); + +struct vmstat_dirty { + bool dirty; +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER + bool cpu_offline; +#endif +}; + +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; + +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER +static inline void vmstat_queue_local_work(void) +{ + bool vmstat_dirty = this_cpu_read(vmstat_dirty_pcpu.dirty); + bool cpu_offline = this_cpu_read(vmstat_dirty_pcpu.cpu_offline); + int cpu = smp_processor_id(); + + if (tick_nohz_full_cpu(cpu) && !vmstat_dirty) { + struct delayed_work *dw; + + dw = this_cpu_ptr(&vmstat_work); + if (!delayed_work_pending(dw) && !cpu_offline) { + unsigned long delay; + + delay = round_jiffies_relative(sysctl_stat_interval); + queue_delayed_work_on(cpu, mm_percpu_wq, dw, delay); + } + } +} +#else +static inline void vmstat_queue_local_work(void) +{ +} +#endif static inline void vmstat_mark_dirty(void) { - this_cpu_write(vmstat_dirty, true); + vmstat_queue_local_work(); + this_cpu_write(vmstat_dirty_pcpu.dirty, true); } static inline void vmstat_clear_dirty(void) { - this_cpu_write(vmstat_dirty, false); + this_cpu_write(vmstat_dirty_pcpu.dirty, false); } static inline bool is_vmstat_dirty(void) { - return this_cpu_read(vmstat_dirty); + return this_cpu_read(vmstat_dirty_pcpu.dirty); } int calculate_pressure_threshold(struct zone *zone) @@ -1893,9 +1930,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) { @@ -1980,7 +2014,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; @@ -1988,13 +2022,19 @@ void quiet_vmstat(void) if (!is_vmstat_dirty()) return; + refresh_cpu_vm_stats(false); + + if (!IS_ENABLED(CONFIG_FLUSH_WORK_ON_RESUME_USER)) + return; + + 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)); } /* @@ -2015,8 +2055,14 @@ 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 (IS_ENABLED(CONFIG_FLUSH_WORK_ON_RESUME_USER)) + /* NOHZ full CPUs manage their own vmstat flushing */ + if (tick_nohz_full_cpu(cpu)) + continue; - if (!delayed_work_pending(dw) && per_cpu(vmstat_dirty, cpu)) + if (!delayed_work_pending(dw) && vms->dirty) queue_delayed_work_on(cpu, mm_percpu_wq, dw, 0); cond_resched(); @@ -2049,8 +2095,36 @@ static void __init init_cpu_node_state(v } } +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER +static void vmstat_cpu_online_rearm(unsigned int cpu) +{ + struct vmstat_dirty *vms = per_cpu_ptr(&vmstat_dirty_pcpu, cpu); + + if (tick_nohz_full_cpu(cpu)) { + struct delayed_work *dw; + + vms->cpu_offline = 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); + } + } +} +#else +static void vmstat_cpu_online_rearm(unsigned int cpu) +{ +} +#endif + static int vmstat_cpu_online(unsigned int cpu) { + vmstat_cpu_online_rearm(cpu); + refresh_zone_stat_thresholds(); if (!node_state(cpu_to_node(cpu), N_CPU)) { @@ -2060,8 +2134,28 @@ static int vmstat_cpu_online(unsigned in return 0; } + +#ifdef CONFIG_FLUSH_WORK_ON_RESUME_USER +static void vmstat_mark_cpu_offline(unsigned int cpu) +{ + struct vmstat_dirty *vms = per_cpu_ptr(&vmstat_dirty_pcpu, cpu); + + vms->cpu_offline = true; +} +#else +static void vmstat_mark_cpu_offline(unsigned int cpu) +{ +} +#endif + +/* + * Callbacks in the ONLINE section (CPUHP_AP_ONLINE_DYN is in this section), + * 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) { + vmstat_mark_cpu_offline(cpu); cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu)); return 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/init/Kconfig =================================================================== --- linux-2.6.orig/init/Kconfig +++ linux-2.6/init/Kconfig @@ -678,6 +678,19 @@ config CPU_ISOLATION Say Y if unsure. +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. + source "kernel/rcu/Kconfig" config BUILD_BIN2C From patchwork Thu Jan 5 12:52:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13089806 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 D1760C3DA7A for ; Thu, 5 Jan 2023 12:56:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EBD87900006; Thu, 5 Jan 2023 07:56:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E951D900005; Thu, 5 Jan 2023 07:56:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C72E1900006; Thu, 5 Jan 2023 07:56:48 -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 9B3E6900003 for ; Thu, 5 Jan 2023 07:56:48 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 696891C08E7 for ; Thu, 5 Jan 2023 12:56:48 +0000 (UTC) X-FDA: 80320744896.27.91880FC 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 CF74F40003 for ; Thu, 5 Jan 2023 12:56:46 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=b63X5ef8; 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=1672923406; 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=hIc0vPvNI1VykGTxKET9i/kMr1uqEk68mXFfOdiRry8=; b=obH2kSV1xTwWOzsn/8LbRJLy9Pbv5pi8kruc4NlsH/sshBU8zP20rmNxIhyq4SpvxTZHlo ersUboZIALHYt09kP0UnvMXMCn7uPRDGuXTP+c5GIeFuGXoi/26pi2+R9sC2p0LakEQGZ4 RjnhCWahGAEKWTa8YwUf1TEOoWQEDp0= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=b63X5ef8; 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=1672923406; a=rsa-sha256; cv=none; b=F8d2UstMVgrwrOr1UYm0GJBaZr7owmWZl3V6LH1ppbXlHGGcWQcYWDck6OYtNsQlVJbDzB 7gZ07uY931T9bnmhiFy49gsQICpJKy+WB4qWuyfS99dFeMxOsxnKoULBvlWqGrFV7G+ZIs thC3WJplG3aNAGQQfZA1cX6G8t9mSoc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672923406; 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=hIc0vPvNI1VykGTxKET9i/kMr1uqEk68mXFfOdiRry8=; b=b63X5ef82Ne7rvWCJKbYJ6G5r8rK0A9jN4LP6vmchMg03NJNTP97JhZH1515paae4gurkZ 5wEZ2yNiHSlWB1JUjGniwpbeWDy1kehLFnU9qFpDiluRaQZCbPA458hXwbF92sivkp2WTA VFToAYtLcoWE5cfz4HqXBWcTt2RjzFM= 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-564-weQCRA3ZMGSGIUvUlcJqnw-1; Thu, 05 Jan 2023 07:56:43 -0500 X-MC-Unique: weQCRA3ZMGSGIUvUlcJqnw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7B67B3C0F425; Thu, 5 Jan 2023 12:56:42 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-2.gru2.redhat.com [10.97.112.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 45D0340C1141; Thu, 5 Jan 2023 12:56:42 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id 9F2C340502F3F; Thu, 5 Jan 2023 09:54:47 -0300 (-03) Message-ID: <20230105125248.892336104@redhat.com> User-Agent: quilt/0.66 Date: Thu, 05 Jan 2023 09:52:22 -0300 From: Marcelo Tosatti To: atomlin@atomlin.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 v13 4/6] tick/nohz_full: Ensure quiet_vmstat() is called on exit to user-mode when the idle tick is stopped References: <20230105125218.031928326@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: CF74F40003 X-Stat-Signature: tsjjpxaansenrw5auis7hhyhpjobgnm7 X-HE-Tag: 1672923406-302199 X-HE-Meta: U2FsdGVkX1/Id+OTXFvMRJUwiNAmjEyNXl6ueqddl39Z/fAsopfZczsL4HWhBdr0dHkGMfSdFb5A9Avta1qXN6DLsj3pJJnseOqj2UNNLn/iV5u11x8Ju1GS+A6lylq8IvQ5HkwEndFlI5wzlLYIx0I3E3nuxhKJGyM45pHubyIDU8zyjaBnxfoiM2Y1oWiEDwEwA6dXDaFr9fSs9ZhUVIqRLYF384eE7Q14VoHtAO8iMPrLAXvLUF+89pefH0MUK9n+KLFcZG8U5tkceMSDDS9t2iS7h2j3SBTKofIcdJwhM0iSWTgXamcsCKou1tHbbSpsXxzS8TjBRsjIG2klw8385Vx+PFLeCwEX8qGjp5q00Qa7JacsOThFKT/Bwbr1Ab8h0duKhzYIlP46t6ng2hWEyWphktVBir4Ptk/QVG7dOx6RibFaw+VOeW6mQQRAKwPU9/ib1dCnSwCGqzg4wR0bZPovXiZ3KknRUkTCRSJB8jovR0lCqO54t84ZlNROsFpdeAQrt3y60MHFfxqYnYr1vUoO2UBUoMnB+PiHPyh8HxpOK1VGBpEUcbzxXeWP2XfzAWMWCm3R+pRMoKndeBqOkjQAGtcKWLudlk/6TVsowY//fceCm+KKKo58UwUEayqImu3jgbQN+toCItHcU3Xy3l0LuNCG9INlK03rYl2tfYclIFG3X4nwLLiDz3ylf/atAcbmBfnrWNDwcr2hTAckWTku9rEuQ/EntGXx+fK7JWtQz3AGRABF9SfjM59d6BTJqZvcI7ieDFZMdpgzDxgPi+a/BEEHn5NUtA0N3L6Nog5H60iI225xFqmSF92HE6CZGFlMIELv77sSdNP/mRlTiFBbTGC2zv63nv+cmsdef1OjxPxfTXIXLVunKr8YHiNCUG+24mQnpmY9Wq2F1mkYI1C/k68Ez6qQpsuBL9Y= 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,23 @@ void __tick_nohz_task_switch(void) } } +void __tick_nohz_user_enter_prepare(void) +{ + if (tick_nohz_full_cpu(smp_processor_id())) { + if (IS_ENABLED(CONFIG_FLUSH_WORK_ON_RESUME_USER)) { + struct tick_sched *ts; + + ts = this_cpu_ptr(&tick_cpu_sched); + + if (ts->tick_stopped) + quiet_vmstat(true); + } + + 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 Thu Jan 5 12:52:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13089804 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 D1087C53210 for ; Thu, 5 Jan 2023 12:56:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 365838E0001; Thu, 5 Jan 2023 07:56:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 28B57900005; Thu, 5 Jan 2023 07:56:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E45BB900003; Thu, 5 Jan 2023 07:56: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 CF6E08E0005 for ; Thu, 5 Jan 2023 07:56:46 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A536A1607C5 for ; Thu, 5 Jan 2023 12:56:46 +0000 (UTC) X-FDA: 80320744812.12.117B805 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 06EA814000E for ; Thu, 5 Jan 2023 12:56:44 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cpxtDrm0; spf=pass (imf09.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672923405; a=rsa-sha256; cv=none; b=V43B5hoew92XCfVqX1q9wmbJ3ZlB5um27ZSLE0WvVFUCz3XYmPD1ZqPDhWk8CMMZ/QhE22 elCrqcrRwshyBBGrDtfGd7wyOywRfFhKO0soe9Ln2SQT9cmLxsnTYqxLhz4tDCVD0RbPnf grCY1W2n3fjlaAVzXoMOLyvx9jUvz+Q= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cpxtDrm0; spf=pass (imf09.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=1672923405; 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=iri/rWZtdhFxtRyLVTm3Z4b/XpaXLxRLi7TX4havHys=; b=EmEJBugyv4qKVlhixoOYNzi3pTSXi2Y1NOv1ohlwXuLyT0AlWiaBkUNCyBDHY9w+Fjr/kh ncxwbNtM8azWOkvhHkaCrv9nJSIKGsB8Mi/T0fesL7c4ZH4qqyAFJP0xNtZRJ4YMLDJioT zgx1SyiOirc2MA8bUYs+mFOY4ki7Pmg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672923404; 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=iri/rWZtdhFxtRyLVTm3Z4b/XpaXLxRLi7TX4havHys=; b=cpxtDrm0QVftga6ROcKcosligsP0++iuWml7PggMA9RalXA6vAoGgRbfxzAkos6TpoWRKf PWibWQOGxQFrktuovcobVX9cFjgw2In8gCc7oMu1vrYNlz1FC2yd0oche1oNyHEfefEK8K O+oxgbIGOE61e6D5BCygUir6Vp3yF3c= 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-63-GnKdm2ZoO8ub5NKyh93f9A-1; Thu, 05 Jan 2023 07:56:41 -0500 X-MC-Unique: GnKdm2ZoO8ub5NKyh93f9A-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D0030811E6E; Thu, 5 Jan 2023 12:56:40 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-2.gru2.redhat.com [10.97.112.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5B937400E40A; Thu, 5 Jan 2023 12:56:40 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id A376E40502F44; Thu, 5 Jan 2023 09:54:47 -0300 (-03) Message-ID: <20230105125248.932725463@redhat.com> User-Agent: quilt/0.66 Date: Thu, 05 Jan 2023 09:52:23 -0300 From: Marcelo Tosatti To: atomlin@atomlin.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 v13 5/6] tick/sched: Ensure quiet_vmstat() is called when the idle tick was stopped too References: <20230105125218.031928326@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Rspam-User: X-Rspamd-Queue-Id: 06EA814000E X-Rspamd-Server: rspam01 X-Stat-Signature: i9igoff74f9qkji1hjszimiow1eufwjk X-HE-Tag: 1672923404-600566 X-HE-Meta: U2FsdGVkX19o6sxX1DY6s3/QESoQe66OdDgfJmpDzLnjuYVPRjL25mr2Z1VLHK9jHrhdXwbzEIYDiqN7Mzr8tTscssrx2zEdMxOwD4aEjcNmBJrau8Fo5q0ICs0+iTrU31miyyjh+WJ11Li8zg157rrQoUHJvNWPNErlLQm6HjRdztrmn9XeC1WZcw/sZvlbfzbjwAPHC/mS9BrxfR9/eoGjrdyqtdCbpB76zAbOEUqpoMfyLAfaAA9z9DJHcCqqVyuhGFjg6Wz6tOU9+dA9j1kfAe2f1XtezrxaT889nTJpR1Vy6xqJCD22e84aYThQAj2fiA4rFswJGPiCse2vjd/IiRJkZZtE+HAs9sqyLsafxavEKDPpbk49li2QcbYx6PEGLR4YfS0tXl960IpQes1qSpXfuK1JWnX7shOWMLKGSTOmcx3EzmAPCUjLNu/gy+5Cn+k5/mGUs3kShjBfMNZ/7mKNL5cm8bUWPkcxYR44kA2cN1RR018aJkM3GZ9EJZ2odxKrQVmfuhI8YY89tAqLA8RriLdLVzDxiK5qNOyR1skHc0FNBAQGPF+2c8Mun0UGKHlihZKEy2weNpDZrBTtZkm9AQapigt8G4IEDm1BrPBMXdqTlfhPa+IJ4X/QsO3B8zrVWt96CKBBy3zb4nhZDXo7jpzjJHnGyHhkwcPXwAt8TyZDXkKZYUWA55gkThbXMo8uZl0zTPJTu8beaVMW0Fx5EFoWuvPXG5EV+1Y1w+/VUhB8jVsJ/iLS9XtmeY2QlAkJUr1/1s2YPq8F786mfEdaggX0296XXznDLmNhA/YdH/Q87MyWCr529sfEy7EuJtkaHxZNE4ZnwuwYdSaigp8MHQ7/4gzmYOw3Q3zQuFBPYwVVtgbuPZ9y0nC2xE2uFuDIG+JgbvjrYd+7YO9enJTeLnPSd260Tp+hICo= 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 @@ -929,13 +929,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 Thu Jan 5 12:52:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13089805 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 56978C3DA7D for ; Thu, 5 Jan 2023 12:56:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 65FDC900004; Thu, 5 Jan 2023 07:56:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 592DD900003; Thu, 5 Jan 2023 07:56:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D5E3900006; Thu, 5 Jan 2023 07:56:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 0FCE7900004 for ; Thu, 5 Jan 2023 07:56:47 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C616340C9E for ; Thu, 5 Jan 2023 12:56:46 +0000 (UTC) X-FDA: 80320744812.13.02C32DC Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 28B0D40004 for ; Thu, 5 Jan 2023 12:56:45 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MJE2ZRuJ; spf=pass (imf01.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=1672923405; 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=C8n9DE0ruCcCCK7cY6ZY83QBYvGpdJ8qqUIL+bP6IWU=; b=RcbuMJ+PS7+AycecuFcxdeEAiV3cdeH/k5Chekn+0ZWdxRMhTvhVN+TqDgeK7ECzW5xux8 7YtexnPFp6L0XqAER/pH6bD1sA0u2CBTTL3AVDvRYQodP2LlVSFa5qke1VTmf+DNoCE673 QYYCGzXkbnSd8/j86MVwrQA9Lcw3PsE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MJE2ZRuJ; spf=pass (imf01.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=1672923405; a=rsa-sha256; cv=none; b=e2lYQwU3d8ug++h5FPQvHnn6z0+jJ3oiCh/JbI2YHAcZeLrGxxaY4l5QmtjxThmrci3wOI NT8GE9iMbTrAoHv+jLJ3ISVmWXBf6cmeBSRoW1hZDq//DL2kUTP/pJNqlCXvgK+JOhhYtb CN3+iIu+QC0biYLciDoNnMdlrVDLyuc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672923404; 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=C8n9DE0ruCcCCK7cY6ZY83QBYvGpdJ8qqUIL+bP6IWU=; b=MJE2ZRuJLAKEd6/4/ypjvTcV4oHeHCXYJghxQiceC/Eo8dHTen94zSh+sn+gK9JmVWsqgV F+c3VDTnXgHhgRbFeNUoyD7VBigG3ZD2CkTpnVq6kZ5SN48TAhcY9sAAnKlVt459bI+cRU DGo2WIS5le0gYMRuGtp+b1y2JIZy9lU= 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-120-zOn_0ctvMduyru0HaogF3g-1; Thu, 05 Jan 2023 07:56:41 -0500 X-MC-Unique: zOn_0ctvMduyru0HaogF3g-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 A49B8802C1C; Thu, 5 Jan 2023 12:56:40 +0000 (UTC) Received: from tpad.localdomain (ovpn-112-2.gru2.redhat.com [10.97.112.2]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3529E53A0; Thu, 5 Jan 2023 12:56:40 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id A7E8340502F46; Thu, 5 Jan 2023 09:54:47 -0300 (-03) Message-ID: <20230105125248.971432211@redhat.com> User-Agent: quilt/0.66 Date: Thu, 05 Jan 2023 09:52:24 -0300 From: Marcelo Tosatti To: atomlin@atomlin.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 v13 6/6] mm/vmstat: avoid queueing work item if cpu stats are clean References: <20230105125218.031928326@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 28B0D40004 X-Stat-Signature: zstbqom3etzoa5casyu376ezqp1hszd1 X-Rspam-User: X-HE-Tag: 1672923405-30864 X-HE-Meta: U2FsdGVkX19uRBo8X4FFh06xKaPdHbfgcbY0NbjLNKH9szPIfgla6dUQOxqkItus0zG5MnCFfijKyaOHIiWboAA0YhEgmJJ5Fh4ZAhJuLEeJ3mLnklw57wKqXsAJQ0T+NA/46pXhcjkhlrPHWWUSEZ+xn81sDQDUFj2v8FSV3hB3wjRAn1odsoZQZufz1z3ajjsSLUdakdxbmJEigVk7BjlYwM306yZ2ivTvA5Nx7rXOVHU+wub0vziOvppnhPFhv/eAlteM0EwhnjzT5k/9ORPHiEOK3dGz8fXzZglaE1qieDxwL1Sa2B6llJNTaDphnA3PlZJ5Sr89i25ZYTAWT24oNmFx4pfVyKcqhHYvSDH/SCzKFKry45cuWTSCXmYR90BoVQMZRuIICsgntoLHfamVURxU92MwKXELk2I2Qffy3utwZh9V0+tQkD76XQGuHiK62vgK17go1Zzx/IFUGNR4L6FJ2w8pycLCfKeN4iUYYMS1EvlLymFJtrnyzNzlrsiveLbL/ObPsQ0S/Gw4RJ7qW9HXehYcsQCedYJ/QlcFXYVoI6uA+gQro0V7RsWky1/PqIZHKDY4U6pkIXkwe2mijf5hf5vEfcDlA2KDXSdNYNXrfHFNb6faiF9z3U0waOZYnySpwtW6CartmnMH/ovckYVSZuNxuvVpoDiASbfb+7Rk1UYRt4tGoDcjiw+oc+maRTF5ZvA5hjsRstPyG0K9zV0CG0vVatoE0WLVjxjWIEyMrjuKvttgcAnEepgWR+M06EPaygEE8FFrXEo1y12w0MPiy10Hh9+lIko8xodnLEPUNDrlMsKLPRXUc6qojVbiH/VJ0v/suu6If1xbeMX6Ckxv3XFZkYwAK9D8QaShXjVghJ4gYQxSyi3MPoDh 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 @@ -1931,6 +1931,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); @@ -1940,8 +1965,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 @@ -1955,9 +1984,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.