From nobody Wed Dec 17 06:28:22 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C2A0E81E1E for ; Fri, 6 Oct 2023 18:46:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233126AbjJFSqw (ORCPT ); Fri, 6 Oct 2023 14:46:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233269AbjJFSqn (ORCPT ); Fri, 6 Oct 2023 14:46:43 -0400 Received: from mail-oi1-x22a.google.com (mail-oi1-x22a.google.com [IPv6:2607:f8b0:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5B85D6; Fri, 6 Oct 2023 11:46:36 -0700 (PDT) Received: by mail-oi1-x22a.google.com with SMTP id 5614622812f47-3ae5ee80c0dso1535581b6e.3; Fri, 06 Oct 2023 11:46:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696617996; x=1697222796; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vY+Soyh4ezPzEsiTwAjxm3PLfRjJVUJ94CEs1TUSu9g=; b=J4fXz1Xk5EqxkJOGtF3SFwrKH5hRiQasPMJP75AvlgwJxXuNhr3Y/is41FytLfI/xT RVUqLYfLOW9MOKhosnnjAPgxyntvxNTcci9sNyAGTOuv4yPyXhslt5eSTm4+qiTHJLRH eG+LMzfx1iSY6uZUmuFKlF0hWPQrTK+dpBRFduvxoz55HCqO44btJas9yoJEGXaC4P4M qFzHdfwV+SxvCSkfaLWZhSrResgIgQhDddcBScuX1U4lVLIsLfxrG3YkmgP2c9JWlMFB IyfjuvMV8TtsNZXrgPjd0y/29d79OfIRRnvnXG46o/EU450yCIW1bs1qI/p+WBB3L2qQ YVvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696617996; x=1697222796; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vY+Soyh4ezPzEsiTwAjxm3PLfRjJVUJ94CEs1TUSu9g=; b=BB9O1P9OFoXsOAPeAvFgC+1CK7QftiqAuzG6hxuQZv5PXFApprQ4a0FcJPkSPocPQ9 qyt1Y+3oYWDK2eTpvCvEDVuVT4z1RQgQf2oGNfZ7ZEhcz5IvrtAXflWyDslDIp4/YPK3 WKDQXwAuNPHjSw16dfbmmo4s6ltE9dgtPJMwwfYI+AgWfibwis6IQ8/53mWS5AzHC2u/ tZrjgn2Q7qECYtznxVLZnrCSg45vt5/9RbNJibVUVcS1uFFN12F3DK3sHTnXsz69Zr46 hqgHyLJy5AiM/wXg5/o7bNuAh1Ybq9QDgboPrqnK3pVqwlk9nFLAJsToybvoydOzjvLP b4Ew== X-Gm-Message-State: AOJu0YyS4lpthtTdxz/nAVUqpNEdQUlUEoIwyQbdE7hf+10Dbymanxu6 +bm6zly2L+8xo9QQzFIBSxo= X-Google-Smtp-Source: AGHT+IEy8b9pbe0uvLgz2KimcopbbkeJwi6+OhwuqZtXmIDIBfGt5NNnJWLdEFwXkYOWepQdee4kyg== X-Received: by 2002:a54:4812:0:b0:3ad:c497:1336 with SMTP id j18-20020a544812000000b003adc4971336mr9048765oij.16.1696617995913; Fri, 06 Oct 2023 11:46:35 -0700 (PDT) Received: from localhost (fwdproxy-prn-013.fbsv.net. [2a03:2880:ff:d::face:b00c]) by smtp.gmail.com with ESMTPSA id a24-20020a637058000000b00581048ffc13sm3722085pgn.81.2023.10.06.11.46.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 11:46:35 -0700 (PDT) From: Nhat Pham To: akpm@linux-foundation.org Cc: riel@surriel.com, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, tj@kernel.org, lizefan.x@bytedance.com, shuah@kernel.org, mike.kravetz@oracle.com, yosryahmed@google.com, fvdl@google.com, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org Subject: [PATCH v4 4/4] selftests: add a selftest to verify hugetlb usage in memcg Date: Fri, 6 Oct 2023 11:46:29 -0700 Message-Id: <20231006184629.155543-5-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231006184629.155543-1-nphamcs@gmail.com> References: <20231006184629.155543-1-nphamcs@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch add a new kselftest to demonstrate and verify the new hugetlb memcg accounting behavior. Signed-off-by: Nhat Pham --- MAINTAINERS | 2 + tools/testing/selftests/cgroup/.gitignore | 1 + tools/testing/selftests/cgroup/Makefile | 2 + .../selftests/cgroup/test_hugetlb_memcg.c | 234 ++++++++++++++++++ 4 files changed, 239 insertions(+) create mode 100644 tools/testing/selftests/cgroup/test_hugetlb_memcg.c diff --git a/MAINTAINERS b/MAINTAINERS index bf0f54c24f81..ce9f40bcc2ba 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5269,6 +5269,7 @@ S: Maintained F: mm/memcontrol.c F: mm/swap_cgroup.c F: tools/testing/selftests/cgroup/memcg_protection.m +F: tools/testing/selftests/cgroup/test_hugetlb_memcg.c F: tools/testing/selftests/cgroup/test_kmem.c F: tools/testing/selftests/cgroup/test_memcontrol.c =20 @@ -9652,6 +9653,7 @@ F: include/linux/hugetlb.h F: mm/hugetlb.c F: mm/hugetlb_vmemmap.c F: mm/hugetlb_vmemmap.h +F: tools/testing/selftests/cgroup/test_hugetlb_memcg.c =20 HVA ST MEDIA DRIVER M: Jean-Christophe Trotin diff --git a/tools/testing/selftests/cgroup/.gitignore b/tools/testing/self= tests/cgroup/.gitignore index af8c3f30b9c1..2732e0b29271 100644 --- a/tools/testing/selftests/cgroup/.gitignore +++ b/tools/testing/selftests/cgroup/.gitignore @@ -7,4 +7,5 @@ test_kill test_cpu test_cpuset test_zswap +test_hugetlb_memcg wait_inotify diff --git a/tools/testing/selftests/cgroup/Makefile b/tools/testing/selfte= sts/cgroup/Makefile index c27f05f6ce9b..00b441928909 100644 --- a/tools/testing/selftests/cgroup/Makefile +++ b/tools/testing/selftests/cgroup/Makefile @@ -14,6 +14,7 @@ TEST_GEN_PROGS +=3D test_kill TEST_GEN_PROGS +=3D test_cpu TEST_GEN_PROGS +=3D test_cpuset TEST_GEN_PROGS +=3D test_zswap +TEST_GEN_PROGS +=3D test_hugetlb_memcg =20 LOCAL_HDRS +=3D $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidf= d.h =20 @@ -27,3 +28,4 @@ $(OUTPUT)/test_kill: cgroup_util.c $(OUTPUT)/test_cpu: cgroup_util.c $(OUTPUT)/test_cpuset: cgroup_util.c $(OUTPUT)/test_zswap: cgroup_util.c +$(OUTPUT)/test_hugetlb_memcg: cgroup_util.c diff --git a/tools/testing/selftests/cgroup/test_hugetlb_memcg.c b/tools/te= sting/selftests/cgroup/test_hugetlb_memcg.c new file mode 100644 index 000000000000..f0fefeb4cc24 --- /dev/null +++ b/tools/testing/selftests/cgroup/test_hugetlb_memcg.c @@ -0,0 +1,234 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include "../kselftest.h" +#include "cgroup_util.h" + +#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) + +/* borrowed from mm/hmm-tests.c */ +static long get_hugepage_size(void) +{ + int fd; + char buf[2048]; + int len; + char *p, *q, *path =3D "/proc/meminfo", *tag =3D "Hugepagesize:"; + long val; + + fd =3D open(path, O_RDONLY); + if (fd < 0) { + /* Error opening the file */ + return -1; + } + + len =3D read(fd, buf, sizeof(buf)); + close(fd); + if (len < 0) { + /* Error in reading the file */ + return -1; + } + if (len =3D=3D sizeof(buf)) { + /* Error file is too large */ + return -1; + } + buf[len] =3D '\0'; + + /* Search for a tag if provided */ + if (tag) { + p =3D strstr(buf, tag); + if (!p) + return -1; /* looks like the line we want isn't there */ + p +=3D strlen(tag); + } else + p =3D buf; + + val =3D strtol(p, &q, 0); + if (*q !=3D ' ') { + /* Error parsing the file */ + return -1; + } + + return val; +} + +static int set_file(const char *path, long value) +{ + FILE *file; + int ret; + + file =3D fopen(path, "w"); + if (!file) + return -1; + ret =3D fprintf(file, "%ld\n", value); + fclose(file); + return ret; +} + +static int set_nr_hugepages(long value) +{ + return set_file("/proc/sys/vm/nr_hugepages", value); +} + +static unsigned int check_first(char *addr) +{ + return *(unsigned int *)addr; +} + +static void write_data(char *addr) +{ + unsigned long i; + + for (i =3D 0; i < LENGTH; i++) + *(addr + i) =3D (char)i; +} + +static int hugetlb_test_program(const char *cgroup, void *arg) +{ + char *test_group =3D (char *)arg; + void *addr; + long old_current, expected_current, current; + int ret =3D EXIT_FAILURE; + + 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)) { + 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; + } + + 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; + } + current =3D cg_read_long(test_group, "memory.current"); + if (current - old_current >=3D MB(2)) { + 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; + } + old_current =3D current; + + /* read the first page */ + check_first(addr); + expected_current =3D old_current + MB(2); + 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"); + ksft_print_msg( + "expected memory: %ld, actual memory: %ld\n", + expected_current, current); + goto out_failed_munmap; + } + + /* write to the whole range */ + write_data(addr); + current =3D cg_read_long(test_group, "memory.current"); + expected_current =3D old_current + MB(8); + if (!values_close(expected_current, current, 5)) { + ksft_print_msg("memory usage should increase by around 8MB.\n"); + ksft_print_msg( + "expected memory: %ld, actual memory: %ld\n", + expected_current, current); + goto out_failed_munmap; + } + + /* unmap the whole range */ + munmap(addr, LENGTH); + current =3D cg_read_long(test_group, "memory.current"); + expected_current =3D old_current; + if (!values_close(expected_current, current, 5)) { + ksft_print_msg("memory usage should go back down.\n"); + ksft_print_msg( + "expected memory: %ld, actual memory: %ld\n", + expected_current, current); + return ret; + } + + ret =3D EXIT_SUCCESS; + return ret; + +out_failed_munmap: + munmap(addr, LENGTH); + return ret; +} + +static int test_hugetlb_memcg(char *root) +{ + int ret =3D KSFT_FAIL; + char *test_group; + + test_group =3D cg_name(root, "hugetlb_memcg_test"); + if (!test_group || cg_create(test_group)) { + ksft_print_msg("fail to create cgroup.\n"); + goto out; + } + + if (cg_write(test_group, "memory.max", "100M")) { + ksft_print_msg("fail to set cgroup memory limit.\n"); + goto out; + } + + /* disable swap */ + if (cg_write(test_group, "memory.swap.max", "0")) { + ksft_print_msg("fail to disable swap.\n"); + goto out; + } + + if (!cg_run(test_group, hugetlb_test_program, (void *)test_group)) + ret =3D KSFT_PASS; +out: + cg_destroy(test_group); + free(test_group); + return ret; +} + +int main(int argc, char **argv) +{ + char root[PATH_MAX]; + int ret =3D EXIT_SUCCESS, has_memory_hugetlb_acc; + + has_memory_hugetlb_acc =3D proc_mount_contains("memory_hugetlb_accounting= "); + if (has_memory_hugetlb_acc < 0) + ksft_exit_skip("Failed to query cgroup mount option\n"); + else if (!has_memory_hugetlb_acc) + ksft_exit_skip("memory hugetlb accounting is disabled\n"); + + /* Unit is kB! */ + if (get_hugepage_size() !=3D 2048) { + ksft_print_msg("test_hugetlb_memcg requires 2MB hugepages\n"); + ksft_test_result_skip("test_hugetlb_memcg\n"); + return ret; + } + + if (cg_find_unified_root(root, sizeof(root))) + ksft_exit_skip("cgroup v2 isn't mounted\n"); + + switch (test_hugetlb_memcg(root)) { + case KSFT_PASS: + ksft_test_result_pass("test_hugetlb_memcg\n"); + break; + case KSFT_SKIP: + ksft_test_result_skip("test_hugetlb_memcg\n"); + break; + default: + ret =3D EXIT_FAILURE; + ksft_test_result_fail("test_hugetlb_memcg\n"); + break; + } + + return ret; +} --=20 2.34.1