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); |
Jan Kara | c3ce520 | 2019-02-01 14:21:23 -0800 | [diff] [blame] | 23 | /* |
| 24 | * We must skip inodes in unusual state. We may also skip |
| 25 | * inodes without pages but we deliberately won't in case |
| 26 | * we need to reschedule to avoid softlockups. |
| 27 | */ |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 28 | if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || |
Jan Kara | c3ce520 | 2019-02-01 14:21:23 -0800 | [diff] [blame] | 29 | (inode->i_mapping->nrpages == 0 && !need_resched())) { |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 30 | spin_unlock(&inode->i_lock); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 31 | continue; |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 32 | } |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 33 | __iget(inode); |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 34 | spin_unlock(&inode->i_lock); |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 35 | spin_unlock(&sb->s_inode_list_lock); |
| 36 | |
Jan Kara | c3ce520 | 2019-02-01 14:21:23 -0800 | [diff] [blame] | 37 | cond_resched(); |
Mike Waychison | 2869735 | 2009-06-16 15:32:59 -0700 | [diff] [blame] | 38 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 39 | iput(toput_inode); |
| 40 | toput_inode = inode; |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 41 | |
| 42 | spin_lock(&sb->s_inode_list_lock); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 43 | } |
Dave Chinner | 74278da | 2015-03-04 12:37:22 -0500 | [diff] [blame] | 44 | spin_unlock(&sb->s_inode_list_lock); |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 45 | iput(toput_inode); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Joe Perches | 1f7e061 | 2014-06-06 14:38:05 -0700 | [diff] [blame] | 48 | int drop_caches_sysctl_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 49 | void __user *buffer, size_t *length, loff_t *ppos) |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 50 | { |
Petr Holasek | cb16e95 | 2011-03-23 16:43:09 -0700 | [diff] [blame] | 51 | int ret; |
| 52 | |
| 53 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 54 | if (ret) |
| 55 | return ret; |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 56 | if (write) { |
Dave Hansen | 5509a5d | 2014-04-03 14:48:19 -0700 | [diff] [blame] | 57 | static int stfu; |
| 58 | |
| 59 | if (sysctl_drop_caches & 1) { |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 60 | iterate_supers(drop_pagecache_sb, NULL); |
Dave Hansen | 5509a5d | 2014-04-03 14:48:19 -0700 | [diff] [blame] | 61 | count_vm_event(DROP_PAGECACHE); |
| 62 | } |
| 63 | if (sysctl_drop_caches & 2) { |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 64 | drop_slab(); |
Dave Hansen | 5509a5d | 2014-04-03 14:48:19 -0700 | [diff] [blame] | 65 | count_vm_event(DROP_SLAB); |
| 66 | } |
| 67 | if (!stfu) { |
| 68 | pr_info("%s (%d): drop_caches: %d\n", |
| 69 | current->comm, task_pid_nr(current), |
| 70 | sysctl_drop_caches); |
| 71 | } |
| 72 | stfu |= sysctl_drop_caches & 4; |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 73 | } |
| 74 | return 0; |
| 75 | } |