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 71A96C02184 for ; Mon, 13 Jan 2025 23:01:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4FE86B0085; Mon, 13 Jan 2025 18:01:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CFFBC6B0089; Mon, 13 Jan 2025 18:01:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B78B16B008A; Mon, 13 Jan 2025 18:01:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9A0C26B0085 for ; Mon, 13 Jan 2025 18:01:18 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 45DC21C6CD9 for ; Mon, 13 Jan 2025 23:01:18 +0000 (UTC) X-FDA: 83003951436.24.633E1ED Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf26.hostedemail.com (Postfix) with ESMTP id C221814000C for ; Mon, 13 Jan 2025 23:01:15 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Zf67z0ww; spf=pass (imf26.hostedemail.com: domain of npache@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=npache@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736809276; 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:dkim-signature; bh=izOH68pBo4Z0/oXt1slydvRalv5kuSIfK5Lkhc2yfbQ=; b=7N/K+6EcvofigRX24IS257jUASpK7p8MYdlfvp2Lr8ALxoRwRNYpS3x8SqnH00xAzBErzX f2kce0D6lOrTYLmFnuLVGPFoGNtImlB+/quhyJweDjvXJn1uho78v1AM5i7UnLtGkVRdRF vie1I6Ve6vKJcxfXyoY7/E2qGKZziyY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736809276; a=rsa-sha256; cv=none; b=qMa3P35BNe+wcHEO3hXboP+Wu2IfVfulny0OCjWMvZ4JA3gfeWAnK+NVLd7SsJ+SLWXXsS Sjc8sCPQMVhAELqSTWpH5O5hQYsuPYKHLN1Esck2VwolCb/E/ihYAx5PDJRLm3SCEIXOLW sCNHdBdTn+1tYhWEO09kPE3FAKqfZZE= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Zf67z0ww; spf=pass (imf26.hostedemail.com: domain of npache@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=npache@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736809275; 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=izOH68pBo4Z0/oXt1slydvRalv5kuSIfK5Lkhc2yfbQ=; b=Zf67z0wwsCT8LJr+JEkfCOCjD31LNcaDCRUPl7PJnyzHsMoBOGp8n7TQFQjklbX3dFfYMn WeFjnpwUlaFrgpToxaAp8jFdsUUiIUAvI6JIinBs94Uu6ZD+1puUI9eX7zCbjsLPrD/oVN CDPp4lYV0Jda/yHCS1L6c2fzMb6IyL0= Received: from mail-yb1-f200.google.com (mail-yb1-f200.google.com [209.85.219.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-578-HTXjN6aiMIKMkxOnvgeTgw-1; Mon, 13 Jan 2025 18:01:13 -0500 X-MC-Unique: HTXjN6aiMIKMkxOnvgeTgw-1 X-Mimecast-MFC-AGG-ID: HTXjN6aiMIKMkxOnvgeTgw Received: by mail-yb1-f200.google.com with SMTP id 3f1490d57ef6-e48beea5778so11538584276.1 for ; Mon, 13 Jan 2025 15:01:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736809273; x=1737414073; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=izOH68pBo4Z0/oXt1slydvRalv5kuSIfK5Lkhc2yfbQ=; b=RY66PQ7pp4Qb2ne4kVlP1s49BdxPBByOYJwaZEFtSyT0BdE7FFQp/V2tFoULr3v3oB ZMXe4Qusj9OLCm3okyj05856ceOKU9phbXKEAFKC2hInrPQUscz/rDL+nUaTXDlLr/J2 PnV5YyCHVD7idv5MTsScGBfeeG4K+vl44c+QeKcd/VkAMZbo+J40jSgXoJrLUTL/Bcwz vjTkr91/1pXKROhwaHDELi0UDtf5n0VrQoffxr2DqsR+Fb9+YP3U0ILtdr3AxO7oVn/d 2K0/mDNQ9vjiMwmba/JdTzuWMG0+s5mZ5ssSnDuP9wKLye4zZpiMbexGNoKHMouKYVnC O4dQ== X-Forwarded-Encrypted: i=1; AJvYcCXVQQ8rZRIyl3SumIsmqMm0/DZx7BRReSapCxQtNmLo5Uw2xswxcJ9VF/mc+xdgSxfzkNjazG+XMg==@kvack.org X-Gm-Message-State: AOJu0YzSupAZrbFEwLkmSej2r4UiZ8vTxWevBuk6X0dgdTAnZwSaU+ZW BTbryeYA1s8GISDbCXTQzWuacNsADdDFGANLS2XVWxhxKbY55jjB4hkPV83JCrTgP8noIdBeiSR pTp9lIjeK2ZvljD4GBTQ1JBhm8ivW+fF3WYLRyl+3icTY8MrDdN65Ql2hzHTAhHZS3wUqHdhzYm X6DkiOlaZSI7kAvzyPFlcF8Js= X-Gm-Gg: ASbGnctMoHemYRB1igOrZ9BHE1o32RUzx7q8ubMkb4VvkAlzr/5vueiloYsZE2VoijP NxIZWVVblF4ye+Wn8Wn5FxSdOiAq+yuJblUnIB62gY865oJM8qzay X-Received: by 2002:a05:690c:6483:b0:6f6:7b02:2568 with SMTP id 00721157ae682-6f67b0227c5mr83045097b3.32.1736809272610; Mon, 13 Jan 2025 15:01:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IGIRQad21gX8fK269na/qYZR+kSMOgRnt23GVu6bq+rq0trogXRsGVOOHLtfw9+7zY5R3DVGrpoXS5p5po6+7Q= X-Received: by 2002:a05:690c:6483:b0:6f6:7b02:2568 with SMTP id 00721157ae682-6f67b0227c5mr83044177b3.32.1736809271665; Mon, 13 Jan 2025 15:01:11 -0800 (PST) MIME-Version: 1.0 References: <20250108233128.14484-1-npache@redhat.com> <3e3252c2-65fc-45aa-99a1-ed66c31aba12@arm.com> <4ab296ec-5dd2-4d1f-a823-29396cade93e@arm.com> <2aed36e5-740e-45bc-ae29-cfc11be780cf@arm.com> In-Reply-To: <2aed36e5-740e-45bc-ae29-cfc11be780cf@arm.com> From: Nico Pache Date: Mon, 13 Jan 2025 16:00:45 -0700 X-Gm-Features: AbW1kvYhCtFLYmExrpDuIBQMsUKPdU_LBnFKcQhRGYFwPNlSCPN9IxJcnfQn_IQ Message-ID: Subject: Re: [RFC 00/11] khugepaged: mTHP support To: Dev Jain 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 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: vPu_8ZpsDMg5kBEdDbXWbNlD_88Z5tU7WTpfkSXGAq4_1736809273 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: C221814000C X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: 431bo4ywg7f1osg6wp7skx6yskarpfje X-HE-Tag: 1736809275-520470 X-HE-Meta: U2FsdGVkX18cY6NSpvjl3gQHkfnm2WSR9Ta8sI+tGnnLAKrXrCSNOnw1ndxKdF7xtHCt98EBVOezoARaf/TclrJPIGjYPJIQRYF5bH7Q/NDG4zo/C3dz3wXW2KfzkOhGgFVs1mUBy5sTguCJW0srG1ZWdRm2ggDjN665oOe7m4YOxn1GASARwpLE8+xyuoB2ao74q4CfLlUbutMwqYthGS7eVTMlTPS7wSpV1jfGapg40PIN0jfYpzYCW4I0dhcrTVcg+4vYt99gx0vNGVjpikb7CfT3lYHLIz1Usu11YsrF5aOyDoQLcNua5pryEqUqnbaTKWgt3rDysBQGuwtM1xCfvyGp6nrcoXO6Qo8vmufY/9nmXLOzMs0bZJuX0PdbzNMaLS0bQhh6FvKs7bsHFUToth+5/ZAzD3ychp6LApnfVcMFBnE3tZBAnwgvumz7GrncHsuXChzmXqmlARpcfcy1Jid7C2yDu3YTFabU439aivtOEuH6e18CvLpULjv/d9m+GR64OG/FSHDNXPg7hjM5a0K0HS3mKENnE2FP95vYKCPKghCb9iToghGtca6WM3cZy97fc1FDwbsHbGnBbecI+q4WdsnZphTftCml1A2KeBHUCS4x09GO6/MCKVveh8AN1+EFr3OW7fWIOAUYQv6AFrsMOL5EuOChToQGk6rBKcm2UJvePIYmOnkHNzXA9cedtNOAGHlpqSP1pchcJ2nfxIdmUl5EbW9RrnXaFa57bctaptEp2jNEw1yUcvS6t78TfIU9T+FFTOblzbWf/6RGib8s0G0v7YhKM3PKbktJO/7aOfIciPeHnh+/vhuqSuodT75Kbr6zr9UZ6+rXp0C71FfwuZqP/WqHZgcWX8XPGKKLNbV7f7mowfcuD4PGLkicrf41of29e6ucr+70Vok67NOP5c2qggf+zMERPO+NIOFT0fg8zJSC/Y3LE01jl+aQw6LzBnE+AZHEvwg J+Otjvs8 aU4b8uS6c1BuWVF0CSR3a3n7NT36WCTZOsbHHqxAcL7CO0w1vLepGE7eKdiVBF0kyQ3lVa6hy2fNSTuR7rdu/zC6Cg5uUzVWtKmsmlkm2lH3QcplPyIPZsVa8eRlPeQzqCJ/2Cnr/mheQBpFQTo3evYrnNoZmSQ7/clVMZ1l/q2Va9mmLQ1W9rLf49uw9p9vcrmXIetQrb2LjwTJ6TB+HbC64lUZWa1xJDdB05pNsQsjfG8yBXN4qrsEfo6WMLoMskfTRKu/IcGz5Deq6NEjkAkihZqQ1H357XPxPMCjNaFR7w9uJs1w+ntOhxx3qU9/JszWyeS1HhomQf6ridfN5KQ4zkbj96ph4qM8DLkJiiwJ9084VkDJpja8p5kZLpplKE8xan2AlpYlIWfaBUd3a4mUIrKRoJP0VRilrDWQi1maJ/0U= 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 Sun, Jan 12, 2025 at 7:12=E2=80=AFAM Dev Jain wrote: > > > > On 11/01/25 3:31 am, Nico Pache wrote: > > On Thu, Jan 9, 2025 at 9:56=E2=80=AFPM Dev Jain wrot= e: > >> > >> > >> > >> On 10/01/25 7:57 am, Nico Pache wrote: > >>> On Wed, Jan 8, 2025 at 11:22=E2=80=AFPM Dev Jain w= rote: > >>>> > >>>> > >>>> 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 t= racked > >>>>> 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 restri= ction > >>>>> on max_ptes_none is removed during the scan, to make sure we accoun= t 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 a= rch's > >>>>> (like PowerPC) the value of MTHP_BITMAP_SIZE cannot be compu= ted 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 P= MD want to > >>>>> compress their bitmap further we can change this value per a= rch. > >>>>> > >>>>> Patch 1-2: Some refactoring to combine madvise_collapse and khugep= aged > >>>>> 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 s= can_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 madvi= se) > >>>>> - Dev scales all khugepaged sysfs tunables by order, while im = removing > >>>>> the restriction of max_ptes_none and converting it to a scal= e 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 wil= l 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@ar= m.com/ > >>>>> > >>>>> Nico Pache (11): > >>>>> introduce khugepaged_collapse_single_pmd to collapse a single = pmd > >>>>> khugepaged: refactor madvise_collapse and khugepaged_scan_mm_s= lot > >>>>> khugepaged: Don't allocate khugepaged mm_slot early > >>>>> khugepaged: rename hpage_collapse_* to khugepaged_* > >>>>> khugepaged: generalize hugepage_vma_revalidate for mTHP suppor= t > >>>>> 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 a= ny 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 testin= g > >>> 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 workin= g; > >> can you please confirm it. > > > > Yes, this is expected because you are not fully filling any 32K chunk > > (MIN_MTHP_ORDER) so no bit is ever set. > > That is weird, because if this is the case, then PMD-collapse should > have also failed, but that succeeded. Do you have some userspace program > I can test with? Not exactly, if max_ptes_none is still 511, the old behavior is kept. I modified your program to set the first 8 pages (32k chunk) in every 64k region. #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { char *ptr; unsigned long mthp_size =3D (1UL << 16); // 64 KB chunk size size_t chunk_size =3D (1UL << 25); // 32 MB total size // mmap() to allocate memory at a specific address (1 GB address) ptr =3D mmap((void *)(1UL << 30), chunk_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (((unsigned long)ptr) !=3D (1UL << 30)) { printf("mmap did not work on required address\n"); return 1; } // Touch the first 8 pages in every 64 KB chunk for (int i =3D 0; i < chunk_size; i +=3D mthp_size) { // Touch the first 8 pages within the 64 KB chunk (8 * 4 KB =3D 32 = KB) for (int j =3D 0; j < 8; ++j) { ptr[i + j * 4096] =3D i + j * 4096; // Touch the first byte of each page } } // Use madvise() to advise the kernel to use huge pages for this memory if (madvise(ptr, chunk_size, MADV_HUGEPAGE)) { perror("madvise"); return 1; } sleep(100); // Sleep to allow time for the kernel to process the advice return 0; } There's some rounding errors in how I compute the threshold_bits... I think I will adopt how you do the max_ptes_none shifting for better accuracy. Currently if you run this with max_ptes_none=3D255 (or even lower values like 200...) it will still collapse to a 64k chunk when in reality it should only do 32k because only half the bitmap is set for this order, and 255 < 50% of 512. I'm adding a threshold to the bitmap_set, and doing better scaling like you do. My next version should handle the example code better. >