| 1 | /* |
| 2 | * mm/page-writeback.c |
| 3 | * |
| 4 | * Copyright (C) 2002, Linus Torvalds. |
| 5 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra |
| 6 | * |
| 7 | * Contains functions related to writing back dirty pages at the |
| 8 | * address_space level. |
| 9 | * |
| 10 | * 10Apr2002 Andrew Morton |
| 11 | * Initial version |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/export.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/swap.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/pagemap.h> |
| 22 | #include <linux/writeback.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/backing-dev.h> |
| 25 | #include <linux/task_io_accounting_ops.h> |
| 26 | #include <linux/blkdev.h> |
| 27 | #include <linux/mpage.h> |
| 28 | #include <linux/rmap.h> |
| 29 | #include <linux/percpu.h> |
| 30 | #include <linux/notifier.h> |
| 31 | #include <linux/smp.h> |
| 32 | #include <linux/sysctl.h> |
| 33 | #include <linux/cpu.h> |
| 34 | #include <linux/syscalls.h> |
| 35 | #include <linux/buffer_head.h> /* __set_page_dirty_buffers */ |
| 36 | #include <linux/pagevec.h> |
| 37 | #include <linux/timer.h> |
| 38 | #include <linux/sched/rt.h> |
| 39 | #include <linux/mm_inline.h> |
| 40 | #include <trace/events/writeback.h> |
| 41 | #include <linux/version.h> |
| 42 | |
| 43 | #include "internal.h" |
| 44 | |
| 45 | /* |
| 46 | * Sleep at most 200ms at a time in balance_dirty_pages(). |
| 47 | */ |
| 48 | #define MAX_PAUSE max(HZ/5, 1) |
| 49 | |
| 50 | /* |
| 51 | * Try to keep balance_dirty_pages() call intervals higher than this many pages |
| 52 | * by raising pause time to max_pause when falls below it. |
| 53 | */ |
| 54 | #define DIRTY_POLL_THRESH (128 >> (PAGE_SHIFT - 10)) |
| 55 | |
| 56 | /* |
| 57 | * Estimate write bandwidth at 200ms intervals. |
| 58 | */ |
| 59 | #define BANDWIDTH_INTERVAL max(HZ/5, 1) |
| 60 | |
| 61 | #define RATELIMIT_CALC_SHIFT 10 |
| 62 | |
| 63 | /* |
| 64 | * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited |
| 65 | * will look to see if it needs to force writeback or throttling. |
| 66 | */ |
| 67 | static long ratelimit_pages = 32; |
| 68 | |
| 69 | /* The following parameters are exported via /proc/sys/vm */ |
| 70 | |
| 71 | /* |
| 72 | * Start background writeback (via writeback threads) at this percentage |
| 73 | */ |
| 74 | #ifdef CONFIG_LARGE_DIRTY_BUFFER |
| 75 | int dirty_background_ratio = 5; |
| 76 | #else |
| 77 | int dirty_background_ratio = 0; |
| 78 | #endif |
| 79 | |
| 80 | /* |
| 81 | * dirty_background_bytes starts at 0 (disabled) so that it is a function of |
| 82 | * dirty_background_ratio * the amount of dirtyable memory |
| 83 | */ |
| 84 | #ifdef CONFIG_LARGE_DIRTY_BUFFER |
| 85 | unsigned long dirty_background_bytes = 0; |
| 86 | #else |
| 87 | unsigned long dirty_background_bytes = 25 * 1024 * 1024; |
| 88 | #endif |
| 89 | |
| 90 | /* |
| 91 | * free highmem will not be subtracted from the total free memory |
| 92 | * for calculating free ratios if vm_highmem_is_dirtyable is true |
| 93 | */ |
| 94 | int vm_highmem_is_dirtyable; |
| 95 | |
| 96 | /* |
| 97 | * The generator of dirty data starts writeback at this percentage |
| 98 | */ |
| 99 | #ifdef CONFIG_LARGE_DIRTY_BUFFER |
| 100 | int vm_dirty_ratio = 25; |
| 101 | #else |
| 102 | int vm_dirty_ratio = 0; |
| 103 | #endif |
| 104 | |
| 105 | /* |
| 106 | * vm_dirty_bytes starts at 0 (disabled) so that it is a function of |
| 107 | * vm_dirty_ratio * the amount of dirtyable memory |
| 108 | */ |
| 109 | #ifdef CONFIG_LARGE_DIRTY_BUFFER |
| 110 | unsigned long vm_dirty_bytes = 0; |
| 111 | #else |
| 112 | unsigned long vm_dirty_bytes = 50 * 1024 * 1024; |
| 113 | #endif |
| 114 | |
| 115 | /* |
| 116 | * The interval between `kupdate'-style writebacks |
| 117 | */ |
| 118 | unsigned int dirty_writeback_interval = 5 * 100; /* centiseconds */ |
| 119 | |
| 120 | EXPORT_SYMBOL_GPL(dirty_writeback_interval); |
| 121 | |
| 122 | /* |
| 123 | * The longest time for which data is allowed to remain dirty |
| 124 | */ |
| 125 | unsigned int dirty_expire_interval = 30 * 100; /* centiseconds */ |
| 126 | |
| 127 | /* |
| 128 | * Flag that makes the machine dump writes/reads and block dirtyings. |
| 129 | */ |
| 130 | int block_dump; |
| 131 | |
| 132 | /* |
| 133 | * Flag that puts the machine in "laptop mode". Doubles as a timeout in jiffies: |
| 134 | * a full sync is triggered after this time elapses without any disk activity. |
| 135 | */ |
| 136 | int laptop_mode; |
| 137 | |
| 138 | EXPORT_SYMBOL(laptop_mode); |
| 139 | |
| 140 | /* End of sysctl-exported parameters */ |
| 141 | |
| 142 | struct wb_domain global_wb_domain; |
| 143 | |
| 144 | /* consolidated parameters for balance_dirty_pages() and its subroutines */ |
| 145 | struct dirty_throttle_control { |
| 146 | #ifdef CONFIG_CGROUP_WRITEBACK |
| 147 | struct wb_domain *dom; |
| 148 | struct dirty_throttle_control *gdtc; /* only set in memcg dtc's */ |
| 149 | #endif |
| 150 | struct bdi_writeback *wb; |
| 151 | struct fprop_local_percpu *wb_completions; |
| 152 | |
| 153 | unsigned long avail; /* dirtyable */ |
| 154 | unsigned long dirty; /* file_dirty + write + nfs */ |
| 155 | unsigned long thresh; /* dirty threshold */ |
| 156 | unsigned long bg_thresh; /* dirty background threshold */ |
| 157 | |
| 158 | unsigned long wb_dirty; /* per-wb counterparts */ |
| 159 | unsigned long wb_thresh; |
| 160 | unsigned long wb_bg_thresh; |
| 161 | |
| 162 | unsigned long pos_ratio; |
| 163 | }; |
| 164 | |
| 165 | /* |
| 166 | * Length of period for aging writeout fractions of bdis. This is an |
| 167 | * arbitrarily chosen number. The longer the period, the slower fractions will |
| 168 | * reflect changes in current writeout rate. |
| 169 | */ |
| 170 | #define VM_COMPLETIONS_PERIOD_LEN (3*HZ) |
| 171 | |
| 172 | #ifdef CONFIG_CGROUP_WRITEBACK |
| 173 | |
| 174 | #define GDTC_INIT(__wb) .wb = (__wb), \ |
| 175 | .dom = &global_wb_domain, \ |
| 176 | .wb_completions = &(__wb)->completions |
| 177 | |
| 178 | #define GDTC_INIT_NO_WB .dom = &global_wb_domain |
| 179 | |
| 180 | #define MDTC_INIT(__wb, __gdtc) .wb = (__wb), \ |
| 181 | .dom = mem_cgroup_wb_domain(__wb), \ |
| 182 | .wb_completions = &(__wb)->memcg_completions, \ |
| 183 | .gdtc = __gdtc |
| 184 | |
| 185 | static bool mdtc_valid(struct dirty_throttle_control *dtc) |
| 186 | { |
| 187 | return dtc->dom; |
| 188 | } |
| 189 | |
| 190 | static struct wb_domain *dtc_dom(struct dirty_throttle_control *dtc) |
| 191 | { |
| 192 | return dtc->dom; |
| 193 | } |
| 194 | |
| 195 | static struct dirty_throttle_control *mdtc_gdtc(struct dirty_throttle_control *mdtc) |
| 196 | { |
| 197 | return mdtc->gdtc; |
| 198 | } |
| 199 | |
| 200 | static struct fprop_local_percpu *wb_memcg_completions(struct bdi_writeback *wb) |
| 201 | { |
| 202 | return &wb->memcg_completions; |
| 203 | } |
| 204 | |
| 205 | static void wb_min_max_ratio(struct bdi_writeback *wb, |
| 206 | unsigned long *minp, unsigned long *maxp) |
| 207 | { |
| 208 | unsigned long this_bw = wb->avg_write_bandwidth; |
| 209 | unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth); |
| 210 | unsigned long long min = wb->bdi->min_ratio; |
| 211 | unsigned long long max = wb->bdi->max_ratio; |
| 212 | |
| 213 | /* |
| 214 | * @wb may already be clean by the time control reaches here and |
| 215 | * the total may not include its bw. |
| 216 | */ |
| 217 | if (this_bw < tot_bw) { |
| 218 | if (min) { |
| 219 | min *= this_bw; |
| 220 | do_div(min, tot_bw); |
| 221 | } |
| 222 | if (max < 100) { |
| 223 | max *= this_bw; |
| 224 | do_div(max, tot_bw); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | *minp = min; |
| 229 | *maxp = max; |
| 230 | } |
| 231 | |
| 232 | #else /* CONFIG_CGROUP_WRITEBACK */ |
| 233 | |
| 234 | #define GDTC_INIT(__wb) .wb = (__wb), \ |
| 235 | .wb_completions = &(__wb)->completions |
| 236 | #define GDTC_INIT_NO_WB |
| 237 | #define MDTC_INIT(__wb, __gdtc) |
| 238 | |
| 239 | static bool mdtc_valid(struct dirty_throttle_control *dtc) |
| 240 | { |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | static struct wb_domain *dtc_dom(struct dirty_throttle_control *dtc) |
| 245 | { |
| 246 | return &global_wb_domain; |
| 247 | } |
| 248 | |
| 249 | static struct dirty_throttle_control *mdtc_gdtc(struct dirty_throttle_control *mdtc) |
| 250 | { |
| 251 | return NULL; |
| 252 | } |
| 253 | |
| 254 | static struct fprop_local_percpu *wb_memcg_completions(struct bdi_writeback *wb) |
| 255 | { |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | static void wb_min_max_ratio(struct bdi_writeback *wb, |
| 260 | unsigned long *minp, unsigned long *maxp) |
| 261 | { |
| 262 | *minp = wb->bdi->min_ratio; |
| 263 | *maxp = wb->bdi->max_ratio; |
| 264 | } |
| 265 | |
| 266 | #endif /* CONFIG_CGROUP_WRITEBACK */ |
| 267 | |
| 268 | /* |
| 269 | * In a memory zone, there is a certain amount of pages we consider |
| 270 | * available for the page cache, which is essentially the number of |
| 271 | * free and reclaimable pages, minus some zone reserves to protect |
| 272 | * lowmem and the ability to uphold the zone's watermarks without |
| 273 | * requiring writeback. |
| 274 | * |
| 275 | * This number of dirtyable pages is the base value of which the |
| 276 | * user-configurable dirty ratio is the effictive number of pages that |
| 277 | * are allowed to be actually dirtied. Per individual zone, or |
| 278 | * globally by using the sum of dirtyable pages over all zones. |
| 279 | * |
| 280 | * Because the user is allowed to specify the dirty limit globally as |
| 281 | * absolute number of bytes, calculating the per-zone dirty limit can |
| 282 | * require translating the configured limit into a percentage of |
| 283 | * global dirtyable memory first. |
| 284 | */ |
| 285 | |
| 286 | /** |
| 287 | * zone_dirtyable_memory - number of dirtyable pages in a zone |
| 288 | * @zone: the zone |
| 289 | * |
| 290 | * Returns the zone's number of pages potentially available for dirty |
| 291 | * page cache. This is the base value for the per-zone dirty limits. |
| 292 | */ |
| 293 | static unsigned long zone_dirtyable_memory(struct zone *zone) |
| 294 | { |
| 295 | unsigned long nr_pages; |
| 296 | |
| 297 | nr_pages = zone_page_state(zone, NR_FREE_PAGES); |
| 298 | nr_pages -= min(nr_pages, zone->dirty_balance_reserve); |
| 299 | |
| 300 | nr_pages += zone_page_state(zone, NR_INACTIVE_FILE); |
| 301 | nr_pages += zone_page_state(zone, NR_ACTIVE_FILE); |
| 302 | |
| 303 | return nr_pages; |
| 304 | } |
| 305 | |
| 306 | static unsigned long highmem_dirtyable_memory(unsigned long total) |
| 307 | { |
| 308 | #ifdef CONFIG_HIGHMEM |
| 309 | int node; |
| 310 | unsigned long x = 0; |
| 311 | |
| 312 | for_each_node_state(node, N_HIGH_MEMORY) { |
| 313 | struct zone *z = &NODE_DATA(node)->node_zones[ZONE_HIGHMEM]; |
| 314 | |
| 315 | x += zone_dirtyable_memory(z); |
| 316 | } |
| 317 | /* |
| 318 | * Unreclaimable memory (kernel memory or anonymous memory |
| 319 | * without swap) can bring down the dirtyable pages below |
| 320 | * the zone's dirty balance reserve and the above calculation |
| 321 | * will underflow. However we still want to add in nodes |
| 322 | * which are below threshold (negative values) to get a more |
| 323 | * accurate calculation but make sure that the total never |
| 324 | * underflows. |
| 325 | */ |
| 326 | if ((long)x < 0) |
| 327 | x = 0; |
| 328 | |
| 329 | /* |
| 330 | * Make sure that the number of highmem pages is never larger |
| 331 | * than the number of the total dirtyable memory. This can only |
| 332 | * occur in very strange VM situations but we want to make sure |
| 333 | * that this does not occur. |
| 334 | */ |
| 335 | return min(x, total); |
| 336 | #else |
| 337 | return 0; |
| 338 | #endif |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * global_dirtyable_memory - number of globally dirtyable pages |
| 343 | * |
| 344 | * Returns the global number of pages potentially available for dirty |
| 345 | * page cache. This is the base value for the global dirty limits. |
| 346 | */ |
| 347 | static unsigned long global_dirtyable_memory(void) |
| 348 | { |
| 349 | unsigned long x; |
| 350 | |
| 351 | x = global_page_state(NR_FREE_PAGES); |
| 352 | x -= min(x, dirty_balance_reserve); |
| 353 | |
| 354 | x += global_page_state(NR_INACTIVE_FILE); |
| 355 | x += global_page_state(NR_ACTIVE_FILE); |
| 356 | |
| 357 | if (!vm_highmem_is_dirtyable) |
| 358 | x -= highmem_dirtyable_memory(x); |
| 359 | |
| 360 | return x + 1; /* Ensure that we never return 0 */ |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * domain_dirty_limits - calculate thresh and bg_thresh for a wb_domain |
| 365 | * @dtc: dirty_throttle_control of interest |
| 366 | * |
| 367 | * Calculate @dtc->thresh and ->bg_thresh considering |
| 368 | * vm_dirty_{bytes|ratio} and dirty_background_{bytes|ratio}. The caller |
| 369 | * must ensure that @dtc->avail is set before calling this function. The |
| 370 | * dirty limits will be lifted by 1/4 for PF_LESS_THROTTLE (ie. nfsd) and |
| 371 | * real-time tasks. |
| 372 | */ |
| 373 | static void domain_dirty_limits(struct dirty_throttle_control *dtc) |
| 374 | { |
| 375 | unsigned long available_memory = dtc->avail; |
| 376 | struct dirty_throttle_control *gdtc = mdtc_gdtc(dtc); |
| 377 | unsigned long bytes = vm_dirty_bytes; |
| 378 | unsigned long bg_bytes = dirty_background_bytes; |
| 379 | /* convert ratios to per-PAGE_SIZE for higher precision */ |
| 380 | unsigned long ratio = (vm_dirty_ratio * PAGE_SIZE) / 100; |
| 381 | unsigned long bg_ratio = (dirty_background_ratio * PAGE_SIZE) / 100; |
| 382 | unsigned long thresh; |
| 383 | unsigned long bg_thresh; |
| 384 | struct task_struct *tsk; |
| 385 | |
| 386 | /* gdtc is !NULL iff @dtc is for memcg domain */ |
| 387 | if (gdtc) { |
| 388 | unsigned long global_avail = gdtc->avail; |
| 389 | |
| 390 | /* |
| 391 | * The byte settings can't be applied directly to memcg |
| 392 | * domains. Convert them to ratios by scaling against |
| 393 | * globally available memory. As the ratios are in |
| 394 | * per-PAGE_SIZE, they can be obtained by dividing bytes by |
| 395 | * number of pages. |
| 396 | */ |
| 397 | if (bytes) |
| 398 | ratio = min(DIV_ROUND_UP(bytes, global_avail), |
| 399 | PAGE_SIZE); |
| 400 | if (bg_bytes) |
| 401 | bg_ratio = min(DIV_ROUND_UP(bg_bytes, global_avail), |
| 402 | PAGE_SIZE); |
| 403 | bytes = bg_bytes = 0; |
| 404 | } |
| 405 | |
| 406 | if (bytes) |
| 407 | thresh = DIV_ROUND_UP(bytes, PAGE_SIZE); |
| 408 | else |
| 409 | thresh = (ratio * available_memory) / PAGE_SIZE; |
| 410 | |
| 411 | #if defined(CONFIG_MAX_DIRTY_THRESH_PAGES) && CONFIG_MAX_DIRTY_THRESH_PAGES > 0 |
| 412 | if (!bytes && thresh > CONFIG_MAX_DIRTY_THRESH_PAGES) { |
| 413 | thresh = CONFIG_MAX_DIRTY_THRESH_PAGES; |
| 414 | /* reduce available memory not to make bg_thresh too high */ |
| 415 | available_memory = thresh * PAGE_SIZE / ratio; |
| 416 | } |
| 417 | #endif |
| 418 | |
| 419 | if (bg_bytes) |
| 420 | bg_thresh = DIV_ROUND_UP(bg_bytes, PAGE_SIZE); |
| 421 | else |
| 422 | bg_thresh = (bg_ratio * available_memory) / PAGE_SIZE; |
| 423 | |
| 424 | if (bg_thresh >= thresh) |
| 425 | bg_thresh = thresh / 2; |
| 426 | tsk = current; |
| 427 | if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) { |
| 428 | bg_thresh += bg_thresh / 4; |
| 429 | thresh += thresh / 4; |
| 430 | } |
| 431 | dtc->thresh = thresh; |
| 432 | dtc->bg_thresh = bg_thresh; |
| 433 | |
| 434 | /* we should eventually report the domain in the TP */ |
| 435 | if (!gdtc) |
| 436 | trace_global_dirty_state(bg_thresh, thresh); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * global_dirty_limits - background-writeback and dirty-throttling thresholds |
| 441 | * @pbackground: out parameter for bg_thresh |
| 442 | * @pdirty: out parameter for thresh |
| 443 | * |
| 444 | * Calculate bg_thresh and thresh for global_wb_domain. See |
| 445 | * domain_dirty_limits() for details. |
| 446 | */ |
| 447 | void global_dirty_limits(unsigned long *pbackground, unsigned long *pdirty) |
| 448 | { |
| 449 | struct dirty_throttle_control gdtc = { GDTC_INIT_NO_WB }; |
| 450 | |
| 451 | gdtc.avail = global_dirtyable_memory(); |
| 452 | domain_dirty_limits(&gdtc); |
| 453 | |
| 454 | *pbackground = gdtc.bg_thresh; |
| 455 | *pdirty = gdtc.thresh; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * zone_dirty_limit - maximum number of dirty pages allowed in a zone |
| 460 | * @zone: the zone |
| 461 | * |
| 462 | * Returns the maximum number of dirty pages allowed in a zone, based |
| 463 | * on the zone's dirtyable memory. |
| 464 | */ |
| 465 | static unsigned long zone_dirty_limit(struct zone *zone) |
| 466 | { |
| 467 | unsigned long zone_memory = zone_dirtyable_memory(zone); |
| 468 | struct task_struct *tsk = current; |
| 469 | unsigned long dirty; |
| 470 | |
| 471 | if (vm_dirty_bytes) |
| 472 | dirty = DIV_ROUND_UP(vm_dirty_bytes, PAGE_SIZE) * |
| 473 | zone_memory / global_dirtyable_memory(); |
| 474 | else |
| 475 | dirty = vm_dirty_ratio * zone_memory / 100; |
| 476 | |
| 477 | #if defined(CONFIG_MAX_DIRTY_THRESH_PAGES) && CONFIG_MAX_DIRTY_THRESH_PAGES > 0 |
| 478 | if (!vm_dirty_bytes && dirty > CONFIG_MAX_DIRTY_THRESH_PAGES) |
| 479 | dirty = CONFIG_MAX_DIRTY_THRESH_PAGES; |
| 480 | #endif |
| 481 | |
| 482 | if (tsk->flags & PF_LESS_THROTTLE || rt_task(tsk)) |
| 483 | dirty += dirty / 4; |
| 484 | |
| 485 | return dirty; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * zone_dirty_ok - tells whether a zone is within its dirty limits |
| 490 | * @zone: the zone to check |
| 491 | * |
| 492 | * Returns %true when the dirty pages in @zone are within the zone's |
| 493 | * dirty limit, %false if the limit is exceeded. |
| 494 | */ |
| 495 | bool zone_dirty_ok(struct zone *zone) |
| 496 | { |
| 497 | unsigned long limit = zone_dirty_limit(zone); |
| 498 | |
| 499 | return zone_page_state(zone, NR_FILE_DIRTY) + |
| 500 | zone_page_state(zone, NR_UNSTABLE_NFS) + |
| 501 | zone_page_state(zone, NR_WRITEBACK) <= limit; |
| 502 | } |
| 503 | |
| 504 | int dirty_background_ratio_handler(struct ctl_table *table, int write, |
| 505 | void __user *buffer, size_t *lenp, |
| 506 | loff_t *ppos) |
| 507 | { |
| 508 | int ret; |
| 509 | |
| 510 | ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
| 511 | if (ret == 0 && write) |
| 512 | dirty_background_bytes = 0; |
| 513 | return ret; |
| 514 | } |
| 515 | |
| 516 | int dirty_background_bytes_handler(struct ctl_table *table, int write, |
| 517 | void __user *buffer, size_t *lenp, |
| 518 | loff_t *ppos) |
| 519 | { |
| 520 | int ret; |
| 521 | |
| 522 | ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos); |
| 523 | if (ret == 0 && write) |
| 524 | dirty_background_ratio = 0; |
| 525 | return ret; |
| 526 | } |
| 527 | |
| 528 | int dirty_ratio_handler(struct ctl_table *table, int write, |
| 529 | void __user *buffer, size_t *lenp, |
| 530 | loff_t *ppos) |
| 531 | { |
| 532 | int old_ratio = vm_dirty_ratio; |
| 533 | int ret; |
| 534 | |
| 535 | ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
| 536 | if (ret == 0 && write && vm_dirty_ratio != old_ratio) { |
| 537 | writeback_set_ratelimit(); |
| 538 | vm_dirty_bytes = 0; |
| 539 | } |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | int dirty_bytes_handler(struct ctl_table *table, int write, |
| 544 | void __user *buffer, size_t *lenp, |
| 545 | loff_t *ppos) |
| 546 | { |
| 547 | unsigned long old_bytes = vm_dirty_bytes; |
| 548 | int ret; |
| 549 | |
| 550 | ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos); |
| 551 | if (ret == 0 && write && vm_dirty_bytes != old_bytes) { |
| 552 | writeback_set_ratelimit(); |
| 553 | vm_dirty_ratio = 0; |
| 554 | } |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | static unsigned long wp_next_time(unsigned long cur_time) |
| 559 | { |
| 560 | cur_time += VM_COMPLETIONS_PERIOD_LEN; |
| 561 | /* 0 has a special meaning... */ |
| 562 | if (!cur_time) |
| 563 | return 1; |
| 564 | return cur_time; |
| 565 | } |
| 566 | |
| 567 | static void wb_domain_writeout_inc(struct wb_domain *dom, |
| 568 | struct fprop_local_percpu *completions, |
| 569 | unsigned int max_prop_frac) |
| 570 | { |
| 571 | __fprop_inc_percpu_max(&dom->completions, completions, |
| 572 | max_prop_frac); |
| 573 | /* First event after period switching was turned off? */ |
| 574 | if (!unlikely(dom->period_time)) { |
| 575 | /* |
| 576 | * We can race with other __bdi_writeout_inc calls here but |
| 577 | * it does not cause any harm since the resulting time when |
| 578 | * timer will fire and what is in writeout_period_time will be |
| 579 | * roughly the same. |
| 580 | */ |
| 581 | dom->period_time = wp_next_time(jiffies); |
| 582 | mod_timer(&dom->period_timer, dom->period_time); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * Increment @wb's writeout completion count and the global writeout |
| 588 | * completion count. Called from test_clear_page_writeback(). |
| 589 | */ |
| 590 | static inline void __wb_writeout_inc(struct bdi_writeback *wb) |
| 591 | { |
| 592 | struct wb_domain *cgdom; |
| 593 | |
| 594 | __inc_wb_stat(wb, WB_WRITTEN); |
| 595 | wb_domain_writeout_inc(&global_wb_domain, &wb->completions, |
| 596 | wb->bdi->max_prop_frac); |
| 597 | |
| 598 | cgdom = mem_cgroup_wb_domain(wb); |
| 599 | if (cgdom) |
| 600 | wb_domain_writeout_inc(cgdom, wb_memcg_completions(wb), |
| 601 | wb->bdi->max_prop_frac); |
| 602 | } |
| 603 | |
| 604 | void wb_writeout_inc(struct bdi_writeback *wb) |
| 605 | { |
| 606 | unsigned long flags; |
| 607 | |
| 608 | local_irq_save(flags); |
| 609 | __wb_writeout_inc(wb); |
| 610 | local_irq_restore(flags); |
| 611 | } |
| 612 | EXPORT_SYMBOL_GPL(wb_writeout_inc); |
| 613 | |
| 614 | /* |
| 615 | * On idle system, we can be called long after we scheduled because we use |
| 616 | * deferred timers so count with missed periods. |
| 617 | */ |
| 618 | static void writeout_period(unsigned long t) |
| 619 | { |
| 620 | struct wb_domain *dom = (void *)t; |
| 621 | int miss_periods = (jiffies - dom->period_time) / |
| 622 | VM_COMPLETIONS_PERIOD_LEN; |
| 623 | |
| 624 | if (fprop_new_period(&dom->completions, miss_periods + 1)) { |
| 625 | dom->period_time = wp_next_time(dom->period_time + |
| 626 | miss_periods * VM_COMPLETIONS_PERIOD_LEN); |
| 627 | mod_timer(&dom->period_timer, dom->period_time); |
| 628 | } else { |
| 629 | /* |
| 630 | * Aging has zeroed all fractions. Stop wasting CPU on period |
| 631 | * updates. |
| 632 | */ |
| 633 | dom->period_time = 0; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | int wb_domain_init(struct wb_domain *dom, gfp_t gfp) |
| 638 | { |
| 639 | memset(dom, 0, sizeof(*dom)); |
| 640 | |
| 641 | spin_lock_init(&dom->lock); |
| 642 | |
| 643 | init_timer_deferrable(&dom->period_timer); |
| 644 | dom->period_timer.function = writeout_period; |
| 645 | dom->period_timer.data = (unsigned long)dom; |
| 646 | |
| 647 | dom->dirty_limit_tstamp = jiffies; |
| 648 | |
| 649 | return fprop_global_init(&dom->completions, gfp); |
| 650 | } |
| 651 | |
| 652 | #ifdef CONFIG_CGROUP_WRITEBACK |
| 653 | void wb_domain_exit(struct wb_domain *dom) |
| 654 | { |
| 655 | del_timer_sync(&dom->period_timer); |
| 656 | fprop_global_destroy(&dom->completions); |
| 657 | } |
| 658 | #endif |
| 659 | |
| 660 | /* |
| 661 | * bdi_min_ratio keeps the sum of the minimum dirty shares of all |
| 662 | * registered backing devices, which, for obvious reasons, can not |
| 663 | * exceed 100%. |
| 664 | */ |
| 665 | static unsigned int bdi_min_ratio; |
| 666 | |
| 667 | int bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ratio) |
| 668 | { |
| 669 | int ret = 0; |
| 670 | |
| 671 | spin_lock_bh(&bdi_lock); |
| 672 | if (min_ratio > bdi->max_ratio) { |
| 673 | ret = -EINVAL; |
| 674 | } else { |
| 675 | min_ratio -= bdi->min_ratio; |
| 676 | if (bdi_min_ratio + min_ratio < 100) { |
| 677 | bdi_min_ratio += min_ratio; |
| 678 | bdi->min_ratio += min_ratio; |
| 679 | } else { |
| 680 | ret = -EINVAL; |
| 681 | } |
| 682 | } |
| 683 | spin_unlock_bh(&bdi_lock); |
| 684 | |
| 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | int bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned max_ratio) |
| 689 | { |
| 690 | int ret = 0; |
| 691 | |
| 692 | if (max_ratio > 100) |
| 693 | return -EINVAL; |
| 694 | |
| 695 | spin_lock_bh(&bdi_lock); |
| 696 | if (bdi->min_ratio > max_ratio) { |
| 697 | ret = -EINVAL; |
| 698 | } else { |
| 699 | bdi->max_ratio = max_ratio; |
| 700 | bdi->max_prop_frac = (FPROP_FRAC_BASE * max_ratio) / 100; |
| 701 | } |
| 702 | spin_unlock_bh(&bdi_lock); |
| 703 | |
| 704 | return ret; |
| 705 | } |
| 706 | EXPORT_SYMBOL(bdi_set_max_ratio); |
| 707 | |
| 708 | static unsigned long dirty_freerun_ceiling(unsigned long thresh, |
| 709 | unsigned long bg_thresh) |
| 710 | { |
| 711 | #ifdef CONFIG_LARGE_DIRTY_BUFFER |
| 712 | return (3 * thresh + bg_thresh) / 4; |
| 713 | #else |
| 714 | return (thresh + bg_thresh) / 2; |
| 715 | #endif |
| 716 | } |
| 717 | |
| 718 | static unsigned long hard_dirty_limit(struct wb_domain *dom, |
| 719 | unsigned long thresh) |
| 720 | { |
| 721 | return max(thresh, dom->dirty_limit); |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * Memory which can be further allocated to a memcg domain is capped by |
| 726 | * system-wide clean memory excluding the amount being used in the domain. |
| 727 | */ |
| 728 | static void mdtc_calc_avail(struct dirty_throttle_control *mdtc, |
| 729 | unsigned long filepages, unsigned long headroom) |
| 730 | { |
| 731 | struct dirty_throttle_control *gdtc = mdtc_gdtc(mdtc); |
| 732 | unsigned long clean = filepages - min(filepages, mdtc->dirty); |
| 733 | unsigned long global_clean = gdtc->avail - min(gdtc->avail, gdtc->dirty); |
| 734 | unsigned long other_clean = global_clean - min(global_clean, clean); |
| 735 | |
| 736 | mdtc->avail = filepages + min(headroom, other_clean); |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * __wb_calc_thresh - @wb's share of dirty throttling threshold |
| 741 | * @dtc: dirty_throttle_context of interest |
| 742 | * |
| 743 | * Returns @wb's dirty limit in pages. The term "dirty" in the context of |
| 744 | * dirty balancing includes all PG_dirty, PG_writeback and NFS unstable pages. |
| 745 | * |
| 746 | * Note that balance_dirty_pages() will only seriously take it as a hard limit |
| 747 | * when sleeping max_pause per page is not enough to keep the dirty pages under |
| 748 | * control. For example, when the device is completely stalled due to some error |
| 749 | * conditions, or when there are 1000 dd tasks writing to a slow 10MB/s USB key. |
| 750 | * In the other normal situations, it acts more gently by throttling the tasks |
| 751 | * more (rather than completely block them) when the wb dirty pages go high. |
| 752 | * |
| 753 | * It allocates high/low dirty limits to fast/slow devices, in order to prevent |
| 754 | * - starving fast devices |
| 755 | * - piling up dirty pages (that will take long time to sync) on slow devices |
| 756 | * |
| 757 | * The wb's share of dirty limit will be adapting to its throughput and |
| 758 | * bounded by the bdi->min_ratio and/or bdi->max_ratio parameters, if set. |
| 759 | */ |
| 760 | static unsigned long __wb_calc_thresh(struct dirty_throttle_control *dtc) |
| 761 | { |
| 762 | struct wb_domain *dom = dtc_dom(dtc); |
| 763 | unsigned long thresh = dtc->thresh; |
| 764 | u64 wb_thresh; |
| 765 | long numerator, denominator; |
| 766 | unsigned long wb_min_ratio, wb_max_ratio; |
| 767 | |
| 768 | /* |
| 769 | * Calculate this BDI's share of the thresh ratio. |
| 770 | */ |
| 771 | fprop_fraction_percpu(&dom->completions, dtc->wb_completions, |
| 772 | &numerator, &denominator); |
| 773 | |
| 774 | wb_thresh = (thresh * (100 - bdi_min_ratio)) / 100; |
| 775 | wb_thresh *= numerator; |
| 776 | do_div(wb_thresh, denominator); |
| 777 | |
| 778 | wb_min_max_ratio(dtc->wb, &wb_min_ratio, &wb_max_ratio); |
| 779 | |
| 780 | wb_thresh += (thresh * wb_min_ratio) / 100; |
| 781 | if (wb_thresh > (thresh * wb_max_ratio) / 100) |
| 782 | wb_thresh = thresh * wb_max_ratio / 100; |
| 783 | |
| 784 | return wb_thresh; |
| 785 | } |
| 786 | |
| 787 | unsigned long wb_calc_thresh(struct bdi_writeback *wb, unsigned long thresh) |
| 788 | { |
| 789 | struct dirty_throttle_control gdtc = { GDTC_INIT(wb), |
| 790 | .thresh = thresh }; |
| 791 | return __wb_calc_thresh(&gdtc); |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * setpoint - dirty 3 |
| 796 | * f(dirty) := 1.0 + (----------------) |
| 797 | * limit - setpoint |
| 798 | * |
| 799 | * it's a 3rd order polynomial that subjects to |
| 800 | * |
| 801 | * (1) f(freerun) = 2.0 => rampup dirty_ratelimit reasonably fast |
| 802 | * (2) f(setpoint) = 1.0 => the balance point |
| 803 | * (3) f(limit) = 0 => the hard limit |
| 804 | * (4) df/dx <= 0 => negative feedback control |
| 805 | * (5) the closer to setpoint, the smaller |df/dx| (and the reverse) |
| 806 | * => fast response on large errors; small oscillation near setpoint |
| 807 | */ |
| 808 | static long long pos_ratio_polynom(unsigned long setpoint, |
| 809 | unsigned long dirty, |
| 810 | unsigned long limit) |
| 811 | { |
| 812 | long long pos_ratio; |
| 813 | long x; |
| 814 | |
| 815 | x = div64_s64(((s64)setpoint - (s64)dirty) << RATELIMIT_CALC_SHIFT, |
| 816 | (limit - setpoint) | 1); |
| 817 | pos_ratio = x; |
| 818 | pos_ratio = pos_ratio * x >> RATELIMIT_CALC_SHIFT; |
| 819 | pos_ratio = pos_ratio * x >> RATELIMIT_CALC_SHIFT; |
| 820 | pos_ratio += 1 << RATELIMIT_CALC_SHIFT; |
| 821 | |
| 822 | return clamp(pos_ratio, 0LL, 2LL << RATELIMIT_CALC_SHIFT); |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | * Dirty position control. |
| 827 | * |
| 828 | * (o) global/bdi setpoints |
| 829 | * |
| 830 | * We want the dirty pages be balanced around the global/wb setpoints. |
| 831 | * When the number of dirty pages is higher/lower than the setpoint, the |
| 832 | * dirty position control ratio (and hence task dirty ratelimit) will be |
| 833 | * decreased/increased to bring the dirty pages back to the setpoint. |
| 834 | * |
| 835 | * pos_ratio = 1 << RATELIMIT_CALC_SHIFT |
| 836 | * |
| 837 | * if (dirty < setpoint) scale up pos_ratio |
| 838 | * if (dirty > setpoint) scale down pos_ratio |
| 839 | * |
| 840 | * if (wb_dirty < wb_setpoint) scale up pos_ratio |
| 841 | * if (wb_dirty > wb_setpoint) scale down pos_ratio |
| 842 | * |
| 843 | * task_ratelimit = dirty_ratelimit * pos_ratio >> RATELIMIT_CALC_SHIFT |
| 844 | * |
| 845 | * (o) global control line |
| 846 | * |
| 847 | * ^ pos_ratio |
| 848 | * | |
| 849 | * | |<===== global dirty control scope ======>| |
| 850 | * 2.0 .............* |
| 851 | * | .* |
| 852 | * | . * |
| 853 | * | . * |
| 854 | * | . * |
| 855 | * | . * |
| 856 | * | . * |
| 857 | * 1.0 ................................* |
| 858 | * | . . * |
| 859 | * | . . * |
| 860 | * | . . * |
| 861 | * | . . * |
| 862 | * | . . * |
| 863 | * 0 +------------.------------------.----------------------*-------------> |
| 864 | * freerun^ setpoint^ limit^ dirty pages |
| 865 | * |
| 866 | * (o) wb control line |
| 867 | * |
| 868 | * ^ pos_ratio |
| 869 | * | |
| 870 | * | * |
| 871 | * | * |
| 872 | * | * |
| 873 | * | * |
| 874 | * | * |<=========== span ============>| |
| 875 | * 1.0 .......................* |
| 876 | * | . * |
| 877 | * | . * |
| 878 | * | . * |
| 879 | * | . * |
| 880 | * | . * |
| 881 | * | . * |
| 882 | * | . * |
| 883 | * | . * |
| 884 | * | . * |
| 885 | * | . * |
| 886 | * | . * |
| 887 | * 1/4 ...............................................* * * * * * * * * * * * |
| 888 | * | . . |
| 889 | * | . . |
| 890 | * | . . |
| 891 | * 0 +----------------------.-------------------------------.-------------> |
| 892 | * wb_setpoint^ x_intercept^ |
| 893 | * |
| 894 | * The wb control line won't drop below pos_ratio=1/4, so that wb_dirty can |
| 895 | * be smoothly throttled down to normal if it starts high in situations like |
| 896 | * - start writing to a slow SD card and a fast disk at the same time. The SD |
| 897 | * card's wb_dirty may rush to many times higher than wb_setpoint. |
| 898 | * - the wb dirty thresh drops quickly due to change of JBOD workload |
| 899 | */ |
| 900 | static void wb_position_ratio(struct dirty_throttle_control *dtc) |
| 901 | { |
| 902 | struct bdi_writeback *wb = dtc->wb; |
| 903 | unsigned long write_bw = wb->avg_write_bandwidth; |
| 904 | unsigned long freerun = dirty_freerun_ceiling(dtc->thresh, dtc->bg_thresh); |
| 905 | unsigned long limit = hard_dirty_limit(dtc_dom(dtc), dtc->thresh); |
| 906 | unsigned long wb_thresh = dtc->wb_thresh; |
| 907 | unsigned long x_intercept; |
| 908 | unsigned long setpoint; /* dirty pages' target balance point */ |
| 909 | unsigned long wb_setpoint; |
| 910 | unsigned long span; |
| 911 | long long pos_ratio; /* for scaling up/down the rate limit */ |
| 912 | long x; |
| 913 | |
| 914 | dtc->pos_ratio = 0; |
| 915 | |
| 916 | if (unlikely(dtc->dirty >= limit)) |
| 917 | return; |
| 918 | |
| 919 | /* |
| 920 | * global setpoint |
| 921 | * |
| 922 | * See comment for pos_ratio_polynom(). |
| 923 | */ |
| 924 | setpoint = (freerun + limit) / 2; |
| 925 | pos_ratio = pos_ratio_polynom(setpoint, dtc->dirty, limit); |
| 926 | |
| 927 | /* |
| 928 | * The strictlimit feature is a tool preventing mistrusted filesystems |
| 929 | * from growing a large number of dirty pages before throttling. For |
| 930 | * such filesystems balance_dirty_pages always checks wb counters |
| 931 | * against wb limits. Even if global "nr_dirty" is under "freerun". |
| 932 | * This is especially important for fuse which sets bdi->max_ratio to |
| 933 | * 1% by default. Without strictlimit feature, fuse writeback may |
| 934 | * consume arbitrary amount of RAM because it is accounted in |
| 935 | * NR_WRITEBACK_TEMP which is not involved in calculating "nr_dirty". |
| 936 | * |
| 937 | * Here, in wb_position_ratio(), we calculate pos_ratio based on |
| 938 | * two values: wb_dirty and wb_thresh. Let's consider an example: |
| 939 | * total amount of RAM is 16GB, bdi->max_ratio is equal to 1%, global |
| 940 | * limits are set by default to 10% and 20% (background and throttle). |
| 941 | * Then wb_thresh is 1% of 20% of 16GB. This amounts to ~8K pages. |
| 942 | * wb_calc_thresh(wb, bg_thresh) is about ~4K pages. wb_setpoint is |
| 943 | * about ~6K pages (as the average of background and throttle wb |
| 944 | * limits). The 3rd order polynomial will provide positive feedback if |
| 945 | * wb_dirty is under wb_setpoint and vice versa. |
| 946 | * |
| 947 | * Note, that we cannot use global counters in these calculations |
| 948 | * because we want to throttle process writing to a strictlimit wb |
| 949 | * much earlier than global "freerun" is reached (~23MB vs. ~2.3GB |
| 950 | * in the example above). |
| 951 | */ |
| 952 | if (unlikely(wb->bdi->capabilities & BDI_CAP_STRICTLIMIT)) { |
| 953 | long long wb_pos_ratio; |
| 954 | |
| 955 | if (dtc->wb_dirty < 8) { |
| 956 | dtc->pos_ratio = min_t(long long, pos_ratio * 2, |
| 957 | 2 << RATELIMIT_CALC_SHIFT); |
| 958 | return; |
| 959 | } |
| 960 | |
| 961 | if (dtc->wb_dirty >= wb_thresh) |
| 962 | return; |
| 963 | |
| 964 | wb_setpoint = dirty_freerun_ceiling(wb_thresh, |
| 965 | dtc->wb_bg_thresh); |
| 966 | |
| 967 | if (wb_setpoint == 0 || wb_setpoint == wb_thresh) |
| 968 | return; |
| 969 | |
| 970 | wb_pos_ratio = pos_ratio_polynom(wb_setpoint, dtc->wb_dirty, |
| 971 | wb_thresh); |
| 972 | |
| 973 | /* |
| 974 | * Typically, for strictlimit case, wb_setpoint << setpoint |
| 975 | * and pos_ratio >> wb_pos_ratio. In the other words global |
| 976 | * state ("dirty") is not limiting factor and we have to |
| 977 | * make decision based on wb counters. But there is an |
| 978 | * important case when global pos_ratio should get precedence: |
| 979 | * global limits are exceeded (e.g. due to activities on other |
| 980 | * wb's) while given strictlimit wb is below limit. |
| 981 | * |
| 982 | * "pos_ratio * wb_pos_ratio" would work for the case above, |
| 983 | * but it would look too non-natural for the case of all |
| 984 | * activity in the system coming from a single strictlimit wb |
| 985 | * with bdi->max_ratio == 100%. |
| 986 | * |
| 987 | * Note that min() below somewhat changes the dynamics of the |
| 988 | * control system. Normally, pos_ratio value can be well over 3 |
| 989 | * (when globally we are at freerun and wb is well below wb |
| 990 | * setpoint). Now the maximum pos_ratio in the same situation |
| 991 | * is 2. We might want to tweak this if we observe the control |
| 992 | * system is too slow to adapt. |
| 993 | */ |
| 994 | dtc->pos_ratio = min(pos_ratio, wb_pos_ratio); |
| 995 | return; |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * We have computed basic pos_ratio above based on global situation. If |
| 1000 | * the wb is over/under its share of dirty pages, we want to scale |
| 1001 | * pos_ratio further down/up. That is done by the following mechanism. |
| 1002 | */ |
| 1003 | |
| 1004 | /* |
| 1005 | * wb setpoint |
| 1006 | * |
| 1007 | * f(wb_dirty) := 1.0 + k * (wb_dirty - wb_setpoint) |
| 1008 | * |
| 1009 | * x_intercept - wb_dirty |
| 1010 | * := -------------------------- |
| 1011 | * x_intercept - wb_setpoint |
| 1012 | * |
| 1013 | * The main wb control line is a linear function that subjects to |
| 1014 | * |
| 1015 | * (1) f(wb_setpoint) = 1.0 |
| 1016 | * (2) k = - 1 / (8 * write_bw) (in single wb case) |
| 1017 | * or equally: x_intercept = wb_setpoint + 8 * write_bw |
| 1018 | * |
| 1019 | * For single wb case, the dirty pages are observed to fluctuate |
| 1020 | * regularly within range |
| 1021 | * [wb_setpoint - write_bw/2, wb_setpoint + write_bw/2] |
| 1022 | * for various filesystems, where (2) can yield in a reasonable 12.5% |
| 1023 | * fluctuation range for pos_ratio. |
| 1024 | * |
| 1025 | * For JBOD case, wb_thresh (not wb_dirty!) could fluctuate up to its |
| 1026 | * own size, so move the slope over accordingly and choose a slope that |
| 1027 | * yields 100% pos_ratio fluctuation on suddenly doubled wb_thresh. |
| 1028 | */ |
| 1029 | if (unlikely(wb_thresh > dtc->thresh)) |
| 1030 | wb_thresh = dtc->thresh; |
| 1031 | /* |
| 1032 | * It's very possible that wb_thresh is close to 0 not because the |
| 1033 | * device is slow, but that it has remained inactive for long time. |
| 1034 | * Honour such devices a reasonable good (hopefully IO efficient) |
| 1035 | * threshold, so that the occasional writes won't be blocked and active |
| 1036 | * writes can rampup the threshold quickly. |
| 1037 | */ |
| 1038 | wb_thresh = max(wb_thresh, (limit - dtc->dirty) / 8); |
| 1039 | /* |
| 1040 | * scale global setpoint to wb's: |
| 1041 | * wb_setpoint = setpoint * wb_thresh / thresh |
| 1042 | */ |
| 1043 | x = div_u64((u64)wb_thresh << 16, dtc->thresh | 1); |
| 1044 | wb_setpoint = setpoint * (u64)x >> 16; |
| 1045 | /* |
| 1046 | * Use span=(8*write_bw) in single wb case as indicated by |
| 1047 | * (thresh - wb_thresh ~= 0) and transit to wb_thresh in JBOD case. |
| 1048 | * |
| 1049 | * wb_thresh thresh - wb_thresh |
| 1050 | * span = --------- * (8 * write_bw) + ------------------ * wb_thresh |
| 1051 | * thresh thresh |
| 1052 | */ |
| 1053 | span = (dtc->thresh - wb_thresh + 8 * write_bw) * (u64)x >> 16; |
| 1054 | x_intercept = wb_setpoint + span; |
| 1055 | |
| 1056 | if (dtc->wb_dirty < x_intercept - span / 4) { |
| 1057 | pos_ratio = div64_u64(pos_ratio * (x_intercept - dtc->wb_dirty), |
| 1058 | (x_intercept - wb_setpoint) | 1); |
| 1059 | } else |
| 1060 | pos_ratio /= 4; |
| 1061 | |
| 1062 | /* |
| 1063 | * wb reserve area, safeguard against dirty pool underrun and disk idle |
| 1064 | * It may push the desired control point of global dirty pages higher |
| 1065 | * than setpoint. |
| 1066 | */ |
| 1067 | x_intercept = wb_thresh / 2; |
| 1068 | if (dtc->wb_dirty < x_intercept) { |
| 1069 | if (dtc->wb_dirty > x_intercept / 8) |
| 1070 | pos_ratio = div_u64(pos_ratio * x_intercept, |
| 1071 | dtc->wb_dirty); |
| 1072 | else |
| 1073 | pos_ratio *= 8; |
| 1074 | } |
| 1075 | |
| 1076 | dtc->pos_ratio = pos_ratio; |
| 1077 | } |
| 1078 | |
| 1079 | static void wb_update_write_bandwidth(struct bdi_writeback *wb, |
| 1080 | unsigned long elapsed, |
| 1081 | unsigned long written) |
| 1082 | { |
| 1083 | const unsigned long period = roundup_pow_of_two(3 * HZ); |
| 1084 | unsigned long avg = wb->avg_write_bandwidth; |
| 1085 | unsigned long old = wb->write_bandwidth; |
| 1086 | u64 bw; |
| 1087 | |
| 1088 | /* |
| 1089 | * bw = written * HZ / elapsed |
| 1090 | * |
| 1091 | * bw * elapsed + write_bandwidth * (period - elapsed) |
| 1092 | * write_bandwidth = --------------------------------------------------- |
| 1093 | * period |
| 1094 | * |
| 1095 | * @written may have decreased due to account_page_redirty(). |
| 1096 | * Avoid underflowing @bw calculation. |
| 1097 | */ |
| 1098 | bw = written - min(written, wb->written_stamp); |
| 1099 | bw *= HZ; |
| 1100 | if (unlikely(elapsed > period)) { |
| 1101 | do_div(bw, elapsed); |
| 1102 | avg = bw; |
| 1103 | goto out; |
| 1104 | } |
| 1105 | bw += (u64)wb->write_bandwidth * (period - elapsed); |
| 1106 | bw >>= ilog2(period); |
| 1107 | |
| 1108 | /* |
| 1109 | * one more level of smoothing, for filtering out sudden spikes |
| 1110 | */ |
| 1111 | if (avg > old && old >= (unsigned long)bw) |
| 1112 | avg -= (avg - old) >> 3; |
| 1113 | |
| 1114 | if (avg < old && old <= (unsigned long)bw) |
| 1115 | avg += (old - avg) >> 3; |
| 1116 | |
| 1117 | out: |
| 1118 | /* keep avg > 0 to guarantee that tot > 0 if there are dirty wbs */ |
| 1119 | avg = max(avg, 1LU); |
| 1120 | if (wb_has_dirty_io(wb)) { |
| 1121 | long delta = avg - wb->avg_write_bandwidth; |
| 1122 | WARN_ON_ONCE(atomic_long_add_return(delta, |
| 1123 | &wb->bdi->tot_write_bandwidth) <= 0); |
| 1124 | } |
| 1125 | wb->write_bandwidth = bw; |
| 1126 | wb->avg_write_bandwidth = avg; |
| 1127 | } |
| 1128 | |
| 1129 | static void update_dirty_limit(struct dirty_throttle_control *dtc) |
| 1130 | { |
| 1131 | struct wb_domain *dom = dtc_dom(dtc); |
| 1132 | unsigned long thresh = dtc->thresh; |
| 1133 | unsigned long limit = dom->dirty_limit; |
| 1134 | |
| 1135 | /* |
| 1136 | * Follow up in one step. |
| 1137 | */ |
| 1138 | if (limit < thresh) { |
| 1139 | limit = thresh; |
| 1140 | goto update; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * Follow down slowly. Use the higher one as the target, because thresh |
| 1145 | * may drop below dirty. This is exactly the reason to introduce |
| 1146 | * dom->dirty_limit which is guaranteed to lie above the dirty pages. |
| 1147 | */ |
| 1148 | thresh = max(thresh, dtc->dirty); |
| 1149 | if (limit > thresh) { |
| 1150 | limit -= (limit - thresh) >> 5; |
| 1151 | goto update; |
| 1152 | } |
| 1153 | return; |
| 1154 | update: |
| 1155 | dom->dirty_limit = limit; |
| 1156 | } |
| 1157 | |
| 1158 | static void domain_update_bandwidth(struct dirty_throttle_control *dtc, |
| 1159 | unsigned long now) |
| 1160 | { |
| 1161 | struct wb_domain *dom = dtc_dom(dtc); |
| 1162 | |
| 1163 | /* |
| 1164 | * check locklessly first to optimize away locking for the most time |
| 1165 | */ |
| 1166 | if (time_before(now, dom->dirty_limit_tstamp + BANDWIDTH_INTERVAL)) |
| 1167 | return; |
| 1168 | |
| 1169 | spin_lock(&dom->lock); |
| 1170 | if (time_after_eq(now, dom->dirty_limit_tstamp + BANDWIDTH_INTERVAL)) { |
| 1171 | update_dirty_limit(dtc); |
| 1172 | dom->dirty_limit_tstamp = now; |
| 1173 | } |
| 1174 | spin_unlock(&dom->lock); |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Maintain wb->dirty_ratelimit, the base dirty throttle rate. |
| 1179 | * |
| 1180 | * Normal wb tasks will be curbed at or below it in long term. |
| 1181 | * Obviously it should be around (write_bw / N) when there are N dd tasks. |
| 1182 | */ |
| 1183 | static void wb_update_dirty_ratelimit(struct dirty_throttle_control *dtc, |
| 1184 | unsigned long dirtied, |
| 1185 | unsigned long elapsed) |
| 1186 | { |
| 1187 | struct bdi_writeback *wb = dtc->wb; |
| 1188 | unsigned long dirty = dtc->dirty; |
| 1189 | unsigned long freerun = dirty_freerun_ceiling(dtc->thresh, dtc->bg_thresh); |
| 1190 | unsigned long limit = hard_dirty_limit(dtc_dom(dtc), dtc->thresh); |
| 1191 | unsigned long setpoint = (freerun + limit) / 2; |
| 1192 | unsigned long write_bw = wb->avg_write_bandwidth; |
| 1193 | unsigned long dirty_ratelimit = wb->dirty_ratelimit; |
| 1194 | unsigned long dirty_rate; |
| 1195 | unsigned long task_ratelimit; |
| 1196 | unsigned long balanced_dirty_ratelimit; |
| 1197 | unsigned long step; |
| 1198 | unsigned long x; |
| 1199 | |
| 1200 | /* |
| 1201 | * The dirty rate will match the writeout rate in long term, except |
| 1202 | * when dirty pages are truncated by userspace or re-dirtied by FS. |
| 1203 | */ |
| 1204 | dirty_rate = (dirtied - wb->dirtied_stamp) * HZ / elapsed; |
| 1205 | |
| 1206 | /* |
| 1207 | * task_ratelimit reflects each dd's dirty rate for the past 200ms. |
| 1208 | */ |
| 1209 | task_ratelimit = (u64)dirty_ratelimit * |
| 1210 | dtc->pos_ratio >> RATELIMIT_CALC_SHIFT; |
| 1211 | task_ratelimit++; /* it helps rampup dirty_ratelimit from tiny values */ |
| 1212 | |
| 1213 | /* |
| 1214 | * A linear estimation of the "balanced" throttle rate. The theory is, |
| 1215 | * if there are N dd tasks, each throttled at task_ratelimit, the wb's |
| 1216 | * dirty_rate will be measured to be (N * task_ratelimit). So the below |
| 1217 | * formula will yield the balanced rate limit (write_bw / N). |
| 1218 | * |
| 1219 | * Note that the expanded form is not a pure rate feedback: |
| 1220 | * rate_(i+1) = rate_(i) * (write_bw / dirty_rate) (1) |
| 1221 | * but also takes pos_ratio into account: |
| 1222 | * rate_(i+1) = rate_(i) * (write_bw / dirty_rate) * pos_ratio (2) |
| 1223 | * |
| 1224 | * (1) is not realistic because pos_ratio also takes part in balancing |
| 1225 | * the dirty rate. Consider the state |
| 1226 | * pos_ratio = 0.5 (3) |
| 1227 | * rate = 2 * (write_bw / N) (4) |
| 1228 | * If (1) is used, it will stuck in that state! Because each dd will |
| 1229 | * be throttled at |
| 1230 | * task_ratelimit = pos_ratio * rate = (write_bw / N) (5) |
| 1231 | * yielding |
| 1232 | * dirty_rate = N * task_ratelimit = write_bw (6) |
| 1233 | * put (6) into (1) we get |
| 1234 | * rate_(i+1) = rate_(i) (7) |
| 1235 | * |
| 1236 | * So we end up using (2) to always keep |
| 1237 | * rate_(i+1) ~= (write_bw / N) (8) |
| 1238 | * regardless of the value of pos_ratio. As long as (8) is satisfied, |
| 1239 | * pos_ratio is able to drive itself to 1.0, which is not only where |
| 1240 | * the dirty count meet the setpoint, but also where the slope of |
| 1241 | * pos_ratio is most flat and hence task_ratelimit is least fluctuated. |
| 1242 | */ |
| 1243 | balanced_dirty_ratelimit = div_u64((u64)task_ratelimit * write_bw, |
| 1244 | dirty_rate | 1); |
| 1245 | /* |
| 1246 | * balanced_dirty_ratelimit ~= (write_bw / N) <= write_bw |
| 1247 | */ |
| 1248 | if (unlikely(balanced_dirty_ratelimit > write_bw)) |
| 1249 | balanced_dirty_ratelimit = write_bw; |
| 1250 | |
| 1251 | /* |
| 1252 | * We could safely do this and return immediately: |
| 1253 | * |
| 1254 | * wb->dirty_ratelimit = balanced_dirty_ratelimit; |
| 1255 | * |
| 1256 | * However to get a more stable dirty_ratelimit, the below elaborated |
| 1257 | * code makes use of task_ratelimit to filter out singular points and |
| 1258 | * limit the step size. |
| 1259 | * |
| 1260 | * The below code essentially only uses the relative value of |
| 1261 | * |
| 1262 | * task_ratelimit - dirty_ratelimit |
| 1263 | * = (pos_ratio - 1) * dirty_ratelimit |
| 1264 | * |
| 1265 | * which reflects the direction and size of dirty position error. |
| 1266 | */ |
| 1267 | |
| 1268 | /* |
| 1269 | * dirty_ratelimit will follow balanced_dirty_ratelimit iff |
| 1270 | * task_ratelimit is on the same side of dirty_ratelimit, too. |
| 1271 | * For example, when |
| 1272 | * - dirty_ratelimit > balanced_dirty_ratelimit |
| 1273 | * - dirty_ratelimit > task_ratelimit (dirty pages are above setpoint) |
| 1274 | * lowering dirty_ratelimit will help meet both the position and rate |
| 1275 | * control targets. Otherwise, don't update dirty_ratelimit if it will |
| 1276 | * only help meet the rate target. After all, what the users ultimately |
| 1277 | * feel and care are stable dirty rate and small position error. |
| 1278 | * |
| 1279 | * |task_ratelimit - dirty_ratelimit| is used to limit the step size |
| 1280 | * and filter out the singular points of balanced_dirty_ratelimit. Which |
| 1281 | * keeps jumping around randomly and can even leap far away at times |
| 1282 | * due to the small 200ms estimation period of dirty_rate (we want to |
| 1283 | * keep that period small to reduce time lags). |
| 1284 | */ |
| 1285 | step = 0; |
| 1286 | |
| 1287 | /* |
| 1288 | * For strictlimit case, calculations above were based on wb counters |
| 1289 | * and limits (starting from pos_ratio = wb_position_ratio() and up to |
| 1290 | * balanced_dirty_ratelimit = task_ratelimit * write_bw / dirty_rate). |
| 1291 | * Hence, to calculate "step" properly, we have to use wb_dirty as |
| 1292 | * "dirty" and wb_setpoint as "setpoint". |
| 1293 | * |
| 1294 | * We rampup dirty_ratelimit forcibly if wb_dirty is low because |
| 1295 | * it's possible that wb_thresh is close to zero due to inactivity |
| 1296 | * of backing device. |
| 1297 | */ |
| 1298 | if (unlikely(wb->bdi->capabilities & BDI_CAP_STRICTLIMIT)) { |
| 1299 | dirty = dtc->wb_dirty; |
| 1300 | if (dtc->wb_dirty < 8) |
| 1301 | setpoint = dtc->wb_dirty + 1; |
| 1302 | else |
| 1303 | setpoint = (dtc->wb_thresh + dtc->wb_bg_thresh) / 2; |
| 1304 | } |
| 1305 | |
| 1306 | if (dirty < setpoint) { |
| 1307 | x = min3(wb->balanced_dirty_ratelimit, |
| 1308 | balanced_dirty_ratelimit, task_ratelimit); |
| 1309 | if (dirty_ratelimit < x) |
| 1310 | step = x - dirty_ratelimit; |
| 1311 | } else { |
| 1312 | x = max3(wb->balanced_dirty_ratelimit, |
| 1313 | balanced_dirty_ratelimit, task_ratelimit); |
| 1314 | if (dirty_ratelimit > x) |
| 1315 | step = dirty_ratelimit - x; |
| 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * Don't pursue 100% rate matching. It's impossible since the balanced |
| 1320 | * rate itself is constantly fluctuating. So decrease the track speed |
| 1321 | * when it gets close to the target. Helps eliminate pointless tremors. |
| 1322 | */ |
| 1323 | step >>= dirty_ratelimit / (2 * step + 1); |
| 1324 | /* |
| 1325 | * Limit the tracking speed to avoid overshooting. |
| 1326 | */ |
| 1327 | step = (step + 7) / 8; |
| 1328 | |
| 1329 | if (dirty_ratelimit < balanced_dirty_ratelimit) |
| 1330 | dirty_ratelimit += step; |
| 1331 | else |
| 1332 | dirty_ratelimit -= step; |
| 1333 | |
| 1334 | wb->dirty_ratelimit = max(dirty_ratelimit, 1UL); |
| 1335 | wb->balanced_dirty_ratelimit = balanced_dirty_ratelimit; |
| 1336 | |
| 1337 | trace_bdi_dirty_ratelimit(wb, dirty_rate, task_ratelimit); |
| 1338 | } |
| 1339 | |
| 1340 | static void __wb_update_bandwidth(struct dirty_throttle_control *gdtc, |
| 1341 | struct dirty_throttle_control *mdtc, |
| 1342 | unsigned long start_time, |
| 1343 | bool update_ratelimit) |
| 1344 | { |
| 1345 | struct bdi_writeback *wb = gdtc->wb; |
| 1346 | unsigned long now = jiffies; |
| 1347 | unsigned long elapsed = now - wb->bw_time_stamp; |
| 1348 | unsigned long dirtied; |
| 1349 | unsigned long written; |
| 1350 | |
| 1351 | lockdep_assert_held(&wb->list_lock); |
| 1352 | |
| 1353 | /* |
| 1354 | * rate-limit, only update once every 200ms. |
| 1355 | */ |
| 1356 | if (elapsed < BANDWIDTH_INTERVAL) |
| 1357 | return; |
| 1358 | |
| 1359 | dirtied = percpu_counter_read(&wb->stat[WB_DIRTIED]); |
| 1360 | written = percpu_counter_read(&wb->stat[WB_WRITTEN]); |
| 1361 | |
| 1362 | /* |
| 1363 | * Skip quiet periods when disk bandwidth is under-utilized. |
| 1364 | * (at least 1s idle time between two flusher runs) |
| 1365 | */ |
| 1366 | if (elapsed > HZ && time_before(wb->bw_time_stamp, start_time)) |
| 1367 | goto snapshot; |
| 1368 | |
| 1369 | if (update_ratelimit) { |
| 1370 | domain_update_bandwidth(gdtc, now); |
| 1371 | wb_update_dirty_ratelimit(gdtc, dirtied, elapsed); |
| 1372 | |
| 1373 | /* |
| 1374 | * @mdtc is always NULL if !CGROUP_WRITEBACK but the |
| 1375 | * compiler has no way to figure that out. Help it. |
| 1376 | */ |
| 1377 | if (IS_ENABLED(CONFIG_CGROUP_WRITEBACK) && mdtc) { |
| 1378 | domain_update_bandwidth(mdtc, now); |
| 1379 | wb_update_dirty_ratelimit(mdtc, dirtied, elapsed); |
| 1380 | } |
| 1381 | } |
| 1382 | wb_update_write_bandwidth(wb, elapsed, written); |
| 1383 | |
| 1384 | snapshot: |
| 1385 | wb->dirtied_stamp = dirtied; |
| 1386 | wb->written_stamp = written; |
| 1387 | wb->bw_time_stamp = now; |
| 1388 | } |
| 1389 | |
| 1390 | void wb_update_bandwidth(struct bdi_writeback *wb, unsigned long start_time) |
| 1391 | { |
| 1392 | struct dirty_throttle_control gdtc = { GDTC_INIT(wb) }; |
| 1393 | |
| 1394 | __wb_update_bandwidth(&gdtc, NULL, start_time, false); |
| 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * After a task dirtied this many pages, balance_dirty_pages_ratelimited() |
| 1399 | * will look to see if it needs to start dirty throttling. |
| 1400 | * |
| 1401 | * If dirty_poll_interval is too low, big NUMA machines will call the expensive |
| 1402 | * global_page_state() too often. So scale it near-sqrt to the safety margin |
| 1403 | * (the number of pages we may dirty without exceeding the dirty limits). |
| 1404 | */ |
| 1405 | static unsigned long dirty_poll_interval(unsigned long dirty, |
| 1406 | unsigned long thresh) |
| 1407 | { |
| 1408 | if (thresh > dirty) |
| 1409 | return 1UL << (ilog2(thresh - dirty) >> 1); |
| 1410 | |
| 1411 | return 1; |
| 1412 | } |
| 1413 | |
| 1414 | static unsigned long wb_max_pause(struct bdi_writeback *wb, |
| 1415 | unsigned long wb_dirty) |
| 1416 | { |
| 1417 | unsigned long bw = wb->avg_write_bandwidth; |
| 1418 | unsigned long t; |
| 1419 | |
| 1420 | /* |
| 1421 | * Limit pause time for small memory systems. If sleeping for too long |
| 1422 | * time, a small pool of dirty/writeback pages may go empty and disk go |
| 1423 | * idle. |
| 1424 | * |
| 1425 | * 8 serves as the safety ratio. |
| 1426 | */ |
| 1427 | t = wb_dirty / (1 + bw / roundup_pow_of_two(1 + HZ / 8)); |
| 1428 | t++; |
| 1429 | |
| 1430 | return min_t(unsigned long, t, MAX_PAUSE); |
| 1431 | } |
| 1432 | |
| 1433 | static long wb_min_pause(struct bdi_writeback *wb, |
| 1434 | long max_pause, |
| 1435 | unsigned long task_ratelimit, |
| 1436 | unsigned long dirty_ratelimit, |
| 1437 | int *nr_dirtied_pause) |
| 1438 | { |
| 1439 | long hi = ilog2(wb->avg_write_bandwidth); |
| 1440 | long lo = ilog2(wb->dirty_ratelimit); |
| 1441 | long t; /* target pause */ |
| 1442 | long pause; /* estimated next pause */ |
| 1443 | int pages; /* target nr_dirtied_pause */ |
| 1444 | |
| 1445 | /* target for 10ms pause on 1-dd case */ |
| 1446 | t = max(1, HZ / 100); |
| 1447 | |
| 1448 | /* |
| 1449 | * Scale up pause time for concurrent dirtiers in order to reduce CPU |
| 1450 | * overheads. |
| 1451 | * |
| 1452 | * (N * 10ms) on 2^N concurrent tasks. |
| 1453 | */ |
| 1454 | if (hi > lo) |
| 1455 | t += (hi - lo) * (10 * HZ) / 1024; |
| 1456 | |
| 1457 | /* |
| 1458 | * This is a bit convoluted. We try to base the next nr_dirtied_pause |
| 1459 | * on the much more stable dirty_ratelimit. However the next pause time |
| 1460 | * will be computed based on task_ratelimit and the two rate limits may |
| 1461 | * depart considerably at some time. Especially if task_ratelimit goes |
| 1462 | * below dirty_ratelimit/2 and the target pause is max_pause, the next |
| 1463 | * pause time will be max_pause*2 _trimmed down_ to max_pause. As a |
| 1464 | * result task_ratelimit won't be executed faithfully, which could |
| 1465 | * eventually bring down dirty_ratelimit. |
| 1466 | * |
| 1467 | * We apply two rules to fix it up: |
| 1468 | * 1) try to estimate the next pause time and if necessary, use a lower |
| 1469 | * nr_dirtied_pause so as not to exceed max_pause. When this happens, |
| 1470 | * nr_dirtied_pause will be "dancing" with task_ratelimit. |
| 1471 | * 2) limit the target pause time to max_pause/2, so that the normal |
| 1472 | * small fluctuations of task_ratelimit won't trigger rule (1) and |
| 1473 | * nr_dirtied_pause will remain as stable as dirty_ratelimit. |
| 1474 | */ |
| 1475 | t = min(t, 1 + max_pause / 2); |
| 1476 | pages = dirty_ratelimit * t / roundup_pow_of_two(HZ); |
| 1477 | |
| 1478 | /* |
| 1479 | * Tiny nr_dirtied_pause is found to hurt I/O performance in the test |
| 1480 | * case fio-mmap-randwrite-64k, which does 16*{sync read, async write}. |
| 1481 | * When the 16 consecutive reads are often interrupted by some dirty |
| 1482 | * throttling pause during the async writes, cfq will go into idles |
| 1483 | * (deadline is fine). So push nr_dirtied_pause as high as possible |
| 1484 | * until reaches DIRTY_POLL_THRESH=32 pages. |
| 1485 | */ |
| 1486 | if (pages < DIRTY_POLL_THRESH) { |
| 1487 | t = max_pause; |
| 1488 | pages = dirty_ratelimit * t / roundup_pow_of_two(HZ); |
| 1489 | if (pages > DIRTY_POLL_THRESH) { |
| 1490 | pages = DIRTY_POLL_THRESH; |
| 1491 | t = HZ * DIRTY_POLL_THRESH / dirty_ratelimit; |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | pause = HZ * pages / (task_ratelimit + 1); |
| 1496 | if (pause > max_pause) { |
| 1497 | t = max_pause; |
| 1498 | pages = task_ratelimit * t / roundup_pow_of_two(HZ); |
| 1499 | } |
| 1500 | |
| 1501 | *nr_dirtied_pause = pages; |
| 1502 | /* |
| 1503 | * The minimal pause time will normally be half the target pause time. |
| 1504 | */ |
| 1505 | return pages >= DIRTY_POLL_THRESH ? 1 + t / 2 : t; |
| 1506 | } |
| 1507 | |
| 1508 | static inline void wb_dirty_limits(struct dirty_throttle_control *dtc) |
| 1509 | { |
| 1510 | struct bdi_writeback *wb = dtc->wb; |
| 1511 | unsigned long wb_reclaimable; |
| 1512 | |
| 1513 | /* |
| 1514 | * wb_thresh is not treated as some limiting factor as |
| 1515 | * dirty_thresh, due to reasons |
| 1516 | * - in JBOD setup, wb_thresh can fluctuate a lot |
| 1517 | * - in a system with HDD and USB key, the USB key may somehow |
| 1518 | * go into state (wb_dirty >> wb_thresh) either because |
| 1519 | * wb_dirty starts high, or because wb_thresh drops low. |
| 1520 | * In this case we don't want to hard throttle the USB key |
| 1521 | * dirtiers for 100 seconds until wb_dirty drops under |
| 1522 | * wb_thresh. Instead the auxiliary wb control line in |
| 1523 | * wb_position_ratio() will let the dirtier task progress |
| 1524 | * at some rate <= (write_bw / 2) for bringing down wb_dirty. |
| 1525 | */ |
| 1526 | dtc->wb_thresh = __wb_calc_thresh(dtc); |
| 1527 | dtc->wb_bg_thresh = dtc->thresh ? |
| 1528 | div_u64((u64)dtc->wb_thresh * dtc->bg_thresh, dtc->thresh) : 0; |
| 1529 | |
| 1530 | /* |
| 1531 | * In order to avoid the stacked BDI deadlock we need |
| 1532 | * to ensure we accurately count the 'dirty' pages when |
| 1533 | * the threshold is low. |
| 1534 | * |
| 1535 | * Otherwise it would be possible to get thresh+n pages |
| 1536 | * reported dirty, even though there are thresh-m pages |
| 1537 | * actually dirty; with m+n sitting in the percpu |
| 1538 | * deltas. |
| 1539 | */ |
| 1540 | if (dtc->wb_thresh < 2 * wb_stat_error(wb)) { |
| 1541 | wb_reclaimable = wb_stat_sum(wb, WB_RECLAIMABLE); |
| 1542 | dtc->wb_dirty = wb_reclaimable + wb_stat_sum(wb, WB_WRITEBACK); |
| 1543 | } else { |
| 1544 | wb_reclaimable = wb_stat(wb, WB_RECLAIMABLE); |
| 1545 | dtc->wb_dirty = wb_reclaimable + wb_stat(wb, WB_WRITEBACK); |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * balance_dirty_pages() must be called by processes which are generating dirty |
| 1551 | * data. It looks at the number of dirty pages in the machine and will force |
| 1552 | * the caller to wait once crossing the (background_thresh + dirty_thresh) / 2. |
| 1553 | * If we're over `background_thresh' then the writeback threads are woken to |
| 1554 | * perform some writeout. |
| 1555 | */ |
| 1556 | |
| 1557 | SIO_PATCH_VERSION(prevent_infinite_writeback, 1, 0, ""); |
| 1558 | |
| 1559 | static void balance_dirty_pages(struct address_space *mapping, |
| 1560 | struct bdi_writeback *wb, |
| 1561 | unsigned long pages_dirtied) |
| 1562 | { |
| 1563 | struct dirty_throttle_control gdtc_stor = { GDTC_INIT(wb) }; |
| 1564 | struct dirty_throttle_control mdtc_stor = { MDTC_INIT(wb, &gdtc_stor) }; |
| 1565 | struct dirty_throttle_control * const gdtc = &gdtc_stor; |
| 1566 | struct dirty_throttle_control * const mdtc = mdtc_valid(&mdtc_stor) ? |
| 1567 | &mdtc_stor : NULL; |
| 1568 | struct dirty_throttle_control *sdtc; |
| 1569 | unsigned long nr_reclaimable; /* = file_dirty + unstable_nfs */ |
| 1570 | long period; |
| 1571 | long pause; |
| 1572 | long max_pause; |
| 1573 | long min_pause; |
| 1574 | int nr_dirtied_pause; |
| 1575 | bool dirty_exceeded = false; |
| 1576 | unsigned long task_ratelimit; |
| 1577 | unsigned long dirty_ratelimit; |
| 1578 | struct backing_dev_info *bdi = wb->bdi; |
| 1579 | bool strictlimit = bdi->capabilities & BDI_CAP_STRICTLIMIT; |
| 1580 | unsigned long start_time = jiffies; |
| 1581 | |
| 1582 | for (;;) { |
| 1583 | unsigned long now = jiffies; |
| 1584 | unsigned long dirty, thresh, bg_thresh; |
| 1585 | unsigned long m_dirty = 0; /* stop bogus uninit warnings */ |
| 1586 | unsigned long m_thresh = 0; |
| 1587 | unsigned long m_bg_thresh = 0; |
| 1588 | |
| 1589 | /* |
| 1590 | * Unstable writes are a feature of certain networked |
| 1591 | * filesystems (i.e. NFS) in which data may have been |
| 1592 | * written to the server's write cache, but has not yet |
| 1593 | * been flushed to permanent storage. |
| 1594 | */ |
| 1595 | nr_reclaimable = global_page_state(NR_FILE_DIRTY) + |
| 1596 | global_page_state(NR_UNSTABLE_NFS); |
| 1597 | gdtc->avail = global_dirtyable_memory(); |
| 1598 | gdtc->dirty = nr_reclaimable + global_page_state(NR_WRITEBACK); |
| 1599 | |
| 1600 | domain_dirty_limits(gdtc); |
| 1601 | |
| 1602 | if (unlikely(strictlimit)) { |
| 1603 | wb_dirty_limits(gdtc); |
| 1604 | |
| 1605 | dirty = gdtc->wb_dirty; |
| 1606 | thresh = gdtc->wb_thresh; |
| 1607 | bg_thresh = gdtc->wb_bg_thresh; |
| 1608 | } else { |
| 1609 | dirty = gdtc->dirty; |
| 1610 | thresh = gdtc->thresh; |
| 1611 | bg_thresh = gdtc->bg_thresh; |
| 1612 | } |
| 1613 | |
| 1614 | if (mdtc) { |
| 1615 | unsigned long filepages, headroom, writeback; |
| 1616 | |
| 1617 | /* |
| 1618 | * If @wb belongs to !root memcg, repeat the same |
| 1619 | * basic calculations for the memcg domain. |
| 1620 | */ |
| 1621 | mem_cgroup_wb_stats(wb, &filepages, &headroom, |
| 1622 | &mdtc->dirty, &writeback); |
| 1623 | mdtc->dirty += writeback; |
| 1624 | mdtc_calc_avail(mdtc, filepages, headroom); |
| 1625 | |
| 1626 | domain_dirty_limits(mdtc); |
| 1627 | |
| 1628 | if (unlikely(strictlimit)) { |
| 1629 | wb_dirty_limits(mdtc); |
| 1630 | m_dirty = mdtc->wb_dirty; |
| 1631 | m_thresh = mdtc->wb_thresh; |
| 1632 | m_bg_thresh = mdtc->wb_bg_thresh; |
| 1633 | } else { |
| 1634 | m_dirty = mdtc->dirty; |
| 1635 | m_thresh = mdtc->thresh; |
| 1636 | m_bg_thresh = mdtc->bg_thresh; |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * Throttle it only when the background writeback cannot |
| 1642 | * catch-up. This avoids (excessively) small writeouts |
| 1643 | * when the wb limits are ramping up in case of !strictlimit. |
| 1644 | * |
| 1645 | * In strictlimit case make decision based on the wb counters |
| 1646 | * and limits. Small writeouts when the wb limits are ramping |
| 1647 | * up are the price we consciously pay for strictlimit-ing. |
| 1648 | * |
| 1649 | * If memcg domain is in effect, @dirty should be under |
| 1650 | * both global and memcg freerun ceilings. |
| 1651 | */ |
| 1652 | if (dirty <= dirty_freerun_ceiling(thresh, bg_thresh) && |
| 1653 | (!mdtc || |
| 1654 | m_dirty <= dirty_freerun_ceiling(m_thresh, m_bg_thresh))) { |
| 1655 | unsigned long intv = dirty_poll_interval(dirty, thresh); |
| 1656 | unsigned long m_intv = ULONG_MAX; |
| 1657 | |
| 1658 | current->dirty_paused_when = now; |
| 1659 | current->nr_dirtied = 0; |
| 1660 | if (mdtc) |
| 1661 | m_intv = dirty_poll_interval(m_dirty, m_thresh); |
| 1662 | current->nr_dirtied_pause = min(intv, m_intv); |
| 1663 | break; |
| 1664 | } |
| 1665 | |
| 1666 | if (unlikely(!writeback_in_progress(wb))) |
| 1667 | wb_start_background_writeback(wb); |
| 1668 | |
| 1669 | /* |
| 1670 | * Calculate global domain's pos_ratio and select the |
| 1671 | * global dtc by default. |
| 1672 | */ |
| 1673 | if (!strictlimit) |
| 1674 | wb_dirty_limits(gdtc); |
| 1675 | |
| 1676 | dirty_exceeded = (gdtc->wb_dirty > gdtc->wb_thresh) && |
| 1677 | ((gdtc->dirty > gdtc->thresh) || strictlimit); |
| 1678 | |
| 1679 | wb_position_ratio(gdtc); |
| 1680 | sdtc = gdtc; |
| 1681 | |
| 1682 | if (mdtc) { |
| 1683 | /* |
| 1684 | * If memcg domain is in effect, calculate its |
| 1685 | * pos_ratio. @wb should satisfy constraints from |
| 1686 | * both global and memcg domains. Choose the one |
| 1687 | * w/ lower pos_ratio. |
| 1688 | */ |
| 1689 | if (!strictlimit) |
| 1690 | wb_dirty_limits(mdtc); |
| 1691 | |
| 1692 | dirty_exceeded |= (mdtc->wb_dirty > mdtc->wb_thresh) && |
| 1693 | ((mdtc->dirty > mdtc->thresh) || strictlimit); |
| 1694 | |
| 1695 | wb_position_ratio(mdtc); |
| 1696 | if (mdtc->pos_ratio < gdtc->pos_ratio) |
| 1697 | sdtc = mdtc; |
| 1698 | } |
| 1699 | |
| 1700 | if (dirty_exceeded && !wb->dirty_exceeded) |
| 1701 | wb->dirty_exceeded = 1; |
| 1702 | |
| 1703 | if (time_is_before_jiffies(wb->bw_time_stamp + |
| 1704 | BANDWIDTH_INTERVAL)) { |
| 1705 | spin_lock(&wb->list_lock); |
| 1706 | __wb_update_bandwidth(gdtc, mdtc, start_time, true); |
| 1707 | spin_unlock(&wb->list_lock); |
| 1708 | } |
| 1709 | |
| 1710 | /* throttle according to the chosen dtc */ |
| 1711 | dirty_ratelimit = wb->dirty_ratelimit; |
| 1712 | task_ratelimit = ((u64)dirty_ratelimit * sdtc->pos_ratio) >> |
| 1713 | RATELIMIT_CALC_SHIFT; |
| 1714 | max_pause = wb_max_pause(wb, sdtc->wb_dirty); |
| 1715 | min_pause = wb_min_pause(wb, max_pause, |
| 1716 | task_ratelimit, dirty_ratelimit, |
| 1717 | &nr_dirtied_pause); |
| 1718 | |
| 1719 | if (unlikely(task_ratelimit == 0)) { |
| 1720 | period = max_pause; |
| 1721 | pause = max_pause; |
| 1722 | goto pause; |
| 1723 | } |
| 1724 | period = HZ * pages_dirtied / task_ratelimit; |
| 1725 | pause = period; |
| 1726 | if (current->dirty_paused_when) |
| 1727 | pause -= now - current->dirty_paused_when; |
| 1728 | /* |
| 1729 | * For less than 1s think time (ext3/4 may block the dirtier |
| 1730 | * for up to 800ms from time to time on 1-HDD; so does xfs, |
| 1731 | * however at much less frequency), try to compensate it in |
| 1732 | * future periods by updating the virtual time; otherwise just |
| 1733 | * do a reset, as it may be a light dirtier. |
| 1734 | */ |
| 1735 | if (pause < min_pause) { |
| 1736 | trace_balance_dirty_pages(wb, |
| 1737 | sdtc->thresh, |
| 1738 | sdtc->bg_thresh, |
| 1739 | sdtc->dirty, |
| 1740 | sdtc->wb_thresh, |
| 1741 | sdtc->wb_dirty, |
| 1742 | dirty_ratelimit, |
| 1743 | task_ratelimit, |
| 1744 | pages_dirtied, |
| 1745 | period, |
| 1746 | min(pause, 0L), |
| 1747 | start_time); |
| 1748 | if (pause < -HZ) { |
| 1749 | current->dirty_paused_when = now; |
| 1750 | current->nr_dirtied = 0; |
| 1751 | } else if (period) { |
| 1752 | current->dirty_paused_when += period; |
| 1753 | current->nr_dirtied = 0; |
| 1754 | } else if (current->nr_dirtied_pause <= pages_dirtied) |
| 1755 | current->nr_dirtied_pause += pages_dirtied; |
| 1756 | break; |
| 1757 | } |
| 1758 | if (unlikely(pause > max_pause)) { |
| 1759 | /* for occasional dropped task_ratelimit */ |
| 1760 | now += min(pause - max_pause, max_pause); |
| 1761 | pause = max_pause; |
| 1762 | } |
| 1763 | |
| 1764 | pause: |
| 1765 | trace_balance_dirty_pages(wb, |
| 1766 | sdtc->thresh, |
| 1767 | sdtc->bg_thresh, |
| 1768 | sdtc->dirty, |
| 1769 | sdtc->wb_thresh, |
| 1770 | sdtc->wb_dirty, |
| 1771 | dirty_ratelimit, |
| 1772 | task_ratelimit, |
| 1773 | pages_dirtied, |
| 1774 | period, |
| 1775 | pause, |
| 1776 | start_time); |
| 1777 | |
| 1778 | /* Do not sleep if the backing device is removed */ |
| 1779 | if (unlikely(!bdi->dev)) |
| 1780 | return; |
| 1781 | |
| 1782 | /* Just collecting approximate value. No lock required. */ |
| 1783 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)) |
| 1784 | bdi->last_thresh = thresh; |
| 1785 | bdi->last_nr_dirty = dirty; |
| 1786 | #else |
| 1787 | bdi->last_thresh = dirty_thresh; |
| 1788 | bdi->last_nr_dirty = nr_dirty; |
| 1789 | #endif |
| 1790 | bdi->paused_total += pause; |
| 1791 | |
| 1792 | __set_current_state(TASK_KILLABLE); |
| 1793 | io_schedule_timeout(pause); |
| 1794 | |
| 1795 | current->dirty_paused_when = now + pause; |
| 1796 | current->nr_dirtied = 0; |
| 1797 | current->nr_dirtied_pause = nr_dirtied_pause; |
| 1798 | |
| 1799 | /* |
| 1800 | * This is typically equal to (dirty < thresh) and can also |
| 1801 | * keep "1000+ dd on a slow USB stick" under control. |
| 1802 | */ |
| 1803 | if (task_ratelimit) |
| 1804 | break; |
| 1805 | |
| 1806 | /* |
| 1807 | * In the case of an unresponding NFS server and the NFS dirty |
| 1808 | * pages exceeds dirty_thresh, give the other good wb's a pipe |
| 1809 | * to go through, so that tasks on them still remain responsive. |
| 1810 | * |
| 1811 | * In theory 1 page is enough to keep the comsumer-producer |
| 1812 | * pipe going: the flusher cleans 1 page => the task dirties 1 |
| 1813 | * more page. However wb_dirty has accounting errors. So use |
| 1814 | * the larger and more IO friendly wb_stat_error. |
| 1815 | */ |
| 1816 | if (sdtc->wb_dirty <= wb_stat_error(wb)) |
| 1817 | break; |
| 1818 | |
| 1819 | if (fatal_signal_pending(current)) |
| 1820 | break; |
| 1821 | } |
| 1822 | |
| 1823 | if (!dirty_exceeded && wb->dirty_exceeded) |
| 1824 | wb->dirty_exceeded = 0; |
| 1825 | |
| 1826 | if (writeback_in_progress(wb)) |
| 1827 | return; |
| 1828 | |
| 1829 | /* |
| 1830 | * In laptop mode, we wait until hitting the higher threshold before |
| 1831 | * starting background writeout, and then write out all the way down |
| 1832 | * to the lower threshold. So slow writers cause minimal disk activity. |
| 1833 | * |
| 1834 | * In normal mode, we start background writeout at the lower |
| 1835 | * background_thresh, to keep the amount of dirty memory low. |
| 1836 | */ |
| 1837 | if (laptop_mode) |
| 1838 | return; |
| 1839 | |
| 1840 | if (nr_reclaimable > gdtc->bg_thresh) |
| 1841 | wb_start_background_writeback(wb); |
| 1842 | } |
| 1843 | |
| 1844 | static DEFINE_PER_CPU(int, bdp_ratelimits); |
| 1845 | |
| 1846 | /* |
| 1847 | * Normal tasks are throttled by |
| 1848 | * loop { |
| 1849 | * dirty tsk->nr_dirtied_pause pages; |
| 1850 | * take a snap in balance_dirty_pages(); |
| 1851 | * } |
| 1852 | * However there is a worst case. If every task exit immediately when dirtied |
| 1853 | * (tsk->nr_dirtied_pause - 1) pages, balance_dirty_pages() will never be |
| 1854 | * called to throttle the page dirties. The solution is to save the not yet |
| 1855 | * throttled page dirties in dirty_throttle_leaks on task exit and charge them |
| 1856 | * randomly into the running tasks. This works well for the above worst case, |
| 1857 | * as the new task will pick up and accumulate the old task's leaked dirty |
| 1858 | * count and eventually get throttled. |
| 1859 | */ |
| 1860 | DEFINE_PER_CPU(int, dirty_throttle_leaks) = 0; |
| 1861 | |
| 1862 | /** |
| 1863 | * balance_dirty_pages_ratelimited - balance dirty memory state |
| 1864 | * @mapping: address_space which was dirtied |
| 1865 | * |
| 1866 | * Processes which are dirtying memory should call in here once for each page |
| 1867 | * which was newly dirtied. The function will periodically check the system's |
| 1868 | * dirty state and will initiate writeback if needed. |
| 1869 | * |
| 1870 | * On really big machines, get_writeback_state is expensive, so try to avoid |
| 1871 | * calling it too often (ratelimiting). But once we're over the dirty memory |
| 1872 | * limit we decrease the ratelimiting by a lot, to prevent individual processes |
| 1873 | * from overshooting the limit by (ratelimit_pages) each. |
| 1874 | */ |
| 1875 | void balance_dirty_pages_ratelimited(struct address_space *mapping) |
| 1876 | { |
| 1877 | struct inode *inode = mapping->host; |
| 1878 | struct backing_dev_info *bdi = inode_to_bdi(inode); |
| 1879 | struct bdi_writeback *wb = NULL; |
| 1880 | int ratelimit; |
| 1881 | int *p; |
| 1882 | |
| 1883 | if (!bdi_cap_account_dirty(bdi)) |
| 1884 | return; |
| 1885 | |
| 1886 | if (inode_cgwb_enabled(inode)) |
| 1887 | wb = wb_get_create_current(bdi, GFP_KERNEL); |
| 1888 | if (!wb) |
| 1889 | wb = &bdi->wb; |
| 1890 | |
| 1891 | ratelimit = current->nr_dirtied_pause; |
| 1892 | if (wb->dirty_exceeded) |
| 1893 | ratelimit = min(ratelimit, 32 >> (PAGE_SHIFT - 10)); |
| 1894 | |
| 1895 | preempt_disable(); |
| 1896 | /* |
| 1897 | * This prevents one CPU to accumulate too many dirtied pages without |
| 1898 | * calling into balance_dirty_pages(), which can happen when there are |
| 1899 | * 1000+ tasks, all of them start dirtying pages at exactly the same |
| 1900 | * time, hence all honoured too large initial task->nr_dirtied_pause. |
| 1901 | */ |
| 1902 | p = this_cpu_ptr(&bdp_ratelimits); |
| 1903 | if (unlikely(current->nr_dirtied >= ratelimit)) |
| 1904 | *p = 0; |
| 1905 | else if (unlikely(*p >= ratelimit_pages)) { |
| 1906 | *p = 0; |
| 1907 | ratelimit = 0; |
| 1908 | } |
| 1909 | /* |
| 1910 | * Pick up the dirtied pages by the exited tasks. This avoids lots of |
| 1911 | * short-lived tasks (eg. gcc invocations in a kernel build) escaping |
| 1912 | * the dirty throttling and livelock other long-run dirtiers. |
| 1913 | */ |
| 1914 | p = this_cpu_ptr(&dirty_throttle_leaks); |
| 1915 | if (*p > 0 && current->nr_dirtied < ratelimit) { |
| 1916 | unsigned long nr_pages_dirtied; |
| 1917 | nr_pages_dirtied = min(*p, ratelimit - current->nr_dirtied); |
| 1918 | *p -= nr_pages_dirtied; |
| 1919 | current->nr_dirtied += nr_pages_dirtied; |
| 1920 | } |
| 1921 | preempt_enable(); |
| 1922 | |
| 1923 | if (unlikely(current->nr_dirtied >= ratelimit)) |
| 1924 | balance_dirty_pages(mapping, wb, current->nr_dirtied); |
| 1925 | |
| 1926 | wb_put(wb); |
| 1927 | } |
| 1928 | EXPORT_SYMBOL(balance_dirty_pages_ratelimited); |
| 1929 | |
| 1930 | /** |
| 1931 | * wb_over_bg_thresh - does @wb need to be written back? |
| 1932 | * @wb: bdi_writeback of interest |
| 1933 | * |
| 1934 | * Determines whether background writeback should keep writing @wb or it's |
| 1935 | * clean enough. Returns %true if writeback should continue. |
| 1936 | */ |
| 1937 | bool wb_over_bg_thresh(struct bdi_writeback *wb) |
| 1938 | { |
| 1939 | struct dirty_throttle_control gdtc_stor = { GDTC_INIT(wb) }; |
| 1940 | struct dirty_throttle_control mdtc_stor = { MDTC_INIT(wb, &gdtc_stor) }; |
| 1941 | struct dirty_throttle_control * const gdtc = &gdtc_stor; |
| 1942 | struct dirty_throttle_control * const mdtc = mdtc_valid(&mdtc_stor) ? |
| 1943 | &mdtc_stor : NULL; |
| 1944 | |
| 1945 | /* |
| 1946 | * Similar to balance_dirty_pages() but ignores pages being written |
| 1947 | * as we're trying to decide whether to put more under writeback. |
| 1948 | */ |
| 1949 | gdtc->avail = global_dirtyable_memory(); |
| 1950 | gdtc->dirty = global_page_state(NR_FILE_DIRTY) + |
| 1951 | global_page_state(NR_UNSTABLE_NFS); |
| 1952 | domain_dirty_limits(gdtc); |
| 1953 | |
| 1954 | if (gdtc->dirty > gdtc->bg_thresh) |
| 1955 | return true; |
| 1956 | |
| 1957 | if (wb_stat(wb, WB_RECLAIMABLE) > |
| 1958 | wb_calc_thresh(gdtc->wb, gdtc->bg_thresh)) |
| 1959 | return true; |
| 1960 | |
| 1961 | if (mdtc) { |
| 1962 | unsigned long filepages, headroom, writeback; |
| 1963 | |
| 1964 | mem_cgroup_wb_stats(wb, &filepages, &headroom, &mdtc->dirty, |
| 1965 | &writeback); |
| 1966 | mdtc_calc_avail(mdtc, filepages, headroom); |
| 1967 | domain_dirty_limits(mdtc); /* ditto, ignore writeback */ |
| 1968 | |
| 1969 | if (mdtc->dirty > mdtc->bg_thresh) |
| 1970 | return true; |
| 1971 | |
| 1972 | if (wb_stat(wb, WB_RECLAIMABLE) > |
| 1973 | wb_calc_thresh(mdtc->wb, mdtc->bg_thresh)) |
| 1974 | return true; |
| 1975 | } |
| 1976 | |
| 1977 | return false; |
| 1978 | } |
| 1979 | |
| 1980 | void throttle_vm_writeout(gfp_t gfp_mask) |
| 1981 | { |
| 1982 | unsigned long background_thresh; |
| 1983 | unsigned long dirty_thresh; |
| 1984 | |
| 1985 | for ( ; ; ) { |
| 1986 | global_dirty_limits(&background_thresh, &dirty_thresh); |
| 1987 | dirty_thresh = hard_dirty_limit(&global_wb_domain, dirty_thresh); |
| 1988 | |
| 1989 | /* |
| 1990 | * Boost the allowable dirty threshold a bit for page |
| 1991 | * allocators so they don't get DoS'ed by heavy writers |
| 1992 | */ |
| 1993 | dirty_thresh += dirty_thresh / 10; /* wheeee... */ |
| 1994 | |
| 1995 | if (global_page_state(NR_UNSTABLE_NFS) + |
| 1996 | global_page_state(NR_WRITEBACK) <= dirty_thresh) |
| 1997 | break; |
| 1998 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
| 1999 | |
| 2000 | /* |
| 2001 | * The caller might hold locks which can prevent IO completion |
| 2002 | * or progress in the filesystem. So we cannot just sit here |
| 2003 | * waiting for IO to complete. |
| 2004 | */ |
| 2005 | if ((gfp_mask & (__GFP_FS|__GFP_IO)) != (__GFP_FS|__GFP_IO)) |
| 2006 | break; |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | /* |
| 2011 | * sysctl handler for /proc/sys/vm/dirty_writeback_centisecs |
| 2012 | */ |
| 2013 | int dirty_writeback_centisecs_handler(struct ctl_table *table, int write, |
| 2014 | void __user *buffer, size_t *length, loff_t *ppos) |
| 2015 | { |
| 2016 | proc_dointvec(table, write, buffer, length, ppos); |
| 2017 | return 0; |
| 2018 | } |
| 2019 | |
| 2020 | #ifdef CONFIG_BLOCK |
| 2021 | void laptop_mode_timer_fn(unsigned long data) |
| 2022 | { |
| 2023 | struct request_queue *q = (struct request_queue *)data; |
| 2024 | int nr_pages = global_page_state(NR_FILE_DIRTY) + |
| 2025 | global_page_state(NR_UNSTABLE_NFS); |
| 2026 | struct bdi_writeback *wb; |
| 2027 | |
| 2028 | /* |
| 2029 | * We want to write everything out, not just down to the dirty |
| 2030 | * threshold |
| 2031 | */ |
| 2032 | if (!bdi_has_dirty_io(&q->backing_dev_info)) |
| 2033 | return; |
| 2034 | |
| 2035 | rcu_read_lock(); |
| 2036 | list_for_each_entry_rcu(wb, &q->backing_dev_info.wb_list, bdi_node) |
| 2037 | if (wb_has_dirty_io(wb)) |
| 2038 | wb_start_writeback(wb, nr_pages, true, |
| 2039 | WB_REASON_LAPTOP_TIMER); |
| 2040 | rcu_read_unlock(); |
| 2041 | } |
| 2042 | |
| 2043 | /* |
| 2044 | * We've spun up the disk and we're in laptop mode: schedule writeback |
| 2045 | * of all dirty data a few seconds from now. If the flush is already scheduled |
| 2046 | * then push it back - the user is still using the disk. |
| 2047 | */ |
| 2048 | void laptop_io_completion(struct backing_dev_info *info) |
| 2049 | { |
| 2050 | mod_timer(&info->laptop_mode_wb_timer, jiffies + laptop_mode); |
| 2051 | } |
| 2052 | |
| 2053 | /* |
| 2054 | * We're in laptop mode and we've just synced. The sync's writes will have |
| 2055 | * caused another writeback to be scheduled by laptop_io_completion. |
| 2056 | * Nothing needs to be written back anymore, so we unschedule the writeback. |
| 2057 | */ |
| 2058 | void laptop_sync_completion(void) |
| 2059 | { |
| 2060 | struct backing_dev_info *bdi; |
| 2061 | |
| 2062 | rcu_read_lock(); |
| 2063 | |
| 2064 | list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) |
| 2065 | del_timer(&bdi->laptop_mode_wb_timer); |
| 2066 | |
| 2067 | rcu_read_unlock(); |
| 2068 | } |
| 2069 | #endif |
| 2070 | |
| 2071 | /* |
| 2072 | * If ratelimit_pages is too high then we can get into dirty-data overload |
| 2073 | * if a large number of processes all perform writes at the same time. |
| 2074 | * If it is too low then SMP machines will call the (expensive) |
| 2075 | * get_writeback_state too often. |
| 2076 | * |
| 2077 | * Here we set ratelimit_pages to a level which ensures that when all CPUs are |
| 2078 | * dirtying in parallel, we cannot go more than 3% (1/32) over the dirty memory |
| 2079 | * thresholds. |
| 2080 | */ |
| 2081 | |
| 2082 | void writeback_set_ratelimit(void) |
| 2083 | { |
| 2084 | struct wb_domain *dom = &global_wb_domain; |
| 2085 | unsigned long background_thresh; |
| 2086 | unsigned long dirty_thresh; |
| 2087 | |
| 2088 | global_dirty_limits(&background_thresh, &dirty_thresh); |
| 2089 | dom->dirty_limit = dirty_thresh; |
| 2090 | ratelimit_pages = dirty_thresh / (num_online_cpus() * 32); |
| 2091 | if (ratelimit_pages < 16) |
| 2092 | ratelimit_pages = 16; |
| 2093 | } |
| 2094 | |
| 2095 | static int |
| 2096 | ratelimit_handler(struct notifier_block *self, unsigned long action, |
| 2097 | void *hcpu) |
| 2098 | { |
| 2099 | |
| 2100 | switch (action & ~CPU_TASKS_FROZEN) { |
| 2101 | case CPU_ONLINE: |
| 2102 | case CPU_DEAD: |
| 2103 | writeback_set_ratelimit(); |
| 2104 | return NOTIFY_OK; |
| 2105 | default: |
| 2106 | return NOTIFY_DONE; |
| 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | static struct notifier_block ratelimit_nb = { |
| 2111 | .notifier_call = ratelimit_handler, |
| 2112 | .next = NULL, |
| 2113 | }; |
| 2114 | |
| 2115 | /* |
| 2116 | * Called early on to tune the page writeback dirty limits. |
| 2117 | * |
| 2118 | * We used to scale dirty pages according to how total memory |
| 2119 | * related to pages that could be allocated for buffers (by |
| 2120 | * comparing nr_free_buffer_pages() to vm_total_pages. |
| 2121 | * |
| 2122 | * However, that was when we used "dirty_ratio" to scale with |
| 2123 | * all memory, and we don't do that any more. "dirty_ratio" |
| 2124 | * is now applied to total non-HIGHPAGE memory (by subtracting |
| 2125 | * totalhigh_pages from vm_total_pages), and as such we can't |
| 2126 | * get into the old insane situation any more where we had |
| 2127 | * large amounts of dirty pages compared to a small amount of |
| 2128 | * non-HIGHMEM memory. |
| 2129 | * |
| 2130 | * But we might still want to scale the dirty_ratio by how |
| 2131 | * much memory the box has.. |
| 2132 | */ |
| 2133 | void __init page_writeback_init(void) |
| 2134 | { |
| 2135 | BUG_ON(wb_domain_init(&global_wb_domain, GFP_KERNEL)); |
| 2136 | |
| 2137 | writeback_set_ratelimit(); |
| 2138 | register_cpu_notifier(&ratelimit_nb); |
| 2139 | } |
| 2140 | |
| 2141 | /** |
| 2142 | * tag_pages_for_writeback - tag pages to be written by write_cache_pages |
| 2143 | * @mapping: address space structure to write |
| 2144 | * @start: starting page index |
| 2145 | * @end: ending page index (inclusive) |
| 2146 | * |
| 2147 | * This function scans the page range from @start to @end (inclusive) and tags |
| 2148 | * all pages that have DIRTY tag set with a special TOWRITE tag. The idea is |
| 2149 | * that write_cache_pages (or whoever calls this function) will then use |
| 2150 | * TOWRITE tag to identify pages eligible for writeback. This mechanism is |
| 2151 | * used to avoid livelocking of writeback by a process steadily creating new |
| 2152 | * dirty pages in the file (thus it is important for this function to be quick |
| 2153 | * so that it can tag pages faster than a dirtying process can create them). |
| 2154 | */ |
| 2155 | /* |
| 2156 | * We tag pages in batches of WRITEBACK_TAG_BATCH to reduce tree_lock latency. |
| 2157 | */ |
| 2158 | void tag_pages_for_writeback(struct address_space *mapping, |
| 2159 | pgoff_t start, pgoff_t end) |
| 2160 | { |
| 2161 | #define WRITEBACK_TAG_BATCH 4096 |
| 2162 | unsigned long tagged; |
| 2163 | |
| 2164 | do { |
| 2165 | spin_lock_irq(&mapping->tree_lock); |
| 2166 | tagged = radix_tree_range_tag_if_tagged(&mapping->page_tree, |
| 2167 | &start, end, WRITEBACK_TAG_BATCH, |
| 2168 | PAGECACHE_TAG_DIRTY, PAGECACHE_TAG_TOWRITE); |
| 2169 | spin_unlock_irq(&mapping->tree_lock); |
| 2170 | WARN_ON_ONCE(tagged > WRITEBACK_TAG_BATCH); |
| 2171 | cond_resched(); |
| 2172 | /* We check 'start' to handle wrapping when end == ~0UL */ |
| 2173 | } while (tagged >= WRITEBACK_TAG_BATCH && start); |
| 2174 | } |
| 2175 | EXPORT_SYMBOL(tag_pages_for_writeback); |
| 2176 | |
| 2177 | /** |
| 2178 | * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. |
| 2179 | * @mapping: address space structure to write |
| 2180 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 2181 | * @writepage: function called for each page |
| 2182 | * @data: data passed to writepage function |
| 2183 | * |
| 2184 | * If a page is already under I/O, write_cache_pages() skips it, even |
| 2185 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 2186 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 2187 | * and msync() need to guarantee that all the data which was dirty at the time |
| 2188 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 2189 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 2190 | * existing IO to complete. |
| 2191 | * |
| 2192 | * To avoid livelocks (when other process dirties new pages), we first tag |
| 2193 | * pages which should be written back with TOWRITE tag and only then start |
| 2194 | * writing them. For data-integrity sync we have to be careful so that we do |
| 2195 | * not miss some pages (e.g., because some other process has cleared TOWRITE |
| 2196 | * tag we set). The rule we follow is that TOWRITE tag can be cleared only |
| 2197 | * by the process clearing the DIRTY tag (and submitting the page for IO). |
| 2198 | */ |
| 2199 | int write_cache_pages(struct address_space *mapping, |
| 2200 | struct writeback_control *wbc, writepage_t writepage, |
| 2201 | void *data) |
| 2202 | { |
| 2203 | int ret = 0; |
| 2204 | int done = 0; |
| 2205 | struct pagevec pvec; |
| 2206 | int nr_pages; |
| 2207 | pgoff_t uninitialized_var(writeback_index); |
| 2208 | pgoff_t index; |
| 2209 | pgoff_t end; /* Inclusive */ |
| 2210 | pgoff_t done_index; |
| 2211 | int cycled; |
| 2212 | int range_whole = 0; |
| 2213 | int tag; |
| 2214 | |
| 2215 | pagevec_init(&pvec, 0); |
| 2216 | if (wbc->range_cyclic) { |
| 2217 | writeback_index = mapping->writeback_index; /* prev offset */ |
| 2218 | index = writeback_index; |
| 2219 | if (index == 0) |
| 2220 | cycled = 1; |
| 2221 | else |
| 2222 | cycled = 0; |
| 2223 | end = -1; |
| 2224 | } else { |
| 2225 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 2226 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 2227 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 2228 | range_whole = 1; |
| 2229 | cycled = 1; /* ignore range_cyclic tests */ |
| 2230 | } |
| 2231 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 2232 | tag = PAGECACHE_TAG_TOWRITE; |
| 2233 | else |
| 2234 | tag = PAGECACHE_TAG_DIRTY; |
| 2235 | retry: |
| 2236 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 2237 | tag_pages_for_writeback(mapping, index, end); |
| 2238 | done_index = index; |
| 2239 | while (!done && (index <= end)) { |
| 2240 | int i; |
| 2241 | |
| 2242 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 2243 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); |
| 2244 | if (nr_pages == 0) |
| 2245 | break; |
| 2246 | |
| 2247 | for (i = 0; i < nr_pages; i++) { |
| 2248 | struct page *page = pvec.pages[i]; |
| 2249 | |
| 2250 | /* |
| 2251 | * At this point, the page may be truncated or |
| 2252 | * invalidated (changing page->mapping to NULL), or |
| 2253 | * even swizzled back from swapper_space to tmpfs file |
| 2254 | * mapping. However, page->index will not change |
| 2255 | * because we have a reference on the page. |
| 2256 | */ |
| 2257 | if (page->index > end) { |
| 2258 | /* |
| 2259 | * can't be range_cyclic (1st pass) because |
| 2260 | * end == -1 in that case. |
| 2261 | */ |
| 2262 | done = 1; |
| 2263 | break; |
| 2264 | } |
| 2265 | |
| 2266 | done_index = page->index; |
| 2267 | |
| 2268 | lock_page(page); |
| 2269 | |
| 2270 | /* |
| 2271 | * Page truncated or invalidated. We can freely skip it |
| 2272 | * then, even for data integrity operations: the page |
| 2273 | * has disappeared concurrently, so there could be no |
| 2274 | * real expectation of this data interity operation |
| 2275 | * even if there is now a new, dirty page at the same |
| 2276 | * pagecache address. |
| 2277 | */ |
| 2278 | if (unlikely(page->mapping != mapping)) { |
| 2279 | continue_unlock: |
| 2280 | unlock_page(page); |
| 2281 | continue; |
| 2282 | } |
| 2283 | |
| 2284 | if (!PageDirty(page)) { |
| 2285 | /* someone wrote it for us */ |
| 2286 | goto continue_unlock; |
| 2287 | } |
| 2288 | |
| 2289 | if (PageWriteback(page)) { |
| 2290 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 2291 | wait_on_page_writeback(page); |
| 2292 | else |
| 2293 | goto continue_unlock; |
| 2294 | } |
| 2295 | |
| 2296 | BUG_ON(PageWriteback(page)); |
| 2297 | if (!clear_page_dirty_for_io(page)) |
| 2298 | goto continue_unlock; |
| 2299 | |
| 2300 | trace_wbc_writepage(wbc, inode_to_bdi(mapping->host)); |
| 2301 | ret = (*writepage)(page, wbc, data); |
| 2302 | if (unlikely(ret)) { |
| 2303 | if (ret == AOP_WRITEPAGE_ACTIVATE) { |
| 2304 | unlock_page(page); |
| 2305 | ret = 0; |
| 2306 | } else { |
| 2307 | /* |
| 2308 | * done_index is set past this page, |
| 2309 | * so media errors will not choke |
| 2310 | * background writeout for the entire |
| 2311 | * file. This has consequences for |
| 2312 | * range_cyclic semantics (ie. it may |
| 2313 | * not be suitable for data integrity |
| 2314 | * writeout). |
| 2315 | */ |
| 2316 | done_index = page->index + 1; |
| 2317 | done = 1; |
| 2318 | break; |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | /* |
| 2323 | * We stop writing back only if we are not doing |
| 2324 | * integrity sync. In case of integrity sync we have to |
| 2325 | * keep going until we have written all the pages |
| 2326 | * we tagged for writeback prior to entering this loop. |
| 2327 | */ |
| 2328 | if (--wbc->nr_to_write <= 0 && |
| 2329 | wbc->sync_mode == WB_SYNC_NONE) { |
| 2330 | done = 1; |
| 2331 | break; |
| 2332 | } |
| 2333 | } |
| 2334 | pagevec_release(&pvec); |
| 2335 | cond_resched(); |
| 2336 | } |
| 2337 | if (!cycled && !done) { |
| 2338 | /* |
| 2339 | * range_cyclic: |
| 2340 | * We hit the last page and there is more work to be done: wrap |
| 2341 | * back to the start of the file |
| 2342 | */ |
| 2343 | cycled = 1; |
| 2344 | index = 0; |
| 2345 | end = writeback_index - 1; |
| 2346 | goto retry; |
| 2347 | } |
| 2348 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 2349 | mapping->writeback_index = done_index; |
| 2350 | |
| 2351 | return ret; |
| 2352 | } |
| 2353 | EXPORT_SYMBOL(write_cache_pages); |
| 2354 | |
| 2355 | /* |
| 2356 | * Function used by generic_writepages to call the real writepage |
| 2357 | * function and set the mapping flags on error |
| 2358 | */ |
| 2359 | static int __writepage(struct page *page, struct writeback_control *wbc, |
| 2360 | void *data) |
| 2361 | { |
| 2362 | struct address_space *mapping = data; |
| 2363 | int ret = mapping->a_ops->writepage(page, wbc); |
| 2364 | mapping_set_error(mapping, ret); |
| 2365 | return ret; |
| 2366 | } |
| 2367 | |
| 2368 | /** |
| 2369 | * generic_writepages - walk the list of dirty pages of the given address space and writepage() all of them. |
| 2370 | * @mapping: address space structure to write |
| 2371 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 2372 | * |
| 2373 | * This is a library function, which implements the writepages() |
| 2374 | * address_space_operation. |
| 2375 | */ |
| 2376 | int generic_writepages(struct address_space *mapping, |
| 2377 | struct writeback_control *wbc) |
| 2378 | { |
| 2379 | struct blk_plug plug; |
| 2380 | int ret; |
| 2381 | |
| 2382 | /* deal with chardevs and other special file */ |
| 2383 | if (!mapping->a_ops->writepage) |
| 2384 | return 0; |
| 2385 | |
| 2386 | blk_start_plug(&plug); |
| 2387 | ret = write_cache_pages(mapping, wbc, __writepage, mapping); |
| 2388 | blk_finish_plug(&plug); |
| 2389 | return ret; |
| 2390 | } |
| 2391 | |
| 2392 | EXPORT_SYMBOL(generic_writepages); |
| 2393 | |
| 2394 | int do_writepages(struct address_space *mapping, struct writeback_control *wbc) |
| 2395 | { |
| 2396 | int ret; |
| 2397 | |
| 2398 | if (wbc->nr_to_write <= 0) |
| 2399 | return 0; |
| 2400 | if (mapping->a_ops->writepages) |
| 2401 | ret = mapping->a_ops->writepages(mapping, wbc); |
| 2402 | else |
| 2403 | ret = generic_writepages(mapping, wbc); |
| 2404 | return ret; |
| 2405 | } |
| 2406 | |
| 2407 | /** |
| 2408 | * write_one_page - write out a single page and optionally wait on I/O |
| 2409 | * @page: the page to write |
| 2410 | * @wait: if true, wait on writeout |
| 2411 | * |
| 2412 | * The page must be locked by the caller and will be unlocked upon return. |
| 2413 | * |
| 2414 | * write_one_page() returns a negative error code if I/O failed. |
| 2415 | */ |
| 2416 | int write_one_page(struct page *page, int wait) |
| 2417 | { |
| 2418 | struct address_space *mapping = page->mapping; |
| 2419 | int ret = 0; |
| 2420 | struct writeback_control wbc = { |
| 2421 | .sync_mode = WB_SYNC_ALL, |
| 2422 | .nr_to_write = 1, |
| 2423 | }; |
| 2424 | |
| 2425 | BUG_ON(!PageLocked(page)); |
| 2426 | |
| 2427 | if (wait) |
| 2428 | wait_on_page_writeback(page); |
| 2429 | |
| 2430 | if (clear_page_dirty_for_io(page)) { |
| 2431 | page_cache_get(page); |
| 2432 | ret = mapping->a_ops->writepage(page, &wbc); |
| 2433 | if (ret == 0 && wait) { |
| 2434 | wait_on_page_writeback(page); |
| 2435 | if (PageError(page)) |
| 2436 | ret = -EIO; |
| 2437 | } |
| 2438 | page_cache_release(page); |
| 2439 | } else { |
| 2440 | unlock_page(page); |
| 2441 | } |
| 2442 | return ret; |
| 2443 | } |
| 2444 | EXPORT_SYMBOL(write_one_page); |
| 2445 | |
| 2446 | /* |
| 2447 | * For address_spaces which do not use buffers nor write back. |
| 2448 | */ |
| 2449 | int __set_page_dirty_no_writeback(struct page *page) |
| 2450 | { |
| 2451 | if (!PageDirty(page)) |
| 2452 | return !TestSetPageDirty(page); |
| 2453 | return 0; |
| 2454 | } |
| 2455 | |
| 2456 | /* |
| 2457 | * Helper function for set_page_dirty family. |
| 2458 | * |
| 2459 | * Caller must hold mem_cgroup_begin_page_stat(). |
| 2460 | * |
| 2461 | * NOTE: This relies on being atomic wrt interrupts. |
| 2462 | */ |
| 2463 | void account_page_dirtied(struct page *page, struct address_space *mapping, |
| 2464 | struct mem_cgroup *memcg) |
| 2465 | { |
| 2466 | struct inode *inode = mapping->host; |
| 2467 | |
| 2468 | trace_writeback_dirty_page(page, mapping); |
| 2469 | |
| 2470 | if (mapping_cap_account_dirty(mapping)) { |
| 2471 | struct bdi_writeback *wb; |
| 2472 | |
| 2473 | inode_attach_wb(inode, page); |
| 2474 | wb = inode_to_wb(inode); |
| 2475 | |
| 2476 | mem_cgroup_inc_page_stat(memcg, MEM_CGROUP_STAT_DIRTY); |
| 2477 | __inc_zone_page_state(page, NR_FILE_DIRTY); |
| 2478 | __inc_zone_page_state(page, NR_DIRTIED); |
| 2479 | __inc_wb_stat(wb, WB_RECLAIMABLE); |
| 2480 | __inc_wb_stat(wb, WB_DIRTIED); |
| 2481 | task_io_account_write(PAGE_CACHE_SIZE); |
| 2482 | current->nr_dirtied++; |
| 2483 | this_cpu_inc(bdp_ratelimits); |
| 2484 | } |
| 2485 | } |
| 2486 | EXPORT_SYMBOL(account_page_dirtied); |
| 2487 | |
| 2488 | /* |
| 2489 | * Helper function for deaccounting dirty page without writeback. |
| 2490 | * |
| 2491 | * Caller must hold mem_cgroup_begin_page_stat(). |
| 2492 | */ |
| 2493 | void account_page_cleaned(struct page *page, struct address_space *mapping, |
| 2494 | struct mem_cgroup *memcg, struct bdi_writeback *wb) |
| 2495 | { |
| 2496 | if (mapping_cap_account_dirty(mapping)) { |
| 2497 | mem_cgroup_dec_page_stat(memcg, MEM_CGROUP_STAT_DIRTY); |
| 2498 | dec_zone_page_state(page, NR_FILE_DIRTY); |
| 2499 | dec_wb_stat(wb, WB_RECLAIMABLE); |
| 2500 | task_io_account_cancelled_write(PAGE_CACHE_SIZE); |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | /* |
| 2505 | * For address_spaces which do not use buffers. Just tag the page as dirty in |
| 2506 | * its radix tree. |
| 2507 | * |
| 2508 | * This is also used when a single buffer is being dirtied: we want to set the |
| 2509 | * page dirty in that case, but not all the buffers. This is a "bottom-up" |
| 2510 | * dirtying, whereas __set_page_dirty_buffers() is a "top-down" dirtying. |
| 2511 | * |
| 2512 | * The caller must ensure this doesn't race with truncation. Most will simply |
| 2513 | * hold the page lock, but e.g. zap_pte_range() calls with the page mapped and |
| 2514 | * the pte lock held, which also locks out truncation. |
| 2515 | */ |
| 2516 | int __set_page_dirty_nobuffers(struct page *page) |
| 2517 | { |
| 2518 | struct mem_cgroup *memcg; |
| 2519 | |
| 2520 | memcg = mem_cgroup_begin_page_stat(page); |
| 2521 | if (!TestSetPageDirty(page)) { |
| 2522 | struct address_space *mapping = page_mapping(page); |
| 2523 | unsigned long flags; |
| 2524 | |
| 2525 | if (!mapping) { |
| 2526 | mem_cgroup_end_page_stat(memcg); |
| 2527 | return 1; |
| 2528 | } |
| 2529 | |
| 2530 | spin_lock_irqsave(&mapping->tree_lock, flags); |
| 2531 | BUG_ON(page_mapping(page) != mapping); |
| 2532 | WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page)); |
| 2533 | account_page_dirtied(page, mapping, memcg); |
| 2534 | radix_tree_tag_set(&mapping->page_tree, page_index(page), |
| 2535 | PAGECACHE_TAG_DIRTY); |
| 2536 | spin_unlock_irqrestore(&mapping->tree_lock, flags); |
| 2537 | mem_cgroup_end_page_stat(memcg); |
| 2538 | |
| 2539 | if (mapping->host) { |
| 2540 | /* !PageAnon && !swapper_space */ |
| 2541 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); |
| 2542 | } |
| 2543 | return 1; |
| 2544 | } |
| 2545 | mem_cgroup_end_page_stat(memcg); |
| 2546 | return 0; |
| 2547 | } |
| 2548 | EXPORT_SYMBOL(__set_page_dirty_nobuffers); |
| 2549 | |
| 2550 | /* |
| 2551 | * Call this whenever redirtying a page, to de-account the dirty counters |
| 2552 | * (NR_DIRTIED, BDI_DIRTIED, tsk->nr_dirtied), so that they match the written |
| 2553 | * counters (NR_WRITTEN, BDI_WRITTEN) in long term. The mismatches will lead to |
| 2554 | * systematic errors in balanced_dirty_ratelimit and the dirty pages position |
| 2555 | * control. |
| 2556 | */ |
| 2557 | void account_page_redirty(struct page *page) |
| 2558 | { |
| 2559 | struct address_space *mapping = page->mapping; |
| 2560 | |
| 2561 | if (mapping && mapping_cap_account_dirty(mapping)) { |
| 2562 | struct inode *inode = mapping->host; |
| 2563 | struct bdi_writeback *wb; |
| 2564 | bool locked; |
| 2565 | |
| 2566 | wb = unlocked_inode_to_wb_begin(inode, &locked); |
| 2567 | current->nr_dirtied--; |
| 2568 | dec_zone_page_state(page, NR_DIRTIED); |
| 2569 | dec_wb_stat(wb, WB_DIRTIED); |
| 2570 | unlocked_inode_to_wb_end(inode, locked); |
| 2571 | } |
| 2572 | } |
| 2573 | EXPORT_SYMBOL(account_page_redirty); |
| 2574 | |
| 2575 | /* |
| 2576 | * When a writepage implementation decides that it doesn't want to write this |
| 2577 | * page for some reason, it should redirty the locked page via |
| 2578 | * redirty_page_for_writepage() and it should then unlock the page and return 0 |
| 2579 | */ |
| 2580 | int redirty_page_for_writepage(struct writeback_control *wbc, struct page *page) |
| 2581 | { |
| 2582 | int ret; |
| 2583 | |
| 2584 | wbc->pages_skipped++; |
| 2585 | ret = __set_page_dirty_nobuffers(page); |
| 2586 | account_page_redirty(page); |
| 2587 | return ret; |
| 2588 | } |
| 2589 | EXPORT_SYMBOL(redirty_page_for_writepage); |
| 2590 | |
| 2591 | /* |
| 2592 | * Dirty a page. |
| 2593 | * |
| 2594 | * For pages with a mapping this should be done under the page lock |
| 2595 | * for the benefit of asynchronous memory errors who prefer a consistent |
| 2596 | * dirty state. This rule can be broken in some special cases, |
| 2597 | * but should be better not to. |
| 2598 | * |
| 2599 | * If the mapping doesn't provide a set_page_dirty a_op, then |
| 2600 | * just fall through and assume that it wants buffer_heads. |
| 2601 | */ |
| 2602 | int set_page_dirty(struct page *page) |
| 2603 | { |
| 2604 | struct address_space *mapping = page_mapping(page); |
| 2605 | |
| 2606 | if (likely(mapping)) { |
| 2607 | int (*spd)(struct page *) = mapping->a_ops->set_page_dirty; |
| 2608 | /* |
| 2609 | * readahead/lru_deactivate_page could remain |
| 2610 | * PG_readahead/PG_reclaim due to race with end_page_writeback |
| 2611 | * About readahead, if the page is written, the flags would be |
| 2612 | * reset. So no problem. |
| 2613 | * About lru_deactivate_page, if the page is redirty, the flag |
| 2614 | * will be reset. So no problem. but if the page is used by readahead |
| 2615 | * it will confuse readahead and make it restart the size rampup |
| 2616 | * process. But it's a trivial problem. |
| 2617 | */ |
| 2618 | if (PageReclaim(page)) |
| 2619 | ClearPageReclaim(page); |
| 2620 | #ifdef CONFIG_BLOCK |
| 2621 | if (!spd) |
| 2622 | spd = __set_page_dirty_buffers; |
| 2623 | #endif |
| 2624 | return (*spd)(page); |
| 2625 | } |
| 2626 | if (!PageDirty(page)) { |
| 2627 | if (!TestSetPageDirty(page)) |
| 2628 | return 1; |
| 2629 | } |
| 2630 | return 0; |
| 2631 | } |
| 2632 | EXPORT_SYMBOL(set_page_dirty); |
| 2633 | |
| 2634 | /* |
| 2635 | * set_page_dirty() is racy if the caller has no reference against |
| 2636 | * page->mapping->host, and if the page is unlocked. This is because another |
| 2637 | * CPU could truncate the page off the mapping and then free the mapping. |
| 2638 | * |
| 2639 | * Usually, the page _is_ locked, or the caller is a user-space process which |
| 2640 | * holds a reference on the inode by having an open file. |
| 2641 | * |
| 2642 | * In other cases, the page should be locked before running set_page_dirty(). |
| 2643 | */ |
| 2644 | int set_page_dirty_lock(struct page *page) |
| 2645 | { |
| 2646 | int ret; |
| 2647 | |
| 2648 | lock_page(page); |
| 2649 | ret = set_page_dirty(page); |
| 2650 | unlock_page(page); |
| 2651 | return ret; |
| 2652 | } |
| 2653 | EXPORT_SYMBOL(set_page_dirty_lock); |
| 2654 | |
| 2655 | /* |
| 2656 | * This cancels just the dirty bit on the kernel page itself, it does NOT |
| 2657 | * actually remove dirty bits on any mmap's that may be around. It also |
| 2658 | * leaves the page tagged dirty, so any sync activity will still find it on |
| 2659 | * the dirty lists, and in particular, clear_page_dirty_for_io() will still |
| 2660 | * look at the dirty bits in the VM. |
| 2661 | * |
| 2662 | * Doing this should *normally* only ever be done when a page is truncated, |
| 2663 | * and is not actually mapped anywhere at all. However, fs/buffer.c does |
| 2664 | * this when it notices that somebody has cleaned out all the buffers on a |
| 2665 | * page without actually doing it through the VM. Can you say "ext3 is |
| 2666 | * horribly ugly"? Thought you could. |
| 2667 | */ |
| 2668 | void cancel_dirty_page(struct page *page) |
| 2669 | { |
| 2670 | struct address_space *mapping = page_mapping(page); |
| 2671 | |
| 2672 | if (mapping_cap_account_dirty(mapping)) { |
| 2673 | struct inode *inode = mapping->host; |
| 2674 | struct bdi_writeback *wb; |
| 2675 | struct mem_cgroup *memcg; |
| 2676 | bool locked; |
| 2677 | |
| 2678 | memcg = mem_cgroup_begin_page_stat(page); |
| 2679 | wb = unlocked_inode_to_wb_begin(inode, &locked); |
| 2680 | |
| 2681 | if (TestClearPageDirty(page)) |
| 2682 | account_page_cleaned(page, mapping, memcg, wb); |
| 2683 | |
| 2684 | unlocked_inode_to_wb_end(inode, locked); |
| 2685 | mem_cgroup_end_page_stat(memcg); |
| 2686 | } else { |
| 2687 | ClearPageDirty(page); |
| 2688 | } |
| 2689 | } |
| 2690 | EXPORT_SYMBOL(cancel_dirty_page); |
| 2691 | |
| 2692 | /* |
| 2693 | * Clear a page's dirty flag, while caring for dirty memory accounting. |
| 2694 | * Returns true if the page was previously dirty. |
| 2695 | * |
| 2696 | * This is for preparing to put the page under writeout. We leave the page |
| 2697 | * tagged as dirty in the radix tree so that a concurrent write-for-sync |
| 2698 | * can discover it via a PAGECACHE_TAG_DIRTY walk. The ->writepage |
| 2699 | * implementation will run either set_page_writeback() or set_page_dirty(), |
| 2700 | * at which stage we bring the page's dirty flag and radix-tree dirty tag |
| 2701 | * back into sync. |
| 2702 | * |
| 2703 | * This incoherency between the page's dirty flag and radix-tree tag is |
| 2704 | * unfortunate, but it only exists while the page is locked. |
| 2705 | */ |
| 2706 | int clear_page_dirty_for_io(struct page *page) |
| 2707 | { |
| 2708 | struct address_space *mapping = page_mapping(page); |
| 2709 | int ret = 0; |
| 2710 | |
| 2711 | BUG_ON(!PageLocked(page)); |
| 2712 | |
| 2713 | if (mapping && mapping_cap_account_dirty(mapping)) { |
| 2714 | struct inode *inode = mapping->host; |
| 2715 | struct bdi_writeback *wb; |
| 2716 | struct mem_cgroup *memcg; |
| 2717 | bool locked; |
| 2718 | |
| 2719 | /* |
| 2720 | * Yes, Virginia, this is indeed insane. |
| 2721 | * |
| 2722 | * We use this sequence to make sure that |
| 2723 | * (a) we account for dirty stats properly |
| 2724 | * (b) we tell the low-level filesystem to |
| 2725 | * mark the whole page dirty if it was |
| 2726 | * dirty in a pagetable. Only to then |
| 2727 | * (c) clean the page again and return 1 to |
| 2728 | * cause the writeback. |
| 2729 | * |
| 2730 | * This way we avoid all nasty races with the |
| 2731 | * dirty bit in multiple places and clearing |
| 2732 | * them concurrently from different threads. |
| 2733 | * |
| 2734 | * Note! Normally the "set_page_dirty(page)" |
| 2735 | * has no effect on the actual dirty bit - since |
| 2736 | * that will already usually be set. But we |
| 2737 | * need the side effects, and it can help us |
| 2738 | * avoid races. |
| 2739 | * |
| 2740 | * We basically use the page "master dirty bit" |
| 2741 | * as a serialization point for all the different |
| 2742 | * threads doing their things. |
| 2743 | */ |
| 2744 | if (page_mkclean(page)) |
| 2745 | set_page_dirty(page); |
| 2746 | /* |
| 2747 | * We carefully synchronise fault handlers against |
| 2748 | * installing a dirty pte and marking the page dirty |
| 2749 | * at this point. We do this by having them hold the |
| 2750 | * page lock while dirtying the page, and pages are |
| 2751 | * always locked coming in here, so we get the desired |
| 2752 | * exclusion. |
| 2753 | */ |
| 2754 | memcg = mem_cgroup_begin_page_stat(page); |
| 2755 | wb = unlocked_inode_to_wb_begin(inode, &locked); |
| 2756 | if (TestClearPageDirty(page)) { |
| 2757 | mem_cgroup_dec_page_stat(memcg, MEM_CGROUP_STAT_DIRTY); |
| 2758 | dec_zone_page_state(page, NR_FILE_DIRTY); |
| 2759 | dec_wb_stat(wb, WB_RECLAIMABLE); |
| 2760 | ret = 1; |
| 2761 | } |
| 2762 | unlocked_inode_to_wb_end(inode, locked); |
| 2763 | mem_cgroup_end_page_stat(memcg); |
| 2764 | return ret; |
| 2765 | } |
| 2766 | return TestClearPageDirty(page); |
| 2767 | } |
| 2768 | EXPORT_SYMBOL(clear_page_dirty_for_io); |
| 2769 | |
| 2770 | int test_clear_page_writeback(struct page *page) |
| 2771 | { |
| 2772 | struct address_space *mapping = page_mapping(page); |
| 2773 | struct mem_cgroup *memcg; |
| 2774 | int ret; |
| 2775 | |
| 2776 | memcg = mem_cgroup_begin_page_stat(page); |
| 2777 | if (mapping) { |
| 2778 | struct inode *inode = mapping->host; |
| 2779 | struct backing_dev_info *bdi = inode_to_bdi(inode); |
| 2780 | unsigned long flags; |
| 2781 | |
| 2782 | spin_lock_irqsave(&mapping->tree_lock, flags); |
| 2783 | ret = TestClearPageWriteback(page); |
| 2784 | if (ret) { |
| 2785 | radix_tree_tag_clear(&mapping->page_tree, |
| 2786 | page_index(page), |
| 2787 | PAGECACHE_TAG_WRITEBACK); |
| 2788 | if (bdi_cap_account_writeback(bdi)) { |
| 2789 | struct bdi_writeback *wb = inode_to_wb(inode); |
| 2790 | |
| 2791 | __dec_wb_stat(wb, WB_WRITEBACK); |
| 2792 | __wb_writeout_inc(wb); |
| 2793 | } |
| 2794 | } |
| 2795 | spin_unlock_irqrestore(&mapping->tree_lock, flags); |
| 2796 | } else { |
| 2797 | ret = TestClearPageWriteback(page); |
| 2798 | } |
| 2799 | if (ret) { |
| 2800 | mem_cgroup_dec_page_stat(memcg, MEM_CGROUP_STAT_WRITEBACK); |
| 2801 | dec_zone_page_state(page, NR_WRITEBACK); |
| 2802 | inc_zone_page_state(page, NR_WRITTEN); |
| 2803 | } |
| 2804 | mem_cgroup_end_page_stat(memcg); |
| 2805 | return ret; |
| 2806 | } |
| 2807 | |
| 2808 | int __test_set_page_writeback(struct page *page, bool keep_write) |
| 2809 | { |
| 2810 | struct address_space *mapping = page_mapping(page); |
| 2811 | struct mem_cgroup *memcg; |
| 2812 | int ret; |
| 2813 | |
| 2814 | memcg = mem_cgroup_begin_page_stat(page); |
| 2815 | if (mapping) { |
| 2816 | struct inode *inode = mapping->host; |
| 2817 | struct backing_dev_info *bdi = inode_to_bdi(inode); |
| 2818 | unsigned long flags; |
| 2819 | |
| 2820 | spin_lock_irqsave(&mapping->tree_lock, flags); |
| 2821 | ret = TestSetPageWriteback(page); |
| 2822 | if (!ret) { |
| 2823 | radix_tree_tag_set(&mapping->page_tree, |
| 2824 | page_index(page), |
| 2825 | PAGECACHE_TAG_WRITEBACK); |
| 2826 | if (bdi_cap_account_writeback(bdi)) |
| 2827 | __inc_wb_stat(inode_to_wb(inode), WB_WRITEBACK); |
| 2828 | } |
| 2829 | if (!PageDirty(page)) |
| 2830 | radix_tree_tag_clear(&mapping->page_tree, |
| 2831 | page_index(page), |
| 2832 | PAGECACHE_TAG_DIRTY); |
| 2833 | if (!keep_write) |
| 2834 | radix_tree_tag_clear(&mapping->page_tree, |
| 2835 | page_index(page), |
| 2836 | PAGECACHE_TAG_TOWRITE); |
| 2837 | spin_unlock_irqrestore(&mapping->tree_lock, flags); |
| 2838 | } else { |
| 2839 | ret = TestSetPageWriteback(page); |
| 2840 | } |
| 2841 | if (!ret) { |
| 2842 | mem_cgroup_inc_page_stat(memcg, MEM_CGROUP_STAT_WRITEBACK); |
| 2843 | inc_zone_page_state(page, NR_WRITEBACK); |
| 2844 | } |
| 2845 | mem_cgroup_end_page_stat(memcg); |
| 2846 | return ret; |
| 2847 | |
| 2848 | } |
| 2849 | EXPORT_SYMBOL(__test_set_page_writeback); |
| 2850 | |
| 2851 | /* |
| 2852 | * Return true if any of the pages in the mapping are marked with the |
| 2853 | * passed tag. |
| 2854 | */ |
| 2855 | int mapping_tagged(struct address_space *mapping, int tag) |
| 2856 | { |
| 2857 | return radix_tree_tagged(&mapping->page_tree, tag); |
| 2858 | } |
| 2859 | EXPORT_SYMBOL(mapping_tagged); |
| 2860 | |
| 2861 | /** |
| 2862 | * wait_for_stable_page() - wait for writeback to finish, if necessary. |
| 2863 | * @page: The page to wait on. |
| 2864 | * |
| 2865 | * This function determines if the given page is related to a backing device |
| 2866 | * that requires page contents to be held stable during writeback. If so, then |
| 2867 | * it will wait for any pending writeback to complete. |
| 2868 | */ |
| 2869 | void wait_for_stable_page(struct page *page) |
| 2870 | { |
| 2871 | if (bdi_cap_stable_pages_required(inode_to_bdi(page->mapping->host))) |
| 2872 | wait_on_page_writeback(page); |
| 2873 | } |
| 2874 | EXPORT_SYMBOL_GPL(wait_for_stable_page); |