2 * Implement the manual drop-all-pagecache function
5 #include <linux/kernel.h>
8 #include <linux/writeback.h>
9 #include <linux/sysctl.h>
10 #include <linux/gfp.h>
11 #include <linux/export.h>
14 /* A global variable is a bit ugly, but it keeps the code simple */
15 int sysctl_drop_caches
;
17 static void drop_pagecache_sb(struct super_block
*sb
, void *unused
)
19 struct inode
*inode
, *toput_inode
= NULL
;
21 spin_lock(&inode_sb_list_lock
);
22 list_for_each_entry(inode
, &sb
->s_inodes
, i_sb_list
) {
23 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
);
30 spin_unlock(&inode
->i_lock
);
31 spin_unlock(&inode_sb_list_lock
);
32 invalidate_mapping_pages(inode
->i_mapping
, 0, -1);
35 spin_lock(&inode_sb_list_lock
);
37 spin_unlock(&inode_sb_list_lock
);
41 static void drop_slab(void)
44 struct shrink_control shrink
= {
45 .gfp_mask
= GFP_KERNEL
,
49 nr_objects
= shrink_slab(&shrink
, 1000, 1000);
50 } while (nr_objects
> 10);
54 void drop_pagecache(void)
56 iterate_supers(drop_pagecache_sb
, NULL
);
58 EXPORT_SYMBOL_GPL(drop_pagecache
);
60 int drop_caches_sysctl_handler(ctl_table
*table
, int write
,
61 void __user
*buffer
, size_t *length
, loff_t
*ppos
)
65 ret
= proc_dointvec_minmax(table
, write
, buffer
, length
, ppos
);
69 if (sysctl_drop_caches
& 1)
70 iterate_supers(drop_pagecache_sb
, NULL
);
71 if (sysctl_drop_caches
& 2)