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

From: David Ahern
Date: Fri Jul 29 2016 - 10:08:18 EST


On 7/28/16 10:00 PM, Tomoki Sekiyama wrote:
sched_out_state() converts the prev_state u64 bitmask to a char in
a wrong way, which may cause invalid memory access.
TASK_STATE_TO_CHAR_STR should also be fixed to adapt current
kernel's sched.h.

Masami's point is that this should be 2 separate patches -- as suggested by the 'also' in the description.


Signed-off-by: Tomoki Sekiyama <tomoki.sekiyama.qu@xxxxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: David Ahern <dsahern@xxxxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Cc: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
Cc: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
---
tools/perf/builtin-sched.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 0dfe8df..5776263 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -70,7 +70,7 @@ struct sched_atom {
struct task_desc *wakee;
};

-#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
+#define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWPNn"

Patch 1 updates this macro to latest kernel version.


enum thread_state {
THREAD_SLEEPING = 0,
@@ -897,9 +897,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 < ARRAY_SIZE(str) - 1 ? str[bit] : '?';
}

static int


Patch 2 fixes access to str by treating prev_state as a bitmask and adding the Fixes:

Fixes: cdce9d738b91e ("perf sched: Add sched latency profiling")