Skip to contentSkip to conversation

[4.15] gnttab: work around "may be used uninitialized" warning

PATCH Superseded Jan Beulich 5 participants 19 messages Mar 10, 2021 – Mar 12, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 [4.15] gnttab: work around "may be used uninitialized" warning

Sadly I was wrong to suggest dropping vaddrs' initializer during review of v2 of the patch introducing this code. gcc 4.3 can't cope.

Fixes 52531c734ea1 ("xen/gnttab: Rework resource acquisition")
xen/common/grant_table.c +1 -1
@@ -4026,7 +4026,7 @@ int gnttab_acquire_resource(
     struct grant_table *gt = d->grant_table;
     unsigned int i, final_frame;
     mfn_t tmp;
-    void **vaddrs;
+    void **vaddrs = NULL;
     int rc = -EINVAL;
 
     if ( !nr_frames )
#1 lore raw

Hi Jan,

Quoted 2 lines · Jan Beulich

Sadly I was wrong to suggest dropping vaddrs' initializer during review of v2 of the patch introducing this code. gcc 4.3 can't cope.

What's the error? Are you sure this is not going to hiding a potential miscompilation of the function?

xen/common/grant_table.c int gnttab_acquire_resource( · line 4029

Quoted 12 lines
Fixes 52531c734ea1 ("xen/gnttab: Rework resource acquisition")
xen/common/grant_table.c +1 -1
@@ -4026,7 +4026,7 @@ int gnttab_acquire_resource(
      struct grant_table *gt = d->grant_table;
      unsigned int i, final_frame;
      mfn_t tmp;
-    void **vaddrs;
+    void **vaddrs = NULL;

I am a bit nervous to inialize vaddrs to NULL for a few reasons:
1) It is not 100% obvious (e.g. it takes more than a second) that
vaddrs will always be initialized.
2) A compiler will not be able to help us if we are adding code
without initialized vaddrs.

It also feels wrong to me to try to write Xen in a way that will make a 10 years compiler happy...

If we still want to support them, then maybe a better approach would be to turn off to turn off -Werror for some version of GCC. So we can continue to benefit from the newer compiler diagnostics.

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw
Quoted 5 lines · Julien Grall
Quoted 2 lines · Jan Beulich

Sadly I was wrong to suggest dropping vaddrs' initializer during review of v2 of the patch introducing this code. gcc 4.3 can't cope.

What's the error?

The one quoted in the title.

Quoted 2 lines

Are you sure this is not going to hiding a potential miscompilation of the function?

Miscompilation? It may hide us screwing up, but addressing such a compiler warning by adding an initializer has been quite common in the past.

Quoted 11 lines

xen/common/grant_table.c int gnttab_acquire_resource( · line 4029

Quoted 8 lines
xen/common/grant_table.c +1 -1
@@ -4026,7 +4026,7 @@ int gnttab_acquire_resource(
      struct grant_table *gt = d->grant_table;
      unsigned int i, final_frame;
      mfn_t tmp;
-    void **vaddrs;
+    void **vaddrs = NULL;

I am a bit nervous to inialize vaddrs to NULL for a few reasons:
1) It is not 100% obvious (e.g. it takes more than a second) that
vaddrs will always be initialized.

But convincing ourselves was necessary even more so prior to this change. We must not ever rely on the compiler to tell us about issues in our code. We can only leverage that in some cases it does. From this it (I think obviously) follows that without the initializer we're at bigger risk than with it.

Quoted 5 lines

2) A compiler will not be able to help us if we are adding code
without initialized vaddrs.

It also feels wrong to me to try to write Xen in a way that will make a 10 years compiler happy...

As said above - we've worked around limitations quite a few times in the past. This is just one more instance.

Quoted 3 lines

If we still want to support them, then maybe a better approach would be to turn off to turn off -Werror for some version of GCC. So we can continue to benefit from the newer compiler diagnostics.

Avoiding use of -Werror is not an option imo: Once you start seeing warnings, you have only two options imo: Either one decides to ignore them all (and then one will also ignore new ones introduce by changes yet to be submitted), or one would have to memorize, for every build one does, which warnings one ought to ignore. The latter doesn't scale, while the former is a code quality problem.

Suppressing a particular class of warning might be an option, but again risks somebody submitting code which elsewhere would trigger warnings.

Jan

#3 lore raw

Hi Jan,

Quoted 7 lines · Jan Beulich
Quoted 5 lines · Julien Grall
Quoted 2 lines · Jan Beulich

Sadly I was wrong to suggest dropping vaddrs' initializer during review of v2 of the patch introducing this code. gcc 4.3 can't cope.

What's the error?

Quoted 8 lines

The one quoted in the title.

Quoted 2 lines

Are you sure this is not going to hiding a potential miscompilation of the function?

Miscompilation? It may hide us screwing up, but addressing such a compiler warning by adding an initializer has been quite common in the past.

Well... When a compiler tells me a value may be unitialized, I read it as some optimization may decide to use the variable in a way I wasn't expected.

Quoted 12 lines
Quoted 11 lines

xen/common/grant_table.c int gnttab_acquire_resource( · line 4029

Quoted 8 lines
xen/common/grant_table.c +1 -1
@@ -4026,7 +4026,7 @@ int gnttab_acquire_resource(
       struct grant_table *gt = d->grant_table;
       unsigned int i, final_frame;
       mfn_t tmp;
-    void **vaddrs;
+    void **vaddrs = NULL;

I am a bit nervous to inialize vaddrs to NULL for a few reasons:
1) It is not 100% obvious (e.g. it takes more than a second) that
vaddrs will always be initialized.

Quoted 4 lines

But convincing ourselves was necessary even more so prior to this change. We must not ever rely on the compiler to tell us about issues in our code. We can only leverage that in some cases it does.

I didn't suggest that we should only rely on the compiler. I pointed out that we are telling the compiler to not worry. This may hide a valid concern from the compiler.

Quoted 2 lines

From this it (I think obviously) follows that without the initializer we're at bigger risk than with it.

I thought deferencing a NULL pointer was still a concern for PV?

For the other setup, I agree that it would only lead to a crash rather than dereferencing anything. Yet I am not convinced this is that much better...

Quoted 8 lines
Quoted 5 lines

2) A compiler will not be able to help us if we are adding code
without initialized vaddrs.

It also feels wrong to me to try to write Xen in a way that will make a 10 years compiler happy...

As said above - we've worked around limitations quite a few times in the past. This is just one more instance.

I find amusing you wrote that when you complained multiple time when someone was re-using existing bad pattern. :)

Quoted 5 lines
Quoted 4 lines

If we still want to support them, then maybe a better approach would be to turn off to turn off -Werror for some version of GCC. So we can continue to benefit from the newer compiler diagnostics.

Quoted 9 lines

Avoiding use of -Werror is not an option imo: Once you start seeing warnings, you have only two options imo: Either one decides to ignore them all (and then one will also ignore new ones introduce by changes yet to be submitted), or one would have to memorize, for every build one does, which warnings one ought to ignore. The latter doesn't scale, while the former is a code quality problem. > Suppressing a particular class of warning might be an option, but again risks somebody submitting code which elsewhere would trigger warnings.

This is pretty much what we are already doing slowly by initializing values to shut up older compilers. I agree this is more limited, but we also waive off diagnostics from every single compiler in that code rather than just one version.

Hence why I suggested dropping -Werror for older compiler. This is not ideal but it would give us the ability to keep support for dinausor compiler and not hamper our ability to take advantage of newer compiler diagnostics.

The ideal solution is to drop support for older compiler (see my other thread). But this sounds like a daunting task so far on x86...

Anyway, I will not Nack the patch but will also not Ack it. I will let another maintainer ack this patch.

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw
Quoted 13 lines · Julien Grall
Quoted 12 lines · Jan Beulich
Quoted 5 lines · Julien Grall
Quoted 2 lines · Jan Beulich

Sadly I was wrong to suggest dropping vaddrs' initializer during review of v2 of the patch introducing this code. gcc 4.3 can't cope.

What's the error?

The one quoted in the title.

Quoted 2 lines

Are you sure this is not going to hiding a potential miscompilation of the function?

Quoted 7 lines
Quoted 3 lines

Miscompilation? It may hide us screwing up, but addressing such a compiler warning by adding an initializer has been quite common in the past.

Well... When a compiler tells me a value may be unitialized, I read it as some optimization may decide to use the variable in a way I wasn't expected.

I don't think that's how warnings like this work in general. Optimization may help spot a case where an uninitialized variable gets used (because optimization may result in sufficient simplification of the internal representation of the original source), and variable lifetime analysis may also be a prereq to optimization, but in general I'd recommend viewing the two as independent aspects.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

xen/common/grant_table.c int gnttab_acquire_resource( · line 4029

Quoted 8 lines
xen/common/grant_table.c +1 -1
@@ -4026,7 +4026,7 @@ int gnttab_acquire_resource(
       struct grant_table *gt = d->grant_table;
       unsigned int i, final_frame;
       mfn_t tmp;
-    void **vaddrs;
+    void **vaddrs = NULL;

I am a bit nervous to inialize vaddrs to NULL for a few reasons:
1) It is not 100% obvious (e.g. it takes more than a second) that
vaddrs will always be initialized.

Quoted 8 lines
Quoted 4 lines

But convincing ourselves was necessary even more so prior to this change. We must not ever rely on the compiler to tell us about issues in our code. We can only leverage that in some cases it does.

I didn't suggest that we should only rely on the compiler. I pointed out that we are telling the compiler to not worry. This may hide a valid concern from the compiler.

As we (have to) do elsewhere.

Quoted 4 lines
Quoted 2 lines

From this it (I think obviously) follows that without the initializer we're at bigger risk than with it.

I thought deferencing a NULL pointer was still a concern for PV?

I could use ZERO_BLOCK_PTR or yet something else, if we decided we want to work around this class of issues this way. Elsewhere we're using NULL afaict (but see also below).

Quoted 3 lines

For the other setup, I agree that it would only lead to a crash rather than dereferencing anything. Yet I am not convinced this is that much better...

When using an uninitialized variable, anything can happen. A crash would still be on the better side of things, as a privilege escalation then also is possible. Again - if you're worried about us introducing an actual use of the thus initialized variable, you should be even more worried about using it when it's uninitialized (and the compiler _not_ being able to spot it).

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines

2) A compiler will not be able to help us if we are adding code
without initialized vaddrs.

It also feels wrong to me to try to write Xen in a way that will make a 10 years compiler happy...

As said above - we've worked around limitations quite a few times in the past. This is just one more instance.

I find amusing you wrote that when you complained multiple time when someone was re-using existing bad pattern. :)

Well, thing is - I don't view this as a bad pattern. The only question really is whether NULL is a good initializer here. As per above a non- canonical pointer may be better, but then we have quite a few places elsewhere to fix. We could also deliberately leave the variable uninitialized, by using past Linux'es uninitialized_var(), but they've dropped that construct for what I think are very good reasons.

Jan

#5 lore raw
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Julien Grall
Quoted 10 lines · Jan Beulich
Quoted 6 lines · Julien Grall

On 10/03/2021 10:13, Jan Beulich wrote:
2) A compiler will not be able to help us if we are adding code
without initialized vaddrs.

It also feels wrong to me to try to write Xen in a way that will make a 10 years compiler happy...

As said above - we've worked around limitations quite a few times in the past. This is just one more instance.

Quoted 7 lines
Quoted 2 lines

I find amusing you wrote that when you complained multiple time when someone was re-using existing bad pattern. :)

Well, thing is - I don't view this as a bad pattern. The only question really is whether NULL is a good initializer here. As per above a non- canonical pointer may be better, but then we have quite a few places elsewhere to fix.

Sorry for jumping in the middle but I think that would be a very dangerous move for Xen to do. We have been using implicit conversions of pointers to booleans all over the place, assuming that NULL == false, hence NULL no longer mapping to false would break a lot of our code. ie:

if ( foo )

	free(foo);

Would no longer work as expected.

Thanks, Roger.

#6 lore raw
Quoted 28 lines · Roger Pau Monné
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Julien Grall
Quoted 10 lines · Jan Beulich
Quoted 6 lines · Julien Grall

On 10/03/2021 10:13, Jan Beulich wrote:
2) A compiler will not be able to help us if we are adding code
without initialized vaddrs.

It also feels wrong to me to try to write Xen in a way that will make a 10 years compiler happy...

As said above - we've worked around limitations quite a few times in the past. This is just one more instance.

Quoted 7 lines
Quoted 2 lines

I find amusing you wrote that when you complained multiple time when someone was re-using existing bad pattern. :)

Well, thing is - I don't view this as a bad pattern. The only question really is whether NULL is a good initializer here. As per above a non- canonical pointer may be better, but then we have quite a few places elsewhere to fix.

Sorry for jumping in the middle but I think that would be a very dangerous move for Xen to do. We have been using implicit conversions of pointers to booleans all over the place, assuming that NULL == false, hence NULL no longer mapping to false would break a lot of our code. ie:

Quoted 4 lines

if ( foo )

	free(foo);

Would no longer work as expected.

Funny you should give this example: Assuming you mean xfree(), it specifically tolerates both NULL and ZERO_BLOCK_PTR (the latter because xmalloc() may return it, and that's what it was invented for). But yes - other non-NULL checking guards would indeed break.

I'm afraid every possible solution here has its downsides, and the suggested change simply follows the pattern we used so far in similar cases - without anyone objecting.

Jan

#7 lore raw
In reply to Julien Grall

Julien Grall writes ("Re: [PATCH][4.15] gnttab: work around "may be used uninitialized" warning"):

Quoted 9 lines

This is pretty much what we are already doing slowly by initializing values to shut up older compilers. I agree this is more limited, but we also waive off diagnostics from every single compiler in that code rather than just one version.

Hence why I suggested dropping -Werror for older compiler. This is not ideal but it would give us the ability to keep support for dinausor compiler and not hamper our ability to take advantage of newer compiler diagnostics.

I agree with Julien. I think we should avoid adding these redundant initialisers for the reasons he gives.

Quoted 5 lines

The ideal solution is to drop support for older compiler (see my other thread). But this sounds like a daunting task so far on x86...

Anyway, I will not Nack the patch but will also not Ack it. I will let another maintainer ack this patch.

But this is outside my usual area so I won't nack it either.

Ian.

#8 lore raw
Quoted 6 lines · Ian Jackson

Julien Grall writes ("Re: [PATCH][4.15] gnttab: work around "may be used uninitialized" warning"):

Quoted 5 lines

This is pretty much what we are already doing slowly by initializing values to shut up older compilers. I agree this is more limited, but we also waive off diagnostics from every single compiler in that code rather than just one version.

Quoted 7 lines
Quoted 4 lines

Hence why I suggested dropping -Werror for older compiler. This is not ideal but it would give us the ability to keep support for dinausor compiler and not hamper our ability to take advantage of newer compiler diagnostics.

I agree with Julien. I think we should avoid adding these redundant initialisers for the reasons he gives.

I find this odd, not only because it is contrary to what we've done so far. What if more modern gcc issues a false-positive warning? If we'd fix it there, where would you suggest to draw the line? Imo our tree should build without issues on all compiler versions which we state we permit to be used.

Of course in the case here I could add a "default:" to the switch(), but this would still only work around the compiler issue. Would the two of you consider this any better?

Also, Ian - do you have any alternative suggestion towards making the build work again (in the more general case, i.e. irrespective of the alternative suggestion for this specific case just above)? Not using -Werror on old compilers (again - where would we draw the line) was already objected to by me.

Jan

#9 lore raw

Jan Beulich writes ("Re: [PATCH][4.15] gnttab: work around "may be used uninitialized" warning"):

Quoted 25 lines · Ian Jackson
Quoted 6 lines

Julien Grall writes ("Re: [PATCH][4.15] gnttab: work around "may be used uninitialized" warning"):

Quoted 5 lines

This is pretty much what we are already doing slowly by initializing values to shut up older compilers. I agree this is more limited, but we also waive off diagnostics from every single compiler in that code rather than just one version.

Quoted 7 lines
Quoted 4 lines

Hence why I suggested dropping -Werror for older compiler. This is not ideal but it would give us the ability to keep support for dinausor compiler and not hamper our ability to take advantage of newer compiler diagnostics.

I agree with Julien. I think we should avoid adding these redundant initialisers for the reasons he gives.

I find this odd, not only because it is contrary to what we've done so far. What if more modern gcc issues a false-positive warning? If we'd fix it there, where would you suggest to draw the line? Imo our tree should build without issues on all compiler versions which we state we permit to be used.

Of course in the case here I could add a "default:" to the switch(), but this would still only work around the compiler issue. Would the two of you consider this any better?

Quoted 5 lines

Also, Ian - do you have any alternative suggestion towards making the build work again (in the more general case, i.e. irrespective of the alternative suggestion for this specific case just above)? Not using -Werror on old compilers (again - where would we draw the line) was already objected to by me.

I read your objection to not using -Werror for such old compilers but I did not agree with it.

I am sympathetic to Julien's desire to try to limit the set of supported compilers.

Ian.

#10 lore raw
Quoted 2 lines · Ian Jackson

I am sympathetic to Julien's desire to try to limit the set of supported compilers.

Yes, and I agree we're long overdue with raising the baseline. It's just that it's not straightforward to establish a good new one.

Jan

#11 lore raw
In reply to Ian Jackson
Quoted 14 lines · Ian Jackson

Julien Grall writes ("Re: [PATCH][4.15] gnttab: work around "may be used uninitialized" warning"):

Quoted 9 lines

This is pretty much what we are already doing slowly by initializing values to shut up older compilers. I agree this is more limited, but we also waive off diagnostics from every single compiler in that code rather than just one version.

Hence why I suggested dropping -Werror for older compiler. This is not ideal but it would give us the ability to keep support for dinausor compiler and not hamper our ability to take advantage of newer compiler diagnostics.

I agree with Julien. I think we should avoid adding these redundant initialisers for the reasons he gives.

Quoted 7 lines
Quoted 5 lines

The ideal solution is to drop support for older compiler (see my other thread). But this sounds like a daunting task so far on x86...

Anyway, I will not Nack the patch but will also not Ack it. I will let another maintainer ack this patch.

But this is outside my usual area so I won't nack it either.

But would you be willing to release-ack v2?

Jan

#12 lore raw

Jan Beulich writes ("Re: [PATCH][4.15] gnttab: work around "may be used uninitialized" warning"):

Quoted 4 lines · Ian Jackson
Quoted 1 line

But this is outside my usual area so I won't nack it either.

But would you be willing to release-ack v2?

Good question. I don't think my code quality/style qualms etc. have any bearing on the release question.

So, I will do that now:

Ian.

#13 lore raw
In reply to Jan Beulich
Quoted 9 lines · Jan Beulich

Sadly I was wrong to suggest dropping vaddrs' initializer during review of v2 of the patch introducing this code. gcc 4.3 can't cope.

Fixes 52531c734ea1 ("xen/gnttab: Rework resource acquisition")

xen/common/grant_table.c int gnttab_acquire_resource( · line 4032

Quoted 8 lines
xen/common/grant_table.c +1 -1
@@ -4026,7 +4026,7 @@ int gnttab_acquire_resource(
     struct grant_table *gt = d->grant_table;
     unsigned int i, final_frame;
     mfn_t tmp;
-    void **vaddrs;
+    void **vaddrs = NULL;
     int rc = -EINVAL;
 
     if ( !nr_frames )

in v1, there was a companion check.

xen/common/grant_table.c +0 -0
index f937c1d350..2bb07f129f 100644
@@ -4059,6 +4059,16 @@ int gnttab_acquire_resource(

     if ( rc )
         goto out;

+    /*
+     * Some older toolchains can't spot that vaddrs is non-NULL on non-error
+     * paths.  Leave some runtime safety.
+     */
+    if ( !vaddrs )
+    {
+        ASSERT_UNREACHABLE();
+        goto out;
+    }
+
     for ( i = 0; i < nr_frames; ++i )
         mfn_list[i] = virt_to_mfn(vaddrs[frame + i]);

With this reinstated, Acked-by: Andrew Cooper <[email protected]>

#14 lore raw
Quoted 34 lines · Andrew Cooper
Quoted 9 lines · Jan Beulich

Sadly I was wrong to suggest dropping vaddrs' initializer during review of v2 of the patch introducing this code. gcc 4.3 can't cope.

Fixes 52531c734ea1 ("xen/gnttab: Rework resource acquisition")

xen/common/grant_table.c int gnttab_acquire_resource( · line 4032

Quoted 8 lines
xen/common/grant_table.c +1 -1
@@ -4026,7 +4026,7 @@ int gnttab_acquire_resource(
     struct grant_table *gt = d->grant_table;
     unsigned int i, final_frame;
     mfn_t tmp;
-    void **vaddrs;
+    void **vaddrs = NULL;
     int rc = -EINVAL;
 
     if ( !nr_frames )

in v1, there was a companion check.

xen/common/grant_table.c +0 -0
index f937c1d350..2bb07f129f 100644
@@ -4059,6 +4059,16 @@ int gnttab_acquire_resource(
      if ( rc )
          goto out;


+    /*
+     * Some older toolchains can't spot that vaddrs is non-NULL on non-error
+     * paths.  Leave some runtime safety.
+     */

Quoted 8 lines

+    if ( !vaddrs )
+    {
+        ASSERT_UNREACHABLE();
+        goto out;
+    }
+
     for ( i = 0; i < nr_frames; ++i ) mfn_list[i] = virt_to_mfn(vaddrs[frame + i]);

Oh, I didn't realize this. Will add, but did you really mean to have the function return success in this case (on a release build)? I'd be inclined to put it ahead of if "if ( rc )" and set rc (to e.g. -ENODATA) in this case.

Quoted 1 line

With this reinstated, Acked-by: Andrew Cooper <[email protected]>

Thanks.

Jan

#15 lore raw
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper
xen/common/grant_table.c +0 -0
@@ -4059,6 +4059,16 @@ int gnttab_acquire_resource(
      if ( rc )
          goto out;


+    /*
+     * Some older toolchains can't spot that vaddrs is non-NULL on non-error
+     * paths.  Leave some runtime safety.
+     */
+    if ( !vaddrs )
+    {
+        ASSERT_UNREACHABLE();
+        goto out;
+    }

Quoted 8 lines
Quoted 3 lines

+
     for ( i = 0; i < nr_frames; ++i ) mfn_list[i] = virt_to_mfn(vaddrs[frame + i]);

Oh, I didn't realize this. Will add, but did you really mean to have the function return success in this case (on a release build)? I'd be inclined to put it ahead of if "if ( rc )" and set rc (to e.g. -ENODATA) in this case.

But I think the comment isn't really correct - the problem isn't NULL or not, but uninitialized without setting it to NULL. How about

/*
* Some older toolchains can't spot that vaddrs won't remain uninitialized
* on non-error paths, and hence it needs setting to NULL at the top of the
* function. Leave some runtime safety.
*/

?

Jan

#16 lore raw
Quoted 29 lines · Jan Beulich
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Andrew Cooper
xen/common/grant_table.c +0 -0
@@ -4059,6 +4059,16 @@ int gnttab_acquire_resource(
      if ( rc )
          goto out;


+    /*
+     * Some older toolchains can't spot that vaddrs is non-NULL on non-error
+     * paths.  Leave some runtime safety.
+     */
+    if ( !vaddrs )
+    {
+        ASSERT_UNREACHABLE();
+        goto out;

Quoted 8 lines
Quoted 4 lines

+    }
+
     for ( i = 0; i < nr_frames; ++i ) mfn_list[i] = virt_to_mfn(vaddrs[frame + i]);

Oh, I didn't realize this. Will add, but did you really mean to have the function return success in this case (on a release build)? I'd be inclined to put it ahead of if "if ( rc )" and set rc (to e.g. -ENODATA) in this case.

But I think the comment isn't really correct - the problem isn't NULL or not, but uninitialized without setting it to NULL. How about

Quoted 7 lines

/*
* Some older toolchains can't spot that vaddrs won't remain uninitialized
* on non-error paths, and hence it needs setting to NULL at the top of the
* function. Leave some runtime safety.
*/

?

Yes - that's fine.

~Andrew

#17 lore raw
In reply to Jan Beulich
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Andrew Cooper
Quoted 9 lines · Jan Beulich

Sadly I was wrong to suggest dropping vaddrs' initializer during review of v2 of the patch introducing this code. gcc 4.3 can't cope.

Fixes 52531c734ea1 ("xen/gnttab: Rework resource acquisition")

xen/common/grant_table.c int gnttab_acquire_resource( · line 4032

Quoted 8 lines
xen/common/grant_table.c +1 -1
@@ -4026,7 +4026,7 @@ int gnttab_acquire_resource(
     struct grant_table *gt = d->grant_table;
     unsigned int i, final_frame;
     mfn_t tmp;
-    void **vaddrs;
+    void **vaddrs = NULL;
     int rc = -EINVAL;
 
     if ( !nr_frames )

in v1, there was a companion check.

xen/common/grant_table.c +0 -0
index f937c1d350..2bb07f129f 100644
@@ -4059,6 +4059,16 @@ int gnttab_acquire_resource(
      if ( rc )
          goto out;


+    /*
+     * Some older toolchains can't spot that vaddrs is non-NULL on non-error
+     * paths.  Leave some runtime safety.
+     */
+    if ( !vaddrs )
+    {
+        ASSERT_UNREACHABLE();
+        goto out;

Quoted 8 lines
Quoted 4 lines

+    }
+
     for ( i = 0; i < nr_frames; ++i ) mfn_list[i] = virt_to_mfn(vaddrs[frame + i]);

Oh, I didn't realize this. Will add, but did you really mean to have the function return success in this case (on a release build)? I'd be inclined to put it ahead of if "if ( rc )" and set rc (to e.g. -ENODATA) in this case.

Oh - quite right.  Returning 0 here will hit the assertion/failsafe protecting against livelock.

I'd be tempted to chose -EINVAL because the only plausible way to get here is a bad id, and that path should have errored out earlier.

And yeah, with the rc adjustment, fine to reposition.

~Andrew

#18 lore raw
Quoted 48 lines · Andrew Cooper
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Andrew Cooper
Quoted 9 lines · Jan Beulich

Sadly I was wrong to suggest dropping vaddrs' initializer during review of v2 of the patch introducing this code. gcc 4.3 can't cope.

Fixes 52531c734ea1 ("xen/gnttab: Rework resource acquisition")

xen/common/grant_table.c int gnttab_acquire_resource( · line 4032

Quoted 8 lines
xen/common/grant_table.c +1 -1
@@ -4026,7 +4026,7 @@ int gnttab_acquire_resource(
     struct grant_table *gt = d->grant_table;
     unsigned int i, final_frame;
     mfn_t tmp;
-    void **vaddrs;
+    void **vaddrs = NULL;
     int rc = -EINVAL;
 
     if ( !nr_frames )

in v1, there was a companion check.

xen/common/grant_table.c +0 -0
index f937c1d350..2bb07f129f 100644
@@ -4059,6 +4059,16 @@ int gnttab_acquire_resource(
      if ( rc )
          goto out;


+    /*
+     * Some older toolchains can't spot that vaddrs is non-NULL on non-error
+     * paths.  Leave some runtime safety.
+     */
+    if ( !vaddrs )
+    {
+        ASSERT_UNREACHABLE();
+        goto out;

Quoted 8 lines
Quoted 4 lines

+    }
+
     for ( i = 0; i < nr_frames; ++i ) mfn_list[i] = virt_to_mfn(vaddrs[frame + i]);

Oh, I didn't realize this. Will add, but did you really mean to have the function return success in this case (on a release build)? I'd be inclined to put it ahead of if "if ( rc )" and set rc (to e.g. -ENODATA) in this case.

Quoted 5 lines

Oh - quite right.  Returning 0 here will hit the assertion/failsafe protecting against livelock.

I'd be tempted to chose -EINVAL because the only plausible way to get here is a bad id, and that path should have errored out earlier.

As you may have seen, I've chosen to stick to ENODATA. This error, should it ever get raised, would better be easily distinguishable from an ordinary -EINVAL.

Jan