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;