RDMA/rxe: Make add/drop key/index APIs type safe
authorBob Pearson <rpearsonhpe@gmail.com>
Wed, 16 Dec 2020 23:15:48 +0000 (17:15 -0600)
committerJason Gunthorpe <jgg@nvidia.com>
Tue, 12 Jan 2021 20:35:38 +0000 (16:35 -0400)
Replace 'void *' parameters with 'struct rxe_pool_entry *' and use a macro
to allow:
   rxe_add_index,
   rxe_drop_index,
   rxe_add_key,
   rxe_drop_key and
   rxe_add_to_pool
APIs to be type safe against changing the position of pelem in the
objects.

Link: https://lore.kernel.org/r/20201216231550.27224-6-rpearson@hpe.com
Signed-off-by: Bob Pearson <rpearson@hpe.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
drivers/infiniband/sw/rxe/rxe_pool.c
drivers/infiniband/sw/rxe/rxe_pool.h
drivers/infiniband/sw/rxe/rxe_verbs.c

index 2873ecf..c8a6d0d 100644 (file)
@@ -266,9 +266,8 @@ out:
        return;
 }
 
-void rxe_add_key(void *arg, void *key)
+void __rxe_add_key(struct rxe_pool_entry *elem, void *key)
 {
-       struct rxe_pool_entry *elem = arg;
        struct rxe_pool *pool = elem->pool;
        unsigned long flags;
 
@@ -278,9 +277,8 @@ void rxe_add_key(void *arg, void *key)
        write_unlock_irqrestore(&pool->pool_lock, flags);
 }
 
-void rxe_drop_key(void *arg)
+void __rxe_drop_key(struct rxe_pool_entry *elem)
 {
-       struct rxe_pool_entry *elem = arg;
        struct rxe_pool *pool = elem->pool;
        unsigned long flags;
 
@@ -289,9 +287,8 @@ void rxe_drop_key(void *arg)
        write_unlock_irqrestore(&pool->pool_lock, flags);
 }
 
-void rxe_add_index(void *arg)
+void __rxe_add_index(struct rxe_pool_entry *elem)
 {
-       struct rxe_pool_entry *elem = arg;
        struct rxe_pool *pool = elem->pool;
        unsigned long flags;
 
@@ -301,9 +298,8 @@ void rxe_add_index(void *arg)
        write_unlock_irqrestore(&pool->pool_lock, flags);
 }
 
-void rxe_drop_index(void *arg)
+void __rxe_drop_index(struct rxe_pool_entry *elem)
 {
-       struct rxe_pool_entry *elem = arg;
        struct rxe_pool *pool = elem->pool;
        unsigned long flags;
 
@@ -356,7 +352,7 @@ out_put_pool:
        return NULL;
 }
 
-int rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem)
+int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem)
 {
        unsigned long flags;
 
index b37df61..5db0bdd 100644 (file)
@@ -110,23 +110,33 @@ void rxe_pool_cleanup(struct rxe_pool *pool);
 void *rxe_alloc(struct rxe_pool *pool);
 
 /* connect already allocated object to pool */
-int rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem);
+int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem);
+
+#define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->pelem)
 
 /* assign an index to an indexed object and insert object into
  *  pool's rb tree
  */
-void rxe_add_index(void *elem);
+void __rxe_add_index(struct rxe_pool_entry *elem);
+
+#define rxe_add_index(obj) __rxe_add_index(&(obj)->pelem)
 
 /* drop an index and remove object from rb tree */
-void rxe_drop_index(void *elem);
+void __rxe_drop_index(struct rxe_pool_entry *elem);
+
+#define rxe_drop_index(obj) __rxe_drop_index(&(obj)->pelem)
 
 /* assign a key to a keyed object and insert object into
  *  pool's rb tree
  */
-void rxe_add_key(void *elem, void *key);
+void __rxe_add_key(struct rxe_pool_entry *elem, void *key);
+
+#define rxe_add_key(obj, key) __rxe_add_key(&(obj)->pelem, key)
 
 /* remove elem from rb tree */
-void rxe_drop_key(void *elem);
+void __rxe_drop_key(struct rxe_pool_entry *elem);
+
+#define rxe_drop_key(obj) __rxe_drop_key(&(obj)->pelem)
 
 /* lookup an indexed object from index. takes a reference on object */
 void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
index a031514..7483a33 100644 (file)
@@ -106,12 +106,12 @@ static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev,
        return IB_LINK_LAYER_ETHERNET;
 }
 
-static int rxe_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata)
+static int rxe_alloc_ucontext(struct ib_ucontext *ibuc, struct ib_udata *udata)
 {
-       struct rxe_dev *rxe = to_rdev(uctx->device);
-       struct rxe_ucontext *uc = to_ruc(uctx);
+       struct rxe_dev *rxe = to_rdev(ibuc->device);
+       struct rxe_ucontext *uc = to_ruc(ibuc);
 
-       return rxe_add_to_pool(&rxe->uc_pool, &uc->pelem);
+       return rxe_add_to_pool(&rxe->uc_pool, uc);
 }
 
 static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)
@@ -145,7 +145,7 @@ static int rxe_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
        struct rxe_dev *rxe = to_rdev(ibpd->device);
        struct rxe_pd *pd = to_rpd(ibpd);
 
-       return rxe_add_to_pool(&rxe->pd_pool, &pd->pelem);
+       return rxe_add_to_pool(&rxe->pd_pool, pd);
 }
 
 static int rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
@@ -169,7 +169,7 @@ static int rxe_create_ah(struct ib_ah *ibah,
        if (err)
                return err;
 
-       err = rxe_add_to_pool(&rxe->ah_pool, &ah->pelem);
+       err = rxe_add_to_pool(&rxe->ah_pool, ah);
        if (err)
                return err;
 
@@ -273,7 +273,7 @@ static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init,
        if (err)
                goto err1;
 
-       err = rxe_add_to_pool(&rxe->srq_pool, &srq->pelem);
+       err = rxe_add_to_pool(&rxe->srq_pool, srq);
        if (err)
                goto err1;
 
@@ -774,7 +774,7 @@ static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
        if (err)
                return err;
 
-       return rxe_add_to_pool(&rxe->cq_pool, &cq->pelem);
+       return rxe_add_to_pool(&rxe->cq_pool, cq);
 }
 
 static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)