Skip to contentSkip to conversation

mm/page_alloc: optimize find_suitable_fallback() and fallbacks array

PATCH In review Yajun Deng 3 participants 8 messages Feb 9, 2023 – Feb 10, 2023

Subsystems (4)
lore
Share & tools
lore

Yajun Deng

author
#0 lore raw

PATCH v2 1/1 mm/page_alloc: optimize find_suitable_fallback() and fallbacks array

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.

At the same time, add !migratetype_is_mergeable() before the loop and reduce the first index size from MIGRATE_TYPES to MIGRATE_PCPTYPES in fallbacks array.

---
 include/linux/mmzone.h |  2 +-
 mm/page_alloc.c        | 11 +++++------
 2 files changed, 6 insertions(+), 7 deletions(-)
include/linux/mmzone.h +1 -1
index ab94985ee7d9..0a817b8c7fb2 100644
@@ -85,7 +85,7 @@ static inline bool is_migrate_movable(int mt)
  * Check whether a migratetype can be merged with another migratetype.
  *
  * It is only mergeable when it can fall back to other migratetypes for
- * allocation. See fallbacks[MIGRATE_TYPES][3] in page_alloc.c.
+ * allocation. See fallbacks[][] array in page_alloc.c.
  */
 static inline bool migratetype_is_mergeable(int mt)
 {
mm/page_alloc.c +5 -6
index 1113483fa6c5..536e8d838fb5 100644
@@ -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;
Signature · 1 line
2.25.1
reviewer
#1 lore raw
Quoted 2 lines · 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.

Can you explain why? If it is the case, MIGRATE_UNMOVABLE cannot fall back to MIGRATE_MOVABLE? And MIGRATE_MOVABLE cannot fall back to MIGRATE_UNMOVABLE? And MIGRATE_RECLAIMABLE cannot fall back to MIGRATE_MOVABLE?

Quoted 4 lines

At the same time, add !migratetype_is_mergeable() before the loop and reduce the first index size from MIGRATE_TYPES to MIGRATE_PCPTYPES in fallbacks array.

You sent a patch: https://lore.kernel.org/all/[email protected]/T/#u, why not squash this one into that? Why do we need two separate small patches working on the same code?

Thanks.

Quoted 60 lines
---
 include/linux/mmzone.h |  2 +-
 mm/page_alloc.c        | 11 +++++------
 2 files changed, 6 insertions(+), 7 deletions(-)
include/linux/mmzone.h +1 -1
index ab94985ee7d9..0a817b8c7fb2 100644
@@ -85,7 +85,7 @@ static inline bool is_migrate_movable(int mt)
  * Check whether a migratetype can be merged with another migratetype.
  *
  * It is only mergeable when it can fall back to other migratetypes for
- * allocation. See fallbacks[MIGRATE_TYPES][3] in page_alloc.c.
+ * allocation. See fallbacks[][] array in page_alloc.c.
  */
 static inline bool migratetype_is_mergeable(int mt)
 {
mm/page_alloc.c +5 -6
index 1113483fa6c5..536e8d838fb5 100644
@@ -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;
Signature · 1 line
2.25.1

--
Best Regards,
Yan, Zi

author
#2 lore raw
Quoted 9 lines · Zi Yan
Quoted 2 lines · 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.

Can you explain why? If it is the case, MIGRATE_UNMOVABLE cannot fall back to MIGRATE_MOVABLE? And MIGRATE_MOVABLE cannot fall back to MIGRATE_UNMOVABLE? And MIGRATE_RECLAIMABLE cannot fall back to MIGRATE_MOVABLE?

The return in the loop is only related to 'order', 'migratetype' and 'only_stealable' variables. Even if it execute the next loop, it can't change the result. So the loop can be broken if the first loop can't return.

Quoted 8 lines
Quoted 3 lines

At the same time, add !migratetype_is_mergeable() before the loop and reduce the first index size from MIGRATE_TYPES to MIGRATE_PCPTYPES in fallbacks array.

You sent a patch: https://lore.kernel.org/all/[email protected]/T/#u,
why not squash this one into that? Why do
we need two separate small patches working on the same code?

Yes, this is better, but I overlooked this one when I sent the first patch. It is already merged.

As Vlastimil Babka said, reduce the first index from MIGRATE_TYPES to MIGRATE_PCPTYPES may be cause out of bounds access of the shrinked fallbacks array If we don't judge the range of migratetype. But this doesn't happen with the second index.

Quoted 65 lines

Thanks.

Quoted 58 lines

---
include/linux/mmzone.h | 2 +-
mm/page_alloc.c | 11 +++++------

2 files changed, 6 insertions(+), 7 deletions(-)
include/linux/mmzone.h +1 -1
index ab94985ee7d9..0a817b8c7fb2 100644
@@ -85,7 +85,7 @@ static inline bool is_migrate_movable(int mt)
 * Check whether a migratetype can be merged with another migratetype.
 *
 * It is only mergeable when it can fall back to other migratetypes for
- * allocation. See fallbacks[MIGRATE_TYPES][3] in page_alloc.c.
+ * allocation. See fallbacks[][] array in page_alloc.c.
 */
 static inline bool migratetype_is_mergeable(int mt)
 {
mm/page_alloc.c +5 -7
index 1113483fa6c5..536e8d838fb5 100644
@@ -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;
--
Quoted 1 line

2.25.1

--
Best Regards,
Yan, Zi

reviewer
#3 lore raw
Quoted 7 lines · Yajun Deng
Quoted 5 lines · Zi Yan
Quoted 2 lines · 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.

Quoted 8 lines
Quoted 4 lines

Can you explain why? If it is the case, MIGRATE_UNMOVABLE cannot fall back to MIGRATE_MOVABLE? And MIGRATE_MOVABLE cannot fall back to MIGRATE_UNMOVABLE? And MIGRATE_RECLAIMABLE cannot fall back to MIGRATE_MOVABLE?

The return in the loop is only related to 'order', 'migratetype' and 'only_stealable' variables. Even if it execute the next loop, it can't change the result. So the loop can be broken if the first loop can't return.

OK. Got it. Would the code below look better?

        for (i = 0; i < MIGRATE_PCPTYPES - 1 ; i++) {
                fallback_mt = fallbacks[migratetype][i];
                if (free_area_empty(area, fallback_mt))
                        continue;
        }

        if (can_steal_fallback(order, migratetype))
                *can_steal = true;

        if (!only_stealable || *can_steal)
                return fallback_mt;

        return -1;
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

At the same time, add !migratetype_is_mergeable() before the loop and reduce the first index size from MIGRATE_TYPES to MIGRATE_PCPTYPES in fallbacks array.

You sent a patch: https://lore.kernel.org/all/[email protected]/T/#u,
why not squash this one into that? Why do
we need two separate small patches working on the same code?

Quoted 71 lines

Yes, this is better, but I overlooked this one when I sent the first patch. It is already merged.

As Vlastimil Babka said, reduce the first index from MIGRATE_TYPES to MIGRATE_PCPTYPES may be cause out of bounds access of the shrinked fallbacks array If we don't judge the range of migratetype. But this doesn't happen with the second index.

Quoted 65 lines

Thanks.

Quoted 58 lines

---
include/linux/mmzone.h | 2 +-
mm/page_alloc.c | 11 +++++------

2 files changed, 6 insertions(+), 7 deletions(-)
include/linux/mmzone.h +1 -1
index ab94985ee7d9..0a817b8c7fb2 100644
@@ -85,7 +85,7 @@ static inline bool is_migrate_movable(int mt)
 * Check whether a migratetype can be merged with another migratetype.
 *
 * It is only mergeable when it can fall back to other migratetypes for
- * allocation. See fallbacks[MIGRATE_TYPES][3] in page_alloc.c.
+ * allocation. See fallbacks[][] array in page_alloc.c.
 */
 static inline bool migratetype_is_mergeable(int mt)
 {
mm/page_alloc.c +5 -7
index 1113483fa6c5..536e8d838fb5 100644
@@ -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;
--
Quoted 1 line

2.25.1

--
Best Regards,
Yan, Zi

--
Best Regards,
Yan, Zi

author
#4 lore raw
Quoted 24 lines · Zi Yan
Quoted 6 lines · Yajun Deng
Quoted 1 line · Zi Yan

On 9 Feb 2023, at 5:11, 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.

Quoted 7 lines
Quoted 3 lines

Can you explain why? If it is the case, MIGRATE_UNMOVABLE cannot fall back to MIGRATE_MOVABLE? And MIGRATE_MOVABLE cannot fall back to MIGRATE_UNMOVABLE? And MIGRATE_RECLAIMABLE cannot fall back to MIGRATE_MOVABLE?

The return in the loop is only related to 'order', 'migratetype' and 'only_stealable' variables. Even if it execute the next loop, it can't change the result. So the loop can be broken if the first loop can't return.

OK. Got it. Would the code below look better?

for (i = 0; i < MIGRATE_PCPTYPES - 1 ; i++) {
fallback_mt = fallbacks[migratetype][i];
if (free_area_empty(area, fallback_mt))
continue;
}

Quoted 8 lines

if (can_steal_fallback(order, migratetype))
*can_steal = true;

if (!only_stealable || *can_steal)
return fallback_mt;

return -1;

Yes, I'll submit a v3 patch.
Thanks.

Quoted 81 lines
Quoted 73 lines

At the same time, add !migratetype_is_mergeable() before the loop and reduce the first index size from MIGRATE_TYPES to MIGRATE_PCPTYPES in fallbacks array.

Quoted 3 lines

You sent a patch: https://lore.kernel.org/all/[email protected]/T/#u,
why not squash this one into that? Why do
we need two separate small patches working on the same code?

Yes, this is better, but I overlooked this one when I sent the first patch. It is already merged.

As Vlastimil Babka said, reduce the first index from MIGRATE_TYPES to MIGRATE_PCPTYPES may be cause out of bounds access of the shrinked fallbacks array If we don't judge the range of migratetype. But this doesn't happen with the second index.

Quoted 1 line

Thanks.

---
include/linux/mmzone.h | 2 +-
mm/page_alloc.c | 11 +++++------

2 files changed, 6 insertions(+), 7 deletions(-)
include/linux/mmzone.h +1 -1
index ab94985ee7d9..0a817b8c7fb2 100644
@@ -85,7 +85,7 @@ static inline bool is_migrate_movable(int mt)
 * Check whether a migratetype can be merged with another migratetype.
 *
 * It is only mergeable when it can fall back to other migratetypes for
- * allocation. See fallbacks[MIGRATE_TYPES][3] in page_alloc.c.
+ * allocation. See fallbacks[][] array in page_alloc.c.
 */
 static inline bool migratetype_is_mergeable(int mt)
 {
mm/page_alloc.c +5 -7
index 1113483fa6c5..536e8d838fb5 100644
@@ -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;
--
Quoted 4 lines

2.25.1

Quoted 3 lines

--
Best Regards,
Yan, Zi

--
Best Regards,
Yan, Zi

author
#5 lore raw
Quoted 28 lines · Yajun Deng
Quoted 26 lines · Zi Yan
Quoted 1 line · Yajun Deng

February 9, 2023 11:50 PM, "Zi Yan" <[email protected]> wrote:

Quoted 2 lines · 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.

Can you explain why? If it is the case, MIGRATE_UNMOVABLE cannot fall back to MIGRATE_MOVABLE? And MIGRATE_MOVABLE cannot fall back to MIGRATE_UNMOVABLE? And MIGRATE_RECLAIMABLE cannot fall back to MIGRATE_MOVABLE?

Quoted 3 lines

The return in the loop is only related to 'order', 'migratetype' and 'only_stealable' variables. Even if it execute the next loop, it can't change the result. So the loop can be broken if the first loop can't return.

OK. Got it. Would the code below look better?

for (i = 0; i < MIGRATE_PCPTYPES - 1 ; i++) {
fallback_mt = fallbacks[migratetype][i];
if (free_area_empty(area, fallback_mt))
continue;
}

if (can_steal_fallback(order, migratetype))
*can_steal = true;

Quoted 8 lines
Quoted 4 lines

if (!only_stealable || *can_steal)
return fallback_mt;

return -1;

Yes, I'll submit a v3 patch.
Thanks.

I found a logical error in your code. It should be like this:

        for (i = 0; i < MIGRATE_PCPTYPES - 1 ; i++) {
                fallback_mt = fallbacks[migratetype][i];
                if (!free_area_empty(area, fallback_mt))
                        break;
        }

        if (can_steal_fallback(order, migratetype))
                *can_steal = true;

        if (!only_stealable || *can_steal)
                return fallback_mt;

        return -1;

This code will modify the logic to the opposite.
So can anyone tell me if I should use this code or the v2 patch?

Quoted 81 lines
Quoted 81 lines
Quoted 3 lines

At the same time, add !migratetype_is_mergeable() before the loop and reduce the first index size from MIGRATE_TYPES to MIGRATE_PCPTYPES in fallbacks array.

You sent a patch: https://lore.kernel.org/all/[email protected]/T/#u,
why not squash this one into that? Why do
we need two separate small patches working on the same code?

Quoted 5 lines

Yes, this is better, but I overlooked this one when I sent the first patch. It is already merged.

As Vlastimil Babka said, reduce the first index from MIGRATE_TYPES to MIGRATE_PCPTYPES may be cause out of bounds access of the shrinked fallbacks array If we don't judge the range of migratetype. But this doesn't happen with the second index.

Thanks.

Quoted 58 lines

---
include/linux/mmzone.h | 2 +-
mm/page_alloc.c | 11 +++++------

2 files changed, 6 insertions(+), 7 deletions(-)
include/linux/mmzone.h +1 -1
index ab94985ee7d9..0a817b8c7fb2 100644
@@ -85,7 +85,7 @@ static inline bool is_migrate_movable(int mt)
 * Check whether a migratetype can be merged with another migratetype.
 *
 * It is only mergeable when it can fall back to other migratetypes for
- * allocation. See fallbacks[MIGRATE_TYPES][3] in page_alloc.c.
+ * allocation. See fallbacks[][] array in page_alloc.c.
 */
 static inline bool migratetype_is_mergeable(int mt)
 {
mm/page_alloc.c +5 -7
index 1113483fa6c5..536e8d838fb5 100644
@@ -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;
--
Quoted 1 line

2.25.1

--
Best Regards,
Yan, Zi

--
Best Regards,
Yan, Zi

#6 lore raw
Quoted 55 lines · Yajun Deng
Quoted 28 lines · Yajun Deng
Quoted 26 lines · Zi Yan
Quoted 1 line · Yajun Deng

February 9, 2023 11:50 PM, "Zi Yan" <[email protected]> wrote:

Quoted 2 lines · 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.

Can you explain why? If it is the case, MIGRATE_UNMOVABLE cannot fall back to MIGRATE_MOVABLE? And MIGRATE_MOVABLE cannot fall back to MIGRATE_UNMOVABLE? And MIGRATE_RECLAIMABLE cannot fall back to MIGRATE_MOVABLE?

Quoted 3 lines

The return in the loop is only related to 'order', 'migratetype' and 'only_stealable' variables. Even if it execute the next loop, it can't change the result. So the loop can be broken if the first loop can't return.

OK. Got it. Would the code below look better?

for (i = 0; i < MIGRATE_PCPTYPES - 1 ; i++) {
fallback_mt = fallbacks[migratetype][i];
if (free_area_empty(area, fallback_mt))
continue;
}

if (can_steal_fallback(order, migratetype))
*can_steal = true;

Quoted 8 lines
Quoted 4 lines

if (!only_stealable || *can_steal)
return fallback_mt;

return -1;

Yes, I'll submit a v3 patch.
Thanks.

I found a logical error in your code. It should be like this:

        for (i = 0; i < MIGRATE_PCPTYPES - 1 ; i++) {
                fallback_mt = fallbacks[migratetype][i];
                if (!free_area_empty(area, fallback_mt))
                        break;
        }

        if (can_steal_fallback(order, migratetype))
                *can_steal = true;

        if (!only_stealable || *can_steal)
                return fallback_mt;

        return -1;
Quoted 1 line

This code will modify the logic to the opposite.

It's still wrong, IMHO. If all fallbacks have free_area_empty(), it will return the last one and not -1. Also will set *can_steal in such case.

Quoted 1 line

So can anyone tell me if I should use this code or the v2 patch?

Once that bugs are fixed, the result will probably not look much better than v2, so I don't mind keeping v2.

author
#7 lore raw
Quoted 53 lines · Vlastimil Babka
Quoted 51 lines · Yajun Deng
Quoted 1 line · Yajun Deng

February 10, 2023 10:14 AM, "Zi Yan" <[email protected]> wrote:

On 9 Feb 2023, at 20:57, Yajun Deng wrote:

February 9, 2023 11:50 PM, "Zi Yan" <[email protected]> wrote:

On 9 Feb 2023, at 5:11, 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.

Can you explain why? If it is the case, MIGRATE_UNMOVABLE cannot fall back to MIGRATE_MOVABLE? And MIGRATE_MOVABLE cannot fall back to MIGRATE_UNMOVABLE? And MIGRATE_RECLAIMABLE cannot fall back to MIGRATE_MOVABLE?
The return in the loop is only related to 'order', 'migratetype' and 'only_stealable' variables. Even if it execute the next loop, it can't change the result. So the loop can be broken if the first loop can't return.

OK. Got it. Would the code below look better?

for (i = 0; i < MIGRATE_PCPTYPES - 1 ; i++) {
fallback_mt = fallbacks[migratetype][i];
if (free_area_empty(area, fallback_mt))
continue;
}

if (can_steal_fallback(order, migratetype))
*can_steal = true;

if (!only_stealable || *can_steal)
return fallback_mt;

return -1;

Quoted 2 lines

Yes, I'll submit a v3 patch.
Thanks.

I found a logical error in your code. It should be like this:

for (i = 0; i < MIGRATE_PCPTYPES - 1 ; i++) {
fallback_mt = fallbacks[migratetype][i];
if (!free_area_empty(area, fallback_mt))
break;
}

if (can_steal_fallback(order, migratetype))
*can_steal = true;

if (!only_stealable || *can_steal)
return fallback_mt;

Quoted 7 lines
Quoted 3 lines

return -1;

This code will modify the logic to the opposite.

It's still wrong, IMHO. If all fallbacks have free_area_empty(), it will return the last one and not -1. Also will set *can_steal in such case.

Yes, you are right.

Quoted 4 lines
Quoted 1 line

So can anyone tell me if I should use this code or the v2 patch?

Once that bugs are fixed, the result will probably not look much better than v2, so I don't mind keeping v2.

I agree with that.