Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1476140 > unrolled thread
| Started by | cheng chao <chengchao@kedacom.com> |
|---|---|
| First post | 2016-09-05 08:30 +0200 |
| Last post | 2016-09-09 12:10 +0200 |
| Articles | 8 — 4 participants |
Back to article view | Back to linux.kernel
[PATCH] sched/core: simpler function for sched_exec migration cheng chao <chengchao@kedacom.com> - 2016-09-05 08:30 +0200
Re: [PATCH] sched/core: simpler function for sched_exec migration Oleg Nesterov <oleg@redhat.com> - 2016-09-05 15:20 +0200
Re: [PATCH] sched/core: simpler function for sched_exec migration chengchao <chengchao@kedacom.com> - 2016-09-06 04:20 +0200
Re: [PATCH] sched/core: simpler function for sched_exec migration Oleg Nesterov <oleg@redhat.com> - 2016-09-06 17:30 +0200
Re: [PATCH] sched/core: simpler function for sched_exec migration chengchao <chengchao@kedacom.com> - 2016-09-07 05:30 +0200
Re: [PATCH] sched/core: simpler function for sched_exec migration Oleg Nesterov <oleg@redhat.com> - 2016-09-07 14:40 +0200
Re: [PATCH] sched/core: simpler function for sched_exec migration chengchao <chengchao@kedacom.com> - 2016-09-08 04:20 +0200
Re: [PATCH] sched/core: simpler function for sched_exec migration Peter Zijlstra <peterz@infradead.org> - 2016-09-09 12:10 +0200
| From | cheng chao <chengchao@kedacom.com> |
|---|---|
| Date | 2016-09-05 08:30 +0200 |
| Subject | [PATCH] sched/core: simpler function for sched_exec migration |
| Message-ID | <sdVzQ-8rK-23@gated-at.bofh.it> |
when sched_exec needs migration and CONFIG_PREEMPT_NONE=y,
migration_cpu_stop almost does nothing due to
the caller is !task_on_rq_queued().
currently CONFIG_PREEMPT and CONFIG_PREEMPT_VOLUNTARY work well because
the caller keeps task_on_rq_queued():
1. when CONFIG_PREEMPT=y
stop_one_cpu
->cpu_stop_queue_work
->spin_unlock_irqrestore (preempt_enable calls __preempt_schedule)
2. when CONFIG_PREEMPT_VOLUNTARY=y
stop_one_cpu
->wait_for_completion
->...
-->might_sleep() (calls _cond_resched()
stop_one_cpu_sync is introduced here to address this problem,more further
it makes more simpler for CONFIG_PREEMPT=y or CONFIG_PREEMPT_VOLUNTARY=y
when sched_exec needs migration.
Signed-off-by: cheng chao <chengchao@kedacom.com>
---
include/linux/stop_machine.h | 1 +
kernel/sched/core.c | 2 +-
kernel/stop_machine.c | 21 +++++++++++++++++++++
3 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/include/linux/stop_machine.h b/include/linux/stop_machine.h
index 3cc9632..e4e7d42 100644
--- a/include/linux/stop_machine.h
+++ b/include/linux/stop_machine.h
@@ -28,6 +28,7 @@ struct cpu_stop_work {
};
int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg);
+void stop_one_cpu_sync(unsigned int cpu, cpu_stop_fn_t fn, void *arg);
int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void *arg);
bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
struct cpu_stop_work *work_buf);
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 556cb07..2fd71e6 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2958,7 +2958,7 @@ void sched_exec(void)
struct migration_arg arg = { p, dest_cpu };
raw_spin_unlock_irqrestore(&p->pi_lock, flags);
- stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
+ stop_one_cpu_sync(task_cpu(p), migration_cpu_stop, &arg);
return;
}
unlock:
diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
index 4a1ca5f..24f8637 100644
--- a/kernel/stop_machine.c
+++ b/kernel/stop_machine.c
@@ -130,6 +130,27 @@ int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
return done.ret;
}
+/**
+ * the caller keeps task_on_rq_queued, so it's more suitable for
+ * sched_exec on the case when needs migration
+ */
+void stop_one_cpu_sync(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
+{
+ struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = NULL };
+
+ if (!cpu_stop_queue_work(cpu, &work))
+ return;
+
+#if defined(CONFIG_PREEMPT_NONE) || defined(CONFIG_PREEMPT_VOLUNTARY)
+ /*
+ * CONFIG_PREEMPT doesn't need call schedule here, because
+ * preempt_enable already does the similar thing when call
+ * cpu_stop_queue_work
+ */
+ schedule();
+#endif
+}
+
/* This controls the threads on each CPU. */
enum multi_stop_state {
/* Dummy starting state for thread. */
--
2.4.11
[toc] | [next] | [standalone]
| From | Oleg Nesterov <oleg@redhat.com> |
|---|---|
| Date | 2016-09-05 15:20 +0200 |
| Message-ID | <se1YC-4eB-17@gated-at.bofh.it> |
| In reply to | #1476140 |
On 09/05, cheng chao wrote:
>
> @@ -2958,7 +2958,7 @@ void sched_exec(void)
> struct migration_arg arg = { p, dest_cpu };
>
> raw_spin_unlock_irqrestore(&p->pi_lock, flags);
> - stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
> + stop_one_cpu_sync(task_cpu(p), migration_cpu_stop, &arg);
> return;
> }
> unlock:
> diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
> index 4a1ca5f..24f8637 100644
> --- a/kernel/stop_machine.c
> +++ b/kernel/stop_machine.c
> @@ -130,6 +130,27 @@ int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
> return done.ret;
> }
>
> +/**
> + * the caller keeps task_on_rq_queued, so it's more suitable for
> + * sched_exec on the case when needs migration
> + */
> +void stop_one_cpu_sync(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
> +{
> + struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = NULL };
> +
> + if (!cpu_stop_queue_work(cpu, &work))
> + return;
> +
> +#if defined(CONFIG_PREEMPT_NONE) || defined(CONFIG_PREEMPT_VOLUNTARY)
> + /*
> + * CONFIG_PREEMPT doesn't need call schedule here, because
> + * preempt_enable already does the similar thing when call
> + * cpu_stop_queue_work
> + */
> + schedule();
> +#endif
> +}
Honestly, I don't really understand the changelog, but this looks wrong.
stop_one_cpu_sync() assumes that cpu == smp_processor_id/task_cpu(current),
and thus the stopper thread should preempt us at least after schedule()
(if CONFIG_PREEMPT_NONE), so we do not need to synchronize.
But this is not necessarily true? This task can migrate to another CPU
before cpu_stop_queue_work() ?
Oleg.
[toc] | [prev] | [next] | [standalone]
| From | chengchao <chengchao@kedacom.com> |
|---|---|
| Date | 2016-09-06 04:20 +0200 |
| Message-ID | <see9r-422-5@gated-at.bofh.it> |
| In reply to | #1476410 |
Oleg, thank you.
the key point is for CONFIG_PREEMPT_NONE=y, if sched_exec needs migrate the current,
migration_cpu_stop doesn't migrate the task(current) at all, it means that the stopper thread does some
unuseful works in this scenario.
finally,the stopper thread calls cpu_stop_signal_done() to wake up this task, it calls select_task_rq() again,
maybe select another different cpu. totally calls select_task_rq() two times(first at sched_exec())
plus one time(wake_up_new_task() also calls select_task_rq()).
it is too much overhead for one task(fork()+exec()), isn't it?
1.
sched_exec()
->stop_one_cpu()
->wait_for_completion().
wait_for_completion() makes the current TASK_UNINTERRUPTIBLE and call schedule_timeout()
schedule_timeout(timeout) timeout is MAX_SCHEDULE_TIMEOUT.
->schedule()
deactivate_task(rq, current, DEQUEUE_SLEEP);
current->on_rq = 0;
2.
migration_cpu_stop() checks the task_on_rq_queued(p), but the task p->on_rq is 0.
#define TASK_ON_RQ_QUEUED 1
static inline int task_on_rq_queued(struct task_struct *p)
{
return p->on_rq == TASK_ON_RQ_QUEUED;
}
migration_cpu_stop()
...
if (task_rq(p) == rq && task_on_rq_queued(p))
rq = __migrate_task(rq, p, arg->dest_cpu);
...
thanks again, any suggestions and more reviews are welcome.
on 09/05/2016 09:11 PM, Oleg Nesterov wrote:
> On 09/05, cheng chao wrote:
>>
>> @@ -2958,7 +2958,7 @@ void sched_exec(void)
>> struct migration_arg arg = { p, dest_cpu };
>>
>> raw_spin_unlock_irqrestore(&p->pi_lock, flags);
>> - stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
>> + stop_one_cpu_sync(task_cpu(p), migration_cpu_stop, &arg);
>> return;
>> }
>> unlock:
>> diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c
>> index 4a1ca5f..24f8637 100644
>> --- a/kernel/stop_machine.c
>> +++ b/kernel/stop_machine.c
>> @@ -130,6 +130,27 @@ int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
>> return done.ret;
>> }
>>
>> +/**
>> + * the caller keeps task_on_rq_queued, so it's more suitable for
>> + * sched_exec on the case when needs migration
>> + */
>> +void stop_one_cpu_sync(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
>> +{
>> + struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = NULL };
>> +
>> + if (!cpu_stop_queue_work(cpu, &work))
>> + return;
>> +
>> +#if defined(CONFIG_PREEMPT_NONE) || defined(CONFIG_PREEMPT_VOLUNTARY)
>> + /*
>> + * CONFIG_PREEMPT doesn't need call schedule here, because
>> + * preempt_enable already does the similar thing when call
>> + * cpu_stop_queue_work
>> + */
>> + schedule();
>> +#endif
>> +}
>
> Honestly, I don't really understand the changelog, but this looks wrong.
>
> stop_one_cpu_sync() assumes that cpu == smp_processor_id/task_cpu(current),
> and thus the stopper thread should preempt us at least after schedule()
> (if CONFIG_PREEMPT_NONE), so we do not need to synchronize.
>
yes. the stop_one_cpu_sync is not a good name, stop_one_cpu_schedule is better?
there is nothing about synchronization.
> But this is not necessarily true? This task can migrate to another CPU
> before cpu_stop_queue_work() ?
>
before sched_exec() calls stop_one_cpu()/cpu_stop_queue_work(), this task(current) cannot migrate
to another cpu,because this task is running on the cpu.
> Oleg.
>
>
[toc] | [prev] | [next] | [standalone]
| From | Oleg Nesterov <oleg@redhat.com> |
|---|---|
| Date | 2016-09-06 17:30 +0200 |
| Message-ID | <seqtY-3JB-67@gated-at.bofh.it> |
| In reply to | #1477021 |
On 09/06, chengchao wrote:
>
> the key point is for CONFIG_PREEMPT_NONE=y,
> ...
> it is too much overhead for one task(fork()+exec()), isn't it?
Yes, yes, I see, this is suboptimal. Not sure we actually do care,
but yes, perhaps another helper which migrates the current task makes
sense, I dunno.
But,
> > stop_one_cpu_sync() assumes that cpu == smp_processor_id/task_cpu(current),
> > and thus the stopper thread should preempt us at least after schedule()
> > (if CONFIG_PREEMPT_NONE), so we do not need to synchronize.
> >
> yes. the stop_one_cpu_sync is not a good name, stop_one_cpu_schedule is better?
> there is nothing about synchronization.
We need to synchronize with the stopper to ensure it can't touch
cpu_stop_work on stack after stop_one_cpu_sync() returns, and
> > But this is not necessarily true? This task can migrate to another CPU
> > before cpu_stop_queue_work() ?
> >
> before sched_exec() calls stop_one_cpu()/cpu_stop_queue_work(), this
> task(current) cannot migrate to another cpu,because this task is running
> on the cpu.
Why? The running task can migrate to another CPU at any moment. Unless it
runs with preemption disabled or CONFIG_PREEMPT_NONE=y.
And this means that cpu_stop_queue_work() can queue the work on another
CPU != smp_processor_id(), and in this case the kernel can crash because
the pending cpu_stop_work can be overwritten right after return.
So you need something like
void stop_one_cpu_sync(cpu_stop_fn_t fn, void *arg)
{
struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = NULL };
preempt_disable();
cpu_stop_queue_work(raw_smp_processor_id(), &work);
preempt_enable_no_resched();
schedule();
}
or I am totally confused. Note that it doesn't (and shouldn't) have
the "int cpu" argument.
Oleg.
[toc] | [prev] | [next] | [standalone]
| From | chengchao <chengchao@kedacom.com> |
|---|---|
| Date | 2016-09-07 05:30 +0200 |
| Message-ID | <seBIJ-2GO-1@gated-at.bofh.it> |
| In reply to | #1477533 |
Oleg, thank you very much.
on 09/06/2016 11:22 PM, Oleg Nesterov wrote:
> On 09/06, chengchao wrote:
>>
>> the key point is for CONFIG_PREEMPT_NONE=y,
>> ...
>> it is too much overhead for one task(fork()+exec()), isn't it?
>
> Yes, yes, I see, this is suboptimal. Not sure we actually do care,
> but yes, perhaps another helper which migrates the current task makes
> sense, I dunno.
for CONFIG_PREEMPT_NONE=y, this patch wants the stopper thread can migrate the current
successfully instead of doing nothing.
>
> But,
>
>>> stop_one_cpu_sync() assumes that cpu == smp_processor_id/task_cpu(current),
>>> and thus the stopper thread should preempt us at least after schedule()
>>> (if CONFIG_PREEMPT_NONE), so we do not need to synchronize.
>>>
>> yes. the stop_one_cpu_sync is not a good name, stop_one_cpu_schedule is better?
>> there is nothing about synchronization.
>
> We need to synchronize with the stopper to ensure it can't touch
> cpu_stop_work on stack after stop_one_cpu_sync() returns, and
yes, you are right.
>
>>> But this is not necessarily true? This task can migrate to another CPU
>>> before cpu_stop_queue_work() ?
>>>
>> before sched_exec() calls stop_one_cpu()/cpu_stop_queue_work(), this
>> task(current) cannot migrate to another cpu,because this task is running
>> on the cpu.
>
> Why? The running task can migrate to another CPU at any moment. Unless it
> runs with preemption disabled or CONFIG_PREEMPT_NONE=y.
yes, this patch focused the CONFIG_PREEMPT_NONE=y at the beginning, so I didn't
pay more attention to the CONFIG_PREEMPT=y and CONFIG_PREEMPT_VOLUNTARY=y.
>
> And this means that cpu_stop_queue_work() can queue the work on another
> CPU != smp_processor_id(), and in this case the kernel can crash because
> the pending cpu_stop_work can be overwritten right after return.
>
> So you need something like
>
> void stop_one_cpu_sync(cpu_stop_fn_t fn, void *arg)
> {
> struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = NULL };
>
> preempt_disable();
> cpu_stop_queue_work(raw_smp_processor_id(), &work);
> preempt_enable_no_resched();
> schedule();
> }
>
> or I am totally confused. Note that it doesn't (and shouldn't) have
> the "int cpu" argument.
>
if preempt happens after preempt_enable_no_resched(), there is still risky that the
stop_one_cpu_sync() returns before the stopper thread can use cpu_stop_work safely.
as you said previously.
thus, I modify the patch:
int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
{
struct cpu_stop_done done;
struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done };
cpu_stop_init_done(&done, 1);
if (!cpu_stop_queue_work(cpu, &work))
return -ENOENT;
#if defined(CONFIG_PREEMPT_NONE)
/*
* let the stopper thread runs as soon as possible,
* and keep current TASK_RUNNING.
*/
scheudle();
#endif
wait_for_completion(&done.completion);
return done.ret;
}
remove the new function stop_one_cpu_sync(). When I posted this patch, I didn't want to
modify the stop_one_cpu(), because there are many functions to call the stop_one_cpu().
but now, I think it's good place to modify.
Any suggestions? thanks again.
> Oleg.
>
>
[toc] | [prev] | [next] | [standalone]
| From | Oleg Nesterov <oleg@redhat.com> |
|---|---|
| Date | 2016-09-07 14:40 +0200 |
| Message-ID | <seKj0-8cn-19@gated-at.bofh.it> |
| In reply to | #1477945 |
On 09/07, chengchao wrote:
>
> Oleg, thank you very much.
>
> on 09/06/2016 11:22 PM, Oleg Nesterov wrote:
> > On 09/06, chengchao wrote:
> >>
> >> the key point is for CONFIG_PREEMPT_NONE=y,
> >> ...
> >> it is too much overhead for one task(fork()+exec()), isn't it?
> >
> > Yes, yes, I see, this is suboptimal. Not sure we actually do care,
> > but yes, perhaps another helper which migrates the current task makes
> > sense, I dunno.
>
> for CONFIG_PREEMPT_NONE=y, this patch wants the stopper thread can migrate the current
> successfully instead of doing nothing.
I understand the intent. But I am not sure this optimization makes
sense.
> > So you need something like
> >
> > void stop_one_cpu_sync(cpu_stop_fn_t fn, void *arg)
> > {
> > struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = NULL };
> >
> > preempt_disable();
> > cpu_stop_queue_work(raw_smp_processor_id(), &work);
> > preempt_enable_no_resched();
> > schedule();
> > }
> >
>
> > or I am totally confused. Note that it doesn't (and shouldn't) have
> > the "int cpu" argument.
> >
>
>
> if preempt happens after preempt_enable_no_resched(),
This doesn't differ from explicit schedule() call. Either way the
stopper thread will preempt us on the same CPU.
> there is still risky that the
> stop_one_cpu_sync() returns before the stopper thread can use cpu_stop_work safely.
> as you said previously.
No.
However, there is another problem. It can race with another
stop_one_cpu(migration_cpu_stop) which comes between preempt_disable()
and cpu_stop_queue_work(). So the caller still can migrate to another
CPU right after after preempt_enable_no_resched() and run before the
stopper thread completes the cpu_stop_work queued by us.
> int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
> {
> struct cpu_stop_done done;
> struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done };
>
> cpu_stop_init_done(&done, 1);
> if (!cpu_stop_queue_work(cpu, &work))
> return -ENOENT;
>
> #if defined(CONFIG_PREEMPT_NONE)
> /*
> * let the stopper thread runs as soon as possible,
> * and keep current TASK_RUNNING.
> */
> scheudle();
> #endif
> wait_for_completion(&done.completion);
> return done.ret;
> }
Agreed this looks better, although I'd suggest _cond_resche().
Again, I am not sure this makes sense, I leave this to maintainers.
Oleg.
[toc] | [prev] | [next] | [standalone]
| From | chengchao <chengchao@kedacom.com> |
|---|---|
| Date | 2016-09-08 04:20 +0200 |
| Message-ID | <seX6x-8fF-5@gated-at.bofh.it> |
| In reply to | #1478253 |
Oled, thank you for moving this patch on.
on 09/07/2016 08:35 PM, Oleg Nesterov wrote:
> On 09/07, chengchao wrote:
>>
>> Oleg, thank you very much.
>>
>> on 09/06/2016 11:22 PM, Oleg Nesterov wrote:
>>> On 09/06, chengchao wrote:
>>>>
>>>> the key point is for CONFIG_PREEMPT_NONE=y,
>>>> ...
>>>> it is too much overhead for one task(fork()+exec()), isn't it?
>>>
>>> Yes, yes, I see, this is suboptimal. Not sure we actually do care,
>>> but yes, perhaps another helper which migrates the current task makes
>>> sense, I dunno.
>>
>> for CONFIG_PREEMPT_NONE=y, this patch wants the stopper thread can migrate the current
>> successfully instead of doing nothing.
>
> I understand the intent. But I am not sure this optimization makes
> sense.
>
For CONFIG_PREEMPT_NONE=y, when sched_exec() needs migration, sched_exec() calls
stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg).
If stopper thread can not migrate for us,why should we call stop_one_cpu() here?
It just makes the task TASK_UNINTERRUPTIBLE, wakes up the stopper thread, executes the
migration_cpu_stop, and the stopper thread wakes up the task.
But in fact, all above works are almost unuseful, the reason is that the migration_cpu_stop
doesn't migrate for us. why? the migration_cpu_stop() needs the task is TASK_ON_RQ_QUEUED
before it calls __migrate_task().
This patch can make the task TASK_RUNNING instead of TASK_UNINTERRUPTIBLE,
so the migration_cpu_stop() can migrate happily.
Does this optimization make sense now?
Any different opinions are always welcome.
>> int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
>> {
>> struct cpu_stop_done done;
>> struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done };
>>
>> cpu_stop_init_done(&done, 1);
>> if (!cpu_stop_queue_work(cpu, &work))
>> return -ENOENT;
>>
>> #if defined(CONFIG_PREEMPT_NONE)
>> /*
>> * let the stopper thread runs as soon as possible,
>> * and keep current TASK_RUNNING.
>> */
>> scheudle();
>> #endif
>> wait_for_completion(&done.completion);
>> return done.ret;
>> }
>
> Agreed this looks better, although I'd suggest _cond_resche().
>
> Again, I am not sure this makes sense, I leave this to maintainers.
>
You have done much works for this patch. Thanks again.
> Oleg.
>
>
[toc] | [prev] | [next] | [standalone]
| From | Peter Zijlstra <peterz@infradead.org> |
|---|---|
| Date | 2016-09-09 12:10 +0200 |
| Message-ID | <sfqUV-1ud-11@gated-at.bofh.it> |
| In reply to | #1478253 |
On Wed, Sep 07, 2016 at 02:35:11PM +0200, Oleg Nesterov wrote: > Again, I am not sure this makes sense, I leave this to maintainers. FWIW, I only received the emails from Oleg. So if one wants a maintainer to look at this one has to fix his mail setup such that the emails arrive at his Inbox.
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web