From nobody Tue Oct 7 22:18:34 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2BB1A2877C7; Fri, 4 Jul 2025 13:49:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751636998; cv=none; b=l1N4EjK0lDJevVvnQodlAvwyhI9Omk3AKbLRNzoBMJjpvR8E0Vpxjd2S46Do4+ulHDzsH2BB1QNpHDtJyNOidvQ0l2PYL0kRazill2NiiVsFzAZ3bwGT3EiTYyRTTeKrGr3TKI0dFFb3qr5mUUC4PRyKtTlojXVKDfxWTFh93/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751636998; c=relaxed/simple; bh=vyVIGCtc0jVROOiaoRDc63+XloT/k7Y60usHpVt467Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uVN8bQJH6RToQI92JSNKizkQ5QlrnDMYPGNJf/2Yn4KskoJLiTV/cCZseHXFfrLeWC+KMmyF6P5FxYxRTUsxW4Hv6zo41MS0lZJSWHHPkLOubHR1GYVWQXtwxiTYTiRLdVZ4bz0Lyspbr2K/gfG0hGZ2e1yF4KCEgLRqJQ+jycQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QeBTJcLv; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QeBTJcLv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 047A4C4CEEB; Fri, 4 Jul 2025 13:49:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751636998; bh=vyVIGCtc0jVROOiaoRDc63+XloT/k7Y60usHpVt467Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QeBTJcLvOcdLQq7prI/RVYQdCGmN/+LTcJOHYLGUX8iY+VcLj1yRa98RNlHOzXWEc ahn08iR5xHokPzlcUIAQ73+776hKf0+qcyFdT/8o5hlTSDaQwAugpnB5JtC3IAobcX +sv9y+8AF034fxCW04J8WnEoYTtST/7KDtzaw5uONIaj515sxtShlw5M70yHDGXuQY o+wESRph61IDDV5a41OwvMIOX8gkyBtSS/wldo76STb0T8DALcWfrKpxx6fy/tDwji KJt6L3V/XNSl5c40uACeUCc3k/fm9uDz3F+fVnpGfPKjRjBJ/DAPUPDKbjSuYnuedr PJBDR6Xylg8Pg== 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 1/8] execmem: drop unused execmem_update_copy() Date: Fri, 4 Jul 2025 16:49:36 +0300 Message-ID: <20250704134943.3524829-2-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" The execmem_update_copy() that used text poking was required when memory allocated from ROX cache was always read-only. Since now its permissions can be switched to read-write there is no need in a function that updates memory with text poking. Remove it. Signed-off-by: Mike Rapoport (Microsoft) --- include/linux/execmem.h | 13 ------------- mm/execmem.c | 5 ----- 2 files changed, 18 deletions(-) diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 3be35680a54f..734fbe83d98e 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -185,19 +185,6 @@ DEFINE_FREE(execmem, void *, if (_T) execmem_free(_T)); struct vm_struct *execmem_vmap(size_t size); #endif =20 -/** - * execmem_update_copy - copy an update to executable memory - * @dst: destination address to update - * @src: source address containing the data - * @size: how many bytes of memory shold be copied - * - * Copy @size bytes from @src to @dst using text poking if the memory at - * @dst is read-only. - * - * Return: a pointer to @dst or NULL on error - */ -void *execmem_update_copy(void *dst, const void *src, size_t size); - /** * execmem_is_rox - check if execmem is read-only * @type - the execmem type to check diff --git a/mm/execmem.c b/mm/execmem.c index 2b683e7d864d..0712ebb4eb77 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -399,11 +399,6 @@ void execmem_free(void *ptr) vfree(ptr); } =20 -void *execmem_update_copy(void *dst, const void *src, size_t size) -{ - return text_poke_copy(dst, src, size); -} - bool execmem_is_rox(enum execmem_type type) { return !!(execmem_info->ranges[type].flags & EXECMEM_ROX_CACHE); --=20 2.47.2 From nobody Tue Oct 7 22:18:34 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D8B5630205F; Fri, 4 Jul 2025 13:50:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637004; cv=none; b=X4XZIUoaw67DtMDntP+Zi+H55jMEwmmdpVWoP9+BYHwwBmKU7w9auCKwuVXoti3YMBqgGbu9hzTvBanfxMIXPyATEWXnz0C7yYHWdBVdn4RHEqeHgo7wLI61OiHnxz3Zdly+HbQTaXsEikr/p0H9JVgEaXwcn882h7R7gJS2dvE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637004; c=relaxed/simple; bh=NrvBoIv7ihWz3msr5ucNgQMNcZ4XLlRn/Kum2bPi/3o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KO6REvpDUpqDvRNVEyvCgKNgknQkduGgL+a3TcuS+eYtGHs9dhMoxX/pmA/XWgQLQho0n0Xjscb9hAd3JPFlAG0yX2J+ske9cMcgy/j4nlKI4xU9RB0QmyYhP8Ft67yWXokRlelgEGHGQk5KOdoYl1lyUGcGBQMnnyMNo8autZg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=du9mSzGa; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="du9mSzGa" 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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; =20 static void *__its_alloc(struct its_array *pages) { - void *page __free(execmem) =3D execmem_alloc(EXECMEM_MODULE_TEXT, PAGE_SI= ZE); + void *page __free(execmem) =3D execmem_alloc_rw(EXECMEM_MODULE_TEXT, PAGE= _SIZE); if (!page) return NULL; =20 @@ -237,7 +237,6 @@ static void *its_alloc(void) if (!page) return NULL; =20 - execmem_make_temp_rw(page, PAGE_SIZE); if (pages =3D=3D &its_pages) set_memory_x((unsigned long)page, 1); =20 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); =20 -/** - * 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 =20 @@ -165,6 +149,28 @@ struct execmem_info *execmem_arch_setup(void); */ void *execmem_alloc(enum execmem_type type, size_t size); =20 +/** + * 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 =3D EXECMEM_MODULE_TEXT; =20 - ptr =3D execmem_alloc(execmem_type, size); + ptr =3D execmem_alloc_rw(execmem_type, size); if (!ptr) return -ENOMEM; =20 - if (execmem_is_rox(execmem_type)) { - int err =3D execmem_make_temp_rw(ptr, size); - - if (err) { - execmem_free(ptr); - return -ENOMEM; - } - - mod->mem[type].is_rox =3D true; - } + mod->mem[type].is_rox =3D execmem_is_rox(execmem_type); =20 /* * 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; } =20 -int execmem_make_temp_rw(void *ptr, size_t size) +static int execmem_force_rw(void *ptr, size_t size) { unsigned int nr =3D PAGE_ALIGN(size) >> PAGE_SHIFT; unsigned long addr =3D (unsigned long)ptr; @@ -358,6 +358,16 @@ int execmem_restore_rox(void *ptr, size_t size) } =20 #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) m= ust + * 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 siz= e) return kasan_reset_tag(p); } =20 +void *execmem_alloc_rw(enum execmem_type type, size_t size) +{ + void *p __free(execmem) =3D execmem_alloc(type, size); + int err; + + if (!p) + return NULL; + + err =3D execmem_force_rw(p, size); + if (err) + return NULL; + + return no_free_ptr(p); +} + void execmem_free(void *ptr) { /* --=20 2.47.2 From nobody Tue Oct 7 22:18:34 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34C1930749E; Fri, 4 Jul 2025 13:50:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637009; cv=none; b=q5foNlV5jVFNDkRxgSeMDeeP4z8ACqhaNMfBxJf3NjUkRRHhFWZgyV4DfeIZBrje6d7Op9CJ4uDOoVLaAURTrKC94ZFfautTrgI3IJTnd/98wZepBLsqaRbxPLjkYCUQh1XeLHHToTL9bfdSuS54U1NhnDjq/NgmlpvE6Ixb0C4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637009; c=relaxed/simple; bh=ZNEJRJfWjPqcxgGqudYL+ToTxSRDO6h8hesaJrQI5DI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DKn3CBKN1C2f5z8HNb4ZH3AHj6oCvo+IF65U3SQYZlFWpZctcj4AHiShDLPgnfbXWh3M3zp5gTsPXkPRQIwnTWzf/sBjNv62TBZXkeSy+LcbkiDnD/M2r6LluoRBcOmqwMKk3ERqn+mhZvBrbja/OjME3+Go0xmT0//54uDxRA8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hkADYyEV; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hkADYyEV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 10628C4CEE3; Fri, 4 Jul 2025 13:50:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751637009; bh=ZNEJRJfWjPqcxgGqudYL+ToTxSRDO6h8hesaJrQI5DI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hkADYyEVBsAD4xvYJYsQt0aFOqjZU0QQRSfnrXMVtaXTRlEr14ZKnEmIcyLYlQ9B7 T87qoQKOZoW+Fe35J6xQLShFllVVa37D2ooami7C5m/C+aoAQ8+cc/jYlPv45TT/N9 z+R3Y/dVyudtJtrK7qldz0PWCkIOp4lnwLxn/8uEt9ZRYsTbhpKkpgemL9SNR0Ay7n xRdwk4w65l3IoBGi6GHxh/seT0aEir6Q7bNZqm+g0SLgbmDVkzx/3wwClFJLvvKE6Z 2zLubvsO9TcXOosj/LslipVCKSELNYMhMgoCpS4d/H11Fz5E1NFO+IVCCa4jZSBwBn Tvjd42nCrNiXQ== 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 3/8] execmem: rework execmem_cache_free() Date: Fri, 4 Jul 2025 16:49:38 +0300 Message-ID: <20250704134943.3524829-4-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" Currently execmem_cache_free() ignores potential allocation failures that may happen in execmem_cache_add(). Besides, it uses text poking to fill the memory with trapping instructions before returning it to cache although it would be more efficient to make that memory writable, update it using memcpy and then restore ROX protection. Rework execmem_cache_free() so that in case of an error it will defer freeing of the memory to a delayed work. With this the happy fast path will now change permissions to RW, fill the memory with trapping instructions using memcpy, restore ROX permissions, add the memory back to the free cache and clear the relevant entry in busy_areas. If any step in the fast path fails, the entry in busy_areas will be marked as pending_free. These entries will be handled by a delayed work and freed asynchronously. To make the fast path faster, use __GFP_NORETRY for memory allocations and let asynchronous handler try harder with GFP_KERNEL. Signed-off-by: Mike Rapoport (Microsoft) --- mm/execmem.c | 120 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 97 insertions(+), 23 deletions(-) diff --git a/mm/execmem.c b/mm/execmem.c index 6b040fbc5f4f..1cc781244593 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -93,8 +93,15 @@ struct execmem_cache { struct mutex mutex; struct maple_tree busy_areas; struct maple_tree free_areas; + unsigned int pending_free_cnt; /* protected by mutex */ }; =20 +/* delay to schedule asynchronous free if fast path free fails */ +#define FREE_DELAY (msecs_to_jiffies(10)) + +/* mark entries in busy_areas that should be freed asynchronously */ +#define PENDING_FREE_MASK (1 << (PAGE_SHIFT - 1)) + static struct execmem_cache execmem_cache =3D { .mutex =3D __MUTEX_INITIALIZER(execmem_cache.mutex), .busy_areas =3D MTREE_INIT_EXT(busy_areas, MT_FLAGS_LOCK_EXTERN, @@ -155,20 +162,17 @@ static void execmem_cache_clean(struct work_struct *w= ork) =20 static DECLARE_WORK(execmem_cache_clean_work, execmem_cache_clean); =20 -static int execmem_cache_add(void *ptr, size_t size) +static int execmem_cache_add_locked(void *ptr, size_t size, gfp_t gfp_mask) { struct maple_tree *free_areas =3D &execmem_cache.free_areas; - struct mutex *mutex =3D &execmem_cache.mutex; unsigned long addr =3D (unsigned long)ptr; MA_STATE(mas, free_areas, addr - 1, addr + 1); unsigned long lower, upper; void *area =3D NULL; - int err; =20 lower =3D addr; upper =3D addr + size - 1; =20 - mutex_lock(mutex); area =3D mas_walk(&mas); if (area && mas.last =3D=3D addr - 1) lower =3D mas.index; @@ -178,12 +182,14 @@ static int execmem_cache_add(void *ptr, size_t size) upper =3D mas.last; =20 mas_set_range(&mas, lower, upper); - err =3D mas_store_gfp(&mas, (void *)lower, GFP_KERNEL); - mutex_unlock(mutex); - if (err) - return err; + return mas_store_gfp(&mas, (void *)lower, gfp_mask); +} =20 - return 0; +static int execmem_cache_add(void *ptr, size_t size, gfp_t gfp_mask) +{ + guard(mutex)(&execmem_cache.mutex); + + return execmem_cache_add_locked(ptr, size, gfp_mask); } =20 static bool within_range(struct execmem_range *range, struct ma_state *mas, @@ -278,7 +284,7 @@ static int execmem_cache_populate(struct execmem_range = *range, size_t size) if (err) goto err_free_mem; =20 - err =3D execmem_cache_add(p, alloc_size); + err =3D execmem_cache_add(p, alloc_size, GFP_KERNEL); if (err) goto err_reset_direct_map; =20 @@ -307,33 +313,101 @@ static void *execmem_cache_alloc(struct execmem_rang= e *range, size_t size) return __execmem_cache_alloc(range, size); } =20 +static inline bool is_pending_free(void *ptr) +{ + return ((unsigned long)ptr & PENDING_FREE_MASK); +} + +static inline void *pending_free_set(void *ptr) +{ + return (void *)((unsigned long)ptr | PENDING_FREE_MASK); +} + +static inline void *pending_free_clear(void *ptr) +{ + return (void *)((unsigned long)ptr & ~PENDING_FREE_MASK); +} + +static int execmem_force_rw(void *ptr, size_t size); + +static int __execmem_cache_free(struct ma_state *mas, void *ptr, gfp_t gfp= _mask) +{ + size_t size =3D mas_range_len(mas); + int err; + + err =3D execmem_force_rw(ptr, size); + if (err) + return err; + + execmem_fill_trapping_insns(ptr, size, /* writable =3D */ true); + execmem_restore_rox(ptr, size); + + err =3D execmem_cache_add_locked(ptr, size, gfp_mask); + if (err) + return err; + + mas_store_gfp(mas, NULL, gfp_mask); + return 0; +} + +static void execmem_cache_free_slow(struct work_struct *work); +static DECLARE_DELAYED_WORK(execmem_cache_free_work, execmem_cache_free_sl= ow); + +static void execmem_cache_free_slow(struct work_struct *work) +{ + struct maple_tree *busy_areas =3D &execmem_cache.busy_areas; + MA_STATE(mas, busy_areas, 0, ULONG_MAX); + void *area; + + guard(mutex)(&execmem_cache.mutex); + + if (!execmem_cache.pending_free_cnt) + return; + + mas_for_each(&mas, area, ULONG_MAX) { + if (!is_pending_free(area)) + continue; + + pending_free_clear(area); + if (__execmem_cache_free(&mas, area, GFP_KERNEL)) + continue; + + execmem_cache.pending_free_cnt--; + } + + if (execmem_cache.pending_free_cnt) + schedule_delayed_work(&execmem_cache_free_work, FREE_DELAY); + else + schedule_work(&execmem_cache_clean_work); +} + static bool execmem_cache_free(void *ptr) { struct maple_tree *busy_areas =3D &execmem_cache.busy_areas; - struct mutex *mutex =3D &execmem_cache.mutex; unsigned long addr =3D (unsigned long)ptr; MA_STATE(mas, busy_areas, addr, addr); - size_t size; void *area; + int err; + + guard(mutex)(&execmem_cache.mutex); =20 - mutex_lock(mutex); area =3D mas_walk(&mas); - if (!area) { - mutex_unlock(mutex); + if (!area) return false; - } - size =3D mas_range_len(&mas); - - mas_store_gfp(&mas, NULL, GFP_KERNEL); - mutex_unlock(mutex); - - execmem_fill_trapping_insns(ptr, size, /* writable =3D */ false); =20 - execmem_cache_add(ptr, size); + err =3D __execmem_cache_free(&mas, ptr, GFP_KERNEL | __GFP_NORETRY); + if (err) + goto err_slowpath; =20 schedule_work(&execmem_cache_clean_work); =20 return true; + +err_slowpath: + mas_store_gfp(&mas, pending_free_set(ptr), GFP_KERNEL); + execmem_cache.pending_free_cnt++; + schedule_delayed_work(&execmem_cache_free_work, FREE_DELAY); + return true; } =20 static int execmem_force_rw(void *ptr, size_t size) --=20 2.47.2 From nobody Tue Oct 7 22:18:34 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BECB2307AE6; Fri, 4 Jul 2025 13:50:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637014; cv=none; b=NUAZ6XuWRUv+F7xF8e+RkRnQlurKUJXir+po7BGWnLAluzTdMC+lApY3ZKh/FdIF4J62+N4RccKHAB3lkggMGF2gzavELTVutuTzf37kEe61Rw2I8cDFy9w+7KOUdWZ96zggD4ZjuBznYgAivHqNiRIJpqV160svrdXf8Hk8rj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637014; c=relaxed/simple; bh=ekIwqw7pcIPGCmYh23KJrg3hSMMH1+4weybXeR57thU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=E/AhNr02RmUaHuoAJmWNFb8heOr/8kc1oIib3kyaSlcsA2igHHdgdq9DXhpgMgSyq909Jb/FelBSiJGtrKHQdmjdRPvT9F8Dn5BvIZHTVuTnO2rprg1U/CSCILc7d94b/y+mBKezhQ5G2k3VUVBOj2WB1CqOT2gb4o6yPHCkJXQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lYXunHQQ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lYXunHQQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 85F10C4CEF2; Fri, 4 Jul 2025 13:50:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751637014; bh=ekIwqw7pcIPGCmYh23KJrg3hSMMH1+4weybXeR57thU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lYXunHQQS4Sf3pr+dPZ9lYeaAI+n4jw/7nwIoOrVZsWb2Q4gaKa1YzTt/ElGU7tN9 0qKqmk0pvsTKQDpXaO2TpSmlTRyqa8OAEU/1fHDtYJdUn605+KEBdvoJWI90DqPgpr PTnJ7xu+hMkerN6GeHmTvBARoLGZ9YNptJ6q/jCbF9WxC/UPjVlQ2ApO8io1D1QgYg TC+tfJjIpEyB411e6C9zyOe+6d0hGBwYX7++uarI2XlpcZrDlNAMylymrlYVd4W8kB nj20K765ZTibqLHYV6tuRsCyPhe403p3Ajhnqxgswy37kz0uNU1Tiyud9xOtaoTyRL QficFfSvv8d3w== 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 4/8] execmem: move execmem_force_rw() and execmem_restore_rox() before use Date: Fri, 4 Jul 2025 16:49:39 +0300 Message-ID: <20250704134943.3524829-5-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" to avoid static declarations. Signed-off-by: Mike Rapoport (Microsoft) --- mm/execmem.c | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/mm/execmem.c b/mm/execmem.c index 1cc781244593..3cb3a9d1c93f 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -137,6 +137,27 @@ static int execmem_set_direct_map_valid(struct vm_stru= ct *vm, bool valid) return err; } =20 +static int execmem_force_rw(void *ptr, size_t size) +{ + unsigned int nr =3D PAGE_ALIGN(size) >> PAGE_SHIFT; + unsigned long addr =3D (unsigned long)ptr; + int ret; + + ret =3D set_memory_nx(addr, nr); + if (ret) + return ret; + + return set_memory_rw(addr, nr); +} + +int execmem_restore_rox(void *ptr, size_t size) +{ + unsigned int nr =3D PAGE_ALIGN(size) >> PAGE_SHIFT; + unsigned long addr =3D (unsigned long)ptr; + + return set_memory_rox(addr, nr); +} + static void execmem_cache_clean(struct work_struct *work) { struct maple_tree *free_areas =3D &execmem_cache.free_areas; @@ -328,8 +349,6 @@ static inline void *pending_free_clear(void *ptr) return (void *)((unsigned long)ptr & ~PENDING_FREE_MASK); } =20 -static int execmem_force_rw(void *ptr, size_t size); - static int __execmem_cache_free(struct ma_state *mas, void *ptr, gfp_t gfp= _mask) { size_t size =3D mas_range_len(mas); @@ -410,27 +429,6 @@ static bool execmem_cache_free(void *ptr) return true; } =20 -static int execmem_force_rw(void *ptr, size_t size) -{ - unsigned int nr =3D PAGE_ALIGN(size) >> PAGE_SHIFT; - unsigned long addr =3D (unsigned long)ptr; - int ret; - - ret =3D set_memory_nx(addr, nr); - if (ret) - return ret; - - return set_memory_rw(addr, nr); -} - -int execmem_restore_rox(void *ptr, size_t size) -{ - unsigned int nr =3D PAGE_ALIGN(size) >> PAGE_SHIFT; - unsigned long addr =3D (unsigned long)ptr; - - return set_memory_rox(addr, nr); -} - #else /* CONFIG_ARCH_HAS_EXECMEM_ROX */ /* * when ROX cache is not used the permissions defined by architectures for --=20 2.47.2 From nobody Tue Oct 7 22:18:34 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 32D9230AAC5; Fri, 4 Jul 2025 13:50:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637021; cv=none; b=FhgajUKFlel/NoCD+dP5wBog5y0h5JZjaqUPD3jPetz/cfXXb92894SA6R2ZlUA+/7dts6sGsbgBiE/XyXaAIiJg2PL3h7RE1BLTzRpvjZmEE2bn2hO71zcOJd2cMNOho3A9qn5dVPzH5Qoh8UUw/cuAeHQXSEgWsPQrKGPAwXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637021; c=relaxed/simple; bh=GUCCcYH19Ye+ebUGH5iigB3tILGu1YhJq+jW18wLp4U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ugOyJF9fL5OYo/G/4JnyDjPdQ1IcJHYBvbTVAEUJkHYZJQRRqGvxSr6z03W3E+iVVtVzlL5Wh0OgikUiCoro+36PAgC55L2v/bcFcK8jqpDsd04DzT1dikqUxW+HvqiC2G1xZyF2TO06rd/DXqpxSPfNrfLsl5KLrisvy4Egydk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AUKnVSBm; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AUKnVSBm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 067D0C4CEE3; Fri, 4 Jul 2025 13:50:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751637020; bh=GUCCcYH19Ye+ebUGH5iigB3tILGu1YhJq+jW18wLp4U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AUKnVSBmR+9ZHYaZdh6o3tuMFSCpTJv8fsfg5Bsuc7N/GIg6OKPKO1uuobiWUOK40 qKsB4BwOXXyzJ2RmLIGqr1ceennVXZB3vvnuYnT0/KnqPRO+XOOfifpRaZD1p++eu/ 2d+28Rxk7NSh7E+J1Ub6ZE+CSAFXfapwaLLK4FKBPcjydUZIKuyIPz5Bu10FekiXDR JMIqWlgYeg3zzNz+6SEXFTIEeml8W037fwdtjJYFeeS28BS91d3pUelBp0Th0GV47H OAZ0Vvc+KS42TfEbyT1q9ngi/XA2sir8YA+sGcCatYmMzIDQSgATIsxgbXBoIbA2gp cI5ZOxWEyQYoQ== 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 5/8] execmem: add fallback for failures in vmalloc(VM_ALLOW_HUGE_VMAP) Date: Fri, 4 Jul 2025 16:49:40 +0300 Message-ID: <20250704134943.3524829-6-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" When execmem populates ROX cache it uses vmalloc(VM_ALLOW_HUGE_VMAP). Although vmalloc falls back to allocating base pages if high order allocation fails, it may happen that it still cannot allocate enough memory. Right now ROX cache is only used by modules and in majority of cases the allocations happen at boot time when there's plenty of free memory, but upcoming enabling ROX cache for ftrace and kprobes would mean that execmem allocations can happen when the system is under memory pressure and a failure to allocate large page worth of memory becomes more likely. Fallback to regular vmalloc() if vmalloc(VM_ALLOW_HUGE_VMAP) fails. Signed-off-by: Mike Rapoport (Microsoft) --- mm/execmem.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mm/execmem.c b/mm/execmem.c index 3cb3a9d1c93f..ec2a6aab143b 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -291,6 +291,11 @@ static int execmem_cache_populate(struct execmem_range= *range, size_t size) =20 alloc_size =3D round_up(size, PMD_SIZE); p =3D execmem_vmalloc(range, alloc_size, PAGE_KERNEL, vm_flags); + if (!p) { + alloc_size =3D size; + p =3D execmem_vmalloc(range, alloc_size, PAGE_KERNEL, vm_flags); + } + if (!p) return err; =20 @@ -457,7 +462,7 @@ void *execmem_alloc(enum execmem_type type, size_t size) bool use_cache =3D range->flags & EXECMEM_ROX_CACHE; unsigned long vm_flags =3D VM_FLUSH_RESET_PERMS; pgprot_t pgprot =3D range->pgprot; - void *p; + void *p =3D NULL; =20 size =3D PAGE_ALIGN(size); =20 --=20 2.47.2 From nobody Tue Oct 7 22:18:34 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 901F7306DDF; Fri, 4 Jul 2025 13:50:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637025; cv=none; b=kdvOy8Jn0cBppm5PZP/79R6k1swBoZtaJGB1EmN27p/PjOOYCle0RHJshOr2oTOC60qYl2qjMKMZ3SGsIcTpKbYr5euYwcIoyd6nd4v1k163XPRkBIk4Xk+eHp2fuQrOrBhB6MeSk+kTN+j4QVFeMxjPdrFKfdPUYvBp1dh+92c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637025; c=relaxed/simple; bh=LpVdGb6KRirZdT05J73u+j0sDks+bcZOsNalMSCnv6o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mgJGwGmBBggftB+ty41BqVh/IE65QiciFn1lt9LkLT9YjnMFrVBGr9ytvMELOUbBMLzpYKC4PpKuhOS/SWzePlcYQzXMNChmoqX6unwRt2eWtg9JZxfWlYRpn7+h0C5rAb0BJJuDxD8OqBlRpJY1/ghuiddK7JnLSDC0uTchWpk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=s5SHeyJO; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="s5SHeyJO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B78EC4CEEB; Fri, 4 Jul 2025 13:50:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751637025; bh=LpVdGb6KRirZdT05J73u+j0sDks+bcZOsNalMSCnv6o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s5SHeyJO0YmS+UuFO30EnLCBY1/QU2m9p5YinIlB6hYFx/7N+5Zczxfo6UkAgJTRA WPfC2UqtpDJL2APhGa1UNLRSRS9OOb4WF33Ne0GkvfxgrdLlkmRF+fz/tn1DOjhxaH ZNXVObc8wxGuh5WXTX8tG1MlYhnlJYNYYFpCzKRL3SD9NhUh0pZ6bQng2tV1eDpuKS 3ytPqvu9imp/jqF5ouyR4oLHgVOblK8w9E61NmvtLIkYen43S0ji2NvtzhxSOdoSyD Pkqx2YAZgFt3yLIvkH+5ePwHgQzZuYoH/vGDe8npvULS4kvQ9I3m3AUOwierxO74ZX LD9maYY9v7vxA== 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 6/8] execmem: drop writable parameter from execmem_fill_trapping_insns() Date: Fri, 4 Jul 2025 16:49:41 +0300 Message-ID: <20250704134943.3524829-7-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" After update of execmem_cache_free() that made memory writable before updating it, there is no need to update read only memory, so the writable parameter to execmem_fill_trapping_insns() is not needed. Drop it. Signed-off-by: Mike Rapoport (Microsoft) --- arch/x86/mm/init.c | 8 ++------ include/linux/execmem.h | 3 +-- mm/execmem.c | 4 ++-- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c index 7456df985d96..dbc63f0d538f 100644 --- a/arch/x86/mm/init.c +++ b/arch/x86/mm/init.c @@ -1063,13 +1063,9 @@ unsigned long arch_max_swapfile_size(void) static struct execmem_info execmem_info __ro_after_init; =20 #ifdef CONFIG_ARCH_HAS_EXECMEM_ROX -void execmem_fill_trapping_insns(void *ptr, size_t size, bool writeable) +void execmem_fill_trapping_insns(void *ptr, size_t size) { - /* fill memory with INT3 instructions */ - if (writeable) - memset(ptr, INT3_INSN_OPCODE, size); - else - text_poke_set(ptr, INT3_INSN_OPCODE, size); + memset(ptr, INT3_INSN_OPCODE, size); } #endif =20 diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 4e510d1c609c..fe367bdadc3e 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -60,12 +60,11 @@ enum execmem_range_flags { * will trap * @ptr: pointer to memory to fill * @size: size of the range to fill - * @writable: is the memory poited by @ptr is writable or ROX * * A hook for architecures to fill execmem ranges with invalid instruction= s. * Architectures that use EXECMEM_ROX_CACHE must implement this. */ -void execmem_fill_trapping_insns(void *ptr, size_t size, bool writable); +void execmem_fill_trapping_insns(void *ptr, size_t size); =20 /** * execmem_restore_rox - restore read-only-execute permissions diff --git a/mm/execmem.c b/mm/execmem.c index ec2a6aab143b..398e60c1002f 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -304,7 +304,7 @@ static int execmem_cache_populate(struct execmem_range = *range, size_t size) goto err_free_mem; =20 /* fill memory with instructions that will trap */ - execmem_fill_trapping_insns(p, alloc_size, /* writable =3D */ true); + execmem_fill_trapping_insns(p, alloc_size); =20 err =3D set_memory_rox((unsigned long)p, vm->nr_pages); if (err) @@ -363,7 +363,7 @@ static int __execmem_cache_free(struct ma_state *mas, v= oid *ptr, gfp_t gfp_mask) if (err) return err; =20 - execmem_fill_trapping_insns(ptr, size, /* writable =3D */ true); + execmem_fill_trapping_insns(ptr, size); execmem_restore_rox(ptr, size); =20 err =3D execmem_cache_add_locked(ptr, size, gfp_mask); --=20 2.47.2 From nobody Tue Oct 7 22:18:34 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18E4C3093D9; Fri, 4 Jul 2025 13:50:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637031; cv=none; b=ELANgKl+/XdFARMfLY0Ef+BZaaHteXxkslZw6v/wV2sPCd99/bl6z7KHopJOAc6WhSFTT7/MehfxHM08HjvkWr/37k2gmIp79xfX7ELCjsmEzVk4xlcNUWzfVo4j+NamsDvZPS2IqHDVpWm1+xpEhHIsKJjhKNiad/mLOVDU7Fs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637031; c=relaxed/simple; bh=SO2FNTrKQArpvSkS2JHAbwQoeech8iUoJBHVmLqUzb0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=t2ykH0gx4Q6gsBZ9xZMY5kjKR4vjo3pE5MNEfsul27Fo41LeWedtp1AKX4Y3zDcvkeTIHZSc6T3tNaQYOsCRlo0oqH7nV23dPY6+MNX5VZ6zcnUsdqGD7yj7lRoH5ucJn+9P7pQ5CjGTTlF4FVpemVVav3eZwuV7lAzQ54HiCCo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OHM1CWKR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OHM1CWKR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF94DC4CEE3; Fri, 4 Jul 2025 13:50:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751637030; bh=SO2FNTrKQArpvSkS2JHAbwQoeech8iUoJBHVmLqUzb0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OHM1CWKRIwGWZdUruYiyIKa0OUcRBfg3/yYHwoPDkppKqzNudoUVAbFLF/tRhKr0S CaHDLw94VcjhEZXPkjkaNKBhLod6IRa8FAO3dyQmsIOXgqwY/Xt4jRLLQCtY3PiGvT +stoNpdu2LMmJ485ccLCwoaFcWU2EFmdafhIThmRHqPi85Uo98iaE3bgWRw/rW/Kt4 ETSjNnyi+t6NIn+uKzrK0QblgxieZnPffy+mg6WdJwc4Kbd5ayK0nhOVyZ79vPgxdt nZUQT9Gf/EDIu5E88uuIkxvOULPIUuYGOf0TCxcYW3e9DEO2I//MjRqMtLQQ9iV3E+ M/sEU5hEqCVqQ== 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 7/8] x86/kprobes: enable EXECMEM_ROX_CACHE for kprobes allocations Date: Fri, 4 Jul 2025 16:49:42 +0300 Message-ID: <20250704134943.3524829-8-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" x86::alloc_insn_page() always allocates ROX memory. Instead of overriding this method, add EXECMEM_KPROBES entry in execmem_info with pgprot set to PAGE_KERNEL_ROX and use ROX cache when configuration and CPU features allow it. Signed-off-by: Mike Rapoport (Microsoft) --- arch/x86/kernel/kprobes/core.c | 18 ------------------ arch/x86/mm/init.c | 9 ++++++++- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index 47cb8eb138ba..6079d15dab8c 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -481,24 +481,6 @@ static int prepare_singlestep(kprobe_opcode_t *buf, st= ruct kprobe *p, return len; } =20 -/* Make page to RO mode when allocate it */ -void *alloc_insn_page(void) -{ - void *page; - - page =3D execmem_alloc(EXECMEM_KPROBES, PAGE_SIZE); - if (!page) - return NULL; - - /* - * TODO: Once additional kernel code protection mechanisms are set, ensure - * that the page was not maliciously altered and it is still zeroed. - */ - set_memory_rox((unsigned long)page, 1); - - return page; -} - /* Kprobe x86 instruction emulation - only regs->ip or IF flag modifiers */ =20 static void kprobe_emulate_ifmodifiers(struct kprobe *p, struct pt_regs *r= egs) diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c index dbc63f0d538f..442fafd8ff52 100644 --- a/arch/x86/mm/init.c +++ b/arch/x86/mm/init.c @@ -1098,7 +1098,14 @@ struct execmem_info __init *execmem_arch_setup(void) .pgprot =3D pgprot, .alignment =3D MODULE_ALIGN, }, - [EXECMEM_KPROBES ... EXECMEM_BPF] =3D { + [EXECMEM_KPROBES] =3D { + .flags =3D flags, + .start =3D start, + .end =3D MODULES_END, + .pgprot =3D PAGE_KERNEL_ROX, + .alignment =3D MODULE_ALIGN, + }, + [EXECMEM_FTRACE ... EXECMEM_BPF] =3D { .flags =3D EXECMEM_KASAN_SHADOW, .start =3D start, .end =3D MODULES_END, --=20 2.47.2 From nobody Tue Oct 7 22:18:34 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCE513093DD; Fri, 4 Jul 2025 13:50:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637037; cv=none; b=Fl06bo51Ru7ALO8rA2GFnInHBkskmnqDa7YRn08yz4PIBLxLl75yYilth/Elcc9rvTtgPFVxTXCT1/iPpC3rLjJcUQbSp3MNXjoF3RCS/E2mzH+edMClqMXoVwLpOrEEYrid0h6VcIGrQdDGLpc+tU9ojJ9UOpB9HhsJ4bt6/aw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751637037; c=relaxed/simple; bh=N93fx/HWGQ9/7LBI0Q5dx377OnCZ8CrZ/OhiD9v/Z/8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JY6Nba9gmq/LFHKeOVjC5qwxO/c+rf+4nTTckwPVGCnTKOLZLkau3XIWYuRQZroS2zC6s42tFt8PoGmLzuvMvbxESJz7P/RMwGiRpBU9S8MSh+7uRyIXTuIpG4ODc9ucun/LAguDlD3jaBXXr+WO3NgxQVMW2TKp0Eqb4o5K01A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IafJ5zhz; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="IafJ5zhz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 716F3C4CEEB; Fri, 4 Jul 2025 13:50:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751637036; bh=N93fx/HWGQ9/7LBI0Q5dx377OnCZ8CrZ/OhiD9v/Z/8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IafJ5zhzJJMzQ5RvGFz9jHElZlpQS/cEpbHiJxGptp5RZk2uaNXSUmlxru9vxG0Kb my6sw81V2pUOKJO1VNXbXkyX54FcpSGrYkNlcQ/zC2JzREUevQHixwULxSwoZP/0Ao L57ARhp2EwtWbNo4JI2cnYce4L0oAVLtG4KnJPruYzna/Ruxpq+TTi3Xed2SikeQCx G0QZsiDA0BAqUNC0WiLG5StfJgJ7NHGRRJSNizFGe+xDAEuQrswFi0JvogCPgcbwwV 53cCDyXsz+5McRnjUaaNiAgEigDmHz7JsdAkKoJqHfzCVV1vqibVOyCx3Q6UJM33Dm j1LzYJybqnstw== 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 8/8] x86/ftrace: enable EXECMEM_ROX_CACHE for ftrace allocations Date: Fri, 4 Jul 2025 16:49:43 +0300 Message-ID: <20250704134943.3524829-9-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" For the most part ftrace uses text poking and can handle ROX memory. The only place that requires writable memory is create_trampoline() that updates the allocated memory and in the end makes it ROX. Use execmem_alloc_rw() in x86::ftrace::alloc_tramp() and enable ROX cache for EXECMEM_FTRACE when configuration and CPU features allow that. Signed-off-by: Mike Rapoport (Microsoft) --- arch/x86/kernel/ftrace.c | 2 +- arch/x86/mm/init.c | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/ftrace.c b/arch/x86/kernel/ftrace.c index 252e82bcfd2f..4450acec9390 100644 --- a/arch/x86/kernel/ftrace.c +++ b/arch/x86/kernel/ftrace.c @@ -263,7 +263,7 @@ void arch_ftrace_update_code(int command) =20 static inline void *alloc_tramp(unsigned long size) { - return execmem_alloc(EXECMEM_FTRACE, size); + return execmem_alloc_rw(EXECMEM_FTRACE, size); } static inline void tramp_free(void *tramp) { diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c index 442fafd8ff52..bb57e93b4caf 100644 --- a/arch/x86/mm/init.c +++ b/arch/x86/mm/init.c @@ -1105,7 +1105,14 @@ struct execmem_info __init *execmem_arch_setup(void) .pgprot =3D PAGE_KERNEL_ROX, .alignment =3D MODULE_ALIGN, }, - [EXECMEM_FTRACE ... EXECMEM_BPF] =3D { + [EXECMEM_FTRACE] =3D { + .flags =3D flags, + .start =3D start, + .end =3D MODULES_END, + .pgprot =3D pgprot, + .alignment =3D MODULE_ALIGN, + }, + [EXECMEM_BPF] =3D { .flags =3D EXECMEM_KASAN_SHADOW, .start =3D start, .end =3D MODULES_END, --=20 2.47.2