Skip to content

Commit 8a94644

Browse files
QiushiWubjorn-helgaas
authored andcommitted
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(). b8eb718 ("net-sysfs: Fix reference count leak in rx|netdev_queue_add_kobject") fixed a similar problem. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Qiushi Wu <[email protected]> Signed-off-by: Bjorn Helgaas <[email protected]>
1 parent b3a9e3b commit 8a94644

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

drivers/pci/slot.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,16 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
268268
slot_name = make_slot_name(name);
269269
if (!slot_name) {
270270
err = -ENOMEM;
271+
kfree(slot);
271272
goto err;
272273
}
273274

274275
err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
275276
"%s", slot_name);
276-
if (err)
277+
if (err) {
278+
kobject_put(&slot->kobj);
277279
goto err;
280+
}
278281

279282
INIT_LIST_HEAD(&slot->list);
280283
list_add(&slot->list, &parent->slots);
@@ -293,7 +296,6 @@ struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
293296
mutex_unlock(&pci_slot_mutex);
294297
return slot;
295298
err:
296-
kfree(slot);
297299
slot = ERR_PTR(err);
298300
goto out;
299301
}

0 commit comments

Comments
 (0)