| .. | .. |
|---|
| 15 | 15 | #include <linux/bitops.h> |
|---|
| 16 | 16 | #include <linux/hardirq.h> /* for in_interrupt() */ |
|---|
| 17 | 17 | #include <linux/hugetlb_inline.h> |
|---|
| 18 | +#include <linux/sched/debug.h> |
|---|
| 18 | 19 | |
|---|
| 19 | 20 | struct pagevec; |
|---|
| 20 | 21 | |
|---|
| .. | .. |
|---|
| 29 | 30 | AS_EXITING = 4, /* final truncate in progress */ |
|---|
| 30 | 31 | /* writeback related tags are not used */ |
|---|
| 31 | 32 | AS_NO_WRITEBACK_TAGS = 5, |
|---|
| 33 | + AS_THP_SUPPORT = 6, /* THPs supported */ |
|---|
| 32 | 34 | }; |
|---|
| 33 | 35 | |
|---|
| 34 | 36 | /** |
|---|
| 35 | 37 | * mapping_set_error - record a writeback error in the address_space |
|---|
| 36 | | - * @mapping - the mapping in which an error should be set |
|---|
| 37 | | - * @error - the error to set in the mapping |
|---|
| 38 | + * @mapping: the mapping in which an error should be set |
|---|
| 39 | + * @error: the error to set in the mapping |
|---|
| 38 | 40 | * |
|---|
| 39 | 41 | * When writeback fails in some way, we must record that error so that |
|---|
| 40 | 42 | * userspace can be informed when fsync and the like are called. We endeavor |
|---|
| .. | .. |
|---|
| 51 | 53 | return; |
|---|
| 52 | 54 | |
|---|
| 53 | 55 | /* Record in wb_err for checkers using errseq_t based tracking */ |
|---|
| 54 | | - filemap_set_wb_err(mapping, error); |
|---|
| 56 | + __filemap_set_wb_err(mapping, error); |
|---|
| 57 | + |
|---|
| 58 | + /* Record it in superblock */ |
|---|
| 59 | + if (mapping->host) |
|---|
| 60 | + errseq_set(&mapping->host->i_sb->s_wb_err, error); |
|---|
| 55 | 61 | |
|---|
| 56 | 62 | /* Record it in flags for now, for legacy callers */ |
|---|
| 57 | 63 | if (error == -ENOSPC) |
|---|
| .. | .. |
|---|
| 70 | 76 | clear_bit(AS_UNEVICTABLE, &mapping->flags); |
|---|
| 71 | 77 | } |
|---|
| 72 | 78 | |
|---|
| 73 | | -static inline int mapping_unevictable(struct address_space *mapping) |
|---|
| 79 | +static inline bool mapping_unevictable(struct address_space *mapping) |
|---|
| 74 | 80 | { |
|---|
| 75 | | - if (mapping) |
|---|
| 76 | | - return test_bit(AS_UNEVICTABLE, &mapping->flags); |
|---|
| 77 | | - return !!mapping; |
|---|
| 81 | + return mapping && test_bit(AS_UNEVICTABLE, &mapping->flags); |
|---|
| 78 | 82 | } |
|---|
| 79 | 83 | |
|---|
| 80 | 84 | static inline void mapping_set_exiting(struct address_space *mapping) |
|---|
| .. | .. |
|---|
| 116 | 120 | static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask) |
|---|
| 117 | 121 | { |
|---|
| 118 | 122 | m->gfp_mask = mask; |
|---|
| 123 | +} |
|---|
| 124 | + |
|---|
| 125 | +static inline bool mapping_thp_support(struct address_space *mapping) |
|---|
| 126 | +{ |
|---|
| 127 | + return test_bit(AS_THP_SUPPORT, &mapping->flags); |
|---|
| 128 | +} |
|---|
| 129 | + |
|---|
| 130 | +static inline int filemap_nr_thps(struct address_space *mapping) |
|---|
| 131 | +{ |
|---|
| 132 | +#ifdef CONFIG_READ_ONLY_THP_FOR_FS |
|---|
| 133 | + return atomic_read(&mapping->nr_thps); |
|---|
| 134 | +#else |
|---|
| 135 | + return 0; |
|---|
| 136 | +#endif |
|---|
| 137 | +} |
|---|
| 138 | + |
|---|
| 139 | +static inline void filemap_nr_thps_inc(struct address_space *mapping) |
|---|
| 140 | +{ |
|---|
| 141 | +#ifdef CONFIG_READ_ONLY_THP_FOR_FS |
|---|
| 142 | + if (!mapping_thp_support(mapping)) |
|---|
| 143 | + atomic_inc(&mapping->nr_thps); |
|---|
| 144 | +#else |
|---|
| 145 | + WARN_ON_ONCE(1); |
|---|
| 146 | +#endif |
|---|
| 147 | +} |
|---|
| 148 | + |
|---|
| 149 | +static inline void filemap_nr_thps_dec(struct address_space *mapping) |
|---|
| 150 | +{ |
|---|
| 151 | +#ifdef CONFIG_READ_ONLY_THP_FOR_FS |
|---|
| 152 | + if (!mapping_thp_support(mapping)) |
|---|
| 153 | + atomic_dec(&mapping->nr_thps); |
|---|
| 154 | +#else |
|---|
| 155 | + WARN_ON_ONCE(1); |
|---|
| 156 | +#endif |
|---|
| 119 | 157 | } |
|---|
| 120 | 158 | |
|---|
| 121 | 159 | void release_pages(struct page **pages, int nr); |
|---|
| .. | .. |
|---|
| 164 | 202 | * will find the page or it will not. Likewise, the old find_get_page could run |
|---|
| 165 | 203 | * either before the insertion or afterwards, depending on timing. |
|---|
| 166 | 204 | */ |
|---|
| 167 | | -static inline int page_cache_get_speculative(struct page *page) |
|---|
| 205 | +static inline int __page_cache_add_speculative(struct page *page, int count) |
|---|
| 168 | 206 | { |
|---|
| 169 | 207 | #ifdef CONFIG_TINY_RCU |
|---|
| 170 | 208 | # ifdef CONFIG_PREEMPT_COUNT |
|---|
| .. | .. |
|---|
| 180 | 218 | * SMP requires. |
|---|
| 181 | 219 | */ |
|---|
| 182 | 220 | VM_BUG_ON_PAGE(page_count(page) == 0, page); |
|---|
| 183 | | - page_ref_inc(page); |
|---|
| 221 | + page_ref_add(page, count); |
|---|
| 184 | 222 | |
|---|
| 185 | 223 | #else |
|---|
| 186 | | - if (unlikely(!get_page_unless_zero(page))) { |
|---|
| 224 | + if (unlikely(!page_ref_add_unless(page, count, 0))) { |
|---|
| 187 | 225 | /* |
|---|
| 188 | 226 | * Either the page has been freed, or will be freed. |
|---|
| 189 | 227 | * In either case, retry here and the caller should |
|---|
| .. | .. |
|---|
| 197 | 235 | return 1; |
|---|
| 198 | 236 | } |
|---|
| 199 | 237 | |
|---|
| 200 | | -/* |
|---|
| 201 | | - * Same as above, but add instead of inc (could just be merged) |
|---|
| 202 | | - */ |
|---|
| 238 | +static inline int page_cache_get_speculative(struct page *page) |
|---|
| 239 | +{ |
|---|
| 240 | + return __page_cache_add_speculative(page, 1); |
|---|
| 241 | +} |
|---|
| 242 | + |
|---|
| 203 | 243 | static inline int page_cache_add_speculative(struct page *page, int count) |
|---|
| 204 | 244 | { |
|---|
| 205 | | - VM_BUG_ON(in_interrupt()); |
|---|
| 245 | + return __page_cache_add_speculative(page, count); |
|---|
| 246 | +} |
|---|
| 206 | 247 | |
|---|
| 207 | | -#if !defined(CONFIG_SMP) && defined(CONFIG_TREE_RCU) |
|---|
| 208 | | -# ifdef CONFIG_PREEMPT_COUNT |
|---|
| 209 | | - VM_BUG_ON(!in_atomic() && !irqs_disabled()); |
|---|
| 210 | | -# endif |
|---|
| 211 | | - VM_BUG_ON_PAGE(page_count(page) == 0, page); |
|---|
| 212 | | - page_ref_add(page, count); |
|---|
| 248 | +/** |
|---|
| 249 | + * attach_page_private - Attach private data to a page. |
|---|
| 250 | + * @page: Page to attach data to. |
|---|
| 251 | + * @data: Data to attach to page. |
|---|
| 252 | + * |
|---|
| 253 | + * Attaching private data to a page increments the page's reference count. |
|---|
| 254 | + * The data must be detached before the page will be freed. |
|---|
| 255 | + */ |
|---|
| 256 | +static inline void attach_page_private(struct page *page, void *data) |
|---|
| 257 | +{ |
|---|
| 258 | + get_page(page); |
|---|
| 259 | + set_page_private(page, (unsigned long)data); |
|---|
| 260 | + SetPagePrivate(page); |
|---|
| 261 | +} |
|---|
| 213 | 262 | |
|---|
| 214 | | -#else |
|---|
| 215 | | - if (unlikely(!page_ref_add_unless(page, count, 0))) |
|---|
| 216 | | - return 0; |
|---|
| 217 | | -#endif |
|---|
| 218 | | - VM_BUG_ON_PAGE(PageCompound(page) && page != compound_head(page), page); |
|---|
| 263 | +/** |
|---|
| 264 | + * detach_page_private - Detach private data from a page. |
|---|
| 265 | + * @page: Page to detach data from. |
|---|
| 266 | + * |
|---|
| 267 | + * Removes the data that was previously attached to the page and decrements |
|---|
| 268 | + * the refcount on the page. |
|---|
| 269 | + * |
|---|
| 270 | + * Return: Data that was attached to the page. |
|---|
| 271 | + */ |
|---|
| 272 | +static inline void *detach_page_private(struct page *page) |
|---|
| 273 | +{ |
|---|
| 274 | + void *data = (void *)page_private(page); |
|---|
| 219 | 275 | |
|---|
| 220 | | - return 1; |
|---|
| 276 | + if (!PagePrivate(page)) |
|---|
| 277 | + return NULL; |
|---|
| 278 | + ClearPagePrivate(page); |
|---|
| 279 | + set_page_private(page, 0); |
|---|
| 280 | + put_page(page); |
|---|
| 281 | + |
|---|
| 282 | + return data; |
|---|
| 221 | 283 | } |
|---|
| 222 | 284 | |
|---|
| 223 | 285 | #ifdef CONFIG_NUMA |
|---|
| .. | .. |
|---|
| 234 | 296 | return __page_cache_alloc(mapping_gfp_mask(x)); |
|---|
| 235 | 297 | } |
|---|
| 236 | 298 | |
|---|
| 237 | | -static inline gfp_t readahead_gfp_mask(struct address_space *x) |
|---|
| 238 | | -{ |
|---|
| 239 | | - return mapping_gfp_mask(x) | __GFP_NORETRY | __GFP_NOWARN; |
|---|
| 240 | | -} |
|---|
| 299 | +gfp_t readahead_gfp_mask(struct address_space *x); |
|---|
| 241 | 300 | |
|---|
| 242 | 301 | typedef int filler_t(void *, struct page *); |
|---|
| 243 | 302 | |
|---|
| 244 | | -pgoff_t page_cache_next_hole(struct address_space *mapping, |
|---|
| 303 | +pgoff_t page_cache_next_miss(struct address_space *mapping, |
|---|
| 245 | 304 | pgoff_t index, unsigned long max_scan); |
|---|
| 246 | | -pgoff_t page_cache_prev_hole(struct address_space *mapping, |
|---|
| 305 | +pgoff_t page_cache_prev_miss(struct address_space *mapping, |
|---|
| 247 | 306 | pgoff_t index, unsigned long max_scan); |
|---|
| 248 | 307 | |
|---|
| 249 | 308 | #define FGP_ACCESSED 0x00000001 |
|---|
| .. | .. |
|---|
| 253 | 312 | #define FGP_NOFS 0x00000010 |
|---|
| 254 | 313 | #define FGP_NOWAIT 0x00000020 |
|---|
| 255 | 314 | #define FGP_FOR_MMAP 0x00000040 |
|---|
| 315 | +#define FGP_HEAD 0x00000080 |
|---|
| 256 | 316 | |
|---|
| 257 | 317 | struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset, |
|---|
| 258 | 318 | int fgp_flags, gfp_t cache_gfp_mask); |
|---|
| .. | .. |
|---|
| 282 | 342 | /** |
|---|
| 283 | 343 | * find_lock_page - locate, pin and lock a pagecache page |
|---|
| 284 | 344 | * @mapping: the address_space to search |
|---|
| 285 | | - * @offset: the page index |
|---|
| 345 | + * @index: the page index |
|---|
| 286 | 346 | * |
|---|
| 287 | | - * Looks up the page cache slot at @mapping & @offset. If there is a |
|---|
| 347 | + * Looks up the page cache entry at @mapping & @index. If there is a |
|---|
| 288 | 348 | * page cache page, it is returned locked and with an increased |
|---|
| 289 | 349 | * refcount. |
|---|
| 290 | 350 | * |
|---|
| 291 | | - * Otherwise, %NULL is returned. |
|---|
| 292 | | - * |
|---|
| 293 | | - * find_lock_page() may sleep. |
|---|
| 351 | + * Context: May sleep. |
|---|
| 352 | + * Return: A struct page or %NULL if there is no page in the cache for this |
|---|
| 353 | + * index. |
|---|
| 294 | 354 | */ |
|---|
| 295 | 355 | static inline struct page *find_lock_page(struct address_space *mapping, |
|---|
| 296 | | - pgoff_t offset) |
|---|
| 356 | + pgoff_t index) |
|---|
| 297 | 357 | { |
|---|
| 298 | | - return pagecache_get_page(mapping, offset, FGP_LOCK, 0); |
|---|
| 358 | + return pagecache_get_page(mapping, index, FGP_LOCK, 0); |
|---|
| 359 | +} |
|---|
| 360 | + |
|---|
| 361 | +/** |
|---|
| 362 | + * find_lock_head - Locate, pin and lock a pagecache page. |
|---|
| 363 | + * @mapping: The address_space to search. |
|---|
| 364 | + * @index: The page index. |
|---|
| 365 | + * |
|---|
| 366 | + * Looks up the page cache entry at @mapping & @index. If there is a |
|---|
| 367 | + * page cache page, its head page is returned locked and with an increased |
|---|
| 368 | + * refcount. |
|---|
| 369 | + * |
|---|
| 370 | + * Context: May sleep. |
|---|
| 371 | + * Return: A struct page which is !PageTail, or %NULL if there is no page |
|---|
| 372 | + * in the cache for this index. |
|---|
| 373 | + */ |
|---|
| 374 | +static inline struct page *find_lock_head(struct address_space *mapping, |
|---|
| 375 | + pgoff_t index) |
|---|
| 376 | +{ |
|---|
| 377 | + return pagecache_get_page(mapping, index, FGP_LOCK | FGP_HEAD, 0); |
|---|
| 299 | 378 | } |
|---|
| 300 | 379 | |
|---|
| 301 | 380 | /** |
|---|
| .. | .. |
|---|
| 318 | 397 | * atomic allocation! |
|---|
| 319 | 398 | */ |
|---|
| 320 | 399 | static inline struct page *find_or_create_page(struct address_space *mapping, |
|---|
| 321 | | - pgoff_t offset, gfp_t gfp_mask) |
|---|
| 400 | + pgoff_t index, gfp_t gfp_mask) |
|---|
| 322 | 401 | { |
|---|
| 323 | | - return pagecache_get_page(mapping, offset, |
|---|
| 402 | + return pagecache_get_page(mapping, index, |
|---|
| 324 | 403 | FGP_LOCK|FGP_ACCESSED|FGP_CREAT, |
|---|
| 325 | 404 | gfp_mask); |
|---|
| 326 | 405 | } |
|---|
| .. | .. |
|---|
| 346 | 425 | mapping_gfp_mask(mapping)); |
|---|
| 347 | 426 | } |
|---|
| 348 | 427 | |
|---|
| 349 | | -struct page *find_get_entry(struct address_space *mapping, pgoff_t offset); |
|---|
| 350 | | -struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset); |
|---|
| 428 | +/* Does this page contain this index? */ |
|---|
| 429 | +static inline bool thp_contains(struct page *head, pgoff_t index) |
|---|
| 430 | +{ |
|---|
| 431 | + /* HugeTLBfs indexes the page cache in units of hpage_size */ |
|---|
| 432 | + if (PageHuge(head)) |
|---|
| 433 | + return head->index == index; |
|---|
| 434 | + return page_index(head) == (index & ~(thp_nr_pages(head) - 1UL)); |
|---|
| 435 | +} |
|---|
| 436 | + |
|---|
| 437 | +/* |
|---|
| 438 | + * Given the page we found in the page cache, return the page corresponding |
|---|
| 439 | + * to this index in the file |
|---|
| 440 | + */ |
|---|
| 441 | +static inline struct page *find_subpage(struct page *head, pgoff_t index) |
|---|
| 442 | +{ |
|---|
| 443 | + /* HugeTLBfs wants the head page regardless */ |
|---|
| 444 | + if (PageHuge(head)) |
|---|
| 445 | + return head; |
|---|
| 446 | + |
|---|
| 447 | + return head + (index & (thp_nr_pages(head) - 1)); |
|---|
| 448 | +} |
|---|
| 449 | + |
|---|
| 351 | 450 | unsigned find_get_entries(struct address_space *mapping, pgoff_t start, |
|---|
| 352 | 451 | unsigned int nr_entries, struct page **entries, |
|---|
| 353 | 452 | pgoff_t *indices); |
|---|
| .. | .. |
|---|
| 364 | 463 | unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start, |
|---|
| 365 | 464 | unsigned int nr_pages, struct page **pages); |
|---|
| 366 | 465 | unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index, |
|---|
| 367 | | - pgoff_t end, int tag, unsigned int nr_pages, |
|---|
| 466 | + pgoff_t end, xa_mark_t tag, unsigned int nr_pages, |
|---|
| 368 | 467 | struct page **pages); |
|---|
| 369 | 468 | static inline unsigned find_get_pages_tag(struct address_space *mapping, |
|---|
| 370 | | - pgoff_t *index, int tag, unsigned int nr_pages, |
|---|
| 469 | + pgoff_t *index, xa_mark_t tag, unsigned int nr_pages, |
|---|
| 371 | 470 | struct page **pages) |
|---|
| 372 | 471 | { |
|---|
| 373 | 472 | return find_get_pages_range_tag(mapping, index, (pgoff_t)-1, tag, |
|---|
| 374 | 473 | nr_pages, pages); |
|---|
| 375 | 474 | } |
|---|
| 376 | | -unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start, |
|---|
| 377 | | - int tag, unsigned int nr_entries, |
|---|
| 378 | | - struct page **entries, pgoff_t *indices); |
|---|
| 379 | 475 | |
|---|
| 380 | 476 | struct page *grab_cache_page_write_begin(struct address_space *mapping, |
|---|
| 381 | 477 | pgoff_t index, unsigned flags); |
|---|
| .. | .. |
|---|
| 457 | 553 | pgoff_t pgoff; |
|---|
| 458 | 554 | if (unlikely(is_vm_hugetlb_page(vma))) |
|---|
| 459 | 555 | return linear_hugepage_index(vma, address); |
|---|
| 460 | | - pgoff = (address - vma->vm_start) >> PAGE_SHIFT; |
|---|
| 461 | | - pgoff += vma->vm_pgoff; |
|---|
| 556 | + pgoff = (address - READ_ONCE(vma->vm_start)) >> PAGE_SHIFT; |
|---|
| 557 | + pgoff += READ_ONCE(vma->vm_pgoff); |
|---|
| 462 | 558 | return pgoff; |
|---|
| 463 | 559 | } |
|---|
| 464 | 560 | |
|---|
| 465 | | -extern void __sched __lock_page(struct page *page); |
|---|
| 466 | | -extern int __sched __lock_page_killable(struct page *page); |
|---|
| 467 | | -extern int __sched __lock_page_or_retry(struct page *page, struct mm_struct *mm, |
|---|
| 561 | +struct wait_page_key { |
|---|
| 562 | + struct page *page; |
|---|
| 563 | + int bit_nr; |
|---|
| 564 | + int page_match; |
|---|
| 565 | +}; |
|---|
| 566 | + |
|---|
| 567 | +struct wait_page_queue { |
|---|
| 568 | + struct page *page; |
|---|
| 569 | + int bit_nr; |
|---|
| 570 | + wait_queue_entry_t wait; |
|---|
| 571 | +}; |
|---|
| 572 | + |
|---|
| 573 | +static inline bool wake_page_match(struct wait_page_queue *wait_page, |
|---|
| 574 | + struct wait_page_key *key) |
|---|
| 575 | +{ |
|---|
| 576 | + if (wait_page->page != key->page) |
|---|
| 577 | + return false; |
|---|
| 578 | + key->page_match = 1; |
|---|
| 579 | + |
|---|
| 580 | + if (wait_page->bit_nr != key->bit_nr) |
|---|
| 581 | + return false; |
|---|
| 582 | + |
|---|
| 583 | + return true; |
|---|
| 584 | +} |
|---|
| 585 | + |
|---|
| 586 | +extern void __lock_page(struct page *page); |
|---|
| 587 | +extern int __lock_page_killable(struct page *page); |
|---|
| 588 | +extern int __lock_page_async(struct page *page, struct wait_page_queue *wait); |
|---|
| 589 | +extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm, |
|---|
| 468 | 590 | unsigned int flags); |
|---|
| 469 | 591 | extern void unlock_page(struct page *page); |
|---|
| 470 | 592 | |
|---|
| 593 | +/* |
|---|
| 594 | + * Return true if the page was successfully locked |
|---|
| 595 | + */ |
|---|
| 471 | 596 | static inline int trylock_page(struct page *page) |
|---|
| 472 | 597 | { |
|---|
| 473 | 598 | page = compound_head(page); |
|---|
| .. | .. |
|---|
| 498 | 623 | } |
|---|
| 499 | 624 | |
|---|
| 500 | 625 | /* |
|---|
| 626 | + * lock_page_async - Lock the page, unless this would block. If the page |
|---|
| 627 | + * is already locked, then queue a callback when the page becomes unlocked. |
|---|
| 628 | + * This callback can then retry the operation. |
|---|
| 629 | + * |
|---|
| 630 | + * Returns 0 if the page is locked successfully, or -EIOCBQUEUED if the page |
|---|
| 631 | + * was already locked and the callback defined in 'wait' was queued. |
|---|
| 632 | + */ |
|---|
| 633 | +static inline __sched int lock_page_async(struct page *page, |
|---|
| 634 | + struct wait_page_queue *wait) |
|---|
| 635 | +{ |
|---|
| 636 | + if (!trylock_page(page)) |
|---|
| 637 | + return __lock_page_async(page, wait); |
|---|
| 638 | + return 0; |
|---|
| 639 | +} |
|---|
| 640 | + |
|---|
| 641 | +/* |
|---|
| 501 | 642 | * lock_page_or_retry - Lock the page, unless this would block and the |
|---|
| 502 | 643 | * caller indicated that it can handle a retry. |
|---|
| 503 | 644 | * |
|---|
| 504 | | - * Return value and mmap_sem implications depend on flags; see |
|---|
| 645 | + * Return value and mmap_lock implications depend on flags; see |
|---|
| 505 | 646 | * __lock_page_or_retry(). |
|---|
| 506 | 647 | */ |
|---|
| 507 | | -static inline __sched int lock_page_or_retry(struct page *page, |
|---|
| 508 | | - struct mm_struct *mm, |
|---|
| 509 | | - unsigned int flags) |
|---|
| 648 | +static inline __sched int lock_page_or_retry(struct page *page, struct mm_struct *mm, |
|---|
| 649 | + unsigned int flags) |
|---|
| 510 | 650 | { |
|---|
| 511 | 651 | might_sleep(); |
|---|
| 512 | 652 | return trylock_page(page) || __lock_page_or_retry(page, mm, flags); |
|---|
| .. | .. |
|---|
| 516 | 656 | * This is exported only for wait_on_page_locked/wait_on_page_writeback, etc., |
|---|
| 517 | 657 | * and should not be used directly. |
|---|
| 518 | 658 | */ |
|---|
| 519 | | -extern void __sched wait_on_page_bit(struct page *page, int bit_nr); |
|---|
| 520 | | -extern int __sched wait_on_page_bit_killable(struct page *page, int bit_nr); |
|---|
| 659 | +extern void wait_on_page_bit(struct page *page, int bit_nr); |
|---|
| 660 | +extern int wait_on_page_bit_killable(struct page *page, int bit_nr); |
|---|
| 521 | 661 | |
|---|
| 522 | 662 | /* |
|---|
| 523 | 663 | * Wait for a page to be unlocked. |
|---|
| .. | .. |
|---|
| 539 | 679 | return wait_on_page_bit_killable(compound_head(page), PG_locked); |
|---|
| 540 | 680 | } |
|---|
| 541 | 681 | |
|---|
| 542 | | -/* |
|---|
| 543 | | - * Wait for a page to complete writeback |
|---|
| 544 | | - */ |
|---|
| 545 | | -static inline __sched void wait_on_page_writeback(struct page *page) |
|---|
| 546 | | -{ |
|---|
| 547 | | - if (PageWriteback(page)) |
|---|
| 548 | | - wait_on_page_bit(page, PG_writeback); |
|---|
| 549 | | -} |
|---|
| 682 | +extern void put_and_wait_on_page_locked(struct page *page); |
|---|
| 550 | 683 | |
|---|
| 684 | +void wait_on_page_writeback(struct page *page); |
|---|
| 551 | 685 | extern void end_page_writeback(struct page *page); |
|---|
| 552 | 686 | void wait_for_stable_page(struct page *page); |
|---|
| 553 | 687 | |
|---|
| .. | .. |
|---|
| 641 | 775 | return error; |
|---|
| 642 | 776 | } |
|---|
| 643 | 777 | |
|---|
| 778 | +/** |
|---|
| 779 | + * struct readahead_control - Describes a readahead request. |
|---|
| 780 | + * |
|---|
| 781 | + * A readahead request is for consecutive pages. Filesystems which |
|---|
| 782 | + * implement the ->readahead method should call readahead_page() or |
|---|
| 783 | + * readahead_page_batch() in a loop and attempt to start I/O against |
|---|
| 784 | + * each page in the request. |
|---|
| 785 | + * |
|---|
| 786 | + * Most of the fields in this struct are private and should be accessed |
|---|
| 787 | + * by the functions below. |
|---|
| 788 | + * |
|---|
| 789 | + * @file: The file, used primarily by network filesystems for authentication. |
|---|
| 790 | + * May be NULL if invoked internally by the filesystem. |
|---|
| 791 | + * @mapping: Readahead this filesystem object. |
|---|
| 792 | + */ |
|---|
| 793 | +struct readahead_control { |
|---|
| 794 | + struct file *file; |
|---|
| 795 | + struct address_space *mapping; |
|---|
| 796 | +/* private: use the readahead_* accessors instead */ |
|---|
| 797 | + pgoff_t _index; |
|---|
| 798 | + unsigned int _nr_pages; |
|---|
| 799 | + unsigned int _batch_count; |
|---|
| 800 | +}; |
|---|
| 801 | + |
|---|
| 802 | +#define DEFINE_READAHEAD(rac, f, m, i) \ |
|---|
| 803 | + struct readahead_control rac = { \ |
|---|
| 804 | + .file = f, \ |
|---|
| 805 | + .mapping = m, \ |
|---|
| 806 | + ._index = i, \ |
|---|
| 807 | + } |
|---|
| 808 | + |
|---|
| 809 | +#define VM_READAHEAD_PAGES (SZ_128K / PAGE_SIZE) |
|---|
| 810 | + |
|---|
| 811 | +void page_cache_ra_unbounded(struct readahead_control *, |
|---|
| 812 | + unsigned long nr_to_read, unsigned long lookahead_count); |
|---|
| 813 | +void page_cache_sync_ra(struct readahead_control *, struct file_ra_state *, |
|---|
| 814 | + unsigned long req_count); |
|---|
| 815 | +void page_cache_async_ra(struct readahead_control *, struct file_ra_state *, |
|---|
| 816 | + struct page *, unsigned long req_count); |
|---|
| 817 | + |
|---|
| 818 | +/** |
|---|
| 819 | + * page_cache_sync_readahead - generic file readahead |
|---|
| 820 | + * @mapping: address_space which holds the pagecache and I/O vectors |
|---|
| 821 | + * @ra: file_ra_state which holds the readahead state |
|---|
| 822 | + * @file: Used by the filesystem for authentication. |
|---|
| 823 | + * @index: Index of first page to be read. |
|---|
| 824 | + * @req_count: Total number of pages being read by the caller. |
|---|
| 825 | + * |
|---|
| 826 | + * page_cache_sync_readahead() should be called when a cache miss happened: |
|---|
| 827 | + * it will submit the read. The readahead logic may decide to piggyback more |
|---|
| 828 | + * pages onto the read request if access patterns suggest it will improve |
|---|
| 829 | + * performance. |
|---|
| 830 | + */ |
|---|
| 831 | +static inline |
|---|
| 832 | +void page_cache_sync_readahead(struct address_space *mapping, |
|---|
| 833 | + struct file_ra_state *ra, struct file *file, pgoff_t index, |
|---|
| 834 | + unsigned long req_count) |
|---|
| 835 | +{ |
|---|
| 836 | + DEFINE_READAHEAD(ractl, file, mapping, index); |
|---|
| 837 | + page_cache_sync_ra(&ractl, ra, req_count); |
|---|
| 838 | +} |
|---|
| 839 | + |
|---|
| 840 | +/** |
|---|
| 841 | + * page_cache_async_readahead - file readahead for marked pages |
|---|
| 842 | + * @mapping: address_space which holds the pagecache and I/O vectors |
|---|
| 843 | + * @ra: file_ra_state which holds the readahead state |
|---|
| 844 | + * @file: Used by the filesystem for authentication. |
|---|
| 845 | + * @page: The page at @index which triggered the readahead call. |
|---|
| 846 | + * @index: Index of first page to be read. |
|---|
| 847 | + * @req_count: Total number of pages being read by the caller. |
|---|
| 848 | + * |
|---|
| 849 | + * page_cache_async_readahead() should be called when a page is used which |
|---|
| 850 | + * is marked as PageReadahead; this is a marker to suggest that the application |
|---|
| 851 | + * has used up enough of the readahead window that we should start pulling in |
|---|
| 852 | + * more pages. |
|---|
| 853 | + */ |
|---|
| 854 | +static inline |
|---|
| 855 | +void page_cache_async_readahead(struct address_space *mapping, |
|---|
| 856 | + struct file_ra_state *ra, struct file *file, |
|---|
| 857 | + struct page *page, pgoff_t index, unsigned long req_count) |
|---|
| 858 | +{ |
|---|
| 859 | + DEFINE_READAHEAD(ractl, file, mapping, index); |
|---|
| 860 | + page_cache_async_ra(&ractl, ra, page, req_count); |
|---|
| 861 | +} |
|---|
| 862 | + |
|---|
| 863 | +/** |
|---|
| 864 | + * readahead_page - Get the next page to read. |
|---|
| 865 | + * @rac: The current readahead request. |
|---|
| 866 | + * |
|---|
| 867 | + * Context: The page is locked and has an elevated refcount. The caller |
|---|
| 868 | + * should decreases the refcount once the page has been submitted for I/O |
|---|
| 869 | + * and unlock the page once all I/O to that page has completed. |
|---|
| 870 | + * Return: A pointer to the next page, or %NULL if we are done. |
|---|
| 871 | + */ |
|---|
| 872 | +static inline struct page *readahead_page(struct readahead_control *rac) |
|---|
| 873 | +{ |
|---|
| 874 | + struct page *page; |
|---|
| 875 | + |
|---|
| 876 | + BUG_ON(rac->_batch_count > rac->_nr_pages); |
|---|
| 877 | + rac->_nr_pages -= rac->_batch_count; |
|---|
| 878 | + rac->_index += rac->_batch_count; |
|---|
| 879 | + |
|---|
| 880 | + if (!rac->_nr_pages) { |
|---|
| 881 | + rac->_batch_count = 0; |
|---|
| 882 | + return NULL; |
|---|
| 883 | + } |
|---|
| 884 | + |
|---|
| 885 | + page = xa_load(&rac->mapping->i_pages, rac->_index); |
|---|
| 886 | + VM_BUG_ON_PAGE(!PageLocked(page), page); |
|---|
| 887 | + rac->_batch_count = thp_nr_pages(page); |
|---|
| 888 | + |
|---|
| 889 | + return page; |
|---|
| 890 | +} |
|---|
| 891 | + |
|---|
| 892 | +static inline unsigned int __readahead_batch(struct readahead_control *rac, |
|---|
| 893 | + struct page **array, unsigned int array_sz) |
|---|
| 894 | +{ |
|---|
| 895 | + unsigned int i = 0; |
|---|
| 896 | + XA_STATE(xas, &rac->mapping->i_pages, 0); |
|---|
| 897 | + struct page *page; |
|---|
| 898 | + |
|---|
| 899 | + BUG_ON(rac->_batch_count > rac->_nr_pages); |
|---|
| 900 | + rac->_nr_pages -= rac->_batch_count; |
|---|
| 901 | + rac->_index += rac->_batch_count; |
|---|
| 902 | + rac->_batch_count = 0; |
|---|
| 903 | + |
|---|
| 904 | + xas_set(&xas, rac->_index); |
|---|
| 905 | + rcu_read_lock(); |
|---|
| 906 | + xas_for_each(&xas, page, rac->_index + rac->_nr_pages - 1) { |
|---|
| 907 | + if (xas_retry(&xas, page)) |
|---|
| 908 | + continue; |
|---|
| 909 | + VM_BUG_ON_PAGE(!PageLocked(page), page); |
|---|
| 910 | + VM_BUG_ON_PAGE(PageTail(page), page); |
|---|
| 911 | + array[i++] = page; |
|---|
| 912 | + rac->_batch_count += thp_nr_pages(page); |
|---|
| 913 | + |
|---|
| 914 | + /* |
|---|
| 915 | + * The page cache isn't using multi-index entries yet, |
|---|
| 916 | + * so the xas cursor needs to be manually moved to the |
|---|
| 917 | + * next index. This can be removed once the page cache |
|---|
| 918 | + * is converted. |
|---|
| 919 | + */ |
|---|
| 920 | + if (PageHead(page)) |
|---|
| 921 | + xas_set(&xas, rac->_index + rac->_batch_count); |
|---|
| 922 | + |
|---|
| 923 | + if (i == array_sz) |
|---|
| 924 | + break; |
|---|
| 925 | + } |
|---|
| 926 | + rcu_read_unlock(); |
|---|
| 927 | + |
|---|
| 928 | + return i; |
|---|
| 929 | +} |
|---|
| 930 | + |
|---|
| 931 | +/** |
|---|
| 932 | + * readahead_page_batch - Get a batch of pages to read. |
|---|
| 933 | + * @rac: The current readahead request. |
|---|
| 934 | + * @array: An array of pointers to struct page. |
|---|
| 935 | + * |
|---|
| 936 | + * Context: The pages are locked and have an elevated refcount. The caller |
|---|
| 937 | + * should decreases the refcount once the page has been submitted for I/O |
|---|
| 938 | + * and unlock the page once all I/O to that page has completed. |
|---|
| 939 | + * Return: The number of pages placed in the array. 0 indicates the request |
|---|
| 940 | + * is complete. |
|---|
| 941 | + */ |
|---|
| 942 | +#define readahead_page_batch(rac, array) \ |
|---|
| 943 | + __readahead_batch(rac, array, ARRAY_SIZE(array)) |
|---|
| 944 | + |
|---|
| 945 | +/** |
|---|
| 946 | + * readahead_pos - The byte offset into the file of this readahead request. |
|---|
| 947 | + * @rac: The readahead request. |
|---|
| 948 | + */ |
|---|
| 949 | +static inline loff_t readahead_pos(struct readahead_control *rac) |
|---|
| 950 | +{ |
|---|
| 951 | + return (loff_t)rac->_index * PAGE_SIZE; |
|---|
| 952 | +} |
|---|
| 953 | + |
|---|
| 954 | +/** |
|---|
| 955 | + * readahead_length - The number of bytes in this readahead request. |
|---|
| 956 | + * @rac: The readahead request. |
|---|
| 957 | + */ |
|---|
| 958 | +static inline loff_t readahead_length(struct readahead_control *rac) |
|---|
| 959 | +{ |
|---|
| 960 | + return (loff_t)rac->_nr_pages * PAGE_SIZE; |
|---|
| 961 | +} |
|---|
| 962 | + |
|---|
| 963 | +/** |
|---|
| 964 | + * readahead_index - The index of the first page in this readahead request. |
|---|
| 965 | + * @rac: The readahead request. |
|---|
| 966 | + */ |
|---|
| 967 | +static inline pgoff_t readahead_index(struct readahead_control *rac) |
|---|
| 968 | +{ |
|---|
| 969 | + return rac->_index; |
|---|
| 970 | +} |
|---|
| 971 | + |
|---|
| 972 | +/** |
|---|
| 973 | + * readahead_count - The number of pages in this readahead request. |
|---|
| 974 | + * @rac: The readahead request. |
|---|
| 975 | + */ |
|---|
| 976 | +static inline unsigned int readahead_count(struct readahead_control *rac) |
|---|
| 977 | +{ |
|---|
| 978 | + return rac->_nr_pages; |
|---|
| 979 | +} |
|---|
| 980 | + |
|---|
| 644 | 981 | static inline unsigned long dir_pages(struct inode *inode) |
|---|
| 645 | 982 | { |
|---|
| 646 | 983 | return (unsigned long)(inode->i_size + PAGE_SIZE - 1) >> |
|---|
| 647 | 984 | PAGE_SHIFT; |
|---|
| 648 | 985 | } |
|---|
| 649 | 986 | |
|---|
| 987 | +/** |
|---|
| 988 | + * page_mkwrite_check_truncate - check if page was truncated |
|---|
| 989 | + * @page: the page to check |
|---|
| 990 | + * @inode: the inode to check the page against |
|---|
| 991 | + * |
|---|
| 992 | + * Returns the number of bytes in the page up to EOF, |
|---|
| 993 | + * or -EFAULT if the page was truncated. |
|---|
| 994 | + */ |
|---|
| 995 | +static inline int page_mkwrite_check_truncate(struct page *page, |
|---|
| 996 | + struct inode *inode) |
|---|
| 997 | +{ |
|---|
| 998 | + loff_t size = i_size_read(inode); |
|---|
| 999 | + pgoff_t index = size >> PAGE_SHIFT; |
|---|
| 1000 | + int offset = offset_in_page(size); |
|---|
| 1001 | + |
|---|
| 1002 | + if (page->mapping != inode->i_mapping) |
|---|
| 1003 | + return -EFAULT; |
|---|
| 1004 | + |
|---|
| 1005 | + /* page is wholly inside EOF */ |
|---|
| 1006 | + if (page->index < index) |
|---|
| 1007 | + return PAGE_SIZE; |
|---|
| 1008 | + /* page is wholly past EOF */ |
|---|
| 1009 | + if (page->index > index || !offset) |
|---|
| 1010 | + return -EFAULT; |
|---|
| 1011 | + /* page is partially inside EOF */ |
|---|
| 1012 | + return offset; |
|---|
| 1013 | +} |
|---|
| 1014 | + |
|---|
| 1015 | +/** |
|---|
| 1016 | + * i_blocks_per_page - How many blocks fit in this page. |
|---|
| 1017 | + * @inode: The inode which contains the blocks. |
|---|
| 1018 | + * @page: The page (head page if the page is a THP). |
|---|
| 1019 | + * |
|---|
| 1020 | + * If the block size is larger than the size of this page, return zero. |
|---|
| 1021 | + * |
|---|
| 1022 | + * Context: The caller should hold a refcount on the page to prevent it |
|---|
| 1023 | + * from being split. |
|---|
| 1024 | + * Return: The number of filesystem blocks covered by this page. |
|---|
| 1025 | + */ |
|---|
| 1026 | +static inline |
|---|
| 1027 | +unsigned int i_blocks_per_page(struct inode *inode, struct page *page) |
|---|
| 1028 | +{ |
|---|
| 1029 | + return thp_size(page) >> inode->i_blkbits; |
|---|
| 1030 | +} |
|---|
| 650 | 1031 | #endif /* _LINUX_PAGEMAP_H */ |
|---|