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 1AED7C4829A for ; Tue, 13 Feb 2024 14:16:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B23E6B0092; Tue, 13 Feb 2024 09:16:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6DF276B0093; Tue, 13 Feb 2024 09:16:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 57FBD6B0095; Tue, 13 Feb 2024 09:16:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 483986B0092 for ; Tue, 13 Feb 2024 09:16:26 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1A1A81C15A0 for ; Tue, 13 Feb 2024 14:16:26 +0000 (UTC) X-FDA: 81786980772.29.2926511 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf29.hostedemail.com (Postfix) with ESMTP id 6B28012002C for ; Tue, 13 Feb 2024 14:16:21 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sgdWUkbC; spf=pass (imf29.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707833783; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=uGK7qUO5lH5ODxHwrit3hymtgb87pvLdNptBYDAhrt0=; b=DfYRlHP3UgWFa9uPWC0lZzdO1pQ31lSkEmInPdRdhchNl+irM6u4BTuq2kBVhq9NJNE2Rf wk7aGf3uwHTWdJykRP0Jji2c0gVEKqLzv6ZhSz3QVcUoB4lyMjQRMbRAjM8jnfdi55drpo ZSg/Mm4mVio+fGGU8U/G/LmctiRdjdU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707833783; a=rsa-sha256; cv=none; b=xmo7jwhPI+fKkCQ4BrImfXfOPphgjcteAH3k7B0KFIPcEEa/r/0wnygAI3jvT4RMsi9zHD 1J0QwHD2aedu82kW0pbigT4xvvqYBR51m8OYZxOhjKcmkdDNU/IO8Q2NnKOoKIL+KKmuSW U8HY49VmYxXE/CMg4L5kPFSdJX6N6DI= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sgdWUkbC; spf=pass (imf29.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 31774CE1BC7; Tue, 13 Feb 2024 14:16:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 73155C433C7; Tue, 13 Feb 2024 14:16:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707833777; bh=L6jRfxMYCh8UD1FD1aOEy6MCfeW64Gaqwg9aSufjq50=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=sgdWUkbCwCbviBUoRnyk0DAh9/UAUx61Z33BY+4ngyew4ErGf0x0MDBgnW55JNIPw t/iytl4JaD0nsJrrqXBSslMdEKDCjS4cYQIrIKT6CQzNj+UmmIlgLogG9CQprWetl+ 1RNy/qVEvbA93I6TIY8zItmAZJdalHOE0R+DjEFXIHXx7JEcgNICVAQqDvaqggyUaL XScLhr8wYnlUVIGcJXYOLejCq1rFHpJ7fLjXtmFN1dOEUGJ/61xLEelyZxeSTHObSe ifmCOlEECfZwwtKkPDiHA2pyogdX5StZMBXA7T7M1+EF5v9xd6Un99N2FL5fUuz/Zn KDXkbIqzlfIyg== Date: Tue, 13 Feb 2024 16:15:54 +0200 From: Mike Rapoport To: Vlastimil Babka Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Michal Hocko , Mel Gorman , Matthew Wilcox , Kent Overstreet , Pasha Tatashin , Mike Rapoport Subject: Re: [PATCH] mm: document memalloc_noreclaim_save() and memalloc_pin_save() Message-ID: References: <20240212182950.32730-2-vbabka@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240212182950.32730-2-vbabka@suse.cz> X-Rspamd-Queue-Id: 6B28012002C X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: k6kmezfpsoipijuxa3gjt8ux5gy3qqqt X-HE-Tag: 1707833781-974259 X-HE-Meta: U2FsdGVkX18pCJcW8pIlY6W5zs/a/l0xDp+E4PivMJFfor2xaZ0zmYsygsKCfWnj95bcSP6nz8FR/3nXh2uv2t8hiOqB19Ch3oXiq9a8perbC3nigbj0oR1HJauFMnpdivvCgV38cfFlHwS+oD1aI6ItYkZlCpYMeDQqRtJ3wY0iqwlofndkZmxQrEVCeIjlXENKz1w2jdi/r2gAQyw8jUd1hsWKO5rdwV8rffVJr/czr4kk7uNJZbJaRmNd4bQtZSshB7ZYVIKNBIDWInNAyxPQs4vDaUfSpepJ9rNl/rxFYFIBCTZv8H4oe39wdAafXOt3WDhw5MCNnfiXJch9qHJH/TnyhVgBGmABsv1OFpS3QFxRU5wV/YwddoJ7qCWnR+1QxdNXLiNVcpWqvktOVJzeox6ZSZhVLHLQ/gu6PQAsMRWUXw8ac/tTNy2/llwRjxbURdwytEjRxigw76YYdc5+q3z4Iq2qEKLczuHtAmCKl5dbtEiJXNLFj4R/vcErIjBR7z+lYKzKJY0vDN294e5J+SWYNX+dhhUIFKRo/vD/RGEGzN0LM+foRz14dvRs6ofEmoLVs476c3zkBa4wNG6b6p/br8ia4LmKP/YEaSKozyNOIL+wB94Lu52EWY7Kvih0gsWfRb3ed+DI0tWUkNXrKxCGp1FCjC7H9Hqi3RUiMuUclYxb/zwkWF34tZ2ftQ7o6F2uumu69D2o4I33mLE8RBWnlvAgjltLyVyKJIGuA7/nd9fqTjObvmeWOOpDJJllpDgrg55n/M2hksPvjgwZfjFYY+pNWblbBH964bup+wEJWl1x9+afc7yOgIU8VJh8BrxpsumAY6wFhOUuVkzRtIye8qq4DDfb5O1EP8R0NP5ycBPBa2Hfbx7sdieYCU/d4exLeTrJAj4AsLhxqy6Vm5VnAUR4K2lS7lJ8ypkjsMLj57hEc+4qBJGIu6iAOPR11Sr2zbPi1lKzq1+ 7laxnOtB SneRdJuLViyUNZfrVN9XJ2E6St97J25hiccmM 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 Mon, Feb 12, 2024 at 07:29:51PM +0100, Vlastimil Babka wrote: > The memalloc_noreclaim_save() function currently has no documentation > comment, so the implications of its usage are not obvious. Namely that > it not only prevents entering reclaim (as the name suggests), but also > allows using all memory reserves and thus should be only used in > contexts that are allocating memory to free memory. This may lead to new > improper usages being added. > > Thus add a documenting comment, based on the description of > __GFP_MEMALLOC. While at it, also document memalloc_pin_save() so that > all the memalloc_ scopes are documented. In the comments describing the > relevant PF_MEMALLOC flags, refer to their scope setting functions. > > Signed-off-by: Vlastimil Babka Except few nits below Reviewed-by: Mike Rapoport (IBM) > --- > include/linux/sched.h | 9 ++++---- > include/linux/sched/mm.h | 45 ++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 50 insertions(+), 4 deletions(-) > > diff --git a/include/linux/sched.h b/include/linux/sched.h > index ffe8f618ab86..f2cb479f56a7 100644 > --- a/include/linux/sched.h > +++ b/include/linux/sched.h > @@ -1623,15 +1623,15 @@ extern struct pid *cad_pid; > #define PF_SUPERPRIV 0x00000100 /* Used super-user privileges */ > #define PF_DUMPCORE 0x00000200 /* Dumped core */ > #define PF_SIGNALED 0x00000400 /* Killed by a signal */ > -#define PF_MEMALLOC 0x00000800 /* Allocating memory */ > +#define PF_MEMALLOC 0x00000800 /* Allocating memory to free memory. See memalloc_noreclaim_save() */ > #define PF_NPROC_EXCEEDED 0x00001000 /* set_user() noticed that RLIMIT_NPROC was exceeded */ > #define PF_USED_MATH 0x00002000 /* If unset the fpu must be initialized before use */ > #define PF_USER_WORKER 0x00004000 /* Kernel thread cloned from userspace thread */ > #define PF_NOFREEZE 0x00008000 /* This thread should not be frozen */ > #define PF__HOLE__00010000 0x00010000 > #define PF_KSWAPD 0x00020000 /* I am kswapd */ > -#define PF_MEMALLOC_NOFS 0x00040000 /* All allocation requests will inherit GFP_NOFS */ > -#define PF_MEMALLOC_NOIO 0x00080000 /* All allocation requests will inherit GFP_NOIO */ > +#define PF_MEMALLOC_NOFS 0x00040000 /* All allocations inherit GFP_NOFS. See memalloc_nfs_save() */ > +#define PF_MEMALLOC_NOIO 0x00080000 /* All allocations inherit GFP_NOIO. See memalloc_noio_save() */ > #define PF_LOCAL_THROTTLE 0x00100000 /* Throttle writes only against the bdi I write to, > * I am cleaning dirty pages from some other bdi. */ > #define PF_KTHREAD 0x00200000 /* I am a kernel thread */ > @@ -1641,7 +1641,8 @@ extern struct pid *cad_pid; > #define PF__HOLE__02000000 0x02000000 > #define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_mask */ > #define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */ > -#define PF_MEMALLOC_PIN 0x10000000 /* Allocation context constrained to zones which allow long term pinning. */ > +#define PF_MEMALLOC_PIN 0x10000000 /* Allocations constrained to zones which allow long term pinning. > + * See memalloc_pin_save() */ > #define PF__HOLE__20000000 0x20000000 > #define PF__HOLE__40000000 0x40000000 > #define PF_SUSPEND_TASK 0x80000000 /* This thread called freeze_processes() and should not be frozen */ > diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h > index 9a19f1b42f64..eef8fa5ba5de 100644 > --- a/include/linux/sched/mm.h > +++ b/include/linux/sched/mm.h > @@ -368,6 +368,27 @@ static inline void memalloc_nofs_restore(unsigned int flags) > current->flags = (current->flags & ~PF_MEMALLOC_NOFS) | flags; > } > > +/** > + * memalloc_noreclaim_save - Marks implicit __GFP_MEMALLOC scope. > + * > + * This functions marks the beginning of the __GFP_MEMALLOC allocation scope. ^ function > + * All further allocations will implicitly add the __GFP_MEMALLOC flag, which > + * prevents entering reclaim and allows access to all memory reserves. This > + * should only be used when the caller guarantees the allocation will allow more > + * memory to be freed very shortly, i.e. it needs to allocate some memory in > + * the process of freeing memory, and cannot reclaim due to potential recursion. > + * > + * Users of this scope have to be extremely careful to not deplete the reserves > + * completely and implement a throttling mechanism which controls the > + * consumption of the reserve based on the amount of freed memory. Usage of a > + * pre-allocated pool (e.g. mempool) should be always considered before using > + * this scope. > + * > + * Individual allocations under the scope can opt out using __GFP_NOMEMALLOC > + * > + * This function should not be used in an interrupt context as that one does not > + * give PF_MEMALLOC access to reserves, see __gfp_pfmemalloc_flags(). Missing Return: description > + */ > static inline unsigned int memalloc_noreclaim_save(void) > { > unsigned int flags = current->flags & PF_MEMALLOC; > @@ -375,11 +396,27 @@ static inline unsigned int memalloc_noreclaim_save(void) > return flags; > } > > +/** > + * memalloc_noreclaim_restore - Ends the implicit __GFP_MEMALLOC scope. > + * @flags: Flags to restore. > + * > + * Ends the implicit __GFP_MEMALLOC scope started by memalloc_noreclaim_save > + * function. Always make sure that the given flags is the return value from the > + * pairing memalloc_noreclaim_save call. > + */ > static inline void memalloc_noreclaim_restore(unsigned int flags) > { > current->flags = (current->flags & ~PF_MEMALLOC) | flags; > } > > +/** > + * memalloc_pin_save - Marks implicit ~__GFP_MOVABLE scope. > + * > + * This functions marks the beginning of the ~__GFP_MOVABLE allocation scope. ^ function > + * All further allocations will implicitly remove the __GFP_MOVABLE flag, which > + * will constraint the allocations to zones that allow long term pinning, i.e. > + * not ZONE_MOVABLE zones. Missing Return: description > + */ > static inline unsigned int memalloc_pin_save(void) > { > unsigned int flags = current->flags & PF_MEMALLOC_PIN; > @@ -388,6 +425,14 @@ static inline unsigned int memalloc_pin_save(void) > return flags; > } > > +/** > + * memalloc_pin_restore - Ends the implicit ~__GFP_MOVABLE scope. > + * @flags: Flags to restore. > + * > + * Ends the implicit ~__GFP_MOVABLE scope started by memalloc_pin_save function. > + * Always make sure that the given flags is the return value from the pairing > + * memalloc_pin_save call. > + */ > static inline void memalloc_pin_restore(unsigned int flags) > { > current->flags = (current->flags & ~PF_MEMALLOC_PIN) | flags; > -- > 2.43.0 > > -- Sincerely yours, Mike.