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 307BEC8303D for ; Fri, 4 Jul 2025 13:50:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C1B6B6B00D7; Fri, 4 Jul 2025 09:50:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BCE9F6B02D5; Fri, 4 Jul 2025 09:50:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ABB196B02D6; Fri, 4 Jul 2025 09:50:06 -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 981D36B00D7 for ; Fri, 4 Jul 2025 09:50:06 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 65C3014029F for ; Fri, 4 Jul 2025 13:50:06 +0000 (UTC) X-FDA: 83626716012.16.DB82EE0 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf22.hostedemail.com (Postfix) with ESMTP id D7713C000B for ; Fri, 4 Jul 2025 13:50:04 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=du9mSzGa; spf=pass (imf22.hostedemail.com: domain of rppt@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751637004; 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=1ZdZoM8sqKOx2fOVEEQg+F8E6z8+9cnEFDhaqowvAAQ=; b=sQPbLICDI/1hw47RXcVKRSOUduh/2Gbo/NgsgoCKUHmgaIufMrvs11gUlf5BowTlYSWIBW 3bLacNp3wnAJbH59Zqnq9/xyp1ukVR+7HjuWNqKyYxt9f6sQVb8Sa5VOkFx0ZSXFOVGpZB 7rsPGhWCMt4nYNm116U1Q5FMs3Taw4U= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=du9mSzGa; spf=pass (imf22.hostedemail.com: domain of rppt@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751637004; a=rsa-sha256; cv=none; b=f8mbre6pkAcLUzBTh4vpFFm2Y9IvibBMacOvnC+jG8KScapgIW1mGlq/t/9YuX5cZKGBUZ LxzNloI3FFDfqq//I0wVJ4bgRtmdyU+yvkxTrdEqiQZBtUwVpqgcbq94gKkaoUXGSZbHCA j5UZXqy27x5KC01CjO4AJ91dhCSck8s= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id D0F5161454; Fri, 4 Jul 2025 13:50:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 79C19C4CEE3; Fri, 4 Jul 2025 13:49:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751637003; bh=NrvBoIv7ihWz3msr5ucNgQMNcZ4XLlRn/Kum2bPi/3o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=du9mSzGaCObmMu4AJfvOoggVAr+IvAH88kHSLClKN/Mfwta17SYcXTn/8LmevqoGx AcvO1HVvuNnHtYa2S3cTeJpP/UAadzyjKs+bueQhrshFpa73GQXfrPqjv/3xZAl5oJ ZuHXPvfRdopM8RuzgkMy1C4gekeUHMofMNM79RXoQkBZ19n34qDLy1rE3DU/u18JZ9 ajmTCNtxJpyfh9oBrBWU0N1CsaKDxgBBnnqkQUC5tMFeaaxSfXsni9oXxgiycthABl whmMPdoAKby8UIZBKLXZUi6axYTd9YPeIGG9ZyGSxn9RkmCzQgrPDQawnEKDvsGZVC 0+BAZ5kPr6WrA== From: Mike Rapoport To: Andrew Morton Cc: Andy Lutomirski , Borislav Petkov , Daniel Gomez , Dave Hansen , Ingo Molnar , Luis Chamberlain , Mark Rutland , Masami Hiramatsu , Mike Rapoport , "H. Peter Anvin" , Peter Zijlstra , Petr Pavlu , Sami Tolvanen , Steven Rostedt , Thomas Gleixner , 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 2/8] execmem: introduce execmem_alloc_rw() Date: Fri, 4 Jul 2025 16:49:37 +0300 Message-ID: <20250704134943.3524829-3-rppt@kernel.org> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250704134943.3524829-1-rppt@kernel.org> References: <20250704134943.3524829-1-rppt@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: xximsqagobd43gas5h9obw1ubtd9wzub X-Rspamd-Queue-Id: D7713C000B X-Rspam-User: X-Rspamd-Server: rspam07 X-HE-Tag: 1751637004-478929 X-HE-Meta: U2FsdGVkX18gkJtpHSTOK+bKr+2+9IpWE2OIHkTFijBjMCzyQYx4nKclsMo6C5FY5730F3r+e5lOpLpjqjrzZZMCEgXn7SqT+nPdGeGI3juo0N6o8KGxDxTycFKHaRkY24VrnAezUAyVIOoCeVej28a97OttlaopOBL3UBeC6Q3ZqQkLdf/s8oApZBj0jM4fXAn8f70ZqJcPjllZQY63W+GBgklHuD3/QloUpC3o/sl2s7xUJzg3aUT0aW7MGqiwv+3haEHWddMpXKAp5xKXMCWT8n2+l2hgp7SQ4UPVr8ZyhMkCK3smzv/Yj0ydQPIXylv84+AGNT9MftFOGp4hOewfHu2Dn311IXVrnwg6Gsee7EfaxC/X9xlreB52Dn9JIsXu4nw780fife8qpqXJXGcgsgoSNIoKqvFFvQY/RcJ1FGSHTuFiSi9nIcb+3gFAOjEeyNStNhuLZ73bw6d+PxhYfhjHUcN0M08ATlq46Y5UDNfSL2YynlEXRak3dtxu8nhQxpT2WGZqcuKooei94OwvZGHb7wb78d2vYRfO9Pdx/Jj+81gREvHw2kkM60UO1UOZq2pRRycjgxU16zOgQfZn1LE4BakupPmmKSw/cCmWJsl/lKfaPgSbd6I13LD/JeBHCpoXoc1odj4TDfXwyAzufnraLfuquCwDbaqziauhK+iUJWAEQRr+RfsqvOR7eQaU1+C0hF+kOqTH9a6lDK7wYkNZ9wuNfG3ZU80M4DSFcDOjdaE2BNPp0AEoSuh4jX+VN6R3UMcp9stnf5hk6XPuWJFg9akf50e6XLey2wRuO1eQsp/FcYojX5T0/YtTBiRawA7lgv2E7lkWBoQFOQbB8FhcOAr8kjL355rVeJCxP8OleGzgQgNvjdavHGjysKjxaa7ogbP5I0uumCnxfTxx/kHK3qhmDFxeWXRDcSHi7/CnL73GyuZ5QiGfoJgK7Jo8fpcG9q11wSMHBNp 0nmGqTRB /8qeLH0rxruov2NP43QBW8lyOY2mEYW3+leJTM7JbNYVzf41fmF1VC2YjIZOCOkrSD2Mx0jUrvcgFJ+aZzjUQjuAvnvODDtN4hDKP9vaH8nlh0C6SHZig1fq+46mpaypslvZT2TEZQm+J1xJEEtWOYK5Itr8B9eoQW0vfq2+YqX3uC+k4s6BhFJr2nyhQFcUn8Fel10Aa6UU2DRkq50R0sKiu0h+f1F7/TDDRCvKexQMTzDTA7dUimatrrwVBT/d7GmhMhRJjUvXQO62AMFs4TjB7ww== 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. 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..4e510d1c609c 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 writatble 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