• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <linux/sched/debug.h>
19 
20 struct pagevec;
21 
22 /*
23  * Bits in mapping->flags.
24  */
25 enum mapping_flags {
26 	AS_EIO		= 0,	/* IO error on async write */
27 	AS_ENOSPC	= 1,	/* ENOSPC on async write */
28 	AS_MM_ALL_LOCKS	= 2,	/* under mm_take_all_locks() */
29 	AS_UNEVICTABLE	= 3,	/* e.g., ramdisk, SHM_LOCK */
30 	AS_EXITING	= 4, 	/* final truncate in progress */
31 	/* writeback related tags are not used */
32 	AS_NO_WRITEBACK_TAGS = 5,
33 	AS_THP_SUPPORT = 6,	/* THPs supported */
34 };
35 
36 /**
37  * mapping_set_error - record a writeback error in the address_space
38  * @mapping: the mapping in which an error should be set
39  * @error: the error to set in the mapping
40  *
41  * When writeback fails in some way, we must record that error so that
42  * userspace can be informed when fsync and the like are called.  We endeavor
43  * to report errors on any file that was open at the time of the error.  Some
44  * internal callers also need to know when writeback errors have occurred.
45  *
46  * When a writeback error occurs, most filesystems will want to call
47  * mapping_set_error to record the error in the mapping so that it can be
48  * reported when the application calls fsync(2).
49  */
mapping_set_error(struct address_space * mapping,int error)50 static inline void mapping_set_error(struct address_space *mapping, int error)
51 {
52 	if (likely(!error))
53 		return;
54 
55 	/* Record in wb_err for checkers using errseq_t based tracking */
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);
61 
62 	/* Record it in flags for now, for legacy callers */
63 	if (error == -ENOSPC)
64 		set_bit(AS_ENOSPC, &mapping->flags);
65 	else
66 		set_bit(AS_EIO, &mapping->flags);
67 }
68 
mapping_set_unevictable(struct address_space * mapping)69 static inline void mapping_set_unevictable(struct address_space *mapping)
70 {
71 	set_bit(AS_UNEVICTABLE, &mapping->flags);
72 }
73 
mapping_clear_unevictable(struct address_space * mapping)74 static inline void mapping_clear_unevictable(struct address_space *mapping)
75 {
76 	clear_bit(AS_UNEVICTABLE, &mapping->flags);
77 }
78 
mapping_unevictable(struct address_space * mapping)79 static inline bool mapping_unevictable(struct address_space *mapping)
80 {
81 	return mapping && test_bit(AS_UNEVICTABLE, &mapping->flags);
82 }
83 
mapping_set_exiting(struct address_space * mapping)84 static inline void mapping_set_exiting(struct address_space *mapping)
85 {
86 	set_bit(AS_EXITING, &mapping->flags);
87 }
88 
mapping_exiting(struct address_space * mapping)89 static inline int mapping_exiting(struct address_space *mapping)
90 {
91 	return test_bit(AS_EXITING, &mapping->flags);
92 }
93 
mapping_set_no_writeback_tags(struct address_space * mapping)94 static inline void mapping_set_no_writeback_tags(struct address_space *mapping)
95 {
96 	set_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
97 }
98 
mapping_use_writeback_tags(struct address_space * mapping)99 static inline int mapping_use_writeback_tags(struct address_space *mapping)
100 {
101 	return !test_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
102 }
103 
mapping_gfp_mask(struct address_space * mapping)104 static inline gfp_t mapping_gfp_mask(struct address_space * mapping)
105 {
106 	return mapping->gfp_mask;
107 }
108 
109 /* Restricts the given gfp_mask to what the mapping allows. */
mapping_gfp_constraint(struct address_space * mapping,gfp_t gfp_mask)110 static inline gfp_t mapping_gfp_constraint(struct address_space *mapping,
111 		gfp_t gfp_mask)
112 {
113 	return mapping_gfp_mask(mapping) & gfp_mask;
114 }
115 
116 /*
117  * This is non-atomic.  Only to be used before the mapping is activated.
118  * Probably needs a barrier...
119  */
mapping_set_gfp_mask(struct address_space * m,gfp_t mask)120 static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
121 {
122 	m->gfp_mask = mask;
123 }
124 
mapping_thp_support(struct address_space * mapping)125 static inline bool mapping_thp_support(struct address_space *mapping)
126 {
127 	return test_bit(AS_THP_SUPPORT, &mapping->flags);
128 }
129 
filemap_nr_thps(struct address_space * mapping)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 
filemap_nr_thps_inc(struct address_space * mapping)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 
filemap_nr_thps_dec(struct address_space * mapping)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
157 }
158 
159 void release_pages(struct page **pages, int nr);
160 
161 /*
162  * speculatively take a reference to a page.
163  * If the page is free (_refcount == 0), then _refcount is untouched, and 0
164  * is returned. Otherwise, _refcount is incremented by 1 and 1 is returned.
165  *
166  * This function must be called inside the same rcu_read_lock() section as has
167  * been used to lookup the page in the pagecache radix-tree (or page table):
168  * this allows allocators to use a synchronize_rcu() to stabilize _refcount.
169  *
170  * Unless an RCU grace period has passed, the count of all pages coming out
171  * of the allocator must be considered unstable. page_count may return higher
172  * than expected, and put_page must be able to do the right thing when the
173  * page has been finished with, no matter what it is subsequently allocated
174  * for (because put_page is what is used here to drop an invalid speculative
175  * reference).
176  *
177  * This is the interesting part of the lockless pagecache (and lockless
178  * get_user_pages) locking protocol, where the lookup-side (eg. find_get_page)
179  * has the following pattern:
180  * 1. find page in radix tree
181  * 2. conditionally increment refcount
182  * 3. check the page is still in pagecache (if no, goto 1)
183  *
184  * Remove-side that cares about stability of _refcount (eg. reclaim) has the
185  * following (with the i_pages lock held):
186  * A. atomically check refcount is correct and set it to 0 (atomic_cmpxchg)
187  * B. remove page from pagecache
188  * C. free the page
189  *
190  * There are 2 critical interleavings that matter:
191  * - 2 runs before A: in this case, A sees elevated refcount and bails out
192  * - A runs before 2: in this case, 2 sees zero refcount and retries;
193  *   subsequently, B will complete and 1 will find no page, causing the
194  *   lookup to return NULL.
195  *
196  * It is possible that between 1 and 2, the page is removed then the exact same
197  * page is inserted into the same position in pagecache. That's OK: the
198  * old find_get_page using a lock could equally have run before or after
199  * such a re-insertion, depending on order that locks are granted.
200  *
201  * Lookups racing against pagecache insertion isn't a big problem: either 1
202  * will find the page or it will not. Likewise, the old find_get_page could run
203  * either before the insertion or afterwards, depending on timing.
204  */
__page_cache_add_speculative(struct page * page,int count)205 static inline int __page_cache_add_speculative(struct page *page, int count)
206 {
207 #ifdef CONFIG_TINY_RCU
208 # ifdef CONFIG_PREEMPT_COUNT
209 	VM_BUG_ON(!in_atomic() && !irqs_disabled());
210 # endif
211 	/*
212 	 * Preempt must be disabled here - we rely on rcu_read_lock doing
213 	 * this for us.
214 	 *
215 	 * Pagecache won't be truncated from interrupt context, so if we have
216 	 * found a page in the radix tree here, we have pinned its refcount by
217 	 * disabling preempt, and hence no need for the "speculative get" that
218 	 * SMP requires.
219 	 */
220 	VM_BUG_ON_PAGE(page_count(page) == 0, page);
221 	page_ref_add(page, count);
222 
223 #else
224 	if (unlikely(!page_ref_add_unless(page, count, 0))) {
225 		/*
226 		 * Either the page has been freed, or will be freed.
227 		 * In either case, retry here and the caller should
228 		 * do the right thing (see comments above).
229 		 */
230 		return 0;
231 	}
232 #endif
233 	VM_BUG_ON_PAGE(PageTail(page), page);
234 
235 	return 1;
236 }
237 
page_cache_get_speculative(struct page * page)238 static inline int page_cache_get_speculative(struct page *page)
239 {
240 	return __page_cache_add_speculative(page, 1);
241 }
242 
page_cache_add_speculative(struct page * page,int count)243 static inline int page_cache_add_speculative(struct page *page, int count)
244 {
245 	return __page_cache_add_speculative(page, count);
246 }
247 
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  */
attach_page_private(struct page * page,void * data)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 }
262 
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  */
detach_page_private(struct page * page)272 static inline void *detach_page_private(struct page *page)
273 {
274 	void *data = (void *)page_private(page);
275 
276 	if (!PagePrivate(page))
277 		return NULL;
278 	ClearPagePrivate(page);
279 	set_page_private(page, 0);
280 	put_page(page);
281 
282 	return data;
283 }
284 
285 #ifdef CONFIG_NUMA
286 extern struct page *__page_cache_alloc(gfp_t gfp);
287 #else
__page_cache_alloc(gfp_t gfp)288 static inline struct page *__page_cache_alloc(gfp_t gfp)
289 {
290 	return alloc_pages(gfp, 0);
291 }
292 #endif
293 
page_cache_alloc(struct address_space * x)294 static inline struct page *page_cache_alloc(struct address_space *x)
295 {
296 	return __page_cache_alloc(mapping_gfp_mask(x));
297 }
298 
299 gfp_t readahead_gfp_mask(struct address_space *x);
300 
301 typedef int filler_t(void *, struct page *);
302 
303 pgoff_t page_cache_next_miss(struct address_space *mapping,
304 			     pgoff_t index, unsigned long max_scan);
305 pgoff_t page_cache_prev_miss(struct address_space *mapping,
306 			     pgoff_t index, unsigned long max_scan);
307 
308 #define FGP_ACCESSED		0x00000001
309 #define FGP_LOCK		0x00000002
310 #define FGP_CREAT		0x00000004
311 #define FGP_WRITE		0x00000008
312 #define FGP_NOFS		0x00000010
313 #define FGP_NOWAIT		0x00000020
314 #define FGP_FOR_MMAP		0x00000040
315 #define FGP_HEAD		0x00000080
316 
317 struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
318 		int fgp_flags, gfp_t cache_gfp_mask);
319 
320 /**
321  * find_get_page - find and get a page reference
322  * @mapping: the address_space to search
323  * @offset: the page index
324  *
325  * Looks up the page cache slot at @mapping & @offset.  If there is a
326  * page cache page, it is returned with an increased refcount.
327  *
328  * Otherwise, %NULL is returned.
329  */
find_get_page(struct address_space * mapping,pgoff_t offset)330 static inline struct page *find_get_page(struct address_space *mapping,
331 					pgoff_t offset)
332 {
333 	return pagecache_get_page(mapping, offset, 0, 0);
334 }
335 
find_get_page_flags(struct address_space * mapping,pgoff_t offset,int fgp_flags)336 static inline struct page *find_get_page_flags(struct address_space *mapping,
337 					pgoff_t offset, int fgp_flags)
338 {
339 	return pagecache_get_page(mapping, offset, fgp_flags, 0);
340 }
341 
342 /**
343  * find_lock_page - locate, pin and lock a pagecache page
344  * @mapping: the address_space to search
345  * @index: the page index
346  *
347  * Looks up the page cache entry at @mapping & @index.  If there is a
348  * page cache page, it is returned locked and with an increased
349  * refcount.
350  *
351  * Context: May sleep.
352  * Return: A struct page or %NULL if there is no page in the cache for this
353  * index.
354  */
find_lock_page(struct address_space * mapping,pgoff_t index)355 static inline struct page *find_lock_page(struct address_space *mapping,
356 					pgoff_t index)
357 {
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  */
find_lock_head(struct address_space * mapping,pgoff_t index)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);
378 }
379 
380 /**
381  * find_or_create_page - locate or add a pagecache page
382  * @mapping: the page's address_space
383  * @index: the page's index into the mapping
384  * @gfp_mask: page allocation mode
385  *
386  * Looks up the page cache slot at @mapping & @offset.  If there is a
387  * page cache page, it is returned locked and with an increased
388  * refcount.
389  *
390  * If the page is not present, a new page is allocated using @gfp_mask
391  * and added to the page cache and the VM's LRU list.  The page is
392  * returned locked and with an increased refcount.
393  *
394  * On memory exhaustion, %NULL is returned.
395  *
396  * find_or_create_page() may sleep, even if @gfp_flags specifies an
397  * atomic allocation!
398  */
find_or_create_page(struct address_space * mapping,pgoff_t index,gfp_t gfp_mask)399 static inline struct page *find_or_create_page(struct address_space *mapping,
400 					pgoff_t index, gfp_t gfp_mask)
401 {
402 	return pagecache_get_page(mapping, index,
403 					FGP_LOCK|FGP_ACCESSED|FGP_CREAT,
404 					gfp_mask);
405 }
406 
407 /**
408  * grab_cache_page_nowait - returns locked page at given index in given cache
409  * @mapping: target address_space
410  * @index: the page index
411  *
412  * Same as grab_cache_page(), but do not wait if the page is unavailable.
413  * This is intended for speculative data generators, where the data can
414  * be regenerated if the page couldn't be grabbed.  This routine should
415  * be safe to call while holding the lock for another page.
416  *
417  * Clear __GFP_FS when allocating the page to avoid recursion into the fs
418  * and deadlock against the caller's locked page.
419  */
grab_cache_page_nowait(struct address_space * mapping,pgoff_t index)420 static inline struct page *grab_cache_page_nowait(struct address_space *mapping,
421 				pgoff_t index)
422 {
423 	return pagecache_get_page(mapping, index,
424 			FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
425 			mapping_gfp_mask(mapping));
426 }
427 
428 /* Does this page contain this index? */
thp_contains(struct page * head,pgoff_t 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  */
find_subpage(struct page * head,pgoff_t index)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 
450 unsigned find_get_entries(struct address_space *mapping, pgoff_t start,
451 			  unsigned int nr_entries, struct page **entries,
452 			  pgoff_t *indices);
453 unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start,
454 			pgoff_t end, unsigned int nr_pages,
455 			struct page **pages);
find_get_pages(struct address_space * mapping,pgoff_t * start,unsigned int nr_pages,struct page ** pages)456 static inline unsigned find_get_pages(struct address_space *mapping,
457 			pgoff_t *start, unsigned int nr_pages,
458 			struct page **pages)
459 {
460 	return find_get_pages_range(mapping, start, (pgoff_t)-1, nr_pages,
461 				    pages);
462 }
463 unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
464 			       unsigned int nr_pages, struct page **pages);
465 unsigned find_get_pages_range_tag(struct address_space *mapping, pgoff_t *index,
466 			pgoff_t end, xa_mark_t tag, unsigned int nr_pages,
467 			struct page **pages);
find_get_pages_tag(struct address_space * mapping,pgoff_t * index,xa_mark_t tag,unsigned int nr_pages,struct page ** pages)468 static inline unsigned find_get_pages_tag(struct address_space *mapping,
469 			pgoff_t *index, xa_mark_t tag, unsigned int nr_pages,
470 			struct page **pages)
471 {
472 	return find_get_pages_range_tag(mapping, index, (pgoff_t)-1, tag,
473 					nr_pages, pages);
474 }
475 
476 struct page *grab_cache_page_write_begin(struct address_space *mapping,
477 			pgoff_t index, unsigned flags);
478 
479 /*
480  * Returns locked page at given index in given cache, creating it if needed.
481  */
grab_cache_page(struct address_space * mapping,pgoff_t index)482 static inline struct page *grab_cache_page(struct address_space *mapping,
483 								pgoff_t index)
484 {
485 	return find_or_create_page(mapping, index, mapping_gfp_mask(mapping));
486 }
487 
488 extern struct page * read_cache_page(struct address_space *mapping,
489 				pgoff_t index, filler_t *filler, void *data);
490 extern struct page * read_cache_page_gfp(struct address_space *mapping,
491 				pgoff_t index, gfp_t gfp_mask);
492 extern int read_cache_pages(struct address_space *mapping,
493 		struct list_head *pages, filler_t *filler, void *data);
494 
read_mapping_page(struct address_space * mapping,pgoff_t index,void * data)495 static inline struct page *read_mapping_page(struct address_space *mapping,
496 				pgoff_t index, void *data)
497 {
498 	return read_cache_page(mapping, index, NULL, data);
499 }
500 
501 /*
502  * Get index of the page within radix-tree (but not for hugetlb pages).
503  * (TODO: remove once hugetlb pages will have ->index in PAGE_SIZE)
504  */
page_to_index(struct page * page)505 static inline pgoff_t page_to_index(struct page *page)
506 {
507 	pgoff_t pgoff;
508 
509 	if (likely(!PageTransTail(page)))
510 		return page->index;
511 
512 	/*
513 	 *  We don't initialize ->index for tail pages: calculate based on
514 	 *  head page
515 	 */
516 	pgoff = compound_head(page)->index;
517 	pgoff += page - compound_head(page);
518 	return pgoff;
519 }
520 
521 extern pgoff_t hugetlb_basepage_index(struct page *page);
522 
523 /*
524  * Get the offset in PAGE_SIZE (even for hugetlb pages).
525  * (TODO: hugetlb pages should have ->index in PAGE_SIZE)
526  */
page_to_pgoff(struct page * page)527 static inline pgoff_t page_to_pgoff(struct page *page)
528 {
529 	if (unlikely(PageHuge(page)))
530 		return hugetlb_basepage_index(page);
531 	return page_to_index(page);
532 }
533 
534 /*
535  * Return byte-offset into filesystem object for page.
536  */
page_offset(struct page * page)537 static inline loff_t page_offset(struct page *page)
538 {
539 	return ((loff_t)page->index) << PAGE_SHIFT;
540 }
541 
page_file_offset(struct page * page)542 static inline loff_t page_file_offset(struct page *page)
543 {
544 	return ((loff_t)page_index(page)) << PAGE_SHIFT;
545 }
546 
547 extern pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
548 				     unsigned long address);
549 
linear_page_index(struct vm_area_struct * vma,unsigned long address)550 static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
551 					unsigned long address)
552 {
553 	pgoff_t pgoff;
554 	if (unlikely(is_vm_hugetlb_page(vma)))
555 		return linear_hugepage_index(vma, address);
556 	pgoff = (address - READ_ONCE(vma->vm_start)) >> PAGE_SHIFT;
557 	pgoff += READ_ONCE(vma->vm_pgoff);
558 	return pgoff;
559 }
560 
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 
wake_page_match(struct wait_page_queue * wait_page,struct wait_page_key * key)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,
590 				unsigned int flags);
591 extern void unlock_page(struct page *page);
592 
593 /*
594  * Return true if the page was successfully locked
595  */
trylock_page(struct page * page)596 static inline int trylock_page(struct page *page)
597 {
598 	page = compound_head(page);
599 	return (likely(!test_and_set_bit_lock(PG_locked, &page->flags)));
600 }
601 
602 /*
603  * lock_page may only be called if we have the page's inode pinned.
604  */
lock_page(struct page * page)605 static inline __sched void lock_page(struct page *page)
606 {
607 	might_sleep();
608 	if (!trylock_page(page))
609 		__lock_page(page);
610 }
611 
612 /*
613  * lock_page_killable is like lock_page but can be interrupted by fatal
614  * signals.  It returns 0 if it locked the page and -EINTR if it was
615  * killed while waiting.
616  */
lock_page_killable(struct page * page)617 static inline __sched int lock_page_killable(struct page *page)
618 {
619 	might_sleep();
620 	if (!trylock_page(page))
621 		return __lock_page_killable(page);
622 	return 0;
623 }
624 
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  */
lock_page_async(struct page * page,struct wait_page_queue * wait)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 /*
642  * lock_page_or_retry - Lock the page, unless this would block and the
643  * caller indicated that it can handle a retry.
644  *
645  * Return value and mmap_lock implications depend on flags; see
646  * __lock_page_or_retry().
647  */
lock_page_or_retry(struct page * page,struct mm_struct * mm,unsigned int flags)648 static inline __sched int lock_page_or_retry(struct page *page, struct mm_struct *mm,
649 				     unsigned int flags)
650 {
651 	might_sleep();
652 	return trylock_page(page) || __lock_page_or_retry(page, mm, flags);
653 }
654 
655 /*
656  * This is exported only for wait_on_page_locked/wait_on_page_writeback, etc.,
657  * and should not be used directly.
658  */
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);
661 
662 /*
663  * Wait for a page to be unlocked.
664  *
665  * This must be called with the caller "holding" the page,
666  * ie with increased "page->count" so that the page won't
667  * go away during the wait..
668  */
wait_on_page_locked(struct page * page)669 static inline __sched void wait_on_page_locked(struct page *page)
670 {
671 	if (PageLocked(page))
672 		wait_on_page_bit(compound_head(page), PG_locked);
673 }
674 
wait_on_page_locked_killable(struct page * page)675 static inline __sched int wait_on_page_locked_killable(struct page *page)
676 {
677 	if (!PageLocked(page))
678 		return 0;
679 	return wait_on_page_bit_killable(compound_head(page), PG_locked);
680 }
681 
682 extern void put_and_wait_on_page_locked(struct page *page);
683 
684 void wait_on_page_writeback(struct page *page);
685 extern void end_page_writeback(struct page *page);
686 void wait_for_stable_page(struct page *page);
687 
688 void page_endio(struct page *page, bool is_write, int err);
689 
690 /*
691  * Add an arbitrary waiter to a page's wait queue
692  */
693 extern void add_page_wait_queue(struct page *page, wait_queue_entry_t *waiter);
694 
695 /*
696  * Fault everything in given userspace address range in.
697  */
fault_in_pages_writeable(char __user * uaddr,int size)698 static inline int fault_in_pages_writeable(char __user *uaddr, int size)
699 {
700 	char __user *end = uaddr + size - 1;
701 
702 	if (unlikely(size == 0))
703 		return 0;
704 
705 	if (unlikely(uaddr > end))
706 		return -EFAULT;
707 	/*
708 	 * Writing zeroes into userspace here is OK, because we know that if
709 	 * the zero gets there, we'll be overwriting it.
710 	 */
711 	do {
712 		if (unlikely(__put_user(0, uaddr) != 0))
713 			return -EFAULT;
714 		uaddr += PAGE_SIZE;
715 	} while (uaddr <= end);
716 
717 	/* Check whether the range spilled into the next page. */
718 	if (((unsigned long)uaddr & PAGE_MASK) ==
719 			((unsigned long)end & PAGE_MASK))
720 		return __put_user(0, end);
721 
722 	return 0;
723 }
724 
fault_in_pages_readable(const char __user * uaddr,int size)725 static inline int fault_in_pages_readable(const char __user *uaddr, int size)
726 {
727 	volatile char c;
728 	const char __user *end = uaddr + size - 1;
729 
730 	if (unlikely(size == 0))
731 		return 0;
732 
733 	if (unlikely(uaddr > end))
734 		return -EFAULT;
735 
736 	do {
737 		if (unlikely(__get_user(c, uaddr) != 0))
738 			return -EFAULT;
739 		uaddr += PAGE_SIZE;
740 	} while (uaddr <= end);
741 
742 	/* Check whether the range spilled into the next page. */
743 	if (((unsigned long)uaddr & PAGE_MASK) ==
744 			((unsigned long)end & PAGE_MASK)) {
745 		return __get_user(c, end);
746 	}
747 
748 	(void)c;
749 	return 0;
750 }
751 
752 int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
753 				pgoff_t index, gfp_t gfp_mask);
754 int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
755 				pgoff_t index, gfp_t gfp_mask);
756 extern void delete_from_page_cache(struct page *page);
757 extern void __delete_from_page_cache(struct page *page, void *shadow);
758 int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask);
759 void delete_from_page_cache_batch(struct address_space *mapping,
760 				  struct pagevec *pvec);
761 
762 /*
763  * Like add_to_page_cache_locked, but used to add newly allocated pages:
764  * the page is new, so we can just run __SetPageLocked() against it.
765  */
add_to_page_cache(struct page * page,struct address_space * mapping,pgoff_t offset,gfp_t gfp_mask)766 static inline int add_to_page_cache(struct page *page,
767 		struct address_space *mapping, pgoff_t offset, gfp_t gfp_mask)
768 {
769 	int error;
770 
771 	__SetPageLocked(page);
772 	error = add_to_page_cache_locked(page, mapping, offset, gfp_mask);
773 	if (unlikely(error))
774 		__ClearPageLocked(page);
775 	return error;
776 }
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
page_cache_sync_readahead(struct address_space * mapping,struct file_ra_state * ra,struct file * file,pgoff_t index,unsigned long req_count)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
page_cache_async_readahead(struct address_space * mapping,struct file_ra_state * ra,struct file * file,struct page * page,pgoff_t index,unsigned long req_count)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  */
readahead_page(struct readahead_control * rac)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 
__readahead_batch(struct readahead_control * rac,struct page ** array,unsigned int array_sz)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  */
readahead_pos(struct readahead_control * rac)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  */
readahead_length(struct readahead_control * rac)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  */
readahead_index(struct readahead_control * rac)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  */
readahead_count(struct readahead_control * rac)976 static inline unsigned int readahead_count(struct readahead_control *rac)
977 {
978 	return rac->_nr_pages;
979 }
980 
dir_pages(struct inode * inode)981 static inline unsigned long dir_pages(struct inode *inode)
982 {
983 	return (unsigned long)(inode->i_size + PAGE_SIZE - 1) >>
984 			       PAGE_SHIFT;
985 }
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  */
page_mkwrite_check_truncate(struct page * page,struct inode * inode)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
i_blocks_per_page(struct inode * inode,struct page * page)1027 unsigned int i_blocks_per_page(struct inode *inode, struct page *page)
1028 {
1029 	return thp_size(page) >> inode->i_blkbits;
1030 }
1031 #endif /* _LINUX_PAGEMAP_H */
1032