From patchwork Wed Jul 26 15:07:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Weiner X-Patchwork-Id: 126473 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp497000vqo; Wed, 26 Jul 2023 08:58:44 -0700 (PDT) X-Google-Smtp-Source: APBJJlG5+N8O0aZf/m02YYgJoUT5qFiQg1x808mgyLW8k/IeCmpda7KlVcT8WodDsuFG9M9ffeWv X-Received: by 2002:a17:906:5345:b0:99b:c0f0:77fe with SMTP id j5-20020a170906534500b0099bc0f077femr1892938ejo.69.1690387123934; Wed, 26 Jul 2023 08:58:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690387123; cv=none; d=google.com; s=arc-20160816; b=ex8IGi0B+Qtr2b+4kvTyEIlF7md+4deEtmvjw9uRG8uS8IXQAwBCwcjNHCCfMdWLgE bqDSGxg0dkCEW8W4AE6Hs6o1+iS8ngWABBuRyY8UY7+iZj53LOFhufp9YWg0RDzw5yAw /mnclccm1w6UXVD7cb3EOF5tCdSd6ytF7guXo2JakmouVQ3nQsNxFtQ6rb6wuEc3l2iQ IlAB5eaTfW/8xwb4DQSsE9uJE37/QzPBAjzNR4VxcHboTyV0mV+fIuxnQQPPeic2DUJ0 BXmOFBAFjDw3AlELHoe1DeXerchttE8qW8js5tTQTn1d/xrCOt10XysBueCkM+f/c5am u80Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=MpZCUc2DN64k7aeMSMLZUCCAbrXdS1MBsuJSZ1mAze4=; fh=oEjcXLqglonowAPz4ZiE+q+8ik5gaZ7NuhQjmIMrr70=; b=j+uQq0zZyJnXdohyQ7sx0SAJxHcAOLALnwsHC/DvJfAIndkfqwYwQqAxKOIOm38E+K 5Niua9bbxqiyYiQVUz0N2iU9MBrIO48zGr6Fl+Pcqt+YqhG9PhHa6KDGMTLK1TM01z2y 2vpmL9FkRJ4MxN4pF6WRkwLcHyEpFtwutYvPHzMndASCVTVkgqGpano2UpQbZk8oOsWK jZCTWF+J9iMpotgBumZ4QupImhCsuVJirvDbeCTPTVCI14flbvfC5kG/5F1WJeExvl7m i18O9DVkahged/AlBI8bA+gHXRBrNKJSjK2VaJZx/OHdSD82sSoubv54Bkq4c9liQoMO enoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20221208.gappssmtp.com header.s=20221208 header.b=Oor1EuXr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a17-20020a1709065f9100b00992ca9eab82si10258109eju.393.2023.07.26.08.58.18; Wed, 26 Jul 2023 08:58:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@cmpxchg-org.20221208.gappssmtp.com header.s=20221208 header.b=Oor1EuXr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234646AbjGZPIl (ORCPT + 99 others); Wed, 26 Jul 2023 11:08:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233512AbjGZPIk (ORCPT ); Wed, 26 Jul 2023 11:08:40 -0400 Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 499B62119 for ; Wed, 26 Jul 2023 08:08:13 -0700 (PDT) Received: by mail-qk1-x72c.google.com with SMTP id af79cd13be357-767b6d6bb87so469337185a.2 for ; Wed, 26 Jul 2023 08:08:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20221208.gappssmtp.com; s=20221208; t=1690384027; x=1690988827; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=MpZCUc2DN64k7aeMSMLZUCCAbrXdS1MBsuJSZ1mAze4=; b=Oor1EuXr2oNsSjTJos+24ANvHw0H7E8wBKKFTXEe/8B87EVmWYgR0mM5AoXugx37Sp zpyyDyg/sexZwYtiqLmXtys2aFDmq/feJc43XUdlO+UgN5TUfMqkAzA2wqVk4/jxFEYg tpKmGp7hkje+DEiBRW1cr53CZkGUo0c5MOvhgvRGdDZYwpNUVlMFIQuOLlP5KnKGtK07 uS0gP4JQNAS8CzzQuuqBmsrOnH2KG9nfMYxpI1ai8Un4pggpXtOd53GAWipu/0QzEwNu DHDudVCBJgPbN2EqUrm9WSnSTwdAoFFlu5yU0QGjDJi6XumFB9xATAQhTclZ+m/VcaSl 3PgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690384027; x=1690988827; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=MpZCUc2DN64k7aeMSMLZUCCAbrXdS1MBsuJSZ1mAze4=; b=kiL8ea/vQD+WJ2PWcPDUUMCn9f/Ds0OvZnOEk6AsR1FAoEBkgIA3IfT+hrJpXsl4Ms ikJPr9EBB0iUoODVCI+F4eIY7qseZpuLaL40mFHRwyye8QD5XI8FnlGNC30FhvmpX+Wy 69D1tNwPBQsmbJlmwndgaR9AcrjXSl1/I2carz5pP8m2sryEpscX6KzaXwW/oIyeVycZ /NqBurLyxwNqj5LKtiyQz1hxIM2nU0HrpJCZejVkSQGTz9fRDLYBNAaiQVFp+kTjxvg5 nwGlWDcFSo+QQuYaarup7jb4SW8TXCvs0MNRLxo/bSR1V5IR48Jm/8eIWyAjVQlEt7s2 nhDw== X-Gm-Message-State: ABy/qLZvDj4lg4WdMPWDs5u3heU8mcTiP+77Hi466+fyQ+xLWQkiFWhe u8Us3F84qa3SF+36jYD2dYvKBmSGBfHsq7KbcmWjeA== X-Received: by 2002:a05:620a:4691:b0:768:1109:397 with SMTP id bq17-20020a05620a469100b0076811090397mr2429148qkb.62.1690384026957; Wed, 26 Jul 2023 08:07:06 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:ad06]) by smtp.gmail.com with ESMTPSA id cx24-20020a05620a51d800b00767c961eb47sm4438556qkb.43.2023.07.26.08.07.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jul 2023 08:07:06 -0700 (PDT) Date: Wed, 26 Jul 2023 11:07:05 -0400 From: Johannes Weiner To: Andrew Morton Cc: Vlastimil Babka , Mel Gorman , Roman Gushchin , Rik van Riel , Joonsoo Kim , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] mm: page_alloc: consume available CMA space first Message-ID: <20230726150705.GA1365610@cmpxchg.org> References: <20230726145304.1319046-1-hannes@cmpxchg.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230726145304.1319046-1-hannes@cmpxchg.org> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772498107044943977 X-GMAIL-MSGID: 1772499368947494945 On a memcache setup with heavy anon usage and no swap, we routinely see premature OOM kills with multiple gigabytes of free space left: Node 0 Normal free:4978632kB [...] free_cma:4893276kB This free space turns out to be CMA. We set CMA regions aside for potential hugetlb users on all of our machines, figuring that even if there aren't any, the memory is available to userspace allocations. When the OOMs trigger, it's from unmovable and reclaimable allocations that aren't allowed to dip into CMA. The non-CMA regions meanwhile are dominated by the anon pages. Movable pages can be migrated out of CMA when necessary, but we don't have a mechanism to migrate them *into* CMA to make room for unmovable allocations. The only recourse we have for these pages is reclaim, which due to a lack of swap is unavailable in our case. Because we have more options for CMA pages, change the policy to always fill up CMA first. This reduces the risk of premature OOMs. Signed-off-by: Johannes Weiner --- mm/page_alloc.c | 53 +++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 33 deletions(-) I realized shortly after sending the first version that the code can be further simplified by removing __rmqueue_cma_fallback() altogether. Build, boot and runtime tested that CMA is indeed used up first. diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 7d3460c7a480..b257f9651ce9 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1634,17 +1634,6 @@ static int fallbacks[MIGRATE_TYPES][MIGRATE_PCPTYPES - 1] = { [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE }, }; -#ifdef CONFIG_CMA -static __always_inline struct page *__rmqueue_cma_fallback(struct zone *zone, - unsigned int order) -{ - return __rmqueue_smallest(zone, order, MIGRATE_CMA); -} -#else -static inline struct page *__rmqueue_cma_fallback(struct zone *zone, - unsigned int order) { return NULL; } -#endif - /* * Move the free pages in a range to the freelist tail of the requested type. * Note that start_page and end_pages are not aligned on a pageblock @@ -2124,29 +2113,27 @@ __rmqueue(struct zone *zone, unsigned int order, int migratetype, { struct page *page; - if (IS_ENABLED(CONFIG_CMA)) { - /* - * Balance movable allocations between regular and CMA areas by - * allocating from CMA when over half of the zone's free memory - * is in the CMA area. - */ - 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); - if (page) - return page; - } +#ifdef CONFIG_CMA + /* + * Use up CMA first. Movable pages can be migrated out of CMA + * if necessary, but they cannot migrate into it to make room + * for unmovables elsewhere. The only recourse for them is + * then reclaim, which might be unavailable without swap. We + * want to reduce the risk of OOM with free CMA space left. + */ + if (alloc_flags & ALLOC_CMA) { + page = __rmqueue_smallest(zone, order, MIGRATE_CMA); + if (page) + return page; } -retry: - page = __rmqueue_smallest(zone, order, migratetype); - if (unlikely(!page)) { - if (alloc_flags & ALLOC_CMA) - page = __rmqueue_cma_fallback(zone, order); - - if (!page && __rmqueue_fallback(zone, order, migratetype, - alloc_flags)) - goto retry; +#endif + + for (;;) { + page = __rmqueue_smallest(zone, order, migratetype); + if (page) + break; + if (!__rmqueue_fallback(zone, order, migratetype, alloc_flags)) + break; } return page; }