From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (unknown [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id CA99D8045D for ; Mon, 19 Oct 2026 19:49:02 +0000 (UTC) Received: from mail.openvz.org (localhost [127.0.0.1]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 69JJll1m025928; Mon, 19 Oct 2026 22:47:48 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 69JJll1m025928 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="w2KpG+HM" Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 69JJlcQs025910 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Mon, 19 Oct 2026 22:47:38 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 69JJlcQs025910 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4a16dc2323cso21669265e9.3 for ; Mon, 19 Oct 2026 12:47:38 -0700 (PDT) X-Gm-Message-State: AFq9FYIfn8K7gFt5I8flC4T5dDTA1+e9uEH9r2Z4sDu4fIdYlq6mOaG2 BSlNi7LI9A6g1rrLU77ooqriSPCDnR4gw1WUKAM8sJzfyJzuAIKy7kWefPyqmY1cQFnaREtcdiY 5oq+BN52NStDPJNZBDSnoDERtCXHpATUWkGk7Uc+tz4H1leEeNfeYeg== X-Gm-Gg: AYBFou3tSGX5uaW9P7oZd3mAiwfM+LTNB4razW7bfkFLAzuBXzYCtPEBfatskyUHlwG ACffxnvUaWGYs6V5gbv49PMYm0vClfHLNq1v4qCRxB9UkLyDS21pwgKF0+8O7Lg8vaXCYtksLWu MdwqS4UHA3akhdz1TyNYD78XO6ibsj/GltxEJw4hMNh5JAd/QF3LnN9fw4zz7RBu91OrEsLlyVN S18z4jtusc1ch3VZL/sb5/Jvef46oS5a2y/nS0aGBsqhyuA/Jepm7X3TBHE8P/tOzYJsMdjQfmb qavALi1mTPgfq876BSqc+57gwgggjIfxigUQJSZFqpQ292h1482n3HwCKmqvTPdvuGQEa7jjShA 6plGVUN76tawEpLth X-Received: by 2002:a05:600c:638e:b0:4a1:e6ad:baa3 with SMTP id 5b1f17b1804b1-4a1e6adbbe4mr89008695e9.15.1792439258459; Mon, 19 Oct 2026 12:47:38 -0700 (PDT) X-Received: by 2002:a05:600c:638e:b0:4a1:e6ad:baa3 with SMTP id 5b1f17b1804b1-4a1e6adbbe4mr89008285e9.15.1792439257825; Mon, 19 Oct 2026 12:47:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1792439257; cv=none; d=google.com; s=arc-20260327; b=drt8GSSVqLLptU342j/ome3frkItW6bPN4gr+KjLhAvKdtU2tXKBXnHy+kW+abD2Vp bplIOSuN2y2LZPCuCEE+Xc757RokagyvprI7CTNn6xO0vJBvwxAhFWpmz9QJLVBRaDIy TbH/bEHrnFq7AHnAx4JYg0JKKv3dzJzlPTyjTlLqHzHwDsKlzJOcrXzDL1g2l3SIiOPM Elvaw4ntivj+XVo+7IBKqWe8M6wWjFma67n7QMrC82uj8Se/MPDtH4nUAFtgBSBg9Nwp zm/3IucA8XFD+LZ1onYKzuDqUjBGbHKdB/OEBWYLerk+LJ4HzkgftZp7B9Iu/MAjdtn0 eGGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:dkim-signature; bh=a9YEJXHixgchWJXvRc6kh1HtC86WQ+iRspTZ0Py0wjo=; fh=u94vpSnbACPnIkIPa1XdGqhjZt/kxdbqUA9AiFps/bg=; b=ZZUTeIJSAwBpr+ScAVPp3LcZsJ08Xv9H1yqxGLqIeqjGWjnY6fGYatAOXW8uu0ClvI fZ4Ka6FUZVadF98DeIyGqQ2I1yzm1ipZQ91fY6v9Al/SGJQWU2S6/cDlb3poGyQ62mlz H+7exMGUPJ1gBSINxhCNpGhn66dGjFptYe7APRJt05NvQaTsjiQjiqWNqrhYGAqQrk61 PB6EchpaEDb/2RjaDejFyMcR8ettvryA+prdZmeLROCWXuI/KzsW3yM5tEGUwL02Pqp8 LhVrH956X8izqRPlWLePFgxoIt0sa5cwmgiCfgcrpx2U4JGhn0Xw9lIZJjIvuh1UeuNp aUQQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=w2KpG+HM; spf=pass (google.com: domain of khorenko@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=khorenko@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Received: from relay.virtuozzo.com (relay.virtuozzo.com. [130.117.225.111]) by mx.google.com with ESMTPS id 5b1f17b1804b1-4a1f2db88d6si10691945e9.18.2026.10.19.12.47.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Oct 2026 12:47:37 -0700 (PDT) Received-SPF: pass (google.com: domain of khorenko@virtuozzo.com designates 130.117.225.111 as permitted sender) client-ip=130.117.225.111; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=w2KpG+HM; spf=pass (google.com: domain of khorenko@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=khorenko@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=Message-Id:Content-Type:MIME-Version:Subject:Date :From; bh=a9YEJXHixgchWJXvRc6kh1HtC86WQ+iRspTZ0Py0wjo=; b=w2KpG+HMy2mJMk849J5 oZ/ohOq2yDPbdkQ3aXA5gSWbHLzgA+mQURqbLZHmmki9q3H8rF/6YqcuB/M6lodsp1UpLquR8iWWX NnrjPVxgEImqPlNJTgc9v1cDt7Mve0W/cokWrCxDpAwyhoh6s9OafyjXRgcsOBgh9s1WgH50MqVwa f2e1WXakRmjt+L9CqVuyLHbi1U6EjplZUiVCG50cTMwg8YiE3R/pliiIG2RNYJOW//B3yJo4NCN6W 0FHiYd2uRLxH44UVoXHwHVEnTxjiXbhPmQ5kyU8+njZeruemVE2+3hcsYXbFqw4VZi0jaPUSphyT8 51ckngP6CWGGIYw==; Received: from ch-demo-asa.virtuozzo.com ([130.117.225.8] helo=f0.sw.ru) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1xItH2-009fSm-1Y; Mon, 19 Oct 2026 21:47:28 +0200 From: Konstantin Khorenko Date: Mon, 19 Oct 2026 21:47:34 +0200 MIME-Version: 1.0 Message-Id: <20261019-b4-prep-khorenko-vstor-149854-selftests-test_memcg_sock-v1-2-efbfa45dc967@virtuozzo.com> References: <20261019-b4-prep-khorenko-vstor-149854-selftests-test_memcg_sock-v1-0-efbfa45dc967@virtuozzo.com> In-Reply-To: <20261019-b4-prep-khorenko-vstor-149854-selftests-test_memcg_sock-v1-0-efbfa45dc967@virtuozzo.com> To: devel@openvz.org, Konstantin Khorenko X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=7971; i=khorenko@virtuozzo.com; h=from:subject:message-id; bh=DcjG3JjTp8cgukaKrkZVyObvGlC5nMDhi6kf3PzJN1s=; b=owEBbQKS/ZANAwAKAVGWCkf5YVwqAcsmYgBq1nPYdbnthCeqhRHQhnS7u/V6TbkzVs8LWd6eM GFFk1lCOU6JAjMEAAEKAB0WIQRWD23IPcXT0GO9CCxRlgpH+WFcKgUCatZz2AAKCRBRlgpH+WFc KrzLD/oDSRznFv8D+iVvXUf7YWFobb/UL8mw9ylCPmI3WzpOvHIHIBb9IG3kFhR5arHNNXGjchT IT/yMU5Vppscp4tqIQgAXRzsyTEIVYLZ05sLgRBGM4SgXNgewDUnO5Ncc38NGma2HiEbrgbgClv av2kDZs/2vRctGKPqNOD0WOAqOwm1gXZjlXOU1TfulpW1ila3K3lxWCH4TVt3yYyjU7fiy4oUG2 ALqvjVMc/fW9XStPnRPHrpN2tzOUC0jq3UkFj2v5+LCs6fqWeoIkcxIqYl3M8VyNImzf5GbCwSW wXYdQBaM+SvKV9ZWZzfQ5w5MIEyC95DlyApR+Zg8Nb+uQ05LiqgzOydCagajOf2PkSzSVrk4IkW M7HlC1fJhx0Lu0Bl5G1SQY4en9oFc8kkSH/EO4rwr5I3aI1oDM0xBzLF36LFzj25sJuFDK8m1RG mVmtFpO/Y4muKjhYp2nt8m3pzzQqUZo+EPQcjrkWQxXjwcJbyCaL6Eu23wQ82T7vED1PNXqEEc/ CD1o/npB9Zeuw5CLLtUYJfrvEl57xD4SRN+eueLuGb9JADJh9CNNorxS5aUizG+j79Beks1aCRr XczxwLzA0DrqRI4BRwF9Rj0iPVDgFO4e7cCvlUXVBOT/ItgTR0vboSx/tvzllxt5vyhalw7PFya Ex981Q1xyBWjOlg== X-Developer-Key: i=khorenko@virtuozzo.com; a=openpgp; fpr=560F6DC83DC5D3D063BD082C51960A47F9615C2A X-OZ-Fwd: true Subject: [Devel] [PATCH hci-8.1 2/2] ms/selftests: cgroup: make test_memcg_sock check flushed sock stats X-BeenThere: devel@openvz.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: OpenVZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: devel-bounces@openvz.org Errors-To: devel-bounces@openvz.org test_memcg_sock compares memory.current with the "sock " entry of memory.stat. memory.current changes at charge time, "sock " only when the memcg stats are flushed: by a memory.stat read once enough updates are pending (MEMCG_CHARGE_BATCH per online CPU), or by a periodic, deferrable worker. Nothing in the test makes sure a flush happens: - tcp_client() takes 16 readings right after connect(). If no flush lands among them, "sock " reads 0, and the test either passes on 0 == 0 without checking anything or, once memory.current shows the first charge, cannot pass any more and fails: not ok 11 test_memcg_sock - The final check waits 3 seconds for "sock " to drop to 0, counting on the deferrable worker, which can run later than that on an idle machine. Make the test cause the flushes instead of waiting for them. Every page of socket or anonymous memory that comes and goes in the cgroup counts towards the flush threshold, and MEMCG_FLUSH_BYTES (twice the vmstat error test_kmem allows for) is enough to cross it. After that much, either another flush has reset the count or the next memory.stat read flushes. - tcp_client() sets a small receive buffer before it connects. Only the server's socket is charged to the test cgroup, and with a large receive window its data moves straight on to the client; with few CPUs the client then drains it between reads and "sock " stays near 0. A small window keeps the data queued in the server's socket. - tcp_client() then keeps reading, comparing after every read, until "sock " is nonzero and matches memory.current, for up to four times MEMCG_FLUSH_BYTES. A comparison can still miss after a flush: one that came midway leaves "sock " behind, and memory.current also holds the per-CPU charge stock. The budget leaves room for several flushes. - After the server has exited, the test allocates and frees MEMCG_FLUSH_BYTES of anonymous memory in the cgroup and reads "sock ", for up to three rounds until it reads 0. The socket memory is uncharged before the first round, so any flush that has gone through all CPUs since sees "sock " at 0. A flush still running in parallel can reset the count before it reaches the CPU that did the uncharge, and the read then misses the uncharge. That flush only has to finish its walk over the CPUs for the next read to see it, and each round gives it the time of another allocation. Three rounds are a margin for a flusher that gets rescheduled midway, not a guarantee: nothing bounds how long it can be held up. MEMCG_CHARGE_BATCH goes to cgroup_util.h, and test_kmem's MAX_VMSTAT_ERROR uses it too. Assisted-by: LLM Signed-off-by: Konstantin Khorenko Signed-off-by: Tejun Heo (cherry picked from commit eb69e955de7bdea6aa932fc4cce413ca439a4a5d) Conflict: 6360d444ae32 ("selftests: cgroup: make test_memcg_sock robust against delayed sock stats") is not in the tree, so the final check here reads "sock " once instead of polling it. The flush rounds replace that single read, and test_memcg_sock() gets the sock_post declaration that commit added. The rest is context: - cgroup_util.h has no DEFAULT_WAIT_INTERVAL_US, MEMCG_CHARGE_BATCH goes after TEST_UID - test_kmem.c's MAX_VMSTAT_ERROR still uses 4096 for the page size - test_memcontrol.c includes "../kselftest.h" https://virtuozzo.atlassian.net/browse/VSTOR-149854 Feature: fix selftests Signed-off-by: Konstantin Khorenko --- .../selftests/cgroup/lib/include/cgroup_util.h | 3 ++ tools/testing/selftests/cgroup/test_kmem.c | 2 +- tools/testing/selftests/cgroup/test_memcontrol.c | 35 ++++++++++++++++++---- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/cgroup/lib/include/cgroup_util.h b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h index 0096bdbb757bd..4faebce101255 100644 --- a/tools/testing/selftests/cgroup/lib/include/cgroup_util.h +++ b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h @@ -13,6 +13,9 @@ #define TEST_UID 65534 /* usually nobody, any !root is fine */ +/* from include/linux/memcontrol.h, which userspace cannot include */ +#define MEMCG_CHARGE_BATCH 64 + /* * Checks if two given values differ by less than err% of their sum. */ diff --git a/tools/testing/selftests/cgroup/test_kmem.c b/tools/testing/selftests/cgroup/test_kmem.c index 96693d8772be2..56255771667f5 100644 --- a/tools/testing/selftests/cgroup/test_kmem.c +++ b/tools/testing/selftests/cgroup/test_kmem.c @@ -24,7 +24,7 @@ * the maximum discrepancy between charge and vmstat entries is number * of cpus multiplied by 64 pages. */ -#define MAX_VMSTAT_ERROR (4096 * 64 * get_nprocs()) +#define MAX_VMSTAT_ERROR (4096 * MEMCG_CHARGE_BATCH * get_nprocs()) static int alloc_dcache(const char *cgroup, void *arg) diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index 003c865d8db93..88960a9331c00 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -17,10 +17,15 @@ #include #include #include +#include #include "../kselftest.h" #include "cgroup_util.h" +/* enough page updates for a memory.stat read to flush, see MAX_VMSTAT_ERROR in test_kmem.c */ +#define MEMCG_FLUSH_BYTES \ + (2 * sysconf(_SC_PAGESIZE) * MEMCG_CHARGE_BATCH * get_nprocs()) + static bool has_localevents; static bool has_recursiveprot; @@ -1310,7 +1315,8 @@ 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 */ + long budget = 4 * MEMCG_FLUSH_BYTES; + int rcvbuf = 64 * 1024; int sk, ret; long allocated; @@ -1324,17 +1330,25 @@ static int tcp_client(const char *cgroup, unsigned short port) if (sk < 0) goto free_ainfo; + /* keep the data in the server's socket, the one charged to the cgroup */ + ret = setsockopt(sk, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)); + if (ret < 0) + goto close_sk; + ret = connect(sk, ai->ai_addr, ai->ai_addrlen); if (ret < 0) goto close_sk; ret = KSFT_FAIL; - while (retries--) { + while (budget > 0) { uint8_t buf[0x100000]; long current, sock; + ssize_t len; - if (read(sk, buf, sizeof(buf)) <= 0) + len = read(sk, buf, sizeof(buf)); + if (len <= 0) goto close_sk; + budget -= len; current = cg_read_long(cgroup, "memory.current"); sock = cg_read_key_long(cgroup, "memory.stat", "sock "); @@ -1343,7 +1357,7 @@ static int tcp_client(const char *cgroup, unsigned short port) goto close_sk; /* exclude the memory not related to socket connection */ - if (values_close(current - allocated, sock, 10)) { + if (sock > 0 && values_close(current - allocated, sock, 10)) { ret = KSFT_PASS; break; } @@ -1387,6 +1401,8 @@ static int test_memcg_sock(const char *root) int bind_retries = 5, ret = KSFT_FAIL, pid, err; unsigned short port; char *memcg; + long sock_post = -1; + int flush_rounds = 3; if (memcg_sockets_disabled()) return KSFT_SKIP; @@ -1438,7 +1454,16 @@ static int test_memcg_sock(const char *root) if (cg_read_long(memcg, "memory.current") < 0) goto cleanup; - if (cg_read_key_long(memcg, "memory.stat", "sock ")) + /* the uncharge is done, but a flush still walking the CPUs can hide it from a read */ + while (flush_rounds--) { + if (cg_run(memcg, alloc_anon, (void *)MEMCG_FLUSH_BYTES)) + goto cleanup; + + sock_post = cg_read_key_long(memcg, "memory.stat", "sock "); + if (!sock_post) + break; + } + if (sock_post) goto cleanup; ret = KSFT_PASS; -- 2.43.0 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel