From: Kuniyuki Iwashima <kuniyu@google.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>
Cc: Muchun Song <muchun.song@linux.dev>,
Kuniyuki Iwashima <kuniyu@google.com>,
Kuniyuki Iwashima <kuni1840@gmail.com>,
linux-mm@kvack.org, cgroups@vger.kernel.org
Subject: [PATCH 5/5] selftests: cgroup: Deflake test_memcg_sock() in test_memcontrol.c.
Date: Fri, 16 Oct 2026 05:27:49 +0000 [thread overview]
Message-ID: <20261016052811.3085635-6-kuniyu@google.com> (raw)
In-Reply-To: <20261016052811.3085635-1-kuniyu@google.com>
With MEMCG_SOCK batched in per-CPU memcg_stock, test_memcg_sock()
can fail in two places:
1. In tcp_client(), reading memory.stat only flushes rstat
synchronously when vmstats->stats_updates exceeds
MEMCG_CHARGE_BATCH * num_online_cpus(). Because mod_memcg_state()
is now called less frequently, 16 read() iterations (0x10) on a
64-CPU system can finish in a few milliseconds before
stats_updates reaches the 4096-page threshold or the 2-second
periodic flusher (FLUSH_TIME) runs, leaving "sock " at 0.
2. After the TCP connection is closed, up to MEMCG_CHARGE_BATCH - 1
pages of MEMCG_SOCK delta can remain cached in per-CPU
stock->nr_sock[] (e.g. when the initial charge published +n via
try_charge_memcg() and mod_memcg_state(), while the uncharge was
absorbed into stock->nr_sock[] as -n without overflowing
stock->nr_pages[]). Since the periodic rstat flusher does not
drain memcg_stock, "sock " in memory.stat can stay non-zero across
cg_read_key_long_poll().
Let's increase retries in tcp_client() from 0x10 to 0x40 and write
"0" to memory.high before polling memory.stat at the end of
test_memcg_sock() to trigger drain_all_stock() and flush
stock->nr_sock[].
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
tools/testing/selftests/cgroup/test_memcontrol.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c
index 0ed82347044e..1311aa5bc522 100644
--- a/tools/testing/selftests/cgroup/test_memcontrol.c
+++ b/tools/testing/selftests/cgroup/test_memcontrol.c
@@ -1333,7 +1333,7 @@ static int tcp_client(const char *cgroup, unsigned short port)
const char server[] = "localhost";
struct addrinfo *ai;
char servport[6];
- int retries = 0x10; /* nice round number */
+ int retries = 0x40; /* nice round number */
int sk, ret;
long allocated;
@@ -1447,6 +1447,13 @@ static int test_memcg_sock(const char *root)
if (cg_read_long(memcg, "memory.current") < 0)
goto cleanup;
+ /*
+ * Drain per-CPU memcg_stock so cached MEMCG_SOCK deltas in
+ * stock->nr_sock[] are flushed to vmstats.
+ */
+ if (cg_write(memcg, "memory.high", "0"))
+ goto cleanup;
+
/*
* memory.stat is updated asynchronously via the memcg rstat
* flushing worker, which runs periodically (every 2 seconds,
--
2.56.0.212.g129ede20d9-goog
prev parent reply other threads:[~2026-10-16 5:28 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-16 5:27 [PATCH 0/5] memcg: Batch mod_memcg_state() for MEMCG_SOCK Kuniyuki Iwashima
2026-10-16 5:27 ` [PATCH 1/5] memcg: Remove unnecessary READ_ONCE() under memcg_stock.lock Kuniyuki Iwashima
2026-10-16 5:27 ` [PATCH 2/5] memcg: Reduce nesting in consume_stock() and refill_stock() Kuniyuki Iwashima
2026-10-16 5:27 ` [PATCH 3/5] memcg: Annotate slow paths " Kuniyuki Iwashima
2026-10-16 5:27 ` [PATCH 4/5] memcg: Batch mod_memcg_state() for MEMCG_SOCK Kuniyuki Iwashima
2026-10-16 5:27 ` Kuniyuki Iwashima [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261016052811.3085635-6-kuniyu@google.com \
--to=kuniyu@google.com \
--cc=akpm@linux-foundation.org \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=kuni1840@gmail.com \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox