From patchwork Thu Mar 21 10:35:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13598624 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 A764AC54E68 for ; Thu, 21 Mar 2024 10:36:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3537B6B007B; Thu, 21 Mar 2024 06:36:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 303696B0082; Thu, 21 Mar 2024 06:36:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F3076B0083; Thu, 21 Mar 2024 06:36:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 0E7716B007B for ; Thu, 21 Mar 2024 06:36:49 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A2BAC41265 for ; Thu, 21 Mar 2024 10:36:48 +0000 (UTC) X-FDA: 81920692896.03.A38B610 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf11.hostedemail.com (Postfix) with ESMTP id 0CF6C40024 for ; Thu, 21 Mar 2024 10:36:46 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf11.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711017407; 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:references; bh=fiMmp83rzLipfB2+9hE9CUGXe0K6ntWNfTM8zQztwWY=; b=RfjfNXX0m/RM5jKis2payr+tmyhtR2ewaLExv5aYIjE0EKB6OIwznV1KHuQdKSPiwFOTjW 05XTVikYzHguH15OxCu21m+6kpobWbu5luDgKWnbO3IwSXIzIPBw4oOsmJTqthDslcEUVF 1cvqiCzDG7l0Fy1IQqg2vrFex3p3tvk= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf11.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711017407; a=rsa-sha256; cv=none; b=w6Ajp9kCGFx3nrRAps8jg4FDYHTVP89w+NqdqBbeXEHpi5N47ACSWLkG4gtr9TWcSLxcR3 40c4/C5LlJ5jzXxoMDziZ3iYLPeT6UPWzqiEzUyVeQ0bKYINhQz3gkKvtNz/fDohkTaFKd qsMW49zsfvsjY4MU7WWRpFdzo1m9i7Y= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 5D3E81007; Thu, 21 Mar 2024 03:37:17 -0700 (PDT) Received: from e116581.blr.arm.com (e116581.arm.com [10.162.41.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 6F6A43F67D; Thu, 21 Mar 2024 03:36:40 -0700 (PDT) From: Dev Jain To: akpm@linux-foundation.org, shuah@kernel.org Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Anshuman.Khandual@arm.com, Dev Jain Subject: [PATCH] selftests/mm: Confirm VA exhaustion without reliance on correctness of mmap() Date: Thu, 21 Mar 2024 16:05:22 +0530 Message-Id: <20240321103522.516097-1-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 0CF6C40024 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: q5yymtzittcgcj8hxwgrhkcw18go8k1p X-HE-Tag: 1711017406-6185 X-HE-Meta: U2FsdGVkX1+KiphvWwBLpTXLx9/cSkmLBRNLRlLkRqzDIAQKYoEV/39wuxFHTM0ODASrCMH3pPPe8cIlDNboV5vR71hYPRbdqY11pKgloato4U76t+BEpCRW6829BX3zYpEiQByv8oKlEuDV4ScwjWnLTvmRWvu0CkTc/O2J2jByxeITH8/b1unG/i3u9koY7Vp8H1n71o60VvCAK52OS3X3+BgWsbus3oObj2+HVXf0NVkh1n817LYsW4IXvAwXGsqv2QWpQrZF5dnwOfxWrKh3ri0uCjnpQAwaMvem4rSiP8SPoG4UfkJ2kakOqQzSZ5d2hc+8vcq9S3LjtqBjX2SI+ShavyIl5AlbT4Ts4XU3Kfh0PQl3d3e0w4YUaG8KCBs0scTSTvi4YrmKc6zh36ue+EUoKkrHVwXG6oGXdGp2fFlEDoU5q3ZL+ZhJGY6m+0GlLNL/1nBk4FKkhEmCypgsvPfQr9b+j5Qtw8nwe5xl2/WDQ/BYbXYXj8nPR/yUjE9h+OegRYCqyRxWxMhsryxBbJ2sXJF11mqi8nzsVda5Q/K+K2GHLUquCtjnNb/tByTHIj0Z85Dtz8KT/mKDRyeOqY81ld8C3VBgSaAz1Lg4rI/HkWnustu8dh5iMTqWm4+2QFlNgxyyybeqzUYyUpWI/2xjffWSxfnzXJN6lpgzSjzVc0NSThoYEGaMxyzPXaEpbLSILkMLK7xMipPcLK5N4yhbCgLInpnAuxFDokZ+2DWJaQGVIALnEGrQulhHXtiEVM1VSm5MGYhi2yNYcc50gBN70yb4nXn16PmmliUAp5OaDgGLl51LmzHYELuIaXTRPJiwY6WhclgS9Fo70MN8oUa4F2QiPtO9vNuW5mKC5hTKHyyFGhNaAccW1/plpdKZ35/7/8TcFrIcbWNQTrLrcmU51CBj/S2F1kHYSb3t3mbM85CI1w4mWiDYqeJeuiX73XO1QzfLZ8AMmS2 BCyhg6xr 1HqHu5+Q+2nGg8Ptf5qMfTMwGL1mERF5SoZQfy/bB8iZnHw+88Y/qWLtOI92hFaZFYkZuh3QGWoPuR+FDHQUxGa2/TdYn//6E2pQ6sLFOxb0f3PD6OGulMWEhdwFt3Jhb83W8VPw5cSFbowQwcvMbrYPiW5W83hqOopjjF9OF9fE4b4UB5sRDnTvnQrAOouHC/nXi4bMuR/yg2GPnvzU/4uH+7Q== 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: Currently, VA exhaustion is being checked by passing a hint to mmap() and expecting it to fail. This patch makes a stricter test by successful write() calls from /proc/self/maps to a dump file, confirming that a free chunk is indeed not available. Signed-off-by: Dev Jain --- Merge dependency: https://lore.kernel.org/all/20240314122250.68534-1-dev.jain@arm.com/ .../selftests/mm/virtual_address_range.c | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tools/testing/selftests/mm/virtual_address_range.c b/tools/testing/selftests/mm/virtual_address_range.c index 7bcf8d48256a..31063613dfd9 100644 --- a/tools/testing/selftests/mm/virtual_address_range.c +++ b/tools/testing/selftests/mm/virtual_address_range.c @@ -12,6 +12,8 @@ #include #include #include +#include + #include "../kselftest.h" /* @@ -93,6 +95,69 @@ static int validate_lower_address_hint(void) return 1; } +static int validate_complete_va_space(void) +{ + unsigned long start_addr, end_addr, prev_end_addr; + char line[400]; + char prot[6]; + FILE *file; + int fd; + + fd = open("va_dump", O_CREAT | O_WRONLY, 0600); + unlink("va_dump"); + if (fd < 0) { + ksft_test_result_skip("cannot create or open dump file\n"); + ksft_finished(); + } + + file = fopen("/proc/self/maps", "r"); + if (file == NULL) + ksft_exit_fail_msg("cannot open /proc/self/maps\n"); + + prev_end_addr = 0; + while (fgets(line, sizeof(line), file)) { + unsigned long hop; + int ret; + + ret = sscanf(line, "%lx-%lx %s[rwxp-]", + &start_addr, &end_addr, prot); + if (ret != 3) + ksft_exit_fail_msg("sscanf failed, cannot parse\n"); + + /* end of userspace mappings; ignore vsyscall mapping */ + if (start_addr & (1UL << 63)) + return 0; + + /* /proc/self/maps must have gaps less than 1GB only */ + if (start_addr - prev_end_addr >= SZ_1GB) + return 1; + + prev_end_addr = end_addr; + + if (prot[0] != 'r') + continue; + + /* + * Confirm whether MAP_CHUNK_SIZE chunk can be found or not. + * If write succeeds, no need to check MAP_CHUNK_SIZE - 1 + * addresses after that. If the address was not held by this + * process, write would fail with errno set to EFAULT. + * Anyways, if write returns anything apart from 1, exit the + * program since that would mean a bug in /proc/self/maps. + */ + hop = 0; + while (start_addr + hop < end_addr) { + if (write(fd, (void *)(start_addr + hop), 1) != 1) + return 1; + else + lseek(fd, 0, SEEK_SET); + + hop += MAP_CHUNK_SIZE; + } + } + return 0; +} + int main(int argc, char *argv[]) { char *ptr[NR_CHUNKS_LOW]; @@ -135,6 +200,10 @@ int main(int argc, char *argv[]) validate_addr(hptr[i], 1); } hchunks = i; + if (validate_complete_va_space()) { + ksft_test_result_fail("BUG in mmap() or /proc/self/maps\n"); + ksft_finished(); + } for (i = 0; i < lchunks; i++) munmap(ptr[i], MAP_CHUNK_SIZE);