pagemap.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. #ifndef _LINUX_PAGEMAP_H
  2. #define _LINUX_PAGEMAP_H
  3. /*
  4. * Copyright 1995 Linus Torvalds
  5. */
  6. #include <linux/mm.h>
  7. #include <linux/fs.h>
  8. #include <linux/list.h>
  9. #include <linux/highmem.h>
  10. #include <linux/compiler.h>
  11. #include <asm/uaccess.h>
  12. #include <linux/gfp.h>
  13. #include <linux/bitops.h>
  14. #include <linux/hardirq.h> /* for in_interrupt() */
  15. #include <linux/hugetlb_inline.h>
  16. /*
  17. * Bits in mapping->flags.
  18. */
  19. enum mapping_flags {
  20. AS_EIO = 0, /* IO error on async write */
  21. AS_ENOSPC = 1, /* ENOSPC on async write */
  22. AS_MM_ALL_LOCKS = 2, /* under mm_take_all_locks() */
  23. AS_UNEVICTABLE = 3, /* e.g., ramdisk, SHM_LOCK */
  24. AS_EXITING = 4, /* final truncate in progress */
  25. /* writeback related tags are not used */
  26. AS_NO_WRITEBACK_TAGS = 5,
  27. };
  28. static inline void mapping_set_error(struct address_space *mapping, int error)
  29. {
  30. if (unlikely(error)) {
  31. if (error == -ENOSPC)
  32. set_bit(AS_ENOSPC, &mapping->flags);
  33. else
  34. set_bit(AS_EIO, &mapping->flags);
  35. }
  36. }
  37. static inline void mapping_set_unevictable(struct address_space *mapping)
  38. {
  39. set_bit(AS_UNEVICTABLE, &mapping->flags);
  40. }
  41. static inline void mapping_clear_unevictable(struct address_space *mapping)
  42. {
  43. clear_bit(AS_UNEVICTABLE, &mapping->flags);
  44. }
  45. static inline int mapping_unevictable(struct address_space *mapping)
  46. {
  47. if (mapping)
  48. return test_bit(AS_UNEVICTABLE, &mapping->flags);
  49. return !!mapping;
  50. }
  51. static inline void mapping_set_exiting(struct address_space *mapping)
  52. {
  53. set_bit(AS_EXITING, &mapping->flags);
  54. }
  55. static inline int mapping_exiting(struct address_space *mapping)
  56. {
  57. return test_bit(AS_EXITING, &mapping->flags);
  58. }
  59. static inline void mapping_set_no_writeback_tags(struct address_space *mapping)
  60. {
  61. set_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
  62. }
  63. static inline int mapping_use_writeback_tags(struct address_space *mapping)
  64. {
  65. return !test_bit(AS_NO_WRITEBACK_TAGS, &mapping->flags);
  66. }
  67. static inline gfp_t mapping_gfp_mask(struct address_space * mapping)
  68. {
  69. return mapping->gfp_mask;
  70. }
  71. /* Restricts the given gfp_mask to what the mapping allows. */
  72. static inline gfp_t mapping_gfp_constraint(struct address_space *mapping,
  73. gfp_t gfp_mask)
  74. {
  75. return mapping_gfp_mask(mapping) & gfp_mask;
  76. }
  77. /*
  78. * This is non-atomic. Only to be used before the mapping is activated.
  79. * Probably needs a barrier...
  80. */
  81. static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
  82. {
  83. m->gfp_mask = mask;
  84. }
  85. void release_pages(struct page **pages, int nr, bool cold);
  86. /*
  87. * speculatively take a reference to a page.
  88. * If the page is free (_refcount == 0), then _refcount is untouched, and 0
  89. * is returned. Otherwise, _refcount is incremented by 1 and 1 is returned.
  90. *
  91. * This function must be called inside the same rcu_read_lock() section as has
  92. * been used to lookup the page in the pagecache radix-tree (or page table):
  93. * this allows allocators to use a synchronize_rcu() to stabilize _refcount.
  94. *
  95. * Unless an RCU grace period has passed, the count of all pages coming out
  96. * of the allocator must be considered unstable. page_count may return higher
  97. * than expected, and put_page must be able to do the right thing when the
  98. * page has been finished with, no matter what it is subsequently allocated
  99. * for (because put_page is what is used here to drop an invalid speculative
  100. * reference).
  101. *
  102. * This is the interesting part of the lockless pagecache (and lockless
  103. * get_user_pages) locking protocol, where the lookup-side (eg. find_get_page)
  104. * has the following pattern:
  105. * 1. find page in radix tree
  106. * 2. conditionally increment refcount
  107. * 3. check the page is still in pagecache (if no, goto 1)
  108. *
  109. * Remove-side that cares about stability of _refcount (eg. reclaim) has the
  110. * following (with tree_lock held for write):
  111. * A. atomically check refcount is correct and set it to 0 (atomic_cmpxchg)
  112. * B. remove page from pagecache
  113. * C. free the page
  114. *
  115. * There are 2 critical interleavings that matter:
  116. * - 2 runs before A: in this case, A sees elevated refcount and bails out
  117. * - A runs before 2: in this case, 2 sees zero refcount and retries;
  118. * subsequently, B will complete and 1 will find no page, causing the
  119. * lookup to return NULL.
  120. *
  121. * It is possible that between 1 and 2, the page is removed then the exact same
  122. * page is inserted into the same position in pagecache. That's OK: the
  123. * old find_get_page using tree_lock could equally have run before or after
  124. * such a re-insertion, depending on order that locks are granted.
  125. *
  126. * Lookups racing against pagecache insertion isn't a big problem: either 1
  127. * will find the page or it will not. Likewise, the old find_get_page could run
  128. * either before the insertion or afterwards, depending on timing.
  129. */
  130. static inline int page_cache_get_speculative(struct page *page)
  131. {
  132. VM_BUG_ON(in_interrupt());
  133. #ifdef CONFIG_TINY_RCU
  134. # ifdef CONFIG_PREEMPT_COUNT
  135. VM_BUG_ON(!in_atomic() && !irqs_disabled());
  136. # endif
  137. /*
  138. * Preempt must be disabled here - we rely on rcu_read_lock doing
  139. * this for us.
  140. *
  141. * Pagecache won't be truncated from interrupt context, so if we have
  142. * found a page in the radix tree here, we have pinned its refcount by
  143. * disabling preempt, and hence no need for the "speculative get" that
  144. * SMP requires.
  145. */
  146. VM_BUG_ON_PAGE(page_count(page) == 0, page);
  147. page_ref_inc(page);
  148. #else
  149. if (unlikely(!get_page_unless_zero(page))) {
  150. /*
  151. * Either the page has been freed, or will be freed.
  152. * In either case, retry here and the caller should
  153. * do the right thing (see comments above).
  154. */
  155. return 0;
  156. }
  157. #endif
  158. VM_BUG_ON_PAGE(PageTail(page), page);
  159. return 1;
  160. }
  161. /*
  162. * Same as above, but add instead of inc (could just be merged)
  163. */
  164. static inline int page_cache_add_speculative(struct page *page, int count)
  165. {
  166. VM_BUG_ON(in_interrupt());
  167. #if !defined(CONFIG_SMP) && defined(CONFIG_TREE_RCU)
  168. # ifdef CONFIG_PREEMPT_COUNT
  169. VM_BUG_ON(!in_atomic() && !irqs_disabled());
  170. # endif
  171. VM_BUG_ON_PAGE(page_count(page) == 0, page);
  172. page_ref_add(page, count);
  173. #else
  174. if (unlikely(!page_ref_add_unless(page, count, 0)))
  175. return 0;
  176. #endif
  177. VM_BUG_ON_PAGE(PageCompound(page) && page != compound_head(page), page);
  178. return 1;
  179. }
  180. #ifdef CONFIG_NUMA
  181. extern struct page *__page_cache_alloc(gfp_t gfp);
  182. #else
  183. static inline struct page *__page_cache_alloc(gfp_t gfp)
  184. {
  185. return alloc_pages(gfp, 0);
  186. }
  187. #endif
  188. static inline struct page *page_cache_alloc(struct address_space *x)
  189. {
  190. return __page_cache_alloc(mapping_gfp_mask(x));
  191. }
  192. static inline struct page *page_cache_alloc_cold(struct address_space *x)
  193. {
  194. return __page_cache_alloc(mapping_gfp_mask(x)|__GFP_COLD);
  195. }
  196. static inline gfp_t readahead_gfp_mask(struct address_space *x)
  197. {
  198. return mapping_gfp_mask(x) |
  199. __GFP_COLD | __GFP_NORETRY | __GFP_NOWARN;
  200. }
  201. typedef int filler_t(void *, struct page *);
  202. pgoff_t page_cache_next_hole(struct address_space *mapping,
  203. pgoff_t index, unsigned long max_scan);
  204. pgoff_t page_cache_prev_hole(struct address_space *mapping,
  205. pgoff_t index, unsigned long max_scan);
  206. #define FGP_ACCESSED 0x00000001
  207. #define FGP_LOCK 0x00000002
  208. #define FGP_CREAT 0x00000004
  209. #define FGP_WRITE 0x00000008
  210. #define FGP_NOFS 0x00000010
  211. #define FGP_NOWAIT 0x00000020
  212. struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset,
  213. int fgp_flags, gfp_t cache_gfp_mask);
  214. /**
  215. * find_get_page - find and get a page reference
  216. * @mapping: the address_space to search
  217. * @offset: the page index
  218. *
  219. * Looks up the page cache slot at @mapping & @offset. If there is a
  220. * page cache page, it is returned with an increased refcount.
  221. *
  222. * Otherwise, %NULL is returned.
  223. */
  224. static inline struct page *find_get_page(struct address_space *mapping,
  225. pgoff_t offset)
  226. {
  227. return pagecache_get_page(mapping, offset, 0, 0);
  228. }
  229. static inline struct page *find_get_page_flags(struct address_space *mapping,
  230. pgoff_t offset, int fgp_flags)
  231. {
  232. return pagecache_get_page(mapping, offset, fgp_flags, 0);
  233. }
  234. /**
  235. * find_lock_page - locate, pin and lock a pagecache page
  236. * pagecache_get_page - find and get a page reference
  237. * @mapping: the address_space to search
  238. * @offset: the page index
  239. *
  240. * Looks up the page cache slot at @mapping & @offset. If there is a
  241. * page cache page, it is returned locked and with an increased
  242. * refcount.
  243. *
  244. * Otherwise, %NULL is returned.
  245. *
  246. * find_lock_page() may sleep.
  247. */
  248. static inline struct page *find_lock_page(struct address_space *mapping,
  249. pgoff_t offset)
  250. {
  251. return pagecache_get_page(mapping, offset, FGP_LOCK, 0);
  252. }
  253. /**
  254. * find_or_create_page - locate or add a pagecache page
  255. * @mapping: the page's address_space
  256. * @index: the page's index into the mapping
  257. * @gfp_mask: page allocation mode
  258. *
  259. * Looks up the page cache slot at @mapping & @offset. If there is a
  260. * page cache page, it is returned locked and with an increased
  261. * refcount.
  262. *
  263. * If the page is not present, a new page is allocated using @gfp_mask
  264. * and added to the page cache and the VM's LRU list. The page is
  265. * returned locked and with an increased refcount.
  266. *
  267. * On memory exhaustion, %NULL is returned.
  268. *
  269. * find_or_create_page() may sleep, even if @gfp_flags specifies an
  270. * atomic allocation!
  271. */
  272. static inline struct page *find_or_create_page(struct address_space *mapping,
  273. pgoff_t offset, gfp_t gfp_mask)
  274. {
  275. return pagecache_get_page(mapping, offset,
  276. FGP_LOCK|FGP_ACCESSED|FGP_CREAT,
  277. gfp_mask);
  278. }
  279. /**
  280. * grab_cache_page_nowait - returns locked page at given index in given cache
  281. * @mapping: target address_space
  282. * @index: the page index
  283. *
  284. * Same as grab_cache_page(), but do not wait if the page is unavailable.
  285. * This is intended for speculative data generators, where the data can
  286. * be regenerated if the page couldn't be grabbed. This routine should
  287. * be safe to call while holding the lock for another page.
  288. *
  289. * Clear __GFP_FS when allocating the page to avoid recursion into the fs
  290. * and deadlock against the caller's locked page.
  291. */
  292. static inline struct page *grab_cache_page_nowait(struct address_space *mapping,
  293. pgoff_t index)
  294. {
  295. return pagecache_get_page(mapping, index,
  296. FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
  297. mapping_gfp_mask(mapping));
  298. }
  299. struct page *find_get_entry(struct address_space *mapping, pgoff_t offset);
  300. struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset);
  301. unsigned find_get_entries(struct address_space *mapping, pgoff_t start,
  302. unsigned int nr_entries, struct page **entries,
  303. pgoff_t *indices);
  304. unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
  305. unsigned int nr_pages, struct page **pages);
  306. unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
  307. unsigned int nr_pages, struct page **pages);
  308. unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
  309. int tag, unsigned int nr_pages, struct page **pages);
  310. unsigned find_get_entries_tag(struct address_space *mapping, pgoff_t start,
  311. int tag, unsigned int nr_entries,
  312. struct page **entries, pgoff_t *indices);
  313. struct page *grab_cache_page_write_begin(struct address_space *mapping,
  314. pgoff_t index, unsigned flags);
  315. /*
  316. * Returns locked page at given index in given cache, creating it if needed.
  317. */
  318. static inline struct page *grab_cache_page(struct address_space *mapping,
  319. pgoff_t index)
  320. {
  321. return find_or_create_page(mapping, index, mapping_gfp_mask(mapping));
  322. }
  323. extern struct page * read_cache_page(struct address_space *mapping,
  324. pgoff_t index, filler_t *filler, void *data);
  325. extern struct page * read_cache_page_gfp(struct address_space *mapping,
  326. pgoff_t index, gfp_t gfp_mask);
  327. extern int read_cache_pages(struct address_space *mapping,
  328. struct list_head *pages, filler_t *filler, void *data);
  329. static inline struct page *read_mapping_page(struct address_space *mapping,
  330. pgoff_t index, void *data)
  331. {
  332. filler_t *filler = (filler_t *)mapping->a_ops->readpage;
  333. return read_cache_page(mapping, index, filler, data);
  334. }
  335. /*
  336. * Get index of the page with in radix-tree
  337. * (TODO: remove once hugetlb pages will have ->index in PAGE_SIZE)
  338. */
  339. static inline pgoff_t page_to_index(struct page *page)
  340. {
  341. pgoff_t pgoff;
  342. if (likely(!PageTransTail(page)))
  343. return page->index;
  344. /*
  345. * We don't initialize ->index for tail pages: calculate based on
  346. * head page
  347. */
  348. pgoff = compound_head(page)->index;
  349. pgoff += page - compound_head(page);
  350. return pgoff;
  351. }
  352. /*
  353. * Get the offset in PAGE_SIZE.
  354. * (TODO: hugepage should have ->index in PAGE_SIZE)
  355. */
  356. static inline pgoff_t page_to_pgoff(struct page *page)
  357. {
  358. if (unlikely(PageHeadHuge(page)))
  359. return page->index << compound_order(page);
  360. return page_to_index(page);
  361. }
  362. /*
  363. * Return byte-offset into filesystem object for page.
  364. */
  365. static inline loff_t page_offset(struct page *page)
  366. {
  367. return ((loff_t)page->index) << PAGE_SHIFT;
  368. }
  369. static inline loff_t page_file_offset(struct page *page)
  370. {
  371. return ((loff_t)page_index(page)) << PAGE_SHIFT;
  372. }
  373. extern pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
  374. unsigned long address);
  375. static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
  376. unsigned long address)
  377. {
  378. pgoff_t pgoff;
  379. if (unlikely(is_vm_hugetlb_page(vma)))
  380. return linear_hugepage_index(vma, address);
  381. pgoff = (address - vma->vm_start) >> PAGE_SHIFT;
  382. pgoff += vma->vm_pgoff;
  383. return pgoff;
  384. }
  385. extern void __lock_page(struct page *page);
  386. extern int __lock_page_killable(struct page *page);
  387. extern int __lock_page_or_retry(struct page *page, struct mm_struct *mm,
  388. unsigned int flags);
  389. extern void unlock_page(struct page *page);
  390. static inline int trylock_page(struct page *page)
  391. {
  392. page = compound_head(page);
  393. return (likely(!test_and_set_bit_lock(PG_locked, &page->flags)));
  394. }
  395. /*
  396. * lock_page may only be called if we have the page's inode pinned.
  397. */
  398. static inline void lock_page(struct page *page)
  399. {
  400. might_sleep();
  401. if (!trylock_page(page))
  402. __lock_page(page);
  403. }
  404. /*
  405. * lock_page_killable is like lock_page but can be interrupted by fatal
  406. * signals. It returns 0 if it locked the page and -EINTR if it was
  407. * killed while waiting.
  408. */
  409. static inline int lock_page_killable(struct page *page)
  410. {
  411. might_sleep();
  412. if (!trylock_page(page))
  413. return __lock_page_killable(page);
  414. return 0;
  415. }
  416. /*
  417. * lock_page_or_retry - Lock the page, unless this would block and the
  418. * caller indicated that it can handle a retry.
  419. *
  420. * Return value and mmap_sem implications depend on flags; see
  421. * __lock_page_or_retry().
  422. */
  423. static inline int lock_page_or_retry(struct page *page, struct mm_struct *mm,
  424. unsigned int flags)
  425. {
  426. might_sleep();
  427. return trylock_page(page) || __lock_page_or_retry(page, mm, flags);
  428. }
  429. /*
  430. * This is exported only for wait_on_page_locked/wait_on_page_writeback,
  431. * and for filesystems which need to wait on PG_private.
  432. */
  433. extern void wait_on_page_bit(struct page *page, int bit_nr);
  434. extern int wait_on_page_bit_killable(struct page *page, int bit_nr);
  435. extern int wait_on_page_bit_killable_timeout(struct page *page,
  436. int bit_nr, unsigned long timeout);
  437. static inline int wait_on_page_locked_killable(struct page *page)
  438. {
  439. if (!PageLocked(page))
  440. return 0;
  441. return wait_on_page_bit_killable(compound_head(page), PG_locked);
  442. }
  443. extern wait_queue_head_t *page_waitqueue(struct page *page);
  444. static inline void wake_up_page(struct page *page, int bit)
  445. {
  446. __wake_up_bit(page_waitqueue(page), &page->flags, bit);
  447. }
  448. /*
  449. * Wait for a page to be unlocked.
  450. *
  451. * This must be called with the caller "holding" the page,
  452. * ie with increased "page->count" so that the page won't
  453. * go away during the wait..
  454. */
  455. static inline void wait_on_page_locked(struct page *page)
  456. {
  457. if (PageLocked(page))
  458. wait_on_page_bit(compound_head(page), PG_locked);
  459. }
  460. /*
  461. * Wait for a page to complete writeback
  462. */
  463. static inline void wait_on_page_writeback(struct page *page)
  464. {
  465. if (PageWriteback(page))
  466. wait_on_page_bit(page, PG_writeback);
  467. }
  468. extern void end_page_writeback(struct page *page);
  469. void wait_for_stable_page(struct page *page);
  470. void page_endio(struct page *page, bool is_write, int err);
  471. /*
  472. * Add an arbitrary waiter to a page's wait queue
  473. */
  474. extern void add_page_wait_queue(struct page *page, wait_queue_t *waiter);
  475. /*
  476. * Fault everything in given userspace address range in.
  477. */
  478. static inline int fault_in_pages_writeable(char __user *uaddr, int size)
  479. {
  480. char __user *end = uaddr + size - 1;
  481. if (unlikely(size == 0))
  482. return 0;
  483. if (unlikely(uaddr > end))
  484. return -EFAULT;
  485. /*
  486. * Writing zeroes into userspace here is OK, because we know that if
  487. * the zero gets there, we'll be overwriting it.
  488. */
  489. do {
  490. if (unlikely(__put_user(0, uaddr) != 0))
  491. return -EFAULT;
  492. uaddr += PAGE_SIZE;
  493. } while (uaddr <= end);
  494. /* Check whether the range spilled into the next page. */
  495. if (((unsigned long)uaddr & PAGE_MASK) ==
  496. ((unsigned long)end & PAGE_MASK))
  497. return __put_user(0, end);
  498. return 0;
  499. }
  500. static inline int fault_in_pages_readable(const char __user *uaddr, int size)
  501. {
  502. volatile char c;
  503. const char __user *end = uaddr + size - 1;
  504. if (unlikely(size == 0))
  505. return 0;
  506. if (unlikely(uaddr > end))
  507. return -EFAULT;
  508. do {
  509. if (unlikely(__get_user(c, uaddr) != 0))
  510. return -EFAULT;
  511. uaddr += PAGE_SIZE;
  512. } while (uaddr <= end);
  513. /* Check whether the range spilled into the next page. */
  514. if (((unsigned long)uaddr & PAGE_MASK) ==
  515. ((unsigned long)end & PAGE_MASK)) {
  516. return __get_user(c, end);
  517. }
  518. (void)c;
  519. return 0;
  520. }
  521. int add_to_page_cache_locked(struct page *page, struct address_space *mapping,
  522. pgoff_t index, gfp_t gfp_mask);
  523. int add_to_page_cache_lru(struct page *page, struct address_space *mapping,
  524. pgoff_t index, gfp_t gfp_mask);
  525. extern void delete_from_page_cache(struct page *page);
  526. extern void __delete_from_page_cache(struct page *page, void *shadow);
  527. int replace_page_cache_page(struct page *old, struct page *new, gfp_t gfp_mask);
  528. /*
  529. * Like add_to_page_cache_locked, but used to add newly allocated pages:
  530. * the page is new, so we can just run __SetPageLocked() against it.
  531. */
  532. static inline int add_to_page_cache(struct page *page,
  533. struct address_space *mapping, pgoff_t offset, gfp_t gfp_mask)
  534. {
  535. int error;
  536. __SetPageLocked(page);
  537. error = add_to_page_cache_locked(page, mapping, offset, gfp_mask);
  538. if (unlikely(error))
  539. __ClearPageLocked(page);
  540. return error;
  541. }
  542. static inline unsigned long dir_pages(struct inode *inode)
  543. {
  544. return (unsigned long)(inode->i_size + PAGE_SIZE - 1) >>
  545. PAGE_SHIFT;
  546. }
  547. #endif /* _LINUX_PAGEMAP_H */