Track create and destroy operations of QP objects.
Reviewed-by: Mark Bloch <markb@mellanox.com>
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
Reviewed-by: Steve Wise <swise@opengridcomputing.com>
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
 /* RDMA device netlink */
 void nldev_init(void);
 void nldev_exit(void);
+
+static inline struct ib_qp *_ib_create_qp(struct ib_device *dev,
+                                         struct ib_pd *pd,
+                                         struct ib_qp_init_attr *attr,
+                                         struct ib_udata *udata)
+{
+       struct ib_qp *qp;
+
+       qp = dev->create_qp(pd, attr, udata);
+       if (IS_ERR(qp))
+               return qp;
+
+       qp->device = dev;
+       qp->pd = pd;
+       /*
+        * We don't track XRC QPs for now, because they don't have PD
+        * and more importantly they are created internaly by driver,
+        * see mlx5 create_dev_resources() as an example.
+        */
+       if (attr->qp_type < IB_QPT_XRC_INI) {
+               qp->res.type = RDMA_RESTRACK_QP;
+               rdma_restrack_add(&qp->res);
+       } else
+               qp->res.valid = false;
+
+       return qp;
+}
 #endif /* _CORE_PRIV_H */
 
        if (cmd->qp_type == IB_QPT_XRC_TGT)
                qp = ib_create_qp(pd, &attr);
        else
-               qp = device->create_qp(pd, &attr, uhw);
+               qp = _ib_create_qp(device, pd, &attr, uhw);
 
        if (IS_ERR(qp)) {
                ret = PTR_ERR(qp);
                        goto err_cb;
 
                qp->real_qp       = qp;
-               qp->device        = device;
                qp->pd            = pd;
                qp->send_cq       = attr.send_cq;
                qp->recv_cq       = attr.recv_cq;
 
        if (qp_init_attr->cap.max_rdma_ctxs)
                rdma_rw_init_qp(device, qp_init_attr);
 
-       qp = device->create_qp(pd, qp_init_attr, NULL);
+       qp = _ib_create_qp(device, pd, qp_init_attr, NULL);
        if (IS_ERR(qp))
                return qp;
 
                return ERR_PTR(ret);
        }
 
-       qp->device     = device;
        qp->real_qp    = qp;
        qp->uobject    = NULL;
        qp->qp_type    = qp_init_attr->qp_type;
                        atomic_inc(&qp_init_attr->srq->usecnt);
        }
 
-       qp->pd      = pd;
        qp->send_cq = qp_init_attr->send_cq;
        qp->xrcd    = NULL;
 
        if (!qp->uobject)
                rdma_rw_cleanup_mrs(qp);
 
+       rdma_restrack_del(&qp->res);
        ret = qp->device->destroy_qp(qp);
        if (!ret) {
                if (pd)