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; |
| 43 | |
| 44 | do { |
Johannes Weiner | 6b4f779 | 2014-12-12 16:56:13 -0800 | [diff] [blame] | 45 | int nid; |
| 46 | |
| 47 | nr_objects = 0; |
| 48 | for_each_online_node(nid) |
| 49 | nr_objects += shrink_node_slabs(GFP_KERNEL, nid, |
| 50 | 1000, 1000); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 51 | } while (nr_objects > 10); |
| 52 | } |
| 53 | |
Joe Perches | 1f7e061 | 2014-06-06 14:38:05 -0700 | [diff] [blame] | 54 | int drop_caches_sysctl_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 55 | void __user *buffer, size_t *length, loff_t *ppos) |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 56 | { |
Petr Holasek | cb16e95 | 2011-03-23 16:43:09 -0700 | [diff] [blame] | 57 | int ret; |
| 58 | |
| 59 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 60 | if (ret) |
| 61 | return ret; |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 62 | if (write) { |
Dave Hansen | 5509a5d | 2014-04-03 14:48:19 -0700 | [diff] [blame] | 63 | static int stfu; |
| 64 | |
| 65 | if (sysctl_drop_caches & 1) { |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 66 | iterate_supers(drop_pagecache_sb, NULL); |
Dave Hansen | 5509a5d | 2014-04-03 14:48:19 -0700 | [diff] [blame] | 67 | count_vm_event(DROP_PAGECACHE); |
| 68 | } |
| 69 | if (sysctl_drop_caches & 2) { |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 70 | drop_slab(); |
Dave Hansen | 5509a5d | 2014-04-03 14:48:19 -0700 | [diff] [blame] | 71 | count_vm_event(DROP_SLAB); |
| 72 | } |
| 73 | if (!stfu) { |
| 74 | pr_info("%s (%d): drop_caches: %d\n", |
| 75 | current->comm, task_pid_nr(current), |
| 76 | sysctl_drop_caches); |
| 77 | } |
| 78 | stfu |= sysctl_drop_caches & 4; |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 79 | } |
| 80 | return 0; |
| 81 | } |