Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp3554977iob; Sat, 7 May 2022 08:07:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzEjyGzl4Y3ysYkPf4uezbJCEEqDi78awStfPwRgiiEjkzJALssLdE9z1vAVvo9wlsVn57P X-Received: by 2002:a17:90b:1e50:b0:1dc:dc64:da90 with SMTP id pi16-20020a17090b1e5000b001dcdc64da90mr8701695pjb.58.1651936063627; Sat, 07 May 2022 08:07:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651936063; cv=none; d=google.com; s=arc-20160816; b=xbUjbSDHez6GHiXgjZkNEQAsVzWCJLgzJlERk+QP+tWyd9uPGWTEByVCU6/D0o+49E jODPrhLYTrGfJftBsuop8CGP1KARFLlZ3q3mNBkA+qm3z3pYGNJvPEQmkYH4j6nf8vip e5Bt7IhokKBxrJCbX6Ci7EUnKEQLidr3xRKAnLvL8yPnwyjnVDKNoscVV6f0oLplz81j rlaFkcagwkNCA90UxN2W3B2v4Ud6JABG5DuD0ErqxI2tFWgLdDwONYPNwYquDnROLFIB 01ZL0nWs+gNj4GKo+/NCSktJgG5Lclw97zZdtXsdaZM3SkV8iw5sER/L8BkCW+GUsGkb aZcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=qCVbWPiaa6ENxqOlzwbFktIig1imZEufd0ql8kw3yik=; b=nU2ZrlsuOqAi7LucAMr121zYVifSFcgwzxmiVjRLPX0QnEW8tdEqC+iJiNBkaKMrrJ EksPNgvp95wv/mXPfk7yHuiwug26WrrtPrSc+nAU6rsrFfu/ZgkWxdZAc1uYBLKybc9X I0bdilOFw8+y2dcgU92pkNcaXfG88R674WPE0TQyVtj3mWV51lyhwSFxlhh/86OxCtRl EJz+OI4i0xlFNkHXzrFxH81r/yRaLGZiwHKnCk0rkY38V2Ph5RfSvgAMBsf0PDBmt8o7 qWkb/DQjM0sZ5IKxZcS0q7lLEp5nLhsOjCP7BiNQXHX0sQyXEV007f3GQRBgV30sDb9H Hs7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=loGFp8N1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cp15-20020a170902e78f00b001567d82c09asi5374033plb.174.2022.05.07.08.07.24; Sat, 07 May 2022 08:07:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=loGFp8N1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354285AbiEDR1t (ORCPT + 99 others); Wed, 4 May 2022 13:27:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355768AbiEDRIu (ORCPT ); Wed, 4 May 2022 13:08:50 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A74D651E5D; Wed, 4 May 2022 09:54:33 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 53E6561852; Wed, 4 May 2022 16:54:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E698C385A5; Wed, 4 May 2022 16:54:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1651683272; bh=uU32vsAIlQhNOwiz7baeDmqduhS8jWk8Pq/Li7uhVgw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=loGFp8N1zl16aS2O2slP2NLm6Z+oA9woLfHTQoJUMa82d7GQA0Hx22ZHrwMvcqT8z 176GBZCwvPhyOL5pUVsMxHlsEUld4wcS52Iu2gHV4IkTiBKPLsu18Rj7AaWdob1hmo y2g581J/WrBq7KRsRPnSlCfb0ybMMnyPJ7Inc9Qg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sidhartha Kumar , Shuah Khan , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.15 138/177] selftest/vm: verify mmap addr in mremap_test Date: Wed, 4 May 2022 18:45:31 +0200 Message-Id: <20220504153105.576914812@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220504153053.873100034@linuxfoundation.org> References: <20220504153053.873100034@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sidhartha Kumar [ Upstream commit 9c85a9bae267f6b5e5e374d0d023bbbe9db096d3 ] Avoid calling mmap with requested addresses that are less than the system's mmap_min_addr. When run as root, mmap returns EACCES when trying to map addresses < mmap_min_addr. This is not one of the error codes for the condition to retry the mmap in the test. Rather than arbitrarily retrying on EACCES, don't attempt an mmap until addr > vm.mmap_min_addr. Add a munmap call after an alignment check as the mappings are retained after the retry and can reach the vm.max_map_count sysctl. Link: https://lkml.kernel.org/r/20220420215721.4868-1-sidhartha.kumar@oracle.com Signed-off-by: Sidhartha Kumar Reviewed-by: Shuah Khan Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- tools/testing/selftests/vm/mremap_test.c | 41 +++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/vm/mremap_test.c b/tools/testing/selftests/vm/mremap_test.c index 0624d1bd71b5..2b3b4f15185f 100644 --- a/tools/testing/selftests/vm/mremap_test.c +++ b/tools/testing/selftests/vm/mremap_test.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -64,6 +65,35 @@ enum { .expect_failure = should_fail \ } +/* Returns mmap_min_addr sysctl tunable from procfs */ +static unsigned long long get_mmap_min_addr(void) +{ + FILE *fp; + int n_matched; + static unsigned long long addr; + + if (addr) + return addr; + + fp = fopen("/proc/sys/vm/mmap_min_addr", "r"); + if (fp == NULL) { + ksft_print_msg("Failed to open /proc/sys/vm/mmap_min_addr: %s\n", + strerror(errno)); + exit(KSFT_SKIP); + } + + n_matched = fscanf(fp, "%llu", &addr); + if (n_matched != 1) { + ksft_print_msg("Failed to read /proc/sys/vm/mmap_min_addr: %s\n", + strerror(errno)); + fclose(fp); + exit(KSFT_SKIP); + } + + fclose(fp); + return addr; +} + /* * Returns the start address of the mapping on success, else returns * NULL on failure. @@ -72,8 +102,15 @@ static void *get_source_mapping(struct config c) { unsigned long long addr = 0ULL; void *src_addr = NULL; + unsigned long long mmap_min_addr; + + mmap_min_addr = get_mmap_min_addr(); + retry: addr += c.src_alignment; + if (addr < mmap_min_addr) + goto retry; + src_addr = mmap((void *) addr, c.region_size, PROT_READ | PROT_WRITE, MAP_FIXED_NOREPLACE | MAP_ANONYMOUS | MAP_SHARED, -1, 0); @@ -91,8 +128,10 @@ static void *get_source_mapping(struct config c) * alignment in the tests. */ if (((unsigned long long) src_addr & (c.src_alignment - 1)) || - !((unsigned long long) src_addr & c.src_alignment)) + !((unsigned long long) src_addr & c.src_alignment)) { + munmap(src_addr, c.region_size); goto retry; + } if (!src_addr) goto error; -- 2.35.1