Skip to contentSkip to conversation

fix BUG: in fw_realloc_buffer

PATCH In review Jeff Moyer 2 participants 5 messages Feb 13, 2006 Driver core

lore
Share & tools
lore

Jeff Moyer

author
#0 lore raw

PATCH 1/1 fix BUG: in fw_realloc_buffer

Hi,

The fw_realloc_buffer routine does not handle an increase in buffer size of more than 4k. It's not clear to me why it expects that it will only get an extra 4k of data. The attached patch modifies fw_realloc_buffer to vmalloc as much memory as is requested, instead of what we previously had + 4k.

I've tested this on my laptop, which would crash occaisionally on boot without the patch. With the patch, it hasn't crashed, but I can't be certain that this code path is exercised.

Comments are very welcome.

Thanks,

Jeff

drivers/base/firmware_class.c renamed from drivers/base/firmware_class.c.orig +6 -2
@@ -211,18 +211,22 @@ static int
 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
 {
 	u8 *new_data;
+	int new_size = fw_priv->alloc_size;
 
 	if (min_size <= fw_priv->alloc_size)
 		return 0;
 
-	new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
+	while (new_size < min_size)
+		new_size += PAGE_SIZE;
+
+	new_data = vmalloc(new_size);
 	if (!new_data) {
 		printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
 		/* Make sure that we don't keep incomplete data */
 		fw_load_abort(fw_priv);
 		return -ENOMEM;
 	}
-	fw_priv->alloc_size += PAGE_SIZE;
+	fw_priv->alloc_size = new_size;
 	if (fw_priv->fw->data) {
 		memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
 		vfree(fw_priv->fw->data);
#1 lore raw
Quoted 37 lines · Jeff Moyer

Hi,

The fw_realloc_buffer routine does not handle an increase in buffer size of more than 4k. It's not clear to me why it expects that it will only get an extra 4k of data. The attached patch modifies fw_realloc_buffer to vmalloc as much memory as is requested, instead of what we previously had + 4k.

I've tested this on my laptop, which would crash occaisionally on boot without the patch. With the patch, it hasn't crashed, but I can't be certain that this code path is exercised.

Comments are very welcome.

Thanks,

Jeff

drivers/base/firmware_class.c renamed from drivers/base/firmware_class.c.orig +5 -1
@@ -211,18 +211,22 @@ static int
 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
 {
 	u8 *new_data;
+	int new_size = fw_priv->alloc_size;
 
 	if (min_size <= fw_priv->alloc_size)
 		return 0;
 
-	new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
+	while (new_size < min_size)
+		new_size += PAGE_SIZE;
+
+	new_data = vmalloc(new_size);
 	if (!new_data) {

drivers/base/firmware_class.c static int · line 232

Quoted 10 lines
drivers/base/firmware_class.c renamed from drivers/base/firmware_class.c.orig +1 -1
 		printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
 		/* Make sure that we don't keep incomplete data */
 		fw_load_abort(fw_priv);
 		return -ENOMEM;
 	}
-	fw_priv->alloc_size += PAGE_SIZE;
+	fw_priv->alloc_size = new_size;
 	if (fw_priv->fw->data) {
 		memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
 		vfree(fw_priv->fw->data);

A little bit neater this way, I think?

drivers/base/firmware_class.c renamed from drivers/base/firmware_class.c~firmware-fix-bug-in-fw_realloc_buffer +4 -2
@@ -211,18 +211,20 @@ static int
 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
 {
 	u8 *new_data;
+	int new_size = fw_priv->alloc_size;
 
 	if (min_size <= fw_priv->alloc_size)
 		return 0;
 
-	new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
+	new_size = ALIGN(min_size, PAGE_SIZE);
+	new_data = vmalloc(new_size);
 	if (!new_data) {
 		printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
 		/* Make sure that we don't keep incomplete data */
 		fw_load_abort(fw_priv);
 		return -ENOMEM;
 	}
-	fw_priv->alloc_size += PAGE_SIZE;
+	fw_priv->alloc_size = new_size;
 	if (fw_priv->fw->data) {
 		memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
 		vfree(fw_priv->fw->data);

_

author
#2 lore raw

==> Regarding Re: [patch] fix BUG: in fw_realloc_buffer; Andrew Morton <[email protected]> adds:

Quoted 8 lines · akpm> Jeff Moyer <[email protected]> wrote:
Quoted 8 lines

Hi,

The fw_realloc_buffer routine does not handle an increase in buffer size of more than 4k. It's not clear to me why it expects that it will only get an extra 4k of data. The attached patch modifies fw_realloc_buffer to vmalloc as much memory as is requested, instead of what we previously had + 4k.

Quoted 6 lines
Quoted 6 lines

I've tested this on my laptop, which would crash occaisionally on boot without the patch. With the patch, it hasn't crashed, but I can't be certain that this code path is exercised.

Comments are very welcome.

[snip]

akpm> A little bit neater this way, I think?

Quoted 25 lines
drivers/base/firmware_class.c renamed from drivers/base/firmware_class.c~firmware-fix-bug-in-fw_realloc_buffer +4 -2
@@ -211,18 +211,20 @@ static int
 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
 {
 	u8 *new_data;
+	int new_size = fw_priv->alloc_size;
 
 	if (min_size <= fw_priv->alloc_size)
 		return 0;
 
-	new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
+	new_size = ALIGN(min_size, PAGE_SIZE);
+	new_data = vmalloc(new_size);
 	if (!new_data) {
 		printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
 		/* Make sure that we don't keep incomplete data */
 		fw_load_abort(fw_priv);
 		return -ENOMEM;
 	}
-	fw_priv->alloc_size += PAGE_SIZE;
+	fw_priv->alloc_size = new_size;
 	if (fw_priv->fw->data) {
 		memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
 		vfree(fw_priv->fw->data);
Quoted 1 line

_

Well, I wasn't sure that you would only need to increase by a PAGE. If you only need to account for page_size + alignment, then yes, this is better. It simply wasn't clear to me that this is how we are called. If I'm not mistaken, this is the write routine for a file in sysfs. If that is the case, why should we assume that writes are broken up into PAGE_SIZE chunks?

Thanks,

Jeff

author
#3 lore raw

==> Regarding Re: [patch] fix BUG: in fw_realloc_buffer; Jeff Moyer <[email protected]> adds:

==> Regarding Re: [patch] fix BUG: in fw_realloc_buffer; Andrew Morton <[email protected]> adds:

Quoted 8 lines · akpm> Jeff Moyer <[email protected]> wrote:
Quoted 8 lines
Quoted 8 lines

Hi,

The fw_realloc_buffer routine does not handle an increase in buffer size of more than 4k. It's not clear to me why it expects that it will only get an extra 4k of data. The attached patch modifies fw_realloc_buffer to vmalloc as much memory as is requested, instead of what we previously had + 4k.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

I've tested this on my laptop, which would crash occaisionally on boot without the patch. With the patch, it hasn't crashed, but I can't be certain that this code path is exercised.

Comments are very welcome.

jmoyer> [snip]

akpm> A little bit neater this way, I think?

Quoted 11 lines
Quoted 10 lines
drivers/base/firmware_class.c renamed from drivers/base/firmware_class.c~firmware-fix-bug-in-fw_realloc_buffer +1 -0
@@ -211,18 +211,20 @@ static int
 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
 {
 u8 *new_data;
+	int new_size = fw_priv->alloc_size;
 
 if (min_size <= fw_priv->alloc_size)
 return 0;
Quoted 15 lines
Quoted 14 lines
drivers/base/firmware_class.c renamed from drivers/base/firmware_class.c~firmware-fix-bug-in-fw_realloc_buffer +3 -2
-	new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
+	new_size = ALIGN(min_size, PAGE_SIZE);
+	new_data = vmalloc(new_size);
 if (!new_data) {
 printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
 /* Make sure that we don't keep incomplete data */
 fw_load_abort(fw_priv);
 return -ENOMEM;
 }
-	fw_priv->alloc_size += PAGE_SIZE;
+	fw_priv->alloc_size = new_size;
 if (fw_priv->fw->data) {
 memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
 vfree(fw_priv->fw->data);
Quoted 1 line

_

jmoyer> Well, I wasn't sure that you would only need to increase by a PAGE. If you jmoyer> only need to account for page_size + alignment, then yes, this is better. jmoyer> It simply wasn't clear to me that this is how we are called. If I'm not jmoyer> mistaken, this is the write routine for a file in sysfs. If that is the jmoyer> case, why should we assume that writes are broken up into PAGE_SIZE chunks?

Doh, I wasn't looking close enough at this. Yes, your fix is what was intended. Thanks.

-Jeff

#4 lore raw
In reply to Jeff Moyer
Quoted 27 lines · Jeff Moyer

akpm> A little bit neater this way, I think?

Quoted 24 lines
drivers/base/firmware_class.c renamed from drivers/base/firmware_class.c~firmware-fix-bug-in-fw_realloc_buffer +4 -2
@@ -211,18 +211,20 @@ static int
 fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
 {
 	u8 *new_data;
+	int new_size = fw_priv->alloc_size;
 
 	if (min_size <= fw_priv->alloc_size)
 		return 0;
 
-	new_data = vmalloc(fw_priv->alloc_size + PAGE_SIZE);
+	new_size = ALIGN(min_size, PAGE_SIZE);
+	new_data = vmalloc(new_size);
 	if (!new_data) {
 		printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
 		/* Make sure that we don't keep incomplete data */
 		fw_load_abort(fw_priv);
 		return -ENOMEM;
 	}
-	fw_priv->alloc_size += PAGE_SIZE;
+	fw_priv->alloc_size = new_size;
 	if (fw_priv->fw->data) {
 		memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
Quoted 9 lines
Quoted 2 lines
drivers/base/firmware_class.c renamed from drivers/base/firmware_class.c~firmware-fix-bug-in-fw_realloc_buffer +0 -0
 		vfree(fw_priv->fw->data);

_

Well, I wasn't sure that you would only need to increase by a PAGE. If you only need to account for page_size + alignment, then yes, this is better. It simply wasn't clear to me that this is how we are called. If I'm not mistaken, this is the write routine for a file in sysfs. If that is the case, why should we assume that writes are broken up into PAGE_SIZE chunks?

hm? The code's equivanent, I think. ->alloc_size is always a multiple of PAGE_SIZE and the ALIGN makes new_size the next multiple of PAGE_SIZE which is >= min_size.