Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

WorkStruct: Merge the pending bit into the wq_data pointer

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 <dhowells@redhat.com>

+57 -15
+2 -2
drivers/block/floppy.c
··· 1868 1868 printk("fdc_busy=%lu\n", fdc_busy); 1869 1869 if (do_floppy) 1870 1870 printk("do_floppy=%p\n", do_floppy); 1871 - if (floppy_work.pending) 1871 + if (work_pending(&floppy_work)) 1872 1872 printk("floppy_work.func=%p\n", floppy_work.func); 1873 1873 if (timer_pending(&fd_timer)) 1874 1874 printk("fd_timer.function=%p\n", fd_timer.function); ··· 4498 4498 printk("floppy timer still active:%s\n", timeout_message); 4499 4499 if (timer_pending(&fd_timer)) 4500 4500 printk("auxiliary floppy timer still active\n"); 4501 - if (floppy_work.pending) 4501 + if (work_pending(&floppy_work)) 4502 4502 printk("work still pending\n"); 4503 4503 #endif 4504 4504 old_fdc = fdc;
+23 -4
include/linux/workqueue.h
··· 14 14 typedef void (*work_func_t)(void *data); 15 15 16 16 struct work_struct { 17 - unsigned long pending; 17 + /* the first word is the work queue pointer and the pending flag 18 + * rolled into one */ 19 + unsigned long management; 20 + #define WORK_STRUCT_PENDING 0 /* T if work item pending execution */ 21 + #define WORK_STRUCT_FLAG_MASK (3UL) 22 + #define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK) 18 23 struct list_head entry; 19 24 work_func_t func; 20 25 void *data; 21 - void *wq_data; 22 26 }; 23 27 24 28 struct delayed_work { ··· 69 65 #define INIT_WORK(_work, _func, _data) \ 70 66 do { \ 71 67 INIT_LIST_HEAD(&(_work)->entry); \ 72 - (_work)->pending = 0; \ 68 + (_work)->management = 0; \ 73 69 PREPARE_WORK((_work), (_func), (_data)); \ 74 70 } while (0) 75 71 ··· 78 74 INIT_WORK(&(_work)->work, (_func), (_data)); \ 79 75 init_timer(&(_work)->timer); \ 80 76 } while (0) 77 + 78 + /** 79 + * work_pending - Find out whether a work item is currently pending 80 + * @work: The work item in question 81 + */ 82 + #define work_pending(work) \ 83 + test_bit(WORK_STRUCT_PENDING, &(work)->management) 84 + 85 + /** 86 + * delayed_work_pending - Find out whether a delayable work item is currently 87 + * pending 88 + * @work: The work item in question 89 + */ 90 + #define delayed_work_pending(work) \ 91 + test_bit(WORK_STRUCT_PENDING, &(work)->work.management) 81 92 82 93 83 94 extern struct workqueue_struct *__create_workqueue(const char *name, ··· 134 115 135 116 ret = del_timer_sync(&work->timer); 136 117 if (ret) 137 - clear_bit(0, &work->work.pending); 118 + clear_bit(WORK_STRUCT_PENDING, &work->work.management); 138 119 return ret; 139 120 } 140 121
+32 -9
kernel/workqueue.c
··· 80 80 return list_empty(&wq->list); 81 81 } 82 82 83 + static inline void set_wq_data(struct work_struct *work, void *wq) 84 + { 85 + unsigned long new, old, res; 86 + 87 + /* assume the pending flag is already set and that the task has already 88 + * been queued on this workqueue */ 89 + new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING); 90 + res = work->management; 91 + if (res != new) { 92 + do { 93 + old = res; 94 + new = (unsigned long) wq; 95 + new |= (old & WORK_STRUCT_FLAG_MASK); 96 + res = cmpxchg(&work->management, old, new); 97 + } while (res != old); 98 + } 99 + } 100 + 101 + static inline void *get_wq_data(struct work_struct *work) 102 + { 103 + return (void *) (work->management & WORK_STRUCT_WQ_DATA_MASK); 104 + } 105 + 83 106 /* Preempt must be disabled. */ 84 107 static void __queue_work(struct cpu_workqueue_struct *cwq, 85 108 struct work_struct *work) ··· 110 87 unsigned long flags; 111 88 112 89 spin_lock_irqsave(&cwq->lock, flags); 113 - work->wq_data = cwq; 90 + set_wq_data(work, cwq); 114 91 list_add_tail(&work->entry, &cwq->worklist); 115 92 cwq->insert_sequence++; 116 93 wake_up(&cwq->more_work); ··· 131 108 { 132 109 int ret = 0, cpu = get_cpu(); 133 110 134 - if (!test_and_set_bit(0, &work->pending)) { 111 + if (!test_and_set_bit(WORK_STRUCT_PENDING, &work->management)) { 135 112 if (unlikely(is_single_threaded(wq))) 136 113 cpu = singlethread_cpu; 137 114 BUG_ON(!list_empty(&work->entry)); ··· 146 123 static void delayed_work_timer_fn(unsigned long __data) 147 124 { 148 125 struct delayed_work *dwork = (struct delayed_work *)__data; 149 - struct workqueue_struct *wq = dwork->work.wq_data; 126 + struct workqueue_struct *wq = get_wq_data(&dwork->work); 150 127 int cpu = smp_processor_id(); 151 128 152 129 if (unlikely(is_single_threaded(wq))) ··· 173 150 if (delay == 0) 174 151 return queue_work(wq, work); 175 152 176 - if (!test_and_set_bit(0, &work->pending)) { 153 + if (!test_and_set_bit(WORK_STRUCT_PENDING, &work->management)) { 177 154 BUG_ON(timer_pending(timer)); 178 155 BUG_ON(!list_empty(&work->entry)); 179 156 180 157 /* This stores wq for the moment, for the timer_fn */ 181 - work->wq_data = wq; 158 + set_wq_data(work, wq); 182 159 timer->expires = jiffies + delay; 183 160 timer->data = (unsigned long)dwork; 184 161 timer->function = delayed_work_timer_fn; ··· 205 182 struct timer_list *timer = &dwork->timer; 206 183 struct work_struct *work = &dwork->work; 207 184 208 - if (!test_and_set_bit(0, &work->pending)) { 185 + if (!test_and_set_bit(WORK_STRUCT_PENDING, &work->management)) { 209 186 BUG_ON(timer_pending(timer)); 210 187 BUG_ON(!list_empty(&work->entry)); 211 188 212 189 /* This stores wq for the moment, for the timer_fn */ 213 - work->wq_data = wq; 190 + set_wq_data(work, wq); 214 191 timer->expires = jiffies + delay; 215 192 timer->data = (unsigned long)dwork; 216 193 timer->function = delayed_work_timer_fn; ··· 246 223 list_del_init(cwq->worklist.next); 247 224 spin_unlock_irqrestore(&cwq->lock, flags); 248 225 249 - BUG_ON(work->wq_data != cwq); 250 - clear_bit(0, &work->pending); 226 + BUG_ON(get_wq_data(work) != cwq); 227 + clear_bit(WORK_STRUCT_PENDING, &work->management); 251 228 f(data); 252 229 253 230 spin_lock_irqsave(&cwq->lock, flags);