From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from fout-a5-smtp.messagingengine.com (fout-a5-smtp.messagingengine.com [103.168.172.148]) (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 E5797377A82 for ; Fri, 11 Sep 2026 15:24:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.148 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789140296; cv=none; b=cwAhIOPz1UxxWD//LHo8P6zlTS/v2lP3AxLfp6sgLl/vTCVqRqx0IBSIkOvP/zjaykOp8NWVWgmBWvmXTq+wrHKFeSOkIOFGr95t+7xK421TyZUH8gy/EJft71HOO+mbtVH7v1IxUIdsySekS8NZ87YwgTF9CRVOc2IzUQ7HyA0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789140296; c=relaxed/simple; bh=vJsMiuSSl7OOUCYsBkM8650K2BhhXPF3mMDSrYWV6vc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=BB2oP/zv+OXf7nwF91/RB2ugCLxol8k40eEwd4d+HXC/lnW+7hxW0AA/wNsHdPdPtLrY6K+A8/Cvo+4njxBIM9deJ3b3ds5VB7iRl44RGXIqE3Jcp/6pdtqnQsyEHOCvpoIyVLHlmNUZ/WY4UDAKo2oWsKMIT02mMqM3niptkVY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name; spf=pass smtp.mailfrom=shutemov.name; dkim=pass (2048-bit key) header.d=shutemov.name header.i=@shutemov.name header.b=XrTwOd06; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=UtGeVKxj; arc=none smtp.client-ip=103.168.172.148 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=shutemov.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=shutemov.name header.i=@shutemov.name header.b="XrTwOd06"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="UtGeVKxj" Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfout.phl.internal (Postfix) with ESMTP id E211FEC03A9; Fri, 11 Sep 2026 11:24:52 -0400 (EDT) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-01.internal (MEProxy); Fri, 11 Sep 2026 11:24:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov.name; h=cc:cc:content-type:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1789140292; x= 1789226692; bh=HAUzE7enydkaOLoMLY2SEAq0TLztLqE8++ymmdkXwxM=; b=X rTwOd06S3cmlFUR/8CmFPLqHYPzH0GAdJErMbx1qtnsFZtyZwmaZ16fH449B98mD 15P8/eGL7KJuMRrMR/bfsY/CKslGVhFsJaE4wZdEYyIEYtiWiP0BsuIxQOMgay3G TrTukiUaYJUmFsWxbb2JdPFYXKybguv9FVyRiNZhkFIO9Cg7hFkuYIrmQbz8FXAz 8l4a7PN2ufOdCSHOA1A68egPhhNGIqVdJmWAz+K2ePao1Ghv5FgM+q0mqgSR6ZwZ T0igMD4zBgBzYJa3Ek5EEqtkTfjQ1wyGiT6qKcjdFAFLxB88rrC5lXDFFfshWGZC KxsHhDVhNCUQrSarRAb0A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1789140292; x=1789226692; bh=HA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BMtmr2VOVs6MDJxr6DqjJJlfXYPQ2+FZdjw X-ME-Proxy: Feedback-ID: ie3994620:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 11 Sep 2026 11:24:51 -0400 (EDT) Date: Fri, 11 Sep 2026 16:24:51 +0100 From: Kiryl Shutsemau To: Zi Yan Cc: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Baolin Wang , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Vlastimil Babka , Jann Horn Subject: Re: [PATCH v2 09/12] mm/collapse: open-code collapse_single_pmd() in its two callers Message-ID: References: <20260910120238.2529819-1-kirill@shutemov.name> <20260910120238.2529819-10-kirill@shutemov.name> 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-Disposition: inline In-Reply-To: On Fri, Sep 11, 2026 at 10:57:46AM -0400, Zi Yan wrote: > On Thu Sep 10, 2026 at 8:02 AM EDT, Kiryl Shutsemau wrote: > > From: "Kiryl Shutsemau (Meta)" > > > > collapse_scan_pmd() and collapse_run_pmd() each have a clear locking > > contract. The scan is called with mmap_lock held for reading and returns > > with it still held. The collapse is called without it. > > > > collapse_single_pmd() kept that boundary inside itself. It dropped the > > lock on some paths and not others, and reported which by way of a bool its > > callers had to carry along and then act on. > > > > Open-code it in the two callers. Each scans under the lock it already > > holds and, on SCAN_SUCCEED, gives the lock up before running the collapse. > > khugepaged's lock_dropped and madvise_collapse()'s mmap_unlocked both go: > > the code dropping the lock is now the code that wanted to know. > > > > khugepaged's walk carries on to the next table while the scan keeps > > refusing, and ends once a collapse has taken the lock from under it. > > madvise_collapse() re-finds its VMA after a collapse, which it did before, > > and now uses a NULL vma to say that it has to. It still reports the drop > > to its own caller, from the line that does it. > > > > The lock is given up and taken again at the same points as before. No > > functional change. > > > > Assisted-by: LLM > > Signed-off-by: Kiryl Shutsemau (Meta) > > --- > > mm/khugepaged.c | 102 +++++++++++++++++++++++------------------------- > > 1 file changed, 49 insertions(+), 53 deletions(-) > > > > LGTM. Thanks. > > Reviewed-by: Zi Yan > > One question: > > What prevents us from doing: > while () { > 1. mmap_lock > 2. scan_pmd > 3. mmap_unlock, bail out if needed > 4. run_pmd > } > > for both cases? It improves readability. What is the downside of > dropping the lock during multiple scans? The scan/run ratio. Once memory is mostly huge nearly every scan refuses, and a refused table is cheap: one pmd read for SCAN_PMD_MAPPED, one PTE walk under the PTL otherwise. In your version of the collapse loop, mmap lock/unlock plus VMA revalidation would dominate the cost. It is not productive. Note that scan in khugepaged is bounded by pages_to_scan so we would not hog the lock. > for madvise_collapse(), I see mmap_read_lock is held when it is called, > so it can be dropped at the entry and the code makes sure it is held at > the exit. That makes every MADV_COLLAPSE report lock_dropped, including one on a range that is already huge, which today never lets the lock go. The same performance consideration as above. -- Kiryl Shutsemau / Kirill A. Shutemov