linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Levi Yun <ppbuk5246@gmail.com>
To: akpm@linux-foundation.org
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Levi Yun <ppbuk5246@gmail.com>
Subject: [PATCH] mm/cma: Fix potential memory loss on cma_declare_contiguous_nid
Date: Wed, 18 Jan 2023 17:05:23 +0900	[thread overview]
Message-ID: <20230118080523.44522-1-ppbuk5246@gmail.com> (raw)

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 <ppbuk5246@gmail.com>
---
 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


             reply	other threads:[~2023-01-18  8:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-18  8:05 Levi Yun [this message]
2023-01-18 21:40 ` Andrew Morton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230118080523.44522-1-ppbuk5246@gmail.com \
    --to=ppbuk5246@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox