From nobody Sun Feb 8 08:48:01 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 37655149DF6 for ; Wed, 3 Apr 2024 15:02:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712156527; cv=none; b=Lah5q1bys1ycFq5gUakhKVfzx6zcqAWiuuJGQZRgMPNmNUS230n7aAhFiKKCP47SQib0eH+wksyfC+TBPoBSvSZE8O11p6c/to8ThU9NKVpo597Ut6C1+odZIct2ss9kpDI5qqREzYS42ZOlbaYZoWND4+dgJZNEGzlk1cTJ60c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712156527; c=relaxed/simple; bh=Ertxvvng45pAjcP3//wTuOOtK+J7Rf7F08jHMkMu0BU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hip7hOq0AHXBkBOo7+clO2QUmhwa0fyy5hZoxsWBWupQ1BoVsA3rsxC+9OxXvnKQbz8GgN49LUDmjfzzcAS2P1oqWFVjTEQHPJqZOaLUdiZRUlwh61qXV4Y0zOUmU9e2OKDnMU3lkbOK4zFfnr/atz2WBq31SQxKkdCTq3D/U9w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 725511595; Wed, 3 Apr 2024 08:02:35 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id C2F883F7B4; Wed, 3 Apr 2024 08:02:02 -0700 (PDT) From: Mark Rutland To: linux-kernel@vger.kernel.org, Catalin Marinas , Will Deacon Cc: anil.s.keshavamurthy@intel.com, aou@eecs.berkeley.edu, davem@davemloft.net, jarkko@kernel.org, linux-arm-kernel@lists.infradead.org, mark.rutland@arm.com, mhiramat@kernel.org, naveen.n.rao@linux.ibm.com, palmer@dabbelt.com, paul.walmsley@sifive.com Subject: [PATCH v2 1/4] arm64: patching: always use fixmap Date: Wed, 3 Apr 2024 16:01:51 +0100 Message-Id: <20240403150154.667649-2-mark.rutland@arm.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240403150154.667649-1-mark.rutland@arm.com> References: <20240403150154.667649-1-mark.rutland@arm.com> 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" For historical reasons, patch_map() won't bother to fixmap non-image addresses when CONFIG_STRICT_MODULE_RWX=3Dn, matching the behaviour prior to the introduction of CONFIG_STRICT_MODULE_RWX. However, as arm64 doesn't select CONFIG_ARCH_OPTIONAL_KERNEL_RWX, CONFIG_MODULES implies CONFIG_STRICT_MODULE_RWX, so any kernel built with module support will use the fixmap for any non-image address. Historically we only used patch_map() for the kernel image and modules, but these days its also used by BPF and KPROBES to write to read-only pages of executable text. Currently these both depend on CONFIG_MODULES, but we'd like to change that in subsequent patches, which will require using the fixmap regardless of CONFIG_STRICT_MODULE_RWX. This patch changes patch_map() to always use the fixmap, and simplifies the logic: * Use is_image_text() directly in the if-else, rather than using a temporary boolean variable. * Use offset_in_page() to get the offset within the mapping. * Remove uintaddr and cast the address directly when using is_image_text(). For kernels built with CONFIG_MODULES=3Dy, there should be no functional change as a result of this patch. For kernels built with CONFIG_MODULES=3Dn, patch_map() will use the fixmap for non-image addresses, but there are no extant users with non-image addresses when CONFIG_MODULES=3Dn, and hence there should be no functional change as a result of this patch alone. Signed-off-by: Mark Rutland Cc: Catalin Marinas Cc: Will Deacon Acked-by: Catalin Marinas Reviewed-by: Jarkko Sakkinen --- arch/arm64/kernel/patching.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) Catalin, Will, this is a prerequisite for the final two patches in the series. Are you happy for this go via the tracing tree? Mark. diff --git a/arch/arm64/kernel/patching.c b/arch/arm64/kernel/patching.c index 2555349303684..f0f3a2a82ca5a 100644 --- a/arch/arm64/kernel/patching.c +++ b/arch/arm64/kernel/patching.c @@ -30,20 +30,16 @@ static bool is_image_text(unsigned long addr) =20 static void __kprobes *patch_map(void *addr, int fixmap) { - unsigned long uintaddr =3D (uintptr_t) addr; - bool image =3D is_image_text(uintaddr); struct page *page; =20 - if (image) + if (is_image_text((unsigned long)addr)) page =3D phys_to_page(__pa_symbol(addr)); - else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) - page =3D vmalloc_to_page(addr); else - return addr; + page =3D vmalloc_to_page(addr); =20 BUG_ON(!page); return (void *)set_fixmap_offset(fixmap, page_to_phys(page) + - (uintaddr & ~PAGE_MASK)); + offset_in_page(addr)); } =20 static void __kprobes patch_unmap(int fixmap) --=20 2.30.2 From nobody Sun Feb 8 08:48:01 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D58614A098 for ; Wed, 3 Apr 2024 15:02:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712156529; cv=none; b=UBIktQ1z6PEditD5W/57Z27FZiR6WshQ5TUJj46eM/zT3ZSaRQDrHkemfucLCib3LukETeIhCbcSs525ODgTymdR3ndYjTeAHbMyVwV+8zBrC6ECAIKt18JeUAGaNDzwFuCG5IEpCdlKiUo4lavBShBKpddHJFKWf02d5GLVCHo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712156529; c=relaxed/simple; bh=6l3ztR+na6fIJOI07vhVnE8PPwhW1EVFzqZBqt5/uwk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bX++vnHLfcd10lvrD8X/OSDeLukLWDni7w8Vk/GaP9D/KlCw5oohZdL0r1GiykuCmRRoaDF5jyl1oOljZh3VAK5HS5ufgAXwsNwAunO/JyctbCw6BZWBKlsErwBF6tCHR7sSCtXiRe/f9ITjyio4wEMMr9Dzg3bs0isaiwGA7gE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D3E361650; Wed, 3 Apr 2024 08:02:37 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 318DB3F7B4; Wed, 3 Apr 2024 08:02:05 -0700 (PDT) From: Mark Rutland To: linux-kernel@vger.kernel.org Cc: anil.s.keshavamurthy@intel.com, aou@eecs.berkeley.edu, catalin.marinas@arm.com, davem@davemloft.net, jarkko@kernel.org, linux-arm-kernel@lists.infradead.org, mark.rutland@arm.com, mhiramat@kernel.org, naveen.n.rao@linux.ibm.com, palmer@dabbelt.com, paul.walmsley@sifive.com, will@kernel.org Subject: [PATCH v2 2/4] kprobes/treewide: Add kprobes_ prefix to insn alloc/free functions Date: Wed, 3 Apr 2024 16:01:52 +0100 Message-Id: <20240403150154.667649-3-mark.rutland@arm.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240403150154.667649-1-mark.rutland@arm.com> References: <20240403150154.667649-1-mark.rutland@arm.com> 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" The alloc_(opt)insn_page() and free_(opt)insn_page() functions are specific to KPROBES, but their name makes them sound more generic than they are. Given them a 'kprobes_' prefix to make it clear that they're part of kprobes. This was generated automatically with: sed -i 's/alloc_insn_page/kprobes_alloc_insn_page/' $(git grep -l 'alloc_= insn_page') sed -i 's/free_insn_page/kprobes_free_insn_page/' $(git grep -l 'free_ins= n_page') sed -i 's/alloc_optinsn_page/kprobes_alloc_optinsn_page/' $(git grep -l '= alloc_optinsn_page') sed -i 's/free_optinsn_page/kprobes_free_optinsn_page/' $(git grep -l 'fr= ee_optinsn_page') There should be no functional change as a result of this patch. Signed-off-by: Mark Rutland Cc: Anil S Keshavamurthy Cc: David S. Miller Cc: Jarkko Sakkinen Cc: Masami Hiramatsu Cc: Naveen N. Rao Reviewed-by: Jarkko Sakkinen --- arch/arm64/kernel/probes/kprobes.c | 2 +- arch/powerpc/kernel/kprobes.c | 2 +- arch/powerpc/kernel/optprobes.c | 4 ++-- arch/riscv/kernel/probes/kprobes.c | 2 +- arch/s390/kernel/kprobes.c | 2 +- arch/x86/kernel/kprobes/core.c | 2 +- include/linux/kprobes.h | 6 +++--- kernel/kprobes.c | 20 ++++++++++---------- 8 files changed, 20 insertions(+), 20 deletions(-) diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/= kprobes.c index 327855a11df2f..4b6ab7b1fa211 100644 --- a/arch/arm64/kernel/probes/kprobes.c +++ b/arch/arm64/kernel/probes/kprobes.c @@ -129,7 +129,7 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p) return 0; } =20 -void *alloc_insn_page(void) +void *kprobes_alloc_insn_page(void) { return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS, diff --git a/arch/powerpc/kernel/kprobes.c b/arch/powerpc/kernel/kprobes.c index bbca90a5e2ec0..0b297718d5de6 100644 --- a/arch/powerpc/kernel/kprobes.c +++ b/arch/powerpc/kernel/kprobes.c @@ -126,7 +126,7 @@ kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long = addr, unsigned long offse return (kprobe_opcode_t *)(addr + offset); } =20 -void *alloc_insn_page(void) +void *kprobes_alloc_insn_page(void) { void *page; =20 diff --git a/arch/powerpc/kernel/optprobes.c b/arch/powerpc/kernel/optprobe= s.c index 004fae2044a3e..0ddbda217073f 100644 --- a/arch/powerpc/kernel/optprobes.c +++ b/arch/powerpc/kernel/optprobes.c @@ -27,7 +27,7 @@ =20 static bool insn_page_in_use; =20 -void *alloc_optinsn_page(void) +void *kprobes_alloc_optinsn_page(void) { if (insn_page_in_use) return NULL; @@ -35,7 +35,7 @@ void *alloc_optinsn_page(void) return &optinsn_slot; } =20 -void free_optinsn_page(void *page) +void kprobes_free_optinsn_page(void *page) { insn_page_in_use =3D false; } diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/= kprobes.c index 2f08c14a933d0..75201ce721057 100644 --- a/arch/riscv/kernel/probes/kprobes.c +++ b/arch/riscv/kernel/probes/kprobes.c @@ -105,7 +105,7 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p) } =20 #ifdef CONFIG_MMU -void *alloc_insn_page(void) +void *kprobes_alloc_insn_page(void) { return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_READ_EXEC, diff --git a/arch/s390/kernel/kprobes.c b/arch/s390/kernel/kprobes.c index f0cf20d4b3c58..91ca4d501d4ef 100644 --- a/arch/s390/kernel/kprobes.c +++ b/arch/s390/kernel/kprobes.c @@ -34,7 +34,7 @@ struct kretprobe_blackpoint kretprobe_blacklist[] =3D { }; =20 static int insn_page_in_use; =20 -void *alloc_insn_page(void) +void *kprobes_alloc_insn_page(void) { void *page; =20 diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index d0e49bd7c6f3f..7f01bbbfa9e2a 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -491,7 +491,7 @@ static int prepare_singlestep(kprobe_opcode_t *buf, str= uct kprobe *p, } =20 /* Make page to RO mode when allocate it */ -void *alloc_insn_page(void) +void *kprobes_alloc_insn_page(void) { void *page; =20 diff --git a/include/linux/kprobes.h b/include/linux/kprobes.h index 0ff44d6633e33..ad4b561100f9e 100644 --- a/include/linux/kprobes.h +++ b/include/linux/kprobes.h @@ -430,10 +430,10 @@ int enable_kprobe(struct kprobe *kp); =20 void dump_kprobe(struct kprobe *kp); =20 -void *alloc_insn_page(void); +void *kprobes_alloc_insn_page(void); =20 -void *alloc_optinsn_page(void); -void free_optinsn_page(void *page); +void *kprobes_alloc_optinsn_page(void); +void kprobes_free_optinsn_page(void *page); =20 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *ty= pe, char *sym); diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 9d9095e817928..35adf56430c9b 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -110,7 +110,7 @@ enum kprobe_slot_state { SLOT_USED =3D 2, }; =20 -void __weak *alloc_insn_page(void) +void __weak *kprobes_alloc_insn_page(void) { /* * Use module_alloc() so this page is within +/- 2GB of where the @@ -121,15 +121,15 @@ void __weak *alloc_insn_page(void) return module_alloc(PAGE_SIZE); } =20 -static void free_insn_page(void *page) +static void kprobes_free_insn_page(void *page) { module_memfree(page); } =20 struct kprobe_insn_cache kprobe_insn_slots =3D { .mutex =3D __MUTEX_INITIALIZER(kprobe_insn_slots.mutex), - .alloc =3D alloc_insn_page, - .free =3D free_insn_page, + .alloc =3D kprobes_alloc_insn_page, + .free =3D kprobes_free_insn_page, .sym =3D KPROBE_INSN_PAGE_SYM, .pages =3D LIST_HEAD_INIT(kprobe_insn_slots.pages), .insn_size =3D MAX_INSN_SIZE, @@ -333,21 +333,21 @@ int kprobe_cache_get_kallsym(struct kprobe_insn_cache= *c, unsigned int *symnum, } =20 #ifdef CONFIG_OPTPROBES -void __weak *alloc_optinsn_page(void) +void __weak *kprobes_alloc_optinsn_page(void) { - return alloc_insn_page(); + return kprobes_alloc_insn_page(); } =20 -void __weak free_optinsn_page(void *page) +void __weak kprobes_free_optinsn_page(void *page) { - free_insn_page(page); + kprobes_free_insn_page(page); } =20 /* For optimized_kprobe buffer */ struct kprobe_insn_cache kprobe_optinsn_slots =3D { .mutex =3D __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex), - .alloc =3D alloc_optinsn_page, - .free =3D free_optinsn_page, + .alloc =3D kprobes_alloc_optinsn_page, + .free =3D kprobes_free_optinsn_page, .sym =3D KPROBE_OPTINSN_PAGE_SYM, .pages =3D LIST_HEAD_INIT(kprobe_optinsn_slots.pages), /* .insn_size is initialized later */ --=20 2.30.2 From nobody Sun Feb 8 08:48:01 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A85C514A4D9 for ; Wed, 3 Apr 2024 15:02:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712156531; cv=none; b=TsogmLoRYkctMDwNic2lhYpahegzq/QSG0G7uuoKpG8e5GjNehnqWr/qJdbLvFzEVj5rRaNaTe3B9ilZ4cRfF9f91i9y6BgEk5PgTmyEDy9ueVnmiS+L/zXJpNQf74PYxV8mZ8qWqEm64ogXF3YrIe7kLrRHYTRFoKVqog7edOE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712156531; c=relaxed/simple; bh=V9CTkZOx/tGYPNlc61sl/gWZULplYRl65rJqks9cElo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bIXkgp6Lx0cUO8id9bcWt15wC9fbVpSXYpD9iLiL5cbXK+4YtXuoNrqJKvwFGtcXStPzRCt4eWR161BD0o0CEtHuA6kXgbFMIsTUtm5i+WYMQMaVRlca0bYqIyxUEmRoSAbGQwFUV81MjUi1CLI4trmX7QzkNPZ0ncbtxd3dYm4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2C7C21007; Wed, 3 Apr 2024 08:02:40 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 7DE443F7B4; Wed, 3 Apr 2024 08:02:07 -0700 (PDT) From: Mark Rutland To: linux-kernel@vger.kernel.org Cc: anil.s.keshavamurthy@intel.com, aou@eecs.berkeley.edu, catalin.marinas@arm.com, davem@davemloft.net, jarkko@kernel.org, linux-arm-kernel@lists.infradead.org, mark.rutland@arm.com, mhiramat@kernel.org, naveen.n.rao@linux.ibm.com, palmer@dabbelt.com, paul.walmsley@sifive.com, will@kernel.org Subject: [PATCH v2 3/4] kprobes/treewide: Explicitly override alloc/free functions Date: Wed, 3 Apr 2024 16:01:53 +0100 Message-Id: <20240403150154.667649-4-mark.rutland@arm.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240403150154.667649-1-mark.rutland@arm.com> References: <20240403150154.667649-1-mark.rutland@arm.com> 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" Currently architectures can override kprobes_alloc_insn_page(), but kprobes_free_insn_page() is always implemented using module_memfree(), which might not be what an architecture needs, especially as we'd like to make it possible to use kprobes without requiring MODULES. It would be nicer if architectures either: (a) Used only the generic kprobes_alloc_insn_page() and kprobes_free_insn_page(), implicitly depending on MODULES. (b) Provided their own implementation of both kprobes_alloc_insn_page() and kprobes_free_insn_page(), handling the relevant dependencies themselves. This patch applies that split treewide: (a) Architectures using the generic kprobes_free_insn_page() and kprobes_free_insn_page() are left as-is. The __weak annotation is removed from the generic implementations so that accidental overrides/misuse can be detected easily. (b) Architectures which provide their own kprobes_free_insn_page() are given a matching implementation of kprobes_free_insn_page(), and select HAVE_KPROBES_ALLOC. This new Kconfig symbol will allow subsequent patches to relax the dependency on MODULES to (MODULES || HAVE_KPROBES_ALLOC) once other module dependencies in the core kprobes code are cleaned up. Architectures which use module_alloc() are given an implementation using module_memfree() along with an explicit dependency on MODULES. Architectures using __vmalloc_node_range() are given an implementation using vfree(). This loses the warning for in_interrupt(), but vfree() can handle this via vfree_atomic(), so the warning isn't necessary. On riscv, the allocator depends on !XIP_KERNEL, which is already a dependency for HAVE_KPROBES in arch/riscv/Kconfig. As of this patch arm64 and riscv have kprobe allocation functions which do not explicitly depend on MODULES. The core kprobes code still depends on MODULES. Signed-off-by: Mark Rutland Cc: Albert Ou Cc: Anil S Keshavamurthy Cc: Catalin Marinas Cc: David S. Miller Cc: Jarkko Sakkinen Cc: Masami Hiramatsu Cc: Naveen N. Rao Cc: Palmer Dabbelt Cc: Paul Walmsley Cc: Will Deacon Reviewed-by: Jarkko Sakkinen --- arch/Kconfig | 3 +++ arch/arm64/Kconfig | 1 + arch/arm64/kernel/probes/kprobes.c | 5 +++++ arch/powerpc/Kconfig | 3 ++- arch/powerpc/kernel/kprobes.c | 5 +++++ arch/riscv/Kconfig | 1 + arch/riscv/kernel/probes/kprobes.c | 5 +++++ arch/s390/Kconfig | 3 ++- arch/s390/kernel/kprobes.c | 5 +++++ arch/x86/Kconfig | 3 ++- arch/x86/kernel/kprobes/core.c | 5 +++++ include/linux/kprobes.h | 1 + kernel/kprobes.c | 6 ++++-- 13 files changed, 41 insertions(+), 5 deletions(-) diff --git a/arch/Kconfig b/arch/Kconfig index 9f066785bb71d..85bb59f7b8c07 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -206,6 +206,9 @@ config HAVE_IOREMAP_PROT config HAVE_KPROBES bool =20 +config HAVE_KPROBES_ALLOC + bool + config HAVE_KRETPROBES bool =20 diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 7b11c98b3e84b..bda7913d6c9b8 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -233,6 +233,7 @@ config ARM64 select HAVE_STACKPROTECTOR select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES + select HAVE_KPROBES_ALLOC select HAVE_KRETPROBES select HAVE_GENERIC_VDSO select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/= kprobes.c index 4b6ab7b1fa211..69d19a390cd48 100644 --- a/arch/arm64/kernel/probes/kprobes.c +++ b/arch/arm64/kernel/probes/kprobes.c @@ -136,6 +136,11 @@ void *kprobes_alloc_insn_page(void) NUMA_NO_NODE, __builtin_return_address(0)); } =20 +void kprobes_free_insn_page(void *page) +{ + vfree(page); +} + /* arm kprobe: install breakpoint in text */ void __kprobes arch_arm_kprobe(struct kprobe *p) { diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index 1c4be33736860..13e0fc51dcdcf 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -254,7 +254,8 @@ config PPC select HAVE_KERNEL_LZMA if DEFAULT_UIMAGE select HAVE_KERNEL_LZO if DEFAULT_UIMAGE select HAVE_KERNEL_XZ if PPC_BOOK3S || 44x - select HAVE_KPROBES + select HAVE_KPROBES if MODULES + select HAVE_KPROBES_ALLOC select HAVE_KPROBES_ON_FTRACE select HAVE_KRETPROBES select HAVE_LD_DEAD_CODE_DATA_ELIMINATION if HAVE_OBJTOOL_MCOUNT && (!ARC= H_USING_PATCHABLE_FUNCTION_ENTRY || (!CC_IS_GCC || GCC_VERSION >=3D 110100)) diff --git a/arch/powerpc/kernel/kprobes.c b/arch/powerpc/kernel/kprobes.c index 0b297718d5de6..d0332aaebab09 100644 --- a/arch/powerpc/kernel/kprobes.c +++ b/arch/powerpc/kernel/kprobes.c @@ -146,6 +146,11 @@ void *kprobes_alloc_insn_page(void) return NULL; } =20 +void kprobes_free_insn_page(void *page) +{ + module_memfree(page); +} + int arch_prepare_kprobe(struct kprobe *p) { int ret =3D 0; diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index be09c8836d56b..4e22549a522a5 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -139,6 +139,7 @@ config RISCV select HAVE_GENERIC_VDSO if MMU && 64BIT select HAVE_IRQ_TIME_ACCOUNTING select HAVE_KPROBES if !XIP_KERNEL + select HAVE_KPROBES_ALLOC select HAVE_KPROBES_ON_FTRACE if !XIP_KERNEL select HAVE_KRETPROBES if !XIP_KERNEL # https://github.com/ClangBuiltLinux/linux/issues/1881 diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/= kprobes.c index 75201ce721057..37fdfa952d999 100644 --- a/arch/riscv/kernel/probes/kprobes.c +++ b/arch/riscv/kernel/probes/kprobes.c @@ -112,6 +112,11 @@ void *kprobes_alloc_insn_page(void) VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, __builtin_return_address(0)); } + +void kprobes_free_insn_page(void *page) +{ + vfree(page); +} #endif =20 /* install breakpoint in text */ diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig index 8f01ada6845e3..635eddc3fce80 100644 --- a/arch/s390/Kconfig +++ b/arch/s390/Kconfig @@ -193,7 +193,8 @@ config S390 select HAVE_KERNEL_UNCOMPRESSED select HAVE_KERNEL_XZ select HAVE_KERNEL_ZSTD - select HAVE_KPROBES + select HAVE_KPROBES if MODULES + select HAVE_KPROBES_ALLOC select HAVE_KPROBES_ON_FTRACE select HAVE_KRETPROBES select HAVE_LIVEPATCH diff --git a/arch/s390/kernel/kprobes.c b/arch/s390/kernel/kprobes.c index 91ca4d501d4ef..a5b142b8eb0f7 100644 --- a/arch/s390/kernel/kprobes.c +++ b/arch/s390/kernel/kprobes.c @@ -45,6 +45,11 @@ void *kprobes_alloc_insn_page(void) return page; } =20 +void kprobes_free_insn_page(void *page) +{ + module_memfree(page); +} + static void *alloc_s390_insn_page(void) { if (xchg(&insn_page_in_use, 1) =3D=3D 1) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 4fff6ed46e902..0810cd0bdeca9 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -240,7 +240,8 @@ config X86 select HAVE_KERNEL_LZO select HAVE_KERNEL_XZ select HAVE_KERNEL_ZSTD - select HAVE_KPROBES + select HAVE_KPROBES if MODULES + select HAVE_KPROBES_ALLOC select HAVE_KPROBES_ON_FTRACE select HAVE_FUNCTION_ERROR_INJECTION select HAVE_KRETPROBES diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index 7f01bbbfa9e2a..5f093b94d9b40 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -508,6 +508,11 @@ void *kprobes_alloc_insn_page(void) return page; } =20 +void kprobes_free_insn_page(void *page) +{ + module_memfree(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/include/linux/kprobes.h b/include/linux/kprobes.h index ad4b561100f9e..651c807727bea 100644 --- a/include/linux/kprobes.h +++ b/include/linux/kprobes.h @@ -431,6 +431,7 @@ int enable_kprobe(struct kprobe *kp); void dump_kprobe(struct kprobe *kp); =20 void *kprobes_alloc_insn_page(void); +void kprobes_free_insn_page(void *page); =20 void *kprobes_alloc_optinsn_page(void); void kprobes_free_optinsn_page(void *page); diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 35adf56430c9b..fa2ee4e59eca2 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -110,7 +110,8 @@ enum kprobe_slot_state { SLOT_USED =3D 2, }; =20 -void __weak *kprobes_alloc_insn_page(void) +#ifndef CONFIG_HAVE_KPROBES_ALLOC +void *kprobes_alloc_insn_page(void) { /* * Use module_alloc() so this page is within +/- 2GB of where the @@ -121,10 +122,11 @@ void __weak *kprobes_alloc_insn_page(void) return module_alloc(PAGE_SIZE); } =20 -static void kprobes_free_insn_page(void *page) +void kprobes_free_insn_page(void *page) { module_memfree(page); } +#endif =20 struct kprobe_insn_cache kprobe_insn_slots =3D { .mutex =3D __MUTEX_INITIALIZER(kprobe_insn_slots.mutex), --=20 2.30.2 From nobody Sun Feb 8 08:48:01 2026 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E41C814A4FF for ; Wed, 3 Apr 2024 15:02:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712156533; cv=none; b=SyhBU6XooknX3gRuOs07zHRVRzMh7beuuGIVNFGhqr4AGLLjwWgsfYxBrYypDjMoeQYb0qz2IA9MTNCecHKUKhtNn2Xjs0MZRI6/3zXBoQvk4CPurN2jgH0HyktAasXbtv5z5SqXkmne20yUuvU6JYsWNDiWLBjuFkhqOCHscpg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712156533; c=relaxed/simple; bh=3TQz803SJKKWWDXdmpXJtdnVe8cb7gnbBOh1swoHYpo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YDabxlZjnFFywA5AoD5XMKphluFc0PVraBOvrG0HJx0t3MnqIOuA1OkRyG9UByAThMO4+mVbo9FtG/NSmcNeYM/Q9oFbi/dkJr9wlOzavyDjJPTn+rU1EFSc0ADzbaYscqVKMjFyA3oWtNHum4rroabTRbpmjAoESR+o6YgU/k8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9E98A1595; Wed, 3 Apr 2024 08:02:42 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id F00113F7B4; Wed, 3 Apr 2024 08:02:09 -0700 (PDT) From: Mark Rutland To: linux-kernel@vger.kernel.org Cc: anil.s.keshavamurthy@intel.com, aou@eecs.berkeley.edu, catalin.marinas@arm.com, davem@davemloft.net, jarkko@kernel.org, linux-arm-kernel@lists.infradead.org, mark.rutland@arm.com, mhiramat@kernel.org, naveen.n.rao@linux.ibm.com, palmer@dabbelt.com, paul.walmsley@sifive.com, will@kernel.org Subject: [PATCH v2 4/4] kprobes: Remove core dependency on modules Date: Wed, 3 Apr 2024 16:01:54 +0100 Message-Id: <20240403150154.667649-5-mark.rutland@arm.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240403150154.667649-1-mark.rutland@arm.com> References: <20240403150154.667649-1-mark.rutland@arm.com> 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: Jarkko Sakkinen Tracing with kprobes while running a monolithic kernel is currently impossible because KPROBES depends on MODULES. While this dependency is necessary when HAVE_KPROBES_ALLOC=3Dn and the core kprobes code allocates memory using module_alloc(), all the other module-specific code only exist to handle the case when MODULES=3Dy, and can be hidden behind ifdeffery. Add the necessary ifdeffery, and remove the dependency on MODULES=3Dy when HAVE_KPROBES_ALLOC=3Dy. As of this patch kprobes can be used when MODULES=3Dn on arm64 and riscv. All other architectures still depend on MODULES, either by virtue of the core dependency on MODULES when HAVE_KPROBES_ALLOC=3Dn, or by virtue of an explciit dependency on MODULES in arch code. Other architectures can enable support by implementing their own kprobes_alloc_insn_page() and kprobes_free_insn_page() which do not depend on MODULES. Signed-off-by: Jarkko Sakkinen Link: https://lore.kernel.org/lkml/20240326134616.7691-1-jarkko@kernel.org/ [Mark: Remove execmem changes, depend on HAVE_KPROBES_ALLOC] Signed-off-by: Mark Rutland Cc: Albert Ou Cc: Anil S Keshavamurthy Cc: Catalin Marinas Cc: David S. Miller Cc: Jarkko Sakkinen Cc: Masami Hiramatsu Cc: Naveen N. Rao Cc: Palmer Dabbelt Cc: Paul Walmsley Cc: Will Deacon Tested-by: Jarkko Sakkinen # arch/riscv --- arch/Kconfig | 2 +- kernel/kprobes.c | 46 ++++++++++++++++++++++--------------- kernel/trace/trace_kprobe.c | 15 ++++++++++-- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/arch/Kconfig b/arch/Kconfig index 85bb59f7b8c07..0df2c88547b3c 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -52,7 +52,7 @@ config GENERIC_ENTRY =20 config KPROBES bool "Kprobes" - depends on MODULES + depends on MODULES || HAVE_KPROBES_ALLOC depends on HAVE_KPROBES select KALLSYMS select TASKS_RCU if PREEMPTION diff --git a/kernel/kprobes.c b/kernel/kprobes.c index fa2ee4e59eca2..ec4493a41b505 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1594,6 +1594,7 @@ static int check_kprobe_address_safe(struct kprobe *p, goto out; } =20 +#ifdef CONFIG_MODULES /* * If the module freed '.init.text', we couldn't insert * kprobes in there. @@ -1604,7 +1605,9 @@ static int check_kprobe_address_safe(struct kprobe *p, *probed_mod =3D NULL; ret =3D -ENOENT; } +#endif /* CONFIG_MODULES */ } + out: preempt_enable(); jump_label_unlock(); @@ -2484,24 +2487,6 @@ int kprobe_add_area_blacklist(unsigned long start, u= nsigned long end) return 0; } =20 -/* Remove all symbols in given area from kprobe blacklist */ -static void kprobe_remove_area_blacklist(unsigned long start, unsigned lon= g 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 >=3D 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 *va= lue, char *type, char *sym) { @@ -2566,6 +2551,25 @@ static int __init populate_kprobe_blacklist(unsigned= long *start, return ret ? : arch_populate_kprobe_blacklist(); } =20 +#ifdef CONFIG_MODULES +/* Remove all symbols in given area from kprobe blacklist */ +static void kprobe_remove_area_blacklist(unsigned long start, unsigned lon= g 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 >=3D 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; @@ -2662,6 +2666,9 @@ static int kprobes_module_callback(struct notifier_bl= ock *nb, mutex_unlock(&kprobe_mutex); return NOTIFY_DONE; } +#else +#define kprobes_module_callback (NULL) +#endif /* CONFIG_MODULES */ =20 static struct notifier_block kprobe_module_nb =3D { .notifier_call =3D kprobes_module_callback, @@ -2726,7 +2733,8 @@ static int __init init_kprobes(void) err =3D arch_init_kprobes(); if (!err) err =3D register_die_notifier(&kprobe_exceptions_nb); - if (!err) + + if (!err && IS_ENABLED(CONFIG_MODULES)) err =3D register_module_notifier(&kprobe_module_nb); =20 kprobes_initialized =3D (err =3D=3D 0); diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 14099cc17fc9e..c509ba776e679 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) =3D=3D 0 && name[len] =3D=3D = ':'; } =20 +#ifdef CONFIG_MODULES static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe = *tk) { char *p; @@ -129,6 +130,9 @@ static nokprobe_inline bool trace_kprobe_module_exist(s= truct trace_kprobe *tk) =20 return ret; } +#else +#define trace_kprobe_module_exist(tk) false /* aka a module never exists */ +#endif /* CONFIG_MODULES */ =20 static bool trace_kprobe_is_busy(struct dyn_event *ev) { @@ -670,6 +674,7 @@ static int register_trace_kprobe(struct trace_kprobe *t= k) return ret; } =20 +#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) @@ -699,6 +704,9 @@ static int trace_kprobe_module_callback(struct notifier= _block *nb, =20 return NOTIFY_DONE; } +#else +#define trace_kprobe_module_callback (NULL) +#endif /* CONFIG_MODULES */ =20 static struct notifier_block trace_kprobe_module_nb =3D { .notifier_call =3D trace_kprobe_module_callback, @@ -1933,8 +1941,11 @@ static __init int init_kprobe_trace_early(void) if (ret) return ret; =20 - if (register_module_notifier(&trace_kprobe_module_nb)) - return -EINVAL; + if (IS_ENABLED(CONFIG_MODULES)) { + ret =3D register_module_notifier(&trace_kprobe_module_nb); + if (ret) + return -EINVAL; + } =20 return 0; } --=20 2.30.2