backing-dev.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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->congested = wb_congested_get_create(bdi, blkcg_id, gfp);
  266. if (!wb->congested) {
  267. err = -ENOMEM;
  268. goto out_put_bdi;
  269. }
  270. err = fprop_local_init_percpu(&wb->completions, gfp);
  271. if (err)
  272. goto out_put_cong;
  273. for (i = 0; i < NR_WB_STAT_ITEMS; i++) {
  274. err = percpu_counter_init(&wb->stat[i], 0, gfp);
  275. if (err)
  276. goto out_destroy_stat;
  277. }
  278. return 0;
  279. out_destroy_stat:
  280. while (i--)
  281. percpu_counter_destroy(&wb->stat[i]);
  282. fprop_local_destroy_percpu(&wb->completions);
  283. out_put_cong:
  284. wb_congested_put(wb->congested);
  285. out_put_bdi:
  286. if (wb != &bdi->wb)
  287. bdi_put(bdi);
  288. return err;
  289. }
  290. static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb);
  291. /*
  292. * Remove bdi from the global list and shutdown any threads we have running
  293. */
  294. static void wb_shutdown(struct bdi_writeback *wb)
  295. {
  296. /* Make sure nobody queues further work */
  297. spin_lock_bh(&wb->work_lock);
  298. if (!test_and_clear_bit(WB_registered, &wb->state)) {
  299. spin_unlock_bh(&wb->work_lock);
  300. /*
  301. * Wait for wb shutdown to finish if someone else is just
  302. * running wb_shutdown(). Otherwise we could proceed to wb /
  303. * bdi destruction before wb_shutdown() is finished.
  304. */
  305. wait_on_bit(&wb->state, WB_shutting_down, TASK_UNINTERRUPTIBLE);
  306. return;
  307. }
  308. set_bit(WB_shutting_down, &wb->state);
  309. spin_unlock_bh(&wb->work_lock);
  310. cgwb_remove_from_bdi_list(wb);
  311. /*
  312. * Drain work list and shutdown the delayed_work. !WB_registered
  313. * tells wb_workfn() that @wb is dying and its work_list needs to
  314. * be drained no matter what.
  315. */
  316. mod_delayed_work(bdi_wq, &wb->dwork, 0);
  317. flush_delayed_work(&wb->dwork);
  318. WARN_ON(!list_empty(&wb->work_list));
  319. /*
  320. * Make sure bit gets cleared after shutdown is finished. Matches with
  321. * the barrier provided by test_and_clear_bit() above.
  322. */
  323. smp_wmb();
  324. clear_bit(WB_shutting_down, &wb->state);
  325. }
  326. static void wb_exit(struct bdi_writeback *wb)
  327. {
  328. int i;
  329. WARN_ON(delayed_work_pending(&wb->dwork));
  330. for (i = 0; i < NR_WB_STAT_ITEMS; i++)
  331. percpu_counter_destroy(&wb->stat[i]);
  332. fprop_local_destroy_percpu(&wb->completions);
  333. wb_congested_put(wb->congested);
  334. if (wb != &wb->bdi->wb)
  335. bdi_put(wb->bdi);
  336. }
  337. #ifdef CONFIG_CGROUP_WRITEBACK
  338. #include <linux/memcontrol.h>
  339. /*
  340. * cgwb_lock protects bdi->cgwb_tree, bdi->cgwb_congested_tree,
  341. * blkcg->cgwb_list, and memcg->cgwb_list. bdi->cgwb_tree is also RCU
  342. * protected.
  343. */
  344. static DEFINE_SPINLOCK(cgwb_lock);
  345. /**
  346. * wb_congested_get_create - get or create a wb_congested
  347. * @bdi: associated bdi
  348. * @blkcg_id: ID of the associated blkcg
  349. * @gfp: allocation mask
  350. *
  351. * Look up the wb_congested for @blkcg_id on @bdi. If missing, create one.
  352. * The returned wb_congested has its reference count incremented. Returns
  353. * NULL on failure.
  354. */
  355. struct bdi_writeback_congested *
  356. wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
  357. {
  358. struct bdi_writeback_congested *new_congested = NULL, *congested;
  359. struct rb_node **node, *parent;
  360. unsigned long flags;
  361. retry:
  362. spin_lock_irqsave(&cgwb_lock, flags);
  363. node = &bdi->cgwb_congested_tree.rb_node;
  364. parent = NULL;
  365. while (*node != NULL) {
  366. parent = *node;
  367. congested = container_of(parent, struct bdi_writeback_congested,
  368. rb_node);
  369. if (congested->blkcg_id < blkcg_id)
  370. node = &parent->rb_left;
  371. else if (congested->blkcg_id > blkcg_id)
  372. node = &parent->rb_right;
  373. else
  374. goto found;
  375. }
  376. if (new_congested) {
  377. /* !found and storage for new one already allocated, insert */
  378. congested = new_congested;
  379. new_congested = NULL;
  380. rb_link_node(&congested->rb_node, parent, node);
  381. rb_insert_color(&congested->rb_node, &bdi->cgwb_congested_tree);
  382. goto found;
  383. }
  384. spin_unlock_irqrestore(&cgwb_lock, flags);
  385. /* allocate storage for new one and retry */
  386. new_congested = kzalloc(sizeof(*new_congested), gfp);
  387. if (!new_congested)
  388. return NULL;
  389. atomic_set(&new_congested->refcnt, 0);
  390. new_congested->__bdi = bdi;
  391. new_congested->blkcg_id = blkcg_id;
  392. goto retry;
  393. found:
  394. atomic_inc(&congested->refcnt);
  395. spin_unlock_irqrestore(&cgwb_lock, flags);
  396. kfree(new_congested);
  397. return congested;
  398. }
  399. /**
  400. * wb_congested_put - put a wb_congested
  401. * @congested: wb_congested to put
  402. *
  403. * Put @congested and destroy it if the refcnt reaches zero.
  404. */
  405. void wb_congested_put(struct bdi_writeback_congested *congested)
  406. {
  407. unsigned long flags;
  408. local_irq_save(flags);
  409. if (!atomic_dec_and_lock(&congested->refcnt, &cgwb_lock)) {
  410. local_irq_restore(flags);
  411. return;
  412. }
  413. /* bdi might already have been destroyed leaving @congested unlinked */
  414. if (congested->__bdi) {
  415. rb_erase(&congested->rb_node,
  416. &congested->__bdi->cgwb_congested_tree);
  417. congested->__bdi = NULL;
  418. }
  419. spin_unlock_irqrestore(&cgwb_lock, flags);
  420. kfree(congested);
  421. }
  422. static void cgwb_release_workfn(struct work_struct *work)
  423. {
  424. struct bdi_writeback *wb = container_of(work, struct bdi_writeback,
  425. release_work);
  426. wb_shutdown(wb);
  427. css_put(wb->memcg_css);
  428. css_put(wb->blkcg_css);
  429. fprop_local_destroy_percpu(&wb->memcg_completions);
  430. percpu_ref_exit(&wb->refcnt);
  431. wb_exit(wb);
  432. kfree_rcu(wb, rcu);
  433. }
  434. static void cgwb_release(struct percpu_ref *refcnt)
  435. {
  436. struct bdi_writeback *wb = container_of(refcnt, struct bdi_writeback,
  437. refcnt);
  438. schedule_work(&wb->release_work);
  439. }
  440. static void cgwb_kill(struct bdi_writeback *wb)
  441. {
  442. lockdep_assert_held(&cgwb_lock);
  443. WARN_ON(!radix_tree_delete(&wb->bdi->cgwb_tree, wb->memcg_css->id));
  444. list_del(&wb->memcg_node);
  445. list_del(&wb->blkcg_node);
  446. percpu_ref_kill(&wb->refcnt);
  447. }
  448. static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb)
  449. {
  450. spin_lock_irq(&cgwb_lock);
  451. list_del_rcu(&wb->bdi_node);
  452. spin_unlock_irq(&cgwb_lock);
  453. }
  454. static int cgwb_create(struct backing_dev_info *bdi,
  455. struct cgroup_subsys_state *memcg_css, gfp_t gfp)
  456. {
  457. struct mem_cgroup *memcg;
  458. struct cgroup_subsys_state *blkcg_css;
  459. struct blkcg *blkcg;
  460. struct list_head *memcg_cgwb_list, *blkcg_cgwb_list;
  461. struct bdi_writeback *wb;
  462. unsigned long flags;
  463. int ret = 0;
  464. memcg = mem_cgroup_from_css(memcg_css);
  465. blkcg_css = cgroup_get_e_css(memcg_css->cgroup, &io_cgrp_subsys);
  466. blkcg = css_to_blkcg(blkcg_css);
  467. memcg_cgwb_list = mem_cgroup_cgwb_list(memcg);
  468. blkcg_cgwb_list = &blkcg->cgwb_list;
  469. /* look up again under lock and discard on blkcg mismatch */
  470. spin_lock_irqsave(&cgwb_lock, flags);
  471. wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
  472. if (wb && wb->blkcg_css != blkcg_css) {
  473. cgwb_kill(wb);
  474. wb = NULL;
  475. }
  476. spin_unlock_irqrestore(&cgwb_lock, flags);
  477. if (wb)
  478. goto out_put;
  479. /* need to create a new one */
  480. wb = kmalloc(sizeof(*wb), gfp);
  481. if (!wb)
  482. return -ENOMEM;
  483. ret = wb_init(wb, bdi, blkcg_css->id, gfp);
  484. if (ret)
  485. goto err_free;
  486. ret = percpu_ref_init(&wb->refcnt, cgwb_release, 0, gfp);
  487. if (ret)
  488. goto err_wb_exit;
  489. ret = fprop_local_init_percpu(&wb->memcg_completions, gfp);
  490. if (ret)
  491. goto err_ref_exit;
  492. wb->memcg_css = memcg_css;
  493. wb->blkcg_css = blkcg_css;
  494. INIT_WORK(&wb->release_work, cgwb_release_workfn);
  495. set_bit(WB_registered, &wb->state);
  496. /*
  497. * The root wb determines the registered state of the whole bdi and
  498. * memcg_cgwb_list and blkcg_cgwb_list's next pointers indicate
  499. * whether they're still online. Don't link @wb if any is dead.
  500. * See wb_memcg_offline() and wb_blkcg_offline().
  501. */
  502. ret = -ENODEV;
  503. spin_lock_irqsave(&cgwb_lock, flags);
  504. if (test_bit(WB_registered, &bdi->wb.state) &&
  505. blkcg_cgwb_list->next && memcg_cgwb_list->next) {
  506. /* we might have raced another instance of this function */
  507. ret = radix_tree_insert(&bdi->cgwb_tree, memcg_css->id, wb);
  508. if (!ret) {
  509. list_add_tail_rcu(&wb->bdi_node, &bdi->wb_list);
  510. list_add(&wb->memcg_node, memcg_cgwb_list);
  511. list_add(&wb->blkcg_node, blkcg_cgwb_list);
  512. css_get(memcg_css);
  513. css_get(blkcg_css);
  514. }
  515. }
  516. spin_unlock_irqrestore(&cgwb_lock, flags);
  517. if (ret) {
  518. if (ret == -EEXIST)
  519. ret = 0;
  520. goto err_fprop_exit;
  521. }
  522. goto out_put;
  523. err_fprop_exit:
  524. fprop_local_destroy_percpu(&wb->memcg_completions);
  525. err_ref_exit:
  526. percpu_ref_exit(&wb->refcnt);
  527. err_wb_exit:
  528. wb_exit(wb);
  529. err_free:
  530. kfree(wb);
  531. out_put:
  532. css_put(blkcg_css);
  533. return ret;
  534. }
  535. /**
  536. * wb_get_create - get wb for a given memcg, create if necessary
  537. * @bdi: target bdi
  538. * @memcg_css: cgroup_subsys_state of the target memcg (must have positive ref)
  539. * @gfp: allocation mask to use
  540. *
  541. * Try to get the wb for @memcg_css on @bdi. If it doesn't exist, try to
  542. * create one. The returned wb has its refcount incremented.
  543. *
  544. * This function uses css_get() on @memcg_css and thus expects its refcnt
  545. * to be positive on invocation. IOW, rcu_read_lock() protection on
  546. * @memcg_css isn't enough. try_get it before calling this function.
  547. *
  548. * A wb is keyed by its associated memcg. As blkcg implicitly enables
  549. * memcg on the default hierarchy, memcg association is guaranteed to be
  550. * more specific (equal or descendant to the associated blkcg) and thus can
  551. * identify both the memcg and blkcg associations.
  552. *
  553. * Because the blkcg associated with a memcg may change as blkcg is enabled
  554. * and disabled closer to root in the hierarchy, each wb keeps track of
  555. * both the memcg and blkcg associated with it and verifies the blkcg on
  556. * each lookup. On mismatch, the existing wb is discarded and a new one is
  557. * created.
  558. */
  559. struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
  560. struct cgroup_subsys_state *memcg_css,
  561. gfp_t gfp)
  562. {
  563. struct bdi_writeback *wb;
  564. might_sleep_if(gfpflags_allow_blocking(gfp));
  565. if (!memcg_css->parent)
  566. return &bdi->wb;
  567. do {
  568. rcu_read_lock();
  569. wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
  570. if (wb) {
  571. struct cgroup_subsys_state *blkcg_css;
  572. /* see whether the blkcg association has changed */
  573. blkcg_css = cgroup_get_e_css(memcg_css->cgroup,
  574. &io_cgrp_subsys);
  575. if (unlikely(wb->blkcg_css != blkcg_css ||
  576. !wb_tryget(wb)))
  577. wb = NULL;
  578. css_put(blkcg_css);
  579. }
  580. rcu_read_unlock();
  581. } while (!wb && !cgwb_create(bdi, memcg_css, gfp));
  582. return wb;
  583. }
  584. static int cgwb_bdi_init(struct backing_dev_info *bdi)
  585. {
  586. int ret;
  587. INIT_RADIX_TREE(&bdi->cgwb_tree, GFP_ATOMIC);
  588. bdi->cgwb_congested_tree = RB_ROOT;
  589. init_rwsem(&bdi->wb_switch_rwsem);
  590. ret = wb_init(&bdi->wb, bdi, 1, GFP_KERNEL);
  591. if (!ret) {
  592. bdi->wb.memcg_css = &root_mem_cgroup->css;
  593. bdi->wb.blkcg_css = blkcg_root_css;
  594. }
  595. return ret;
  596. }
  597. static void cgwb_bdi_unregister(struct backing_dev_info *bdi)
  598. {
  599. struct radix_tree_iter iter;
  600. void **slot;
  601. struct bdi_writeback *wb;
  602. WARN_ON(test_bit(WB_registered, &bdi->wb.state));
  603. spin_lock_irq(&cgwb_lock);
  604. radix_tree_for_each_slot(slot, &bdi->cgwb_tree, &iter, 0)
  605. cgwb_kill(*slot);
  606. while (!list_empty(&bdi->wb_list)) {
  607. wb = list_first_entry(&bdi->wb_list, struct bdi_writeback,
  608. bdi_node);
  609. spin_unlock_irq(&cgwb_lock);
  610. wb_shutdown(wb);
  611. spin_lock_irq(&cgwb_lock);
  612. }
  613. spin_unlock_irq(&cgwb_lock);
  614. }
  615. /**
  616. * wb_memcg_offline - kill all wb's associated with a memcg being offlined
  617. * @memcg: memcg being offlined
  618. *
  619. * Also prevents creation of any new wb's associated with @memcg.
  620. */
  621. void wb_memcg_offline(struct mem_cgroup *memcg)
  622. {
  623. LIST_HEAD(to_destroy);
  624. struct list_head *memcg_cgwb_list = mem_cgroup_cgwb_list(memcg);
  625. struct bdi_writeback *wb, *next;
  626. spin_lock_irq(&cgwb_lock);
  627. list_for_each_entry_safe(wb, next, memcg_cgwb_list, memcg_node)
  628. cgwb_kill(wb);
  629. memcg_cgwb_list->next = NULL; /* prevent new wb's */
  630. spin_unlock_irq(&cgwb_lock);
  631. }
  632. /**
  633. * wb_blkcg_offline - kill all wb's associated with a blkcg being offlined
  634. * @blkcg: blkcg being offlined
  635. *
  636. * Also prevents creation of any new wb's associated with @blkcg.
  637. */
  638. void wb_blkcg_offline(struct blkcg *blkcg)
  639. {
  640. LIST_HEAD(to_destroy);
  641. struct bdi_writeback *wb, *next;
  642. spin_lock_irq(&cgwb_lock);
  643. list_for_each_entry_safe(wb, next, &blkcg->cgwb_list, blkcg_node)
  644. cgwb_kill(wb);
  645. blkcg->cgwb_list.next = NULL; /* prevent new wb's */
  646. spin_unlock_irq(&cgwb_lock);
  647. }
  648. static void cgwb_bdi_exit(struct backing_dev_info *bdi)
  649. {
  650. struct rb_node *rbn;
  651. spin_lock_irq(&cgwb_lock);
  652. while ((rbn = rb_first(&bdi->cgwb_congested_tree))) {
  653. struct bdi_writeback_congested *congested =
  654. rb_entry(rbn, struct bdi_writeback_congested, rb_node);
  655. rb_erase(rbn, &bdi->cgwb_congested_tree);
  656. congested->__bdi = NULL; /* mark @congested unlinked */
  657. }
  658. spin_unlock_irq(&cgwb_lock);
  659. }
  660. static void cgwb_bdi_register(struct backing_dev_info *bdi)
  661. {
  662. spin_lock_irq(&cgwb_lock);
  663. list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
  664. spin_unlock_irq(&cgwb_lock);
  665. }
  666. #else /* CONFIG_CGROUP_WRITEBACK */
  667. static int cgwb_bdi_init(struct backing_dev_info *bdi)
  668. {
  669. int err;
  670. bdi->wb_congested = kzalloc(sizeof(*bdi->wb_congested), GFP_KERNEL);
  671. if (!bdi->wb_congested)
  672. return -ENOMEM;
  673. atomic_set(&bdi->wb_congested->refcnt, 1);
  674. err = wb_init(&bdi->wb, bdi, 1, GFP_KERNEL);
  675. if (err) {
  676. wb_congested_put(bdi->wb_congested);
  677. return err;
  678. }
  679. return 0;
  680. }
  681. static void cgwb_bdi_unregister(struct backing_dev_info *bdi) { }
  682. static void cgwb_bdi_exit(struct backing_dev_info *bdi)
  683. {
  684. wb_congested_put(bdi->wb_congested);
  685. }
  686. static void cgwb_bdi_register(struct backing_dev_info *bdi)
  687. {
  688. list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
  689. }
  690. static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb)
  691. {
  692. list_del_rcu(&wb->bdi_node);
  693. }
  694. #endif /* CONFIG_CGROUP_WRITEBACK */
  695. int bdi_init(struct backing_dev_info *bdi)
  696. {
  697. int ret;
  698. bdi->dev = NULL;
  699. kref_init(&bdi->refcnt);
  700. bdi->min_ratio = 0;
  701. bdi->max_ratio = 100;
  702. bdi->max_prop_frac = FPROP_FRAC_BASE;
  703. INIT_LIST_HEAD(&bdi->bdi_list);
  704. INIT_LIST_HEAD(&bdi->wb_list);
  705. init_waitqueue_head(&bdi->wb_waitq);
  706. ret = cgwb_bdi_init(bdi);
  707. return ret;
  708. }
  709. EXPORT_SYMBOL(bdi_init);
  710. struct backing_dev_info *bdi_alloc_node(gfp_t gfp_mask, int node_id)
  711. {
  712. struct backing_dev_info *bdi;
  713. bdi = kmalloc_node(sizeof(struct backing_dev_info),
  714. gfp_mask | __GFP_ZERO, node_id);
  715. if (!bdi)
  716. return NULL;
  717. if (bdi_init(bdi)) {
  718. kfree(bdi);
  719. return NULL;
  720. }
  721. return bdi;
  722. }
  723. int bdi_register(struct backing_dev_info *bdi, struct device *parent,
  724. const char *fmt, ...)
  725. {
  726. va_list args;
  727. struct device *dev;
  728. if (bdi->dev) /* The driver needs to use separate queues per device */
  729. return 0;
  730. va_start(args, fmt);
  731. dev = device_create_vargs(bdi_class, parent, MKDEV(0, 0), bdi, fmt, args);
  732. va_end(args);
  733. if (IS_ERR(dev))
  734. return PTR_ERR(dev);
  735. cgwb_bdi_register(bdi);
  736. bdi->dev = dev;
  737. bdi_debug_register(bdi, dev_name(dev));
  738. set_bit(WB_registered, &bdi->wb.state);
  739. spin_lock_bh(&bdi_lock);
  740. list_add_tail_rcu(&bdi->bdi_list, &bdi_list);
  741. spin_unlock_bh(&bdi_lock);
  742. trace_writeback_bdi_register(bdi);
  743. return 0;
  744. }
  745. EXPORT_SYMBOL(bdi_register);
  746. int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev)
  747. {
  748. return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev));
  749. }
  750. EXPORT_SYMBOL(bdi_register_dev);
  751. int bdi_register_owner(struct backing_dev_info *bdi, struct device *owner)
  752. {
  753. int rc;
  754. rc = bdi_register(bdi, NULL, "%u:%u", MAJOR(owner->devt),
  755. MINOR(owner->devt));
  756. if (rc)
  757. return rc;
  758. /* Leaking owner reference... */
  759. WARN_ON(bdi->owner);
  760. bdi->owner = owner;
  761. get_device(owner);
  762. return 0;
  763. }
  764. EXPORT_SYMBOL(bdi_register_owner);
  765. /*
  766. * Remove bdi from bdi_list, and ensure that it is no longer visible
  767. */
  768. static void bdi_remove_from_list(struct backing_dev_info *bdi)
  769. {
  770. spin_lock_bh(&bdi_lock);
  771. list_del_rcu(&bdi->bdi_list);
  772. spin_unlock_bh(&bdi_lock);
  773. synchronize_rcu_expedited();
  774. }
  775. void bdi_unregister(struct backing_dev_info *bdi)
  776. {
  777. /* make sure nobody finds us on the bdi_list anymore */
  778. bdi_remove_from_list(bdi);
  779. wb_shutdown(&bdi->wb);
  780. cgwb_bdi_unregister(bdi);
  781. if (bdi->dev) {
  782. bdi_debug_unregister(bdi);
  783. device_unregister(bdi->dev);
  784. bdi->dev = NULL;
  785. }
  786. if (bdi->owner) {
  787. put_device(bdi->owner);
  788. bdi->owner = NULL;
  789. }
  790. }
  791. static void bdi_exit(struct backing_dev_info *bdi)
  792. {
  793. WARN_ON_ONCE(bdi->dev);
  794. wb_exit(&bdi->wb);
  795. cgwb_bdi_exit(bdi);
  796. }
  797. static void release_bdi(struct kref *ref)
  798. {
  799. struct backing_dev_info *bdi =
  800. container_of(ref, struct backing_dev_info, refcnt);
  801. bdi_exit(bdi);
  802. kfree(bdi);
  803. }
  804. void bdi_put(struct backing_dev_info *bdi)
  805. {
  806. kref_put(&bdi->refcnt, release_bdi);
  807. }
  808. void bdi_destroy(struct backing_dev_info *bdi)
  809. {
  810. bdi_unregister(bdi);
  811. bdi_exit(bdi);
  812. }
  813. EXPORT_SYMBOL(bdi_destroy);
  814. /*
  815. * For use from filesystems to quickly init and register a bdi associated
  816. * with dirty writeback
  817. */
  818. int bdi_setup_and_register(struct backing_dev_info *bdi, char *name)
  819. {
  820. int err;
  821. bdi->name = name;
  822. bdi->capabilities = 0;
  823. err = bdi_init(bdi);
  824. if (err)
  825. return err;
  826. err = bdi_register(bdi, NULL, "%.28s-%ld", name,
  827. atomic_long_inc_return(&bdi_seq));
  828. if (err) {
  829. bdi_destroy(bdi);
  830. return err;
  831. }
  832. return 0;
  833. }
  834. EXPORT_SYMBOL(bdi_setup_and_register);
  835. static wait_queue_head_t congestion_wqh[2] = {
  836. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
  837. __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
  838. };
  839. static atomic_t nr_wb_congested[2];
  840. void clear_wb_congested(struct bdi_writeback_congested *congested, int sync)
  841. {
  842. wait_queue_head_t *wqh = &congestion_wqh[sync];
  843. enum wb_congested_state bit;
  844. bit = sync ? WB_sync_congested : WB_async_congested;
  845. if (test_and_clear_bit(bit, &congested->state))
  846. atomic_dec(&nr_wb_congested[sync]);
  847. smp_mb__after_atomic();
  848. if (waitqueue_active(wqh))
  849. wake_up(wqh);
  850. }
  851. EXPORT_SYMBOL(clear_wb_congested);
  852. void set_wb_congested(struct bdi_writeback_congested *congested, int sync)
  853. {
  854. enum wb_congested_state bit;
  855. bit = sync ? WB_sync_congested : WB_async_congested;
  856. if (!test_and_set_bit(bit, &congested->state))
  857. atomic_inc(&nr_wb_congested[sync]);
  858. }
  859. EXPORT_SYMBOL(set_wb_congested);
  860. /**
  861. * congestion_wait - wait for a backing_dev to become uncongested
  862. * @sync: SYNC or ASYNC IO
  863. * @timeout: timeout in jiffies
  864. *
  865. * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
  866. * write congestion. If no backing_devs are congested then just wait for the
  867. * next write to be completed.
  868. */
  869. long congestion_wait(int sync, long timeout)
  870. {
  871. long ret;
  872. unsigned long start = jiffies;
  873. DEFINE_WAIT(wait);
  874. wait_queue_head_t *wqh = &congestion_wqh[sync];
  875. prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
  876. ret = io_schedule_timeout(timeout);
  877. finish_wait(wqh, &wait);
  878. trace_writeback_congestion_wait(jiffies_to_usecs(timeout),
  879. jiffies_to_usecs(jiffies - start));
  880. return ret;
  881. }
  882. EXPORT_SYMBOL(congestion_wait);
  883. /**
  884. * wait_iff_congested - Conditionally wait for a backing_dev to become uncongested or a pgdat to complete writes
  885. * @pgdat: A pgdat to check if it is heavily congested
  886. * @sync: SYNC or ASYNC IO
  887. * @timeout: timeout in jiffies
  888. *
  889. * In the event of a congested backing_dev (any backing_dev) and the given
  890. * @pgdat has experienced recent congestion, this waits for up to @timeout
  891. * jiffies for either a BDI to exit congestion of the given @sync queue
  892. * or a write to complete.
  893. *
  894. * In the absence of pgdat congestion, cond_resched() is called to yield
  895. * the processor if necessary but otherwise does not sleep.
  896. *
  897. * The return value is 0 if the sleep is for the full timeout. Otherwise,
  898. * it is the number of jiffies that were still remaining when the function
  899. * returned. return_value == timeout implies the function did not sleep.
  900. */
  901. long wait_iff_congested(struct pglist_data *pgdat, int sync, long timeout)
  902. {
  903. long ret;
  904. unsigned long start = jiffies;
  905. DEFINE_WAIT(wait);
  906. wait_queue_head_t *wqh = &congestion_wqh[sync];
  907. /*
  908. * If there is no congestion, or heavy congestion is not being
  909. * encountered in the current pgdat, yield if necessary instead
  910. * of sleeping on the congestion queue
  911. */
  912. if (atomic_read(&nr_wb_congested[sync]) == 0 ||
  913. !test_bit(PGDAT_CONGESTED, &pgdat->flags)) {
  914. cond_resched();
  915. /* In case we scheduled, work out time remaining */
  916. ret = timeout - (jiffies - start);
  917. if (ret < 0)
  918. ret = 0;
  919. goto out;
  920. }
  921. /* Sleep until uncongested or a write happens */
  922. prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
  923. ret = io_schedule_timeout(timeout);
  924. finish_wait(wqh, &wait);
  925. out:
  926. trace_writeback_wait_iff_congested(jiffies_to_usecs(timeout),
  927. jiffies_to_usecs(jiffies - start));
  928. return ret;
  929. }
  930. EXPORT_SYMBOL(wait_iff_congested);
  931. int pdflush_proc_obsolete(struct ctl_table *table, int write,
  932. void __user *buffer, size_t *lenp, loff_t *ppos)
  933. {
  934. char kbuf[] = "0\n";
  935. if (*ppos || *lenp < sizeof(kbuf)) {
  936. *lenp = 0;
  937. return 0;
  938. }
  939. if (copy_to_user(buffer, kbuf, sizeof(kbuf)))
  940. return -EFAULT;
  941. pr_warn_once("%s exported in /proc is scheduled for removal\n",
  942. table->procname);
  943. *lenp = 2;
  944. *ppos += *lenp;
  945. return 2;
  946. }