Skip to contentSkip to conversation

kdb: Adopt scheduler's task classification

PATCH Superseded Daniel Thompson 2 participants 3 messages Nov 1, 2021 – Nov 2, 2021 KGDB / KDB /debug_core

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 kdb: Adopt scheduler's task classification

Currently kdb contains some open-coded routines to generate a summary character for each task. This code currently issues warnings, is almost certainly broken and won't make sense to any kernel dev who has ever used /proc to examine task states.

Fix both the warning and the potential for confusion by adopting the scheduler's task classification. Whilst doing this we also simplify the filtering by using mask strings directly (which means we don't have to guess all the characters the scheduler might give us).

Unfortunately we can't quite match the scheduler classification completely. We add four extra states: - for idle loops and i, m and s sleeping system daemons (which means kthreads in one of the I, M and S states). These extra states are used to manage the filters for tools to make the output of ps and bta less noisy.

Note: The Fixes below is the last point the original dubious code was
moved; it was not introduced by that patch. However it gives us the last point to which this patch can be easily backported. Happily that should be enough to cover the introduction of CONFIG_WERROR!

Fixes 2f064a59a11f ("sched: Change task_struct::state")

---

Notes:
v3:
- Fix the uninitialized cpu variable (Doug and 0-day CI bot)
- Added a Fixes: (Doug)
- Changed "state I" -> "state -" and "state M" to "state [ism]"

v2:
- Fix the typos in the description (Doug)
- Stop trying to bend to world so I can keep 'I' exactly as it was before. Instead we now replace 'I' with '-' and fully adopt the scheduler description of tasks. kdb it an interactive tool, not ABI so this is OK. (Doug)
- Don't try to enumerate all possible letters in the comments and help. You can learn what to filter from the output of ps anyway, (Doug)
- Fix the sleeping system daemon stuff.

 kernel/debug/kdb/kdb_bt.c      |  14 ++--
 kernel/debug/kdb/kdb_main.c    |  35 +++++-----
 kernel/debug/kdb/kdb_private.h |   4 +-
 kernel/debug/kdb/kdb_support.c | 118 +++++++--------------------------
 4 files changed, 51 insertions(+), 120 deletions(-)
kernel/debug/kdb/kdb_bt.c +7 -7
index 1f9f0e47aeda..3368a2d15d73 100644
@@ -74,7 +74,7 @@ static void kdb_show_stack(struct task_struct *p, void *addr)
  */
 
 static int
-kdb_bt1(struct task_struct *p, unsigned long mask, bool btaprompt)
+kdb_bt1(struct task_struct *p, const char *mask, bool btaprompt)
 {
 	char ch;
 
@@ -120,7 +120,7 @@ kdb_bt_cpu(unsigned long cpu)
 		return;
 	}
 
-	kdb_bt1(kdb_tsk, ~0UL, false);
+	kdb_bt1(kdb_tsk, "A", false);
 }
 
 int
@@ -138,8 +138,8 @@ kdb_bt(int argc, const char **argv)
 	if (strcmp(argv[0], "bta") == 0) {
 		struct task_struct *g, *p;
 		unsigned long cpu;
-		unsigned long mask = kdb_task_state_string(argc ? argv[1] :
-							   NULL);
+		const char *mask = argc ? argv[1] : kdbgetenv("PS");
+
 		if (argc == 0)
 			kdb_ps_suppressed();
 		/* Run the active tasks first */
@@ -167,7 +167,7 @@ kdb_bt(int argc, const char **argv)
 			return diag;
 		p = find_task_by_pid_ns(pid, &init_pid_ns);
 		if (p)
-			return kdb_bt1(p, ~0UL, false);
+			return kdb_bt1(p, "A", false);
 		kdb_printf("No process with pid == %ld found\n", pid);
 		return 0;
 	} else if (strcmp(argv[0], "btt") == 0) {
@@ -176,7 +176,7 @@ kdb_bt(int argc, const char **argv)
 		diag = kdbgetularg((char *)argv[1], &addr);
 		if (diag)
 			return diag;
-		return kdb_bt1((struct task_struct *)addr, ~0UL, false);
+		return kdb_bt1((struct task_struct *)addr, "A", false);
 	} else if (strcmp(argv[0], "btc") == 0) {
 		unsigned long cpu = ~0;
 		if (argc > 1)
@@ -212,7 +212,7 @@ kdb_bt(int argc, const char **argv)
 			kdb_show_stack(kdb_current_task, (void *)addr);
 			return 0;
 		} else {
-			return kdb_bt1(kdb_current_task, ~0UL, false);
+			return kdb_bt1(kdb_current_task, "A", false);
 		}
 	}
 
kernel/debug/kdb/kdb_main.c +18 -17
index fa6deda894a1..60862af3d27a 100644
@@ -2203,8 +2203,8 @@ static void kdb_cpu_status(void)
 			state = 'D';	/* cpu is online but unresponsive */
 		} else {
 			state = ' ';	/* cpu is responding to kdb */
-			if (kdb_task_state_char(KDB_TSK(i)) == 'I')
-				state = 'I';	/* idle task */
+			if (kdb_task_state_char(KDB_TSK(i)) == '-')
+				state = '-';	/* idle task */
 		}
 		if (state != prev_state) {
 			if (prev_state != '?') {
@@ -2271,37 +2271,30 @@ static int kdb_cpu(int argc, const char **argv)
 void kdb_ps_suppressed(void)
 {
 	int idle = 0, daemon = 0;
-	unsigned long mask_I = kdb_task_state_string("I"),
-		      mask_M = kdb_task_state_string("M");
 	unsigned long cpu;
 	const struct task_struct *p, *g;
 	for_each_online_cpu(cpu) {
 		p = kdb_curr_task(cpu);
-		if (kdb_task_state(p, mask_I))
+		if (kdb_task_state(p, "-"))
 			++idle;
 	}
 	for_each_process_thread(g, p) {
-		if (kdb_task_state(p, mask_M))
+		if (kdb_task_state(p, "ims"))
 			++daemon;
 	}
 	if (idle || daemon) {
 		if (idle)
-			kdb_printf("%d idle process%s (state I)%s\n",
+			kdb_printf("%d idle process%s (state -)%s\n",
 				   idle, idle == 1 ? "" : "es",
 				   daemon ? " and " : "");
 		if (daemon)
-			kdb_printf("%d sleeping system daemon (state M) "
+			kdb_printf("%d sleeping system daemon (state [ism]) "
 				   "process%s", daemon,
 				   daemon == 1 ? "" : "es");
 		kdb_printf(" suppressed,\nuse 'ps A' to see all.\n");
 	}
 }
 
-/*
- * kdb_ps - This function implements the 'ps' command which shows a
- *	list of the active processes.
- *		ps [DRSTCZEUIMA]   All processes, optionally filtered by state
- */
 void kdb_ps1(const struct task_struct *p)
 {
 	int cpu;
@@ -2330,17 +2323,25 @@ void kdb_ps1(const struct task_struct *p)
 	}
 }
 
+/*
+ * kdb_ps - This function implements the 'ps' command which shows a
+ *	    list of the active processes.
+ *
+ * ps [<state_chars>]   Show processes, optionally selecting only those whose
+ *                      state character is found in <state_chars>.
+ */
 static int kdb_ps(int argc, const char **argv)
 {
 	struct task_struct *g, *p;
-	unsigned long mask, cpu;
+	const char *mask;
+	unsigned long cpu;
 
 	if (argc == 0)
 		kdb_ps_suppressed();
 	kdb_printf("%-*s      Pid   Parent [*] cpu State %-*s Command\n",
 		(int)(2*sizeof(void *))+2, "Task Addr",
 		(int)(2*sizeof(void *))+2, "Thread");
-	mask = kdb_task_state_string(argc ? argv[1] : NULL);
+	mask = argc ? argv[1] : kdbgetenv("PS");
 	/* Run the active tasks first */
 	for_each_online_cpu(cpu) {
 		if (KDB_FLAG(CMD_INTERRUPT))
@@ -2742,8 +2743,8 @@ static kdbtab_t maintab[] = {
 	},
 	{	.name = "bta",
 		.func = kdb_bt,
-		.usage = "[D|R|S|T|C|Z|E|U|I|M|A]",
-		.help = "Backtrace all processes matching state flag",
+		.usage = "[<state_chars>|A]",
+		.help = "Backtrace all processes matching whose state matches",
 		.flags = KDB_ENABLE_INSPECT,
 	},
 	{	.name = "btc",
kernel/debug/kdb/kdb_private.h +1 -3
index 629590084a0d..0d2f9feea0a4 100644
@@ -190,10 +190,8 @@ extern char kdb_grep_string[];
 extern int kdb_grep_leading;
 extern int kdb_grep_trailing;
 extern char *kdb_cmds[];
-extern unsigned long kdb_task_state_string(const char *);
 extern char kdb_task_state_char (const struct task_struct *);
-extern unsigned long kdb_task_state(const struct task_struct *p,
-				    unsigned long mask);
+extern bool kdb_task_state(const struct task_struct *p, const char *mask);
 extern void kdb_ps_suppressed(void);
 extern void kdb_ps1(const struct task_struct *p);
 extern void kdb_send_sig(struct task_struct *p, int sig);
kernel/debug/kdb/kdb_support.c +25 -93

159 lines not on this page · raw message

base-commit: 6880fa6c56601bb8ed59df6c30fd390cc5f6dd8f --
2.31.1

reviewer
#1 lore raw

Hi,

Quoted 55 lines · Daniel Thompson

Currently kdb contains some open-coded routines to generate a summary character for each task. This code currently issues warnings, is almost certainly broken and won't make sense to any kernel dev who has ever used /proc to examine task states.

Fix both the warning and the potential for confusion by adopting the scheduler's task classification. Whilst doing this we also simplify the filtering by using mask strings directly (which means we don't have to guess all the characters the scheduler might give us).

Unfortunately we can't quite match the scheduler classification completely. We add four extra states: - for idle loops and i, m and s sleeping system daemons (which means kthreads in one of the I, M and S states). These extra states are used to manage the filters for tools to make the output of ps and bta less noisy.

Note: The Fixes below is the last point the original dubious code was
moved; it was not introduced by that patch. However it gives us the last point to which this patch can be easily backported. Happily that should be enough to cover the introduction of CONFIG_WERROR!

Fixes 2f064a59a11f ("sched: Change task_struct::state")

---

Notes:
v3:
- Fix the uninitialized cpu variable (Doug and 0-day CI bot)
- Added a Fixes: (Doug)
- Changed "state I" -> "state -" and "state M" to "state [ism]"

v2:
- Fix the typos in the description (Doug)
- Stop trying to bend to world so I can keep 'I' exactly as it was before. Instead we now replace 'I' with '-' and fully adopt the scheduler description of tasks. kdb it an interactive tool, not ABI so this is OK. (Doug)
- Don't try to enumerate all possible letters in the comments and help. You can learn what to filter from the output of ps anyway, (Doug)
- Fix the sleeping system daemon stuff.

 kernel/debug/kdb/kdb_bt.c      |  14 ++--
 kernel/debug/kdb/kdb_main.c    |  35 +++++-----
 kernel/debug/kdb/kdb_private.h |   4 +-
 kernel/debug/kdb/kdb_support.c | 118 +++++++--------------------------
 4 files changed, 51 insertions(+), 120 deletions(-)
kernel/debug/kdb/kdb_bt.c +0 -0
index 1f9f0e47aeda..3368a2d15d73 100644

kernel/debug/kdb/kdb_bt.c static void kdb_show_stack(struct task_struct *p, void *addr) · line 77

Quoted 5 lines
kernel/debug/kdb/kdb_bt.c +1 -1
@@ -74,7 +74,7 @@ static void kdb_show_stack(struct task_struct *p, void *addr)
  */
 
 static int
-kdb_bt1(struct task_struct *p, unsigned long mask, bool btaprompt)
+kdb_bt1(struct task_struct *p, const char *mask, bool btaprompt)

(Copied from v1 review):

In the comment above this function there is still a reference to "DRSTCZEUIMA". Update that?

Quoted 18 lines
kernel/debug/kdb/kdb_bt.c +2 -4
@@ -2271,37 +2271,30 @@ static int kdb_cpu(int argc, const char **argv)
 void kdb_ps_suppressed(void)
 {
        int idle = 0, daemon = 0;
-       unsigned long mask_I = kdb_task_state_string("I"),
-                     mask_M = kdb_task_state_string("M");
        unsigned long cpu;
        const struct task_struct *p, *g;
        for_each_online_cpu(cpu) {
                p = kdb_curr_task(cpu);
-               if (kdb_task_state(p, mask_I))
+               if (kdb_task_state(p, "-"))
                        ++idle;
        }
        for_each_process_thread(g, p) {
-               if (kdb_task_state(p, mask_M))
+               if (kdb_task_state(p, "ims"))
                        ++daemon;

kernel/debug/kdb/kdb_bt.c static int kdb_cpu(int argc, const char **argv) · line 2291

Quoted 10 lines
kernel/debug/kdb/kdb_bt.c +2 -2
        }
        if (idle || daemon) {
                if (idle)
-                       kdb_printf("%d idle process%s (state I)%s\n",
+                       kdb_printf("%d idle process%s (state -)%s\n",
                                   idle, idle == 1 ? "" : "es",
                                   daemon ? " and " : "");
                if (daemon)
-                       kdb_printf("%d sleeping system daemon (state M) "
+                       kdb_printf("%d sleeping system daemon (state [ism]) "

super nitty: elsewhere you use "ims", not "ism". Can you be consistent and change the above to "ims"?

@@ -2742,8 +2743,8 @@ static kdbtab_t maintab[] = {

kernel/debug/kdb/kdb_bt.c static int kdb_cpu(int argc, const char **argv) · line 2296

Quoted 7 lines
kernel/debug/kdb/kdb_bt.c +2 -2
        },
        {       .name = "bta",
                .func = kdb_bt,
-               .usage = "[D|R|S|T|C|Z|E|U|I|M|A]",
-               .help = "Backtrace all processes matching state flag",
+               .usage = "[<state_chars>|A]",
+               .help = "Backtrace all processes matching whose state matches",

"matching whose state matches" sounds odd. Clean it up and use the saved chars to document "A":

.help = "Backtrace all processes whose state matches (A=all)",

-Doug

#2 lore raw
Quoted 11 lines · Doug Anderson

Hi,

Quoted 7 lines · Daniel Thompson
kernel/debug/kdb/kdb_bt.c +0 -0
index 1f9f0e47aeda..3368a2d15d73 100644
@@ -74,7 +74,7 @@ static void kdb_show_stack(struct task_struct *p, void *addr)
  */
 
Quoted 8 lines

kernel/debug/kdb/kdb_bt.c static void kdb_show_stack(struct task_struct *p, void *addr) · line 77

Quoted 3 lines
kernel/debug/kdb/kdb_bt.c +1 -1
 static int
-kdb_bt1(struct task_struct *p, unsigned long mask, bool btaprompt)
+kdb_bt1(struct task_struct *p, const char *mask, bool btaprompt)

(Copied from v1 review):

In the comment above this function there is still a reference to "DRSTCZEUIMA". Update that?

/me slaps head

Sorry. This patch really doesn't seem to have been my finest hour. Really thought I'd got it right at v3 (even threw in extra manual testing for good measure).

Will fix... immediately.

Quoted 23 lines
Quoted 23 lines
kernel/debug/kdb/kdb_bt.c +3 -5
@@ -2271,37 +2271,30 @@ static int kdb_cpu(int argc, const char **argv)
 void kdb_ps_suppressed(void)
 {
        int idle = 0, daemon = 0;
-       unsigned long mask_I = kdb_task_state_string("I"),
-                     mask_M = kdb_task_state_string("M");
        unsigned long cpu;
        const struct task_struct *p, *g;
        for_each_online_cpu(cpu) {
                p = kdb_curr_task(cpu);
-               if (kdb_task_state(p, mask_I))
+               if (kdb_task_state(p, "-"))
                        ++idle;
        }
        for_each_process_thread(g, p) {
-               if (kdb_task_state(p, mask_M))
+               if (kdb_task_state(p, "ims"))
                        ++daemon;
        }
        if (idle || daemon) {
                if (idle)
-                       kdb_printf("%d idle process%s (state I)%s\n",
+                       kdb_printf("%d idle process%s (state -)%s\n",
Quoted 8 lines

kernel/debug/kdb/kdb_bt.c static int kdb_cpu(int argc, const char **argv) · line 2291

Quoted 5 lines
kernel/debug/kdb/kdb_bt.c +1 -1
                                   idle, idle == 1 ? "" : "es",
                                   daemon ? " and " : "");
                if (daemon)
-                       kdb_printf("%d sleeping system daemon (state M) "
+                       kdb_printf("%d sleeping system daemon (state [ism]) "

super nitty: elsewhere you use "ims", not "ism". Can you be consistent and change the above to "ims"?

Ack.

Quoted 9 lines

@@ -2742,8 +2743,8 @@ static kdbtab_t maintab[] = {

kernel/debug/kdb/kdb_bt.c static int kdb_cpu(int argc, const char **argv) · line 2296

Quoted 7 lines
kernel/debug/kdb/kdb_bt.c +2 -2
        },
        {       .name = "bta",
                .func = kdb_bt,
-               .usage = "[D|R|S|T|C|Z|E|U|I|M|A]",
-               .help = "Backtrace all processes matching state flag",
+               .usage = "[<state_chars>|A]",
+               .help = "Backtrace all processes matching whose state matches",
Quoted 4 lines

"matching whose state matches" sounds odd. Clean it up and use the saved chars to document "A":

.help = "Backtrace all processes whose state matches (A=all)",

Turns out we don't have enough characters to document A on an 80 character terminal (some of the other help text is too long as well but I'll put that on the list for later).

Will settle for "Backtrace all processes whose state matches" and leave it at that.

Will also updates the .usage for ps which doesn't use the <state_chars>.

Daniel.