linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg@tv-sign.ru>
To: Srivatsa Vaddagiri <vatsa@in.ibm.com>
Cc: Andrew Morton <akpm@osdl.org>,
	David Howells <dhowells@redhat.com>,
	Christoph Hellwig <hch@infradead.org>,
	Ingo Molnar <mingo@elte.hu>, Linus Torvalds <torvalds@osdl.org>,
	linux-kernel@vger.kernel.org, Gautham shenoy <ego@in.ibm.com>,
	"Pallipadi, Venkatesh" <venkatesh.pallipadi@intel.com>
Subject: Re: [PATCH] flush_cpu_workqueue: don't flush an empty ->worklist
Date: Mon, 15 Jan 2007 02:54:10 +0300	[thread overview]
Message-ID: <20070114235410.GA6165@tv-sign.ru> (raw)
In-Reply-To: <20070109165655.GA215@tv-sign.ru>

How about the pseudo-code below?

workqueue_mutex is only used to protect "struct list_head workqueues",
all workqueue operations can run in parallel with cpuhotplug callback path.
take_over_work(), migrate_sequence, CPU_LOCK_ACQUIRE/RELEASE go away.

I'd like to make a couple of cleanups (and fix schedule_on_each_cpu) before
sending the patch, but if somebody doesn't like this intrusive change, he
can nack it right now.

Oleg.

struct cpu_workqueue_srtuct {
	...
	int should_stop;
	...
};

// also used by flush_work/flush_workqueue
static cpumask_t cpu_populated_map __read_mostly;

/*
 * NOTE: the caller must not touch *cwq if this func returns true
 */
static inline int cwq_should_stop(struct cpu_workqueue_struct *cwq)
{
	int should_stop = cwq->should_stop;

	if (unlikely(should_stop)) {
		spin_lock_irq(&cwq->lock);
		should_stop = cwq->should_stop && list_empty(&cwq->worklist);
		if (should_stop)
			cwq->thread = NULL;
		spin_unlock_irq(&cwq->lock);
	}

	return should_stop;
}

static int worker_thread(void *cwq)
{
	while (!cwq_should_stop(cwq)) {
		...
		run_workqueue();
		...
	}
}

static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
{
	struct task_struct *p;

	spin_lock_irq(&cwq->lock);
	cwq->should_stop = 0;
	p = cwq->thread;
	spin_unlock_irq(&cwq->lock);

	if (!p) {
		struct workqueue_struct *wq = cwq->wq;
		const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";

		p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
		/*
		 * Nobody can add the work_struct to this cwq,
		 *	if (caller is __create_workqueue)
		 *		nobody should see this wq
		 *	else // caller is CPU_UP_PREPARE
		 *		cpu is not on cpu_online_map
		 * so we can abort safely.
		 */
		if (IS_ERR(p))
			return PTR_ERR(p);

		if (!is_single_threaded(wq))
			kthread_bind(p, cpu);
		/*
		 * Cancels affinity if the caller is CPU_UP_PREPARE.
		 * Needs a cleanup, but OK.
		 */
		wake_up_process(p);
		cwq->thread = p;
	}

	return 0;
}

struct workqueue_struct *__create_workqueue(const char *name,
					    int singlethread, int freezeable)
{
	struct workqueue_struct *wq;
	struct cpu_workqueue_struct *cwq;
	int err = 0, cpu;

	wq = kzalloc(sizeof(*wq), GFP_KERNEL);
	if (!wq)
		return NULL;

	wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
	if (!wq->cpu_wq) {
		kfree(wq);
		return NULL;
	}

	wq->name = name;
	wq->freezeable = freezeable;

	if (singlethread) {
		INIT_LIST_HEAD(&wq->list);
		cwq = init_cpu_workqueue(wq, singlethread_cpu);
		err = create_workqueue_thread(cwq, singlethread_cpu);
	} else {
		mutex_lock(&workqueue_mutex);
		list_add(&wq->list, &workqueues);

		for_each_possible_cpu(cpu) {
			cwq = init_cpu_workqueue(wq, cpu);
			if (err || !cpu_isset(cpu, cpu_populated_map))
				continue;
			err = create_workqueue_thread(cwq, cpu);
		}
		mutex_unlock(&workqueue_mutex);
	}

	if (err) {
		destroy_workqueue(wq);
		wq = NULL;
	}
	return wq;
}

static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
{
	struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
	struct wq_barrier barr;
	int alive = 0;

	spin_lock_irq(&cwq->lock);
	if (cwq->thread != NULL) {
		insert_wq_barrier(cwq, &barr, 1);
		cwq->should_stop = 1;
		alive = 1;
	}
	spin_unlock_irq(&cwq->lock);

	if (alive) {
		wait_for_completion(&barr.done);

		while (unlikely(cwq->thread != NULL))
			cpu_relax();
		/*
		 * Wait until cwq->thread unlocks cwq->lock,
		 * it won't touch *cwq after that.
		 */
		smp_rmb();
		spin_unlock_wait(&cwq->lock);
	}
}

void destroy_workqueue(struct workqueue_struct *wq)
{
	if (is_single_threaded(wq))
		cleanup_workqueue_thread(wq, singlethread_cpu);
	else {
		int cpu;

		mutex_lock(&workqueue_mutex);
		list_del(&wq->list);
		mutex_unlock(&workqueue_mutex);

		for_each_cpu_mask(cpu, cpu_populated_map)
			cleanup_workqueue_thread(wq, cpu);
	}

	free_percpu(wq->cpu_wq);
	kfree(wq);
}

static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
						unsigned long action,
						void *hcpu)
{
	struct workqueue_struct *wq;
	struct cpu_workqueue_struct *cwq;
	unsigned int cpu = (unsigned long)hcpu;
	int ret = NOTIFY_OK;

	mutex_lock(&workqueue_mutex);
	if (action == CPU_UP_PREPARE)
		cpu_set(cpu, cpu_populated_map);

	list_for_each_entry(wq, &workqueues, list) {
		cwq = per_cpu_ptr(wq->cpu_wq, cpu);

		switch (action) {
		case CPU_UP_PREPARE:
			if (create_workqueue_thread(cwq, cpu))
				ret = NOTIFY_BAD;
			break;

		case CPU_ONLINE:
			set_cpus_allowed(cwq->thread, cpumask_of_cpu(cpu));
			break;

		case CPU_UP_CANCELED:
		case CPU_DEAD:
			cwq->should_stop = 1;
			wake_up(&cwq->more_work);
			break;
		}

		if (ret != NOTIFY_OK) {
			printk(KERN_ERR "workqueue for %i failed\n", cpu);
			break;
		}
	}
	mutex_unlock(&workqueue_mutex);

	return ret;
}

void init_workqueues(void)
{
	...
	cpu_populated_map = cpu_online_map;
	...
}


  reply	other threads:[~2007-01-14 23:55 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-17 22:34 [PATCH, RFC] reimplement flush_workqueue() Oleg Nesterov
2006-12-18  3:09 ` Linus Torvalds
2006-12-19  0:27 ` Andrew Morton
2006-12-19  0:43   ` Oleg Nesterov
2006-12-19  1:00     ` Andrew Morton
2007-01-04 11:32     ` Srivatsa Vaddagiri
2007-01-04 14:29       ` Oleg Nesterov
2007-01-04 15:56         ` Srivatsa Vaddagiri
2007-01-04 16:31           ` Oleg Nesterov
2007-01-04 16:57             ` Srivatsa Vaddagiri
2007-01-04 17:18         ` Andrew Morton
2007-01-04 18:09           ` Oleg Nesterov
2007-01-04 18:31             ` Andrew Morton
2007-01-05  9:03               ` Srivatsa Vaddagiri
2007-01-05 14:07                 ` Oleg Nesterov
2007-01-06 15:24                   ` Srivatsa Vaddagiri
2007-01-05  8:56           ` Srivatsa Vaddagiri
2007-01-05 12:42             ` Oleg Nesterov
2007-01-06 15:11               ` Srivatsa Vaddagiri
2007-01-06 15:10           ` [PATCH] fix-flush_workqueue-vs-cpu_dead-race-update Oleg Nesterov
2007-01-06 15:45             ` Srivatsa Vaddagiri
2007-01-06 16:30               ` Oleg Nesterov
2007-01-06 16:38                 ` Srivatsa Vaddagiri
2007-01-06 17:34                   ` Oleg Nesterov
2007-01-07 10:43                     ` Srivatsa Vaddagiri
2007-01-07 12:56                       ` Oleg Nesterov
2007-01-07 14:22                         ` Oleg Nesterov
2007-01-07 14:42                           ` Oleg Nesterov
2007-01-07 16:43                           ` Srivatsa Vaddagiri
2007-01-07 17:01                             ` Srivatsa Vaddagiri
2007-01-07 17:33                               ` Oleg Nesterov
2007-01-07 17:18                             ` Oleg Nesterov
2007-01-07 16:21                         ` Srivatsa Vaddagiri
2007-01-07 17:09                           ` Oleg Nesterov
2007-01-06 19:11                   ` Andrew Morton
2007-01-06 19:13                     ` Ingo Molnar
2007-01-07 11:00                     ` Srivatsa Vaddagiri
2007-01-07 19:59                       ` Andrew Morton
2007-01-07 21:01                         ` [PATCH] flush_cpu_workqueue: don't flush an empty ->worklist Oleg Nesterov
2007-01-08 23:54                           ` Andrew Morton
2007-01-09  5:04                             ` Srivatsa Vaddagiri
2007-01-09  5:26                               ` Andrew Morton
2007-01-09  6:56                                 ` Ingo Molnar
2007-01-09  9:33                                 ` Srivatsa Vaddagiri
2007-01-09  9:44                                   ` Ingo Molnar
2007-01-09  9:51                                   ` Andrew Morton
2007-01-09 10:09                                     ` Srivatsa Vaddagiri
2007-01-09 10:15                                       ` Andrew Morton
2007-01-09 15:07                                 ` Oleg Nesterov
2007-01-09 15:59                                   ` Srivatsa Vaddagiri
2007-01-09 16:38                                     ` Oleg Nesterov
2007-01-09 16:46                                       ` Srivatsa Vaddagiri
2007-01-09 16:56                                         ` Oleg Nesterov
2007-01-14 23:54                                           ` Oleg Nesterov [this message]
2007-01-15  4:33                                             ` Srivatsa Vaddagiri
2007-01-15 12:54                                               ` Oleg Nesterov
2007-01-15 13:08                                                 ` Oleg Nesterov
2007-01-15 16:18                                                 ` Srivatsa Vaddagiri
2007-01-15 16:55                                                   ` Oleg Nesterov
2007-01-16  5:26                                                     ` Srivatsa Vaddagiri
2007-01-16 13:27                                                       ` Oleg Nesterov
2007-01-17  6:17                                                         ` Srivatsa Vaddagiri
2007-01-17 15:47                                                           ` Oleg Nesterov
2007-01-17 16:12                                                             ` Srivatsa Vaddagiri
2007-01-17 17:01                                                               ` Oleg Nesterov
2007-01-17 16:25                                                             ` Srivatsa Vaddagiri
2007-01-07 21:51                         ` [PATCH] fix-flush_workqueue-vs-cpu_dead-race-update Oleg Nesterov
2007-01-08 15:22                           ` Srivatsa Vaddagiri
2007-01-08 15:56                             ` Oleg Nesterov
2007-01-08 16:31                               ` Srivatsa Vaddagiri
2007-01-08 17:06                                 ` Oleg Nesterov
2007-01-08 18:37                                   ` Pallipadi, Venkatesh
2007-01-09  1:11                                     ` Srivatsa Vaddagiri
2007-01-09  4:39                                   ` Srivatsa Vaddagiri
2007-01-09 14:38                                     ` Oleg Nesterov
2007-01-08 15:37                         ` Srivatsa Vaddagiri
2007-01-04 12:02 ` [PATCH, RFC] reimplement flush_workqueue() Srivatsa Vaddagiri
2007-01-04 14:38   ` Oleg Nesterov

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=20070114235410.GA6165@tv-sign.ru \
    --to=oleg@tv-sign.ru \
    --cc=akpm@osdl.org \
    --cc=dhowells@redhat.com \
    --cc=ego@in.ibm.com \
    --cc=hch@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=torvalds@osdl.org \
    --cc=vatsa@in.ibm.com \
    --cc=venkatesh.pallipadi@intel.com \
    /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;
as well as URLs for NNTP newsgroup(s).