From patchwork Fri Mar 22 07:42:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10865175 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8540317EF for ; Fri, 22 Mar 2019 07:43:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 756E82A5CE for ; Fri, 22 Mar 2019 07:43:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 69BA82A5D1; Fri, 22 Mar 2019 07:43:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A12562A5D0 for ; Fri, 22 Mar 2019 07:43:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CD81D6B0003; Fri, 22 Mar 2019 03:43:40 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C88066B0006; Fri, 22 Mar 2019 03:43:40 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B50946B0007; Fri, 22 Mar 2019 03:43:40 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by kanga.kvack.org (Postfix) with ESMTP id 589BF6B0003 for ; Fri, 22 Mar 2019 03:43:40 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id o27so575298edc.14 for ; Fri, 22 Mar 2019 00:43:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=ybDGclxaljSScwU0omwGU4QDKMIsw3Rl4Ng/SK8EA6g=; b=uGvuVXWRIAQgmnsRa9ny2OLXCHelF2gdcq2NZlIAJjN7+k03Y4mek01Z8sdI17gh6S AxnnrpesRNsvMW+g0/iNbiBUMfsPUGADHHqwfyrSlARk18kq2lwvgPpS94Vou6rO217c YDZDAqFP760lcnnjcUiSt05G7NJUkkx5XM0+p4x65GvFjH5dBv9MRMB3dA+PQF/ZAaBS s2/eaPhmrkhMNWl1k8s98n/mLD6YkBo484V24EZ6bpa01z/eIUj73RCjnwPDoBD+pXEz 41lLSv4Bc5r3Kw8DXRUhlZYGD4Re3UYbJHF4Jgn7u7BWUfSQqoRDLNcf2+lLDEA4bzw3 n49A== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAVRBxahuDaw85BE3l09WB991OGiUFiK5KF1ivcFVgiNLgnNLtxR 3S0cm6U+6K8dDsR2czNLKCBc6CJRkzR6xnvHEGFX2by4CTpBu8d+LUUl18lQwxeCR9stIISrOtH T9gRkoWQR8LRZ+jeW7G7zO+jFv235XLoApj0D7Rdk7EI92a8EygloqnILyxE5wPo= X-Received: by 2002:a50:b615:: with SMTP id b21mr5095745ede.175.1553240619817; Fri, 22 Mar 2019 00:43:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqx3rFg+AO2e6D6R8NQ5KR+u8Ofq3RWtVgedPG1UDobavi1Csxk9Owo55N3m0tfnZOuBfNNb X-Received: by 2002:a50:b615:: with SMTP id b21mr5095699ede.175.1553240618765; Fri, 22 Mar 2019 00:43:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553240618; cv=none; d=google.com; s=arc-20160816; b=qINyUefJGMbTgNyj2iFUPxfuNFswOOmHxZ9x5bQqb7WovnrxpYV9kOhnz3Zaj4C2da XuIhukDCMYAbG6/YlvJPNjei9u+wn6JCYsWDhp9d02Hi9MpHrICE2OYb1xeUmxdxscop Yl2Rn5/ZSYsdC+Icwes9D14KJgnervlWaha//Ex+3s86p4oP1b/451GlVpVtx+aaHNnv Cno8+Q1GK1wO47MNCD42kmEi7qLqRiGdocfxScEQZY+Fe2vi65/RuSbkm1Xivez5GSAt W1bH+xRPKJ/B2hvj1UtW2JHNIylYN/u1v+pHUSTz6AO7nTfRxq3BK+m+mprm10RfqyPC 0UMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=ybDGclxaljSScwU0omwGU4QDKMIsw3Rl4Ng/SK8EA6g=; b=b+UZV4h/G4Y9Dd1/ykcluapmvHReWaFFNXzf6djMN/gltGRYFSB5MRqO1ZbhNa2Qug EcfvTFm2gEy9qEODLydcgznQ+Y7dDEYl5cZtl/2IpUxCSJuFiybT73Pnoyc2kXbPekB+ cQ61FoT2DnI6YKW5YSr4Jz+wELYB3UVFtKEh606gIKo06OVNL4Wg6XEZE2dBaR6kzmFq gSICD03IUbFS2dHAZZOSci9J5tn0Q6RMviztM2ODHc+NHExtsgX/KJll5p7YBbUOD//C xAhQuI8XOtboFHruPnkUViuULGFV3XRqfBMlDb+DIfpVWjWHPlA21sYZdgPrcy6Bw2kD vxrA== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay12.mail.gandi.net (relay12.mail.gandi.net. [217.70.178.232]) by mx.google.com with ESMTPS id s1si839095edx.17.2019.03.22.00.43.38 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 22 Mar 2019 00:43:38 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.178.232; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay12.mail.gandi.net (Postfix) with ESMTPSA id D314820000B; Fri, 22 Mar 2019 07:43:32 +0000 (UTC) From: Alexandre Ghiti To: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Cc: Alexandre Ghiti Subject: [PATCH 1/4] arm64, mm: Move generic mmap layout functions to mm Date: Fri, 22 Mar 2019 03:42:22 -0400 Message-Id: <20190322074225.22282-2-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190322074225.22282-1-alex@ghiti.fr> References: <20190322074225.22282-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP arm64 handles top-down mmap layout in a way that can be easily reused by other architectures, so make it available in mm. This commit also takes the opportunity to: - make use of is_compat_task instead of specific arm64 test test_thread_flag(TIF_32BIT), which allows more genericity and is equivalent. - fix the case where stack randomization should not be taken into account. It then introduces a new define ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT that can be defined by other architectures to benefit from those functions. Suggested-by: Christoph Hellwig Signed-off-by: Alexandre Ghiti --- arch/arm64/include/asm/processor.h | 2 +- arch/arm64/mm/mmap.c | 72 ---------------------- fs/binfmt_elf.c | 20 ------ include/linux/mm.h | 2 + kernel/sysctl.c | 6 +- mm/util.c | 99 +++++++++++++++++++++++++++++- 6 files changed, 105 insertions(+), 96 deletions(-) diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h index 5d9ce62bdebd..2358707c31ff 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -274,7 +274,7 @@ static inline void spin_lock_prefetch(const void *ptr) "nop") : : "p" (ptr)); } -#define HAVE_ARCH_PICK_MMAP_LAYOUT +#define ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT #endif diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c index 842c8a5fcd53..c74224421216 100644 --- a/arch/arm64/mm/mmap.c +++ b/arch/arm64/mm/mmap.c @@ -31,78 +31,6 @@ #include -/* - * Leave enough space between the mmap area and the stack to honour ulimit in - * the face of randomisation. - */ -#define MIN_GAP (SZ_128M) -#define MAX_GAP (STACK_TOP/6*5) - -static int mmap_is_legacy(struct rlimit *rlim_stack) -{ - if (current->personality & ADDR_COMPAT_LAYOUT) - return 1; - - if (rlim_stack->rlim_cur == RLIM_INFINITY) - return 1; - - return sysctl_legacy_va_layout; -} - -unsigned long arch_mmap_rnd(void) -{ - unsigned long rnd; - -#ifdef CONFIG_COMPAT - if (test_thread_flag(TIF_32BIT)) - rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); - else -#endif - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); - return rnd << PAGE_SHIFT; -} - -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) -{ - unsigned long gap = rlim_stack->rlim_cur; - unsigned long pad = (STACK_RND_MASK << PAGE_SHIFT) + stack_guard_gap; - - /* Values close to RLIM_INFINITY can overflow. */ - if (gap + pad > gap) - gap += pad; - - if (gap < MIN_GAP) - gap = MIN_GAP; - else if (gap > MAX_GAP) - gap = MAX_GAP; - - return PAGE_ALIGN(STACK_TOP - gap - rnd); -} - -/* - * This function, called very early during the creation of a new process VM - * image, sets up which VM layout function to use: - */ -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) -{ - unsigned long random_factor = 0UL; - - if (current->flags & PF_RANDOMIZE) - random_factor = arch_mmap_rnd(); - - /* - * Fall back to the standard layout if the personality bit is set, or - * if the expected stack growth is unlimited: - */ - if (mmap_is_legacy(rlim_stack)) { - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area = arch_get_unmapped_area; - } else { - mm->mmap_base = mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area = arch_get_unmapped_area_topdown; - } -} - /* * You really shouldn't be using read() or write() on /dev/mem. This might go * away in the future. diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 7d09d125f148..045f3b29d264 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -662,26 +662,6 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex, * libraries. There is no binary dependent code anywhere else. */ -#ifndef STACK_RND_MASK -#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */ -#endif - -static unsigned long randomize_stack_top(unsigned long stack_top) -{ - unsigned long random_variable = 0; - - if (current->flags & PF_RANDOMIZE) { - random_variable = get_random_long(); - random_variable &= STACK_RND_MASK; - random_variable <<= PAGE_SHIFT; - } -#ifdef CONFIG_STACK_GROWSUP - return PAGE_ALIGN(stack_top) + random_variable; -#else - return PAGE_ALIGN(stack_top) - random_variable; -#endif -} - static int load_elf_binary(struct linux_binprm *bprm) { struct file *interpreter = NULL; /* to shut gcc up */ diff --git a/include/linux/mm.h b/include/linux/mm.h index 76769749b5a5..087824a5059f 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2312,6 +2312,8 @@ extern int install_special_mapping(struct mm_struct *mm, unsigned long addr, unsigned long len, unsigned long flags, struct page **pages); +unsigned long randomize_stack_top(unsigned long stack_top); + extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); extern unsigned long mmap_region(struct file *file, unsigned long addr, diff --git a/kernel/sysctl.c b/kernel/sysctl.c index e5da394d1ca3..ac388f8ccbe4 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -269,7 +269,8 @@ extern struct ctl_table epoll_table[]; extern struct ctl_table firmware_config_table[]; #endif -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ + defined(ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) int sysctl_legacy_va_layout; #endif @@ -1564,7 +1565,8 @@ static struct ctl_table vm_table[] = { .proc_handler = proc_dointvec, .extra1 = &zero, }, -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ + defined(ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) { .procname = "legacy_va_layout", .data = &sysctl_legacy_va_layout, diff --git a/mm/util.c b/mm/util.c index d559bde497a9..5a0d4b1e17d9 100644 --- a/mm/util.c +++ b/mm/util.c @@ -14,6 +14,13 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include #include @@ -291,13 +298,103 @@ int vma_is_stack_for_current(struct vm_area_struct *vma) return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t)); } -#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) +#ifndef STACK_RND_MASK +#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */ +#endif + +unsigned long randomize_stack_top(unsigned long stack_top) +{ + unsigned long random_variable = 0; + + if (current->flags & PF_RANDOMIZE) { + random_variable = get_random_long(); + random_variable &= STACK_RND_MASK; + random_variable <<= PAGE_SHIFT; + } +#ifdef CONFIG_STACK_GROWSUP + return PAGE_ALIGN(stack_top) + random_variable; +#else + return PAGE_ALIGN(stack_top) - random_variable; +#endif +} + +#ifdef CONFIG_MMU +#ifdef ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT + +#ifdef CONFIG_ARCH_HAS_ELF_RANDOMIZE +unsigned long arch_mmap_rnd(void) +{ + unsigned long rnd; + +#ifdef CONFIG_COMPAT + if (is_compat_task()) + rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); + else +#endif + rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); + + return rnd << PAGE_SHIFT; +} +#endif + +static int mmap_is_legacy(struct rlimit *rlim_stack) +{ + if (current->personality & ADDR_COMPAT_LAYOUT) + return 1; + + if (rlim_stack->rlim_cur == RLIM_INFINITY) + return 1; + + return sysctl_legacy_va_layout; +} + +#define MIN_GAP (SZ_128M) +#define MAX_GAP (STACK_TOP / 6 * 5) + +static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) +{ + unsigned long gap = rlim_stack->rlim_cur; + unsigned long pad = stack_guard_gap; + + /* Account for stack randomization if necessary */ + if (current->flags & PF_RANDOMIZE) + pad += (STACK_RND_MASK << PAGE_SHIFT); + + /* Values close to RLIM_INFINITY can overflow. */ + if (gap + pad > gap) + gap += pad; + + if (gap < MIN_GAP) + gap = MIN_GAP; + else if (gap > MAX_GAP) + gap = MAX_GAP; + + return PAGE_ALIGN(STACK_TOP - gap - rnd); +} + +void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) +{ + unsigned long random_factor = 0UL; + + if (current->flags & PF_RANDOMIZE) + random_factor = arch_mmap_rnd(); + + if (mmap_is_legacy(rlim_stack)) { + mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; + mm->get_unmapped_area = arch_get_unmapped_area; + } else { + mm->mmap_base = mmap_base(random_factor, rlim_stack); + mm->get_unmapped_area = arch_get_unmapped_area_topdown; + } +} +#elif !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) { mm->mmap_base = TASK_UNMAPPED_BASE; mm->get_unmapped_area = arch_get_unmapped_area; } #endif +#endif /* CONFIG_MMU */ /* * Like get_user_pages_fast() except its IRQ-safe in that it won't fall From patchwork Fri Mar 22 07:42:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10865185 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AAAF917EF for ; Fri, 22 Mar 2019 07:44:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9CEC02A350 for ; Fri, 22 Mar 2019 07:44:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 917552A35A; Fri, 22 Mar 2019 07:44:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2803E2A350 for ; Fri, 22 Mar 2019 07:44:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E84886B0006; Fri, 22 Mar 2019 03:44:44 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E342D6B0007; Fri, 22 Mar 2019 03:44:44 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CFDE86B0008; Fri, 22 Mar 2019 03:44:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by kanga.kvack.org (Postfix) with ESMTP id 82B016B0006 for ; Fri, 22 Mar 2019 03:44:44 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id x29so570731edb.17 for ; Fri, 22 Mar 2019 00:44:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=Uz7p6g8xsr4/tuzcqVtmbZTVNIaPsr5ZFq4yt3MBowc=; b=R+C22ii40rD/UA/GB2H3a1RcxaVi5JRL6/Ta6JtlNY7bSuJ28nSpWV2use0qhrqq/6 lHArlhBELf9t8FRKc1IZthxK/wWSBoPGll/EYbrou6l1rMtvusPXC9V1ocak6WD9SSTi jCS+jy9hcK9qjyq30AGAYzg4DHZkLgXkI1F1C74MF3TreoFRcjFhD2EF82LB6cYoCq2C 0mQ9FAWdV01h0VbT9WcWOJye8dgsSvJfwH94ZX6peb95dM46PVs28v/2n0eQfeg1yutC 8NNZMJyReMPdf/ub1FDE9gKZdLSJYLu+1D2egwif2eHAIMdjwxKEZi7ZHARCrl+ZUnO+ xogQ== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAVM429fSE+pJuohdouDcn/1KU2F/dkb8QxJZ1uTissJueaYM9fJ LL3TF55w+RfsQnb+z9z7KpAMMrBtzwqa+3YRhqTzeFMv9jdBUAvWCZgNB1Kdo+4wl21QekPv3Sm dK11f7umFUFBzSOWMgAF8ZFFtZ1IBmGEyOj2xYb2Th8pDhdZM0+xkR+33RI67zoE= X-Received: by 2002:a17:906:6d8f:: with SMTP id h15mr4688273ejt.107.1553240684030; Fri, 22 Mar 2019 00:44:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqwHQu01vpk/ugAd1PtvK4cyASoEAFphxkXeJnFDqTY98vsJkRC9bCUtkNiKEW4mHSapO3UQ X-Received: by 2002:a17:906:6d8f:: with SMTP id h15mr4688245ejt.107.1553240683171; Fri, 22 Mar 2019 00:44:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553240683; cv=none; d=google.com; s=arc-20160816; b=N33l6Yf/IJggpWqn0DhxthzYt0MimeweIC5mGwUI3/C7sNSqqgMIeWVhttU/6Der+3 2yA1VBHRrrsd7yTFvsOqPgpIh6YoyRidTL/rUUyum3m6GlHArdG6s3SkP74Q7Td3loWh DqHFSCI3JK3SkGpj9ivmVLh0ZbKQEHdwtDiMyNRkgPLaKb/SOW5NXWVqILwUc4NTdbJH QCwalc/0xygBXZuPIh4kRqClVXjkqUfat7qR2a0Ol1+3kkWczfzdAVJuCePJjZAgZ7qT nqR74PD5zVcj7Wpo56dY8QQPdp+dvbpKbo36X5QLCp1O/rE9XOZOuKoiXvaCJv0pdM8w 8ksQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=Uz7p6g8xsr4/tuzcqVtmbZTVNIaPsr5ZFq4yt3MBowc=; b=RdeB/3khSkPPmH358ljo7gQ2lEUUS9ML9wJCyizPhVK6iwHFvwGCkBtXtu/Adrfaus BnIN76QD68KPObebOESoEtUFkaALRhhJK2bjGPs/vHyPO7X0KvVKuCfk36qig2f8UbzZ 8N5H9pSCTaZtuMQLXNfUPwWVXj07VI85pZFhCT3fAPWqF69/2IHqzUKejs/6VzCQAXww GnK43ibLlZLEcUS07v6MTRpEx2Z7TZxFFdE6WgU0x1m8cYL7jkn3hZ6HuVy4DHBs4zhz BYLTB8iR9nkCvPq64/H6XrM89LhIs2dlieSQy27g2p5m+2kYUFYhWlujODm+GPrqI/71 ahbw== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay12.mail.gandi.net (relay12.mail.gandi.net. [217.70.178.232]) by mx.google.com with ESMTPS id e47si2318365edd.164.2019.03.22.00.44.42 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 22 Mar 2019 00:44:43 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.178.232; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay12.mail.gandi.net (Postfix) with ESMTPSA id E7FFE20000D; Fri, 22 Mar 2019 07:44:38 +0000 (UTC) From: Alexandre Ghiti To: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Cc: Alexandre Ghiti Subject: [PATCH 2/4] arm: Use generic mmap top-down layout Date: Fri, 22 Mar 2019 03:42:23 -0400 Message-Id: <20190322074225.22282-3-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190322074225.22282-1-alex@ghiti.fr> References: <20190322074225.22282-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP arm uses a top-down layout by default that fits the generic functions. At the same time, this commit allows to fix the following problems: - one uncovered and not fixed for arm here: https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg1429066.html - the use of TASK_SIZE instead of STACK_TOP in mmap_base which, when address space of a task is 26 bits, would assign mmap base way too high. Signed-off-by: Alexandre Ghiti --- arch/arm/include/asm/processor.h | 2 +- arch/arm/mm/mmap.c | 52 -------------------------------- 2 files changed, 1 insertion(+), 53 deletions(-) diff --git a/arch/arm/include/asm/processor.h b/arch/arm/include/asm/processor.h index 57fe73ea0f72..03837d325a2f 100644 --- a/arch/arm/include/asm/processor.h +++ b/arch/arm/include/asm/processor.h @@ -143,7 +143,7 @@ static inline void prefetchw(const void *ptr) #endif #endif -#define HAVE_ARCH_PICK_MMAP_LAYOUT +#define ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT #endif diff --git a/arch/arm/mm/mmap.c b/arch/arm/mm/mmap.c index f866870db749..b8d912ac9e61 100644 --- a/arch/arm/mm/mmap.c +++ b/arch/arm/mm/mmap.c @@ -17,33 +17,6 @@ ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \ (((pgoff)<personality & ADDR_COMPAT_LAYOUT) - return 1; - - if (rlim_stack->rlim_cur == RLIM_INFINITY) - return 1; - - return sysctl_legacy_va_layout; -} - -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) -{ - unsigned long gap = rlim_stack->rlim_cur; - - if (gap < MIN_GAP) - gap = MIN_GAP; - else if (gap > MAX_GAP) - gap = MAX_GAP; - - return PAGE_ALIGN(TASK_SIZE - gap - rnd); -} - /* * We need to ensure that shared mappings are correctly aligned to * avoid aliasing issues with VIPT caches. We need to ensure that @@ -171,31 +144,6 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, return addr; } -unsigned long arch_mmap_rnd(void) -{ - unsigned long rnd; - - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); - - return rnd << PAGE_SHIFT; -} - -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) -{ - unsigned long random_factor = 0UL; - - if (current->flags & PF_RANDOMIZE) - random_factor = arch_mmap_rnd(); - - if (mmap_is_legacy(rlim_stack)) { - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area = arch_get_unmapped_area; - } else { - mm->mmap_base = mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area = arch_get_unmapped_area_topdown; - } -} - /* * You really shouldn't be using read() or write() on /dev/mem. This * might go away in the future. From patchwork Fri Mar 22 07:42:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10865193 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3AD8B13B5 for ; Fri, 22 Mar 2019 07:45:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1F80B2A5C2 for ; Fri, 22 Mar 2019 07:45:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E77D2A148; Fri, 22 Mar 2019 07:45:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7CE2E2A148 for ; Fri, 22 Mar 2019 07:45:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 954086B0003; Fri, 22 Mar 2019 03:45:49 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 9027F6B0006; Fri, 22 Mar 2019 03:45:49 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7CB716B0007; Fri, 22 Mar 2019 03:45:49 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by kanga.kvack.org (Postfix) with ESMTP id 3173F6B0003 for ; Fri, 22 Mar 2019 03:45:49 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id s27so572458eda.16 for ; Fri, 22 Mar 2019 00:45:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=LUGImeOjzmNJpIPAM8RQUWM+8kkl6UbTjBKYFhKUvaE=; b=hKXH0GYskHPB7VrmZsmdxyikuevNqG4bpS2j+Rs9YRviNp6tsU1lXATRyBC01eOJPX BvyWTsHQ42AGLP1WckNDRN8I80E5QKyFF/uM3gdeDMlBGjvRNAkPpFKmSW9fdpxKnXDq J3qXGOdm4Y57/ujnPnwOyjO09sCEgsUHrVene9LYlVbYVEHkCtoLE3PLFh7QlH5a4PEG V4x0AoTuQTsJwb5XeXb5yRDiH78P9S/+7uHF3qm+l9Xzi5ZTc4wtXGrEH81ufBxY81Z5 hi//6eB9I5wn6wuABxzV3D2r87z0s/jNi5oe7HqII/2u4b2FMerykeRwfNFgfUdBWfaB p3Vw== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAXV8gJZMas0BxG74a6VFq2dXf0Z5HG9pcUG5HUYw4MW4LMxnAe0 tLpvh4oczwofgtFBiAsN7ccGgNo9pVxBdpPRFBMJDX0KHAS1ObGx3ddgsMgPvKhKRJNUrkecwkc ovZKE4dX24fPU8wQhteFSDEDg7hpJQyQ/UGsukT8fnFjuXjNV4E6dvvw9Zq0lRzc= X-Received: by 2002:a50:ad72:: with SMTP id z47mr5394504edc.270.1553240748723; Fri, 22 Mar 2019 00:45:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqz2r8MPPlzle2xiUiBqJ54GWuWSaYAZNi5PoypuqPAcZ9UbbiGnWI+/3hPou05ECMTy83P4 X-Received: by 2002:a50:ad72:: with SMTP id z47mr5394460edc.270.1553240747766; Fri, 22 Mar 2019 00:45:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553240747; cv=none; d=google.com; s=arc-20160816; b=XL/VXIIJ4CRTSD2z2PlGR0HbbsTxhuzKw1+3K4kb8VE5shvA+sSY0dGmd1RLcjr5xL yedFtyS10hyEJSPuzUmkxS9NuXFs1LQkmYf1+RSgk4U3P74EV03tJirAc+lq9TnqZjib mIYlmYag3zaD7J3eBIphYN6YnOhPhtO10URK6emCofftcTX8kycQAHHCGAw20z93BE9f Zhg5SYd6ND2Gr7+xOqmOikLR0Il+cxkntmZ5kGanUjxvX/4MFUw89ts9ap/+EnbKd56t xRzPEu6QrR+X/U1xWRtE5HP5AK+DPTMjjny6TskkErCR914IJPEQnJaPJ98v8pJtBEF6 lfbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=LUGImeOjzmNJpIPAM8RQUWM+8kkl6UbTjBKYFhKUvaE=; b=l2EF7+JVRUkHoFkjkR51BYqbXYnJHVH9WosGIHIfycEY+3caU//7a7QQzq0JakDUQa +Z8WS5OJM+1yCAiS931htJ8Pz0akMy3aXUsdROlD+G1v+Rt90hMarjNc4KNFk8y//otT Rc8f/5p86dLGULs2lqRz9/TyIAvsHqygNykar5u0PVyplpjadTi7aP7mW3QTGe+hHFmB pRBBUBObBv62m2SXmf9048aCmrKAOfZwxvM4ULvM+E9xoz5PexElBaLNUHTo0/dX+nHn 4BqC5Usu+BQUonUWsKOeR9ym6x7s11NhCi1WIhTJaGAKpIkfYxYwvIYwPJ1Ea4HwLrqu aEBg== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay12.mail.gandi.net (relay12.mail.gandi.net. [217.70.178.232]) by mx.google.com with ESMTPS id i4si2518218ejb.200.2019.03.22.00.45.47 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 22 Mar 2019 00:45:47 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.178.232; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay12.mail.gandi.net (Postfix) with ESMTPSA id 4A88E20001D; Fri, 22 Mar 2019 07:45:43 +0000 (UTC) From: Alexandre Ghiti To: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Cc: Alexandre Ghiti Subject: [PATCH 3/4] mips: Use generic mmap top-down layout Date: Fri, 22 Mar 2019 03:42:24 -0400 Message-Id: <20190322074225.22282-4-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190322074225.22282-1-alex@ghiti.fr> References: <20190322074225.22282-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP mips uses a top-down layout by default that fits the generic functions. At the same time, this commit allows to fix problem uncovered and not fixed for mips here: https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg1429066.html Signed-off-by: Alexandre Ghiti --- arch/mips/include/asm/processor.h | 4 +-- arch/mips/mm/mmap.c | 57 ------------------------------- 2 files changed, 2 insertions(+), 59 deletions(-) diff --git a/arch/mips/include/asm/processor.h b/arch/mips/include/asm/processor.h index aca909bd7841..f8e04962b52d 100644 --- a/arch/mips/include/asm/processor.h +++ b/arch/mips/include/asm/processor.h @@ -30,9 +30,9 @@ extern unsigned int vced_count, vcei_count; /* - * MIPS does have an arch_pick_mmap_layout() + * MIPS uses the default implementation of topdown mmap layout. */ -#define HAVE_ARCH_PICK_MMAP_LAYOUT 1 +#define ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT #ifdef CONFIG_32BIT #ifdef CONFIG_KVM_GUEST diff --git a/arch/mips/mm/mmap.c b/arch/mips/mm/mmap.c index 2f616ebeb7e0..61e65a69bb09 100644 --- a/arch/mips/mm/mmap.c +++ b/arch/mips/mm/mmap.c @@ -20,33 +20,6 @@ unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ EXPORT_SYMBOL(shm_align_mask); -/* gap between mmap and stack */ -#define MIN_GAP (128*1024*1024UL) -#define MAX_GAP ((TASK_SIZE)/6*5) - -static int mmap_is_legacy(struct rlimit *rlim_stack) -{ - if (current->personality & ADDR_COMPAT_LAYOUT) - return 1; - - if (rlim_stack->rlim_cur == RLIM_INFINITY) - return 1; - - return sysctl_legacy_va_layout; -} - -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) -{ - unsigned long gap = rlim_stack->rlim_cur; - - if (gap < MIN_GAP) - gap = MIN_GAP; - else if (gap > MAX_GAP) - gap = MAX_GAP; - - return PAGE_ALIGN(TASK_SIZE - gap - rnd); -} - #define COLOUR_ALIGN(addr, pgoff) \ ((((addr) + shm_align_mask) & ~shm_align_mask) + \ (((pgoff) << PAGE_SHIFT) & shm_align_mask)) @@ -144,36 +117,6 @@ unsigned long arch_get_unmapped_area_topdown(struct file *filp, addr0, len, pgoff, flags, DOWN); } -unsigned long arch_mmap_rnd(void) -{ - unsigned long rnd; - -#ifdef CONFIG_COMPAT - if (TASK_IS_32BIT_ADDR) - rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); - else -#endif /* CONFIG_COMPAT */ - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); - - return rnd << PAGE_SHIFT; -} - -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) -{ - unsigned long random_factor = 0UL; - - if (current->flags & PF_RANDOMIZE) - random_factor = arch_mmap_rnd(); - - if (mmap_is_legacy(rlim_stack)) { - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area = arch_get_unmapped_area; - } else { - mm->mmap_base = mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area = arch_get_unmapped_area_topdown; - } -} - static inline unsigned long brk_rnd(void) { unsigned long rnd = get_random_long(); From patchwork Fri Mar 22 07:42:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10865205 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 06E106C2 for ; Fri, 22 Mar 2019 07:46:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ECA432A5CE for ; Fri, 22 Mar 2019 07:46:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DFE302A5D1; Fri, 22 Mar 2019 07:46:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AD7C22A5CE for ; Fri, 22 Mar 2019 07:46:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DBE5E6B0003; Fri, 22 Mar 2019 03:46:53 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D6FEB6B0006; Fri, 22 Mar 2019 03:46:53 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C5FD96B0007; Fri, 22 Mar 2019 03:46:53 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by kanga.kvack.org (Postfix) with ESMTP id 703B96B0003 for ; Fri, 22 Mar 2019 03:46:53 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id d5so568562edl.22 for ; Fri, 22 Mar 2019 00:46:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=7Gg0IZxgIeJmu+DqH3rLY2Wqv1iOmyqWzpnIt69wZAg=; b=qZhGe4++zsxsJDcZV6j+ukXiU6WSPSo422vI6Ong6MjZsg729sotMClpNkZgXjkJXn cNmjLlehbytPcpgji02WQd9uxxVp+DGm0cSGbvCbaOahU32pDfGx11T40nEieZqiD2ko myPt+Rp/zCCpaE55lPJGZEdPCcpjqrV0vrwF0hHfTLsziU0LxJbPa21TWeHDkcDbFrDI hDmzCrhHMsGkqzGS9d327eXupQbdMlIYk2DVyxKtKhvir5/f7T3eSDIeMjXi9NwY7fZw CGkcTbZ3amR2cFx3E95UZWU4QY5rZydogj0Q04gj8hh4OqDeculxOGkgYZbAJrlJf9Lh l79g== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAUkE8xaoEcIAVJfmks+mpjXejbnIbILBmRgrdtU4dsnt2p96zaB scD+viUWo2mbFUP2zWzfQFYfBWJ77nInuqJZzabdPA+HC7Zi75L7L3x4IQohJs3PvfXD2qka1/w 0PUt7GhqrmoYxAUsX9ueYlRzWXc6w4ZhXCKl+nNpa/k21JsGbDdSrBww9RF2etLg= X-Received: by 2002:a50:ad72:: with SMTP id z47mr5397345edc.270.1553240812987; Fri, 22 Mar 2019 00:46:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqynb3N7cowu9XH6gCtyl3h+4Ag1t9L5WKfCPpKOOTh1eT5rDQYQrmqWanFiJW0cwgVcYaOD X-Received: by 2002:a50:ad72:: with SMTP id z47mr5397313edc.270.1553240812098; Fri, 22 Mar 2019 00:46:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553240812; cv=none; d=google.com; s=arc-20160816; b=XA7ahTHZcDRLpgPNkG5n/3E2qGhCeR9GJ0sJxY4DvSaNchtXQcVqJ3N+ttIQauAOfJ Eq8PPWy76RrJA2D3cSgrQ15v+Kl4wva3YDzuhwoszwGqbyZNmKUlXu1Ee++ZUNIUa7/9 Y7XIzdNUxCE8uTu/tb7bAWNRY4ftEb9VrSqCRqMTnfB+SL20jgDdc+T9IjAiqEqWm4m0 y8cCX5nNeR4ke8smJNuwxyUeNhCPDy8xDKc8iT5qt2F+dJ8f6yblQg7BYvP8CbF1IeJE m7EHZyyS39EMMVFhYdWQV5l3yc9mAlTk+81Wwaa5XZ2vVrLDyfO3gG0rr0rqSrU1Px2/ PESw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=7Gg0IZxgIeJmu+DqH3rLY2Wqv1iOmyqWzpnIt69wZAg=; b=jZfSkshJrfGBX+8rzAMF/gOHwgJgWMQlq3x2cBtHdY3uc1kKUPZYCg9WB9MYLTYZD2 rWmuPmDng/toFQqjK91oBMtYfXDFjLOL+8CKyYVjKyUaE3l3M3l15dYYRkWQFuFnffTx KVm4lhehh2KSTWZVXApf/vk2U68fDO27A7sJ3GYSLYrVkWewyV3m4ObJ10K9PIqrPuHU fcVtgaqYYeOzwChNNUPQk+gOd7CAq0j0je6i+uiZeE9nyJ0IuCttXjDRwusCEIklFilZ cLX9+vD1Y62UkwR0BInOrGgppBRGf1xrGkwRTb/9kAsWtS3PSmBsiHmDvdZxskc8mQV8 CWBQ== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay12.mail.gandi.net (relay12.mail.gandi.net. [217.70.178.232]) by mx.google.com with ESMTPS id l25si785296ejg.53.2019.03.22.00.46.51 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 22 Mar 2019 00:46:52 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.178.232; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.232 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay12.mail.gandi.net (Postfix) with ESMTPSA id EECA9200009; Fri, 22 Mar 2019 07:46:47 +0000 (UTC) From: Alexandre Ghiti To: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Cc: Alexandre Ghiti Subject: [PATCH 4/4] riscv: Make mmap allocation top-down by default Date: Fri, 22 Mar 2019 03:42:25 -0400 Message-Id: <20190322074225.22282-5-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190322074225.22282-1-alex@ghiti.fr> References: <20190322074225.22282-1-alex@ghiti.fr> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP In order to avoid wasting user address space by using bottom-up mmap allocation scheme, prefer top-down scheme when possible. Before: root@qemuriscv64:~# cat /proc/self/maps 00010000-00016000 r-xp 00000000 fe:00 6389 /bin/cat.coreutils 00016000-00017000 r--p 00005000 fe:00 6389 /bin/cat.coreutils 00017000-00018000 rw-p 00006000 fe:00 6389 /bin/cat.coreutils 00018000-00039000 rw-p 00000000 00:00 0 [heap] 1555556000-155556d000 r-xp 00000000 fe:00 7193 /lib/ld-2.28.so 155556d000-155556e000 r--p 00016000 fe:00 7193 /lib/ld-2.28.so 155556e000-155556f000 rw-p 00017000 fe:00 7193 /lib/ld-2.28.so 155556f000-1555570000 rw-p 00000000 00:00 0 1555570000-1555572000 r-xp 00000000 00:00 0 [vdso] 1555574000-1555576000 rw-p 00000000 00:00 0 1555576000-1555674000 r-xp 00000000 fe:00 7187 /lib/libc-2.28.so 1555674000-1555678000 r--p 000fd000 fe:00 7187 /lib/libc-2.28.so 1555678000-155567a000 rw-p 00101000 fe:00 7187 /lib/libc-2.28.so 155567a000-15556a0000 rw-p 00000000 00:00 0 3fffb90000-3fffbb1000 rw-p 00000000 00:00 0 [stack] After: root@qemuriscv64:~# cat /proc/self/maps 00010000-00016000 r-xp 00000000 fe:00 6389 /bin/cat.coreutils 00016000-00017000 r--p 00005000 fe:00 6389 /bin/cat.coreutils 00017000-00018000 rw-p 00006000 fe:00 6389 /bin/cat.coreutils 00018000-00039000 rw-p 00000000 00:00 0 [heap] 3ff7eb6000-3ff7ed8000 rw-p 00000000 00:00 0 3ff7ed8000-3ff7fd6000 r-xp 00000000 fe:00 7187 /lib/libc-2.28.so 3ff7fd6000-3ff7fda000 r--p 000fd000 fe:00 7187 /lib/libc-2.28.so 3ff7fda000-3ff7fdc000 rw-p 00101000 fe:00 7187 /lib/libc-2.28.so 3ff7fdc000-3ff7fe2000 rw-p 00000000 00:00 0 3ff7fe4000-3ff7fe6000 r-xp 00000000 00:00 0 [vdso] 3ff7fe6000-3ff7ffd000 r-xp 00000000 fe:00 7193 /lib/ld-2.28.so 3ff7ffd000-3ff7ffe000 r--p 00016000 fe:00 7193 /lib/ld-2.28.so 3ff7ffe000-3ff7fff000 rw-p 00017000 fe:00 7193 /lib/ld-2.28.so 3ff7fff000-3ff8000000 rw-p 00000000 00:00 0 3fff888000-3fff8a9000 rw-p 00000000 00:00 0 [stack] Signed-off-by: Alexandre Ghiti --- arch/riscv/Kconfig | 12 ++++++++++++ arch/riscv/include/asm/processor.h | 1 + 2 files changed, 13 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index eb56c82d8aa1..7661335d1667 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -50,6 +50,18 @@ config RISCV select ARCH_HAS_PTE_SPECIAL select HAVE_EBPF_JIT if 64BIT +config HAVE_ARCH_MMAP_RND_BITS + def_bool y + +config ARCH_MMAP_RND_BITS_MIN + default 18 + +# max bits determined by the following formula: +# VA_BITS - PAGE_SHIFT - 3 +config ARCH_MMAP_RND_BITS_MAX + default 33 if 64BIT # SV48 based + default 18 + config MMU def_bool y diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index ce70bceb8872..e68a1b1e144a 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -23,6 +23,7 @@ * space during mmap's. */ #define TASK_UNMAPPED_BASE PAGE_ALIGN(TASK_SIZE / 3) +#define ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT #define STACK_TOP TASK_SIZE #define STACK_TOP_MAX STACK_TOP