| 1 | /* |
| 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> |
| 11 | #include <linux/export.h> |
| 12 | #include "internal.h" |
| 13 | |
| 14 | /* A global variable is a bit ugly, but it keeps the code simple */ |
| 15 | int sysctl_drop_caches; |
| 16 | |
| 17 | static void drop_pagecache_sb(struct super_block *sb, void *unused) |
| 18 | { |
| 19 | struct inode *inode, *toput_inode = NULL; |
| 20 | |
| 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); |
| 27 | continue; |
| 28 | } |
| 29 | __iget(inode); |
| 30 | spin_unlock(&inode->i_lock); |
| 31 | spin_unlock(&inode_sb_list_lock); |
| 32 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
| 33 | iput(toput_inode); |
| 34 | toput_inode = inode; |
| 35 | spin_lock(&inode_sb_list_lock); |
| 36 | } |
| 37 | spin_unlock(&inode_sb_list_lock); |
| 38 | iput(toput_inode); |
| 39 | } |
| 40 | |
| 41 | static void drop_slab(void) |
| 42 | { |
| 43 | int nr_objects; |
| 44 | struct shrink_control shrink = { |
| 45 | .gfp_mask = GFP_KERNEL, |
| 46 | }; |
| 47 | |
| 48 | do { |
| 49 | nr_objects = shrink_slab(&shrink, 1000, 1000); |
| 50 | } while (nr_objects > 10); |
| 51 | } |
| 52 | |
| 53 | /* For TuxOnIce */ |
| 54 | void drop_pagecache(void) |
| 55 | { |
| 56 | iterate_supers(drop_pagecache_sb, NULL); |
| 57 | } |
| 58 | EXPORT_SYMBOL_GPL(drop_pagecache); |
| 59 | |
| 60 | int drop_caches_sysctl_handler(ctl_table *table, int write, |
| 61 | void __user *buffer, size_t *length, loff_t *ppos) |
| 62 | { |
| 63 | int ret; |
| 64 | |
| 65 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 66 | if (ret) |
| 67 | return ret; |
| 68 | if (write) { |
| 69 | if (sysctl_drop_caches & 1) |
| 70 | iterate_supers(drop_pagecache_sb, NULL); |
| 71 | if (sysctl_drop_caches & 2) |
| 72 | drop_slab(); |
| 73 | } |
| 74 | return 0; |
| 75 | } |