Back to home page

LXR

 
 

    


0001 
0002 #include <linux/wait.h>
0003 #include <linux/backing-dev.h>
0004 #include <linux/kthread.h>
0005 #include <linux/freezer.h>
0006 #include <linux/fs.h>
0007 #include <linux/pagemap.h>
0008 #include <linux/mm.h>
0009 #include <linux/sched.h>
0010 #include <linux/module.h>
0011 #include <linux/writeback.h>
0012 #include <linux/device.h>
0013 #include <trace/events/writeback.h>
0014 
0015 static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
0016 
0017 struct backing_dev_info noop_backing_dev_info = {
0018     .name       = "noop",
0019     .capabilities   = BDI_CAP_NO_ACCT_AND_WRITEBACK,
0020 };
0021 EXPORT_SYMBOL_GPL(noop_backing_dev_info);
0022 
0023 static struct class *bdi_class;
0024 
0025 /*
0026  * bdi_lock protects updates to bdi_list. bdi_list has RCU reader side
0027  * locking.
0028  */
0029 DEFINE_SPINLOCK(bdi_lock);
0030 LIST_HEAD(bdi_list);
0031 
0032 /* bdi_wq serves all asynchronous writeback tasks */
0033 struct workqueue_struct *bdi_wq;
0034 
0035 #ifdef CONFIG_DEBUG_FS
0036 #include <linux/debugfs.h>
0037 #include <linux/seq_file.h>
0038 
0039 static struct dentry *bdi_debug_root;
0040 
0041 static void bdi_debug_init(void)
0042 {
0043     bdi_debug_root = debugfs_create_dir("bdi", NULL);
0044 }
0045 
0046 static int bdi_debug_stats_show(struct seq_file *m, void *v)
0047 {
0048     struct backing_dev_info *bdi = m->private;
0049     struct bdi_writeback *wb = &bdi->wb;
0050     unsigned long background_thresh;
0051     unsigned long dirty_thresh;
0052     unsigned long wb_thresh;
0053     unsigned long nr_dirty, nr_io, nr_more_io, nr_dirty_time;
0054     struct inode *inode;
0055 
0056     nr_dirty = nr_io = nr_more_io = nr_dirty_time = 0;
0057     spin_lock(&wb->list_lock);
0058     list_for_each_entry(inode, &wb->b_dirty, i_io_list)
0059         nr_dirty++;
0060     list_for_each_entry(inode, &wb->b_io, i_io_list)
0061         nr_io++;
0062     list_for_each_entry(inode, &wb->b_more_io, i_io_list)
0063         nr_more_io++;
0064     list_for_each_entry(inode, &wb->b_dirty_time, i_io_list)
0065         if (inode->i_state & I_DIRTY_TIME)
0066             nr_dirty_time++;
0067     spin_unlock(&wb->list_lock);
0068 
0069     global_dirty_limits(&background_thresh, &dirty_thresh);
0070     wb_thresh = wb_calc_thresh(wb, dirty_thresh);
0071 
0072 #define K(x) ((x) << (PAGE_SHIFT - 10))
0073     seq_printf(m,
0074            "BdiWriteback:       %10lu kB\n"
0075            "BdiReclaimable:     %10lu kB\n"
0076            "BdiDirtyThresh:     %10lu kB\n"
0077            "DirtyThresh:        %10lu kB\n"
0078            "BackgroundThresh:   %10lu kB\n"
0079            "BdiDirtied:         %10lu kB\n"
0080            "BdiWritten:         %10lu kB\n"
0081            "BdiWriteBandwidth:  %10lu kBps\n"
0082            "b_dirty:            %10lu\n"
0083            "b_io:               %10lu\n"
0084            "b_more_io:          %10lu\n"
0085            "b_dirty_time:       %10lu\n"
0086            "bdi_list:           %10u\n"
0087            "state:              %10lx\n",
0088            (unsigned long) K(wb_stat(wb, WB_WRITEBACK)),
0089            (unsigned long) K(wb_stat(wb, WB_RECLAIMABLE)),
0090            K(wb_thresh),
0091            K(dirty_thresh),
0092            K(background_thresh),
0093            (unsigned long) K(wb_stat(wb, WB_DIRTIED)),
0094            (unsigned long) K(wb_stat(wb, WB_WRITTEN)),
0095            (unsigned long) K(wb->write_bandwidth),
0096            nr_dirty,
0097            nr_io,
0098            nr_more_io,
0099            nr_dirty_time,
0100            !list_empty(&bdi->bdi_list), bdi->wb.state);
0101 #undef K
0102 
0103     return 0;
0104 }
0105 
0106 static int bdi_debug_stats_open(struct inode *inode, struct file *file)
0107 {
0108     return single_open(file, bdi_debug_stats_show, inode->i_private);
0109 }
0110 
0111 static const struct file_operations bdi_debug_stats_fops = {
0112     .open       = bdi_debug_stats_open,
0113     .read       = seq_read,
0114     .llseek     = seq_lseek,
0115     .release    = single_release,
0116 };
0117 
0118 static void bdi_debug_register(struct backing_dev_info *bdi, const char *name)
0119 {
0120     bdi->debug_dir = debugfs_create_dir(name, bdi_debug_root);
0121     bdi->debug_stats = debugfs_create_file("stats", 0444, bdi->debug_dir,
0122                            bdi, &bdi_debug_stats_fops);
0123 }
0124 
0125 static void bdi_debug_unregister(struct backing_dev_info *bdi)
0126 {
0127     debugfs_remove(bdi->debug_stats);
0128     debugfs_remove(bdi->debug_dir);
0129 }
0130 #else
0131 static inline void bdi_debug_init(void)
0132 {
0133 }
0134 static inline void bdi_debug_register(struct backing_dev_info *bdi,
0135                       const char *name)
0136 {
0137 }
0138 static inline void bdi_debug_unregister(struct backing_dev_info *bdi)
0139 {
0140 }
0141 #endif
0142 
0143 static ssize_t read_ahead_kb_store(struct device *dev,
0144                   struct device_attribute *attr,
0145                   const char *buf, size_t count)
0146 {
0147     struct backing_dev_info *bdi = dev_get_drvdata(dev);
0148     unsigned long read_ahead_kb;
0149     ssize_t ret;
0150 
0151     ret = kstrtoul(buf, 10, &read_ahead_kb);
0152     if (ret < 0)
0153         return ret;
0154 
0155     bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10);
0156 
0157     return count;
0158 }
0159 
0160 #define K(pages) ((pages) << (PAGE_SHIFT - 10))
0161 
0162 #define BDI_SHOW(name, expr)                        \
0163 static ssize_t name##_show(struct device *dev,              \
0164                struct device_attribute *attr, char *page)   \
0165 {                                   \
0166     struct backing_dev_info *bdi = dev_get_drvdata(dev);        \
0167                                     \
0168     return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr);  \
0169 }                                   \
0170 static DEVICE_ATTR_RW(name);
0171 
0172 BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
0173 
0174 static ssize_t min_ratio_store(struct device *dev,
0175         struct device_attribute *attr, const char *buf, size_t count)
0176 {
0177     struct backing_dev_info *bdi = dev_get_drvdata(dev);
0178     unsigned int ratio;
0179     ssize_t ret;
0180 
0181     ret = kstrtouint(buf, 10, &ratio);
0182     if (ret < 0)
0183         return ret;
0184 
0185     ret = bdi_set_min_ratio(bdi, ratio);
0186     if (!ret)
0187         ret = count;
0188 
0189     return ret;
0190 }
0191 BDI_SHOW(min_ratio, bdi->min_ratio)
0192 
0193 static ssize_t max_ratio_store(struct device *dev,
0194         struct device_attribute *attr, const char *buf, size_t count)
0195 {
0196     struct backing_dev_info *bdi = dev_get_drvdata(dev);
0197     unsigned int ratio;
0198     ssize_t ret;
0199 
0200     ret = kstrtouint(buf, 10, &ratio);
0201     if (ret < 0)
0202         return ret;
0203 
0204     ret = bdi_set_max_ratio(bdi, ratio);
0205     if (!ret)
0206         ret = count;
0207 
0208     return ret;
0209 }
0210 BDI_SHOW(max_ratio, bdi->max_ratio)
0211 
0212 static ssize_t stable_pages_required_show(struct device *dev,
0213                       struct device_attribute *attr,
0214                       char *page)
0215 {
0216     struct backing_dev_info *bdi = dev_get_drvdata(dev);
0217 
0218     return snprintf(page, PAGE_SIZE-1, "%d\n",
0219             bdi_cap_stable_pages_required(bdi) ? 1 : 0);
0220 }
0221 static DEVICE_ATTR_RO(stable_pages_required);
0222 
0223 static struct attribute *bdi_dev_attrs[] = {
0224     &dev_attr_read_ahead_kb.attr,
0225     &dev_attr_min_ratio.attr,
0226     &dev_attr_max_ratio.attr,
0227     &dev_attr_stable_pages_required.attr,
0228     NULL,
0229 };
0230 ATTRIBUTE_GROUPS(bdi_dev);
0231 
0232 static __init int bdi_class_init(void)
0233 {
0234     bdi_class = class_create(THIS_MODULE, "bdi");
0235     if (IS_ERR(bdi_class))
0236         return PTR_ERR(bdi_class);
0237 
0238     bdi_class->dev_groups = bdi_dev_groups;
0239     bdi_debug_init();
0240     return 0;
0241 }
0242 postcore_initcall(bdi_class_init);
0243 
0244 static int __init default_bdi_init(void)
0245 {
0246     int err;
0247 
0248     bdi_wq = alloc_workqueue("writeback", WQ_MEM_RECLAIM | WQ_FREEZABLE |
0249                           WQ_UNBOUND | WQ_SYSFS, 0);
0250     if (!bdi_wq)
0251         return -ENOMEM;
0252 
0253     err = bdi_init(&noop_backing_dev_info);
0254 
0255     return err;
0256 }
0257 subsys_initcall(default_bdi_init);
0258 
0259 /*
0260  * This function is used when the first inode for this wb is marked dirty. It
0261  * wakes-up the corresponding bdi thread which should then take care of the
0262  * periodic background write-out of dirty inodes. Since the write-out would
0263  * starts only 'dirty_writeback_interval' centisecs from now anyway, we just
0264  * set up a timer which wakes the bdi thread up later.
0265  *
0266  * Note, we wouldn't bother setting up the timer, but this function is on the
0267  * fast-path (used by '__mark_inode_dirty()'), so we save few context switches
0268  * by delaying the wake-up.
0269  *
0270  * We have to be careful not to postpone flush work if it is scheduled for
0271  * earlier. Thus we use queue_delayed_work().
0272  */
0273 void wb_wakeup_delayed(struct bdi_writeback *wb)
0274 {
0275     unsigned long timeout;
0276 
0277     timeout = msecs_to_jiffies(dirty_writeback_interval * 10);
0278     spin_lock_bh(&wb->work_lock);
0279     if (test_bit(WB_registered, &wb->state))
0280         queue_delayed_work(bdi_wq, &wb->dwork, timeout);
0281     spin_unlock_bh(&wb->work_lock);
0282 }
0283 
0284 /*
0285  * Initial write bandwidth: 100 MB/s
0286  */
0287 #define INIT_BW     (100 << (20 - PAGE_SHIFT))
0288 
0289 static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
0290            int blkcg_id, gfp_t gfp)
0291 {
0292     int i, err;
0293 
0294     memset(wb, 0, sizeof(*wb));
0295 
0296     wb->bdi = bdi;
0297     wb->last_old_flush = jiffies;
0298     INIT_LIST_HEAD(&wb->b_dirty);
0299     INIT_LIST_HEAD(&wb->b_io);
0300     INIT_LIST_HEAD(&wb->b_more_io);
0301     INIT_LIST_HEAD(&wb->b_dirty_time);
0302     spin_lock_init(&wb->list_lock);
0303 
0304     wb->bw_time_stamp = jiffies;
0305     wb->balanced_dirty_ratelimit = INIT_BW;
0306     wb->dirty_ratelimit = INIT_BW;
0307     wb->write_bandwidth = INIT_BW;
0308     wb->avg_write_bandwidth = INIT_BW;
0309 
0310     spin_lock_init(&wb->work_lock);
0311     INIT_LIST_HEAD(&wb->work_list);
0312     INIT_DELAYED_WORK(&wb->dwork, wb_workfn);
0313     wb->dirty_sleep = jiffies;
0314 
0315     wb->congested = wb_congested_get_create(bdi, blkcg_id, gfp);
0316     if (!wb->congested)
0317         return -ENOMEM;
0318 
0319     err = fprop_local_init_percpu(&wb->completions, gfp);
0320     if (err)
0321         goto out_put_cong;
0322 
0323     for (i = 0; i < NR_WB_STAT_ITEMS; i++) {
0324         err = percpu_counter_init(&wb->stat[i], 0, gfp);
0325         if (err)
0326             goto out_destroy_stat;
0327     }
0328 
0329     return 0;
0330 
0331 out_destroy_stat:
0332     while (i--)
0333         percpu_counter_destroy(&wb->stat[i]);
0334     fprop_local_destroy_percpu(&wb->completions);
0335 out_put_cong:
0336     wb_congested_put(wb->congested);
0337     return err;
0338 }
0339 
0340 /*
0341  * Remove bdi from the global list and shutdown any threads we have running
0342  */
0343 static void wb_shutdown(struct bdi_writeback *wb)
0344 {
0345     /* Make sure nobody queues further work */
0346     spin_lock_bh(&wb->work_lock);
0347     if (!test_and_clear_bit(WB_registered, &wb->state)) {
0348         spin_unlock_bh(&wb->work_lock);
0349         return;
0350     }
0351     spin_unlock_bh(&wb->work_lock);
0352 
0353     /*
0354      * Drain work list and shutdown the delayed_work.  !WB_registered
0355      * tells wb_workfn() that @wb is dying and its work_list needs to
0356      * be drained no matter what.
0357      */
0358     mod_delayed_work(bdi_wq, &wb->dwork, 0);
0359     flush_delayed_work(&wb->dwork);
0360     WARN_ON(!list_empty(&wb->work_list));
0361 }
0362 
0363 static void wb_exit(struct bdi_writeback *wb)
0364 {
0365     int i;
0366 
0367     WARN_ON(delayed_work_pending(&wb->dwork));
0368 
0369     for (i = 0; i < NR_WB_STAT_ITEMS; i++)
0370         percpu_counter_destroy(&wb->stat[i]);
0371 
0372     fprop_local_destroy_percpu(&wb->completions);
0373     wb_congested_put(wb->congested);
0374 }
0375 
0376 #ifdef CONFIG_CGROUP_WRITEBACK
0377 
0378 #include <linux/memcontrol.h>
0379 
0380 /*
0381  * cgwb_lock protects bdi->cgwb_tree, bdi->cgwb_congested_tree,
0382  * blkcg->cgwb_list, and memcg->cgwb_list.  bdi->cgwb_tree is also RCU
0383  * protected.  cgwb_release_wait is used to wait for the completion of cgwb
0384  * releases from bdi destruction path.
0385  */
0386 static DEFINE_SPINLOCK(cgwb_lock);
0387 static DECLARE_WAIT_QUEUE_HEAD(cgwb_release_wait);
0388 
0389 /**
0390  * wb_congested_get_create - get or create a wb_congested
0391  * @bdi: associated bdi
0392  * @blkcg_id: ID of the associated blkcg
0393  * @gfp: allocation mask
0394  *
0395  * Look up the wb_congested for @blkcg_id on @bdi.  If missing, create one.
0396  * The returned wb_congested has its reference count incremented.  Returns
0397  * NULL on failure.
0398  */
0399 struct bdi_writeback_congested *
0400 wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
0401 {
0402     struct bdi_writeback_congested *new_congested = NULL, *congested;
0403     struct rb_node **node, *parent;
0404     unsigned long flags;
0405 retry:
0406     spin_lock_irqsave(&cgwb_lock, flags);
0407 
0408     node = &bdi->cgwb_congested_tree.rb_node;
0409     parent = NULL;
0410 
0411     while (*node != NULL) {
0412         parent = *node;
0413         congested = container_of(parent, struct bdi_writeback_congested,
0414                      rb_node);
0415         if (congested->blkcg_id < blkcg_id)
0416             node = &parent->rb_left;
0417         else if (congested->blkcg_id > blkcg_id)
0418             node = &parent->rb_right;
0419         else
0420             goto found;
0421     }
0422 
0423     if (new_congested) {
0424         /* !found and storage for new one already allocated, insert */
0425         congested = new_congested;
0426         new_congested = NULL;
0427         rb_link_node(&congested->rb_node, parent, node);
0428         rb_insert_color(&congested->rb_node, &bdi->cgwb_congested_tree);
0429         goto found;
0430     }
0431 
0432     spin_unlock_irqrestore(&cgwb_lock, flags);
0433 
0434     /* allocate storage for new one and retry */
0435     new_congested = kzalloc(sizeof(*new_congested), gfp);
0436     if (!new_congested)
0437         return NULL;
0438 
0439     atomic_set(&new_congested->refcnt, 0);
0440     new_congested->bdi = bdi;
0441     new_congested->blkcg_id = blkcg_id;
0442     goto retry;
0443 
0444 found:
0445     atomic_inc(&congested->refcnt);
0446     spin_unlock_irqrestore(&cgwb_lock, flags);
0447     kfree(new_congested);
0448     return congested;
0449 }
0450 
0451 /**
0452  * wb_congested_put - put a wb_congested
0453  * @congested: wb_congested to put
0454  *
0455  * Put @congested and destroy it if the refcnt reaches zero.
0456  */
0457 void wb_congested_put(struct bdi_writeback_congested *congested)
0458 {
0459     unsigned long flags;
0460 
0461     local_irq_save(flags);
0462     if (!atomic_dec_and_lock(&congested->refcnt, &cgwb_lock)) {
0463         local_irq_restore(flags);
0464         return;
0465     }
0466 
0467     /* bdi might already have been destroyed leaving @congested unlinked */
0468     if (congested->bdi) {
0469         rb_erase(&congested->rb_node,
0470              &congested->bdi->cgwb_congested_tree);
0471         congested->bdi = NULL;
0472     }
0473 
0474     spin_unlock_irqrestore(&cgwb_lock, flags);
0475     kfree(congested);
0476 }
0477 
0478 static void cgwb_release_workfn(struct work_struct *work)
0479 {
0480     struct bdi_writeback *wb = container_of(work, struct bdi_writeback,
0481                         release_work);
0482     struct backing_dev_info *bdi = wb->bdi;
0483 
0484     spin_lock_irq(&cgwb_lock);
0485     list_del_rcu(&wb->bdi_node);
0486     spin_unlock_irq(&cgwb_lock);
0487 
0488     wb_shutdown(wb);
0489 
0490     css_put(wb->memcg_css);
0491     css_put(wb->blkcg_css);
0492 
0493     fprop_local_destroy_percpu(&wb->memcg_completions);
0494     percpu_ref_exit(&wb->refcnt);
0495     wb_exit(wb);
0496     kfree_rcu(wb, rcu);
0497 
0498     if (atomic_dec_and_test(&bdi->usage_cnt))
0499         wake_up_all(&cgwb_release_wait);
0500 }
0501 
0502 static void cgwb_release(struct percpu_ref *refcnt)
0503 {
0504     struct bdi_writeback *wb = container_of(refcnt, struct bdi_writeback,
0505                         refcnt);
0506     schedule_work(&wb->release_work);
0507 }
0508 
0509 static void cgwb_kill(struct bdi_writeback *wb)
0510 {
0511     lockdep_assert_held(&cgwb_lock);
0512 
0513     WARN_ON(!radix_tree_delete(&wb->bdi->cgwb_tree, wb->memcg_css->id));
0514     list_del(&wb->memcg_node);
0515     list_del(&wb->blkcg_node);
0516     percpu_ref_kill(&wb->refcnt);
0517 }
0518 
0519 static int cgwb_create(struct backing_dev_info *bdi,
0520                struct cgroup_subsys_state *memcg_css, gfp_t gfp)
0521 {
0522     struct mem_cgroup *memcg;
0523     struct cgroup_subsys_state *blkcg_css;
0524     struct blkcg *blkcg;
0525     struct list_head *memcg_cgwb_list, *blkcg_cgwb_list;
0526     struct bdi_writeback *wb;
0527     unsigned long flags;
0528     int ret = 0;
0529 
0530     memcg = mem_cgroup_from_css(memcg_css);
0531     blkcg_css = cgroup_get_e_css(memcg_css->cgroup, &io_cgrp_subsys);
0532     blkcg = css_to_blkcg(blkcg_css);
0533     memcg_cgwb_list = mem_cgroup_cgwb_list(memcg);
0534     blkcg_cgwb_list = &blkcg->cgwb_list;
0535 
0536     /* look up again under lock and discard on blkcg mismatch */
0537     spin_lock_irqsave(&cgwb_lock, flags);
0538     wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
0539     if (wb && wb->blkcg_css != blkcg_css) {
0540         cgwb_kill(wb);
0541         wb = NULL;
0542     }
0543     spin_unlock_irqrestore(&cgwb_lock, flags);
0544     if (wb)
0545         goto out_put;
0546 
0547     /* need to create a new one */
0548     wb = kmalloc(sizeof(*wb), gfp);
0549     if (!wb)
0550         return -ENOMEM;
0551 
0552     ret = wb_init(wb, bdi, blkcg_css->id, gfp);
0553     if (ret)
0554         goto err_free;
0555 
0556     ret = percpu_ref_init(&wb->refcnt, cgwb_release, 0, gfp);
0557     if (ret)
0558         goto err_wb_exit;
0559 
0560     ret = fprop_local_init_percpu(&wb->memcg_completions, gfp);
0561     if (ret)
0562         goto err_ref_exit;
0563 
0564     wb->memcg_css = memcg_css;
0565     wb->blkcg_css = blkcg_css;
0566     INIT_WORK(&wb->release_work, cgwb_release_workfn);
0567     set_bit(WB_registered, &wb->state);
0568 
0569     /*
0570      * The root wb determines the registered state of the whole bdi and
0571      * memcg_cgwb_list and blkcg_cgwb_list's next pointers indicate
0572      * whether they're still online.  Don't link @wb if any is dead.
0573      * See wb_memcg_offline() and wb_blkcg_offline().
0574      */
0575     ret = -ENODEV;
0576     spin_lock_irqsave(&cgwb_lock, flags);
0577     if (test_bit(WB_registered, &bdi->wb.state) &&
0578         blkcg_cgwb_list->next && memcg_cgwb_list->next) {
0579         /* we might have raced another instance of this function */
0580         ret = radix_tree_insert(&bdi->cgwb_tree, memcg_css->id, wb);
0581         if (!ret) {
0582             atomic_inc(&bdi->usage_cnt);
0583             list_add_tail_rcu(&wb->bdi_node, &bdi->wb_list);
0584             list_add(&wb->memcg_node, memcg_cgwb_list);
0585             list_add(&wb->blkcg_node, blkcg_cgwb_list);
0586             css_get(memcg_css);
0587             css_get(blkcg_css);
0588         }
0589     }
0590     spin_unlock_irqrestore(&cgwb_lock, flags);
0591     if (ret) {
0592         if (ret == -EEXIST)
0593             ret = 0;
0594         goto err_fprop_exit;
0595     }
0596     goto out_put;
0597 
0598 err_fprop_exit:
0599     fprop_local_destroy_percpu(&wb->memcg_completions);
0600 err_ref_exit:
0601     percpu_ref_exit(&wb->refcnt);
0602 err_wb_exit:
0603     wb_exit(wb);
0604 err_free:
0605     kfree(wb);
0606 out_put:
0607     css_put(blkcg_css);
0608     return ret;
0609 }
0610 
0611 /**
0612  * wb_get_create - get wb for a given memcg, create if necessary
0613  * @bdi: target bdi
0614  * @memcg_css: cgroup_subsys_state of the target memcg (must have positive ref)
0615  * @gfp: allocation mask to use
0616  *
0617  * Try to get the wb for @memcg_css on @bdi.  If it doesn't exist, try to
0618  * create one.  The returned wb has its refcount incremented.
0619  *
0620  * This function uses css_get() on @memcg_css and thus expects its refcnt
0621  * to be positive on invocation.  IOW, rcu_read_lock() protection on
0622  * @memcg_css isn't enough.  try_get it before calling this function.
0623  *
0624  * A wb is keyed by its associated memcg.  As blkcg implicitly enables
0625  * memcg on the default hierarchy, memcg association is guaranteed to be
0626  * more specific (equal or descendant to the associated blkcg) and thus can
0627  * identify both the memcg and blkcg associations.
0628  *
0629  * Because the blkcg associated with a memcg may change as blkcg is enabled
0630  * and disabled closer to root in the hierarchy, each wb keeps track of
0631  * both the memcg and blkcg associated with it and verifies the blkcg on
0632  * each lookup.  On mismatch, the existing wb is discarded and a new one is
0633  * created.
0634  */
0635 struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
0636                     struct cgroup_subsys_state *memcg_css,
0637                     gfp_t gfp)
0638 {
0639     struct bdi_writeback *wb;
0640 
0641     might_sleep_if(gfpflags_allow_blocking(gfp));
0642 
0643     if (!memcg_css->parent)
0644         return &bdi->wb;
0645 
0646     do {
0647         rcu_read_lock();
0648         wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
0649         if (wb) {
0650             struct cgroup_subsys_state *blkcg_css;
0651 
0652             /* see whether the blkcg association has changed */
0653             blkcg_css = cgroup_get_e_css(memcg_css->cgroup,
0654                              &io_cgrp_subsys);
0655             if (unlikely(wb->blkcg_css != blkcg_css ||
0656                      !wb_tryget(wb)))
0657                 wb = NULL;
0658             css_put(blkcg_css);
0659         }
0660         rcu_read_unlock();
0661     } while (!wb && !cgwb_create(bdi, memcg_css, gfp));
0662 
0663     return wb;
0664 }
0665 
0666 static int cgwb_bdi_init(struct backing_dev_info *bdi)
0667 {
0668     int ret;
0669 
0670     INIT_RADIX_TREE(&bdi->cgwb_tree, GFP_ATOMIC);
0671     bdi->cgwb_congested_tree = RB_ROOT;
0672     atomic_set(&bdi->usage_cnt, 1);
0673 
0674     ret = wb_init(&bdi->wb, bdi, 1, GFP_KERNEL);
0675     if (!ret) {
0676         bdi->wb.memcg_css = &root_mem_cgroup->css;
0677         bdi->wb.blkcg_css = blkcg_root_css;
0678     }
0679     return ret;
0680 }
0681 
0682 static void cgwb_bdi_destroy(struct backing_dev_info *bdi)
0683 {
0684     struct radix_tree_iter iter;
0685     struct rb_node *rbn;
0686     void **slot;
0687 
0688     WARN_ON(test_bit(WB_registered, &bdi->wb.state));
0689 
0690     spin_lock_irq(&cgwb_lock);
0691 
0692     radix_tree_for_each_slot(slot, &bdi->cgwb_tree, &iter, 0)
0693         cgwb_kill(*slot);
0694 
0695     while ((rbn = rb_first(&bdi->cgwb_congested_tree))) {
0696         struct bdi_writeback_congested *congested =
0697             rb_entry(rbn, struct bdi_writeback_congested, rb_node);
0698 
0699         rb_erase(rbn, &bdi->cgwb_congested_tree);
0700         congested->bdi = NULL;  /* mark @congested unlinked */
0701     }
0702 
0703     spin_unlock_irq(&cgwb_lock);
0704 
0705     /*
0706      * All cgwb's and their congested states must be shutdown and
0707      * released before returning.  Drain the usage counter to wait for
0708      * all cgwb's and cgwb_congested's ever created on @bdi.
0709      */
0710     atomic_dec(&bdi->usage_cnt);
0711     wait_event(cgwb_release_wait, !atomic_read(&bdi->usage_cnt));
0712 }
0713 
0714 /**
0715  * wb_memcg_offline - kill all wb's associated with a memcg being offlined
0716  * @memcg: memcg being offlined
0717  *
0718  * Also prevents creation of any new wb's associated with @memcg.
0719  */
0720 void wb_memcg_offline(struct mem_cgroup *memcg)
0721 {
0722     LIST_HEAD(to_destroy);
0723     struct list_head *memcg_cgwb_list = mem_cgroup_cgwb_list(memcg);
0724     struct bdi_writeback *wb, *next;
0725 
0726     spin_lock_irq(&cgwb_lock);
0727     list_for_each_entry_safe(wb, next, memcg_cgwb_list, memcg_node)
0728         cgwb_kill(wb);
0729     memcg_cgwb_list->next = NULL;   /* prevent new wb's */
0730     spin_unlock_irq(&cgwb_lock);
0731 }
0732 
0733 /**
0734  * wb_blkcg_offline - kill all wb's associated with a blkcg being offlined
0735  * @blkcg: blkcg being offlined
0736  *
0737  * Also prevents creation of any new wb's associated with @blkcg.
0738  */
0739 void wb_blkcg_offline(struct blkcg *blkcg)
0740 {
0741     LIST_HEAD(to_destroy);
0742     struct bdi_writeback *wb, *next;
0743 
0744     spin_lock_irq(&cgwb_lock);
0745     list_for_each_entry_safe(wb, next, &blkcg->cgwb_list, blkcg_node)
0746         cgwb_kill(wb);
0747     blkcg->cgwb_list.next = NULL;   /* prevent new wb's */
0748     spin_unlock_irq(&cgwb_lock);
0749 }
0750 
0751 #else   /* CONFIG_CGROUP_WRITEBACK */
0752 
0753 static int cgwb_bdi_init(struct backing_dev_info *bdi)
0754 {
0755     int err;
0756 
0757     bdi->wb_congested = kzalloc(sizeof(*bdi->wb_congested), GFP_KERNEL);
0758     if (!bdi->wb_congested)
0759         return -ENOMEM;
0760 
0761     err = wb_init(&bdi->wb, bdi, 1, GFP_KERNEL);
0762     if (err) {
0763         kfree(bdi->wb_congested);
0764         return err;
0765     }
0766     return 0;
0767 }
0768 
0769 static void cgwb_bdi_destroy(struct backing_dev_info *bdi) { }
0770 
0771 #endif  /* CONFIG_CGROUP_WRITEBACK */
0772 
0773 int bdi_init(struct backing_dev_info *bdi)
0774 {
0775     int ret;
0776 
0777     bdi->dev = NULL;
0778 
0779     bdi->min_ratio = 0;
0780     bdi->max_ratio = 100;
0781     bdi->max_prop_frac = FPROP_FRAC_BASE;
0782     INIT_LIST_HEAD(&bdi->bdi_list);
0783     INIT_LIST_HEAD(&bdi->wb_list);
0784     init_waitqueue_head(&bdi->wb_waitq);
0785 
0786     ret = cgwb_bdi_init(bdi);
0787 
0788     list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
0789 
0790     return ret;
0791 }
0792 EXPORT_SYMBOL(bdi_init);
0793 
0794 int bdi_register(struct backing_dev_info *bdi, struct device *parent,
0795         const char *fmt, ...)
0796 {
0797     va_list args;
0798     struct device *dev;
0799 
0800     if (bdi->dev)   /* The driver needs to use separate queues per device */
0801         return 0;
0802 
0803     va_start(args, fmt);
0804     dev = device_create_vargs(bdi_class, parent, MKDEV(0, 0), bdi, fmt, args);
0805     va_end(args);
0806     if (IS_ERR(dev))
0807         return PTR_ERR(dev);
0808 
0809     bdi->dev = dev;
0810 
0811     bdi_debug_register(bdi, dev_name(dev));
0812     set_bit(WB_registered, &bdi->wb.state);
0813 
0814     spin_lock_bh(&bdi_lock);
0815     list_add_tail_rcu(&bdi->bdi_list, &bdi_list);
0816     spin_unlock_bh(&bdi_lock);
0817 
0818     trace_writeback_bdi_register(bdi);
0819     return 0;
0820 }
0821 EXPORT_SYMBOL(bdi_register);
0822 
0823 int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev)
0824 {
0825     return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev));
0826 }
0827 EXPORT_SYMBOL(bdi_register_dev);
0828 
0829 int bdi_register_owner(struct backing_dev_info *bdi, struct device *owner)
0830 {
0831     int rc;
0832 
0833     rc = bdi_register(bdi, NULL, "%u:%u", MAJOR(owner->devt),
0834             MINOR(owner->devt));
0835     if (rc)
0836         return rc;
0837     bdi->owner = owner;
0838     get_device(owner);
0839     return 0;
0840 }
0841 EXPORT_SYMBOL(bdi_register_owner);
0842 
0843 /*
0844  * Remove bdi from bdi_list, and ensure that it is no longer visible
0845  */
0846 static void bdi_remove_from_list(struct backing_dev_info *bdi)
0847 {
0848     spin_lock_bh(&bdi_lock);
0849     list_del_rcu(&bdi->bdi_list);
0850     spin_unlock_bh(&bdi_lock);
0851 
0852     synchronize_rcu_expedited();
0853 }
0854 
0855 void bdi_unregister(struct backing_dev_info *bdi)
0856 {
0857     /* make sure nobody finds us on the bdi_list anymore */
0858     bdi_remove_from_list(bdi);
0859     wb_shutdown(&bdi->wb);
0860     cgwb_bdi_destroy(bdi);
0861 
0862     if (bdi->dev) {
0863         bdi_debug_unregister(bdi);
0864         device_unregister(bdi->dev);
0865         bdi->dev = NULL;
0866     }
0867 
0868     if (bdi->owner) {
0869         put_device(bdi->owner);
0870         bdi->owner = NULL;
0871     }
0872 }
0873 
0874 void bdi_exit(struct backing_dev_info *bdi)
0875 {
0876     WARN_ON_ONCE(bdi->dev);
0877     wb_exit(&bdi->wb);
0878 }
0879 
0880 void bdi_destroy(struct backing_dev_info *bdi)
0881 {
0882     bdi_unregister(bdi);
0883     bdi_exit(bdi);
0884 }
0885 EXPORT_SYMBOL(bdi_destroy);
0886 
0887 /*
0888  * For use from filesystems to quickly init and register a bdi associated
0889  * with dirty writeback
0890  */
0891 int bdi_setup_and_register(struct backing_dev_info *bdi, char *name)
0892 {
0893     int err;
0894 
0895     bdi->name = name;
0896     bdi->capabilities = 0;
0897     err = bdi_init(bdi);
0898     if (err)
0899         return err;
0900 
0901     err = bdi_register(bdi, NULL, "%.28s-%ld", name,
0902                atomic_long_inc_return(&bdi_seq));
0903     if (err) {
0904         bdi_destroy(bdi);
0905         return err;
0906     }
0907 
0908     return 0;
0909 }
0910 EXPORT_SYMBOL(bdi_setup_and_register);
0911 
0912 static wait_queue_head_t congestion_wqh[2] = {
0913         __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
0914         __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
0915     };
0916 static atomic_t nr_wb_congested[2];
0917 
0918 void clear_wb_congested(struct bdi_writeback_congested *congested, int sync)
0919 {
0920     wait_queue_head_t *wqh = &congestion_wqh[sync];
0921     enum wb_congested_state bit;
0922 
0923     bit = sync ? WB_sync_congested : WB_async_congested;
0924     if (test_and_clear_bit(bit, &congested->state))
0925         atomic_dec(&nr_wb_congested[sync]);
0926     smp_mb__after_atomic();
0927     if (waitqueue_active(wqh))
0928         wake_up(wqh);
0929 }
0930 EXPORT_SYMBOL(clear_wb_congested);
0931 
0932 void set_wb_congested(struct bdi_writeback_congested *congested, int sync)
0933 {
0934     enum wb_congested_state bit;
0935 
0936     bit = sync ? WB_sync_congested : WB_async_congested;
0937     if (!test_and_set_bit(bit, &congested->state))
0938         atomic_inc(&nr_wb_congested[sync]);
0939 }
0940 EXPORT_SYMBOL(set_wb_congested);
0941 
0942 /**
0943  * congestion_wait - wait for a backing_dev to become uncongested
0944  * @sync: SYNC or ASYNC IO
0945  * @timeout: timeout in jiffies
0946  *
0947  * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
0948  * write congestion.  If no backing_devs are congested then just wait for the
0949  * next write to be completed.
0950  */
0951 long congestion_wait(int sync, long timeout)
0952 {
0953     long ret;
0954     unsigned long start = jiffies;
0955     DEFINE_WAIT(wait);
0956     wait_queue_head_t *wqh = &congestion_wqh[sync];
0957 
0958     prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
0959     ret = io_schedule_timeout(timeout);
0960     finish_wait(wqh, &wait);
0961 
0962     trace_writeback_congestion_wait(jiffies_to_usecs(timeout),
0963                     jiffies_to_usecs(jiffies - start));
0964 
0965     return ret;
0966 }
0967 EXPORT_SYMBOL(congestion_wait);
0968 
0969 /**
0970  * wait_iff_congested - Conditionally wait for a backing_dev to become uncongested or a pgdat to complete writes
0971  * @pgdat: A pgdat to check if it is heavily congested
0972  * @sync: SYNC or ASYNC IO
0973  * @timeout: timeout in jiffies
0974  *
0975  * In the event of a congested backing_dev (any backing_dev) and the given
0976  * @pgdat has experienced recent congestion, this waits for up to @timeout
0977  * jiffies for either a BDI to exit congestion of the given @sync queue
0978  * or a write to complete.
0979  *
0980  * In the absence of pgdat congestion, cond_resched() is called to yield
0981  * the processor if necessary but otherwise does not sleep.
0982  *
0983  * The return value is 0 if the sleep is for the full timeout. Otherwise,
0984  * it is the number of jiffies that were still remaining when the function
0985  * returned. return_value == timeout implies the function did not sleep.
0986  */
0987 long wait_iff_congested(struct pglist_data *pgdat, int sync, long timeout)
0988 {
0989     long ret;
0990     unsigned long start = jiffies;
0991     DEFINE_WAIT(wait);
0992     wait_queue_head_t *wqh = &congestion_wqh[sync];
0993 
0994     /*
0995      * If there is no congestion, or heavy congestion is not being
0996      * encountered in the current pgdat, yield if necessary instead
0997      * of sleeping on the congestion queue
0998      */
0999     if (atomic_read(&nr_wb_congested[sync]) == 0 ||
1000         !test_bit(PGDAT_CONGESTED, &pgdat->flags)) {
1001         cond_resched();
1002 
1003         /* In case we scheduled, work out time remaining */
1004         ret = timeout - (jiffies - start);
1005         if (ret < 0)
1006             ret = 0;
1007 
1008         goto out;
1009     }
1010 
1011     /* Sleep until uncongested or a write happens */
1012     prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
1013     ret = io_schedule_timeout(timeout);
1014     finish_wait(wqh, &wait);
1015 
1016 out:
1017     trace_writeback_wait_iff_congested(jiffies_to_usecs(timeout),
1018                     jiffies_to_usecs(jiffies - start));
1019 
1020     return ret;
1021 }
1022 EXPORT_SYMBOL(wait_iff_congested);
1023 
1024 int pdflush_proc_obsolete(struct ctl_table *table, int write,
1025             void __user *buffer, size_t *lenp, loff_t *ppos)
1026 {
1027     char kbuf[] = "0\n";
1028 
1029     if (*ppos || *lenp < sizeof(kbuf)) {
1030         *lenp = 0;
1031         return 0;
1032     }
1033 
1034     if (copy_to_user(buffer, kbuf, sizeof(kbuf)))
1035         return -EFAULT;
1036     pr_warn_once("%s exported in /proc is scheduled for removal\n",
1037              table->procname);
1038 
1039     *lenp = 2;
1040     *ppos += *lenp;
1041     return 2;
1042 }