Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2079417lqt; Mon, 22 Apr 2024 00:09:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXAMB/bka+wRYHulfCuO9Dka76jKwfeEj0jmtzBFwu7V0rKSJ2pROXXmyobcxaH2mvJ+V6oRrxtemdc055v5aYdr6bXJhFNgf/yOGblFQ== X-Google-Smtp-Source: AGHT+IFTAdSrzaPrxX+xSC1YWmdX7KcbiIdr+Hciu6JJKZuCeIrxnSGXVfNC8lS5IEDsk5d6oaaO X-Received: by 2002:a17:907:7711:b0:a52:54be:de25 with SMTP id kw17-20020a170907771100b00a5254bede25mr8419689ejc.28.1713769744162; Mon, 22 Apr 2024 00:09:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713769744; cv=pass; d=google.com; s=arc-20160816; b=PWBbDWcrDkFLYk56/7MR19u9krJewH26bwXRU/RULNvk1eteIK8045qUY3io73SNyo FlGOwSM66wM8rCDcnbmRZYmudozV/MunfUL1Zk8TFyewXxxBjrhepj8/8+m83eEwtdYc m16cwypLCoQbIdtJN9lvo8IwdHlmX5niQy2cJnjdBIR8Mxc/fUC7pRA7s0C8Fd8jslav a+A0VBWfzpJ3AR8ttVdocyvrlD9G6bBtx8XGABp4XNKasG03HQjGi8EOBzB1g6cIeehE 1rZYtrjIAzOayRvoWgNKHJ3ejqhXBItHofzaQRw2cfaR9Tn7LPXdPgGSYS8+u5YQjebO hQWQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=hgSRc65hSixYcm17dQhj7KCzoSehC891HIzT+BYJvT0=; fh=G7URjldd+zhTeoeltx+vTEHlC/QJJYvkZ4fEkU1pyGg=; b=cLm1ol4FQztfjEPjxJSK3OQeGwyprsCAw6VdqqcTXGpK5iG6KQVH1bPftq6J1NCK4a DAfxp1B/OTYNXxFIXcS2dvBQgd/tgw/h8lNE666AbZ8HxGOXytDTizGjSwFZhnfCcZDz 8Q6r7qnvfuO0KYCMVu+vHlrTrcr8bMsm8994Eu3G+1XrvUUmu2z/nVFNalaI2RsRc6+9 8g5BdDHH1bUSJkWbZlFT/RD8zFF1ul1GIsTYr4GE2jX3vTgO/sAAous0V05OqJ6tTeck +3l5Vb2fRKpEXI/gS9razb8REuAvKvu0mvWhFcga9KJS93owhDAS4ud9G1BzvW+r8hgO W8uA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-152826-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152826-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 25-20020a170906301900b00a55afc07373si1470421ejz.700.2024.04.22.00.09.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 00:09:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-152826-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-152826-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152826-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id E09081F21DDF for ; Mon, 22 Apr 2024 07:09:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2B2CE4AEE0; Mon, 22 Apr 2024 07:08:53 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 24CB4482D7; Mon, 22 Apr 2024 07:08:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713769732; cv=none; b=MT0hyJSgdaI4+Ljn6AP35sZ3AmwKDDzyHQVapX7pocNeohilxFwcYy/K5MAGRUsuWZUeIQsnKTq5uGjAYtqJdg7cWPJdMTZIw5j/NNIS9nIktEjvfKb12dBrR+IBCBkeH7GQVewSWPedqJQkjq2ag2Q3/jmCYNEkFF47YbU3DCs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713769732; c=relaxed/simple; bh=ypJhMt8Uo2wr/UL7YmAzqCdpKHS3vYTHgdA1ZbWhI9g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oKTgHlcbP+gkU5b1B2/Ww3VF+aPQ73EwMJNrojIG7MAkcd9iaC9crbplQ3G4Ag+AjrpII9ftsSBUrMYkQseq0GoCutdjrKm2DCrTX1EezsZTFAQyGuADAsy5tQRJkDsjrR3p6k9g5NZ5pbRFmPqi4wgv8FUaCkbimt3MeEHJU+w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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 966B31063; Mon, 22 Apr 2024 00:09:18 -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 CC65C3F7BD; Mon, 22 Apr 2024 00:08:45 -0700 (PDT) From: Dev Jain To: shuah@kernel.org, linux-arm-kernel@lists.infradead.org Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Anshuman.Khandual@arm.com, suzuki.poulose@arm.com, ryan.roberts@arm.com, rob.herring@arm.com, Catalin.Marinas@arm.com, broonie@kernel.org, will@kernel.org, mark.rutland@arm.com, linux@armlinux.org.uk, Dev Jain Subject: [PATCH v2 1/4] selftests/arm: Add mm test Date: Mon, 22 Apr 2024 12:37:14 +0530 Message-Id: <20240422070717.2194201-2-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240422070717.2194201-1-dev.jain@arm.com> References: <20240422070717.2194201-1-dev.jain@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This patch tests the 4GB VA restriction for 32-bit processes; it is required to test the compat layer, whether the kernel knows that it is running a 32-bit process or not. Chunks are allocated until the VA gets exhausted; mmap must fail beyond 4GB. This is asserted against the VA mappings found in /proc/self/maps. Signed-off-by: Dev Jain --- tools/testing/selftests/arm/mm/compat_va.c | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tools/testing/selftests/arm/mm/compat_va.c diff --git a/tools/testing/selftests/arm/mm/compat_va.c b/tools/testing/selftests/arm/mm/compat_va.c new file mode 100644 index 000000000000..03f662fad79f --- /dev/null +++ b/tools/testing/selftests/arm/mm/compat_va.c @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 ARM Limited + * + * Author : Dev Jain + * + * Tests 4GB VA restriction for 32 bit process + */ + +#define _GNU_SOURCE +#include +#include +#include +#include + +#include +#include + +#define MAP_CHUNK_SIZE SZ_1M +#define NR_CHUNKS_4G (SZ_1G / MAP_CHUNK_SIZE) * 4 /* prevent overflow */ + +static int validate_address_hint(void) +{ + char *ptr; + + ptr = mmap((void *) (1UL << 29), MAP_CHUNK_SIZE, PROT_READ | + PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (ptr == MAP_FAILED) + return 0; + + return -1; +} + +int main(int argc, char *argv[]) +{ + char *ptr[NR_CHUNKS_4G + 3]; + char line[1000]; + int chunks; + FILE *file; + int i; + + ksft_print_header(); + ksft_set_plan(1); + + /* try allocation beyond 4 GB */ + for (i = 0; i < NR_CHUNKS_4G + 3; ++i) { + ptr[i] = mmap(NULL, MAP_CHUNK_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (ptr[i] == MAP_FAILED) { + if (validate_address_hint()) + ksft_exit_fail_msg("VA exhaustion failed\n"); + break; + } + } + + chunks = i; + if (chunks >= NR_CHUNKS_4G) { + ksft_test_result_fail("mmapped chunks beyond 4GB\n"); + ksft_finished(); + } + + /* parse /proc/self/maps, confirm 32 bit VA mappings */ + file = fopen("/proc/self/maps", "r"); + if (file == NULL) + ksft_exit_fail_msg("/proc/self/maps cannot be opened\n"); + + while (fgets(line, sizeof(line), file)) { + const char *whitespace_loc, *hyphen_loc; + + hyphen_loc = strchr(line, '-'); + whitespace_loc = strchr(line, ' '); + + if (!(hyphen_loc && whitespace_loc)) { + ksft_test_result_skip("Unexpected format"); + ksft_finished(); + } + + if ((hyphen_loc - line > 8) || + (whitespace_loc - hyphen_loc) > 9) { + ksft_test_result_fail("Memory map more than 32 bits\n"); + ksft_finished(); + } + } + + for (int i = 0; i < chunks; ++i) + munmap(ptr[i], MAP_CHUNK_SIZE); + + ksft_test_result_pass("Test\n"); + ksft_finished(); +} -- 2.39.2