Skip to contentSkip to conversation

kgdb: Fix to kill breakpoints on initmem after boot

PATCH Applied Sumit Garg 5 participants 9 messages Feb 24, 2021 – Feb 26, 2021

Subsystems (2)
lore
Share & tools
lore

Sumit Garg

author
#0 lore raw

PATCH 1/1 kgdb: Fix to kill breakpoints on initmem after boot

Currently breakpoints in kernel .init.text section are not handled correctly while allowing to remove them even after corresponding pages have been freed.

Fix it via killing .init.text section breakpoints just prior to initmem pages being freed.

---
 include/linux/kgdb.h      |  2 ++
 init/main.c               |  1 +
 kernel/debug/debug_core.c | 11 +++++++++++
 3 files changed, 14 insertions(+)
include/linux/kgdb.h +2 -0
index 57b8885708e5..3aa503ef06fc 100644
@@ -361,9 +361,11 @@ extern atomic_t			kgdb_active;
 extern bool dbg_is_early;
 extern void __init dbg_late_init(void);
 extern void kgdb_panic(const char *msg);
+extern void kgdb_free_init_mem(void);
 #else /* ! CONFIG_KGDB */
 #define in_dbg_master() (0)
 #define dbg_late_init()
 static inline void kgdb_panic(const char *msg) {}
+static inline void kgdb_free_init_mem(void) { }
 #endif /* ! CONFIG_KGDB */
 #endif /* _KGDB_H_ */
init/main.c +1 -0
index c68d784376ca..a446ca3d334e 100644
@@ -1417,6 +1417,7 @@ static int __ref kernel_init(void *unused)
 	async_synchronize_full();
 	kprobe_free_init_mem();
 	ftrace_free_init_mem();
+	kgdb_free_init_mem();
 	free_initmem();
 	mark_readonly();
 
kernel/debug/debug_core.c +11 -0
index 229dd119f430..319381e95d1d 100644
@@ -465,6 +465,17 @@ int dbg_remove_all_break(void)
 	return 0;
 }
 
+void kgdb_free_init_mem(void)
+{
+	int i;
+
+	/* Clear init memory breakpoints. */
+	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
+		if (init_section_contains((void *)kgdb_break[i].bpt_addr, 0))
+			kgdb_break[i].state = BP_UNDEFINED;
+	}
+}
+
 #ifdef CONFIG_KGDB_KDB
 void kdb_dump_stack_on_cpu(int cpu)
 {
Signature · 1 line
2.25.1
reviewer Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 7 lines · Sumit Garg

Currently breakpoints in kernel .init.text section are not handled correctly while allowing to remove them even after corresponding pages have been freed.

Fix it via killing .init.text section breakpoints just prior to initmem pages being freed.

It might be worth it to mention that HW breakpoints aren't handled by this patch but it's probably not such a big deal.

Quoted 41 lines
---
 include/linux/kgdb.h      |  2 ++
 init/main.c               |  1 +
 kernel/debug/debug_core.c | 11 +++++++++++
 3 files changed, 14 insertions(+)
include/linux/kgdb.h +2 -0
index 57b8885708e5..3aa503ef06fc 100644
@@ -361,9 +361,11 @@ extern atomic_t                    kgdb_active;
 extern bool dbg_is_early;
 extern void __init dbg_late_init(void);
 extern void kgdb_panic(const char *msg);
+extern void kgdb_free_init_mem(void);
 #else /* ! CONFIG_KGDB */
 #define in_dbg_master() (0)
 #define dbg_late_init()
 static inline void kgdb_panic(const char *msg) {}
+static inline void kgdb_free_init_mem(void) { }
 #endif /* ! CONFIG_KGDB */
 #endif /* _KGDB_H_ */
init/main.c +1 -0
index c68d784376ca..a446ca3d334e 100644
@@ -1417,6 +1417,7 @@ static int __ref kernel_init(void *unused)
        async_synchronize_full();
        kprobe_free_init_mem();
        ftrace_free_init_mem();
+       kgdb_free_init_mem();
        free_initmem();
        mark_readonly();
 
kernel/debug/debug_core.c +0 -0
index 229dd119f430..319381e95d1d 100644

kernel/debug/debug_core.c int dbg_remove_all_break(void) · line 474

Quoted 10 lines
kernel/debug/debug_core.c +7 -0
@@ -465,6 +465,17 @@ int dbg_remove_all_break(void)
        return 0;
 }
 
+void kgdb_free_init_mem(void)
+{
+       int i;
+
+       /* Clear init memory breakpoints. */
+       for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
+               if (init_section_contains((void *)kgdb_break[i].bpt_addr, 0))

A nit, but instead of 0 should this be passing "BREAK_INSTR_SIZE" ?

Also: even if memory is about to get freed it still seems like it'd be wise to call this:

  kgdb_arch_remove_breakpoint(&kgdb_break[i]);

It looks like it shouldn't matter today but just in case an architecture decides to do something fancy in the future it might not hurt to tell it that the breakpoint is going away.

Everything here is pretty nitty, though. This looks good to me now.

#2 lore raw
Quoted 11 lines · On Wed, 24 Feb 2021 10:09:25 -0800 Doug Anderson <[email protected]> wrote:
Quoted 7 lines · Sumit Garg

Currently breakpoints in kernel .init.text section are not handled correctly while allowing to remove them even after corresponding pages have been freed.

Fix it via killing .init.text section breakpoints just prior to initmem pages being freed.

It might be worth it to mention that HW breakpoints aren't handled by this patch but it's probably not such a big deal.

I added that to the changelog, thanks.

I'll take your response to be the coveted acked-by :)

author
#3 lore raw
Quoted 9 lines · Andrew Morton
Quoted 6 lines · On Wed, 24 Feb 2021 10:09:25 -0800 Doug Anderson <[email protected]> wrote:
Quoted 5 lines · Sumit Garg

Currently breakpoints in kernel .init.text section are not handled correctly while allowing to remove them even after corresponding pages have been freed.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Fix it via killing .init.text section breakpoints just prior to initmem pages being freed.

It might be worth it to mention that HW breakpoints aren't handled by this patch but it's probably not such a big deal.

I added that to the changelog, thanks.

Thanks Andrew for picking this up.

-Sumit

Quoted 1 line

I'll take your response to be the coveted acked-by :)

author
#4 lore raw
In reply to Doug Anderson
Quoted 75 lines · Doug Anderson

Hi,

Quoted 7 lines · Sumit Garg

Currently breakpoints in kernel .init.text section are not handled correctly while allowing to remove them even after corresponding pages have been freed.

Fix it via killing .init.text section breakpoints just prior to initmem pages being freed.

It might be worth it to mention that HW breakpoints aren't handled by this patch but it's probably not such a big deal.

Quoted 41 lines
---
 include/linux/kgdb.h      |  2 ++
 init/main.c               |  1 +
 kernel/debug/debug_core.c | 11 +++++++++++
 3 files changed, 14 insertions(+)
include/linux/kgdb.h +2 -0
index 57b8885708e5..3aa503ef06fc 100644
@@ -361,9 +361,11 @@ extern atomic_t                    kgdb_active;
 extern bool dbg_is_early;
 extern void __init dbg_late_init(void);
 extern void kgdb_panic(const char *msg);
+extern void kgdb_free_init_mem(void);
 #else /* ! CONFIG_KGDB */
 #define in_dbg_master() (0)
 #define dbg_late_init()
 static inline void kgdb_panic(const char *msg) {}
+static inline void kgdb_free_init_mem(void) { }
 #endif /* ! CONFIG_KGDB */
 #endif /* _KGDB_H_ */
init/main.c +1 -0
index c68d784376ca..a446ca3d334e 100644
@@ -1417,6 +1417,7 @@ static int __ref kernel_init(void *unused)
        async_synchronize_full();
        kprobe_free_init_mem();
        ftrace_free_init_mem();
+       kgdb_free_init_mem();
        free_initmem();
        mark_readonly();
 
kernel/debug/debug_core.c +0 -0
index 229dd119f430..319381e95d1d 100644

kernel/debug/debug_core.c int dbg_remove_all_break(void) · line 474

Quoted 10 lines
kernel/debug/debug_core.c +7 -0
@@ -465,6 +465,17 @@ int dbg_remove_all_break(void)
        return 0;
 }
 
+void kgdb_free_init_mem(void)
+{
+       int i;
+
+       /* Clear init memory breakpoints. */
+       for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
+               if (init_section_contains((void *)kgdb_break[i].bpt_addr, 0))

A nit, but instead of 0 should this be passing "BREAK_INSTR_SIZE" ?

Also: even if memory is about to get freed it still seems like it'd be wise to call this:

  kgdb_arch_remove_breakpoint(&kgdb_break[i]);
Quoted 8 lines

It looks like it shouldn't matter today but just in case an architecture decides to do something fancy in the future it might not hurt to tell it that the breakpoint is going away.

Everything here is pretty nitty, though. This looks good to me now.

Thanks Doug for your review.

-Sumit

Acked-by Tested-by on v1 1/1
#5 lore raw
In reply to Sumit Garg
Quoted 9 lines · Sumit Garg

Currently breakpoints in kernel .init.text section are not handled correctly while allowing to remove them even after corresponding pages have been freed.

Fix it via killing .init.text section breakpoints just prior to initmem pages being freed.

I saw Andrew has picked this one up. That's ok for me:

I already enriched kgdbtest to cover this (and they pass) so I guess this is also:

BTW this is not Cc:ed to stable and I do wonder if it crosses the threshold to be considered a fix rather than a feature. Normally I consider adding safety rails for kgdb to be a new feature but, in this case, the problem would easily ensnare an inexperienced developer who is doing nothing more than debugging their own driver (assuming they correctly marked their probe function as .init) so I think this weighs in favour of being a fix.

Daniel.

Quoted 58 lines
---
 include/linux/kgdb.h      |  2 ++
 init/main.c               |  1 +
 kernel/debug/debug_core.c | 11 +++++++++++
 3 files changed, 14 insertions(+)
include/linux/kgdb.h +2 -0
index 57b8885708e5..3aa503ef06fc 100644
@@ -361,9 +361,11 @@ extern atomic_t			kgdb_active;
 extern bool dbg_is_early;
 extern void __init dbg_late_init(void);
 extern void kgdb_panic(const char *msg);
+extern void kgdb_free_init_mem(void);
 #else /* ! CONFIG_KGDB */
 #define in_dbg_master() (0)
 #define dbg_late_init()
 static inline void kgdb_panic(const char *msg) {}
+static inline void kgdb_free_init_mem(void) { }
 #endif /* ! CONFIG_KGDB */
 #endif /* _KGDB_H_ */
init/main.c +1 -0
index c68d784376ca..a446ca3d334e 100644
@@ -1417,6 +1417,7 @@ static int __ref kernel_init(void *unused)
 	async_synchronize_full();
 	kprobe_free_init_mem();
 	ftrace_free_init_mem();
+	kgdb_free_init_mem();
 	free_initmem();
 	mark_readonly();
 
kernel/debug/debug_core.c +11 -0
index 229dd119f430..319381e95d1d 100644
@@ -465,6 +465,17 @@ int dbg_remove_all_break(void)
 	return 0;
 }
 
+void kgdb_free_init_mem(void)
+{
+	int i;
+
+	/* Clear init memory breakpoints. */
+	for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
+		if (init_section_contains((void *)kgdb_break[i].bpt_addr, 0))
+			kgdb_break[i].state = BP_UNDEFINED;
+	}
+}
+
 #ifdef CONFIG_KGDB_KDB
 void kdb_dump_stack_on_cpu(int cpu)
 {
Signature · 1 line
2.25.1
author
#6 lore raw

+ stable ML

Quoted 12 lines · Daniel Thompson
Quoted 9 lines · Sumit Garg

Currently breakpoints in kernel .init.text section are not handled correctly while allowing to remove them even after corresponding pages have been freed.

Fix it via killing .init.text section breakpoints just prior to initmem pages being freed.

Quoted 7 lines

I saw Andrew has picked this one up. That's ok for me:

I already enriched kgdbtest to cover this (and they pass) so I guess this is also:

Thanks Daniel.

Quoted 8 lines

BTW this is not Cc:ed to stable and I do wonder if it crosses the threshold to be considered a fix rather than a feature. Normally I consider adding safety rails for kgdb to be a new feature but, in this case, the problem would easily ensnare an inexperienced developer who is doing nothing more than debugging their own driver (assuming they correctly marked their probe function as .init) so I think this weighs in favour of being a fix.

Makes sense, Cc:ed stable.

-Sumit

Quoted 62 lines

Daniel.

Quoted 58 lines
---
 include/linux/kgdb.h      |  2 ++
 init/main.c               |  1 +
 kernel/debug/debug_core.c | 11 +++++++++++
 3 files changed, 14 insertions(+)
include/linux/kgdb.h +2 -0
index 57b8885708e5..3aa503ef06fc 100644
@@ -361,9 +361,11 @@ extern atomic_t                  kgdb_active;
 extern bool dbg_is_early;
 extern void __init dbg_late_init(void);
 extern void kgdb_panic(const char *msg);
+extern void kgdb_free_init_mem(void);
 #else /* ! CONFIG_KGDB */
 #define in_dbg_master() (0)
 #define dbg_late_init()
 static inline void kgdb_panic(const char *msg) {}
+static inline void kgdb_free_init_mem(void) { }
 #endif /* ! CONFIG_KGDB */
 #endif /* _KGDB_H_ */
init/main.c +1 -0
index c68d784376ca..a446ca3d334e 100644
@@ -1417,6 +1417,7 @@ static int __ref kernel_init(void *unused)
      async_synchronize_full();
      kprobe_free_init_mem();
      ftrace_free_init_mem();
+     kgdb_free_init_mem();
      free_initmem();
      mark_readonly();
 
kernel/debug/debug_core.c +11 -1
index 229dd119f430..319381e95d1d 100644
@@ -465,6 +465,17 @@ int dbg_remove_all_break(void)
      return 0;
 }
 
+void kgdb_free_init_mem(void)
+{
+     int i;
+
+     /* Clear init memory breakpoints. */
+     for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
+             if (init_section_contains((void *)kgdb_break[i].bpt_addr, 0))
+                     kgdb_break[i].state = BP_UNDEFINED;
+     }
+}
+
 #ifdef CONFIG_KGDB_KDB
 void kdb_dump_stack_on_cpu(int cpu)
 {
--

2.25.1

maintainer
#7 lore raw
Quoted 35 lines · Sumit Garg

+ stable ML

Quoted 12 lines · Daniel Thompson
Quoted 9 lines · Sumit Garg

Currently breakpoints in kernel .init.text section are not handled correctly while allowing to remove them even after corresponding pages have been freed.

Fix it via killing .init.text section breakpoints just prior to initmem pages being freed.

Quoted 7 lines

I saw Andrew has picked this one up. That's ok for me:

I already enriched kgdbtest to cover this (and they pass) so I guess this is also:

Thanks Daniel.

Quoted 8 lines

BTW this is not Cc:ed to stable and I do wonder if it crosses the threshold to be considered a fix rather than a feature. Normally I consider adding safety rails for kgdb to be a new feature but, in this case, the problem would easily ensnare an inexperienced developer who is doing nothing more than debugging their own driver (assuming they correctly marked their probe function as .init) so I think this weighs in favour of being a fix.

Quoted 1 line

Makes sense, Cc:ed stable.

<formletter>

This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

</formletter>

author
#8 lore raw
Quoted 40 lines · Greg KH
Quoted 35 lines · Sumit Garg

+ stable ML

Quoted 12 lines · Daniel Thompson
Quoted 9 lines · Sumit Garg

Currently breakpoints in kernel .init.text section are not handled correctly while allowing to remove them even after corresponding pages have been freed.

Fix it via killing .init.text section breakpoints just prior to initmem pages being freed.

Quoted 7 lines

I saw Andrew has picked this one up. That's ok for me:

I already enriched kgdbtest to cover this (and they pass) so I guess this is also:

Thanks Daniel.

Quoted 8 lines

BTW this is not Cc:ed to stable and I do wonder if it crosses the threshold to be considered a fix rather than a feature. Normally I consider adding safety rails for kgdb to be a new feature but, in this case, the problem would easily ensnare an inexperienced developer who is doing nothing more than debugging their own driver (assuming they correctly marked their probe function as .init) so I think this weighs in favour of being a fix.

Quoted 1 line

Makes sense, Cc:ed stable.

Quoted 8 lines

<formletter>

This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

</formletter>

Thanks for the pointer, let me wait for this patch to land in Linus’ tree and then will drop a mail to [email protected].

-Sumit