mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@osdl.org>
To: Linus Torvalds <torvalds@osdl.org>
Cc: David Howells <dhowells@redhat.com>,
	"Maciej W. Rozycki" <macro@linux-mips.org>,
	Roland Dreier <rdreier@cisco.com>,
	Andy Fleming <afleming@freescale.com>,
	Ben Collins <ben.collins@ubuntu.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Jeff Garzik <jeff@garzik.org>
Subject: Re: [PATCH] Export current_is_keventd() for libphy
Date: Thu, 7 Dec 2006 09:52:53 -0800	[thread overview]
Message-ID: <20061207095253.30059224.akpm@osdl.org> (raw)
In-Reply-To: <Pine.LNX.4.64.0612070846550.3615@woody.osdl.org>

On Thu, 7 Dec 2006 08:49:02 -0800 (PST)
Linus Torvalds <torvalds@osdl.org> wrote:

> 
> 
> On Wed, 6 Dec 2006, Andrew Morton wrote:
> >
> > But this will return to the caller if the callback is presently running on
> > a different CPU.  The whole point here is to be able to reliably kill off
> > the pending work so that the caller can free resources.
> 
> I mentioned that in one of the emails.
> 
> We do not _have_ the information to not do that. It simply doesn't exist. 
> We can either wait for _all_ pending entries on the to complete (by 
> waiting for the workqueue counters for added/removed to be the same), or 
> we can have the race.

Well we'll need to add the infrastructure to be able to do this, won't we? 
The whole point of calling flush_scheduled_work() (which we're trying to
replace/simplify) is to block the caller until it is safe to release
resources.

It might be a challenge to do this without adding more stuff to work_struct
though.

umm..  Putting a work_struct* into struct cpu_workqueue_struct and then
doing appropriate things with cpu_workqueue_struct.lock might work.

<hack, hack>

Something along these lines.  The keventd-calls-flush_work() case rather
sucks though.


diff -puN kernel/workqueue.c~a kernel/workqueue.c
--- a/kernel/workqueue.c~a
+++ a/kernel/workqueue.c
@@ -323,6 +323,7 @@ static void run_workqueue(struct cpu_wor
 		work_func_t f = work->func;
 
 		list_del_init(cwq->worklist.next);
+		cwq->current_work = work;
 		spin_unlock_irqrestore(&cwq->lock, flags);
 
 		BUG_ON(get_wq_data(work) != cwq);
@@ -342,6 +343,7 @@ static void run_workqueue(struct cpu_wor
 		}
 
 		spin_lock_irqsave(&cwq->lock, flags);
+		cwq->current_work = NULL;
 		cwq->remove_sequence++;
 		wake_up(&cwq->work_done);
 	}
@@ -425,6 +427,64 @@ static void flush_cpu_workqueue(struct c
 	}
 }
 
+static void wait_on_work(struct cpu_workqueue_struct *cwq,
+				struct work_struct *work)
+{
+	DEFINE_WAIT(wait);
+
+	spin_lock_irq(&cwq->lock);
+	while (cwq->current_work == work) {
+		prepare_to_wait(&cwq->work_done, &wait, TASK_UNINTERRUPTIBLE);
+		spin_unlock_irq(&cwq->lock);
+		schedule();
+		spin_lock_irq(&cwq->lock);
+	}
+	finish_wait(&cwq->work_done, &wait);
+	spin_unlock_irq(&cwq->lock);
+}
+
+static void flush_one_work(struct cpu_workqueue_struct *cwq,
+				struct work_struct *work)
+{
+	spin_lock_irq(&cwq->lock);
+	if (test_and_clear_bit(WORK_STRUCT_PENDING, &work->management)) {
+		list_del_init(&work->entry);
+		spin_unlock_irq(&cwq->lock);
+		return;
+	}
+	spin_unlock_irq(&cwq->lock);
+
+	/* It's running, or it has completed */
+
+	if (cwq->thread == current) {
+		/* This stinks */
+		/*
+		 * Probably keventd trying to flush its own queue. So simply run
+		 * it by hand rather than deadlocking.
+		 */
+		run_workqueue(cwq);
+	} else {
+		wait_on_work(cwq, work);
+	}
+}
+
+void flush_work(struct work_struct *work)
+{
+	might_sleep();
+
+	if (is_single_threaded(wq)) {
+		/* Always use first cpu's area. */
+		flush_one_work(per_cpu_ptr(wq->cpu_wq, singlethread_cpu), work);
+	} else {
+		int cpu;
+
+		mutex_lock(&workqueue_mutex);
+		for_each_online_cpu(cpu)
+			flush_one_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
+		mutex_unlock(&workqueue_mutex);
+	}
+}
+
 /**
  * flush_workqueue - ensure that any scheduled work has run to completion.
  * @wq: workqueue to flush
_


  reply	other threads:[~2006-12-07 17:53 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-03  5:50 Ben Collins
2006-12-03  9:16 ` Andrew Morton
2006-12-04 19:17   ` Steve Fox
2006-12-05 18:05     ` Maciej W. Rozycki
2006-12-05 17:48   ` Maciej W. Rozycki
2006-12-05 18:07     ` Linus Torvalds
2006-12-05 19:31       ` Andrew Morton
2006-12-05 18:57     ` Andy Fleming
2006-12-06 12:31       ` Maciej W. Rozycki
2006-12-05 20:39     ` Andrew Morton
2006-12-05 20:59       ` Andy Fleming
2006-12-05 21:26         ` Andrew Morton
2006-12-05 21:37           ` Roland Dreier
2006-12-05 21:57             ` Andrew Morton
2006-12-05 23:49               ` Roland Dreier
2006-12-05 23:52               ` Roland Dreier
2006-12-06 15:25               ` Maciej W. Rozycki
2006-12-06 15:57                 ` Andrew Morton
2006-12-06 17:17                   ` Linus Torvalds
2006-12-07  1:21                     ` Linus Torvalds
2006-12-07  6:42                       ` Andrew Morton
2006-12-07  7:49                         ` Andrew Morton
2006-12-07 10:29                         ` David Howells
2006-12-07 10:42                           ` Andrew Morton
2006-12-07 17:05                             ` Jeff Garzik
2006-12-07 17:57                               ` Andrew Morton
2006-12-07 18:17                                 ` Andrew Morton
2006-12-08 16:52                                 ` [PATCH] group xtime, xtime_lock, wall_to_monotonic, avenrun, calc_load_count fields together in ktimed Eric Dumazet
2006-12-09  5:46                                   ` Andrew Morton
2006-12-09  6:07                                     ` Randy Dunlap
2006-12-11 20:44                                     ` Eric Dumazet
2006-12-11 22:00                                       ` Andrew Morton
2006-12-13 21:26                                   ` [PATCH] Introduce time_data, a new structure to hold jiffies, xtime, xtime_lock, wall_to_monotonic, calc_load_count and avenrun Eric Dumazet
2006-12-15  5:24                                     ` Andrew Morton
2006-12-15 11:21                                       ` Eric Dumazet
2006-12-15 16:21                                     ` Eric Dumazet
2006-12-07 18:08                               ` [PATCH] Export current_is_keventd() for libphy Maciej W. Rozycki
2006-12-07 18:59                               ` Andy Fleming
2006-12-07 16:49                         ` Linus Torvalds
2006-12-07 17:52                           ` Andrew Morton [this message]
2006-12-07 18:01                             ` Linus Torvalds
2006-12-07 18:16                               ` Andrew Morton
2006-12-07 18:27                                 ` Linus Torvalds
2006-12-07 15:28                       ` Maciej W. Rozycki
2006-12-06 17:43                   ` David Howells
2006-12-06 17:50                     ` Jeff Garzik
2006-12-06 18:07                       ` Linus Torvalds
2006-12-06 17:53                     ` Linus Torvalds
2006-12-06 17:58                       ` Linus Torvalds
2006-12-06 18:33                         ` Linus Torvalds
2006-12-06 18:37                           ` Linus Torvalds
2006-12-06 18:43                         ` David Howells
2006-12-06 19:02                           ` Linus Torvalds
2006-12-06 18:02                     ` David Howells

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=20061207095253.30059224.akpm@osdl.org \
    --to=akpm@osdl.org \
    --cc=afleming@freescale.com \
    --cc=ben.collins@ubuntu.com \
    --cc=dhowells@redhat.com \
    --cc=jeff@garzik.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=macro@linux-mips.org \
    --cc=rdreier@cisco.com \
    --cc=torvalds@osdl.org \
    /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

all inboxes | Powered by JetHome®