From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 199111F7569 for ; Fri, 19 Dec 2025 09:00:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766134859; cv=none; b=Dhc2n532F8xoRfrQnh72A4ZpTxjUxvIH97U2CnouBbY6rg9x++xJL90QLIYnp1uyu6MaUscEL6wlIdOZc3lj5qhETV+VbJsG3neS/+SWhiOKpAwKYyt+OaLjGdViSgrY5l3NpO+Pb7d2YFNBckFrl4TpSYWebO2UBI3NYwf038o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766134859; c=relaxed/simple; bh=94/ju6t+/0jAwX5bMv+Fy0c4ul9j5y1hst+W53vgUis=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=EXZm5xajxx3fz+I4UYoB9fHq9d6CUXzQWihkl4nvpdrfxXTi8S7qKIyFfbumnUE622xd8XqWivdgy4djCVKaymqSvbohPRpaCMAGaiQdXjIIlwoN5t6LKWChiP6xc2YFz+5aQITsYdxUH70qnPyZ3a6bxwD/ir+bd5S+bPYDqgU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=X0TH5GsV; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="X0TH5GsV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A9597C4CEF1; Fri, 19 Dec 2025 09:00:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1766134858; bh=94/ju6t+/0jAwX5bMv+Fy0c4ul9j5y1hst+W53vgUis=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=X0TH5GsVRRkHvMW8e8oMJvYSg1DV6BW3dUIUqWUR9hqW3FPkPM+e1U83V3DKmvrfW sNHgNC52nVAqULjrYJuxWmbsOyHlzio3kPJ5KcGKdQ9yk9ac5tzdu6BTFPGujTT2Kw STT/1GuoKq7lQdhAeziARjfzRQpsvKKjscmsGKu2XDHH1OrHt2wnpoyS0lnZaDYtO7 1hGsF9BnqpxPMgtYsQB9YbJD8/RE/7DjYtNVqVNbzh39rmK5QU5IztEpa1ypNrnhbx wVlvvEfOWw9OW6bTLfd0TVkpt0BaAKXtayqpoqhuePorKtV2eNYf63nZGNb6PgBYBl njwwMP8e4TYXA== Message-ID: Date: Fri, 19 Dec 2025 10:00:53 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/4] mm: khugepaged: remove mm when all memory has been collapsed To: Vernon Yang Cc: akpm@linux-foundation.org, lorenzo.stoakes@oracle.com, ziy@nvidia.com, baohua@kernel.org, lance.yang@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Vernon Yang References: <20251215090419.174418-1-yanglincheng@kylinos.cn> <20251215090419.174418-3-yanglincheng@kylinos.cn> <26e65878-f214-4890-8bcb-24a45122bfd6@kernel.org> From: "David Hildenbrand (Red Hat)" Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 12/19/25 06:24, Vernon Yang wrote: > On Thu, Dec 18, 2025 at 10:29:18AM +0100, David Hildenbrand (Red Hat) wrote: >> On 12/15/25 10:04, Vernon Yang wrote: >>> The following data is traced by bpftrace on a desktop system. After >>> the system has been left idle for 10 minutes upon booting, a lot of >>> SCAN_PMD_MAPPED or SCAN_PMD_NONE are observed during a full scan by >>> khugepaged. >>> >>> @scan_pmd_status[1]: 1 ## SCAN_SUCCEED >>> @scan_pmd_status[4]: 158 ## SCAN_PMD_MAPPED >>> @scan_pmd_status[3]: 174 ## SCAN_PMD_NONE >>> total progress size: 701 MB >>> Total time : 440 seconds ## include khugepaged_scan_sleep_millisecs >>> >>> The khugepaged_scan list save all task that support collapse into hugepage, >>> as long as the take is not destroyed, khugepaged will not remove it from >>> the khugepaged_scan list. This exist a phenomenon where task has already >>> collapsed all memory regions into hugepage, but khugepaged continues to >>> scan it, which wastes CPU time and invalid, and due to >>> khugepaged_scan_sleep_millisecs (default 10s) causes a long wait for >>> scanning a large number of invalid task, so scanning really valid task >>> is later. >>> >>> After applying this patch, when all memory is either SCAN_PMD_MAPPED or >>> SCAN_PMD_NONE, the mm is automatically removed from khugepaged's scan >>> list. If the page fault or MADV_HUGEPAGE again, it is added back to >>> khugepaged. >> >> I don't like that, as it assumes that memory within such a process would be >> rather static, which is easily not the case (e.g., allocators just doing >> MADV_DONTNEED to free memory). >> >> If most stuff is collapsed to PMDs already, can't we just skip over these >> regions a bit faster? > > /* default scan 8*HPAGE_PMD_NR ptes (or vmas) every 10 second */ > static unsigned int khugepaged_pages_to_scan __read_mostly; > > The observed phenomenon is that when scanning these regions, the loop is > broken upon reaching the number of khugepaged_pages_to_scan, thereforce > the khugepaged enters 10s sleep. BTW, the 10s sleep is ridiculous :) I wonder whether we were more careful in the past regarding canning overhead due to the mmap read lock. Nowadays page faults typicaly use per-vma locks, so I wonder whether the scanning overhead is still a problem. (I assume there is more to optimize long-term) -- Cheers David