Skip to content

Commit eb18f1b

Browse files
committed
memblock: Make memblock functions handle overflowing range @SiZe
Allow memblock users to specify range where @base + @SiZe overflows and automatically cap it at maximum. This makes the interface more robust and specifying till-the-end-of-memory easier. Signed-off-by: Tejun Heo <tj@kernel.org> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Yinghai Lu <yinghai@kernel.org>
1 parent 7193618 commit eb18f1b

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

‎mm/memblock.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ static inline const char *memblock_type_name(struct memblock_type *type)
4949
return "unknown";
5050
}
5151

52+
/* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
53+
static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
54+
{
55+
return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
56+
}
57+
5258
/*
5359
* Address comparison utilities
5460
*/
@@ -328,7 +334,8 @@ static int __init_memblock memblock_add_region(struct memblock_type *type,
328334
phys_addr_t base, phys_addr_t size)
329335
{
330336
bool insert = false;
331-
phys_addr_t obase = base, end = base + size;
337+
phys_addr_t obase = base;
338+
phys_addr_t end = base + memblock_cap_size(base, &size);
332339
int i, nr_new;
333340

334341
/* special case for empty array */
@@ -420,7 +427,7 @@ static int __init_memblock memblock_isolate_range(struct memblock_type *type,
420427
phys_addr_t base, phys_addr_t size,
421428
int *start_rgn, int *end_rgn)
422429
{
423-
phys_addr_t end = base + size;
430+
phys_addr_t end = base + memblock_cap_size(base, &size);
424431
int i;
425432

426433
*start_rgn = *end_rgn = 0;
@@ -868,16 +875,18 @@ int __init_memblock memblock_is_memory(phys_addr_t addr)
868875
int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
869876
{
870877
int idx = memblock_search(&memblock.memory, base);
878+
phys_addr_t end = base + memblock_cap_size(base, &size);
871879

872880
if (idx == -1)
873881
return 0;
874882
return memblock.memory.regions[idx].base <= base &&
875883
(memblock.memory.regions[idx].base +
876-
memblock.memory.regions[idx].size) >= (base + size);
884+
memblock.memory.regions[idx].size) >= end;
877885
}
878886

879887
int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
880888
{
889+
memblock_cap_size(base, &size);
881890
return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
882891
}
883892

0 commit comments

Comments
 (0)