IB/core: Make ib_alloc_device init the kobject

This gets rid of the weird in-between state where struct ib_device
was allocated but the kobject didn't work.

Consequently ib_device_release is now guaranteed to be called in
all situations and we needn't duplicate its kfrees on error paths.

Signed-off-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
Signed-off-by: Doug Ledford <dledford@redhat.com>
This commit is contained in:
Jason Gunthorpe 2015-08-04 15:23:34 -06:00 коммит произвёл Doug Ledford
Родитель e999869548
Коммит 55aeed0654
4 изменённых файлов: 68 добавлений и 87 удалений

Просмотреть файл

@ -351,10 +351,10 @@ static void ib_cache_setup_one(struct ib_device *device)
rwlock_init(&device->cache.lock); rwlock_init(&device->cache.lock);
device->cache.pkey_cache = device->cache.pkey_cache =
kmalloc(sizeof *device->cache.pkey_cache * kzalloc(sizeof *device->cache.pkey_cache *
(rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL); (rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL);
device->cache.gid_cache = device->cache.gid_cache =
kmalloc(sizeof *device->cache.gid_cache * kzalloc(sizeof *device->cache.gid_cache *
(rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL); (rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL);
device->cache.lmc_cache = kmalloc(sizeof *device->cache.lmc_cache * device->cache.lmc_cache = kmalloc(sizeof *device->cache.lmc_cache *
@ -369,11 +369,8 @@ static void ib_cache_setup_one(struct ib_device *device)
goto err; goto err;
} }
for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p) { for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
device->cache.pkey_cache[p] = NULL;
device->cache.gid_cache [p] = NULL;
ib_cache_update(device, p + rdma_start_port(device)); ib_cache_update(device, p + rdma_start_port(device));
}
INIT_IB_EVENT_HANDLER(&device->cache.event_handler, INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
device, ib_cache_event); device, ib_cache_event);

Просмотреть файл

@ -43,9 +43,6 @@ int ib_device_register_sysfs(struct ib_device *device,
u8, struct kobject *)); u8, struct kobject *));
void ib_device_unregister_sysfs(struct ib_device *device); void ib_device_unregister_sysfs(struct ib_device *device);
int ib_sysfs_setup(void);
void ib_sysfs_cleanup(void);
int ib_cache_setup(void); int ib_cache_setup(void);
void ib_cache_cleanup(void); void ib_cache_cleanup(void);

Просмотреть файл

@ -165,6 +165,35 @@ static int alloc_name(char *name)
return 0; return 0;
} }
static void ib_device_release(struct device *device)
{
struct ib_device *dev = container_of(device, struct ib_device, dev);
kfree(dev->port_immutable);
kfree(dev);
}
static int ib_device_uevent(struct device *device,
struct kobj_uevent_env *env)
{
struct ib_device *dev = container_of(device, struct ib_device, dev);
if (add_uevent_var(env, "NAME=%s", dev->name))
return -ENOMEM;
/*
* It would be nice to pass the node GUID with the event...
*/
return 0;
}
static struct class ib_class = {
.name = "infiniband",
.dev_release = ib_device_release,
.dev_uevent = ib_device_uevent,
};
/** /**
* ib_alloc_device - allocate an IB device struct * ib_alloc_device - allocate an IB device struct
* @size:size of structure to allocate * @size:size of structure to allocate
@ -177,9 +206,27 @@ static int alloc_name(char *name)
*/ */
struct ib_device *ib_alloc_device(size_t size) struct ib_device *ib_alloc_device(size_t size)
{ {
BUG_ON(size < sizeof (struct ib_device)); struct ib_device *device;
return kzalloc(size, GFP_KERNEL); if (WARN_ON(size < sizeof(struct ib_device)))
return NULL;
device = kzalloc(size, GFP_KERNEL);
if (!device)
return NULL;
device->dev.class = &ib_class;
device_initialize(&device->dev);
dev_set_drvdata(&device->dev, device);
INIT_LIST_HEAD(&device->event_handler_list);
spin_lock_init(&device->event_handler_lock);
spin_lock_init(&device->client_data_lock);
INIT_LIST_HEAD(&device->client_data_list);
INIT_LIST_HEAD(&device->port_list);
return device;
} }
EXPORT_SYMBOL(ib_alloc_device); EXPORT_SYMBOL(ib_alloc_device);
@ -191,13 +238,8 @@ EXPORT_SYMBOL(ib_alloc_device);
*/ */
void ib_dealloc_device(struct ib_device *device) void ib_dealloc_device(struct ib_device *device)
{ {
if (device->reg_state == IB_DEV_UNINITIALIZED) { WARN_ON(device->reg_state != IB_DEV_UNREGISTERED &&
kfree(device); device->reg_state != IB_DEV_UNINITIALIZED);
return;
}
BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);
kobject_put(&device->dev.kobj); kobject_put(&device->dev.kobj);
} }
EXPORT_SYMBOL(ib_dealloc_device); EXPORT_SYMBOL(ib_dealloc_device);
@ -235,7 +277,7 @@ static int verify_immutable(const struct ib_device *dev, u8 port)
static int read_port_immutable(struct ib_device *device) static int read_port_immutable(struct ib_device *device)
{ {
int ret = -ENOMEM; int ret;
u8 start_port = rdma_start_port(device); u8 start_port = rdma_start_port(device);
u8 end_port = rdma_end_port(device); u8 end_port = rdma_end_port(device);
u8 port; u8 port;
@ -251,26 +293,18 @@ static int read_port_immutable(struct ib_device *device)
* (end_port + 1), * (end_port + 1),
GFP_KERNEL); GFP_KERNEL);
if (!device->port_immutable) if (!device->port_immutable)
goto err; return -ENOMEM;
for (port = start_port; port <= end_port; ++port) { for (port = start_port; port <= end_port; ++port) {
ret = device->get_port_immutable(device, port, ret = device->get_port_immutable(device, port,
&device->port_immutable[port]); &device->port_immutable[port]);
if (ret) if (ret)
goto err;
if (verify_immutable(device, port)) {
ret = -EINVAL;
goto err;
}
}
ret = 0;
goto out;
err:
kfree(device->port_immutable);
out:
return ret; return ret;
if (verify_immutable(device, port))
return -EINVAL;
}
return 0;
} }
/** /**
@ -301,11 +335,6 @@ int ib_register_device(struct ib_device *device,
goto out; goto out;
} }
INIT_LIST_HEAD(&device->event_handler_list);
INIT_LIST_HEAD(&device->client_data_list);
spin_lock_init(&device->event_handler_lock);
spin_lock_init(&device->client_data_lock);
ret = read_port_immutable(device); ret = read_port_immutable(device);
if (ret) { if (ret) {
printk(KERN_WARNING "Couldn't create per port immutable data %s\n", printk(KERN_WARNING "Couldn't create per port immutable data %s\n",
@ -317,7 +346,6 @@ int ib_register_device(struct ib_device *device,
if (ret) { if (ret) {
printk(KERN_WARNING "Couldn't register device %s with driver model\n", printk(KERN_WARNING "Couldn't register device %s with driver model\n",
device->name); device->name);
kfree(device->port_immutable);
goto out; goto out;
} }
@ -834,7 +862,7 @@ static int __init ib_core_init(void)
if (!ib_wq) if (!ib_wq)
return -ENOMEM; return -ENOMEM;
ret = ib_sysfs_setup(); ret = class_register(&ib_class);
if (ret) { if (ret) {
printk(KERN_WARNING "Couldn't create InfiniBand device class\n"); printk(KERN_WARNING "Couldn't create InfiniBand device class\n");
goto err; goto err;
@ -858,7 +886,7 @@ err_nl:
ibnl_cleanup(); ibnl_cleanup();
err_sysfs: err_sysfs:
ib_sysfs_cleanup(); class_unregister(&ib_class);
err: err:
destroy_workqueue(ib_wq); destroy_workqueue(ib_wq);
@ -869,7 +897,7 @@ static void __exit ib_core_cleanup(void)
{ {
ib_cache_cleanup(); ib_cache_cleanup();
ibnl_cleanup(); ibnl_cleanup();
ib_sysfs_cleanup(); class_unregister(&ib_class);
/* Make sure that any pending umem accounting work is done. */ /* Make sure that any pending umem accounting work is done. */
destroy_workqueue(ib_wq); destroy_workqueue(ib_wq);
} }

Просмотреть файл

@ -457,29 +457,6 @@ static struct kobj_type port_type = {
.default_attrs = port_default_attrs .default_attrs = port_default_attrs
}; };
static void ib_device_release(struct device *device)
{
struct ib_device *dev = container_of(device, struct ib_device, dev);
kfree(dev->port_immutable);
kfree(dev);
}
static int ib_device_uevent(struct device *device,
struct kobj_uevent_env *env)
{
struct ib_device *dev = container_of(device, struct ib_device, dev);
if (add_uevent_var(env, "NAME=%s", dev->name))
return -ENOMEM;
/*
* It would be nice to pass the node GUID with the event...
*/
return 0;
}
static struct attribute ** static struct attribute **
alloc_group_attrs(ssize_t (*show)(struct ib_port *, alloc_group_attrs(ssize_t (*show)(struct ib_port *,
struct port_attribute *, char *buf), struct port_attribute *, char *buf),
@ -702,12 +679,6 @@ static struct device_attribute *ib_class_attributes[] = {
&dev_attr_node_desc &dev_attr_node_desc
}; };
static struct class ib_class = {
.name = "infiniband",
.dev_release = ib_device_release,
.dev_uevent = ib_device_uevent,
};
/* Show a given an attribute in the statistics group */ /* Show a given an attribute in the statistics group */
static ssize_t show_protocol_stat(const struct device *device, static ssize_t show_protocol_stat(const struct device *device,
struct device_attribute *attr, char *buf, struct device_attribute *attr, char *buf,
@ -846,14 +817,12 @@ int ib_device_register_sysfs(struct ib_device *device,
int ret; int ret;
int i; int i;
class_dev->class = &ib_class; device->dev.parent = device->dma_device;
class_dev->parent = device->dma_device; ret = dev_set_name(class_dev, "%s", device->name);
dev_set_name(class_dev, "%s", device->name); if (ret)
dev_set_drvdata(class_dev, device); return ret;
INIT_LIST_HEAD(&device->port_list); ret = device_add(class_dev);
ret = device_register(class_dev);
if (ret) if (ret)
goto err; goto err;
@ -916,13 +885,3 @@ void ib_device_unregister_sysfs(struct ib_device *device)
device_unregister(&device->dev); device_unregister(&device->dev);
} }
int ib_sysfs_setup(void)
{
return class_register(&ib_class);
}
void ib_sysfs_cleanup(void)
{
class_unregister(&ib_class);
}