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