Skip to content

Commit 080dcec

Browse files
Artem BityutskiyJens Axboe
authored andcommitted
writeback: simplify bdi code a little
This patch simplifies bdi code a little by removing the 'pending_list' which is redundant. Indeed, currently the forker thread ('bdi_forker_thread()') is working like this: 1. In a loop, fetch all bdi's which have works but have no writeback thread and move them to the 'pending_list'. 2. If the list is empty, sleep for 5 sec. 3. Otherwise, take one bdi from the list, fork the writeback thread for this bdi, and repeat the loop. IOW, it first moves everything to the 'pending_list', then process only one element, and so on. This patch simplifies the algorithm, which is now as follows. 1. Find the first bdi which has a work and remove it from the global list of bdi's (bdi_list). 2. If there was not such bdi, sleep 5 sec. 3. Fork the writeback thread for this bdi and repeat the loop. IOW, now we find the first bdi to process, process it, and so on. This is simpler and involves less lists. The bonus now is that we can get rid of a couple of functions, as well as remove complications which involve 'rcu_call()' and 'bdi->rcu_head'. This patch also makes sure we use 'list_add_tail_rcu()', instead of plain 'list_add_tail()', but this piece of code is going to be removed in the next patch anyway. Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
1 parent 297252c commit 080dcec

2 files changed

Lines changed: 18 additions & 65 deletions

File tree

‎include/linux/backing-dev.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ struct bdi_writeback {
5858

5959
struct backing_dev_info {
6060
struct list_head bdi_list;
61-
struct rcu_head rcu_head;
6261
unsigned long ra_pages; /* max readahead in PAGE_CACHE_SIZE units */
6362
unsigned long state; /* Always use atomic bitops on this */
6463
unsigned int capabilities; /* Device capabilities */

‎mm/backing-dev.c‎

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ static struct timer_list sync_supers_timer;
5050
static int bdi_sync_supers(void *);
5151
static void sync_supers_timer_fn(unsigned long);
5252

53-
static void bdi_add_default_flusher_thread(struct backing_dev_info *bdi);
54-
5553
#ifdef CONFIG_DEBUG_FS
5654
#include <linux/debugfs.h>
5755
#include <linux/seq_file.h>
@@ -331,6 +329,7 @@ static int bdi_forker_thread(void *ptr)
331329
set_user_nice(current, 0);
332330

333331
for (;;) {
332+
bool fork = false;
334333
struct task_struct *task;
335334
struct backing_dev_info *bdi, *tmp;
336335

@@ -349,23 +348,30 @@ static int bdi_forker_thread(void *ptr)
349348
* a thread registered. If so, set that up.
350349
*/
351350
list_for_each_entry_safe(bdi, tmp, &bdi_list, bdi_list) {
351+
if (!bdi_cap_writeback_dirty(bdi))
352+
continue;
352353
if (bdi->wb.task)
353354
continue;
354355
if (list_empty(&bdi->work_list) &&
355356
!bdi_has_dirty_io(bdi))
356357
continue;
357358

358-
bdi_add_default_flusher_thread(bdi);
359+
WARN(!test_bit(BDI_registered, &bdi->state),
360+
"bdi %p/%s is not registered!\n", bdi, bdi->name);
361+
362+
list_del_rcu(&bdi->bdi_list);
363+
fork = true;
364+
break;
359365
}
366+
spin_unlock_bh(&bdi_lock);
360367

361368
/* Keep working if default bdi still has things to do */
362369
if (!list_empty(&me->bdi->work_list))
363370
__set_current_state(TASK_RUNNING);
364371

365-
if (list_empty(&bdi_pending_list)) {
372+
if (!fork) {
366373
unsigned long wait;
367374

368-
spin_unlock_bh(&bdi_lock);
369375
wait = msecs_to_jiffies(dirty_writeback_interval * 10);
370376
if (wait)
371377
schedule_timeout(wait);
@@ -378,13 +384,13 @@ static int bdi_forker_thread(void *ptr)
378384
__set_current_state(TASK_RUNNING);
379385

380386
/*
381-
* This is our real job - check for pending entries in
382-
* bdi_pending_list, and create the threads that got added
387+
* Set the pending bit - if someone will try to unregister this
388+
* bdi - it'll wait on this bit.
383389
*/
384-
bdi = list_entry(bdi_pending_list.next, struct backing_dev_info,
385-
bdi_list);
386-
list_del_init(&bdi->bdi_list);
387-
spin_unlock_bh(&bdi_lock);
390+
set_bit(BDI_pending, &bdi->state);
391+
392+
/* Make sure no one uses the picked bdi */
393+
synchronize_rcu();
388394

389395
task = kthread_run(bdi_writeback_thread, &bdi->wb, "flush-%s",
390396
dev_name(bdi->dev));
@@ -397,7 +403,7 @@ static int bdi_forker_thread(void *ptr)
397403
* flush other bdi's to free memory.
398404
*/
399405
spin_lock_bh(&bdi_lock);
400-
list_add_tail(&bdi->bdi_list, &bdi_pending_list);
406+
list_add_tail_rcu(&bdi->bdi_list, &bdi_list);
401407
spin_unlock_bh(&bdi_lock);
402408

403409
bdi_flush_io(bdi);
@@ -408,57 +414,6 @@ static int bdi_forker_thread(void *ptr)
408414
return 0;
409415
}
410416

411-
static void bdi_add_to_pending(struct rcu_head *head)
412-
{
413-
struct backing_dev_info *bdi;
414-
415-
bdi = container_of(head, struct backing_dev_info, rcu_head);
416-
INIT_LIST_HEAD(&bdi->bdi_list);
417-
418-
spin_lock(&bdi_lock);
419-
list_add_tail(&bdi->bdi_list, &bdi_pending_list);
420-
spin_unlock(&bdi_lock);
421-
422-
/*
423-
* We are now on the pending list, wake up bdi_forker_task()
424-
* to finish the job and add us back to the active bdi_list
425-
*/
426-
wake_up_process(default_backing_dev_info.wb.task);
427-
}
428-
429-
/*
430-
* Add the default flusher thread that gets created for any bdi
431-
* that has dirty data pending writeout
432-
*/
433-
static void bdi_add_default_flusher_thread(struct backing_dev_info *bdi)
434-
{
435-
if (!bdi_cap_writeback_dirty(bdi))
436-
return;
437-
438-
if (WARN_ON(!test_bit(BDI_registered, &bdi->state))) {
439-
printk(KERN_ERR "bdi %p/%s is not registered!\n",
440-
bdi, bdi->name);
441-
return;
442-
}
443-
444-
/*
445-
* Check with the helper whether to proceed adding a thread. Will only
446-
* abort if we two or more simultanous calls to
447-
* bdi_add_default_flusher_thread() occured, further additions will
448-
* block waiting for previous additions to finish.
449-
*/
450-
if (!test_and_set_bit(BDI_pending, &bdi->state)) {
451-
list_del_rcu(&bdi->bdi_list);
452-
453-
/*
454-
* We must wait for the current RCU period to end before
455-
* moving to the pending list. So schedule that operation
456-
* from an RCU callback.
457-
*/
458-
call_rcu(&bdi->rcu_head, bdi_add_to_pending);
459-
}
460-
}
461-
462417
/*
463418
* Remove bdi from bdi_list, and ensure that it is no longer visible
464419
*/
@@ -599,7 +554,6 @@ int bdi_init(struct backing_dev_info *bdi)
599554
bdi->max_ratio = 100;
600555
bdi->max_prop_frac = PROP_FRAC_BASE;
601556
spin_lock_init(&bdi->wb_lock);
602-
INIT_RCU_HEAD(&bdi->rcu_head);
603557
INIT_LIST_HEAD(&bdi->bdi_list);
604558
INIT_LIST_HEAD(&bdi->work_list);
605559

0 commit comments

Comments
 (0)