diff options
Diffstat (limited to 'mm/backing-dev.c')
-rw-r--r-- | mm/backing-dev.c | 449 |
1 files changed, 212 insertions, 237 deletions
diff --git a/mm/backing-dev.c b/mm/backing-dev.c index 123bcef13e51..eaa4a5bbe063 100644 --- a/mm/backing-dev.c +++ b/mm/backing-dev.c | |||
@@ -10,6 +10,7 @@ | |||
10 | #include <linux/module.h> | 10 | #include <linux/module.h> |
11 | #include <linux/writeback.h> | 11 | #include <linux/writeback.h> |
12 | #include <linux/device.h> | 12 | #include <linux/device.h> |
13 | #include <trace/events/writeback.h> | ||
13 | 14 | ||
14 | static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0); | 15 | static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0); |
15 | 16 | ||
@@ -49,8 +50,6 @@ static struct timer_list sync_supers_timer; | |||
49 | static int bdi_sync_supers(void *); | 50 | static int bdi_sync_supers(void *); |
50 | static void sync_supers_timer_fn(unsigned long); | 51 | static void sync_supers_timer_fn(unsigned long); |
51 | 52 | ||
52 | static void bdi_add_default_flusher_task(struct backing_dev_info *bdi); | ||
53 | |||
54 | #ifdef CONFIG_DEBUG_FS | 53 | #ifdef CONFIG_DEBUG_FS |
55 | #include <linux/debugfs.h> | 54 | #include <linux/debugfs.h> |
56 | #include <linux/seq_file.h> | 55 | #include <linux/seq_file.h> |
@@ -65,31 +64,25 @@ static void bdi_debug_init(void) | |||
65 | static int bdi_debug_stats_show(struct seq_file *m, void *v) | 64 | static int bdi_debug_stats_show(struct seq_file *m, void *v) |
66 | { | 65 | { |
67 | struct backing_dev_info *bdi = m->private; | 66 | struct backing_dev_info *bdi = m->private; |
68 | struct bdi_writeback *wb; | 67 | struct bdi_writeback *wb = &bdi->wb; |
69 | unsigned long background_thresh; | 68 | unsigned long background_thresh; |
70 | unsigned long dirty_thresh; | 69 | unsigned long dirty_thresh; |
71 | unsigned long bdi_thresh; | 70 | unsigned long bdi_thresh; |
72 | unsigned long nr_dirty, nr_io, nr_more_io, nr_wb; | 71 | unsigned long nr_dirty, nr_io, nr_more_io, nr_wb; |
73 | struct inode *inode; | 72 | struct inode *inode; |
74 | 73 | ||
75 | /* | ||
76 | * inode lock is enough here, the bdi->wb_list is protected by | ||
77 | * RCU on the reader side | ||
78 | */ | ||
79 | nr_wb = nr_dirty = nr_io = nr_more_io = 0; | 74 | nr_wb = nr_dirty = nr_io = nr_more_io = 0; |
80 | spin_lock(&inode_lock); | 75 | spin_lock(&inode_lock); |
81 | list_for_each_entry(wb, &bdi->wb_list, list) { | 76 | list_for_each_entry(inode, &wb->b_dirty, i_list) |
82 | nr_wb++; | 77 | nr_dirty++; |
83 | list_for_each_entry(inode, &wb->b_dirty, i_list) | 78 | list_for_each_entry(inode, &wb->b_io, i_list) |
84 | nr_dirty++; | 79 | nr_io++; |
85 | list_for_each_entry(inode, &wb->b_io, i_list) | 80 | list_for_each_entry(inode, &wb->b_more_io, i_list) |
86 | nr_io++; | 81 | nr_more_io++; |
87 | list_for_each_entry(inode, &wb->b_more_io, i_list) | ||
88 | nr_more_io++; | ||
89 | } | ||
90 | spin_unlock(&inode_lock); | 82 | spin_unlock(&inode_lock); |
91 | 83 | ||
92 | get_dirty_limits(&background_thresh, &dirty_thresh, &bdi_thresh, bdi); | 84 | global_dirty_limits(&background_thresh, &dirty_thresh); |
85 | bdi_thresh = bdi_dirty_limit(bdi, dirty_thresh); | ||
93 | 86 | ||
94 | #define K(x) ((x) << (PAGE_SHIFT - 10)) | 87 | #define K(x) ((x) << (PAGE_SHIFT - 10)) |
95 | seq_printf(m, | 88 | seq_printf(m, |
@@ -98,19 +91,16 @@ static int bdi_debug_stats_show(struct seq_file *m, void *v) | |||
98 | "BdiDirtyThresh: %8lu kB\n" | 91 | "BdiDirtyThresh: %8lu kB\n" |
99 | "DirtyThresh: %8lu kB\n" | 92 | "DirtyThresh: %8lu kB\n" |
100 | "BackgroundThresh: %8lu kB\n" | 93 | "BackgroundThresh: %8lu kB\n" |
101 | "WritebackThreads: %8lu\n" | ||
102 | "b_dirty: %8lu\n" | 94 | "b_dirty: %8lu\n" |
103 | "b_io: %8lu\n" | 95 | "b_io: %8lu\n" |
104 | "b_more_io: %8lu\n" | 96 | "b_more_io: %8lu\n" |
105 | "bdi_list: %8u\n" | 97 | "bdi_list: %8u\n" |
106 | "state: %8lx\n" | 98 | "state: %8lx\n", |
107 | "wb_list: %8u\n", | ||
108 | (unsigned long) K(bdi_stat(bdi, BDI_WRITEBACK)), | 99 | (unsigned long) K(bdi_stat(bdi, BDI_WRITEBACK)), |
109 | (unsigned long) K(bdi_stat(bdi, BDI_RECLAIMABLE)), | 100 | (unsigned long) K(bdi_stat(bdi, BDI_RECLAIMABLE)), |
110 | K(bdi_thresh), K(dirty_thresh), | 101 | K(bdi_thresh), K(dirty_thresh), |
111 | K(background_thresh), nr_wb, nr_dirty, nr_io, nr_more_io, | 102 | K(background_thresh), nr_dirty, nr_io, nr_more_io, |
112 | !list_empty(&bdi->bdi_list), bdi->state, | 103 | !list_empty(&bdi->bdi_list), bdi->state); |
113 | !list_empty(&bdi->wb_list)); | ||
114 | #undef K | 104 | #undef K |
115 | 105 | ||
116 | return 0; | 106 | return 0; |
@@ -247,7 +237,6 @@ static int __init default_bdi_init(void) | |||
247 | sync_supers_tsk = kthread_run(bdi_sync_supers, NULL, "sync_supers"); | 237 | sync_supers_tsk = kthread_run(bdi_sync_supers, NULL, "sync_supers"); |
248 | BUG_ON(IS_ERR(sync_supers_tsk)); | 238 | BUG_ON(IS_ERR(sync_supers_tsk)); |
249 | 239 | ||
250 | init_timer(&sync_supers_timer); | ||
251 | setup_timer(&sync_supers_timer, sync_supers_timer_fn, 0); | 240 | setup_timer(&sync_supers_timer, sync_supers_timer_fn, 0); |
252 | bdi_arm_supers_timer(); | 241 | bdi_arm_supers_timer(); |
253 | 242 | ||
@@ -259,77 +248,6 @@ static int __init default_bdi_init(void) | |||
259 | } | 248 | } |
260 | subsys_initcall(default_bdi_init); | 249 | subsys_initcall(default_bdi_init); |
261 | 250 | ||
262 | static void bdi_wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi) | ||
263 | { | ||
264 | memset(wb, 0, sizeof(*wb)); | ||
265 | |||
266 | wb->bdi = bdi; | ||
267 | wb->last_old_flush = jiffies; | ||
268 | INIT_LIST_HEAD(&wb->b_dirty); | ||
269 | INIT_LIST_HEAD(&wb->b_io); | ||
270 | INIT_LIST_HEAD(&wb->b_more_io); | ||
271 | } | ||
272 | |||
273 | static void bdi_task_init(struct backing_dev_info *bdi, | ||
274 | struct bdi_writeback *wb) | ||
275 | { | ||
276 | struct task_struct *tsk = current; | ||
277 | |||
278 | spin_lock(&bdi->wb_lock); | ||
279 | list_add_tail_rcu(&wb->list, &bdi->wb_list); | ||
280 | spin_unlock(&bdi->wb_lock); | ||
281 | |||
282 | tsk->flags |= PF_FLUSHER | PF_SWAPWRITE; | ||
283 | set_freezable(); | ||
284 | |||
285 | /* | ||
286 | * Our parent may run at a different priority, just set us to normal | ||
287 | */ | ||
288 | set_user_nice(tsk, 0); | ||
289 | } | ||
290 | |||
291 | static int bdi_start_fn(void *ptr) | ||
292 | { | ||
293 | struct bdi_writeback *wb = ptr; | ||
294 | struct backing_dev_info *bdi = wb->bdi; | ||
295 | int ret; | ||
296 | |||
297 | /* | ||
298 | * Add us to the active bdi_list | ||
299 | */ | ||
300 | spin_lock_bh(&bdi_lock); | ||
301 | list_add_rcu(&bdi->bdi_list, &bdi_list); | ||
302 | spin_unlock_bh(&bdi_lock); | ||
303 | |||
304 | bdi_task_init(bdi, wb); | ||
305 | |||
306 | /* | ||
307 | * Clear pending bit and wakeup anybody waiting to tear us down | ||
308 | */ | ||
309 | clear_bit(BDI_pending, &bdi->state); | ||
310 | smp_mb__after_clear_bit(); | ||
311 | wake_up_bit(&bdi->state, BDI_pending); | ||
312 | |||
313 | ret = bdi_writeback_task(wb); | ||
314 | |||
315 | /* | ||
316 | * Remove us from the list | ||
317 | */ | ||
318 | spin_lock(&bdi->wb_lock); | ||
319 | list_del_rcu(&wb->list); | ||
320 | spin_unlock(&bdi->wb_lock); | ||
321 | |||
322 | /* | ||
323 | * Flush any work that raced with us exiting. No new work | ||
324 | * will be added, since this bdi isn't discoverable anymore. | ||
325 | */ | ||
326 | if (!list_empty(&bdi->work_list)) | ||
327 | wb_do_writeback(wb, 1); | ||
328 | |||
329 | wb->task = NULL; | ||
330 | return ret; | ||
331 | } | ||
332 | |||
333 | int bdi_has_dirty_io(struct backing_dev_info *bdi) | 251 | int bdi_has_dirty_io(struct backing_dev_info *bdi) |
334 | { | 252 | { |
335 | return wb_has_dirty_io(&bdi->wb); | 253 | return wb_has_dirty_io(&bdi->wb); |
@@ -348,10 +266,10 @@ static void bdi_flush_io(struct backing_dev_info *bdi) | |||
348 | } | 266 | } |
349 | 267 | ||
350 | /* | 268 | /* |
351 | * kupdated() used to do this. We cannot do it from the bdi_forker_task() | 269 | * kupdated() used to do this. We cannot do it from the bdi_forker_thread() |
352 | * or we risk deadlocking on ->s_umount. The longer term solution would be | 270 | * or we risk deadlocking on ->s_umount. The longer term solution would be |
353 | * to implement sync_supers_bdi() or similar and simply do it from the | 271 | * to implement sync_supers_bdi() or similar and simply do it from the |
354 | * bdi writeback tasks individually. | 272 | * bdi writeback thread individually. |
355 | */ | 273 | */ |
356 | static int bdi_sync_supers(void *unused) | 274 | static int bdi_sync_supers(void *unused) |
357 | { | 275 | { |
@@ -387,144 +305,198 @@ static void sync_supers_timer_fn(unsigned long unused) | |||
387 | bdi_arm_supers_timer(); | 305 | bdi_arm_supers_timer(); |
388 | } | 306 | } |
389 | 307 | ||
390 | static int bdi_forker_task(void *ptr) | 308 | static void wakeup_timer_fn(unsigned long data) |
309 | { | ||
310 | struct backing_dev_info *bdi = (struct backing_dev_info *)data; | ||
311 | |||
312 | spin_lock_bh(&bdi->wb_lock); | ||
313 | if (bdi->wb.task) { | ||
314 | trace_writeback_wake_thread(bdi); | ||
315 | wake_up_process(bdi->wb.task); | ||
316 | } else { | ||
317 | /* | ||
318 | * When bdi tasks are inactive for long time, they are killed. | ||
319 | * In this case we have to wake-up the forker thread which | ||
320 | * should create and run the bdi thread. | ||
321 | */ | ||
322 | trace_writeback_wake_forker_thread(bdi); | ||
323 | wake_up_process(default_backing_dev_info.wb.task); | ||
324 | } | ||
325 | spin_unlock_bh(&bdi->wb_lock); | ||
326 | } | ||
327 | |||
328 | /* | ||
329 | * This function is used when the first inode for this bdi is marked dirty. It | ||
330 | * wakes-up the corresponding bdi thread which should then take care of the | ||
331 | * periodic background write-out of dirty inodes. Since the write-out would | ||
332 | * starts only 'dirty_writeback_interval' centisecs from now anyway, we just | ||
333 | * set up a timer which wakes the bdi thread up later. | ||
334 | * | ||
335 | * Note, we wouldn't bother setting up the timer, but this function is on the | ||
336 | * fast-path (used by '__mark_inode_dirty()'), so we save few context switches | ||
337 | * by delaying the wake-up. | ||
338 | */ | ||
339 | void bdi_wakeup_thread_delayed(struct backing_dev_info *bdi) | ||
340 | { | ||
341 | unsigned long timeout; | ||
342 | |||
343 | timeout = msecs_to_jiffies(dirty_writeback_interval * 10); | ||
344 | mod_timer(&bdi->wb.wakeup_timer, jiffies + timeout); | ||
345 | } | ||
346 | |||
347 | /* | ||
348 | * Calculate the longest interval (jiffies) bdi threads are allowed to be | ||
349 | * inactive. | ||
350 | */ | ||
351 | static unsigned long bdi_longest_inactive(void) | ||
352 | { | ||
353 | unsigned long interval; | ||
354 | |||
355 | interval = msecs_to_jiffies(dirty_writeback_interval * 10); | ||
356 | return max(5UL * 60 * HZ, interval); | ||
357 | } | ||
358 | |||
359 | static int bdi_forker_thread(void *ptr) | ||
391 | { | 360 | { |
392 | struct bdi_writeback *me = ptr; | 361 | struct bdi_writeback *me = ptr; |
393 | 362 | ||
394 | bdi_task_init(me->bdi, me); | 363 | current->flags |= PF_FLUSHER | PF_SWAPWRITE; |
364 | set_freezable(); | ||
365 | |||
366 | /* | ||
367 | * Our parent may run at a different priority, just set us to normal | ||
368 | */ | ||
369 | set_user_nice(current, 0); | ||
395 | 370 | ||
396 | for (;;) { | 371 | for (;;) { |
397 | struct backing_dev_info *bdi, *tmp; | 372 | struct task_struct *task = NULL; |
398 | struct bdi_writeback *wb; | 373 | struct backing_dev_info *bdi; |
374 | enum { | ||
375 | NO_ACTION, /* Nothing to do */ | ||
376 | FORK_THREAD, /* Fork bdi thread */ | ||
377 | KILL_THREAD, /* Kill inactive bdi thread */ | ||
378 | } action = NO_ACTION; | ||
399 | 379 | ||
400 | /* | 380 | /* |
401 | * Temporary measure, we want to make sure we don't see | 381 | * Temporary measure, we want to make sure we don't see |
402 | * dirty data on the default backing_dev_info | 382 | * dirty data on the default backing_dev_info |
403 | */ | 383 | */ |
404 | if (wb_has_dirty_io(me) || !list_empty(&me->bdi->work_list)) | 384 | if (wb_has_dirty_io(me) || !list_empty(&me->bdi->work_list)) { |
385 | del_timer(&me->wakeup_timer); | ||
405 | wb_do_writeback(me, 0); | 386 | wb_do_writeback(me, 0); |
387 | } | ||
406 | 388 | ||
407 | spin_lock_bh(&bdi_lock); | 389 | spin_lock_bh(&bdi_lock); |
390 | set_current_state(TASK_INTERRUPTIBLE); | ||
408 | 391 | ||
409 | /* | 392 | list_for_each_entry(bdi, &bdi_list, bdi_list) { |
410 | * Check if any existing bdi's have dirty data without | 393 | bool have_dirty_io; |
411 | * a thread registered. If so, set that up. | 394 | |
412 | */ | 395 | if (!bdi_cap_writeback_dirty(bdi) || |
413 | list_for_each_entry_safe(bdi, tmp, &bdi_list, bdi_list) { | 396 | bdi_cap_flush_forker(bdi)) |
414 | if (bdi->wb.task) | ||
415 | continue; | ||
416 | if (list_empty(&bdi->work_list) && | ||
417 | !bdi_has_dirty_io(bdi)) | ||
418 | continue; | 397 | continue; |
419 | 398 | ||
420 | bdi_add_default_flusher_task(bdi); | 399 | WARN(!test_bit(BDI_registered, &bdi->state), |
421 | } | 400 | "bdi %p/%s is not registered!\n", bdi, bdi->name); |
422 | 401 | ||
423 | set_current_state(TASK_INTERRUPTIBLE); | 402 | have_dirty_io = !list_empty(&bdi->work_list) || |
403 | wb_has_dirty_io(&bdi->wb); | ||
424 | 404 | ||
425 | if (list_empty(&bdi_pending_list)) { | 405 | /* |
426 | unsigned long wait; | 406 | * If the bdi has work to do, but the thread does not |
407 | * exist - create it. | ||
408 | */ | ||
409 | if (!bdi->wb.task && have_dirty_io) { | ||
410 | /* | ||
411 | * Set the pending bit - if someone will try to | ||
412 | * unregister this bdi - it'll wait on this bit. | ||
413 | */ | ||
414 | set_bit(BDI_pending, &bdi->state); | ||
415 | action = FORK_THREAD; | ||
416 | break; | ||
417 | } | ||
418 | |||
419 | spin_lock(&bdi->wb_lock); | ||
420 | |||
421 | /* | ||
422 | * If there is no work to do and the bdi thread was | ||
423 | * inactive long enough - kill it. The wb_lock is taken | ||
424 | * to make sure no-one adds more work to this bdi and | ||
425 | * wakes the bdi thread up. | ||
426 | */ | ||
427 | if (bdi->wb.task && !have_dirty_io && | ||
428 | time_after(jiffies, bdi->wb.last_active + | ||
429 | bdi_longest_inactive())) { | ||
430 | task = bdi->wb.task; | ||
431 | bdi->wb.task = NULL; | ||
432 | spin_unlock(&bdi->wb_lock); | ||
433 | set_bit(BDI_pending, &bdi->state); | ||
434 | action = KILL_THREAD; | ||
435 | break; | ||
436 | } | ||
437 | spin_unlock(&bdi->wb_lock); | ||
438 | } | ||
439 | spin_unlock_bh(&bdi_lock); | ||
427 | 440 | ||
428 | spin_unlock_bh(&bdi_lock); | 441 | /* Keep working if default bdi still has things to do */ |
429 | wait = msecs_to_jiffies(dirty_writeback_interval * 10); | 442 | if (!list_empty(&me->bdi->work_list)) |
430 | if (wait) | 443 | __set_current_state(TASK_RUNNING); |
431 | schedule_timeout(wait); | 444 | |
445 | switch (action) { | ||
446 | case FORK_THREAD: | ||
447 | __set_current_state(TASK_RUNNING); | ||
448 | task = kthread_run(bdi_writeback_thread, &bdi->wb, "flush-%s", | ||
449 | dev_name(bdi->dev)); | ||
450 | if (IS_ERR(task)) { | ||
451 | /* | ||
452 | * If thread creation fails, force writeout of | ||
453 | * the bdi from the thread. | ||
454 | */ | ||
455 | bdi_flush_io(bdi); | ||
456 | } else { | ||
457 | /* | ||
458 | * The spinlock makes sure we do not lose | ||
459 | * wake-ups when racing with 'bdi_queue_work()'. | ||
460 | */ | ||
461 | spin_lock_bh(&bdi->wb_lock); | ||
462 | bdi->wb.task = task; | ||
463 | spin_unlock_bh(&bdi->wb_lock); | ||
464 | } | ||
465 | break; | ||
466 | |||
467 | case KILL_THREAD: | ||
468 | __set_current_state(TASK_RUNNING); | ||
469 | kthread_stop(task); | ||
470 | break; | ||
471 | |||
472 | case NO_ACTION: | ||
473 | if (!wb_has_dirty_io(me) || !dirty_writeback_interval) | ||
474 | /* | ||
475 | * There are no dirty data. The only thing we | ||
476 | * should now care about is checking for | ||
477 | * inactive bdi threads and killing them. Thus, | ||
478 | * let's sleep for longer time, save energy and | ||
479 | * be friendly for battery-driven devices. | ||
480 | */ | ||
481 | schedule_timeout(bdi_longest_inactive()); | ||
432 | else | 482 | else |
433 | schedule(); | 483 | schedule_timeout(msecs_to_jiffies(dirty_writeback_interval * 10)); |
434 | try_to_freeze(); | 484 | try_to_freeze(); |
485 | /* Back to the main loop */ | ||
435 | continue; | 486 | continue; |
436 | } | 487 | } |
437 | 488 | ||
438 | __set_current_state(TASK_RUNNING); | ||
439 | |||
440 | /* | ||
441 | * This is our real job - check for pending entries in | ||
442 | * bdi_pending_list, and create the tasks that got added | ||
443 | */ | ||
444 | bdi = list_entry(bdi_pending_list.next, struct backing_dev_info, | ||
445 | bdi_list); | ||
446 | list_del_init(&bdi->bdi_list); | ||
447 | spin_unlock_bh(&bdi_lock); | ||
448 | |||
449 | wb = &bdi->wb; | ||
450 | wb->task = kthread_run(bdi_start_fn, wb, "flush-%s", | ||
451 | dev_name(bdi->dev)); | ||
452 | /* | 489 | /* |
453 | * If task creation fails, then readd the bdi to | 490 | * Clear pending bit and wakeup anybody waiting to tear us down. |
454 | * the pending list and force writeout of the bdi | ||
455 | * from this forker thread. That will free some memory | ||
456 | * and we can try again. | ||
457 | */ | 491 | */ |
458 | if (IS_ERR(wb->task)) { | 492 | clear_bit(BDI_pending, &bdi->state); |
459 | wb->task = NULL; | 493 | smp_mb__after_clear_bit(); |
460 | 494 | wake_up_bit(&bdi->state, BDI_pending); | |
461 | /* | ||
462 | * Add this 'bdi' to the back, so we get | ||
463 | * a chance to flush other bdi's to free | ||
464 | * memory. | ||
465 | */ | ||
466 | spin_lock_bh(&bdi_lock); | ||
467 | list_add_tail(&bdi->bdi_list, &bdi_pending_list); | ||
468 | spin_unlock_bh(&bdi_lock); | ||
469 | |||
470 | bdi_flush_io(bdi); | ||
471 | } | ||
472 | } | 495 | } |
473 | 496 | ||
474 | return 0; | 497 | return 0; |
475 | } | 498 | } |
476 | 499 | ||
477 | static void bdi_add_to_pending(struct rcu_head *head) | ||
478 | { | ||
479 | struct backing_dev_info *bdi; | ||
480 | |||
481 | bdi = container_of(head, struct backing_dev_info, rcu_head); | ||
482 | INIT_LIST_HEAD(&bdi->bdi_list); | ||
483 | |||
484 | spin_lock(&bdi_lock); | ||
485 | list_add_tail(&bdi->bdi_list, &bdi_pending_list); | ||
486 | spin_unlock(&bdi_lock); | ||
487 | |||
488 | /* | ||
489 | * We are now on the pending list, wake up bdi_forker_task() | ||
490 | * to finish the job and add us back to the active bdi_list | ||
491 | */ | ||
492 | wake_up_process(default_backing_dev_info.wb.task); | ||
493 | } | ||
494 | |||
495 | /* | ||
496 | * Add the default flusher task that gets created for any bdi | ||
497 | * that has dirty data pending writeout | ||
498 | */ | ||
499 | void static bdi_add_default_flusher_task(struct backing_dev_info *bdi) | ||
500 | { | ||
501 | if (!bdi_cap_writeback_dirty(bdi)) | ||
502 | return; | ||
503 | |||
504 | if (WARN_ON(!test_bit(BDI_registered, &bdi->state))) { | ||
505 | printk(KERN_ERR "bdi %p/%s is not registered!\n", | ||
506 | bdi, bdi->name); | ||
507 | return; | ||
508 | } | ||
509 | |||
510 | /* | ||
511 | * Check with the helper whether to proceed adding a task. Will only | ||
512 | * abort if we two or more simultanous calls to | ||
513 | * bdi_add_default_flusher_task() occured, further additions will block | ||
514 | * waiting for previous additions to finish. | ||
515 | */ | ||
516 | if (!test_and_set_bit(BDI_pending, &bdi->state)) { | ||
517 | list_del_rcu(&bdi->bdi_list); | ||
518 | |||
519 | /* | ||
520 | * We must wait for the current RCU period to end before | ||
521 | * moving to the pending list. So schedule that operation | ||
522 | * from an RCU callback. | ||
523 | */ | ||
524 | call_rcu(&bdi->rcu_head, bdi_add_to_pending); | ||
525 | } | ||
526 | } | ||
527 | |||
528 | /* | 500 | /* |
529 | * Remove bdi from bdi_list, and ensure that it is no longer visible | 501 | * Remove bdi from bdi_list, and ensure that it is no longer visible |
530 | */ | 502 | */ |
@@ -541,23 +513,16 @@ int bdi_register(struct backing_dev_info *bdi, struct device *parent, | |||
541 | const char *fmt, ...) | 513 | const char *fmt, ...) |
542 | { | 514 | { |
543 | va_list args; | 515 | va_list args; |
544 | int ret = 0; | ||
545 | struct device *dev; | 516 | struct device *dev; |
546 | 517 | ||
547 | if (bdi->dev) /* The driver needs to use separate queues per device */ | 518 | if (bdi->dev) /* The driver needs to use separate queues per device */ |
548 | goto exit; | 519 | return 0; |
549 | 520 | ||
550 | va_start(args, fmt); | 521 | va_start(args, fmt); |
551 | dev = device_create_vargs(bdi_class, parent, MKDEV(0, 0), bdi, fmt, args); | 522 | dev = device_create_vargs(bdi_class, parent, MKDEV(0, 0), bdi, fmt, args); |
552 | va_end(args); | 523 | va_end(args); |
553 | if (IS_ERR(dev)) { | 524 | if (IS_ERR(dev)) |
554 | ret = PTR_ERR(dev); | 525 | return PTR_ERR(dev); |
555 | goto exit; | ||
556 | } | ||
557 | |||
558 | spin_lock_bh(&bdi_lock); | ||
559 | list_add_tail_rcu(&bdi->bdi_list, &bdi_list); | ||
560 | spin_unlock_bh(&bdi_lock); | ||
561 | 526 | ||
562 | bdi->dev = dev; | 527 | bdi->dev = dev; |
563 | 528 | ||
@@ -569,21 +534,21 @@ int bdi_register(struct backing_dev_info *bdi, struct device *parent, | |||
569 | if (bdi_cap_flush_forker(bdi)) { | 534 | if (bdi_cap_flush_forker(bdi)) { |
570 | struct bdi_writeback *wb = &bdi->wb; | 535 | struct bdi_writeback *wb = &bdi->wb; |
571 | 536 | ||
572 | wb->task = kthread_run(bdi_forker_task, wb, "bdi-%s", | 537 | wb->task = kthread_run(bdi_forker_thread, wb, "bdi-%s", |
573 | dev_name(dev)); | 538 | dev_name(dev)); |
574 | if (IS_ERR(wb->task)) { | 539 | if (IS_ERR(wb->task)) |
575 | wb->task = NULL; | 540 | return PTR_ERR(wb->task); |
576 | ret = -ENOMEM; | ||
577 | |||
578 | bdi_remove_from_list(bdi); | ||
579 | goto exit; | ||
580 | } | ||
581 | } | 541 | } |
582 | 542 | ||
583 | bdi_debug_register(bdi, dev_name(dev)); | 543 | bdi_debug_register(bdi, dev_name(dev)); |
584 | set_bit(BDI_registered, &bdi->state); | 544 | set_bit(BDI_registered, &bdi->state); |
585 | exit: | 545 | |
586 | return ret; | 546 | spin_lock_bh(&bdi_lock); |
547 | list_add_tail_rcu(&bdi->bdi_list, &bdi_list); | ||
548 | spin_unlock_bh(&bdi_lock); | ||
549 | |||
550 | trace_writeback_bdi_register(bdi); | ||
551 | return 0; | ||
587 | } | 552 | } |
588 | EXPORT_SYMBOL(bdi_register); | 553 | EXPORT_SYMBOL(bdi_register); |
589 | 554 | ||
@@ -598,31 +563,29 @@ EXPORT_SYMBOL(bdi_register_dev); | |||
598 | */ | 563 | */ |
599 | static void bdi_wb_shutdown(struct backing_dev_info *bdi) | 564 | static void bdi_wb_shutdown(struct backing_dev_info *bdi) |
600 | { | 565 | { |
601 | struct bdi_writeback *wb; | ||
602 | |||
603 | if (!bdi_cap_writeback_dirty(bdi)) | 566 | if (!bdi_cap_writeback_dirty(bdi)) |
604 | return; | 567 | return; |
605 | 568 | ||
606 | /* | 569 | /* |
607 | * If setup is pending, wait for that to complete first | 570 | * Make sure nobody finds us on the bdi_list anymore |
608 | */ | 571 | */ |
609 | wait_on_bit(&bdi->state, BDI_pending, bdi_sched_wait, | 572 | bdi_remove_from_list(bdi); |
610 | TASK_UNINTERRUPTIBLE); | ||
611 | 573 | ||
612 | /* | 574 | /* |
613 | * Make sure nobody finds us on the bdi_list anymore | 575 | * If setup is pending, wait for that to complete first |
614 | */ | 576 | */ |
615 | bdi_remove_from_list(bdi); | 577 | wait_on_bit(&bdi->state, BDI_pending, bdi_sched_wait, |
578 | TASK_UNINTERRUPTIBLE); | ||
616 | 579 | ||
617 | /* | 580 | /* |
618 | * Finally, kill the kernel threads. We don't need to be RCU | 581 | * Finally, kill the kernel thread. We don't need to be RCU |
619 | * safe anymore, since the bdi is gone from visibility. Force | 582 | * safe anymore, since the bdi is gone from visibility. Force |
620 | * unfreeze of the thread before calling kthread_stop(), otherwise | 583 | * unfreeze of the thread before calling kthread_stop(), otherwise |
621 | * it would never exet if it is currently stuck in the refrigerator. | 584 | * it would never exet if it is currently stuck in the refrigerator. |
622 | */ | 585 | */ |
623 | list_for_each_entry(wb, &bdi->wb_list, list) { | 586 | if (bdi->wb.task) { |
624 | thaw_process(wb->task); | 587 | thaw_process(bdi->wb.task); |
625 | kthread_stop(wb->task); | 588 | kthread_stop(bdi->wb.task); |
626 | } | 589 | } |
627 | } | 590 | } |
628 | 591 | ||
@@ -644,7 +607,9 @@ static void bdi_prune_sb(struct backing_dev_info *bdi) | |||
644 | void bdi_unregister(struct backing_dev_info *bdi) | 607 | void bdi_unregister(struct backing_dev_info *bdi) |
645 | { | 608 | { |
646 | if (bdi->dev) { | 609 | if (bdi->dev) { |
610 | trace_writeback_bdi_unregister(bdi); | ||
647 | bdi_prune_sb(bdi); | 611 | bdi_prune_sb(bdi); |
612 | del_timer_sync(&bdi->wb.wakeup_timer); | ||
648 | 613 | ||
649 | if (!bdi_cap_flush_forker(bdi)) | 614 | if (!bdi_cap_flush_forker(bdi)) |
650 | bdi_wb_shutdown(bdi); | 615 | bdi_wb_shutdown(bdi); |
@@ -655,6 +620,18 @@ void bdi_unregister(struct backing_dev_info *bdi) | |||
655 | } | 620 | } |
656 | EXPORT_SYMBOL(bdi_unregister); | 621 | EXPORT_SYMBOL(bdi_unregister); |
657 | 622 | ||
623 | static void bdi_wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi) | ||
624 | { | ||
625 | memset(wb, 0, sizeof(*wb)); | ||
626 | |||
627 | wb->bdi = bdi; | ||
628 | wb->last_old_flush = jiffies; | ||
629 | INIT_LIST_HEAD(&wb->b_dirty); | ||
630 | INIT_LIST_HEAD(&wb->b_io); | ||
631 | INIT_LIST_HEAD(&wb->b_more_io); | ||
632 | setup_timer(&wb->wakeup_timer, wakeup_timer_fn, (unsigned long)bdi); | ||
633 | } | ||
634 | |||
658 | int bdi_init(struct backing_dev_info *bdi) | 635 | int bdi_init(struct backing_dev_info *bdi) |
659 | { | 636 | { |
660 | int i, err; | 637 | int i, err; |
@@ -665,9 +642,7 @@ int bdi_init(struct backing_dev_info *bdi) | |||
665 | bdi->max_ratio = 100; | 642 | bdi->max_ratio = 100; |
666 | bdi->max_prop_frac = PROP_FRAC_BASE; | 643 | bdi->max_prop_frac = PROP_FRAC_BASE; |
667 | spin_lock_init(&bdi->wb_lock); | 644 | spin_lock_init(&bdi->wb_lock); |
668 | INIT_RCU_HEAD(&bdi->rcu_head); | ||
669 | INIT_LIST_HEAD(&bdi->bdi_list); | 645 | INIT_LIST_HEAD(&bdi->bdi_list); |
670 | INIT_LIST_HEAD(&bdi->wb_list); | ||
671 | INIT_LIST_HEAD(&bdi->work_list); | 646 | INIT_LIST_HEAD(&bdi->work_list); |
672 | 647 | ||
673 | bdi_wb_init(&bdi->wb, bdi); | 648 | bdi_wb_init(&bdi->wb, bdi); |