Skip to content

Commit 364c1ee

Browse files
Yang Shitorvalds
authored andcommitted
mm: thp: extract split_queue_* into a struct
Patch series "Make deferred split shrinker memcg aware", v6. Currently THP deferred split shrinker is not memcg aware, this may cause premature OOM with some configuration. For example the below test would run into premature OOM easily: $ cgcreate -g memory:thp $ echo 4G > /sys/fs/cgroup/memory/thp/memory/limit_in_bytes $ cgexec -g memory:thp transhuge-stress 4000 transhuge-stress comes from kernel selftest. It is easy to hit OOM, but there are still a lot THP on the deferred split queue, memcg direct reclaim can't touch them since the deferred split shrinker is not memcg aware. Convert deferred split shrinker memcg aware by introducing per memcg deferred split queue. The THP should be on either per node or per memcg deferred split queue if it belongs to a memcg. When the page is immigrated to the other memcg, it will be immigrated to the target memcg's deferred split queue too. Reuse the second tail page's deferred_list for per memcg list since the same THP can't be on multiple deferred split queues. Make deferred split shrinker not depend on memcg kmem since it is not slab. It doesn't make sense to not shrink THP even though memcg kmem is disabled. With the above change the test demonstrated above doesn't trigger OOM even though with cgroup.memory=nokmem. This patch (of 4): Put split_queue, split_queue_lock and split_queue_len into a struct in order to reduce code duplication when we convert deferred_split to memcg aware in the later patches. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Yang Shi <[email protected]> Suggested-by: "Kirill A . Shutemov" <[email protected]> Acked-by: Kirill A. Shutemov <[email protected]> Reviewed-by: Kirill Tkhai <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Shakeel Butt <[email protected]> Cc: David Rientjes <[email protected]> Cc: Qian Cai <[email protected]> Cc: Vladimir Davydov <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 09d91cd commit 364c1ee

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

include/linux/mmzone.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,14 @@ struct zonelist {
679679
extern struct page *mem_map;
680680
#endif
681681

682+
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
683+
struct deferred_split {
684+
spinlock_t split_queue_lock;
685+
struct list_head split_queue;
686+
unsigned long split_queue_len;
687+
};
688+
#endif
689+
682690
/*
683691
* On NUMA machines, each NUMA node would have a pg_data_t to describe
684692
* it's memory layout. On UMA machines there is a single pglist_data which
@@ -758,9 +766,7 @@ typedef struct pglist_data {
758766
#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
759767

760768
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
761-
spinlock_t split_queue_lock;
762-
struct list_head split_queue;
763-
unsigned long split_queue_len;
769+
struct deferred_split deferred_split_queue;
764770
#endif
765771

766772
/* Fields commonly accessed by the page reclaim scanner */

mm/huge_memory.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,6 +2691,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
26912691
{
26922692
struct page *head = compound_head(page);
26932693
struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
2694+
struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
26942695
struct anon_vma *anon_vma = NULL;
26952696
struct address_space *mapping = NULL;
26962697
int count, mapcount, extra_pins, ret;
@@ -2777,17 +2778,17 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
27772778
}
27782779

27792780
/* Prevent deferred_split_scan() touching ->_refcount */
2780-
spin_lock(&pgdata->split_queue_lock);
2781+
spin_lock(&ds_queue->split_queue_lock);
27812782
count = page_count(head);
27822783
mapcount = total_mapcount(head);
27832784
if (!mapcount && page_ref_freeze(head, 1 + extra_pins)) {
27842785
if (!list_empty(page_deferred_list(head))) {
2785-
pgdata->split_queue_len--;
2786+
ds_queue->split_queue_len--;
27862787
list_del(page_deferred_list(head));
27872788
}
27882789
if (mapping)
27892790
__dec_node_page_state(page, NR_SHMEM_THPS);
2790-
spin_unlock(&pgdata->split_queue_lock);
2791+
spin_unlock(&ds_queue->split_queue_lock);
27912792
__split_huge_page(page, list, end, flags);
27922793
if (PageSwapCache(head)) {
27932794
swp_entry_t entry = { .val = page_private(head) };
@@ -2804,7 +2805,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
28042805
dump_page(page, "total_mapcount(head) > 0");
28052806
BUG();
28062807
}
2807-
spin_unlock(&pgdata->split_queue_lock);
2808+
spin_unlock(&ds_queue->split_queue_lock);
28082809
fail: if (mapping)
28092810
xa_unlock(&mapping->i_pages);
28102811
spin_unlock_irqrestore(&pgdata->lru_lock, flags);
@@ -2827,65 +2828,69 @@ fail: if (mapping)
28272828
void free_transhuge_page(struct page *page)
28282829
{
28292830
struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
2831+
struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
28302832
unsigned long flags;
28312833

2832-
spin_lock_irqsave(&pgdata->split_queue_lock, flags);
2834+
spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
28332835
if (!list_empty(page_deferred_list(page))) {
2834-
pgdata->split_queue_len--;
2836+
ds_queue->split_queue_len--;
28352837
list_del(page_deferred_list(page));
28362838
}
2837-
spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
2839+
spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
28382840
free_compound_page(page);
28392841
}
28402842

28412843
void deferred_split_huge_page(struct page *page)
28422844
{
28432845
struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
2846+
struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
28442847
unsigned long flags;
28452848

28462849
VM_BUG_ON_PAGE(!PageTransHuge(page), page);
28472850

2848-
spin_lock_irqsave(&pgdata->split_queue_lock, flags);
2851+
spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
28492852
if (list_empty(page_deferred_list(page))) {
28502853
count_vm_event(THP_DEFERRED_SPLIT_PAGE);
2851-
list_add_tail(page_deferred_list(page), &pgdata->split_queue);
2852-
pgdata->split_queue_len++;
2854+
list_add_tail(page_deferred_list(page), &ds_queue->split_queue);
2855+
ds_queue->split_queue_len++;
28532856
}
2854-
spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
2857+
spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
28552858
}
28562859

28572860
static unsigned long deferred_split_count(struct shrinker *shrink,
28582861
struct shrink_control *sc)
28592862
{
28602863
struct pglist_data *pgdata = NODE_DATA(sc->nid);
2861-
return READ_ONCE(pgdata->split_queue_len);
2864+
struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
2865+
return READ_ONCE(ds_queue->split_queue_len);
28622866
}
28632867

28642868
static unsigned long deferred_split_scan(struct shrinker *shrink,
28652869
struct shrink_control *sc)
28662870
{
28672871
struct pglist_data *pgdata = NODE_DATA(sc->nid);
2872+
struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
28682873
unsigned long flags;
28692874
LIST_HEAD(list), *pos, *next;
28702875
struct page *page;
28712876
int split = 0;
28722877

2873-
spin_lock_irqsave(&pgdata->split_queue_lock, flags);
2878+
spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
28742879
/* Take pin on all head pages to avoid freeing them under us */
2875-
list_for_each_safe(pos, next, &pgdata->split_queue) {
2880+
list_for_each_safe(pos, next, &ds_queue->split_queue) {
28762881
page = list_entry((void *)pos, struct page, mapping);
28772882
page = compound_head(page);
28782883
if (get_page_unless_zero(page)) {
28792884
list_move(page_deferred_list(page), &list);
28802885
} else {
28812886
/* We lost race with put_compound_page() */
28822887
list_del_init(page_deferred_list(page));
2883-
pgdata->split_queue_len--;
2888+
ds_queue->split_queue_len--;
28842889
}
28852890
if (!--sc->nr_to_scan)
28862891
break;
28872892
}
2888-
spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
2893+
spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
28892894

28902895
list_for_each_safe(pos, next, &list) {
28912896
page = list_entry((void *)pos, struct page, mapping);
@@ -2899,15 +2904,15 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
28992904
put_page(page);
29002905
}
29012906

2902-
spin_lock_irqsave(&pgdata->split_queue_lock, flags);
2903-
list_splice_tail(&list, &pgdata->split_queue);
2904-
spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
2907+
spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2908+
list_splice_tail(&list, &ds_queue->split_queue);
2909+
spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
29052910

29062911
/*
29072912
* Stop shrinker if we didn't split any page, but the queue is empty.
29082913
* This can happen if pages were freed under us.
29092914
*/
2910-
if (!split && list_empty(&pgdata->split_queue))
2915+
if (!split && list_empty(&ds_queue->split_queue))
29112916
return SHRINK_STOP;
29122917
return split;
29132918
}

mm/page_alloc.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6646,9 +6646,11 @@ static unsigned long __init calc_memmap_size(unsigned long spanned_pages,
66466646
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
66476647
static void pgdat_init_split_queue(struct pglist_data *pgdat)
66486648
{
6649-
spin_lock_init(&pgdat->split_queue_lock);
6650-
INIT_LIST_HEAD(&pgdat->split_queue);
6651-
pgdat->split_queue_len = 0;
6649+
struct deferred_split *ds_queue = &pgdat->deferred_split_queue;
6650+
6651+
spin_lock_init(&ds_queue->split_queue_lock);
6652+
INIT_LIST_HEAD(&ds_queue->split_queue);
6653+
ds_queue->split_queue_len = 0;
66526654
}
66536655
#else
66546656
static void pgdat_init_split_queue(struct pglist_data *pgdat) {}

0 commit comments

Comments
 (0)