Skip to contentSkip to conversation

FAILED: patch "[PATCH] Multi-gen LRU: fix per-zone reclaim" failed to apply to 6.1-stable tree

PATCH New [email protected] 2 participants 4 messages Sep 9, 2023 – Sep 13, 2023

Subsystems (4)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

The patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

To reproduce the conflict and resubmit, you may use the following commands:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x 669281ee7ef731fb5204df9d948669bf32a5e68d
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2023090959-mothproof-scarf-6195@gregkh' --subject-prefix 'PATCH 6.1.y' HEAD^..

Possible dependencies:

669281ee7ef7 ("Multi-gen LRU: fix per-zone reclaim")
6df1b2212950 ("mm: multi-gen LRU: rename lrugen->lists[] to lrugen->folios[]")

thanks,

greg k-h

------------------ original commit in Linus's tree ------------------

From 669281ee7ef731fb5204df9d948669bf32a5e68d Mon Sep 17 00:00:00 2001
From: Kalesh Singh <[email protected]>
Date: Tue, 1 Aug 2023 19:56:02 -0700
Subject: [PATCH] Multi-gen LRU: fix per-zone reclaim

MGLRU has a LRU list for each zone for each type (anon/file) in each generation:

	long nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];

The min_seq (oldest generation) can progress independently for each type but the max_seq (youngest generation) is shared for both anon and file. This is to maintain a common frame of reference.

In order for eviction to advance the min_seq of a type, all the per-zone lists in the oldest generation of that type must be empty.

The eviction logic only considers pages from eligible zones for eviction or promotion.

    scan_folios() {
	...
	for (zone = sc->reclaim_idx; zone >= 0; zone--)  {
	    ...
	    sort_folio(); 	// Promote
	    ...
	    isolate_folio(); 	// Evict
	}
	...
    }

Consider the system has the movable zone configured and default 4 generations. The current state of the system is as shown below (only illustrating one type for simplicity):

Type: ANON

Zone DMA32 Normal Movable Device

Gen 0 0 0 4GB 0

Gen 1 0 1GB 1MB 0

Gen 2 1MB 4GB 1MB 0

Gen 3 1MB 1MB 1MB 0

Now consider there is a GFP_KERNEL allocation request (eligible zone index <= Normal), evict_folios() will return without doing any work since there are no pages to scan in the eligible zones of the oldest generation. Reclaim won't make progress until triggered from a ZONE_MOVABLE allocation request; which may not happen soon if there is a lot of free memory in the movable zone. This can lead to OOM kills, although there is 1GB pages in the Normal zone of Gen 1 that we have not yet tried to reclaim.

This issue is not seen in the conventional active/inactive LRU since there are no per-zone lists.

If there are no (not enough) folios to scan in the eligible zones, move folios from ineligible zone (zone_index > reclaim_index) to the next generation. This allows for the progression of min_seq and reclaiming from the next generation (Gen 1).

Qualcomm, Mediatek and raspberrypi [1] discovered this issue independently.

[1] https://github.com/raspberrypi/linux/issues/5395

Fixes ac35a4902374 ("mm: multi-gen LRU: minimal implementation")
Cc Yu Zhao <[email protected]>
Cc Barry Song <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Jan Alexander Steffens (heftig) <[email protected]>
Cc Matthias Brugger <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Qi Zheng <[email protected]>
Cc Steven Barrett <[email protected]>
Cc Suleiman Souhlal <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Aneesh Kumar K V <[email protected]>
mm/vmscan.c +14 -4
index 4039620d30fe..489a4fc7d9b1 100644
@@ -4889,7 +4889,8 @@ static int lru_gen_memcg_seg(struct lruvec *lruvec)
  *                          the eviction
  ******************************************************************************/
 
-static bool sort_folio(struct lruvec *lruvec, struct folio *folio, int tier_idx)
+static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc,
+		       int tier_idx)
 {
 	bool success;
 	int gen = folio_lru_gen(folio);
@@ -4939,6 +4940,13 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, int tier_idx)
 		return true;
 	}
 
+	/* ineligible */
+	if (zone > sc->reclaim_idx) {
+		gen = folio_inc_gen(lruvec, folio, false);
+		list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]);
+		return true;
+	}
+
 	/* waiting for writeback */
 	if (folio_test_locked(folio) || folio_test_writeback(folio) ||
 	    (type == LRU_GEN_FILE && folio_test_dirty(folio))) {
@@ -4987,7 +4995,8 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca
 static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
 		       int type, int tier, struct list_head *list)
 {
-	int gen, zone;
+	int i;
+	int gen;
 	enum vm_event_item item;
 	int sorted = 0;
 	int scanned = 0;
@@ -5003,9 +5012,10 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
 
 	gen = lru_gen_from_seq(lrugen->min_seq[type]);
 
-	for (zone = sc->reclaim_idx; zone >= 0; zone--) {
+	for (i = MAX_NR_ZONES; i > 0; i--) {
 		LIST_HEAD(moved);
 		int skipped = 0;
+		int zone = (sc->reclaim_idx + i) % MAX_NR_ZONES;
 		struct list_head *head = &lrugen->folios[gen][type][zone];
 
 		while (!list_empty(head)) {
@@ -5019,7 +5029,7 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
 
 			scanned += delta;
 
-			if (sort_folio(lruvec, folio, tier))
+			if (sort_folio(lruvec, folio, sc, tier))
 				sorted += delta;
 			else if (isolate_folio(lruvec, folio, sc)) {
 				list_add(&folio->lru, list);
#1 lore raw
Quoted 16 lines · On Sat, Sep 9, 2023 at 6:04 AM <[email protected]> wrote:

The patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

To reproduce the conflict and resubmit, you may use the following commands:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x 669281ee7ef731fb5204df9d948669bf32a5e68d
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2023090959-mothproof-scarf-6195@gregkh' --subject-prefix 'PATCH 6.1.y' HEAD^..

Quoted 4 lines

Possible dependencies:

669281ee7ef7 ("Multi-gen LRU: fix per-zone reclaim")
6df1b2212950 ("mm: multi-gen LRU: rename lrugen->lists[] to lrugen->folios[]")

Hi Greg,

Can you apply in this order please:

1) 6df1b2212950 ("mm: multi-gen LRU: rename lrugen->lists[] to lrugen->folios[]")
2) 669281ee7ef7 ("Multi-gen LRU: fix per-zone reclaim")

With the one rename dependency, I've checked that this applies cleanly and tested it.
Or let me know if you prefer I resend both.

Thanks,
Kalesh

Quoted 156 lines

thanks,

greg k-h

------------------ original commit in Linus's tree ------------------

From 669281ee7ef731fb5204df9d948669bf32a5e68d Mon Sep 17 00:00:00 2001
From: Kalesh Singh <[email protected]>
Date: Tue, 1 Aug 2023 19:56:02 -0700
Subject: [PATCH] Multi-gen LRU: fix per-zone reclaim

MGLRU has a LRU list for each zone for each type (anon/file) in each generation:

        long nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];

The min_seq (oldest generation) can progress independently for each type but the max_seq (youngest generation) is shared for both anon and file. This is to maintain a common frame of reference.

In order for eviction to advance the min_seq of a type, all the per-zone lists in the oldest generation of that type must be empty.

The eviction logic only considers pages from eligible zones for eviction or promotion.

    scan_folios() {
        ...
        for (zone = sc->reclaim_idx; zone >= 0; zone--)  {
            ...
            sort_folio();       // Promote
            ...
            isolate_folio();    // Evict
        }
        ...
    }

Consider the system has the movable zone configured and default 4 generations. The current state of the system is as shown below (only illustrating one type for simplicity):

Type: ANON

Zone DMA32 Normal Movable Device

Gen 0 0 0 4GB 0

Gen 1 0 1GB 1MB 0

Gen 2 1MB 4GB 1MB 0

Gen 3 1MB 1MB 1MB 0

Now consider there is a GFP_KERNEL allocation request (eligible zone index <= Normal), evict_folios() will return without doing any work since there are no pages to scan in the eligible zones of the oldest generation. Reclaim won't make progress until triggered from a ZONE_MOVABLE allocation request; which may not happen soon if there is a lot of free memory in the movable zone. This can lead to OOM kills, although there is 1GB pages in the Normal zone of Gen 1 that we have not yet tried to reclaim.

This issue is not seen in the conventional active/inactive LRU since there are no per-zone lists.

If there are no (not enough) folios to scan in the eligible zones, move folios from ineligible zone (zone_index > reclaim_index) to the next generation. This allows for the progression of min_seq and reclaiming from the next generation (Gen 1).

Qualcomm, Mediatek and raspberrypi [1] discovered this issue independently.

[1] https://github.com/raspberrypi/linux/issues/5395

Fixes ac35a4902374 ("mm: multi-gen LRU: minimal implementation")
Cc Yu Zhao <[email protected]>
Cc Barry Song <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Jan Alexander Steffens (heftig) <[email protected]>
Cc Matthias Brugger <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Qi Zheng <[email protected]>
Cc Steven Barrett <[email protected]>
Cc Suleiman Souhlal <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Aneesh Kumar K V <[email protected]>
mm/vmscan.c +14 -4
index 4039620d30fe..489a4fc7d9b1 100644
@@ -4889,7 +4889,8 @@ static int lru_gen_memcg_seg(struct lruvec *lruvec)
  *                          the eviction
  ******************************************************************************/
 
-static bool sort_folio(struct lruvec *lruvec, struct folio *folio, int tier_idx)
+static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc,
+                      int tier_idx)
 {
        bool success;
        int gen = folio_lru_gen(folio);
@@ -4939,6 +4940,13 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, int tier_idx)
                return true;
        }
 
+       /* ineligible */
+       if (zone > sc->reclaim_idx) {
+               gen = folio_inc_gen(lruvec, folio, false);
+               list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]);
+               return true;
+       }
+
        /* waiting for writeback */
        if (folio_test_locked(folio) || folio_test_writeback(folio) ||
            (type == LRU_GEN_FILE && folio_test_dirty(folio))) {
@@ -4987,7 +4995,8 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca
 static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
                       int type, int tier, struct list_head *list)
 {
-       int gen, zone;
+       int i;
+       int gen;
        enum vm_event_item item;
        int sorted = 0;
        int scanned = 0;
@@ -5003,9 +5012,10 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
 
        gen = lru_gen_from_seq(lrugen->min_seq[type]);
 
-       for (zone = sc->reclaim_idx; zone >= 0; zone--) {
+       for (i = MAX_NR_ZONES; i > 0; i--) {
                LIST_HEAD(moved);
                int skipped = 0;
+               int zone = (sc->reclaim_idx + i) % MAX_NR_ZONES;
                struct list_head *head = &lrugen->folios[gen][type][zone];
 
                while (!list_empty(head)) {
@@ -5019,7 +5029,7 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
 
                        scanned += delta;
 
-                       if (sort_folio(lruvec, folio, tier))
+                       if (sort_folio(lruvec, folio, sc, tier))
                                sorted += delta;
                        else if (isolate_folio(lruvec, folio, sc)) {
                                list_add(&folio->lru, list);
author maintainer
#2 lore raw
Quoted 26 lines · Kalesh Singh
Quoted 16 lines · On Sat, Sep 9, 2023 at 6:04 AM <[email protected]> wrote:

The patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

To reproduce the conflict and resubmit, you may use the following commands:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x 669281ee7ef731fb5204df9d948669bf32a5e68d
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2023090959-mothproof-scarf-6195@gregkh' --subject-prefix 'PATCH 6.1.y' HEAD^..

Quoted 4 lines

Possible dependencies:

669281ee7ef7 ("Multi-gen LRU: fix per-zone reclaim")
6df1b2212950 ("mm: multi-gen LRU: rename lrugen->lists[] to lrugen->folios[]")

Hi Greg,

Can you apply in this order please:

Quoted 7 lines

1) 6df1b2212950 ("mm: multi-gen LRU: rename lrugen->lists[] to lrugen->folios[]")
2) 669281ee7ef7 ("Multi-gen LRU: fix per-zone reclaim")

With the one rename dependency, I've checked that this applies cleanly and tested it.
Or let me know if you prefer I resend both.

That worked, now queued up, thanks!

greg k-h

#3 lore raw

PATCH 1/1 Multi-gen LRU: fix per-zone reclaim

In reply to [email protected]

MGLRU has a LRU list for each zone for each type (anon/file) in each generation:

	long nr_pages[MAX_NR_GENS][ANON_AND_FILE][MAX_NR_ZONES];

The min_seq (oldest generation) can progress independently for each type but the max_seq (youngest generation) is shared for both anon and file. This is to maintain a common frame of reference.

In order for eviction to advance the min_seq of a type, all the per-zone lists in the oldest generation of that type must be empty.

The eviction logic only considers pages from eligible zones for eviction or promotion.

    scan_folios() {
	...
	for (zone = sc->reclaim_idx; zone >= 0; zone--)  {
	    ...
	    sort_folio(); 	// Promote
	    ...
	    isolate_folio(); 	// Evict
	}
	...
    }

Consider the system has the movable zone configured and default 4 generations. The current state of the system is as shown below (only illustrating one type for simplicity):

Type: ANON

Zone DMA32 Normal Movable Device

Gen 0 0 0 4GB 0

Gen 1 0 1GB 1MB 0

Gen 2 1MB 4GB 1MB 0

Gen 3 1MB 1MB 1MB 0

Now consider there is a GFP_KERNEL allocation request (eligible zone index <= Normal), evict_folios() will return without doing any work since there are no pages to scan in the eligible zones of the oldest generation. Reclaim won't make progress until triggered from a ZONE_MOVABLE allocation request; which may not happen soon if there is a lot of free memory in the movable zone. This can lead to OOM kills, although there is 1GB pages in the Normal zone of Gen 1 that we have not yet tried to reclaim.

This issue is not seen in the conventional active/inactive LRU since there are no per-zone lists.

If there are no (not enough) folios to scan in the eligible zones, move folios from ineligible zone (zone_index > reclaim_index) to the next generation. This allows for the progression of min_seq and reclaiming from the next generation (Gen 1).

Qualcomm, Mediatek and raspberrypi [1] discovered this issue independently.

[1] https://github.com/raspberrypi/linux/issues/5395

Fixes ac35a4902374 ("mm: multi-gen LRU: minimal implementation")
Cc Yu Zhao <[email protected]>
Cc Barry Song <[email protected]>
Cc Brian Geffon <[email protected]>
Cc Jan Alexander Steffens (heftig) <[email protected]>
Cc Matthias Brugger <[email protected]>
Cc Oleksandr Natalenko <[email protected]>
Cc Qi Zheng <[email protected]>
Cc Steven Barrett <[email protected]>
Cc Suleiman Souhlal <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Aneesh Kumar K V <[email protected]>
(cherry picked from commit 669281ee7ef731fb5204df9d948669bf32a5e68d)

[ Kalesh Singh - Fix conflicts caused by rename of lrugen->lists to
lrugen->folios ]

---
 mm/vmscan.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
mm/vmscan.c +14 -4
index d18296109aa7..d7016fdb7c14 100644
@@ -4728,7 +4728,8 @@ void lru_gen_look_around(struct page_vma_mapped_walk *pvmw)
  *                          the eviction
  ******************************************************************************/
 
-static bool sort_folio(struct lruvec *lruvec, struct folio *folio, int tier_idx)
+static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc,
+		       int tier_idx)
 {
 	bool success;
 	int gen = folio_lru_gen(folio);
@@ -4779,6 +4780,13 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, int tier_idx)
 		return true;
 	}
 
+	/* ineligible */
+	if (zone > sc->reclaim_idx) {
+		gen = folio_inc_gen(lruvec, folio, false);
+		list_move_tail(&folio->lru, &lrugen->lists[gen][type][zone]);
+		return true;
+	}
+
 	/* waiting for writeback */
 	if (folio_test_locked(folio) || folio_test_writeback(folio) ||
 	    (type == LRU_GEN_FILE && folio_test_dirty(folio))) {
@@ -4831,7 +4839,8 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca
 static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
 		       int type, int tier, struct list_head *list)
 {
-	int gen, zone;
+	int i;
+	int gen;
 	enum vm_event_item item;
 	int sorted = 0;
 	int scanned = 0;
@@ -4847,9 +4856,10 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
 
 	gen = lru_gen_from_seq(lrugen->min_seq[type]);
 
-	for (zone = sc->reclaim_idx; zone >= 0; zone--) {
+	for (i = MAX_NR_ZONES; i > 0; i--) {
 		LIST_HEAD(moved);
 		int skipped = 0;
+		int zone = (sc->reclaim_idx + i) % MAX_NR_ZONES;
 		struct list_head *head = &lrugen->lists[gen][type][zone];
 
 		while (!list_empty(head)) {
@@ -4863,7 +4873,7 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc,
 
 			scanned += delta;
 
-			if (sort_folio(lruvec, folio, tier))
+			if (sort_folio(lruvec, folio, sc, tier))
 				sorted += delta;
 			else if (isolate_folio(lruvec, folio, sc)) {
 				list_add(&folio->lru, list);
Signature · 1 line
2.42.0.283.g2d96d420d3-goog