From nobody Sun Feb 8 22:58:22 2026 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 177D333986D for ; Sun, 1 Feb 2026 12:26:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769948818; cv=none; b=lUYBxxCSK38xx9b9fzX1nSsAjSdJS12TRFIju8Btj5B4Ebr781X8vhaNIQ8FZb/9v/yfGHBK7EG858ZM3+GOrJCrtK4iVyiUU3eHL26tyTK5NFB9dP5fCYdDPwFAJhipriEuPZC/r48heDBcPvLkOhKOY1/SeeKVxsPUB6aTDTk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769948818; c=relaxed/simple; bh=mcYHQF7V8hw0U8tFlKOZdDI846s1K/YXmZZhLqUi7TQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r0LcxU9xflgCeEJBtiQoEVhf4ZuB4Lt4uvYxIQiS5LIsCsqMjjWqISuw47PSx6XA/rzqlW6Yy1LD4cwSfGv7nSegoBsueALW9hMJLwWzXbNsnSGcgOOuglYrlRKRC7oMrXafb37eoa+7pFcyWW3FotHjVkd/QATY8OQUSpLa9YA= 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=SSMVz2+0; arc=none smtp.client-ip=209.85.214.179 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="SSMVz2+0" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2a7a9b8ed69so37403145ad.2 for ; Sun, 01 Feb 2026 04:26:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769948816; x=1770553616; 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=g1KIjvX7rp1rAqsSo7NpZu+Xr3OmRRK8GikF7hwaL00=; b=SSMVz2+07rAdn3h8HLPK5qfJfKMoGsQs4n1EQpvDgOFcNjacmWWeMYGYJ5XHAqv7F5 92YArhQyH5kPOc/iKEatQBU5mrmXBNcBZiJCuToevGqU++dwp5V76V3Bk8TeH8RWxGDu gC/6Xv9pFbJnLFp3OyD3LWvTdyIeSu7N9/3rIgfIp2Y6ugL7BGbbmks7h9tuygTaPHYG sgUhdVc+tyY14r4GYZBhb06dDzfXgedap2oXDw+S8gKHWvqvhc8iqHw3fLmSIHSwQ9wP 5aGyf6G/tu8mlhYYMT5pptpe4d7GASkWwOkHdRJFACkxNN8fexiFZkt+0pHpzE1sy7M3 qzMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769948816; x=1770553616; 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=g1KIjvX7rp1rAqsSo7NpZu+Xr3OmRRK8GikF7hwaL00=; b=wEQc35DK6KbU3MOmWESsE/XauEl/fHubMGZhNNuRP+V3eWMFwVAaGfG6SZIhC3GVuX rNLml2qxxJKHCrgFsqdy8AVPFHBU8PbwxRPDOOamnjQnTZlp9SGhwP/GlrSv5MG8J6ZH As/sX4zlLdDGoJ90zuQcYm4AFDd2jZBdrgkD1+vakHsTKqyWQeWmmKAgLzAsV3VNtuCo piWmt+FX8Fx0mvgr0JE9jgyqoYG3PiCNCr4dZ6oF4JL7blyVguG1e8gGusQ9LoZC1Sqn nabIEWsxelV26Chgkq3WKoEL2sXHnmo6B+aDe3jGByDyZnM75Oft+DgzA86T1YqfWQCZ 4fGg== X-Forwarded-Encrypted: i=1; AJvYcCVXuv/VUlcw23YWBN5MOYBNofVofthpbM5Kov26lsPndWvQH85O0zO5/19voxR0Y2tsMP7BV0e7YXFcTDs=@vger.kernel.org X-Gm-Message-State: AOJu0YwgH5P1EsARUI/FdzBPFBFmk7Vct5P23ACUza1ioyTMknFFWxR/ 8aSiyDPIOkdc3NZtZT4jd8d//ag7FzFncwvsxKeEYH6oqDyU5PFhOWhh X-Gm-Gg: AZuq6aKBT5I8eNuF4JV/KJQPbnFoTWjw5bO503HzRi5m/i5xbFXZmZm8vYFT/I4FUgq AUnDf9I5lNah8xukZtkitLLxaGZnB4DxODpoS65evOL0XmcuAxMWwLUwNUsUkOIZ0iRUzYR1btl riE/dNhfDTwwgf9rce2u7rWgC5yNfBIG6CnegoFfSsrJdfK1+AoXctYRfTiy0yGqp+vjRq1z/GF v22wr85J3I+V4b86QWmd+Lu1VU3K4mE1SyyOjnFoC2IaufQf5N+TrFU5TpDTghwAU+stGReuofJ gJJ1NxBBVUnN7gk4JDRwxhl+7hgTrf43e8/WOEyIT6gWipgEqkf4/u1OsQP3fkuItRBEL3KTxW4 l/oJazJ5RKDrTRj9XaQh1fHn+NZTpUzMvDNZJjuIaZm2D18vTq4aZ6E1MhSjD9hDl+ZggAoDg3r wbKO62u+LEYaFHTHN7Sta2LbJV X-Received: by 2002:a17:903:1b10:b0:2a0:b02b:210c with SMTP id d9443c01a7336-2a8d96a6e47mr88899225ad.1.1769948816454; Sun, 01 Feb 2026 04:26:56 -0800 (PST) Received: from localhost.localdomain ([114.231.118.96]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a88b6e5ac4sm116941185ad.86.2026.02.01.04.26.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Feb 2026 04:26:56 -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 v6 4/5] mm: khugepaged: skip lazy-free folios Date: Sun, 1 Feb 2026 20:25:53 +0800 Message-ID: <20260201122554.1470071-5-vernon2gm@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260201122554.1470071-1-vernon2gm@gmail.com> References: <20260201122554.1470071-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 Acked-by: David Hildenbrand (arm) Reviewed-by: Lance Yang --- include/trace/events/huge_memory.h | 1 + mm/khugepaged.c | 13 +++++++++++++ 2 files changed, 14 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 df22b2274d92..b4def001ccd0 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,12 @@ 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 (cc->is_khugepaged && !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; @@ -1332,6 +1339,12 @@ static enum scan_result hpage_collapse_scan_pmd(stru= ct mm_struct *mm, } folio =3D page_folio(page); =20 + if (cc->is_khugepaged && !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