]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blob - include/linux/pagemap.h
vfs: keep inodes with page cache off the inode shrinker LRU
[mirror_ubuntu-kernels.git] / include / linux / pagemap.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PAGEMAP_H
3 #define _LINUX_PAGEMAP_H
4
5 /*
6 * Copyright 1995 Linus Torvalds
7 */
8 #include <linux/mm.h>
9 #include <linux/fs.h>
10 #include <linux/list.h>
11 #include <linux/highmem.h>
12 #include <linux/compiler.h>
13 #include <linux/uaccess.h>
14 #include <linux/gfp.h>
15 #include <linux/bitops.h>
16 #include <linux/hardirq.h> /* for in_interrupt() */
17 #include <linux/hugetlb_inline.h>
18
19 struct pagevec;
20
21 static inline bool mapping_empty(struct address_space *mapping)
22 {
23 return xa_empty(&mapping->i_pages);
24 }
25
26 /*
27 * mapping_shrinkable - test if page cache state allows inode reclaim
28 * @mapping: the page cache mapping
29 *
30 * This checks the mapping's cache state for the pupose of inode
31 * reclaim and LRU management.
32 *
33 * The caller is expected to hold the i_lock, but is not required to
34 * hold the i_pages lock, which usually protects cache state. That's
35 * because the i_lock and the list_lru lock that protect the inode and
36 * its LRU state don't nest inside the irq-safe i_pages lock.
37 *
38 * Cache deletions are performed under the i_lock, which ensures that
39 * when an inode goes empty, it will reliably get queued on the LRU.
40 *
41 * Cache additions do not acquire the i_lock and may race with this
42 * check, in which case we'll report the inode as shrinkable when it
43 * has cache pages. This is okay: the shrinker also checks the
44 * refcount and the referenced bit, which will be elevated or set in
45 * the process of adding new cache pages to an inode.
46 */
47 static inline bool mapping_shrinkable(struct address_space *mapping)
48 {
49 void *head;
50
51 /*
52 * On highmem systems, there could be lowmem pressure from the
53 * inodes before there is highmem pressure from the page
54 * cache. Make inodes shrinkable regardless of cache state.
55 */
56 if (IS_ENABLED(CONFIG_HIGHMEM))
57 return true;
58
59 /* Cache completely empty? Shrink away. */
60 head = rcu_access_pointer(mapping->i_pages.xa_head);
61 if (!head)
62 return true;
63
64 /*
65 * The xarray stores single offset-0 entries directly in the
66 * head pointer, which allows non-resident page cache entries
67 * to escape the shadow shrinker's list of xarray nodes. The
68 * inode shrinker needs to pick them up under memory pressure.
69 */
70 if (!xa_is_node(head) && xa_is_value(head))
71 return true;
72
73 return false;
74 }
75
76 /*
77 * Bits in mapping->flags.
78 */
79 enum mapping_flags {
80 AS_EIO = 0, /* IO error on async write */
81 AS_ENOSPC = 1, /* ENOSPC on async write */
82 AS_MM_ALL_LOCKS = 2, /* under mm_take_all_locks() */
83 AS_UNEVICTABLE = 3, /* e.g., ramdisk, SHM_LOCK */
84 AS_EXITING = 4, /* final truncate in progress */
85 /* writeback related tags are not used */
86 AS_NO_WRITEBACK_TAGS = 5,
87 AS_THP_SUPPORT = 6, /* THPs supported */
88 };
89
90 /**
91 * mapping_set_error - record a writeback error in the address_space
92 * @mapping: the mapping in which an error should be set
93 * @error: the error to set in the mapping
94 *
95 * When writeback fails in some way, we must record that error so that
96 * userspace can be informed when fsync and the like are called. We endeavor
97 * to report errors on any file that was open at the time of the error. Some
98 * internal callers also need to know when writeback errors have occurred.
99 *
100 * When a writeback error occurs, most filesystems will want to call
101 * mapping_set_error to record the error in the mapping so that it can be
102 * reported when the application calls fsync(2).
103 */
104 static inline void mapping_set_error(struct address_space *mapping, int error)
105 {
106 if (likely(!error))
107 return;
108
109 /* Record in wb_err for checkers using errseq_t based tracking */
110 __filemap_set_wb_err(mapping, error);
111
112 /* Record it in superblock */
113 if (mapping->host)
114 errseq_set(&mapping->host->i_sb->s_wb_err, error);
115
116 /* Record it in flags for now, for legacy callers */
117 if (error == -ENOSPC)
118 set_bit(AS_ENOSPC, &mapping->flags);
119 else
120 set_bit(AS_EIO, &mapping->flags);
121 }
122
123 static inline void mapping_set_unevictable(struct address_space *mapping)
124 {
125 set_bit(AS_UNEVICTABLE, &mapping->flags);
126 }
127
128 static inline void mapping_clear_unevictable(struct address_space *mapping)
129 {
130 clear_bit(AS_UNEVICTABLE, &mapping->flags);
131 }
132
133 static inline bool mapping_unevictable(struct address_space *mapping)
134 {
135 return mapping && test_bit(AS_UNEVICTABLE, &mapping->flags);
136 }
137
138 static inline void mapping_set_exiting(struct address_space *mapping)
139 {
140 set_bit(AS_EXITING, &mapping->flags);
141 }
142
143 static inline int mapping_exiting(struct address_space *mapping)
144 {
145 return test_bit(AS_EXITING, &mapping->flags);
146 }
147
148 static inline void mapping_set_no_writeback_tags(struct address_space *mapping)
149 {
150 set_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
151 }
152
153 static inline int mapping_use_writeback_tags(struct address_space *mapping)
154 {
155 return !test_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
156 }
157
158 static inline gfp_t mapping_gfp_mask(struct address_space * mapping)
159 {
160 return mapping->gfp_mask;
161 }
162
163 /* Restricts the given gfp_mask to what the mapping allows. */
164 static inline gfp_t mapping_gfp_constraint(struct address_space *mapping,
165 gfp_t gfp_mask)
166 {
167 return mapping_gfp_mask(mapping) & gfp_mask;
168 }
169
170 /*
171 * This is non-atomic. Only to be used before the mapping is activated.
172 * Probably needs a barrier...
173 */
174 static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
175 {
176 m->gfp_mask = mask;
177 }
178
179 static inline bool mapping_thp_support(struct address_space *mapping)
180 {
181 return test_bit(AS_THP_SUPPORT, &mapping->flags);
182 }
183
184 static inline int filemap_nr_thps(struct address_space *mapping)
185 {
186 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
187 return atomic_read(&mapping->nr_thps);
188 #else
189 return 0;
190 #endif
191 }
192
193 static inline void filemap_nr_thps_inc(struct address_space *mapping)
194 {
195 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
196 if (!mapping_thp_support(mapping))
197 atomic_inc(&mapping->nr_thps);
198 #else
199 WARN_ON_ONCE(1);
200 #endif
201 }
202
203 static inline void filemap_nr_thps_dec(struct address_space *mapping)
204 {
205 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
206 if (!mapping_thp_support(mapping))
207 atomic_dec(&mapping->nr_thps);
208 #else
209 WARN_ON_ONCE(1);
210 #endif
211 }
212
213 void release_pages(struct page **pages, int nr);
214
215 /*
216 * For file cache pages, return the address_space, otherwise return NULL
217 */
218 static inline struct address_space *page_mapping_file(struct page *page)
219 {
220 if (unlikely(PageSwapCache(page)))
221 return NULL;
222 return page_mapping(page);
223 }
224
225 /*
226 * speculatively take a reference to a page.
227 * If the page is free (_refcount == 0), then _refcount is untouched, and 0
228 * is returned. Otherwise, _refcount is incremented by 1 and 1 is returned.
229 *
230 * This function must be called inside the same rcu_read_lock() section as has
231 * been used to lookup the page in the pagecache radix-tree (or page table):
232 * this allows allocators to use a synchronize_rcu() to stabilize _refcount.
233 *
234 * Unless an RCU grace period has passed, the count of all pages coming out
235 * of the allocator must be considered unstable. page_count may return higher
236 * than expected, and put_page must be able to do the right thing when the
237 * page has been finished with, no matter what it is subsequently allocated
238 * for (because put_page is what is used here to drop an invalid speculative
239 * reference).
240 *
241 * This is the interesting part of the lockless pagecache (and lockless
242 * get_user_pages) locking protocol, where the lookup-side (eg. find_get_page)
243 * has the following pattern:
244 * 1. find page in radix tree
245 * 2. conditionally increment refcount
246 * 3. check the page is still in pagecache (if no, goto 1)
247 *
248 * Remove-side that cares about stability of _refcount (eg. reclaim) has the
249 * following (with the i_pages lock held):
250 * A. atomically check refcount is correct and set it to 0 (atomic_cmpxchg)
251 * B. remove page from pagecache
252 * C. free the page
253 *
254 * There are 2 critical interleavings that matter:
255 * - 2 runs before A: in this case, A sees elevated refcount and bails out
256 * - A runs before 2: in this case, 2 sees zero refcount and retries;
257 * subsequently, B will complete and 1 will find no page, causing the
258 * lookup to return NULL.
259 *
260 * It is possible that between 1 and 2, the page is removed then the exact same
261 * page is inserted into the same position in pagecache. That's OK: the
262 * old find_get_page using a lock could equally have run before or after
263 * such a re-insertion, depending on order that locks are granted.
264 *
265 * Lookups racing against pagecache insertion isn't a big problem: either 1
266 * will find the page or it will not. Likewise, the old find_get_page could run
267 * either before the insertion or afterwards, depending on timing.
268 */
269 static inline int __page_cache_add_speculative(struct page *page, int count)
270 {
271 #ifdef CONFIG_TINY_RCU
272 # ifdef CONFIG_PREEMPT_COUNT
273 VM_BUG_ON(!in_atomic() && !irqs_disabled());
274 # endif
275 /*
276 * Preempt must be disabled here - we rely on rcu_read_lock doing
277 * this for us.
278 *
279 * Pagecache won't be truncated from interrupt context, so if we have
280 * found a page in the radix tree here, we have pinned its refcount by
281 * disabling preempt, and hence no need for the "speculative get" that
282 * SMP requires.
283 */
284 VM_BUG_ON_PAGE(page_count(page) == 0, page);
285 page_ref_add(page, count);
286
287 #else
288 if (unlikely(!page_ref_add_unless(page, count, 0))) {
289 /*
290 * Either the page has been freed, or will be freed.
291 * In either case, retry here and the caller should
292 * do the right thing (see comments above).
293 */
294 return 0;
295 }
296 #endif
297 VM_BUG_ON_PAGE(PageTail(page), page);
298
299 return 1;
300 }
301
302 static inline int page_cache_get_speculative(struct page *page)
303 {
304 return __page_cache_add_speculative(page, 1);
305 }
306
307 static inline int page_cache_add_speculative(struct page *page, int count)
308 {
309 return __page_cache_add_speculative(page, count);
310 }
311
312 /**
313 * attach_page_private - Attach private data to a page.
314 * @page: Page to attach data to.
315 * @data: Data to attach to page.
316 *
317 * Attaching private data to a page increments the page's reference count.
318 * The data must be detached before the page will be freed.
319 */
320 static inline void attach_page_private(struct page *page, void *data)
321 {
322 get_page(page);
323 set_page_private(page, (unsigned long)data);
324 SetPagePrivate(page);
325 }
326
327 /**
328 * detach_page_private - Detach private data from a page.
329 * @page: Page to detach data from.
330 *
331 * Removes the data that was previously attached to the page and decrements
332 * the refcount on the page.
333 *
334 * Return: Data that was attached to the page.
335 */
336 static inline void *detach_page_private(struct page *page)
337 {
338 void *data = (void *)page_private(page);
339
340 if (!PagePrivate(page))
341 return NULL;
342 ClearPagePrivate(page);
343 set_page_private(page, 0);
344 put_page(page);
345
346 return data;
347 }
348
349 #ifdef CONFIG_NUMA
350 extern struct page *__page_cache_alloc(gfp_t gfp);
351 #else
352 static inline struct page *__page_cache_alloc(gfp_t gfp)
353 {
354 return alloc_pages(gfp, 0);
355 }
356 #endif
357
358 static inline struct page *page_cache_alloc(struct address_space *x)
359 {
360 return __page_cache_alloc(mapping_gfp_mask(x));
361 }
362
363 static inline gfp_t readahead_gfp_mask(struct address_space *x)
364 {
365 return mapping_gfp_mask(x) | __GFP_NORETRY | __GFP_NOWARN;
366 }
367
368 typedef int filler_t(void *, struct page *);
369
370 pgoff_t page_cache_next_miss(struct address_space *mapping,
371 pgoff_t index, unsigned long max_scan);
372 pgoff_t page_cache_prev_miss(struct address_space *mapping,
373 pgoff_t index, unsigned long max_scan);
374
375 #define FGP_ACCESSED 0x00000001
376 #define FGP_LOCK 0x00000002
377 #define FGP_CREAT 0x00000004
378 #define FGP_WRITE 0x00000008
379 #define FGP_NOFS 0x00000010
380 #define FGP_NOWAIT 0x00000020
381 #define FGP_FOR_MMAP 0x00000040
382 #define FGP_HEAD 0x00000080
383 #define FGP_ENTRY 0x00000100
384
385 struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
386 int fgp_flags, gfp_t cache_gfp_mask);
387
388 /**
389 * find_get_page - find and get a page reference
390 * @mapping: the address_space to search
391 * @offset: the page index
392 *
393 * Looks up the page cache slot at @mapping & @offset. If there is a
394 * page cache page, it is returned with an increased refcount.
395 *
396 * Otherwise, %NULL is returned.
397 */
398 static inline struct page *find_get_page(struct address_space *mapping,
399 pgoff_t offset)
400 {
401 return pagecache_get_page(mapping, offset, 0, 0);
402 }
403
404 static inline struct page *find_get_page_flags(struct address_space *mapping,
405 pgoff_t offset, int fgp_flags)
406 {
407 return pagecache_get_page(mapping, offset, fgp_flags, 0);
408 }
409
410 /**
411 * find_lock_page - locate, pin and lock a pagecache page
412 * @mapping: the address_space to search
413 * @index: the page index
414 *
415 * Looks up the page cache entry at @mapping & @index. If there is a
416 * page cache page, it is returned locked and with an increased
417 * refcount.
418 *
419 * Context: May sleep.
420 * Return: A struct page or %NULL if there is no page in the cache for this
421 * index.
422 */
423 static inline struct page *find_lock_page(struct address_space *mapping,
424 pgoff_t index)
425 {
426 return pagecache_get_page(mapping, index, FGP_LOCK, 0);
427 }
428
429 /**
430 * find_lock_head - Locate, pin and lock a pagecache page.
431 * @mapping: The address_space to search.
432 * @index: The page index.
433 *
434 * Looks up the page cache entry at @mapping & @index. If there is a
435 * page cache page, its head page is returned locked and with an increased
436 * refcount.
437 *
438 * Context: May sleep.
439 * Return: A struct page which is !PageTail, or %NULL if there is no page
440 * in the cache for this index.
441 */
442 static inline struct page *find_lock_head(struct address_space *mapping,
443 pgoff_t index)
444 {
445 return pagecache_get_page(mapping, index, FGP_LOCK | FGP_HEAD, 0);
446 }
447
448 /**
449 * find_or_create_page - locate or add a pagecache page
450 * @mapping: the page's address_space
451 * @index: the page's index into the mapping
452 * @gfp_mask: page allocation mode
453 *
454 * Looks up the page cache slot at @mapping & @offset. If there is a
455 * page cache page, it is returned locked and with an increased
456 * refcount.
457 *
458 * If the page is not present, a new page is allocated using @gfp_mask
459 * and added to the page cache and the VM's LRU list. The page is
460 * returned locked and with an increased refcount.
461 *
462 * On memory exhaustion, %NULL is returned.
463 *
464 * find_or_create_page() may sleep, even if @gfp_flags specifies an
465 * atomic allocation!
466 */
467 static inline struct page *find_or_create_page(struct address_space *mapping,
468 pgoff_t index, gfp_t gfp_mask)
469 {
470 return pagecache_get_page(mapping, index,
471 FGP_LOCK|FGP_ACCESSED|FGP_CREAT,
472 gfp_mask);
473 }
474
475 /**
476 * grab_cache_page_nowait - returns locked page at given index in given cache
477 * @mapping: target address_space
478 * @index: the page index
479 *
480 * Same as grab_cache_page(), but do not wait if the page is unavailable.
481 * This is intended for speculative data generators, where the data can
482 * be regenerated if the page couldn't be grabbed. This routine should
483 * be safe to call while holding the lock for another page.
484 *
485 * Clear __GFP_FS when allocating the page to avoid recursion into the fs
486 * and deadlock against the caller's locked page.
487 */
488 static inline struct page *grab_cache_page_nowait(struct address_space *mapping,
489 pgoff_t index)
490 {
491 return pagecache_get_page(mapping, index,
492 FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
493 mapping_gfp_mask(mapping));
494 }
495
496 /* Does this page contain this index? */
497 static inline bool thp_contains(struct page *head, pgoff_t index)
498 {
499 /* HugeTLBfs indexes the page cache in units of hpage_size */
500 if (PageHuge(head))
501 return head->index == index;
502 return page_index(head) == (index & ~(thp_nr_pages(head) - 1UL));
503 }
504
505 /*
506 * Given the page we found in the page cache, return the page corresponding
507 * to this index in the file
508 */
509 static inline struct page *find_subpage(struct page *head, pgoff_t index)
510 {
511 /* HugeTLBfs wants the head page regardless */
512 if (PageHuge(head))
513 return head;
514
515 return head + (index & (thp_nr_pages(head) - 1));
516 }
517
518 unsigned find_get_entries(struct address_space *mapping, pgoff_t start,
519 pgoff_t end, struct pagevec *pvec, pgoff_t *indices);
520 unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
521 pgoff_t end, unsigned int nr_pages,
522 struct page **pages);
523 static inline unsigned find_get_pages(struct address_space *mapping,
524 pgoff_t *start, unsigned int nr_pages,
525 struct page **pages)
526 {
527 return find_get_pages_range(mapping, start, (pgoff_t)-1, nr_pages,
528 pages);
529 }
530 unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
531 unsigned int nr_pages, struct page **pages);
532 unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
533 pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
534 struct page **pages);
535 static inline unsigned find_get_pages_tag(struct address_space *mapping,
536 pgoff_t *index, xa_mark_t tag, unsigned int nr_pages,
537 struct page **pages)
538 {
539 return find_get_pages_range_tag(mapping, index, (pgoff_t)-1, tag,
540 nr_pages, pages);
541 }
542
543 struct page *grab_cache_page_write_begin(struct address_space *mapping,
544 pgoff_t index, unsigned flags);
545
546 /*
547 * Returns locked page at given index in given cache, creating it if needed.
548 */
549 static inline struct page *grab_cache_page(struct address_space *mapping,
550 pgoff_t index)
551 {
552 return find_or_create_page(mapping, index, mapping_gfp_mask(mapping));
553 }
554
555 extern struct page * read_cache_page(struct address_space *mapping,
556 pgoff_t index, filler_t *filler, void *data);
557 extern struct page * read_cache_page_gfp(struct address_space *mapping,
558 pgoff_t index, gfp_t gfp_mask);
559 extern int read_cache_pages(struct address_space *mapping,
560 struct list_head *pages, filler_t *filler, void *data);
561
562 static inline struct page *read_mapping_page(struct address_space *mapping,
563 pgoff_t index, void *data)
564 {
565 return read_cache_page(mapping, index, NULL, data);
566 }
567
568 /*
569 * Get index of the page within radix-tree (but not for hugetlb pages).
570 * (TODO: remove once hugetlb pages will have ->index in PAGE_SIZE)
571 */
572 static inline pgoff_t page_to_index(struct page *page)
573 {
574 struct page *head;
575
576 if (likely(!PageTransTail(page)))
577 return page->index;
578
579 head = compound_head(page);
580 /*
581 * We don't initialize ->index for tail pages: calculate based on
582 * head page
583 */
584 return head->index + page - head;
585 }
586
587 extern pgoff_t hugetlb_basepage_index(struct page *page);
588
589 /*
590 * Get the offset in PAGE_SIZE (even for hugetlb pages).
591 * (TODO: hugetlb pages should have ->index in PAGE_SIZE)
592 */
593 static inline pgoff_t page_to_pgoff(struct page *page)
594 {
595 if (unlikely(PageHuge(page)))
596 return hugetlb_basepage_index(page);
597 return page_to_index(page);
598 }
599
600 /*
601 * Return byte-offset into filesystem object for page.
602 */
603 static inline loff_t page_offset(struct page *page)
604 {
605 return ((loff_t)page->index) << PAGE_SHIFT;
606 }
607
608 static inline loff_t page_file_offset(struct page *page)
609 {
610 return ((loff_t)page_index(page)) << PAGE_SHIFT;
611 }
612
613 extern pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
614 unsigned long address);
615
616 static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
617 unsigned long address)
618 {
619 pgoff_t pgoff;
620 if (unlikely(is_vm_hugetlb_page(vma)))
621 return linear_hugepage_index(vma, address);
622 pgoff = (address - vma->vm_start) >> PAGE_SHIFT;
623 pgoff += vma->vm_pgoff;
624 return pgoff;
625 }
626
627 struct wait_page_key {
628 struct page *page;
629 int bit_nr;
630 int page_match;
631 };
632
633 struct wait_page_queue {
634 struct page *page;
635 int bit_nr;
636 wait_queue_entry_t wait;
637 };
638
639 static inline bool wake_page_match(struct wait_page_queue *wait_page,
640 struct wait_page_key *key)
641 {
642 if (wait_page->page != key->page)
643 return false;
644 key->page_match = 1;
645
646 if (wait_page->bit_nr != key->bit_nr)
647 return false;
648
649 return true;
650 }
651
652 extern void __lock_page(struct page *page);
653 extern int __lock_page_killable(struct page *page);
654 extern int __lock_page_async(struct page *page, struct wait_page_queue *wait);
655 extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
656 unsigned int flags);
657 extern void unlock_page(struct page *page);
658
659 /*
660 * Return true if the page was successfully locked
661 */
662 static inline int trylock_page(struct page *page)
663 {
664 page = compound_head(page);
665 return (likely(!test_and_set_bit_lock(PG_locked, &page->flags)));
666 }
667
668 /*
669 * lock_page may only be called if we have the page's inode pinned.
670 */
671 static inline void lock_page(struct page *page)
672 {
673 might_sleep();
674 if (!trylock_page(page))
675 __lock_page(page);
676 }
677
678 /*
679 * lock_page_killable is like lock_page but can be interrupted by fatal
680 * signals. It returns 0 if it locked the page and -EINTR if it was
681 * killed while waiting.
682 */
683 static inline int lock_page_killable(struct page *page)
684 {
685 might_sleep();
686 if (!trylock_page(page))
687 return __lock_page_killable(page);
688 return 0;
689 }
690
691 /*
692 * lock_page_async - Lock the page, unless this would block. If the page
693 * is already locked, then queue a callback when the page becomes unlocked.
694 * This callback can then retry the operation.
695 *
696 * Returns 0 if the page is locked successfully, or -EIOCBQUEUED if the page
697 * was already locked and the callback defined in 'wait' was queued.
698 */
699 static inline int lock_page_async(struct page *page,
700 struct wait_page_queue *wait)
701 {
702 if (!trylock_page(page))
703 return __lock_page_async(page, wait);
704 return 0;
705 }
706
707 /*
708 * lock_page_or_retry - Lock the page, unless this would block and the
709 * caller indicated that it can handle a retry.
710 *
711 * Return value and mmap_lock implications depend on flags; see
712 * __lock_page_or_retry().
713 */
714 static inline int lock_page_or_retry(struct page *page, struct mm_struct *mm,
715 unsigned int flags)
716 {
717 might_sleep();
718 return trylock_page(page) || __lock_page_or_retry(page, mm, flags);
719 }
720
721 /*
722 * This is exported only for wait_on_page_locked/wait_on_page_writeback, etc.,
723 * and should not be used directly.
724 */
725 extern void wait_on_page_bit(struct page *page, int bit_nr);
726 extern int wait_on_page_bit_killable(struct page *page, int bit_nr);
727
728 /*
729 * Wait for a page to be unlocked.
730 *
731 * This must be called with the caller "holding" the page,
732 * ie with increased "page->count" so that the page won't
733 * go away during the wait..
734 */
735 static inline void wait_on_page_locked(struct page *page)
736 {
737 if (PageLocked(page))
738 wait_on_page_bit(compound_head(page), PG_locked);
739 }
740
741 static inline int wait_on_page_locked_killable(struct page *page)
742 {
743 if (!PageLocked(page))
744 return 0;
745 return wait_on_page_bit_killable(compound_head(page), PG_locked);
746 }
747
748 int put_and_wait_on_page_locked(struct page *page, int state);
749 void wait_on_page_writeback(struct page *page);
750 int wait_on_page_writeback_killable(struct page *page);
751 extern void end_page_writeback(struct page *page);
752 void wait_for_stable_page(struct page *page);
753
754 void __set_page_dirty(struct page *, struct address_space *, int warn);
755 int __set_page_dirty_nobuffers(struct page *page);
756 int __set_page_dirty_no_writeback(struct page *page);
757
758 void page_endio(struct page *page, bool is_write, int err);
759
760 /**
761 * set_page_private_2 - Set PG_private_2 on a page and take a ref
762 * @page: The page.
763 *
764 * Set the PG_private_2 flag on a page and take the reference needed for the VM
765 * to handle its lifetime correctly. This sets the flag and takes the
766 * reference unconditionally, so care must be taken not to set the flag again
767 * if it's already set.
768 */
769 static inline void set_page_private_2(struct page *page)
770 {
771 page = compound_head(page);
772 get_page(page);
773 SetPagePrivate2(page);
774 }
775
776 void end_page_private_2(struct page *page);
777 void wait_on_page_private_2(struct page *page);
778 int wait_on_page_private_2_killable(struct page *page);
779
780 /*
781 * Add an arbitrary waiter to a page's wait queue
782 */
783 extern void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter);
784
785 /*
786 * Fault everything in given userspace address range in.
787 */
788 static inline int fault_in_pages_writeable(char __user *uaddr, size_t size)
789 {
790 char __user *end = uaddr + size - 1;
791
792 if (unlikely(size == 0))
793 return 0;
794
795 if (unlikely(uaddr > end))
796 return -EFAULT;
797 /*
798 * Writing zeroes into userspace here is OK, because we know that if
799 * the zero gets there, we'll be overwriting it.
800 */
801 do {
802 if (unlikely(__put_user(0, uaddr) != 0))
803 return -EFAULT;
804 uaddr += PAGE_SIZE;
805 } while (uaddr <= end);
806
807 /* Check whether the range spilled into the next page. */
808 if (((unsigned long)uaddr & PAGE_MASK) ==
809 ((unsigned long)end & PAGE_MASK))
810 return __put_user(0, end);
811
812 return 0;
813 }
814
815 static inline int fault_in_pages_readable(const char __user *uaddr, size_t size)
816 {
817 volatile char c;
818 const char __user *end = uaddr + size - 1;
819
820 if (unlikely(size == 0))
821 return 0;
822
823 if (unlikely(uaddr > end))
824 return -EFAULT;
825
826 do {
827 if (unlikely(__get_user(c, uaddr) != 0))
828 return -EFAULT;
829 uaddr += PAGE_SIZE;
830 } while (uaddr <= end);
831
832 /* Check whether the range spilled into the next page. */
833 if (((unsigned long)uaddr & PAGE_MASK) ==
834 ((unsigned long)end & PAGE_MASK)) {
835 return __get_user(c, end);
836 }
837
838 (void)c;
839 return 0;
840 }
841
842 int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
843 pgoff_t index, gfp_t gfp_mask);
844 int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
845 pgoff_t index, gfp_t gfp_mask);
846 extern void delete_from_page_cache(struct page *page);
847 extern void __delete_from_page_cache(struct page *page, void *shadow);
848 void replace_page_cache_page(struct page *old, struct page *new);
849 void delete_from_page_cache_batch(struct address_space *mapping,
850 struct pagevec *pvec);
851 loff_t mapping_seek_hole_data(struct address_space *, loff_t start, loff_t end,
852 int whence);
853
854 /*
855 * Like add_to_page_cache_locked, but used to add newly allocated pages:
856 * the page is new, so we can just run __SetPageLocked() against it.
857 */
858 static inline int add_to_page_cache(struct page *page,
859 struct address_space *mapping, pgoff_t offset, gfp_t gfp_mask)
860 {
861 int error;
862
863 __SetPageLocked(page);
864 error = add_to_page_cache_locked(page, mapping, offset, gfp_mask);
865 if (unlikely(error))
866 __ClearPageLocked(page);
867 return error;
868 }
869
870 /**
871 * struct readahead_control - Describes a readahead request.
872 *
873 * A readahead request is for consecutive pages. Filesystems which
874 * implement the ->readahead method should call readahead_page() or
875 * readahead_page_batch() in a loop and attempt to start I/O against
876 * each page in the request.
877 *
878 * Most of the fields in this struct are private and should be accessed
879 * by the functions below.
880 *
881 * @file: The file, used primarily by network filesystems for authentication.
882 * May be NULL if invoked internally by the filesystem.
883 * @mapping: Readahead this filesystem object.
884 * @ra: File readahead state. May be NULL.
885 */
886 struct readahead_control {
887 struct file *file;
888 struct address_space *mapping;
889 struct file_ra_state *ra;
890 /* private: use the readahead_* accessors instead */
891 pgoff_t _index;
892 unsigned int _nr_pages;
893 unsigned int _batch_count;
894 };
895
896 #define DEFINE_READAHEAD(ractl, f, r, m, i) \
897 struct readahead_control ractl = { \
898 .file = f, \
899 .mapping = m, \
900 .ra = r, \
901 ._index = i, \
902 }
903
904 #define VM_READAHEAD_PAGES (SZ_128K / PAGE_SIZE)
905
906 void page_cache_ra_unbounded(struct readahead_control *,
907 unsigned long nr_to_read, unsigned long lookahead_count);
908 void page_cache_sync_ra(struct readahead_control *, unsigned long req_count);
909 void page_cache_async_ra(struct readahead_control *, struct page *,
910 unsigned long req_count);
911 void readahead_expand(struct readahead_control *ractl,
912 loff_t new_start, size_t new_len);
913
914 /**
915 * page_cache_sync_readahead - generic file readahead
916 * @mapping: address_space which holds the pagecache and I/O vectors
917 * @ra: file_ra_state which holds the readahead state
918 * @file: Used by the filesystem for authentication.
919 * @index: Index of first page to be read.
920 * @req_count: Total number of pages being read by the caller.
921 *
922 * page_cache_sync_readahead() should be called when a cache miss happened:
923 * it will submit the read. The readahead logic may decide to piggyback more
924 * pages onto the read request if access patterns suggest it will improve
925 * performance.
926 */
927 static inline
928 void page_cache_sync_readahead(struct address_space *mapping,
929 struct file_ra_state *ra, struct file *file, pgoff_t index,
930 unsigned long req_count)
931 {
932 DEFINE_READAHEAD(ractl, file, ra, mapping, index);
933 page_cache_sync_ra(&ractl, req_count);
934 }
935
936 /**
937 * page_cache_async_readahead - file readahead for marked pages
938 * @mapping: address_space which holds the pagecache and I/O vectors
939 * @ra: file_ra_state which holds the readahead state
940 * @file: Used by the filesystem for authentication.
941 * @page: The page at @index which triggered the readahead call.
942 * @index: Index of first page to be read.
943 * @req_count: Total number of pages being read by the caller.
944 *
945 * page_cache_async_readahead() should be called when a page is used which
946 * is marked as PageReadahead; this is a marker to suggest that the application
947 * has used up enough of the readahead window that we should start pulling in
948 * more pages.
949 */
950 static inline
951 void page_cache_async_readahead(struct address_space *mapping,
952 struct file_ra_state *ra, struct file *file,
953 struct page *page, pgoff_t index, unsigned long req_count)
954 {
955 DEFINE_READAHEAD(ractl, file, ra, mapping, index);
956 page_cache_async_ra(&ractl, page, req_count);
957 }
958
959 /**
960 * readahead_page - Get the next page to read.
961 * @rac: The current readahead request.
962 *
963 * Context: The page is locked and has an elevated refcount. The caller
964 * should decreases the refcount once the page has been submitted for I/O
965 * and unlock the page once all I/O to that page has completed.
966 * Return: A pointer to the next page, or %NULL if we are done.
967 */
968 static inline struct page *readahead_page(struct readahead_control *rac)
969 {
970 struct page *page;
971
972 BUG_ON(rac->_batch_count > rac->_nr_pages);
973 rac->_nr_pages -= rac->_batch_count;
974 rac->_index += rac->_batch_count;
975
976 if (!rac->_nr_pages) {
977 rac->_batch_count = 0;
978 return NULL;
979 }
980
981 page = xa_load(&rac->mapping->i_pages, rac->_index);
982 VM_BUG_ON_PAGE(!PageLocked(page), page);
983 rac->_batch_count = thp_nr_pages(page);
984
985 return page;
986 }
987
988 static inline unsigned int __readahead_batch(struct readahead_control *rac,
989 struct page **array, unsigned int array_sz)
990 {
991 unsigned int i = 0;
992 XA_STATE(xas, &rac->mapping->i_pages, 0);
993 struct page *page;
994
995 BUG_ON(rac->_batch_count > rac->_nr_pages);
996 rac->_nr_pages -= rac->_batch_count;
997 rac->_index += rac->_batch_count;
998 rac->_batch_count = 0;
999
1000 xas_set(&xas, rac->_index);
1001 rcu_read_lock();
1002 xas_for_each(&xas, page, rac->_index + rac->_nr_pages - 1) {
1003 if (xas_retry(&xas, page))
1004 continue;
1005 VM_BUG_ON_PAGE(!PageLocked(page), page);
1006 VM_BUG_ON_PAGE(PageTail(page), page);
1007 array[i++] = page;
1008 rac->_batch_count += thp_nr_pages(page);
1009
1010 /*
1011 * The page cache isn't using multi-index entries yet,
1012 * so the xas cursor needs to be manually moved to the
1013 * next index. This can be removed once the page cache
1014 * is converted.
1015 */
1016 if (PageHead(page))
1017 xas_set(&xas, rac->_index + rac->_batch_count);
1018
1019 if (i == array_sz)
1020 break;
1021 }
1022 rcu_read_unlock();
1023
1024 return i;
1025 }
1026
1027 /**
1028 * readahead_page_batch - Get a batch of pages to read.
1029 * @rac: The current readahead request.
1030 * @array: An array of pointers to struct page.
1031 *
1032 * Context: The pages are locked and have an elevated refcount. The caller
1033 * should decreases the refcount once the page has been submitted for I/O
1034 * and unlock the page once all I/O to that page has completed.
1035 * Return: The number of pages placed in the array. 0 indicates the request
1036 * is complete.
1037 */
1038 #define readahead_page_batch(rac, array) \
1039 __readahead_batch(rac, array, ARRAY_SIZE(array))
1040
1041 /**
1042 * readahead_pos - The byte offset into the file of this readahead request.
1043 * @rac: The readahead request.
1044 */
1045 static inline loff_t readahead_pos(struct readahead_control *rac)
1046 {
1047 return (loff_t)rac->_index * PAGE_SIZE;
1048 }
1049
1050 /**
1051 * readahead_length - The number of bytes in this readahead request.
1052 * @rac: The readahead request.
1053 */
1054 static inline size_t readahead_length(struct readahead_control *rac)
1055 {
1056 return rac->_nr_pages * PAGE_SIZE;
1057 }
1058
1059 /**
1060 * readahead_index - The index of the first page in this readahead request.
1061 * @rac: The readahead request.
1062 */
1063 static inline pgoff_t readahead_index(struct readahead_control *rac)
1064 {
1065 return rac->_index;
1066 }
1067
1068 /**
1069 * readahead_count - The number of pages in this readahead request.
1070 * @rac: The readahead request.
1071 */
1072 static inline unsigned int readahead_count(struct readahead_control *rac)
1073 {
1074 return rac->_nr_pages;
1075 }
1076
1077 /**
1078 * readahead_batch_length - The number of bytes in the current batch.
1079 * @rac: The readahead request.
1080 */
1081 static inline size_t readahead_batch_length(struct readahead_control *rac)
1082 {
1083 return rac->_batch_count * PAGE_SIZE;
1084 }
1085
1086 static inline unsigned long dir_pages(struct inode *inode)
1087 {
1088 return (unsigned long)(inode->i_size + PAGE_SIZE - 1) >>
1089 PAGE_SHIFT;
1090 }
1091
1092 /**
1093 * page_mkwrite_check_truncate - check if page was truncated
1094 * @page: the page to check
1095 * @inode: the inode to check the page against
1096 *
1097 * Returns the number of bytes in the page up to EOF,
1098 * or -EFAULT if the page was truncated.
1099 */
1100 static inline int page_mkwrite_check_truncate(struct page *page,
1101 struct inode *inode)
1102 {
1103 loff_t size = i_size_read(inode);
1104 pgoff_t index = size >> PAGE_SHIFT;
1105 int offset = offset_in_page(size);
1106
1107 if (page->mapping != inode->i_mapping)
1108 return -EFAULT;
1109
1110 /* page is wholly inside EOF */
1111 if (page->index < index)
1112 return PAGE_SIZE;
1113 /* page is wholly past EOF */
1114 if (page->index > index || !offset)
1115 return -EFAULT;
1116 /* page is partially inside EOF */
1117 return offset;
1118 }
1119
1120 /**
1121 * i_blocks_per_page - How many blocks fit in this page.
1122 * @inode: The inode which contains the blocks.
1123 * @page: The page (head page if the page is a THP).
1124 *
1125 * If the block size is larger than the size of this page, return zero.
1126 *
1127 * Context: The caller should hold a refcount on the page to prevent it
1128 * from being split.
1129 * Return: The number of filesystem blocks covered by this page.
1130 */
1131 static inline
1132 unsigned int i_blocks_per_page(struct inode *inode, struct page *page)
1133 {
1134 return thp_size(page) >> inode->i_blkbits;
1135 }
1136 #endif /* _LINUX_PAGEMAP_H */