Skip to content

Commit

Permalink
WorkStruct: Merge the pending bit into the wq_data pointer
Browse files Browse the repository at this point in the history
Reclaim a word from the size of the work_struct by folding the pending bit and
the wq_data pointer together.  This shouldn't cause misalignment problems as
all pointers should be at least 4-byte aligned.

Signed-Off-By: David Howells <[email protected]>
  • Loading branch information
dhowells committed Nov 22, 2006
1 parent 6bb49e5 commit 365970a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
4 changes: 2 additions & 2 deletions drivers/block/floppy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,7 @@ static void show_floppy(void)
printk("fdc_busy=%lu\n", fdc_busy);
if (do_floppy)
printk("do_floppy=%p\n", do_floppy);
if (floppy_work.pending)
if (work_pending(&floppy_work))
printk("floppy_work.func=%p\n", floppy_work.func);
if (timer_pending(&fd_timer))
printk("fd_timer.function=%p\n", fd_timer.function);
Expand Down Expand Up @@ -4498,7 +4498,7 @@ static void floppy_release_irq_and_dma(void)
printk("floppy timer still active:%s\n", timeout_message);
if (timer_pending(&fd_timer))
printk("auxiliary floppy timer still active\n");
if (floppy_work.pending)
if (work_pending(&floppy_work))
printk("work still pending\n");
#endif
old_fdc = fdc;
Expand Down
27 changes: 23 additions & 4 deletions include/linux/workqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ struct workqueue_struct;
typedef void (*work_func_t)(void *data);

struct work_struct {
unsigned long pending;
/* the first word is the work queue pointer and the pending flag
* rolled into one */
unsigned long management;
#define WORK_STRUCT_PENDING 0 /* T if work item pending execution */
#define WORK_STRUCT_FLAG_MASK (3UL)
#define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)
struct list_head entry;
work_func_t func;
void *data;
void *wq_data;
};

struct delayed_work {
Expand Down Expand Up @@ -65,7 +69,7 @@ struct execute_work {
#define INIT_WORK(_work, _func, _data) \
do { \
INIT_LIST_HEAD(&(_work)->entry); \
(_work)->pending = 0; \
(_work)->management = 0; \
PREPARE_WORK((_work), (_func), (_data)); \
} while (0)

Expand All @@ -75,6 +79,21 @@ struct execute_work {
init_timer(&(_work)->timer); \
} while (0)

/**
* work_pending - Find out whether a work item is currently pending
* @work: The work item in question
*/
#define work_pending(work) \
test_bit(WORK_STRUCT_PENDING, &(work)->management)

/**
* delayed_work_pending - Find out whether a delayable work item is currently
* pending
* @work: The work item in question
*/
#define delayed_work_pending(work) \
test_bit(WORK_STRUCT_PENDING, &(work)->work.management)


extern struct workqueue_struct *__create_workqueue(const char *name,
int singlethread);
Expand Down Expand Up @@ -115,7 +134,7 @@ static inline int cancel_delayed_work(struct delayed_work *work)

ret = del_timer_sync(&work->timer);
if (ret)
clear_bit(0, &work->work.pending);
clear_bit(WORK_STRUCT_PENDING, &work->work.management);
return ret;
}

Expand Down
41 changes: 32 additions & 9 deletions kernel/workqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,37 @@ static inline int is_single_threaded(struct workqueue_struct *wq)
return list_empty(&wq->list);
}

static inline void set_wq_data(struct work_struct *work, void *wq)
{
unsigned long new, old, res;

/* assume the pending flag is already set and that the task has already
* been queued on this workqueue */
new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING);
res = work->management;
if (res != new) {
do {
old = res;
new = (unsigned long) wq;
new |= (old & WORK_STRUCT_FLAG_MASK);
res = cmpxchg(&work->management, old, new);
} while (res != old);
}
}

static inline void *get_wq_data(struct work_struct *work)
{
return (void *) (work->management & WORK_STRUCT_WQ_DATA_MASK);
}

/* Preempt must be disabled. */
static void __queue_work(struct cpu_workqueue_struct *cwq,
struct work_struct *work)
{
unsigned long flags;

spin_lock_irqsave(&cwq->lock, flags);
work->wq_data = cwq;
set_wq_data(work, cwq);
list_add_tail(&work->entry, &cwq->worklist);
cwq->insert_sequence++;
wake_up(&cwq->more_work);
Expand All @@ -108,7 +131,7 @@ int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
{
int ret = 0, cpu = get_cpu();

if (!test_and_set_bit(0, &work->pending)) {
if (!test_and_set_bit(WORK_STRUCT_PENDING, &work->management)) {
if (unlikely(is_single_threaded(wq)))
cpu = singlethread_cpu;
BUG_ON(!list_empty(&work->entry));
Expand All @@ -123,7 +146,7 @@ EXPORT_SYMBOL_GPL(queue_work);
static void delayed_work_timer_fn(unsigned long __data)
{
struct delayed_work *dwork = (struct delayed_work *)__data;
struct workqueue_struct *wq = dwork->work.wq_data;
struct workqueue_struct *wq = get_wq_data(&dwork->work);
int cpu = smp_processor_id();

if (unlikely(is_single_threaded(wq)))
Expand All @@ -150,12 +173,12 @@ int fastcall queue_delayed_work(struct workqueue_struct *wq,
if (delay == 0)
return queue_work(wq, work);

if (!test_and_set_bit(0, &work->pending)) {
if (!test_and_set_bit(WORK_STRUCT_PENDING, &work->management)) {
BUG_ON(timer_pending(timer));
BUG_ON(!list_empty(&work->entry));

/* This stores wq for the moment, for the timer_fn */
work->wq_data = wq;
set_wq_data(work, wq);
timer->expires = jiffies + delay;
timer->data = (unsigned long)dwork;
timer->function = delayed_work_timer_fn;
Expand All @@ -182,12 +205,12 @@ int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
struct timer_list *timer = &dwork->timer;
struct work_struct *work = &dwork->work;

if (!test_and_set_bit(0, &work->pending)) {
if (!test_and_set_bit(WORK_STRUCT_PENDING, &work->management)) {
BUG_ON(timer_pending(timer));
BUG_ON(!list_empty(&work->entry));

/* This stores wq for the moment, for the timer_fn */
work->wq_data = wq;
set_wq_data(work, wq);
timer->expires = jiffies + delay;
timer->data = (unsigned long)dwork;
timer->function = delayed_work_timer_fn;
Expand Down Expand Up @@ -223,8 +246,8 @@ static void run_workqueue(struct cpu_workqueue_struct *cwq)
list_del_init(cwq->worklist.next);
spin_unlock_irqrestore(&cwq->lock, flags);

BUG_ON(work->wq_data != cwq);
clear_bit(0, &work->pending);
BUG_ON(get_wq_data(work) != cwq);
clear_bit(WORK_STRUCT_PENDING, &work->management);
f(data);

spin_lock_irqsave(&cwq->lock, flags);
Expand Down

0 comments on commit 365970a

Please sign in to comment.