From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (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 9979D218ADD for ; Wed, 11 Mar 2026 14:28:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773239329; cv=none; b=rGdP9EkOiVzeSlXLtg4Zfm8v2SeUoRgTtYjvTtROiN1owij+yd9Tx3WRhBUqWUQq8Y3hENIEiTYMloXEi2c+OszkZ12KLYvMAnsS7n3TFYSh1AXqhLgmAk0CO0kdmuiDazeVQLpWgFc1oPJGrBjpXVKUJqhJDlrOAuRu5F7NkCk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773239329; c=relaxed/simple; bh=eWMcpYR7910oFDRm+PgynQzGg51QedpiZsw/lX2kC0I=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=sx+w8oZtFPBp6W2QAvfc3nAehli9rtr4UhaAt94Lk5+5YHLr0sIofxgz4EgjA8IKEH5KBQctJ8pitcqCvtje+UfhH05ThACu6XGMB0I/+0pDHIhjZju0bMtEuaPQWcN4wEHBnlzQ0yYT85nnr7/e6bAtW7Aj5dv9ZzOmuoS9xMA= 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=MWKkzPWQ; arc=none smtp.client-ip=91.218.175.189 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="MWKkzPWQ" Message-ID: <3048ea54-1691-4098-90ff-ffb78dbc5cea@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1773239325; 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=rMglndSp+PIHHlEAetwjxySW2nxoKP5bwJuh4e/DEyA=; b=MWKkzPWQs48gIU3QM3JULAIUosOcv27xYnp/KCpC7CHItK86o/YW0GWfjF9GPrMPeebwVl dWO+ojK2bM3nHepkXamJm+CykbpdtS5wrsGclhkuA8G/mvYack1QHqHQOjlWoA76B/JOIb OycSVKTH8VjV0N+gDzUyGn2rmPJ9a2U= Date: Wed, 11 Mar 2026 17:28:41 +0300 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH] mm: migrate: transfer large_rmappable flag in folio_migrate_flags() Content-Language: en-GB X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Usama Arif To: Zi Yan , "David Hildenbrand (Arm)" Cc: Andrew Morton , npache@redhat.com, willy@infradead.org, linux-mm@kvack.org, matthew.brost@intel.com, joshua.hahnjy@gmail.com, hannes@cmpxchg.org, rakie.kim@sk.com, byungchul@sk.com, gourry@gourry.net, ying.huang@linux.alibaba.com, apopple@nvidia.com, linux-kernel@vger.kernel.org, kernel-team@meta.com References: <20260311132342.3193160-1-usama.arif@linux.dev> <46edc7f1-f97e-4ff3-bf11-a56feb8e33b4@kernel.org> <1DA53E52-795E-4057-81F5-E39B85D7CF05@nvidia.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 11/03/2026 17:24, Usama Arif wrote: > > > On 11/03/2026 16:38, Zi Yan wrote: >> On 11 Mar 2026, at 9:33, David Hildenbrand (Arm) wrote: >> >>> On 3/11/26 14:23, Usama Arif wrote: >>>> folio_migrate_flags() transfers folio state from source to destination >>>> during migration, but does not transfer the large_rmappable flag. >>>> >>>> Migration allocators like alloc_migration_target() and >>>> alloc_misplaced_dst_folio() use __folio_alloc() directly without >>>> wrapping the result in page_rmappable_folio(), so the destination folio >>>> never gets large_rmappable set. >>>> >>>> This becomes a problem when a folio on the deferred split queue is >>>> migrated: the destination folio can be added to the deferred split queue >>>> via deferred_split_folio() (which does not check large_rmappable), but >>>> when the folio is later freed, folio_unqueue_deferred_split() bails out >>>> early because large_rmappable is not set: >>>> >>>> if (folio_order(folio) <= 1 || !folio_test_large_rmappable(folio)) >>>> return false; >>>> >>>> This leaves a stale entry on the deferred split queue, leading to >>>> use-after-free when the shrinker walks the list. >>>> >>>> Fix this by transferring large_rmappable in folio_migrate_flags(), >>>> consistent with how all other folio flags are handled. >>>> >>>> Fixes: dafff3f4c850 ("mm: split underused THPs") >>>> Signed-off-by: Usama Arif >>>> --- >>>> mm/migrate.c | 3 +++ >>>> 1 file changed, 3 insertions(+) >>>> >>>> diff --git a/mm/migrate.c b/mm/migrate.c >>>> index 3380021fd3db..ee1c7bc851dd 100644 >>>> --- a/mm/migrate.c >>>> +++ b/mm/migrate.c >>>> @@ -846,6 +846,9 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio) >>>> folio_copy_owner(newfolio, folio); >>>> pgalloc_tag_swap(newfolio, folio); >>>> >>>> + if (folio_test_large_rmappable(folio)) >>>> + folio_set_large_rmappable(newfolio); >>>> + >>>> mem_cgroup_migrate(folio, newfolio); >>>> } >>>> EXPORT_SYMBOL(folio_migrate_flags); >>> >>> compaction_alloc_noprof() does the page_rmappable_folio() at the end. >>> >>> I'd assume that all migration allocation functions must take care of that. >>> >>> It's a responsibility of the folio allocation code, not folio migration >>> code. >> >> I agree. The migration allocation function needs to give a folio or other >> types of page matching the original one. Do we want to turn this into >> a WARN to make sure newfolio matches folio's large_rmappable state? >> > > hmm its done in compaction_alloc() and alloc_migration_target_by_mpol() but I > dont see this being done in alloc_migration_target() and alloc_misplaced_dst_folio(). > I think alloc_misplaced_dst_folio() is used by NUMA balancing and alloc_migration_target() > by hotplug and memory failure? > > I am not very familiar with migration code, so maybe I am missing where its being done > in these paths? ah no ignore me :) __folio_alloc_noprof should set it. Thanks and sorry for the noise!