Skip to contentSkip to conversation

mfd: viperboard: fix cache line sharing and cleanups

PATCH In review Octavian Purdila 2 participants 8 messages Sep 25, 2014 – Oct 9, 2014 Multifunction devices (MFD)

lore
Share & tools
lore
#0 lore raw

v4 cover mfd: viperboard: fix cache line sharing and cleanups

Changes since v3:

* undo a whitespace change

* one more error path cleanup

* add commit body to the last patch

Changes since v2:

* switch to using devm_ allocation

* remove unused platform_device

Changes since v1:

* split cache sharing fix and cleanups into separate patches

Octavian Purdila (3):
mfd: viperboard: allocate I/O buffer separately
mfd: viperboard: switch to devm_ allocation
mfd: viperboard: remove unused platform_device

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

PATCH v4 1/3 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
Reviewed-by on v4 1/3
#2 lore raw

Re: [PATCH v4 1/3] mfd: viperboard: allocate I/O buffer separately

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;
 };
#3 lore raw

PATCH v4 2/3 mfd: viperboard: switch to devm_ allocation

In reply to Octavian Purdila

Switch to using devm_ allocation to simplify the error path. Also remove a redundant OOM error message.

---
 drivers/mfd/viperboard.c | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)
drivers/mfd/viperboard.c +7 -16
index 5f62f4e..0d46d05 100644
@@ -58,17 +58,14 @@ static int vprbrd_probe(struct usb_interface *interface,
 	int pipe, ret;
 
 	/* 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");
+	vb = devm_kzalloc(&interface->dev, sizeof(*vb), GFP_KERNEL);
+	if (vb == NULL)
 		return -ENOMEM;
-	}
 
-	vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);
-	if (vb->buf == NULL) {
-		ret = -ENOMEM;
-		goto error;
-	}
+	vb->buf = devm_kzalloc(&interface->dev,
+			       sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);
+	if (vb->buf == NULL)
+		return -ENOMEM;
 
 	mutex_init(&vb->lock);
 
@@ -109,11 +106,7 @@ 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);
 
 	return ret;
 }
@@ -125,8 +118,6 @@ 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");
 }
Signature · 1 line
1.9.1
Reviewed-by on v4 2/3
#4 lore raw

Re: [PATCH v4 2/3] mfd: viperboard: switch to devm_ allocation

Quoted 4 lines · Octavian Purdila

Switch to using devm_ allocation to simplify the error path. Also remove a redundant OOM error message.

Quoted 54 lines
---
 drivers/mfd/viperboard.c | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)
drivers/mfd/viperboard.c +7 -16
index 5f62f4e..0d46d05 100644
@@ -58,17 +58,14 @@ static int vprbrd_probe(struct usb_interface *interface,
 	int pipe, ret;
 
 	/* 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");
+	vb = devm_kzalloc(&interface->dev, sizeof(*vb), GFP_KERNEL);
+	if (vb == NULL)
 		return -ENOMEM;
-	}
 
-	vb->buf = kzalloc(sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);
-	if (vb->buf == NULL) {
-		ret = -ENOMEM;
-		goto error;
-	}
+	vb->buf = devm_kzalloc(&interface->dev,
+			       sizeof(struct vprbrd_i2c_write_msg), GFP_KERNEL);
+	if (vb->buf == NULL)
+		return -ENOMEM;
 
 	mutex_init(&vb->lock);
 
@@ -109,11 +106,7 @@ 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);
 
 	return ret;
 }
@@ -125,8 +118,6 @@ 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");
 }
#5 lore raw

PATCH v4 3/3 mfd: viperboard: remove unused platform_device

In reply to Octavian Purdila

Remove pdev field from struct vpbrd as it is not used in the MFD driver or in any of the sub-drivers.

---
 drivers/mfd/viperboard.c       | 1 -
 include/linux/mfd/viperboard.h | 1 -
 2 files changed, 2 deletions(-)
drivers/mfd/viperboard.c +0 -1
index 0d46d05..f4dcba6 100644
@@ -73,7 +73,6 @@ static int vprbrd_probe(struct usb_interface *interface,
 
 	/* save our data pointer in this interface device */
 	usb_set_intfdata(interface, vb);
-	dev_set_drvdata(&vb->pdev.dev, vb);
 
 	/* get version information, major first, minor then */
 	pipe = usb_rcvctrlpipe(vb->usb_dev, 0);
include/linux/mfd/viperboard.h +0 -1
index af928d0..afc14ed 100644
@@ -104,7 +104,6 @@ struct vprbrd {
 	struct usb_device *usb_dev; /* the usb device for this device */
 	struct mutex lock;
 	u8 *buf;
-	struct platform_device pdev;
 };
 
 #endif /* __MFD_VIPERBOARD_H__ */
Signature · 1 line
1.9.1
Reviewed-by on v4 3/3
#6 lore raw

Re: [PATCH v4 3/3] mfd: viperboard: remove unused platform_device

Quoted 4 lines · Octavian Purdila

Remove pdev field from struct vpbrd as it is not used in the MFD driver or in any of the sub-drivers.

Quoted 29 lines
---
 drivers/mfd/viperboard.c       | 1 -
 include/linux/mfd/viperboard.h | 1 -
 2 files changed, 2 deletions(-)
drivers/mfd/viperboard.c +0 -1
index 0d46d05..f4dcba6 100644
@@ -73,7 +73,6 @@ static int vprbrd_probe(struct usb_interface *interface,
 
 	/* save our data pointer in this interface device */
 	usb_set_intfdata(interface, vb);
-	dev_set_drvdata(&vb->pdev.dev, vb);
 
 	/* get version information, major first, minor then */
 	pipe = usb_rcvctrlpipe(vb->usb_dev, 0);
include/linux/mfd/viperboard.h +0 -1
index af928d0..afc14ed 100644
@@ -104,7 +104,6 @@ struct vprbrd {
 	struct usb_device *usb_dev; /* the usb device for this device */
 	struct mutex lock;
 	u8 *buf;
-	struct platform_device pdev;
 };
 
 #endif /* __MFD_VIPERBOARD_H__ */
#7 lore raw
In reply to Octavian Purdila
Quoted 19 lines · Octavian Purdila

Changes since v3:

* undo a whitespace change

* one more error path cleanup

* add commit body to the last patch

Changes since v2:

* switch to using devm_ allocation

* remove unused platform_device

Changes since v1:

* split cache sharing fix and cleanups into separate patches

Quoted 9 lines

Octavian Purdila (3):
mfd: viperboard: allocate I/O buffer separately
mfd: viperboard: switch to devm_ allocation
mfd: viperboard: remove unused platform_device

 drivers/mfd/viperboard.c       | 18 ++++++++----------
 include/linux/mfd/viperboard.h |  3 +--
 2 files changed, 9 insertions(+), 12 deletions(-)

Hi Lee,

Does these look ok now?

Thanks,
Tavi