Received: by 10.213.65.68 with SMTP id h4csp826867imn; Tue, 27 Mar 2018 09:29:47 -0700 (PDT) X-Google-Smtp-Source: AG47ELsslhq+I2m/uu5eL8RIhpyXF1T+40zfIx+bt9fIQQn5eyCx3uWrA1rX6+g33OS1m5RBGo6A X-Received: by 2002:a17:902:6085:: with SMTP id s5-v6mr42633991plj.307.1522168187106; Tue, 27 Mar 2018 09:29:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522168187; cv=none; d=google.com; s=arc-20160816; b=rtKyyM+kQPMaHmByJf7Ap6sVs30K1292Dl2Q56GvNeD/uy4XKyLfGgLQB4E3eryVe/ uoMvltD18xz4t83SKG0HKv2RRp/hoDypV6x0RE2sxdPVCs7PlM/LL6tV0sCbpDUNVeQb dxwt/aYjB3z8HWDOI/varIRzt/NGN3zikXnHE4AGK9jZ/6Y5pgc02LyjVVdOpoGKXKgs juO22axQvm1Op3DA+T/25kbyHBgRKIor/NaKXXEwWH/2HYnfmqEfIxMLATiw5vMdC3Th o6waPawjFwfyn4tu/Qm+Y2N/QiSu50uULyyKeUnCFzSdjgF/LZtUKT5wXxs7SyX7VDrO RXNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject:dkim-signature:arc-authentication-results; bh=I5sTbdY4VPzi7uWkMPEIzyLmT1kNYdXNN3ea49Tn/+Q=; b=WV5T+RK20vn+6KCKl5qriCFDbimKTR+KbYel8RXFpSTuJjqGcJFOgVO1myOrro3xBE i9xba2Rgzx3UHzPSCDwu/NuSn/5xDsJreFe+j2CTGPsd4NDw38LH7ffzQlvZgYNEDCS1 yOeqjiZjxooqAMhgcRrHgCpQ+8WuYudXVG/vFl1TrNVXcf0xpNZXoY3IWDouXm42j7wK BGNlriGT41TB6n54LzzkvxtmiNLNBp/RlBoGIBapGPTUPCnfXJ48BM10qM/T8kDDjM6o lnOzEzxy265WmhkubwPhNHiX/fihJVariGmIqeM1xup8Lhg4xR/5NZ85PiHgLSNP6wuC dwqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=paA6JTTA; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s78si1215025pfj.259.2018.03.27.09.29.32; Tue, 27 Mar 2018 09:29:47 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=paA6JTTA; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752044AbeC0Q2G (ORCPT + 99 others); Tue, 27 Mar 2018 12:28:06 -0400 Received: from mail-pg0-f65.google.com ([74.125.83.65]:35149 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750922AbeC0Q2F (ORCPT ); Tue, 27 Mar 2018 12:28:05 -0400 Received: by mail-pg0-f65.google.com with SMTP id j3so3010308pgf.2 for ; Tue, 27 Mar 2018 09:28:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=I5sTbdY4VPzi7uWkMPEIzyLmT1kNYdXNN3ea49Tn/+Q=; b=paA6JTTAcRRIadpaqTwRDvK9VNluagQBDReUDBCglLR6PWaBQaSR8TZ3mQNSCTvcm6 MBkxsNLZ93IIRatnW12zQZl3zBPw9g9qCEKOII16/JS9dnXCoUwvv2nfI+vXlFp+zgyY 40hd7VtcO+D1YE2lQb1cgs8MXsnjzTlfJRLCDAudVlpR4cd+NZ2lwzLnls+me+44eGxy xYHznwIkypLeUVP0APZ2IDex5sqKv+VBdHKNj+FeZNza7kZuISHyZSFDXZQH5953VlvM lPiaytHG70A2P1STiFb8sgyJWFafyxf8H9Bs664jykbqhXV+gWc/vNxtYl7FGgGQ9x4K 1qjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=I5sTbdY4VPzi7uWkMPEIzyLmT1kNYdXNN3ea49Tn/+Q=; b=Qc9iJ45eKRYpMQ4STp87eCEgo20pWaqp2MrW5w66wBh3eKJM7LNGWL8DznoJ5KtP49 3G41bqhlpBLC79jDdZeNgRlZ0DpfguXBaTEFmoygHN3ig2QD5IiTq2YKVLMDElQHzsHu 8DOd7SKYuPixup3Cp9gPO0NYHkp6OwHqteyzFvBEksklrFJlGDzeXz+NluNsFtHNU0oB Gaz1xnY9nZ1a9HReSR7REXHj++slEiNxojToqTi4iA9Z3Rq8nNoMhL9uetolm1Z/6Wpq AFuAaMv+vOWNAJ44hqjUMP1QlEV4xVaMsJDPpz3WKldaW6It6Ncy3JxL4kZHMJwx4jSZ EABg== X-Gm-Message-State: AElRT7H2YPJjErryyNyK0cSRdgZsWgl+woZib8RwW6bx56ToT9R6mB/r ZkpyT8kqAOXpxRwIrnauSf0beA== X-Received: by 10.98.149.78 with SMTP id p75mr24012pfd.188.1522168084499; Tue, 27 Mar 2018 09:28:04 -0700 (PDT) Received: from [192.168.0.104] ([106.51.29.61]) by smtp.gmail.com with ESMTPSA id j83sm3894126pfj.18.2018.03.27.09.28.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Mar 2018 09:28:04 -0700 (PDT) Subject: Re: [PATCH] coresight: use put_device() instead of kfree() To: Mathieu Poirier References: <5AB9ACDA.9090207@gmail.com> Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org From: arvindY Message-ID: <5ABA7111.3020908@gmail.com> Date: Tue, 27 Mar 2018 21:58:01 +0530 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tuesday 27 March 2018 09:37 PM, Mathieu Poirier wrote: > On 26 March 2018 at 20:30, arvindY wrote: >> >> On Tuesday 27 March 2018 03:16 AM, Mathieu Poirier wrote: >>> drivers/hwtracing/coresight/coresight.c >>> On 18 March 2018 at 01:38, Arvind Yadav wrote: >>>> Never directly free @dev after calling device_register(), even >>>> if it returned an error. Always use put_device() to give up the >>>> reference initialized. >>>> >>>> Signed-off-by: Arvind Yadav >>>> --- >>>> drivers/hwtracing/coresight/coresight.c | 8 ++++---- >>>> 1 file changed, 4 insertions(+), 4 deletions(-) >>>> >>>> diff --git a/drivers/hwtracing/coresight/coresight.c >>>> b/drivers/hwtracing/coresight/coresight.c >>>> index 389c4ba..132dfbc 100644 >>>> --- a/drivers/hwtracing/coresight/coresight.c >>>> +++ b/drivers/hwtracing/coresight/coresight.c >>>> @@ -1026,8 +1026,10 @@ struct coresight_device *coresight_register(struct >>>> coresight_desc *desc) >>>> dev_set_name(&csdev->dev, "%s", desc->pdata->name); >>>> >>>> ret = device_register(&csdev->dev); >>>> - if (ret) >>>> - goto err_device_register; >>>> + if (ret) { >>>> + put_device(&csdev->dev); >>>> + goto err_kzalloc_csdev; >>>> + } >>>> >>>> mutex_lock(&coresight_mutex); >>>> >>>> @@ -1038,8 +1040,6 @@ struct coresight_device *coresight_register(struct >>>> coresight_desc *desc) >>>> >>>> return csdev; >>>> >>>> -err_device_register: >>>> - kfree(conns); >>> Apologies for the late reply, I was travelling. >>> >>> I suggest to simply replace kfree() with put_device() in order to >>> concentrate the error handling code in the same area and make sure >>> that memory allocated for @conns and @refcnts is freed. >>> >>> Thanks, >>> Mathieu >> >> If you will see the comment for device_register(drivers/base/core.c) >> there is mentioned that 'NOTE: _Never_ directly free @dev >> after calling this function, even if it returned an error! >> Always use put_device() to give up the reference initialized >> in this function instead. > I have read the notice and in full agreement with the put_device() part. > >> Here put_device() will decrement the last reference and then >> free the memory by calling dev->release. Internally >> put_device() -> kobject_put() -> kobject_cleanup() which is >> responsible to call 'dev -> release' and also free other kobject >> resources. > Memory would be automatically freed if it would have been allocated > with the devm_XYZ() helpers but it is not the case here. As such it > has to be freed explicitly. Reading your patch again (and the jetlag > somewhat fading) I think you've done the right thing except for the > "goto" that should still point to "err_device_register". > Take rest :) 'goto' should not point to "err_device_register" because put_device() will call 'dev->release' which is coresight_device_release(). It's release @conns, @refcnt and @csdev . If we will keep same 'goto then kfree() will be redundant for all. >> we should always avoid kfree() if device_register() >> returned an error. Otherwise it'll not do clean up of other >> kobject resources. >> >> ~arvind >>>> err_kzalloc_conns: >>>> kfree(refcnts); >>>> err_kzalloc_refcnts: >>>> -- >>>> 2.7.4 >>>>