From nobody Mon Apr 6 18:24:12 2026 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (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 7751832AABC; Mon, 6 Apr 2026 09:21:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775467264; cv=none; b=OjGTyv4Ce+GqUVKx+Qub6mrR+6TcpbcvfaPrbTjBohvfJrYwFquX3pWsnE+Od22UqfNgEln5BCNUXI29BoHZNBgZAIJ4yxY2BrZ0IMEuSSwUUT9cutlkpP6/68J+XLX9kxWzF8o1Hj/hpR2lo3UrQCIR6jXoOltl9h9QWzES2bs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775467264; c=relaxed/simple; bh=I9hKO3ZpHjbISwmdEF8CLOOaeVXybTGuoAfORzfgPcg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BQB4NDR8msXPiusRhdC7GX9F38kgY3lx49WlwaAFKrQ2vcNtxZ2wYVyS+LaL8s0mwUiyBkMgFovOJzDX93Qp9LtBPAQR6x/H9CtwHly5hzcMrOIUUtrMl5zqIdHMEBXJiHe20scZGnhVHYyLmVZt87KWGN/lCUj+AVparRRn5K8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=HCyNBrsG; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="HCyNBrsG" Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 635GnGdO3110257; Mon, 6 Apr 2026 09:20:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=pp1; bh=hR9Avs ESc0VB0jLe+QQG3Q1Tfmm/xPeUm+bAL5UGGaM=; b=HCyNBrsGgoDyc9Z7Kd9xLT ffrSA3KEavgwn0qfI6gwSvKJsuAdrOmLwp9ilyKiL4WC4vAsRdXr/zZTfICU1xEN QzGbkrA9DW+Utvyq4FCoHc6BeBqyk+LCge8iFRaZLWO4WpJFiIylXepXJM53TSZI 0zaiRekDsuymj+DIxSyFqOYhGviW/KGKFsiBGA3YcsDpuibyiXs+GkLNYf1V7zZm vTB9z3uHNQAhtc0hhigJQ6LD6/MmZnVLFxJGlXnMi5cnpeCcbSyZvD4cViuEpPJJ E30BV8eufjiLvCEpIkBc+yORAC9W150PQYlgZI3+lGvI7ZaqQEt682byis6Uwygw == Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4datap5vrc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 06 Apr 2026 09:20:44 +0000 (GMT) Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 6364i7Mf021117; Mon, 6 Apr 2026 09:20:43 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4dbdbyc5jd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 06 Apr 2026 09:20:43 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 6369Kd6a51446198 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 6 Apr 2026 09:20:39 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id EC7E920043; Mon, 6 Apr 2026 09:20:38 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3A56520040; Mon, 6 Apr 2026 09:20:35 +0000 (GMT) Received: from li-3f69264c-2ce6-11b2-a85c-c3f414d24014.bl1-in.ibm.com (unknown [9.123.3.209]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 6 Apr 2026 09:20:34 +0000 (GMT) From: Sayali Patil To: Andrew Morton , Shuah Khan , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Ritesh Harjani Cc: David Hildenbrand , Zi Yan , Michal Hocko , Oscar Salvador , Lorenzo Stoakes , Dev Jain , Liam.Howlett@oracle.com, linuxppc-dev@lists.ozlabs.org, Miaohe Lin , Venkat Rao Bagalkote , Sayali Patil Subject: [PATCH v4 15/15] selftests/cgroup: extend test_hugetlb_memcg.c to support all huge page sizes Date: Mon, 6 Apr 2026 14:49:34 +0530 Message-ID: X-Mailer: git-send-email 2.52.0 In-Reply-To: References: 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="utf-8" Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-Reinject: loops=2 maxloops=12 X-Authority-Analysis: v=2.4 cv=Bp+QAIX5 c=1 sm=1 tr=0 ts=69d37aec cx=c_pps a=5BHTudwdYE3Te8bg5FgnPg==:117 a=5BHTudwdYE3Te8bg5FgnPg==:17 a=IkcTkHD0fZMA:10 a=A5OVakUREuEA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=uAbxVGIbfxUO_5tXvNgY:22 a=VnNF1IyMAAAA:8 a=4tZCw_sC0xjTHO7m73IA:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNDA2MDA5MCBTYWx0ZWRfX5n0Or/YQTGbD zXROJzFv7cLnirHO9BgQRuNEPQr+ZEVKCUhyxfdr3hxWBvM+7OLz8myBm2C7Z7B3zUT6MBslmAf AeBNQUtaXcQV+vBnj17KLPuQ2WDiEz0YZTpkZ6pKjYKxV5xz4oHmBU0fTFPOADbWuCEEazlLu7B biHajIZRQsJuqBbpt0h3v6UnI/zY2PYOi55yanRYBPyUDR8ToKc4HPmF25rIdQC1Ik+eNbssUV0 Gl9+XMJsQx9wjZS/h+232McNGZ1gcI7clYMqH9Rav6y69rhHLMDuea2g7BdjVbwYjRa262KMrDa gOajp5FgX7CMdet1sEEaSs+XMJJlGDMd83IPNNBLdfZvnZLknWjpET6qdoQNK45+IAoSOAb3w+g uRsXNs6xF+EBOKbL+K5qBiYXSsiyp23/kFkQw7uovc7esvIWc82ndcaYzyKTr+t3I0o5G5AL7A0 8Kg8KQzjRVWmnmTBj2A== X-Proofpoint-GUID: UQqDuCzvxJXuNF9VCUai46TOluqGlLmY X-Proofpoint-ORIG-GUID: 70yvyT2i2bR0oABye0WkT-5h7pHBZuNW X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-04-06_02,2026-04-03_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 malwarescore=0 suspectscore=0 adultscore=0 impostorscore=0 spamscore=0 priorityscore=1501 lowpriorityscore=0 bulkscore=0 clxscore=1015 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2603050001 definitions=main-2604060090 The hugetlb memcg selftest was previously skipped when the configured huge page size was not 2MB, preventing the test from running on systems using other default huge page sizes. Detect the system's configured huge page size at runtime and use it for the allocation instead of assuming a fixed 2MB size. This allows the test to run on configurations using non-2MB huge pages and avoids unnecessary skips. Fixes: c0dddb7aa5f8 ("selftests: add a selftest to verify hugetlb usage in = memcg") Signed-off-by: Sayali Patil --- .../selftests/cgroup/test_hugetlb_memcg.c | 90 ++++++++++++++----- 1 file changed, 68 insertions(+), 22 deletions(-) diff --git a/tools/testing/selftests/cgroup/test_hugetlb_memcg.c b/tools/te= sting/selftests/cgroup/test_hugetlb_memcg.c index f451aa449be6..e6157a784138 100644 --- a/tools/testing/selftests/cgroup/test_hugetlb_memcg.c +++ b/tools/testing/selftests/cgroup/test_hugetlb_memcg.c @@ -7,15 +7,21 @@ #include #include #include +#include #include "kselftest.h" #include "cgroup_util.h" =20 #define ADDR ((void *)(0x0UL)) #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB) -/* mapping 8 MBs =3D=3D 4 hugepages */ -#define LENGTH (8UL*1024*1024) #define PROTECTION (PROT_READ | PROT_WRITE) =20 +/* + * This value matches the kernel's MEMCG_CHARGE_BATCH definition: + * see include/linux/memcontrol.h. If the kernel value changes, this + * test constant must be updated accordingly to stay consistent. + */ +#define MEMCG_CHARGE_BATCH 64U + /* borrowed from mm/hmm-tests.c */ static long get_hugepage_size(void) { @@ -84,11 +90,11 @@ static unsigned int check_first(char *addr) return *(unsigned int *)addr; } =20 -static void write_data(char *addr) +static void write_data(char *addr, size_t length) { unsigned long i; =20 - for (i =3D 0; i < LENGTH; i++) + for (i =3D 0; i < length; i++) *(addr + i) =3D (char)i; } =20 @@ -96,26 +102,33 @@ static int hugetlb_test_program(const char *cgroup, vo= id *arg) { char *test_group =3D (char *)arg; void *addr; + long hpage_size, batch_bytes; long old_current, expected_current, current; int ret =3D EXIT_FAILURE; + size_t length; + int pagesize, nr_pages; + + pagesize =3D getpagesize(); + hpage_size =3D get_hugepage_size() * 1024; + length =3D 4 * hpage_size; + batch_bytes =3D MEMCG_CHARGE_BATCH * pagesize; =20 old_current =3D cg_read_long(test_group, "memory.current"); set_nr_hugepages(20); current =3D cg_read_long(test_group, "memory.current"); - if (current - old_current >=3D MB(2)) { + if (current - old_current >=3D hpage_size) { ksft_print_msg( "setting nr_hugepages should not increase hugepage usage.\n"); ksft_print_msg("before: %ld, after: %ld\n", old_current, current); return EXIT_FAILURE; } =20 - addr =3D mmap(ADDR, LENGTH, PROTECTION, FLAGS, 0, 0); - if (addr =3D=3D MAP_FAILED) { - ksft_print_msg("fail to mmap.\n"); - return EXIT_FAILURE; - } + addr =3D mmap(ADDR, length, PROTECTION, FLAGS, 0, 0); + if (addr =3D=3D MAP_FAILED) + ksft_exit_skip("mmap failed, not enough memory.\n"); + current =3D cg_read_long(test_group, "memory.current"); - if (current - old_current >=3D MB(2)) { + if (current - old_current >=3D hpage_size) { ksft_print_msg("mmap should not increase hugepage usage.\n"); ksft_print_msg("before: %ld, after: %ld\n", old_current, current); goto out_failed_munmap; @@ -124,10 +137,34 @@ static int hugetlb_test_program(const char *cgroup, v= oid *arg) =20 /* read the first page */ check_first(addr); - expected_current =3D old_current + MB(2); + nr_pages =3D hpage_size / pagesize; + expected_current =3D old_current + hpage_size; current =3D cg_read_long(test_group, "memory.current"); - if (!values_close(expected_current, current, 5)) { - ksft_print_msg("memory usage should increase by around 2MB.\n"); + if (nr_pages < MEMCG_CHARGE_BATCH && + (current =3D=3D old_current || + values_close(old_current + batch_bytes, current, 5))) { + /* + * Memory cgroup charging uses per-CPU stocks and batched updates to the + * memcg usage counters. For hugetlb allocations, the number of pages + * that memcg charges is expressed in base pages (nr_pages), not + * in hugepage units. When the charge for an allocation is smaller than + * the internal batching threshold (nr_pages < MEMCG_CHARGE_BATCH), + * it may be fully satisfied from the CPU=E2=80=99s local stock. In such + * cases memory.current does not necessarily increase. + * + * If the local stock is insufficient, it may be refilled in batches + * of MEMCG_CHARGE_BATCH base pages, causing memory.current to increase + * by more than the allocation size. + * + * Therefore, Treat both a zero delta and a batched increase as a + * valid behaviour here. + */ + if (current =3D=3D old_current) + ksft_print_msg("allocation consumed from local stock.\n"); + else + ksft_print_msg("memcg charge batched via stock refill.\n"); + } else if (!values_close(expected_current, current, 5)) { + ksft_print_msg("memory usage should increase by ~1 huge page.\n"); ksft_print_msg( "expected memory: %ld, actual memory: %ld\n", expected_current, current); @@ -135,11 +172,11 @@ static int hugetlb_test_program(const char *cgroup, v= oid *arg) } =20 /* write to the whole range */ - write_data(addr); + write_data(addr, length); current =3D cg_read_long(test_group, "memory.current"); - expected_current =3D old_current + MB(8); + expected_current =3D old_current + length; if (!values_close(expected_current, current, 5)) { - ksft_print_msg("memory usage should increase by around 8MB.\n"); + ksft_print_msg("memory usage should increase by around 4 huge pages.\n"); ksft_print_msg( "expected memory: %ld, actual memory: %ld\n", expected_current, current); @@ -147,7 +184,7 @@ static int hugetlb_test_program(const char *cgroup, voi= d *arg) } =20 /* unmap the whole range */ - munmap(addr, LENGTH); + munmap(addr, length); current =3D cg_read_long(test_group, "memory.current"); expected_current =3D old_current; if (!values_close(expected_current, current, 5)) { @@ -162,14 +199,17 @@ static int hugetlb_test_program(const char *cgroup, v= oid *arg) return ret; =20 out_failed_munmap: - munmap(addr, LENGTH); + munmap(addr, length); return ret; } =20 static int test_hugetlb_memcg(char *root) { int ret =3D KSFT_FAIL; + int num_pages =3D 20; + long hpage_size =3D get_hugepage_size(); char *test_group; + uint64_t limit; =20 test_group =3D cg_name(root, "hugetlb_memcg_test"); if (!test_group || cg_create(test_group)) { @@ -177,7 +217,9 @@ static int test_hugetlb_memcg(char *root) goto out; } =20 - if (cg_write(test_group, "memory.max", "100M")) { + limit =3D (uint64_t)num_pages * hpage_size * 1024ULL; + + if (cg_write_numeric(test_group, "memory.max", limit)) { ksft_print_msg("fail to set cgroup memory limit.\n"); goto out; } @@ -200,6 +242,7 @@ int main(int argc, char **argv) { char root[PATH_MAX]; int ret =3D EXIT_SUCCESS, has_memory_hugetlb_acc; + long val; =20 has_memory_hugetlb_acc =3D proc_mount_contains("memory_hugetlb_accounting= "); if (has_memory_hugetlb_acc < 0) @@ -208,12 +251,15 @@ int main(int argc, char **argv) ksft_exit_skip("memory hugetlb accounting is disabled\n"); =20 /* Unit is kB! */ - if (get_hugepage_size() !=3D 2048) { - ksft_print_msg("test_hugetlb_memcg requires 2MB hugepages\n"); + val =3D get_hugepage_size(); + if (val < 0) { + ksft_print_msg("Failed to read hugepage size\n"); ksft_test_result_skip("test_hugetlb_memcg\n"); return ret; } =20 + ksft_print_msg("Hugepage size: %ld kB\n", val); + if (cg_find_unified_root(root, sizeof(root), NULL)) ksft_exit_skip("cgroup v2 isn't mounted\n"); =20 --=20 2.52.0