Skip to content

Commit 7e103ac

Browse files
committed
Merge branch 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull scheduler fixes from Thomas Gleixner: "The scheduler pull request comes with the following updates: - Prevent a divide by zero issue by validating the input value of sysctl_sched_time_avg - Make task state printing consistent all over the place and have explicit state characters for IDLE and PARKED so they wont be displayed as 'D' state which confuses tools" * 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: sched/sysctl: Check user input value of sysctl_sched_time_avg sched/debug: Add explicit TASK_PARKED printing sched/debug: Ignore TASK_IDLE for SysRq-W sched/debug: Add explicit TASK_IDLE printing sched/tracing: Use common task-state helpers sched/tracing: Fix trace_sched_switch task-state printing sched/debug: Remove unused variable sched/debug: Convert TASK_state to hex sched/debug: Implement consistent task-state printing
2 parents 1c6f705 + 5ccba44 commit 7e103ac

File tree

8 files changed

+102
-74
lines changed

8 files changed

+102
-74
lines changed

fs/proc/array.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,30 +119,25 @@ static inline void task_name(struct seq_file *m, struct task_struct *p)
119119
* simple bit tests.
120120
*/
121121
static const char * const task_state_array[] = {
122-
"R (running)", /* 0 */
123-
"S (sleeping)", /* 1 */
124-
"D (disk sleep)", /* 2 */
125-
"T (stopped)", /* 4 */
126-
"t (tracing stop)", /* 8 */
127-
"X (dead)", /* 16 */
128-
"Z (zombie)", /* 32 */
122+
123+
/* states in TASK_REPORT: */
124+
"R (running)", /* 0x00 */
125+
"S (sleeping)", /* 0x01 */
126+
"D (disk sleep)", /* 0x02 */
127+
"T (stopped)", /* 0x04 */
128+
"t (tracing stop)", /* 0x08 */
129+
"X (dead)", /* 0x10 */
130+
"Z (zombie)", /* 0x20 */
131+
"P (parked)", /* 0x40 */
132+
133+
/* states beyond TASK_REPORT: */
134+
"I (idle)", /* 0x80 */
129135
};
130136

131137
static inline const char *get_task_state(struct task_struct *tsk)
132138
{
133-
unsigned int state = (tsk->state | tsk->exit_state) & TASK_REPORT;
134-
135-
/*
136-
* Parked tasks do not run; they sit in __kthread_parkme().
137-
* Without this check, we would report them as running, which is
138-
* clearly wrong, so we report them as sleeping instead.
139-
*/
140-
if (tsk->state == TASK_PARKED)
141-
state = TASK_INTERRUPTIBLE;
142-
143-
BUILD_BUG_ON(1 + ilog2(TASK_REPORT) != ARRAY_SIZE(task_state_array)-1);
144-
145-
return task_state_array[fls(state)];
139+
BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != ARRAY_SIZE(task_state_array));
140+
return task_state_array[__get_task_state(tsk)];
146141
}
147142

148143
static inline int get_task_umask(struct task_struct *tsk)

include/linux/sched.h

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,23 @@ struct task_group;
6565
*/
6666

6767
/* Used in tsk->state: */
68-
#define TASK_RUNNING 0
69-
#define TASK_INTERRUPTIBLE 1
70-
#define TASK_UNINTERRUPTIBLE 2
71-
#define __TASK_STOPPED 4
72-
#define __TASK_TRACED 8
68+
#define TASK_RUNNING 0x0000
69+
#define TASK_INTERRUPTIBLE 0x0001
70+
#define TASK_UNINTERRUPTIBLE 0x0002
71+
#define __TASK_STOPPED 0x0004
72+
#define __TASK_TRACED 0x0008
7373
/* Used in tsk->exit_state: */
74-
#define EXIT_DEAD 16
75-
#define EXIT_ZOMBIE 32
74+
#define EXIT_DEAD 0x0010
75+
#define EXIT_ZOMBIE 0x0020
7676
#define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)
7777
/* Used in tsk->state again: */
78-
#define TASK_DEAD 64
79-
#define TASK_WAKEKILL 128
80-
#define TASK_WAKING 256
81-
#define TASK_PARKED 512
82-
#define TASK_NOLOAD 1024
83-
#define TASK_NEW 2048
84-
#define TASK_STATE_MAX 4096
85-
86-
#define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWPNn"
78+
#define TASK_PARKED 0x0040
79+
#define TASK_DEAD 0x0080
80+
#define TASK_WAKEKILL 0x0100
81+
#define TASK_WAKING 0x0200
82+
#define TASK_NOLOAD 0x0400
83+
#define TASK_NEW 0x0800
84+
#define TASK_STATE_MAX 0x1000
8785

8886
/* Convenience macros for the sake of set_current_state: */
8987
#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
@@ -99,7 +97,8 @@ struct task_group;
9997
/* get_task_state(): */
10098
#define TASK_REPORT (TASK_RUNNING | TASK_INTERRUPTIBLE | \
10199
TASK_UNINTERRUPTIBLE | __TASK_STOPPED | \
102-
__TASK_TRACED | EXIT_ZOMBIE | EXIT_DEAD)
100+
__TASK_TRACED | EXIT_DEAD | EXIT_ZOMBIE | \
101+
TASK_PARKED)
103102

104103
#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
105104

@@ -1243,17 +1242,34 @@ static inline pid_t task_pgrp_nr(struct task_struct *tsk)
12431242
return task_pgrp_nr_ns(tsk, &init_pid_ns);
12441243
}
12451244

1246-
static inline char task_state_to_char(struct task_struct *task)
1245+
#define TASK_REPORT_IDLE (TASK_REPORT + 1)
1246+
#define TASK_REPORT_MAX (TASK_REPORT_IDLE << 1)
1247+
1248+
static inline unsigned int __get_task_state(struct task_struct *tsk)
1249+
{
1250+
unsigned int tsk_state = READ_ONCE(tsk->state);
1251+
unsigned int state = (tsk_state | tsk->exit_state) & TASK_REPORT;
1252+
1253+
BUILD_BUG_ON_NOT_POWER_OF_2(TASK_REPORT_MAX);
1254+
1255+
if (tsk_state == TASK_IDLE)
1256+
state = TASK_REPORT_IDLE;
1257+
1258+
return fls(state);
1259+
}
1260+
1261+
static inline char __task_state_to_char(unsigned int state)
12471262
{
1248-
const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
1249-
unsigned long state = task->state;
1263+
static const char state_char[] = "RSDTtXZPI";
12501264

1251-
state = state ? __ffs(state) + 1 : 0;
1265+
BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != sizeof(state_char) - 1);
12521266

1253-
/* Make sure the string lines up properly with the number of task states: */
1254-
BUILD_BUG_ON(sizeof(TASK_STATE_TO_CHAR_STR)-1 != ilog2(TASK_STATE_MAX)+1);
1267+
return state_char[state];
1268+
}
12551269

1256-
return state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?';
1270+
static inline char task_state_to_char(struct task_struct *tsk)
1271+
{
1272+
return __task_state_to_char(__get_task_state(tsk));
12571273
}
12581274

12591275
/**

include/trace/events/sched.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ static inline long __trace_sched_switch_state(bool preempt, struct task_struct *
114114
* Preemption ignores task state, therefore preempted tasks are always
115115
* RUNNING (we will not have dequeued if state != RUNNING).
116116
*/
117-
return preempt ? TASK_RUNNING | TASK_STATE_MAX : p->state;
117+
if (preempt)
118+
return TASK_STATE_MAX;
119+
120+
return __get_task_state(p);
118121
}
119122
#endif /* CREATE_TRACE_POINTS */
120123

@@ -152,12 +155,14 @@ TRACE_EVENT(sched_switch,
152155

153156
TP_printk("prev_comm=%s prev_pid=%d prev_prio=%d prev_state=%s%s ==> next_comm=%s next_pid=%d next_prio=%d",
154157
__entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
155-
__entry->prev_state & (TASK_STATE_MAX-1) ?
156-
__print_flags(__entry->prev_state & (TASK_STATE_MAX-1), "|",
157-
{ 1, "S"} , { 2, "D" }, { 4, "T" }, { 8, "t" },
158-
{ 16, "Z" }, { 32, "X" }, { 64, "x" },
159-
{ 128, "K" }, { 256, "W" }, { 512, "P" },
160-
{ 1024, "N" }) : "R",
158+
159+
(__entry->prev_state & (TASK_REPORT_MAX - 1)) ?
160+
__print_flags(__entry->prev_state & (TASK_REPORT_MAX - 1), "|",
161+
{ 0x01, "S" }, { 0x02, "D" }, { 0x04, "T" },
162+
{ 0x08, "t" }, { 0x10, "X" }, { 0x20, "Z" },
163+
{ 0x40, "P" }, { 0x80, "I" }) :
164+
"R",
165+
161166
__entry->prev_state & TASK_STATE_MAX ? "+" : "",
162167
__entry->next_comm, __entry->next_pid, __entry->next_prio)
163168
);

kernel/sched/core.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5166,6 +5166,28 @@ void sched_show_task(struct task_struct *p)
51665166
put_task_stack(p);
51675167
}
51685168

5169+
static inline bool
5170+
state_filter_match(unsigned long state_filter, struct task_struct *p)
5171+
{
5172+
/* no filter, everything matches */
5173+
if (!state_filter)
5174+
return true;
5175+
5176+
/* filter, but doesn't match */
5177+
if (!(p->state & state_filter))
5178+
return false;
5179+
5180+
/*
5181+
* When looking for TASK_UNINTERRUPTIBLE skip TASK_IDLE (allows
5182+
* TASK_KILLABLE).
5183+
*/
5184+
if (state_filter == TASK_UNINTERRUPTIBLE && p->state == TASK_IDLE)
5185+
return false;
5186+
5187+
return true;
5188+
}
5189+
5190+
51695191
void show_state_filter(unsigned long state_filter)
51705192
{
51715193
struct task_struct *g, *p;
@@ -5188,7 +5210,7 @@ void show_state_filter(unsigned long state_filter)
51885210
*/
51895211
touch_nmi_watchdog();
51905212
touch_all_softlockup_watchdogs();
5191-
if (!state_filter || (p->state & state_filter))
5213+
if (state_filter_match(state_filter, p))
51925214
sched_show_task(p);
51935215
}
51945216

kernel/sched/debug.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,6 @@ static char *task_group_path(struct task_group *tg)
466466
}
467467
#endif
468468

469-
static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
470-
471469
static void
472470
print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
473471
{

kernel/sysctl.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ static struct ctl_table kern_table[] = {
367367
.data = &sysctl_sched_time_avg,
368368
.maxlen = sizeof(unsigned int),
369369
.mode = 0644,
370-
.proc_handler = proc_dointvec,
370+
.proc_handler = proc_dointvec_minmax,
371+
.extra1 = &one,
371372
},
372373
#ifdef CONFIG_SCHEDSTATS
373374
{

kernel/trace/trace_output.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -656,15 +656,6 @@ int trace_print_lat_context(struct trace_iterator *iter)
656656
return !trace_seq_has_overflowed(s);
657657
}
658658

659-
static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
660-
661-
static int task_state_char(unsigned long state)
662-
{
663-
int bit = state ? __ffs(state) + 1 : 0;
664-
665-
return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
666-
}
667-
668659
/**
669660
* ftrace_find_event - find a registered event
670661
* @type: the type of event to look for
@@ -930,8 +921,8 @@ static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
930921

931922
trace_assign_type(field, iter->ent);
932923

933-
T = task_state_char(field->next_state);
934-
S = task_state_char(field->prev_state);
924+
T = __task_state_to_char(field->next_state);
925+
S = __task_state_to_char(field->prev_state);
935926
trace_find_cmdline(field->next_pid, comm);
936927
trace_seq_printf(&iter->seq,
937928
" %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
@@ -966,8 +957,8 @@ static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
966957
trace_assign_type(field, iter->ent);
967958

968959
if (!S)
969-
S = task_state_char(field->prev_state);
970-
T = task_state_char(field->next_state);
960+
S = __task_state_to_char(field->prev_state);
961+
T = __task_state_to_char(field->next_state);
971962
trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
972963
field->prev_pid,
973964
field->prev_prio,
@@ -1002,8 +993,8 @@ static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
1002993
trace_assign_type(field, iter->ent);
1003994

1004995
if (!S)
1005-
S = task_state_char(field->prev_state);
1006-
T = task_state_char(field->next_state);
996+
S = __task_state_to_char(field->prev_state);
997+
T = __task_state_to_char(field->next_state);
1007998

1008999
SEQ_PUT_HEX_FIELD(s, field->prev_pid);
10091000
SEQ_PUT_HEX_FIELD(s, field->prev_prio);

kernel/trace/trace_sched_wakeup.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,10 @@ tracing_sched_switch_trace(struct trace_array *tr,
397397
entry = ring_buffer_event_data(event);
398398
entry->prev_pid = prev->pid;
399399
entry->prev_prio = prev->prio;
400-
entry->prev_state = prev->state;
400+
entry->prev_state = __get_task_state(prev);
401401
entry->next_pid = next->pid;
402402
entry->next_prio = next->prio;
403-
entry->next_state = next->state;
403+
entry->next_state = __get_task_state(next);
404404
entry->next_cpu = task_cpu(next);
405405

406406
if (!call_filter_check_discard(call, entry, buffer, event))
@@ -425,10 +425,10 @@ tracing_sched_wakeup_trace(struct trace_array *tr,
425425
entry = ring_buffer_event_data(event);
426426
entry->prev_pid = curr->pid;
427427
entry->prev_prio = curr->prio;
428-
entry->prev_state = curr->state;
428+
entry->prev_state = __get_task_state(curr);
429429
entry->next_pid = wakee->pid;
430430
entry->next_prio = wakee->prio;
431-
entry->next_state = wakee->state;
431+
entry->next_state = __get_task_state(wakee);
432432
entry->next_cpu = task_cpu(wakee);
433433

434434
if (!call_filter_check_discard(call, entry, buffer, event))

0 commit comments

Comments
 (0)