Skip to content

Commit

Permalink
IB: Remove sysfs files before unregistering device
Browse files Browse the repository at this point in the history
Move the ib_device_unregister_sysfs() call from ib_dealloc_device() to
ib_unregister_device().  The old code allows device unregister to
proceed even if some sysfs files are open, which leaves a window where
userspace can open a file before a device is removed but then end up
reading the file after the device is removed, which leads to various
kernel crashes either because the device data structure is freed or
because the low-level driver code is gone after module removal.

By not returning from ib_unregister_device() until after all sysfs
entries are removed, we make sure that data structures and/or module
code is not freed until after all sysfs access is done.

Reported-by: Jack Morgenstein <jackm@dev.mellanox.co.il>
Signed-off-by: Roland Dreier <rolandd@cisco.com>
  • Loading branch information
Roland Dreier committed Feb 25, 2009
1 parent f3b8436 commit 9206dff
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion drivers/infiniband/core/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void ib_dealloc_device(struct ib_device *device)

BUG_ON(device->reg_state != IB_DEV_UNREGISTERED);

ib_device_unregister_sysfs(device);
kobject_put(&device->dev.kobj);
}
EXPORT_SYMBOL(ib_dealloc_device);

Expand Down Expand Up @@ -348,6 +348,8 @@ void ib_unregister_device(struct ib_device *device)

mutex_unlock(&device_mutex);

ib_device_unregister_sysfs(device);

spin_lock_irqsave(&device->client_data_lock, flags);
list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
kfree(context);
Expand Down
3 changes: 3 additions & 0 deletions drivers/infiniband/core/sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,9 @@ void ib_device_unregister_sysfs(struct ib_device *device)
struct kobject *p, *t;
struct ib_port *port;

/* Hold kobject until ib_dealloc_device() */
kobject_get(&device->dev.kobj);

list_for_each_entry_safe(p, t, &device->port_list, entry) {
list_del(&p->entry);
port = container_of(p, struct ib_port, kobj);
Expand Down

0 comments on commit 9206dff

Please sign in to comment.