Skip to contentSkip to conversation

mfd: intel_quark_i2c_gpio: Don't play dirty trick with const

PATCH Superseded Andy Shevchenko 4 participants 4 messages Mar 25, 2021 – Mar 26, 2021 Multifunction devices (MFD)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mfd: intel_quark_i2c_gpio: Don't play dirty trick with const

As Linus rightfully noticed, the driver plays dirty trick with const, i.e. it assigns a place holder data structure to the const field in the MFD cell and then drops the const by explicit casting. This is not how it should be.

Replace cell parameter by bar and assign local pointer res to the respective non-const place holder in the intel_quark_i2c_setup() and intel_quark_gpio_setup().

---
 drivers/mfd/intel_quark_i2c_gpio.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)
drivers/mfd/intel_quark_i2c_gpio.c +12 -14
index 7f90e6f022ba..f2bce2ade510 100644
@@ -157,17 +157,16 @@ static void intel_quark_unregister_i2c_clk(struct device *dev)
 	clk_unregister(quark_mfd->i2c_clk);
 }
 
-static int intel_quark_i2c_setup(struct pci_dev *pdev, struct mfd_cell *cell)
+static int intel_quark_i2c_setup(struct pci_dev *pdev, int bar)
 {
+	struct mfd_cell *cell = &intel_quark_mfd_cells[bar];
+	struct resource *res = intel_quark_i2c_res;
 	const struct dmi_system_id *dmi_id;
 	struct dw_i2c_platform_data *pdata;
-	struct resource *res = (struct resource *)cell->resources;
 	struct device *dev = &pdev->dev;
 
-	res[INTEL_QUARK_IORES_MEM].start =
-		pci_resource_start(pdev, MFD_I2C_BAR);
-	res[INTEL_QUARK_IORES_MEM].end =
-		pci_resource_end(pdev, MFD_I2C_BAR);
+	res[INTEL_QUARK_IORES_MEM].start = pci_resource_start(pdev, bar);
+	res[INTEL_QUARK_IORES_MEM].end = pci_resource_end(pdev, bar);
 
 	res[INTEL_QUARK_IORES_IRQ].start = pci_irq_vector(pdev, 0);
 	res[INTEL_QUARK_IORES_IRQ].end = pci_irq_vector(pdev, 0);
@@ -189,16 +188,15 @@ static int intel_quark_i2c_setup(struct pci_dev *pdev, struct mfd_cell *cell)
 	return 0;
 }
 
-static int intel_quark_gpio_setup(struct pci_dev *pdev, struct mfd_cell *cell)
+static int intel_quark_gpio_setup(struct pci_dev *pdev, int bar)
 {
+	struct mfd_cell *cell = &intel_quark_mfd_cells[bar];
+	struct resource *res = intel_quark_gpio_res;
 	struct dwapb_platform_data *pdata;
-	struct resource *res = (struct resource *)cell->resources;
 	struct device *dev = &pdev->dev;
 
-	res[INTEL_QUARK_IORES_MEM].start =
-		pci_resource_start(pdev, MFD_GPIO_BAR);
-	res[INTEL_QUARK_IORES_MEM].end =
-		pci_resource_end(pdev, MFD_GPIO_BAR);
+	res[INTEL_QUARK_IORES_MEM].start = pci_resource_start(pdev, bar);
+	res[INTEL_QUARK_IORES_MEM].end = pci_resource_end(pdev, bar);
 
 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
 	if (!pdata)
@@ -252,11 +250,11 @@ static int intel_quark_mfd_probe(struct pci_dev *pdev,
 	if (ret < 0)
 		goto err_unregister_i2c_clk;
 
-	ret = intel_quark_i2c_setup(pdev, &intel_quark_mfd_cells[MFD_I2C_BAR]);
+	ret = intel_quark_i2c_setup(pdev, MFD_I2C_BAR);
 	if (ret)
 		goto err_free_irq_vectors;
 
-	ret = intel_quark_gpio_setup(pdev, &intel_quark_mfd_cells[MFD_GPIO_BAR]);
+	ret = intel_quark_gpio_setup(pdev, MFD_GPIO_BAR);
 	if (ret)
 		goto err_free_irq_vectors;
 
Signature · 1 line
2.30.2
#1 lore raw
Quoted 4 lines · Andy Shevchenko

Replace cell parameter by bar and assign local pointer res to the respective non-const place holder in the intel_quark_i2c_setup() and intel_quark_gpio_setup().

Thanks. I assume/hope this got tested on hardware too? It looks ObviouslyCorrect(tm), but that's never stopped bugs before ;)

Linus

#2 lore raw
Quoted 9 lines · Linus Torvalds
Quoted 4 lines · Andy Shevchenko

Replace cell parameter by bar and assign local pointer res to the respective non-const place holder in the intel_quark_i2c_setup() and intel_quark_gpio_setup().

Thanks. I assume/hope this got tested on hardware too?

Yes. On exact hardware this driver is for.

Quoted 2 lines

It looks
ObviouslyCorrect(tm), but that's never stopped bugs before ;)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko

As Linus rightfully noticed, the driver plays dirty trick with const, i.e. it assigns a place holder data structure to the const field in the MFD cell and then drops the const by explicit casting. This is not how it should be.

Replace cell parameter by bar and assign local pointer res to the respective non-const place holder in the intel_quark_i2c_setup() and intel_quark_gpio_setup().

Thanks for spotting this Linus.

Quoted 10 lines
---
 drivers/mfd/intel_quark_i2c_gpio.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)
drivers/mfd/intel_quark_i2c_gpio.c +0 -0
index 7f90e6f022ba..f2bce2ade510 100644

drivers/mfd/intel_quark_i2c_gpio.c static void intel_quark_unregister_i2c_clk(struct device *dev) · line 162

Quoted 7 lines
drivers/mfd/intel_quark_i2c_gpio.c +2 -1
@@ -157,17 +157,16 @@ static void intel_quark_unregister_i2c_clk(struct device *dev)
 	clk_unregister(quark_mfd->i2c_clk);
 }
 
-static int intel_quark_i2c_setup(struct pci_dev *pdev, struct mfd_cell *cell)
+static int intel_quark_i2c_setup(struct pci_dev *pdev, int bar)
 {
+	struct mfd_cell *cell = &intel_quark_mfd_cells[bar];

What's stopping you from removing the seemingly superfluous 'bar' parameter and simply doing:

	struct mfd_cell *cell = &intel_quark_mfd_cells[MFD_I2C_BAR];	  

drivers/mfd/intel_quark_i2c_gpio.c static void intel_quark_unregister_i2c_clk(struct device *dev) · line 169

Quoted 12 lines
drivers/mfd/intel_quark_i2c_gpio.c +3 -5
+	struct resource *res = intel_quark_i2c_res;
 	const struct dmi_system_id *dmi_id;
 	struct dw_i2c_platform_data *pdata;
-	struct resource *res = (struct resource *)cell->resources;
 	struct device *dev = &pdev->dev;
 
-	res[INTEL_QUARK_IORES_MEM].start =
-		pci_resource_start(pdev, MFD_I2C_BAR);
-	res[INTEL_QUARK_IORES_MEM].end =
-		pci_resource_end(pdev, MFD_I2C_BAR);
+	res[INTEL_QUARK_IORES_MEM].start = pci_resource_start(pdev, bar);
+	res[INTEL_QUARK_IORES_MEM].end = pci_resource_end(pdev, bar);

If you do end up using 'MFD_I2C_BAR' again in here, please omit the line wrap. 80-char lines are so 1990s!

Quoted 5 lines
drivers/mfd/intel_quark_i2c_gpio.c +0 -0
 	res[INTEL_QUARK_IORES_IRQ].start = pci_irq_vector(pdev, 0);
 	res[INTEL_QUARK_IORES_IRQ].end = pci_irq_vector(pdev, 0);
@@ -189,16 +188,15 @@ static int intel_quark_i2c_setup(struct pci_dev *pdev, struct mfd_cell *cell)
 	return 0;
 }
Signature · 4 lines
Lee Jones [李琼斯]
Senior Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog