From: Kaike Wan <kaike.wan@xxxxxxxxx> This patch is implemented to address the concerns raised in: https://marc.info/?l=linux-rdma&m=158101337614772&w=2 The hfi1 driver dynammically allocates a struct device to represent the cdev in sysfs and devtmpfs (/dev/hfi1_x). On the other hand, the hfi1_devdata already contains a struct device in its ibdev field (hfi1_devdata.verbs_dev.rdi.ibdev.dev), and it is therefore possible to eliminate the dynamical allocation when creating the cdev. Since each device could be added to the sysfs only once and the function device_add() is already called for the ibdev in ib_register_device(), the function cdev_device_add() could not be used to create the cdev, even though the hfi1_devdata contains both cdev and ibdev in the same structure. This patch eliminates the dynamic allocation by creating the cdev first, setting up the ibdev, and then calling the ib_register_device() to add the device to sysfs and devtmpfs. Reviewed-by: Mike Marciniszyn <mike.marciniszyn@xxxxxxxxx> Reviewed-by: Dennis Dalessandro <dennis.dalessandro@xxxxxxxxx> Signed-off-by: Kaike Wan <kaike.wan@xxxxxxxxx> Signed-off-by: Dennis Dalessandro <dennis.dalessandro@xxxxxxxxx> --- drivers/infiniband/hw/hfi1/device.c | 23 ++++++++++++++++------- drivers/infiniband/hw/hfi1/file_ops.c | 5 ++--- drivers/infiniband/hw/hfi1/hfi.h | 1 - drivers/infiniband/hw/hfi1/init.c | 18 +++++++++--------- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/drivers/infiniband/hw/hfi1/device.c b/drivers/infiniband/hw/hfi1/device.c index bbb6069..4e1ad5f 100644 --- a/drivers/infiniband/hw/hfi1/device.c +++ b/drivers/infiniband/hw/hfi1/device.c @@ -79,10 +79,12 @@ int hfi1_cdev_init(int minor, const char *name, goto done; } - if (user_accessible) - device = device_create(user_class, NULL, dev, NULL, "%s", name); - else + if (user_accessible) { + device = kobj_to_dev(parent); + device->devt = dev; + } else { device = device_create(class, NULL, dev, NULL, "%s", name); + } if (IS_ERR(device)) { ret = PTR_ERR(device); @@ -92,20 +94,27 @@ int hfi1_cdev_init(int minor, const char *name, cdev_del(cdev); } done: - *devp = device; + if (devp) + *devp = device; return ret; } +/* + * The pointer devp will be provided only if *devp is allocated + * dynamically, as shown in device_create(). + */ void hfi1_cdev_cleanup(struct cdev *cdev, struct device **devp) { - struct device *device = *devp; + struct device *device = NULL; + if (devp) + device = *devp; if (device) { device_unregister(device); *devp = NULL; - - cdev_del(cdev); } + /* This will also decrement its parent's refcount */ + cdev_del(cdev); } static const char *hfi1_class_name = "hfi1"; diff --git a/drivers/infiniband/hw/hfi1/file_ops.c b/drivers/infiniband/hw/hfi1/file_ops.c index e7fdd70..38827e4 100644 --- a/drivers/infiniband/hw/hfi1/file_ops.c +++ b/drivers/infiniband/hw/hfi1/file_ops.c @@ -1682,8 +1682,7 @@ static int ctxt_reset(struct hfi1_ctxtdata *uctxt) static void user_remove(struct hfi1_devdata *dd) { - - hfi1_cdev_cleanup(&dd->user_cdev, &dd->user_device); + hfi1_cdev_cleanup(&dd->user_cdev, NULL); } static int user_add(struct hfi1_devdata *dd) @@ -1693,7 +1692,7 @@ static int user_add(struct hfi1_devdata *dd) snprintf(name, sizeof(name), "%s_%d", class_name(), dd->unit); ret = hfi1_cdev_init(dd->unit, name, &hfi1_file_ops, - &dd->user_cdev, &dd->user_device, + &dd->user_cdev, NULL, true, &dd->verbs_dev.rdi.ibdev.dev.kobj); if (ret) user_remove(dd); diff --git a/drivers/infiniband/hw/hfi1/hfi.h b/drivers/infiniband/hw/hfi1/hfi.h index b06c259..8e63b11 100644 --- a/drivers/infiniband/hw/hfi1/hfi.h +++ b/drivers/infiniband/hw/hfi1/hfi.h @@ -1084,7 +1084,6 @@ struct hfi1_devdata { struct cdev user_cdev; struct cdev diag_cdev; struct cdev ui_cdev; - struct device *user_device; struct device *diag_device; struct device *ui_device; diff --git a/drivers/infiniband/hw/hfi1/init.c b/drivers/infiniband/hw/hfi1/init.c index 3759d92..3c605dd 100644 --- a/drivers/infiniband/hw/hfi1/init.c +++ b/drivers/infiniband/hw/hfi1/init.c @@ -1665,6 +1665,10 @@ static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent) /* setup vnic */ hfi1_vnic_setup(dd); + + j = hfi1_device_create(dd); + if (j) + dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j); ret = hfi1_register_ib_device(dd); @@ -1680,10 +1684,6 @@ static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent) hfi1_dbg_ibdev_init(&dd->verbs_dev); } - j = hfi1_device_create(dd); - if (j) - dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j); - if (initfail || ret) { msix_clean_up_interrupts(dd); stop_timers(dd); @@ -1700,11 +1700,11 @@ static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent) ppd->link_wq = NULL; } } - if (!j) - hfi1_device_remove(dd); if (!ret) hfi1_unregister_ib_device(dd); hfi1_vnic_cleanup(dd); + if (!j) + hfi1_device_remove(dd); postinit_cleanup(dd); if (initfail) ret = initfail; @@ -1740,9 +1740,6 @@ static void remove_one(struct pci_dev *pdev) /* close debugfs files before ib unregister */ hfi1_dbg_ibdev_exit(&dd->verbs_dev); - /* remove the /dev hfi1 interface */ - hfi1_device_remove(dd); - /* wait for existing user space clients to finish */ wait_for_clients(dd); @@ -1751,6 +1748,9 @@ static void remove_one(struct pci_dev *pdev) /* cleanup vnic */ hfi1_vnic_cleanup(dd); + + /* remove the /dev hfi1 interface */ + hfi1_device_remove(dd); /* * Disable the IB link, disable interrupts on the device,