From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 3A82D346FA0 for ; Tue, 6 Jan 2026 13:56:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767707790; cv=none; b=cnjFVwikLt0Le6+0MOCPgrvqHyEf87zieCgto9DesWjc27DMOrVqIHV6gnmjkcVJflad+n4O6Ei7Smi2mj6p/L4OoYmg33pCuCVh/q0hxBp0ktHr7gCBi6Iwyaas58WQQtyer+85vvNlnIkP8JE8P/igNiya7AS4Ed+mSZztqjc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767707790; c=relaxed/simple; bh=c3G6G+3fr0MRgKMsB/DBUK3DlYgGffa4vB2Uol55Mf4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=BrMUqmLg0s0cSkw34H0XBVEkt7oFBioqivvh3o2+1CgiwVo7qEzO3sNsHnYnV5M7oCOh2UFrooEtWEekZNvcUybhKaUIk0wQ7kWZK+zS/an3V+MhXKHvFgQEm07zSqu6ECX+B7KnCcwLGNttGk7hV6k4V6ybn/O0JMKnbz1zfuw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=Dyav3T8E; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="Dyav3T8E" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-4779cc419b2so8209445e9.3 for ; Tue, 06 Jan 2026 05:56:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1767707786; x=1768312586; 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=P9U1EgROnTPgLiCYOpEnaofbmAnZpWUDuK849oJPJ2s=; b=Dyav3T8EcbHnxrZGgpKjkMRBVOFken69Ott324Zp6OA+7VCx0A7I/z4e1cj4rrQT1c G5sbXRf2HkAh9fe0aJvionjww6hB0ffrdeQHJhXArTCIireBjTtD6vmiLYydpNl63aB7 8stRUwByx1VmeQ8wYkfnRUS0fMZsMD1SpjsoaQgVUXzauZsFZVDa4S5AC8tum5PKLSL5 Ah3zkDlWVjOs0nRawHw2mpDAJqTQZASNU8bdJIJTJuy2T2r/jxOZeL+e5T/DLei6vtse nWtedWAJs1MSaL0GGGsbWCrQuiGAaHrFuXQG/SPu92tBNd4TaKjOidAraDlSzHDZFr0W FmGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767707786; x=1768312586; 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=P9U1EgROnTPgLiCYOpEnaofbmAnZpWUDuK849oJPJ2s=; b=wWR4c9XD+q61uCIfpMS9W0FTl37cCAARlB6Y2fXy3k42UAxb7ufNeE7uvwL2ie3HWG WzT9l0zwoIj3mgoXpufiSo079scc3lWF7cQWv+awH+K72pUuxU1Go1JfSEDflbxdWIn2 NVFVWr7rwLkLVSfKWdCVehk0TEMEIWJgHhJ6z1l8Bf8+Imp+wf2pLxi6jR2dF8wYyvPP 8tOwhhNFsyGhXkc8s1cN2JORSEWgHcnuFKnsvF0X+dlLOOM3+MJBbbSQfBEqdRuf60bB +5FKV3W1vR7t0W+e19kZC4/AFMjbASh+Jkvk5Ib/zWxtrfsah2kEKD6SPEmuxYfAJYkl zNXg== X-Forwarded-Encrypted: i=1; AJvYcCX0qRleW5mFX08apofIvuI7A8S1B0R1v80dmbcpUeuyBJ0I2sUBu5FKLI22o8zYPuxS4VZ8c8d6OsCCWPw=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2kQMHHm0U4h7Vnwff0s3u7VyeMOO/VNSdIQ+1hNRx8zF37ARN iyYgNmoHygkt1uvuJWfMZFcZOZ3WsNQKr/NAZ8Vo9QD6Cbwk6WdYPb2TRULaDeMaNHE= X-Gm-Gg: AY/fxX6F83POKlVdkr77CVr/Q+8bswDJZLOPXlUdvOFKq2VKFypQNbLrLZR3I5030y1 fdkm+OwpKY1dMM4X5f3XpLj0BVY2kFcC3G5FvCeSqPA1rkoQG/ELYrCl0NG9lfOYbccZj4+uDFa lome+Bt60BbkF+im7DAKE2yDMvQEEMnc5CEH26dlMx9Wj5N1i21tHhSupJmXhh6fIQcyzYPoalm T3hH04WJxBF/unwSgzzoLruWF1lTrpuHuizLnhhoEJvoogUb+CzQM/zN1sitP+CTUgZEAmDaZHq nhX1LRocq/IKOXPaYBXVMObeJA8dAcf/Rm7LgHYJL0Erb6Tmqt70XkbfLGhW4mQKaEayKF3BXtd dvMNELDY3J0AOO2LweyCax2ANOFVzGIdawicilsHMSsPOUeV1D1o0i3pX6d7kLYP+wAsjz95PhH Aga8Ju0iyp24Jpm4sRnBkvtNXQ X-Google-Smtp-Source: AGHT+IE4XW8UA8O6FTFvSLSZT78RW9Frwzig8Nc3GGs5Vr2i319n/ah+9CnKoWL5u7mLcYdyy95I7g== X-Received: by 2002:a05:600c:5306:b0:477:582e:7a81 with SMTP id 5b1f17b1804b1-47d7f067283mr32135105e9.4.1767707786371; Tue, 06 Jan 2026 05:56:26 -0800 (PST) Received: from localhost (109-81-90-116.rct.o2.cz. [109.81.90.116]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-432bd0e6784sm4449493f8f.19.2026.01.06.05.56.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jan 2026 05:56:26 -0800 (PST) Date: Tue, 6 Jan 2026 14:56:24 +0100 From: Michal Hocko To: Vlastimil Babka Cc: Andrew Morton , Suren Baghdasaryan , Brendan Jackman , Johannes Weiner , Zi Yan , David Rientjes , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Mike Rapoport , Joshua Hahn , Pedro Falcato , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH mm-unstable v3 2/3] mm/page_alloc: refactor the initial compaction handling Message-ID: References: <20260106-thp-thisnode-tweak-v3-0-f5d67c21a193@suse.cz> <20260106-thp-thisnode-tweak-v3-2-f5d67c21a193@suse.cz> 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: <20260106-thp-thisnode-tweak-v3-2-f5d67c21a193@suse.cz> On Tue 06-01-26 12:52:37, Vlastimil Babka wrote: > The initial direct compaction done in some cases in > __alloc_pages_slowpath() stands out from the main retry loop of > reclaim + compaction. > > We can simplify this by instead skipping the initial reclaim attempt via > a new local variable compact_first, and handle the compact_prority as > necessary to match the original behavior. No functional change intended. > > Suggested-by: Johannes Weiner > Signed-off-by: Vlastimil Babka > Reviewed-by: Joshua Hahn LGTM and it makes the code flow easier to follow Acked-by: Michal Hocko > --- > include/linux/gfp.h | 8 ++++- > mm/page_alloc.c | 100 +++++++++++++++++++++++++--------------------------- > 2 files changed, 55 insertions(+), 53 deletions(-) > > diff --git a/include/linux/gfp.h b/include/linux/gfp.h > index aa45989f410d..6ecf6dda93e0 100644 > --- a/include/linux/gfp.h > +++ b/include/linux/gfp.h > @@ -407,9 +407,15 @@ extern gfp_t gfp_allowed_mask; > /* Returns true if the gfp_mask allows use of ALLOC_NO_WATERMARK */ > bool gfp_pfmemalloc_allowed(gfp_t gfp_mask); > > +/* A helper for checking if gfp includes all the specified flags */ > +static inline bool gfp_has_flags(gfp_t gfp, gfp_t flags) > +{ > + return (gfp & flags) == flags; > +} > + > static inline bool gfp_has_io_fs(gfp_t gfp) > { > - return (gfp & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS); > + return gfp_has_flags(gfp, __GFP_IO | __GFP_FS); > } > > /* > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index b06b1cb01e0e..3b2579c5716f 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -4702,7 +4702,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, > struct alloc_context *ac) > { > bool can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM; > - bool can_compact = gfp_compaction_allowed(gfp_mask); > + bool can_compact = can_direct_reclaim && gfp_compaction_allowed(gfp_mask); > bool nofail = gfp_mask & __GFP_NOFAIL; > const bool costly_order = order > PAGE_ALLOC_COSTLY_ORDER; > struct page *page = NULL; > @@ -4715,6 +4715,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, > unsigned int cpuset_mems_cookie; > unsigned int zonelist_iter_cookie; > int reserve_flags; > + bool compact_first = false; > > if (unlikely(nofail)) { > /* > @@ -4738,6 +4739,19 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, > cpuset_mems_cookie = read_mems_allowed_begin(); > zonelist_iter_cookie = zonelist_iter_begin(); > > + /* > + * For costly allocations, try direct compaction first, as it's likely > + * that we have enough base pages and don't need to reclaim. For non- > + * movable high-order allocations, do that as well, as compaction will > + * try prevent permanent fragmentation by migrating from blocks of the > + * same migratetype. > + */ > + if (can_compact && (costly_order || (order > 0 && > + ac->migratetype != MIGRATE_MOVABLE))) { > + compact_first = true; > + compact_priority = INIT_COMPACT_PRIORITY; > + } > + > /* > * The fast path uses conservative alloc_flags to succeed only until > * kswapd needs to be woken up, and to avoid the cost of setting up > @@ -4780,53 +4794,6 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, > if (page) > goto got_pg; > > - /* > - * For costly allocations, try direct compaction first, as it's likely > - * that we have enough base pages and don't need to reclaim. For non- > - * movable high-order allocations, do that as well, as compaction will > - * try prevent permanent fragmentation by migrating from blocks of the > - * same migratetype. > - * Don't try this for allocations that are allowed to ignore > - * watermarks, as the ALLOC_NO_WATERMARKS attempt didn't yet happen. > - */ > - if (can_direct_reclaim && can_compact && > - (costly_order || > - (order > 0 && ac->migratetype != MIGRATE_MOVABLE)) > - && !gfp_pfmemalloc_allowed(gfp_mask)) { > - page = __alloc_pages_direct_compact(gfp_mask, order, > - alloc_flags, ac, > - INIT_COMPACT_PRIORITY, > - &compact_result); > - if (page) > - goto got_pg; > - > - /* > - * Checks for costly allocations with __GFP_NORETRY, which > - * includes some THP page fault allocations > - */ > - if (costly_order && (gfp_mask & __GFP_NORETRY)) { > - /* > - * THP page faults may attempt local node only first, > - * but are then allowed to only compact, not reclaim, > - * see alloc_pages_mpol(). > - * > - * Compaction has failed above and we don't want such > - * THP allocations to put reclaim pressure on a single > - * node in a situation where other nodes might have > - * plenty of available memory. > - */ > - if (gfp_mask & __GFP_THISNODE) > - goto nopage; > - > - /* > - * Proceed with single round of reclaim/compaction, but > - * since sync compaction could be very expensive, keep > - * using async compaction. > - */ > - compact_priority = INIT_COMPACT_PRIORITY; > - } > - } > - > retry: > /* > * Deal with possible cpuset update races or zonelist updates to avoid > @@ -4870,10 +4837,12 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, > goto nopage; > > /* Try direct reclaim and then allocating */ > - page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, ac, > - &did_some_progress); > - if (page) > - goto got_pg; > + if (!compact_first) { > + page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, > + ac, &did_some_progress); > + if (page) > + goto got_pg; > + } > > /* Try direct compaction and then allocating */ > page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags, ac, > @@ -4881,6 +4850,33 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, > if (page) > goto got_pg; > > + if (compact_first) { > + /* > + * THP page faults may attempt local node only first, but are > + * then allowed to only compact, not reclaim, see > + * alloc_pages_mpol(). > + * > + * Compaction has failed above and we don't want such THP > + * allocations to put reclaim pressure on a single node in a > + * situation where other nodes might have plenty of available > + * memory. > + */ > + if (gfp_has_flags(gfp_mask, __GFP_NORETRY | __GFP_THISNODE)) > + goto nopage; > + > + /* > + * For the initial compaction attempt we have lowered its > + * priority. Restore it for further retries, if those are > + * allowed. With __GFP_NORETRY there will be a single round of > + * reclaim and compaction with the lowered priority. > + */ > + if (!(gfp_mask & __GFP_NORETRY)) > + compact_priority = DEF_COMPACT_PRIORITY; > + > + compact_first = false; > + goto retry; > + } > + > /* Do not loop if specifically requested */ > if (gfp_mask & __GFP_NORETRY) > goto nopage; > > -- > 2.52.0 -- Michal Hocko SUSE Labs