blob: a53b9360b72ec8cf3bfd8af86486a56de1b6be5a [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Andrew Morton3fcfab12006-10-19 23:28:16 -07002
Christoph Hellwigc97ab272022-04-20 06:27:19 +02003#include <linux/blkdev.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -07004#include <linux/wait.h>
Tejun Heo34f8fe52019-08-26 09:06:53 -07005#include <linux/rbtree.h>
Jens Axboe03ba3782009-09-09 09:08:54 +02006#include <linux/kthread.h>
Christoph Hellwige41d12f2021-09-20 14:33:13 +02007#include <linux/backing-dev.h>
8#include <linux/blk-cgroup.h>
Jens Axboe03ba3782009-09-09 09:08:54 +02009#include <linux/freezer.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070010#include <linux/fs.h>
Jens Axboe26160152009-03-17 09:35:06 +010011#include <linux/pagemap.h>
Jens Axboe03ba3782009-09-09 09:08:54 +020012#include <linux/mm.h>
Daniel Vetterc1ca59a2021-02-25 17:18:45 -080013#include <linux/sched/mm.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070014#include <linux/sched.h>
15#include <linux/module.h>
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070016#include <linux/writeback.h>
17#include <linux/device.h>
Dave Chinner455b2862010-07-07 13:24:06 +100018#include <trace/events/writeback.h>
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070019
Christoph Hellwigf56753a2020-09-24 08:51:40 +020020struct backing_dev_info noop_backing_dev_info;
Tejun Heoa212b102015-05-22 17:13:33 -040021EXPORT_SYMBOL_GPL(noop_backing_dev_info);
Jörn Engel5129a462010-04-25 08:54:42 +020022
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -070023static struct class *bdi_class;
Christoph Hellwigeb7ae5e2020-05-04 14:47:54 +020024static const char *bdi_unknown_name = "(unknown)";
Jens Axboecfc4ba52009-09-14 13:12:40 +020025
26/*
Tejun Heo34f8fe52019-08-26 09:06:53 -070027 * bdi_lock protects bdi_tree and updates to bdi_list. bdi_list has RCU
28 * reader side locking.
Jens Axboecfc4ba52009-09-14 13:12:40 +020029 */
Jens Axboe03ba3782009-09-09 09:08:54 +020030DEFINE_SPINLOCK(bdi_lock);
Tejun Heo34f8fe52019-08-26 09:06:53 -070031static u64 bdi_id_cursor;
32static struct rb_root bdi_tree = RB_ROOT;
Jens Axboe66f3b8e2009-09-02 09:19:46 +020033LIST_HEAD(bdi_list);
Jens Axboe03ba3782009-09-09 09:08:54 +020034
Tejun Heo839a8e82013-04-01 19:08:06 -070035/* bdi_wq serves all asynchronous writeback tasks */
36struct workqueue_struct *bdi_wq;
37
Baolin Wang6986c3e2021-02-24 12:02:52 -080038#define K(x) ((x) << (PAGE_SHIFT - 10))
39
Miklos Szeredi76f14182008-04-30 00:54:36 -070040#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;
Christoph Hellwigc1955ce2010-06-19 23:08:06 +020054 struct bdi_writeback *wb = &bdi->wb;
David Rientjes364aeb22009-01-06 14:39:29 -080055 unsigned long background_thresh;
56 unsigned long dirty_thresh;
Tejun Heo0d960a32015-05-22 18:23:19 -040057 unsigned long wb_thresh;
Theodore Ts'o0ae45f62015-02-02 00:37:00 -050058 unsigned long nr_dirty, nr_io, nr_more_io, nr_dirty_time;
Jens Axboef09b00d2009-05-25 09:08:21 +020059 struct inode *inode;
60
Theodore Ts'o0ae45f62015-02-02 00:37:00 -050061 nr_dirty = nr_io = nr_more_io = nr_dirty_time = 0;
Christoph Hellwigf758eea2011-04-21 18:19:44 -060062 spin_lock(&wb->list_lock);
Dave Chinnerc7f54082015-03-04 14:07:22 -050063 list_for_each_entry(inode, &wb->b_dirty, i_io_list)
Christoph Hellwigc1955ce2010-06-19 23:08:06 +020064 nr_dirty++;
Dave Chinnerc7f54082015-03-04 14:07:22 -050065 list_for_each_entry(inode, &wb->b_io, i_io_list)
Christoph Hellwigc1955ce2010-06-19 23:08:06 +020066 nr_io++;
Dave Chinnerc7f54082015-03-04 14:07:22 -050067 list_for_each_entry(inode, &wb->b_more_io, i_io_list)
Christoph Hellwigc1955ce2010-06-19 23:08:06 +020068 nr_more_io++;
Dave Chinnerc7f54082015-03-04 14:07:22 -050069 list_for_each_entry(inode, &wb->b_dirty_time, i_io_list)
Theodore Ts'o0ae45f62015-02-02 00:37:00 -050070 if (inode->i_state & I_DIRTY_TIME)
71 nr_dirty_time++;
Christoph Hellwigf758eea2011-04-21 18:19:44 -060072 spin_unlock(&wb->list_lock);
Miklos Szeredi76f14182008-04-30 00:54:36 -070073
Wu Fengguang16c40422010-08-11 14:17:39 -070074 global_dirty_limits(&background_thresh, &dirty_thresh);
Tejun Heo0d960a32015-05-22 18:23:19 -040075 wb_thresh = wb_calc_thresh(wb, dirty_thresh);
Miklos Szeredi76f14182008-04-30 00:54:36 -070076
Miklos Szeredi76f14182008-04-30 00:54:36 -070077 seq_printf(m,
Wu Fengguang00821b02010-08-29 11:28:45 -060078 "BdiWriteback: %10lu kB\n"
79 "BdiReclaimable: %10lu kB\n"
80 "BdiDirtyThresh: %10lu kB\n"
81 "DirtyThresh: %10lu kB\n"
82 "BackgroundThresh: %10lu kB\n"
Wu Fengguangc8e28ce2011-01-23 10:07:47 -060083 "BdiDirtied: %10lu kB\n"
Wu Fengguang00821b02010-08-29 11:28:45 -060084 "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"
Theodore Ts'o0ae45f62015-02-02 00:37:00 -050089 "b_dirty_time: %10lu\n"
Wu Fengguang00821b02010-08-29 11:28:45 -060090 "bdi_list: %10u\n"
91 "state: %10lx\n",
Tejun Heo93f78d82015-05-22 17:13:27 -040092 (unsigned long) K(wb_stat(wb, WB_WRITEBACK)),
93 (unsigned long) K(wb_stat(wb, WB_RECLAIMABLE)),
Tejun Heo0d960a32015-05-22 18:23:19 -040094 K(wb_thresh),
Jan Karaf7d2b1e2010-12-08 22:44:24 -060095 K(dirty_thresh),
96 K(background_thresh),
Tejun Heo93f78d82015-05-22 17:13:27 -040097 (unsigned long) K(wb_stat(wb, WB_DIRTIED)),
98 (unsigned long) K(wb_stat(wb, WB_WRITTEN)),
Tejun Heoa88a3412015-05-22 17:13:28 -040099 (unsigned long) K(wb->write_bandwidth),
Jan Karaf7d2b1e2010-12-08 22:44:24 -0600100 nr_dirty,
101 nr_io,
102 nr_more_io,
Theodore Ts'o0ae45f62015-02-02 00:37:00 -0500103 nr_dirty_time,
Tejun Heo44522262015-05-22 17:13:26 -0400104 !list_empty(&bdi->bdi_list), bdi->wb.state);
Miklos Szeredi76f14182008-04-30 00:54:36 -0700105
106 return 0;
107}
Andy Shevchenko5ad35092018-04-05 16:23:16 -0700108DEFINE_SHOW_ATTRIBUTE(bdi_debug_stats);
Miklos Szeredi76f14182008-04-30 00:54:36 -0700109
Greg Kroah-Hartman2d146b92019-01-22 16:21:07 +0100110static void bdi_debug_register(struct backing_dev_info *bdi, const char *name)
Miklos Szeredi76f14182008-04-30 00:54:36 -0700111{
112 bdi->debug_dir = debugfs_create_dir(name, bdi_debug_root);
weiping zhang97f07692017-10-31 18:37:54 +0800113
Greg Kroah-Hartman2d146b92019-01-22 16:21:07 +0100114 debugfs_create_file("stats", 0444, bdi->debug_dir, bdi,
115 &bdi_debug_stats_fops);
Miklos Szeredi76f14182008-04-30 00:54:36 -0700116}
117
118static void bdi_debug_unregister(struct backing_dev_info *bdi)
119{
Greg Kroah-Hartman2d146b92019-01-22 16:21:07 +0100120 debugfs_remove_recursive(bdi->debug_dir);
Miklos Szeredi76f14182008-04-30 00:54:36 -0700121}
122#else
123static inline void bdi_debug_init(void)
124{
125}
Greg Kroah-Hartman2d146b92019-01-22 16:21:07 +0100126static inline void bdi_debug_register(struct backing_dev_info *bdi,
Miklos Szeredi76f14182008-04-30 00:54:36 -0700127 const char *name)
128{
129}
130static inline void bdi_debug_unregister(struct backing_dev_info *bdi)
131{
132}
133#endif
134
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700135static 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);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700140 unsigned long read_ahead_kb;
Namjae Jeon7034ed12012-08-25 16:57:27 +0800141 ssize_t ret;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700142
Namjae Jeon7034ed12012-08-25 16:57:27 +0800143 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;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700150}
151
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700152#define BDI_SHOW(name, expr) \
153static ssize_t name##_show(struct device *dev, \
Joe Perches5e4c0d82020-12-14 19:14:50 -0800154 struct device_attribute *attr, char *buf) \
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700155{ \
156 struct backing_dev_info *bdi = dev_get_drvdata(dev); \
157 \
Joe Perches5e4c0d82020-12-14 19:14:50 -0800158 return sysfs_emit(buf, "%lld\n", (long long)expr); \
Greg Kroah-Hartmand9e12412013-07-24 15:05:26 -0700159} \
160static DEVICE_ATTR_RW(name);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700161
162BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
163
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700164static 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);
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700168 unsigned int ratio;
Namjae Jeon7034ed12012-08-25 16:57:27 +0800169 ssize_t ret;
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700170
Namjae Jeon7034ed12012-08-25 16:57:27 +0800171 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;
178
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700179 return ret;
180}
Stefan Roeschae822912022-11-18 16:51:59 -0800181BDI_SHOW(min_ratio, bdi->min_ratio / BDI_RATIO_SCALE)
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700182
Stefan Roeschad3e6da2022-11-18 16:52:14 -0800183static ssize_t min_ratio_fine_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);
187 unsigned int ratio;
188 ssize_t ret;
189
190 ret = kstrtouint(buf, 10, &ratio);
191 if (ret < 0)
192 return ret;
193
194 ret = bdi_set_min_ratio_no_scale(bdi, ratio);
195 if (!ret)
196 ret = count;
197
198 return ret;
199}
200BDI_SHOW(min_ratio_fine, bdi->min_ratio)
201
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700202static ssize_t max_ratio_store(struct device *dev,
203 struct device_attribute *attr, const char *buf, size_t count)
204{
205 struct backing_dev_info *bdi = dev_get_drvdata(dev);
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700206 unsigned int ratio;
Namjae Jeon7034ed12012-08-25 16:57:27 +0800207 ssize_t ret;
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700208
Namjae Jeon7034ed12012-08-25 16:57:27 +0800209 ret = kstrtouint(buf, 10, &ratio);
210 if (ret < 0)
211 return ret;
212
213 ret = bdi_set_max_ratio(bdi, ratio);
214 if (!ret)
215 ret = count;
216
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700217 return ret;
218}
Stefan Roeschae822912022-11-18 16:51:59 -0800219BDI_SHOW(max_ratio, bdi->max_ratio / BDI_RATIO_SCALE)
Peter Zijlstraa42dde02008-04-30 00:54:36 -0700220
Stefan Roeschbca52dc2022-11-18 16:52:11 -0800221static ssize_t max_ratio_fine_store(struct device *dev,
222 struct device_attribute *attr, const char *buf, size_t count)
223{
224 struct backing_dev_info *bdi = dev_get_drvdata(dev);
225 unsigned int ratio;
226 ssize_t ret;
227
228 ret = kstrtouint(buf, 10, &ratio);
229 if (ret < 0)
230 return ret;
231
232 ret = bdi_set_max_ratio_no_scale(bdi, ratio);
233 if (!ret)
234 ret = count;
235
236 return ret;
237}
238BDI_SHOW(max_ratio_fine, bdi->max_ratio)
239
Stefan Roesch9c848192022-11-18 16:52:08 -0800240static ssize_t min_bytes_show(struct device *dev,
241 struct device_attribute *attr,
242 char *buf)
243{
244 struct backing_dev_info *bdi = dev_get_drvdata(dev);
245
246 return sysfs_emit(buf, "%llu\n", bdi_get_min_bytes(bdi));
247}
248
249static ssize_t min_bytes_store(struct device *dev,
250 struct device_attribute *attr, const char *buf, size_t count)
251{
252 struct backing_dev_info *bdi = dev_get_drvdata(dev);
253 u64 bytes;
254 ssize_t ret;
255
256 ret = kstrtoull(buf, 10, &bytes);
257 if (ret < 0)
258 return ret;
259
260 ret = bdi_set_min_bytes(bdi, bytes);
261 if (!ret)
262 ret = count;
263
264 return ret;
265}
266DEVICE_ATTR_RW(min_bytes);
267
Stefan Roeschc56e0492022-11-18 16:52:03 -0800268static ssize_t max_bytes_show(struct device *dev,
269 struct device_attribute *attr,
270 char *buf)
271{
272 struct backing_dev_info *bdi = dev_get_drvdata(dev);
273
274 return sysfs_emit(buf, "%llu\n", bdi_get_max_bytes(bdi));
275}
276
277static ssize_t max_bytes_store(struct device *dev,
278 struct device_attribute *attr, const char *buf, size_t count)
279{
280 struct backing_dev_info *bdi = dev_get_drvdata(dev);
281 u64 bytes;
282 ssize_t ret;
283
284 ret = kstrtoull(buf, 10, &bytes);
285 if (ret < 0)
286 return ret;
287
288 ret = bdi_set_max_bytes(bdi, bytes);
289 if (!ret)
290 ret = count;
291
292 return ret;
293}
294DEVICE_ATTR_RW(max_bytes);
295
Darrick J. Wong7d311cd2013-02-21 16:42:48 -0800296static ssize_t stable_pages_required_show(struct device *dev,
297 struct device_attribute *attr,
Joe Perches5e4c0d82020-12-14 19:14:50 -0800298 char *buf)
Darrick J. Wong7d311cd2013-02-21 16:42:48 -0800299{
Christoph Hellwig1cb039f2020-09-24 08:51:38 +0200300 dev_warn_once(dev,
301 "the stable_pages_required attribute has been removed. Use the stable_writes queue attribute instead.\n");
Joe Perches5e4c0d82020-12-14 19:14:50 -0800302 return sysfs_emit(buf, "%d\n", 0);
Darrick J. Wong7d311cd2013-02-21 16:42:48 -0800303}
Greg Kroah-Hartmand9e12412013-07-24 15:05:26 -0700304static DEVICE_ATTR_RO(stable_pages_required);
Darrick J. Wong7d311cd2013-02-21 16:42:48 -0800305
Stefan Roesch27bbe9d2022-11-18 16:51:57 -0800306static ssize_t strict_limit_store(struct device *dev,
307 struct device_attribute *attr, const char *buf, size_t count)
308{
309 struct backing_dev_info *bdi = dev_get_drvdata(dev);
310 unsigned int strict_limit;
311 ssize_t ret;
312
313 ret = kstrtouint(buf, 10, &strict_limit);
314 if (ret < 0)
315 return ret;
316
317 ret = bdi_set_strict_limit(bdi, strict_limit);
318 if (!ret)
319 ret = count;
320
321 return ret;
322}
323
324static ssize_t strict_limit_show(struct device *dev,
325 struct device_attribute *attr, char *buf)
326{
327 struct backing_dev_info *bdi = dev_get_drvdata(dev);
328
329 return sysfs_emit(buf, "%d\n",
330 !!(bdi->capabilities & BDI_CAP_STRICTLIMIT));
331}
332static DEVICE_ATTR_RW(strict_limit);
333
Greg Kroah-Hartmand9e12412013-07-24 15:05:26 -0700334static struct attribute *bdi_dev_attrs[] = {
335 &dev_attr_read_ahead_kb.attr,
336 &dev_attr_min_ratio.attr,
Stefan Roeschad3e6da2022-11-18 16:52:14 -0800337 &dev_attr_min_ratio_fine.attr,
Greg Kroah-Hartmand9e12412013-07-24 15:05:26 -0700338 &dev_attr_max_ratio.attr,
Stefan Roeschbca52dc2022-11-18 16:52:11 -0800339 &dev_attr_max_ratio_fine.attr,
Stefan Roesch9c848192022-11-18 16:52:08 -0800340 &dev_attr_min_bytes.attr,
Stefan Roeschc56e0492022-11-18 16:52:03 -0800341 &dev_attr_max_bytes.attr,
Greg Kroah-Hartmand9e12412013-07-24 15:05:26 -0700342 &dev_attr_stable_pages_required.attr,
Stefan Roesch27bbe9d2022-11-18 16:51:57 -0800343 &dev_attr_strict_limit.attr,
Greg Kroah-Hartmand9e12412013-07-24 15:05:26 -0700344 NULL,
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700345};
Greg Kroah-Hartmand9e12412013-07-24 15:05:26 -0700346ATTRIBUTE_GROUPS(bdi_dev);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700347
348static __init int bdi_class_init(void)
349{
350 bdi_class = class_create(THIS_MODULE, "bdi");
Anton Blanchard14421452010-04-02 09:46:55 +0200351 if (IS_ERR(bdi_class))
352 return PTR_ERR(bdi_class);
353
Greg Kroah-Hartmand9e12412013-07-24 15:05:26 -0700354 bdi_class->dev_groups = bdi_dev_groups;
Miklos Szeredi76f14182008-04-30 00:54:36 -0700355 bdi_debug_init();
Jan Karad03f6cd2017-02-02 15:56:51 +0100356
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700357 return 0;
358}
Miklos Szeredi76f14182008-04-30 00:54:36 -0700359postcore_initcall(bdi_class_init);
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700360
Jens Axboe26160152009-03-17 09:35:06 +0100361static int __init default_bdi_init(void)
362{
Mika Westerberga2b90f12019-10-04 13:00:24 +0300363 bdi_wq = alloc_workqueue("writeback", WQ_MEM_RECLAIM | WQ_UNBOUND |
364 WQ_SYSFS, 0);
Tejun Heo839a8e82013-04-01 19:08:06 -0700365 if (!bdi_wq)
366 return -ENOMEM;
Jan Kara4bca7e82022-06-15 15:22:29 +0200367 return 0;
Jens Axboe26160152009-03-17 09:35:06 +0100368}
369subsys_initcall(default_bdi_init);
370
Artem Bityutskiy64677162010-07-25 14:29:22 +0300371/*
Tejun Heof0054bb2015-05-22 17:13:30 -0400372 * This function is used when the first inode for this wb is marked dirty. It
Artem Bityutskiy64677162010-07-25 14:29:22 +0300373 * wakes-up the corresponding bdi thread which should then take care of the
374 * periodic background write-out of dirty inodes. Since the write-out would
375 * starts only 'dirty_writeback_interval' centisecs from now anyway, we just
376 * set up a timer which wakes the bdi thread up later.
377 *
378 * Note, we wouldn't bother setting up the timer, but this function is on the
379 * fast-path (used by '__mark_inode_dirty()'), so we save few context switches
380 * by delaying the wake-up.
Derek Basehore6ca738d2014-04-03 14:46:22 -0700381 *
382 * We have to be careful not to postpone flush work if it is scheduled for
383 * earlier. Thus we use queue_delayed_work().
Artem Bityutskiy64677162010-07-25 14:29:22 +0300384 */
Tejun Heof0054bb2015-05-22 17:13:30 -0400385void wb_wakeup_delayed(struct bdi_writeback *wb)
Artem Bityutskiy64677162010-07-25 14:29:22 +0300386{
387 unsigned long timeout;
388
389 timeout = msecs_to_jiffies(dirty_writeback_interval * 10);
Khazhismel Kumykovf87904c2022-08-01 08:50:34 -0700390 spin_lock_irq(&wb->work_lock);
Tejun Heof0054bb2015-05-22 17:13:30 -0400391 if (test_bit(WB_registered, &wb->state))
392 queue_delayed_work(bdi_wq, &wb->dwork, timeout);
Khazhismel Kumykovf87904c2022-08-01 08:50:34 -0700393 spin_unlock_irq(&wb->work_lock);
Jens Axboe03ba3782009-09-09 09:08:54 +0200394}
395
Jan Kara45a29662021-09-02 14:53:09 -0700396static void wb_update_bandwidth_workfn(struct work_struct *work)
397{
398 struct bdi_writeback *wb = container_of(to_delayed_work(work),
399 struct bdi_writeback, bw_dwork);
400
401 wb_update_bandwidth(wb);
402}
403
Jens Axboecfc4ba52009-09-14 13:12:40 +0200404/*
Tejun Heoa88a3412015-05-22 17:13:28 -0400405 * Initial write bandwidth: 100 MB/s
Jens Axboecfc4ba52009-09-14 13:12:40 +0200406 */
Tejun Heoa88a3412015-05-22 17:13:28 -0400407#define INIT_BW (100 << (20 - PAGE_SHIFT))
Jens Axboecfc4ba52009-09-14 13:12:40 +0200408
Tejun Heo8395cd92015-05-22 17:13:34 -0400409static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
Christoph Hellwig8c911f32020-07-01 11:06:21 +0200410 gfp_t gfp)
Artem Bityutskiy64677162010-07-25 14:29:22 +0300411{
Tejun Heo93f78d82015-05-22 17:13:27 -0400412 int i, err;
413
Artem Bityutskiy64677162010-07-25 14:29:22 +0300414 memset(wb, 0, sizeof(*wb));
415
416 wb->bdi = bdi;
417 wb->last_old_flush = jiffies;
418 INIT_LIST_HEAD(&wb->b_dirty);
419 INIT_LIST_HEAD(&wb->b_io);
420 INIT_LIST_HEAD(&wb->b_more_io);
Theodore Ts'o0ae45f62015-02-02 00:37:00 -0500421 INIT_LIST_HEAD(&wb->b_dirty_time);
Christoph Hellwigf758eea2011-04-21 18:19:44 -0600422 spin_lock_init(&wb->list_lock);
Tejun Heo93f78d82015-05-22 17:13:27 -0400423
Jan Kara633a2ab2021-09-02 14:53:04 -0700424 atomic_set(&wb->writeback_inodes, 0);
Tejun Heoa88a3412015-05-22 17:13:28 -0400425 wb->bw_time_stamp = jiffies;
426 wb->balanced_dirty_ratelimit = INIT_BW;
427 wb->dirty_ratelimit = INIT_BW;
428 wb->write_bandwidth = INIT_BW;
429 wb->avg_write_bandwidth = INIT_BW;
430
Tejun Heof0054bb2015-05-22 17:13:30 -0400431 spin_lock_init(&wb->work_lock);
432 INIT_LIST_HEAD(&wb->work_list);
433 INIT_DELAYED_WORK(&wb->dwork, wb_workfn);
Jan Kara45a29662021-09-02 14:53:09 -0700434 INIT_DELAYED_WORK(&wb->bw_dwork, wb_update_bandwidth_workfn);
Jens Axboeb57d74a2016-09-01 10:20:33 -0600435 wb->dirty_sleep = jiffies;
Tejun Heof0054bb2015-05-22 17:13:30 -0400436
Tejun Heo8395cd92015-05-22 17:13:34 -0400437 err = fprop_local_init_percpu(&wb->completions, gfp);
Tejun Heoa88a3412015-05-22 17:13:28 -0400438 if (err)
Christoph Hellwigefee1712021-11-05 13:37:04 -0700439 return err;
Tejun Heoa88a3412015-05-22 17:13:28 -0400440
Tejun Heo93f78d82015-05-22 17:13:27 -0400441 for (i = 0; i < NR_WB_STAT_ITEMS; i++) {
Tejun Heo8395cd92015-05-22 17:13:34 -0400442 err = percpu_counter_init(&wb->stat[i], 0, gfp);
Tejun Heoa13f35e2015-07-02 08:44:34 -0600443 if (err)
444 goto out_destroy_stat;
Tejun Heo93f78d82015-05-22 17:13:27 -0400445 }
446
447 return 0;
Tejun Heoa13f35e2015-07-02 08:44:34 -0600448
449out_destroy_stat:
Rasmus Villemoes078c6c32016-02-11 16:13:06 -0800450 while (i--)
Tejun Heoa13f35e2015-07-02 08:44:34 -0600451 percpu_counter_destroy(&wb->stat[i]);
452 fprop_local_destroy_percpu(&wb->completions);
Tejun Heoa13f35e2015-07-02 08:44:34 -0600453 return err;
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700454}
Jens Axboe03ba3782009-09-09 09:08:54 +0200455
Jan Karae8cb72b2017-03-23 01:36:56 +0100456static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb);
457
Tejun Heo46100072015-05-22 17:13:31 -0400458/*
459 * Remove bdi from the global list and shutdown any threads we have running
460 */
461static void wb_shutdown(struct bdi_writeback *wb)
462{
463 /* Make sure nobody queues further work */
Khazhismel Kumykovf87904c2022-08-01 08:50:34 -0700464 spin_lock_irq(&wb->work_lock);
Tejun Heo46100072015-05-22 17:13:31 -0400465 if (!test_and_clear_bit(WB_registered, &wb->state)) {
Khazhismel Kumykovf87904c2022-08-01 08:50:34 -0700466 spin_unlock_irq(&wb->work_lock);
Tejun Heo46100072015-05-22 17:13:31 -0400467 return;
468 }
Khazhismel Kumykovf87904c2022-08-01 08:50:34 -0700469 spin_unlock_irq(&wb->work_lock);
Tejun Heo46100072015-05-22 17:13:31 -0400470
Jan Karae8cb72b2017-03-23 01:36:56 +0100471 cgwb_remove_from_bdi_list(wb);
Tejun Heo46100072015-05-22 17:13:31 -0400472 /*
473 * Drain work list and shutdown the delayed_work. !WB_registered
474 * tells wb_workfn() that @wb is dying and its work_list needs to
475 * be drained no matter what.
476 */
477 mod_delayed_work(bdi_wq, &wb->dwork, 0);
478 flush_delayed_work(&wb->dwork);
479 WARN_ON(!list_empty(&wb->work_list));
Jan Kara45a29662021-09-02 14:53:09 -0700480 flush_delayed_work(&wb->bw_dwork);
Tejun Heo46100072015-05-22 17:13:31 -0400481}
482
Tejun Heof0054bb2015-05-22 17:13:30 -0400483static void wb_exit(struct bdi_writeback *wb)
Tejun Heo93f78d82015-05-22 17:13:27 -0400484{
485 int i;
486
487 WARN_ON(delayed_work_pending(&wb->dwork));
488
489 for (i = 0; i < NR_WB_STAT_ITEMS; i++)
490 percpu_counter_destroy(&wb->stat[i]);
Artem Bityutskiy64677162010-07-25 14:29:22 +0300491
Tejun Heoa88a3412015-05-22 17:13:28 -0400492 fprop_local_destroy_percpu(&wb->completions);
493}
Wu Fengguange98be2d2010-08-29 11:22:30 -0600494
Tejun Heo52ebea72015-05-22 17:13:37 -0400495#ifdef CONFIG_CGROUP_WRITEBACK
496
497#include <linux/memcontrol.h>
498
499/*
Roman Gushchinc22d70a2021-06-28 19:36:03 -0700500 * cgwb_lock protects bdi->cgwb_tree, blkcg->cgwb_list, offline_cgwbs and
501 * memcg->cgwb_list. bdi->cgwb_tree is also RCU protected.
Tejun Heo52ebea72015-05-22 17:13:37 -0400502 */
503static DEFINE_SPINLOCK(cgwb_lock);
Tejun Heof1834642018-05-23 10:56:32 -0700504static struct workqueue_struct *cgwb_release_wq;
Tejun Heo52ebea72015-05-22 17:13:37 -0400505
Roman Gushchinc22d70a2021-06-28 19:36:03 -0700506static LIST_HEAD(offline_cgwbs);
507static void cleanup_offline_cgwbs_workfn(struct work_struct *work);
508static DECLARE_WORK(cleanup_offline_cgwbs_work, cleanup_offline_cgwbs_workfn);
509
Tejun Heo52ebea72015-05-22 17:13:37 -0400510static void cgwb_release_workfn(struct work_struct *work)
511{
512 struct bdi_writeback *wb = container_of(work, struct bdi_writeback,
513 release_work);
Christoph Hellwigefee1712021-11-05 13:37:04 -0700514 struct backing_dev_info *bdi = wb->bdi;
Tejun Heo52ebea72015-05-22 17:13:37 -0400515
Jan Kara3ee7e862018-06-18 15:46:58 +0200516 mutex_lock(&wb->bdi->cgwb_release_mutex);
Tejun Heo52ebea72015-05-22 17:13:37 -0400517 wb_shutdown(wb);
518
519 css_put(wb->memcg_css);
520 css_put(wb->blkcg_css);
Jan Kara3ee7e862018-06-18 15:46:58 +0200521 mutex_unlock(&wb->bdi->cgwb_release_mutex);
Tejun Heo52ebea72015-05-22 17:13:37 -0400522
Tejun Heod866dbf2019-07-24 10:37:22 -0700523 /* triggers blkg destruction if no online users left */
Christoph Hellwig397c9f42022-04-20 06:27:14 +0200524 blkcg_unpin_online(wb->blkcg_css);
Dennis Zhou (Facebook)59b57712018-08-31 16:22:43 -0400525
Tejun Heo841710a2015-05-22 18:23:33 -0400526 fprop_local_destroy_percpu(&wb->memcg_completions);
Roman Gushchinc22d70a2021-06-28 19:36:03 -0700527
528 spin_lock_irq(&cgwb_lock);
529 list_del(&wb->offline_node);
530 spin_unlock_irq(&cgwb_lock);
531
Roman Gushchinb43a9e72021-07-23 15:50:29 -0700532 percpu_ref_exit(&wb->refcnt);
Tejun Heo52ebea72015-05-22 17:13:37 -0400533 wb_exit(wb);
Christoph Hellwigefee1712021-11-05 13:37:04 -0700534 bdi_put(bdi);
Roman Gushchinf3b6a6d2021-06-28 19:35:53 -0700535 WARN_ON_ONCE(!list_empty(&wb->b_attached));
Tejun Heo52ebea72015-05-22 17:13:37 -0400536 kfree_rcu(wb, rcu);
Tejun Heo52ebea72015-05-22 17:13:37 -0400537}
538
539static void cgwb_release(struct percpu_ref *refcnt)
540{
541 struct bdi_writeback *wb = container_of(refcnt, struct bdi_writeback,
542 refcnt);
Tejun Heof1834642018-05-23 10:56:32 -0700543 queue_work(cgwb_release_wq, &wb->release_work);
Tejun Heo52ebea72015-05-22 17:13:37 -0400544}
545
546static void cgwb_kill(struct bdi_writeback *wb)
547{
548 lockdep_assert_held(&cgwb_lock);
549
550 WARN_ON(!radix_tree_delete(&wb->bdi->cgwb_tree, wb->memcg_css->id));
551 list_del(&wb->memcg_node);
552 list_del(&wb->blkcg_node);
Roman Gushchinc22d70a2021-06-28 19:36:03 -0700553 list_add(&wb->offline_node, &offline_cgwbs);
Tejun Heo52ebea72015-05-22 17:13:37 -0400554 percpu_ref_kill(&wb->refcnt);
555}
556
Jan Karae8cb72b2017-03-23 01:36:56 +0100557static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb)
558{
559 spin_lock_irq(&cgwb_lock);
560 list_del_rcu(&wb->bdi_node);
561 spin_unlock_irq(&cgwb_lock);
562}
563
Tejun Heo52ebea72015-05-22 17:13:37 -0400564static int cgwb_create(struct backing_dev_info *bdi,
565 struct cgroup_subsys_state *memcg_css, gfp_t gfp)
566{
567 struct mem_cgroup *memcg;
568 struct cgroup_subsys_state *blkcg_css;
Tejun Heo52ebea72015-05-22 17:13:37 -0400569 struct list_head *memcg_cgwb_list, *blkcg_cgwb_list;
570 struct bdi_writeback *wb;
571 unsigned long flags;
572 int ret = 0;
573
574 memcg = mem_cgroup_from_css(memcg_css);
Tejun Heoc165b3e2015-08-18 14:55:29 -0700575 blkcg_css = cgroup_get_e_css(memcg_css->cgroup, &io_cgrp_subsys);
Wang Long9ccc3612018-06-07 17:07:19 -0700576 memcg_cgwb_list = &memcg->cgwb_list;
Christoph Hellwigdec223c2022-04-20 06:27:15 +0200577 blkcg_cgwb_list = blkcg_get_cgwb_list(blkcg_css);
Tejun Heo52ebea72015-05-22 17:13:37 -0400578
579 /* look up again under lock and discard on blkcg mismatch */
580 spin_lock_irqsave(&cgwb_lock, flags);
581 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
582 if (wb && wb->blkcg_css != blkcg_css) {
583 cgwb_kill(wb);
584 wb = NULL;
585 }
586 spin_unlock_irqrestore(&cgwb_lock, flags);
587 if (wb)
588 goto out_put;
589
590 /* need to create a new one */
591 wb = kmalloc(sizeof(*wb), gfp);
Christophe JAILLET0b045bd2017-09-11 21:43:23 +0200592 if (!wb) {
593 ret = -ENOMEM;
594 goto out_put;
595 }
Tejun Heo52ebea72015-05-22 17:13:37 -0400596
Christoph Hellwig8c911f32020-07-01 11:06:21 +0200597 ret = wb_init(wb, bdi, gfp);
Tejun Heo52ebea72015-05-22 17:13:37 -0400598 if (ret)
599 goto err_free;
600
601 ret = percpu_ref_init(&wb->refcnt, cgwb_release, 0, gfp);
602 if (ret)
603 goto err_wb_exit;
604
Tejun Heo841710a2015-05-22 18:23:33 -0400605 ret = fprop_local_init_percpu(&wb->memcg_completions, gfp);
606 if (ret)
607 goto err_ref_exit;
608
Tejun Heo52ebea72015-05-22 17:13:37 -0400609 wb->memcg_css = memcg_css;
610 wb->blkcg_css = blkcg_css;
Roman Gushchinf3b6a6d2021-06-28 19:35:53 -0700611 INIT_LIST_HEAD(&wb->b_attached);
Tejun Heo52ebea72015-05-22 17:13:37 -0400612 INIT_WORK(&wb->release_work, cgwb_release_workfn);
613 set_bit(WB_registered, &wb->state);
Christoph Hellwigefee1712021-11-05 13:37:04 -0700614 bdi_get(bdi);
Tejun Heo52ebea72015-05-22 17:13:37 -0400615
616 /*
617 * The root wb determines the registered state of the whole bdi and
618 * memcg_cgwb_list and blkcg_cgwb_list's next pointers indicate
619 * whether they're still online. Don't link @wb if any is dead.
620 * See wb_memcg_offline() and wb_blkcg_offline().
621 */
622 ret = -ENODEV;
623 spin_lock_irqsave(&cgwb_lock, flags);
624 if (test_bit(WB_registered, &bdi->wb.state) &&
625 blkcg_cgwb_list->next && memcg_cgwb_list->next) {
626 /* we might have raced another instance of this function */
627 ret = radix_tree_insert(&bdi->cgwb_tree, memcg_css->id, wb);
628 if (!ret) {
Tejun Heob8175252015-10-02 14:47:05 -0400629 list_add_tail_rcu(&wb->bdi_node, &bdi->wb_list);
Tejun Heo52ebea72015-05-22 17:13:37 -0400630 list_add(&wb->memcg_node, memcg_cgwb_list);
631 list_add(&wb->blkcg_node, blkcg_cgwb_list);
Christoph Hellwig397c9f42022-04-20 06:27:14 +0200632 blkcg_pin_online(blkcg_css);
Tejun Heo52ebea72015-05-22 17:13:37 -0400633 css_get(memcg_css);
634 css_get(blkcg_css);
635 }
636 }
637 spin_unlock_irqrestore(&cgwb_lock, flags);
638 if (ret) {
639 if (ret == -EEXIST)
640 ret = 0;
Tejun Heoa13f35e2015-07-02 08:44:34 -0600641 goto err_fprop_exit;
Tejun Heo52ebea72015-05-22 17:13:37 -0400642 }
643 goto out_put;
644
Tejun Heo841710a2015-05-22 18:23:33 -0400645err_fprop_exit:
Christoph Hellwigefee1712021-11-05 13:37:04 -0700646 bdi_put(bdi);
Tejun Heo841710a2015-05-22 18:23:33 -0400647 fprop_local_destroy_percpu(&wb->memcg_completions);
Tejun Heo52ebea72015-05-22 17:13:37 -0400648err_ref_exit:
649 percpu_ref_exit(&wb->refcnt);
650err_wb_exit:
651 wb_exit(wb);
652err_free:
653 kfree(wb);
654out_put:
655 css_put(blkcg_css);
656 return ret;
657}
658
659/**
Tejun Heoed288dc2019-08-26 09:06:54 -0700660 * wb_get_lookup - get wb for a given memcg
Tejun Heo52ebea72015-05-22 17:13:37 -0400661 * @bdi: target bdi
662 * @memcg_css: cgroup_subsys_state of the target memcg (must have positive ref)
Tejun Heo52ebea72015-05-22 17:13:37 -0400663 *
Tejun Heoed288dc2019-08-26 09:06:54 -0700664 * Try to get the wb for @memcg_css on @bdi. The returned wb has its
665 * refcount incremented.
Tejun Heo52ebea72015-05-22 17:13:37 -0400666 *
667 * This function uses css_get() on @memcg_css and thus expects its refcnt
668 * to be positive on invocation. IOW, rcu_read_lock() protection on
669 * @memcg_css isn't enough. try_get it before calling this function.
670 *
671 * A wb is keyed by its associated memcg. As blkcg implicitly enables
672 * memcg on the default hierarchy, memcg association is guaranteed to be
673 * more specific (equal or descendant to the associated blkcg) and thus can
674 * identify both the memcg and blkcg associations.
675 *
676 * Because the blkcg associated with a memcg may change as blkcg is enabled
677 * and disabled closer to root in the hierarchy, each wb keeps track of
678 * both the memcg and blkcg associated with it and verifies the blkcg on
679 * each lookup. On mismatch, the existing wb is discarded and a new one is
680 * created.
681 */
Tejun Heoed288dc2019-08-26 09:06:54 -0700682struct bdi_writeback *wb_get_lookup(struct backing_dev_info *bdi,
683 struct cgroup_subsys_state *memcg_css)
684{
685 struct bdi_writeback *wb;
686
687 if (!memcg_css->parent)
688 return &bdi->wb;
689
690 rcu_read_lock();
691 wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);
692 if (wb) {
693 struct cgroup_subsys_state *blkcg_css;
694
695 /* see whether the blkcg association has changed */
696 blkcg_css = cgroup_get_e_css(memcg_css->cgroup, &io_cgrp_subsys);
697 if (unlikely(wb->blkcg_css != blkcg_css || !wb_tryget(wb)))
698 wb = NULL;
699 css_put(blkcg_css);
700 }
701 rcu_read_unlock();
702
703 return wb;
704}
705
706/**
707 * wb_get_create - get wb for a given memcg, create if necessary
708 * @bdi: target bdi
709 * @memcg_css: cgroup_subsys_state of the target memcg (must have positive ref)
710 * @gfp: allocation mask to use
711 *
712 * Try to get the wb for @memcg_css on @bdi. If it doesn't exist, try to
713 * create one. See wb_get_lookup() for more details.
714 */
Tejun Heo52ebea72015-05-22 17:13:37 -0400715struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
716 struct cgroup_subsys_state *memcg_css,
717 gfp_t gfp)
718{
719 struct bdi_writeback *wb;
720
Daniel Vetterc1ca59a2021-02-25 17:18:45 -0800721 might_alloc(gfp);
Tejun Heo52ebea72015-05-22 17:13:37 -0400722
723 if (!memcg_css->parent)
724 return &bdi->wb;
725
726 do {
Tejun Heoed288dc2019-08-26 09:06:54 -0700727 wb = wb_get_lookup(bdi, memcg_css);
Tejun Heo52ebea72015-05-22 17:13:37 -0400728 } while (!wb && !cgwb_create(bdi, memcg_css, gfp));
729
730 return wb;
731}
732
Tejun Heoa13f35e2015-07-02 08:44:34 -0600733static int cgwb_bdi_init(struct backing_dev_info *bdi)
Tejun Heo52ebea72015-05-22 17:13:37 -0400734{
Tejun Heoa13f35e2015-07-02 08:44:34 -0600735 int ret;
736
Tejun Heo52ebea72015-05-22 17:13:37 -0400737 INIT_RADIX_TREE(&bdi->cgwb_tree, GFP_ATOMIC);
Jan Kara3ee7e862018-06-18 15:46:58 +0200738 mutex_init(&bdi->cgwb_release_mutex);
Tejun Heo7fc58542017-12-12 08:38:30 -0800739 init_rwsem(&bdi->wb_switch_rwsem);
Tejun Heoa13f35e2015-07-02 08:44:34 -0600740
Christoph Hellwig8c911f32020-07-01 11:06:21 +0200741 ret = wb_init(&bdi->wb, bdi, GFP_KERNEL);
Tejun Heoa13f35e2015-07-02 08:44:34 -0600742 if (!ret) {
Johannes Weiner7d828602016-01-14 15:20:56 -0800743 bdi->wb.memcg_css = &root_mem_cgroup->css;
Tejun Heoa13f35e2015-07-02 08:44:34 -0600744 bdi->wb.blkcg_css = blkcg_root_css;
745 }
746 return ret;
Tejun Heo52ebea72015-05-22 17:13:37 -0400747}
748
Jan Karab1c51af2017-03-23 01:36:59 +0100749static void cgwb_bdi_unregister(struct backing_dev_info *bdi)
Tejun Heo52ebea72015-05-22 17:13:37 -0400750{
751 struct radix_tree_iter iter;
752 void **slot;
Jan Kara5318ce72017-03-23 01:36:57 +0100753 struct bdi_writeback *wb;
Tejun Heo52ebea72015-05-22 17:13:37 -0400754
755 WARN_ON(test_bit(WB_registered, &bdi->wb.state));
756
757 spin_lock_irq(&cgwb_lock);
758 radix_tree_for_each_slot(slot, &bdi->cgwb_tree, &iter, 0)
759 cgwb_kill(*slot);
Jan Kara3ee7e862018-06-18 15:46:58 +0200760 spin_unlock_irq(&cgwb_lock);
Jan Kara5318ce72017-03-23 01:36:57 +0100761
Jan Kara3ee7e862018-06-18 15:46:58 +0200762 mutex_lock(&bdi->cgwb_release_mutex);
763 spin_lock_irq(&cgwb_lock);
Jan Kara5318ce72017-03-23 01:36:57 +0100764 while (!list_empty(&bdi->wb_list)) {
765 wb = list_first_entry(&bdi->wb_list, struct bdi_writeback,
766 bdi_node);
767 spin_unlock_irq(&cgwb_lock);
768 wb_shutdown(wb);
769 spin_lock_irq(&cgwb_lock);
770 }
Tejun Heo52ebea72015-05-22 17:13:37 -0400771 spin_unlock_irq(&cgwb_lock);
Jan Kara3ee7e862018-06-18 15:46:58 +0200772 mutex_unlock(&bdi->cgwb_release_mutex);
Tejun Heo52ebea72015-05-22 17:13:37 -0400773}
774
Roman Gushchinc22d70a2021-06-28 19:36:03 -0700775/*
776 * cleanup_offline_cgwbs_workfn - try to release dying cgwbs
777 *
778 * Try to release dying cgwbs by switching attached inodes to the nearest
779 * living ancestor's writeback. Processed wbs are placed at the end
780 * of the list to guarantee the forward progress.
781 */
782static void cleanup_offline_cgwbs_workfn(struct work_struct *work)
783{
784 struct bdi_writeback *wb;
785 LIST_HEAD(processed);
786
787 spin_lock_irq(&cgwb_lock);
788
789 while (!list_empty(&offline_cgwbs)) {
790 wb = list_first_entry(&offline_cgwbs, struct bdi_writeback,
791 offline_node);
792 list_move(&wb->offline_node, &processed);
793
794 /*
795 * If wb is dirty, cleaning up the writeback by switching
796 * attached inodes will result in an effective removal of any
797 * bandwidth restrictions, which isn't the goal. Instead,
798 * it can be postponed until the next time, when all io
799 * will be likely completed. If in the meantime some inodes
800 * will get re-dirtied, they should be eventually switched to
801 * a new cgwb.
802 */
803 if (wb_has_dirty_io(wb))
804 continue;
805
806 if (!wb_tryget(wb))
807 continue;
808
809 spin_unlock_irq(&cgwb_lock);
810 while (cleanup_offline_cgwb(wb))
811 cond_resched();
812 spin_lock_irq(&cgwb_lock);
813
814 wb_put(wb);
815 }
816
817 if (!list_empty(&processed))
818 list_splice_tail(&processed, &offline_cgwbs);
819
820 spin_unlock_irq(&cgwb_lock);
821}
822
Tejun Heo52ebea72015-05-22 17:13:37 -0400823/**
824 * wb_memcg_offline - kill all wb's associated with a memcg being offlined
825 * @memcg: memcg being offlined
826 *
827 * Also prevents creation of any new wb's associated with @memcg.
828 */
829void wb_memcg_offline(struct mem_cgroup *memcg)
830{
Wang Long9ccc3612018-06-07 17:07:19 -0700831 struct list_head *memcg_cgwb_list = &memcg->cgwb_list;
Tejun Heo52ebea72015-05-22 17:13:37 -0400832 struct bdi_writeback *wb, *next;
833
834 spin_lock_irq(&cgwb_lock);
835 list_for_each_entry_safe(wb, next, memcg_cgwb_list, memcg_node)
836 cgwb_kill(wb);
837 memcg_cgwb_list->next = NULL; /* prevent new wb's */
838 spin_unlock_irq(&cgwb_lock);
Roman Gushchinc22d70a2021-06-28 19:36:03 -0700839
840 queue_work(system_unbound_wq, &cleanup_offline_cgwbs_work);
Tejun Heo52ebea72015-05-22 17:13:37 -0400841}
842
843/**
844 * wb_blkcg_offline - kill all wb's associated with a blkcg being offlined
Christoph Hellwigdec223c2022-04-20 06:27:15 +0200845 * @css: blkcg being offlined
Tejun Heo52ebea72015-05-22 17:13:37 -0400846 *
847 * Also prevents creation of any new wb's associated with @blkcg.
848 */
Christoph Hellwigdec223c2022-04-20 06:27:15 +0200849void wb_blkcg_offline(struct cgroup_subsys_state *css)
Tejun Heo52ebea72015-05-22 17:13:37 -0400850{
Tejun Heo52ebea72015-05-22 17:13:37 -0400851 struct bdi_writeback *wb, *next;
Christoph Hellwigdec223c2022-04-20 06:27:15 +0200852 struct list_head *list = blkcg_get_cgwb_list(css);
Tejun Heo52ebea72015-05-22 17:13:37 -0400853
854 spin_lock_irq(&cgwb_lock);
Christoph Hellwigdec223c2022-04-20 06:27:15 +0200855 list_for_each_entry_safe(wb, next, list, blkcg_node)
Tejun Heo52ebea72015-05-22 17:13:37 -0400856 cgwb_kill(wb);
Christoph Hellwigdec223c2022-04-20 06:27:15 +0200857 list->next = NULL; /* prevent new wb's */
Tejun Heo52ebea72015-05-22 17:13:37 -0400858 spin_unlock_irq(&cgwb_lock);
859}
860
Jan Karae8cb72b2017-03-23 01:36:56 +0100861static void cgwb_bdi_register(struct backing_dev_info *bdi)
862{
863 spin_lock_irq(&cgwb_lock);
864 list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
865 spin_unlock_irq(&cgwb_lock);
866}
867
Tejun Heof1834642018-05-23 10:56:32 -0700868static int __init cgwb_init(void)
869{
870 /*
871 * There can be many concurrent release work items overwhelming
872 * system_wq. Put them in a separate wq and limit concurrency.
873 * There's no point in executing many of these in parallel.
874 */
875 cgwb_release_wq = alloc_workqueue("cgwb_release", 0, 1);
876 if (!cgwb_release_wq)
877 return -ENOMEM;
878
879 return 0;
880}
881subsys_initcall(cgwb_init);
882
Tejun Heo52ebea72015-05-22 17:13:37 -0400883#else /* CONFIG_CGROUP_WRITEBACK */
884
Tejun Heoa13f35e2015-07-02 08:44:34 -0600885static int cgwb_bdi_init(struct backing_dev_info *bdi)
886{
Christoph Hellwig8c911f32020-07-01 11:06:21 +0200887 return wb_init(&bdi->wb, bdi, GFP_KERNEL);
Tejun Heoa13f35e2015-07-02 08:44:34 -0600888}
889
Jan Karab1c51af2017-03-23 01:36:59 +0100890static void cgwb_bdi_unregister(struct backing_dev_info *bdi) { }
Jan Karadf23de52017-03-08 17:48:32 +0100891
Jan Karae8cb72b2017-03-23 01:36:56 +0100892static void cgwb_bdi_register(struct backing_dev_info *bdi)
893{
894 list_add_tail_rcu(&bdi->wb.bdi_node, &bdi->wb_list);
895}
896
897static void cgwb_remove_from_bdi_list(struct bdi_writeback *wb)
898{
899 list_del_rcu(&wb->bdi_node);
900}
901
Tejun Heo52ebea72015-05-22 17:13:37 -0400902#endif /* CONFIG_CGROUP_WRITEBACK */
903
Jan Kara4bca7e82022-06-15 15:22:29 +0200904int bdi_init(struct backing_dev_info *bdi)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700905{
Peter Zijlstracf0ca9f2008-04-30 00:54:32 -0700906 bdi->dev = NULL;
907
Jan Karad03f6cd2017-02-02 15:56:51 +0100908 kref_init(&bdi->refcnt);
Peter Zijlstra189d3c42008-04-30 00:54:35 -0700909 bdi->min_ratio = 0;
Stefan Roeschae822912022-11-18 16:51:59 -0800910 bdi->max_ratio = 100 * BDI_RATIO_SCALE;
Jan Karaeb608e32012-05-24 18:59:11 +0200911 bdi->max_prop_frac = FPROP_FRAC_BASE;
Jens Axboe66f3b8e2009-09-02 09:19:46 +0200912 INIT_LIST_HEAD(&bdi->bdi_list);
Tejun Heob8175252015-10-02 14:47:05 -0400913 INIT_LIST_HEAD(&bdi->wb_list);
Tejun Heocc395d72015-05-22 17:13:58 -0400914 init_waitqueue_head(&bdi->wb_waitq);
Jens Axboe03ba3782009-09-09 09:08:54 +0200915
ye xingchen3083da72022-08-26 07:19:06 +0000916 return cgwb_bdi_init(bdi);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700917}
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700918
Christoph Hellwigaef33c22020-05-04 14:48:00 +0200919struct backing_dev_info *bdi_alloc(int node_id)
Jan Karad03f6cd2017-02-02 15:56:51 +0100920{
921 struct backing_dev_info *bdi;
922
Christoph Hellwigaef33c22020-05-04 14:48:00 +0200923 bdi = kzalloc_node(sizeof(*bdi), GFP_KERNEL, node_id);
Jan Karad03f6cd2017-02-02 15:56:51 +0100924 if (!bdi)
925 return NULL;
926
927 if (bdi_init(bdi)) {
928 kfree(bdi);
929 return NULL;
930 }
Christoph Hellwigf56753a2020-09-24 08:51:40 +0200931 bdi->capabilities = BDI_CAP_WRITEBACK | BDI_CAP_WRITEBACK_ACCT;
Christoph Hellwig55b25982020-09-24 08:51:32 +0200932 bdi->ra_pages = VM_READAHEAD_PAGES;
933 bdi->io_pages = VM_READAHEAD_PAGES;
Christoph Hellwig5ed964f2021-08-09 16:17:40 +0200934 timer_setup(&bdi->laptop_mode_wb_timer, laptop_mode_timer_fn, 0);
Jan Karad03f6cd2017-02-02 15:56:51 +0100935 return bdi;
936}
Christoph Hellwigaef33c22020-05-04 14:48:00 +0200937EXPORT_SYMBOL(bdi_alloc);
Jan Karad03f6cd2017-02-02 15:56:51 +0100938
Tejun Heo34f8fe52019-08-26 09:06:53 -0700939static struct rb_node **bdi_lookup_rb_node(u64 id, struct rb_node **parentp)
940{
941 struct rb_node **p = &bdi_tree.rb_node;
942 struct rb_node *parent = NULL;
943 struct backing_dev_info *bdi;
944
945 lockdep_assert_held(&bdi_lock);
946
947 while (*p) {
948 parent = *p;
949 bdi = rb_entry(parent, struct backing_dev_info, rb_node);
950
951 if (bdi->id > id)
952 p = &(*p)->rb_left;
953 else if (bdi->id < id)
954 p = &(*p)->rb_right;
955 else
956 break;
957 }
958
959 if (parentp)
960 *parentp = parent;
961 return p;
962}
963
964/**
965 * bdi_get_by_id - lookup and get bdi from its id
966 * @id: bdi id to lookup
967 *
968 * Find bdi matching @id and get it. Returns NULL if the matching bdi
969 * doesn't exist or is already unregistered.
970 */
971struct backing_dev_info *bdi_get_by_id(u64 id)
972{
973 struct backing_dev_info *bdi = NULL;
974 struct rb_node **p;
975
976 spin_lock_bh(&bdi_lock);
977 p = bdi_lookup_rb_node(id, NULL);
978 if (*p) {
979 bdi = rb_entry(*p, struct backing_dev_info, rb_node);
980 bdi_get(bdi);
981 }
982 spin_unlock_bh(&bdi_lock);
983
984 return bdi;
985}
986
Jan Kara7c4cc302017-04-12 12:24:49 +0200987int bdi_register_va(struct backing_dev_info *bdi, const char *fmt, va_list args)
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700988{
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700989 struct device *dev;
Tejun Heo34f8fe52019-08-26 09:06:53 -0700990 struct rb_node *parent, **p;
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700991
992 if (bdi->dev) /* The driver needs to use separate queues per device */
993 return 0;
994
Christoph Hellwig6bd87ee2020-05-04 14:47:56 +0200995 vsnprintf(bdi->dev_name, sizeof(bdi->dev_name), fmt, args);
996 dev = device_create(bdi_class, NULL, MKDEV(0, 0), bdi, bdi->dev_name);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -0700997 if (IS_ERR(dev))
998 return PTR_ERR(dev);
999
Jan Karae8cb72b2017-03-23 01:36:56 +01001000 cgwb_bdi_register(bdi);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -07001001 bdi->dev = dev;
1002
Jens Axboe6d0e4822017-12-21 10:01:30 -07001003 bdi_debug_register(bdi, dev_name(dev));
Tejun Heo46100072015-05-22 17:13:31 -04001004 set_bit(WB_registered, &bdi->wb.state);
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -07001005
1006 spin_lock_bh(&bdi_lock);
Tejun Heo34f8fe52019-08-26 09:06:53 -07001007
1008 bdi->id = ++bdi_id_cursor;
1009
1010 p = bdi_lookup_rb_node(bdi->id, &parent);
1011 rb_link_node(&bdi->rb_node, parent, p);
1012 rb_insert_color(&bdi->rb_node, &bdi_tree);
1013
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -07001014 list_add_tail_rcu(&bdi->bdi_list, &bdi_list);
Tejun Heo34f8fe52019-08-26 09:06:53 -07001015
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -07001016 spin_unlock_bh(&bdi_lock);
1017
1018 trace_writeback_bdi_register(bdi);
1019 return 0;
1020}
Jan Karabaf7a612017-04-12 12:24:25 +02001021
Jan Kara7c4cc302017-04-12 12:24:49 +02001022int bdi_register(struct backing_dev_info *bdi, const char *fmt, ...)
Jan Karabaf7a612017-04-12 12:24:25 +02001023{
1024 va_list args;
1025 int ret;
1026
1027 va_start(args, fmt);
Jan Kara7c4cc302017-04-12 12:24:49 +02001028 ret = bdi_register_va(bdi, fmt, args);
Jan Karabaf7a612017-04-12 12:24:25 +02001029 va_end(args);
1030 return ret;
1031}
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -07001032EXPORT_SYMBOL(bdi_register);
1033
Christoph Hellwig3c5d2022020-05-04 14:47:59 +02001034void bdi_set_owner(struct backing_dev_info *bdi, struct device *owner)
Dan Williamsdf08c322016-07-31 11:15:13 -07001035{
Christoph Hellwig3c5d2022020-05-04 14:47:59 +02001036 WARN_ON_ONCE(bdi->owner);
Dan Williamsdf08c322016-07-31 11:15:13 -07001037 bdi->owner = owner;
1038 get_device(owner);
Dan Williamsdf08c322016-07-31 11:15:13 -07001039}
Dan Williamsdf08c322016-07-31 11:15:13 -07001040
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -07001041/*
Tejun Heo46100072015-05-22 17:13:31 -04001042 * Remove bdi from bdi_list, and ensure that it is no longer visible
Peter Zijlstrab2e8fb62007-10-16 23:25:47 -07001043 */
Tejun Heo46100072015-05-22 17:13:31 -04001044static void bdi_remove_from_list(struct backing_dev_info *bdi)
Peter Zijlstra04fbfdc2007-10-16 23:25:50 -07001045{
Tejun Heo46100072015-05-22 17:13:31 -04001046 spin_lock_bh(&bdi_lock);
Tejun Heo34f8fe52019-08-26 09:06:53 -07001047 rb_erase(&bdi->rb_node, &bdi_tree);
Tejun Heo46100072015-05-22 17:13:31 -04001048 list_del_rcu(&bdi->bdi_list);
1049 spin_unlock_bh(&bdi_lock);
Andrew Morton3fcfab12006-10-19 23:28:16 -07001050
Tejun Heo46100072015-05-22 17:13:31 -04001051 synchronize_rcu_expedited();
Andrew Morton3fcfab12006-10-19 23:28:16 -07001052}
1053
Tejun Heob02176f2015-09-08 12:20:22 -04001054void bdi_unregister(struct backing_dev_info *bdi)
Andrew Morton3fcfab12006-10-19 23:28:16 -07001055{
Christoph Hellwig5ed964f2021-08-09 16:17:40 +02001056 del_timer_sync(&bdi->laptop_mode_wb_timer);
1057
Tejun Heof0054bb2015-05-22 17:13:30 -04001058 /* make sure nobody finds us on the bdi_list anymore */
1059 bdi_remove_from_list(bdi);
1060 wb_shutdown(&bdi->wb);
Jan Karab1c51af2017-03-23 01:36:59 +01001061 cgwb_bdi_unregister(bdi);
Rabin Vincent7a401a92011-11-11 13:29:04 +01001062
Manjong Lee3c376df2021-12-10 14:47:11 -08001063 /*
1064 * If this BDI's min ratio has been set, use bdi_set_min_ratio() to
1065 * update the global bdi_min_ratio.
1066 */
1067 if (bdi->min_ratio)
1068 bdi_set_min_ratio(bdi, 0);
1069
Christoph Hellwigc4db59d2015-01-20 14:05:00 -07001070 if (bdi->dev) {
1071 bdi_debug_unregister(bdi);
1072 device_unregister(bdi->dev);
1073 bdi->dev = NULL;
1074 }
Dan Williamsdf08c322016-07-31 11:15:13 -07001075
1076 if (bdi->owner) {
1077 put_device(bdi->owner);
1078 bdi->owner = NULL;
1079 }
Tejun Heob02176f2015-09-08 12:20:22 -04001080}
Christoph Hellwigc6fd3ac2021-11-05 13:36:52 -07001081EXPORT_SYMBOL(bdi_unregister);
Christoph Hellwigc4db59d2015-01-20 14:05:00 -07001082
Jan Karad03f6cd2017-02-02 15:56:51 +01001083static void release_bdi(struct kref *ref)
1084{
1085 struct backing_dev_info *bdi =
1086 container_of(ref, struct backing_dev_info, refcnt);
1087
Christoph Hellwig702f2d12021-11-05 13:37:01 -07001088 WARN_ON_ONCE(test_bit(WB_registered, &bdi->wb.state));
Jan Kara2e82b842017-04-12 12:24:48 +02001089 WARN_ON_ONCE(bdi->dev);
1090 wb_exit(&bdi->wb);
Jan Karad03f6cd2017-02-02 15:56:51 +01001091 kfree(bdi);
1092}
1093
1094void bdi_put(struct backing_dev_info *bdi)
1095{
1096 kref_put(&bdi->refcnt, release_bdi);
1097}
Jan Kara62bf42a2017-04-12 12:24:27 +02001098EXPORT_SYMBOL(bdi_put);
Jan Karad03f6cd2017-02-02 15:56:51 +01001099
Christoph Hellwigccdf7742021-09-20 14:33:14 +02001100struct backing_dev_info *inode_to_bdi(struct inode *inode)
1101{
1102 struct super_block *sb;
1103
1104 if (!inode)
1105 return &noop_backing_dev_info;
1106
1107 sb = inode->i_sb;
1108#ifdef CONFIG_BLOCK
1109 if (sb_is_blkdev_sb(sb))
1110 return I_BDEV(inode)->bd_disk->bdi;
1111#endif
1112 return sb->s_bdi;
1113}
1114EXPORT_SYMBOL(inode_to_bdi);
1115
Christoph Hellwigeb7ae5e2020-05-04 14:47:54 +02001116const char *bdi_dev_name(struct backing_dev_info *bdi)
1117{
1118 if (!bdi || !bdi->dev)
1119 return bdi_unknown_name;
Christoph Hellwig6bd87ee2020-05-04 14:47:56 +02001120 return bdi->dev_name;
Christoph Hellwigeb7ae5e2020-05-04 14:47:54 +02001121}
1122EXPORT_SYMBOL_GPL(bdi_dev_name);