From nobody Thu Dec 18 13:01:03 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 861DBC32793 for ; Tue, 23 Aug 2022 09:06:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240817AbiHWJF6 (ORCPT ); Tue, 23 Aug 2022 05:05:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243457AbiHWJFI (ORCPT ); Tue, 23 Aug 2022 05:05:08 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25C24844D7; Tue, 23 Aug 2022 01:29:19 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 08F57B81C54; Tue, 23 Aug 2022 08:28:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 534FBC433C1; Tue, 23 Aug 2022 08:28:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661243291; bh=ZFjxRbiSTCcpDSWigsu2rT70OLmGRjEoFzUdbveCR54=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tbAalBBabyavGiPT756JXSNRUjfTqMGER3tURX/vXoIL4PlJHOM455Kh/73AL5PQz vyP8W5/f8hVehYgUXMSXdU8SWXihIBBL+ghoSSj4U2KB0nr0GRNmwD6oYvzVdWxoQX /9Je85SS2wri6fjkYIPTwSO8NjxIzTS4dyJxsaNc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Oleksij Rempel , Wolfram Sang Subject: [PATCH 5.19 229/365] i2c: imx: Make sure to unregister adapter on remove() Date: Tue, 23 Aug 2022 10:02:10 +0200 Message-Id: <20220823080127.791052445@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080118.128342613@linuxfoundation.org> References: <20220823080118.128342613@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Uwe Kleine-K=C3=B6nig commit d98bdd3a5b50446d8e010be5b04ce81c4eabf728 upstream. If for whatever reasons pm_runtime_resume_and_get() fails and .remove() is exited early, the i2c adapter stays around and the irq still calls its handler, while the driver data and the register mapping go away. So if later the i2c adapter is accessed or the irq triggers this results in havoc accessing freed memory and unmapped registers. So unregister the software resources even if resume failed, and only skip the hardware access in that case. Fixes: 588eb93ea49f ("i2c: imx: add runtime pm support to improve the perfo= rmance") Signed-off-by: Uwe Kleine-K=C3=B6nig Acked-by: Oleksij Rempel Signed-off-by: Wolfram Sang Signed-off-by: Greg Kroah-Hartman --- drivers/i2c/busses/i2c-imx.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) --- a/drivers/i2c/busses/i2c-imx.c +++ b/drivers/i2c/busses/i2c-imx.c @@ -1572,9 +1572,7 @@ static int i2c_imx_remove(struct platfor struct imx_i2c_struct *i2c_imx =3D platform_get_drvdata(pdev); int irq, ret; =20 - ret =3D pm_runtime_resume_and_get(&pdev->dev); - if (ret < 0) - return ret; + ret =3D pm_runtime_get_sync(&pdev->dev); =20 hrtimer_cancel(&i2c_imx->slave_timer); =20 @@ -1585,17 +1583,21 @@ static int i2c_imx_remove(struct platfor if (i2c_imx->dma) i2c_imx_dma_free(i2c_imx); =20 - /* setup chip registers to defaults */ - imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR); - imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR); - imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR); - imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR); + if (ret =3D=3D 0) { + /* setup chip registers to defaults */ + imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR); + imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR); + imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR); + imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR); + clk_disable(i2c_imx->clk); + } =20 clk_notifier_unregister(i2c_imx->clk, &i2c_imx->clk_change_nb); irq =3D platform_get_irq(pdev, 0); if (irq >=3D 0) free_irq(irq, i2c_imx); - clk_disable_unprepare(i2c_imx->clk); + + clk_unprepare(i2c_imx->clk); =20 pm_runtime_put_noidle(&pdev->dev); pm_runtime_disable(&pdev->dev);