@@ -77,6 +77,7 @@ static struct hstate * __initdata parsed_hstate;
77
77
static unsigned long __initdata default_hstate_max_huge_pages ;
78
78
static bool __initdata parsed_valid_hugepagesz = true;
79
79
static bool __initdata parsed_default_hugepagesz ;
80
+ static unsigned int default_hugepages_in_node [MAX_NUMNODES ] __initdata ;
80
81
81
82
/*
82
83
* Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
@@ -2963,33 +2964,39 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,
2963
2964
return ERR_PTR (- ENOSPC );
2964
2965
}
2965
2966
2966
- int alloc_bootmem_huge_page (struct hstate * h )
2967
+ int alloc_bootmem_huge_page (struct hstate * h , int nid )
2967
2968
__attribute__ ((weak , alias ("__alloc_bootmem_huge_page" )));
2968
- int __alloc_bootmem_huge_page (struct hstate * h )
2969
+ int __alloc_bootmem_huge_page (struct hstate * h , int nid )
2969
2970
{
2970
- struct huge_bootmem_page * m ;
2971
+ struct huge_bootmem_page * m = NULL ; /* initialize for clang */
2971
2972
int nr_nodes , node ;
2972
2973
2974
+ if (nid >= nr_online_nodes )
2975
+ return 0 ;
2976
+ /* do node specific alloc */
2977
+ if (nid != NUMA_NO_NODE ) {
2978
+ m = memblock_alloc_try_nid_raw (huge_page_size (h ), huge_page_size (h ),
2979
+ 0 , MEMBLOCK_ALLOC_ACCESSIBLE , nid );
2980
+ if (!m )
2981
+ return 0 ;
2982
+ goto found ;
2983
+ }
2984
+ /* allocate from next node when distributing huge pages */
2973
2985
for_each_node_mask_to_alloc (h , nr_nodes , node , & node_states [N_MEMORY ]) {
2974
- void * addr ;
2975
-
2976
- addr = memblock_alloc_try_nid_raw (
2986
+ m = memblock_alloc_try_nid_raw (
2977
2987
huge_page_size (h ), huge_page_size (h ),
2978
2988
0 , MEMBLOCK_ALLOC_ACCESSIBLE , node );
2979
- if (addr ) {
2980
- /*
2981
- * Use the beginning of the huge page to store the
2982
- * huge_bootmem_page struct (until gather_bootmem
2983
- * puts them into the mem_map).
2984
- */
2985
- m = addr ;
2986
- goto found ;
2987
- }
2989
+ /*
2990
+ * Use the beginning of the huge page to store the
2991
+ * huge_bootmem_page struct (until gather_bootmem
2992
+ * puts them into the mem_map).
2993
+ */
2994
+ if (!m )
2995
+ return 0 ;
2996
+ goto found ;
2988
2997
}
2989
- return 0 ;
2990
2998
2991
2999
found :
2992
- BUG_ON (!IS_ALIGNED (virt_to_phys (m ), huge_page_size (h )));
2993
3000
/* Put them into a private list first because mem_map is not up yet */
2994
3001
INIT_LIST_HEAD (& m -> list );
2995
3002
list_add (& m -> list , & huge_boot_pages );
@@ -3029,12 +3036,61 @@ static void __init gather_bootmem_prealloc(void)
3029
3036
cond_resched ();
3030
3037
}
3031
3038
}
3039
+ static void __init hugetlb_hstate_alloc_pages_onenode (struct hstate * h , int nid )
3040
+ {
3041
+ unsigned long i ;
3042
+ char buf [32 ];
3043
+
3044
+ for (i = 0 ; i < h -> max_huge_pages_node [nid ]; ++ i ) {
3045
+ if (hstate_is_gigantic (h )) {
3046
+ if (!alloc_bootmem_huge_page (h , nid ))
3047
+ break ;
3048
+ } else {
3049
+ struct page * page ;
3050
+ gfp_t gfp_mask = htlb_alloc_mask (h ) | __GFP_THISNODE ;
3051
+
3052
+ page = alloc_fresh_huge_page (h , gfp_mask , nid ,
3053
+ & node_states [N_MEMORY ], NULL );
3054
+ if (!page )
3055
+ break ;
3056
+ put_page (page ); /* free it into the hugepage allocator */
3057
+ }
3058
+ cond_resched ();
3059
+ }
3060
+ if (i == h -> max_huge_pages_node [nid ])
3061
+ return ;
3062
+
3063
+ string_get_size (huge_page_size (h ), 1 , STRING_UNITS_2 , buf , 32 );
3064
+ pr_warn ("HugeTLB: allocating %u of page size %s failed node%d. Only allocated %lu hugepages.\n" ,
3065
+ h -> max_huge_pages_node [nid ], buf , nid , i );
3066
+ h -> max_huge_pages -= (h -> max_huge_pages_node [nid ] - i );
3067
+ h -> max_huge_pages_node [nid ] = i ;
3068
+ }
3032
3069
3033
3070
static void __init hugetlb_hstate_alloc_pages (struct hstate * h )
3034
3071
{
3035
3072
unsigned long i ;
3036
3073
nodemask_t * node_alloc_noretry ;
3074
+ bool node_specific_alloc = false;
3037
3075
3076
+ /* skip gigantic hugepages allocation if hugetlb_cma enabled */
3077
+ if (hstate_is_gigantic (h ) && hugetlb_cma_size ) {
3078
+ pr_warn_once ("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n" );
3079
+ return ;
3080
+ }
3081
+
3082
+ /* do node specific alloc */
3083
+ for (i = 0 ; i < nr_online_nodes ; i ++ ) {
3084
+ if (h -> max_huge_pages_node [i ] > 0 ) {
3085
+ hugetlb_hstate_alloc_pages_onenode (h , i );
3086
+ node_specific_alloc = true;
3087
+ }
3088
+ }
3089
+
3090
+ if (node_specific_alloc )
3091
+ return ;
3092
+
3093
+ /* below will do all node balanced alloc */
3038
3094
if (!hstate_is_gigantic (h )) {
3039
3095
/*
3040
3096
* Bit mask controlling how hard we retry per-node allocations.
@@ -3055,11 +3111,7 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
3055
3111
3056
3112
for (i = 0 ; i < h -> max_huge_pages ; ++ i ) {
3057
3113
if (hstate_is_gigantic (h )) {
3058
- if (hugetlb_cma_size ) {
3059
- pr_warn_once ("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n" );
3060
- goto free ;
3061
- }
3062
- if (!alloc_bootmem_huge_page (h ))
3114
+ if (!alloc_bootmem_huge_page (h , NUMA_NO_NODE ))
3063
3115
break ;
3064
3116
} else if (!alloc_pool_huge_page (h ,
3065
3117
& node_states [N_MEMORY ],
@@ -3075,7 +3127,6 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
3075
3127
h -> max_huge_pages , buf , i );
3076
3128
h -> max_huge_pages = i ;
3077
3129
}
3078
- free :
3079
3130
kfree (node_alloc_noretry );
3080
3131
}
3081
3132
@@ -3990,6 +4041,10 @@ static int __init hugetlb_init(void)
3990
4041
}
3991
4042
default_hstate .max_huge_pages =
3992
4043
default_hstate_max_huge_pages ;
4044
+
4045
+ for (i = 0 ; i < nr_online_nodes ; i ++ )
4046
+ default_hstate .max_huge_pages_node [i ] =
4047
+ default_hugepages_in_node [i ];
3993
4048
}
3994
4049
}
3995
4050
@@ -4050,6 +4105,10 @@ void __init hugetlb_add_hstate(unsigned int order)
4050
4105
parsed_hstate = h ;
4051
4106
}
4052
4107
4108
+ bool __init __weak hugetlb_node_alloc_supported (void )
4109
+ {
4110
+ return true;
4111
+ }
4053
4112
/*
4054
4113
* hugepages command line processing
4055
4114
* hugepages normally follows a valid hugepagsz or default_hugepagsz
@@ -4061,6 +4120,10 @@ static int __init hugepages_setup(char *s)
4061
4120
{
4062
4121
unsigned long * mhp ;
4063
4122
static unsigned long * last_mhp ;
4123
+ int node = NUMA_NO_NODE ;
4124
+ int count ;
4125
+ unsigned long tmp ;
4126
+ char * p = s ;
4064
4127
4065
4128
if (!parsed_valid_hugepagesz ) {
4066
4129
pr_warn ("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n" , s );
@@ -4084,8 +4147,40 @@ static int __init hugepages_setup(char *s)
4084
4147
return 0 ;
4085
4148
}
4086
4149
4087
- if (sscanf (s , "%lu" , mhp ) <= 0 )
4088
- * mhp = 0 ;
4150
+ while (* p ) {
4151
+ count = 0 ;
4152
+ if (sscanf (p , "%lu%n" , & tmp , & count ) != 1 )
4153
+ goto invalid ;
4154
+ /* Parameter is node format */
4155
+ if (p [count ] == ':' ) {
4156
+ if (!hugetlb_node_alloc_supported ()) {
4157
+ pr_warn ("HugeTLB: architecture can't support node specific alloc, ignoring!\n" );
4158
+ return 0 ;
4159
+ }
4160
+ node = tmp ;
4161
+ p += count + 1 ;
4162
+ if (node < 0 || node >= nr_online_nodes )
4163
+ goto invalid ;
4164
+ /* Parse hugepages */
4165
+ if (sscanf (p , "%lu%n" , & tmp , & count ) != 1 )
4166
+ goto invalid ;
4167
+ if (!hugetlb_max_hstate )
4168
+ default_hugepages_in_node [node ] = tmp ;
4169
+ else
4170
+ parsed_hstate -> max_huge_pages_node [node ] = tmp ;
4171
+ * mhp += tmp ;
4172
+ /* Go to parse next node*/
4173
+ if (p [count ] == ',' )
4174
+ p += count + 1 ;
4175
+ else
4176
+ break ;
4177
+ } else {
4178
+ if (p != s )
4179
+ goto invalid ;
4180
+ * mhp = tmp ;
4181
+ break ;
4182
+ }
4183
+ }
4089
4184
4090
4185
/*
4091
4186
* Global state is always initialized later in hugetlb_init.
@@ -4098,6 +4193,10 @@ static int __init hugepages_setup(char *s)
4098
4193
last_mhp = mhp ;
4099
4194
4100
4195
return 1 ;
4196
+
4197
+ invalid :
4198
+ pr_warn ("HugeTLB: Invalid hugepages parameter %s\n" , p );
4199
+ return 0 ;
4101
4200
}
4102
4201
__setup ("hugepages=" , hugepages_setup );
4103
4202
@@ -4159,6 +4258,7 @@ __setup("hugepagesz=", hugepagesz_setup);
4159
4258
static int __init default_hugepagesz_setup (char * s )
4160
4259
{
4161
4260
unsigned long size ;
4261
+ int i ;
4162
4262
4163
4263
parsed_valid_hugepagesz = false;
4164
4264
if (parsed_default_hugepagesz ) {
@@ -4187,6 +4287,9 @@ static int __init default_hugepagesz_setup(char *s)
4187
4287
*/
4188
4288
if (default_hstate_max_huge_pages ) {
4189
4289
default_hstate .max_huge_pages = default_hstate_max_huge_pages ;
4290
+ for (i = 0 ; i < nr_online_nodes ; i ++ )
4291
+ default_hstate .max_huge_pages_node [i ] =
4292
+ default_hugepages_in_node [i ];
4190
4293
if (hstate_is_gigantic (& default_hstate ))
4191
4294
hugetlb_hstate_alloc_pages (& default_hstate );
4192
4295
default_hstate_max_huge_pages = 0 ;
0 commit comments