Skip to contentSkip to conversation

mm: thp: update split_queue_len correctly

PATCH NAK Shakeel Butt 3 participants 6 messages Nov 23, 2021 – Nov 24, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm: thp: update split_queue_len correctly

The deferred THPs are split on memory pressure through shrinker callback and splitting of THP during reclaim can fail for several reasons like unable to lock the THP, under writeback or unexpected number of pins on the THP. Such pages are put back on the deferred split list for consideration later. However kernel does not update the deferred queue size on putting back the pages whose split was failed. This patch fixes that.

Without this patch the split_queue_len can underflow. Shrinker will always get that there are some THPs to split even if there are not and waste some cpu to scan the empty list.

Fixes 364c1eebe453 ("mm: thp: extract split_queue_* into a struct")

---
Changes since v1:
- updated commit message
- incorporated Yang Shi's suggestion

 mm/huge_memory.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/huge_memory.c +2 -1
index e5483347291c..d393028681e2 100644
@@ -2809,7 +2809,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
 	unsigned long flags;
 	LIST_HEAD(list), *pos, *next;
 	struct page *page;
-	int split = 0;
+	unsigned long split = 0;
 
 #ifdef CONFIG_MEMCG
 	if (sc->memcg)
@@ -2847,6 +2847,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
 
 	spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
 	list_splice_tail(&list, &ds_queue->split_queue);
+	ds_queue->split_queue_len -= split;
 	spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
 
 	/*
Signature · 1 line
2.34.0.rc2.393.gf8c9666880-goog
Reviewed-by on v2 1/1
#1 lore raw
Quoted 13 lines · Shakeel Butt

The deferred THPs are split on memory pressure through shrinker callback and splitting of THP during reclaim can fail for several reasons like unable to lock the THP, under writeback or unexpected number of pins on the THP. Such pages are put back on the deferred split list for consideration later. However kernel does not update the deferred queue size on putting back the pages whose split was failed. This patch fixes that.

Without this patch the split_queue_len can underflow. Shrinker will always get that there are some THPs to split even if there are not and waste some cpu to scan the empty list.

Quoted 6 lines
Fixes 364c1eebe453 ("mm: thp: extract split_queue_* into a struct")

---
Changes since v1:
- updated commit message
- incorporated Yang Shi's suggestion

Quoted 28 lines
 mm/huge_memory.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/huge_memory.c +2 -2
index e5483347291c..d393028681e2 100644
@@ -2809,7 +2809,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
        unsigned long flags;
        LIST_HEAD(list), *pos, *next;
        struct page *page;
-       int split = 0;
+       unsigned long split = 0;
 
 #ifdef CONFIG_MEMCG
        if (sc->memcg)
@@ -2847,6 +2847,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
 
        spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
        list_splice_tail(&list, &ds_queue->split_queue);
+       ds_queue->split_queue_len -= split;
        spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
 
        /*
--

2.34.0.rc2.393.gf8c9666880-goog

#2 lore raw
In reply to Shakeel Butt
Quoted 7 lines · Shakeel Butt

The deferred THPs are split on memory pressure through shrinker callback and splitting of THP during reclaim can fail for several reasons like unable to lock the THP, under writeback or unexpected number of pins on the THP. Such pages are put back on the deferred split list for consideration later. However kernel does not update the deferred queue size on putting back the pages whose split was failed. This patch fixes that.

Hm. No. split_huge_page_to_list() updates the queue size on split success.

NAK.

Signature · 1 line
 Kirill A. Shutemov
#3 lore raw
Quoted 10 lines · Kirill A. Shutemov
Quoted 7 lines · Shakeel Butt

The deferred THPs are split on memory pressure through shrinker callback and splitting of THP during reclaim can fail for several reasons like unable to lock the THP, under writeback or unexpected number of pins on the THP. Such pages are put back on the deferred split list for consideration later. However kernel does not update the deferred queue size on putting back the pages whose split was failed. This patch fixes that.

Quoted 2 lines

Hm. No. split_huge_page_to_list() updates the queue size on split success.

Right. This is really convoluted. split_huge_page_to_list() is just assuming that if the given page is on a deferred list then it must be on the list returned by get_deferred_split_queue(page). The interaction of move_charge and deferred split seems broken.

Andrew, can you please drop this patch?

#4 lore raw
Quoted 13 lines · Shakeel Butt
Quoted 10 lines · Kirill A. Shutemov
Quoted 7 lines · Shakeel Butt

The deferred THPs are split on memory pressure through shrinker callback and splitting of THP during reclaim can fail for several reasons like unable to lock the THP, under writeback or unexpected number of pins on the THP. Such pages are put back on the deferred split list for consideration later. However kernel does not update the deferred queue size on putting back the pages whose split was failed. This patch fixes that.

Quoted 7 lines
Quoted 2 lines

Hm. No. split_huge_page_to_list() updates the queue size on split success.

Right. This is really convoluted. split_huge_page_to_list() is just assuming that if the given page is on a deferred list then it must be on the list returned by get_deferred_split_queue(page). The interaction of move_charge and deferred split seems broken.

Because memcg code doesn't move charge for PTE mapped THP at all. See the below comment from mem_cgroup_move_charge_pte_range():

"We can have a part of the split pmd here. Moving it can be done but it would be too convoluted so simply ignore such a partial THP and keep it in original memcg. There should be somebody mapping the head."

BTW, did you run into any problem related to this?

Quoted 2 lines

Andrew, can you please drop this patch?

#5 lore raw
Quoted 23 lines · Yang Shi
Quoted 13 lines · Shakeel Butt
Quoted 10 lines · Kirill A. Shutemov
Quoted 7 lines · Shakeel Butt

The deferred THPs are split on memory pressure through shrinker callback and splitting of THP during reclaim can fail for several reasons like unable to lock the THP, under writeback or unexpected number of pins on the THP. Such pages are put back on the deferred split list for consideration later. However kernel does not update the deferred queue size on putting back the pages whose split was failed. This patch fixes that.

Quoted 7 lines
Quoted 2 lines

Hm. No. split_huge_page_to_list() updates the queue size on split success.

Right. This is really convoluted. split_huge_page_to_list() is just assuming that if the given page is on a deferred list then it must be on the list returned by get_deferred_split_queue(page). The interaction of move_charge and deferred split seems broken.

Quoted 9 lines

Because memcg code doesn't move charge for PTE mapped THP at all. See the below comment from mem_cgroup_move_charge_pte_range():

"We can have a part of the split pmd here. Moving it can be done but it would be too convoluted so simply ignore such a partial THP and keep it in original memcg. There should be somebody mapping the head."

BTW, did you run into any problem related to this?

No, just reading code to see if I can share code for the sync splitting of THPs.