mm: Patch le9ec
Protect the working set under memory pressure to prevent thrashing, avoid high latency and prevent livelock in near-OOM conditions The kernel does not provide a way to protect the working set under memory pressure. A certain amount of anonymous and clean file pages is required by the userspace for normal operation. First of all, the userspace needs a cache of shared libraries and executable binaries. If the amount of the clean file pages falls below a certain level, then thrashing and even livelock can take place. The patch provides sysctl knobs for protecting the working set (anonymous and clean file pages) under memory pressure. The vm.anon_min_kbytes sysctl knob provides hard protection of anonymous pages. The anonymous pages on the current node won't be reclaimed under any conditions when their amount is below vm.anon_min_kbytes. This knob may be used to prevent excessive swap thrashing when anonymous memory is low (for example, when memory is going to be overfilled by compressed data of zram module). The vm.clean_low_kbytes sysctl knob provides best-effort protection of clean file pages. The file pages on the current node won't be reclaimed under memory pressure when the amount of clean file pages is below vm.clean_low_kbytes unless we threaten to OOM. Protection of clean file pages using this knob may be used when swapping is still possible to - Prevent disk I/O thrashing under memory pressure. - Improve performance in disk cache-bound tasks under memory pressure. The vm.clean_min_kbytes sysctl knob provides hard protection of clean file pages. The file pages on the current node won't be reclaimed under memory pressure when the amount of clean file pages is below vm.clean_min_kbytes. Hard protection of clean file pages using this knob may be used to - Prevent disk I/O thrashing under memory pressure even with no free swap space. - Improve performance in disk cache-bound tasks under memory pressure. - Avoid high latency and prevent livelock in near-OOM conditions. le9ec patches provide three sysctl knobs (vm.anon_min_kbytes, vm.clean_low_kbytes, vm.clean_min_kbytes) with zero values and does not protect the working set by default (CONFIG_ANON_MIN_KBYTES=0, CONFIG_CLEAN_LOW_KBYTES=0, CONFIG_CLEAN_MIN_KBYTES=0). You can specify other values during kernel build, or change the knob values on the fly. Effects - Improving system responsiveness under low-memory conditions. - Improving performance in I/O bound tasks under memory pressure; - OOM killer comes faster (with hard protection). - Fast system reclaiming after OOM (with hard protection). Note that the effects depend on the values of the sysctl tunables. source patch: https://github.com/hakavlad/le9-patch Signed-off-by: kanonifyX <kanonify01@gmail.com>
This commit is contained in:
@@ -19,7 +19,10 @@ files can be found in mm/swap.c.
|
||||
Currently, these files are in /proc/sys/vm:
|
||||
|
||||
- admin_reserve_kbytes
|
||||
- anon_min_kbytes
|
||||
- block_dump
|
||||
- clean_low_kbytes
|
||||
- clean_min_kbytes
|
||||
- compact_memory
|
||||
- compact_unevictable_allowed
|
||||
- dirty_background_bytes
|
||||
@@ -97,6 +100,23 @@ Changing this takes effect whenever an application requests memory.
|
||||
|
||||
==============================================================
|
||||
|
||||
anon_min_kbytes
|
||||
|
||||
This knob provides *hard* protection of anonymous pages. The anonymous pages
|
||||
on the current node won't be reclaimed under any conditions when their amount
|
||||
is below vm.anon_min_kbytes.
|
||||
|
||||
This knob may be used to prevent excessive swap thrashing when anonymous
|
||||
memory is low (for example, when memory is going to be overfilled by
|
||||
compressed data of zram module).
|
||||
|
||||
Setting this value too high (close to MemTotal) can result in inability to
|
||||
swap and can lead to early OOM under memory pressure.
|
||||
|
||||
The default value is defined by CONFIG_ANON_MIN_KBYTES.
|
||||
|
||||
==============================================================
|
||||
|
||||
block_dump
|
||||
|
||||
block_dump enables block I/O debugging when set to a nonzero value. More
|
||||
@@ -104,6 +124,44 @@ information on block I/O debugging is in Documentation/laptops/laptop-mode.txt.
|
||||
|
||||
==============================================================
|
||||
|
||||
clean_low_kbytes
|
||||
|
||||
This knob provides *best-effort* protection of clean file pages. The file pages
|
||||
on the current node won't be reclaimed under memory pressure when the amount of
|
||||
clean file pages is below vm.clean_low_kbytes *unless* we threaten to OOM.
|
||||
|
||||
Protection of clean file pages using this knob may be used when swapping is
|
||||
still possible to
|
||||
- prevent disk I/O thrashing under memory pressure;
|
||||
- improve performance in disk cache-bound tasks under memory pressure.
|
||||
|
||||
Setting it to a high value may result in a early eviction of anonymous pages
|
||||
into the swap space by attempting to hold the protected amount of clean file
|
||||
pages in memory.
|
||||
|
||||
The default value is defined by CONFIG_CLEAN_LOW_KBYTES.
|
||||
|
||||
==============================================================
|
||||
|
||||
clean_min_kbytes
|
||||
|
||||
This knob provides *hard* protection of clean file pages. The file pages on the
|
||||
current node won't be reclaimed under memory pressure when the amount of clean
|
||||
file pages is below vm.clean_min_kbytes.
|
||||
|
||||
Hard protection of clean file pages using this knob may be used to
|
||||
- prevent disk I/O thrashing under memory pressure even with no free swap space;
|
||||
- improve performance in disk cache-bound tasks under memory pressure;
|
||||
- avoid high latency and prevent livelock in near-OOM conditions.
|
||||
|
||||
Setting it to a high value may result in a early out-of-memory condition due to
|
||||
the inability to reclaim the protected amount of clean file pages when other
|
||||
types of pages cannot be reclaimed.
|
||||
|
||||
The default value is defined by CONFIG_CLEAN_MIN_KBYTES.
|
||||
|
||||
==============================================================
|
||||
|
||||
compact_memory
|
||||
|
||||
Available only when CONFIG_COMPACTION is set. When 1 is written to the file,
|
||||
@@ -817,6 +875,14 @@ decrease the amount of swap. A value of 0 instructs the kernel not to
|
||||
initiate swap until the amount of free and file-backed pages is less
|
||||
than the high water mark in a zone.
|
||||
|
||||
This knob has no effect if the amount of clean file pages on the current
|
||||
node is below vm.clean_low_kbytes or vm.clean_min_kbytes. In this case,
|
||||
only anonymous pages can be reclaimed.
|
||||
|
||||
If the number of anonymous pages on the current node is below
|
||||
vm.anon_min_kbytes, then only file pages can be reclaimed with
|
||||
any vm.swappiness value.
|
||||
|
||||
The default value is 60.
|
||||
|
||||
==============================================================
|
||||
|
||||
@@ -128,6 +128,10 @@ extern int mmap_rnd_compat_bits __read_mostly;
|
||||
|
||||
extern int sysctl_max_map_count;
|
||||
|
||||
extern unsigned long sysctl_anon_min_kbytes;
|
||||
extern unsigned long sysctl_clean_low_kbytes;
|
||||
extern unsigned long sysctl_clean_min_kbytes;
|
||||
|
||||
extern unsigned long sysctl_user_reserve_kbytes;
|
||||
extern unsigned long sysctl_admin_reserve_kbytes;
|
||||
|
||||
|
||||
@@ -1816,6 +1816,27 @@ static struct ctl_table vm_table[] = {
|
||||
.extra2 = &one,
|
||||
},
|
||||
#endif
|
||||
{
|
||||
.procname = "anon_min_kbytes",
|
||||
.data = &sysctl_anon_min_kbytes,
|
||||
.maxlen = sizeof(unsigned long),
|
||||
.mode = 0644,
|
||||
.proc_handler = proc_doulongvec_minmax,
|
||||
},
|
||||
{
|
||||
.procname = "clean_low_kbytes",
|
||||
.data = &sysctl_clean_low_kbytes,
|
||||
.maxlen = sizeof(unsigned long),
|
||||
.mode = 0644,
|
||||
.proc_handler = proc_doulongvec_minmax,
|
||||
},
|
||||
{
|
||||
.procname = "clean_min_kbytes",
|
||||
.data = &sysctl_clean_min_kbytes,
|
||||
.maxlen = sizeof(unsigned long),
|
||||
.mode = 0644,
|
||||
.proc_handler = proc_doulongvec_minmax,
|
||||
},
|
||||
{
|
||||
.procname = "user_reserve_kbytes",
|
||||
.data = &sysctl_user_reserve_kbytes,
|
||||
|
||||
63
mm/Kconfig
63
mm/Kconfig
@@ -128,6 +128,69 @@ config SPARSEMEM_VMEMMAP
|
||||
pfn_to_page and page_to_pfn operations. This is the most
|
||||
efficient option when sufficient kernel resources are available.
|
||||
|
||||
config ANON_MIN_KBYTES
|
||||
int "Default value for vm.anon_min_kbytes"
|
||||
depends on SYSCTL
|
||||
range 0 4294967295
|
||||
default 0
|
||||
help
|
||||
This option sets the default value for vm.anon_min_kbytes sysctl knob.
|
||||
|
||||
The vm.anon_min_kbytes sysctl knob provides *hard* protection of
|
||||
anonymous pages. The anonymous pages on the current node won't be
|
||||
reclaimed under any conditions when their amount is below
|
||||
vm.anon_min_kbytes. This knob may be used to prevent excessive swap
|
||||
thrashing when anonymous memory is low (for example, when memory is
|
||||
going to be overfilled by compressed data of zram module).
|
||||
|
||||
Setting this value too high (close to MemTotal) can result in
|
||||
inability to swap and can lead to early OOM under memory pressure.
|
||||
|
||||
config CLEAN_LOW_KBYTES
|
||||
int "Default value for vm.clean_low_kbytes"
|
||||
depends on SYSCTL
|
||||
range 0 4294967295
|
||||
default 0
|
||||
help
|
||||
This option sets the default value for vm.clean_low_kbytes sysctl knob.
|
||||
|
||||
The vm.clean_low_kbytes sysctl knob provides *best-effort*
|
||||
protection of clean file pages. The file pages on the current node
|
||||
won't be reclaimed under memory pressure when the amount of clean file
|
||||
pages is below vm.clean_low_kbytes *unless* we threaten to OOM.
|
||||
Protection of clean file pages using this knob may be used when
|
||||
swapping is still possible to
|
||||
- prevent disk I/O thrashing under memory pressure;
|
||||
- improve performance in disk cache-bound tasks under memory
|
||||
pressure.
|
||||
|
||||
Setting it to a high value may result in a early eviction of anonymous
|
||||
pages into the swap space by attempting to hold the protected amount
|
||||
of clean file pages in memory.
|
||||
|
||||
config CLEAN_MIN_KBYTES
|
||||
int "Default value for vm.clean_min_kbytes"
|
||||
depends on SYSCTL
|
||||
range 0 4294967295
|
||||
default 0
|
||||
help
|
||||
This option sets the default value for vm.clean_min_kbytes sysctl knob.
|
||||
|
||||
The vm.clean_min_kbytes sysctl knob provides *hard* protection of
|
||||
clean file pages. The file pages on the current node won't be
|
||||
reclaimed under memory pressure when the amount of clean file pages is
|
||||
below vm.clean_min_kbytes. Hard protection of clean file pages using
|
||||
this knob may be used to
|
||||
- prevent disk I/O thrashing under memory pressure even with no free
|
||||
swap space;
|
||||
- improve performance in disk cache-bound tasks under memory
|
||||
pressure;
|
||||
- avoid high latency and prevent livelock in near-OOM conditions.
|
||||
|
||||
Setting it to a high value may result in a early out-of-memory condition
|
||||
due to the inability to reclaim the protected amount of clean file pages
|
||||
when other types of pages cannot be reclaimed.
|
||||
|
||||
config HAVE_MEMBLOCK
|
||||
bool
|
||||
|
||||
|
||||
91
mm/vmscan.c
91
mm/vmscan.c
@@ -73,6 +73,15 @@ struct scan_control {
|
||||
/* This context's GFP mask */
|
||||
gfp_t gfp_mask;
|
||||
|
||||
/* The anonymous pages on the current node are below vm.anon_min_kbytes */
|
||||
unsigned int anon_below_min:1;
|
||||
|
||||
/* The clean file pages on the current node are below vm.clean_low_kbytes */
|
||||
unsigned int clean_below_low:1;
|
||||
|
||||
/* The clean file pages on the current node are below vm.clean_min_kbytes */
|
||||
unsigned int clean_below_min:1;
|
||||
|
||||
/* Allocation order */
|
||||
int order;
|
||||
|
||||
@@ -159,6 +168,10 @@ struct scan_control {
|
||||
#define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
|
||||
#endif
|
||||
|
||||
unsigned long sysctl_anon_min_kbytes __read_mostly = CONFIG_ANON_MIN_KBYTES;
|
||||
unsigned long sysctl_clean_low_kbytes __read_mostly = CONFIG_CLEAN_LOW_KBYTES;
|
||||
unsigned long sysctl_clean_min_kbytes __read_mostly = CONFIG_CLEAN_MIN_KBYTES;
|
||||
|
||||
/*
|
||||
* From 0 .. 100. Higher means more swappy.
|
||||
*/
|
||||
@@ -2312,6 +2325,54 @@ static bool swap_is_allowed(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
static void prepare_workingset_protection(pg_data_t *pgdat, struct scan_control *sc)
|
||||
{
|
||||
/*
|
||||
* Check the number of anonymous pages to protect them from
|
||||
* reclaiming if their amount is below the specified.
|
||||
*/
|
||||
if (sysctl_anon_min_kbytes) {
|
||||
unsigned long reclaimable_anon;
|
||||
|
||||
reclaimable_anon =
|
||||
node_page_state(pgdat, NR_ACTIVE_ANON) +
|
||||
node_page_state(pgdat, NR_INACTIVE_ANON) +
|
||||
node_page_state(pgdat, NR_ISOLATED_ANON);
|
||||
reclaimable_anon <<= (PAGE_SHIFT - 10);
|
||||
|
||||
sc->anon_below_min = reclaimable_anon < sysctl_anon_min_kbytes;
|
||||
} else
|
||||
sc->anon_below_min = 0;
|
||||
|
||||
/*
|
||||
* Check the number of clean file pages to protect them from
|
||||
* reclaiming if their amount is below the specified.
|
||||
*/
|
||||
if (sysctl_clean_low_kbytes || sysctl_clean_min_kbytes) {
|
||||
unsigned long reclaimable_file, dirty, clean;
|
||||
|
||||
reclaimable_file =
|
||||
node_page_state(pgdat, NR_ACTIVE_FILE) +
|
||||
node_page_state(pgdat, NR_INACTIVE_FILE) +
|
||||
node_page_state(pgdat, NR_ISOLATED_FILE);
|
||||
dirty = node_page_state(pgdat, NR_FILE_DIRTY);
|
||||
/*
|
||||
* node_page_state() sum can go out of sync since
|
||||
* all the values are not read at once.
|
||||
*/
|
||||
if (likely(reclaimable_file > dirty))
|
||||
clean = (reclaimable_file - dirty) << (PAGE_SHIFT - 10);
|
||||
else
|
||||
clean = 0;
|
||||
|
||||
sc->clean_below_low = clean < sysctl_clean_low_kbytes;
|
||||
sc->clean_below_min = clean < sysctl_clean_min_kbytes;
|
||||
} else {
|
||||
sc->clean_below_low = 0;
|
||||
sc->clean_below_min = 0;
|
||||
}
|
||||
}
|
||||
|
||||
enum scan_balance {
|
||||
SCAN_EQUAL,
|
||||
SCAN_FRACT,
|
||||
@@ -2364,6 +2425,9 @@ static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg,
|
||||
}
|
||||
#endif
|
||||
|
||||
prepare_workingset_protection(pgdat, sc);
|
||||
|
||||
|
||||
/* If we have no swap space, do not bother scanning anon pages. */
|
||||
if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) {
|
||||
scan_balance = SCAN_FILE;
|
||||
@@ -2434,6 +2498,15 @@ static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Force-scan anon if clean file pages is under vm.clean_low_kbytes
|
||||
* or vm.clean_min_kbytes.
|
||||
*/
|
||||
if (sc->clean_below_low || sc->clean_below_min) {
|
||||
scan_balance = SCAN_ANON;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*
|
||||
* If there is enough inactive page cache, i.e. if the size of the
|
||||
* inactive list is greater than that of the active list *and* the
|
||||
@@ -2548,6 +2621,24 @@ out:
|
||||
BUG();
|
||||
}
|
||||
|
||||
/*
|
||||
* Hard protection of the working set.
|
||||
*/
|
||||
if (file) {
|
||||
/*
|
||||
* Don't reclaim file pages when the amount of
|
||||
* clean file pages is below vm.clean_min_kbytes.
|
||||
*/
|
||||
if (sc->clean_below_min)
|
||||
scan = 0;
|
||||
} else {
|
||||
/*
|
||||
* Don't reclaim anonymous pages when their
|
||||
* amount is below vm.anon_min_kbytes.
|
||||
*/
|
||||
if (sc->anon_below_min)
|
||||
scan = 0;
|
||||
}
|
||||
*lru_pages += size;
|
||||
nr[lru] = scan;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user