From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E0225C3ABC9 for ; Thu, 15 May 2025 13:35:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A8B926B0095; Thu, 15 May 2025 09:35:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A3D946B0096; Thu, 15 May 2025 09:35:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 842966B0098; Thu, 15 May 2025 09:35:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 646726B0095 for ; Thu, 15 May 2025 09:35:40 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3A23CBB6BC for ; Thu, 15 May 2025 13:35:41 +0000 (UTC) X-FDA: 83445239682.24.26ADAD8 Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) by imf26.hostedemail.com (Postfix) with ESMTP id 491FF140016 for ; Thu, 15 May 2025 13:35:39 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=VH7IrM6r; spf=pass (imf26.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.222.173 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747316139; a=rsa-sha256; cv=none; b=gBAazRezVx8qRjBu29qH8CUNDcHWpLQ8SR1wqJKcLyqoh/DnNAnW0+xp9hWjX6a3tXkPl5 4qI73LJG8RIDYwNQy8JEHe6ERX4SU3Ah7nUYp2w7lgZjl+7QNCqxb5weFuA826m9YW55tV xcrmdFUAl35BboJTMZ3v4uyf5zup0Hs= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=VH7IrM6r; spf=pass (imf26.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.222.173 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1747316139; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wlNG5Htln8HKvc+6gp+8bgvPUce9d9z2JFDhosQneXo=; b=55VTTk6uW9W38TT69LNVB/iV1Ka8HYf2oDR7Nr0JBbkQwzp5E3iQdfI0uu1W4f95a9fLmN q8ot5GZtrEVAo3DrZWVF7Vbabu+QzuXYttZpu9Cilz9co1zgcAw5mXlU5EfOV+3XQPhjoy p5CzW6vJPPG2yF4u3RHQ4gQ5E1zJkzc= Received: by mail-qk1-f173.google.com with SMTP id af79cd13be357-7c5b8d13f73so105482985a.0 for ; Thu, 15 May 2025 06:35:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747316138; x=1747920938; darn=kvack.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=wlNG5Htln8HKvc+6gp+8bgvPUce9d9z2JFDhosQneXo=; b=VH7IrM6riAT41hZaIiVcCkYaRcaVIJTzI6KNUDGyf5vt3x9rRnLKQ4VSjWq57SkWTx tjBdr6SZa1JD6dzFLaHtho2ydBoYpCn51mX9YcL7ZFUbnDDeQTBu23tFNwGymu/wfQrA IwcixPYvosKSCFnvy+sEu7FfrAAg9b8ne1sYYu4e4cXk1ZpGKNVmsTPCH/NZaDqHnNc7 O386R+2cv2MPc96TO3QXAq/j6vWiU4Iupj3JW04cBedAndQH4W5t2fFfsmy8RBQIp3Ow M29Sdyyhx0RhQdxcvjLO4fJib5hAmcFzbE3/5JpAdoWGGmxHQ+rhECy3413aiGRbjzD0 BLZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747316138; x=1747920938; 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=wlNG5Htln8HKvc+6gp+8bgvPUce9d9z2JFDhosQneXo=; b=l4HdMEBQjKHdruzz2d0ReSwJEqyVXhVGg0TPHMwYpylhX4loqLM47wdtCYXoWklbnZ V1VesZ+VzL1lZbaN/UE6PYSl7qQ4USnsF18VH0a6aBn9l+2e6CqRCCAqkRKq28zpoeN/ bdQl/OHQabdt4REFGipB852rHO27GXcvtvN2jxC+i/Hf6DtKEkVObZrB+/AH712egkS5 35vKKYAmm7mzYwQ9sHosD6Sx/kuiimFhQuGZk1j4nkULi6xZ1+ECCDbtQXJrLUdqrwCU InQlbpbjmqUcgDeYeP+eQ9v4kyN8DCTAkrfbs+nI1WYdwh8FvlDQzqG2ASsIfPbRrYxI bdpw== X-Forwarded-Encrypted: i=1; AJvYcCVeSfskc2o+Lr+AaT38WR6SJ9BGB4TtUdPbkxm80gLU2WW/Dmk2tB3uWM2TJx6LG3uFlAMbR8cMEA==@kvack.org X-Gm-Message-State: AOJu0Yx/iilvwC7Q57MtI3qjr+wc9Y8tW/8boAVMXTMVDpNdzmh+vdNm yXTApviYDCL8/Knimdvsf3qK9i/55uYnagydynsZ9u6EF1LLBdXBKs5Girbx X-Gm-Gg: ASbGncuPOz5UpqhiZPAh6FglvnIUMTCT9qo/H2SBp/LvOYevzOoYH041EebmUcA58Ov q4Eev34+mfZteY1ynt5afKXaI4Chmabb8wIwbTY7eqh0SQ4KFoH7vDG5gMtE51500YDzXx9CF0Y y//AqEw7NaaaDG3BBbzYDHspp2Pnb6UpI5RHr4oR5FXdIjf0tKPoLLqbLFZjp7hMjPNGgGJcT5Y O6c0P4F3WSpgyp8IImboU5lH1xPSOofM5/rvnwuBkw/xIMvTxHE1tGvHRcn9CjfWBQ6HXH2Eucm xs0rygJDUYiQPVx6BdjX8s+7EuvBysPrQ+ZncCPFRRmIlN5q78TrQsqxslA= X-Google-Smtp-Source: AGHT+IEn86Vf0zD3MKqHNneuDkpt+TgrRPxRiPuPRhn5N/sfp5gibmt93u4lmV51ja/uctouLfRO6w== X-Received: by 2002:a05:6214:260d:b0:6f5:35fe:6a80 with SMTP id 6a1803df08f44-6f896e3f49emr111367906d6.11.1747316127235; Thu, 15 May 2025 06:35:27 -0700 (PDT) Received: from localhost ([2a03:2880:20ff:3::]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6f8a793fe1fsm5417566d6.108.2025.05.15.06.35.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 May 2025 06:35:26 -0700 (PDT) From: Usama Arif To: Andrew Morton , david@redhat.com, linux-mm@kvack.org Cc: hannes@cmpxchg.org, shakeel.butt@linux.dev, riel@surriel.com, ziy@nvidia.com, laoar.shao@gmail.com, baolin.wang@linux.alibaba.com, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kernel-team@meta.com, Usama Arif Subject: [PATCH 4/6] selftests: prctl: introduce tests for PR_THP_POLICY_DEFAULT_NOHUGE Date: Thu, 15 May 2025 14:33:33 +0100 Message-ID: <20250515133519.2779639-5-usamaarif642@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250515133519.2779639-1-usamaarif642@gmail.com> References: <20250515133519.2779639-1-usamaarif642@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 491FF140016 X-Stat-Signature: ab1k9si7u16hfq9iodk3bctxgsfqbhh8 X-HE-Tag: 1747316139-697192 X-HE-Meta: U2FsdGVkX1/izoBhqzTRkdq6uh//oXrASBdEargoAbTC9oiLn9sgTlQLXLVAN/gFJ9ujmZeYsXrOXr+u/OTwAyxLSfD+kVYrmAUTBsSmgEq79ViU866UTZKFD8+yKK4hjjtnvHRQwkvjroXWrMKAcWRaLiqeN0vls8Ag+nm6ecsxDd+55Awm538RoPvO5PYkG/3LSSyfVKYwQ9Vw9gyhTSDXt8DWxwXjsUO0OIO9F5Rw8gVHXvHdBDEp1VTLIzblQwu7ECNby4gWWZeaBudlkMCM50KYXShh0vAvhhb+D5VZI1RL+r17r3sInIMez50lcRTVStz2X3YXCFLdi8MZG23nYxuF0VA5T8DH7gPoI5upa9abmvRYtuPdx/+T0OgBQhHWYy0T7YhLN+L3yvo55StnJ/4NQZFg9x7w6QEnZ1or4mcdx5tJtwpV3oUcD9Du4tQiWhuAELxDG9L/WMdstyfOnBUbUl4ATXDYuhbY3gsdspR5P9aCEsXZp+KS+tdAnNA4GOs0x0CCdRdvRxh6M1ZfNTzyhEUejRpea09fWOhQEdhXJYOKtppvjZFA00yg6hAPMP4ALAj+pjXNRPEKa+AzlR7QtShuBtZAqJZfbTj+O0bbfXmBDpgd+gKsrMcqROYi/DuJFW/zUCmxyFUvvTPo08eoRIEM25lxEtQ/YXIZ2vAXCAC8T9jeknjh63lDjsBd3l3M7p94b/Rn+OdejGq+WWc0pifK7zVnzkSHcnybuqPVQJpuwK/mmaY18LTdeyixrxIQjrkIYm/GdMlHiQ3y+pGL1PdFLutCujd9/UjEqQLT2/FcU8FvxjV++bqYqfBzHmHTRSZxEFBVgXgsLXn9lU/SqirzDMZ+sPKg9FqdYJ7gEruV9C885ipEUjysGPGBvOi5o8/K32C+mgWwvTFXg129V+LtioMugu+SsSXOovHyiVaep23zL5BcZ5YluMkoicCDjSL2SGPYnEP BAammL96 KOkWGi/dVnu/36VlpZnd0MwvxBwPbmRNC+PMXkRBkzeoSAsnMzeKKbnquyXM1gJ05d8pwmkIzI1vldYYiggyvgOJ8Edlbc+21uHdXpv0lEVeTn0ei/89UVIp4zDDmADlMUiIxV4hR116EngbwtBgI932Tk8IXpWSFeXY1a/nwbvx/GftfSixBJhn34/JGbTddUPykPO7vHIM3y+D9bxBLuhs/AEmVsgguNuZBztv4OasHUZX/b3HkmMZqL3vivYl1wwZQCthstI/sNyLc+oYqE++csIa/OUFIRwqdAxfEvzmr67jukfwS6EjYTjfzCaqcT1Kv9gTVyPBEjYAcFxOcoZdmltDsMMLHzEqA7MfzWcEMnPLIa37gJlDfIISN6Mfc+9XHGj+y7F4YpxkuhDey+S8D7Lx0H2N/2e1GiN01hk84qnsAkJpVL4lUiKKtpP+JeVSV0UxEGof5DLyiOIWipRpFh6qSU2QQtLCNlXiMw+vPV7prhVBGCUP86Y5QTikcaotiX3XghHiyDkNqEOzR/2Gr4JubWlWsoLfAvzDqnYABZJOPZidl91lJzafqO3db+fOIeYWXWMFlO+c= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The test is limited to 2M PMD THPs. It does not modify the system settings in order to not disturb other process running in the system. It checks if the PMD size is 2M, if the 2M policy is set to inherit and if the system global THP policy is set to "always", so that the change in behaviour due to PR_THP_POLICY_DEFAULT_NOHUGE can be seen. This tests if: - the process can successfully set the policy - carry it over to the new process with fork - if no hugepage is gotten when the process doesn't MADV_HUGEPAGE - if hugepage is gotten when the process does MADV_HUGEPAGE - the process can successfully reset the policy to PR_THP_POLICY_SYSTEM - if hugepage is gotten after the policy reset Signed-off-by: Usama Arif --- tools/testing/selftests/prctl/Makefile | 2 +- tools/testing/selftests/prctl/thp_policy.c | 214 +++++++++++++++++++++ 2 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/prctl/thp_policy.c diff --git a/tools/testing/selftests/prctl/Makefile b/tools/testing/selftests/prctl/Makefile index 01dc90fbb509..ee8c98e45b53 100644 --- a/tools/testing/selftests/prctl/Makefile +++ b/tools/testing/selftests/prctl/Makefile @@ -5,7 +5,7 @@ ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/) ifeq ($(ARCH),x86) TEST_PROGS := disable-tsc-ctxt-sw-stress-test disable-tsc-on-off-stress-test \ - disable-tsc-test set-anon-vma-name-test set-process-name + disable-tsc-test set-anon-vma-name-test set-process-name thp_policy all: $(TEST_PROGS) include ../lib.mk diff --git a/tools/testing/selftests/prctl/thp_policy.c b/tools/testing/selftests/prctl/thp_policy.c new file mode 100644 index 000000000000..e39872a6d429 --- /dev/null +++ b/tools/testing/selftests/prctl/thp_policy.c @@ -0,0 +1,214 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * This test covers the PR_GET/SET_THP_POLICY functionality of prctl calls + */ +#include +#include +#include +#include +#include +#include +#include + +#ifndef PR_SET_THP_POLICY +#define PR_SET_THP_POLICY 78 +#define PR_GET_THP_POLICY 79 +#define PR_THP_POLICY_DEFAULT_HUGE 0 +#define PR_THP_POLICY_DEFAULT_NOHUGE 1 +#define PR_THP_POLICY_SYSTEM 2 +#endif + +#define CONTENT_SIZE 256 +#define BUF_SIZE (12 * 2 * 1024 * 1024) // 12 x 2MB pages + +enum system_policy { + SYSTEM_POLICY_ALWAYS, + SYSTEM_POLICY_MADVISE, + SYSTEM_POLICY_NEVER, +}; + +int system_thp_policy; + +/* check if the sysfs file contains the expected substring */ +static int check_file_content(const char *file_path, const char *expected_substring) +{ + FILE *file = fopen(file_path, "r"); + char buffer[CONTENT_SIZE]; + + if (!file) { + perror("Failed to open file"); + return -1; + } + if (fgets(buffer, CONTENT_SIZE, file) == NULL) { + perror("Failed to read file"); + fclose(file); + return -1; + } + fclose(file); + // Remove newline character from the buffer + buffer[strcspn(buffer, "\n")] = '\0'; + if (strstr(buffer, expected_substring)) + return 0; + else + return 1; +} + +/* + * The test is designed for 2M hugepages only. + * Check if hugepage size is 2M, if 2M size inherits from global + * setting, and if the global setting is madvise or always. + */ +static int sysfs_check(void) +{ + int res = 0; + + res = check_file_content("/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", "2097152"); + if (res) { + printf("hpage_pmd_size is not set to 2MB. Skipping test.\n"); + return -1; + } + res |= check_file_content("/sys/kernel/mm/transparent_hugepage/hugepages-2048kB/enabled", + "[inherit]"); + if (res) { + printf("hugepages-2048kB does not inherit global setting. Skipping test.\n"); + return -1; + } + + res = check_file_content("/sys/kernel/mm/transparent_hugepage/enabled", "[madvise]"); + if (!res) { + system_thp_policy = SYSTEM_POLICY_MADVISE; + return 0; + } + res = check_file_content("/sys/kernel/mm/transparent_hugepage/enabled", "[always]"); + if (!res) { + system_thp_policy = SYSTEM_POLICY_ALWAYS; + return 0; + } + printf("Global THP policy not set to madvise or always. Skipping test.\n"); + return -1; +} + +static int check_smaps_for_huge(void) +{ + FILE *file = fopen("/proc/self/smaps", "r"); + int is_anonhuge = 0; + char line[256]; + + if (!file) { + perror("fopen"); + return -1; + } + + while (fgets(line, sizeof(line), file)) { + if (strstr(line, "AnonHugePages:") && strstr(line, "24576 kB")) { + is_anonhuge = 1; + break; + } + } + fclose(file); + return is_anonhuge; +} + +static int test_mmap_thp(int madvise_buffer) +{ + int is_anonhuge; + + char *buffer = (char *)mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (buffer == MAP_FAILED) { + perror("mmap"); + return -1; + } + if (madvise_buffer) + madvise(buffer, BUF_SIZE, MADV_HUGEPAGE); + + // set memory to ensure it's allocated + memset(buffer, 0, BUF_SIZE); + is_anonhuge = check_smaps_for_huge(); + munmap(buffer, BUF_SIZE); + return is_anonhuge; +} + +/* Global policy is always, process is changed to NOHUGE (process becomes madvise) */ +static int test_global_always_process_nohuge(void) +{ + int is_anonhuge = 0, res = 0, status = 0; + pid_t pid; + + if (prctl(PR_SET_THP_POLICY, PR_THP_POLICY_DEFAULT_NOHUGE, NULL, NULL, NULL) != 0) { + perror("prctl failed to set policy to madvise"); + return -1; + } + + /* Make sure prctl changes are carried across fork */ + pid = fork(); + if (pid < 0) { + perror("fork"); + exit(EXIT_FAILURE); + } + + res = prctl(PR_GET_THP_POLICY, NULL, NULL, NULL, NULL); + if (res != PR_THP_POLICY_DEFAULT_NOHUGE) { + printf("prctl PR_GET_THP_POLICY returned %d pid %d\n", res, pid); + goto err_out; + } + + /* global = always, process = madvise, we shouldn't get HPs without madvise */ + is_anonhuge = test_mmap_thp(0); + if (is_anonhuge) { + printf( + "PR_THP_POLICY_DEFAULT_NOHUGE set but still got hugepages without MADV_HUGEPAGE\n"); + goto err_out; + } + + is_anonhuge = test_mmap_thp(1); + if (!is_anonhuge) { + printf( + "PR_THP_POLICY_DEFAULT_NOHUGE set but did't get hugepages with MADV_HUGEPAGE\n"); + goto err_out; + } + + /* Reset to system policy */ + if (prctl(PR_SET_THP_POLICY, PR_THP_POLICY_SYSTEM, NULL, NULL, NULL) != 0) { + perror("prctl failed to set policy to system"); + goto err_out; + } + + is_anonhuge = test_mmap_thp(0); + if (!is_anonhuge) { + printf("global policy is always but we still didn't get hugepages\n"); + goto err_out; + } + + is_anonhuge = test_mmap_thp(1); + if (!is_anonhuge) { + printf("global policy is always but we still didn't get hugepages\n"); + goto err_out; + } + + if (pid == 0) { + exit(EXIT_SUCCESS); + } else { + wait(&status); + if (WIFEXITED(status)) + return 0; + else + return -1; + } + +err_out: + if (pid == 0) + exit(EXIT_FAILURE); + else + return -1; +} + +int main(void) +{ + if (sysfs_check()) + return 0; + + if (system_thp_policy == SYSTEM_POLICY_ALWAYS) + return test_global_always_process_nohuge(); + +} -- 2.47.1