summaryrefslogtreecommitdiffstats
path: root/patches/boot_time_opt_guest/0154-sysctl-vm-Fine-grained-cache-shrinking.patch
diff options
context:
space:
mode:
Diffstat (limited to 'patches/boot_time_opt_guest/0154-sysctl-vm-Fine-grained-cache-shrinking.patch')
-rw-r--r--patches/boot_time_opt_guest/0154-sysctl-vm-Fine-grained-cache-shrinking.patch137
1 files changed, 137 insertions, 0 deletions
diff --git a/patches/boot_time_opt_guest/0154-sysctl-vm-Fine-grained-cache-shrinking.patch b/patches/boot_time_opt_guest/0154-sysctl-vm-Fine-grained-cache-shrinking.patch
new file mode 100644
index 0000000..07d4a83
--- /dev/null
+++ b/patches/boot_time_opt_guest/0154-sysctl-vm-Fine-grained-cache-shrinking.patch
@@ -0,0 +1,137 @@
1From 2c145b5233b504f5226a0f4bc44baeef33b444d8 Mon Sep 17 00:00:00 2001
2From: Sebastien Boeuf <sebastien.boeuf@intel.com>
3Date: Mon, 23 Jan 2017 15:32:39 -0800
4Subject: [PATCH 154/154] sysctl: vm: Fine-grained cache shrinking
5
6Lots of virtual machines are let in idle state for days until they
7are terminated, and they can keep a large amount of memory in their
8cache, meaning this memory cannot be used by other processes.
9
10We tried to release this memory using existing drop_caches sysctl,
11but it led to the complete cache loss while it could have been used
12whether the idle process wakes up. Indeed, the process can't find any
13available cached data and it directly affects performances to rebuild
14it from scratch.
15
16Instead, the solution we want is based on shrinking gradually system
17cache over time. This patch adds a new sysctl shrink_caches_mb so as
18to allow userspace applications indicating the kernel it should shrink
19system cache up to the amount (in MiB) specified.
20
21There is an application called "memshrinker" which uses this new
22mechanism. It runs in the background and periodically releases a
23specified amount of cache. This amount is based on the remaining
24cache on the system, and period is computed to follow a shrinking
25model. It results in saving a lot of memory for other processes
26running on the system.
27
28Suggested-by: Arjan van de Ven <arjan.van.de.ven@intel.com>
29Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
30---
31 fs/drop_caches.c | 25 +++++++++++++++++++++++++
32 include/linux/mm.h | 4 ++++
33 kernel/sysctl.c | 8 ++++++++
34 mm/vmscan.c | 2 --
35 4 files changed, 37 insertions(+), 2 deletions(-)
36
37diff --git a/fs/drop_caches.c b/fs/drop_caches.c
38index d72d52b90433..f564dfcc13a4 100644
39--- a/fs/drop_caches.c
40+++ b/fs/drop_caches.c
41@@ -8,10 +8,12 @@
42 #include <linux/writeback.h>
43 #include <linux/sysctl.h>
44 #include <linux/gfp.h>
45+#include <linux/swap.h>
46 #include "internal.h"
47
48 /* A global variable is a bit ugly, but it keeps the code simple */
49 int sysctl_drop_caches;
50+int sysctl_shrink_caches_mb;
51
52 static void drop_pagecache_sb(struct super_block *sb, void *unused)
53 {
54@@ -67,3 +69,26 @@ int drop_caches_sysctl_handler(struct ctl_table *table, int write,
55 }
56 return 0;
57 }
58+
59+int shrink_caches_sysctl_handler(struct ctl_table *table, int write,
60+ void __user *buffer, size_t *length, loff_t *ppos)
61+{
62+ int ret;
63+ unsigned long nr_to_reclaim, page_reclaimed;
64+
65+ ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
66+ if (ret)
67+ return ret;
68+
69+ nr_to_reclaim = sysctl_shrink_caches_mb * (1 << 20) / PAGE_SIZE;
70+ if (write) {
71+ page_reclaimed = shrink_all_memory(nr_to_reclaim);
72+ if (page_reclaimed > 0)
73+ lru_add_drain_all();
74+
75+ if (page_reclaimed != nr_to_reclaim)
76+ return page_reclaimed;
77+ }
78+
79+ return 0;
80+}
81diff --git a/include/linux/mm.h b/include/linux/mm.h
82index 833f23d98baa..0bb66c1c31c9 100644
83--- a/include/linux/mm.h
84+++ b/include/linux/mm.h
85@@ -2308,6 +2308,10 @@ extern int kvm_ret_mem_advice;
86 int kvm_madv_instant_free_sysctl_handler(struct ctl_table *table, int write,
87 void __user *buffer, size_t *length,
88 loff_t *ppos);
89+extern int sysctl_shrink_caches_mb;
90+int shrink_caches_sysctl_handler(struct ctl_table *table, int write,
91+ void __user *buffer, size_t *length,
92+ loff_t *ppos);
93 #endif
94
95 void drop_slab(void);
96diff --git a/kernel/sysctl.c b/kernel/sysctl.c
97index d8ae774fa042..5dc9a46ae212 100644
98--- a/kernel/sysctl.c
99+++ b/kernel/sysctl.c
100@@ -1405,6 +1405,14 @@ static struct ctl_table vm_table[] = {
101 .mode = 0644,
102 .proc_handler = kvm_madv_instant_free_sysctl_handler,
103 },
104+ {
105+ .procname = "shrink_caches_mb",
106+ .data = &sysctl_shrink_caches_mb,
107+ .maxlen = sizeof(int),
108+ .mode = 0644,
109+ .proc_handler = shrink_caches_sysctl_handler,
110+ .extra1 = &one,
111+ },
112 #ifdef CONFIG_COMPACTION
113 {
114 .procname = "compact_memory",
115diff --git a/mm/vmscan.c b/mm/vmscan.c
116index 30a88b945a44..1198e74d1860 100644
117--- a/mm/vmscan.c
118+++ b/mm/vmscan.c
119@@ -3525,7 +3525,6 @@ void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx)
120 wake_up_interruptible(&pgdat->kswapd_wait);
121 }
122
123-#ifdef CONFIG_HIBERNATION
124 /*
125 * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
126 * freed pages.
127@@ -3564,7 +3563,6 @@ unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
128
129 return nr_reclaimed;
130 }
131-#endif /* CONFIG_HIBERNATION */
132
133 /* It's optimal to keep kswapds on the same CPUs as their memory, but
134 not required for correctness. So if the last cpu in a node goes
135--
1362.12.1
137