From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E5251C982DE for ; Mon, 21 Sep 2026 09:35:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA56C6B00E9; Mon, 21 Sep 2026 05:35:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A7C3B6B00EB; Mon, 21 Sep 2026 05:35:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96D226B00EA; Mon, 21 Sep 2026 05:35:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 751766B00BF for ; Mon, 21 Sep 2026 05:35:53 -0400 (EDT) Received: from smtpin19.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1B0FE405EF for ; Mon, 21 Sep 2026 09:35:52 +0000 (UTC) X-FDA: 85237262544.19.6F67F42 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf03.hostedemail.com (Postfix) with ESMTP id 3171C20009 for ; Mon, 21 Sep 2026 09:35:50 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="mW/ciwfo"; spf=pass (imf03.hostedemail.com: domain of vbabka@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=vbabka@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1789983350; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vB6u7hTafA7/3xCUaJmo0nI+NPov+j5mxQHYBu1u2js=; b=oDRuwSISk0rTyXgmcwVuRR5eigV6kdNW5NWkG3TDxxrtssjqDNpq9X2jb+gNBSRi/vnEwM 6UF1e8HBMGxLdZO6fmn2tqO7OilUAQIwxye7MG+Srfg84gE0RLsOsjtuuzu4JPiVirmDmw lMzWhi6thUe/FJciltVPwvsF/JI3KsU= ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1789983350; b=fQhvmr3ET+e5dDRxoppSg24m7KAnasN9x0w1Hvv6mFHhk2wCTQf5ydfMevcPhlmafrhv5w hvrLY9SwJYUm+lUYSeZp/vs3mQJSwk5IMvAK92a3PaVE3woQfH7HU1++2k1Z5GanFvpT4V 6RFA3TCYC6NSagWATtLDjYSXcKimQzQ= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="mW/ciwfo"; spf=pass (imf03.hostedemail.com: domain of vbabka@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=vbabka@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 54B3240E3A; Mon, 21 Sep 2026 09:35:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8C8F91F00898; Mon, 21 Sep 2026 09:35:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789983349; bh=vB6u7hTafA7/3xCUaJmo0nI+NPov+j5mxQHYBu1u2js=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=mW/ciwfoGLhjzMJByNGdweG+U3lhBfjpJfFRxw3fuUVWAHoXAWab+F+wWo6W8TUPA oPZqFSqAc1sfBg758UByY0v43uhFNg5YpP174+lUh0067wc9rBeepQGGoieBUIfPfG wbLXX8Tgt9LjF1AnWKsnTy/MuOWoNQvd56pz5cXhi+welfgIpXeDXF7bgKWC6w/G7Y W9SMLmP8InpfKddJ1hukjOJatG0Qo9VnSTh1nCLHMTG29ut9beEWQXehPILBuu+Bm5 HCGwHUkV/5BsUo0d6dU53AsXKU70e57ifNK1RWzwCmIN4UUEOPvqS0MqyRmg6qylac rDNiqGvUnuw7Q== Message-ID: <6c94405a-48e4-4842-a096-e2fbbbaa862f@kernel.org> Date: Mon, 21 Sep 2026 11:35:42 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4] mm/page_alloc: avoid direct compaction for costly __GFP_NORETRY allocations Content-Language: en-US To: Matthew Wilcox , Andrew Morton Cc: Johannes Weiner , Salvatore Dipietro , abuehaze@amazon.com, alisaidi@amazon.com, blakgeof@amazon.com, brauner@kernel.org, brendan.jackman@linux.dev, david@redhat.com, dgc@kernel.org, dipietro.salvatore@gmail.com, djwong@kernel.org, hch@infradead.org, hch@lst.de, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-xfs@vger.kernel.org, mhocko@suse.com, ritesh.list@gmail.com, rvvandan@amazon.com, stable@vger.kernel.org, surenb@google.com, ziy@nvidia.com References: <20260905174239.99e31515fabe220aa7d8e6fa@linux-foundation.org> <20260910114602.926944-1-dipiets@amazon.it> <8d6a8a63-4adc-458a-b548-a47bf5ff8eb7@kernel.org> <20260916153426.3265e2e152cebb6a8d66373d@linux-foundation.org> <20260916153540.9a3fb449f5b3fd481ca1b544@linux-foundation.org> From: "Vlastimil Babka (SUSE)" Autocrypt: addr=vbabka@kernel.org; keydata= xsFNBFZdmxYBEADsw/SiUSjB0dM+vSh95UkgcHjzEVBlby/Fg+g42O7LAEkCYXi/vvq31JTB KxRWDHX0R2tgpFDXHnzZcQywawu8eS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dq7o7VpIUK1nCTQALMuMjKCdi8OdU/+V+R3O4 0PXWvX8qrvqYapVbZ+9KqT74FsuB0Ya9uXwgBF2Q6cRuETZk5vqaqKxzqoQZCO8AOz/58j6O 2RHNy/mZEN+7tJ5Tsq42zVJ4jxsT8b9YplavCMsnBgDeRWhcbYhCyttoL7nYISyWg4kQYZ/P wIV3OuNv2f8iKYsxNsRuClOAF82+gvqOy1/1pprFjy8uo2pkoOrb63aOP3vO5VHnRKgra6dq NcaZ+c6J4H+nEJGi2SkHAUJz5oBzuThvPudLvPA/SK8sKoM01IRxSihev/S/5WLazXB1PGem OCbvzC1IjWJJraxiDJ5IygokapUa2RP7+WBR22skQ3SSl6G107QgWKSyTOGWEaRmV53vxQLV jXuCmzSSasTL60zq5yGrT4/DYQVSNEUiUbG4pYekxJujNeEDkUlky0Y= In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam04 X-Rspam-User: X-Stat-Signature: xat5rmspxepku7nghiakrdeeeufuj9xu X-Rspamd-Queue-Id: 3171C20009 X-HE-Tag: 1789983350-37986 X-HE-Meta: U2FsdGVkX1/0Xho1AmD/Zq5/b4P/u8I1TDutWiERAN7pigzabnuM8yX51rf6HV9L+Y+vApN4ChQWvKiolJ5kWMlppxZVn15b8A5CU9+9Yg7iGnY6izAElwye2IRkbhYLQCbH5T1v0Q25Bf56z8HVeYTRo8cpkQ8kQjv87Jhf7kQc8IxVnsWlRArywYPKssNx/wJ/DXumz/MgEa3Ka+Se8cIS9CBXgLMO2rLpVqNWhZIEITRT/bjnC0QPyaLmImHIc4r2I67XECLMGjE6wXX+aQQkdGWBnP61IOJzaeWsPl3GngmC5IMhd+FRYQqOIwFsa4NRfkvbdgdcTwDEZfKL4k85JL9TJ6iQlOIp/F+jodpmBq4Xyu4FdFbfJEo2+z3SEFf8tcGQn42ZhnczaK5thhkEn+6pr01x3avpmumfqRjeaHkKI4MAkD5VVBpl7EpSIs2v0hYH/lz/no1cjVmaNLbTmY+a7d8/Y2+jL7caBx5HLbGzDPLQfxc00kB0zLeRyhWneTlWRpP+PkTo+RxVagWdd9avwCxUYCYsqZTE95XM1iKYyV2EeRRK936WuDwF/NIGDH84vbxSbwt0KyD1wEukPKKytZdhscAmcAvfjqcOlgETwIEMPXCNiktMfDG+SG0elw2jDdZMBm0gcJBhAULnzbY78R4NT/qstdyI1sI8WueDw5mkO3SnvG1QGSnKNpcGA3IwA3b1qMHbWCvM9JeLfs+b9hTk+zTWGVsjzMl1mhFbfnh/xVnZ9IKWq5OaeH/rEZFJJb/mct9PgfbcyDlguKdUC/ECQIgmvbJDad8jXcgSuK8jepUPvm9ZN5Qh0JdV1Z3dSBM/N7idz2cp1secAZ1rZo0pY+ypBGAXr4HRN5jJ0a+Yu9PiFheHiEGYDlvVydYCY1JSGAWQZXrGfMx58BkXvjFIVJleBIauVCGUMPFMVCtRH9eNc8TOQ1YitInr0z+Sdl/FBlD0Yqj MWnxAsH/ beNYo1Tym4cMbsg+mNWT5zq9J0MpokvI724h6WeUU37+CNVhuP97hb9JNvKrsQOdupCUVoDU3rEuheVbFKKP+JBk0S3CAWlqMSyWSBsg5+exm/S4B4h8H98ipjsjCGfL2IgHVwDE9AarrjA5eu9pYZKH1u87ivu1UUvAMLmsf+4IN2V8IIRCLExTYneXTlP26ZjZ+rEVOIksPuZE16V2964svno2Pjm9ZH6/68GZQ0ZY4aOocWvZMu2ofUQNWQbSAE/mQe9Uu/AVAjQO55amcg32UlpJi/o880LVdr4Idf1wnUng/CMqdxvT/bbrFiQG5+iB5fe0X9g3RPGtl3XJ86lWZBfKL+n1ZrlRSlu9XDMYk9/y8VoYabIEIqKhAtOv8MqVQpfxgu/KFouOeRJUxtYjyoF/z4QUBP7zQkl9OEuv0HdUvLJLtwsydfq9MccySFLwoY3kagBaWvyD1Zy2TnJa+AQ== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 9/19/26 06:13, Matthew Wilcox wrote: > On Wed, Sep 16, 2026 at 03:35:40PM -0700, Andrew Morton wrote: >> From: Salvatore Dipietro >> Subject: mm/page_alloc: avoid direct compaction for costly __GFP_NORETRY allocations >> Date: Fri, 4 Sep 2026 11:56:28 +0000 >> >> Commit 5d8edfb900d5 ("iomap: Copy larger chunks from userspace") >> introduced high-order folio allocations in the iomap buffered write path. >> When memory is fragmented, each failed costly-order allocation enters >> __alloc_pages_slowpath() which runs direct compaction and >> drain_all_pages(), causing a 0.38x throughput drop on PostgreSQL pgbench >> (simple-update) with 1024 clients on a 96-vCPU arm64 system. >> >> The root issue is that direct compaction is too expensive for hot >> allocation paths that have fallbacks to smaller allocations. >> __filemap_get_folio_mpol() already marks higher-order allocations with >> __GFP_NORETRY | __GFP_NOWARN, signalling that the caller can handle >> failure. However, the page allocator still attempts full direct >> compaction for costly orders with __GFP_NORETRY, which is unnecessarily >> aggressive when the caller will simply retry at a lower order. >> >> For costly-order allocations with __GFP_NORETRY, clear >> __GFP_DIRECT_RECLAIM at the very start of the slowpath, before >> can_direct_reclaim, can_compact and the nofail checks are evaluated. This >> makes the entire slowpath treat the request as non-blocking: no direct >> reclaim, no direct compaction and no drain_all_pages() IPI across every >> CPU. kswapd (and in turn kcompactd) is still woken further down for >> background defragmentation, so compaction keeps working for long-term >> system health while being removed from the latency-critical direct >> allocation path. >> >> Allocations that also request __GFP_THISNODE are exempted. That flag >> pairing identifies the local-node-first THP attempt issued by >> alloc_pages_mpol() (mempolicy.c), which relies on direct compaction to >> form transparent huge pages. > > This patch is still piling hack on hack. We haven't made a serious > effort to understand what's going on, we're just adjusting flags until > things stop sucking. > > What we know: > - Doing compaction every time (which is what we currently do) > performs badly > - Doing compaction once at the requested large size and then not > again until the smallest size also performs badly. So it's not > that we're doing too much compaction, it's that we're doing > compaction at all. The very first version [1] mentions the associated drain_all_pages(), interestingly. > But we don't know why compaction is performing badly. For example, > we could specify MIGRATE_ASYNC or MIGRATE_SYNC_LIGHT if __GFP_NORETRY > is set. Just as a reminder, here's how __GFP_NORETRY is documented: That already happens (for costly orders) in __alloc_pages_slowpath() where it sets INIT_COMPACT_PRIORITY. > * %__GFP_NORETRY: The VM implementation will try only very lightweight > * memory direct reclaim to get some memory under memory pressure (thus > * it can sleep). It will avoid disruptive actions like OOM killer. The > * caller must handle the failure which is quite likely to happen under > * heavy memory pressure. The flag is suitable when failure can easily be > * handled at small cost, such as reduced throughput. > > So the callers aren't doing anything unreasonable when they say > __GFP_NORETRY. It's the page allocator (and apparently the compaction > side of it) that's not living up to the documented contract. Note that all these different versions of this patch mention compaction, but implement a removal of __GFP_DIRECT_RECLAIM, thus suppressing both reclaim and compaction, in response to __GFP_NORETRY. Thus turning this effectively to a GFP_NOWAIT allocation. So that's also a violation of the contract, no? In [1] thread and also later on v2 thread by you [2] it was proposed to make the caller itself do this downgrade to GFP_NOWAIT. But Christoph and Dave didn't like that approach (that was conditional to costly order, I don't think yours had a response), which is understandable for the costly-order case, but for your case it makes sense that if you don't want any reclaim+compaction, you tell the allocator GFP_NOWAIT and not __GFP_NORETRY. > So that's one approach which has not, as far as I can tell, been > investigated. > > The other thing that icks me about this patch is how complex the > condition is: > >> + /* >> + * Costly __GFP_NORETRY callers have a cheap fallback, so don't stall >> + * them in reclaim or compaction. __GFP_THISNODE callers are exempt. >> + */ >> + if (costly_order && (gfp_mask & __GFP_NORETRY) && >> + !(gfp_mask & __GFP_THISNODE)) >> + gfp_mask &= ~__GFP_DIRECT_RECLAIM; > > So basically we're saying that __GFP_NORETRY means don't do compaction > unless __GFP_THISNODE is set, which is just special pleading. Surely > the right answer is to remove __GFP_NORETRY from the one caller which > needs __GFP_THISNODE? > > > A slightly unrelated critique of this patch is that it's far too > complicated for what it does. We could achieve the same thing by doing: > > static inline bool gfp_compaction_allowed(gfp_t gfp_mask) > { > - return IS_ENABLED(CONFIG_COMPACTION) && (gfp_mask & __GFP_IO); > + return IS_ENABLED(CONFIG_COMPACTION) && (gfp_mask & __GFP_IO) && > + (!(gfp_mask & __GFP_NORETRY) || (gfp_mask & __GFP_THISNODE)); > } > > > Something I've been noodling on the past day or so is cleaning up the > GFP flags for "how hard to reclaim". We currently have five > possibilities encoded in four bits: > > 1. No direct reclaim (__GFP_DIRECT_RECLAIM clear) > 2. Light reclaim (__GFP_DIRECT_RECLAIM | __GFP_NORETRY) > 3. Normal reclaim (__GFP_DIRECT_RECLAIM) > 4. Extra reclaim (__GFP_DIRECT_RECLAIM | ___GFP_RETRY_MAYFAIL) > 5. Reclaim forever (__GFP_DIRECT_RECLAIM | __GFP_NOFAIL) > > Clearly we can save ourselves a GFP flag bit by encoding those five > options into three bits. Some of the code that manipulates "how hard to > reclaim" will need to be adjusted, but it shouldn't be that many places > to change. > > If we do that, we can insert more options into the mix if they're really > needed. Like we could have: > > 0 - No direct reclaim > 1 - Light reclaim, no compaction > 2 - Light reclaim with compaction > 3 - Normal reclaim > 4 - Extra reclaim > 5 - Reclaim forever There's also still (for better or worse) the THP special case of light compaction with no reclaim. And the long-standing principle (maybe cargo cult? who knows) that for non-costly-order allocations (with fallback) the tradeoff of e.g. 2 is more favorable than 0. But we don't want the callers to have to change flags depending on the order. So it would have to be a distinct option? > and we'd still have two extra states in case we need to add more > flavours. [1] https://lore.kernel.org/all/20260403193535.9970-1-dipiets@amazon.it/ [2] https://lore.kernel.org/all/alEz4Chf7Ibyg-ZG@casper.infradead.org/