backing-dev.c 27 KB

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