When memcg approaches its limit, charging mTHP becomes difficult. At this point, when the charge fails, we fallback to the next order to avoid repeatedly retrying larger orders. Reported-by: Kairui Song <ryncsn@xxxxxxxxx> Signed-off-by: Barry Song <v-songbaohua@xxxxxxxx> --- mm/memory.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 0ed3603aaf31..6cba28ef91e7 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4121,8 +4121,12 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf) while (orders) { addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); folio = vma_alloc_folio(gfp, order, vma, addr, true); - if (folio) - return folio; + if (folio) { + if (!mem_cgroup_swapin_charge_folio(folio, + vma->vm_mm, gfp, entry)) + return folio; + folio_put(folio); + } order = next_order(&orders, order); } @@ -4244,7 +4248,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) } need_clear_cache = true; - if (mem_cgroup_swapin_charge_folio(folio, + if (nr_pages == 1 && mem_cgroup_swapin_charge_folio(folio, vma->vm_mm, GFP_KERNEL, entry)) { ret = VM_FAULT_OOM; -- 2.34.1 Thanks Barry