From patchwork Thu Jun 16 17:51:46 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Rowand X-Patchwork-Id: 636615 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rVrc82Vzqz9t1f for ; Fri, 17 Jun 2016 03:54:16 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=BmRhSGfR; dkim-atps=neutral Received: from ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3rVrc808S9zDqpy for ; Fri, 17 Jun 2016 03:54:16 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=BmRhSGfR; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Received: from mail-pf0-x242.google.com (mail-pf0-x242.google.com [IPv6:2607:f8b0:400e:c00::242]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3rVrYP6kyczDqlc for ; Fri, 17 Jun 2016 03:51:53 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=BmRhSGfR; dkim-atps=neutral Received: by mail-pf0-x242.google.com with SMTP id t190so4447820pfb.2 for ; Thu, 16 Jun 2016 10:51:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=to:cc:from:subject:message-id:date:user-agent:mime-version :content-transfer-encoding; bh=wJow1FLHhG36r/39jG0U+FMh4/C5XzyyXvF1Tsa7/VU=; b=BmRhSGfR15Ah0f8ocnYrzPMNM+8fSErNCow2EoWEC8DSE4dnuB8LAR+Jq+PWTfA0Lo hD3vHPTsOkXhWpklJti7Or/PxSeoBSnltejMD/qL66IZbTqb+kcqYztKkgnVggQ0cpWU pE1krBKIJxikR8QEts13jzcySXmdWQdl1DFNzcoQ6aFn3vXjIbXhQ8FeGTc27fsVFDgA UAdYDFkzHpX+1RvNkjGjTCEkoSgEgcgHGKxefsB1ss3qNxOS6tbNxAkcOAnPVaLKmeCZ ypBbINXMqahyhF27r9laDi+G8gOVJ4DWz5NJRrBg9Kf4/64mzBGuGHn9zQC51ndzqbA5 Z8fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:to:cc:from:subject:message-id:date:user-agent :mime-version:content-transfer-encoding; bh=wJow1FLHhG36r/39jG0U+FMh4/C5XzyyXvF1Tsa7/VU=; b=gwepI+LPl/+Ur32PTFo+PBRKLgQ7R27j7dUKxgiAg46wxYCF+zFT0Rf0cwc/tEt82P rh/wgoLhTIjkk406sOKUfU9LC02gfF7KZc8jHtaIurYnxpz8fEVdvBshvAsjh07xd462 a8/MwBHCtIZKFame46G8VWwI2n9hnW0tP0b10CkIgaBX75JmhEEKB6ofB+5gGtUjD+nR P1dJlt/1OLIB5LIPwE69iNMdTUytCYoMKWsQbUmjGZpwsMo4iar1BU+QslR8aVHqwWGW kVIF0BuxYHYkEE1QIQ18NMCyECIPvXiMVNpi6KZ02RLWUFEaBDPSn7BIpEzSrmRJjvmk TrFw== X-Gm-Message-State: ALyK8tLDs9Q0zc7CSpzjC33AYMaLfaDnQ4MsSXtCUeSbDEgblaCPLDDashFxRASN8JXaoQ== X-Received: by 10.98.46.70 with SMTP id u67mr6433429pfu.134.1466099511994; Thu, 16 Jun 2016 10:51:51 -0700 (PDT) Received: from [192.168.1.46] (c-50-148-165-216.hsd1.ca.comcast.net. [50.148.165.216]) by smtp.gmail.com with ESMTPSA id e71sm43234591pfd.44.2016.06.16.10.51.50 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 16 Jun 2016 10:51:51 -0700 (PDT) To: Mathieu Malaterre , Rob Herring From: Frank Rowand Subject: [PATCH v3] of: fix memory leak related to safe_name() Message-ID: <5762E732.1030908@gmail.com> Date: Thu, 16 Jun 2016 10:51:46 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "devicetree@vger.kernel.org" , linuxppc-dev , "linux-kernel@vger.kernel.org" Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" From: Frank Rowand Fix a memory leak resulting from memory allocation in safe_name(). This patch fixes all call sites of safe_name(). Mathieu Malaterre reported the memory leak on boot: On my PowerMac device-tree would generate a duplicate name: [ 0.023043] device-tree: Duplicate name in PowerPC,G4@0, renamed to "l2-cache#1" in this case a newly allocated name is generated by `safe_name`. However in this case it is never deallocated. The bug was found using kmemleak reported as: unreferenced object 0xdf532e60 (size 32): comm "swapper", pid 1, jiffies 4294892300 (age 1993.532s) hex dump (first 32 bytes): 6c 32 2d 63 61 63 68 65 23 31 00 dd e4 dd 1e c2 l2-cache#1...... ec d4 ba ce 04 ec cc de 8e 85 e9 ca c4 ec cc 9e ................ backtrace: [] kvasprintf+0x64/0xc8 [] kasprintf+0x4c/0x5c [] safe_name.isra.1+0x80/0xc4 [] __of_attach_node_sysfs+0x6c/0x11c [] of_core_init+0x8c/0xf8 [] kernel_init_freeable+0xd4/0x208 [] kernel_init+0x24/0x11c [] ret_from_kernel_thread+0x5c/0x64 Link: https://bugzilla.kernel.org/show_bug.cgi?id=120331 Signed-off-by: Frank Rowand Reported-by: mathieu.malaterre@gmail.com Tested-by: Mathieu Malaterre --- changes from v1 Move the prototype of __of_sysfs_remove_bin_file() into of_private.h changes from v2 Add the kfree that was in Mathieu's original patch drivers/of/base.c | 30 +++++++++++++++++++++--------- drivers/of/dynamic.c | 2 +- drivers/of/of_private.h | 3 +++ 3 files changed, 25 insertions(+), 10 deletions(-) Index: b/drivers/of/base.c =================================================================== --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -112,6 +112,7 @@ static ssize_t of_node_property_read(str return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length); } +/* always return newly allocated name, caller must free after use */ static const char *safe_name(struct kobject *kobj, const char *orig_name) { const char *name = orig_name; @@ -126,9 +127,12 @@ static const char *safe_name(struct kobj name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i); } - if (name != orig_name) + if (name == orig_name) { + name = kstrdup(orig_name, GFP_KERNEL); + } else { pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n", kobject_name(kobj), name); + } return name; } @@ -159,6 +163,7 @@ int __of_add_property_sysfs(struct devic int __of_attach_node_sysfs(struct device_node *np) { const char *name; + struct kobject *parent; struct property *pp; int rc; @@ -171,15 +176,16 @@ int __of_attach_node_sysfs(struct device np->kobj.kset = of_kset; if (!np->parent) { /* Nodes without parents are new top level trees */ - rc = kobject_add(&np->kobj, NULL, "%s", - safe_name(&of_kset->kobj, "base")); + name = safe_name(&of_kset->kobj, "base"); + parent = NULL; } else { name = safe_name(&np->parent->kobj, kbasename(np->full_name)); - if (!name || !name[0]) - return -EINVAL; - - rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name); + parent = &np->parent->kobj; } + if (!name) + return -ENOMEM; + rc = kobject_add(&np->kobj, parent, "%s", name); + kfree(name); if (rc) return rc; @@ -1815,6 +1821,12 @@ int __of_remove_property(struct device_n return 0; } +void __of_sysfs_remove_bin_file(struct device_node *np, struct property *prop) +{ + sysfs_remove_bin_file(&np->kobj, &prop->attr); + kfree(prop->attr.attr.name); +} + void __of_remove_property_sysfs(struct device_node *np, struct property *prop) { if (!IS_ENABLED(CONFIG_SYSFS)) @@ -1822,7 +1834,7 @@ void __of_remove_property_sysfs(struct d /* at early boot, bail here and defer setup to of_init() */ if (of_kset && of_node_is_attached(np)) - sysfs_remove_bin_file(&np->kobj, &prop->attr); + __of_sysfs_remove_bin_file(np, prop); } /** @@ -1895,7 +1907,7 @@ void __of_update_property_sysfs(struct d return; if (oldprop) - sysfs_remove_bin_file(&np->kobj, &oldprop->attr); + __of_sysfs_remove_bin_file(np, oldprop); __of_add_property_sysfs(np, newprop); } Index: b/drivers/of/dynamic.c =================================================================== --- a/drivers/of/dynamic.c +++ b/drivers/of/dynamic.c @@ -55,7 +55,7 @@ void __of_detach_node_sysfs(struct devic /* only remove properties if on sysfs */ if (of_node_is_attached(np)) { for_each_property_of_node(np, pp) - sysfs_remove_bin_file(&np->kobj, &pp->attr); + __of_sysfs_remove_bin_file(np, pp); kobject_del(&np->kobj); } Index: b/drivers/of/of_private.h =================================================================== --- a/drivers/of/of_private.h +++ b/drivers/of/of_private.h @@ -83,6 +83,9 @@ extern int __of_attach_node_sysfs(struct extern void __of_detach_node(struct device_node *np); extern void __of_detach_node_sysfs(struct device_node *np); +extern void __of_sysfs_remove_bin_file(struct device_node *np, + struct property *prop); + /* iterators for transactions, used for overlays */ /* forward iterator */ #define for_each_transaction_entry(_oft, _te) \