mm: add bdi_set_max_ratio_no_scale() function
[linux-block.git] / mm / backing-dev.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
3fcfab16 2
c97ab271 3#include <linux/blkdev.h>
3fcfab16 4#include <linux/wait.h>
34f8fe50 5#include <linux/rbtree.h>
03ba3782 6#include <linux/kthread.h>
e41d12f5
CH
7#include <linux/backing-dev.h>
8#include <linux/blk-cgroup.h>
03ba3782 9#include <linux/freezer.h>
3fcfab16 10#include <linux/fs.h>
26160158 11#include <linux/pagemap.h>
03ba3782 12#include <linux/mm.h>
c1ca59a1 13#include <linux/sched/mm.h>
3fcfab16
AM
14#include <linux/sched.h>
15#include <linux/module.h>
cf0ca9fe
PZ
16#include <linux/writeback.h>
17#include <linux/device.h>
455b2864 18#include <trace/events/writeback.h>
cf0ca9fe 19
f56753ac 20struct backing_dev_info noop_backing_dev_info;
a212b105 21EXPORT_SYMBOL_GPL(noop_backing_dev_info);
5129a469 22
cf0ca9fe 23static struct class *bdi_class;
eb7ae5e0 24static const char *bdi_unknown_name = "(unknown)";
cfc4ba53
JA
25
26/*
34f8fe50
TH
27 * bdi_lock protects bdi_tree and updates to bdi_list. bdi_list has RCU
28 * reader side locking.
cfc4ba53 29 */
03ba3782 30DEFINE_SPINLOCK(bdi_lock);
34f8fe50
TH
31static u64 bdi_id_cursor;
32static struct rb_root bdi_tree = RB_ROOT;
66f3b8e2 33LIST_HEAD(bdi_list);
03ba3782 34
839a8e86
TH
35/* bdi_wq serves all asynchronous writeback tasks */
36struct workqueue_struct *bdi_wq;
37
6986c3e2
BW
38#define K(x) ((x) << (PAGE_SHIFT - 10))
39
76f1418b
MS
40#ifdef CONFIG_DEBUG_FS
41#include <linux/debugfs.h>
42#include <linux/seq_file.h>
43
44static struct dentry *bdi_debug_root;
45
46static void bdi_debug_init(void)
47{
48 bdi_debug_root = debugfs_create_dir("bdi", NULL);
49}
50
51static int bdi_debug_stats_show(struct seq_file *m, void *v)
52{
53 struct backing_dev_info *bdi = m->private;
c1955ce3 54 struct bdi_writeback *wb = &bdi->wb;
364aeb28
DR
55 unsigned long background_thresh;
56 unsigned long dirty_thresh;
0d960a38 57 unsigned long wb_thresh;
0ae45f63 58 unsigned long nr_dirty, nr_io, nr_more_io, nr_dirty_time;
f09b00d3
JA
59 struct inode *inode;
60
0ae45f63 61 nr_dirty = nr_io = nr_more_io = nr_dirty_time = 0;
f758eeab 62 spin_lock(&wb->list_lock);
c7f54084 63 list_for_each_entry(inode, &wb->b_dirty, i_io_list)
c1955ce3 64 nr_dirty++;
c7f54084 65 list_for_each_entry(inode, &wb->b_io, i_io_list)
c1955ce3 66 nr_io++;
c7f54084 67 list_for_each_entry(inode, &wb->b_more_io, i_io_list)
c1955ce3 68 nr_more_io++;
c7f54084 69 list_for_each_entry(inode, &wb->b_dirty_time, i_io_list)
0ae45f63
TT
70 if (inode->i_state & I_DIRTY_TIME)
71 nr_dirty_time++;
f758eeab 72 spin_unlock(&wb->list_lock);
76f1418b 73
16c4042f 74 global_dirty_limits(&background_thresh, &dirty_thresh);
0d960a38 75 wb_thresh = wb_calc_thresh(wb, dirty_thresh);
76f1418b 76
76f1418b 77 seq_printf(m,
00821b00
WF
78 "BdiWriteback: %10lu kB\n"
79 "BdiReclaimable: %10lu kB\n"
80 "BdiDirtyThresh: %10lu kB\n"
81 "DirtyThresh: %10lu kB\n"
82 "BackgroundThresh: %10lu kB\n"
c8e28ce0 83 "BdiDirtied: %10lu kB\n"
00821b00
WF
84 "BdiWritten: %10lu kB\n"
85 "BdiWriteBandwidth: %10lu kBps\n"
86 "b_dirty: %10lu\n"
87 "b_io: %10lu\n"
88 "b_more_io: %10lu\n"
0ae45f63 89 "b_dirty_time: %10lu\n"
00821b00
WF
90 "bdi_list: %10u\n"
91 "state: %10lx\n",
93f78d88
TH
92 (unsigned long) K(wb_stat(wb, WB_WRITEBACK)),
93 (unsigned long) K(wb_stat(wb, WB_RECLAIMABLE)),
0d960a38 94 K(wb_thresh),
f7d2b1ec
JK
95 K(dirty_thresh),
96 K(background_thresh),
93f78d88
TH
97 (unsigned long) K(wb_stat(wb, WB_DIRTIED)),
98 (unsigned long) K(wb_stat(wb, WB_WRITTEN)),
a88a341a 99 (unsigned long) K(wb->write_bandwidth),
f7d2b1ec
JK
100 nr_dirty,
101 nr_io,
102 nr_more_io,
0ae45f63 103 nr_dirty_time,
4452226e 104 !list_empty(&bdi->bdi_list), bdi->wb.state);
76f1418b
MS
105
106 return 0;
107}
5ad35093 108DEFINE_SHOW_ATTRIBUTE(bdi_debug_stats);
76f1418b 109
2d146b92 110static void bdi_debug_register(struct backing_dev_info *bdi, const char *name)
76f1418b
MS
111{
112 bdi->debug_dir = debugfs_create_dir(name, bdi_debug_root);
97f07697 113
2d146b92
GKH
114 debugfs_create_file("stats", 0444, bdi->debug_dir, bdi,
115 &bdi_debug_stats_fops);
76f1418b
MS
116}
117
118static void bdi_debug_unregister(struct backing_dev_info *bdi)
119{
2d146b92 120 debugfs_remove_recursive(bdi->debug_dir);
76f1418b
MS
121}
122#else
123static inline void bdi_debug_init(void)
124{
125}
2d146b92 126static inline void bdi_debug_register(struct backing_dev_info *bdi,
76f1418b
MS
127 const char *name)
128{
129}
130static inline void bdi_debug_unregister(struct backing_dev_info *bdi)
131{
132}
133#endif
134
cf0ca9fe
PZ
135static ssize_t read_ahead_kb_store(struct device *dev,
136 struct device_attribute *attr,
137 const char *buf, size_t count)
138{
139 struct backing_dev_info *bdi = dev_get_drvdata(dev);
cf0ca9fe 140 unsigned long read_ahead_kb;
7034ed13 141 ssize_t ret;
cf0ca9fe 142
7034ed13
NJ
143 ret = kstrtoul(buf, 10, &read_ahead_kb);
144 if (ret < 0)
145 return ret;
146
147 bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10);
148
149 return count;
cf0ca9fe
PZ
150}
151
cf0ca9fe
PZ
152#define BDI_SHOW(name, expr) \
153static ssize_t name##_show(struct device *dev, \
5e4c0d86 154 struct device_attribute *attr, char *buf) \
cf0ca9fe
PZ
155{ \
156 struct backing_dev_info *bdi = dev_get_drvdata(dev); \
157 \
5e4c0d86 158 return sysfs_emit(buf, "%lld\n", (long long)expr); \
d9e1241e
GKH
159} \
160static DEVICE_ATTR_RW(name);
cf0ca9fe
PZ
161
162BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
163
189d3c4a
PZ
164static ssize_t min_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);
189d3c4a 168 unsigned int ratio;
7034ed13
NJ
169 ssize_t ret;
170
171 ret = kstrtouint(buf, 10, &ratio);
172 if (ret < 0)
173 return ret;
174
175 ret = bdi_set_min_ratio(bdi, ratio);
176 if (!ret)
177 ret = count;
189d3c4a 178
189d3c4a
PZ
179 return ret;
180}
ae82291e 181BDI_SHOW(min_ratio, bdi->min_ratio / BDI_RATIO_SCALE)
189d3c4a 182
a42dde04
PZ
183static ssize_t max_ratio_store(struct device *dev,
184 struct device_attribute *attr, const char *buf, size_t count)
185{
186 struct backing_dev_info *bdi = dev_get_drvdata(dev);
a42dde04 187 unsigned int ratio;
7034ed13
NJ
188 ssize_t ret;
189
190 ret = kstrtouint(buf, 10, &ratio);
191 if (ret < 0)
192 return ret;
193
194 ret = bdi_set_max_ratio(bdi, ratio);
195 if (!ret)
196 ret = count;
a42dde04 197
a42dde04
PZ
198 return ret;
199}
ae82291e 200BDI_SHOW(max_ratio, bdi->max_ratio / BDI_RATIO_SCALE)
a42dde04 201
9c84819b
SR
202static ssize_t min_bytes_show(struct device *dev,
203 struct device_attribute *attr,
204 char *buf)
205{
206 struct backing_dev_info *bdi = dev_get_drvdata(dev);
207
208 return sysfs_emit(buf, "%llu\n", bdi_get_min_bytes(bdi));
209}
210
211static ssize_t min_bytes_store(struct device *dev,
212 struct device_attribute *attr, const char *buf, size_t count)
213{
214 struct backing_dev_info *bdi = dev_get_drvdata(dev);
215 u64 bytes;
216 ssize_t ret;
217
218 ret = kstrtoull(buf, 10, &bytes);
219 if (ret < 0)
220 return ret;
221
222 ret = bdi_set_min_bytes(bdi, bytes);
223 if (!ret)
224 ret = count;
225
226 return ret;
227}
228DEVICE_ATTR_RW(min_bytes);
229
c56e049a
SR
230static ssize_t max_bytes_show(struct device *dev,
231 struct device_attribute *attr,
232 char *buf)
233{
234 struct backing_dev_info *bdi = dev_get_drvdata(dev);
235
236 return sysfs_emit(buf, "%llu\n", bdi_get_max_bytes(bdi));
237}
238
239static ssize_t max_bytes_store(struct device *dev,
240 struct device_attribute *attr, const char *buf, size_t count)
241{
242 struct backing_dev_info *bdi = dev_get_drvdata(dev);
243 u64 bytes;
244 ssize_t ret;
245
246 ret = kstrtoull(buf, 10, &bytes);
247 if (ret < 0)
248 return ret;
249
250 ret = bdi_set_max_bytes(bdi, bytes);
251 if (!ret)
252 ret = count;
253
254 return ret;
255}
256DEVICE_ATTR_RW(max_bytes);
257
7d311cda
DW
258static ssize_t stable_pages_required_show(struct device *dev,
259 struct device_attribute *attr,
5e4c0d86 260 char *buf)
7d311cda 261{
1cb039f3
CH
262 dev_warn_once(dev,
263 "the stable_pages_required attribute has been removed. Use the stable_writes queue attribute instead.\n");
5e4c0d86 264 return sysfs_emit(buf, "%d\n", 0);
7d311cda 265}
d9e1241e
GKH
266static DEVICE_ATTR_RO(stable_pages_required);
267
27bbe9d4
SR
268static ssize_t strict_limit_store(struct device *dev,
269 struct device_attribute *attr, const char *buf, size_t count)
270{
271 struct backing_dev_info *bdi = dev_get_drvdata(dev);
272 unsigned int strict_limit;
273 ssize_t ret;
274
275 ret = kstrtouint(buf, 10, &strict_limit);
276 if (ret < 0)
277 return ret;
278
279 ret = bdi_set_strict_limit(bdi, strict_limit);
280 if (!ret)
281 ret = count;
282
283 return ret;
284}
285
286static ssize_t strict_limit_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
288{
289 struct backing_dev_info *bdi = dev_get_drvdata(dev);
290
291 return sysfs_emit(buf, "%d\n",
292 !!(bdi->capabilities & BDI_CAP_STRICTLIMIT));
293}
294static DEVICE_ATTR_RW(strict_limit);
295
d9e1241e
GKH
296static struct attribute *bdi_dev_attrs[] = {
297 &dev_attr_read_ahead_kb.attr,
298 &dev_attr_min_ratio.attr,
299 &dev_attr_max_ratio.attr,
9c84819b 300 &dev_attr_min_bytes.attr,
c56e049a 301 &dev_attr_max_bytes.attr,
d9e1241e 302 &dev_attr_stable_pages_required.attr,
27bbe9d4 303 &dev_attr_strict_limit.attr,
d9e1241e 304 NULL,
cf0ca9fe 305};
d9e1241e 306ATTRIBUTE_GROUPS(bdi_dev);
cf0ca9fe
PZ
307
308static __init int bdi_class_init(void)
309{
310 bdi_class = class_create(THIS_MODULE, "bdi");
14421453
AB
311 if (IS_ERR(bdi_class))
312 return PTR_ERR(bdi_class);
313
d9e1241e 314 bdi_class->dev_groups = bdi_dev_groups;
76f1418b 315 bdi_debug_init();
d03f6cdc 316
cf0ca9fe
PZ
317 return 0;
318}
76f1418b 319postcore_initcall(bdi_class_init);
cf0ca9fe 320
26160158
JA
321static int __init default_bdi_init(void)
322{
a2b90f11
MW
323 bdi_wq = alloc_workqueue("writeback", WQ_MEM_RECLAIM | WQ_UNBOUND |
324 WQ_SYSFS, 0);
839a8e86
TH
325 if (!bdi_wq)
326 return -ENOMEM;
4bca7e80 327 return 0;
26160158
JA
328}
329subsys_initcall(default_bdi_init);
330
6467716a 331/*
f0054bb1 332 * This function is used when the first inode for this wb is marked dirty. It
6467716a
AB
333 * wakes-up the corresponding bdi thread which should then take care of the
334 * periodic background write-out of dirty inodes. Since the write-out would
335 * starts only 'dirty_writeback_interval' centisecs from now anyway, we just
336 * set up a timer which wakes the bdi thread up later.
337 *
338 * Note, we wouldn't bother setting up the timer, but this function is on the
339 * fast-path (used by '__mark_inode_dirty()'), so we save few context switches
340 * by delaying the wake-up.
6ca738d6
DB
341 *
342 * We have to be careful not to postpone flush work if it is scheduled for
343 * earlier. Thus we use queue_delayed_work().
6467716a 344 */
f0054bb1 345void wb_wakeup_delayed(struct bdi_writeback *wb)
6467716a
AB
346{
347 unsigned long timeout;
348
349 timeout = msecs_to_jiffies(dirty_writeback_interval * 10);
f87904c0 350 spin_lock_irq(&wb->work_lock);
f0054bb1
TH
351 if (test_bit(WB_registered, &wb->state))
352 queue_delayed_work(bdi_wq, &wb->dwork, timeout);
f87904c0 353 spin_unlock_irq(&wb->work_lock);
03ba3782
JA
354}
355
45a2966f
JK
356static void wb_update_bandwidth_workfn(struct work_struct *work)
357{
358 struct bdi_writeback *wb = container_of(to_delayed_work(work),
359 struct bdi_writeback, bw_dwork);
360
361 wb_update_bandwidth(wb);
362}
363
cfc4ba53 364/*
a88a341a 365 * Initial write bandwidth: 100 MB/s
cfc4ba53 366 */
a88a341a 367#define INIT_BW (100 << (20 - PAGE_SHIFT))
cfc4ba53 368
8395cd9f 369static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
8c911f3d 370 gfp_t gfp)
cf0ca9fe 371{
93f78d88 372 int i, err;
cf0ca9fe 373
6467716a 374 memset(wb, 0, sizeof(*wb));
f1d0b063 375
6467716a
AB
376 wb->bdi = bdi;
377 wb->last_old_flush = jiffies;
378 INIT_LIST_HEAD(&wb->b_dirty);
379 INIT_LIST_HEAD(&wb->b_io);
380 INIT_LIST_HEAD(&wb->b_more_io);
0ae45f63 381 INIT_LIST_HEAD(&wb->b_dirty_time);
f758eeab 382 spin_lock_init(&wb->list_lock);
66f3b8e2 383
633a2abb 384 atomic_set(&wb->writeback_inodes, 0);
a88a341a
TH
385 wb->bw_time_stamp = jiffies;
386 wb->balanced_dirty_ratelimit = INIT_BW;
387 wb->dirty_ratelimit = INIT_BW;
388 wb->write_bandwidth = INIT_BW;
389 wb->avg_write_bandwidth = INIT_BW;
cf0ca9fe 390
f0054bb1
TH
391 spin_lock_init(&wb->work_lock);
392 INIT_LIST_HEAD(&wb->work_list);
393 INIT_DELAYED_WORK(&wb->dwork, wb_workfn);
45a2966f 394 INIT_DELAYED_WORK(&wb->bw_dwork, wb_update_bandwidth_workfn);
b57d74af 395 wb->dirty_sleep = jiffies;
c284de61 396
8395cd9f 397 err = fprop_local_init_percpu(&wb->completions, gfp);
a88a341a 398 if (err)
efee1713 399 return err;
c284de61 400
93f78d88 401 for (i = 0; i < NR_WB_STAT_ITEMS; i++) {
8395cd9f 402 err = percpu_counter_init(&wb->stat[i], 0, gfp);
a13f35e8
TH
403 if (err)
404 goto out_destroy_stat;
93f78d88 405 }
cf0ca9fe 406
93f78d88 407 return 0;
a13f35e8
TH
408
409out_destroy_stat:
078c6c3a 410 while (i--)
a13f35e8
TH
411 percpu_counter_destroy(&wb->stat[i]);
412 fprop_local_destroy_percpu(&wb->completions);
a13f35e8 413 return err;
cf0ca9fe 414}
cf0ca9fe 415
e8cb72b3
JK
416static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb);
417
03ba3782
JA
418/*
419 * Remove bdi from the global list and shutdown any threads we have running
420 */
46100071 421static void wb_shutdown(struct bdi_writeback *wb)
66f3b8e2 422{
c4db59d3 423 /* Make sure nobody queues further work */
f87904c0 424 spin_lock_irq(&wb->work_lock);
46100071 425 if (!test_and_clear_bit(WB_registered, &wb->state)) {
f87904c0 426 spin_unlock_irq(&wb->work_lock);
03ba3782 427 return;
c4db59d3 428 }
f87904c0 429 spin_unlock_irq(&wb->work_lock);
03ba3782 430
e8cb72b3 431 cgwb_remove_from_bdi_list(wb);
03ba3782 432 /*
46100071
TH
433 * Drain work list and shutdown the delayed_work. !WB_registered
434 * tells wb_workfn() that @wb is dying and its work_list needs to
435 * be drained no matter what.
03ba3782 436 */
46100071
TH
437 mod_delayed_work(bdi_wq, &wb->dwork, 0);
438 flush_delayed_work(&wb->dwork);
439 WARN_ON(!list_empty(&wb->work_list));
45a2966f 440 flush_delayed_work(&wb->bw_dwork);
46100071
TH
441}
442
f0054bb1 443static void wb_exit(struct bdi_writeback *wb)
93f78d88
TH
444{
445 int i;
446
447 WARN_ON(delayed_work_pending(&wb->dwork));
448
449 for (i = 0; i < NR_WB_STAT_ITEMS; i++)
450 percpu_counter_destroy(&wb->stat[i]);
6467716a 451
a88a341a
TH
452 fprop_local_destroy_percpu(&wb->completions);
453}
e98be2d5 454
52ebea74
TH
455#ifdef CONFIG_CGROUP_WRITEBACK
456
457#include <linux/memcontrol.h>
458
459/*
c22d70a1
RG
460 * cgwb_lock protects bdi->cgwb_tree, blkcg->cgwb_list, offline_cgwbs and
461 * memcg->cgwb_list. bdi->cgwb_tree is also RCU protected.
52ebea74
TH
462 */
463static DEFINE_SPINLOCK(cgwb_lock);
f1834646 464static struct workqueue_struct *cgwb_release_wq;
52ebea74 465
c22d70a1
RG
466static LIST_HEAD(offline_cgwbs);
467static void cleanup_offline_cgwbs_workfn(struct work_struct *work);
468static DECLARE_WORK(cleanup_offline_cgwbs_work, cleanup_offline_cgwbs_workfn);
469
52ebea74
TH
470static void cgwb_release_workfn(struct work_struct *work)
471{
472 struct bdi_writeback *wb = container_of(work, struct bdi_writeback,
473 release_work);
efee1713 474 struct backing_dev_info *bdi = wb->bdi;
52ebea74 475
3ee7e869 476 mutex_lock(&wb->bdi->cgwb_release_mutex);
52ebea74
TH
477 wb_shutdown(wb);
478
479 css_put(wb->memcg_css);
480 css_put(wb->blkcg_css);
3ee7e869 481 mutex_unlock(&wb->bdi->cgwb_release_mutex);
52ebea74 482
d866dbf6 483 /* triggers blkg destruction if no online users left */
397c9f46 484 blkcg_unpin_online(wb->blkcg_css);
59b57717 485
841710aa 486 fprop_local_destroy_percpu(&wb->memcg_completions);
c22d70a1
RG
487
488 spin_lock_irq(&cgwb_lock);
489 list_del(&wb->offline_node);
490 spin_unlock_irq(&cgwb_lock);
491
b43a9e76 492 percpu_ref_exit(&wb->refcnt);
52ebea74 493 wb_exit(wb);
efee1713 494 bdi_put(bdi);
f3b6a6df 495 WARN_ON_ONCE(!list_empty(&wb->b_attached));
52ebea74 496 kfree_rcu(wb, rcu);
52ebea74
TH
497}
498
499static void cgwb_release(struct percpu_ref *refcnt)
500{
501 struct bdi_writeback *wb = container_of(refcnt, struct bdi_writeback,
502 refcnt);
f1834646 503 queue_work(cgwb_release_wq, &wb->release_work);
52ebea74
TH
504}
505
506static void cgwb_kill(struct bdi_writeback *wb)
507{
508 lockdep_assert_held(&cgwb_lock);
509
510 WARN_ON(!radix_tree_delete(&wb->bdi->cgwb_tree, wb->memcg_css->id));
511 list_del(&wb->memcg_node);
512 list_del(&wb->blkcg_node);
c22d70a1 513 list_add(&wb->offline_node, &offline_cgwbs);
52ebea74
TH
514 percpu_ref_kill(&wb->refcnt);
515}
516
e8cb72b3
JK
517static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb)
518{
519 spin_lock_irq(&cgwb_lock);
520 list_del_rcu(&wb->bdi_node);
521 spin_unlock_irq(&cgwb_lock);
522}
523
52ebea74
TH
524static int cgwb_create(struct backing_dev_info *bdi,
525 struct cgroup_subsys_state *memcg_css, gfp_t gfp)
526{
527 struct mem_cgroup *memcg;
528 struct cgroup_subsys_state *blkcg_css;
52ebea74
TH
529 struct list_head *memcg_cgwb_list, *blkcg_cgwb_list;
530 struct bdi_writeback *wb;
531 unsigned long flags;
532 int ret = 0;
533
534 memcg = mem_cgroup_from_css(memcg_css);
c165b3e3 535 blkcg_css = cgroup_get_e_css(memcg_css->cgroup, &io_cgrp_subsys);
9ccc3617 536 memcg_cgwb_list = &memcg->cgwb_list;
dec223c9 537 blkcg_cgwb_list = blkcg_get_cgwb_list(blkcg_css);
52ebea74
TH
538
539 /* look up again under lock and discard on blkcg mismatch */
540 spin_lock_irqsave(&cgwb_lock, flags);
541 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
542 if (wb && wb->blkcg_css != blkcg_css) {
543 cgwb_kill(wb);
544 wb = NULL;
545 }
546 spin_unlock_irqrestore(&cgwb_lock, flags);
547 if (wb)
548 goto out_put;
549
550 /* need to create a new one */
551 wb = kmalloc(sizeof(*wb), gfp);
0b045bd1
CJ
552 if (!wb) {
553 ret = -ENOMEM;
554 goto out_put;
555 }
52ebea74 556
8c911f3d 557 ret = wb_init(wb, bdi, gfp);
52ebea74
TH
558 if (ret)
559 goto err_free;
560
561 ret = percpu_ref_init(&wb->refcnt, cgwb_release, 0, gfp);
562 if (ret)
563 goto err_wb_exit;
564
841710aa
TH
565 ret = fprop_local_init_percpu(&wb->memcg_completions, gfp);
566 if (ret)
567 goto err_ref_exit;
568
52ebea74
TH
569 wb->memcg_css = memcg_css;
570 wb->blkcg_css = blkcg_css;
f3b6a6df 571 INIT_LIST_HEAD(&wb->b_attached);
52ebea74
TH
572 INIT_WORK(&wb->release_work, cgwb_release_workfn);
573 set_bit(WB_registered, &wb->state);
efee1713 574 bdi_get(bdi);
03ba3782
JA
575
576 /*
52ebea74
TH
577 * The root wb determines the registered state of the whole bdi and
578 * memcg_cgwb_list and blkcg_cgwb_list's next pointers indicate
579 * whether they're still online. Don't link @wb if any is dead.
580 * See wb_memcg_offline() and wb_blkcg_offline().
03ba3782 581 */
52ebea74
TH
582 ret = -ENODEV;
583 spin_lock_irqsave(&cgwb_lock, flags);
584 if (test_bit(WB_registered, &bdi->wb.state) &&
585 blkcg_cgwb_list->next && memcg_cgwb_list->next) {
586 /* we might have raced another instance of this function */
587 ret = radix_tree_insert(&bdi->cgwb_tree, memcg_css->id, wb);
588 if (!ret) {
b817525a 589 list_add_tail_rcu(&wb->bdi_node, &bdi->wb_list);
52ebea74
TH
590 list_add(&wb->memcg_node, memcg_cgwb_list);
591 list_add(&wb->blkcg_node, blkcg_cgwb_list);
397c9f46 592 blkcg_pin_online(blkcg_css);
52ebea74
TH
593 css_get(memcg_css);
594 css_get(blkcg_css);
595 }
596 }
597 spin_unlock_irqrestore(&cgwb_lock, flags);
598 if (ret) {
599 if (ret == -EEXIST)
600 ret = 0;
a13f35e8 601 goto err_fprop_exit;
52ebea74
TH
602 }
603 goto out_put;
604
841710aa 605err_fprop_exit:
efee1713 606 bdi_put(bdi);
841710aa 607 fprop_local_destroy_percpu(&wb->memcg_completions);
52ebea74
TH
608err_ref_exit:
609 percpu_ref_exit(&wb->refcnt);
610err_wb_exit:
611 wb_exit(wb);
612err_free:
613 kfree(wb);
614out_put:
615 css_put(blkcg_css);
616 return ret;
66f3b8e2
JA
617}
618
52ebea74 619/**
ed288dc0 620 * wb_get_lookup - get wb for a given memcg
52ebea74
TH
621 * @bdi: target bdi
622 * @memcg_css: cgroup_subsys_state of the target memcg (must have positive ref)
52ebea74 623 *
ed288dc0
TH
624 * Try to get the wb for @memcg_css on @bdi. The returned wb has its
625 * refcount incremented.
52ebea74
TH
626 *
627 * This function uses css_get() on @memcg_css and thus expects its refcnt
628 * to be positive on invocation. IOW, rcu_read_lock() protection on
629 * @memcg_css isn't enough. try_get it before calling this function.
630 *
631 * A wb is keyed by its associated memcg. As blkcg implicitly enables
632 * memcg on the default hierarchy, memcg association is guaranteed to be
633 * more specific (equal or descendant to the associated blkcg) and thus can
634 * identify both the memcg and blkcg associations.
635 *
636 * Because the blkcg associated with a memcg may change as blkcg is enabled
637 * and disabled closer to root in the hierarchy, each wb keeps track of
638 * both the memcg and blkcg associated with it and verifies the blkcg on
639 * each lookup. On mismatch, the existing wb is discarded and a new one is
640 * created.
641 */
ed288dc0
TH
642struct bdi_writeback *wb_get_lookup(struct backing_dev_info *bdi,
643 struct cgroup_subsys_state *memcg_css)
644{
645 struct bdi_writeback *wb;
646
647 if (!memcg_css->parent)
648 return &bdi->wb;
649
650 rcu_read_lock();
651 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
652 if (wb) {
653 struct cgroup_subsys_state *blkcg_css;
654
655 /* see whether the blkcg association has changed */
656 blkcg_css = cgroup_get_e_css(memcg_css->cgroup, &io_cgrp_subsys);
657 if (unlikely(wb->blkcg_css != blkcg_css || !wb_tryget(wb)))
658 wb = NULL;
659 css_put(blkcg_css);
660 }
661 rcu_read_unlock();
662
663 return wb;
664}
665
666/**
667 * wb_get_create - get wb for a given memcg, create if necessary
668 * @bdi: target bdi
669 * @memcg_css: cgroup_subsys_state of the target memcg (must have positive ref)
670 * @gfp: allocation mask to use
671 *
672 * Try to get the wb for @memcg_css on @bdi. If it doesn't exist, try to
673 * create one. See wb_get_lookup() for more details.
674 */
52ebea74
TH
675struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
676 struct cgroup_subsys_state *memcg_css,
677 gfp_t gfp)
6467716a 678{
52ebea74
TH
679 struct bdi_writeback *wb;
680
c1ca59a1 681 might_alloc(gfp);
52ebea74
TH
682
683 if (!memcg_css->parent)
684 return &bdi->wb;
685
686 do {
ed288dc0 687 wb = wb_get_lookup(bdi, memcg_css);
52ebea74
TH
688 } while (!wb && !cgwb_create(bdi, memcg_css, gfp));
689
690 return wb;
691}
6467716a 692
a13f35e8 693static int cgwb_bdi_init(struct backing_dev_info *bdi)
52ebea74 694{
a13f35e8
TH
695 int ret;
696
52ebea74 697 INIT_RADIX_TREE(&bdi->cgwb_tree, GFP_ATOMIC);
3ee7e869 698 mutex_init(&bdi->cgwb_release_mutex);
7fc5854f 699 init_rwsem(&bdi->wb_switch_rwsem);
a13f35e8 700
8c911f3d 701 ret = wb_init(&bdi->wb, bdi, GFP_KERNEL);
a13f35e8 702 if (!ret) {
7d828602 703 bdi->wb.memcg_css = &root_mem_cgroup->css;
a13f35e8
TH
704 bdi->wb.blkcg_css = blkcg_root_css;
705 }
706 return ret;
6467716a
AB
707}
708
b1c51afc 709static void cgwb_bdi_unregister(struct backing_dev_info *bdi)
52ebea74
TH
710{
711 struct radix_tree_iter iter;
712 void **slot;
5318ce7d 713 struct bdi_writeback *wb;
52ebea74
TH
714
715 WARN_ON(test_bit(WB_registered, &bdi->wb.state));
716
717 spin_lock_irq(&cgwb_lock);
718 radix_tree_for_each_slot(slot, &bdi->cgwb_tree, &iter, 0)
719 cgwb_kill(*slot);
3ee7e869 720 spin_unlock_irq(&cgwb_lock);
5318ce7d 721
3ee7e869
JK
722 mutex_lock(&bdi->cgwb_release_mutex);
723 spin_lock_irq(&cgwb_lock);
5318ce7d
JK
724 while (!list_empty(&bdi->wb_list)) {
725 wb = list_first_entry(&bdi->wb_list, struct bdi_writeback,
726 bdi_node);
727 spin_unlock_irq(&cgwb_lock);
728 wb_shutdown(wb);
729 spin_lock_irq(&cgwb_lock);
730 }
52ebea74 731 spin_unlock_irq(&cgwb_lock);
3ee7e869 732 mutex_unlock(&bdi->cgwb_release_mutex);
52ebea74
TH
733}
734
c22d70a1
RG
735/*
736 * cleanup_offline_cgwbs_workfn - try to release dying cgwbs
737 *
738 * Try to release dying cgwbs by switching attached inodes to the nearest
739 * living ancestor's writeback. Processed wbs are placed at the end
740 * of the list to guarantee the forward progress.
741 */
742static void cleanup_offline_cgwbs_workfn(struct work_struct *work)
743{
744 struct bdi_writeback *wb;
745 LIST_HEAD(processed);
746
747 spin_lock_irq(&cgwb_lock);
748
749 while (!list_empty(&offline_cgwbs)) {
750 wb = list_first_entry(&offline_cgwbs, struct bdi_writeback,
751 offline_node);
752 list_move(&wb->offline_node, &processed);
753
754 /*
755 * If wb is dirty, cleaning up the writeback by switching
756 * attached inodes will result in an effective removal of any
757 * bandwidth restrictions, which isn't the goal. Instead,
758 * it can be postponed until the next time, when all io
759 * will be likely completed. If in the meantime some inodes
760 * will get re-dirtied, they should be eventually switched to
761 * a new cgwb.
762 */
763 if (wb_has_dirty_io(wb))
764 continue;
765
766 if (!wb_tryget(wb))
767 continue;
768
769 spin_unlock_irq(&cgwb_lock);
770 while (cleanup_offline_cgwb(wb))
771 cond_resched();
772 spin_lock_irq(&cgwb_lock);
773
774 wb_put(wb);
775 }
776
777 if (!list_empty(&processed))
778 list_splice_tail(&processed, &offline_cgwbs);
779
780 spin_unlock_irq(&cgwb_lock);
781}
782
52ebea74
TH
783/**
784 * wb_memcg_offline - kill all wb's associated with a memcg being offlined
785 * @memcg: memcg being offlined
786 *
787 * Also prevents creation of any new wb's associated with @memcg.
e98be2d5 788 */
52ebea74
TH
789void wb_memcg_offline(struct mem_cgroup *memcg)
790{
9ccc3617 791 struct list_head *memcg_cgwb_list = &memcg->cgwb_list;
52ebea74
TH
792 struct bdi_writeback *wb, *next;
793
794 spin_lock_irq(&cgwb_lock);
795 list_for_each_entry_safe(wb, next, memcg_cgwb_list, memcg_node)
796 cgwb_kill(wb);
797 memcg_cgwb_list->next = NULL; /* prevent new wb's */
798 spin_unlock_irq(&cgwb_lock);
c22d70a1
RG
799
800 queue_work(system_unbound_wq, &cleanup_offline_cgwbs_work);
52ebea74
TH
801}
802
803/**
804 * wb_blkcg_offline - kill all wb's associated with a blkcg being offlined
dec223c9 805 * @css: blkcg being offlined
52ebea74
TH
806 *
807 * Also prevents creation of any new wb's associated with @blkcg.
808 */
dec223c9 809void wb_blkcg_offline(struct cgroup_subsys_state *css)
52ebea74 810{
52ebea74 811 struct bdi_writeback *wb, *next;
dec223c9 812 struct list_head *list = blkcg_get_cgwb_list(css);
52ebea74
TH
813
814 spin_lock_irq(&cgwb_lock);
dec223c9 815 list_for_each_entry_safe(wb, next, list, blkcg_node)
52ebea74 816 cgwb_kill(wb);
dec223c9 817 list->next = NULL; /* prevent new wb's */
52ebea74
TH
818 spin_unlock_irq(&cgwb_lock);
819}
820
e8cb72b3
JK
821static void cgwb_bdi_register(struct backing_dev_info *bdi)
822{
823 spin_lock_irq(&cgwb_lock);
824 list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
825 spin_unlock_irq(&cgwb_lock);
826}
827
f1834646
TH
828static int __init cgwb_init(void)
829{
830 /*
831 * There can be many concurrent release work items overwhelming
832 * system_wq. Put them in a separate wq and limit concurrency.
833 * There's no point in executing many of these in parallel.
834 */
835 cgwb_release_wq = alloc_workqueue("cgwb_release", 0, 1);
836 if (!cgwb_release_wq)
837 return -ENOMEM;
838
839 return 0;
840}
841subsys_initcall(cgwb_init);
842
52ebea74
TH
843#else /* CONFIG_CGROUP_WRITEBACK */
844
a13f35e8
TH
845static int cgwb_bdi_init(struct backing_dev_info *bdi)
846{
8c911f3d 847 return wb_init(&bdi->wb, bdi, GFP_KERNEL);
a13f35e8
TH
848}
849
b1c51afc 850static void cgwb_bdi_unregister(struct backing_dev_info *bdi) { }
df23de55 851
e8cb72b3
JK
852static void cgwb_bdi_register(struct backing_dev_info *bdi)
853{
854 list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
855}
856
857static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb)
858{
859 list_del_rcu(&wb->bdi_node);
860}
861
52ebea74 862#endif /* CONFIG_CGROUP_WRITEBACK */
e98be2d5 863
4bca7e80 864int bdi_init(struct backing_dev_info *bdi)
b2e8fb6e 865{
cf0ca9fe
PZ
866 bdi->dev = NULL;
867
d03f6cdc 868 kref_init(&bdi->refcnt);
189d3c4a 869 bdi->min_ratio = 0;
ae82291e 870 bdi->max_ratio = 100 * BDI_RATIO_SCALE;
eb608e3a 871 bdi->max_prop_frac = FPROP_FRAC_BASE;
66f3b8e2 872 INIT_LIST_HEAD(&bdi->bdi_list);
b817525a 873 INIT_LIST_HEAD(&bdi->wb_list);
cc395d7f 874 init_waitqueue_head(&bdi->wb_waitq);
03ba3782 875
3083da7b 876 return cgwb_bdi_init(bdi);
b2e8fb6e 877}
e98be2d5 878
aef33c2f 879struct backing_dev_info *bdi_alloc(int node_id)
d03f6cdc
JK
880{
881 struct backing_dev_info *bdi;
882
aef33c2f 883 bdi = kzalloc_node(sizeof(*bdi), GFP_KERNEL, node_id);
d03f6cdc
JK
884 if (!bdi)
885 return NULL;
886
887 if (bdi_init(bdi)) {
888 kfree(bdi);
889 return NULL;
890 }
f56753ac 891 bdi->capabilities = BDI_CAP_WRITEBACK | BDI_CAP_WRITEBACK_ACCT;
55b2598e
CH
892 bdi->ra_pages = VM_READAHEAD_PAGES;
893 bdi->io_pages = VM_READAHEAD_PAGES;
5ed964f8 894 timer_setup(&bdi->laptop_mode_wb_timer, laptop_mode_timer_fn, 0);
d03f6cdc
JK
895 return bdi;
896}
aef33c2f 897EXPORT_SYMBOL(bdi_alloc);
d03f6cdc 898
34f8fe50
TH
899static struct rb_node **bdi_lookup_rb_node(u64 id, struct rb_node **parentp)
900{
901 struct rb_node **p = &bdi_tree.rb_node;
902 struct rb_node *parent = NULL;
903 struct backing_dev_info *bdi;
904
905 lockdep_assert_held(&bdi_lock);
906
907 while (*p) {
908 parent = *p;
909 bdi = rb_entry(parent, struct backing_dev_info, rb_node);
910
911 if (bdi->id > id)
912 p = &(*p)->rb_left;
913 else if (bdi->id < id)
914 p = &(*p)->rb_right;
915 else
916 break;
917 }
918
919 if (parentp)
920 *parentp = parent;
921 return p;
922}
923
924/**
925 * bdi_get_by_id - lookup and get bdi from its id
926 * @id: bdi id to lookup
927 *
928 * Find bdi matching @id and get it. Returns NULL if the matching bdi
929 * doesn't exist or is already unregistered.
930 */
931struct backing_dev_info *bdi_get_by_id(u64 id)
932{
933 struct backing_dev_info *bdi = NULL;
934 struct rb_node **p;
935
936 spin_lock_bh(&bdi_lock);
937 p = bdi_lookup_rb_node(id, NULL);
938 if (*p) {
939 bdi = rb_entry(*p, struct backing_dev_info, rb_node);
940 bdi_get(bdi);
941 }
942 spin_unlock_bh(&bdi_lock);
943
944 return bdi;
945}
946
7c4cc300 947int bdi_register_va(struct backing_dev_info *bdi, const char *fmt, va_list args)
46100071 948{
46100071 949 struct device *dev;
34f8fe50 950 struct rb_node *parent, **p;
e98be2d5 951
46100071
TH
952 if (bdi->dev) /* The driver needs to use separate queues per device */
953 return 0;
e98be2d5 954
6bd87eec
CH
955 vsnprintf(bdi->dev_name, sizeof(bdi->dev_name), fmt, args);
956 dev = device_create(bdi_class, NULL, MKDEV(0, 0), bdi, bdi->dev_name);
46100071
TH
957 if (IS_ERR(dev))
958 return PTR_ERR(dev);
04fbfdc1 959
e8cb72b3 960 cgwb_bdi_register(bdi);
46100071 961 bdi->dev = dev;
b2e8fb6e 962
6d0e4827 963 bdi_debug_register(bdi, dev_name(dev));
46100071
TH
964 set_bit(WB_registered, &bdi->wb.state);
965
966 spin_lock_bh(&bdi_lock);
34f8fe50
TH
967
968 bdi->id = ++bdi_id_cursor;
969
970 p = bdi_lookup_rb_node(bdi->id, &parent);
971 rb_link_node(&bdi->rb_node, parent, p);
972 rb_insert_color(&bdi->rb_node, &bdi_tree);
973
46100071 974 list_add_tail_rcu(&bdi->bdi_list, &bdi_list);
34f8fe50 975
46100071
TH
976 spin_unlock_bh(&bdi_lock);
977
978 trace_writeback_bdi_register(bdi);
979 return 0;
b2e8fb6e 980}
baf7a616 981
7c4cc300 982int bdi_register(struct backing_dev_info *bdi, const char *fmt, ...)
baf7a616
JK
983{
984 va_list args;
985 int ret;
986
987 va_start(args, fmt);
7c4cc300 988 ret = bdi_register_va(bdi, fmt, args);
baf7a616
JK
989 va_end(args);
990 return ret;
991}
46100071 992EXPORT_SYMBOL(bdi_register);
b2e8fb6e 993
3c5d202b 994void bdi_set_owner(struct backing_dev_info *bdi, struct device *owner)
df08c32c 995{
3c5d202b 996 WARN_ON_ONCE(bdi->owner);
df08c32c
DW
997 bdi->owner = owner;
998 get_device(owner);
df08c32c 999}
df08c32c 1000
46100071
TH
1001/*
1002 * Remove bdi from bdi_list, and ensure that it is no longer visible
1003 */
1004static void bdi_remove_from_list(struct backing_dev_info *bdi)
1005{
1006 spin_lock_bh(&bdi_lock);
34f8fe50 1007 rb_erase(&bdi->rb_node, &bdi_tree);
46100071
TH
1008 list_del_rcu(&bdi->bdi_list);
1009 spin_unlock_bh(&bdi_lock);
b2e8fb6e 1010
46100071
TH
1011 synchronize_rcu_expedited();
1012}
cf0ca9fe 1013
b02176f3 1014void bdi_unregister(struct backing_dev_info *bdi)
b2e8fb6e 1015{
5ed964f8
CH
1016 del_timer_sync(&bdi->laptop_mode_wb_timer);
1017
f0054bb1
TH
1018 /* make sure nobody finds us on the bdi_list anymore */
1019 bdi_remove_from_list(bdi);
1020 wb_shutdown(&bdi->wb);
b1c51afc 1021 cgwb_bdi_unregister(bdi);
7a401a97 1022
3c376dfa
ML
1023 /*
1024 * If this BDI's min ratio has been set, use bdi_set_min_ratio() to
1025 * update the global bdi_min_ratio.
1026 */
1027 if (bdi->min_ratio)
1028 bdi_set_min_ratio(bdi, 0);
1029
c4db59d3
CH
1030 if (bdi->dev) {
1031 bdi_debug_unregister(bdi);
1032 device_unregister(bdi->dev);
1033 bdi->dev = NULL;
1034 }
df08c32c
DW
1035
1036 if (bdi->owner) {
1037 put_device(bdi->owner);
1038 bdi->owner = NULL;
1039 }
b02176f3 1040}
c6fd3ac0 1041EXPORT_SYMBOL(bdi_unregister);
c4db59d3 1042
d03f6cdc
JK
1043static void release_bdi(struct kref *ref)
1044{
1045 struct backing_dev_info *bdi =
1046 container_of(ref, struct backing_dev_info, refcnt);
1047
702f2d1e 1048 WARN_ON_ONCE(test_bit(WB_registered, &bdi->wb.state));
2e82b84c
JK
1049 WARN_ON_ONCE(bdi->dev);
1050 wb_exit(&bdi->wb);
d03f6cdc
JK
1051 kfree(bdi);
1052}
1053
1054void bdi_put(struct backing_dev_info *bdi)
1055{
1056 kref_put(&bdi->refcnt, release_bdi);
1057}
62bf42ad 1058EXPORT_SYMBOL(bdi_put);
d03f6cdc 1059
ccdf7741
CH
1060struct backing_dev_info *inode_to_bdi(struct inode *inode)
1061{
1062 struct super_block *sb;
1063
1064 if (!inode)
1065 return &noop_backing_dev_info;
1066
1067 sb = inode->i_sb;
1068#ifdef CONFIG_BLOCK
1069 if (sb_is_blkdev_sb(sb))
1070 return I_BDEV(inode)->bd_disk->bdi;
1071#endif
1072 return sb->s_bdi;
1073}
1074EXPORT_SYMBOL(inode_to_bdi);
1075
eb7ae5e0
CH
1076const char *bdi_dev_name(struct backing_dev_info *bdi)
1077{
1078 if (!bdi || !bdi->dev)
1079 return bdi_unknown_name;
6bd87eec 1080 return bdi->dev_name;
eb7ae5e0
CH
1081}
1082EXPORT_SYMBOL_GPL(bdi_dev_name);