Skip to content

Commit 4b38b36

Browse files
charlie-rivospalmer-dabbelt
authored andcommitted
riscv: Correctly free relocation hashtable on error
When there is not enough allocatable memory for the relocation hashtable, module loading should exit gracefully. Previously, this was attempted to be accomplished by checking if an unsigned number is less than zero which does not work. Instead have the caller check if the hashtable was correctly allocated and add a comment explaining that hashtable_bits that is 0 is valid. Signed-off-by: Charlie Jenkins <[email protected]> Fixes: d8792a5 ("riscv: Safely remove entries from relocation list") Reported-by: kernel test robot <[email protected]> Reported-by: Dan Carpenter <[email protected]> Closes: https://lore.kernel.org/r/[email protected]/ Reported-by: kernel test robot <[email protected]> Reported-by: Julia Lawall <[email protected]> Closes: https://lore.kernel.org/r/[email protected]/ Reviewed-by: Dan Carpenter <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 78996ee commit 4b38b36

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

arch/riscv/kernel/module.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,10 @@ initialize_relocation_hashtable(unsigned int num_relocations,
747747
{
748748
/* Can safely assume that bits is not greater than sizeof(long) */
749749
unsigned long hashtable_size = roundup_pow_of_two(num_relocations);
750+
/*
751+
* When hashtable_size == 1, hashtable_bits == 0.
752+
* This is valid because the hashing algorithm returns 0 in this case.
753+
*/
750754
unsigned int hashtable_bits = ilog2(hashtable_size);
751755

752756
/*
@@ -763,7 +767,7 @@ initialize_relocation_hashtable(unsigned int num_relocations,
763767
sizeof(*relocation_hashtable),
764768
GFP_KERNEL);
765769
if (!*relocation_hashtable)
766-
return -ENOMEM;
770+
return 0;
767771

768772
__hash_init(*relocation_hashtable, hashtable_size);
769773

@@ -789,8 +793,8 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
789793
hashtable_bits = initialize_relocation_hashtable(num_relocations,
790794
&relocation_hashtable);
791795

792-
if (hashtable_bits < 0)
793-
return hashtable_bits;
796+
if (!relocation_hashtable)
797+
return -ENOMEM;
794798

795799
INIT_LIST_HEAD(&used_buckets_list);
796800

0 commit comments

Comments
 (0)