fadvise.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_fdatawrite_range(mapping, offset, endbyte,
  102. WB_SYNC_NONE);
  103. /*
  104. * First and last FULL page! Partial pages are deliberately
  105. * preserved on the expectation that it is better to preserve
  106. * needed memory than to discard unneeded memory.
  107. */
  108. start_index = (offset+(PAGE_SIZE-1)) >> PAGE_SHIFT;
  109. end_index = (endbyte >> PAGE_SHIFT);
  110. /*
  111. * The page at end_index will be inclusively discarded according
  112. * by invalidate_mapping_pages(), so subtracting 1 from
  113. * end_index means we will skip the last page. But if endbyte
  114. * is page aligned or is at the end of file, we should not skip
  115. * that page - discarding the last page is safe enough.
  116. */
  117. if ((endbyte & ~PAGE_MASK) != ~PAGE_MASK &&
  118. endbyte != inode->i_size - 1) {
  119. /* First page is tricky as 0 - 1 = -1, but pgoff_t
  120. * is unsigned, so the end_index >= start_index
  121. * check below would be true and we'll discard the whole
  122. * file cache which is not what was asked.
  123. */
  124. if (end_index == 0)
  125. break;
  126. end_index--;
  127. }
  128. if (end_index >= start_index) {
  129. unsigned long nr_failed = 0;
  130. /*
  131. * It's common to FADV_DONTNEED right after
  132. * the read or write that instantiates the
  133. * pages, in which case there will be some
  134. * sitting on the local LRU cache. Try to
  135. * avoid the expensive remote drain and the
  136. * second cache tree walk below by flushing
  137. * them out right away.
  138. */
  139. lru_add_drain();
  140. mapping_try_invalidate(mapping, start_index, end_index,
  141. &nr_failed);
  142. /*
  143. * The failures may be due to the folio being
  144. * in the LRU cache of a remote CPU. Drain all
  145. * caches and try again.
  146. */
  147. if (nr_failed) {
  148. lru_add_drain_all();
  149. invalidate_mapping_pages(mapping, start_index,
  150. end_index);
  151. }
  152. }
  153. break;
  154. default:
  155. return -EINVAL;
  156. }
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(generic_fadvise);
  160. int vfs_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
  161. {
  162. if (file->f_op->fadvise)
  163. return file->f_op->fadvise(file, offset, len, advice);
  164. return generic_fadvise(file, offset, len, advice);
  165. }
  166. EXPORT_SYMBOL(vfs_fadvise);
  167. #ifdef CONFIG_ADVISE_SYSCALLS
  168. int ksys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
  169. {
  170. struct fd f = fdget(fd);
  171. int ret;
  172. if (!fd_file(f))
  173. return -EBADF;
  174. ret = vfs_fadvise(fd_file(f), offset, len, advice);
  175. fdput(f);
  176. return ret;
  177. }
  178. SYSCALL_DEFINE4(fadvise64_64, int, fd, loff_t, offset, loff_t, len, int, advice)
  179. {
  180. return ksys_fadvise64_64(fd, offset, len, advice);
  181. }
  182. #ifdef __ARCH_WANT_SYS_FADVISE64
  183. SYSCALL_DEFINE4(fadvise64, int, fd, loff_t, offset, size_t, len, int, advice)
  184. {
  185. return ksys_fadvise64_64(fd, offset, len, advice);
  186. }
  187. #endif
  188. #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_FADVISE64_64)
  189. COMPAT_SYSCALL_DEFINE6(fadvise64_64, int, fd, compat_arg_u64_dual(offset),
  190. compat_arg_u64_dual(len), int, advice)
  191. {
  192. return ksys_fadvise64_64(fd, compat_arg_u64_glue(offset),
  193. compat_arg_u64_glue(len), advice);
  194. }
  195. #endif
  196. #endif