Skip to content

Commit 9d0243b

Browse files
Andrew MortonLinus Torvalds
authored andcommitted
[PATCH] drop-pagecache
Add /proc/sys/vm/drop_caches. When written to, this will cause the kernel to discard as much pagecache and/or reclaimable slab objects as it can. THis operation requires root permissions. It won't drop dirty data, so the user should run `sync' first. Caveats: a) Holds inode_lock for exorbitant amounts of time. b) Needs to be taught about NUMA nodes: propagate these all the way through so the discarding can be controlled on a per-node basis. This is a debugging feature: useful for getting consistent results between filesystem benchmarks. We could possibly put it under a config option, but it's less than 300 bytes. Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent bec6b0c commit 9d0243b

9 files changed

Lines changed: 107 additions & 5 deletions

File tree

‎Documentation/filesystems/proc.txt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,23 @@ VM has token based thrashing control mechanism and uses the token to prevent
13021302
unnecessary page faults in thrashing situation. The unit of the value is
13031303
second. The value would be useful to tune thrashing behavior.
13041304

1305+
drop_caches
1306+
-----------
1307+
1308+
Writing to this will cause the kernel to drop clean caches, dentries and
1309+
inodes from memory, causing that memory to become free.
1310+
1311+
To free pagecache:
1312+
echo 1 > /proc/sys/vm/drop_caches
1313+
To free dentries and inodes:
1314+
echo 2 > /proc/sys/vm/drop_caches
1315+
To free pagecache, dentries and inodes:
1316+
echo 3 > /proc/sys/vm/drop_caches
1317+
1318+
As this is a non-destructive operation and dirty objects are not freeable, the
1319+
user should run `sync' first.
1320+
1321+
13051322
2.5 /proc/sys/dev - Device specific parameters
13061323
----------------------------------------------
13071324

‎Documentation/sysctl/vm.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ Currently, these files are in /proc/sys/vm:
2626
- min_free_kbytes
2727
- laptop_mode
2828
- block_dump
29+
- drop-caches
2930

3031
==============================================================
3132

3233
dirty_ratio, dirty_background_ratio, dirty_expire_centisecs,
3334
dirty_writeback_centisecs, vfs_cache_pressure, laptop_mode,
34-
block_dump, swap_token_timeout:
35+
block_dump, swap_token_timeout, drop-caches:
3536

3637
See Documentation/filesystems/proc.txt
3738

‎fs/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ obj-y := open.o read_write.o file_table.o buffer.o bio.o super.o \
1010
ioctl.o readdir.o select.o fifo.o locks.o dcache.o inode.o \
1111
attr.o bad_inode.o file.o filesystems.o namespace.o aio.o \
1212
seq_file.o xattr.o libfs.o fs-writeback.o mpage.o direct-io.o \
13-
ioprio.o pnode.o
13+
ioprio.o pnode.o drop_caches.o
1414

1515
obj-$(CONFIG_INOTIFY) += inotify.o
1616
obj-$(CONFIG_EPOLL) += eventpoll.o

‎fs/drop_caches.c‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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>
11+
12+
/* A global variable is a bit ugly, but it keeps the code simple */
13+
int sysctl_drop_caches;
14+
15+
static void drop_pagecache_sb(struct super_block *sb)
16+
{
17+
struct inode *inode;
18+
19+
spin_lock(&inode_lock);
20+
list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
21+
if (inode->i_state & (I_FREEING|I_WILL_FREE))
22+
continue;
23+
invalidate_inode_pages(inode->i_mapping);
24+
}
25+
spin_unlock(&inode_lock);
26+
}
27+
28+
void drop_pagecache(void)
29+
{
30+
struct super_block *sb;
31+
32+
spin_lock(&sb_lock);
33+
restart:
34+
list_for_each_entry(sb, &super_blocks, s_list) {
35+
sb->s_count++;
36+
spin_unlock(&sb_lock);
37+
down_read(&sb->s_umount);
38+
if (sb->s_root)
39+
drop_pagecache_sb(sb);
40+
up_read(&sb->s_umount);
41+
spin_lock(&sb_lock);
42+
if (__put_super_and_need_restart(sb))
43+
goto restart;
44+
}
45+
spin_unlock(&sb_lock);
46+
}
47+
48+
void drop_slab(void)
49+
{
50+
int nr_objects;
51+
52+
do {
53+
nr_objects = shrink_slab(1000, GFP_KERNEL, 1000);
54+
} while (nr_objects > 10);
55+
}
56+
57+
int drop_caches_sysctl_handler(ctl_table *table, int write,
58+
struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
59+
{
60+
proc_dointvec_minmax(table, write, file, buffer, length, ppos);
61+
if (write) {
62+
if (sysctl_drop_caches & 1)
63+
drop_pagecache();
64+
if (sysctl_drop_caches & 2)
65+
drop_slab();
66+
}
67+
return 0;
68+
}

‎include/linux/mm.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,5 +1036,12 @@ int in_gate_area_no_task(unsigned long addr);
10361036
/* /proc/<pid>/oom_adj set to -17 protects from the oom-killer */
10371037
#define OOM_DISABLE -17
10381038

1039+
int drop_caches_sysctl_handler(struct ctl_table *, int, struct file *,
1040+
void __user *, size_t *, loff_t *);
1041+
int shrink_slab(unsigned long scanned, gfp_t gfp_mask,
1042+
unsigned long lru_pages);
1043+
void drop_pagecache(void);
1044+
void drop_slab(void);
1045+
10391046
#endif /* __KERNEL__ */
10401047
#endif /* _LINUX_MM_H */

‎include/linux/sysctl.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ enum
180180
VM_VFS_CACHE_PRESSURE=26, /* dcache/icache reclaim pressure */
181181
VM_LEGACY_VA_LAYOUT=27, /* legacy/compatibility virtual address space layout */
182182
VM_SWAP_TOKEN_TIMEOUT=28, /* default time for token time out */
183+
VM_DROP_PAGECACHE=29, /* int: nuke lots of pagecache */
183184
};
184185

185186

‎kernel/sysctl.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ extern int min_free_kbytes;
6868
extern int printk_ratelimit_jiffies;
6969
extern int printk_ratelimit_burst;
7070
extern int pid_max_min, pid_max_max;
71+
extern int sysctl_drop_caches;
7172

7273
#if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86)
7374
int unknown_nmi_panic;
@@ -774,6 +775,15 @@ static ctl_table vm_table[] = {
774775
.proc_handler = &lowmem_reserve_ratio_sysctl_handler,
775776
.strategy = &sysctl_intvec,
776777
},
778+
{
779+
.ctl_name = VM_DROP_PAGECACHE,
780+
.procname = "drop_caches",
781+
.data = &sysctl_drop_caches,
782+
.maxlen = sizeof(int),
783+
.mode = 0644,
784+
.proc_handler = drop_caches_sysctl_handler,
785+
.strategy = &sysctl_intvec,
786+
},
777787
{
778788
.ctl_name = VM_MIN_FREE_KBYTES,
779789
.procname = "min_free_kbytes",

‎mm/truncate.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ unsigned long invalidate_mapping_pages(struct address_space *mapping,
249249
break;
250250
}
251251
pagevec_release(&pvec);
252-
cond_resched();
253252
}
254253
return ret;
255254
}

‎mm/vmscan.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ EXPORT_SYMBOL(remove_shrinker);
180180
*
181181
* Returns the number of slab objects which we shrunk.
182182
*/
183-
static int shrink_slab(unsigned long scanned, gfp_t gfp_mask,
184-
unsigned long lru_pages)
183+
int shrink_slab(unsigned long scanned, gfp_t gfp_mask, unsigned long lru_pages)
185184
{
186185
struct shrinker *shrinker;
187186
int ret = 0;

0 commit comments

Comments
 (0)