diff mbox series

[net,v3,1/7] octeon_ep: Add checks to fix double free crashes.

Message ID 20241108074543.1123036-2-srasheed@marvell.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series Double free fixes and NULL pointer checks | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 3 this patch: 3
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 9 of 9 maintainers
netdev/build_clang success Errors and warnings before: 3 this patch: 3
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 4 this patch: 4
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns WARNING: line length of 83 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns WARNING: line length of 87 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 20 this patch: 20
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-11-09--03-00 (tests: 784)

Commit Message

Shinas Rasheed Nov. 8, 2024, 7:45 a.m. UTC
From: Vimlesh Kumar <vimleshk@marvell.com>

Add required checks to avoid double free. Crashes were
observed due to the same on reset scenarios

Fixes: 37d79d059606 ("octeon_ep: add Tx/Rx processing and interrupt support")
Signed-off-by: Vimlesh Kumar <vimleshk@marvell.com>
Signed-off-by: Shinas Rasheed <srasheed@marvell.com>
---
V3:
  - Added back "Fixes" to the changelist

V2: https://lore.kernel.org/all/20241107132846.1118835-2-srasheed@marvell.com/
  - No changes

V1: https://lore.kernel.org/all/20241101103416.1064930-2-srasheed@marvell.com/

 .../ethernet/marvell/octeon_ep/octep_main.c   | 39 +++++++++++--------
 .../net/ethernet/marvell/octeon_ep/octep_tx.c |  2 +
 2 files changed, 25 insertions(+), 16 deletions(-)

Comments

Vadim Fedorenko Nov. 9, 2024, 3:10 p.m. UTC | #1
On 08/11/2024 07:45, Shinas Rasheed wrote:
> From: Vimlesh Kumar <vimleshk@marvell.com>
> 
> Add required checks to avoid double free. Crashes were
> observed due to the same on reset scenarios
> 
> Fixes: 37d79d059606 ("octeon_ep: add Tx/Rx processing and interrupt support")
> Signed-off-by: Vimlesh Kumar <vimleshk@marvell.com>
> Signed-off-by: Shinas Rasheed <srasheed@marvell.com>

As you reposted in <24h, could you please take a look at the comments in v2?

FYI, 24h rule in netdev:
https://docs.kernel.org/process/maintainer-netdev.html

Thanks.
Shinas Rasheed Nov. 11, 2024, 5:21 a.m. UTC | #2
Hi Vadim,

On 08/11/2024 07:45, Shinas Rasheed wrote:
>> From: Vimlesh Kumar <mailto:vimleshk@marvell.com>
>> 
>> Add required checks to avoid double free. Crashes were
>> observed due to the same on reset scenarios
>> 
>> Fixes: 37d79d059606 ("octeon_ep: add Tx/Rx processing and interrupt support")
>> Signed-off-by: Vimlesh Kumar <mailto:vimleshk@marvell.com>
>> Signed-off-by: Shinas Rasheed <mailto:srasheed@marvell.com>
>
>As you reposted in <24h, could you please take a look at the comments in v2?
>
>FYI, 24h rule in netdev:
>https://urldefense.proofpoint.com/v2/url?u=https-3A__docs.kernel.org_process_maintainer-2Dnetdev.html&d=DwICaQ&c=nKjWec2b6R0mOyPaz7xtfQ&r=1OxLD4y-oxrlgQ1rjXgWtmLz1pnaDjD96sDq->cKUwK4&m=VcY_pBmYwqgydJILlz4916iUWNPjepPuBfe3p4cw71yyziV7gvWgoDV9pEY1gq1g&s=tAroBPYvyDp0HN_HVt1jDiWKnBrMeyy0bzTq8AEgA64&e=
>
>Thanks.

Extremely sorry for that, I had noticed the fixes comment was missing and mistakenly thought that it had been a day. Sure, I'll take a look at the v2 comments

Thanks for your time!
Shinas Rasheed Nov. 12, 2024, 1:03 p.m. UTC | #3
Hi Vadim,

Replying to the V2 patch comments:

On 07/11/2024 13:28, Shinas Rasheed wrote:
>> From: Vimlesh Kumar <vimleshk@marvell.com>
>> 
>> Add required checks to avoid double free. Crashes were
>> observed due to the same on reset scenarios
>> 
>> Signed-off-by: Vimlesh Kumar <vimleshk@marvell.com>
>> Signed-off-by: Shinas Rasheed <srasheed@marvell.com>
>> ---
>> V2:
>>    - No changes
>> 
>> V1: https://urldefense.proofpoint.com/v2/url?u=https-3A__lore.kernel.org_all_20241101103416.1064930-2D2-2Dsrasheed-40marvell.com_&d=DwICaQ&c=nKjWec2b6R0mOyPaz7xtfQ&r=1OxLD4y-oxrlgQ1rjXgWtmLz1pnaDjD96sDq-cKUwK4&m=qcKdIM1zsbBD4TFAsa19yJzKNkTlFLrDfM4ffSlTitSWElqZqgdFqodALcL83iWB&s=M_kpJB7LL7JqIBk-MBXonCyf_BWg3fcwHuMc8FQpun0&e=
>> 
>>   .../ethernet/marvell/octeon_ep/octep_main.c   | 39 +++++++++++--------
>>   .../net/ethernet/marvell/octeon_ep/octep_tx.c |  2 +
>>   2 files changed, 25 insertions(+), 16 deletions(-)
>> 
>> diff --git a/drivers/net/ethernet/marvell/octeon_ep/octep_main.c b/drivers/net/ethernet/marvell/octeon_ep/octep_main.c
>> index 549436efc204..ff72b796bd25 100644
>> --- a/drivers/net/ethernet/marvell/octeon_ep/octep_main.c
>> +++ b/drivers/net/ethernet/marvell/octeon_ep/octep_main.c
>>@@ -154,9 +154,11 @@ static int octep_enable_msix_range(struct octep_device *oct)
>>  */
>>   static void octep_disable_msix(struct octep_device *oct)
>>   {
>> -	pci_disable_msix(oct->pdev);
>> -	kfree(oct->msix_entries);
>> -	oct->msix_entries = NULL;
>> +	if (oct->msix_entries) {
>> +		pci_disable_msix(oct->pdev);
>> +		kfree(oct->msix_entries);
>> +		oct->msix_entries = NULL;
>> +	}
>>   	dev_info(&oct->pdev->dev, "Disabled MSI-X\n");
>
>How can this function crash? pci_disable_msix() will have checks for
>already disabled msix, kfree can properly deal with NULL pointer.
>Do you have stack trace of the crash here?
>

I think you're right. This won't perhaps be the actual part of the code "fixing"
the crash, and might just as a protection. I shall remove this.

>>  }
>>   
>> @@ -496,16 +498,18 @@ static void octep_free_irqs(struct octep_device *oct)
>>  {
>>   	int i;
>>   
>> -	/* First few MSI-X interrupts are non queue interrupts; free them */
>> -	for (i = 0; i < CFG_GET_NON_IOQ_MSIX(oct->conf); i++)
>> -		free_irq(oct->msix_entries[i].vector, oct);
>> -	kfree(oct->non_ioq_irq_names);
>> -
>> -	/* Free IRQs for Input/Output (Tx/Rx) queues */
>> -	for (i = CFG_GET_NON_IOQ_MSIX(oct->conf); i < oct->num_irqs; i++) {
>> -		irq_set_affinity_hint(oct->msix_entries[i].vector, NULL);
>> -		free_irq(oct->msix_entries[i].vector,
>> -			 oct->ioq_vector[i - CFG_GET_NON_IOQ_MSIX(oct->conf)]);
>> +	if (oct->msix_entries) {
>> +		/* First few MSI-X interrupts are non queue interrupts; free them */
>> +		for (i = 0; i < CFG_GET_NON_IOQ_MSIX(oct->conf); i++)
>> +			free_irq(oct->msix_entries[i].vector, oct);
>> +		kfree(oct->non_ioq_irq_names);
>> +
>> +		/* Free IRQs for Input/Output (Tx/Rx) queues */
>> +		for (i = CFG_GET_NON_IOQ_MSIX(oct->conf); i < oct->num_irqs; i++) {
>> +			irq_set_affinity_hint(oct->msix_entries[i].vector, NULL);
>> +			free_irq(oct->msix_entries[i].vector,
>> +				 oct->ioq_vector[i - CFG_GET_NON_IOQ_MSIX(oct->conf)]);
>> +		}
>>   	}
>>   	netdev_info(oct->netdev, "IRQs freed\n");
>>   }
>
>Have you considered fast return option? like
>
>if (!octep_disable_msix)
>	return;
>
>It will make less intendation and less changes in LoC but will presume
>the same behavior.
>

Do you mean this:
if (!oct->msix_entries)
	return;
	
I'll make that change as well.

Thanks!
Shinas
diff mbox series

Patch

diff --git a/drivers/net/ethernet/marvell/octeon_ep/octep_main.c b/drivers/net/ethernet/marvell/octeon_ep/octep_main.c
index 549436efc204..ff72b796bd25 100644
--- a/drivers/net/ethernet/marvell/octeon_ep/octep_main.c
+++ b/drivers/net/ethernet/marvell/octeon_ep/octep_main.c
@@ -154,9 +154,11 @@  static int octep_enable_msix_range(struct octep_device *oct)
  */
 static void octep_disable_msix(struct octep_device *oct)
 {
-	pci_disable_msix(oct->pdev);
-	kfree(oct->msix_entries);
-	oct->msix_entries = NULL;
+	if (oct->msix_entries) {
+		pci_disable_msix(oct->pdev);
+		kfree(oct->msix_entries);
+		oct->msix_entries = NULL;
+	}
 	dev_info(&oct->pdev->dev, "Disabled MSI-X\n");
 }
 
@@ -496,16 +498,18 @@  static void octep_free_irqs(struct octep_device *oct)
 {
 	int i;
 
-	/* First few MSI-X interrupts are non queue interrupts; free them */
-	for (i = 0; i < CFG_GET_NON_IOQ_MSIX(oct->conf); i++)
-		free_irq(oct->msix_entries[i].vector, oct);
-	kfree(oct->non_ioq_irq_names);
-
-	/* Free IRQs for Input/Output (Tx/Rx) queues */
-	for (i = CFG_GET_NON_IOQ_MSIX(oct->conf); i < oct->num_irqs; i++) {
-		irq_set_affinity_hint(oct->msix_entries[i].vector, NULL);
-		free_irq(oct->msix_entries[i].vector,
-			 oct->ioq_vector[i - CFG_GET_NON_IOQ_MSIX(oct->conf)]);
+	if (oct->msix_entries) {
+		/* First few MSI-X interrupts are non queue interrupts; free them */
+		for (i = 0; i < CFG_GET_NON_IOQ_MSIX(oct->conf); i++)
+			free_irq(oct->msix_entries[i].vector, oct);
+		kfree(oct->non_ioq_irq_names);
+
+		/* Free IRQs for Input/Output (Tx/Rx) queues */
+		for (i = CFG_GET_NON_IOQ_MSIX(oct->conf); i < oct->num_irqs; i++) {
+			irq_set_affinity_hint(oct->msix_entries[i].vector, NULL);
+			free_irq(oct->msix_entries[i].vector,
+				 oct->ioq_vector[i - CFG_GET_NON_IOQ_MSIX(oct->conf)]);
+		}
 	}
 	netdev_info(oct->netdev, "IRQs freed\n");
 }
@@ -635,8 +639,10 @@  static void octep_napi_delete(struct octep_device *oct)
 
 	for (i = 0; i < oct->num_oqs; i++) {
 		netdev_dbg(oct->netdev, "Deleting NAPI on Q-%d\n", i);
-		netif_napi_del(&oct->ioq_vector[i]->napi);
-		oct->oq[i]->napi = NULL;
+		if (oct->oq[i]->napi) {
+			netif_napi_del(&oct->ioq_vector[i]->napi);
+			oct->oq[i]->napi = NULL;
+		}
 	}
 }
 
@@ -666,7 +672,8 @@  static void octep_napi_disable(struct octep_device *oct)
 
 	for (i = 0; i < oct->num_oqs; i++) {
 		netdev_dbg(oct->netdev, "Disabling NAPI on Q-%d\n", i);
-		napi_disable(&oct->ioq_vector[i]->napi);
+		if (oct->oq[i]->napi)
+			napi_disable(&oct->ioq_vector[i]->napi);
 	}
 }
 
diff --git a/drivers/net/ethernet/marvell/octeon_ep/octep_tx.c b/drivers/net/ethernet/marvell/octeon_ep/octep_tx.c
index 06851b78aa28..157bf489ae19 100644
--- a/drivers/net/ethernet/marvell/octeon_ep/octep_tx.c
+++ b/drivers/net/ethernet/marvell/octeon_ep/octep_tx.c
@@ -323,6 +323,8 @@  void octep_free_iqs(struct octep_device *oct)
 	int i;
 
 	for (i = 0; i < CFG_GET_PORTS_ACTIVE_IO_RINGS(oct->conf); i++) {
+		if (!oct->iq[i])
+			continue;
 		octep_free_iq(oct->iq[i]);
 		dev_dbg(&oct->pdev->dev,
 			"Successfully destroyed IQ(TxQ)-%d.\n", i);