Skip to contentSkip to conversation

dma-buf: system_heap: Use LIST_HEAD() to initialize on stack list head

PATCH In review Jisheng Zhang 2 participants 3 messages May 19

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma-buf: system_heap: Use LIST_HEAD() to initialize on stack list head

Use LIST_HEAD to initialize on stack list head. No intentional functional impact.

Change generated with below coccinelle script:

@@
identifier name;
@@
- struct list_head name;
+ LIST_HEAD(name);
... when != name
- INIT_LIST_HEAD(&name);

---
 drivers/dma-buf/heaps/system_heap.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/dma-buf/heaps/system_heap.c +1 -2
index 03c2b87cb111..6f8b7138ff56 100644
@@ -409,7 +409,7 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
 	struct dma_buf *dmabuf;
 	struct sg_table *table;
 	struct scatterlist *sg;
-	struct list_head pages;
+	LIST_HEAD(pages);
 	struct page *page, *tmp_page;
 	int i, ret = -ENOMEM;
 
@@ -423,7 +423,6 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
 	buffer->len = len;
 	buffer->cc_shared = cc_shared;
 
-	INIT_LIST_HEAD(&pages);
 	i = 0;
 	while (size_remaining > 0) {
 		/*
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 5 lines · Jisheng Zhang

Use LIST_HEAD to initialize on stack list head. No intentional functional impact.

Change generated with below coccinelle script:

Quoted 7 lines

@@
identifier name;
@@
- struct list_head name;
+ LIST_HEAD(name);
... when != name
- INIT_LIST_HEAD(&name);

The patch itself looks correct, but my question is why would we want to do that?

Initializing the list head (or any other result variable) directly before it is filled in is usually good practice.

Regards,
Christian.

Quoted 30 lines
---
 drivers/dma-buf/heaps/system_heap.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/dma-buf/heaps/system_heap.c +1 -3
index 03c2b87cb111..6f8b7138ff56 100644
@@ -409,7 +409,7 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
        struct dma_buf *dmabuf;
        struct sg_table *table;
        struct scatterlist *sg;
-       struct list_head pages;
+       LIST_HEAD(pages);
        struct page *page, *tmp_page;
        int i, ret = -ENOMEM;
 
@@ -423,7 +423,6 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
        buffer->len = len;
        buffer->cc_shared = cc_shared;
 
-       INIT_LIST_HEAD(&pages);
        i = 0;
        while (size_remaining > 0) {
                /*
--

2.53.0

#2 lore raw
Quoted 14 lines · Christian König
Quoted 5 lines · Jisheng Zhang

Use LIST_HEAD to initialize on stack list head. No intentional functional impact.

Change generated with below coccinelle script:

Quoted 7 lines

@@
identifier name;
@@
- struct list_head name;
+ LIST_HEAD(name);
... when != name
- INIT_LIST_HEAD(&name);

Quoted 1 line

The patch itself looks correct, but my question is why would we want to do that?

The benefit is: simpler code, combine the linked list defintion and initialization in one step, thus 1 LoC vs 2 LoCs; And potential bug prevention, e.g use the list before intialized.
But I agree, no strong benefit here.

Quoted 37 lines

Initializing the list head (or any other result variable) directly before it is filled in is usually good practice.

Regards,
Christian.

Quoted 30 lines
---
 drivers/dma-buf/heaps/system_heap.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/dma-buf/heaps/system_heap.c +1 -3
index 03c2b87cb111..6f8b7138ff56 100644
@@ -409,7 +409,7 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
        struct dma_buf *dmabuf;
        struct sg_table *table;
        struct scatterlist *sg;
-       struct list_head pages;
+       LIST_HEAD(pages);
        struct page *page, *tmp_page;
        int i, ret = -ENOMEM;
 
@@ -423,7 +423,6 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
        buffer->len = len;
        buffer->cc_shared = cc_shared;
 
-       INIT_LIST_HEAD(&pages);
        i = 0;
        while (size_remaining > 0) {
                /*
--

2.53.0