1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * mm/fadvise.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 2002, Linus Torvalds
61da177e4SLinus Torvalds *
7e1f8e874SFrancois Cami * 11Jan2003 Andrew Morton
81da177e4SLinus Torvalds * Initial version.
91da177e4SLinus Torvalds */
101da177e4SLinus Torvalds
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/file.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
141da177e4SLinus Torvalds #include <linux/mm.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
161da177e4SLinus Torvalds #include <linux/backing-dev.h>
171da177e4SLinus Torvalds #include <linux/fadvise.h>
18ebcf28e1SAndrew Morton #include <linux/writeback.h>
191da177e4SLinus Torvalds #include <linux/syscalls.h>
2067d46b29SMel Gorman #include <linux/swap.h>
211da177e4SLinus Torvalds
221da177e4SLinus Torvalds #include <asm/unistd.h>
231da177e4SLinus Torvalds
24cee9a0c4SMatthew Wilcox (Oracle) #include "internal.h"
25cee9a0c4SMatthew Wilcox (Oracle)
261da177e4SLinus Torvalds /*
271da177e4SLinus Torvalds * POSIX_FADV_WILLNEED could set PG_Referenced, and POSIX_FADV_NOREUSE could
281da177e4SLinus Torvalds * deactivate the pages and clear PG_Referenced.
291da177e4SLinus Torvalds */
309d5b7c95SDominik Brodowski
generic_fadvise(struct file * file,loff_t offset,loff_t len,int advice)31cf1ea059SJan Kara int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
321da177e4SLinus Torvalds {
33e748dcd0SMatthew Wilcox struct inode *inode;
341da177e4SLinus Torvalds struct address_space *mapping;
351da177e4SLinus Torvalds struct backing_dev_info *bdi;
36ebcf28e1SAndrew Morton loff_t endbyte; /* inclusive */
371da177e4SLinus Torvalds pgoff_t start_index;
381da177e4SLinus Torvalds pgoff_t end_index;
391da177e4SLinus Torvalds unsigned long nrpages;
401da177e4SLinus Torvalds
4145cd0faaSAmir Goldstein inode = file_inode(file);
4245cd0faaSAmir Goldstein if (S_ISFIFO(inode->i_mode))
4345cd0faaSAmir Goldstein return -ESPIPE;
441da177e4SLinus Torvalds
4545cd0faaSAmir Goldstein mapping = file->f_mapping;
4645cd0faaSAmir Goldstein if (!mapping || len < 0)
4745cd0faaSAmir Goldstein return -EINVAL;
481da177e4SLinus Torvalds
493a77d214SShakeel Butt bdi = inode_to_bdi(mapping->host);
503a77d214SShakeel Butt
513a77d214SShakeel Butt if (IS_DAX(inode) || (bdi == &noop_backing_dev_info)) {
52b5beb1caSMasatake YAMATO switch (advice) {
53b5beb1caSMasatake YAMATO case POSIX_FADV_NORMAL:
54b5beb1caSMasatake YAMATO case POSIX_FADV_RANDOM:
55b5beb1caSMasatake YAMATO case POSIX_FADV_SEQUENTIAL:
56b5beb1caSMasatake YAMATO case POSIX_FADV_WILLNEED:
57b5beb1caSMasatake YAMATO case POSIX_FADV_NOREUSE:
58b5beb1caSMasatake YAMATO case POSIX_FADV_DONTNEED:
59fe77ba6fSCarsten Otte /* no bad return value, but ignore advice */
60b5beb1caSMasatake YAMATO break;
61b5beb1caSMasatake YAMATO default:
6245cd0faaSAmir Goldstein return -EINVAL;
63b5beb1caSMasatake YAMATO }
6445cd0faaSAmir Goldstein return 0;
65b5beb1caSMasatake YAMATO }
66fe77ba6fSCarsten Otte
67a718e28fSAndrey Ryabinin /*
68a718e28fSAndrey Ryabinin * Careful about overflows. Len == 0 means "as much as possible". Use
69a718e28fSAndrey Ryabinin * unsigned math because signed overflows are undefined and UBSan
70a718e28fSAndrey Ryabinin * complains.
71a718e28fSAndrey Ryabinin */
72a718e28fSAndrey Ryabinin endbyte = (u64)offset + (u64)len;
731da177e4SLinus Torvalds if (!len || endbyte < len)
743cd629e5SBrian Foster endbyte = LLONG_MAX;
75ebcf28e1SAndrew Morton else
76ebcf28e1SAndrew Morton endbyte--; /* inclusive */
771da177e4SLinus Torvalds
781da177e4SLinus Torvalds switch (advice) {
791da177e4SLinus Torvalds case POSIX_FADV_NORMAL:
8045cd0faaSAmir Goldstein file->f_ra.ra_pages = bdi->ra_pages;
8145cd0faaSAmir Goldstein spin_lock(&file->f_lock);
8217e81022SYu Zhao file->f_mode &= ~(FMODE_RANDOM | FMODE_NOREUSE);
8345cd0faaSAmir Goldstein spin_unlock(&file->f_lock);
841da177e4SLinus Torvalds break;
851da177e4SLinus Torvalds case POSIX_FADV_RANDOM:
8645cd0faaSAmir Goldstein spin_lock(&file->f_lock);
8745cd0faaSAmir Goldstein file->f_mode |= FMODE_RANDOM;
8845cd0faaSAmir Goldstein spin_unlock(&file->f_lock);
891da177e4SLinus Torvalds break;
901da177e4SLinus Torvalds case POSIX_FADV_SEQUENTIAL:
9145cd0faaSAmir Goldstein file->f_ra.ra_pages = bdi->ra_pages * 2;
9245cd0faaSAmir Goldstein spin_lock(&file->f_lock);
9345cd0faaSAmir Goldstein file->f_mode &= ~FMODE_RANDOM;
9445cd0faaSAmir Goldstein spin_unlock(&file->f_lock);
951da177e4SLinus Torvalds break;
961da177e4SLinus Torvalds case POSIX_FADV_WILLNEED:
971da177e4SLinus Torvalds /* First and last PARTIAL page! */
9809cbfeafSKirill A. Shutemov start_index = offset >> PAGE_SHIFT;
9909cbfeafSKirill A. Shutemov end_index = endbyte >> PAGE_SHIFT;
1001da177e4SLinus Torvalds
1011da177e4SLinus Torvalds /* Careful about overflow on the "+1" */
1021da177e4SLinus Torvalds nrpages = end_index - start_index + 1;
1031da177e4SLinus Torvalds if (!nrpages)
1041da177e4SLinus Torvalds nrpages = ~0UL;
1051da177e4SLinus Torvalds
10645cd0faaSAmir Goldstein force_page_cache_readahead(mapping, file, start_index, nrpages);
1071da177e4SLinus Torvalds break;
10860c371bcSAndrew Morton case POSIX_FADV_NOREUSE:
10917e81022SYu Zhao spin_lock(&file->f_lock);
11017e81022SYu Zhao file->f_mode |= FMODE_NOREUSE;
11117e81022SYu Zhao spin_unlock(&file->f_lock);
11260c371bcSAndrew Morton break;
1131da177e4SLinus Torvalds case POSIX_FADV_DONTNEED:
114ad8a1b55SShawn Bohrer __filemap_fdatawrite_range(mapping, offset, endbyte,
115ad8a1b55SShawn Bohrer WB_SYNC_NONE);
1161da177e4SLinus Torvalds
117441c228fSMel Gorman /*
118441c228fSMel Gorman * First and last FULL page! Partial pages are deliberately
119441c228fSMel Gorman * preserved on the expectation that it is better to preserve
120441c228fSMel Gorman * needed memory than to discard unneeded memory.
121441c228fSMel Gorman */
12209cbfeafSKirill A. Shutemov start_index = (offset+(PAGE_SIZE-1)) >> PAGE_SHIFT;
12309cbfeafSKirill A. Shutemov end_index = (endbyte >> PAGE_SHIFT);
124a7ab400dSshidao.ytt /*
125a7ab400dSshidao.ytt * The page at end_index will be inclusively discarded according
126a7ab400dSshidao.ytt * by invalidate_mapping_pages(), so subtracting 1 from
127a7ab400dSshidao.ytt * end_index means we will skip the last page. But if endbyte
128a7ab400dSshidao.ytt * is page aligned or is at the end of file, we should not skip
129a7ab400dSshidao.ytt * that page - discarding the last page is safe enough.
130a7ab400dSshidao.ytt */
131a7ab400dSshidao.ytt if ((endbyte & ~PAGE_MASK) != ~PAGE_MASK &&
132a7ab400dSshidao.ytt endbyte != inode->i_size - 1) {
13318aba41cSOleg Drokin /* First page is tricky as 0 - 1 = -1, but pgoff_t
13418aba41cSOleg Drokin * is unsigned, so the end_index >= start_index
13518aba41cSOleg Drokin * check below would be true and we'll discard the whole
13618aba41cSOleg Drokin * file cache which is not what was asked.
13718aba41cSOleg Drokin */
13818aba41cSOleg Drokin if (end_index == 0)
13918aba41cSOleg Drokin break;
14018aba41cSOleg Drokin
14118aba41cSOleg Drokin end_index--;
14218aba41cSOleg Drokin }
1431da177e4SLinus Torvalds
14467d46b29SMel Gorman if (end_index >= start_index) {
145*1a0fc811SMatthew Wilcox (Oracle) unsigned long nr_failed = 0;
1464dd72b4aSJohannes Weiner
1474dd72b4aSJohannes Weiner /*
1484dd72b4aSJohannes Weiner * It's common to FADV_DONTNEED right after
1494dd72b4aSJohannes Weiner * the read or write that instantiates the
1504dd72b4aSJohannes Weiner * pages, in which case there will be some
1514dd72b4aSJohannes Weiner * sitting on the local LRU cache. Try to
1524dd72b4aSJohannes Weiner * avoid the expensive remote drain and the
1534dd72b4aSJohannes Weiner * second cache tree walk below by flushing
1544dd72b4aSJohannes Weiner * them out right away.
1554dd72b4aSJohannes Weiner */
1564dd72b4aSJohannes Weiner lru_add_drain();
1574dd72b4aSJohannes Weiner
158*1a0fc811SMatthew Wilcox (Oracle) mapping_try_invalidate(mapping, start_index, end_index,
159*1a0fc811SMatthew Wilcox (Oracle) &nr_failed);
16067d46b29SMel Gorman
16167d46b29SMel Gorman /*
162*1a0fc811SMatthew Wilcox (Oracle) * The failures may be due to the folio being
163*1a0fc811SMatthew Wilcox (Oracle) * in the LRU cache of a remote CPU. Drain all
164*1a0fc811SMatthew Wilcox (Oracle) * caches and try again.
16567d46b29SMel Gorman */
166*1a0fc811SMatthew Wilcox (Oracle) if (nr_failed) {
16767d46b29SMel Gorman lru_add_drain_all();
168ebcf28e1SAndrew Morton invalidate_mapping_pages(mapping, start_index,
169ebcf28e1SAndrew Morton end_index);
17067d46b29SMel Gorman }
17167d46b29SMel Gorman }
172ebcf28e1SAndrew Morton break;
1731da177e4SLinus Torvalds default:
17445cd0faaSAmir Goldstein return -EINVAL;
1751da177e4SLinus Torvalds }
17645cd0faaSAmir Goldstein return 0;
17745cd0faaSAmir Goldstein }
178cf1ea059SJan Kara EXPORT_SYMBOL(generic_fadvise);
17945cd0faaSAmir Goldstein
vfs_fadvise(struct file * file,loff_t offset,loff_t len,int advice)18045cd0faaSAmir Goldstein int vfs_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
18145cd0faaSAmir Goldstein {
18245cd0faaSAmir Goldstein if (file->f_op->fadvise)
18345cd0faaSAmir Goldstein return file->f_op->fadvise(file, offset, len, advice);
18445cd0faaSAmir Goldstein
18545cd0faaSAmir Goldstein return generic_fadvise(file, offset, len, advice);
18645cd0faaSAmir Goldstein }
18745cd0faaSAmir Goldstein EXPORT_SYMBOL(vfs_fadvise);
18845cd0faaSAmir Goldstein
1893d8f7615SAmir Goldstein #ifdef CONFIG_ADVISE_SYSCALLS
1903d8f7615SAmir Goldstein
ksys_fadvise64_64(int fd,loff_t offset,loff_t len,int advice)19145cd0faaSAmir Goldstein int ksys_fadvise64_64(int fd, loff_t offset, loff_t len, int advice)
19245cd0faaSAmir Goldstein {
19345cd0faaSAmir Goldstein struct fd f = fdget(fd);
19445cd0faaSAmir Goldstein int ret;
19545cd0faaSAmir Goldstein
19645cd0faaSAmir Goldstein if (!f.file)
19745cd0faaSAmir Goldstein return -EBADF;
19845cd0faaSAmir Goldstein
19945cd0faaSAmir Goldstein ret = vfs_fadvise(f.file, offset, len, advice);
20045cd0faaSAmir Goldstein
2012903ff01SAl Viro fdput(f);
2021da177e4SLinus Torvalds return ret;
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds
SYSCALL_DEFINE4(fadvise64_64,int,fd,loff_t,offset,loff_t,len,int,advice)2059d5b7c95SDominik Brodowski SYSCALL_DEFINE4(fadvise64_64, int, fd, loff_t, offset, loff_t, len, int, advice)
2069d5b7c95SDominik Brodowski {
2079d5b7c95SDominik Brodowski return ksys_fadvise64_64(fd, offset, len, advice);
2089d5b7c95SDominik Brodowski }
2099d5b7c95SDominik Brodowski
2101da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_FADVISE64
2111da177e4SLinus Torvalds
SYSCALL_DEFINE4(fadvise64,int,fd,loff_t,offset,size_t,len,int,advice)2124a0fd5bfSAl Viro SYSCALL_DEFINE4(fadvise64, int, fd, loff_t, offset, size_t, len, int, advice)
2131da177e4SLinus Torvalds {
2149d5b7c95SDominik Brodowski return ksys_fadvise64_64(fd, offset, len, advice);
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds
2171da177e4SLinus Torvalds #endif
21859c10c52SGuo Ren
21959c10c52SGuo Ren #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_FADVISE64_64)
22059c10c52SGuo Ren
COMPAT_SYSCALL_DEFINE6(fadvise64_64,int,fd,compat_arg_u64_dual (offset),compat_arg_u64_dual (len),int,advice)22159c10c52SGuo Ren COMPAT_SYSCALL_DEFINE6(fadvise64_64, int, fd, compat_arg_u64_dual(offset),
22259c10c52SGuo Ren compat_arg_u64_dual(len), int, advice)
22359c10c52SGuo Ren {
22459c10c52SGuo Ren return ksys_fadvise64_64(fd, compat_arg_u64_glue(offset),
22559c10c52SGuo Ren compat_arg_u64_glue(len), advice);
22659c10c52SGuo Ren }
22759c10c52SGuo Ren
22859c10c52SGuo Ren #endif
2293d8f7615SAmir Goldstein #endif
230