From nobody Mon Feb 9 06:34:41 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 12ED518BBBB for ; Tue, 29 Apr 2025 11:45:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745927130; cv=none; b=jdyav6aReWDALH7YEFzui+8MrRu1DAjBXYnqwWnK0kFs4WDhoFuesSz9fd036nOoCd2U1sYQr2fZt4g1nGpClVd/NIEWwpJiCLjQC2MU4reSJBAqnl2krTySWM7g7H3PH0C8qTxBmt7G3/qYOp566dRenZ9x8B8cViyfG18XLII= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745927130; c=relaxed/simple; bh=NJfpXMbwdIjKMcwhuZUnZrajNhJDS7nuZLz4u6h1L98=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FgrTRH56EBMzAeMtXv3bEDkgRVwsPD7yQNiOvv3V5q68DGaSNSYGORh9glpEdgcHyvLpGlxor+qikmh9gitb4eYrzui0d8lKynn0UVO5ZTomf8Umno+IgOoSXfm/b7K1r4My3UL8fsP+M9fuXthXDGUlVYpKdeOdsbbqCOlsTeM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Vp7KN/DS; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Vp7KN/DS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1745927127; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6eRqqbGq1uFGChwVCp3H62kpC0FqCEBM1hCsGgM03Z0=; b=Vp7KN/DSprbslTpbyFtmIcm8sFVcAHtgXOjSt27sTR4E2DjezN65U0f17gWTIGLckAP20n IdXrsYzJ8Pg3cgEPpWAI7fr80/wdI26v1IbZWBYS3IbGuWgOACfzbpHz3PXMAI7n083Cxa 5on+KBxPa1hiNasd1uLJ8CidY831JOU= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-422-Wh1l12g2O4OqWBygOwaAeg-1; Tue, 29 Apr 2025 07:45:25 -0400 X-MC-Unique: Wh1l12g2O4OqWBygOwaAeg-1 X-Mimecast-MFC-AGG-ID: Wh1l12g2O4OqWBygOwaAeg_1745927120 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B668E1800984; Tue, 29 Apr 2025 11:45:20 +0000 (UTC) Received: from vschneid-thinkpadt14sgen2i.remote.csb (unknown [10.45.225.102]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1DB8F19560A3; Tue, 29 Apr 2025 11:44:51 +0000 (UTC) From: Valentin Schneider To: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linux-riscv@lists.infradead.org, linux-perf-users@vger.kernel.org, kvm@vger.kernel.org, linux-arch@vger.kernel.org, linux-modules@vger.kernel.org, linux-trace-kernel@vger.kernel.org, rcu@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kselftest@vger.kernel.org, bpf@vger.kernel.org Cc: Juri Lelli , Marcelo Tosatti , Yair Podemsky , Josh Poimboeuf , Daniel Wagner , Petr Tesarik , Nicolas Saenz Julienne , Frederic Weisbecker , "Paul E. McKenney" , Dave Hansen , Sean Christopherson , Juergen Gross , Ajay Kaher , Alexey Makhalov , Broadcom internal kernel review list , Russell King , Catalin Marinas , Will Deacon , Huacai Chen , WANG Xuerui , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Pawan Gupta , Paolo Bonzini , Arnd Bergmann , Jason Baron , Steven Rostedt , Ard Biesheuvel , Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Naveen N Rao , Anil S Keshavamurthy , "David S. Miller" , Masami Hiramatsu , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Uladzislau Rezki , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Vincent Guittot , Dietmar Eggemann , Ben Segall , Mel Gorman , Kees Cook , Shuah Khan , Masahiro Yamada , Alice Ryhl , Miguel Ojeda , "Mike Rapoport (Microsoft)" , Rong Xu , Rafael Aquini , Song Liu , Andrii Nakryiko , Dan Carpenter , Brian Gerst , "Kirill A. Shutemov" , Benjamin Berg , Vishal Annapurve , Randy Dunlap , John Stultz , Tiezhu Yang Subject: [PATCH v5 23/25] module: Add MOD_NOINSTR_TEXT mem_type Date: Tue, 29 Apr 2025 13:32:40 +0200 Message-ID: <20250429113242.998312-24-vschneid@redhat.com> In-Reply-To: <20250429113242.998312-1-vschneid@redhat.com> References: <20250429113242.998312-1-vschneid@redhat.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 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" As pointed out by Sean [1], is_kernel_noinstr_text() will return false for an address contained within a module's .noinstr.text section. A later patch will require checking whether a text address is noinstr, and this can unfortunately be the case of modules - KVM is one such case. A module's .noinstr.text section is already tracked as of commit 66e9b0717102 ("kprobes: Prevent probes in .noinstr.text section") for kprobe blacklisting purposes, but via an ad-hoc mechanism. Add a MOD_NOINSTR_TEXT mem_type, and reorganize __layout_sections() so that it maps all the sections in a single invocation. [1]: http://lore.kernel.org/r/Z4qQL89GZ_gk0vpu@google.com Signed-off-by: Valentin Schneider --- include/linux/module.h | 6 ++-- kernel/kprobes.c | 8 ++--- kernel/module/main.c | 76 ++++++++++++++++++++++++++++++++---------- 3 files changed, 66 insertions(+), 24 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index d94b196d5a34e..193d8d34eeee0 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -332,6 +332,7 @@ struct mod_tree_node { =20 enum mod_mem_type { MOD_TEXT =3D 0, + MOD_NOINSTR_TEXT, MOD_DATA, MOD_RODATA, MOD_RO_AFTER_INIT, @@ -502,8 +503,6 @@ struct module { void __percpu *percpu; unsigned int percpu_size; #endif - void *noinstr_text_start; - unsigned int noinstr_text_size; =20 #ifdef CONFIG_TRACEPOINTS unsigned int num_tracepoints; @@ -622,12 +621,13 @@ static inline bool module_is_coming(struct module *mo= d) return mod->state =3D=3D MODULE_STATE_COMING; } =20 -struct module *__module_text_address(unsigned long addr); struct module *__module_address(unsigned long addr); +struct module *__module_text_address(unsigned long addr); bool is_module_address(unsigned long addr); bool __is_module_percpu_address(unsigned long addr, unsigned long *can_add= r); bool is_module_percpu_address(unsigned long addr); bool is_module_text_address(unsigned long addr); +bool is_module_noinstr_text_address(unsigned long addr); =20 static inline bool within_module_mem_type(unsigned long addr, const struct module *mod, diff --git a/kernel/kprobes.c b/kernel/kprobes.c index ffe0c3d523063..9a799faee68a1 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -2547,9 +2547,9 @@ static void add_module_kprobe_blacklist(struct module= *mod) kprobe_add_area_blacklist(start, end); } =20 - start =3D (unsigned long)mod->noinstr_text_start; + start =3D (unsigned long)mod->mem[MOD_NOINSTR_TEXT].base; if (start) { - end =3D start + mod->noinstr_text_size; + end =3D start + mod->mem[MOD_NOINSTR_TEXT].size; kprobe_add_area_blacklist(start, end); } } @@ -2570,9 +2570,9 @@ static void remove_module_kprobe_blacklist(struct mod= ule *mod) kprobe_remove_area_blacklist(start, end); } =20 - start =3D (unsigned long)mod->noinstr_text_start; + start =3D (unsigned long)mod->mem[MOD_NOINSTR_TEXT].base; if (start) { - end =3D start + mod->noinstr_text_size; + end =3D start + mod->mem[MOD_NOINSTR_TEXT].size; kprobe_remove_area_blacklist(start, end); } } diff --git a/kernel/module/main.c b/kernel/module/main.c index b9f010daaa4c7..0126bae64b698 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1558,7 +1558,17 @@ bool module_init_layout_section(const char *sname) return module_init_section(sname); } =20 -static void __layout_sections(struct module *mod, struct load_info *info, = bool is_init) +static bool module_noinstr_layout_section(const char *sname) +{ + return strstarts(sname, ".noinstr"); +} + +static bool module_default_layout_section(const char *sname) +{ + return !module_init_layout_section(sname) && !module_noinstr_layout_secti= on(sname); +} + +static void __layout_sections(struct module *mod, struct load_info *info) { unsigned int m, i; =20 @@ -1567,20 +1577,44 @@ static void __layout_sections(struct module *mod, s= truct load_info *info, bool i * Mask of excluded section header flags } */ static const unsigned long masks[][2] =3D { + /* Core */ + { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL }, + { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL }, + { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL }, + { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL }, + { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL }, + { ARCH_SHF_SMALL | SHF_ALLOC, 0 }, + /* Init */ { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL }, { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL }, { SHF_RO_AFTER_INIT | SHF_ALLOC, ARCH_SHF_SMALL }, { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL }, - { ARCH_SHF_SMALL | SHF_ALLOC, 0 } + { ARCH_SHF_SMALL | SHF_ALLOC, 0 }, }; - static const int core_m_to_mem_type[] =3D { + static bool (*const section_filter[])(const char *) =3D { + /* Core */ + module_default_layout_section, + module_noinstr_layout_section, + module_default_layout_section, + module_default_layout_section, + module_default_layout_section, + module_default_layout_section, + /* Init */ + module_init_layout_section, + module_init_layout_section, + module_init_layout_section, + module_init_layout_section, + module_init_layout_section, + }; + static const int mem_type_map[] =3D { + /* Core */ MOD_TEXT, + MOD_NOINSTR_TEXT, MOD_RODATA, MOD_RO_AFTER_INIT, MOD_DATA, MOD_DATA, - }; - static const int init_m_to_mem_type[] =3D { + /* Init */ MOD_INIT_TEXT, MOD_INIT_RODATA, MOD_INVALID, @@ -1589,16 +1623,16 @@ static void __layout_sections(struct module *mod, s= truct load_info *info, bool i }; =20 for (m =3D 0; m < ARRAY_SIZE(masks); ++m) { - enum mod_mem_type type =3D is_init ? init_m_to_mem_type[m] : core_m_to_m= em_type[m]; + enum mod_mem_type type =3D mem_type_map[m]; =20 for (i =3D 0; i < info->hdr->e_shnum; ++i) { Elf_Shdr *s =3D &info->sechdrs[i]; const char *sname =3D info->secstrings + s->sh_name; =20 - if ((s->sh_flags & masks[m][0]) !=3D masks[m][0] - || (s->sh_flags & masks[m][1]) - || s->sh_entsize !=3D ~0UL - || is_init !=3D module_init_layout_section(sname)) + if ((s->sh_flags & masks[m][0]) !=3D masks[m][0] || + (s->sh_flags & masks[m][1]) || + s->sh_entsize !=3D ~0UL || + !section_filter[m](sname)) continue; =20 if (WARN_ON_ONCE(type =3D=3D MOD_INVALID)) @@ -1638,10 +1672,7 @@ static void layout_sections(struct module *mod, stru= ct load_info *info) info->sechdrs[i].sh_entsize =3D ~0UL; =20 pr_debug("Core section allocation order for %s:\n", mod->name); - __layout_sections(mod, info, false); - - pr_debug("Init section allocation order for %s:\n", mod->name); - __layout_sections(mod, info, true); + __layout_sections(mod, info); } =20 static void module_license_taint_check(struct module *mod, const char *lic= ense) @@ -2515,9 +2546,6 @@ static int find_module_sections(struct module *mod, s= truct load_info *info) } #endif =20 - mod->noinstr_text_start =3D section_objs(info, ".noinstr.text", 1, - &mod->noinstr_text_size); - #ifdef CONFIG_TRACEPOINTS mod->tracepoints_ptrs =3D section_objs(info, "__tracepoints_ptrs", sizeof(*mod->tracepoints_ptrs), @@ -3769,12 +3797,26 @@ struct module *__module_text_address(unsigned long = addr) if (mod) { /* Make sure it's within the text section. */ if (!within_module_mem_type(addr, mod, MOD_TEXT) && + !within_module_mem_type(addr, mod, MOD_NOINSTR_TEXT) && !within_module_mem_type(addr, mod, MOD_INIT_TEXT)) mod =3D NULL; } return mod; } =20 +bool is_module_noinstr_text_address(unsigned long addr) +{ + scoped_guard(preempt) { + struct module *mod =3D __module_address(addr); + + /* Make sure it's within the .noinstr.text section. */ + if (mod) + return within_module_mem_type(addr, mod, MOD_NOINSTR_TEXT); + } + + return false; +} + /* Don't grab lock, we're oopsing. */ void print_modules(void) { --=20 2.49.0