Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
9d0243bc AM |
2 | /* |
3 | * Implement the manual drop-all-pagecache function | |
4 | */ | |
5 | ||
16e2df2a | 6 | #include <linux/pagemap.h> |
9d0243bc AM |
7 | #include <linux/kernel.h> |
8 | #include <linux/mm.h> | |
9 | #include <linux/fs.h> | |
10 | #include <linux/writeback.h> | |
11 | #include <linux/sysctl.h> | |
12 | #include <linux/gfp.h> | |
55fa6091 | 13 | #include "internal.h" |
9d0243bc AM |
14 | |
15 | /* A global variable is a bit ugly, but it keeps the code simple */ | |
16 | int sysctl_drop_caches; | |
17 | ||
01a05b33 | 18 | static void drop_pagecache_sb(struct super_block *sb, void *unused) |
9d0243bc | 19 | { |
eccb95ce | 20 | struct inode *inode, *toput_inode = NULL; |
9d0243bc | 21 | |
74278da9 | 22 | spin_lock(&sb->s_inode_list_lock); |
9d0243bc | 23 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { |
250df6ed | 24 | spin_lock(&inode->i_lock); |
c27d82f5 JK |
25 | /* |
26 | * We must skip inodes in unusual state. We may also skip | |
27 | * inodes without pages but we deliberately won't in case | |
28 | * we need to reschedule to avoid softlockups. | |
29 | */ | |
250df6ed | 30 | if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || |
16e2df2a | 31 | (mapping_empty(inode->i_mapping) && !need_resched())) { |
250df6ed | 32 | spin_unlock(&inode->i_lock); |
af065b8a | 33 | continue; |
250df6ed | 34 | } |
eccb95ce | 35 | __iget(inode); |
250df6ed | 36 | spin_unlock(&inode->i_lock); |
74278da9 DC |
37 | spin_unlock(&sb->s_inode_list_lock); |
38 | ||
28697355 | 39 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
eccb95ce JK |
40 | iput(toput_inode); |
41 | toput_inode = inode; | |
74278da9 | 42 | |
04646aeb | 43 | cond_resched(); |
74278da9 | 44 | spin_lock(&sb->s_inode_list_lock); |
9d0243bc | 45 | } |
74278da9 | 46 | spin_unlock(&sb->s_inode_list_lock); |
eccb95ce | 47 | iput(toput_inode); |
9d0243bc AM |
48 | } |
49 | ||
1f7e0616 | 50 | int drop_caches_sysctl_handler(struct ctl_table *table, int write, |
32927393 | 51 | void *buffer, size_t *length, loff_t *ppos) |
9d0243bc | 52 | { |
cb16e95f PH |
53 | int ret; |
54 | ||
55 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); | |
56 | if (ret) | |
57 | return ret; | |
9d0243bc | 58 | if (write) { |
5509a5d2 DH |
59 | static int stfu; |
60 | ||
61 | if (sysctl_drop_caches & 1) { | |
01a05b33 | 62 | iterate_supers(drop_pagecache_sb, NULL); |
5509a5d2 DH |
63 | count_vm_event(DROP_PAGECACHE); |
64 | } | |
65 | if (sysctl_drop_caches & 2) { | |
9d0243bc | 66 | drop_slab(); |
5509a5d2 DH |
67 | count_vm_event(DROP_SLAB); |
68 | } | |
69 | if (!stfu) { | |
70 | pr_info("%s (%d): drop_caches: %d\n", | |
71 | current->comm, task_pid_nr(current), | |
72 | sysctl_drop_caches); | |
73 | } | |
74 | stfu |= sysctl_drop_caches & 4; | |
9d0243bc AM |
75 | } |
76 | return 0; | |
77 | } |