]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
i2c: jz4780: Cache host clock rate at probe to prevent CCF prepare_lock deadlock
authorH. Nikolaus Schaller <hns@goldelico.com>
Sun, 19 Jul 2026 20:19:43 +0000 (22:19 +0200)
committerAndi Shyti <andi.shyti@kernel.org>
Mon, 27 Jul 2026 20:56:08 +0000 (22:56 +0200)
Fix a severe AB/BA deadlock between the Common Clock Framework (CCF)
and the I2C adapter lock, which triggers when an I2C-controlled clock
generator client (like the Si5351) is registered or modified under the CCF.

During an i2c client clock (generator) frequency change, the CCF acquires its global
'prepare_lock' mutex and the driver calls i2c_transfer() to update the client's
chip registers, stalling for the adapter's I2C bus lock.

Concurrently, an independent, parallel transfer on the same bus (e.g., a GPIO
expander handling LEDs) can hold the I2C adapter lock. Inside this parallel
transfer path, jz4780_i2c_set_speed() calls clk_get_rate() on the host
controller's input clock to calculate bus timings. This call attempts to acquire
the blocked CCF 'prepare_lock', creating a circular dependency that freezes
the system.

The jz4780 host controller clock itself is static and never changes at runtime.

However, calling clk_get_rate() inside the active transfer path introduces
an unnecessary dependency on the CCF internal locks.

Eliminate this synchronous clk_get_rate() call from the active transfer
path by caching the static host peripheral clock rate once - inside the private
jz4780_i2c structure during jz4780_i2c_probe(). Update jz4780_i2c_set_speed()
to use this cached value, safely decoupling active I2C transactions from the
CCF internal locks without any risk of stale timings.

Assisted-by web based Google AI (pinpointing the bug and writing the message).

Fixes: ba92222ed63a12 ("i2c: jz4780: Add i2c bus controller driver for Ingenic JZ4780")
Signed-off-by: H. Nikolaus Schaller <hns@goldelico.com>
Cc: <stable@vger.kernel.org> # v4.1+
Signed-off-by: Andi Shyti <andi.shyti@kernel.org>
Link: https://lore.kernel.org/r/2db6fd233aceb7238474e4833f4d25ca681c3ffb.1784492382.git.hns@goldelico.com
drivers/i2c/busses/i2c-jz4780.c

index 664a5471d9335781ee5f361add84380ebb35b012..695be3b21460856c544a31c6834a2376784dcdbd 100644 (file)
@@ -141,6 +141,7 @@ struct jz4780_i2c {
        void __iomem            *iomem;
        int                      irq;
        struct clk              *clk;
+       unsigned long            clk_rate_khz;
        struct i2c_adapter       adap;
        const struct ingenic_i2c_config *cdata;
 
@@ -246,7 +247,7 @@ static int jz4780_i2c_set_target(struct jz4780_i2c *i2c, unsigned char address)
 
 static int jz4780_i2c_set_speed(struct jz4780_i2c *i2c)
 {
-       int dev_clk_khz = clk_get_rate(i2c->clk) / 1000;
+       int dev_clk_khz = i2c->clk_rate_khz;
        int cnt_high = 0;       /* HIGH period count of the SCL clock */
        int cnt_low = 0;        /* LOW period count of the SCL clock */
        int cnt_period = 0;     /* period count of the SCL clock */
@@ -796,6 +797,8 @@ static int jz4780_i2c_probe(struct platform_device *pdev)
        if (IS_ERR(i2c->clk))
                return PTR_ERR(i2c->clk);
 
+       i2c->clk_rate_khz = clk_get_rate(i2c->clk) / 1000;
+
        ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
                                   &clk_freq);
        if (ret) {