@@ -4110,17 +4110,12 @@ static unsigned int slub_min_objects;
41104110 * the smallest order which will fit the object.
41114111 */
41124112static inline unsigned int calc_slab_order (unsigned int size ,
4113- unsigned int min_objects , unsigned int max_order ,
4113+ unsigned int min_order , unsigned int max_order ,
41144114 unsigned int fract_leftover )
41154115{
4116- unsigned int min_order = slub_min_order ;
41174116 unsigned int order ;
41184117
4119- if (order_objects (min_order , size ) > MAX_OBJS_PER_PAGE )
4120- return get_order (size * MAX_OBJS_PER_PAGE ) - 1 ;
4121-
4122- for (order = max (min_order , (unsigned int )get_order (min_objects * size ));
4123- order <= max_order ; order ++ ) {
4118+ for (order = min_order ; order <= max_order ; order ++ ) {
41244119
41254120 unsigned int slab_size = (unsigned int )PAGE_SIZE << order ;
41264121 unsigned int rem ;
@@ -4139,16 +4134,8 @@ static inline int calculate_order(unsigned int size)
41394134 unsigned int order ;
41404135 unsigned int min_objects ;
41414136 unsigned int max_objects ;
4142- unsigned int nr_cpus ;
4137+ unsigned int min_order ;
41434138
4144- /*
4145- * Attempt to find best configuration for a slab. This
4146- * works by first attempting to generate a layout with
4147- * the best configuration and backing off gradually.
4148- *
4149- * First we increase the acceptable waste in a slab. Then
4150- * we reduce the minimum objects required in a slab.
4151- */
41524139 min_objects = slub_min_objects ;
41534140 if (!min_objects ) {
41544141 /*
@@ -4160,40 +4147,46 @@ static inline int calculate_order(unsigned int size)
41604147 * order on systems that appear larger than they are, and too
41614148 * low order on systems that appear smaller than they are.
41624149 */
4163- nr_cpus = num_present_cpus ();
4150+ unsigned int nr_cpus = num_present_cpus ();
41644151 if (nr_cpus <= 1 )
41654152 nr_cpus = nr_cpu_ids ;
41664153 min_objects = 4 * (fls (nr_cpus ) + 1 );
41674154 }
4168- max_objects = order_objects (slub_max_order , size );
4155+ /* min_objects can't be 0 because get_order(0) is undefined */
4156+ max_objects = max (order_objects (slub_max_order , size ), 1U );
41694157 min_objects = min (min_objects , max_objects );
41704158
4171- while (min_objects > 1 ) {
4172- unsigned int fraction ;
4173-
4174- fraction = 16 ;
4175- while (fraction >= 4 ) {
4176- order = calc_slab_order (size , min_objects ,
4177- slub_max_order , fraction );
4178- if (order <= slub_max_order )
4179- return order ;
4180- fraction /= 2 ;
4181- }
4182- min_objects -- ;
4183- }
4159+ min_order = max_t (unsigned int , slub_min_order ,
4160+ get_order (min_objects * size ));
4161+ if (order_objects (min_order , size ) > MAX_OBJS_PER_PAGE )
4162+ return get_order (size * MAX_OBJS_PER_PAGE ) - 1 ;
41844163
41854164 /*
4186- * We were unable to place multiple objects in a slab. Now
4187- * lets see if we can place a single object there.
4165+ * Attempt to find best configuration for a slab. This works by first
4166+ * attempting to generate a layout with the best possible configuration
4167+ * and backing off gradually.
4168+ *
4169+ * We start with accepting at most 1/16 waste and try to find the
4170+ * smallest order from min_objects-derived/slub_min_order up to
4171+ * slub_max_order that will satisfy the constraint. Note that increasing
4172+ * the order can only result in same or less fractional waste, not more.
4173+ *
4174+ * If that fails, we increase the acceptable fraction of waste and try
4175+ * again. The last iteration with fraction of 1/2 would effectively
4176+ * accept any waste and give us the order determined by min_objects, as
4177+ * long as at least single object fits within slub_max_order.
41884178 */
4189- order = calc_slab_order (size , 1 , slub_max_order , 1 );
4190- if (order <= slub_max_order )
4191- return order ;
4179+ for (unsigned int fraction = 16 ; fraction > 1 ; fraction /= 2 ) {
4180+ order = calc_slab_order (size , min_order , slub_max_order ,
4181+ fraction );
4182+ if (order <= slub_max_order )
4183+ return order ;
4184+ }
41924185
41934186 /*
41944187 * Doh this slab cannot be placed using slub_max_order.
41954188 */
4196- order = calc_slab_order (size , 1 , MAX_ORDER , 1 );
4189+ order = get_order (size );
41974190 if (order <= MAX_ORDER )
41984191 return order ;
41994192 return - ENOSYS ;
@@ -4711,6 +4704,9 @@ static int __init setup_slub_min_order(char *str)
47114704{
47124705 get_option (& str , (int * )& slub_min_order );
47134706
4707+ if (slub_min_order > slub_max_order )
4708+ slub_max_order = slub_min_order ;
4709+
47144710 return 1 ;
47154711}
47164712
@@ -4721,6 +4717,9 @@ static int __init setup_slub_max_order(char *str)
47214717 get_option (& str , (int * )& slub_max_order );
47224718 slub_max_order = min_t (unsigned int , slub_max_order , MAX_ORDER );
47234719
4720+ if (slub_min_order > slub_max_order )
4721+ slub_min_order = slub_max_order ;
4722+
47244723 return 1 ;
47254724}
47264725
0 commit comments