Skip to contentSkip to conversation

staging: tidspbridge: memory leak fixes

PATCH In review Omar Ramirez Luna 4 participants 12 messages Jan 31, 2012 – Feb 1, 2012 Staging

lore
Share & tools
lore
#0 lore raw

cover staging: tidspbridge: memory leak fixes

These patches solve two lingering memory leak scenarios introduced in tidspbridge code.

1. Due to missing balanced calls during bridge_release deallocation, and a potential leak in bridge_open because of missing cleanup path.
2. Leaks because of a misuse of an already freed pointer, which triggered error paths during module removal leaking some memory.

When accepted they must be propagated to stable kernel releases.

Omar Ramirez Luna (2):
staging: tidspbridge: fix bridge_open memory leaks
staging: tidspbridge: fix incorrect free to drv_datap

 drivers/staging/tidspbridge/core/tiomap3430.c    |    2 -
 drivers/staging/tidspbridge/rmgr/drv_interface.c |   58 +++++++++++++---------
 2 files changed, 35 insertions(+), 25 deletions(-)
Signature · 1 line
1.7.4.1
#1 lore raw

PATCH 1/2 staging: tidspbridge: fix bridge_open memory leaks

There are two members of pr_ctxt allocated during bridge_open that are never freed resulting in memory leaks, these are stream_id and node_id, they are now freed on release of the handle (bridge_release) right before freeing pr_ctxt.

Error path for bridge_open was also fixed since the same variables could result in memory leaking due to missing handling of failure scenarios. While at it, the indentation changes were introduced to avoid interleaved goto statements inside big if blocks.

---
 drivers/staging/tidspbridge/rmgr/drv_interface.c |   55 +++++++++++++---------
 1 files changed, 32 insertions(+), 23 deletions(-)
drivers/staging/tidspbridge/rmgr/drv_interface.c +32 -23
index 76cfc6e..8bac511 100644
@@ -500,35 +500,42 @@ static int bridge_open(struct inode *ip, struct file *filp)
 	}
 #endif
 	pr_ctxt = kzalloc(sizeof(struct process_context), GFP_KERNEL);
-	if (pr_ctxt) {
-		pr_ctxt->res_state = PROC_RES_ALLOCATED;
-		spin_lock_init(&pr_ctxt->dmm_map_lock);
-		INIT_LIST_HEAD(&pr_ctxt->dmm_map_list);
-		spin_lock_init(&pr_ctxt->dmm_rsv_lock);
-		INIT_LIST_HEAD(&pr_ctxt->dmm_rsv_list);
-
-		pr_ctxt->node_id = kzalloc(sizeof(struct idr), GFP_KERNEL);
-		if (pr_ctxt->node_id) {
-			idr_init(pr_ctxt->node_id);
-		} else {
-			status = -ENOMEM;
-			goto err;
-		}
+	if (!pr_ctxt)
+		return -ENOMEM;
+
+	pr_ctxt->res_state = PROC_RES_ALLOCATED;
+	spin_lock_init(&pr_ctxt->dmm_map_lock);
+	INIT_LIST_HEAD(&pr_ctxt->dmm_map_list);
+	spin_lock_init(&pr_ctxt->dmm_rsv_lock);
+	INIT_LIST_HEAD(&pr_ctxt->dmm_rsv_list);
 
-		pr_ctxt->stream_id = kzalloc(sizeof(struct idr), GFP_KERNEL);
-		if (pr_ctxt->stream_id)
-			idr_init(pr_ctxt->stream_id);
-		else
-			status = -ENOMEM;
-	} else {
+	pr_ctxt->node_id = kzalloc(sizeof(struct idr), GFP_KERNEL);
+	if (!pr_ctxt->node_id) {
 		status = -ENOMEM;
+		goto err1;
 	}
-err:
+
+	idr_init(pr_ctxt->node_id);
+
+	pr_ctxt->stream_id = kzalloc(sizeof(struct idr), GFP_KERNEL);
+	if (!pr_ctxt->stream_id) {
+		status = -ENOMEM;
+		goto err2;
+	}
+
+	idr_init(pr_ctxt->stream_id);
+
 	filp->private_data = pr_ctxt;
+
 #ifdef CONFIG_TIDSPBRIDGE_RECOVERY
-	if (!status)
-		atomic_inc(&bridge_cref);
+	atomic_inc(&bridge_cref);
 #endif
+	return 0;
+
+err2:
+	kfree(pr_ctxt->node_id);
+err1:
+	kfree(pr_ctxt);
 	return status;
 }
 
@@ -550,6 +557,8 @@ static int bridge_release(struct inode *ip, struct file *filp)
 	flush_signals(current);
 	drv_remove_all_resources(pr_ctxt);
 	proc_detach(pr_ctxt);
+	kfree(pr_ctxt->node_id);
+	kfree(pr_ctxt->stream_id);
 	kfree(pr_ctxt);
 
 	filp->private_data = NULL;
Signature · 1 line
1.7.4.1
#2 lore raw

Re: [PATCH 1/2] staging: tidspbridge: fix bridge_open memory leaks

Quoted 10 lines · Omar Ramirez Luna

There are two members of pr_ctxt allocated during bridge_open that are never freed resulting in memory leaks, these are stream_id and node_id, they are now freed on release of the handle (bridge_release) right before freeing pr_ctxt.

Error path for bridge_open was also fixed since the same variables could result in memory leaking due to missing handling of failure scenarios. While at it, the indentation changes were introduced to avoid interleaved goto statements inside big if blocks.

You mentioned in the cover letter email that you wanted these to go into stable. I don't think that is needed here. These are tiny leaks that we aren't going to hit in real life.

But if we did want to include them in stable then we would add a tag to the patch next to the Signed-off-by.

Read Documentation/stable_kernel_rules.txt. It's short.

regards,
dan carpenter

#3 lore raw

Re: [PATCH 1/2] staging: tidspbridge: fix bridge_open memory leaks

Quoted 12 lines · Dan Carpenter
Quoted 10 lines · Omar Ramirez Luna

There are two members of pr_ctxt allocated during bridge_open that are never freed resulting in memory leaks, these are stream_id and node_id, they are now freed on release of the handle (bridge_release) right before freeing pr_ctxt.

Error path for bridge_open was also fixed since the same variables could result in memory leaking due to missing handling of failure scenarios. While at it, the indentation changes were introduced to avoid interleaved goto statements inside big if blocks.

Quoted 3 lines

You mentioned in the cover letter email that you wanted these to go into stable.  I don't think that is needed here.  These are tiny leaks that we aren't going to hit in real life.

They are hit each time an app opens a handle e.g.:

gst-launch <something>

So after 'X' number of playbacks there won't be memory left.

Quoted 2 lines

But if we did want to include them in stable then we would add a tag to the patch next to the Signed-off-by.

I thought first wait for inclusion and then send it to stable list, given that there are other patches touching this file and I don't know the order they are going to be picked, it is likely that I have to rebase it given that I might be the last in the queue.

Quoted 1 line

Read Documentation/stable_kernel_rules.txt.  It's short.

I had read it before.

I don't mind these to sit outside stable tree if they are not required though.

Thanks,

Omar
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw

Re: [PATCH 1/2] staging: tidspbridge: fix bridge_open memory leaks

Quoted 17 lines · Omar Ramirez Luna
Quoted 12 lines · Dan Carpenter
Quoted 10 lines · Omar Ramirez Luna

There are two members of pr_ctxt allocated during bridge_open that are never freed resulting in memory leaks, these are stream_id and node_id, they are now freed on release of the handle (bridge_release) right before freeing pr_ctxt.

Error path for bridge_open was also fixed since the same variables could result in memory leaking due to missing handling of failure scenarios. While at it, the indentation changes were introduced to avoid interleaved goto statements inside big if blocks.

Quoted 3 lines

You mentioned in the cover letter email that you wanted these to go into stable.  I don't think that is needed here.  These are tiny leaks that we aren't going to hit in real life.

Quoted 6 lines

They are hit each time an app opens a handle e.g.:

gst-launch <something>

So after 'X' number of playbacks there won't be memory left.

Ah. That sounds worth while then.

regards,
dan carpenter

#5 lore raw

PATCH 2/2 staging: tidspbridge: fix incorrect free to drv_datap

In reply to Omar Ramirez Luna

This structure is still used after it has been freed, since it is being allocated in probe, calls to free it have been moved to module's remove routine.

This should fix the follwoing messages when attempting to remove the module:
drv_get_first_dev_extension: Failed to retrieve the object handle drv_get_first_dev_extension: Failed to retrieve the object handle
drv_destroy: Failed to store DRV object
mgr_destroy: Failed to store MGR object

---
 drivers/staging/tidspbridge/core/tiomap3430.c    |    2 --
 drivers/staging/tidspbridge/rmgr/drv_interface.c |    3 +++
 2 files changed, 3 insertions(+), 2 deletions(-)
drivers/staging/tidspbridge/core/tiomap3430.c +0 -2
index e1c4492..dde559d 100644
@@ -1046,8 +1046,6 @@ static int bridge_dev_destroy(struct bridge_dev_context *dev_ctxt)
 
 	/* Free the driver's device context: */
 	kfree(drv_datap->base_img);
-	kfree(drv_datap);
-	dev_set_drvdata(bridge, NULL);
 	kfree((void *)dev_ctxt);
 	return status;
 }
drivers/staging/tidspbridge/rmgr/drv_interface.c +3 -0
index 8bac511..385740b 100644
@@ -410,6 +410,9 @@ static int __devexit omap34_xx_bridge_remove(struct platform_device *pdev)
 		DBC_ASSERT(ret == true);
 	}
 
+	kfree(drv_datap);
+	dev_set_drvdata(bridge, NULL);
+
 func_cont:
 	mem_ext_phys_pool_release();
 
Signature · 1 line
1.7.4.1
#6 lore raw

Re: [PATCH 2/2] staging: tidspbridge: fix incorrect free to drv_datap

Quoted 11 lines · Omar Ramirez Luna

This structure is still used after it has been freed, since it is being allocated in probe, calls to free it have been moved to module's remove routine.

This should fix the follwoing messages when attempting to remove the module:
drv_get_first_dev_extension: Failed to retrieve the object handle drv_get_first_dev_extension: Failed to retrieve the object handle
drv_destroy: Failed to store DRV object
mgr_destroy: Failed to store MGR object

So this is only triggered when you do an rmmod to remove the module?

Probably that's not stable material.

regards,
dan carpenter

#7 lore raw

Re: [PATCH 2/2] staging: tidspbridge: fix incorrect free to drv_datap

Quoted 13 lines · Dan Carpenter
Quoted 11 lines · Omar Ramirez Luna

This structure is still used after it has been freed, since it is being allocated in probe, calls to free it have been moved to module's remove routine.

This should fix the follwoing messages when attempting to remove the module:
 drv_get_first_dev_extension: Failed to retrieve the object handle drv_get_first_dev_extension: Failed to retrieve the object handle
 drv_destroy: Failed to store DRV object
 mgr_destroy: Failed to store MGR object

Quoted 1 line

So this is only triggered when you do an rmmod to remove the module?

Yes.

Quoted 1 line

Probably that's not stable material.

The critical issue is that for a small window the freed memory can be filled with something else and the driver still might dereference that memory which no longer belongs to it, thus causing a crash.

But I guess that falls into "this can be a problem", it is ok if it is being left out of stable.

Thanks,

Omar
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw

Re: [PATCH 2/2] staging: tidspbridge: fix incorrect free to drv_datap

Quoted 14 lines · Omar
Quoted 13 lines · Dan Carpenter
Quoted 11 lines · Omar Ramirez Luna

This structure is still used after it has been freed, since it is being allocated in probe, calls to free it have been moved to module's remove routine.

This should fix the follwoing messages when attempting to remove the module:
 drv_get_first_dev_extension: Failed to retrieve the object handle drv_get_first_dev_extension: Failed to retrieve the object handle
 drv_destroy: Failed to store DRV object
 mgr_destroy: Failed to store MGR object

Quoted 4 lines
Quoted 1 line

So this is only triggered when you do an rmmod to remove the module?

Yes.

How often do people rmmod things on a production system? Hopefully, never right?

regards,
dan carpenter

#9 lore raw

Re: [PATCH 2/2] staging: tidspbridge: fix incorrect free to drv_datap

Quoted 2 lines · Dan Carpenter

How often do people rmmod things on a production system?  Hopefully, never right?

That's right... At least in recent versions of the tidspbrdige, that have recovery support. Before, we needed a script to detect MMU faults, and reload the module =/ IIRC this is still the case on the Nokia N900.

Signature · 5 lines
Felipe Contreras
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#10 lore raw

Re: [PATCH 2/2] staging: tidspbridge: fix incorrect free to drv_datap

Quoted 8 lines · Felipe Contreras
Quoted 2 lines · Dan Carpenter

How often do people rmmod things on a production system?  Hopefully, never right?

That's right... At least in recent versions of the tidspbrdige, that have recovery support. Before, we needed a script to detect MMU faults, and reload the module =/ IIRC this is still the case on the Nokia N900.

If you have a script that's doing rmmods then that's different. I didn't know about that.

regards,
dan carpenter

#11 lore raw

Re: [PATCH 2/2] staging: tidspbridge: fix incorrect free to drv_datap

Quoted 5 lines · Dan Carpenter
Quoted 4 lines · Felipe Contreras
Quoted 2 lines · Dan Carpenter

How often do people rmmod things on a production system?  Hopefully, never right?

Quoted 7 lines
Quoted 4 lines

That's right... At least in recent versions of the tidspbrdige, that have recovery support. Before, we needed a script to detect MMU faults, and reload the module =/ IIRC this is still the case on the Nokia N900.

If you have a script that's doing rmmods then that's different.  I didn't know about that.

*Had*, there's no need any more.

Signature · 5 lines
Felipe Contreras
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html