public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Oleg Nesterov <oleg@tv-sign.ru>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Jarek Poplawski <jarkao2@o2.pl>,
	Max Krasnyansky <maxk@qualcomm.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] workqueues: insert_work: use "list_head *" instead of "int tail"
Date: Thu, 12 Jun 2008 20:38:59 +0200	[thread overview]
Message-ID: <1213295939.31518.159.camel@twins> (raw)
In-Reply-To: <20080612174433.GA12204@tv-sign.ru>

On Thu, 2008-06-12 at 21:44 +0400, Oleg Nesterov wrote:

> > Hence that idea of flush context and completions.
> 
> Do you mean something like (just for example) below? If yes, then yes
> sure, flush_work() is limited. But I can't see how it is possible to
> "generalize" this idea.
> 
> (hmm... actually, if we add flush_work(), we can speedup schedule_on_each_cpu(),
>  instead of flush_workqueue(keventd_wq) we can do
> 
> 	for_each_online_cpu(cpu)
> 		flush_work(per_cpu_ptr(works, cpu));
> 
>  not sure this really makes sense though).

Speedups are always nice ;-), but the below also gets us there.

> Oleg.
> 
> --- kernel/workqueue.c~	2007-07-28 16:58:17.000000000 +0400
> +++ kernel/workqueue.c	2007-08-06 20:33:25.000000000 +0400
> @@ -590,25 +590,54 @@ EXPORT_SYMBOL(schedule_delayed_work_on);
>   *
>   * schedule_on_each_cpu() is very slow.
>   */
> +
> +struct xxx
> +{
> +	atomic_t count;
> +	struct completion done;
> +	work_func_t func;
> +};
> +
> +struct yyy
> +{
> +	struct work_struct work;
> +	struct xxx *xxx;
> +};
> +
> +static void yyy_func(struct work_struct *work)
> +{
> +	struct xxx *xxx = container_of(work, struct yyy, work)->xxx;
> +	xxx->func(work);
> +
> +	if (atomic_dec_and_test(&xxx->count))
> +		complete(&xxx->done);
> +}
> +
>  int schedule_on_each_cpu(work_func_t func)
>  {
>  	int cpu;
> -	struct work_struct *works;
> +	struct xxx xxx;
> +	struct yyy *works;
>  
> -	works = alloc_percpu(struct work_struct);
> +	init_completion(&xxx.done);
> +	xxx.func = func;
> +
> +	works = alloc_percpu(struct yyy);
>  	if (!works)
>  		return -ENOMEM;
>  
>  	get_online_cpus();
> +	atomic_set(&xxx.count, num_online_cpus());
>  	for_each_online_cpu(cpu) {
> -		struct work_struct *work = per_cpu_ptr(works, cpu);
> +		struct yyy *yyy = per_cpu_ptr(works, cpu);
>  
> -		INIT_WORK(work, func);
> -		set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
> -		__queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
> +		yyy->xxx = &xxx;
> +		INIT_WORK(&yyy->work, yyy_func);
> +		set_bit(WORK_STRUCT_PENDING, work_data_bits(&yyy->work));
> +		__queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), &yyy->work);
>  	}
> -	flush_workqueue(keventd_wq);
>  	put_online_cpus();
> +	wait_for_completion(&xxx.done);
>  	free_percpu(works);
>  	return 0;
>  }

Yes, along those lines.

you can call xxx a flush_context and create an interface like:

int queue_work_contex(struct workqueue_struct *wq, 
		      struct flush_context *fc, struct work_struct *work)
{
	work->context = fc;
	return queue_work(wq, work);
}

void flush_workqueue_context(struct workqueue_strucy *wq, t
			     struct flush_context *fc)
{
	if (atomic_read(&context->count))
		wait_for_completion(&fc->completion);
	/* except that the above is racy, wait_event() comes to mind */
}

of course run_workqueue() would then need to be augmented with something
like:

  context = work->context;
  ...
  f(work);
  ...
  if (context && atomic_dec_and_test(&context->count))
    complete(&context->done);

making all this PI savvy for -rt is going to be fun though.. I guess we
can just queue a normal barrier of the flusher's priority, and cancel it
once we complete.. hey - that doesn't sound hard at all :-)

also, I seem to have quitely ignored the fact that struct work doesn't
have the context pointer, and growing it unconditionally like this isn't
nice - hummm,. perhaps we have a bit left in data and can signify a
larger struct work_struct.. ?




  reply	other threads:[~2008-06-12 18:39 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-12 16:51 [PATCH] workqueues: insert_work: use "list_head *" instead of "int tail" Oleg Nesterov
2008-06-12 16:55 ` Oleg Nesterov
2008-06-12 17:01   ` Peter Zijlstra
2008-06-12 17:44     ` Oleg Nesterov
2008-06-12 18:38       ` Peter Zijlstra [this message]
2008-06-13 14:26         ` Oleg Nesterov
2008-06-13 14:43           ` Peter Zijlstra
2008-06-13 15:17             ` Oleg Nesterov
2008-06-13 15:32               ` Peter Zijlstra
2008-06-24  5:41                 ` Max Krasnyansky
2008-06-12 22:24   ` Jarek Poplawski
2008-06-13 10:13     ` Jarek Poplawski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1213295939.31518.159.camel@twins \
    --to=peterz@infradead.org \
    --cc=akpm@linux-foundation.org \
    --cc=jarkao2@o2.pl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maxk@qualcomm.com \
    --cc=oleg@tv-sign.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox