1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * fs/fs-writeback.c 4 * 5 * Copyright (C) 2002, Linus Torvalds. 6 * 7 * Contains all the functions related to writing back and waiting 8 * upon dirty inodes against superblocks, and writing back dirty 9 * pages against inodes. ie: data writeback. Writeout of the 10 * inode itself is not handled here. 11 * 12 * 10Apr2002 Andrew Morton 13 * Split out of fs/inode.c 14 * Additions for address_space-based writeback 15 */ 16 17 #include <linux/sched/sysctl.h> 18 #include <linux/kernel.h> 19 #include <linux/export.h> 20 #include <linux/spinlock.h> 21 #include <linux/slab.h> 22 #include <linux/sched.h> 23 #include <linux/fs.h> 24 #include <linux/mm.h> 25 #include <linux/pagemap.h> 26 #include <linux/kthread.h> 27 #include <linux/writeback.h> 28 #include <linux/blk_plug.h> 29 #include <linux/backing-dev.h> 30 #include <linux/tracepoint.h> 31 #include <linux/device.h> 32 #include <linux/memcontrol.h> 33 #include "internal.h" 34 35 /* 36 * Passed into wb_writeback(), essentially a subset of writeback_control 37 */ 38 struct wb_writeback_work { 39 long nr_pages; 40 struct super_block *sb; 41 enum writeback_sync_modes sync_mode; 42 unsigned int tagged_writepages:1; 43 unsigned int for_kupdate:1; 44 unsigned int range_cyclic:1; 45 unsigned int for_background:1; 46 unsigned int for_sync:1; /* sync(2) WB_SYNC_ALL writeback */ 47 unsigned int auto_free:1; /* free on completion */ 48 enum wb_reason reason; /* why was writeback initiated? */ 49 50 struct list_head list; /* pending work list */ 51 struct wb_completion *done; /* set if the caller waits */ 52 }; 53 54 /* 55 * If an inode is constantly having its pages dirtied, but then the 56 * updates stop dirtytime_expire_interval seconds in the past, it's 57 * possible for the worst case time between when an inode has its 58 * timestamps updated and when they finally get written out to be two 59 * dirtytime_expire_intervals. We set the default to 12 hours (in 60 * seconds), which means most of the time inodes will have their 61 * timestamps written to disk after 12 hours, but in the worst case a 62 * few inodes might not their timestamps updated for 24 hours. 63 */ 64 static unsigned int dirtytime_expire_interval = 12 * 60 * 60; 65 66 static inline struct inode *wb_inode(struct list_head *head) 67 { 68 return list_entry(head, struct inode, i_io_list); 69 } 70 71 /* 72 * Include the creation of the trace points after defining the 73 * wb_writeback_work structure and inline functions so that the definition 74 * remains local to this file. 75 */ 76 #define CREATE_TRACE_POINTS 77 #include <trace/events/writeback.h> 78 79 EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage); 80 81 static bool wb_io_lists_populated(struct bdi_writeback *wb) 82 { 83 if (wb_has_dirty_io(wb)) { 84 return false; 85 } else { 86 set_bit(WB_has_dirty_io, &wb->state); 87 WARN_ON_ONCE(!wb->avg_write_bandwidth); 88 atomic_long_add(wb->avg_write_bandwidth, 89 &wb->bdi->tot_write_bandwidth); 90 return true; 91 } 92 } 93 94 static void wb_io_lists_depopulated(struct bdi_writeback *wb) 95 { 96 if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) && 97 list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) { 98 clear_bit(WB_has_dirty_io, &wb->state); 99 WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth, 100 &wb->bdi->tot_write_bandwidth) < 0); 101 } 102 } 103 104 /** 105 * inode_io_list_move_locked - move an inode onto a bdi_writeback IO list 106 * @inode: inode to be moved 107 * @wb: target bdi_writeback 108 * @head: one of @wb->b_{dirty|io|more_io|dirty_time} 109 * 110 * Move @inode->i_io_list to @list of @wb and set %WB_has_dirty_io. 111 * Returns %true if @inode is the first occupant of the !dirty_time IO 112 * lists; otherwise, %false. 113 */ 114 static bool inode_io_list_move_locked(struct inode *inode, 115 struct bdi_writeback *wb, 116 struct list_head *head) 117 { 118 assert_spin_locked(&wb->list_lock); 119 assert_spin_locked(&inode->i_lock); 120 WARN_ON_ONCE(inode_state_read(inode) & I_FREEING); 121 122 list_move(&inode->i_io_list, head); 123 124 /* dirty_time doesn't count as dirty_io until expiration */ 125 if (head != &wb->b_dirty_time) 126 return wb_io_lists_populated(wb); 127 128 wb_io_lists_depopulated(wb); 129 return false; 130 } 131 132 static void wb_wakeup(struct bdi_writeback *wb) 133 { 134 spin_lock_irq(&wb->work_lock); 135 if (test_bit(WB_registered, &wb->state)) 136 mod_delayed_work(bdi_wq, &wb->dwork, 0); 137 spin_unlock_irq(&wb->work_lock); 138 } 139 140 /* 141 * This function is used when the first inode for this wb is marked dirty. It 142 * wakes-up the corresponding bdi thread which should then take care of the 143 * periodic background write-out of dirty inodes. Since the write-out would 144 * starts only 'dirty_writeback_interval' centisecs from now anyway, we just 145 * set up a timer which wakes the bdi thread up later. 146 * 147 * Note, we wouldn't bother setting up the timer, but this function is on the 148 * fast-path (used by '__mark_inode_dirty()'), so we save few context switches 149 * by delaying the wake-up. 150 * 151 * We have to be careful not to postpone flush work if it is scheduled for 152 * earlier. Thus we use queue_delayed_work(). 153 */ 154 static void wb_wakeup_delayed(struct bdi_writeback *wb) 155 { 156 unsigned long timeout; 157 158 timeout = msecs_to_jiffies(dirty_writeback_interval * 10); 159 spin_lock_irq(&wb->work_lock); 160 if (test_bit(WB_registered, &wb->state)) 161 queue_delayed_work(bdi_wq, &wb->dwork, timeout); 162 spin_unlock_irq(&wb->work_lock); 163 } 164 165 static void finish_writeback_work(struct wb_writeback_work *work) 166 { 167 struct wb_completion *done = work->done; 168 169 if (work->auto_free) 170 kfree(work); 171 if (done) { 172 wait_queue_head_t *waitq = done->waitq; 173 174 /* @done can't be accessed after the following dec */ 175 if (atomic_dec_and_test(&done->cnt)) 176 wake_up_all(waitq); 177 } 178 } 179 180 static void wb_queue_work(struct bdi_writeback *wb, 181 struct wb_writeback_work *work) 182 { 183 trace_writeback_queue(wb, work); 184 185 if (work->done) 186 atomic_inc(&work->done->cnt); 187 188 spin_lock_irq(&wb->work_lock); 189 190 if (test_bit(WB_registered, &wb->state)) { 191 list_add_tail(&work->list, &wb->work_list); 192 mod_delayed_work(bdi_wq, &wb->dwork, 0); 193 } else 194 finish_writeback_work(work); 195 196 spin_unlock_irq(&wb->work_lock); 197 } 198 199 static bool wb_wait_for_completion_cb(struct wb_completion *done) 200 { 201 unsigned long timeout = sysctl_hung_task_timeout_secs; 202 unsigned long waited_secs = (jiffies - done->wait_start) / HZ; 203 204 done->progress_stamp = jiffies; 205 if (timeout && (waited_secs > timeout)) 206 pr_info("INFO: The task %s:%d has been waiting for writeback " 207 "completion for more than %lu seconds.", 208 current->comm, current->pid, waited_secs); 209 210 return !atomic_read(&done->cnt); 211 } 212 213 /** 214 * wb_wait_for_completion - wait for completion of bdi_writeback_works 215 * @done: target wb_completion 216 * 217 * Wait for one or more work items issued to @bdi with their ->done field 218 * set to @done, which should have been initialized with 219 * DEFINE_WB_COMPLETION(). This function returns after all such work items 220 * are completed. Work items which are waited upon aren't freed 221 * automatically on completion. 222 */ 223 void wb_wait_for_completion(struct wb_completion *done) 224 { 225 done->wait_start = jiffies; 226 atomic_dec(&done->cnt); /* put down the initial count */ 227 wait_event(*done->waitq, wb_wait_for_completion_cb(done)); 228 } 229 230 #ifdef CONFIG_CGROUP_WRITEBACK 231 232 /* 233 * Parameters for foreign inode detection, see wbc_detach_inode() to see 234 * how they're used. 235 * 236 * These parameters are inherently heuristical as the detection target 237 * itself is fuzzy. All we want to do is detaching an inode from the 238 * current owner if it's being written to by some other cgroups too much. 239 * 240 * The current cgroup writeback is built on the assumption that multiple 241 * cgroups writing to the same inode concurrently is very rare and a mode 242 * of operation which isn't well supported. As such, the goal is not 243 * taking too long when a different cgroup takes over an inode while 244 * avoiding too aggressive flip-flops from occasional foreign writes. 245 * 246 * We record, very roughly, 2s worth of IO time history and if more than 247 * half of that is foreign, trigger the switch. The recording is quantized 248 * to 16 slots. To avoid tiny writes from swinging the decision too much, 249 * writes smaller than 1/8 of avg size are ignored. 250 */ 251 #define WB_FRN_TIME_SHIFT 13 /* 1s = 2^13, up to 8 secs w/ 16bit */ 252 #define WB_FRN_TIME_AVG_SHIFT 3 /* avg = avg * 7/8 + new * 1/8 */ 253 #define WB_FRN_TIME_CUT_DIV 8 /* ignore rounds < avg / 8 */ 254 #define WB_FRN_TIME_PERIOD (2 * (1 << WB_FRN_TIME_SHIFT)) /* 2s */ 255 256 #define WB_FRN_HIST_SLOTS 16 /* inode->i_wb_frn_history is 16bit */ 257 #define WB_FRN_HIST_UNIT (WB_FRN_TIME_PERIOD / WB_FRN_HIST_SLOTS) 258 /* each slot's duration is 2s / 16 */ 259 #define WB_FRN_HIST_THR_SLOTS (WB_FRN_HIST_SLOTS / 2) 260 /* if foreign slots >= 8, switch */ 261 #define WB_FRN_HIST_MAX_SLOTS (WB_FRN_HIST_THR_SLOTS / 2 + 1) 262 /* one round can affect up to 5 slots */ 263 #define WB_FRN_MAX_IN_FLIGHT 1024 /* don't queue too many concurrently */ 264 265 /* 266 * Maximum inodes per isw. A specific value has been chosen to make 267 * struct inode_switch_wbs_context fit into 1024 bytes kmalloc. 268 */ 269 #define WB_MAX_INODES_PER_ISW ((1024UL - sizeof(struct inode_switch_wbs_context)) \ 270 / sizeof(struct inode *)) 271 272 static atomic_t isw_nr_in_flight = ATOMIC_INIT(0); 273 static struct workqueue_struct *isw_wq; 274 275 void __inode_attach_wb(struct inode *inode, struct folio *folio) 276 { 277 struct backing_dev_info *bdi = inode_to_bdi(inode); 278 struct bdi_writeback *wb = NULL; 279 280 if (inode_cgwb_enabled(inode)) { 281 struct cgroup_subsys_state *memcg_css; 282 283 /* must pin memcg_css, see wb_get_create() */ 284 if (folio) 285 memcg_css = get_mem_cgroup_css_from_folio(folio); 286 else 287 memcg_css = task_get_css(current, memory_cgrp_id); 288 wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 289 css_put(memcg_css); 290 } 291 292 if (!wb) 293 wb = &bdi->wb; 294 295 /* 296 * There may be multiple instances of this function racing to 297 * update the same inode. Use cmpxchg() to tell the winner. 298 */ 299 if (unlikely(cmpxchg(&inode->i_wb, NULL, wb))) 300 wb_put(wb); 301 } 302 EXPORT_SYMBOL_GPL(__inode_attach_wb); 303 304 /** 305 * inode_cgwb_move_to_attached - put the inode onto wb->b_attached list 306 * @inode: inode of interest with i_lock held 307 * @wb: target bdi_writeback 308 * 309 * Remove the inode from wb's io lists and if necessarily put onto b_attached 310 * list. Only inodes attached to cgwb's are kept on this list. 311 */ 312 static void inode_cgwb_move_to_attached(struct inode *inode, 313 struct bdi_writeback *wb) 314 { 315 assert_spin_locked(&wb->list_lock); 316 assert_spin_locked(&inode->i_lock); 317 WARN_ON_ONCE(inode_state_read(inode) & I_FREEING); 318 319 inode_state_clear(inode, I_SYNC_QUEUED); 320 if (wb != &wb->bdi->wb) 321 list_move(&inode->i_io_list, &wb->b_attached); 322 else 323 list_del_init(&inode->i_io_list); 324 wb_io_lists_depopulated(wb); 325 } 326 327 /** 328 * locked_inode_to_wb_and_lock_list - determine a locked inode's wb and lock it 329 * @inode: inode of interest with i_lock held 330 * 331 * Returns @inode's wb with its list_lock held. @inode->i_lock must be 332 * held on entry and is released on return. The returned wb is guaranteed 333 * to stay @inode's associated wb until its list_lock is released. 334 */ 335 static struct bdi_writeback * 336 locked_inode_to_wb_and_lock_list(struct inode *inode) 337 __releases(&inode->i_lock) 338 __acquires(&wb->list_lock) 339 { 340 while (true) { 341 struct bdi_writeback *wb = inode_to_wb(inode); 342 343 /* 344 * inode_to_wb() association is protected by both 345 * @inode->i_lock and @wb->list_lock but list_lock nests 346 * outside i_lock. Drop i_lock and verify that the 347 * association hasn't changed after acquiring list_lock. 348 */ 349 wb_get(wb); 350 spin_unlock(&inode->i_lock); 351 spin_lock(&wb->list_lock); 352 353 /* i_wb may have changed inbetween, can't use inode_to_wb() */ 354 if (likely(wb == inode->i_wb)) { 355 wb_put(wb); /* @inode already has ref */ 356 return wb; 357 } 358 359 spin_unlock(&wb->list_lock); 360 wb_put(wb); 361 cpu_relax(); 362 spin_lock(&inode->i_lock); 363 } 364 } 365 366 /** 367 * inode_to_wb_and_lock_list - determine an inode's wb and lock it 368 * @inode: inode of interest 369 * 370 * Same as locked_inode_to_wb_and_lock_list() but @inode->i_lock isn't held 371 * on entry. 372 */ 373 static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) 374 __acquires(&wb->list_lock) 375 { 376 spin_lock(&inode->i_lock); 377 return locked_inode_to_wb_and_lock_list(inode); 378 } 379 380 struct inode_switch_wbs_context { 381 /* List of queued switching contexts for the wb */ 382 struct llist_node list; 383 384 /* 385 * Multiple inodes can be switched at once. The switching procedure 386 * consists of two parts, separated by a RCU grace period. To make 387 * sure that the second part is executed for each inode gone through 388 * the first part, all inode pointers are placed into a NULL-terminated 389 * array embedded into struct inode_switch_wbs_context. Otherwise 390 * an inode could be left in a non-consistent state. 391 */ 392 struct inode *inodes[]; 393 }; 394 395 static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi) 396 { 397 down_write(&bdi->wb_switch_rwsem); 398 } 399 400 static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi) 401 { 402 up_write(&bdi->wb_switch_rwsem); 403 } 404 405 static bool inode_do_switch_wbs(struct inode *inode, 406 struct bdi_writeback *old_wb, 407 struct bdi_writeback *new_wb) 408 { 409 struct address_space *mapping = inode->i_mapping; 410 XA_STATE(xas, &mapping->i_pages, 0); 411 struct folio *folio; 412 bool switched = false; 413 414 spin_lock(&inode->i_lock); 415 xa_lock_irq(&mapping->i_pages); 416 417 /* 418 * Once I_FREEING or I_WILL_FREE are visible under i_lock, the eviction 419 * path owns the inode and we shouldn't modify ->i_io_list. 420 */ 421 if (unlikely(inode_state_read(inode) & (I_FREEING | I_WILL_FREE))) 422 goto skip_switch; 423 424 trace_inode_switch_wbs(inode, old_wb, new_wb); 425 426 /* 427 * Count and transfer stats. Note that PAGECACHE_TAG_DIRTY points 428 * to possibly dirty folios while PAGECACHE_TAG_WRITEBACK points to 429 * folios actually under writeback. 430 */ 431 xas_for_each_marked(&xas, folio, ULONG_MAX, PAGECACHE_TAG_DIRTY) { 432 if (folio_test_dirty(folio)) { 433 long nr = folio_nr_pages(folio); 434 wb_stat_mod(old_wb, WB_RECLAIMABLE, -nr); 435 wb_stat_mod(new_wb, WB_RECLAIMABLE, nr); 436 if (folio_test_dropbehind(folio)) { 437 wb_stat_mod(old_wb, WB_DONTCACHE_DIRTY, -nr); 438 wb_stat_mod(new_wb, WB_DONTCACHE_DIRTY, nr); 439 } 440 } 441 } 442 443 xas_set(&xas, 0); 444 xas_for_each_marked(&xas, folio, ULONG_MAX, PAGECACHE_TAG_WRITEBACK) { 445 long nr = folio_nr_pages(folio); 446 WARN_ON_ONCE(!folio_test_writeback(folio)); 447 wb_stat_mod(old_wb, WB_WRITEBACK, -nr); 448 wb_stat_mod(new_wb, WB_WRITEBACK, nr); 449 } 450 451 if (mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) { 452 atomic_dec(&old_wb->writeback_inodes); 453 atomic_inc(&new_wb->writeback_inodes); 454 } 455 456 wb_get(new_wb); 457 458 /* 459 * Transfer to @new_wb's IO list if necessary. If the @inode is dirty, 460 * the specific list @inode was on is ignored and the @inode is put on 461 * ->b_dirty which is always correct including from ->b_dirty_time. 462 * If the @inode was clean, it means it was on the b_attached list, so 463 * move it onto the b_attached list of @new_wb. 464 */ 465 if (!list_empty(&inode->i_io_list)) { 466 inode->i_wb = new_wb; 467 468 if (inode_state_read(inode) & I_DIRTY_ALL) { 469 /* 470 * We need to keep b_dirty list sorted by 471 * dirtied_when. However properly sorting the 472 * inode in the list gets too expensive when switching 473 * many inodes. So just attach inode at the end of the 474 * dirty list and clobber the dirtied_when. 475 */ 476 inode->dirtied_when = jiffies; 477 inode_io_list_move_locked(inode, new_wb, 478 &new_wb->b_dirty); 479 } else { 480 inode_cgwb_move_to_attached(inode, new_wb); 481 } 482 } else { 483 inode->i_wb = new_wb; 484 } 485 486 /* ->i_wb_frn updates may race wbc_detach_inode() but doesn't matter */ 487 inode->i_wb_frn_winner = 0; 488 inode->i_wb_frn_avg_time = 0; 489 inode->i_wb_frn_history = 0; 490 switched = true; 491 skip_switch: 492 /* 493 * Paired with an acquire fence in unlocked_inode_to_wb_begin() and 494 * ensures that the new wb is visible if they see !I_WB_SWITCH. 495 */ 496 smp_wmb(); 497 inode_state_clear(inode, I_WB_SWITCH); 498 499 xa_unlock_irq(&mapping->i_pages); 500 spin_unlock(&inode->i_lock); 501 502 return switched; 503 } 504 505 static inline void cgroup_writeback_pin(struct super_block *sb) 506 { 507 atomic_inc(&sb->s_isw_nr_in_flight); 508 } 509 510 static inline void cgroup_writeback_unpin(struct super_block *sb) 511 { 512 if (atomic_dec_and_test(&sb->s_isw_nr_in_flight)) 513 wake_up_var(&sb->s_isw_nr_in_flight); 514 } 515 516 static inline void cgroup_writeback_drain(struct super_block *sb) 517 { 518 wait_var_event(&sb->s_isw_nr_in_flight, 519 !atomic_read(&sb->s_isw_nr_in_flight)); 520 } 521 522 static void process_inode_switch_wbs(struct bdi_writeback *new_wb, 523 struct inode_switch_wbs_context *isw) 524 { 525 struct backing_dev_info *bdi = inode_to_bdi(isw->inodes[0]); 526 struct bdi_writeback *old_wb = isw->inodes[0]->i_wb; 527 unsigned long nr_switched = 0; 528 struct inode **inodep; 529 530 /* 531 * If @inode switches cgwb membership while sync_inodes_sb() is 532 * being issued, sync_inodes_sb() might miss it. Synchronize. 533 */ 534 down_read(&bdi->wb_switch_rwsem); 535 536 inodep = isw->inodes; 537 /* 538 * By the time control reaches here, RCU grace period has passed 539 * since I_WB_SWITCH assertion and all wb stat update transactions 540 * between unlocked_inode_to_wb_begin/end() are guaranteed to be 541 * synchronizing against the i_pages lock. 542 * 543 * Grabbing old_wb->list_lock, inode->i_lock and the i_pages lock 544 * gives us exclusion against all wb related operations on @inode 545 * including IO list manipulations and stat updates. 546 */ 547 relock: 548 if (old_wb < new_wb) { 549 spin_lock(&old_wb->list_lock); 550 spin_lock_nested(&new_wb->list_lock, SINGLE_DEPTH_NESTING); 551 } else { 552 spin_lock(&new_wb->list_lock); 553 spin_lock_nested(&old_wb->list_lock, SINGLE_DEPTH_NESTING); 554 } 555 556 while (*inodep) { 557 WARN_ON_ONCE((*inodep)->i_wb != old_wb); 558 if (inode_do_switch_wbs(*inodep, old_wb, new_wb)) 559 nr_switched++; 560 inodep++; 561 if (*inodep && need_resched()) { 562 spin_unlock(&new_wb->list_lock); 563 spin_unlock(&old_wb->list_lock); 564 cond_resched(); 565 goto relock; 566 } 567 } 568 569 wb_io_lists_depopulated(old_wb); 570 spin_unlock(&new_wb->list_lock); 571 spin_unlock(&old_wb->list_lock); 572 573 up_read(&bdi->wb_switch_rwsem); 574 575 if (nr_switched) { 576 wb_wakeup(new_wb); 577 wb_put_many(old_wb, nr_switched); 578 } 579 580 for (inodep = isw->inodes; *inodep; inodep++) { 581 struct super_block *sb = (*inodep)->i_sb; 582 583 iput(*inodep); 584 cgroup_writeback_unpin(sb); 585 } 586 wb_put(new_wb); 587 kfree(isw); 588 atomic_dec(&isw_nr_in_flight); 589 } 590 591 void inode_switch_wbs_work_fn(struct work_struct *work) 592 { 593 struct bdi_writeback *new_wb = container_of(work, struct bdi_writeback, 594 switch_work); 595 struct inode_switch_wbs_context *isw, *next_isw; 596 struct llist_node *list; 597 598 list = llist_del_all(&new_wb->switch_wbs_ctxs); 599 /* 600 * Nothing to do? That would be a problem as references held by isw 601 * items protect wb from freeing... 602 */ 603 if (WARN_ON_ONCE(!list)) 604 return; 605 606 /* 607 * Grab our reference to wb so that it cannot get freed under us 608 * after we process all the isw items. 609 */ 610 wb_get(new_wb); 611 /* 612 * In addition to synchronizing among switchers, I_WB_SWITCH 613 * tells the RCU protected stat update paths to grab the i_page 614 * lock so that stat transfer can synchronize against them. 615 * Let's continue after I_WB_SWITCH is guaranteed to be 616 * visible. 617 */ 618 synchronize_rcu(); 619 620 llist_for_each_entry_safe(isw, next_isw, list, list) 621 process_inode_switch_wbs(new_wb, isw); 622 wb_put(new_wb); 623 } 624 625 static bool inode_prepare_wbs_switch(struct inode *inode, 626 struct bdi_writeback *new_wb) 627 { 628 /* Avoid the atomic_inc/smp_mb dance once SB_ACTIVE is gone. */ 629 if (!(inode->i_sb->s_flags & SB_ACTIVE)) 630 return false; 631 632 /* 633 * Pairs with smp_mb() in cgroup_writeback_umount(): the umounter either 634 * sees a non-zero counter and waits, or we see SB_ACTIVE clear below. 635 */ 636 cgroup_writeback_pin(inode->i_sb); 637 smp_mb(); 638 639 if (IS_DAX(inode)) 640 goto out_unpin; 641 642 /* while holding I_WB_SWITCH, no one else can update the association */ 643 spin_lock(&inode->i_lock); 644 if (!(inode->i_sb->s_flags & SB_ACTIVE) || 645 inode_state_read(inode) & (I_WB_SWITCH | I_FREEING | I_WILL_FREE) || 646 inode_to_wb(inode) == new_wb) { 647 spin_unlock(&inode->i_lock); 648 goto out_unpin; 649 } 650 inode_state_set(inode, I_WB_SWITCH); 651 __iget(inode); 652 spin_unlock(&inode->i_lock); 653 654 return true; 655 656 out_unpin: 657 cgroup_writeback_unpin(inode->i_sb); 658 return false; 659 } 660 661 static void wb_queue_isw(struct bdi_writeback *wb, 662 struct inode_switch_wbs_context *isw) 663 { 664 if (llist_add(&isw->list, &wb->switch_wbs_ctxs)) 665 queue_work(isw_wq, &wb->switch_work); 666 } 667 668 /** 669 * inode_switch_wbs - change the wb association of an inode 670 * @inode: target inode 671 * @new_wb_id: ID of the new wb 672 * 673 * Switch @inode's wb association to the wb identified by @new_wb_id. The 674 * switching is performed asynchronously and may fail silently. 675 */ 676 static void inode_switch_wbs(struct inode *inode, int new_wb_id) 677 { 678 struct backing_dev_info *bdi = inode_to_bdi(inode); 679 struct cgroup_subsys_state *memcg_css; 680 struct inode_switch_wbs_context *isw; 681 struct bdi_writeback *new_wb = NULL; 682 683 /* noop if seems to be already in progress */ 684 if (inode_state_read_once(inode) & I_WB_SWITCH) 685 return; 686 687 /* avoid queueing a new switch if too many are already in flight */ 688 if (atomic_read(&isw_nr_in_flight) > WB_FRN_MAX_IN_FLIGHT) 689 return; 690 691 isw = kzalloc_flex(*isw, inodes, 2, GFP_ATOMIC); 692 if (!isw) 693 return; 694 695 atomic_inc(&isw_nr_in_flight); 696 697 /* find and pin the new wb */ 698 rcu_read_lock(); 699 memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys); 700 if (memcg_css && !css_tryget(memcg_css)) 701 memcg_css = NULL; 702 rcu_read_unlock(); 703 if (!memcg_css) 704 goto out_free; 705 706 new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 707 css_put(memcg_css); 708 if (!new_wb) 709 goto out_free; 710 711 if (!inode_prepare_wbs_switch(inode, new_wb)) 712 goto out_free; 713 714 isw->inodes[0] = inode; 715 716 trace_inode_switch_wbs_queue(inode->i_wb, new_wb, 1); 717 wb_queue_isw(new_wb, isw); 718 return; 719 720 out_free: 721 atomic_dec(&isw_nr_in_flight); 722 if (new_wb) 723 wb_put(new_wb); 724 kfree(isw); 725 } 726 727 static bool isw_prepare_wbs_switch(struct bdi_writeback *new_wb, 728 struct inode_switch_wbs_context *isw, 729 struct list_head *list, int *nr) 730 { 731 struct inode *inode, *tmp; 732 LIST_HEAD(scanned); 733 bool full = false; 734 735 /* 736 * Walk from the oldest end and move scanned inodes to the newest 737 * end, so the next scan resumes at unscanned inodes instead of 738 * re-walking an ever-growing run of prepared and skipped ones. 739 * For b_dirty_time this keeps the oldest unscanned inode at the 740 * end move_expired_inodes() picks from; b_attached is unordered. 741 */ 742 list_for_each_entry_safe_reverse(inode, tmp, list, i_io_list) { 743 list_move(&inode->i_io_list, &scanned); 744 745 if (!inode_prepare_wbs_switch(inode, new_wb)) 746 continue; 747 748 isw->inodes[*nr] = inode; 749 (*nr)++; 750 751 if (*nr >= WB_MAX_INODES_PER_ISW - 1) { 752 full = true; 753 break; 754 } 755 } 756 list_splice(&scanned, list); 757 758 return full; 759 } 760 761 /** 762 * cleanup_offline_cgwb - detach associated inodes 763 * @wb: target wb 764 * 765 * Switch all inodes attached to @wb to a nearest living ancestor's wb in order 766 * to eventually release the dying @wb. Returns %true if not all inodes were 767 * switched and the function has to be restarted. 768 */ 769 bool cleanup_offline_cgwb(struct bdi_writeback *wb) 770 { 771 struct cgroup_subsys_state *memcg_css; 772 struct inode_switch_wbs_context *isw; 773 struct bdi_writeback *new_wb; 774 int nr; 775 bool restart = false; 776 777 isw = kzalloc_flex(*isw, inodes, WB_MAX_INODES_PER_ISW); 778 if (!isw) 779 return restart; 780 781 atomic_inc(&isw_nr_in_flight); 782 783 for (memcg_css = wb->memcg_css->parent; memcg_css; 784 memcg_css = memcg_css->parent) { 785 new_wb = wb_get_create(wb->bdi, memcg_css, GFP_KERNEL); 786 if (new_wb) 787 break; 788 } 789 if (unlikely(!new_wb)) 790 new_wb = &wb->bdi->wb; /* wb_get() is noop for bdi's wb */ 791 792 nr = 0; 793 /* 794 * The scan moves walked inodes to the newest end of b_dirty_time, 795 * past ones it didn't reach. If one of those was dirtied after a 796 * sync's cutoff, the sync's queue_io() stops there and misses the 797 * moved ones. Exclude syncs as process_inode_switch_wbs() does. 798 */ 799 down_read(&wb->bdi->wb_switch_rwsem); 800 spin_lock(&wb->list_lock); 801 /* 802 * In addition to the inodes that have completed writeback, also switch 803 * cgwbs for those inodes only with dirty timestamps. Otherwise, those 804 * inodes won't be written back for a long time when lazytime is 805 * enabled, and thus pinning the dying cgwbs. It won't break the 806 * bandwidth restrictions, as writeback of inode metadata is not 807 * accounted for. 808 */ 809 restart = isw_prepare_wbs_switch(new_wb, isw, &wb->b_attached, &nr); 810 if (!restart) 811 restart = isw_prepare_wbs_switch(new_wb, isw, &wb->b_dirty_time, 812 &nr); 813 spin_unlock(&wb->list_lock); 814 up_read(&wb->bdi->wb_switch_rwsem); 815 816 /* no attached inodes? bail out */ 817 if (nr == 0) { 818 atomic_dec(&isw_nr_in_flight); 819 wb_put(new_wb); 820 kfree(isw); 821 return restart; 822 } 823 824 trace_inode_switch_wbs_queue(wb, new_wb, nr); 825 wb_queue_isw(new_wb, isw); 826 827 return restart; 828 } 829 830 /** 831 * wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it 832 * @wbc: writeback_control of interest 833 * @inode: target inode 834 * 835 * @inode is locked and about to be written back under the control of @wbc. 836 * Record @inode's writeback context into @wbc and unlock the i_lock. On 837 * writeback completion, wbc_detach_inode() should be called. This is used 838 * to track the cgroup writeback context. 839 */ 840 static void wbc_attach_and_unlock_inode(struct writeback_control *wbc, 841 struct inode *inode) 842 __releases(&inode->i_lock) 843 { 844 if (!inode_cgwb_enabled(inode)) { 845 spin_unlock(&inode->i_lock); 846 return; 847 } 848 849 wbc->wb = inode_to_wb(inode); 850 wbc->inode = inode; 851 852 wbc->wb_id = wbc->wb->memcg_css->id; 853 wbc->wb_lcand_id = inode->i_wb_frn_winner; 854 wbc->wb_tcand_id = 0; 855 wbc->wb_bytes = 0; 856 wbc->wb_lcand_bytes = 0; 857 wbc->wb_tcand_bytes = 0; 858 859 wb_get(wbc->wb); 860 spin_unlock(&inode->i_lock); 861 862 /* 863 * A dying wb indicates that either the blkcg associated with the 864 * memcg changed or the associated memcg is dying. In the first 865 * case, a replacement wb should already be available and we should 866 * refresh the wb immediately. In the second case, trying to 867 * refresh will keep failing. 868 */ 869 if (unlikely(wb_dying(wbc->wb) && !css_is_dying(wbc->wb->memcg_css))) 870 inode_switch_wbs(inode, wbc->wb_id); 871 } 872 873 /** 874 * wbc_attach_fdatawrite_inode - associate wbc and inode for fdatawrite 875 * @wbc: writeback_control of interest 876 * @inode: target inode 877 * 878 * This function is to be used by filemap_writeback(), which is an alternative 879 * entry point into writeback code, and first ensures @inode is associated with 880 * a bdi_writeback and attaches it to @wbc. 881 */ 882 void wbc_attach_fdatawrite_inode(struct writeback_control *wbc, 883 struct inode *inode) 884 { 885 spin_lock(&inode->i_lock); 886 inode_attach_wb(inode, NULL); 887 wbc_attach_and_unlock_inode(wbc, inode); 888 } 889 EXPORT_SYMBOL_GPL(wbc_attach_fdatawrite_inode); 890 891 /** 892 * wbc_detach_inode - disassociate wbc from inode and perform foreign detection 893 * @wbc: writeback_control of the just finished writeback 894 * 895 * To be called after a writeback attempt of an inode finishes and undoes 896 * wbc_attach_and_unlock_inode(). Can be called under any context. 897 * 898 * As concurrent write sharing of an inode is expected to be very rare and 899 * memcg only tracks page ownership on first-use basis severely confining 900 * the usefulness of such sharing, cgroup writeback tracks ownership 901 * per-inode. While the support for concurrent write sharing of an inode 902 * is deemed unnecessary, an inode being written to by different cgroups at 903 * different points in time is a lot more common, and, more importantly, 904 * charging only by first-use can too readily lead to grossly incorrect 905 * behaviors (single foreign page can lead to gigabytes of writeback to be 906 * incorrectly attributed). 907 * 908 * To resolve this issue, cgroup writeback detects the majority dirtier of 909 * an inode and transfers the ownership to it. To avoid unnecessary 910 * oscillation, the detection mechanism keeps track of history and gives 911 * out the switch verdict only if the foreign usage pattern is stable over 912 * a certain amount of time and/or writeback attempts. 913 * 914 * On each writeback attempt, @wbc tries to detect the majority writer 915 * using Boyer-Moore majority vote algorithm. In addition to the byte 916 * count from the majority voting, it also counts the bytes written for the 917 * current wb and the last round's winner wb (max of last round's current 918 * wb, the winner from two rounds ago, and the last round's majority 919 * candidate). Keeping track of the historical winner helps the algorithm 920 * to semi-reliably detect the most active writer even when it's not the 921 * absolute majority. 922 * 923 * Once the winner of the round is determined, whether the winner is 924 * foreign or not and how much IO time the round consumed is recorded in 925 * inode->i_wb_frn_history. If the amount of recorded foreign IO time is 926 * over a certain threshold, the switch verdict is given. 927 */ 928 void wbc_detach_inode(struct writeback_control *wbc) 929 { 930 struct bdi_writeback *wb = wbc->wb; 931 struct inode *inode = wbc->inode; 932 unsigned long avg_time, max_bytes, max_time; 933 u16 history; 934 int max_id; 935 936 if (!wb) 937 return; 938 939 history = inode->i_wb_frn_history; 940 avg_time = inode->i_wb_frn_avg_time; 941 942 /* pick the winner of this round */ 943 if (wbc->wb_bytes >= wbc->wb_lcand_bytes && 944 wbc->wb_bytes >= wbc->wb_tcand_bytes) { 945 max_id = wbc->wb_id; 946 max_bytes = wbc->wb_bytes; 947 } else if (wbc->wb_lcand_bytes >= wbc->wb_tcand_bytes) { 948 max_id = wbc->wb_lcand_id; 949 max_bytes = wbc->wb_lcand_bytes; 950 } else { 951 max_id = wbc->wb_tcand_id; 952 max_bytes = wbc->wb_tcand_bytes; 953 } 954 955 /* 956 * Calculate the amount of IO time the winner consumed and fold it 957 * into the running average kept per inode. If the consumed IO 958 * time is lower than avag / WB_FRN_TIME_CUT_DIV, ignore it for 959 * deciding whether to switch or not. This is to prevent one-off 960 * small dirtiers from skewing the verdict. 961 */ 962 max_time = DIV_ROUND_UP((max_bytes >> PAGE_SHIFT) << WB_FRN_TIME_SHIFT, 963 wb->avg_write_bandwidth); 964 if (avg_time) 965 avg_time += (max_time >> WB_FRN_TIME_AVG_SHIFT) - 966 (avg_time >> WB_FRN_TIME_AVG_SHIFT); 967 else 968 avg_time = max_time; /* immediate catch up on first run */ 969 970 if (max_time >= avg_time / WB_FRN_TIME_CUT_DIV) { 971 int slots; 972 973 /* 974 * The switch verdict is reached if foreign wb's consume 975 * more than a certain proportion of IO time in a 976 * WB_FRN_TIME_PERIOD. This is loosely tracked by 16 slot 977 * history mask where each bit represents one sixteenth of 978 * the period. Determine the number of slots to shift into 979 * history from @max_time. 980 */ 981 slots = min(DIV_ROUND_UP(max_time, WB_FRN_HIST_UNIT), 982 (unsigned long)WB_FRN_HIST_MAX_SLOTS); 983 history <<= slots; 984 if (wbc->wb_id != max_id) 985 history |= (1U << slots) - 1; 986 987 if (history) 988 trace_inode_foreign_history(inode, wbc, history); 989 990 /* 991 * Switch if the current wb isn't the consistent winner. 992 * If there are multiple closely competing dirtiers, the 993 * inode may switch across them repeatedly over time, which 994 * is okay. The main goal is avoiding keeping an inode on 995 * the wrong wb for an extended period of time. 996 */ 997 if (hweight16(history) > WB_FRN_HIST_THR_SLOTS) 998 inode_switch_wbs(inode, max_id); 999 } 1000 1001 /* 1002 * Multiple instances of this function may race to update the 1003 * following fields but we don't mind occassional inaccuracies. 1004 */ 1005 inode->i_wb_frn_winner = max_id; 1006 inode->i_wb_frn_avg_time = min(avg_time, (unsigned long)U16_MAX); 1007 inode->i_wb_frn_history = history; 1008 1009 wb_put(wbc->wb); 1010 wbc->wb = NULL; 1011 } 1012 EXPORT_SYMBOL_GPL(wbc_detach_inode); 1013 1014 /** 1015 * wbc_account_cgroup_owner - account writeback to update inode cgroup ownership 1016 * @wbc: writeback_control of the writeback in progress 1017 * @folio: folio being written out 1018 * @bytes: number of bytes being written out 1019 * 1020 * @bytes from @folio are about to written out during the writeback 1021 * controlled by @wbc. Keep the book for foreign inode detection. See 1022 * wbc_detach_inode(). 1023 */ 1024 void wbc_account_cgroup_owner(struct writeback_control *wbc, struct folio *folio, 1025 size_t bytes) 1026 { 1027 struct cgroup_subsys_state *css; 1028 int id; 1029 1030 /* 1031 * pageout() path doesn't attach @wbc to the inode being written 1032 * out. This is intentional as we don't want the function to block 1033 * behind a slow cgroup. Ultimately, we want pageout() to kick off 1034 * regular writeback instead of writing things out itself. 1035 */ 1036 if (!wbc->wb || wbc->no_cgroup_owner) 1037 return; 1038 1039 css = get_mem_cgroup_css_from_folio(folio); 1040 /* dead cgroups shouldn't contribute to inode ownership arbitration */ 1041 if (!css_is_online(css)) 1042 goto out; 1043 1044 id = css->id; 1045 1046 if (id == wbc->wb_id) { 1047 wbc->wb_bytes += bytes; 1048 goto out; 1049 } 1050 1051 if (id == wbc->wb_lcand_id) 1052 wbc->wb_lcand_bytes += bytes; 1053 1054 /* Boyer-Moore majority vote algorithm */ 1055 if (!wbc->wb_tcand_bytes) 1056 wbc->wb_tcand_id = id; 1057 if (id == wbc->wb_tcand_id) 1058 wbc->wb_tcand_bytes += bytes; 1059 else 1060 wbc->wb_tcand_bytes -= min(bytes, wbc->wb_tcand_bytes); 1061 out: 1062 css_put(css); 1063 } 1064 EXPORT_SYMBOL_GPL(wbc_account_cgroup_owner); 1065 1066 /** 1067 * wb_split_bdi_pages - split nr_pages to write according to bandwidth 1068 * @wb: target bdi_writeback to split @nr_pages to 1069 * @nr_pages: number of pages to write for the whole bdi 1070 * 1071 * Split @wb's portion of @nr_pages according to @wb's write bandwidth in 1072 * relation to the total write bandwidth of all wb's w/ dirty inodes on 1073 * @wb->bdi. 1074 */ 1075 static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 1076 { 1077 unsigned long this_bw = wb->avg_write_bandwidth; 1078 unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth); 1079 1080 if (nr_pages == LONG_MAX) 1081 return LONG_MAX; 1082 1083 /* 1084 * This may be called on clean wb's and proportional distribution 1085 * may not make sense, just use the original @nr_pages in those 1086 * cases. In general, we wanna err on the side of writing more. 1087 */ 1088 if (!tot_bw || this_bw >= tot_bw) 1089 return nr_pages; 1090 else 1091 return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw); 1092 } 1093 1094 /** 1095 * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi 1096 * @bdi: target backing_dev_info 1097 * @base_work: wb_writeback_work to issue 1098 * @skip_if_busy: skip wb's which already have writeback in progress 1099 * 1100 * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which 1101 * have dirty inodes. If @base_work->nr_page isn't %LONG_MAX, it's 1102 * distributed to the busy wbs according to each wb's proportion in the 1103 * total active write bandwidth of @bdi. 1104 */ 1105 static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 1106 struct wb_writeback_work *base_work, 1107 bool skip_if_busy) 1108 { 1109 struct bdi_writeback *last_wb = NULL; 1110 struct bdi_writeback *wb = list_entry(&bdi->wb_list, 1111 struct bdi_writeback, bdi_node); 1112 1113 might_sleep(); 1114 restart: 1115 rcu_read_lock(); 1116 list_for_each_entry_continue_rcu(wb, &bdi->wb_list, bdi_node) { 1117 DEFINE_WB_COMPLETION(fallback_work_done, bdi); 1118 struct wb_writeback_work fallback_work; 1119 struct wb_writeback_work *work; 1120 long nr_pages; 1121 1122 if (last_wb) { 1123 wb_put(last_wb); 1124 last_wb = NULL; 1125 } 1126 1127 /* SYNC_ALL writes out I_DIRTY_TIME too */ 1128 if (!wb_has_dirty_io(wb) && 1129 (base_work->sync_mode == WB_SYNC_NONE || 1130 list_empty(&wb->b_dirty_time))) 1131 continue; 1132 if (skip_if_busy && writeback_in_progress(wb)) 1133 continue; 1134 1135 nr_pages = wb_split_bdi_pages(wb, base_work->nr_pages); 1136 1137 work = kmalloc_obj(*work, GFP_ATOMIC); 1138 if (work) { 1139 *work = *base_work; 1140 work->nr_pages = nr_pages; 1141 work->auto_free = 1; 1142 wb_queue_work(wb, work); 1143 continue; 1144 } 1145 1146 /* 1147 * If wb_tryget fails, the wb has been shutdown, skip it. 1148 * 1149 * Pin @wb so that it stays on @bdi->wb_list. This allows 1150 * continuing iteration from @wb after dropping and 1151 * regrabbing rcu read lock. 1152 */ 1153 if (!wb_tryget(wb)) 1154 continue; 1155 1156 /* alloc failed, execute synchronously using on-stack fallback */ 1157 work = &fallback_work; 1158 *work = *base_work; 1159 work->nr_pages = nr_pages; 1160 work->auto_free = 0; 1161 work->done = &fallback_work_done; 1162 1163 wb_queue_work(wb, work); 1164 last_wb = wb; 1165 1166 rcu_read_unlock(); 1167 wb_wait_for_completion(&fallback_work_done); 1168 goto restart; 1169 } 1170 rcu_read_unlock(); 1171 1172 if (last_wb) 1173 wb_put(last_wb); 1174 } 1175 1176 /** 1177 * cgroup_writeback_by_id - initiate cgroup writeback from bdi and memcg IDs 1178 * @bdi_id: target bdi id 1179 * @memcg_id: target memcg css id 1180 * @reason: reason why some writeback work initiated 1181 * @done: target wb_completion 1182 * 1183 * Initiate flush of the bdi_writeback identified by @bdi_id and @memcg_id 1184 * with the specified parameters. 1185 */ 1186 int cgroup_writeback_by_id(u64 bdi_id, int memcg_id, 1187 enum wb_reason reason, struct wb_completion *done) 1188 { 1189 struct backing_dev_info *bdi; 1190 struct cgroup_subsys_state *memcg_css; 1191 struct bdi_writeback *wb; 1192 struct wb_writeback_work *work; 1193 long dirty; 1194 int ret; 1195 1196 /* lookup bdi and memcg */ 1197 bdi = bdi_get_by_id(bdi_id); 1198 if (!bdi) 1199 return -ENOENT; 1200 1201 rcu_read_lock(); 1202 memcg_css = css_from_id(memcg_id, &memory_cgrp_subsys); 1203 if (memcg_css && !css_tryget(memcg_css)) 1204 memcg_css = NULL; 1205 rcu_read_unlock(); 1206 if (!memcg_css) { 1207 ret = -ENOENT; 1208 goto out_bdi_put; 1209 } 1210 1211 /* 1212 * And find the associated wb. If the wb isn't there already 1213 * there's nothing to flush, don't create one. 1214 */ 1215 wb = wb_get_lookup(bdi, memcg_css); 1216 if (!wb) { 1217 ret = -ENOENT; 1218 goto out_css_put; 1219 } 1220 1221 /* 1222 * The caller is attempting to write out most of the target wb's 1223 * currently dirty pages. Size the work from the wb's reclaimable pages 1224 * and inflate the count by 25%, which should be large enough to flush 1225 * out most dirty pages while avoiding getting livelocked by concurrent 1226 * dirtiers. 1227 */ 1228 dirty = wb_stat_sum(wb, WB_RECLAIMABLE); 1229 dirty = dirty * 10 / 8; 1230 1231 /* issue the writeback work */ 1232 work = kzalloc_obj(*work, GFP_NOWAIT); 1233 if (work) { 1234 work->nr_pages = dirty; 1235 work->sync_mode = WB_SYNC_NONE; 1236 work->range_cyclic = 1; 1237 work->reason = reason; 1238 work->done = done; 1239 work->auto_free = 1; 1240 wb_queue_work(wb, work); 1241 ret = 0; 1242 } else { 1243 ret = -ENOMEM; 1244 } 1245 1246 wb_put(wb); 1247 out_css_put: 1248 css_put(memcg_css); 1249 out_bdi_put: 1250 bdi_put(bdi); 1251 return ret; 1252 } 1253 1254 /** 1255 * cgroup_writeback_umount - wait for in-flight inode wb switches on @sb 1256 * @sb: target super_block 1257 * 1258 * Wait until every inode wb switch that already passed the SB_ACTIVE 1259 * check on this superblock has been completed by the worker. Since 1260 * SB_ACTIVE is cleared before this is called, no new switches can start 1261 * for @sb, so s_isw_nr_in_flight will monotonically drop to zero. 1262 */ 1263 void cgroup_writeback_umount(struct super_block *sb) 1264 { 1265 if (!(sb->s_bdi->capabilities & BDI_CAP_WRITEBACK)) 1266 return; 1267 1268 /* 1269 * Pairs with smp_mb() in inode_prepare_wbs_switch(): we either observe 1270 * a non-zero counter and wait, or the switcher sees SB_ACTIVE clear 1271 * (cleared by generic_shutdown_super()) and bails before grabbing the 1272 * inode. 1273 */ 1274 smp_mb(); 1275 cgroup_writeback_drain(sb); 1276 } 1277 1278 static int __init cgroup_writeback_init(void) 1279 { 1280 isw_wq = alloc_workqueue("inode_switch_wbs", WQ_PERCPU, 0); 1281 if (!isw_wq) 1282 return -ENOMEM; 1283 return 0; 1284 } 1285 fs_initcall(cgroup_writeback_init); 1286 1287 #else /* CONFIG_CGROUP_WRITEBACK */ 1288 1289 static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi) { } 1290 static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi) { } 1291 1292 static void inode_cgwb_move_to_attached(struct inode *inode, 1293 struct bdi_writeback *wb) 1294 { 1295 assert_spin_locked(&wb->list_lock); 1296 assert_spin_locked(&inode->i_lock); 1297 WARN_ON_ONCE(inode_state_read(inode) & I_FREEING); 1298 1299 inode_state_clear(inode, I_SYNC_QUEUED); 1300 list_del_init(&inode->i_io_list); 1301 wb_io_lists_depopulated(wb); 1302 } 1303 1304 static struct bdi_writeback * 1305 locked_inode_to_wb_and_lock_list(struct inode *inode) 1306 __releases(&inode->i_lock) 1307 __acquires(&wb->list_lock) 1308 { 1309 struct bdi_writeback *wb = inode_to_wb(inode); 1310 1311 spin_unlock(&inode->i_lock); 1312 spin_lock(&wb->list_lock); 1313 return wb; 1314 } 1315 1316 static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) 1317 __acquires(&wb->list_lock) 1318 { 1319 struct bdi_writeback *wb = inode_to_wb(inode); 1320 1321 spin_lock(&wb->list_lock); 1322 return wb; 1323 } 1324 1325 static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 1326 { 1327 return nr_pages; 1328 } 1329 1330 static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 1331 struct wb_writeback_work *base_work, 1332 bool skip_if_busy) 1333 { 1334 might_sleep(); 1335 1336 if (!skip_if_busy || !writeback_in_progress(&bdi->wb)) { 1337 base_work->auto_free = 0; 1338 wb_queue_work(&bdi->wb, base_work); 1339 } 1340 } 1341 1342 static inline void wbc_attach_and_unlock_inode(struct writeback_control *wbc, 1343 struct inode *inode) 1344 __releases(&inode->i_lock) 1345 { 1346 spin_unlock(&inode->i_lock); 1347 } 1348 1349 #endif /* CONFIG_CGROUP_WRITEBACK */ 1350 1351 /* 1352 * Add in the number of potentially dirty inodes, because each inode 1353 * write can dirty pagecache in the underlying blockdev. 1354 */ 1355 static unsigned long get_nr_dirty_pages(void) 1356 { 1357 return global_node_page_state(NR_FILE_DIRTY) + 1358 get_nr_dirty_inodes(); 1359 } 1360 1361 static void wb_start_writeback(struct bdi_writeback *wb, enum wb_reason reason) 1362 { 1363 if (!wb_has_dirty_io(wb)) 1364 return; 1365 1366 /* 1367 * All callers of this function want to start writeback of all 1368 * dirty pages. Places like vmscan can call this at a very 1369 * high frequency, causing pointless allocations of tons of 1370 * work items and keeping the flusher threads busy retrieving 1371 * that work. Ensure that we only allow one of them pending and 1372 * inflight at the time. 1373 */ 1374 if (test_bit(WB_start_all, &wb->state) || 1375 test_and_set_bit(WB_start_all, &wb->state)) 1376 return; 1377 1378 wb->start_all_reason = reason; 1379 wb_wakeup(wb); 1380 } 1381 1382 /** 1383 * wb_start_background_writeback - start background writeback 1384 * @wb: bdi_writback to write from 1385 * 1386 * Description: 1387 * This makes sure WB_SYNC_NONE background writeback happens. When 1388 * this function returns, it is only guaranteed that for given wb 1389 * some IO is happening if we are over background dirty threshold. 1390 * Caller need not hold sb s_umount semaphore. 1391 */ 1392 void wb_start_background_writeback(struct bdi_writeback *wb) 1393 { 1394 /* 1395 * We just wake up the flusher thread. It will perform background 1396 * writeback as soon as there is no other work to do. 1397 */ 1398 trace_writeback_wake_background(wb); 1399 wb_wakeup(wb); 1400 } 1401 1402 /* 1403 * Remove the inode from the writeback list it is on. 1404 */ 1405 void inode_io_list_del(struct inode *inode) 1406 { 1407 struct bdi_writeback *wb; 1408 1409 /* 1410 * FIXME: ext4 can call here from ext4_evict_inode() after evict() already 1411 * unlinked the inode. 1412 */ 1413 if (list_empty_careful(&inode->i_io_list)) 1414 return; 1415 1416 wb = inode_to_wb_and_lock_list(inode); 1417 spin_lock(&inode->i_lock); 1418 1419 inode_state_clear(inode, I_SYNC_QUEUED); 1420 list_del_init(&inode->i_io_list); 1421 wb_io_lists_depopulated(wb); 1422 1423 spin_unlock(&inode->i_lock); 1424 spin_unlock(&wb->list_lock); 1425 } 1426 EXPORT_SYMBOL(inode_io_list_del); 1427 1428 /* 1429 * mark an inode as under writeback on the sb 1430 */ 1431 void sb_mark_inode_writeback(struct inode *inode) 1432 { 1433 struct super_block *sb = inode->i_sb; 1434 unsigned long flags; 1435 1436 if (list_empty(&inode->i_wb_list)) { 1437 spin_lock_irqsave(&sb->s_inode_wblist_lock, flags); 1438 if (list_empty(&inode->i_wb_list)) { 1439 list_add_tail(&inode->i_wb_list, &sb->s_inodes_wb); 1440 trace_sb_mark_inode_writeback(inode); 1441 } 1442 spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags); 1443 } 1444 } 1445 1446 /* 1447 * clear an inode as under writeback on the sb 1448 */ 1449 void sb_clear_inode_writeback(struct inode *inode) 1450 { 1451 struct super_block *sb = inode->i_sb; 1452 unsigned long flags; 1453 1454 if (!list_empty(&inode->i_wb_list)) { 1455 spin_lock_irqsave(&sb->s_inode_wblist_lock, flags); 1456 if (!list_empty(&inode->i_wb_list)) { 1457 list_del_init(&inode->i_wb_list); 1458 trace_sb_clear_inode_writeback(inode); 1459 } 1460 spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags); 1461 } 1462 } 1463 1464 /* 1465 * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 1466 * furthest end of its superblock's dirty-inode list. 1467 * 1468 * Before stamping the inode's ->dirtied_when, we check to see whether it is 1469 * already the most-recently-dirtied inode on the b_dirty list. If that is 1470 * the case then the inode must have been redirtied while it was being written 1471 * out and we don't reset its dirtied_when. 1472 */ 1473 static void redirty_tail_locked(struct inode *inode, struct bdi_writeback *wb) 1474 { 1475 assert_spin_locked(&inode->i_lock); 1476 1477 inode_state_clear(inode, I_SYNC_QUEUED); 1478 /* 1479 * When the inode is being freed just don't bother with dirty list 1480 * tracking. Flush worker will ignore this inode anyway and it will 1481 * trigger assertions in inode_io_list_move_locked(). 1482 */ 1483 if (inode_state_read(inode) & I_FREEING) { 1484 list_del_init(&inode->i_io_list); 1485 wb_io_lists_depopulated(wb); 1486 return; 1487 } 1488 if (!list_empty(&wb->b_dirty)) { 1489 struct inode *tail; 1490 1491 tail = wb_inode(wb->b_dirty.next); 1492 if (time_before(inode->dirtied_when, tail->dirtied_when)) 1493 inode->dirtied_when = jiffies; 1494 } 1495 inode_io_list_move_locked(inode, wb, &wb->b_dirty); 1496 } 1497 1498 static void redirty_tail(struct inode *inode, struct bdi_writeback *wb) 1499 { 1500 spin_lock(&inode->i_lock); 1501 redirty_tail_locked(inode, wb); 1502 spin_unlock(&inode->i_lock); 1503 } 1504 1505 /* 1506 * requeue inode for re-scanning after bdi->b_io list is exhausted. 1507 */ 1508 static void requeue_io(struct inode *inode, struct bdi_writeback *wb) 1509 { 1510 inode_io_list_move_locked(inode, wb, &wb->b_more_io); 1511 } 1512 1513 static void inode_sync_complete(struct inode *inode) 1514 { 1515 assert_spin_locked(&inode->i_lock); 1516 1517 inode_state_clear(inode, I_SYNC); 1518 /* If inode is clean an unused, put it into LRU now... */ 1519 inode_lru_list_add(inode); 1520 /* Called with inode->i_lock which ensures memory ordering. */ 1521 inode_wake_up_bit(inode, __I_SYNC); 1522 } 1523 1524 static bool inode_dirtied_after(struct inode *inode, unsigned long t) 1525 { 1526 bool ret = time_after(inode->dirtied_when, t); 1527 #ifndef CONFIG_64BIT 1528 /* 1529 * For inodes being constantly redirtied, dirtied_when can get stuck. 1530 * It _appears_ to be in the future, but is actually in distant past. 1531 * This test is necessary to prevent such wrapped-around relative times 1532 * from permanently stopping the whole bdi writeback. 1533 */ 1534 ret = ret && time_before_eq(inode->dirtied_when, jiffies); 1535 #endif 1536 return ret; 1537 } 1538 1539 /* 1540 * Move expired (dirtied before dirtied_before) dirty inodes from 1541 * @delaying_queue to @dispatch_queue. 1542 */ 1543 static int move_expired_inodes(struct list_head *delaying_queue, 1544 struct list_head *dispatch_queue, 1545 unsigned long dirtied_before) 1546 { 1547 LIST_HEAD(tmp); 1548 struct list_head *pos, *node; 1549 struct super_block *sb = NULL; 1550 struct inode *inode; 1551 int do_sb_sort = 0; 1552 int moved = 0; 1553 1554 while (!list_empty(delaying_queue)) { 1555 inode = wb_inode(delaying_queue->prev); 1556 if (inode_dirtied_after(inode, dirtied_before)) 1557 break; 1558 spin_lock(&inode->i_lock); 1559 list_move(&inode->i_io_list, &tmp); 1560 moved++; 1561 inode_state_set(inode, I_SYNC_QUEUED); 1562 spin_unlock(&inode->i_lock); 1563 if (sb_is_blkdev_sb(inode->i_sb)) 1564 continue; 1565 if (sb && sb != inode->i_sb) 1566 do_sb_sort = 1; 1567 sb = inode->i_sb; 1568 } 1569 1570 /* just one sb in list, splice to dispatch_queue and we're done */ 1571 if (!do_sb_sort) { 1572 list_splice(&tmp, dispatch_queue); 1573 goto out; 1574 } 1575 1576 /* 1577 * Although inode's i_io_list is moved from 'tmp' to 'dispatch_queue', 1578 * we don't take inode->i_lock here because it is just a pointless overhead. 1579 * Inode is already marked as I_SYNC_QUEUED so writeback list handling is 1580 * fully under our control. 1581 */ 1582 while (!list_empty(&tmp)) { 1583 sb = wb_inode(tmp.prev)->i_sb; 1584 list_for_each_prev_safe(pos, node, &tmp) { 1585 inode = wb_inode(pos); 1586 if (inode->i_sb == sb) 1587 list_move(&inode->i_io_list, dispatch_queue); 1588 } 1589 } 1590 out: 1591 return moved; 1592 } 1593 1594 /* 1595 * Queue all expired dirty inodes for io, eldest first. 1596 * Before 1597 * newly dirtied b_dirty b_io b_more_io 1598 * =============> gf edc BA 1599 * After 1600 * newly dirtied b_dirty b_io b_more_io 1601 * =============> g fBAedc 1602 * | 1603 * +--> dequeue for IO 1604 */ 1605 static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work, 1606 unsigned long dirtied_before) 1607 { 1608 int moved; 1609 unsigned long time_expire_jif = dirtied_before; 1610 1611 assert_spin_locked(&wb->list_lock); 1612 list_splice_init(&wb->b_more_io, &wb->b_io); 1613 moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, dirtied_before); 1614 if (!work->for_sync) 1615 time_expire_jif = jiffies - dirtytime_expire_interval * HZ; 1616 moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io, 1617 time_expire_jif); 1618 if (moved) 1619 wb_io_lists_populated(wb); 1620 trace_writeback_queue_io(wb, work, dirtied_before, moved); 1621 } 1622 1623 static int write_inode(struct inode *inode, struct writeback_control *wbc) 1624 { 1625 int ret; 1626 1627 if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) { 1628 trace_writeback_write_inode_start(inode, wbc); 1629 ret = inode->i_sb->s_op->write_inode(inode, wbc); 1630 trace_writeback_write_inode(inode, wbc); 1631 return ret; 1632 } 1633 return 0; 1634 } 1635 1636 /* 1637 * Wait for writeback on an inode to complete. Called with i_lock held. 1638 * Caller must make sure inode cannot go away when we drop i_lock. 1639 */ 1640 void inode_wait_for_writeback(struct inode *inode) 1641 { 1642 struct wait_bit_queue_entry wqe; 1643 struct wait_queue_head *wq_head; 1644 1645 assert_spin_locked(&inode->i_lock); 1646 1647 if (!(inode_state_read(inode) & I_SYNC)) 1648 return; 1649 1650 wq_head = inode_bit_waitqueue(&wqe, inode, __I_SYNC); 1651 for (;;) { 1652 prepare_to_wait_event(wq_head, &wqe.wq_entry, TASK_UNINTERRUPTIBLE); 1653 /* Checking I_SYNC with inode->i_lock guarantees memory ordering. */ 1654 if (!(inode_state_read(inode) & I_SYNC)) 1655 break; 1656 spin_unlock(&inode->i_lock); 1657 schedule(); 1658 spin_lock(&inode->i_lock); 1659 } 1660 finish_wait(wq_head, &wqe.wq_entry); 1661 } 1662 1663 /* 1664 * Sleep until I_SYNC is cleared. This function must be called with i_lock 1665 * held and drops it. It is aimed for callers not holding any inode reference 1666 * so once i_lock is dropped, inode can go away. 1667 */ 1668 static void inode_sleep_on_writeback(struct inode *inode) 1669 __releases(inode->i_lock) 1670 { 1671 struct wait_bit_queue_entry wqe; 1672 struct wait_queue_head *wq_head; 1673 bool sleep; 1674 1675 assert_spin_locked(&inode->i_lock); 1676 1677 wq_head = inode_bit_waitqueue(&wqe, inode, __I_SYNC); 1678 prepare_to_wait_event(wq_head, &wqe.wq_entry, TASK_UNINTERRUPTIBLE); 1679 /* Checking I_SYNC with inode->i_lock guarantees memory ordering. */ 1680 sleep = !!(inode_state_read(inode) & I_SYNC); 1681 spin_unlock(&inode->i_lock); 1682 if (sleep) 1683 schedule(); 1684 finish_wait(wq_head, &wqe.wq_entry); 1685 } 1686 1687 /* 1688 * Find proper writeback list for the inode depending on its current state and 1689 * possibly also change of its state while we were doing writeback. Here we 1690 * handle things such as livelock prevention or fairness of writeback among 1691 * inodes. This function can be called only by flusher thread - noone else 1692 * processes all inodes in writeback lists and requeueing inodes behind flusher 1693 * thread's back can have unexpected consequences. 1694 */ 1695 static void requeue_inode(struct inode *inode, struct bdi_writeback *wb, 1696 struct writeback_control *wbc, 1697 unsigned long dirtied_before) 1698 { 1699 if (inode_state_read(inode) & I_FREEING) 1700 return; 1701 1702 /* 1703 * Sync livelock prevention. Each inode is tagged and synced in one 1704 * shot. If still dirty, it will be redirty_tail()'ed below. Update 1705 * the dirty time to prevent enqueue and sync it again. 1706 */ 1707 if ((inode_state_read(inode) & I_DIRTY) && 1708 (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) 1709 inode->dirtied_when = jiffies; 1710 1711 if (wbc->pages_skipped) { 1712 /* 1713 * Writeback is not making progress due to locked buffers. 1714 * Skip this inode for now. Although having skipped pages 1715 * is odd for clean inodes, it can happen for some 1716 * filesystems so handle that gracefully. 1717 */ 1718 if (inode_state_read(inode) & I_DIRTY_ALL) 1719 redirty_tail_locked(inode, wb); 1720 else 1721 inode_cgwb_move_to_attached(inode, wb); 1722 return; 1723 } 1724 1725 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { 1726 /* 1727 * We didn't write back all the pages. nfs_writepages() 1728 * sometimes bales out without doing anything. 1729 */ 1730 if (wbc->nr_to_write <= 0 && 1731 !inode_dirtied_after(inode, dirtied_before)) { 1732 /* Slice used up. Queue for next turn. */ 1733 requeue_io(inode, wb); 1734 } else { 1735 /* 1736 * Writeback blocked by something other than 1737 * congestion. Delay the inode for some time to 1738 * avoid spinning on the CPU (100% iowait) 1739 * retrying writeback of the dirty page/inode 1740 * that cannot be performed immediately. 1741 */ 1742 redirty_tail_locked(inode, wb); 1743 } 1744 } else if (inode_state_read(inode) & I_DIRTY) { 1745 /* 1746 * Filesystems can dirty the inode during writeback operations, 1747 * such as delayed allocation during submission or metadata 1748 * updates after data IO completion. 1749 */ 1750 redirty_tail_locked(inode, wb); 1751 } else if (inode_state_read(inode) & I_DIRTY_TIME) { 1752 inode->dirtied_when = jiffies; 1753 inode_io_list_move_locked(inode, wb, &wb->b_dirty_time); 1754 inode_state_clear(inode, I_SYNC_QUEUED); 1755 } else { 1756 /* The inode is clean. Remove from writeback lists. */ 1757 inode_cgwb_move_to_attached(inode, wb); 1758 } 1759 } 1760 1761 static bool __sync_lazytime(struct inode *inode) 1762 { 1763 spin_lock(&inode->i_lock); 1764 if (!(inode_state_read(inode) & I_DIRTY_TIME)) { 1765 spin_unlock(&inode->i_lock); 1766 return false; 1767 } 1768 inode_state_clear(inode, I_DIRTY_TIME); 1769 spin_unlock(&inode->i_lock); 1770 inode->i_op->sync_lazytime(inode); 1771 return true; 1772 } 1773 1774 bool sync_lazytime(struct inode *inode) 1775 { 1776 if (!(inode_state_read_once(inode) & I_DIRTY_TIME)) 1777 return false; 1778 1779 trace_writeback_lazytime(inode); 1780 if (inode->i_op->sync_lazytime) 1781 return __sync_lazytime(inode); 1782 mark_inode_dirty_sync(inode); 1783 return true; 1784 } 1785 1786 /* 1787 * Write out an inode and its dirty pages (or some of its dirty pages, depending 1788 * on @wbc->nr_to_write), and clear the relevant dirty flags from i_state. 1789 * 1790 * This doesn't remove the inode from the writeback list it is on, except 1791 * potentially to move it from b_dirty_time to b_dirty due to timestamp 1792 * expiration. The caller is otherwise responsible for writeback list handling. 1793 * 1794 * The caller is also responsible for setting the I_SYNC flag beforehand and 1795 * calling inode_sync_complete() to clear it afterwards. 1796 */ 1797 static int 1798 __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 1799 { 1800 struct address_space *mapping = inode->i_mapping; 1801 long nr_to_write = wbc->nr_to_write; 1802 unsigned dirty; 1803 int ret; 1804 1805 WARN_ON(!(inode_state_read_once(inode) & I_SYNC)); 1806 1807 trace_writeback_single_inode_start(inode, wbc, nr_to_write); 1808 1809 ret = do_writepages(mapping, wbc); 1810 1811 /* 1812 * Make sure to wait on the data before writing out the metadata. 1813 * This is important for filesystems that modify metadata on data 1814 * I/O completion. We don't do it for sync(2) writeback because it has a 1815 * separate, external IO completion path and ->sync_fs for guaranteeing 1816 * inode metadata is written back correctly. 1817 */ 1818 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) { 1819 int err = filemap_fdatawait(mapping); 1820 if (ret == 0) 1821 ret = err; 1822 } 1823 1824 /* 1825 * For data integrity writeback, or when the dirty interval expired, 1826 * ask the file system to propagata lazy timestamp updates into real 1827 * dirty state. 1828 */ 1829 if ((inode_state_read_once(inode) & I_DIRTY_TIME) && 1830 (wbc->sync_mode == WB_SYNC_ALL || 1831 time_after(jiffies, inode->dirtied_time_when + 1832 dirtytime_expire_interval * HZ))) 1833 sync_lazytime(inode); 1834 1835 /* 1836 * Get and clear the dirty flags from i_state. This needs to be done 1837 * after calling writepages because some filesystems may redirty the 1838 * inode during writepages due to delalloc. It also needs to be done 1839 * after handling timestamp expiration, as that may dirty the inode too. 1840 */ 1841 spin_lock(&inode->i_lock); 1842 dirty = inode_state_read(inode) & I_DIRTY; 1843 inode_state_clear(inode, dirty); 1844 1845 /* 1846 * Paired with smp_mb() in __mark_inode_dirty(). This allows 1847 * __mark_inode_dirty() to test i_state without grabbing i_lock - 1848 * either they see the I_DIRTY bits cleared or we see the dirtied 1849 * inode. 1850 * 1851 * I_DIRTY_PAGES is always cleared together above even if @mapping 1852 * still has dirty pages. The flag is reinstated after smp_mb() if 1853 * necessary. This guarantees that either __mark_inode_dirty() 1854 * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY. 1855 */ 1856 smp_mb(); 1857 1858 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 1859 inode_state_set(inode, I_DIRTY_PAGES); 1860 else if (unlikely(inode_state_read(inode) & I_PINNING_NETFS_WB)) { 1861 if (!(inode_state_read(inode) & I_DIRTY_PAGES)) { 1862 inode_state_clear(inode, I_PINNING_NETFS_WB); 1863 wbc->unpinned_netfs_wb = true; 1864 dirty |= I_PINNING_NETFS_WB; /* Cause write_inode */ 1865 } 1866 } 1867 1868 spin_unlock(&inode->i_lock); 1869 1870 /* Don't write the inode if only I_DIRTY_PAGES was set */ 1871 if (dirty & ~I_DIRTY_PAGES) { 1872 int err = write_inode(inode, wbc); 1873 if (ret == 0) 1874 ret = err; 1875 } 1876 1877 /* 1878 * Do we need to wait for inode metadata IO possibly submitted 1879 * by previous WB_SYNC_NONE writeback? 1880 */ 1881 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync && 1882 inode_state_read_once(inode) & I_METADATA_WRITEBACK) { 1883 int err; 1884 1885 spin_lock(&inode->i_lock); 1886 inode_state_clear(inode, I_METADATA_WRITEBACK); 1887 spin_unlock(&inode->i_lock); 1888 err = inode->i_sb->s_op->sync_inode_metadata(inode, wbc); 1889 if (ret == 0) 1890 ret = err; 1891 } 1892 wbc->unpinned_netfs_wb = false; 1893 trace_writeback_single_inode(inode, wbc, nr_to_write); 1894 return ret; 1895 } 1896 1897 /* 1898 * Write out an inode's dirty data and metadata on-demand, i.e. separately from 1899 * the regular batched writeback done by the flusher threads in 1900 * writeback_sb_inodes(). @wbc controls various aspects of the write, such as 1901 * whether it is a data-integrity sync (%WB_SYNC_ALL) or not (%WB_SYNC_NONE). 1902 * 1903 * To prevent the inode from going away, either the caller must have a reference 1904 * to the inode, or the inode must have I_WILL_FREE or I_FREEING set. 1905 */ 1906 static int writeback_single_inode(struct inode *inode, 1907 struct writeback_control *wbc) 1908 { 1909 struct bdi_writeback *wb; 1910 int ret = 0; 1911 1912 spin_lock(&inode->i_lock); 1913 if (!icount_read(inode)) 1914 WARN_ON(!(inode_state_read(inode) & (I_WILL_FREE | I_FREEING))); 1915 else 1916 WARN_ON(inode_state_read(inode) & I_WILL_FREE); 1917 1918 if (inode_state_read(inode) & I_SYNC) { 1919 /* 1920 * Writeback is already running on the inode. For WB_SYNC_NONE, 1921 * that's enough and we can just return. For WB_SYNC_ALL, we 1922 * must wait for the existing writeback to complete, then do 1923 * writeback again if there's anything left. 1924 */ 1925 if (wbc->sync_mode != WB_SYNC_ALL) 1926 goto out; 1927 inode_wait_for_writeback(inode); 1928 } 1929 WARN_ON(inode_state_read(inode) & I_SYNC); 1930 /* 1931 * If the inode is already fully clean, then there's nothing to do. 1932 * 1933 * For data-integrity syncs we also need to check whether any folios or 1934 * metadata are still under writeback, e.g. due to prior WB_SYNC_NONE 1935 * writeback. If there, we'll need to wait for them. 1936 */ 1937 if (!(inode_state_read(inode) & I_DIRTY_ALL)) { 1938 if (wbc->sync_mode != WB_SYNC_ALL) 1939 goto out; 1940 if (!mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK) && 1941 !(inode_state_read(inode) & I_METADATA_WRITEBACK)) 1942 goto out; 1943 } 1944 inode_state_set(inode, I_SYNC); 1945 wbc_attach_and_unlock_inode(wbc, inode); 1946 1947 ret = __writeback_single_inode(inode, wbc); 1948 1949 wbc_detach_inode(wbc); 1950 1951 wb = inode_to_wb_and_lock_list(inode); 1952 spin_lock(&inode->i_lock); 1953 /* 1954 * If the inode is freeing, its i_io_list shoudn't be updated 1955 * as it can be finally deleted at this moment. 1956 */ 1957 if (!(inode_state_read(inode) & I_FREEING)) { 1958 /* 1959 * If the inode is now fully clean, then it can be safely 1960 * removed from its writeback list (if any). Otherwise the 1961 * flusher threads are responsible for the writeback lists. 1962 */ 1963 if (!(inode_state_read(inode) & I_DIRTY_ALL)) 1964 inode_cgwb_move_to_attached(inode, wb); 1965 else if (!(inode_state_read(inode) & I_SYNC_QUEUED)) { 1966 if ((inode_state_read(inode) & I_DIRTY)) 1967 redirty_tail_locked(inode, wb); 1968 else if (inode_state_read(inode) & I_DIRTY_TIME) { 1969 inode->dirtied_when = jiffies; 1970 inode_io_list_move_locked(inode, 1971 wb, 1972 &wb->b_dirty_time); 1973 } 1974 } 1975 } 1976 1977 spin_unlock(&wb->list_lock); 1978 inode_sync_complete(inode); 1979 out: 1980 spin_unlock(&inode->i_lock); 1981 return ret; 1982 } 1983 1984 static long writeback_chunk_size(struct super_block *sb, 1985 struct bdi_writeback *wb, struct wb_writeback_work *work) 1986 { 1987 long pages; 1988 1989 /* 1990 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty 1991 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX 1992 * here avoids calling into writeback_inodes_wb() more than once. 1993 * 1994 * The intended call sequence for WB_SYNC_ALL writeback is: 1995 * 1996 * wb_writeback() 1997 * writeback_sb_inodes() <== called only once 1998 * write_cache_pages() <== called once for each inode 1999 * (quickly) tag currently dirty pages 2000 * (maybe slowly) sync all tagged pages 2001 */ 2002 if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) 2003 return LONG_MAX; 2004 2005 pages = min(wb->avg_write_bandwidth / 2, 2006 global_wb_domain.dirty_limit / DIRTY_SCOPE); 2007 pages = min(pages, work->nr_pages); 2008 return round_down(pages + sb->s_min_writeback_pages, 2009 sb->s_min_writeback_pages); 2010 } 2011 2012 /* 2013 * Write a portion of b_io inodes which belong to @sb. 2014 * 2015 * Return the number of pages and/or inodes written. 2016 * 2017 * NOTE! This is called with wb->list_lock held, and will 2018 * unlock and relock that for each inode it ends up doing 2019 * IO for. 2020 */ 2021 static long writeback_sb_inodes(struct super_block *sb, 2022 struct bdi_writeback *wb, 2023 struct wb_writeback_work *work) 2024 { 2025 struct writeback_control wbc = { 2026 .sync_mode = work->sync_mode, 2027 .tagged_writepages = work->tagged_writepages, 2028 .for_kupdate = work->for_kupdate, 2029 .for_background = work->for_background, 2030 .for_sync = work->for_sync, 2031 .range_cyclic = work->range_cyclic, 2032 .range_start = 0, 2033 .range_end = LLONG_MAX, 2034 }; 2035 unsigned long start_time = jiffies; 2036 unsigned long timeout = sysctl_hung_task_timeout_secs; 2037 long write_chunk; 2038 long total_wrote = 0; /* count both pages and inodes */ 2039 unsigned long dirtied_before = jiffies; 2040 2041 if (work->for_kupdate) 2042 dirtied_before = jiffies - 2043 msecs_to_jiffies(dirty_expire_interval * 10); 2044 2045 while (!list_empty(&wb->b_io)) { 2046 struct inode *inode = wb_inode(wb->b_io.prev); 2047 struct bdi_writeback *tmp_wb; 2048 long wrote; 2049 2050 if (inode->i_sb != sb) { 2051 if (work->sb) { 2052 /* 2053 * We only want to write back data for this 2054 * superblock, move all inodes not belonging 2055 * to it back onto the dirty list. 2056 */ 2057 redirty_tail(inode, wb); 2058 continue; 2059 } 2060 2061 /* 2062 * The inode belongs to a different superblock. 2063 * Bounce back to the caller to unpin this and 2064 * pin the next superblock. 2065 */ 2066 break; 2067 } 2068 2069 /* 2070 * Don't bother with new inodes or inodes being freed, first 2071 * kind does not need periodic writeout yet, and for the latter 2072 * kind writeout is handled by the freer. 2073 */ 2074 spin_lock(&inode->i_lock); 2075 if (inode_state_read(inode) & (I_NEW | I_FREEING | I_WILL_FREE)) { 2076 redirty_tail_locked(inode, wb); 2077 spin_unlock(&inode->i_lock); 2078 continue; 2079 } 2080 if ((inode_state_read(inode) & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) { 2081 /* 2082 * If this inode is locked for writeback and we are not 2083 * doing writeback-for-data-integrity, move it to 2084 * b_more_io so that writeback can proceed with the 2085 * other inodes on s_io. 2086 * 2087 * We'll have another go at writing back this inode 2088 * when we completed a full scan of b_io. 2089 */ 2090 requeue_io(inode, wb); 2091 spin_unlock(&inode->i_lock); 2092 trace_writeback_sb_inodes_requeue(inode); 2093 continue; 2094 } 2095 spin_unlock(&wb->list_lock); 2096 2097 /* 2098 * We already requeued the inode if it had I_SYNC set and we 2099 * are doing WB_SYNC_NONE writeback. So this catches only the 2100 * WB_SYNC_ALL case. 2101 */ 2102 if (inode_state_read(inode) & I_SYNC) { 2103 /* Wait for I_SYNC. This function drops i_lock... */ 2104 inode_sleep_on_writeback(inode); 2105 /* Inode may be gone, start again */ 2106 spin_lock(&wb->list_lock); 2107 continue; 2108 } 2109 inode_state_set(inode, I_SYNC); 2110 wbc_attach_and_unlock_inode(&wbc, inode); 2111 2112 write_chunk = writeback_chunk_size(inode->i_sb, wb, work); 2113 wbc.nr_to_write = write_chunk; 2114 wbc.pages_skipped = 0; 2115 2116 /* 2117 * We use I_SYNC to pin the inode in memory. While it is set 2118 * evict_inode() will wait so the inode cannot be freed. 2119 */ 2120 __writeback_single_inode(inode, &wbc); 2121 2122 /* Report progress to inform the hung task detector of the progress. */ 2123 if (work->done && work->done->progress_stamp && timeout && 2124 (jiffies - work->done->progress_stamp) > HZ * timeout / 2) 2125 wake_up_all(work->done->waitq); 2126 2127 wbc_detach_inode(&wbc); 2128 work->nr_pages -= write_chunk - wbc.nr_to_write; 2129 wrote = write_chunk - wbc.nr_to_write - wbc.pages_skipped; 2130 wrote = wrote < 0 ? 0 : wrote; 2131 total_wrote += wrote; 2132 2133 if (need_resched()) { 2134 /* 2135 * We're trying to balance between building up a nice 2136 * long list of IOs to improve our merge rate, and 2137 * getting those IOs out quickly for anyone throttling 2138 * in balance_dirty_pages(). cond_resched() doesn't 2139 * unplug, so get our IOs out the door before we 2140 * give up the CPU. 2141 */ 2142 blk_flush_plug(current->plug, false); 2143 cond_resched(); 2144 } 2145 2146 /* 2147 * Requeue @inode if still dirty. Be careful as @inode may 2148 * have been switched to another wb in the meantime. 2149 */ 2150 tmp_wb = inode_to_wb_and_lock_list(inode); 2151 spin_lock(&inode->i_lock); 2152 if (!(inode_state_read(inode) & I_DIRTY_ALL)) 2153 total_wrote++; 2154 requeue_inode(inode, tmp_wb, &wbc, dirtied_before); 2155 inode_sync_complete(inode); 2156 spin_unlock(&inode->i_lock); 2157 2158 if (unlikely(tmp_wb != wb)) { 2159 spin_unlock(&tmp_wb->list_lock); 2160 spin_lock(&wb->list_lock); 2161 } 2162 2163 /* 2164 * bail out to wb_writeback() often enough to check 2165 * background threshold and other termination conditions. 2166 */ 2167 if (total_wrote) { 2168 if (time_is_before_jiffies(start_time + HZ / 10UL)) 2169 break; 2170 if (work->nr_pages <= 0) 2171 break; 2172 } 2173 } 2174 return total_wrote; 2175 } 2176 2177 static long __writeback_inodes_wb(struct bdi_writeback *wb, 2178 struct wb_writeback_work *work) 2179 { 2180 unsigned long start_time = jiffies; 2181 long wrote = 0; 2182 2183 while (!list_empty(&wb->b_io)) { 2184 struct inode *inode = wb_inode(wb->b_io.prev); 2185 struct super_block *sb = inode->i_sb; 2186 2187 if (!super_trylock_shared(sb)) { 2188 /* 2189 * super_trylock_shared() may fail consistently due to 2190 * s_umount being grabbed by someone else. Don't use 2191 * requeue_io() to avoid busy retrying the inode/sb. 2192 */ 2193 redirty_tail(inode, wb); 2194 continue; 2195 } 2196 wrote += writeback_sb_inodes(sb, wb, work); 2197 up_read(&sb->s_umount); 2198 2199 /* refer to the same tests at the end of writeback_sb_inodes */ 2200 if (wrote) { 2201 if (time_is_before_jiffies(start_time + HZ / 10UL)) 2202 break; 2203 if (work->nr_pages <= 0) 2204 break; 2205 } 2206 } 2207 /* Leave any unwritten inodes on b_io */ 2208 return wrote; 2209 } 2210 2211 static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages, 2212 enum wb_reason reason) 2213 { 2214 struct wb_writeback_work work = { 2215 .nr_pages = nr_pages, 2216 .sync_mode = WB_SYNC_NONE, 2217 .range_cyclic = 1, 2218 .reason = reason, 2219 }; 2220 struct blk_plug plug; 2221 2222 blk_start_plug(&plug); 2223 spin_lock(&wb->list_lock); 2224 if (list_empty(&wb->b_io)) 2225 queue_io(wb, &work, jiffies); 2226 __writeback_inodes_wb(wb, &work); 2227 spin_unlock(&wb->list_lock); 2228 blk_finish_plug(&plug); 2229 2230 return nr_pages - work.nr_pages; 2231 } 2232 2233 /* 2234 * Explicit flushing or periodic writeback of "old" data. 2235 * 2236 * Define "old": the first time one of an inode's pages is dirtied, we mark the 2237 * dirtying-time in the inode's address_space. So this periodic writeback code 2238 * just walks the superblock inode list, writing back any inodes which are 2239 * older than a specific point in time. 2240 * 2241 * Try to run once per dirty_writeback_interval. But if a writeback event 2242 * takes longer than a dirty_writeback_interval interval, then leave a 2243 * one-second gap. 2244 * 2245 * dirtied_before takes precedence over nr_to_write. So we'll only write back 2246 * all dirty pages if they are all attached to "old" mappings. 2247 */ 2248 static long wb_writeback(struct bdi_writeback *wb, 2249 struct wb_writeback_work *work) 2250 { 2251 long nr_pages = work->nr_pages; 2252 unsigned long dirtied_before = jiffies; 2253 struct inode *inode; 2254 long progress; 2255 struct blk_plug plug; 2256 bool queued = false; 2257 2258 blk_start_plug(&plug); 2259 for (;;) { 2260 /* 2261 * Stop writeback when nr_pages has been consumed 2262 */ 2263 if (work->nr_pages <= 0) 2264 break; 2265 2266 /* 2267 * Background writeout and kupdate-style writeback may 2268 * run forever. Stop them if there is other work to do 2269 * so that e.g. sync can proceed. They'll be restarted 2270 * after the other works are all done. 2271 */ 2272 if ((work->for_background || work->for_kupdate) && 2273 !list_empty(&wb->work_list)) 2274 break; 2275 2276 /* 2277 * For background writeout, stop when we are below the 2278 * background dirty threshold 2279 */ 2280 if (work->for_background && !wb_over_bg_thresh(wb)) 2281 break; 2282 2283 2284 spin_lock(&wb->list_lock); 2285 2286 trace_writeback_start(wb, work); 2287 if (list_empty(&wb->b_io)) { 2288 /* 2289 * Kupdate and background works are special and we want 2290 * to include all inodes that need writing. Livelock 2291 * avoidance is handled by these works yielding to any 2292 * other work so we are safe. 2293 */ 2294 if (work->for_kupdate) { 2295 dirtied_before = jiffies - 2296 msecs_to_jiffies(dirty_expire_interval * 2297 10); 2298 } else if (work->for_background) 2299 dirtied_before = jiffies; 2300 2301 queue_io(wb, work, dirtied_before); 2302 queued = true; 2303 } 2304 if (work->sb) 2305 progress = writeback_sb_inodes(work->sb, wb, work); 2306 else 2307 progress = __writeback_inodes_wb(wb, work); 2308 trace_writeback_written(wb, work); 2309 2310 /* 2311 * Did we write something? Try for more 2312 * 2313 * Dirty inodes are moved to b_io for writeback in batches. 2314 * The completion of the current batch does not necessarily 2315 * mean the overall work is done. So we keep looping as long 2316 * as made some progress on cleaning pages or inodes. 2317 */ 2318 if (progress || !queued) { 2319 spin_unlock(&wb->list_lock); 2320 continue; 2321 } 2322 2323 /* 2324 * No more inodes for IO, bail 2325 */ 2326 if (list_empty(&wb->b_more_io)) { 2327 spin_unlock(&wb->list_lock); 2328 break; 2329 } 2330 2331 /* 2332 * Nothing written. Wait for some inode to 2333 * become available for writeback. Otherwise 2334 * we'll just busyloop. 2335 */ 2336 trace_writeback_wait(wb, work); 2337 inode = wb_inode(wb->b_more_io.prev); 2338 spin_lock(&inode->i_lock); 2339 spin_unlock(&wb->list_lock); 2340 /* This function drops i_lock... */ 2341 inode_sleep_on_writeback(inode); 2342 } 2343 blk_finish_plug(&plug); 2344 2345 return nr_pages - work->nr_pages; 2346 } 2347 2348 /* 2349 * Return the next wb_writeback_work struct that hasn't been processed yet. 2350 */ 2351 static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb) 2352 { 2353 struct wb_writeback_work *work = NULL; 2354 2355 spin_lock_irq(&wb->work_lock); 2356 if (!list_empty(&wb->work_list)) { 2357 work = list_entry(wb->work_list.next, 2358 struct wb_writeback_work, list); 2359 list_del_init(&work->list); 2360 } 2361 spin_unlock_irq(&wb->work_lock); 2362 return work; 2363 } 2364 2365 static long wb_check_background_flush(struct bdi_writeback *wb) 2366 { 2367 if (wb_over_bg_thresh(wb)) { 2368 2369 struct wb_writeback_work work = { 2370 .nr_pages = LONG_MAX, 2371 .sync_mode = WB_SYNC_NONE, 2372 .for_background = 1, 2373 .range_cyclic = 1, 2374 .reason = WB_REASON_BACKGROUND, 2375 }; 2376 2377 return wb_writeback(wb, &work); 2378 } 2379 2380 return 0; 2381 } 2382 2383 static long wb_check_old_data_flush(struct bdi_writeback *wb) 2384 { 2385 unsigned long expired; 2386 long nr_pages; 2387 2388 /* 2389 * When set to zero, disable periodic writeback 2390 */ 2391 if (!dirty_writeback_interval) 2392 return 0; 2393 2394 expired = wb->last_old_flush + 2395 msecs_to_jiffies(dirty_writeback_interval * 10); 2396 if (time_before(jiffies, expired)) 2397 return 0; 2398 2399 wb->last_old_flush = jiffies; 2400 nr_pages = get_nr_dirty_pages(); 2401 2402 if (nr_pages) { 2403 struct wb_writeback_work work = { 2404 .nr_pages = nr_pages, 2405 .sync_mode = WB_SYNC_NONE, 2406 .for_kupdate = 1, 2407 .range_cyclic = 1, 2408 .reason = WB_REASON_PERIODIC, 2409 }; 2410 2411 return wb_writeback(wb, &work); 2412 } 2413 2414 return 0; 2415 } 2416 2417 static long wb_check_start_all(struct bdi_writeback *wb) 2418 { 2419 long nr_pages; 2420 2421 if (!test_bit(WB_start_all, &wb->state)) 2422 return 0; 2423 2424 nr_pages = get_nr_dirty_pages(); 2425 if (nr_pages) { 2426 struct wb_writeback_work work = { 2427 .nr_pages = wb_split_bdi_pages(wb, nr_pages), 2428 .sync_mode = WB_SYNC_NONE, 2429 .range_cyclic = 1, 2430 .reason = wb->start_all_reason, 2431 }; 2432 2433 nr_pages = wb_writeback(wb, &work); 2434 } 2435 2436 clear_bit(WB_start_all, &wb->state); 2437 return nr_pages; 2438 } 2439 2440 static long wb_check_start_dontcache(struct bdi_writeback *wb) 2441 { 2442 long nr_pages; 2443 2444 if (!test_and_clear_bit(WB_start_dontcache, &wb->state)) 2445 return 0; 2446 2447 nr_pages = wb_stat_sum(wb, WB_DONTCACHE_DIRTY); 2448 if (nr_pages) { 2449 struct wb_writeback_work work = { 2450 .nr_pages = nr_pages, 2451 .sync_mode = WB_SYNC_NONE, 2452 .range_cyclic = 1, 2453 .reason = WB_REASON_DONTCACHE, 2454 }; 2455 2456 nr_pages = wb_writeback(wb, &work); 2457 } 2458 2459 return nr_pages; 2460 } 2461 2462 /* 2463 * Retrieve work items and do the writeback they describe 2464 */ 2465 static long wb_do_writeback(struct bdi_writeback *wb) 2466 { 2467 struct wb_writeback_work *work; 2468 long wrote = 0; 2469 2470 set_bit(WB_writeback_running, &wb->state); 2471 while ((work = get_next_work_item(wb)) != NULL) { 2472 trace_writeback_exec(wb, work); 2473 wrote += wb_writeback(wb, work); 2474 finish_writeback_work(work); 2475 } 2476 2477 /* 2478 * Check for a flush-everything request 2479 */ 2480 wrote += wb_check_start_all(wb); 2481 2482 /* 2483 * Check for dontcache writeback request 2484 */ 2485 wrote += wb_check_start_dontcache(wb); 2486 2487 /* 2488 * Check for periodic writeback, kupdated() style 2489 */ 2490 wrote += wb_check_old_data_flush(wb); 2491 wrote += wb_check_background_flush(wb); 2492 clear_bit(WB_writeback_running, &wb->state); 2493 2494 return wrote; 2495 } 2496 2497 /* 2498 * Handle writeback of dirty data for the device backed by this bdi. Also 2499 * reschedules periodically and does kupdated style flushing. 2500 */ 2501 void wb_workfn(struct work_struct *work) 2502 { 2503 struct bdi_writeback *wb = container_of(to_delayed_work(work), 2504 struct bdi_writeback, dwork); 2505 long pages_written; 2506 2507 set_worker_desc("flush-%s", bdi_dev_name(wb->bdi)); 2508 2509 if (likely(!current_is_workqueue_rescuer() || 2510 !test_bit(WB_registered, &wb->state))) { 2511 /* 2512 * The normal path. Keep writing back @wb until its 2513 * work_list is empty. Note that this path is also taken 2514 * if @wb is shutting down even when we're running off the 2515 * rescuer as work_list needs to be drained. 2516 */ 2517 do { 2518 pages_written = wb_do_writeback(wb); 2519 trace_writeback_pages_written(pages_written); 2520 } while (!list_empty(&wb->work_list)); 2521 } else { 2522 /* 2523 * bdi_wq can't get enough workers and we're running off 2524 * the emergency worker. Don't hog it. Hopefully, 1024 is 2525 * enough for efficient IO. 2526 */ 2527 pages_written = writeback_inodes_wb(wb, 1024, 2528 WB_REASON_FORKER_THREAD); 2529 trace_writeback_pages_written(pages_written); 2530 } 2531 2532 if (!list_empty(&wb->work_list)) 2533 wb_wakeup(wb); 2534 else if (wb_has_dirty_io(wb) && dirty_writeback_interval) 2535 wb_wakeup_delayed(wb); 2536 } 2537 2538 /* 2539 * Start writeback of all dirty pages on this bdi. 2540 */ 2541 static void __wakeup_flusher_threads_bdi(struct backing_dev_info *bdi, 2542 enum wb_reason reason) 2543 { 2544 struct bdi_writeback *wb; 2545 2546 if (!bdi_has_dirty_io(bdi)) 2547 return; 2548 2549 list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) 2550 wb_start_writeback(wb, reason); 2551 } 2552 2553 void wakeup_flusher_threads_bdi(struct backing_dev_info *bdi, 2554 enum wb_reason reason) 2555 { 2556 rcu_read_lock(); 2557 __wakeup_flusher_threads_bdi(bdi, reason); 2558 rcu_read_unlock(); 2559 } 2560 2561 /** 2562 * filemap_dontcache_kick_writeback - kick flusher for IOCB_DONTCACHE writes 2563 * @mapping: address_space that was just written to 2564 * 2565 * Kick the writeback flusher thread to expedite writeback of dontcache dirty 2566 * pages. Queue writeback for the inode's wb for as many pages as there are 2567 * dontcache pages, but don't restrict writeback to dontcache pages only. 2568 * 2569 * This significantly improves performance over either writing all wb's pages 2570 * or writing only dontcache pages. Although it doesn't guarantee quick 2571 * writeback and reclaim of dontcache pages, it keeps the amount of dirty pages 2572 * in check. Over longer term dontcache pages get written and reclaimed by 2573 * background writeback even with this rough heuristic. 2574 */ 2575 void filemap_dontcache_kick_writeback(struct address_space *mapping) 2576 { 2577 struct inode *inode = mapping->host; 2578 struct bdi_writeback *wb; 2579 struct wb_lock_cookie cookie = {}; 2580 bool need_wakeup = false; 2581 2582 /* An inode which was never dirtied has no wb and nothing to flush. */ 2583 wb = unlocked_inode_to_wb_begin(inode, &cookie); 2584 if (wb && wb_has_dirty_io(wb) && 2585 !test_bit(WB_start_dontcache, &wb->state) && 2586 !test_and_set_bit(WB_start_dontcache, &wb->state)) { 2587 wb_get(wb); 2588 need_wakeup = true; 2589 } 2590 unlocked_inode_to_wb_end(inode, &cookie); 2591 2592 if (need_wakeup) { 2593 wb_wakeup(wb); 2594 wb_put(wb); 2595 } 2596 } 2597 EXPORT_SYMBOL_GPL(filemap_dontcache_kick_writeback); 2598 2599 /* 2600 * Wakeup the flusher threads to start writeback of all currently dirty pages 2601 */ 2602 void wakeup_flusher_threads(enum wb_reason reason) 2603 { 2604 struct backing_dev_info *bdi; 2605 2606 /* 2607 * If we are expecting writeback progress we must submit plugged IO. 2608 */ 2609 blk_flush_plug(current->plug, true); 2610 2611 rcu_read_lock(); 2612 list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) 2613 __wakeup_flusher_threads_bdi(bdi, reason); 2614 rcu_read_unlock(); 2615 } 2616 2617 /* 2618 * Wake up bdi's periodically to make sure dirtytime inodes gets 2619 * written back periodically. We deliberately do *not* check the 2620 * b_dirtytime list in wb_has_dirty_io(), since this would cause the 2621 * kernel to be constantly waking up once there are any dirtytime 2622 * inodes on the system. So instead we define a separate delayed work 2623 * function which gets called much more rarely. (By default, only 2624 * once every 12 hours.) 2625 * 2626 * If there is any other write activity going on in the file system, 2627 * this function won't be necessary. But if the only thing that has 2628 * happened on the file system is a dirtytime inode caused by an atime 2629 * update, we need this infrastructure below to make sure that inode 2630 * eventually gets pushed out to disk. 2631 */ 2632 static void wakeup_dirtytime_writeback(struct work_struct *w); 2633 static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback); 2634 2635 static void wakeup_dirtytime_writeback(struct work_struct *w) 2636 { 2637 struct backing_dev_info *bdi; 2638 2639 rcu_read_lock(); 2640 list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 2641 struct bdi_writeback *wb; 2642 2643 list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) 2644 if (!list_empty(&wb->b_dirty_time)) 2645 wb_wakeup(wb); 2646 } 2647 rcu_read_unlock(); 2648 if (dirtytime_expire_interval) 2649 schedule_delayed_work(&dirtytime_work, 2650 round_jiffies_relative(dirtytime_expire_interval * HZ)); 2651 } 2652 2653 static int dirtytime_interval_handler(const struct ctl_table *table, int write, 2654 void *buffer, size_t *lenp, loff_t *ppos) 2655 { 2656 int ret; 2657 2658 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 2659 if (ret == 0 && write) { 2660 if (dirtytime_expire_interval) 2661 mod_delayed_work(system_percpu_wq, &dirtytime_work, 0); 2662 else 2663 cancel_delayed_work_sync(&dirtytime_work); 2664 } 2665 return ret; 2666 } 2667 2668 static const struct ctl_table vm_fs_writeback_table[] = { 2669 { 2670 .procname = "dirtytime_expire_seconds", 2671 .data = &dirtytime_expire_interval, 2672 .maxlen = sizeof(dirtytime_expire_interval), 2673 .mode = 0644, 2674 .proc_handler = dirtytime_interval_handler, 2675 .extra1 = SYSCTL_ZERO, 2676 }, 2677 }; 2678 2679 static int __init start_dirtytime_writeback(void) 2680 { 2681 if (dirtytime_expire_interval) 2682 schedule_delayed_work(&dirtytime_work, 2683 round_jiffies_relative(dirtytime_expire_interval * HZ)); 2684 register_sysctl_init("vm", vm_fs_writeback_table); 2685 return 0; 2686 } 2687 __initcall(start_dirtytime_writeback); 2688 2689 /** 2690 * __mark_inode_dirty - internal function to mark an inode dirty 2691 * 2692 * @inode: inode to mark 2693 * @flags: what kind of dirty, e.g. I_DIRTY_SYNC. This can be a combination of 2694 * multiple I_DIRTY_* flags, except that I_DIRTY_TIME can't be combined 2695 * with I_DIRTY_PAGES. 2696 * 2697 * Mark an inode as dirty. We notify the filesystem, then update the inode's 2698 * dirty flags. Then, if needed we add the inode to the appropriate dirty list. 2699 * 2700 * Most callers should use mark_inode_dirty() or mark_inode_dirty_sync() 2701 * instead of calling this directly. 2702 * 2703 * CAREFUL! We only add the inode to the dirty list if it is hashed or if it 2704 * refers to a blockdev. Unhashed inodes will never be added to the dirty list 2705 * even if they are later hashed, as they will have been marked dirty already. 2706 * 2707 * In short, ensure you hash any inodes _before_ you start marking them dirty. 2708 * 2709 * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 2710 * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 2711 * the kernel-internal blockdev inode represents the dirtying time of the 2712 * blockdev's pages. This is why for I_DIRTY_PAGES we always use 2713 * page->mapping->host, so the page-dirtying time is recorded in the internal 2714 * blockdev inode. 2715 */ 2716 void __mark_inode_dirty(struct inode *inode, int flags) 2717 { 2718 struct super_block *sb = inode->i_sb; 2719 int dirtytime = 0; 2720 struct bdi_writeback *wb = NULL; 2721 2722 trace_writeback_mark_inode_dirty(inode, flags); 2723 2724 if (flags & I_DIRTY_INODE) { 2725 bool was_dirty_time = false; 2726 2727 /* 2728 * Inode timestamp update will piggback on this dirtying. 2729 * We tell ->dirty_inode callback that timestamps need to 2730 * be updated by setting I_DIRTY_TIME in flags. 2731 */ 2732 if (inode_state_read_once(inode) & I_DIRTY_TIME) { 2733 spin_lock(&inode->i_lock); 2734 if (inode_state_read(inode) & I_DIRTY_TIME) { 2735 inode_state_clear(inode, I_DIRTY_TIME); 2736 flags |= I_DIRTY_TIME; 2737 was_dirty_time = true; 2738 } 2739 spin_unlock(&inode->i_lock); 2740 } 2741 2742 /* 2743 * Notify the filesystem about the inode being dirtied, so that 2744 * (if needed) it can update on-disk fields and journal the 2745 * inode. This is only needed when the inode itself is being 2746 * dirtied now. I.e. it's only needed for I_DIRTY_INODE, not 2747 * for just I_DIRTY_PAGES or I_DIRTY_TIME. 2748 */ 2749 trace_writeback_dirty_inode_start(inode, flags); 2750 if (sb->s_op->dirty_inode) { 2751 sb->s_op->dirty_inode(inode, 2752 flags & (I_DIRTY_INODE | I_DIRTY_TIME)); 2753 } else if (was_dirty_time && inode->i_op->sync_lazytime) { 2754 inode->i_op->sync_lazytime(inode); 2755 } 2756 trace_writeback_dirty_inode(inode, flags); 2757 2758 /* I_DIRTY_INODE supersedes I_DIRTY_TIME. */ 2759 flags &= ~I_DIRTY_TIME; 2760 } else { 2761 /* 2762 * Else it's either I_DIRTY_PAGES, I_DIRTY_TIME, or nothing. 2763 * (We don't support setting both I_DIRTY_PAGES and I_DIRTY_TIME 2764 * in one call to __mark_inode_dirty().) 2765 */ 2766 dirtytime = flags & I_DIRTY_TIME; 2767 WARN_ON_ONCE(dirtytime && flags != I_DIRTY_TIME); 2768 } 2769 2770 /* 2771 * Paired with smp_mb() in __writeback_single_inode() for the 2772 * following lockless i_state test. See there for details. 2773 */ 2774 smp_mb(); 2775 2776 if ((inode_state_read_once(inode) & flags) == flags) 2777 return; 2778 2779 spin_lock(&inode->i_lock); 2780 if ((inode_state_read(inode) & flags) != flags) { 2781 const int was_dirty = inode_state_read(inode) & I_DIRTY; 2782 2783 inode_attach_wb(inode, NULL); 2784 2785 inode_state_set(inode, flags); 2786 2787 /* 2788 * Grab inode's wb early because it requires dropping i_lock and we 2789 * need to make sure following checks happen atomically with dirty 2790 * list handling so that we don't move inodes under flush worker's 2791 * hands. 2792 */ 2793 if (!was_dirty) { 2794 wb = locked_inode_to_wb_and_lock_list(inode); 2795 spin_lock(&inode->i_lock); 2796 } 2797 2798 /* 2799 * If the inode is queued for writeback by flush worker, just 2800 * update its dirty state. Once the flush worker is done with 2801 * the inode it will place it on the appropriate superblock 2802 * list, based upon its state. 2803 */ 2804 if (inode_state_read(inode) & I_SYNC_QUEUED) 2805 goto out_unlock; 2806 2807 /* 2808 * Only add valid (hashed) inodes to the superblock's 2809 * dirty list. Add blockdev inodes as well. 2810 */ 2811 if (!S_ISBLK(inode->i_mode)) { 2812 if (inode_unhashed(inode)) 2813 goto out_unlock; 2814 } 2815 if (inode_state_read(inode) & I_FREEING) 2816 goto out_unlock; 2817 2818 /* 2819 * If the inode was already on b_dirty/b_io/b_more_io, don't 2820 * reposition it (that would break b_dirty time-ordering). 2821 */ 2822 if (!was_dirty) { 2823 struct list_head *dirty_list; 2824 bool wakeup_bdi = false; 2825 2826 inode->dirtied_when = jiffies; 2827 if (dirtytime) 2828 inode->dirtied_time_when = jiffies; 2829 2830 if (inode_state_read(inode) & I_DIRTY) 2831 dirty_list = &wb->b_dirty; 2832 else 2833 dirty_list = &wb->b_dirty_time; 2834 2835 wakeup_bdi = inode_io_list_move_locked(inode, wb, 2836 dirty_list); 2837 2838 /* 2839 * If this is the first dirty inode for this bdi, 2840 * we have to wake-up the corresponding bdi thread 2841 * to make sure background write-back happens 2842 * later. 2843 */ 2844 if (wakeup_bdi && 2845 (wb->bdi->capabilities & BDI_CAP_WRITEBACK)) 2846 wb_wakeup_delayed(wb); 2847 2848 spin_unlock(&wb->list_lock); 2849 spin_unlock(&inode->i_lock); 2850 trace_writeback_dirty_inode_enqueue(inode); 2851 2852 return; 2853 } 2854 } 2855 out_unlock: 2856 if (wb) 2857 spin_unlock(&wb->list_lock); 2858 spin_unlock(&inode->i_lock); 2859 } 2860 EXPORT_SYMBOL(__mark_inode_dirty); 2861 2862 /* 2863 * The @s_sync_lock is used to serialise concurrent sync operations 2864 * to avoid lock contention problems with concurrent wait_sb_inodes() calls. 2865 * Concurrent callers will block on the s_sync_lock rather than doing contending 2866 * walks. The queueing maintains sync(2) required behaviour as all the IO that 2867 * has been issued up to the time this function is enter is guaranteed to be 2868 * completed by the time we have gained the lock and waited for all IO that is 2869 * in progress regardless of the order callers are granted the lock. 2870 */ 2871 static void wait_sb_inodes(struct super_block *sb) 2872 { 2873 LIST_HEAD(sync_list); 2874 2875 /* 2876 * We need to be protected against the filesystem going from 2877 * r/o to r/w or vice versa. 2878 */ 2879 WARN_ON(!rwsem_is_locked(&sb->s_umount)); 2880 2881 mutex_lock(&sb->s_sync_lock); 2882 2883 /* 2884 * Splice the writeback list onto a temporary list to avoid waiting on 2885 * inodes that have started writeback after this point. 2886 * 2887 * Use rcu_read_lock() to keep the inodes around until we have a 2888 * reference. s_inode_wblist_lock protects sb->s_inodes_wb as well as 2889 * the local list because inodes can be dropped from either by writeback 2890 * completion. 2891 */ 2892 rcu_read_lock(); 2893 spin_lock_irq(&sb->s_inode_wblist_lock); 2894 list_splice_init(&sb->s_inodes_wb, &sync_list); 2895 2896 /* 2897 * Data integrity sync. Must wait for all pages under writeback, because 2898 * there may have been pages dirtied before our sync call, but which had 2899 * writeout started before we write it out. In which case, the inode 2900 * may not be on the dirty list, but we still have to wait for that 2901 * writeout. 2902 */ 2903 while (!list_empty(&sync_list)) { 2904 struct inode *inode = list_first_entry(&sync_list, struct inode, 2905 i_wb_list); 2906 struct address_space *mapping = inode->i_mapping; 2907 2908 /* 2909 * Move each inode back to the wb list before we drop the lock 2910 * to preserve consistency between i_wb_list and the mapping 2911 * writeback tag. Writeback completion is responsible to remove 2912 * the inode from either list once the writeback tag is cleared. 2913 */ 2914 list_move_tail(&inode->i_wb_list, &sb->s_inodes_wb); 2915 2916 /* 2917 * The mapping can appear untagged while still on-list since we 2918 * do not have the mapping lock. Skip it here, wb completion 2919 * will remove it. 2920 */ 2921 if (!mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) 2922 continue; 2923 2924 spin_unlock_irq(&sb->s_inode_wblist_lock); 2925 2926 spin_lock(&inode->i_lock); 2927 if (inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) { 2928 spin_unlock(&inode->i_lock); 2929 2930 spin_lock_irq(&sb->s_inode_wblist_lock); 2931 continue; 2932 } 2933 __iget(inode); 2934 spin_unlock(&inode->i_lock); 2935 rcu_read_unlock(); 2936 2937 /* 2938 * We keep the error status of individual mapping so that 2939 * applications can catch the writeback error using fsync(2). 2940 * See filemap_fdatawait_keep_errors() for details. 2941 */ 2942 filemap_fdatawait_keep_errors(mapping); 2943 2944 cond_resched(); 2945 2946 iput(inode); 2947 2948 rcu_read_lock(); 2949 spin_lock_irq(&sb->s_inode_wblist_lock); 2950 } 2951 spin_unlock_irq(&sb->s_inode_wblist_lock); 2952 rcu_read_unlock(); 2953 mutex_unlock(&sb->s_sync_lock); 2954 } 2955 2956 static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr, 2957 enum wb_reason reason, bool skip_if_busy) 2958 { 2959 struct backing_dev_info *bdi = sb->s_bdi; 2960 DEFINE_WB_COMPLETION(done, bdi); 2961 struct wb_writeback_work work = { 2962 .sb = sb, 2963 .sync_mode = WB_SYNC_NONE, 2964 .tagged_writepages = 1, 2965 .done = &done, 2966 .nr_pages = nr, 2967 .reason = reason, 2968 }; 2969 2970 if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) 2971 return; 2972 WARN_ON(!rwsem_is_locked(&sb->s_umount)); 2973 2974 bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy); 2975 wb_wait_for_completion(&done); 2976 } 2977 2978 /** 2979 * writeback_inodes_sb_nr - writeback dirty inodes from given super_block 2980 * @sb: the superblock 2981 * @nr: the number of pages to write 2982 * @reason: reason why some writeback work initiated 2983 * 2984 * Start writeback on some inodes on this super_block. No guarantees are made 2985 * on how many (if any) will be written, and this function does not wait 2986 * for IO completion of submitted IO. 2987 */ 2988 void writeback_inodes_sb_nr(struct super_block *sb, 2989 unsigned long nr, 2990 enum wb_reason reason) 2991 { 2992 __writeback_inodes_sb_nr(sb, nr, reason, false); 2993 } 2994 EXPORT_SYMBOL(writeback_inodes_sb_nr); 2995 2996 /** 2997 * writeback_inodes_sb - writeback dirty inodes from given super_block 2998 * @sb: the superblock 2999 * @reason: reason why some writeback work was initiated 3000 * 3001 * Start writeback on some inodes on this super_block. No guarantees are made 3002 * on how many (if any) will be written, and this function does not wait 3003 * for IO completion of submitted IO. 3004 */ 3005 void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 3006 { 3007 writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 3008 } 3009 EXPORT_SYMBOL(writeback_inodes_sb); 3010 3011 /** 3012 * try_to_writeback_inodes_sb - try to start writeback if none underway 3013 * @sb: the superblock 3014 * @reason: reason why some writeback work was initiated 3015 * 3016 * Invoke __writeback_inodes_sb_nr if no writeback is currently underway. 3017 */ 3018 void try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 3019 { 3020 if (!down_read_trylock(&sb->s_umount)) 3021 return; 3022 3023 __writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason, true); 3024 up_read(&sb->s_umount); 3025 } 3026 EXPORT_SYMBOL(try_to_writeback_inodes_sb); 3027 3028 /** 3029 * sync_inodes_sb - sync sb inode pages 3030 * @sb: the superblock 3031 * 3032 * This function writes and waits on any dirty inode belonging to this 3033 * super_block. 3034 */ 3035 void sync_inodes_sb(struct super_block *sb) 3036 { 3037 struct backing_dev_info *bdi = sb->s_bdi; 3038 DEFINE_WB_COMPLETION(done, bdi); 3039 struct wb_writeback_work work = { 3040 .sb = sb, 3041 .sync_mode = WB_SYNC_ALL, 3042 .nr_pages = LONG_MAX, 3043 .range_cyclic = 0, 3044 .done = &done, 3045 .reason = WB_REASON_SYNC, 3046 .for_sync = 1, 3047 }; 3048 3049 /* 3050 * Can't skip on !bdi_has_dirty() because we should wait for !dirty 3051 * inodes under writeback and I_DIRTY_TIME inodes ignored by 3052 * bdi_has_dirty() need to be written out too. 3053 */ 3054 if (bdi == &noop_backing_dev_info) 3055 return; 3056 3057 /* 3058 * If the superblock has SB_I_NO_DATA_INTEGRITY set, there's no need to 3059 * wait for the writeout to complete, as the filesystem cannot guarantee 3060 * data persistence on sync. Just kick off writeback and return. 3061 */ 3062 if (sb->s_iflags & SB_I_NO_DATA_INTEGRITY) { 3063 wakeup_flusher_threads_bdi(bdi, WB_REASON_SYNC); 3064 return; 3065 } 3066 3067 WARN_ON(!rwsem_is_locked(&sb->s_umount)); 3068 3069 /* protect against inode wb switch, see inode_switch_wbs_work_fn() */ 3070 bdi_down_write_wb_switch_rwsem(bdi); 3071 bdi_split_work_to_wbs(bdi, &work, false); 3072 wb_wait_for_completion(&done); 3073 bdi_up_write_wb_switch_rwsem(bdi); 3074 3075 wait_sb_inodes(sb); 3076 } 3077 EXPORT_SYMBOL(sync_inodes_sb); 3078 3079 /** 3080 * write_inode_now - write an inode to disk 3081 * @inode: inode to write to disk 3082 * @sync: whether the write should be synchronous or not 3083 * 3084 * This function commits an inode to disk immediately if it is dirty. This is 3085 * primarily needed by knfsd. 3086 * 3087 * The caller must either have a ref on the inode or must have set I_WILL_FREE. 3088 */ 3089 int write_inode_now(struct inode *inode, int sync) 3090 { 3091 struct writeback_control wbc = { 3092 .nr_to_write = LONG_MAX, 3093 .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 3094 .range_start = 0, 3095 .range_end = LLONG_MAX, 3096 }; 3097 3098 if (!mapping_can_writeback(inode->i_mapping)) 3099 wbc.nr_to_write = 0; 3100 3101 might_sleep(); 3102 return writeback_single_inode(inode, &wbc); 3103 } 3104 EXPORT_SYMBOL(write_inode_now); 3105 3106 /** 3107 * sync_inode_metadata - write an inode to disk 3108 * @inode: the inode to sync 3109 * @wait: wait for I/O to complete. 3110 * 3111 * Write an inode to disk and adjust its dirty state after completion. 3112 * 3113 * Note: only writes the actual inode, no associated data or other metadata. 3114 */ 3115 int sync_inode_metadata(struct inode *inode, int wait) 3116 { 3117 struct writeback_control wbc = { 3118 .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 3119 .nr_to_write = 0, /* metadata-only */ 3120 }; 3121 3122 return writeback_single_inode(inode, &wbc); 3123 } 3124 EXPORT_SYMBOL(sync_inode_metadata); 3125