Skip to contentSkip to conversation

net: dsa: microchip: Do not reinit mutexes on KSZ87xx

PATCH Applied Marek Vasut 2 participants 7 messages Oct 10, 2019 – Oct 13, 2019

Subsystems (4)

v1 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 net: dsa: microchip: Do not reinit mutexes on KSZ87xx

The KSZ87xx driver calls mutex_init() on mutexes already inited in ksz_common.c ksz_switch_register(). Do not do it twice, drop the reinitialization.

Cc Andrew Lunn <[email protected]>
Cc David S. Miller <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc George McCollister <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>
---
 drivers/net/dsa/microchip/ksz8795.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/net/dsa/microchip/ksz8795.c +0 -4
index a23d3ffdf0c4..24a5e99f7fd5 100644
@@ -1224,10 +1224,6 @@ static int ksz8795_switch_init(struct ksz_device *dev)
 {
 	int i;
 
-	mutex_init(&dev->stats_mutex);
-	mutex_init(&dev->alu_mutex);
-	mutex_init(&dev->vlan_mutex);
-
 	dev->ds->ops = &ksz8795_switch_ops;
 
 	for (i = 0; i < ARRAY_SIZE(ksz8795_switch_chips); i++) {
Signature · 1 line
2.23.0
#1 lore raw

PATCH 2/2 net: dsa: microchip: Add shared regmap mutex

The KSZ driver uses one regmap per register width (8/16/32), each with it's own lock, but accessing the same set of registers. In theory, it is possible to create a race condition between these regmaps, although the underlying bus (SPI or I2C) locking should assure nothing bad will really happen and the accesses would be correct.

To make the driver do the right thing, add one single shared mutex for all the regmaps used by the driver instead. This assures that even if some future hardware is on a bus which does not serialize the accesses the same way SPI or I2C does, nothing bad will happen.

Note that the status_mutex was unused and only initied, hence it was renamed and repurposed as the regmap mutex.

Cc Andrew Lunn <[email protected]>
Cc David S. Miller <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc George McCollister <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>
---
 drivers/net/dsa/microchip/ksz8795_spi.c |  7 ++++---
 drivers/net/dsa/microchip/ksz9477_i2c.c |  6 ++++--
 drivers/net/dsa/microchip/ksz9477_spi.c |  6 ++++--
 drivers/net/dsa/microchip/ksz_common.c  | 14 +++++++++++++-
 drivers/net/dsa/microchip/ksz_common.h  |  7 ++++++-
 5 files changed, 31 insertions(+), 9 deletions(-)
drivers/net/dsa/microchip/ksz8795_spi.c +4 -3
index d0f8153e86b7..614404c40cba 100644
@@ -26,6 +26,7 @@ KSZ_REGMAP_TABLE(ksz8795, 16, SPI_ADDR_SHIFT,
 static int ksz8795_spi_probe(struct spi_device *spi)
 {
 	struct ksz_device *dev;
+	struct regmap_config rc;
 	int i, ret;
 
 	dev = ksz_switch_alloc(&spi->dev, spi);
@@ -33,9 +34,9 @@ static int ksz8795_spi_probe(struct spi_device *spi)
 		return -ENOMEM;
 
 	for (i = 0; i < ARRAY_SIZE(ksz8795_regmap_config); i++) {
-		dev->regmap[i] = devm_regmap_init_spi(spi,
-						      &ksz8795_regmap_config
-						      [i]);
+		rc = ksz8795_regmap_config[i];
+		rc.lock_arg = &dev->regmap_mutex;
+		dev->regmap[i] = devm_regmap_init_spi(spi, &rc);
 		if (IS_ERR(dev->regmap[i])) {
 			ret = PTR_ERR(dev->regmap[i]);
 			dev_err(&spi->dev,
drivers/net/dsa/microchip/ksz9477_i2c.c +4 -2
index b0a1595d780d..2cb178675b83 100644
@@ -18,6 +18,7 @@ static int ksz9477_i2c_probe(struct i2c_client *i2c,
 			     const struct i2c_device_id *i2c_id)
 {
 	struct ksz_device *dev;
+	struct regmap_config rc;
 	int i, ret;
 
 	dev = ksz_switch_alloc(&i2c->dev, i2c);
@@ -25,8 +26,9 @@ static int ksz9477_i2c_probe(struct i2c_client *i2c,
 		return -ENOMEM;
 
 	for (i = 0; i < ARRAY_SIZE(ksz9477_regmap_config); i++) {
-		dev->regmap[i] = devm_regmap_init_i2c(i2c,
-					&ksz9477_regmap_config[i]);
+		rc = ksz9477_regmap_config[i];
+		rc.lock_arg = &dev->regmap_mutex;
+		dev->regmap[i] = devm_regmap_init_i2c(i2c, &rc);
 		if (IS_ERR(dev->regmap[i])) {
 			ret = PTR_ERR(dev->regmap[i]);
 			dev_err(&i2c->dev,
drivers/net/dsa/microchip/ksz9477_spi.c +4 -2
index f4198d6f72be..a39d2373eb3a 100644
@@ -25,6 +25,7 @@ KSZ_REGMAP_TABLE(ksz9477, 32, SPI_ADDR_SHIFT,
 static int ksz9477_spi_probe(struct spi_device *spi)
 {
 	struct ksz_device *dev;
+	struct regmap_config rc;
 	int i, ret;
 
 	dev = ksz_switch_alloc(&spi->dev, spi);
@@ -32,8 +33,9 @@ static int ksz9477_spi_probe(struct spi_device *spi)
 		return -ENOMEM;
 
 	for (i = 0; i < ARRAY_SIZE(ksz9477_regmap_config); i++) {
-		dev->regmap[i] = devm_regmap_init_spi(spi,
-					&ksz9477_regmap_config[i]);
+		rc = ksz9477_regmap_config[i];
+		rc.lock_arg = &dev->regmap_mutex;
+		dev->regmap[i] = devm_regmap_init_spi(spi, &rc);
 		if (IS_ERR(dev->regmap[i])) {
 			ret = PTR_ERR(dev->regmap[i]);
 			dev_err(&spi->dev,
drivers/net/dsa/microchip/ksz_common.c +13 -1
index b0b870f0c252..21e483f6e786 100644
@@ -416,6 +416,18 @@ struct ksz_device *ksz_switch_alloc(struct device *base, void *priv)
 }
 EXPORT_SYMBOL(ksz_switch_alloc);
 
+void ksz_regmap_lock(void *__mtx)
+{
+	struct mutex *mtx = __mtx;
+	mutex_lock(mtx);
+}
+
+void ksz_regmap_unlock(void *__mtx)
+{
+	struct mutex *mtx = __mtx;
+	mutex_unlock(mtx);
+}
+
 int ksz_switch_register(struct ksz_device *dev,
 			const struct ksz_dev_ops *ops)
 {
@@ -436,7 +448,7 @@ int ksz_switch_register(struct ksz_device *dev,
 	}
 
 	mutex_init(&dev->dev_mutex);
-	mutex_init(&dev->stats_mutex);
+	mutex_init(&dev->regmap_mutex);
 	mutex_init(&dev->alu_mutex);
 	mutex_init(&dev->vlan_mutex);
 
drivers/net/dsa/microchip/ksz_common.h +6 -1
index dd60d0837fc6..c21410353ea8 100644
@@ -47,7 +47,7 @@ struct ksz_device {
 	const char *name;
 
 	struct mutex dev_mutex;		/* device access */
-	struct mutex stats_mutex;	/* status access */
+	struct mutex regmap_mutex;	/* regmap access */
 	struct mutex alu_mutex;		/* ALU access */
 	struct mutex vlan_mutex;	/* vlan access */
 	const struct ksz_dev_ops *dev_ops;
@@ -145,6 +145,9 @@ struct ksz_dev_ops {
 	void (*exit)(struct ksz_device *dev);
 };
 
+void ksz_regmap_lock(void *__mtx);
+void ksz_regmap_unlock(void *__mtx);
+
 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
 int ksz_switch_register(struct ksz_device *dev,
 			const struct ksz_dev_ops *ops);
@@ -314,6 +317,8 @@ static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
 		.write_flag_mask =					\
 			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp,	\
 					     regbits, regpad),		\
+		.lock = ksz_regmap_lock,				\
+		.unlock = ksz_regmap_unlock,				\
 		.reg_format_endian = REGMAP_ENDIAN_BIG,			\
 		.val_format_endian = REGMAP_ENDIAN_BIG			\
 	}
Signature · 1 line
2.23.0
maintainer Applied
#2 lore raw

Re: [PATCH 2/2] net: dsa: microchip: Add shared regmap mutex

In reply to Marek Vasut

From: Marek Vasut <[email protected]>
Date: Thu, 10 Oct 2019 20:25:08 +0200

Quoted 11 lines

The KSZ driver uses one regmap per register width (8/16/32), each with it's own lock, but accessing the same set of registers. In theory, it is possible to create a race condition between these regmaps, although the underlying bus (SPI or I2C) locking should assure nothing bad will really happen and the accesses would be correct.

To make the driver do the right thing, add one single shared mutex for all the regmaps used by the driver instead. This assures that even if some future hardware is on a bus which does not serialize the accesses the same way SPI or I2C does, nothing bad will happen.

Quoted 4 lines

Note that the status_mutex was unused and only initied, hence it was renamed and repurposed as the regmap mutex.

Applied.

maintainer
#3 lore raw

Re: [PATCH 2/2] net: dsa: microchip: Add shared regmap mutex

From: David Miller <[email protected]>
Date: Sat, 12 Oct 2019 17:20:55 -0700 (PDT)

Quoted 14 lines

From: Marek Vasut <[email protected]>
Date: Thu, 10 Oct 2019 20:25:08 +0200

Quoted 11 lines

The KSZ driver uses one regmap per register width (8/16/32), each with it's own lock, but accessing the same set of registers. In theory, it is possible to create a race condition between these regmaps, although the underlying bus (SPI or I2C) locking should assure nothing bad will really happen and the accesses would be correct.

To make the driver do the right thing, add one single shared mutex for all the regmaps used by the driver instead. This assures that even if some future hardware is on a bus which does not serialize the accesses the same way SPI or I2C does, nothing bad will happen.

Quoted 6 lines
Quoted 4 lines

Note that the status_mutex was unused and only initied, hence it was renamed and repurposed as the regmap mutex.

Applied.

Actually, both patches reverted. Please test your changes properly:

ERROR: "ksz_regmap_unlock" [drivers/net/dsa/microchip/ksz8795_spi.ko] undefined!
ERROR: "ksz_regmap_lock" [drivers/net/dsa/microchip/ksz8795_spi.ko] undefined!
ERROR: "ksz_regmap_unlock" [drivers/net/dsa/microchip/ksz9477_spi.ko] undefined!
ERROR: "ksz_regmap_lock" [drivers/net/dsa/microchip/ksz9477_spi.ko] undefined!
ERROR: "ksz_regmap_unlock" [drivers/net/dsa/microchip/ksz9477_i2c.ko] undefined!
ERROR: "ksz_regmap_lock" [drivers/net/dsa/microchip/ksz9477_i2c.ko] undefined!

#4 lore raw

Re: [PATCH 2/2] net: dsa: microchip: Add shared regmap mutex

Quoted 24 lines · David Miller

From: David Miller <[email protected]>
Date: Sat, 12 Oct 2019 17:20:55 -0700 (PDT)

Quoted 14 lines

From: Marek Vasut <[email protected]>
Date: Thu, 10 Oct 2019 20:25:08 +0200

Quoted 11 lines

The KSZ driver uses one regmap per register width (8/16/32), each with it's own lock, but accessing the same set of registers. In theory, it is possible to create a race condition between these regmaps, although the underlying bus (SPI or I2C) locking should assure nothing bad will really happen and the accesses would be correct.

To make the driver do the right thing, add one single shared mutex for all the regmaps used by the driver instead. This assures that even if some future hardware is on a bus which does not serialize the accesses the same way SPI or I2C does, nothing bad will happen.

Quoted 6 lines
Quoted 4 lines

Note that the status_mutex was unused and only initied, hence it was renamed and repurposed as the regmap mutex.

Applied.

Quoted 8 lines

Actually, both patches reverted. Please test your changes properly:

ERROR: "ksz_regmap_unlock" [drivers/net/dsa/microchip/ksz8795_spi.ko] undefined!
ERROR: "ksz_regmap_lock" [drivers/net/dsa/microchip/ksz8795_spi.ko] undefined!
ERROR: "ksz_regmap_unlock" [drivers/net/dsa/microchip/ksz9477_spi.ko] undefined!
ERROR: "ksz_regmap_lock" [drivers/net/dsa/microchip/ksz9477_spi.ko] undefined!
ERROR: "ksz_regmap_unlock" [drivers/net/dsa/microchip/ksz9477_i2c.ko] undefined!
ERROR: "ksz_regmap_lock" [drivers/net/dsa/microchip/ksz9477_i2c.ko] undefined!

So the test is to compile it as a module ?

Signature · 2 lines
Best regards,
Marek Vasut
maintainer
#5 lore raw

Re: [PATCH 2/2] net: dsa: microchip: Add shared regmap mutex

From: Marek Vasut <[email protected]>
Date: Sun, 13 Oct 2019 12:50:15 +0200

Quoted 27 lines · David Miller
Quoted 26 lines

From: David Miller <[email protected]>
Date: Sat, 12 Oct 2019 17:20:55 -0700 (PDT)

Quoted 14 lines

From: Marek Vasut <[email protected]>
Date: Thu, 10 Oct 2019 20:25:08 +0200

Quoted 11 lines

The KSZ driver uses one regmap per register width (8/16/32), each with it's own lock, but accessing the same set of registers. In theory, it is possible to create a race condition between these regmaps, although the underlying bus (SPI or I2C) locking should assure nothing bad will really happen and the accesses would be correct.

To make the driver do the right thing, add one single shared mutex for all the regmaps used by the driver instead. This assures that even if some future hardware is on a bus which does not serialize the accesses the same way SPI or I2C does, nothing bad will happen.

Quoted 6 lines
Quoted 4 lines

Note that the status_mutex was unused and only initied, hence it was renamed and repurposed as the regmap mutex.

Applied.

Actually, both patches reverted. Please test your changes properly:

Quoted 8 lines
Quoted 6 lines

ERROR: "ksz_regmap_unlock" [drivers/net/dsa/microchip/ksz8795_spi.ko] undefined!
ERROR: "ksz_regmap_lock" [drivers/net/dsa/microchip/ksz8795_spi.ko] undefined!
ERROR: "ksz_regmap_unlock" [drivers/net/dsa/microchip/ksz9477_spi.ko] undefined!
ERROR: "ksz_regmap_lock" [drivers/net/dsa/microchip/ksz9477_spi.ko] undefined!
ERROR: "ksz_regmap_unlock" [drivers/net/dsa/microchip/ksz9477_i2c.ko] undefined!
ERROR: "ksz_regmap_lock" [drivers/net/dsa/microchip/ksz9477_i2c.ko] undefined!

So the test is to compile it as a module ?

The test is to compile it in all relevant possible configurations.

As a module, statically, and with dependent modules both static and modular as is relevant.