From patchwork Thu Feb 28 11:01:52 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Malat X-Patchwork-Id: 223882 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id DBAF52C029C for ; Thu, 28 Feb 2013 21:58:17 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752175Ab3B1K6O (ORCPT ); Thu, 28 Feb 2013 05:58:14 -0500 Received: from mail-ee0-f45.google.com ([74.125.83.45]:42274 "EHLO mail-ee0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751163Ab3B1K6M (ORCPT ); Thu, 28 Feb 2013 05:58:12 -0500 Received: by mail-ee0-f45.google.com with SMTP id b57so1310578eek.4 for ; Thu, 28 Feb 2013 02:58:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:sender:date:from:to:cc:subject:message-id:mime-version :content-type:content-disposition:user-agent:x-gm-message-state; bh=x9fJTusTVxaF3GA/p7Aw/jqLtQNF/xJ2d8ab4OXaes8=; b=iR45CXk9qkvEgw+wQAijl6GZkuY6uiKRmJEti2KHkpY9IBMheXZT7JAu24f4LXOJzS atD6tvqTLc1NCPblCEpEIirAjqTu1hx29wRxwH3UgyTRBOKAt/Ija3gCfIpis8MsEJJm FbZNMi3rXHyZLuuzQ/Du9d7z0ffeBOOWj8RMXPtRVii3+Ws+yPAvzZJfHnjP0XXkyvbQ 1nUvzIeMoK9nEbZvZA1Sm1gSi0kcRNkZPVrXUVCPDFDkzMKVZNMyBOx3CViPJZvn35e3 AUnkYtTBlpx/DYOWDH5FE6FJwXngV3N5uzWCjOXao9nGMHVfDd6eQUkA3GN1U6J404lq kJ1g== X-Received: by 10.14.205.68 with SMTP id i44mr15779924eeo.25.1362049091016; Thu, 28 Feb 2013 02:58:11 -0800 (PST) Received: from bordel.klfree.net (bordel.klfree.cz. [81.201.48.42]) by mx.google.com with ESMTPS id m46sm11187642eeo.16.2013.02.28.02.58.09 (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 28 Feb 2013 02:58:10 -0800 (PST) Date: Thu, 28 Feb 2013 12:01:52 +0100 From: Petr Malat To: netdev@vger.kernel.org Cc: oss@malat.biz Subject: [PATCH v2] phy: Fix phy_device_free memory leak Message-ID: <20130228110152.GA26262@bordel.klfree.net> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-Gm-Message-State: ALoCoQkofJc4C3WapRmSr/ADhAhSG3l5bp5/tTSAEU/LMg1Ez2kZ+6JiQAuPBx+OHc2IaBIrOPiE Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Petr Malat Fix memory leak in phy_device_free() for the case when phy_device* returned by phy_device_create() is not registered in the system. Bug description: phy_device_create() sets name of kobject using dev_set_name(), which allocates memory using kvasprintf(), but this memory isn't freed if the underlying device isn't registered properly, because kobject_cleanup() is not called in that case. This can happen (and actually is happening on our machines) if phy_device_register(), called by mdiobus_scan(), fails. Patch description: Embedded struct device is initialized in phy_device_create() and it counterpart phy_device_free() just drops one reference to the device, which leads to proper deinitialization including releasing the kobject name memory. Signed-off-by: Petr Malat --- Updated according to according to David Miller proposal - put_device is used to release the memory now. Please put me on CC, I'm not signed into the mailing list. -- To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html --- linux-3.8/drivers/net/phy/phy_device.c.orig 2013-02-19 00:58:34.000000000 +0100 +++ linux-3.8/drivers/net/phy/phy_device.c 2013-02-28 11:20:51.841528627 +0100 @@ -44,13 +44,13 @@ MODULE_LICENSE("GPL"); void phy_device_free(struct phy_device *phydev) { - kfree(phydev); + put_device(&phydev->dev); } EXPORT_SYMBOL(phy_device_free); static void phy_device_release(struct device *dev) { - phy_device_free(to_phy_device(dev)); + kfree(to_phy_device(dev)); } static struct phy_driver genphy_driver; @@ -201,6 +201,8 @@ struct phy_device *phy_device_create(str there's no driver _already_ loaded. */ request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id)); + device_initialize(&dev->dev); + return dev; } EXPORT_SYMBOL(phy_device_create); @@ -363,9 +365,9 @@ int phy_device_register(struct phy_devic /* Run all of the fixups for this PHY */ phy_scan_fixups(phydev); - err = device_register(&phydev->dev); + err = device_add(&phydev->dev); if (err) { - pr_err("phy %d failed to register\n", phydev->addr); + pr_err("PHY %d failed to add\n", phydev->addr); goto out; }