From patchwork Thu Apr 4 05:51: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: 10884969 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 742AC922 for ; Thu, 4 Apr 2019 05:52:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5CC8128A13 for ; Thu, 4 Apr 2019 05:52:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5062928A26; Thu, 4 Apr 2019 05:52:47 +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 C285128A13 for ; Thu, 4 Apr 2019 05:52:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D17536B0266; Thu, 4 Apr 2019 01:52:45 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id CA1086B0269; Thu, 4 Apr 2019 01:52:45 -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 BB7636B026A; Thu, 4 Apr 2019 01:52:45 -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 6CF9D6B0266 for ; Thu, 4 Apr 2019 01:52:45 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id c41so778055edb.7 for ; Wed, 03 Apr 2019 22:52:45 -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=LL6RU279tCfBqukysjR+SCVmsM25jmGkfMsRMhI0V8A=; b=SLmAeNMhoh4o4qUE4c1C7f7Tk/2SL4Hk/tX3zOZ+dnzEMKO2yjyo7UGs2Lg0juCssd 0BJB12uaAVw7KSKJUy+uskQ5Q8mD+LBRzQ+tOdvc2en5ul6atYKGGVF1BydboML8YjRi VHYz3zXmswAO05XVe8MmA8EIM/1iQtOEaeoxQM3/re6hMy/N/+S40y/OoK0w213kKCRk 3VTeLY4nBB7j76n5QKach6mfkpU4+g7BMxTDA/l0FuTI2jYv13J6QClLYY98Ms5ruQ5O fKydFHA3AwdhB8NtbYiMn7DOejYaLBg38kx/A8MAERFVf55I5P1O/WqCDbOQHHwpupBi eNwQ== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.197 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAXPy37nTkwslDKWlqM1lhHNsUZcinkxnEvhESi7opUjK//TrADK nlaMmX+aOdQCcXIiIjXQQyHza1xH1h4l8dRBe4eaAowJpr9DUUruUux9qKJRbSifOZ5D5A6g93K mDIKWjIMt8gsEjbrHE1xq4RCAaA56qKI+cfyBY3HOhWsDTPb+bUeImMVhefyI2VI= X-Received: by 2002:a17:906:6152:: with SMTP id p18mr2286360ejl.245.1554357164962; Wed, 03 Apr 2019 22:52:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqyfulHiPH3Iepesx3/C1zBEki59a9dUmt8VR3R3N6NKmnVt/i9j2fESfjckRNJ7u51TunA8 X-Received: by 2002:a17:906:6152:: with SMTP id p18mr2286327ejl.245.1554357164061; Wed, 03 Apr 2019 22:52:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554357164; cv=none; d=google.com; s=arc-20160816; b=USCpY6KeFP5oPS9ljJT+5aQKgeRNLozg8oWzD81KbiWGwfNBUYfi8RT4ijVaN8bsSs vab/ebvKCCYHkYGUt1SrgkCLj0CD0XmMiJyT+5VygwgNr7mDwx6Ox4EgZL0oXpD5mr+r EBbMn+4X9ZTtzxRTaBGeRHEKKUbcA7YBLvp+JJDD1qR2Lh+301LY6ZShAzYPGgvMdu7d 709KeePGwMcaWZmYYDrlmP6grAQsnOaMzni/5p7qrAWDGtmKDEcnF00xtX4tsRXLGCmW 7OsFjyWJthqbjAJ2673KLyrWG8mwuXMY3fwhIrygS46QvDxHPMQv8cjfBXZMy9sGuezz IRtA== 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=LL6RU279tCfBqukysjR+SCVmsM25jmGkfMsRMhI0V8A=; b=actRUcnGBU2SWrQDtmIcgqJiZgzhTPX0zlHDSqu1H/5uNL2sf/acVWu71jQUF91R6P zE76QZP/DuSVxQdWitZsM/amgeQ1ERuKeQWnlT+dARpBozCYFGFSwMJUQYti1k9sqqgT 22gGbxQvgbffaQjOS2XYfHcCogHvUmQQbuj5b1c+2f9ahZB1vqBnXm7ICkZm+D0gLvIw LlFKenCMjRq8aquPa5+yesjMmP85w5rNQa2i6OrQWUdSRj0ArC4F7XCyEwio3PCPQAyM O19C5QWQRK8R70/+B1g2007THYLXaRqY7GbTzDEI5QxOnDTMjHDX3k+OMzgx5OyMCmMf 9nIA== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.197 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net. [217.70.183.197]) by mx.google.com with ESMTPS id i4si979704edr.263.2019.04.03.22.52.43 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 03 Apr 2019 22:52:44 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.197 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.197; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.197 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay5-d.mail.gandi.net (Postfix) with ESMTPSA id 867E61C000C; Thu, 4 Apr 2019 05:52:35 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: 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, Alexandre Ghiti Subject: [PATCH v2 1/5] mm, fs: Move randomize_stack_top from fs to mm Date: Thu, 4 Apr 2019 01:51:24 -0400 Message-Id: <20190404055128.24330-2-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190404055128.24330-1-alex@ghiti.fr> References: <20190404055128.24330-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 This preparatory commit moves this function so that further introduction of generic topdown mmap layout is contained only in mm/util.c. Signed-off-by: Alexandre Ghiti Reviewed-by: Christoph Hellwig --- fs/binfmt_elf.c | 20 -------------------- include/linux/mm.h | 2 ++ mm/util.c | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 20 deletions(-) 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/mm/util.c b/mm/util.c index d559bde497a9..a54afb9b4faa 100644 --- a/mm/util.c +++ b/mm/util.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include @@ -291,6 +293,26 @@ int vma_is_stack_for_current(struct vm_area_struct *vma) return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t)); } +#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 +} + #if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) { From patchwork Thu Apr 4 05:51: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: 10884981 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 9DF0617EE for ; Thu, 4 Apr 2019 05:53:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8AAA128A13 for ; Thu, 4 Apr 2019 05:53:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7EC4328A21; Thu, 4 Apr 2019 05:53:52 +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 BEDE828A1F for ; Thu, 4 Apr 2019 05:53:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DFF516B026A; Thu, 4 Apr 2019 01:53:50 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D8C5A6B026B; Thu, 4 Apr 2019 01:53:50 -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 C519C6B026C; Thu, 4 Apr 2019 01:53:50 -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 6DF456B026A for ; Thu, 4 Apr 2019 01:53:50 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id n24so767051edd.21 for ; Wed, 03 Apr 2019 22:53:50 -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=CwVLZGCT2pFidty3RGwl2qOplGjaAzcw2zp+WCWmDxU=; b=R2U1aUx+VZtCZdELdjKl6pHOtmZBwwGRg+15tMavczNJ/BD77+q5Ng7uO3YiKyNbgs N5JrkcZve25FPvUQfQR7IvSHLFZt/km+MzU49dGeaTSMXoU2llJnRhnG4nL6772fviVV a21biu76qIwt+Qy6s1fFbqRvhZybNUb1UB3lrQO1dwqpAF2gINLHNmbPma0OY35v4lA9 6zBwJmicqkC4GvouVxur8naA99f0n9UFzRODNyp9AsKaV+x6Xjhg2YyeKf0D0grb7F3M 7q3E3lXPwPdtrxPf8Gh/D+6mAAdWuQ7fqQ87o6AOC5CdUjX+bEK0Kc0rOBqY80MjMXMJ CdIA== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAWwAQraB+wKs+2onIWJ2p/aJ7hAQvYlRq4AUlcDOz/h31hBetro 7IJIh9RVt89P1fqYifBVjrnzEE3nsghRM91m5UKDIXMLHwLyqigyE75Q9TyEK598UVDH56yzJ3X cp34ZP4bDpp4BWBQ94elWHc1crCv2ki4683/znegngOVxVAsjb6hXlaiOcME7XK4= X-Received: by 2002:a17:906:708d:: with SMTP id b13mr2373206ejk.120.1554357229934; Wed, 03 Apr 2019 22:53:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqziYehIVBTuSfEupJiu2iqtoMk2D17cw4Gah3GVmgqcS6ERNZYem1FyIk4EaceBdCB3+Gl+ X-Received: by 2002:a17:906:708d:: with SMTP id b13mr2373147ejk.120.1554357228618; Wed, 03 Apr 2019 22:53:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554357228; cv=none; d=google.com; s=arc-20160816; b=Qp94MN15TdajtqAVBB2uHz9b46zAmWkt6J45wvFieSwU+Kpv4gIyPao8CX8pLyvtZC BxmADhBvS5Cy53S5P64eZ7wTxUfHrbUeVPi2X5jLomGASfqHrooxIftoBYxQXmTH7OJq QmLg7kCymVWe7R3mtecVoVUbhoWoNkJzda367M/BBvL2/j2oeibxblrZj+mMPNApGHLC mwSRlso+9enP34tz8u+ZVmt0tJ/zYBiqU9J/5EvbTWHQ2YxJMYgcC3aGIABBjF2x9yQ4 4YSBzbnrM9iqEOJPQYevxTAJg4sVxaoc6uvtxMlSICn0e5VZGm3S/p2wXY/9UurNjreb 5NIg== 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=CwVLZGCT2pFidty3RGwl2qOplGjaAzcw2zp+WCWmDxU=; b=CL+uzFp+hFdVBHuPuPgLG+P9a49PxyLb0LaWcZpYOLiodAbxPKx6delWXcTBjW3XUy u9ejYjSPUCuHrY668qU7h/aq6HkBQF+Vc05v/S5aMyfplDzp3Yx0UBlTTY3TNb+dpXJg Wb3DzSOGoQNjDRfVl8BAxt5wJdqQUXWyHcqvb0YEqRo9ZMBTsd2HJDvcwukCgr+3uxPm UHL4N8Mg9twBj8I0kqUJY6knPM85lhXTit6m4QHXnFlCIOf9sORYjOOaRwByN1JTUb/q 5JrJy+PKMQ+ZEVsAMOh8pvmSF4VxIdpwO0AD7um4sxf+BOjq5BFvoJXUQx8ia7KR2pat ishQ== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net. [217.70.183.199]) by mx.google.com with ESMTPS id z10si2492906ejr.238.2019.04.03.22.53.48 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 03 Apr 2019 22:53:48 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.199; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay9-d.mail.gandi.net (Postfix) with ESMTPSA id 4E94BFF80F; Thu, 4 Apr 2019 05:53:44 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: 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, Alexandre Ghiti Subject: [PATCH v2 2/5] arm64, mm: Move generic mmap layout functions to mm Date: Thu, 4 Apr 2019 01:51:25 -0400 Message-Id: <20190404055128.24330-3-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190404055128.24330-1-alex@ghiti.fr> References: <20190404055128.24330-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 config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT that can be set by other architectures to benefit from those functions. Suggested-by: Christoph Hellwig Signed-off-by: Alexandre Ghiti --- arch/Kconfig | 8 ++++ arch/arm64/Kconfig | 1 + arch/arm64/include/asm/processor.h | 2 - arch/arm64/mm/mmap.c | 72 ---------------------------- kernel/sysctl.c | 6 ++- mm/util.c | 77 +++++++++++++++++++++++++++++- 6 files changed, 89 insertions(+), 77 deletions(-) diff --git a/arch/Kconfig b/arch/Kconfig index 33687dddd86a..ef8d0b50cc41 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -684,6 +684,14 @@ config HAVE_ARCH_COMPAT_MMAP_BASES and vice-versa 32-bit applications to call 64-bit mmap(). Required for applications doing different bitness syscalls. +config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT + bool + help + This allows to use a set of generic functions to determine mmap base + address by giving priority to top-down scheme only if the process + is not in legacy mode (compat task, unlimited stack size or + sysctl_legacy_va_layout). + config HAVE_COPY_THREAD_TLS bool help diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 7e34b9eba5de..670719a26b45 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -66,6 +66,7 @@ config ARM64 select ARCH_SUPPORTS_INT128 if GCC_VERSION >= 50000 || CC_IS_CLANG select ARCH_SUPPORTS_NUMA_BALANCING select ARCH_WANT_COMPAT_IPC_PARSE_VERSION + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT select ARCH_WANT_FRAME_POINTERS select ARCH_HAS_UBSAN_SANITIZE_ALL select ARM_AMBA diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h index 5d9ce62bdebd..4de2a2fd605a 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -274,8 +274,6 @@ static inline void spin_lock_prefetch(const void *ptr) "nop") : : "p" (ptr)); } -#define HAVE_ARCH_PICK_MMAP_LAYOUT - #endif extern unsigned long __ro_after_init signal_minsigstksz; /* sigframe size */ 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/kernel/sysctl.c b/kernel/sysctl.c index e5da394d1ca3..eb3414e78986 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(CONFIG_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(CONFIG_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 a54afb9b4faa..2027457ec30d 100644 --- a/mm/util.c +++ b/mm/util.c @@ -15,7 +15,12 @@ #include #include #include +#include +#include #include +#include +#include +#include #include @@ -313,13 +318,83 @@ unsigned long randomize_stack_top(unsigned long stack_top) #endif } -#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) +#ifdef CONFIG_MMU +#ifdef CONFIG_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 Thu Apr 4 05:51:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10884989 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 EAB22922 for ; Thu, 4 Apr 2019 05:54:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D40162848B for ; Thu, 4 Apr 2019 05:54:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C79FE28608; Thu, 4 Apr 2019 05:54:57 +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 36F8B2848B for ; Thu, 4 Apr 2019 05:54:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 770CD6B026A; Thu, 4 Apr 2019 01:54:56 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 71F756B026B; Thu, 4 Apr 2019 01:54:56 -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 5E8E46B026C; Thu, 4 Apr 2019 01:54:56 -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 1039D6B026A for ; Thu, 4 Apr 2019 01:54:56 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id e55so787079edd.6 for ; Wed, 03 Apr 2019 22:54:56 -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=sU9HZfgPaKNHBQ0fjctkOcrIiYmSQbuucDIbf7Melp0=; b=I46oebhWKH8S8DCvzQoZ2EaVdfIfuFZuHTn6t+em9Vjs+hYckXrX8gNF3/bkFv0EiO UAEAJU2YzPx+EO2jiJKrw9L1kQUTB/lN3l3ppzDfCtOwV3+yJrXWu2ynskFGFLTfkGie VIczG7JIKz7AnIHoOi/M0CXQ7pf21bW5+tdxHq/4YHkydi7MwPxU0pLZGUMRJ6izg2QF 4yUFk43GdmkdHhBfutn7VDISd5Gu2uEuL9OOt4kgVx4JwOIuiByoJ/oLvuGqdsawKGZL 3mtfwlj9X1MxUsYVEzBIQyq+ROlYFX5iQ3lKOjzfxi+M+HHk/Xd8Y/E0dqBCJCefxHMF afJw== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAUo3PcmsIokoPRLU9dMXe9h18qU2qSKmz0JZQ9XVYIU+KLylZvS vPUcTk8rVKgDeHYYnvX/v3TxtuGtRIEZycLijAf+gwf9WM379/CniWfhg7mG84mllkSM3hYXRzF HzqofGfWC8gIxRT1fTB5EGO1uFayJ4ZIPs5gcLDEaSlSfiW0GAa/4UY68KlGONaM= X-Received: by 2002:a17:906:6a12:: with SMTP id o18mr2370283ejr.204.1554357295566; Wed, 03 Apr 2019 22:54:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqzZvDMhfZ0f49oumu9w6etYeEph/3N0Mxq+ebAmKUxyqW9EKyqsI20msGquslRBZ/7zZeJ0 X-Received: by 2002:a17:906:6a12:: with SMTP id o18mr2370249ejr.204.1554357294601; Wed, 03 Apr 2019 22:54:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554357294; cv=none; d=google.com; s=arc-20160816; b=paC1XG6r+0YlRc/kJiwHtPjQVw3rVQvBKI6ThC88mEDg95ZBHTzRtmTefR/o+pN5lp x8parCGMDJVB9yHpsiWio63WAJxyPjkah2Tfl8EokM8eCaIzt0tMoexVivh41oR9GwTM eohNKeVHg+gccToqSMs5yX2b6f/c4o335P684amzlNcXudKfs2Pvf8DMg4k6NuKS8LrN hLlo/puyqtTN5SpVMRHXzjlU7BaZ1W2TwMeR2MqfLxtmF4u8FWhWHJpiE/GjPkzXBckl 4nEcjxPBM+PxBdpyepYElgbMYmD7fhj6ZjS5fIYXlUchONIG9zhdw99pMc++iJfBQ/BX r4bg== 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=sU9HZfgPaKNHBQ0fjctkOcrIiYmSQbuucDIbf7Melp0=; b=kF4+NiUyPrARPjtgyalrk8GU+pDfvtULW3TRInJl9IPV0hCAXTlMwp0unNqJXJNHiT tjB6ieCXTRk70gds6s8xSuBCoF8AVT7PJMVtOPj4WE19R5fKYLmeNA+fmD5FLrrX8VqI sb9Mj4XAxzXQgSYpvgLQ0bHcG+PCBOidgf/HQQIjHEhQJpe0nJgNvhG+Qm9LUKtY1fX1 EaXQ6N55AHOuGMtI0VrcoyHxoB7et9falw0GSm7QTqbTFWkYT/nkJo8GADINtSTvGtsu BMzE+A4njAd26/7ZeE4t+FpSWWTMiPK232RLnPn2VGh1G4g8tdh53Oc9flBhtowmzMWE bLxw== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net. [217.70.183.195]) by mx.google.com with ESMTPS id m1si1127128edb.138.2019.04.03.22.54.54 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 03 Apr 2019 22:54:54 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.195; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id BDCFF60007; Thu, 4 Apr 2019 05:54:48 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: 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, Alexandre Ghiti Subject: [PATCH v2 3/5] arm: Use generic mmap top-down layout Date: Thu, 4 Apr 2019 01:51:26 -0400 Message-Id: <20190404055128.24330-4-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190404055128.24330-1-alex@ghiti.fr> References: <20190404055128.24330-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/Kconfig | 1 + arch/arm/include/asm/processor.h | 2 -- arch/arm/mm/mmap.c | 52 -------------------------------- 3 files changed, 1 insertion(+), 54 deletions(-) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index 850b4805e2d1..747101a8e989 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -28,6 +28,7 @@ config ARM select ARCH_SUPPORTS_ATOMIC_RMW select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_CMPXCHG_LOCKREF + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT select ARCH_WANT_IPC_PARSE_VERSION select BUILDTIME_EXTABLE_SORT if MMU select CLONE_BACKWARDS diff --git a/arch/arm/include/asm/processor.h b/arch/arm/include/asm/processor.h index 57fe73ea0f72..944ef1fb1237 100644 --- a/arch/arm/include/asm/processor.h +++ b/arch/arm/include/asm/processor.h @@ -143,8 +143,6 @@ static inline void prefetchw(const void *ptr) #endif #endif -#define HAVE_ARCH_PICK_MMAP_LAYOUT - #endif #endif /* __ASM_ARM_PROCESSOR_H */ 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 Thu Apr 4 05:51:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10884999 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 4F5D41708 for ; Thu, 4 Apr 2019 05:56:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 373BD285C6 for ; Thu, 4 Apr 2019 05:56:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2A4D628A21; Thu, 4 Apr 2019 05:56:03 +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 96F8A285C6 for ; Thu, 4 Apr 2019 05:56:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACBAB6B026C; Thu, 4 Apr 2019 01:56:01 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A52AB6B026D; Thu, 4 Apr 2019 01:56:01 -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 91BF36B026E; Thu, 4 Apr 2019 01:56:01 -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 443916B026C for ; Thu, 4 Apr 2019 01:56:01 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id y17so768145edd.20 for ; Wed, 03 Apr 2019 22:56:01 -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=awYD9EzQwsHM4vOBJueyQeu5vHH60jnxNDLkfj3BPUs=; b=oOiOWWDU1ziJyWBoSzGejdkpQDv+fwnCkmKxxVmlBIl2z5pmoFiLjKTiRQeoT8YzNx +Wo+3vB3lAqNa2AaRHOSziyGhX32HY04n8kvx3q47csrZUIlRJAHvV/zadn98BT+g68B dCw1Vs8MFDj8ggc2otjF2fbiKWbOyr1vS5YgfLOgkfmhDWuBbVfq/RJI9xgIrrTMQ8OQ 4lcSV8WSLxg2CGoPe9JMfidCJRv5L6qNx75B+pvYtSvS6wfbbHwZJPMmaNqfITlwosU8 cZiauQ7p/P2TwuiYxAgnfcTustIm8ZftacU1YnxO4S5nCquj8Sxffp6N5FxXUgWnHEp0 R6XQ== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAVWjgVSs8njGXCNWXv0Zz49KcbqWzAn6HYUE3XFYG9uJgBcJ+HS 0j2hCF14iVuOKxbyCDHo/DBmgOrmNWWjmhxp30/NHnvDN3NrOarsr/NT8WMOYjSWBPw9CY4wYAQ o0McRH6+D7bcYKFx3i2FrObujhPHAZv+F+tQErg8AE7t8+izTVE3yOrlp9sjS510= X-Received: by 2002:aa7:dd0e:: with SMTP id i14mr2460480edv.172.1554357360791; Wed, 03 Apr 2019 22:56:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqwnRwUhiE5RCeAlW8Aa8aSbEthniUmw9URdjI/Qa+iquAOzfhDZsSMJE+bx9zDU2LdTB0Ek X-Received: by 2002:aa7:dd0e:: with SMTP id i14mr2460437edv.172.1554357359896; Wed, 03 Apr 2019 22:55:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554357359; cv=none; d=google.com; s=arc-20160816; b=K007QDXlbL4+vD+TgnJARffNwTRrJOQVR1RUk/8h/KTw580avlISQUI8sq9XnjBOG9 IZEu10V1BUyyyUOdm8upylbGIjph4H7lWosra1inoG5aghA2uwPnDYL1vYYCFSGijBsw OT+BlZaHQjvHWDOhXDl4VQHky3ZoEL6uy+meHaF5GJR0CrvOiEHOYqFOMXvUoQPLHHac oGjKR95VHKyWFaHrGudPTjJVyFf+ARCLoceAxyEPAjTQtoB6f18IySEfo0WkpOIYyBWL tqVF6Jtl0e5r3MXooZkSHOcZwzFMhr5MP7i5tnDThKAwDrcSAomF1Tg21D6k5uNkJgcO N+GQ== 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=awYD9EzQwsHM4vOBJueyQeu5vHH60jnxNDLkfj3BPUs=; b=HUG3uXZWb3wQ7Q3AK2VU1Uxb3kkIK22rXYqL10VHC04rWbqjJgTFz5mp4/cZDwg2iS IU5irvslMx0sSqxoJZ4qJnwGwnuCZxohidSq2hvr3hg1vvhuhiOB2IM5CxOiAqD4WALe TH/tgshKWhKnujqUEAeQNrWQ5FdrIHFZ9qEu6x7cKg3Lx39yiiFRe6QxLQStnjTwo2po imzyev//X2jmFxw/XeUqaX3D/G/nLHnhve1xTpaBazuxHivJ1Y8StMX7iOwTg+TRtIIK gMa2DZZAbZMrqu0dsb972VwV8qNIq/Y6THuXd9CtdQG0hv38oys/w0tMRauStGF1XQpM EqgA== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net. [217.70.183.195]) by mx.google.com with ESMTPS id q1si2299281ejs.275.2019.04.03.22.55.59 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 03 Apr 2019 22:55:59 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.195; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id BB81F60007; Thu, 4 Apr 2019 05:55:55 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: 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, Alexandre Ghiti Subject: [PATCH v2 4/5] mips: Use generic mmap top-down layout Date: Thu, 4 Apr 2019 01:51:27 -0400 Message-Id: <20190404055128.24330-5-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190404055128.24330-1-alex@ghiti.fr> References: <20190404055128.24330-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/Kconfig | 1 + arch/mips/include/asm/processor.h | 5 --- arch/mips/mm/mmap.c | 57 ------------------------------- 3 files changed, 1 insertion(+), 62 deletions(-) diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig index 4a5f5b0ee9a9..c21aa6371eab 100644 --- a/arch/mips/Kconfig +++ b/arch/mips/Kconfig @@ -14,6 +14,7 @@ config MIPS select ARCH_USE_CMPXCHG_LOCKREF if 64BIT select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKS + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT select ARCH_WANT_IPC_PARSE_VERSION select BUILDTIME_EXTABLE_SORT select CLONE_BACKWARDS diff --git a/arch/mips/include/asm/processor.h b/arch/mips/include/asm/processor.h index aca909bd7841..fba18d4a9190 100644 --- a/arch/mips/include/asm/processor.h +++ b/arch/mips/include/asm/processor.h @@ -29,11 +29,6 @@ extern unsigned int vced_count, vcei_count; -/* - * MIPS does have an arch_pick_mmap_layout() - */ -#define HAVE_ARCH_PICK_MMAP_LAYOUT 1 - #ifdef CONFIG_32BIT #ifdef CONFIG_KVM_GUEST /* User space process size is limited to 1GB in KVM Guest Mode */ 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 Thu Apr 4 05:51:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 10885009 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 18589922 for ; Thu, 4 Apr 2019 05:57:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F1C0F285C6 for ; Thu, 4 Apr 2019 05:57:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E223728A26; Thu, 4 Apr 2019 05:57:07 +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 5136C285C6 for ; Thu, 4 Apr 2019 05:57:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8CE3A6B0003; Thu, 4 Apr 2019 01:57:06 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 87C9A6B0006; Thu, 4 Apr 2019 01:57:06 -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 745FA6B026E; Thu, 4 Apr 2019 01:57:06 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by kanga.kvack.org (Postfix) with ESMTP id 26FFF6B0003 for ; Thu, 4 Apr 2019 01:57:06 -0400 (EDT) Received: by mail-ed1-f71.google.com with SMTP id w3so798832edt.2 for ; Wed, 03 Apr 2019 22:57:06 -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=Y+bi4NQArS1VzeWh7XXC5FNgs+8EM5PQ5CBJ928Rp7U=; b=E+fVkwaql1y6uheqQRgA5BqQcme+C+39BbqMg3RaklPG78kH/uWVGsWRD2UJA0H2ZK 9r0Qg1q0lsvhsVKLg5Gps7KIBBjB+G+UCCaWUomy09B9aGZW/D6eCiXd5qBuMyoKKW73 pYbA7FHUL1fSf+2JtSKv74jx8c4tSbxQpcRTzmHFzxHOiAPBAWfwdGSmgLu7ArHBQLHf 2+BhMjxbcqti7ptA099NGEdP9o9tvVt/RhtkyP+wT2D1XTpIogb3TBq4XdJWY3l+50Zs ZiUnimM4ids9+gZ+oXZnGUjV6IBz1EBEVjnyq1sTInRLmu+ketK4p4prs2TGkqWnvEjq jSuQ== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAXu05D/Hn267HjU0Y6RldbxScv9arssN6a760+qPEbN7QZC2Du2 Hzd4Jebm8zZ8FWBvnw6YvD72KZ5y3EOUgdjwG8b62g5IxGj8ig9KJSQidE2SoxcpC9p7nFC7k0U 9ADq3e+lM889qpsg3QGdu8vyDv2UQnQioW2QIs4dtQRBno9bGuOn6hNiS6gmtZCU= X-Received: by 2002:aa7:c803:: with SMTP id a3mr2454178edt.39.1554357425619; Wed, 03 Apr 2019 22:57:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqw0cZyrQfKpU9DzON8E+CDNVtj9QpP3x3XMrKAozscYLUVGRLLc/xcT2758hq2x1OPxRXcp X-Received: by 2002:aa7:c803:: with SMTP id a3mr2454122edt.39.1554357424762; Wed, 03 Apr 2019 22:57:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554357424; cv=none; d=google.com; s=arc-20160816; b=UHqKSz1vOZsKDIOFUdwtRrxUu5uozfrNSGls0wjZBe8K4W+oxqVn1KaGRP7HE+dFpJ UqPqDEMrIuNO5ByEznWeWhC7ZMTVheAlo1R5o543Va0xe+P1R8PI2dzYqMRNSquAx+rg eqt6BGqi4MduyetGDw+eivuQjz6j4uet6LqJ7SXWluAl2KevKLRxLRi2qNAFu1W3Oqm1 XycByctZgervlFlUxyWfNspfrOXUsekg+UW2NPf5qqtZustFqfmr4BPoqhOtcNopl5o5 ctcDNCGJD52+GqtA9UK8yoGgoGg1Nz4jeVUYmcW3OyoBo38cpDakUkB06TUu+jJQsmKq Mr5w== 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=Y+bi4NQArS1VzeWh7XXC5FNgs+8EM5PQ5CBJ928Rp7U=; b=r1AbBWi6j/gD3Y7eq2v9DnhVBRETBzjdAdL+AeVketHjChQfRUaxsZXSle8BLHnVkS q3CCZK20vljLeiulecgWKtVZKjs0KuBofHFZ9xr+8ptQgSo9iFVf95m3uX/6uj5XknlC g+rh+nHt+4giwbBIEJJvIku0gaOn6fQ5RFFBh/Nsz0KFsEjVth7KKtl5lCAptHFaSFfj nZF8i3mjYrtGYe/AsWPwvvUfuFrBhPI5DUGxImQnalBibL7RvGVc/E/OvI8MbJmVK1Sm Lm8hfIM0j+Gnpxr0dCCblcPE58J4Ng/2QIkArU029KqIUgRv73vTUwUT6gLljv6UHB+3 zfZA== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net. [217.70.183.200]) by mx.google.com with ESMTPS id f55si907239edb.217.2019.04.03.22.57.04 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 03 Apr 2019 22:57:04 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.200; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 144E02000D; Thu, 4 Apr 2019 05:56:59 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: 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, Alexandre Ghiti Subject: [PATCH v2 5/5] riscv: Make mmap allocation top-down by default Date: Thu, 4 Apr 2019 01:51:28 -0400 Message-Id: <20190404055128.24330-6-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190404055128.24330-1-alex@ghiti.fr> References: <20190404055128.24330-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000093, 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 Reviewed-by: Christoph Hellwig --- arch/riscv/Kconfig | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index eb56c82d8aa1..fe09f38ef9a9 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -49,6 +49,17 @@ config RISCV select GENERIC_IRQ_MULTI_HANDLER select ARCH_HAS_PTE_SPECIAL select HAVE_EBPF_JIT if 64BIT + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT + select HAVE_ARCH_MMAP_RND_BITS + +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