Re: [PATCH v2] perf sched: fix wrong conversion of task state

From: Masami Hiramatsu
Date: Wed Jul 27 2016 - 19:43:26 EST


On Thu, 28 Jul 2016 01:07:40 +0900
Tomoki Sekiyama <tomoki.sekiyama.qu@xxxxxxxxxxx> wrote:

> sched_out_state() converts the prev_state u64 bitmask to a char in
> a wrong way, which may cause wrong results of 'perf sched latency'.
> This patch fixes the conversion.
> Also, preempted tasks must be considered that they are in the
> THREAD_WAIT_CPU state.

Hmm, this includes several fixes and enhancements.
1. Use first bit of the state instead of state itself (critical bug to avoid crash?)
2. Check the range of the array and return '?' if out (minor bug, it can access data area)
3. Fix TASK_STATE_TO_CHAR_STR to swap X and Z.
4. Add new 'N+' to TASK_STATE_TO_CHAR_STR. (how about 'n'?)
5. Treat a preempted task as THREAD_WAIT_CPU.

so IMHO, it is better to split this patch into atleast 2, #1 and #2 (critical bugfix),
#3, #4, and #5 (minor update).

[..]
> @@ -897,9 +898,10 @@ static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
>
> static char sched_out_state(u64 prev_state)
> {
> - const char *str = TASK_STATE_TO_CHAR_STR;
> + const char str[] = TASK_STATE_TO_CHAR_STR;
> + unsigned int bit = prev_state ? __ffs(prev_state) + 1 : 0;
>
> - return str[prev_state];
> + return bit < sizeof(str) - 1 ? str[bit] : '?';

You'd better use ARRAY_SIZE(str) instead of sizeof() for array here.

Thank you,


--
Masami Hiramatsu <mhiramat@xxxxxxxxxx>