From patchwork Tue Jun 6 15:59:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johan Hovold X-Patchwork-Id: 9769225 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B4D436034B for ; Tue, 6 Jun 2017 16:03:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BDFC9283CF for ; Tue, 6 Jun 2017 16:03:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B1EB52843F; Tue, 6 Jun 2017 16:03:06 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable 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 9C8E228415 for ; Tue, 6 Jun 2017 16:03:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751775AbdFFQCa (ORCPT ); Tue, 6 Jun 2017 12:02:30 -0400 Received: from mail-lf0-f66.google.com ([209.85.215.66]:34825 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751541AbdFFQAx (ORCPT ); Tue, 6 Jun 2017 12:00:53 -0400 Received: by mail-lf0-f66.google.com with SMTP id v20so9203745lfa.2; Tue, 06 Jun 2017 09:00:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=ldlmI310O1z0vka5o2XxX57bVJBBSBUoZiL+moUoZAo=; b=Qzo969QN6J2KXb0Tk+8nSkQlbiMDqWpckuRMvFHuH7O720p5pAHta7/SkAhllXS2GJ hsUovqkcmm8dYzdH1y4NC4GWgRXMU5FR2vI2A5iL8fLfE5yt1v49pRqNTxb+j1EYp7sf Kg5vw5xq8YXQFxcFyfmja1kies2CPXWNFmQ+7e62HMnFCVeLnHFhv02BSji8enCUhqon +PZmqEbuLRSSSqQXOh1VvHCaFMvvUprejEzV2iOSfMpNLTvqM0P7uCZHvILxKaU54nBO K9o3rW0jqPC388rhoctF43G1D9qnMDfpdk4RRGJdWAjPygw3V+uGcKsEXW+3byueCriT iMuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=ldlmI310O1z0vka5o2XxX57bVJBBSBUoZiL+moUoZAo=; b=Z+GKN2Zp6/BVkW9dQ/z/XtWQTnqzs3IfOq9LtfwaqC1/iRRQwK21rHsS3ebwzCwURU ytyrPq28l2e8pH1aQDRFXbhGxKkYMEvStO/wJ3VS8qORFIV3r/fRFzxKBJe/fVu3vPMA 8aLyomgaXBg1uzqrha3hYTDfpEnIoXArs9PBNRw9meHAsibd1kHFCWZJJvBjgBemBLZP ffsETWfN/2el55wyjnf4c/d7P2Gc6YIj/9IFVv+6RdMvc4AFR6XrtPa2toZVf4mKZHW0 584pVVKXmqc86302lEEXZ6177PSE/xkKxATgQ2aO2mBBLZGyLFm2Cb17AMnmiRXAquR9 OHhw== X-Gm-Message-State: AODbwcCScp58AOU834GxTFtHYhfx7vsHJuqHbMr8a+hU2krceU3P+9UN jBjDtb06flC1Uw== X-Received: by 10.25.31.9 with SMTP id f9mr6734363lff.160.1496764850799; Tue, 06 Jun 2017 09:00:50 -0700 (PDT) Received: from xi.terra (c-42c8e255.07-184-6d6c6d4.cust.bredbandsbolaget.se. [85.226.200.66]) by smtp.gmail.com with ESMTPSA id c2sm3206602ljd.66.2017.06.06.09.00.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Jun 2017 09:00:47 -0700 (PDT) Received: from johan by xi.terra with local (Exim 4.89) (envelope-from ) id 1dIGuV-00071P-30; Tue, 06 Jun 2017 18:00:47 +0200 From: Johan Hovold To: Greg Kroah-Hartman Cc: Linus Walleij , Peter Chen , Rob Herring , Arnd Bergmann , Sricharan R , Zhang Rui , Eduardo Valentin , linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, linux-usb@vger.kernel.org, Johan Hovold , stable , Laxman Dewangan Subject: [PATCH v2 6/7] thermal: max77620: fix device-node reference imbalance Date: Tue, 6 Jun 2017 17:59:03 +0200 Message-Id: <20170606155904.26819-7-johan@kernel.org> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170606155904.26819-1-johan@kernel.org> References: <20170606155904.26819-1-johan@kernel.org> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The thermal child device reuses the parent MFD-device device-tree node when registering a thermal zone, but did not take a reference to the node. This leads to a reference imbalance, and potential use-after-free, when the node reference is dropped by the platform-bus device destructor (once for the child and later again for the parent). Fix this by dropping any reference already held to a device-tree node and getting a reference to the parent's node which will be balanced on reprobe or on platform-device release, whichever comes first. Note that simply clearing the of_node pointer on probe errors and on driver unbind would not allow the use of device-managed resources as specifically thermal_zone_of_sensor_unregister() claims that a valid device-tree node pointer is needed during deregistration (even if it currently does not seem to use it). Fixes: ec4664b3fd6d ("thermal: max77620: Add thermal driver for reporting junction temp") Cc: stable # 4.9 Cc: Laxman Dewangan Signed-off-by: Johan Hovold --- drivers/thermal/max77620_thermal.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/drivers/thermal/max77620_thermal.c b/drivers/thermal/max77620_thermal.c index e9a1fe342760..71d35f3c9215 100644 --- a/drivers/thermal/max77620_thermal.c +++ b/drivers/thermal/max77620_thermal.c @@ -104,8 +104,6 @@ static int max77620_thermal_probe(struct platform_device *pdev) return -EINVAL; } - pdev->dev.of_node = pdev->dev.parent->of_node; - mtherm->dev = &pdev->dev; mtherm->rmap = dev_get_regmap(pdev->dev.parent, NULL); if (!mtherm->rmap) { @@ -113,6 +111,14 @@ static int max77620_thermal_probe(struct platform_device *pdev) return -ENODEV; } + /* + * Drop any current reference to a device-tree node and get a + * reference to the parent's node which will be balanced on reprobe or + * on platform-device release. + */ + of_node_put(pdev->dev.of_node); + pdev->dev.of_node = of_node_get(pdev->dev.parent->of_node); + mtherm->tz_device = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, mtherm, &max77620_thermal_ops); if (IS_ERR(mtherm->tz_device)) {