Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3901647imu; Fri, 18 Jan 2019 21:08:05 -0800 (PST) X-Google-Smtp-Source: ALg8bN4bB9ti9PglVP2WfjLeJo2Pu9FIZGcUzc+p9rhqdOLJ0r476+0zG7ASZ4tFKSJ/go1L4n4d X-Received: by 2002:a63:587:: with SMTP id 129mr20248630pgf.273.1547874485372; Fri, 18 Jan 2019 21:08:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547874485; cv=none; d=google.com; s=arc-20160816; b=grSVD0aAExr3/T9pOJUx0odhV4iVfMGVZY/LdApbukf4Sk8wjBfvPKz2VqaO1+K6TG X5MhTcHHaNLjvqj+4B/Cb9XyZ6iBGZs5UzGHli7eVsVkUNbasdxPLH1gqPr4hNIhbo7r Rw3pKojBW4pRF3krj6/QBKu+udy2X0q0NFClkIUsSdUsY/bhO/bPDGsx1751PAH+ULgD qGxHluQ6C/3kMOmEM2A3GCPV+35WQfDp86uQRqoxiMPd6uMHiwfnJFi58AlvN6VuqJq+ qau3BqzGC8corf2eCrzIdm7AIkTsaeA88XQQy5MQmtTQFW74Ugax8Z3vvi2l3CQcG/6O sM6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=3/Snpyih4xmGjMkVJWyCg5M/HlR3AsZUTEyMtROheFs=; b=yUeYL65lnaHKsucvz8plPEx9Lr7TdzaKV2BwhdxTmM55xJZCrquHlOszqFF/xtNBeb H2RdawIvf4PZ0mUPDFqH0ZrWD/bsXgjFN1zNTHBlSSZlVAatmoXRBvsAovdg6JX9+acb nH3GH/+GYFfzc7hObK4/wPens197r5LdCKDwf48JQIng5o9ziZ8SHGnah7B6Dp+yl+Ve WQaruPp+yVme3IwmAY1+Lll/nl3c/tERmNVKyb95asdd3airdcnsEQnvqFq5kWG08PWX d188sV0peCfo1grHDQQP4Ci0ZV33VFgHEEkhj/c9GSwyEGDmWafTjRm9PUuiQ0/0q++z Ci3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=tUUR1vXg; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c125si6623458pfa.216.2019.01.18.21.07.11; Fri, 18 Jan 2019 21:08:05 -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=@gmail.com header.s=20161025 header.b=tUUR1vXg; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726870AbfASFDB (ORCPT + 99 others); Sat, 19 Jan 2019 00:03:01 -0500 Received: from mail-pl1-f193.google.com ([209.85.214.193]:35048 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725897AbfASFDA (ORCPT ); Sat, 19 Jan 2019 00:03:00 -0500 Received: by mail-pl1-f193.google.com with SMTP id p8so7247560plo.2; Fri, 18 Jan 2019 21:02:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=3/Snpyih4xmGjMkVJWyCg5M/HlR3AsZUTEyMtROheFs=; b=tUUR1vXgU6yrt+lnrhISmeP5/mYJQTF93vr/8lBKsbLt8fdvNWwHTiS65iW8wGP0uF wxG9G8NPdsXh4EBneNMgP+QIS96pZsKWcbYnnBuU9+19ES1RGMfB04l70JsHNllWuE8r CY9OlzNpQ6q85lbtTHNORSMae2YiFxjVF7vy6hE8a07W5JpTck9syd+CAWxfn//VauCQ DrjSF1FHe16tdTXKfIVagP/h2oyrRnYdkFLnClFi8nF5QMrWi51RNO/IjFhLVNfexpBN WwO/RKoZ2jb8lUSWqwe3nQGKxGQdPjKzDaR4aT3bI2E0bDiw7CikoRPxozBcEOPi+RVp a3vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=3/Snpyih4xmGjMkVJWyCg5M/HlR3AsZUTEyMtROheFs=; b=mvsK1tKum2t2yzXMtT4fdBjSRWbg6z7GP4fSzqIYh1W5UrrG3dh9zBnpyzFmNkPILJ 5H/hszCbzrUhev0POJybZpemrNZaTeSVeaw29R1L4p4Z693ke8ZWTznPHjrKGFKd/hR4 iXY3Xrg4/wXQfn5Vt7+33K7DfPT+/jQuepzy/qytxlh18HG/XrIb730EQafOVqbck/fN pd/8WlAqCMJZvQulvU16YDuaYOU9CdrtXLjQni980CC40q2yB2GBz4nCmW2SFP6XIwuL /XhVDXqCueg4wBUVIy1DpIoPWyusGeu+Wdn8uvwy4E0IaUvEwn5YdvK6YR3a3v3lKEmA hSXg== X-Gm-Message-State: AJcUukcIjfi+CrEuzcp7DYIlE0o5VtLKhJhGmDj+BoTiF/tWCf1gfQG6 W+vbuFQjj8d6mpgohiAcm0U= X-Received: by 2002:a17:902:82c2:: with SMTP id u2mr21782414plz.110.1547874179505; Fri, 18 Jan 2019 21:02:59 -0800 (PST) Received: from localhost (68.168.130.77.16clouds.com. [68.168.130.77]) by smtp.gmail.com with ESMTPSA id 4sm8040214pfq.10.2019.01.18.21.02.58 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 18 Jan 2019 21:02:58 -0800 (PST) From: Yangtao Li To: myungjoo.ham@samsung.com, kyungmin.park@samsung.com, cw00.choi@samsung.com Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Yangtao Li Subject: [PATCH] PM / devfreq: fix mem leak and missing check of return value in devfreq_add_device() Date: Sat, 19 Jan 2019 00:02:56 -0500 Message-Id: <20190119050256.22520-1-tiny.windzz@gmail.com> X-Mailer: git-send-email 2.17.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 'devfreq' is malloced in devfreq_add_device() and should be freed in the error handling cases, otherwise it will cause memory leak. devm_kzalloc() could fail, so insert a check of its return value. And if it fails, returns -ENOMEM. Signed-off-by: Yangtao Li --- drivers/devfreq/devfreq.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c index 0ae3de76833b..fe6c157cb7e0 100644 --- a/drivers/devfreq/devfreq.c +++ b/drivers/devfreq/devfreq.c @@ -651,7 +651,7 @@ struct devfreq *devfreq_add_device(struct device *dev, mutex_unlock(&devfreq->lock); err = set_freq_table(devfreq); if (err < 0) - goto err_out; + goto err_dev; mutex_lock(&devfreq->lock); } @@ -683,16 +683,27 @@ struct devfreq *devfreq_add_device(struct device *dev, goto err_out; } - devfreq->trans_table = - devm_kzalloc(&devfreq->dev, - array3_size(sizeof(unsigned int), - devfreq->profile->max_state, - devfreq->profile->max_state), - GFP_KERNEL); + devfreq->trans_table = devm_kzalloc(&devfreq->dev, + array3_size(sizeof(unsigned int), + devfreq->profile->max_state, + devfreq->profile->max_state), + GFP_KERNEL); + if (!devfreq->trans_table) { + mutex_unlock(&devfreq->lock); + err = -ENOMEM; + goto err_devfreq; + } + devfreq->time_in_state = devm_kcalloc(&devfreq->dev, - devfreq->profile->max_state, - sizeof(unsigned long), - GFP_KERNEL); + devfreq->profile->max_state, + sizeof(unsigned long), + GFP_KERNEL); + if (!devfreq->time_in_state) { + mutex_unlock(&devfreq->lock); + err = -ENOMEM; + goto err_devfreq; + } + devfreq->last_stat_updated = jiffies; srcu_init_notifier_head(&devfreq->transition_notifier_list); @@ -726,7 +737,7 @@ struct devfreq *devfreq_add_device(struct device *dev, err_init: mutex_unlock(&devfreq_list_lock); - +err_devfreq: devfreq_remove_device(devfreq); devfreq = NULL; err_dev: -- 2.17.0