Skip to content

Commit e851dfa

Browse files
committed
Merge tag 'kgdb-5.16-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/danielt/linux
Pull kgdb update from Daniel Thompson: "A single patch this cycle. We replace some open-coded routines to classify task states with the scheduler's own function to do this. Alongside the obvious benefits of removing funky code and aligning more exactly with the scheduler's task classification, this also fixes a long standing compiler warning by removing the open-coded routines that generated the warning" * tag 'kgdb-5.16-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/danielt/linux: kdb: Adopt scheduler's task classification
2 parents a2b03e4 + b77dbc8 commit e851dfa

File tree

4 files changed

+53
-122
lines changed

4 files changed

+53
-122
lines changed

kernel/debug/kdb/kdb_bt.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void kdb_show_stack(struct task_struct *p, void *addr)
4646
* btp <pid> Kernel stack for <pid>
4747
* btt <address-expression> Kernel stack for task structure at
4848
* <address-expression>
49-
* bta [DRSTCZEUIMA] All useful processes, optionally
49+
* bta [state_chars>|A] All useful processes, optionally
5050
* filtered by state
5151
* btc [<cpu>] The current process on one cpu,
5252
* default is all cpus
@@ -74,7 +74,7 @@ static void kdb_show_stack(struct task_struct *p, void *addr)
7474
*/
7575

7676
static int
77-
kdb_bt1(struct task_struct *p, unsigned long mask, bool btaprompt)
77+
kdb_bt1(struct task_struct *p, const char *mask, bool btaprompt)
7878
{
7979
char ch;
8080

@@ -120,7 +120,7 @@ kdb_bt_cpu(unsigned long cpu)
120120
return;
121121
}
122122

123-
kdb_bt1(kdb_tsk, ~0UL, false);
123+
kdb_bt1(kdb_tsk, "A", false);
124124
}
125125

126126
int
@@ -138,8 +138,8 @@ kdb_bt(int argc, const char **argv)
138138
if (strcmp(argv[0], "bta") == 0) {
139139
struct task_struct *g, *p;
140140
unsigned long cpu;
141-
unsigned long mask = kdb_task_state_string(argc ? argv[1] :
142-
NULL);
141+
const char *mask = argc ? argv[1] : kdbgetenv("PS");
142+
143143
if (argc == 0)
144144
kdb_ps_suppressed();
145145
/* Run the active tasks first */
@@ -167,7 +167,7 @@ kdb_bt(int argc, const char **argv)
167167
return diag;
168168
p = find_task_by_pid_ns(pid, &init_pid_ns);
169169
if (p)
170-
return kdb_bt1(p, ~0UL, false);
170+
return kdb_bt1(p, "A", false);
171171
kdb_printf("No process with pid == %ld found\n", pid);
172172
return 0;
173173
} else if (strcmp(argv[0], "btt") == 0) {
@@ -176,7 +176,7 @@ kdb_bt(int argc, const char **argv)
176176
diag = kdbgetularg((char *)argv[1], &addr);
177177
if (diag)
178178
return diag;
179-
return kdb_bt1((struct task_struct *)addr, ~0UL, false);
179+
return kdb_bt1((struct task_struct *)addr, "A", false);
180180
} else if (strcmp(argv[0], "btc") == 0) {
181181
unsigned long cpu = ~0;
182182
if (argc > 1)
@@ -212,7 +212,7 @@ kdb_bt(int argc, const char **argv)
212212
kdb_show_stack(kdb_current_task, (void *)addr);
213213
return 0;
214214
} else {
215-
return kdb_bt1(kdb_current_task, ~0UL, false);
215+
return kdb_bt1(kdb_current_task, "A", false);
216216
}
217217
}
218218

kernel/debug/kdb/kdb_main.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,8 +2203,8 @@ static void kdb_cpu_status(void)
22032203
state = 'D'; /* cpu is online but unresponsive */
22042204
} else {
22052205
state = ' '; /* cpu is responding to kdb */
2206-
if (kdb_task_state_char(KDB_TSK(i)) == 'I')
2207-
state = 'I'; /* idle task */
2206+
if (kdb_task_state_char(KDB_TSK(i)) == '-')
2207+
state = '-'; /* idle task */
22082208
}
22092209
if (state != prev_state) {
22102210
if (prev_state != '?') {
@@ -2271,37 +2271,30 @@ static int kdb_cpu(int argc, const char **argv)
22712271
void kdb_ps_suppressed(void)
22722272
{
22732273
int idle = 0, daemon = 0;
2274-
unsigned long mask_I = kdb_task_state_string("I"),
2275-
mask_M = kdb_task_state_string("M");
22762274
unsigned long cpu;
22772275
const struct task_struct *p, *g;
22782276
for_each_online_cpu(cpu) {
22792277
p = kdb_curr_task(cpu);
2280-
if (kdb_task_state(p, mask_I))
2278+
if (kdb_task_state(p, "-"))
22812279
++idle;
22822280
}
22832281
for_each_process_thread(g, p) {
2284-
if (kdb_task_state(p, mask_M))
2282+
if (kdb_task_state(p, "ims"))
22852283
++daemon;
22862284
}
22872285
if (idle || daemon) {
22882286
if (idle)
2289-
kdb_printf("%d idle process%s (state I)%s\n",
2287+
kdb_printf("%d idle process%s (state -)%s\n",
22902288
idle, idle == 1 ? "" : "es",
22912289
daemon ? " and " : "");
22922290
if (daemon)
2293-
kdb_printf("%d sleeping system daemon (state M) "
2291+
kdb_printf("%d sleeping system daemon (state [ims]) "
22942292
"process%s", daemon,
22952293
daemon == 1 ? "" : "es");
22962294
kdb_printf(" suppressed,\nuse 'ps A' to see all.\n");
22972295
}
22982296
}
22992297

2300-
/*
2301-
* kdb_ps - This function implements the 'ps' command which shows a
2302-
* list of the active processes.
2303-
* ps [DRSTCZEUIMA] All processes, optionally filtered by state
2304-
*/
23052298
void kdb_ps1(const struct task_struct *p)
23062299
{
23072300
int cpu;
@@ -2330,17 +2323,25 @@ void kdb_ps1(const struct task_struct *p)
23302323
}
23312324
}
23322325

2326+
/*
2327+
* kdb_ps - This function implements the 'ps' command which shows a
2328+
* list of the active processes.
2329+
*
2330+
* ps [<state_chars>] Show processes, optionally selecting only those whose
2331+
* state character is found in <state_chars>.
2332+
*/
23332333
static int kdb_ps(int argc, const char **argv)
23342334
{
23352335
struct task_struct *g, *p;
2336-
unsigned long mask, cpu;
2336+
const char *mask;
2337+
unsigned long cpu;
23372338

23382339
if (argc == 0)
23392340
kdb_ps_suppressed();
23402341
kdb_printf("%-*s Pid Parent [*] cpu State %-*s Command\n",
23412342
(int)(2*sizeof(void *))+2, "Task Addr",
23422343
(int)(2*sizeof(void *))+2, "Thread");
2343-
mask = kdb_task_state_string(argc ? argv[1] : NULL);
2344+
mask = argc ? argv[1] : kdbgetenv("PS");
23442345
/* Run the active tasks first */
23452346
for_each_online_cpu(cpu) {
23462347
if (KDB_FLAG(CMD_INTERRUPT))
@@ -2742,8 +2743,8 @@ static kdbtab_t maintab[] = {
27422743
},
27432744
{ .name = "bta",
27442745
.func = kdb_bt,
2745-
.usage = "[D|R|S|T|C|Z|E|U|I|M|A]",
2746-
.help = "Backtrace all processes matching state flag",
2746+
.usage = "[<state_chars>|A]",
2747+
.help = "Backtrace all processes whose state matches",
27472748
.flags = KDB_ENABLE_INSPECT,
27482749
},
27492750
{ .name = "btc",
@@ -2797,7 +2798,7 @@ static kdbtab_t maintab[] = {
27972798
},
27982799
{ .name = "ps",
27992800
.func = kdb_ps,
2800-
.usage = "[<flags>|A]",
2801+
.usage = "[<state_chars>|A]",
28012802
.help = "Display active task list",
28022803
.flags = KDB_ENABLE_INSPECT,
28032804
},

kernel/debug/kdb/kdb_private.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,8 @@ extern char kdb_grep_string[];
190190
extern int kdb_grep_leading;
191191
extern int kdb_grep_trailing;
192192
extern char *kdb_cmds[];
193-
extern unsigned long kdb_task_state_string(const char *);
194193
extern char kdb_task_state_char (const struct task_struct *);
195-
extern unsigned long kdb_task_state(const struct task_struct *p,
196-
unsigned long mask);
194+
extern bool kdb_task_state(const struct task_struct *p, const char *mask);
197195
extern void kdb_ps_suppressed(void);
198196
extern void kdb_ps1(const struct task_struct *p);
199197
extern void kdb_send_sig(struct task_struct *p, int sig);

kernel/debug/kdb/kdb_support.c

Lines changed: 25 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/uaccess.h>
2525
#include <linux/kdb.h>
2626
#include <linux/slab.h>
27+
#include <linux/ctype.h>
2728
#include "kdb_private.h"
2829

2930
/*
@@ -473,82 +474,7 @@ int kdb_putword(unsigned long addr, unsigned long word, size_t size)
473474
return diag;
474475
}
475476

476-
/*
477-
* kdb_task_state_string - Convert a string containing any of the
478-
* letters DRSTCZEUIMA to a mask for the process state field and
479-
* return the value. If no argument is supplied, return the mask
480-
* that corresponds to environment variable PS, DRSTCZEU by
481-
* default.
482-
* Inputs:
483-
* s String to convert
484-
* Returns:
485-
* Mask for process state.
486-
* Notes:
487-
* The mask folds data from several sources into a single long value, so
488-
* be careful not to overlap the bits. TASK_* bits are in the LSB,
489-
* special cases like UNRUNNABLE are in the MSB. As of 2.6.10-rc1 there
490-
* is no overlap between TASK_* and EXIT_* but that may not always be
491-
* true, so EXIT_* bits are shifted left 16 bits before being stored in
492-
* the mask.
493-
*/
494-
495-
/* unrunnable is < 0 */
496-
#define UNRUNNABLE (1UL << (8*sizeof(unsigned long) - 1))
497-
#define RUNNING (1UL << (8*sizeof(unsigned long) - 2))
498-
#define IDLE (1UL << (8*sizeof(unsigned long) - 3))
499-
#define DAEMON (1UL << (8*sizeof(unsigned long) - 4))
500477

501-
unsigned long kdb_task_state_string(const char *s)
502-
{
503-
long res = 0;
504-
if (!s) {
505-
s = kdbgetenv("PS");
506-
if (!s)
507-
s = "DRSTCZEU"; /* default value for ps */
508-
}
509-
while (*s) {
510-
switch (*s) {
511-
case 'D':
512-
res |= TASK_UNINTERRUPTIBLE;
513-
break;
514-
case 'R':
515-
res |= RUNNING;
516-
break;
517-
case 'S':
518-
res |= TASK_INTERRUPTIBLE;
519-
break;
520-
case 'T':
521-
res |= TASK_STOPPED;
522-
break;
523-
case 'C':
524-
res |= TASK_TRACED;
525-
break;
526-
case 'Z':
527-
res |= EXIT_ZOMBIE << 16;
528-
break;
529-
case 'E':
530-
res |= EXIT_DEAD << 16;
531-
break;
532-
case 'U':
533-
res |= UNRUNNABLE;
534-
break;
535-
case 'I':
536-
res |= IDLE;
537-
break;
538-
case 'M':
539-
res |= DAEMON;
540-
break;
541-
case 'A':
542-
res = ~0UL;
543-
break;
544-
default:
545-
kdb_func_printf("unknown flag '%c' ignored\n", *s);
546-
break;
547-
}
548-
++s;
549-
}
550-
return res;
551-
}
552478

553479
/*
554480
* kdb_task_state_char - Return the character that represents the task state.
@@ -559,7 +485,6 @@ unsigned long kdb_task_state_string(const char *s)
559485
*/
560486
char kdb_task_state_char (const struct task_struct *p)
561487
{
562-
unsigned int p_state;
563488
unsigned long tmp;
564489
char state;
565490
int cpu;
@@ -568,25 +493,18 @@ char kdb_task_state_char (const struct task_struct *p)
568493
copy_from_kernel_nofault(&tmp, (char *)p, sizeof(unsigned long)))
569494
return 'E';
570495

571-
cpu = kdb_process_cpu(p);
572-
p_state = READ_ONCE(p->__state);
573-
state = (p_state == 0) ? 'R' :
574-
(p_state < 0) ? 'U' :
575-
(p_state & TASK_UNINTERRUPTIBLE) ? 'D' :
576-
(p_state & TASK_STOPPED) ? 'T' :
577-
(p_state & TASK_TRACED) ? 'C' :
578-
(p->exit_state & EXIT_ZOMBIE) ? 'Z' :
579-
(p->exit_state & EXIT_DEAD) ? 'E' :
580-
(p_state & TASK_INTERRUPTIBLE) ? 'S' : '?';
496+
state = task_state_to_char((struct task_struct *) p);
497+
581498
if (is_idle_task(p)) {
582499
/* Idle task. Is it really idle, apart from the kdb
583500
* interrupt? */
501+
cpu = kdb_process_cpu(p);
584502
if (!kdb_task_has_cpu(p) || kgdb_info[cpu].irq_depth == 1) {
585503
if (cpu != kdb_initial_cpu)
586-
state = 'I'; /* idle task */
504+
state = '-'; /* idle task */
587505
}
588-
} else if (!p->mm && state == 'S') {
589-
state = 'M'; /* sleeping system daemon */
506+
} else if (!p->mm && strchr("IMS", state)) {
507+
state = tolower(state); /* sleeping system daemon */
590508
}
591509
return state;
592510
}
@@ -596,14 +514,28 @@ char kdb_task_state_char (const struct task_struct *p)
596514
* given by the mask.
597515
* Inputs:
598516
* p struct task for the process
599-
* mask mask from kdb_task_state_string to select processes
517+
* mask set of characters used to select processes; both NULL
518+
* and the empty string mean adopt a default filter, which
519+
* is to suppress sleeping system daemons and the idle tasks
600520
* Returns:
601521
* True if the process matches at least one criteria defined by the mask.
602522
*/
603-
unsigned long kdb_task_state(const struct task_struct *p, unsigned long mask)
523+
bool kdb_task_state(const struct task_struct *p, const char *mask)
604524
{
605-
char state[] = { kdb_task_state_char(p), '\0' };
606-
return (mask & kdb_task_state_string(state)) != 0;
525+
char state = kdb_task_state_char(p);
526+
527+
/* If there is no mask, then we will filter code that runs when the
528+
* scheduler is idling and any system daemons that are currently
529+
* sleeping.
530+
*/
531+
if (!mask || mask[0] == '\0')
532+
return !strchr("-ims", state);
533+
534+
/* A is a special case that matches all states */
535+
if (strchr(mask, 'A'))
536+
return true;
537+
538+
return strchr(mask, state);
607539
}
608540

609541
/* Maintain a small stack of kdb_flags to allow recursion without disturbing

0 commit comments

Comments
 (0)