From patchwork Mon Mar 25 04:26:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13601172 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 C2093CD1280 for ; Mon, 25 Mar 2024 04:29:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0CE716B0082; Mon, 25 Mar 2024 00:29:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 07DCB6B0083; Mon, 25 Mar 2024 00:29:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E86F36B0085; Mon, 25 Mar 2024 00:29:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D63576B0082 for ; Mon, 25 Mar 2024 00:29:00 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 5B4CC1A081C for ; Mon, 25 Mar 2024 04:29:00 +0000 (UTC) X-FDA: 81934281240.05.63B21DB Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf10.hostedemail.com (Postfix) with ESMTP id 910FEC0003 for ; Mon, 25 Mar 2024 04:28:58 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf10.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=1711340939; 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=xKqg2PEznvMJQ5FJBi4PLd46DzqDe3upmq+NRGzGAlk=; b=FNNB/HI+CE7m0+NWx7c9BWJjZtZoyozDHXa7d1H/06I9gIX/PtIfhxkOoRF1ojixhdai4d hg07XD6UkIkfTlVMIiMnnvT6kigy74s5S46oO5uAr5DczcugAhq2PJAVJ10Bi/JVUdm2Yb XpdZTG5ipZGlriarkAGo26bqISZYvnY= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf10.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=1711340939; a=rsa-sha256; cv=none; b=Ad2KtEqZ+la/eCio190MDfsQjYa4+ldHGuBY01ovRD8nYwourZmLC++M+//4XX1QUzqn02 O1zy3ttwnU35fi7C9yYSGRfqoxc/2iYaAEcQYE16ZhcpENQLQNWDR18qQ4SNQydFQCLtCe zUNZwIyaZAjwah9oYXPy6DpzLdLkXds= 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 40CCF1FB; Sun, 24 Mar 2024 21:29:25 -0700 (PDT) Received: from e116581.blr.arm.com (e116581.arm.com [10.162.41.8]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id A6BD83F67D; Sun, 24 Mar 2024 21:28:48 -0700 (PDT) From: Dev Jain To: akpm@linux-foundation.org Cc: shuah@kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Anshuman.Khandual@arm.com, Dev Jain Subject: [PATCH v2] selftests/mm: Confirm VA exhaustion without reliance on correctness of mmap() Date: Mon, 25 Mar 2024 09:56:53 +0530 Message-Id: <20240325042653.867055-1-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 910FEC0003 X-Stat-Signature: c7aarqg69u1hhb3qjk367fcur85fphod X-Rspam-User: X-HE-Tag: 1711340938-347883 X-HE-Meta: U2FsdGVkX18PrVkiYvspTOf+DaCPYukTLE1szVzuK+G4XFlaW9vE4Y4Cl17t+EnWcBtyyKTaDPkaG2iAdYms+PVMyV6a5lBExMlVb0srd0FJJrey4xWqIR9bYR6nJ739snlMPnP/IZXsf4GyuHITC+Hm3Y00XW/+57ajkdZgenvFjbK1BkZNqEgQYi17oC5DzqExX4HPJRUaHC0eTIvd2qHdhdF7NL0ZE7RmN7X+b+8yDB5PfsTMAfESDETTNP83OwAE/qlJkJyZEYd0NNnTzoCKY98UNyQ9Pk7CG+Wwi1i001LHTD5OpJc1y0Mlnowc1ykHGwbyGWgN8O+MWGcXxjDKjRpxMhehDhB1w+aX6cPxLbi+RkPic0thoJt7wtONcbu74323kjm2/RFiwzYGwyzkqhhglhw+9ouWzw5HEMrjZ8GRd0/PtgwvgcVj/WXLz4J8fqMdp0sFEok9r5J1tJy4JOMjEosN+Uua9ueZIEwhs1JwK9KwUFagTBiUCUjWk0Qfe3znOdH7xahdCB9Oqh4taUqKvnkJT76iYMv0Y1vzSSsNpax39ni6X0l9vI/YG9dI+Q216CvOiPb5yieC7xLpd1bWVfrlDOuzXsHyf92PGEeXOq56FjlrM3QEYabrXmyy3yiuQK14PThiMYn7dW25GveJzY+EsIaOCkFznOegtDrtXvQvX1EHM9KkLuz2cFBS15lUYZPIp6oRoQxSTNPwI6Fmj71w11mR2f0x/0Am1/93yklel6vMqOUFcx8eUHRn2AhxjGf3QmfSkWOiwiAdL7zuf/YM/t3g1OI383og1DOcxCZcyg3TGIAVVKAWN74V1v6SQ1aoWj2nyxkKKFt6uJXARSypjuYH4vON2R5zJwMGjNx+m4gHoRw62kW0FM/EqruAAVwZL1WDtDQ2SLKfGNEbIefqfIEAlsgG60lsiSAknpRx+A94atVz44kx9h2hVBfhQY4R8vP1kuO 721OYQm2 BQ+oYQGp7Y+oop/lP4Z+9zdMff/0i4Q4KTOyKopFAj5cWYwJegSmh2gZ4Y05JgV9tAKOqzqALX5qAs0XfxJB8M2aT1yZjUxLSdRu3KBZtxziBGdHfyYDeeAU1UH8dRC+lb9nBq4GP2hbleanQCrXqZVHuic/geGjXrptxiWi5+esL7CzaasHlZHfw3PVANF5yQt3YvGmtO4t2XW2wbUEPnu3o7g== 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. Changes in v2: - Replace SZ_1GB with MAP_CHUNK_SIZE, tidy-up 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 | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tools/testing/selftests/mm/virtual_address_range.c b/tools/testing/selftests/mm/virtual_address_range.c index 7bcf8d48256a..050e997e3be2 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,66 @@ 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; + + if (sscanf(line, "%lx-%lx %s[rwxp-]", + &start_addr, &end_addr, prot) != 3) + ksft_exit_fail_msg("cannot parse /proc/self/maps\n"); + + /* end of userspace mappings; ignore vsyscall mapping */ + if (start_addr & (1UL << 63)) + return 0; + + /* /proc/self/maps must have gaps less than MAP_CHUNK_SIZE */ + if (start_addr - prev_end_addr >= MAP_CHUNK_SIZE) + 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; + lseek(fd, 0, SEEK_SET); + + hop += MAP_CHUNK_SIZE; + } + } + return 0; +} + int main(int argc, char *argv[]) { char *ptr[NR_CHUNKS_LOW]; @@ -135,6 +197,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);