Skip to contentSkip to conversation

mfd: viperboard: cleanups and fix for cache line sharing

PATCH Superseded Octavian Purdila 3 participants 13 messages Sep 22, 2014 – Sep 24, 2014 Multifunction devices (MFD)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover mfd: viperboard: cleanups and fix for cache line sharing

Changes since v1:

* split cache sharing fix and cleanups into separate patches

Octavian Purdila (2):
mfd: viperboard: allocate I/O buffer separately
mfd: viperboard: remove redundant OOM message and NULL pointer check

 drivers/mfd/viperboard.c       | 16 ++++++++++------
 include/linux/mfd/viperboard.h |  2 +-
 2 files changed, 11 insertions(+), 7 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 1/2 mfd: viperboard: allocate I/O buffer separately

Currently the I/O buffer is allocated part of the device status structure, potentially sharing the same cache line with other members in this structure.

Allocate the buffer separately, to avoid the I/O operations corrupting the device status structure due to cache line sharing.

Compiled tested only as I don't have access to hardware.

---
 drivers/mfd/viperboard.c       | 8 ++++++++
 include/linux/mfd/viperboard.h | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/mfd/viperboard.c +8 -0
index e00f534..5f62f4e 100644
@@ -64,6 +64,12 @@ static int vprbrd_probe(struct usb_interface *interface,
 		return -ENOMEM;
 	}
 
+	vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);
+	if (vb->buf == NULL) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
 	mutex_init(&vb->lock);
 
 	vb->usb_dev = usb_get_dev(interface_to_usbdev(interface));
@@ -105,6 +111,7 @@ static int vprbrd_probe(struct usb_interface *interface,
 error:
 	if (vb) {
 		usb_put_dev(vb->usb_dev);
+		kfree(vb->buf);
 		kfree(vb);
 	}
 
@@ -118,6 +125,7 @@ static void vprbrd_disconnect(struct usb_interface *interface)
 	mfd_remove_devices(&interface->dev);
 	usb_set_intfdata(interface, NULL);
 	usb_put_dev(vb->usb_dev);
+	kfree(vb->buf);
 	kfree(vb);
 
 	dev_dbg(&interface->dev, "disconnected\n");
include/linux/mfd/viperboard.h +1 -1
index 1934528..af928d0 100644
@@ -103,7 +103,7 @@ struct vprbrd_i2c_addr_msg {
 struct vprbrd {
 	struct usb_device *usb_dev; /* the usb device for this device */
 	struct mutex lock;
-	u8 buf[sizeof(struct vprbrd_i2c_write_msg)];
+	u8 *buf;
 	struct platform_device pdev;
 };
 
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH v2 1/2] mfd: viperboard: allocate I/O buffer separately

Quoted 20 lines · Octavian Purdila

Currently the I/O buffer is allocated part of the device status structure, potentially sharing the same cache line with other members in this structure.

Allocate the buffer separately, to avoid the I/O operations corrupting the device status structure due to cache line sharing.

Compiled tested only as I don't have access to hardware.

---
 drivers/mfd/viperboard.c       | 8 ++++++++
 include/linux/mfd/viperboard.h | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/mfd/viperboard.c +0 -0
index e00f534..5f62f4e 100644

drivers/mfd/viperboard.c static int vprbrd_probe(struct usb_interface *interface, · line 70

Quoted 7 lines
drivers/mfd/viperboard.c +4 -0
@@ -64,6 +64,12 @@ static int vprbrd_probe(struct usb_interface *interface,
 		return -ENOMEM;
 	}
 
+	vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);
+	if (vb->buf == NULL) {
+		ret = -ENOMEM;
+		goto error;

This will cause a kref imbalance as you have a usb_put_dev in error, but haven't done the get yet.

drivers/mfd/viperboard.c static int vprbrd_probe(struct usb_interface *interface, · line 75

Quoted 5 lines
drivers/mfd/viperboard.c +2 -0
+	}
+
 	mutex_init(&vb->lock);
 
 	vb->usb_dev = usb_get_dev(interface_to_usbdev(interface));

Here's the get.

Johan

#3 lore raw

Re: [PATCH v2 1/2] mfd: viperboard: allocate I/O buffer separately

Quoted 24 lines · Johan Hovold
Quoted 23 lines · Octavian Purdila

Currently the I/O buffer is allocated part of the device status structure, potentially sharing the same cache line with other members in this structure.

Allocate the buffer separately, to avoid the I/O operations corrupting the device status structure due to cache line sharing.

Compiled tested only as I don't have access to hardware.

---
 drivers/mfd/viperboard.c       | 8 ++++++++
 include/linux/mfd/viperboard.h | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/mfd/viperboard.c +0 -0
index e00f534..5f62f4e 100644
@@ -64,6 +64,12 @@ static int vprbrd_probe(struct usb_interface *interface,
 		return -ENOMEM;
 	}
 
Quoted 7 lines

drivers/mfd/viperboard.c static int vprbrd_probe(struct usb_interface *interface, · line 70

Quoted 4 lines
drivers/mfd/viperboard.c +4 -0
+	vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);
+	if (vb->buf == NULL) {
+		ret = -ENOMEM;
+		goto error;

This will cause a kref imbalance as you have a usb_put_dev in error, but haven't done the get yet.

Nevermind. This isn't problem as the usb device is null.

Haven't had my morning coffee yet. ;)

Johan

Reviewed-by on v2 1/2
#4 lore raw

Re: [PATCH v2 1/2] mfd: viperboard: allocate I/O buffer separately

In reply to Octavian Purdila
Quoted 10 lines · Octavian Purdila

Currently the I/O buffer is allocated part of the device status structure, potentially sharing the same cache line with other members in this structure.

Allocate the buffer separately, to avoid the I/O operations corrupting the device status structure due to cache line sharing.

Compiled tested only as I don't have access to hardware.

Quoted 50 lines
---
 drivers/mfd/viperboard.c       | 8 ++++++++
 include/linux/mfd/viperboard.h | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/mfd/viperboard.c +8 -0
index e00f534..5f62f4e 100644
@@ -64,6 +64,12 @@ static int vprbrd_probe(struct usb_interface *interface,
 		return -ENOMEM;
 	}
 
+	vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);
+	if (vb->buf == NULL) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
 	mutex_init(&vb->lock);
 
 	vb->usb_dev = usb_get_dev(interface_to_usbdev(interface));
@@ -105,6 +111,7 @@ static int vprbrd_probe(struct usb_interface *interface,
 error:
 	if (vb) {
 		usb_put_dev(vb->usb_dev);
+		kfree(vb->buf);
 		kfree(vb);
 	}
 
@@ -118,6 +125,7 @@ static void vprbrd_disconnect(struct usb_interface *interface)
 	mfd_remove_devices(&interface->dev);
 	usb_set_intfdata(interface, NULL);
 	usb_put_dev(vb->usb_dev);
+	kfree(vb->buf);
 	kfree(vb);
 
 	dev_dbg(&interface->dev, "disconnected\n");
include/linux/mfd/viperboard.h +1 -1
index 1934528..af928d0 100644
@@ -103,7 +103,7 @@ struct vprbrd_i2c_addr_msg {
 struct vprbrd {
 	struct usb_device *usb_dev; /* the usb device for this device */
 	struct mutex lock;
-	u8 buf[sizeof(struct vprbrd_i2c_write_msg)];
+	u8 *buf;
 	struct platform_device pdev;
 };
#5 lore raw

Re: [PATCH v2 1/2] mfd: viperboard: allocate I/O buffer separately

In reply to Octavian Purdila
Quoted 20 lines · Octavian Purdila

Currently the I/O buffer is allocated part of the device status structure, potentially sharing the same cache line with other members in this structure.

Allocate the buffer separately, to avoid the I/O operations corrupting the device status structure due to cache line sharing.

Compiled tested only as I don't have access to hardware.

---
 drivers/mfd/viperboard.c       | 8 ++++++++
 include/linux/mfd/viperboard.h | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/mfd/viperboard.c +0 -0
index e00f534..5f62f4e 100644

drivers/mfd/viperboard.c static int vprbrd_probe(struct usb_interface *interface, · line 67

Quoted 4 lines
drivers/mfd/viperboard.c +1 -0
@@ -64,6 +64,12 @@ static int vprbrd_probe(struct usb_interface *interface,
 		return -ENOMEM;
 	}
 
+	vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);

Can you obtain the 'struct device' first then use managed resources (devm_*)?

drivers/mfd/viperboard.c static int vprbrd_probe(struct usb_interface *interface, · line 114

Quoted 13 lines
drivers/mfd/viperboard.c +6 -0
+	if (vb->buf == NULL) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
 	mutex_init(&vb->lock);
 
 	vb->usb_dev = usb_get_dev(interface_to_usbdev(interface));
@@ -105,6 +111,7 @@ static int vprbrd_probe(struct usb_interface *interface,
 error:
 	if (vb) {
 		usb_put_dev(vb->usb_dev);
+		kfree(vb->buf);

Then you don't need this.

drivers/mfd/viperboard.c static void vprbrd_disconnect(struct usb_interface *interface) · line 128

Quoted 8 lines
drivers/mfd/viperboard.c +1 -0
 		kfree(vb);
 	}
 
@@ -118,6 +125,7 @@ static void vprbrd_disconnect(struct usb_interface *interface)
 	mfd_remove_devices(&interface->dev);
 	usb_set_intfdata(interface, NULL);
 	usb_put_dev(vb->usb_dev);
+	kfree(vb->buf);

Or this.

Quoted 16 lines
drivers/mfd/viperboard.c +0 -0
 	kfree(vb);
 
 	dev_dbg(&interface->dev, "disconnected\n");
include/linux/mfd/viperboard.h +1 -1
index 1934528..af928d0 100644
@@ -103,7 +103,7 @@ struct vprbrd_i2c_addr_msg {
 struct vprbrd {
 	struct usb_device *usb_dev; /* the usb device for this device */
 	struct mutex lock;
-	u8 buf[sizeof(struct vprbrd_i2c_write_msg)];
+	u8 *buf;
 	struct platform_device pdev;
 };
 
Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#6 lore raw

Re: [PATCH v2 1/2] mfd: viperboard: allocate I/O buffer separately

Quoted 25 lines · Lee Jones
Quoted 23 lines · Octavian Purdila

Currently the I/O buffer is allocated part of the device status structure, potentially sharing the same cache line with other members in this structure.

Allocate the buffer separately, to avoid the I/O operations corrupting the device status structure due to cache line sharing.

Compiled tested only as I don't have access to hardware.

---
 drivers/mfd/viperboard.c       | 8 ++++++++
 include/linux/mfd/viperboard.h | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/mfd/viperboard.c +0 -0
index e00f534..5f62f4e 100644
@@ -64,6 +64,12 @@ static int vprbrd_probe(struct usb_interface *interface,
 		return -ENOMEM;
 	}
 
Quoted 4 lines

drivers/mfd/viperboard.c static int vprbrd_probe(struct usb_interface *interface, · line 67

Quoted 1 line
drivers/mfd/viperboard.c +1 -0
+	vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);

Can you obtain the 'struct device' first then use managed resources (devm_*)?

I think any devres conversion should be done in a follow-up patch and not be included in the fix (e.g. in order to facilitate backporting). We also don't want to mix allocation schemes.

Johan

#7 lore raw

Re: [PATCH v2 1/2] mfd: viperboard: allocate I/O buffer separately

Quoted 26 lines · Johan Hovold
Quoted 25 lines · Lee Jones
Quoted 23 lines · Octavian Purdila

Currently the I/O buffer is allocated part of the device status structure, potentially sharing the same cache line with other members in this structure.

Allocate the buffer separately, to avoid the I/O operations corrupting the device status structure due to cache line sharing.

Compiled tested only as I don't have access to hardware.

---
 drivers/mfd/viperboard.c       | 8 ++++++++
 include/linux/mfd/viperboard.h | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/mfd/viperboard.c +0 -0
index e00f534..5f62f4e 100644
@@ -64,6 +64,12 @@ static int vprbrd_probe(struct usb_interface *interface,
 		return -ENOMEM;
 	}
 
Quoted 8 lines
Quoted 4 lines

drivers/mfd/viperboard.c static int vprbrd_probe(struct usb_interface *interface, · line 67

Quoted 1 line
drivers/mfd/viperboard.c +1 -0
+	vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);

Can you obtain the 'struct device' first then use managed resources (devm_*)?

I think any devres conversion should be done in a follow-up patch and not be included in the fix (e.g. in order to facilitate backporting). We also don't want to mix allocation schemes.

I agree, but equally I'm not keen on accepting this patch as I believe it could be done better.

Please submit two patches, one converting to shared resources and this being the subsequent one, fixed up to do the right thing.

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#8 lore raw

Re: [PATCH v2 1/2] mfd: viperboard: allocate I/O buffer separately

Quoted 37 lines · Lee Jones
Quoted 26 lines · Johan Hovold
Quoted 25 lines · Lee Jones
Quoted 23 lines · Octavian Purdila

Currently the I/O buffer is allocated part of the device status structure, potentially sharing the same cache line with other members in this structure.

Allocate the buffer separately, to avoid the I/O operations corrupting the device status structure due to cache line sharing.

Compiled tested only as I don't have access to hardware.

---
 drivers/mfd/viperboard.c       | 8 ++++++++
 include/linux/mfd/viperboard.h | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/mfd/viperboard.c +0 -0
index e00f534..5f62f4e 100644
@@ -64,6 +64,12 @@ static int vprbrd_probe(struct usb_interface *interface,
 		return -ENOMEM;
 	}
 
Quoted 8 lines
Quoted 4 lines

drivers/mfd/viperboard.c static int vprbrd_probe(struct usb_interface *interface, · line 67

Quoted 1 line
drivers/mfd/viperboard.c +1 -0
+	vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);

Can you obtain the 'struct device' first then use managed resources (devm_*)?

I think any devres conversion should be done in a follow-up patch and not be included in the fix (e.g. in order to facilitate backporting). We also don't want to mix allocation schemes.

Quoted 5 lines

I agree, but equally I'm not keen on accepting this patch as I believe it could be done better.

Please submit two patches, one converting to shared resources and this being the subsequent one, fixed up to do the right thing.

A buffer-corruption fix is a candidate for stable, whereas a devres conversion (clean up) is not. Hence the former should not depend on the latter.

Johan

#9 lore raw

Re: [PATCH v2 1/2] mfd: viperboard: allocate I/O buffer separately

Quoted 41 lines · Johan Hovold
Quoted 40 lines · Lee Jones
Quoted 26 lines · Johan Hovold
Quoted 25 lines · Lee Jones
Quoted 23 lines · Octavian Purdila

Currently the I/O buffer is allocated part of the device status structure, potentially sharing the same cache line with other members in this structure.

Allocate the buffer separately, to avoid the I/O operations corrupting the device status structure due to cache line sharing.

Compiled tested only as I don't have access to hardware.

---
 drivers/mfd/viperboard.c       | 8 ++++++++
 include/linux/mfd/viperboard.h | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/mfd/viperboard.c +0 -0
index e00f534..5f62f4e 100644
@@ -64,6 +64,12 @@ static int vprbrd_probe(struct usb_interface *interface,
                return -ENOMEM;
        }
 
Quoted 8 lines
Quoted 4 lines

drivers/mfd/viperboard.c static int vprbrd_probe(struct usb_interface *interface, · line 67

Quoted 1 line
drivers/mfd/viperboard.c +1 -0
+       vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);

Can you obtain the 'struct device' first then use managed resources (devm_*)?

I think any devres conversion should be done in a follow-up patch and not be included in the fix (e.g. in order to facilitate backporting). We also don't want to mix allocation schemes.

I agree, but equally I'm not keen on accepting this patch as I believe it could be done better.

Quoted 7 lines
Quoted 2 lines

Please submit two patches, one converting to shared resources and this being the subsequent one, fixed up to do the right thing.

A buffer-corruption fix is a candidate for stable, whereas a devres conversion (clean up) is not. Hence the former should not depend on the latter.

I can follow-up with a v3 3 patch series: first for the fix, second for the OOM & error path cleanup, third for devm conversion.

I think this will address both issues (backport and better final result).

#10 lore raw

Re: [PATCH v2 1/2] mfd: viperboard: allocate I/O buffer separately

Quoted 2 lines · Octavian Purdila

I can follow-up with a v3 3 patch series: first for the fix, second for the OOM & error path cleanup, third for devm conversion.

I'd include the error-path clean up bit in the devres conversion as that is what it's really all about.

Johan

#11 lore raw

PATCH v2 2/2 mfd: viperboard: remove redundant OOM message and NULL pointer check

In reply to Octavian Purdila
---
 drivers/mfd/viperboard.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/mfd/viperboard.c +4 -8
index 5f62f4e..d27c131 100644
@@ -59,10 +59,8 @@ static int vprbrd_probe(struct usb_interface *interface,
 
 	/* allocate memory for our device state and initialize it */
 	vb = kzalloc(sizeof(*vb), GFP_KERNEL);
-	if (vb == NULL) {
-		dev_err(&interface->dev, "Out of memory\n");
+	if (vb == NULL)
 		return -ENOMEM;
-	}
 
 	vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);
 	if (vb->buf == NULL) {
@@ -109,11 +107,9 @@ static int vprbrd_probe(struct usb_interface *interface,
 	return 0;
 
 error:
-	if (vb) {
-		usb_put_dev(vb->usb_dev);
-		kfree(vb->buf);
-		kfree(vb);
-	}
+	usb_put_dev(vb->usb_dev);
+	kfree(vb->buf);
+	kfree(vb);
 
 	return ret;
 }
Signature · 1 line
1.9.1
#12 lore raw

Re: [PATCH v2 2/2] mfd: viperboard: remove redundant OOM message and NULL pointer check

Quoted 4 lines · Octavian Purdila
---
 drivers/mfd/viperboard.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

Looks fine, but won't apply without patch 1.

Quoted 31 lines
drivers/mfd/viperboard.c +4 -8
index 5f62f4e..d27c131 100644
@@ -59,10 +59,8 @@ static int vprbrd_probe(struct usb_interface *interface,
 
 	/* allocate memory for our device state and initialize it */
 	vb = kzalloc(sizeof(*vb), GFP_KERNEL);
-	if (vb == NULL) {
-		dev_err(&interface->dev, "Out of memory\n");
+	if (vb == NULL)
 		return -ENOMEM;
-	}
 
 	vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);
 	if (vb->buf == NULL) {
@@ -109,11 +107,9 @@ static int vprbrd_probe(struct usb_interface *interface,
 	return 0;
 
 error:
-	if (vb) {
-		usb_put_dev(vb->usb_dev);
-		kfree(vb->buf);
-		kfree(vb);
-	}
+	usb_put_dev(vb->usb_dev);
+	kfree(vb->buf);
+	kfree(vb);
 
 	return ret;
 }
Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog