From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8251F39C014 for ; Mon, 23 Mar 2026 12:46:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774270010; cv=none; b=oZAwP1y1HDplC1U5CAqktqGR9t5cqw2dgGlkTYYQlUwwTo9rehpO6tbx1+iVGC4UdG/WTE/FmhX+c3q+lGzQGptm+yd85If9NaFG9HwU+NotikmN47kdhMva7R7yb6zHHZexR1DDxKjMS8032G2yY6dC9MtjXBLRaFC5U3gOBCs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774270010; c=relaxed/simple; bh=XnysLVfVXj++X3qYmTmEaUUk366ZaIjwcFzBQj0b448=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=sCzwxlk5p83KRd3RkyNpwlDODrj4kaFIcthegVFku8PCqOH9SfoxNQUMQs9cWsiyN3CixAeEC+3hCioEBgtEcPQhIhogu75XkbLbsFxUhfIy3FlmgK1G1v3m4i8CkFSV6P9RW/PsMm3Oa6v0DVBNcUAIq6cczoE2Gx5MMHzcX4o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=GpDFlzck; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=au8/bzNr; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="GpDFlzck"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="au8/bzNr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774270008; 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: in-reply-to:in-reply-to:references:references; bh=zj+HJ3NHVe31Ua9iuOX+I5shCryiTmPAEr1rzKm60IA=; b=GpDFlzckhDJYw0kBL7A5QlcyrB+YaOEYEswf4or1TWT+mxM8Web6z5Ujr0Qd92gyknZ9uy JalQM7zO4Atj76iqh6i63K8qTZAgBrbMYzjuNNbdV92RX6dHOYBGPkJRFKrm7Lf2Rd65jZ QHCUQEFJ8jZ6Q8iraZROCmTJLmqG6VM= Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-398-NrWL8T_IPNSLBtgorMSwuA-1; Mon, 23 Mar 2026 08:46:47 -0400 X-MC-Unique: NrWL8T_IPNSLBtgorMSwuA-1 X-Mimecast-MFC-AGG-ID: NrWL8T_IPNSLBtgorMSwuA_1774270006 Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-829a655f5e4so1433565b3a.1 for ; Mon, 23 Mar 2026 05:46:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1774270006; x=1774874806; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=zj+HJ3NHVe31Ua9iuOX+I5shCryiTmPAEr1rzKm60IA=; b=au8/bzNrqamLX3SGu2LgcicHx8MX+vBqijgNMz9k00Lv/5bZSzH3aIuq0FPRJ6sbmp P1mkb95uCowdmCVSuuXs5caqXysmcw/as/dt7odJszh3MXwU0rk6G2L88G6yaBY8G50b Bzw4Eb7+ZOG/c9qL1U3sUNm3pVtJdxOv5jpr2FUYQu2VigeNoF5EhMZFgkabW9vASATq eV35XobF3NjFxA6a0djNtjZdAiZewqSr1ZydQGRFbUw4F+2v+SyyC4BNhz7wPKILKzM+ mA0GcVQgz8k5J89Nzx1Af5yDEa4MRpzi+ALLu6v4Hv5Evn8wyCtcMQ0+VIOAfkeLUEe4 h/Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774270006; x=1774874806; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zj+HJ3NHVe31Ua9iuOX+I5shCryiTmPAEr1rzKm60IA=; b=q5G22t3hmDHVrL1xPG571+AED1dFewlskbWRO7noXGCiN+s7r+fPE8yoZ/ICb3Gbos JLkS9a67J/kUFwRCLuYnUA/t6lJ8Z/WqIA6aWW/7YUSduD47b6Sned3jLWbxoPQChv3A WtUMpxEnPtkLEiml0oZNOqWyvmT35wcHJ4kcqeEbWjlm7i+dkl4BUonyJXqjCM1x4Tcq bKypwc/NHzF2IWMm13+IfKzuaLetCAKWMaekmjzEVdWVbULPg/iw4ohBOnMn8tKCOzxb SU357As6rKGn1DoU6W+auulCrRzPD9qTm64RjfSa5NEk66oa8zLZZZge0W9lKzKOAV3F 0R6g== X-Forwarded-Encrypted: i=1; AJvYcCXI6SfAT1FFOGoXvHhhNsH2idFGpYfTKonzeGXGgE9qxPvns+di+PoYXYZ7dcOBv0Pb9e85Mu5MC3F04Wg=@vger.kernel.org X-Gm-Message-State: AOJu0YyqM1N3KXEwEhQTRTmpeCxmGrmHKvoYHVlWuT5r0nbwFWdLOZnG vJaHuktWn7O8riACXw8+N/hBrRa7w1tVwGiA8pFWbXhS9j5W9E6BZqVPaFaZ/ncKpnbQYq1hCzR atY37O3VoY9Nh/EruWKlpoxiCeeSOnmiuTqg6KUfmbi7gAnwLBjt8fthW97U5RmrdTw== X-Gm-Gg: ATEYQzzRV6GWcmLA5uqeNS7yYxv3XEVKHT2yIUbpEVUW57HTuji+C1uCFqJpTjrbBD7 2F1UD9ZZyrZrbnXCUxIx95tC6fJNUJAJ5IZEgL9j/bxgOzvEo7h+8NbO4DR3NPM+vxJiQe0dbqB K9V1oe5tZELZAEjgtky66tGp7CvRzcCdv9ATrg51rugOD+AXhuVyoW6hbdRBAsm+N+lbAQpF97G 8dqamtsJQLgp2Bhf1BbYBWhgL+mPEUKuA1cFDZ6Jyzn3BSSzwzym22/Sm590MoJdjc9Gx3A4GrO tmAKf0qxD+w125WjfiB/1DjxZ259vVrWrjKCRr29zTZejpAp4Oe0lm07f4J9rVKJpLlG+rQNsJw lsi+WSyXIY8Folbi0HQ== X-Received: by 2002:a05:6a00:2d1d:b0:829:7efc:fe20 with SMTP id d2e1a72fcca58-82a8c325f74mr10131675b3a.40.1774270006181; Mon, 23 Mar 2026 05:46:46 -0700 (PDT) X-Received: by 2002:a05:6a00:2d1d:b0:829:7efc:fe20 with SMTP id d2e1a72fcca58-82a8c325f74mr10131638b3a.40.1774270005733; Mon, 23 Mar 2026 05:46:45 -0700 (PDT) Received: from redhat.com ([209.132.188.88]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82b03aa5a9bsm9609699b3a.8.2026.03.23.05.46.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Mar 2026 05:46:44 -0700 (PDT) Date: Mon, 23 Mar 2026 20:46:42 +0800 From: Li Wang To: Waiman Long Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , Tejun Heo , Michal =?utf-8?Q?Koutn=C3=BD?= , Shuah Khan , Mike Rapoport , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Sean Christopherson , James Houghton , Sebastian Chlad , Guopeng Zhang , Li Wang Subject: Re: [PATCH v2 1/7] memcg: Scale up vmstats flush threshold with int_sqrt(nr_cpus+2) Message-ID: References: <20260320204241.1613861-1-longman@redhat.com> <20260320204241.1613861-2-longman@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260320204241.1613861-2-longman@redhat.com> On Fri, Mar 20, 2026 at 04:42:35PM -0400, Waiman Long wrote: > The vmstats flush threshold currently increases linearly with the > number of online CPUs. As the number of CPUs increases over time, it > will become increasingly difficult to meet the threshold and update the > vmstats data in a timely manner. These days, systems with hundreds of > CPUs or even thousands of them are becoming more common. > > For example, the test_memcg_sock test of test_memcontrol always fails > when running on an arm64 system with 128 CPUs. It is because the > threshold is now 64*128 = 8192. With 4k page size, it needs changes in > 32 MB of memory. It will be even worse with larger page size like 64k. > > To make the output of memory.stat more correct, it is better to scale > up the threshold slower than linearly with the number of CPUs. The > int_sqrt() function is a good compromise as suggested by Li Wang [1]. > An extra 2 is added to make sure that we will double the threshold for > a 2-core system. The increase will be slower after that. > > With the int_sqrt() scale, we can use the possibly larger > num_possible_cpus() instead of num_online_cpus() which may change at > run time. > > Although there is supposed to be a periodic and asynchronous flush of > vmstats every 2 seconds, the actual time lag between succesive runs > can actually vary quite a bit. In fact, I have seen time lags of up > to 10s of seconds in some cases. So we couldn't too rely on the hope > that there will be an asynchronous vmstats flush every 2 seconds. This > may be something we need to look into. > > [1] https://lore.kernel.org/lkml/ab0kAE7mJkEL9kWb@redhat.com/ > > Suggested-by: Li Wang > Signed-off-by: Waiman Long > --- > mm/memcontrol.c | 18 +++++++++++++----- > 1 file changed, 13 insertions(+), 5 deletions(-) > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 772bac21d155..cc1fc0f5aeea 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -548,20 +548,20 @@ struct memcg_vmstats { > * rstat update tree grow unbounded. > * > * 2) Flush the stats synchronously on reader side only when there are more than > - * (MEMCG_CHARGE_BATCH * nr_cpus) update events. Though this optimization > - * will let stats be out of sync by atmost (MEMCG_CHARGE_BATCH * nr_cpus) but > - * only for 2 seconds due to (1). > + * (MEMCG_CHARGE_BATCH * int_sqrt(nr_cpus+2)) update events. Though this > + * optimization will let stats be out of sync by up to that amount. This is > + * supposed to last for up to 2 seconds due to (1). > */ > static void flush_memcg_stats_dwork(struct work_struct *w); > static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork); > static u64 flush_last_time; > +static int vmstats_flush_threshold __ro_after_init; > > #define FLUSH_TIME (2UL*HZ) > > static bool memcg_vmstats_needs_flush(struct memcg_vmstats *vmstats) > { > - return atomic_read(&vmstats->stats_updates) > > - MEMCG_CHARGE_BATCH * num_online_cpus(); > + return atomic_read(&vmstats->stats_updates) > vmstats_flush_threshold; > } > > static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val, > @@ -5191,6 +5191,14 @@ int __init mem_cgroup_init(void) > > memcg_pn_cachep = KMEM_CACHE(mem_cgroup_per_node, > SLAB_PANIC | SLAB_HWCACHE_ALIGN); > + /* > + * Scale up vmstats flush threshold with int_sqrt(nr_cpus+2). The extra > + * 2 constant is to make sure that the threshold is double for a 2-core > + * system. After that, it will increase by MEMCG_CHARGE_BATCH when the > + * number of the CPUs reaches the next (2^n - 2) value. > + */ > + vmstats_flush_threshold = MEMCG_CHARGE_BATCH * > + (int_sqrt(num_possible_cpus() + 2)); > > return 0; > } Reviewed-by: Li Wang -- Regards, Li Wang