From patchwork Mon Jan 13 13:15:36 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: 13937334 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 88B17C02180 for ; Mon, 13 Jan 2025 13:17:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED5566B009F; Mon, 13 Jan 2025 08:17:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E59846B00A2; Mon, 13 Jan 2025 08:17:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BBF6F6B00A1; Mon, 13 Jan 2025 08:17:15 -0500 (EST) 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 7E6BA6B00A0 for ; Mon, 13 Jan 2025 08:17:15 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 2DD9D120265 for ; Mon, 13 Jan 2025 13:17:15 +0000 (UTC) X-FDA: 83002479630.24.385AD9F Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by imf25.hostedemail.com (Postfix) with ESMTP id 484E2A000F for ; Mon, 13 Jan 2025 13:17:13 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=1CBuV9LM; dkim=pass header.d=linutronix.de header.s=2020e header.b=4mvRayE0; dmarc=pass (policy=none) header.from=linutronix.de; 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736774233; a=rsa-sha256; cv=none; b=uqTun2l8GJ5gI6F1Dg0zUghgm2Q/oUnuObHb1QSes5HIMWW6d97BBd+3oGP5f7zwv9kZqn +z67uLFw1F2aKiUaMU2ia2ByHd9b3W8yvtxnVxP9Iuv5EnreIRNDJPRmZDlNWMY3m1k+Ru kU9arkfzEqqQ05cUVq4pTHIa/6i9eLA= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=1CBuV9LM; dkim=pass header.d=linutronix.de header.s=2020e header.b=4mvRayE0; dmarc=pass (policy=none) header.from=linutronix.de; 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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736774233; 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=BB2O49jg7bdHh5I8SFkvOSGpR/2gpeyIP8hG/oWKnuA=; b=nhA0qwBYcXFK52kmYDN2471BmKuM4sDgwvslC3sdjxakiuiKYEhWyfBAsd8KfuePuAEgHj Jv2wuhML0/pb0CcUBTqLX9+o094Bm4APtkkP+hwApsJw0waLv2BxuYra4sWf9ShkdJ06a+ gPusrnUlP50bGoQko7eyOT+IjccpENs= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1736774232; 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=BB2O49jg7bdHh5I8SFkvOSGpR/2gpeyIP8hG/oWKnuA=; b=1CBuV9LMXAMrlJ9IflbNljk0JXuo2zd1MPzCAD9297JXLmLapQgdndJiXwT9Vw1dv1uKvA 3kICr8J0Qe1Tq4VBT8i+HBZMz24xJjspAfUAPBBG8Hlg6ueJV0YyCI5RFnaBAG1OgXxsKq YapIM26nzRMoFgxDh73szQ+M2S2EEJ9gRWRIypMbuhzp1HP1p9qlmWMp6i2uZcRUHN+ABj exBQpNpNN6nUzy3zt3I1iNRjVKI3GKVsYRj43hvJtSVDXSxH5aF/yxKFFF0pU/tduSeWpL jAqd4m77yFE9yk/NJfqGzFRd/38BDctkg8gkUVC/8WmJLzSILbZAxQcBcL6GVA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1736774232; 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=BB2O49jg7bdHh5I8SFkvOSGpR/2gpeyIP8hG/oWKnuA=; b=4mvRayE0CAw8tX3kK9BfIK84n94lkVUmzadM8HdJlJ0M9Ts30Nn0HAT/7T/QT7TYsJ4nq9 3oA4maHReyzVsBAA== Date: Mon, 13 Jan 2025 14:15:36 +0100 Subject: [PATCH v3 2/4] selftests/mm: virtual_address_range: Unmap chunks after validation MIME-Version: 1.0 Message-Id: <20250113-virtual_address_range-tests-v3-2-f4a8e6b7feed@linutronix.de> References: <20250113-virtual_address_range-tests-v3-0-f4a8e6b7feed@linutronix.de> In-Reply-To: <20250113-virtual_address_range-tests-v3-0-f4a8e6b7feed@linutronix.de> To: Andrew Morton , Shuah Khan , Dev Jain , Thomas Gleixner , David Hildenbrand 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=1736774229; l=3957; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=+J+pIjUkTrbA4CGXJ2S1U5YIbW5DfoH8SKBNfbwggwk=; b=uq6vmORxXVZbLi6UjqFMoF/YkYKRm8KQxCroQXGFlQap/Mqol+by324sYz153F6iNnwjZ6KOL VMaXve+XTJRDEdc0V6gkAQOwLRX6E/dWtAUFFJK3OOzNeRGN9Dg8+3q X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 484E2A000F X-Stat-Signature: 3eimobbstbm6t4jq8cjix8iomgonyz58 X-Rspam-User: X-HE-Tag: 1736774233-941500 X-HE-Meta: U2FsdGVkX184kR+3mwh7Pne7aiuQ/yloUOXgYfzra37/9KBYibNH2EYfzej7PBX+W1ek/3ygCgdkkfgdGpdnihtJm5PyIfFOZR/h7Mzc4bl2uRQxfOu6o4pH20CGJIqzkhoXcFa/gTRWnFjVyul8swz8re+ANBKvnzeIlYf0utbfctQnJdenIn8f7EnY2kHBqD79ZuK7E9IKbxhsBS5OEpuiaLYQ9dKxijmNmUaGNeIDsKIyzIJaVa6qHkaFdPYXkd3v9pXafVD1JwEn7jFaVVLLZzZ+2QpcuIpyzTca0QW4fmsm2NcYFlQ7KxhLeDHnkUs9cty11n9n//qUuEliqwIgUwQCyF+Pb0U6vPAXk8H6OjOnFtNTMpbkecqXVWJpHgR17x3Y5EZctKcceHEMdyMvbeCj4Appy2rs+qJNS24kCpCAAbpS00Cmu8EidesCnlysxS5ch/TBw7+mR46bDZrK9H6AE3x60r8/A7Zglj8N2731aEUGEp4ANx9taFYTTC/yZafhJnhPnMhJU0xLUzxfXR8+aZNAEX0JrlD+qLbTQgrbvlDoN9UW5TOYyOd60k4+eG+xm8sJ9jm5rJvtZFpdsRjiiBlx9+05wAXkSfTfL9d1X2lolXjG8Z+xLR+IwCEILQryZgqQRGf+At0vG8NPuoi4HSt/RPNzvKsD5NfMMMiri8hvLyVpH1Y6/m/TqMwepnda9349CwvhEWwBSVBgc5Xl+3M0uEOEPmVRp/BC9HHMyBprDGf8YZCuxgmKiE0St4/Zqh2coDpCIihE3c/gQel/A06WvGt4tavDqUKeub31barFghZBW+V/18ghjH3v2oG8nE7jUvCxpKyxyLA3MdEmomgyVN9b6UNceHUaIqHMMd9XTkJQvuJaz5Hh8SeUEUZ2qc4LEHW436/Cq9SFKZfXqNLRRGvobyc7QbG0uHUSeBJjbrFtjdaR+L/GxXkhHs2yqy5ocrUbho2 O8X5tczo pAqnEEe5Edt+AvyS3nnz1sMEEz0dnJAWnYarpj/1iwcKeMsYtceR9CZMYHJz3dcHkL5+OE/qu3SBqzreR3WY4RNgqAxfZz4bRLbjaPtYyZ+12DULHWMbE/9WTmobp30d52Az2xMMgcyQgT6DmudBnNjnqfgHUI8xyepryxujY7d/20WHadzVx+iuwC2Dz0xe8rZBApb32N58fu3npeTH2hpNzKILyhMiWLbhJAWrmcAWXa+gPLOBpg8c5kVUF47UuE4jC5XnE2gWhWkV22a94rjmoUL1WcoYgN8936al0EftUQ3CXUZ9QzSojU+vzuy4HAzqTPtkKZnbNMKXCan/IP1jPMlMXxKIJG964Xlucl/2KE9KPlVAXZBV4lHJkuJx5GYJC+ds3VNkfddqqvgfGEBueUdTJoh+l5o26M3a6BaiulNvBLyzkooZhDeI86KWsTxtJ 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;