return ret;
 }
 
+static void srpt_free_sdev(struct kref *refcnt)
+{
+       struct srpt_device *sdev = container_of(refcnt, typeof(*sdev), refcnt);
+
+       kfree(sdev);
+}
+
+static void srpt_sdev_put(struct srpt_device *sdev)
+{
+       kref_put(&sdev->refcnt, srpt_free_sdev);
+}
+
 /**
  * srpt_add_one - InfiniBand device addition callback function
  * @device: Describes a HCA.
        if (!sdev)
                return -ENOMEM;
 
+       kref_init(&sdev->refcnt);
        sdev->device = device;
        mutex_init(&sdev->sdev_mutex);
 
        srpt_free_srq(sdev);
        ib_dealloc_pd(sdev->pd);
 free_dev:
-       kfree(sdev);
+       srpt_sdev_put(sdev);
        pr_info("%s(%s) failed.\n", __func__, dev_name(&device->dev));
        return ret;
 }
 
        ib_dealloc_pd(sdev->pd);
 
-       kfree(sdev);
+       srpt_sdev_put(sdev);
 }
 
 static struct ib_client srpt_client = {
 
 
 /**
  * struct srpt_device - information associated by SRPT with a single HCA
+ * @refcnt:       Reference count for this device.
  * @device:        Backpointer to the struct ib_device managed by the IB core.
  * @pd:            IB protection domain.
  * @lkey:          L_Key (local key) with write access to all local memory.
  * @port:          Information about the ports owned by this HCA.
  */
 struct srpt_device {
+       struct kref             refcnt;
        struct ib_device        *device;
        struct ib_pd            *pd;
        u32                     lkey;