Skip to content

Commit 49dc6fb

Browse files
committed
Merge tag 'kgdb-5.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/danielt/linux
Pull kgdb updates from Daniel Thompson: "A fairly modest set of changes for this cycle. Of particular note are an earlycon fix from Doug Anderson and my own changes to get kgdb/kdb to honour the kprobe blocklist. The later creates a safety rail that strongly encourages developers not to place breakpoints in, for example, arch specific trap handling code. Also included are a couple of small fixes and tweaks: an API update, eliminate a coverity dead code warning, improved handling of search during multi-line printk and a couple of typo corrections" * tag 'kgdb-5.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/danielt/linux: kdb: Fix pager search for multi-line strings kernel: debug: Centralize dbg_[de]activate_sw_breakpoints kgdb: Add NOKPROBE labels on the trap handler functions kgdb: Honour the kprobe blocklist when setting breakpoints kernel/debug: Fix spelling mistake in debug_core.c kdb: Use newer api for tasklist scanning kgdb: Make "kgdbcon" work properly with "kgdb_earlycon" kdb: remove unnecessary null check of dbg_io_ops
2 parents 09a31a7 + d081a6e commit 49dc6fb

File tree

10 files changed

+101
-34
lines changed

10 files changed

+101
-34
lines changed

include/linux/kgdb.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/linkage.h>
1717
#include <linux/init.h>
1818
#include <linux/atomic.h>
19+
#include <linux/kprobes.h>
1920
#ifdef CONFIG_HAVE_ARCH_KGDB
2021
#include <asm/kgdb.h>
2122
#endif
@@ -335,6 +336,23 @@ extern int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
335336
atomic_t *snd_rdy);
336337
extern void gdbstub_exit(int status);
337338

339+
/*
340+
* kgdb and kprobes both use the same (kprobe) blocklist (which makes sense
341+
* given they are both typically hooked up to the same trap meaning on most
342+
* architectures one cannot be used to debug the other)
343+
*
344+
* However on architectures where kprobes is not (yet) implemented we permit
345+
* breakpoints everywhere rather than blocking everything by default.
346+
*/
347+
static inline bool kgdb_within_blocklist(unsigned long addr)
348+
{
349+
#ifdef CONFIG_KGDB_HONOUR_BLOCKLIST
350+
return within_kprobe_blacklist(addr);
351+
#else
352+
return false;
353+
#endif
354+
}
355+
338356
extern int kgdb_single_step;
339357
extern atomic_t kgdb_active;
340358
#define in_dbg_master() \

kernel/debug/debug_core.c

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static int exception_level;
8080
struct kgdb_io *dbg_io_ops;
8181
static DEFINE_SPINLOCK(kgdb_registration_lock);
8282

83-
/* Action for the reboot notifiter, a global allow kdb to change it */
83+
/* Action for the reboot notifier, a global allow kdb to change it */
8484
static int kgdbreboot;
8585
/* kgdb console driver is loaded */
8686
static int kgdb_con_registered;
@@ -94,14 +94,6 @@ int dbg_switch_cpu;
9494
/* Use kdb or gdbserver mode */
9595
int dbg_kdb_mode = 1;
9696

97-
static int __init opt_kgdb_con(char *str)
98-
{
99-
kgdb_use_con = 1;
100-
return 0;
101-
}
102-
103-
early_param("kgdbcon", opt_kgdb_con);
104-
10597
module_param(kgdb_use_con, int, 0644);
10698
module_param(kgdbreboot, int, 0644);
10799

@@ -163,7 +155,7 @@ early_param("nokgdbroundup", opt_nokgdbroundup);
163155

164156
/*
165157
* Weak aliases for breakpoint management,
166-
* can be overriden by architectures when needed:
158+
* can be overridden by architectures when needed:
167159
*/
168160
int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
169161
{
@@ -177,17 +169,23 @@ int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
177169
arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
178170
return err;
179171
}
172+
NOKPROBE_SYMBOL(kgdb_arch_set_breakpoint);
180173

181174
int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
182175
{
183176
return copy_to_kernel_nofault((char *)bpt->bpt_addr,
184177
(char *)bpt->saved_instr, BREAK_INSTR_SIZE);
185178
}
179+
NOKPROBE_SYMBOL(kgdb_arch_remove_breakpoint);
186180

187181
int __weak kgdb_validate_break_address(unsigned long addr)
188182
{
189183
struct kgdb_bkpt tmp;
190184
int err;
185+
186+
if (kgdb_within_blocklist(addr))
187+
return -EINVAL;
188+
191189
/* Validate setting the breakpoint and then removing it. If the
192190
* remove fails, the kernel needs to emit a bad message because we
193191
* are deep trouble not being able to put things back the way we
@@ -208,6 +206,7 @@ unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
208206
{
209207
return instruction_pointer(regs);
210208
}
209+
NOKPROBE_SYMBOL(kgdb_arch_pc);
211210

212211
int __weak kgdb_arch_init(void)
213212
{
@@ -218,6 +217,7 @@ int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
218217
{
219218
return 0;
220219
}
220+
NOKPROBE_SYMBOL(kgdb_skipexception);
221221

222222
#ifdef CONFIG_SMP
223223

@@ -239,6 +239,7 @@ void __weak kgdb_call_nmi_hook(void *ignored)
239239
*/
240240
kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
241241
}
242+
NOKPROBE_SYMBOL(kgdb_call_nmi_hook);
242243

243244
void __weak kgdb_roundup_cpus(void)
244245
{
@@ -272,6 +273,7 @@ void __weak kgdb_roundup_cpus(void)
272273
kgdb_info[cpu].rounding_up = false;
273274
}
274275
}
276+
NOKPROBE_SYMBOL(kgdb_roundup_cpus);
275277

276278
#endif
277279

@@ -298,6 +300,7 @@ static void kgdb_flush_swbreak_addr(unsigned long addr)
298300
/* Force flush instruction cache if it was outside the mm */
299301
flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
300302
}
303+
NOKPROBE_SYMBOL(kgdb_flush_swbreak_addr);
301304

302305
/*
303306
* SW breakpoint management:
@@ -325,6 +328,7 @@ int dbg_activate_sw_breakpoints(void)
325328
}
326329
return ret;
327330
}
331+
NOKPROBE_SYMBOL(dbg_activate_sw_breakpoints);
328332

329333
int dbg_set_sw_break(unsigned long addr)
330334
{
@@ -388,6 +392,7 @@ int dbg_deactivate_sw_breakpoints(void)
388392
}
389393
return ret;
390394
}
395+
NOKPROBE_SYMBOL(dbg_deactivate_sw_breakpoints);
391396

392397
int dbg_remove_sw_break(unsigned long addr)
393398
{
@@ -509,6 +514,7 @@ static int kgdb_io_ready(int print_wait)
509514
}
510515
return 1;
511516
}
517+
NOKPROBE_SYMBOL(kgdb_io_ready);
512518

513519
static int kgdb_reenter_check(struct kgdb_state *ks)
514520
{
@@ -556,13 +562,15 @@ static int kgdb_reenter_check(struct kgdb_state *ks)
556562

557563
return 1;
558564
}
565+
NOKPROBE_SYMBOL(kgdb_reenter_check);
559566

560567
static void dbg_touch_watchdogs(void)
561568
{
562569
touch_softlockup_watchdog_sync();
563570
clocksource_touch_watchdog();
564571
rcu_cpu_stall_reset();
565572
}
573+
NOKPROBE_SYMBOL(dbg_touch_watchdogs);
566574

567575
static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
568576
int exception_state)
@@ -752,6 +760,8 @@ static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
752760
}
753761
}
754762

763+
dbg_activate_sw_breakpoints();
764+
755765
/* Call the I/O driver's post_exception routine */
756766
if (dbg_io_ops->post_exception)
757767
dbg_io_ops->post_exception();
@@ -794,6 +804,7 @@ static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
794804

795805
return kgdb_info[cpu].ret_state;
796806
}
807+
NOKPROBE_SYMBOL(kgdb_cpu_enter);
797808

798809
/*
799810
* kgdb_handle_exception() - main entry point from a kernel exception
@@ -838,6 +849,7 @@ kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
838849
arch_kgdb_ops.enable_nmi(1);
839850
return ret;
840851
}
852+
NOKPROBE_SYMBOL(kgdb_handle_exception);
841853

842854
/*
843855
* GDB places a breakpoint at this function to know dynamically loaded objects.
@@ -872,6 +884,7 @@ int kgdb_nmicallback(int cpu, void *regs)
872884
#endif
873885
return 1;
874886
}
887+
NOKPROBE_SYMBOL(kgdb_nmicallback);
875888

876889
int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
877890
atomic_t *send_ready)
@@ -897,6 +910,7 @@ int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
897910
#endif
898911
return 1;
899912
}
913+
NOKPROBE_SYMBOL(kgdb_nmicallin);
900914

901915
static void kgdb_console_write(struct console *co, const char *s,
902916
unsigned count)
@@ -920,6 +934,20 @@ static struct console kgdbcons = {
920934
.index = -1,
921935
};
922936

937+
static int __init opt_kgdb_con(char *str)
938+
{
939+
kgdb_use_con = 1;
940+
941+
if (kgdb_io_module_registered && !kgdb_con_registered) {
942+
register_console(&kgdbcons);
943+
kgdb_con_registered = 1;
944+
}
945+
946+
return 0;
947+
}
948+
949+
early_param("kgdbcon", opt_kgdb_con);
950+
923951
#ifdef CONFIG_MAGIC_SYSRQ
924952
static void sysrq_handle_dbg(int key)
925953
{

kernel/debug/gdbstub.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ static void gdb_cmd_query(struct kgdb_state *ks)
725725
}
726726
}
727727

728-
do_each_thread(g, p) {
728+
for_each_process_thread(g, p) {
729729
if (i >= ks->thr_query && !finished) {
730730
int_to_threadref(thref, p->pid);
731731
ptr = pack_threadid(ptr, thref);
@@ -735,7 +735,7 @@ static void gdb_cmd_query(struct kgdb_state *ks)
735735
finished = 1;
736736
}
737737
i++;
738-
} while_each_thread(g, p);
738+
}
739739

740740
*(--ptr) = '\0';
741741
break;
@@ -1061,7 +1061,6 @@ int gdb_serial_stub(struct kgdb_state *ks)
10611061
error_packet(remcom_out_buffer, -EINVAL);
10621062
break;
10631063
}
1064-
dbg_activate_sw_breakpoints();
10651064
fallthrough; /* to default processing */
10661065
default:
10671066
default_handle:

kernel/debug/kdb/kdb_bp.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ static int kdb_bp(int argc, const char **argv)
306306
if (!template.bp_addr)
307307
return KDB_BADINT;
308308

309+
/*
310+
* This check is redundant (since the breakpoint machinery should
311+
* be doing the same check during kdb_bp_install) but gives the
312+
* user immediate feedback.
313+
*/
314+
diag = kgdb_validate_break_address(template.bp_addr);
315+
if (diag)
316+
return diag;
317+
309318
/*
310319
* Find an empty bp structure to allocate
311320
*/

kernel/debug/kdb/kdb_bt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ kdb_bt(int argc, const char **argv)
149149
return 0;
150150
}
151151
/* Now the inactive tasks */
152-
kdb_do_each_thread(g, p) {
152+
for_each_process_thread(g, p) {
153153
if (KDB_FLAG(CMD_INTERRUPT))
154154
return 0;
155155
if (task_curr(p))
156156
continue;
157157
if (kdb_bt1(p, mask, btaprompt))
158158
return 0;
159-
} kdb_while_each_thread(g, p);
159+
}
160160
} else if (strcmp(argv[0], "btp") == 0) {
161161
struct task_struct *p;
162162
unsigned long pid;

kernel/debug/kdb/kdb_debugger.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ int kdb_stub(struct kgdb_state *ks)
147147
return DBG_PASS_EVENT;
148148
}
149149
kdb_bp_install(ks->linux_regs);
150-
dbg_activate_sw_breakpoints();
151150
/* Set the exit state to a single step or a continue */
152151
if (KDB_STATE(DOING_SS))
153152
gdbstub_state(ks, "s");
@@ -167,7 +166,6 @@ int kdb_stub(struct kgdb_state *ks)
167166
* differently vs the gdbstub
168167
*/
169168
kgdb_single_step = 0;
170-
dbg_deactivate_sw_breakpoints();
171169
return DBG_SWITCH_CPU_EVENT;
172170
}
173171
return kgdb_info[ks->cpu].ret_state;

kernel/debug/kdb/kdb_io.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -545,18 +545,18 @@ static int kdb_search_string(char *searched, char *searchfor)
545545
static void kdb_msg_write(const char *msg, int msg_len)
546546
{
547547
struct console *c;
548+
const char *cp;
549+
int len;
548550

549551
if (msg_len == 0)
550552
return;
551553

552-
if (dbg_io_ops) {
553-
const char *cp = msg;
554-
int len = msg_len;
554+
cp = msg;
555+
len = msg_len;
555556

556-
while (len--) {
557-
dbg_io_ops->write_char(*cp);
558-
cp++;
559-
}
557+
while (len--) {
558+
dbg_io_ops->write_char(*cp);
559+
cp++;
560560
}
561561

562562
for_each_console(c) {
@@ -706,12 +706,16 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
706706
size_avail = sizeof(kdb_buffer) - len;
707707
goto kdb_print_out;
708708
}
709-
if (kdb_grepping_flag >= KDB_GREPPING_FLAG_SEARCH)
709+
if (kdb_grepping_flag >= KDB_GREPPING_FLAG_SEARCH) {
710710
/*
711711
* This was a interactive search (using '/' at more
712-
* prompt) and it has completed. Clear the flag.
712+
* prompt) and it has completed. Replace the \0 with
713+
* its original value to ensure multi-line strings
714+
* are handled properly, and return to normal mode.
713715
*/
716+
*cphold = replaced_byte;
714717
kdb_grepping_flag = 0;
718+
}
715719
/*
716720
* at this point the string is a full line and
717721
* should be printed, up to the null.

kernel/debug/kdb/kdb_main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,10 +2299,10 @@ void kdb_ps_suppressed(void)
22992299
if (kdb_task_state(p, mask_I))
23002300
++idle;
23012301
}
2302-
kdb_do_each_thread(g, p) {
2302+
for_each_process_thread(g, p) {
23032303
if (kdb_task_state(p, mask_M))
23042304
++daemon;
2305-
} kdb_while_each_thread(g, p);
2305+
}
23062306
if (idle || daemon) {
23072307
if (idle)
23082308
kdb_printf("%d idle process%s (state I)%s\n",
@@ -2370,12 +2370,12 @@ static int kdb_ps(int argc, const char **argv)
23702370
}
23712371
kdb_printf("\n");
23722372
/* Now the real tasks */
2373-
kdb_do_each_thread(g, p) {
2373+
for_each_process_thread(g, p) {
23742374
if (KDB_FLAG(CMD_INTERRUPT))
23752375
return 0;
23762376
if (kdb_task_state(p, mask))
23772377
kdb_ps1(p);
2378-
} kdb_while_each_thread(g, p);
2378+
}
23792379

23802380
return 0;
23812381
}

kernel/debug/kdb/kdb_private.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,6 @@ extern struct task_struct *kdb_curr_task(int);
230230

231231
#define kdb_task_has_cpu(p) (task_curr(p))
232232

233-
/* Simplify coexistence with NPTL */
234-
#define kdb_do_each_thread(g, p) do_each_thread(g, p)
235-
#define kdb_while_each_thread(g, p) while_each_thread(g, p)
236-
237233
#define GFP_KDB (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL)
238234

239235
extern void *debug_kmalloc(size_t size, gfp_t flags);

0 commit comments

Comments
 (0)