From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DED5430674B for ; Fri, 12 Jun 2026 16:04:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781280293; cv=none; b=QusEuRogvS8dOesGJaIJL1iTTPU2rSNUBBWdwqBv5Tiz7TPuL9uAyTkA3R6fdoENHUThzBghioSd2neAEhxZolKc44k7setR4sdySki/jDyoYh04NYBivEEcVngiewMaMOTh5VDvEOdAWnby9hsVkfzRjJqGdVi1UWx1bO0Rjvc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781280293; c=relaxed/simple; bh=UnG7+ZWoQag/1/JTU5dIzVAqps0sztrBEk1ixEVHc3s=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=CMXOwIT0mf27ydKGd96nrG78DG4xT1SFMBeAPt3OugLCTmyGjRqIPsAqFqL9kQjAXr2o789NcwX3ByDZoySwjS08DlWFnTQ9Q2Oo3KxJp+4+CWS4vrsXNMFSPBUufIfhMDjvuEB7/K5tNT/LRteH9auGDXW3eCk1jkzau2W+Xks= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gourry.net; spf=pass smtp.mailfrom=gourry.net; dkim=pass (2048-bit key) header.d=gourry.net header.i=@gourry.net header.b=JZRfcHf0; arc=none smtp.client-ip=209.85.167.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gourry.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gourry.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gourry.net header.i=@gourry.net header.b="JZRfcHf0" Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-48650c78e09so447460b6e.2 for ; Fri, 12 Jun 2026 09:04:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gourry.net; s=google; t=1781280291; x=1781885091; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=WhTsWnt1ltZwTIfMb0wntlq3uSFMtI1YmVFNWpYqAW8=; b=JZRfcHf04a1qsfdBz5mi8ZoO1yEce6r85JJAP3071cxJ4+o/q0IZfLBxtXT2LpO2eH zLh68XbltGR6NQpt0LmJ53u9R4XgiMdACIU976QM9vhXuC3fyinqPNPM+JD+7YjOQE5C KQ5c+JHgWJTtMEmXcY/QtA2cDwyjVqqIMmCIbhzk4AJNNFLxY/UDzEmIeZMIrw3T/OR3 dKkz91GzOkozSefiFHnDU+BKmpYmRv/etzdMloemEgg8G9wYzAL3/7qQb/djudNCulYq ZaEwNR5PFt6QGQkdPeAorNSqneIqL2urgghY1ZCHXbGKZLzQbZi79crxnAlEUUA+gHWz XJew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781280291; x=1781885091; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WhTsWnt1ltZwTIfMb0wntlq3uSFMtI1YmVFNWpYqAW8=; b=l+RsE93HoV/Y/G3I5SSGI1Bf1X6RdHy9wly7kHKna5oJAhZvNzwIyRgD5m1analRQf jN9UIakWo5inNlDfLER7fZwSPLLN9+FZ+w0g0O6yoq4iQbVbqhwNOl6dKbyorauXHtdk NvzvfNwE1ldSvlOOL/r7MrUM1pcrcK4+0CxPKZnpFIBlm/iAzILyQq23JZ6NF7HN+fNd dtzdTHZjXr9LrIEaZkBRXB4ShXt75mCEJJgZDq9ENomQzrNCIAVDafP8JEiJ8TYpt99z yBiQvGK55TUrrbSZbQSUm/48bgVj6IZ//8n4BTw/51njzSWMATOKqtZgy76pKa2PP3jr naVw== X-Forwarded-Encrypted: i=1; AFNElJ+BN9y3DPVIh4gcECm5r3Oxpf0QF1MlvP3OyOvIoCJdY6lHCb+GPF7Xc6OAMgix2IsMqUSuOjCzuYZuCGI=@vger.kernel.org X-Gm-Message-State: AOJu0Yzo8BrzDa955z5mHwOJGadXQMp8YJ7AOgzfEs6aQfSecLXQWe8+ 1d8HrhqdLhSfe7IXR2876sSUONRWXbtrUahvMNnZocb1jpkaqsU3t4nVc6TYnoTcogo= X-Gm-Gg: Acq92OG4eF9gJgwIkByh4YgjKQU+k6kbTBOCCEOxbswI4Iq+uuJlXeu4zz081FOiJyG J8/dZnkZJ8SC4k706VaQ0uqu2fqLk/a069HylJwQO3COg4glb/6BibcVtAFfZ8oZGEIaL1QJF0U arinMiAZHY7tJ6sAMUJx/pjam9VeicZ3p6pKB6t21e0paLmuxAZFbd3Rz0CbzR6lXJymeFRO/Ev zUP51mIWWC50S+61z8GsG2TcsaZKMASafftseG5sTmFW4a5wnNd4znb/CKdYv/aTinCcS3YOg13 HWHC/GP2JMg4N6VXP8gYtVeRobPnZFqzIRh9lNZITH1v6bHrSTWYxcFl6D4bTRkTk3lACTx+I3Z M+9WF+wvO3VOd+XMb9RIphyiqYTGAqAjP8sYXO7mte5IojzkoUTIb3HVnrtKlkELrg4WjupMUJ7 ImW0vlJD65Z1gMDvVlSBG2eDqIMrlEis/4kNXhBC3jFUpWHtNa38s6lgbP19iX8jibw6h+ndyuM zxZ5M0= X-Received: by 2002:a05:6808:c192:b0:47b:c8d0:512e with SMTP id 5614622812f47-48741b24246mr195066b6e.34.1781280290644; Fri, 12 Jun 2026 09:04:50 -0700 (PDT) Received: from gourry-fedora-PF4VCD3F (pool-173-79-60-52.washdc.fios.verizon.net. [173.79.60.52]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-517fb61d948sm24363771cf.1.2026.06.12.09.04.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jun 2026 09:04:49 -0700 (PDT) Date: Fri, 12 Jun 2026 12:04:48 -0400 From: Gregory Price To: Brendan Jackman Cc: Andrew Morton , Vlastimil Babka , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Zi Yan , "Harry Yoo (Oracle)" , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/page_alloc: drop flag-conversion "optimisation" Message-ID: References: <20260612-gfp-pessimisation-v1-1-936eb04202e7@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260612-gfp-pessimisation-v1-1-936eb04202e7@google.com> On Fri, Jun 12, 2026 at 02:15:44PM +0000, 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. > > (Used AI for the disasm but the rest is all manual). > > Signed-off-by: Brendan Jackman Love me some readability improvements and code reduction :] Reviewed-by: Gregory Price > --- > 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, > -- > Brendan Jackman >