blob: d72d52b904333767328235a2de720107503cdcf2 [file] [log] [blame]
Andrew Morton9d0243b2006-01-08 01:00:39 -08001/*
2 * Implement the manual drop-all-pagecache function
3 */
4
5#include <linux/kernel.h>
6#include <linux/mm.h>
7#include <linux/fs.h>
8#include <linux/writeback.h>
9#include <linux/sysctl.h>
10#include <linux/gfp.h>
Dave Chinner55fa6092011-03-22 22:23:40 +110011#include "internal.h"
Andrew Morton9d0243b2006-01-08 01:00:39 -080012
13/* A global variable is a bit ugly, but it keeps the code simple */
14int sysctl_drop_caches;
15
Al Viro01a05b32010-03-23 06:06:58 -040016static void drop_pagecache_sb(struct super_block *sb, void *unused)
Andrew Morton9d0243b2006-01-08 01:00:39 -080017{
Jan Karaeccb95c2008-04-29 00:59:37 -070018 struct inode *inode, *toput_inode = NULL;
Andrew Morton9d0243b2006-01-08 01:00:39 -080019
Dave Chinner74278da2015-03-04 12:37:22 -050020 spin_lock(&sb->s_inode_list_lock);
Andrew Morton9d0243b2006-01-08 01:00:39 -080021 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Dave Chinner250df6e2011-03-22 22:23:36 +110022 spin_lock(&inode->i_lock);
23 if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
24 (inode->i_mapping->nrpages == 0)) {
25 spin_unlock(&inode->i_lock);
Andrew Morton9d0243b2006-01-08 01:00:39 -080026 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +110027 }
Jan Karaeccb95c2008-04-29 00:59:37 -070028 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +110029 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -050030 spin_unlock(&sb->s_inode_list_lock);
31
Mike Waychison28697352009-06-16 15:32:59 -070032 invalidate_mapping_pages(inode->i_mapping, 0, -1);
Jan Karaeccb95c2008-04-29 00:59:37 -070033 iput(toput_inode);
34 toput_inode = inode;
Dave Chinner74278da2015-03-04 12:37:22 -050035
36 spin_lock(&sb->s_inode_list_lock);
Andrew Morton9d0243b2006-01-08 01:00:39 -080037 }
Dave Chinner74278da2015-03-04 12:37:22 -050038 spin_unlock(&sb->s_inode_list_lock);
Jan Karaeccb95c2008-04-29 00:59:37 -070039 iput(toput_inode);
Andrew Morton9d0243b2006-01-08 01:00:39 -080040}
41
Joe Perches1f7e0612014-06-06 14:38:05 -070042int drop_caches_sysctl_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -070043 void __user *buffer, size_t *length, loff_t *ppos)
Andrew Morton9d0243b2006-01-08 01:00:39 -080044{
Petr Holasekcb16e952011-03-23 16:43:09 -070045 int ret;
46
47 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
48 if (ret)
49 return ret;
Andrew Morton9d0243b2006-01-08 01:00:39 -080050 if (write) {
Dave Hansen5509a5d2014-04-03 14:48:19 -070051 static int stfu;
52
53 if (sysctl_drop_caches & 1) {
Al Viro01a05b32010-03-23 06:06:58 -040054 iterate_supers(drop_pagecache_sb, NULL);
Dave Hansen5509a5d2014-04-03 14:48:19 -070055 count_vm_event(DROP_PAGECACHE);
56 }
57 if (sysctl_drop_caches & 2) {
Andrew Morton9d0243b2006-01-08 01:00:39 -080058 drop_slab();
Dave Hansen5509a5d2014-04-03 14:48:19 -070059 count_vm_event(DROP_SLAB);
60 }
61 if (!stfu) {
62 pr_info("%s (%d): drop_caches: %d\n",
63 current->comm, task_pid_nr(current),
64 sysctl_drop_caches);
65 }
66 stfu |= sysctl_drop_caches & 4;
Andrew Morton9d0243b2006-01-08 01:00:39 -080067 }
68 return 0;
69}