Skip to contentSkip to conversation

memcg: add interface to force disable swap

PATCH In review Jianlin Lv 3 participants 7 messages Oct 7, 2023 – Oct 9, 2023

Subsystems (7)
lore
Share & tools
lore

Jianlin Lv

author
#0 lore raw

PATCH 1/1 memcg: add interface to force disable swap

From: Jianlin Lv <[email protected]>

Global reclaim will swap even if swappiness is set to 0. In particular case, users wish to be able to completely disable swap for specific processes. One scenario is that if JVM memory pages falls into swap, the performance will noticeably reduce and the GC pauses tend to increase to levels not tolerable by most applications.
If it's possible to only disable swap out for specific processes, it can address the JVM GC pauses issues, and at the same time, memory reclaim pressure is also manageable.

This patch adds "memory.swap_force_disable" control file to support disable swap for non-root cgroup. When process is associated with a cgroup, 'echo 1 > memory.swap_force_disable' will forbid anon pages be swapped out. This patch also adds read and write handler of the control file.

---
 .../admin-guide/cgroup-v1/memory.rst          | 15 ++++++++++
 include/linux/memcontrol.h                    |  1 +
 include/linux/swap.h                          | 15 ++++++++++
 mm/memcontrol.c                               | 28 +++++++++++++++++++
 mm/vmscan.c                                   |  3 +-
 5 files changed, 61 insertions(+), 1 deletion(-)
Documentation/admin-guide/cgroup-v1/memory.rst +15 -0
index ff456871bf4b..be84b98bc6fe 100644
@@ -86,6 +86,7 @@ Brief summary of control files.
  memory.pressure_level		     set memory pressure notifications
  memory.swappiness		     set/show swappiness parameter of vmscan
 				     (See sysctl's vm.swappiness)
+ memory.swap_force_disable           set/show force disable swap
  memory.move_charge_at_immigrate     set/show controls of moving charges
                                      This knob is deprecated and shouldn't be
                                      used.
@@ -615,6 +616,20 @@ enforces that 0 swappiness really prevents from any swapping even if
 there is a swap storage available. This might lead to memcg OOM killer
 if there are no file pages to reclaim.
 
+swap_force_disable is used to allow control group to disable swap even if swap
+storage is available. This feature is disabled by default. If you want to
+disable swap for specified processes, swap_force_disable can be setup by
+following commands::
+
+	# cd /sys/fs/cgroup/memory/
+	# mkdir test
+	# cd test
+	# echo 1 > memory.swap_force_disable
+	# echo <PID> > cgroup.procs
+
+.. note::
+	swap_force_disable only take effect for non-root cgroups.
+
 5.4 failcnt
 -----------
 
include/linux/memcontrol.h +1 -0
index e4e24da16d2c..b26dcb0756c0 100644
@@ -246,6 +246,7 @@ struct mem_cgroup {
 	int		under_oom;
 
 	int	swappiness;
+	int	swap_force_disable;
 	/* OOM-Killer disable */
 	int		oom_kill_disable;
 
include/linux/swap.h +15 -0
index 493487ed7c38..b202de576984 100644
@@ -624,6 +624,21 @@ static inline int mem_cgroup_swappiness(struct mem_cgroup *mem)
 }
 #endif
 
+#ifdef CONFIG_MEMCG
+static inline int mem_cgroup_swap_force_disable(struct mem_cgroup *memcg)
+{
+	if (mem_cgroup_disabled() || mem_cgroup_is_root(memcg))
+		return 0;
+
+	return memcg->swap_force_disable;
+}
+#else
+static inline int mem_cgroup_swap_force_disable(struct mem_cgroup *memcg)
+{
+	return 0;
+}
+#endif
+
 #if defined(CONFIG_SWAP) && defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
 void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp);
 static inline void folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
mm/memcontrol.c +28 -0
index 5b009b233ab8..024750444c79 100644
@@ -4196,6 +4196,28 @@ static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
 	return 0;
 }
 
+static u64 mem_cgroup_swap_force_disable_read(struct cgroup_subsys_state *css,
+					struct cftype *cft)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
+
+	return mem_cgroup_swap_force_disable(memcg);
+}
+
+static int mem_cgroup_swap_force_disable_write(struct cgroup_subsys_state *css,
+					struct cftype *cft, u64 val)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
+
+	/* cannot set to root cgroup and only 0 and 1 are allowed */
+	if (mem_cgroup_is_root(memcg) || !((val == 0) || (val == 1)))
+		return -EINVAL;
+
+	memcg->swap_force_disable = val;
+
+	return 0;
+}
+
 static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
 {
 	struct mem_cgroup_threshold_ary *t;
@@ -5064,6 +5086,11 @@ static struct cftype mem_cgroup_legacy_files[] = {
 		.read_u64 = mem_cgroup_swappiness_read,
 		.write_u64 = mem_cgroup_swappiness_write,
 	},
+	{
+		.name = "swap_force_disable",
+		.read_u64 = mem_cgroup_swap_force_disable_read,
+		.write_u64 = mem_cgroup_swap_force_disable_write,
+	},
 	{
 		.name = "move_charge_at_immigrate",
 		.read_u64 = mem_cgroup_move_charge_read,
@@ -5367,6 +5394,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
 	page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
 	if (parent) {
 		WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent));
+		WRITE_ONCE(memcg->swap_force_disable, mem_cgroup_swap_force_disable(parent));
 		WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable));
 
 		page_counter_init(&memcg->memory, &parent->memory);
mm/vmscan.c +2 -1
index 6f13394b112e..5fdb4ac07007 100644
@@ -3029,6 +3029,7 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
 	unsigned long anon_cost, file_cost, total_cost;
 	int swappiness = mem_cgroup_swappiness(memcg);
+	int swap_force_disable = mem_cgroup_swap_force_disable(memcg);
 	u64 fraction[ANON_AND_FILE];
 	u64 denominator = 0;	/* gcc */
 	enum scan_balance scan_balance;
@@ -3036,7 +3037,7 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
 	enum lru_list lru;
 
 	/* If we have no swap space, do not bother scanning anon folios. */
-	if (!sc->may_swap || !can_reclaim_anon_pages(memcg, pgdat->node_id, sc)) {
+	if (!sc->may_swap || !can_reclaim_anon_pages(memcg, pgdat->node_id, sc) || swap_force_disable) {
 		scan_balance = SCAN_FILE;
 		goto out;
 	}
Signature · 1 line
2.34.1
#1 lore raw
Quoted 3 lines · Jianlin Lv

From: Jianlin Lv <[email protected]>

Global reclaim will swap even if swappiness is set to 0.

Why? Can you elaborate the situation?

Quoted 9 lines

In particular
case, users wish to be able to completely disable swap for specific processes. One scenario is that if JVM memory pages falls into swap, the performance will noticeably reduce and the GC pauses tend to increase to levels not tolerable by most applications.
If it's possible to only disable swap out for specific processes, it can address the JVM GC pauses issues, and at the same time, memory reclaim pressure is also manageable.

Quoted 4 lines

This patch adds "memory.swap_force_disable" control file to support disable swap for non-root cgroup. When process is associated with a cgroup, 'echo 1 > memory.swap_force_disable' will forbid anon pages be swapped out. This patch also adds read and write handler of the control file.

--
Best Regards,
Huang, Ying

author
#2 lore raw
Quoted 8 lines · Ying
Quoted 3 lines · Jianlin Lv

From: Jianlin Lv <[email protected]>

Global reclaim will swap even if swappiness is set to 0.

Why? Can you elaborate the situation?

We reproduced the issue of pages being swapped out even when swappiness is set to 0 in the production environment through the following test program. Not sure whether this program can reproduce the issue in any environment.

From the implementation of the get_scan_count code, it can be seen that, based on the current runtime situation, memory reclamation will choose a scanning method (SCAN_ANON/SCAN_FILE/SCAN_FRACT) to determine how aggressively the anon and file LRU are scanned. However, this introduces uncertainty.

For the JVM issue at hand, we expect deterministic SCAN_FILE scan to avoid swapping out anon pages.

code::
#!/usr/bin/env python

import mmap
import os
import sys

def write_files():
count = 1
if not os.path.isdir(WRITE_DIR):
os.mkdir(WRITE_DIR)

while True:
_, i = divmod(count, 6000)
file = "{}/{}_{}.txt".format(WRITE_DIR, WRITE_FILE, i)

with open(file, 'w') as f:
# Write 100 MB to a file
num_chars = 100 * 1024 * 1024
f.write('0' * num_chars)
count = count + 1

def create_read_file():

    with open(READ_FILE, 'wb') as f:
        num_chars = 10000 * 1024 * 1024
        f.write(b'0' * num_chars)

def read_file():
with open(READ_FILE, mode="r", encoding="utf8") as f:
mm = mmap.mmap(f.fileno(), length=0, access=mmap.ACCESS_READ) text = mm.read()
write_files()

WRITE_FILE = "file"
WRITE_DIR = "/tmp/rm_rf_me"
READ_FILE="/tmp/10g_file_delete"
if not os.path.isfile(READ_FILE):
create_read_file()
read_file()

Jianlin

Quoted 18 lines
Quoted 10 lines

In particular
case, users wish to be able to completely disable swap for specific processes. One scenario is that if JVM memory pages falls into swap, the performance will noticeably reduce and the GC pauses tend to increase to levels not tolerable by most applications.
If it's possible to only disable swap out for specific processes, it can address the JVM GC pauses issues, and at the same time, memory reclaim pressure is also manageable.

Quoted 4 lines

This patch adds "memory.swap_force_disable" control file to support disable swap for non-root cgroup. When process is associated with a cgroup, 'echo 1 > memory.swap_force_disable' will forbid anon pages be swapped out. This patch also adds read and write handler of the control file.

--
Best Regards,
Huang, Ying

#3 lore raw
Quoted 14 lines · Jianlin Lv
Quoted 8 lines · Ying
Quoted 3 lines · Jianlin Lv

From: Jianlin Lv <[email protected]>

Global reclaim will swap even if swappiness is set to 0.

Why? Can you elaborate the situation?

We reproduced the issue of pages being swapped out even when swappiness is set to 0 in the production environment through the following test program. Not sure whether this program can reproduce the issue in any environment.

Quoted 8 lines

From the implementation of the get_scan_count code, it can be seen that, based on the current runtime situation, memory reclamation will choose a scanning method (SCAN_ANON/SCAN_FILE/SCAN_FRACT) to determine how aggressively the anon and file LRU are scanned. However, this introduces uncertainty.

For the JVM issue at hand, we expect deterministic SCAN_FILE scan to avoid swapping out anon pages.

Why doesn't memory.swap.max work?

--
Best Regards,
Huang, Ying

author
#4 lore raw
Quoted 23 lines · Ying
Quoted 20 lines · Jianlin Lv
Quoted 8 lines · Ying
Quoted 3 lines · Jianlin Lv

From: Jianlin Lv <[email protected]>

Global reclaim will swap even if swappiness is set to 0.

Why? Can you elaborate the situation?

We reproduced the issue of pages being swapped out even when swappiness is set to 0 in the production environment through the following test program. Not sure whether this program can reproduce the issue in any environment.

From the implementation of the get_scan_count code, it can be seen that, based on the current runtime situation, memory reclamation will choose a scanning method (SCAN_ANON/SCAN_FILE/SCAN_FRACT) to determine how aggressively the anon and file LRU are scanned. However, this introduces uncertainty.

Quoted 4 lines
Quoted 2 lines

For the JVM issue at hand, we expect deterministic SCAN_FILE scan to avoid swapping out anon pages.

Why doesn't memory.swap.max work?

The main reason is that deployed nodes are kept on cgroups v1.

Quoted 4 lines

--
Best Regards,
Huang, Ying

#5 lore raw
Quoted 24 lines · Jianlin Lv
Quoted 23 lines · Ying
Quoted 20 lines · Jianlin Lv
Quoted 8 lines · Ying
Quoted 3 lines · Jianlin Lv

From: Jianlin Lv <[email protected]>

Global reclaim will swap even if swappiness is set to 0.

Why? Can you elaborate the situation?

We reproduced the issue of pages being swapped out even when swappiness is set to 0 in the production environment through the following test program. Not sure whether this program can reproduce the issue in any environment.

From the implementation of the get_scan_count code, it can be seen that, based on the current runtime situation, memory reclamation will choose a scanning method (SCAN_ANON/SCAN_FILE/SCAN_FRACT) to determine how aggressively the anon and file LRU are scanned. However, this introduces uncertainty.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

For the JVM issue at hand, we expect deterministic SCAN_FILE scan to avoid swapping out anon pages.

Why doesn't memory.swap.max work?

The main reason is that deployed nodes are kept on cgroups v1.

Check the code again. IIUC, for swappiness == 0, anonymous pages will only be reclaimed if sc->file_is_tiny is true. If we don't swap in that situation, OOM may be triggerred. I don't think that it's a good idea to do that. Or I miss something?

--
Best Regards,
Huang, Ying

#6 lore raw
Quoted 26 lines · Ying
Quoted 24 lines · Jianlin Lv
Quoted 23 lines · Ying
Quoted 20 lines · Jianlin Lv
Quoted 8 lines · Ying
Quoted 3 lines · Jianlin Lv

From: Jianlin Lv <[email protected]>

Global reclaim will swap even if swappiness is set to 0.

Why? Can you elaborate the situation?

We reproduced the issue of pages being swapped out even when swappiness is set to 0 in the production environment through the following test program. Not sure whether this program can reproduce the issue in any environment.

From the implementation of the get_scan_count code, it can be seen that, based on the current runtime situation, memory reclamation will choose a scanning method (SCAN_ANON/SCAN_FILE/SCAN_FRACT) to determine how aggressively the anon and file LRU are scanned. However, this introduces uncertainty.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

For the JVM issue at hand, we expect deterministic SCAN_FILE scan to avoid swapping out anon pages.

Why doesn't memory.swap.max work?

The main reason is that deployed nodes are kept on cgroups v1.

Please note that cgroups v1 is in the maintenance mode with no new functionality to be added. What is the reason you are sticking with v1?

Quoted 2 lines

Check the code again. IIUC, for swappiness == 0, anonymous pages will only be reclaimed if sc->file_is_tiny is true.

For the memcg reclaim (i.e. not the global one) we try to avoid swapping even when file_is_tiny IIRC.

Quoted 3 lines

If we don't swap in that
situation, OOM may be triggerred. I don't think that it's a good idea to do that. Or I miss something?

Or even worse the system might start trashing heavily over that remaining tiny page cache.

Signature · 2 lines
Michal Hocko
SUSE Labs