Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp6194425rwb; Wed, 18 Jan 2023 02:18:15 -0800 (PST) X-Google-Smtp-Source: AMrXdXsn3iTfQHiEWlUYvujswixTugcW4YcZNEmv8UPlHYWD3bdi8dO+xkXRQGXcuN8iBzGCCqQ2 X-Received: by 2002:a17:90a:67c2:b0:226:c364:2d1d with SMTP id g2-20020a17090a67c200b00226c3642d1dmr6626884pjm.41.1674037095563; Wed, 18 Jan 2023 02:18:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674037095; cv=none; d=google.com; s=arc-20160816; b=PoaIPxCrgALsBYGbscI9jv35oWOJam82G4jNSh81Qc4IBt3c0/cZobYNymOODij0qq +xucb/eC6/jT0QO521uVOpq0zFFW7Z690As78Wx3B340OVjR5ii02pLEJDnqidbrRmtz pIUIu+zsclnavBuRSn2GjMCKQfNbKqwD8ffujhJ3AlzyifcxgjaZ01ntm5kpx+KAKGTZ y8B9sFWS3Wj9Uf/ou5cfRRi21b3Vi3uXtlHDTZaiOTMFtNGbl0FOdRGv+j5JRSoXXfYz aFvbqG/CuUkp37ktcEQfOV2FaW0xps8/XE1A4vgY+sG8JxIppFJuRVSYYdnAuYf77vVu D9OQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=SfvGmMtFJjRRSAyhf9LFXYJKD8Rnq768IEyE92RvJw0=; b=XxjoxsWWpIOZTZGT58qHUNShYgW/Fcx9XnfqEAyRnWuP5YCNCIvb0YPYRy4onONMA9 wbOHiTvJEeUHEtFAxN4tuz1JsmrvykCu2atHT+ykQ5xlcuihXRPb2gsWa7FX9MNHV9Ps 1MRxcbqxCt12Y9XmbSyTvaWRTMHofCuq6R+KwS+wCVei5ZExRztjhaljFns5rkhaW8Ex 7xJziChSx8PB/K/dH8C9f7uNLHhvloSDPmQeyQr8a91Gl4tUYLySYAFfhe5Jco2cVF3L 2lYe7QPFAsIuBOtNf2l3ZQse71lcSrXLld7RDNHU6pl1+SOg9uxbwrl4et44UfE5vzYO zpQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=E9VQ8Fox; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ip6-20020a17090b314600b002290cb1ca2csi1846331pjb.177.2023.01.18.02.18.08; Wed, 18 Jan 2023 02:18:15 -0800 (PST) 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=@gmail.com header.s=20210112 header.b=E9VQ8Fox; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229537AbjARIx1 (ORCPT + 45 others); Wed, 18 Jan 2023 03:53:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229602AbjARIwe (ORCPT ); Wed, 18 Jan 2023 03:52:34 -0500 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D32B553F8D for ; Wed, 18 Jan 2023 00:05:36 -0800 (PST) Received: by mail-pj1-x1030.google.com with SMTP id dw9so33556454pjb.5 for ; Wed, 18 Jan 2023 00:05:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=SfvGmMtFJjRRSAyhf9LFXYJKD8Rnq768IEyE92RvJw0=; b=E9VQ8FoxNSUrQG0Ly8zk8LSOYx2YwXs5F9WrtNhP7H1MeG+L3NyPfbaDSVccJ0lWGz RcAd/w4VRWGyQFpV/QPZVU0TlLju/hnt90mC8UFcS01K0hDeOEN6LXWGge/8nm3yzp7B JCFWXpYqI95zBsSMn4rh80lqcZDIe0dFIBig+Qj3Q703Q3jGVFosiZY+c1fSL06SvJCN YNccXl6lbEewgH/XIEHJ6gsuqK9lzoziCY3koDuKZNMM0qMSV1CJvhQjD0ahh2NWFsPK JmZA3RkP9Wf8eRke+GhlXKnht2oU/FiEcA9Tfy3//kX43lRB8WrI1FDjB+9mj0oUz9nF 73+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=SfvGmMtFJjRRSAyhf9LFXYJKD8Rnq768IEyE92RvJw0=; b=T619zVJMt3U37tvZspG9umayeCkxSRJCxo+YFnFf1dTbKYSb25HjDksM3FyjvqHBDE tRfMzd/YOGEF5wq8IlWCpRZKM7kEMswVWew2qgZbSJrr2cs+IAVvOglCwePs1RNbBr/A h6OHDvYcUDSrpzZE+6H+FlYmdj6vhyXyclgsv3NlI6hrF8bW/nH/FQmjt/Yn3c28jNHR 9DxYby5Gp3trckJbxt5t7ludirv7Jw3EmmdqNz6MjHG+OSaySw1CryJVeTVjKUeGRt/u 5gJZQVLafTvL/mqXRFcg3XOWE/nqfs7NQzZ/7+y7r+qVCMknn/qnF900VXXHtUDrkNut v79w== X-Gm-Message-State: AFqh2kpw7yuJp9r5AIDDT3Qk2Sop2iQaAgO0JVl3zlvk/4VZp/kwb8sL 8Yzo2xTUXmKAFLLuUduvigw= X-Received: by 2002:a17:902:ccc9:b0:194:749a:1e78 with SMTP id z9-20020a170902ccc900b00194749a1e78mr7136533ple.40.1674029136134; Wed, 18 Jan 2023 00:05:36 -0800 (PST) Received: from localhost.localdomain ([2001:e60:8009:708:f1fc:e9cc:7685:9c2c]) by smtp.gmail.com with ESMTPSA id d11-20020a170902654b00b001895d87225csm22359430pln.182.2023.01.18.00.05.34 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 18 Jan 2023 00:05:35 -0800 (PST) From: Levi Yun To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Levi Yun Subject: [PATCH] mm/cma: Fix potential memory loss on cma_declare_contiguous_nid Date: Wed, 18 Jan 2023 17:05:23 +0900 Message-Id: <20230118080523.44522-1-ppbuk5246@gmail.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 Suppose, memblock_alloc_ranged_nid with highmem_start is success when cma_declare_contiguous_nid is called with !fixed in the system 32-bit system with PHYS_ADDR_T_64BIT enabled with memblock.bottom_up == false. But, Next trial to memblock_alloc_range_nid to allocate in [SIZE_4G, limits) nullifies former success allocated addr and it retries to memblock_alloc_ragne_nid again. In this situation, first success address area is lost. By changing order of allocation (SIZE_4G, high_memory and base) and checking whether allocated prevents potential memory lost. Signed-off-by: Levi Yun --- mm/cma.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mm/cma.c b/mm/cma.c index 4a978e09547a..49d9b08d9a47 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -321,18 +321,6 @@ int __init cma_declare_contiguous_nid(phys_addr_t base, } else { phys_addr_t addr = 0; - /* - * All pages in the reserved area must come from the same zone. - * If the requested region crosses the low/high memory boundary, - * try allocating from high memory first and fall back to low - * memory in case of failure. - */ - if (base < highmem_start && limit > highmem_start) { - addr = memblock_alloc_range_nid(size, alignment, - highmem_start, limit, nid, true); - limit = highmem_start; - } - /* * If there is enough memory, try a bottom-up allocation first. * It will place the new cma area close to the start of the node @@ -350,6 +338,18 @@ int __init cma_declare_contiguous_nid(phys_addr_t base, } #endif + /* + * All pages in the reserved area must come from the same zone. + * If the requested region crosses the low/high memory boundary, + * try allocating from high memory first and fall back to low + * memory in case of failure. + */ + if (!addr && base < highmem_start && limit > highmem_start) { + addr = memblock_alloc_range_nid(size, alignment, + highmem_start, limit, nid, true); + limit = highmem_start; + } + if (!addr) { addr = memblock_alloc_range_nid(size, alignment, base, limit, nid, true); -- 2.35.1