From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D465939902A for ; Wed, 14 Jan 2026 12:45:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768394713; cv=none; b=pErB1GX3eR+Si19EVO5HvPshn9NOO4x3WCQ12emzJLTlORPHDn1kCc5nV9FkFmI68+g8L0JWoBlK1LBsdQXFW39/KKls4C498an64/8kH7e17+2xH+CkrbwYpxRpNEs3ExBOFrzJfL7XGQvh7M8ltGGiMtVxu7hW+UcT+OwBvpY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768394713; c=relaxed/simple; bh=M1+A6oStwvyCOUSWstSKISjW6VNC6ruSeGkuSsSjfAc=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=HKECyxSc/nd9nRAeWIpoJLsppRsb/k9mSz511rPvz2VW9OoXUAPUyq0n3MX5wEfN0D2CFMqj7SsSaRI7XCcJ/a21tZTRpU4avqO0N56ApsWl6ee3hPCKAeIzWAFLq2la2n+wul+27XyDI2PLd8+To5hZXYVhn0LIEIJBX3jrstg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=pPDRJ5h4; arc=none smtp.client-ip=91.218.175.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="pPDRJ5h4" Message-ID: <56c004dd-fe54-42a7-a8a0-38aeaf97c8c4@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1768394705; h=from:from: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; bh=Kkx9U6E3R5XQonI/fhBV4DP/DMljouvF3+/pnXfZpGk=; b=pPDRJ5h4aaAaWS8SPzqMcI9dLtCOQsHPUFI0aChPzmXXOfA4rNIbXcdqBxhPt3mafVhoKM vAbjusKIDRJjPNHcPWblUaINahoJbPGFML3j2S9Kl/xU/xL5e/ZsYMKpCbVUFrgJGDNbVU VF+zYeNTeCpZzEIJjjYvYMVSHL/rFnI= Date: Wed, 14 Jan 2026 20:44:52 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH mm-new v4 5/6] mm: khugepaged: skip lazy-free folios at scanning Content-Language: en-US To: "David Hildenbrand (Red Hat)" , Vernon Yang Cc: lorenzo.stoakes@oracle.com, ziy@nvidia.com, dev.jain@arm.com, baohua@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Vernon Yang , akpm@linux-foundation.org References: <20260111121909.8410-1-yanglincheng@kylinos.cn> <20260111121909.8410-6-yanglincheng@kylinos.cn> <06c2e619-0e60-4e57-b2ea-37333b2f6f5d@kernel.org> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Lance Yang In-Reply-To: <06c2e619-0e60-4e57-b2ea-37333b2f6f5d@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT On 2026/1/14 19:50, David Hildenbrand (Red Hat) wrote: > On 1/11/26 13:19, Vernon Yang wrote: >> For example, create three task: hot1 -> cold -> hot2. After all three >> task are created, each allocate memory 128MB. the hot1/hot2 task >> continuously access 128 MB memory, while the cold task only accesses >> its memory briefly andthen call madvise(MADV_FREE). However, khugepaged >> still prioritizes scanning the cold task and only scans the hot2 task >> after completing the scan of the cold task. >> >> So if the user has explicitly informed us via MADV_FREE that this memory >> will be freed, it is appropriate for khugepaged to skip it only, thereby >> avoiding unnecessary scan and collapse operations to reducing CPU >> wastage. >> >> Here are the performance test results: >> (Throughput bigger is better, other smaller is better) >> >> Testing on x86_64 machine: >> >> | task hot2           | without patch | with patch    |  delta  | >> |---------------------|---------------|---------------|---------| >> | total accesses time |  3.14 sec     |  2.93 sec     | -6.69%  | >> | cycles per access   |  4.96         |  2.21         | -55.44% | >> | Throughput          |  104.38 M/sec |  111.89 M/sec | +7.19%  | >> | dTLB-load-misses    |  284814532    |  69597236     | -75.56% | >> >> Testing on qemu-system-x86_64 -enable-kvm: >> >> | task hot2           | without patch | with patch    |  delta  | >> |---------------------|---------------|---------------|---------| >> | total accesses time |  3.35 sec     |  2.96 sec     | -11.64% | >> | cycles per access   |  7.29         |  2.07         | -71.60% | >> | Throughput          |  97.67 M/sec  |  110.77 M/sec | +13.41% | >> | dTLB-load-misses    |  241600871    |  3216108      | -98.67% | >> >> Signed-off-by: Vernon Yang >> --- >>   include/trace/events/huge_memory.h |  1 + >>   mm/khugepaged.c                    | 17 +++++++++++++++++ >>   2 files changed, 18 insertions(+) >> >> diff --git a/include/trace/events/huge_memory.h b/include/trace/ >> events/huge_memory.h >> index 3d1069c3f0c5..e3856f8ab9eb 100644 >> --- a/include/trace/events/huge_memory.h >> +++ b/include/trace/events/huge_memory.h >> @@ -25,6 +25,7 @@ >>       EM( SCAN_PAGE_LRU,        "page_not_in_lru")        \ >>       EM( SCAN_PAGE_LOCK,        "page_locked")            \ >>       EM( SCAN_PAGE_ANON,        "page_not_anon")        \ >> +    EM( SCAN_PAGE_LAZYFREE,        "page_lazyfree")        \ >>       EM( SCAN_PAGE_COMPOUND,        "page_compound")        \ >>       EM( SCAN_ANY_PROCESS,        "no_process_for_page")        \ >>       EM( SCAN_VMA_NULL,        "vma_null")            \ >> diff --git a/mm/khugepaged.c b/mm/khugepaged.c >> index 6df2857d94c6..8a7008760566 100644 >> --- a/mm/khugepaged.c >> +++ b/mm/khugepaged.c >> @@ -46,6 +46,7 @@ enum scan_result { >>       SCAN_PAGE_LRU, >>       SCAN_PAGE_LOCK, >>       SCAN_PAGE_ANON, >> +    SCAN_PAGE_LAZYFREE, >>       SCAN_PAGE_COMPOUND, >>       SCAN_ANY_PROCESS, >>       SCAN_VMA_NULL, >> @@ -1258,6 +1259,7 @@ static enum scan_result >> hpage_collapse_scan_pmd(struct mm_struct *mm, >>       pmd_t *pmd; >>       pte_t *pte, *_pte; >>       int none_or_zero = 0, shared = 0, referenced = 0; >> +    int lazyfree = 0; >>       enum scan_result result = SCAN_FAIL; >>       struct page *page = NULL; >>       struct folio *folio = NULL; >> @@ -1343,6 +1345,21 @@ static enum scan_result >> hpage_collapse_scan_pmd(struct mm_struct *mm, >>           } >>           folio = page_folio(page); >> +        if (cc->is_khugepaged && !pte_dirty(pteval) && >> +            folio_is_lazyfree(folio)) { >> +            ++lazyfree; >> + >> +            /* >> +             * The lazyfree folios are reclaimed and become pte_none. >> +             * Ensure they do not continue to be collapsed when >> +             * skipped ahead. >> +             */ >> +            if ((lazyfree + none_or_zero) > khugepaged_max_ptes_none) { >> +                result = SCAN_PAGE_LAZYFREE; >> +                goto out_unmap; > > I dislike adding another khugepaged_max_ptes_none check. Gah. > > > Can't we should just keep it simple and do > > if (!pte_dirty(pteval) && folio_is_lazyfree(folio)) { >     result = SCAN_PAGE_LAZYFREE; >     goto out_unmap; > } > > Reasoning: once they are none, we have a zero-filled page that e.g., the > deferred shrinker can reclaim. > > If you collapse with a lazyfree page, that content will never be none > and the deferred shrinker cannot reclaim them. > > So there is a real difference between them being none and them still > being around. > > > We could also try turning them into none entries here, that is, test of > we can discard them, to then just threat them like none entries. Right, I would prefer turning them into none entries, but that seems to complicate things a bit, e.g., making sure we don't copy content from them during collapse ... So let's keep it simple: just bail out if the page is lazyfree and clean :) > > > Why don't we want to similarly handle this in > __collapse_huge_page_isolate() ? Yeah, that should be added there as well.