@@ -230,34 +230,36 @@ struct ext4_new_flex_group_data {
230
230
#define MAX_RESIZE_BG 16384
231
231
232
232
/*
233
- * alloc_flex_gd() allocates a ext4_new_flex_group_data with size of
234
- * @flexbg_size.
233
+ * alloc_flex_gd() allocates an ext4_new_flex_group_data that satisfies the
234
+ * resizing from @o_group to @n_group, its size is typically @flexbg_size.
235
235
*
236
236
* Returns NULL on failure otherwise address of the allocated structure.
237
237
*/
238
238
static struct ext4_new_flex_group_data * alloc_flex_gd (unsigned int flexbg_size ,
239
239
ext4_group_t o_group , ext4_group_t n_group )
240
240
{
241
241
ext4_group_t last_group ;
242
+ unsigned int max_resize_bg ;
242
243
struct ext4_new_flex_group_data * flex_gd ;
243
244
244
245
flex_gd = kmalloc (sizeof (* flex_gd ), GFP_NOFS );
245
246
if (flex_gd == NULL )
246
247
goto out3 ;
247
248
248
- if (unlikely (flexbg_size > MAX_RESIZE_BG ))
249
- flex_gd -> resize_bg = MAX_RESIZE_BG ;
250
- else
251
- flex_gd -> resize_bg = flexbg_size ;
249
+ max_resize_bg = umin (flexbg_size , MAX_RESIZE_BG );
250
+ flex_gd -> resize_bg = max_resize_bg ;
252
251
253
252
/* Avoid allocating large 'groups' array if not needed */
254
253
last_group = o_group | (flex_gd -> resize_bg - 1 );
255
254
if (n_group <= last_group )
256
- flex_gd -> resize_bg = 1 << fls (n_group - o_group + 1 );
255
+ flex_gd -> resize_bg = 1 << fls (n_group - o_group );
257
256
else if (n_group - last_group < flex_gd -> resize_bg )
258
- flex_gd -> resize_bg = 1 << max (fls (last_group - o_group + 1 ),
257
+ flex_gd -> resize_bg = 1 << max (fls (last_group - o_group ),
259
258
fls (n_group - last_group ));
260
259
260
+ if (WARN_ON_ONCE (flex_gd -> resize_bg > max_resize_bg ))
261
+ flex_gd -> resize_bg = max_resize_bg ;
262
+
261
263
flex_gd -> groups = kmalloc_array (flex_gd -> resize_bg ,
262
264
sizeof (struct ext4_new_group_data ),
263
265
GFP_NOFS );
0 commit comments