[PATCH v2] RDMA/hns: drop dead empty check in setup_root_hem()

Maoyi Xie posted 1 patch 1 week, 6 days ago
drivers/infiniband/hw/hns/hns_roce_hem.c | 2 --
1 file changed, 2 deletions(-)
[PATCH v2] RDMA/hns: drop dead empty check in setup_root_hem()
Posted by Maoyi Xie 1 week, 6 days ago
setup_root_hem() reads the first entry of head->root and checks
the returned pointer against NULL:

    root_hem = list_first_entry(&head->root,
                                struct hns_roce_hem_item, list);
    if (!root_hem)
        return -ENOMEM;

list_first_entry() never returns NULL. On an empty list it returns
container_of(head, ..., list), a non-NULL garbage pointer that
aliases the head. So the check is dead.

The only caller adds an entry to head.root right before invoking
setup_root_hem():

    list_add(&root_hem->list, &head.root);
    ret = setup_root_hem(..., &head, ...);

So head.root is guaranteed non-empty on entry. Drop the check.

Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Maoyi Xie <maoyixie.tju@gmail.com>
---
v2: drop the check entirely per Jason's review, instead of
    converting to list_first_entry_or_null() as in v1.
v1: https://lore.kernel.org/r/20260521132045.3430906-1-maoyixie.tju@gmail.com

 drivers/infiniband/hw/hns/hns_roce_hem.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/drivers/infiniband/hw/hns/hns_roce_hem.c b/drivers/infiniband/hw/hns/hns_roce_hem.c
index e7c9e30ad2d8..61cd9f96423e 100644
--- a/drivers/infiniband/hw/hns/hns_roce_hem.c
+++ b/drivers/infiniband/hw/hns/hns_roce_hem.c
@@ -1269,8 +1269,6 @@ setup_root_hem(struct hns_roce_dev *hr_dev, struct hns_roce_hem_list *hem_list,
 
 	root_hem = list_first_entry(&head->root,
 				    struct hns_roce_hem_item, list);
-	if (!root_hem)
-		return -ENOMEM;
 
 	total = 0;
 	for (i = 0; i < region_cnt && total <= max_ba_num; i++) {
-- 
2.34.1
Re: [PATCH v2] RDMA/hns: drop dead empty check in setup_root_hem()
Posted by Jason Gunthorpe 5 days, 3 hours ago
On Tue, May 26, 2026 at 01:46:53PM +0800, Maoyi Xie wrote:
> setup_root_hem() reads the first entry of head->root and checks
> the returned pointer against NULL:
> 
>     root_hem = list_first_entry(&head->root,
>                                 struct hns_roce_hem_item, list);
>     if (!root_hem)
>         return -ENOMEM;
> 
> list_first_entry() never returns NULL. On an empty list it returns
> container_of(head, ..., list), a non-NULL garbage pointer that
> aliases the head. So the check is dead.
> 
> The only caller adds an entry to head.root right before invoking
> setup_root_hem():
> 
>     list_add(&root_hem->list, &head.root);
>     ret = setup_root_hem(..., &head, ...);
> 
> So head.root is guaranteed non-empty on entry. Drop the check.
> 
> Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
> Signed-off-by: Maoyi Xie <maoyixie.tju@gmail.com>
> ---
> v2: drop the check entirely per Jason's review, instead of
>     converting to list_first_entry_or_null() as in v1.
> v1: https://lore.kernel.org/r/20260521132045.3430906-1-maoyixie.tju@gmail.com
> 
>  drivers/infiniband/hw/hns/hns_roce_hem.c | 2 --
>  1 file changed, 2 deletions(-)

Applied to for-next, thanks

Jason