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.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 5691BC433E1 for ; Wed, 19 Aug 2020 03:09:08 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0220520829 for ; Wed, 19 Aug 2020 03:09:07 +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="pRNzF+FY" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0220520829 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 182DE6B006C; Tue, 18 Aug 2020 23:09:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 133E46B006E; Tue, 18 Aug 2020 23:09:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 04AA06B0070; Tue, 18 Aug 2020 23:09:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0128.hostedemail.com [216.40.44.128]) by kanga.kvack.org (Postfix) with ESMTP id E359B6B006C for ; Tue, 18 Aug 2020 23:09:06 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id A25361DF7 for ; Wed, 19 Aug 2020 03:09:06 +0000 (UTC) X-FDA: 77165836692.20.cow88_3b069cd27024 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin20.hostedemail.com (Postfix) with ESMTP id 75352180C07AB for ; Wed, 19 Aug 2020 03:09:06 +0000 (UTC) X-HE-Tag: cow88_3b069cd27024 X-Filterd-Recvd-Size: 5389 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf06.hostedemail.com (Postfix) with ESMTP for ; Wed, 19 Aug 2020 03:09:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=F5wTaICdDFqxr2lOQHsLPPgf5C2VHfmiS1ulJQG/qrA=; b=pRNzF+FYBoVBpJhZ4R4i+WnjDI 1rGRrz3LMMHXZBee7/S7chsnLOL2j7o5Jr/cV5H8htkVDLem2lYo0Lke8nmwFxDJHpKdjHDmm/Qfj vIiqSF0Bc9PicVZTRIcQlx/WT8Hw3kx6YjAQ7KtmxoUD+LxdnOq7ALqN3uSoukkBTHLfSph2A0C+u 6dh8dO6PnhRztlEwDtYhRRBnUCeKmQtXH4Xec4upjPxDBV6IZKheIh6mc8YbPn90+R0NZ9TBsPipt SAHQVYYOIcHf98RhwJx4Y9SvcQ4hxpyQSHGjuq63nJs+8tNjG+hmxf2tUE8tOLmpZWngTYxUpw/0G tvc8/y7w==; Received: from willy by casper.infradead.org with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1k8ET6-00039Y-GW; Wed, 19 Aug 2020 03:08:52 +0000 Date: Wed, 19 Aug 2020 04:08:52 +0100 From: Matthew Wilcox To: Yafang Shao Cc: david@fromorbit.com, hch@infradead.org, darrick.wong@oracle.com, mhocko@kernel.org, akpm@linux-foundation.org, linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v5 1/2] mm: Add become_kswapd and restore_kswapd Message-ID: <20200819030852.GX17456@casper.infradead.org> References: <20200819022425.25188-1-laoar.shao@gmail.com> <20200819022425.25188-2-laoar.shao@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200819022425.25188-2-laoar.shao@gmail.com> X-Rspamd-Queue-Id: 75352180C07AB X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 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: On Wed, Aug 19, 2020 at 10:24:24AM +0800, Yafang Shao wrote: > From: "Matthew Wilcox (Oracle)" > > 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. > > Cc: Dave Chinner > Cc: Christoph Hellwig > Cc: Michal Hocko > Cc: Darrick J. Wong > Cc: Matthew Wilcox > Signed-off-by: Matthew Wilcox (Oracle) > Signed-off-by: Yafang Shao See https://lore.kernel.org/linux-mm/20200625123143.GK1320@dhcp22.suse.cz/ Please add: Acked-by: Michal Hocko > +/* > + * 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). > + */ And let's change that comment as suggested by Michal (slightly edited by me): /* * Tell the memory management code that this thread is working on behalf * of background memory reclaim (like kswapd). That means that it will * get access to memory reserves should it need to allocate memory in * order to make forward progress. With this great power comes great * responsibility to not exhaust those reserves. */ > +#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); > +} > + > #ifdef CONFIG_MEMCG > /** > * memalloc_use_memcg - Starts the remote memcg charging scope. > diff --git a/mm/vmscan.c b/mm/vmscan.c > index 99e1796eb833..3a2615bfde35 100644 > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -3859,19 +3859,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); > @@ -3921,8 +3909,6 @@ static int kswapd(void *p) > goto kswapd_try_sleep; > } > > - tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD); > - > return 0; > } > > -- > 2.18.1 >