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 X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0307EC433E0 for ; Thu, 25 Jun 2020 11:32:03 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BF54B20781 for ; Thu, 25 Jun 2020 11:32:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="VfnNJg/f" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BF54B20781 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6A4B76B0027; Thu, 25 Jun 2020 07:32:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6539C6B0028; Thu, 25 Jun 2020 07:32:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5691B6B0029; Thu, 25 Jun 2020 07:32:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0090.hostedemail.com [216.40.44.90]) by kanga.kvack.org (Postfix) with ESMTP id 3C01D6B0027 for ; Thu, 25 Jun 2020 07:32:02 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id F10D11EE6 for ; Thu, 25 Jun 2020 11:32:01 +0000 (UTC) X-FDA: 76967520042.27.snail64_031196826e4c Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin27.hostedemail.com (Postfix) with ESMTP id B6DDC3D669 for ; Thu, 25 Jun 2020 11:32:01 +0000 (UTC) X-HE-Tag: snail64_031196826e4c X-Filterd-Recvd-Size: 5934 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf14.hostedemail.com (Postfix) with ESMTP for ; Thu, 25 Jun 2020 11:32:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=1HLoZSacyI8SRPtt8h+SndljxFcVBZvNxY1kJjrCDnQ=; b=VfnNJg/fQFw02jR0S1ZX1voZqB S+rlS7Po7AVxOoJEZ0Rhn0fPCvOZVPJX12GDmWY1QWvEbdWKfNf52qajSWr65FqoMjQ6blVMlRy+Y Ftf5aGhMSjJQLwjiBlb7jkjSUBjGQcZnmGKawWT/gTmBBRHEHBJTopVFCwScipNrzi2KqMXFcHYjJ zb3Wi28CaHKfkXHoM0IKsC92VwtyGQ/cw8tbv/soZFDv4Lb1gIoOpRU5DvZJjAMKRdz6OUJF0rk2Z SkOd+J7mbTCuuyIFP2ZtaFcZw40mdSh0BbgCx9dhDqTBzdCc9dYB2z9QWV+/YvIlZ2oWyRImZDyN0 hWFICJ9Q==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1joQ6W-0001zS-Rp; Thu, 25 Jun 2020 11:31:40 +0000 From: "Matthew Wilcox (Oracle)" To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: "Matthew Wilcox (Oracle)" , linux-xfs@vger.kernel.org, dm-devel@redhat.com, Mikulas Patocka , Jens Axboe , NeilBrown Subject: [PATCH 2/6] mm: Add become_kswapd and restore_kswapd Date: Thu, 25 Jun 2020 12:31:18 +0100 Message-Id: <20200625113122.7540-3-willy@infradead.org> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20200625113122.7540-1-willy@infradead.org> References: <20200625113122.7540-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: B6DDC3D669 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 Content-Transfer-Encoding: quoted-printable 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: Since XFS needs to pretend to be kswapd in some of its worker threads, create methods to save & restore kswapd state. Don't bother restoring kswapd state in kswapd -- the only time we reach this code is when we're exiting and the task_struct is about to be destroyed anyway. Signed-off-by: Matthew Wilcox (Oracle) --- fs/xfs/libxfs/xfs_btree.c | 14 ++++++++------ include/linux/sched/mm.h | 26 ++++++++++++++++++++++++++ mm/vmscan.c | 16 +--------------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/fs/xfs/libxfs/xfs_btree.c b/fs/xfs/libxfs/xfs_btree.c index 2d25bab68764..a04a44238aab 100644 --- a/fs/xfs/libxfs/xfs_btree.c +++ b/fs/xfs/libxfs/xfs_btree.c @@ -2813,8 +2813,9 @@ xfs_btree_split_worker( { struct xfs_btree_split_args *args =3D container_of(work, struct xfs_btree_split_args, work); + bool is_kswapd =3D args->kswapd; unsigned long pflags; - unsigned long new_pflags =3D PF_MEMALLOC_NOFS; + int memalloc_nofs; =20 /* * we are in a transaction context here, but may also be doing work @@ -2822,16 +2823,17 @@ xfs_btree_split_worker( * temporarily to ensure that we don't block waiting for memory reclaim * in any way. */ - if (args->kswapd) - new_pflags |=3D PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; - - current_set_flags_nested(&pflags, new_pflags); + if (is_kswapd) + pflags =3D become_kswapd(); + memalloc_nofs =3D memalloc_nofs_save(); =20 args->result =3D __xfs_btree_split(args->cur, args->level, args->ptrp, args->key, args->curp, args->stat); complete(args->done); =20 - current_restore_flags_nested(&pflags, new_pflags); + memalloc_nofs_restore(memalloc_nofs); + if (is_kswapd) + restore_kswapd(pflags); } =20 /* diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index 1a7e1ab1be85..b0089eadc367 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -308,6 +308,32 @@ static inline void memalloc_nocma_restore(unsigned i= nt flags) } #endif =20 +/* + * Tell the memory management that we're a "memory allocator", + * and that if we need more memory we should get access to it + * regardless (see "__alloc_pages()"). "kswapd" should + * never get caught in the normal page freeing logic. + * + * (Kswapd normally doesn't need memory anyway, but sometimes + * you need a small amount of memory in order to be able to + * page out something else, and this flag essentially protects + * us from recursively trying to free more memory as we're + * trying to free the first piece of memory in the first place). + */ +#define KSWAPD_PF_FLAGS (PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD) + +static inline unsigned long become_kswapd(void) +{ + unsigned long flags =3D current->flags & KSWAPD_PF_FLAGS; + current->flags |=3D KSWAPD_PF_FLAGS; + return flags; +} + +static inline void restore_kswapd(unsigned long flags) +{ + current->flags &=3D ~(flags ^ KSWAPD_PF_FLAGS); +} + static inline void set_current_io_flusher(void) { current->flags |=3D PF_LOCAL_THROTTLE; diff --git a/mm/vmscan.c b/mm/vmscan.c index b6d84326bdf2..27ae76699899 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3870,19 +3870,7 @@ static int kswapd(void *p) if (!cpumask_empty(cpumask)) set_cpus_allowed_ptr(tsk, cpumask); =20 - /* - * Tell the memory management that we're a "memory allocator", - * and that if we need more memory we should get access to it - * regardless (see "__alloc_pages()"). "kswapd" should - * never get caught in the normal page freeing logic. - * - * (Kswapd normally doesn't need memory anyway, but sometimes - * you need a small amount of memory in order to be able to - * page out something else, and this flag essentially protects - * us from recursively trying to free more memory as we're - * trying to free the first piece of memory in the first place). - */ - tsk->flags |=3D PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; + become_kswapd(); set_freezable(); =20 WRITE_ONCE(pgdat->kswapd_order, 0); @@ -3932,8 +3920,6 @@ static int kswapd(void *p) goto kswapd_try_sleep; } =20 - tsk->flags &=3D ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); - return 0; } =20 --=20 2.27.0