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 C8DFEE77188 for ; Fri, 10 Jan 2025 04:56:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2F7FD6B007B; Thu, 9 Jan 2025 23:56:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2A7BF6B0082; Thu, 9 Jan 2025 23:56:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1975E6B0083; Thu, 9 Jan 2025 23:56:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F0B486B007B for ; Thu, 9 Jan 2025 23:56:35 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9EEB3AF2AF for ; Fri, 10 Jan 2025 04:56:35 +0000 (UTC) X-FDA: 82990331550.10.CA8DD8C Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf27.hostedemail.com (Postfix) with ESMTP id AC87240003 for ; Fri, 10 Jan 2025 04:56:33 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf27.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736484994; a=rsa-sha256; cv=none; b=rXHcjzF0SBR8UqxzXgW+8s09aiLO0sQdHljouGAlJBSu5Qt+OPjRqiyq3RQ7Gi/H/SLyLS jye93xy1LAEBLZq/VCRPQsX7x15Tqqi+vH24G0vicJp7hfok+sCwlIhPVYKWS7tpoc3jAY ptfsrhD2rlUro2DSQQde7dUC8N9PwkI= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf27.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736484994; 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; bh=4bqWfco/d0Md/FJRPlwgG5Dg33x1G1jGlbWxip43XAc=; b=oQb5DoJlmOFnxRU+Ic2lWbNOtufM58TL4TqEBGDi9R4rQ+Ua9cnlESfPYmkdWOVqQyqzVM 1L0aH8mZo+8xq6MA2BBSs1S78SAle+o34hdAUbjzBTCJiliQI5EIEWPglY0Hoj46J2+ok/ OM2fYOGmTlaoH4XMQUcwshcvJJPoV30= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E06AA1477; Thu, 9 Jan 2025 20:57:00 -0800 (PST) Received: from [10.162.42.21] (K4MQJ0H1H2.blr.arm.com [10.162.42.21]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A4D883F673; Thu, 9 Jan 2025 20:56:20 -0800 (PST) Message-ID: <4ab296ec-5dd2-4d1f-a823-29396cade93e@arm.com> Date: Fri, 10 Jan 2025 10:26:17 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC 00/11] khugepaged: mTHP support To: Nico Pache Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, ryan.roberts@arm.com, anshuman.khandual@arm.com, catalin.marinas@arm.com, cl@gentwo.org, vbabka@suse.cz, mhocko@suse.com, apopple@nvidia.com, dave.hansen@linux.intel.com, will@kernel.org, baohua@kernel.org, jack@suse.cz, srivatsa@csail.mit.edu, haowenchao22@gmail.com, hughd@google.com, aneesh.kumar@kernel.org, yang@os.amperecomputing.com, peterx@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ziy@nvidia.com, jglisse@google.com, surenb@google.com, vishal.moola@gmail.com, zokeefe@google.com, zhengqi.arch@bytedance.com, jhubbard@nvidia.com, 21cnbao@gmail.com, willy@infradead.org, kirill.shutemov@linux.intel.com, david@redhat.com, aarcange@redhat.com, raquini@redhat.com, sunnanyong@huawei.com, usamaarif642@gmail.com, audra@redhat.com, akpm@linux-foundation.org References: <20250108233128.14484-1-npache@redhat.com> <3e3252c2-65fc-45aa-99a1-ed66c31aba12@arm.com> Content-Language: en-US From: Dev Jain In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: AC87240003 X-Stat-Signature: cz46ehj9bpctpgwizej1ajagbfhfp4z9 X-HE-Tag: 1736484993-153531 X-HE-Meta: U2FsdGVkX1+wbOQmnntuNICcvGG33Gg5D1PslZZpr0P9vZFKNOk/1o+vWqmhh+JLwUXXLUp8R7apYKPoWIABNFpXUUiSwUUXpeLffkpHaWxMVdUZ7Fa1A+dfd01hbmCY2xAugUNzTWdWxsfpzMaD8j45rCLuKl4JeeQNP3cQ3uIg7Zj4Bvf2zt/qUjE/wODECwEpjYPYhLflMx0nR5VFipqBu8UZ+CAjHw3uKJUGvNSrJO4in/Z+SQmc4at+MwF5VV/1NqYXHx9cgNwhXHNn5JCdoVU1sDcombN6g5MLRJpWPu0HJtiFqzDpb16LYqUbPIIEr55gJVDKMHkf5zzGZ5Lq0lCbR9ey8G9HIdAWXRjT2d2OXT7yLRp6kchOJJDyZn+q53/bGYOlTrEXtwWDHFjPJo5O7i+4BLnulMk105BpDTzJHpcuUPHNHk5bzDzwmPFbShTC5V2B+Mhg0NI+9dP2FMpc0TY6buC7FAWyvzWQ6IZ6L68FGBQv8qsjtn/rux3n55LBvbjDeZ0me9Iyhnvt8EvOgT71SDUFvw611oLHvJhg/pRfX4q6xfIqtcbPo/WCNYqacmdcUgZ31/GiUXlcuqIf9HmziOtQIZLgrprEGYQgqsFe3fMawZEelTNpXnLUiEfzlm8H7e2ZbHLC0yEmA5AP/cxUTT1jhAsQNjXrbDKkb4XQSk6kowle6wE6QI8Ox6JO+32Cg6lyMYLXuA/h9HcqU8DXzfVOztk39avw0qOGx2epbnRZOnopwg93yzeLcljf32/2ikjNSzV5HQv2LHdk2b3ruEEJdZrn/geoGBFO/2kH3wxzFRSze/sooLv1Q8xGp/cXaBJzSCYyu9YC0LwBC6Y18LN6+Sz969m7SPeQ6iLLHTPQ5GIFOM0Aki3QMY+SaONuRmM65cPwNFHVj2YMKF/P4sglVXM+YmIS4lc3SME+TxitomMDni/NdiN1r8FGcXbh8/ZywU7 e7wX1/yt E6qHT7urvFBafmJ8In5chkplEpuBat4qR6s1tKntfpoJXI7GmI/ddYUmi/IxDLhDpSUp4fOxd4XHQb/8QmszYMRG0Pq65uUv7a9oChb6os2x/n2/o3x2UG165l14mAsv5BKC5g7c/pd62nOFhJP2WlrsyxuUfwtqnU894UFsY8fRHh/9g6oj/C6omXjBOR9dM2kgJmGTg9zZAYOzHJP3TBr4qA45Hjj3lifBz1TuNiQaO7I3W+0AGbzwN9ArpZTx4uQVaIt2oiqi2VSGhUDPag99Fou8Yr6zgIHIwWYWaM27TbwDndFK46B5nckRcD6wCos53zP4iusZQVprKxG4FyGOCA9mTmzq/qJZg 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: On 10/01/25 7:57 am, Nico Pache wrote: > On Wed, Jan 8, 2025 at 11:22 PM Dev Jain wrote: >> >> >> On 09/01/25 5:01 am, Nico Pache wrote: >>> The following series provides khugepaged and madvise collapse with the >>> capability to collapse regions to mTHPs. >>> >>> To achieve this we generalize the khugepaged functions to no longer depend >>> on PMD_ORDER. Then during the PMD scan, we keep track of chunks of pages >>> (defined by MTHP_MIN_ORDER) that are fully utilized. This info is tracked >>> using a bitmap. After the PMD scan is done, we do binary recursion on the >>> bitmap to find the optimal mTHP sizes for the PMD range. The restriction >>> on max_ptes_none is removed during the scan, to make sure we account for >>> the whole PMD range. max_ptes_none is mapped to a 0-100 range to >>> determine how full a mTHP order needs to be before collapsing it. >>> >>> Some design choices to note: >>> - bitmap structures are allocated dynamically because on some arch's >>> (like PowerPC) the value of MTHP_BITMAP_SIZE cannot be computed at >>> compile time leading to warnings. >>> - The recursion is masked through a stack structure. >>> - A MTHP_MIN_ORDER was added to compress the bitmap, and ensure it was >>> 64bit on x86. This provides some optimization on the bitmap operations. >>> if other arches/configs that have larger than 512 PTEs per PMD want to >>> compress their bitmap further we can change this value per arch. >>> >>> Patch 1-2: Some refactoring to combine madvise_collapse and khugepaged >>> Patch 3: A minor "fix"/optimization >>> Patch 4: Refactor/rename hpage_collapse >>> Patch 5-7: Generalize khugepaged functions for arbitrary orders >>> Patch 8-11: The mTHP patches >>> >>> This series acts as an alternative to Dev Jain's approach [1]. The two >>> series differ in a few ways: >>> - My approach uses a bitmap to store the state of the linear scan_pmd to >>> then determine potential mTHP batches. Devs incorporates his directly >>> into the scan, and will try each available order. >>> - Dev is attempting to optimize the locking, while my approach keeps the >>> locking changes to a minimum. I believe his changes are not safe for >>> uffd. >>> - Dev's changes only work for khugepaged not madvise_collapse (although >>> i think that was by choice and it could easily support madvise) >>> - Dev scales all khugepaged sysfs tunables by order, while im removing >>> the restriction of max_ptes_none and converting it to a scale to >>> determine a (m)THP threshold. >>> - Dev turns on khugepaged if any order is available while mine still >>> only runs if PMDs are enabled. I like Dev's approach and will most >>> likely do the same in my PATCH posting. >>> - mTHPs need their ref count updated to 1<>> >>> Patch 11 was inspired by one of Dev's changes. >>> >>> [1] https://lore.kernel.org/lkml/20241216165105.56185-1-dev.jain@arm.com/ >>> >>> Nico Pache (11): >>> introduce khugepaged_collapse_single_pmd to collapse a single pmd >>> khugepaged: refactor madvise_collapse and khugepaged_scan_mm_slot >>> khugepaged: Don't allocate khugepaged mm_slot early >>> khugepaged: rename hpage_collapse_* to khugepaged_* >>> khugepaged: generalize hugepage_vma_revalidate for mTHP support >>> khugepaged: generalize alloc_charge_folio for mTHP support >>> khugepaged: generalize __collapse_huge_page_* for mTHP support >>> khugepaged: introduce khugepaged_scan_bitmap for mTHP support >>> khugepaged: add mTHP support >>> khugepaged: remove max_ptes_none restriction on the pmd scan >>> khugepaged: skip collapsing mTHP to smaller orders >>> >>> include/linux/khugepaged.h | 4 +- >>> mm/huge_memory.c | 3 +- >>> mm/khugepaged.c | 436 +++++++++++++++++++++++++------------ >>> 3 files changed, 306 insertions(+), 137 deletions(-) >> >> Before I take a proper look at your series, can you please include any testing >> you may have done? > > I Built these changes for the following arches: x86_64, arm64, > arm64-64k, ppc64le, s390x > > x86 testing: > - Selftests mm > - some stress-ng tests > - compile kernel > - I did some tests with my defer [1] set on top. This pushes all the > work to khugepaged, which removes the noise of all the PF allocations. > > I recently got an ARM64 machine and did some simple sanity tests (on > both 4k and 64k) like selftests, stress-ng, and playing around with > the tunables, etc. > > I will also be running all the builds through our CI, and perf testing > environments before posting. > > [1] https://lore.kernel.org/lkml/20240729222727.64319-1-npache@redhat.com/ > >> > I tested your series with the program I was using and it is not working; can you please confirm it. diff --git a/mytests/mthp.c b/mytests/mthp.c new file mode 100644 index 000000000000..e3029dbcf035 --- /dev/null +++ b/mytests/mthp.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * + * Author: Dev Jain + * + * Program to test khugepaged mTHP collapse + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + char *ptr; + unsigned long mthp_size = (1UL << 16); + size_t chunk_size = (1UL << 25); + + ptr = mmap((void *)(1UL << 30), chunk_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (((unsigned long)ptr) != (1UL << 30)) { + printf("mmap did not work on required address\n"); + return 1; + } + + /* Fill first pte in every 64K interval */ + for (int i = 0; i < chunk_size; i += mthp_size) + ptr[i] = i; + + if (madvise(ptr, chunk_size, MADV_HUGEPAGE)) { + perror("madvise"); + return 1; + } + sleep(100); + return 0; +} -- 2.30.2 Set enabled = madvise, hugepages-2048k/enabled = hugepages-64k/enabled = inherit. Run the program in the background, then run tools/mm/thpmaps. You will see PMD collapse correctly, but when you echo never into hugepages-2048k/enabled and test this again, you won't see contpte 64K collapse. With my series, you will see something like anon-cont-pte-aligned-64kB : 32768 kB (100%).