]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm: vmscan: try to reclaim swapcache pages if no swap space
authorLiu Shixin <liushixin2@huawei.com>
Wed, 30 Aug 2023 03:56:00 +0000 (11:56 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Mon, 20 Nov 2023 21:16:45 +0000 (13:16 -0800)
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/

Link: https://lore.kernel.org/lkml/CAJD7tkZAfgncV+KbKr36=eDzMnT=9dZOT0dpMWcurHLr6Do+GA@mail.gmail.com/
Link: https://lkml.kernel.org/r/20230830035600.1656792-1-liushixin2@huawei.com
Signed-off-by: Liu Shixin <liushixin2@huawei.com>
Tested-by: Yosry Ahmed <yosryahmed@google.com>
Reviewed-by: "Huang, Ying" <ying.huang@intel.com>
Reviewed-by: Yosry Ahmed <yosryahmed@google.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: Sachin Sant <sachinp@linux.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
include/linux/swap.h
mm/memcontrol.c
mm/vmscan.c

index f6dd6575b90545c3c927efbb033c1134e091b69a..3ba146ae7cf5db7766b2cd3a3d7cdf6ae1ce31ae 100644 (file)
@@ -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();
index b226090fd9061c6261ace5a09850ebcc0c183060..564aa8f25b7187985680f8204629b33af07ebe29 100644 (file)
@@ -7866,6 +7866,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;
index 5dc581cac225144b3bdb748341cad5026e1e24a9..e78235927deede9d42c044ecca861ee3bf5f1971 100644 (file)
@@ -136,6 +136,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;
 
@@ -319,10 +322,20 @@ static inline bool can_reclaim_anon_pages(struct mem_cgroup *memcg,
                 */
                if (get_nr_swap_pages() > 0)
                        return true;
+               /* Is there any swapcache pages to reclaim? */
+               if (total_swapcache_pages() > 0) {
+                       sc->swapcache_only = 1;
+                       return true;
+               }
        } else {
                /* Is the memcg below its swap limit? */
                if (mem_cgroup_get_nr_swap_pages(memcg) > 0)
                        return true;
+               /* Is there any swapcache pages in memcg to reclaim? */
+               if (mem_cgroup_get_nr_swapcache_pages(memcg) > 0) {
+                       sc->swapcache_only = 1;
+                       return true;
+               }
        }
 
        /*
@@ -1584,6 +1597,19 @@ static bool skip_cma(struct folio *folio, struct scan_control *sc)
 }
 #endif
 
+static bool skip_isolate(struct folio *folio, struct scan_control *sc,
+                        enum lru_list lru)
+{
+       if (folio_zonenum(folio) > sc->reclaim_idx)
+               return true;
+       if (skip_cma(folio, sc))
+               return true;
+       if (unlikely(sc->swapcache_only && !is_file_lru(lru) &&
+           !folio_test_swapcache(folio)))
+               return true;
+       return false;
+}
+
 /*
  * Isolating page from the lruvec to fill in @dst list by nr_to_scan times.
  *
@@ -1630,8 +1656,7 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan,
                nr_pages = folio_nr_pages(folio);
                total_scan += nr_pages;
 
-               if (folio_zonenum(folio) > sc->reclaim_idx ||
-                               skip_cma(folio, sc)) {
+               if (skip_isolate(folio, sc, lru)) {
                        nr_skipped[folio_zonenum(folio)] += nr_pages;
                        move_to = &folios_skipped;
                        goto move;