linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Romain Gantois <romain.gantois@bootlin.com>
To: Wolfram Sang <wsa+renesas@sang-engineering.com>,
	 Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>,
	 Luca Ceresoli <luca.ceresoli@bootlin.com>,
	 Andi Shyti <andi.shyti@kernel.org>,
	Rob Herring <robh@kernel.org>,
	 Krzysztof Kozlowski <krzk+dt@kernel.org>,
	 Conor Dooley <conor+dt@kernel.org>,
	Derek Kiernan <derek.kiernan@amd.com>,
	 Dragan Cvetic <dragan.cvetic@amd.com>,
	Arnd Bergmann <arnd@arndb.de>,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 Mauro Carvalho Chehab <mchehab@kernel.org>,
	 Linus Walleij <linus.walleij@linaro.org>,
	 Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
	 Kory Maincent <kory.maincent@bootlin.com>,
	linux-i2c@vger.kernel.org,  linux-kernel@vger.kernel.org,
	devicetree@vger.kernel.org,  linux-media@vger.kernel.org,
	linux-gpio@vger.kernel.org,
	 Romain Gantois <romain.gantois@bootlin.com>
Subject: [PATCH v7 3/9] media: i2c: ds90ub960: Protect alias_use_mask with a mutex
Date: Tue, 04 Feb 2025 10:29:14 +0100	[thread overview]
Message-ID: <20250204-fpc202-v7-3-78b4b8a35cf1@bootlin.com> (raw)
In-Reply-To: <20250204-fpc202-v7-0-78b4b8a35cf1@bootlin.com>

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.

Signed-off-by: Romain Gantois <romain.gantois@bootlin.com>
---
 drivers/media/i2c/ds90ub960.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/media/i2c/ds90ub960.c b/drivers/media/i2c/ds90ub960.c
index e1d53ef087538f7df2c992612e70ce6a3e24906b..5969692480409a1632ac05e43c58df479982bdd5 100644
--- a/drivers/media/i2c/ds90ub960.c
+++ b/drivers/media/i2c/ds90ub960.c
@@ -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;
@@ -3236,6 +3243,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;
 	}
@@ -3456,6 +3465,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:

-- 
2.47.1


  parent reply	other threads:[~2025-02-04  9:29 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-04  9:29 [PATCH v7 0/9] misc: Support TI FPC202 dual-port controller Romain Gantois
2025-02-04  9:29 ` [PATCH v7 1/9] dt-bindings: misc: Describe TI FPC202 dual port controller Romain Gantois
2025-02-04  9:29 ` [PATCH v7 2/9] media: i2c: ds90ub960: Replace aliased clients list with address list Romain Gantois
2025-02-04  9:29 ` Romain Gantois [this message]
2025-02-04  9:29 ` [PATCH v7 4/9] i2c: use client addresses directly in ATR interface Romain Gantois
2025-02-04  9:29 ` [PATCH v7 5/9] i2c: move ATR alias pool to a separate struct Romain Gantois
2025-02-04  9:29 ` [PATCH v7 6/9] i2c: rename field 'alias_list' of struct i2c_atr_chan to 'alias_pairs' Romain Gantois
2025-02-04  9:29 ` [PATCH v7 7/9] i2c: support per-channel ATR alias pools Romain Gantois
2025-02-04  9:29 ` [PATCH v7 8/9] i2c: Support dynamic address translation Romain Gantois
2025-02-04  9:29 ` [PATCH v7 9/9] misc: add FPC202 dual port controller driver Romain Gantois
2025-02-20 11:57   ` Greg Kroah-Hartman
2025-02-20 12:09     ` Wolfram Sang
2025-02-20 12:13       ` Greg Kroah-Hartman
2025-02-20 12:51       ` Romain Gantois
2025-02-07 12:02 ` [PATCH v7 0/9] misc: Support TI FPC202 dual-port controller Tomi Valkeinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250204-fpc202-v7-3-78b4b8a35cf1@bootlin.com \
    --to=romain.gantois@bootlin.com \
    --cc=andi.shyti@kernel.org \
    --cc=arnd@arndb.de \
    --cc=brgl@bgdev.pl \
    --cc=conor+dt@kernel.org \
    --cc=derek.kiernan@amd.com \
    --cc=devicetree@vger.kernel.org \
    --cc=dragan.cvetic@amd.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=kory.maincent@bootlin.com \
    --cc=krzk+dt@kernel.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=luca.ceresoli@bootlin.com \
    --cc=mchehab@kernel.org \
    --cc=robh@kernel.org \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=tomi.valkeinen@ideasonboard.com \
    --cc=wsa+renesas@sang-engineering.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).