From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) (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 DEF7D361DAB for ; Sun, 15 Mar 2026 15:16:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773587795; cv=none; b=Bf/toVez5SVaMc6x1u9fWTCbqrg9YmQS0o0LjhfbASt5A3BlGKhtLPRxsT4Mmk7fumfReX+IpjVdzv8x9KxBx6vVw0FxOOc3dexMVI7qlwt1y8F+tYqryLDcOu9eSc8X3BmW8poVXN67w1gymslaE4bAoeDNRprGYx3tloq4720= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773587795; c=relaxed/simple; bh=f3RD/RWN8eCl0DydQB3JKx99EjrDOvvqBUUt0uXcpwk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=biS5iTFAuHxSZ/qaLl1b+Fr0QCao7FBB/ab49rHGDDJhOgP2T9TuvFACCfXa0ey31w7+8SXHncBJZPg/zwKLI1GkYvHbTB7wn0jRygv+AxK1Lun9q9qsEK5KvzIEC1CnYTDXYzL1oHgZlR3cST9w0IsSgYJh08l0em1PDhbE2eI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=EwAlJNi3; arc=none smtp.client-ip=91.218.175.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="EwAlJNi3" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1773587789; 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=UFUEeOE40dUbmpNmOseZzqA/dkA/x7XhqP12l7OJCEw=; b=EwAlJNi3CDgDSDQ/bmR+8tHhWfFADikcQ8X3kT+FZHI6WU8bhvOTcpenGxM4vlz4hg5rCN 5vgAqxC9F6Xn/PoCQFPwkGplqXe0XtSagqtGRifhfTsbPAkZZ+mwD4kLhBKUo0ZO9xS8gA QyUS0VoPam+6VxVSrW5aNhxmZuuiVJo= From: Lance Yang To: npache@redhat.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, aarcange@redhat.com, akpm@linux-foundation.org, 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 Subject: Re: [PATCH mm-unstable v3 5/5] mm/khugepaged: unify khugepaged and madv_collapse with collapse_single_pmd() Date: Sun, 15 Mar 2026 23:16:20 +0800 Message-Id: <20260315151620.30552-1-lance.yang@linux.dev> In-Reply-To: <20260311211315.450947-6-npache@redhat.com> References: <20260311211315.450947-6-npache@redhat.com> 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=UTF-8 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT On Wed, Mar 11, 2026 at 03:13:15PM -0600, Nico Pache wrote: >The khugepaged daemon and madvise_collapse have two different >implementations that do almost the same thing. Create collapse_single_pmd >to increase code reuse and create an entry point to these two users. > >Refactor madvise_collapse and collapse_scan_mm_slot to use the new >collapse_single_pmd function. This introduces a minor behavioral change >that is most likely an undiscovered bug. The current implementation of >khugepaged tests collapse_test_exit_or_disable before calling >collapse_pte_mapped_thp, but we weren't doing it in the madvise_collapse >case. By unifying these two callers madvise_collapse now also performs >this check. We also modify the return value to be SCAN_ANY_PROCESS which >properly indicates that this process is no longer valid to operate on. > >By moving the madvise_collapse writeback-retry logic into the helper >function we can also avoid having to revalidate the VMA. > >We also guard the khugepaged_pages_collapsed variable to ensure its only >incremented for khugepaged. > >Signed-off-by: Nico Pache >--- > mm/khugepaged.c | 120 +++++++++++++++++++++++++----------------------- > 1 file changed, 63 insertions(+), 57 deletions(-) > >diff --git a/mm/khugepaged.c b/mm/khugepaged.c >index 33ae56e313ed..733c4a42c2ce 100644 >--- a/mm/khugepaged.c >+++ b/mm/khugepaged.c >@@ -2409,6 +2409,65 @@ static enum scan_result collapse_scan_file(struct mm_struct *mm, > return result; > } > >+/* >+ * Try to collapse a single PMD starting at a PMD aligned addr, and return >+ * the results. >+ */ >+static enum scan_result collapse_single_pmd(unsigned long addr, >+ struct vm_area_struct *vma, bool *mmap_locked, >+ struct collapse_control *cc) >+{ >+ struct mm_struct *mm = vma->vm_mm; >+ bool triggered_wb = false; >+ enum scan_result result; >+ struct file *file; >+ pgoff_t pgoff; >+ >+ if (vma_is_anonymous(vma)) { >+ result = collapse_scan_pmd(mm, vma, addr, mmap_locked, cc); >+ goto end; >+ } >+ >+ file = get_file(vma->vm_file); >+ pgoff = linear_page_index(vma, addr); >+ >+ mmap_read_unlock(mm); >+ *mmap_locked = false; >+retry: >+ result = collapse_scan_file(mm, addr, file, pgoff, cc); >+ >+ /* >+ * For MADV_COLLAPSE, when encountering dirty pages, try to writeback, >+ * then retry the collapse one time. >+ */ >+ if (!cc->is_khugepaged && result == SCAN_PAGE_DIRTY_OR_WRITEBACK && >+ !triggered_wb && mapping_can_writeback(file->f_mapping)) { >+ const loff_t lstart = (loff_t)pgoff << PAGE_SHIFT; >+ const loff_t lend = lstart + HPAGE_PMD_SIZE - 1; >+ >+ filemap_write_and_wait_range(file->f_mapping, lstart, lend); >+ triggered_wb = true; >+ goto retry; While the old retry path did go back through hugepage_vma_revalidate(), the retry itself is not relying on the original VMA remaining unchanged IIUC. After dropping mmap_lock, the code still holds a reference to the file, so no lifetime issue should arise here :) So, LGTM! Reviewed-by: Lance Yang Cheers, Lance