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_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 A78F9C433DF for ; Wed, 19 Aug 2020 05:50:13 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6A37320738 for ; Wed, 19 Aug 2020 05:50:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JMnBeDhb" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6A37320738 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 196D78D000A; Wed, 19 Aug 2020 01:50:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1702D8D0002; Wed, 19 Aug 2020 01:50:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0ABD08D000A; Wed, 19 Aug 2020 01:50:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0036.hostedemail.com [216.40.44.36]) by kanga.kvack.org (Postfix) with ESMTP id E8CF08D0002 for ; Wed, 19 Aug 2020 01:50:12 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id AACA9181AEF0B for ; Wed, 19 Aug 2020 05:50:12 +0000 (UTC) X-FDA: 77166242664.05.bite07_4b1343827025 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin05.hostedemail.com (Postfix) with ESMTP id 878E218021CA6 for ; Wed, 19 Aug 2020 05:50:12 +0000 (UTC) X-HE-Tag: bite07_4b1343827025 X-Filterd-Recvd-Size: 7050 Received: from mail-io1-f68.google.com (mail-io1-f68.google.com [209.85.166.68]) by imf41.hostedemail.com (Postfix) with ESMTP for ; Wed, 19 Aug 2020 05:50:11 +0000 (UTC) Received: by mail-io1-f68.google.com with SMTP id z6so23568815iow.6 for ; Tue, 18 Aug 2020 22:50:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=FWORnw1azmbSbvzLnh/tIRdOU+3S3lvKRvzW43g3p2w=; b=JMnBeDhbIEpSSRZGvurhYc+pGaAGIrbMaMl1813GA6QctAE2ylv5occLjJTYAx5ubq maARt6EJlG5pfR3dW0y4IjDxcqqS8MD333FA8+2CD0ZZLthlNgWf03Ypih7DeXB62ffv EkSzzf3R8Ku47raX3Ry0VEBCb6bctlJucbBnidY8RIaWCRP3iSFbZvaORPWQVCsHqlys ry1en3ZTKW83VLg1NF3jAX9RXmh9qk+cwz4KsIQ6t6cqbg8RNpHpABb6zD8/Undn3SX3 p+d2qvgFSdUhymwW/G0Txsr/A0bSMJ/nCRVkq61VsWVizKwuVcdslMcX6/eGzeBHPIQY dKIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=FWORnw1azmbSbvzLnh/tIRdOU+3S3lvKRvzW43g3p2w=; b=p38SUMU/hxMS4SdUW3aSyEWlKIbKnf4lk4POfli/RRP1JkCU2t16uXqKE3iPMHJSLR GEMSUq6P3vn3I5WY7APDZtCwexv4TH8Cn2EGiC7IDwQW0Qk/hYSptA/Wvi/6//LFYRIq CRJWRIlHftJSDv0Qp8vKf986qT0L3VDor662ekTsspPNX8nlsWtBlaFZJaJ42N7F9LC0 MStYrt8mqPm6jroIglWbnFAPFIS/J+urU4PXdnWLXJqZnmquQ3vJtuhI9aPnPKxyQ4lA 7+7ps4n29eYMDv3SgW9ejHxPhM20FsBWPLh5bj64LugTNEssQYdzfQiv1l6vj41bnNNh yGVg== X-Gm-Message-State: AOAM531NMt3GT0+yMdraC4LUcVFk2Sp8moMWNTlQ1+yIX/9nWY2Ao+pS I42aYWdLFQC2/Ej/xLzkCTQvTeAIQlv0IiMTLOw= X-Google-Smtp-Source: ABdhPJxs/FnoumZ+flV4yn0aL3VPpcffDP4FcVyZBIaaRWA+RRu5MB8bmikf88LxvrChlUaek0ceQ1JYbzNQvvb8SA4= X-Received: by 2002:a05:6638:22d0:: with SMTP id j16mr22572725jat.97.1597816211407; Tue, 18 Aug 2020 22:50:11 -0700 (PDT) MIME-Version: 1.0 References: <20200819022425.25188-1-laoar.shao@gmail.com> <20200819022425.25188-2-laoar.shao@gmail.com> <20200819030852.GX17456@casper.infradead.org> In-Reply-To: <20200819030852.GX17456@casper.infradead.org> From: Yafang Shao Date: Wed, 19 Aug 2020 13:49:35 +0800 Message-ID: Subject: Re: [PATCH v5 1/2] mm: Add become_kswapd and restore_kswapd To: Matthew Wilcox Cc: Dave Chinner , Christoph Hellwig , "Darrick J. Wong" , Michal Hocko , Andrew Morton , linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, Linux MM Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 878E218021CA6 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 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 11:08 AM Matthew Wilcox wrote: > > 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 > Sure. I missed that discussion. > > +/* > > + * 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. > */ > I will update it with that comment. > > +#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 > > -- Thanks Yafang