Skip to content

Commit 5a6d924

Browse files
committed
Merge tag 'memblock-v6.3-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rppt/memblock
Pull memblock updates from Mike Rapoport: "Small optimizations: - fix off-by-one in the check whether memblock_add_range() should reallocate memory to accommodate newly inserted range - check only for relevant regions in memblock_merge_regions() rather than swipe over the entire array" * tag 'memblock-v6.3-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rppt/memblock: memblock: Avoid useless checks in memblock_merge_regions(). memblock: Make a boundary tighter in memblock_add_range().
2 parents 02a737f + 2fe0341 commit 5a6d924

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

mm/memblock.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -500,15 +500,19 @@ static int __init_memblock memblock_double_array(struct memblock_type *type,
500500
/**
501501
* memblock_merge_regions - merge neighboring compatible regions
502502
* @type: memblock type to scan
503-
*
504-
* Scan @type and merge neighboring compatible regions.
503+
* @start_rgn: start scanning from (@start_rgn - 1)
504+
* @end_rgn: end scanning at (@end_rgn - 1)
505+
* Scan @type and merge neighboring compatible regions in [@start_rgn - 1, @end_rgn)
505506
*/
506-
static void __init_memblock memblock_merge_regions(struct memblock_type *type)
507+
static void __init_memblock memblock_merge_regions(struct memblock_type *type,
508+
unsigned long start_rgn,
509+
unsigned long end_rgn)
507510
{
508511
int i = 0;
509-
510-
/* cnt never goes below 1 */
511-
while (i < type->cnt - 1) {
512+
if (start_rgn)
513+
i = start_rgn - 1;
514+
end_rgn = min(end_rgn, type->cnt - 1);
515+
while (i < end_rgn) {
512516
struct memblock_region *this = &type->regions[i];
513517
struct memblock_region *next = &type->regions[i + 1];
514518

@@ -525,6 +529,7 @@ static void __init_memblock memblock_merge_regions(struct memblock_type *type)
525529
/* move forward from next + 1, index of which is i + 2 */
526530
memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
527531
type->cnt--;
532+
end_rgn--;
528533
}
529534
}
530535

@@ -581,7 +586,7 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
581586
bool insert = false;
582587
phys_addr_t obase = base;
583588
phys_addr_t end = base + memblock_cap_size(base, &size);
584-
int idx, nr_new;
589+
int idx, nr_new, start_rgn = -1, end_rgn;
585590
struct memblock_region *rgn;
586591

587592
if (!size)
@@ -601,11 +606,11 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
601606
/*
602607
* The worst case is when new range overlaps all existing regions,
603608
* then we'll need type->cnt + 1 empty regions in @type. So if
604-
* type->cnt * 2 + 1 is less than type->max, we know
609+
* type->cnt * 2 + 1 is less than or equal to type->max, we know
605610
* that there is enough empty regions in @type, and we can insert
606611
* regions directly.
607612
*/
608-
if (type->cnt * 2 + 1 < type->max)
613+
if (type->cnt * 2 + 1 <= type->max)
609614
insert = true;
610615

611616
repeat:
@@ -635,10 +640,14 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
635640
#endif
636641
WARN_ON(flags != rgn->flags);
637642
nr_new++;
638-
if (insert)
643+
if (insert) {
644+
if (start_rgn == -1)
645+
start_rgn = idx;
646+
end_rgn = idx + 1;
639647
memblock_insert_region(type, idx++, base,
640648
rbase - base, nid,
641649
flags);
650+
}
642651
}
643652
/* area below @rend is dealt with, forget about it */
644653
base = min(rend, end);
@@ -647,9 +656,13 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
647656
/* insert the remaining portion */
648657
if (base < end) {
649658
nr_new++;
650-
if (insert)
659+
if (insert) {
660+
if (start_rgn == -1)
661+
start_rgn = idx;
662+
end_rgn = idx + 1;
651663
memblock_insert_region(type, idx, base, end - base,
652664
nid, flags);
665+
}
653666
}
654667

655668
if (!nr_new)
@@ -666,7 +679,7 @@ static int __init_memblock memblock_add_range(struct memblock_type *type,
666679
insert = true;
667680
goto repeat;
668681
} else {
669-
memblock_merge_regions(type);
682+
memblock_merge_regions(type, start_rgn, end_rgn);
670683
return 0;
671684
}
672685
}
@@ -902,7 +915,7 @@ static int __init_memblock memblock_setclr_flag(phys_addr_t base,
902915
r->flags &= ~flag;
903916
}
904917

905-
memblock_merge_regions(type);
918+
memblock_merge_regions(type, start_rgn, end_rgn);
906919
return 0;
907920
}
908921

@@ -1275,7 +1288,7 @@ int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
12751288
for (i = start_rgn; i < end_rgn; i++)
12761289
memblock_set_region_node(&type->regions[i], nid);
12771290

1278-
memblock_merge_regions(type);
1291+
memblock_merge_regions(type, start_rgn, end_rgn);
12791292
#endif
12801293
return 0;
12811294
}

0 commit comments

Comments
 (0)