usb: phy: tegra: Using devm API for memory allocation
Subsystems (2)
PATCH 1/1 usb: phy: tegra: Using devm API for memory allocation
Using devm_kzalloc for allocating memory needed for PHY pointer and hence removing kfree calls to PHY pointer.
--- drivers/usb/phy/tegra_usb_phy.c | 4 +--- 1 files changed, 1 insertions(+), 3 deletions(-)
drivers/usb/phy/tegra_usb_phy.c +1 -3
@@ -704,7 +704,6 @@ static void tegra_usb_phy_close(struct usb_phy *x)utmip_pad_close (phy);clk_disable_unprepare (phy->pll_u);clk_put (phy->pll_u); -kfree (phy); }static int tegra_usb_phy_power_on (struct tegra_usb_phy *phy)
@@ -740,7 +739,7 @@ struct tegra_usb_phy *tegra_usb_phy_open(struct device *dev, int instance,int i;int err; - phy =kmalloc (sizeof (struct tegra_usb_phy ),GFP_KERNEL ); + phy =devm_kzalloc (dev,sizeof (struct tegra_usb_phy ),GFP_KERNEL );if (!phy)return ERR_PTR (-ENOMEM );
@@ -791,7 +790,6 @@ err1:clk_disable_unprepare (phy->pll_u);clk_put (phy->pll_u); err0: -kfree (phy);return ERR_PTR (err); }EXPORT_SYMBOL_GPL (tegra_usb_phy_open);
Signature · 1 line
1.7.0.4
Quoted 2 lines · Venu Byravarasu
Using
devm_kzallocfor allocating memory needed for PHY pointer and hence removing kfree calls to PHY pointer.
Since the kfree() here used to be in tegra_usb_phy_close() rather than any remove() function, does it actually make sense to use devm_kzalloc(); would plain using kzalloc() instead, and not removing the kfree() calls, be better?
When the PHY code gets converted to be an actual probed driver, then perhaps using devm will make sense.
Quoted 8 lines
-----Original Message-----
From: Stephen Warren [mailto:[email protected]]
Sent: Tuesday, December 18, 2012 10:03 PM
To: Venu Byravarasu
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] usb: phy: tegra: Using devm API for memory allocation
Quoted 9 lines
Quoted 2 lines · Venu Byravarasu
Using
devm_kzallocfor allocating memory needed for PHY pointer and hence removing kfree calls to PHY pointer.Since the
kfree()here used to be integra_usb_phy_close()rather than anyremove()function, does it actually make sense to usedevm_kzalloc(); would plain usingkzalloc()instead, and not removing thekfree()calls, be better?
Stephen,
As you mentioned I can replace kmalloc with kzalloc in the original code and push an updated patch.
However, I just wanted to understand if there exists any issue in using devm_kzalloc instead of kzalloc?
Quoted 2 lines
When the PHY code gets converted to be an actual probed driver, then perhaps using devm will make sense.
Quoted 18 lines · Venu Byravarasu
Quoted 8 lines
-----Original Message-----
From: Stephen Warren [mailto:[email protected]]
Sent: Tuesday, December 18, 2012 10:03 PM
To: Venu Byravarasu
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] usb: phy: tegra: Using devm API for memory allocationQuoted 9 lines
Quoted 2 lines · Venu Byravarasu
Using
devm_kzallocfor allocating memory needed for PHY pointer and hence removing kfree calls to PHY pointer.Since the
kfree()here used to be integra_usb_phy_close()rather than anyremove()function, does it actually make sense to usedevm_kzalloc(); would plain usingkzalloc()instead, and not removing thekfree()calls, be better?
Quoted 5 lines
Stephen,
As you mentioned I can replace kmalloc with kzalloc in the original code and push an updated patch.
However, I just wanted to understand if there exists any issue in usingdevm_kzallocinstead of kzalloc?
devm_* are intended for objects allocated during probe(), and free()d during remove(). The object you're allocating here isn't that case.
Now, once you convert the Tegra PHY driver to be a true device, perhaps this object will be allocated/freed during probe/remove, so the devm_ functions will be useful then?
The problem this may cause is a memory leak. Consider the Tegra EHCI and PHY drivers being built as modules, the PHY driver module being inserted and never removed, yet the EHCI driver being continually inserted and removed. Since the PHY is never removed, the memory allocated by its devm_kzalloc() call is never freed, but it's continually re-allocated since tegra_usb_phy_open() is called whenever the EHCI driver module is inserted. You need the explicit kfree() to avoid that, and since you're kfree()ing somewhere other than remove(), using devm_* to make the allocation isn't appropriate.
Quoted 32 lines
-----Original Message-----
From: Stephen Warren [mailto:[email protected]]
Sent: Wednesday, December 19, 2012 11:08 PM
To: Venu Byravarasu
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] usb: phy: tegra: Using devm API for memory allocationQuoted 7 lines · Venu Byravarasu
Quoted 7 lines
-----Original Message-----
From: Stephen Warren [mailto:[email protected]]
Sent: Tuesday, December 18, 2012 10:03 PM
To: Venu Byravarasu
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH] usb: phy: tegra: Using devm API for memoryallocation
Quoted 7 lines
Quoted 1 line
Stephen,
As you mentioned I can replace kmalloc with kzalloc in the original code and push an updated patch.
However, I just wanted to understand if there exists any issue in usingdevm_kzallocinstead of kzalloc?devm_* are intended for objects allocated during
probe(), and free()d duringremove(). The object you're allocating here isn't that case.Now, once you convert the Tegra PHY driver to be a true device, perhaps this object will be allocated/freed during probe/remove, so the
devm_functions will be useful then?
Quoted 9 lines
The problem this may cause is a memory leak. Consider the Tegra EHCI and PHY drivers being built as modules, the PHY driver module being inserted and never removed, yet the EHCI driver being continually inserted and removed. Since the PHY is never removed, the memory allocated by its
devm_kzalloc()call is never freed, but it's continually re-allocated sincetegra_usb_phy_open()is called whenever the EHCI driver module is inserted. You need the explicitkfree()to avoid that, and since you're kfree()ing somewhere other thanremove(), using devm_* to make the allocation isn't appropriate.
Thanks Stephen for the detailed explanation.
Sent updated patch for review: http://marc.info/?l=linux-usb&m=135599303216132&w=2 .