* [PATCH net v2] net/mlx4: avoid GCC 10 __bad_copy_from() false positive
@ 2026-06-03 6:10 Yao Sang
2026-06-04 19:21 ` Jacob Keller
2026-06-09 0:30 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 3+ messages in thread
From: Yao Sang @ 2026-06-03 6:10 UTC (permalink / raw)
To: Tariq Toukan, David S . Miller, Jakub Kicinski, Paolo Abeni
Cc: Andrew Lunn, Eric Dumazet, Gustavo A . R . Silva, netdev,
linux-rdma, Yao Sang
mlx4_init_user_cqes() fills a scratch buffer with the CQE
initialization pattern and then copies from that buffer to userspace.
In the single-copy path, the copy length is array_size(entries,
cqe_size), but the scratch buffer is allocated with PAGE_SIZE. GCC 10
does not carry the branch invariant strongly enough through the object
size checks and falsely triggers __bad_copy_from().
Size the scratch buffer to the actual copy length for the active path,
keep array_size() for the single-copy case, and retain a WARN_ON_ONCE()
guard for the PAGE_SIZE invariant before allocating the buffer.
Fixes: f69bf5dee7ef ("net/mlx4: Use array_size() helper in copy_to_user()")
Signed-off-by: Yao Sang <sangyao@kylinos.cn>
---
Changes in v2:
- Replace silent clamping with WARN_ON_ONCE guard for array_size()
over PAGE_SIZE, return -EINVAL.
drivers/net/ethernet/mellanox/mlx4/cq.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/drivers/net/ethernet/mellanox/mlx4/cq.c b/drivers/net/ethernet/mellanox/mlx4/cq.c
index e130e7259275..5c55971abbf0 100644
--- a/drivers/net/ethernet/mellanox/mlx4/cq.c
+++ b/drivers/net/ethernet/mellanox/mlx4/cq.c
@@ -290,6 +290,7 @@ static void mlx4_cq_free_icm(struct mlx4_dev *dev, int cqn)
static int mlx4_init_user_cqes(void *buf, int entries, int cqe_size)
{
int entries_per_copy = PAGE_SIZE / cqe_size;
+ size_t copy_bytes;
void *init_ents;
int err = 0;
int i;
@@ -314,8 +315,14 @@ static int mlx4_init_user_cqes(void *buf, int entries, int cqe_size)
buf += PAGE_SIZE;
}
} else {
+ copy_bytes = array_size(entries, cqe_size);
+ if (WARN_ON_ONCE(copy_bytes > PAGE_SIZE)) {
+ err = -EINVAL;
+ goto out;
+ }
+
err = copy_to_user((void __user *)buf, init_ents,
- array_size(entries, cqe_size)) ?
+ copy_bytes) ?
-EFAULT : 0;
}
--
2.25.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH net v2] net/mlx4: avoid GCC 10 __bad_copy_from() false positive
2026-06-03 6:10 [PATCH net v2] net/mlx4: avoid GCC 10 __bad_copy_from() false positive Yao Sang
@ 2026-06-04 19:21 ` Jacob Keller
2026-06-09 0:30 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: Jacob Keller @ 2026-06-04 19:21 UTC (permalink / raw)
To: Yao Sang, Tariq Toukan, David S . Miller, Jakub Kicinski,
Paolo Abeni
Cc: Andrew Lunn, Eric Dumazet, Gustavo A . R . Silva, netdev,
linux-rdma
On 6/2/2026 11:10 PM, Yao Sang wrote:
> mlx4_init_user_cqes() fills a scratch buffer with the CQE
> initialization pattern and then copies from that buffer to userspace.
>
> In the single-copy path, the copy length is array_size(entries,
> cqe_size), but the scratch buffer is allocated with PAGE_SIZE. GCC 10
> does not carry the branch invariant strongly enough through the object
> size checks and falsely triggers __bad_copy_from().
>
> Size the scratch buffer to the actual copy length for the active path,
> keep array_size() for the single-copy case, and retain a WARN_ON_ONCE()
> guard for the PAGE_SIZE invariant before allocating the buffer.
>
> Fixes: f69bf5dee7ef ("net/mlx4: Use array_size() helper in copy_to_user()")
> Signed-off-by: Yao Sang <sangyao@kylinos.cn>
> ---
> Changes in v2:
> - Replace silent clamping with WARN_ON_ONCE guard for array_size()
> over PAGE_SIZE, return -EINVAL.
>
Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>
> drivers/net/ethernet/mellanox/mlx4/cq.c | 9 ++++++++-
> 1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/net/ethernet/mellanox/mlx4/cq.c b/drivers/net/ethernet/mellanox/mlx4/cq.c
> index e130e7259275..5c55971abbf0 100644
> --- a/drivers/net/ethernet/mellanox/mlx4/cq.c
> +++ b/drivers/net/ethernet/mellanox/mlx4/cq.c
> @@ -290,6 +290,7 @@ static void mlx4_cq_free_icm(struct mlx4_dev *dev, int cqn)
> static int mlx4_init_user_cqes(void *buf, int entries, int cqe_size)
> {
> int entries_per_copy = PAGE_SIZE / cqe_size;
> + size_t copy_bytes;
> void *init_ents;
> int err = 0;
> int i;
> @@ -314,8 +315,14 @@ static int mlx4_init_user_cqes(void *buf, int entries, int cqe_size)
> buf += PAGE_SIZE;
> }
> } else {
> + copy_bytes = array_size(entries, cqe_size);
> + if (WARN_ON_ONCE(copy_bytes > PAGE_SIZE)) {
> + err = -EINVAL;
> + goto out;
> + }
> +
> err = copy_to_user((void __user *)buf, init_ents,
> - array_size(entries, cqe_size)) ?
> + copy_bytes) ?
> -EFAULT : 0;
> }
>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH net v2] net/mlx4: avoid GCC 10 __bad_copy_from() false positive
2026-06-03 6:10 [PATCH net v2] net/mlx4: avoid GCC 10 __bad_copy_from() false positive Yao Sang
2026-06-04 19:21 ` Jacob Keller
@ 2026-06-09 0:30 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-06-09 0:30 UTC (permalink / raw)
To: Yao Sang
Cc: tariqt, davem, kuba, pabeni, andrew+netdev, edumazet, gustavoars,
netdev, linux-rdma
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Wed, 3 Jun 2026 14:10:44 +0800 you wrote:
> mlx4_init_user_cqes() fills a scratch buffer with the CQE
> initialization pattern and then copies from that buffer to userspace.
>
> In the single-copy path, the copy length is array_size(entries,
> cqe_size), but the scratch buffer is allocated with PAGE_SIZE. GCC 10
> does not carry the branch invariant strongly enough through the object
> size checks and falsely triggers __bad_copy_from().
>
> [...]
Here is the summary with links:
- [net,v2] net/mlx4: avoid GCC 10 __bad_copy_from() false positive
https://git.kernel.org/netdev/net/c/2365343f4aad
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-06-09 0:30 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-03 6:10 [PATCH net v2] net/mlx4: avoid GCC 10 __bad_copy_from() false positive Yao Sang
2026-06-04 19:21 ` Jacob Keller
2026-06-09 0:30 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox