Skip to contentSkip to conversation

platform/mellanox: mlxbf-tmfifo: Fix a warning message

PATCH Superseded Liming Sun 2 participants 9 messages Oct 5, 2023 – Oct 18, 2023

Subsystems (2)
lore
Share & tools
lore

Liming Sun

author
#0 lore raw

PATCH 1/1 platform/mellanox: mlxbf-tmfifo: Fix a warning message

This commit fixes the smatch static checker warning in mlxbf_tmfifo_rxtx_word() which complains data not initialized at line 634 when IS_VRING_DROP() is TRUE. This is not a real bug since line 634 is for Tx while IS_VRING_DROP() is only set for Rx. So there is no case that line 634 is executed when IS_VRING_DROP() is TRUE.

This commit initializes the local data variable to avoid unnecessary confusion to those static analyzing tools.

---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -1
index f3696a54a2bd..ccc4b51d3379 100644
@@ -595,8 +595,8 @@ static void mlxbf_tmfifo_rxtx_word(struct mlxbf_tmfifo_vring *vring,
 {
 	struct virtio_device *vdev = vring->vq->vdev;
 	struct mlxbf_tmfifo *fifo = vring->fifo;
+	u64 data = 0;
 	void *addr;
-	u64 data;
 
 	/* Get the buffer address of this desc. */
 	addr = phys_to_virt(virtio64_to_cpu(vdev, desc->addr));
Signature · 1 line
2.30.1
#1 lore raw

Hi Liming,

Quoted 19 lines · Liming Sun

This commit fixes the smatch static checker warning in mlxbf_tmfifo_rxtx_word() which complains data not initialized at line 634 when IS_VRING_DROP() is TRUE. This is not a real bug since line 634 is for Tx while IS_VRING_DROP() is only set for Rx. So there is no case that line 634 is executed when IS_VRING_DROP() is TRUE.

This commit initializes the local data variable to avoid unnecessary confusion to those static analyzing tools.

---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/platform/mellanox/mlxbf-tmfifo.c +0 -0
index f3696a54a2bd..ccc4b51d3379 100644

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx_word(struct mlxbf_tmfifo_vring *vring, · line 602

Quoted 9 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -1
@@ -595,8 +595,8 @@ static void mlxbf_tmfifo_rxtx_word(struct mlxbf_tmfifo_vring *vring,
 {
 	struct virtio_device *vdev = vring->vq->vdev;
 	struct mlxbf_tmfifo *fifo = vring->fifo;
+	u64 data = 0;
 	void *addr;
-	u64 data;
 
 	/* Get the buffer address of this desc. */
 	addr = phys_to_virt(virtio64_to_cpu(vdev, desc->addr));

This will fix the warning but not the issue at hand. As Dan pointed out in his original bug report, the issue is that after:

78034cbece79 ("platform/mellanox: mlxbf-tmfifo: Drop the Rx packet if no descriptors")

We now have this IS_VRING_DROP() check in the path, which despite
the subject writeq(data, fifo->tx.data);is currently being applied to both rx and tx vring-s and when this returns true the memcpy from the ring to &data
will not happen, but the code will still do:

writeq(data, fifo->tx.data);

So you may have silenced the warning now, but you will still write data not coming from the vring to transmit. The only difference is you are now guaranteed to write all zeroes.

Note another older issue is that if you hit the not enough space path:

       } else {
                /* Leftover bytes. */
                if (!IS_VRING_DROP(vring)) {
                        if (is_rx)
                                memcpy(addr + vring->cur_len, &data,
                                       len - vring->cur_len);
                        else
                                memcpy(&data, addr + vring->cur_len,
                                       len - vring->cur_len);
                }
                vring->cur_len = len;
        }

Then even if IS_VRING_DROP() returns true you are only initializing some bytes of the 8 bytes data variable and the other bytes will stay at whatever random value they had before and you end up writing this random bytes when doing:

writeq(data, fifo->tx.data);

Regards,

Hans

author
#2 lore raw

Thanks Hans.

Below is the logic:

IS_VRING_DROP() is ONLY set to TRUE for Rx, which is done in two places:
Line 696: *desc = &vring->drop_desc;
Line 742: desc = &vring->drop_desc;

So line 634 below will never happen when IS_VRING_DROP() is TRUE due the checking of line 633.
633 if (!is_rx)
634 writeq(data, fifo->tx.data);

Please correct me if it's my misunderstanding.

Thanks,
Liming

Quoted 91 lines

-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Friday, October 6, 2023 8:54 AM
To: Liming Sun <[email protected]>; Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v1 1/1] platform/mellanox: mlxbf-tmfifo: Fix a warning message

Hi Liming,

Quoted 9 lines · Liming Sun

This commit fixes the smatch static checker warning in mlxbf_tmfifo_rxtx_word() which complains data not initialized at line 634 when IS_VRING_DROP() is TRUE. This is not a real bug since line 634 is for Tx while IS_VRING_DROP() is only set for Rx. So there is no case that line 634 is executed when IS_VRING_DROP() is TRUE.

This commit initializes the local data variable to avoid unnecessary confusion to those static analyzing tools.

Review

Quoted 6 lines
---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/platform/mellanox/mlxbf-tmfifo.c

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx_word(struct

Quoted 4 lines

index f3696a54a2bd..ccc4b51d3379 100644

drivers/platform/mellanox/mlxbf-tmfifo.c +0 -0
@@ -595,8 +595,8 @@ static void mlxbf_tmfifo_rxtx_word(struct

mlxbf_tmfifo_vring *vring,

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx_word(struct mlxbf_tmfifo_vring *vring, · line 602

Quoted 9 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -1
 {
 	struct virtio_device *vdev = vring->vq->vdev;
 	struct mlxbf_tmfifo *fifo = vring->fifo;
+	u64 data = 0;
 	void *addr;
-	u64 data;
 
 	/* Get the buffer address of this desc. */
 	addr = phys_to_virt(virtio64_to_cpu(vdev, desc->addr));

This will fix the warning but not the issue at hand. As Dan pointed out in his original bug report, the issue is that after:

78034cbece79 ("platform/mellanox: mlxbf-tmfifo: Drop the Rx packet if no descriptors")

We now have this IS_VRING_DROP() check in the path, which despite the subject writeq(data, fifo->tx.data);is currently being applied to both rx and tx vring-s
and when this returns true the memcpy from the ring to &data
will not happen, but the code will still do:

writeq(data, fifo->tx.data);

So you may have silenced the warning now, but you will still write data not coming from the vring to transmit. The only difference is you are now guaranteed to write all zeroes.

Note another older issue is that if you hit the not enough space path:

       } else {
                /* Leftover bytes. */
                if (!IS_VRING_DROP(vring)) {
                        if (is_rx)
                                memcpy(addr + vring->cur_len, &data,
                                       len - vring->cur_len);
                        else
                                memcpy(&data, addr + vring->cur_len,
                                       len - vring->cur_len);
                }
                vring->cur_len = len;
        }

Then even if IS_VRING_DROP() returns true you are only initializing some bytes of the 8 bytes data variable and the other bytes will stay at whatever random value they had before and you end up writing this random bytes when doing:

writeq(data, fifo->tx.data);

Regards,

Hans

#3 lore raw

Hi Liming,

Quoted 8 lines · Liming Sun

Thanks Hans.

Below is the logic:

IS_VRING_DROP() is ONLY set to TRUE for Rx, which is done in two places:
Line 696: *desc = &vring->drop_desc;
Line 742: desc = &vring->drop_desc;

Quoted 5 lines

So line 634 below will never happen when IS_VRING_DROP() is TRUE due the checking of line 633.
633 if (!is_rx)
634 writeq(data, fifo->tx.data);

Please correct me if it's my misunderstanding.

If IS_VRING_DROP() is ONLY set to TRUE for Rx, then it should simply *not* be checked *at all* in the tx paths.

Just setting data = 0 is simply papering over the warning without actually fixing anything.

Regards,

Hans

Quoted 92 lines
Quoted 91 lines

-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Friday, October 6, 2023 8:54 AM
To: Liming Sun <[email protected]>; Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v1 1/1] platform/mellanox: mlxbf-tmfifo: Fix a warning message

Hi Liming,

Quoted 9 lines · Liming Sun

This commit fixes the smatch static checker warning in mlxbf_tmfifo_rxtx_word() which complains data not initialized at line 634 when IS_VRING_DROP() is TRUE. This is not a real bug since line 634 is for Tx while IS_VRING_DROP() is only set for Rx. So there is no case that line 634 is executed when IS_VRING_DROP() is TRUE.

This commit initializes the local data variable to avoid unnecessary confusion to those static analyzing tools.

Review

Quoted 6 lines
---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/platform/mellanox/mlxbf-tmfifo.c

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx_word(struct

Quoted 4 lines

index f3696a54a2bd..ccc4b51d3379 100644

drivers/platform/mellanox/mlxbf-tmfifo.c +0 -0
@@ -595,8 +595,8 @@ static void mlxbf_tmfifo_rxtx_word(struct

mlxbf_tmfifo_vring *vring,

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx_word(struct mlxbf_tmfifo_vring *vring, · line 602

Quoted 9 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -1
 {
 	struct virtio_device *vdev = vring->vq->vdev;
 	struct mlxbf_tmfifo *fifo = vring->fifo;
+	u64 data = 0;
 	void *addr;
-	u64 data;
 
 	/* Get the buffer address of this desc. */
 	addr = phys_to_virt(virtio64_to_cpu(vdev, desc->addr));

This will fix the warning but not the issue at hand. As Dan pointed out in his original bug report, the issue is that after:

78034cbece79 ("platform/mellanox: mlxbf-tmfifo: Drop the Rx packet if no descriptors")

We now have this IS_VRING_DROP() check in the path, which despite the subject writeq(data, fifo->tx.data);is currently being applied to both rx and tx vring-s
and when this returns true the memcpy from the ring to &data
will not happen, but the code will still do:

writeq(data, fifo->tx.data);

So you may have silenced the warning now, but you will still write data not coming from the vring to transmit. The only difference is you are now guaranteed to write all zeroes.

Note another older issue is that if you hit the not enough space path:

       } else {
                /* Leftover bytes. */
                if (!IS_VRING_DROP(vring)) {
                        if (is_rx)
                                memcpy(addr + vring->cur_len, &data,
                                       len - vring->cur_len);
                        else
                                memcpy(&data, addr + vring->cur_len,
                                       len - vring->cur_len);
                }
                vring->cur_len = len;
        }

Then even if IS_VRING_DROP() returns true you are only initializing some bytes of the 8 bytes data variable and the other bytes will stay at whatever random value they had before and you end up writing this random bytes when doing:

writeq(data, fifo->tx.data);

Regards,

Hans

author
#4 lore raw
Quoted 24 lines

-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Friday, October 6, 2023 1:07 PM
To: Liming Sun <[email protected]>; Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v1 1/1] platform/mellanox: mlxbf-tmfifo: Fix a warning message

Hi Liming,

Quoted 9 lines · Liming Sun

Thanks Hans.

Below is the logic:

IS_VRING_DROP() is ONLY set to TRUE for Rx, which is done in two places:
Line 696: *desc = &vring->drop_desc;
Line 742: desc = &vring->drop_desc;

So line 634 below will never happen when IS_VRING_DROP() is TRUE due the

Quoted 8 lines

checking of line 633.

Quoted 4 lines

633 if (!is_rx)
634 writeq(data, fifo->tx.data);

Please correct me if it's my misunderstanding.

If IS_VRING_DROP() is ONLY set to TRUE for Rx, then it should simply *not* be checked *at all* in the tx paths.

IS_VRING_DROP() itself is actually not checked in the Tx path. It is the "! IS_VRING_DROP()" that checks the Rx/Tx, something like:

if (!IS_VRING_DROP(vring)) {
	if (is_rx)
		...
	else
		...
}

The reason is that I thought we might reuse the ' IS_VRING_DROP' for Tx later.

However, if the logic looks confusing, I could revise it to something like:

if (is_rx) {
	if (!IS_VRING_DROP(vring)) 
		...
} else {
		...
}

Thanks.

Quoted 108 lines

Just setting data = 0 is simply papering over the warning without actually fixing anything.

Regards,

Hans

Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Friday, October 6, 2023 8:54 AM
To: Liming Sun <[email protected]>; Vadim Pasternak <[email protected]>; David Thompson <[email protected]>;

Mark

Quoted 43 lines
Quoted 43 lines

Gross <[email protected]>; Dan Carpenter <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v1 1/1] platform/mellanox: mlxbf-tmfifo: Fix a warning message

Hi Liming,

Quoted 9 lines · Liming Sun

This commit fixes the smatch static checker warning in mlxbf_tmfifo_rxtx_word() which complains data not initialized at line 634 when IS_VRING_DROP() is TRUE. This is not a real bug since line 634 is for Tx while IS_VRING_DROP() is only set for Rx. So there is no case that line 634 is executed when IS_VRING_DROP() is TRUE.

This commit initializes the local data variable to avoid unnecessary confusion to those static analyzing tools.

Review

Quoted 6 lines
---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/platform/mellanox/mlxbf-tmfifo.c

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx_word(struct

Quoted 4 lines

index f3696a54a2bd..ccc4b51d3379 100644

drivers/platform/mellanox/mlxbf-tmfifo.c +0 -0
@@ -595,8 +595,8 @@ static void mlxbf_tmfifo_rxtx_word(struct

mlxbf_tmfifo_vring *vring,

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx_word(struct mlxbf_tmfifo_vring *vring, · line 602

Quoted 9 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -1
 {
 	struct virtio_device *vdev = vring->vq->vdev;
 	struct mlxbf_tmfifo *fifo = vring->fifo;
+	u64 data = 0;
 	void *addr;
-	u64 data;
 
 	/* Get the buffer address of this desc. */
 	addr = phys_to_virt(virtio64_to_cpu(vdev, desc->addr));

This will fix the warning but not the issue at hand. As Dan pointed out in his original bug report, the issue is that after:

Quoted 5 lines
Quoted 5 lines

78034cbece79 ("platform/mellanox: mlxbf-tmfifo: Drop the Rx packet if no descriptors")

We now have this IS_VRING_DROP() check in the path, which despite the subject writeq(data, fifo->tx.data);is currently being applied to both rx

and

Quoted 26 lines
Quoted 26 lines

tx vring-s
and when this returns true the memcpy from the ring to &data will not happen, but the code will still do:

writeq(data, fifo->tx.data);

So you may have silenced the warning now, but you will still write data not coming from the vring to transmit. The only difference is you are now guaranteed to write all zeroes.

Note another older issue is that if you hit the not enough space path:

       } else {
                /* Leftover bytes. */
                if (!IS_VRING_DROP(vring)) {
                        if (is_rx)
                                memcpy(addr + vring->cur_len, &data,
                                       len - vring->cur_len);
                        else
                                memcpy(&data, addr + vring->cur_len,
                                       len - vring->cur_len);
                }
                vring->cur_len = len;
        }
Quoted 1 line
Quoted 1 line

Then even if IS_VRING_DROP() returns true you are only initializing some

bytes

Quoted 1 line
Quoted 1 line

of the 8 bytes data variable and the other bytes will stay at whatever

random

Quoted 1 line
Quoted 1 line

value they had before and you end up writing this random bytes when

doing:

Quoted 10 lines
Quoted 9 lines

writeq(data, fifo->tx.data);

Regards,

Hans

#5 lore raw

Hi,

Quoted 44 lines · Liming Sun
Quoted 24 lines

-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Friday, October 6, 2023 1:07 PM
To: Liming Sun <[email protected]>; Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v1 1/1] platform/mellanox: mlxbf-tmfifo: Fix a warning message

Hi Liming,

Quoted 9 lines · Liming Sun

Thanks Hans.

Below is the logic:

IS_VRING_DROP() is ONLY set to TRUE for Rx, which is done in two places:
Line 696: *desc = &vring->drop_desc;
Line 742: desc = &vring->drop_desc;

So line 634 below will never happen when IS_VRING_DROP() is TRUE due the

Quoted 8 lines

checking of line 633.

Quoted 4 lines

633 if (!is_rx)
634 writeq(data, fifo->tx.data);

Please correct me if it's my misunderstanding.

If IS_VRING_DROP() is ONLY set to TRUE for Rx, then it should simply *not* be checked *at all* in the tx paths.

IS_VRING_DROP() itself is actually not checked in the Tx path. It is the "! IS_VRING_DROP()" that checks the Rx/Tx, something like:

if (!IS_VRING_DROP(vring)) {
	if (is_rx)
		...
	else
		...
}

The reason is that I thought we might reuse the ' IS_VRING_DROP' for Tx later.

Quoted 8 lines

However, if the logic looks confusing, I could revise it to something like:

if (is_rx) {
	if (!IS_VRING_DROP(vring)) 
		...
} else {
		...
}

Yes please revise the log to look like this, this should also fix the warning without needing to initialize data to 0.

Since now in the tx path you are guaranteed to first fill data before sending it.

Regards,

Hans

Quoted 98 lines
Quoted 97 lines
Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Friday, October 6, 2023 8:54 AM
To: Liming Sun <[email protected]>; Vadim Pasternak <[email protected]>; David Thompson <[email protected]>;

Mark

Quoted 43 lines
Quoted 43 lines

Gross <[email protected]>; Dan Carpenter <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v1 1/1] platform/mellanox: mlxbf-tmfifo: Fix a warning message

Hi Liming,

Quoted 9 lines · Liming Sun

This commit fixes the smatch static checker warning in mlxbf_tmfifo_rxtx_word() which complains data not initialized at line 634 when IS_VRING_DROP() is TRUE. This is not a real bug since line 634 is for Tx while IS_VRING_DROP() is only set for Rx. So there is no case that line 634 is executed when IS_VRING_DROP() is TRUE.

This commit initializes the local data variable to avoid unnecessary confusion to those static analyzing tools.

Review

Quoted 6 lines
---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/platform/mellanox/mlxbf-tmfifo.c

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx_word(struct

Quoted 4 lines

index f3696a54a2bd..ccc4b51d3379 100644

drivers/platform/mellanox/mlxbf-tmfifo.c +0 -0
@@ -595,8 +595,8 @@ static void mlxbf_tmfifo_rxtx_word(struct

mlxbf_tmfifo_vring *vring,

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx_word(struct mlxbf_tmfifo_vring *vring, · line 602

Quoted 9 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -1
 {
 	struct virtio_device *vdev = vring->vq->vdev;
 	struct mlxbf_tmfifo *fifo = vring->fifo;
+	u64 data = 0;
 	void *addr;
-	u64 data;
 
 	/* Get the buffer address of this desc. */
 	addr = phys_to_virt(virtio64_to_cpu(vdev, desc->addr));

This will fix the warning but not the issue at hand. As Dan pointed out in his original bug report, the issue is that after:

Quoted 5 lines
Quoted 5 lines

78034cbece79 ("platform/mellanox: mlxbf-tmfifo: Drop the Rx packet if no descriptors")

We now have this IS_VRING_DROP() check in the path, which despite the subject writeq(data, fifo->tx.data);is currently being applied to both rx

and

Quoted 26 lines
Quoted 26 lines

tx vring-s
and when this returns true the memcpy from the ring to &data will not happen, but the code will still do:

writeq(data, fifo->tx.data);

So you may have silenced the warning now, but you will still write data not coming from the vring to transmit. The only difference is you are now guaranteed to write all zeroes.

Note another older issue is that if you hit the not enough space path:

       } else {
                /* Leftover bytes. */
                if (!IS_VRING_DROP(vring)) {
                        if (is_rx)
                                memcpy(addr + vring->cur_len, &data,
                                       len - vring->cur_len);
                        else
                                memcpy(&data, addr + vring->cur_len,
                                       len - vring->cur_len);
                }
                vring->cur_len = len;
        }
Quoted 1 line
Quoted 1 line

Then even if IS_VRING_DROP() returns true you are only initializing some

bytes

Quoted 1 line
Quoted 1 line

of the 8 bytes data variable and the other bytes will stay at whatever

random

Quoted 1 line
Quoted 1 line

value they had before and you end up writing this random bytes when

doing:

Quoted 10 lines
Quoted 9 lines

writeq(data, fifo->tx.data);

Regards,

Hans

author
#6 lore raw
Quoted 72 lines

-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Tuesday, October 10, 2023 4:06 AM
To: Liming Sun <[email protected]>; Vadim Pasternak <[email protected]>; David Thompson <[email protected]>; Mark Gross <[email protected]>; Dan Carpenter <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v1 1/1] platform/mellanox: mlxbf-tmfifo: Fix a warning message

Hi,

Quoted 7 lines · Liming Sun
Quoted 7 lines

-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Friday, October 6, 2023 1:07 PM
To: Liming Sun <[email protected]>; Vadim Pasternak <[email protected]>; David Thompson <[email protected]>;

Mark

Quoted 10 lines
Quoted 10 lines

Gross <[email protected]>; Dan Carpenter <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v1 1/1] platform/mellanox: mlxbf-tmfifo: Fix a warning message

Hi Liming,

Quoted 2 lines · Liming Sun

Thanks Hans.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Below is the logic:

IS_VRING_DROP() is ONLY set to TRUE for Rx, which is done in two places:
Line 696: *desc = &vring->drop_desc;
Line 742: desc = &vring->drop_desc;

So line 634 below will never happen when IS_VRING_DROP() is TRUE due

the

Quoted 10 lines
Quoted 8 lines

checking of line 633.

Quoted 4 lines

633 if (!is_rx)
634 writeq(data, fifo->tx.data);

Please correct me if it's my misunderstanding.

If IS_VRING_DROP() is ONLY set to TRUE for Rx, then it should simply *not* be checked *at all* in the tx paths.

IS_VRING_DROP() itself is actually not checked in the Tx path. It is the "!

IS_VRING_DROP()" that checks the Rx/Tx, something like:

Quoted 9 lines
if (!IS_VRING_DROP(vring)) {
	if (is_rx)
		...
	else
		...
}

The reason is that I thought we might reuse the ' IS_VRING_DROP' for Tx

later.

Quoted 9 lines

However, if the logic looks confusing, I could revise it to something like:

if (is_rx) {
	if (!IS_VRING_DROP(vring))
		...
} else {
		...
}
Quoted 5 lines

Yes please revise the log to look like this, this should also fix the warning without needing to initialize data to 0.

Since now in the tx path you are guaranteed to first fill data before sending it.

Thanks, done. Updated in v2.

Quoted 109 lines

Regards,

Hans

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Hans de Goede <[email protected]>
Sent: Friday, October 6, 2023 8:54 AM
To: Liming Sun <[email protected]>; Vadim Pasternak <[email protected]>; David Thompson <[email protected]>;

Mark

Quoted 1 line
Quoted 1 line

Gross <[email protected]>; Dan Carpenter

<[email protected]>

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Cc: [email protected]; [email protected]
Subject: Re: [PATCH v1 1/1] platform/mellanox: mlxbf-tmfifo: Fix a

warning

Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines

message

Hi Liming,

Quoted 9 lines · Liming Sun

This commit fixes the smatch static checker warning in mlxbf_tmfifo_rxtx_word() which complains data not initialized at line 634 when IS_VRING_DROP() is TRUE. This is not a real bug since line 634 is for Tx while IS_VRING_DROP() is only set for Rx. So there is no case that line 634 is executed when IS_VRING_DROP() is TRUE.

This commit initializes the local data variable to avoid unnecessary confusion to those static analyzing tools.

Review

Quoted 6 lines
---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/platform/mellanox/mlxbf-tmfifo.c

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx_word(struct

Quoted 4 lines

index f3696a54a2bd..ccc4b51d3379 100644

drivers/platform/mellanox/mlxbf-tmfifo.c +0 -0
@@ -595,8 +595,8 @@ static void mlxbf_tmfifo_rxtx_word(struct

mlxbf_tmfifo_vring *vring,

Quoted 7 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +1 -1
 {
 	struct virtio_device *vdev = vring->vq->vdev;
 	struct mlxbf_tmfifo *fifo = vring->fifo;
+	u64 data = 0;
 	void *addr;
-	u64 data;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/platform/mellanox/mlxbf-tmfifo.c static void mlxbf_tmfifo_rxtx_word(struct mlxbf_tmfifo_vring *vring, · line 602

Quoted 2 lines
drivers/platform/mellanox/mlxbf-tmfifo.c +0 -0
 	/* Get the buffer address of this desc. */
 	addr = phys_to_virt(virtio64_to_cpu(vdev, desc->addr));

This will fix the warning but not the issue at hand. As Dan pointed out in his original bug report, the issue is that after:

78034cbece79 ("platform/mellanox: mlxbf-tmfifo: Drop the Rx packet if

no

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

descriptors")

We now have this IS_VRING_DROP() check in the path, which despite the subject writeq(data, fifo->tx.data);is currently being applied to both

rx

Quoted 44 lines
Quoted 43 lines

and

Quoted 26 lines
Quoted 26 lines

tx vring-s
and when this returns true the memcpy from the ring to &data will not happen, but the code will still do:

writeq(data, fifo->tx.data);

So you may have silenced the warning now, but you will still write data not coming from the vring to transmit. The only difference is you are now guaranteed to write all zeroes.

Note another older issue is that if you hit the not enough space path:

       } else {
                /* Leftover bytes. */
                if (!IS_VRING_DROP(vring)) {
                        if (is_rx)
                                memcpy(addr + vring->cur_len, &data,
                                       len - vring->cur_len);
                        else
                                memcpy(&data, addr + vring->cur_len,
                                       len - vring->cur_len);
                }
                vring->cur_len = len;
        }
Quoted 1 line
Quoted 1 line

Then even if IS_VRING_DROP() returns true you are only initializing some

bytes

Quoted 1 line
Quoted 1 line

of the 8 bytes data variable and the other bytes will stay at whatever

random

Quoted 1 line
Quoted 1 line

value they had before and you end up writing this random bytes when

doing:

Quoted 10 lines
Quoted 9 lines

writeq(data, fifo->tx.data);

Regards,

Hans

author
#7 lore raw

PATCH v2 1/1 platform/mellanox: mlxbf-tmfifo: Fix a warning message

In reply to Liming Sun

This commit fixes the smatch static checker warning in function mlxbf_tmfifo_rxtx_word() which complains data not initialized at line 634 when IS_VRING_DROP() is TRUE.

---
v1->v2: Logic adjustment for Hans's comment
- Adjust the logic to avoid confusion.
v1: Initial version.

---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
drivers/platform/mellanox/mlxbf-tmfifo.c +11 -10
index f3696a54a2bd..d9615ad60012 100644
@@ -607,24 +607,25 @@ static void mlxbf_tmfifo_rxtx_word(struct mlxbf_tmfifo_vring *vring,
 
 	if (vring->cur_len + sizeof(u64) <= len) {
 		/* The whole word. */
-		if (!IS_VRING_DROP(vring)) {
-			if (is_rx)
+		if (is_rx) {
+			if (!IS_VRING_DROP(vring))
 				memcpy(addr + vring->cur_len, &data,
 				       sizeof(u64));
-			else
-				memcpy(&data, addr + vring->cur_len,
-				       sizeof(u64));
+		} else {
+			memcpy(&data, addr + vring->cur_len,
+			       sizeof(u64));
 		}
 		vring->cur_len += sizeof(u64);
 	} else {
 		/* Leftover bytes. */
-		if (!IS_VRING_DROP(vring)) {
-			if (is_rx)
+		if (is_rx) {
+			if (!IS_VRING_DROP(vring))
 				memcpy(addr + vring->cur_len, &data,
 				       len - vring->cur_len);
-			else
-				memcpy(&data, addr + vring->cur_len,
-				       len - vring->cur_len);
+		} else {
+			data = 0;
+			memcpy(&data, addr + vring->cur_len,
+			       len - vring->cur_len);
 		}
 		vring->cur_len = len;
 	}
Signature · 1 line
2.30.1
#8 lore raw

Hi,

Quoted 8 lines · Liming Sun

This commit fixes the smatch static checker warning in function mlxbf_tmfifo_rxtx_word() which complains data not initialized at line 634 when IS_VRING_DROP() is TRUE.

---
v1->v2: Logic adjustment for Hans's comment
- Adjust the logic to avoid confusion.

Thank you for your patch/series, I've applied this patch (series) to the pdx86 fixes branch:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=fixes

I will include this patch in my next fixes pull-req to Linus for the current kernel development cycle.

Regards,

Hans

Quoted 45 lines

v1: Initial version.

---
 drivers/platform/mellanox/mlxbf-tmfifo.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
drivers/platform/mellanox/mlxbf-tmfifo.c +11 -10
index f3696a54a2bd..d9615ad60012 100644
@@ -607,24 +607,25 @@ static void mlxbf_tmfifo_rxtx_word(struct mlxbf_tmfifo_vring *vring,
 
 	if (vring->cur_len + sizeof(u64) <= len) {
 		/* The whole word. */
-		if (!IS_VRING_DROP(vring)) {
-			if (is_rx)
+		if (is_rx) {
+			if (!IS_VRING_DROP(vring))
 				memcpy(addr + vring->cur_len, &data,
 				       sizeof(u64));
-			else
-				memcpy(&data, addr + vring->cur_len,
-				       sizeof(u64));
+		} else {
+			memcpy(&data, addr + vring->cur_len,
+			       sizeof(u64));
 		}
 		vring->cur_len += sizeof(u64);
 	} else {
 		/* Leftover bytes. */
-		if (!IS_VRING_DROP(vring)) {
-			if (is_rx)
+		if (is_rx) {
+			if (!IS_VRING_DROP(vring))
 				memcpy(addr + vring->cur_len, &data,
 				       len - vring->cur_len);
-			else
-				memcpy(&data, addr + vring->cur_len,
-				       len - vring->cur_len);
+		} else {
+			data = 0;
+			memcpy(&data, addr + vring->cur_len,
+			       len - vring->cur_len);
 		}
 		vring->cur_len = len;
 	}