1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/blkdev.h>
4 #include <linux/wait.h>
5 #include <linux/rbtree.h>
6 #include <linux/kthread.h>
7 #include <linux/backing-dev.h>
8 #include <linux/blk-cgroup.h>
9 #include <linux/freezer.h>
11 #include <linux/pagemap.h>
13 #include <linux/sched/mm.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <linux/writeback.h>
17 #include <linux/device.h>
18 #include <trace/events/writeback.h>
20 struct backing_dev_info noop_backing_dev_info;
21 EXPORT_SYMBOL_GPL(noop_backing_dev_info);
23 static const char *bdi_unknown_name = "(unknown)";
26 * bdi_lock protects bdi_tree and updates to bdi_list. bdi_list has RCU
27 * reader side locking.
29 DEFINE_SPINLOCK(bdi_lock);
30 static u64 bdi_id_cursor;
31 static struct rb_root bdi_tree = RB_ROOT;
34 /* bdi_wq serves all asynchronous writeback tasks */
35 struct workqueue_struct *bdi_wq;
37 #define K(x) ((x) << (PAGE_SHIFT - 10))
39 #ifdef CONFIG_DEBUG_FS
40 #include <linux/debugfs.h>
41 #include <linux/seq_file.h>
43 static struct dentry *bdi_debug_root;
45 static void bdi_debug_init(void)
47 bdi_debug_root = debugfs_create_dir("bdi", NULL);
50 static int bdi_debug_stats_show(struct seq_file *m, void *v)
52 struct backing_dev_info *bdi = m->private;
53 struct bdi_writeback *wb = &bdi->wb;
54 unsigned long background_thresh;
55 unsigned long dirty_thresh;
56 unsigned long wb_thresh;
57 unsigned long nr_dirty, nr_io, nr_more_io, nr_dirty_time;
60 nr_dirty = nr_io = nr_more_io = nr_dirty_time = 0;
61 spin_lock(&wb->list_lock);
62 list_for_each_entry(inode, &wb->b_dirty, i_io_list)
64 list_for_each_entry(inode, &wb->b_io, i_io_list)
66 list_for_each_entry(inode, &wb->b_more_io, i_io_list)
68 list_for_each_entry(inode, &wb->b_dirty_time, i_io_list)
69 if (inode->i_state & I_DIRTY_TIME)
71 spin_unlock(&wb->list_lock);
73 global_dirty_limits(&background_thresh, &dirty_thresh);
74 wb_thresh = wb_calc_thresh(wb, dirty_thresh);
77 "BdiWriteback: %10lu kB\n"
78 "BdiReclaimable: %10lu kB\n"
79 "BdiDirtyThresh: %10lu kB\n"
80 "DirtyThresh: %10lu kB\n"
81 "BackgroundThresh: %10lu kB\n"
82 "BdiDirtied: %10lu kB\n"
83 "BdiWritten: %10lu kB\n"
84 "BdiWriteBandwidth: %10lu kBps\n"
88 "b_dirty_time: %10lu\n"
91 (unsigned long) K(wb_stat(wb, WB_WRITEBACK)),
92 (unsigned long) K(wb_stat(wb, WB_RECLAIMABLE)),
96 (unsigned long) K(wb_stat(wb, WB_DIRTIED)),
97 (unsigned long) K(wb_stat(wb, WB_WRITTEN)),
98 (unsigned long) K(wb->write_bandwidth),
103 !list_empty(&bdi->bdi_list), bdi->wb.state);
107 DEFINE_SHOW_ATTRIBUTE(bdi_debug_stats);
109 static void bdi_debug_register(struct backing_dev_info *bdi, const char *name)
111 bdi->debug_dir = debugfs_create_dir(name, bdi_debug_root);
113 debugfs_create_file("stats", 0444, bdi->debug_dir, bdi,
114 &bdi_debug_stats_fops);
117 static void bdi_debug_unregister(struct backing_dev_info *bdi)
119 debugfs_remove_recursive(bdi->debug_dir);
122 static inline void bdi_debug_init(void)
125 static inline void bdi_debug_register(struct backing_dev_info *bdi,
129 static inline void bdi_debug_unregister(struct backing_dev_info *bdi)
134 static ssize_t read_ahead_kb_store(struct device *dev,
135 struct device_attribute *attr,
136 const char *buf, size_t count)
138 struct backing_dev_info *bdi = dev_get_drvdata(dev);
139 unsigned long read_ahead_kb;
142 ret = kstrtoul(buf, 10, &read_ahead_kb);
146 bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10);
151 #define BDI_SHOW(name, expr) \
152 static ssize_t name##_show(struct device *dev, \
153 struct device_attribute *attr, char *buf) \
155 struct backing_dev_info *bdi = dev_get_drvdata(dev); \
157 return sysfs_emit(buf, "%lld\n", (long long)expr); \
159 static DEVICE_ATTR_RW(name);
161 BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
163 static ssize_t min_ratio_store(struct device *dev,
164 struct device_attribute *attr, const char *buf, size_t count)
166 struct backing_dev_info *bdi = dev_get_drvdata(dev);
170 ret = kstrtouint(buf, 10, &ratio);
174 ret = bdi_set_min_ratio(bdi, ratio);
180 BDI_SHOW(min_ratio, bdi->min_ratio / BDI_RATIO_SCALE)
182 static ssize_t min_ratio_fine_store(struct device *dev,
183 struct device_attribute *attr, const char *buf, size_t count)
185 struct backing_dev_info *bdi = dev_get_drvdata(dev);
189 ret = kstrtouint(buf, 10, &ratio);
193 ret = bdi_set_min_ratio_no_scale(bdi, ratio);
199 BDI_SHOW(min_ratio_fine, bdi->min_ratio)
201 static ssize_t max_ratio_store(struct device *dev,
202 struct device_attribute *attr, const char *buf, size_t count)
204 struct backing_dev_info *bdi = dev_get_drvdata(dev);
208 ret = kstrtouint(buf, 10, &ratio);
212 ret = bdi_set_max_ratio(bdi, ratio);
218 BDI_SHOW(max_ratio, bdi->max_ratio / BDI_RATIO_SCALE)
220 static ssize_t max_ratio_fine_store(struct device *dev,
221 struct device_attribute *attr, const char *buf, size_t count)
223 struct backing_dev_info *bdi = dev_get_drvdata(dev);
227 ret = kstrtouint(buf, 10, &ratio);
231 ret = bdi_set_max_ratio_no_scale(bdi, ratio);
237 BDI_SHOW(max_ratio_fine, bdi->max_ratio)
239 static ssize_t min_bytes_show(struct device *dev,
240 struct device_attribute *attr,
243 struct backing_dev_info *bdi = dev_get_drvdata(dev);
245 return sysfs_emit(buf, "%llu\n", bdi_get_min_bytes(bdi));
248 static ssize_t min_bytes_store(struct device *dev,
249 struct device_attribute *attr, const char *buf, size_t count)
251 struct backing_dev_info *bdi = dev_get_drvdata(dev);
255 ret = kstrtoull(buf, 10, &bytes);
259 ret = bdi_set_min_bytes(bdi, bytes);
265 static DEVICE_ATTR_RW(min_bytes);
267 static ssize_t max_bytes_show(struct device *dev,
268 struct device_attribute *attr,
271 struct backing_dev_info *bdi = dev_get_drvdata(dev);
273 return sysfs_emit(buf, "%llu\n", bdi_get_max_bytes(bdi));
276 static ssize_t max_bytes_store(struct device *dev,
277 struct device_attribute *attr, const char *buf, size_t count)
279 struct backing_dev_info *bdi = dev_get_drvdata(dev);
283 ret = kstrtoull(buf, 10, &bytes);
287 ret = bdi_set_max_bytes(bdi, bytes);
293 static DEVICE_ATTR_RW(max_bytes);
295 static ssize_t stable_pages_required_show(struct device *dev,
296 struct device_attribute *attr,
300 "the stable_pages_required attribute has been removed. Use the stable_writes queue attribute instead.\n");
301 return sysfs_emit(buf, "%d\n", 0);
303 static DEVICE_ATTR_RO(stable_pages_required);
305 static ssize_t strict_limit_store(struct device *dev,
306 struct device_attribute *attr, const char *buf, size_t count)
308 struct backing_dev_info *bdi = dev_get_drvdata(dev);
309 unsigned int strict_limit;
312 ret = kstrtouint(buf, 10, &strict_limit);
316 ret = bdi_set_strict_limit(bdi, strict_limit);
323 static ssize_t strict_limit_show(struct device *dev,
324 struct device_attribute *attr, char *buf)
326 struct backing_dev_info *bdi = dev_get_drvdata(dev);
328 return sysfs_emit(buf, "%d\n",
329 !!(bdi->capabilities & BDI_CAP_STRICTLIMIT));
331 static DEVICE_ATTR_RW(strict_limit);
333 static struct attribute *bdi_dev_attrs[] = {
334 &dev_attr_read_ahead_kb.attr,
335 &dev_attr_min_ratio.attr,
336 &dev_attr_min_ratio_fine.attr,
337 &dev_attr_max_ratio.attr,
338 &dev_attr_max_ratio_fine.attr,
339 &dev_attr_min_bytes.attr,
340 &dev_attr_max_bytes.attr,
341 &dev_attr_stable_pages_required.attr,
342 &dev_attr_strict_limit.attr,
345 ATTRIBUTE_GROUPS(bdi_dev);
347 static const struct class bdi_class = {
349 .dev_groups = bdi_dev_groups,
352 static __init int bdi_class_init(void)
356 ret = class_register(&bdi_class);
364 postcore_initcall(bdi_class_init);
366 static int __init default_bdi_init(void)
368 bdi_wq = alloc_workqueue("writeback", WQ_MEM_RECLAIM | WQ_UNBOUND |
374 subsys_initcall(default_bdi_init);
377 * This function is used when the first inode for this wb is marked dirty. It
378 * wakes-up the corresponding bdi thread which should then take care of the
379 * periodic background write-out of dirty inodes. Since the write-out would
380 * starts only 'dirty_writeback_interval' centisecs from now anyway, we just
381 * set up a timer which wakes the bdi thread up later.
383 * Note, we wouldn't bother setting up the timer, but this function is on the
384 * fast-path (used by '__mark_inode_dirty()'), so we save few context switches
385 * by delaying the wake-up.
387 * We have to be careful not to postpone flush work if it is scheduled for
388 * earlier. Thus we use queue_delayed_work().
390 void wb_wakeup_delayed(struct bdi_writeback *wb)
392 unsigned long timeout;
394 timeout = msecs_to_jiffies(dirty_writeback_interval * 10);
395 spin_lock_irq(&wb->work_lock);
396 if (test_bit(WB_registered, &wb->state))
397 queue_delayed_work(bdi_wq, &wb->dwork, timeout);
398 spin_unlock_irq(&wb->work_lock);
401 static void wb_update_bandwidth_workfn(struct work_struct *work)
403 struct bdi_writeback *wb = container_of(to_delayed_work(work),
404 struct bdi_writeback, bw_dwork);
406 wb_update_bandwidth(wb);
410 * Initial write bandwidth: 100 MB/s
412 #define INIT_BW (100 << (20 - PAGE_SHIFT))
414 static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
419 memset(wb, 0, sizeof(*wb));
422 wb->last_old_flush = jiffies;
423 INIT_LIST_HEAD(&wb->b_dirty);
424 INIT_LIST_HEAD(&wb->b_io);
425 INIT_LIST_HEAD(&wb->b_more_io);
426 INIT_LIST_HEAD(&wb->b_dirty_time);
427 spin_lock_init(&wb->list_lock);
429 atomic_set(&wb->writeback_inodes, 0);
430 wb->bw_time_stamp = jiffies;
431 wb->balanced_dirty_ratelimit = INIT_BW;
432 wb->dirty_ratelimit = INIT_BW;
433 wb->write_bandwidth = INIT_BW;
434 wb->avg_write_bandwidth = INIT_BW;
436 spin_lock_init(&wb->work_lock);
437 INIT_LIST_HEAD(&wb->work_list);
438 INIT_DELAYED_WORK(&wb->dwork, wb_workfn);
439 INIT_DELAYED_WORK(&wb->bw_dwork, wb_update_bandwidth_workfn);
440 wb->dirty_sleep = jiffies;
442 err = fprop_local_init_percpu(&wb->completions, gfp);
446 for (i = 0; i < NR_WB_STAT_ITEMS; i++) {
447 err = percpu_counter_init(&wb->stat[i], 0, gfp);
449 goto out_destroy_stat;
456 percpu_counter_destroy(&wb->stat[i]);
457 fprop_local_destroy_percpu(&wb->completions);
461 static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb);
464 * Remove bdi from the global list and shutdown any threads we have running
466 static void wb_shutdown(struct bdi_writeback *wb)
468 /* Make sure nobody queues further work */
469 spin_lock_irq(&wb->work_lock);
470 if (!test_and_clear_bit(WB_registered, &wb->state)) {
471 spin_unlock_irq(&wb->work_lock);
474 spin_unlock_irq(&wb->work_lock);
476 cgwb_remove_from_bdi_list(wb);
478 * Drain work list and shutdown the delayed_work. !WB_registered
479 * tells wb_workfn() that @wb is dying and its work_list needs to
480 * be drained no matter what.
482 mod_delayed_work(bdi_wq, &wb->dwork, 0);
483 flush_delayed_work(&wb->dwork);
484 WARN_ON(!list_empty(&wb->work_list));
485 flush_delayed_work(&wb->bw_dwork);
488 static void wb_exit(struct bdi_writeback *wb)
492 WARN_ON(delayed_work_pending(&wb->dwork));
494 for (i = 0; i < NR_WB_STAT_ITEMS; i++)
495 percpu_counter_destroy(&wb->stat[i]);
497 fprop_local_destroy_percpu(&wb->completions);
500 #ifdef CONFIG_CGROUP_WRITEBACK
502 #include <linux/memcontrol.h>
505 * cgwb_lock protects bdi->cgwb_tree, blkcg->cgwb_list, offline_cgwbs and
506 * memcg->cgwb_list. bdi->cgwb_tree is also RCU protected.
508 static DEFINE_SPINLOCK(cgwb_lock);
509 static struct workqueue_struct *cgwb_release_wq;
511 static LIST_HEAD(offline_cgwbs);
512 static void cleanup_offline_cgwbs_workfn(struct work_struct *work);
513 static DECLARE_WORK(cleanup_offline_cgwbs_work, cleanup_offline_cgwbs_workfn);
515 static void cgwb_free_rcu(struct rcu_head *rcu_head)
517 struct bdi_writeback *wb = container_of(rcu_head,
518 struct bdi_writeback, rcu);
520 percpu_ref_exit(&wb->refcnt);
524 static void cgwb_release_workfn(struct work_struct *work)
526 struct bdi_writeback *wb = container_of(work, struct bdi_writeback,
528 struct backing_dev_info *bdi = wb->bdi;
530 mutex_lock(&wb->bdi->cgwb_release_mutex);
533 css_put(wb->memcg_css);
534 css_put(wb->blkcg_css);
535 mutex_unlock(&wb->bdi->cgwb_release_mutex);
537 /* triggers blkg destruction if no online users left */
538 blkcg_unpin_online(wb->blkcg_css);
540 fprop_local_destroy_percpu(&wb->memcg_completions);
542 spin_lock_irq(&cgwb_lock);
543 list_del(&wb->offline_node);
544 spin_unlock_irq(&cgwb_lock);
548 WARN_ON_ONCE(!list_empty(&wb->b_attached));
549 call_rcu(&wb->rcu, cgwb_free_rcu);
552 static void cgwb_release(struct percpu_ref *refcnt)
554 struct bdi_writeback *wb = container_of(refcnt, struct bdi_writeback,
556 queue_work(cgwb_release_wq, &wb->release_work);
559 static void cgwb_kill(struct bdi_writeback *wb)
561 lockdep_assert_held(&cgwb_lock);
563 WARN_ON(!radix_tree_delete(&wb->bdi->cgwb_tree, wb->memcg_css->id));
564 list_del(&wb->memcg_node);
565 list_del(&wb->blkcg_node);
566 list_add(&wb->offline_node, &offline_cgwbs);
567 percpu_ref_kill(&wb->refcnt);
570 static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb)
572 spin_lock_irq(&cgwb_lock);
573 list_del_rcu(&wb->bdi_node);
574 spin_unlock_irq(&cgwb_lock);
577 static int cgwb_create(struct backing_dev_info *bdi,
578 struct cgroup_subsys_state *memcg_css, gfp_t gfp)
580 struct mem_cgroup *memcg;
581 struct cgroup_subsys_state *blkcg_css;
582 struct list_head *memcg_cgwb_list, *blkcg_cgwb_list;
583 struct bdi_writeback *wb;
587 memcg = mem_cgroup_from_css(memcg_css);
588 blkcg_css = cgroup_get_e_css(memcg_css->cgroup, &io_cgrp_subsys);
589 memcg_cgwb_list = &memcg->cgwb_list;
590 blkcg_cgwb_list = blkcg_get_cgwb_list(blkcg_css);
592 /* look up again under lock and discard on blkcg mismatch */
593 spin_lock_irqsave(&cgwb_lock, flags);
594 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
595 if (wb && wb->blkcg_css != blkcg_css) {
599 spin_unlock_irqrestore(&cgwb_lock, flags);
603 /* need to create a new one */
604 wb = kmalloc(sizeof(*wb), gfp);
610 ret = wb_init(wb, bdi, gfp);
614 ret = percpu_ref_init(&wb->refcnt, cgwb_release, 0, gfp);
618 ret = fprop_local_init_percpu(&wb->memcg_completions, gfp);
622 wb->memcg_css = memcg_css;
623 wb->blkcg_css = blkcg_css;
624 INIT_LIST_HEAD(&wb->b_attached);
625 INIT_WORK(&wb->release_work, cgwb_release_workfn);
626 set_bit(WB_registered, &wb->state);
630 * The root wb determines the registered state of the whole bdi and
631 * memcg_cgwb_list and blkcg_cgwb_list's next pointers indicate
632 * whether they're still online. Don't link @wb if any is dead.
633 * See wb_memcg_offline() and wb_blkcg_offline().
636 spin_lock_irqsave(&cgwb_lock, flags);
637 if (test_bit(WB_registered, &bdi->wb.state) &&
638 blkcg_cgwb_list->next && memcg_cgwb_list->next) {
639 /* we might have raced another instance of this function */
640 ret = radix_tree_insert(&bdi->cgwb_tree, memcg_css->id, wb);
642 list_add_tail_rcu(&wb->bdi_node, &bdi->wb_list);
643 list_add(&wb->memcg_node, memcg_cgwb_list);
644 list_add(&wb->blkcg_node, blkcg_cgwb_list);
645 blkcg_pin_online(blkcg_css);
650 spin_unlock_irqrestore(&cgwb_lock, flags);
660 fprop_local_destroy_percpu(&wb->memcg_completions);
662 percpu_ref_exit(&wb->refcnt);
673 * wb_get_lookup - get wb for a given memcg
675 * @memcg_css: cgroup_subsys_state of the target memcg (must have positive ref)
677 * Try to get the wb for @memcg_css on @bdi. The returned wb has its
678 * refcount incremented.
680 * This function uses css_get() on @memcg_css and thus expects its refcnt
681 * to be positive on invocation. IOW, rcu_read_lock() protection on
682 * @memcg_css isn't enough. try_get it before calling this function.
684 * A wb is keyed by its associated memcg. As blkcg implicitly enables
685 * memcg on the default hierarchy, memcg association is guaranteed to be
686 * more specific (equal or descendant to the associated blkcg) and thus can
687 * identify both the memcg and blkcg associations.
689 * Because the blkcg associated with a memcg may change as blkcg is enabled
690 * and disabled closer to root in the hierarchy, each wb keeps track of
691 * both the memcg and blkcg associated with it and verifies the blkcg on
692 * each lookup. On mismatch, the existing wb is discarded and a new one is
695 struct bdi_writeback *wb_get_lookup(struct backing_dev_info *bdi,
696 struct cgroup_subsys_state *memcg_css)
698 struct bdi_writeback *wb;
700 if (!memcg_css->parent)
704 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
706 struct cgroup_subsys_state *blkcg_css;
708 /* see whether the blkcg association has changed */
709 blkcg_css = cgroup_get_e_css(memcg_css->cgroup, &io_cgrp_subsys);
710 if (unlikely(wb->blkcg_css != blkcg_css || !wb_tryget(wb)))
720 * wb_get_create - get wb for a given memcg, create if necessary
722 * @memcg_css: cgroup_subsys_state of the target memcg (must have positive ref)
723 * @gfp: allocation mask to use
725 * Try to get the wb for @memcg_css on @bdi. If it doesn't exist, try to
726 * create one. See wb_get_lookup() for more details.
728 struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
729 struct cgroup_subsys_state *memcg_css,
732 struct bdi_writeback *wb;
736 if (!memcg_css->parent)
740 wb = wb_get_lookup(bdi, memcg_css);
741 } while (!wb && !cgwb_create(bdi, memcg_css, gfp));
746 static int cgwb_bdi_init(struct backing_dev_info *bdi)
750 INIT_RADIX_TREE(&bdi->cgwb_tree, GFP_ATOMIC);
751 mutex_init(&bdi->cgwb_release_mutex);
752 init_rwsem(&bdi->wb_switch_rwsem);
754 ret = wb_init(&bdi->wb, bdi, GFP_KERNEL);
756 bdi->wb.memcg_css = &root_mem_cgroup->css;
757 bdi->wb.blkcg_css = blkcg_root_css;
762 static void cgwb_bdi_unregister(struct backing_dev_info *bdi)
764 struct radix_tree_iter iter;
766 struct bdi_writeback *wb;
768 WARN_ON(test_bit(WB_registered, &bdi->wb.state));
770 spin_lock_irq(&cgwb_lock);
771 radix_tree_for_each_slot(slot, &bdi->cgwb_tree, &iter, 0)
773 spin_unlock_irq(&cgwb_lock);
775 mutex_lock(&bdi->cgwb_release_mutex);
776 spin_lock_irq(&cgwb_lock);
777 while (!list_empty(&bdi->wb_list)) {
778 wb = list_first_entry(&bdi->wb_list, struct bdi_writeback,
780 spin_unlock_irq(&cgwb_lock);
782 spin_lock_irq(&cgwb_lock);
784 spin_unlock_irq(&cgwb_lock);
785 mutex_unlock(&bdi->cgwb_release_mutex);
789 * cleanup_offline_cgwbs_workfn - try to release dying cgwbs
791 * Try to release dying cgwbs by switching attached inodes to the nearest
792 * living ancestor's writeback. Processed wbs are placed at the end
793 * of the list to guarantee the forward progress.
795 static void cleanup_offline_cgwbs_workfn(struct work_struct *work)
797 struct bdi_writeback *wb;
798 LIST_HEAD(processed);
800 spin_lock_irq(&cgwb_lock);
802 while (!list_empty(&offline_cgwbs)) {
803 wb = list_first_entry(&offline_cgwbs, struct bdi_writeback,
805 list_move(&wb->offline_node, &processed);
808 * If wb is dirty, cleaning up the writeback by switching
809 * attached inodes will result in an effective removal of any
810 * bandwidth restrictions, which isn't the goal. Instead,
811 * it can be postponed until the next time, when all io
812 * will be likely completed. If in the meantime some inodes
813 * will get re-dirtied, they should be eventually switched to
816 if (wb_has_dirty_io(wb))
822 spin_unlock_irq(&cgwb_lock);
823 while (cleanup_offline_cgwb(wb))
825 spin_lock_irq(&cgwb_lock);
830 if (!list_empty(&processed))
831 list_splice_tail(&processed, &offline_cgwbs);
833 spin_unlock_irq(&cgwb_lock);
837 * wb_memcg_offline - kill all wb's associated with a memcg being offlined
838 * @memcg: memcg being offlined
840 * Also prevents creation of any new wb's associated with @memcg.
842 void wb_memcg_offline(struct mem_cgroup *memcg)
844 struct list_head *memcg_cgwb_list = &memcg->cgwb_list;
845 struct bdi_writeback *wb, *next;
847 spin_lock_irq(&cgwb_lock);
848 list_for_each_entry_safe(wb, next, memcg_cgwb_list, memcg_node)
850 memcg_cgwb_list->next = NULL; /* prevent new wb's */
851 spin_unlock_irq(&cgwb_lock);
853 queue_work(system_unbound_wq, &cleanup_offline_cgwbs_work);
857 * wb_blkcg_offline - kill all wb's associated with a blkcg being offlined
858 * @css: blkcg being offlined
860 * Also prevents creation of any new wb's associated with @blkcg.
862 void wb_blkcg_offline(struct cgroup_subsys_state *css)
864 struct bdi_writeback *wb, *next;
865 struct list_head *list = blkcg_get_cgwb_list(css);
867 spin_lock_irq(&cgwb_lock);
868 list_for_each_entry_safe(wb, next, list, blkcg_node)
870 list->next = NULL; /* prevent new wb's */
871 spin_unlock_irq(&cgwb_lock);
874 static void cgwb_bdi_register(struct backing_dev_info *bdi)
876 spin_lock_irq(&cgwb_lock);
877 list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
878 spin_unlock_irq(&cgwb_lock);
881 static int __init cgwb_init(void)
884 * There can be many concurrent release work items overwhelming
885 * system_wq. Put them in a separate wq and limit concurrency.
886 * There's no point in executing many of these in parallel.
888 cgwb_release_wq = alloc_workqueue("cgwb_release", 0, 1);
889 if (!cgwb_release_wq)
894 subsys_initcall(cgwb_init);
896 #else /* CONFIG_CGROUP_WRITEBACK */
898 static int cgwb_bdi_init(struct backing_dev_info *bdi)
900 return wb_init(&bdi->wb, bdi, GFP_KERNEL);
903 static void cgwb_bdi_unregister(struct backing_dev_info *bdi) { }
905 static void cgwb_bdi_register(struct backing_dev_info *bdi)
907 list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
910 static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb)
912 list_del_rcu(&wb->bdi_node);
915 #endif /* CONFIG_CGROUP_WRITEBACK */
917 int bdi_init(struct backing_dev_info *bdi)
921 kref_init(&bdi->refcnt);
923 bdi->max_ratio = 100 * BDI_RATIO_SCALE;
924 bdi->max_prop_frac = FPROP_FRAC_BASE;
925 INIT_LIST_HEAD(&bdi->bdi_list);
926 INIT_LIST_HEAD(&bdi->wb_list);
927 init_waitqueue_head(&bdi->wb_waitq);
929 return cgwb_bdi_init(bdi);
932 struct backing_dev_info *bdi_alloc(int node_id)
934 struct backing_dev_info *bdi;
936 bdi = kzalloc_node(sizeof(*bdi), GFP_KERNEL, node_id);
944 bdi->capabilities = BDI_CAP_WRITEBACK | BDI_CAP_WRITEBACK_ACCT;
945 bdi->ra_pages = VM_READAHEAD_PAGES;
946 bdi->io_pages = VM_READAHEAD_PAGES;
947 timer_setup(&bdi->laptop_mode_wb_timer, laptop_mode_timer_fn, 0);
950 EXPORT_SYMBOL(bdi_alloc);
952 static struct rb_node **bdi_lookup_rb_node(u64 id, struct rb_node **parentp)
954 struct rb_node **p = &bdi_tree.rb_node;
955 struct rb_node *parent = NULL;
956 struct backing_dev_info *bdi;
958 lockdep_assert_held(&bdi_lock);
962 bdi = rb_entry(parent, struct backing_dev_info, rb_node);
966 else if (bdi->id < id)
978 * bdi_get_by_id - lookup and get bdi from its id
979 * @id: bdi id to lookup
981 * Find bdi matching @id and get it. Returns NULL if the matching bdi
982 * doesn't exist or is already unregistered.
984 struct backing_dev_info *bdi_get_by_id(u64 id)
986 struct backing_dev_info *bdi = NULL;
989 spin_lock_bh(&bdi_lock);
990 p = bdi_lookup_rb_node(id, NULL);
992 bdi = rb_entry(*p, struct backing_dev_info, rb_node);
995 spin_unlock_bh(&bdi_lock);
1000 int bdi_register_va(struct backing_dev_info *bdi, const char *fmt, va_list args)
1003 struct rb_node *parent, **p;
1005 if (bdi->dev) /* The driver needs to use separate queues per device */
1008 vsnprintf(bdi->dev_name, sizeof(bdi->dev_name), fmt, args);
1009 dev = device_create(&bdi_class, NULL, MKDEV(0, 0), bdi, bdi->dev_name);
1011 return PTR_ERR(dev);
1013 cgwb_bdi_register(bdi);
1016 bdi_debug_register(bdi, dev_name(dev));
1017 set_bit(WB_registered, &bdi->wb.state);
1019 spin_lock_bh(&bdi_lock);
1021 bdi->id = ++bdi_id_cursor;
1023 p = bdi_lookup_rb_node(bdi->id, &parent);
1024 rb_link_node(&bdi->rb_node, parent, p);
1025 rb_insert_color(&bdi->rb_node, &bdi_tree);
1027 list_add_tail_rcu(&bdi->bdi_list, &bdi_list);
1029 spin_unlock_bh(&bdi_lock);
1031 trace_writeback_bdi_register(bdi);
1035 int bdi_register(struct backing_dev_info *bdi, const char *fmt, ...)
1040 va_start(args, fmt);
1041 ret = bdi_register_va(bdi, fmt, args);
1045 EXPORT_SYMBOL(bdi_register);
1047 void bdi_set_owner(struct backing_dev_info *bdi, struct device *owner)
1049 WARN_ON_ONCE(bdi->owner);
1055 * Remove bdi from bdi_list, and ensure that it is no longer visible
1057 static void bdi_remove_from_list(struct backing_dev_info *bdi)
1059 spin_lock_bh(&bdi_lock);
1060 rb_erase(&bdi->rb_node, &bdi_tree);
1061 list_del_rcu(&bdi->bdi_list);
1062 spin_unlock_bh(&bdi_lock);
1064 synchronize_rcu_expedited();
1067 void bdi_unregister(struct backing_dev_info *bdi)
1069 del_timer_sync(&bdi->laptop_mode_wb_timer);
1071 /* make sure nobody finds us on the bdi_list anymore */
1072 bdi_remove_from_list(bdi);
1073 wb_shutdown(&bdi->wb);
1074 cgwb_bdi_unregister(bdi);
1077 * If this BDI's min ratio has been set, use bdi_set_min_ratio() to
1078 * update the global bdi_min_ratio.
1081 bdi_set_min_ratio(bdi, 0);
1084 bdi_debug_unregister(bdi);
1085 device_unregister(bdi->dev);
1090 put_device(bdi->owner);
1094 EXPORT_SYMBOL(bdi_unregister);
1096 static void release_bdi(struct kref *ref)
1098 struct backing_dev_info *bdi =
1099 container_of(ref, struct backing_dev_info, refcnt);
1101 WARN_ON_ONCE(test_bit(WB_registered, &bdi->wb.state));
1102 WARN_ON_ONCE(bdi->dev);
1107 void bdi_put(struct backing_dev_info *bdi)
1109 kref_put(&bdi->refcnt, release_bdi);
1111 EXPORT_SYMBOL(bdi_put);
1113 struct backing_dev_info *inode_to_bdi(struct inode *inode)
1115 struct super_block *sb;
1118 return &noop_backing_dev_info;
1122 if (sb_is_blkdev_sb(sb))
1123 return I_BDEV(inode)->bd_disk->bdi;
1127 EXPORT_SYMBOL(inode_to_bdi);
1129 const char *bdi_dev_name(struct backing_dev_info *bdi)
1131 if (!bdi || !bdi->dev)
1132 return bdi_unknown_name;
1133 return bdi->dev_name;
1135 EXPORT_SYMBOL_GPL(bdi_dev_name);