Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1633893

[PATCH 4.4 37/43] MIPS: KGDB: Use kernel context for sleeping threads

From Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Newsgroups linux.kernel
Subject [PATCH 4.4 37/43] MIPS: KGDB: Use kernel context for sleeping threads
Date 2017-05-01 23:40 +0200
Message-ID <tCrd0-5UI-29@gated-at.bofh.it> (permalink)
References <tCr3j-5Nl-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


4.4-stable review patch.  If anyone has any objections, please let me know.

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

From: James Hogan <james.hogan@imgtec.com>

commit 162b270c664dca2e0944308e92f9fcc887151a72 upstream.

KGDB is a kernel debug stub and it can't be used to debug userland as it
can only safely access kernel memory.

On MIPS however KGDB has always got the register state of sleeping
processes from the userland register context at the beginning of the
kernel stack. This is meaningless for kernel threads (which never enter
userland), and for user threads it prevents the user seeing what it is
doing while in the kernel:

(gdb) info threads
  Id   Target Id         Frame
  ...
  3    Thread 2 (kthreadd) 0x0000000000000000 in ?? ()
  2    Thread 1 (init)   0x000000007705c4b4 in ?? ()
  1    Thread -2 (shadowCPU0) 0xffffffff8012524c in arch_kgdb_breakpoint () at arch/mips/kernel/kgdb.c:201

Get the register state instead from the (partial) kernel register
context stored in the task's thread_struct for resume() to restore. All
threads now correctly appear to be in context_switch():

(gdb) info threads
  Id   Target Id         Frame
  ...
  3    Thread 2 (kthreadd) context_switch (rq=<optimized out>, cookie=..., next=<optimized out>, prev=0x0) at kernel/sched/core.c:2903
  2    Thread 1 (init)   context_switch (rq=<optimized out>, cookie=..., next=<optimized out>, prev=0x0) at kernel/sched/core.c:2903
  1    Thread -2 (shadowCPU0) 0xffffffff8012524c in arch_kgdb_breakpoint () at arch/mips/kernel/kgdb.c:201

Call clobbered registers which aren't saved and exception registers
(BadVAddr & Cause) which can't be easily determined without stack
unwinding are reported as 0. The PC is taken from the return address,
such that the state presented matches that found immediately after
returning from resume().

Fixes: 8854700115ec ("[MIPS] kgdb: add arch support for the kernel's kgdb core")
Signed-off-by: James Hogan <james.hogan@imgtec.com>
Cc: Jason Wessel <jason.wessel@windriver.com>
Cc: linux-mips@linux-mips.org
Patchwork: https://patchwork.linux-mips.org/patch/15829/
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 arch/mips/kernel/kgdb.c |   48 +++++++++++++++++++++++++++++++++---------------
 1 file changed, 33 insertions(+), 15 deletions(-)

--- a/arch/mips/kernel/kgdb.c
+++ b/arch/mips/kernel/kgdb.c
@@ -244,9 +244,6 @@ static int compute_signal(int tt)
 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
 {
 	int reg;
-	struct thread_info *ti = task_thread_info(p);
-	unsigned long ksp = (unsigned long)ti + THREAD_SIZE - 32;
-	struct pt_regs *regs = (struct pt_regs *)ksp - 1;
 #if (KGDB_GDB_REG_SIZE == 32)
 	u32 *ptr = (u32 *)gdb_regs;
 #else
@@ -254,25 +251,46 @@ void sleeping_thread_to_gdb_regs(unsigne
 #endif
 
 	for (reg = 0; reg < 16; reg++)
-		*(ptr++) = regs->regs[reg];
+		*(ptr++) = 0;
 
 	/* S0 - S7 */
-	for (reg = 16; reg < 24; reg++)
-		*(ptr++) = regs->regs[reg];
+	*(ptr++) = p->thread.reg16;
+	*(ptr++) = p->thread.reg17;
+	*(ptr++) = p->thread.reg18;
+	*(ptr++) = p->thread.reg19;
+	*(ptr++) = p->thread.reg20;
+	*(ptr++) = p->thread.reg21;
+	*(ptr++) = p->thread.reg22;
+	*(ptr++) = p->thread.reg23;
 
 	for (reg = 24; reg < 28; reg++)
 		*(ptr++) = 0;
 
 	/* GP, SP, FP, RA */
-	for (reg = 28; reg < 32; reg++)
-		*(ptr++) = regs->regs[reg];
-
-	*(ptr++) = regs->cp0_status;
-	*(ptr++) = regs->lo;
-	*(ptr++) = regs->hi;
-	*(ptr++) = regs->cp0_badvaddr;
-	*(ptr++) = regs->cp0_cause;
-	*(ptr++) = regs->cp0_epc;
+	*(ptr++) = (long)p;
+	*(ptr++) = p->thread.reg29;
+	*(ptr++) = p->thread.reg30;
+	*(ptr++) = p->thread.reg31;
+
+	*(ptr++) = p->thread.cp0_status;
+
+	/* lo, hi */
+	*(ptr++) = 0;
+	*(ptr++) = 0;
+
+	/*
+	 * BadVAddr, Cause
+	 * Ideally these would come from the last exception frame up the stack
+	 * but that requires unwinding, otherwise we can't know much for sure.
+	 */
+	*(ptr++) = 0;
+	*(ptr++) = 0;
+
+	/*
+	 * PC
+	 * use return address (RA), i.e. the moment after return from resume()
+	 */
+	*(ptr++) = p->thread.reg31;
 }
 
 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 4.4 00/43] 4.4.66-stable review Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-01 23:30 +0200
  [PATCH 4.4 07/43] ext4: check if in-inode xattr is corrupted in ext4_expand_extra_isize_ea() Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-01 23:30 +0200
  [PATCH 4.4 23/43] l2tp: take reference on sessions being dumped Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-01 23:30 +0200
  [PATCH 4.4 31/43] ip6mr: fix notification device destruction Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-01 23:40 +0200
  [PATCH 4.4 19/43] net: phy: handle state correctly in phy_stop_machine Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-01 23:40 +0200
  [PATCH 4.4 37/43] MIPS: KGDB: Use kernel context for sleeping threads Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-01 23:40 +0200
  [PATCH 4.4 24/43] l2tp: fix PPP pseudo-wire auto-loading Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 02/43] [media] xc2028: unlock on error in xc2028_set_config() Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 04/43] clk: sunxi: Add apb0 gates for H3 Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 03/43] ARM: OMAP2+: timer: add probe for clocksources Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 13/43] regulator: core: Clear the supply pointer if enabling fails Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 36/43] ALSA: seq: Dont break snd_use_lock_sync() loop by timeout Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 27/43] tcp: clear saved_syn in tcp_disconnect() Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 05/43] crypto: testmgr - fix out of bound read in __test_aead() Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 21/43] net/packet: fix overflow in check for tp_frame_nr Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 16/43] sparc64: kern_addr_valid regression Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 15/43] xen/x86: dont lose event interrupts Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 08/43] md:raid1: fix a dead loop when read from a WriteMostly disk Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 11/43] net_sched: close another race condition in tcf_mirred_release() Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 38/43] MIPS: Avoid BUG warning in arch_check_elf Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 39/43] p9_client_readdir() fix Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 12/43] RDS: Fix the atomicity for congestion map update Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 18/43] net: neigh: guard against NULL solicit() method Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 35/43] ALSA: firewire-lib: fix inappropriate assignment between signed/unsigned type Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 43/43] ftrace/x86: Fix triple fault with graph tracing and suspend-to-ram Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 14/43] usb: gadget: f_midi: Fixed a bug when buflen was smaller than wMaxPacketSize Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 17/43] sparc64: Fix kernel panic due to erroneous #ifdef surrounding pmd_write() Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 09/43] MIPS: Fix crash registers on non-crashing CPUs Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 10/43] net: cavium: liquidio: Avoid dma_unmap_single on uninitialized ndata Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
    Re: [PATCH 4.4 10/43] net: cavium: liquidio: Avoid dma_unmap_single  on uninitialized ndata Ben Hutchings <ben.hutchings@codethink.co.uk> - 2017-05-10 17:40 +0200
  [PATCH 4.4 22/43] net/packet: fix overflow in check for tp_reserve Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  [PATCH 4.4 26/43] sctp: listen on the sock only when its state is listening or closed Greg Kroah-Hartman <gregkh@linuxfoundation.org> - 2017-05-02 00:10 +0200
  Re: [PATCH 4.4 00/43] 4.4.66-stable review Shuah Khan <shuahkh@osg.samsung.com> - 2017-05-02 16:00 +0200
  Re: [PATCH 4.4 00/43] 4.4.66-stable review Guenter Roeck <linux@roeck-us.net> - 2017-05-02 19:40 +0200

csiph-web