From mboxrd@z Thu Jan 1 00:00:00 1970 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 7253BC83F1D for ; Sun, 13 Jul 2025 07:17:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE2A26B0093; Sun, 13 Jul 2025 03:17:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E93146B0095; Sun, 13 Jul 2025 03:17:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D824B6B0096; Sun, 13 Jul 2025 03:17:55 -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 C78316B0093 for ; Sun, 13 Jul 2025 03:17:55 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 46A7EB852E for ; Sun, 13 Jul 2025 07:17:55 +0000 (UTC) X-FDA: 83658386910.17.EBA9363 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf05.hostedemail.com (Postfix) with ESMTP id B3A2E100002 for ; Sun, 13 Jul 2025 07:17:53 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kXpza8uf; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 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=1752391073; 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=5oKzlo8titjaohvpjvlNmkUQ+QxHjx53tLTXbNO0gSY=; b=F5IbjITL8V+O4cGzp7zSwBLbAP7iSQ+yvyIG3HkRdx/hkQY+bmGggim30WaA9fxvRR/zyy FgkEQgfVdUyR4JSa2bpQOQD5Q/5FQ2roAOWVxZBhBjz/g7PCoqgc5FtYQmItlv0S9Xg/k/ hdktHMESFQXL3wHsPB9hxKRfFSD3gA0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752391073; a=rsa-sha256; cv=none; b=iBpad0RYNk8UL65VD0Mx/bq52xWPbwHQ0U7aWzqNnRsa2M7cNFEmJMw/3OQNADqs4Mx0vi vPmqCWQyUNUdS9tdF79T0a32WHHBHay41VqwhdXKElI+Shl2H+3CJM82040D7zJRPtYvF4 UBKVax9wwvdd9RCmKImKE18fpzkmrM0= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kXpza8uf; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of rppt@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=rppt@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 0162EA4B5CE; Sun, 13 Jul 2025 07:17:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E6C6AC4CEF9; Sun, 13 Jul 2025 07:17:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752391072; bh=3dUBpvEhYnlflMVnyRwOxMHiJIQysIRUBLUfy0VHZwY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kXpza8uf1fpZTHSm35hPSgJxQLhzXrdXpH/RX0ji3Z1XguEuWUQ9JT/jx8CqQAZEl 7HSZz/Iq2CY2Fe1t5oS3TPYKeZ8v93bV5hK4tDYwRl7VVq03+XCmcv89gctSoJ1Vkq QdWcpJsQ5TQZYb23jqSLscMepwHxYvO8QHQI8xQ58e7rTS1wIimyRqgYzrPUKGF54C yBsnVy330Hp7e87+oJuxNp8rPyLFe+43pcFfqiK/hYbpWZv4YjSOfMiR+Usc/aqbQB RPcsI5rnUTl5B/5/al4NEX9Fl5x7Fj3Q5zBaqUSwisYxBQRJHWtLUz8PV56DlAiahL PWlIIXbzMDwkg== From: Mike Rapoport To: Andrew Morton Cc: Andy Lutomirski , Borislav Petkov , Christophe Leroy , Daniel Gomez , Dave Hansen , Ingo Molnar , "Liam R. Howlett" , Luis Chamberlain , Mark Rutland , Masami Hiramatsu , Mike Rapoport , "H. Peter Anvin" , Peter Zijlstra , Petr Pavlu , Sami Tolvanen , Steven Rostedt , Thomas Gleixner , Yann Ylavic , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org Subject: [PATCH v3 2/8] execmem: introduce execmem_alloc_rw() Date: Sun, 13 Jul 2025 10:17:24 +0300 Message-ID: <20250713071730.4117334-3-rppt@kernel.org> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250713071730.4117334-1-rppt@kernel.org> References: <20250713071730.4117334-1-rppt@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: B3A2E100002 X-Stat-Signature: uoo7m5ws34ekty8g7e7qcwmspgooctpz X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1752391073-684792 X-HE-Meta: U2FsdGVkX1+YmAeGLvTzvDGfY/ixFgSC+DvdvL014a4gvX/bSo4cnMKZF7h7QhqkCnNaRov/FnjIz2WN+/ivhDuvCa5/W8Cf5MF4NFhmF+ZXUk75qGOQtl9LR4d7v3QdzvJqDMbvxd/pByTHVaVbo0gGG6QQlWHovxF89LNCJLECoLEZ5R+zxgW+GzdSa1/NHmywMponZK0UfDY2eNXtPSD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fl+D2vAQi/ooybNmGGw3f+5QyD9OL3EuH4gw3s3hSXsopFVfSzZDjkUYINrHggz8vjHVEx8AFmJed94ATUh9wgp8fZKHgLFkHtYmU9H66WV/jJQcVrR82ZqEdcxerQ6Sno3BFo3ALIQ6VFHDOJaBstoWLWWGzw41sPobxH9lIH2xAcFUdKczdw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Mike Rapoport (Microsoft)" Some callers of execmem_alloc() require the memory to be temporarily writable even when it is allocated from ROX cache. These callers use execemem_make_temp_rw() right after the call to execmem_alloc(). Wrap this sequence in execmem_alloc_rw() API. Reviewed-by: Daniel Gomez Reviewed-by: Petr Pavlu Acked-by: Peter Zijlstra (Intel) Signed-off-by: Mike Rapoport (Microsoft) --- arch/x86/kernel/alternative.c | 3 +-- include/linux/execmem.h | 38 ++++++++++++++++++++--------------- kernel/module/main.c | 13 ++---------- mm/execmem.c | 27 ++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 30 deletions(-) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index ea1d984166cd..526a5fef93ab 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -120,7 +120,7 @@ struct its_array its_pages; static void *__its_alloc(struct its_array *pages) { - void *page __free(execmem) = execmem_alloc(EXECMEM_MODULE_TEXT, PAGE_SIZE); + void *page __free(execmem) = execmem_alloc_rw(EXECMEM_MODULE_TEXT, PAGE_SIZE); if (!page) return NULL; @@ -237,7 +237,6 @@ static void *its_alloc(void) if (!page) return NULL; - execmem_make_temp_rw(page, PAGE_SIZE); if (pages == &its_pages) set_memory_x((unsigned long)page, 1); diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 734fbe83d98e..8b61b05da7d5 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -67,21 +67,6 @@ enum execmem_range_flags { */ void execmem_fill_trapping_insns(void *ptr, size_t size, bool writable); -/** - * execmem_make_temp_rw - temporarily remap region with read-write - * permissions - * @ptr: address of the region to remap - * @size: size of the region to remap - * - * Remaps a part of the cached large page in the ROX cache in the range - * [@ptr, @ptr + @size) as writable and not executable. The caller must - * have exclusive ownership of this range and ensure nothing will try to - * execute code in this range. - * - * Return: 0 on success or negative error code on failure. - */ -int execmem_make_temp_rw(void *ptr, size_t size); - /** * execmem_restore_rox - restore read-only-execute permissions * @ptr: address of the region to remap @@ -95,7 +80,6 @@ int execmem_make_temp_rw(void *ptr, size_t size); */ int execmem_restore_rox(void *ptr, size_t size); #else -static inline int execmem_make_temp_rw(void *ptr, size_t size) { return 0; } static inline int execmem_restore_rox(void *ptr, size_t size) { return 0; } #endif @@ -165,6 +149,28 @@ struct execmem_info *execmem_arch_setup(void); */ void *execmem_alloc(enum execmem_type type, size_t size); +/** + * execmem_alloc_rw - allocate writable 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. + * + * Allocates memory that will contain data coupled with executable code, + * like data sections in kernel modules. + * + * Forces writable permissions on the allocated memory and the caller is + * responsible to manage the permissions afterwards. + * + * For architectures that use ROX cache the permissions will be set to R+W. + * For architectures that don't use ROX cache the default permissions for @type + * will be used as they must be writable. + * + * Return: a pointer to the allocated memory or %NULL + */ +void *execmem_alloc_rw(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 413ac6ea3702..d009326ef7bb 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1292,20 +1292,11 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type) else execmem_type = EXECMEM_MODULE_TEXT; - ptr = execmem_alloc(execmem_type, size); + ptr = execmem_alloc_rw(execmem_type, size); if (!ptr) return -ENOMEM; - if (execmem_is_rox(execmem_type)) { - int err = execmem_make_temp_rw(ptr, size); - - if (err) { - execmem_free(ptr); - return -ENOMEM; - } - - mod->mem[type].is_rox = true; - } + mod->mem[type].is_rox = execmem_is_rox(execmem_type); /* * The pointer to these blocks of memory are stored on the module diff --git a/mm/execmem.c b/mm/execmem.c index 0712ebb4eb77..6b040fbc5f4f 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -336,7 +336,7 @@ static bool execmem_cache_free(void *ptr) return true; } -int execmem_make_temp_rw(void *ptr, size_t size) +static int execmem_force_rw(void *ptr, size_t size) { unsigned int nr = PAGE_ALIGN(size) >> PAGE_SHIFT; unsigned long addr = (unsigned long)ptr; @@ -358,6 +358,16 @@ int execmem_restore_rox(void *ptr, size_t size) } #else /* CONFIG_ARCH_HAS_EXECMEM_ROX */ +/* + * when ROX cache is not used the permissions defined by architectures for + * execmem ranges that are updated before use (e.g. EXECMEM_MODULE_TEXT) must + * be writable anyway + */ +static inline int execmem_force_rw(void *ptr, size_t size) +{ + return 0; +} + static void *execmem_cache_alloc(struct execmem_range *range, size_t size) { return NULL; @@ -387,6 +397,21 @@ void *execmem_alloc(enum execmem_type type, size_t size) return kasan_reset_tag(p); } +void *execmem_alloc_rw(enum execmem_type type, size_t size) +{ + void *p __free(execmem) = execmem_alloc(type, size); + int err; + + if (!p) + return NULL; + + err = execmem_force_rw(p, size); + if (err) + return NULL; + + return no_free_ptr(p); +} + void execmem_free(void *ptr) { /* -- 2.47.2