From patchwork Tue Feb 6 21:50:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 13547801 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 27328C4829A for ; Tue, 6 Feb 2024 21:50:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 165936B0078; Tue, 6 Feb 2024 16:50:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C7036B007E; Tue, 6 Feb 2024 16:50:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E15D78D0002; Tue, 6 Feb 2024 16:50:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id BC7BA6B0078 for ; Tue, 6 Feb 2024 16:50:37 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8D618A058A for ; Tue, 6 Feb 2024 21:50:37 +0000 (UTC) X-FDA: 81762723714.03.3523840 Received: from out-175.mta0.migadu.com (out-175.mta0.migadu.com [91.218.175.175]) by imf02.hostedemail.com (Postfix) with ESMTP id E35EB80006 for ; Tue, 6 Feb 2024 21:50:34 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=oQ5zAB3t; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf02.hostedemail.com: domain of kent.overstreet@linux.dev designates 91.218.175.175 as permitted sender) smtp.mailfrom=kent.overstreet@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707256235; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PMnu4dKfXWpdSsa89ceamn3rfdEYh+qeIqCY1GsWG3s=; b=bi5Gb1JF1NTu5uDtzkov/beJ6qKBxag496u6zeyd01q/3uX2nXaQkMLT7KU5peWuaeEl70 BoCreMtTvsee09yaKYq58rTARVY8B4GitvG+FU3dkiKHEGFr0qZPviniivrcB7VKEcdxAl AxNv5JTl8DrIDHE3w3uzdeVoyJ4kF/4= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=oQ5zAB3t; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf02.hostedemail.com: domain of kent.overstreet@linux.dev designates 91.218.175.175 as permitted sender) smtp.mailfrom=kent.overstreet@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707256235; a=rsa-sha256; cv=none; b=GuJpyr1MPDuHGWmN7N7Iai/ECCuSZN0+R2j+P/0mok8XXO8S5cMM61y6obWzDNXjVUmN+n FGnDWyQFo9WjrggzFY+RmOZiRAIH3/c4nPWk38Xo5l6Sfy9/UA9TUDXzeHePm+9HHdKwc6 ZATu69nqHt961+CKoF0nUZqHGhgFnSY= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1707256233; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PMnu4dKfXWpdSsa89ceamn3rfdEYh+qeIqCY1GsWG3s=; b=oQ5zAB3taD+tTxyVw3ipX7rxGNX/XcZzEzLjTlWnsB44TnnNwKJrkeG9lJftcBT3ARlQrj 49akRKF7lx7OsTsEMkE/jD0vaJE0VXQlWs8xsxMbOzaUsujG9bw0jfinyefXlxPleCWtV+ 5mGygACeGMCme5Jeb/Jd3GuJmfiQWYU= From: Kent Overstreet To: linux-mm@kvack.org Cc: Kent Overstreet , Vlastimil Babka , Matthew Wilcox , Michal Hocko , "Darrick J . Wong" Subject: [PATCH 1/3] mm: introduce memalloc_flags_{save,restore} Date: Tue, 6 Feb 2024 16:50:14 -0500 Message-ID: <20240206215016.961253-2-kent.overstreet@linux.dev> In-Reply-To: <20240206215016.961253-1-kent.overstreet@linux.dev> References: <20240206215016.961253-1-kent.overstreet@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: E35EB80006 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ytdtgkeh6j8aza1rux3gethb1fprrw5c X-HE-Tag: 1707256234-321207 X-HE-Meta: U2FsdGVkX19rGWInyfDFeGDbzshpumC4+MN1Oglz2kg34oy9EeTv92jjQMJt8BzEI0J3G4zKjhdokYAyI5olPeieEHesxkY7+fDT8wZ7N2W/Trrx37G+oX+NofH5+c4rtREwLprRinw0tAoqjewvMxeAWyfK428udaUNyCbUBW0APiPTqfGvMxmjj9vs/q1SZZ6pZmDYCp8etrpVPRtmVvOg2cvo/KQVD/21m6SdbxK8bHIMhWOXgQJ6WuH8SKNxivSZ7mTK6lnvwp/lguu/ly83aMPABzS6zrnGWtAhyYCf0TzyRINBwatpTw8PFW+qnd/2JsVB/Mf0Jj7ufBMKgdZ4KVYeoLhR3VoxXJmib2gahwCcxq2B6xS3sux6RCDHtfbJEfdHCQ757d9DZmc6rYPtN/pX3B/v3ohqIkWVke5PAUCDceEW52EvWhwcwMytYUnN1GfCoctXT3vl6a74eyj+9nC2v7r8CIWEJoC2BP3mCRAmEEI4fAePYsB9FAss712CpYrMP5NiOvjTdj1k0EIdKPPRWncSb6nR+s6C+uQ/flfzUE6rpIKDXDf7y93w+3XEh0t8tzOf0vJPGgzI/cxWmEJzJ5BfmyMVtlIVtvgMTf86LsjwDJY0F4YNmkrbhAEVkSzOO6dgu23sfH4PC6MZkAnVE16xDAn18lxTlOOFp835KTmWrnkeJuYYRVi1o3BLEHV+d6e/qXo3OBiQvrZIqZYeDYVJRBCA7TiVQPJEfesgwUsTr1zzXzc7mdy2NZhVOGLnj/VCGoEtgn1E1G5G7q3bnHKx3MCLdQdAwsNUi7TJ1Lzhdok670o0Di98kvmd428YUJ6Cp5dwwepd5ZLO6WPm2W50RdkztMVJs4awK4Ghr6TSG15fHvgABbGC/dCcFLLD1OHeDpkGizOqQvVMLvW5lQ4D04yqWD3LUtfLGS5pl39cxXcacjlVZ0Gtnf1JGA56V8QgXqAMg8i enveeSmw A9+HAEzqjHYqMIFnG8bYKZi5pWvyr32GQaiSc+H2VXhTjqW6IDgPKc4NyEyYdPQQvhi7ScMrO7qRnb4K9SCfzeOegSyqMdlWq3SDYEecTnGet09NgM7jcLr4XnpEltv55w69MkmZWNOSwG6h92pU6/0PPVBzjJ04wSRAzWFTAkQxpTIAKnyuF8rX1UqePu375n+/brrz+znzwjRsPjdagKXvjeqIDM9ITpin0zEcWkHf866ti48I32fzurKQ8YO+FDDwpDWaG+vlE7xkzVL5Y0zDL/nw3L821/32l3yn+mAejZ+qVc78QHu2puw== 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: Our proliferation of memalloc_*_{save,restore} APIs is getting a bit silly, this adds a generic version and converts the existing save/restore functions to wrappers. Signed-off-by: Kent Overstreet Cc: Vlastimil Babka Cc: Matthew Wilcox Cc: Michal Hocko Cc: Darrick J. Wong Cc: linux-mm@kvack.org Acked-by: Vlastimil Babka --- include/linux/sched/mm.h | 43 ++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index 9a19f1b42f64..f00d7ecc2adf 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -306,6 +306,24 @@ static inline void might_alloc(gfp_t gfp_mask) might_sleep_if(gfpflags_allow_blocking(gfp_mask)); } +/** + * memalloc_flags_save - Add a PF_* flag to current->flags, save old value + * + * This allows PF_* flags to be conveniently added, irrespective of current + * value, and then the old version restored with memalloc_flags_restore(). + */ +static inline unsigned memalloc_flags_save(unsigned flags) +{ + unsigned oldflags = ~current->flags & flags; + current->flags |= flags; + return oldflags; +} + +static inline void memalloc_flags_restore(unsigned flags) +{ + current->flags &= ~flags; +} + /** * memalloc_noio_save - Marks implicit GFP_NOIO allocation scope. * @@ -319,9 +337,7 @@ static inline void might_alloc(gfp_t gfp_mask) */ static inline unsigned int memalloc_noio_save(void) { - unsigned int flags = current->flags & PF_MEMALLOC_NOIO; - current->flags |= PF_MEMALLOC_NOIO; - return flags; + return memalloc_flags_save(PF_MEMALLOC_NOIO); } /** @@ -334,7 +350,7 @@ static inline unsigned int memalloc_noio_save(void) */ static inline void memalloc_noio_restore(unsigned int flags) { - current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags; + memalloc_flags_restore(flags); } /** @@ -350,9 +366,7 @@ static inline void memalloc_noio_restore(unsigned int flags) */ static inline unsigned int memalloc_nofs_save(void) { - unsigned int flags = current->flags & PF_MEMALLOC_NOFS; - current->flags |= PF_MEMALLOC_NOFS; - return flags; + return memalloc_flags_save(PF_MEMALLOC_NOFS); } /** @@ -365,32 +379,27 @@ static inline unsigned int memalloc_nofs_save(void) */ static inline void memalloc_nofs_restore(unsigned int flags) { - current->flags = (current->flags & ~PF_MEMALLOC_NOFS) | flags; + memalloc_flags_restore(flags); } static inline unsigned int memalloc_noreclaim_save(void) { - unsigned int flags = current->flags & PF_MEMALLOC; - current->flags |= PF_MEMALLOC; - return flags; + return memalloc_flags_save(PF_MEMALLOC); } static inline void memalloc_noreclaim_restore(unsigned int flags) { - current->flags = (current->flags & ~PF_MEMALLOC) | flags; + memalloc_flags_restore(flags); } static inline unsigned int memalloc_pin_save(void) { - unsigned int flags = current->flags & PF_MEMALLOC_PIN; - - current->flags |= PF_MEMALLOC_PIN; - return flags; + return memalloc_flags_save(PF_MEMALLOC_PIN); } static inline void memalloc_pin_restore(unsigned int flags) { - current->flags = (current->flags & ~PF_MEMALLOC_PIN) | flags; + memalloc_flags_restore(flags); } #ifdef CONFIG_MEMCG