From mboxrd@z Thu Jan 1 00:00:00 1970 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 80FCBE7717D for ; Fri, 13 Dec 2024 08:41:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E1C796B0083; Fri, 13 Dec 2024 03:41:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DCC4E6B0085; Fri, 13 Dec 2024 03:41:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C93CD6B0088; Fri, 13 Dec 2024 03:41:41 -0500 (EST) 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 ACEB46B0083 for ; Fri, 13 Dec 2024 03:41:41 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D1D151C7C46 for ; Fri, 13 Dec 2024 08:41:40 +0000 (UTC) X-FDA: 82889291772.15.A1C34CF Received: from m16.mail.126.com (m16.mail.126.com [117.135.210.6]) by imf03.hostedemail.com (Postfix) with ESMTP id 2615320008 for ; Fri, 13 Dec 2024 08:41:25 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b="ZtMM5O/y"; spf=pass (imf03.hostedemail.com: domain of yangge1116@126.com designates 117.135.210.6 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=1734079287; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3VHdoh6qZKjjSI6N1JNEN/E9xuIJhplBCcKSHl3u1Ko=; b=NF22LZFcHatb0s/pbO2Xej1iwZ+VLyjdEG10VRJtCsRaHh8oPniq767DM3inKYVxqqe3SH gucxlKR8zRVlptzOBdIO31a7dqrlCGPQ4+42Qh9pihWJsOhRj+Zp87LqrY8zskmniGXbgK j5RDj32r+hqO0uhcIcW5Dm5/hnj14yE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734079287; a=rsa-sha256; cv=none; b=EP0Pj3zVM+c1IBQAhV83mgm8NGnjxKi6sWe/o+zBd7Fn/9KRPwS/+vMnzk1xLQ+6W38IGm UvkZeRKN1MhETGwCf+p98arUVwazre1c5WUIRRCWaEEgmRMd3bCf/zRNJnuPwaesGEDHAr cnWDm+cI4cjTG5ClQsW/P17obILKjUw= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b="ZtMM5O/y"; spf=pass (imf03.hostedemail.com: domain of yangge1116@126.com designates 117.135.210.6 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=Message-ID:Date:MIME-Version:Subject:From: Content-Type; bh=3VHdoh6qZKjjSI6N1JNEN/E9xuIJhplBCcKSHl3u1Ko=; b=ZtMM5O/y7zpgxgY5AAAxqm88/dVuNPPVSONvrQBVPj1mA/piH7jTBp9Y992Amy BQPdRZjDaX6+dJKMvYAsnJaF3wWGcfFwykfTCYApD1iLmsJwfKFmbn+798A+dO2H 84fq3WAolLKK88C4SYQWEWOuRV4fWBP5GoOm9oHnnIq48= Received: from [172.21.22.210] (unknown [118.242.3.34]) by gzsmtp2 (Coremail) with SMTP id pikvCgD3P6o181tn9vusCQ--.21428S2; Fri, 13 Dec 2024 16:41:26 +0800 (CST) Message-ID: <766c24df-3dbc-4b15-bd3a-3051a0b5f3ee@126.com> Date: Fri, 13 Dec 2024 16:41:25 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] mm, compaction: don't use ALLOC_CMA in long term GUP flow To: Barry Song <21cnbao@gmail.com> Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, david@redhat.com, baolin.wang@linux.alibaba.com, vbabka@suse.cz, liuzixing@hygon.cn References: <1734075432-14131-1-git-send-email-yangge1116@126.com> From: Ge Yang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:pikvCgD3P6o181tn9vusCQ--.21428S2 X-Coremail-Antispam: 1Uf129KBjvJXoW3Jr4DGFy5GrW8Jr47GFy7Awb_yoW7Xw47pF 4xA3Wqyws5XFy3Cr18tw4v9F4Yvw4xKF45Gr9Fqr1DuwnIkF9a9F1kKFyUZFW5ur1akw4Y qFWq9asrZFsxZaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07Uq1v3UUUUU= X-Originating-IP: [118.242.3.34] X-CM-SenderInfo: 51dqwwjhrrila6rslhhfrp/1tbiOh20G2db5cfQGwABsc X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 2615320008 X-Stat-Signature: hpmksn34g75t4tyois5rnwmkk1ngx1z7 X-Rspam-User: X-HE-Tag: 1734079285-851205 X-HE-Meta: U2FsdGVkX18eQljBxs6QzFhnN5vDWaVTKCKr+DIkdRAgy2AoayspTTVSNNOYlBCK/gwVzzt8oS4BWKkOM2TPCDYCn9Vp1Qb0OSsdsdfCPnv+OLmn+FjU6RtLK7bALmicR/dOo7uH7n9hB5k/bDJEjZTsW4gKqtXREhpqLsisGidqhiqa+eZMTxEu9WZ+ubALFBJ6ovo8Fnu2BZOhrFl4VLYMgmMzIqPWHpZJwZgnTf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IL3H8hfufrirlR7StUa/SKsrUwoDSKoRQsIq66UEOV6Z9s78W7ra2g8OZ+CU5G0rzhTV/zvNaqUD6fYR/U6zt29bOZnkFh+sBv3LT7jWfWJNfjWZe0cgxfTyKsHBSK9gPRSoZT5Z+g4Y8yVznDtCc42LRrjr0N+o4C5bx2S3ve4Wzmxfd9Lxfm6MVsrMJZrW2oyfqpdf//hdZ5xnsRA+PWtuVAO 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: 在 2024/12/13 15:56, Barry Song 写道: > On Fri, Dec 13, 2024 at 3:37 PM wrote: >> >> From: yangge >> >> Since commit 984fdba6a32e ("mm, compaction: use proper alloc_flags >> in __compaction_suitable()") allow compaction to proceed when free >> pages required for compaction reside in the CMA pageblocks, it's >> possible that __compaction_suitable() always returns true, and in >> some cases, it's not acceptable. >> >> There are 4 NUMA nodes on my machine, and each NUMA node has 32GB >> of memory. I have configured 16GB of CMA memory on each NUMA node, >> and starting a 32GB virtual machine with device passthrough is >> extremely slow, taking almost an hour. > > I don't fully understand why each node has a 16GB CMA. As I recall, I designed > the per-NUMA CMA to support devices that are not behind the IOMMU, such as > the IOMMU itself or certain device drivers which are not having IOMMU and > need contiguous memory for DMA. These devices don't seem to require that > much memory. Our hardware supports setting specific protection for contiguous memory block, but the granularity of protection is relatively large, exceeding 4MB, which makes it unsuitable for allocation from buddy. Therefore, during system startup, a certain percentage of memory on each node is reserved as CMA memory, allowing for the allocation of large contiguous memory blocks through cma_alloc. > >> >> During the start-up of the virtual machine, it will call >> pin_user_pages_remote(..., FOLL_LONGTERM, ...) to allocate memory. >> Long term GUP cannot allocate memory from CMA area, so a maximum >> of 16 GB of no-CMA memory on a NUMA node can be used as virtual >> machine memory. Since there is 16G of free CMA memory on the NUMA >> node, watermark for order-0 always be met for compaction, so >> __compaction_suitable() always returns true, even if the node is >> unable to allocate non-CMA memory for the virtual machine. >> >> For costly allocations, because __compaction_suitable() always >> returns true, __alloc_pages_slowpath() can't exit at the appropriate >> place, resulting in excessively long virtual machine startup times. >> Call trace: >> __alloc_pages_slowpath >> if (compact_result == COMPACT_SKIPPED || >> compact_result == COMPACT_DEFERRED) >> goto nopage; // should exit __alloc_pages_slowpath() from here >> >> To sum up, during long term GUP flow, we should remove ALLOC_CMA >> both in __compaction_suitable() and __isolate_free_page(). > > What’s the outcome after your fix? Will it quickly fall back to remote > NUMA nodes > for the pin? Starting a 32GB virtual machine with device passthrough takes only a free seconds. Yes, it will quickly fall back to remote NUMA nodes. > >> >> Fixes: 984fdba6a32e ("mm, compaction: use proper alloc_flags in __compaction_suitable()") >> Cc: >> Signed-off-by: yangge >> --- >> mm/compaction.c | 8 +++++--- >> mm/page_alloc.c | 4 +++- >> 2 files changed, 8 insertions(+), 4 deletions(-) >> >> diff --git a/mm/compaction.c b/mm/compaction.c >> index 07bd227..044c2247 100644 >> --- a/mm/compaction.c >> +++ b/mm/compaction.c >> @@ -2384,6 +2384,7 @@ static bool __compaction_suitable(struct zone *zone, int order, >> unsigned long wmark_target) >> { >> unsigned long watermark; >> + bool pin; >> /* >> * Watermarks for order-0 must be met for compaction to be able to >> * isolate free pages for migration targets. This means that the >> @@ -2395,14 +2396,15 @@ static bool __compaction_suitable(struct zone *zone, int order, >> * even if compaction succeeds. >> * For costly orders, we require low watermark instead of min for >> * compaction to proceed to increase its chances. >> - * ALLOC_CMA is used, as pages in CMA pageblocks are considered >> - * suitable migration targets >> + * In addition to long term GUP flow, ALLOC_CMA is used, as pages in >> + * CMA pageblocks are considered suitable migration targets >> */ >> watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ? >> low_wmark_pages(zone) : min_wmark_pages(zone); >> watermark += compact_gap(order); >> + pin = !!(current->flags & PF_MEMALLOC_PIN); >> return __zone_watermark_ok(zone, 0, watermark, highest_zoneidx, >> - ALLOC_CMA, wmark_target); >> + pin ? 0 : ALLOC_CMA, wmark_target); >> } >> >> /* >> diff --git a/mm/page_alloc.c b/mm/page_alloc.c >> index dde19db..9a5dfda 100644 >> --- a/mm/page_alloc.c >> +++ b/mm/page_alloc.c >> @@ -2813,6 +2813,7 @@ int __isolate_free_page(struct page *page, unsigned int order) >> { >> struct zone *zone = page_zone(page); >> int mt = get_pageblock_migratetype(page); >> + bool pin; >> >> if (!is_migrate_isolate(mt)) { >> unsigned long watermark; >> @@ -2823,7 +2824,8 @@ int __isolate_free_page(struct page *page, unsigned int order) >> * exists. >> */ >> watermark = zone->_watermark[WMARK_MIN] + (1UL << order); >> - if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA)) >> + pin = !!(current->flags & PF_MEMALLOC_PIN); >> + if (!zone_watermark_ok(zone, 0, watermark, 0, pin ? 0 : ALLOC_CMA)) >> return 0; >> } >> >> -- >> 2.7.4 >> > Thanks > Barry