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: 857078 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 B86E51A28D; Mon, 13 Jan 2025 13:17:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736774235; cv=none; b=EAywfreqjMcjf/G6hBlgdJEOUfJVYtXYiTB2MBjXdRz41zUNEchuiFc8whd/P0sZi1e1zpOwu3C40eEh/5UqHeCRCfebKzyg09ogt2ponBxqpjJSa2sVHy0wkatYQAKSqsy8EOwBUUHYBZAqsU6SoPBTDbms4hGTKcPQlD4y5eI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736774235; c=relaxed/simple; bh=+J+pIjUkTrbA4CGXJ2S1U5YIbW5DfoH8SKBNfbwggwk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=M5/LK/DbgKl06X2rL5HaREY1eiq9YQA8o9RgpgZkpIp5AWG5hJXm7gQaCbtUQCPsmPxp4SZ02zSUTRTYraMS3h0hq5GUiPi5I9YAqSL1bmNK/07fSiewMtE4tpE6BsaNyGCQiIRSgjES01gkL/iHsbqf5VGe+fA6JsKcXMU8HZk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=1CBuV9LM; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=4mvRayE0; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="1CBuV9LM"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="4mvRayE0" 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 Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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= 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 --- 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; From patchwork Mon Jan 13 13:15:38 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: 857077 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 9A9BA24B28; Mon, 13 Jan 2025 13:17:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736774236; cv=none; b=QRBCsWntBbw5J85d52BzBPGlg9v6MvbjrJe76L7D2BPwY0Ojw7omPLjK/ktGIbZz498BNLeBiu3YovVI8At4jC+AwrY6+msf9P9+5n/Hx4lVrh6m3YJtw6K16bRi6DKJ8D753APCVlJtTexWNE/ijED2Y0BuTAfItW8zujetTF8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736774236; c=relaxed/simple; bh=4PwI9Ccn5M7eMxfUmG8BWAwz4GRkkm02QUveGYfCnxc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=vBrgbRluhDp77Eh1sJ/5UQs1OYgGIzrEFZGnQLeWjPJjhwqk53CEu2imBMGkbEWUBvTGloVyirrQvOaS0n1FzcvcZblEyoW4f3dwH7mFU6b83BV6vsHzskYr5cvE1V3LQ7aTg0+ITJRbw1FSt/rG5OCH33S8gPFZOp7E9c6iTx0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=tOd1+oYb; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=4WUy0BVW; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="tOd1+oYb"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="4WUy0BVW" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1736774233; 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=UqkR3Vf3Utpm6qqhuJmf9/1TcUxTIh5FO0zgNY2iBlQ=; b=tOd1+oYbaw93xI5qiPtbOLngwYfXVaCDgpIb0F1rOhjs9p2VWQsgJU8vJnllZX7z5BAL/S lNcAVwjI7RNiLWH69IWcGQGISkmh0cJeeFdJohCBn9Ev0akqMBoTZsEtKugqXkaIUQIMbA Y0JLN/xKfRZLpa+XIz6yt+0rv8gmuwgXoIDcv2bwgljVfOM5lxDsD35nCnCXzHdqay88Bw wsMrnqYQGPhArtr8ejOSDsRhii0cx8/jEyFDE06fZo8gIcL8oH8dZtffl41RDRAHPI8xbE RsP1TvFYfLiGegojESCE2ZOvE+xxuo8PhYvnxcvx/D61mjmfyrrYjPiCVD4Ayw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1736774233; 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=UqkR3Vf3Utpm6qqhuJmf9/1TcUxTIh5FO0zgNY2iBlQ=; b=4WUy0BVWvBSBOdixeQzZNV2YvPplN6D1GIvqp47n45grd+/sld7MFBsaL5fjWk6wiukVtK PJw7pr58sz4JilAQ== Date: Mon, 13 Jan 2025 14:15:38 +0100 Subject: [PATCH v3 4/4] selftests/mm: virtual_address_range: Avoid reading from VM_IO mappings Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250113-virtual_address_range-tests-v3-4-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?= , kernel test robot X-Developer-Signature: v=1; a=ed25519-sha256; t=1736774229; l=3934; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=4PwI9Ccn5M7eMxfUmG8BWAwz4GRkkm02QUveGYfCnxc=; b=xbBux7r8n6o18h59gp/9UfYQ+pHgD0rP3QBiHdmTwKMk9vdZ/SHtmSYtgzFGZcQ52GrBGiPKZ YYGMIiM1TpWA/PGh6exbFX6/gcaT+cXJ6QBX0kjNQWFo+32arGd47fF X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= The virtual_address_range selftest reads from the start of each mapping listed in /proc/self/maps. However not all mappings are valid to be arbitrarily accessed. For example the vvar data used for virtual clocks on x86 [vvar_vclock] can only be accessed if 1) the kernel configuration enables virtual clocks and 2) the hypervisor provided the data for it. Only the VDSO itself has the necessary information to know this. Since commit e93d2521b27f ("x86/vdso: Split virtual clock pages into dedicated mapping") the virtual clock data was split out into its own mapping, leading to EFAULT from read() during the validation. Check for the VM_IO flag as a proxy. It is present for the VVAR mappings and MMIO ranges can be dangerous to access arbitrarily. Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-lkp/202412271148.2656e485-lkp@intel.com Fixes: e93d2521b27f ("x86/vdso: Split virtual clock pages into dedicated mapping") Fixes: 010409649885 ("selftests/mm: confirm VA exhaustion without reliance on correctness of mmap()") Suggested-by: David Hildenbrand Link: https://lore.kernel.org/lkml/e97c2a5d-c815-4936-a767-ac42a3220a90@redhat.com/ Signed-off-by: Thomas Weißschuh Revert "selftests/mm: virtual_address_range: Avoid reading VVAR mappings" This reverts commit 05cc5d292ac4238684b59922aecf59c932edefa0. --- tools/testing/selftests/mm/virtual_address_range.c | 4 ++++ tools/testing/selftests/mm/vm_util.c | 21 +++++++++++++++++++++ tools/testing/selftests/mm/vm_util.h | 1 + 3 files changed, 26 insertions(+) diff --git a/tools/testing/selftests/mm/virtual_address_range.c b/tools/testing/selftests/mm/virtual_address_range.c index 386e4e46fa65b98af78dee4bb30144eb2b51f528..b380e102b22f0a44654ab046f257e8c35e8d90e9 100644 --- a/tools/testing/selftests/mm/virtual_address_range.c +++ b/tools/testing/selftests/mm/virtual_address_range.c @@ -15,6 +15,7 @@ #include #include +#include "vm_util.h" #include "../kselftest.h" /* @@ -159,6 +160,9 @@ static int validate_complete_va_space(void) if (prot[0] != 'r') continue; + if (check_vmflag_io((void *)start_addr)) + continue; + /* * Confirm whether MAP_CHUNK_SIZE chunk can be found or not. * If write succeeds, no need to check MAP_CHUNK_SIZE - 1 diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c index 8468a19d6acca10c7e9228c03a935cdeb2402b5d..161fe03b07af78244efc669a36155ad603fa6f7d 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -400,3 +400,24 @@ unsigned long get_free_hugepages(void) fclose(f); return fhp; } + +bool check_vmflag_io(void *addr) +{ + char *saveptr, *flag, *strtok_arg; + char buffer[MAX_LINE_LENGTH]; + + strtok_arg = __get_smap_entry(addr, "VmFlags:", buffer, sizeof(buffer)); + if (!strtok_arg) + ksft_exit_fail_msg("%s: No VmFlags for %p\n", __func__, addr); + + while (true) { + flag = strtok_r(strtok_arg, " ", &saveptr); + if (!flag) + break; + if (strcmp(flag, "io") == 0) + return true; + strtok_arg = NULL; + } + + return false; +} diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h index 2eaed82099255e09ffd38ad9714994397f304685..b60ac68a9dc8893895f49946b258260f7a82218a 100644 --- a/tools/testing/selftests/mm/vm_util.h +++ b/tools/testing/selftests/mm/vm_util.h @@ -53,6 +53,7 @@ int uffd_unregister(int uffd, void *addr, uint64_t len); int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len, bool miss, bool wp, bool minor, uint64_t *ioctls); unsigned long get_free_hugepages(void); +bool check_vmflag_io(void *addr); /* * On ppc64 this will only work with radix 2M hugepage size