Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3294868rwa; Tue, 23 Aug 2022 02:10:26 -0700 (PDT) X-Google-Smtp-Source: AA6agR7dPRM/QrytUczclC95/qKb4SssU96lcG25F7Sa2T/VrH09stvKfX7hBRljWPemSRVmZQhC X-Received: by 2002:a05:6402:884:b0:446:5811:c096 with SMTP id e4-20020a056402088400b004465811c096mr2884512edy.382.1661245825989; Tue, 23 Aug 2022 02:10:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661245825; cv=none; d=google.com; s=arc-20160816; b=n81UCcb7sAoPpjwsv+LqxP7OSPuOfnTFmqLmz5frylSRvNnBp3UTqvNdnPM8O50a5P 1y57o65ff5Un/GkOViPCNf5+tuwSe927PBgWgKEuXl2zzr+tN+hX5c4HkzmWVhkBZ7TY Od4K+3/E78vkPq060998v0FKGvVfi8Qw4ddXg/1UAm7ZscMjpsfsBUCSA4xUzqlV5X6R 2A2JCRaIYj3eDlfyCOO4MPslrhJI379jySkEj2z6RzILaeoXlTkuiR3bSU1eHj+zT3Tv 3AyUQuPArreaADP/lFo0uvIOOXwGntQSNisSy1S5FS8E/+cuVWqcMbLLdUx+azfnkmoA XTHw== 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=P4ih3D10L54SdNs3uNqXj2HaqL6j58teqzNz94iF5hA=; b=qxE6LB6KRwG9b7gS+dDEKYdhRnB6V08i2hjCnPhvbCi556Que+6h1CZn3vbQK1lOnZ pxo6+PkGPi2ruwQzMUpSPUmwTgHZB0ViVgvXnYDKHWDWVf9W3VjdIQHIY7bRYo4vdd/F hXLL6RupidubWZ8OWi2awcSvlQsKcEIfC4M+oIW5TsmTbEkgUxM2wdMu5IT9kFIvBkx2 wrhL3Jlsji1eqFntSQYpEcqsNUNPh8RfykoS44ZjST0HXTFyzs2XOciS7vfDSeWxTA19 IyPreimxDRc6bBob4BV2y9Fp37X9LWl0+7X/dqUSft/knI0l5AbdywRB6gz87+dQ1bF8 59hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tbAalBBa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j26-20020a1709062a1a00b0073099d0fe90si9806748eje.216.2022.08.23.02.09.55; Tue, 23 Aug 2022 02:10:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tbAalBBa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S240684AbiHWJF4 (ORCPT + 99 others); Tue, 23 Aug 2022 05:05:56 -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: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Uwe Kleine-König 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 performance") Signed-off-by: Uwe Kleine-König 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 = platform_get_drvdata(pdev); int irq, ret; - ret = pm_runtime_resume_and_get(&pdev->dev); - if (ret < 0) - return ret; + ret = pm_runtime_get_sync(&pdev->dev); hrtimer_cancel(&i2c_imx->slave_timer); @@ -1585,17 +1583,21 @@ static int i2c_imx_remove(struct platfor if (i2c_imx->dma) i2c_imx_dma_free(i2c_imx); - /* 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 == 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); + } clk_notifier_unregister(i2c_imx->clk, &i2c_imx->clk_change_nb); irq = platform_get_irq(pdev, 0); if (irq >= 0) free_irq(irq, i2c_imx); - clk_disable_unprepare(i2c_imx->clk); + + clk_unprepare(i2c_imx->clk); pm_runtime_put_noidle(&pdev->dev); pm_runtime_disable(&pdev->dev);