blob: 82377017130f0137a4208e0082bb61946cc0b817 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andrew Morton9d0243b2006-01-08 01:00:39 -08002/*
3 * Implement the manual drop-all-pagecache function
4 */
5
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>
Dave Chinner55fa6092011-03-22 22:23:40 +110012#include "internal.h"
Andrew Morton9d0243b2006-01-08 01:00:39 -080013
14/* A global variable is a bit ugly, but it keeps the code simple */
15int sysctl_drop_caches;
16
Al Viro01a05b32010-03-23 06:06:58 -040017static void drop_pagecache_sb(struct super_block *sb, void *unused)
Andrew Morton9d0243b2006-01-08 01:00:39 -080018{
Jan Karaeccb95c2008-04-29 00:59:37 -070019 struct inode *inode, *toput_inode = NULL;
Andrew Morton9d0243b2006-01-08 01:00:39 -080020
Dave Chinner74278da2015-03-04 12:37:22 -050021 spin_lock(&sb->s_inode_list_lock);
Andrew Morton9d0243b2006-01-08 01:00:39 -080022 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +110023 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);
Andrew Morton9d0243b2006-01-08 01:00:39 -080027 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +110028 }
Jan Karaeccb95c2008-04-29 00:59:37 -070029 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +110030 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -050031 spin_unlock(&sb->s_inode_list_lock);
32
Mike Waychison28697352009-06-16 15:32:59 -070033 invalidate_mapping_pages(inode->i_mapping, 0, -1);
Jan Karaeccb95c2008-04-29 00:59:37 -070034 iput(toput_inode);
35 toput_inode = inode;
Dave Chinner74278da2015-03-04 12:37:22 -050036
37 spin_lock(&sb->s_inode_list_lock);
Andrew Morton9d0243b2006-01-08 01:00:39 -080038 }
Dave Chinner74278da2015-03-04 12:37:22 -050039 spin_unlock(&sb->s_inode_list_lock);
Jan Karaeccb95c2008-04-29 00:59:37 -070040 iput(toput_inode);
Andrew Morton9d0243b2006-01-08 01:00:39 -080041}
42
Joe Perches1f7e0612014-06-06 14:38:05 -070043int drop_caches_sysctl_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -070044 void __user *buffer, size_t *length, loff_t *ppos)
Andrew Morton9d0243b2006-01-08 01:00:39 -080045{
Petr Holasekcb16e952011-03-23 16:43:09 -070046 int ret;
47
48 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
49 if (ret)
50 return ret;
Andrew Morton9d0243b2006-01-08 01:00:39 -080051 if (write) {
Dave Hansen5509a5d2014-04-03 14:48:19 -070052 static int stfu;
53
54 if (sysctl_drop_caches & 1) {
Al Viro01a05b32010-03-23 06:06:58 -040055 iterate_supers(drop_pagecache_sb, NULL);
Dave Hansen5509a5d2014-04-03 14:48:19 -070056 count_vm_event(DROP_PAGECACHE);
57 }
58 if (sysctl_drop_caches & 2) {
Andrew Morton9d0243b2006-01-08 01:00:39 -080059 drop_slab();
Dave Hansen5509a5d2014-04-03 14:48:19 -070060 count_vm_event(DROP_SLAB);
61 }
62 if (!stfu) {
63 pr_info("%s (%d): drop_caches: %d\n",
64 current->comm, task_pid_nr(current),
65 sysctl_drop_caches);
66 }
67 stfu |= sysctl_drop_caches & 4;
Andrew Morton9d0243b2006-01-08 01:00:39 -080068 }
69 return 0;
70}