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