# \[PATCH v2 0/7\] sections: Unify kernel sections range check and use

- Subsystems: LINUX FOR POWERPC (32-BIT AND 64-BIT), BPF JIT for X86 64-BIT, BPF \[GENERAL\] (Safe Dynamic Programs and Tools), CLANG CONTROL FLOW INTEGRITY SUPPORT, FUNCTION HOOKS (FTRACE), GENERIC INCLUDE/ASM HEADER FILES, KASAN, LOCKING PRIMITIVES, MEMORY MANAGEMENT, NETWORKING \[GENERAL\], THE REST, TRACING, X86 ARCHITECTURE (32-BIT AND 64-BIT), X86 STACK UNWINDING
- Maintainers: Madhavan Srinivasan, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi, Sami Tolvanen, Kees Cook, Steven Rostedt, Masami Hiramatsu, Arnd Bergmann, Andrey Ryabinin, Peter Zijlstra, Ingo Molnar, Will Deacon, Boqun Feng, Andrew Morton, "David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, Linus Torvalds, Thomas Gleixner, Borislav Petkov, Dave Hansen, Josh Poimboeuf
- Series: 7 patches touching 12 files, +76/-81
- Status: SUPERSEDED (Superseded by v4 posted 2021-09-30)
- Superseded by v4 (posted 2021-09-30).
- Revision v2 (4 revisions archived).
- List: linuxppc-dev
- 16 messages, 2 authors
- Page: https://ratatoskr.run/linuxppc-dev/2021/07/1460478/t

## \[PATCH v2 0/7\] sections: Unify kernel sections range check and use

- From: Kefeng Wang \<hidden\>
- Date: 2021-07-28 08:07:16 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1460478.md

```text
There are three head files(kallsyms.h, kernel.h and sections.h) which
include the kernel sections range check, let's make some cleanup and
unify them.

1. cleanup arch specific text/data check and fix address boundary check
   in kallsyms.h
2. make all the basic/core kernel range check function into sections.h
3. update all the callers, and use the helper in sections.h to simplify
   the code

After this series, we have 5 APIs about kernel sections range check in
sections.h

 * is_kernel_core_data()	--- come from core_kernel_data() in kernel.h
 * is_kernel_rodata()		--- already in sections.h
 * is_kernel_text()		--- come from kallsyms.h
 * is_kernel_inittext()		--- come from kernel.h and kallsyms.h
 * is_kernel()			--- come from kallsyms.h


Cc: <redacted>
Cc: <redacted>
Cc: <redacted> 
Cc: <redacted>
Cc: <redacted> 

v2:
- add ACK/RW to patch2, and drop inappropriate fix tag
- keep 'core' to check kernel data, suggestted by Steven Rostedt
  [ref], rename is_kernel_data() to is_kernel_core_data()
- drop patch8 which is merged
- drop patch9 which is resend independently

v1:
https://lore.kernel.org/linux-arch/20210626073439.150586-1-wangkefeng.wang@huawei.com

Kefeng Wang (7):
  kallsyms: Remove arch specific text and data check
  kallsyms: Fix address-checks for kernel related range
  sections: Move and rename core_kernel_data() to is_kernel_core_data()
  sections: Move is_kernel_inittext() into sections.h
  kallsyms: Rename is_kernel() and is_kernel_text()
  sections: Add new is_kernel() and is_kernel_text()
  powerpc/mm: Use is_kernel_text() and is_kernel_inittext() helper

 arch/powerpc/mm/pgtable_32.c   |  7 +---
 arch/x86/kernel/unwind_orc.c   |  2 +-
 arch/x86/net/bpf_jit_comp.c    |  2 +-
 include/asm-generic/sections.h | 71 ++++++++++++++++++++++++++--------
 include/linux/kallsyms.h       | 21 +++-------
 include/linux/kernel.h         |  2 -
 kernel/cfi.c                   |  2 +-
 kernel/extable.c               | 33 ++--------------
 kernel/locking/lockdep.c       |  3 --
 kernel/trace/ftrace.c          |  2 +-
 mm/kasan/report.c              |  2 +-
 net/sysctl_net.c               |  2 +-
 12 files changed, 72 insertions(+), 77 deletions(-)

-- 
2.26.2

```

## \[PATCH v2 1/7\] kallsyms: Remove arch specific text and data check

- From: Kefeng Wang \<hidden\>
- Date: 2021-07-28 08:07:18 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1460479.md

```text
After commit 4ba66a976072 ("arch: remove blackfin port"),
no need arch-specific text/data check.

Cc: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Kefeng Wang <redacted>
---
 include/asm-generic/sections.h | 16 ----------------
 include/linux/kallsyms.h       |  3 +--
 kernel/locking/lockdep.c       |  3 ---
 3 files changed, 1 insertion(+), 21 deletions(-)

diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h
index d16302d3eb59..817309e289db 100644
--- a/include/asm-generic/sections.h
+++ b/include/asm-generic/sections.h
@@ -64,22 +64,6 @@ extern __visible const void __nosave_begin, __nosave_end;
 #define dereference_kernel_function_descriptor(p) ((void *)(p))
 #endif
 
-/* random extra sections (if any).  Override
- * in asm/sections.h */
-#ifndef arch_is_kernel_text
-static inline int arch_is_kernel_text(unsigned long addr)
-{
-	return 0;
-}
-#endif
-
-#ifndef arch_is_kernel_data
-static inline int arch_is_kernel_data(unsigned long addr)
-{
-	return 0;
-}
-#endif
-
 /*
  * Check if an address is part of freed initmem. This is needed on architectures
  * with virt == phys kernel mapping, for code that wants to check if an address
diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h
index 6851c2313cad..2a241e3f063f 100644
--- a/include/linux/kallsyms.h
+++ b/include/linux/kallsyms.h
@@ -34,8 +34,7 @@ static inline int is_kernel_inittext(unsigned long addr)
 
 static inline int is_kernel_text(unsigned long addr)
 {
-	if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext) ||
-	    arch_is_kernel_text(addr))
+	if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext))
 		return 1;
 	return in_gate_area_no_mm(addr);
 }
diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
index bf1c00c881e4..64b17e995108 100644
--- a/kernel/locking/lockdep.c
+++ b/kernel/locking/lockdep.c
@@ -803,9 +803,6 @@ static int static_obj(const void *obj)
 	if ((addr >= start) && (addr < end))
 		return 1;
 
-	if (arch_is_kernel_data(addr))
-		return 1;
-
 	/*
 	 * in-kernel percpu var?
 	 */
-- 
2.26.2

```

## \[PATCH v2 4/7\] sections: Move is\_kernel\_inittext() into sections.h

- From: Kefeng Wang \<hidden\>
- Date: 2021-07-28 08:07:20 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1460480.md

```text
The is_kernel_inittext() and init_kernel_text() are with same
functionality, let's just keep is_kernel_inittext() and move
it into sections.h, then update all the callers.

Cc: Ingo Molnar <mingo@redhat.com>
Cc: Thomas Gleixner <redacted>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: x86@kernel.org
Signed-off-by: Kefeng Wang <redacted>
---
 arch/x86/kernel/unwind_orc.c   |  2 +-
 include/asm-generic/sections.h | 14 ++++++++++++++
 include/linux/kallsyms.h       |  8 --------
 include/linux/kernel.h         |  1 -
 kernel/extable.c               | 12 ++----------
 5 files changed, 17 insertions(+), 20 deletions(-)

diff --git a/arch/x86/kernel/unwind_orc.c b/arch/x86/kernel/unwind_orc.c
index a1202536fc57..d92ec2ced059 100644
--- a/arch/x86/kernel/unwind_orc.c
+++ b/arch/x86/kernel/unwind_orc.c
@@ -175,7 +175,7 @@ static struct orc_entry *orc_find(unsigned long ip)
 	}
 
 	/* vmlinux .init slow lookup: */
-	if (init_kernel_text(ip))
+	if (is_kernel_inittext(ip))
 		return __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
 				  __stop_orc_unwind_ip - __start_orc_unwind_ip, ip);
 
diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h
index 26ed9fc9b4e3..4f2f32aa2b7a 100644
--- a/include/asm-generic/sections.h
+++ b/include/asm-generic/sections.h
@@ -170,4 +170,18 @@ static inline bool is_kernel_rodata(unsigned long addr)
 	       addr < (unsigned long)__end_rodata;
 }
 
+/**
+ * is_kernel_inittext - checks if the pointer address is located in the
+ *                    .init.text section
+ *
+ * @addr: address to check
+ *
+ * Returns: true if the address is located in .init.text, false otherwise.
+ */
+static inline bool is_kernel_inittext(unsigned long addr)
+{
+	return addr >= (unsigned long)_sinittext &&
+	       addr < (unsigned long)_einittext;
+}
+
 #endif /* _ASM_GENERIC_SECTIONS_H_ */
diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h
index b016c62f30a6..8a9d329c927c 100644
--- a/include/linux/kallsyms.h
+++ b/include/linux/kallsyms.h
@@ -24,14 +24,6 @@
 struct cred;
 struct module;
 
-static inline int is_kernel_inittext(unsigned long addr)
-{
-	if (addr >= (unsigned long)_sinittext
-	    && addr < (unsigned long)_einittext)
-		return 1;
-	return 0;
-}
-
 static inline int is_kernel_text(unsigned long addr)
 {
 	if ((addr >= (unsigned long)_stext && addr < (unsigned long)_etext))
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 0622418bafbc..d4ba46cf4737 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -229,7 +229,6 @@ extern bool parse_option_str(const char *str, const char *option);
 extern char *next_arg(char *args, char **param, char **val);
 
 extern int core_kernel_text(unsigned long addr);
-extern int init_kernel_text(unsigned long addr);
 extern int __kernel_text_address(unsigned long addr);
 extern int kernel_text_address(unsigned long addr);
 extern int func_ptr_is_kernel_text(void *ptr);
diff --git a/kernel/extable.c b/kernel/extable.c
index da26203841d4..98ca627ac5ef 100644
--- a/kernel/extable.c
+++ b/kernel/extable.c
@@ -62,14 +62,6 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr)
 	return e;
 }
 
-int init_kernel_text(unsigned long addr)
-{
-	if (addr >= (unsigned long)_sinittext &&
-	    addr < (unsigned long)_einittext)
-		return 1;
-	return 0;
-}
-
 int notrace core_kernel_text(unsigned long addr)
 {
 	if (addr >= (unsigned long)_stext &&
@@ -77,7 +69,7 @@ int notrace core_kernel_text(unsigned long addr)
 		return 1;
 
 	if (system_state < SYSTEM_RUNNING &&
-	    init_kernel_text(addr))
+	    is_kernel_inittext(addr))
 		return 1;
 	return 0;
 }
@@ -94,7 +86,7 @@ int __kernel_text_address(unsigned long addr)
 	 * Since we are after the module-symbols check, there's
 	 * no danger of address overlap:
 	 */
-	if (init_kernel_text(addr))
+	if (is_kernel_inittext(addr))
 		return 1;
 	return 0;
 }
-- 
2.26.2

```

## \[PATCH v2 7/7\] powerpc/mm: Use is\_kernel\_text() and is\_kernel\_inittext() helper

- From: Kefeng Wang \<hidden\>
- Date: 2021-07-28 08:07:23 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1460481.md

```text
Use is_kernel_text() and is_kernel_inittext() helper to simplify code,
also drop etext, _stext, _sinittext, _einittext declaration which
already declared in section.h.

Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <redacted>
Cc: <redacted>
Signed-off-by: Kefeng Wang <redacted>
---
 arch/powerpc/mm/pgtable_32.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/arch/powerpc/mm/pgtable_32.c b/arch/powerpc/mm/pgtable_32.c
index dcf5ecca19d9..13c798308c2e 100644
--- a/arch/powerpc/mm/pgtable_32.c
+++ b/arch/powerpc/mm/pgtable_32.c
@@ -33,8 +33,6 @@
 
 #include <mm/mmu_decl.h>
 
-extern char etext[], _stext[], _sinittext[], _einittext[];
-
 static u8 early_fixmap_pagetable[FIXMAP_PTE_SIZE] __page_aligned_data;
 
 notrace void __init early_ioremap_init(void)
@@ -104,14 +102,13 @@ static void __init __mapin_ram_chunk(unsigned long offset, unsigned long top)
 {
 	unsigned long v, s;
 	phys_addr_t p;
-	int ktext;
+	bool ktext;
 
 	s = offset;
 	v = PAGE_OFFSET + s;
 	p = memstart_addr + s;
 	for (; s < top; s += PAGE_SIZE) {
-		ktext = ((char *)v >= _stext && (char *)v < etext) ||
-			((char *)v >= _sinittext && (char *)v < _einittext);
+		ktext = (is_kernel_text(v) || is_kernel_inittext(v));
 		map_kernel_page(v, p, ktext ? PAGE_KERNEL_TEXT : PAGE_KERNEL);
 		v += PAGE_SIZE;
 		p += PAGE_SIZE;
-- 
2.26.2

```

## \[PATCH v2 6/7\] sections: Add new is\_kernel() and is\_kernel\_text()

- From: Kefeng Wang \<hidden\>
- Date: 2021-07-28 08:07:25 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1460482.md

```text
The new is_kernel() check the kernel address ranges, and the
new is_kernel_text() check the kernel text section ranges.

Then use them to make some code clear.

Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Signed-off-by: Kefeng Wang <redacted>
---
 include/asm-generic/sections.h | 27 +++++++++++++++++++++++++++
 include/linux/kallsyms.h       |  4 ++--
 kernel/extable.c               |  3 +--
 mm/kasan/report.c              |  2 +-
 4 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h
index 4f2f32aa2b7a..6b143637ab88 100644
--- a/include/asm-generic/sections.h
+++ b/include/asm-generic/sections.h
@@ -170,6 +170,20 @@ static inline bool is_kernel_rodata(unsigned long addr)
 	       addr < (unsigned long)__end_rodata;
 }
 
+/**
+ * is_kernel_text - checks if the pointer address is located in the
+ *                  .text section
+ *
+ * @addr: address to check
+ *
+ * Returns: true if the address is located in .text, false otherwise.
+ */
+static inline bool is_kernel_text(unsigned long addr)
+{
+	return addr >= (unsigned long)_stext &&
+	       addr < (unsigned long)_etext;
+}
+
 /**
  * is_kernel_inittext - checks if the pointer address is located in the
  *                    .init.text section
@@ -184,4 +198,17 @@ static inline bool is_kernel_inittext(unsigned long addr)
 	       addr < (unsigned long)_einittext;
 }
 
+/**
+ * is_kernel - checks if the pointer address is located in the kernel range
+ *
+ * @addr: address to check
+ *
+ * Returns: true if the address is located in kernel range, false otherwise.
+ */
+static inline bool is_kernel(unsigned long addr)
+{
+	return addr >= (unsigned long)_stext &&
+	       addr < (unsigned long)_end;
+}
+
 #endif /* _ASM_GENERIC_SECTIONS_H_ */
diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h
index 4f501ac9c2c2..897d5720884f 100644
--- a/include/linux/kallsyms.h
+++ b/include/linux/kallsyms.h
@@ -26,14 +26,14 @@ struct module;
 
 static inline int is_kernel_text_or_gate_area(unsigned long addr)
 {
-	if ((addr >= (unsigned long)_stext && addr < (unsigned long)_etext))
+	if (is_kernel_text(addr))
 		return 1;
 	return in_gate_area_no_mm(addr);
 }
 
 static inline int is_kernel_or_gate_area(unsigned long addr)
 {
-	if (addr >= (unsigned long)_stext && addr < (unsigned long)_end)
+	if (is_kernel(addr))
 		return 1;
 	return in_gate_area_no_mm(addr);
 }
diff --git a/kernel/extable.c b/kernel/extable.c
index 98ca627ac5ef..0ba383d850ff 100644
--- a/kernel/extable.c
+++ b/kernel/extable.c
@@ -64,8 +64,7 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr)
 
 int notrace core_kernel_text(unsigned long addr)
 {
-	if (addr >= (unsigned long)_stext &&
-	    addr < (unsigned long)_etext)
+	if (is_kernel_text(addr))
 		return 1;
 
 	if (system_state < SYSTEM_RUNNING &&
diff --git a/mm/kasan/report.c b/mm/kasan/report.c
index 884a950c7026..88f5b0c058b7 100644
--- a/mm/kasan/report.c
+++ b/mm/kasan/report.c
@@ -235,7 +235,7 @@ static void describe_object(struct kmem_cache *cache, void *object,
 
 static inline bool kernel_or_module_addr(const void *addr)
 {
-	if (addr >= (void *)_stext && addr < (void *)_end)
+	if (is_kernel((unsigned long)addr))
 		return true;
 	if (is_module_address((unsigned long)addr))
 		return true;
-- 
2.26.2

```

## \[PATCH v2 3/7\] sections: Move and rename core\_kernel\_data() to is\_kernel\_core\_data()

- From: Kefeng Wang \<hidden\>
- Date: 2021-07-28 08:07:30 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1460483.md

```text
Move core_kernel_data() into sections.h and rename it to
is_kernel_core_data(), also make it return bool value, then
update all the callers.

Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "David S. Miller" <davem@davemloft.net>
Signed-off-by: Kefeng Wang <redacted>
---
 include/asm-generic/sections.h | 14 ++++++++++++++
 include/linux/kernel.h         |  1 -
 kernel/extable.c               | 18 ------------------
 kernel/trace/ftrace.c          |  2 +-
 net/sysctl_net.c               |  2 +-
 5 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h
index 817309e289db..26ed9fc9b4e3 100644
--- a/include/asm-generic/sections.h
+++ b/include/asm-generic/sections.h
@@ -142,6 +142,20 @@ static inline bool init_section_intersects(void *virt, size_t size)
 	return memory_intersects(__init_begin, __init_end, virt, size);
 }
 
+/**
+ * is_kernel_core_data - checks if the pointer address is located in the
+ *			 .data section
+ *
+ * @addr: address to check
+ *
+ * Returns: true if the address is located in .data, false otherwise.
+ */
+static inline bool is_kernel_core_data(unsigned long addr)
+{
+	return addr >= (unsigned long)_sdata &&
+	       addr < (unsigned long)_edata;
+}
+
 /**
  * is_kernel_rodata - checks if the pointer address is located in the
  *                    .rodata section
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 1b2f0a7e00d6..0622418bafbc 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -230,7 +230,6 @@ extern char *next_arg(char *args, char **param, char **val);
 
 extern int core_kernel_text(unsigned long addr);
 extern int init_kernel_text(unsigned long addr);
-extern int core_kernel_data(unsigned long addr);
 extern int __kernel_text_address(unsigned long addr);
 extern int kernel_text_address(unsigned long addr);
 extern int func_ptr_is_kernel_text(void *ptr);
diff --git a/kernel/extable.c b/kernel/extable.c
index b0ea5eb0c3b4..da26203841d4 100644
--- a/kernel/extable.c
+++ b/kernel/extable.c
@@ -82,24 +82,6 @@ int notrace core_kernel_text(unsigned long addr)
 	return 0;
 }
 
-/**
- * core_kernel_data - tell if addr points to kernel data
- * @addr: address to test
- *
- * Returns true if @addr passed in is from the core kernel data
- * section.
- *
- * Note: On some archs it may return true for core RODATA, and false
- *  for others. But will always be true for core RW data.
- */
-int core_kernel_data(unsigned long addr)
-{
-	if (addr >= (unsigned long)_sdata &&
-	    addr < (unsigned long)_edata)
-		return 1;
-	return 0;
-}
-
 int __kernel_text_address(unsigned long addr)
 {
 	if (kernel_text_address(addr))
diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index e6fb3e6e1ffc..d01ca1cb2d5f 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -323,7 +323,7 @@ int __register_ftrace_function(struct ftrace_ops *ops)
 	if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
 		return -EBUSY;
 
-	if (!core_kernel_data((unsigned long)ops))
+	if (!is_kernel_core_data((unsigned long)ops))
 		ops->flags |= FTRACE_OPS_FL_DYNAMIC;
 
 	add_ftrace_ops(&ftrace_ops_list, ops);
diff --git a/net/sysctl_net.c b/net/sysctl_net.c
index f6cb0d4d114c..4b45ed631eb8 100644
--- a/net/sysctl_net.c
+++ b/net/sysctl_net.c
@@ -144,7 +144,7 @@ static void ensure_safe_net_sysctl(struct net *net, const char *path,
 		addr = (unsigned long)ent->data;
 		if (is_module_address(addr))
 			where = "module";
-		else if (core_kernel_data(addr))
+		else if (is_kernel_core_data(addr))
 			where = "kernel";
 		else
 			continue;
-- 
2.26.2

```

## \[PATCH v2 5/7\] kallsyms: Rename is\_kernel() and is\_kernel\_text()

- From: Kefeng Wang \<hidden\>
- Date: 2021-07-28 08:07:35 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1460484.md

```text
The is_kernel[_text]() function check the address whether or not
in kernel[_text] ranges, also they will check the address whether
or not in gate area, so use better name.

Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Sami Tolvanen <samitolvanen@google.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: <redacted>
Signed-off-by: Kefeng Wang <redacted>
---
 arch/x86/net/bpf_jit_comp.c | 2 +-
 include/linux/kallsyms.h    | 8 ++++----
 kernel/cfi.c                | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
index 333650b9372a..c87d0dd4370d 100644
--- a/arch/x86/net/bpf_jit_comp.c
+++ b/arch/x86/net/bpf_jit_comp.c
@@ -372,7 +372,7 @@ static int __bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
 		       void *old_addr, void *new_addr)
 {
-	if (!is_kernel_text((long)ip) &&
+	if (!is_kernel_text_or_gate_area((long)ip) &&
 	    !is_bpf_text_address((long)ip))
 		/* BPF poking in modules is not supported */
 		return -EINVAL;
diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h
index 8a9d329c927c..4f501ac9c2c2 100644
--- a/include/linux/kallsyms.h
+++ b/include/linux/kallsyms.h
@@ -24,14 +24,14 @@
 struct cred;
 struct module;
 
-static inline int is_kernel_text(unsigned long addr)
+static inline int is_kernel_text_or_gate_area(unsigned long addr)
 {
 	if ((addr >= (unsigned long)_stext && addr < (unsigned long)_etext))
 		return 1;
 	return in_gate_area_no_mm(addr);
 }
 
-static inline int is_kernel(unsigned long addr)
+static inline int is_kernel_or_gate_area(unsigned long addr)
 {
 	if (addr >= (unsigned long)_stext && addr < (unsigned long)_end)
 		return 1;
@@ -41,9 +41,9 @@ static inline int is_kernel(unsigned long addr)
 static inline int is_ksym_addr(unsigned long addr)
 {
 	if (IS_ENABLED(CONFIG_KALLSYMS_ALL))
-		return is_kernel(addr);
+		return is_kernel_or_gate_area(addr);
 
-	return is_kernel_text(addr) || is_kernel_inittext(addr);
+	return is_kernel_text_or_gate_area(addr) || is_kernel_inittext(addr);
 }
 
 static inline void *dereference_symbol_descriptor(void *ptr)
diff --git a/kernel/cfi.c b/kernel/cfi.c
index e17a56639766..e7d90eff4382 100644
--- a/kernel/cfi.c
+++ b/kernel/cfi.c
@@ -282,7 +282,7 @@ static inline cfi_check_fn find_check_fn(unsigned long ptr)
 {
 	cfi_check_fn fn = NULL;
 
-	if (is_kernel_text(ptr))
+	if (is_kernel_text_or_gate_area(ptr))
 		return __cfi_check;
 
 	/*
-- 
2.26.2

```

## \[PATCH v2 2/7\] kallsyms: Fix address-checks for kernel related range

- From: Kefeng Wang \<hidden\>
- Date: 2021-07-28 08:07:37 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1460485.md

```text
The is_kernel_inittext/is_kernel_text/is_kernel function should not
include the end address(the labels _einittext, _etext and _end) when
check the address range, the issue exists since Linux v2.6.12.

Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Sergey Senozhatsky <redacted>
Cc: Petr Mladek <pmladek@suse.com>
Acked-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Signed-off-by: Kefeng Wang <redacted>
---
 include/linux/kallsyms.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h
index 2a241e3f063f..b016c62f30a6 100644
--- a/include/linux/kallsyms.h
+++ b/include/linux/kallsyms.h
@@ -27,21 +27,21 @@ struct module;
 static inline int is_kernel_inittext(unsigned long addr)
 {
 	if (addr >= (unsigned long)_sinittext
-	    && addr <= (unsigned long)_einittext)
+	    && addr < (unsigned long)_einittext)
 		return 1;
 	return 0;
 }
 
 static inline int is_kernel_text(unsigned long addr)
 {
-	if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext))
+	if ((addr >= (unsigned long)_stext && addr < (unsigned long)_etext))
 		return 1;
 	return in_gate_area_no_mm(addr);
 }
 
 static inline int is_kernel(unsigned long addr)
 {
-	if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
+	if (addr >= (unsigned long)_stext && addr < (unsigned long)_end)
 		return 1;
 	return in_gate_area_no_mm(addr);
 }
-- 
2.26.2

```

## Re: \[PATCH v2 2/7\] kallsyms: Fix address-checks for kernel related range

- From: Steven Rostedt \<rostedt@goodmis.org\>
- Date: 2021-07-28 14:46:55 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1460963.md

```text
On Wed, 28 Jul 2021 16:13:15 +0800
Kefeng Wang [ref] wrote:

> The is_kernel_inittext/is_kernel_text/is_kernel function should not
> include the end address(the labels _einittext, _etext and _end) when
> check the address range, the issue exists since Linux v2.6.12.
> 
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Sergey Senozhatsky <redacted>
> Cc: Petr Mladek <pmladek@suse.com>
> Acked-by: Sergey Senozhatsky <senozhatsky@chromium.org>
> Reviewed-by: Petr Mladek <pmladek@suse.com>
> Signed-off-by: Kefeng Wang <redacted>

Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>

-- Steve

> ---
>  include/linux/kallsyms.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h
> index 2a241e3f063f..b016c62f30a6 100644
> --- a/include/linux/kallsyms.h
> +++ b/include/linux/kallsyms.h
> @@ -27,21 +27,21 @@ struct module;
>  static inline int is_kernel_inittext(unsigned long addr)
>  {
>  	if (addr >= (unsigned long)_sinittext
> -	    && addr <= (unsigned long)_einittext)
> +	    && addr < (unsigned long)_einittext)
>  		return 1;
>  	return 0;
>  }
>  
>  static inline int is_kernel_text(unsigned long addr)
>  {
> -	if ((addr >= (unsigned long)_stext && addr <= (unsigned long)_etext))
> +	if ((addr >= (unsigned long)_stext && addr < (unsigned long)_etext))
>  		return 1;
>  	return in_gate_area_no_mm(addr);
>  }
>  
>  static inline int is_kernel(unsigned long addr)
>  {
> -	if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
> +	if (addr >= (unsigned long)_stext && addr < (unsigned long)_end)
>  		return 1;
>  	return in_gate_area_no_mm(addr);
>  }

```

## Re: \[PATCH v2 5/7\] kallsyms: Rename is\_kernel() and is\_kernel\_text()

- From: Steven Rostedt \<rostedt@goodmis.org\>
- Date: 2021-07-28 15:28:41 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1461024.md

```text
On Wed, 28 Jul 2021 16:13:18 +0800
Kefeng Wang [ref] wrote:

> The is_kernel[_text]() function check the address whether or not
> in kernel[_text] ranges, also they will check the address whether
> or not in gate area, so use better name.

Do you know what a gate area is?

Because I believe gate area is kernel text, so the rename just makes it
redundant and more confusing.

-- Steve
```

## Re: \[PATCH v2 6/7\] sections: Add new is\_kernel() and is\_kernel\_text()

- From: Steven Rostedt \<rostedt@goodmis.org\>
- Date: 2021-07-28 15:32:14 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1461035.md

```text
On Wed, 28 Jul 2021 16:13:19 +0800
Kefeng Wang [ref] wrote:

> @@ -64,8 +64,7 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr)
>  
>  int notrace core_kernel_text(unsigned long addr)
>  {
> -	if (addr >= (unsigned long)_stext &&
> -	    addr < (unsigned long)_etext)
> +	if (is_kernel_text(addr))

Perhaps this was a bug, and these functions should be checking the gate
area as well, as that is part of kernel text.

-- Steve


>  		return 1;
>  
>  	if (system_state < SYSTEM_RUNNING &&
> diff --git a/mm/kasan/report.c b/mm/kasan/report.c
> index 884a950c7026..88f5b0c058b7 100644
> --- a/mm/kasan/report.c
> +++ b/mm/kasan/report.c
> @@ -235,7 +235,7 @@ static void describe_object(struct kmem_cache *cache, void *object,
>  
>  static inline bool kernel_or_module_addr(const void *addr)
>  {
> -	if (addr >= (void *)_stext && addr < (void *)_end)
> +	if (is_kernel((unsigned long)addr))
>  		return true;
>  	if (is_module_address((unsigned long)addr))
>  		return true;
> -- 
```

## Re: \[PATCH v2 5/7\] kallsyms: Rename is\_kernel() and is\_kernel\_text()

- From: Kefeng Wang \<hidden\>
- Date: 2021-07-29 02:00:59 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1461531.md

```text

On 2021/7/28 23:28, Steven Rostedt wrote:
> On Wed, 28 Jul 2021 16:13:18 +0800
> Kefeng Wang [ref] wrote:
>
>> The is_kernel[_text]() function check the address whether or not
>> in kernel[_text] ranges, also they will check the address whether
>> or not in gate area, so use better name.
> Do you know what a gate area is?
>
> Because I believe gate area is kernel text, so the rename just makes it
> redundant and more confusing.

Yes, the gate area(eg, vectors part on ARM32, similar on x86/ia64) is kernel text.

I want to keep the 'basic' section boundaries check, which only check the start/end

of sections, all in section.h,  could we use 'generic' or 'basic' or 'core' in the naming?

 * is_kernel_generic_data()	--- come from core_kernel_data() in kernel.h
 * is_kernel_generic_text()

The old helper could remain unchanged, any suggestion, thanks.

>
> -- Steve
> .
>
```

## Re: \[PATCH v2 6/7\] sections: Add new is\_kernel() and is\_kernel\_text()

- From: Kefeng Wang \<hidden\>
- Date: 2021-07-29 02:03:10 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1461532.md

```text

On 2021/7/28 23:32, Steven Rostedt wrote:
> On Wed, 28 Jul 2021 16:13:19 +0800
> Kefeng Wang [ref] wrote:
>
>> @@ -64,8 +64,7 @@ const struct exception_table_entry *search_exception_tables(unsigned long addr)
>>   
>>   int notrace core_kernel_text(unsigned long addr)
>>   {
>> -	if (addr >= (unsigned long)_stext &&
>> -	    addr < (unsigned long)_etext)
>> +	if (is_kernel_text(addr))
> Perhaps this was a bug, and these functions should be checking the gate
> area as well, as that is part of kernel text.
Ok, I would fix this if patch5 is reviewed well.
>
> -- Steve
>
>
>>   		return 1;
>>   
>>   	if (system_state < SYSTEM_RUNNING &&
>> diff --git a/mm/kasan/report.c b/mm/kasan/report.c
>> index 884a950c7026..88f5b0c058b7 100644
>> --- a/mm/kasan/report.c
>> +++ b/mm/kasan/report.c
>> @@ -235,7 +235,7 @@ static void describe_object(struct kmem_cache *cache, void *object,
>>   
>>   static inline bool kernel_or_module_addr(const void *addr)
>>   {
>> -	if (addr >= (void *)_stext && addr < (void *)_end)
>> +	if (is_kernel((unsigned long)addr))
>>   		return true;
>>   	if (is_module_address((unsigned long)addr))
>>   		return true;
>> -- 
> .
>
```

## Re: \[PATCH v2 2/7\] kallsyms: Fix address-checks for kernel related range

- From: Kefeng Wang \<hidden\>
- Date: 2021-07-29 02:03:33 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1461533.md

```text

On 2021/7/28 22:46, Steven Rostedt wrote:
> On Wed, 28 Jul 2021 16:13:15 +0800
> Kefeng Wang [ref] wrote:
>
>> The is_kernel_inittext/is_kernel_text/is_kernel function should not
>> include the end address(the labels _einittext, _etext and _end) when
>> check the address range, the issue exists since Linux v2.6.12.
>>
>> Cc: Arnd Bergmann <arnd@arndb.de>
>> Cc: Sergey Senozhatsky <redacted>
>> Cc: Petr Mladek <pmladek@suse.com>
>> Acked-by: Sergey Senozhatsky <senozhatsky@chromium.org>
>> Reviewed-by: Petr Mladek <pmladek@suse.com>
>> Signed-off-by: Kefeng Wang <redacted>
> Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>

Thanks.

>
> -- Steve
>
```

## Re: \[PATCH v2 5/7\] kallsyms: Rename is\_kernel() and is\_kernel\_text()

- From: Steven Rostedt \<rostedt@goodmis.org\>
- Date: 2021-07-29 04:05:42 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1461572.md

```text
On Thu, 29 Jul 2021 10:00:51 +0800
Kefeng Wang [ref] wrote:

> On 2021/7/28 23:28, Steven Rostedt wrote:
> > On Wed, 28 Jul 2021 16:13:18 +0800
> > Kefeng Wang [ref] wrote:
> >  
> >> The is_kernel[_text]() function check the address whether or not
> >> in kernel[_text] ranges, also they will check the address whether
> >> or not in gate area, so use better name.  
> > Do you know what a gate area is?
> >
> > Because I believe gate area is kernel text, so the rename just makes it
> > redundant and more confusing.  
> 
> Yes, the gate area(eg, vectors part on ARM32, similar on x86/ia64) is 
> kernel text.
> 
> I want to keep the 'basic' section boundaries check, which only check 
> the start/end
> 
> of sections, all in section.h,  could we use 'generic' or 'basic' or 
> 'core' in the naming?
> 
>   * is_kernel_generic_data()	--- come from core_kernel_data() in kernel.h
>   * is_kernel_generic_text()
> 
> The old helper could remain unchanged, any suggestion, thanks.

Because it looks like the check of just being in the range of "_stext"
to "_end" is just an internal helper, why not do what we do all over
the kernel, and just prefix the function with a couple of underscores,
that denote that it's internal?

  __is_kernel_text()

Then you have:

 static inline int is_kernel_text(unsigned long addr)
 {
	if (__is_kernel_text(addr))
 		return 1;
 	return in_gate_area_no_mm(addr);
 }

-- Steve
```

## Re: \[PATCH v2 5/7\] kallsyms: Rename is\_kernel() and is\_kernel\_text()

- From: Kefeng Wang \<hidden\>
- Date: 2021-07-29 11:06:14 
- Message: https://ratatoskr.run/linuxppc-dev/2021/07/1461849.md

```text

On 2021/7/29 12:05, Steven Rostedt wrote:
> On Thu, 29 Jul 2021 10:00:51 +0800
> Kefeng Wang [ref] wrote:
>
>> On 2021/7/28 23:28, Steven Rostedt wrote:
>>> On Wed, 28 Jul 2021 16:13:18 +0800
>>> Kefeng Wang [ref] wrote:
>>>   
>>>> The is_kernel[_text]() function check the address whether or not
>>>> in kernel[_text] ranges, also they will check the address whether
>>>> or not in gate area, so use better name.
>>> Do you know what a gate area is?
>>>
>>> Because I believe gate area is kernel text, so the rename just makes it
>>> redundant and more confusing.
>> Yes, the gate area(eg, vectors part on ARM32, similar on x86/ia64) is
>> kernel text.
>>
>> I want to keep the 'basic' section boundaries check, which only check
>> the start/end
>>
>> of sections, all in section.h,  could we use 'generic' or 'basic' or
>> 'core' in the naming?
>>
>>    * is_kernel_generic_data()	--- come from core_kernel_data() in kernel.h
>>    * is_kernel_generic_text()
>>
>> The old helper could remain unchanged, any suggestion, thanks.
> Because it looks like the check of just being in the range of "_stext"
> to "_end" is just an internal helper, why not do what we do all over
> the kernel, and just prefix the function with a couple of underscores,
> that denote that it's internal?
>
>    __is_kernel_text()

OK， thanks for your advise,  there's already a __is_kernel_text() in arch/x86/mm/init_32.c,

I will change it to is_x32_kernel_text() to avoid conflict on x86_32.

>
> Then you have:
>
>   static inline int is_kernel_text(unsigned long addr)
>   {
> 	if (__is_kernel_text(addr))
>   		return 1;
>   	return in_gate_area_no_mm(addr);
>   }
>
> -- Steve
> .
>
```
