From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02ED94E1A2 for ; Tue, 26 Mar 2024 02:17:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419437; cv=none; b=k3rzAoQAmNXbAjaKnF1+mism4nXyMYEJlUfOLs5lNqyj5S/EniaaRh8TEFS5KEfRx3m719CO2TxKnnhp6geNT4NSU4hJiJ/sIem6uWqtpp7fYke1kJcROxz3/cmDxXYdLCqfQcen1WRibl2FMKIvlXSU4Vm+eZo9v24jIi0kD3o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419437; c=relaxed/simple; bh=C3HKCFW/8Pu4EQqUdufuRIZx47fZhhZskIiwq7U/w8E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZQgDLVX1DK3KmyOX7HQdzoqGOlv2eER1qqTOv6mODTDRTF2iMAivaF2Te4kIhuQiTXaT1WwtiprtPvHiArKTMnaSGxjKFeZJXq8pPqeBsy/JdtV1NxB8IuUAblozJWkKvddKvjohlWQYz8uX2Jhjo9kmSR5CnZBqLaCGcexYXy8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Lzw0SOWH; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Lzw0SOWH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419436; x=1742955436; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=C3HKCFW/8Pu4EQqUdufuRIZx47fZhhZskIiwq7U/w8E=; b=Lzw0SOWHPNiaX558/E/Fv/meWpEiMiI4vWXV1oPMJi7s7+J1HrkdG084 jm5UUmaYHhhCt/P9E2PIlLGzeADwYC8snAlSnM05IgaDHTroinMlyfoUz IGj0g/GUDEbWw0N+K5b3BG/S2seaCDPuDZwfaPpYH6XxAB0nPoJU6XI6f Ui8/kBvOBNqK9e+s2i7oEMXEmpE3ARAIawl5kSLQK3F/GiVj6C7YjbdmX b6L8YC7eSYZRjzp2jFNyb9vvEEFOdydKz9ITwVmCGkR0ZlFdhLp1gp4Kr fY94pivLXghVS8HLOiGLzWlWnezpAgQ/mnmSOgtOnQFn0HNT1QuTlJn3K w==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564205" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564205" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489864" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:14 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 01/14] proc: Refactor pde_get_unmapped_area as prep Date: Mon, 25 Mar 2024 19:16:43 -0700 Message-Id: <20240326021656.202649-2-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Future changes will perform a treewide change to remove the indirect branch that is involved in calling mm->get_unmapped_area(). After doing this, the function will no longer be able to be handled as a function pointer. To make the treewide change diff cleaner and easier to review, refactor pde_get_unmapped_area() such that mm->get_unmapped_area() is called without being stored in a local function pointer. With this in refactoring, follow on changes will be able to simply replace the call site with a future function that calls it directly. Signed-off-by: Rick Edgecombe --- v4: - New patch split from "mm: Switch mm->get_unmapped_area() to a flag" (Christophe Leroy) --- fs/proc/inode.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/fs/proc/inode.c b/fs/proc/inode.c index dcd513dccf55..75396a24fd8c 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -451,15 +451,12 @@ pde_get_unmapped_area(struct proc_dir_entry *pde, str= uct file *file, unsigned lo unsigned long len, unsigned long pgoff, unsigned long flags) { - typeof_member(struct proc_ops, proc_get_unmapped_area) get_area; + if (pde->proc_ops->proc_get_unmapped_area) + return pde->proc_ops->proc_get_unmapped_area(file, orig_addr, len, pgoff= , flags); =20 - get_area =3D pde->proc_ops->proc_get_unmapped_area; #ifdef CONFIG_MMU - if (!get_area) - get_area =3D current->mm->get_unmapped_area; + return current->mm->get_unmapped_area(file, orig_addr, len, pgoff, flags); #endif - if (get_area) - return get_area(file, orig_addr, len, pgoff, flags); return orig_addr; } =20 --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE42C4E1A8; Tue, 26 Mar 2024 02:17:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419440; cv=none; b=eaVfLLZ/k/BuoMkDF5iibIE+MCRvKB2wCHXhaQi5IevXb4DrtN1dp1SdsQZ6XJe+5lR6ru+nJBdZby3qyBws2hV3AsfuFLo30ScRLHKaXqruTvpZ9hw1dNFQcrGk56Lt2kvMsPjdixARwpWMghnO6eJmKGKL0Hd8v/lCuaXf5DE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419440; c=relaxed/simple; bh=vkTcVT9jigW1nbd2RmwlNu3cwMbNkxnWB23GTD8rbXw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=d2SNuDlwEJQyGnkswYsSOjCHsuYLSlen/MQbeeGYi7WQzcrwZUNihvfesdJ0vBkYGTYrsAGDiHG105byL1sO8xcl0WIqdmAhGRbo6pSn7uKnQ/kqpJ9E+4ulXD/Pn8GJYexSVNo1ocbkgarmB74j7HSB4ZUuuq2ua4+Xj1ct+rg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=aJ1aYul0; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="aJ1aYul0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419438; x=1742955438; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vkTcVT9jigW1nbd2RmwlNu3cwMbNkxnWB23GTD8rbXw=; b=aJ1aYul0ARnSVVv04Avty7gYVlb6Kt8cEy2YEST7D+qRn2PdhsjoyruD uh7Xi1Bfh2+fqKcXTFGfr/OQV+cFtt1TE8AxwdRZ3BIac0wKYdb1z91Ii mhikUe5ea0OFn8BZqufIbx6Lpp+TVk767NK5h6nz5eZscpm1IF2MP8TO2 PI5vEjKKAJiYc+ZDbOdv8UTTz3A9hny2Z5T4NthO0xrzUNlOrDwSwiffS rj+ubmWmLwc6EAzcjhZbqAtSq2jX8F8RvfNrXD7apv5Dj3aJFbXzNzKJt wljm9fO9eIktPttrKPQA5GCT6hL5ZgQCoVXFKApA4ChjQ/VCTOxCe9rO7 A==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564218" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564218" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489867" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:14 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, linux-sgx@vger.kernel.org, nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH v4 02/14] mm: Switch mm->get_unmapped_area() to a flag Date: Mon, 25 Mar 2024 19:16:44 -0700 Message-Id: <20240326021656.202649-3-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 inheriting 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 Cc: linux-s390@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: sparclinux@vger.kernel.org Cc: linux-sgx@vger.kernel.org Cc: nvdimm@lists.linux.dev Cc: linux-cxl@vger.kernel.org Cc: linux-mm@kvack.org Cc: linux-fsdevel@vger.kernel.org Cc: io-uring@vger.kernel.org Cc: bpf@vger.kernel.org Acked-by: Alexei Starovoitov --- v4: - Split out pde_get_unmapped_area() refactor into separate patch (Christophe Leroy) v3: - Fix comment that still referred to mm->get_unmapped_area() - Resolve trivial rebase conflicts with "mm: thp_get_unmapped_area must honour topdown preference" - Spelling fix in log 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 | 4 ++-- fs/proc/inode.c | 3 ++- 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 +- kernel/bpf/arena.c | 2 +- kernel/bpf/syscall.c | 2 +- mm/debug.c | 6 ------ mm/huge_memory.c | 9 ++++----- mm/mmap.c | 21 ++++++++++++++++++--- mm/shmem.c | 11 +++++------ mm/util.c | 6 +++--- 23 files changed, 66 insertions(+), 57 deletions(-) diff --git a/arch/s390/mm/hugetlbpage.c b/arch/s390/mm/hugetlbpage.c index c2e8242bd15d..219d906fe830 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 *fi= le, unsigned long addr, goto check_asce_limit; } =20 - if (mm->get_unmapped_area =3D=3D arch_get_unmapped_area) + if (!test_bit(MMF_TOPDOWN, &mm->flags)) addr =3D 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 b14fc0887654..6b2e4436ad4a 100644 --- a/arch/s390/mm/mmap.c +++ b/arch/s390/mm/mmap.c @@ -185,10 +185,10 @@ void arch_pick_mmap_layout(struct mm_struct *mm, stru= ct rlimit *rlim_stack) */ if (mmap_is_legacy(rlim_stack)) { mm->mmap_base =3D mmap_base_legacy(random_factor); - mm->get_unmapped_area =3D arch_get_unmapped_area; + clear_bit(MMF_TOPDOWN, &mm->flags); } else { mm->mmap_base =3D mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area =3D arch_get_unmapped_area_topdown; + set_bit(MMF_TOPDOWN, &mm->flags); } } =20 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, con= st unsigned long addr0, unsigned long get_fb_unmapped_area(struct file *filp, unsigned long orig_a= ddr, unsigned long len, unsigned long pgoff, unsigned long flags) { unsigned long align_goal, addr =3D -ENOMEM; - unsigned long (*get_area)(struct file *, unsigned long, - unsigned long, unsigned long, unsigned long); - - get_area =3D current->mm->get_unmapped_area; =20 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, fl= ags); } flags &=3D ~MAP_SHARED; =20 @@ -238,7 +234,8 @@ unsigned long get_fb_unmapped_area(struct file *filp, u= nsigned long orig_addr, u align_goal =3D (64UL * 1024); =20 do { - addr =3D get_area(NULL, orig_addr, len + (align_goal - PAGE_SIZE), pgoff= , flags); + addr =3D mm_get_unmapped_area(current->mm, NULL, orig_addr, + len + (align_goal - PAGE_SIZE), pgoff, flags); if (!(addr & ~PAGE_MASK)) { addr =3D (addr + (align_goal - 1UL)) & ~(align_goal - 1UL); break; @@ -256,7 +253,7 @@ unsigned long get_fb_unmapped_area(struct file *filp, u= nsigned long orig_addr, u * be obtained. */ if (addr & ~PAGE_MASK) - addr =3D get_area(NULL, orig_addr, len, pgoff, flags); + addr =3D mm_get_unmapped_area(current->mm, NULL, orig_addr, len, pgoff, = flags); =20 return addr; } @@ -292,7 +289,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm, struct= rlimit *rlim_stack) gap =3D=3D RLIM_INFINITY || sysctl_legacy_va_layout) { mm->mmap_base =3D TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area =3D arch_get_unmapped_area; + clear_bit(MMF_TOPDOWN, &mm->flags); } else { /* We know it's 32-bit */ unsigned long task_size =3D STACK_TOP32; @@ -303,7 +300,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm, struct= rlimit *rlim_stack) gap =3D (task_size / 6 * 5); =20 mm->mmap_base =3D PAGE_ALIGN(task_size - gap - random_factor); - mm->get_unmapped_area =3D arch_get_unmapped_area_topdown; + set_bit(MMF_TOPDOWN, &mm->flags); } } =20 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 l= ong addr, (!vma || addr + len <=3D vm_start_gap(vma))) return addr; } - if (mm->get_unmapped_area =3D=3D 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/dri= ver.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; =20 - return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); + return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags); } =20 #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 l= ong addr, } =20 get_unmapped_area: - if (mm->get_unmapped_area =3D=3D 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, un= signed long *legacy_base, void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) { if (mmap_is_legacy()) - mm->get_unmapped_area =3D arch_get_unmapped_area; + clear_bit(MMF_TOPDOWN, &mm->flags); else - mm->get_unmapped_area =3D arch_get_unmapped_area_topdown; + set_bit(MMF_TOPDOWN, &mm->flags); =20 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, } =20 /* 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 fil= e *filp, if ((off + len_align) < off) goto out; =20 - addr_align =3D current->mm->get_unmapped_area(filp, addr, len_align, - pgoff, flags); + addr_align =3D mm_get_unmapped_area(current->mm, filp, addr, len_align, + pgoff, flags); if (!IS_ERR_VALUE(addr_align)) { addr_align +=3D (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); } =20 static const struct address_space_operations dev_dax_aops =3D { diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 6502c7e776d1..3dee18bf47ed 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -249,11 +249,11 @@ generic_hugetlb_get_unmapped_area(struct file *file, = unsigned long addr, } =20 /* - * Use mm->get_unmapped_area value as a hint to use topdown routine. + * Use MMF_TOPDOWN flag as a hint to use topdown routine. * If architectures have special needs, they should define their own * version of hugetlb_get_unmapped_area. */ - if (mm->get_unmapped_area =3D=3D 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 75396a24fd8c..d19434e2a58e 100644 --- a/fs/proc/inode.c +++ b/fs/proc/inode.c @@ -455,8 +455,9 @@ pde_get_unmapped_area(struct proc_dir_entry *pde, struc= t file *file, unsigned lo return pde->proc_ops->proc_get_unmapped_area(file, orig_addr, len, pgoff= , flags); =20 #ifdef CONFIG_MMU - return current->mm->get_unmapped_area(file, orig_addr, len, pgoff, flags); + return mm_get_unmapped_area(current->mm, file, orig_addr, len, pgoff, fla= gs); #endif + return orig_addr; } =20 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 f= ile *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); } =20 const struct file_operations ramfs_file_operations =3D { diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 5240bd7bca33..9313e43123d4 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -777,11 +777,7 @@ struct mm_struct { } ____cacheline_aligned_in_smp; =20 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 alloca= tions */ #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) =20 +#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) =20 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 b6543f9d78d6..ed1caa26c8be 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -8,6 +8,7 @@ #include #include #include +#include =20 /* * Routines for handling mm_structs @@ -186,6 +187,10 @@ arch_get_unmapped_area_topdown(struct file *filp, unsi= gned long addr, unsigned long len, unsigned long pgoff, unsigned long flags); =20 +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 5d4b448fdc50..405bab0a560c 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -3520,7 +3520,7 @@ static unsigned long io_uring_mmu_get_unmapped_area(s= truct file *filp, #else addr =3D 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); } =20 #else /* !CONFIG_MMU */ diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index 86571e760dd6..74d566dcd2cb 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -314,7 +314,7 @@ static unsigned long arena_get_unmapped_area(struct fil= e *filp, unsigned long ad return -EINVAL; } =20 - ret =3D current->mm->get_unmapped_area(filp, addr, len * 2, 0, flags); + ret =3D mm_get_unmapped_area(current->mm, filp, addr, len * 2, 0, flags); if (IS_ERR_VALUE(ret)) return ret; if ((ret >> 32) =3D=3D ((ret + len - 1) >> 32)) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index ae2ff73bde7e..dead5e1977d8 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -980,7 +980,7 @@ static unsigned long bpf_get_unmapped_area(struct file = *filp, unsigned long addr if (map->ops->map_get_unmapped_area) return map->ops->map_get_unmapped_area(filp, addr, len, pgoff, flags); #ifdef CONFIG_MMU - return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags); + return mm_get_unmapped_area(current->mm, filp, addr, len, pgoff, flags); #else return addr; #endif diff --git a/mm/debug.c b/mm/debug.c index c1c1a6a484e4..37a17f77df9f 100644 --- a/mm/debug.c +++ b/mm/debug.c @@ -180,9 +180,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" @@ -208,9 +205,6 @@ void dump_mm(const struct mm_struct *mm) "def_flags: %#lx(%pGv)\n", =20 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 9859aa4f7553..cede9ccb84dc 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -824,8 +824,8 @@ static unsigned long __thp_get_unmapped_area(struct fil= e *filp, if (len_pad < len || (off + len_pad) < off) return 0; =20 - ret =3D current->mm->get_unmapped_area(filp, addr, len_pad, - off >> PAGE_SHIFT, flags); + ret =3D mm_get_unmapped_area(current->mm, filp, addr, len_pad, + off >> PAGE_SHIFT, flags); =20 /* * The failure might be due to length padding. The caller will retry @@ -843,8 +843,7 @@ static unsigned long __thp_get_unmapped_area(struct fil= e *filp, =20 off_sub =3D (off - ret) & (size - 1); =20 - if (current->mm->get_unmapped_area =3D=3D arch_get_unmapped_area_topdown = && - !off_sub) + if (test_bit(MMF_TOPDOWN, ¤t->mm->flags) && !off_sub) return ret + size; =20 ret +=3D off_sub; @@ -861,7 +860,7 @@ unsigned long thp_get_unmapped_area(struct file *filp, = unsigned long addr, if (ret) return ret; =20 - 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); =20 diff --git a/mm/mmap.c b/mm/mmap.c index 6dbda99a47da..224e9ce1e2fd 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1813,7 +1813,8 @@ get_unmapped_area(struct file *file, unsigned long ad= dr, 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) + =3D NULL; =20 unsigned long error =3D arch_mmap_check(addr, len, flags); if (error) @@ -1823,7 +1824,6 @@ get_unmapped_area(struct file *file, unsigned long ad= dr, unsigned long len, if (len > TASK_SIZE) return -ENOMEM; =20 - get_area =3D current->mm->get_unmapped_area; if (file) { if (file->f_op->get_unmapped_area) get_area =3D file->f_op->get_unmapped_area; @@ -1842,7 +1842,11 @@ get_unmapped_area(struct file *file, unsigned long a= ddr, unsigned long len, if (!file) pgoff =3D 0; =20 - addr =3D get_area(file, addr, len, pgoff, flags); + if (get_area) + addr =3D get_area(file, addr, len, pgoff, flags); + else + addr =3D mm_get_unmapped_area(current->mm, file, addr, len, + pgoff, flags); if (IS_ERR_VALUE(addr)) return addr; =20 @@ -1857,6 +1861,17 @@ get_unmapped_area(struct file *file, unsigned long a= ddr, unsigned long len, =20 EXPORT_SYMBOL(get_unmapped_area); =20 +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 in= terval * @mm: The process address space. diff --git a/mm/shmem.c b/mm/shmem.c index 0aad0d9a621b..4078c3a1b2d0 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2273,8 +2273,6 @@ unsigned long shmem_get_unmapped_area(struct file *fi= le, 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; @@ -2284,8 +2282,8 @@ unsigned long shmem_get_unmapped_area(struct file *fi= le, if (len > TASK_SIZE) return -ENOMEM; =20 - get_area =3D current->mm->get_unmapped_area; - addr =3D get_area(file, uaddr, len, pgoff, flags); + addr =3D mm_get_unmapped_area(current->mm, file, uaddr, len, pgoff, + flags); =20 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) return addr; @@ -2342,7 +2340,8 @@ unsigned long shmem_get_unmapped_area(struct file *fi= le, if (inflated_len < len) return addr; =20 - inflated_addr =3D get_area(NULL, uaddr, inflated_len, 0, flags); + inflated_addr =3D 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) @@ -4807,7 +4806,7 @@ unsigned long shmem_get_unmapped_area(struct file *fi= le, 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 =20 diff --git a/mm/util.c b/mm/util.c index 669397235787..8619d353a1aa 100644 --- a/mm/util.c +++ b/mm/util.c @@ -469,17 +469,17 @@ void arch_pick_mmap_layout(struct mm_struct *mm, stru= ct rlimit *rlim_stack) =20 if (mmap_is_legacy(rlim_stack)) { mm->mmap_base =3D TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area =3D arch_get_unmapped_area; + clear_bit(MMF_TOPDOWN, &mm->flags); } else { mm->mmap_base =3D mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area =3D 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 =3D TASK_UNMAPPED_BASE; - mm->get_unmapped_area =3D arch_get_unmapped_area; + clear_bit(MMF_TOPDOWN, &mm->flags); } #endif =20 --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00D3A4EB22 for ; Tue, 26 Mar 2024 02:17:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419439; cv=none; b=kcCfrw7/Vuxo3TBHpL+hx2kA5zJie+rDZMf1ig/G2w144I1yLvCrJZdymjWAAb42ZPtauT/RTwuuvKRjMCVy+DLN+G66d32nKvh3aBtb3F2CBL1G1FhRr/S7ZkuSte5+knTKMCt9Nc3ilYAJVUIu07+gJXfgLTrHXK0PZMs4qvU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419439; c=relaxed/simple; bh=y5DokqN/WlvuTFZmqP1zrzAjZ8lFdRvaKiOQWgxpins=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=k6AtYfXvr7IAqOIEJosQfE/AkkcGOLu+56cVIdbpUSw/GtiSSmf2M4TJLBEMJIUhI553nHuj6SMGBTyo9piMSGmaZsbCbjJ6mWzqPBK+GxwOODlOm+wJinuSgNjDz+aoBore+9poxnyG6Oknk3mNpMI9sdBS+LdXfNpQOFcA4+s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ni2KB+jk; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ni2KB+jk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419438; x=1742955438; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=y5DokqN/WlvuTFZmqP1zrzAjZ8lFdRvaKiOQWgxpins=; b=ni2KB+jk9TIRHBle9lEIlTJgLmrMItVkCE9hmcONN8yPSWNvDmD2/l+Y bMslvnC6Yc6nEI/ICi4vPOs7b3QzKPtlVWdXM02zbveTyddw+jn0/cb2P SJySuTSuabaGjXgtsRmtBhr6H6L6KOUcz78et9SM3na9JV6eJHUpcSh2P iIP8X4dzsglILFhDBMIzTz9CEcM+2HXKNeQDq4BUSwKpNpHWZ8dFluFO2 OKGIaqHPpzWNIFPdEQ7oZbM+PHa/p626tD7LFu8g7wlnldb+ziFgtROOA wJRC2w8od4N43A850M3mquT98H7it3DJp4AAphJppPebr2skOULPJlk5U g==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564243" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564243" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489872" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:14 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 03/14] mm: Introduce arch_get_unmapped_area_vmflags() Date: Mon, 25 Mar 2024 19:16:45 -0700 Message-Id: <20240326021656.202649-4-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=E2=80=99t placed in an any existing mappings g= uard gaps. 2. That the new mapping isn=E2=80=99t placed such that any existing mappin= gs 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 --- v4: - Remove externs (Christophe Leroy) --- 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 ed1caa26c8be..91546493c43d 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 *m= m, struct file *filp, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags); =20 +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); +unsigned long +arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long ad= dr, + 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 224e9ce1e2fd..2bd7580b8f0b 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1808,6 +1808,34 @@ arch_get_unmapped_area_topdown(struct file *filp, un= signed long addr, } #endif =20 +#ifndef HAVE_ARCH_UNMAPPED_AREA_VMFLAGS +unsigned long +arch_get_unmapped_area_vmflags(struct file *filp, unsigned long addr, unsi= gned long len, + unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) +{ + return arch_get_unmapped_area(filp, addr, len, pgoff, flags); +} + +unsigned long +arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long ad= dr, + 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 fi= le *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_f= lags); +} + unsigned long get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02DAF524D8 for ; Tue, 26 Mar 2024 02:17:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419439; cv=none; b=uA6WZkFgQMiBk6SqKSb6vtzxN+4xbc6xaY2MBnfBwyH6YWqAUadUBTPB6gBCkgleTjs504Ffzg993ZSjYTRWrPyBUbMle4mhxK17pSY5jIRw6UUbJbIKbJu9/KImxRDDwwExE2qCSa+f7daZVi+3dCP/MCYiVvQhc+3l2iqAWsU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419439; c=relaxed/simple; bh=naXFIdNU33tz38vUXdohRFxUFMIrzK3pZH6Hd6XFg2c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Q1w2wThnCnExK4gxAQqOQu4IE10br4wKfCy0F2uPXoxNDSxK+ATur/qwrUZyPa+Bn5StpA4et8VDDAeH+0cKnm8e0RuECI2f6G4hrsT17mdHOxl+vaf9P/E3+G4r/1W2lE+2TRuI7MJGSjBC2vDWHB7aKiXB40naYZBT84iECPM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=UoMGkBH0; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="UoMGkBH0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419438; x=1742955438; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=naXFIdNU33tz38vUXdohRFxUFMIrzK3pZH6Hd6XFg2c=; b=UoMGkBH0uyS2VTPaOfQNePoojtX6b6tOhiPrnTlyKTpqQyxJAct16hjC UxqOxwDvuxRkqQJaKtjDmfOzUYBwjkUZcvH8rxAjGl5T30Q0cZ7B4ieAf F7tzGnXLtoPMJndRVMQgvleUfHLAg2GmFzevk+1LKXLz1RTYag4ZKmuks dyCLCMj/J0HexByKOFa6funmPMKnTvEIodD7T1nty9P8PQX9dmK4EPFY6 HVnzcX3fB9HPwOW7wpIQAadRJlfUZrPpkDdpheUjE2FKUaemcoWifNFKu sFYq/fDhLppEL4r4wOZhNISaagNc/q+HLe5x8rj6HjcVVWvSU2xgbGKrk w==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564244" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564244" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489877" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:15 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 04/14] mm: Remove export for get_unmapped_area() Date: Mon, 25 Mar 2024 19:16:46 -0700 Message-Id: <20240326021656.202649-5-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The mm/mmap.c function get_unmapped_area() is not used from any modules, so it doesn't need to be exported. Remove the export. Signed-off-by: Rick Edgecombe --- v4: - New patch split from "mm: Use get_unmapped_area_vmflags()" (Christophe Leroy) --- mm/mmap.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 2bd7580b8f0b..d160e88b1b1e 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1887,8 +1887,6 @@ get_unmapped_area(struct file *file, unsigned long ad= dr, unsigned long len, return error ? error : addr; } =20 -EXPORT_SYMBOL(get_unmapped_area); - unsigned long mm_get_unmapped_area(struct mm_struct *mm, struct file *file, unsigned long addr, unsigned long len, --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1316839E8 for ; Tue, 26 Mar 2024 02:17:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419441; cv=none; b=BfBw3L3QDMDYNlPs1jjpx4sJEyVhWREZWp3tkrTwTLCaUop5CPdq3rRhCn+wheoDmfppecoV6ev26P8WuNJqm8wCd31e3t+FXOrl2tdmzDXoG0yyD3NfsBF3xaHV1euUluD4sCOOCcd9SlMAXiIUqPRgI+TxeQmqQxvmyV014ws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419441; c=relaxed/simple; bh=Bt/iCncZprnekNGl7/DyAAsQ7Fn3bf5rVZDGMJUr1K4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=GdKhC9qzbMH6UQQUElYru9QIgGeqZtJL6m4mTZqS37XW+JvhKLPbGmQnMOIRzCZ+g5wI4YoGcbOVBgTmsQ2Z7ouV9GTDgwTajhsGxhXG/2gMlwhektgIpM3ZN26KQOO2rMbyMO6OSD0QeaxifksB59eMUi+MADmjo83Xw6zzQtA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Lkdm4LcS; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Lkdm4LcS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419440; x=1742955440; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Bt/iCncZprnekNGl7/DyAAsQ7Fn3bf5rVZDGMJUr1K4=; b=Lkdm4LcSQ5+9KGpu5/rYceOBiYWxnVvcdj983C6t4hHrgvPy52S8hppp ShSSHkzPlmtffuGvMA5Y2yqmvKVneKJi3aZXQyHeXMV7AVmJKpTd8Edbw IUG8HeZKMZin24gbYVjFQaEEgQACl2ZaPpHDwh4IYgPSeqFKFEKkPs2zC IrNAsApCC4QMhFlk7MUjdOoDRWgf7VU6NcsBocMbv0DzEPMIhOIRb13qX 5/QS2Bv7dYDAqxLDKEHgcmMmKmIw//Cye/TlbgS7dNWed0OHggcEWcIi3 I+Ni6A154JB8l8AcLMhYWdHuk/ByR1wtwnmtVmpFlsCfI1kqw6cFiT5ZP w==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564259" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564259" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489883" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:15 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 05/14] mm: Use get_unmapped_area_vmflags() Date: Mon, 25 Mar 2024 19:16:47 -0700 Message-Id: <20240326021656.202649-6-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=E2=80=99t placed in an any existing mappings g= uard gaps. 2. That the new mapping isn=E2=80=99t placed such that any existing mappin= gs 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 Reviewed-by: Christophe Leroy --- v4: - Split removal of get_unmapped_area() export into a separate patch (Christophe Leroy) v2: - Make get_unmapped_area() a static inline (Kirill) --- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 32 ++++++++++++++++---------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 0436b919f1c7..8b13cd891b53 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3383,7 +3383,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); =20 -extern unsigned long get_unmapped_area(struct file *, unsigned long, unsig= ned long, unsigned long, unsigned long); +unsigned long +__get_unmapped_area(struct file *file, unsigned long addr, unsigned long l= en, + 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); +} =20 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 d160e88b1b1e..68b5bfcebadd 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1255,18 +1255,6 @@ unsigned long do_mmap(struct file *file, unsigned lo= ng addr, if (mm->map_count > sysctl_max_map_count) return -ENOMEM; =20 - /* Obtain the address to map to. we verify (or select) it and ensure - * that it represents a valid section of the address space. - */ - addr =3D 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 =3D=3D PROT_EXEC) { pkey =3D execute_only_pkey(mm); if (pkey < 0) @@ -1280,6 +1268,18 @@ unsigned long do_mmap(struct file *file, unsigned lo= ng addr, vm_flags |=3D calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; =20 + /* Obtain the address to map to. we verify (or select) it and ensure + * that it represents a valid section of the address space. + */ + addr =3D __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; @@ -1837,8 +1837,8 @@ unsigned long mm_get_unmapped_area_vmflags(struct mm_= struct *mm, struct file *fi } =20 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 l= en, + 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) @@ -1873,8 +1873,8 @@ get_unmapped_area(struct file *file, unsigned long ad= dr, unsigned long len, if (get_area) addr =3D get_area(file, addr, len, pgoff, flags); else - addr =3D mm_get_unmapped_area(current->mm, file, addr, len, - pgoff, flags); + addr =3D mm_get_unmapped_area_vmflags(current->mm, file, addr, len, + pgoff, flags, vm_flags); if (IS_ERR_VALUE(addr)) return addr; =20 --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EA33C83CBE for ; Tue, 26 Mar 2024 02:17:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419441; cv=none; b=bzh4A5FikK3VCPFXBx01WB2BIoNJ38TnI9E3Qc1AfFkuM+yD3arB4li7GvR5GyzhtYNhKJOpM/zyJLPVza5C29K9Rro5OaBv5d0ETxRArEPfjiJf5LIlMUk6l6/CMlKXKn8XHBY5O5KeT1a8/hkeJmWr1bF+6Vpn0aqJUWodHTo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419441; c=relaxed/simple; bh=lEMD/P3qLo8sANIiWTYyDSHpg8TQh31/i79RKWObRQk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=qBwNmYhWcVTy2/kF6XNbvzxmG6wsFGeofUV5uZq2KEZAyo7SNKANV/EZ5E0SnepiUR/An7+2v73A0WFaim5Q95jisRieg9ezra3qD1XD7IuU/7CpNeizst7/cEtvQxz4qVLPf+d4V/NeWB1FEpgGq9oA5c60JeolF0cawgD+RJw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ENDzvvFT; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ENDzvvFT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419440; x=1742955440; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=lEMD/P3qLo8sANIiWTYyDSHpg8TQh31/i79RKWObRQk=; b=ENDzvvFTmcuGILvodFjMByE/RdSGTrACDm3U5tOd03uYjtAUA/2t1vs5 h2Ahrlbm+P5gHzVlQk07Go74UGUTrSbyzqF7R9EryGpB2jpZKeOGw9kdk kN4JVuPu+YgeFv2x3iRK3Shn2J1VJR9qLQG1Xcs78yhYxA6ZSfqXOPGIa Q8zy37sijVR2aE6mpImDfbEkmBT+or628AEFpQ4r53W6+rD1lQRRyeenx xQbOX+1MLdd4l1/WKw7SzlxSD7OEequQCp7s0sEMo7MM6YH/C1rsUkF/N St85FZNOoghwua85+gTtf9bP0s3EOg0L1RNjsHRs0tOuD66lK62icyddT g==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564273" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564273" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489886" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:15 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 06/14] thp: Add thp_get_unmapped_area_vmflags() Date: Mon, 25 Mar 2024 19:16:48 -0700 Message-Id: <20240326021656.202649-7-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=E2=80=99t placed in an any existing mappings g= uard gaps. 2. That the new mapping isn=E2=80=99t placed such that any existing mappin= gs 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 Reviewed-by: Christophe Leroy --- 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 de0c89105076..cc599de5e397 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -262,6 +262,9 @@ unsigned long thp_vma_allowable_orders(struct vm_area_s= truct *vma, =20 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 lo= ng addr, + unsigned long len, unsigned long pgoff, unsigned long flags, + vm_flags_t vm_flags); =20 void folio_prep_large_rmappable(struct folio *folio); bool can_split_folio(struct folio *folio, int *pextra_pins); @@ -417,6 +420,14 @@ static inline void folio_prep_large_rmappable(struct f= olio *folio) {} =20 #define thp_get_unmapped_area NULL =20 +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 cede9ccb84dc..b29f3e456888 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -808,7 +808,8 @@ static inline bool is_transparent_hugepage(struct folio= *folio) =20 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 =3D off + len; loff_t off_align =3D round_up(off, size); @@ -824,8 +825,8 @@ static unsigned long __thp_get_unmapped_area(struct fil= e *filp, if (len_pad < len || (off + len_pad) < off) return 0; =20 - ret =3D mm_get_unmapped_area(current->mm, filp, addr, len_pad, - off >> PAGE_SHIFT, flags); + ret =3D mm_get_unmapped_area_vmflags(current->mm, filp, addr, len_pad, + off >> PAGE_SHIFT, flags, vm_flags); =20 /* * The failure might be due to length padding. The caller will retry @@ -850,17 +851,25 @@ static unsigned long __thp_get_unmapped_area(struct f= ile *filp, return ret; } =20 -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 lo= ng addr, + unsigned long len, unsigned long pgoff, unsigned long flags, + vm_flags_t vm_flags) { unsigned long ret; loff_t off =3D (loff_t)pgoff << PAGE_SHIFT; =20 - ret =3D __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE); + ret =3D __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE, vm= _flags); if (ret) return ret; =20 - 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); =20 diff --git a/mm/mmap.c b/mm/mmap.c index 68b5bfcebadd..f734e4fa6d94 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1861,20 +1861,22 @@ __get_unmapped_area(struct file *file, unsigned lon= g addr, unsigned long len, * so use shmem's get_unmapped_area in case it can be huge. */ get_area =3D shmem_get_unmapped_area; - } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { - /* Ensures that larger anonymous mappings are THP aligned. */ - get_area =3D thp_get_unmapped_area; } =20 /* Always treat pgoff as zero for anonymous memory. */ if (!file) pgoff =3D 0; =20 - if (get_area) + if (get_area) { addr =3D get_area(file, addr, len, pgoff, flags); - else + } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { + /* Ensures that larger anonymous mappings are THP aligned. */ + addr =3D thp_get_unmapped_area_vmflags(file, addr, len, + pgoff, flags, vm_flags); + } else { addr =3D mm_get_unmapped_area_vmflags(current->mm, file, addr, len, pgoff, flags, vm_flags); + } if (IS_ERR_VALUE(addr)) return addr; =20 --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7DCD18526B; Tue, 26 Mar 2024 02:17:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419442; cv=none; b=hry8/MFVTts/urGtPg0wE4+baOw3OsKb/3Q/ubYDOTBZWemJG+EptCQczLv+2kAcJConcNZd/R0VaRFPfWy+5LnhRNKZnF2/qPylcmfPCTkjJpOeB+vsBXZpRjFEflyOyeFEShbI1f7ZzzsqZAne5tTEw6Cad+xRnsSDlpi8J7g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419442; c=relaxed/simple; bh=whLNPBFJ+q2+vc2R8+bRAmAZKOkjtqN6kDoBWWZgKCE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bZ6GsP1l8JkYSQbOXJZhO0AXaUHO5+rTxzyYhABTj7PGZGZi+c/6IuCN6PH4QByyKsC6t+htnXoDJCyux7GjJg/HTbqxKfue0U5csp6BiuzZLk4ad2X8woQYOCo8uDnbm3aFuRaakQmOwpGs27ng6FJ9IbbpxW9psX6B8BcgfCs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=mI1YHUW9; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="mI1YHUW9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419440; x=1742955440; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=whLNPBFJ+q2+vc2R8+bRAmAZKOkjtqN6kDoBWWZgKCE=; b=mI1YHUW9dZ1FKkX9i2CnJ8jbOOW0d8SI8FCjwEW1FycXl4/kQxqPiHQ+ C6eLYDBVVaqSfpH3E2mWtvi031zt18yserZfj4SlxqNs8I2p6OgR7cCdq 9u6DiHYT7g2/TVFthgLioP4BdhVrW4SFWRC4RZLgx/87SpWJi1l7AmXsa Pr++gJW9+H0DgidWyJwsKcoZKG7XAyrX2Y35KnzYZhkD7QAlpRjgRiSFW B1hVjb2pGS23QyHOvHl8CblzfrwSh72O4B5dYFIHT9HdKtYBB7X2ZS+TX DTFWEznjswhIfltL5KSZDuZagYBlZ6Vbmq+i5N73w/SKki7QtNTJndEg5 w==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564286" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564286" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489892" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:16 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Guo Ren , linux-csky@vger.kernel.org Subject: [PATCH v4 07/14] csky: Use initializer for struct vm_unmapped_area_info Date: Mon, 25 Mar 2024 19:16:49 -0700 Message-Id: <20240326021656.202649-8-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Future changes will need to add a new member to struct vm_unmapped_area_info. This would cause trouble for any call site that doesn't initialize the struct. Currently every caller sets each member manually, so if new members are added they will be uninitialized and the core code parsing the struct will see garbage in the new member. It could be possible to initialize the new member manually to 0 at each call site. This and a couple other options were discussed, and a working consensus (see links) was that in general the best way to accomplish this would be via static initialization with designated member initiators. Having some struct vm_unmapped_area_info instances not zero initialized will put those sites at risk of feeding garbage into vm_unmapped_area() if the convention is to zero initialize the struct and any new member addition misses a call site that initializes each member manually. It could be possible to leave the code mostly untouched, and just change the line: struct vm_unmapped_area_info info to: struct vm_unmapped_area_info info =3D {}; However, that would leave cleanup for the members that are manually set to zero, as it would no longer be required. So to be reduce the chance of bugs via uninitialized members, instead simply continue the process to initialize the struct this way tree wide. This will zero any unspecified members. Move the member initializers to the struct declaration when they are known at that time. Leave the members out that were manually initialized to zero, as this would be redundant for designated initializers. Signed-off-by: Rick Edgecombe Reviewed-by: Guo Ren Reviewed-by: Christophe Leroy Cc: Guo Ren Cc: linux-csky@vger.kernel.org Link: https://lore.kernel.org/lkml/202402280912.33AEE7A9CF@keescook/#t Link: https://lore.kernel.org/lkml/j7bfvig3gew3qruouxrh7z7ehjjafrgkbcmg6tcg= hhfh3rhmzi@wzlcoecgy5rs/ --- v3: - Fixed spelling errors in log - Be consistent about field vs member in log Hi, This patch was split and refactored out of a tree-wide change [0] to just zero-init each struct vm_unmapped_area_info. The overall goal of the series is to help shadow stack guard gaps. Currently, there is only one arch with shadow stacks, but two more are in progress. It is compile tested only. There was further discussion that this method of initializing the structs while nice in some ways has a greater risk of introducing bugs in some of the more complicated callers. Since this version was reviewed my arch maintainers already, leave it as was already acknowledged. Thanks, Rick [0] https://lore.kernel.org/lkml/20240226190951.3240433-6-rick.p.edgecombe@= intel.com/ --- arch/csky/abiv1/mmap.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/csky/abiv1/mmap.c b/arch/csky/abiv1/mmap.c index 6792aca49999..7f826331d409 100644 --- a/arch/csky/abiv1/mmap.c +++ b/arch/csky/abiv1/mmap.c @@ -28,7 +28,12 @@ arch_get_unmapped_area(struct file *filp, unsigned long = addr, struct mm_struct *mm =3D current->mm; struct vm_area_struct *vma; int do_align =3D 0; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D { + .length =3D len, + .low_limit =3D mm->mmap_base, + .high_limit =3D TASK_SIZE, + .align_offset =3D pgoff << PAGE_SHIFT + }; =20 /* * We only need to do colour alignment if either the I or D @@ -61,11 +66,6 @@ arch_get_unmapped_area(struct file *filp, unsigned long = addr, return addr; } =20 - info.flags =3D 0; - info.length =3D len; - info.low_limit =3D mm->mmap_base; - info.high_limit =3D TASK_SIZE; info.align_mask =3D do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0; - info.align_offset =3D pgoff << PAGE_SHIFT; return vm_unmapped_area(&info); } --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9859127B77; Tue, 26 Mar 2024 02:17:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419444; cv=none; b=RrbF4oFjobdZXXoDYUWtAcIS0J/5fsGmdZicZfkdlfe1/VuJS2rpdQtvBygwcau1yAlYJ+ehPIkrjlcKBbeQf/NMr/GhoFDn9gxBmsG6Ffke7MVQE1YE0m6drbZ5uZ3nu/EKFKcI0ho20dvmFaDpZWl7/LjWp1ZzFwDLO77XePg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419444; c=relaxed/simple; bh=1Q2Y9vtgXw1JXW1FsaqsQyB2Shfhn7MnIwpk2ZhDn1g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DBz14C6fbKb+uAy5rvX1X9NREPese1af9anVzJIlLFoFCDVlsG7JJMxnj4/UG0MpfZCrQAVysVhZaL7AzLe6wLzPnNSj6d7Hx2vRzRSe/1v75WnAFYCCqxT8TwXEqM3v2gWtUnJJ1jmj4s9RhwVay2qQgfD/mdLPpIq4cw1pAPg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=n2fEt/wz; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="n2fEt/wz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419443; x=1742955443; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1Q2Y9vtgXw1JXW1FsaqsQyB2Shfhn7MnIwpk2ZhDn1g=; b=n2fEt/wzxx8/Tsb3M4/J3VYrfVYF0JNfxNM+NztaxCO4wP5OdQjq1TrW j4crDcXGMkWNvIQzKxfIFbyBOu1lXJvttDtrb/GFcUkRIMuMkgjfIKF4j 0FlmvEEJJen+NnN2GotfRxXesaIzRXC0ApgcD8Nmbms3LafwQyqQfGqgi JeykckMGfRj6jCyWuHo4q2eRsFTWbUnW5tOq5EWJ8lZ/tv7xx8RN8k0s7 l23wqI6J3PtTWx3/mQ+dsHJFqnIqPnjHg08QGAouoXoEqiiLo0xOE6pPz mlTFFf6z/yDwJ/tMdlqoaekKp1As0j7e3OKMZWecnwccpEQAFldP1t6XH g==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564300" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564300" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489897" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:16 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Helge Deller , "James E.J. Bottomley" , linux-parisc@vger.kernel.org Subject: [PATCH v4 08/14] parisc: Use initializer for struct vm_unmapped_area_info Date: Mon, 25 Mar 2024 19:16:50 -0700 Message-Id: <20240326021656.202649-9-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Future changes will need to add a new member to struct vm_unmapped_area_info. This would cause trouble for any call site that doesn't initialize the struct. Currently every caller sets each member manually, so if new members are added they will be uninitialized and the core code parsing the struct will see garbage in the new member. It could be possible to initialize the new member manually to 0 at each call site. This and a couple other options were discussed, and a working consensus (see links) was that in general the best way to accomplish this would be via static initialization with designated member initiators. Having some struct vm_unmapped_area_info instances not zero initialized will put those sites at risk of feeding garbage into vm_unmapped_area() if the convention is to zero initialize the struct and any new member addition misses a call site that initializes each member manually. It could be possible to leave the code mostly untouched, and just change the line: struct vm_unmapped_area_info info to: struct vm_unmapped_area_info info =3D {}; However, that would leave cleanup for the members that are manually set to zero, as it would no longer be required. So to be reduce the chance of bugs via uninitialized members, instead simply continue the process to initialize the struct this way tree wide. This will zero any unspecified members. Move the member initializers to the struct declaration when they are known at that time. Leave the members out that were manually initialized to zero, as this would be redundant for designated initializers. Signed-off-by: Rick Edgecombe Reviewed-by: Christophe Leroy Acked-by: Helge Deller Cc: "James E.J. Bottomley" Cc: Helge Deller Cc: linux-parisc@vger.kernel.org Link: https://lore.kernel.org/lkml/202402280912.33AEE7A9CF@keescook/#t Link: https://lore.kernel.org/lkml/j7bfvig3gew3qruouxrh7z7ehjjafrgkbcmg6tcg= hhfh3rhmzi@wzlcoecgy5rs/ --- v3: - Fixed spelling errors in log - Be consistent about field vs member in log Hi, This patch was split and refactored out of a tree-wide change [0] to just zero-init each struct vm_unmapped_area_info. The overall goal of the series is to help shadow stack guard gaps. Currently, there is only one arch with shadow stacks, but two more are in progress. It is compile tested only. There was further discussion that this method of initializing the structs while nice in some ways has a greater risk of introducing bugs in some of the more complicated callers. Since this version was reviewed my arch maintainers already, leave it as was already acknowledged. Thanks, Rick [0] https://lore.kernel.org/lkml/20240226190951.3240433-6-rick.p.edgecombe@= intel.com/ --- arch/parisc/kernel/sys_parisc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/parisc/kernel/sys_parisc.c b/arch/parisc/kernel/sys_paris= c.c index 98af719d5f85..f7722451276e 100644 --- a/arch/parisc/kernel/sys_parisc.c +++ b/arch/parisc/kernel/sys_parisc.c @@ -104,7 +104,9 @@ static unsigned long arch_get_unmapped_area_common(stru= ct 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 =3D { + .length =3D len + }; =20 if (unlikely(len > TASK_SIZE)) return -ENOMEM; @@ -139,7 +141,6 @@ static unsigned long arch_get_unmapped_area_common(stru= ct file *filp, return addr; } =20 - info.length =3D len; info.align_mask =3D do_color_align ? (PAGE_MASK & (SHM_COLOUR - 1)) : 0; info.align_offset =3D shared_align_offset(filp_pgoff, pgoff); =20 @@ -160,7 +161,6 @@ static unsigned long arch_get_unmapped_area_common(stru= ct file *filp, */ } =20 - info.flags =3D 0; info.low_limit =3D mm->mmap_base; info.high_limit =3D mmap_upper_limit(NULL); return vm_unmapped_area(&info); --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D980D127B75 for ; Tue, 26 Mar 2024 02:17:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419445; cv=none; b=ucYo9L6u/uyV/BZzFCzHctmgK4qfCqJtAq0VxfZHSrjDg1eyC4vIPPdcUoTc/C+3J0SMCvBeItyvpmig9ytirly/TegW/S1xjPH7gjmHLSomkkmTIDa/bthOvUFlkpdx5hEOwhF0P5VN1lDYWWHprS3vXwXyYJ9lnmnC5R7ZjgQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419445; c=relaxed/simple; bh=BjoIXKt5bAU2p5uENJE46EVXE51w6Jj985k5TB+SvjQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=a2bYsez6Xae0m8sv9NJr9d2QXZDBEXCTwmsodQo3QLBIUoi7r1dcwsaNRy+/kYBn1JLsZ8laB9LM0fZ5qgkvqLms7cxo74S+ZrBa8za7JNG8P9gK3hWHyDphQGaIYL2qW1g0jGcujehi22glRwYSp0OqCBT9EYQh8wNAFepozvs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=fDo5v7yr; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="fDo5v7yr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419443; x=1742955443; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BjoIXKt5bAU2p5uENJE46EVXE51w6Jj985k5TB+SvjQ=; b=fDo5v7yrPWxGzUR4dNsGTgKg4umlk/mlCBEF8KQQlR6Fj2hKhXqFUPf3 64HBX5rsydtFfaNSgNA+25vBKDKt3cM0BVhp1kVpypRElm2D+dj8auhVN /+sGnW5DvTz1biR6Jg7HhVkzWKCyl5n3FAziq/swRsQ5N+HRPSvZMCjB5 pdOI3so9teb+/7TtujYz+ISBGiQdOjfsRTw9lsng2f7wMLAoqCtnOKtjP ZRNUtnQOeNHqDnOr6AwUbAhaXlTxRv+LktCzC85DdX9snTnNDUqektNQs 26jZVA3ltaH76RsPKaI/O+vPhWFWqHrJDELBaGP5s2Fsr+AEKddfyXgo9 A==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564322" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564322" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489901" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:16 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Michael Ellerman , Nicholas Piggin , "Aneesh Kumar K . V" , "Naveen N . Rao" , linuxppc-dev@lists.ozlabs.org Subject: [PATCH v4 09/14] powerpc: Use initializer for struct vm_unmapped_area_info Date: Mon, 25 Mar 2024 19:16:51 -0700 Message-Id: <20240326021656.202649-10-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Future changes will need to add a new member to struct vm_unmapped_area_info. This would cause trouble for any call site that doesn't initialize the struct. Currently every caller sets each member manually, so if new members are added they will be uninitialized and the core code parsing the struct will see garbage in the new member. It could be possible to initialize the new member manually to 0 at each call site. This and a couple other options were discussed, and a working consensus (see links) was that in general the best way to accomplish this would be via static initialization with designated member initiators. Having some struct vm_unmapped_area_info instances not zero initialized will put those sites at risk of feeding garbage into vm_unmapped_area() if the convention is to zero initialize the struct and any new member addition misses a call site that initializes each member manually. It could be possible to leave the code mostly untouched, and just change the line: struct vm_unmapped_area_info info to: struct vm_unmapped_area_info info =3D {}; However, that would leave cleanup for the members that are manually set to zero, as it would no longer be required. So to be reduce the chance of bugs via uninitialized members, instead simply continue the process to initialize the struct this way tree wide. This will zero any unspecified members. Move the member initializers to the struct declaration when they are known at that time. Leave the members out that were manually initialized to zero, as this would be redundant for designated initializers. Signed-off-by: Rick Edgecombe Acked-by: Michael Ellerman Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: Aneesh Kumar K.V Cc: Naveen N. Rao Cc: linuxppc-dev@lists.ozlabs.org Link: https://lore.kernel.org/lkml/202402280912.33AEE7A9CF@keescook/#t Link: https://lore.kernel.org/lkml/j7bfvig3gew3qruouxrh7z7ehjjafrgkbcmg6tcg= hhfh3rhmzi@wzlcoecgy5rs/ --- v4: - Remove designated zero initialization (Christophe Leroy) v3: - Fixed spelling errors in log - Be consistent about field vs member in log Hi, This patch was split and refactored out of a tree-wide change [0] to just zero-init each struct vm_unmapped_area_info. The overall goal of the series is to help shadow stack guard gaps. Currently, there is only one arch with shadow stacks, but two more are in progress. It is compile tested only. There was further discussion that this method of initializing the structs while nice in some ways has a greater risk of introducing bugs in some of the more complicated callers. Since this version was reviewed my arch maintainers already, leave it as was already acknowledged. Thanks, Rick [0] https://lore.kernel.org/lkml/20240226190951.3240433-6-rick.p.edgecombe@= intel.com/ --- arch/powerpc/mm/book3s64/slice.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/arch/powerpc/mm/book3s64/slice.c b/arch/powerpc/mm/book3s64/sl= ice.c index c0b58afb9a47..ef3ce37f1bb3 100644 --- a/arch/powerpc/mm/book3s64/slice.c +++ b/arch/powerpc/mm/book3s64/slice.c @@ -282,12 +282,10 @@ static unsigned long slice_find_area_bottomup(struct = mm_struct *mm, { int pshift =3D max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT); unsigned long found, next_end; - struct vm_unmapped_area_info info; - - info.flags =3D 0; - info.length =3D len; - info.align_mask =3D PAGE_MASK & ((1ul << pshift) - 1); - info.align_offset =3D 0; + struct vm_unmapped_area_info info =3D { + .length =3D len, + .align_mask =3D PAGE_MASK & ((1ul << pshift) - 1), + }; /* * Check till the allow max value for this mmap request */ @@ -326,13 +324,13 @@ static unsigned long slice_find_area_topdown(struct m= m_struct *mm, { int pshift =3D 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 =3D { + .flags =3D VM_UNMAPPED_AREA_TOPDOWN, + .length =3D len, + .align_mask =3D PAGE_MASK & ((1ul << pshift) - 1), + }; unsigned long min_addr =3D max(PAGE_SIZE, mmap_min_addr); =20 - info.flags =3D VM_UNMAPPED_AREA_TOPDOWN; - info.length =3D len; - info.align_mask =3D PAGE_MASK & ((1ul << pshift) - 1); - info.align_offset =3D 0; /* * If we are trying to allocate above DEFAULT_MAP_WINDOW * Add the different to the mmap_base. --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E022953387; Tue, 26 Mar 2024 02:17:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419446; cv=none; b=sMD14zvNgc2DRH5jQRo5PwqBCBc+E4SF3lP+aQlCgaPHU0eMMyiwYjGjFP5GFbCGFb13rFPuQMfpLk3IS0649HUXcHvlV8IdZEaioTpU288EofFOzQsCE7ffcoRi6CDQVvhVPZRtNp7SpCh4N/4PjEFHtfM+APQ1oXqUqbL4RnQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419446; c=relaxed/simple; bh=0XSWLwa7U1N+f/kgoHZwIHzVuDJ8CpV4eXuAoBVCNvI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hlcygfWLQ5Y4GXU97iBQMquJUhzzmAkSnzZ7D6wUivsGO0zDSw24FsuBg5sjxfmk/3ICRdKsBGCnlPFSvh30RzZCUmx1RLeUgCfc6F8InXhjm10e2Oj5VkQ5a3GXGFQQ9bfpS9opaQypZabRU5ycDsDx8/wkPkA29BuQA11DPwg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=jcc2cLXh; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="jcc2cLXh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419443; x=1742955443; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0XSWLwa7U1N+f/kgoHZwIHzVuDJ8CpV4eXuAoBVCNvI=; b=jcc2cLXhTzNAH9zl7PZHN0sPn9QAopzFTsVDRPEy2xGPXNnQQ+PrqZO6 BZpiXR+FxHr3Y9zla1GeKWZxnf1DGhtKbBDoWhd0MxnSxo2pOScPwras4 xeHrrPQLQY/BIQoohxW8G5FW9y1JXe1f0OV1zR2tHGjfhBMSi84u+Qzuj uTJC4y24OqC4jQQycu0ybalOZe6ve1q9svylFpoLG6XC8yKsHt21qZke+ m1Hxm4DfWJ8nfCaw0YuvPC5ovBiGE+gqEGbtzQoHTUmaZIV8sRFmHhCp7 B5SRAs4wKZnMJ9vBXa4CqNLjH7It1NoK7TR8XTV+y1Ft/McXgH/HLxYzq g==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564336" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564336" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489906" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:17 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, 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-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org Subject: [PATCH v4 10/14] treewide: Use initializer for struct vm_unmapped_area_info Date: Mon, 25 Mar 2024 19:16:52 -0700 Message-Id: <20240326021656.202649-11-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Future changes will need to add a new member to struct vm_unmapped_area_info. This would cause trouble for any call site that doesn't initialize the struct. Currently every caller sets each member manually, so if new ones are added they will be uninitialized and the core code parsing the struct will see garbage in the new member. It could be possible to initialize the new member manually to 0 at each call site. This and a couple other options were discussed. Having some struct vm_unmapped_area_info instances not zero initialized will put those sites at risk of feeding garbage into vm_unmapped_area(), if the convention is to zero initialize the struct and any new field addition missed a call site that initializes each field manually. So it is useful to do things similar across the kernel. The consensus (see links) was that in general the best way to accomplish taking into account both code cleanliness and minimizing the chance of introducing bugs, was to do C99 static initialization. As in: struct vm_unmapped_area_info info =3D {}; With this method of initialization, the whole struct will be zero initialized, and any statements setting fields to zero will be unneeded. The change should not leave cleanup at the call sides. While iterating though the possible solutions a few archs kindly acked other variations that still zero initialized the struct. These sites have been modified in previous changes using the pattern acked by the respective arch. So to be reduce the chance of bugs via uninitialized fields, perform a tree wide change using the consensus for the best general way to do this change. Use C99 static initializing to zero the struct and remove and statements that simply set members to zero. Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook 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-s390@vger.kernel.org Cc: linux-sh@vger.kernel.org Cc: sparclinux@vger.kernel.org Link: https://lore.kernel.org/lkml/202402280912.33AEE7A9CF@keescook/#t Link: https://lore.kernel.org/lkml/j7bfvig3gew3qruouxrh7z7ehjjafrgkbcmg6tcg= hhfh3rhmzi@wzlcoecgy5rs/ Link: https://lore.kernel.org/lkml/ec3e377a-c0a0-4dd3-9cb9-96517e54d17e@csg= roup.eu/ --- v4: - Trivial rebase conflict in s390 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. Thanks, Rick --- arch/alpha/kernel/osf_sys.c | 5 +---- arch/arc/mm/mmap.c | 4 +--- arch/arm/mm/mmap.c | 5 ++--- arch/loongarch/mm/mmap.c | 3 +-- arch/mips/mm/mmap.c | 3 +-- arch/s390/mm/hugetlbpage.c | 7 ++----- arch/s390/mm/mmap.c | 5 ++--- arch/sh/mm/mmap.c | 5 ++--- arch/sparc/kernel/sys_sparc_32.c | 3 +-- arch/sparc/kernel/sys_sparc_64.c | 5 ++--- arch/sparc/mm/hugetlbpage.c | 7 ++----- arch/x86/kernel/sys_x86_64.c | 7 ++----- arch/x86/mm/hugetlbpage.c | 7 ++----- fs/hugetlbfs/inode.c | 7 ++----- mm/mmap.c | 9 ++------- 15 files changed, 25 insertions(+), 57 deletions(-) diff --git a/arch/alpha/kernel/osf_sys.c b/arch/alpha/kernel/osf_sys.c index 5db88b627439..e5f881bc8288 100644 --- a/arch/alpha/kernel/osf_sys.c +++ b/arch/alpha/kernel/osf_sys.c @@ -1218,14 +1218,11 @@ 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 =3D {}; =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D addr; info.high_limit =3D limit; - info.align_mask =3D 0; - info.align_offset =3D 0; return vm_unmapped_area(&info); } =20 diff --git a/arch/arc/mm/mmap.c b/arch/arc/mm/mmap.c index 3c1c7ae73292..69a915297155 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 a= ddr, { struct mm_struct *mm =3D current->mm; struct vm_area_struct *vma; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 /* * We enforce the MAP_FIXED case. @@ -51,11 +51,9 @@ arch_get_unmapped_area(struct file *filp, unsigned long = addr, return addr; } =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D mm->mmap_base; info.high_limit =3D TASK_SIZE; - info.align_mask =3D 0; info.align_offset =3D pgoff << PAGE_SHIFT; return vm_unmapped_area(&info); } diff --git a/arch/arm/mm/mmap.c b/arch/arm/mm/mmap.c index a0f8a0ca0788..d65d0e6ed10a 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 a= ddr, struct vm_area_struct *vma; int do_align =3D 0; int aliasing =3D cache_is_vipt_aliasing(); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 /* * We only need to do colour alignment if either the I or D @@ -68,7 +68,6 @@ arch_get_unmapped_area(struct file *filp, unsigned long a= ddr, return addr; } =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D mm->mmap_base; info.high_limit =3D TASK_SIZE; @@ -87,7 +86,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const u= nsigned long addr0, unsigned long addr =3D addr0; int do_align =3D 0; int aliasing =3D cache_is_vipt_aliasing(); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 /* * 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..4bbd449b4a47 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 =3D addr0; int do_color_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 if (unlikely(len > TASK_SIZE)) return -ENOMEM; @@ -82,7 +82,6 @@ static unsigned long arch_get_unmapped_area_common(struct= file *filp, */ } =20 - info.flags =3D 0; info.low_limit =3D mm->mmap_base; info.high_limit =3D TASK_SIZE; return vm_unmapped_area(&info); diff --git a/arch/mips/mm/mmap.c b/arch/mips/mm/mmap.c index 00fe90c6db3e..7e11d7b58761 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 =3D addr0; int do_color_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 if (unlikely(len > TASK_SIZE)) return -ENOMEM; @@ -92,7 +92,6 @@ static unsigned long arch_get_unmapped_area_common(struct= file *filp, */ } =20 - info.flags =3D 0; info.low_limit =3D mm->mmap_base; info.high_limit =3D TASK_SIZE; return vm_unmapped_area(&info); diff --git a/arch/s390/mm/hugetlbpage.c b/arch/s390/mm/hugetlbpage.c index 219d906fe830..46de7a4c0309 100644 --- a/arch/s390/mm/hugetlbpage.c +++ b/arch/s390/mm/hugetlbpage.c @@ -258,14 +258,12 @@ static unsigned long hugetlb_get_unmapped_area_bottom= up(struct file *file, unsigned long pgoff, unsigned long flags) { struct hstate *h =3D hstate_file(file); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D current->mm->mmap_base; info.high_limit =3D TASK_SIZE; info.align_mask =3D PAGE_MASK & ~huge_page_mask(h); - info.align_offset =3D 0; return vm_unmapped_area(&info); } =20 @@ -274,7 +272,7 @@ static unsigned long hugetlb_get_unmapped_area_topdown(= struct file *file, unsigned long pgoff, unsigned long flags) { struct hstate *h =3D hstate_file(file); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; unsigned long addr; =20 info.flags =3D VM_UNMAPPED_AREA_TOPDOWN; @@ -282,7 +280,6 @@ static unsigned long hugetlb_get_unmapped_area_topdown(= struct file *file, info.low_limit =3D PAGE_SIZE; info.high_limit =3D current->mm->mmap_base; info.align_mask =3D PAGE_MASK & ~huge_page_mask(h); - info.align_offset =3D 0; addr =3D vm_unmapped_area(&info); =20 /* diff --git a/arch/s390/mm/mmap.c b/arch/s390/mm/mmap.c index 6b2e4436ad4a..206756946589 100644 --- a/arch/s390/mm/mmap.c +++ b/arch/s390/mm/mmap.c @@ -86,7 +86,7 @@ unsigned long arch_get_unmapped_area(struct file *filp, u= nsigned long addr, { struct mm_struct *mm =3D current->mm; struct vm_area_struct *vma; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 if (len > TASK_SIZE - mmap_min_addr) return -ENOMEM; @@ -102,7 +102,6 @@ unsigned long arch_get_unmapped_area(struct file *filp,= unsigned long addr, goto check_asce_limit; } =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D mm->mmap_base; info.high_limit =3D TASK_SIZE; @@ -122,7 +121,7 @@ unsigned long arch_get_unmapped_area_topdown(struct fil= e *filp, unsigned long ad { struct vm_area_struct *vma; struct mm_struct *mm =3D current->mm; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 /* 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..bee329d4149a 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, u= nsigned long addr, struct mm_struct *mm =3D current->mm; struct vm_area_struct *vma; int do_colour_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 if (flags & MAP_FIXED) { /* We do not accept a shared mapping if it would violate @@ -88,7 +88,6 @@ unsigned long arch_get_unmapped_area(struct file *filp, u= nsigned long addr, return addr; } =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D TASK_UNMAPPED_BASE; info.high_limit =3D TASK_SIZE; @@ -106,7 +105,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const= unsigned long addr0, struct mm_struct *mm =3D current->mm; unsigned long addr =3D addr0; int do_colour_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 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..08a19727795c 100644 --- a/arch/sparc/kernel/sys_sparc_32.c +++ b/arch/sparc/kernel/sys_sparc_32.c @@ -41,7 +41,7 @@ SYSCALL_DEFINE0(getpagesize) =20 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 =3D {}; =20 if (flags & MAP_FIXED) { /* We do not accept a shared mapping if it would violate @@ -59,7 +59,6 @@ unsigned long arch_get_unmapped_area(struct file *filp, u= nsigned long addr, unsi if (!addr) addr =3D TASK_UNMAPPED_BASE; =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D addr; info.high_limit =3D TASK_SIZE; diff --git a/arch/sparc/kernel/sys_sparc_64.c b/arch/sparc/kernel/sys_sparc= _64.c index 1dbf7211666e..d9c3b34ca744 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, u= nsigned long addr, unsi struct vm_area_struct * vma; unsigned long task_size =3D TASK_SIZE; int do_color_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 if (flags & MAP_FIXED) { /* We do not accept a shared mapping if it would violate @@ -126,7 +126,6 @@ unsigned long arch_get_unmapped_area(struct file *filp,= unsigned long addr, unsi return addr; } =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D TASK_UNMAPPED_BASE; info.high_limit =3D min(task_size, VA_EXCLUDE_START); @@ -154,7 +153,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const= unsigned long addr0, unsigned long task_size =3D STACK_TOP32; unsigned long addr =3D addr0; int do_color_align; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 /* 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..4caf56b32e26 100644 --- a/arch/sparc/mm/hugetlbpage.c +++ b/arch/sparc/mm/hugetlbpage.c @@ -31,17 +31,15 @@ static unsigned long hugetlb_get_unmapped_area_bottomup= (struct file *filp, { struct hstate *h =3D hstate_file(filp); unsigned long task_size =3D TASK_SIZE; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 if (test_thread_flag(TIF_32BIT)) task_size =3D STACK_TOP32; =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D TASK_UNMAPPED_BASE; info.high_limit =3D min(task_size, VA_EXCLUDE_START); info.align_mask =3D PAGE_MASK & ~huge_page_mask(h); - info.align_offset =3D 0; addr =3D vm_unmapped_area(&info); =20 if ((addr & ~PAGE_MASK) && task_size > VA_EXCLUDE_END) { @@ -63,7 +61,7 @@ hugetlb_get_unmapped_area_topdown(struct file *filp, cons= t unsigned long addr0, struct hstate *h =3D hstate_file(filp); struct mm_struct *mm =3D current->mm; unsigned long addr =3D addr0; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 /* This should only ever run for 32-bit processes. */ BUG_ON(!test_thread_flag(TIF_32BIT)); @@ -73,7 +71,6 @@ hugetlb_get_unmapped_area_topdown(struct file *filp, cons= t unsigned long addr0, info.low_limit =3D PAGE_SIZE; info.high_limit =3D mm->mmap_base; info.align_mask =3D PAGE_MASK & ~huge_page_mask(h); - info.align_offset =3D 0; addr =3D vm_unmapped_area(&info); =20 /* diff --git a/arch/x86/kernel/sys_x86_64.c b/arch/x86/kernel/sys_x86_64.c index cb9fa1d5c66f..96b9d29aead0 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -118,7 +118,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long= addr, { struct mm_struct *mm =3D current->mm; struct vm_area_struct *vma; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; unsigned long begin, end; =20 if (flags & MAP_FIXED) @@ -137,11 +137,9 @@ arch_get_unmapped_area(struct file *filp, unsigned lon= g addr, return addr; } =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D begin; info.high_limit =3D end; - info.align_mask =3D 0; info.align_offset =3D pgoff << PAGE_SHIFT; if (filp) { info.align_mask =3D get_align_mask(); @@ -158,7 +156,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const= unsigned long addr0, struct vm_area_struct *vma; struct mm_struct *mm =3D current->mm; unsigned long addr =3D addr0; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 /* requested length too big for entire address space */ if (len > TASK_SIZE) @@ -203,7 +201,6 @@ arch_get_unmapped_area_topdown(struct file *filp, const= unsigned long addr0, if (addr > DEFAULT_MAP_WINDOW && !in_32bit_syscall()) info.high_limit +=3D TASK_SIZE_MAX - DEFAULT_MAP_WINDOW; =20 - info.align_mask =3D 0; info.align_offset =3D pgoff << PAGE_SHIFT; if (filp) { info.align_mask =3D get_align_mask(); diff --git a/arch/x86/mm/hugetlbpage.c b/arch/x86/mm/hugetlbpage.c index 6d77c0039617..fb600949a355 100644 --- a/arch/x86/mm/hugetlbpage.c +++ b/arch/x86/mm/hugetlbpage.c @@ -51,9 +51,8 @@ static unsigned long hugetlb_get_unmapped_area_bottomup(s= truct file *file, unsigned long pgoff, unsigned long flags) { struct hstate *h =3D hstate_file(file); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D get_mmap_base(1); =20 @@ -65,7 +64,6 @@ static unsigned long hugetlb_get_unmapped_area_bottomup(s= truct file *file, task_size_32bit() : task_size_64bit(addr > DEFAULT_MAP_WINDOW); =20 info.align_mask =3D PAGE_MASK & ~huge_page_mask(h); - info.align_offset =3D 0; return vm_unmapped_area(&info); } =20 @@ -74,7 +72,7 @@ static unsigned long hugetlb_get_unmapped_area_topdown(st= ruct file *file, unsigned long pgoff, unsigned long flags) { struct hstate *h =3D hstate_file(file); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 info.flags =3D VM_UNMAPPED_AREA_TOPDOWN; info.length =3D len; @@ -89,7 +87,6 @@ static unsigned long hugetlb_get_unmapped_area_topdown(st= ruct file *file, info.high_limit +=3D TASK_SIZE_MAX - DEFAULT_MAP_WINDOW; =20 info.align_mask =3D PAGE_MASK & ~huge_page_mask(h); - info.align_offset =3D 0; addr =3D vm_unmapped_area(&info); =20 /* diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 3dee18bf47ed..2f4e88552d3f 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -176,14 +176,12 @@ hugetlb_get_unmapped_area_bottomup(struct file *file,= unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { struct hstate *h =3D hstate_file(file); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D current->mm->mmap_base; info.high_limit =3D arch_get_mmap_end(addr, len, flags); info.align_mask =3D PAGE_MASK & ~huge_page_mask(h); - info.align_offset =3D 0; return vm_unmapped_area(&info); } =20 @@ -192,14 +190,13 @@ hugetlb_get_unmapped_area_topdown(struct file *file, = unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { struct hstate *h =3D hstate_file(file); - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; =20 info.flags =3D VM_UNMAPPED_AREA_TOPDOWN; info.length =3D len; info.low_limit =3D PAGE_SIZE; info.high_limit =3D arch_get_mmap_base(addr, current->mm->mmap_base); info.align_mask =3D PAGE_MASK & ~huge_page_mask(h); - info.align_offset =3D 0; addr =3D vm_unmapped_area(&info); =20 /* diff --git a/mm/mmap.c b/mm/mmap.c index f734e4fa6d94..609c087bba8e 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1705,7 +1705,7 @@ generic_get_unmapped_area(struct file *filp, unsigned= long addr, { struct mm_struct *mm =3D current->mm; struct vm_area_struct *vma, *prev; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; const unsigned long mmap_end =3D arch_get_mmap_end(addr, len, flags); =20 if (len > mmap_end - mmap_min_addr) @@ -1723,12 +1723,9 @@ generic_get_unmapped_area(struct file *filp, unsigne= d long addr, return addr; } =20 - info.flags =3D 0; info.length =3D len; info.low_limit =3D mm->mmap_base; info.high_limit =3D mmap_end; - info.align_mask =3D 0; - info.align_offset =3D 0; return vm_unmapped_area(&info); } =20 @@ -1753,7 +1750,7 @@ generic_get_unmapped_area_topdown(struct file *filp, = unsigned long addr, { struct vm_area_struct *vma, *prev; struct mm_struct *mm =3D current->mm; - struct vm_unmapped_area_info info; + struct vm_unmapped_area_info info =3D {}; const unsigned long mmap_end =3D arch_get_mmap_end(addr, len, flags); =20 /* requested length too big for entire address space */ @@ -1777,8 +1774,6 @@ generic_get_unmapped_area_topdown(struct file *filp, = unsigned long addr, info.length =3D len; info.low_limit =3D PAGE_SIZE; info.high_limit =3D arch_get_mmap_base(addr, mm->mmap_base); - info.align_mask =3D 0; - info.align_offset =3D 0; addr =3D vm_unmapped_area(&info); =20 /* --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BEA0312881C for ; Tue, 26 Mar 2024 02:17:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419446; cv=none; b=p+HcUSS+XCYVX9MknMgT9fh/kFVB+sTNMNCTu+1akyOxaax0lrCNfp4Pe+4Rguhp2P6Ih9O1Nd9pjYmFW5yEDlZJscAm9EbJNVAaX0BWUnF8GjUBgp/rO8MvyMc3nWOa26MRu/UiB9yepM27WbGAILd8mLy4qbYH0CTF2E9XhzE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419446; c=relaxed/simple; bh=sxOpaISc6XsCBcTnaTun5GkWeCaUbW+4JANtdWXac9M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=WdZnbWk5QUtzkZ4mfLGpJk3L9OWwIZTTgDoG58RYr49CrPwQaUXwDLQNXgxKu0+8URAThEu1yINUP/uxCaP4Oi4Gex1jewpWblVcvfQhzoGwqdkD225fn/uAcrxeJXsQCA8MMZIoPBrunIQQNNW+cn2b9FVWKFnrlg/uFversVc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ETk6m2J4; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ETk6m2J4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419445; x=1742955445; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sxOpaISc6XsCBcTnaTun5GkWeCaUbW+4JANtdWXac9M=; b=ETk6m2J4GpnzWV5nizrWiKss6f6vTKFVdcy9TBlglh45FMosZRFn6EjP BIxIlXdZcOewqb15Qh3wogt7TZK+E+5J9I1EHJPy4NLNU4Xr98/JnTuIg F2dxyJKWly2jwFB4MnquHHmhMqC6zSUFi/Jli6mRhiz2RlcwwxlMjfSjt eV6xe9w5Cl0GwioSYZjfuAdue8XFsYCyDHhHuzefxmPxuZLc2xxwquGk+ qhFJMEsAlc04KW7e6+5q58ZOssjivFf9ibEMZbSSGFkUtHfCeewF+LsTk +vUcdENqIi9YMak817WKTbmZIJRVQWFcoKNEo53cK1yN+lpQYbezCmV5B A==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564362" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564362" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489909" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:17 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 11/14] mm: Take placement mappings gap into account Date: Mon, 25 Mar 2024 19:16:53 -0700 Message-Id: <20240326021656.202649-12-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=E2=80=99t placed in an any existing mappings g= uard gaps. 2. That the new mapping isn=E2=80=99t placed such that any existing mappin= gs 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 Reviewed-by: Christophe Leroy --- v3: - Spelling fix in comment 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 8b13cd891b53..5c7f75edfde1 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3438,6 +3438,7 @@ struct vm_unmapped_area_info { unsigned long high_limit; unsigned long align_mask; unsigned long align_offset; + unsigned long start_gap; }; =20 extern unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info); diff --git a/mm/mmap.c b/mm/mmap.c index 609c087bba8e..2d9e7a999774 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1580,7 +1580,7 @@ static unsigned long unmapped_area(struct vm_unmapped= _area_info *info) MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); =20 /* Adjust search length to account for worst case alignment overhead */ - length =3D info->length + info->align_mask; + length =3D info->length + info->align_mask + info->start_gap; if (length < info->length) return -ENOMEM; =20 @@ -1592,7 +1592,13 @@ static unsigned long unmapped_area(struct vm_unmappe= d_area_info *info) if (mas_empty_area(&mas, low_limit, high_limit - 1, length)) return -ENOMEM; =20 - gap =3D mas.index; + /* + * Adjust for the gap first so it doesn't interfere with the + * later alignment. The first step is the minimum needed to + * fulill the start gap, the next steps is the minimum to align + * that. It is the minimum needed to fulill both. + */ + gap =3D mas.index + info->start_gap; gap +=3D (info->align_offset - gap) & info->align_mask; tmp =3D mas_next(&mas, ULONG_MAX); if (tmp && (tmp->vm_flags & VM_STARTGAP_FLAGS)) { /* Avoid prev check if = possible */ @@ -1631,7 +1637,7 @@ static unsigned long unmapped_area_topdown(struct vm_= unmapped_area_info *info) =20 MA_STATE(mas, ¤t->mm->mm_mt, 0, 0); /* Adjust search length to account for worst case alignment overhead */ - length =3D info->length + info->align_mask; + length =3D info->length + info->align_mask + info->start_gap; if (length < info->length) return -ENOMEM; =20 --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 745A912AAEC for ; Tue, 26 Mar 2024 02:17:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419450; cv=none; b=m3HEYJJnlk5NiZBs7tZRmv2sUa+59hUNA5S8MvR+n1xC51Ogw7bPB25vSmnwsR7rS/M5Dzc2ZE27mH0W0e/eFC1RDJ6CintP81ga4L2Pn2iPiJAdAorZtCogiq9YtKULnmwcavQpPhXw3fycWOMX5qcnmU5ncizjNLxzkVBCMxU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419450; c=relaxed/simple; bh=uO697Q/RqZjisoLPzX7dc/TQTP+f7eWP95gfUGD1wX8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=BJ/AvYSxXhBGR13yxkxle7efysOfvCcbGFNirx2xWreqglVBY058c8bSgS28B7cYil0BhcNyNAw1nCdN7sPZ9DYw480lPRP60adMOYidYI6nSn3/qs8dHHfloBO5umZYYp3f6q4bE+3mb6FCgPrtVbkQ/ypvU3W7RYQ7qv0niIY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=kN0ZnWiG; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="kN0ZnWiG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419448; x=1742955448; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uO697Q/RqZjisoLPzX7dc/TQTP+f7eWP95gfUGD1wX8=; b=kN0ZnWiGKB4j4TJILGXH/XyZ5+/aBPgbrcxUGJW5ve6UugBCLMBON1Or uWQW0zkKfue1oe8ASnFo8H2+3v41ulzT57MMKuB9Ic9DLqDdCHXdpZ7gf eyWTckmmBPocK5ECxzNZsZ7OtwXgwFPZ+uiHKMoFKY2smvm0tOx0sXv2x oze26M1EGCX/MbeE8o8s1wm/NVBh+2A37pFiZP8OuWpIKN35dE2roLGs+ qAy5MWfSJ9P2xMH2cvdQf6aE0TUkLceNZgdSf1E3Ejps0IB/ZduXxPQXn FjiagO2ZTwD+UvVRWAQvqY31euXgab0nmCMSyCYIay+fKvjqboXKJGUmu A==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564376" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564376" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489913" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:17 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 12/14] x86/mm: Implement HAVE_ARCH_UNMAPPED_AREA_VMFLAGS Date: Mon, 25 Mar 2024 19:16:54 -0700 Message-Id: <20240326021656.202649-13-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=E2=80=99t placed in an any existing mappings g= uard gaps. 2. That the new mapping isn=E2=80=99t placed such that any existing mappin= gs 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 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 --- v3: - Commit log grammar 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/pgtab= le_64.h index 7e9db77231ac..3c4407271d08 100644 --- a/arch/x86/include/asm/pgtable_64.h +++ b/arch/x86/include/asm/pgtable_64.h @@ -245,6 +245,7 @@ extern void cleanup_highmap(void); =20 #define HAVE_ARCH_UNMAPPED_AREA #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN +#define HAVE_ARCH_UNMAPPED_AREA_VMFLAGS =20 #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 96b9d29aead0..75966afb6251 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -113,8 +113,8 @@ static void find_start_end(unsigned long addr, unsigned= long flags, } =20 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, unsi= gned long len, + unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) { struct mm_struct *mm =3D current->mm; struct vm_area_struct *vma; @@ -149,9 +149,9 @@ arch_get_unmapped_area(struct file *filp, unsigned long= addr, } =20 unsigned long -arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr= 0, - const unsigned long len, const unsigned long pgoff, - const unsigned long flags) +arch_get_unmapped_area_topdown_vmflags(struct file *filp, unsigned long ad= dr0, + unsigned long len, unsigned long pgoff, + unsigned long flags, vm_flags_t vm_flags) { struct vm_area_struct *vma; struct mm_struct *mm =3D current->mm; @@ -220,3 +220,18 @@ arch_get_unmapped_area_topdown(struct file *filp, cons= t 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, fla= gs, 0); +} --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91CEC1292E5 for ; Tue, 26 Mar 2024 02:17:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419447; cv=none; b=oxWqmt3bo/QKe+oFgc4LENI8X5aJ1tyO02Pf4KCqCli7wpwrhCHiffZyss7o/STh+vG5MUMt1F59iEywjWYLy9WPl0OVIiGO+vbLQJa3nYM1xveYN7AocM5YHFINiVSLFva+yic/Th2kdQdHUIRrE7joG/KIMcbkISFd1vPpufA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419447; c=relaxed/simple; bh=mZPCbr0rDs8wcMO8G+Ur+xnvhS6uTOIeQkjfiLGhRhI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=WwwQuK/hW+wwbVmd4JYA9LNj0lw3H70TwUXP9qu7lQ1oapzGm/xkWFVZjdN8rQSCfrVgcqUm3a3etThh6WBjVnNPOM7hdJTzZd9lATCN0tKlwlULwAM3tCjESPae5dqnbcWPekphIl96TlQBk292UyVirWNM3biz/yIwpnc6fHM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=mYEdhCIL; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="mYEdhCIL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419445; x=1742955445; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mZPCbr0rDs8wcMO8G+Ur+xnvhS6uTOIeQkjfiLGhRhI=; b=mYEdhCILPXAa8FIFejmi7DaWvmj1GpnnOvbatn9a8Jlk0Do4CEhdzrO5 C7e7CKvAOGBgqOT02QCk329IdT1AH4mLsapDiLekWorSZDNPf+B/d1WlC WKP1MD5ZShkc+1ZIRXu1hc/seFO3k9/BGHcppIgayMaoGRpK4MdilLz8B 8e+X2vMgpqOxP/axGFVkNBk/TCUqxdyVgqnV7ENHKo7Fv2c1zuvoveWKW /HkZHngShbT5ZTesDDHSZfbUevKnrqjjoo4rZx0F/cJWcK/HzcQNOSety rJ1HeJtfrLw2tBO43yW0qR3Ns/HhaGy9GyT5i/ryzC+YgEfGEGeR/pIKA A==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564378" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564378" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489917" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:18 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 13/14] x86/mm: Care about shadow stack guard gap during placement Date: Mon, 25 Mar 2024 19:16:55 -0700 Message-Id: <20240326021656.202649-14-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=E2=80=99t placed in an any existing mappings g= uard gaps. 2. That the new mapping isn=E2=80=99t placed such that any existing mappin= gs 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 75966afb6251..01d7cd85ef97 100644 --- a/arch/x86/kernel/sys_x86_64.c +++ b/arch/x86/kernel/sys_x86_64.c @@ -112,6 +112,14 @@ static void find_start_end(unsigned long addr, unsigne= d long flags, *end =3D task_size_64bit(addr > DEFAULT_MAP_WINDOW); } =20 +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, unsi= gned long len, unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags) @@ -141,6 +149,7 @@ arch_get_unmapped_area_vmflags(struct file *filp, unsig= ned long addr, unsigned l info.low_limit =3D begin; info.high_limit =3D end; info.align_offset =3D pgoff << PAGE_SHIFT; + info.start_gap =3D stack_guard_placement(vm_flags); if (filp) { info.align_mask =3D get_align_mask(); info.align_offset +=3D get_align_bits(); @@ -190,6 +199,7 @@ arch_get_unmapped_area_topdown_vmflags(struct file *fil= p, unsigned long addr0, info.low_limit =3D PAGE_SIZE; =20 info.high_limit =3D get_mmap_base(0); + info.start_gap =3D stack_guard_placement(vm_flags); =20 /* * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area --=20 2.34.1 From nobody Sat Feb 7 19:41:08 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F6DB12AAEA for ; Tue, 26 Mar 2024 02:17:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419450; cv=none; b=pgVp4duOHYAZgv+r+v7kDS7rxXReSUT3/lNfDrXCumgQ4V/rS0ZXEVn1zL7t7lYuZfCw7oBSGnNWAlpLNyZjnI/5T3lqgSkJUS7eFIvlO3Py1DVmo5aVlgPVPJtR5W4PMo6F6tVBcMy40BJIlnNOHuZK6h3djkFD5KS8tBH7B0M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419450; c=relaxed/simple; bh=9Kz1B1g9YBWVQVBA7u5n8sBfRhBddgPHEWEy/INdrdY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=j08hyYHb7aK3NxWz/o+qGEu3FgcQcWowi4482K5dRoL5ApHnEg8z6nce4TmNpWSNXNIV8Y0AzGUkIMBxiYuS6weeEP7JhBGicKcFDCkFjRGZIVY2E8RWq98tKscOaDajTlf2zphHfuiT0jVlWckTIkRZk1L+j9aos4a9i2lONwc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=LYYZyUFr; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="LYYZyUFr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419448; x=1742955448; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9Kz1B1g9YBWVQVBA7u5n8sBfRhBddgPHEWEy/INdrdY=; b=LYYZyUFrtDZkQhAOhqtCiGspppwQiSNYt0J9ZxHDi60absIqwAZ2EZR5 pCBEajXfa3H9NHI68e+NyPUj8PnASFZxA7ZJOwBETuF7CFwpMR/90+46y A6IbGfBr23dXDkyOGz2AqMcy2MQ2Fk8PV4bqx3sVW+AZZLaCWCk+gNB3Z I5LHhzdXjHFuCcQgfUOshc21whlZ31lCYNvcN+Xdq5l/ehIokD/MXOseh NqBdTP1YCJcqqkoTj5+p8WnFCDnRuCU2GGuvewkDPhToZNgPK45DM6tCF FBS7wbeJxDJpwDGHzrua9+HoUysX5gl/gvAKWVAGeuNr3SoOduI90ztvv w==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564393" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564393" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489921" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:18 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 14/14] selftests/x86: Add placement guard gap test for shstk Date: Mon, 25 Mar 2024 19:16:56 -0700 Message-Id: <20240326021656.202649-15-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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/testin= g/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 =3D (void *)0xFFFFFFFFFFFFFFFF; struct node *head =3D NULL, *cur; @@ -593,11 +593,64 @@ int test_guard_gap(void) if (shstk - test_map - PAGE_SIZE !=3D PAGE_SIZE) return 1; =20 - printf("[OK]\tGuard gap test\n"); + printf("[OK]\tGuard gap test, other mapping's gaps\n"); =20 return 0; } =20 +/* 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 =3D (void *)0xFFFFFFFFFFFFFFFF; + struct node *head =3D NULL, *cur; + int ret =3D 0; + + free_area =3D 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 =3D mmap(free_area, PAGE_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (shstk_start =3D=3D MAP_FAILED || shstk_start !=3D free_area) + return 1; + + while (test_map > shstk_start) { + test_map =3D (void *)syscall(__NR_map_shadow_stack, 0, PAGE_SIZE, 0); + if (test_map =3D=3D MAP_FAILED) { + printf("[INFO]\tmap_shadow_stack MAP_FAILED\n"); + ret =3D 1; + break; + } + + cur =3D malloc(sizeof(*cur)); + cur->mapping =3D test_map; + + cur->next =3D head; + head =3D cur; + + if (test_map =3D=3D free_area + PAGE_SIZE) { + printf("[INFO]\tNew mapping has other mapping in guard gap!\n"); + ret =3D 1; + break; + } + } + + while (head) { + cur =3D head; + head =3D 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; } =20 - if (test_guard_gap()) { + if (test_guard_gap_other_gaps()) { ret =3D 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 =3D 1; + printf("[FAIL]\tGuard gap test, placement mapping's gaps\n"); goto out; } =20 --=20 2.34.1