diff mbox series

riscv: module: use a plain variable for list_head instead of a pointer

Message ID 20241127103016.2699179-1-cleger@rivosinc.com (mailing list archive)
State Superseded
Headers show
Series riscv: module: use a plain variable for list_head instead of a pointer | expand

Checks

Context Check Description
conchuod/vmtest-for-next-PR success PR summary
conchuod/patch-1-test-1 success .github/scripts/patches/tests/build_rv32_defconfig.sh took 206.75s
conchuod/patch-1-test-2 success .github/scripts/patches/tests/build_rv64_clang_allmodconfig.sh took 1371.24s
conchuod/patch-1-test-3 success .github/scripts/patches/tests/build_rv64_gcc_allmodconfig.sh took 1575.15s
conchuod/patch-1-test-4 success .github/scripts/patches/tests/build_rv64_nommu_k210_defconfig.sh took 77.31s
conchuod/patch-1-test-5 success .github/scripts/patches/tests/build_rv64_nommu_virt_defconfig.sh took 78.24s
conchuod/patch-1-test-6 success .github/scripts/patches/tests/checkpatch.sh took 0.49s
conchuod/patch-1-test-7 success .github/scripts/patches/tests/dtb_warn_rv64.sh took 47.16s
conchuod/patch-1-test-8 success .github/scripts/patches/tests/header_inline.sh took 0.01s
conchuod/patch-1-test-9 success .github/scripts/patches/tests/kdoc.sh took 0.58s
conchuod/patch-1-test-10 success .github/scripts/patches/tests/module_param.sh took 0.01s
conchuod/patch-1-test-11 success .github/scripts/patches/tests/verify_fixes.sh took 0.00s
conchuod/patch-1-test-12 success .github/scripts/patches/tests/verify_signedoff.sh took 0.03s

Commit Message

Clément Léger Nov. 27, 2024, 10:30 a.m. UTC
list_head does not need to be allocated, it can be a plain variable.
Remove the pointer which allows to get rid of the allocation as well as
an existing memory leak.

Signed-off-by: Clément Léger <cleger@rivosinc.com>

---
 arch/riscv/kernel/module.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)

Comments

Andrew Jones Nov. 27, 2024, 2:02 p.m. UTC | #1
On Wed, Nov 27, 2024 at 11:30:14AM +0100, Clément Léger wrote:
> list_head does not need to be allocated, it can be a plain variable.

rel_head's list_head member, rel_entry, doesn't need to be allocated,
its storage can just be part of the allocated rel_head.

> Remove the pointer which allows to get rid of the allocation as well as
> an existing memory leak.

It'd be nice to add how the memory leak was found. Inspection or some
tool?

> 
> Signed-off-by: Clément Léger <cleger@rivosinc.com>
> 
> ---
>  arch/riscv/kernel/module.c | 18 ++++--------------
>  1 file changed, 4 insertions(+), 14 deletions(-)
> 
> diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
> index 1cd461f3d872..47d0ebeec93c 100644
> --- a/arch/riscv/kernel/module.c
> +++ b/arch/riscv/kernel/module.c
> @@ -23,7 +23,7 @@ struct used_bucket {
>  
>  struct relocation_head {
>  	struct hlist_node node;
> -	struct list_head *rel_entry;
> +	struct list_head rel_entry;
>  	void *location;
>  };
>  
> @@ -634,7 +634,7 @@ process_accumulated_relocations(struct module *me,
>  			location = rel_head_iter->location;
>  			list_for_each_entry_safe(rel_entry_iter,
>  						 rel_entry_iter_tmp,
> -						 rel_head_iter->rel_entry,
> +						 &rel_head_iter->rel_entry,
>  						 head) {
>  				curr_type = rel_entry_iter->type;
>  				reloc_handlers[curr_type].reloc_handler(
> @@ -704,16 +704,7 @@ static int add_relocation_to_accumulate(struct module *me, int type,
>  			return -ENOMEM;
>  		}
>  
> -		rel_head->rel_entry =
> -			kmalloc(sizeof(struct list_head), GFP_KERNEL);
> -
> -		if (!rel_head->rel_entry) {
> -			kfree(entry);
> -			kfree(rel_head);
> -			return -ENOMEM;
> -		}
> -
> -		INIT_LIST_HEAD(rel_head->rel_entry);
> +		INIT_LIST_HEAD(&rel_head->rel_entry);
>  		rel_head->location = location;
>  		INIT_HLIST_NODE(&rel_head->node);
>  		if (!current_head->first) {
> @@ -722,7 +713,6 @@ static int add_relocation_to_accumulate(struct module *me, int type,
>  
>  			if (!bucket) {
>  				kfree(entry);
> -				kfree(rel_head->rel_entry);
>  				kfree(rel_head);
>  				return -ENOMEM;
>  			}
> @@ -735,7 +725,7 @@ static int add_relocation_to_accumulate(struct module *me, int type,
>  	}
>  
>  	/* Add relocation to head of discovered rel_head */
> -	list_add_tail(&entry->head, rel_head->rel_entry);
> +	list_add_tail(&entry->head, &rel_head->rel_entry);
>  
>  	return 0;
>  }
> -- 
> 2.45.2
> 
>

Other than the commit message change suggestions,

Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Clément Léger Nov. 27, 2024, 2:18 p.m. UTC | #2
On 27/11/2024 15:02, Andrew Jones wrote:
> On Wed, Nov 27, 2024 at 11:30:14AM +0100, Clément Léger wrote:
>> list_head does not need to be allocated, it can be a plain variable.
> 
> rel_head's list_head member, rel_entry, doesn't need to be allocated,
> its storage can just be part of the allocated rel_head.
> 
>> Remove the pointer which allows to get rid of the allocation as well as
>> an existing memory leak.
> 
> It'd be nice to add how the memory leak was found. Inspection or some
> tool?

Yeah sure, it was actually found by Kai Zang, I have added him as Cc but
I can surely give him credit for finding the memleak, I'll add him with
a Reported-by.

Thanks,

Clément

> 
>>
>> Signed-off-by: Clément Léger <cleger@rivosinc.com>
>>
>> ---
>>  arch/riscv/kernel/module.c | 18 ++++--------------
>>  1 file changed, 4 insertions(+), 14 deletions(-)
>>
>> diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
>> index 1cd461f3d872..47d0ebeec93c 100644
>> --- a/arch/riscv/kernel/module.c
>> +++ b/arch/riscv/kernel/module.c
>> @@ -23,7 +23,7 @@ struct used_bucket {
>>  
>>  struct relocation_head {
>>  	struct hlist_node node;
>> -	struct list_head *rel_entry;
>> +	struct list_head rel_entry;
>>  	void *location;
>>  };
>>  
>> @@ -634,7 +634,7 @@ process_accumulated_relocations(struct module *me,
>>  			location = rel_head_iter->location;
>>  			list_for_each_entry_safe(rel_entry_iter,
>>  						 rel_entry_iter_tmp,
>> -						 rel_head_iter->rel_entry,
>> +						 &rel_head_iter->rel_entry,
>>  						 head) {
>>  				curr_type = rel_entry_iter->type;
>>  				reloc_handlers[curr_type].reloc_handler(
>> @@ -704,16 +704,7 @@ static int add_relocation_to_accumulate(struct module *me, int type,
>>  			return -ENOMEM;
>>  		}
>>  
>> -		rel_head->rel_entry =
>> -			kmalloc(sizeof(struct list_head), GFP_KERNEL);
>> -
>> -		if (!rel_head->rel_entry) {
>> -			kfree(entry);
>> -			kfree(rel_head);
>> -			return -ENOMEM;
>> -		}
>> -
>> -		INIT_LIST_HEAD(rel_head->rel_entry);
>> +		INIT_LIST_HEAD(&rel_head->rel_entry);
>>  		rel_head->location = location;
>>  		INIT_HLIST_NODE(&rel_head->node);
>>  		if (!current_head->first) {
>> @@ -722,7 +713,6 @@ static int add_relocation_to_accumulate(struct module *me, int type,
>>  
>>  			if (!bucket) {
>>  				kfree(entry);
>> -				kfree(rel_head->rel_entry);
>>  				kfree(rel_head);
>>  				return -ENOMEM;
>>  			}
>> @@ -735,7 +725,7 @@ static int add_relocation_to_accumulate(struct module *me, int type,
>>  	}
>>  
>>  	/* Add relocation to head of discovered rel_head */
>> -	list_add_tail(&entry->head, rel_head->rel_entry);
>> +	list_add_tail(&entry->head, &rel_head->rel_entry);
>>  
>>  	return 0;
>>  }
>> -- 
>> 2.45.2
>>
>>
> 
> Other than the commit message change suggestions,
> 
> Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
diff mbox series

Patch

diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
index 1cd461f3d872..47d0ebeec93c 100644
--- a/arch/riscv/kernel/module.c
+++ b/arch/riscv/kernel/module.c
@@ -23,7 +23,7 @@  struct used_bucket {
 
 struct relocation_head {
 	struct hlist_node node;
-	struct list_head *rel_entry;
+	struct list_head rel_entry;
 	void *location;
 };
 
@@ -634,7 +634,7 @@  process_accumulated_relocations(struct module *me,
 			location = rel_head_iter->location;
 			list_for_each_entry_safe(rel_entry_iter,
 						 rel_entry_iter_tmp,
-						 rel_head_iter->rel_entry,
+						 &rel_head_iter->rel_entry,
 						 head) {
 				curr_type = rel_entry_iter->type;
 				reloc_handlers[curr_type].reloc_handler(
@@ -704,16 +704,7 @@  static int add_relocation_to_accumulate(struct module *me, int type,
 			return -ENOMEM;
 		}
 
-		rel_head->rel_entry =
-			kmalloc(sizeof(struct list_head), GFP_KERNEL);
-
-		if (!rel_head->rel_entry) {
-			kfree(entry);
-			kfree(rel_head);
-			return -ENOMEM;
-		}
-
-		INIT_LIST_HEAD(rel_head->rel_entry);
+		INIT_LIST_HEAD(&rel_head->rel_entry);
 		rel_head->location = location;
 		INIT_HLIST_NODE(&rel_head->node);
 		if (!current_head->first) {
@@ -722,7 +713,6 @@  static int add_relocation_to_accumulate(struct module *me, int type,
 
 			if (!bucket) {
 				kfree(entry);
-				kfree(rel_head->rel_entry);
 				kfree(rel_head);
 				return -ENOMEM;
 			}
@@ -735,7 +725,7 @@  static int add_relocation_to_accumulate(struct module *me, int type,
 	}
 
 	/* Add relocation to head of discovered rel_head */
-	list_add_tail(&entry->head, rel_head->rel_entry);
+	list_add_tail(&entry->head, &rel_head->rel_entry);
 
 	return 0;
 }