bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/9] sections: Unify kernel sections range check and use
@ 2021-09-26  7:20 Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 1/9] kallsyms: Remove arch specific text and data check Kefeng Wang
                   ` (8 more replies)
  0 siblings, 9 replies; 12+ messages in thread
From: Kefeng Wang @ 2021-09-26  7:20 UTC (permalink / raw)
  To: arnd, linux-arch, linux-kernel, linuxppc-dev, rostedt, mingo,
	davem, ast, ryabinin.a.a, akpm
  Cc: mpe, benh, paulus, bpf, Kefeng Wang

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_rodata()		--- already in sections.h
 * is_kernel_core_data()	--- come from core_kernel_data() in kernel.h
 * is_kernel_inittext()		--- come from kernel.h and kallsyms.h
 * __is_kernel_text()		--- add new internal helper
 * __is_kernel()		--- add new internal helper

Note: For the last two helpers, people should not use directly, consider to
      use corresponding function in kallsyms.h.

v3:
- Add Steven's RB to patch2
- Introduce two internal helper, then use is_kernel_text() in core_kernel_text()
  and is_kernel() in kernel_or_module_addr() suggested by Steven

v2:
https://lore.kernel.org/linux-arch/20210728081320.20394-1-wangkefeng.wang@huawei.com/
- add ACK/RW to patch2, and drop inappropriate fix tag
- keep 'core' to check kernel data, suggestted by Steven Rostedt
  <rostedt@goodmis.org>, 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

Cc: linuxppc-dev@lists.ozlabs.org
Cc: linux-arch@vger.kernel.org 
Cc: bpf@vger.kernel.org 

Kefeng Wang (9):
  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
  x86: mm: Rename __is_kernel_text() to is_x86_32_kernel_text()
  sections: Provide internal __is_kernel() and __is_kernel_text() helper
  mm: kasan: Use is_kernel() helper
  extable: Use is_kernel_text() helper
  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/mm/init_32.c          | 14 +++----
 include/asm-generic/sections.h | 75 ++++++++++++++++++++++++++--------
 include/linux/kallsyms.h       | 13 +-----
 include/linux/kernel.h         |  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 +-
 11 files changed, 75 insertions(+), 80 deletions(-)

-- 
2.26.2


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 1/9] kallsyms: Remove arch specific text and data check
  2021-09-26  7:20 [PATCH v3 0/9] sections: Unify kernel sections range check and use Kefeng Wang
@ 2021-09-26  7:20 ` Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 2/9] kallsyms: Fix address-checks for kernel related range Kefeng Wang
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2021-09-26  7:20 UTC (permalink / raw)
  To: arnd, linux-arch, linux-kernel, linuxppc-dev, rostedt, mingo,
	davem, ast, ryabinin.a.a, akpm
  Cc: mpe, benh, paulus, bpf, Kefeng Wang

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 <wangkefeng.wang@huawei.com>
---
 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 7096384dc60f..dcdbcee391cd 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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 2/9] kallsyms: Fix address-checks for kernel related range
  2021-09-26  7:20 [PATCH v3 0/9] sections: Unify kernel sections range check and use Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 1/9] kallsyms: Remove arch specific text and data check Kefeng Wang
@ 2021-09-26  7:20 ` Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 3/9] sections: Move and rename core_kernel_data() to is_kernel_core_data() Kefeng Wang
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2021-09-26  7:20 UTC (permalink / raw)
  To: arnd, linux-arch, linux-kernel, linuxppc-dev, rostedt, mingo,
	davem, ast, ryabinin.a.a, akpm
  Cc: mpe, benh, paulus, bpf, Kefeng Wang, Sergey Senozhatsky,
	Petr Mladek, Sergey Senozhatsky

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 <sergey.senozhatsky@gmail.com>
Cc: Petr Mladek <pmladek@suse.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Acked-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 3/9] sections: Move and rename core_kernel_data() to is_kernel_core_data()
  2021-09-26  7:20 [PATCH v3 0/9] sections: Unify kernel sections range check and use Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 1/9] kallsyms: Remove arch specific text and data check Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 2/9] kallsyms: Fix address-checks for kernel related range Kefeng Wang
@ 2021-09-26  7:20 ` Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 4/9] sections: Move is_kernel_inittext() into sections.h Kefeng Wang
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2021-09-26  7:20 UTC (permalink / raw)
  To: arnd, linux-arch, linux-kernel, linuxppc-dev, rostedt, mingo,
	davem, ast, ryabinin.a.a, akpm
  Cc: mpe, benh, paulus, bpf, Kefeng Wang

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 <wangkefeng.wang@huawei.com>
---
 include/asm-generic/sections.h | 16 ++++++++++++++++
 include/linux/kernel.h         |  1 -
 kernel/extable.c               | 18 ------------------
 kernel/trace/ftrace.c          |  2 +-
 net/sysctl_net.c               |  2 +-
 5 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h
index 817309e289db..24780c0f40b1 100644
--- a/include/asm-generic/sections.h
+++ b/include/asm-generic/sections.h
@@ -142,6 +142,22 @@ 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.
+ * Note: On some archs it may return true for core RODATA, and false
+ *       for others. But will always be true for core RW data.
+ */
+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 2776423a587e..e5a9af8a4e20 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 7efbc8aaf7f6..f15badf31f52 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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 4/9] sections: Move is_kernel_inittext() into sections.h
  2021-09-26  7:20 [PATCH v3 0/9] sections: Unify kernel sections range check and use Kefeng Wang
                   ` (2 preceding siblings ...)
  2021-09-26  7:20 ` [PATCH v3 3/9] sections: Move and rename core_kernel_data() to is_kernel_core_data() Kefeng Wang
@ 2021-09-26  7:20 ` Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 5/9] x86: mm: Rename __is_kernel_text() to is_x86_32_kernel_text() Kefeng Wang
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2021-09-26  7:20 UTC (permalink / raw)
  To: arnd, linux-arch, linux-kernel, linuxppc-dev, rostedt, mingo,
	davem, ast, ryabinin.a.a, akpm
  Cc: mpe, benh, paulus, bpf, Kefeng Wang, Thomas Gleixner, x86

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 <tglx@linutronix.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: x86@kernel.org
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 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 24780c0f40b1..811583ca8bd0 100644
--- a/include/asm-generic/sections.h
+++ b/include/asm-generic/sections.h
@@ -172,4 +172,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 e5a9af8a4e20..445d0dceefb8 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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 5/9] x86: mm: Rename __is_kernel_text() to is_x86_32_kernel_text()
  2021-09-26  7:20 [PATCH v3 0/9] sections: Unify kernel sections range check and use Kefeng Wang
                   ` (3 preceding siblings ...)
  2021-09-26  7:20 ` [PATCH v3 4/9] sections: Move is_kernel_inittext() into sections.h Kefeng Wang
@ 2021-09-26  7:20 ` Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 6/9] sections: Provide internal __is_kernel() and __is_kernel_text() helper Kefeng Wang
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2021-09-26  7:20 UTC (permalink / raw)
  To: arnd, linux-arch, linux-kernel, linuxppc-dev, rostedt, mingo,
	davem, ast, ryabinin.a.a, akpm
  Cc: mpe, benh, paulus, bpf, Kefeng Wang, Borislav Petkov, x86

Commit b56cd05c55a1 ("x86/mm: Rename is_kernel_text to __is_kernel_text"),
add '__' prefix not to get in conflict with existing is_kernel_text() in
<linux/kallsyms.h>.

We will add __is_kernel_text() for the basic kernel text range check in the
next patch, so use private is_x86_32_kernel_text() naming for x86 special
check.

Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: x86@kernel.org
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 arch/x86/mm/init_32.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/arch/x86/mm/init_32.c b/arch/x86/mm/init_32.c
index bd90b8fe81e4..523743ee9dea 100644
--- a/arch/x86/mm/init_32.c
+++ b/arch/x86/mm/init_32.c
@@ -238,11 +238,7 @@ page_table_range_init(unsigned long start, unsigned long end, pgd_t *pgd_base)
 	}
 }
 
-/*
- * The <linux/kallsyms.h> already defines is_kernel_text,
- * using '__' prefix not to get in conflict.
- */
-static inline int __is_kernel_text(unsigned long addr)
+static inline int is_x86_32_kernel_text(unsigned long addr)
 {
 	if (addr >= (unsigned long)_text && addr <= (unsigned long)__init_end)
 		return 1;
@@ -333,8 +329,8 @@ kernel_physical_mapping_init(unsigned long start,
 				addr2 = (pfn + PTRS_PER_PTE-1) * PAGE_SIZE +
 					PAGE_OFFSET + PAGE_SIZE-1;
 
-				if (__is_kernel_text(addr) ||
-				    __is_kernel_text(addr2))
+				if (is_x86_32_kernel_text(addr) ||
+				    is_x86_32_kernel_text(addr2))
 					prot = PAGE_KERNEL_LARGE_EXEC;
 
 				pages_2m++;
@@ -359,7 +355,7 @@ kernel_physical_mapping_init(unsigned long start,
 				 */
 				pgprot_t init_prot = __pgprot(PTE_IDENT_ATTR);
 
-				if (__is_kernel_text(addr))
+				if (is_x86_32_kernel_text(addr))
 					prot = PAGE_KERNEL_EXEC;
 
 				pages_4k++;
@@ -820,7 +816,7 @@ static void mark_nxdata_nx(void)
 	 */
 	unsigned long start = PFN_ALIGN(_etext);
 	/*
-	 * This comes from __is_kernel_text upper limit. Also HPAGE where used:
+	 * This comes from is_x86_32_kernel_text upper limit. Also HPAGE where used:
 	 */
 	unsigned long size = (((unsigned long)__init_end + HPAGE_SIZE) & HPAGE_MASK) - start;
 
-- 
2.26.2


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 6/9] sections: Provide internal __is_kernel() and __is_kernel_text() helper
  2021-09-26  7:20 [PATCH v3 0/9] sections: Unify kernel sections range check and use Kefeng Wang
                   ` (4 preceding siblings ...)
  2021-09-26  7:20 ` [PATCH v3 5/9] x86: mm: Rename __is_kernel_text() to is_x86_32_kernel_text() Kefeng Wang
@ 2021-09-26  7:20 ` Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 7/9] mm: kasan: Use is_kernel() helper Kefeng Wang
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2021-09-26  7:20 UTC (permalink / raw)
  To: arnd, linux-arch, linux-kernel, linuxppc-dev, rostedt, mingo,
	davem, ast, ryabinin.a.a, akpm
  Cc: mpe, benh, paulus, bpf, Kefeng Wang

An internal __is_kernel() helper which only check the kernel address ranges,
and an internal __is_kernel_text() helper which only check text section ranges.

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 include/asm-generic/sections.h | 29 +++++++++++++++++++++++++++++
 include/linux/kallsyms.h       |  4 ++--
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h
index 811583ca8bd0..a7abeadddc7a 100644
--- a/include/asm-generic/sections.h
+++ b/include/asm-generic/sections.h
@@ -186,4 +186,33 @@ static inline bool is_kernel_inittext(unsigned long addr)
 	       addr < (unsigned long)_einittext;
 }
 
+/**
+ * __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.
+ * Note: an internal helper, only check the range of _stext to _etext.
+ */
+static inline bool __is_kernel_text(unsigned long addr)
+{
+	return addr >= (unsigned long)_stext &&
+	       addr < (unsigned long)_etext;
+}
+
+/**
+ * __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 the kernel range, false otherwise.
+ * Note: an internal helper, only check the range of _stext to _end.
+ */
+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 8a9d329c927c..5fb17dd4b6fa 100644
--- a/include/linux/kallsyms.h
+++ b/include/linux/kallsyms.h
@@ -26,14 +26,14 @@ struct module;
 
 static inline int is_kernel_text(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(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);
 }
-- 
2.26.2


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 7/9] mm: kasan: Use is_kernel() helper
  2021-09-26  7:20 [PATCH v3 0/9] sections: Unify kernel sections range check and use Kefeng Wang
                   ` (5 preceding siblings ...)
  2021-09-26  7:20 ` [PATCH v3 6/9] sections: Provide internal __is_kernel() and __is_kernel_text() helper Kefeng Wang
@ 2021-09-26  7:20 ` Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 8/9] extable: Use is_kernel_text() helper Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 9/9] powerpc/mm: Use is_kernel_text() and is_kernel_inittext() helper Kefeng Wang
  8 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2021-09-26  7:20 UTC (permalink / raw)
  To: arnd, linux-arch, linux-kernel, linuxppc-dev, rostedt, mingo,
	davem, ast, ryabinin.a.a, akpm
  Cc: mpe, benh, paulus, bpf, Kefeng Wang, Alexander Potapenko,
	Andrey Konovalov, Dmitry Vyukov

Directly use is_kernel() helper in kernel_or_module_addr().

Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 mm/kasan/report.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/kasan/report.c b/mm/kasan/report.c
index 3239fd8f8747..1c955e1c98d5 100644
--- a/mm/kasan/report.c
+++ b/mm/kasan/report.c
@@ -226,7 +226,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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 8/9] extable: Use is_kernel_text() helper
  2021-09-26  7:20 [PATCH v3 0/9] sections: Unify kernel sections range check and use Kefeng Wang
                   ` (6 preceding siblings ...)
  2021-09-26  7:20 ` [PATCH v3 7/9] mm: kasan: Use is_kernel() helper Kefeng Wang
@ 2021-09-26  7:20 ` Kefeng Wang
  2021-09-26  7:20 ` [PATCH v3 9/9] powerpc/mm: Use is_kernel_text() and is_kernel_inittext() helper Kefeng Wang
  8 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2021-09-26  7:20 UTC (permalink / raw)
  To: arnd, linux-arch, linux-kernel, linuxppc-dev, rostedt, mingo,
	davem, ast, ryabinin.a.a, akpm
  Cc: mpe, benh, paulus, bpf, Kefeng Wang

The core_kernel_text() should check the gate area, as it is part
of kernel text range.

Cc: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 kernel/extable.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

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 &&
-- 
2.26.2


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v3 9/9] powerpc/mm: Use is_kernel_text() and is_kernel_inittext() helper
  2021-09-26  7:20 [PATCH v3 0/9] sections: Unify kernel sections range check and use Kefeng Wang
                   ` (7 preceding siblings ...)
  2021-09-26  7:20 ` [PATCH v3 8/9] extable: Use is_kernel_text() helper Kefeng Wang
@ 2021-09-26  7:20 ` Kefeng Wang
  2021-09-28 17:51   ` Christophe Leroy
  8 siblings, 1 reply; 12+ messages in thread
From: Kefeng Wang @ 2021-09-26  7:20 UTC (permalink / raw)
  To: arnd, linux-arch, linux-kernel, linuxppc-dev, rostedt, mingo,
	davem, ast, ryabinin.a.a, akpm
  Cc: mpe, benh, paulus, bpf, Kefeng Wang

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 <paulus@samba.org>
Cc: linuxppc-dev@lists.ozlabs.org
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v3 9/9] powerpc/mm: Use is_kernel_text() and is_kernel_inittext() helper
  2021-09-26  7:20 ` [PATCH v3 9/9] powerpc/mm: Use is_kernel_text() and is_kernel_inittext() helper Kefeng Wang
@ 2021-09-28 17:51   ` Christophe Leroy
  2021-09-29  1:14     ` Kefeng Wang
  0 siblings, 1 reply; 12+ messages in thread
From: Christophe Leroy @ 2021-09-28 17:51 UTC (permalink / raw)
  To: Kefeng Wang, arnd, linux-arch, linux-kernel, linuxppc-dev,
	rostedt, mingo, davem, ast, ryabinin.a.a, akpm
  Cc: bpf, paulus



Le 26/09/2021 à 09:20, Kefeng Wang a écrit :
> 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 <paulus@samba.org>
> Cc: linuxppc-dev@lists.ozlabs.org
> Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
> ---
>   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));

I think we could use core_kernel_next() instead.


>   		map_kernel_page(v, p, ktext ? PAGE_KERNEL_TEXT : PAGE_KERNEL);
>   		v += PAGE_SIZE;
>   		p += PAGE_SIZE;
> 




Build failure on mpc885_ads_defconfig

arch/powerpc/mm/pgtable_32.c: In function '__mapin_ram_chunk':
arch/powerpc/mm/pgtable_32.c:111:26: error: implicit declaration of 
function 'is_kernel_text'; did you mean 'is_kernel_inittext'? 
[-Werror=implicit-function-declaration]
   111 |                 ktext = (is_kernel_text(v) || 
is_kernel_inittext(v));
       |                          ^~~~~~~~~~~~~~
       |                          is_kernel_inittext
cc1: all warnings being treated as errors
make[2]: *** [scripts/Makefile.build:277: arch/powerpc/mm/pgtable_32.o] 
Error 1
make[1]: *** [scripts/Makefile.build:540: arch/powerpc/mm] Error 2
make: *** [Makefile:1868: arch/powerpc] Error 2



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v3 9/9] powerpc/mm: Use is_kernel_text() and is_kernel_inittext() helper
  2021-09-28 17:51   ` Christophe Leroy
@ 2021-09-29  1:14     ` Kefeng Wang
  0 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2021-09-29  1:14 UTC (permalink / raw)
  To: Christophe Leroy, arnd, linux-arch, linux-kernel, linuxppc-dev,
	rostedt, mingo, davem, ast, ryabinin.a.a, akpm
  Cc: bpf, paulus



On 2021/9/29 1:51, Christophe Leroy wrote:
> 
> 
> Le 26/09/2021 à 09:20, Kefeng Wang a écrit :
>> 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 <paulus@samba.org>
>> Cc: linuxppc-dev@lists.ozlabs.org
>> Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
>> ---
>>   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));
> 
> I think we could use core_kernel_next() instead.
Indead. oops, sorry for the build error, will update, thanks.

> 
> Build failure on mpc885_ads_defconfig
> 
> arch/powerpc/mm/pgtable_32.c: In function '__mapin_ram_chunk':
> arch/powerpc/mm/pgtable_32.c:111:26: error: implicit declaration of 
> function 'is_kernel_text'; did you mean 'is_kernel_inittext'? 
> [-Werror=implicit-function-declaration]
>    111 |                 ktext = (is_kernel_text(v) || 
> is_kernel_inittext(v));
>        |                          ^~~~~~~~~~~~~~
>        |                          is_kernel_inittext
> cc1: all warnings being treated as errors
> make[2]: *** [scripts/Makefile.build:277: arch/powerpc/mm/pgtable_32.o] 
> Error 1
> make[1]: *** [scripts/Makefile.build:540: arch/powerpc/mm] Error 2
> make: *** [Makefile:1868: arch/powerpc] Error 2
> 
> 
> .

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2021-09-29  1:14 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-26  7:20 [PATCH v3 0/9] sections: Unify kernel sections range check and use Kefeng Wang
2021-09-26  7:20 ` [PATCH v3 1/9] kallsyms: Remove arch specific text and data check Kefeng Wang
2021-09-26  7:20 ` [PATCH v3 2/9] kallsyms: Fix address-checks for kernel related range Kefeng Wang
2021-09-26  7:20 ` [PATCH v3 3/9] sections: Move and rename core_kernel_data() to is_kernel_core_data() Kefeng Wang
2021-09-26  7:20 ` [PATCH v3 4/9] sections: Move is_kernel_inittext() into sections.h Kefeng Wang
2021-09-26  7:20 ` [PATCH v3 5/9] x86: mm: Rename __is_kernel_text() to is_x86_32_kernel_text() Kefeng Wang
2021-09-26  7:20 ` [PATCH v3 6/9] sections: Provide internal __is_kernel() and __is_kernel_text() helper Kefeng Wang
2021-09-26  7:20 ` [PATCH v3 7/9] mm: kasan: Use is_kernel() helper Kefeng Wang
2021-09-26  7:20 ` [PATCH v3 8/9] extable: Use is_kernel_text() helper Kefeng Wang
2021-09-26  7:20 ` [PATCH v3 9/9] powerpc/mm: Use is_kernel_text() and is_kernel_inittext() helper Kefeng Wang
2021-09-28 17:51   ` Christophe Leroy
2021-09-29  1:14     ` Kefeng Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).