Back to home page

LXR

 
 

    


0001 /*
0002  * Implement the manual drop-all-pagecache function
0003  */
0004 
0005 #include <linux/kernel.h>
0006 #include <linux/mm.h>
0007 #include <linux/fs.h>
0008 #include <linux/writeback.h>
0009 #include <linux/sysctl.h>
0010 #include <linux/gfp.h>
0011 #include "internal.h"
0012 
0013 /* A global variable is a bit ugly, but it keeps the code simple */
0014 int sysctl_drop_caches;
0015 
0016 static void drop_pagecache_sb(struct super_block *sb, void *unused)
0017 {
0018     struct inode *inode, *toput_inode = NULL;
0019 
0020     spin_lock(&sb->s_inode_list_lock);
0021     list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
0022         spin_lock(&inode->i_lock);
0023         if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
0024             (inode->i_mapping->nrpages == 0)) {
0025             spin_unlock(&inode->i_lock);
0026             continue;
0027         }
0028         __iget(inode);
0029         spin_unlock(&inode->i_lock);
0030         spin_unlock(&sb->s_inode_list_lock);
0031 
0032         invalidate_mapping_pages(inode->i_mapping, 0, -1);
0033         iput(toput_inode);
0034         toput_inode = inode;
0035 
0036         spin_lock(&sb->s_inode_list_lock);
0037     }
0038     spin_unlock(&sb->s_inode_list_lock);
0039     iput(toput_inode);
0040 }
0041 
0042 int drop_caches_sysctl_handler(struct ctl_table *table, int write,
0043     void __user *buffer, size_t *length, loff_t *ppos)
0044 {
0045     int ret;
0046 
0047     ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
0048     if (ret)
0049         return ret;
0050     if (write) {
0051         static int stfu;
0052 
0053         if (sysctl_drop_caches & 1) {
0054             iterate_supers(drop_pagecache_sb, NULL);
0055             count_vm_event(DROP_PAGECACHE);
0056         }
0057         if (sysctl_drop_caches & 2) {
0058             drop_slab();
0059             count_vm_event(DROP_SLAB);
0060         }
0061         if (!stfu) {
0062             pr_info("%s (%d): drop_caches: %d\n",
0063                 current->comm, task_pid_nr(current),
0064                 sysctl_drop_caches);
0065         }
0066         stfu |= sysctl_drop_caches & 4;
0067     }
0068     return 0;
0069 }