/*
 * 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>
#include "internal.h"

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

static void drop_pagecache_sb(struct super_block *sb, void *unused)
{
	struct inode *inode, *toput_inode = NULL;

	spin_lock(&sb->s_inode_list_lock);
	list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
		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);
			continue;
		}
		__iget(inode);
		spin_unlock(&inode->i_lock);
		spin_unlock(&sb->s_inode_list_lock);

		invalidate_mapping_pages(inode->i_mapping, 0, -1);
		iput(toput_inode);
		toput_inode = inode;

		spin_lock(&sb->s_inode_list_lock);
	}
	spin_unlock(&sb->s_inode_list_lock);
	iput(toput_inode);
}

int drop_caches_sysctl_handler(struct ctl_table *table, int write,
	void __user *buffer, size_t *length, loff_t *ppos)
{
	int ret;

	ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
	if (ret)
		return ret;
	if (write) {
		static int stfu;

		if (sysctl_drop_caches & 1) {
			iterate_supers(drop_pagecache_sb, NULL);
			count_vm_event(DROP_PAGECACHE);
		}
		if (sysctl_drop_caches & 2) {
			drop_slab();
			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;
	}
	return 0;
}
ux/net-next.git/refs/?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78'>refs</a><a class='active' href='/cgit.cgi/linux/net-next.git/log/tools/perf/pmu-events/arch/x86/jaketown/memory.json'>log</a><a href='/cgit.cgi/linux/net-next.git/tree/tools/perf/pmu-events/arch/x86/jaketown/memory.json?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78'>tree</a><a href='/cgit.cgi/linux/net-next.git/commit/tools/perf/pmu-events/arch/x86/jaketown/memory.json?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78'>commit</a><a href='/cgit.cgi/linux/net-next.git/diff/tools/perf/pmu-events/arch/x86/jaketown/memory.json?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78'>diff</a></td><td class='form'><form class='right' method='get' action='/cgit.cgi/linux/net-next.git/log/tools/perf/pmu-events/arch/x86/jaketown/memory.json'>
<input type='hidden' name='id' value='33a48927c193d030c80ecaeb3e021b7ed85f9c78'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/cgit.cgi/linux/net-next.git/log/?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78'>root</a>/<a href='/cgit.cgi/linux/net-next.git/log/tools?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78'>tools</a>/<a href='/cgit.cgi/linux/net-next.git/log/tools/perf?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78'>perf</a>/<a href='/cgit.cgi/linux/net-next.git/log/tools/perf/pmu-events?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78'>pmu-events</a>/<a href='/cgit.cgi/linux/net-next.git/log/tools/perf/pmu-events/arch?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78'>arch</a>/<a href='/cgit.cgi/linux/net-next.git/log/tools/perf/pmu-events/arch/x86?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78'>x86</a>/<a href='/cgit.cgi/linux/net-next.git/log/tools/perf/pmu-events/arch/x86/jaketown?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78'>jaketown</a>/<a href='/cgit.cgi/linux/net-next.git/log/tools/perf/pmu-events/arch/x86/jaketown/memory.json?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78'>memory.json</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th class='left'>Age</th><th class='left'>Commit message (<a href='/cgit.cgi/linux/net-next.git/log/tools/perf/pmu-events/arch/x86/jaketown/memory.json?id=33a48927c193d030c80ecaeb3e021b7ed85f9c78&amp;showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Files</th><th class='left'>Lines</th></tr>