2016-12-11 13:01:33

by Wei Yang

[permalink] [raw]
Subject: [PATCH 0/2] mm/memblock.c: fix potential bug and code refine

Here are two patch of mm/memblock.c.
[1]. A trivial code refine in memblock_is_region_memory(), which removes an
unnecessary check on base address.
[2]. The original code forgets to check the return value of
memblock_reserve(), which may lead to potential problem. The patch fix this.

Wei Yang (2):
mm/memblock.c: trivial code refine in memblock_is_region_memory()
mm/memblock.c: check return value of memblock_reserve() in
memblock_virt_alloc_internal()

include/linux/memblock.h | 5 ++---
mm/memblock.c | 8 +++-----
2 files changed, 5 insertions(+), 8 deletions(-)

--
1.7.9.5


2016-12-11 13:01:45

by Wei Yang

[permalink] [raw]
Subject: [PATCH 1/2] mm/memblock.c: trivial code refine in memblock_is_region_memory()

The base address is already guaranteed to be in the region by
memblock_search().

This patch removes the check on base, also a little refine in a macro.

Signed-off-by: Wei Yang <[email protected]>
---
include/linux/memblock.h | 5 ++---
mm/memblock.c | 2 +-
2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/include/linux/memblock.h b/include/linux/memblock.h
index 3106ac1..e611819 100644
--- a/include/linux/memblock.h
+++ b/include/linux/memblock.h
@@ -408,9 +408,8 @@ static inline unsigned long memblock_region_reserved_end_pfn(const struct memblo
region++)

#define for_each_memblock_type(memblock_type, rgn) \
- idx = 0; \
- rgn = &memblock_type->regions[idx]; \
- for (idx = 0; idx < memblock_type->cnt; \
+ for (idx = 0, rgn = &memblock_type->regions[idx]; \
+ idx < memblock_type->cnt; \
idx++,rgn = &memblock_type->regions[idx])

#ifdef CONFIG_MEMTEST
diff --git a/mm/memblock.c b/mm/memblock.c
index ac12489..9d402d05 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -1557,7 +1557,7 @@ int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size

if (idx == -1)
return 0;
- return memblock.memory.regions[idx].base <= base &&
+ return /* memblock.memory.regions[idx].base <= base && */
(memblock.memory.regions[idx].base +
memblock.memory.regions[idx].size) >= end;
}
--
1.7.9.5

2016-12-11 13:01:58

by Wei Yang

[permalink] [raw]
Subject: [PATCH 2/2] mm/memblock.c: check return value of memblock_reserve() in memblock_virt_alloc_internal()

memblock_reserve() may fail in case there is not enough regions.

This patch checks the return value.

Signed-off-by: Wei Yang <[email protected]>
---
mm/memblock.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/mm/memblock.c b/mm/memblock.c
index 9d402d05..83ad703 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -1268,18 +1268,17 @@ static void * __init memblock_virt_alloc_internal(

if (max_addr > memblock.current_limit)
max_addr = memblock.current_limit;
-
again:
alloc = memblock_find_in_range_node(size, align, min_addr, max_addr,
nid, flags);
- if (alloc)
+ if (alloc && !memblock_reserve(alloc, size))
goto done;

if (nid != NUMA_NO_NODE) {
alloc = memblock_find_in_range_node(size, align, min_addr,
max_addr, NUMA_NO_NODE,
flags);
- if (alloc)
+ if (alloc && !memblock_reserve(alloc, size))
goto done;
}

@@ -1297,7 +1296,6 @@ again:

return NULL;
done:
- memblock_reserve(alloc, size);
ptr = phys_to_virt(alloc);
memset(ptr, 0, size);

--
1.7.9.5