From patchwork Thu Jun 25 11:31:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 11624927 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A8B5B14B7 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 6AF1A20781 for ; Thu, 25 Jun 2020 11:32:03 +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 6AF1A20781 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) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6539C6B0028; Thu, 25 Jun 2020 07:32:02 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org 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-Original-To: linux-mm@kvack.org 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-Spam-Summary: 1,0,0,4d1e781a3f55f1a4,d41d8cd98f00b204,willy@infradead.org,,RULES_HIT:41:69:355:379:541:800:960:966:973:981:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1544:1605:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2553:2559:2562:2731:2901:2903:3138:3139:3140:3141:3142:3165:3865:3866:3867:3868:3870:3871:3872:3873:3874:4250:4321:4385:4605:5007:6119:6261:6653:9592:10004:11026:11473:11658:11914:12043:12291:12296:12297:12438:12555:12895:12986:13869:13894:14130:14181:14394:14721:14877:21080:21324:21433:21451:21627:21740:21990:30005:30012:30045:30054:30069:30070:30090,0,RBL:90.155.50.34:@infradead.org:.lbl8.mailshell.net-62.8.15.100 64.201.201.201;04y8ukekukrwo6z4mzfpdyoktze6iopf4turx3i34mdokn9jfe5pgawksywjan7.yso3c671iqiitc3nu4aj98wb5s9yxdmgqyrumptw3i643kahenmj7apjnuj1ge7.c-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none 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 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) Acked-by: Michal Hocko --- 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 = container_of(work, struct xfs_btree_split_args, work); + bool is_kswapd = args->kswapd; unsigned long pflags; - unsigned long new_pflags = PF_MEMALLOC_NOFS; + int memalloc_nofs; /* * 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 |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; - - current_set_flags_nested(&pflags, new_pflags); + if (is_kswapd) + pflags = become_kswapd(); + memalloc_nofs = memalloc_nofs_save(); args->result = __xfs_btree_split(args->cur, args->level, args->ptrp, args->key, args->curp, args->stat); complete(args->done); - current_restore_flags_nested(&pflags, new_pflags); + memalloc_nofs_restore(memalloc_nofs); + if (is_kswapd) + restore_kswapd(pflags); } /* 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 int flags) } #endif +/* + * 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 = current->flags & KSWAPD_PF_FLAGS; + current->flags |= KSWAPD_PF_FLAGS; + return flags; +} + +static inline void restore_kswapd(unsigned long flags) +{ + current->flags &= ~(flags ^ KSWAPD_PF_FLAGS); +} + static inline void set_current_io_flusher(void) { current->flags |= 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); - /* - * 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 |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; + become_kswapd(); set_freezable(); WRITE_ONCE(pgdat->kswapd_order, 0); @@ -3932,8 +3920,6 @@ static int kswapd(void *p) goto kswapd_try_sleep; } - tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); - return 0; }