From patchwork Thu Jul 23 01:49:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joonsoo Kim X-Patchwork-Id: 11679529 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 0127713A4 for ; Thu, 23 Jul 2020 01:49:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B7BEC22CA0 for ; Thu, 23 Jul 2020 01:49:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MBBm7WTU" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B7BEC22CA0 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 C35CC6B0002; Wed, 22 Jul 2020 21:49:22 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BE5046B0005; Wed, 22 Jul 2020 21:49:22 -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 AAB8F6B0006; Wed, 22 Jul 2020 21:49:22 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0201.hostedemail.com [216.40.44.201]) by kanga.kvack.org (Postfix) with ESMTP id 8FCB06B0002 for ; Wed, 22 Jul 2020 21:49:22 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 2C3951802344C for ; Thu, 23 Jul 2020 01:49:22 +0000 (UTC) X-FDA: 77067658164.12.tray03_01043c526f3a Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id F1C471813F58E for ; Thu, 23 Jul 2020 01:49:21 +0000 (UTC) X-Spam-Summary: 1,0,0,70040a735d276367,d41d8cd98f00b204,js1304@gmail.com,,RULES_HIT:2:41:69:355:379:541:800:960:966:973:981:988:989:1260:1345:1437:1535:1605:1606:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2731:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3874:4119:4250:4321:4385:4605:5007:6119:6261:6653:6742:7576:7903:9413:10004:11026:11473:11658:11914:12043:12114:12291:12296:12297:12438:12517:12519:12555:12679:12683:12895:13161:13215:13229:14096:14130:14394:21080:21220:21433:21444:21451:21627:21666:21740:21795:21990:30051:30054:30069:30070,0,RBL:209.85.210.196:@gmail.com:.lbl8.mailshell.net-66.100.201.100 62.50.0.100;04y8khg47hmt1t48xb9odak1p4f4gypsnthj6rb9as6d4dzmjqk6n37ry1ed9g5.bwdtxqyrtq74h5adw9bnyko1588nee5z3m6ynqnojtdm7wcjzo46hucf16gqiu3.o-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:25,LUA_SUMMARY:none X-HE-Tag: tray03_01043c526f3a X-Filterd-Recvd-Size: 8347 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by imf25.hostedemail.com (Postfix) with ESMTP for ; Thu, 23 Jul 2020 01:49:21 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id a24so2196818pfc.10 for ; Wed, 22 Jul 2020 18:49:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=X9S60bF0x51rzQ7ntN8JyfNKAz6dUEkcZzUI5TDgG14=; b=MBBm7WTUiiOuCsPTJp5qo0K1GbLaB89bzPkBXSPYX/unc9HQ6lER8kiS0jp1MhztU0 IGN3vx1Wtj5GtUzWgR+NiFfNfTbshxdpZk623F22NIU0FdVrhauvGfOfIZ4iM14wBLi/ ocKCOfdAJHSYWDqlOX5JF7S/tvHw4/2m2l0oUNKN1+znt3lJ+P3MgnKi8yXz0YTME93X oxLjlaYKDNqA1+MkcRzq2V/tn4f6dNanDlqYz51YdM7K2FeiSC5I6kKvWu0GtLhzZGn4 MbmNNotScBjEn5KbFVe/YgoRf3samtMhzDh0v43EiYhDtHeM/vS4Em7EQdtedgbZbVR0 JnTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=X9S60bF0x51rzQ7ntN8JyfNKAz6dUEkcZzUI5TDgG14=; b=UgLaWbZ7wSO7FB/AOUdvO+0nhZDrz4rSI7NxDDu/ARvOXyJlRGi7Dh8918gw5W3vM0 KTnAS8KVPXIoAzXfgOhgX/DvG1DhtNY5gWSHqcTuUanURTdYj4bDEx3SURNGjrqK8ucy s0GPsXnn9X0g3LuC7bn/mLL6gBtMYaF/42PHMhZAtWU4/JqQs5g0wbP4RRtilH7pbpNR 07zQVx1Da4A6VHqBU085PLZTI3BNc+Co7W94EYRcAM1XXhvcQhxrlardTaQ9ENbyGmAm /HU4tjXKY4bJSF753L1WqKCxGUh4EzKVi47DDnc5WUV9QJvBDDUKYbWWlPZ18EaJ6n6A HZFQ== X-Gm-Message-State: AOAM533Ho+bOfgi+r+y5sJ5llyosqNb+hkiphYtiTZ3Jymz55JsWxI1f IehQj5CSbwZ0aBlWhgBEYX8= X-Google-Smtp-Source: ABdhPJxw8gNAXkDEm7OnN1Hxgg9MIjdh0ViCYejDRxXLj8oyxSUEvvLIef7rU4EjsH8L63vVeweoKA== X-Received: by 2002:a62:7650:: with SMTP id r77mr2058875pfc.235.1595468960492; Wed, 22 Jul 2020 18:49:20 -0700 (PDT) Received: from localhost.localdomain ([114.206.198.176]) by smtp.gmail.com with ESMTPSA id e18sm887210pff.37.2020.07.22.18.49.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Jul 2020 18:49:20 -0700 (PDT) From: js1304@gmail.com X-Google-Original-From: iamjoonsoo.kim@lge.com To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@lge.com, Vlastimil Babka , Christoph Hellwig , Roman Gushchin , Mike Kravetz , Naoya Horiguchi , Michal Hocko , "Aneesh Kumar K . V" , Joonsoo Kim , stable@vger.kernel.org Subject: [PATCH v2] mm/page_alloc: fix memalloc_nocma_{save/restore} APIs Date: Thu, 23 Jul 2020 10:49:02 +0900 Message-Id: <1595468942-29687-1-git-send-email-iamjoonsoo.kim@lge.com> X-Mailer: git-send-email 2.7.4 X-Rspamd-Queue-Id: F1C471813F58E 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: From: Joonsoo Kim Currently, memalloc_nocma_{save/restore} API that prevents CMA area in page allocation is implemented by using current_gfp_context(). However, there are two problems of this implementation. First, this doesn't work for allocation fastpath. In the fastpath, original gfp_mask is used since current_gfp_context() is introduced in order to control reclaim and it is on slowpath. So, CMA area can be allocated through the allocation fastpath even if memalloc_nocma_{save/restore} APIs are used. Currently, there is just one user for these APIs and it has a fallback method to prevent actual problem. Second, clearing __GFP_MOVABLE in current_gfp_context() has a side effect to exclude the memory on the ZONE_MOVABLE for allocation target. To fix these problems, this patch changes the implementation to exclude CMA area in page allocation. Main point of this change is using the alloc_flags. alloc_flags is mainly used to control allocation so it fits for excluding CMA area in allocation. Fixes: d7fefcc8de91 (mm/cma: add PF flag to force non cma alloc) Cc: Reviewed-by: Vlastimil Babka Signed-off-by: Joonsoo Kim --- include/linux/sched/mm.h | 8 +------- mm/page_alloc.c | 31 +++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index 480a4d1..17e0c31 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -177,12 +177,10 @@ static inline bool in_vfork(struct task_struct *tsk) * Applies per-task gfp context to the given allocation flags. * PF_MEMALLOC_NOIO implies GFP_NOIO * PF_MEMALLOC_NOFS implies GFP_NOFS - * PF_MEMALLOC_NOCMA implies no allocation from CMA region. */ static inline gfp_t current_gfp_context(gfp_t flags) { - if (unlikely(current->flags & - (PF_MEMALLOC_NOIO | PF_MEMALLOC_NOFS | PF_MEMALLOC_NOCMA))) { + if (unlikely(current->flags & (PF_MEMALLOC_NOIO | PF_MEMALLOC_NOFS))) { /* * NOIO implies both NOIO and NOFS and it is a weaker context * so always make sure it makes precedence @@ -191,10 +189,6 @@ static inline gfp_t current_gfp_context(gfp_t flags) flags &= ~(__GFP_IO | __GFP_FS); else if (current->flags & PF_MEMALLOC_NOFS) flags &= ~__GFP_FS; -#ifdef CONFIG_CMA - if (current->flags & PF_MEMALLOC_NOCMA) - flags &= ~__GFP_MOVABLE; -#endif } return flags; } diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e028b87c..7336e94 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -2790,7 +2790,7 @@ __rmqueue(struct zone *zone, unsigned int order, int migratetype, * allocating from CMA when over half of the zone's free memory * is in the CMA area. */ - if (migratetype == MIGRATE_MOVABLE && + if (alloc_flags & ALLOC_CMA && zone_page_state(zone, NR_FREE_CMA_PAGES) > zone_page_state(zone, NR_FREE_PAGES) / 2) { page = __rmqueue_cma_fallback(zone, order); @@ -2801,7 +2801,7 @@ __rmqueue(struct zone *zone, unsigned int order, int migratetype, retry: page = __rmqueue_smallest(zone, order, migratetype); if (unlikely(!page)) { - if (migratetype == MIGRATE_MOVABLE) + if (alloc_flags & ALLOC_CMA) page = __rmqueue_cma_fallback(zone, order); if (!page && __rmqueue_fallback(zone, order, migratetype, @@ -3671,6 +3671,20 @@ alloc_flags_nofragment(struct zone *zone, gfp_t gfp_mask) return alloc_flags; } +static inline unsigned int current_alloc_flags(gfp_t gfp_mask, + unsigned int alloc_flags) +{ +#ifdef CONFIG_CMA + unsigned int pflags = current->flags; + + if (!(pflags & PF_MEMALLOC_NOCMA) && + gfp_migratetype(gfp_mask) == MIGRATE_MOVABLE) + alloc_flags |= ALLOC_CMA; + +#endif + return alloc_flags; +} + /* * get_page_from_freelist goes through the zonelist trying to allocate * a page. @@ -4316,10 +4330,8 @@ gfp_to_alloc_flags(gfp_t gfp_mask) } else if (unlikely(rt_task(current)) && !in_interrupt()) alloc_flags |= ALLOC_HARDER; -#ifdef CONFIG_CMA - if (gfp_migratetype(gfp_mask) == MIGRATE_MOVABLE) - alloc_flags |= ALLOC_CMA; -#endif + alloc_flags = current_alloc_flags(gfp_mask, alloc_flags); + return alloc_flags; } @@ -4620,7 +4632,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, reserve_flags = __gfp_pfmemalloc_flags(gfp_mask); if (reserve_flags) - alloc_flags = reserve_flags; + alloc_flags = current_alloc_flags(gfp_mask, reserve_flags); /* * Reset the nodemask and zonelist iterators if memory policies can be @@ -4697,7 +4709,7 @@ __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, /* Avoid allocations with no watermarks from looping endlessly */ if (tsk_is_oom_victim(current) && - (alloc_flags == ALLOC_OOM || + (alloc_flags & ALLOC_OOM || (gfp_mask & __GFP_NOMEMALLOC))) goto nopage; @@ -4785,8 +4797,7 @@ static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, if (should_fail_alloc_page(gfp_mask, order)) return false; - if (IS_ENABLED(CONFIG_CMA) && ac->migratetype == MIGRATE_MOVABLE) - *alloc_flags |= ALLOC_CMA; + *alloc_flags = current_alloc_flags(gfp_mask, *alloc_flags); return true; }