From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793724513; b=HYtf93G24F1zHH8LZPof9h3dJFJ7zBJX+ChUPdj1MoWcxEWSPo0DAguXRQnUy6SdMQQF jFhnRcw21HgGX9i+HjcqI7rBl7Uki4fCQBTqf4siEpL7AuDvF72NAjmKGD9Sg0/6fpuzE B/iqxEdDjAkNeIjKbDLkEwCoQdZt5+Otduu1p5PdlDdupfsEzVBiscyTsjVCv9TZUBer6 BvkHFhzduqMMWXT+s8WnVIao1uQ1hzypuWJ5itmfHDgXy8aV875DF9iRG8c59EAIqs5s9 tEaN7CLcOAR/32lPgTbMuachOs/7Y70dfE/seHpoaSGthKWu8Ku5DeEiS8ZyTa1SD9g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793724513; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:Content-Language:Autocrypt:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=fzxroPSjg7djG2/E7Jnxw7+FksX3byDhaT1k3G3X82E=; b=ih7bJOotBLUsvirUIj/CrKK/M0gfD85S1FnHcyZXswzAHIcmx8L6ac7u41yWRIHAoTTb RfZaWcnga+/LjHojdzxvlBqUu3XFxlUTyw4LTvxeIr8VWs5G11ZF/epvMFG/mjOlJrDLZ TYrfgp8d4h6Pk5Lu5epOCEvz7vNS3nrS0nxJVkAqLzGH90HHUgxxYmMnmjKv6FVXo6RYx v0T14Tj6dsFHhxqamfgLlkNVKFcVPbAfumSi38oPQXkO0Jq0NtPX//GNjIX3JCWDi+wvO pfkdev/VvcYlGQeIrT9XI5cmBLVcuG3v0Zs1OH9Jt0h47ahE3xpWKhI5yldvJEPLxEA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kKjb+uZv; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B57A9606A328 for ; Tue, 3 Nov 2026 16:48:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kKjb+uZv Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5847A1F00919; Tue, 3 Nov 2026 16:48:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793724512; bh=fzxroPSjg7djG2/E7Jnxw7+FksX3byDhaT1k3G3X82E=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=kKjb+uZvI/3jfX+ELwR4splMxe7pwzFhYBfrEt9iyYiE1DL9agVZLgu3AJaB0Y0IB cApJ7w8OpkUG+ML/D8IwVmrjfyjr86K+f/UdCFMk8NKuQLpd+e8GPXslyhq/B/x+dl pj9IrnJGZQU1YkYGOwdnOwzZVlVulhz+D1rowr93yTki3O8fxTsbcjYqdVujZ2t14B dsgoqZNxFV1Ss/jx4YEbSLc2rtu03+rnhS4BrlRc1TmE9E24lj/+ALEd6WUpmcMCib MhO5IXpTxw9Z75N/06GFpCORtyDW5cF76LRcFDz193BlrU98AKGkx4OiQoSRoahLgO gpAK6EE+MzGKQ== Message-ID: <85bbc54e-4c77-4385-9119-590f6c97aa2e@kernel.org> Date: Tue, 3 Nov 2026 17:48:24 +0100 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] mm/page_alloc: more checks for alloc_contig_frozen_range() with __GFP_COMP To: Zi Yan , Andrew Morton , Vlastimil Babka , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Lorenzo Stoakes , "Liam R. Howlett" , Mike Rapoport , Yu Zhao , Zhenghui Hao , Brendan Jackman Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sashiko References: <20261102-free-pfn-on-alloc-contig-range-error-path-v2-1-a9fe939a85c3@nvidia.com> From: "David Hildenbrand (Arm)" Content-Language: en-US Autocrypt: addr=david@kernel.org; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzS5EYXZpZCBIaWxk ZW5icmFuZCAoQ3VycmVudCkgPGRh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nIa3w1uZ 3iyY2Nsd7JxfKu1PRhCGwXzRw5TlfEsoRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: <20261102-free-pfn-on-alloc-contig-range-error-path-v2-1-a9fe939a85c3@nvidia.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/2/26 17:24, Zi Yan wrote: > When alloc_contig_frozen_range() is called with __GFP_COMP, certain inputs > can cause a mismatch between a requested range and its allocated range, > leading to the WARN at the end of the function and page leaks. No in-tree > user passes such inputs. Add checks to prevent it: > > 1. the order is at least MAX_PAGE_ORDER: free pages during isolation can > merge up to MAX_PAGE_ORDER, extending the allocated range beyond the > requested range; > 2. the number of pages in the range is a power of 2: ilog2() can round > down, so that the compound page will not cover the requested range; > 3. start and end are naturally aligned to nr_pages: a compound page must be > naturally aligned, and the unaligned range can also lead to a mismatched > allocated range. > > Because of check 2, the same power of 2 at the end is redundant and > removed. > > Add a comment on the WARN path to clarify it should not be reachable, so no > one in the future will try to fix the impossible page leak. > > Reported-by: Sashiko > Closes: https://sashiko.dev/#/patchset/20260628-keep-subpage-private-zero-at-free-v1-0-f4ce3930d10f%40nvidia.com?part=1 > Signed-off-by: Zi Yan > --- > Sashiko reports that if alloc_contig_frozen_range() with __GFP_COMP can > allocate a free page range different from the requested range, the function > triggers a WARN and returns EINVAL without freeing the allocated PFNs and > causes free memory leaks. Add checks to prevent it. > > The is_power_of_2() check and start PFN alignment check are added with the > help of LLM. > --- > Changes in v2: > 1. added 3 checks for __GFP_COMP to prevent the allocated range from > mismatching the requested range, which can trigger the warning. > 2. removed the redundant power of 2 check at the end of the function. > 3. added a comment on the WARN path to clarify it becomes unreachable. > 4. dropped Fixes tag. > - Link to v1: https://lore.kernel.org/r/20260629-free-pfn-on-alloc-contig-range-error-path-v1-1-496ff9ca22db@nvidia.com > --- > mm/page_alloc.c | 25 +++++++++++++++++++------ > 1 file changed, 19 insertions(+), 6 deletions(-) > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index 15ff5c1feef4f..003911ef393be 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -7326,12 +7326,24 @@ int alloc_contig_frozen_range_noprof(unsigned long start, unsigned long end, > PB_ISOLATE_MODE_OTHER; > > /* > - * In contrast to the buddy, we allow for orders here that exceed > - * MAX_PAGE_ORDER, so we must manually make sure that we are not > - * exceeding the maximum folio order. > + * The use of __GFP_COMP has the following requirements: > + * 1. the order cannot exceed MAX_FOLIO_ORDER (>MAX_PAGE_ORDER is OK); > + * 2. the order is at least MAX_PAGE_ORDER to ensure at the end > + * outer_start and outer_end match start and end respectively; > + * 3. the number of pages in the range is a power of 2; > + * 4. start and end are naturally aligned to nr_pages. > + * > + * 3 and 4 are also the requirements for __GFP_COMP pages in general. > */ > - if (WARN_ON_ONCE((gfp_mask & __GFP_COMP) && order > MAX_FOLIO_ORDER)) > - return -EINVAL; > + if (gfp_mask & __GFP_COMP) { > + const unsigned long nr_pages = end - start; > + > + if (WARN_ON_ONCE(order > MAX_FOLIO_ORDER || > + order < MAX_PAGE_ORDER || > + !is_power_of_2(nr_pages) || > + !IS_ALIGNED(start, nr_pages))) > + return -EINVAL; > + } > > gfp_mask = current_gfp_context(gfp_mask); > if (__alloc_contig_verify_gfp_mask(gfp_mask, (gfp_t *)&cc.gfp_mask)) > @@ -7428,12 +7440,13 @@ int alloc_contig_frozen_range_noprof(unsigned long start, unsigned long end, > __free_contig_frozen_range(outer_start, start - outer_start); > if (end != outer_end) > __free_contig_frozen_range(end, outer_end - end); > - } else if (start == outer_start && end == outer_end && is_power_of_2(end - start)) { > + } else if (start == outer_start && end == outer_end) { > struct page *head = pfn_to_page(start); > > check_new_pages(head, order); > prep_new_page(head, order, gfp_mask, ALLOC_DEFAULT); > } else { > + /* Unreachable with the __GFP_COMP checks above */ > ret = -EINVAL; > WARN(true, "PFN range: requested [%lu, %lu), allocated [%lu, %lu)\n", > start, end, outer_start, outer_end); > > --- > base-commit: afb4eea28037fa55c4560313ecbd8a1e1da8369a > change-id: 20260629-free-pfn-on-alloc-contig-range-error-path-acc001232468 > > Best regards, > -- > Yan, Zi > Thanks! Reviewed-by: David Hildenbrand (Arm) -- Cheers, David