Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1123817iob; Wed, 4 May 2022 15:20:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyaDYBnBA2yByu3KbmJU8sGfNlni43kgKrGV9UF9mEaI4cnmgYtE4AAqXKQ4W8lCFjTjbrp X-Received: by 2002:a17:90a:730c:b0:1d9:3f5:9a00 with SMTP id m12-20020a17090a730c00b001d903f59a00mr2050040pjk.109.1651702800324; Wed, 04 May 2022 15:20:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651702800; cv=none; d=google.com; s=arc-20160816; b=gyV+Vb2f4kmmfMKLVMqmrAbMZ5riEd09W4QbKogPq7hC0EpMMdBWDFPMBGJuu0NFYA 5fat1t6GTBlgm5gTYxiVaRR4EW+IjqsLH9uCCI+j06R9yH7yF7xx3qFH9T/ZC/gjBIY2 6X00iq9zF9gdm+z8k2V3Y3I4CoiUebZXQPfryebMz34JhXBzNOPy8sA/9xK1SNTYdrq9 y50Ue1xSbDWQTfj0WDp7BVp7cKMG+ph7v6IYKSzj7mre5WnqIctuy9NyVGI5o78dtPSP kFix8orEgEUGOIXYMaUaKt8zDgzpdukqAR8SxPlNqi7wPRyk0ARwhTPNhz/a3jBPWFcT KxjA== 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=hITJBUAABwH49S0NzS0Rl14c/2XwZXYmFX6vlQxJh8s=; b=bTM5PWI2UF3pIVYtezIlDOmi1sxnJNbZQIzc8a6ih5Yjhjb1lMBndXPfVmb1m3y/Nq N8YA8tKt/6HZxe86i4bNAHojzj71OeCYKiloNxFkftdQsIQ43ixezUpcSmqItl/+CPbl lR3huO6vKh1xgell6iQrGNSrXXP51k3atb2/VcsGaf9T2xK+7aA1qcoKm5ggaQKQVeCX kQ2KYLaIIG90E/xOd6yx9qaG0vUELkVH7CP18HalqSRnhEo0YrlfbVEL5DjLp+xE/lh8 Vg0u83hGQ1WBDiAOWy8otrxFft+Yld6EuO4AoDCsl+XLROBpFAjENaL+k7yKHAtYPfDS Y2+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fXIZXqaT; 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 l5-20020a170903244500b0015454f1f0dfsi9289pls.28.2022.05.04.15.19.44; Wed, 04 May 2022 15:20:00 -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=fXIZXqaT; 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 S1359239AbiEDRs4 (ORCPT + 99 others); Wed, 4 May 2022 13:48:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357466AbiEDRPD (ORCPT ); Wed, 4 May 2022 13:15:03 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E705D54F84; Wed, 4 May 2022 09:58:36 -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 ams.source.kernel.org (Postfix) with ESMTPS id 14641B827A9; Wed, 4 May 2022 16:58:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8EFFBC385A4; Wed, 4 May 2022 16:58:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1651683512; bh=YZK3UpV4c8YazR2b40tUH2JL2rStNLtVLCBp2JIEX9I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fXIZXqaTHKbnxRhckFT3iO5AXltO2E+lN/GWHdHIS+S+T646AH2pHa/aASx3cIIHm 0LMHPJYnuZBQ8eK7dJpA7rw6/d1iv4VHnbd7k0G9KPObBx5usiATVl55Z5dDzyClQA YIGCpZ2T6LPChzbR09pIirDlkRux229IvhXCh8bY= 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.17 175/225] selftest/vm: verify mmap addr in mremap_test Date: Wed, 4 May 2022 18:46:53 +0200 Message-Id: <20220504153125.927307796@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220504153110.096069935@linuxfoundation.org> References: <20220504153110.096069935@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 7c0b0617b9f8..9a518fee6306 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 @@ -63,6 +64,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. @@ -71,8 +101,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); @@ -90,8 +127,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