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]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D791C3DA4A for ; Thu, 15 Aug 2024 03:26:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D67DE6B007B; Wed, 14 Aug 2024 23:26:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D17B46B0082; Wed, 14 Aug 2024 23:26:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDE176B0083; Wed, 14 Aug 2024 23:26:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A03266B007B for ; Wed, 14 Aug 2024 23:26:50 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4D0084135E for ; Thu, 15 Aug 2024 03:26:50 +0000 (UTC) X-FDA: 82453042980.08.57CCC0E Received: from mail-yb1-f175.google.com (mail-yb1-f175.google.com [209.85.219.175]) by imf22.hostedemail.com (Postfix) with ESMTP id 87E3CC0014 for ; Thu, 15 Aug 2024 03:26:48 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fGxnAn7w; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.219.175 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723692336; 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=yFfMWORYoBpF0kpuHqcETmw0Fcag3tVw/GjGoSWPTvM=; b=lDskhWeA6sNxWjUdLcOHdx99HypDbZc2RfWzOQ2w4UASLSQvX5g3Toi3fJjTVBPvLVFvGK SAp9qHOFNT4BKUfyb2w1QK8t0REr7Vlji0Tb6/Md9HLruv9QUzGf0BXDcXjTAJS2pLmpEG ai9WkuurH+1Q70j/d5eB12eMxrMduL0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723692336; a=rsa-sha256; cv=none; b=A0khFF+HN1txK7S1A6bBw3mz/hOxItO/S3K6jtb0cHBAnWqYYtcgh1t+QZWW9MKERVzrcb T/D3zlUZrg1+bIys7PDos4Q+tE6/uKFkfGDvfJQpNXWomjmMMNAMbIEPzf6oIT0P9p0XJd Anda7+DiVGWp0R2rLB9iWglKqgk/wNk= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fGxnAn7w; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.219.175 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-yb1-f175.google.com with SMTP id 3f1490d57ef6-e0ea24477f0so628978276.2 for ; Wed, 14 Aug 2024 20:26:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723692407; x=1724297207; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=yFfMWORYoBpF0kpuHqcETmw0Fcag3tVw/GjGoSWPTvM=; b=fGxnAn7wXBseZ/GIPPRLCIn+kdCPYEt7UPchTeiYPWBhCRzgb8v0oX2cG23lArQA/k /skyq1nnICtz+sBaO9pVTu7vX2n9FvGNF79dMASnuLqBPN5J++vWvDKzRmdRwIfu0MtF 5KVTh7y003QP5ZYnLUDVTdH29HvUCljyJHNyDih0q4Cinnp35Z1VX0+rWp93w5e/Ht+q UZeXIaW/b2Il5Kj8NbDvs4Abx7fu8TH5bG/eyUjMd16OSUDNws9RmKFSq+wzX81mym55 HkRSQKxb7ZCqnHizqx0gQOY3E+luwvg4etgSx/Sr1Es1zp+V5R5us1AhH0w85N5+WK5G k01Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723692407; x=1724297207; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yFfMWORYoBpF0kpuHqcETmw0Fcag3tVw/GjGoSWPTvM=; b=Xu0RMsoCCjImJ7nJyqmXF1iTbfozpv/8Zh93znx+BiaoX7IfKlhZD9zw/ky0o92VHM 1zDpfC6Kv/zmjCIqWfED8htCj99gV8cuPccmZRnGr6LlXzoWrtw/2oLYolzGgJ3xnHyN cY8tCTywCvNu2nFa7rmUJ8wcuo8FA9d1KiX+d8SnlWKMoJ4uimiro36u5E4EMjBru1F+ jsgcfH5F5axhJlLnEF6BuEhScisHaRTChAiKmBcA6MsCNLO5+fBe7vsG7KEcabdjI3wN 78LW6QvosRSzruPv6Z7lhbQutACDAuJiKDij7ntF4Cyulv9k48NCrEhw6RgV1RV/RJ75 tEtA== X-Forwarded-Encrypted: i=1; AJvYcCWKqNK8WiJufMdUcV8vHXOZHNKgR7cNgpX1U1iUCXEldymqPxhdkrv8Mw+w8ZClY/hpsDiO07G9Mmref2KVfI1JKdM= X-Gm-Message-State: AOJu0YxoBCxPswri51n19QPm+w/fxtvm34wNtC0AaUxDaUHoD1rphRFt kuWBjYf7tZjpqiMQwqIrTBOJSPUG1mW1tPdvtyJ+jB6/Zc7jbTiOu/kZqKj3qqc/OHmJmDAbI9T I/L+Na19cAgpmYPTMdp/rzRpbhHc= X-Google-Smtp-Source: AGHT+IG7Hn4LJy0dye4N83a07fHjFom4Pikv7OZAHH9fYak5Ir8TNfRuLLFsamKGohm3jtAIbhl4tMMBHDfA4tYJrWM= X-Received: by 2002:a05:6902:2506:b0:e11:44b9:6bb7 with SMTP id 3f1490d57ef6-e1155ae252amr5061387276.24.1723692407495; Wed, 14 Aug 2024 20:26:47 -0700 (PDT) MIME-Version: 1.0 References: <20240812090525.80299-1-laoar.shao@gmail.com> <20240812090525.80299-2-laoar.shao@gmail.com> In-Reply-To: From: Yafang Shao Date: Thu, 15 Aug 2024 11:26:09 +0800 Message-ID: Subject: Re: [PATCH 1/2] mm: Add memalloc_nowait_{save,restore} To: Michal Hocko Cc: Christoph Hellwig , akpm@linux-foundation.org, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, david@fromorbit.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Kent Overstreet Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 87E3CC0014 X-Stat-Signature: min35y557meybqq6r4ci5dob7bmacty3 X-HE-Tag: 1723692408-38481 X-HE-Meta: U2FsdGVkX19DNIZ/UIfsIGHqR2Cv6AtcDjUxiEJehK0o6LrohIfrXLnjWruOnPeXUhnTr6EDWqh2exbBw+0ymuJhmCi19mXhqLQR5keacovvF5z4ZjWiq3kuGafXD84mFDJ1VGmuWIIaAcK6Hn51tYvbKVu/8xJ5K2E01oAbcVM6CH7nBaZHDyPD8ERakrjFQXPNF7Gcc4Q/iM4iuLQpzssOrxYktKqgwq3SXDsYNkyuIU8TZnCdk89YN0HGnbPE6rzMlKzdyDVHZ1Tl4iZwRK4HSDOSDpQYAuugfWuHncasiDEoS3H9AoaTS5HIhFQweyj2Z9xHByG15ULzcBbuKixo/eQ7U/z0SfOvSd7MfyCrg4+R9pWWzKW2iV11U1oeTAgTzs66aR5KibvWC30lL7lWX65dOxfIg15ljMYzYdqyN9JxBxXgo+Dt1loANF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auYsqNNC0Uxak/oCFfz63GZzvVe95udoHgN5dUh94UQJQL X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, Aug 14, 2024 at 8:43=E2=80=AFPM Michal Hocko wrot= e: > > On Wed 14-08-24 16:12:27, Yafang Shao wrote: > > On Wed, Aug 14, 2024 at 3:42=E2=80=AFPM Michal Hocko = wrote: > > > > > > On Mon 12-08-24 20:59:53, Yafang Shao wrote: > > > > On Mon, Aug 12, 2024 at 7:37=E2=80=AFPM Christoph Hellwig wrote: > > > > > > > > > > On Mon, Aug 12, 2024 at 05:05:24PM +0800, Yafang Shao wrote: > > > > > > The PF_MEMALLOC_NORECLAIM flag was introduced in commit eab0af9= 05bfc > > > > > > ("mm: introduce PF_MEMALLOC_NORECLAIM, PF_MEMALLOC_NOWARN"). To= complement > > > > > > this, let's add two helper functions, memalloc_nowait_{save,res= tore}, which > > > > > > will be useful in scenarios where we want to avoid waiting for = memory > > > > > > reclamation. > > > > > > > > > > No, forcing nowait on callee contets is just asking for trouble. > > > > > Unlike NOIO or NOFS this is incompatible with NOFAIL allocations > > > > > > > > I don=E2=80=99t see any incompatibility in __alloc_pages_slowpath()= . The > > > > ~__GFP_DIRECT_RECLAIM flag only ensures that direct reclaim is not > > > > performed, but it doesn=E2=80=99t prevent the allocation of pages f= rom > > > > ALLOC_MIN_RESERVE, correct? > > > > > > Right but this means that you just made any potential nested allocati= on > > > within the scope that is GFP_NOFAIL a busy loop essentially. Not to > > > mention it BUG_ON as non-sleeping GFP_NOFAIL allocations are > > > unsupported. I believe this is what Christoph had in mind. > > > > If that's the case, I believe we should at least consider adding the > > following code change to the kernel: > > We already do have that > /* > * All existing users of the __GFP_NOFAIL are blockable, = so warn > * of any new users that actually require GFP_NOWAIT > */ > if (WARN_ON_ONCE_GFP(!can_direct_reclaim, gfp_mask)) > goto fail; I don't see a reason to place the `goto fail;` above the `__alloc_pages_cpuset_fallback(gfp_mask, order, ALLOC_MIN_RESERVE, ac);` line. Since we've already woken up kswapd, it should be acceptable to allocate memory from ALLOC_MIN_RESERVE temporarily. Why not consider implementing the following changes instead? diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 9ecf99190ea2..598d4df829cd 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -4386,13 +4386,6 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, * we always retry */ if (gfp_mask & __GFP_NOFAIL) { - /* - * All existing users of the __GFP_NOFAIL are blockable, so= warn - * of any new users that actually require GFP_NOWAIT - */ - if (WARN_ON_ONCE_GFP(!can_direct_reclaim, gfp_mask)) - goto fail; - /* * PF_MEMALLOC request from this context is rather bizarre * because we cannot reclaim anything and only can loop wai= ting @@ -4419,6 +4412,14 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, if (page) goto got_pg; + /* + * All existing users of the __GFP_NOFAIL are blockable, so= warn + * of any new users that actually require GFP_NOWAIT + */ + if (WARN_ON_ONCE_GFP(!can_direct_reclaim, gfp_mask)) { + goto fail; + } + cond_resched(); goto retry; } > > But Barry has patches to turn that into BUG because failing NOFAIL > allocations is not cool and cause unexpected failures. Have a look at > https://lore.kernel.org/all/20240731000155.109583-1-21cnbao@gmail.com/ > > > > I am really > > > surprised that we even have PF_MEMALLOC_NORECLAIM in the first place! > > > > There's use cases for it. > > Right but there are certain constrains that we need to worry about to > have a maintainable code. Scope allocation contrains are really a good > feature when that has a well defined semantic. E.g. NOFS, NOIO or > NOMEMALLOC (although this is more self inflicted injury exactly because > PF_MEMALLOC had a "use case"). NOWAIT scope semantic might seem a good > feature but it falls appart on nested NOFAIL allocations! So the flag is > usable _only_ if you fully control the whole scoped context. Good luck > with that long term! This is fragile, hard to review and even harder to > keep working properly. The flag would have been Nacked on that ground. > But nobody asked... It's already implemented, and complaints won't resolve the issue. How about making the following change to provide a warning when this new flag is used incorrectly? diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index 4fbae0013166..5a1e1bcde347 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -267,9 +267,10 @@ static inline gfp_t current_gfp_context(gfp_t flags) * Stronger flags before weaker flags: * NORECLAIM implies NOIO, which in turn implies NOFS */ - if (pflags & PF_MEMALLOC_NORECLAIM) + if (pflags & PF_MEMALLOC_NORECLAIM) { flags &=3D ~__GFP_DIRECT_RECLAIM; - else if (pflags & PF_MEMALLOC_NOIO) + WARN_ON_ONCE_GFP(flags & __GFP_NOFAIL, flags) + } else if (pflags & PF_MEMALLOC_NOIO) flags &=3D ~(__GFP_IO | __GFP_FS); else if (pflags & PF_MEMALLOC_NOFS) flags &=3D ~__GFP_FS; -- Regards Yafang