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 2A62A389DF3 for ; Fri, 12 Jun 2026 14:24:58 +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=1781274300; cv=none; b=BQ0A+h7HAngDJ8TLbDvRcXY+XamS/7z268+4QtEEl2VMficbB3JAZjw7cOymaG6liVS8+jlAoI6JYEovvehCrV6KxpPCPYt0oWXpA7MLOTtxiWHovtPN7Yjz0YRNxwkHLZlBiRQeMMMQnlr1nE1m2qFtUvltNgZNm9Oth23kltI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781274300; c=relaxed/simple; bh=7peIzfw+Qs6Ilb7hG7X25UmTubmMQcxBTuWkwg5GCcU=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=cqxpxrFBEsxG0O4AhcMIZXfiEhZvOwD1YZqrgbb/PLjXJepA4PiUCklAkXjh0YeTDBPaE2haTfYVJ7U+cWPeWEJtZcQlAleQLLBCCnmZlp4nJ/j/IU0sTzW7e98OFF6u1pPhgEjx8wpRn3o1hJRvZynDhdlhyp3XvULoyjORS54= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BmXV9GHE; 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="BmXV9GHE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DD2B71F000E9; Fri, 12 Jun 2026 14:24:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1781274298; bh=x6Ya9QYj0XFq7gyk+7qpbpXHofjdcXC20XPazYbg+ns=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=BmXV9GHEYRrExHCEy4rRQ9NVeIIfANNm8fdnohNfi3LqhcEsqiCgBPCpAHyW83C0X JbLp6+tiWoS86I2ljesOhQIKPjMe6YoOY5uzPsym0dQKF/oDu3qK0Wc9mhURPN1F6P yh5s54niXCzItUe+nqBrj3ZqRtJtflQmClW5A/83s/G1LzqOPloSrEXa76rZ/AKp0p gv+ZJdXMakjEB86tXICzGIBZFWl+1jt3SEwFQZVt0u8thwBN3raQ0Gs9JyMAcRiPtE ZQK8DDczVgSpxWAfRYUj9KJIMpOKxFGeytO7lu6yq6Njm1X9drZ7TDlOP2b70Qqz4b GQLTptAxLJ/IQ== Message-ID: <9ee74fad-9426-4bab-a64c-024a3c9959b0@kernel.org> Date: Fri, 12 Jun 2026 16:24:55 +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/page_alloc: drop flag-conversion "optimisation" Content-Language: en-US To: Brendan Jackman , Andrew Morton , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Zi Yan Cc: "Harry Yoo (Oracle)" , Gregory Price , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20260612-gfp-pessimisation-v1-1-936eb04202e7@google.com> From: "Vlastimil Babka (SUSE)" Autocrypt: addr=vbabka@kernel.org; keydata= xsFNBFZdmxYBEADsw/SiUSjB0dM+vSh95UkgcHjzEVBlby/Fg+g42O7LAEkCYXi/vvq31JTB KxRWDHX0R2tgpFDXHnzZcQywawu8eSq0LxzxFNYMvtB7sV1pxYwej2qx9B75qW2plBs+7+YB 87tMFA+u+L4Z5xAzIimfLD5EKC56kJ1CsXlM8S/LHcmdD9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qaqKxzqoQZCO8AOz/58j6O 2RHNy/mZEN+7tJ5Tsq42zVJ4jxsT8b9YplavCMsnBgDeRWhcbYhCyttoL7nYISyWg4kQYZ/P wIV3OuNv2f8iKYsxNsRuClOAF82+gvqOy1/1pprFjy8uo2pkoOrb63aOP3vO5VHnRKgra6dq NcaZ+c6J4H+nEJGi2SkHAUJz5oBzuThvPudLvPA/SK8sKoM01IRxSihev/S/5WLazXB1PGem OCbvzC1IjWJJraxiDJ5IygokapUa2RP7+WBR22skQ3SSl6G107QgWKSyTOGWEaRmV53vxQLV jXuCmzSSasTL60zq5yGrT4/DYQVSNEUiUbG4pYekxJujNeEDkUlky0Y= In-Reply-To: <20260612-gfp-pessimisation-v1-1-936eb04202e7@google.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 6/12/26 16:15, Brendan Jackman wrote: > This code uses flag equivalences to try to optimise conversion from > GFP_ to ALLOC_ but there's no clear reason to believe it makes things > faster. Even if it gets rid of conditional branches, it just trades them > for a data dependency. > > CPUs are pretty good at conditional branches. But, in my GCC x86 build > it doesn't look like there are any branches anyway, the compiler found > some conditional instruction tricks. (Caveat: This was extracted & > annotated by Gemini AI, I did not actually read the disasm myself) > > Old code: > > ae50: 8b 04 24 mov (%rsp),%eax # Load gfp_mask > ... > ae5d: 41 89 c4 mov %eax,%r12d > ae64: 41 81 e4 20 08 00 00 and $0x820,%r12d # Mask both flags at once > ... > ae6f: 44 89 e1 mov %r12d,%ecx > ae77: 83 c9 40 or $0x40,%ecx # OR with ALLOC_CPUSET (0x40) > ae7a: 89 4c 24 60 mov %ecx,0x60(%rsp) # Store to alloc_flags > > New code: > > For __GFP_HIGH ( 0x20 ): > It uses the Carry Flag (via sbb ) to conditionally add 0x20 to the base 0x40 ( ALLOC_CPUSET ) flag: > > ae63: 83 e0 20 and $0x20,%eax # Test __GFP_HIGH > ... > ae6a: 83 f8 01 cmp $0x1,%eax # Set carry flag if 0 > ae6f: 45 19 e4 sbb %r12d,%r12d # %r12d = (gfp & 0x20) ? 0 : -1 > ae80: 41 83 e4 e0 and $0xffffffe0,%r12d # %r12d = (gfp & 0x20) ? 0 : -32 > ae87: 41 83 c4 60 add $0x60,%r12d # %r12d = (gfp & 0x20) ? 0x60 : 0x40 > > For __GFP_KSWAPD_RECLAIM ( 0x800 ): > It uses a conditional move ( cmov ) later in the function to set the ALLOC_KSWAPD ( 0x800 ) bit: > > ae72: 25 00 08 00 00 and $0x800,%eax # Test __GFP_KSWAPD_RECLAIM > ae77: 89 44 24 30 mov %eax,0x30(%rsp) # Store result > ... > af2c: 80 cf 08 or $0x8,%bh # Set ALLOC_KSWAPD (0x800) in temp reg > af2f: 45 85 c9 test %r9d,%r9d # Check if __GFP_KSWAPD_RECLAIM was set > af32: 0f 44 d8 cmove %eax,%ebx # If not, revert to flags without it > > Testing with a modified version[0] of lib/free_pages_test.c (adding > printks with timing)... > > [0] https://github.com/bjackman/aethelred/blob/2ccdc84ef087c2a631914f58e106e99e19bd3b98/page-alloc-test/page-alloc-test.c > > Old results from a Sapphire Rapids consumer CPU: > > [ 67.157118] page_alloc_test: Testing with GFP_KERNEL > [ 67.157122] page_alloc_test: Starting 1,000,000 allocations... > [ 70.704446] page_alloc_test: Completed. Time: 3543002 us (Avg: 3543.00 ns per alloc+free loop) > [ 70.704456] page_alloc_test: Testing with GFP_KERNEL | __GFP_COMP > [ 70.704460] page_alloc_test: Starting 1,000,000 allocations... > [ 70.944672] page_alloc_test: Completed. Time: 239980 us (Avg: 239.98 ns per alloc+free loop) > [ 70.944675] page_alloc_test: Test completed > > New results: > > [ 70.079015] page_alloc_test: Testing with GFP_KERNEL > [ 70.079020] page_alloc_test: Starting 1,000,000 allocations... > [ 73.669396] page_alloc_test: Completed. Time: 3586954 us (Avg: 3586.95 ns per alloc+free loop) > [ 73.669402] page_alloc_test: Testing with GFP_KERNEL | __GFP_COMP > [ 73.669405] page_alloc_test: Starting 1,000,000 allocations... > [ 73.905084] page_alloc_test: Completed. Time: 235496 us (Avg: 235.49 ns per alloc+free loop) > [ 73.905086] page_alloc_test: Test completed > > Seems like a wash. > > So, drop the flag value coupling here and let the compiler and CPU do > their job. Superscalar CPUs are pretty neat after all. True > (Used AI for the disasm but the rest is all manual). > > Signed-off-by: Brendan Jackman Reviewed-by: Vlastimil Babka (SUSE) Thanks. > --- > mm/page_alloc.c | 14 ++++---------- > 1 file changed, 4 insertions(+), 10 deletions(-) > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index ee902a468c2f5..9e1949ea13a6d 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -4478,22 +4478,16 @@ gfp_to_alloc_flags(gfp_t gfp_mask, unsigned int order) > { > unsigned int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET; > > - /* > - * __GFP_HIGH is assumed to be the same as ALLOC_MIN_RESERVE > - * and __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD > - * to save two branches. > - */ > - BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_MIN_RESERVE); > - BUILD_BUG_ON(__GFP_KSWAPD_RECLAIM != (__force gfp_t) ALLOC_KSWAPD); > - > /* > * The caller may dip into page reserves a bit more if the caller > * cannot run direct reclaim, or if the caller has realtime scheduling > * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will > * set both ALLOC_NON_BLOCK and ALLOC_MIN_RESERVE(__GFP_HIGH). > */ > - alloc_flags |= (__force int) > - (gfp_mask & (__GFP_HIGH | __GFP_KSWAPD_RECLAIM)); > + if (gfp_mask & __GFP_HIGH) > + alloc_flags |= ALLOC_MIN_RESERVE; > + if (gfp_mask & __GFP_KSWAPD_RECLAIM) > + alloc_flags |= ALLOC_KSWAPD; > > if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) { > /* > > --- > base-commit: ca2351ac6da277a470d4fcf122b53267e02b2716 > change-id: 20260612-gfp-pessimisation-b258a4bb5ebd > > Best regards,