Skip to contentSkip to conversation

virtio-pci: Remove wrong address verification in vp_del_vqs()

PATCH In review Murilo Opsfelder Araujo 3 participants 8 messages Apr 15, 2022 – Apr 28, 2022 virtio core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-pci: Remove wrong address verification in vp_del_vqs()

GCC 12 enhanced -Waddress when comparing array address to null [0], which warns:

    drivers/virtio/virtio_pci_common.c: In function ‘vp_del_vqs’:
    drivers/virtio/virtio_pci_common.c:257:29: warning: the comparison will always evaluate as ‘true’ for the pointer operand in ‘vp_dev->msix_affinity_masks + (sizetype)((long unsigned int)i * 256)’ must not be NULL [-Waddress]
      257 |                         if (vp_dev->msix_affinity_masks[i])
          |                             ^~~~~~

In fact, the verification is comparing the result of a pointer arithmetic, the address "msix_affinity_masks + i", which will always evaluate to true.

Under the hood, free_cpumask_var() calls kfree(), which is safe to pass NULL, not requiring non-null verification. So remove the verification to make compiler happy (happy compiler, happy life).

[0] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102103

---
 drivers/virtio/virtio_pci_common.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/virtio/virtio_pci_common.c +1 -2
index d724f676608b..5046efcffb4c 100644
@@ -254,8 +254,7 @@ void vp_del_vqs(struct virtio_device *vdev)
 
 	if (vp_dev->msix_affinity_masks) {
 		for (i = 0; i < vp_dev->msix_vectors; i++)
-			if (vp_dev->msix_affinity_masks[i])
-				free_cpumask_var(vp_dev->msix_affinity_masks[i]);
+			free_cpumask_var(vp_dev->msix_affinity_masks[i]);
 	}
 
 	if (vp_dev->msix_enabled) {
Signature · 1 line
2.35.1
#1 lore raw
Quoted 28 lines · Murilo Opsfelder Araujo

GCC 12 enhanced -Waddress when comparing array address to null [0], which warns:

     drivers/virtio/virtio_pci_common.c: In function ‘vp_del_vqs’:
     drivers/virtio/virtio_pci_common.c:257:29: warning: the comparison will always evaluate as ‘true’ for the pointer operand in ‘vp_dev->msix_affinity_masks + (sizetype)((long unsigned int)i * 256)’ must not be NULL [-Waddress]
       257 |                         if (vp_dev->msix_affinity_masks[i])
           |                             ^~~~~~

In fact, the verification is comparing the result of a pointer arithmetic, the address "msix_affinity_masks + i", which will always evaluate to true.

Under the hood, free_cpumask_var() calls kfree(), which is safe to pass NULL, not requiring non-null verification. So remove the verification to make compiler happy (happy compiler, happy life).

[0] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102103

---
  drivers/virtio/virtio_pci_common.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/virtio/virtio_pci_common.c +0 -0
index d724f676608b..5046efcffb4c 100644

drivers/virtio/virtio_pci_common.c void vp_del_vqs(struct virtio_device *vdev) · line 260

Quoted 9 lines
drivers/virtio/virtio_pci_common.c +1 -2
@@ -254,8 +254,7 @@ void vp_del_vqs(struct virtio_device *vdev)
 
  	if (vp_dev->msix_affinity_masks) {
  		for (i = 0; i < vp_dev->msix_vectors; i++)
-			if (vp_dev->msix_affinity_masks[i])
-				free_cpumask_var(vp_dev->msix_affinity_masks[i]);
+			free_cpumask_var(vp_dev->msix_affinity_masks[i]);
  	}
 
  	if (vp_dev->msix_enabled) {

After I sent this message, I realized that Christophe (copied here) had already proposed a fix:

    https://lore.kernel.org/lkml/[email protected]/

Christophe,

Since free_cpumask_var() calls kfree() and kfree() is null-safe, can we just drop this null verification and call free_cpumask_var() right away?

Signature · 1 line
Murilo
#2 lore raw
Quoted 38 lines · Murilo Opsfelder Araújo
Quoted 22 lines · Murilo Opsfelder Araujo

GCC 12 enhanced -Waddress when comparing array address to null [0], which warns:

    drivers/virtio/virtio_pci_common.c: In function ‘vp_del_vqs’:
    drivers/virtio/virtio_pci_common.c:257:29: warning: the comparison will always evaluate as ‘true’ for the pointer operand in ‘vp_dev->msix_affinity_masks + (sizetype)((long unsigned int)i * 256)’ must not be NULL [-Waddress]
      257 |                         if (vp_dev->msix_affinity_masks[i])
          |                             ^~~~~~

In fact, the verification is comparing the result of a pointer arithmetic, the address "msix_affinity_masks + i", which will always evaluate to true.
Under the hood, free_cpumask_var() calls kfree(), which is safe to pass NULL, not requiring non-null verification. So remove the verification to make compiler happy (happy compiler, happy life).
[0] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102103

---
 drivers/virtio/virtio_pci_common.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/virtio/virtio_pci_common.c +0 -0
index d724f676608b..5046efcffb4c 100644

drivers/virtio/virtio_pci_common.c void vp_del_vqs(struct virtio_device *vdev) · line 258

Quoted 7 lines
drivers/virtio/virtio_pci_common.c +1 -2
@@ -254,8 +254,7 @@ void vp_del_vqs(struct virtio_device *vdev)
   	if (vp_dev->msix_affinity_masks) {
 		for (i = 0; i < vp_dev->msix_vectors; i++)
-			if (vp_dev->msix_affinity_masks[i])
-				free_cpumask_var(vp_dev->msix_affinity_masks[i]);
+			free_cpumask_var(vp_dev->msix_affinity_masks[i]);
 	}
   	if (vp_dev->msix_enabled) {

After I sent this message, I realized that Christophe (copied here) had already proposed a fix:

Quoted 6 lines
   https://lore.kernel.org/lkml/[email protected]/

Christophe,

Since free_cpumask_var() calls kfree() and kfree() is null-safe, can we just drop this null verification and call free_cpumask_var() right away?

Apologies for the delay in responding, broken laptop…

In the case where CONFIG_CPUMASK_OFFSTACK is not defined, we have:

	typedef struct cpumask cpumask_var_t[1];

So that vp_dev->msix_affinity_masks[i] is statically not null (that’s the warning) but also a static pointer, so not kfree-safe IMO.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#3 lore raw
Quoted 49 lines · Christophe Marie Francois Dupont de Dinechin
Quoted 39 lines · Murilo Opsfelder Araújo
Quoted 22 lines · Murilo Opsfelder Araujo

GCC 12 enhanced -Waddress when comparing array address to null [0],
which warns:
drivers/virtio/virtio_pci_common.c: In function ‘vp_del_vqs’:
drivers/virtio/virtio_pci_common.c:257:29: warning: the comparison will always evaluate as ‘true’ for the pointer operand in ‘vp_dev->msix_affinity_masks + (sizetype)((long unsigned int)i * 256)’ must not be NULL [-Waddress] 257 | if (vp_dev->msix_affinity_masks[i])
| ^~~~~~
In fact, the verification is comparing the result of a pointer
arithmetic, the address "msix_affinity_masks + i", which will always
evaluate to true.
Under the hood, free_cpumask_var() calls kfree(), which is safe to pass
NULL, not requiring non-null verification. So remove the verification
to make compiler happy (happy compiler, happy life).
[0] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102103

---
drivers/virtio/virtio_pci_common.c | 3 +--

1 file changed, 1 insertion(+), 2 deletions(-)
drivers/virtio/virtio_pci_common.c +0 -0
index d724f676608b..5046efcffb4c 100644

drivers/virtio/virtio_pci_common.c void vp_del_vqs(struct virtio_device *vdev) · line 258

Quoted 7 lines
drivers/virtio/virtio_pci_common.c +1 -2
@@ -254,8 +254,7 @@ void vp_del_vqs(struct virtio_device *vdev)
 	if (vp_dev->msix_affinity_masks) {
 		for (i = 0; i < vp_dev->msix_vectors; i++)
-			if (vp_dev->msix_affinity_masks[i])
-				free_cpumask_var(vp_dev->msix_affinity_masks[i]);
+			free_cpumask_var(vp_dev->msix_affinity_masks[i]);
 	}
 	if (vp_dev->msix_enabled) {

After I sent this message, I realized that Christophe (copied here) had already proposed a fix:

Quoted 6 lines

https://lore.kernel.org/lkml/[email protected]/

Christophe,

Since free_cpumask_var() calls kfree() and kfree() is null-safe, can we just drop this null verification and call free_cpumask_var() right away?

Quoted 8 lines

Apologies for the delay in responding, broken laptop…

In the case where CONFIG_CPUMASK_OFFSTACK is not defined, we have:

	typedef struct cpumask cpumask_var_t[1];

So that vp_dev->msix_affinity_masks[i] is statically not null (that’s the warning) but also a static pointer, so not kfree-safe IMO.

… which also renders my own patch invalid :-/

Compiler warnings are good. Clearly not sufficient.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#4 lore raw
Quoted 57 lines · Christophe Marie Francois Dupont de Dinechin
Quoted 54 lines · Christophe Marie Francois Dupont de Dinechin
Quoted 39 lines · Murilo Opsfelder Araújo
Quoted 22 lines · Murilo Opsfelder Araujo

GCC 12 enhanced -Waddress when comparing array address to null [0],
which warns:
drivers/virtio/virtio_pci_common.c: In function ‘vp_del_vqs’:
drivers/virtio/virtio_pci_common.c:257:29: warning: the comparison will always evaluate as ‘true’ for the pointer operand in ‘vp_dev->msix_affinity_masks + (sizetype)((long unsigned int)i * 256)’ must not be NULL [-Waddress] 257 | if (vp_dev->msix_affinity_masks[i])
| ^~~~~~
In fact, the verification is comparing the result of a pointer
arithmetic, the address "msix_affinity_masks + i", which will always
evaluate to true.
Under the hood, free_cpumask_var() calls kfree(), which is safe to pass
NULL, not requiring non-null verification. So remove the verification
to make compiler happy (happy compiler, happy life).
[0] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102103

---
drivers/virtio/virtio_pci_common.c | 3 +--

1 file changed, 1 insertion(+), 2 deletions(-)
drivers/virtio/virtio_pci_common.c +0 -0
index d724f676608b..5046efcffb4c 100644

drivers/virtio/virtio_pci_common.c void vp_del_vqs(struct virtio_device *vdev) · line 258

Quoted 7 lines
drivers/virtio/virtio_pci_common.c +1 -2
@@ -254,8 +254,7 @@ void vp_del_vqs(struct virtio_device *vdev)
 	if (vp_dev->msix_affinity_masks) {
 		for (i = 0; i < vp_dev->msix_vectors; i++)
-			if (vp_dev->msix_affinity_masks[i])
-				free_cpumask_var(vp_dev->msix_affinity_masks[i]);
+			free_cpumask_var(vp_dev->msix_affinity_masks[i]);
 	}
 	if (vp_dev->msix_enabled) {

After I sent this message, I realized that Christophe (copied here) had already proposed a fix:

Quoted 6 lines

https://lore.kernel.org/lkml/[email protected]/

Christophe,

Since free_cpumask_var() calls kfree() and kfree() is null-safe, can we just drop this null verification and call free_cpumask_var() right away?

Apologies for the delay in responding, broken laptop…

In the case where CONFIG_CPUMASK_OFFSTACK is not defined, we have:

Quoted 8 lines
Quoted 4 lines
	typedef struct cpumask cpumask_var_t[1];

So that vp_dev->msix_affinity_masks[i] is statically not null (that’s the warning) but also a static pointer, so not kfree-safe IMO.

… which also renders my own patch invalid :-/

Compiler warnings are good. Clearly not sufficient.

Ah, I just noticed that free_cpumask_var is a noop in that case.

So yes, your fix is better :-)

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#5 lore raw
Quoted 62 lines · Christophe Marie Francois Dupont de Dinechin
Quoted 62 lines · Christophe Marie Francois Dupont de Dinechin
Quoted 58 lines · Christophe Marie Francois Dupont de Dinechin
Quoted 39 lines · Murilo Opsfelder Araújo
Quoted 22 lines · Murilo Opsfelder Araujo

GCC 12 enhanced -Waddress when comparing array address to null [0],
which warns:
drivers/virtio/virtio_pci_common.c: In function ‘vp_del_vqs’:
drivers/virtio/virtio_pci_common.c:257:29: warning: the comparison will always evaluate as ‘true’ for the pointer operand in ‘vp_dev->msix_affinity_masks + (sizetype)((long unsigned int)i * 256)’ must not be NULL [-Waddress] 257 | if (vp_dev->msix_affinity_masks[i])
| ^~~~~~
In fact, the verification is comparing the result of a pointer
arithmetic, the address "msix_affinity_masks + i", which will always
evaluate to true.
Under the hood, free_cpumask_var() calls kfree(), which is safe to pass
NULL, not requiring non-null verification. So remove the verification
to make compiler happy (happy compiler, happy life).
[0] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102103

---
drivers/virtio/virtio_pci_common.c | 3 +--

1 file changed, 1 insertion(+), 2 deletions(-)
drivers/virtio/virtio_pci_common.c +0 -0
index d724f676608b..5046efcffb4c 100644

drivers/virtio/virtio_pci_common.c void vp_del_vqs(struct virtio_device *vdev) · line 258

Quoted 7 lines
drivers/virtio/virtio_pci_common.c +1 -2
@@ -254,8 +254,7 @@ void vp_del_vqs(struct virtio_device *vdev)
 	if (vp_dev->msix_affinity_masks) {
 		for (i = 0; i < vp_dev->msix_vectors; i++)
-			if (vp_dev->msix_affinity_masks[i])
-				free_cpumask_var(vp_dev->msix_affinity_masks[i]);
+			free_cpumask_var(vp_dev->msix_affinity_masks[i]);
 	}
 	if (vp_dev->msix_enabled) {

After I sent this message, I realized that Christophe (copied here) had already proposed a fix:

Quoted 6 lines

https://lore.kernel.org/lkml/[email protected]/

Christophe,

Since free_cpumask_var() calls kfree() and kfree() is null-safe, can we just drop this null verification and call free_cpumask_var() right away?

Apologies for the delay in responding, broken laptop…

In the case where CONFIG_CPUMASK_OFFSTACK is not defined, we have:

	typedef struct cpumask cpumask_var_t[1];

So that vp_dev->msix_affinity_masks[i] is statically not null (that’s the warning) but also a static pointer, so not kfree-safe IMO.

Quoted 7 lines
Quoted 3 lines

… which also renders my own patch invalid :-/

Compiler warnings are good. Clearly not sufficient.

Ah, I just noticed that free_cpumask_var is a noop in that case.

So yes, your fix is better :-)

ACK then?

Acked-by Christophe de Dinechin
#6 lore raw
Quoted 69 lines · Michael S. Tsirkin
Quoted 64 lines · Christophe Marie Francois Dupont de Dinechin
Quoted 64 lines · Christophe Marie Francois Dupont de Dinechin
Quoted 50 lines · Christophe Marie Francois Dupont de Dinechin
Quoted 39 lines · Murilo Opsfelder Araújo
Quoted 22 lines · Murilo Opsfelder Araujo

GCC 12 enhanced -Waddress when comparing array address to null [0],
which warns:
drivers/virtio/virtio_pci_common.c: In function ‘vp_del_vqs’:
drivers/virtio/virtio_pci_common.c:257:29: warning: the comparison will always evaluate as ‘true’ for the pointer operand in ‘vp_dev->msix_affinity_masks + (sizetype)((long unsigned int)i * 256)’ must not be NULL [-Waddress] 257 | if (vp_dev->msix_affinity_masks[i])
| ^~~~~~
In fact, the verification is comparing the result of a pointer
arithmetic, the address "msix_affinity_masks + i", which will always
evaluate to true.
Under the hood, free_cpumask_var() calls kfree(), which is safe to pass
NULL, not requiring non-null verification. So remove the verification
to make compiler happy (happy compiler, happy life).
[0] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102103

---
drivers/virtio/virtio_pci_common.c | 3 +--

1 file changed, 1 insertion(+), 2 deletions(-)
drivers/virtio/virtio_pci_common.c +0 -0
index d724f676608b..5046efcffb4c 100644

drivers/virtio/virtio_pci_common.c void vp_del_vqs(struct virtio_device *vdev) · line 258

Quoted 7 lines
drivers/virtio/virtio_pci_common.c +1 -2
@@ -254,8 +254,7 @@ void vp_del_vqs(struct virtio_device *vdev)
 	if (vp_dev->msix_affinity_masks) {
 		for (i = 0; i < vp_dev->msix_vectors; i++)
-			if (vp_dev->msix_affinity_masks[i])
-				free_cpumask_var(vp_dev->msix_affinity_masks[i]);
+			free_cpumask_var(vp_dev->msix_affinity_masks[i]);
 	}
 	if (vp_dev->msix_enabled) {

After I sent this message, I realized that Christophe (copied here) had already proposed a fix:

Quoted 6 lines

https://lore.kernel.org/lkml/[email protected]/

Christophe,

Since free_cpumask_var() calls kfree() and kfree() is null-safe, can we just drop this null verification and call free_cpumask_var() right away?

Quoted 8 lines

Apologies for the delay in responding, broken laptop…

In the case where CONFIG_CPUMASK_OFFSTACK is not defined, we have:

	typedef struct cpumask cpumask_var_t[1];

So that vp_dev->msix_affinity_masks[i] is statically not null (that’s the warning) but also a static pointer, so not kfree-safe IMO.

… which also renders my own patch invalid :-/

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Compiler warnings are good. Clearly not sufficient.

Ah, I just noticed that free_cpumask_var is a noop in that case.

So yes, your fix is better :-)

ACK then?

Yes.

Acked-by Christophe de Dinechin <[email protected] <mailto:[email protected]>>
Quoted 1 line

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Acked-by Christophe de Dinechin
#7 lore raw
In reply to Michael S. Tsirkin

[Resend, still struggling with new laptop email settings]

Quoted 67 lines · Michael S. Tsirkin
Quoted 64 lines · Christophe Marie Francois Dupont de Dinechin
Quoted 64 lines · Christophe Marie Francois Dupont de Dinechin
Quoted 50 lines · Christophe Marie Francois Dupont de Dinechin
Quoted 39 lines · Murilo Opsfelder Araújo
Quoted 22 lines · Murilo Opsfelder Araujo

GCC 12 enhanced -Waddress when comparing array address to null [0],
which warns:
drivers/virtio/virtio_pci_common.c: In function ‘vp_del_vqs’:
drivers/virtio/virtio_pci_common.c:257:29: warning: the comparison will always evaluate as ‘true’ for the pointer operand in ‘vp_dev->msix_affinity_masks + (sizetype)((long unsigned int)i * 256)’ must not be NULL [-Waddress] 257 | if (vp_dev->msix_affinity_masks[i])
| ^~~~~~
In fact, the verification is comparing the result of a pointer
arithmetic, the address "msix_affinity_masks + i", which will always
evaluate to true.
Under the hood, free_cpumask_var() calls kfree(), which is safe to pass
NULL, not requiring non-null verification. So remove the verification
to make compiler happy (happy compiler, happy life).
[0] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102103

---
drivers/virtio/virtio_pci_common.c | 3 +--

1 file changed, 1 insertion(+), 2 deletions(-)
drivers/virtio/virtio_pci_common.c +0 -0
index d724f676608b..5046efcffb4c 100644

drivers/virtio/virtio_pci_common.c void vp_del_vqs(struct virtio_device *vdev) · line 258

Quoted 7 lines
drivers/virtio/virtio_pci_common.c +1 -2
@@ -254,8 +254,7 @@ void vp_del_vqs(struct virtio_device *vdev)
 	if (vp_dev->msix_affinity_masks) {
 		for (i = 0; i < vp_dev->msix_vectors; i++)
-			if (vp_dev->msix_affinity_masks[i])
-				free_cpumask_var(vp_dev->msix_affinity_masks[i]);
+			free_cpumask_var(vp_dev->msix_affinity_masks[i]);
 	}
 	if (vp_dev->msix_enabled) {

After I sent this message, I realized that Christophe (copied here) had already proposed a fix:

Quoted 6 lines

https://lore.kernel.org/lkml/[email protected]/

Christophe,

Since free_cpumask_var() calls kfree() and kfree() is null-safe, can we just drop this null verification and call free_cpumask_var() right away?

Quoted 8 lines

Apologies for the delay in responding, broken laptop…

In the case where CONFIG_CPUMASK_OFFSTACK is not defined, we have:

	typedef struct cpumask cpumask_var_t[1];

So that vp_dev->msix_affinity_masks[i] is statically not null (that’s the warning) but also a static pointer, so not kfree-safe IMO.

… which also renders my own patch invalid :-/

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Compiler warnings are good. Clearly not sufficient.

Ah, I just noticed that free_cpumask_var is a noop in that case.

So yes, your fix is better :-)

ACK then?

Yes.

Quoted 1 line