From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from flow-a3-smtp.messagingengine.com (flow-a3-smtp.messagingengine.com [103.168.172.138]) (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 A841334C9A6; Mon, 24 Aug 2026 14:13:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.138 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787580797; cv=none; b=FWCMlafqZE43mrxQMoFJQuQkLVcnycQS74SSCoS2RMwp9E5IO6JZJ3myZSaZgvUykHaGcyGX1p9AuZyakjM6UjeDCYQw9/PTVUu2ZKDpoC7paBVh+5jwC/JAjBZDSSSnRShVkaI/3lBz1JBYSEe/ChzWnuLfBK05zvSrhVxJ4XY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787580797; c=relaxed/simple; bh=G1QNp5h3QCg/occyjcJSeBpT5BqfFu02ZpX4idrSsPw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=kagFydNhrHYDOWL+Vn7ddPVOjdyO1xpFby0loJGr+CHbx9XZusV896oupqtMLslWau0brFsJt+nC23qKfcQWwKJFck9iDWFZXTkGIVozR6QxACbBf41mUoc7LBDyro2b35LEMOU6x2LEp4ud7hwVYdXu2eGJCgfqJedLoxVwhGs= 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=iP63UKXB; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=LJp4A8dO; arc=none smtp.client-ip=103.168.172.138 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="iP63UKXB"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="LJp4A8dO" Received: from phl-compute-02.internal (phl-compute-02.internal [10.202.2.42]) by mailflow.phl.internal (Postfix) with ESMTP id C01AB13801E4; Mon, 24 Aug 2026 10:13:14 -0400 (EDT) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-02.internal (MEProxy); Mon, 24 Aug 2026 10:13:14 -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=1787580794; x= 1787587994; bh=sGr2voTn/4URYLRPHsH5IQjOMwzItseS07qYBk1Gieg=; b=i P63UKXBP8FWswjF/FJlq4Xe5RuBitvDLEwD8PRAfeP1vVx7qDO4r6OYRZXmj+W0t tDOyEyhf5c8Scg1TKL9sAw/u29dW62XdNZwmqMNPKFfW7dId/14J2bIuizLUuVN0 DlB/pVYr6sFZvjCwzSg33ZbNs4RapL1uzskaySoqxUVgRM4DjrRS1X2szQ00frjk ZKaVAtonwJ9JaSAIMnbOSWqtF++OCB+EgxhE023V7z2jSFX1BqEJCijQF8ydXBFl auHff9b5YmubO7F4qR6FiCuDuBSHnjTi0oiBy8OQhh2o5k6IumQiI8gy8hqWoRGV L5iZnJE6j91qvSZ1E+ZxA== 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=fm3; t= 1787580794; x=1787587994; bh=sGr2voTn/4URYLRPHsH5IQjOMwz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QpRm6lgqA2Oo/WwA X-ME-Proxy: Feedback-ID: ie3994620:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 24 Aug 2026 10:13:12 -0400 (EDT) Date: Mon, 24 Aug 2026 15:13:10 +0100 From: Kiryl Shutsemau To: Lance Yang , Johannes Weiner Cc: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, nico.pache@linux.dev, baolin.wang@linux.alibaba.com, baohua@kernel.org, dev.jain@arm.com, hughd@google.com, liam@infradead.org, mhocko@suse.com, rppt@kernel.org, ryan.roberts@arm.com, shuah@kernel.org, surenb@google.com, usama.arif@linux.dev, vbabka@kernel.org, ziy@nvidia.com, usama.anjum@arm.com, agordeev@linux.ibm.com, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, jannh@google.com, willy@infradead.org, pfalcato@suse.de, rostedt@goodmis.org, mhiramat@kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org Subject: Re: [RFC PATCH 16/57] mm/collapse: freeze the sources behind migration entries Message-ID: References: <20260816224609.308019-17-kirill@shutemov.name> <20260824131224.73344-1-lance.yang@linux.dev> Precedence: bulk X-Mailing-List: linux-trace-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: <20260824131224.73344-1-lance.yang@linux.dev> On Mon, Aug 24, 2026 at 09:12:24PM +0800, Lance Yang wrote: > > On Sun, Aug 16, 2026 at 11:45:28PM +0100, Kiryl Shutsemau wrote: > >+ if (!folio_ref_freeze(folio, > >+ folio_expected_ref_count(folio) + 1)) { > >+ result = SCAN_PAGE_COUNT; > >+ goto unfreeze; > >+ } > >+ nr_frozen = nr_saved; > > Just one thing I was wondering about ... can deferred_split_isolate() > remove a source folio from deferred_split_lru while its refcount is > frozen by collapse_freeze_candidate()? > > Assume an earlier span belongs to an anonymous large folio on the > deferred split queue, then a later span fails folio_trylock(). > collapse_freeze_candidate() continues after freezing each source folio > and calls collapse_unfreeze_candidate() on a later failure: > > static noinline enum scan_result collapse_freeze_candidate(struct mm_struct *mm, > struct collapse_candidate *cand, pte_t *pte) > { > ... > for (i = 0, addr = cand->addr; i < nr_pages;) { > ... > if (!folio_trylock(folio)) { > folio_put(folio); > result = SCAN_PAGE_LOCK; > goto unfreeze; > } > ... > nr_saved = i + nr; > > if (!folio_ref_freeze(folio, > folio_expected_ref_count(folio) + 1)) { > result = SCAN_PAGE_COUNT; > goto unfreeze; > } > nr_frozen = nr_saved; > > i += nr; > addr += nr * PAGE_SIZE; > } > > ... > unfreeze: > collapse_unfreeze_candidate(mm, cand, pte, nr_saved, nr_frozen); > return result; > } > > folio_ref_freeze() takes the source folio's refcount to zero: > > static inline int folio_ref_freeze(struct folio *folio, int count) > { > return page_ref_freeze(&folio->page, count); > } > > static inline int page_ref_freeze(struct page *page, int count) > { > int ret = likely(atomic_cmpxchg(&page->_refcount, count, 0) == count); > > ... > return ret; > } > > While collapse_freeze_candidate() still holds the source folio lock, > deferred_split_scan() can call deferred_split_isolate(): > > static unsigned long deferred_split_scan(struct shrinker *shrink, > struct shrink_control *sc) > { > LIST_HEAD(dispose); > struct folio *folio, *next; > int split = 0; > unsigned long isolated; > > isolated = list_lru_shrink_walk_irq(&deferred_split_lru, sc, > deferred_split_isolate, &dispose); > } > > static enum lru_status deferred_split_isolate(struct list_head *item, > struct list_lru_one *lru, > void *cb_arg) > { > struct folio *folio = container_of(item, struct folio, _deferred_list); > struct list_head *freeable = cb_arg; > > if (folio_try_get(folio)) { > list_lru_isolate_move(lru, item, freeable); > return LRU_REMOVED; > } > > /* > * We lost race with folio_put(). Read folio state before the > * isolate: folio_unqueue_deferred_split() checks list_empty() > * locklessly, so once removed the folio can be freed any time. > */ > if (folio_test_partially_mapped(folio)) { > folio_clear_partially_mapped(folio); > mod_mthp_stat(folio_order(folio), > MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1); > } > list_lru_isolate(lru, item); > return LRU_REMOVED; > } > > And folio_try_get() fails because the source folio has a frozen refcount. > deferred_split_isolate() treats the failure as a race with folio_put(), > clears PG_partially_mapped and its MTHP_STAT_NR_ANON_PARTIALLY_MAPPED > accounting when set, then removes the folio from deferred_split_lru ... Hm. So, the premise in deferred_split_isolate() is false: !folio_try_get() doesn't mean lost race with folio_put(). I think deferred_split_isolate() should do something like: if (!folio_try_get(folio)) return LRU_SKIP; list_lru_isolate_move(lru, item, freeable); return LRU_REMOVED; Johannes, do I miss something? > > >+ > >+ i += nr; > >+ addr += nr * PAGE_SIZE; > >+ } > >+ > >+ cand->state = CAND_FROZEN; > >+ return SCAN_SUCCEED; > >+ > >+unfreeze: > >+ collapse_unfreeze_candidate(mm, cand, pte, nr_saved, nr_frozen); > >+ return result; > > And collapse_unfreeze_candidate() restores the source PTEs and refcount, > then unlocks and puts the source folio :) > > The source folio is not added back to the deferred split queue, so an > underused or partially mapped folio can remain off the queue. > > Should collapse unqueue the source folio after folio_ref_freeze() > succeeds, remember whether it was on the deferred split queue and whether > PG_partially_mapped was set, then requeue it if > collapse_unfreeze_candidate() restores the source folio? > > [...] > > Cheers, Lance -- Kiryl Shutsemau / Kirill A. Shutemov