Skip to contentSkip to conversation

mm: remove a redundant condition in the for loop

PATCH In review Hao Lee 5 participants 10 messages Jun 19, 2017 – Jun 24, 2017

Subsystems (2)
lore
Share & tools
lore

Hao Lee

author
#0 lore raw

PATCH 1/1 mm: remove a redundant condition in the for loop

The variable current_order decreases from MAX_ORDER-1 to order, so the condition current_order <= MAX_ORDER-1 is always true.

---
 mm/page_alloc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
mm/page_alloc.c +2 -3
index 2302f25..9120c2b 100644
@@ -2215,9 +2215,8 @@ __rmqueue_fallback(struct zone *zone, unsigned int order, int start_migratetype)
 	bool can_steal;
 
 	/* Find the largest possible block of pages in the other list */
-	for (current_order = MAX_ORDER-1;
-				current_order >= order && current_order <= MAX_ORDER-1;
-				--current_order) {
+	for (current_order = MAX_ORDER-1; current_order >= order;
+							--current_order) {
 		area = &(zone->free_area[current_order]);
 		fallback_mt = find_suitable_fallback(area, current_order,
 				start_migratetype, false, &can_steal);
Signature · 7 lines
2.9.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Hao Lee

The variable current_order decreases from MAX_ORDER-1 to order, so the condition current_order <= MAX_ORDER-1 is always true.

Sounds right.

Quoted 11 lines
---
 mm/page_alloc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
mm/page_alloc.c +0 -0
index 2302f25..9120c2b 100644
@@ -2215,9 +2215,8 @@ __rmqueue_fallback(struct zone *zone, unsigned int order, int start_migratetype)
 	bool can_steal;

mm/page_alloc.c __rmqueue_fallback(struct zone *zone, unsigned int order, int start_migratetype) · line 2222

Quoted 10 lines
mm/page_alloc.c +2 -3
 
 	/* Find the largest possible block of pages in the other list */
-	for (current_order = MAX_ORDER-1;
-				current_order >= order && current_order <= MAX_ORDER-1;
-				--current_order) {
+	for (current_order = MAX_ORDER-1; current_order >= order;
+							--current_order) {
 		area = &(zone->free_area[current_order]);
 		fallback_mt = find_suitable_fallback(area, current_order,
 				start_migratetype, false, &can_steal);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
Quoted 9 lines · Vlastimil Babka
Quoted 4 lines · Hao Lee

The variable current_order decreases from MAX_ORDER-1 to order, so the condition current_order <= MAX_ORDER-1 is always true.

Sounds right.

current_order and order are both unsigned, and if order==0, current_order >= order is always true, and we may decrement current_order past 0 making it UINT_MAX... A comment would be in order, though.

Quoted 11 lines
Quoted 11 lines
---
 mm/page_alloc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
mm/page_alloc.c +0 -0
index 2302f25..9120c2b 100644
@@ -2215,9 +2215,8 @@ __rmqueue_fallback(struct zone *zone, unsigned int order, int start_migratetype)
 	bool can_steal;
 
Quoted 10 lines

mm/page_alloc.c __rmqueue_fallback(struct zone *zone, unsigned int order, int start_migratetype) · line 2222

Quoted 10 lines
mm/page_alloc.c +2 -3
 	/* Find the largest possible block of pages in the other list */
-	for (current_order = MAX_ORDER-1;
-				current_order >= order && current_order <= MAX_ORDER-1;
-				--current_order) {
+	for (current_order = MAX_ORDER-1; current_order >= order;
+							--current_order) {
 		area = &(zone->free_area[current_order]);
 		fallback_mt = find_suitable_fallback(area, current_order,
 				start_migratetype, false, &can_steal);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
Quoted 8 lines · Rasmus Villemoes
Quoted 6 lines · Vlastimil Babka
Quoted 4 lines · Hao Lee

The variable current_order decreases from MAX_ORDER-1 to order, so the condition current_order <= MAX_ORDER-1 is always true.

Quoted 8 lines
Quoted 3 lines

Sounds right.

current_order and order are both unsigned, and if order==0, current_order >= order is always true, and we may decrement current_order past 0 making it UINT_MAX... A comment would be in order, though.

Doh, right. Thanks.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#4 lore raw
In reply to Rasmus Villemoes
Quoted 8 lines · Rasmus Villemoes
Quoted 6 lines · Vlastimil Babka
Quoted 4 lines · Hao Lee

The variable current_order decreases from MAX_ORDER-1 to order, so the condition current_order <= MAX_ORDER-1 is always true.

Quoted 8 lines
Quoted 3 lines

Sounds right.

current_order and order are both unsigned, and if order==0, current_order >= order is always true, and we may decrement current_order past 0 making it UINT_MAX... A comment would be in order, though.

Thanks, I didn't notice unsigned subtraction. Sorry about that.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw
In reply to Rasmus Villemoes
Quoted 8 lines · Rasmus Villemoes
Quoted 6 lines · Vlastimil Babka
Quoted 4 lines · Hao Lee

The variable current_order decreases from MAX_ORDER-1 to order, so the condition current_order <= MAX_ORDER-1 is always true.

Quoted 8 lines
Quoted 3 lines

Sounds right.

current_order and order are both unsigned, and if order==0, current_order >= order is always true, and we may decrement current_order past 0 making it UINT_MAX... A comment would be in order, though.

Yes, not the first time this has been brought up
https://lkml.org/lkml/2016/6/20/493. I guess a comment is long overdue. Or just get rid of the unsigned trap which would be probably more clean.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
---
 mm/page_alloc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
mm/page_alloc.c +2 -3
index 2302f25..9120c2b 100644
@@ -2215,9 +2215,8 @@ __rmqueue_fallback(struct zone *zone, unsigned int order, int start_migratetype)
 	bool can_steal;
 
 	/* Find the largest possible block of pages in the other list */
-	for (current_order = MAX_ORDER-1;
-				current_order >= order && current_order <= MAX_ORDER-1;
-				--current_order) {
+	for (current_order = MAX_ORDER-1; current_order >= order;
+							--current_order) {
 		area = &(zone->free_area[current_order]);
 		fallback_mt = find_suitable_fallback(area, current_order,
 				start_migratetype, false, &can_steal);
Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#6 lore raw

PATCH 1/1 mm/page_alloc.c: eliminate unsigned confusion in __rmqueue_fallback

Since current_order starts as MAX_ORDER-1 and is then only decremented, the second half of the loop condition seems superfluous. However, if order is 0, we may decrement current_order past 0, making it UINT_MAX. This is obviously too subtle ([1], [2]).

Since we need to add some comment anyway, change the two variables to signed, making the counting-down for loop look more familiar, and apparently also making gcc generate slightly smaller code.

[1] https://lkml.org/lkml/2016/6/20/493
[2] https://lkml.org/lkml/2017/6/19/345

---
Michal, something like this, perhaps?

mm/page_alloc.c | 10 +++++++---

 1 file changed, 7 insertions(+), 3 deletions(-)
mm/page_alloc.c +7 -3
index 2302f250d6b1..e656f4da9772 100644
@@ -2204,19 +2204,23 @@ static bool unreserve_highatomic_pageblock(const struct alloc_context *ac,
  * list of requested migratetype, possibly along with other pages from the same
  * block, depending on fragmentation avoidance heuristics. Returns true if
  * fallback was found so that __rmqueue_smallest() can grab it.
+ *
+ * The use of signed ints for order and current_order is a deliberate
+ * deviation from the rest of this file, to make the for loop
+ * condition simpler.
  */
 static inline bool
-__rmqueue_fallback(struct zone *zone, unsigned int order, int start_migratetype)
+__rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
 {
 	struct free_area *area;
-	unsigned int current_order;
+	int current_order;
 	struct page *page;
 	int fallback_mt;
 	bool can_steal;
 
 	/* Find the largest possible block of pages in the other list */
 	for (current_order = MAX_ORDER-1;
-				current_order >= order && current_order <= MAX_ORDER-1;
+				current_order >= order;
 				--current_order) {
 		area = &(zone->free_area[current_order]);
 		fallback_mt = find_suitable_fallback(area, current_order,
Signature · 7 lines
2.11.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v1 1/1
#7 lore raw

Re: [PATCH] mm/page_alloc.c: eliminate unsigned confusion in __rmqueue_fallback

Quoted 5 lines · Rasmus Villemoes

Since current_order starts as MAX_ORDER-1 and is then only decremented, the second half of the loop condition seems superfluous. However, if order is 0, we may decrement current_order past 0, making it UINT_MAX. This is obviously too subtle ([1], [2]).

Quoted 8 lines

Since we need to add some comment anyway, change the two variables to signed, making the counting-down for loop look more familiar, and apparently also making gcc generate slightly smaller code.

[1] https://lkml.org/lkml/2016/6/20/493
[2] https://lkml.org/lkml/2017/6/19/345

I would hope for a more consistent usage of the type but his alone should prevent future attempts to "clean up" the code.

Quoted 40 lines

---
Michal, something like this, perhaps?

mm/page_alloc.c | 10 +++++++---

 1 file changed, 7 insertions(+), 3 deletions(-)
mm/page_alloc.c +7 -3
index 2302f250d6b1..e656f4da9772 100644
@@ -2204,19 +2204,23 @@ static bool unreserve_highatomic_pageblock(const struct alloc_context *ac,
  * list of requested migratetype, possibly along with other pages from the same
  * block, depending on fragmentation avoidance heuristics. Returns true if
  * fallback was found so that __rmqueue_smallest() can grab it.
+ *
+ * The use of signed ints for order and current_order is a deliberate
+ * deviation from the rest of this file, to make the for loop
+ * condition simpler.
  */
 static inline bool
-__rmqueue_fallback(struct zone *zone, unsigned int order, int start_migratetype)
+__rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
 {
 	struct free_area *area;
-	unsigned int current_order;
+	int current_order;
 	struct page *page;
 	int fallback_mt;
 	bool can_steal;
 
 	/* Find the largest possible block of pages in the other list */
 	for (current_order = MAX_ORDER-1;
-				current_order >= order && current_order <= MAX_ORDER-1;
+				current_order >= order;
 				--current_order) {
 		area = &(zone->free_area[current_order]);
 		fallback_mt = find_suitable_fallback(area, current_order,
Signature · 1 line
2.11.0
Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v1 1/1
#8 lore raw

Re: [PATCH] mm/page_alloc.c: eliminate unsigned confusion in __rmqueue_fallback

In reply to Rasmus Villemoes
Quoted 5 lines · Rasmus Villemoes

Since current_order starts as MAX_ORDER-1 and is then only decremented, the second half of the loop condition seems superfluous. However, if order is 0, we may decrement current_order past 0, making it UINT_MAX. This is obviously too subtle ([1], [2]).

Quoted 8 lines

Since we need to add some comment anyway, change the two variables to signed, making the counting-down for loop look more familiar, and apparently also making gcc generate slightly smaller code.

[1] https://lkml.org/lkml/2016/6/20/493
[2] https://lkml.org/lkml/2017/6/19/345

Quoted 28 lines

---
Michal, something like this, perhaps?

mm/page_alloc.c | 10 +++++++---

 1 file changed, 7 insertions(+), 3 deletions(-)
mm/page_alloc.c +6 -2
index 2302f250d6b1..e656f4da9772 100644
@@ -2204,19 +2204,23 @@ static bool unreserve_highatomic_pageblock(const struct alloc_context *ac,
  * list of requested migratetype, possibly along with other pages from the same
  * block, depending on fragmentation avoidance heuristics. Returns true if
  * fallback was found so that __rmqueue_smallest() can grab it.
+ *
+ * The use of signed ints for order and current_order is a deliberate
+ * deviation from the rest of this file, to make the for loop
+ * condition simpler.
  */
 static inline bool
-__rmqueue_fallback(struct zone *zone, unsigned int order, int start_migratetype)
+__rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
 {
 	struct free_area *area;
-	unsigned int current_order;
+	int current_order;
 	struct page *page;

mm/page_alloc.c static bool unreserve_highatomic_pageblock(const struct alloc_context *ac, · line 2226

Quoted 10 lines
mm/page_alloc.c +1 -1
 	int fallback_mt;
 	bool can_steal;
 
 	/* Find the largest possible block of pages in the other list */
 	for (current_order = MAX_ORDER-1;
-				current_order >= order && current_order <= MAX_ORDER-1;
+				current_order >= order;
 				--current_order) {
 		area = &(zone->free_area[current_order]);
 		fallback_mt = find_suitable_fallback(area, current_order,

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by on v1 1/1
#9 lore raw

Re: [PATCH] mm/page_alloc.c: eliminate unsigned confusion in __rmqueue_fallback

In reply to Rasmus Villemoes
Quoted 47 lines · Rasmus Villemoes

Since current_order starts as MAX_ORDER-1 and is then only decremented, the second half of the loop condition seems superfluous. However, if order is 0, we may decrement current_order past 0, making it UINT_MAX. This is obviously too subtle ([1], [2]).

Since we need to add some comment anyway, change the two variables to signed, making the counting-down for loop look more familiar, and apparently also making gcc generate slightly smaller code.

[1] https://lkml.org/lkml/2016/6/20/493
[2] https://lkml.org/lkml/2017/6/19/345

---
Michal, something like this, perhaps?

mm/page_alloc.c | 10 +++++++---

1 file changed, 7 insertions(+), 3 deletions(-)
mm/page_alloc.c +4 -0
index 2302f250d6b1..e656f4da9772 100644
@@ -2204,19 +2204,23 @@ static bool unreserve_highatomic_pageblock(const struct alloc_context *ac,
 * list of requested migratetype, possibly along with other pages from the same
 * block, depending on fragmentation avoidance heuristics. Returns true if
 * fallback was found so that __rmqueue_smallest() can grab it.
+ *
+ * The use of signed ints for order and current_order is a deliberate
+ * deviation from the rest of this file, to make the for loop
+ * condition simpler.
 */

static inline bool

mm/page_alloc.c +1 -1
-__rmqueue_fallback(struct zone *zone, unsigned int order, int start_migratetype)
+__rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
{
	struct free_area *area;
mm/page_alloc.c +1 -1
-	unsigned int current_order;
+	int current_order;
	struct page *page;
	int fallback_mt;
	bool can_steal;

	/* Find the largest possible block of pages in the other list */
	for (current_order = MAX_ORDER-1;
mm/page_alloc.c +1 -1
-				current_order >= order && current_order <= MAX_ORDER-1;
+				current_order >= order;
Quoted 5 lines
				--current_order) {
		area = &(zone->free_area[current_order]);
		fallback_mt = find_suitable_fallback(area, current_order,
Signature · 1 line
2.11.0

Looks nice. Why I didn't come up with this change.

Signature · 2 lines
Wei Yang
Help you, Help me