From nobody Sat Feb 7 11:39:03 2026 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6100333B97D for ; Fri, 23 Jan 2026 08:23:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769156593; cv=none; b=U7pQbsycMJ+mb/feusW4OqNxKxWUvSnqBSXPGG3kSg5fDPbU7jmwdRKvEqYhfM0J4Co+lCdZ2J9w8LYtjXWRcbdls6CwIs1xP1BKwQ2nCG9tlhhDUmAPLFWpNNjEPiTIrAZaSGah9LJN3tPzRPce0OAK/Y3qwIKf1NhyZAA/ZbE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769156593; c=relaxed/simple; bh=exL1bWSKIR70ViPzmj/Xtp4Z05YHqWI7lOre6cPxfLs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YJXrt9/xQX5M7AIPnNHcktv0e1AM8fv1vg4U5YW7iodhjj+eIATMSjf9UCdFX436PqBR3vzZo/D3+rrhtYtCGz4UM/X7O8z3FhL+RTCWDOHrwgZg9wSIP2SwmnHF3OVCHgJaI0iM4fVGWhmRFw7m8SIOUI4aFm6EnJHx1cWK8K4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dXjgG4O6; arc=none smtp.client-ip=209.85.210.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dXjgG4O6" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-8230c839409so1413546b3a.3 for ; Fri, 23 Jan 2026 00:23:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769156590; x=1769761390; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0NYRDoQ/3A47f1BvErhk2cyyDCVpwgJBfQUk6OyEvgE=; b=dXjgG4O6SZ3QhkempR2o3+PwJ+DjEiXwCsZhyS+tRNzQ8RqOMvz//Lmr2OJLmICv4p jzZlYuchgGLghFwOHzeuvOpCNBy9bsXon48e8jFRghmnl9ULKYuBRXGJFulZvoqJQJtq 689A+x6iF7v+/hiI57VXJD1A5I9thC2U9wBR505LPMvqlwXmuc6VRIJcdZdv5rkCrQpT 1f+R8rHjZ/E+9lZJ9Ghgvd48aI2xHCCL+CqIL8h8NQ9FmKC/lUGKQRKM79SCxCgoqd5Q pPxUTaD5CU4CvzfjyTA0f88N56XOCMklvTrksIIcl38PMcajmN7An7hcJXRg7ohb8GEz 2lVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769156590; x=1769761390; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=0NYRDoQ/3A47f1BvErhk2cyyDCVpwgJBfQUk6OyEvgE=; b=KTmdgoAw59Vga54QnOuQWD8zg9zGNXR87l0lp0MPfgdyS/h8kTReXL4VEo+JybDggP oH0te5ZOKirMQBwtjBaw/p3nIi2L/4boQYktGdAJKdPFligXzu+W9RB0HKXPHPAaYfqG jCdsCarT4gmElEShVfCnpQAkC3M0Sz45VvwFP+KDEcD3GedNLbXo1NuLgR7PIkDYxRmA vBPyq0SR6R/P6T9gDlVx6J9Hsy0kzd8CLw+MTkx3cIr62x2p3VfyzVZxegBEO5hgc7io mr1O+B/y8CkACdL/pmf5RhfNinpALcUUA7/g3hfW3+mFThici92mKQ2+Lw6Kct/4V20y IaRQ== X-Forwarded-Encrypted: i=1; AJvYcCV25RKsbx9t5dL95BrjG5UcOZtHSFhDvClO8Hn/aJDqQsjjRtaZAJIuF6ox8Zeypu8NZfaYN8RoHY+myWA=@vger.kernel.org X-Gm-Message-State: AOJu0YwjrqSdWNcsVwi1+9IQV04XpYk+oem2e9kRWdNxpMVjstMjlhRL rDumkPRjHhju82DV1mBsY+6Vj/EUmBzVPtS3SWQUrrEDkZO0erKIrEnj X-Gm-Gg: AZuq6aIMCXHabu50eMIgbJAvdQFJUzpJbiqFNM02DyW6XrtrA9MSBW2Noo4OWa8bRTr Y+I4vgCjg6UEoXV1RfvqZZivlScOomqQy9k50qpXLENzs8awcC54FR2MbS6DvxfHE1Lo5Tmf6QO e69yOLo8b0FGpSsU1RlmlP5jiuMpzNjEjY7FapA0F9E0q+52DiEmGeZDZi+5Pfuml8BUuhcFo8k xhdXgFl3K+jNLwSo16FyEOxeIWa6iAql+mzxq/OlescyZqAwxGOH2TS6J5If3cdYGR8ZGwnER4K NLbhLvd0BmPBYQPhYmRo13ElZ1FSCK9sa6i27TUPb4kGVruBdUawomc2VAHnw0yF1aBFdNkxoeU No9ORHX6CFW9v+ojEm78NQN7dVLmUcQUlCETSRgWONhh91/dnsGeLye1pankCzfEGTmRtFOfG3Q kt0AtmsNMoxXdazps00yoUK/4Cz22LxO3uH7Y= X-Received: by 2002:a05:6a21:33aa:b0:38b:eb25:7741 with SMTP id adf61e73a8af0-38e6f6a6badmr2287356637.1.1769156590493; Fri, 23 Jan 2026 00:23:10 -0800 (PST) Received: from localhost.localdomain ([221.227.246.159]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c635a43f11csm1348363a12.35.2026.01.23.00.23.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jan 2026 00:23:10 -0800 (PST) From: Vernon Yang To: akpm@linux-foundation.org, david@kernel.org Cc: lorenzo.stoakes@oracle.com, ziy@nvidia.com, dev.jain@arm.com, baohua@kernel.org, lance.yang@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Vernon Yang Subject: [PATCH mm-new v5 4/5] mm: khugepaged: skip lazy-free folios Date: Fri, 23 Jan 2026 16:22:31 +0800 Message-ID: <20260123082232.16413-5-vernon2gm@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260123082232.16413-1-vernon2gm@gmail.com> References: <20260123082232.16413-1-vernon2gm@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vernon Yang 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. And if we collapse with a lazyfree page, that content will never be none and the deferred shrinker cannot reclaim them. 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 | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge= _memory.h index 384e29f6bef0..bcdc57eea270 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 de95029e3763..be1c09842ea2 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, @@ -583,6 +584,11 @@ static enum scan_result __collapse_huge_page_isolate(s= truct vm_area_struct *vma, folio =3D page_folio(page); VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio); =20 + if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) { + result =3D SCAN_PAGE_LAZYFREE; + goto out; + } + /* See hpage_collapse_scan_pmd(). */ if (folio_maybe_mapped_shared(folio)) { ++shared; @@ -1330,6 +1336,11 @@ static enum scan_result hpage_collapse_scan_pmd(stru= ct mm_struct *mm, } folio =3D page_folio(page); =20 + if (!pte_dirty(pteval) && folio_test_lazyfree(folio)) { + result =3D SCAN_PAGE_LAZYFREE; + goto out_unmap; + } + if (!folio_test_anon(folio)) { result =3D SCAN_PAGE_ANON; goto out_unmap; --=20 2.51.0