Skip to contentSkip to conversation

iscsi_boot_sysfs: Fix a memory leak in iscsi_boot_destroy_kset()

PATCH In review Ethan Zhao 3 participants 9 messages Dec 9, 2013 – Dec 11, 2013

Subsystems (2)
lore
Share & tools
lore

Ethan Zhao

author
#0 lore raw

PATCH 1/1 iscsi_boot_sysfs: Fix a memory leak in iscsi_boot_destroy_kset()

From: "Ethan Zhao" <[email protected]>

Load and unload iscsi_ibft module will cause kernel memory leak, fix it in scsi/iscsi_boot_sysfs.c iscsi_boot_destroy_kset().

---
 drivers/scsi/iscsi_boot_sysfs.c | 1 +
 1 file changed, 1 insertion(+)
drivers/scsi/iscsi_boot_sysfs.c +1 -0
index 14c1c8f..680bf6f 100644
@@ -490,5 +490,6 @@ void iscsi_boot_destroy_kset(struct iscsi_boot_kset *boot_kset)
 		iscsi_boot_remove_kobj(boot_kobj);
 
 	kset_unregister(boot_kset->kset);
+	kfree(boot_kset);
 }
 EXPORT_SYMBOL_GPL(iscsi_boot_destroy_kset);
Signature · 1 line
1.8.3.4 (Apple Git-47)
#1 lore raw
Quoted 5 lines · Ethan Zhao

From: "Ethan Zhao" <[email protected]>

Load and unload iscsi_ibft module will cause kernel memory leak, fix it in scsi/iscsi_boot_sysfs.c iscsi_boot_destroy_kset().

Is there a stack trace?

Quoted 19 lines
---
 drivers/scsi/iscsi_boot_sysfs.c | 1 +
 1 file changed, 1 insertion(+)
drivers/scsi/iscsi_boot_sysfs.c +1 -0
index 14c1c8f..680bf6f 100644
@@ -490,5 +490,6 @@ void iscsi_boot_destroy_kset(struct iscsi_boot_kset *boot_kset)
 		iscsi_boot_remove_kobj(boot_kobj);
 
 	kset_unregister(boot_kset->kset);
+	kfree(boot_kset);
 }
 EXPORT_SYMBOL_GPL(iscsi_boot_destroy_kset);
Signature · 1 line
1.8.3.4 (Apple Git-47)
author
#2 lore raw

Konrad,

boot_kset was allocated when module loaded by
ibft_init()
iscsi_boot_create_kset()
kzalloc()

but wasn't freed when module unloaded by
ibft_exit()
ibft_cleanup()
iscsi_boot_destroy_kset()

Thanks,
Ethan

Quoted 27 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Ethan Zhao

From: "Ethan Zhao" <[email protected]>

Load and unload iscsi_ibft module will cause kernel memory leak, fix it in scsi/iscsi_boot_sysfs.c iscsi_boot_destroy_kset().

Is there a stack trace?

Quoted 19 lines
---
 drivers/scsi/iscsi_boot_sysfs.c | 1 +
 1 file changed, 1 insertion(+)
drivers/scsi/iscsi_boot_sysfs.c +1 -1
index 14c1c8f..680bf6f 100644
@@ -490,5 +490,6 @@ void iscsi_boot_destroy_kset(struct iscsi_boot_kset *boot_kset)
              iscsi_boot_remove_kobj(boot_kobj);
 
      kset_unregister(boot_kset->kset);
+     kfree(boot_kset);
 }
 EXPORT_SYMBOL_GPL(iscsi_boot_destroy_kset);
--

1.8.3.4 (Apple Git-47)

#3 lore raw
Quoted 31 lines · Ethan Zhao

Konrad,

boot_kset was allocated when module loaded by
ibft_init()
iscsi_boot_create_kset()
kzalloc()

but wasn't freed when module unloaded by
ibft_exit()
ibft_cleanup()
iscsi_boot_destroy_kset()

Thanks,
Ethan

Quoted 4 lines · Konrad Rzeszutek Wilk
Quoted 3 lines · Ethan Zhao

From: "Ethan Zhao" <[email protected]>

Load and unload iscsi_ibft module will cause kernel memory leak, fix

it

Quoted 9 lines
Quoted 2 lines

in scsi/iscsi_boot_sysfs.c iscsi_boot_destroy_kset().

Is there a stack trace?

Quoted 5 lines
---
 drivers/scsi/iscsi_boot_sysfs.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 16 lines
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/scsi/iscsi_boot_sysfs.c

Quoted 4 lines

drivers/scsi/iscsi_boot_sysfs.c void iscsi_boot_destroy_kset(struct

Quoted 4 lines

index 14c1c8f..680bf6f 100644

drivers/scsi/iscsi_boot_sysfs.c +0 -0
@@ -490,5 +490,6 @@ void iscsi_boot_destroy_kset(struct

iscsi_boot_kset *boot_kset)

Quoted 9 lines
Quoted 9 lines
drivers/scsi/iscsi_boot_sysfs.c +1 -1
              iscsi_boot_remove_kobj(boot_kobj);
 
      kset_unregister(boot_kset->kset);
+     kfree(boot_kset);
 }
 EXPORT_SYMBOL_GPL(iscsi_boot_destroy_kset);
--

1.8.3.4 (Apple Git-47)

Right. In the past we did not do that b/c certain drivers (like broadcom) would allocate on the kset their name and try to free (the driver would after freeing the ibft). So you would end up with this patch a double free ( and it might be fixed by now but I can't recall).

My question was - did you observer a crash or bug. Hence the question about stack trace? How did you see this?

Thank you.

#4 lore raw
Quoted 49 lines · Konrad Rzeszutek Wilk
Quoted 31 lines · Ethan Zhao

Konrad,

boot_kset was allocated when module loaded by
ibft_init()
iscsi_boot_create_kset()
kzalloc()

but wasn't freed when module unloaded by
ibft_exit()
ibft_cleanup()
iscsi_boot_destroy_kset()

Thanks,
Ethan

Quoted 4 lines · Konrad Rzeszutek Wilk
Quoted 3 lines · Ethan Zhao

From: "Ethan Zhao" <[email protected]>

Load and unload iscsi_ibft module will cause kernel memory leak, fix

it

Quoted 9 lines
Quoted 2 lines

in scsi/iscsi_boot_sysfs.c iscsi_boot_destroy_kset().

Is there a stack trace?

Quoted 5 lines
---
  drivers/scsi/iscsi_boot_sysfs.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 16 lines
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/scsi/iscsi_boot_sysfs.c

Quoted 4 lines

drivers/scsi/iscsi_boot_sysfs.c void iscsi_boot_destroy_kset(struct

Quoted 4 lines

index 14c1c8f..680bf6f 100644

drivers/scsi/iscsi_boot_sysfs.c +0 -0
@@ -490,5 +490,6 @@ void iscsi_boot_destroy_kset(struct

iscsi_boot_kset *boot_kset)

Quoted 9 lines
Quoted 9 lines
drivers/scsi/iscsi_boot_sysfs.c +1 -1
               iscsi_boot_remove_kobj(boot_kobj);
 
       kset_unregister(boot_kset->kset);
+     kfree(boot_kset);
  }
  EXPORT_SYMBOL_GPL(iscsi_boot_destroy_kset);
--

1.8.3.4 (Apple Git-47)

Quoted 2 lines

Right. In the past we did not do that b/c certain drivers (like broadcom) would allocate on the kset their name and try to free (the driver would after freeing the ibft). So you would end up with this patch a double free ( and it might be fixed by now but I can't recall).

Do you mean the name string that is passed into kset_create_and_add? If so that should not happen now. There does not seem to be any user freeing that string.

There used to be bug where at the initiator/target/ethernet level there was a double free because iscsi_boot_sysfs and be2iscsi were both freeing the struct associated with the initiator/target/ethernet. That is fixed in f457a46f179df41b0f6d80dee33b6e629945f276.

#5 lore raw
Quoted 50 lines · Mike Christie
Quoted 49 lines · Konrad Rzeszutek Wilk
Quoted 47 lines · Ethan Zhao

Konrad,

boot_kset was allocated when module loaded by
ibft_init()
iscsi_boot_create_kset()
kzalloc()

but wasn't freed when module unloaded by
ibft_exit()
ibft_cleanup()
iscsi_boot_destroy_kset()

Thanks,
Ethan

Quoted 4 lines · Konrad Rzeszutek Wilk
Quoted 3 lines · Ethan Zhao

From: "Ethan Zhao" <[email protected]>

Load and unload iscsi_ibft module will cause kernel memory leak, fix

it

Quoted 10 lines
Quoted 2 lines

in scsi/iscsi_boot_sysfs.c iscsi_boot_destroy_kset().

Is there a stack trace?

Review

Quoted 6 lines
---
 drivers/scsi/iscsi_boot_sysfs.c | 1 +
 1 file changed, 1 insertion(+)
(quoted diff) +0 -0

b/drivers/scsi/iscsi_boot_sysfs.c

Quoted 4 lines

drivers/scsi/iscsi_boot_sysfs.c void iscsi_boot_destroy_kset(struct

Quoted 4 lines

index 14c1c8f..680bf6f 100644

drivers/scsi/iscsi_boot_sysfs.c +0 -0
@@ -490,5 +490,6 @@ void iscsi_boot_destroy_kset(struct

iscsi_boot_kset *boot_kset)

Quoted 9 lines
Quoted 9 lines
drivers/scsi/iscsi_boot_sysfs.c +1 -1
              iscsi_boot_remove_kobj(boot_kobj);
 
      kset_unregister(boot_kset->kset);
+     kfree(boot_kset);
 }
 EXPORT_SYMBOL_GPL(iscsi_boot_destroy_kset);
--

1.8.3.4 (Apple Git-47)

Quoted 4 lines
Quoted 2 lines

Right. In the past we did not do that b/c certain drivers (like broadcom) would allocate on the kset their name and try to free (the driver would after freeing the ibft). So you would end up with this patch a double free ( and it might be fixed by now but I can't recall).

Do you mean the name string that is passed into kset_create_and_add?

Yes.

Quoted 8 lines

If so that should not happen now. There does not seem to be any user freeing that string.

There used to be bug where at the initiator/target/ethernet level there was a double free because iscsi_boot_sysfs and be2iscsi were both freeing the struct associated with the initiator/target/ethernet. That is fixed in f457a46f179df41b0f6d80dee33b6e629945f276.

That is the one!

Quoted 2 lines
#6 lore raw
Quoted 60 lines · Konrad Rzeszutek Wilk
Quoted 50 lines · Mike Christie
Quoted 49 lines · Konrad Rzeszutek Wilk
Quoted 47 lines · Ethan Zhao

Konrad,

boot_kset was allocated when module loaded by
ibft_init()
iscsi_boot_create_kset()
kzalloc()

but wasn't freed when module unloaded by
ibft_exit()
ibft_cleanup()
iscsi_boot_destroy_kset()

Thanks,
Ethan

Quoted 4 lines · Konrad Rzeszutek Wilk
Quoted 3 lines · Ethan Zhao

From: "Ethan Zhao" <[email protected]>

Load and unload iscsi_ibft module will cause kernel memory leak, fix

it

Quoted 10 lines
Quoted 2 lines

in scsi/iscsi_boot_sysfs.c iscsi_boot_destroy_kset().

Is there a stack trace?

Review

Quoted 6 lines
---
 drivers/scsi/iscsi_boot_sysfs.c | 1 +
 1 file changed, 1 insertion(+)
(quoted diff) +0 -0

b/drivers/scsi/iscsi_boot_sysfs.c

Quoted 4 lines

drivers/scsi/iscsi_boot_sysfs.c void iscsi_boot_destroy_kset(struct

Quoted 4 lines

index 14c1c8f..680bf6f 100644

drivers/scsi/iscsi_boot_sysfs.c +0 -0
@@ -490,5 +490,6 @@ void iscsi_boot_destroy_kset(struct

iscsi_boot_kset *boot_kset)

Quoted 9 lines
Quoted 9 lines
drivers/scsi/iscsi_boot_sysfs.c +1 -1
              iscsi_boot_remove_kobj(boot_kobj);
 
      kset_unregister(boot_kset->kset);
+     kfree(boot_kset);
 }
 EXPORT_SYMBOL_GPL(iscsi_boot_destroy_kset);
--

1.8.3.4 (Apple Git-47)

Quoted 4 lines
Quoted 2 lines

Right. In the past we did not do that b/c certain drivers (like broadcom) would allocate on the kset their name and try to free (the driver would after freeing the ibft). So you would end up with this patch a double free ( and it might be fixed by now but I can't recall).

Do you mean the name string that is passed into kset_create_and_add?

Yes.

Quoted 3 lines

If so that should not happen now. There does not seem to be any user freeing that string.

Quoted 7 lines
Quoted 5 lines

There used to be bug where at the initiator/target/ethernet level there was a double free because iscsi_boot_sysfs and be2iscsi were both freeing the struct associated with the initiator/target/ethernet. That is fixed in f457a46f179df41b0f6d80dee33b6e629945f276.

That is the one!

With that in mind, do you want to carry this patch to Linus (or James) or should I do it?

Quoted 2 lines
Quoted 2 lines
author
#7 lore raw
In reply to Konrad Rzeszutek Wilk
Quoted 49 lines · Konrad Rzeszutek Wilk
Quoted 31 lines · Ethan Zhao

Konrad,

boot_kset was allocated when module loaded by
ibft_init()
iscsi_boot_create_kset()
kzalloc()

but wasn't freed when module unloaded by
ibft_exit()
ibft_cleanup()
iscsi_boot_destroy_kset()

Thanks,
Ethan

Quoted 4 lines · Konrad Rzeszutek Wilk
Quoted 3 lines · Ethan Zhao

From: "Ethan Zhao" <[email protected]>

Load and unload iscsi_ibft module will cause kernel memory leak, fix

it

Quoted 9 lines
Quoted 2 lines

in scsi/iscsi_boot_sysfs.c iscsi_boot_destroy_kset().

Is there a stack trace?

Quoted 5 lines
---
 drivers/scsi/iscsi_boot_sysfs.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 16 lines
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/scsi/iscsi_boot_sysfs.c

Quoted 4 lines

drivers/scsi/iscsi_boot_sysfs.c void iscsi_boot_destroy_kset(struct

Quoted 4 lines

index 14c1c8f..680bf6f 100644

drivers/scsi/iscsi_boot_sysfs.c +0 -0
@@ -490,5 +490,6 @@ void iscsi_boot_destroy_kset(struct

iscsi_boot_kset *boot_kset)

Quoted 9 lines
Quoted 9 lines
drivers/scsi/iscsi_boot_sysfs.c +1 -1
              iscsi_boot_remove_kobj(boot_kobj);
 
      kset_unregister(boot_kset->kset);
+     kfree(boot_kset);
 }
 EXPORT_SYMBOL_GPL(iscsi_boot_destroy_kset);
--

1.8.3.4 (Apple Git-47)

Quoted 3 lines

Right. In the past we did not do that b/c certain drivers (like broadcom) would allocate on the kset their name and try to free (the driver would after freeing the ibft). So you would end up with this patch a double free ( and it might be fixed by now but I can't recall).

My question was - did you observer a crash or bug. Hence the question about stack trace? How did you see this?

That is quite simple, just like fuzzy test, load and unload a module millions of times with a script in low memory size machine, in the meanwhile, monitor OOM etc.

Thanks,
Ethan

Quoted 3 lines

Thank you.

Reviewed-by on v1 1/1
#8 lore raw
In reply to Ethan Zhao
Quoted 15 lines · Ethan Zhao

From: "Ethan Zhao" <[email protected]>

Load and unload iscsi_ibft module will cause kernel memory leak, fix it in scsi/iscsi_boot_sysfs.c iscsi_boot_destroy_kset().

---
 drivers/scsi/iscsi_boot_sysfs.c | 1 +
 1 file changed, 1 insertion(+)
drivers/scsi/iscsi_boot_sysfs.c +0 -0
index 14c1c8f..680bf6f 100644

drivers/scsi/iscsi_boot_sysfs.c void iscsi_boot_destroy_kset(struct iscsi_boot_kset *boot_kset) · line 495

Quoted 6 lines
drivers/scsi/iscsi_boot_sysfs.c +1 -0
@@ -490,5 +490,6 @@ void iscsi_boot_destroy_kset(struct iscsi_boot_kset *boot_kset)
 		iscsi_boot_remove_kobj(boot_kobj);
 
 	kset_unregister(boot_kset->kset);
+	kfree(boot_kset);
 }
 EXPORT_SYMBOL_GPL(iscsi_boot_destroy_kset);

Thanks. Patch looks ok.

It is probably best to just kill the iscsi_boot_kset use and use the kset directly. To do that, we would need to make kset's list a klist and add some iterators then modify iscsi_boot_sysfs.c and iscsi_ibft.c to use them instead of using the iscsi_boot_kset->kobj_list. I will work on a patch to do this. But for now your patch is ok.