]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
media: i2c: ds90ub960: Protect alias_use_mask with a mutex
authorRomain Gantois <romain.gantois@bootlin.com>
Thu, 6 Mar 2025 16:23:24 +0000 (17:23 +0100)
committerWolfram Sang <wsa+renesas@sang-engineering.com>
Fri, 18 Apr 2025 21:33:25 +0000 (23:33 +0200)
The aliased_addrs list represents the occupation of an RX port's hardware
alias table. This list and the underlying hardware table are only accessed
in the attach/detach_client() callbacks.

These functions are only called from a bus notifier handler in i2c-atr.c,
which is always called with the notifier chain's semaphore held. This
indirectly prevents concurrent access to the aliased_addrs list.
However, more explicit and direct locking is preferable. Moreover, with the
introduction of dynamic address translation in a future patch, the
attach/detach_client() callbacks will be called from outside of the
notifier chain's read section.

Introduce a mutex to protect access to the aliased_addrs list and its
underlying hardware table.

Tested-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Signed-off-by: Romain Gantois <romain.gantois@bootlin.com>
Acked-by: Andi Shyti <andi.shyti@kernel.org>
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
drivers/media/i2c/ds90ub960.c

index fa06ce38e35f123277ad6cb356a3668bb0fc4820..fc5c43718ca46349854f9724848fdc8961e4eecf 100644 (file)
@@ -27,6 +27,7 @@
  */
 
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/fwnode.h>
@@ -478,6 +479,8 @@ struct ub960_rxport {
                };
        } eq;
 
+       /* lock for aliased_addrs and associated registers */
+       struct mutex aliased_addrs_lock;
        u16 aliased_addrs[UB960_MAX_PORT_ALIASES];
 };
 
@@ -1054,6 +1057,8 @@ static int ub960_atr_attach_client(struct i2c_atr *atr, u32 chan_id,
        struct device *dev = &priv->client->dev;
        unsigned int reg_idx;
 
+       guard(mutex)(&rxport->aliased_addrs_lock);
+
        for (reg_idx = 0; reg_idx < ARRAY_SIZE(rxport->aliased_addrs); reg_idx++) {
                if (!rxport->aliased_addrs[reg_idx])
                        break;
@@ -1085,6 +1090,8 @@ static void ub960_atr_detach_client(struct i2c_atr *atr, u32 chan_id,
        struct device *dev = &priv->client->dev;
        unsigned int reg_idx;
 
+       guard(mutex)(&rxport->aliased_addrs_lock);
+
        for (reg_idx = 0; reg_idx < ARRAY_SIZE(rxport->aliased_addrs); reg_idx++) {
                if (rxport->aliased_addrs[reg_idx] == client->addr)
                        break;
@@ -3235,6 +3242,8 @@ static void ub960_rxport_free_ports(struct ub960_data *priv)
                fwnode_handle_put(rxport->source.ep_fwnode);
                fwnode_handle_put(rxport->ser.fwnode);
 
+               mutex_destroy(&rxport->aliased_addrs_lock);
+
                kfree(rxport);
                priv->rxports[nport] = NULL;
        }
@@ -3455,6 +3464,8 @@ static int ub960_parse_dt_rxport(struct ub960_data *priv, unsigned int nport,
        if (ret)
                goto err_put_remote_fwnode;
 
+       mutex_init(&rxport->aliased_addrs_lock);
+
        return 0;
 
 err_put_remote_fwnode: