From: Parav Pandit <parav@xxxxxxxxxxxx> Introduce and use helper functions for device register/unregister notifications to clients and for cleaning up client contexts. Use these helpers in subsequent patch during namespace related netlink command(s). Signed-off-by: Parav Pandit <parav@xxxxxxxxxxxx> Signed-off-by: Leon Romanovsky <leonro@xxxxxxxxxxxx> --- drivers/infiniband/core/device.c | 84 +++++++++++++++++++------------- 1 file changed, 51 insertions(+), 33 deletions(-) diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c index 6d8f3d87633c..3ccab52ab243 100644 --- a/drivers/infiniband/core/device.c +++ b/drivers/infiniband/core/device.c @@ -621,6 +621,19 @@ static int setup_device(struct ib_device *device) return ret; } +static void register_add_clients(struct ib_device *device) +{ + struct ib_client *client; + + list_for_each_entry(client, &client_list, list) + if (!add_client_context(device, client) && client->add) + client->add(device); + + down_write(&ib_lists_rwsem); + list_add_tail(&device->core_list, &ib_device_list); + up_write(&ib_lists_rwsem); +} + /** * ib_register_device - Register an IB device with IB core * @device:Device to register @@ -633,7 +646,6 @@ static int setup_device(struct ib_device *device) int ib_register_device(struct ib_device *device, const char *name) { int ret; - struct ib_client *client; setup_dma_device(device); @@ -684,13 +696,7 @@ int ib_register_device(struct ib_device *device, const char *name) device->reg_state = IB_DEV_REGISTERED; - list_for_each_entry(client, &client_list, list) - if (!add_client_context(device, client) && client->add) - client->add(device); - - down_write(&ib_lists_rwsem); - list_add_tail(&device->core_list, &ib_device_list); - up_write(&ib_lists_rwsem); + register_add_clients(device); mutex_unlock(&ib_device_mutex); return 0; @@ -704,6 +710,41 @@ int ib_register_device(struct ib_device *device, const char *name) } EXPORT_SYMBOL(ib_register_device); +static void unregister_remove_clients(struct ib_device *device) +{ + struct ib_client_data *context; + + down_write(&ib_lists_rwsem); + list_del(&device->core_list); + write_lock_irq(&device->client_data_lock); + list_for_each_entry(context, &device->client_data_list, list) + context->going_down = true; + write_unlock_irq(&device->client_data_lock); + downgrade_write(&ib_lists_rwsem); + + list_for_each_entry(context, &device->client_data_list, list) { + if (context->client->remove) + context->client->remove(device, context->data); + } + up_read(&ib_lists_rwsem); +} + +static void free_client_contexts(struct ib_device *device) +{ + struct ib_client_data *context, *tmp; + unsigned long flags; + + down_write(&ib_lists_rwsem); + write_lock_irqsave(&device->client_data_lock, flags); + list_for_each_entry_safe(context, tmp, &device->client_data_list, + list) { + list_del(&context->list); + kfree(context); + } + write_unlock_irqrestore(&device->client_data_lock, flags); + up_write(&ib_lists_rwsem); +} + /** * ib_unregister_device - Unregister an IB device * @device:Device to unregister @@ -712,9 +753,6 @@ EXPORT_SYMBOL(ib_register_device); */ void ib_unregister_device(struct ib_device *device) { - struct ib_client_data *context, *tmp; - unsigned long flags; - /* * Wait for all netlink command callers to finish working on the * device. @@ -724,19 +762,7 @@ void ib_unregister_device(struct ib_device *device) mutex_lock(&ib_device_mutex); - down_write(&ib_lists_rwsem); - list_del(&device->core_list); - write_lock_irq(&device->client_data_lock); - list_for_each_entry(context, &device->client_data_list, list) - context->going_down = true; - write_unlock_irq(&device->client_data_lock); - downgrade_write(&ib_lists_rwsem); - - list_for_each_entry(context, &device->client_data_list, list) { - if (context->client->remove) - context->client->remove(device, context->data); - } - up_read(&ib_lists_rwsem); + unregister_remove_clients(device); rdma_compatdev_remove(device); ib_device_unregister_sysfs(device); @@ -749,15 +775,7 @@ void ib_unregister_device(struct ib_device *device) ib_security_destroy_port_pkey_list(device); kfree(device->port_pkey_list); - down_write(&ib_lists_rwsem); - write_lock_irqsave(&device->client_data_lock, flags); - list_for_each_entry_safe(context, tmp, &device->client_data_list, - list) { - list_del(&context->list); - kfree(context); - } - write_unlock_irqrestore(&device->client_data_lock, flags); - up_write(&ib_lists_rwsem); + free_client_contexts(device); device->reg_state = IB_DEV_UNREGISTERED; } -- 2.19.1