From mboxrd@z Thu Jan 1 00:00:00 1970 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 EBA72C3DA60 for ; Thu, 18 Jul 2024 08:12:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 634696B0089; Thu, 18 Jul 2024 04:12:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5BEE06B008C; Thu, 18 Jul 2024 04:12:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 45DD86B0092; Thu, 18 Jul 2024 04:12:47 -0400 (EDT) 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 2434D6B0089 for ; Thu, 18 Jul 2024 04:12:47 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DEE5A1A0E74 for ; Thu, 18 Jul 2024 08:12:46 +0000 (UTC) X-FDA: 82352157132.09.CF29D43 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf01.hostedemail.com (Postfix) with ESMTP id 1630A40023 for ; Thu, 18 Jul 2024 08:12:43 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=topNe8KL; spf=pass (imf01.hostedemail.com: domain of hawk@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=hawk@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721290344; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/SGW+IgOM0Upvw8FijS4jDVY3bNU42tfSJg7/9zPBuM=; b=zBx1yBPzNX4SisSvEXVKJE0pVJs1srLH2cFAHNK+KPCqBALLIGkhmgGCUV+QkERewFLaSo b3ZMVeg1d4hj2ofNNJmbID9m05EhgCTW0h1YhIq8zguldOeA/DjkWPTUyhFqGah4ofC1ii czZE3HSaIVg+P5CKvNhUY9IlCDhUgHU= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=topNe8KL; spf=pass (imf01.hostedemail.com: domain of hawk@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=hawk@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721290344; a=rsa-sha256; cv=none; b=ZhT1TSULqkZPApIfIC5QQ89wNH6xixiCMPxSnVtUucP2En739ILXLeY5zws3hUu0XSySyF yInAq2fobcdr9Ggs+DPE5W9Ac9Pmip9gjXoiquA6dFNbtw1zQ0a32YwoOvKntao6vi4jZQ 3oiguu+ZVHykEo59P/YgApuv0tv4yxA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 118C561A09; Thu, 18 Jul 2024 08:12:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2683C4AF0C; Thu, 18 Jul 2024 08:12:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721290362; bh=dk+iL5Q7Ym85T0WoUq3bMcJRKL94pL7/kW/Z6U4W7UU=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=topNe8KLbnhdMnyS1Zsd+GsWr55kpB3t63FkZY11mfQsZiJQpc4/h1w71u2ghO3mV n4GwGgaSKEEl39J1YDrWtM+MLFtwbKhC2PowjaGNhlHJ1ZW2lOT7i6sJbJkpTpUhyg mZ7yefndZj/FB3krYrd2kgzAThHd4Je1mlz0hkXT9nShdEgxnQmD41ZCRjH4+AHJ+8 eef4wEoARq2oWlJyg+Bdz9j01KEti83fmYayYEeUIKEOWT2hMRqQ4PRBZOPy3F0hqH cZsQicVIBmCWaEpwvJi+OEIpJa9ZNfDiLZ+tvMWEBQZruivHsYpBuvsPZzn3PLkzG8 NFQl7EU3cqZYw== Message-ID: Date: Thu, 18 Jul 2024 10:12:39 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH V7 1/2] cgroup/rstat: Avoid thundering herd problem by kswapd across NUMA nodes To: Yosry Ahmed Cc: tj@kernel.org, cgroups@vger.kernel.org, shakeel.butt@linux.dev, hannes@cmpxchg.org, lizefan.x@bytedance.com, longman@redhat.com, kernel-team@cloudflare.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <172070450139.2992819.13210624094367257881.stgit@firesoul> <100caebf-c11c-45c9-b864-d8562e2a5ac5@kernel.org> Content-Language: en-US From: Jesper Dangaard Brouer In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 1630A40023 X-Stat-Signature: q1qhkqb3wecms8tnzsg4jad1a71p3af8 X-HE-Tag: 1721290363-902885 X-HE-Meta: U2FsdGVkX19lpYm+8GIQ3XS5s5t1hnsW7TZV/GhsNH/ODRapbeNMnf9IG5wq3lFo8NcE1uEYtyJanuFQBRdNjV3FrHQchOFQvThDyLt0j79LyNYpgTurzrpFrtDQ96B1xd/JrLsLwHyDQSnqbenW8rCtNWEiWbfcDGvNyclFo7N095I1lEDAr8XuMHDprVFAoFz36/ClYellLjEnu++HDrMhG4WPWn9hEg3fgSVegWr1l62d0zGq+1LWfm/GsTEy2YIohPpdRYr1pA9iKzSXFkKu3E329nz3j08Q0maxhlnbJ6H+ktNLpeaEswFmA5XhbOvMhthqJGfA6HBqd77C4lkZ9jS5AxbfNUHW+/PnBbhQQny6T0wwURgv38ucX3qo9m7x8yDblWk/CZz+S7hNRcyfgAtTEgW7gcReoE+pQm/raIOLxrnRmDpZRuEt4SfnO5+7hgnj3BHG2+M2Lsvnmwf7gL9wLm8UeWvpY/UeEO07xLu7UpopmqH5TdxRyMMj9UDkPA4HiFbY8lZ227v5XqfmmC/V9Fw52iKVx9DmeX0gwmdW6QS5y5Sc2cXTJWTs7n0SvU9RzzUy1ekHDsHye/MO0N5Pdj/S3TDFuge6pVhVknH95qXLpADmTsgJ0qN7cFbKU/+57eRYFhdGIeEyBUzXuPR3irCIUYjHrSctmoqj1QRr3I0r+O2lFjyXznNKgw6oKA6E5GrYYD3IhWWcq8NIWn0Yhy23RgJlwlb3kZ3MjI5F8ccsjS3eA8/LWsrpd7w2xqUHoA/XTm1gEeeciOffxzT2RhaC3oxk+T929oK5/Z3AQONKxCVVuIv6wWp14xUuf8kcMNHbCmBlGSQpzxo805NQDZ+JCZjOe2N0YWOBNqjdh6ktM22sMOR69iMwrYrF6RFlF2c+176JECzHNvu9moYTe7aGziDx1Y0isww8qw3vWel7XzsSvnC9G4aVXR0xPNCc1WyixqJK+j2 7oMAYalk EgjwCXfhpY+4afjouW43Tp9Gaz5BQiKmJCNvqzOO/w5M2EdwacDZeYVGelnK62YNxSk8uTnAXLGJDLX+Ku505Gtl9lpSLTI8nl3AHrp35aWRYW4GxAz+JKOkpfBxmm8qxXMKUWoFw5aadrP9uFCv5adDdMR4jtEviIszpqkW8vbe/www5t1sZWqCpYY1mdyeSafacaJcnpOr8JjmK1jhLlfCOy1lsnEnJJ9HpRl9j22zGI2gsfgsZllvwpkpVqy5kHcgtu9suDb/SUUCmrO4lm4NICygBCCXJRt+I9YqRksMJu23eSQaelv7Ulw== 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: List-Subscribe: List-Unsubscribe: On 17/07/2024 18.49, Yosry Ahmed wrote: > On Wed, Jul 17, 2024 at 9:36 AM Jesper Dangaard Brouer wrote: >> >> >> On 17/07/2024 02.35, Yosry Ahmed wrote: >>> [..] >>>> >>>> >>>> This is a clean (meaning no cadvisor interference) example of kswapd >>>> starting simultaniously on many NUMA nodes, that in 27 out of 98 cases >>>> hit the race (which is handled in V6 and V7). >>>> >>>> The BPF "cnt" maps are getting cleared every second, so this >>>> approximates per sec numbers. This patch reduce pressure on the lock, >>>> but we are still seeing (kfunc:vmlinux:cgroup_rstat_flush_locked) full >>>> flushes approx 37 per sec (every 27 ms). On the positive side >>>> ongoing_flusher mitigation stopped 98 per sec of these. >>>> >>>> In this clean kswapd case the patch removes the lock contention issue >>>> for kswapd. The lock_contended cases 27 seems to be all related to >>>> handled_race cases 27. >>>> >>>> The remaning high flush rate should also be addressed, and we should >>>> also work on aproaches to limit this like my ealier proposal[1]. >>> >>> I honestly don't think a high number of flushes is a problem on its >>> own as long as we are not spending too much time flushing, especially >>> when we have magnitude-based thresholding so we know there is >>> something to flush (although it may not be relevant to what we are >>> doing). >>> >> >> We are "spending too much time flushing" see below. >> >>> If we keep observing a lot of lock contention, one thing that I >>> thought about is to have a variant of spin_lock with a timeout. This >>> limits the flushing latency, instead of limiting the number of flushes >>> (which I believe is the wrong metric to optimize). >>> >>> It also seems to me that we are doing a flush each 27ms, and your >>> proposed threshold was once per 50ms. It doesn't seem like a >>> fundamental difference. >>> >> >> >> Looking at the production numbers for the time the lock is held for level 0: >> >> @locked_time_level[0]: >> [4M, 8M) 623 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | >> [8M, 16M) 860 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| >> [16M, 32M) 295 |@@@@@@@@@@@@@@@@@ | >> [32M, 64M) 275 |@@@@@@@@@@@@@@@@ | >> >> The time is in nanosec, so M corresponds to ms (milliseconds). >> >> With 36 flushes per second (as shown earlier) this is a flush every >> 27.7ms. It is not unreasonable (from above data) that the flush time >> also spend 27ms, which means that we spend a full CPU second flushing. >> That is spending too much time flushing. >> >> This around 1 sec CPU usage for kswapd is also quite clear in the >> attached grafana graph for when server was rebooted into this V7 kernel. >> >> I choose 50ms because at the time I saw flush taking around 30ms, and I >> view the flush time as queue service-time. When arrival-rate is faster >> than service-time, then a queue will form. So, choosing 50ms as >> arrival-rate gave me some headroom. As I mentioned earlier, optimally >> this threshold should be dynamically measured. > > Thanks for the data. Yeah this doesn't look good. > > Does it make sense to just throttle flushers at some point to increase > the chances of coalescing multiple flushers? > > Otherwise I think it makes sense in this case to ratelimit flushing in > general. Although instead of just checking how much time elapsed since > the last flush, can we use something like __ratelimit()? > > This will make sure that we skip flushes when we actually have a high > rate of flushing over a period of time, not because two flushes > happened to be requested in close succession and the flushing rate is > generally low. > I really think "time elapsed since the last flush" is the right solution here. As, we *do* want to catch the case you describe "two flushes happened to be requested in close succession and the flushing rate is generally low." (After this patch fixing the lock contention triggered by kswapd). The remaining problem with kswapd is that those flushes that doesn't "collide" on the lock, will be flushing in close succession. And we likely have a generally low flushing rate, until kswapd starts up. Some production data from a "slow" period where only kswapd is active: 05:59:32 @ongoing_flusher_cnt[kswapd11]: 1 @ongoing_flusher_cnt[kswapd7]: 1 @ongoing_flusher_cnt[kswapd3]: 1 @ongoing_flusher_cnt[kswapd5]: 1 @ongoing_flusher_cnt[kswapd10]: 1 @ongoing_flusher_cnt[kswapd6]: 2 @ongoing_flusher_cnt[kswapd8]: 2 @ongoing_flusher_cnt[kswapd1]: 2 @ongoing_flusher_cnt[kswapd9]: 2 @ongoing_flusher_cnt[kswapd0]: 2 @ongoing_flusher_cnt[kswapd2]: 2 @ongoing_flusher_cnt[kswapd4]: 2 @ongoing_flusher_cnt[handled_race]: 2 @ongoing_flusher_cnt[all]: 14 @cnt[tracepoint:cgroup:cgroup_rstat_lock_contended]: 2 @cnt[tracepoint:cgroup:cgroup_ongoing_flusher_wait]: 10 @cnt[kfunc:vmlinux:cgroup_rstat_flush_locked]: 43 @cnt[tracepoint:cgroup:cgroup_rstat_locked]: 51 We observe that ongoing_flusher scheme saves/avoids 14 of the flushes great, but we still have 43 flushes in this period. I think only kswapd is doing the flushing here, so I claim 41 flushes are likely unnecessary (as there are indication of 2 kswapd startups in the period). Also observe that some of the kswapdNN processes only have ongoing_flusher_cnt 1, which indicate they didn't fully "collide" with an ongoing flusher, while others have 2. --Jesper