2 * Copyright (C) 2012 Pierre Habouzit <pierre.habouzit@intersec.com>
3 * Copyright (C) 2012 Intersec SAS
5 * This file implements the Linux Pthread Workqueue Regulator, and is part
8 * The Linux Kernel is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
12 * The Linux Kernel is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU General Public License version 2
18 * along with The Linux Kernel. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/cdev.h>
22 #include <linux/device.h>
23 #include <linux/file.h>
25 #include <linux/hash.h>
26 #include <linux/init.h>
27 #include <linux/kref.h>
28 #include <linux/module.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/spinlock.h>
32 #include <linux/timer.h>
33 #include <linux/uaccess.h>
34 #include <linux/wait.h>
36 #ifndef CONFIG_PREEMPT_NOTIFIERS
37 # error PWQ module requires CONFIG_PREEMPT_NOTIFIERS
42 #define PWQR_HASH_BITS 5
43 #define PWQR_HASH_SIZE (1 << PWQR_HASH_BITS)
45 #define PWQR_UC_DELAY (HZ / 10)
46 #define PWQR_OC_DELAY (HZ / 20)
48 #define PWQR_STATE_NONE 0
49 #define PWQR_STATE_UC 1
50 #define PWQR_STATE_OC 2
51 #define PWQR_STATE_DEAD (-1)
53 struct pwqr_task_bucket {
55 struct hlist_head tasks;
61 struct timer_list timer;
62 wait_queue_head_t wqh;
70 unsigned overcommit_wakes;
76 struct preempt_notifier notifier;
77 struct hlist_node link;
79 struct task_struct *task;
86 static struct class *pwqr_class;
87 static int pwqr_major;
88 static struct pwqr_task_bucket pwqr_tasks_hash[PWQR_HASH_SIZE];
89 static struct preempt_ops pwqr_preempt_running_ops;
90 static struct preempt_ops pwqr_preempt_blocked_ops;
91 static struct preempt_ops pwqr_preempt_noop_ops;
93 /*****************************************************************************
97 #define pwqr_sb_lock_irqsave(sb, flags) \
98 spin_lock_irqsave(&(sb)->wqh.lock, flags)
99 #define pwqr_sb_unlock_irqrestore(sb, flags) \
100 spin_unlock_irqrestore(&(sb)->wqh.lock, flags)
102 static inline void pwqr_arm_timer(struct pwqr_sb *sb, int how, int delay)
104 if (timer_pending(&sb->timer) && sb->state == how)
106 mod_timer(&sb->timer, jiffies + delay);
110 static inline void __pwqr_sb_update_state(struct pwqr_sb *sb, int running_delta)
112 sb->running += running_delta;
114 if (sb->running < sb->concurrency && sb->waiting == 0 && sb->parked) {
115 pwqr_arm_timer(sb, PWQR_STATE_UC, PWQR_UC_DELAY);
116 } else if (sb->running > sb->concurrency) {
117 pwqr_arm_timer(sb, PWQR_STATE_OC, PWQR_OC_DELAY);
119 sb->state = PWQR_STATE_NONE;
120 if (!timer_pending(&sb->timer))
121 del_timer(&sb->timer);
125 static void pwqr_sb_timer_cb(unsigned long arg)
127 struct pwqr_sb *sb = (struct pwqr_sb *)arg;
130 pwqr_sb_lock_irqsave(sb, flags);
131 if (sb->running < sb->concurrency && sb->waiting == 0 && sb->parked) {
132 if (sb->overcommit_wakes == 0)
133 wake_up_locked(&sb->wqh);
135 if (sb->running > sb->concurrency) {
136 /* See ../Documentation/pwqr.adoc */
138 pwqr_sb_unlock_irqrestore(sb, flags);
141 static struct pwqr_sb *pwqr_sb_create(void)
145 sb = kzalloc(sizeof(struct pwqr_sb), GFP_KERNEL);
147 return ERR_PTR(-ENOMEM);
149 kref_init(&sb->kref);
150 init_waitqueue_head(&sb->wqh);
151 sb->concurrency = num_online_cpus();
152 init_timer(&sb->timer);
153 sb->timer.function = pwqr_sb_timer_cb;
154 sb->timer.data = (unsigned long)sb;
156 __module_get(THIS_MODULE);
159 static inline void pwqr_sb_get(struct pwqr_sb *sb)
164 static void pwqr_sb_finalize(struct rcu_head *rcu)
166 struct pwqr_sb *sb = container_of(rcu, struct pwqr_sb, rcu);
168 module_put(THIS_MODULE);
172 static void pwqr_sb_release(struct kref *kref)
174 struct pwqr_sb *sb = container_of(kref, struct pwqr_sb, kref);
176 del_timer_sync(&sb->timer);
177 call_rcu(&sb->rcu, pwqr_sb_finalize);
179 static inline void pwqr_sb_put(struct pwqr_sb *sb)
181 kref_put(&sb->kref, pwqr_sb_release);
184 /*****************************************************************************
187 static inline struct pwqr_task_bucket *task_hbucket(struct task_struct *task)
189 return &pwqr_tasks_hash[hash_ptr(task, PWQR_HASH_BITS)];
192 static struct pwqr_task *pwqr_task_find(struct task_struct *task)
194 struct pwqr_task_bucket *b = task_hbucket(task);
195 struct hlist_node *node;
196 struct pwqr_task *pwqt = NULL;
199 hlist_for_each_entry(pwqt, node, &b->tasks, link) {
200 if (pwqt->task == task)
203 spin_unlock(&b->lock);
207 static struct pwqr_task *pwqr_task_create(struct task_struct *task)
209 struct pwqr_task_bucket *b = task_hbucket(task);
210 struct pwqr_task *pwqt;
212 pwqt = kmalloc(sizeof(*pwqt), GFP_KERNEL);
214 return ERR_PTR(-ENOMEM);
216 preempt_notifier_init(&pwqt->notifier, &pwqr_preempt_running_ops);
217 preempt_notifier_register(&pwqt->notifier);
221 hlist_add_head(&pwqt->link, &b->tasks);
222 spin_unlock(&b->lock);
228 static void pwqr_task_detach(struct pwqr_task *pwqt, struct pwqr_sb *sb)
232 pwqr_sb_lock_irqsave(sb, flags);
234 if (pwqt->notifier.ops == &pwqr_preempt_running_ops) {
235 __pwqr_sb_update_state(sb, -1);
237 __pwqr_sb_update_state(sb, 0);
239 pwqr_sb_unlock_irqrestore(sb, flags);
245 static void pwqr_task_attach(struct pwqr_task *pwqt, struct pwqr_sb *sb)
249 pwqr_sb_lock_irqsave(sb, flags);
250 pwqr_sb_get(pwqt->sb = sb);
252 __pwqr_sb_update_state(sb, 1);
253 pwqr_sb_unlock_irqrestore(sb, flags);
257 static void pwqr_task_release(struct pwqr_task *pwqt, bool from_notifier)
259 struct pwqr_task_bucket *b = task_hbucket(pwqt->task);
262 hlist_del(&pwqt->link);
263 spin_unlock(&b->lock);
264 pwqt->notifier.ops = &pwqr_preempt_noop_ops;
267 /* When called from sched_{out,in}, it's not allowed to
268 * call preempt_notifier_unregister (or worse kfree())
270 * Though it's not a good idea to kfree() still registered
271 * callbacks if we're not dying, it'll panic on the next
272 * sched_{in,out} call.
274 BUG_ON(!(pwqt->task->state & TASK_DEAD));
275 kfree_rcu(pwqt, rcu);
277 preempt_notifier_unregister(&pwqt->notifier);
282 static void pwqr_task_noop_sched_in(struct preempt_notifier *notifier, int cpu)
286 static void pwqr_task_noop_sched_out(struct preempt_notifier *notifier,
287 struct task_struct *next)
291 static void pwqr_task_blocked_sched_in(struct preempt_notifier *notifier, int cpu)
293 struct pwqr_task *pwqt = container_of(notifier, struct pwqr_task, notifier);
294 struct pwqr_sb *sb = pwqt->sb;
297 if (unlikely(sb->state < 0)) {
298 pwqr_task_detach(pwqt, sb);
299 pwqr_task_release(pwqt, true);
303 pwqt->notifier.ops = &pwqr_preempt_running_ops;
304 pwqr_sb_lock_irqsave(sb, flags);
305 __pwqr_sb_update_state(sb, 1);
306 pwqr_sb_unlock_irqrestore(sb, flags);
309 static void pwqr_task_sched_out(struct preempt_notifier *notifier,
310 struct task_struct *next)
312 struct pwqr_task *pwqt = container_of(notifier, struct pwqr_task, notifier);
313 struct pwqr_sb *sb = pwqt->sb;
314 struct task_struct *p = pwqt->task;
316 if (unlikely(p->state & TASK_DEAD) || unlikely(sb->state < 0)) {
317 pwqr_task_detach(pwqt, sb);
318 pwqr_task_release(pwqt, true);
321 if (p->state == 0 || (p->state & (__TASK_STOPPED | __TASK_TRACED)))
324 pwqt->notifier.ops = &pwqr_preempt_blocked_ops;
325 /* see preempt.h: irq are disabled for sched_out */
326 spin_lock(&sb->wqh.lock);
327 __pwqr_sb_update_state(sb, -1);
328 spin_unlock(&sb->wqh.lock);
331 static struct preempt_ops __read_mostly pwqr_preempt_noop_ops = {
332 .sched_in = pwqr_task_noop_sched_in,
333 .sched_out = pwqr_task_noop_sched_out,
336 static struct preempt_ops __read_mostly pwqr_preempt_running_ops = {
337 .sched_in = pwqr_task_noop_sched_in,
338 .sched_out = pwqr_task_sched_out,
341 static struct preempt_ops __read_mostly pwqr_preempt_blocked_ops = {
342 .sched_in = pwqr_task_blocked_sched_in,
343 .sched_out = pwqr_task_sched_out,
346 /*****************************************************************************
349 static int pwqr_open(struct inode *inode, struct file *filp)
353 sb = pwqr_sb_create();
356 filp->private_data = sb;
360 static int pwqr_release(struct inode *inode, struct file *filp)
362 struct pwqr_sb *sb = filp->private_data;
365 pwqr_sb_lock_irqsave(sb, flags);
366 sb->state = PWQR_STATE_DEAD;
367 pwqr_sb_unlock_irqrestore(sb, flags);
368 wake_up_all(&sb->wqh);
374 do_pwqr_wait(struct pwqr_sb *sb, struct pwqr_task *pwqt,
375 int is_wait, struct pwqr_ioc_wait __user *arg)
378 struct pwqr_ioc_wait wait;
382 preempt_notifier_unregister(&pwqt->notifier);
385 if (copy_from_user(&wait, arg, sizeof(wait))) {
389 if (unlikely((long)wait.pwqr_uaddr % sizeof(int) != 0)) {
395 pwqr_sb_lock_irqsave(sb, flags);
396 if (sb->running + sb->waiting <= sb->concurrency) {
398 while (probe_kernel_address(wait.pwqr_uaddr, uval)) {
399 pwqr_sb_unlock_irqrestore(sb, flags);
400 rc = get_user(uval, (u32 *)wait.pwqr_uaddr);
403 pwqr_sb_lock_irqsave(sb, flags);
406 if (uval != (u32)wait.pwqr_ticket) {
415 /* @ see <wait_event_interruptible_exclusive_locked_irq> */
416 if (likely(sb->state >= 0)) {
419 __wait.flags |= WQ_FLAG_EXCLUSIVE;
423 __add_wait_queue(&sb->wqh, &__wait);
426 __add_wait_queue_tail(&sb->wqh, &__wait);
428 __pwqr_sb_update_state(sb, -1);
429 set_current_state(TASK_INTERRUPTIBLE);
432 if (sb->overcommit_wakes)
434 if (signal_pending(current)) {
438 spin_unlock_irq(&sb->wqh.lock);
440 spin_lock_irq(&sb->wqh.lock);
443 if (sb->running + sb->waiting < sb->concurrency)
445 } while (likely(sb->state >= 0));
447 __remove_wait_queue(&sb->wqh, &__wait);
448 __set_current_state(TASK_RUNNING);
455 __pwqr_sb_update_state(sb, 1);
456 if (sb->overcommit_wakes)
457 sb->overcommit_wakes--;
458 if (sb->waiting + sb->running > sb->concurrency)
463 if (unlikely(sb->state < 0))
465 pwqr_sb_unlock_irqrestore(sb, flags);
467 preempt_notifier_register(&pwqt->notifier);
471 static long do_pwqr_unregister(struct pwqr_sb *sb, struct pwqr_task *pwqt)
477 pwqr_task_detach(pwqt, sb);
478 pwqr_task_release(pwqt, false);
482 static long do_pwqr_set_conc(struct pwqr_sb *sb, int conc)
484 long old_conc = sb->concurrency;
487 pwqr_sb_lock_irqsave(sb, flags);
489 conc = num_online_cpus();
490 if (conc != old_conc) {
491 sb->concurrency = conc;
492 __pwqr_sb_update_state(sb, 0);
494 pwqr_sb_unlock_irqrestore(sb, flags);
499 static long do_pwqr_wake(struct pwqr_sb *sb, int oc, int count)
507 pwqr_sb_lock_irqsave(sb, flags);
510 nwake = sb->waiting + sb->parked - sb->overcommit_wakes;
516 sb->overcommit_wakes += count;
517 } else if (sb->running + sb->overcommit_wakes < sb->concurrency) {
518 nwake = sb->concurrency - sb->overcommit_wakes - sb->running;
519 if (nwake > sb->waiting + sb->parked - sb->overcommit_wakes) {
520 nwake = sb->waiting + sb->parked -
521 sb->overcommit_wakes;
530 * This codepath deserves an explanation: waking the thread
531 * "for real" would overcommit, though userspace KNOWS there
532 * is at least one waiting thread. Such threads are threads
533 * that are "quarantined".
535 * Quarantined threads are woken up one by one, to allow a
536 * slow ramp down, trying to minimize "waiting" <-> "parked"
537 * flip-flops, no matter how many wakes have been asked.
539 * Since releasing one quarantined thread will wake up a
540 * thread that will (almost) straight go to parked mode, lie
541 * to userland about the fact that we unblocked that thread,
544 * Though if we're already waking all waiting threads for
545 * overcommitting jobs, well, we don't need that.
548 nwake = sb->waiting > sb->overcommit_wakes;
551 wake_up_locked(&sb->wqh);
552 pwqr_sb_unlock_irqrestore(sb, flags);
557 static long pwqr_ioctl(struct file *filp, unsigned command, unsigned long arg)
559 struct pwqr_sb *sb = filp->private_data;
560 struct task_struct *task = current;
561 struct pwqr_task *pwqt;
566 return sb->concurrency;
568 return do_pwqr_set_conc(sb, (int)arg);
572 return do_pwqr_wake(sb, command == PWQR_WAKE_OC, (int)arg);
577 case PWQR_UNREGISTER:
583 pwqt = pwqr_task_find(task);
584 if (command == PWQR_UNREGISTER)
585 return do_pwqr_unregister(sb, pwqt);
588 pwqt = pwqr_task_create(task);
590 return PTR_ERR(pwqt);
591 pwqr_task_attach(pwqt, sb);
592 } else if (unlikely(pwqt->sb != sb)) {
593 pwqr_task_detach(pwqt, pwqt->sb);
594 pwqr_task_attach(pwqt, sb);
599 rc = do_pwqr_wait(sb, pwqt, true, (struct pwqr_ioc_wait __user *)arg);
602 rc = do_pwqr_wait(sb, pwqt, false, NULL);
606 if (unlikely(sb->state < 0)) {
607 pwqr_task_detach(pwqt, pwqt->sb);
613 static const struct file_operations pwqr_dev_fops = {
614 .owner = THIS_MODULE,
616 .release = pwqr_release,
617 .unlocked_ioctl = pwqr_ioctl,
619 .compat_ioctl = pwqr_ioctl,
623 /*****************************************************************************
626 static int __init pwqr_start(void)
630 for (i = 0; i < PWQR_HASH_SIZE; i++) {
631 spin_lock_init(&pwqr_tasks_hash[i].lock);
632 INIT_HLIST_HEAD(&pwqr_tasks_hash[i].tasks);
635 /* Register as a character device */
636 pwqr_major = register_chrdev(0, "pwqr", &pwqr_dev_fops);
637 if (pwqr_major < 0) {
638 printk(KERN_ERR "pwqr: register_chrdev() failed\n");
642 /* Create a device node */
643 pwqr_class = class_create(THIS_MODULE, PWQR_DEVICE_NAME);
644 if (IS_ERR(pwqr_class)) {
645 printk(KERN_ERR "pwqr: Error creating raw class\n");
646 unregister_chrdev(pwqr_major, PWQR_DEVICE_NAME);
647 return PTR_ERR(pwqr_class);
649 device_create(pwqr_class, NULL, MKDEV(pwqr_major, 0), NULL, PWQR_DEVICE_NAME);
650 printk(KERN_INFO "pwqr: PThreads Work Queues Regulator v1 loaded");
654 static void __exit pwqr_end(void)
657 device_destroy(pwqr_class, MKDEV(pwqr_major, 0));
658 class_destroy(pwqr_class);
659 unregister_chrdev(pwqr_major, PWQR_DEVICE_NAME);
662 module_init(pwqr_start);
663 module_exit(pwqr_end);
665 MODULE_LICENSE("GPL");
666 MODULE_AUTHOR("Pierre Habouzit <pierre.habouzit@intersec.com>");
667 MODULE_DESCRIPTION("PThreads Work Queues Regulator");
669 // vim:noet:sw=8:cinoptions+=\:0,L-1,=1s: