backing-dev.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/wait.h>
  3. #include <linux/rbtree.h>
  4. #include <linux/backing-dev.h>
  5. #include <linux/kthread.h>
  6. #include <linux/freezer.h>
  7. #include <linux/fs.h>
  8. #include <linux/pagemap.h>
  9. #include <linux/mm.h>
  10. #include <linux/sched.h>
  11. #include <linux/module.h>
  12. #include <linux/writeback.h>
  13. #include <linux/device.h>
  14. #include <trace/events/writeback.h>
  15. struct backing_dev_info noop_backing_dev_info;
  16. EXPORT_SYMBOL_GPL(noop_backing_dev_info);
  17. static struct class *bdi_class;
  18. static const char *bdi_unknown_name = "(unknown)";
  19. /*
  20. * bdi_lock protects bdi_tree and updates to bdi_list. bdi_list has RCU
  21. * reader side locking.
  22. */
  23. DEFINE_SPINLOCK(bdi_lock);
  24. static u64 bdi_id_cursor;
  25. static struct rb_root bdi_tree = RB_ROOT;
  26. LIST_HEAD(bdi_list);
  27. /* bdi_wq serves all asynchronous writeback tasks */
  28. struct workqueue_struct *bdi_wq;
  29. #ifdef CONFIG_DEBUG_FS
  30. #include <linux/debugfs.h>
  31. #include <linux/seq_file.h>
  32. static struct dentry *bdi_debug_root;
  33. static void bdi_debug_init(void)
  34. {
  35. bdi_debug_root = debugfs_create_dir("bdi", NULL);
  36. }
  37. static int bdi_debug_stats_show(struct seq_file *m, void *v)
  38. {
  39. struct backing_dev_info *bdi = m->private;
  40. struct bdi_writeback *wb = &bdi->wb;
  41. unsigned long background_thresh;
  42. unsigned long dirty_thresh;
  43. unsigned long wb_thresh;
  44. unsigned long nr_dirty, nr_io, nr_more_io, nr_dirty_time;
  45. struct inode *inode;
  46. nr_dirty = nr_io = nr_more_io = nr_dirty_time = 0;
  47. spin_lock(&wb->list_lock);
  48. list_for_each_entry(inode, &wb->b_dirty, i_io_list)
  49. nr_dirty++;
  50. list_for_each_entry(inode, &wb->b_io, i_io_list)
  51. nr_io++;
  52. list_for_each_entry(inode, &wb->b_more_io, i_io_list)
  53. nr_more_io++;
  54. list_for_each_entry(inode, &wb->b_dirty_time, i_io_list)
  55. if (inode->i_state & I_DIRTY_TIME)
  56. nr_dirty_time++;
  57. spin_unlock(&wb->list_lock);
  58. global_dirty_limits(&background_thresh, &dirty_thresh);
  59. wb_thresh = wb_calc_thresh(wb, dirty_thresh);
  60. #define K(x) ((x) << (PAGE_SHIFT - 10))
  61. seq_printf(m,
  62. "BdiWriteback: %10lu kB\n"
  63. "BdiReclaimable: %10lu kB\n"
  64. "BdiDirtyThresh: %10lu kB\n"
  65. "DirtyThresh: %10lu kB\n"
  66. "BackgroundThresh: %10lu kB\n"
  67. "BdiDirtied: %10lu kB\n"
  68. "BdiWritten: %10lu kB\n"
  69. "BdiWriteBandwidth: %10lu kBps\n"
  70. "b_dirty: %10lu\n"
  71. "b_io: %10lu\n"
  72. "b_more_io: %10lu\n"
  73. "b_dirty_time: %10lu\n"
  74. "bdi_list: %10u\n"
  75. "state: %10lx\n",
  76. (unsigned long) K(wb_stat(wb, WB_WRITEBACK)),
  77. (unsigned long) K(wb_stat(wb, WB_RECLAIMABLE)),
  78. K(wb_thresh),
  79. K(dirty_thresh),
  80. K(background_thresh),
  81. (unsigned long) K(wb_stat(wb, WB_DIRTIED)),
  82. (unsigned long) K(wb_stat(wb, WB_WRITTEN)),
  83. (unsigned long) K(wb->write_bandwidth),
  84. nr_dirty,
  85. nr_io,
  86. nr_more_io,
  87. nr_dirty_time,
  88. !list_empty(&bdi->bdi_list), bdi->wb.state);
  89. #undef K
  90. return 0;
  91. }
  92. DEFINE_SHOW_ATTRIBUTE(bdi_debug_stats);
  93. static void bdi_debug_register(struct backing_dev_info *bdi, const char *name)
  94. {
  95. bdi->debug_dir = debugfs_create_dir(name, bdi_debug_root);
  96. debugfs_create_file("stats", 0444, bdi->debug_dir, bdi,
  97. &bdi_debug_stats_fops);
  98. }
  99. static void bdi_debug_unregister(struct backing_dev_info *bdi)
  100. {
  101. debugfs_remove_recursive(bdi->debug_dir);
  102. }
  103. #else
  104. static inline void bdi_debug_init(void)
  105. {
  106. }
  107. static inline void bdi_debug_register(struct backing_dev_info *bdi,
  108. const char *name)
  109. {
  110. }
  111. static inline void bdi_debug_unregister(struct backing_dev_info *bdi)
  112. {
  113. }
  114. #endif
  115. static ssize_t read_ahead_kb_store(struct device *dev,
  116. struct device_attribute *attr,
  117. const char *buf, size_t count)
  118. {
  119. struct backing_dev_info *bdi = dev_get_drvdata(dev);
  120. unsigned long read_ahead_kb;
  121. ssize_t ret;
  122. ret = kstrtoul(buf, 10, &read_ahead_kb);
  123. if (ret < 0)
  124. return ret;
  125. bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10);
  126. return count;
  127. }
  128. #define K(pages) ((pages) << (PAGE_SHIFT - 10))
  129. #define BDI_SHOW(name, expr) \
  130. static ssize_t name##_show(struct device *dev, \
  131. struct device_attribute *attr, char *page) \
  132. { \
  133. struct backing_dev_info *bdi = dev_get_drvdata(dev); \
  134. \
  135. return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr); \
  136. } \
  137. static DEVICE_ATTR_RW(name);
  138. BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
  139. static ssize_t min_ratio_store(struct device *dev,
  140. struct device_attribute *attr, const char *buf, size_t count)
  141. {
  142. struct backing_dev_info *bdi = dev_get_drvdata(dev);
  143. unsigned int ratio;
  144. ssize_t ret;
  145. ret = kstrtouint(buf, 10, &ratio);
  146. if (ret < 0)
  147. return ret;
  148. ret = bdi_set_min_ratio(bdi, ratio);
  149. if (!ret)
  150. ret = count;
  151. return ret;
  152. }
  153. BDI_SHOW(min_ratio, bdi->min_ratio)
  154. static ssize_t max_ratio_store(struct device *dev,
  155. struct device_attribute *attr, const char *buf, size_t count)
  156. {
  157. struct backing_dev_info *bdi = dev_get_drvdata(dev);
  158. unsigned int ratio;
  159. ssize_t ret;
  160. ret = kstrtouint(buf, 10, &ratio);
  161. if (ret < 0)
  162. return ret;
  163. ret = bdi_set_max_ratio(bdi, ratio);
  164. if (!ret)
  165. ret = count;
  166. return ret;
  167. }
  168. BDI_SHOW(max_ratio, bdi->max_ratio)
  169. static ssize_t stable_pages_required_show(struct device *dev,
  170. struct device_attribute *attr,
  171. char *page)
  172. {
  173. dev_warn_once(dev,
  174. "the stable_pages_required attribute has been removed. Use the stable_writes queue attribute instead.\n");
  175. return snprintf(page, PAGE_SIZE-1, "%d\n", 0);
  176. }
  177. static DEVICE_ATTR_RO(stable_pages_required);
  178. static struct attribute *bdi_dev_attrs[] = {
  179. &dev_attr_read_ahead_kb.attr,
  180. &dev_attr_min_ratio.attr,
  181. &dev_attr_max_ratio.attr,
  182. &dev_attr_stable_pages_required.attr,
  183. NULL,
  184. };
  185. ATTRIBUTE_GROUPS(bdi_dev);
  186. static __init int bdi_class_init(void)
  187. {
  188. bdi_class = class_create(THIS_MODULE, "bdi");
  189. if (IS_ERR(bdi_class))
  190. return PTR_ERR(bdi_class);
  191. bdi_class->dev_groups = bdi_dev_groups;
  192. bdi_debug_init();
  193. return 0;
  194. }
  195. postcore_initcall(bdi_class_init);
  196. static int bdi_init(struct backing_dev_info *bdi);
  197. static int __init default_bdi_init(void)
  198. {
  199. int err;
  200. bdi_wq = alloc_workqueue("writeback", WQ_MEM_RECLAIM | WQ_UNBOUND |
  201. WQ_SYSFS, 0);
  202. if (!bdi_wq)
  203. return -ENOMEM;
  204. err = bdi_init(&noop_backing_dev_info);
  205. return err;
  206. }
  207. subsys_initcall(default_bdi_init);
  208. /*
  209. * This function is used when the first inode for this wb is marked dirty. It
  210. * wakes-up the corresponding bdi thread which should then take care of the
  211. * periodic background write-out of dirty inodes. Since the write-out would
  212. * starts only 'dirty_writeback_interval' centisecs from now anyway, we just
  213. * set up a timer which wakes the bdi thread up later.
  214. *
  215. * Note, we wouldn't bother setting up the timer, but this function is on the
  216. * fast-path (used by '__mark_inode_dirty()'), so we save few context switches
  217. * by delaying the wake-up.
  218. *
  219. * We have to be careful not to postpone flush work if it is scheduled for
  220. * earlier. Thus we use queue_delayed_work().
  221. */
  222. void wb_wakeup_delayed(struct bdi_writeback *wb)
  223. {
  224. unsigned long timeout;
  225. timeout = msecs_to_jiffies(dirty_writeback_interval * 10);
  226. spin_lock_bh(&wb->work_lock);
  227. if (test_bit(WB_registered, &wb->state))
  228. queue_delayed_work(bdi_wq, &wb->dwork, timeout);
  229. spin_unlock_bh(&wb->work_lock);
  230. }
  231. /*
  232. * Initial write bandwidth: 100 MB/s
  233. */
  234. #define INIT_BW (100 << (20 - PAGE_SHIFT))
  235. static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
  236. gfp_t gfp)
  237. {
  238. int i, err;
  239. memset(wb, 0, sizeof(*wb));
  240. if (wb != &bdi->wb)
  241. bdi_get(bdi);
  242. wb->bdi = bdi;
  243. wb->last_old_flush = jiffies;
  244. INIT_LIST_HEAD(&wb->b_dirty);
  245. INIT_LIST_HEAD(&wb->b_io);
  246. INIT_LIST_HEAD(&wb->b_more_io);
  247. INIT_LIST_HEAD(&wb->b_dirty_time);
  248. spin_lock_init(&wb->list_lock);
  249. wb->bw_time_stamp = jiffies;
  250. wb->balanced_dirty_ratelimit = INIT_BW;
  251. wb->dirty_ratelimit = INIT_BW;
  252. wb->write_bandwidth = INIT_BW;
  253. wb->avg_write_bandwidth = INIT_BW;
  254. spin_lock_init(&wb->work_lock);
  255. INIT_LIST_HEAD(&wb->work_list);
  256. INIT_DELAYED_WORK(&wb->dwork, wb_workfn);
  257. wb->dirty_sleep = jiffies;
  258. err = fprop_local_init_percpu(&wb->completions, gfp);
  259. if (err)
  260. goto out_put_bdi;
  261. for (i = 0; i < NR_WB_STAT_ITEMS; i++) {
  262. err = percpu_counter_init(&wb->stat[i], 0, gfp);
  263. if (err)
  264. goto out_destroy_stat;
  265. }
  266. return 0;
  267. out_destroy_stat:
  268. while (i--)
  269. percpu_counter_destroy(&wb->stat[i]);
  270. fprop_local_destroy_percpu(&wb->completions);
  271. out_put_bdi:
  272. if (wb != &bdi->wb)
  273. bdi_put(bdi);
  274. return err;
  275. }
  276. static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb);
  277. /*
  278. * Remove bdi from the global list and shutdown any threads we have running
  279. */
  280. static void wb_shutdown(struct bdi_writeback *wb)
  281. {
  282. /* Make sure nobody queues further work */
  283. spin_lock_bh(&wb->work_lock);
  284. if (!test_and_clear_bit(WB_registered, &wb->state)) {
  285. spin_unlock_bh(&wb->work_lock);
  286. return;
  287. }
  288. spin_unlock_bh(&wb->work_lock);
  289. cgwb_remove_from_bdi_list(wb);
  290. /*
  291. * Drain work list and shutdown the delayed_work. !WB_registered
  292. * tells wb_workfn() that @wb is dying and its work_list needs to
  293. * be drained no matter what.
  294. */
  295. mod_delayed_work(bdi_wq, &wb->dwork, 0);
  296. flush_delayed_work(&wb->dwork);
  297. WARN_ON(!list_empty(&wb->work_list));
  298. }
  299. static void wb_exit(struct bdi_writeback *wb)
  300. {
  301. int i;
  302. WARN_ON(delayed_work_pending(&wb->dwork));
  303. for (i = 0; i < NR_WB_STAT_ITEMS; i++)
  304. percpu_counter_destroy(&wb->stat[i]);
  305. fprop_local_destroy_percpu(&wb->completions);
  306. if (wb != &wb->bdi->wb)
  307. bdi_put(wb->bdi);
  308. }
  309. #ifdef CONFIG_CGROUP_WRITEBACK
  310. #include <linux/memcontrol.h>
  311. /*
  312. * cgwb_lock protects bdi->cgwb_tree, blkcg->cgwb_list, and memcg->cgwb_list.
  313. * bdi->cgwb_tree is also RCU protected.
  314. */
  315. static DEFINE_SPINLOCK(cgwb_lock);
  316. static struct workqueue_struct *cgwb_release_wq;
  317. static void cgwb_release_workfn(struct work_struct *work)
  318. {
  319. struct bdi_writeback *wb = container_of(work, struct bdi_writeback,
  320. release_work);
  321. struct blkcg *blkcg = css_to_blkcg(wb->blkcg_css);
  322. mutex_lock(&wb->bdi->cgwb_release_mutex);
  323. wb_shutdown(wb);
  324. css_put(wb->memcg_css);
  325. css_put(wb->blkcg_css);
  326. mutex_unlock(&wb->bdi->cgwb_release_mutex);
  327. /* triggers blkg destruction if no online users left */
  328. blkcg_unpin_online(blkcg);
  329. fprop_local_destroy_percpu(&wb->memcg_completions);
  330. percpu_ref_exit(&wb->refcnt);
  331. wb_exit(wb);
  332. kfree_rcu(wb, rcu);
  333. }
  334. static void cgwb_release(struct percpu_ref *refcnt)
  335. {
  336. struct bdi_writeback *wb = container_of(refcnt, struct bdi_writeback,
  337. refcnt);
  338. queue_work(cgwb_release_wq, &wb->release_work);
  339. }
  340. static void cgwb_kill(struct bdi_writeback *wb)
  341. {
  342. lockdep_assert_held(&cgwb_lock);
  343. WARN_ON(!radix_tree_delete(&wb->bdi->cgwb_tree, wb->memcg_css->id));
  344. list_del(&wb->memcg_node);
  345. list_del(&wb->blkcg_node);
  346. percpu_ref_kill(&wb->refcnt);
  347. }
  348. static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb)
  349. {
  350. spin_lock_irq(&cgwb_lock);
  351. list_del_rcu(&wb->bdi_node);
  352. spin_unlock_irq(&cgwb_lock);
  353. }
  354. static int cgwb_create(struct backing_dev_info *bdi,
  355. struct cgroup_subsys_state *memcg_css, gfp_t gfp)
  356. {
  357. struct mem_cgroup *memcg;
  358. struct cgroup_subsys_state *blkcg_css;
  359. struct blkcg *blkcg;
  360. struct list_head *memcg_cgwb_list, *blkcg_cgwb_list;
  361. struct bdi_writeback *wb;
  362. unsigned long flags;
  363. int ret = 0;
  364. memcg = mem_cgroup_from_css(memcg_css);
  365. blkcg_css = cgroup_get_e_css(memcg_css->cgroup, &io_cgrp_subsys);
  366. blkcg = css_to_blkcg(blkcg_css);
  367. memcg_cgwb_list = &memcg->cgwb_list;
  368. blkcg_cgwb_list = &blkcg->cgwb_list;
  369. /* look up again under lock and discard on blkcg mismatch */
  370. spin_lock_irqsave(&cgwb_lock, flags);
  371. wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
  372. if (wb && wb->blkcg_css != blkcg_css) {
  373. cgwb_kill(wb);
  374. wb = NULL;
  375. }
  376. spin_unlock_irqrestore(&cgwb_lock, flags);
  377. if (wb)
  378. goto out_put;
  379. /* need to create a new one */
  380. wb = kmalloc(sizeof(*wb), gfp);
  381. if (!wb) {
  382. ret = -ENOMEM;
  383. goto out_put;
  384. }
  385. ret = wb_init(wb, bdi, gfp);
  386. if (ret)
  387. goto err_free;
  388. ret = percpu_ref_init(&wb->refcnt, cgwb_release, 0, gfp);
  389. if (ret)
  390. goto err_wb_exit;
  391. ret = fprop_local_init_percpu(&wb->memcg_completions, gfp);
  392. if (ret)
  393. goto err_ref_exit;
  394. wb->memcg_css = memcg_css;
  395. wb->blkcg_css = blkcg_css;
  396. INIT_WORK(&wb->release_work, cgwb_release_workfn);
  397. set_bit(WB_registered, &wb->state);
  398. /*
  399. * The root wb determines the registered state of the whole bdi and
  400. * memcg_cgwb_list and blkcg_cgwb_list's next pointers indicate
  401. * whether they're still online. Don't link @wb if any is dead.
  402. * See wb_memcg_offline() and wb_blkcg_offline().
  403. */
  404. ret = -ENODEV;
  405. spin_lock_irqsave(&cgwb_lock, flags);
  406. if (test_bit(WB_registered, &bdi->wb.state) &&
  407. blkcg_cgwb_list->next && memcg_cgwb_list->next) {
  408. /* we might have raced another instance of this function */
  409. ret = radix_tree_insert(&bdi->cgwb_tree, memcg_css->id, wb);
  410. if (!ret) {
  411. list_add_tail_rcu(&wb->bdi_node, &bdi->wb_list);
  412. list_add(&wb->memcg_node, memcg_cgwb_list);
  413. list_add(&wb->blkcg_node, blkcg_cgwb_list);
  414. blkcg_pin_online(blkcg);
  415. css_get(memcg_css);
  416. css_get(blkcg_css);
  417. }
  418. }
  419. spin_unlock_irqrestore(&cgwb_lock, flags);
  420. if (ret) {
  421. if (ret == -EEXIST)
  422. ret = 0;
  423. goto err_fprop_exit;
  424. }
  425. goto out_put;
  426. err_fprop_exit:
  427. fprop_local_destroy_percpu(&wb->memcg_completions);
  428. err_ref_exit:
  429. percpu_ref_exit(&wb->refcnt);
  430. err_wb_exit:
  431. wb_exit(wb);
  432. err_free:
  433. kfree(wb);
  434. out_put:
  435. css_put(blkcg_css);
  436. return ret;
  437. }
  438. /**
  439. * wb_get_lookup - get wb for a given memcg
  440. * @bdi: target bdi
  441. * @memcg_css: cgroup_subsys_state of the target memcg (must have positive ref)
  442. *
  443. * Try to get the wb for @memcg_css on @bdi. The returned wb has its
  444. * refcount incremented.
  445. *
  446. * This function uses css_get() on @memcg_css and thus expects its refcnt
  447. * to be positive on invocation. IOW, rcu_read_lock() protection on
  448. * @memcg_css isn't enough. try_get it before calling this function.
  449. *
  450. * A wb is keyed by its associated memcg. As blkcg implicitly enables
  451. * memcg on the default hierarchy, memcg association is guaranteed to be
  452. * more specific (equal or descendant to the associated blkcg) and thus can
  453. * identify both the memcg and blkcg associations.
  454. *
  455. * Because the blkcg associated with a memcg may change as blkcg is enabled
  456. * and disabled closer to root in the hierarchy, each wb keeps track of
  457. * both the memcg and blkcg associated with it and verifies the blkcg on
  458. * each lookup. On mismatch, the existing wb is discarded and a new one is
  459. * created.
  460. */
  461. struct bdi_writeback *wb_get_lookup(struct backing_dev_info *bdi,
  462. struct cgroup_subsys_state *memcg_css)
  463. {
  464. struct bdi_writeback *wb;
  465. if (!memcg_css->parent)
  466. return &bdi->wb;
  467. rcu_read_lock();
  468. wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
  469. if (wb) {
  470. struct cgroup_subsys_state *blkcg_css;
  471. /* see whether the blkcg association has changed */
  472. blkcg_css = cgroup_get_e_css(memcg_css->cgroup, &io_cgrp_subsys);
  473. if (unlikely(wb->blkcg_css != blkcg_css || !wb_tryget(wb)))
  474. wb = NULL;
  475. css_put(blkcg_css);
  476. }
  477. rcu_read_unlock();
  478. return wb;
  479. }
  480. /**
  481. * wb_get_create - get wb for a given memcg, create if necessary
  482. * @bdi: target bdi
  483. * @memcg_css: cgroup_subsys_state of the target memcg (must have positive ref)
  484. * @gfp: allocation mask to use
  485. *
  486. * Try to get the wb for @memcg_css on @bdi. If it doesn't exist, try to
  487. * create one. See wb_get_lookup() for more details.
  488. */
  489. struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
  490. struct cgroup_subsys_state *memcg_css,
  491. gfp_t gfp)
  492. {
  493. struct bdi_writeback *wb;
  494. might_sleep_if(gfpflags_allow_blocking(gfp));
  495. if (!memcg_css->parent)
  496. return &bdi->wb;
  497. do {
  498. wb = wb_get_lookup(bdi, memcg_css);
  499. } while (!wb && !cgwb_create(bdi, memcg_css, gfp));
  500. return wb;
  501. }
  502. static int cgwb_bdi_init(struct backing_dev_info *bdi)
  503. {
  504. int ret;
  505. INIT_RADIX_TREE(&bdi->cgwb_tree, GFP_ATOMIC);
  506. mutex_init(&bdi->cgwb_release_mutex);
  507. init_rwsem(&bdi->wb_switch_rwsem);
  508. ret = wb_init(&bdi->wb, bdi, GFP_KERNEL);
  509. if (!ret) {
  510. bdi->wb.memcg_css = &root_mem_cgroup->css;
  511. bdi->wb.blkcg_css = blkcg_root_css;
  512. }
  513. return ret;
  514. }
  515. static void cgwb_bdi_unregister(struct backing_dev_info *bdi)
  516. {
  517. struct radix_tree_iter iter;
  518. void **slot;
  519. struct bdi_writeback *wb;
  520. WARN_ON(test_bit(WB_registered, &bdi->wb.state));
  521. spin_lock_irq(&cgwb_lock);
  522. radix_tree_for_each_slot(slot, &bdi->cgwb_tree, &iter, 0)
  523. cgwb_kill(*slot);
  524. spin_unlock_irq(&cgwb_lock);
  525. mutex_lock(&bdi->cgwb_release_mutex);
  526. spin_lock_irq(&cgwb_lock);
  527. while (!list_empty(&bdi->wb_list)) {
  528. wb = list_first_entry(&bdi->wb_list, struct bdi_writeback,
  529. bdi_node);
  530. spin_unlock_irq(&cgwb_lock);
  531. wb_shutdown(wb);
  532. spin_lock_irq(&cgwb_lock);
  533. }
  534. spin_unlock_irq(&cgwb_lock);
  535. mutex_unlock(&bdi->cgwb_release_mutex);
  536. }
  537. /**
  538. * wb_memcg_offline - kill all wb's associated with a memcg being offlined
  539. * @memcg: memcg being offlined
  540. *
  541. * Also prevents creation of any new wb's associated with @memcg.
  542. */
  543. void wb_memcg_offline(struct mem_cgroup *memcg)
  544. {
  545. struct list_head *memcg_cgwb_list = &memcg->cgwb_list;
  546. struct bdi_writeback *wb, *next;
  547. spin_lock_irq(&cgwb_lock);
  548. list_for_each_entry_safe(wb, next, memcg_cgwb_list, memcg_node)
  549. cgwb_kill(wb);
  550. memcg_cgwb_list->next = NULL; /* prevent new wb's */
  551. spin_unlock_irq(&cgwb_lock);
  552. }
  553. /**
  554. * wb_blkcg_offline - kill all wb's associated with a blkcg being offlined
  555. * @blkcg: blkcg being offlined
  556. *
  557. * Also prevents creation of any new wb's associated with @blkcg.
  558. */
  559. void wb_blkcg_offline(struct blkcg *blkcg)
  560. {
  561. struct bdi_writeback *wb, *next;
  562. spin_lock_irq(&cgwb_lock);
  563. list_for_each_entry_safe(wb, next, &blkcg->cgwb_list, blkcg_node)
  564. cgwb_kill(wb);
  565. blkcg->cgwb_list.next = NULL; /* prevent new wb's */
  566. spin_unlock_irq(&cgwb_lock);
  567. }
  568. static void cgwb_bdi_register(struct backing_dev_info *bdi)
  569. {
  570. spin_lock_irq(&cgwb_lock);
  571. list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
  572. spin_unlock_irq(&cgwb_lock);
  573. }
  574. static int __init cgwb_init(void)
  575. {
  576. /*
  577. * There can be many concurrent release work items overwhelming
  578. * system_wq. Put them in a separate wq and limit concurrency.
  579. * There's no point in executing many of these in parallel.
  580. */
  581. cgwb_release_wq = alloc_workqueue("cgwb_release", 0, 1);
  582. if (!cgwb_release_wq)
  583. return -ENOMEM;
  584. return 0;
  585. }
  586. subsys_initcall(cgwb_init);
  587. #else /* CONFIG_CGROUP_WRITEBACK */
  588. static int cgwb_bdi_init(struct backing_dev_info *bdi)
  589. {
  590. return wb_init(&bdi->wb, bdi, GFP_KERNEL);
  591. }
  592. static void cgwb_bdi_unregister(struct backing_dev_info *bdi) { }
  593. static void cgwb_bdi_register(struct backing_dev_info *bdi)
  594. {
  595. list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
  596. }
  597. static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb)
  598. {
  599. list_del_rcu(&wb->bdi_node);
  600. }
  601. #endif /* CONFIG_CGROUP_WRITEBACK */
  602. static int bdi_init(struct backing_dev_info *bdi)
  603. {
  604. int ret;
  605. bdi->dev = NULL;
  606. kref_init(&bdi->refcnt);
  607. bdi->min_ratio = 0;
  608. bdi->max_ratio = 100;
  609. bdi->max_prop_frac = FPROP_FRAC_BASE;
  610. INIT_LIST_HEAD(&bdi->bdi_list);
  611. INIT_LIST_HEAD(&bdi->wb_list);
  612. init_waitqueue_head(&bdi->wb_waitq);
  613. ret = cgwb_bdi_init(bdi);
  614. return ret;
  615. }
  616. struct backing_dev_info *bdi_alloc(int node_id)
  617. {
  618. struct backing_dev_info *bdi;
  619. bdi = kzalloc_node(sizeof(*bdi), GFP_KERNEL, node_id);
  620. if (!bdi)
  621. return NULL;
  622. if (bdi_init(bdi)) {
  623. kfree(bdi);
  624. return NULL;
  625. }
  626. bdi->capabilities = BDI_CAP_WRITEBACK | BDI_CAP_WRITEBACK_ACCT;
  627. bdi->ra_pages = VM_READAHEAD_PAGES;
  628. bdi->io_pages = VM_READAHEAD_PAGES;
  629. return bdi;
  630. }
  631. EXPORT_SYMBOL(bdi_alloc);
  632. static struct rb_node **bdi_lookup_rb_node(u64 id, struct rb_node **parentp)
  633. {
  634. struct rb_node **p = &bdi_tree.rb_node;
  635. struct rb_node *parent = NULL;
  636. struct backing_dev_info *bdi;
  637. lockdep_assert_held(&bdi_lock);
  638. while (*p) {
  639. parent = *p;
  640. bdi = rb_entry(parent, struct backing_dev_info, rb_node);
  641. if (bdi->id > id)
  642. p = &(*p)->rb_left;
  643. else if (bdi->id < id)
  644. p = &(*p)->rb_right;
  645. else
  646. break;
  647. }
  648. if (parentp)
  649. *parentp = parent;
  650. return p;
  651. }
  652. /**
  653. * bdi_get_by_id - lookup and get bdi from its id
  654. * @id: bdi id to lookup
  655. *
  656. * Find bdi matching @id and get it. Returns NULL if the matching bdi
  657. * doesn't exist or is already unregistered.
  658. */
  659. struct backing_dev_info *bdi_get_by_id(u64 id)
  660. {
  661. struct backing_dev_info *bdi = NULL;
  662. struct rb_node **p;
  663. spin_lock_bh(&bdi_lock);
  664. p = bdi_lookup_rb_node(id, NULL);
  665. if (*p) {
  666. bdi = rb_entry(*p, struct backing_dev_info, rb_node);
  667. bdi_get(bdi);
  668. }
  669. spin_unlock_bh(&bdi_lock);
  670. return bdi;
  671. }
  672. int bdi_register_va(struct backing_dev_info *bdi, const char *fmt, va_list args)
  673. {
  674. struct device *dev;
  675. struct rb_node *parent, **p;
  676. if (bdi->dev) /* The driver needs to use separate queues per device */
  677. return 0;
  678. vsnprintf(bdi->dev_name, sizeof(bdi->dev_name), fmt, args);
  679. dev = device_create(bdi_class, NULL, MKDEV(0, 0), bdi, bdi->dev_name);
  680. if (IS_ERR(dev))
  681. return PTR_ERR(dev);
  682. cgwb_bdi_register(bdi);
  683. bdi->dev = dev;
  684. bdi_debug_register(bdi, dev_name(dev));
  685. set_bit(WB_registered, &bdi->wb.state);
  686. spin_lock_bh(&bdi_lock);
  687. bdi->id = ++bdi_id_cursor;
  688. p = bdi_lookup_rb_node(bdi->id, &parent);
  689. rb_link_node(&bdi->rb_node, parent, p);
  690. rb_insert_color(&bdi->rb_node, &bdi_tree);
  691. list_add_tail_rcu(&bdi->bdi_list, &bdi_list);
  692. spin_unlock_bh(&bdi_lock);
  693. trace_writeback_bdi_register(bdi);
  694. return 0;
  695. }
  696. int bdi_register(struct backing_dev_info *bdi, const char *fmt, ...)
  697. {
  698. va_list args;
  699. int ret;
  700. va_start(args, fmt);
  701. ret = bdi_register_va(bdi, fmt, args);
  702. va_end(args);
  703. return ret;
  704. }
  705. EXPORT_SYMBOL(bdi_register);
  706. void bdi_set_owner(struct backing_dev_info *bdi, struct device *owner)
  707. {
  708. WARN_ON_ONCE(bdi->owner);
  709. bdi->owner = owner;
  710. get_device(owner);
  711. }
  712. /*
  713. * Remove bdi from bdi_list, and ensure that it is no longer visible
  714. */
  715. static void bdi_remove_from_list(struct backing_dev_info *bdi)
  716. {
  717. spin_lock_bh(&bdi_lock);
  718. rb_erase(&bdi->rb_node, &bdi_tree);
  719. list_del_rcu(&bdi->bdi_list);
  720. spin_unlock_bh(&bdi_lock);
  721. synchronize_rcu_expedited();
  722. }
  723. void bdi_unregister(struct backing_dev_info *bdi)
  724. {
  725. /* make sure nobody finds us on the bdi_list anymore */
  726. bdi_remove_from_list(bdi);
  727. wb_shutdown(&bdi->wb);
  728. cgwb_bdi_unregister(bdi);
  729. /*
  730. * If this BDI's min ratio has been set, use bdi_set_min_ratio() to
  731. * update the global bdi_min_ratio.
  732. */
  733. if (bdi->min_ratio)
  734. bdi_set_min_ratio(bdi, 0);
  735. if (bdi->dev) {
  736. bdi_debug_unregister(bdi);
  737. device_unregister(bdi->dev);
  738. bdi->dev = NULL;
  739. }
  740. if (bdi->owner) {
  741. put_device(bdi->owner);
  742. bdi->owner = NULL;
  743. }
  744. }
  745. static void release_bdi(struct kref *ref)
  746. {
  747. struct backing_dev_info *bdi =
  748. container_of(ref, struct backing_dev_info, refcnt);
  749. if (test_bit(WB_registered, &bdi->wb.state))
  750. bdi_unregister(bdi);
  751. WARN_ON_ONCE(bdi->dev);
  752. wb_exit(&bdi->wb);
  753. kfree(bdi);
  754. }
  755. void bdi_put(struct backing_dev_info *bdi)
  756. {
  757. kref_put(&bdi->refcnt, release_bdi);
  758. }
  759. EXPORT_SYMBOL(bdi_put);
  760. const char *bdi_dev_name(struct backing_dev_info *bdi)
  761. {
  762. if (!bdi || !bdi->dev)
  763. return bdi_unknown_name;
  764. return bdi->dev_name;
  765. }
  766. EXPORT_SYMBOL_GPL(bdi_dev_name);
  767. static wait_queue_head_t congestion_wqh[2] = {
  768. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
  769. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
  770. };
  771. static atomic_t nr_wb_congested[2];
  772. void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
  773. {
  774. wait_queue_head_t *wqh = &congestion_wqh[sync];
  775. enum wb_congested_state bit;
  776. bit = sync ? WB_sync_congested : WB_async_congested;
  777. if (test_and_clear_bit(bit, &bdi->wb.congested))
  778. atomic_dec(&nr_wb_congested[sync]);
  779. smp_mb__after_atomic();
  780. if (waitqueue_active(wqh))
  781. wake_up(wqh);
  782. }
  783. EXPORT_SYMBOL(clear_bdi_congested);
  784. void set_bdi_congested(struct backing_dev_info *bdi, int sync)
  785. {
  786. enum wb_congested_state bit;
  787. bit = sync ? WB_sync_congested : WB_async_congested;
  788. if (!test_and_set_bit(bit, &bdi->wb.congested))
  789. atomic_inc(&nr_wb_congested[sync]);
  790. }
  791. EXPORT_SYMBOL(set_bdi_congested);
  792. /**
  793. * congestion_wait - wait for a backing_dev to become uncongested
  794. * @sync: SYNC or ASYNC IO
  795. * @timeout: timeout in jiffies
  796. *
  797. * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
  798. * write congestion. If no backing_devs are congested then just wait for the
  799. * next write to be completed.
  800. */
  801. long congestion_wait(int sync, long timeout)
  802. {
  803. long ret;
  804. unsigned long start = jiffies;
  805. DEFINE_WAIT(wait);
  806. wait_queue_head_t *wqh = &congestion_wqh[sync];
  807. prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
  808. ret = io_schedule_timeout(timeout);
  809. finish_wait(wqh, &wait);
  810. trace_writeback_congestion_wait(jiffies_to_usecs(timeout),
  811. jiffies_to_usecs(jiffies - start));
  812. return ret;
  813. }
  814. EXPORT_SYMBOL(congestion_wait);
  815. /**
  816. * wait_iff_congested - Conditionally wait for a backing_dev to become uncongested or a pgdat to complete writes
  817. * @sync: SYNC or ASYNC IO
  818. * @timeout: timeout in jiffies
  819. *
  820. * In the event of a congested backing_dev (any backing_dev) this waits
  821. * for up to @timeout jiffies for either a BDI to exit congestion of the
  822. * given @sync queue or a write to complete.
  823. *
  824. * The return value is 0 if the sleep is for the full timeout. Otherwise,
  825. * it is the number of jiffies that were still remaining when the function
  826. * returned. return_value == timeout implies the function did not sleep.
  827. */
  828. long wait_iff_congested(int sync, long timeout)
  829. {
  830. long ret;
  831. unsigned long start = jiffies;
  832. DEFINE_WAIT(wait);
  833. wait_queue_head_t *wqh = &congestion_wqh[sync];
  834. /*
  835. * If there is no congestion, yield if necessary instead
  836. * of sleeping on the congestion queue
  837. */
  838. if (atomic_read(&nr_wb_congested[sync]) == 0) {
  839. cond_resched();
  840. /* In case we scheduled, work out time remaining */
  841. ret = timeout - (jiffies - start);
  842. if (ret < 0)
  843. ret = 0;
  844. goto out;
  845. }
  846. /* Sleep until uncongested or a write happens */
  847. prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
  848. ret = io_schedule_timeout(timeout);
  849. finish_wait(wqh, &wait);
  850. out:
  851. trace_writeback_wait_iff_congested(jiffies_to_usecs(timeout),
  852. jiffies_to_usecs(jiffies - start));
  853. return ret;
  854. }
  855. EXPORT_SYMBOL(wait_iff_congested);