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 | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame^] | 20 | spin_lock(&sb->s_inode_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 | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame^] | 30 | spin_unlock(&sb->s_inode_list_lock); |
| 31 | |
Mike Waychison | 2869735 | 2009-06-16 15:32:59 -0700 | [diff] [blame] | 32 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 33 | iput(toput_inode); |
| 34 | toput_inode = inode; |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame^] | 35 | |
| 36 | spin_lock(&sb->s_inode_list_lock); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 37 | } |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame^] | 38 | spin_unlock(&sb->s_inode_list_lock); |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 39 | iput(toput_inode); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Joe Perches | 1f7e061 | 2014-06-06 14:38:05 -0700 | [diff] [blame] | 42 | int drop_caches_sysctl_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 43 | void __user *buffer, size_t *length, loff_t *ppos) |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 44 | { |
Petr Holasek | cb16e95 | 2011-03-23 16:43:09 -0700 | [diff] [blame] | 45 | int ret; |
| 46 | |
| 47 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 48 | if (ret) |
| 49 | return ret; |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 50 | if (write) { |
Dave Hansen | 5509a5d | 2014-04-03 14:48:19 -0700 | [diff] [blame] | 51 | static int stfu; |
| 52 | |
| 53 | if (sysctl_drop_caches & 1) { |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 54 | iterate_supers(drop_pagecache_sb, NULL); |
Dave Hansen | 5509a5d | 2014-04-03 14:48:19 -0700 | [diff] [blame] | 55 | count_vm_event(DROP_PAGECACHE); |
| 56 | } |
| 57 | if (sysctl_drop_caches & 2) { |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 58 | drop_slab(); |
Dave Hansen | 5509a5d | 2014-04-03 14:48:19 -0700 | [diff] [blame] | 59 | count_vm_event(DROP_SLAB); |
| 60 | } |
| 61 | if (!stfu) { |
| 62 | pr_info("%s (%d): drop_caches: %d\n", |
| 63 | current->comm, task_pid_nr(current), |
| 64 | sysctl_drop_caches); |
| 65 | } |
| 66 | stfu |= sysctl_drop_caches & 4; |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 67 | } |
| 68 | return 0; |
| 69 | } |