fadvise.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mm/fadvise.c
  4. *
  5. * Copyright (C) 2002, Linus Torvalds
  6. *
  7. * 11Jan2003 Andrew Morton
  8. * Initial version.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/file.h>
  12. #include <linux/fs.h>
  13. #include <linux/mm.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/backing-dev.h>
  16. #include <linux/pagevec.h>
  17. #include <linux/fadvise.h>
  18. #include <linux/writeback.h>
  19. #include <linux/syscalls.h>
  20. #include <linux/swap.h>
  21. #include <asm/unistd.h>
  22. #include "internal.h"
  23. /*
  24. * POSIX_FADV_WILLNEED could set PG_Referenced, and POSIX_FADV_NOREUSE could
  25. * deactivate the pages and clear PG_Referenced.
  26. */
  27. int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
  28. {
  29. struct inode *inode;
  30. struct address_space *mapping;
  31. struct backing_dev_info *bdi;
  32. loff_t endbyte; /* inclusive */
  33. pgoff_t start_index;
  34. pgoff_t end_index;
  35. unsigned long nrpages;
  36. inode = file_inode(file);
  37. if (S_ISFIFO(inode->i_mode))
  38. return -ESPIPE;
  39. mapping = file->f_mapping;
  40. if (!mapping || len < 0)
  41. return -EINVAL;
  42. bdi = inode_to_bdi(mapping->host);
  43. if (IS_DAX(inode) || (bdi == &noop_backing_dev_info)) {
  44. switch (advice) {
  45. case POSIX_FADV_NORMAL:
  46. case POSIX_FADV_RANDOM:
  47. case POSIX_FADV_SEQUENTIAL:
  48. case POSIX_FADV_WILLNEED:
  49. case POSIX_FADV_NOREUSE:
  50. case POSIX_FADV_DONTNEED:
  51. /* no bad return value, but ignore advice */
  52. break;
  53. default:
  54. return -EINVAL;
  55. }
  56. return 0;
  57. }
  58. /*
  59. * Careful about overflows. Len == 0 means "as much as possible". Use
  60. * unsigned math because signed overflows are undefined and UBSan
  61. * complains.
  62. */
  63. endbyte = (u64)offset + (u64)len;
  64. if (!len || endbyte < len)
  65. endbyte = -1;
  66. else
  67. endbyte--; /* inclusive */
  68. switch (advice) {
  69. case POSIX_FADV_NORMAL:
  70. file->f_ra.ra_pages = bdi->ra_pages;
  71. spin_lock(&file->f_lock);
  72. file->f_mode &= ~FMODE_RANDOM;
  73. spin_unlock(&file->f_lock);
  74. break;
  75. case POSIX_FADV_RANDOM:
  76. spin_lock(&file->f_lock);
  77. file->f_mode |= FMODE_RANDOM;
  78. spin_unlock(&file->f_lock);
  79. break;
  80. case POSIX_FADV_SEQUENTIAL:
  81. file->f_ra.ra_pages = bdi->ra_pages * 2;
  82. spin_lock(&file->f_lock);
  83. file->f_mode &= ~FMODE_RANDOM;
  84. spin_unlock(&file->f_lock);
  85. break;
  86. case POSIX_FADV_WILLNEED:
  87. /* First and last PARTIAL page! */
  88. start_index = offset >> PAGE_SHIFT;
  89. end_index = endbyte >> PAGE_SHIFT;
  90. /* Careful about overflow on the "+1" */
  91. nrpages = end_index - start_index + 1;
  92. if (!nrpages)
  93. nrpages = ~0UL;
  94. force_page_cache_readahead(mapping, file, start_index, nrpages);
  95. break;
  96. case POSIX_FADV_NOREUSE:
  97. break;
  98. case POSIX_FADV_DONTNEED:
  99. if (!inode_write_congested(mapping->host))
  100. __filemap_fdatawrite_range(mapping, offset, endbyte,
  101. WB_SYNC_NONE);
  102. /*
  103. * First and last FULL page! Partial pages are deliberately
  104. * preserved on the expectation that it is better to preserve
  105. * needed memory than to discard unneeded memory.
  106. */
  107. start_index = (offset+(PAGE_SIZE-1)) >> PAGE_SHIFT;
  108. end_index = (endbyte >> PAGE_SHIFT);
  109. /*
  110. * The page at end_index will be inclusively discarded according
  111. * by invalidate_mapping_pages(), so subtracting 1 from
  112. * end_index means we will skip the last page. But if endbyte
  113. * is page aligned or is at the end of file, we should not skip
  114. * that page - discarding the last page is safe enough.
  115. */
  116. if ((endbyte & ~PAGE_MASK) != ~PAGE_MASK &&
  117. endbyte != inode->i_size - 1) {
  118. /* First page is tricky as 0 - 1 = -1, but pgoff_t
  119. * is unsigned, so the end_index >= start_index
  120. * check below would be true and we'll discard the whole
  121. * file cache which is not what was asked.
  122. */
  123. if (end_index == 0)
  124. break;
  125. end_index--;
  126. }
  127. if (end_index >= start_index) {
  128. unsigned long nr_pagevec = 0;
  129. /*
  130. * It's common to FADV_DONTNEED right after
  131. * the read or write that instantiates the
  132. * pages, in which case there will be some
  133. * sitting on the local LRU cache. Try to
  134. * avoid the expensive remote drain and the
  135. * second cache tree walk below by flushing
  136. * them out right away.
  137. */
  138. lru_add_drain();
  139. invalidate_mapping_pagevec(mapping,
  140. start_index, end_index,
  141. &nr_pagevec);
  142. /*
  143. * If fewer pages were invalidated than expected then
  144. * it is possible that some of the pages were on
  145. * a per-cpu pagevec for a remote CPU. Drain all
  146. * pagevecs and try again.
  147. */
  148. if (nr_pagevec) {
  149. lru_add_drain_all();
  150. invalidate_mapping_pages(mapping, start_index,
  151. end_index);
  152. }
  153. }
  154. break;
  155. default:
  156. return -EINVAL;
  157. }
  158. return 0;
  159. }
  160. EXPORT_SYMBOL(generic_fadvise);
  161. int vfs_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
  162. {
  163. if (file->f_op->fadvise)
  164. return file->f_op->fadvise(file, offset, len, advice);
  165. return generic_fadvise(file, offset, len, advice);
  166. }
  167. EXPORT_SYMBOL(vfs_fadvise);
  168. #ifdef CONFIG_ADVISE_SYSCALLS
  169. int ksys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
  170. {
  171. struct fd f = fdget(fd);
  172. int ret;
  173. if (!f.file)
  174. return -EBADF;
  175. ret = vfs_fadvise(f.file, offset, len, advice);
  176. fdput(f);
  177. return ret;
  178. }
  179. SYSCALL_DEFINE4(fadvise64_64, int, fd, loff_t, offset, loff_t, len, int, advice)
  180. {
  181. return ksys_fadvise64_64(fd, offset, len, advice);
  182. }
  183. #ifdef __ARCH_WANT_SYS_FADVISE64
  184. SYSCALL_DEFINE4(fadvise64, int, fd, loff_t, offset, size_t, len, int, advice)
  185. {
  186. return ksys_fadvise64_64(fd, offset, len, advice);
  187. }
  188. #endif
  189. #endif