linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/page_alloc: optimize the loop in find_suitable_fallback()
@ 2023-02-09  2:44 Yajun Deng
  2023-02-09  8:12 ` Vlastimil Babka
  2023-02-09  8:44 ` Yajun Deng
  0 siblings, 2 replies; 4+ messages in thread
From: Yajun Deng @ 2023-02-09  2:44 UTC (permalink / raw)
  To: akpm
  Cc: ziy, mgorman, david, vbabka, rppt, osalvador, rppt, linux-mm,
	linux-kernel, Yajun Deng

There is no need to execute the next loop if it not return in the first
loop. So add a break at the end of the loop.

There are only three rows in fallbacks, so reduce the first index size
from MIGRATE_TYPES to MIGRATE_PCPTYPES.

Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
---
 mm/page_alloc.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 1113483fa6c5..536e8d838fb5 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -2603,7 +2603,7 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
  *
  * The other migratetypes do not have fallbacks.
  */
-static int fallbacks[MIGRATE_TYPES][MIGRATE_PCPTYPES - 1] = {
+static int fallbacks[MIGRATE_PCPTYPES][MIGRATE_PCPTYPES - 1] = {
 	[MIGRATE_UNMOVABLE]   = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE   },
 	[MIGRATE_MOVABLE]     = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE },
 	[MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE,   MIGRATE_MOVABLE   },
@@ -2861,7 +2861,7 @@ int find_suitable_fallback(struct free_area *area, unsigned int order,
 	int i;
 	int fallback_mt;
 
-	if (area->nr_free == 0)
+	if (area->nr_free == 0 || !migratetype_is_mergeable(migratetype))
 		return -1;
 
 	*can_steal = false;
@@ -2873,11 +2873,10 @@ int find_suitable_fallback(struct free_area *area, unsigned int order,
 		if (can_steal_fallback(order, migratetype))
 			*can_steal = true;
 
-		if (!only_stealable)
-			return fallback_mt;
-
-		if (*can_steal)
+		if (!only_stealable || *can_steal)
 			return fallback_mt;
+		else
+			break;
 	}
 
 	return -1;
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/page_alloc: optimize the loop in find_suitable_fallback()
  2023-02-09  2:44 [PATCH] mm/page_alloc: optimize the loop in find_suitable_fallback() Yajun Deng
@ 2023-02-09  8:12 ` Vlastimil Babka
  2023-02-09  8:44 ` Yajun Deng
  1 sibling, 0 replies; 4+ messages in thread
From: Vlastimil Babka @ 2023-02-09  8:12 UTC (permalink / raw)
  To: Yajun Deng, akpm
  Cc: ziy, mgorman, david, rppt, osalvador, rppt, linux-mm, linux-kernel

On 2/9/23 03:44, Yajun Deng wrote:
> There is no need to execute the next loop if it not return in the first
> loop. So add a break at the end of the loop.
> 
> There are only three rows in fallbacks, so reduce the first index size
> from MIGRATE_TYPES to MIGRATE_PCPTYPES.
> 
> Signed-off-by: Yajun Deng <yajun.deng@linux.dev>

Acked-by: Vlastimil Babka <vbabka@suse.cz>

> ---
>  mm/page_alloc.c | 11 +++++------
>  1 file changed, 5 insertions(+), 6 deletions(-)
> 
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index 1113483fa6c5..536e8d838fb5 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -2603,7 +2603,7 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
>   *
>   * The other migratetypes do not have fallbacks.
>   */
> -static int fallbacks[MIGRATE_TYPES][MIGRATE_PCPTYPES - 1] = {
> +static int fallbacks[MIGRATE_PCPTYPES][MIGRATE_PCPTYPES - 1] = {
>  	[MIGRATE_UNMOVABLE]   = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE   },
>  	[MIGRATE_MOVABLE]     = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE },
>  	[MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE,   MIGRATE_MOVABLE   },
> @@ -2861,7 +2861,7 @@ int find_suitable_fallback(struct free_area *area, unsigned int order,
>  	int i;
>  	int fallback_mt;
>  
> -	if (area->nr_free == 0)
> +	if (area->nr_free == 0 || !migratetype_is_mergeable(migratetype))

Just curious, did you the check for extra safety or did you find (by running
or code inspection) that this can be indeed called with a non-mergeable
migratetype, and cause out of bounds access of the shrinked fallbacks array?

BTW, I noticed the commment on migratetype_is_mergeable() contains:

"See fallbacks[MIGRATE_TYPES][3] in page_alloc.c. "

Should probably change it to e.g. "See fallbacks[][] array ..." so we don't
have to keep it in exact sync...

>  		return -1;
>  
>  	*can_steal = false;
> @@ -2873,11 +2873,10 @@ int find_suitable_fallback(struct free_area *area, unsigned int order,
>  		if (can_steal_fallback(order, migratetype))
>  			*can_steal = true;
>  
> -		if (!only_stealable)
> -			return fallback_mt;
> -
> -		if (*can_steal)
> +		if (!only_stealable || *can_steal)
>  			return fallback_mt;
> +		else
> +			break;
>  	}
>  
>  	return -1;



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/page_alloc: optimize the loop in find_suitable_fallback()
  2023-02-09  2:44 [PATCH] mm/page_alloc: optimize the loop in find_suitable_fallback() Yajun Deng
  2023-02-09  8:12 ` Vlastimil Babka
@ 2023-02-09  8:44 ` Yajun Deng
  2023-02-09  9:22   ` Vlastimil Babka
  1 sibling, 1 reply; 4+ messages in thread
From: Yajun Deng @ 2023-02-09  8:44 UTC (permalink / raw)
  To: Vlastimil Babka, akpm
  Cc: ziy, mgorman, david, rppt, osalvador, rppt, linux-mm, linux-kernel

February 9, 2023 4:12 PM, "Vlastimil Babka" <vbabka@suse.cz> wrote:

> On 2/9/23 03:44, Yajun Deng wrote:
> 
>> There is no need to execute the next loop if it not return in the first
>> loop. So add a break at the end of the loop.
>> 
>> There are only three rows in fallbacks, so reduce the first index size
>> from MIGRATE_TYPES to MIGRATE_PCPTYPES.
>> 
>> Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
> 
> Acked-by: Vlastimil Babka <vbabka@suse.cz>
> 
>> ---
>> mm/page_alloc.c | 11 +++++------
>> 1 file changed, 5 insertions(+), 6 deletions(-)
>> 
>> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
>> index 1113483fa6c5..536e8d838fb5 100644
>> --- a/mm/page_alloc.c
>> +++ b/mm/page_alloc.c
>> @@ -2603,7 +2603,7 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
>> *
>> * The other migratetypes do not have fallbacks.
>> */
>> -static int fallbacks[MIGRATE_TYPES][MIGRATE_PCPTYPES - 1] = {
>> +static int fallbacks[MIGRATE_PCPTYPES][MIGRATE_PCPTYPES - 1] = {
>> [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE },
>> [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE },
>> [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE },
>> @@ -2861,7 +2861,7 @@ int find_suitable_fallback(struct free_area *area, unsigned int order,
>> int i;
>> int fallback_mt;
>> 
>> - if (area->nr_free == 0)
>> + if (area->nr_free == 0 || !migratetype_is_mergeable(migratetype))
> 
> Just curious, did you the check for extra safety or did you find (by running
> or code inspection) that this can be indeed called with a non-mergeable
> migratetype, and cause out of bounds access of the shrinked fallbacks array?
> 

No, I'm not sure if it is called with a non-mergeable migratetype.
It is just for extra safety.

> BTW, I noticed the commment on migratetype_is_mergeable() contains:
> 
> "See fallbacks[MIGRATE_TYPES][3] in page_alloc.c. "
> 
> Should probably change it to e.g. "See fallbacks[][] array ..." so we don't
> have to keep it in exact sync...
> 

Yes, this comment should be changed.
So do I need to submit a v2 patch?

>> return -1;
>> 
>> *can_steal = false;
>> @@ -2873,11 +2873,10 @@ int find_suitable_fallback(struct free_area *area, unsigned int order,
>> if (can_steal_fallback(order, migratetype))
>> *can_steal = true;
>> 
>> - if (!only_stealable)
>> - return fallback_mt;
>> -
>> - if (*can_steal)
>> + if (!only_stealable || *can_steal)
>> return fallback_mt;
>> + else
>> + break;
>> }
>> 
>> return -1;


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm/page_alloc: optimize the loop in find_suitable_fallback()
  2023-02-09  8:44 ` Yajun Deng
@ 2023-02-09  9:22   ` Vlastimil Babka
  0 siblings, 0 replies; 4+ messages in thread
From: Vlastimil Babka @ 2023-02-09  9:22 UTC (permalink / raw)
  To: Yajun Deng, akpm
  Cc: ziy, mgorman, david, rppt, osalvador, rppt, linux-mm, linux-kernel

On 2/9/23 09:44, Yajun Deng wrote:
> February 9, 2023 4:12 PM, "Vlastimil Babka" <vbabka@suse.cz> wrote:
> 
>> On 2/9/23 03:44, Yajun Deng wrote:
>> 
>>> There is no need to execute the next loop if it not return in the first
>>> loop. So add a break at the end of the loop.
>>> 
>>> There are only three rows in fallbacks, so reduce the first index size
>>> from MIGRATE_TYPES to MIGRATE_PCPTYPES.
>>> 
>>> Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
>> 
>> Acked-by: Vlastimil Babka <vbabka@suse.cz>
>> 
>>> ---
>>> mm/page_alloc.c | 11 +++++------
>>> 1 file changed, 5 insertions(+), 6 deletions(-)
>>> 
>>> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
>>> index 1113483fa6c5..536e8d838fb5 100644
>>> --- a/mm/page_alloc.c
>>> +++ b/mm/page_alloc.c
>>> @@ -2603,7 +2603,7 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
>>> *
>>> * The other migratetypes do not have fallbacks.
>>> */
>>> -static int fallbacks[MIGRATE_TYPES][MIGRATE_PCPTYPES - 1] = {
>>> +static int fallbacks[MIGRATE_PCPTYPES][MIGRATE_PCPTYPES - 1] = {
>>> [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE },
>>> [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE },
>>> [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE },
>>> @@ -2861,7 +2861,7 @@ int find_suitable_fallback(struct free_area *area, unsigned int order,
>>> int i;
>>> int fallback_mt;
>>> 
>>> - if (area->nr_free == 0)
>>> + if (area->nr_free == 0 || !migratetype_is_mergeable(migratetype))
>> 
>> Just curious, did you the check for extra safety or did you find (by running
>> or code inspection) that this can be indeed called with a non-mergeable
>> migratetype, and cause out of bounds access of the shrinked fallbacks array?
>> 
> 
> No, I'm not sure if it is called with a non-mergeable migratetype.
> It is just for extra safety.

OK, I agree with that.

>> BTW, I noticed the commment on migratetype_is_mergeable() contains:
>> 
>> "See fallbacks[MIGRATE_TYPES][3] in page_alloc.c. "
>> 
>> Should probably change it to e.g. "See fallbacks[][] array ..." so we don't
>> have to keep it in exact sync...
>> 
> 
> Yes, this comment should be changed.
> So do I need to submit a v2 patch?

Please do, with my acked-by.

>>> return -1;
>>> 
>>> *can_steal = false;
>>> @@ -2873,11 +2873,10 @@ int find_suitable_fallback(struct free_area *area, unsigned int order,
>>> if (can_steal_fallback(order, migratetype))
>>> *can_steal = true;
>>> 
>>> - if (!only_stealable)
>>> - return fallback_mt;
>>> -
>>> - if (*can_steal)
>>> + if (!only_stealable || *can_steal)
>>> return fallback_mt;
>>> + else
>>> + break;
>>> }
>>> 
>>> return -1;



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-02-09  9:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-09  2:44 [PATCH] mm/page_alloc: optimize the loop in find_suitable_fallback() Yajun Deng
2023-02-09  8:12 ` Vlastimil Babka
2023-02-09  8:44 ` Yajun Deng
2023-02-09  9:22   ` Vlastimil Babka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).