Linux PCI Non-Transparent Bridge framework and drivers
 help / color / mirror / Atom feed
* [PATCH] NTB: Fix UB/bug in ntb_mw_get_align()
@ 2017-11-30 21:39 Serge Semin
  2017-11-30 22:07 ` Logan Gunthorpe
  0 siblings, 1 reply; 2+ messages in thread
From: Serge Semin @ 2017-11-30 21:39 UTC (permalink / raw)
  To: jdmason, dave.jiang, Allen.Hubbe, Shyam-sundar.S-k, Xiangliang.Yu
  Cc: Sergey.Semin, linux-ntb, linux-kernel, Serge Semin

Simple (1 << pidx) operation causes undefined behaviour when
pidx >= 32. It must be casted to u64 to match the actual return
value of ntb_link_is_up() method, so to have all the possible
peer indexes covered and to get rid of undefined behaviour.
Additionally there are special macros in "linux/bitops.h" to perform
the bit-set-shift operations, so it's recommended to have them used
for proper bit setting.

Signed-off-by: Serge Semin <fancer.lancer@gmail.com>
---
 include/linux/ntb.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/ntb.h b/include/linux/ntb.h
index c1646f2c6344..488e586fb76c 100644
--- a/include/linux/ntb.h
+++ b/include/linux/ntb.h
@@ -764,7 +764,7 @@ static inline int ntb_mw_get_align(struct ntb_dev *ntb, int pidx, int widx,
 				   resource_size_t *size_align,
 				   resource_size_t *size_max)
 {
-	if (!(ntb_link_is_up(ntb, NULL, NULL) & (1 << pidx)))
+	if (!(ntb_link_is_up(ntb, NULL, NULL) & BIT_ULL(pidx)))
 		return -ENOTCONN;
 
 	return ntb->ops->mw_get_align(ntb, pidx, widx, addr_align, size_align,
-- 
2.12.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] NTB: Fix UB/bug in ntb_mw_get_align()
  2017-11-30 21:39 [PATCH] NTB: Fix UB/bug in ntb_mw_get_align() Serge Semin
@ 2017-11-30 22:07 ` Logan Gunthorpe
  0 siblings, 0 replies; 2+ messages in thread
From: Logan Gunthorpe @ 2017-11-30 22:07 UTC (permalink / raw)
  To: Serge Semin, jdmason, dave.jiang, Allen.Hubbe, Shyam-sundar.S-k,
	Xiangliang.Yu
  Cc: Sergey.Semin, linux-ntb, linux-kernel



On 30/11/17 02:39 PM, Serge Semin wrote:
> Simple (1 << pidx) operation causes undefined behaviour when
> pidx >= 32. It must be casted to u64 to match the actual return
> value of ntb_link_is_up() method, so to have all the possible
> peer indexes covered and to get rid of undefined behaviour.
> Additionally there are special macros in "linux/bitops.h" to perform
> the bit-set-shift operations, so it's recommended to have them used
> for proper bit setting.
> 
> Signed-off-by: Serge Semin <fancer.lancer@gmail.com>

Reviewed-by: Logan Gunthorpe <logang@deltatee.com>

> ---
>   include/linux/ntb.h | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/linux/ntb.h b/include/linux/ntb.h
> index c1646f2c6344..488e586fb76c 100644
> --- a/include/linux/ntb.h
> +++ b/include/linux/ntb.h
> @@ -764,7 +764,7 @@ static inline int ntb_mw_get_align(struct ntb_dev *ntb, int pidx, int widx,
>   				   resource_size_t *size_align,
>   				   resource_size_t *size_max)
>   {
> -	if (!(ntb_link_is_up(ntb, NULL, NULL) & (1 << pidx)))
> +	if (!(ntb_link_is_up(ntb, NULL, NULL) & BIT_ULL(pidx)))
>   		return -ENOTCONN;
>   
>   	return ntb->ops->mw_get_align(ntb, pidx, widx, addr_align, size_align,
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-11-30 22:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-11-30 21:39 [PATCH] NTB: Fix UB/bug in ntb_mw_get_align() Serge Semin
2017-11-30 22:07 ` Logan Gunthorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox