Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp839174pxu; Mon, 23 Nov 2020 05:35:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJzosvy4HtsV9ZKDBE8QEc7k9pVR7eFohbhy+MruOGDAxdPQXfMyQiVjn6gejEwBVgQ5wT9y X-Received: by 2002:aa7:cb4a:: with SMTP id w10mr47500495edt.343.1606138550289; Mon, 23 Nov 2020 05:35:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606138550; cv=none; d=google.com; s=arc-20160816; b=XpGRlK0PrtH3K1ifI9vFJVD0wmoTnTYeTS33+zRme2kaTQbdDMp4JqfGe72hH9a3iM fpG6j1HXJ+tsWG1YmccumujmApPfpFCAX/piug8Hp5jAmCZ2kkP25ICTPkSC7V9ygMMS fPoxc0/rexWqmh6D9JXw3Jua20JsFGW6vK7mB7qH2DFav7iyKfDx+syHQe2P2uivaD+M TJalZKRztS1+kxDAmSP3vKPagtoowJ/nDPFecb0YqoXwPdRK1h+kKUp9iBYhH/0AnxwQ vH7Jwp5DMRAqKkCSSd3YvIAaAcZLpoQjt/YLEVpzTqrtClJqK0UuYpnH4dD0s3w/Y//m JaEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zmvQyhRRfeKLcqZ/VM1vmF8sSDAT1EG39afq+qKrAHE=; b=wFfAjq8Fhkeg5EXyEmnjQnrIgIGd/GRmJ+9gTiFLYPVbVcWiqNX2Igixi42D8csltt VECX/PDZ36uceaEa/R49vaXYhJWMZEEVrvEAO5OxkVZB7DluruA7nVfpThPbZpcfWyz/ cKd4TGG/W7v1NxPuiN9/3pGc2+N9J7cjaygbj/+sObB2IWI3eKi9M7XKSHFHAXpWTTXt 8FV3sTMODEqJYuNT4gsyXEZUQda+qE0q5W3MEmCDw6KjOeJeI3X2nnH/Oyz+swfciEIl NUVh/5G4vfoTbWkT3tzNmGphv+gwnqWL0h/GwlV7JKtpv7UuYjNIPO5P8tgDrv8NIfBL aYQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Tr5nocSz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bm4si6486131ejb.283.2020.11.23.05.35.27; Mon, 23 Nov 2020 05:35:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Tr5nocSz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730100AbgKWM0I (ORCPT + 99 others); Mon, 23 Nov 2020 07:26:08 -0500 Received: from mail.kernel.org ([198.145.29.99]:35876 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730093AbgKWM0C (ORCPT ); Mon, 23 Nov 2020 07:26:02 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (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 7BB082076E; Mon, 23 Nov 2020 12:26:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1606134362; bh=hmvQGJEWgLNpb6GPmpt469sJIfq15Tw1rgxN5BXvMUM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Tr5nocSzecG86pUJpaB7pz1xzfbqgONrAL/R0qJORNbg9d8/KQLsAoY/rxQcuWsvl Th7i2sVMnwgl/bdzDkoOTr2Lm3qGZEizWznOGWFgNR6ZJD/6+uyBaqZNHR63A+wh8f adhnJHZo+6bwEc7aPrelGTlPTXL3G7r263iAGSdw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?Micha=C5=82=20Miros=C5=82aw?= , Mark Brown , Ahmad Fatoum Subject: [PATCH 4.9 41/47] regulator: fix memory leak with repeated set_machine_constraints() Date: Mon, 23 Nov 2020 13:22:27 +0100 Message-Id: <20201123121807.545655380@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201123121805.530891002@linuxfoundation.org> References: <20201123121805.530891002@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Michał Mirosław commit 57a6ad482af256b2a13de14194fb8f67c1a65f10 upstream. Fixed commit introduced a possible second call to set_machine_constraints() and that allocates memory for rdev->constraints. Move the allocation to the caller so it's easier to manage and done once. Fixes: aea6cb99703e ("regulator: resolve supply after creating regulator") Cc: stable@vger.kernel.org Signed-off-by: Michał Mirosław Tested-by: Ahmad Fatoum # stpmic1 Link: https://lore.kernel.org/r/78c3d4016cebc08d441aad18cb924b4e4d9cf9df.1605226675.git.mirq-linux@rere.qmqm.pl Signed-off-by: Mark Brown Signed-off-by: Greg Kroah-Hartman --- drivers/regulator/core.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -1025,7 +1025,6 @@ static int _regulator_do_enable(struct r /** * set_machine_constraints - sets regulator constraints * @rdev: regulator source - * @constraints: constraints to apply * * Allows platform initialisation code to define and constrain * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: @@ -1033,21 +1032,11 @@ static int _regulator_do_enable(struct r * regulator operations to proceed i.e. set_voltage, set_current_limit, * set_mode. */ -static int set_machine_constraints(struct regulator_dev *rdev, - const struct regulation_constraints *constraints) +static int set_machine_constraints(struct regulator_dev *rdev) { int ret = 0; const struct regulator_ops *ops = rdev->desc->ops; - if (constraints) - rdev->constraints = kmemdup(constraints, sizeof(*constraints), - GFP_KERNEL); - else - rdev->constraints = kzalloc(sizeof(*constraints), - GFP_KERNEL); - if (!rdev->constraints) - return -ENOMEM; - ret = machine_constraints_voltage(rdev, rdev->constraints); if (ret != 0) return ret; @@ -3924,7 +3913,6 @@ struct regulator_dev * regulator_register(const struct regulator_desc *regulator_desc, const struct regulator_config *cfg) { - const struct regulation_constraints *constraints = NULL; const struct regulator_init_data *init_data; struct regulator_config *config = NULL; static atomic_t regulator_no = ATOMIC_INIT(-1); @@ -4024,14 +4012,23 @@ regulator_register(const struct regulato /* set regulator constraints */ if (init_data) - constraints = &init_data->constraints; + rdev->constraints = kmemdup(&init_data->constraints, + sizeof(*rdev->constraints), + GFP_KERNEL); + else + rdev->constraints = kzalloc(sizeof(*rdev->constraints), + GFP_KERNEL); + if (!rdev->constraints) { + ret = -ENOMEM; + goto wash; + } if (init_data && init_data->supply_regulator) rdev->supply_name = init_data->supply_regulator; else if (regulator_desc->supply_name) rdev->supply_name = regulator_desc->supply_name; - ret = set_machine_constraints(rdev, constraints); + ret = set_machine_constraints(rdev); if (ret == -EPROBE_DEFER) { /* Regulator might be in bypass mode and so needs its supply * to set the constraints */ @@ -4040,7 +4037,7 @@ regulator_register(const struct regulato * that is just being created */ ret = regulator_resolve_supply(rdev); if (!ret) - ret = set_machine_constraints(rdev, constraints); + ret = set_machine_constraints(rdev); else rdev_dbg(rdev, "unable to resolve supply early: %pe\n", ERR_PTR(ret));