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