fadvise.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/fadvise.h>
  17. #include <linux/writeback.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/swap.h>
  20. #include <asm/unistd.h>
  21. #include "internal.h"
  22. /*
  23. * POSIX_FADV_WILLNEED could set PG_Referenced, and POSIX_FADV_NOREUSE could
  24. * deactivate the pages and clear PG_Referenced.
  25. */
  26. int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
  27. {
  28. struct inode *inode;
  29. struct address_space *mapping;
  30. struct backing_dev_info *bdi;
  31. loff_t endbyte; /* inclusive */
  32. pgoff_t start_index;
  33. pgoff_t end_index;
  34. unsigned long nrpages;
  35. inode = file_inode(file);
  36. if (S_ISFIFO(inode->i_mode))
  37. return -ESPIPE;
  38. mapping = file->f_mapping;
  39. if (!mapping || len < 0)
  40. return -EINVAL;
  41. bdi = inode_to_bdi(mapping->host);
  42. if (IS_DAX(inode) || (bdi == &noop_backing_dev_info)) {
  43. switch (advice) {
  44. case POSIX_FADV_NORMAL:
  45. case POSIX_FADV_RANDOM:
  46. case POSIX_FADV_SEQUENTIAL:
  47. case POSIX_FADV_WILLNEED:
  48. case POSIX_FADV_NOREUSE:
  49. case POSIX_FADV_DONTNEED:
  50. /* no bad return value, but ignore advice */
  51. break;
  52. default:
  53. return -EINVAL;
  54. }
  55. return 0;
  56. }
  57. /*
  58. * Careful about overflows. Len == 0 means "as much as possible". Use
  59. * unsigned math because signed overflows are undefined and UBSan
  60. * complains.
  61. */
  62. endbyte = (u64)offset + (u64)len;
  63. if (!len || endbyte < len)
  64. endbyte = LLONG_MAX;
  65. else
  66. endbyte--; /* inclusive */
  67. switch (advice) {
  68. case POSIX_FADV_NORMAL:
  69. file->f_ra.ra_pages = bdi->ra_pages;
  70. spin_lock(&file->f_lock);
  71. file->f_mode &= ~(FMODE_RANDOM | FMODE_NOREUSE);
  72. spin_unlock(&file->f_lock);
  73. break;
  74. case POSIX_FADV_RANDOM:
  75. spin_lock(&file->f_lock);
  76. file->f_mode |= FMODE_RANDOM;
  77. spin_unlock(&file->f_lock);
  78. break;
  79. case POSIX_FADV_SEQUENTIAL:
  80. file->f_ra.ra_pages = bdi->ra_pages * 2;
  81. spin_lock(&file->f_lock);
  82. file->f_mode &= ~FMODE_RANDOM;
  83. spin_unlock(&file->f_lock);
  84. break;
  85. case POSIX_FADV_WILLNEED:
  86. /* First and last PARTIAL page! */
  87. start_index = offset >> PAGE_SHIFT;
  88. end_index = endbyte >> PAGE_SHIFT;
  89. /* Careful about overflow on the "+1" */
  90. nrpages = end_index - start_index + 1;
  91. if (!nrpages)
  92. nrpages = ~0UL;
  93. force_page_cache_readahead(mapping, file, start_index, nrpages);
  94. break;
  95. case POSIX_FADV_NOREUSE:
  96. spin_lock(&file->f_lock);
  97. file->f_mode |= FMODE_NOREUSE;
  98. spin_unlock(&file->f_lock);
  99. break;
  100. case POSIX_FADV_DONTNEED:
  101. filemap_flush_range(mapping, offset, endbyte);
  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_failed = 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. mapping_try_invalidate(mapping, start_index, end_index,
  140. &nr_failed);
  141. /*
  142. * The failures may be due to the folio being
  143. * in the LRU cache of a remote CPU. Drain all
  144. * caches and try again.
  145. */
  146. if (nr_failed) {
  147. lru_add_drain_all();
  148. invalidate_mapping_pages(mapping, start_index,
  149. end_index);
  150. }
  151. }
  152. break;
  153. default:
  154. return -EINVAL;
  155. }
  156. return 0;
  157. }
  158. EXPORT_SYMBOL(generic_fadvise);
  159. int vfs_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
  160. {
  161. if (file->f_op->fadvise)
  162. return file->f_op->fadvise(file, offset, len, advice);
  163. return generic_fadvise(file, offset, len, advice);
  164. }
  165. EXPORT_SYMBOL(vfs_fadvise);
  166. #ifdef CONFIG_ADVISE_SYSCALLS
  167. int ksys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
  168. {
  169. CLASS(fd, f)(fd);
  170. if (fd_empty(f))
  171. return -EBADF;
  172. return vfs_fadvise(fd_file(f), offset, len, advice);
  173. }
  174. SYSCALL_DEFINE4(fadvise64_64, int, fd, loff_t, offset, loff_t, len, int, advice)
  175. {
  176. return ksys_fadvise64_64(fd, offset, len, advice);
  177. }
  178. #ifdef __ARCH_WANT_SYS_FADVISE64
  179. SYSCALL_DEFINE4(fadvise64, int, fd, loff_t, offset, size_t, len, int, advice)
  180. {
  181. return ksys_fadvise64_64(fd, offset, len, advice);
  182. }
  183. #endif
  184. #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_FADVISE64_64)
  185. COMPAT_SYSCALL_DEFINE6(fadvise64_64, int, fd, compat_arg_u64_dual(offset),
  186. compat_arg_u64_dual(len), int, advice)
  187. {
  188. return ksys_fadvise64_64(fd, compat_arg_u64_glue(offset),
  189. compat_arg_u64_glue(len), advice);
  190. }
  191. #endif
  192. #endif