[PATCH] infiniband/core: Fix logic error in ib_get_gids_from_rdma_hdr()

Jang Ingyu posted 1 patch 1 month, 3 weeks ago
drivers/infiniband/core/verbs.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] infiniband/core: Fix logic error in ib_get_gids_from_rdma_hdr()
Posted by Jang Ingyu 1 month, 3 weeks ago
Fix missing comparison operator for RDMA_NETWORK_ROCE_V1 in the
conditional statement. The constant was used directly instead of
being compared with net_type, causing the condition to always
evaluate to true.

Signed-off-by: Jang Ingyu <ingyujang25@korea.ac.kr>
---
 drivers/infiniband/core/verbs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/infiniband/core/verbs.c b/drivers/infiniband/core/verbs.c
index 11b1a194d..ee3909285 100644
--- a/drivers/infiniband/core/verbs.c
+++ b/drivers/infiniband/core/verbs.c
@@ -738,7 +738,7 @@ int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
 				       (struct in6_addr *)dgid);
 		return 0;
 	} else if (net_type == RDMA_NETWORK_IPV6 ||
-		   net_type == RDMA_NETWORK_IB || RDMA_NETWORK_ROCE_V1) {
+		   net_type == RDMA_NETWORK_IB || net_type == RDMA_NETWORK_ROCE_V1) {
 		*dgid = hdr->ibgrh.dgid;
 		*sgid = hdr->ibgrh.sgid;
 		return 0;
-- 
2.34.1
Re: [PATCH] infiniband/core: Fix logic error in ib_get_gids_from_rdma_hdr()
Posted by Leon Romanovsky 1 month, 2 weeks ago
On Fri, Dec 19, 2025 at 01:15:08PM +0900, Jang Ingyu wrote:
> Fix missing comparison operator for RDMA_NETWORK_ROCE_V1 in the
> conditional statement. The constant was used directly instead of
> being compared with net_type, causing the condition to always
> evaluate to true.

In current code, it doesn't matter as network type can be one of four
possible values, and this "else if" will be always true anyway.

I changed your patch to this and added Fixes line:
diff --git a/drivers/infiniband/core/verbs.c b/drivers/infiniband/core/verbs.c
index ee390928511ae..256f81c5803ff 100644
--- a/drivers/infiniband/core/verbs.c
+++ b/drivers/infiniband/core/verbs.c
@@ -737,14 +737,11 @@ int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
                ipv6_addr_set_v4mapped(dst_saddr,
                                       (struct in6_addr *)dgid);
                return 0;
-       } else if (net_type == RDMA_NETWORK_IPV6 ||
-                  net_type == RDMA_NETWORK_IB || net_type == RDMA_NETWORK_ROCE_V1) {
-               *dgid = hdr->ibgrh.dgid;
-               *sgid = hdr->ibgrh.sgid;
-               return 0;
-       } else {
-               return -EINVAL;
        }
+
+       *dgid = hdr->ibgrh.dgid;
+       *sgid = hdr->ibgrh.sgid;
+       return 0;
 }
 EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);

> 
> Signed-off-by: Jang Ingyu <ingyujang25@korea.ac.kr>
> ---
>  drivers/infiniband/core/verbs.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/infiniband/core/verbs.c b/drivers/infiniband/core/verbs.c
> index 11b1a194d..ee3909285 100644
> --- a/drivers/infiniband/core/verbs.c
> +++ b/drivers/infiniband/core/verbs.c
> @@ -738,7 +738,7 @@ int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
>  				       (struct in6_addr *)dgid);
>  		return 0;
>  	} else if (net_type == RDMA_NETWORK_IPV6 ||
> -		   net_type == RDMA_NETWORK_IB || RDMA_NETWORK_ROCE_V1) {
> +		   net_type == RDMA_NETWORK_IB || net_type == RDMA_NETWORK_ROCE_V1) {
>  		*dgid = hdr->ibgrh.dgid;
>  		*sgid = hdr->ibgrh.sgid;
>  		return 0;
> -- 
> 2.34.1
> 
>
Re: [PATCH] infiniband/core: Fix logic error in ib_get_gids_from_rdma_hdr()
Posted by Leon Romanovsky 1 month, 2 weeks ago
On Sun, Dec 21, 2025 at 11:24:18AM +0200, Leon Romanovsky wrote:
> On Fri, Dec 19, 2025 at 01:15:08PM +0900, Jang Ingyu wrote:
> > Fix missing comparison operator for RDMA_NETWORK_ROCE_V1 in the
> > conditional statement. The constant was used directly instead of
> > being compared with net_type, causing the condition to always
> > evaluate to true.
> 
> In current code, it doesn't matter as network type can be one of four
> possible values, and this "else if" will be always true anyway.
> 
> I changed your patch to this and added Fixes line:
> diff --git a/drivers/infiniband/core/verbs.c b/drivers/infiniband/core/verbs.c
> index ee390928511ae..256f81c5803ff 100644
> --- a/drivers/infiniband/core/verbs.c
> +++ b/drivers/infiniband/core/verbs.c
> @@ -737,14 +737,11 @@ int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
>                 ipv6_addr_set_v4mapped(dst_saddr,
>                                        (struct in6_addr *)dgid);
>                 return 0;
> -       } else if (net_type == RDMA_NETWORK_IPV6 ||
> -                  net_type == RDMA_NETWORK_IB || net_type == RDMA_NETWORK_ROCE_V1) {
> -               *dgid = hdr->ibgrh.dgid;
> -               *sgid = hdr->ibgrh.sgid;
> -               return 0;
> -       } else {
> -               return -EINVAL;
>         }
> +
> +       *dgid = hdr->ibgrh.dgid;
> +       *sgid = hdr->ibgrh.sgid;
> +       return 0;
>  }
>  EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);

After some additional consideration, I'll keep your patch as is.

My change is technically correct, but it's risky since some drivers  
use non‑conformant values.

Thanks.

> 
> > 
> > Signed-off-by: Jang Ingyu <ingyujang25@korea.ac.kr>
> > ---
> >  drivers/infiniband/core/verbs.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/infiniband/core/verbs.c b/drivers/infiniband/core/verbs.c
> > index 11b1a194d..ee3909285 100644
> > --- a/drivers/infiniband/core/verbs.c
> > +++ b/drivers/infiniband/core/verbs.c
> > @@ -738,7 +738,7 @@ int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
> >  				       (struct in6_addr *)dgid);
> >  		return 0;
> >  	} else if (net_type == RDMA_NETWORK_IPV6 ||
> > -		   net_type == RDMA_NETWORK_IB || RDMA_NETWORK_ROCE_V1) {
> > +		   net_type == RDMA_NETWORK_IB || net_type == RDMA_NETWORK_ROCE_V1) {
> >  		*dgid = hdr->ibgrh.dgid;
> >  		*sgid = hdr->ibgrh.sgid;
> >  		return 0;
> > -- 
> > 2.34.1
> > 
> > 
> 
Re: [PATCH] infiniband/core: Fix logic error in ib_get_gids_from_rdma_hdr()
Posted by Jason Gunthorpe 1 month ago
On Sun, Dec 21, 2025 at 11:30:38AM +0200, Leon Romanovsky wrote:
> On Sun, Dec 21, 2025 at 11:24:18AM +0200, Leon Romanovsky wrote:
> > On Fri, Dec 19, 2025 at 01:15:08PM +0900, Jang Ingyu wrote:
> > > Fix missing comparison operator for RDMA_NETWORK_ROCE_V1 in the
> > > conditional statement. The constant was used directly instead of
> > > being compared with net_type, causing the condition to always
> > > evaluate to true.
> > 
> > In current code, it doesn't matter as network type can be one of four
> > possible values, and this "else if" will be always true anyway.
> > 
> > I changed your patch to this and added Fixes line:
> > diff --git a/drivers/infiniband/core/verbs.c b/drivers/infiniband/core/verbs.c
> > index ee390928511ae..256f81c5803ff 100644
> > --- a/drivers/infiniband/core/verbs.c
> > +++ b/drivers/infiniband/core/verbs.c
> > @@ -737,14 +737,11 @@ int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
> >                 ipv6_addr_set_v4mapped(dst_saddr,
> >                                        (struct in6_addr *)dgid);
> >                 return 0;
> > -       } else if (net_type == RDMA_NETWORK_IPV6 ||
> > -                  net_type == RDMA_NETWORK_IB || net_type == RDMA_NETWORK_ROCE_V1) {
> > -               *dgid = hdr->ibgrh.dgid;
> > -               *sgid = hdr->ibgrh.sgid;
> > -               return 0;
> > -       } else {
> > -               return -EINVAL;
> >         }
> > +
> > +       *dgid = hdr->ibgrh.dgid;
> > +       *sgid = hdr->ibgrh.sgid;
> > +       return 0;
> >  }
> >  EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);
> 
> After some additional consideration, I'll keep your patch as is.
> 
> My change is technically correct, but it's risky since some drivers  
> use non‑conformant values.

I don't think it is that risky because it is what has been happening
all this time anyhow.

> > >  	} else if (net_type == RDMA_NETWORK_IPV6 ||
> > > -		   net_type == RDMA_NETWORK_IB || RDMA_NETWORK_ROCE_V1) {

That expression is always true

Conversely with the "correct" patch if we have a net type of
RDMA_NETWORK_IPV4 we now get an EINVAL that we didn't get before..

I guess we will see if this turns into a problem or not..

Jason
Re: [PATCH] infiniband/core: Fix logic error in ib_get_gids_from_rdma_hdr()
Posted by Leon Romanovsky 1 month, 2 weeks ago
On Fri, 19 Dec 2025 13:15:08 +0900, Jang Ingyu wrote:
> Fix missing comparison operator for RDMA_NETWORK_ROCE_V1 in the
> conditional statement. The constant was used directly instead of
> being compared with net_type, causing the condition to always
> evaluate to true.
> 
> 

Applied, thanks!

[1/1] infiniband/core: Fix logic error in ib_get_gids_from_rdma_hdr()
      https://git.kernel.org/rdma/rdma/c/8aaa848eaddd9e

Best regards,
-- 
Leon Romanovsky <leon@kernel.org>