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 F400FC04FFE for ; Thu, 18 Apr 2024 15:49:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3FBDB6B0082; Thu, 18 Apr 2024 11:49:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A97B6B0088; Thu, 18 Apr 2024 11:49:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BFA26B0089; Thu, 18 Apr 2024 11:49:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 106476B0082 for ; Thu, 18 Apr 2024 11:49:54 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9AC4D1204F1 for ; Thu, 18 Apr 2024 15:49:53 +0000 (UTC) X-FDA: 82023088266.23.887663C Received: from out-170.mta0.migadu.com (out-170.mta0.migadu.com [91.218.175.170]) by imf12.hostedemail.com (Postfix) with ESMTP id 0BA4240014 for ; Thu, 18 Apr 2024 15:49:49 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=IsVD6mVu; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.170 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713455390; 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=ziaS2a2JLbKpn9yWeB1wELMDTiamfefOEg/5bnjE4gE=; b=zxjUUgQUPuEx9dJ5yZAxTURdBNuQMSHRADLZE9xivl+joYEZgC6X7H/Vqi41OxpovsRVSw 07jxW4Utxo5ges4tRPTKS/UIR3WezHSgRJ+zAJEH9OkAyHZsWrXobGKDCLpXf+UTIqySSO M0pHSDRmHphyzTW2h+f6QYsimo3jDcM= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=IsVD6mVu; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.170 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713455390; a=rsa-sha256; cv=none; b=qPug145pxsk3LTmbDDOwx+jLXg97RrZzgu3o/L2vzBqJCiKiq7QgqXPkditLURtOHpp2+P W4TFuFLdVHg0Z2wvcLR1GlEq3pZvfrCmJ7SKKRhrbuIs2VeQBn5W8CdQn57K6azAWZ6B0D aBGd26t8+glCJteeQ053ORMWydMHzbM= Date: Thu, 18 Apr 2024 08:49:42 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1713455387; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ziaS2a2JLbKpn9yWeB1wELMDTiamfefOEg/5bnjE4gE=; b=IsVD6mVumHoODJn7B/wl4ZNhpjw8ihVo+pRJpCI3y59wPDzivclT2aF5c/mQG55Mp+pFY+ hEtMsbiS1SKwooNaoh23/Hf/B/G5u+7a4sg+2RchIP7qIEsZEfKw3QL2xjHtkUE6VINkrO tqr5ma/KinE2a4BcflYzP6lbU5+7ekY= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Shakeel Butt To: Jesper Dangaard Brouer Cc: Yosry Ahmed , tj@kernel.org, hannes@cmpxchg.org, lizefan.x@bytedance.com, cgroups@vger.kernel.org, longman@redhat.com, netdev@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@cloudflare.com, Arnaldo Carvalho de Melo , Sebastian Andrzej Siewior , mhocko@kernel.org Subject: Re: [PATCH v1 3/3] cgroup/rstat: introduce ratelimited rstat flushing Message-ID: <4o4qxf3tcos5rl7h2noldeg3knqkgc2ph36tv2cceourbsxgas@xicxkcacme7v> References: <171328983017.3930751.9484082608778623495.stgit@firesoul> <171328990014.3930751.10674097155895405137.stgit@firesoul> <72e4a55e-a246-4e28-9d2e-d4f1ef5637c2@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <72e4a55e-a246-4e28-9d2e-d4f1ef5637c2@kernel.org> X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 0BA4240014 X-Rspam-User: X-Stat-Signature: kripszer9aw53n8aksn6qkkdbb513iew X-HE-Tag: 1713455389-107213 X-HE-Meta: U2FsdGVkX1/6SXS/s7JjpZyqJgTDCLcJb10eEkMPfza57f6Q+b0/4IyYkPZZsnuAhL07gpnjY3vkIyw5PwXYmgfg/t7+sFsQRHduCY5W+IG2ntzsMtkVvNuMFJ2qrv2g4RlNNSrqqD2BPK3TwuTahl3d698aYfT9Uu6k1z8DHwZmswBEyO/cbsWxt6GHVMnAu3hSuSNKZHpiJ9Oq+gCPA+AyLCyxUPp0I/eSvxbDz+x6cmBdJvp0UPUtggVZn/hgFZ+2Uz0+vkhXiJRx6I+rbSs8TqQRUu/eisUtXn7I451wsvxYV4/JnX76+pJiYKRZGjHznO5/WWi2UftW7cE7X3VjtvNSVHXR8a0GMgJEhOYsWWbBZhEccJtlVifHAhlkaJTKQMw+NAZlN25LmZqlDich4yoMP37xVdlJamF/bapopjDmZ6hVWTVoJAEgduGIQoChvj2pvan4IMNLnPOAfZpPfkPwWZAFK4M4geyu8FODA2vmQYR1rqr/34z0ZTA+y41C3J9CqM/yNkLkNhpeUy7sg11gwzCiCxrBQT32+uEpkCsJDIeKBbJ4BF38W1/8c7klaJUZPBr6aKh8TUjg/QDNy9HmdgnMYYnwu35OaNHKcfBz4z9i+oEbYKeQnSAOZEqXBvaUwia2KvVPusdA44JrTRtyoGtIkXx1K/GnQgPNBtC5CkgbsRsHMSqppm+grZ7dQplEpL2AP8MbveMpJlP65GVBsh2Ow3yy2Kms929EjZsWMhm3YP7Aiqjb8yUWezFEqbPHBYi/llHPQXtxndyRXnwHjzTrExchdUIyzrqU9tV+dJDxq5IgAH1MPgpsTuczHtzk5FveJwQCLcKHCO2Jadz8uTSaHqBPWv3KjcEPEoAaEajsehrm7hOeAbET6H5ILBgjJ4XLdivABI+QW3IRDgEUM2L70EG6gmIOgOEQ9mYzkYEre3+4IRrhBCJkvZjyX7+xDAi2OF5Ia1F Zg8UF8LJ 87eCwmot7x2ODq3WjRuDypykBAWeC39jXRAk33xP9q0ou7lG6hXvRpnrxOymWbgkO66uYRB+LD2MyT1PCld1aGKeIdqXQ2r4mRaUfouXjyu1o1TYrHd6r3iXFoip8apKKyUVX45ggsGKL2jU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000582, 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 Thu, Apr 18, 2024 at 01:00:30PM +0200, Jesper Dangaard Brouer wrote: > > > On 18/04/2024 04.21, Yosry Ahmed wrote: > > On Tue, Apr 16, 2024 at 10:51 AM Jesper Dangaard Brouer wrote: > > > > > > This patch aims to reduce userspace-triggered pressure on the global > > > cgroup_rstat_lock by introducing a mechanism to limit how often reading > > > stat files causes cgroup rstat flushing. > > > > > > In the memory cgroup subsystem, memcg_vmstats_needs_flush() combined with > > > mem_cgroup_flush_stats_ratelimited() already limits pressure on the > > > global lock (cgroup_rstat_lock). As a result, reading memory-related stat > > > files (such as memory.stat, memory.numa_stat, zswap.current) is already > > > a less userspace-triggerable issue. > > > > > > However, other userspace users of cgroup_rstat_flush(), such as when > > > reading io.stat (blk-cgroup.c) and cpu.stat, lack a similar system to > > > limit pressure on the global lock. Furthermore, userspace can easily > > > trigger this issue by reading those stat files. > > > > > > Typically, normal userspace stats tools (e.g., cadvisor, nomad, systemd) > > > spawn threads that read io.stat, cpu.stat, and memory.stat (even from the > > > same cgroup) without realizing that on the kernel side, they share the > > > same global lock. This limitation also helps prevent malicious userspace > > > applications from harming the kernel by reading these stat files in a > > > tight loop. > > > > > > To address this, the patch introduces cgroup_rstat_flush_ratelimited(), > > > similar to memcg's mem_cgroup_flush_stats_ratelimited(). > > > > > > Flushing occurs per cgroup (even though the lock remains global) a > > > variable named rstat_flush_last_time is introduced to track when a given > > > cgroup was last flushed. This variable, which contains the jiffies of the > > > flush, shares properties and a cache line with rstat_flush_next and is > > > updated simultaneously. > > > > > > For cpu.stat, we need to acquire the lock (via cgroup_rstat_flush_hold) > > > because other data is read under the lock, but we skip the expensive > > > flushing if it occurred recently. > > > > > > Regarding io.stat, there is an opportunity outside the lock to skip the > > > flush, but inside the lock, we must recheck to handle races. > > > > > > Signed-off-by: Jesper Dangaard Brouer > > > > As I mentioned in another thread, I really don't like time-based > > rate-limiting [1]. Would it be possible to generalize the > > magnitude-based rate-limiting instead? Have something like > > memcg_vmstats_needs_flush() in the core rstat code? > > > > I've taken a closer look at memcg_vmstats_needs_flush(). And I'm > concerned about overhead maintaining the stats (that is used as a filter). > > static bool memcg_vmstats_needs_flush(struct memcg_vmstats *vmstats) > { > return atomic64_read(&vmstats->stats_updates) > > MEMCG_CHARGE_BATCH * num_online_cpus(); > } > > I looked at `vmstats->stats_updates` to see how often this is getting > updated. It is updated in memcg_rstat_updated(), but it gets inlined into a > number function (__mod_memcg_state, __mod_memcg_lruvec_state, > __count_memcg_events), plus it calls cgroup_rstat_updated(). > Counting invocations per sec (via funccount): > > 10:28:09 > FUNC COUNT > __mod_memcg_state 377553 > __count_memcg_events 393078 > __mod_memcg_lruvec_state 1229673 > cgroup_rstat_updated 2632389 > Is it possible for you to also measure the frequency of the unique callstacks calling these functions? In addition the frequency of the each stat item update would be awesome. > > I'm surprised to see how many time per sec this is getting invoked. > Originating from memcg_rstat_updated() = 2,000,304 times per sec. > (On a 128 CPU core machine with 39% idle CPU-load.) > Maintaining these stats seems excessive... > > Then how often does the filter lower pressure on lock: > > MEMCG_CHARGE_BATCH(64) * 128 CPU = 8192 > 2000304/(64*128) = 244 time per sec (every ~4ms) > (assuming memcg_rstat_updated val=1) > It seems like we have opportunities to improve the stat update side and we definitely need to improve the stat flush side. One issue from the memcg side is that kernel has to do a lot of work, so we should be reducing that.