Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 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> |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 11 | #include "internal.h" |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 12 | |
| 13 | /* A global variable is a bit ugly, but it keeps the code simple */ |
| 14 | int sysctl_drop_caches; |
| 15 | |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 16 | static void drop_pagecache_sb(struct super_block *sb, void *unused) |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 17 | { |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 18 | struct inode *inode, *toput_inode = NULL; |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 19 | |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 20 | spin_lock(&inode_sb_list_lock); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 21 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 22 | spin_lock(&inode->i_lock); |
| 23 | if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || |
| 24 | (inode->i_mapping->nrpages == 0)) { |
| 25 | spin_unlock(&inode->i_lock); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 26 | continue; |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 27 | } |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 28 | __iget(inode); |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 29 | spin_unlock(&inode->i_lock); |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 30 | spin_unlock(&inode_sb_list_lock); |
Mike Waychison | 2869735 | 2009-06-16 15:32:59 -0700 | [diff] [blame] | 31 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 32 | iput(toput_inode); |
| 33 | toput_inode = inode; |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 34 | spin_lock(&inode_sb_list_lock); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 35 | } |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 36 | spin_unlock(&inode_sb_list_lock); |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 37 | iput(toput_inode); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Adrian Bunk | 07d45da | 2008-04-29 00:58:57 -0700 | [diff] [blame] | 40 | static void drop_slab(void) |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 41 | { |
| 42 | int nr_objects; |
Ying Han | a09ed5e | 2011-05-24 17:12:26 -0700 | [diff] [blame] | 43 | struct shrink_control shrink = { |
| 44 | .gfp_mask = GFP_KERNEL, |
Ying Han | a09ed5e | 2011-05-24 17:12:26 -0700 | [diff] [blame] | 45 | }; |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 46 | |
Dave Chinner | 0ce3d74 | 2013-08-28 10:18:03 +1000 | [diff] [blame] | 47 | nodes_setall(shrink.nodes_to_scan); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 48 | do { |
Ying Han | 1495f23 | 2011-05-24 17:12:27 -0700 | [diff] [blame] | 49 | nr_objects = shrink_slab(&shrink, 1000, 1000); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 50 | } while (nr_objects > 10); |
| 51 | } |
| 52 | |
| 53 | int drop_caches_sysctl_handler(ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 54 | void __user *buffer, size_t *length, loff_t *ppos) |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 55 | { |
Petr Holasek | cb16e95 | 2011-03-23 16:43:09 -0700 | [diff] [blame] | 56 | int ret; |
| 57 | |
| 58 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 59 | if (ret) |
| 60 | return ret; |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 61 | if (write) { |
Dave Hansen | 5509a5d | 2014-04-03 14:48:19 -0700 | [diff] [blame] | 62 | static int stfu; |
| 63 | |
| 64 | if (sysctl_drop_caches & 1) { |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 65 | iterate_supers(drop_pagecache_sb, NULL); |
Dave Hansen | 5509a5d | 2014-04-03 14:48:19 -0700 | [diff] [blame] | 66 | count_vm_event(DROP_PAGECACHE); |
| 67 | } |
| 68 | if (sysctl_drop_caches & 2) { |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 69 | drop_slab(); |
Dave Hansen | 5509a5d | 2014-04-03 14:48:19 -0700 | [diff] [blame] | 70 | count_vm_event(DROP_SLAB); |
| 71 | } |
| 72 | if (!stfu) { |
| 73 | pr_info("%s (%d): drop_caches: %d\n", |
| 74 | current->comm, task_pid_nr(current), |
| 75 | sysctl_drop_caches); |
| 76 | } |
| 77 | stfu |= sysctl_drop_caches & 4; |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 78 | } |
| 79 | return 0; |
| 80 | } |