From patchwork Tue Jan 14 16:06:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13938947 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 7B6F4C02185 for ; Tue, 14 Jan 2025 16:07:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4437B280002; Tue, 14 Jan 2025 11:07:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F1F2280001; Tue, 14 Jan 2025 11:07:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E0C2280002; Tue, 14 Jan 2025 11:07:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 10A62280001 for ; Tue, 14 Jan 2025 11:07:14 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AE984160B31 for ; Tue, 14 Jan 2025 16:07:13 +0000 (UTC) X-FDA: 83006536746.18.5F2C469 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by imf25.hostedemail.com (Postfix) with ESMTP id BFE1EA0023 for ; Tue, 14 Jan 2025 16:07:11 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=MbCGZCKp; dkim=pass header.d=linutronix.de header.s=2020e header.b=DZpT+FHa; spf=pass (imf25.hostedemail.com: domain of t-8ch@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=t-8ch@linutronix.de; dmarc=pass (policy=none) header.from=linutronix.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736870832; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HHdz6D6xQVTO4bkE6hrOmhg63h4CtMukNog1b9dscsc=; b=soWA3BLcpvbuXz3a4BwtOYBxQffsvWM0zjqf/LyN8nt4hhZES7RKkz7UPZ5JZNULvfyZzK +e0SNbyyupOlduQbLKnn7iKFA9PDwIuW1Ba4EhkLaXCFketM4RbJUcJyPHir0zlDL296YS w1KCIU3Jv2xGmQaSOCbt5x90E01e5J0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736870832; a=rsa-sha256; cv=none; b=0KhqU6NwZeulDTzIqzxDKgmktvVREf39syVhz+pkQ67tALHMimEQdbIoudJTv7DFKtdq4A i2aL3tJnYhfayy96opZdjrmrNHFqNp44R7Hf247Xrg7iMLilArB2wsmi0JasK4QpAJ3w3k 9z9SgPAChG4o01LQnsyuChLCtzE4usk= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=MbCGZCKp; dkim=pass header.d=linutronix.de header.s=2020e header.b=DZpT+FHa; spf=pass (imf25.hostedemail.com: domain of t-8ch@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=t-8ch@linutronix.de; dmarc=pass (policy=none) header.from=linutronix.de From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1736870830; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HHdz6D6xQVTO4bkE6hrOmhg63h4CtMukNog1b9dscsc=; b=MbCGZCKplIQcKN5TErGlMPeR08toq1UH+O+tIlF++i2Erh1tWc+m0yFL5vxQwEjI91VGr5 nEXjaAP22h8s3GzjqTRUPGezJU2lYE8nRTK66/BKA9dJjJFS/tLlQaXq5qX7xzRBe/OsQ5 8ffkqjWJIubzs8nRGKQ8lSH5hzyOLmkgVNEHgUAzU0RRChCdX0y4kPtVwskHkkKV4p6vx+ pyr2paCgjw7yCD+VEME5M42BMvTfzpM8RRfapSav7dHT/SM5tuSAhXzG04Pm3NqWMiv1w7 bTd7k2sWsm0dMHrr1g+1b5ewaeel10i3eP6snHwA9z2jWZTJjpQ6YTGvrra4aA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1736870830; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HHdz6D6xQVTO4bkE6hrOmhg63h4CtMukNog1b9dscsc=; b=DZpT+FHaDSddwumeiTKVkMQotTZFJIFvJnQAW3V+YEbJDHdXS1YkTG6jZzapE/XjkvDueA P8QdLYx03nCk6zCg== Date: Tue, 14 Jan 2025 17:06:46 +0100 Subject: [PATCH v4 2/4] selftests/mm: virtual_address_range: Unmap chunks after validation MIME-Version: 1.0 Message-Id: <20250114-virtual_address_range-tests-v4-2-6fd7269934a5@linutronix.de> References: <20250114-virtual_address_range-tests-v4-0-6fd7269934a5@linutronix.de> In-Reply-To: <20250114-virtual_address_range-tests-v4-0-6fd7269934a5@linutronix.de> To: Andrew Morton , Shuah Khan , Dev Jain , Thomas Gleixner , David Hildenbrand , Anshuman Khandual Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1736870827; l=4005; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=82ZDLn8Ie616VeVZksZ+ubTOfL8+PSxtmk+JZNkNkyo=; b=oPv0Faclo57WmFtlCTOx25wxfWPHxrzmgr+oQuEWlAm5dMTgsvyf6eWeOshX+7AJRPEXs6tq8 xlW9TYQdyCzDR2m8rRreQCxCAWiDTX+sQh/R7IY2xopSEVQPFTGyUIC X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= X-Rspamd-Queue-Id: BFE1EA0023 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: yaj9aak9y4re3qy7fqxmgmtedtf55njd X-HE-Tag: 1736870831-491731 X-HE-Meta: U2FsdGVkX1/l1cWBD4rF66Ta7D2e8UDpg4q8vzQ7mQEsIRoIhCYuMcfPu4/GMmvAOVbZ8+Wsp+9zjlm15nEbTMF5+ylB99Cau74CpMWXBVOcYlIf9nB0Fw7J2wZDUI9OiB7k9Dq4Bxm87WQo0N2T98KznrXN7SM8KunX7E5izxtOqncog3dbpwY+sGQwprBc6Qavx4IFyDOqAxojqqM7QOjKOLBS/pwa9WMNU5QSUk3FchKx/QYR8llTjqqiCD957ZH1bmmK3PS1guuOtkHYEcoInEqhnjAKHjOExQxf7ZaSEHKvzbBRoqlykaVdKQjDtqeHS/r9DaSk18xJr7iKzL4rRcNl/gDgAAqWIvT45CrtftPPiaACUU00k09zGtOSk3N9Wk1rc75nqEnDIDg7B2ETVoaIFwlIkaTiyPXekVClYo5xspqfshZ6hcSnrxy2b2IQHl9mAxweNxeSEQ+KBBLsVPC4sX5r/Tt+5BHLEMiwvwKz5q9D0VXJn/GHR4UJkcUk6ZeW1vwpZ446eDP9rp0j5BXgyQcdGQFaifutRAT15IFqk7GMJcAHIfMKL1oxVmDGKBR+CLqAS6M8CnDswgbRMt24sekbX35yPCLlYUvPmtDenUUR3z81fXc/NsSR5CtzA2IB+G5eUuAT1ulRFs1TQi4HXwodQBL8Dz3NKd0G+q9m4T9xSElMe8HFAdrpBrmTSpK5kQlGrUh4g32iImEndWHEoUKhEXtZNF/VhppYAgSPFIcoOhYHLHq0K5fEREVQh7jA5dMMQHijEfo3IG3OFt49LdmzzXUC/nhbxC44fXbCaPplCIO/rf6LCRWSkNC3/xP3QIHxHG98eUZ692lQCaf8O+A4XQYP9OMVOnmAJ2nc63sEZKzA+2SE6DEwuJzIswn83+dA2OThYwS78wsnGUZFaMj0ccEoWIgbrCb0o8GbMF89TsEdxhvnv5gt6JCd5xx7PmHVH07eo02 fo5F8n/g I+blz5daK9WSBYCL1+sU4HhZxJCrZy+Cy159LIbPtSl9oBnO+HrsO5citd9KFp6fzE2dz2csw+9vyvFLitl2CTEDJosilz94rPHYYg1ViL4KgCLTnfZuWGzeK4aWkI1IBmommv4aStqVPYkOJ7wi0QKUMfVCJCBIKvFLPE9n1+7dKlFwflxFl3twM6Le0CRytcyCUWOGlLhX34q4Lp4sQiq/+hVFrvKUU1t02RxB0mFL5dzWbLDKjDOn0Bt8BLmdU23j+4zjDITeoOzSilL6R1YceRYA+2lHECOoyzl4qzbyrpxgY8wEJuVsnGOOvBxevhr8SsYZhf3ql+jTNUEUf5hIKi8VKw8jq+eXpzfRyoSRL8YJX1XEch8HLwjangG2maWCxmYz2oe4TJzABURB4Wwfkwwzem3bb4ykO1DatXpdE9524dmwk12wCQJC9PN5BftY1UubAN8cXtMdcE6LxHuMRC1OGM/kJ+58m 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: For each accessed chunk a PTE is created. More than 1GiB of PTEs is used in this way. Remove each PTE after validating a chunk to reduce peak memory usage. It is important to only unmap memory that previously mmap()ed, as unmapping other mappings like the stack, heap or executable mappings will crash the process. The mappings read from /proc/self/maps and the return values from mmap() don't allow a simple correlation due to merging and no guaranteed order. To correlate the pointers and mappings use prctl(PR_SET_VMA_ANON_NAME). While it introduces a test dependency, other alternatives would introduce runtime or development overhead. Fixes: 010409649885 ("selftests/mm: confirm VA exhaustion without reliance on correctness of mmap()") Signed-off-by: Thomas Weißschuh Acked-by: David Hildenbrand --- tools/testing/selftests/mm/config | 1 + tools/testing/selftests/mm/virtual_address_range.c | 33 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/mm/config b/tools/testing/selftests/mm/config index 4309916f629e36498efb07eb606b2f0c49ee6211..a28baa536332f3fcfb1b83759b5fbb432ae80178 100644 --- a/tools/testing/selftests/mm/config +++ b/tools/testing/selftests/mm/config @@ -7,3 +7,4 @@ CONFIG_TEST_HMM=m CONFIG_GUP_TEST=y CONFIG_TRANSPARENT_HUGEPAGE=y CONFIG_MEM_SOFT_DIRTY=y +CONFIG_ANON_VMA_NAME=y diff --git a/tools/testing/selftests/mm/virtual_address_range.c b/tools/testing/selftests/mm/virtual_address_range.c index ea6ccf49ef4c552f26317c2a40b09bca1a677f8f..386e4e46fa65b98af78dee4bb30144eb2b51f528 100644 --- a/tools/testing/selftests/mm/virtual_address_range.c +++ b/tools/testing/selftests/mm/virtual_address_range.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -82,6 +83,24 @@ static void validate_addr(char *ptr, int high_addr) ksft_exit_fail_msg("Bad address %lx\n", addr); } +static void mark_range(char *ptr, size_t size) +{ + if (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, size, "virtual_address_range") == -1) { + if (errno == EINVAL) { + /* Depends on CONFIG_ANON_VMA_NAME */ + ksft_test_result_skip("prctl(PR_SET_VMA_ANON_NAME) not supported\n"); + ksft_finished(); + } else { + ksft_exit_fail_perror("prctl(PR_SET_VMA_ANON_NAME) failed\n"); + } + } +} + +static int is_marked_vma(const char *vma_name) +{ + return vma_name && !strcmp(vma_name, "[anon:virtual_address_range]\n"); +} + static int validate_lower_address_hint(void) { char *ptr; @@ -116,12 +135,17 @@ static int validate_complete_va_space(void) prev_end_addr = 0; while (fgets(line, sizeof(line), file)) { + const char *vma_name = NULL; + int vma_name_start = 0; unsigned long hop; - if (sscanf(line, "%lx-%lx %s[rwxp-]", - &start_addr, &end_addr, prot) != 3) + if (sscanf(line, "%lx-%lx %4s %*s %*s %*s %n", + &start_addr, &end_addr, prot, &vma_name_start) != 3) ksft_exit_fail_msg("cannot parse /proc/self/maps\n"); + if (vma_name_start) + vma_name = line + vma_name_start; + /* end of userspace mappings; ignore vsyscall mapping */ if (start_addr & (1UL << 63)) return 0; @@ -149,6 +173,9 @@ static int validate_complete_va_space(void) return 1; lseek(fd, 0, SEEK_SET); + if (is_marked_vma(vma_name)) + munmap((char *)(start_addr + hop), MAP_CHUNK_SIZE); + hop += MAP_CHUNK_SIZE; } } @@ -175,6 +202,7 @@ int main(int argc, char *argv[]) break; } + mark_range(ptr[i], MAP_CHUNK_SIZE); validate_addr(ptr[i], 0); } lchunks = i; @@ -192,6 +220,7 @@ int main(int argc, char *argv[]) if (hptr[i] == MAP_FAILED) break; + mark_range(ptr[i], MAP_CHUNK_SIZE); validate_addr(hptr[i], 1); } hchunks = i;