Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp5680544ybv; Tue, 18 Feb 2020 01:56:37 -0800 (PST) X-Google-Smtp-Source: APXvYqw9Cx4wX06ibG3A5ljCqwdzXG2JFuXmWL/wLMqWj47ioYbL600B+o49lS6cSpX5R9FRrggR X-Received: by 2002:aca:ab51:: with SMTP id u78mr696249oie.21.1582019796915; Tue, 18 Feb 2020 01:56:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582019796; cv=none; d=google.com; s=arc-20160816; b=fPTCsxTPc6TXAi9IRQppJ2IgNtrTCEnB8KR5orBEG81XtkiclOC++c+gADBhEDMFcJ kZXjrV4CiVu923kWCdhOHyADk5HvuhLX7UlS8hbreQLdzcPE9AxH9JL1Mq1EdZ5hlaQT SqvfBS8TGk6lbRGfK4k3I1NXrjA/4mq1hrHHZPcmaSWV0LJdTy9Rsj7bY/hGrme7Eykq L/WZuIDwzHmKpE3DJZCgW+KecMsV+mJWrQZgT9eVdd2IgHV/kVfkGBoRIMOEMHuXRKQ2 jOC9WFv/8QOOSOvkUoGVOUkQg3V6eV9CHWkTCtC2nyKIjND66VH2zkgCbcqj0f6XuBRk xmww== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=UdH2WwRjeP0392FSfzUeJpWFNKo0z7G6MGSSmZM+wnw=; b=cvHQAUyNRBYC4budwnkZBDCwv/pv3fK3Bk8a3bBG5yodfWYk1+/ejxM8SXDzhRCLhz iQajaoK6hLiECBeOmqLtJX9gj33puP07rR5x+cJaUy9+t6p4DdgQgSWC5oi3WnNAk0nW ZjM+IObO7rAb1NNl1WFiWoqEkH5O70Y98u0T51sunH05KFz4qQUABRlL7I1KwuTjaxd3 dyjmd2jBkQte2nZuxIb5amSHNWmCf0xiUIvFUf8FxbYhT2CfYeun5vJc0AX+65vYTOtA oCTzwwClHF7UfzCTnTLIOCWq5D4gq4YYnsqXZZurEiziYgP7HzmCshZ3fVSQNKlyIwth WNdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JkGobR7n; 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=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u21si1572607otq.137.2020.02.18.01.56.24; Tue, 18 Feb 2020 01:56:36 -0800 (PST) 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=@linaro.org header.s=google header.b=JkGobR7n; 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=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726439AbgBRJ4R (ORCPT + 99 others); Tue, 18 Feb 2020 04:56:17 -0500 Received: from mail-wm1-f67.google.com ([209.85.128.67]:53298 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726373AbgBRJ4R (ORCPT ); Tue, 18 Feb 2020 04:56:17 -0500 Received: by mail-wm1-f67.google.com with SMTP id s10so2087265wmh.3 for ; Tue, 18 Feb 2020 01:56:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=UdH2WwRjeP0392FSfzUeJpWFNKo0z7G6MGSSmZM+wnw=; b=JkGobR7ne7hE4lO8Y9G7F0mwGgXu/l74bR/V2dOzMUri9Jndvty2pi+NIGotqZmZ8N HBo97sQj1xBSQSYgD6BbGL3uQe26Q/j1SOWQOxIzG+pfQb8vH1l4UFQ3JnWANsCss2xp l03JegdpvjVjSqzYW43xqrtJgsoFn1uRaRzCabyggFPT8pt6oNXGbn1i6/SKE0/2BDPT BDmIp2NIjiUUtLycIG38oQH0Nv+X0gIASUrKcMtn3hsU7X/ZabQiQ8nEbYxHMFbJ734V FyTj1fNiudVfEVUUGcEfCu6zsPo6BFhbzrQLV8eTj5iXyrei+J1uxqEyJ7aS8tteAG5c 3HhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=UdH2WwRjeP0392FSfzUeJpWFNKo0z7G6MGSSmZM+wnw=; b=g9pLPPeoChoBTlxNO562euEJUWiiXQd7TIU+6hLX7JVNTZHKAD4RNP/pd/LeJUeP6V 517JjG62TTaHWEOtxSbbeWsaTxVDY7iG4jth/oadOeE6X5QKUhtdoupSh8idQK3IaCpe nJVGbiTxbqk+TLAMmcVrlBZTR7lMj6LqFRMEJoj4yMEk3u7u0VoZhHdxiW48wM87i8cC jT18zOB8EK5Wmc9C6uAORJ9EulNPF/894GBqWPoM2Kcm/mQn3zzUitCo9fqRU/qXaOWT OnMJOmS5RoshrYNQiNsYrGSY7EXs74PjSuMi1F0Glqcbo9th2GNyFLRbjJqRA3dxdgWE 6www== X-Gm-Message-State: APjAAAXQa+ayb75CkKivmqDvhlCARLMgDTeHZyqv6aNEYIGqYG3PZyC6 5Eo2qHLarOmwCExyzJuTWUx3xw== X-Received: by 2002:a1c:38c7:: with SMTP id f190mr2161681wma.94.1582019774942; Tue, 18 Feb 2020 01:56:14 -0800 (PST) Received: from [192.168.86.34] (cpc89974-aztw32-2-0-cust43.18-1.cable.virginm.net. [86.30.250.44]) by smtp.googlemail.com with ESMTPSA id g21sm2835191wmh.17.2020.02.18.01.56.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Feb 2020 01:56:14 -0800 (PST) Subject: Re: [PATCH v2 2/7] nvmem: fix another memory leak in error path To: Bartosz Golaszewski , Linus Walleij , Khouloud Touil , Geert Uytterhoeven Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski , stable@vger.kernel.org References: <20200218094234.23896-1-brgl@bgdev.pl> <20200218094234.23896-3-brgl@bgdev.pl> From: Srinivas Kandagatla Message-ID: <6e7a5df7-6ded-7777-5552-879934c185ad@linaro.org> Date: Tue, 18 Feb 2020 09:56:13 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <20200218094234.23896-3-brgl@bgdev.pl> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 18/02/2020 09:42, Bartosz Golaszewski wrote: > From: Bartosz Golaszewski > > The nvmem struct is only freed on the first error check after its > allocation and leaked after that. Fix it with a new label. > > Cc: > Signed-off-by: Bartosz Golaszewski > --- > drivers/nvmem/core.c | 8 ++++---- > 1 file changed, 4 insertions(+), 4 deletions(-) > > diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c > index b0be03d5f240..c9b3f4047154 100644 > --- a/drivers/nvmem/core.c > +++ b/drivers/nvmem/core.c > @@ -343,10 +343,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) > return ERR_PTR(-ENOMEM); > > rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL); > - if (rval < 0) { > - kfree(nvmem); > - return ERR_PTR(rval); > - } > + if (rval < 0) > + goto err_free_nvmem; > if (config->wp_gpio) > nvmem->wp_gpio = config->wp_gpio; > else > @@ -432,6 +430,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) > put_device(&nvmem->dev); > err_ida_remove: > ida_simple_remove(&nvmem_ida, nvmem->id); > +err_free_nvmem: > + kfree(nvmem); This is not correct fix to start with, if the device has already been intialized before jumping here then nvmem would be freed as part of nvmem_release(). So the bug was actually introduced by adding err_ida_remove label. You can free nvmem at that point but not at any point after that as device core would be holding a reference to it. --srini > > return ERR_PTR(rval); > } >