From nobody Mon Feb 9 16:06:51 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 9CAC3204F70 for ; Wed, 8 Jan 2025 23:34:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736379257; cv=none; b=nVH+RfZAEKVExZ2FlZaAbBcdL2P9UFnxNqpLorZ4CNd7/Td3wbqWtBbA8gnKR0k1CLaipzIiR+8YFTx3iE3w3MarXI6IN+5VDi/WX0jrBkK+2QypQr6/DCuisQtKljPUoHQ7G5+ISZizwb6ldcqT1FU8Dw7IZx0P+Ri3UMTmYdQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736379257; c=relaxed/simple; bh=onJbqRGIwbsxztFIp6xG0EJikaf01qGsC91fw7O8XHM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N3Mv5X0aUghUDShnQIjxCodhnYatE+YrHwFV4d6fqS9lVanL3LUnUgHRq9Qpe83Hh5/kD2N5W3UBgRHyApjzez70Z1EDGlOiTa44bB7pbZTDLXx2v5wgDORooNC0JVYdsr0pR29ffyCYZknpLvXlIL7l47AljuPXaV2qmVAVVZg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=EB80bCDE; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="EB80bCDE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736379254; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0aDz9njSBgcFlph30zgTLUgh8iNh2Ug8pSZWMj0+9ks=; b=EB80bCDEdRDRPW2YV2zPn9/X7J2MzrOi2xpBqxKhqX3yojypAjedgkGmjowq6WqfmxP41M hB4KrCWiXeFoFhH+0awnvQ5805nU4wfVCMHBYBnTilljn8CVIL33FeXftJJHdLfWqSIT5j vtdqFqVKfWPZMI3yZdxElyxTC/U6ITQ= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-682-IzrUvAYLNmSq9Wj_N5ghYw-1; Wed, 08 Jan 2025 18:34:10 -0500 X-MC-Unique: IzrUvAYLNmSq9Wj_N5ghYw-1 X-Mimecast-MFC-AGG-ID: IzrUvAYLNmSq9Wj_N5ghYw Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8C0DF19560B1; Wed, 8 Jan 2025 23:34:05 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.80.41]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2718F19560AE; Wed, 8 Jan 2025 23:33:57 +0000 (UTC) From: Nico Pache To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: 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, dev.jain@arm.com, sunnanyong@huawei.com, usamaarif642@gmail.com, audra@redhat.com, akpm@linux-foundation.org Subject: [RFC 08/11] khugepaged: introduce khugepaged_scan_bitmap for mTHP support Date: Wed, 8 Jan 2025 16:31:24 -0700 Message-ID: <20250108233128.14484-9-npache@redhat.com> In-Reply-To: <20250108233128.14484-1-npache@redhat.com> References: <20250108233128.14484-1-npache@redhat.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 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Content-Type: text/plain; charset="utf-8" khugepaged scans PMD ranges for potential collapse to a hugepage. To add mTHP support we use this scan to instead record chunks of fully utilized sections of the PMD. create a bitmap to represent a PMD in order MTHP_MIN_ORDER chunks. by default we will set this to order 3. The reasoning is that for 4K 512 PMD size this results in a 64 bit bitmap which has some optimizations. For other arches like ARM64 64K, we can set a larger order if needed. khugepaged_scan_bitmap uses a stack struct to recursively scan a bitmap that represents chunks of fully utilized regions. We can then determine what mTHP size fits best and in the following patch, we set this bitmap while scanning the PMD. max_ptes_none is used as a scale to determine how "full" an order must be before being considered for collapse. Signed-off-by: Nico Pache --- include/linux/khugepaged.h | 4 +- mm/khugepaged.c | 129 +++++++++++++++++++++++++++++++++++-- 2 files changed, 126 insertions(+), 7 deletions(-) diff --git a/include/linux/khugepaged.h b/include/linux/khugepaged.h index 1f46046080f5..31cff8aeec4a 100644 --- a/include/linux/khugepaged.h +++ b/include/linux/khugepaged.h @@ -1,7 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0 */ #ifndef _LINUX_KHUGEPAGED_H #define _LINUX_KHUGEPAGED_H - +#define MIN_MTHP_ORDER 3 +#define MIN_MTHP_NR (1<mthp_bitmap_stack[++top] =3D (struct scan_bit_state) + { HPAGE_PMD_ORDER - MIN_MTHP_ORDER, 0 }; + + while (top >=3D 0) { + state =3D cc->mthp_bitmap_stack[top--]; + order =3D state.order; + offset =3D state.offset; + num_chunks =3D 1 << order; + // Skip mTHP orders that are not enabled + if (!(enabled_orders >> (order + MIN_MTHP_ORDER)) & 1) + goto next; + + // copy the relavant section to a new bitmap + bitmap_shift_right(cc->mthp_bitmap_temp, cc->mthp_bitmap, offset, + MTHP_BITMAP_SIZE); + + bits_set =3D bitmap_weight(cc->mthp_bitmap_temp, num_chunks); + + // Check if the region is "almost full" based on the threshold + max_percent =3D ((HPAGE_PMD_NR - khugepaged_max_ptes_none - 1) * 100) + / (HPAGE_PMD_NR - 1); + threshold_bits =3D (max_percent * num_chunks) / 100; + + if (bits_set >=3D threshold_bits) { + ret =3D collapse_huge_page(mm, address, referenced, unmapped, cc, + mmap_locked, order + MIN_MTHP_ORDER, offset * MIN_MTHP_NR); + if (ret =3D=3D SCAN_SUCCEED) + collapsed +=3D (1 << (order + MIN_MTHP_ORDER)); + continue; + } + +next: + if (order > 0) { + next_order =3D order - 1; + mid_offset =3D offset + (num_chunks / 2); + cc->mthp_bitmap_stack[++top] =3D (struct scan_bit_state) + { next_order, mid_offset }; + cc->mthp_bitmap_stack[++top] =3D (struct scan_bit_state) + { next_order, offset }; + } + } + return collapsed; +} + static int khugepaged_scan_pmd(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, bool *mmap_locked, @@ -1430,7 +1528,7 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, pte_unmap_unlock(pte, ptl); if (result =3D=3D SCAN_SUCCEED) { result =3D collapse_huge_page(mm, address, referenced, - unmapped, cc); + unmapped, cc, mmap_locked, HPAGE_PMD_ORDER, 0); /* collapse_huge_page will return with the mmap_lock released */ *mmap_locked =3D false; } @@ -2767,6 +2865,21 @@ int madvise_collapse(struct vm_area_struct *vma, str= uct vm_area_struct **prev, return -ENOMEM; cc->is_khugepaged =3D false; =20 + cc->mthp_bitmap =3D kmalloc_array( + BITS_TO_LONGS(MTHP_BITMAP_SIZE), sizeof(unsigned long), GFP_KERNEL); + if (!cc->mthp_bitmap) + return -ENOMEM; + + cc->mthp_bitmap_temp =3D kmalloc_array( + BITS_TO_LONGS(MTHP_BITMAP_SIZE), sizeof(unsigned long), GFP_KERNEL); + if (!cc->mthp_bitmap_temp) + return -ENOMEM; + + cc->mthp_bitmap_stack =3D kmalloc_array( + MTHP_BITMAP_SIZE, sizeof(struct scan_bit_state), GFP_KERNEL); + if (!cc->mthp_bitmap_stack) + return -ENOMEM; + mmgrab(mm); lru_add_drain_all(); =20 @@ -2831,8 +2944,12 @@ int madvise_collapse(struct vm_area_struct *vma, str= uct vm_area_struct **prev, out_nolock: mmap_assert_locked(mm); mmdrop(mm); + kfree(cc->mthp_bitmap); + kfree(cc->mthp_bitmap_temp); + kfree(cc->mthp_bitmap_stack); kfree(cc); =20 + return thps =3D=3D ((hend - hstart) >> HPAGE_PMD_SHIFT) ? 0 : madvise_collapse_errno(last_fail); } --=20 2.47.1