From patchwork Mon Feb 26 19:09:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13572723 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1386C5478C for ; Mon, 26 Feb 2024 19:10:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C8C9F4401B1; Mon, 26 Feb 2024 14:10:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C3B9D44017F; Mon, 26 Feb 2024 14:10:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A66B04401B1; Mon, 26 Feb 2024 14:10:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 93D4344017F for ; Mon, 26 Feb 2024 14:10:02 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 65158409B2 for ; Mon, 26 Feb 2024 19:10:02 +0000 (UTC) X-FDA: 81834895044.06.8B4E319 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf16.hostedemail.com (Postfix) with ESMTP id 7CA4E180017 for ; Mon, 26 Feb 2024 19:10:00 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=OSt65J8o; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708974600; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bL1SZY389qbnApMbft1l+2IEAzO4i9QutCx173tdjdc=; b=y9YlQTzAr5ryos4ARcVzg2pJXR68zLwRSidJ5n0WwMnHkUdNRhmVF1bVKA5WNz3v05cBya UWDWzSifi9yRP537EpRwh1DgFMsNthPp3xKbFSoFYfWqQbxEZui27WW6AXhJfvlXTXzX1o YWSAZbia8E2m72s5iCFgz13g2GIxp8Q= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=OSt65J8o; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974600; a=rsa-sha256; cv=none; b=GrlEa4PDXEqrPpmBeYgyygD1BCZqPczC8Mz1l7xBhu/C54aCEXAxt+67BRha9+05Rb+Ibz JzD9vPI9PyEaWb5cLgvaLvPRlnyt1Haemd3GMxIJrhf+MfmmuDjxt8+h5UeIKFtS9MiOEF JYccpMzd8gVY+GlVg4Rxr3v5DGuYGhA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974601; x=1740510601; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=g4JChMg3I76Dgv8nBf8Y40nYEQC6u5audbIsXOb5O2c=; b=OSt65J8o/mLCjWLV50JFxdnceCzWoNmE3zSNkT3Q7PPrNNLcwCi1XHTC Rjp8kNqSfaX2kMdws7BpybdwE/RgfMQw4zigRL8lNqv1r+GcFyU7TYzWH ArfGDzXDsGWKSqPti8YseP3xu3MxLPEkKEJyLKCCK5GHp+Vz8zqxUTnRo 8DN476lPLrR72MJfkc968T0ZbZxOcS/RXQqBMd8t9OyfQSP7XCo4TO/ow ICxS/2rdRpNvFn+/4Bun9UT4f8SAYJ8INMPx7HxPebkDxlLSt+Afj2xE2 5fXUSO2Q6qZr+EuGLmyrRMXmoko5GgovXxuth6Ii3lFjg6T+L8fqpWk4c w==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721335" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721335" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:09:58 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911442" Received: from bdmirand-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.3.213]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:09:57 -0800 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, kirill.shutemov@linux.intel.com, keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH v2 1/9] mm: Switch mm->get_unmapped_area() to a flag Date: Mon, 26 Feb 2024 11:09:43 -0800 Message-Id: <20240226190951.3240433-2-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> References: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: fnmmpb6epbbbftpuaktemkia5qrmz4ur X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 7CA4E180017 X-HE-Tag: 1708974600-505140 X-HE-Meta: U2FsdGVkX1+pIPlqCBjVmxre4Hu2aOBrN91v+vJtrdt0Jwfj40B62xmkgdIvpqKG8W4g9i1Cy7UFf0lGS6Yqgjgy3CyTHO72U9wTcxtm6XkBleeQq3qOhUNGQiOOAQNWshK96DvkCxq6dYFSejaq9XcJI7xtZjnjzhi3Ofi+rGZVxNVBwiyMzmb6BSLbSZ3IJCkiXuq6kpox7rAXLgiPUh47eVITQxPxWsGoJJBublbdXCtcX8okoBV+G7E6M/Kor2LToIq+T9hKw9yPMJrtyOzfWlakC+CQ0j/vFtZj3Jq/UMhoKiqL/ja/CUm5PGG4DP5M78WkTEe3iV/1wUi9+9Honq2tmqH17gQ0NdbKZOUanRlCllarc8kH7fD9ZTRVtg2U+KZJ0Uf5g+ckgoj2ZRWyVAXlB3vfRjgFGRZXJSUBNBNCkzOrouML5WbPnJEtI/IFEE9GLDZPVVlzMpn+x2ERV0/wYvuzOVEiXXgVOvMhpYbQ/hUWyv05grz7xXCz1FcztckIBdVxCj63L0z0TNb65JtA8FzD3bjnZW6SX45qZyx0SMmWEfa7U6VMy/++lh95sTmFs+IgiY/wlzUa2ghmm4fiGgHsm1BVBvMM0awSt8SUIFFUOeCBEGJ1odROg3MrDjzDIRqVCH2W8TPM+BpSMtVVOJVvis/X4uew6fGpcdFrknGuLRjXZ1NJ6Di/uHSQuUZ3j1LzzWtPmqrowm0J0Z/g5Hee9uDvYesDHynR069O4lW3FYfyDBmxhY0GI5bhb4spY1V0YLXyl81aQEM3Pc8uLCgvPywKpRhHknByGZWy1t94Q009imjZyP3zvXjx3qzWuhDsTAiPcCV0EJzLxm8sIzQDCxvO/sL7zqG+0/qqPMgEYksHydbVgnpqy/vWQAFfW/0ZJfy36Y2gLMwRk6wB7qiDVdOTrasGuAqqmR3H36dACw== 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: List-Subscribe: List-Unsubscribe: The mm_struct contains a function pointer *get_unmapped_area(), which is set to either arch_get_unmapped_area() or arch_get_unmapped_area_topdown() during the initialization of the mm. Since the function pointer only ever points to two functions that are named the same across all arch's, a function pointer is not really required. In addition future changes will want to add versions of the functions that take additional arguments. So to save a pointers worth of bytes in mm_struct, and prevent adding additional function pointers to mm_struct in future changes, remove it and keep the information about which get_unmapped_area() to use in a flag. Add the new flag to MMF_INIT_MASK so it doesn't get clobbered on fork by mmf_init_flags(). Most MM flags get clobbered on fork. In the pre-existing behavior mm->get_unmapped_area() would get copied to the new mm in dup_mm(), so not clobbering the flag preserves the existing behavior around inherriting the topdown-ness. Introduce a helper, mm_get_unmapped_area(), to easily convert code that refers to the old function pointer to instead select and call either arch_get_unmapped_area() or arch_get_unmapped_area_topdown() based on the flag. Then drop the mm->get_unmapped_area() function pointer. Leave the get_unmapped_area() pointer in struct file_operations alone. The main purpose of this change is to reorganize in preparation for future changes, but it also converts the calls of mm->get_unmapped_area() from indirect branches into a direct ones. The stress-ng bigheap benchmark calls realloc a lot, which calls through get_unmapped_area() in the kernel. On x86, the change yielded a ~1% improvement there on a retpoline config. In testing a few x86 configs, removing the pointer unfortunately didn't result in any actual size reductions in the compiled layout of mm_struct. But depending on compiler or arch alignment requirements, the change could shrink the size of mm_struct. Signed-off-by: Rick Edgecombe Acked-by: Dave Hansen Acked-by: Liam R. Howlett Reviewed-by: Kirill A. Shutemov --- v2: - Fix comment on MMF_TOPDOWN (Kirill, rppt) - Move MMF_TOPDOWN to actually unused bit - Add MMF_TOPDOWN to MMF_INIT_MASK so it doesn't get clobbered on fork, and result in the children using the search up path. - New lower performance results after above bug fix - Add Reviews and Acks --- arch/s390/mm/hugetlbpage.c | 2 +- arch/s390/mm/mmap.c | 4 ++-- arch/sparc/kernel/sys_sparc_64.c | 15 ++++++--------- arch/sparc/mm/hugetlbpage.c | 2 +- arch/x86/kernel/cpu/sgx/driver.c | 2 +- arch/x86/mm/hugetlbpage.c | 2 +- arch/x86/mm/mmap.c | 4 ++-- drivers/char/mem.c | 2 +- drivers/dax/device.c | 6 +++--- fs/hugetlbfs/inode.c | 2 +- fs/proc/inode.c | 15 ++++++++------- fs/ramfs/file-mmu.c | 2 +- include/linux/mm_types.h | 6 +----- include/linux/sched/coredump.h | 5 ++++- include/linux/sched/mm.h | 5 +++++ io_uring/io_uring.c | 2 +- mm/debug.c | 6 ------ mm/huge_memory.c | 6 +++--- mm/mmap.c | 21 ++++++++++++++++++--- mm/shmem.c | 11 +++++------ mm/util.c | 6 +++--- 21 files changed, 68 insertions(+), 58 deletions(-) diff --git a/arch/s390/mm/hugetlbpage.c b/arch/s390/mm/hugetlbpage.c index 297a6d897d5a..c2d2850ec8d5 100644 --- a/arch/s390/mm/hugetlbpage.c +++ b/arch/s390/mm/hugetlbpage.c @@ -328,7 +328,7 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, goto check_asce_limit; } - if (mm->get_unmapped_area == arch_get_unmapped_area) + if (!test_bit(MMF_TOPDOWN, &mm->flags)) addr = hugetlb_get_unmapped_area_bottomup(file, addr, len, pgoff, flags); else diff --git a/arch/s390/mm/mmap.c b/arch/s390/mm/mmap.c index fc9a7dc26c5e..cd52d72b59cf 100644 --- a/arch/s390/mm/mmap.c +++ b/arch/s390/mm/mmap.c @@ -182,10 +182,10 @@ void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) */ if (mmap_is_legacy(rlim_stack)) { mm->mmap_base = mmap_base_legacy(random_factor); - mm->get_unmapped_area = arch_get_unmapped_area; + clear_bit(MMF_TOPDOWN, &mm->flags); } else { mm->mmap_base = mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area = arch_get_unmapped_area_topdown; + set_bit(MMF_TOPDOWN, &mm->flags); } } diff --git a/arch/sparc/kernel/sys_sparc_64.c b/arch/sparc/kernel/sys_sparc_64.c index 1e9a9e016237..1dbf7211666e 100644 --- a/arch/sparc/kernel/sys_sparc_64.c +++ b/arch/sparc/kernel/sys_sparc_64.c @@ -218,14 +218,10 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, unsigned long len, unsigned long pgoff, unsigned long flags) { unsigned long align_goal, addr = -ENOMEM; - unsigned long (*get_area)(struct file *, unsigned long, - unsigned long, unsigned long, unsigned long); - - get_area = current->mm->get_unmapped_area; if (flags & MAP_FIXED) { /* Ok, don't mess with it. */ - return get_area(NULL, orig_addr, len, pgoff, flags); + return mm_get_unmapped_area(current->mm, NULL, orig_addr, len, pgoff, flags); } flags &= ~MAP_SHARED; @@ -238,7 +234,8 @@ unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, u align_goal = (64UL * 1024); do { - addr = get_area(NULL, orig_addr, len + (align_goal - PAGE_SIZE), pgoff, flags); + addr = mm_get_unmapped_area(current->mm, NULL, orig_addr, + len + (align_goal - PAGE_SIZE), pgoff, flags); if (!(addr & ~PAGE_MASK)) { addr = (addr + (align_goal - 1UL)) & ~(align_goal - 1UL); break; @@ -256,7 +253,7 @@ unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_addr, u * be obtained. */ if (addr & ~PAGE_MASK) - addr = get_area(NULL, orig_addr, len, pgoff, flags); + addr = mm_get_unmapped_area(current->mm, NULL, orig_addr, len, pgoff, flags); return addr; } @@ -292,7 +289,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) gap == RLIM_INFINITY || sysctl_legacy_va_layout) { mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area = arch_get_unmapped_area; + clear_bit(MMF_TOPDOWN, &mm->flags); } else { /* We know it's 32-bit */ unsigned long task_size = STACK_TOP32; @@ -303,7 +300,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) gap = (task_size / 6 * 5); mm->mmap_base = PAGE_ALIGN(task_size - gap - random_factor); - mm->get_unmapped_area = arch_get_unmapped_area_topdown; + set_bit(MMF_TOPDOWN, &mm->flags); } } diff --git a/arch/sparc/mm/hugetlbpage.c b/arch/sparc/mm/hugetlbpage.c index b432500c13a5..38a1bef47efb 100644 --- a/arch/sparc/mm/hugetlbpage.c +++ b/arch/sparc/mm/hugetlbpage.c @@ -123,7 +123,7 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr, (!vma || addr + len <= vm_start_gap(vma))) return addr; } - if (mm->get_unmapped_area == arch_get_unmapped_area) + if (!test_bit(MMF_TOPDOWN, &mm->flags)) return hugetlb_get_unmapped_area_bottomup(file, addr, len, pgoff, flags); else diff --git a/arch/x86/kernel/cpu/sgx/driver.c b/arch/x86/kernel/cpu/sgx/driver.c index 262f5fb18d74..22b65a5f5ec6 100644 --- a/arch/x86/kernel/cpu/sgx/driver.c +++ b/arch/x86/kernel/cpu/sgx/driver.c @@ -113,7 +113,7 @@ static unsigned long sgx_get_unmapped_area(struct file *file, if (flags & MAP_FIXED) return addr; - return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); + return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags); } #ifdef CONFIG_COMPAT diff --git a/arch/x86/mm/hugetlbpage.c b/arch/x86/mm/hugetlbpage.c index 5804bbae4f01..6d77c0039617 100644 --- a/arch/x86/mm/hugetlbpage.c +++ b/arch/x86/mm/hugetlbpage.c @@ -141,7 +141,7 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr, } get_unmapped_area: - if (mm->get_unmapped_area == arch_get_unmapped_area) + if (!test_bit(MMF_TOPDOWN, &mm->flags)) return hugetlb_get_unmapped_area_bottomup(file, addr, len, pgoff, flags); else diff --git a/arch/x86/mm/mmap.c b/arch/x86/mm/mmap.c index c90c20904a60..a2cabb1c81e1 100644 --- a/arch/x86/mm/mmap.c +++ b/arch/x86/mm/mmap.c @@ -129,9 +129,9 @@ static void arch_pick_mmap_base(unsigned long *base, unsigned long *legacy_base, void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) { if (mmap_is_legacy()) - mm->get_unmapped_area = arch_get_unmapped_area; + clear_bit(MMF_TOPDOWN, &mm->flags); else - mm->get_unmapped_area = arch_get_unmapped_area_topdown; + set_bit(MMF_TOPDOWN, &mm->flags); arch_pick_mmap_base(&mm->mmap_base, &mm->mmap_legacy_base, arch_rnd(mmap64_rnd_bits), task_size_64bit(0), diff --git a/drivers/char/mem.c b/drivers/char/mem.c index 3c6670cf905f..9b80e622ae80 100644 --- a/drivers/char/mem.c +++ b/drivers/char/mem.c @@ -544,7 +544,7 @@ static unsigned long get_unmapped_area_zero(struct file *file, } /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */ - return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); + return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags); #else return -ENOSYS; #endif diff --git a/drivers/dax/device.c b/drivers/dax/device.c index 93ebedc5ec8c..47c126d37b59 100644 --- a/drivers/dax/device.c +++ b/drivers/dax/device.c @@ -329,14 +329,14 @@ static unsigned long dax_get_unmapped_area(struct file *filp, if ((off + len_align) < off) goto out; - addr_align = current->mm->get_unmapped_area(filp, addr, len_align, - pgoff, flags); + addr_align = mm_get_unmapped_area(current->mm, filp, addr, len_align, + pgoff, flags); if (!IS_ERR_VALUE(addr_align)) { addr_align += (off - addr_align) & (align - 1); return addr_align; } out: - return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags); + return mm_get_unmapped_area(current->mm, filp, addr, len, pgoff, flags); } static const struct address_space_operations dev_dax_aops = { diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index f757d4f7ad98..a63d2eee086f 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -242,7 +242,7 @@ generic_hugetlb_get_unmapped_area(struct file *file, unsigned long addr, * If architectures have special needs, they should define their own * version of hugetlb_get_unmapped_area. */ - if (mm->get_unmapped_area == arch_get_unmapped_area_topdown) + if (test_bit(MMF_TOPDOWN, &mm->flags)) return hugetlb_get_unmapped_area_topdown(file, addr, len, pgoff, flags); return hugetlb_get_unmapped_area_bottomup(file, addr, len, diff --git a/fs/proc/inode.c b/fs/proc/inode.c index b33e490e3fd9..6f4c2e21e68f 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -454,15 +454,16 @@ pde_get_unmapped_area(struct proc_dir_entry *pde, struct file *file, unsigned lo unsigned long len, unsigned long pgoff, unsigned long flags) { - typeof_member(struct proc_ops, proc_get_unmapped_area) get_area; - - get_area = pde->proc_ops->proc_get_unmapped_area; + if (pde->proc_ops->proc_get_unmapped_area) + return pde->proc_ops->proc_get_unmapped_area(file, orig_addr, + len, pgoff, + flags); #ifdef CONFIG_MMU - if (!get_area) - get_area = current->mm->get_unmapped_area; + else + return mm_get_unmapped_area(current->mm, file, orig_addr, + len, pgoff, flags); #endif - if (get_area) - return get_area(file, orig_addr, len, pgoff, flags); + return orig_addr; } diff --git a/fs/ramfs/file-mmu.c b/fs/ramfs/file-mmu.c index c7a1aa3c882b..b45c7edc3225 100644 --- a/fs/ramfs/file-mmu.c +++ b/fs/ramfs/file-mmu.c @@ -35,7 +35,7 @@ static unsigned long ramfs_mmu_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { - return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); + return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags); } const struct file_operations ramfs_file_operations = { diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 957ce38768b2..f01c01b4a4fc 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -745,11 +745,7 @@ struct mm_struct { } ____cacheline_aligned_in_smp; struct maple_tree mm_mt; -#ifdef CONFIG_MMU - unsigned long (*get_unmapped_area) (struct file *filp, - unsigned long addr, unsigned long len, - unsigned long pgoff, unsigned long flags); -#endif + unsigned long mmap_base; /* base of mmap area */ unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */ #ifdef CONFIG_HAVE_ARCH_COMPAT_MMAP_BASES diff --git a/include/linux/sched/coredump.h b/include/linux/sched/coredump.h index 02f5090ffea2..e62ff805cfc9 100644 --- a/include/linux/sched/coredump.h +++ b/include/linux/sched/coredump.h @@ -92,9 +92,12 @@ static inline int get_dumpable(struct mm_struct *mm) #define MMF_VM_MERGE_ANY 30 #define MMF_VM_MERGE_ANY_MASK (1 << MMF_VM_MERGE_ANY) +#define MMF_TOPDOWN 31 /* mm searches top down by default */ +#define MMF_TOPDOWN_MASK (1 << MMF_TOPDOWN) + #define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\ MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\ - MMF_VM_MERGE_ANY_MASK) + MMF_VM_MERGE_ANY_MASK | MMF_TOPDOWN_MASK) static inline unsigned long mmf_init_flags(unsigned long flags) { diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index 9a19f1b42f64..cde946e926d8 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -8,6 +8,7 @@ #include #include #include +#include /* * Routines for handling mm_structs @@ -186,6 +187,10 @@ arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags); +unsigned long mm_get_unmapped_area(struct mm_struct *mm, struct file *filp, + unsigned long addr, unsigned long len, + unsigned long pgoff, unsigned long flags); + unsigned long generic_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 9626a363f121..b3c4ec115989 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -3569,7 +3569,7 @@ static unsigned long io_uring_mmu_get_unmapped_area(struct file *filp, #else addr = 0UL; #endif - return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags); + return mm_get_unmapped_area(current->mm, filp, addr, len, pgoff, flags); } #else /* !CONFIG_MMU */ diff --git a/mm/debug.c b/mm/debug.c index ee533a5ceb79..32db5de8e1e7 100644 --- a/mm/debug.c +++ b/mm/debug.c @@ -162,9 +162,6 @@ EXPORT_SYMBOL(dump_vma); void dump_mm(const struct mm_struct *mm) { pr_emerg("mm %px task_size %lu\n" -#ifdef CONFIG_MMU - "get_unmapped_area %px\n" -#endif "mmap_base %lu mmap_legacy_base %lu\n" "pgd %px mm_users %d mm_count %d pgtables_bytes %lu map_count %d\n" "hiwater_rss %lx hiwater_vm %lx total_vm %lx locked_vm %lx\n" @@ -190,9 +187,6 @@ void dump_mm(const struct mm_struct *mm) "def_flags: %#lx(%pGv)\n", mm, mm->task_size, -#ifdef CONFIG_MMU - mm->get_unmapped_area, -#endif mm->mmap_base, mm->mmap_legacy_base, mm->pgd, atomic_read(&mm->mm_users), atomic_read(&mm->mm_count), diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 86ee29b5c39c..e9ef43a719a5 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -641,8 +641,8 @@ static unsigned long __thp_get_unmapped_area(struct file *filp, if (len_pad < len || (off + len_pad) < off) return 0; - ret = current->mm->get_unmapped_area(filp, addr, len_pad, - off >> PAGE_SHIFT, flags); + ret = mm_get_unmapped_area(current->mm, filp, addr, len_pad, + off >> PAGE_SHIFT, flags); /* * The failure might be due to length padding. The caller will retry @@ -672,7 +672,7 @@ unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, if (ret) return ret; - return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags); + return mm_get_unmapped_area(current->mm, filp, addr, len, pgoff, flags); } EXPORT_SYMBOL_GPL(thp_get_unmapped_area); diff --git a/mm/mmap.c b/mm/mmap.c index aa82eec17489..b61bc515c729 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1807,7 +1807,8 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { unsigned long (*get_area)(struct file *, unsigned long, - unsigned long, unsigned long, unsigned long); + unsigned long, unsigned long, unsigned long) + = NULL; unsigned long error = arch_mmap_check(addr, len, flags); if (error) @@ -1817,7 +1818,6 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, if (len > TASK_SIZE) return -ENOMEM; - get_area = current->mm->get_unmapped_area; if (file) { if (file->f_op->get_unmapped_area) get_area = file->f_op->get_unmapped_area; @@ -1834,7 +1834,11 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, get_area = thp_get_unmapped_area; } - addr = get_area(file, addr, len, pgoff, flags); + if (get_area) + addr = get_area(file, addr, len, pgoff, flags); + else + addr = mm_get_unmapped_area(current->mm, file, addr, len, + pgoff, flags); if (IS_ERR_VALUE(addr)) return addr; @@ -1849,6 +1853,17 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, EXPORT_SYMBOL(get_unmapped_area); +unsigned long +mm_get_unmapped_area(struct mm_struct *mm, struct file *file, + unsigned long addr, unsigned long len, + unsigned long pgoff, unsigned long flags) +{ + if (test_bit(MMF_TOPDOWN, &mm->flags)) + return arch_get_unmapped_area_topdown(file, addr, len, pgoff, flags); + return arch_get_unmapped_area(file, addr, len, pgoff, flags); +} +EXPORT_SYMBOL(mm_get_unmapped_area); + /** * find_vma_intersection() - Look up the first VMA which intersects the interval * @mm: The process address space. diff --git a/mm/shmem.c b/mm/shmem.c index 0d1ce70bce38..a8cac92dff88 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2242,8 +2242,6 @@ unsigned long shmem_get_unmapped_area(struct file *file, unsigned long uaddr, unsigned long len, unsigned long pgoff, unsigned long flags) { - unsigned long (*get_area)(struct file *, - unsigned long, unsigned long, unsigned long, unsigned long); unsigned long addr; unsigned long offset; unsigned long inflated_len; @@ -2253,8 +2251,8 @@ unsigned long shmem_get_unmapped_area(struct file *file, if (len > TASK_SIZE) return -ENOMEM; - get_area = current->mm->get_unmapped_area; - addr = get_area(file, uaddr, len, pgoff, flags); + addr = mm_get_unmapped_area(current->mm, file, uaddr, len, pgoff, + flags); if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) return addr; @@ -2311,7 +2309,8 @@ unsigned long shmem_get_unmapped_area(struct file *file, if (inflated_len < len) return addr; - inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags); + inflated_addr = mm_get_unmapped_area(current->mm, NULL, uaddr, + inflated_len, 0, flags); if (IS_ERR_VALUE(inflated_addr)) return addr; if (inflated_addr & ~PAGE_MASK) @@ -4757,7 +4756,7 @@ unsigned long shmem_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { - return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); + return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags); } #endif diff --git a/mm/util.c b/mm/util.c index 744b4d7e3fae..10c836a75e66 100644 --- a/mm/util.c +++ b/mm/util.c @@ -452,17 +452,17 @@ void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) if (mmap_is_legacy(rlim_stack)) { mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area = arch_get_unmapped_area; + clear_bit(MMF_TOPDOWN, &mm->flags); } else { mm->mmap_base = mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area = arch_get_unmapped_area_topdown; + set_bit(MMF_TOPDOWN, &mm->flags); } } #elif defined(CONFIG_MMU) && !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; + clear_bit(MMF_TOPDOWN, &mm->flags); } #endif From patchwork Mon Feb 26 19:09:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13572724 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24AA8C48BF6 for ; Mon, 26 Feb 2024 19:10:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 38B994401B2; Mon, 26 Feb 2024 14:10:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 33B2644017F; Mon, 26 Feb 2024 14:10:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B4234401B2; Mon, 26 Feb 2024 14:10:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0A35B44017F for ; Mon, 26 Feb 2024 14:10:04 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D934A1209BF for ; Mon, 26 Feb 2024 19:10:03 +0000 (UTC) X-FDA: 81834895086.20.E11B7A0 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf09.hostedemail.com (Postfix) with ESMTP id E3985140010 for ; Mon, 26 Feb 2024 19:10:01 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GI3FDB7A; spf=pass (imf09.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974602; a=rsa-sha256; cv=none; b=w7CjrSsnyXFo0vCieQdN0zZ/49uePfQWVAnkDnLDKPlOBtoEaDY3EtmjOFfal5bF+9zf7J oFdBZJtoGzOSHAxxiuExlApyeN8o3YANuIhdhJowMMUeGlH8ueqByDyYakAF5UUMMDVlrn cJ3E+raIOJskLa7BEqxVlpIZDeQ9yBA= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GI3FDB7A; spf=pass (imf09.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708974602; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GJ3WV1MyMnvFy0laLvbLjyXhu5lENNpTuhfkEsO4ulk=; b=nQ6sp/644xU/V6SyqNaGcrFv1jst2cEiqLiX3Z9z47rtq8988lcRRHckfolBpEFTtIj+du kSjmtqP+UsAE0rQv6xaZBI8nLIl0orquHzZsO6V95ZDg3yhj1UKzRTVoOeu9U3Af7t59c+ PpEo2BUnANaab5xHdVnpPz9e/jQE4QE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974602; x=1740510602; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NmgdWGUKdUuPH56dpQmxlcj6RB+edyhe9JvhgeXosrc=; b=GI3FDB7A4L7loxuFlkw3GSYt4xkL1fDlK+Kp8LsnDxd/3VAjGa/JwjDD JcswTHKH6CBPkMGgNMflyRToWMgmlqR6fLqIdqA/EnVdWFe7I9x1kCNyx Rj/zlsXq7lffJ6FHPe1hLobNeky8C68U+abakvVjES3jjnIpKRttczKWE 1ZoFRKNyyy1j+D/lAUYOMf7HXn+/YwxRQRHmzKfGZ2HOmMof8+s3K76vl 9hZqU0DvPL4T8vp9GXPFECDe/GmPymQfwmzVZw9LxNMaSMPEhJUhsCLed +oexXymWJEwYZA84YFNIENy7TR1XB/0yRNt+Vxtakf8/cdz++ZSuZjyfs w==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721346" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721346" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:09:59 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911445" Received: from bdmirand-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.3.213]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:09:58 -0800 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, kirill.shutemov@linux.intel.com, keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH v2 2/9] mm: Introduce arch_get_unmapped_area_vmflags() Date: Mon, 26 Feb 2024 11:09:44 -0800 Message-Id: <20240226190951.3240433-3-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> References: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E3985140010 X-Stat-Signature: sumewo9uakbx5xwhyd91ty7a4x3gtret X-Rspam-User: X-HE-Tag: 1708974601-752103 X-HE-Meta: U2FsdGVkX1/hB13eCsXx00V3H8x8jeKytemyFH0LcZIMY0Cc7xoiqHALYxY5NqnY9WJ6mkvwUsZzqhbBAqMs8KF8dHd0lp7xhc3NblHDfJFx8pZEz47dnA9/3IuRebHUnfIxzFA6yb7lLzy1Qm35u3zybESJnMmJ7RlPnX0rqoKY5h3BAfc4cXGJ2u4DLmjJWj3XorctmYtyVxIHJiglpgsaD6pOgQ9JPbBAoCeGRU+yC1A0tz/btLOZk/6hKW5MENiRfjq0S4TSn/evd6AgeuVN1kSHVtBNKKzxLXM1e5jSLR1nLvBIuq8J9emLJr+29VNBM43GhEw68G8EnvxbfbkkyCLGsEJ6R8Q8c+Jpo6iF8b1Mf/mrCEa2+Y3hTYPhh37q5s1J9gMMklqXO4FtUXD/458wZ3udwp/hyjNEQ9P6bKePyu/ZIIMQwaCRhQVTjPty7/9Xb22Qobz04Kjfmzhh7KV8UoQJ5C1QZHyvrnzUH5alDOA9P35n25LvA2MQfvl7zVVLAcqJa/ecOROjSpsw2ezqXufxXnslw7iO+nMYQZu5ZEQfv9Fsi7QkOlscXHI2JuU5rJ4qg4NMflmww6gykvxM/8GNmYhKwroHSrTQcXehBCjYd3LXLwPOMpeB5qXIBlWSTJi2ro9dYgNVuz6MTU4UggpcZwLq/iHQI5lOHvFawm7fjUUfhfyT0/5lCGLQqm55Qyz57ZqWV9Ir85H6ThzelPgvvQm/61+MX2vstFisx3OYwxUcUgBo+fIB5gBKWJTza7S0mxcQDRx8G9jvz6Dtb+AlhgDbica3MBmmYErU+0hWDrIeeDKTNEvkUdW+PaUhHR41Q/JE5Qz6UyC/heduigweeemYV8Vf5gLpkPHspioSPRCcvgldYsbhRn3OU/75OeM= 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: List-Subscribe: List-Unsubscribe: When memory is being placed, mmap() will take care to respect the guard gaps of certain types of memory (VM_SHADOWSTACK, VM_GROWSUP and VM_GROWSDOWN). In order to ensure guard gaps between mappings, mmap() needs to consider two things: 1. That the new mapping isn’t placed in an any existing mappings guard gaps. 2. That the new mapping isn’t placed such that any existing mappings are not in *its* guard gaps. The long standing behavior of mmap() is to ensure 1, but not take any care around 2. So for example, if there is a PAGE_SIZE free area, and a mmap() with a PAGE_SIZE size, and a type that has a guard gap is being placed, mmap() may place the shadow stack in the PAGE_SIZE free area. Then the mapping that is supposed to have a guard gap will not have a gap to the adjacent VMA. In order to take the start gap into account, the maple tree search needs to know the size of start gap the new mapping will need. The call chain from do_mmap() to the actual maple tree search looks like this: do_mmap(size, vm_flags, map_flags, ..) mm/mmap.c:get_unmapped_area(size, map_flags, ...) arch_get_unmapped_area(size, map_flags, ...) vm_unmapped_area(struct vm_unmapped_area_info) One option would be to add another MAP_ flag to mean a one page start gap (as is for shadow stack), but this consumes a flag unnecessarily. Another option could be to simply increase the size passed in do_mmap() by the start gap size, and adjust after the fact, but this will interfere with the alignment requirements passed in struct vm_unmapped_area_info, and unknown to mmap.c. Instead, introduce variants of arch_get_unmapped_area/_topdown() that take vm_flags. In future changes, these variants can be used in mmap.c:get_unmapped_area() to allow the vm_flags to be passed through to vm_unmapped_area(), while preserving the normal arch_get_unmapped_area/_topdown() for the existing callers. Signed-off-by: Rick Edgecombe --- include/linux/sched/mm.h | 17 +++++++++++++++++ mm/mmap.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index cde946e926d8..7b44441865c5 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -191,6 +191,23 @@ unsigned long mm_get_unmapped_area(struct mm_struct *mm, struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags); +extern unsigned long +arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, + unsigned long flags, vm_flags_t vm_flags); +extern unsigned long +arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, + unsigned long flags, vm_flags_t); + +unsigned long mm_get_unmapped_area_vmflags(struct mm_struct *mm, + struct file *filp, + unsigned long addr, + unsigned long len, + unsigned long pgoff, + unsigned long flags, + vm_flags_t vm_flags); + unsigned long generic_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, diff --git a/mm/mmap.c b/mm/mmap.c index b61bc515c729..2021bc040e81 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1802,6 +1802,34 @@ arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr, } #endif +#ifndef HAVE_ARCH_UNMAPPED_AREA_VMFLAGS +extern unsigned long +arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, unsigned long len, + unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) +{ + return arch_get_unmapped_area(filp, addr, len, pgoff, flags); +} + +extern unsigned long +arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, + unsigned long flags, vm_flags_t vm_flags) +{ + return arch_get_unmapped_area_topdown(filp, addr, len, pgoff, flags); +} +#endif + +unsigned long mm_get_unmapped_area_vmflags(struct mm_struct *mm, struct file *filp, + unsigned long addr, unsigned long len, + unsigned long pgoff, unsigned long flags, + vm_flags_t vm_flags) +{ + if (test_bit(MMF_TOPDOWN, &mm->flags)) + return arch_get_unmapped_area_topdown_vmflags(filp, addr, len, pgoff, + flags, vm_flags); + return arch_get_unmapped_area_vmflags(filp, addr, len, pgoff, flags, vm_flags); +} + unsigned long get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) From patchwork Mon Feb 26 19:09:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13572725 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5CFFCC5478C for ; Mon, 26 Feb 2024 19:10:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 909FA4401B3; Mon, 26 Feb 2024 14:10:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 86A7D44017F; Mon, 26 Feb 2024 14:10:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E3A74401B3; Mon, 26 Feb 2024 14:10:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5C49944017F for ; Mon, 26 Feb 2024 14:10:04 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 14C16C07F6 for ; Mon, 26 Feb 2024 19:10:04 +0000 (UTC) X-FDA: 81834895128.19.439064A Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf07.hostedemail.com (Postfix) with ESMTP id 18CDC40008 for ; Mon, 26 Feb 2024 19:10:01 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=eT3Gcc7I; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708974602; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5NbTiyI+8TCmJc9FYOl/qFJ5qCvsFXa+XT0LadSrGrk=; b=T2bwEVw7LpCyJtnc011Tf2RBy6lakNoTurn2BB/K1ZJ6LebkhqbpnBwEaIn9l778bMeWZH d9jooW9FZ2KhsHzibbd2m5LlOka03EYt2CRdBNQ8/bMo4Gqfu0k2XKxzAh2RdkinHSS4oF rRY3RclbS8RdcXtONFKKdRcPLk+cx50= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974602; a=rsa-sha256; cv=none; b=v7RpSra6rzVOmnecMkh0xaThMWwDY5GMCAcbVOLufDrpyZrQ1eYqCpIF1TP+34soJFymSf gHRNALu0II+aMXvdPg0x67s7+53AYsQMcpJ33twPE32R/6wJsDPXdMzNmWqu3yEGrWss63 3c1HKf8eCuf/SG6Zi6vv3s9ALL8RzhU= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=eT3Gcc7I; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974603; x=1740510603; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UpMkpaIresTqtixl68LlSa8NKJ54JsLLMvQyZqHlQM8=; b=eT3Gcc7I1HDGt1Qf52wPJ5QIYdbtgcgdkdHD/pfFSZTSQZFPF9Pdzezy bcTGfRbx1vrlRHU12DSKAtXOT2wBH80ZkJzqyeZiJ0L364RyUgxjuOedM La1jmQsh3U7yonDxTElxyI9zx+JL7pbZ8Cfnk7r/xEpD35pNBHeAsJZ7j WVWdVyVW3Xo2K7r1mxtV8JZ8lrEHSH1Ekz9hZU3TiyJntff8+8qzWePG0 fsIM9p85bfVXt8h5qcw4Gu0WGsdBReQSr0kuUTsbUTAcgxYoEaXQlXqn/ POFq//E4D/w03LnNkPxpcwQT1kM+8GcM8SppkP59DigWH19/APfVdpPN+ Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721361" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721361" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:00 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911448" Received: from bdmirand-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.3.213]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:09:58 -0800 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, kirill.shutemov@linux.intel.com, keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH v2 3/9] mm: Use get_unmapped_area_vmflags() Date: Mon, 26 Feb 2024 11:09:45 -0800 Message-Id: <20240226190951.3240433-4-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> References: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 18CDC40008 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 4zm4pg19ipcxbzo4cwxxpk6by4hgahit X-HE-Tag: 1708974601-671512 X-HE-Meta: U2FsdGVkX1/MANv5wJ97Qovuiw3EPtGYi6squKaMfrFtwss76Mv4UfN2vikV+z8OSU4+N9LZCgIySbmmsAm8HBHCWEDg7J0ApdhbjI77AgArOHfTRTKfbH1tppuQfRfIPAG3YbZeq/IHfbF9g86z0/VzwaPxVVWSkM/G+6G1ij4PDcgY1sRkO9P4H44gdV9DSwPwILh6TCaS57XnAbRCOx2svZRWRiq5n0DweEz/UO9pkajr6W+aOfUJdhMGYTluAM0ymqeY7KWfSicuiH/qBKmob7eXrapakdqfUFBZq79KLXUglcPup2Ye3LbQBDisOB+qDBrE6jy3z9EZdwByXdPMPs9ojsTfRO/X8yCF0Jdr57ZXNh8SRdkaTZGYoh1p1PJho+uLrhkIOFmtwYF2QohH+M2j+aca+sxPIvPo3qxgLFYqYjtq+zrQ593yelOhe9aGUlpd/Bn1AmuGb3JR22e2E1RXJW5rUze7QKZnXFILqmx5t0o534zKCI5nQrRuVVvEhaSWHbvVhkbK0HLcMBJBgIIihfdUNYD8nYEGG0sd859ZOn+iMPKVQQE+F1TDkZ/1XP0t2EKnXZwrsUlIAICNx0piX7tvi1cHoIw+vijkl4pVyQ2rKJyDWz/DHsJ37b/+/KvUC7r4oMaZ7Rj671TVyFLNvBxId9GAZxz1QQdmqWsJ41KA6sEnJyB4GHFfzMrccZhi30RotBfOOnu13aUUTmL2L8Qun1mxg/F6T4+jehaGuXRH6GguM5F+xVU2nVLe1XQfiflAIeXIYkfMC+z7ylhvyZgdHIk51Bgca1NiL5ojtjssC1bCzl/KxoOm+nmifWtts8WO9uIBdAdoKvTr/8CqZYRl36p2VXejNPP2c+AAqjvMSdkdfXJVk7ks1uZdhGZXkKo= 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: List-Subscribe: List-Unsubscribe: When memory is being placed, mmap() will take care to respect the guard gaps of certain types of memory (VM_SHADOWSTACK, VM_GROWSUP and VM_GROWSDOWN). In order to ensure guard gaps between mappings, mmap() needs to consider two things: 1. That the new mapping isn’t placed in an any existing mappings guard gaps. 2. That the new mapping isn’t placed such that any existing mappings are not in *its* guard gaps. The long standing behavior of mmap() is to ensure 1, but not take any care around 2. So for example, if there is a PAGE_SIZE free area, and a mmap() with a PAGE_SIZE size, and a type that has a guard gap is being placed, mmap() may place the shadow stack in the PAGE_SIZE free area. Then the mapping that is supposed to have a guard gap will not have a gap to the adjacent VMA. Use mm_get_unmapped_area_vmflags() in the do_mmap() so future changes can cause shadow stack mappings to be placed with a guard gap. Also use the THP variant that takes vm_flags, such that THP shadow stack can get the same treatment. Adjust the vm_flags calculation to happen earlier so that the vm_flags can be passed into __get_unmapped_area(). Signed-off-by: Rick Edgecombe --- v2: - Make get_unmapped_area() a static inline (Kirill) --- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 34 ++++++++++++++++------------------ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index da5219b48d52..a091181ef65a 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3351,7 +3351,16 @@ extern int install_special_mapping(struct mm_struct *mm, unsigned long randomize_stack_top(unsigned long stack_top); unsigned long randomize_page(unsigned long start, unsigned long range); -extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); +unsigned long +__get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, + unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags); + +static inline unsigned long +get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, + unsigned long pgoff, unsigned long flags) +{ + return __get_unmapped_area(file, addr, len, pgoff, flags, 0); +} extern unsigned long mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, diff --git a/mm/mmap.c b/mm/mmap.c index 2021bc040e81..ac7601d05e89 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1249,18 +1249,6 @@ unsigned long do_mmap(struct file *file, unsigned long addr, if (mm->map_count > sysctl_max_map_count) return -ENOMEM; - /* Obtain the address to map to. we verify (or select) it and ensure - * that it represents a valid section of the address space. - */ - addr = get_unmapped_area(file, addr, len, pgoff, flags); - if (IS_ERR_VALUE(addr)) - return addr; - - if (flags & MAP_FIXED_NOREPLACE) { - if (find_vma_intersection(mm, addr, addr + len)) - return -EEXIST; - } - if (prot == PROT_EXEC) { pkey = execute_only_pkey(mm); if (pkey < 0) @@ -1274,6 +1262,18 @@ unsigned long do_mmap(struct file *file, unsigned long addr, vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; + /* Obtain the address to map to. we verify (or select) it and ensure + * that it represents a valid section of the address space. + */ + addr = __get_unmapped_area(file, addr, len, pgoff, flags, vm_flags); + if (IS_ERR_VALUE(addr)) + return addr; + + if (flags & MAP_FIXED_NOREPLACE) { + if (find_vma_intersection(mm, addr, addr + len)) + return -EEXIST; + } + if (flags & MAP_LOCKED) if (!can_do_mlock()) return -EPERM; @@ -1831,8 +1831,8 @@ unsigned long mm_get_unmapped_area_vmflags(struct mm_struct *mm, struct file *fi } unsigned long -get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, - unsigned long pgoff, unsigned long flags) +__get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, + unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) { unsigned long (*get_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long) @@ -1865,8 +1865,8 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, if (get_area) addr = get_area(file, addr, len, pgoff, flags); else - addr = mm_get_unmapped_area(current->mm, file, addr, len, - pgoff, flags); + addr = mm_get_unmapped_area_vmflags(current->mm, file, addr, len, + pgoff, flags, vm_flags); if (IS_ERR_VALUE(addr)) return addr; @@ -1879,8 +1879,6 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, return error ? error : addr; } -EXPORT_SYMBOL(get_unmapped_area); - unsigned long mm_get_unmapped_area(struct mm_struct *mm, struct file *file, unsigned long addr, unsigned long len, From patchwork Mon Feb 26 19:09:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13572726 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2EE29C48BF6 for ; Mon, 26 Feb 2024 19:10:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 032394401B4; Mon, 26 Feb 2024 14:10:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EFA6A44017F; Mon, 26 Feb 2024 14:10:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D77664401B4; Mon, 26 Feb 2024 14:10:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C7D2B44017F for ; Mon, 26 Feb 2024 14:10:04 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 9451A140970 for ; Mon, 26 Feb 2024 19:10:04 +0000 (UTC) X-FDA: 81834895128.15.B0B6BB6 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf16.hostedemail.com (Postfix) with ESMTP id A35C018000D for ; Mon, 26 Feb 2024 19:10:02 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cBElWRVi; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708974602; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=w9uxx0p4IqyzsB+rWsNbj/igvIL8fkPgh5yQf8P8jHg=; b=HSAFOCz+AowEY2BmE7cwKtEzZurDznzTS1cn4HQcpMxZXM3HG8s45FG+cHHEvhDMitf3pf X7mUzYWjdBpZwvYhKYKxI9esxsZKoF6O1lk+s7p3RMmsZE/8gpKuu6CUIe3NPMs293yzq8 e8M9m1PHCXKJD1X26IaKGtjZY8sQqBw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cBElWRVi; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974602; a=rsa-sha256; cv=none; b=cI8FA5TEaDsxBG8jnl1eI0vGfED3a57uaR4eC+ABMlEHF5XyO3ENP0Y3fQqcYgc6HAnlyk FmgGNgEPhh1/J68dKbPEVWmMyNwnSaLWEP3T6OmZ1dEdgEN4EDDgboAAOfEl7dz5bicrSX rOC6tM1UQkFgFtzAiRDyQsJY26A2+No= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974603; x=1740510603; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=P5XlQHAG5P20JDtEHex7uiurCNEBN1oum2HZdu24mXg=; b=cBElWRVinfatkFy7iksgb9z1+I/4cCNbPIOke0pDkZvucoFbc0ss+czX SoQsaiF0gikkekP13zZYy9V/MtiXRgKYE7KeOjZ5GgLqoqv4eTUwZZpna gEIDAI2ql58lFHYScsvxLmsoalJwduoFPh/5cS2pjF5UJWVtMa+C5Wbkc e8Oqummu9Hd7jMzFWE+pNEsY7NtlQxVterqoH+EMT5sKIyKesx0uomKeI W/cBBIBr6cO64x/9t79rlG6zauAIYQgDtW9NT214XaRqgT3GRtJuCHryQ PTcK6nXJRJ814sd+AveoF5xLM/02pZghGHquUKKRkHXigSzo7BvvhbLCZ w==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721375" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721375" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:01 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911452" Received: from bdmirand-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.3.213]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:09:59 -0800 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, kirill.shutemov@linux.intel.com, keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH v2 4/9] thp: Add thp_get_unmapped_area_vmflags() Date: Mon, 26 Feb 2024 11:09:46 -0800 Message-Id: <20240226190951.3240433-5-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> References: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: fzpp8g4ksn3qz71cd5pi5j4rzup6rg84 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A35C018000D X-HE-Tag: 1708974602-409473 X-HE-Meta: U2FsdGVkX19GzYMdfIGUylllaW+uIH/Vfjucw7hM/hB8Vnzm0JA/1M5lan+KNrAlJuUYw8ALP4kDO/grYkoGFjviA+DHcFufGJCd3OoSXb1eUbFaUVBHFcwydn9vw2q2SYkozMG/csGDtmBc+5emN3BFkZe66uvV+knqzipevvhU9kPdlHcN0WcyOLXpne0IlO7oAcdrI0yaz3ZPCJLDL2us5NodvGWF2u0zZVzXJBFn3NyPbPUR7MTMUAgAR59hV8qa8iGgPQqEo8g/V4aUMoLxJrP9BMTBQEIeVI97kLvUaZ1cq1G4wuOqh2AAeea2q/W1a8Jno8giV5TRZTM7QYXFomert77NuZ7wQT7YOm+PPinJnwXu+16ITaTDRieWORCCLGPwoJ59TqMmnS5jTgpcYsiAg5XzsZmW6NCMQ7KD505R7EbGpAT+Q9rS+tlf8o5dLMn+7xwKJzwbvedTG60rd8WC2TPWO8SpRTInkX3B5bJQrsFS7WnRli3EQF1EGFdebJXfvHEf5UKNYs66DKb0ssylaFJtRPAUT7ee/uSYt3lT1L55FdcH4C9GHC7hh9wGxt5VQ2JtPvFh4PYY5Pw+I1sJ526RgRCQXXv2O6g72+vgNqZprn6rLN3kmzMmuM/oDelyMDD82cKdcvWgr0WO3LB4COM8mg3wXxE4WC/wKAQY1GSG/qKUqZg6+Uv0aqzS1an9FJuiv87i0A3R09NJKDJCzEY5H/PElThXfN2KI3xiInbwjhIPrxkj2rw1eK0MQbHgeu3WaVTYY9nFPNweE9t/3dbeFizvC6F+5p2X4sWnWU286nbYOoRj2vTcD9KapgrxOpaqwhAuavew+YCxUZuPT6F20Mv905xDIa2A+YBpcc/s3nahXe8k5Kv+mCwf2itKoYg= 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: List-Subscribe: List-Unsubscribe: When memory is being placed, mmap() will take care to respect the guard gaps of certain types of memory (VM_SHADOWSTACK, VM_GROWSUP and VM_GROWSDOWN). In order to ensure guard gaps between mappings, mmap() needs to consider two things: 1. That the new mapping isn’t placed in an any existing mappings guard gaps. 2. That the new mapping isn’t placed such that any existing mappings are not in *its* guard gaps. The long standing behavior of mmap() is to ensure 1, but not take any care around 2. So for example, if there is a PAGE_SIZE free area, and a mmap() with a PAGE_SIZE size, and a type that has a guard gap is being placed, mmap() may place the shadow stack in the PAGE_SIZE free area. Then the mapping that is supposed to have a guard gap will not have a gap to the adjacent VMA. Add a THP implementations of the vm_flags variant of get_unmapped_area(). Future changes will call this from mmap.c in the do_mmap() path to allow shadow stacks to be placed with consideration taken for the start guard gap. Shadow stack memory is always private and anonymous and so special guard gap logic is not needed in a lot of caseis, but it can be mapped by THP, so needs to be handled. Signed-off-by: Rick Edgecombe --- include/linux/huge_mm.h | 11 +++++++++++ mm/huge_memory.c | 23 ++++++++++++++++------- mm/mmap.c | 12 +++++++----- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index fa0350b0812a..ef7251dfd9f9 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -139,6 +139,9 @@ bool hugepage_vma_check(struct vm_area_struct *vma, unsigned long vm_flags, unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags); +unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, unsigned long flags, + vm_flags_t vm_flags); void folio_prep_large_rmappable(struct folio *folio); bool can_split_folio(struct folio *folio, int *pextra_pins); @@ -286,6 +289,14 @@ static inline void folio_prep_large_rmappable(struct folio *folio) {} #define thp_get_unmapped_area NULL +static inline unsigned long +thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, + unsigned long flags, vm_flags_t vm_flags) +{ + return 0; +} + static inline bool can_split_folio(struct folio *folio, int *pextra_pins) { diff --git a/mm/huge_memory.c b/mm/huge_memory.c index e9ef43a719a5..f235f6d3ff62 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -628,7 +628,8 @@ static inline bool is_transparent_hugepage(struct folio *folio) static unsigned long __thp_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, - loff_t off, unsigned long flags, unsigned long size) + loff_t off, unsigned long flags, unsigned long size, + vm_flags_t vm_flags) { loff_t off_end = off + len; loff_t off_align = round_up(off, size); @@ -641,8 +642,8 @@ static unsigned long __thp_get_unmapped_area(struct file *filp, if (len_pad < len || (off + len_pad) < off) return 0; - ret = mm_get_unmapped_area(current->mm, filp, addr, len_pad, - off >> PAGE_SHIFT, flags); + ret = mm_get_unmapped_area_vmflags(current->mm, filp, addr, len_pad, + off >> PAGE_SHIFT, flags, vm_flags); /* * The failure might be due to length padding. The caller will retry @@ -662,17 +663,25 @@ static unsigned long __thp_get_unmapped_area(struct file *filp, return ret; } -unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, - unsigned long len, unsigned long pgoff, unsigned long flags) +unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, unsigned long flags, + vm_flags_t vm_flags) { unsigned long ret; loff_t off = (loff_t)pgoff << PAGE_SHIFT; - ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE); + ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE, vm_flags); if (ret) return ret; - return mm_get_unmapped_area(current->mm, filp, addr, len, pgoff, flags); + return mm_get_unmapped_area_vmflags(current->mm, filp, addr, len, pgoff, flags, + vm_flags); +} + +unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, unsigned long flags) +{ + return thp_get_unmapped_area_vmflags(filp, addr, len, pgoff, flags, 0); } EXPORT_SYMBOL_GPL(thp_get_unmapped_area); diff --git a/mm/mmap.c b/mm/mmap.c index ac7601d05e89..e02bb17fef5b 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1857,16 +1857,18 @@ __get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, */ pgoff = 0; get_area = shmem_get_unmapped_area; - } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { - /* Ensures that larger anonymous mappings are THP aligned. */ - get_area = thp_get_unmapped_area; } - if (get_area) + if (get_area) { addr = get_area(file, addr, len, pgoff, flags); - else + } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { + /* Ensures that larger anonymous mappings are THP aligned. */ + addr = thp_get_unmapped_area_vmflags(file, addr, len, + pgoff, flags, vm_flags); + } else { addr = mm_get_unmapped_area_vmflags(current->mm, file, addr, len, pgoff, flags, vm_flags); + } if (IS_ERR_VALUE(addr)) return addr; From patchwork Mon Feb 26 19:09:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13572727 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43E8DC48BF6 for ; Mon, 26 Feb 2024 19:10:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4A42A4401B5; Mon, 26 Feb 2024 14:10:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E6824401B6; Mon, 26 Feb 2024 14:10:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DC804401B5; Mon, 26 Feb 2024 14:10:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0A22944017F for ; Mon, 26 Feb 2024 14:10:06 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C63CEA0B75 for ; Mon, 26 Feb 2024 19:10:05 +0000 (UTC) X-FDA: 81834895170.05.70E5C60 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf09.hostedemail.com (Postfix) with ESMTP id F0F4C140010 for ; Mon, 26 Feb 2024 19:10:03 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=G8SCwBUv; spf=pass (imf09.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974604; a=rsa-sha256; cv=none; b=J6RyHVKoE2Bec7IMdQiRqR68ewt9P9+RfBYcykOWHj+g/O34iUQfk8qdAI3IgxGlu60c5V Yq2Ua9fyaEX5ASL+A5Yk2VAz0/RXPMmDOiA06pECe2EATDC6EHEMnViEl0OlbUHqlqVxDb h8x3Yq5TpbaIOJCy837ZHIzm3/4MqUU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=G8SCwBUv; spf=pass (imf09.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708974604; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=6mHy37VmvlRmrDV3R0ptBwPC2WKBOKGgSYsH3isxCVo=; b=QcfNrW6kG8n3BFYJWZB/0oR2RFnaqEJeYNjwoXQZ+vZEdVSNxTfYY1x2xmnXEUx5134U3v BgcEQro/cWRHnR5iWppf622xOkJ5s3k050v5HwgqpGElI3sMuJTGmLRCNXlDcYRo6QIRA6 dkUNbVH0R5wDRDTM63NirhPANQ6YJyI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974604; x=1740510604; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=M4PemZaS0aqUDnC2dki1OuhIuHLGRlk1+X+hvrPVA+A=; b=G8SCwBUv/K5l7eHgcZtAtfdacRgZ/J73rBkRExkNcngEOGY7qQ1lVH1F rPjJT+vE4RlGlGtPo4+dDvdBE1f8dDkDagW8CKfOXydJ9ZDSgVZpGmUsf gy3Fr74ioYSp6cpmVvoj6CWQo5vzpj1OVAczYrO4j2zSeL5s2YM4p2JiA IB+xyh6IK7OBBH+e4zbf2MYcyO2M9Z31qxUATLRzFsuNd5O0lwHIDR4jB 5W3M0DYrbGPPW9anRhZ5d2/dHLEGzCH+GyvOcYtzwcsS9puzD/YN7aRJa ZqpgQpqOAWAm9vSWW59ZTIIOkwza7WNg9Y+YWa7JeYYcHPN1uYRNrAFwh A==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721389" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721389" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:02 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911458" Received: from bdmirand-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.3.213]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:00 -0800 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, kirill.shutemov@linux.intel.com, keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com, linux-alpha@vger.kernel.org, linux-snps-arc@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-csky@vger.kernel.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org Subject: [PATCH v2 5/9] mm: Initialize struct vm_unmapped_area_info Date: Mon, 26 Feb 2024 11:09:47 -0800 Message-Id: <20240226190951.3240433-6-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> References: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: F0F4C140010 X-Stat-Signature: wd3tui4rmwz6e9xs4k86ktkjui31i5br X-Rspam-User: X-HE-Tag: 1708974603-909226 X-HE-Meta: U2FsdGVkX185VIcNrlRmOz5G6tN4EuKEsPyOc8rK0Ttumpu4li8WEgTAPY1ZLFD6MqkyRs0uC2FjHDsRSMRKI06G+3uzFcBWiTa1zhIhla/pu9HNunbzqwxq+xEF/u0vWXJ9AprXUzk2TaTH4alB2r5zNkHULUIOkOrNOuvpGp+ZKXH3wol7U/oFjTSO5lZOAxt7/I05oDAyk1fV0nzb4cuxhIl2VGgQ3jaMshLLj7Ur7VjRF3iTOd3imSHeGa+YeJUZIC4X/f+zWhrBIAWJ7M11bc7+hVV6kdW4ownB+g//UasIIeBIjlfPfqWyIUeWwgb35R6+akzSIPkbAOtzYS2sfO17EXSsgWNtXHFi0kwvfZG4tDQWdNvWks9HQykRf+XqYncjIWj7uU4o/irMTyOrXiLdNu+0zWIulMiUDtw//qjboefN2PZExoFHy12SxApxy6SC1shUW7MyzkabSwlkuuLRRGWYuWp8/H68gcm1BarJqo76gAVVSu6foojLdJ+K0gE6xzl7QOlrBkzpr/bUS2vxHuM/dkvYW0Qov9uQhaKAB7bvoy5S+MEX445EiwmxYVR/IBJ2DZvbbBFKqmQml0QXD4njQq7zLrnr6bk9nw+kkTmDyKw1v9wuVUndr5pcIJly4lFQakIosDonPbWZ4Q8GxzAHNXEJRdYz8XNM1RUEACmasFMeA7JyozjGcQ2CVboAxelKlg0yneObhY4DbeAqmEz/m7soxMHx4V2/opSmVeuWkqWrkK977+epstPPXnkPL1zqo3CQZIoRNpVCN86IHBiZBCg5+dHBEZubvLMXSb7nHCVyHdPBHTgk3EgOtFx883w6QvvkqHOSjIPIjO/KLX3dFy2DNv0hf1E3agT7o3RkBmccFZje5pJ4BHlVW/uRXxNO1Tav61cPB6uXWyYWD3u6ZlT4SB39+4VB7FL2BmhWAw== 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: List-Subscribe: List-Unsubscribe: Future changes will need to add a field to struct vm_unmapped_area_info. This would cause trouble for any archs that don't initialize the struct. Currently every user sets each field, so if new fields are added, the core code parsing the struct will see garbage in the new field. It could be possible to initialize the new field for each arch to 0, but instead simply inialize the field with a C99 struct inializing syntax. Cc: linux-mm@kvack.org Cc: linux-alpha@vger.kernel.org Cc: linux-snps-arc@lists.infradead.org Cc: linux-arm-kernel@lists.infradead.org Cc: linux-csky@vger.kernel.org Cc: loongarch@lists.linux.dev Cc: linux-mips@vger.kernel.org Cc: linux-parisc@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org Cc: linux-s390@vger.kernel.org Cc: linux-sh@vger.kernel.org Cc: sparclinux@vger.kernel.org Cc: x86@kernel.org Suggested-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe Link: https://lore.kernel.org/lkml/3ynogxcgokc6i6xojbxzzwqectg472laes24u7jmtktlxcch5e@dfytra3ia3zc/#t --- Hi archs, For some context, this is part of a larger series to improve shadow stack guard gaps. It involves plumbing a new field via struct vm_unmapped_area_info. The first user is x86, but arm and riscv may likely use it as well. The change is compile tested only for non-x86 but seems like a relatively safe one. Thanks, Rick v2: - New patch --- arch/alpha/kernel/osf_sys.c | 2 +- arch/arc/mm/mmap.c | 2 +- arch/arm/mm/mmap.c | 4 ++-- arch/csky/abiv1/mmap.c | 2 +- arch/loongarch/mm/mmap.c | 2 +- arch/mips/mm/mmap.c | 2 +- arch/parisc/kernel/sys_parisc.c | 2 +- arch/powerpc/mm/book3s64/slice.c | 4 ++-- arch/s390/mm/hugetlbpage.c | 4 ++-- arch/s390/mm/mmap.c | 4 ++-- arch/sh/mm/mmap.c | 4 ++-- arch/sparc/kernel/sys_sparc_32.c | 2 +- arch/sparc/kernel/sys_sparc_64.c | 4 ++-- arch/sparc/mm/hugetlbpage.c | 4 ++-- arch/x86/kernel/sys_x86_64.c | 4 ++-- arch/x86/mm/hugetlbpage.c | 4 ++-- fs/hugetlbfs/inode.c | 4 ++-- mm/mmap.c | 4 ++-- 18 files changed, 29 insertions(+), 29 deletions(-) diff --git a/arch/alpha/kernel/osf_sys.c b/arch/alpha/kernel/osf_sys.c index 5db88b627439..dd6801bb9240 100644 --- a/arch/alpha/kernel/osf_sys.c +++ b/arch/alpha/kernel/osf_sys.c @@ -1218,7 +1218,7 @@ static unsigned long arch_get_unmapped_area_1(unsigned long addr, unsigned long len, unsigned long limit) { - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; info.flags = 0; info.length = len; diff --git a/arch/arc/mm/mmap.c b/arch/arc/mm/mmap.c index 3c1c7ae73292..6549b3375f54 100644 --- a/arch/arc/mm/mmap.c +++ b/arch/arc/mm/mmap.c @@ -27,7 +27,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr, { struct mm_struct *mm = current->mm; struct vm_area_struct *vma; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; /* * We enforce the MAP_FIXED case. diff --git a/arch/arm/mm/mmap.c b/arch/arm/mm/mmap.c index a0f8a0ca0788..525795578c29 100644 --- a/arch/arm/mm/mmap.c +++ b/arch/arm/mm/mmap.c @@ -34,7 +34,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr, struct vm_area_struct *vma; int do_align = 0; int aliasing = cache_is_vipt_aliasing(); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; /* * We only need to do colour alignment if either the I or D @@ -87,7 +87,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, unsigned long addr = addr0; int do_align = 0; int aliasing = cache_is_vipt_aliasing(); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; /* * We only need to do colour alignment if either the I or D diff --git a/arch/csky/abiv1/mmap.c b/arch/csky/abiv1/mmap.c index 6792aca49999..726659d41fa9 100644 --- a/arch/csky/abiv1/mmap.c +++ b/arch/csky/abiv1/mmap.c @@ -28,7 +28,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr, struct mm_struct *mm = current->mm; struct vm_area_struct *vma; int do_align = 0; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; /* * We only need to do colour alignment if either the I or D diff --git a/arch/loongarch/mm/mmap.c b/arch/loongarch/mm/mmap.c index a9630a81b38a..664bf4abfdcf 100644 --- a/arch/loongarch/mm/mmap.c +++ b/arch/loongarch/mm/mmap.c @@ -24,7 +24,7 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp, struct vm_area_struct *vma; unsigned long addr = addr0; int do_color_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; if (unlikely(len > TASK_SIZE)) return -ENOMEM; diff --git a/arch/mips/mm/mmap.c b/arch/mips/mm/mmap.c index 00fe90c6db3e..6321b53dc995 100644 --- a/arch/mips/mm/mmap.c +++ b/arch/mips/mm/mmap.c @@ -34,7 +34,7 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp, struct vm_area_struct *vma; unsigned long addr = addr0; int do_color_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; if (unlikely(len > TASK_SIZE)) return -ENOMEM; diff --git a/arch/parisc/kernel/sys_parisc.c b/arch/parisc/kernel/sys_parisc.c index 98af719d5f85..e87c0e325abf 100644 --- a/arch/parisc/kernel/sys_parisc.c +++ b/arch/parisc/kernel/sys_parisc.c @@ -104,7 +104,7 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp, struct vm_area_struct *vma, *prev; unsigned long filp_pgoff; int do_color_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; if (unlikely(len > TASK_SIZE)) return -ENOMEM; diff --git a/arch/powerpc/mm/book3s64/slice.c b/arch/powerpc/mm/book3s64/slice.c index c0b58afb9a47..5884f384866f 100644 --- a/arch/powerpc/mm/book3s64/slice.c +++ b/arch/powerpc/mm/book3s64/slice.c @@ -282,7 +282,7 @@ static unsigned long slice_find_area_bottomup(struct mm_struct *mm, { int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT); unsigned long found, next_end; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; info.flags = 0; info.length = len; @@ -326,7 +326,7 @@ static unsigned long slice_find_area_topdown(struct mm_struct *mm, { int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT); unsigned long found, prev; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; unsigned long min_addr = max(PAGE_SIZE, mmap_min_addr); info.flags = VM_UNMAPPED_AREA_TOPDOWN; diff --git a/arch/s390/mm/hugetlbpage.c b/arch/s390/mm/hugetlbpage.c index c2d2850ec8d5..7f68485feea0 100644 --- a/arch/s390/mm/hugetlbpage.c +++ b/arch/s390/mm/hugetlbpage.c @@ -258,7 +258,7 @@ static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file, unsigned long pgoff, unsigned long flags) { struct hstate *h = hstate_file(file); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; info.flags = 0; info.length = len; @@ -274,7 +274,7 @@ static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file, unsigned long pgoff, unsigned long flags) { struct hstate *h = hstate_file(file); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; unsigned long addr; info.flags = VM_UNMAPPED_AREA_TOPDOWN; diff --git a/arch/s390/mm/mmap.c b/arch/s390/mm/mmap.c index cd52d72b59cf..df88496e2903 100644 --- a/arch/s390/mm/mmap.c +++ b/arch/s390/mm/mmap.c @@ -77,7 +77,7 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, { struct mm_struct *mm = current->mm; struct vm_area_struct *vma; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; if (len > TASK_SIZE - mmap_min_addr) return -ENOMEM; @@ -116,7 +116,7 @@ unsigned long arch_get_unmapped_area_topdown(struct file *filp, unsigned long ad { struct vm_area_struct *vma; struct mm_struct *mm = current->mm; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; /* requested length too big for entire address space */ if (len > TASK_SIZE - mmap_min_addr) diff --git a/arch/sh/mm/mmap.c b/arch/sh/mm/mmap.c index b82199878b45..6aee5f761e08 100644 --- a/arch/sh/mm/mmap.c +++ b/arch/sh/mm/mmap.c @@ -57,7 +57,7 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, struct mm_struct *mm = current->mm; struct vm_area_struct *vma; int do_colour_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; if (flags & MAP_FIXED) { /* We do not accept a shared mapping if it would violate @@ -106,7 +106,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, struct mm_struct *mm = current->mm; unsigned long addr = addr0; int do_colour_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; if (flags & MAP_FIXED) { /* We do not accept a shared mapping if it would violate diff --git a/arch/sparc/kernel/sys_sparc_32.c b/arch/sparc/kernel/sys_sparc_32.c index 082a551897ed..7e781dbfd052 100644 --- a/arch/sparc/kernel/sys_sparc_32.c +++ b/arch/sparc/kernel/sys_sparc_32.c @@ -41,7 +41,7 @@ SYSCALL_DEFINE0(getpagesize) unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; if (flags & MAP_FIXED) { /* We do not accept a shared mapping if it would violate diff --git a/arch/sparc/kernel/sys_sparc_64.c b/arch/sparc/kernel/sys_sparc_64.c index 1dbf7211666e..fc48ab3f83af 100644 --- a/arch/sparc/kernel/sys_sparc_64.c +++ b/arch/sparc/kernel/sys_sparc_64.c @@ -93,7 +93,7 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsi struct vm_area_struct * vma; unsigned long task_size = TASK_SIZE; int do_color_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; if (flags & MAP_FIXED) { /* We do not accept a shared mapping if it would violate @@ -154,7 +154,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, unsigned long task_size = STACK_TOP32; unsigned long addr = addr0; int do_color_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; /* This should only ever run for 32-bit processes. */ BUG_ON(!test_thread_flag(TIF_32BIT)); diff --git a/arch/sparc/mm/hugetlbpage.c b/arch/sparc/mm/hugetlbpage.c index 38a1bef47efb..614e2c46d781 100644 --- a/arch/sparc/mm/hugetlbpage.c +++ b/arch/sparc/mm/hugetlbpage.c @@ -31,7 +31,7 @@ static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *filp, { struct hstate *h = hstate_file(filp); unsigned long task_size = TASK_SIZE; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; if (test_thread_flag(TIF_32BIT)) task_size = STACK_TOP32; @@ -63,7 +63,7 @@ hugetlb_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, struct hstate *h = hstate_file(filp); struct mm_struct *mm = current->mm; unsigned long addr = addr0; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; /* This should only ever run for 32-bit processes. */ BUG_ON(!test_thread_flag(TIF_32BIT)); diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c index c783aeb37dce..6e5d4fa5fc42 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -125,7 +125,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr, { struct mm_struct *mm = current->mm; struct vm_area_struct *vma; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; unsigned long begin, end; if (flags & MAP_FIXED) @@ -165,7 +165,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, struct vm_area_struct *vma; struct mm_struct *mm = current->mm; unsigned long addr = addr0; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; /* requested length too big for entire address space */ if (len > TASK_SIZE) diff --git a/arch/x86/mm/hugetlbpage.c b/arch/x86/mm/hugetlbpage.c index 6d77c0039617..88726bd1f72d 100644 --- a/arch/x86/mm/hugetlbpage.c +++ b/arch/x86/mm/hugetlbpage.c @@ -51,7 +51,7 @@ static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file, unsigned long pgoff, unsigned long flags) { struct hstate *h = hstate_file(file); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; info.flags = 0; info.length = len; @@ -74,7 +74,7 @@ static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file, unsigned long pgoff, unsigned long flags) { struct hstate *h = hstate_file(file); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; info.flags = VM_UNMAPPED_AREA_TOPDOWN; info.length = len; diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index a63d2eee086f..848b2239a215 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -165,7 +165,7 @@ hugetlb_get_unmapped_area_bottomup(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { struct hstate *h = hstate_file(file); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; info.flags = 0; info.length = len; @@ -181,7 +181,7 @@ hugetlb_get_unmapped_area_topdown(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { struct hstate *h = hstate_file(file); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; info.flags = VM_UNMAPPED_AREA_TOPDOWN; info.length = len; diff --git a/mm/mmap.c b/mm/mmap.c index e02bb17fef5b..33af683a643f 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1699,7 +1699,7 @@ generic_get_unmapped_area(struct file *filp, unsigned long addr, { struct mm_struct *mm = current->mm; struct vm_area_struct *vma, *prev; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); if (len > mmap_end - mmap_min_addr) @@ -1747,7 +1747,7 @@ generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr, { struct vm_area_struct *vma, *prev; struct mm_struct *mm = current->mm; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info = {}; const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); /* requested length too big for entire address space */ From patchwork Mon Feb 26 19:09:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13572728 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37168C54E4A for ; Mon, 26 Feb 2024 19:10:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A78C4401B6; Mon, 26 Feb 2024 14:10:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 907A144017F; Mon, 26 Feb 2024 14:10:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C69F4401B6; Mon, 26 Feb 2024 14:10:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3D74944017F for ; Mon, 26 Feb 2024 14:10:06 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2300B1C0B06 for ; Mon, 26 Feb 2024 19:10:06 +0000 (UTC) X-FDA: 81834895212.01.7C1B4CB Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf07.hostedemail.com (Postfix) with ESMTP id 33BB040008 for ; Mon, 26 Feb 2024 19:10:04 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=F2rNtU6k; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708974604; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xPLva11Xq7XC8tYhVSCbr9K0IrR4H1dbybdarN6i4iY=; b=V904t29W3xv4gy5+Ynt2/KYPb6Y8/5tSax0EEnjxpc93HgNW/KMhuFJbDA3sugn2mrRdCJ aqH1HcDbWclS0eI6UurkqeIFi1YftAIOdsa8EV5Wu+TVEd8b/AVLkyMr7vVjy9lMCaI9Zy PZQcbVIb0yTYsOEFb+khvxG/3LAe2GI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974604; a=rsa-sha256; cv=none; b=EeM12yZlR5wzR5BN8o4lFo3dsWcrOgkonR00C5wwxA/dvrR21B9klMxlfVe2sQmARflQaP yHxkZm5OCH160t4bI/p/lEqtiumEkF9lsN6cc8JkYbN8lUfB8KMbEmAxy06/35vphZIsys Zctmz55Jd4kp0xJqunRvOrfzifkQbTA= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=F2rNtU6k; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974605; x=1740510605; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U3F9hHxdfZ7A6mCry7GoiZtnuJCP/wXQbUGWHcLnyx0=; b=F2rNtU6kPY4lJwDGnQB/SsdN8Gxd5kYU1/fkSo89vVfqMzZ+1M9ybOa0 MWsGZYme5+7FHmr5KPFqoTE5pmC3x5fvHJqc2rI/ln8jQtXPbKMokY7Zq El+6qYn8qB9L7rKkcmfoafifzRkANs5Tw5gHsPIWI16SejSFm/JNbFmfY Q5r788LuLDZ28tWLDccsLDHSUR/iq/a+JGKnZM+JL+8y0aAQmKtSry5So XId+nP5bxBlGmqvJeUtlosfqul+ynOOE3SRgCU1Os+Hv17TEnxyydHkAy QF9DehaJyeAI+6Y9CBjcmw83AV43X+WRDt3rzpKF/OGZavN7GaxudIocT g==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721404" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721404" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:03 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911463" Received: from bdmirand-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.3.213]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:02 -0800 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, kirill.shutemov@linux.intel.com, keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH v2 6/9] mm: Take placement mappings gap into account Date: Mon, 26 Feb 2024 11:09:48 -0800 Message-Id: <20240226190951.3240433-7-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> References: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 33BB040008 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: hwm14qaxpqz4aoi5k7zx11ankczt4wms X-HE-Tag: 1708974604-665373 X-HE-Meta: U2FsdGVkX186etTdsjS02tbynFChWESa/Li7oJ56YlHW4M9xbf7nFV2ircZ3iIASV8g8uU2gtxQWhNKkSWRNxTmnn1JzAIpM0aLOg1up8yiCrSNChicKkJ1aEEMJYIHQP1gxTwP1AFs33h2ol4LQ1u3NvAoesz3j2At7YhL/m5QzLH1glSUTRQYUtDakIScLSry6BAo8/w6t/wpCkUsPuITRTcrbrjyirth//qTXLABemSkJeD9nMXB9LI5AtZmA+gWDRL90MXcw+qTqCZJWLtrKSOg9buVl9iieJPLxeu2ypBgrVYoCJ2hD+feZyMr87z5ynL+C2LSyM2Ad0Xd9aBNLfn7TJtGsdpKdcKlau3SvE/XPI7hUaUqRmYS2KJ/JOXqv0DBN+KkJDEFw63Xy16RR9CMoA03ZaAvcD3k2SyoNpyQXuz1CI+01xoOaoqO7YWqJueHHUiY445wGGbxf71U5dDPQePay99nBTP0jpTsBrx28jyIN5j3MVXK4+CDK0/xLzq9e7HdmvAKtrdzIIquxXfiD3WpMrIg44xT3PmkuWksjJybg8fCkaQyAZno+lBkTBrHJhV8VLm2GCLhSlBB8tXDymHoZtdTUf/lj+MaXe55qub2GWN+uxPaESY91o5viCR5LCKWhFlX4Vj5HqgnwapiT8+PQuUQv66n9aic18sqc6xpq9y4h5Ati78z8YgU9jsMdzEU3X6nkmiCIVmjDO2CWAIdK9iPtyVSALrPU5eVIK0VJDYzJ4/N2f/fxQd+kb4PMiYGK6NvzgMwWE6nG7x16hInpqwjACWZidlsjwbSUq02Tc8MIYf097LI4/qwdjJOkqzQbAi+7WKg+QmFKy9J+PsaH 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: List-Subscribe: List-Unsubscribe: When memory is being placed, mmap() will take care to respect the guard gaps of certain types of memory (VM_SHADOWSTACK, VM_GROWSUP and VM_GROWSDOWN). In order to ensure guard gaps between mappings, mmap() needs to consider two things: 1. That the new mapping isn’t placed in an any existing mappings guard gaps. 2. That the new mapping isn’t placed such that any existing mappings are not in *its* guard gaps. The long standing behavior of mmap() is to ensure 1, but not take any care around 2. So for example, if there is a PAGE_SIZE free area, and a mmap() with a PAGE_SIZE size, and a type that has a guard gap is being placed, mmap() may place the shadow stack in the PAGE_SIZE free area. Then the mapping that is supposed to have a guard gap will not have a gap to the adjacent VMA. For MAP_GROWSDOWN/VM_GROWSDOWN and MAP_GROWSUP/VM_GROWSUP this has not been a problem in practice because applications place these kinds of mappings very early, when there is not many mappings to find a space between. But for shadow stacks, they may be placed throughout the lifetime of the application. Use the start_gap field to find a space that includes the guard gap for the new mapping. Take care to not interfere with the alignment. Signed-off-by: Rick Edgecombe --- v2: - Remove VM_UNMAPPED_START_GAP_SET and have struct vm_unmapped_area_info initialized with zeros (in another patch). (Kirill) - Drop unrelated space change (Kirill) - Add comment around interactions of alignment and start gap step (Kirill) --- include/linux/mm.h | 1 + mm/mmap.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index a091181ef65a..19fc1eb86b9a 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3406,6 +3406,7 @@ struct vm_unmapped_area_info { unsigned long high_limit; unsigned long align_mask; unsigned long align_offset; + unsigned long start_gap; }; extern unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info); diff --git a/mm/mmap.c b/mm/mmap.c index 33af683a643f..3d7642eb84ea 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1574,7 +1574,7 @@ static unsigned long unmapped_area(struct vm_unmapped_area_info *info) MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); /* Adjust search length to account for worst case alignment overhead */ - length = info->length + info->align_mask; + length = info->length + info->align_mask + info->start_gap; if (length < info->length) return -ENOMEM; @@ -1586,7 +1586,13 @@ static unsigned long unmapped_area(struct vm_unmapped_area_info *info) if (mas_empty_area(&mas, low_limit, high_limit - 1, length)) return -ENOMEM; - gap = mas.index; + /* + * Adjust for the gap first so it doesn't interfere with the + * later alignment. The first step is the minimum needed to + * fufill the start gap, the next steps is the minimum to align + * that. It is the minimum needed to fufill both. + */ + gap = mas.index + info->start_gap; gap += (info->align_offset - gap) & info->align_mask; tmp = mas_next(&mas, ULONG_MAX); if (tmp && (tmp->vm_flags & VM_STARTGAP_FLAGS)) { /* Avoid prev check if possible */ @@ -1625,7 +1631,7 @@ static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info) MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); /* Adjust search length to account for worst case alignment overhead */ - length = info->length + info->align_mask; + length = info->length + info->align_mask + info->start_gap; if (length < info->length) return -ENOMEM; From patchwork Mon Feb 26 19:09:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13572729 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3064C48BF6 for ; Mon, 26 Feb 2024 19:10:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0F4064401B7; Mon, 26 Feb 2024 14:10:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 07BB744017F; Mon, 26 Feb 2024 14:10:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DE8F64401B7; Mon, 26 Feb 2024 14:10:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CB45F44017F for ; Mon, 26 Feb 2024 14:10:06 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 97CA91A0989 for ; Mon, 26 Feb 2024 19:10:06 +0000 (UTC) X-FDA: 81834895212.23.7F4BA19 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf16.hostedemail.com (Postfix) with ESMTP id A5013180022 for ; Mon, 26 Feb 2024 19:10:04 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=k1A88vYc; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708974604; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QpIDopAa6aJWE/FyfJUqodq2X8Jt1lCOE+G5EQZEW2I=; b=j4nrueIEr1b8rgqTUPIqpG9/BY9bMyCLmjWIpFH20bnkRckE6vBn0lQcbIa2V7YLFao3HM 6PmZItT7+8/hhe3y8fyREj6ptqP8z5WCySWp2KgWWpQFLe7HSasLnlvfRCyvpBSC/D2B9q /1iboWA1LTnIr+eKl8iACwVV0aU4i3I= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=k1A88vYc; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974604; a=rsa-sha256; cv=none; b=xQFyIldQvy6kdQL7QhYHc57kJD8dSjL3iB4Uq03TV1ghwVaqsaExyYi1lDzfTGXhNKODGi xXNyaR94oJqZtrkVN+LAVVU27DrpMVmM/U1m/DcIfSqEQHRAXMuA8eBFAgf/t0DgDRWUq7 qi7qNW7MBERTwK2OwFgb/mOo9eVsV+E= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974605; x=1740510605; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=K/25qnwR+QY0dPYdq2kDmPj6NmWSjdLBsEBSug4d6Ew=; b=k1A88vYcyjw6WJXT1de92wDZar6loS1AxV8YLoIv16Pp514N5h4ks4gV VupC4kF1yOrreFkc5xSd2JyW9hGvOqCqbIL6raTD5K1B+lkiJkjby3RVM hW6utWLD5isoT0WGW5no3u3kcuJHpDUOQQl6/LDB61M7jIf83MIgq/oGE 2bOFVi219AITYKBhv41YqYBpqO7ZBVQENU2CaRzwoAgc1FcOZdGwdNWfU 2bbCNiCalVwmhcApvu/wqD5It1gwu+LqRN6wTx+miXELWz8f4XuunxiiJ cOVoCbQPlgK2wHQVoZjkeXxIW0zwzY1tnySUSJdAx7sxCjEKI1IJ68SrB A==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721416" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721416" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911467" Received: from bdmirand-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.3.213]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:02 -0800 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, kirill.shutemov@linux.intel.com, keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH v2 7/9] x86/mm: Implement HAVE_ARCH_UNMAPPED_AREA_VMFLAGS Date: Mon, 26 Feb 2024 11:09:49 -0800 Message-Id: <20240226190951.3240433-8-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> References: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: zenkc355t5jfxkjogc4oj115r69nw3fi X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A5013180022 X-HE-Tag: 1708974604-154642 X-HE-Meta: U2FsdGVkX1/e4jqlDlYFbcWnuqc917Bq/XPwfFddoS7eqexUc7Ujx/FhcGURjTPhbs8GQpgTEb+Kr8De1rDNJwYCDZKiVw1Aeu/a+oMaESfdpkIpPPsj7UxDnhKHNKx3JSACk5+9dSPtGBI2RNYTFHwL+LpnrdvC9ZDwfjYnihwAPUS9fc0UxKSphnv9ZCdj4uzXjSyopEDRp2bUKTGX9gRrT5xNYx68fSBDAhIcooSNiRr2j1B0BqUmTLDtOXSO1NLfoOtHFLBg1cT/NRRuU+humQL+wcG2I1CPuaK634SDr6pmDf+fFUp+NMWYco+FiE3qNrBBuCfTZ2RIcgOYE9i1ro6NPyb5NVcHc1yq3MbOk08peK8rHUuff9DEsFfpk3wMx2TlLr7MAgE/AzBJFGwun7ZYd6KxTYNZge5Eox7mqX4Ne5zVlsqMp+lyF3tRwaBvRKolS8TbolbQmyZ80uYRYsALLspHxiOKcPyWIqjFMTHFoAft0oGwMPpfC5uOaYImeFQWk7ZWF8NAcKM3HwJ20DRSbIKSyaoi2IzDDBOZQ2J07WcJqeEQla7OejEPMGf9X/tj+cWN9BPJ8oNHO/pECGO7bZpQfVp6XhA6Gq8hCj6HNzh9zv4YatxF9fM9r4a8xdyD5saBhogjS1nse8MkiwPAfdqThIzjzWRQVZXbFhBdA0Jhlm3pipUGDn2Yij3C5MIesO5adksVfsGTH4E8gW52vCXdN4wCMvl6Myek76XyvSvs5AZE24kYdkdyzWuHzTuQKvTivEZsiXZqudKbCKbUG3rMfJoXOnDgDpMsbmpoFZacy9BdfuzytU0c6EzqXIa4g+Nz213tVzb0BhN+1i+UWuNEhVA8okgp2ksoZ76h3CWBPQi0W9P8oLzk28X4dQPW8rg= 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: List-Subscribe: List-Unsubscribe: When memory is being placed, mmap() will take care to respect the guard gaps of certain types of memory (VM_SHADOWSTACK, VM_GROWSUP and VM_GROWSDOWN). In order to ensure guard gaps between mappings, mmap() needs to consider two things: 1. That the new mapping isn’t placed in an any existing mappings guard gaps. 2. That the new mapping isn’t placed such that any existing mappings are not in *its* guard gaps. The long standing behavior of mmap() is to ensure 1, but not take any care around 2. So for example, if there is a PAGE_SIZE free area, and a mmap() with a PAGE_SIZE size, and a type that has a guard gap is being placed, mmap() may place the shadow stack in the PAGE_SIZE free area. Then the mapping that is supposed to have a guard gap will not have a gap to the adjacent VMA. Add x86 arch implementations of arch_get_unmapped_area_vmflags/_topdown() so future changes can allow the the guard gap of type of vma being placed to be taken into account. This will be used for shadow stack memory. Signed-off-by: Rick Edgecombe --- v2: - Remove unnecessary added extern --- arch/x86/include/asm/pgtable_64.h | 1 + arch/x86/kernel/sys_x86_64.c | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/pgtable_64.h b/arch/x86/include/asm/pgtable_64.h index a629b1b9f65a..eb09a11621ad 100644 --- a/arch/x86/include/asm/pgtable_64.h +++ b/arch/x86/include/asm/pgtable_64.h @@ -244,6 +244,7 @@ extern void cleanup_highmap(void); #define HAVE_ARCH_UNMAPPED_AREA #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN +#define HAVE_ARCH_UNMAPPED_AREA_VMFLAGS #define PAGE_AGP PAGE_KERNEL_NOCACHE #define HAVE_PAGE_AGP 1 diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c index 6e5d4fa5fc42..95cb9efc47fb 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -120,8 +120,8 @@ static void find_start_end(unsigned long addr, unsigned long flags, } unsigned long -arch_get_unmapped_area(struct file *filp, unsigned long addr, - unsigned long len, unsigned long pgoff, unsigned long flags) +arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, unsigned long len, + unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) { struct mm_struct *mm = current->mm; struct vm_area_struct *vma; @@ -158,9 +158,9 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr, } unsigned long -arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, - const unsigned long len, const unsigned long pgoff, - const unsigned long flags) +arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long addr0, + unsigned long len, unsigned long pgoff, + unsigned long flags, vm_flags_t vm_flags) { struct vm_area_struct *vma; struct mm_struct *mm = current->mm; @@ -230,3 +230,18 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, */ return arch_get_unmapped_area(filp, addr0, len, pgoff, flags); } + +unsigned long +arch_get_unmapped_area(struct file *filp, unsigned long addr, + unsigned long len, unsigned long pgoff, unsigned long flags) +{ + return arch_get_unmapped_area_vmflags(filp, addr, len, pgoff, flags, 0); +} + +unsigned long +arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr, + const unsigned long len, const unsigned long pgoff, + const unsigned long flags) +{ + return arch_get_unmapped_area_topdown_vmflags(filp, addr, len, pgoff, flags, 0); +} From patchwork Mon Feb 26 19:09:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13572730 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D8F8C48BF6 for ; Mon, 26 Feb 2024 19:10:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3B1B34401B8; Mon, 26 Feb 2024 14:10:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 361E744017F; Mon, 26 Feb 2024 14:10:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DDF64401B8; Mon, 26 Feb 2024 14:10:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id F044744017F for ; Mon, 26 Feb 2024 14:10:07 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id CB7C116097C for ; Mon, 26 Feb 2024 19:10:07 +0000 (UTC) X-FDA: 81834895254.13.E7B344C Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf09.hostedemail.com (Postfix) with ESMTP id DDF1D140010 for ; Mon, 26 Feb 2024 19:10:05 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=JWx+emSz; spf=pass (imf09.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974606; a=rsa-sha256; cv=none; b=7NdbhdugwWTF3Zm1ZZmSEhsMVP+O7ziIpreLGNWCu1BYjEY3RP2Ew3R1hWF12S58L/2b14 mWIc7OuD9oGNMP3DDsk5UHdCizkL+dHivITJCNDjnB9+uYmji/sb0F9iBYKJmxgzLAIQD9 Cop97qZmr9wV21X4KscElYTqhFClI9U= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=JWx+emSz; spf=pass (imf09.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708974606; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iPNFB5Ox9id13YpJe9qz5rG991kAdQLlrvip6tYfvmU=; b=LcPWj1d7zRBkaKg16UhxiB0tp9o3Y4RwRZu/nAkw7xwETR46o5HEeCouhEfzX1mJus/I92 UcSI+lR8ykYHaPrswJlE4UiCIqTWFonJ0JYaAKCRA3PHYq8CCBSRUoXNc2BVWKhyAh9NkS cghTKBs1u9NesF1BNZ6PFCuAmXYYKSI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974606; x=1740510606; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PSY3c+TQ2hmITdUufmc9vn7rLXvDTbUQjAaHGocBj3w=; b=JWx+emSzV3ci/Z2O7FOdvmYppiYGQJWzDofyyJ2fA1jjvD6vwaa84LNY HRjhYAjlEZrTqU1ipM4YSk7toRr9/tG9azEPefajlKF9+tVE16SrjDV/X UZYlWE9Grft4ZIt0gmCvOLz6xjZpWpcTK1o+WCNcECPmNSwYmcfD0SA3q biWe9UtMXu/SfBJ2Nw8+ZRO1aSeFGd3pH7dwYLoDSoqAeSTeRgt2D6dC/ 7mnAf+5wtv4ra+BhNelWVuR/sWbsGixM5d7UwbxSqJeuLNGyNzzfq3x8N SKyfe5k/zHgdd6mnO8OtFrhwxpGxygaf4Mvrsj4vQjT2VmV7a+Wgl6E9L A==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721429" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721429" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911471" Received: from bdmirand-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.3.213]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:03 -0800 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, kirill.shutemov@linux.intel.com, keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH v2 8/9] x86/mm: Care about shadow stack guard gap during placement Date: Mon, 26 Feb 2024 11:09:50 -0800 Message-Id: <20240226190951.3240433-9-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> References: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: DDF1D140010 X-Stat-Signature: h1at8x1aycpead6ftiezwewtwu6111io X-Rspam-User: X-HE-Tag: 1708974605-449386 X-HE-Meta: U2FsdGVkX18Tu2dK4Q5mxRxrwc82sPuThwUQ5foW7iEzkSbn5r9dsda6vVT+0KnszSuUi/QIFbuIzy/o0wpNEHj5JlipY+045D5ppn93z9YfvhSviDzUwF/UYPYDcnhbnJjO8YmT2nY8mFXkcSlDH4YAyvE9IcM5ELztkxazdUgsnQuHh534qeYo3RQ4NTE4QYlJCPX+hfIlNhqpJiekw0Zb4pTdOW8h+vul5q6wN8XXtbcac1/RHLW8PaRa1r9EZn0VKIAbu3ZB8wxtdKH195RCkJcosE/yNekMm5qf2yv/YSWIaqfiFikk096ioSiJzmps8/ZVYZ41XajHmTE3MlOlOi2Az/2UCuZLFq1B5KQsE+kZnZtvRVkoC+6FUd6awyltybLVqcU14WxJ7+4HCCM+aQOtbZOAPBcEbxFAw4rsr7ZnJ99SLbD0y83KrXZVziDmFSNXyak30lBOd8Ghid5oxUUBwd+IZD5Pn1jkpfqYeRu3SC2Hk8BfBwZFUW8bGU0gjTHz/u0aV1TncWXXdyh++CtPI88LjpDK8vy4I8AeUor6ry+G7gQ9Ysm2fRYjfl1OcitP9WWwEmtuWqhmkDL62cNcGv96VVIoyiD852DOL6k9gjbkZP2ogRNA0DGajRc1iuheanrM/AEn/uNp5yM0sOr9fCBqTsD+GLhLZRMBvfvWXlAEo2AYHBjuSvPmZ3fygwpM0554p4NskowBSlp+meSEWsIHK2F8nAHwpdKEgSuy/NpEHm23EQu/aPkzmz5LrUZQeXsSogt0bI6rD0iyDhQNtln+3EVwGgy4TwvHVNy0uMWduxOvzHGFpIaBfwYaSUZuDsD7HOb0tmEoFU7MQu/T0VjQ 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: List-Subscribe: List-Unsubscribe: When memory is being placed, mmap() will take care to respect the guard gaps of certain types of memory (VM_SHADOWSTACK, VM_GROWSUP and VM_GROWSDOWN). In order to ensure guard gaps between mappings, mmap() needs to consider two things: 1. That the new mapping isn’t placed in an any existing mappings guard gaps. 2. That the new mapping isn’t placed such that any existing mappings are not in *its* guard gaps. The long standing behavior of mmap() is to ensure 1, but not take any care around 2. So for example, if there is a PAGE_SIZE free area, and a mmap() with a PAGE_SIZE size, and a type that has a guard gap is being placed, mmap() may place the shadow stack in the PAGE_SIZE free area. Then the mapping that is supposed to have a guard gap will not have a gap to the adjacent VMA. Now that the vm_flags is passed into the arch get_unmapped_area()'s, and vm_unmapped_area() is ready to consider it, have VM_SHADOW_STACK's get guard gap consideration for scenario 2. Signed-off-by: Rick Edgecombe --- arch/x86/kernel/sys_x86_64.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c index 95cb9efc47fb..f44afb3345db 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -119,6 +119,14 @@ static void find_start_end(unsigned long addr, unsigned long flags, *end = task_size_64bit(addr > DEFAULT_MAP_WINDOW); } +static inline unsigned long stack_guard_placement(vm_flags_t vm_flags) +{ + if (vm_flags & VM_SHADOW_STACK) + return PAGE_SIZE; + + return 0; +} + unsigned long arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) @@ -150,6 +158,7 @@ arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, unsigned l info.high_limit = end; info.align_mask = 0; info.align_offset = pgoff << PAGE_SHIFT; + info.start_gap = stack_guard_placement(vm_flags); if (filp) { info.align_mask = get_align_mask(); info.align_offset += get_align_bits(); @@ -199,6 +208,7 @@ arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long addr0, info.low_limit = PAGE_SIZE; info.high_limit = get_mmap_base(0); + info.start_gap = stack_guard_placement(vm_flags); /* * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area From patchwork Mon Feb 26 19:09:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13572731 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59AA6C48BF6 for ; Mon, 26 Feb 2024 19:10:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B7B554401B9; Mon, 26 Feb 2024 14:10:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B29CC44017F; Mon, 26 Feb 2024 14:10:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 97C4B4401B9; Mon, 26 Feb 2024 14:10:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 848C144017F for ; Mon, 26 Feb 2024 14:10:08 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 063F4A0B75 for ; Mon, 26 Feb 2024 19:10:08 +0000 (UTC) X-FDA: 81834895296.14.A7A8028 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf07.hostedemail.com (Postfix) with ESMTP id 2CE6F40008 for ; Mon, 26 Feb 2024 19:10:05 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jOT4lAGg; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708974606; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NFCSdAhrunIzMrgwpPIPHm0AfcuiyWMeHA0mu0m/29Q=; b=a6QZyl+KvTBm8CpllZJf8DzhdTEoL5FVezVDSn+sw5EmLnY8QP/PPKx0U/oud3nO7Ue+lF ZuL6pP9R7i9AdYOj7L4GNxg9UDGP7gAmmQbeq2H0RpXHtT93nyKON2L8pII/1Ypv/P9H+R 6kwr/JRJo1aoyVp+e6r3Jmfvcb8LzkY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974606; a=rsa-sha256; cv=none; b=HXZquSsz+Gn6ihXoA0mkVdeq+GaB7tfhTphya1XpUsGCGXaYpKGvOCSLOCXoCiqJh9yna6 lP8k/4yp63hUc2qoGhERPxJV9xe5Na8yItPHw/VW6fXix2Yj+B6OQ4gBDAF5eydYGMgpJz VlbzfskcTO/yuODLzv/e/Jvi5m97S9o= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jOT4lAGg; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974606; x=1740510606; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9Kz1B1g9YBWVQVBA7u5n8sBfRhBddgPHEWEy/INdrdY=; b=jOT4lAGgGPJSBIobtFG6DfkeDJDgCH9S1EcXTIpJpCTFNDri0NJf14+l oPC5sVR5E5RLLYfdmLBOiYsmoyATTvnh96egcmJn1rs4CtG+HqLkL9LoI Ew0vQAn5R+LW7AZiyn3e+uS9d4p7Bodw01OY/SLZuZpC8ri/rTx2b/UQw CFhhdD6iMng2Q3j8kVxc3owxbgRLoZAv0834YLBEKOyz6Jbb74JZd5e4X uZRyrmcC/iC5QP/JwpQwR6pArw9vHT0u5acncF0VipYu1mBU4qGSLnMmr YNfkH2UkDydNgcScAwHoZLJRs2P5DKVzMu115buy2CjIZWaBV75v5NJJ7 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721441" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721441" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:06 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911475" Received: from bdmirand-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.3.213]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:04 -0800 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, kirill.shutemov@linux.intel.com, keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH v2 9/9] selftests/x86: Add placement guard gap test for shstk Date: Mon, 26 Feb 2024 11:09:51 -0800 Message-Id: <20240226190951.3240433-10-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> References: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2CE6F40008 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: demw59pe8na4o8n73iir6say53uxunqz X-HE-Tag: 1708974605-407686 X-HE-Meta: U2FsdGVkX1+R3bDPkRcfYFUda7o/IcWS5gS40RnNU6QdQE3rCTIoiSbG2NWIML+WEFdfUlxUHSGAgbhDbhruDk4uFcMiy95UY0CCXaDIiCLmQnZdaZq5cHF/Ctij77nYppFW7rm/DmbS1N964A78imDOSLdvaWGTnpoPbrI+okQJCxPsoETsGrHCgrkseiHjSe8zZgutEOSj505NG7MUEJqNmgBcatqHk2W4fQk3/su2lOX/fvSFynfWKOvVTLViXR4KfDZYRzMTQBOEsTec6BYrniZPxoxiBFmm+CiyhlS9awxOY2oxE2U0iSjxMwyo/Nvi0JOfVmKAxl6AjCAhPcl8yAsduZOHDuDmm/qndwylTCPgx9f5x1itSdfE5Axg6IPBuEsRQy+voxT6S9gkJICa6bXtck/3LjOLugiDpp67/d3FWrSO6o5vviXjBr8m+zdn6gYE59Ey06W2odKeM4wtBED3t3d73EhNNZxaG/sY0Izc6ZYxWVzptx1ETiaTxErqEnFjPpkPWIyTgywOmE6wmzcdI0B/Gwpkygthetni6tBxIRh3JiWLrdr3UsI8z/hJKvTc55EZA7xzV/pV1k9Q9PIV2726uK9H9Kv1LzNPVkP3JnYourHmG7/6fuU1Bihvm/82dRNGaG01QpBJ09NKV4NCIDmQLYWVRMxtaf85/ejs/lrBiTnRuKCupJ9lD8DQZ0RiOuRH9ovg+dlk8BD+TWZrk8HEnAlnQz8bt1tixWJ3bIGPP+i1O8Zawl58teMeLIX+/wQWL9AwC5BJTo2xy7xCClEM5NoT6WtNAOyYZTpS5+A1B6w/mw6FvD3tay1ZRetD08/43n3ESurB4BJDc8v3jgJ9zTaaO0t1MzSpR/hILKQ24qXEkbCCef9u 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: List-Subscribe: List-Unsubscribe: The existing shadow stack test for guard gaps just checks that new mappings are not placed in an existing mapping's guard gap. Add one that checks that new mappings are not placed such that preexisting mappings are in the new mappings guard gap. Signed-off-by: Rick Edgecombe --- .../testing/selftests/x86/test_shadow_stack.c | 67 +++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/x86/test_shadow_stack.c b/tools/testing/selftests/x86/test_shadow_stack.c index 757e6527f67e..ee909a7927f9 100644 --- a/tools/testing/selftests/x86/test_shadow_stack.c +++ b/tools/testing/selftests/x86/test_shadow_stack.c @@ -556,7 +556,7 @@ struct node { * looked at the shadow stack gaps. * 5. See if it landed in the gap. */ -int test_guard_gap(void) +int test_guard_gap_other_gaps(void) { void *free_area, *shstk, *test_map = (void *)0xFFFFFFFFFFFFFFFF; struct node *head = NULL, *cur; @@ -593,11 +593,64 @@ int test_guard_gap(void) if (shstk - test_map - PAGE_SIZE != PAGE_SIZE) return 1; - printf("[OK]\tGuard gap test\n"); + printf("[OK]\tGuard gap test, other mapping's gaps\n"); return 0; } +/* Tests respecting the guard gap of the mapping getting placed */ +int test_guard_gap_new_mappings_gaps(void) +{ + void *free_area, *shstk_start, *test_map = (void *)0xFFFFFFFFFFFFFFFF; + struct node *head = NULL, *cur; + int ret = 0; + + free_area = mmap(0, PAGE_SIZE * 4, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + munmap(free_area, PAGE_SIZE * 4); + + /* Test letting map_shadow_stack find a free space */ + shstk_start = mmap(free_area, PAGE_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (shstk_start == MAP_FAILED || shstk_start != free_area) + return 1; + + while (test_map > shstk_start) { + test_map = (void *)syscall(__NR_map_shadow_stack, 0, PAGE_SIZE, 0); + if (test_map == MAP_FAILED) { + printf("[INFO]\tmap_shadow_stack MAP_FAILED\n"); + ret = 1; + break; + } + + cur = malloc(sizeof(*cur)); + cur->mapping = test_map; + + cur->next = head; + head = cur; + + if (test_map == free_area + PAGE_SIZE) { + printf("[INFO]\tNew mapping has other mapping in guard gap!\n"); + ret = 1; + break; + } + } + + while (head) { + cur = head; + head = cur->next; + munmap(cur->mapping, PAGE_SIZE); + free(cur); + } + + munmap(shstk_start, PAGE_SIZE); + + if (!ret) + printf("[OK]\tGuard gap test, placement mapping's gaps\n"); + + return ret; +} + /* * Too complicated to pull it out of the 32 bit header, but also get the * 64 bit one needed above. Just define a copy here. @@ -850,9 +903,15 @@ int main(int argc, char *argv[]) goto out; } - if (test_guard_gap()) { + if (test_guard_gap_other_gaps()) { ret = 1; - printf("[FAIL]\tGuard gap test\n"); + printf("[FAIL]\tGuard gap test, other mappings' gaps\n"); + goto out; + } + + if (test_guard_gap_new_mappings_gaps()) { + ret = 1; + printf("[FAIL]\tGuard gap test, placement mapping's gaps\n"); goto out; }