backing-dev.h 14.7 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11
/*
 * include/linux/backing-dev.h
 *
 * low-level device information and state which is propagated up through
 * to high-level code.
 */

#ifndef _LINUX_BACKING_DEV_H
#define _LINUX_BACKING_DEV_H

12
#include <linux/kernel.h>
13
#include <linux/fs.h>
14
#include <linux/sched.h>
T
Tejun Heo 已提交
15
#include <linux/blkdev.h>
16
#include <linux/writeback.h>
17
#include <linux/blk-cgroup.h>
18
#include <linux/backing-dev-defs.h>
19
#include <linux/slab.h>
20

21 22 23 24 25 26 27
static inline struct backing_dev_info *bdi_get(struct backing_dev_info *bdi)
{
	kref_get(&bdi->refcnt);
	return bdi;
}

void bdi_put(struct backing_dev_info *bdi);
28

29 30
__printf(2, 3)
int bdi_register(struct backing_dev_info *bdi, const char *fmt, ...);
31
__printf(2, 0)
32 33
int bdi_register_va(struct backing_dev_info *bdi, const char *fmt,
		    va_list args);
34
int bdi_register_owner(struct backing_dev_info *bdi, struct device *owner);
35 36
void bdi_unregister(struct backing_dev_info *bdi);

37
struct backing_dev_info *bdi_alloc_node(gfp_t gfp_mask, int node_id);
38 39 40 41
static inline struct backing_dev_info *bdi_alloc(gfp_t gfp_mask)
{
	return bdi_alloc_node(gfp_mask, NUMA_NO_NODE);
}
42

43
void wb_start_background_writeback(struct bdi_writeback *wb);
44 45
void wb_workfn(struct work_struct *work);
void wb_wakeup_delayed(struct bdi_writeback *wb);
46

47
extern spinlock_t bdi_lock;
48 49
extern struct list_head bdi_list;

50 51
extern struct workqueue_struct *bdi_wq;

52
static inline bool wb_has_dirty_io(struct bdi_writeback *wb)
53
{
54
	return test_bit(WB_has_dirty_io, &wb->state);
55 56
}

57 58 59 60 61 62 63
static inline bool bdi_has_dirty_io(struct backing_dev_info *bdi)
{
	/*
	 * @bdi->tot_write_bandwidth is guaranteed to be > 0 if there are
	 * any dirty wbs.  See wb_update_write_bandwidth().
	 */
	return atomic_long_read(&bdi->tot_write_bandwidth);
64 65
}

66 67
static inline void __add_wb_stat(struct bdi_writeback *wb,
				 enum wb_stat_item item, s64 amount)
68
{
69
	percpu_counter_add_batch(&wb->stat[item], amount, WB_STAT_BATCH);
70 71
}

72
static inline void inc_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
73
{
74
	__add_wb_stat(wb, item, 1);
75 76
}

77
static inline void dec_wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
78
{
79
	__add_wb_stat(wb, item, -1);
80 81
}

82
static inline s64 wb_stat(struct bdi_writeback *wb, enum wb_stat_item item)
83
{
84
	return percpu_counter_read_positive(&wb->stat[item]);
85 86
}

87
static inline s64 wb_stat_sum(struct bdi_writeback *wb, enum wb_stat_item item)
P
Peter Zijlstra 已提交
88
{
89
	return percpu_counter_sum_positive(&wb->stat[item]);
P
Peter Zijlstra 已提交
90 91
}

92
extern void wb_writeout_inc(struct bdi_writeback *wb);
93

94 95 96
/*
 * maximal error of a stat counter.
 */
97
static inline unsigned long wb_stat_error(void)
P
Peter Zijlstra 已提交
98
{
99
#ifdef CONFIG_SMP
100
	return nr_cpu_ids * WB_STAT_BATCH;
101 102 103
#else
	return 1;
#endif
P
Peter Zijlstra 已提交
104
}
L
Linus Torvalds 已提交
105

106
int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio);
107
int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ratio);
108

L
Linus Torvalds 已提交
109 110
/*
 * Flags in backing_dev_info::capability
111 112 113 114 115 116 117 118 119 120 121 122
 *
 * The first three flags control whether dirty pages will contribute to the
 * VM's accounting and whether writepages() should be called for dirty pages
 * (something that would not, for example, be appropriate for ramfs)
 *
 * WARNING: these flags are closely related and should not normally be
 * used separately.  The BDI_CAP_NO_ACCT_AND_WRITEBACK combines these
 * three flags into a single convenience macro.
 *
 * BDI_CAP_NO_ACCT_DIRTY:  Dirty pages shouldn't contribute to accounting
 * BDI_CAP_NO_WRITEBACK:   Don't write pages back
 * BDI_CAP_NO_ACCT_WB:     Don't automatically account writeback pages
123
 * BDI_CAP_STRICTLIMIT:    Keep number of dirty pages below bdi threshold.
124 125
 *
 * BDI_CAP_CGROUP_WRITEBACK: Supports cgroup-aware writeback.
126 127
 * BDI_CAP_SYNCHRONOUS_IO: Device is so fast that asynchronous IO would be
 *			   inefficient.
L
Linus Torvalds 已提交
128
 */
129 130
#define BDI_CAP_NO_ACCT_DIRTY	0x00000001
#define BDI_CAP_NO_WRITEBACK	0x00000002
131 132 133
#define BDI_CAP_NO_ACCT_WB	0x00000004
#define BDI_CAP_STABLE_WRITES	0x00000008
#define BDI_CAP_STRICTLIMIT	0x00000010
134
#define BDI_CAP_CGROUP_WRITEBACK 0x00000020
135
#define BDI_CAP_SYNCHRONOUS_IO	0x00000040
L
Linus Torvalds 已提交
136

137 138 139
#define BDI_CAP_NO_ACCT_AND_WRITEBACK \
	(BDI_CAP_NO_WRITEBACK | BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_ACCT_WB)

J
Jörn Engel 已提交
140
extern struct backing_dev_info noop_backing_dev_info;
L
Linus Torvalds 已提交
141

142 143 144 145 146 147 148 149
/**
 * writeback_in_progress - determine whether there is writeback in progress
 * @wb: bdi_writeback of interest
 *
 * Determine whether there is writeback waiting to be handled against a
 * bdi_writeback.
 */
static inline bool writeback_in_progress(struct bdi_writeback *wb)
L
Linus Torvalds 已提交
150
{
151
	return test_bit(WB_writeback_running, &wb->state);
L
Linus Torvalds 已提交
152 153
}

T
Tejun Heo 已提交
154
static inline struct backing_dev_info *inode_to_bdi(struct inode *inode)
L
Linus Torvalds 已提交
155
{
T
Tejun Heo 已提交
156
	struct super_block *sb;
L
Linus Torvalds 已提交
157

T
Tejun Heo 已提交
158 159 160 161 162 163
	if (!inode)
		return &noop_backing_dev_info;

	sb = inode->i_sb;
#ifdef CONFIG_BLOCK
	if (sb_is_blkdev_sb(sb))
164
		return I_BDEV(inode)->bd_bdi;
T
Tejun Heo 已提交
165 166
#endif
	return sb->s_bdi;
L
Linus Torvalds 已提交
167 168
}

169
static inline int wb_congested(struct bdi_writeback *wb, int cong_bits)
L
Linus Torvalds 已提交
170
{
171
	struct backing_dev_info *bdi = wb->bdi;
L
Linus Torvalds 已提交
172

173 174 175
	if (bdi->congested_fn)
		return bdi->congested_fn(bdi->congested_data, cong_bits);
	return wb->congested->state & cong_bits;
L
Linus Torvalds 已提交
176
}
177

178
long congestion_wait(int sync, long timeout);
179
long wait_iff_congested(int sync, long timeout);
L
Linus Torvalds 已提交
180

181 182 183 184 185
static inline bool bdi_cap_synchronous_io(struct backing_dev_info *bdi)
{
	return bdi->capabilities & BDI_CAP_SYNCHRONOUS_IO;
}

186 187 188 189 190
static inline bool bdi_cap_stable_pages_required(struct backing_dev_info *bdi)
{
	return bdi->capabilities & BDI_CAP_STABLE_WRITES;
}

191 192 193 194 195 196 197 198 199
static inline bool bdi_cap_writeback_dirty(struct backing_dev_info *bdi)
{
	return !(bdi->capabilities & BDI_CAP_NO_WRITEBACK);
}

static inline bool bdi_cap_account_dirty(struct backing_dev_info *bdi)
{
	return !(bdi->capabilities & BDI_CAP_NO_ACCT_DIRTY);
}
L
Linus Torvalds 已提交
200

201 202 203 204 205 206
static inline bool bdi_cap_account_writeback(struct backing_dev_info *bdi)
{
	/* Paranoia: BDI_CAP_NO_WRITEBACK implies BDI_CAP_NO_ACCT_WB */
	return !(bdi->capabilities & (BDI_CAP_NO_ACCT_WB |
				      BDI_CAP_NO_WRITEBACK));
}
L
Linus Torvalds 已提交
207

208 209
static inline bool mapping_cap_writeback_dirty(struct address_space *mapping)
{
210
	return bdi_cap_writeback_dirty(inode_to_bdi(mapping->host));
211
}
L
Linus Torvalds 已提交
212

213 214
static inline bool mapping_cap_account_dirty(struct address_space *mapping)
{
215
	return bdi_cap_account_dirty(inode_to_bdi(mapping->host));
216
}
L
Linus Torvalds 已提交
217

218 219 220 221 222 223
static inline int bdi_sched_wait(void *word)
{
	schedule();
	return 0;
}

224 225
#ifdef CONFIG_CGROUP_WRITEBACK

226 227 228 229 230 231 232 233
struct bdi_writeback_congested *
wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp);
void wb_congested_put(struct bdi_writeback_congested *congested);
struct bdi_writeback *wb_get_create(struct backing_dev_info *bdi,
				    struct cgroup_subsys_state *memcg_css,
				    gfp_t gfp);
void wb_memcg_offline(struct mem_cgroup *memcg);
void wb_blkcg_offline(struct blkcg *blkcg);
234
int inode_congested(struct inode *inode, int cong_bits);
235

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
extern bool cgwb_v1;

static inline bool memcg_blkcg_on_dfl(void)
{
	return cgroup_subsys_on_dfl(memory_cgrp_subsys) &&
	       cgroup_subsys_on_dfl(io_cgrp_subsys);
}

static inline bool cgroup_writeback_support_v1(void)
{
	return cgwb_v1 &&
	       !cgroup_subsys_on_dfl(memory_cgrp_subsys) &&
	       !cgroup_subsys_on_dfl(io_cgrp_subsys);
}

251 252 253 254 255
/**
 * inode_cgwb_enabled - test whether cgroup writeback is enabled on an inode
 * @inode: inode of interest
 *
 * cgroup writeback requires support from both the bdi and filesystem.
256 257 258 259 260
 * Also, both memcg and iocg have to be on the default hierarchy.  Test
 * whether all conditions are met.
 *
 * Note that the test result may change dynamically on the same inode
 * depending on how memcg and iocg are configured.
261 262 263 264 265
 */
static inline bool inode_cgwb_enabled(struct inode *inode)
{
	struct backing_dev_info *bdi = inode_to_bdi(inode);

266 267 268
	return (memcg_blkcg_on_dfl() ||
		cgroup_writeback_support_v1()) &&
		bdi_cap_account_dirty(bdi) &&
269
		(bdi->capabilities & BDI_CAP_CGROUP_WRITEBACK) &&
270
		(inode->i_sb->s_iflags & SB_I_CGROUPWB);
271 272
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
/**
 * wb_find_current - find wb for %current on a bdi
 * @bdi: bdi of interest
 *
 * Find the wb of @bdi which matches both the memcg and blkcg of %current.
 * Must be called under rcu_read_lock() which protects the returend wb.
 * NULL if not found.
 */
static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
{
	struct cgroup_subsys_state *memcg_css;
	struct bdi_writeback *wb;

	memcg_css = task_css(current, memory_cgrp_id);
	if (!memcg_css->parent)
		return &bdi->wb;

	wb = radix_tree_lookup(&bdi->cgwb_tree, memcg_css->id);

	/*
	 * %current's blkcg equals the effective blkcg of its memcg.  No
	 * need to use the relatively expensive cgroup_get_e_css().
	 */
296
	if (likely(wb && wb->blkcg_css == task_css(current, io_cgrp_id)))
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		return wb;
	return NULL;
}

/**
 * wb_get_create_current - get or create wb for %current on a bdi
 * @bdi: bdi of interest
 * @gfp: allocation mask
 *
 * Equivalent to wb_get_create() on %current's memcg.  This function is
 * called from a relatively hot path and optimizes the common cases using
 * wb_find_current().
 */
static inline struct bdi_writeback *
wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
{
	struct bdi_writeback *wb;

	rcu_read_lock();
	wb = wb_find_current(bdi);
	if (wb && unlikely(!wb_tryget(wb)))
		wb = NULL;
	rcu_read_unlock();

	if (unlikely(!wb)) {
		struct cgroup_subsys_state *memcg_css;

		memcg_css = task_get_css(current, memory_cgrp_id);
		wb = wb_get_create(bdi, memcg_css, gfp);
		css_put(memcg_css);
	}
	return wb;
}

331 332 333 334 335 336 337 338 339 340 341 342
/**
 * inode_to_wb_is_valid - test whether an inode has a wb associated
 * @inode: inode of interest
 *
 * Returns %true if @inode has a wb associated.  May be called without any
 * locking.
 */
static inline bool inode_to_wb_is_valid(struct inode *inode)
{
	return inode->i_wb;
}

343 344 345 346
/**
 * inode_to_wb - determine the wb of an inode
 * @inode: inode of interest
 *
347
 * Returns the wb @inode is currently associated with.  The caller must be
M
Matthew Wilcox 已提交
348
 * holding either @inode->i_lock, the i_pages lock, or the
349
 * associated wb's list_lock.
350
 */
351
static inline struct bdi_writeback *inode_to_wb(const struct inode *inode)
352
{
353 354 355
#ifdef CONFIG_LOCKDEP
	WARN_ON_ONCE(debug_locks &&
		     (!lockdep_is_held(&inode->i_lock) &&
M
Matthew Wilcox 已提交
356
		      !lockdep_is_held(&inode->i_mapping->i_pages.xa_lock) &&
357 358
		      !lockdep_is_held(&inode->i_wb->list_lock)));
#endif
359 360 361
	return inode->i_wb;
}

362 363 364
/**
 * unlocked_inode_to_wb_begin - begin unlocked inode wb access transaction
 * @inode: target inode
G
Greg Thelen 已提交
365
 * @cookie: output param, to be passed to the end function
366 367
 *
 * The caller wants to access the wb associated with @inode but isn't
M
Matthew Wilcox 已提交
368
 * holding inode->i_lock, the i_pages lock or wb->list_lock.  This
369 370 371 372
 * function determines the wb associated with @inode and ensures that the
 * association doesn't change until the transaction is finished with
 * unlocked_inode_to_wb_end().
 *
G
Greg Thelen 已提交
373 374 375
 * The caller must call unlocked_inode_to_wb_end() with *@cookie afterwards and
 * can't sleep during the transaction.  IRQs may or may not be disabled on
 * return.
376 377
 */
static inline struct bdi_writeback *
G
Greg Thelen 已提交
378
unlocked_inode_to_wb_begin(struct inode *inode, struct wb_lock_cookie *cookie)
379 380 381 382 383 384 385
{
	rcu_read_lock();

	/*
	 * Paired with store_release in inode_switch_wb_work_fn() and
	 * ensures that we see the new wb if we see cleared I_WB_SWITCH.
	 */
G
Greg Thelen 已提交
386
	cookie->locked = smp_load_acquire(&inode->i_state) & I_WB_SWITCH;
387

G
Greg Thelen 已提交
388 389
	if (unlikely(cookie->locked))
		xa_lock_irqsave(&inode->i_mapping->i_pages, cookie->flags);
390 391

	/*
M
Matthew Wilcox 已提交
392 393
	 * Protected by either !I_WB_SWITCH + rcu_read_lock() or the i_pages
	 * lock.  inode_to_wb() will bark.  Deref directly.
394 395
	 */
	return inode->i_wb;
396 397 398 399 400
}

/**
 * unlocked_inode_to_wb_end - end inode wb access transaction
 * @inode: target inode
G
Greg Thelen 已提交
401
 * @cookie: @cookie from unlocked_inode_to_wb_begin()
402
 */
G
Greg Thelen 已提交
403 404
static inline void unlocked_inode_to_wb_end(struct inode *inode,
					    struct wb_lock_cookie *cookie)
405
{
G
Greg Thelen 已提交
406 407
	if (unlikely(cookie->locked))
		xa_unlock_irqrestore(&inode->i_mapping->i_pages, cookie->flags);
408 409 410 411

	rcu_read_unlock();
}

412 413 414 415 416 417 418
void insert_memcg_blkcg_link(struct cgroup_subsys *ss,
			     struct list_head *tmp_links,
			     struct css_set *cset);
int allocate_memcg_blkcg_links(int count, struct list_head *tmp_links);
void free_memcg_blkcg_links(struct list_head *links_to_free);
void delete_memcg_blkcg_link(struct cgroup_subsys *ss,
			     struct cgroup_subsys_state *css);
419 420 421 422 423 424 425
#else	/* CONFIG_CGROUP_WRITEBACK */

static inline bool inode_cgwb_enabled(struct inode *inode)
{
	return false;
}

426 427 428
static inline struct bdi_writeback_congested *
wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
{
429
	refcount_inc(&bdi->wb_congested->refcnt);
430
	return bdi->wb_congested;
431 432 433 434
}

static inline void wb_congested_put(struct bdi_writeback_congested *congested)
{
435
	if (refcount_dec_and_test(&congested->refcnt))
436
		kfree(congested);
437 438 439 440 441 442 443 444 445 446 447 448 449
}

static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
{
	return &bdi->wb;
}

static inline struct bdi_writeback *
wb_get_create_current(struct backing_dev_info *bdi, gfp_t gfp)
{
	return &bdi->wb;
}

450 451 452 453 454
static inline bool inode_to_wb_is_valid(struct inode *inode)
{
	return true;
}

455 456 457 458 459
static inline struct bdi_writeback *inode_to_wb(struct inode *inode)
{
	return &inode_to_bdi(inode)->wb;
}

460
static inline struct bdi_writeback *
G
Greg Thelen 已提交
461
unlocked_inode_to_wb_begin(struct inode *inode, struct wb_lock_cookie *cookie)
462 463 464 465
{
	return inode_to_wb(inode);
}

G
Greg Thelen 已提交
466 467
static inline void unlocked_inode_to_wb_end(struct inode *inode,
					    struct wb_lock_cookie *cookie)
468 469 470
{
}

471 472 473 474 475 476 477 478
static inline void wb_memcg_offline(struct mem_cgroup *memcg)
{
}

static inline void wb_blkcg_offline(struct blkcg *blkcg)
{
}

479 480 481 482 483
static inline int inode_congested(struct inode *inode, int cong_bits)
{
	return wb_congested(&inode_to_bdi(inode)->wb, cong_bits);
}

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
static inline void insert_memcg_blkcg_link(struct cgroup_subsys *ss,
					   struct list_head *tmp_links,
					   struct css_set *cset)
{
}

static inline int allocate_memcg_blkcg_links(int count, struct list_head *tmp_links)
{
	return 0;
}

static inline void free_memcg_blkcg_links(struct list_head *links_to_free)
{
}

static inline void delete_memcg_blkcg_link(struct cgroup_subsys *ss,
					   struct cgroup_subsys_state *css)
{
}

504 505
#endif	/* CONFIG_CGROUP_WRITEBACK */

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
static inline int inode_read_congested(struct inode *inode)
{
	return inode_congested(inode, 1 << WB_sync_congested);
}

static inline int inode_write_congested(struct inode *inode)
{
	return inode_congested(inode, 1 << WB_async_congested);
}

static inline int inode_rw_congested(struct inode *inode)
{
	return inode_congested(inode, (1 << WB_sync_congested) |
				      (1 << WB_async_congested));
}

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
static inline int bdi_congested(struct backing_dev_info *bdi, int cong_bits)
{
	return wb_congested(&bdi->wb, cong_bits);
}

static inline int bdi_read_congested(struct backing_dev_info *bdi)
{
	return bdi_congested(bdi, 1 << WB_sync_congested);
}

static inline int bdi_write_congested(struct backing_dev_info *bdi)
{
	return bdi_congested(bdi, 1 << WB_async_congested);
}

static inline int bdi_rw_congested(struct backing_dev_info *bdi)
{
	return bdi_congested(bdi, (1 << WB_sync_congested) |
				  (1 << WB_async_congested));
}

543
#endif	/* _LINUX_BACKING_DEV_H */