From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 84D7C2D9EE4; Thu, 30 Jul 2026 14:18:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785421128; cv=none; b=dM/dOCJCc6YvKiKRhTXyjRxxApWzUoTDQMMq/ws9i0j9BXIH13LXBbR7WtWQkbagz48Z4OVgIA1Q5Bkwa9HNgMCUOGg308m2TPP5cDLf69zVdFacRt6s1VJsBmvTM26U0m7j7bzrKmhAncQ8wS/IqCgrtBJ0iPgJ2Wu3nnpJCz4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785421128; c=relaxed/simple; bh=SlNoTaTO2F2kNzueTPeP0qHJlKf72zK7TB6XRZpbhDI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Us6M5sYUcIkQv7iAFaTJX5ehaBXbTPBjHMZ7XYVtIdngOGBq9F6mNojg4BBWuIedEm3opaJWmZ2cxFzPJV9VPm3KAIS91ya4FlcW//JAOw6UzAyUlJss+FBe0bzRptOteYjoENdBMcBn2Hwk9ZOIY1fds0yDwAuQmfmx/EH6EQg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gYYCtwAj; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gYYCtwAj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 04AC21F000E9; Thu, 30 Jul 2026 14:18:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1785421127; bh=swoCWGdlaECteypHrMqmfRUyWFHKvTb0U1amc3WON8s=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=gYYCtwAjaY13b3bjz7310oW6xyE1WYdcYTfN42+1zsrWTBgFqquOV4/hYdWz0mQYH gvvvBF6ds6F6MNDfEblEHibOx6T1G9vedrXxdFznRBpl4VZh9bUtHQc9fIuemNEJM4 eUoj3bsfe3NU8fT/zXsBT7gABj4/WGHF+KqXyWCr2rFKr0TH88xSlNa0EWQPkjfoVz pLHwNdUHaSbQsCblpILUavZeVpLiNmsFqmCUpWOgjfedQ4RxpVQ0x3hBMSZZtx0Nh7 gYL1fxBd0cYPCc7Mlzl8Rew1GsvDF+nnptugHnm6y9kup55cBZseSEZnyP5J+usp6n WLboD8Yzar8Zw== Message-ID: <7c19c4f3-2d89-48ff-bdf1-5bad418e719b@kernel.org> Date: Thu, 30 Jul 2026 16:18:38 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH mm-hotfixes v2 1/2] mm/huge_memory: fix huge_zero_pfn race To: "Lorenzo Stoakes (ARM)" , Andrew Morton , Zi Yan , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Pankaj Raghav , Hannes Reinecke , Hugh Dickins , Yang Shi , Kiryl Shutsemau Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hengbin Zhang , stable@vger.kernel.org References: <20260730-fix-refcounted-huge-zero-v2-0-c5d8a41b317f@kernel.org> <20260730-fix-refcounted-huge-zero-v2-1-c5d8a41b317f@kernel.org> From: "David Hildenbrand (Arm)" Content-Language: en-US Autocrypt: addr=david@kernel.org; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgc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dZ5eEkj1dCyx+t/ga6x2rHyc8Sl86oK1tvAkwBNsfKou3v+jP/l14a7DGBvrmlYjO 59o3t6inu6H7pt7OL6u6BQj7DoMAEQEAAcLBfAQYAQgAJgIbDBYhBBvZyq1zXEw6Rg38yk3e EPcA/4NaBQJonNqrBQkmWAihAAoJEE3eEPcA/4NaKtMQALAJ8PzprBEXbXcEXwDKQu+P/vts IfUb1UNMfMV76BicGa5NCZnJNQASDP/+bFg6O3gx5NbhHHPeaWz/VxlOmYHokHodOvtL0WCC 8A5PEP8tOk6029Z+J+xUcMrJClNVFpzVvOpb1lCbhjwAV465Hy+NUSbbUiRxdzNQtLtgZzOV Zw7jxUCs4UUZLQTCuBpFgb15bBxYZ/BL9MbzxPxvfUQIPbnzQMcqtpUs21CMK2PdfCh5c4gS sDci6D5/ZIBw94UQWmGpM/O1ilGXde2ZzzGYl64glmccD8e87OnEgKnH3FbnJnT4iJchtSvx yJNi1+t0+qDti4m88+/9IuPqCKb6Stl+s2dnLtJNrjXBGJtsQG/sRpqsJz5x1/2nPJSRMsx9 5YfqbdrJSOFXDzZ8/r82HgQEtUvlSXNaXCa95ez0UkOG7+bDm2b3s0XahBQeLVCH0mw3RAQg r7xDAYKIrAwfHHmMTnBQDPJwVqxJjVNr7yBic4yfzVWGCGNE4DnOW0vcIeoyhy9vnIa3w1uZ 3iyY2Nsd7JxfKu1PRhCGwXzRw5TlfEsoRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: <20260730-fix-refcounted-huge-zero-v2-1-c5d8a41b317f@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 7/30/26 12:55, Lorenzo Stoakes (ARM) wrote: > If !CONFIG_PERSISTENT_HUGE_ZERO_FOLIO, the huge_zero_folio is refcounted by > huge_zero_refcount and returned by mm_get_huge_zero_folio(). > > When the caller is done with the huge zero page, its reference count is > decremented. Only a shrinker can set the reference count to zero. > > A race can unfortunately occur between a shrinker decrementing the > reference count to zero and a concurrent page fault. > > This is because shrink_huge_zero_folio_scan() might, if very > unlucky, be preempted between setting huge_zero_refcount to zero > and writing an invalid value. > > During this time get_huge_zero_folio() could write to huge_zero_pfn > before shrink_huge_zero_folio_scan() resumes. > > In this event the huge zero folio will be persistently > misidentified causing the THP code path to be entered > inappropriately for the huge zero folio: > > CPU 0 CPU 1 > =======================================|================================= > shrink_huge_zero_folio_scan() | > atomic_cmpxchg() sets refcount to 0 | > xchg() sets huge_zero_folio to NULL | get_huge_zero_folio() > | | atomic_inc_not_zero() -> zero > preempted for a long time | Allocate new huge zero folio > | | Write valid huge_zero_folio > v | Write valid huge_zero_pfn > Overwrite huge_zero_pfn with ~0UL <--- Invalid overwrite! > > This results in is_huge_zero_pfn() and is_huge_zero_pmd() incorrectly > returning false for a huge zero page which could result in issues like the > huge zero folio being incorrectly split. > > Note that the issue is with huge_zero_pfn not huge_zero_folio, as > get_huge_zero_folio() uses cmpxchg() gated on huge_zero_folio being NULL > with a retry loop and shrink_huge_zero_folio_scan() uses xchg() to set > huge_zero_folio. > > Fix the issue by introducing a spinlock, huge_zero_lock, to prevent > concurrent write of huge_zero_folio, huge_zero_pfn and huge_zero_refcount. > > There needs to be significant care taken here to ensure correctness: > > The fast path in get_huge_zero_folio() uses atomic_inc_not_zero(), which is > outside of the critical section, and means huge zero allocation is gated on > zero huge_zero_refcount. > > The fast path doesn't use huge_zero_lock, so the critical section is > irrelevant to it. > > So invariants are required - huge_zero_refcount MUST: > > * Only be set in the huge_zero_lock critical section to ensure > serialisation of huge_zero_pfn, huge_zero_folio and huge_zero_refcount > writes. > > * Be set non-zero only AFTER huge_zero_[pfn, folio] are set to valid values > so installation of the huge zero folio on read page fault ensures > concurrent is_huge_zero_*() calls correctly identify the huge zero folio. > > * Be set zero only BEFORE huge_zero_[pfn, folio] are set to NULL and ~0UL > respectively, and atomically. > > Establish these by: > > * Only setting huge_zero_refcount to zero or an absolute value in the > huge_zero_lock critical section in get_huge_zero_folio() and > shrink_huge_zero_folio_scan(), and always updating atomically there > and elsewhere. > > * Using atomic_set_release(&huge_zero_refcount) in get_huge_zero_folio() > after huge_zero_[pfn, folio] are set. This is paired with > atomic_inc_not_zero() to ensure atomic_inc_not_zero() only observes a > non-zero value if huge_zero_[pfn, folio] are set. > > * Using atomic_cmpxchg() in shrink_huge_zero_folio_scan() (as before) to > ensure that it is set zero only when equal to 1 and set atomically. > > * atomic_cmpxchg() being fully ordered ensures this is done prior to > huge_zero_[folio, pfn] being set to NULL and ~0UL respectively. > > Eliminate the retry loop in get_huge_zero_folio() as the atomic_cmpxchg() > in shrink_huge_zero_folio_scan() is now performed under the lock, and > replace with an equally locked atomic_inc() to set the reference count > should the caller be raced on huge zero folio installation. > > folio_put() naturally implies a full memory barrier so its ordering is > maintained correctly. > > The huge zero folio also cannot be released except when the shrinker does > so as it is non-LRU and non-rmappable. > > Note that only the huge zero shrinker (via shrink_huge_zero_folio_scan()) > can actually set huge_zero_refcount to zero, which is the count of mm's > which have at least one huge zero folio installed plus one shrinker pin. > > Additionally convert a BUG_ON() to a VM_WARN_ON_ONCE(). > > Suggested-by: David Hildenbrand (Arm) > Reported-by: Hengbin Zhang > Closes: https://lore.kernel.org/linux-mm/20260727154001.4102341-1-uqbarz@gmail.com/ > Fixes: 3b77e8c8cde5 ("mm/thp: make is_huge_zero_pmd() safe and quicker") > Cc: stable@vger.kernel.org > Signed-off-by: Lorenzo Stoakes (ARM) > --- Thanks! Acked-by: David Hildenbrand (Arm) -- Cheers, David