async-thread.c 8.2 KB
Newer Older
1 2
/*
 * Copyright (C) 2007 Oracle.  All rights reserved.
3
 * Copyright (C) 2014 Fujitsu.  All rights reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#include <linux/kthread.h>
21
#include <linux/slab.h>
22 23
#include <linux/list.h>
#include <linux/spinlock.h>
24
#include <linux/freezer.h>
25
#include <linux/workqueue.h>
26
#include "async-thread.h"
27
#include "ctree.h"
28

Q
Qu Wenruo 已提交
29 30 31
#define WORK_DONE_BIT 0
#define WORK_ORDER_DONE_BIT 1
#define WORK_HIGH_PRIO_BIT 2
C
Chris Mason 已提交
32

33 34 35
#define NO_THRESHOLD (-1)
#define DFT_THRESHOLD (32)

36
struct __btrfs_workqueue {
37 38 39 40 41 42
	struct workqueue_struct *normal_wq;
	/* List head pointing to ordered work list */
	struct list_head ordered_list;

	/* Spinlock for ordered_list */
	spinlock_t list_lock;
43 44 45 46 47 48 49 50

	/* Thresholding related variants */
	atomic_t pending;
	int max_active;
	int current_max;
	int thresh;
	unsigned int count;
	spinlock_t thres_lock;
51 52
};

53 54 55
struct btrfs_workqueue {
	struct __btrfs_workqueue *normal;
	struct __btrfs_workqueue *high;
56 57
};

58
static inline struct __btrfs_workqueue
59 60
*__btrfs_alloc_workqueue(const char *name, int flags, int max_active,
			 int thresh)
61
{
62
	struct __btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS);
63 64 65 66

	if (unlikely(!ret))
		return NULL;

67 68 69 70 71 72 73 74 75 76 77 78 79
	ret->max_active = max_active;
	atomic_set(&ret->pending, 0);
	if (thresh == 0)
		thresh = DFT_THRESHOLD;
	/* For low threshold, disabling threshold is a better choice */
	if (thresh < DFT_THRESHOLD) {
		ret->current_max = max_active;
		ret->thresh = NO_THRESHOLD;
	} else {
		ret->current_max = 1;
		ret->thresh = thresh;
	}

80 81
	if (flags & WQ_HIGHPRI)
		ret->normal_wq = alloc_workqueue("%s-%s-high", flags,
82 83
						 ret->max_active,
						 "btrfs", name);
84 85
	else
		ret->normal_wq = alloc_workqueue("%s-%s", flags,
86 87
						 ret->max_active, "btrfs",
						 name);
88 89 90 91 92 93 94
	if (unlikely(!ret->normal_wq)) {
		kfree(ret);
		return NULL;
	}

	INIT_LIST_HEAD(&ret->ordered_list);
	spin_lock_init(&ret->list_lock);
95
	spin_lock_init(&ret->thres_lock);
96
	trace_btrfs_workqueue_alloc(ret, name, flags & WQ_HIGHPRI);
97 98 99 100
	return ret;
}

static inline void
101
__btrfs_destroy_workqueue(struct __btrfs_workqueue *wq);
102

103
struct btrfs_workqueue *btrfs_alloc_workqueue(const char *name,
104 105 106
					      int flags,
					      int max_active,
					      int thresh)
107
{
108
	struct btrfs_workqueue *ret = kzalloc(sizeof(*ret), GFP_NOFS);
109 110 111 112

	if (unlikely(!ret))
		return NULL;

113
	ret->normal = __btrfs_alloc_workqueue(name, flags & ~WQ_HIGHPRI,
114
					      max_active, thresh);
115
	if (unlikely(!ret->normal)) {
116 117 118 119
		kfree(ret);
		return NULL;
	}

120
	if (flags & WQ_HIGHPRI) {
121 122
		ret->high = __btrfs_alloc_workqueue(name, flags, max_active,
						    thresh);
123 124 125 126 127 128
		if (unlikely(!ret->high)) {
			__btrfs_destroy_workqueue(ret->normal);
			kfree(ret);
			return NULL;
		}
	}
129 130 131
	return ret;
}

132 133 134 135 136
/*
 * Hook for threshold which will be called in btrfs_queue_work.
 * This hook WILL be called in IRQ handler context,
 * so workqueue_set_max_active MUST NOT be called in this hook
 */
137
static inline void thresh_queue_hook(struct __btrfs_workqueue *wq)
138 139 140 141 142 143 144 145 146 147 148
{
	if (wq->thresh == NO_THRESHOLD)
		return;
	atomic_inc(&wq->pending);
}

/*
 * Hook for threshold which will be called before executing the work,
 * This hook is called in kthread content.
 * So workqueue_set_max_active is called here.
 */
149
static inline void thresh_exec_hook(struct __btrfs_workqueue *wq)
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
{
	int new_max_active;
	long pending;
	int need_change = 0;

	if (wq->thresh == NO_THRESHOLD)
		return;

	atomic_dec(&wq->pending);
	spin_lock(&wq->thres_lock);
	/*
	 * Use wq->count to limit the calling frequency of
	 * workqueue_set_max_active.
	 */
	wq->count++;
	wq->count %= (wq->thresh / 4);
	if (!wq->count)
		goto  out;
	new_max_active = wq->current_max;

	/*
	 * pending may be changed later, but it's OK since we really
	 * don't need it so accurate to calculate new_max_active.
	 */
	pending = atomic_read(&wq->pending);
	if (pending > wq->thresh)
		new_max_active++;
	if (pending < wq->thresh / 2)
		new_max_active--;
	new_max_active = clamp_val(new_max_active, 1, wq->max_active);
	if (new_max_active != wq->current_max)  {
		need_change = 1;
		wq->current_max = new_max_active;
	}
out:
	spin_unlock(&wq->thres_lock);

	if (need_change) {
		workqueue_set_max_active(wq->normal_wq, wq->current_max);
	}
}

192
static void run_ordered_work(struct __btrfs_workqueue *wq)
193 194
{
	struct list_head *list = &wq->ordered_list;
195
	struct btrfs_work *work;
196 197 198 199 200 201 202
	spinlock_t *lock = &wq->list_lock;
	unsigned long flags;

	while (1) {
		spin_lock_irqsave(lock, flags);
		if (list_empty(list))
			break;
203
		work = list_entry(list->next, struct btrfs_work,
204 205 206 207 208 209 210 211 212 213 214 215
				  ordered_list);
		if (!test_bit(WORK_DONE_BIT, &work->flags))
			break;

		/*
		 * we are going to call the ordered done function, but
		 * we leave the work item on the list as a barrier so
		 * that later work items that are done don't have their
		 * functions called before this one returns
		 */
		if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags))
			break;
216
		trace_btrfs_ordered_sched(work);
217 218 219 220 221 222 223 224 225 226 227 228 229
		spin_unlock_irqrestore(lock, flags);
		work->ordered_func(work);

		/* now take the lock again and drop our item from the list */
		spin_lock_irqsave(lock, flags);
		list_del(&work->ordered_list);
		spin_unlock_irqrestore(lock, flags);

		/*
		 * we don't want to call the ordered free functions
		 * with the lock held though
		 */
		work->ordered_free(work);
230
		trace_btrfs_all_work_done(work);
231 232 233 234 235 236
	}
	spin_unlock_irqrestore(lock, flags);
}

static void normal_work_helper(struct work_struct *arg)
{
237 238
	struct btrfs_work *work;
	struct __btrfs_workqueue *wq;
239 240
	int need_order = 0;

241
	work = container_of(arg, struct btrfs_work, normal_work);
242 243 244 245 246 247 248 249 250 251 252 253
	/*
	 * We should not touch things inside work in the following cases:
	 * 1) after work->func() if it has no ordered_free
	 *    Since the struct is freed in work->func().
	 * 2) after setting WORK_DONE_BIT
	 *    The work may be freed in other threads almost instantly.
	 * So we save the needed things here.
	 */
	if (work->ordered_func)
		need_order = 1;
	wq = work->wq;

254
	trace_btrfs_work_sched(work);
255
	thresh_exec_hook(wq);
256 257 258 259 260
	work->func(work);
	if (need_order) {
		set_bit(WORK_DONE_BIT, &work->flags);
		run_ordered_work(wq);
	}
261 262
	if (!need_order)
		trace_btrfs_all_work_done(work);
263 264
}

265
void btrfs_init_work(struct btrfs_work *work,
266 267 268
		     btrfs_func_t func,
		     btrfs_func_t ordered_func,
		     btrfs_func_t ordered_free)
269 270 271 272 273 274 275 276 277
{
	work->func = func;
	work->ordered_func = ordered_func;
	work->ordered_free = ordered_free;
	INIT_WORK(&work->normal_work, normal_work_helper);
	INIT_LIST_HEAD(&work->ordered_list);
	work->flags = 0;
}

278 279
static inline void __btrfs_queue_work(struct __btrfs_workqueue *wq,
				      struct btrfs_work *work)
280 281 282 283
{
	unsigned long flags;

	work->wq = wq;
284
	thresh_queue_hook(wq);
285 286 287 288 289 290
	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);
	}
	queue_work(wq->normal_wq, &work->normal_work);
291
	trace_btrfs_work_queued(work);
292 293
}

294 295
void btrfs_queue_work(struct btrfs_workqueue *wq,
		      struct btrfs_work *work)
296
{
297
	struct __btrfs_workqueue *dest_wq;
298 299 300 301 302 303 304 305 306

	if (test_bit(WORK_HIGH_PRIO_BIT, &work->flags) && wq->high)
		dest_wq = wq->high;
	else
		dest_wq = wq->normal;
	__btrfs_queue_work(dest_wq, work);
}

static inline void
307
__btrfs_destroy_workqueue(struct __btrfs_workqueue *wq)
308 309
{
	destroy_workqueue(wq->normal_wq);
310
	trace_btrfs_workqueue_destroy(wq);
311 312 313
	kfree(wq);
}

314
void btrfs_destroy_workqueue(struct btrfs_workqueue *wq)
315 316 317 318 319 320
{
	if (!wq)
		return;
	if (wq->high)
		__btrfs_destroy_workqueue(wq->high);
	__btrfs_destroy_workqueue(wq->normal);
321
	kfree(wq);
322 323
}

324
void btrfs_workqueue_set_max(struct btrfs_workqueue *wq, int max)
325
{
326 327
	if (!wq)
		return;
328
	wq->normal->max_active = max;
329
	if (wq->high)
330
		wq->high->max_active = max;
331 332
}

333
void btrfs_set_work_high_priority(struct btrfs_work *work)
334 335
{
	set_bit(WORK_HIGH_PRIO_BIT, &work->flags);
336
}