From patchwork Mon Sep 18 07:29:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22094CD37B0 for ; Mon, 18 Sep 2023 07:30:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A9A1D6B0276; Mon, 18 Sep 2023 03:30:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A23356B0278; Mon, 18 Sep 2023 03:30:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 89BD66B0279; Mon, 18 Sep 2023 03:30:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 713CD6B0276 for ; Mon, 18 Sep 2023 03:30:29 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 4881116094B for ; Mon, 18 Sep 2023 07:30:29 +0000 (UTC) X-FDA: 81248895378.28.79C1FA9 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf02.hostedemail.com (Postfix) with ESMTP id A411D8001F for ; Mon, 18 Sep 2023 07:30:26 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=n1BsBmpf; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf02.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022226; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IkPztNd7bnD91GoOX9u3X5sY7LI8HpPP33/j7zBq9yU=; b=Ynybp5qB/ZIp6S1eMwZ8VB3Y8D3TIO+Ls2kBlr33XJlhbZ0KW9FlKaNffccpLIXwwGIAp4 sI5F4u83UV9Tg2jyYJcJ/9llWIAfNyKSdhtQG5+HwFocUNODK8axJBEMo+hFgndi5JdKuU ufSAnlmFSmJ2QwQADVc8/G1x9YhsFkM= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=n1BsBmpf; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf02.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022226; a=rsa-sha256; cv=none; b=u5KIkcyjp35dEENB/A1djvxRm0+y5f+D5jlaI9aV0BZ5cmqRwjiUeLEyCkHgb4Ybt6pQxM +ccWAgMnI7ZSEtyuJKZuPaIeKldLO2buPRoDVztdUo2RlKjEZFrVXaCGt/+c2wY/WD5zK/ UnNIx6GKH5b8oYGa1m48RZsF+Brp3Cw= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 6C5CD60E15; Mon, 18 Sep 2023 07:30:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55C0EC43397; Mon, 18 Sep 2023 07:30:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022224; bh=KSPIxmcvM6pw+gN3UxNfSb/hLNccmDr75av1M8zdRQI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n1BsBmpfW3QKK3NJROVE3ZzVUFsmFfDjPKEBn27xuClR0kSquHorXzHaU80koKHjq oZZubDbrJshf2OzAsSW/lYZeiCxCVkNHUyRKf0J25B3oyQqT68RZdj7eYC/N0Cta3F s7s8WnXGl08TzUW7RJcoZTxms2v0q/MjRq68IGaI1IRBqPpLAZ6vaNChPkgshwIF5h 3KnstVmy4jP0LDvFd3KogmLI2XFZUpl0AY5gcnG4Aq2Zy+SxUhV393i/W7f5/3shGG k9UrFYiH9x7//wdEvOIFolwWa2pPRCHkauyGN/FXYwMai04Ht7kCMou6e5Iw71dZtp HuRwsr2VE4kwA== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 01/13] nios2: define virtual address space for modules Date: Mon, 18 Sep 2023 10:29:43 +0300 Message-Id: <20230918072955.2507221-2-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: A411D8001F X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 3qxex7hhjuj153f5c1y3pncnukb8gyim X-HE-Tag: 1695022226-671701 X-HE-Meta: U2FsdGVkX19RwcEfRuHa5RRKFUFTkimBmFRcIL4kIKRqskiynVo3gdhq5rdqbaaT3KwM5inRhF9RCTm4sASV21SdtkMT6ZNKVN/RwYG8Zdo+czgunDmp+LvjcThjjn/RmVFYVbOGvB4O+uAIQ9Z+IxOERR/HalNwnJGfgiFSTE4vnI7ScIi7wAdXOPvc+uaDya/hjwE73MEusLmaab265O2C0jBqlauFH9NgVOQ+jbsHfJrSR4Z4G6CfZQ7z3T9eipPP1/tOECE5nvhIIu3OM5KDxG1U3DU0L1h2eca/KNd/e5Mcc3FZHa8T5d96587z3ufvtf0YWzHdFEhwJUfFo5N8jb2awh3wR1ow6W43CqHGOaD9BRxxpHur3YtzBPUSPe66XFKPxWSYAnCXlmo45DJ5V37PxApimVO8FXp8dc6bDsFCLPPLOAlURY+Ug2FNLvCLvVuzIjAa4cUvte9F9mkGod4Q3Bkp8aFUEwt3qJr+Ny6A0aJT1ETNztJWzSv1t8c8JBAmjpCY4ZC/RN6SuaKJRddI4dqABvy7NQAMwYvQ8Gom50MkCANZQhOqGgwSGyFj5a8X/ZeG/56IvYFZFK4NzLDFPRpu6/xDw/Kv3bWojZSUtz3lzUr2UaTjEvslNSOpYATLEyjocD4n2tOgtdo2fjsutTwMFoNSc2WxJGHgjAbooFtvdkNPgnQvt9hYMKmvVFNDMAmAiV8SMo3m/yhJvT63ex04aQ7e/kYMpKotjtMFG7wHEbuGtHi7Jbp5ozO4CQ5mGbZROotxbOc/v9EqNYJUUzlQjBhW1+273ejxterSE24rI7F43jAA1s1aIDFqEfl3CzCOAYe9aOCamXQ1t+lphYCFy/y4prVBcHijySA3UI1RH1fXLmJRTuZNj3i/ejZm1y967UkEVTAcYS3iH/KSs6fTLpnGPhi7J2kMGymeKwEVKW0nWti9O9796pzK309PP04eoFOGMS9 XCtxx5JY ARqNH16DGeus4VmQsrYN0TMbQIZkzZ7rFczJBvCAACw9qN90L3ggaJB6kytwKpHECxf7RA2P9dSEeMjUKMVzlH6sJyFoQn28ofb6ar4R2edqteGBuN51E+JE9sBp76ecDDzwrrXDwMGnSxLrq8hHfu8QpfOj2QewxDn8y+YV9ute9AQJpX5MGsZjTr0XyzaBAygQbbwIBCMcHM1eG8DjbZ0eWxiIxYz2Hx7JikFttPU93ZzgyofNkDEOh+uOxe1HsJSugLJuHToar6O7OiQTccohpU/JERq8lrEV+8rGu7aJIQoKSlRPBBGJp8LyDlvlrPtQHMteCTYNMAJEN+PoMZLCu097fW89IZTVNTvLPEixbe01oLESAHiyz5iso7qsmjMiB83pX4D8bLK9ONFwnFga2JpDCUmX0hQu8PyH0Ye/XegdQqEwx3UYqO7c7kahQq9hL X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" nios2 uses kmalloc() to implement module_alloc() because CALL26/PCREL26 cannot reach all of vmalloc address space. Define module space as 32MiB below the kernel base and switch nios2 to use vmalloc for module allocations. Suggested-by: Thomas Gleixner Acked-by: Dinh Nguyen Acked-by: Song Liu Signed-off-by: Mike Rapoport (IBM) --- arch/nios2/include/asm/pgtable.h | 5 ++++- arch/nios2/kernel/module.c | 19 ++++--------------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/arch/nios2/include/asm/pgtable.h b/arch/nios2/include/asm/pgtable.h index 5144506dfa69..d2fb42fb6db8 100644 --- a/arch/nios2/include/asm/pgtable.h +++ b/arch/nios2/include/asm/pgtable.h @@ -25,7 +25,10 @@ #include #define VMALLOC_START CONFIG_NIOS2_KERNEL_MMU_REGION_BASE -#define VMALLOC_END (CONFIG_NIOS2_KERNEL_REGION_BASE - 1) +#define VMALLOC_END (CONFIG_NIOS2_KERNEL_REGION_BASE - SZ_32M - 1) + +#define MODULES_VADDR (CONFIG_NIOS2_KERNEL_REGION_BASE - SZ_32M) +#define MODULES_END (CONFIG_NIOS2_KERNEL_REGION_BASE - 1) struct mm_struct; diff --git a/arch/nios2/kernel/module.c b/arch/nios2/kernel/module.c index 76e0a42d6e36..9c97b7513853 100644 --- a/arch/nios2/kernel/module.c +++ b/arch/nios2/kernel/module.c @@ -21,23 +21,12 @@ #include -/* - * Modules should NOT be allocated with kmalloc for (obvious) reasons. - * But we do it for now to avoid relocation issues. CALL26/PCREL26 cannot reach - * from 0x80000000 (vmalloc area) to 0xc00000000 (kernel) (kmalloc returns - * addresses in 0xc0000000) - */ void *module_alloc(unsigned long size) { - if (size == 0) - return NULL; - return kmalloc(size, GFP_KERNEL); -} - -/* Free memory returned from module_alloc */ -void module_memfree(void *module_region) -{ - kfree(module_region); + return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END, + GFP_KERNEL, PAGE_KERNEL_EXEC, + VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, + __builtin_return_address(0)); } int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab, From patchwork Mon Sep 18 07:29:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388971 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2BB98CD37B0 for ; Mon, 18 Sep 2023 07:30:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BAAD76B0278; Mon, 18 Sep 2023 03:30:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B5AE36B027D; Mon, 18 Sep 2023 03:30:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9AD846B027E; Mon, 18 Sep 2023 03:30:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 8679C6B0278 for ; Mon, 18 Sep 2023 03:30:39 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 57AAEA07BF for ; Mon, 18 Sep 2023 07:30:39 +0000 (UTC) X-FDA: 81248895798.04.5567E62 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf26.hostedemail.com (Postfix) with ESMTP id 6E47A14000F for ; Mon, 18 Sep 2023 07:30:37 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TBLQcR1l; spf=pass (imf26.hostedemail.com: domain of rppt@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022237; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=W/azGQGI+uP/W0OvBaLCbphxVCzvPSm9O0Qscmq5+HA=; b=xml5+XJoXMYF5l+J7NewqADA30snZceWcEsW3PCIvgvze1a8ScT1xRuWHuqyZsGRs8sXFn ZQzUVpmGOjRzT4CMBvIBa1YqdTf1S7W5x2SGFGuXnb79gSorAk1z3u4O6YlmoBhkT793Cv OuzlWuAzdK8sF7i3fEI3qV01tyiMSbE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022237; a=rsa-sha256; cv=none; b=fBcnMYsc17X2c+68QBCG/wTrABw6PIOI9mI4WS8dX80fbSwCENLbNrtXXwKqfhNhg/eGYx ADYc+JGB9fHGgYe1VB/yM/8ftrQWuFnG8E3XmH6DHstl7dso7MfrDRcyYJjJPTBhGuZ3el u0CkFVCuiwC4mS1+tfZzkb88k/lhANo= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TBLQcR1l; spf=pass (imf26.hostedemail.com: domain of rppt@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id D32ADB80BEC; Mon, 18 Sep 2023 07:30:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0D537C433C8; Mon, 18 Sep 2023 07:30:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022234; bh=bvwcP2LqH7M0LqBQS8gC0daDhK3BG4ji8BhLvEjeq+s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TBLQcR1lAmU6KOalSsJ/oR1o5zZoazG/YXTfJUDqgJb0o6vPktmqTK+vjgO53nDe2 wRRsB421t+7jR7di9+tRCC1Vc5UL9ISE3HJXSgRpGXB2AcnDWCxIzabugyKHowlQRM dVtPneEfqW56D4trpvlIHzeEr1S//A3oRTZGBXCHfaluSiZJrflI+JHArvH50+TYYA RgIwhEVFloUsg24G4ybg8nt2Ea/Mr7bBH7Fum/wXH0+jrq62z5TRAralvtFuyV85hy FhD+mpWRLiJSUQyEBvo38Sy93YKxiF+tl1IvR4PCSFVan844bfOzzMlfFZccF2EytU +u0ufqp2nMUIg== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 02/13] mm: introduce execmem_text_alloc() and execmem_free() Date: Mon, 18 Sep 2023 10:29:44 +0300 Message-Id: <20230918072955.2507221-3-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Stat-Signature: ca5uz3ob1isggscodbsc5yij8y3787p4 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 6E47A14000F X-Rspam-User: X-HE-Tag: 1695022237-990351 X-HE-Meta: U2FsdGVkX19ZyNhnx31X1+mh4pv1WvLaAYkui7Ck4OPSV9YKd3ar0jh4PNgI8D4DRFg4rl59GE0tb6+mOQMu/CoJgIeachsUMie2xLiUdEyx6l01RcaonrIht9BgU7MYXs1QDWcAmWeOhavASbYrpeZDGIqksyfuO4Yaau7u2ZiojeQ6gDpOPp8XJ7z8KJJQ/Bfuz1VvdONV3w5v9+KW2gpNuQ79vLi34IprRHvQozpYOVI/jT95zxW2eH1LfJgLNXI3GpCxAIHCskXVMOH03oBS5UHjpQOyPng7Pz2efuRQ9KDoVUdoIslb+ngTo8bCgswabiCpkuxo13yB2JXY1UBygMWvp1WAE77WJv6GF604QI3fRzsWluKKTrLY/i7uhsf4lm5l0Kvrj2+H6VgpToXRk8xPG354vkQqwrZpcX+WA6Wqzr7AcsK+bSyMSz7XTOCf+pjX4HmT4W4qZPgP9QrYW2v9m5EBfjDfyxSFLTX0fZfkjIBHIs8lrTyb0y0Lp90uTd6y4Upuf2k4rLvoBKC8ZqtK3Wf1zzTAXxEn0HLKNzxftnR5zAIEY7GKJOD+CGsNgP0J8P7rTLXkjKeMDFGmkLQJAiOffH7L7wq3+jzMVTQIOY0RJEMGXsHTcK+s2xclvswYIM7SWKkDbnH9VMZEDmHzX1eonk2ZepDt4G8UDUhgZB8iwzvFdX9QFa+txAl3CH0XWJue2bafm9ozCQY7KTMkrUqzbZmgG3FUSAtzsG6DvKVCYNPfdf5GHuMqtYwYW/p+XYo0IkF16A3I7WszVVV4i13EAf9paW4DrNIW/J0k/ha6InyCaxEVO+w11hmZ7Ns/Tipo9Ok+DB/EqJsLtJn1YoDFUvwPSrZ9SeQlXZYPZ5ghUEnxbJ6NqDXBYfyO4TRVVIK9kjFj/2V0p+lkDsgJlnC8FrzqNSlcduUOVbpqDqCVdCk7v+bdN3RyyZOjBAx8gJRq8GOdVXj O5iQOS9B 0gZ5s12PwD1QS8itdUlf9XA+1iufFqA4+VQtmkWJ9+Ly9LyeQcCqFE2Q0mwr/S1eja2V4sh3TC59O7U+znFEOU55GmnseF5XH2Eoj9kSKxhOqCYXttx+W95l1U+cBbsusMioGA7hHbz4FLb51t0fIVJ7Iium2DN884UNTCWjvXT+RhkcMLdgjhQS4eHewSg8r0l/SpAwy++E7jqKPa4Xsq2z+nj2pTe8SauazpAiKNHOuVDmNjXOWj0PTitRlIndfMiSFk46hGR9ZMCei44I45ZI4A9RoDVkN8dGGrsE6ZdihsP9X/4tQ6zIY0KEpL/p3P4CLYK5ZoSRH60+Vn2+iFOcf4gRQZv05iNqcxOr6u9aGgoZnjjf6etYjxjHGNnwzEO4MWUVONNzKGHy5nEgwQlfR2Z/TeDe5uQOP2IahajxVt7G1BjapS2P/Qwa5U37U3xpZCUDrJxucJ58ZiyXMXygcFOV3MKEvY2dy X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" module_alloc() is used everywhere as a mean to allocate memory for code. Beside being semantically wrong, this unnecessarily ties all subsystems that need to allocate code, such as ftrace, kprobes and BPF to modules and puts the burden of code allocation to the modules code. Several architectures override module_alloc() because of various constraints where the executable memory can be located and this causes additional obstacles for improvements of code allocation. Start splitting code allocation from modules by introducing execmem_text_alloc() and execmem_free() APIs. Initially, execmem_text_alloc() is a wrapper for module_alloc() and execmem_free() is a replacement of module_memfree() to allow updating all call sites to use the new APIs. Since architectures define different restrictions on placement, permissions, alignment and other parameters for memory that can be used by different subsystems that allocate executable memory, execmem_text_alloc() takes a type argument, that will be used to identify the calling subsystem and to allow architectures define parameters for ranges suitable for that subsystem. The name execmem_text_alloc() emphasizes that the allocated memory is for executable code, the allocations of the associated data, like data sections of a module will use execmem_data_alloc() interface that will be added later. Signed-off-by: Mike Rapoport (IBM) --- arch/powerpc/kernel/kprobes.c | 4 +-- arch/s390/kernel/ftrace.c | 4 +-- arch/s390/kernel/kprobes.c | 4 +-- arch/s390/kernel/module.c | 5 +-- arch/sparc/net/bpf_jit_comp_32.c | 8 ++--- arch/x86/kernel/ftrace.c | 6 ++-- arch/x86/kernel/kprobes/core.c | 4 +-- include/linux/execmem.h | 56 ++++++++++++++++++++++++++++++++ include/linux/moduleloader.h | 3 -- kernel/bpf/core.c | 6 ++-- kernel/kprobes.c | 8 ++--- kernel/module/Kconfig | 1 + kernel/module/main.c | 25 +++++--------- mm/Kconfig | 3 ++ mm/Makefile | 1 + mm/execmem.c | 26 +++++++++++++++ 16 files changed, 120 insertions(+), 44 deletions(-) create mode 100644 include/linux/execmem.h create mode 100644 mm/execmem.c diff --git a/arch/powerpc/kernel/kprobes.c b/arch/powerpc/kernel/kprobes.c index b20ee72e873a..62228c7072a2 100644 --- a/arch/powerpc/kernel/kprobes.c +++ b/arch/powerpc/kernel/kprobes.c @@ -19,8 +19,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -130,7 +130,7 @@ void *alloc_insn_page(void) { void *page; - page = module_alloc(PAGE_SIZE); + page = execmem_text_alloc(EXECMEM_KPROBES, PAGE_SIZE); if (!page) return NULL; diff --git a/arch/s390/kernel/ftrace.c b/arch/s390/kernel/ftrace.c index c46381ea04ec..4052e10eb6a4 100644 --- a/arch/s390/kernel/ftrace.c +++ b/arch/s390/kernel/ftrace.c @@ -7,13 +7,13 @@ * Author(s): Martin Schwidefsky */ -#include #include #include #include #include #include #include +#include #include #include #include @@ -220,7 +220,7 @@ static int __init ftrace_plt_init(void) { const char *start, *end; - ftrace_plt = module_alloc(PAGE_SIZE); + ftrace_plt = execmem_text_alloc(EXECMEM_FTRACE, PAGE_SIZE); if (!ftrace_plt) panic("cannot allocate ftrace plt\n"); diff --git a/arch/s390/kernel/kprobes.c b/arch/s390/kernel/kprobes.c index d4b863ed0aa7..48928460dcb9 100644 --- a/arch/s390/kernel/kprobes.c +++ b/arch/s390/kernel/kprobes.c @@ -9,7 +9,6 @@ #define pr_fmt(fmt) "kprobes: " fmt -#include #include #include #include @@ -21,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -38,7 +38,7 @@ void *alloc_insn_page(void) { void *page; - page = module_alloc(PAGE_SIZE); + page = execmem_text_alloc(EXECMEM_KPROBES, PAGE_SIZE); if (!page) return NULL; set_memory_rox((unsigned long)page, 1); diff --git a/arch/s390/kernel/module.c b/arch/s390/kernel/module.c index 42215f9404af..db5561d0c233 100644 --- a/arch/s390/kernel/module.c +++ b/arch/s390/kernel/module.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -76,7 +77,7 @@ void *module_alloc(unsigned long size) #ifdef CONFIG_FUNCTION_TRACER void module_arch_cleanup(struct module *mod) { - module_memfree(mod->arch.trampolines_start); + execmem_free(mod->arch.trampolines_start); } #endif @@ -510,7 +511,7 @@ static int module_alloc_ftrace_hotpatch_trampolines(struct module *me, size = FTRACE_HOTPATCH_TRAMPOLINES_SIZE(s->sh_size); numpages = DIV_ROUND_UP(size, PAGE_SIZE); - start = module_alloc(numpages * PAGE_SIZE); + start = execmem_text_alloc(EXECMEM_FTRACE, numpages * PAGE_SIZE); if (!start) return -ENOMEM; set_memory_rox((unsigned long)start, numpages); diff --git a/arch/sparc/net/bpf_jit_comp_32.c b/arch/sparc/net/bpf_jit_comp_32.c index a74e5004c6c8..5fa9c45fba0a 100644 --- a/arch/sparc/net/bpf_jit_comp_32.c +++ b/arch/sparc/net/bpf_jit_comp_32.c @@ -1,10 +1,10 @@ // SPDX-License-Identifier: GPL-2.0 -#include #include #include #include #include #include +#include #include #include @@ -713,7 +713,7 @@ cond_branch: f_offset = addrs[i + filter[i].jf]; if (unlikely(proglen + ilen > oldproglen)) { pr_err("bpb_jit_compile fatal error\n"); kfree(addrs); - module_memfree(image); + execmem_free(image); return; } memcpy(image + proglen, temp, ilen); @@ -736,7 +736,7 @@ cond_branch: f_offset = addrs[i + filter[i].jf]; break; } if (proglen == oldproglen) { - image = module_alloc(proglen); + image = execmem_text_alloc(EXECMEM_BPF, proglen); if (!image) goto out; } @@ -758,7 +758,7 @@ cond_branch: f_offset = addrs[i + filter[i].jf]; void bpf_jit_free(struct bpf_prog *fp) { if (fp->jited) - module_memfree(fp->bpf_func); + execmem_free(fp->bpf_func); bpf_prog_unlock_free(fp); } diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c index 12df54ff0e81..ae56d79a6a74 100644 --- a/arch/x86/kernel/ftrace.c +++ b/arch/x86/kernel/ftrace.c @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -261,15 +262,14 @@ void arch_ftrace_update_code(int command) #ifdef CONFIG_X86_64 #ifdef CONFIG_MODULES -#include /* Module allocation simplifies allocating memory for code */ static inline void *alloc_tramp(unsigned long size) { - return module_alloc(size); + return execmem_text_alloc(EXECMEM_FTRACE, size); } static inline void tramp_free(void *tramp) { - module_memfree(tramp); + execmem_free(tramp); } #else /* Trampolines can only be created if modules are supported */ diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index e8babebad7b8..c4f58e893efd 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -40,12 +40,12 @@ #include #include #include -#include #include #include #include #include #include +#include #include #include @@ -448,7 +448,7 @@ void *alloc_insn_page(void) { void *page; - page = module_alloc(PAGE_SIZE); + page = execmem_text_alloc(EXECMEM_KPROBES, PAGE_SIZE); if (!page) return NULL; diff --git a/include/linux/execmem.h b/include/linux/execmem.h new file mode 100644 index 000000000000..3491bf7e9714 --- /dev/null +++ b/include/linux/execmem.h @@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_EXECMEM_ALLOC_H +#define _LINUX_EXECMEM_ALLOC_H + +#include + +/** + * enum execmem_type - types of executable memory ranges + * + * There are several subsystems that allocate executable memory. + * Architectures define different restrictions on placement, + * permissions, alignment and other parameters for memory that can be used + * by these subsystems. + * Types in this enum identify subsystems that allocate executable memory + * and let architectures define parameters for ranges suitable for + * allocations by each subsystem. + * + * @EXECMEM_DEFAULT: default parameters that would be used for types that + * are not explcitly defined. + * @EXECMEM_MODULE_TEXT: parameters for module text sections + * @EXECMEM_KPROBES: parameters for kprobes + * @EXECMEM_FTRACE: parameters for ftrace + * @EXECMEM_BPF: parameters for BPF + * @EXECMEM_TYPE_MAX: + */ +enum execmem_type { + EXECMEM_DEFAULT, + EXECMEM_MODULE_TEXT = EXECMEM_DEFAULT, + EXECMEM_KPROBES, + EXECMEM_FTRACE, + EXECMEM_BPF, + EXECMEM_TYPE_MAX, +}; + +/** + * execmem_text_alloc - allocate executable memory + * @type: type of the allocation + * @size: how many bytes of memory are required + * + * Allocates memory that will contain executable code, either generated or + * loaded from kernel modules. + * + * The memory will have protections defined by architecture for executable + * region of the @type. + * + * Return: a pointer to the allocated memory or %NULL + */ +void *execmem_text_alloc(enum execmem_type type, size_t size); + +/** + * execmem_free - free executable memory + * @ptr: pointer to the memory that should be freed + */ +void execmem_free(void *ptr); + +#endif /* _LINUX_EXECMEM_ALLOC_H */ diff --git a/include/linux/moduleloader.h b/include/linux/moduleloader.h index 001b2ce83832..a23718aa2f4d 100644 --- a/include/linux/moduleloader.h +++ b/include/linux/moduleloader.h @@ -29,9 +29,6 @@ unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section); sections. Returns NULL on failure. */ void *module_alloc(unsigned long size); -/* Free memory returned from module_alloc. */ -void module_memfree(void *module_region); - /* Determines if the section name is an init section (that is only used during * module loading). */ diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 4e3ce0542e31..75249f2d9f77 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -37,6 +36,7 @@ #include #include #include +#include #include #include @@ -1007,12 +1007,12 @@ void bpf_jit_uncharge_modmem(u32 size) void *__weak bpf_jit_alloc_exec(unsigned long size) { - return module_alloc(size); + return execmem_text_alloc(EXECMEM_BPF, size); } void __weak bpf_jit_free_exec(void *addr) { - module_memfree(addr); + execmem_free(addr); } struct bpf_binary_header * diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 0c6185aefaef..0ccb4d2ec9a2 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -39,6 +38,7 @@ #include #include #include +#include #include #include @@ -113,17 +113,17 @@ enum kprobe_slot_state { void __weak *alloc_insn_page(void) { /* - * Use module_alloc() so this page is within +/- 2GB of where the + * Use execmem_text_alloc() so this page is within +/- 2GB of where the * kernel image and loaded module images reside. This is required * for most of the architectures. * (e.g. x86-64 needs this to handle the %rip-relative fixups.) */ - return module_alloc(PAGE_SIZE); + return execmem_text_alloc(EXECMEM_KPROBES, PAGE_SIZE); } static void free_insn_page(void *page) { - module_memfree(page); + execmem_free(page); } struct kprobe_insn_cache kprobe_insn_slots = { diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig index 33a2e991f608..813e116bdee6 100644 --- a/kernel/module/Kconfig +++ b/kernel/module/Kconfig @@ -2,6 +2,7 @@ menuconfig MODULES bool "Enable loadable module support" modules + select EXECMEM help Kernel modules are small pieces of compiled code which can be inserted in the running kernel, rather than being diff --git a/kernel/module/main.c b/kernel/module/main.c index 98fedfdb8db5..4ec982cc943c 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -57,6 +57,7 @@ #include #include #include +#include #include #include "internal.h" @@ -1179,16 +1180,6 @@ resolve_symbol_wait(struct module *mod, return ksym; } -void __weak module_memfree(void *module_region) -{ - /* - * This memory may be RO, and freeing RO memory in an interrupt is not - * supported by vmalloc. - */ - WARN_ON(in_interrupt()); - vfree(module_region); -} - void __weak module_arch_cleanup(struct module *mod) { } @@ -1207,7 +1198,7 @@ static void *module_memory_alloc(unsigned int size, enum mod_mem_type type) { if (mod_mem_use_vmalloc(type)) return vzalloc(size); - return module_alloc(size); + return execmem_text_alloc(EXECMEM_MODULE_TEXT, size); } static void module_memory_free(void *ptr, enum mod_mem_type type) @@ -1215,7 +1206,7 @@ static void module_memory_free(void *ptr, enum mod_mem_type type) if (mod_mem_use_vmalloc(type)) vfree(ptr); else - module_memfree(ptr); + execmem_free(ptr); } static void free_mod_mem(struct module *mod) @@ -2479,9 +2470,9 @@ static void do_free_init(struct work_struct *w) llist_for_each_safe(pos, n, list) { initfree = container_of(pos, struct mod_initfree, node); - module_memfree(initfree->init_text); - module_memfree(initfree->init_data); - module_memfree(initfree->init_rodata); + execmem_free(initfree->init_text); + execmem_free(initfree->init_data); + execmem_free(initfree->init_rodata); kfree(initfree); } } @@ -2584,10 +2575,10 @@ static noinline int do_init_module(struct module *mod) * We want to free module_init, but be aware that kallsyms may be * walking this with preempt disabled. In all the failure paths, we * call synchronize_rcu(), but we don't want to slow down the success - * path. module_memfree() cannot be called in an interrupt, so do the + * path. execmem_free() cannot be called in an interrupt, so do the * work and call synchronize_rcu() in a work queue. * - * Note that module_alloc() on most architectures creates W+X page + * Note that execmem_text_alloc() on most architectures creates W+X page * mappings which won't be cleaned up until do_free_init() runs. Any * code such as mark_rodata_ro() which depends on those mappings to * be cleaned up needs to sync with the queued work - ie diff --git a/mm/Kconfig b/mm/Kconfig index 264a2df5ecf5..fb12931238e8 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1258,6 +1258,9 @@ config LOCK_MM_AND_FIND_VMA bool depends on !STACK_GROWSUP +config EXECMEM + bool + source "mm/damon/Kconfig" endmenu diff --git a/mm/Makefile b/mm/Makefile index ec65984e2ade..2e5fec94f09c 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o +obj-$(CONFIG_EXECMEM) += execmem.o diff --git a/mm/execmem.c b/mm/execmem.c new file mode 100644 index 000000000000..638dc2b26a81 --- /dev/null +++ b/mm/execmem.c @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include + +static void *execmem_alloc(size_t size) +{ + return module_alloc(size); +} + +void *execmem_text_alloc(enum execmem_type type, size_t size) +{ + return execmem_alloc(size); +} + +void execmem_free(void *ptr) +{ + /* + * This memory may be RO, and freeing RO memory in an interrupt is not + * supported by vmalloc. + */ + WARN_ON(in_interrupt()); + vfree(ptr); +} From patchwork Mon Sep 18 07:29:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388972 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7F9B9C46CA1 for ; Mon, 18 Sep 2023 07:30:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 14AF16B027D; Mon, 18 Sep 2023 03:30:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0D3F66B027F; Mon, 18 Sep 2023 03:30:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EB8176B0280; Mon, 18 Sep 2023 03:30:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D21D56B027D for ; Mon, 18 Sep 2023 03:30:52 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8E5BD120BB0 for ; Mon, 18 Sep 2023 07:30:52 +0000 (UTC) X-FDA: 81248896344.29.15174D0 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf03.hostedemail.com (Postfix) with ESMTP id 3F85720013 for ; Mon, 18 Sep 2023 07:30:49 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sSCiLbiN; spf=pass (imf03.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022251; a=rsa-sha256; cv=none; b=6IZ8zy6a0lNgdgYT5QsGSanPllFNmW9ZXQ9g/NIDLHsH1Gt0eusmmJN4XkFxquKQW8fGs3 8YGIhLstkjLa5pKWVlOnEoRINxKV7Te9M8+NgNleytqhDFjlUjZ3eCEVjO307QimqlsPIN I7M1OQxn1GuiwEcn/ONFxR/EUskd08c= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sSCiLbiN; spf=pass (imf03.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022250; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QiFG+H2gwHWRM2G3A0YywAW1M0fMEUih/EbPX4rtawY=; b=lik7ktRda7X1WPm87w5s/PhS3mjXGLz3G8BVsAs/YdejLvuntdM1ofIIxpnFq7hBdwpIdm j7tuozSS9JcPmOCk3+myGRTs0aFKXiIKwLRBPOrYu9gZd7bFAWL4VG3cuKh7hb/Mkym3/a XtsqjGnRg+7pdqbkkkg+5Axn9n1Wsyk= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 19782CE0B82; Mon, 18 Sep 2023 07:30:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F1667C433AB; Mon, 18 Sep 2023 07:30:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022244; bh=sRqJHHj6sN+zVuxAlEgaERQMpk9iFfs+P0v/5bsjzqU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sSCiLbiNncF007b8yg/PCaamnCR3IktSTan43cauE8/7iFy9m2xixDIpEVuIJReLP jrggUxEzdIvYURXnGZUsrjawe2u27YGN2kA82YiX1l3NRKgcmaNnhRD4rJ3lwcYZFH kcsyJaoBejSzi/mjeTbKUXDpZatwwEE68FNOT2kvW74EcuAw7/7pEI72l1dj86JMJG tO7yZMu8nliG1qmp1eySQMtAnE8tCUfbmYaCkFhY/eq4QY0IuzpNmP6jmRXX4716Qz r28qd7FAGP4eyZIrKeBmxc2Zjbl6SHjAiWIHvssIlogTeakI1PofbFTxkyfvNtlbRA f8aCB2/C6EHLw== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 03/13] mm/execmem, arch: convert simple overrides of module_alloc to execmem Date: Mon, 18 Sep 2023 10:29:45 +0300 Message-Id: <20230918072955.2507221-4-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 3F85720013 X-Stat-Signature: 3owzgx8k3jybjp6boghiaaixnf7amgbm X-Rspam-User: X-HE-Tag: 1695022249-601090 X-HE-Meta: U2FsdGVkX194PiIIJ1ITgBlUoaajxQ1C2WEegWIuZe7iiU8MFS8/ts49yuZzwsjkaJmCuFRNdrYDcDPlMF8mnQ/yXho23lD+cQ06PVLnhX0PGP1TCiCwO3F2rQWqex27dDtGbzJnfyDH1jYWkCxC2uguUhRe+UnrR40f+ju/+03Ntl1ibMcRiVd4cl3wo+IZfijqNfUDst2IXpfBOcMF+vflQmDTDixd0RtuS3oG6BL30ODGUzcNVk3WpLFWqa2d8k9l3lh83QbbuseSkVylWYWpsT3LtX1jxjKYVZtua/WQwQSJlWymaoCzJYQJU3qUyUEact56YBwj4F3Rrne6lrxs0oaUne1jZe5aha9H/voPj1eYARePZMmoQC1/HKtLQMW+4kSdYG4VKrH96F5iDsUZdGC/fLWQdJevg7EjX/J/3sVy0ab6OBMfT++7hfHd6DPYHU0yCGtbCv25bl/RYCJ7jAhG0PolhyOgUtqcti3ltfB7KGwzMmxgmnRbcAGymz+Jzn/yNM2Hv2utaHoG4B0kci2Rlo7cufXMSG71OZ53u3dIY0ANiqBZb+F9QPQI9nJ2ALztVJ8IieJ1yJw4q98pLhQ6YCp9pI+cC/kSpLICgNScpRDKw7R5LTJQoDjWDX8uU6qfvo5vd834m0TRyLURT8YAdIrCSALE0bbt8L5gO2NU9yvHpO0gCqo8shlQskJxxMTsdE+rE2PJgnrgSld+CJ8BnwcQJ5Qs4HT8q7o6yfDR7m/zG6G9FapsH9gRa0XFYz4fU5zDKiMomf8TYSv1dqUIngwmMLlZh/MXMToCDHr6kQFixfFv5sV4P1Qbzr4RnrbWjV2dYKzmHCgttCb4w35w1tyUsRJUke7sp9L6HdqtZFg3rkFQFIMZEALF3tKY+t5RkqOCHzqxjzhdm6YmUpOay3CbYPPqatWQtQnkDK+rId1orRMV9D4VVCs1Kpp3Q8Ye1XStH3T9awF vua5Zpyy ocn5R4hmKN1AHdsFJIGs5JdbUFh+mZODVcH3pKf6zQByeYlhahZdFDqDPzBbDDRrCZFmX79Wif7lXql06eb61ngxpgQPQVHqzwly2TBy/ZSt37X57JevrkuxxER6tcC3GMT63tenjsgBdCW/S0Hd6vt1C1/3HddzMfCzFUKlkr3pOsFxSF9MAZx5zaTL+l6U6Svlj8rSDAPI0ZNjqiGOMMpi/bUUG1WTrkwU+aWyZpGta7SN42biQcMleOLh4sl4ui6bg9XsJMqOPxuIJT25EFIvpCJKaz21jUk8WHhwW+uUcXCT6DQ3SV9VeC1al/m5LehNWQBuA12hbDIrzkTa+ZhoV/x3yIJ2+nPuZ0VJHVID01kAsANoeNj7ajrftXsJs0na/G0/CLw/zJBMbJNnbwM3WUtY01TbYhn8DbwWld3eJJcc7QYk2P6wwqJvUPOi/ldZSA1+libH/nHs= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" Several architectures override module_alloc() only to define address range for code allocations different than VMALLOC address space. Provide a generic implementation in execmem that uses the parameters for address space ranges, required alignment and page protections provided by architectures. The architectures must fill execmem_params structure and implement execmem_arch_params() that returns a pointer to that structure. This way the execmem initialization won't be called from every architecture, but rather from a central place, namely initialization of the core memory management. The execmem provides execmem_text_alloc() API that wraps __vmalloc_node_range() with the parameters defined by the architectures. If an architecture does not implement execmem_arch_params(), execmem_text_alloc() will fall back to module_alloc(). The name execmem_text_alloc() emphasizes that the allocated memory is for executable code, the allocations of the associated data, like data sections of a module will use execmem_data_alloc() interface that will be added later. Signed-off-by: Mike Rapoport (IBM) --- arch/loongarch/kernel/module.c | 18 ++++++++-- arch/mips/kernel/module.c | 19 +++++++--- arch/nios2/kernel/module.c | 19 +++++++--- arch/parisc/kernel/module.c | 23 +++++++----- arch/riscv/kernel/module.c | 20 ++++++++--- arch/sparc/kernel/module.c | 44 +++++++++++------------ include/linux/execmem.h | 44 +++++++++++++++++++++++ mm/execmem.c | 66 ++++++++++++++++++++++++++++++++-- mm/mm_init.c | 2 ++ 9 files changed, 203 insertions(+), 52 deletions(-) diff --git a/arch/loongarch/kernel/module.c b/arch/loongarch/kernel/module.c index b8b86088b2dd..a1d8fe9796fa 100644 --- a/arch/loongarch/kernel/module.c +++ b/arch/loongarch/kernel/module.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -469,10 +470,21 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, return 0; } -void *module_alloc(unsigned long size) +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .pgprot = PAGE_KERNEL, + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) { - return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END, - GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE, __builtin_return_address(0)); + execmem_params.ranges[EXECMEM_DEFAULT].start = MODULES_VADDR; + execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; + + return &execmem_params; } static void module_init_ftrace_plt(const Elf_Ehdr *hdr, diff --git a/arch/mips/kernel/module.c b/arch/mips/kernel/module.c index 0c936cbf20c5..1c959074b35f 100644 --- a/arch/mips/kernel/module.c +++ b/arch/mips/kernel/module.c @@ -20,6 +20,7 @@ #include #include #include +#include extern void jump_label_apply_nops(struct module *mod); @@ -33,11 +34,21 @@ static LIST_HEAD(dbe_list); static DEFINE_SPINLOCK(dbe_lock); #ifdef MODULE_START -void *module_alloc(unsigned long size) +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .start = MODULE_START, + .end = MODULE_END, + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) { - return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END, - GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE, - __builtin_return_address(0)); + execmem_params.ranges[EXECMEM_DEFAULT].pgprot = PAGE_KERNEL; + + return &execmem_params; } #endif diff --git a/arch/nios2/kernel/module.c b/arch/nios2/kernel/module.c index 9c97b7513853..5a8df4f9c04e 100644 --- a/arch/nios2/kernel/module.c +++ b/arch/nios2/kernel/module.c @@ -18,15 +18,24 @@ #include #include #include +#include #include -void *module_alloc(unsigned long size) +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .start = MODULES_VADDR, + .end = MODULES_END, + .pgprot = PAGE_KERNEL_EXEC, + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) { - return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END, - GFP_KERNEL, PAGE_KERNEL_EXEC, - VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, - __builtin_return_address(0)); + return &execmem_params; } int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab, diff --git a/arch/parisc/kernel/module.c b/arch/parisc/kernel/module.c index d214bbe3c2af..0c6dfd1daef3 100644 --- a/arch/parisc/kernel/module.c +++ b/arch/parisc/kernel/module.c @@ -49,6 +49,7 @@ #include #include #include +#include #include #include @@ -173,15 +174,21 @@ static inline int reassemble_22(int as22) ((as22 & 0x0003ff) << 3)); } -void *module_alloc(unsigned long size) +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .pgprot = PAGE_KERNEL_RWX, + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) { - /* using RWX means less protection for modules, but it's - * easier than trying to map the text, data, init_text and - * init_data correctly */ - return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, - GFP_KERNEL, - PAGE_KERNEL_RWX, 0, NUMA_NO_NODE, - __builtin_return_address(0)); + execmem_params.ranges[EXECMEM_DEFAULT].start = VMALLOC_START; + execmem_params.ranges[EXECMEM_DEFAULT].end = VMALLOC_END; + + return &execmem_params; } #ifndef CONFIG_64BIT diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 7c651d55fcbd..343a0edfb6dd 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -436,12 +437,21 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, } #if defined(CONFIG_MMU) && defined(CONFIG_64BIT) -void *module_alloc(unsigned long size) +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .pgprot = PAGE_KERNEL, + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) { - return __vmalloc_node_range(size, 1, MODULES_VADDR, - MODULES_END, GFP_KERNEL, - PAGE_KERNEL, 0, NUMA_NO_NODE, - __builtin_return_address(0)); + execmem_params.ranges[EXECMEM_DEFAULT].start = MODULES_VADDR; + execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; + + return &execmem_params; } #endif diff --git a/arch/sparc/kernel/module.c b/arch/sparc/kernel/module.c index 66c45a2764bc..1d8d1fba95b9 100644 --- a/arch/sparc/kernel/module.c +++ b/arch/sparc/kernel/module.c @@ -14,6 +14,10 @@ #include #include #include +#include +#ifdef CONFIG_SPARC64 +#include +#endif #include #include @@ -21,34 +25,26 @@ #include "entry.h" +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { #ifdef CONFIG_SPARC64 - -#include - -static void *module_map(unsigned long size) -{ - if (PAGE_ALIGN(size) > MODULES_LEN) - return NULL; - return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END, - GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE, - __builtin_return_address(0)); -} + .start = MODULES_VADDR, + .end = MODULES_END, #else -static void *module_map(unsigned long size) -{ - return vmalloc(size); -} -#endif /* CONFIG_SPARC64 */ - -void *module_alloc(unsigned long size) + .start = VMALLOC_START, + .end = VMALLOC_END, +#endif + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) { - void *ret; - - ret = module_map(size); - if (ret) - memset(ret, 0, size); + execmem_params.ranges[EXECMEM_DEFAULT].pgprot = PAGE_KERNEL; - return ret; + return &execmem_params; } /* Make generic code ignore STT_REGISTER dummy undefined symbols. */ diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 3491bf7e9714..44e213625053 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -32,6 +32,44 @@ enum execmem_type { EXECMEM_TYPE_MAX, }; +/** + * struct execmem_range - definition of a memory range suitable for code and + * related data allocations + * @start: address space start + * @end: address space end (inclusive) + * @pgprot: permissions for memory in this address space + * @alignment: alignment required for text allocations + */ +struct execmem_range { + unsigned long start; + unsigned long end; + pgprot_t pgprot; + unsigned int alignment; +}; + +/** + * struct execmem_params - architecture parameters for code allocations + * @ranges: array of ranges defining architecture specific parameters for + * each type of executable memory allocations + */ +struct execmem_params { + struct execmem_range ranges[EXECMEM_TYPE_MAX]; +}; + +/** + * execmem_arch_params - supply parameters for allocations of executable memory + * + * A hook for architectures to define parameters for allocations of + * executable memory described by struct execmem_params + * + * For architectures that do not implement this method a default set of + * parameters will be used + * + * Return: a structure defining architecture parameters and restrictions + * for allocations of executable memory + */ +struct execmem_params *execmem_arch_params(void); + /** * execmem_text_alloc - allocate executable memory * @type: type of the allocation @@ -53,4 +91,10 @@ void *execmem_text_alloc(enum execmem_type type, size_t size); */ void execmem_free(void *ptr); +#ifdef CONFIG_EXECMEM +void execmem_init(void); +#else +static inline void execmem_init(void) {} +#endif + #endif /* _LINUX_EXECMEM_ALLOC_H */ diff --git a/mm/execmem.c b/mm/execmem.c index 638dc2b26a81..f25a5e064886 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -5,14 +5,26 @@ #include #include -static void *execmem_alloc(size_t size) +static struct execmem_params execmem_params; + +static void *execmem_alloc(size_t size, struct execmem_range *range) { - return module_alloc(size); + unsigned long start = range->start; + unsigned long end = range->end; + unsigned int align = range->alignment; + pgprot_t pgprot = range->pgprot; + + return __vmalloc_node_range(size, align, start, end, + GFP_KERNEL, pgprot, VM_FLUSH_RESET_PERMS, + NUMA_NO_NODE, __builtin_return_address(0)); } void *execmem_text_alloc(enum execmem_type type, size_t size) { - return execmem_alloc(size); + if (!execmem_params.ranges[type].start) + return module_alloc(size); + + return execmem_alloc(size, &execmem_params.ranges[type]); } void execmem_free(void *ptr) @@ -24,3 +36,51 @@ void execmem_free(void *ptr) WARN_ON(in_interrupt()); vfree(ptr); } + +struct execmem_params * __weak execmem_arch_params(void) +{ + return NULL; +} + +static bool execmem_validate_params(struct execmem_params *p) +{ + struct execmem_range *r = &p->ranges[EXECMEM_DEFAULT]; + + if (!r->alignment || !r->start || !r->end || !pgprot_val(r->pgprot)) { + pr_crit("Invalid parameters for execmem allocator, module loading will fail"); + return false; + } + + return true; +} + +static void execmem_init_missing(struct execmem_params *p) +{ + struct execmem_range *default_range = &p->ranges[EXECMEM_DEFAULT]; + + for (int i = EXECMEM_DEFAULT + 1; i < EXECMEM_TYPE_MAX; i++) { + struct execmem_range *r = &p->ranges[i]; + + if (!r->start) { + r->pgprot = default_range->pgprot; + r->alignment = default_range->alignment; + r->start = default_range->start; + r->end = default_range->end; + } + } +} + +void __init execmem_init(void) +{ + struct execmem_params *p = execmem_arch_params(); + + if (!p) + return; + + if (!execmem_validate_params(p)) + return; + + execmem_init_missing(p); + + execmem_params = *p; +} diff --git a/mm/mm_init.c b/mm/mm_init.c index 50f2f34745af..7c002b36da21 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "internal.h" #include "slab.h" #include "shuffle.h" @@ -2797,4 +2798,5 @@ void __init mm_core_init(void) pti_init(); kmsan_init_runtime(); mm_cache_init(); + execmem_init(); } From patchwork Mon Sep 18 07:29:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388973 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 63DA7CD37B0 for ; Mon, 18 Sep 2023 07:30:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F06D86B027F; Mon, 18 Sep 2023 03:30:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EB6E56B0281; Mon, 18 Sep 2023 03:30:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D09656B0282; Mon, 18 Sep 2023 03:30:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BB0DA6B027F for ; Mon, 18 Sep 2023 03:30:57 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7AA32C0C23 for ; Mon, 18 Sep 2023 07:30:57 +0000 (UTC) X-FDA: 81248896554.29.C09ADE9 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf08.hostedemail.com (Postfix) with ESMTP id BB44E160017 for ; Mon, 18 Sep 2023 07:30:55 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=B3MpqZd8; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf08.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022255; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=pR/DurLeWvhG1FKfSYFkfT2vHvRwZ8tEPH/eAXER1RU=; b=qQp0hOtK3xcaG6vM+38LBgc2CwrHbzyU26GimzNMp981bXUFqd2naK4HlTQRx6a4agPJWK GgJ5gt7VJzyla7M0OcFkT375Y19QtoB+zG9f27bVWOVpI2b0mMj/zLP2wdZanTXeypWLxn SwSSU+NWZsnCveEfqwumzftjZO4Afmw= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=B3MpqZd8; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf08.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022255; a=rsa-sha256; cv=none; b=IzLV10BO8RXETJGJ9D1Iux4ekzJjZtHqBP7AxQmlsUBTvpTMXQvMAKZVQSjyFzsaJ4s6EC IV5ALmppgpd47T6bU0tvLJ9YdUOaUUukZoEgx36D3TewtZRTJLygG2mFmlCO5D4EsQs5eV 4c8gYazlbc7iY7R1U858GG5SAW0I4rs= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id AF9D260F83; Mon, 18 Sep 2023 07:30:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AFDC9C433B9; Mon, 18 Sep 2023 07:30:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022253; bh=Ab3JxL0vUtd+Dgg9Evr/24yil9x7cJIlVNJ+mIHZ+Fw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B3MpqZd8kSnshGSIZk32sAvbfxOLIcvRzx/L6e6twO1GyIkCPbyDJ8YFZK7YV3DYj XGhAnNQsgAWy/xSCUlo5IqNl0WBTUqkmBh0vCS1bt48EfgT1+dVwPuJNvN1ivOBOMn gvH0utW4rdMOUN0nD6KFxNUfq8geoRPWIJ6VKmk1aH2z09LsV+3CM1A5GN8hUlJfXJ ASdeswxXtYTc0LaHjCD2b/Jj+5rTLeYIx1gwiLh7ZEjRZ1XJfx7lSWfp5ovjLk7FgH t/b+tjGlJaBG8XvWXgVzie1/pkYA3m8DAD1W+ztdCKD9/UtdKJD1oiqOVjKdWg5YrB ItW+yxqIqOhzA== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 04/13] mm/execmem, arch: convert remaining overrides of module_alloc to execmem Date: Mon, 18 Sep 2023 10:29:46 +0300 Message-Id: <20230918072955.2507221-5-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: BB44E160017 X-Stat-Signature: 4bxdo73f5xenoqtzektghixuus13ag6x X-Rspam-User: X-HE-Tag: 1695022255-87998 X-HE-Meta: U2FsdGVkX1+kJcv/kZtNw8dgzKeRHeZog8oci+Y2nx0Gk45FePZ3CM8ig4jfxTDpxn/rL55JFCZZTLMKb4/edjxPXeF//0Do9Tn/qQIaVQ3rh6UzqxozQ/TSnTLDx2S455hsQy4Poqxe5yrNAtQmUZXEWbHhBGZwT3iGvZm6Ncnrj9ez81JUFzwdHsNkb1ihPt3ozJOEYsQk0JV4EvvisVqBk2yx88hpHWUCHf8av3IH527ZRLJSEFEFn//tOhJDBTBw2I7ZiKHy+5NQlwDKbG85TCOl/xtf6tU/1Yioc3U6VAYnxM2yL370bQOQpnj8w6KSMlwRJa52pw91HA7wBL54C38KjrduK/or9bpwZOO1Hhg/V9GEwMTDJWoR0t4KTNV8vNg9Q3avSSCNHgb1hSf0eUmkq02yFhEyXgDlb6QJQizWv7jNaBHrjg9jGOtmpgOyg/W0pv34YtnvyLQK7PQWsRN1w90YaaVK/hAM6Dm0V5JKsSDXRfKR1rg9Cw7MNtvJJsEo/ghrpNYl22C35FZz05NvsWebSyMy7O8vlSlcigntBXMTuOxg7Bx9cwjQ0rN7xTPuTh2JgBuM5t+6gx6oSUPUiE2TUragVM3xsTkzMQCH0FlaMLHugmZaF4P5/HDLHyLNP9ANC2zzPgSn+lVNIBDoixFl7c63+jDvuj1oUBw9eHEonEzPZQ+Ms4hfGzoC0pdppjXs8tReLPncmAQt1prTD5bKfFkoZG+IcueZF+iIrsQmHH2FFGYpjDcvMIqoVYD1gg5jsC2DuPGMLTEHcZYeDiF7A/TL9IeD9vkAF3HxWTGfxfxfeVg7Zi+lpAW9z5BWFIQrdezTgce+4xR3Kr80TSO4+zJDhui9CQ+VVgg0G0Btwe66ypCcB4bn/bbY/jLWXELNac2J0dKJZNTqEd3Qib6x71uzOnyt7hSZ4uTmW0rMjk387dRG1urvVfljIS0KP2UPuwi+V8l 4k1Em/mA G6PxeR94ydjatFsPrp5wiAQkEOd6jPEDTY+/RIldy8UaCoYPkOc+adY7g4/BP7uJMCbrVyJbMJrGnzD68MM8ih8Iz/Y6ViDcpjCRu2xB0If9icK1FPRYCK64TXPq1VPUtYwm8SDoLsBMiyQtw0QbdXE6X9ISIKhBebXoQrpZioJA0uncgteFcglLGVi6N+pZsojKa2DSCgOkv/c4wi3jVkQil9LLWNK3adgk7oDaoOVFymUGnXI3GtzyXl1A1NP93QwY3u2lF5gd+z3Mw48WsPc7BeQzmEnlnvpYObEDCscrs72rb9KT4RMCOGeOn7gjhHvNYYFKauu49lJ1+Li0zEVFKYO9/sqZ3eP9lBn4WpVuOM40j9HulrdEXQ4MDwhdSqZZQPeiGb/PDXQXMAd0v8REbtH3AxrRXAq9mkM6l4Uq5v/AOqtNyN5TdgB5a5XHtNHYUxLX0pdVHl8I= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" Extend execmem parameters to accommodate more complex overrides of module_alloc() by architectures. This includes specification of a fallback range required by arm, arm64 and powerpc and support for allocation of KASAN shadow required by arm64, s390 and x86. The core implementation of execmem_alloc() takes care of suppressing warnings when the initial allocation fails but there is a fallback range defined. Signed-off-by: Mike Rapoport (IBM) --- arch/arm/kernel/module.c | 38 ++++++++++++--------- arch/arm64/kernel/module.c | 57 ++++++++++++++------------------ arch/powerpc/kernel/module.c | 52 ++++++++++++++--------------- arch/s390/kernel/module.c | 52 +++++++++++------------------ arch/x86/kernel/module.c | 64 +++++++++++------------------------- include/linux/execmem.h | 14 ++++++++ mm/execmem.c | 43 ++++++++++++++++++++++-- 7 files changed, 167 insertions(+), 153 deletions(-) diff --git a/arch/arm/kernel/module.c b/arch/arm/kernel/module.c index e74d84f58b77..2c7651a2d84c 100644 --- a/arch/arm/kernel/module.c +++ b/arch/arm/kernel/module.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -34,23 +35,28 @@ #endif #ifdef CONFIG_MMU -void *module_alloc(unsigned long size) +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .start = MODULES_VADDR, + .end = MODULES_END, + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) { - gfp_t gfp_mask = GFP_KERNEL; - void *p; - - /* Silence the initial allocation */ - if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS)) - gfp_mask |= __GFP_NOWARN; - - p = __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END, - gfp_mask, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE, - __builtin_return_address(0)); - if (!IS_ENABLED(CONFIG_ARM_MODULE_PLTS) || p) - return p; - return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, - GFP_KERNEL, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE, - __builtin_return_address(0)); + struct execmem_range *r = &execmem_params.ranges[EXECMEM_DEFAULT]; + + r->pgprot = PAGE_KERNEL_EXEC; + + if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS)) { + r->fallback_start = VMALLOC_START; + r->fallback_end = VMALLOC_END; + } + + return &execmem_params; } #endif diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c index dd851297596e..cd6320de1c54 100644 --- a/arch/arm64/kernel/module.c +++ b/arch/arm64/kernel/module.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -108,46 +109,38 @@ static int __init module_init_limits(void) return 0; } -subsys_initcall(module_init_limits); -void *module_alloc(unsigned long size) +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .flags = EXECMEM_KASAN_SHADOW, + .alignment = MODULE_ALIGN, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) { - void *p = NULL; + struct execmem_range *r = &execmem_params.ranges[EXECMEM_DEFAULT]; - /* - * Where possible, prefer to allocate within direct branch range of the - * kernel such that no PLTs are necessary. - */ - if (module_direct_base) { - p = __vmalloc_node_range(size, MODULE_ALIGN, - module_direct_base, - module_direct_base + SZ_128M, - GFP_KERNEL | __GFP_NOWARN, - PAGE_KERNEL, 0, NUMA_NO_NODE, - __builtin_return_address(0)); - } + module_init_limits(); - if (!p && module_plt_base) { - p = __vmalloc_node_range(size, MODULE_ALIGN, - module_plt_base, - module_plt_base + SZ_2G, - GFP_KERNEL | __GFP_NOWARN, - PAGE_KERNEL, 0, NUMA_NO_NODE, - __builtin_return_address(0)); - } + r->pgprot = PAGE_KERNEL; - if (!p) { - pr_warn_ratelimited("%s: unable to allocate memory\n", - __func__); - } + if (module_direct_base) { + r->start = module_direct_base; + r->end = module_direct_base + SZ_128M; - if (p && (kasan_alloc_module_shadow(p, size, GFP_KERNEL) < 0)) { - vfree(p); - return NULL; + if (module_plt_base) { + r->fallback_start = module_plt_base; + r->fallback_end = module_plt_base + SZ_2G; + } + } else if (module_plt_base) { + r->start = module_plt_base; + r->end = module_plt_base + SZ_2G; } - /* Memory is intended to be executable, reset the pointer tag. */ - return kasan_reset_tag(p); + return &execmem_params; } enum aarch64_reloc_op { diff --git a/arch/powerpc/kernel/module.c b/arch/powerpc/kernel/module.c index f6d6ae0a1692..f4dd26f693a3 100644 --- a/arch/powerpc/kernel/module.c +++ b/arch/powerpc/kernel/module.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -89,39 +90,38 @@ int module_finalize(const Elf_Ehdr *hdr, return 0; } -static __always_inline void * -__module_alloc(unsigned long size, unsigned long start, unsigned long end, bool nowarn) +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) { pgprot_t prot = strict_module_rwx_enabled() ? PAGE_KERNEL : PAGE_KERNEL_EXEC; - gfp_t gfp = GFP_KERNEL | (nowarn ? __GFP_NOWARN : 0); - - /* - * Don't do huge page allocations for modules yet until more testing - * is done. STRICT_MODULE_RWX may require extra work to support this - * too. - */ - return __vmalloc_node_range(size, 1, start, end, gfp, prot, - VM_FLUSH_RESET_PERMS, - NUMA_NO_NODE, __builtin_return_address(0)); -} + struct execmem_range *range = &execmem_params.ranges[EXECMEM_DEFAULT]; -void *module_alloc(unsigned long size) -{ #ifdef MODULES_VADDR unsigned long limit = (unsigned long)_etext - SZ_32M; - void *ptr = NULL; - - BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR); /* First try within 32M limit from _etext to avoid branch trampolines */ - if (MODULES_VADDR < PAGE_OFFSET && MODULES_END > limit) - ptr = __module_alloc(size, limit, MODULES_END, true); - - if (!ptr) - ptr = __module_alloc(size, MODULES_VADDR, MODULES_END, false); - - return ptr; + if (MODULES_VADDR < PAGE_OFFSET && MODULES_END > limit) { + range->start = limit; + range->end = MODULES_END; + range->fallback_start = MODULES_VADDR; + range->fallback_end = MODULES_END; + } else { + range->start = MODULES_VADDR; + range->end = MODULES_END; + } #else - return __module_alloc(size, VMALLOC_START, VMALLOC_END, false); + range->start = VMALLOC_START; + range->end = VMALLOC_END; #endif + + range->pgprot = prot; + + return &execmem_params; } diff --git a/arch/s390/kernel/module.c b/arch/s390/kernel/module.c index db5561d0c233..538d5f24af66 100644 --- a/arch/s390/kernel/module.c +++ b/arch/s390/kernel/module.c @@ -37,41 +37,29 @@ #define PLT_ENTRY_SIZE 22 -static unsigned long get_module_load_offset(void) +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .flags = EXECMEM_KASAN_SHADOW, + .alignment = MODULE_ALIGN, + .pgprot = PAGE_KERNEL, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) { - static DEFINE_MUTEX(module_kaslr_mutex); - static unsigned long module_load_offset; - - if (!kaslr_enabled()) - return 0; - /* - * Calculate the module_load_offset the first time this code - * is called. Once calculated it stays the same until reboot. - */ - mutex_lock(&module_kaslr_mutex); - if (!module_load_offset) + unsigned long module_load_offset = 0; + unsigned long start; + + if (kaslr_enabled()) module_load_offset = get_random_u32_inclusive(1, 1024) * PAGE_SIZE; - mutex_unlock(&module_kaslr_mutex); - return module_load_offset; -} -void *module_alloc(unsigned long size) -{ - gfp_t gfp_mask = GFP_KERNEL; - void *p; - - if (PAGE_ALIGN(size) > MODULES_LEN) - return NULL; - p = __vmalloc_node_range(size, MODULE_ALIGN, - MODULES_VADDR + get_module_load_offset(), - MODULES_END, gfp_mask, PAGE_KERNEL, - VM_FLUSH_RESET_PERMS | VM_DEFER_KMEMLEAK, - NUMA_NO_NODE, __builtin_return_address(0)); - if (p && (kasan_alloc_module_shadow(p, size, gfp_mask) < 0)) { - vfree(p); - return NULL; - } - return p; + start = MODULES_VADDR + module_load_offset; + execmem_params.ranges[EXECMEM_DEFAULT].start = start; + execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; + + return &execmem_params; } #ifdef CONFIG_FUNCTION_TRACER diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c index 5f71a0cf4399..9d37375e2f05 100644 --- a/arch/x86/kernel/module.c +++ b/arch/x86/kernel/module.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -36,55 +37,30 @@ do { \ } while (0) #endif -#ifdef CONFIG_RANDOMIZE_BASE -static unsigned long module_load_offset; +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .flags = EXECMEM_KASAN_SHADOW, + .alignment = MODULE_ALIGN, + }, + }, +}; -/* Mutex protects the module_load_offset. */ -static DEFINE_MUTEX(module_kaslr_mutex); - -static unsigned long int get_module_load_offset(void) -{ - if (kaslr_enabled()) { - mutex_lock(&module_kaslr_mutex); - /* - * Calculate the module_load_offset the first time this - * code is called. Once calculated it stays the same until - * reboot. - */ - if (module_load_offset == 0) - module_load_offset = - get_random_u32_inclusive(1, 1024) * PAGE_SIZE; - mutex_unlock(&module_kaslr_mutex); - } - return module_load_offset; -} -#else -static unsigned long int get_module_load_offset(void) -{ - return 0; -} -#endif - -void *module_alloc(unsigned long size) +struct execmem_params __init *execmem_arch_params(void) { - gfp_t gfp_mask = GFP_KERNEL; - void *p; - - if (PAGE_ALIGN(size) > MODULES_LEN) - return NULL; + unsigned long module_load_offset = 0; + unsigned long start; - p = __vmalloc_node_range(size, MODULE_ALIGN, - MODULES_VADDR + get_module_load_offset(), - MODULES_END, gfp_mask, PAGE_KERNEL, - VM_FLUSH_RESET_PERMS | VM_DEFER_KMEMLEAK, - NUMA_NO_NODE, __builtin_return_address(0)); + if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled()) + module_load_offset = + get_random_u32_inclusive(1, 1024) * PAGE_SIZE; - if (p && (kasan_alloc_module_shadow(p, size, gfp_mask) < 0)) { - vfree(p); - return NULL; - } + start = MODULES_VADDR + module_load_offset; + execmem_params.ranges[EXECMEM_DEFAULT].start = start; + execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; + execmem_params.ranges[EXECMEM_DEFAULT].pgprot = PAGE_KERNEL; - return p; + return &execmem_params; } #ifdef CONFIG_X86_32 diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 44e213625053..806ad1a0088d 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -32,19 +32,33 @@ enum execmem_type { EXECMEM_TYPE_MAX, }; +/** + * enum execmem_module_flags - options for executable memory allocations + * @EXECMEM_KASAN_SHADOW: allocate kasan shadow + */ +enum execmem_range_flags { + EXECMEM_KASAN_SHADOW = (1 << 0), +}; + /** * struct execmem_range - definition of a memory range suitable for code and * related data allocations * @start: address space start * @end: address space end (inclusive) + * @fallback_start: start of the range for fallback allocations + * @fallback_end: end of the range for fallback allocations (inclusive) * @pgprot: permissions for memory in this address space * @alignment: alignment required for text allocations + * @flags: options for memory allocations for this range */ struct execmem_range { unsigned long start; unsigned long end; + unsigned long fallback_start; + unsigned long fallback_end; pgprot_t pgprot; unsigned int alignment; + enum execmem_range_flags flags; }; /** diff --git a/mm/execmem.c b/mm/execmem.c index f25a5e064886..a8c2f44d0133 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -11,12 +11,46 @@ static void *execmem_alloc(size_t size, struct execmem_range *range) { unsigned long start = range->start; unsigned long end = range->end; + unsigned long fallback_start = range->fallback_start; + unsigned long fallback_end = range->fallback_end; unsigned int align = range->alignment; pgprot_t pgprot = range->pgprot; + bool kasan = range->flags & EXECMEM_KASAN_SHADOW; + unsigned long vm_flags = VM_FLUSH_RESET_PERMS; + bool fallback = !!fallback_start; + gfp_t gfp_flags = GFP_KERNEL; + void *p; - return __vmalloc_node_range(size, align, start, end, - GFP_KERNEL, pgprot, VM_FLUSH_RESET_PERMS, - NUMA_NO_NODE, __builtin_return_address(0)); + if (PAGE_ALIGN(size) > (end - start)) + return NULL; + + if (kasan) + vm_flags |= VM_DEFER_KMEMLEAK; + + if (fallback) + gfp_flags |= __GFP_NOWARN; + + p = __vmalloc_node_range(size, align, start, end, gfp_flags, + pgprot, vm_flags, NUMA_NO_NODE, + __builtin_return_address(0)); + + if (!p && fallback) { + start = fallback_start; + end = fallback_end; + gfp_flags = GFP_KERNEL; + + p = __vmalloc_node_range(size, align, start, end, gfp_flags, + pgprot, vm_flags, NUMA_NO_NODE, + __builtin_return_address(0)); + } + + if (p && kasan && + (kasan_alloc_module_shadow(p, size, GFP_KERNEL) < 0)) { + vfree(p); + return NULL; + } + + return kasan_reset_tag(p); } void *execmem_text_alloc(enum execmem_type type, size_t size) @@ -66,6 +100,9 @@ static void execmem_init_missing(struct execmem_params *p) r->alignment = default_range->alignment; r->start = default_range->start; r->end = default_range->end; + r->flags = default_range->flags; + r->fallback_start = default_range->fallback_start; + r->fallback_end = default_range->fallback_end; } } } From patchwork Mon Sep 18 07:29:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388974 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B1D5CCD37B0 for ; Mon, 18 Sep 2023 07:31:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 431AF6B0281; Mon, 18 Sep 2023 03:31:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BADB6B0283; Mon, 18 Sep 2023 03:31:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 234F76B0284; Mon, 18 Sep 2023 03:31:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 0E8716B0281 for ; Mon, 18 Sep 2023 03:31:07 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D5A101CA7F0 for ; Mon, 18 Sep 2023 07:31:06 +0000 (UTC) X-FDA: 81248896932.06.F566075 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf30.hostedemail.com (Postfix) with ESMTP id 2824B8002A for ; Mon, 18 Sep 2023 07:31:04 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ojfgNgsu; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf30.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022265; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=D67hK1WK/glEPsWwRha4zs/WTd3TgpZNhVD7Yn+lGNM=; b=u76w6QBEGmwX0lK2IG0P1p0o0uSGDagFmNEsI0QKbWcl4+MPiTV4M+pggbU0mUk+dw39xC XgnOSndRcLfmq9wIoWKnzxQiuhKJNNqBBvErEDlmB+cCSml9v8ajy39mmH7Gv/yi3iMTNv lS8OnLcqnTyTq4FPSvROgdVeIfdMS1g= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ojfgNgsu; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf30.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022265; a=rsa-sha256; cv=none; b=MMahGRyqBqE+LBGbjR6RMFPhG4DHLdWPERxoKhNxkkso1sGdZxEpVBSMPRRx8K8qgwO8cU 9z58dKbsXJOk0InwyisS7aS22ZLWpeBGzBNvfWLbDQHXv3jRPW3JGyNzWRS/aJoW/CzjS8 kaCXh57JTGpYzHWHkSMOl8XOe1etxxA= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 44E2A60E15; Mon, 18 Sep 2023 07:31:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 68ED7C433C8; Mon, 18 Sep 2023 07:30:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022263; bh=mbiiTE+Ax/Si/BZxcJVEdVXi15nThNmQlUqiyILb0zo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ojfgNgsup07S+iaZErkHdPn6pFXvUxpfd0rqcJ0g/fFjdgDTdp5RCwZChtRwqs+hU 358zdLhiRZXyw3aIFuKwPq3ciyzug59B+euqQg31hDJF2IYAsE+6D264chrRpy0JCv lIJDaEg0ezBhP+IOLDXi95uXUolbh37QjiMgyCp6vToq0/niAQX3TSUJ4+A3chXFuR Fz6pQnaNxbhFzVVMJL+/JI6h0Va38Wr9Y8Zzq0rWzkuGOl0+j0c7QpIR3g6g0WJ4vh Na6tomyrzTQLlYxetqXYeBzvBjSJ3J7TMO8PQeFVdg4iyOutejtYTgrDjMGzC7l/J1 0uZomhqve3rWw== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 05/13] modules, execmem: drop module_alloc Date: Mon, 18 Sep 2023 10:29:47 +0300 Message-Id: <20230918072955.2507221-6-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 2824B8002A X-Stat-Signature: 8cdza1m3jrm3xb3qoi6e3fjha849zauf X-Rspam-User: X-HE-Tag: 1695022264-667032 X-HE-Meta: U2FsdGVkX1+vxaAmgjAUc/ake5ryVirAUWdXf2s7Nlhan6Vv8veH71MSCxaYJorKWyoPnFacxogiZGGyYF9LsC52FJ+yTRmnGfRyvKmVm16bzVk9UxkvJkDeqNH4Y5CyQ11usdtnp9ZLBV+CiSyZNrBeUhaS/maqmP8HA9IBMZPsz6py0K9Vx2XNSqDyDG3etotxqEUacQKpvPmo5aioHbl16hhdLBTu8JsLG1RydGgQSEOb6i6zMr/pFAPKEUh5Omnl21h8jCXMgfMCIxmnWI3gVRpQaN87Sd1xRY3KuRhRf62hiX8ZvG+Mxo66fA2hsmn2BkWDpcaFx2Ym+ECUfIblx+Z14t0g2OuRGCIMU1NkWPgqjb2vaNGB0Ja8hrrdPHrKk9iHhyg0PT0LnFBs0S0lIBNH6Xbyixh0c7vEFIFmB1XZx1rSUtYrtbT1f6IEQcmGafEy/bpwpNoyxCkgbj5d1ka+n64IBBHVU1lM1eXzDZvyc9akzS+OKEpd6R1Z1LS9cE/tFYXvb7gHNoEYGerRbXeTGDtNDeFTnKAXmL3rp5gpnbTPu8vgTp/c+aQts7kAtE4YcZD4O9chCASv3Fp27XfbzUNTxV2VuGpwC2JKM/66J43SY1+49rgJHq96pwVu2489JqBMJ/OF6wUHq+Js4025x3nsC5KIrDTQo8ctKIKW1JIh2FN+SirRH1vQuNtvDw3hba0Qcr6jLaoNmTYbyjNKlNcNK0HAI9feVhPx8B430tIM73VzmqDuDuDowrUtZBjC0XsLbdDnjWwpIEqysPDtyx4uvC0Kl9dpiyGLO74xMudm+O8wvhoGJXeSLwku4+DUTLsiZGI9jiLrO9GrIgGz3AQnhXykJzYF43q/+eUszj0lF+MeuE6Re2rfn9Rd6qK/r+9W5+acWBjsUt9P/mvvT1HQnNWUENwbUOegpRLb4BRYbGg3U/BTvYcbRGDA4gHzWsOHoF52gfe eRtmZMrm jYVC/OtP/4Li5cMfNq40ki8xUYzy/gdCc17lNefO0jjTyAIIq8XeUuKQ+OqzzExtpVsEMA8sOpkU2jupOYzNMEAi3kw7mbvIdaHr0+x+scYh3uWe1MurAHf5QNy12H03Tv+1RgMT/GPdqjng1CnQktE0Hh3+AeNmJoT0oPtsFnYe5c4hiH3UTGZEMSbGpN3Fntf5lg272ZAZAmtQJrvO0xshgNCCeFexoao8pTKjZ/l65abLzmnP0kPLSeAc7x3Gld1YLxCapjhQwMhNA7OtOqZSaVXWtsZ2Ys/Spc6s+saTevLh2M9Y7YNWtsM0JPWarKa+du0szRAL47EHQ0Ngb3nXIf/h8PmmA7EAf0ak7FMKDwJBhZplzF9Vqyb59Eb8soamwkIXPagG4RAy6/t631jbJXv7xRZ/Lk72BJelPaKJIgJNsrImpL3U8xPLQRmY0HS6QCy6DN1C30CA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" Define default parameters for address range for code allocations using the current values in module_alloc() and make execmem_text_alloc() use these defaults when an architecture does not supply its specific parameters. With this, execmem_text_alloc() implements memory allocation in a way compatible with module_alloc() and can be used as a replacement for module_alloc(). Signed-off-by: Mike Rapoport (IBM) --- include/linux/execmem.h | 8 ++++++++ include/linux/moduleloader.h | 12 ------------ kernel/module/main.c | 7 ------- mm/execmem.c | 12 ++++++++---- 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 806ad1a0088d..519bdfdca595 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -4,6 +4,14 @@ #include +#if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \ + !defined(CONFIG_KASAN_VMALLOC) +#include +#define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT) +#else +#define MODULE_ALIGN PAGE_SIZE +#endif + /** * enum execmem_type - types of executable memory ranges * diff --git a/include/linux/moduleloader.h b/include/linux/moduleloader.h index a23718aa2f4d..8c81f389117d 100644 --- a/include/linux/moduleloader.h +++ b/include/linux/moduleloader.h @@ -25,10 +25,6 @@ int module_frob_arch_sections(Elf_Ehdr *hdr, /* Additional bytes needed by arch in front of individual sections */ unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section); -/* Allocator used for allocating struct module, core sections and init - sections. Returns NULL on failure. */ -void *module_alloc(unsigned long size); - /* Determines if the section name is an init section (that is only used during * module loading). */ @@ -118,12 +114,4 @@ void module_arch_cleanup(struct module *mod); /* Any cleanup before freeing mod->module_init */ void module_arch_freeing_init(struct module *mod); -#if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \ - !defined(CONFIG_KASAN_VMALLOC) -#include -#define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT) -#else -#define MODULE_ALIGN PAGE_SIZE -#endif - #endif diff --git a/kernel/module/main.c b/kernel/module/main.c index 4ec982cc943c..c4146bfcd0a7 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1601,13 +1601,6 @@ static void free_modinfo(struct module *mod) } } -void * __weak module_alloc(unsigned long size) -{ - return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, - GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS, - NUMA_NO_NODE, __builtin_return_address(0)); -} - bool __weak module_init_section(const char *name) { return strstarts(name, ".init"); diff --git a/mm/execmem.c b/mm/execmem.c index a8c2f44d0133..abcbd07e05ac 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -55,9 +55,6 @@ static void *execmem_alloc(size_t size, struct execmem_range *range) void *execmem_text_alloc(enum execmem_type type, size_t size) { - if (!execmem_params.ranges[type].start) - return module_alloc(size); - return execmem_alloc(size, &execmem_params.ranges[type]); } @@ -111,8 +108,15 @@ void __init execmem_init(void) { struct execmem_params *p = execmem_arch_params(); - if (!p) + if (!p) { + p = &execmem_params; + p->ranges[EXECMEM_DEFAULT].start = VMALLOC_START; + p->ranges[EXECMEM_DEFAULT].end = VMALLOC_END; + p->ranges[EXECMEM_DEFAULT].pgprot = PAGE_KERNEL_EXEC; + p->ranges[EXECMEM_DEFAULT].alignment = 1; + return; + } if (!execmem_validate_params(p)) return; From patchwork Mon Sep 18 07:29:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388975 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C9FDCD37B0 for ; Mon, 18 Sep 2023 07:31:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC0706B0283; Mon, 18 Sep 2023 03:31:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A4B4F6B0285; Mon, 18 Sep 2023 03:31:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C3CF6B0286; Mon, 18 Sep 2023 03:31:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 7BFB16B0283 for ; Mon, 18 Sep 2023 03:31:20 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 56B1C40B8F for ; Mon, 18 Sep 2023 07:31:20 +0000 (UTC) X-FDA: 81248897520.14.33F9F08 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf12.hostedemail.com (Postfix) with ESMTP id E522140005 for ; Mon, 18 Sep 2023 07:31:17 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=L26X5Mym; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022278; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=rYcSVBLgotk26OnoL1AjLxqTJ0/L7cxqyELacD3vUwM=; b=Vn+HN4X6mIw5ol948ntAMl6BBbGt6GAh79SnEmx6A8Q+eH5PyHcIF6T5V44rwfUsKjXNTN I2f6bH4KS9YF+TA6gwDacjxm47uam6rrmwqWpqXIkgnePMdx5uIz/4bYxkp0Wk/7c5ljRK muJFPcEGonjXjRfbJDSxryeRGx8yLRE= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=L26X5Mym; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022278; a=rsa-sha256; cv=none; b=b1QIqHt/9aXq8cKN9VzX7JLPaxwhqNSgXlsyBd6cFoZn7qiAbzzlvhSo+/+ijEN7zKcQ7B PSPg8IY8WkuaEswbYvnCXRH4FtwQiT80IyVlFnuyO9Bl7C530LDSloX4eN78kO4VIkqIGL plviFiCtVM8GYkEOEXhAaY65vqm+cCw= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 03A47CE0C47; Mon, 18 Sep 2023 07:31:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2285FC433AD; Mon, 18 Sep 2023 07:31:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022273; bh=GrzrJESS7vx7QyV0O3w+oX12p1W5MbqUcsXcqyR+rMA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L26X5Mym5jbDCDCi9pBttJiCUmGDJK7QCrrwON0wsqLsaMRtQtziMfk+8vmq102wK apB9eMngz4FDoZOnyfOnpxsYAYV+s6z6uy/A7GUFANPsDrtCidzVD8sw9PZ9EeZeSS BBZbnyT3UkLAeKFTSJ+WQXJ44CEr3Yz7/npPDOeCiIyuKPQdJ6wFULwDSI42C+21i2 TtK4M26N760ml2Bd8ec+c+dtMjxHoi8aQYpVD0iQ48GL/MfHLj1xfBm+sXCRSzVgn2 IlOo26svfqfJVEKxvnTHvne8XyU+XB4i1UAyBnP2cTSXuXl9ONzyCPVFbHoWx7LKOg fqTz/6KsDW2hQ== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 06/13] mm/execmem: introduce execmem_data_alloc() Date: Mon, 18 Sep 2023 10:29:48 +0300 Message-Id: <20230918072955.2507221-7-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: E522140005 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: z3qonidtr18xtjfui4oz9raja341997z X-HE-Tag: 1695022277-654783 X-HE-Meta: U2FsdGVkX1/ytRLOtgRcFnyAgLLxP+a8y5803TPYX2AEt4b1YvLeE2bO1/bq8Q1AEdQ6UOkjO9jttqe7lvn2KArRjeD6M3Xe4pZmMGafefCLG+iTJA4BL3RFn0vFFZIWiDcxs2USXaHkh+KjbvrWBIVfYVJqTgcGC7eEZkfNOVfktq/weUV87XLXhyr4U5Eg6N6wWbo8oubz9zzqptlrYyj3wYoVQwaeKzIUwBv+0h9OrDSA8gJXSQR5OD5q5LQ4gj8P+kCIqMuwCL7kklr6mJiDgqEyu627UoAt1324GTkrYaToIjg0dUAbsdMLTgHBywfnxRzBUEePyWCk1sRqfN7vKIq9tOEdiL8BSrbRyku/ajANW8hdMJgnjK2gQVY04NjCLKKlp5l1/UWojt/NXGfXPkD+r4jNoNAcg4OTTYXzFfDeT1c+Rp+CL191TN+TPOUZZlWa+1KOqAHNii1EsSbuhryFP/6aJHbilw4KQKMjwKNad3Oz04q6j7bKLcTqkIHthnN/HNFpytgKdsexWIGwyHZgXvNnI3TUM2Kat+bi4NIOQEzuqKa7gkYsz6l860xgFCJvLKNJKCRU+kw+UI8nbN//d5yvJ1SsmuT91O2hozEPmdqnT3Qr+t7KOavmVyN5d619lY7tQdtKvQU4HgHzsPEIN/GvccuGmCwp3N5B9CK8joKgqV2pZ7TC+xLKiDSZTQ7qFmLjTkUWAlO4ih8Hax95bii4YXe8z9YQp+3gFIw+sujGaT6W8LcKLp69VO6NXN8bLwn5BepGVcxXPcYuYBBEeZoBdVeyHdAsyYLVvjE2yFxUMblC5M/++xel1EY/X6Tw/4BVDvn25JctFRoPVw6qlc508qU0fy/I9J0G6vvDYtq996V+7uFotaOGCuOQF3tJhg933ejKX86l8ys9+gGCMmeRvvrQxlP9/Y+Uw3zW7yN1c+53U00l6IHxSQBGDjd27wwWc8fQA2p o/cDU0rS E5YCGkE2q3lPwa9xgkVTIpryzoFufxWIjfRobdc/NTKtDJVd3N7P9rnGNwTNR5xUYJFsSJy2vmzjmeVf7PhSQrMAKspwCE/e+fEQE1zAdGhLiVRH93NZgOL09HYDRMzTe4RBA7L1curBYrJu1C5xsuiHPL2pCb2LbtqtWcjkxAXTOO6fHO9NEUCOCPrbRu8TJuowxiLkxmoKuIPpBkb7UbyP/GC7xKXNO6eWZkYYbGO7rNkb827wHw35Q/7MkLKqvea2xPesQA+H30wEDYZbXwfBWggIU9UajXNn/y85ysPbt+2EpEuxl7loaToRFJOtskvM5rQH2VHiPWspC/ZuEfrcysgnj+nXu7sYeg+gL9Yu7aISqGLnI8XHca1agq+tgSnfouRJgPN5HEC8MRZ5gzURuShcDPkf43Kvh6aKdunMu/pGNI5oJLpcRjxBsGJLaPEa0JL7203aPO68= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" Data related to code allocations, such as module data section, need to comply with architecture constraints for its placement and its allocation right now was done using execmem_text_alloc(). Create a dedicated API for allocating data related to code allocations and allow architectures to define address ranges for data allocations. Since currently this is only relevant for powerpc variants that use the VMALLOC address space for module data allocations, automatically reuse address ranges defined for text unless address range for data is explicitly defined by an architecture. With separation of code and data allocations, data sections of the modules are now mapped as PAGE_KERNEL rather than PAGE_KERNEL_EXEC which was a default on many architectures. Signed-off-by: Mike Rapoport (IBM) --- arch/powerpc/kernel/module.c | 12 ++++++++++++ include/linux/execmem.h | 19 +++++++++++++++++++ kernel/module/main.c | 15 +++------------ mm/execmem.c | 17 ++++++++++++++++- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/arch/powerpc/kernel/module.c b/arch/powerpc/kernel/module.c index f4dd26f693a3..824d9541a310 100644 --- a/arch/powerpc/kernel/module.c +++ b/arch/powerpc/kernel/module.c @@ -95,6 +95,9 @@ static struct execmem_params execmem_params __ro_after_init = { [EXECMEM_DEFAULT] = { .alignment = 1, }, + [EXECMEM_MODULE_DATA] = { + .alignment = 1, + }, }, }; @@ -103,7 +106,12 @@ struct execmem_params __init *execmem_arch_params(void) pgprot_t prot = strict_module_rwx_enabled() ? PAGE_KERNEL : PAGE_KERNEL_EXEC; struct execmem_range *range = &execmem_params.ranges[EXECMEM_DEFAULT]; + /* + * BOOK3S_32 and 8xx define MODULES_VADDR for text allocations and + * allow allocating data in the entire vmalloc space + */ #ifdef MODULES_VADDR + struct execmem_range *data = &execmem_params.ranges[EXECMEM_MODULE_DATA]; unsigned long limit = (unsigned long)_etext - SZ_32M; /* First try within 32M limit from _etext to avoid branch trampolines */ @@ -116,6 +124,10 @@ struct execmem_params __init *execmem_arch_params(void) range->start = MODULES_VADDR; range->end = MODULES_END; } + data->start = VMALLOC_START; + data->end = VMALLOC_END; + data->pgprot = PAGE_KERNEL; + data->alignment = 1; #else range->start = VMALLOC_START; range->end = VMALLOC_END; diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 519bdfdca595..09d45ac786e9 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -29,6 +29,7 @@ * @EXECMEM_KPROBES: parameters for kprobes * @EXECMEM_FTRACE: parameters for ftrace * @EXECMEM_BPF: parameters for BPF + * @EXECMEM_MODULE_DATA: parameters for module data sections * @EXECMEM_TYPE_MAX: */ enum execmem_type { @@ -37,6 +38,7 @@ enum execmem_type { EXECMEM_KPROBES, EXECMEM_FTRACE, EXECMEM_BPF, + EXECMEM_MODULE_DATA, EXECMEM_TYPE_MAX, }; @@ -107,6 +109,23 @@ struct execmem_params *execmem_arch_params(void); */ void *execmem_text_alloc(enum execmem_type type, size_t size); +/** + * execmem_data_alloc - allocate memory for data coupled to code + * @type: type of the allocation + * @size: how many bytes of memory are required + * + * Allocates memory that will contain data coupled with executable code, + * like data sections in kernel modules. + * + * The memory will have protections defined by architecture. + * + * The allocated memory will reside in an area that does not impose + * restrictions on the addressing modes. + * + * Return: a pointer to the allocated memory or %NULL + */ +void *execmem_data_alloc(enum execmem_type type, size_t size); + /** * execmem_free - free executable memory * @ptr: pointer to the memory that should be freed diff --git a/kernel/module/main.c b/kernel/module/main.c index c4146bfcd0a7..2ae83a6abf66 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1188,25 +1188,16 @@ void __weak module_arch_freeing_init(struct module *mod) { } -static bool mod_mem_use_vmalloc(enum mod_mem_type type) -{ - return IS_ENABLED(CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC) && - mod_mem_type_is_core_data(type); -} - static void *module_memory_alloc(unsigned int size, enum mod_mem_type type) { - if (mod_mem_use_vmalloc(type)) - return vzalloc(size); + if (mod_mem_type_is_data(type)) + return execmem_data_alloc(EXECMEM_MODULE_DATA, size); return execmem_text_alloc(EXECMEM_MODULE_TEXT, size); } static void module_memory_free(void *ptr, enum mod_mem_type type) { - if (mod_mem_use_vmalloc(type)) - vfree(ptr); - else - execmem_free(ptr); + execmem_free(ptr); } static void free_mod_mem(struct module *mod) diff --git a/mm/execmem.c b/mm/execmem.c index abcbd07e05ac..aeff85261360 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -53,11 +53,23 @@ static void *execmem_alloc(size_t size, struct execmem_range *range) return kasan_reset_tag(p); } +static inline bool execmem_range_is_data(enum execmem_type type) +{ + return type == EXECMEM_MODULE_DATA; +} + void *execmem_text_alloc(enum execmem_type type, size_t size) { return execmem_alloc(size, &execmem_params.ranges[type]); } +void *execmem_data_alloc(enum execmem_type type, size_t size) +{ + WARN_ON_ONCE(!execmem_range_is_data(type)); + + return execmem_alloc(size, &execmem_params.ranges[type]); +} + void execmem_free(void *ptr) { /* @@ -93,7 +105,10 @@ static void execmem_init_missing(struct execmem_params *p) struct execmem_range *r = &p->ranges[i]; if (!r->start) { - r->pgprot = default_range->pgprot; + if (execmem_range_is_data(i)) + r->pgprot = PAGE_KERNEL; + else + r->pgprot = default_range->pgprot; r->alignment = default_range->alignment; r->start = default_range->start; r->end = default_range->end; From patchwork Mon Sep 18 07:29:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388976 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 563DAC46CA1 for ; Mon, 18 Sep 2023 07:31:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E42B16B0285; Mon, 18 Sep 2023 03:31:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DF3476B0287; Mon, 18 Sep 2023 03:31:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C6EA96B0288; Mon, 18 Sep 2023 03:31:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B44FD6B0285 for ; Mon, 18 Sep 2023 03:31:29 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8F29740B7B for ; Mon, 18 Sep 2023 07:31:29 +0000 (UTC) X-FDA: 81248897898.14.974F6A6 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf26.hostedemail.com (Postfix) with ESMTP id 51C7B14001A for ; Mon, 18 Sep 2023 07:31:26 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QwbKIwCm; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022288; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Qo37PvQ23r50OzMpHp40Xs5RSpcqAH908YBD31i86Ss=; b=XNJVkdr/UGHg+w0jGJPjb28EpmnhZAMmN7worHQPyF8a6ZFzLzCN9Hbz0QCZeAkc3d8+qa hk7mdHl42Gp2YLEFUw6zrKfJeq/PT7LR4/95a8WtGE6bqnKN9w1ogPD4cFOv28CDL3pqFc Z1/OMuVCxCv3hLvVaD2Oe9dtB6wwl0g= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QwbKIwCm; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022288; a=rsa-sha256; cv=none; b=KcIGlxhMd6QiHVW6t0/cZC9z8oF+kCYdod3OIaGAp4a2H83D/TIA3DG8TfgaEX1uO3KaOp ZziGAzhJ3o+6ZhP4+387+Q5ndJCPxRYCWcF62ETfrWFsFQj7qORZl9R/StQTyMBPEpzMW2 DQ41r4nA6tIzM30O2eHj2ZlA4y4joek= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id B566CCE0BDB; Mon, 18 Sep 2023 07:31:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D3F8CC433BF; Mon, 18 Sep 2023 07:31:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022283; bh=uhUpg7LkgDj87OPKWv+2ZQQTqeXwsgqGQfVhMMOlf6I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QwbKIwCmsi/m+QIPzGWyDvn+Tk81CaEzaNlQaj1OrbfY6aH7VjNTOMfyqPjfbO3ZI IrekkL8ga776lr+J3x8Ygyh2wVMU8QELU6aweeJeBpGEbBfrtJxHf+rNjrvDsHVs7+ 6bm1TS2tPNGOkx/h7R28N2r8hennFfDFz6aE6FuiYGkO4GZPqz8JGrUJkS1iup7wLd Isg0JWVRjOAmJTjw/rUuKi838+cqznX9dO82PejgtCEzU2RQuKCDDPVyOOKasyX6kb 8fpNnivohYt0YSrYt8tzLAAtD+1zq8pRlzkl8JhZ7kRPGcY2/LRuqibiB20CPobXX4 xLjJAnEDRG4hQ== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 07/13] arm64, execmem: extend execmem_params for generated code allocations Date: Mon, 18 Sep 2023 10:29:49 +0300 Message-Id: <20230918072955.2507221-8-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 51C7B14001A X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: sb97pp5oywy4nh5tczdkknbxtj6xswy7 X-HE-Tag: 1695022286-902116 X-HE-Meta: U2FsdGVkX19G8pOuZf8irXOI2T/2UANN8zfVGz3f8154qGUjNu9ZY/BdFuwUZ3i0BBKFhAzVo+jnmzFkbF14RC/Pqjkbwv3phNte7rzkZ/YTSLlFvDJIWdTa+h73kTntfxaV5BbKiKvCoInA6rk3EnspvnttqCPcsPDYNR2TBr/r1NTCHMCLMb3JtD3XEBG2LoIJIWIPlEAS63r4QbSAG6dqJxHICRcvkkvkDImOpQWfWpmgqnBHr7mLjNgCz6SWlBHMSVesq+u/jgu2EnNW0XHIJO951fVixBCXjwr4MsK68ebE0jdwMfUDYWr81p2vIh/Wz3kR7jAZu7MtSIEOnYaffSuJ2d6zExQQ67nGVgbWfDh1EIj9kNYxR40Bs7XAPVRk/4SDtgtrQTliiwnVpDkfl1V/A6NZ9K7wX+sF5a8b2a8HULKgV+x2VdTzlXeL766pUdcmMhwemjWc07EPWxd8qyxl05sGFnW63yvnTm8TH8zO4JZC7TSef3lW5tWhzaY2lCJRk0Go/0/O85fT1CPmSMUQ/u3zo1usDtbPDlAK7fLUDMLhOh0Sikj1Y2EtcuYs95PNpg6J5tZlz56RYbS6IChXR5Y28WRVI+r3n9fehvbxpzTu91XjAw745BGAZhD07FAsuBoYdkp7gAWmb8cDR/7rB9+SJw2UFPiXAACd6vg7JoZ+oU8qT8snO2MdTvgaOpNgsIg/tXv173E+FoK5I3/v11fh6sBXidiaQJcsIzuPL+M99XOuFZqoS5sYsqWpIRUzgfz/0tA3u+hKL5oC6y/HYpFsZHvtkJ4mcLsZbEa4mZTk8959LfpWIac/YPVmJTIG76j9xORsGJU8hiMkzmlJ0xhT55/WUvcz+8QI83jvCPXmPKhz1eRhV06X4IkGtc9S2jIk9/nv3rvG30gueeQFajjfwnTe2y02XIEapDaUGb/PQkpIPqnHS5oDFXgBz7AnxNNtZ/xBikb zg1JZgB3 V43uTaxUbktwRyGxxsMghio97we1ZMFrtycnHRmfOGZrba9TRJBAoE87lxBY2J6XoutynKlhdsPh+Iw2oHS6C9NpJL3X0sofmxKRSC8DzOJ7N0AGmgEDScmjHCOnbUWux76z3888r9aZb1ORrxTRFev819DLJ4fsRYPhGrcIwwzLT1X+Ee8BahjMsLhOGS78MHnGNNeDVMeFJLliV/YKLoQbrjpVwyeDWEMjy/Vb10ARS7hFvxrY4zHN2Jvnjp2oQ0IROQJxgFpNdrRWj01qp84nkT71CkpxSwPcEPT9KWTYd/Q2BnIFpdftwiEl/ZFdNun1ILeA7XomkCuUvjzBdAYYVSQxOfZL12i4TKEqBYFSR52AHkAtwcqy29reyxL2RFITIwYF/amamg/W2uc+NNyuOCi7318M+51OgYmL6PrQ6J0TXCXLaAaYwnW/oL+DGbFP1fYNMz73x7dc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" The memory allocations for kprobes and BPF on arm64 can be placed anywhere in vmalloc address space and currently this is implemented with overrides of alloc_insn_page() and bpf_jit_alloc_exec() in arm64. Define EXECMEM_KPROBES and EXECMEM_BPF ranges in arm64::execmem_params and drop overrides of alloc_insn_page() and bpf_jit_alloc_exec(). Signed-off-by: Mike Rapoport (IBM) Acked-by: Will Deacon --- arch/arm64/kernel/module.c | 13 +++++++++++++ arch/arm64/kernel/probes/kprobes.c | 7 ------- arch/arm64/net/bpf_jit_comp.c | 11 ----------- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c index cd6320de1c54..d27db168d2a2 100644 --- a/arch/arm64/kernel/module.c +++ b/arch/arm64/kernel/module.c @@ -116,6 +116,16 @@ static struct execmem_params execmem_params __ro_after_init = { .flags = EXECMEM_KASAN_SHADOW, .alignment = MODULE_ALIGN, }, + [EXECMEM_KPROBES] = { + .start = VMALLOC_START, + .end = VMALLOC_END, + .alignment = 1, + }, + [EXECMEM_BPF] = { + .start = VMALLOC_START, + .end = VMALLOC_END, + .alignment = 1, + }, }, }; @@ -140,6 +150,9 @@ struct execmem_params __init *execmem_arch_params(void) r->end = module_plt_base + SZ_2G; } + execmem_params.ranges[EXECMEM_KPROBES].pgprot = PAGE_KERNEL_ROX; + execmem_params.ranges[EXECMEM_BPF].pgprot = PAGE_KERNEL; + return &execmem_params; } diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c index 70b91a8c6bb3..6fccedd02b2a 100644 --- a/arch/arm64/kernel/probes/kprobes.c +++ b/arch/arm64/kernel/probes/kprobes.c @@ -129,13 +129,6 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p) return 0; } -void *alloc_insn_page(void) -{ - return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END, - GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS, - NUMA_NO_NODE, __builtin_return_address(0)); -} - /* arm kprobe: install breakpoint in text */ void __kprobes arch_arm_kprobe(struct kprobe *p) { diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index 150d1c6543f7..3a7590f828d1 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -1687,17 +1687,6 @@ u64 bpf_jit_alloc_exec_limit(void) return VMALLOC_END - VMALLOC_START; } -void *bpf_jit_alloc_exec(unsigned long size) -{ - /* Memory is intended to be executable, reset the pointer tag. */ - return kasan_reset_tag(vmalloc(size)); -} - -void bpf_jit_free_exec(void *addr) -{ - return vfree(addr); -} - /* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */ bool bpf_jit_supports_subprog_tailcalls(void) { From patchwork Mon Sep 18 07:29:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388977 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 791EECD37B0 for ; Mon, 18 Sep 2023 07:31:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0FCBE6B0287; Mon, 18 Sep 2023 03:31:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0AD816B0289; Mon, 18 Sep 2023 03:31:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E6AF46B028A; Mon, 18 Sep 2023 03:31:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D2EFA6B0287 for ; Mon, 18 Sep 2023 03:31:39 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id AB4D380BAB for ; Mon, 18 Sep 2023 07:31:39 +0000 (UTC) X-FDA: 81248898318.03.29A5161 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf10.hostedemail.com (Postfix) with ESMTP id 612D8C0002 for ; Mon, 18 Sep 2023 07:31:36 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sUl4tcVd; spf=pass (imf10.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022298; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=r7WH6k3cQ1gcDKVDqjiXKIrMyc70EktDzeRRqm44eao=; b=guK22mI3q5bVkvRQ3AgzwHdZlAm5g6GyBNiu66iyQJOEvJ9YsEftb+o5Cwh3LqPYMzpdDq lwLz7/xKuRtcq4GZSb2hdWYFm9p98ikadrQIfEeSTOXDxYzVqitbRvzCIBSXWZhJ63DW3m VQCWEQWjztckOTPAuvbaqoqOOofGcb8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022298; a=rsa-sha256; cv=none; b=sHb4Me9aVLRn3hH6787Qxu6RhbYfIp4+Z9bIB+JI8YSPjz5wi0y98TwrlZETxWzoaZNvnA qrEbOYgCef+BhkCI3IJwLUL7UpuodgWWNNCerERaHS7EwgM7etwi4ecQTWH+QzCXFnAf8P Cxtayl8iEiG+E9oonMWhpL8GCkw6bis= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sUl4tcVd; spf=pass (imf10.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id 771DBCE0CE2; Mon, 18 Sep 2023 07:31:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 90D66C4160E; Mon, 18 Sep 2023 07:31:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022292; bh=Bxy7NNRI6l4akC8Uk0JGSyZsyMn1J934EmvkFA96qyo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sUl4tcVd+CE1RIfDd+iHzBuo/TtqH+sjUX4AKoidjsBhiPE9N8u5sBFCrh0U2OnnW 02VNXIu/PasFZvJmqp4LQS/fRSyOWf9W0w4R1mThqbVlQHxfh9deE8Gen6PrybSKrA HQTfOkMmKWxzdY89y0l1tlT8/WpmYk4oOHqiKiyXMFacErx4oC2oV4nkojcJ6bpZun 9S9TLaBh1Kth+hdxlxcmXjxDVU8y/jq3PgPfSmOZxNeFoZYJdyBqlLfSCGOoTzYYQr s02+6pJi35T4N9EWcJQvWIbsw2teHNSr/sLV1vIIuBQWC8WuxxKTSKFPi24i20hJWy PMgDbwaP/8Fzw== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 08/13] riscv: extend execmem_params for generated code allocations Date: Mon, 18 Sep 2023 10:29:50 +0300 Message-Id: <20230918072955.2507221-9-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 612D8C0002 X-Rspam-User: X-Stat-Signature: 91uacuni16mnaxxbxebdg9bfwxmgmgud X-Rspamd-Server: rspam03 X-HE-Tag: 1695022296-782610 X-HE-Meta: U2FsdGVkX19cw3NReJRGrRno8Ss/HLHeKSjmP4c4RvMIo5DbeUPXOE0cTcLMGG5mMt+0tREZAipuYH+GMRibLZirDgF909EMIq3cNjGJCKgi75hYZUoJp/Rg4Ro/UiJ4rU/qXq8O85oW/IxRwbOk0I+1QmL+rEs5oxRld8bnBDrc3fOtAMcnGFpZdTT39w4joJxCRxFnRaEeM5LPeA5yh2EdwVzq67SxhctZ64QsC+rialVEDeS3Y09cFEmEOL4ocmrCu6QemR4ZrdflMtVFVUI6cmqgWTG46tLdeUuxH6dhMMsJGZMK6jBDKoD/ndbZ+TCskjYIT6Cjgdrxq7+LuzmfmpJnYVjw+jpMjqLpdQDePKazmQ4rqSbFs0etFlHFnrIup8UqkysLJ3XcBxTR04m2kP7LvDDxJz72cmalCL3OfJBZL1LHOwBSDM671IgHavPdOmaDWPDJpxRPnmt0cPg4MgW+qx0kMm63VtXywq7so9VGEp4VSXyxZaUKYA6YoJijkbUFLK2wUe4NomSmJIPgRuTX0FE9ozwUcALsWXSxLmoRT/xesHUQRtcyIR1q3Evntf9UoHwUGxvJBOCXJOxE0ehUeRKhSCzaWZoc4V+e3bWG+AzQr6tCK9JUzOnaBXfVMFX+Gd4TkIxIscxj5ZQC6vU+XR3+4UQshA6uc+nUFWFWDDFmOOYqliWqvBSTD91ENnIhuvKiS5jon9AlgC+3tGvEHHofGfFxSGAdjeEdQQjSMSCM6ilY0oE4IEj/nug1IjBNRFy+5oXyJyp75kt+VpMXYX+puDEN2XKDzULEjHs/Ptm5xSYzsJYLw2S9K91hC8sTOgXo1yjBqafWhtj/eVnUDVmTKQY1UYHQdWxKydwf933qvqkHCy8wc5uL/rU1zHA/SakmBHHdobFXh454p47ZmTwRmmtNt4jpavUXm6OybqtBqDeWs/wlclw8s7B1+ifyPo5D5pDbqdX QPtma5e/ t62//pW4E2H0Ctv9ViX9RWll5Hw1mOxe1e/ihw8vE8/Z7F2OYGhmoGWYbyPPpbGhAvI30oxIuVTolysUWPgyIixJKcNwTA23krhfw0UqCk6wfum8LYIFa2N9wdELtFW2rogsN0Du8iP0Q6Ho7K+FootEBeTKuNllssPhWRZkUeOORrCwSJjodRyEqu53laHAnTjoUPs0b5Y+l452DmmHB98DqjWaOqmGf/ObDPIQ8SRiV80Da0BhoK3Gki7/e6VDws4NgcaPo7PWYci+ylrwPxq+EPe7yrDvY0pBoehRiYf+fhmeAr8aUK/XkUDFJjt/cQ9lWVOgkDQpAOCukZm50GRD55KJNkOHen84w5eIXnB463uO+AXOYt2LhRst1BzUY4k2x57MviP2EM1h68bJi8aBppXQs6GO0SMSJTsssnIJ+06H/g4swN8WZv+MKr8Oe7FuhPdafTuM8j7c= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" The memory allocations for kprobes and BPF on RISC-V are not placed in the modules area and these custom allocations are implemented with overrides of alloc_insn_page() and bpf_jit_alloc_exec(). Slightly reorder execmem_params initialization to support both 32 and 64 bit variants, define EXECMEM_KPROBES and EXECMEM_BPF ranges in riscv::execmem_params and drop overrides of alloc_insn_page() and bpf_jit_alloc_exec(). Signed-off-by: Mike Rapoport (IBM) Reviewed-by: Alexandre Ghiti --- arch/riscv/kernel/module.c | 21 ++++++++++++++++++++- arch/riscv/kernel/probes/kprobes.c | 10 ---------- arch/riscv/net/bpf_jit_core.c | 13 ------------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 343a0edfb6dd..31505ecb5c72 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -436,20 +436,39 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, return 0; } -#if defined(CONFIG_MMU) && defined(CONFIG_64BIT) +#ifdef CONFIG_MMU static struct execmem_params execmem_params __ro_after_init = { .ranges = { [EXECMEM_DEFAULT] = { .pgprot = PAGE_KERNEL, .alignment = 1, }, + [EXECMEM_KPROBES] = { + .pgprot = PAGE_KERNEL_READ_EXEC, + .alignment = 1, + }, + [EXECMEM_BPF] = { + .pgprot = PAGE_KERNEL, + .alignment = 1, + }, }, }; struct execmem_params __init *execmem_arch_params(void) { +#ifdef CONFIG_64BIT execmem_params.ranges[EXECMEM_DEFAULT].start = MODULES_VADDR; execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; +#else + execmem_params.ranges[EXECMEM_DEFAULT].start = VMALLOC_START; + execmem_params.ranges[EXECMEM_DEFAULT].end = VMALLOC_END; +#endif + + execmem_params.ranges[EXECMEM_KPROBES].start = VMALLOC_START; + execmem_params.ranges[EXECMEM_KPROBES].end = VMALLOC_END; + + execmem_params.ranges[EXECMEM_BPF].start = BPF_JIT_REGION_START; + execmem_params.ranges[EXECMEM_BPF].end = BPF_JIT_REGION_END; return &execmem_params; } diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c index 2f08c14a933d..e64f2f3064eb 100644 --- a/arch/riscv/kernel/probes/kprobes.c +++ b/arch/riscv/kernel/probes/kprobes.c @@ -104,16 +104,6 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p) return 0; } -#ifdef CONFIG_MMU -void *alloc_insn_page(void) -{ - return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END, - GFP_KERNEL, PAGE_KERNEL_READ_EXEC, - VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, - __builtin_return_address(0)); -} -#endif - /* install breakpoint in text */ void __kprobes arch_arm_kprobe(struct kprobe *p) { diff --git a/arch/riscv/net/bpf_jit_core.c b/arch/riscv/net/bpf_jit_core.c index 7b70ccb7fec3..c8a758f0882b 100644 --- a/arch/riscv/net/bpf_jit_core.c +++ b/arch/riscv/net/bpf_jit_core.c @@ -218,19 +218,6 @@ u64 bpf_jit_alloc_exec_limit(void) return BPF_JIT_REGION_SIZE; } -void *bpf_jit_alloc_exec(unsigned long size) -{ - return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START, - BPF_JIT_REGION_END, GFP_KERNEL, - PAGE_KERNEL, 0, NUMA_NO_NODE, - __builtin_return_address(0)); -} - -void bpf_jit_free_exec(void *addr) -{ - return vfree(addr); -} - void *bpf_arch_text_copy(void *dst, void *src, size_t len) { int ret; From patchwork Mon Sep 18 07:29:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388978 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E03EC46CA1 for ; Mon, 18 Sep 2023 07:31:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B0466B0289; Mon, 18 Sep 2023 03:31:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 938E06B028B; Mon, 18 Sep 2023 03:31:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B4786B028C; Mon, 18 Sep 2023 03:31:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 675776B0289 for ; Mon, 18 Sep 2023 03:31:45 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4545980BAA for ; Mon, 18 Sep 2023 07:31:45 +0000 (UTC) X-FDA: 81248898570.18.BDA0EE7 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf15.hostedemail.com (Postfix) with ESMTP id 9D04FA0036 for ; Mon, 18 Sep 2023 07:31:43 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=d2NNM60P; spf=pass (imf15.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022303; a=rsa-sha256; cv=none; b=KIwQdG1umaWrfIIeCpAnjHUIB7qDdpoSYWVtMXZh60ynIdVrICfPusrD9ySNVit3ujlInN D4YWy51wk9+7Cgyp6XzwAUowmTZ3AxRF9Oihlcsy+tnIpi2tng4os8JkeOTHeZBYIkpqgC 3MFQBVeZ9e2LhYvNx/wm1d24IwK5ov8= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=d2NNM60P; spf=pass (imf15.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022303; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=EJSx7dTPSl55UPeMrpomdQNp6HYHj5K4Qaz1intYvHs=; b=UvDPegsHz7KTtAMYLartKAc6IRGTf1od33FrD0NMCFjj+3eCDfRhbNjjeMajMVloKel9ua /MZugPqlu4ZUyhqTl+nwyjcUnmRAtj1XpgrJuO666dI/3tRLY834pW/TJrdpot5nxj4ifZ ZDqfa/UMocOk9VVPKTEbLM8xYpEaX+g= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id DE75660F8F; Mon, 18 Sep 2023 07:31:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5155AC433B7; Mon, 18 Sep 2023 07:31:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022302; bh=35ZlmvV7rKqwzQaOmbBijH380/EeVtPirZbGWiToTW8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d2NNM60PB58jmuhS2dGN8NLbq1KB4ygYvRR0t6DZZJS7qBVZpROHao20O2uQV3bli 0QvXp0s5+eLFeJhc/w10Fn3KC9lksq3uhoYzEZC5CWp+6gjai+j/d0a89/bBUhyjJy FfXXBFOSn88hPGWgJbSxUtSr8ULAUympoyAWJKP5uJmt4lykdt9r5kJb2u9np9CG9l pAU2vChu/4/QGlbV5wO6V/EhzFjCpLIlsY1oLhcdgzQ+bitYphtILzsTup5ZLrX357 OmssgGZ5JmBAamSicGtU177ClYYdO2+YHkslBuFOroSFhEYDOcBWDp8NAr0VRv3fiX mkBw/0g11HpDQ== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 09/13] powerpc: extend execmem_params for kprobes allocations Date: Mon, 18 Sep 2023 10:29:51 +0300 Message-Id: <20230918072955.2507221-10-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 9D04FA0036 X-Stat-Signature: rc6te47437e8yhskfhfj3eq33t6d3bw6 X-Rspam-User: X-HE-Tag: 1695022303-290320 X-HE-Meta: U2FsdGVkX1/PCodlaFOW1iC+EeEjRnC3PTTCvy5d+VBsPCsxQyqyKOaf2TdhFeQ7qoOZJTtKI5HY7DKCxKw4jzizGO9QrEbt1ROt5kcw2O6stdWgoo4TTf71kGeyIFH6QvENf9eQ2aekv5nePzC+hHCjJwLCEloIcCQmOgeuVIxP1gl1JAxm9eTZOI7NhlQSSAK7NtuhjEutLI4T051c7q8y+e82Zic7xq2pf7GuRTsY+imU2kVlUxCGjtZpdMM196rLeiioNyAUIocxgubrblRyFjB1k7M7MV02tuSsTzSdb3AbQjrYBmxnwVHd5W2ulcgzLfoGUc9xRtl9ZYlX5aGH+2n+3h9n1MTPka1hynvxMYc77l0v3IEyV+De2SM/XG4cWruv1E85hlbylUJGjMA0RhDy2FUqaM1FycwDEROC+PwZHZ+lkUcoUEeKWcGttvURBZhI2hjYP93sSo5sNAxVP//j3mG5+N6nourTU89VqL3TufX2mVlNx4tRH9KVZjBH95NRSQ2a/RhE2FEcdxsJbBP/4scLEsxa3uOc1Dy1sQa5+4JTFnfwMEvdJBzJbXjbZ2GIXjiXIdYNlBv+exiZMq1Vuqbo0BDmUtsii59b1sIvMJrxntaLDylTtlLvACfan+jovso0O0vlQTkN5uB9EvbPsy+ULuZ/bVRJFfkdXY65WSgizRgNXVbnMJxC0RwvpQB8g7HxlLVsZ6FfpO/xp6dw+nXDbXVGRrwp+sJ+4nUDZ3fOcVCebmVPLk/un5BfYZDE6cM7fGcK7XfjCupjksa4GZX2T4Jq0I0KPIIFpZ9240SfGWLUn58aoFmTlR5pYvO65uoFSc7w7ut+SHNEkQRt9lKFi6OIgnLv29Tfe4h/h0fnQuFKj+F4TOP3/ikuJam5gvlYn5OkOrLHfiUMardW7RYlWYz90cqjEgfFPLBG0J53yOqlipdsvRTUpNAUZ12wW6hHLPSa7xH CFxhejvX Om9K8LSk/HOGFWhVlqGnfnlv5tbNDu17N8FLoDX8noAZrQYI+IAl566ndzSdYPFfu7ulfmgQguZNLfprc8WAo2WnOqQFr0Ziq+qiSzzmw1Rl2zAWBuJpH4K45QQmtD5mHiZUHWXXlEmVcqH64WZh7wnAWrAOtAM/HdKqt2A12IfcbD9oCSRhae88cuZdvkVk02WFeYNhgpKBcT0bhnnixJcciB2DHgMUF+rm+uwkKO7WybIXpmpDv/mBYcnb1N+IoIlwQ+Zend5xLzmadPkuv1eknMxSl2mbZlXxizA3+rTVOulfDGZaS4zLE4KTf35l7cBAtiIR89BrRo/PwaNHVEMWS0dc9AGo+Q4QpM+UVE6gsZM9HMN2atKhMaOCt/G1BKqYki23Csf5ZwFUP+BGXBJQ+LiuA3TOV/LIouciZjE/nM6vHIkf0ett2Ym/zfrFjcGLgLiulBDAfR/Q= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" powerpc overrides kprobes::alloc_insn_page() to remove writable permissions when STRICT_MODULE_RWX is on. Add definition of EXECMEM_KRPOBES to execmem_params to allow using the generic kprobes::alloc_insn_page() with the desired permissions. As powerpc uses breakpoint instructions to inject kprobes, it does not need to constrain kprobe allocations to the modules area and can use the entire vmalloc address space. Signed-off-by: Mike Rapoport (IBM) --- arch/powerpc/kernel/kprobes.c | 14 -------------- arch/powerpc/kernel/module.c | 11 +++++++++++ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/arch/powerpc/kernel/kprobes.c b/arch/powerpc/kernel/kprobes.c index 62228c7072a2..14c5ddec3056 100644 --- a/arch/powerpc/kernel/kprobes.c +++ b/arch/powerpc/kernel/kprobes.c @@ -126,20 +126,6 @@ kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offse return (kprobe_opcode_t *)(addr + offset); } -void *alloc_insn_page(void) -{ - void *page; - - page = execmem_text_alloc(EXECMEM_KPROBES, PAGE_SIZE); - if (!page) - return NULL; - - if (strict_module_rwx_enabled()) - set_memory_rox((unsigned long)page, 1); - - return page; -} - int arch_prepare_kprobe(struct kprobe *p) { int ret = 0; diff --git a/arch/powerpc/kernel/module.c b/arch/powerpc/kernel/module.c index 824d9541a310..bf2c62aef628 100644 --- a/arch/powerpc/kernel/module.c +++ b/arch/powerpc/kernel/module.c @@ -95,6 +95,9 @@ static struct execmem_params execmem_params __ro_after_init = { [EXECMEM_DEFAULT] = { .alignment = 1, }, + [EXECMEM_KPROBES] = { + .alignment = 1, + }, [EXECMEM_MODULE_DATA] = { .alignment = 1, }, @@ -135,5 +138,13 @@ struct execmem_params __init *execmem_arch_params(void) range->pgprot = prot; + execmem_params.ranges[EXECMEM_KPROBES].start = VMALLOC_START; + execmem_params.ranges[EXECMEM_KPROBES].start = VMALLOC_END; + + if (strict_module_rwx_enabled()) + execmem_params.ranges[EXECMEM_KPROBES].pgprot = PAGE_KERNEL_ROX; + else + execmem_params.ranges[EXECMEM_KPROBES].pgprot = PAGE_KERNEL_EXEC; + return &execmem_params; } From patchwork Mon Sep 18 07:29:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388979 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3AEF4CD13D2 for ; Mon, 18 Sep 2023 07:31:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BD2226B028B; Mon, 18 Sep 2023 03:31:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B5D7B6B028D; Mon, 18 Sep 2023 03:31:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 98BF96B028E; Mon, 18 Sep 2023 03:31:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 81FD76B028B for ; Mon, 18 Sep 2023 03:31:57 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 50F0580B61 for ; Mon, 18 Sep 2023 07:31:57 +0000 (UTC) X-FDA: 81248899074.29.295532F Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf08.hostedemail.com (Postfix) with ESMTP id 706EA16000F for ; Mon, 18 Sep 2023 07:31:55 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BpYN3pG2; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf08.hostedemail.com: domain of rppt@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022315; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=6g26NJg24TXt6CFg+UEJLVlOMObq4F61xl8/MmH9ZEU=; b=V2Rek68tkau6QSxkzWHSHoBnsyk8YCsKEbwiFEV3T4r/33xY1l/EUTW2eCEOx16CLciABS nusnA/u5Q3Dv/Kh9qScaxYiwgQ42ygaoiErFI2wAdEPrmnO7ZoRcOJcwaTIPzg9Tqakhp6 WaZsY4n0U+vE1EfeKZaZKRjaZacK8iQ= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BpYN3pG2; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf08.hostedemail.com: domain of rppt@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022315; a=rsa-sha256; cv=none; b=SyNdTZaNWRT0i/HNBeiX6HfB1lHYaiMV8KY7Vcs+7AFbDaxrwu6jWX5YbSnhSk3OD0cgMU 8v42V4w+30dOAArluAE0wlZzYqaSmRZHlzc3oZMUi/btrIJ7BytJ9MASKk+hppjr+5DJbM uean9vF/76cobG1r9H9mxQST8MFivUM= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id B7B3BB80BFF; Mon, 18 Sep 2023 07:31:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0D35BC433CD; Mon, 18 Sep 2023 07:31:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022312; bh=/lkcmGgfZ0h40rnIgrlGtkFY3fqKKa7LS9K2y40s078=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BpYN3pG2mJV/rqYXUts/nj29qV9EPMjt0zx/SMPu/4jisp6o2O8xaHc0tuU0dozmC EkX/reooL7FObsZ0D9hJ8qVr3s0jxZwQDniBogZP502PMqTzmb9zf8IC8v48ADq9JJ vhZzaS3bIiYR4KpLC0gQ9XGiX8Sg8U3ZgIlfxPbUxlyBunRPZGtKxFV4rwutmG1U2Q xNiJqoyc2QgzMbsZ7fvm26WevVyonKdQpLbr248krCbEnUGRO38cNr6hKTgmskgkoC M+BAIpPXGnu21jR6jiTi0lpov13TwKfc4fKKAJMozA7dQLpxyNDE6MCIa6xWY0rBPk BD29RoWukxl2Q== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 10/13] arch: make execmem setup available regardless of CONFIG_MODULES Date: Mon, 18 Sep 2023 10:29:52 +0300 Message-Id: <20230918072955.2507221-11-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 706EA16000F X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: a64hkza5588t18yrnd1r7xuuqwfsnap1 X-HE-Tag: 1695022315-781383 X-HE-Meta: U2FsdGVkX19WXi9hN5iStDPctNNlN8bI8xG41dcm2CHFySCYUmyWVj7OBJowM6fLOLz69HVv/rVbWow+LeKYcqo5xdX7KP6JlHtcq+Co7Geag+L2xanjfiDvGq6EcrSZYcRPvQr37BnfswIeGFNcZ54Asmg6YOsva1AB1jB5VHTDgt98Nb7vnFDe4p9KwIRApVdI8s0A5o4tT/A/DUrSjC30qlvylHkmcpqcoJIiw0gvjhUoO75S80NCd+tae04P+hIyfp0Ul2dpj7sauDMlGlxyC/ZgSRK5PxxgLs/RwdRIRkhUOHyOOAhwcDWPNZNTa3UY6ssvDoLKnDivtlRPkWj9cbkrQsdAY8/pePyc/I9vKgDNdtBlcsbeKseoUS8y1Rgw6W+o/tJjGKtDG9jteOwTmZomjYolf0pKPS96kBRcPp9mIEwHYRHXdMdiH2vyUBrfj6yikfoK6Sw5CztrgLIazJDNLCiEFWz6RkC2swCCkDqsZVRC6o4Pre55dV6+qgCS44mz7xnFN1g4/faGSXH1gJAzJtLowjGTdNSvV0TVHwWqsFwdlwAYmYWJt8OjgPP8jVe5864Ce00Ebpd1x7cEUQhV4UE9td1JWgKMvlnLicm+qgDFc3iMbozYH5QHRsVK5Tdsa3ecuKKTCNDkkBVew3ta2g6kNXWr4Aw3EdSXMeLjRax29ybYPV4UhyBok7cZBwfDPKfDSvMJtqLmxfJMr8ANvcUZxHiX7/05UTOB5wTbfT7lXfwhbM3jyfuP5AetCSmo9F1fVUa49trGcnsnyd44/eAeh07kdeS+h42GW9GTmTNyZGhbqF2iE9FK+zNFHUBgj8yhhIgexNvRWL1GHNr21EKdV/HXtau7QuXAw5VWSlsWUzy9DOSA1Qq0bJLW3ERahQPvqs8Dor92bfcnu1RDE9e+yDFjnsTLzwzjhXtiu0vpi/nGTv5762dfFkqY5UkyJN1lCC1D6s8 xtmxw69d Cn05v5qYpYplkh7ah8OvsD3HAhe20/BZE/7I+7zGLNHe2FXcK61oKk71b0jZhUXAmeUYJHaAXBc+rxodQoAgzXOApkM3739CDBAJ/KUcqHnhGRqPYJ6bjp+TLCJfsoTLvi9MkgiJZF9+0orcj23d2wYES634cJZINxCtF0f+gmTiAqnH+E+1KewxT5aw94lYCM2GI9GrBTWA8qTKUpLotpkQ8CrJJRnkfWVl22SpyC7/pKimlhIDGW+ETeHmH+7Q/5xHQIP7yolpuQF56S5c8yXZwbZRBDP3/QM/aeZwFU3xdadvWVidOG7zWPjG1bpeJj9rYa7AyTIPSRISnkOiVojjtNemqvLtmQqQbJWEN2KF0c8/SAr5Mo+IhDcCBFmDf4Tzp+Ja9juv8JeFxwL8iL1RaamcpG5k6nAF5DSkOiw6wv6w20STMTmJ/Lg7hTQjKlMp9pYbSb/LkY9o= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" execmem does not depend on modules, on the contrary modules use execmem. To make execmem available when CONFIG_MODULES=n, for instance for kprobes, split execmem_params initialization out from arch/kernel/module.c and compile it when CONFIG_EXECMEM=y Signed-off-by: Mike Rapoport (IBM) --- arch/arm/kernel/module.c | 38 ---------- arch/arm/mm/init.c | 38 ++++++++++ arch/arm64/kernel/module.c | 130 -------------------------------- arch/arm64/mm/init.c | 132 +++++++++++++++++++++++++++++++++ arch/loongarch/kernel/module.c | 18 ----- arch/loongarch/mm/init.c | 20 +++++ arch/mips/kernel/module.c | 19 ----- arch/mips/mm/init.c | 20 +++++ arch/parisc/kernel/module.c | 17 ----- arch/parisc/mm/init.c | 22 +++++- arch/powerpc/kernel/module.c | 60 --------------- arch/powerpc/mm/mem.c | 62 ++++++++++++++++ arch/riscv/kernel/module.c | 39 ---------- arch/riscv/mm/init.c | 39 ++++++++++ arch/s390/kernel/module.c | 25 ------- arch/s390/mm/init.c | 28 +++++++ arch/sparc/kernel/module.c | 23 ------ arch/sparc/mm/Makefile | 2 + arch/sparc/mm/execmem.c | 25 +++++++ arch/x86/kernel/module.c | 27 ------- arch/x86/mm/init.c | 29 ++++++++ 21 files changed, 416 insertions(+), 397 deletions(-) create mode 100644 arch/sparc/mm/execmem.c diff --git a/arch/arm/kernel/module.c b/arch/arm/kernel/module.c index 2c7651a2d84c..3282f304f6b1 100644 --- a/arch/arm/kernel/module.c +++ b/arch/arm/kernel/module.c @@ -16,50 +16,12 @@ #include #include #include -#include #include #include #include #include -#ifdef CONFIG_XIP_KERNEL -/* - * The XIP kernel text is mapped in the module area for modules and - * some other stuff to work without any indirect relocations. - * MODULES_VADDR is redefined here and not in asm/memory.h to avoid - * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off. - */ -#undef MODULES_VADDR -#define MODULES_VADDR (((unsigned long)_exiprom + ~PMD_MASK) & PMD_MASK) -#endif - -#ifdef CONFIG_MMU -static struct execmem_params execmem_params __ro_after_init = { - .ranges = { - [EXECMEM_DEFAULT] = { - .start = MODULES_VADDR, - .end = MODULES_END, - .alignment = 1, - }, - }, -}; - -struct execmem_params __init *execmem_arch_params(void) -{ - struct execmem_range *r = &execmem_params.ranges[EXECMEM_DEFAULT]; - - r->pgprot = PAGE_KERNEL_EXEC; - - if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS)) { - r->fallback_start = VMALLOC_START; - r->fallback_end = VMALLOC_END; - } - - return &execmem_params; -} -#endif - bool module_init_section(const char *name) { return strstarts(name, ".init") || diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c index a42e4cd11db2..c0b536e398b4 100644 --- a/arch/arm/mm/init.c +++ b/arch/arm/mm/init.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -486,3 +487,40 @@ void free_initrd_mem(unsigned long start, unsigned long end) free_reserved_area((void *)start, (void *)end, -1, "initrd"); } #endif + +#ifdef CONFIG_XIP_KERNEL +/* + * The XIP kernel text is mapped in the module area for modules and + * some other stuff to work without any indirect relocations. + * MODULES_VADDR is redefined here and not in asm/memory.h to avoid + * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off. + */ +#undef MODULES_VADDR +#define MODULES_VADDR (((unsigned long)_exiprom + ~PMD_MASK) & PMD_MASK) +#endif + +#if defined(CONFIG_MMU) && defined(CONFIG_EXECMEM) +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .start = MODULES_VADDR, + .end = MODULES_END, + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) +{ + struct execmem_range *r = &execmem_params.ranges[EXECMEM_DEFAULT]; + + r->pgprot = PAGE_KERNEL_EXEC; + + if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS)) { + r->fallback_start = VMALLOC_START; + r->fallback_end = VMALLOC_END; + } + + return &execmem_params; +} +#endif diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c index d27db168d2a2..eb1505128b75 100644 --- a/arch/arm64/kernel/module.c +++ b/arch/arm64/kernel/module.c @@ -20,142 +20,12 @@ #include #include #include -#include #include #include #include #include -static u64 module_direct_base __ro_after_init = 0; -static u64 module_plt_base __ro_after_init = 0; - -/* - * Choose a random page-aligned base address for a window of 'size' bytes which - * entirely contains the interval [start, end - 1]. - */ -static u64 __init random_bounding_box(u64 size, u64 start, u64 end) -{ - u64 max_pgoff, pgoff; - - if ((end - start) >= size) - return 0; - - max_pgoff = (size - (end - start)) / PAGE_SIZE; - pgoff = get_random_u32_inclusive(0, max_pgoff); - - return start - pgoff * PAGE_SIZE; -} - -/* - * Modules may directly reference data and text anywhere within the kernel - * image and other modules. References using PREL32 relocations have a +/-2G - * range, and so we need to ensure that the entire kernel image and all modules - * fall within a 2G window such that these are always within range. - * - * Modules may directly branch to functions and code within the kernel text, - * and to functions and code within other modules. These branches will use - * CALL26/JUMP26 relocations with a +/-128M range. Without PLTs, we must ensure - * that the entire kernel text and all module text falls within a 128M window - * such that these are always within range. With PLTs, we can expand this to a - * 2G window. - * - * We chose the 128M region to surround the entire kernel image (rather than - * just the text) as using the same bounds for the 128M and 2G regions ensures - * by construction that we never select a 128M region that is not a subset of - * the 2G region. For very large and unusual kernel configurations this means - * we may fall back to PLTs where they could have been avoided, but this keeps - * the logic significantly simpler. - */ -static int __init module_init_limits(void) -{ - u64 kernel_end = (u64)_end; - u64 kernel_start = (u64)_text; - u64 kernel_size = kernel_end - kernel_start; - - /* - * The default modules region is placed immediately below the kernel - * image, and is large enough to use the full 2G relocation range. - */ - BUILD_BUG_ON(KIMAGE_VADDR != MODULES_END); - BUILD_BUG_ON(MODULES_VSIZE < SZ_2G); - - if (!kaslr_enabled()) { - if (kernel_size < SZ_128M) - module_direct_base = kernel_end - SZ_128M; - if (kernel_size < SZ_2G) - module_plt_base = kernel_end - SZ_2G; - } else { - u64 min = kernel_start; - u64 max = kernel_end; - - if (IS_ENABLED(CONFIG_RANDOMIZE_MODULE_REGION_FULL)) { - pr_info("2G module region forced by RANDOMIZE_MODULE_REGION_FULL\n"); - } else { - module_direct_base = random_bounding_box(SZ_128M, min, max); - if (module_direct_base) { - min = module_direct_base; - max = module_direct_base + SZ_128M; - } - } - - module_plt_base = random_bounding_box(SZ_2G, min, max); - } - - pr_info("%llu pages in range for non-PLT usage", - module_direct_base ? (SZ_128M - kernel_size) / PAGE_SIZE : 0); - pr_info("%llu pages in range for PLT usage", - module_plt_base ? (SZ_2G - kernel_size) / PAGE_SIZE : 0); - - return 0; -} - -static struct execmem_params execmem_params __ro_after_init = { - .ranges = { - [EXECMEM_DEFAULT] = { - .flags = EXECMEM_KASAN_SHADOW, - .alignment = MODULE_ALIGN, - }, - [EXECMEM_KPROBES] = { - .start = VMALLOC_START, - .end = VMALLOC_END, - .alignment = 1, - }, - [EXECMEM_BPF] = { - .start = VMALLOC_START, - .end = VMALLOC_END, - .alignment = 1, - }, - }, -}; - -struct execmem_params __init *execmem_arch_params(void) -{ - struct execmem_range *r = &execmem_params.ranges[EXECMEM_DEFAULT]; - - module_init_limits(); - - r->pgprot = PAGE_KERNEL; - - if (module_direct_base) { - r->start = module_direct_base; - r->end = module_direct_base + SZ_128M; - - if (module_plt_base) { - r->fallback_start = module_plt_base; - r->fallback_end = module_plt_base + SZ_2G; - } - } else if (module_plt_base) { - r->start = module_plt_base; - r->end = module_plt_base + SZ_2G; - } - - execmem_params.ranges[EXECMEM_KPROBES].pgprot = PAGE_KERNEL_ROX; - execmem_params.ranges[EXECMEM_BPF].pgprot = PAGE_KERNEL; - - return &execmem_params; -} - enum aarch64_reloc_op { RELOC_OP_NONE, RELOC_OP_ABS, diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 8a0f8604348b..9b7716b4d84c 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -547,3 +548,134 @@ void dump_mem_limit(void) pr_emerg("Memory Limit: none\n"); } } + +#ifdef CONFIG_EXECMEM +static u64 module_direct_base __ro_after_init = 0; +static u64 module_plt_base __ro_after_init = 0; + +/* + * Choose a random page-aligned base address for a window of 'size' bytes which + * entirely contains the interval [start, end - 1]. + */ +static u64 __init random_bounding_box(u64 size, u64 start, u64 end) +{ + u64 max_pgoff, pgoff; + + if ((end - start) >= size) + return 0; + + max_pgoff = (size - (end - start)) / PAGE_SIZE; + pgoff = get_random_u32_inclusive(0, max_pgoff); + + return start - pgoff * PAGE_SIZE; +} + +/* + * Modules may directly reference data and text anywhere within the kernel + * image and other modules. References using PREL32 relocations have a +/-2G + * range, and so we need to ensure that the entire kernel image and all modules + * fall within a 2G window such that these are always within range. + * + * Modules may directly branch to functions and code within the kernel text, + * and to functions and code within other modules. These branches will use + * CALL26/JUMP26 relocations with a +/-128M range. Without PLTs, we must ensure + * that the entire kernel text and all module text falls within a 128M window + * such that these are always within range. With PLTs, we can expand this to a + * 2G window. + * + * We chose the 128M region to surround the entire kernel image (rather than + * just the text) as using the same bounds for the 128M and 2G regions ensures + * by construction that we never select a 128M region that is not a subset of + * the 2G region. For very large and unusual kernel configurations this means + * we may fall back to PLTs where they could have been avoided, but this keeps + * the logic significantly simpler. + */ +static int __init module_init_limits(void) +{ + u64 kernel_end = (u64)_end; + u64 kernel_start = (u64)_text; + u64 kernel_size = kernel_end - kernel_start; + + /* + * The default modules region is placed immediately below the kernel + * image, and is large enough to use the full 2G relocation range. + */ + BUILD_BUG_ON(KIMAGE_VADDR != MODULES_END); + BUILD_BUG_ON(MODULES_VSIZE < SZ_2G); + + if (!kaslr_enabled()) { + if (kernel_size < SZ_128M) + module_direct_base = kernel_end - SZ_128M; + if (kernel_size < SZ_2G) + module_plt_base = kernel_end - SZ_2G; + } else { + u64 min = kernel_start; + u64 max = kernel_end; + + if (IS_ENABLED(CONFIG_RANDOMIZE_MODULE_REGION_FULL)) { + pr_info("2G module region forced by RANDOMIZE_MODULE_REGION_FULL\n"); + } else { + module_direct_base = random_bounding_box(SZ_128M, min, max); + if (module_direct_base) { + min = module_direct_base; + max = module_direct_base + SZ_128M; + } + } + + module_plt_base = random_bounding_box(SZ_2G, min, max); + } + + pr_info("%llu pages in range for non-PLT usage", + module_direct_base ? (SZ_128M - kernel_size) / PAGE_SIZE : 0); + pr_info("%llu pages in range for PLT usage", + module_plt_base ? (SZ_2G - kernel_size) / PAGE_SIZE : 0); + + return 0; +} + +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .flags = EXECMEM_KASAN_SHADOW, + .alignment = MODULE_ALIGN, + }, + [EXECMEM_KPROBES] = { + .start = VMALLOC_START, + .end = VMALLOC_END, + .alignment = 1, + }, + [EXECMEM_BPF] = { + .start = VMALLOC_START, + .end = VMALLOC_END, + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) +{ + struct execmem_range *r = &execmem_params.ranges[EXECMEM_DEFAULT]; + + module_init_limits(); + + r->pgprot = PAGE_KERNEL; + + if (module_direct_base) { + r->start = module_direct_base; + r->end = module_direct_base + SZ_128M; + + if (module_plt_base) { + r->fallback_start = module_plt_base; + r->fallback_end = module_plt_base + SZ_2G; + } + } else if (module_plt_base) { + r->start = module_plt_base; + r->end = module_plt_base + SZ_2G; + } + + execmem_params.ranges[EXECMEM_KPROBES].pgprot = PAGE_KERNEL_ROX; + execmem_params.ranges[EXECMEM_BPF].pgprot = PAGE_KERNEL; + + return &execmem_params; +} +#endif diff --git a/arch/loongarch/kernel/module.c b/arch/loongarch/kernel/module.c index a1d8fe9796fa..181b5f8b09f1 100644 --- a/arch/loongarch/kernel/module.c +++ b/arch/loongarch/kernel/module.c @@ -18,7 +18,6 @@ #include #include #include -#include #include #include @@ -470,23 +469,6 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, return 0; } -static struct execmem_params execmem_params __ro_after_init = { - .ranges = { - [EXECMEM_DEFAULT] = { - .pgprot = PAGE_KERNEL, - .alignment = 1, - }, - }, -}; - -struct execmem_params __init *execmem_arch_params(void) -{ - execmem_params.ranges[EXECMEM_DEFAULT].start = MODULES_VADDR; - execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; - - return &execmem_params; -} - static void module_init_ftrace_plt(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, struct module *mod) { diff --git a/arch/loongarch/mm/init.c b/arch/loongarch/mm/init.c index f3fe8c06ba4d..26b10a51309c 100644 --- a/arch/loongarch/mm/init.c +++ b/arch/loongarch/mm/init.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -247,3 +248,22 @@ EXPORT_SYMBOL(invalid_pmd_table); #endif pte_t invalid_pte_table[PTRS_PER_PTE] __page_aligned_bss; EXPORT_SYMBOL(invalid_pte_table); + +#ifdef CONFIG_EXECMEM +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .pgprot = PAGE_KERNEL, + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) +{ + execmem_params.ranges[EXECMEM_DEFAULT].start = MODULES_VADDR; + execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; + + return &execmem_params; +} +#endif diff --git a/arch/mips/kernel/module.c b/arch/mips/kernel/module.c index 1c959074b35f..ebf9496f5db0 100644 --- a/arch/mips/kernel/module.c +++ b/arch/mips/kernel/module.c @@ -33,25 +33,6 @@ struct mips_hi16 { static LIST_HEAD(dbe_list); static DEFINE_SPINLOCK(dbe_lock); -#ifdef MODULE_START -static struct execmem_params execmem_params __ro_after_init = { - .ranges = { - [EXECMEM_DEFAULT] = { - .start = MODULE_START, - .end = MODULE_END, - .alignment = 1, - }, - }, -}; - -struct execmem_params __init *execmem_arch_params(void) -{ - execmem_params.ranges[EXECMEM_DEFAULT].pgprot = PAGE_KERNEL; - - return &execmem_params; -} -#endif - static void apply_r_mips_32(u32 *location, u32 base, Elf_Addr v) { *location = base + v; diff --git a/arch/mips/mm/init.c b/arch/mips/mm/init.c index 5dcb525a8995..55e7869d03f2 100644 --- a/arch/mips/mm/init.c +++ b/arch/mips/mm/init.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -573,3 +574,22 @@ EXPORT_SYMBOL_GPL(invalid_pmd_table); #endif pte_t invalid_pte_table[PTRS_PER_PTE] __page_aligned_bss; EXPORT_SYMBOL(invalid_pte_table); + +#if defined(CONFIG_EXECMEM) && defined(MODULE_START) +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .start = MODULE_START, + .end = MODULE_END, + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) +{ + execmem_params.ranges[EXECMEM_DEFAULT].pgprot = PAGE_KERNEL; + + return &execmem_params; +} +#endif diff --git a/arch/parisc/kernel/module.c b/arch/parisc/kernel/module.c index 0c6dfd1daef3..fecd2760b7a6 100644 --- a/arch/parisc/kernel/module.c +++ b/arch/parisc/kernel/module.c @@ -174,23 +174,6 @@ static inline int reassemble_22(int as22) ((as22 & 0x0003ff) << 3)); } -static struct execmem_params execmem_params __ro_after_init = { - .ranges = { - [EXECMEM_DEFAULT] = { - .pgprot = PAGE_KERNEL_RWX, - .alignment = 1, - }, - }, -}; - -struct execmem_params __init *execmem_arch_params(void) -{ - execmem_params.ranges[EXECMEM_DEFAULT].start = VMALLOC_START; - execmem_params.ranges[EXECMEM_DEFAULT].end = VMALLOC_END; - - return &execmem_params; -} - #ifndef CONFIG_64BIT static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n) { diff --git a/arch/parisc/mm/init.c b/arch/parisc/mm/init.c index a088c243edea..c87fed38e38e 100644 --- a/arch/parisc/mm/init.c +++ b/arch/parisc/mm/init.c @@ -24,6 +24,7 @@ #include /* for node_online_map */ #include /* for release_pages */ #include +#include #include #include @@ -479,7 +480,7 @@ void free_initmem(void) /* finally dump all the instructions which were cached, since the * pages are no-longer executable */ flush_icache_range(init_begin, init_end); - + free_initmem_default(POISON_FREE_INITMEM); /* set up a new led state on systems shipped LED State panel */ @@ -919,3 +920,22 @@ static const pgprot_t protection_map[16] = { [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_RWX }; DECLARE_VM_GET_PAGE_PROT + +#ifdef CONFIG_EXECMEM +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .pgprot = PAGE_KERNEL_RWX, + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) +{ + execmem_params.ranges[EXECMEM_DEFAULT].start = VMALLOC_START; + execmem_params.ranges[EXECMEM_DEFAULT].end = VMALLOC_END; + + return &execmem_params; +} +#endif diff --git a/arch/powerpc/kernel/module.c b/arch/powerpc/kernel/module.c index bf2c62aef628..b30e00964a60 100644 --- a/arch/powerpc/kernel/module.c +++ b/arch/powerpc/kernel/module.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -89,62 +88,3 @@ int module_finalize(const Elf_Ehdr *hdr, return 0; } - -static struct execmem_params execmem_params __ro_after_init = { - .ranges = { - [EXECMEM_DEFAULT] = { - .alignment = 1, - }, - [EXECMEM_KPROBES] = { - .alignment = 1, - }, - [EXECMEM_MODULE_DATA] = { - .alignment = 1, - }, - }, -}; - -struct execmem_params __init *execmem_arch_params(void) -{ - pgprot_t prot = strict_module_rwx_enabled() ? PAGE_KERNEL : PAGE_KERNEL_EXEC; - struct execmem_range *range = &execmem_params.ranges[EXECMEM_DEFAULT]; - - /* - * BOOK3S_32 and 8xx define MODULES_VADDR for text allocations and - * allow allocating data in the entire vmalloc space - */ -#ifdef MODULES_VADDR - struct execmem_range *data = &execmem_params.ranges[EXECMEM_MODULE_DATA]; - unsigned long limit = (unsigned long)_etext - SZ_32M; - - /* First try within 32M limit from _etext to avoid branch trampolines */ - if (MODULES_VADDR < PAGE_OFFSET && MODULES_END > limit) { - range->start = limit; - range->end = MODULES_END; - range->fallback_start = MODULES_VADDR; - range->fallback_end = MODULES_END; - } else { - range->start = MODULES_VADDR; - range->end = MODULES_END; - } - data->start = VMALLOC_START; - data->end = VMALLOC_END; - data->pgprot = PAGE_KERNEL; - data->alignment = 1; -#else - range->start = VMALLOC_START; - range->end = VMALLOC_END; -#endif - - range->pgprot = prot; - - execmem_params.ranges[EXECMEM_KPROBES].start = VMALLOC_START; - execmem_params.ranges[EXECMEM_KPROBES].start = VMALLOC_END; - - if (strict_module_rwx_enabled()) - execmem_params.ranges[EXECMEM_KPROBES].pgprot = PAGE_KERNEL_ROX; - else - execmem_params.ranges[EXECMEM_KPROBES].pgprot = PAGE_KERNEL_EXEC; - - return &execmem_params; -} diff --git a/arch/powerpc/mm/mem.c b/arch/powerpc/mm/mem.c index 8b121df7b08f..06f4bb6fb780 100644 --- a/arch/powerpc/mm/mem.c +++ b/arch/powerpc/mm/mem.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -406,3 +407,64 @@ int devmem_is_allowed(unsigned long pfn) * the EHEA driver. Drop this when drivers/net/ethernet/ibm/ehea is removed. */ EXPORT_SYMBOL_GPL(walk_system_ram_range); + +#ifdef CONFIG_EXECMEM +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .alignment = 1, + }, + [EXECMEM_KPROBES] = { + .alignment = 1, + }, + [EXECMEM_MODULE_DATA] = { + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) +{ + pgprot_t prot = strict_module_rwx_enabled() ? PAGE_KERNEL : PAGE_KERNEL_EXEC; + struct execmem_range *range = &execmem_params.ranges[EXECMEM_DEFAULT]; + + /* + * BOOK3S_32 and 8xx define MODULES_VADDR for text allocations and + * allow allocating data in the entire vmalloc space + */ +#ifdef MODULES_VADDR + struct execmem_range *data = &execmem_params.ranges[EXECMEM_MODULE_DATA]; + unsigned long limit = (unsigned long)_etext - SZ_32M; + + /* First try within 32M limit from _etext to avoid branch trampolines */ + if (MODULES_VADDR < PAGE_OFFSET && MODULES_END > limit) { + range->start = limit; + range->end = MODULES_END; + range->fallback_start = MODULES_VADDR; + range->fallback_end = MODULES_END; + } else { + range->start = MODULES_VADDR; + range->end = MODULES_END; + } + data->start = VMALLOC_START; + data->end = VMALLOC_END; + data->pgprot = PAGE_KERNEL; + data->alignment = 1; +#else + range->start = VMALLOC_START; + range->end = VMALLOC_END; +#endif + + range->pgprot = prot; + + execmem_params.ranges[EXECMEM_KPROBES].start = VMALLOC_START; + execmem_params.ranges[EXECMEM_KPROBES].start = VMALLOC_END; + + if (strict_module_rwx_enabled()) + execmem_params.ranges[EXECMEM_KPROBES].pgprot = PAGE_KERNEL_ROX; + else + execmem_params.ranges[EXECMEM_KPROBES].pgprot = PAGE_KERNEL_EXEC; + + return &execmem_params; +} +#endif diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 31505ecb5c72..8af08d5449bf 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -11,7 +11,6 @@ #include #include #include -#include #include #include @@ -436,44 +435,6 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, return 0; } -#ifdef CONFIG_MMU -static struct execmem_params execmem_params __ro_after_init = { - .ranges = { - [EXECMEM_DEFAULT] = { - .pgprot = PAGE_KERNEL, - .alignment = 1, - }, - [EXECMEM_KPROBES] = { - .pgprot = PAGE_KERNEL_READ_EXEC, - .alignment = 1, - }, - [EXECMEM_BPF] = { - .pgprot = PAGE_KERNEL, - .alignment = 1, - }, - }, -}; - -struct execmem_params __init *execmem_arch_params(void) -{ -#ifdef CONFIG_64BIT - execmem_params.ranges[EXECMEM_DEFAULT].start = MODULES_VADDR; - execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; -#else - execmem_params.ranges[EXECMEM_DEFAULT].start = VMALLOC_START; - execmem_params.ranges[EXECMEM_DEFAULT].end = VMALLOC_END; -#endif - - execmem_params.ranges[EXECMEM_KPROBES].start = VMALLOC_START; - execmem_params.ranges[EXECMEM_KPROBES].end = VMALLOC_END; - - execmem_params.ranges[EXECMEM_BPF].start = BPF_JIT_REGION_START; - execmem_params.ranges[EXECMEM_BPF].end = BPF_JIT_REGION_END; - - return &execmem_params; -} -#endif - int module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, struct module *me) diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 0798bd861dcb..b0f7848f39e3 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -24,6 +24,7 @@ #include #endif #include +#include #include #include @@ -1564,3 +1565,41 @@ void __init pgtable_cache_init(void) preallocate_pgd_pages_range(MODULES_VADDR, MODULES_END, "bpf/modules"); } #endif + +#if defined(CONFIG_MMU) && defined(CONFIG_EXECMEM) +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .pgprot = PAGE_KERNEL, + .alignment = 1, + }, + [EXECMEM_KPROBES] = { + .pgprot = PAGE_KERNEL_READ_EXEC, + .alignment = 1, + }, + [EXECMEM_BPF] = { + .pgprot = PAGE_KERNEL, + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) +{ +#ifdef CONFIG_64BIT + execmem_params.ranges[EXECMEM_DEFAULT].start = MODULES_VADDR; + execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; +#else + execmem_params.ranges[EXECMEM_DEFAULT].start = VMALLOC_START; + execmem_params.ranges[EXECMEM_DEFAULT].end = VMALLOC_END; +#endif + + execmem_params.ranges[EXECMEM_KPROBES].start = VMALLOC_START; + execmem_params.ranges[EXECMEM_KPROBES].end = VMALLOC_END; + + execmem_params.ranges[EXECMEM_BPF].start = BPF_JIT_REGION_START; + execmem_params.ranges[EXECMEM_BPF].end = BPF_JIT_REGION_END; + + return &execmem_params; +} +#endif diff --git a/arch/s390/kernel/module.c b/arch/s390/kernel/module.c index 538d5f24af66..81a8d92ca092 100644 --- a/arch/s390/kernel/module.c +++ b/arch/s390/kernel/module.c @@ -37,31 +37,6 @@ #define PLT_ENTRY_SIZE 22 -static struct execmem_params execmem_params __ro_after_init = { - .ranges = { - [EXECMEM_DEFAULT] = { - .flags = EXECMEM_KASAN_SHADOW, - .alignment = MODULE_ALIGN, - .pgprot = PAGE_KERNEL, - }, - }, -}; - -struct execmem_params __init *execmem_arch_params(void) -{ - unsigned long module_load_offset = 0; - unsigned long start; - - if (kaslr_enabled()) - module_load_offset = get_random_u32_inclusive(1, 1024) * PAGE_SIZE; - - start = MODULES_VADDR + module_load_offset; - execmem_params.ranges[EXECMEM_DEFAULT].start = start; - execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; - - return &execmem_params; -} - #ifdef CONFIG_FUNCTION_TRACER void module_arch_cleanup(struct module *mod) { diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c index 8b94d2212d33..2e6d6512fc5f 100644 --- a/arch/s390/mm/init.c +++ b/arch/s390/mm/init.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -311,3 +312,30 @@ void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap) vmem_remove_mapping(start, size); } #endif /* CONFIG_MEMORY_HOTPLUG */ + +#ifdef CONFIG_EXECMEM +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .flags = EXECMEM_KASAN_SHADOW, + .alignment = MODULE_ALIGN, + .pgprot = PAGE_KERNEL, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) +{ + unsigned long module_load_offset = 0; + unsigned long start; + + if (kaslr_enabled()) + module_load_offset = get_random_u32_inclusive(1, 1024) * PAGE_SIZE; + + start = MODULES_VADDR + module_load_offset; + execmem_params.ranges[EXECMEM_DEFAULT].start = start; + execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; + + return &execmem_params; +} +#endif diff --git a/arch/sparc/kernel/module.c b/arch/sparc/kernel/module.c index 1d8d1fba95b9..dff1d85ba202 100644 --- a/arch/sparc/kernel/module.c +++ b/arch/sparc/kernel/module.c @@ -14,7 +14,6 @@ #include #include #include -#include #ifdef CONFIG_SPARC64 #include #endif @@ -25,28 +24,6 @@ #include "entry.h" -static struct execmem_params execmem_params __ro_after_init = { - .ranges = { - [EXECMEM_DEFAULT] = { -#ifdef CONFIG_SPARC64 - .start = MODULES_VADDR, - .end = MODULES_END, -#else - .start = VMALLOC_START, - .end = VMALLOC_END, -#endif - .alignment = 1, - }, - }, -}; - -struct execmem_params __init *execmem_arch_params(void) -{ - execmem_params.ranges[EXECMEM_DEFAULT].pgprot = PAGE_KERNEL; - - return &execmem_params; -} - /* Make generic code ignore STT_REGISTER dummy undefined symbols. */ int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, diff --git a/arch/sparc/mm/Makefile b/arch/sparc/mm/Makefile index 871354aa3c00..87e2cf7efb5b 100644 --- a/arch/sparc/mm/Makefile +++ b/arch/sparc/mm/Makefile @@ -15,3 +15,5 @@ obj-$(CONFIG_SPARC32) += leon_mm.o # Only used by sparc64 obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o + +obj-$(CONFIG_EXECMEM) += execmem.o diff --git a/arch/sparc/mm/execmem.c b/arch/sparc/mm/execmem.c new file mode 100644 index 000000000000..fb53a859869a --- /dev/null +++ b/arch/sparc/mm/execmem.c @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { +#ifdef CONFIG_SPARC64 + .start = MODULES_VADDR, + .end = MODULES_END, +#else + .start = VMALLOC_START, + .end = VMALLOC_END, +#endif + .alignment = 1, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) +{ + execmem_params.ranges[EXECMEM_DEFAULT].pgprot = PAGE_KERNEL; + + return &execmem_params; +} diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c index 9d37375e2f05..c52d591c0f3f 100644 --- a/arch/x86/kernel/module.c +++ b/arch/x86/kernel/module.c @@ -19,7 +19,6 @@ #include #include #include -#include #include #include @@ -37,32 +36,6 @@ do { \ } while (0) #endif -static struct execmem_params execmem_params __ro_after_init = { - .ranges = { - [EXECMEM_DEFAULT] = { - .flags = EXECMEM_KASAN_SHADOW, - .alignment = MODULE_ALIGN, - }, - }, -}; - -struct execmem_params __init *execmem_arch_params(void) -{ - unsigned long module_load_offset = 0; - unsigned long start; - - if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled()) - module_load_offset = - get_random_u32_inclusive(1, 1024) * PAGE_SIZE; - - start = MODULES_VADDR + module_load_offset; - execmem_params.ranges[EXECMEM_DEFAULT].start = start; - execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; - execmem_params.ranges[EXECMEM_DEFAULT].pgprot = PAGE_KERNEL; - - return &execmem_params; -} - #ifdef CONFIG_X86_32 int apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c index 679893ea5e68..022af7ab50f9 100644 --- a/arch/x86/mm/init.c +++ b/arch/x86/mm/init.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -1099,3 +1100,31 @@ unsigned long arch_max_swapfile_size(void) return pages; } #endif + +#ifdef CONFIG_EXECMEM +static struct execmem_params execmem_params __ro_after_init = { + .ranges = { + [EXECMEM_DEFAULT] = { + .flags = EXECMEM_KASAN_SHADOW, + .alignment = MODULE_ALIGN, + }, + }, +}; + +struct execmem_params __init *execmem_arch_params(void) +{ + unsigned long module_load_offset = 0; + unsigned long start; + + if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled()) + module_load_offset = + get_random_u32_inclusive(1, 1024) * PAGE_SIZE; + + start = MODULES_VADDR + module_load_offset; + execmem_params.ranges[EXECMEM_DEFAULT].start = start; + execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END; + execmem_params.ranges[EXECMEM_DEFAULT].pgprot = PAGE_KERNEL; + + return &execmem_params; +} +#endif /* CONFIG_EXECMEM */ From patchwork Mon Sep 18 07:29:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388980 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A9C69C46CA1 for ; Mon, 18 Sep 2023 07:32:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4311E6B028D; Mon, 18 Sep 2023 03:32:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BBE46B0290; Mon, 18 Sep 2023 03:32:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2369A6B0291; Mon, 18 Sep 2023 03:32:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 0E7A46B028D for ; Mon, 18 Sep 2023 03:32:05 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id DA1A9A0C3B for ; Mon, 18 Sep 2023 07:32:04 +0000 (UTC) X-FDA: 81248899368.29.190210F Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf30.hostedemail.com (Postfix) with ESMTP id 11C6C80005 for ; Mon, 18 Sep 2023 07:32:02 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=jOnWnURB; spf=pass (imf30.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022323; a=rsa-sha256; cv=none; b=oF5PQWxiOsBsodkxoxwc8D44wQAqcMaaxx6y3VVvDKBJdv9Ehtgdr/wNlrhzNS/M19/2PV zRwMMLx+sZ0jKSZ0E4DiFbGs8HOOisM6Coyt9xrKLn4yaMQ21vtxL8OoJe4zDt6Fv+CYWe pLhFPl7nMD2LCnbx+mJAV1Gxsj59szg= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=jOnWnURB; spf=pass (imf30.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022323; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=pZR7i50VIMpGPwB+fYLRiSUppIV7vmV9jffC49cC9qk=; b=rgp2S2YSPD3MX7j3qYeVMqVBWUxZZN0YOI76i9VPqJ9+G4agQnjtn/Y/Dski4Ch2QsFiIj wMRvVTyhPVWpF3KeD1PPEmM+J2r35nFux3UhQeXs7LOiwwBWy/jWWUF9+eYHFwSFeOD/SR bQBfcvmJ9Yi8V3CaxgIFad3QXFFLS6I= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 6804C60FA1; Mon, 18 Sep 2023 07:32:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE792C433BC; Mon, 18 Sep 2023 07:31:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022322; bh=1UL8tl9eANdNOFG3ZTSYgBTww7VCSn8pNWCEuJfnTK4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jOnWnURBWcrt92iQ1E5Vr3mXWyQ6RAJ4i9B8tsY7FXOUVwWaNtFLbRVsEzVfJ0Nfg zcF2T76bilI3SPkaUjpMzEfs1sQN/JzTRMR83DnXh+Ov+MJ2AuKwZwF8Fnu/AObC0k QAdcrio7/1JZevOAFEj9aMF9cUGQUD6UCX9K1G2jVF2I2HWuIYbnj0wy/Rm4EXVNom HeAH3TaXGuMZDe4Y3BnUwv/DnuIZbNW8G+v45crhACvScxYPJb49nAjf6MNnqIIZFe chFFMgGweSzKG7hh/3g3keI9zbSw1HmwgbPcUexA3rwgKV2tInhKeo5IsMuopaavQ2 VqOvvHTW8VtZw== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 11/13] x86/ftrace: enable dynamic ftrace without CONFIG_MODULES Date: Mon, 18 Sep 2023 10:29:53 +0300 Message-Id: <20230918072955.2507221-12-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 11C6C80005 X-Stat-Signature: yk81k8wp5mdygpcw4kio6arwf53o1kyc X-Rspam-User: X-HE-Tag: 1695022322-329436 X-HE-Meta: U2FsdGVkX199awmVAgTGJQUqJgtqiVJOWjxScekzMVGUi/YT3Hc+LKamIKXLXnhUHQhof4u0X6+lMUGP0R7zFcB1pQhXCC+OW59ClLBqgcPpu8qdlqOVZeCiJdFUpUYppXeo1HPmBVNLtjWCEaXG5uVzqa9VYLn6UPXN9VMI3pzvX/u5xx1frkZk43uOujc0oNmsHlRydJYm7W3ytBpo3T78zNN3nl2CETHYq0BROtwE6HBjPe3QOWpc0CZPUIauQ5feMRwq3S6WpmfdfJhfPfZ4NvVUPRg8m41suoPqHRldxk20pDj4MqOFI2ILawGkewqEcyd1T1G450SR6mEI+U9JpRk/P8G9DEgx5KzlegfZmbEp8DwFMyYzGQhoc6sse/w1tPVRortOSbuFPMFTDaI6u0VMXrMCH/tf66ONkW9y42lVwx7R4ZebRmuBwQcqt+XHbv3Jtm/U/9h1X0uEcfq76hcIQ7f41gmrcIeph1ExTYgUfDIr8RZmFsjj0eoE+pXmWDqnK1zcR18lrOq98QXor2JE/52fgjtEqo791dC8AvIDOJdXy7SEuJn07KZZ3WZFouF5gwGTdkXdbssWGUknJ/hE/wQmRAwIP9FrzteT78dnkq+UuORnvPPVEpiimkUj+2FNV+nhd9V2a8ESJBk5nyeuAQJAbIirHPZ2QTyDHACCqryfTkomDP07Mjcgee3e49JI/Sd8mAm6F36vTQl3r8Nlw3Iwy64wR2pdSOhSDcuErw6lNX644WkSgb3kAC8i5N3ltASBufOGbvNO+iHLo9qwiulxbTeRzqz+ieNNudTf+8cEdBTB7WHfpf6+9VTni5aL+ExZDNRIhyzSyF4Dk2Vv+tujurUNzcWOAQqyo4yssudCOKk+3xrWvA19oy0wsGOCiUSArl7TBRUsFX5CQJUDNkCbusCP8X/g6INw8I8HlJK2cLlSf2/nWW8lyaQuw2T0EpDT1ySxOSA 9DXMHxiP FiqvNCWF7ltLNxctv7ARoNvWu81ikTVrmVrdy8f3W3AetXUhPHTMZa7d2J7JEUj/iMJDZ/43nT4SskASNu3u+9IYKR8HPPQp4pmsfCnF2bcFqCYty5gGbDSdJmeu23op38vJdWJLUAoXKsgnp2tg7MjrLw+smvXdbYXHKRzEhlvnKtnOvs0OhsHbHLBTVMiujrfJngsJJHdzMVtwY8rkCYlRqvCkI4Y+vaBsvp7XmjgtKm3dz26phUCkwPzCwCh7qwvQebbnCYR0BhmaLZL26ex4BDKmuhHwJ4ZPkQfZp/O3q+9VsK8RZiwq23V//48tOkwy6vYoNvXHCTK/08tkbfmJxNcv8Y4GO7oASonXhiPdE55DhtPACRlVClixwaBpeWaw09Le09YexfhIc8RgKmuyXwOLGkGkDvVN4kF5MX1un++KduI320syIrbrvNhEjsMSN93sxyt+HlDA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" Dynamic ftrace must allocate memory for code and this was impossible without CONFIG_MODULES. With execmem separated from the modules code, execmem_text_alloc() is available regardless of CONFIG_MODULES. Remove dependency of dynamic ftrace on CONFIG_MODULES and make CONFIG_DYNAMIC_FTRACE select CONFIG_EXECMEM in Kconfig. Signed-off-by: Mike Rapoport (IBM) --- arch/x86/Kconfig | 1 + arch/x86/kernel/ftrace.c | 10 ---------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 982b777eadc7..cc7c4a0a8c16 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -35,6 +35,7 @@ config X86_64 select SWIOTLB select ARCH_HAS_ELFCORE_COMPAT select ZONE_DMA32 + select EXECMEM if DYNAMIC_FTRACE config FORCE_DYNAMIC_FTRACE def_bool y diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c index ae56d79a6a74..7ed7e8297ba3 100644 --- a/arch/x86/kernel/ftrace.c +++ b/arch/x86/kernel/ftrace.c @@ -261,8 +261,6 @@ void arch_ftrace_update_code(int command) /* Currently only x86_64 supports dynamic trampolines */ #ifdef CONFIG_X86_64 -#ifdef CONFIG_MODULES -/* Module allocation simplifies allocating memory for code */ static inline void *alloc_tramp(unsigned long size) { return execmem_text_alloc(EXECMEM_FTRACE, size); @@ -271,14 +269,6 @@ static inline void tramp_free(void *tramp) { execmem_free(tramp); } -#else -/* Trampolines can only be created if modules are supported */ -static inline void *alloc_tramp(unsigned long size) -{ - return NULL; -} -static inline void tramp_free(void *tramp) { } -#endif /* Defined as markers to the end of the ftrace default trampolines */ extern void ftrace_regs_caller_end(void); From patchwork Mon Sep 18 07:29:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388981 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED1D6CD13D1 for ; Mon, 18 Sep 2023 07:32:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 877666B0290; Mon, 18 Sep 2023 03:32:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 827AB6B0292; Mon, 18 Sep 2023 03:32:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C8E26B0294; Mon, 18 Sep 2023 03:32:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5CA616B0290 for ; Mon, 18 Sep 2023 03:32:15 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2877B140BD7 for ; Mon, 18 Sep 2023 07:32:15 +0000 (UTC) X-FDA: 81248899830.05.22A94D8 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf27.hostedemail.com (Postfix) with ESMTP id 759DB4002E for ; Mon, 18 Sep 2023 07:32:13 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kT3NctsE; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf27.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022333; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dqCPvIMXOTJlhRnOJhYfwq6l5xz7OPueXxaVIBOdmGc=; b=HOABFERSYY+j5/fVjXvfsoYophbC5Vf3unX8Ac8H/QBHLPVIF8ayaS4GhR7C9KXv3jDhT+ hzi+Xj02hc5Zwmwk75vhvNwirxA+2gXSrkKSApWLPAReZVfXPNw4qZpwPQYa+uXd6jgJsF R3nyWiKNl5mGFt//b50Ti0RmiT3VcBk= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kT3NctsE; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf27.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022333; a=rsa-sha256; cv=none; b=Mp9xNGeeaDRV1ALZRMbggKE82beLy1/51jdPest2ACyIdPGLNC/HqYSYacJfL94Fd9STCg CIijZIDpfVdjotDWF1CFSC1M+yepl1PphVkyn9RwXkg3iHlVvVSeIKhYpkuwAGlneIgMMR wzWTlzflbFaRsiQkPJxmzZP3LV8QtkA= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B68D660FA0; Mon, 18 Sep 2023 07:32:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B212CC433A9; Mon, 18 Sep 2023 07:32:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022331; bh=g2ysQzPrupxvZBEk8R4nz6nGqIwxox2mdTADaK2KSBo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kT3NctsEzR46ZMcvQmZYEzpOY6ldmIA+UpjJflzhcN6xMD3eDfrH5TshpxPSzqYKZ 4RtqZhzZqqMGjQFg0TiMJ4pIA+4pgayZMlfPQIIGjFZSqPjnRRF0/SACLSULT/WzJX e5w4J3wzseysRp3HjruUqlEklzGHP8/6ICjr9417gwdOyxSuUT0n9ca5FaDmjgMb4N wh+TlKoXNolf6tuh2rWFnkZ3l88wMsKsT34tZyIMrsdUBBtjJC/AKV44QLF3klepDN XUU8NCM4DCe8D1KxoJHEl8Op3szJMcdX1IE3cH+lAoAp79l+6uCu78MbUgn4aNR/c+ j8TJxIqTZhi6w== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 12/13] kprobes: remove dependency on CONFIG_MODULES Date: Mon, 18 Sep 2023 10:29:54 +0300 Message-Id: <20230918072955.2507221-13-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 759DB4002E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 7kqwe6p5x3ckpr8w9q9yxwa71d1549xp X-HE-Tag: 1695022333-256426 X-HE-Meta: U2FsdGVkX1+BBUlQ/udIjdXmNkoIttxJ8dIhB2SSH5OPzUzAcYc64YmmTc1IDv19R+n4k1N9IrR8fpz9XQCsKR4Di+EBE+A34pBo/La3bAeoY+eqH7KwluAVAzthnAkj1QE/fBWAO0UTTXVsgnpEax5fTSwE6FXo6FzPPX43QqZ/qYsKy9M0StXvmcgchRkI72xEZECu/OdUDnqMqoywfRJAxdFf28lACq4jJovfepmbhEnicv27wEIN9jlj8uVNBm6AGmy2wJoNcTAr8jiJfy3emPx3vltpYMuz8YwNrpPvhxkMqxQBEzIvPvkpcw7rRNTOhZuLn+Uv2aLMzQWFv9esTUNtER8EwfYDK86orEpBISbzuyC/VZQUBxTiinJ3qOkphUtruQA/ZAoWwhnMALVFCt+st3Ye483/rR2NdPUjllZrdHTEIf1PZ+rxUxjfeVlJlFb40GiInCq2Cu60peHCcRSZqudCWnxYCUs1TUuKw21NcDwX4nyUdCcZXoRY0Pt8ur/Geev+LomKaI5gW2JS1/rImnRr5DwnkKubkp2/wXXk/MKBMr7QR/iN1/BR9vPL5Pg6dgg1TfHUoF8M4gar7OXuXKrHSTFhJrn6XFHuoWMyCp0oJ17MYQs4RwYFJlZZLG/wW99ZDYoKS8S+dQMhysXOg651O5/7EGa/0vTiFyE24PzzO89PNL6p4tI/68wUeDY1BaFyA9S8naRUmVR/kDndM5xI+s09rrm98c1Sh4TqcHko/BFWR5XEkcEKW9KkoiuwkiDA3jtYCi7LV7WnegM5d1UTfSj6r8Fo2NIgk6aNhasU4D7LZ6Yv0xIRzjSnhGjaMs71xrTyCHlG2QHNO8HN2xw34YmQS5UuAonO4S+8O5Qvi1NwixPoyvLVgX6qxB02ohnJbCR/jBsrPvCvlIP29R93WGGkIAWuGp30mbiNvKhIyXDDiudXKompAI7D3pQxMU1nbOdwKkE I7LJvSq/ tMzhw/KjCRVfr5KDi94RD0yH2uF9wFaIUb2BbRuuV9z51G7tbXk67NJ9p/eUP2vV7EFf6zVqZe59EWO9mUOfXM2PxjTUxILQTFHrU7IvM1gOyDUwS5w7RzurtT8/Osn7pGbYGGa/8LPQZXEONuBTRcSCbzc/ifDhPSCFcWl/cWjSqbG0+vAMkfHSnPgRleVamFc20ocsFMz+Nl5jGyh78V4XN1TGGiYP9napCwYW4S2LKP1bs4+DW6OIFZjnGDjGx5IBPYR3IkurczsWOyhONR02FrHXNUgIrdK3iFJWkxgYzxHgU1U9miNd3DaR7xztZLf5aWsLNy9Is0tyADTXi5FfskM93r3UN89fpBhDmXvzMDQutRmzH7rB85eg6wYKHoJmk4kWL58WE0OnPtM6OilUbNn4f6sMCs4Cw4rrt1iYajaMSbTCd4dbSKmjO840QuwXi3Asz7Us6/1A= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" kprobes depended on CONFIG_MODULES because it has to allocate memory for code. Since code allocations are now implemented with execmem, kprobes can be enabled in non-modular kernels. Add #ifdef CONFIG_MODULE guards for the code dealing with kprobes inside modules, make CONFIG_KPROBES select CONFIG_EXECMEM and drop the dependency of CONFIG_KPROBES on CONFIG_MODULES. Signed-off-by: Mike Rapoport (IBM) --- arch/Kconfig | 2 +- kernel/kprobes.c | 43 +++++++++++++++++++++---------------- kernel/trace/trace_kprobe.c | 11 ++++++++++ 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/arch/Kconfig b/arch/Kconfig index 12d51495caec..c52a600b63ca 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -52,9 +52,9 @@ config GENERIC_ENTRY config KPROBES bool "Kprobes" - depends on MODULES depends on HAVE_KPROBES select KALLSYMS + select EXECMEM select TASKS_RCU if PREEMPTION help Kprobes allows you to trap at almost any kernel address and diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 0ccb4d2ec9a2..c95d0088f966 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1580,6 +1580,7 @@ static int check_kprobe_address_safe(struct kprobe *p, goto out; } +#ifdef CONFIG_MODULES /* Check if 'p' is probing a module. */ *probed_mod = __module_text_address((unsigned long) p->addr); if (*probed_mod) { @@ -1603,6 +1604,8 @@ static int check_kprobe_address_safe(struct kprobe *p, ret = -ENOENT; } } +#endif + out: preempt_enable(); jump_label_unlock(); @@ -2495,24 +2498,6 @@ int kprobe_add_area_blacklist(unsigned long start, unsigned long end) return 0; } -/* Remove all symbols in given area from kprobe blacklist */ -static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end) -{ - struct kprobe_blacklist_entry *ent, *n; - - list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) { - if (ent->start_addr < start || ent->start_addr >= end) - continue; - list_del(&ent->list); - kfree(ent); - } -} - -static void kprobe_remove_ksym_blacklist(unsigned long entry) -{ - kprobe_remove_area_blacklist(entry, entry + 1); -} - int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value, char *type, char *sym) { @@ -2577,6 +2562,25 @@ static int __init populate_kprobe_blacklist(unsigned long *start, return ret ? : arch_populate_kprobe_blacklist(); } +#ifdef CONFIG_MODULES +/* Remove all symbols in given area from kprobe blacklist */ +static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end) +{ + struct kprobe_blacklist_entry *ent, *n; + + list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) { + if (ent->start_addr < start || ent->start_addr >= end) + continue; + list_del(&ent->list); + kfree(ent); + } +} + +static void kprobe_remove_ksym_blacklist(unsigned long entry) +{ + kprobe_remove_area_blacklist(entry, entry + 1); +} + static void add_module_kprobe_blacklist(struct module *mod) { unsigned long start, end; @@ -2678,6 +2682,7 @@ static struct notifier_block kprobe_module_nb = { .notifier_call = kprobes_module_callback, .priority = 0 }; +#endif void kprobe_free_init_mem(void) { @@ -2737,8 +2742,10 @@ static int __init init_kprobes(void) err = arch_init_kprobes(); if (!err) err = register_die_notifier(&kprobe_exceptions_nb); +#ifdef CONFIG_MODULES if (!err) err = register_module_notifier(&kprobe_module_nb); +#endif kprobes_initialized = (err == 0); kprobe_sysctls_init(); diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 3d7a180a8427..25a5293a80c0 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -111,6 +111,7 @@ static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk, return strncmp(module_name(mod), name, len) == 0 && name[len] == ':'; } +#ifdef CONFIG_MODULES static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk) { char *p; @@ -129,6 +130,12 @@ static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk) return ret; } +#else +static inline bool trace_kprobe_module_exist(struct trace_kprobe *tk) +{ + return false; +} +#endif static bool trace_kprobe_is_busy(struct dyn_event *ev) { @@ -670,6 +677,7 @@ static int register_trace_kprobe(struct trace_kprobe *tk) return ret; } +#ifdef CONFIG_MODULES /* Module notifier call back, checking event on the module */ static int trace_kprobe_module_callback(struct notifier_block *nb, unsigned long val, void *data) @@ -704,6 +712,7 @@ static struct notifier_block trace_kprobe_module_nb = { .notifier_call = trace_kprobe_module_callback, .priority = 1 /* Invoked after kprobe module callback */ }; +#endif static int __trace_kprobe_create(int argc, const char *argv[]) { @@ -1810,8 +1819,10 @@ static __init int init_kprobe_trace_early(void) if (ret) return ret; +#ifdef CONFIG_MODULES if (register_module_notifier(&trace_kprobe_module_nb)) return -EINVAL; +#endif return 0; } From patchwork Mon Sep 18 07:29:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13388982 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A48B1CD3423 for ; Mon, 18 Sep 2023 07:32:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2366C6B0292; Mon, 18 Sep 2023 03:32:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C0C46B0295; Mon, 18 Sep 2023 03:32:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 087D06B0296; Mon, 18 Sep 2023 03:32:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id ED0B36B0292 for ; Mon, 18 Sep 2023 03:32:24 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C4D36120BC4 for ; Mon, 18 Sep 2023 07:32:24 +0000 (UTC) X-FDA: 81248900208.09.E8E292C Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf09.hostedemail.com (Postfix) with ESMTP id 26E8A140007 for ; Mon, 18 Sep 2023 07:32:22 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Vnbzf3Qh; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695022343; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hpRvplrQS5IqpGgGr3tvfF4hOei026V7oQnE9P+m2j0=; b=QZs2lXMITSP4H0WxEo8k8I7uqs9mcaTH0F+984kKREEAt+nvjV9SOkKw8IMekmu5JkN88L MOiLJJwt6yhyDUeYBNvEgswYfXdE8PyXVXku1gI9VErWPLhUJwLtorTKfqDYR/Jh58pOd6 H+zHRtNif3brt0hZjFY074HnSpvET5s= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Vnbzf3Qh; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695022343; a=rsa-sha256; cv=none; b=agfnUnAj1aYIDsQHS+qwIfPmPbSOiCOLtgl810wZW6q3rh61K8tSanbLu7zT5KomVc9cAx YSZsLWVGdH/cUU8jU29s5MWaQgWsCDueqQbVvqzeyiOGOkPR6EW21WmFa+CnUsu8ofKkyH fpNK3VLtlj3BFi5vRNEFvQPRyPeNwIw= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5D75D60F9A; Mon, 18 Sep 2023 07:32:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6BC40C433B9; Mon, 18 Sep 2023 07:32:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695022341; bh=07Jp3LphXzl4EkHja0JdIUA/xjHpNGiZuguIXloPztM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Vnbzf3QhQuVmwYQAf2Mt0WSZBefd5lhLSFtkXhwMC+n+X7N4ki+6T0boDwsgMr0XN AAdXg1yYsIa/lFW8JGjF+xmiLYY63eHqaMwytJIlEVwOkPrMrojuYJZhQpE797YZju GZcg8J0n+fEArc0GhUtiTV4LpXaeVrImgv2qDiS2pZ01DSA9wJwPYqPfL7KPbaX6bE QYQH9HSRdQuGBz3raOjQKyLrYnfzs7cZoMWOYQ1LzctZYuLHDNxMU/K31vWL6RTmKL zi7xoAjilllhk4DMDBlwvUkOVzwZl6SwuVwOf6rk+tbCRJ2s6/D0PIr6IMDdrgSqtE mxvGlqxn371yQ== From: Mike Rapoport To: linux-kernel@vger.kernel.org Cc: Andrew Morton , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Catalin Marinas , Christophe Leroy , "David S. Miller" , Dinh Nguyen , Heiko Carstens , Helge Deller , Huacai Chen , Kent Overstreet , Luis Chamberlain , Mark Rutland , Michael Ellerman , Mike Rapoport , Nadav Amit , "Naveen N. Rao" , Palmer Dabbelt , Puranjay Mohan , Rick Edgecombe , Russell King , Song Liu , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Will Deacon , bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 13/13] bpf: remove CONFIG_BPF_JIT dependency on CONFIG_MODULES of Date: Mon, 18 Sep 2023 10:29:55 +0300 Message-Id: <20230918072955.2507221-14-rppt@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918072955.2507221-1-rppt@kernel.org> References: <20230918072955.2507221-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 26E8A140007 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 8493stk45qj1p3ixtukoe45nrjbxmmor X-HE-Tag: 1695022342-966688 X-HE-Meta: U2FsdGVkX1/bqTqB6SrYp6g8x2NGm54lTyO2TPVN0+Lc0Qo3uEuJd3DeCBDw4atMzYMm6bt8GKmB3o/yRNZf38NIpNCnA4CRw4fKArJezYDHGeus+/Flf8iLq9HC3IfuPM7dXxHif23stir+wyc+oQ6G6xAzuaWYlLfJxgtEIwom17X+jdD2RPbVi72yRLL2Xks26Bt9TCV7jCkD9oEwlBVcyN0pZ23ufyWTUNt0rHx2gnWN9OggeGlhyNlYNbueojll0W0Ta4Amk5TnsY7Om3FlGzyOqrBFt+y4yF1ZVbpGEgZxNnjwMM9Dmf42BSRjY+ZvgUqovQ8BjqxU7b7rMSIhmeG+Gmjtz9WvIEFh36PwyRl7jUOXg8xcO6jygis97n5xBoKqqRQzFR7CErepUVEKX/Tocsx5ou1PS//vlp+OEcdyX2FJwK9BvGVAMjT0S2XkG4II9lo7ZoiJR4Mn38nRJmkq/S5f5ckhYRreSf+Ziejtm9DH5LgZMY3w3euxbZZ5kLGIOrmjNlt8zO7TOZzte4wzYmJn1tIavweFFGLXP0x6JN5MajOwSMAH2IXlvsxWxtm0S13FsTqIDl4+6vS0Crarpp5tmlrOwV6AVZ9G29D71J4SMO0besWYspyMpiD86QA3JZCCdrzIZ83gvPdCfGIhddnK4GX0sRRTwMZFdY+rXfG7Zigsumkx1DLSVUGTBNhpWcvSKS165gtGYuhIWiRor/GCzREeXUiZkfVrSi8OwcqJjAs+ZzOdQJRQ3fpjYaczXZ3ajYAJj4AgDRWlPVoh3pSimTxkWGbHtVIP7lB+3coJrE2+C0JtDbtnGbDfd8nMNsGumegQPpowN0Z7LmT4yd/FXcYCV9ZiZ51+Fj8GmhXF6T2zCwNboJEYLQkf/XX73d7V1xWC0v21G2KaTO7I0WIJXEXA/KGrGpcouPapEvzXWT52OZu2Ldtym3Eb/LvH0/tPYvTMUU4 7VZSvHjp 5dNW3hjqCadW1hk3W+klKNAz0XlHa1Vl8wwFyww0wt44qQFTveLYr8tcliaUZFITCMO3345We26WsIXtNxj/+ANS8stTQ52qxvQP3xyk6L1mw/cr0OYtJ4XjhrzfSmfjroIKvD5YkAvB/N31NEBynkMNbsb4bNKXILfvWZpmSv5SOVp7szSiuyUF7t9PSVe8m30yE6lBYsrVnhZ77HMI8Byk49UWrpwSsGnv7Q58duVybyoZvzPb1ohh5W0OHHyaQkYQXaoayc3F0ZBlKAXsvV396u9CDRPprtBZpHK2TsHwjaZSms4DhMqJ4SVT0zYxpr0CHGD2F5vhkOJUhU97Q0udde2NNxk6x/JknXaFAUBAJHzO0rzaciMEtRfMhXjeGBuOn59SXSyMRLUtBlWacjDgErNo/hUpbvjfCpqyLsBHTf/v0l9mQK5AsSPe1nN4B5qbbp3UWDBZIDAxwXPfXNp+4Ng== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: "Mike Rapoport (IBM)" BPF just-in-time compiler depended on CONFIG_MODULES because it used module_alloc() to allocate memory for the generated code. Since code allocations are now implemented with execmem, drop dependency of CONFIG_BPF_JIT on CONFIG_MODULES and make it select CONFIG_EXECMEM. Suggested-by: Björn Töpel Signed-off-by: Mike Rapoport (IBM) --- kernel/bpf/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/Kconfig b/kernel/bpf/Kconfig index 6a906ff93006..5be11c906f93 100644 --- a/kernel/bpf/Kconfig +++ b/kernel/bpf/Kconfig @@ -42,7 +42,7 @@ config BPF_JIT bool "Enable BPF Just In Time compiler" depends on BPF depends on HAVE_CBPF_JIT || HAVE_EBPF_JIT - depends on MODULES + select EXECMEM help BPF programs are normally handled by a BPF interpreter. This option allows the kernel to generate native code when a program is loaded