From patchwork Wed Jun 19 12:54:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ge Yang X-Patchwork-Id: 13703803 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD181C27C53 for ; Wed, 19 Jun 2024 12:54:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D8B86B0368; Wed, 19 Jun 2024 08:54:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 489376B036A; Wed, 19 Jun 2024 08:54:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 301BE6B036B; Wed, 19 Jun 2024 08:54:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0EDEF6B0368 for ; Wed, 19 Jun 2024 08:54:48 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 7C048411C3 for ; Wed, 19 Jun 2024 12:54:47 +0000 (UTC) X-FDA: 82247632614.08.FBD68DF Received: from m16.mail.126.com (m16.mail.126.com [117.135.210.8]) by imf09.hostedemail.com (Postfix) with ESMTP id 1162D140004 for ; Wed, 19 Jun 2024 12:54:44 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=ZcEAFWRk; spf=pass (imf09.hostedemail.com: domain of yangge1116@126.com designates 117.135.210.8 as permitted sender) smtp.mailfrom=yangge1116@126.com; dmarc=pass (policy=none) header.from=126.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718801677; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references:dkim-signature; bh=BWbJKJrKISUYvlz18rIPuVxukddjG6yGS92jiK8VMdY=; b=OIdnBWrzvVA0He/Lr615IMDAvF4iOGw1LJvNuzzGN4y71+fbnc0Kj4Gj97PoIT/LNf/6OB L+cpPNRJHng/RS8r97cKkcQZWgSIlMldMf2rJU0nXXLFCJD6qr9L3JV6Ji6OPPMml9LpnV KD3jjxzC9IbLuPHqpiW8og4Zd9w6Q+g= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718801677; a=rsa-sha256; cv=none; b=1q7G2Yge+KJ2pLv/g1skwWqTgeXzBv/AmUb47Ub29w6HTIoIWIUls5+CSwXPa6TMzHV0K+ gYTpIdhuYsGb4AL/IsXFa/68sHtE89rJtOLD9il8mtOsEhFaqk7V8s5T9sJzEQ5tWhjeOZ /tYCRrRXfQkaWwzXpqPnj2yjUKJnENs= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=ZcEAFWRk; spf=pass (imf09.hostedemail.com: domain of yangge1116@126.com designates 117.135.210.8 as permitted sender) smtp.mailfrom=yangge1116@126.com; dmarc=pass (policy=none) header.from=126.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=BWbJKJrKISUYvlz18r IPuVxukddjG6yGS92jiK8VMdY=; b=ZcEAFWRkIYOXRuPGREZuoEQj+PNYSi0amg cSW0B8HdBJDzv5fbY9WWmryADS60l+TmR8RRtNs0u8JdO//qoFg4rv+mhZu7yTH/ 6lfZ7Jq4wB4CIFbGwQSNvHH1bjx3QZSU9ckVBQQSl91j06q3BmxyOrad3OzGt8YF WCnlZuxmk= Received: from hg-OptiPlex-7040.hygon.cn (unknown [118.242.3.34]) by gzga-smtp-mta-g1-1 (Coremail) with SMTP id _____wCnd28K1XJmBrxyBQ--.10118S2; Wed, 19 Jun 2024 20:54:38 +0800 (CST) From: yangge1116@126.com To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, 21cnbao@gmail.com, baolin.wang@linux.alibaba.com, mgorman@techsingularity.net, liuzixing@hygon.cn, yangge Subject: [PATCH] mm/page_alloc: add one PCP list for THP Date: Wed, 19 Jun 2024 20:54:32 +0800 Message-Id: <1718801672-30152-1-git-send-email-yangge1116@126.com> X-Mailer: git-send-email 2.7.4 X-CM-TRANSID: _____wCnd28K1XJmBrxyBQ--.10118S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxAFWUtF17Xw18JF1UuFWDXFb_yoWrAr18pF WxJr4ayayjqryYyr1xA3Wqkr1rCwnxGFsrCrW8ury8ZwsxJFyS9a4UK3WqvF95ArW7AF48 XryDt34fCF4DZ3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UfhL5UUUUU= X-Originating-IP: [118.242.3.34] X-CM-SenderInfo: 51dqwwjhrrila6rslhhfrp/1tbiOg8DG2VEw410VQAAsj X-Stat-Signature: fe8kg8oone6kb6n5wtg456accgxtb34h X-Rspamd-Queue-Id: 1162D140004 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1718801684-587784 X-HE-Meta: U2FsdGVkX18gauQwXVvkE7UiD2+YjVmeOjgOr6PSukrdevADQIGkQlIntl2hWWZsqvgUwGYamOOUoby4TMKv1pRfQUAcvwufNjKOK0IHfosiTMxLzmCpeqS+BCj6QamTGtWK5HlT3lj6rg1AZGG/3EZeqRyQwyajr8ceJpyC7H+uv0ewQCQ/9OnPsZrHjuFFxZ9MWj5NgeVvvagm9FVKOMaCVu6l8vxFpz8P8oXJHYFMKZCRyvcyBTTXCfY8dha4OdYV3ro3dnxoFLOt3oh5CuNb8LKfbyvgaeqUWV3Z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bmDEJ9BkSKE2DCWkxQIJ5z0H22ed6ZxTRtrW2KpotxeYNO29Cd8A3mQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: yangge Since commit 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations") no longer differentiates the migration type of pages in THP-sized PCP list, it's possible that non-movable allocation requests may get a CMA page from the list, in some cases, it's not acceptable. If a large number of CMA memory are configured in system (for example, the CMA memory accounts for 50% of the system memory), starting a virtual machine with device passthrough will get stuck. During starting the virtual machine, it will call pin_user_pages_remote(..., FOLL_LONGTERM, ...) to pin memory. Normally if a page is present and in CMA area, pin_user_pages_remote() will migrate the page from CMA area to non-CMA area because of FOLL_LONGTERM flag. But if non-movable allocation requests return CMA memory, migrate_longterm_unpinnable_pages() will migrate a CMA page to another CMA page, which will fail to pass the check in check_and_migrate_movable_pages() and cause migration endless. Call trace: pin_user_pages_remote --__gup_longterm_locked // endless loops in this function ----_get_user_pages_locked ----check_and_migrate_movable_pages ------migrate_longterm_unpinnable_pages --------alloc_migration_target This problem will also have a negative impact on CMA itself. For example, when CMA is borrowed by THP, and we need to reclaim it through cma_alloc() or dma_alloc_coherent(), we must move those pages out to ensure CMA's users can retrieve that contigous memory. Currently, CMA's memory is occupied by non-movable pages, meaning we can't relocate them. As a result, cma_alloc() is more likely to fail. To fix the problem above, we add one PCP list for THP, which will not introduce a new cacheline for struct per_cpu_pages. THP will have 2 PCP lists, one PCP list is used by MOVABLE allocation, and the other PCP list is used by UNMOVABLE allocation. MOVABLE allocation contains GPF_MOVABLE, and UNMOVABLE allocation contains GFP_UNMOVABLE and GFP_RECLAIMABLE. Fixes: 5d0a661d808f ("mm/page_alloc: use only one PCP list for THP-sized allocations") Signed-off-by: yangge --- include/linux/mmzone.h | 9 ++++----- mm/page_alloc.c | 9 +++++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index b7546dd..cb7f265 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -656,13 +656,12 @@ enum zone_watermarks { }; /* - * One per migratetype for each PAGE_ALLOC_COSTLY_ORDER. One additional list - * for THP which will usually be GFP_MOVABLE. Even if it is another type, - * it should not contribute to serious fragmentation causing THP allocation - * failures. + * One per migratetype for each PAGE_ALLOC_COSTLY_ORDER. Two additional lists + * are added for THP. One PCP list is used by GPF_MOVABLE, and the other PCP list + * is used by GFP_UNMOVABLE and GFP_RECLAIMABLE. */ #ifdef CONFIG_TRANSPARENT_HUGEPAGE -#define NR_PCP_THP 1 +#define NR_PCP_THP 2 #else #define NR_PCP_THP 0 #endif diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 8f416a0..0a837e6 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -504,10 +504,15 @@ static void bad_page(struct page *page, const char *reason) static inline unsigned int order_to_pindex(int migratetype, int order) { + bool __maybe_unused movable; + #ifdef CONFIG_TRANSPARENT_HUGEPAGE if (order > PAGE_ALLOC_COSTLY_ORDER) { VM_BUG_ON(order != HPAGE_PMD_ORDER); - return NR_LOWORDER_PCP_LISTS; + + movable = migratetype == MIGRATE_MOVABLE; + + return NR_LOWORDER_PCP_LISTS + movable; } #else VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER); @@ -521,7 +526,7 @@ static inline int pindex_to_order(unsigned int pindex) int order = pindex / MIGRATE_PCPTYPES; #ifdef CONFIG_TRANSPARENT_HUGEPAGE - if (pindex == NR_LOWORDER_PCP_LISTS) + if (pindex >= NR_LOWORDER_PCP_LISTS) order = HPAGE_PMD_ORDER; #else VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER);