123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125 |
- #include <linux/wait.h>
- #include <linux/backing-dev.h>
- #include <linux/kthread.h>
- #include <linux/freezer.h>
- #include <linux/fs.h>
- #include <linux/pagemap.h>
- #include <linux/mm.h>
- #include <linux/sched.h>
- #include <linux/module.h>
- #include <linux/writeback.h>
- #include <linux/device.h>
- #include <trace/events/writeback.h>
- static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
- struct backing_dev_info noop_backing_dev_info = {
- .name = "noop",
- .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
- };
- EXPORT_SYMBOL_GPL(noop_backing_dev_info);
- static struct class *bdi_class;
- DEFINE_SPINLOCK(bdi_lock);
- LIST_HEAD(bdi_list);
- struct workqueue_struct *bdi_wq;
- #ifdef CONFIG_DEBUG_FS
- #include <linux/debugfs.h>
- #include <linux/seq_file.h>
- static struct dentry *bdi_debug_root;
- static void bdi_debug_init(void)
- {
- bdi_debug_root = debugfs_create_dir("bdi", NULL);
- }
- static int bdi_debug_stats_show(struct seq_file *m, void *v)
- {
- struct backing_dev_info *bdi = m->private;
- struct bdi_writeback *wb = &bdi->wb;
- unsigned long background_thresh;
- unsigned long dirty_thresh;
- unsigned long wb_thresh;
- unsigned long nr_dirty, nr_io, nr_more_io, nr_dirty_time;
- struct inode *inode;
- nr_dirty = nr_io = nr_more_io = nr_dirty_time = 0;
- spin_lock(&wb->list_lock);
- list_for_each_entry(inode, &wb->b_dirty, i_io_list)
- nr_dirty++;
- list_for_each_entry(inode, &wb->b_io, i_io_list)
- nr_io++;
- list_for_each_entry(inode, &wb->b_more_io, i_io_list)
- nr_more_io++;
- list_for_each_entry(inode, &wb->b_dirty_time, i_io_list)
- if (inode->i_state & I_DIRTY_TIME)
- nr_dirty_time++;
- spin_unlock(&wb->list_lock);
- global_dirty_limits(&background_thresh, &dirty_thresh);
- wb_thresh = wb_calc_thresh(wb, dirty_thresh);
- #define K(x) ((x) << (PAGE_SHIFT - 10))
- seq_printf(m,
- "BdiWriteback: %10lu kB\n"
- "BdiReclaimable: %10lu kB\n"
- "BdiDirtyThresh: %10lu kB\n"
- "DirtyThresh: %10lu kB\n"
- "BackgroundThresh: %10lu kB\n"
- "BdiDirtied: %10lu kB\n"
- "BdiWritten: %10lu kB\n"
- "BdiWriteBandwidth: %10lu kBps\n"
- "b_dirty: %10lu\n"
- "b_io: %10lu\n"
- "b_more_io: %10lu\n"
- "b_dirty_time: %10lu\n"
- "bdi_list: %10u\n"
- "state: %10lx\n",
- (unsigned long) K(wb_stat(wb, WB_WRITEBACK)),
- (unsigned long) K(wb_stat(wb, WB_RECLAIMABLE)),
- K(wb_thresh),
- K(dirty_thresh),
- K(background_thresh),
- (unsigned long) K(wb_stat(wb, WB_DIRTIED)),
- (unsigned long) K(wb_stat(wb, WB_WRITTEN)),
- (unsigned long) K(wb->write_bandwidth),
- nr_dirty,
- nr_io,
- nr_more_io,
- nr_dirty_time,
- !list_empty(&bdi->bdi_list), bdi->wb.state);
- #undef K
- return 0;
- }
- static int bdi_debug_stats_open(struct inode *inode, struct file *file)
- {
- return single_open(file, bdi_debug_stats_show, inode->i_private);
- }
- static const struct file_operations bdi_debug_stats_fops = {
- .open = bdi_debug_stats_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static void bdi_debug_register(struct backing_dev_info *bdi, const char *name)
- {
- bdi->debug_dir = debugfs_create_dir(name, bdi_debug_root);
- bdi->debug_stats = debugfs_create_file("stats", 0444, bdi->debug_dir,
- bdi, &bdi_debug_stats_fops);
- }
- static void bdi_debug_unregister(struct backing_dev_info *bdi)
- {
- debugfs_remove(bdi->debug_stats);
- debugfs_remove(bdi->debug_dir);
- }
- #else
- static inline void bdi_debug_init(void)
- {
- }
- static inline void bdi_debug_register(struct backing_dev_info *bdi,
- const char *name)
- {
- }
- static inline void bdi_debug_unregister(struct backing_dev_info *bdi)
- {
- }
- #endif
- static ssize_t read_ahead_kb_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t count)
- {
- struct backing_dev_info *bdi = dev_get_drvdata(dev);
- unsigned long read_ahead_kb;
- ssize_t ret;
- ret = kstrtoul(buf, 10, &read_ahead_kb);
- if (ret < 0)
- return ret;
- bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10);
- return count;
- }
- #define K(pages) ((pages) << (PAGE_SHIFT - 10))
- #define BDI_SHOW(name, expr) \
- static ssize_t name##_show(struct device *dev, \
- struct device_attribute *attr, char *page) \
- { \
- struct backing_dev_info *bdi = dev_get_drvdata(dev); \
- \
- return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr); \
- } \
- static DEVICE_ATTR_RW(name);
- BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
- static ssize_t min_ratio_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t count)
- {
- struct backing_dev_info *bdi = dev_get_drvdata(dev);
- unsigned int ratio;
- ssize_t ret;
- ret = kstrtouint(buf, 10, &ratio);
- if (ret < 0)
- return ret;
- ret = bdi_set_min_ratio(bdi, ratio);
- if (!ret)
- ret = count;
- return ret;
- }
- BDI_SHOW(min_ratio, bdi->min_ratio)
- static ssize_t max_ratio_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t count)
- {
- struct backing_dev_info *bdi = dev_get_drvdata(dev);
- unsigned int ratio;
- ssize_t ret;
- ret = kstrtouint(buf, 10, &ratio);
- if (ret < 0)
- return ret;
- ret = bdi_set_max_ratio(bdi, ratio);
- if (!ret)
- ret = count;
- return ret;
- }
- BDI_SHOW(max_ratio, bdi->max_ratio)
- static ssize_t stable_pages_required_show(struct device *dev,
- struct device_attribute *attr,
- char *page)
- {
- struct backing_dev_info *bdi = dev_get_drvdata(dev);
- return snprintf(page, PAGE_SIZE-1, "%d\n",
- bdi_cap_stable_pages_required(bdi) ? 1 : 0);
- }
- static DEVICE_ATTR_RO(stable_pages_required);
- static struct attribute *bdi_dev_attrs[] = {
- &dev_attr_read_ahead_kb.attr,
- &dev_attr_min_ratio.attr,
- &dev_attr_max_ratio.attr,
- &dev_attr_stable_pages_required.attr,
- NULL,
- };
- ATTRIBUTE_GROUPS(bdi_dev);
- static __init int bdi_class_init(void)
- {
- bdi_class = class_create(THIS_MODULE, "bdi");
- if (IS_ERR(bdi_class))
- return PTR_ERR(bdi_class);
- bdi_class->dev_groups = bdi_dev_groups;
- bdi_debug_init();
- return 0;
- }
- postcore_initcall(bdi_class_init);
- static int __init default_bdi_init(void)
- {
- int err;
- bdi_wq = alloc_workqueue("writeback", WQ_MEM_RECLAIM | WQ_FREEZABLE |
- WQ_UNBOUND | WQ_SYSFS, 0);
- if (!bdi_wq)
- return -ENOMEM;
- err = bdi_init(&noop_backing_dev_info);
- return err;
- }
- subsys_initcall(default_bdi_init);
- void wb_wakeup_delayed(struct bdi_writeback *wb)
- {
- unsigned long timeout;
- timeout = msecs_to_jiffies(dirty_writeback_interval * 10);
- spin_lock_bh(&wb->work_lock);
- if (test_bit(WB_registered, &wb->state))
- queue_delayed_work(bdi_wq, &wb->dwork, timeout);
- spin_unlock_bh(&wb->work_lock);
- }
- #define INIT_BW (100 << (20 - PAGE_SHIFT))
- static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
- int blkcg_id, gfp_t gfp)
- {
- int i, err;
- memset(wb, 0, sizeof(*wb));
- if (wb != &bdi->wb)
- bdi_get(bdi);
- wb->bdi = bdi;
- wb->last_old_flush = jiffies;
- INIT_LIST_HEAD(&wb->b_dirty);
- INIT_LIST_HEAD(&wb->b_io);
- INIT_LIST_HEAD(&wb->b_more_io);
- INIT_LIST_HEAD(&wb->b_dirty_time);
- spin_lock_init(&wb->list_lock);
- wb->bw_time_stamp = jiffies;
- wb->balanced_dirty_ratelimit = INIT_BW;
- wb->dirty_ratelimit = INIT_BW;
- wb->write_bandwidth = INIT_BW;
- wb->avg_write_bandwidth = INIT_BW;
- spin_lock_init(&wb->work_lock);
- INIT_LIST_HEAD(&wb->work_list);
- INIT_DELAYED_WORK(&wb->dwork, wb_workfn);
- wb->congested = wb_congested_get_create(bdi, blkcg_id, gfp);
- if (!wb->congested) {
- err = -ENOMEM;
- goto out_put_bdi;
- }
- err = fprop_local_init_percpu(&wb->completions, gfp);
- if (err)
- goto out_put_cong;
- for (i = 0; i < NR_WB_STAT_ITEMS; i++) {
- err = percpu_counter_init(&wb->stat[i], 0, gfp);
- if (err)
- goto out_destroy_stat;
- }
- return 0;
- out_destroy_stat:
- while (i--)
- percpu_counter_destroy(&wb->stat[i]);
- fprop_local_destroy_percpu(&wb->completions);
- out_put_cong:
- wb_congested_put(wb->congested);
- out_put_bdi:
- if (wb != &bdi->wb)
- bdi_put(bdi);
- return err;
- }
- static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb);
- static void wb_shutdown(struct bdi_writeback *wb)
- {
-
- spin_lock_bh(&wb->work_lock);
- if (!test_and_clear_bit(WB_registered, &wb->state)) {
- spin_unlock_bh(&wb->work_lock);
-
- wait_on_bit(&wb->state, WB_shutting_down, TASK_UNINTERRUPTIBLE);
- return;
- }
- set_bit(WB_shutting_down, &wb->state);
- spin_unlock_bh(&wb->work_lock);
- cgwb_remove_from_bdi_list(wb);
-
- mod_delayed_work(bdi_wq, &wb->dwork, 0);
- flush_delayed_work(&wb->dwork);
- WARN_ON(!list_empty(&wb->work_list));
-
- smp_wmb();
- clear_bit(WB_shutting_down, &wb->state);
- }
- static void wb_exit(struct bdi_writeback *wb)
- {
- int i;
- WARN_ON(delayed_work_pending(&wb->dwork));
- for (i = 0; i < NR_WB_STAT_ITEMS; i++)
- percpu_counter_destroy(&wb->stat[i]);
- fprop_local_destroy_percpu(&wb->completions);
- wb_congested_put(wb->congested);
- if (wb != &wb->bdi->wb)
- bdi_put(wb->bdi);
- }
- #ifdef CONFIG_CGROUP_WRITEBACK
- #include <linux/memcontrol.h>
- static DEFINE_SPINLOCK(cgwb_lock);
- struct bdi_writeback_congested *
- wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
- {
- struct bdi_writeback_congested *new_congested = NULL, *congested;
- struct rb_node **node, *parent;
- unsigned long flags;
- retry:
- spin_lock_irqsave(&cgwb_lock, flags);
- node = &bdi->cgwb_congested_tree.rb_node;
- parent = NULL;
- while (*node != NULL) {
- parent = *node;
- congested = container_of(parent, struct bdi_writeback_congested,
- rb_node);
- if (congested->blkcg_id < blkcg_id)
- node = &parent->rb_left;
- else if (congested->blkcg_id > blkcg_id)
- node = &parent->rb_right;
- else
- goto found;
- }
- if (new_congested) {
-
- congested = new_congested;
- new_congested = NULL;
- rb_link_node(&congested->rb_node, parent, node);
- rb_insert_color(&congested->rb_node, &bdi->cgwb_congested_tree);
- goto found;
- }
- spin_unlock_irqrestore(&cgwb_lock, flags);
-
- new_congested = kzalloc(sizeof(*new_congested), gfp);
- if (!new_congested)
- return NULL;
- atomic_set(&new_congested->refcnt, 0);
- new_congested->__bdi = bdi;
- new_congested->blkcg_id = blkcg_id;
- goto retry;
- found:
- atomic_inc(&congested->refcnt);
- spin_unlock_irqrestore(&cgwb_lock, flags);
- kfree(new_congested);
- return congested;
- }
- void wb_congested_put(struct bdi_writeback_congested *congested)
- {
- unsigned long flags;
- local_irq_save(flags);
- if (!atomic_dec_and_lock(&congested->refcnt, &cgwb_lock)) {
- local_irq_restore(flags);
- return;
- }
-
- if (congested->__bdi) {
- rb_erase(&congested->rb_node,
- &congested->__bdi->cgwb_congested_tree);
- congested->__bdi = NULL;
- }
- spin_unlock_irqrestore(&cgwb_lock, flags);
- kfree(congested);
- }
- static void cgwb_release_workfn(struct work_struct *work)
- {
- struct bdi_writeback *wb = container_of(work, struct bdi_writeback,
- release_work);
- wb_shutdown(wb);
- css_put(wb->memcg_css);
- css_put(wb->blkcg_css);
- fprop_local_destroy_percpu(&wb->memcg_completions);
- percpu_ref_exit(&wb->refcnt);
- wb_exit(wb);
- kfree_rcu(wb, rcu);
- }
- static void cgwb_release(struct percpu_ref *refcnt)
- {
- struct bdi_writeback *wb = container_of(refcnt, struct bdi_writeback,
- refcnt);
- schedule_work(&wb->release_work);
- }
- static void cgwb_kill(struct bdi_writeback *wb)
- {
- lockdep_assert_held(&cgwb_lock);
- WARN_ON(!radix_tree_delete(&wb->bdi->cgwb_tree, wb->memcg_css->id));
- list_del(&wb->memcg_node);
- list_del(&wb->blkcg_node);
- percpu_ref_kill(&wb->refcnt);
- }
- static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb)
- {
- spin_lock_irq(&cgwb_lock);
- list_del_rcu(&wb->bdi_node);
- spin_unlock_irq(&cgwb_lock);
- }
- static int cgwb_create(struct backing_dev_info *bdi,
- struct cgroup_subsys_state *memcg_css, gfp_t gfp)
- {
- struct mem_cgroup *memcg;
- struct cgroup_subsys_state *blkcg_css;
- struct blkcg *blkcg;
- struct list_head *memcg_cgwb_list, *blkcg_cgwb_list;
- struct bdi_writeback *wb;
- unsigned long flags;
- int ret = 0;
- memcg = mem_cgroup_from_css(memcg_css);
- blkcg_css = cgroup_get_e_css(memcg_css->cgroup, &io_cgrp_subsys);
- blkcg = css_to_blkcg(blkcg_css);
- memcg_cgwb_list = mem_cgroup_cgwb_list(memcg);
- blkcg_cgwb_list = &blkcg->cgwb_list;
-
- spin_lock_irqsave(&cgwb_lock, flags);
- wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
- if (wb && wb->blkcg_css != blkcg_css) {
- cgwb_kill(wb);
- wb = NULL;
- }
- spin_unlock_irqrestore(&cgwb_lock, flags);
- if (wb)
- goto out_put;
-
- wb = kmalloc(sizeof(*wb), gfp);
- if (!wb)
- return -ENOMEM;
- ret = wb_init(wb, bdi, blkcg_css->id, gfp);
- if (ret)
- goto err_free;
- ret = percpu_ref_init(&wb->refcnt, cgwb_release, 0, gfp);
- if (ret)
- goto err_wb_exit;
- ret = fprop_local_init_percpu(&wb->memcg_completions, gfp);
- if (ret)
- goto err_ref_exit;
- wb->memcg_css = memcg_css;
- wb->blkcg_css = blkcg_css;
- INIT_WORK(&wb->release_work, cgwb_release_workfn);
- set_bit(WB_registered, &wb->state);
-
- ret = -ENODEV;
- spin_lock_irqsave(&cgwb_lock, flags);
- if (test_bit(WB_registered, &bdi->wb.state) &&
- blkcg_cgwb_list->next && memcg_cgwb_list->next) {
-
- ret = radix_tree_insert(&bdi->cgwb_tree, memcg_css->id, wb);
- if (!ret) {
- list_add_tail_rcu(&wb->bdi_node, &bdi->wb_list);
- list_add(&wb->memcg_node, memcg_cgwb_list);
- list_add(&wb->blkcg_node, blkcg_cgwb_list);
- css_get(memcg_css);
- css_get(blkcg_css);
- }
- }
- spin_unlock_irqrestore(&cgwb_lock, flags);
- if (ret) {
- if (ret == -EEXIST)
- ret = 0;
- goto err_fprop_exit;
- }
- goto out_put;
- err_fprop_exit:
- fprop_local_destroy_percpu(&wb->memcg_completions);
- err_ref_exit:
- percpu_ref_exit(&wb->refcnt);
- err_wb_exit:
- wb_exit(wb);
- err_free:
- kfree(wb);
- out_put:
- css_put(blkcg_css);
- return ret;
- }
- struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
- struct cgroup_subsys_state *memcg_css,
- gfp_t gfp)
- {
- struct bdi_writeback *wb;
- might_sleep_if(gfpflags_allow_blocking(gfp));
- if (!memcg_css->parent)
- return &bdi->wb;
- do {
- rcu_read_lock();
- wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
- if (wb) {
- struct cgroup_subsys_state *blkcg_css;
-
- blkcg_css = cgroup_get_e_css(memcg_css->cgroup,
- &io_cgrp_subsys);
- if (unlikely(wb->blkcg_css != blkcg_css ||
- !wb_tryget(wb)))
- wb = NULL;
- css_put(blkcg_css);
- }
- rcu_read_unlock();
- } while (!wb && !cgwb_create(bdi, memcg_css, gfp));
- return wb;
- }
- static int cgwb_bdi_init(struct backing_dev_info *bdi)
- {
- int ret;
- INIT_RADIX_TREE(&bdi->cgwb_tree, GFP_ATOMIC);
- bdi->cgwb_congested_tree = RB_ROOT;
- init_rwsem(&bdi->wb_switch_rwsem);
- ret = wb_init(&bdi->wb, bdi, 1, GFP_KERNEL);
- if (!ret) {
- bdi->wb.memcg_css = &root_mem_cgroup->css;
- bdi->wb.blkcg_css = blkcg_root_css;
- }
- return ret;
- }
- static void cgwb_bdi_unregister(struct backing_dev_info *bdi)
- {
- struct radix_tree_iter iter;
- void **slot;
- struct bdi_writeback *wb;
- WARN_ON(test_bit(WB_registered, &bdi->wb.state));
- spin_lock_irq(&cgwb_lock);
- radix_tree_for_each_slot(slot, &bdi->cgwb_tree, &iter, 0)
- cgwb_kill(*slot);
- while (!list_empty(&bdi->wb_list)) {
- wb = list_first_entry(&bdi->wb_list, struct bdi_writeback,
- bdi_node);
- spin_unlock_irq(&cgwb_lock);
- wb_shutdown(wb);
- spin_lock_irq(&cgwb_lock);
- }
- spin_unlock_irq(&cgwb_lock);
- }
- void wb_memcg_offline(struct mem_cgroup *memcg)
- {
- LIST_HEAD(to_destroy);
- struct list_head *memcg_cgwb_list = mem_cgroup_cgwb_list(memcg);
- struct bdi_writeback *wb, *next;
- spin_lock_irq(&cgwb_lock);
- list_for_each_entry_safe(wb, next, memcg_cgwb_list, memcg_node)
- cgwb_kill(wb);
- memcg_cgwb_list->next = NULL;
- spin_unlock_irq(&cgwb_lock);
- }
- void wb_blkcg_offline(struct blkcg *blkcg)
- {
- LIST_HEAD(to_destroy);
- struct bdi_writeback *wb, *next;
- spin_lock_irq(&cgwb_lock);
- list_for_each_entry_safe(wb, next, &blkcg->cgwb_list, blkcg_node)
- cgwb_kill(wb);
- blkcg->cgwb_list.next = NULL;
- spin_unlock_irq(&cgwb_lock);
- }
- static void cgwb_bdi_exit(struct backing_dev_info *bdi)
- {
- struct rb_node *rbn;
- spin_lock_irq(&cgwb_lock);
- while ((rbn = rb_first(&bdi->cgwb_congested_tree))) {
- struct bdi_writeback_congested *congested =
- rb_entry(rbn, struct bdi_writeback_congested, rb_node);
- rb_erase(rbn, &bdi->cgwb_congested_tree);
- congested->__bdi = NULL;
- }
- spin_unlock_irq(&cgwb_lock);
- }
- static void cgwb_bdi_register(struct backing_dev_info *bdi)
- {
- spin_lock_irq(&cgwb_lock);
- list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
- spin_unlock_irq(&cgwb_lock);
- }
- #else
- static int cgwb_bdi_init(struct backing_dev_info *bdi)
- {
- int err;
- bdi->wb_congested = kzalloc(sizeof(*bdi->wb_congested), GFP_KERNEL);
- if (!bdi->wb_congested)
- return -ENOMEM;
- atomic_set(&bdi->wb_congested->refcnt, 1);
- err = wb_init(&bdi->wb, bdi, 1, GFP_KERNEL);
- if (err) {
- wb_congested_put(bdi->wb_congested);
- return err;
- }
- return 0;
- }
- static void cgwb_bdi_unregister(struct backing_dev_info *bdi) { }
- static void cgwb_bdi_exit(struct backing_dev_info *bdi)
- {
- wb_congested_put(bdi->wb_congested);
- }
- static void cgwb_bdi_register(struct backing_dev_info *bdi)
- {
- list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
- }
- static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb)
- {
- list_del_rcu(&wb->bdi_node);
- }
- #endif
- int bdi_init(struct backing_dev_info *bdi)
- {
- int ret;
- bdi->dev = NULL;
- kref_init(&bdi->refcnt);
- bdi->min_ratio = 0;
- bdi->max_ratio = 100;
- bdi->max_prop_frac = FPROP_FRAC_BASE;
- INIT_LIST_HEAD(&bdi->bdi_list);
- INIT_LIST_HEAD(&bdi->wb_list);
- init_waitqueue_head(&bdi->wb_waitq);
- ret = cgwb_bdi_init(bdi);
- return ret;
- }
- EXPORT_SYMBOL(bdi_init);
- struct backing_dev_info *bdi_alloc_node(gfp_t gfp_mask, int node_id)
- {
- struct backing_dev_info *bdi;
- bdi = kmalloc_node(sizeof(struct backing_dev_info),
- gfp_mask | __GFP_ZERO, node_id);
- if (!bdi)
- return NULL;
- if (bdi_init(bdi)) {
- kfree(bdi);
- return NULL;
- }
- return bdi;
- }
- int bdi_register(struct backing_dev_info *bdi, struct device *parent,
- const char *fmt, ...)
- {
- va_list args;
- struct device *dev;
- if (bdi->dev)
- return 0;
- va_start(args, fmt);
- dev = device_create_vargs(bdi_class, parent, MKDEV(0, 0), bdi, fmt, args);
- va_end(args);
- if (IS_ERR(dev))
- return PTR_ERR(dev);
- cgwb_bdi_register(bdi);
- bdi->dev = dev;
- bdi_debug_register(bdi, dev_name(dev));
- set_bit(WB_registered, &bdi->wb.state);
- spin_lock_bh(&bdi_lock);
- list_add_tail_rcu(&bdi->bdi_list, &bdi_list);
- spin_unlock_bh(&bdi_lock);
- trace_writeback_bdi_register(bdi);
- return 0;
- }
- EXPORT_SYMBOL(bdi_register);
- int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev)
- {
- return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev));
- }
- EXPORT_SYMBOL(bdi_register_dev);
- int bdi_register_owner(struct backing_dev_info *bdi, struct device *owner)
- {
- int rc;
- rc = bdi_register(bdi, NULL, "%u:%u", MAJOR(owner->devt),
- MINOR(owner->devt));
- if (rc)
- return rc;
-
- WARN_ON(bdi->owner);
- bdi->owner = owner;
- get_device(owner);
- return 0;
- }
- EXPORT_SYMBOL(bdi_register_owner);
- static void bdi_remove_from_list(struct backing_dev_info *bdi)
- {
- spin_lock_bh(&bdi_lock);
- list_del_rcu(&bdi->bdi_list);
- spin_unlock_bh(&bdi_lock);
- synchronize_rcu_expedited();
- }
- void bdi_unregister(struct backing_dev_info *bdi)
- {
-
- bdi_remove_from_list(bdi);
- wb_shutdown(&bdi->wb);
- cgwb_bdi_unregister(bdi);
- if (bdi->dev) {
- bdi_debug_unregister(bdi);
- device_unregister(bdi->dev);
- bdi->dev = NULL;
- }
- if (bdi->owner) {
- put_device(bdi->owner);
- bdi->owner = NULL;
- }
- }
- static void bdi_exit(struct backing_dev_info *bdi)
- {
- WARN_ON_ONCE(bdi->dev);
- wb_exit(&bdi->wb);
- cgwb_bdi_exit(bdi);
- }
- static void release_bdi(struct kref *ref)
- {
- struct backing_dev_info *bdi =
- container_of(ref, struct backing_dev_info, refcnt);
- bdi_exit(bdi);
- kfree(bdi);
- }
- void bdi_put(struct backing_dev_info *bdi)
- {
- kref_put(&bdi->refcnt, release_bdi);
- }
- void bdi_destroy(struct backing_dev_info *bdi)
- {
- bdi_unregister(bdi);
- bdi_exit(bdi);
- }
- EXPORT_SYMBOL(bdi_destroy);
- int bdi_setup_and_register(struct backing_dev_info *bdi, char *name)
- {
- int err;
- bdi->name = name;
- bdi->capabilities = 0;
- err = bdi_init(bdi);
- if (err)
- return err;
- err = bdi_register(bdi, NULL, "%.28s-%ld", name,
- atomic_long_inc_return(&bdi_seq));
- if (err) {
- bdi_destroy(bdi);
- return err;
- }
- return 0;
- }
- EXPORT_SYMBOL(bdi_setup_and_register);
- static wait_queue_head_t congestion_wqh[2] = {
- __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
- __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
- };
- static atomic_t nr_wb_congested[2];
- void clear_wb_congested(struct bdi_writeback_congested *congested, int sync)
- {
- wait_queue_head_t *wqh = &congestion_wqh[sync];
- enum wb_congested_state bit;
- bit = sync ? WB_sync_congested : WB_async_congested;
- if (test_and_clear_bit(bit, &congested->state))
- atomic_dec(&nr_wb_congested[sync]);
- smp_mb__after_atomic();
- if (waitqueue_active(wqh))
- wake_up(wqh);
- }
- EXPORT_SYMBOL(clear_wb_congested);
- void set_wb_congested(struct bdi_writeback_congested *congested, int sync)
- {
- enum wb_congested_state bit;
- bit = sync ? WB_sync_congested : WB_async_congested;
- if (!test_and_set_bit(bit, &congested->state))
- atomic_inc(&nr_wb_congested[sync]);
- }
- EXPORT_SYMBOL(set_wb_congested);
- long congestion_wait(int sync, long timeout)
- {
- long ret;
- unsigned long start = jiffies;
- DEFINE_WAIT(wait);
- wait_queue_head_t *wqh = &congestion_wqh[sync];
- prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
- ret = io_schedule_timeout(timeout);
- finish_wait(wqh, &wait);
- trace_writeback_congestion_wait(jiffies_to_usecs(timeout),
- jiffies_to_usecs(jiffies - start));
- return ret;
- }
- EXPORT_SYMBOL(congestion_wait);
- long wait_iff_congested(struct pglist_data *pgdat, int sync, long timeout)
- {
- long ret;
- unsigned long start = jiffies;
- DEFINE_WAIT(wait);
- wait_queue_head_t *wqh = &congestion_wqh[sync];
-
- if (atomic_read(&nr_wb_congested[sync]) == 0 ||
- !test_bit(PGDAT_CONGESTED, &pgdat->flags)) {
- cond_resched();
-
- ret = timeout - (jiffies - start);
- if (ret < 0)
- ret = 0;
- goto out;
- }
-
- prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
- ret = io_schedule_timeout(timeout);
- finish_wait(wqh, &wait);
- out:
- trace_writeback_wait_iff_congested(jiffies_to_usecs(timeout),
- jiffies_to_usecs(jiffies - start));
- return ret;
- }
- EXPORT_SYMBOL(wait_iff_congested);
- int pdflush_proc_obsolete(struct ctl_table *table, int write,
- void __user *buffer, size_t *lenp, loff_t *ppos)
- {
- char kbuf[] = "0\n";
- if (*ppos || *lenp < sizeof(kbuf)) {
- *lenp = 0;
- return 0;
- }
- if (copy_to_user(buffer, kbuf, sizeof(kbuf)))
- return -EFAULT;
- pr_warn_once("%s exported in /proc is scheduled for removal\n",
- table->procname);
- *lenp = 2;
- *ppos += *lenp;
- return 2;
- }
|