Skip to contentSkip to conversation

mm: vmscan: try to reclaim swapcache pages if no swap space

PATCH Superseded Liu Shixin 2 participants 3 messages Nov 4, 2023 – Nov 6, 2023

Subsystems (5)

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Liu Shixin

author
#0 lore raw

PATCH v7 1/1 mm: vmscan: try to reclaim swapcache pages if no swap space

When spaces of swap devices are exhausted, only file pages can be reclaimed. But there are still some swapcache pages in anon lru list. This can lead to a premature out-of-memory.

The problem is found with such step:

Firstly, set a 9MB disk swap space, then create a cgroup with 10MB memory limit, then runs an program to allocates about 15MB memory.

The problem occurs occasionally, which may need about 100 times [1].

Fix it by checking number of swapcache pages in can_reclaim_anon_pages(). If the number is not zero, return true and set swapcache_only to 1. When scan anon lru list in swapcache_only mode, non-swapcache pages will be skipped to isolate in order to accelerate reclaim efficiency.

However, in swapcache_only mode, the scan count still increased when scan non-swapcache pages because there are large number of non-swapcache pages and rare swapcache pages in swapcache_only mode, and if the non-swapcache is skipped and do not count, the scan of pages in isolate_lru_folios() can eventually lead to hung task, just as Sachin reported [2].

By the way, since there are enough times of memory reclaim before OOM, it is not need to isolate too much swapcache pages in one times.

[1]. https://lore.kernel.org/lkml/CAJD7tkZAfgncV+KbKr36=eDzMnT=9dZOT0dpMWcurHLr6Do+GA@mail.gmail.com/
[2]. https://lore.kernel.org/linux-mm/CAJD7tkafz_2XAuqE8tGLPEcpLngewhUo=5US14PAtSM9tLBUQg@mail.gmail.com/

---
v6->v7: Reset swapcache_only to zero after there are swap spaces. v5->v6: Fix NULL pointing derefence and hung task problem reported by Sachin.

 include/linux/swap.h |  6 ++++++
 mm/memcontrol.c      |  8 ++++++++
 mm/vmscan.c          | 36 ++++++++++++++++++++++++++++++++++--
 3 files changed, 48 insertions(+), 2 deletions(-)
include/linux/swap.h +6 -0
index f6dd6575b905..3ba146ae7cf5 100644
@@ -659,6 +659,7 @@ static inline void mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_p
 }
 
 extern long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg);
+extern long mem_cgroup_get_nr_swapcache_pages(struct mem_cgroup *memcg);
 extern bool mem_cgroup_swap_full(struct folio *folio);
 #else
 static inline void mem_cgroup_swapout(struct folio *folio, swp_entry_t entry)
@@ -681,6 +682,11 @@ static inline long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
 	return get_nr_swap_pages();
 }
 
+static inline long mem_cgroup_get_nr_swapcache_pages(struct mem_cgroup *memcg)
+{
+	return total_swapcache_pages();
+}
+
 static inline bool mem_cgroup_swap_full(struct folio *folio)
 {
 	return vm_swap_full();
mm/memcontrol.c +8 -0
index 5b009b233ab8..29e34c06ca83 100644
@@ -7584,6 +7584,14 @@ long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
 	return nr_swap_pages;
 }
 
+long mem_cgroup_get_nr_swapcache_pages(struct mem_cgroup *memcg)
+{
+	if (mem_cgroup_disabled())
+		return total_swapcache_pages();
+
+	return memcg_page_state(memcg, NR_SWAPCACHE);
+}
+
 bool mem_cgroup_swap_full(struct folio *folio)
 {
 	struct mem_cgroup *memcg;
mm/vmscan.c +34 -2
index 6f13394b112e..a5e04291662f 100644
@@ -137,6 +137,9 @@ struct scan_control {
 	/* Always discard instead of demoting to lower tier memory */
 	unsigned int no_demotion:1;
 
+	/* Swap space is exhausted, only reclaim swapcache for anon LRU */
+	unsigned int swapcache_only:1;
+
 	/* Allocation order */
 	s8 order;
 
@@ -602,6 +605,12 @@ static bool can_demote(int nid, struct scan_control *sc)
 	return true;
 }
 
+static void set_swapcache_mode(struct scan_control *sc, bool swapcache_only)
+{
+	if (sc)
+		sc->swapcache_only = swapcache_only;
+}
+
 static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
 					  int nid,
 					  struct scan_control *sc)
@@ -611,12 +620,26 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
 		 * For non-memcg reclaim, is there
 		 * space in any swap device?
 		 */
-		if (get_nr_swap_pages() > 0)
+		if (get_nr_swap_pages() > 0) {
+			set_swapcache_mode(sc, false);
 			return true;
+		}
+		/* Is there any swapcache pages to reclaim? */
+		if (total_swapcache_pages() > 0) {
+			set_swapcache_mode(sc, true);
+			return true;
+		}
 	} else {
 		/* Is the memcg below its swap limit? */
-		if (mem_cgroup_get_nr_swap_pages(memcg) > 0)
+		if (mem_cgroup_get_nr_swap_pages(memcg) > 0) {
+			set_swapcache_mode(sc, false);
 			return true;
+		}
+		/* Is there any swapcache pages in memcg to reclaim? */
+		if (mem_cgroup_get_nr_swapcache_pages(memcg) > 0) {
+			set_swapcache_mode(sc, true);
+			return true;
+		}
 	}
 
 	/*
@@ -2342,6 +2365,15 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan,
 		 */
 		scan += nr_pages;
 
+		/*
+		 * Count non-swapcache too because the swapcache pages may
+		 * be rare and it takes too much times here if not count
+		 * the non-swapcache pages.
+		 */
+		if (unlikely(sc->swapcache_only && !is_file_lru(lru) &&
+		    !folio_test_swapcache(folio)))
+			goto move;
+
 		if (!folio_test_lru(folio))
 			goto move;
 		if (!sc->may_unmap && folio_mapped(folio))
Signature · 1 line
2.25.1
#1 lore raw
Quoted 99 lines · Liu Shixin

When spaces of swap devices are exhausted, only file pages can be reclaimed. But there are still some swapcache pages in anon lru list. This can lead to a premature out-of-memory.

The problem is found with such step:

Firstly, set a 9MB disk swap space, then create a cgroup with 10MB memory limit, then runs an program to allocates about 15MB memory.

The problem occurs occasionally, which may need about 100 times [1].

Fix it by checking number of swapcache pages in can_reclaim_anon_pages(). If the number is not zero, return true and set swapcache_only to 1. When scan anon lru list in swapcache_only mode, non-swapcache pages will be skipped to isolate in order to accelerate reclaim efficiency.

However, in swapcache_only mode, the scan count still increased when scan non-swapcache pages because there are large number of non-swapcache pages and rare swapcache pages in swapcache_only mode, and if the non-swapcache is skipped and do not count, the scan of pages in isolate_lru_folios() can eventually lead to hung task, just as Sachin reported [2].

By the way, since there are enough times of memory reclaim before OOM, it is not need to isolate too much swapcache pages in one times.

[1]. https://lore.kernel.org/lkml/CAJD7tkZAfgncV+KbKr36=eDzMnT=9dZOT0dpMWcurHLr6Do+GA@mail.gmail.com/
[2]. https://lore.kernel.org/linux-mm/CAJD7tkafz_2XAuqE8tGLPEcpLngewhUo=5US14PAtSM9tLBUQg@mail.gmail.com/

---
v6->v7: Reset swapcache_only to zero after there are swap spaces. v5->v6: Fix NULL pointing derefence and hung task problem reported by Sachin.

 include/linux/swap.h |  6 ++++++
 mm/memcontrol.c      |  8 ++++++++
 mm/vmscan.c          | 36 ++++++++++++++++++++++++++++++++++--
 3 files changed, 48 insertions(+), 2 deletions(-)
include/linux/swap.h +6 -0
index f6dd6575b905..3ba146ae7cf5 100644
@@ -659,6 +659,7 @@ static inline void mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_p
 }
 
 extern long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg);
+extern long mem_cgroup_get_nr_swapcache_pages(struct mem_cgroup *memcg);
 extern bool mem_cgroup_swap_full(struct folio *folio);
 #else
 static inline void mem_cgroup_swapout(struct folio *folio, swp_entry_t entry)
@@ -681,6 +682,11 @@ static inline long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
 	return get_nr_swap_pages();
 }
 
+static inline long mem_cgroup_get_nr_swapcache_pages(struct mem_cgroup *memcg)
+{
+	return total_swapcache_pages();
+}
+
 static inline bool mem_cgroup_swap_full(struct folio *folio)
 {
 	return vm_swap_full();
mm/memcontrol.c +8 -0
index 5b009b233ab8..29e34c06ca83 100644
@@ -7584,6 +7584,14 @@ long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
 	return nr_swap_pages;
 }
 
+long mem_cgroup_get_nr_swapcache_pages(struct mem_cgroup *memcg)
+{
+	if (mem_cgroup_disabled())
+		return total_swapcache_pages();
+
+	return memcg_page_state(memcg, NR_SWAPCACHE);
+}
+
 bool mem_cgroup_swap_full(struct folio *folio)
 {
 	struct mem_cgroup *memcg;
mm/vmscan.c +3 -0
index 6f13394b112e..a5e04291662f 100644
@@ -137,6 +137,9 @@ struct scan_control {
 	/* Always discard instead of demoting to lower tier memory */
 	unsigned int no_demotion:1;
 
+	/* Swap space is exhausted, only reclaim swapcache for anon LRU */
+	unsigned int swapcache_only:1;
+
 	/* Allocation order */
 	s8 order;
 

mm/vmscan.c static bool can_demote(int nid, struct scan_control *sc) · line 613

Quoted 9 lines
mm/vmscan.c +6 -0
@@ -602,6 +605,12 @@ static bool can_demote(int nid, struct scan_control *sc)
 	return true;
 }
 
+static void set_swapcache_mode(struct scan_control *sc, bool swapcache_only)
+{
+	if (sc)
+		sc->swapcache_only = swapcache_only;
+}
+

I think that it's unnecessary to introduce a new function. I understand that you want to reduce the code duplication. We can add

        sc->swapcache_only = false;

at the beginning of can_reclaim_anon_pages() to reduce code duplication. That can cover even more cases IIUC.

Quoted 20 lines
mm/vmscan.c +8 -2
 static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
 					  int nid,
 					  struct scan_control *sc)
@@ -611,12 +620,26 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
 		 * For non-memcg reclaim, is there
 		 * space in any swap device?
 		 */
-		if (get_nr_swap_pages() > 0)
+		if (get_nr_swap_pages() > 0) {
+			set_swapcache_mode(sc, false);
 			return true;
+		}
+		/* Is there any swapcache pages to reclaim? */
+		if (total_swapcache_pages() > 0) {
+			set_swapcache_mode(sc, true);
+			return true;
+		}
 	} else {
 		/* Is the memcg below its swap limit? */
-		if (mem_cgroup_get_nr_swap_pages(memcg) > 0)

mm/vmscan.c static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, · line 643

Quoted 10 lines
mm/vmscan.c +8 -0
+		if (mem_cgroup_get_nr_swap_pages(memcg) > 0) {
+			set_swapcache_mode(sc, false);
 			return true;
+		}
+		/* Is there any swapcache pages in memcg to reclaim? */
+		if (mem_cgroup_get_nr_swapcache_pages(memcg) > 0) {
+			set_swapcache_mode(sc, true);
+			return true;
+		}
 	}

If can_demote() returns true, we shouldn't scan swapcache only.

--
Best Regards,
Huang, Ying

Quoted 17 lines
mm/vmscan.c +9 -0
 	/*
@@ -2342,6 +2365,15 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan,
 		 */
 		scan += nr_pages;
 
+		/*
+		 * Count non-swapcache too because the swapcache pages may
+		 * be rare and it takes too much times here if not count
+		 * the non-swapcache pages.
+		 */
+		if (unlikely(sc->swapcache_only && !is_file_lru(lru) &&
+		    !folio_test_swapcache(folio)))
+			goto move;
+
 		if (!folio_test_lru(folio))
 			goto move;
 		if (!sc->may_unmap && folio_mapped(folio))
author
#2 lore raw
Quoted 113 lines · Ying
Quoted 99 lines · Liu Shixin

When spaces of swap devices are exhausted, only file pages can be reclaimed. But there are still some swapcache pages in anon lru list. This can lead to a premature out-of-memory.

The problem is found with such step:

Firstly, set a 9MB disk swap space, then create a cgroup with 10MB memory limit, then runs an program to allocates about 15MB memory.

The problem occurs occasionally, which may need about 100 times [1].

Fix it by checking number of swapcache pages in can_reclaim_anon_pages(). If the number is not zero, return true and set swapcache_only to 1. When scan anon lru list in swapcache_only mode, non-swapcache pages will be skipped to isolate in order to accelerate reclaim efficiency.

However, in swapcache_only mode, the scan count still increased when scan non-swapcache pages because there are large number of non-swapcache pages and rare swapcache pages in swapcache_only mode, and if the non-swapcache is skipped and do not count, the scan of pages in isolate_lru_folios() can eventually lead to hung task, just as Sachin reported [2].

By the way, since there are enough times of memory reclaim before OOM, it is not need to isolate too much swapcache pages in one times.

[1]. https://lore.kernel.org/lkml/CAJD7tkZAfgncV+KbKr36=eDzMnT=9dZOT0dpMWcurHLr6Do+GA@mail.gmail.com/
[2]. https://lore.kernel.org/linux-mm/CAJD7tkafz_2XAuqE8tGLPEcpLngewhUo=5US14PAtSM9tLBUQg@mail.gmail.com/

---
v6->v7: Reset swapcache_only to zero after there are swap spaces. v5->v6: Fix NULL pointing derefence and hung task problem reported by Sachin.

 include/linux/swap.h |  6 ++++++
 mm/memcontrol.c      |  8 ++++++++
 mm/vmscan.c          | 36 ++++++++++++++++++++++++++++++++++--
 3 files changed, 48 insertions(+), 2 deletions(-)
include/linux/swap.h +6 -0
index f6dd6575b905..3ba146ae7cf5 100644
@@ -659,6 +659,7 @@ static inline void mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_p
 }
 
 extern long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg);
+extern long mem_cgroup_get_nr_swapcache_pages(struct mem_cgroup *memcg);
 extern bool mem_cgroup_swap_full(struct folio *folio);
 #else
 static inline void mem_cgroup_swapout(struct folio *folio, swp_entry_t entry)
@@ -681,6 +682,11 @@ static inline long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
 	return get_nr_swap_pages();
 }
 
+static inline long mem_cgroup_get_nr_swapcache_pages(struct mem_cgroup *memcg)
+{
+	return total_swapcache_pages();
+}
+
 static inline bool mem_cgroup_swap_full(struct folio *folio)
 {
 	return vm_swap_full();
mm/memcontrol.c +8 -0
index 5b009b233ab8..29e34c06ca83 100644
@@ -7584,6 +7584,14 @@ long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg)
 	return nr_swap_pages;
 }
 
+long mem_cgroup_get_nr_swapcache_pages(struct mem_cgroup *memcg)
+{
+	if (mem_cgroup_disabled())
+		return total_swapcache_pages();
+
+	return memcg_page_state(memcg, NR_SWAPCACHE);
+}
+
 bool mem_cgroup_swap_full(struct folio *folio)
 {
 	struct mem_cgroup *memcg;
mm/vmscan.c +3 -0
index 6f13394b112e..a5e04291662f 100644
@@ -137,6 +137,9 @@ struct scan_control {
 	/* Always discard instead of demoting to lower tier memory */
 	unsigned int no_demotion:1;
 
+	/* Swap space is exhausted, only reclaim swapcache for anon LRU */
+	unsigned int swapcache_only:1;
+
 	/* Allocation order */
 	s8 order;
 

mm/vmscan.c static bool can_demote(int nid, struct scan_control *sc) · line 613

Quoted 9 lines
mm/vmscan.c +6 -0
@@ -602,6 +605,12 @@ static bool can_demote(int nid, struct scan_control *sc)
 	return true;
 }
 
+static void set_swapcache_mode(struct scan_control *sc, bool swapcache_only)
+{
+	if (sc)
+		sc->swapcache_only = swapcache_only;
+}
+

I think that it's unnecessary to introduce a new function. I understand that you want to reduce the code duplication. We can add

Quoted 4 lines
        sc->swapcache_only = false;

at the beginning of can_reclaim_anon_pages() to reduce code duplication. That can cover even more cases IIUC.

OK, it‘s more appropriate, I will resend v8, thank you.

Quoted 55 lines
Quoted 20 lines
mm/vmscan.c +8 -2
 static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
 					  int nid,
 					  struct scan_control *sc)
@@ -611,12 +620,26 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
 		 * For non-memcg reclaim, is there
 		 * space in any swap device?
 		 */
-		if (get_nr_swap_pages() > 0)
+		if (get_nr_swap_pages() > 0) {
+			set_swapcache_mode(sc, false);
 			return true;
+		}
+		/* Is there any swapcache pages to reclaim? */
+		if (total_swapcache_pages() > 0) {
+			set_swapcache_mode(sc, true);
+			return true;
+		}
 	} else {
 		/* Is the memcg below its swap limit? */
-		if (mem_cgroup_get_nr_swap_pages(memcg) > 0)

mm/vmscan.c static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg, · line 643

Quoted 10 lines
mm/vmscan.c +8 -0
+		if (mem_cgroup_get_nr_swap_pages(memcg) > 0) {
+			set_swapcache_mode(sc, false);
 			return true;
+		}
+		/* Is there any swapcache pages in memcg to reclaim? */
+		if (mem_cgroup_get_nr_swapcache_pages(memcg) > 0) {
+			set_swapcache_mode(sc, true);
+			return true;
+		}
 	}

If can_demote() returns true, we shouldn't scan swapcache only.

--
Best Regards,
Huang, Ying

Quoted 7 lines
mm/vmscan.c +2 -0
 	/*
@@ -2342,6 +2365,15 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan,
 		 */
 		scan += nr_pages;
 
+		/*
+		 * Count non-swapcache too because the swapcache pages may

mm/vmscan.c static unsigned long isolate_lru_folios(unsigned long nr_to_scan, · line 2379

Quoted 10 lines
mm/vmscan.c +7 -0
+		 * be rare and it takes too much times here if not count
+		 * the non-swapcache pages.
+		 */
+		if (unlikely(sc->swapcache_only && !is_file_lru(lru) &&
+		    !folio_test_swapcache(folio)))
+			goto move;
+
 		if (!folio_test_lru(folio))
 			goto move;
 		if (!sc->may_unmap && folio_mapped(folio))

.