Skip to contentSkip to conversation

mm, compaction: avoid uninitialized variable use

PATCH In review Arnd Bergmann 3 participants 8 messages May 11, 2016 – May 12, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm, compaction: avoid uninitialized variable use

A recent rework of the compaction code introduced a warning about an uninitialized variable when CONFIG_COMPACTION is disabled and __alloc_pages_direct_compact() does not set its 'compact_result' output argument:

mm/page_alloc.c: In function '__alloc_pages_nodemask':
mm/page_alloc.c:3651:6: error: 'compact_result' may be used uninitialized in this function [-Werror=maybe-uninitialized]

This adds another check for CONFIG_COMPACTION to ensure we never evaluate the uninitialized variable in this configuration, which is probably the simplest way to avoid the warning.

A more elaborate rework might make this more readable.

Fixes 13cff7b81275 ("mm, compaction: simplify __alloc_pages_direct_compact feedback interface")
---
 mm/page_alloc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/page_alloc.c +2 -1
index 477d9382f70d..bedadc686a22 100644
@@ -3640,7 +3640,8 @@ retry:
 		goto got_pg;
 
 	/* Checks for THP-specific high-order allocations */
-	if (is_thp_gfp_mask(gfp_mask)) {
+	if (IS_ENABLED(CONFIG_COMPACTION) &&
+		is_thp_gfp_mask(gfp_mask)) {
 		/*
 		 * If compaction is deferred for high-order allocations, it is
 		 * because sync compaction recently failed. If this is the case
Signature · 1 line
2.7.0
#1 lore raw
Quoted 11 lines · Arnd Bergmann

A recent rework of the compaction code introduced a warning about an uninitialized variable when CONFIG_COMPACTION is disabled and __alloc_pages_direct_compact() does not set its 'compact_result' output argument:

mm/page_alloc.c: In function '__alloc_pages_nodemask':
mm/page_alloc.c:3651:6: error: 'compact_result' may be used uninitialized in this function [-Werror=maybe-uninitialized]

This adds another check for CONFIG_COMPACTION to ensure we never evaluate the uninitialized variable in this configuration, which is probably the simplest way to avoid the warning.

I think that hiding this into __alloc_pages_direct_compact is a better idea. See the diff below

Quoted 5 lines

A more elaborate rework might make this more readable.

Fixes 13cff7b81275 ("mm, compaction: simplify __alloc_pages_direct_compact feedback interface")

Please do not use SHA for mmotm commits because they are unstable and change each linux-next release.

---

mm/page_alloc.c +1 -0
index 4950d01ff935..14e3b4d93adc 100644
@@ -3300,6 +3300,7 @@ __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
 		unsigned int alloc_flags, const struct alloc_context *ac,
 		enum migrate_mode mode, enum compact_result *compact_result)
 {
+	*compact_result = COMPACT_DEFERRED;
 	return NULL;
 }
 
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>
#2 lore raw
Quoted 9 lines · On Wednesday 11 May 2016 16:44:07 Michal Hocko wrote:
Quoted 8 lines · Arnd Bergmann

A recent rework of the compaction code introduced a warning about an uninitialized variable when CONFIG_COMPACTION is disabled and __alloc_pages_direct_compact() does not set its 'compact_result' output argument:

mm/page_alloc.c: In function '__alloc_pages_nodemask':
mm/page_alloc.c:3651:6: error: 'compact_result' may be used uninitialized in this function [-Werror=maybe-uninitialized]

Quoted 6 lines
Quoted 3 lines

This adds another check for CONFIG_COMPACTION to ensure we never evaluate the uninitialized variable in this configuration, which is probably the simplest way to avoid the warning.

I think that hiding this into __alloc_pages_direct_compact is a better idea. See the diff below

Ok, sounds good.

mm/page_alloc.c __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, · line 3306

Quoted 13 lines

---

mm/page_alloc.c +1 -0
index 4950d01ff935..14e3b4d93adc 100644
@@ -3300,6 +3300,7 @@ __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
                unsigned int alloc_flags, const struct alloc_context *ac,
                enum migrate_mode mode, enum compact_result *compact_result)
 {
+       *compact_result = COMPACT_DEFERRED;
        return NULL;
 }
 

I thought about this but didn't know which COMPACT_* value was appropriate here.

The behavior then changes a bit with your approach compared to mine, because

                if (compact_result == COMPACT_DEFERRED)
                        goto nopage;

is true now. I assume this is what we want though.

Arnd

#3 lore raw
Quoted 35 lines · Arnd Bergmann
Quoted 9 lines · On Wednesday 11 May 2016 16:44:07 Michal Hocko wrote:
Quoted 8 lines · Arnd Bergmann

A recent rework of the compaction code introduced a warning about an uninitialized variable when CONFIG_COMPACTION is disabled and __alloc_pages_direct_compact() does not set its 'compact_result' output argument:

mm/page_alloc.c: In function '__alloc_pages_nodemask':
mm/page_alloc.c:3651:6: error: 'compact_result' may be used uninitialized in this function [-Werror=maybe-uninitialized]

Quoted 6 lines
Quoted 3 lines

This adds another check for CONFIG_COMPACTION to ensure we never evaluate the uninitialized variable in this configuration, which is probably the simplest way to avoid the warning.

I think that hiding this into __alloc_pages_direct_compact is a better idea. See the diff below

Ok, sounds good.

mm/page_alloc.c __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, · line 3306

Quoted 13 lines

---

mm/page_alloc.c +1 -0
index 4950d01ff935..14e3b4d93adc 100644
@@ -3300,6 +3300,7 @@ __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
                unsigned int alloc_flags, const struct alloc_context *ac,
                enum migrate_mode mode, enum compact_result *compact_result)
 {
+       *compact_result = COMPACT_DEFERRED;
        return NULL;
 }
 

I thought about this but didn't know which COMPACT_* value was appropriate here.

Quoted 7 lines

The behavior then changes a bit with your approach compared to mine, because

                if (compact_result == COMPACT_DEFERRED)
                        goto nopage;

is true now. I assume this is what we want though.

No, we actually want to try to reclaim, that's why COMPACT_SKIPPED is more appropriate. COMPACT_SKIPPED is even correct semantically because the compaction wasn't invoked.

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>
#4 lore raw
In reply to Michal Hocko
Quoted 30 lines · Michal Hocko
Quoted 11 lines · Arnd Bergmann

A recent rework of the compaction code introduced a warning about an uninitialized variable when CONFIG_COMPACTION is disabled and __alloc_pages_direct_compact() does not set its 'compact_result' output argument:

mm/page_alloc.c: In function '__alloc_pages_nodemask':
mm/page_alloc.c:3651:6: error: 'compact_result' may be used uninitialized in this function [-Werror=maybe-uninitialized]

This adds another check for CONFIG_COMPACTION to ensure we never evaluate the uninitialized variable in this configuration, which is probably the simplest way to avoid the warning.

I think that hiding this into __alloc_pages_direct_compact is a better idea. See the diff below

Quoted 5 lines

A more elaborate rework might make this more readable.

Fixes 13cff7b81275 ("mm, compaction: simplify __alloc_pages_direct_compact feedback interface")

Please do not use SHA for mmotm commits because they are unstable and change each linux-next release.

---

mm/page_alloc.c +0 -0
index 4950d01ff935..14e3b4d93adc 100644

mm/page_alloc.c __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, · line 3303

Quoted 4 lines
mm/page_alloc.c +1 -0
@@ -3300,6 +3300,7 @@ __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
 		unsigned int alloc_flags, const struct alloc_context *ac,
 		enum migrate_mode mode, enum compact_result *compact_result)
 {
+	*compact_result = COMPACT_DEFERRED;

Sorry, this should have been COMPACT_SKIPPED.

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>
#5 lore raw
In reply to Arnd Bergmann

I think this would be slightly better than your proposal. Andrew, could you fold it into the original mm-compaction-simplify-__alloc_pages_direct_compact-feedback-interface.patch patch?
---

#6 lore raw
Quoted 15 lines · Michal Hocko

I think this would be slightly better than your proposal. Andrew, could you fold it into the original mm-compaction-simplify-__alloc_pages_direct_compact-feedback-interface.patch patch?
---
From 434bc8b6f3787724327499998c4fe651e8ce5d68 Mon Sep 17 00:00:00 2001
From: Michal Hocko <[email protected]>
Date: Thu, 12 May 2016 08:10:33 +0200
Subject: [PATCH] mmotm:
mm-compaction-simplify-__alloc_pages_direct_compact-feedback-interface-fix

Arnd has reported the following compilation warning:
mm/page_alloc.c: In function '__alloc_pages_nodemask':
mm/page_alloc.c:3651:6: error: 'compact_result' may be used uninitialized in this function [-Werror=maybe-uninitialized]

Quoted 8 lines

This should be a false positive TRANSPARENT_HUGEPAGE depends on COMPACTION so is_thp_gfp_mask shouldn't be true. GFP_TRANSHUGE is a bit tricky and somebody might be using this accidently. Make sure that compact_result is defined also for !CONFIG_COMPACT and set it to COMPACT_SKIPPED because the compaction was really withdrawn.

Ack.

Quoted 10 lines
---
  mm/page_alloc.c | 1 +
  1 file changed, 1 insertion(+)
mm/page_alloc.c +0 -0
index 4950d01ff935..0d9008042efa 100644

mm/page_alloc.c __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, · line 3306

Quoted 7 lines
mm/page_alloc.c +1 -0
@@ -3300,6 +3300,7 @@ __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
  		unsigned int alloc_flags, const struct alloc_context *ac,
  		enum migrate_mode mode, enum compact_result *compact_result)
  {
+	*compact_result = COMPACT_SKIPPED;
  	return NULL;
  }
 

--
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 Acked-by
#7 lore raw
In reply to Michal Hocko
Quoted 15 lines · On Thursday 12 May 2016 08:16:36 Michal Hocko wrote:

I think this would be slightly better than your proposal. Andrew, could you fold it into the original mm-compaction-simplify-__alloc_pages_direct_compact-feedback-interface.patch patch?
---
From 434bc8b6f3787724327499998c4fe651e8ce5d68 Mon Sep 17 00:00:00 2001
From: Michal Hocko <[email protected]>
Date: Thu, 12 May 2016 08:10:33 +0200
Subject: [PATCH] mmotm:
mm-compaction-simplify-__alloc_pages_direct_compact-feedback-interface-fix

Arnd has reported the following compilation warning:
mm/page_alloc.c: In function '__alloc_pages_nodemask':
mm/page_alloc.c:3651:6: error: 'compact_result' may be used uninitialized in this function [-Werror=maybe-uninitialized]

Quoted 9 lines

This should be a false positive TRANSPARENT_HUGEPAGE depends on COMPACTION so is_thp_gfp_mask shouldn't be true. GFP_TRANSHUGE is a bit tricky and somebody might be using this accidently. Make sure that compact_result is defined also for !CONFIG_COMPACT and set it to COMPACT_SKIPPED because the compaction was really withdrawn.

Looks much nicer than my version.

Arnd