Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp7262527pxb; Thu, 18 Feb 2021 05:55:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJw82OKAzAvzyFexh+NAAG0rEZN/if//DZRlaRh9SzJgh7Y9rpS8jZd0fB0JVihiUFAaJT2K X-Received: by 2002:a17:907:9056:: with SMTP id az22mr2074842ejc.279.1613656546610; Thu, 18 Feb 2021 05:55:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613656546; cv=none; d=google.com; s=arc-20160816; b=Ur229y8shUw6QPUIHeFPFy8gu9MWvxQkk+gOwAqlORIMkZcbTU6l8V+5Q8fueogetr tmIWCwMca577clO6045BVkQlf59WC/ygOSmpzTeLi36nlQyOnPqAFW6Z7Aofr5iQx2Xg dAdmFGTkOMDhKRB3Sqt0GcwXSFfrgLt3jBtMAy7fO+YMoktt8FHzCh4jAwPyoceWGgNw 4xfWqgH3ZX4+iv4FP+7Oz9zF/5nwG3YZw/J7/zcntyQbfDnLOfpLeAVw7OnfoEeZWQUi /CVgcTmd5U8Ft/ExoLQ+jd91bXQpyZjPRMqLD/Wm6c0ZMutPnudn5YVe486Plc1cKjkt S7jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=nVAfrpQnVCmh0MWWT4CtiEYGp8mxt4WGLGMRP8fq2FQ=; b=GmHOv+gHu8oWohryvDyqfjXXqO4BvJjU22zyxiG4m1aqo7qGhqC9HWxsn4slKm52Vp sYCnSsKDIoOl20BdATghJoIfhxtRWCS751PaDq19YWfvJZopsGtP1Hq+jUP/2SibAXRO OStmGKk36jbqOUdtb95cblKVHtOC2kai8WHs7VM0Cg1bj5uGgdq2/HDbdO+KyB3fuGaW S366JjmcjzEuK9D1SeZPM4KNP4vPCOYGJIc6oAXofDhiXuY6AeQZ19+djGIoJdJttpbm lgUgk3fOklwR7p4PJm9IfLmJ9MCnqrKNoB3qqm7IuXO8l2UM4qr0oai54e9cqsmzDQFx F7BA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ss7si3750158ejb.449.2021.02.18.05.55.22; Thu, 18 Feb 2021 05:55:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232024AbhBRNwt (ORCPT + 99 others); Thu, 18 Feb 2021 08:52:49 -0500 Received: from mx2.suse.de ([195.135.220.15]:58950 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230495AbhBRMBX (ORCPT ); Thu, 18 Feb 2021 07:01:23 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 99A7AAF1B; Thu, 18 Feb 2021 12:00:39 +0000 (UTC) From: Oscar Salvador To: Andrew Morton Cc: Mike Kravetz , David Hildenbrand , Michal Hocko , Muchun Song , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH v2 1/2] mm: Make alloc_contig_range handle free hugetlb pages Date: Thu, 18 Feb 2021 13:00:27 +0100 Message-Id: <20210218120028.6499-2-osalvador@suse.de> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210218120028.6499-1-osalvador@suse.de> References: <20210218120028.6499-1-osalvador@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org alloc_contig_range will fail if it ever sees a HugeTLB page within the range we are trying to allocate, even when that page is free and can be easily reallocated. This has proofed to be problematic for some users of alloc_contic_range, e.g: CMA and virtio-mem, where those would fail the call even when those pages lay in ZONE_MOVABLE and are free. We can do better by trying to dissolve such pages. Free hugepages are tricky to handle so as to no userspace application notices disruption, we need to replace the current free hugepage with a new one. In order to do that, a new function called alloc_and_dissolve_huge_page is introduced. This function will first try to get a new fresh hugepage, and if it succeeds, it will dissolve the old one. If the old hugepage cannot be be dissolved, we have to dissolve the new hugepage we just got. Should that fail as well, we count is as a surplus, so the pool will be re-balanced when a hugepage gets free instead of enqueues again. With regard to the allocation, we restrict it to the node the page belongs to with __GFP_THISNODE, meaning we do not fallback on other node's zones. Note that gigantic hugetlb pages are fenced off since there is a cyclic dependency between them and alloc_contig_range. Signed-off-by: Oscar Salvador --- include/linux/hugetlb.h | 6 ++++ mm/compaction.c | 12 ++++++++ mm/hugetlb.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index b5807f23caf8..72352d718829 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -505,6 +505,7 @@ struct huge_bootmem_page { struct hstate *hstate; }; +bool isolate_or_dissolve_huge_page(struct page *page); struct page *alloc_huge_page(struct vm_area_struct *vma, unsigned long addr, int avoid_reserve); struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid, @@ -775,6 +776,11 @@ void set_page_huge_active(struct page *page); #else /* CONFIG_HUGETLB_PAGE */ struct hstate {}; +static inline bool isolate_or_dissolve_huge_page(struct page *page) +{ + return false; +} + static inline struct page *alloc_huge_page(struct vm_area_struct *vma, unsigned long addr, int avoid_reserve) diff --git a/mm/compaction.c b/mm/compaction.c index 190ccdaa6c19..d52506ed9db7 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -905,6 +905,18 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, valid_page = page; } + if (PageHuge(page) && cc->alloc_contig) { + if (!isolate_or_dissolve_huge_page(page)) + goto isolate_fail; + + /* + * Ok, the hugepage was dissolved. Now these pages are + * Buddy and cannot be re-allocated because they are + * isolated. Fall-through as the check below handles + * Buddy pages. + */ + } + /* * Skip if free. We read page order here without zone lock * which is generally unsafe, but the race window is small and diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 4bdb58ab14cb..a4fbbe924a55 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2294,6 +2294,81 @@ static void restore_reserve_on_error(struct hstate *h, } } +static bool alloc_and_dissolve_huge_page(struct hstate *h, struct page *page) +{ + gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; + int nid = page_to_nid(page); + struct page *new_page; + bool ret = false; + + /* + * Before dissolving the page, we need to allocate a new one, + * so the pool remains stable. + */ + new_page = alloc_fresh_huge_page(h, gfp_mask, nid, NULL, NULL); + if (new_page) { + /* + * Free it into the hugepage allocator + */ + put_page(new_page); + + /* + * Ok, we got a new free hugepage to replace this one. Try to + * dissolve the old page. + */ + if (!dissolve_free_huge_page(page)) { + ret = true; + } else if (dissolve_free_huge_page(new_page)) { + /* + * Seems the old page could not be dissolved, so try to + * dissolve the freshly allocated page. If that fails + * too, let us count the new page as a surplus. Doing so + * allows the pool to be re-balanced when pages are freed + * instead of enqueued again. + */ + spin_lock(&hugetlb_lock); + h->surplus_huge_pages++; + h->surplus_huge_pages_node[nid]++; + spin_unlock(&hugetlb_lock); + } + } + + return ret; +} + +bool isolate_or_dissolve_huge_page(struct page *page) +{ + struct hstate *h = NULL; + struct page *head; + bool ret = false; + + spin_lock(&hugetlb_lock); + if (PageHuge(page)) { + head = compound_head(page); + h = page_hstate(head); + } + spin_unlock(&hugetlb_lock); + + /* + * The page might have been dissolved from under our feet. + * If that is the case, return success as if we dissolved it ourselves. + */ + if (!h) + return true; + + /* + * Fence off gigantic pages as there is a cyclic dependency + * between alloc_contig_range and them. + */ + if (hstate_is_gigantic(h)) + return ret; + + if(!page_count(head) && alloc_and_dissolve_huge_page(h, head)) + ret = true; + + return ret; +} + struct page *alloc_huge_page(struct vm_area_struct *vma, unsigned long addr, int avoid_reserve) { -- 2.16.3