Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1370053ybt; Thu, 25 Jun 2020 04:34:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy4KXSQMO45sxAmJOmlX7HLNYLFnQaUfE3LrA17W4/oVmltG7EcG1jQsrohPcO7mBUH/KBB X-Received: by 2002:a05:6402:128f:: with SMTP id w15mr32312867edv.240.1593084858259; Thu, 25 Jun 2020 04:34:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593084858; cv=none; d=google.com; s=arc-20160816; b=Pxe1KxEdrZSPnh/qWufGcjSY+UfjZMnWGm3kL0tFf4+VwqBstJQrBxmVQuB33bXf0W 5gJRA6QV+ZQJjNoqZ2hvo+ZgkQE1Gz4EMJJugYb6waZaS0g/lslK2wXY8Bmx3c2r+D4F GVpi1jrL+LZNpeq3Y6a6NiqMksK0f+ZCYgs+X8wqJ3iKYGgaxn3F7cjRA31Fy7EGyNv9 7W0ZPz0jUxAA+K7GG+UBzreFe0MUN0jrnYzP+NOLRON4sqqYRlT3RMokfJSCDrkVd7gd veLlo+H3r+56zGVKVeSJjYciqum5bBhNuujP/ExWQFUK7gqNCGyAlWwV+zx08O6Lzffg zs+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1HLoZSacyI8SRPtt8h+SndljxFcVBZvNxY1kJjrCDnQ=; b=01iUpK95hlilQNmPdB8mGxYfZy1WyiEQPscq1mI9dtnP7mkBp5q2SyI+isQScSdttZ w/ubMHVyUgn4cwVOZTwZ6SliUNZUMkWUfTVowx02J5mSvC5hd7SmskhVm6q0vyieCurY Osz3E0yL1SaMUEbdxTN0VvJjoRwraTbm2UprH5vPKucz52fYZqRF6lDe3aHqW2Wvcu8o PKvvkbuDzrsX4/kWrG1ddcuqMRifveV3rpLHyG1es+4qFxnpoWaQrwEp3V/jDN748R78 XvazsIaOCy4T4HWtBFtw06aYPJ9rrWvXawZhCsFrB0lXNIaDfW4YBx1lbckQHi40a3oi G/BA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=casper.20170209 header.b="VfnNJg/f"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c13si7818032edw.351.2020.06.25.04.33.54; Thu, 25 Jun 2020 04:34:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=casper.20170209 header.b="VfnNJg/f"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404389AbgFYLcO (ORCPT + 99 others); Thu, 25 Jun 2020 07:32:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404274AbgFYLcN (ORCPT ); Thu, 25 Jun 2020 07:32:13 -0400 Received: from casper.infradead.org (unknown [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE48EC0613ED; Thu, 25 Jun 2020 04:32:12 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 = 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; } -- 2.27.0