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 51E7BC83F1B for ; Thu, 17 Jul 2025 07:24:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E18DB6B00AD; Thu, 17 Jul 2025 03:24:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DCE8D6B00AE; Thu, 17 Jul 2025 03:24:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB7DA6B00AF; Thu, 17 Jul 2025 03:24:18 -0400 (EDT) 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 B96766B00AD for ; Thu, 17 Jul 2025 03:24:18 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 80D1D5A18D for ; Thu, 17 Jul 2025 07:24:18 +0000 (UTC) X-FDA: 83672918196.20.6B23FFF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf21.hostedemail.com (Postfix) with ESMTP id 59F731C0007 for ; Thu, 17 Jul 2025 07:24:16 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=I3af7Y2u; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf21.hostedemail.com: domain of npache@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=npache@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752737056; a=rsa-sha256; cv=none; b=BuLeiEsVlKRJstB1gXElmWFg59R4aWMrpG3KAgBO/aMEEw5Zh5VHYt0gCDA3T4zLwhu/6V pb4XfzdE1rNhfBoPlyUq4hwp8yjhOvsIhIBkj38zKFuONeSaU7o0NSqjiRGAFeD5pKNivM KlNjyqm3snNnhQJzitqqEx1/CPzlDyI= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=I3af7Y2u; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf21.hostedemail.com: domain of npache@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=npache@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1752737056; 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=KfCPGePoOp3LYY/EgVnEL9WU60m9R4sUTYWVjJ8p3AQ=; b=P/RcpQR1sSxuOIlR+HI4nkX362wM292HvzKv2d0VVmTsqyR/htTSTQMjZv5kK/Pkux98Wu 08PFpSTbfT+5OuH5SCFJ0GUNSlIgaSi7XmQrTdXR5lDVlU2iA2MIScn3Z3vJIBNgGtGko+ ZUg7w/pemPUl5WnOZFl8xNwILy450x4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1752737055; 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=KfCPGePoOp3LYY/EgVnEL9WU60m9R4sUTYWVjJ8p3AQ=; b=I3af7Y2u1DfN4/bwtqtP6tQf/yENvk+Ubph4eTPB5133M9JCBO3E8xU7JZcwVaILTFTGfh uN6Z6erepdp8af6MpQcoNZS24QLJWrWyJVrG6l+VjqLhBRdHMsfOXid9wtZAXRfEk6YWVC Yst3z5LQJSKQUVyyHisNpPrg0SgDe0A= Received: from mail-yw1-f198.google.com (mail-yw1-f198.google.com [209.85.128.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-377-0OxEyUtmNXKoCfl-YYg2zw-1; Thu, 17 Jul 2025 03:24:09 -0400 X-MC-Unique: 0OxEyUtmNXKoCfl-YYg2zw-1 X-Mimecast-MFC-AGG-ID: 0OxEyUtmNXKoCfl-YYg2zw_1752737049 Received: by mail-yw1-f198.google.com with SMTP id 00721157ae682-710e75f9229so8886037b3.2 for ; Thu, 17 Jul 2025 00:24:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752737049; x=1753341849; 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=KfCPGePoOp3LYY/EgVnEL9WU60m9R4sUTYWVjJ8p3AQ=; b=sPI04ryzFpyNqLzO7kV/MQCXwXmL5n96UVUpHYgwt/Xaym74KlNhN5iMEUVT4FgZyq r2Lm2tgB/tvtiTkH7IB7m1hv++6HPNPDNTSpcqRPAjxxQmubmSlNigaEPf0gMsQNGeoH SRpQPKsWXe2UVQLPEQkW5trYTperw/iWZE9aVDBXq7j6IDnhHFeclEwJ/Z8t0AFNlNq+ 9Ek9Ij+YhkhNCon9R+Bf1eePeIfCCV9i4reB+xh78yWrO+gSKCxLrM12onZjlyZVjo3b dmnZqvUAarku5qBsF+5BvuHwAyPcYAYb27Pf02SdWbsASLe6f9L8p7THtxLMDARbzwtb 4eGw== X-Gm-Message-State: AOJu0Yy0tt6dWfgZCsjtudH/U4AOcTqtdi5g/76LuT4QbK4OXot8IjaM aas7twX8BAjZxBG7zr2TN4VGSYtcG0ZVHxgCk5eoI+6N+6O5KR1jNZuBJbDzK2WURToM14od1uQ 1WUxoPzSMzLPbJJtBDPX/4F1L0GclXZ0gY2mrc06A1M/1d9ELFYZZL215NSC4fv7BBruuq1AL0s y72s2us8BdPfr+wKhu7Gg50mIFSuc= X-Gm-Gg: ASbGncvv4PrkopRlA4Q/hrktYF8sLdqsT0BkQl/bPxPeo8W+vY+QatW7sTfz0Ucrpt3 uDnB2F9smkQMBXQwfw7yHSGrh7iZwUBn7wcuNEveue+GSDU2LPCc6Y17zb1aiGtVngJax7CIJ/w tc+TmHK727YL1Bd3bVCsb0zG4= X-Received: by 2002:a05:690c:9a83:b0:714:250:833a with SMTP id 00721157ae682-71835178c52mr75128727b3.27.1752737048790; Thu, 17 Jul 2025 00:24:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFfJvKaPzv08Vi4PZEJPmCDSNU/tGELVSQ+5Z8J3b7WhlI4rCEfMfiHtnXHhhga1V1PSe4f41mdlF7ouQLMBAo= X-Received: by 2002:a05:690c:9a83:b0:714:250:833a with SMTP id 00721157ae682-71835178c52mr75128277b3.27.1752737048378; Thu, 17 Jul 2025 00:24:08 -0700 (PDT) MIME-Version: 1.0 References: <20250714003207.113275-1-npache@redhat.com> <20250714003207.113275-7-npache@redhat.com> <82cd57c2-d72f-423a-8dbc-d9b64d1d469b@redhat.com> In-Reply-To: <82cd57c2-d72f-423a-8dbc-d9b64d1d469b@redhat.com> From: Nico Pache Date: Thu, 17 Jul 2025 01:23:42 -0600 X-Gm-Features: Ac12FXxiLSehaUAA88Omm39G4YDE8fV_Jkcck06oY9gUgXBKp4aUCgW1_s9ZvsQ Message-ID: Subject: Re: [PATCH v9 06/14] khugepaged: introduce collapse_scan_bitmap for mTHP support To: David Hildenbrand Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, ziy@nvidia.com, baolin.wang@linux.alibaba.com, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, ryan.roberts@arm.com, dev.jain@arm.com, corbet@lwn.net, rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, akpm@linux-foundation.org, baohua@kernel.org, willy@infradead.org, peterx@redhat.com, wangkefeng.wang@huawei.com, usamaarif642@gmail.com, sunnanyong@huawei.com, vishal.moola@gmail.com, thomas.hellstrom@linux.intel.com, yang@os.amperecomputing.com, kirill.shutemov@linux.intel.com, aarcange@redhat.com, raquini@redhat.com, anshuman.khandual@arm.com, catalin.marinas@arm.com, tiwai@suse.de, will@kernel.org, dave.hansen@linux.intel.com, jack@suse.cz, cl@gentwo.org, jglisse@google.com, surenb@google.com, zokeefe@google.com, hannes@cmpxchg.org, rientjes@google.com, mhocko@suse.com, rdunlap@infradead.org, hughd@google.com X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 1t4RLyUN0dAwJvAD-Z8sLw5VzAXDf8Qn9Ymj7z9TKoo_1752737049 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 59F731C0007 X-Stat-Signature: yizpqk9btbyqmm65cwg7gmur58x9gztf X-HE-Tag: 1752737056-21754 X-HE-Meta: U2FsdGVkX1/WxDDiBba4VNCNduMSfV2RhXV6rlzSvOfPhDl3PraVRs8log5hKD4e4vDgTtKqrhM/xHx47KvLp3jvUWsiIw71e65azP1obTUupKE2MGZiNNPhebElo9oPzvrJhjPFxoxzDhtoSGJApgnDJ5CNXu4UeLIXVPOso1n73vIG8nQbNzPpvS1Q5yZj/ChykY1ojgJHJfOHGe53FjUD0CVR5IZZB/XH1wrZow3srh6DaAwfwvCGaicKAyhIxh2kuFeXdztYpjEHlbzrFrFXHbBNf9e579NYlBbgfSCkMQomF3Tx9CMqRWbwoPlMH9bvyifvoMiCWXsMK/2A1hSoUAKNNk2+ZR0YwHNQjq3gIOxtccpHanNqWYRJ1kdS+xmVaYMORqaymH+CBwkfVLU3CFQ+x3JaBcLtCj/TG4FAC7ViYS/pzUzQX+bwcmI2Gwr7FRpfZf7GCiRB/tX3+9vbnCMOaDVl4EqxW3Ggxwb0b4xPby8hQTDGBKuQ7aIh/s6ehiYWN40Q6aTB3EtVO9duSf8AJfDbiBSMXbc/rBcta8dWK4s90qXYt1hiMQQjWh0QZ1UlVfhaljM1+rQ/nxfFEB7IwBtImrhCF3eqAZaKeOElw2AUkcsfwRmLjojeGOJkl31IO9iTh+YV5veMqHIuMrEg79+VmcFk0dV9B/bcg8/zpiKcLEIbrFz3t2yCNbg5fNDBGw7vXyYp6aVPV93yMfxufX/F1QCeupEP7Ld2GpcXebtnaO/RSBrW83IUPJR04n4xAxIkkksNTr4jE4z61dtYwi7VUj0SjL90Tcqyotpy8xx76LAG/wgBjZBFlOZueAknZQhZgDaMhQLQTdtC6k0CqCwAdZn9rDsjUGpFO7m7XfMJH6MR9RrLNYI0kW7IFB7wgda+v7VbZ3UQ5JQTkRrbvUVxKIoUF5r42HDBxPpToM7F+Jc5T6iUgCs0R/UhV8avBVo/oLKdzbY I42kLwke QnuowI21shjkUMGaX/9lsy7IwJkYPa7F7uR6VBE83Yi/XagcNi0tbVNxDvexGfRukXbshqLBk1L/2/cRHczUqnYf8eUsEXruFvy9vnGROEYhZnfp7z6gdiJrBHtPdduMTFpVgNWJXZFTq0NHwK0nUxWchFIjBHMD4+Dy3eVs8Aj027aQHuMo+Au3bm756T7jiwtjCdVsFCQHFU7ornsJtP9Pr2zl/OCURNyky9FpU5KjiIdH17ADwpzr6vorEGEATDIX3uCH9yfJhDKJho/PKEKdHMtGWSNBmEZUrOJd6GSCtvuL//ruh9Ljf+DedktL1SFh0bi2UbR+6LaPuH0wy16kfoCTfez14xSGcgMZ5TY0voRyysOVckZquwIPo+8eOFat1VLhgSHbQFb3WSyGvlFkwcTv6p5rqTG5H6r1FVQEkL5TPtO9zePo0NA== 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 Wed, Jul 16, 2025 at 8:03=E2=80=AFAM David Hildenbrand wrote: > > On 14.07.25 02:31, Nico Pache wrote: > > khugepaged scans anons PMD ranges for potential collapse to a hugepage. > > To add mTHP support we use this scan to instead record chunks of utiliz= ed > > sections of the PMD. > > > > collapse_scan_bitmap uses a stack struct to recursively scan a bitmap > > that represents chunks of utilized regions. We can then determine what > > mTHP size fits best and in the following patch, we set this bitmap whil= e > > scanning the anon PMD. A minimum collapse order of 2 is used as this is > > the lowest order supported by anon memory. > > > > max_ptes_none is used as a scale to determine how "full" an order must > > be before being considered for collapse. > > > > When attempting to collapse an order that has its order set to "always" > > lets always collapse to that order in a greedy manner without > > considering the number of bits set. > > > > Signed-off-by: Nico Pache > > Any reason this should not be squashed into the actual mTHP collapse patc= h? I wanted to keep them seperate to conceptually separate the bitmap and collapse, but given youre the second person to point this out, I went ahead and squashed them and their commit messages to be one commit. > > In particular > > a) The locking changes look weird without the bigger context > > b) The compiler complains about unused functions > > > --- > > include/linux/khugepaged.h | 4 ++ > > mm/khugepaged.c | 94 ++++++++++++++++++++++++++++++++++---= - > > 2 files changed, 89 insertions(+), 9 deletions(-) > > > > diff --git a/include/linux/khugepaged.h b/include/linux/khugepaged.h > > index ff6120463745..0f957711a117 100644 > > --- a/include/linux/khugepaged.h > > +++ b/include/linux/khugepaged.h > > @@ -1,6 +1,10 @@ > > /* SPDX-License-Identifier: GPL-2.0 */ > > #ifndef _LINUX_KHUGEPAGED_H > > #define _LINUX_KHUGEPAGED_H > > +#define KHUGEPAGED_MIN_MTHP_ORDER 2 > > +#define KHUGEPAGED_MIN_MTHP_NR (1< > "1 << " Ah there is a mix of these ("1<<" vs "1 << ") being used across the kernel. Fixed it thank you. > > > +#define MAX_MTHP_BITMAP_SIZE (1 << (ilog2(MAX_PTRS_PER_PTE) - KHUGEPA= GED_MIN_MTHP_ORDER)) > > +#define MTHP_BITMAP_SIZE (1 << (HPAGE_PMD_ORDER - KHUGEPAGED_MIN_MTHP= _ORDER)) > > > > extern unsigned int khugepaged_max_ptes_none __read_mostly; > > #ifdef CONFIG_TRANSPARENT_HUGEPAGE > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > > index ee54e3c1db4e..59b2431ca616 100644 > > --- a/mm/khugepaged.c > > +++ b/mm/khugepaged.c > > @@ -94,6 +94,11 @@ static DEFINE_READ_MOSTLY_HASHTABLE(mm_slots_hash, M= M_SLOTS_HASH_BITS); > > > > static struct kmem_cache *mm_slot_cache __ro_after_init; > > > > +struct scan_bit_state { > > + u8 order; > > + u16 offset; > > +}; > > + > > struct collapse_control { > > bool is_khugepaged; > > > > @@ -102,6 +107,18 @@ struct collapse_control { > > > > /* nodemask for allocation fallback */ > > nodemask_t alloc_nmask; > > + > > + /* > > + * bitmap used to collapse mTHP sizes. > > + * 1bit =3D order KHUGEPAGED_MIN_MTHP_ORDER mTHP > > + */ > > + DECLARE_BITMAP(mthp_bitmap, MAX_MTHP_BITMAP_SIZE); > > + DECLARE_BITMAP(mthp_bitmap_temp, MAX_MTHP_BITMAP_SIZE); > > + struct scan_bit_state mthp_bitmap_stack[MAX_MTHP_BITMAP_SIZE]; > > +}; > > + > > +struct collapse_control khugepaged_collapse_control =3D { > > + .is_khugepaged =3D true, > > }; > > > > /** > > @@ -838,10 +855,6 @@ static void khugepaged_alloc_sleep(void) > > remove_wait_queue(&khugepaged_wait, &wait); > > } > > > > -struct collapse_control khugepaged_collapse_control =3D { > > - .is_khugepaged =3D true, > > -}; > > - > > static bool collapse_scan_abort(int nid, struct collapse_control *cc) > > { > > int i; > > @@ -1115,7 +1128,8 @@ static int alloc_charge_folio(struct folio **foli= op, struct mm_struct *mm, > > > > static int collapse_huge_page(struct mm_struct *mm, unsigned long add= ress, > > int referenced, int unmapped, > > - struct collapse_control *cc) > > + struct collapse_control *cc, bool *mmap_loc= ked, > > + u8 order, u16 offset) > > Indent broken. Fixed! > > > { > > LIST_HEAD(compound_pagelist); > > pmd_t *pmd, _pmd; > > @@ -1134,8 +1148,12 @@ static int collapse_huge_page(struct mm_struct *= mm, unsigned long address, > > * The allocation can take potentially a long time if it involves > > * sync compaction, and we do not need to hold the mmap_lock duri= ng > > * that. We will recheck the vma after taking it again in write m= ode. > > + * If collapsing mTHPs we may have already released the read_lock= . > > */ > > - mmap_read_unlock(mm); > > + if (*mmap_locked) { > > + mmap_read_unlock(mm); > > + *mmap_locked =3D false; > > + } > > > > result =3D alloc_charge_folio(&folio, mm, cc, HPAGE_PMD_ORDER); > > if (result !=3D SCAN_SUCCEED) > > @@ -1272,12 +1290,72 @@ static int collapse_huge_page(struct mm_struct = *mm, unsigned long address, > > out_up_write: > > mmap_write_unlock(mm); > > out_nolock: > > + *mmap_locked =3D false; > > if (folio) > > folio_put(folio); > > trace_mm_collapse_huge_page(mm, result =3D=3D SCAN_SUCCEED, resul= t); > > return result; > > } > > > > +/* Recursive function to consume the bitmap */ > > +static int collapse_scan_bitmap(struct mm_struct *mm, unsigned long ad= dress, > > + int referenced, int unmapped, struct collapse_con= trol *cc, > > + bool *mmap_locked, unsigned long enabled_orders) > > +{ > > + u8 order, next_order; > > + u16 offset, mid_offset; > > + int num_chunks; > > + int bits_set, threshold_bits; > > + int top =3D -1; > > + int collapsed =3D 0; > > + int ret; > > + struct scan_bit_state state; > > + bool is_pmd_only =3D (enabled_orders =3D=3D (1 << HPAGE_PMD_ORDER= )); > > + > > + cc->mthp_bitmap_stack[++top] =3D (struct scan_bit_state) > > + { HPAGE_PMD_ORDER - KHUGEPAGED_MIN_MTHP_ORDER, 0 }; > > + > > + while (top >=3D 0) { > > + state =3D cc->mthp_bitmap_stack[top--]; > > + order =3D state.order + KHUGEPAGED_MIN_MTHP_ORDER; > > + offset =3D state.offset; > > + num_chunks =3D 1 << (state.order); > > + // Skip mTHP orders that are not enabled > > > /* */ > > Same applies to the other instances. Thank you! Fixed all instances > > -- > Cheers, > > David / dhildenb >