Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp4675853ybi; Tue, 28 May 2019 00:16:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqywXGoZoeZY2v6CRPIVbdGAaR7EKEPfk8o0pg02DPH3p7qMQS0RZARChfNP1KBH2R1ml5i5 X-Received: by 2002:a17:902:aa85:: with SMTP id d5mr132392291plr.245.1559027808740; Tue, 28 May 2019 00:16:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559027808; cv=none; d=google.com; s=arc-20160816; b=BRoXeUcr0NMdxgs/ZtDEzTPwHHXSwvDUuWQRaHD0rP8LBmogeBW4UXX4r6XVsUtPRR IaUPlKqdI+4oAwV2eRYe0cjr4LAxEsSXHHa4I4JhbUvm4jcY65VixYdxvT/vYBJhz5x0 d9KOCHRQ1Dcl2NzEYOyD8/qAFhzTi056IuZsU5b2Qq98f79ekSySTl+c/GMCW5q87QNJ otoOI1eqAopoxwf/7y7wKNHXQYlobGtqP5QXtr5MwrZAscGJnu8/7WWIyGG+wwHQyL3T IBtsTXVmj2jqd3fWQ+g055zCtOKCV+GVEPoDlsYX/dDqRVjwGIJR7z32156RamRh0J/g WFNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=r1FatOp08eZVWsQPZh9iebz5bH/kKuOBcpebc8XwEuo=; b=RJ6ybbPnwi+Qv9oTIrhF/Pvt2tmIO7Fu+xWn4hnzuFic4QTO2pDTNt8cDWUQz/qrBV 0Ep6/DrnzK0sIqnS09MHZOxZg/16uqKm9TUiLyZsn8oJW9Y/V9jgXv2gvw5gqIiLa20n T5EwXSFqe9RDtNp9cj7kVjeZBx5pje4i1mFjkEGdHTy+HXferanFt+jxGzVSoV/XNlH6 /bF/O7ljTgiQHsaHmJeYIU8I6UhyH3j8aY1TtzEif/wePh7D0p+BYPdixcpmOhBWQcu6 7+mcFD6ngilsm6nBHrKcy3V8f4DhBsC8BUIWJJN+EuBwZ/RRjVHAe0p2XIrFOB6x83P+ nEkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=dg7Oc40U; 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 s3si2146819pji.94.2019.05.28.00.16.32; Tue, 28 May 2019 00:16:48 -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=dg7Oc40U; 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 S1727422AbfE1HOP (ORCPT + 99 others); Tue, 28 May 2019 03:14:15 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:42386 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726879AbfE1HOP (ORCPT ); Tue, 28 May 2019 03:14:15 -0400 Received: by mail-pg1-f193.google.com with SMTP id 33so7440798pgv.9 for ; Tue, 28 May 2019 00:14:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=r1FatOp08eZVWsQPZh9iebz5bH/kKuOBcpebc8XwEuo=; b=dg7Oc40UXgXulZy8g9KFZp0pGxBh4V1kuchdu6VxszmlQ68x8aAEzWNCmdt1QSmfsL +GxwEM3fiooKXscLQcaf9602gbPHZcXiovzk3tRvq4LhJ4gV3XJUsSBvVtX/x3nP00xP 6yA8NOpw+t71USU/Kjms8pOnER7LTb1VBSYxQA9LhDDHdYxt+Xo9t4maEfEzLYHRDtIQ gtb7OneeGFZbhlRRR8PCatAjYfE/WH1YxwJaVgawU1QIykVqftVMzuV6Jxi1kmu/3Ax2 3nx6yZOOx18eH86fjtdyD2+kEiGJWDTQZ9lD8A7kuWKbU7BXRh9hChjnV4uajRe+jtD6 vtIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=r1FatOp08eZVWsQPZh9iebz5bH/kKuOBcpebc8XwEuo=; b=F9eOokLskaC9HRdAuV6+lkABn6bMwSWfbwj7YGXzWtT9STd8NW3mayP7nc8E/3xS16 6iHuyGiA+4Mv7oBZ134h1Y2ur3ClwAqcXaUjUD4vS/4bJWlr1RPR+p3lVWEcbsUBYR8W tx2DDPHjuDGaXFSO7k1kItStwwfE62Iid73SHPViJ7f9Uc46d1iPSr4ogy0+Hwh8XobS y7P0IpzAOfp+wEWb68sboY0/Z6FB9OkthL0HEmSNBoEEtiBZxyYLewBVzaLT2mCnPUkG zTtQFp6Ue2tngMFald9VIc452xN7emL5quzZM205NqLPqn9wkz5SQx6fCwE/oZ/Zt+0w Qiug== X-Gm-Message-State: APjAAAWy3TMTXdn3fIqgB2uYCHK88npJP9OczgdZZfyz0Hn78/VhdJPv T9mWnoiKv0W9g3TmSBfFnl/mQpTK X-Received: by 2002:a63:db4e:: with SMTP id x14mr116985224pgi.119.1559027654994; Tue, 28 May 2019 00:14:14 -0700 (PDT) Received: from zhanggen-UX430UQ ([66.42.35.75]) by smtp.gmail.com with ESMTPSA id t10sm16321871pfe.2.2019.05.28.00.14.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 May 2019 00:14:14 -0700 (PDT) Date: Tue, 28 May 2019 15:14:00 +0800 From: Gen Zhang To: Greg KH Cc: rafael@kernel.org, akpm@linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [Question: devm_kfree] When should devm_kfree() be used? Message-ID: <20190528071400.GB18498@zhanggen-UX430UQ> References: <20190528003257.GA12065@zhanggen-UX430UQ> <20190528064949.GC2428@kroah.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190528064949.GC2428@kroah.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 28, 2019 at 08:49:49AM +0200, Greg KH wrote: > On Tue, May 28, 2019 at 08:32:57AM +0800, Gen Zhang wrote: > > devm_kmalloc() is used to allocate memory for a driver dev. Comments > > above the definition and doc > > (https://www.kernel.org/doc/Documentation/driver-model/devres.txt) all > > imply that allocated the memory is automatically freed on driver attach, > > no matter allocation fail or not. However, I examined the code, and > > there are many sites that devm_kfree() is used to free devm_kmalloc(). > > e.g. hisi_sas_debugfs_init() in drivers/scsi/hisi_sas/hisi_sas_main.c. > > So I am totally confused about this issue. Can anybody give me some > > guidance? When should we use devm_kfree()? > > If you "know" you need to free the memory now, call devm_kfree(). If > you want to wait for it to be cleaned up latter, like normal, then do > not call it. > > Do you have a driver that you think uses it incorrectly? > > thanks, > > greg k-h Yes, that is my question! I do have several patches about devm_kfree() and debate with maintainers. e.g. you can kindly refer to this lkml: https://lkml.org/lkml/2019/5/23/1547. In tegra_wm9712_driver_probe(), 'machine->codec' is allocated by platform_device_alloc(). When it is NULL, function returns ENOMEM. However, 'machine' is allocated by devm_kzalloc() before this site. Thus we should free 'machine' before function ends to prevent memory leaking. And as mentioned above, e.g. hisi_sas_debugfs_init() in drivers/scsi/hisi_sas/hisi_sas_main.c, devm_kfree() is used to free devm_kmalloc(). So, thanks for looking into this, Greg. Thanks Gen