From patchwork Tue Dec 27 12:11:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13082274 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 34A29C4167B for ; Tue, 27 Dec 2022 12:36:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C50B8E0001; Tue, 27 Dec 2022 07:36:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 444FC8E0006; Tue, 27 Dec 2022 07:36:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 136418E0001; Tue, 27 Dec 2022 07:36:38 -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 EDC708E0003 for ; Tue, 27 Dec 2022 07:36:37 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C3CA140BAF for ; Tue, 27 Dec 2022 12:36:37 +0000 (UTC) X-FDA: 80288034834.09.8F96FE3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id 3FF161C0006 for ; Tue, 27 Dec 2022 12:36:36 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=P1lucjZq; spf=pass (imf18.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=1672144596; 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=hzmUsXXXU+ZXZgqGgDkSUHFsv9wBgV9HyxfbwFvVtmndvnk6Lj0MNUq0JGIj/X4Bh2As9W besFKMLuM4bH1t1SShNq+KNX2YFz3zymvBy7Yx2Cyw1wON4fedAvcIa/AE1Yckh2xTjiVR rYrsxUJaoOYjSIrbZquOvc/Se2OSt8E= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=P1lucjZq; spf=pass (imf18.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=1672144596; a=rsa-sha256; cv=none; b=fXsm/mMV7BkMu0DlT2TR/FOB+hJ5Ne3Hc/0BfhuMkCo000MApofMWoc6vIaQ4IKVlUHkEr VXTay+47pO3XHanipcpszZd7pw8hQKQzVxQyHe+kwdDyXjGAgOkBuRf/5052DFDchb+f6M aCkHtbzA2KWdqsenYp73bnYFLxfe3nU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672144595; 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=P1lucjZqSHng4BMKDHfWovm2puO7XSK2+wHkKHb4502oxWDmLCL+VnWZD3V+KNJRUScVrn WQpYfAwUzRuwquLpTpp4smql/R5LCu/A48vblaeUCB1wZ6j/EsmOCPXAOpNg3dpcz98/Uh o2ggPNwrR063i3WAdt3KMRAs99K5DKc= 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-434-dM8Hsox4PtyMTRv7NbIZzw-1; Tue, 27 Dec 2022 07:36:32 -0500 X-MC-Unique: dM8Hsox4PtyMTRv7NbIZzw-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 ED6432803D61; Tue, 27 Dec 2022 12:36:31 +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 7AA29111F3BB; Tue, 27 Dec 2022 12:36:31 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id B6A704052AA30; Tue, 27 Dec 2022 09:14:52 -0300 (-03) Message-ID: <20221227121341.940831921@redhat.com> User-Agent: quilt/0.66 Date: Tue, 27 Dec 2022 09:11:38 -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 v12 1/6] mm/vmstat: Add CPU-specific variable to track a vmstat discrepancy References: <20221227121137.102497574@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Rspamd-Queue-Id: 3FF161C0006 X-Stat-Signature: c1erzmscasxp1q1euo665p1wserujanm X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1672144596-358980 X-HE-Meta: U2FsdGVkX1/LE7Zcx8ZZnzS1OCyuxG1ZHVV7lxAYPbBQG/xuKe6vdDrsiACvvUiiT5Qlj+ufGN5MzZK/+Q+FXidnQZefeuq9klT1p9Ej5yzj9QrTTbkzI/nV/KEUdTMMa3x8Oftmr/kn/OL0EGFhVIUQnA9B3e/VvOLQtA8ThnhaXPSIXvSzFxTMqwRWXgIZRvF4qmn/yYSHkLiHSx6kJXMUAwibBCqVttAVC4j8KBQE0Y5pIKE4fsW/dGl7xkdoEMdpBskrIsrQTX/8AS91qKRFV7muBCLf4j6qEzFrnqSmS0ulkSmHuaWujXnBNoYBk1bWmm9uJ44Fggoq9EgJchh2fsH5rfJNmb9q/icsZPithlEuBbaSCYdgG2yjqXBRJJjpoup062eVH9eQL+6HIc/eD4w4vaMIImTjxn1iG98Bn6wOGzMBy3k3I/cQ0FpYEpnFoz/FgNSPZyh9Zonqu3GHdRixVPgZY1TVkq4/V8zq+ZwaGlJK0S1RusSRoWOc8ex+fobivmB9CotrpzR7JdQB6HCpa60AEn8HVk4IeTYHqkOlvFxoi6nx3delwNjGl7U83Pl9mn6mW44RNqGZcl0sCBynJWrUJLJyK5AwywkI1MrzeUznddhLJgIG7ev4tx8vgF8MuiUi0hZOD+E1I0YcVX8jFUA0mUm+lIMU6Rw9yDnl0blEhqYUUSsgwBiM3UwqAVx1ZwWrbrMACT3UdZQCxouPAiQUOziIfNsuB946dYz9wfjJ+lR4byzjTIgt8iMCrn0gFie4c4DGoZ0waxfvAL2heNc/Kq3FMxhLd1Mu2YpAG4OspulByzNOEug0drX9Fh8En5s4zMdJ1w364w6Ky6nFAhwxwa0vARo+ltQ7e2UjF+5SwZaFkYobnOBc 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 Tue Dec 27 12:11:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13082276 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 15020C4332F for ; Tue, 27 Dec 2022 12:36:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41BFC8E0007; Tue, 27 Dec 2022 07:36:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 37E8C8E0003; Tue, 27 Dec 2022 07:36:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D1708E0007; Tue, 27 Dec 2022 07:36:40 -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 F0EE08E0003 for ; Tue, 27 Dec 2022 07:36:39 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D0330805E8 for ; Tue, 27 Dec 2022 12:36:39 +0000 (UTC) X-FDA: 80288034918.17.16217B7 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf30.hostedemail.com (Postfix) with ESMTP id 40D488000A for ; Tue, 27 Dec 2022 12:36:38 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XeCec2yN; spf=pass (imf30.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=1672144598; 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=r/LpF1VkdRdOhfpQs0+SAaS5Z0AIHcjMWCSMDWY5FIc=; b=bV1BpHMTYPp627NiYslWQ+SGDSU57ohUwpBG9c7d1EUeAgbmrZ6wzk6jfQa7PAlwqjXehT c26+RNdBLPG465kkcoRfkKI5w2O613hWXMTvZMa+mrZN37qWgTjl2vVwLlS7DPqOgurGaH 57IreI4ZLHvGdxjWEukPQKzGngTbKOs= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XeCec2yN; spf=pass (imf30.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=1672144598; a=rsa-sha256; cv=none; b=uTWDiWqIWoWivaE3FJwW9bDqcyDyVcJiI997llrpXhhWjNQ0gu7oDPNm+JorgWE6BX5YfU h0mrg9tnJy3kxyNXMtpLGl1Y4T6nDnMcGm4faniJnMjte7zFdGyglQzrB4bhJ+nyXjmYxm CYpnKfrGLhhBLhNuCRfzIzSbOBYj8PM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672144597; 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=r/LpF1VkdRdOhfpQs0+SAaS5Z0AIHcjMWCSMDWY5FIc=; b=XeCec2yNfKUCy1lNzImG2sckCMpXggdHjfpy6xFR2nDtvQjz3NaZFJsWhsXS9/3rhx/FM/ KfIu8qihfegMfOvk9SZPPFvdJEYRsvBkvRymYE/5HKhVMEVZM/gXUqECpDkye50g/kcv5r psW1FQ3dJhtnqio75uzeMoxCf6i/g7Q= 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-364-9zq5xs8cOyORDLSMFE_AvQ-1; Tue, 27 Dec 2022 07:36:34 -0500 X-MC-Unique: 9zq5xs8cOyORDLSMFE_AvQ-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 F40DD2803D63; Tue, 27 Dec 2022 12:36:33 +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 BD63A140EBF6; Tue, 27 Dec 2022 12:36:33 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id B8EDE4062774F; Tue, 27 Dec 2022 09:14:52 -0300 (-03) Message-ID: <20221227121341.981235373@redhat.com> User-Agent: quilt/0.66 Date: Tue, 27 Dec 2022 09:11:39 -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 v12 2/6] mm/vmstat: Use vmstat_dirty to track CPU-specific vmstat discrepancies References: <20221227121137.102497574@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: 40D488000A X-Rspam-User: X-Stat-Signature: wwncfzwr7gyfz1cibxfwxzkjhskuzz4u X-HE-Tag: 1672144598-404247 X-HE-Meta: U2FsdGVkX186GoGH9aY95k26IC+/A0QwTQ4Krww2GsvPvSiIt3qGjvImN3f/dMb3PFGJMW4oGfrMpRrEc3VN4iciDLhnuEAEGkO9MPeDf8DR7jmahMx4xWt81t+FtKdQC3TuCSG4Zb/McjOzvQJJ6+w+Wb7kj8ERVxsh2bzgdqZTgMeMo7NII0NaVI8f5UV1ovEbWzHRQo0Fpt5QLKjtxsEps1d9Hvp1aAjmYhSZFmau0wPBELuRiTDJren2YLQx065KnLF+SLDxKy2fZNEqYfKd7QjEALxVjIw1oM/oAqDT6OX19PkEyCR/aDDWbU5v9hheVgN86v37vZhcuNdF8WbDmVvuhdj/MIe9Q/dPO9NIXniF2QbWEgByXHIaMHyL6tvjiDunOd8BTWQXmWTcpSlvt+IHpk3rHKLAv+dnJTQ43vM1HfDjW2lvn+oPPdgGQKjm9Qp1QlK/zwGbtiKFwiS+pdtQuJvxV+RX6nLSQazGtKdZKbUo5wZELAhd+XdjIRbzUrdwG1+geFSqTUcNi15pQ7MMVVi4nwHXqErwtYvZecESLBiQhrEk9VElho6+wuVIOauNb1dIlx/CCYKorrdIAZ5Xs+WiEDr/48tzZTIFveRkspG+tVzDPqd8kTtq4Yf8jsOYCdadtXsoYeiwg/fxnbpVe5A8c4ZADGJmJQRP6LvEguTC9NTq0Iw4JkVcDgspl8wzVzgSjG9WLpCOxoHmhprUeyuqS8Wdw1d7im5YvPATbLsSJcS7QEpz+dki2ndo+9jvsTazxvLAXRXuRMqomMwruCATcEpSzGT5mwuZZH/H57PY2+VVqiZP7695IJMOxJ2ZKf7MPRppmsLf3Ll3ZJ5C/1KB39qZVBt1ffQpHAlrak2HvzfijNxRa+awOIYZlKcqBNR/wb0VyagJ01dED2Ks9Uyhg3DmrMsf8uo= 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 Tue Dec 27 12:11:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 13082277 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 60D78C10F1B for ; Tue, 27 Dec 2022 12:36:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BBFC48E0003; Tue, 27 Dec 2022 07:36:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B23F18E0009; Tue, 27 Dec 2022 07:36:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8135A8E0003; Tue, 27 Dec 2022 07:36:40 -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 550CD8E0008 for ; Tue, 27 Dec 2022 07:36:40 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2F49BAB096 for ; Tue, 27 Dec 2022 12:36:40 +0000 (UTC) X-FDA: 80288034960.16.D7BF76A 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 8FD151C0014 for ; Tue, 27 Dec 2022 12:36:38 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=g0IhgUzJ; 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=1672144598; a=rsa-sha256; cv=none; b=rVmyysxutEgzOgQla0TJREOGtERmgVYDuqP0GGYui1KUEoDg06DlNimtK/GeO+D+NGE9DW sy0M3ntKOkyE30/nwWFAfCeu9uBsYO7OrmQycV1Vb4Guwr8FE2xIXlkASqwwIPY6P0k39Q jDp7eHG27kbtVCKqOi30FbEwqQ1mJjM= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=g0IhgUzJ; 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=1672144598; 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=SOpyQwoCtFinH0fm5Xs947e8NK9akFHNxtsVnF6/lgM=; b=FoAJIvdanPSybHexFZ/4dwQHUw2qvKVlo/t8wu72H+v1F8jx8yA3HWZdxCop5S1OW26PEj jtbZypPjEnWe3wN4XqKKcwiB8858dcKPz4K0j03O/WwbfLrJAsMNaqFl7KYPonTYYcRQUt U8qLakltiFurCybJqQOle2rQxIMlwJA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672144597; 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=SOpyQwoCtFinH0fm5Xs947e8NK9akFHNxtsVnF6/lgM=; b=g0IhgUzJ6tL92O5spA68UrHVXFzvmcNHXnaPNPP4t4Mkclh9j9nmsykrr5o43/X4xU1Y1Z zMpyr36dLstp4qWfe6FxUllSzpVtxTN1mo9+jJSFKSnuwBb2HmT8NQaofB77uEg/FtFzD4 GUieY2MrAzallu/Pcod7PF+18ue7iXE= 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-63-qavEe3xWMlKRx3s8ruTvkA-1; Tue, 27 Dec 2022 07:36:34 -0500 X-MC-Unique: qavEe3xWMlKRx3s8ruTvkA-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 F10542803D60; Tue, 27 Dec 2022 12:36:33 +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 9D01A111F3BB; Tue, 27 Dec 2022 12:36:33 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id BE00340C519A6; Tue, 27 Dec 2022 09:14:52 -0300 (-03) Message-ID: <20221227121342.020984662@redhat.com> User-Agent: quilt/0.66 Date: Tue, 27 Dec 2022 09:11:40 -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 v12 3/6] mm/vmstat: manage per-CPU stats from CPU context when NOHZ full References: <20221227121137.102497574@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 8FD151C0014 X-Stat-Signature: dsyp4bh4ajqqpiesy9mt8zz1584jzrum X-HE-Tag: 1672144598-337994 X-HE-Meta: U2FsdGVkX19rXAj4Pb63KOYZhx/1wI9mjCHUuVYpj/L5UVkR5LnJFEQAV1f8y+3qaZXiZjrIIke8OEM8JBfYPKpkcMzXU2XFrHOT34V+hOoF/Ea3F0QtPVqeMcZEAd1iE1tzI4JevpnR5q/ZKfOB8p5608XuBUIc5GqzdenVV7Vl4tFOkU39HavlTGDALoTXTQQX8DQobOF4ulOoBZfSDA9maPLOmbQpIxDR3iSKTZ0pX8mHe9lm73aoNy2oSmB2AMN0P4ZgO0oxbJQVU0eX/mFpxRNATKiEmEE1/ieSXj6pIqJcZcZp0T7KUlfmFLY6PCXCKA5k/GEQZ6lbcoc28CHzRIoGEk/1UrvYxJ09h/6GuicevLMr/W166wGsNZ/LPzFX1Flv9mVEi1LMTzsBbuX1zsPcSH4iBdtvKJM4YuM2NujCQyES26YbkfD0Kkpw/rmmCOXKTsZMe+0frnLqgbDctvo1VtEPWU6rioCKrNHrr/CJMv3BE+z2XI6eewLXbKjrZOYGXKFmhTF9/zfIvJTzwA4H10e8TjHY2CIy6LFwFmD5I13Vuq1uOkcCdTYRuVJrbPrVL9aXK7Cw16dZqfeZGd+mDcmkVaDR8JYKCdnuupyMJrPbx5IHLOv9AP7Fs8+fkIEMyzSWqe9Tz/j2cuJSBp2+hCWbQtKHY3icPYeu9Qz9mQRC7POUv028QkrkKFxyo67fVQaKp8/4aMW3LnL+yMIrY/m4NM3MzjwNKei9BzHlxiQxX1Vqk3mTsA8Dm21AyMYdGPHqvHA3hRwUUwZ4rwZUSZlHsD2gFYmKsiLr6EozzcsGdWeYCKJxUXKkVTqA6phM/5/LG0MMlfQO5ntJ6Ofjuec5POYjQp3c2CEovwgQrjdLjyCQGI//dP4a 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) @@ -1886,9 +1923,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 +2007,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 +2015,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)); } /* @@ -2008,8 +2048,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(); @@ -2042,8 +2088,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)) { @@ -2053,8 +2127,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 Tue Dec 27 12:11: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: 13082278 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 98A1AC4332F for ; Tue, 27 Dec 2022 12:36:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E56138E0008; Tue, 27 Dec 2022 07:36:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D9ACA8E000A; Tue, 27 Dec 2022 07:36:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 924688E0008; Tue, 27 Dec 2022 07:36:40 -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 69AFA8E0009 for ; Tue, 27 Dec 2022 07:36:40 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 37AFB140BDF for ; Tue, 27 Dec 2022 12:36:40 +0000 (UTC) X-FDA: 80288034960.13.8472F2B 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 97B21C000E for ; Tue, 27 Dec 2022 12:36:38 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=JPZcxvGR; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf22.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=1672144598; 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=jdqQiMw52UWAoILafyRDwBCkqK7ENId/lt6wlUiiARR887OJoFMrV/UX/RFkB+xl87K80P IPPaWJF2ZNuODgZeXtMehmD49uUvmijKwbmqP/YgYI+F81WWKEPR3rmMO+rcnkjkzvDOSh E7SaIReyfkrDqr7ptVExi0fpsSL6C7Y= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=JPZcxvGR; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf22.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=1672144598; a=rsa-sha256; cv=none; b=8LkEIO34FNXueAfsvd2OeBDR3SEcwiWv4LrcrV6Mh5Geu7zKUxKGmmNBq3FcCNNt6D38lV CbXEXGEROwd9dHzNPMyXLq4afPwCtvt9yD5aYBnHmpLtrX4vVCRB3oCdkYmYYr9Zo5CHHv NDLYd2yeplIKr3AT7jDpNm2soHT6tn8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672144598; 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=JPZcxvGRC8M/ZE4cXjg0pSyA8doqgvwOHgpgfRMoXxjSqX3VvebWcUA+OyeMgw5jYPPu9S JRceFEEXRZu7SZCaUYt5wjCLzuWFlP/Y6qJcij4CD3OCLIpf3ns3JRfVJVMiFZjKux8XuY wews7D8+ST7autpnKJJgtxqkccifrV0= 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-625-tDoyWPJcMYanbhPprYBZTQ-1; Tue, 27 Dec 2022 07:36:32 -0500 X-MC-Unique: tDoyWPJcMYanbhPprYBZTQ-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 EC3CD2803D60; Tue, 27 Dec 2022 12:36:31 +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 6D15C492B00; Tue, 27 Dec 2022 12:36:31 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id C28B14257A81F; Tue, 27 Dec 2022 09:14:52 -0300 (-03) Message-ID: <20221227121342.060153015@redhat.com> User-Agent: quilt/0.66 Date: Tue, 27 Dec 2022 09:11:41 -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 v12 4/6] tick/nohz_full: Ensure quiet_vmstat() is called on exit to user-mode when the idle tick is stopped References: <20221227121137.102497574@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 97B21C000E X-Stat-Signature: uw4wgfi95banckifcisjpbpjb9xbi6iz X-HE-Tag: 1672144598-708415 X-HE-Meta: U2FsdGVkX1+vSoSRJNAL4gkvlmtQcin+FR6MW2NYxSSDwFO4Qub4U6Z2m/5jyVTUgOOq0WjFSYOR0EwCB95as4mCiCezDEZOmsdQ3V6y6iDG5RqIPIYAaMeiBqdj8V0PtQ+85KsmpWxxAHPLGJ201/UDRRZ596/3nmenlDUXeZcI6Fli6rO6cqTb9juL5unVOT9C3aqIx6nmKS4+BuEO78ebq5a+u6k/ZoFZhbPguf1LYRR1W346D/JxQYQOiomqJ3Nvt3/Yn0JgDLVScjLe+SIttjG4Jvg/hx1jwT2TnSxWDsHP103NI/Bn8BFwuAXWXiceZNibRFdQWdMYfi4kE/Nqx5az4PRScVJFwWVKH7uHjjWfGNCIxPA6VW7M1KphK5Vwf1KPC3hJ9rq0GGuCaVdV8s1UBjllWkwyb5+1OiIvhXvBoeezGZhHfXJUgXo41YgKCDKrql90ba3BYx4ULZncNWknLHvM0oHC+FUrnwjbCVmdFLVoO3lsxNTfuUwMud6lOSGc8Tax8Gip/m57hBiu9bAqyb4Iesw5x2L8wND6ZKNHtKrcXuwk7uVotR5vIID7PU2I6ILEYO8BKK0wpmX2dLOVb7RAwoXJiPNuhlNe2yDbBu+5yxaKRnm0GALkg7qoYp+UfeVVwl+dZ5f+97aArnVOrzZwpGyUSPP006Q6jBw/uwvSzo1guvr18LHTLLdOWBrlAfXPWv+S2ag7+zxbw0SxIGujpqsSFrbWqprMsfYR37F2wH4E5NF1J9M6V95n9ISctC9D/2TAjECLzPhbDR3jP08dfYKW+QsvqdHq42+WegwIZDImEBoHZ+oGHOyarB3Ir39WIO8NIoPG8DylZbHaqnffbdlE8cQjk8IHuX8cCHUD9nDn9BpKkYj7W1dw2YMu0+GdBcAzrzoV8Leaq1PuDruwxks0rSsP6Nc= 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 Tue Dec 27 12:11: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: 13082272 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 141ABC4332F for ; Tue, 27 Dec 2022 12:36:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 702CF8E0002; Tue, 27 Dec 2022 07:36:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 68A108E0001; Tue, 27 Dec 2022 07:36:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 503BD8E0002; Tue, 27 Dec 2022 07:36:37 -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 3BC648E0001 for ; Tue, 27 Dec 2022 07:36:37 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 09347A0B5C for ; Tue, 27 Dec 2022 12:36:37 +0000 (UTC) X-FDA: 80288034834.17.FA55CBA 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 D17E420008 for ; Tue, 27 Dec 2022 12:36:34 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Nc4Mjf7K; 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=1672144595; 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=4jdyMiXPOXqUhKQi/xhIBKGXouyLhI2CTwVQHQBfE2jIns6vWHmX0eeZ4owJ0xWJnGD9WN jChQIGi1clfS5moOh0T+DKMabxZ6OKrZxjrc6uevm6NjxVUeS3SWjntrh2IUDtxtD1r3qt PVGapyrC70Nci8FWSgZ6QxyqQ7PCgqc= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Nc4Mjf7K; 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=1672144595; a=rsa-sha256; cv=none; b=5sP6S1W/kaWx0KeDKEz50tp70vhlRhURrZ0/QQAFcM1FtBCQb6rz9AHHYefIR5DR8Y5pHZ /TzvjD361s7+nEzl4JgRPa1P+6D7Yt+dsE7v0sUqbdh3J2JWe4hi6jtzDQwUFiC0xWHCWU LHUrLQDcZnNiLvwP5aqUhPjl8uNSz6A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672144594; 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=Nc4Mjf7KSt4nYHo96oEJXqeTPpBOozrZ4F/hqUpcjZYZxF0ohgl3s91ufA+pGOhXKCtJlV l+EU7StYEUM48uPLrGLXX8hU5TZ/nG2emv3u85BmxXEkxIbhrD8A5KU5ir+0zgo6Rk6dcE xfwL9n4gu+OmgUHEX6LE6ERK8C9droo= 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-490-SeTNXHpqNVmGJHzsBLa9yw-1; Tue, 27 Dec 2022 07:36:32 -0500 X-MC-Unique: SeTNXHpqNVmGJHzsBLa9yw-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 2A3E83C02183; Tue, 27 Dec 2022 12:36:32 +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 7D6FC40C945A; Tue, 27 Dec 2022 12:36:31 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id C611E4257A821; Tue, 27 Dec 2022 09:14:52 -0300 (-03) Message-ID: <20221227121342.100243810@redhat.com> User-Agent: quilt/0.66 Date: Tue, 27 Dec 2022 09:11:42 -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 v12 5/6] tick/sched: Ensure quiet_vmstat() is called when the idle tick was stopped too References: <20221227121137.102497574@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: D17E420008 X-Stat-Signature: 1xpqfh8it84eidyi77pcpeef3xt8exmk X-Rspam-User: X-HE-Tag: 1672144594-458217 X-HE-Meta: U2FsdGVkX1/zSehxByefJok1W1nP0VZvH5rc+8huq9IRGBpxalEMq7Rxq3erv+0tnkRGgUD6z843XXwwH0oJBkVfBSYEpfWWxc6T+JG4h9itZovdt/De91PkIewvRF03I/n7lGWo/g04LSSWOgZTBCLd6yxh2B1BnzjiASTqIPfKWwGXqGpkbGnJhT+9NuIwskfQMb21e51AE9sZbdNxC6jHDo4lR31BzldwsfRuW/C4IoKT00j5UK/6aUAdCS69ky2p4y2ljNcG0/XkNoYXfmTtG02eaj0NdGyPB+nZV+7p7NUOrcZ29jSWJYm4B7W909b4Mgh5ktebMLzNV5dSSZrFSalGE5DTXc4wjB9Uz/zOhqOGgHSaIjOSJRJrcwSvM5TejdNYCkDNKUSbqNNNuZ5Y/UORb1l2w7T6imeaO38SGLIZlWPXMTkOa1Xt4wvUMAe53G37T0t8b7k2rXjAjOGp+V/qeR3l4seR9vucxXzhQpwouunLcZW5t5hs2BQE1mwxsPES3zNf6FA206Bbvhsnb2a7gIunfW/dvil7hzSmz9GZnkKGc7888HbQb59hrErcJpPfGhFNogHiPmdFtrUBCc1VRcayWoUGBVsl3T0dBIhwwDYSisJzhSeMu+QBh48Nt4hmDebWNVnc4bXMHNWsyLSlWYzZu/F5rB4BRta3HBRF1LrfucfFeWvhrf+HCuBcqqzlyyz+3BcW/fPWkdQoH755CzwoKJ+6cR8poJknbuXh26mqBEVvAfm0eJFC0P+q3SUuzwTXQdw+zUZZ18sY5cjZVLcE/pwes8RAewVwCo0MvQi4c0dU1iTg4CmcngGa1cKGbJSjkfXSRGRE+Cuj4cpaMH4pmaEGOJfL6W7yd6B201OURMsMek5jzY60GmvGt8m4UNYfsc+fnMvmgylKrvgkpbyCA5wPoNBZif4= 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 Tue Dec 27 12:11: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: 13082273 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 EF935C10F1B for ; Tue, 27 Dec 2022 12:36:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2956C8E0005; Tue, 27 Dec 2022 07:36:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 21D788E0003; Tue, 27 Dec 2022 07:36:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 096D48E0005; Tue, 27 Dec 2022 07:36:37 -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 E970A8E0001 for ; Tue, 27 Dec 2022 07:36:37 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B7FCDAB0AA for ; Tue, 27 Dec 2022 12:36:37 +0000 (UTC) X-FDA: 80288034834.10.84B6E8B 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 30AA4140008 for ; Tue, 27 Dec 2022 12:36:36 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FbqDj8yh; spf=pass (imf23.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=1672144596; 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=CqggzM+YtlNY8+VheXxfHye6fQtUBH0h9jBpeWHBdBQ=; b=sWrjEfA4wY0fkTL9w7PG2AJtGb8zlzo+zECI8oWMUCLJtTRkyFQ9NgkllWeX0jCNIq0q+L MVkAW+bNB2zva74Uj5qO6tjFWe1hrOTjFxsAG0/Fg3wPVYJTBcdOr+2uJ+HFKyOG8iZv++ nuSwKOMZJrPxA/Dhg9B8p0QPAnokwks= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FbqDj8yh; spf=pass (imf23.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=1672144596; a=rsa-sha256; cv=none; b=2coWOiNtHHas1IdGR1Q6vCzmsOK7Bbmjl3sh9jCEEhjTD1YC7YptSCAdmxDODJyHXXO9M4 b/jIAlCIKwfYvgTR4A3SAy+QW34udSsmHx5V9lcCBxNPwJiy/AgwSR8icKFj30yrqZ2evZ iBcT+bV+hnViy1dXgJJ5zpZ50RucHOo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672144595; 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=CqggzM+YtlNY8+VheXxfHye6fQtUBH0h9jBpeWHBdBQ=; b=FbqDj8yh6sclRTk6faNzUlOC2GfIzRXgZqjXGufj+Whx3oPHBOJ69VkyaCDRx+S6/1Jlxi rP0iTcYd8AqunJoZbaVWHy7evYDu0VwW1Ox1ses+Oa++5GGpHGUryYzHAgENvC3rNQolsR TWta0zEt8sPkWhNXuGh5O1Nto3dLjc4= 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-434-bzfDgGI4OBe12qE5XtAC4g-1; Tue, 27 Dec 2022 07:36:32 -0500 X-MC-Unique: bzfDgGI4OBe12qE5XtAC4g-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 ECCA9183B3C0; Tue, 27 Dec 2022 12:36:31 +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 7D40579A1; Tue, 27 Dec 2022 12:36:31 +0000 (UTC) Received: by tpad.localdomain (Postfix, from userid 1000) id C96BF4257A831; Tue, 27 Dec 2022 09:14:52 -0300 (-03) Message-ID: <20221227121342.139017111@redhat.com> User-Agent: quilt/0.66 Date: Tue, 27 Dec 2022 09:11:43 -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 v12 6/6] mm/vmstat: avoid queueing work item if cpu stats are clean References: <20221227121137.102497574@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspamd-Queue-Id: 30AA4140008 X-Stat-Signature: p74z39dt7519pykuucs3uz1996cqdpsu X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1672144596-926598 X-HE-Meta: U2FsdGVkX182YLa3Op+m8m+4hUmLTJrVzn/GdGn/go5CU08/ZM8I4eCZ5pNXsTDZ3IufHjedP4uo/wSe5IcZ3zTUqSLnWMUGoyQmSsp5nJIugseQLf2Ub+RFCoG3D3mAWyZ8om+VsBym0j6sOhjQdGu8diCWprYoT5QTAMEmdfivj5HdRdhTvIYjKyQdT8Ppy8b8L6qRc2I665fPmIZDFUgMd0T8GFr4Vai5uNU1UcdMhTbF/hKVb4mYc7E39wyYMg1CO5TJOpm+tpGtmKq97vySIgRoTtE4apoRV/WNdzLeRI1F70oHBIm23gy1gaWuOn+VVxIwuEyFzQVKOM5RqqTmgiZg2j8lRh6yc868m4POKXA5Qu9E8xpZZQt7c0TMw2ASBYkR17mBmoq/8UNgInqwFZBb078e14lrunLCynNrNrHXuk1Bcx40AqoZ/XeFrszjCAy3z9KOpWDbcpBRHOXfRiJA99oQY9XDTab0ig+3kBht2ebM/JWc3asNlfTA/CBXa9GEXK3P0iEo5UIn6RFN4/RMMFWWqwdKDOpSBf3ylanPnIY18zJzGeAGOGh2mUlJwTBaQfHvP8ejKtHocEuK9GI1JwwZjAycr2lo+tLxNLsqrj1H1mN/wiOZ75pehsddObADfSnMXzUGK6VyTsIv3UhP8kkDSIJh2qhaCmscPD34VoJK28ki40slkcTyNaGKRjJT/BxlawqusAOfLUiwy6pwbBV/CWzqyj4AAohcVzzlvfh1y7My1+N5CMc/tz+bvK3iPt2IvBNd6LzZeQshyq/JTGh1zPtxqx9aIoz9MBCbVbbOfDF2ODVCotXZ5XZmX9vXQI0zx8j4QWrfKRO519S1/eMKPz2q9iZEL50Y8uYmgXeddD9EElp9ZT+H 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 @@ -1924,6 +1924,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); @@ -1933,8 +1958,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 @@ -1948,9 +1977,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.