@@ -438,20 +438,20 @@ static unsigned long __init init_range_memory_mapping(
438438static unsigned long __init get_new_step_size (unsigned long step_size )
439439{
440440 /*
441- * Explain why we shift by 5 and why we don't have to worry about
442- * 'step_size << 5' overflowing:
443- *
444- * initial mapped size is PMD_SIZE (2M).
441+ * Initial mapped size is PMD_SIZE (2M).
445442 * We can not set step_size to be PUD_SIZE (1G) yet.
446443 * In worse case, when we cross the 1G boundary, and
447444 * PG_LEVEL_2M is not set, we will need 1+1+512 pages (2M + 8k)
448- * to map 1G range with PTE. Use 5 as shift for now.
445+ * to map 1G range with PTE. Hence we use one less than the
446+ * difference of page table level shifts.
449447 *
450- * Don't need to worry about overflow, on 32bit, when step_size
451- * is 0, round_down() returns 0 for start, and that turns it
452- * into 0x100000000ULL.
448+ * Don't need to worry about overflow in the top-down case, on 32bit,
449+ * when step_size is 0, round_down() returns 0 for start, and that
450+ * turns it into 0x100000000ULL.
451+ * In the bottom-up case, round_up(x, 0) returns 0 though too, which
452+ * needs to be taken into consideration by the code below.
453453 */
454- return step_size << 5 ;
454+ return step_size << ( PMD_SHIFT - PAGE_SHIFT - 1 ) ;
455455}
456456
457457/**
@@ -471,7 +471,6 @@ static void __init memory_map_top_down(unsigned long map_start,
471471 unsigned long step_size ;
472472 unsigned long addr ;
473473 unsigned long mapped_ram_size = 0 ;
474- unsigned long new_mapped_ram_size ;
475474
476475 /* xen has big range in reserved near end of ram, skip it at first.*/
477476 addr = memblock_find_in_range (map_start , map_end , PMD_SIZE , PMD_SIZE );
@@ -496,14 +495,12 @@ static void __init memory_map_top_down(unsigned long map_start,
496495 start = map_start ;
497496 } else
498497 start = map_start ;
499- new_mapped_ram_size = init_range_memory_mapping (start ,
498+ mapped_ram_size + = init_range_memory_mapping (start ,
500499 last_start );
501500 last_start = start ;
502501 min_pfn_mapped = last_start >> PAGE_SHIFT ;
503- /* only increase step_size after big range get mapped */
504- if (new_mapped_ram_size > mapped_ram_size )
502+ if (mapped_ram_size >= step_size )
505503 step_size = get_new_step_size (step_size );
506- mapped_ram_size += new_mapped_ram_size ;
507504 }
508505
509506 if (real_end < map_end )
@@ -524,7 +521,7 @@ static void __init memory_map_top_down(unsigned long map_start,
524521static void __init memory_map_bottom_up (unsigned long map_start ,
525522 unsigned long map_end )
526523{
527- unsigned long next , new_mapped_ram_size , start ;
524+ unsigned long next , start ;
528525 unsigned long mapped_ram_size = 0 ;
529526 /* step_size need to be small so pgt_buf from BRK could cover it */
530527 unsigned long step_size = PMD_SIZE ;
@@ -539,19 +536,19 @@ static void __init memory_map_bottom_up(unsigned long map_start,
539536 * for page table.
540537 */
541538 while (start < map_end ) {
542- if (map_end - start > step_size ) {
539+ if (step_size && map_end - start > step_size ) {
543540 next = round_up (start + 1 , step_size );
544541 if (next > map_end )
545542 next = map_end ;
546- } else
543+ } else {
547544 next = map_end ;
545+ }
548546
549- new_mapped_ram_size = init_range_memory_mapping (start , next );
547+ mapped_ram_size + = init_range_memory_mapping (start , next );
550548 start = next ;
551549
552- if (new_mapped_ram_size > mapped_ram_size )
550+ if (mapped_ram_size >= step_size )
553551 step_size = get_new_step_size (step_size );
554- mapped_ram_size += new_mapped_ram_size ;
555552 }
556553}
557554
0 commit comments