backing-dev.c 28 KB

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