drop_caches.c 1.8 KB
Newer Older
A
Andrew Morton 已提交
1 2 3 4 5 6 7 8 9 10
/*
 * Implement the manual drop-all-pagecache function
 */

#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/writeback.h>
#include <linux/sysctl.h>
#include <linux/gfp.h>
11
#include "internal.h"
A
Andrew Morton 已提交
12 13 14 15

/* A global variable is a bit ugly, but it keeps the code simple */
int sysctl_drop_caches;

A
Al Viro 已提交
16
static void drop_pagecache_sb(struct super_block *sb, void *unused)
A
Andrew Morton 已提交
17
{
18
	struct inode *inode, *toput_inode = NULL;
A
Andrew Morton 已提交
19

20
	spin_lock(&inode_sb_list_lock);
A
Andrew Morton 已提交
21
	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
22 23 24 25
		spin_lock(&inode->i_lock);
		if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
		    (inode->i_mapping->nrpages == 0)) {
			spin_unlock(&inode->i_lock);
26
			continue;
27
		}
28
		__iget(inode);
29
		spin_unlock(&inode->i_lock);
30
		spin_unlock(&inode_sb_list_lock);
31
		invalidate_mapping_pages(inode->i_mapping, 0, -1);
32 33
		iput(toput_inode);
		toput_inode = inode;
34
		spin_lock(&inode_sb_list_lock);
A
Andrew Morton 已提交
35
	}
36
	spin_unlock(&inode_sb_list_lock);
37
	iput(toput_inode);
A
Andrew Morton 已提交
38 39
}

40
static void drop_slab(void)
A
Andrew Morton 已提交
41 42 43 44
{
	int nr_objects;

	do {
45 46 47 48 49 50
		int nid;

		nr_objects = 0;
		for_each_online_node(nid)
			nr_objects += shrink_node_slabs(GFP_KERNEL, nid,
							1000, 1000);
A
Andrew Morton 已提交
51 52 53
	} while (nr_objects > 10);
}

54
int drop_caches_sysctl_handler(struct ctl_table *table, int write,
55
	void __user *buffer, size_t *length, loff_t *ppos)
A
Andrew Morton 已提交
56
{
57 58 59 60 61
	int ret;

	ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
	if (ret)
		return ret;
A
Andrew Morton 已提交
62
	if (write) {
63 64 65
		static int stfu;

		if (sysctl_drop_caches & 1) {
A
Al Viro 已提交
66
			iterate_supers(drop_pagecache_sb, NULL);
67 68 69
			count_vm_event(DROP_PAGECACHE);
		}
		if (sysctl_drop_caches & 2) {
A
Andrew Morton 已提交
70
			drop_slab();
71 72 73 74 75 76 77 78
			count_vm_event(DROP_SLAB);
		}
		if (!stfu) {
			pr_info("%s (%d): drop_caches: %d\n",
				current->comm, task_pid_nr(current),
				sysctl_drop_caches);
		}
		stfu |= sysctl_drop_caches & 4;
A
Andrew Morton 已提交
79 80 81
	}
	return 0;
}