2022-05-17 16:37:44

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [PATCH] RDMA: remove null check after call container_of()

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


2022-05-18 03:40:26

by Christophe JAILLET

[permalink] [raw]
Subject: Re: [PATCH] RDMA: remove null check after call container_of()

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
>


2022-05-18 04:11:15

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [PATCH] RDMA: remove null check after call container_of()

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

2022-05-18 04:25:28

by baihaowen

[permalink] [raw]
Subject: Re: [PATCH] RDMA: remove null check after call container_of()

在 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


2022-05-18 04:31:09

by Christophe JAILLET

[permalink] [raw]
Subject: Re: [PATCH] RDMA: remove null check after call container_of()

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