Re: [PATCH 6/6] sched: Change task_struct::state

From: Mathieu Desnoyers
Date: Wed Jun 02 2021 - 10:09:03 EST


----- On Jun 2, 2021, at 9:12 AM, Peter Zijlstra peterz@xxxxxxxxxxxxx wrote:

> Change the type and name of task_struct::state. Drop the volatile and
> shrink it to an 'unsigned int'. Rename it in order to find all uses
> such that we can use READ_ONCE/WRITE_ONCE as appropriate.
>
[...]
>
> --- a/block/blk-mq.c
> +++ b/block/blk-mq.c

[...]
> @@ -1559,7 +1560,8 @@ static int fill_psinfo(struct elf_prpsin
> psinfo->pr_pgrp = task_pgrp_vnr(p);
> psinfo->pr_sid = task_session_vnr(p);
>
> - i = p->state ? ffz(~p->state) + 1 : 0;
> + state = READ_ONCE(p->__state);
> + i = state ? ffz(~state) + 1 : 0;
> psinfo->pr_state = i;
> psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
> psinfo->pr_zomb = psinfo->pr_sname == 'Z';

[...]

> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -113,13 +113,13 @@ struct task_group;
> __TASK_TRACED | EXIT_DEAD | EXIT_ZOMBIE | \
> TASK_PARKED)
>
> -#define task_is_running(task) (READ_ONCE((task)->state) == TASK_RUNNING)
> +#define task_is_running(task) (READ_ONCE((task)->__state) == TASK_RUNNING)
>
> -#define task_is_traced(task) ((task->state & __TASK_TRACED) != 0)
> +#define task_is_traced(task) ((READ_ONCE(task->__state) & __TASK_TRACED) != 0)
>
> -#define task_is_stopped(task) ((task->state & __TASK_STOPPED) != 0)
> +#define task_is_stopped(task) ((READ_ONCE(task->__state) & __TASK_STOPPED) !=
> 0)
>
> -#define task_is_stopped_or_traced(task) ((task->state & (__TASK_STOPPED |
> __TASK_TRACED)) != 0)
> +#define task_is_stopped_or_traced(task) ((READ_ONCE(task->__state) &
> (__TASK_STOPPED | __TASK_TRACED)) != 0)
>
> #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
>
> @@ -134,14 +134,14 @@ struct task_group;
> do { \
> WARN_ON_ONCE(is_special_task_state(state_value));\
> current->task_state_change = _THIS_IP_; \
> - current->state = (state_value); \
> + WRITE_ONCE(current->__state, (state_value)); \
> } while (0)

Why not introduce set_task_state(p) and get_task_state(p) rather than sprinkle
READ_ONCE() and WRITE_ONCE() all over the kernel ?

Thanks,

Mathieu

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com