Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1313098pxu; Mon, 23 Nov 2020 18:06:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJwjhqb5e9QjMCHz9zzzl3b0HfWc5800nuu9YiILzFnNIUYU2KKfToikzFKRS9Vjc88JKy4S X-Received: by 2002:a17:906:57c9:: with SMTP id u9mr2377690ejr.434.1606183566113; Mon, 23 Nov 2020 18:06:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606183566; cv=none; d=google.com; s=arc-20160816; b=x89a504AH78/WbdJ/T4OtMS3vfZniiBqcX6tuFhgpK5g7zvlWtiPQT1VMYnlsBNjf8 UyKNP248eKCvEyfK3XnCqiCDGvVenfdJuOZdthPMh4T/npcPCjd7sk33f5Y+e0Kp1AFL wl2XP++xoXF+ZP24VJ57cf1H8H9StTkO3axdMGu2OtvFu18kMnKy6LInd3CFMzmpnOsk fh2EDz1HRyw18WEYVp4HNEpLPwgo+103OoIQfCd3y/4+LW/EmXkDQbwLiKvbordlE5yQ Qzdu10hlHy21iGRYVOiDj6/tX4KCBlwPH2+IEQk4jLzL0LOd8/2i2VrPNc+mdmpzRVg0 3Z/g== 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=IlpCRRZstFvsrH8n3Kvo95trJI6/N77c3RjKLoAYS4k=; b=OobrHnm0E2JHtrRN2T9KrCIW1CEv3Ew2bBZdEdBT03xxSgMAUZvaa2ZwgJPAdYIzmG a291Z5u4GmJ/Vym2VaV7O+4xOpuezvBJYgjujXkIX6/mkzwZWB+5BdC93oUIDVC49QlR eBLD1j3p07/33aRkwTZB+uRz/c0MsOseHzqJenC4r0eaK2KiRlVD4p7ecmE0LZzV9eBY Fs5jbHSuK7/3HHRUwdqA9bSe60gux/VWwlw0xByYMeaYxx0mewfcHDSa8MXLfPRlTZtT OQv20CQ8Zq4q6NGk0uDGJ1xxyEcs/PNlkkjvKvw/+XR+PSW88thwaoFqjDQ0OXKTaYV5 6PSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=qt1E2jMV; 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 m16si7477548ejc.357.2020.11.23.18.05.43; Mon, 23 Nov 2020 18:06:06 -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=qt1E2jMV; 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 S2387610AbgKWNEj (ORCPT + 99 others); Mon, 23 Nov 2020 08:04:39 -0500 Received: from mail.kernel.org ([198.145.29.99]:35426 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387614AbgKWMvE (ORCPT ); Mon, 23 Nov 2020 07:51:04 -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 AEFA6204EF; Mon, 23 Nov 2020 12:51:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1606135863; bh=DyV3NMrTf54fo09w7VNMgr1zyAz6enWWvzekMq9V1QY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qt1E2jMVY2/0+TjcZykeoCLaVh0/COWvX/csTVdPxWbZmT2HQJ00lX9NoILpOI6wC nLDoSOEVX3jAiXfaW2YqK0Kn152HVKPG7LziNm9hP4qAuREn7kmZhebrOWm04Wdfom CyonkjQRFLpjfrmLxxue61vVRX6RcOhX5HoyacLg= 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 5.9 223/252] regulator: fix memory leak with repeated set_machine_constraints() Date: Mon, 23 Nov 2020 13:22:53 +0100 Message-Id: <20201123121846.334515356@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201123121835.580259631@linuxfoundation.org> References: <20201123121835.580259631@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 @@ -1280,7 +1280,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: @@ -1288,21 +1287,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; @@ -5112,7 +5101,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); @@ -5251,14 +5239,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 */ @@ -5267,7 +5264,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));