Skip to contentSkip to conversation

hdpvr: reduce latency of i2c read/write w/recycled buffer

PATCH Superseded Jarod Wilson 2 participants 5 messages Jan 14, 2011 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hdpvr: reduce latency of i2c read/write w/recycled buffer

The current hdpvr code kmalloc's a new buffer for every i2c read and write. Rather than do that, lets allocate a buffer in the driver's device struct and just use that every time.

The size I've chosen for the buffer is the maximum size I could ascertain might be used by either ir-kbd-i2c or lirc_zilog, plus a bit of padding (lirc_zilog may use up to 100 bytes on tx, rounded that up to 128).

Note that this might also remedy user reports of very sluggish behavior of IR receive with hdpvr hardware.

---

Nb: This patch was done atop my prior patch 'hdpvr: enable IR part', and serves no purpose if that patch isn't applied first.

 drivers/media/video/hdpvr/hdpvr-i2c.c |   24 +++++++-----------------
 drivers/media/video/hdpvr/hdpvr.h     |    3 +++
 2 files changed, 10 insertions(+), 17 deletions(-)
drivers/media/video/hdpvr/hdpvr-i2c.c +7 -17
index c0696c3..7f1a313 100644
@@ -57,23 +57,18 @@ static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
 			  unsigned char addr, char *data, int len)
 {
 	int ret;
-	char *buf = kmalloc(len, GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
 
 	ret = usb_control_msg(dev->udev,
 			      usb_rcvctrlpipe(dev->udev, 0),
 			      REQTYPE_I2C_READ, CTRL_READ_REQUEST,
-			      (bus << 8) | addr, 0, buf, len, 1000);
+			      (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
 
 	if (ret == len) {
-		memcpy(data, buf, len);
+		memcpy(data, &dev->i2c_buf, len);
 		ret = 0;
 	} else if (ret >= 0)
 		ret = -EIO;
 
-	kfree(buf);
-
 	return ret;
 }
 
@@ -81,31 +76,26 @@ static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
 			   unsigned char addr, char *data, int len)
 {
 	int ret;
-	char *buf = kmalloc(len, GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
 
-	memcpy(buf, data, len);
+	memcpy(&dev->i2c_buf, data, len);
 	ret = usb_control_msg(dev->udev,
 			      usb_sndctrlpipe(dev->udev, 0),
 			      REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
-			      (bus << 8) | addr, 0, buf, len, 1000);
+			      (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
 
 	if (ret < 0)
-		goto error;
+		return ret;
 
 	ret = usb_control_msg(dev->udev,
 			      usb_rcvctrlpipe(dev->udev, 0),
 			      REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
-			      0, 0, buf, 2, 1000);
+			      0, 0, &dev->i2c_buf, 2, 1000);
 
-	if ((ret == 2) && (buf[1] == (len - 1)))
+	if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))
 		ret = 0;
 	else if (ret >= 0)
 		ret = -EIO;
 
-error:
-	kfree(buf);
 	return ret;
 }
 
drivers/media/video/hdpvr/hdpvr.h +3 -0
index 29f7426..ee74e3b 100644
@@ -25,6 +25,7 @@
 	KERNEL_VERSION(HDPVR_MAJOR_VERSION, HDPVR_MINOR_VERSION, HDPVR_RELEASE)
 
 #define HDPVR_MAX 8
+#define HDPVR_I2C_MAX_SIZE 128
 
 /* Define these values to match your devices */
 #define HD_PVR_VENDOR_ID	0x2040
@@ -109,6 +110,8 @@ struct hdpvr_device {
 	struct i2c_adapter	i2c_adapter;
 	/* I2C lock */
 	struct mutex		i2c_mutex;
+	/* I2C message buffer space */
+	char			i2c_buf[HDPVR_I2C_MAX_SIZE];
 
 	/* For passing data to ir-kbd-i2c */
 	struct IR_i2c_init_data	ir_i2c_init_data;
Signature · 5 lines
1.7.1

-- 
Jarod Wilson
[email protected]
#1 lore raw

Hi Jarod,

Quoted 8 lines · Jarod Wilson

The current hdpvr code kmalloc's a new buffer for every i2c read and write. Rather than do that, lets allocate a buffer in the driver's device struct and just use that every time.

The size I've chosen for the buffer is the maximum size I could ascertain might be used by either ir-kbd-i2c or lirc_zilog, plus a bit of padding (lirc_zilog may use up to 100 bytes on tx, rounded that up to 128).

Definitely a good move, as discussed on IRC earlier this week.

Quoted 2 lines

Note that this might also remedy user reports of very sluggish behavior of IR receive with hdpvr hardware.

Maybe. But the fact that the Zilog is unresponsive during processing of sent data certainly contributes to this feeling too.

Quoted 19 lines

---

Nb: This patch was done atop my prior patch 'hdpvr: enable IR part', and serves no purpose if that patch isn't applied first.

 drivers/media/video/hdpvr/hdpvr-i2c.c |   24 +++++++-----------------
 drivers/media/video/hdpvr/hdpvr.h     |    3 +++
 2 files changed, 10 insertions(+), 17 deletions(-)
drivers/media/video/hdpvr/hdpvr-i2c.c +0 -0
index c0696c3..7f1a313 100644
@@ -57,23 +57,18 @@ static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
 			  unsigned char addr, char *data, int len)
 {

drivers/media/video/hdpvr/hdpvr-i2c.c static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus, · line 64

Quoted 10 lines
drivers/media/video/hdpvr/hdpvr-i2c.c +1 -4
 	int ret;
-	char *buf = kmalloc(len, GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
 
 	ret = usb_control_msg(dev->udev,
 			      usb_rcvctrlpipe(dev->udev, 0),
 			      REQTYPE_I2C_READ, CTRL_READ_REQUEST,
-			      (bus << 8) | addr, 0, buf, len, 1000);
+			      (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);

Shouldn't you first ensure that len <= sizeof(dev->i2c_buf)? It should hopefully always be the case, but... if it isn't, you'll corrupt your data structure and possibly more.

Quoted 14 lines
drivers/media/video/hdpvr/hdpvr-i2c.c +1 -3
 	if (ret == len) {
-		memcpy(data, buf, len);
+		memcpy(data, &dev->i2c_buf, len);
 		ret = 0;
 	} else if (ret >= 0)
 		ret = -EIO;
 
-	kfree(buf);
-
 	return ret;
 }
 

drivers/media/video/hdpvr/hdpvr-i2c.c static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus, · line 80

Quoted 9 lines
drivers/media/video/hdpvr/hdpvr-i2c.c +1 -4
@@ -81,31 +76,26 @@ static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
 			   unsigned char addr, char *data, int len)
 {
 	int ret;
-	char *buf = kmalloc(len, GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
 
-	memcpy(buf, data, len);
+	memcpy(&dev->i2c_buf, data, len);

Same here.

Quoted 40 lines
drivers/media/video/hdpvr/hdpvr-i2c.c +4 -6
 	ret = usb_control_msg(dev->udev,
 			      usb_sndctrlpipe(dev->udev, 0),
 			      REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
-			      (bus << 8) | addr, 0, buf, len, 1000);
+			      (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
 
 	if (ret < 0)
-		goto error;
+		return ret;
 
 	ret = usb_control_msg(dev->udev,
 			      usb_rcvctrlpipe(dev->udev, 0),
 			      REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
-			      0, 0, buf, 2, 1000);
+			      0, 0, &dev->i2c_buf, 2, 1000);
 
-	if ((ret == 2) && (buf[1] == (len - 1)))
+	if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))
 		ret = 0;
 	else if (ret >= 0)
 		ret = -EIO;
 
-error:
-	kfree(buf);
 	return ret;
 }
 
drivers/media/video/hdpvr/hdpvr.h +1 -0
index 29f7426..ee74e3b 100644
@@ -25,6 +25,7 @@
 	KERNEL_VERSION(HDPVR_MAJOR_VERSION, HDPVR_MINOR_VERSION, HDPVR_RELEASE)
 
 #define HDPVR_MAX 8
+#define HDPVR_I2C_MAX_SIZE 128
 
 /* Define these values to match your devices */
 #define HD_PVR_VENDOR_ID	0x2040

drivers/media/video/hdpvr/hdpvr.h struct hdpvr_device { · line 117

Quoted 8 lines
drivers/media/video/hdpvr/hdpvr.h +2 -0
@@ -109,6 +110,8 @@ struct hdpvr_device {
 	struct i2c_adapter	i2c_adapter;
 	/* I2C lock */
 	struct mutex		i2c_mutex;
+	/* I2C message buffer space */
+	char			i2c_buf[HDPVR_I2C_MAX_SIZE];
 
 	/* For passing data to ir-kbd-i2c */
 	struct IR_i2c_init_data	ir_i2c_init_data;

Other than that, it looks pretty good.

Signature · 1 line
Jean Delvare
#2 lore raw
Quoted 12 lines · Jean Delvare

Hi Jarod,

Quoted 8 lines · Jarod Wilson

The current hdpvr code kmalloc's a new buffer for every i2c read and write. Rather than do that, lets allocate a buffer in the driver's device struct and just use that every time.

The size I've chosen for the buffer is the maximum size I could ascertain might be used by either ir-kbd-i2c or lirc_zilog, plus a bit of padding (lirc_zilog may use up to 100 bytes on tx, rounded that up to 128).

Quoted 7 lines

Definitely a good move, as discussed on IRC earlier this week.

Quoted 2 lines

Note that this might also remedy user reports of very sluggish behavior of IR receive with hdpvr hardware.

Maybe. But the fact that the Zilog is unresponsive during processing of sent data certainly contributes to this feeling too.

I believe this was the case even when tx wasn't in use, so I'm hopeful.

Quoted 21 lines
Quoted 7 lines
drivers/media/video/hdpvr/hdpvr-i2c.c +0 -0
index c0696c3..7f1a313 100644
@@ -57,23 +57,18 @@ static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
 			  unsigned char addr, char *data, int len)
 {

drivers/media/video/hdpvr/hdpvr-i2c.c static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus, · line 64

Quoted 10 lines
drivers/media/video/hdpvr/hdpvr-i2c.c +1 -4
 	int ret;
-	char *buf = kmalloc(len, GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
 
 	ret = usb_control_msg(dev->udev,
 			      usb_rcvctrlpipe(dev->udev, 0),
 			      REQTYPE_I2C_READ, CTRL_READ_REQUEST,
-			      (bus << 8) | addr, 0, buf, len, 1000);
+			      (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);

Shouldn't you first ensure that len <= sizeof(dev->i2c_buf)? It should hopefully always be the case, but... if it isn't, you'll corrupt your data structure and possibly more.

...

Quoted 8 lines
Quoted 8 lines
drivers/media/video/hdpvr/hdpvr-i2c.c +0 -3
@@ -81,31 +76,26 @@ static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
 			   unsigned char addr, char *data, int len)
 {
 	int ret;
-	char *buf = kmalloc(len, GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
 
Quoted 4 lines

drivers/media/video/hdpvr/hdpvr-i2c.c static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus, · line 80

Quoted 2 lines
drivers/media/video/hdpvr/hdpvr-i2c.c +1 -1
-	memcpy(buf, data, len);
+	memcpy(&dev->i2c_buf, data, len);

Same here.

Yeah, while it *shouldn't* happen, making sure seems prudent. v2 in a moment.

Signature · 2 lines
Jarod Wilson
[email protected]
#3 lore raw

PATCH v2 1/1 hdpvr: reduce latency of i2c read/write w/recycled buffer

In reply to Jarod Wilson

The current hdpvr code kmalloc's a new buffer for every i2c read and write. Rather than do that, lets allocate a buffer in the driver's device struct and just use that every time.

The size I've chosen for the buffer is the maximum size I could ascertain might be used by either ir-kbd-i2c or lirc_zilog, plus a bit of padding (lirc_zilog may use up to 100 bytes on tx, rounded that up to 128).

Note that this might also remedy user reports of very sluggish behavior of IR receive with hdpvr hardware.

v2: make sure (len <= (dev->i2c_buf)) [Jean Delvare]

---

Nb: This patch was done atop my prior patch 'hdpvr: enable IR part', and serves no purpose if that patch isn't applied first.

 drivers/media/video/hdpvr/hdpvr-i2c.c |   30 +++++++++++++-----------------
 drivers/media/video/hdpvr/hdpvr.h     |    3 +++
 2 files changed, 16 insertions(+), 17 deletions(-)
drivers/media/video/hdpvr/hdpvr-i2c.c +13 -17
index c0696c3..2adfe90 100644
@@ -57,23 +57,21 @@ static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
 			  unsigned char addr, char *data, int len)
 {
 	int ret;
-	char *buf = kmalloc(len, GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
+
+	if (len > sizeof(dev->i2c_buf))
+		return -EINVAL;
 
 	ret = usb_control_msg(dev->udev,
 			      usb_rcvctrlpipe(dev->udev, 0),
 			      REQTYPE_I2C_READ, CTRL_READ_REQUEST,
-			      (bus << 8) | addr, 0, buf, len, 1000);
+			      (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
 
 	if (ret == len) {
-		memcpy(data, buf, len);
+		memcpy(data, &dev->i2c_buf, len);
 		ret = 0;
 	} else if (ret >= 0)
 		ret = -EIO;
 
-	kfree(buf);
-
 	return ret;
 }
 
@@ -81,31 +79,29 @@ static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
 			   unsigned char addr, char *data, int len)
 {
 	int ret;
-	char *buf = kmalloc(len, GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
 
-	memcpy(buf, data, len);
+	if (len > sizeof(dev->i2c_buf))
+		return -EINVAL;
+
+	memcpy(&dev->i2c_buf, data, len);
 	ret = usb_control_msg(dev->udev,
 			      usb_sndctrlpipe(dev->udev, 0),
 			      REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
-			      (bus << 8) | addr, 0, buf, len, 1000);
+			      (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
 
 	if (ret < 0)
-		goto error;
+		return ret;
 
 	ret = usb_control_msg(dev->udev,
 			      usb_rcvctrlpipe(dev->udev, 0),
 			      REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
-			      0, 0, buf, 2, 1000);
+			      0, 0, &dev->i2c_buf, 2, 1000);
 
-	if ((ret == 2) && (buf[1] == (len - 1)))
+	if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))
 		ret = 0;
 	else if (ret >= 0)
 		ret = -EIO;
 
-error:
-	kfree(buf);
 	return ret;
 }
 
drivers/media/video/hdpvr/hdpvr.h +3 -0
index 29f7426..ee74e3b 100644
@@ -25,6 +25,7 @@
 	KERNEL_VERSION(HDPVR_MAJOR_VERSION, HDPVR_MINOR_VERSION, HDPVR_RELEASE)
 
 #define HDPVR_MAX 8
+#define HDPVR_I2C_MAX_SIZE 128
 
 /* Define these values to match your devices */
 #define HD_PVR_VENDOR_ID	0x2040
@@ -109,6 +110,8 @@ struct hdpvr_device {
 	struct i2c_adapter	i2c_adapter;
 	/* I2C lock */
 	struct mutex		i2c_mutex;
+	/* I2C message buffer space */
+	char			i2c_buf[HDPVR_I2C_MAX_SIZE];
 
 	/* For passing data to ir-kbd-i2c */
 	struct IR_i2c_init_data	ir_i2c_init_data;
Signature · 6 lines
1.7.1


-- 
Jarod Wilson
[email protected]
Acked-by on v2 1/1
#4 lore raw
Quoted 9 lines · Jarod Wilson

The current hdpvr code kmalloc's a new buffer for every i2c read and write. Rather than do that, lets allocate a buffer in the driver's device struct and just use that every time.

The size I've chosen for the buffer is the maximum size I could ascertain might be used by either ir-kbd-i2c or lirc_zilog, plus a bit of padding (lirc_zilog may use up to 100 bytes on tx, rounded that up to 128).

Quoted 7 lines

Note that this might also remedy user reports of very sluggish behavior of IR receive with hdpvr hardware.

v2: make sure (len <= (dev->i2c_buf)) [Jean Delvare]

Signature · 1 line
Jean Delvare