123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- #include <linux/kthread.h>
- #include <linux/slab.h>
- #include <linux/list.h>
- #include <linux/spinlock.h>
- #include <linux/freezer.h>
- #include "async-thread.h"
- #include "ctree.h"
- enum {
- WORK_DONE_BIT,
- WORK_ORDER_DONE_BIT,
- };
- #define NO_THRESHOLD (-1)
- #define DFT_THRESHOLD (32)
- struct btrfs_workqueue {
- struct workqueue_struct *normal_wq;
-
- struct btrfs_fs_info *fs_info;
-
- struct list_head ordered_list;
-
- spinlock_t list_lock;
-
- atomic_t pending;
-
- int limit_active;
-
- int current_active;
-
- int thresh;
- unsigned int count;
- spinlock_t thres_lock;
- };
- struct btrfs_fs_info * __pure btrfs_workqueue_owner(const struct btrfs_workqueue *wq)
- {
- return wq->fs_info;
- }
- struct btrfs_fs_info * __pure btrfs_work_owner(const struct btrfs_work *work)
- {
- return work->wq->fs_info;
- }
- bool btrfs_workqueue_normal_congested(const struct btrfs_workqueue *wq)
- {
-
- if (wq->thresh == NO_THRESHOLD)
- return false;
- return atomic_read(&wq->pending) > wq->thresh * 2;
- }
- struct btrfs_workqueue *btrfs_alloc_workqueue(struct btrfs_fs_info *fs_info,
- const char *name, unsigned int flags,
- int limit_active, int thresh)
- {
- struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_KERNEL);
- if (!ret)
- return NULL;
- ret->fs_info = fs_info;
- ret->limit_active = limit_active;
- atomic_set(&ret->pending, 0);
- if (thresh == 0)
- thresh = DFT_THRESHOLD;
-
- if (thresh < DFT_THRESHOLD) {
- ret->current_active = limit_active;
- ret->thresh = NO_THRESHOLD;
- } else {
-
- ret->current_active = 1;
- ret->thresh = thresh;
- }
- ret->normal_wq = alloc_workqueue("btrfs-%s", flags, ret->current_active,
- name);
- if (!ret->normal_wq) {
- kfree(ret);
- return NULL;
- }
- INIT_LIST_HEAD(&ret->ordered_list);
- spin_lock_init(&ret->list_lock);
- spin_lock_init(&ret->thres_lock);
- trace_btrfs_workqueue_alloc(ret, name);
- return ret;
- }
- static inline void thresh_queue_hook(struct btrfs_workqueue *wq)
- {
- if (wq->thresh == NO_THRESHOLD)
- return;
- atomic_inc(&wq->pending);
- }
- static inline void thresh_exec_hook(struct btrfs_workqueue *wq)
- {
- int new_current_active;
- long pending;
- int need_change = 0;
- if (wq->thresh == NO_THRESHOLD)
- return;
- atomic_dec(&wq->pending);
- spin_lock(&wq->thres_lock);
-
- wq->count++;
- wq->count %= (wq->thresh / 4);
- if (!wq->count)
- goto out;
- new_current_active = wq->current_active;
-
- pending = atomic_read(&wq->pending);
- if (pending > wq->thresh)
- new_current_active++;
- if (pending < wq->thresh / 2)
- new_current_active--;
- new_current_active = clamp_val(new_current_active, 1, wq->limit_active);
- if (new_current_active != wq->current_active) {
- need_change = 1;
- wq->current_active = new_current_active;
- }
- out:
- spin_unlock(&wq->thres_lock);
- if (need_change) {
- workqueue_set_max_active(wq->normal_wq, wq->current_active);
- }
- }
- static void run_ordered_work(struct btrfs_workqueue *wq,
- struct btrfs_work *self)
- {
- struct list_head *list = &wq->ordered_list;
- struct btrfs_work *work;
- spinlock_t *lock = &wq->list_lock;
- unsigned long flags;
- bool free_self = false;
- while (1) {
- spin_lock_irqsave(lock, flags);
- if (list_empty(list))
- break;
- work = list_entry(list->next, struct btrfs_work,
- ordered_list);
- if (!test_bit(WORK_DONE_BIT, &work->flags))
- break;
-
- smp_rmb();
-
- if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
- break;
- trace_btrfs_ordered_sched(work);
- spin_unlock_irqrestore(lock, flags);
- work->ordered_func(work);
-
- spin_lock_irqsave(lock, flags);
- list_del(&work->ordered_list);
- spin_unlock_irqrestore(lock, flags);
- if (work == self) {
-
- free_self = true;
- } else {
-
- work->ordered_free(work);
-
- trace_btrfs_all_work_done(wq->fs_info, work);
- }
- }
- spin_unlock_irqrestore(lock, flags);
- if (free_self) {
- self->ordered_free(self);
-
- trace_btrfs_all_work_done(wq->fs_info, self);
- }
- }
- static void btrfs_work_helper(struct work_struct *normal_work)
- {
- struct btrfs_work *work = container_of(normal_work, struct btrfs_work,
- normal_work);
- struct btrfs_workqueue *wq = work->wq;
- int need_order = 0;
-
- if (work->ordered_func)
- need_order = 1;
- trace_btrfs_work_sched(work);
- thresh_exec_hook(wq);
- work->func(work);
- if (need_order) {
-
- smp_mb__before_atomic();
- set_bit(WORK_DONE_BIT, &work->flags);
- run_ordered_work(wq, work);
- } else {
-
- trace_btrfs_all_work_done(wq->fs_info, work);
- }
- }
- void btrfs_init_work(struct btrfs_work *work, btrfs_func_t func,
- btrfs_func_t ordered_func, btrfs_func_t ordered_free)
- {
- work->func = func;
- work->ordered_func = ordered_func;
- work->ordered_free = ordered_free;
- INIT_WORK(&work->normal_work, btrfs_work_helper);
- INIT_LIST_HEAD(&work->ordered_list);
- work->flags = 0;
- }
- void btrfs_queue_work(struct btrfs_workqueue *wq, struct btrfs_work *work)
- {
- unsigned long flags;
- work->wq = wq;
- thresh_queue_hook(wq);
- if (work->ordered_func) {
- spin_lock_irqsave(&wq->list_lock, flags);
- list_add_tail(&work->ordered_list, &wq->ordered_list);
- spin_unlock_irqrestore(&wq->list_lock, flags);
- }
- trace_btrfs_work_queued(work);
- queue_work(wq->normal_wq, &work->normal_work);
- }
- void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
- {
- if (!wq)
- return;
- destroy_workqueue(wq->normal_wq);
- trace_btrfs_workqueue_destroy(wq);
- kfree(wq);
- }
- void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int limit_active)
- {
- if (wq)
- wq->limit_active = limit_active;
- }
- void btrfs_flush_workqueue(struct btrfs_workqueue *wq)
- {
- flush_workqueue(wq->normal_wq);
- }
|