fadvise.c 5.0 KB

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