[PATCH] mfd: viperboard: allocate I/O buffer separately

Octavian Purdila
2014-09-22T14:47:09Z

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.

Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
---
drivers/mfd/viperboard.c | 16 ++++++++++------
include/linux/mfd/viperboard.h | 2 +-
2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/drivers/mfd/viperboard.c b/drivers/mfd/viperboard.c
index e00f534..d27c131 100644
--- a/drivers/mfd/viperboard.c
+++ b/drivers/mfd/viperboard.c
@@ -59,9 +59,13 @@ 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) {
+ ret = -ENOMEM;
+ goto error;
}

mutex_init(&vb->lock);
@@ -103,10 +107,9 @@ static int vprbrd_probe(struct usb_interface *interface,
return 0;

error:
- if (vb) {
- usb_put_dev(vb->usb_dev);
- kfree(vb);
- }
+ usb_put_dev(vb->usb_dev);
+ kfree(vb->buf);
+ kfree(vb);

return ret;
}
@@ -118,6 +121,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");
diff --git a/include/linux/mfd/viperboard.h b/include/linux/mfd/viperboard.h
index 1934528..af928d0 100644
--- a/include/linux/mfd/viperboard.h
+++ b/include/linux/mfd/viperboard.h
@@ -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;
};

--
1.9.1