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 B53A636DA0D; Mon, 12 Oct 2026 14:03:24 +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=1791813805; cv=none; b=GO0+lD6JnqKt0I/4VHqHjI+E/FiiNSCMvwwnMbj9Tsh2+rDLmnICdZg+qXr5QTBvXDh10EbnvFGUPxioGe8/VeN485rYZPRTmgQVvE3UGvHRIXyS7PZPXCec9HYYlx/FL4DSGFU3ljAZ9irLT31BWvlG0+I/jS3epjgppMMF6LY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791813805; c=relaxed/simple; bh=XurgxqthUZN1ASrF664LEBNKW0TlH+VNdMopqbFG3m8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=VE6EEpAlr9rgxj0Y4bummWuqPwMR8mYGnq5TvHgtupj1c2zdCEO07aDUqQL4jJ/Ioc5YpMfJHIO+Zl5LBTsD9Z4kI90nX7NBQ1Jer0opaYkV/B3myp/MTV6FvH/jio1Sedo75s82oB2U6l75APvv/rw9WlfDP0zYuTsquBJZUeM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gw/Cr0tm; 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="gw/Cr0tm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0A4BC1F0089B; Mon, 12 Oct 2026 14:03:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1791813804; bh=ae6+9fsHBuTqfSzmzTNh6Q0vtGeTZM5nGNA/BO85wZw=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=gw/Cr0tmM7dts1lWLtfjm/zCdTtZJ74AtM8vagxoe9q7mR1K3KfpA49DxZxe05xR3 c352MZQ2TN/rShWV46Mgn3fC3f9eLoUlryf4CvXuEYDYKcO1vBp76FcZEBeRir0NAc 8XsgYJrzlod1y/OZxikQeqK3ikSMb4qsQifQX9SXq3M0EK5fhrKL2VVQFqZCse4mFB LsM8e2e9EYjvAaiia4Zfx9edDBPyAr3yxapc2yzhiZVFPyxBvcYD1ltkjK3t6TD+x0 rP2okYLeI/e2SJPI8B+OaXJrrgmzEw4PofuLnvClkH04b6Gn+eLZOOvBKg2PiGHDXF DDBzI09shhopQ== Message-ID: <11e317b4-8e28-4234-843e-96ad80fd6d96@kernel.org> Date: Mon, 12 Oct 2026 16:03:20 +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 v3] mm/memfd: fix hugetlb reservation accounting in error paths To: Andrew Morton , Hongfu Li Cc: hughd@google.com, baolin.wang@linux.alibaba.com, vivek.kasireddy@intel.com, muchun.song@linux.dev, osalvador@suse.de, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hongfu Li , stable@vger.kernel.org References: <20260927094757.31665-1-hongfu.li@linux.dev> <20261007153526.cb4473601fc9038a9ca4f517@linux-foundation.org> From: "David Hildenbrand (Arm)" Content-Language: en-US Autocrypt: addr=david@kernel.org; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL Q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QgAJgIbDBYhBBvZyq1zXEw6Rg38yk3e 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: <20261007153526.cb4473601fc9038a9ca4f517@linux-foundation.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 10/8/26 00:35, Andrew Morton wrote: > On Sun, 27 Sep 2026 17:47:57 +0800 Hongfu Li wrote: > >> From: Hongfu Li >> >> If hugetlb_add_to_page_cache() in memfd_alloc_folio() fails with -EEXIST, >> a concurrent fault has already instantiated the folio in the page cache, >> and the reservation now belongs to that folio. Calling >> hugetlb_unreserve_pages() in that case incorrectly removes the region >> backing the cached folio and releases one reservation more than it >> should, leaving that folio in the page cache with no region recording it. >> >> That leaves resv_huge_pages one page short until that folio is removed >> from the page cache, so available_huge_pages() reports a page that is >> not actually free and the pool can grant one reservation more than it >> can back. Applications using hugetlb memfds can then fail to allocate >> or fault in a page they already reserved. They see -ENOMEM or -ENOSPC >> from the allocation or fault path even though the reservation and >> HugePages_Free still look healthy. >> >> So hold the hugetlb fault mutex from hugetlb_reserve_pages() until the >> error-path unreserve completes to make the reserve, allocate and >> instantiate steps atomic against concurrent faults. With the mutex held >> from the start, a concurrent fault can no longer consume the reservation >> between reserve and allocate/instantiate. If a fault completed before the >> mutex was taken, it has already added the region for that index, so >> hugetlb_reserve_pages() returns 0 and the error path leaves the region in >> place. >> >> Fixes: 717cf9357325 ("mm/memfd: reserve hugetlb folios before allocation") >> Cc: stable@vger.kernel.org >> Signed-off-by: Hongfu Li > > Can we please have review of this cc:stable regression fix? Hugetlb AND reservation? what could possibly be nicer to review ;) > >> v3: >> - Update commit message to describe the accounting damage and its >> user-visible effect; no code changes. >> - Add Cc: stable@vger.kernel.org. >> v2: >> - Take the hugetlb fault mutex before hugetlb_reserve_pages() and hold >> it until the error-path unreserve completes. >> - Update commit message >> - Link to v1: https://lore.kernel.org/all/20260831090631.29227-1-hongfu.li@linux.dev/ >> --- >> mm/memfd.c | 31 ++++++++++++++++--------------- >> 1 file changed, 16 insertions(+), 15 deletions(-) >> >> diff --git a/mm/memfd.c b/mm/memfd.c >> index c708d92533f4..0f6fff004f5e 100644 >> --- a/mm/memfd.c >> +++ b/mm/memfd.c >> @@ -82,22 +82,31 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) >> struct hstate *h = hstate_file(memfd); >> int err = -ENOMEM; >> long nr_resv; >> + u32 hash; >> >> gfp_mask = htlb_alloc_mask(h); >> gfp_mask &= ~(__GFP_HIGHMEM | __GFP_MOVABLE); >> idx >>= huge_page_order(h); >> >> + /* >> + * Serialize hugepage allocation and instantiation to prevent >> + * races with concurrent allocations, as required by all other >> + * callers of hugetlb_add_to_page_cache(). >> + */ >> + hash = hugetlb_fault_mutex_hash(memfd->f_mapping, idx); I'd just do const u32 hash = hugetlb_fault_mutex_hash(memfd->f_mapping, idx); above, but doesn't really matter for the fix. >> + mutex_lock(&hugetlb_fault_mutex_table[hash]); >> + >> nr_resv = hugetlb_reserve_pages(inode, idx, idx + 1, NULL, EMPTY_VMA_FLAGS); >> - if (nr_resv < 0) >> - return ERR_PTR(nr_resv); >> + if (nr_resv < 0) { >> + err = nr_resv; >> + goto out_unlock; >> + } >> >> folio = alloc_hugetlb_folio_reserve(h, >> numa_node_id(), >> NULL, >> gfp_mask); >> if (folio) { >> - u32 hash; >> - >> /* >> * Zero the folio to prevent information leaks to userspace. >> * Use folio_zero_user() which is optimized for huge/gigantic >> @@ -112,20 +121,9 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) >> */ >> __folio_mark_uptodate(folio); >> >> - /* >> - * Serialize hugepage allocation and instantiation to prevent >> - * races with concurrent allocations, as required by all other >> - * callers of hugetlb_add_to_page_cache(). >> - */ >> - hash = hugetlb_fault_mutex_hash(memfd->f_mapping, idx); >> - mutex_lock(&hugetlb_fault_mutex_table[hash]); >> - >> err = hugetlb_add_to_page_cache(folio, >> memfd->f_mapping, >> idx); >> - >> - mutex_unlock(&hugetlb_fault_mutex_table[hash]); >> - >> if (err) { >> folio_put(folio); >> goto err_unresv; >> @@ -133,11 +131,14 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) >> >> hugetlb_set_folio_subpool(folio, subpool_inode(inode)); >> folio_unlock(folio); >> + mutex_unlock(&hugetlb_fault_mutex_table[hash]); >> return folio; >> } >> err_unresv: >> if (nr_resv > 0) >> hugetlb_unreserve_pages(inode, idx, idx + 1, 0); >> +out_unlock: >> + mutex_unlock(&hugetlb_fault_mutex_table[hash]); >> return ERR_PTR(err); >> } >> #endif We now run allcoation+zeroing under the mutex. Which is the same as done in hugetlb_no_page(). (I just looked at that function, now I have to wash my eyes) So holding the mutex for longer seems to be the right thing to do. Acked-by: David Hildenbrand (Arm) -- Cheers, David