Merge tag 'for-4.19-rc2-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[linux-2.6/btrfs-unstable.git] / fs / drop_caches.c
blob82377017130f0137a4208e0082bb61946cc0b817
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Implement the manual drop-all-pagecache function
4 */
6 #include <linux/kernel.h>
7 #include <linux/mm.h>
8 #include <linux/fs.h>
9 #include <linux/writeback.h>
10 #include <linux/sysctl.h>
11 #include <linux/gfp.h>
12 #include "internal.h"
14 /* A global variable is a bit ugly, but it keeps the code simple */
15 int sysctl_drop_caches;
17 static void drop_pagecache_sb(struct super_block *sb, void *unused)
19 struct inode *inode, *toput_inode = NULL;
21 spin_lock(&sb->s_inode_list_lock);
22 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
23 spin_lock(&inode->i_lock);
24 if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
25 (inode->i_mapping->nrpages == 0)) {
26 spin_unlock(&inode->i_lock);
27 continue;
29 __iget(inode);
30 spin_unlock(&inode->i_lock);
31 spin_unlock(&sb->s_inode_list_lock);
33 invalidate_mapping_pages(inode->i_mapping, 0, -1);
34 iput(toput_inode);
35 toput_inode = inode;
37 spin_lock(&sb->s_inode_list_lock);
39 spin_unlock(&sb->s_inode_list_lock);
40 iput(toput_inode);
43 int drop_caches_sysctl_handler(struct ctl_table *table, int write,
44 void __user *buffer, size_t *length, loff_t *ppos)
46 int ret;
48 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
49 if (ret)
50 return ret;
51 if (write) {
52 static int stfu;
54 if (sysctl_drop_caches & 1) {
55 iterate_supers(drop_pagecache_sb, NULL);
56 count_vm_event(DROP_PAGECACHE);
58 if (sysctl_drop_caches & 2) {
59 drop_slab();
60 count_vm_event(DROP_SLAB);
62 if (!stfu) {
63 pr_info("%s (%d): drop_caches: %d\n",
64 current->comm, task_pid_nr(current),
65 sysctl_drop_caches);
67 stfu |= sysctl_drop_caches & 4;
69 return 0;