From patchwork Tue Jan 14 16:06:45 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: 13938940 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 DE19918952C; Tue, 14 Jan 2025 16:07:11 +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=1736870833; cv=none; b=JNiX6MOufzOZi0GQmmqTr1v9U8E1ehW6rO4Ytx+KC0eIrkUy6p1jiJX5RG8rg6EDDF3d7BozZFBk9djj1Nszzz70evrx5LN7QxFQHNYvJ3SyypYSUeUyz22MejktYF9cDkwlVfKGK+2JYMM+b5W+fBBfL+0UL1rByTS/Wcxkmqs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736870833; c=relaxed/simple; bh=CZpuloUjytwkHL2aPWwv10o9ubxrDZ5i+6K2DZNoL+M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=X/hQLrEPq4fc2eSJBNk4Rz9mGXlvJZ2HWh88kdNl7NfODvdCVkNDQIJm/K/McQNclESSUP4VGq4C2hOpp9Gr+Is5imhLsmmx0g6ZtKHOOgZ3x7wq/G2uX6ArHO8+rvtIpnjgp6NLfhJmIoooUVLynqansMEAKGgJvPsIxL1NlIM= 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=qIV/Sqi/; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=TODIgtjD; 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="qIV/Sqi/"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TODIgtjD" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1736870829; 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=WMw4Pqpozqcj+M46rlKOQ3x4xAheM6IvB7ridCkcbVk=; b=qIV/Sqi/Yoks2I8Nqn0d21+mfb/g+17wevTd7eWnB9iPPJgxpq/hvMWR4Cbcrtv3Ad9LKt rpHmgGdMS3mWZSlTBuJIfppw2+TmA0G0hkrFDpuRY0u0rrgAyfhuqS5vNbkM0FfR3Q4Q5k 2OBXZeTyiCU/hzd++lBA+X2QpAVp2fhYJPjpXzNx3JHRKjjoDhZ9rs29ROVEVKzwtF6k+e Yanq0CIZvkDJMrtPKQs0voJpfqzZ3brLHAeaEsr+Wjb/Mef7dzU+WIjQZXrzuS2hR4+HcM WF8mgBXS9+7jYWCODWyklQAvZCyt/2ymxndgYqyMG/hUWGRmV1/US3rFsJqwEA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1736870829; 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=WMw4Pqpozqcj+M46rlKOQ3x4xAheM6IvB7ridCkcbVk=; b=TODIgtjDbHTyshHRlgReOZN8Eur6i1Ig6fNAmzvJVWuIRBv56dUMgA9GNPCFURWWrKYHmh 1tnnWGa9evG4r7Cg== Date: Tue, 14 Jan 2025 17:06:45 +0100 Subject: [PATCH v4 1/4] selftests/mm: virtual_address_range: mmap() without PROT_WRITE Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250114-virtual_address_range-tests-v4-1-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=1679; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=CZpuloUjytwkHL2aPWwv10o9ubxrDZ5i+6K2DZNoL+M=; b=GqgQ3xml2Bf4VP4ld+L2fBtKx7TbedcU6otFZT9oJT34c/htO60k0G8UfJ7KMpb3YK+QdSVhb 7XeerjJ2ST8BpBkEE9OUCb9tJz8Maa+jIEBAF1TCFkRLjAjlZP85Dy1 X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= When mapping a larger chunk than physical memory is available with PROT_WRITE and overcommit is disabled, the mapping will fail. This will prevent the test from running on systems with less then ~1GiB of memory and triggering an inscrutinable test failure. As the mappings are never written to anyways, the flag can be removed. Fixes: 4e5ce33ceb32 ("selftests/vm: add a test for virtual address range mapping") Signed-off-by: Thomas Weißschuh Acked-by: David Hildenbrand Acked-by: Dev Jain --- tools/testing/selftests/mm/virtual_address_range.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/mm/virtual_address_range.c b/tools/testing/selftests/mm/virtual_address_range.c index 2a2b69e91950a37999f606847c9c8328d79890c2..ea6ccf49ef4c552f26317c2a40b09bca1a677f8f 100644 --- a/tools/testing/selftests/mm/virtual_address_range.c +++ b/tools/testing/selftests/mm/virtual_address_range.c @@ -166,7 +166,7 @@ int main(int argc, char *argv[]) ksft_set_plan(1); for (i = 0; i < NR_CHUNKS_LOW; i++) { - ptr[i] = mmap(NULL, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE, + ptr[i] = mmap(NULL, MAP_CHUNK_SIZE, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (ptr[i] == MAP_FAILED) { @@ -186,7 +186,7 @@ int main(int argc, char *argv[]) for (i = 0; i < NR_CHUNKS_HIGH; i++) { hint = hint_addr(); - hptr[i] = mmap(hint, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE, + hptr[i] = mmap(hint, MAP_CHUNK_SIZE, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (hptr[i] == MAP_FAILED) 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: 13938941 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 3A847189B8F; Tue, 14 Jan 2025 16:07:11 +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=1736870834; cv=none; b=UMJ0QJHE0Guyyo2fAD5vgF6QVhaqKOkwuXtzBBTYOWQL04wsVZECRZlhfchUDe5I0OFgndG8/4rFBb3sKJXYLdBrEYya6qO36CQLbdJla0oom5cwCaPN+E4djZb1irQmYvRYWpIvweSpvJHPv8GGeQkEH7oTXvWzH1OWvbt3hPU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736870834; c=relaxed/simple; bh=82ZDLn8Ie616VeVZksZ+ubTOfL8+PSxtmk+JZNkNkyo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fatf3L9k5P0h+tMKxThpaA2v+emaBk8FnNWBBdfEFAiZvi7ljkzxooI522l6MXHuESxNkzF/FhFyEOaT9oJkl15UUzdCFF3OvOKZ6akxc2EStdHppeW2MgFheD87Cooa/7fRuLHe0P2+qTkTEPdVAfwXJdfsNVrYNa/juljvPBw= 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=MbCGZCKp; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=DZpT+FHa; 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="MbCGZCKp"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="DZpT+FHa" 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 Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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= 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; From patchwork Tue Jan 14 16:06:47 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: 13938943 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 A54FF15C15C; Tue, 14 Jan 2025 16:07:12 +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=1736870834; cv=none; b=jW4aqQcRztbWYLbzm27wpu1dgplswZY5B7Q8eHZh8EmA20QbzQmLm7T6rvBLXBZY1cCI5dTnVG1fak+xwraxgJ/FvF4oi51G2S/CFDmyJHMQtuDNeLyJPU26XZhD9aIFn2ag8bYaaojkM7JZJhuzeIjxY68Q5dNY65DLBsd1TiI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736870834; c=relaxed/simple; bh=gR157o96GOD5Fi5AXkbUAbIHuV8f9whd7ZV2AeOIVu8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TSsk6oTowIKFtPyuYq0IMIrnZKxhkxidtOSPBmnjiKc0LBK5yR4qMDefp0fmwbeztvTxk8UJrroOKfdSO9gDj6A2lfd1gnwIje8EVS8dGf6J0QBpH/QMt9MIZFvqWoJfNO376HyqJ9LiVIuANVLyRpQV5cltFhQ6AJH/q+iaZ20= 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=TYd+3SM/; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=TN14Vn5s; 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="TYd+3SM/"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TN14Vn5s" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1736870831; 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=jc6lPJWBXfz2WyOhO1yRIjUxMkwiFWRg7L1C1Eo7ZW8=; b=TYd+3SM/Zzv8AeGJDtkHtFw5Zc07ZYkaqucBnYDTJBEwsbwaWlXHvqvHWkDlu72h3Z6Wd1 TmTfIV0GaDhM/tD898JAabg9DEIa43vvV8rhJfw4ELBS27eASXQVxqVX7R5qvadW1uZ3SJ 7ClAvHdSkHaoJYIiNYtUJL8a2ifV/z52TGJyCJx+gFdqPZUIy20DhrWGVzN58UuoTMRhF6 aCS4pVMfvt1vT7yD1Y1F6+MtAEQmRktdAuc1vpPaerfqKk890qiemIJa7domNDepp1pdvU A8IafqZZmYsoTwklNjujlNr9NLKnGF8LOKGgvr9OZZJi+8rGx/R2VCR7vvxq3Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1736870831; 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=jc6lPJWBXfz2WyOhO1yRIjUxMkwiFWRg7L1C1Eo7ZW8=; b=TN14Vn5silJWC6WJq8XDyL63P8p11yF1750ISYCYPkpibeDxk2BH0U6KeJuuQECo7vLBzn x7BU9R0ToQjc6rAg== Date: Tue, 14 Jan 2025 17:06:47 +0100 Subject: [PATCH v4 3/4] selftests/mm: vm_util: Split up /proc/self/smaps parsing Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20250114-virtual_address_range-tests-v4-3-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=2915; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=gR157o96GOD5Fi5AXkbUAbIHuV8f9whd7ZV2AeOIVu8=; b=w4OIeFsyazqsWCb42afv7zGrsXsPmDhiUXiU0G/0S+jHi5uqcgpHGFHrnJlu+GKmwp+XkwCiX TMob5JaYSQdDteTjGBmt3vMUIt6mJRqhvM03Tv7+D6ZfV2KKG4mQuPX X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= Upcoming changes want to reuse the /proc/self/smaps parsing logic to parse the VmFlags field. As that works differently from the currently parsed HugePage counters, split up the logic so common functionality can be shared. While reworking this code, also use the correct sscanf placeholder for the "uint64_t thp" variable. Signed-off-by: Thomas Weißschuh Acked-by: David Hildenbrand --- tools/testing/selftests/mm/vm_util.c | 42 +++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c index d8d0cf04bb57fd22bd7748fffec6a23c3103e35c..a450ab353f8e710a6bfce347bc3a7309920c70f5 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -193,13 +194,11 @@ unsigned long rss_anon(void) return rss_anon; } -bool __check_huge(void *addr, char *pattern, int nr_hpages, - uint64_t hpage_size) +char *__get_smap_entry(void *addr, const char *pattern, char *buf, size_t len) { - uint64_t thp = -1; int ret; FILE *fp; - char buffer[MAX_LINE_LENGTH]; + char *entry = NULL; char addr_pattern[MAX_LINE_LENGTH]; ret = snprintf(addr_pattern, MAX_LINE_LENGTH, "%08lx-", @@ -211,23 +210,40 @@ bool __check_huge(void *addr, char *pattern, int nr_hpages, if (!fp) ksft_exit_fail_msg("%s: Failed to open file %s\n", __func__, SMAP_FILE_PATH); - if (!check_for_pattern(fp, addr_pattern, buffer, sizeof(buffer))) + if (!check_for_pattern(fp, addr_pattern, buf, len)) goto err_out; - /* - * Fetch the pattern in the same block and check the number of - * hugepages. - */ - if (!check_for_pattern(fp, pattern, buffer, sizeof(buffer))) + /* Fetch the pattern in the same block */ + if (!check_for_pattern(fp, pattern, buf, len)) goto err_out; - snprintf(addr_pattern, MAX_LINE_LENGTH, "%s%%9ld kB", pattern); + /* Trim trailing newline */ + entry = strchr(buf, '\n'); + if (entry) + *entry = '\0'; - if (sscanf(buffer, addr_pattern, &thp) != 1) - ksft_exit_fail_msg("Reading smap error\n"); + entry = buf + strlen(pattern); err_out: fclose(fp); + return entry; +} + +bool __check_huge(void *addr, char *pattern, int nr_hpages, + uint64_t hpage_size) +{ + char buffer[MAX_LINE_LENGTH]; + uint64_t thp = -1; + char *entry; + + entry = __get_smap_entry(addr, pattern, buffer, sizeof(buffer)); + if (!entry) + goto err_out; + + if (sscanf(entry, "%9" SCNu64 " kB", &thp) != 1) + ksft_exit_fail_msg("Reading smap error\n"); + +err_out: return thp == (nr_hpages * (hpage_size >> 10)); } From patchwork Tue Jan 14 16:06:48 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: 13938944 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 6F1601A8F8A; Tue, 14 Jan 2025 16:07: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=1736870835; cv=none; b=Ufj9lzUQ9rnFWmRhwqB/jP2IF5VizdiXr/hj7ZwQRN2XHcwzrU67DdRY2lhhg/ML+aGWZ3mmCfkJbFV8NvLIeznif1Xi9eWSUBhNY14hXVlP3Dr5BTJ1uQQugEDRjEys9gsKPUq1qJPk8B6OELC0a521hPTtZ2fiZW3OG5Rijfs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736870835; c=relaxed/simple; bh=dH3RzX1kWHmc070i/rqPW6ADGmajD2eJb8ZOtqkz4DI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CXbMNm4EoEUPHMaZgm5Wkg4biHnW10fd6o+c3qzxiVJ5FzADcErDVX13mTPdcFSkq3OOqTn82ovPDYhYCQxcX+AbNyQRw0fjAQ9EEiDiXx+EDdPK0GIaAZlXb/iEd4NcYpKrG/fi2w6AXuv29vfCr6K9LT00uNfhGhmfAbdNeHU= 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=cnFtEm18; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=KnaL0Bqp; 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="cnFtEm18"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="KnaL0Bqp" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1736870831; 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=8RpmujbkErM0AyBS/jsicWpVLkLAFbMaYP9rwEqJEd4=; b=cnFtEm18MbrSTZZNuEaTkbh1BCcJt6ODsBTtckfbbeBTuMwWvfgoDH3wreQrDdYd1I0hw8 97ExHGFWJLfiU6XcyeJtpAwoC0Qz1BIovPU4xCrc8zfK7umEq2FCW1b9xv3S92UJChPxeI tKtUWD1C7mvfBy86MAPmtENlpMqANSi8EfbAps+ZDGu9SEPH548AoatKNvljt7zOw90zTk oTEyluL2uULENRWrH4iVCaTTYUj05nGT4qftjRCQp0nC+/v0mveb4TPDoLF3HpAt3N8xVE QXHaHQqJ10QuHj+hBWYkwwD9Srq37GiphfOKHOOYwuSSW0V8RyGLEC17HbnREA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1736870831; 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=8RpmujbkErM0AyBS/jsicWpVLkLAFbMaYP9rwEqJEd4=; b=KnaL0Bqp9VgUi6gfGyMii2Jd+8VSJEQIwpet4OdI32Z/16BYWMCnsNmSsVtWJTtQZh+xOC Zg4dYFNssdyZ38Ag== Date: Tue, 14 Jan 2025 17:06:48 +0100 Subject: [PATCH v4 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: <20250114-virtual_address_range-tests-v4-4-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?= , kernel test robot X-Developer-Signature: v=1; a=ed25519-sha256; t=1736870827; l=4152; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=dH3RzX1kWHmc070i/rqPW6ADGmajD2eJb8ZOtqkz4DI=; b=xQ+14+XJAZIgmtLRSLuvQ0RJRXCT1ZR5pG5JrfZEm4tjhZLiKT2rLZ0+9AZVNI24R/g7D4PPN oQPBYszSdrkDyMlNo2pPeLn+tt9sG8qIS0glZuGpfXKUlxWf9D0u92u 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 Acked-by: David Hildenbrand --- I left out the comment about the requirement for check_vmflag_io() to be called with the start address of a mapping. It's the same for the check_huge_*() functions and there it's not documented either. Also there is only a single, correct user and any misuse will instantly result in visible breakage. --- tools/testing/selftests/mm/virtual_address_range.c | 4 ++++ tools/testing/selftests/mm/vm_util.c | 24 ++++++++++++++++++++++ tools/testing/selftests/mm/vm_util.h | 1 + 3 files changed, 29 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 a450ab353f8e710a6bfce347bc3a7309920c70f5..8bd2c5e59bc73bdfa617d4b11b448da84e2a3daf 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -400,3 +400,27 @@ unsigned long get_free_hugepages(void) fclose(f); return fhp; } + +bool check_vmflag_io(void *addr) +{ + char buffer[MAX_LINE_LENGTH]; + const char *flags; + size_t flaglen; + + flags = __get_smap_entry(addr, "VmFlags:", buffer, sizeof(buffer)); + if (!flags) + ksft_exit_fail_msg("%s: No VmFlags for %p\n", __func__, addr); + + while (true) { + flags += strspn(flags, " "); + + flaglen = strcspn(flags, " "); + if (!flaglen) + return false; + + if (flaglen == strlen("io") && !memcmp(flags, "io", flaglen)) + return true; + + flags += flaglen; + } +} 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