Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp5689398ybv; Tue, 18 Feb 2020 02:06:48 -0800 (PST) X-Google-Smtp-Source: APXvYqw3HrzLOMyfZgV+79pXoqYEUFBBs5eJkqq2brJzbAGJhqjEydtevRAZ0WesBjg8Ow9+yvLL X-Received: by 2002:a05:6808:358:: with SMTP id j24mr732024oie.89.1582020408610; Tue, 18 Feb 2020 02:06:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582020408; cv=none; d=google.com; s=arc-20160816; b=IQ4/wWCdTkEsKPOX8aeuipoIeU53zG1ku4vV8ymX8mKqpfrybXEnetTGfmHppv73Zc i4EAtnd9wDiAMT9fL9edu1yeVXSenV1PBD0BZ9uiqOzTyny+6IWq1y2ApqEjD76f2LjH FiTLFJYQdJJocLDLu/8x4iCIrNhnKTJuam3XR2F2VcFct1pxxX91TGtNqhynlmJ99jJP VFVu56RFWP/eVtjWFmRQMnsfg+DT8SwO1n94guVJXvkkLQwngVgN0qWeQ2muV/jmxSOJ NC9dl5vs3bYq7PLR+5Kepe60urlNhSs+orWDtzlY9J4sFuLu5s6laPGjtE+4nHuTyeU/ d/Qg== 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:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=bo6pwHsMO3Fl7f+CYI2NiFOFstRv2BITpPEO1O08RVU=; b=hZmPGuB2ylPRzmixq3FXKsdvUSno4qnxVJVquQQXAhlRXCKjtKGvakbY4fybUdyvEc KBnQ1Zi81rGYblVwgS41F1Sd1FvHKrf/FI4G3tco6mQ90MKtZEK8fXoPsNtsE8gB5RDD m+sAY9uO0IYZ6rwXphlqCy38wEgF1e2AIPW6GmzC2xaYmv8AU8WgOvWefq25gU2Y0TiC CEh3cfFfiy8iC9mBl4HRa2BPqpXC0Kq2fvhlAv9wAq2ROepTtOnBjvTejqeF0Z2G+7q7 SYk0hIz0HSkUWl8AeJ94N+FZseRJ8HmpMyUDst5Z6m5Z2k31fbiInWZ6s20aN39kJJ3U 7lDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=OX2lL6mU; 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 i9si1724584otp.139.2020.02.18.02.06.35; Tue, 18 Feb 2020 02:06:48 -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=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=OX2lL6mU; 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 S1726700AbgBRKF6 (ORCPT + 99 others); Tue, 18 Feb 2020 05:05:58 -0500 Received: from mail-il1-f194.google.com ([209.85.166.194]:45441 "EHLO mail-il1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726682AbgBRKF5 (ORCPT ); Tue, 18 Feb 2020 05:05:57 -0500 Received: by mail-il1-f194.google.com with SMTP id p8so16777094iln.12 for ; Tue, 18 Feb 2020 02:05:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=bo6pwHsMO3Fl7f+CYI2NiFOFstRv2BITpPEO1O08RVU=; b=OX2lL6mUGKvWsONNxNeQhgNsEMx4tDkhQEeAE9Ee8qhkCh0DbNwjGmW7IIk9/AhWBk TklebYtsegUDb9Bz8k5n/C99gUKH8OJQkpQUScJeKddcOft8KyABgVEc5OqX2k9aTUY5 vZjeK34uEmbHAFBdAPPgIl44hRRO559slOre0K9NWGFZuUY+mwMwERxliDSZehVUC3c9 Ax3+i0lgRnuQTXoqsvy0sboRLyX4koir7/Y3FUJc3OyoyVtMt2EJzFci9+CD1qHiBgux TPOY2Nt3bXKARZ9f7t1hq55i8KJ4eoqcF58DekNoNQhWh9y2+wMkc2bZx78sQMg9gZUp qPAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=bo6pwHsMO3Fl7f+CYI2NiFOFstRv2BITpPEO1O08RVU=; b=ODWA8bcns8+lv5Nyc1hwCYcA7vTr6X0581RyFkdKd/tF3OwRxtmOHlz4h+SSpq0k1J pef39fSxQJPES8RP11mgUG0i/b9oMVl6NzPl4O8JN4pg653KuYIhtjfXJ0imzRhNW2Ew HbnYXDb5np52g+i8KmCjl88m/v0UBDIukD+HV8IY9Ng6D/PWphVjOIVNBJ/MK112MCJ8 1y9wrV0QCCXf+mCS8gPz0f6FZzx8+Ux0SynX66AARr6znRn08NbhRvzv2FcVnz5iamtQ wUzyMKm4TeuYmULdrYDP0MX6mXbxUrmIQ37AQHWmk4V40EuEF3Z2eXKYht6H9LxS21Qx HoFw== X-Gm-Message-State: APjAAAU/d4UleIUsNGnGUSvdrd891ibcfMmNeFy0wPTDgdQpo0dfCl4P s66IzKjKux+rWe4FJvOGGB83/BsDIlaCKH0/ySFpTQ== X-Received: by 2002:a92:9c1c:: with SMTP id h28mr17741163ili.189.1582020355566; Tue, 18 Feb 2020 02:05:55 -0800 (PST) MIME-Version: 1.0 References: <20200218094234.23896-1-brgl@bgdev.pl> <20200218094234.23896-3-brgl@bgdev.pl> <6e7a5df7-6ded-7777-5552-879934c185ad@linaro.org> In-Reply-To: <6e7a5df7-6ded-7777-5552-879934c185ad@linaro.org> From: Bartosz Golaszewski Date: Tue, 18 Feb 2020 11:05:44 +0100 Message-ID: Subject: Re: [PATCH v2 2/7] nvmem: fix another memory leak in error path To: Srinivas Kandagatla Cc: Linus Walleij , Khouloud Touil , Geert Uytterhoeven , "open list:GPIO SUBSYSTEM" , Linux Kernel Mailing List , Bartosz Golaszewski , stable Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org wt., 18 lut 2020 o 10:56 Srinivas Kandagatla napisa=C5=82(a): > > > > 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 n= vmem_config *config) > > return ERR_PTR(-ENOMEM); > > > > rval =3D 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 =3D config->wp_gpio; > > else > > @@ -432,6 +430,8 @@ struct nvmem_device *nvmem_register(const struct nv= mem_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. > OK I see - I missed the release() callback assignment. Frankly: I find this split of resource management responsibility confusing. Since the users are expected to call nvmem_unregister() anyway - wouldn't it be more clear to just free all resources there? What is the advantage of defining the release() callback for device type here? Bartosz