Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0 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> 12 : #include "internal.h" 13 : 14 : /* A global variable is a bit ugly, but it keeps the code simple */ 15 : int sysctl_drop_caches; 16 : 17 0 : static void drop_pagecache_sb(struct super_block *sb, void *unused) 18 : { 19 0 : struct inode *inode, *toput_inode = NULL; 20 : 21 0 : spin_lock(&sb->s_inode_list_lock); 22 0 : list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 23 0 : spin_lock(&inode->i_lock); 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 : */ 29 0 : if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || 30 0 : (inode->i_mapping->nrpages == 0 && !need_resched())) { 31 0 : spin_unlock(&inode->i_lock); 32 0 : continue; 33 : } 34 0 : __iget(inode); 35 0 : spin_unlock(&inode->i_lock); 36 0 : spin_unlock(&sb->s_inode_list_lock); 37 : 38 0 : invalidate_mapping_pages(inode->i_mapping, 0, -1); 39 0 : iput(toput_inode); 40 0 : toput_inode = inode; 41 : 42 0 : cond_resched(); 43 0 : spin_lock(&sb->s_inode_list_lock); 44 : } 45 0 : spin_unlock(&sb->s_inode_list_lock); 46 0 : iput(toput_inode); 47 0 : } 48 : 49 0 : int drop_caches_sysctl_handler(struct ctl_table *table, int write, 50 : void *buffer, size_t *length, loff_t *ppos) 51 : { 52 0 : int ret; 53 : 54 0 : ret = proc_dointvec_minmax(table, write, buffer, length, ppos); 55 0 : if (ret) 56 : return ret; 57 0 : if (write) { 58 0 : static int stfu; 59 : 60 0 : if (sysctl_drop_caches & 1) { 61 0 : iterate_supers(drop_pagecache_sb, NULL); 62 0 : count_vm_event(DROP_PAGECACHE); 63 : } 64 0 : if (sysctl_drop_caches & 2) { 65 0 : drop_slab(); 66 0 : count_vm_event(DROP_SLAB); 67 : } 68 0 : if (!stfu) { 69 0 : pr_info("%s (%d): drop_caches: %d\n", 70 : current->comm, task_pid_nr(current), 71 : sysctl_drop_caches); 72 : } 73 0 : stfu |= sysctl_drop_caches & 4; 74 : } 75 : return 0; 76 : }