From patchwork Thu May 30 02:56:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduardo Valentin X-Patchwork-Id: 10968051 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 413AA92A for ; Thu, 30 May 2019 02:56:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2FEE7201CB for ; Thu, 30 May 2019 02:56:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 21C5D287AA; Thu, 30 May 2019 02:56:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EF864201CB for ; Thu, 30 May 2019 02:56:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725821AbfE3C4R (ORCPT ); Wed, 29 May 2019 22:56:17 -0400 Received: from smtp-fw-4101.amazon.com ([72.21.198.25]:2423 "EHLO smtp-fw-4101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727382AbfE3C4P (ORCPT ); Wed, 29 May 2019 22:56:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1559184975; x=1590720975; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Q0hg2IWCcaHXAkM88ceASeZAnofrWOY4R4oYb9tjtt8=; b=bNBXxBWXxvWn+aQBGZxVA2WN7UnD4XE9bu5YzBqHK693aRNFBgpmJh08 HGU/F5psfYM1EiRU3PMpxURbXlyCCv2dIGHdH+gGJ9Nty5RJQRpuUv3uL sfS2jyDuDZf1QVL42Iv3c+LcFEvAJQw4zDDwDUIzSo6LcWTo4KT0fU+QW 8=; X-IronPort-AV: E=Sophos;i="5.60,529,1549929600"; d="scan'208";a="768210709" Received: from iad6-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-2b-8cc5d68b.us-west-2.amazon.com) ([10.124.125.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 30 May 2019 02:56:11 +0000 Received: from EX13MTAUWB001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-2b-8cc5d68b.us-west-2.amazon.com (Postfix) with ESMTPS id 43735A2153; Thu, 30 May 2019 02:56:11 +0000 (UTC) Received: from EX13D05UWB003.ant.amazon.com (10.43.161.26) by EX13MTAUWB001.ant.amazon.com (10.43.161.249) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 30 May 2019 02:56:10 +0000 Received: from EX13MTAUWB001.ant.amazon.com (10.43.161.207) by EX13D05UWB003.ant.amazon.com (10.43.161.26) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 30 May 2019 02:56:10 +0000 Received: from localhost (10.94.220.85) by mail-relay.amazon.com (10.43.161.249) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Thu, 30 May 2019 02:56:10 +0000 From: Eduardo Valentin To: Guenter Roeck CC: Eduardo Valentin , Jean Delvare , , Subject: [PATCHv2 1/2] hwmon: core: add thermal sensors only if dev->of_node is present Date: Wed, 29 May 2019 19:56:04 -0700 Message-ID: <20190530025605.3698-2-eduval@amazon.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190530025605.3698-1-eduval@amazon.com> References: <20190530025605.3698-1-eduval@amazon.com> MIME-Version: 1.0 Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Drivers may register to hwmon and request for also registering with the thermal subsystem (HWMON_C_REGISTER_TZ). However, some of these driver, e.g. marvell phy, may be probed from Device Tree or being dynamically allocated, and in the later case, it will not have a dev->of_node entry. Registering with hwmon without the dev->of_node may result in different outcomes depending on the device tree, which may be a bit misleading. If the device tree blob has no 'thermal-zones' node, the *hwmon_device_register*() family functions are going to gracefully succeed, because of-thermal, *thermal_zone_of_sensor_register() return -ENODEV in this case, and the hwmon error path handles this error code as success to cover for the case where CONFIG_THERMAL_OF is not set. However, if the device tree blob has the 'thermal-zones' entry, the *hwmon_device_register*() will always fail on callers with no dev->of_node, propagating -EINVAL. If dev->of_node is not present, calling of-thermal does not make sense. For this reason, this patch checks first if the device has a of_node before going over the process of registering with the thermal subsystem of-thermal interface. And in this case, when a caller of *hwmon_device_register*() with HWMON_C_REGISTER_TZ and no dev->of_node will still register with hwmon, but not with the thermal subsystem. If all the hwmon part bits are in place, the registration will succeed. Cc: Jean Delvare Cc: Guenter Roeck Cc: linux-hwmon@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Eduardo Valentin --- V1->V2: no change drivers/hwmon/hwmon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c index e694c46ff039..429784edd5ff 100644 --- a/drivers/hwmon/hwmon.c +++ b/drivers/hwmon/hwmon.c @@ -636,7 +636,7 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, if (err) goto free_hwmon; - if (dev && chip && chip->ops->read && + if (dev && dev->of_node && chip && chip->ops->read && chip->info[0]->type == hwmon_chip && (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { const struct hwmon_channel_info **info = chip->info; From patchwork Thu May 30 02:56:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduardo Valentin X-Patchwork-Id: 10968049 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 22D0B92A for ; Thu, 30 May 2019 02:56:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 117E7201CB for ; Thu, 30 May 2019 02:56:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 005C0287AA; Thu, 30 May 2019 02:56:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 90E9D201CB for ; Thu, 30 May 2019 02:56:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727357AbfE3C4O (ORCPT ); Wed, 29 May 2019 22:56:14 -0400 Received: from smtp-fw-9101.amazon.com ([207.171.184.25]:23435 "EHLO smtp-fw-9101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726483AbfE3C4O (ORCPT ); Wed, 29 May 2019 22:56:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1559184973; x=1590720973; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qDsL18aDwQTcILov+CgblUA1JhXa5nFHLDzuuC/mnGA=; b=MjLytlaqESDRNy/PcQzbkUWUbn1LeIPx37VN2HYgfvsUNA4kaTLVjS6k 3UYNZf/8DF9ygNvwE2RwZ8JJER9ilvdRD8nff3HsvQlRAvQ5bUzcpRasB wYye1qZduNZB+tks77k312dUt+xEiazQ52FJF1K8rzZWK1JOXcSDYO9kh 4=; X-IronPort-AV: E=Sophos;i="5.60,529,1549929600"; d="scan'208";a="807528124" Received: from sea3-co-svc-lb6-vlan3.sea.amazon.com (HELO email-inbound-relay-2b-3714e498.us-west-2.amazon.com) ([10.47.22.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 30 May 2019 02:56:12 +0000 Received: from EX13MTAUWB001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-2b-3714e498.us-west-2.amazon.com (Postfix) with ESMTPS id 2C6DDA25B0; Thu, 30 May 2019 02:56:11 +0000 (UTC) Received: from EX13D05UWB004.ant.amazon.com (10.43.161.208) by EX13MTAUWB001.ant.amazon.com (10.43.161.249) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 30 May 2019 02:56:11 +0000 Received: from EX13MTAUWB001.ant.amazon.com (10.43.161.207) by EX13D05UWB004.ant.amazon.com (10.43.161.208) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 30 May 2019 02:56:10 +0000 Received: from localhost (10.94.220.85) by mail-relay.amazon.com (10.43.161.249) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Thu, 30 May 2019 02:56:11 +0000 From: Eduardo Valentin To: Guenter Roeck CC: Eduardo Valentin , Jean Delvare , , Subject: [PATCHv2 2/2] hwmon: core: fix potential memory leak in *hwmon_device_register* Date: Wed, 29 May 2019 19:56:05 -0700 Message-ID: <20190530025605.3698-3-eduval@amazon.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190530025605.3698-1-eduval@amazon.com> References: <20190530025605.3698-1-eduval@amazon.com> MIME-Version: 1.0 Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When registering a hwmon device with HWMON_C_REGISTER_TZ flag in place, the hwmon subsystem will attempt to register the device also with the thermal subsystem. When the of-thermal registration fails, __hwmon_device_register jumps to ida_remove, leaving the locally allocated hwdev pointer. This patch fixes the leak by jumping to a new label that will first unregister hdev and then fall into the kfree of hwdev to finally remove the idas and propagate the error code. Cc: Jean Delvare Cc: Guenter Roeck Cc: linux-hwmon@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Eduardo Valentin --- V1->V2: removed the device_unregister() before jumping into the new label, as suggested in the first review round. drivers/hwmon/hwmon.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c index 429784edd5ff..620f05fc412a 100644 --- a/drivers/hwmon/hwmon.c +++ b/drivers/hwmon/hwmon.c @@ -652,10 +652,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, if (info[i]->config[j] & HWMON_T_INPUT) { err = hwmon_thermal_add_sensor(dev, hwdev, j); - if (err) { - device_unregister(hdev); - goto ida_remove; - } + if (err) + goto device_unregister; } } } @@ -663,6 +661,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, return hdev; +device_unregister: + device_unregister(hdev); free_hwmon: kfree(hwdev); ida_remove: