Skip to contentSkip to conversation

mm: fix maybe-uninitialized warning in section_deactivate()

PATCH In review Arnd Bergmann 3 participants 3 messages Jan 23, 2017 – Jan 24, 2017

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

PATCH 1/1 mm: fix maybe-uninitialized warning in section_deactivate()

gcc cannot track the combined state of the 'mask' variable across the barrier in pgdat_resize_unlock() at compile time, so it warns that we can run into undefined behavior:

mm/sparse.c: In function 'section_deactivate':
mm/sparse.c:802:7: error: 'early_section' may be used uninitialized in this function [-Werror=maybe-uninitialized]

We know that this can't happen because the spin_unlock() doesn't affect the mask variable, so this is a false-postive warning, but rearranging the code to bail out earlier here makes it obvious to the compiler as well.

Fixes mmotm ("mm: support section-unaligned ZONE_DEVICE memory ranges")
---
 mm/sparse.c | 33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)
mm/sparse.c +17 -16
index 4267d09b656b..dd0c2dd08ee2 100644
@@ -807,23 +807,24 @@ static void section_deactivate(struct pglist_data *pgdat, unsigned long pfn,
 	unsigned long mask = section_active_mask(pfn, nr_pages), flags;
 
 	pgdat_resize_lock(pgdat, &flags);
-	if (!ms->usage) {
-		mask = 0;
-	} else if ((ms->usage->map_active & mask) != mask) {
-		WARN(1, "section already deactivated active: %#lx mask: %#lx\n",
-				ms->usage->map_active, mask);
-		mask = 0;
-	} else {
-		early_section = is_early_section(ms);
-		ms->usage->map_active ^= mask;
-		if (ms->usage->map_active == 0) {
-			usage = ms->usage;
-			ms->usage = NULL;
-			memmap = sparse_decode_mem_map(ms->section_mem_map,
-					section_nr);
-			ms->section_mem_map = 0;
-		}
+	if (!ms->usage ||
+	    WARN((ms->usage->map_active & mask) != mask,
+		 "section already deactivated active: %#lx mask: %#lx\n",
+			ms->usage->map_active, mask)) {
+		pgdat_resize_unlock(pgdat, &flags);
+		return;
 	}
+
+	early_section = is_early_section(ms);
+	ms->usage->map_active ^= mask;
+	if (ms->usage->map_active == 0) {
+		usage = ms->usage;
+		ms->usage = NULL;
+		memmap = sparse_decode_mem_map(ms->section_mem_map,
+				section_nr);
+		ms->section_mem_map = 0;
+	}
+
 	pgdat_resize_unlock(pgdat, &flags);
 
 	/*
Signature · 1 line
2.9.0
maintainer
#1 lore raw
Quoted 44 lines · On Mon, 23 Jan 2017 17:51:17 +0100 Arnd Bergmann <[email protected]> wrote:

gcc cannot track the combined state of the 'mask' variable across the barrier in pgdat_resize_unlock() at compile time, so it warns that we can run into undefined behavior:

mm/sparse.c: In function 'section_deactivate':
mm/sparse.c:802:7: error: 'early_section' may be used uninitialized in this function [-Werror=maybe-uninitialized]

We know that this can't happen because the spin_unlock() doesn't affect the mask variable, so this is a false-postive warning, but rearranging the code to bail out earlier here makes it obvious to the compiler as well.

...

mm/sparse.c +7 -16
@@ -807,23 +807,24 @@ static void section_deactivate(struct pglist_data *pgdat, unsigned long pfn,
 	unsigned long mask = section_active_mask(pfn, nr_pages), flags;
 
 	pgdat_resize_lock(pgdat, &flags);
-	if (!ms->usage) {
-		mask = 0;
-	} else if ((ms->usage->map_active & mask) != mask) {
-		WARN(1, "section already deactivated active: %#lx mask: %#lx\n",
-				ms->usage->map_active, mask);
-		mask = 0;
-	} else {
-		early_section = is_early_section(ms);
-		ms->usage->map_active ^= mask;
-		if (ms->usage->map_active == 0) {
-			usage = ms->usage;
-			ms->usage = NULL;
-			memmap = sparse_decode_mem_map(ms->section_mem_map,
-					section_nr);
-			ms->section_mem_map = 0;
-		}
+	if (!ms->usage ||
+	    WARN((ms->usage->map_active & mask) != mask,
+		 "section already deactivated active: %#lx mask: %#lx\n",
+			ms->usage->map_active, mask)) {
+		pgdat_resize_unlock(pgdat, &flags);
+		return;
 	}
+

mm/sparse.c static void section_deactivate(struct pglist_data *pgdat, unsigned long pfn, · line 827

Quoted 10 lines
mm/sparse.c +10 -0
+	early_section = is_early_section(ms);
+	ms->usage->map_active ^= mask;
+	if (ms->usage->map_active == 0) {
+		usage = ms->usage;
+		ms->usage = NULL;
+		memmap = sparse_decode_mem_map(ms->section_mem_map,
+				section_nr);
+		ms->section_mem_map = 0;
+	}
+

hm, OK, that looks equivalent.

I wonder if we still need the later

	if (!mask)
		return;

I wonder if this code is appropriately handling the `mask == -1' case. section_active_mask() can do that.

What does that -1 in section_active_mask() mean anyway? Was it really intended to represent the all-ones pattern or is it an error? If the latter, was it appropriate for section_active_mask() to return an unsigned type?

How come section_active_mask() is __init but its caller section_deactivate() is not?

--
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 61 lines · Andrew Morton
Quoted 44 lines · On Mon, 23 Jan 2017 17:51:17 +0100 Arnd Bergmann <[email protected]> wrote:

gcc cannot track the combined state of the 'mask' variable across the barrier in pgdat_resize_unlock() at compile time, so it warns that we can run into undefined behavior:

mm/sparse.c: In function 'section_deactivate':
mm/sparse.c:802:7: error: 'early_section' may be used uninitialized in this function [-Werror=maybe-uninitialized]

We know that this can't happen because the spin_unlock() doesn't affect the mask variable, so this is a false-postive warning, but rearranging the code to bail out earlier here makes it obvious to the compiler as well.

...

mm/sparse.c +7 -16
@@ -807,23 +807,24 @@ static void section_deactivate(struct pglist_data *pgdat, unsigned long pfn,
      unsigned long mask = section_active_mask(pfn, nr_pages), flags;
 
      pgdat_resize_lock(pgdat, &flags);
-     if (!ms->usage) {
-             mask = 0;
-     } else if ((ms->usage->map_active & mask) != mask) {
-             WARN(1, "section already deactivated active: %#lx mask: %#lx\n",
-                             ms->usage->map_active, mask);
-             mask = 0;
-     } else {
-             early_section = is_early_section(ms);
-             ms->usage->map_active ^= mask;
-             if (ms->usage->map_active == 0) {
-                     usage = ms->usage;
-                     ms->usage = NULL;
-                     memmap = sparse_decode_mem_map(ms->section_mem_map,
-                                     section_nr);
-                     ms->section_mem_map = 0;
-             }
+     if (!ms->usage ||
+         WARN((ms->usage->map_active & mask) != mask,
+              "section already deactivated active: %#lx mask: %#lx\n",
+                     ms->usage->map_active, mask)) {
+             pgdat_resize_unlock(pgdat, &flags);
+             return;
      }
+

mm/sparse.c static void section_deactivate(struct pglist_data *pgdat, unsigned long pfn, · line 827

Quoted 10 lines
mm/sparse.c +10 -0
+     early_section = is_early_section(ms);
+     ms->usage->map_active ^= mask;
+     if (ms->usage->map_active == 0) {
+             usage = ms->usage;
+             ms->usage = NULL;
+             memmap = sparse_decode_mem_map(ms->section_mem_map,
+                             section_nr);
+             ms->section_mem_map = 0;
+     }
+

hm, OK, that looks equivalent.

I wonder if we still need the later

Quoted 8 lines
        if (!mask)
                return;

I wonder if this code is appropriately handling the `mask == -1' case. section_active_mask() can do that.

What does that -1 in section_active_mask() mean anyway? Was it really intended to represent the all-ones pattern or is it an error?

It's supposed to represent a full section's worth of bits, patch below to add comments and switch over to ULONG_MAX to make it clearer. I also fixed a bug with the case where the start pfn is section aligned, but nr_pages is less than a section.

Quoted 6 lines

If the
latter, was it appropriate for section_active_mask() to return an unsigned type?

How come section_active_mask() is __init but its caller section_deactivate() is not?

section_deactivate() is called from the memory hot-remove path which has traditionally not been tagged __meminit, so section_active_mask() can't be __init either. I missed this earlier when I reviewed your fix, and it seems you got it clarified now with the fix from Arnd.

Fix up patch attached, and possibly whitespace damaged version below:

--->8---