From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 895B74CB5B for ; Thu, 26 Mar 2026 00:25:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774484703; cv=none; b=izkrXLa0sIHrypOk7IKXYB8CXt16Kezr/U2TpooWZF3zhgVnuH7k82+3zuUA6mM0mC3FL+yyR3koD1PikWPduKXs1krJ7tRPbfo0uxlp0/fzWcMQ3B4pus5cMHBrOwLFRoMzRvahB0r28xfHjKSBw1GGvjz6IYIMpea81W+BJKo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774484703; c=relaxed/simple; bh=jw10rFuWh7AsZhfu1sKiKIvAy3LPVihvatPrrfiKFLc=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=WlBLocrAzxu5VrxkFRt8GyM2gkAzKDVflCIiGmf1SPOJVZzw+2rrPP4w4F0FWNwM74FAh/aCX4YLdAk0A5cF6KdKMvQvVpHBWqrvLnSr/lMBR5SM3mUipY2jhi6cEocLyn6jZwNlW9o4BHH/27f6OH+HqRXle8YchYln8C6QfLA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=r5mCPAkQ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="r5mCPAkQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 05A18C4CEF7; Thu, 26 Mar 2026 00:25:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1774484703; bh=jw10rFuWh7AsZhfu1sKiKIvAy3LPVihvatPrrfiKFLc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=r5mCPAkQne0USNTuu/AZVcKbCG0T9TWkBm8kbK/GAKN3MTLKUZy0npSosa5zlCJl7 V5A26W0Eq8bPTwdGZ5W0f+0VVhJUmi5EFHJ1iaxH5S6Jj56Dbn/aaCXy4XZ4PKFNyx aZXWKHTumk2XC+gW+wI0O8ZTM6EhI9pW+t9cMKmg= Date: Wed, 25 Mar 2026 17:25:00 -0700 From: Andrew Morton To: Nico Pache Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, aarcange@redhat.com, anshuman.khandual@arm.com, apopple@nvidia.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, byungchul@sk.com, catalin.marinas@arm.com, cl@gentwo.org, corbet@lwn.net, dave.hansen@linux.intel.com, david@kernel.org, dev.jain@arm.com, gourry@gourry.net, hannes@cmpxchg.org, hughd@google.com, jackmanb@google.com, jack@suse.cz, jannh@google.com, jglisse@google.com, joshua.hahnjy@gmail.com, kas@kernel.org, lance.yang@linux.dev, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, mathieu.desnoyers@efficios.com, matthew.brost@intel.com, mhiramat@kernel.org, mhocko@suse.com, peterx@redhat.com, pfalcato@suse.de, rakie.kim@sk.com, raquini@redhat.com, rdunlap@infradead.org, richard.weiyang@gmail.com, rientjes@google.com, rostedt@goodmis.org, rppt@kernel.org, ryan.roberts@arm.com, shivankg@amd.com, sunnanyong@huawei.com, surenb@google.com, thomas.hellstrom@linux.intel.com, tiwai@suse.de, usamaarif642@gmail.com, vbabka@suse.cz, vishal.moola@gmail.com, wangkefeng.wang@huawei.com, will@kernel.org, willy@infradead.org, yang@os.amperecomputing.com, ying.huang@linux.alibaba.com, ziy@nvidia.com, zokeefe@google.com, Roman Gushchin Subject: Re: [PATCH mm-unstable v4 0/5] mm: khugepaged cleanups and mTHP prerequisites Message-Id: <20260325172500.990e240d813a4b2db300e0e9@linux-foundation.org> In-Reply-To: <20260325114022.444081-1-npache@redhat.com> References: <20260325114022.444081-1-npache@redhat.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 25 Mar 2026 05:40:17 -0600 Nico Pache wrote: > MAINTAINER NOTE: This is based on mm-unstable with the coresponding > patches reverted then reapplied. Unfortunately the update-in-place trick fooled AI review, which might have been useful. Oh well. In retrospect we could have avoided that by you asking me to drop v3 a couple of days before mailing out v4. otoh, this series *does* apply to the mm-stable branch. Roman, I though Sashiko is attempting that? > The following series contains cleanups and prerequisites for my work on > khugepaged mTHP support [1]. These have been separated out to ease review. And boy that's a lot of reviewers! Aren't you a lucky ducky ;) > The first patch in the series refactors the page fault folio to pte mapping > and follows a similar convention as defined by map_anon_folio_pmd_(no)pf(). > This not only cleans up the current implementation of do_anonymous_page(), > but will allow for reuse later in the khugepaged mTHP implementation. > > The second patch adds a small is_pmd_order() helper to check if an order is > the PMD order. This check is open-coded in a number of places. This patch > aims to clean this up and will be used more in the khugepaged mTHP work. > The third patch also adds a small DEFINE for (HPAGE_PMD_NR - 1) which is > used often across the khugepaged code. > > The fourth and fifth patch come from the khugepaged mTHP patchset [1]. > These two patches include the rename of function prefixes, and the > unification of khugepaged and madvise_collapse via a new > collapse_single_pmd function. > > Patch 1: refactor do_anonymous_page into map_anon_folio_pte_(no)pf > Patch 2: add is_pmd_order helper > Patch 3: Add define for (HPAGE_PMD_NR - 1) > Patch 4: Refactor/rename hpage_collapse > Patch 5: Refactoring to combine madvise_collapse and khugepaged > Thanks, I updated mm.git's mm-unstable branch to this version. > V4 Changes: > - added RB and SB tags > - Patch1: commit message cleanup/additions > - Patch1: constify two variables, and change 1< - Patch1: change zero-page read path to use update_mmu_cache varient > - Patch5: remove dead code switch statement (SCAN_PTE_MAPPED_HUGEPAGE) > - Patch5: remove local mmap_locked from madvise_collapse() > - Patch5: rename mmap_locked to lock_dropped in ..scan_mm_slot() and > invert the logic. the madvise|khugepaged code now share the same > naming convention across both functions. > - Patch5: add assertion to collapse_single_pmd() so both madvise_collapse > and khugepaged assert the lock. > - Patch5: Convert one of the VM_BUG_ON's to VM_WARN_ON Below is how v4 altered mm,git: mm/khugepaged.c | 34 +++++++++++++++------------------- mm/memory.c | 11 +++++------ 2 files changed, 20 insertions(+), 25 deletions(-) --- a/mm/khugepaged.c~b +++ a/mm/khugepaged.c @@ -1250,7 +1250,7 @@ out_nolock: static enum scan_result collapse_scan_pmd(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long start_addr, - bool *mmap_locked, struct collapse_control *cc) + bool *lock_dropped, struct collapse_control *cc) { pmd_t *pmd; pte_t *pte, *_pte; @@ -1425,7 +1425,7 @@ out_unmap: result = collapse_huge_page(mm, start_addr, referenced, unmapped, cc); /* collapse_huge_page will return with the mmap_lock released */ - *mmap_locked = false; + *lock_dropped = true; } out: trace_mm_khugepaged_scan_pmd(mm, folio, referenced, @@ -2422,7 +2422,7 @@ static enum scan_result collapse_scan_fi * the results. */ static enum scan_result collapse_single_pmd(unsigned long addr, - struct vm_area_struct *vma, bool *mmap_locked, + struct vm_area_struct *vma, bool *lock_dropped, struct collapse_control *cc) { struct mm_struct *mm = vma->vm_mm; @@ -2431,8 +2431,10 @@ static enum scan_result collapse_single_ struct file *file; pgoff_t pgoff; + mmap_assert_locked(mm); + if (vma_is_anonymous(vma)) { - result = collapse_scan_pmd(mm, vma, addr, mmap_locked, cc); + result = collapse_scan_pmd(mm, vma, addr, lock_dropped, cc); goto end; } @@ -2440,7 +2442,7 @@ static enum scan_result collapse_single_ pgoff = linear_page_index(vma, addr); mmap_read_unlock(mm); - *mmap_locked = false; + *lock_dropped = true; retry: result = collapse_scan_file(mm, addr, file, pgoff, cc); @@ -2537,21 +2539,21 @@ static void collapse_scan_mm_slot(unsign VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK); while (khugepaged_scan.address < hend) { - bool mmap_locked = true; + bool lock_dropped = false; cond_resched(); if (unlikely(collapse_test_exit_or_disable(mm))) goto breakouterloop; - VM_BUG_ON(khugepaged_scan.address < hstart || + VM_WARN_ON_ONCE(khugepaged_scan.address < hstart || khugepaged_scan.address + HPAGE_PMD_SIZE > hend); *result = collapse_single_pmd(khugepaged_scan.address, - vma, &mmap_locked, cc); + vma, &lock_dropped, cc); /* move to next address */ khugepaged_scan.address += HPAGE_PMD_SIZE; - if (!mmap_locked) + if (lock_dropped) /* * We released mmap_lock so break loop. Note * that we drop mmap_lock before all hugepage @@ -2826,7 +2828,6 @@ int madvise_collapse(struct vm_area_stru unsigned long hstart, hend, addr; enum scan_result last_fail = SCAN_FAIL; int thps = 0; - bool mmap_locked = true; BUG_ON(vma->vm_start > start); BUG_ON(vma->vm_end < end); @@ -2849,10 +2850,10 @@ int madvise_collapse(struct vm_area_stru for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) { enum scan_result result = SCAN_FAIL; - if (!mmap_locked) { + if (*lock_dropped) { cond_resched(); mmap_read_lock(mm); - mmap_locked = true; + *lock_dropped = false; result = hugepage_vma_revalidate(mm, addr, false, &vma, cc); if (result != SCAN_SUCCEED) { @@ -2862,12 +2863,8 @@ int madvise_collapse(struct vm_area_stru hend = min(hend, vma->vm_end & HPAGE_PMD_MASK); } - mmap_assert_locked(mm); - - result = collapse_single_pmd(addr, vma, &mmap_locked, cc); - if (!mmap_locked) - *lock_dropped = true; + result = collapse_single_pmd(addr, vma, lock_dropped, cc); switch (result) { case SCAN_SUCCEED: @@ -2876,7 +2873,6 @@ int madvise_collapse(struct vm_area_stru break; /* Whitelisted set of results where continuing OK */ case SCAN_NO_PTE_TABLE: - case SCAN_PTE_MAPPED_HUGEPAGE: case SCAN_PTE_NON_PRESENT: case SCAN_PTE_UFFD_WP: case SCAN_LACK_REFERENCED_PAGE: @@ -2897,7 +2893,7 @@ int madvise_collapse(struct vm_area_stru out_maybelock: /* Caller expects us to hold mmap_lock on return */ - if (!mmap_locked) + if (*lock_dropped) mmap_read_lock(mm); out_nolock: mmap_assert_locked(mm); --- a/mm/memory.c~b +++ a/mm/memory.c @@ -5201,7 +5201,7 @@ void map_anon_folio_pte_nopf(struct foli struct vm_area_struct *vma, unsigned long addr, bool uffd_wp) { - unsigned int nr_pages = folio_nr_pages(folio); + const unsigned int nr_pages = folio_nr_pages(folio); pte_t entry = folio_mk_pte(folio, vma->vm_page_prot); entry = pte_sw_mkyoung(entry); @@ -5221,10 +5221,10 @@ void map_anon_folio_pte_nopf(struct foli static void map_anon_folio_pte_pf(struct folio *folio, pte_t *pte, struct vm_area_struct *vma, unsigned long addr, bool uffd_wp) { - unsigned int order = folio_order(folio); + const unsigned int order = folio_order(folio); map_anon_folio_pte_nopf(folio, pte, vma, addr, uffd_wp); - add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1 << order); + add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1L << order); count_mthp_stat(order, MTHP_STAT_ANON_FAULT_ALLOC); } @@ -5239,7 +5239,7 @@ static vm_fault_t do_anonymous_page(stru unsigned long addr = vmf->address; struct folio *folio; vm_fault_t ret = 0; - int nr_pages = 1; + int nr_pages; pte_t entry; /* File mapping without ->vm_ops ? */ @@ -5279,8 +5279,7 @@ static vm_fault_t do_anonymous_page(stru set_pte_at(vma->vm_mm, addr, vmf->pte, entry); /* No need to invalidate - it was non-present before */ - update_mmu_cache_range(vmf, vma, addr, vmf->pte, - /*nr_pages=*/ 1); + update_mmu_cache(vma, addr, vmf->pte); goto unlock; } _