bnx2fc: fix memory leaks and NULL pointer dereferences
Subsystems (2)
PATCH v3 3/3 bnx2fc: fix memory leak in bnx2fc_allocate_hash_table()
In reply to Maurizio LombardiIn case of error, the bnx2fc_allocate_hash_table() didn't free all the memory it allocated.
--- drivers/scsi/bnx2fc/bnx2fc_hwi.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-)
drivers/scsi/bnx2fc/bnx2fc_hwi.c +19 -12
@@ -2026,7 +2026,7 @@ static int bnx2fc_allocate_hash_table(struct bnx2fc_hba *hba) dma_segment_array =kzalloc (dma_segment_array_size,GFP_KERNEL );if (!dma_segment_array) { printk(KERN_ERR PFX "hash table pointers (dma) alloc failed\n"); -return -ENOMEM ; + goto cleanup_ht; }for (i =0 ; i < segment_count; ++i) {
@@ -2037,15 +2037,7 @@ static int bnx2fc_allocate_hash_table(struct bnx2fc_hba *hba) GFP_KERNEL);if (!hba->hash_tbl_segments[i]) { printk(KERN_ERR PFX "hash segment alloc failed\n"); -while (--i >=0 ) { -dma_free_coherent (&hba->pcidev->dev, -BNX2FC_HASH_TBL_CHUNK_SIZE , - hba->hash_tbl_segments[i], - dma_segment_array[i]); - hba->hash_tbl_segments[i] =NULL ; - } -kfree (dma_segment_array); -return -ENOMEM ; + goto cleanup_dma; }memset (hba->hash_tbl_segments[i],0 ,BNX2FC_HASH_TBL_CHUNK_SIZE );
@@ -2057,8 +2049,7 @@ static int bnx2fc_allocate_hash_table(struct bnx2fc_hba *hba) GFP_KERNEL);if (!hba->hash_tbl_pbl) { printk(KERN_ERR PFX "hash table pbl alloc failed\n"); -kfree (dma_segment_array); -return -ENOMEM ; + goto cleanup_dma; }memset (hba->hash_tbl_pbl,0 ,PAGE_SIZE );
@@ -2083,6 +2074,22 @@ static int bnx2fc_allocate_hash_table(struct bnx2fc_hba *hba) }kfree (dma_segment_array);return 0 ; + +cleanup_dma: +for (i =0 ; i < segment_count; ++i) { +if (hba->hash_tbl_segments[i]) +dma_free_coherent (&hba->pcidev->dev, +BNX2FC_HASH_TBL_CHUNK_SIZE , + hba->hash_tbl_segments[i], + dma_segment_array[i]); + } + +kfree (dma_segment_array); + +cleanup_ht: +kfree (hba->hash_tbl_segments); + hba->hash_tbl_segments =NULL ; +return -ENOMEM ; }/**
Signature · 1 line
Maurizio Lombardi
v3 cover bnx2fc: fix memory leaks and NULL pointer dereferences
PATCH 1/3 removes a unused variable from the bnx2fc_free_hash_table() function,
PATCH 2/3 fixes a memory leak and some NULL pointer dereferences in the bnx2fc_free_hash_table() function that may happen if bnx2fc_allocate_hash_table() fails.
PATCH 3/3 fixes a memory leak in the bnx2fc_allocate_hash_table() function.
Maurizio Lombardi (3):
bnx2fc: remove unused variable hash_table_size
bnx2fc: fix memory leak and potential NULL pointer dereference.
bnx2fc: fix memory leak in bnx2fc_allocate_hash_table()
drivers/scsi/bnx2fc/bnx2fc_hwi.c | 64 +++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 27 deletions(-)
Signature · 1 line
Maurizio Lombardi
PATCH v3 1/3 bnx2fc: remove unused variable hash_table_size
hash_table_size is not used by the bnx2fc_free_hash_table() function.
--- drivers/scsi/bnx2fc/bnx2fc_hwi.c | 3 --- 1 file changed, 3 deletions(-)
drivers/scsi/bnx2fc/bnx2fc_hwi.c +0 -3
@@ -1966,12 +1966,9 @@ static void bnx2fc_free_hash_table(struct bnx2fc_hba *hba) {int i;int segment_count; -int hash_table_size;u32 *pbl; segment_count = hba->hash_tbl_segment_count; - hash_table_size =BNX2FC_NUM_MAX_SESS *BNX2FC_MAX_ROWS_IN_HASH_TBL * -sizeof (struct fcoe_hash_table_entry ); pbl = hba->hash_tbl_pbl;for (i =0 ; i < segment_count; ++i) {
Signature · 1 line
Maurizio Lombardi
PATCH v3 2/3 bnx2fc: fix memory leak and potential NULL pointer dereference.
In reply to Maurizio LombardiIf bnx2fc_allocate_hash_table() for some reasons fails, it is possible that the hash_tbl_segments or the hash_tbl_pbl pointers are NULL.
In this case bnx2fc_free_hash_table() will panic the system.
this patch also fixes a memory leak, the hash_tbl_segments pointer was never freed.
--- drivers/scsi/bnx2fc/bnx2fc_hwi.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-)
drivers/scsi/bnx2fc/bnx2fc_hwi.c +20 -14
@@ -1968,21 +1968,27 @@ static void bnx2fc_free_hash_table(struct bnx2fc_hba *hba)int segment_count;u32 *pbl; - segment_count = hba->hash_tbl_segment_count; - - pbl = hba->hash_tbl_pbl; -for (i =0 ; i < segment_count; ++i) { -dma_addr_t dma_address; - - dma_address =le32_to_cpu (*pbl); - ++pbl; - dma_address += ((u64 )le32_to_cpu (*pbl)) <<32 ; - ++pbl; -dma_free_coherent (&hba->pcidev->dev, -BNX2FC_HASH_TBL_CHUNK_SIZE , - hba->hash_tbl_segments[i], - dma_address); +if (hba->hash_tbl_segments) { + + pbl = hba->hash_tbl_pbl; +if (pbl) { + segment_count = hba->hash_tbl_segment_count; +for (i =0 ; i < segment_count; ++i) { +dma_addr_t dma_address; + + dma_address =le32_to_cpu (*pbl); + ++pbl; + dma_address += ((u64 )le32_to_cpu (*pbl)) <<32 ; + ++pbl; +dma_free_coherent (&hba->pcidev->dev, +BNX2FC_HASH_TBL_CHUNK_SIZE , + hba->hash_tbl_segments[i], + dma_address); + } + } +kfree (hba->hash_tbl_segments); + hba->hash_tbl_segments =NULL ; }if (hba->hash_tbl_pbl) {
Signature · 1 line
Maurizio Lombardi
The patchset looks good. Thanks.
Quoted 15 lines · Maurizio Lombardi
PATCH 1/3 removes a unused variable from the
bnx2fc_free_hash_table()function,PATCH 2/3 fixes a memory leak and some NULL pointer dereferences in the
bnx2fc_free_hash_table()function that may happen ifbnx2fc_allocate_hash_table()fails.PATCH 3/3 fixes a memory leak in the
bnx2fc_allocate_hash_table()function.Maurizio Lombardi (3):
bnx2fc: remove unused variablehash_table_size
bnx2fc: fix memory leak and potential NULL pointer dereference.
bnx2fc: fix memory leak inbnx2fc_allocate_hash_table()drivers/scsi/bnx2fc/bnx2fc_hwi.c | 64 +++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 27 deletions(-)