Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp4655637ybi; Mon, 27 May 2019 23:51:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqx/0akEQpBrZJV1BXw2IZ2pTKJVQTz8Lk5P9GTwST0e5ILkJ/XEGQGhG+Ex0tlkPvQX+0Ab X-Received: by 2002:a17:902:e18d:: with SMTP id cd13mr65689759plb.301.1559026272066; Mon, 27 May 2019 23:51:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559026272; cv=none; d=google.com; s=arc-20160816; b=sH7exbY5k9nrP9FNz9iA91Qaqo2mCvYA7Ng0Vl7tf5d3fXrOuk1ads94vV39YXr+i/ udYQYBW52TQ2Er0pb1c2IQGzCNswdc84R42Fw3JYb6c4Kr2na7mHBpc8XQyTRvjO8/mW wcJFC49HKxqL+/U5+V5r7lnRLuXqvKuwHMlIvHJhf6QtGm6Ht8T8uX+nKGUDa5HCCzxt XmqjzVF6TnRgx9RS5fBK3IoWRWrLAWCXmn5eaWesAtKFR0HAuz4dl+JKhpNxspWkBw0i 2Cq/obEjedVCyLjkHshrFRMactyYiWdGjtSpG26h4qDTgpJ0CR16sUfr4h/dCw3ZCDG2 phSg== 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=1Y17x30uDVSZq7IVYSNXhxLNFmu46Yx+8A8N5LPDFeU=; b=XiKeacoYfyhAcbL+BKEjFH3USMjDc695ctO3gqPLkgrGPqAeP9QOrROlzwQ0tFqayy bd3JrU2Os1JZousggUTCj1ydPozNpFsK626yr+CqVcuX8wg4be2mScAcJ9yaY+AfzrJY 2ji8/K3e1UpzCQTyZqs4RosiWFhsHZicy8G591wMus4p8U6r/xp60ORYIe4++l8Wb9+r 3SMhBKQrWIbZ39Mgef10G2IwvL3ODt5RwQN942KKCrHAPkpEIjm67X5Mswev5iSYqPTl qR2kCxXKP/bBzwN+WNDcG6d5dL1rT2gPSOlwENS4W7usVCxFmSdsZIO9JgoyGyXlSEiv d4NQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=WMBmOVGH; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x32si22684854pld.190.2019.05.27.23.50.56; Mon, 27 May 2019 23:51:12 -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=@kernel.org header.s=default header.b=WMBmOVGH; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727395AbfE1Gtw (ORCPT + 99 others); Tue, 28 May 2019 02:49:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:55878 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726305AbfE1Gtw (ORCPT ); Tue, 28 May 2019 02:49:52 -0400 Received: from localhost (unknown [77.241.229.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 992552070D; Tue, 28 May 2019 06:49:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1559026192; bh=w8wEyswIoAanFRoTC6ZEDsAAUBLLUnBhh9Z4d5GQpX8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=WMBmOVGH7ifB0y0fPJxFk1t08VJn9LvzJkLyKYFrE2+8a4a5OxzcmHHQhd2/KzL3N KfzEIPE8kM+00dYlJlZa8B49O0r8KN0Jjl3l0SZYYberERwIzi9tUNBaadkcL2Ue9W PGhO3TQP1MxJ0SVPxmiXR3NlnzXP51+8/SE/WAUs= Date: Tue, 28 May 2019 08:49:49 +0200 From: Greg KH To: Gen Zhang 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: <20190528064949.GC2428@kroah.com> References: <20190528003257.GA12065@zhanggen-UX430UQ> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190528003257.GA12065@zhanggen-UX430UQ> User-Agent: Mutt/1.12.0 (2019-05-25) 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: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