Received: by 10.192.165.156 with SMTP id m28csp185637imm; Thu, 12 Apr 2018 19:38:59 -0700 (PDT) X-Google-Smtp-Source: AIpwx49nz+UpBNER6h4yHx85KzchswUFA5v3PsfyNk6Q0ehpLArEmANVg+fTwKY7KopGV10kWz2S X-Received: by 10.98.227.16 with SMTP id g16mr9592047pfh.171.1523587139557; Thu, 12 Apr 2018 19:38:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523587139; cv=none; d=google.com; s=arc-20160816; b=sHvXI+i5I6OmZel/RcCETKiw5wiocxfaS1kQJ5tFtua/J9Prlu+k3RSnnD/M0KwRck RaHD1GUcWXz0VtFjtELPvbx7iU58G9XA22LPTjd5gMVbtzCn8dedql4NdK9Z0ww2rdJh /lt2VxavHQ2JN2kUQ4f2SB3AwfdORW23q8Pv1bpHtYPfEZngQl9aC4lBFBQatSpHBgaq vp6k0LztSaXn/9R/AkLEbemCCYGGt3ji+o22Gzl8dbvy+nvQFN4p0h+o6bsSSdCKeXQk Lfpyou43VJdbHpE2lm5Rlr0Jmqxx54hxqiGC3+/itxHv5X3A2HipILMVwpMvybwx9Xlb 9fcw== 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=l/oUzTa9w9KpfToN5u/KlD8ME/ovBasMJRu2jhVjfVU=; b=TWQrmUH27MINFmbmYlteoskr7MqIQ2emShWy+NO5dwNse2vUKrwwGnPkvn6J/ikRmS rfmXY0fe5SKrRxzJVBdt5zOShl7crqhiFpJEcN/zDOaErv6o9+HluAf05qT3To70Qyse ORsF865mLnlkWnyZ8vsKZJglgiI8d/WukoEL8+BJ/vyMSHKMMEMiiVpGIZjexElmc+9E AUaTFh9huQHG0RyKm72MOJvqmV/US+vyt7508I4xM9zzbfLvOpw+I407Hg2qqeEc2Lq5 BvgTeK8jsDKvimFUs9+FQIowugTYj9Bvc6K/wBtcgX/ylnMyHPgMoRnhhIvBHFwQUVLl BnCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MtgMiYeO; 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 l6si1502856pgs.531.2018.04.12.19.38.44; Thu, 12 Apr 2018 19:38:59 -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=MtgMiYeO; 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 S1753296AbeDMChf (ORCPT + 99 others); Thu, 12 Apr 2018 22:37:35 -0400 Received: from mail-pg0-f67.google.com ([74.125.83.67]:45045 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752920AbeDMChd (ORCPT ); Thu, 12 Apr 2018 22:37:33 -0400 Received: by mail-pg0-f67.google.com with SMTP id v26so1254204pge.11; Thu, 12 Apr 2018 19:37:33 -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=l/oUzTa9w9KpfToN5u/KlD8ME/ovBasMJRu2jhVjfVU=; b=MtgMiYeOEEU7gg+k1fh4G7TLHRE0+rzD6gWG4ydGRZalVnFYjRZscZfmEJOuOF8mtF qoawx1rNTeP0BmUAYMPLBynx/I4BVaN8TOCSp6KkR8O4dyVU2uF3/12b82x2w03JkwIU YCKKEqOuJsK4qcZQJST592cUWCkKVW6TOBvGxPSCk18w9jB4XAOkAhvUSpK+OPfvxxDm QXdA9vcHBJUycrS0IrF91jHJNjM5pLEVOSDaKLx3i5HvQlde4r3i/jZUVfbhdi8kzN9j XnMklwyohA3JWrMJZoMALj65BIJQalT/o3jfKHrtycQvoUEP8GGuWmPKm5JAWVQliqGp a4xA== 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=l/oUzTa9w9KpfToN5u/KlD8ME/ovBasMJRu2jhVjfVU=; b=GV8TTD8ktUV6lG9UaZh3yZtsCR1HakS/N5lNLteqbJHcOzEDC6bjhz14+C7qXCsKzR 7L0pkDeTW5nroDKpqP7c/5UqmDxB7c0HI7a0ay7Fphv3wxn8dxaiohJeJB96B7DFzsQ5 W4QSwdNlsoVd2FLQTwmBli4UqsuHipGq2CdsV052dWQKGXX63fONuJVz6nYg9ksW6Iha iWSEgiUyolDNixSgrQuu1vXOChrUzHERH+nX1iGXN3NXbdiH+F6gxxe/u5O3Y9H4klao dVAe5s1AoRNf43qMsOnE92HSKCKwURKhj2UEeReFlUx+EId5m4AyiJUN2fkg84E4RJIU MJzQ== X-Gm-Message-State: ALQs6tCDYI/UG2Xk4IvL0gzIh8jzn6WjLPD4vSok1k0ucX5lFLxLEf6U fjD/fBxHX1Z/sqr0fgj5+Pesrg== X-Received: by 10.101.77.145 with SMTP id p17mr2593864pgq.275.1523587052893; Thu, 12 Apr 2018 19:37:32 -0700 (PDT) Received: from [192.168.0.102] ([106.51.29.61]) by smtp.gmail.com with ESMTPSA id f69sm174869pgc.27.2018.04.12.19.37.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Apr 2018 19:37:32 -0700 (PDT) Subject: Re: [PATCH] PM / devfreq: use put_device() instead of kfree() To: Chanwoo Choi , myungjoo.ham@samsung.com, kyungmin.park@samsung.com References: <5AD001C5.2040406@samsung.com> <5AD0041D.2050506@samsung.com> <5AD012CB.7030003@gmail.com> <5AD0160E.7050601@samsung.com> Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org From: arvindY Message-ID: <5AD017E8.7020901@gmail.com> Date: Fri, 13 Apr 2018 08:07:28 +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: <5AD0160E.7050601@samsung.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Friday 13 April 2018 07:59 AM, Chanwoo Choi wrote: > On 2018년 04월 13일 11:15, arvindY wrote: >> Hi Chanwoo, >> >> On Friday 13 April 2018 06:43 AM, Chanwoo Choi wrote: >>> On 2018년 04월 13일 10:03, Chanwoo Choi wrote: >>>> Hi, >>>> >>>> I'm sorry for the late reply. >>>> >>>> On 2018년 03월 30일 20:44, Arvind Yadav wrote: >>>>> Never directly free @dev after calling device_register() or >>>>> device_unregister(), even if device_register() returned an error. >>>>> Always use put_device() to give up the reference initialized. >>>>> >>>>> Signed-off-by: Arvind Yadav >>>>> --- >>>>> drivers/devfreq/devfreq.c | 4 +++- >>>>> 1 file changed, 3 insertions(+), 1 deletion(-) >>>>> >>>>> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c >>>>> index fe2af6a..a225b94 100644 >>>>> --- a/drivers/devfreq/devfreq.c >>>>> +++ b/drivers/devfreq/devfreq.c >>>>> @@ -625,7 +625,8 @@ struct devfreq *devfreq_add_device(struct device *dev, >>>>> err = device_register(&devfreq->dev); >>>>> if (err) { >>>>> mutex_unlock(&devfreq->lock); >>>>> - goto err_dev; >>>>> + put_device(&devfreq->dev); >>>>> + goto err_out; >>>> why do you change the goto postion? >>>> err_out is correct to free the memory of devfreq instance. >>> Sorry. err_dev is correct instead of err_out. >> 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. 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. >> >> We are releasing devfreq in devfreq_dev_release(). So no need >> to call kfree() again. It'll be redundant. 'err_out' is correct. > You're right. err_out is correct. > put_device() -> dev->release() -> devfreq_dev_release() -> kfree(devfreq) > >>>>> } >>>>> devfreq->trans_table = devm_kzalloc(&devfreq->dev, >>>>> @@ -671,6 +672,7 @@ struct devfreq *devfreq_add_device(struct device *dev, >>>>> mutex_unlock(&devfreq_list_lock); >>>>> device_unregister(&devfreq->dev); >>>>> + devfreq = NULL; >>>> It is wrong. If you initialize the devfreq as NULL, >>>> never free the 'devfreq' instance. >> No need to release memory after device_unregister(). >> driver core will take care of this. It will release memory >> So no need to call free again. > If you have to initialize the devfreq instance as NULL, > I think that you better to init in the devfreq_dev_release() > as following: > > diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c > index fe2af6aa88fc..8c52a13d3887 100644 > --- a/drivers/devfreq/devfreq.c > +++ b/drivers/devfreq/devfreq.c > @@ -543,6 +543,7 @@ static void devfreq_dev_release(struct device *dev) > > mutex_destroy(&devfreq->lock); > kfree(devfreq); > + devfreq = NULL; > } Yes, You are right. I will share a update patch. Thanks for reviewing. > >>>>> err_dev: >>>>> if (devfreq) >>>>> kfree(devfreq); >>>>> >> ~arvind >> >> >> >