Skip to contentSkip to conversation

et131x: fix allocation failures

PATCH In review Alan 3 participants 6 messages Feb 17, 2014 – Feb 21, 2014 Staging

lore
Share & tools
lore

Alan

author
#0 lore raw

PATCH 1/1 et131x: fix allocation failures

We should check the ring allocations don't fail.
If we get a fail we need to clean up properly. The allocator assumes the deallocator will be used on failure, but it isn't. Make sure the right deallocator is always called and add a missing check against fbr allocation failure.

[v2]: Correct check logic

---
 drivers/staging/et131x/et131x.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/et131x/et131x.c +7 -2
index 6413500..cc600df 100644
@@ -2124,7 +2124,11 @@ static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
 
 	/* Alloc memory for the lookup table */
 	rx_ring->fbr[0] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
+	if (rx_ring->fbr[0] == NULL)
+		return -ENOMEM;
 	rx_ring->fbr[1] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
+	if (rx_ring->fbr[1] == NULL)
+		return -ENOMEM;
 
 	/* The first thing we will do is configure the sizes of the buffer
 	 * rings. These will change based on jumbo packet support.  Larger
@@ -2289,7 +2293,7 @@ static void et131x_rx_dma_memory_free(struct et131x_adapter *adapter)
 	for (id = 0; id < NUM_FBRS; id++) {
 		fbr = rx_ring->fbr[id];
 
-		if (!fbr->ring_virtaddr)
+		if (!fbr || !fbr->ring_virtaddr)
 			continue;
 
 		/* First the packet memory */
@@ -3591,6 +3595,7 @@ static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
 	if (status) {
 		dev_err(&adapter->pdev->dev,
 			  "et131x_tx_dma_memory_alloc FAILED\n");
+		et131x_tx_dma_memory_free(adapter);
 		return status;
 	}
 	/* Receive buffer memory allocation */
@@ -3598,7 +3603,7 @@ static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
 	if (status) {
 		dev_err(&adapter->pdev->dev,
 			  "et131x_rx_dma_memory_alloc FAILED\n");
-		et131x_tx_dma_memory_free(adapter);
+		et131x_adapter_memory_free(adapter);
 		return status;
 	}
 
#1 lore raw

Alan, thanks for resending this patch. But it seems you overlooked something we discussed earlier.

Quoted 18 lines · Alan

We should check the ring allocations don't fail.
If we get a fail we need to clean up properly. The allocator assumes the deallocator will be used on failure, but it isn't. Make sure the right deallocator is always called and add a missing check against fbr allocation failure.

[v2]: Correct check logic

---
 drivers/staging/et131x/et131x.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/et131x/et131x.c +0 -0
index 6413500..cc600df 100644

drivers/staging/et131x/et131x.c static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter) · line 2131

Quoted 8 lines
drivers/staging/et131x/et131x.c +4 -0
@@ -2124,7 +2124,11 @@ static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
 
 	/* Alloc memory for the lookup table */
 	rx_ring->fbr[0] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
+	if (rx_ring->fbr[0] == NULL)
+		return -ENOMEM;
 	rx_ring->fbr[1] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
+	if (rx_ring->fbr[1] == NULL)
+		return -ENOMEM;

Shouldn't rx_ring->fbr[0] be freed when allocation of rx_ring->fbr[1] fails ? Or we will leak memory here.

Quoted 29 lines
drivers/staging/et131x/et131x.c +3 -2
 	/* The first thing we will do is configure the sizes of the buffer
 	 * rings. These will change based on jumbo packet support.  Larger
@@ -2289,7 +2293,7 @@ static void et131x_rx_dma_memory_free(struct et131x_adapter *adapter)
 	for (id = 0; id < NUM_FBRS; id++) {
 		fbr = rx_ring->fbr[id];
 
-		if (!fbr->ring_virtaddr)
+		if (!fbr || !fbr->ring_virtaddr)
 			continue;
 
 		/* First the packet memory */
@@ -3591,6 +3595,7 @@ static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
 	if (status) {
 		dev_err(&adapter->pdev->dev,
 			  "et131x_tx_dma_memory_alloc FAILED\n");
+		et131x_tx_dma_memory_free(adapter);
 		return status;
 	}
 	/* Receive buffer memory allocation */
@@ -3598,7 +3603,7 @@ static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
 	if (status) {
 		dev_err(&adapter->pdev->dev,
 			  "et131x_rx_dma_memory_alloc FAILED\n");
-		et131x_tx_dma_memory_free(adapter);
+		et131x_adapter_memory_free(adapter);
 		return status;
 	}
 
#2 lore raw
Quoted 23 lines · Gang

Alan, thanks for resending this patch. But it seems you overlooked something we discussed earlier.

Quoted 19 lines · Alan

We should check the ring allocations don't fail.
If we get a fail we need to clean up properly. The allocator assumes the deallocator will be used on failure, but it isn't. Make sure the right deallocator is always called and add a missing check against fbr allocation failure.

[v2]: Correct check logic

---
 drivers/staging/et131x/et131x.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/et131x/et131x.c +0 -0
index 6413500..cc600df 100644
@@ -2124,7 +2124,11 @@ static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
 
Quoted 10 lines

drivers/staging/et131x/et131x.c static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter) · line 2131

Quoted 7 lines
drivers/staging/et131x/et131x.c +4 -0
 	/* Alloc memory for the lookup table */
 	rx_ring->fbr[0] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
+	if (rx_ring->fbr[0] == NULL)
+		return -ENOMEM;
 	rx_ring->fbr[1] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
+	if (rx_ring->fbr[1] == NULL)
+		return -ENOMEM;

Shouldn't rx_ring->fbr[0] be freed when allocation of rx_ring->fbr[1] fails ? Or we will leak memory here.

No.. the tx_dma_memory_free and rx_dma_memory_free functions are designed to handle incomplete set up. They are now called on incomplete setup and will clean up all the resources.

Quoted 9 lines
Quoted 9 lines
drivers/staging/et131x/et131x.c +1 -0
@@ -3591,6 +3595,7 @@ static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
 	if (status) {
 		dev_err(&adapter->pdev->dev,
 			  "et131x_tx_dma_memory_alloc FAILED\n");
+		et131x_tx_dma_memory_free(adapter);
 		return status;
 	}
 	/* Receive buffer memory allocation */
@@ -3598,7 +3603,7 @@ static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
Quoted 8 lines

drivers/staging/et131x/et131x.c static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter) · line 3609

Quoted 8 lines
drivers/staging/et131x/et131x.c +1 -1
 	if (status) {
 		dev_err(&adapter->pdev->dev,
 			  "et131x_rx_dma_memory_alloc FAILED\n");
-		et131x_tx_dma_memory_free(adapter);
+		et131x_adapter_memory_free(adapter);
 		return status;
 	}
 

Which is what these changes are about.

Whoever wrote the allocator and cleanup methods arranged (except for the rx_ring->fbr cases) that the free method should be called on a failure. It looks as if somewhere along the line of the driver development whoever wrote the higher level bits didn't understand that.

Alan

#3 lore raw
Quoted 37 lines · One Thousand Gnomes
Quoted 23 lines · Gang

Alan, thanks for resending this patch. But it seems you overlooked something we discussed earlier.

Quoted 19 lines · Alan

We should check the ring allocations don't fail.
If we get a fail we need to clean up properly. The allocator assumes the deallocator will be used on failure, but it isn't. Make sure the right deallocator is always called and add a missing check against fbr allocation failure.

[v2]: Correct check logic

---
 drivers/staging/et131x/et131x.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/et131x/et131x.c +0 -0
index 6413500..cc600df 100644
@@ -2124,7 +2124,11 @@ static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
 
Quoted 10 lines

drivers/staging/et131x/et131x.c static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter) · line 2131

Quoted 7 lines
drivers/staging/et131x/et131x.c +4 -0
 	/* Alloc memory for the lookup table */
 	rx_ring->fbr[0] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
+	if (rx_ring->fbr[0] == NULL)
+		return -ENOMEM;
 	rx_ring->fbr[1] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
+	if (rx_ring->fbr[1] == NULL)
+		return -ENOMEM;

Shouldn't rx_ring->fbr[0] be freed when allocation of rx_ring->fbr[1] fails ? Or we will leak memory here.

Quoted 4 lines

No.. the tx_dma_memory_free and rx_dma_memory_free functions are designed to handle incomplete set up. They are now called on incomplete setup and will clean up all the resources.

Yes, you are right. By calling {tx, rx}_dma_memory_free the memory will be freed.

But I think a comment is needed here, to make this more clear ? Without proper comment the above code looks a little strange to let one think it's right. :)

Quoted 26 lines
Quoted 9 lines
Quoted 9 lines
drivers/staging/et131x/et131x.c +1 -0
@@ -3591,6 +3595,7 @@ static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
 	if (status) {
 		dev_err(&adapter->pdev->dev,
 			  "et131x_tx_dma_memory_alloc FAILED\n");
+		et131x_tx_dma_memory_free(adapter);
 		return status;
 	}
 	/* Receive buffer memory allocation */
@@ -3598,7 +3603,7 @@ static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
Quoted 8 lines

drivers/staging/et131x/et131x.c static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter) · line 3609

Quoted 8 lines
drivers/staging/et131x/et131x.c +1 -1
 	if (status) {
 		dev_err(&adapter->pdev->dev,
 			  "et131x_rx_dma_memory_alloc FAILED\n");
-		et131x_tx_dma_memory_free(adapter);
+		et131x_adapter_memory_free(adapter);
 		return status;
 	}
 

Which is what these changes are about.

Whoever wrote the allocator and cleanup methods arranged (except for the rx_ring->fbr cases) that the free method should be called on a failure. It looks as if somewhere along the line of the driver development whoever wrote the higher level bits didn't understand that.

Alan

#4 lore raw
Quoted 43 lines · Gang
Quoted 37 lines · One Thousand Gnomes
Quoted 23 lines · Gang

Alan, thanks for resending this patch. But it seems you overlooked something we discussed earlier.

Quoted 19 lines · Alan

We should check the ring allocations don't fail.
If we get a fail we need to clean up properly. The allocator assumes the deallocator will be used on failure, but it isn't. Make sure the right deallocator is always called and add a missing check against fbr allocation failure.

[v2]: Correct check logic

---
 drivers/staging/et131x/et131x.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/et131x/et131x.c +0 -0
index 6413500..cc600df 100644
@@ -2124,7 +2124,11 @@ static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
 
Quoted 10 lines

drivers/staging/et131x/et131x.c static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter) · line 2131

Quoted 7 lines
drivers/staging/et131x/et131x.c +4 -0
 	/* Alloc memory for the lookup table */
 	rx_ring->fbr[0] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
+	if (rx_ring->fbr[0] == NULL)
+		return -ENOMEM;
 	rx_ring->fbr[1] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
+	if (rx_ring->fbr[1] == NULL)
+		return -ENOMEM;

Shouldn't rx_ring->fbr[0] be freed when allocation of rx_ring->fbr[1] fails ? Or we will leak memory here.

Quoted 4 lines

No.. the tx_dma_memory_free and rx_dma_memory_free functions are designed to handle incomplete set up. They are now called on incomplete setup and will clean up all the resources.

Quoted 6 lines

Yes, you are right. By calling {tx, rx}_dma_memory_free the memory will be freed.

But I think a comment is needed here, to make this more clear ? Without proper comment the above code looks a little strange to let one think it's right. :)

No. We don't need a comment. If people start adding kfree() calls all over the place without thinking then we are already screwed and no comment is going to help us.

regards,
dan carpenter

#5 lore raw
Quoted 47 lines · Dan Carpenter
Quoted 46 lines · Gang
Quoted 37 lines · One Thousand Gnomes
Quoted 23 lines · Gang

Alan, thanks for resending this patch. But it seems you overlooked something we discussed earlier.

Quoted 19 lines · Alan

We should check the ring allocations don't fail.
If we get a fail we need to clean up properly. The allocator assumes the deallocator will be used on failure, but it isn't. Make sure the right deallocator is always called and add a missing check against fbr allocation failure.

[v2]: Correct check logic

---
 drivers/staging/et131x/et131x.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/staging/et131x/et131x.c +0 -0
index 6413500..cc600df 100644
@@ -2124,7 +2124,11 @@ static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
 
Quoted 10 lines

drivers/staging/et131x/et131x.c static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter) · line 2131

Quoted 7 lines
drivers/staging/et131x/et131x.c +4 -0
 	/* Alloc memory for the lookup table */
 	rx_ring->fbr[0] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
+	if (rx_ring->fbr[0] == NULL)
+		return -ENOMEM;
 	rx_ring->fbr[1] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
+	if (rx_ring->fbr[1] == NULL)
+		return -ENOMEM;

Shouldn't rx_ring->fbr[0] be freed when allocation of rx_ring->fbr[1] fails ? Or we will leak memory here.

Quoted 4 lines

No.. the tx_dma_memory_free and rx_dma_memory_free functions are designed to handle incomplete set up. They are now called on incomplete setup and will clean up all the resources.

Yes, you are right. By calling {tx, rx}_dma_memory_free the memory will be freed.

Quoted 7 lines
Quoted 3 lines

But I think a comment is needed here, to make this more clear ? Without proper comment the above code looks a little strange to let one think it's right. :)

No. We don't need a comment. If people start adding kfree() calls all over the place without thinking then we are already screwed and no comment is going to help us.

Hi, I thought this a little more.

AFAIK, most functions deal with this "fail in the middle" allocation failure themselves. Honestly, relying on the caller to handle this type of error seems a bad idea to me.

Code reviewer has to check *every* caller of this function to make sure whether rx_ring->fbr[0] is leaked or not when allocation of rx_ring->fbr[1] fails.(By examing if the caller called the correct freeing function when this function returns error) This is just a waste of time. By freeing rx_ring->fbr[0] in this function the above type of memory leak can't be happen at the beginning.

So now my suggestion is freeing rx_ring->fbr[0] *and* set the pointer rx_ring->fbr[0] to NULL when allocation of rx_ring->fbr[1] fails *in* this function. The freeing function which can handle "fail in the middle" allocation failure surely can handle this situation correctly, isn't it ?

Quoted 3 lines

regards,
dan carpenter