Skip to contentSkip to conversation

RDMA/hns: fix dead empty check on head->root in setup_root_hem()

PATCH In review Maoyi Xie 2 participants 4 messages May 21 – Jun 3

Subsystems (2)
lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

PATCH 1/1 RDMA/hns: fix dead empty check on head->root in setup_root_hem()

setup_root_hem() reads head->root with list_first_entry() and then tests the returned pointer against NULL. list_first_entry() never returns NULL. On an empty list it returns container_of(&head->root, struct hns_roce_hem_item, list), which equals &head->root because list is the first member. The -ENOMEM early return is dead code.

If head->root is ever empty here, the aliased root_hem points at &head->root. root_hem->addr and root_hem->dma_addr then read past the end of struct hns_roce_hem_head into adjacent memory. The garbage value is handed to the hardware as a DMA base address.

Use list_first_entry_or_null() so the empty case returns NULL and the -ENOMEM early return runs.

The same shape has been cleaned up elsewhere, for example in
commit fbb8bc408027 ("net: qed: Remove redundant NULL checks after list_first_entry()"), commit c708d3fad421 ("crypto: atmel - use list_first_entry_or_null to simplify find_dev"), and commit 10379171f346 ("ksmbd: use list_first_entry_or_null for opinfo_get_list()"). This hns site was missed by those cleanups.

---
 drivers/infiniband/hw/hns/hns_roce_hem.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/infiniband/hw/hns/hns_roce_hem.c +2 -2
index e7c9e30ad2d8..12ac9ba6b312 100644
@@ -1267,8 +1267,8 @@ setup_root_hem(struct hns_roce_dev *hr_dev, struct hns_roce_hem_list *hem_list,
 	int i, total;
 	int ret;
 
-	root_hem = list_first_entry(&head->root,
-				    struct hns_roce_hem_item, list);
+	root_hem = list_first_entry_or_null(&head->root,
+					    struct hns_roce_hem_item, list);
 	if (!root_hem)
 		return -ENOMEM;
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw

drivers/infiniband/hw/hns/hns_roce_hem.c setup_root_hem(struct hns_roce_dev *hr_dev, struct hns_roce_hem_list *hem_list, · line 1273

Quoted 12 lines · Maoyi Xie
drivers/infiniband/hw/hns/hns_roce_hem.c +2 -2
@@ -1267,8 +1267,8 @@ setup_root_hem(struct hns_roce_dev *hr_dev, struct hns_roce_hem_list *hem_list,
 	int i, total;
 	int ret;
 
-	root_hem = list_first_entry(&head->root,
-				    struct hns_roce_hem_item, list);
+	root_hem = list_first_entry_or_null(&head->root,
+					    struct hns_roce_hem_item, list);
 	if (!root_hem)
 		return -ENOMEM;

Delete the if, the list can never be empty. The driver immediately adds an entry after initializing it and never removes entries

Jason

author
#2 lore raw

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

In reply to Maoyi Xie

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.

---
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/[email protected]

 drivers/infiniband/hw/hns/hns_roce_hem.c | 2 --
 1 file changed, 2 deletions(-)
drivers/infiniband/hw/hns/hns_roce_hem.c +0 -2
index e7c9e30ad2d8..61cd9f96423e 100644
@@ -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++) {
Signature · 1 line
2.34.1
maintainer Applied
#3 lore raw

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

Quoted 22 lines · Maoyi Xie

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.

Quoted 7 lines

---
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/[email protected]

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

Applied to for-next, thanks

Jason