linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] RDMA: remove null check after call container_of()
@ 2022-05-17  1:33 Haowen Bai
  2022-05-17 12:16 ` Jason Gunthorpe
  0 siblings, 1 reply; 6+ messages in thread
From: Haowen Bai @ 2022-05-17  1:33 UTC (permalink / raw)
  To: Dennis Dalessandro, Jason Gunthorpe, Leon Romanovsky
  Cc: Haowen Bai, linux-rdma, linux-kernel

container_of() will never return NULL, so remove useless code.

Signed-off-by: Haowen Bai <baihaowen@meizu.com>
---
 drivers/infiniband/sw/rdmavt/vt.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/drivers/infiniband/sw/rdmavt/vt.c b/drivers/infiniband/sw/rdmavt/vt.c
index 59481ae39505..b2d83b4958fc 100644
--- a/drivers/infiniband/sw/rdmavt/vt.c
+++ b/drivers/infiniband/sw/rdmavt/vt.c
@@ -50,8 +50,6 @@ struct rvt_dev_info *rvt_alloc_device(size_t size, int nports)
 	struct rvt_dev_info *rdi;
 
 	rdi = container_of(_ib_alloc_device(size), struct rvt_dev_info, ibdev);
-	if (!rdi)
-		return rdi;
 
 	rdi->ports = kcalloc(nports, sizeof(*rdi->ports), GFP_KERNEL);
 	if (!rdi->ports)
-- 
2.7.4


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

* Re: [PATCH] RDMA: remove null check after call container_of()
  2022-05-17  1:33 [PATCH] RDMA: remove null check after call container_of() Haowen Bai
@ 2022-05-17 12:16 ` Jason Gunthorpe
  2022-05-17 17:54   ` Christophe JAILLET
  0 siblings, 1 reply; 6+ messages in thread
From: Jason Gunthorpe @ 2022-05-17 12:16 UTC (permalink / raw)
  To: Haowen Bai; +Cc: Dennis Dalessandro, Leon Romanovsky, linux-rdma, linux-kernel

On Tue, May 17, 2022 at 09:33:28AM +0800, Haowen Bai wrote:
> container_of() will never return NULL, so remove useless code.

It is confusing here, but it can be null. If you want to do this then
you have to split out the _ib_alloc_device call and check it
seperately.

Jason

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

* Re: [PATCH] RDMA: remove null check after call container_of()
  2022-05-17 12:16 ` Jason Gunthorpe
@ 2022-05-17 17:54   ` Christophe JAILLET
  2022-05-17 18:03     ` Jason Gunthorpe
  0 siblings, 1 reply; 6+ messages in thread
From: Christophe JAILLET @ 2022-05-17 17:54 UTC (permalink / raw)
  To: Jason Gunthorpe, Haowen Bai
  Cc: Dennis Dalessandro, Leon Romanovsky, linux-rdma, linux-kernel

Le 17/05/2022 à 14:16, Jason Gunthorpe a écrit :
> On Tue, May 17, 2022 at 09:33:28AM +0800, Haowen Bai wrote:
>> container_of() will never return NULL, so remove useless code.
> 
> It is confusing here, but it can be null. 

Hi,

out of curiosity, can you elaborate how it can be NULL?

CJ

> If you want to do this then
> you have to split out the _ib_alloc_device call and check it
> seperately.
> 
> Jason
> 


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

* Re: [PATCH] RDMA: remove null check after call container_of()
  2022-05-17 17:54   ` Christophe JAILLET
@ 2022-05-17 18:03     ` Jason Gunthorpe
  2022-05-17 19:42       ` Christophe JAILLET
  2022-05-18  1:55       ` baihaowen
  0 siblings, 2 replies; 6+ messages in thread
From: Jason Gunthorpe @ 2022-05-17 18:03 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Haowen Bai, Dennis Dalessandro, Leon Romanovsky, linux-rdma,
	linux-kernel

On Tue, May 17, 2022 at 07:54:38PM +0200, Christophe JAILLET wrote:
> Le 17/05/2022 à 14:16, Jason Gunthorpe a écrit :
> > On Tue, May 17, 2022 at 09:33:28AM +0800, Haowen Bai wrote:
> > > container_of() will never return NULL, so remove useless code.
> > 
> > It is confusing here, but it can be null.
> 
> Hi,
> 
> out of curiosity, can you elaborate how it can be NULL?

It is guarented/required that that container_of is a 0 offset. The
normal usage of the ib_alloc_device macro:

#define ib_alloc_device(drv_struct, member)                                    \
	container_of(_ib_alloc_device(sizeof(struct drv_struct) +              \
				      BUILD_BUG_ON_ZERO(offsetof(              \
					      struct drv_struct, member))),    \
		     struct drv_struct, member)

Enforces this property with a BUILD_BUG_ON

So, if the input pointer to container_of is reliably NULL or ERR_PTR
then the output pointer will be the same.

The rvt code here open codes the call because it is a mid-layer and
the sizeof() calculation above is not correct for it.

Jason

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

* Re: [PATCH] RDMA: remove null check after call container_of()
  2022-05-17 18:03     ` Jason Gunthorpe
@ 2022-05-17 19:42       ` Christophe JAILLET
  2022-05-18  1:55       ` baihaowen
  1 sibling, 0 replies; 6+ messages in thread
From: Christophe JAILLET @ 2022-05-17 19:42 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Dennis Dalessandro, Leon Romanovsky, linux-rdma, linux-kernel,
	Haowen Bai

Le 17/05/2022 à 20:03, Jason Gunthorpe a écrit :
> On Tue, May 17, 2022 at 07:54:38PM +0200, Christophe JAILLET wrote:
>> Le 17/05/2022 à 14:16, Jason Gunthorpe a écrit :
>>> On Tue, May 17, 2022 at 09:33:28AM +0800, Haowen Bai wrote:
>>>> container_of() will never return NULL, so remove useless code.
>>>
>>> It is confusing here, but it can be null.
>>
>> Hi,
>>
>> out of curiosity, can you elaborate how it can be NULL?
> 
> It is guarented/required that that container_of is a 0 offset. The
> normal usage of the ib_alloc_device macro:
> 
> #define ib_alloc_device(drv_struct, member)                                    \
> 	container_of(_ib_alloc_device(sizeof(struct drv_struct) +              \
> 				      BUILD_BUG_ON_ZERO(offsetof(              \
> 					      struct drv_struct, member))),    \
> 		     struct drv_struct, member)
> 
> Enforces this property with a BUILD_BUG_ON
> 
> So, if the input pointer to container_of is reliably NULL or ERR_PTR
> then the output pointer will be the same.
> 
> The rvt code here open codes the call because it is a mid-layer and
> the sizeof() calculation above is not correct for it.
> 
> Jason
> 

Crystal clear.
Thank you for the explanation.

CJ

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

* Re: [PATCH] RDMA: remove null check after call container_of()
  2022-05-17 18:03     ` Jason Gunthorpe
  2022-05-17 19:42       ` Christophe JAILLET
@ 2022-05-18  1:55       ` baihaowen
  1 sibling, 0 replies; 6+ messages in thread
From: baihaowen @ 2022-05-18  1:55 UTC (permalink / raw)
  To: Jason Gunthorpe, Christophe JAILLET
  Cc: Dennis Dalessandro, Leon Romanovsky, linux-rdma, linux-kernel

在 2022/5/18 上午2:03, Jason Gunthorpe 写道:
> On Tue, May 17, 2022 at 07:54:38PM +0200, Christophe JAILLET wrote:
>> Le 17/05/2022 à 14:16, Jason Gunthorpe a écrit :
>>> On Tue, May 17, 2022 at 09:33:28AM +0800, Haowen Bai wrote:
>>>> container_of() will never return NULL, so remove useless code.
>>> It is confusing here, but it can be null.
>> Hi,
>>
>> out of curiosity, can you elaborate how it can be NULL?
> It is guarented/required that that container_of is a 0 offset. The
> normal usage of the ib_alloc_device macro:
>
> #define ib_alloc_device(drv_struct, member)                                    \
> 	container_of(_ib_alloc_device(sizeof(struct drv_struct) +              \
> 				      BUILD_BUG_ON_ZERO(offsetof(              \
> 					      struct drv_struct, member))),    \
> 		     struct drv_struct, member)
>
> Enforces this property with a BUILD_BUG_ON
>
> So, if the input pointer to container_of is reliably NULL or ERR_PTR
> then the output pointer will be the same.
>
> The rvt code here open codes the call because it is a mid-layer and
> the sizeof() calculation above is not correct for it.
>
> Jason
Thank you for the explanation.   : )

-- 
Haowen Bai


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

end of thread, other threads:[~2022-05-18  1:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-17  1:33 [PATCH] RDMA: remove null check after call container_of() Haowen Bai
2022-05-17 12:16 ` Jason Gunthorpe
2022-05-17 17:54   ` Christophe JAILLET
2022-05-17 18:03     ` Jason Gunthorpe
2022-05-17 19:42       ` Christophe JAILLET
2022-05-18  1:55       ` baihaowen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).