PCI: Fix pci_create_slot() reference count leak
kobject_init_and_add() takes a reference even when it fails. If it returns
an error, kobject_put() must be called to clean up the memory associated
with the object.
When kobject_init_and_add() fails, call kobject_put() instead of kfree().
b8eb718348
("net-sysfs: Fix reference count leak in
rx|netdev_queue_add_kobject") fixed a similar problem.
Link: https://lore.kernel.org/r/20200528021322.1984-1-wu000273@umn.edu
Signed-off-by: Qiushi Wu <wu000273@umn.edu>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
This commit is contained in:
parent
b3a9e3b962
commit
8a94644b44
1 changed files with 4 additions and 2 deletions
|
@ -268,13 +268,16 @@ placeholder:
|
||||||
slot_name = make_slot_name(name);
|
slot_name = make_slot_name(name);
|
||||||
if (!slot_name) {
|
if (!slot_name) {
|
||||||
err = -ENOMEM;
|
err = -ENOMEM;
|
||||||
|
kfree(slot);
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
|
err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
|
||||||
"%s", slot_name);
|
"%s", slot_name);
|
||||||
if (err)
|
if (err) {
|
||||||
|
kobject_put(&slot->kobj);
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
INIT_LIST_HEAD(&slot->list);
|
INIT_LIST_HEAD(&slot->list);
|
||||||
list_add(&slot->list, &parent->slots);
|
list_add(&slot->list, &parent->slots);
|
||||||
|
@ -293,7 +296,6 @@ out:
|
||||||
mutex_unlock(&pci_slot_mutex);
|
mutex_unlock(&pci_slot_mutex);
|
||||||
return slot;
|
return slot;
|
||||||
err:
|
err:
|
||||||
kfree(slot);
|
|
||||||
slot = ERR_PTR(err);
|
slot = ERR_PTR(err);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue