Skip to contentSkip to conversation

kgdb light

PATCH In review Ingo Molnar 6 participants 22 messages Feb 10, 2008

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover kgdb light

this is the "kgdb light" tree that has been also posted at:

http://lkml.org/lkml/2008/2/9/236

it is available at:

git://git.kernel.org/pub/scm/linux/kernel/git/mingo/linux-2.6-kgdb.git

See the shortlog below.

various iterations of this have also been included in x86.git for the past 3 months.

This is a slimmed-down and cleaned up version of KGDB that i've created out of the original patches that we submitted two weeks ago. I went over the kgdb patches with Thomas and we cut out everything that we did not like, and cleaned up the result.

KGDB is still just as functional as it was before (i tested it on 32-bit and 64-bit x86) - and any desired extra capability or complexity should be added as a delta improvement, not in this initial merge.

The difference between the original kgdb submission and this submission is best visible in the diffstat:

before: 41 files changed, 4007 insertions(+), 33 deletions(-)
after: 22 files changed, 3448 insertions(+), 2 deletions(-)

what got removed:

- removed _all_ critical path impact, even if KGDB is enabled and active. The only notifier list it is registered in is the die notifiers, but even there it has the minimum priority of -INT_MAX, to be called as the last one of the die notifiers. I removed the 'early trap hook', the trap handler tweaks, everything. KGDB's only impact now are the arch details it implements in arch/x86/kernel/kgdb.c, nothing else.

- removed all the lowlevel serial drivers. KGDB should not be in the business of writing special-purpose Linux drivers. In fact i found a testsystem where the KGDB 8250 driver would not work - it's simply reimplementing the wheel that drivers/serial already implements, and poorly so. Any "early debugging" functionality should be done via extending the early-console concept, not via special-purpose KGDB drivers.

- I have added a redesigned and cleaned up version of the "KGDB over polled consoles" approach (KGDBOC) - i believe this should be the only IO transport for KGDB: it is an extension of the "console" concept - nothing more, nothing less. Netconsole fits this concept quite nicely as well. The moment a console driver is extended with polling functionality, KGDB will be usable through that IO transport, without having to know about hardware details.

- I have removed the longjump code. That code was ugly beyond belief, it tried to fix up KGDB's own faults and needed to hook into all the fault handlers. It is totally, utterly wrong to do it like that. The code now uses pure probe_kernel_address() accesses.

- removed the module symbol hacks - those need a clean solution.

- removed the GTOD/clocksource hacks. If a user uses kdgb for extended periods of time then GTOD clocksources can get out of sync and we might fall back to other clocksources. That is the _right_ thing to do for the kernel, hacking it around to avoid kernel messages was wrong.

- i have removed the softlockup hacks as well.

- removed the toplevel Makefile changes - if any change is needed in that area (i'm not convinced thre is), then those changes need to go through Sam & the kbuild folks.

- removed the might_sleep scheduler hack as well, and the thread_return hack.

- [ and did lots of other cleanups and rewrites as well. ]

as a result, this kgdb series has _obviously_ zero impact on the kernel, because it just does not touch any dangerous codepath. From this point on KGDB can evolve in small, well-controlled baby steps, as all other kernel code as well.

and the resulting kgdb is still very functional: it can still break into a kernel (via SysRq-G), can catch crashes, can single-step, etc. It's already a quite usable first step.

I have tested this tree on x86 32-bit and 64-bit. Other architectures are not expected to be impacted.

Ingo

------------------>
Ingo Molnar (3):
pids: add pid_max prototype
uaccess: add probe_kernel_write()
x86: kgdb support

Jan Kiszka (1):
consoles: polling support, kgdboc

Jason Wessel (2):
kgdb: core
kgdb: document parameters

 Documentation/kernel-parameters.txt |    5 +
 arch/x86/Kconfig                    |    4 +
 arch/x86/kernel/Makefile            |    1 +
 arch/x86/kernel/kgdb.c              |  550 ++++++++++
 drivers/char/tty_io.c               |   47 +
 drivers/serial/8250.c               |   62 ++
 drivers/serial/Kconfig              |    3 +
 drivers/serial/Makefile             |    1 +
 drivers/serial/kgdboc.c             |  164 +++
 drivers/serial/serial_core.c        |   67 ++-
 include/asm-generic/kgdb.h          |   93 ++
 include/asm-x86/kgdb.h              |   87 ++
 include/linux/kgdb.h                |  264 +++++
 include/linux/pid.h                 |    2 +
 include/linux/serial_core.h         |    4 +
 include/linux/tty_driver.h          |   12 +
 include/linux/uaccess.h             |   22 +
 kernel/Makefile                     |    1 +
 kernel/kgdb.c                       | 2020 +++++++++++++++++++++++++++++++++++
 kernel/sysctl.c                     |    2 +-
 lib/Kconfig.debug                   |    2 +
 lib/Kconfig.kgdb                    |   37 +
 22 files changed, 3448 insertions(+), 2 deletions(-)
 create mode 100644 arch/x86/kernel/kgdb.c
 create mode 100644 drivers/serial/kgdboc.c
 create mode 100644 include/asm-generic/kgdb.h
 create mode 100644 include/asm-x86/kgdb.h
 create mode 100644 include/linux/kgdb.h
 create mode 100644 kernel/kgdb.c
 create mode 100644 lib/Kconfig.kgdb
#1 lore raw

From: Ingo Molnar <[email protected]>
Date: Sun, 10 Feb 2008 08:13:04 +0100

Quoted 9 lines

this is the "kgdb light" tree that has been also posted at:

   http://lkml.org/lkml/2008/2/9/236

it is available at:

   git://git.kernel.org/pub/scm/linux/kernel/git/mingo/linux-2.6-kgdb.git

See the shortlog below.

Thanks for keeping this work alive.

Quoted 5 lines

- removed the GTOD/clocksource hacks. If a user uses kdgb for extended periods of time then GTOD clocksources can get out of sync and we might fall back to other clocksources. That is the _right_ thing to do for the kernel, hacking it around to avoid kernel messages was wrong.

I suspect something will however need to be done with watchdogs and things of that nature which will get very confused if the kernel sits in a breakpoint for a period of time whilst the user looks at things from the kgdb prompt.

Just a heads up...

#2 lore raw
In reply to Ingo Molnar

Hi Ingo.

A few comments based on the git tree pulled as of
~11:00.

We have following header files in the core part:

linux/kgdb.h
asm-generic/kgdb.h

I would expect linux/kgdb.h to contain all the common definitions needed by an arch.
And asm-generic/kgdb.h to list everything that the arch needs to provide to support kgdb. + anything that rely on the arch definitions provided by the arch.

Yet linux/kgdb.h says:
+/*
+ * Functions each KGDB-supporting architecture must provide:
+ */

So I am obviously wrong here. What triggered the division in the two header files?

Some minor specific comments:
+/**
+ * kgdb_skipexception - Bail of of KGDB when we've been triggered.
+ * @exception: Exception vector number
+ * @regs: Current &struct pt_regs.
+ *
+ * On some architectures we need to skip a breakpoint exception when
+ * it occurs after a breakpoint has been removed.
+ */
+int kgdb_skipexception(int exception, struct pt_regs *regs); +
+/**
+ * kgdb_post_master_code - Save error vector/code numbers.
+ * @regs: Original pt_regs.
+ * @e_vector: Original error vector.
+ * @err_code: Original error code.
+ *
+ * This is needed on architectures which support SMP and KGDB.
+ * This function is called after all the slave cpus have been put
+ * to a know spin state and the master CPU has control over KGDB.
+ */
+extern void kgdb_post_master_code(struct pt_regs *regs, int e_vector,
+ int err_code);
+

Please be consistent in use of extern.
Either drop it all over or use it all over.

+#ifndef _KGDB_H_
+#define _KGDB_H_
+
+#include <asm/atomic.h>
+
+#ifdef CONFIG_KGDB
+
+#include <linux/serial_8250.h>
+#include <linux/linkage.h>
+#include <linux/init.h>
+
+#include <asm/kgdb.h>

I do not really see the point of the CONFIG_KGDB ifdef. And if we want it then why not above the inclusion of atomic.h?

Looking a bit closer it looks like all content from asm-generic/kgdb.h is included in linux/kgdb.h - correct?
I assume the header file in asm-generic should have been deleted?

+ */
+void
+sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p); +
Make return value visible on same line as the function to be consistent in style.

+/* Is a host GDB connected to us? */
+int kgdb_connected;
+EXPORT_SYMBOL_GPL(kgdb_connected);
Kernel-doc comments for EXPORTs.

+extern int kgdb_may_fault;
I searched but I could not find any places this
variable were set to anuthing else than 0 neither where it was tested.

lib/Kconfig.kgdb new file +15 -0
new file mode 100644
index 0000000..00263c0
@@ -0,0 +1,37 @@
+
+menuconfig KGDB
+	bool "KGDB: kernel debugging with remote gdb"
+	select FRAME_POINTER
+	depends on HAVE_ARCH_KGDB
+	depends on DEBUG_KERNEL && EXPERIMENTAL
+	help
+	  If you say Y here, it will be possible to remotely debug the
+	  kernel using gdb.  Documentation of kernel debugger is available
+	  at http://kgdb.sourceforge.net as well as in DocBook form
+	  in Documentation/DocBook/.  If unsure, say N.
+
+config HAVE_ARCH_KGDB_SHADOW_INFO
+	bool
+

Please add:
config HAVE_ARCH_KGDB
bool

So we later in x86/Kconfig can do:
config X86
select HAVE_ARCH_KGDB

And we can get rid of (from x86/Kconfig):
+config HAVE_ARCH_KGDB
+ def_bool y

Back to Kconfig.kgdb:

+config KGDBOC
+ tristate "KGDB: use kgdb over the serial console"
+ depends on KGDB

Can we have a more descriptive name here.
For example:
config KGDB_SERIAL_CONSOLE
It is only used in one place so there is no
specific need for such a magic short name.

I see that all my other comments were addresses - thanks.

Sam

#3 lore raw
Quoted 4 lines · Sam Ravnborg

...
+extern int kgdb_may_fault;
I searched but I could not find any places this
variable were set to anuthing else than 0 neither where it was tested.

This is a leftover from the old jmp-on-fault logic that was missed by this tree. I have a patch under test that kills this (and further 150 LOC) from the core.

Quoted 42 lines
lib/Kconfig.kgdb new file +15 -0
new file mode 100644
index 0000000..00263c0
@@ -0,0 +1,37 @@
+
+menuconfig KGDB
+	bool "KGDB: kernel debugging with remote gdb"
+	select FRAME_POINTER
+	depends on HAVE_ARCH_KGDB
+	depends on DEBUG_KERNEL && EXPERIMENTAL
+	help
+	  If you say Y here, it will be possible to remotely debug the
+	  kernel using gdb.  Documentation of kernel debugger is available
+	  at http://kgdb.sourceforge.net as well as in DocBook form
+	  in Documentation/DocBook/.  If unsure, say N.
+
+config HAVE_ARCH_KGDB_SHADOW_INFO
+	bool
+

Please add:
config HAVE_ARCH_KGDB
bool

So we later in x86/Kconfig can do:
config X86
select HAVE_ARCH_KGDB

And we can get rid of (from x86/Kconfig):

lib/Kconfig.kgdb new file +2 -0
+config HAVE_ARCH_KGDB
+	def_bool y

Back to Kconfig.kgdb:

lib/Kconfig.kgdb new file +3 -0
+config KGDBOC
+	tristate "KGDB: use kgdb over the serial console"
+	depends on KGDB
Quoted 6 lines

Can we have a more descriptive name here. For example:
config KGDB_SERIAL_CONSOLE
It is only used in one place so there is no specific need for such a magic short name.

Is the patch below OK? I also added an "if KGB" to unbreak kgdb's kconfig menu again and included two minor cleanups I posted yesterday.

Jan

PS: Ingo, kgdboc runtime re-configuration is broken as you dropped the __init removal from uart_parse_options and uart_set_option (probably due to the ifdef'ery). Better remove it unconditionally?

--------------

Refactor KGDB kbuild menu.

---
 arch/x86/Kconfig        |    4 +---
 drivers/serial/Makefile |    2 +-
 lib/Kconfig.kgdb        |   15 ++++++++++-----
 3 files changed, 12 insertions(+), 9 deletions(-)
arch/x86/Kconfig +1 -3
index 85bcc23..5e0fab5 100644
@@ -22,6 +22,7 @@ config X86
 	select HAVE_IDE
 	select HAVE_OPROFILE
 	select HAVE_KPROBES
+	select HAVE_ARCH_KGDB
 
 config GENERIC_LOCKBREAK
 	def_bool n
@@ -144,9 +145,6 @@ config AUDIT_ARCH
 config ARCH_SUPPORTS_AOUT
 	def_bool y
 
-config HAVE_ARCH_KGDB
-	def_bool y
-
 # Use the generic interrupt handling code in kernel/irq/:
 config GENERIC_HARDIRQS
 	bool
drivers/serial/Makefile +1 -1
index 1d8ee05..dfd8e72 100644
@@ -66,5 +66,5 @@ obj-$(CONFIG_SERIAL_UARTLITE) += uartlite.o
 obj-$(CONFIG_SERIAL_NETX) += netx-serial.o
 obj-$(CONFIG_SERIAL_OF_PLATFORM) += of_serial.o
 obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o
-obj-$(CONFIG_KGDBOC) += kgdboc.o
+obj-$(CONFIG_KGDB_IO_SERIAL_CONSOLE) += kgdboc.o
 obj-$(CONFIG_SERIAL_QE) += ucc_uart.o
lib/Kconfig.kgdb +10 -5
index 00263c0..947c7f1 100644
@@ -10,12 +10,16 @@ menuconfig KGDB
 	  at http://kgdb.sourceforge.net as well as in DocBook form
 	  in Documentation/DocBook/.  If unsure, say N.
 
+if KGDB
+
+config HAVE_ARCH_KGDB
+	bool
+
 config HAVE_ARCH_KGDB_SHADOW_INFO
 	bool
 
 config KGDB_CONSOLE_OUTPUT
-	bool "KGDB: Console messages through gdb"
-	depends on KGDB
+	bool "Console messages through gdb"
 	help
 	  If you say Y here, console messages will appear through gdb.
 	  Other consoles such as tty or ttyS will continue to work as usual.
@@ -26,12 +30,13 @@ config KGDB_CONSOLE_OUTPUT
 	  to use NETCONSOLE in conjunction with KGDBOE instead of
 	  KGDB_CONSOLE.
 
-config KGDBOC
-	tristate "KGDB: use kgdb over the serial console"
-	depends on KGDB
+config KGDB_IO_SERIAL_CONSOLE
+	tristate "Use kgdb over the serial console"
 	select CONSOLE_POLL
 	select MAGIC_SYSRQ
 	default y
 	help
 	  Share a serial console with kgdb. Sysrq-g must be used
 	  to break in initially.
+
+endif
#4 lore raw
Quoted 52 lines · Jan Kiszka
Quoted 4 lines · Sam Ravnborg

...
+extern int kgdb_may_fault;
I searched but I could not find any places this
variable were set to anuthing else than 0 neither where it was tested.

This is a leftover from the old jmp-on-fault logic that was missed by this tree. I have a patch under test that kills this (and further 150 LOC) from the core.

Quoted 42 lines
lib/Kconfig.kgdb new file +15 -0
new file mode 100644
index 0000000..00263c0
@@ -0,0 +1,37 @@
+
+menuconfig KGDB
+	bool "KGDB: kernel debugging with remote gdb"
+	select FRAME_POINTER
+	depends on HAVE_ARCH_KGDB
+	depends on DEBUG_KERNEL && EXPERIMENTAL
+	help
+	  If you say Y here, it will be possible to remotely debug the
+	  kernel using gdb.  Documentation of kernel debugger is available
+	  at http://kgdb.sourceforge.net as well as in DocBook form
+	  in Documentation/DocBook/.  If unsure, say N.
+
+config HAVE_ARCH_KGDB_SHADOW_INFO
+	bool
+

Please add:
config HAVE_ARCH_KGDB
bool

So we later in x86/Kconfig can do:
config X86
select HAVE_ARCH_KGDB

And we can get rid of (from x86/Kconfig):

lib/Kconfig.kgdb new file +2 -0
+config HAVE_ARCH_KGDB
+	def_bool y

Back to Kconfig.kgdb:

lib/Kconfig.kgdb new file +3 -0
+config KGDBOC
+	tristate "KGDB: use kgdb over the serial console"
+	depends on KGDB
Quoted 9 lines
Quoted 6 lines

Can we have a more descriptive name here. For example:
config KGDB_SERIAL_CONSOLE
It is only used in one place so there is no specific need for such a magic short name.

Is the patch below OK? I also added an "if KGB" to unbreak kgdb's kconfig menu again and included two minor cleanups I posted yesterday.

Looks OK - but I think Ingo already addressed this.

Sam

#5 lore raw
Quoted 60 lines · Sam Ravnborg
Quoted 50 lines · Jan Kiszka
Quoted 4 lines · Sam Ravnborg

...
+extern int kgdb_may_fault;
I searched but I could not find any places this
variable were set to anuthing else than 0 neither where it was tested.

This is a leftover from the old jmp-on-fault logic that was missed by this tree. I have a patch under test that kills this (and further 150 LOC) from the core.

Quoted 41 lines
lib/Kconfig.kgdb new file +15 -0
new file mode 100644
index 0000000..00263c0
@@ -0,0 +1,37 @@
+
+menuconfig KGDB
+	bool "KGDB: kernel debugging with remote gdb"
+	select FRAME_POINTER
+	depends on HAVE_ARCH_KGDB
+	depends on DEBUG_KERNEL && EXPERIMENTAL
+	help
+	  If you say Y here, it will be possible to remotely debug the
+	  kernel using gdb.  Documentation of kernel debugger is available
+	  at http://kgdb.sourceforge.net as well as in DocBook form
+	  in Documentation/DocBook/.  If unsure, say N.
+
+config HAVE_ARCH_KGDB_SHADOW_INFO
+	bool
+

Please add:
config HAVE_ARCH_KGDB
bool

So we later in x86/Kconfig can do:
config X86
select HAVE_ARCH_KGDB

And we can get rid of (from x86/Kconfig):

lib/Kconfig.kgdb new file +2 -0
+config HAVE_ARCH_KGDB
+	def_bool y

Back to Kconfig.kgdb:

lib/Kconfig.kgdb new file +3 -0
+config KGDBOC
+	tristate "KGDB: use kgdb over the serial console"
+	depends on KGDB
Quoted 8 lines
Quoted 6 lines

Can we have a more descriptive name here. For example:
config KGDB_SERIAL_CONSOLE
It is only used in one place so there is no specific need for such a magic short name.

Is the patch below OK? I also added an "if KGB" to unbreak kgdb's kconfig menu again and included two minor cleanups I posted yesterday.

Quoted 1 line

Looks OK - but I think Ingo already addressed this.

Should be merged meanwhile.

At this chance: Is it necessary that prompt-less configs like HAVE_ARCH_KGDB break the menu indention? I had to work around this via "if KGDB" (or even "depends on"). This is easy to miss IMHO.

Jan

#6 lore raw
In reply to Sam Ravnborg
Quoted 5 lines · Sam Ravnborg
Quoted 3 lines

Is the patch below OK? I also added an "if KGB" to unbreak kgdb's kconfig menu again and included two minor cleanups I posted yesterday.

Looks OK - but I think Ingo already addressed this.

i picked up everything from Jan that would apply - please yell if you still see something not absolutely squeeky clean ;-)

btw., we should convert all those current:

config ARCH_POPULATES_NODE_MAP
def_bool y

config AUDIT_ARCH

         bool
         default X86_64

config ARCH_SUPPORTS_AOUT
def_bool y

instances to select lines after config X86 / config X86_64.

KGDB just followed the current Kconfig style status quo.

Ingo

#7 lore raw
Quoted 10 lines

btw., we should convert all those current:

config ARCH_POPULATES_NODE_MAP
def_bool y

config AUDIT_ARCH

         bool
         default X86_64
Quoted 6 lines

config ARCH_SUPPORTS_AOUT
def_bool y

instances to select lines after config X86 / config X86_64.

KGDB just followed the current Kconfig style status quo.

Indeed. I just do not have time to do so. A day-time job that is not about kernel-hacking, three kids and a wife etc. consumes some decent amount of time and leaves only a few hours for linux. And I am a strange person that actually needs to sleep several hours each night (as my baby daughter permits me to).

I promote the "select HAVE_*" style on all new stuff and expect that a herd of janitors one day pick it up and convert most of the old-style stuff.
And if you look around then you will see that for this merge window almost all new stuff used "select HAVE_" so we on the right track.

Sam

#8 lore raw
Quoted 5 lines · Sam Ravnborg

I promote the "select HAVE_*" style on all new stuff and expect that a herd of janitors one day pick it up and convert most of the old-style stuff. And if you look around then you will see that for this merge window almost all new stuff used "select HAVE_" so we on the right track.

yeah, it's not like i'm complaining - you are doing a terrific job with kbuild and kconfig.

Perhaps instead of outright converting it (which needs thought and real hard work), how about just running a quick script over all Kconfigs and marking old-style entries as:

# TODO: this stuff needs to be fixed, see Documentation/blah.txt

? Should be 15 minutes of awk and can be committed right after -rc1. Annoying messages like that tend to get the attention of architecture maintainers :-) The Kconfig rules are not always obvious so people need help.

Ingo

#9 lore raw

[git pull] kgdb light, v5

In reply to Sam Ravnborg
Quoted 4 lines · Sam Ravnborg

Hi Ingo.

A few comments based on the git tree pulled as of
~11:00.

Thanks Sam for all your comments - i've implemented all of your suggestions (see below for the fine details and shortlog and patches, etc.). I've backmerged all the fixlets to keep the splitup clean, merged the tree to latest -git, and uploaded the resulting new tree to:

git://git.kernel.org/pub/scm/linux/kernel/git/mingo/linux-2.6-kgdb.git

(and i have build, boot and functionality tested it as well.) Shortlog, diffstat and patch included further below. Tip of the tree is commit b4c015cde3cb272ab931f88666f787766402688e.

But before i go into the fine details of your observations, let me please point out one fundamental thing that people might be asking: why are we arch/x86 maintainers even bothering about all this?

After all the Linux kernel has already got _three_ separate kgdb remote debugging stubs implemented in the current upstream tree:

arch/mips/kernel/gdb-stub.c
arch/frv/kernel/gdb-stub.c
arch/mn10300/kernel/gdb-stub.c

one of them was merged upstream just 2 days ago...

So we could have done it the same way, by doing cp kernel/kgdb.c arch/x86/kernel/gdb-stub.c and merging that. Nobody could have said a _single_ word - we already have lowlevel UART code in early_printk.c that we could have reused.

but we wanted to do it _right_ and not add an arch/x86/kernel/gdb-stub.c special hack. We wanted to do it the same way we did it with genirq, gtod, hrtimers, clockevents, dynticks and all the other facilities. We could have easily implemented dynticks the same way ARM did, via arch level hackery.

so lets please all keep that goal in mind. This is not about "will we have KGDB support or not", this is about "WHERE will we have KGDB support", and i strongly support the notion that it should be in the core kernel, where we can keep it clean, tidy and architecturally agile.

Quoted 4 lines

We have following header files in the core part:

linux/kgdb.h
asm-generic/kgdb.h

oh, i've already eliminated asm-generic/kgdb.h altogether - just forgot to delete the old asm-generic/kgdb.h file. If you look at the code nothing references that file anymore. I zapped it from the tree and uploaded a new kgdb.git.

Quoted 2 lines

I would expect linux/kgdb.h to contain all the common definitions needed by an arch.

agreed.

Quoted 2 lines

So I am obviously wrong here. What triggered the division in the two header files?

nothing, asm-generic/kgdb.h is now gone.

(this was the result of cleanups suggested by Christoph earlier today, i just forgot to do the last step and remove the file.)

Quoted 4 lines

Some minor specific comments:

Please be consistent in use of extern.
Either drop it all over or use it all over.

fixed. (there were about 10 other places that needed fixing.)

(the right thing is to carry an extern anywhere - to make it stand separate from inlines visually.)

Quoted 1 line

I do not really see the point of the CONFIG_KGDB ifdef.

fixed.

Quoted 1 line

Kernel-doc comments for EXPORTs.

fixed. (and i documented the other ones too that were missing.)

Quoted 3 lines

+extern int kgdb_may_fault;
I searched but I could not find any places this
variable were set to anuthing else than 0 neither where it was tested.

thanks, zapped. This was a leftover from my cut-slack efforts :-) This got rid of a good 50 lines of code as well. Thanks!

Review

Quoted 11 lines

Please add:
config HAVE_ARCH_KGDB
bool

So we later in x86/Kconfig can do:
config X86
select HAVE_ARCH_KGDB

And we can get rid of (from x86/Kconfig):

(quoted diff) +2 -0
+config HAVE_ARCH_KGDB
+	def_bool y

thanks, all of these done.

Quoted 9 lines

Back to Kconfig.kgdb:

(quoted diff) +3 -0
+config KGDBOC
+	tristate "KGDB: use kgdb over the serial console"
+	depends on KGDB

Can we have a more descriptive name here.
For example:
config KGDB_SERIAL_CONSOLE

done.

Quoted 1 line

I see that all my other comments were addresses - thanks.

cool :-) your observations were very useful and substantial. Please keep them coming.

Ingo

------------------>
Ingo Molnar (3):
pids: add pid_max prototype
uaccess: add probe_kernel_write()
x86: kgdb support

Jason Wessel (3):
kgdb: core
consoles: polling support, kgdboc
kgdb: document parameters

 Documentation/kernel-parameters.txt |    5 +
 arch/x86/Kconfig                    |    2 +
 arch/x86/kernel/Makefile            |    1 +
 arch/x86/kernel/kgdb.c              |  672 ++++++++++++
 drivers/char/tty_io.c               |   47 +
 drivers/serial/8250.c               |   62 ++
 drivers/serial/Kconfig              |    3 +
 drivers/serial/Makefile             |    1 +
 drivers/serial/kgdboc.c             |  164 +++
 drivers/serial/serial_core.c        |   71 ++-
 include/asm-x86/kgdb.h              |   85 ++
 include/linux/kgdb.h                |  329 ++++++
 include/linux/pid.h                 |    2 +
 include/linux/serial_core.h         |    4 +
 include/linux/tty_driver.h          |   12 +
 include/linux/uaccess.h             |   22 +
 kernel/Makefile                     |    1 +
 kernel/kgdb.c                       | 2005 +++++++++++++++++++++++++++++++++++
 kernel/sysctl.c                     |    2 +-
 lib/Kconfig.debug                   |    2 +
 lib/Kconfig.kgdb                    |   40 +
 21 files changed, 3528 insertions(+), 4 deletions(-)
 create mode 100644 arch/x86/kernel/kgdb.c
 create mode 100644 drivers/serial/kgdboc.c
 create mode 100644 include/asm-x86/kgdb.h
 create mode 100644 include/linux/kgdb.h
 create mode 100644 kernel/kgdb.c
 create mode 100644 lib/Kconfig.kgdb
Documentation/kernel-parameters.txt +5 -0
index a4fc7fc..6e97307 100644
@@ -930,6 +930,11 @@ and is between 256 and 4096 characters. It is defined in the file
 	kstack=N	[X86-32,X86-64] Print N words from the kernel stack
 			in oops dumps.
 
+	kgdboc=		[HW] kgdb over consoles.
+			Requires a tty driver that supports console polling.
+			(only serial suported for now)
+			Format: <serial_device>[,baud]
+
 	l2cr=		[PPC]
 
 	lapic		[X86-32,APIC] Enable the local APIC even if BIOS
arch/x86/Kconfig +2 -0
index aaed1a3..5e0fab5 100644
@@ -14,6 +14,7 @@ config X86_32
 
 config X86_64
 	def_bool 64BIT
+	select HAVE_ARCH_KGDB_SHADOW_INFO
 
 ### Arch settings
 config X86
@@ -21,6 +22,7 @@ config X86
 	select HAVE_IDE
 	select HAVE_OPROFILE
 	select HAVE_KPROBES
+	select HAVE_ARCH_KGDB
 
 config GENERIC_LOCKBREAK
 	def_bool n
arch/x86/kernel/Makefile +1 -0
index 76ec0f8..4cd39cd 100644
@@ -58,6 +58,7 @@ obj-$(CONFIG_MODULES)		+= module_$(BITS).o
 obj-$(CONFIG_ACPI_SRAT) 	+= srat_32.o
 obj-$(CONFIG_EFI) 		+= efi.o efi_$(BITS).o efi_stub_$(BITS).o
 obj-$(CONFIG_DOUBLEFAULT) 	+= doublefault_32.o
+obj-$(CONFIG_KGDB)		+= kgdb.o
 obj-$(CONFIG_VM86)		+= vm86_32.o
 obj-$(CONFIG_EARLY_PRINTK)	+= early_printk.o
 
arch/x86/kernel/kgdb.c new file +672 -0

672 lines not on this page · raw message

drivers/char/tty_io.c +47 -0

59 lines not on this page · raw message

drivers/serial/8250.c +62 -0

74 lines not on this page · raw message

drivers/serial/Kconfig +3 -0
index b82595c..5d9667c 100644
@@ -961,6 +961,9 @@ config SERIAL_CORE
 config SERIAL_CORE_CONSOLE
 	bool
 
+config CONSOLE_POLL
+	bool
+
 config SERIAL_68328
 	bool "68328 serial support"
 	depends on M68328 || M68EZ328 || M68VZ328
drivers/serial/Makefile +1 -0
index 640cfe4..3cbea54 100644
@@ -66,4 +66,5 @@ obj-$(CONFIG_SERIAL_UARTLITE) += uartlite.o
 obj-$(CONFIG_SERIAL_NETX) += netx-serial.o
 obj-$(CONFIG_SERIAL_OF_PLATFORM) += of_serial.o
 obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o
+obj-$(CONFIG_KGDB_SERIAL_CONSOLE) += kgdboc.o
 obj-$(CONFIG_SERIAL_QE) += ucc_uart.o
drivers/serial/kgdboc.c new file +164 -0

164 lines not on this page · raw message

drivers/serial/serial_core.c +68 -3

101 lines not on this page · raw message

include/asm-x86/kgdb.h new file +85 -0

85 lines not on this page · raw message

include/linux/kgdb.h new file +329 -0

329 lines not on this page · raw message

include/linux/pid.h +2 -0
index f84d532..e066d3d 100644
@@ -86,6 +86,8 @@ extern struct task_struct *FASTCALL(get_pid_task(struct pid *pid,
 
 extern struct pid *get_task_pid(struct task_struct *task, enum pid_type type);
 
+extern int pid_max;
+
 /*
  * attach_pid() and detach_pid() must be called with the tasklist_lock
  * write-held.
include/linux/serial_core.h +4 -0
index 1a0b6cf..7d1a46b 100644
@@ -211,6 +211,10 @@ struct uart_ops {
 	void		(*config_port)(struct uart_port *, int);
 	int		(*verify_port)(struct uart_port *, struct serial_struct *);
 	int		(*ioctl)(struct uart_port *, unsigned int, unsigned long);
+#ifdef CONFIG_CONSOLE_POLL
+	void	(*poll_put_char)(struct uart_port *, unsigned char);
+	int		(*poll_get_char)(struct uart_port *);
+#endif
 };
 
 #define UART_CONFIG_TYPE	(1 << 0)
include/linux/tty_driver.h +12 -0
index 85c95cd..21f69ac 100644
@@ -125,6 +125,7 @@
 #include <linux/cdev.h>
 
 struct tty_struct;
+struct tty_driver;
 
 struct tty_operations {
 	int  (*open)(struct tty_struct * tty, struct file * filp);
@@ -157,6 +158,11 @@ struct tty_operations {
 	int (*tiocmget)(struct tty_struct *tty, struct file *file);
 	int (*tiocmset)(struct tty_struct *tty, struct file *file,
 			unsigned int set, unsigned int clear);
+#ifdef CONFIG_CONSOLE_POLL
+	int (*poll_init)(struct tty_driver *driver, int line, char *options);
+	int (*poll_get_char)(struct tty_driver *driver, int line);
+	void (*poll_put_char)(struct tty_driver *driver, int line, char ch);
+#endif
 };
 
 struct tty_driver {
@@ -220,6 +226,11 @@ struct tty_driver {
 	int (*tiocmget)(struct tty_struct *tty, struct file *file);
 	int (*tiocmset)(struct tty_struct *tty, struct file *file,
 			unsigned int set, unsigned int clear);
+#ifdef CONFIG_CONSOLE_POLL
+	int (*poll_init)(struct tty_driver *driver, int line, char *options);
+	int (*poll_get_char)(struct tty_driver *driver, int line);
+	void (*poll_put_char)(struct tty_driver *driver, int line, char ch);
+#endif
 
 	struct list_head tty_drivers;
 };
@@ -230,6 +241,7 @@ struct tty_driver *alloc_tty_driver(int lines);
 void put_tty_driver(struct tty_driver *driver);
 void tty_set_operations(struct tty_driver *driver,
 			const struct tty_operations *op);
+extern struct tty_driver *tty_find_polling_driver(char *name, int *line);
 
 /* tty driver magic number */
 #define TTY_DRIVER_MAGIC		0x5402
include/linux/uaccess.h +22 -0
index 975c963..98cfe02 100644
@@ -84,4 +84,26 @@ static inline unsigned long __copy_from_user_nocache(void *to,
 		ret;					\
 	})
 
+/**
+ * probe_kernel_write(): safely attempt to write to a location
+ * @addr: address to write to - its type is type typeof(rdval)*
+ * @rdval: write to this variable
+ *
+ * Safely write to address @addr from variable @rdval.  If a kernel fault
+ * happens, handle that and return -EFAULT.
+ */
+#define probe_kernel_write(addr, rdval)			\
+	({						\
+		long ret;				\
+		mm_segment_t old_fs = get_fs();		\
+							\
+		set_fs(KERNEL_DS);			\
+		pagefault_disable();			\
+		ret = __put_user(rdval,			\
+	 (__force typeof(rdval) __user *)(addr));	\
+		pagefault_enable();			\
+		set_fs(old_fs);				\
+		ret;					\
+	})
+
 #endif		/* __LINUX_UACCESS_H__ */
kernel/Makefile +1 -0
index 6c584c5..05c8003 100644
@@ -53,6 +53,7 @@ obj-$(CONFIG_AUDIT) += audit.o auditfilter.o
 obj-$(CONFIG_AUDITSYSCALL) += auditsc.o
 obj-$(CONFIG_AUDIT_TREE) += audit_tree.o
 obj-$(CONFIG_KPROBES) += kprobes.o
+obj-$(CONFIG_KGDB) += kgdb.o
 obj-$(CONFIG_DETECT_SOFTLOCKUP) += softlockup.o
 obj-$(CONFIG_GENERIC_HARDIRQS) += irq/
 obj-$(CONFIG_SECCOMP) += seccomp.o
kernel/kgdb.c new file +2005 -0

1,378 lines not on this page · raw message

kernel/sysctl.c +1 -1
index d41ef6b..97a152c 100644

Lines of this file are not shown. View the raw message.

lib/Kconfig.debug +2 -0
index a370fe8..a904916 100644

Lines of this file are not shown. View the raw message.

lib/Kconfig.kgdb new file +40 -0
new file mode 100644
index 0000000..a739e9d

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

#10 lore raw

Re: [git pull] kgdb light, v5

Quoted 32 lines · Ingo Molnar
kernel/kgdb.c new file +32 -0
+#ifdef CONFIG_64BIT
+       } else if ((count == 8) && (((long)mem & 7) == 0)) {
+               u64 tmp_ll;
+               if (probe_kernel_address(mem, tmp_ll))
+                       return ERR_PTR(-EINVAL);
+
+               mem += 8;
+#ifdef __BIG_ENDIAN
+               *buf++ = hexchars[(tmp_ll >> 60) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 56) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 52) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 48) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 44) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 40) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 36) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 32) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 28) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 24) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 20) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 16) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 12) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 8) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 4) & 0xf];
+               *buf++ = hexchars[tmp_ll & 0xf];
+#else
+               *buf++ = hexchars[(tmp_ll >> 4) & 0xf];
+               *buf++ = hexchars[tmp_ll & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 12) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 8) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 20) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 16) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 28) & 0xf];

kernel/kgdb.c line 443

Quoted 10 lines
kernel/kgdb.c new file +10 -0
+               *buf++ = hexchars[(tmp_ll >> 24) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 36) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 32) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 44) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 40) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 52) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 48) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 60) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 56) & 0xf];
+#endif

Am I missing something? (I'm due, so it's not really a rhetorical question :-) .) Wouldn't

unsigned int void u64_to_hex(u64 val, unsigned char *buf)

{
         int i;
         for (i=15; i>=0; i--) {
                  buf[i] = hexchars[ val & 0x0f ];
                  val >>= 4;
         }
         return 16;
}
...
         buf += u64_to_hex(cpu_to_be64(tmp_ll), buf);

be clearer both visually, and code-as-intent? (And equivalent helpers for u32, u16 -- though they could all be rolled into one.)

#11 lore raw

Re: [git pull] kgdb light, v5

Quoted 59 lines · Ray Lee
Quoted 32 lines · Ingo Molnar
kernel/kgdb.c new file +32 -0
+#ifdef CONFIG_64BIT
+       } else if ((count == 8) && (((long)mem & 7) == 0)) {
+               u64 tmp_ll;
+               if (probe_kernel_address(mem, tmp_ll))
+                       return ERR_PTR(-EINVAL);
+
+               mem += 8;
+#ifdef __BIG_ENDIAN
+               *buf++ = hexchars[(tmp_ll >> 60) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 56) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 52) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 48) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 44) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 40) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 36) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 32) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 28) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 24) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 20) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 16) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 12) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 8) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 4) & 0xf];
+               *buf++ = hexchars[tmp_ll & 0xf];
+#else
+               *buf++ = hexchars[(tmp_ll >> 4) & 0xf];
+               *buf++ = hexchars[tmp_ll & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 12) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 8) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 20) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 16) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 28) & 0xf];

kernel/kgdb.c line 443

Quoted 10 lines
kernel/kgdb.c new file +10 -0
+               *buf++ = hexchars[(tmp_ll >> 24) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 36) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 32) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 44) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 40) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 52) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 48) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 60) & 0xf];
+               *buf++ = hexchars[(tmp_ll >> 56) & 0xf];
+#endif

Am I missing something? (I'm due, so it's not really a rhetorical question :-) .) Wouldn't

unsigned int void u64_to_hex(u64 val, unsigned char *buf)

{
         int i;
         for (i=15; i>=0; i--) {
                  buf[i] = hexchars[ val & 0x0f ];
                  val >>= 4;
         }
         return 16;
}
...
         buf += u64_to_hex(cpu_to_be64(tmp_ll), buf);
Quoted 2 lines

be clearer both visually, and code-as-intent? (And equivalent helpers for u32, u16 -- though they could all be rolled into one.)

Yes, will come, I just produced some ETOOMANYCHANGESATONCE issue while improving this. The code goes down by more than 150 lines!

Jan

#12 lore raw

Re: [git pull] kgdb light, v5

Quoted 13 lines · Jan Kiszka
Quoted 12 lines · Ray Lee

unsigned int void u64_to_hex(u64 val, unsigned char *buf)

{
         int i;
         for (i=15; i>=0; i--) {
                  buf[i] = hexchars[ val & 0x0f ];
                  val >>= 4;
         }
         return 16;
}
...
         buf += u64_to_hex(cpu_to_be64(tmp_ll), buf);
Quoted 5 lines
Quoted 2 lines

be clearer both visually, and code-as-intent? (And equivalent helpers for u32, u16 -- though they could all be rolled into one.)

Yes, will come, I just produced some ETOOMANYCHANGESATONCE issue while improving this. The code goes down by more than 150 lines!

Thanks for responding. I'd mentioned it twice already and gotten no response, so wasn't sure if my microphone was on :-).

If you're feeling energetic, the same sort of cleanup can be done against:

+#ifdef __BIG_ENDIAN

+               tmp_s |= hex(*buf++) << 12;
+               tmp_s |= hex(*buf++) << 8;
+               tmp_s |= hex(*buf++) << 4;
+               tmp_s |= hex(*buf++);
+#else
+               tmp_s |= hex(*buf++) << 4;
+               tmp_s |= hex(*buf++);
+               tmp_s |= hex(*buf++) << 12;
+               tmp_s |= hex(*buf++) << 8;

+#endif
+ if (probe_kernel_write(mem, tmp_s))
+ return ERR_PTR(-EINVAL);
+
+ mem += 2;
+ } else if ((count == 4) && (((long)mem & 3) == 0)) {
+ u32 tmp_l = 0;
+
+#ifdef __BIG_ENDIAN

+               tmp_l |= hex(*buf++) << 28;
+               tmp_l |= hex(*buf++) << 24;
+               tmp_l |= hex(*buf++) << 20;
+               tmp_l |= hex(*buf++) << 16;
+               tmp_l |= hex(*buf++) << 12;
+               tmp_l |= hex(*buf++) << 8;
+               tmp_l |= hex(*buf++) << 4;
+               tmp_l |= hex(*buf++);
+#else
+               tmp_l |= hex(*buf++) << 4;
+               tmp_l |= hex(*buf++);
+               tmp_l |= hex(*buf++) << 12;
+               tmp_l |= hex(*buf++) << 8;
+               tmp_l |= hex(*buf++) << 20;
+               tmp_l |= hex(*buf++) << 16;
+               tmp_l |= hex(*buf++) << 28;
+               tmp_l |= hex(*buf++) << 24;

+#endif
+ if (probe_kernel_write(mem, tmp_l))
+ return ERR_PTR(-EINVAL);
+ mem += 4;

As in, write a helper to parse a hex16, hex32, hex64, then do a be[16|32|64]_to_cpu, and probe_kernel_write the result.

u64 hex_to_u64(unsigned char *buf)

{
        int i;
        u64 val = 0;

        for (i=0; i<16; i++)
                val = val<<4 | hex(buf[i]);   /* or *buf++, take your pick */
        return val;
}

...

        if (probe_kernel_write(mem, be64_to_cpu(hex_to_u64(unsigned char *buf)))
                return ERR_PTR(-EINVAL));
        mem += 4;
        buf += 16;

Or, you know, something like that.

#13 lore raw

Re: [git pull] kgdb light, v5

In reply to Ray Lee

[This still runs fine here, but sharp eyes are always welcome!]

Cleanup of the way kgdb
- accesses unsafe memory via probe_kernel_*
- converts to/from hex representation
- passes errors due to such accesses around

At this chance I also fix kgdb_ebin2mem, which was broken /wrt escape sequence handling.

---
 include/linux/kgdb.h |    4 
 kernel/kgdb.c        |  349 +++++++++++++++------------------------------------
 2 files changed, 109 insertions(+), 244 deletions(-)
include/linux/kgdb.h +2 -2
index 7f4ee55..0359280 100644
@@ -313,8 +313,8 @@ extern int kgdb_register_io_module(struct kgdb_io *local_kgdb_io_ops);
 extern void kgdb_unregister_io_module(struct kgdb_io *local_kgdb_io_ops);
 
 extern int kgdb_hex2long(char **ptr, long *long_val);
-extern char *kgdb_mem2hex(char *mem, char *buf, int count);
-extern char *kgdb_hex2mem(char *buf, char *mem, int count);
+extern int kgdb_mem2hex(char *mem, char *buf, int count);
+extern int kgdb_hex2mem(char *buf, char *mem, int count);
 
 extern int kgdb_isremovedbreak(unsigned long addr);
 
kernel/kgdb.c +107 -242

481 lines not on this page · raw message

#14 lore raw

Re: [git pull] kgdb light, v5

Quoted 9 lines · Jan Kiszka

[This still runs fine here, but sharp eyes are always welcome!]

Cleanup of the way kgdb
- accesses unsafe memory via probe_kernel_*
- converts to/from hex representation
- passes errors due to such accesses around

At this chance I also fix kgdb_ebin2mem, which was broken /wrt escape sequence handling.

great! Applied. The kgdb memory accesses look _much_ cleaner now, and it's a significant simplification as well:

   2 files changed, 109 insertions(+), 244 deletions(-)

the uaccess.h macros look a bit ugly now (it all got inherited from the existing [and ancient] probe_kernel_address() macro) and because we now touch it materially anyway, we might as well do it right?

Ingo

#15 lore raw

Re: [git pull] kgdb light, v5

In reply to Jan Kiszka

kernel/kgdb.c static struct notifier_block kgdb_reboot_notifier = { · line 154

Quoted 8 lines · Jan Kiszka
kernel/kgdb.c +4 -0
+static int kgdb_get_mem(char *addr, unsigned char *buf, int count)
 {
+	if ((unsigned long)addr < TASK_SIZE)
+		return -EFAULT;
 
+	return probe_kernel_read(buf, addr, count);
 }

Ok, so this is a pretty function after all the cleanups, but I actually don't think that "if ((unsigned long)addr < TASK_SIZE)" is really even asked for.

Why not let kgdb look at user memory? I'd argue that in a lot of cases, it might be quite nice to do, to see what user arguments in memory are etc etc (think things like futexes, where user memory contents really do matter).

So I'd suggest getting rid of the whole "kgdb_{get|set}_mem()" functions, and just using "probe_kernel_{read|write}()" directly instead.

(Not that I necessarily love those names either, but whatever..)

The TASK_SIZE checks make more sense in kgdb_validate_break_address() and friends, where it actually does make sense to check that it's really a *kernel* address.

But even there, I'm not sure if the right check is to compare against TASK_SIZE, since kernel and user memory addresses can in theory be distinct (that's why we have "set_fs()" historically, and while it's no longer true on x86 and hasn't been in a long time, the kernel conceptually allows it - see my previous reply about that whole get_fs/set_fs thing in the definition of probe_kernel_read/write).

kernel/kgdb.c static void put_packet(char *buffer) · line 351

Quoted 8 lines
kernel/kgdb.c +8 -0
+	if (count == 2 && ((long)mem & 1) == 0)
+		err = probe_kernel_read(tmp, mem, 2);
+	else if (count == 4 && ((long)mem & 3) == 0)
+		err = probe_kernel_read(tmp, mem, 4);
+	else if (count == 8 && ((long)mem & 7) == 0)
+		err = probe_kernel_read(tmp, mem, 8);
+	else
+		err = probe_kernel_read(tmp, mem, count);

There's absolutely no reason to care about the alignment, since if you now use "probe_kernel_read()", the sane thing to do is to just do

	err = probe_kernel_read(tmp, mem, count);
	if (!err) {
		while (count > 0) {
			buf = pack_hex_byte(buf, *tmp);
			tmp++;
			count--;
	}

and you're all done. No?

Linus

#16 lore raw

Re: [git pull] kgdb light, v5

Quoted 11 lines · Linus Torvalds

kernel/kgdb.c static struct notifier_block kgdb_reboot_notifier = { · line 153

Quoted 6 lines
kernel/kgdb.c +4 -0
+static int kgdb_get_mem(char *addr, unsigned char *buf, int count)
 {
+	if ((unsigned long)addr < TASK_SIZE)
+		return -EFAULT;
 
+	return probe_kernel_read(buf, addr, count);

Ok, so this is a pretty function after all the cleanups, but I actually don't think that "if ((unsigned long)addr < TASK_SIZE)" is really even asked for.

Quoted 4 lines

Why not let kgdb look at user memory? I'd argue that in a lot of cases, it might be quite nice to do, to see what user arguments in memory are etc etc (think things like futexes, where user memory contents really do matter).

yes. We should allow kgdb to look at just about anything that can be done safely - and we've got all the necessary protections against pagefaults via pagefault_disable().

Quoted 10 lines

So I'd suggest getting rid of the whole "kgdb_{get|set}_mem()" functions, and just using "probe_kernel_{read|write}()" directly instead.

(Not that I necessarily love those names either, but whatever..)

The TASK_SIZE checks make more sense in kgdb_validate_break_address() and friends, where it actually does make sense to check that it's really a *kernel* address.

Quoted 6 lines

But even there, I'm not sure if the right check is to compare against TASK_SIZE, since kernel and user memory addresses can in theory be distinct (that's why we have "set_fs()" historically, and while it's no longer true on x86 and hasn't been in a long time, the kernel conceptually allows it - see my previous reply about that whole get_fs/set_fs thing in the definition of probe_kernel_read/write).

hm, is access_ok() safe on all architectures from irq context? That's the cross-arch equivalent of TASK_SIZE checks normally.

Quoted 20 lines

kernel/kgdb.c static void put_packet(char *buffer) · line 351

Quoted 8 lines
kernel/kgdb.c +8 -0
+	if (count == 2 && ((long)mem & 1) == 0)
+		err = probe_kernel_read(tmp, mem, 2);
+	else if (count == 4 && ((long)mem & 3) == 0)
+		err = probe_kernel_read(tmp, mem, 4);
+	else if (count == 8 && ((long)mem & 7) == 0)
+		err = probe_kernel_read(tmp, mem, 8);
+	else
+		err = probe_kernel_read(tmp, mem, count);

There's absolutely no reason to care about the alignment, since if you now use "probe_kernel_read()", the sane thing to do is to just do

	err = probe_kernel_read(tmp, mem, count);
	if (!err) {
		while (count > 0) {
			buf = pack_hex_byte(buf, *tmp);
			tmp++;
			count--;
	}
Quoted 1 line

and you're all done. No?

yes, the full function now looks like this:

int kgdb_mem2hex(char *mem, char *buf, int count)

{
	char *tmp;
	int err;

	/*
	 * We use the upper half of buf as an intermediate buffer for the
	 * raw memory copy.  Hex conversion will work against this one.
	 */
	tmp = buf + count;

	err = probe_kernel_read(tmp, mem, count);
	if (!err) {
		while (count > 0) {
			buf = pack_hex_byte(buf, *tmp);
			tmp++;
			count--;
		}

		*buf = 0;
	}

	return err;
}

i'll test this a bit.

Ingo

#17 lore raw

Re: [git pull] kgdb light, v5

In reply to Linus Torvalds
Quoted 7 lines · Linus Torvalds
Quoted 5 lines · Jan Kiszka
kernel/kgdb.c +3 -0
+static int kgdb_get_mem(char *addr, unsigned char *buf, int count)
 {
+	if ((unsigned long)addr < TASK_SIZE)
+		return -EFAULT;
Quoted 8 lines

kernel/kgdb.c static struct notifier_block kgdb_reboot_notifier = { · line 154

Quoted 2 lines
kernel/kgdb.c +1 -0
+	return probe_kernel_read(buf, addr, count);
 }

Ok, so this is a pretty function after all the cleanups, but I actually don't think that "if ((unsigned long)addr < TASK_SIZE)" is really even asked for.

Why not let kgdb look at user memory? I'd argue that in a lot of cases, it might be quite nice to do, to see what user arguments in memory are etc etc (think things like futexes, where user memory contents really do matter).

So I'd suggest getting rid of the whole "kgdb_{get|set}_mem()" functions, and just using "probe_kernel_{read|write}()" directly instead.

Makes indeed more sense.

Quoted 26 lines

(Not that I necessarily love those names either, but whatever..)

The TASK_SIZE checks make more sense in kgdb_validate_break_address() and friends, where it actually does make sense to check that it's really a *kernel* address.

But even there, I'm not sure if the right check is to compare against TASK_SIZE, since kernel and user memory addresses can in theory be distinct (that's why we have "set_fs()" historically, and while it's no longer true on x86 and hasn't been in a long time, the kernel conceptually allows it - see my previous reply about that whole get_fs/set_fs thing in the definition of probe_kernel_read/write).

kernel/kgdb.c static void put_packet(char *buffer) · line 351

Quoted 8 lines
kernel/kgdb.c +8 -0
+	if (count == 2 && ((long)mem & 1) == 0)
+		err = probe_kernel_read(tmp, mem, 2);
+	else if (count == 4 && ((long)mem & 3) == 0)
+		err = probe_kernel_read(tmp, mem, 4);
+	else if (count == 8 && ((long)mem & 7) == 0)
+		err = probe_kernel_read(tmp, mem, 8);
+	else
+		err = probe_kernel_read(tmp, mem, count);

There's absolutely no reason to care about the alignment, since if you now use "probe_kernel_read()", the sane thing to do is to just do

	err = probe_kernel_read(tmp, mem, count);
	if (!err) {
		while (count > 0) {
			buf = pack_hex_byte(buf, *tmp);
			tmp++;
			count--;
	}
Quoted 1 line

and you're all done. No?

Maybe, maybe not. I followed the comment in the original code, saying that we need word-wise access for I/O memory poking. Can I assume across a all archs that __copy_to/from_user will not perform byte accesses if count is 2, 4, or 8? I would be glad if we can kill other couple of line.

Ingo, if you are close to an editor, please pick those up? Here are some offline things cooking on my side...

Jan

#18 lore raw

Re: [git pull] kgdb light, v5

Quoted 5 lines · Jan Kiszka

Maybe, maybe not. I followed the comment in the original code, saying that we need word-wise access for I/O memory poking. Can I assume across a all archs that __copy_to/from_user will not perform byte accesses if count is 2, 4, or 8? I would be glad if we can kill other couple of line.

those architectures should extend mm/maccess.c accordingly. It's now the collector point for "weird kernel-owned memory access functionality".

Quoted 2 lines

Ingo, if you are close to an editor, please pick those up? Here are some offline things cooking on my side...

yeah, fixed these in my tree.

Ingo

#19 lore raw

Re: [git pull] kgdb light, v5

In reply to Linus Torvalds
Quoted 12 lines · Linus Torvalds

kernel/kgdb.c static struct notifier_block kgdb_reboot_notifier = { · line 154

Quoted 7 lines
kernel/kgdb.c +4 -0
+static int kgdb_get_mem(char *addr, unsigned char *buf, int count)
 {
+	if ((unsigned long)addr < TASK_SIZE)
+		return -EFAULT;
 
+	return probe_kernel_read(buf, addr, count);
 }

Ok, so this is a pretty function after all the cleanups, but I actually don't think that "if ((unsigned long)addr < TASK_SIZE)" is really even asked for.

Quoted 8 lines

Why not let kgdb look at user memory? I'd argue that in a lot of cases, it might be quite nice to do, to see what user arguments in memory are etc etc (think things like futexes, where user memory contents really do matter).

So I'd suggest getting rid of the whole "kgdb_{get|set}_mem()" functions, and just using "probe_kernel_{read|write}()" directly instead.

ok, on a second thought: kgdb_{get|set}_mem() is _only_ used to validate and set the software breakpoint (int3). And i think kgdb correctly restricts that to kernel-space addresses only - you can typo an address down into user-space and overwrite user-space memory and not know what hit you ... [you can still explicitly touch user-space memory, but that has to be done intentionally]

So to reduce the confusion i've removed these functions and open-coded the probe_kernel_*() functions into kgdb_validate_break_address() and kgdb_arch_set_breakpoint().

all other places already use probe_kernel_{read|write}. (Now, there are a few stray TASK_SIZE checks still, i'll double check them and convert them to access_ok() checks.)

btw., based on your previous comment about alignment, i found another function that used weird alignment checks, kgdb_hex2mem():

	if (count == 2 && ((long)mem & 1) == 0)
		err = probe_kernel_write(mem, tmp_raw, 2);
	else if (count == 4 && ((long)mem & 3) == 0)
		err = probe_kernel_write(mem, tmp_raw, 4);
	else if (count == 8 && ((long)mem & 7) == 0)
		err = probe_kernel_write(mem, tmp_raw, 8);
	else
		err = probe_kernel_write(mem, tmp_raw, count);

	return err;
}

I just converted it to:

	return probe_kernel_write(mem, tmp_raw, count);

which looks _a lot_ cleaner.

Ingo

#20 lore raw

Re: [git pull] kgdb light, v5

Quoted 3 lines · Ingo Molnar

all other places already use probe_kernel_{read|write}. (Now, there are a few stray TASK_SIZE checks still, i'll double check them and convert them to access_ok() checks.)

all the TASK_SIZE checks relate to the soft breakpoint write accesses.

and access_ok() does not cut it: it's also a bit dangerous from debug
context: uses current->address_space, which is task dependent and can accidentally allow an int3 write to userspace if executed in a kernel thread that has lazy-inherited the TLB from a user task, etc., and it also does not give enough protection on some other architectures.

is_kernel_text() is not good, because it does not cover modules. is_module_address() is not good either, because it also covers module data areas, and is a bit thick (hence crash-risky) as well. So there's no existing facility to cover this.

so i'd say the safest would be to remove the TASK_SIZE check altogether. If someone typoes a raw breakpoint - it is still enumerated by gdb and can still be cleared. It's not like kgdb cannot be used to shoot in one's own foot ...

Ingo

#21 lore raw

Re: [git pull] kgdb light, v5

In reply to Ingo Molnar
Quoted 5 lines

so lets please all keep that goal in mind. This is not about "will we have KGDB support or not", this is about "WHERE will we have KGDB support", and i strongly support the notion that it should be in the core kernel, where we can keep it clean, tidy and architecturally agile.

I think it was good to remind people that this is mostly generic cross-architecture kgdb support and that x86 is just the first adaptor.

Sam