Skip to content

Commit 0977a3a

Browse files
committed
i2c: lpi2c: Avoid calling clk_get_rate during transfer
JIRA: https://issues.redhat.com/browse/RHEL-48206 CVE: CVE-2024-40965 commit 4268254 Author: Alexander Stein <alexander.stein@ew.tq-group.com> Date: Mon Apr 22 13:36:29 2024 +0200 i2c: lpi2c: Avoid calling clk_get_rate during transfer Instead of repeatedly calling clk_get_rate for each transfer, lock the clock rate and cache the value. A deadlock has been observed while adding tlv320aic32x4 audio codec to the system. When this clock provider adds its clock, the clk mutex is locked already, it needs to access i2c, which in return needs the mutex for clk_get_rate as well. Signed-off-by: Alexander Stein <alexander.stein@ew.tq-group.com> Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Reviewed-by: Andi Shyti <andi.shyti@kernel.org> Signed-off-by: Andi Shyti <andi.shyti@kernel.org> Signed-off-by: David Arcari <darcari@redhat.com>
1 parent 035c324 commit 0977a3a

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

drivers/i2c/busses/i2c-imx-lpi2c.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ struct lpi2c_imx_struct {
100100
__u8 *rx_buf;
101101
__u8 *tx_buf;
102102
struct completion complete;
103+
unsigned long rate_per;
103104
unsigned int msglen;
104105
unsigned int delivered;
105106
unsigned int block_data;
@@ -208,9 +209,7 @@ static int lpi2c_imx_config(struct lpi2c_imx_struct *lpi2c_imx)
208209

209210
lpi2c_imx_set_mode(lpi2c_imx);
210211

211-
clk_rate = clk_get_rate(lpi2c_imx->clks[0].clk);
212-
if (!clk_rate)
213-
return -EINVAL;
212+
clk_rate = lpi2c_imx->rate_per;
214213

215214
if (lpi2c_imx->mode == HS || lpi2c_imx->mode == ULTRA_FAST)
216215
filt = 0;
@@ -597,6 +596,20 @@ static int lpi2c_imx_probe(struct platform_device *pdev)
597596
if (ret)
598597
return ret;
599598

599+
/*
600+
* Lock the parent clock rate to avoid getting parent clock upon
601+
* each transfer
602+
*/
603+
ret = devm_clk_rate_exclusive_get(&pdev->dev, lpi2c_imx->clks[0].clk);
604+
if (ret)
605+
return dev_err_probe(&pdev->dev, ret,
606+
"can't lock I2C peripheral clock rate\n");
607+
608+
lpi2c_imx->rate_per = clk_get_rate(lpi2c_imx->clks[0].clk);
609+
if (!lpi2c_imx->rate_per)
610+
return dev_err_probe(&pdev->dev, -EINVAL,
611+
"can't get I2C peripheral clock rate\n");
612+
600613
pm_runtime_set_autosuspend_delay(&pdev->dev, I2C_PM_TIMEOUT);
601614
pm_runtime_use_autosuspend(&pdev->dev);
602615
pm_runtime_get_noresume(&pdev->dev);

0 commit comments

Comments
 (0)