From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933286AbcHNLAu (ORCPT ); Sun, 14 Aug 2016 07:00:50 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:51921 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933262AbcHNLAr (ORCPT ); Sun, 14 Aug 2016 07:00:47 -0400 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Florian Fainelli" , "David S. Miller" Date: Sat, 13 Aug 2016 18:42:51 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 250/305] net: phy: Manage fixed PHY address space using IDA In-Reply-To: X-SA-Exim-Connect-IP: 92.40.249.202 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.37-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Florian Fainelli commit 69fc58a57e56bf5e39b48809aefffdaa1b04c945 upstream. If we have a system which uses fixed PHY devices and calls fixed_phy_register() then fixed_phy_unregister() we can exhaust the number of fixed PHYs available after a while, since we keep incrementing the variable phy_fixed_addr, but we never decrement it. This patch fixes that by converting the fixed PHY allocation to using IDA, which takes care of the allocation/dealloaction of the PHY addresses for us. Fixes: a75951217472 ("net: phy: extend fixed driver with fixed_phy_register()") Signed-off-by: Florian Fainelli Signed-off-by: David S. Miller [bwh: Backported to 3.16: - Adjust filename, context - fixed_phy_register() returns an integer, not a pointer/error] Signed-off-by: Ben Hutchings --- drivers/net/phy/fixed.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) --- a/drivers/net/phy/fixed.c +++ b/drivers/net/phy/fixed.c @@ -22,6 +22,7 @@ #include #include #include +#include #define MII_REGS_NUM 29 @@ -204,6 +205,8 @@ err_regs: } EXPORT_SYMBOL_GPL(fixed_phy_add); +static DEFINE_IDA(phy_fixed_ida); + void fixed_phy_del(int phy_addr) { struct fixed_mdio_bus *fmb = &platform_fmb; @@ -213,15 +216,13 @@ void fixed_phy_del(int phy_addr) if (fp->addr == phy_addr) { list_del(&fp->node); kfree(fp); + ida_simple_remove(&phy_fixed_ida, phy_addr); return; } } } EXPORT_SYMBOL_GPL(fixed_phy_del); -static int phy_fixed_addr; -static DEFINE_SPINLOCK(phy_fixed_addr_lock); - int fixed_phy_register(unsigned int irq, struct fixed_phy_status *status, struct device_node *np) @@ -232,17 +233,15 @@ int fixed_phy_register(unsigned int irq, int ret; /* Get the next available PHY address, up to PHY_MAX_ADDR */ - spin_lock(&phy_fixed_addr_lock); - if (phy_fixed_addr == PHY_MAX_ADDR) { - spin_unlock(&phy_fixed_addr_lock); - return -ENOSPC; - } - phy_addr = phy_fixed_addr++; - spin_unlock(&phy_fixed_addr_lock); + phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL); + if (phy_addr < 0) + return phy_addr; ret = fixed_phy_add(irq, phy_addr, status); - if (ret < 0) + if (ret < 0) { + ida_simple_remove(&phy_fixed_ida, phy_addr); return ret; + } phy = get_phy_device(fmb->mii_bus, phy_addr, false); if (!phy || IS_ERR(phy)) { @@ -317,6 +316,7 @@ static void __exit fixed_mdio_bus_exit(v list_del(&fp->node); kfree(fp); } + ida_destroy(&phy_fixed_ida); } module_exit(fixed_mdio_bus_exit);