Workqueue Abstraction, 2.5.40-H7
Subsystems (2)
PATCH 1/1 Workqueue Abstraction, 2.5.40-H7
the attached (compressed) patch is the next iteration of the workqueue abstraction. There are two major categories of changes:
- a much improved (i hope) core framework
- (almost) complete conversion of existing drivers to the new framework.
1) The framework improvements include:
- per-CPU queueing support.
on SMP there is a per-CPU worker thread (bound to its CPU) and per-CPU work queues - this feature is completely transparent to workqueue-users. keventd automatically uses this feature. XFS can now update to work-queues and have the same per-CPU performance as it had with its per-CPU worker threads.
- delayed work submission
there's a new queue_delayed_work(wq, work, delay) function and a new schedule_delayed_work(work, delay) function. The later one is used to correctly fix former tq_timer users. I've reverted those changes in 2.5.40 that changed tq_timer uses to schedule_work() - eg. in the case of random.c or the tty flip queue it was definitely the wrong thing to do.
delayed work means a timer embedded in work_t. I considered using split work_t and delayed_work_t types, but lots of code actively uses task-queues in both delayed and non-delayed mode, so i went for the more generic approach that allows both methods of work submission. Delayed timers do not cause any other overhead in the normal submission path otherwise.
- multithreaded run_workqueue() implementation
the run_workqueue() function can now be called from multiple contexts, and a worker thread will only use up a single entryy - this property is used by the flushing code, and can potentially be used in the future to extend the number of per-CPU worker threads.
- more reliable flushing
there's now a 'pending work' counter, which is used to accurately detect when the last work-function has finished execution. It's also used to correctly flush against timed requests. I'm not convinced whether the old keventd implementation got this detail right.
- i switched the arguments of the queueing function(s) per Jeff's suggestion, it's more straightforward this way.
2) driver fixes.
i have converted almost every affected driver to the new framework. This cleaned up tons of code. I also fixed a number of drivers that were still using BHs (these drivers did not compile in 2.5.40).
while this means lots of changes, it might ease the QA decision whether to put this patch into 2.5.
The pach converts roughly 80% of all tqueue-using code to workqueues - and all the places that are not converted to workqueues yet are places that do not compile in vanilla 2.5.40 anyway, due to unrelated changes. I've converted a fair number of drivers that do not compile in 2.5.40, and i think i've managed to convert every driver that compiles under 2.5.40.
i've tested the patch on 2.5.40, UP and SMP x86, boots & works just fine.
I've also attached maxi-config, which is a pretty large .config - this config file compiles the kernel just fine. (it's too large to be booted though.) No workqueue/taskqueue related warning messages or compile errors.
Ingo
Quoted 5 lines · Ingo Molnar
2) driver fixes.
i have converted almost every affected driver to the new framework. This cleaned up tons of code. I also fixed a number of drivers that were still using BHs (these drivers did not compile in 2.5.40).
I'm possibly messing things up here, but doesn't it generally make more sense to convert tq_immediate users to tasklets instead of work queues?
tq_immediate users do not need process context, and one use I'm familiar with is basically doing bottom half interrupt processing, e.g. in lots of places in the ISDN code. Introducing a context switch for no obvious gain there seems rather pointless to me?
The same may be true for the tq_timer users as well?
--Kai
Quoted 9 lines · Kai Germaschewski
I'm possibly messing things up here, but doesn't it generally make more sense to convert
tq_immediateusers to tasklets instead of work queues?
tq_immediateusers do not need process context, and one use I'm familiar with is basically doing bottom half interrupt processing, e.g. in lots of places in the ISDN code. Introducing a context switch for no obvious gain there seems rather pointless to me?The same may be true for the
tq_timerusers as well?
the main reason was that it was easier to convert everything (even old-BH style code) that did deferred processing to workqueues than to tasklets - since a fair chunk of deferred processing needs process context. Another reason is that generally it's easier to handle overload situations if the work is done in process contexts. But i agree, for things where it really matters performance-wise, introducing a tasklet should be the next step.
Ingo
Ingo,
Looking real good.
I still think that schedule_work() should have void* cookie passed to it directly, instead of at INIT_WORK time [and possibly changing it by hand in the driver, immediately before schedule_work() is called]
For drivers that pass an interface pointer like struct net_device*, INIT_WORK-time, the current scheme is fine, but when the cookie fluctuates more, it makes a lot more sense to pass void* to schedule_work() itself.
Further, schedule_work(wq,data) is conceptually very close to my_work_func(data) and makes the code easier to trace through: it becomes more obvious what is the value of the my_work_func arg, at the place in the code where schedule_work() is called. I see passing the void* cookie as covering one common case, while adding void* arg to schedule_work() would cover all cases...
[IMO the same argument can be applied to the existing timer API as well, but timers are less often one-shot in kernel code, so it matter less...]
That said, I don't feel strongly about this, so can be convinced otherwise fairly easily :) I would not complain if Linus applied your patch as-is.
Jeff
Quoted 3 lines · Jeff Garzik
I still think that
schedule_work()should have void* cookie passed to it directly, instead of atINIT_WORKtime [and possibly changing it by hand in the driver, immediately beforeschedule_work()is called]
i dont think this is a good idea, this pretty much forces the argument passing upon every user of the interface - which argument would be put into the worqueue-entry struct anyway.
the code behaves just right when only PREPARE_WORK() is used - the completion code leaves the entry in a restartable state. A full INIT_WORK() is only needed at init time. (or if DECLARE_WORK() was used then no INIT_WORK() is needed.) And this is all intentional.
Quoted 4 lines
For drivers that pass an interface pointer like struct net_device*, INIT_WORK-time, the current scheme is fine, but when the cookie fluctuates more, it makes a lot more sense to pass void* to
schedule_work()itself.
these places should use PREPARE_WORK().
Ingo
At the risk of being on-topic <g>, would it be reasonable to request a 2.4 version of this patch, that can be used in driver-compat code and vendor kernels? i.e. something that makes the workqueue API work in 2.4, no more, no less.
Jeff
Quoted 4 lines · Jeff Garzik
At the risk of being on-topic <g>, would it be reasonable to request a 2.4 version of this patch, that can be used in driver-compat code and vendor kernels? i.e. something that makes the workqueue API work in 2.4, no more, no less.
i've concentrated all the code into kernel/workqueue.c, and all the interfaces into include/linux/workqueue.h - so you can simply copy those into the 2.4 tree and it should just work. There should be no namespace collision either. A O(1) scheduler tree is needed, due to set_cpus_allowed(), but there is no other dependency i think.
Ingo
Quoted 3 lines · Ingo Molnar
the attached (compressed) patch is the next iteration of the workqueue abstraction. There are two major categories of changes:
Pease don't introduce more typedefs. They only hide what the hell the thing is, which is actively _bad_ for structures, since passing a structure by value etc is something that should never be done, for example.
The few saved characters of typing do not actually _buy_ you anything else, and only obscures what the thing is.
Also, it's against the Linux coding standard, which does not like adding magic single-letter suffixes to things - that also is the case for your strange "_s" suffix for a structure (the real suffix is "_struct").
Remember: typing out something is not bad. It's _especially_ not bad if the typing makes it more clear what the thing is.
I've done a global search-and-replace on the patch. The resulting patch is actually _cleaner_, because it also matches more closely the old code (which used "struct tq_struct"), so things like tabbed comment alignment etc tend to be more correct (not always, but closer).
Linus
In article <[email protected]>,
Quoted 5 lines · Linus Torvalds
Pease don't introduce more typedefs. They only hide what the hell the thing is, which is actively _bad_ for structures, since passing a structure by value etc is something that should never be done, for example.
Btw, just to avoid counter-examples: Linux does use structures and typedefs occasionally to hide and force compiler typechecking on small structures on purpose. We have a few places where we do things like
typedef struct {
unsigned int value;
} atomic_t;(and similar things for the page table entries etc).
This is done because the things are often really regular scalars, but we use the structure as a strict type checking mechanism. In this case, using a typedef is fine, because we don't actually ever want to _access_ it as a structure, and the typedef provices exactly the kind of information hiding that we need.
But type hiding for a real structure just doesn't make sense, since we use it as a true structure, and hiding information just makes it harder to see.
Linus
Quoted 3 lines · Ingo Molnar
the attached (compressed) patch is the next iteration of the workqueue abstraction. There are two major categories of changes:
What about forcing a flush in destory_workqueue?
kernel/workqueue.c +2 -0
@@ -317,6 +317,8 @@struct cpu_workqueue_struct *cwq;int cpu; +flush_workqueue (wq); +for (cpu =0 ; cpu <NR_CPUS ; cpu++) {if (!cpu_online (cpu))continue ;
Quoted 6 lines · Christoph Hellwig
Quoted 3 lines · Ingo Molnar
the attached (compressed) patch is the next iteration of the workqueue abstraction. There are two major categories of changes:
What about forcing a flush in
destory_workqueue?
yes, most definitely - this was done in yesterday's patch but forgot it in today's.
Ingo
Quoted 9 lines · Ingo Molnar
the attached (compressed) patch is the next iteration of the workqueue abstraction. There are two major categories of changes:
- a much improved (i hope) core framework
- (almost) complete conversion of existing drivers to the new framework.
1) The framework improvements include:
Quoted 7 lines
- per-CPU queueing support.
on SMP there is a per-CPU worker thread (bound to its CPU) and per-CPU work queues - this feature is completely transparent to workqueue-users. keventd automatically uses this feature. XFS can now update to work-queues and have the same per-CPU performance as it had with its per-CPU worker threads.
Hi Ingo,
I read through your patch and I think it looks great. I'm one of the ieee1394 maintainers, and we also a have a worker thread mechanism in the ieee1394 subsystem, which could (and will, I'm going to look into this) be replaced by your work queue stuff. We use the thread for reading configuration information from ieee1394 devices. This work isn't very performance critical, but the reason we dont just use keventd is that we don't want to stall keventd while reading this information. So, my point is, that in this case (I'm sure there are more situations like this, usb has a similar worker thread, khubd), the per-cpu worker thread is overkill, and it would be sufficient with just one thread, running on all cpus. So maybe this could be an option to create_workqueue()? Either create a cpu-bound thread for each cpu, or create one thread that can run on all cpus.
Another minor comment: why do you kmalloc() the workqueue_t? Wouldn't it be more flexible to allow the user to provide a pointer to a pre-allocated workqueue_t structure, e.g.:
static workqueue_t aio_wq;
[...]
create_workqueue(&aio_wq, "aio");Kristian
Quoted 5 lines · Kristian Hogsberg
I read through your patch and I think it looks great. I'm one of the ieee1394 maintainers, and we also a have a worker thread mechanism in the ieee1394 subsystem, which could (and will, I'm going to look into this) be replaced by your work queue stuff. We use the thread for reading configuration information from ieee1394 devices. This work
Quoted 8 lines
isn't very performance critical, but the reason we dont just use keventd is that we don't want to stall keventd while reading this information. So, my point is, that in this case (I'm sure there are more situations like this, usb has a similar worker thread, khubd), the per-cpu worker thread is overkill, and it would be sufficient with just one thread, running on all cpus. So maybe this could be an option to
create_workqueue()? Either create a cpu-bound thread for each cpu, or create one thread that can run on all cpus.
i understand your point, and i really tried to get this problem solved prior sending the multiple-workers patch, but it's not really doable without major kludges. Is it really a problem on SMP boxes to have a few more kernel threads? Those boxes are supposed to have enough RAM.
the only other way would be to introduce 1) runtime overhead [this sucks] or 2) to split the API into per-CPU and global ones. [this isnt too good either i think.]
there is enough flexibility internally - eg. we can in the future do better load-balancing (if the XFS people will ever notice any problems in this area), because right now the load-balancing is the simplest and fastest variant: purely per-CPU. [in theory we could look at other CPU's worker queues and queue there if they are empty or much shorter than this CPU's worker queue.]
I also kept open the possibility of introducing multiple worker threads per CPU in the future. But having a single-threaded an per-CPU behavior in a single API looks quite hard.
we could perhaps do the following, add a single branch to the queue_work() fastpath:
if (!cwq->thread) cwq = wq->cpu_wq;
and the single-thread queue variant would thus fall back to using a single queue only. Plus some sort of new variant could be used:
struct workqueue_struct *create_single_workqueue(char *name);
[or a 'flags' argument to create_workqueue();]
The runtime thing looks slightly ugly, but i think it's acceptable. Has anyone any better idea?
Quoted 9 lines
Another minor comment: why do you
kmalloc()theworkqueue_t? Wouldn't it be more flexible to allow the user to provide a pointer to a pre-allocatedworkqueue_tstructure, e.g.:static workqueue_t aio_wq; [...] create_workqueue(&aio_wq, "aio");
yes, but every creation/destruction use of workqueues is in some sort of init/shutdown very-slow-path, so efficiency is not a factor. But clarity of the code is a factor, and in the dynamic allocation case it's much cleaner to have this:
wq = create_workqueue("worker");
if (!wq)
goto error;than:
wq = kmalloc(sizeof(*wq), GFP_KERNEL);
if (!wq)
goto error;
if (create_workqueue(wq, "worker")) {
kfree(w);
goto error;
}[and we might even switch to a workqueue SLAB cache internally anytime.]
Ingo
Ingo Molnar <[email protected]> writes:
[...]
Quoted 23 lines
Quoted 2 lines
create_workqueue()? Either create a cpu-bound thread for each cpu, or create one thread that can run on all cpus.i understand your point, and i really tried to get this problem solved prior sending the multiple-workers patch, but it's not really doable without major kludges. Is it really a problem on SMP boxes to have a few more kernel threads? Those boxes are supposed to have enough RAM.
the only other way would be to introduce 1) runtime overhead [this sucks] or 2) to split the API into per-CPU and global ones. [this isnt too good either i think.]
there is enough flexibility internally - eg. we can in the future do better load-balancing (if the XFS people will ever notice any problems in this area), because right now the load-balancing is the simplest and fastest variant: purely per-CPU. [in theory we could look at other CPU's worker queues and queue there if they are empty or much shorter than this CPU's worker queue.]
I also kept open the possibility of introducing multiple worker threads per CPU in the future. But having a single-threaded an per-CPU behavior in a single API looks quite hard.
Quoted 5 lines
we could perhaps do the following, add a single branch to the
queue_work()fastpath:if (!cwq->thread) cwq = wq->cpu_wq;
Yeah, that was what I was thinking of... if you want to avoid the branch in the fastpath, we could do something like
struct workqueue_s {
cpu_workqueue_t *cpu_wq_ptr[NR_CPUS]
cpu_workqueue_t cpu_wq[NR_CPUS];
};and initialize the cpu_wq_ptr entries to
cpu_wq_ptr[i] = cpu_wq + i
in the multi-threaded case and to
cpu_wq_ptr[i] = cpu_wq
in the single thread case. The lookup in queue_work() becomes
cpu_workqueue_t *cwq = wq->cpu_wq[cpu];
which costs an extra pointer deref. Not sure if it's better though. Alternatively, the !cwq->thread test you're suggesting could be marked unlikely() so that the multi threaded case which will be used for performance critical workqueues will be favoured by the compiler.
Quoted 31 lines
and the single-thread queue variant would thus fall back to using a single queue only. Plus some sort of new variant could be used:
struct workqueue_struct *create_single_workqueue(char *name);[or a 'flags' argument to
create_workqueue();]The runtime thing looks slightly ugly, but i think it's acceptable. Has anyone any better idea?
Quoted 9 lines
Another minor comment: why do you
kmalloc()theworkqueue_t? Wouldn't it be more flexible to allow the user to provide a pointer to a pre-allocatedworkqueue_tstructure, e.g.:static workqueue_t aio_wq; [...] create_workqueue(&aio_wq, "aio");yes, but every creation/destruction use of workqueues is in some sort of init/shutdown very-slow-path, so efficiency is not a factor. But clarity of the code is a factor, and in the dynamic allocation case it's much cleaner to have this:
wq = create_workqueue("worker"); if (!wq) goto error;than:
Quoted 7 lines
wq = kmalloc(sizeof(*wq), GFP_KERNEL);
if (!wq)
goto error;
if (create_workqueue(wq, "worker")) {
kfree(w);
goto error;
}Sure code clarity is the biggest factor here, but I was thinking we could cut down the number of dynamic allocations. My guess is that in the common case, the workqueue_t is either statically allocated (as in the case of keventd) or embedded in another struct and allocated as part of that struct:
struct hpsb_host {
char *name;
/* whatnot */
workqueue_t wq;
};
...
host = kmalloc(sizeof *host, GFP_KERNEL);
if (!host)
goto error:
host->name = "ohci1394";
workqueue_init(&host->wq, "ieee1394");best regards,
Kristian
Hello!
Quoted 5 lines · Ingo Molnar
The pach converts roughly 80% of all tqueue-using code to workqueues - and all the places that are not converted to workqueues yet are places that do not compile in vanilla 2.5.40 anyway, due to unrelated changes. I've converted a fair number of drivers that do not compile in 2.5.40, and i think i've managed to convert every driver that compiles under 2.5.40.
Here are corresponding changes to UML code (that were not done by Ingo just because this code cannot be compiled due to Makefile bug).
This patch is required to make UML compilable again in bk-current (after you fix the Makefile, of course).
I tested the patch and UML works fine for me.
Bye,
Oleg
===== arch/um/drivers/chan_kern.c 1.2 vs edited =====
arch/um/drivers/chan_kern.c +2 -2
@@ -395,7 +395,7 @@return (-1 ); } -void chan_interrupt (struct list_head *chans,struct tq_struct *task, +void chan_interrupt (struct list_head *chans,struct work_struct *task,struct tty_struct *tty,int irq,void *dev) {struct list_head *ele, *next;
@@ -409,7 +409,7 @@ do { if((tty != NULL) && (tty->flip.count >= TTY_FLIPBUF_SIZE)){ - schedule_task(task); + schedule_work(task); goto out; } err = chan->ops->read(chan->fd, &c, chan->data);
===== arch/um/drivers/line.c 1.1 vs edited =====
arch/um/drivers/line.c +1 -1
@@ -215,7 +215,7 @@if (err) goto out; }enable_chan (&line->chan_list, line); -INIT_TQUEUE (&line->task, line_timer_cb, line); +INIT_WORK (&line->task, line_timer_cb, line); }if (!line->sigio){
===== arch/um/drivers/mconsole_kern.c 1.1 vs edited =====
arch/um/drivers/mconsole_kern.c +4 -7
@@ -13,7 +13,7 @@#include "linux/ctype.h" #include "linux/interrupt.h" #include "linux/sysrq.h" -#include "linux/tqueue.h" +#include "linux/workqueue.h" #include "linux/module.h" #include "linux/proc_fs.h" #include "asm/irq.h"
@@ -42,7 +42,7 @@LIST_HEAD (mc_requests); -void mc_task_proc (void *unused) +void mc_work_proc (void *unused) {struct mconsole_entry *req;unsigned long flags;
@@ -60,10 +60,7 @@ }while (!done); } -struct tq_struct mconsole_task = { - routine: mc_task_proc, - data:NULL -}; +DECLARE_WORK (mconsole_work, mc_work_proc,NULL );void mconsole_interrupt (int irq,void *dev_id,struct pt_regs *regs) {
@@ -84,7 +81,7 @@ } } } -if (!list_empty (&mc_requests))schedule_task (&mconsole_task); +if (!list_empty (&mc_requests))schedule_work (&mconsole_work);reactivate_fd (fd,MCONSOLE_IRQ ); }
===== arch/um/include/chan_kern.h 1.1 vs edited =====
arch/um/include/chan_kern.h +1 -1
@@ -22,7 +22,7 @@void *data; }; -extern void chan_interrupt (struct list_head *chans,struct tq_struct *task, +extern void chan_interrupt (struct list_head *chans,struct work_struct *task,struct tty_struct *tty,int irq,void *dev);extern int parse_chan_pair (char *str,struct list_head *chans,int pri,int device,struct chan_opts *opts);
===== arch/um/include/line.h 1.1 vs edited =====
arch/um/include/line.h +2 -2
@@ -7,7 +7,7 @@#define __LINE_H__#include "linux/list.h" -#include "linux/tqueue.h" +#include "linux/workqueue.h" #include "linux/tty.h" #include "asm/semaphore.h" #include "chan_user.h"
@@ -39,7 +39,7 @@char *head;char *tail;int sigio; -struct tq_struct task; +struct work_struct task;struct line_driver *driver;int have_irq; };
Jeff Dike
AppliedQuoted 1 line · [email protected] said:
Here are corresponding changes to UML code
Applied, thanks.
Jeff