From patchwork Wed Jan 8 23:31:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nico Pache X-Patchwork-Id: 13931721 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 90539E77188 for ; Wed, 8 Jan 2025 23:34:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2189B6B009F; Wed, 8 Jan 2025 18:34:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1CABA6B00A0; Wed, 8 Jan 2025 18:34:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 019E26B00A1; Wed, 8 Jan 2025 18:34:24 -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 D923F6B009F for ; Wed, 8 Jan 2025 18:34:24 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 665D8161562 for ; Wed, 8 Jan 2025 23:34:24 +0000 (UTC) X-FDA: 82985890848.29.AF83E39 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf30.hostedemail.com (Postfix) with ESMTP id B40118000D for ; Wed, 8 Jan 2025 23:34:22 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="ND4aWY/w"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf30.hostedemail.com: domain of npache@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=npache@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736379262; a=rsa-sha256; cv=none; b=EZp25gBwp94nPsMd4LENYGZYWvJ6Zizb1SpWEYsSl/iuEnxdP7olXC4vtKF2zl6NO7uwLw 3EUs5yN47y9cLMXcxECbrMCcWLHlWuw6LCzpXUiJIFjXSEPBiydvyP7E2oM2RS9+ATsUZ0 sM2N+SrqMqztPndfz4jv9SGKYoJJoiQ= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="ND4aWY/w"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf30.hostedemail.com: domain of npache@redhat.com designates 170.10.133.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=1736379262; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=90X6GfV2bcC688lGkgxcg3viPnFdaoWJIxc9fBgBRMY=; b=UlvNMaFM3nr8NWLsovS6eznpkWTHHxZano9tXUbxcukVbgqYBOo3C7jAtXFYDyMq9rAA6U dkZxb/5wlGkBcPb1ZXZv9WlSM7pYFAUzY2n6lZdXbldFollrM6IqiB4j1OtlJN7pr9Y1vH krgIsUdssdC5QAIYGp89Xne1pu9WQ48= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736379262; 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=90X6GfV2bcC688lGkgxcg3viPnFdaoWJIxc9fBgBRMY=; b=ND4aWY/wEkmNKWDmpgg1Y/KAQheuR+jZgVjwfnz6bTcnWr1x7GMm7U3T+s54jJ84V4XKfv 3WiAqm/mBtHsp/PszO38fZ7dKPU63FSLnpyhIK1i0AwJfeqt8iTGM5oZkUi03iFuZStsde +rT/XooRUFYjaa1iTT77Uyfei8Keetk= Received: from mx-prod-mc-02.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-480-6NSDnLaZPwSRWloZEf12ug-1; Wed, 08 Jan 2025 18:34:18 -0500 X-MC-Unique: 6NSDnLaZPwSRWloZEf12ug-1 X-Mimecast-MFC-AGG-ID: 6NSDnLaZPwSRWloZEf12ug 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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E318A19560B1; Wed, 8 Jan 2025 23:34:13 +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 DD72519560AE; Wed, 8 Jan 2025 23:34:05 +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 09/11] khugepaged: add mTHP support Date: Wed, 8 Jan 2025 16:31:25 -0700 Message-ID: <20250108233128.14484-10-npache@redhat.com> In-Reply-To: <20250108233128.14484-1-npache@redhat.com> References: <20250108233128.14484-1-npache@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Stat-Signature: oe1n7zeahdpmy1h7ce9jaa8n6sx1bbf1 X-Rspam-User: X-Rspamd-Queue-Id: B40118000D X-Rspamd-Server: rspam08 X-HE-Tag: 1736379262-236338 X-HE-Meta: U2FsdGVkX182qhSZ+6RLGmfmXjjAZTwOQKu97LKB7nzOvBy2F53oAstXEpBspidzG1GsbcWwlsaXdxzf1npgfdh+8/K1yKqugfcVr5AIGlcth52qBrLajjFPWl2L5xy4KaAlL+AOiaZFFyGIb0+L0xZcXuYXblDScenC3GL5q8Agonsj52/UCEtMaErv60stYFzlKmz16nlvi3+Yci9Vr8xGZgUcXuA2zB5Qr4rIrtZzg+6JnJHocaQlI4kDaOD6RhVCETB4I9ZKkZlkmDy0a7FNVqDxA+qXVVRg9S50WrnSmFI9n5x81VB4X8R7AGKsGPQO2evFK849ISnPsx4H6BS/Mu/HtKVloIkPZ0GQF8+5EnhzeV9ZD4S8dERAeap6pFUslI5FveFjlkY2xFshtB7qtIbV8nsM0pZgKyfOrQSkwFCoyQ0qh4kHgvmaZRhpCew3tK1Os8KwfVEs2qWjOeBcfjOCAMnkbtXcXjO0kkW2aSpXWeLj3dOS/iyT5O1WuLQnDR/Q4MQ8lYV7j9K926Opl33gjLBf4zI+vysSVdhvCSdwYWO3VBDbA5yUCyoVqNdgrxiGpIAc3Ue3Pqrn6YH/Ns+qhHvwTe3h1ti7i8+vM73DH3iIpwqAZaG/RM3tS85PDQLy4KEkJmDJ8pYYEfeL3LiGmx2lRVj1PqwWA3kNrKtIpRFJ4quBSgbPvbxWBAecoxdPrCH5dba+bdCjBY3GbFWO8QCQDW1ccIgWVSYrqkKqEpp6Evf+npUE/pMcNSiJ25iYtp1pbhiWg1jd/WmNyszorCqEyvTVCk5C5+PqmQBK0Hvj1MCCW9Dp++N5yl78+6EteL6LsuhKJFAXEGo9MdZrQJavEKvH0y3uo1wiA+YevgX5ED/8Vx7LP4vO4klVNMWPKVdnskEUc8xg4VhnllitCxkop4Cl4psawswVQe2EBoiRxEqXLXysncoK5xyBHUTGfmhTWPYHiVg JnHq89iN QyAQaPRHxf1HY2I0KoPG/By7FL9Aug7gIf+uLcxV7e5KXbZi5Omho6dO2rwEZfJKSSH15aFvNv9mos2dTN3lbg5L9TcFtTQdH3ikfZMt70fEiTSLQdVIcyJUsAbiVyUnmWxgEN4kIhRfbRkgrUzqfTmcsJB+QH35GeP3G8Lq7ZnimbujAXp5Ah3sd8TqLhWMBVE3rHNCjs8VcbP0cBrfTn0sjBskmajAhXpgNxqvcXGe7QWFSReaU+GYuc4XekTv3FzwqhVvdj/KLsRi9XrU6TxvuNRJErNVMH4fn6Gi0I9QYbFl1CYUtx0FfYB+Md0uYTtTWh4Ihc3VyEAMaLu7KEGh4YCoACZ8XbC9JFm+vycVsTHdDlLmvKITAA7SSJcGmLfchf5RASqp/0fM= 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: Introduce the ability for khugepaged to collapse to different mTHP sizes. While scanning a PMD range for potential hugepage collapse, track pages in MIN_MTHP_ORDER chunks. Each bit represents a fully utilized region of order MIN_MTHP_ORDER ptes. With this bitmap we can determine which mTHP sizes would be the most efficient to collapse to if the PMD collapse is not suitible. Signed-off-by: Nico Pache --- mm/khugepaged.c | 111 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 77 insertions(+), 34 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index de1dc6ea3c71..4d3c560f20b4 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1139,13 +1139,14 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, { LIST_HEAD(compound_pagelist); pmd_t *pmd, _pmd; - pte_t *pte; + pte_t *pte, mthp_pte; pgtable_t pgtable; struct folio *folio; spinlock_t *pmd_ptl, *pte_ptl; int result = SCAN_FAIL; struct vm_area_struct *vma; struct mmu_notifier_range range; + unsigned long _address = address + offset * PAGE_SIZE; VM_BUG_ON(address & ~HPAGE_PMD_MASK); /* if collapsing mTHPs we may have already released the read_lock, and @@ -1162,12 +1163,13 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, mmap_read_unlock(mm); *mmap_locked = false; - result = alloc_charge_folio(&folio, mm, cc, HPAGE_PMD_ORDER); + result = alloc_charge_folio(&folio, mm, cc, order); if (result != SCAN_SUCCEED) goto out_nolock; mmap_read_lock(mm); - result = hugepage_vma_revalidate(mm, address, true, &vma, cc, HPAGE_PMD_ORDER); + *mmap_locked = true; + result = hugepage_vma_revalidate(mm, address, true, &vma, cc, order); if (result != SCAN_SUCCEED) { mmap_read_unlock(mm); goto out_nolock; @@ -1185,13 +1187,14 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, * released when it fails. So we jump out_nolock directly in * that case. Continuing to collapse causes inconsistency. */ - result = __collapse_huge_page_swapin(mm, vma, address, pmd, - referenced, HPAGE_PMD_ORDER); + result = __collapse_huge_page_swapin(mm, vma, _address, pmd, + referenced, order); if (result != SCAN_SUCCEED) goto out_nolock; } mmap_read_unlock(mm); + *mmap_locked = false; /* * Prevent all access to pagetables with the exception of * gup_fast later handled by the ptep_clear_flush and the VM @@ -1201,7 +1204,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, * mmap_lock. */ mmap_write_lock(mm); - result = hugepage_vma_revalidate(mm, address, true, &vma, cc, HPAGE_PMD_ORDER); + result = hugepage_vma_revalidate(mm, address, true, &vma, cc, order); if (result != SCAN_SUCCEED) goto out_up_write; /* check if the pmd is still valid */ @@ -1212,11 +1215,12 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, vma_start_write(vma); anon_vma_lock_write(vma->anon_vma); - mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, address, - address + HPAGE_PMD_SIZE); + mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, _address, + _address + (PAGE_SIZE << order)); mmu_notifier_invalidate_range_start(&range); pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */ + /* * This removes any huge TLB entry from the CPU so we won't allow * huge and small TLB entries for the same virtual address to @@ -1230,10 +1234,10 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, mmu_notifier_invalidate_range_end(&range); tlb_remove_table_sync_one(); - pte = pte_offset_map_lock(mm, &_pmd, address, &pte_ptl); + pte = pte_offset_map_lock(mm, &_pmd, _address, &pte_ptl); if (pte) { - result = __collapse_huge_page_isolate(vma, address, pte, cc, - &compound_pagelist, HPAGE_PMD_ORDER); + result = __collapse_huge_page_isolate(vma, _address, pte, cc, + &compound_pagelist, order); spin_unlock(pte_ptl); } else { result = SCAN_PMD_NULL; @@ -1262,8 +1266,8 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, anon_vma_unlock_write(vma->anon_vma); result = __collapse_huge_page_copy(pte, folio, pmd, _pmd, - vma, address, pte_ptl, - &compound_pagelist, HPAGE_PMD_ORDER); + vma, _address, pte_ptl, + &compound_pagelist, order); pte_unmap(pte); if (unlikely(result != SCAN_SUCCEED)) goto out_up_write; @@ -1274,20 +1278,37 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, * write. */ __folio_mark_uptodate(folio); - pgtable = pmd_pgtable(_pmd); - - _pmd = mk_huge_pmd(&folio->page, vma->vm_page_prot); - _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma); - - spin_lock(pmd_ptl); - BUG_ON(!pmd_none(*pmd)); - folio_add_new_anon_rmap(folio, vma, address, RMAP_EXCLUSIVE); - folio_add_lru_vma(folio, vma); - pgtable_trans_huge_deposit(mm, pmd, pgtable); - set_pmd_at(mm, address, pmd, _pmd); - update_mmu_cache_pmd(vma, address, pmd); - deferred_split_folio(folio, false); - spin_unlock(pmd_ptl); + if (order == HPAGE_PMD_ORDER) { + pgtable = pmd_pgtable(_pmd); + _pmd = mk_huge_pmd(&folio->page, vma->vm_page_prot); + _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma); + + spin_lock(pmd_ptl); + BUG_ON(!pmd_none(*pmd)); + folio_add_new_anon_rmap(folio, vma, _address, RMAP_EXCLUSIVE); + folio_add_lru_vma(folio, vma); + pgtable_trans_huge_deposit(mm, pmd, pgtable); + set_pmd_at(mm, address, pmd, _pmd); + update_mmu_cache_pmd(vma, address, pmd); + deferred_split_folio(folio, false); + spin_unlock(pmd_ptl); + } else { //mTHP + mthp_pte = mk_pte(&folio->page, vma->vm_page_prot); + mthp_pte = maybe_mkwrite(pte_mkdirty(mthp_pte), vma); + + spin_lock(pmd_ptl); + folio_ref_add(folio, (1 << order) - 1); + folio_add_new_anon_rmap(folio, vma, _address, RMAP_EXCLUSIVE); + folio_add_lru_vma(folio, vma); + spin_lock(pte_ptl); + set_ptes(vma->vm_mm, _address, pte, mthp_pte, (1 << order)); + update_mmu_cache_range(NULL, vma, _address, pte, (1 << order)); + spin_unlock(pte_ptl); + smp_wmb(); /* make pte visible before pmd */ + pmd_populate(mm, pmd, pmd_pgtable(_pmd)); + deferred_split_folio(folio, false); + spin_unlock(pmd_ptl); + } folio = NULL; @@ -1367,21 +1388,26 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, { pmd_t *pmd; pte_t *pte, *_pte; + int i; int result = SCAN_FAIL, referenced = 0; int none_or_zero = 0, shared = 0; struct page *page = NULL; struct folio *folio = NULL; unsigned long _address; + unsigned long enabled_orders; spinlock_t *ptl; int node = NUMA_NO_NODE, unmapped = 0; bool writable = false; - + bool all_valid = true; + unsigned long tva_flags = cc->is_khugepaged ? TVA_ENFORCE_SYSFS : 0; VM_BUG_ON(address & ~HPAGE_PMD_MASK); result = find_pmd_or_thp_or_none(mm, address, &pmd); if (result != SCAN_SUCCEED) goto out; + bitmap_zero(cc->mthp_bitmap, 1 << (HPAGE_PMD_ORDER - MIN_MTHP_ORDER)); + bitmap_zero(cc->mthp_bitmap_temp, 1 << (HPAGE_PMD_ORDER - MIN_MTHP_ORDER)); memset(cc->node_load, 0, sizeof(cc->node_load)); nodes_clear(cc->alloc_nmask); pte = pte_offset_map_lock(mm, pmd, address, &ptl); @@ -1390,8 +1416,12 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, goto out; } - for (_address = address, _pte = pte; _pte < pte + HPAGE_PMD_NR; - _pte++, _address += PAGE_SIZE) { + for (i = 0; i < HPAGE_PMD_NR; i++) { + if (i % MIN_MTHP_NR == 0) + all_valid = true; + + _pte = pte + i; + _address = address + i * PAGE_SIZE; pte_t pteval = ptep_get(_pte); if (is_swap_pte(pteval)) { ++unmapped; @@ -1414,6 +1444,7 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, } } if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { + all_valid = false; ++none_or_zero; if (!userfaultfd_armed(vma) && (!cc->is_khugepaged || @@ -1514,7 +1545,15 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, folio_test_referenced(folio) || mmu_notifier_test_young(vma->vm_mm, address))) referenced++; + + /* + * we are reading in MIN_MTHP_NR page chunks. if there are no empty + * pages keep track of it in the bitmap for mTHP collapsing. + */ + if (all_valid && (i + 1) % MIN_MTHP_NR == 0) + bitmap_set(cc->mthp_bitmap, i / MIN_MTHP_NR, 1); } + if (!writable) { result = SCAN_PAGE_RO; } else if (cc->is_khugepaged && @@ -1527,10 +1566,12 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, out_unmap: pte_unmap_unlock(pte, ptl); if (result == SCAN_SUCCEED) { - result = collapse_huge_page(mm, address, referenced, - unmapped, cc, mmap_locked, HPAGE_PMD_ORDER, 0); - /* collapse_huge_page will return with the mmap_lock released */ - *mmap_locked = false; + enabled_orders = thp_vma_allowable_orders(vma, vma->vm_flags, + tva_flags, THP_ORDERS_ALL_ANON); + result = khugepaged_scan_bitmap(mm, address, referenced, unmapped, cc, + mmap_locked, enabled_orders); + if (result > 0) + result = SCAN_SUCCEED; } out: trace_mm_khugepaged_scan_pmd(mm, &folio->page, writable, referenced, @@ -2477,11 +2518,13 @@ static int khugepaged_collapse_single_pmd(unsigned long addr, struct mm_struct * fput(file); if (result == SCAN_PTE_MAPPED_HUGEPAGE) { mmap_read_lock(mm); + *mmap_locked = true; if (khugepaged_test_exit_or_disable(mm)) goto end; result = collapse_pte_mapped_thp(mm, addr, !cc->is_khugepaged); mmap_read_unlock(mm); + *mmap_locked = false; } } else { result = khugepaged_scan_pmd(mm, vma, addr,