From patchwork Thu Dec 16 23:31:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 12683001 X-Patchwork-Delegate: jgg@ziepe.ca Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44ED6C433EF for ; Thu, 16 Dec 2021 23:34:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229728AbhLPXeA (ORCPT ); Thu, 16 Dec 2021 18:34:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229662AbhLPXeA (ORCPT ); Thu, 16 Dec 2021 18:34:00 -0500 Received: from mail-ot1-x333.google.com (mail-ot1-x333.google.com [IPv6:2607:f8b0:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 011A9C061574 for ; Thu, 16 Dec 2021 15:34:00 -0800 (PST) Received: by mail-ot1-x333.google.com with SMTP id h19-20020a9d3e53000000b0056547b797b2so792846otg.4 for ; Thu, 16 Dec 2021 15:33:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LRYKZjFdSjtdG8kiotD0M2CMN3lOL458vgDrfV2EU2k=; b=La92p/CIglQJkSB5zyZiZ5gFCTIbphu5yiwy/a9rWquZMtp5SFYcODAoGFNqZvSJaH Po0FS6RWwCn1d3nOPnDdypUoLOJnK4VdclPJ9qR1jBrbroPTCvloS2tCcUllNqwifOJF hWPOk+o3DLiXVzV1qHbAoJrtk02mTxcue8usH2kBF+6CVLA8kv9hMleQC6owbhg57QdI W76xa9Rv3ImNfQJTUAAsZedPqnNLPeU/by9snDDA5K+6YNHcfAQdtGGZnjCZimcOJmFp zSqK+FaKJ57w6ejMkLk0qsRQV33lsKoTZLmQz/EuqCTFh++g4S0dVeKJJAkPzaoWJ0xv Xv6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LRYKZjFdSjtdG8kiotD0M2CMN3lOL458vgDrfV2EU2k=; b=wq9j/EpBsyZFCiIqf4tk+ODdIPL4tI2CmkYP6B6hQ4MAlLc5/isbt9ebGa+0B3y98s POJ6yFY8tfDSGeNKvstnfjPyASRpzxli9XyjFWxpDKD5I00VtTQDptGnAIYJiUCi9tlu 0OsWwu+etJ58iynDQKAlXr7zaiSw0Ut0wayu0YbkRKYg8HpjCDGgAmtfDX+KH6TupXC3 sKZxuWB83rtEqgh3nGbLoP6/Gh0k9utoAuYAUtlfTZE0wcsJhSQQvbMla/LBN4dihqVL C6jSSoblY9ChNh0Bj8yzXx5IJSZwwAXiroarhgs+gtEJzpJ8j5CvMOXo50ltZzZ2HoGr BUiA== X-Gm-Message-State: AOAM530LSJDJuuPjcukVYeHre3xkSTOwYsp+f5vhJqD/Y1zWv3k07acf 16kB5qY805IKBPBI+FC34zQ= X-Google-Smtp-Source: ABdhPJyO60MiKdC2lYcNRW+lh6101H1UOu3Xpm/Ch2MbEEL21fvOrrCP0Czz9RuMiKdfcdkvrYyApw== X-Received: by 2002:a9d:6257:: with SMTP id i23mr334959otk.145.1639697639250; Thu, 16 Dec 2021 15:33:59 -0800 (PST) Received: from ubuntu-21.tx.rr.com (2603-8081-140c-1a00-ec41-d089-dfdb-6fb5.res6.spectrum.com. [2603:8081:140c:1a00:ec41:d089:dfdb:6fb5]) by smtp.googlemail.com with ESMTPSA id w19sm1253888oih.44.2021.12.16.15.33.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Dec 2021 15:33:58 -0800 (PST) From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next v8 1/8] RDMA/rxe: Replace RB tree by xarray for indexes Date: Thu, 16 Dec 2021 17:31:55 -0600 Message-Id: <20211216233201.14893-2-rpearsonhpe@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211216233201.14893-1-rpearsonhpe@gmail.com> References: <20211216233201.14893-1-rpearsonhpe@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Currently the rxe driver uses red-black trees to add indices and keys to the rxe object pool. Linux xarrays provide a better way to implement the same functionality for indices but not keys. This patch replaces red-black trees by xarrays for indexed objects. Since caller managed locks for indexed objects are not used these APIs are deleted as well. To avoid double locking since xarray already includes a spinlock replace the rxe_pool rwlock by the spinlock included in xarray. The RDMA objects are created and destroyed by verbs calls from rdma_core but are looked up from indices or keys from soft IRQs so _bh style locks are the correct type to use. Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe.c | 100 ++---------- drivers/infiniband/sw/rxe/rxe_mcast.c | 6 +- drivers/infiniband/sw/rxe/rxe_mr.c | 1 - drivers/infiniband/sw/rxe/rxe_mw.c | 4 - drivers/infiniband/sw/rxe/rxe_pool.c | 221 ++++++++------------------ drivers/infiniband/sw/rxe/rxe_pool.h | 77 ++++----- drivers/infiniband/sw/rxe/rxe_verbs.c | 12 -- 7 files changed, 112 insertions(+), 309 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe.c b/drivers/infiniband/sw/rxe/rxe.c index 8e0f9c489cab..09c73a0d8513 100644 --- a/drivers/infiniband/sw/rxe/rxe.c +++ b/drivers/infiniband/sw/rxe/rxe.c @@ -116,97 +116,31 @@ static void rxe_init_ports(struct rxe_dev *rxe) } /* init pools of managed objects */ -static int rxe_init_pools(struct rxe_dev *rxe) +static void rxe_init_pools(struct rxe_dev *rxe) { - int err; - - err = rxe_pool_init(rxe, &rxe->uc_pool, RXE_TYPE_UC, - rxe->max_ucontext); - if (err) - goto err1; - - err = rxe_pool_init(rxe, &rxe->pd_pool, RXE_TYPE_PD, - rxe->attr.max_pd); - if (err) - goto err2; - - err = rxe_pool_init(rxe, &rxe->ah_pool, RXE_TYPE_AH, - rxe->attr.max_ah); - if (err) - goto err3; - - err = rxe_pool_init(rxe, &rxe->srq_pool, RXE_TYPE_SRQ, - rxe->attr.max_srq); - if (err) - goto err4; - - err = rxe_pool_init(rxe, &rxe->qp_pool, RXE_TYPE_QP, - rxe->attr.max_qp); - if (err) - goto err5; - - err = rxe_pool_init(rxe, &rxe->cq_pool, RXE_TYPE_CQ, - rxe->attr.max_cq); - if (err) - goto err6; - - err = rxe_pool_init(rxe, &rxe->mr_pool, RXE_TYPE_MR, - rxe->attr.max_mr); - if (err) - goto err7; - - err = rxe_pool_init(rxe, &rxe->mw_pool, RXE_TYPE_MW, - rxe->attr.max_mw); - if (err) - goto err8; - - err = rxe_pool_init(rxe, &rxe->mc_grp_pool, RXE_TYPE_MC_GRP, + rxe_pool_init(rxe, &rxe->uc_pool, RXE_TYPE_UC, rxe->max_ucontext); + rxe_pool_init(rxe, &rxe->pd_pool, RXE_TYPE_PD, rxe->attr.max_pd); + rxe_pool_init(rxe, &rxe->ah_pool, RXE_TYPE_AH, rxe->attr.max_ah); + rxe_pool_init(rxe, &rxe->srq_pool, RXE_TYPE_SRQ, rxe->attr.max_srq); + rxe_pool_init(rxe, &rxe->qp_pool, RXE_TYPE_QP, rxe->attr.max_qp); + rxe_pool_init(rxe, &rxe->cq_pool, RXE_TYPE_CQ, rxe->attr.max_cq); + rxe_pool_init(rxe, &rxe->mr_pool, RXE_TYPE_MR, rxe->attr.max_mr); + rxe_pool_init(rxe, &rxe->mw_pool, RXE_TYPE_MW, rxe->attr.max_mw); + rxe_pool_init(rxe, &rxe->mc_grp_pool, RXE_TYPE_MC_GRP, rxe->attr.max_mcast_grp); - if (err) - goto err9; - - err = rxe_pool_init(rxe, &rxe->mc_elem_pool, RXE_TYPE_MC_ELEM, + rxe_pool_init(rxe, &rxe->mc_elem_pool, RXE_TYPE_MC_ELEM, rxe->attr.max_total_mcast_qp_attach); - if (err) - goto err10; - - return 0; - -err10: - rxe_pool_cleanup(&rxe->mc_grp_pool); -err9: - rxe_pool_cleanup(&rxe->mw_pool); -err8: - rxe_pool_cleanup(&rxe->mr_pool); -err7: - rxe_pool_cleanup(&rxe->cq_pool); -err6: - rxe_pool_cleanup(&rxe->qp_pool); -err5: - rxe_pool_cleanup(&rxe->srq_pool); -err4: - rxe_pool_cleanup(&rxe->ah_pool); -err3: - rxe_pool_cleanup(&rxe->pd_pool); -err2: - rxe_pool_cleanup(&rxe->uc_pool); -err1: - return err; } /* initialize rxe device state */ -static int rxe_init(struct rxe_dev *rxe) +static void rxe_init(struct rxe_dev *rxe) { - int err; - /* init default device parameters */ rxe_init_device_param(rxe); rxe_init_ports(rxe); - err = rxe_init_pools(rxe); - if (err) - return err; + rxe_init_pools(rxe); /* init pending mmap list */ spin_lock_init(&rxe->mmap_offset_lock); @@ -214,8 +148,6 @@ static int rxe_init(struct rxe_dev *rxe) INIT_LIST_HEAD(&rxe->pending_mmaps); mutex_init(&rxe->usdev_lock); - - return 0; } void rxe_set_mtu(struct rxe_dev *rxe, unsigned int ndev_mtu) @@ -237,11 +169,7 @@ void rxe_set_mtu(struct rxe_dev *rxe, unsigned int ndev_mtu) */ int rxe_add(struct rxe_dev *rxe, unsigned int mtu, const char *ibdev_name) { - int err; - - err = rxe_init(rxe); - if (err) - return err; + rxe_init(rxe); rxe_set_mtu(rxe, mtu); diff --git a/drivers/infiniband/sw/rxe/rxe_mcast.c b/drivers/infiniband/sw/rxe/rxe_mcast.c index bd1ac88b8700..1692526c5b57 100644 --- a/drivers/infiniband/sw/rxe/rxe_mcast.c +++ b/drivers/infiniband/sw/rxe/rxe_mcast.c @@ -44,7 +44,7 @@ int rxe_mcast_get_grp(struct rxe_dev *rxe, union ib_gid *mgid, if (rxe->attr.max_mcast_qp_attach == 0) return -EINVAL; - write_lock_bh(&pool->pool_lock); + rxe_pool_lock_bh(pool); grp = rxe_pool_get_key_locked(pool, mgid); if (grp) @@ -52,13 +52,13 @@ int rxe_mcast_get_grp(struct rxe_dev *rxe, union ib_gid *mgid, grp = create_grp(rxe, pool, mgid); if (IS_ERR(grp)) { - write_unlock_bh(&pool->pool_lock); + rxe_pool_unlock_bh(pool); err = PTR_ERR(grp); return err; } done: - write_unlock_bh(&pool->pool_lock); + rxe_pool_unlock_bh(pool); *grp_p = grp; return 0; } diff --git a/drivers/infiniband/sw/rxe/rxe_mr.c b/drivers/infiniband/sw/rxe/rxe_mr.c index 25c78aade822..3c4390adfb80 100644 --- a/drivers/infiniband/sw/rxe/rxe_mr.c +++ b/drivers/infiniband/sw/rxe/rxe_mr.c @@ -693,7 +693,6 @@ int rxe_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata) mr->state = RXE_MR_STATE_INVALID; rxe_drop_ref(mr_pd(mr)); - rxe_drop_index(mr); rxe_drop_ref(mr); return 0; diff --git a/drivers/infiniband/sw/rxe/rxe_mw.c b/drivers/infiniband/sw/rxe/rxe_mw.c index 32dd8c0b8b9e..3ae981d77c25 100644 --- a/drivers/infiniband/sw/rxe/rxe_mw.c +++ b/drivers/infiniband/sw/rxe/rxe_mw.c @@ -20,7 +20,6 @@ int rxe_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata) return ret; } - rxe_add_index(mw); mw->rkey = ibmw->rkey = (mw->elem.index << 8) | rxe_get_next_key(-1); mw->state = (mw->ibmw.type == IB_MW_TYPE_2) ? RXE_MW_STATE_FREE : RXE_MW_STATE_VALID; @@ -332,7 +331,4 @@ struct rxe_mw *rxe_lookup_mw(struct rxe_qp *qp, int access, u32 rkey) void rxe_mw_cleanup(struct rxe_pool_elem *elem) { - struct rxe_mw *mw = container_of(elem, typeof(*mw), elem); - - rxe_drop_index(mw); } diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c index 4cb003885e00..ff7ca2dbcb0a 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.c +++ b/drivers/infiniband/sw/rxe/rxe_pool.c @@ -97,37 +97,13 @@ static const struct rxe_type_info { }, }; -static int rxe_pool_init_index(struct rxe_pool *pool, u32 max, u32 min) -{ - int err = 0; - - if ((max - min + 1) < pool->max_elem) { - pr_warn("not enough indices for max_elem\n"); - err = -EINVAL; - goto out; - } - - pool->index.max_index = max; - pool->index.min_index = min; - - pool->index.table = bitmap_zalloc(max - min + 1, GFP_KERNEL); - if (!pool->index.table) { - err = -ENOMEM; - goto out; - } - -out: - return err; -} - -int rxe_pool_init( +void rxe_pool_init( struct rxe_dev *rxe, struct rxe_pool *pool, enum rxe_elem_type type, unsigned int max_elem) { const struct rxe_type_info *info = &rxe_type_info[type]; - int err = 0; memset(pool, 0, sizeof(*pool)); @@ -142,24 +118,18 @@ int rxe_pool_init( atomic_set(&pool->num_elem, 0); - rwlock_init(&pool->pool_lock); - - if (pool->flags & RXE_POOL_INDEX) { - pool->index.tree = RB_ROOT; - err = rxe_pool_init_index(pool, info->max_index, - info->min_index); - if (err) - goto out; - } + /* used for pools with RXE_POOL_INDEX and + * the xa spinlock for other pools + */ + xa_init_flags(&pool->xarray.xa, XA_FLAGS_ALLOC); + pool->xarray.limit.max = info->max_index; + pool->xarray.limit.min = info->min_index; if (pool->flags & RXE_POOL_KEY) { pool->key.tree = RB_ROOT; pool->key.key_offset = info->key_offset; pool->key.key_size = info->key_size; } - -out: - return err; } void rxe_pool_cleanup(struct rxe_pool *pool) @@ -167,51 +137,6 @@ void rxe_pool_cleanup(struct rxe_pool *pool) if (atomic_read(&pool->num_elem) > 0) pr_warn("%s pool destroyed with unfree'd elem\n", pool->name); - - if (pool->flags & RXE_POOL_INDEX) - bitmap_free(pool->index.table); -} - -static u32 alloc_index(struct rxe_pool *pool) -{ - u32 index; - u32 range = pool->index.max_index - pool->index.min_index + 1; - - index = find_next_zero_bit(pool->index.table, range, pool->index.last); - if (index >= range) - index = find_first_zero_bit(pool->index.table, range); - - WARN_ON_ONCE(index >= range); - set_bit(index, pool->index.table); - pool->index.last = index; - return index + pool->index.min_index; -} - -static int rxe_insert_index(struct rxe_pool *pool, struct rxe_pool_elem *new) -{ - struct rb_node **link = &pool->index.tree.rb_node; - struct rb_node *parent = NULL; - struct rxe_pool_elem *elem; - - while (*link) { - parent = *link; - elem = rb_entry(parent, struct rxe_pool_elem, index_node); - - if (elem->index == new->index) { - pr_warn("element already exists!\n"); - return -EINVAL; - } - - if (elem->index > new->index) - link = &(*link)->rb_left; - else - link = &(*link)->rb_right; - } - - rb_link_node(&new->index_node, parent, link); - rb_insert_color(&new->index_node, &pool->index.tree); - - return 0; } static int rxe_insert_key(struct rxe_pool *pool, struct rxe_pool_elem *new) @@ -262,9 +187,9 @@ int __rxe_add_key(struct rxe_pool_elem *elem, void *key) struct rxe_pool *pool = elem->pool; int err; - write_lock_bh(&pool->pool_lock); + rxe_pool_lock_bh(pool); err = __rxe_add_key_locked(elem, key); - write_unlock_bh(&pool->pool_lock); + rxe_pool_unlock_bh(pool); return err; } @@ -280,55 +205,16 @@ void __rxe_drop_key(struct rxe_pool_elem *elem) { struct rxe_pool *pool = elem->pool; - write_lock_bh(&pool->pool_lock); + rxe_pool_lock_bh(pool); __rxe_drop_key_locked(elem); - write_unlock_bh(&pool->pool_lock); -} - -int __rxe_add_index_locked(struct rxe_pool_elem *elem) -{ - struct rxe_pool *pool = elem->pool; - int err; - - elem->index = alloc_index(pool); - err = rxe_insert_index(pool, elem); - - return err; -} - -int __rxe_add_index(struct rxe_pool_elem *elem) -{ - struct rxe_pool *pool = elem->pool; - int err; - - write_lock_bh(&pool->pool_lock); - err = __rxe_add_index_locked(elem); - write_unlock_bh(&pool->pool_lock); - - return err; -} - -void __rxe_drop_index_locked(struct rxe_pool_elem *elem) -{ - struct rxe_pool *pool = elem->pool; - - clear_bit(elem->index - pool->index.min_index, pool->index.table); - rb_erase(&elem->index_node, &pool->index.tree); -} - -void __rxe_drop_index(struct rxe_pool_elem *elem) -{ - struct rxe_pool *pool = elem->pool; - - write_lock_bh(&pool->pool_lock); - __rxe_drop_index_locked(elem); - write_unlock_bh(&pool->pool_lock); + rxe_pool_unlock_bh(pool); } void *rxe_alloc_locked(struct rxe_pool *pool) { struct rxe_pool_elem *elem; void *obj; + int err; if (atomic_inc_return(&pool->num_elem) > pool->max_elem) goto out_cnt; @@ -343,8 +229,18 @@ void *rxe_alloc_locked(struct rxe_pool *pool) elem->obj = obj; kref_init(&elem->ref_cnt); + if (pool->flags & RXE_POOL_INDEX) { + err = xa_alloc_cyclic_bh(&pool->xarray.xa, &elem->index, elem, + pool->xarray.limit, + &pool->xarray.next, GFP_ATOMIC); + if (err) + goto out_free; + } + return obj; +out_free: + kfree(obj); out_cnt: atomic_dec(&pool->num_elem); return NULL; @@ -354,6 +250,7 @@ void *rxe_alloc(struct rxe_pool *pool) { struct rxe_pool_elem *elem; void *obj; + int err; if (atomic_inc_return(&pool->num_elem) > pool->max_elem) goto out_cnt; @@ -368,8 +265,18 @@ void *rxe_alloc(struct rxe_pool *pool) elem->obj = obj; kref_init(&elem->ref_cnt); + if (pool->flags & RXE_POOL_INDEX) { + err = xa_alloc_cyclic_bh(&pool->xarray.xa, &elem->index, elem, + pool->xarray.limit, + &pool->xarray.next, GFP_KERNEL); + if (err) + goto out_free; + } + return obj; +out_free: + kfree(obj); out_cnt: atomic_dec(&pool->num_elem); return NULL; @@ -377,6 +284,8 @@ void *rxe_alloc(struct rxe_pool *pool) int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem) { + int err = -EINVAL; + if (atomic_inc_return(&pool->num_elem) > pool->max_elem) goto out_cnt; @@ -384,11 +293,19 @@ int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem) elem->obj = (u8 *)elem - pool->elem_offset; kref_init(&elem->ref_cnt); + if (pool->flags & RXE_POOL_INDEX) { + err = xa_alloc_cyclic_bh(&pool->xarray.xa, &elem->index, elem, + pool->xarray.limit, + &pool->xarray.next, GFP_KERNEL); + if (err) + goto out_cnt; + } + return 0; out_cnt: atomic_dec(&pool->num_elem); - return -EINVAL; + return err; } void rxe_elem_release(struct kref *kref) @@ -398,6 +315,9 @@ void rxe_elem_release(struct kref *kref) struct rxe_pool *pool = elem->pool; void *obj; + if (pool->flags & RXE_POOL_INDEX) + xa_erase(&pool->xarray.xa, elem->index); + if (pool->cleanup) pool->cleanup(elem); @@ -409,42 +329,25 @@ void rxe_elem_release(struct kref *kref) atomic_dec(&pool->num_elem); } -void *rxe_pool_get_index_locked(struct rxe_pool *pool, u32 index) +/** + * rxe_pool_get_index - lookup object from index + * @pool: the object pool + * @index: the index of the object + * + * Returns: the object if the index exists in the pool + * and the reference count on the object is positive + * else NULL + */ +void *rxe_pool_get_index(struct rxe_pool *pool, u32 index) { - struct rb_node *node; struct rxe_pool_elem *elem; void *obj; - node = pool->index.tree.rb_node; - - while (node) { - elem = rb_entry(node, struct rxe_pool_elem, index_node); - - if (elem->index > index) - node = node->rb_left; - else if (elem->index < index) - node = node->rb_right; - else - break; - } - - if (node) { - kref_get(&elem->ref_cnt); + elem = xa_load(&pool->xarray.xa, index); + if (elem && kref_get_unless_zero(&elem->ref_cnt)) obj = elem->obj; - } else { + else obj = NULL; - } - - return obj; -} - -void *rxe_pool_get_index(struct rxe_pool *pool, u32 index) -{ - void *obj; - - read_lock_bh(&pool->pool_lock); - obj = rxe_pool_get_index_locked(pool, index); - read_unlock_bh(&pool->pool_lock); return obj; } @@ -486,9 +389,9 @@ void *rxe_pool_get_key(struct rxe_pool *pool, void *key) { void *obj; - read_lock_bh(&pool->pool_lock); + rxe_pool_lock_bh(pool); obj = rxe_pool_get_key_locked(pool, key); - read_unlock_bh(&pool->pool_lock); + rxe_pool_unlock_bh(pool); return obj; } diff --git a/drivers/infiniband/sw/rxe/rxe_pool.h b/drivers/infiniband/sw/rxe/rxe_pool.h index 214279310f4d..fbef351783dc 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.h +++ b/drivers/infiniband/sw/rxe/rxe_pool.h @@ -37,14 +37,12 @@ struct rxe_pool_elem { struct rb_node key_node; /* only used if indexed */ - struct rb_node index_node; u32 index; }; struct rxe_pool { struct rxe_dev *rxe; const char *name; - rwlock_t pool_lock; /* protects pool add/del/search */ void (*cleanup)(struct rxe_pool_elem *obj); enum rxe_pool_flags flags; enum rxe_elem_type type; @@ -56,12 +54,10 @@ struct rxe_pool { /* only used if indexed */ struct { - struct rb_root tree; - unsigned long *table; - u32 last; - u32 max_index; - u32 min_index; - } index; + struct xarray xa; + struct xa_limit limit; + u32 next; + } xarray; /* only used if keyed */ struct { @@ -71,11 +67,10 @@ struct rxe_pool { } key; }; -/* initialize a pool of objects with given limit on - * number of elements. gets parameters from rxe_type_info - * pool elements will be allocated out of a slab cache - */ -int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool, +#define rxe_pool_lock_bh(pool) xa_lock_bh(&pool->xarray.xa) +#define rxe_pool_unlock_bh(pool) xa_unlock_bh(&pool->xarray.xa) + +void rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool, enum rxe_elem_type type, u32 max_elem); /* free resources from object pool */ @@ -91,28 +86,6 @@ int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem); #define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->elem) -/* assign an index to an indexed object and insert object into - * pool's rb tree holding and not holding the pool_lock - */ -int __rxe_add_index_locked(struct rxe_pool_elem *elem); - -#define rxe_add_index_locked(obj) __rxe_add_index_locked(&(obj)->elem) - -int __rxe_add_index(struct rxe_pool_elem *elem); - -#define rxe_add_index(obj) __rxe_add_index(&(obj)->elem) - -/* drop an index and remove object from rb tree - * holding and not holding the pool_lock - */ -void __rxe_drop_index_locked(struct rxe_pool_elem *elem); - -#define rxe_drop_index_locked(obj) __rxe_drop_index_locked(&(obj)->elem) - -void __rxe_drop_index(struct rxe_pool_elem *elem); - -#define rxe_drop_index(obj) __rxe_drop_index(&(obj)->elem) - /* assign a key to a keyed object and insert object into * pool's rb tree holding and not holding pool_lock */ @@ -133,11 +106,6 @@ void __rxe_drop_key(struct rxe_pool_elem *elem); #define rxe_drop_key(obj) __rxe_drop_key(&(obj)->elem) -/* lookup an indexed object from index holding and not holding the pool_lock. - * takes a reference on object - */ -void *rxe_pool_get_index_locked(struct rxe_pool *pool, u32 index); - void *rxe_pool_get_index(struct rxe_pool *pool, u32 index); /* lookup keyed object from key holding and not holding the pool_lock. @@ -150,10 +118,31 @@ void *rxe_pool_get_key(struct rxe_pool *pool, void *key); /* cleanup an object when all references are dropped */ void rxe_elem_release(struct kref *kref); -/* take a reference on an object */ -#define rxe_add_ref(obj) kref_get(&(obj)->elem.ref_cnt) +/** + * __rxe_add_ref() - adds a reference to a pool element + * @elem: pool element + */ +static inline void __rxe_add_ref(struct rxe_pool_elem *elem) +{ + return kref_get(&elem->ref_cnt); +} + +#define rxe_add_ref(obj) __rxe_add_ref(&(obj)->elem) + +/** + * __rxe_drop_ref() - drops a reference to a pool element + * @elem: pool element + * + * Drop reference to pool element and call rxe_elem_release() if + * the reference count reaches zero. + * + * Returns: 1 if rxe_elem_release is called else 0. + */ +static inline bool __rxe_drop_ref(struct rxe_pool_elem *elem) +{ + return kref_put(&elem->ref_cnt, rxe_elem_release); +} -/* drop a reference on an object */ -#define rxe_drop_ref(obj) kref_put(&(obj)->elem.ref_cnt, rxe_elem_release) +#define rxe_drop_ref(obj) __rxe_drop_ref(&(obj)->elem) #endif /* RXE_POOL_H */ diff --git a/drivers/infiniband/sw/rxe/rxe_verbs.c b/drivers/infiniband/sw/rxe/rxe_verbs.c index 07ca169110bf..e3f64eae088c 100644 --- a/drivers/infiniband/sw/rxe/rxe_verbs.c +++ b/drivers/infiniband/sw/rxe/rxe_verbs.c @@ -181,7 +181,6 @@ static int rxe_create_ah(struct ib_ah *ibah, return err; /* create index > 0 */ - rxe_add_index(ah); ah->ah_num = ah->elem.index; if (uresp) { @@ -189,7 +188,6 @@ static int rxe_create_ah(struct ib_ah *ibah, err = copy_to_user(&uresp->ah_num, &ah->ah_num, sizeof(uresp->ah_num)); if (err) { - rxe_drop_index(ah); rxe_drop_ref(ah); return -EFAULT; } @@ -230,7 +228,6 @@ static int rxe_destroy_ah(struct ib_ah *ibah, u32 flags) { struct rxe_ah *ah = to_rah(ibah); - rxe_drop_index(ah); rxe_drop_ref(ah); return 0; } @@ -437,7 +434,6 @@ static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init, if (err) return err; - rxe_add_index(qp); err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata); if (err) goto qp_init; @@ -445,7 +441,6 @@ static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init, return 0; qp_init: - rxe_drop_index(qp); rxe_drop_ref(qp); return err; } @@ -490,7 +485,6 @@ static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) struct rxe_qp *qp = to_rqp(ibqp); rxe_qp_destroy(qp); - rxe_drop_index(qp); rxe_drop_ref(qp); return 0; } @@ -893,7 +887,6 @@ static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access) if (!mr) return ERR_PTR(-ENOMEM); - rxe_add_index(mr); rxe_add_ref(pd); rxe_mr_init_dma(pd, access, mr); @@ -917,7 +910,6 @@ static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd, goto err2; } - rxe_add_index(mr); rxe_add_ref(pd); @@ -929,7 +921,6 @@ static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd, err3: rxe_drop_ref(pd); - rxe_drop_index(mr); rxe_drop_ref(mr); err2: return ERR_PTR(err); @@ -952,8 +943,6 @@ static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type, goto err1; } - rxe_add_index(mr); - rxe_add_ref(pd); err = rxe_mr_init_fast(pd, max_num_sg, mr); @@ -964,7 +953,6 @@ static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type, err2: rxe_drop_ref(pd); - rxe_drop_index(mr); rxe_drop_ref(mr); err1: return ERR_PTR(err); From patchwork Thu Dec 16 23:31:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 12683009 X-Patchwork-Delegate: jgg@ziepe.ca Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90985C4332F for ; Thu, 16 Dec 2021 23:34:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229748AbhLPXeB (ORCPT ); Thu, 16 Dec 2021 18:34:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229662AbhLPXeA (ORCPT ); Thu, 16 Dec 2021 18:34:00 -0500 Received: from mail-oi1-x229.google.com (mail-oi1-x229.google.com [IPv6:2607:f8b0:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAB59C061574 for ; Thu, 16 Dec 2021 15:34:00 -0800 (PST) Received: by mail-oi1-x229.google.com with SMTP id u74so1132074oie.8 for ; Thu, 16 Dec 2021 15:34:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RDdExr3eMeEb1tazYUSHIAsXh6hcObg1zDJhekd64bE=; b=PgSQA3JN4jJ4mfnWnrtgMj6lQ+epEzsxXclUUoXwwjNZ3S5rdn6WKJrRuzokfX5jyP foTF+Au4qkMuLQQLWVK72EDTIepdFuouNsPXXEtnVXLFe9/tjBb4ehoWbyatJi2441xb UN5vkZsW5SgbZUWs4uCPEuEgoe7/GWPqZYup3SQjH5huizZ/6yaGFNiG8bwRm42HXV7e MA17BOB5xL4YGHdoNP4KxmHyyFkdyoWdFInlZBYNYIrflIKsYgIp/XzKe2eKULBcLOzE G9jMBu9GHrInvGXr4XksTgyJVo0pJpG1vd4ZMZf4uvrsXeF+5mVceITvOr5Ik6R23L7t UApg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RDdExr3eMeEb1tazYUSHIAsXh6hcObg1zDJhekd64bE=; b=mW7EpRtXGFyM3lY518kXLYdHPRGfsDTjK+SlRqEb3XIRiNgrfiOet1g+5gjZ5iZMXs oYQF+4NdCvZo9xiYP14t2cgzyCjBg0YaMH+kJK4MgDlDGdRTjm6NfkiKrnSnRRmqn6Tc E7qzUCErNeF8ABCiycMASDqvyOIMO8hxZbY7ksSq6kTdH0PKFFN94HzDCCKE4whixgGO ssnmsKEZOiS4lbQff0Zz32sH1HNYsL0kNQsNZYxFnZGg0hZBHRn9wHHzaxQsEH9WdqT6 xi6m49pFo3ZEG0BR9vFg8gBSjoAcUJF8FjfeosRNkTxXYICvQfuTB2tnHaJrOl2NfTCr B6VA== X-Gm-Message-State: AOAM533dAydoTlBLxUXxm0vrXMLagMkhclttDtMHF1uDbPUJQRSsE/hU 7ZXdsthkZ7U4zUul2dQmmXw= X-Google-Smtp-Source: ABdhPJyClAOFYvXbE2+/BZt+JZke8ACa3LvbcdPcYbh0b1kCMjEVp8xPz1MeSkvc56P0EMP6EuX2kA== X-Received: by 2002:a54:4494:: with SMTP id v20mr6150987oiv.95.1639697640115; Thu, 16 Dec 2021 15:34:00 -0800 (PST) Received: from ubuntu-21.tx.rr.com (2603-8081-140c-1a00-ec41-d089-dfdb-6fb5.res6.spectrum.com. [2603:8081:140c:1a00:ec41:d089:dfdb:6fb5]) by smtp.googlemail.com with ESMTPSA id w19sm1253888oih.44.2021.12.16.15.33.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Dec 2021 15:33:59 -0800 (PST) From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next v8 2/8] RDMA/rxe: Reverse the sense of RXE_POOL_NO_ALLOC Date: Thu, 16 Dec 2021 17:31:56 -0600 Message-Id: <20211216233201.14893-3-rpearsonhpe@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211216233201.14893-1-rpearsonhpe@gmail.com> References: <20211216233201.14893-1-rpearsonhpe@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Since most rxe objects are now allocated in rdma-core change the sense of RXE_POOL_NO_ALLOC to RXE_POOL_ALLOC. This makes the code easier to understand. Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe_pool.c | 18 ++++++++---------- drivers/infiniband/sw/rxe/rxe_pool.h | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c index ff7ca2dbcb0a..d1981309aa23 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.c +++ b/drivers/infiniband/sw/rxe/rxe_pool.c @@ -23,19 +23,17 @@ static const struct rxe_type_info { .name = "rxe-uc", .size = sizeof(struct rxe_ucontext), .elem_offset = offsetof(struct rxe_ucontext, elem), - .flags = RXE_POOL_NO_ALLOC, }, [RXE_TYPE_PD] = { .name = "rxe-pd", .size = sizeof(struct rxe_pd), .elem_offset = offsetof(struct rxe_pd, elem), - .flags = RXE_POOL_NO_ALLOC, }, [RXE_TYPE_AH] = { .name = "rxe-ah", .size = sizeof(struct rxe_ah), .elem_offset = offsetof(struct rxe_ah, elem), - .flags = RXE_POOL_INDEX | RXE_POOL_NO_ALLOC, + .flags = RXE_POOL_INDEX, .min_index = RXE_MIN_AH_INDEX, .max_index = RXE_MAX_AH_INDEX, }, @@ -43,7 +41,7 @@ static const struct rxe_type_info { .name = "rxe-srq", .size = sizeof(struct rxe_srq), .elem_offset = offsetof(struct rxe_srq, elem), - .flags = RXE_POOL_INDEX | RXE_POOL_NO_ALLOC, + .flags = RXE_POOL_INDEX, .min_index = RXE_MIN_SRQ_INDEX, .max_index = RXE_MAX_SRQ_INDEX, }, @@ -52,7 +50,7 @@ static const struct rxe_type_info { .size = sizeof(struct rxe_qp), .elem_offset = offsetof(struct rxe_qp, elem), .cleanup = rxe_qp_cleanup, - .flags = RXE_POOL_INDEX | RXE_POOL_NO_ALLOC, + .flags = RXE_POOL_INDEX, .min_index = RXE_MIN_QP_INDEX, .max_index = RXE_MAX_QP_INDEX, }, @@ -60,7 +58,6 @@ static const struct rxe_type_info { .name = "rxe-cq", .size = sizeof(struct rxe_cq), .elem_offset = offsetof(struct rxe_cq, elem), - .flags = RXE_POOL_NO_ALLOC, .cleanup = rxe_cq_cleanup, }, [RXE_TYPE_MR] = { @@ -68,7 +65,7 @@ static const struct rxe_type_info { .size = sizeof(struct rxe_mr), .elem_offset = offsetof(struct rxe_mr, elem), .cleanup = rxe_mr_cleanup, - .flags = RXE_POOL_INDEX, + .flags = RXE_POOL_INDEX | RXE_POOL_ALLOC, .min_index = RXE_MIN_MR_INDEX, .max_index = RXE_MAX_MR_INDEX, }, @@ -77,7 +74,7 @@ static const struct rxe_type_info { .size = sizeof(struct rxe_mw), .elem_offset = offsetof(struct rxe_mw, elem), .cleanup = rxe_mw_cleanup, - .flags = RXE_POOL_INDEX | RXE_POOL_NO_ALLOC, + .flags = RXE_POOL_INDEX, .min_index = RXE_MIN_MW_INDEX, .max_index = RXE_MAX_MW_INDEX, }, @@ -86,7 +83,7 @@ static const struct rxe_type_info { .size = sizeof(struct rxe_mc_grp), .elem_offset = offsetof(struct rxe_mc_grp, elem), .cleanup = rxe_mc_cleanup, - .flags = RXE_POOL_KEY, + .flags = RXE_POOL_KEY | RXE_POOL_ALLOC, .key_offset = offsetof(struct rxe_mc_grp, mgid), .key_size = sizeof(union ib_gid), }, @@ -94,6 +91,7 @@ static const struct rxe_type_info { .name = "rxe-mc_elem", .size = sizeof(struct rxe_mc_elem), .elem_offset = offsetof(struct rxe_mc_elem, elem), + .flags = RXE_POOL_ALLOC, }, }; @@ -321,7 +319,7 @@ void rxe_elem_release(struct kref *kref) if (pool->cleanup) pool->cleanup(elem); - if (!(pool->flags & RXE_POOL_NO_ALLOC)) { + if (pool->flags & RXE_POOL_ALLOC) { obj = elem->obj; kfree(obj); } diff --git a/drivers/infiniband/sw/rxe/rxe_pool.h b/drivers/infiniband/sw/rxe/rxe_pool.h index fbef351783dc..514be1582bce 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.h +++ b/drivers/infiniband/sw/rxe/rxe_pool.h @@ -10,7 +10,7 @@ enum rxe_pool_flags { RXE_POOL_INDEX = BIT(1), RXE_POOL_KEY = BIT(2), - RXE_POOL_NO_ALLOC = BIT(4), + RXE_POOL_ALLOC = BIT(4), }; enum rxe_elem_type { From patchwork Thu Dec 16 23:31:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 12683011 X-Patchwork-Delegate: jgg@ziepe.ca Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4664FC433F5 for ; Thu, 16 Dec 2021 23:34:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229662AbhLPXeC (ORCPT ); Thu, 16 Dec 2021 18:34:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229742AbhLPXeB (ORCPT ); Thu, 16 Dec 2021 18:34:01 -0500 Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98E64C061574 for ; Thu, 16 Dec 2021 15:34:01 -0800 (PST) Received: by mail-ot1-x32c.google.com with SMTP id x43-20020a056830246b00b00570d09d34ebso814473otr.2 for ; Thu, 16 Dec 2021 15:34:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ugpQMgFCHMdTH1QwRKVau8nh91zaNwjtsyBTN7Vtutw=; b=EMuzslA7+VuA3bCba+tVZOdht7QmV41tHIzITs8BHIw48VUYDEEw7hVXDgP3MRKyaQ SkSMZ10CReHTURbyWcu+ZsgfMiTuTAkbAF7Nk7uUS9KKRXQNbyu4/sOKHK86n7N/z+j/ Z6MkJe/U5R8uQ7ozUfw6AyRCj5cQqnaglWvrDR8tbxSHAHGa3ED+3c8wJwow4YN99ARB yQtzlHsqoJBykfQjw4oPNC1LyEw1ZfdLf6zYoGnrB1xuiZFhNdueqHRijmXUddz4yVLJ Qw4+hP5no4GejN8wbAet1dp5sKgHrB3CoNrRD9bFEl08/TXKlo3Z0u7Kmuk7pKLVvyYW ncZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ugpQMgFCHMdTH1QwRKVau8nh91zaNwjtsyBTN7Vtutw=; b=MK/aTknhMMLMByIgd41P+koLEpdNlhiIudwjKXvAgWksI30G1jxta9UI/am1L/P73K mir7aIXb7Qh8ZT3jO4C2gvak1vypHJ87FDo3v9aiwy+gB1aZRv5bSk1+EuMnPZ8tYOMO J4gS1GhtXhoKxbtinhGpeUodIKUw1Al/WCC4DX/qWeH2df07ZMwssCbplkOut77+zita Rt4nd8FMkXoNTZAM0HPFhAk+cYCOtFzjRg1kVGyZTIIjM3YSh2OJJEvMThlRTGuny6oI 5mlIpONZf4oCMCbDOfXREnfInUVq4/Of7WU43TUZaQEWhgjve8tTJpWHQXKrTdPaKJVn U3KQ== X-Gm-Message-State: AOAM530zb+ksLERLngE0lxvlXViGp/2v4T9ZzYz/VpnloX1OZQnMqLiO uJDXg+Om31kfON9s22ao8Tc= X-Google-Smtp-Source: ABdhPJw9tJclnZDDMtLQ9SVC5LDsRtoQUSpXPDABK3znBkBEqx12yVFF+4GMF1/GnBwTQi+eBKIS1Q== X-Received: by 2002:a9d:1ca9:: with SMTP id l41mr324901ota.341.1639697640839; Thu, 16 Dec 2021 15:34:00 -0800 (PST) Received: from ubuntu-21.tx.rr.com (2603-8081-140c-1a00-ec41-d089-dfdb-6fb5.res6.spectrum.com. [2603:8081:140c:1a00:ec41:d089:dfdb:6fb5]) by smtp.googlemail.com with ESMTPSA id w19sm1253888oih.44.2021.12.16.15.34.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Dec 2021 15:34:00 -0800 (PST) From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next v8 3/8] RDMA/rxe: Cleanup pool APIs for keyed objects Date: Thu, 16 Dec 2021 17:31:57 -0600 Message-Id: <20211216233201.14893-4-rpearsonhpe@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211216233201.14893-1-rpearsonhpe@gmail.com> References: <20211216233201.14893-1-rpearsonhpe@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Simplify the rxe pool APIs for keyed objects. Eliminate xxx_locked() APIs. Merge rxe_drop_key into rxe_drop_ref. Replace separate rxe_get_key, and add_key by one call rxe_add_key which looks up and if necessary allocates a new object. This routine is written to avoid using GFP_ATOMIC by maintaining a free list of objects allocated without holding a spinlock. Signed-off-by: Bob Pearson --- v8 Corrected an additional race condition where another tread uses the remaining objects on the free list after this thread creates them before they are used. drivers/infiniband/sw/rxe/rxe_loc.h | 5 +- drivers/infiniband/sw/rxe/rxe_mcast.c | 46 ++-- drivers/infiniband/sw/rxe/rxe_pool.c | 297 ++++++++++++++------------ drivers/infiniband/sw/rxe/rxe_pool.h | 59 +---- 4 files changed, 187 insertions(+), 220 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe_loc.h b/drivers/infiniband/sw/rxe/rxe_loc.h index b1e174afb1d4..6558602be751 100644 --- a/drivers/infiniband/sw/rxe/rxe_loc.h +++ b/drivers/infiniband/sw/rxe/rxe_loc.h @@ -40,17 +40,14 @@ void rxe_cq_disable(struct rxe_cq *cq); void rxe_cq_cleanup(struct rxe_pool_elem *arg); /* rxe_mcast.c */ +int rxe_init_grp(struct rxe_pool_elem *elem); int rxe_mcast_get_grp(struct rxe_dev *rxe, union ib_gid *mgid, struct rxe_mc_grp **grp_p); - int rxe_mcast_add_grp_elem(struct rxe_dev *rxe, struct rxe_qp *qp, struct rxe_mc_grp *grp); - int rxe_mcast_drop_grp_elem(struct rxe_dev *rxe, struct rxe_qp *qp, union ib_gid *mgid); - void rxe_drop_all_mcast_groups(struct rxe_qp *qp); - void rxe_mc_cleanup(struct rxe_pool_elem *arg); /* rxe_mmap.c */ diff --git a/drivers/infiniband/sw/rxe/rxe_mcast.c b/drivers/infiniband/sw/rxe/rxe_mcast.c index 1692526c5b57..e110c4d3fbf4 100644 --- a/drivers/infiniband/sw/rxe/rxe_mcast.c +++ b/drivers/infiniband/sw/rxe/rxe_mcast.c @@ -7,59 +7,38 @@ #include "rxe.h" #include "rxe_loc.h" -/* caller should hold mc_grp_pool->pool_lock */ -static struct rxe_mc_grp *create_grp(struct rxe_dev *rxe, - struct rxe_pool *pool, - union ib_gid *mgid) +int rxe_init_grp(struct rxe_pool_elem *elem) { + struct rxe_dev *rxe = elem->pool->rxe; + struct rxe_mc_grp *grp = elem->obj; int err; - struct rxe_mc_grp *grp; - - grp = rxe_alloc_locked(&rxe->mc_grp_pool); - if (!grp) - return ERR_PTR(-ENOMEM); INIT_LIST_HEAD(&grp->qp_list); spin_lock_init(&grp->mcg_lock); grp->rxe = rxe; - rxe_add_key_locked(grp, mgid); - err = rxe_mcast_add(rxe, mgid); - if (unlikely(err)) { - rxe_drop_key_locked(grp); + err = rxe_mcast_add(rxe, &grp->mgid); + if (err) rxe_drop_ref(grp); - return ERR_PTR(err); - } - return grp; + return err; } int rxe_mcast_get_grp(struct rxe_dev *rxe, union ib_gid *mgid, struct rxe_mc_grp **grp_p) { - int err; - struct rxe_mc_grp *grp; struct rxe_pool *pool = &rxe->mc_grp_pool; + struct rxe_mc_grp *grp; if (rxe->attr.max_mcast_qp_attach == 0) return -EINVAL; - rxe_pool_lock_bh(pool); - - grp = rxe_pool_get_key_locked(pool, mgid); - if (grp) - goto done; - - grp = create_grp(rxe, pool, mgid); - if (IS_ERR(grp)) { - rxe_pool_unlock_bh(pool); - err = PTR_ERR(grp); - return err; - } + grp = rxe_pool_add_key(pool, mgid); + if (!grp) + return -EINVAL; -done: - rxe_pool_unlock_bh(pool); *grp_p = grp; + return 0; } @@ -84,7 +63,7 @@ int rxe_mcast_add_grp_elem(struct rxe_dev *rxe, struct rxe_qp *qp, goto out; } - elem = rxe_alloc_locked(&rxe->mc_elem_pool); + elem = rxe_alloc(&rxe->mc_elem_pool); if (!elem) { err = -ENOMEM; goto out; @@ -173,6 +152,5 @@ void rxe_mc_cleanup(struct rxe_pool_elem *elem) struct rxe_mc_grp *grp = container_of(elem, typeof(*grp), elem); struct rxe_dev *rxe = grp->rxe; - rxe_drop_key(grp); rxe_mcast_delete(rxe, &grp->mgid); } diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c index d1981309aa23..a5ff2fd692c9 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.c +++ b/drivers/infiniband/sw/rxe/rxe_pool.c @@ -12,7 +12,8 @@ static const struct rxe_type_info { const char *name; size_t size; size_t elem_offset; - void (*cleanup)(struct rxe_pool_elem *obj); + int (*init)(struct rxe_pool_elem *elem); + void (*cleanup)(struct rxe_pool_elem *elem); enum rxe_pool_flags flags; u32 min_index; u32 max_index; @@ -82,6 +83,7 @@ static const struct rxe_type_info { .name = "rxe-mc_grp", .size = sizeof(struct rxe_mc_grp), .elem_offset = offsetof(struct rxe_mc_grp, elem), + .init = rxe_init_grp, .cleanup = rxe_mc_cleanup, .flags = RXE_POOL_KEY | RXE_POOL_ALLOC, .key_offset = offsetof(struct rxe_mc_grp, mgid), @@ -112,7 +114,9 @@ void rxe_pool_init( pool->elem_size = ALIGN(info->size, RXE_POOL_ALIGN); pool->elem_offset = info->elem_offset; pool->flags = info->flags; + pool->init = info->init; pool->cleanup = info->cleanup; + INIT_LIST_HEAD(&pool->free_list); atomic_set(&pool->num_elem, 0); @@ -132,83 +136,23 @@ void rxe_pool_init( void rxe_pool_cleanup(struct rxe_pool *pool) { - if (atomic_read(&pool->num_elem) > 0) - pr_warn("%s pool destroyed with unfree'd elem\n", - pool->name); -} - -static int rxe_insert_key(struct rxe_pool *pool, struct rxe_pool_elem *new) -{ - struct rb_node **link = &pool->key.tree.rb_node; - struct rb_node *parent = NULL; struct rxe_pool_elem *elem; - int cmp; - - while (*link) { - parent = *link; - elem = rb_entry(parent, struct rxe_pool_elem, key_node); - - cmp = memcmp((u8 *)elem + pool->key.key_offset, - (u8 *)new + pool->key.key_offset, - pool->key.key_size); - - if (cmp == 0) { - pr_warn("key already exists!\n"); - return -EINVAL; - } - - if (cmp > 0) - link = &(*link)->rb_left; - else - link = &(*link)->rb_right; - } - - rb_link_node(&new->key_node, parent, link); - rb_insert_color(&new->key_node, &pool->key.tree); - - return 0; -} - -int __rxe_add_key_locked(struct rxe_pool_elem *elem, void *key) -{ - struct rxe_pool *pool = elem->pool; - int err; - - memcpy((u8 *)elem + pool->key.key_offset, key, pool->key.key_size); - err = rxe_insert_key(pool, elem); - - return err; -} - -int __rxe_add_key(struct rxe_pool_elem *elem, void *key) -{ - struct rxe_pool *pool = elem->pool; - int err; rxe_pool_lock_bh(pool); - err = __rxe_add_key_locked(elem, key); + while (!list_empty(&pool->free_list)) { + elem = list_first_entry(&pool->free_list, + struct rxe_pool_elem, list); + list_del(&elem->list); + __rxe_drop_ref(elem); + } rxe_pool_unlock_bh(pool); - return err; -} - -void __rxe_drop_key_locked(struct rxe_pool_elem *elem) -{ - struct rxe_pool *pool = elem->pool; - - rb_erase(&elem->key_node, &pool->key.tree); -} - -void __rxe_drop_key(struct rxe_pool_elem *elem) -{ - struct rxe_pool *pool = elem->pool; - - rxe_pool_lock_bh(pool); - __rxe_drop_key_locked(elem); - rxe_pool_unlock_bh(pool); + if (atomic_read(&pool->num_elem) > 0) + pr_warn("%s pool destroyed with unfree'd elem\n", + pool->name); } -void *rxe_alloc_locked(struct rxe_pool *pool) +void *rxe_alloc(struct rxe_pool *pool) { struct rxe_pool_elem *elem; void *obj; @@ -217,7 +161,7 @@ void *rxe_alloc_locked(struct rxe_pool *pool) if (atomic_inc_return(&pool->num_elem) > pool->max_elem) goto out_cnt; - obj = kzalloc(pool->elem_size, GFP_ATOMIC); + obj = kzalloc(pool->elem_size, GFP_KERNEL); if (!obj) goto out_cnt; @@ -227,42 +171,12 @@ void *rxe_alloc_locked(struct rxe_pool *pool) elem->obj = obj; kref_init(&elem->ref_cnt); - if (pool->flags & RXE_POOL_INDEX) { - err = xa_alloc_cyclic_bh(&pool->xarray.xa, &elem->index, elem, - pool->xarray.limit, - &pool->xarray.next, GFP_ATOMIC); + if (pool->init) { + err = pool->init(elem); if (err) goto out_free; } - return obj; - -out_free: - kfree(obj); -out_cnt: - atomic_dec(&pool->num_elem); - return NULL; -} - -void *rxe_alloc(struct rxe_pool *pool) -{ - struct rxe_pool_elem *elem; - void *obj; - int err; - - if (atomic_inc_return(&pool->num_elem) > pool->max_elem) - goto out_cnt; - - obj = kzalloc(pool->elem_size, GFP_KERNEL); - if (!obj) - goto out_cnt; - - elem = (struct rxe_pool_elem *)((u8 *)obj + pool->elem_offset); - - elem->pool = pool; - elem->obj = obj; - kref_init(&elem->ref_cnt); - if (pool->flags & RXE_POOL_INDEX) { err = xa_alloc_cyclic_bh(&pool->xarray.xa, &elem->index, elem, pool->xarray.limit, @@ -291,6 +205,12 @@ int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem) elem->obj = (u8 *)elem - pool->elem_offset; kref_init(&elem->ref_cnt); + if (pool->init) { + err = pool->init(elem); + if (err) + goto out_cnt; + } + if (pool->flags & RXE_POOL_INDEX) { err = xa_alloc_cyclic_bh(&pool->xarray.xa, &elem->index, elem, pool->xarray.limit, @@ -306,27 +226,6 @@ int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem) return err; } -void rxe_elem_release(struct kref *kref) -{ - struct rxe_pool_elem *elem = - container_of(kref, struct rxe_pool_elem, ref_cnt); - struct rxe_pool *pool = elem->pool; - void *obj; - - if (pool->flags & RXE_POOL_INDEX) - xa_erase(&pool->xarray.xa, elem->index); - - if (pool->cleanup) - pool->cleanup(elem); - - if (pool->flags & RXE_POOL_ALLOC) { - obj = elem->obj; - kfree(obj); - } - - atomic_dec(&pool->num_elem); -} - /** * rxe_pool_get_index - lookup object from index * @pool: the object pool @@ -339,18 +238,17 @@ void rxe_elem_release(struct kref *kref) void *rxe_pool_get_index(struct rxe_pool *pool, u32 index) { struct rxe_pool_elem *elem; - void *obj; + void *obj = NULL; elem = xa_load(&pool->xarray.xa, index); if (elem && kref_get_unless_zero(&elem->ref_cnt)) obj = elem->obj; - else - obj = NULL; return obj; } -void *rxe_pool_get_key_locked(struct rxe_pool *pool, void *key) +/* lookup key in pool. Caller must hold pool lock */ +static void *__rxe_get_key(struct rxe_pool *pool, void *key) { struct rb_node *node; struct rxe_pool_elem *elem; @@ -362,7 +260,7 @@ void *rxe_pool_get_key_locked(struct rxe_pool *pool, void *key) while (node) { elem = rb_entry(node, struct rxe_pool_elem, key_node); - cmp = memcmp((u8 *)elem + pool->key.key_offset, + cmp = memcmp((u8 *)elem->obj + pool->key.key_offset, key, pool->key.key_size); if (cmp > 0) @@ -383,13 +281,150 @@ void *rxe_pool_get_key_locked(struct rxe_pool *pool, void *key) return obj; } +/* add key to pool. Caller must hold pool lock */ +static int __rxe_add_key(struct rxe_pool_elem *new, void *key) +{ + struct rxe_pool *pool = new->pool; + struct rb_node **link = &pool->key.tree.rb_node; + struct rb_node *parent = NULL; + struct rxe_pool_elem *elem; + int cmp; + + while (*link) { + parent = *link; + elem = rb_entry(parent, struct rxe_pool_elem, key_node); + + cmp = memcmp(key, (u8 *)elem->obj + pool->key.key_offset, + pool->key.key_size); + if (cmp == 0) { + pr_warn("key already exists!\n"); + return -EINVAL; + } + + if (cmp > 0) + link = &(*link)->rb_left; + else + link = &(*link)->rb_right; + } + + rb_link_node(&new->key_node, parent, link); + rb_insert_color(&new->key_node, &pool->key.tree); + + memcpy((u8 *)new->obj + pool->key.key_offset, key, + pool->key.key_size); + + return 0; +} + +/** + * rxe_pool_get_key() - lookup key in pool and return object + * @pool: the object pool + * @key: the key + * + * Returns: if the object matching key is present in pool + * return its address and take a reference else NULL + */ void *rxe_pool_get_key(struct rxe_pool *pool, void *key) { void *obj; rxe_pool_lock_bh(pool); - obj = rxe_pool_get_key_locked(pool, key); + obj = __rxe_get_key(pool, key); + rxe_pool_unlock_bh(pool); + + return obj; +} + +/** + * rxe_pool_add_key() - lookup or add object with key in pool + * @pool: the object pool + * @key: the key + * + * Context: should be called in process context without holding + * any spin locks. Currently this is only used for the + * de/attach multicast group verbs. + * + * Returns: If object matching key is present in pool return + * its address and take a reference else allocate a + * new object from free list, add key and return its address + * with one reference. + */ +void *rxe_pool_add_key(struct rxe_pool *pool, void *key) +{ + struct rxe_pool_elem *elem; + void *obj; + int again = 1; + +again: + /* unless we are out of memory will add one or more + * elements to the free list if it is currently empty + */ + if (list_empty(&pool->free_list)) { + obj = rxe_alloc(pool); + if (obj) { + elem = (struct rxe_pool_elem *)((u8 *)obj + + pool->elem_offset); + rxe_pool_lock_bh(pool); + list_add(&pool->free_list, &elem->list); + rxe_pool_unlock_bh(pool); + } else { + /* we are out of memory don't retry */ + again = 0; + } + } + + rxe_pool_lock_bh(pool); + obj = __rxe_get_key(pool, key); + if (obj) + goto done; + + if (list_empty(&pool->free_list)) { + if (again) { + /* someone may have grabbed the last + * member of the free list while we didn't + * hold the lock so worth trying again + * This should be a rare event + */ + rxe_pool_unlock_bh(pool); + goto again; + } + pr_warn("Unable to allocate a new %s object\n", + pool->name); + goto done; + } + + elem = list_first_entry(&pool->free_list, struct rxe_pool_elem, list); + list_del(&elem->list); + __rxe_add_key(elem, key); + obj = elem->obj; +done: rxe_pool_unlock_bh(pool); return obj; } + +/** + * rxe_elem_release() - cleanup pool element when last reference dropped + * @kref: address of the kref contained in pool element + * + * Caller should hold pool lock + */ +void rxe_elem_release(struct kref *kref) +{ + struct rxe_pool_elem *elem = + container_of(kref, struct rxe_pool_elem, ref_cnt); + struct rxe_pool *pool = elem->pool; + + if (pool->flags & RXE_POOL_INDEX) + xa_erase(&pool->xarray.xa, elem->index); + else if (pool->flags & RXE_POOL_KEY) + rb_erase(&elem->key_node, &pool->key.tree); + + if (pool->cleanup) + pool->cleanup(elem); + + if (pool->flags & RXE_POOL_ALLOC) + kfree(elem->obj); + + atomic_dec(&pool->num_elem); +} diff --git a/drivers/infiniband/sw/rxe/rxe_pool.h b/drivers/infiniband/sw/rxe/rxe_pool.h index 514be1582bce..894ffef4d6bd 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.h +++ b/drivers/infiniband/sw/rxe/rxe_pool.h @@ -43,9 +43,11 @@ struct rxe_pool_elem { struct rxe_pool { struct rxe_dev *rxe; const char *name; - void (*cleanup)(struct rxe_pool_elem *obj); + int (*init)(struct rxe_pool_elem *elem); + void (*cleanup)(struct rxe_pool_elem *elem); enum rxe_pool_flags flags; enum rxe_elem_type type; + struct list_head free_list; unsigned int max_elem; atomic_t num_elem; @@ -71,78 +73,33 @@ struct rxe_pool { #define rxe_pool_unlock_bh(pool) xa_unlock_bh(&pool->xarray.xa) void rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool, - enum rxe_elem_type type, u32 max_elem); + enum rxe_elem_type type, u32 max_elem); -/* free resources from object pool */ void rxe_pool_cleanup(struct rxe_pool *pool); -/* allocate an object from pool holding and not holding the pool lock */ -void *rxe_alloc_locked(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_elem *elem); - #define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->elem) -/* assign a key to a keyed object and insert object into - * pool's rb tree holding and not holding pool_lock - */ -int __rxe_add_key_locked(struct rxe_pool_elem *elem, void *key); - -#define rxe_add_key_locked(obj, key) __rxe_add_key_locked(&(obj)->elem, key) - -int __rxe_add_key(struct rxe_pool_elem *elem, void *key); - -#define rxe_add_key(obj, key) __rxe_add_key(&(obj)->elem, key) - -/* remove elem from rb tree holding and not holding the pool_lock */ -void __rxe_drop_key_locked(struct rxe_pool_elem *elem); - -#define rxe_drop_key_locked(obj) __rxe_drop_key_locked(&(obj)->elem) - -void __rxe_drop_key(struct rxe_pool_elem *elem); - -#define rxe_drop_key(obj) __rxe_drop_key(&(obj)->elem) - void *rxe_pool_get_index(struct rxe_pool *pool, u32 index); -/* lookup keyed object from key holding and not holding the pool_lock. - * takes a reference on the objecti - */ -void *rxe_pool_get_key_locked(struct rxe_pool *pool, void *key); - void *rxe_pool_get_key(struct rxe_pool *pool, void *key); -/* cleanup an object when all references are dropped */ -void rxe_elem_release(struct kref *kref); +void *rxe_pool_add_key(struct rxe_pool *pool, void *key); -/** - * __rxe_add_ref() - adds a reference to a pool element - * @elem: pool element - */ static inline void __rxe_add_ref(struct rxe_pool_elem *elem) { - return kref_get(&elem->ref_cnt); + kref_get(&elem->ref_cnt); } - #define rxe_add_ref(obj) __rxe_add_ref(&(obj)->elem) -/** - * __rxe_drop_ref() - drops a reference to a pool element - * @elem: pool element - * - * Drop reference to pool element and call rxe_elem_release() if - * the reference count reaches zero. - * - * Returns: 1 if rxe_elem_release is called else 0. - */ +void rxe_elem_release(struct kref *kref); + static inline bool __rxe_drop_ref(struct rxe_pool_elem *elem) { return kref_put(&elem->ref_cnt, rxe_elem_release); } - #define rxe_drop_ref(obj) __rxe_drop_ref(&(obj)->elem) #endif /* RXE_POOL_H */ From patchwork Thu Dec 16 23:31:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 12683005 X-Patchwork-Delegate: jgg@ziepe.ca Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 84B7DC43217 for ; Thu, 16 Dec 2021 23:34:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229742AbhLPXeD (ORCPT ); Thu, 16 Dec 2021 18:34:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229750AbhLPXeC (ORCPT ); Thu, 16 Dec 2021 18:34:02 -0500 Received: from mail-oi1-x229.google.com (mail-oi1-x229.google.com [IPv6:2607:f8b0:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 397EDC06173E for ; Thu, 16 Dec 2021 15:34:02 -0800 (PST) Received: by mail-oi1-x229.google.com with SMTP id 7so1103000oip.12 for ; Thu, 16 Dec 2021 15:34:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=g0lv6MwU7O424lrSMNF72fEwBygTrHb+eHXQDmW9jHg=; b=OwxkT5DACsHkJO9VP3MLeMk9Oq73HTozR9GThFytGtrrX7xYbvVIF3J57xGhLTCWVn xPq/nK5Nj6QAs6Nf9Mn42b3NkiayVpiUJVRSTOq6btxslqVKeW0rV/t9s08rANNwfVN6 kXw9Q0b50Hmb6D4sudBY0aYJ5iJRVAS5hhfemEERPT6+GI+XTy9DtEawo/RkGYUcJPGl 1ptkYbznpmJ5lrL37suh5fuN71IEjF7GfKyjzruWAflCBKNr2NHXJ1LQ14Zc+DTQypIf mGsL2V4YZ3Mg/MywwM2s89AMDvhf1lAzQgLbbPrYH83FwdJNQa1tN12XHdVI90+gCRk3 pLHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=g0lv6MwU7O424lrSMNF72fEwBygTrHb+eHXQDmW9jHg=; b=V366HQXT5jpEJPVP3eeiZLvUv+9tVqm8Pg768ZkV+ASXGZ78VOWXOSdVr89rN1dnD0 snCcTGktjiacF5Zkct93kgL7ks/jxo+X79NtlDX44q6as9GEjRPEjgEz9rgL/hCjpwxV v1z9LrKgo1m+OUzQyCA6328Ge3eiqsTA3mSiTUx/tBSrnQBOXTz5eUSv8cAd8KG+yp49 4RzJmOsASBCe+SnIPoTLk8G2+DLVKnJ0hQXAiaiqFuqQ1kgkpet4PzCXA+1xLOWme8/i CyWIPJD/pzpcONK6678r2wlP4fgRNmPBJmhMxLGGn2C6iv1ALGQI0jyUcbc8k6XFuwhB GZIA== X-Gm-Message-State: AOAM533xWfuleYoilpz14WxRRBo/S+DDdeDpSP7C6Rq9DJye+jsCP+iu jDYjxJF9xnxmKBTsSZBXQNJP3+vPNSE= X-Google-Smtp-Source: ABdhPJxggwfKg73Z7ZI3mbuyRqX6tceFzrXbAn/L/u1apdgeC1BSFoWyKws4w2m+z6caf29mAxhdDQ== X-Received: by 2002:aca:280d:: with SMTP id 13mr6192783oix.61.1639697641620; Thu, 16 Dec 2021 15:34:01 -0800 (PST) Received: from ubuntu-21.tx.rr.com (2603-8081-140c-1a00-ec41-d089-dfdb-6fb5.res6.spectrum.com. [2603:8081:140c:1a00:ec41:d089:dfdb:6fb5]) by smtp.googlemail.com with ESMTPSA id w19sm1253888oih.44.2021.12.16.15.34.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Dec 2021 15:34:01 -0800 (PST) From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next v8 4/8] RDMA/rxe: Fix ref error in rxe_av.c Date: Thu, 16 Dec 2021 17:31:58 -0600 Message-Id: <20211216233201.14893-5-rpearsonhpe@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211216233201.14893-1-rpearsonhpe@gmail.com> References: <20211216233201.14893-1-rpearsonhpe@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org The commit referenced below can take a reference to the AH which is never dropped. This only happens in the UD request path. This patch optionally passes that AH back to the caller so that it can hold the reference while the AV is being accessed and then drop it. Code to do this is added to rxe_req.c. The AV is also passed to rxe_prepare in rxe_net.c as an optimization. Fixes: e2fe06c90806 ("RDMA/rxe: Lookup kernel AH from ah index in UD WQEs") Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe_av.c | 19 +++++++++- drivers/infiniband/sw/rxe/rxe_loc.h | 5 ++- drivers/infiniband/sw/rxe/rxe_net.c | 17 +++++---- drivers/infiniband/sw/rxe/rxe_req.c | 55 +++++++++++++++++----------- drivers/infiniband/sw/rxe/rxe_resp.c | 2 +- 5 files changed, 63 insertions(+), 35 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe_av.c b/drivers/infiniband/sw/rxe/rxe_av.c index 38c7b6fb39d7..360a567159fe 100644 --- a/drivers/infiniband/sw/rxe/rxe_av.c +++ b/drivers/infiniband/sw/rxe/rxe_av.c @@ -99,11 +99,14 @@ void rxe_av_fill_ip_info(struct rxe_av *av, struct rdma_ah_attr *attr) av->network_type = type; } -struct rxe_av *rxe_get_av(struct rxe_pkt_info *pkt) +struct rxe_av *rxe_get_av(struct rxe_pkt_info *pkt, struct rxe_ah **ahp) { struct rxe_ah *ah; u32 ah_num; + if (ahp) + *ahp = NULL; + if (!pkt || !pkt->qp) return NULL; @@ -117,10 +120,22 @@ struct rxe_av *rxe_get_av(struct rxe_pkt_info *pkt) if (ah_num) { /* only new user provider or kernel client */ ah = rxe_pool_get_index(&pkt->rxe->ah_pool, ah_num); - if (!ah || ah->ah_num != ah_num || rxe_ah_pd(ah) != pkt->qp->pd) { + if (!ah) { pr_warn("Unable to find AH matching ah_num\n"); return NULL; } + + if (rxe_ah_pd(ah) != pkt->qp->pd) { + pr_warn("PDs don't match for AH and QP\n"); + rxe_drop_ref(ah); + return NULL; + } + + if (ahp) + *ahp = ah; + else + rxe_drop_ref(ah); + return &ah->av; } diff --git a/drivers/infiniband/sw/rxe/rxe_loc.h b/drivers/infiniband/sw/rxe/rxe_loc.h index 6558602be751..02d57c894e34 100644 --- a/drivers/infiniband/sw/rxe/rxe_loc.h +++ b/drivers/infiniband/sw/rxe/rxe_loc.h @@ -19,7 +19,7 @@ void rxe_av_to_attr(struct rxe_av *av, struct rdma_ah_attr *attr); void rxe_av_fill_ip_info(struct rxe_av *av, struct rdma_ah_attr *attr); -struct rxe_av *rxe_get_av(struct rxe_pkt_info *pkt); +struct rxe_av *rxe_get_av(struct rxe_pkt_info *pkt, struct rxe_ah **ahp); /* rxe_cq.c */ int rxe_cq_chk_attr(struct rxe_dev *rxe, struct rxe_cq *cq, @@ -99,7 +99,8 @@ void rxe_mw_cleanup(struct rxe_pool_elem *arg); /* rxe_net.c */ struct sk_buff *rxe_init_packet(struct rxe_dev *rxe, struct rxe_av *av, int paylen, struct rxe_pkt_info *pkt); -int rxe_prepare(struct rxe_pkt_info *pkt, struct sk_buff *skb); +int rxe_prepare(struct rxe_av *av, struct rxe_pkt_info *pkt, + struct sk_buff *skb); int rxe_xmit_packet(struct rxe_qp *qp, struct rxe_pkt_info *pkt, struct sk_buff *skb); const char *rxe_parent_name(struct rxe_dev *rxe, unsigned int port_num); diff --git a/drivers/infiniband/sw/rxe/rxe_net.c b/drivers/infiniband/sw/rxe/rxe_net.c index 2cb810cb890a..456e960cacd7 100644 --- a/drivers/infiniband/sw/rxe/rxe_net.c +++ b/drivers/infiniband/sw/rxe/rxe_net.c @@ -293,13 +293,13 @@ static void prepare_ipv6_hdr(struct dst_entry *dst, struct sk_buff *skb, ip6h->payload_len = htons(skb->len - sizeof(*ip6h)); } -static int prepare4(struct rxe_pkt_info *pkt, struct sk_buff *skb) +static int prepare4(struct rxe_av *av, struct rxe_pkt_info *pkt, + struct sk_buff *skb) { struct rxe_qp *qp = pkt->qp; struct dst_entry *dst; bool xnet = false; __be16 df = htons(IP_DF); - struct rxe_av *av = rxe_get_av(pkt); struct in_addr *saddr = &av->sgid_addr._sockaddr_in.sin_addr; struct in_addr *daddr = &av->dgid_addr._sockaddr_in.sin_addr; @@ -319,11 +319,11 @@ static int prepare4(struct rxe_pkt_info *pkt, struct sk_buff *skb) return 0; } -static int prepare6(struct rxe_pkt_info *pkt, struct sk_buff *skb) +static int prepare6(struct rxe_av *av, struct rxe_pkt_info *pkt, + struct sk_buff *skb) { struct rxe_qp *qp = pkt->qp; struct dst_entry *dst; - struct rxe_av *av = rxe_get_av(pkt); struct in6_addr *saddr = &av->sgid_addr._sockaddr_in6.sin6_addr; struct in6_addr *daddr = &av->dgid_addr._sockaddr_in6.sin6_addr; @@ -344,16 +344,17 @@ static int prepare6(struct rxe_pkt_info *pkt, struct sk_buff *skb) return 0; } -int rxe_prepare(struct rxe_pkt_info *pkt, struct sk_buff *skb) +int rxe_prepare(struct rxe_av *av, struct rxe_pkt_info *pkt, + struct sk_buff *skb) { int err = 0; if (skb->protocol == htons(ETH_P_IP)) - err = prepare4(pkt, skb); + err = prepare4(av, pkt, skb); else if (skb->protocol == htons(ETH_P_IPV6)) - err = prepare6(pkt, skb); + err = prepare6(av, pkt, skb); - if (ether_addr_equal(skb->dev->dev_addr, rxe_get_av(pkt)->dmac)) + if (ether_addr_equal(skb->dev->dev_addr, av->dmac)) pkt->mask |= RXE_LOOPBACK_MASK; return err; diff --git a/drivers/infiniband/sw/rxe/rxe_req.c b/drivers/infiniband/sw/rxe/rxe_req.c index 5eb89052dd66..f44535f82bea 100644 --- a/drivers/infiniband/sw/rxe/rxe_req.c +++ b/drivers/infiniband/sw/rxe/rxe_req.c @@ -358,6 +358,7 @@ static inline int get_mtu(struct rxe_qp *qp) } static struct sk_buff *init_req_packet(struct rxe_qp *qp, + struct rxe_av *av, struct rxe_send_wqe *wqe, int opcode, int payload, struct rxe_pkt_info *pkt) @@ -365,7 +366,6 @@ static struct sk_buff *init_req_packet(struct rxe_qp *qp, struct rxe_dev *rxe = to_rdev(qp->ibqp.device); struct sk_buff *skb; struct rxe_send_wr *ibwr = &wqe->wr; - struct rxe_av *av; int pad = (-payload) & 0x3; int paylen; int solicited; @@ -374,21 +374,9 @@ static struct sk_buff *init_req_packet(struct rxe_qp *qp, /* length from start of bth to end of icrc */ paylen = rxe_opcode[opcode].length + payload + pad + RXE_ICRC_SIZE; - - /* pkt->hdr, port_num and mask are initialized in ifc layer */ - pkt->rxe = rxe; - pkt->opcode = opcode; - pkt->qp = qp; - pkt->psn = qp->req.psn; - pkt->mask = rxe_opcode[opcode].mask; - pkt->paylen = paylen; - pkt->wqe = wqe; + pkt->paylen = paylen; /* init skb */ - av = rxe_get_av(pkt); - if (!av) - return NULL; - skb = rxe_init_packet(rxe, av, paylen, pkt); if (unlikely(!skb)) return NULL; @@ -447,13 +435,13 @@ static struct sk_buff *init_req_packet(struct rxe_qp *qp, return skb; } -static int finish_packet(struct rxe_qp *qp, struct rxe_send_wqe *wqe, - struct rxe_pkt_info *pkt, struct sk_buff *skb, - int paylen) +static int finish_packet(struct rxe_qp *qp, struct rxe_av *av, + struct rxe_send_wqe *wqe, struct rxe_pkt_info *pkt, + struct sk_buff *skb, int paylen) { int err; - err = rxe_prepare(pkt, skb); + err = rxe_prepare(av, pkt, skb); if (err) return err; @@ -608,6 +596,7 @@ static int rxe_do_local_ops(struct rxe_qp *qp, struct rxe_send_wqe *wqe) int rxe_requester(void *arg) { struct rxe_qp *qp = (struct rxe_qp *)arg; + struct rxe_dev *rxe = to_rdev(qp->ibqp.device); struct rxe_pkt_info pkt; struct sk_buff *skb; struct rxe_send_wqe *wqe; @@ -619,6 +608,8 @@ int rxe_requester(void *arg) struct rxe_send_wqe rollback_wqe; u32 rollback_psn; struct rxe_queue *q = qp->sq.queue; + struct rxe_ah *ah; + struct rxe_av *av; rxe_add_ref(qp); @@ -705,14 +696,28 @@ int rxe_requester(void *arg) payload = mtu; } - skb = init_req_packet(qp, wqe, opcode, payload, &pkt); + pkt.rxe = rxe; + pkt.opcode = opcode; + pkt.qp = qp; + pkt.psn = qp->req.psn; + pkt.mask = rxe_opcode[opcode].mask; + pkt.wqe = wqe; + + av = rxe_get_av(&pkt, &ah); + if (unlikely(!av)) { + pr_err("qp#%d Failed no address vector\n", qp_num(qp)); + wqe->status = IB_WC_LOC_QP_OP_ERR; + goto err_drop_ah; + } + + skb = init_req_packet(qp, av, wqe, opcode, payload, &pkt); if (unlikely(!skb)) { pr_err("qp#%d Failed allocating skb\n", qp_num(qp)); wqe->status = IB_WC_LOC_QP_OP_ERR; - goto err; + goto err_drop_ah; } - ret = finish_packet(qp, wqe, &pkt, skb, payload); + ret = finish_packet(qp, av, wqe, &pkt, skb, payload); if (unlikely(ret)) { pr_debug("qp#%d Error during finish packet\n", qp_num(qp)); if (ret == -EFAULT) @@ -720,9 +725,12 @@ int rxe_requester(void *arg) else wqe->status = IB_WC_LOC_QP_OP_ERR; kfree_skb(skb); - goto err; + goto err_drop_ah; } + if (ah) + rxe_drop_ref(ah); + /* * To prevent a race on wqe access between requester and completer, * wqe members state and psn need to be set before calling @@ -751,6 +759,9 @@ int rxe_requester(void *arg) goto next_wqe; +err_drop_ah: + if (ah) + rxe_drop_ref(ah); err: wqe->state = wqe_state_error; __rxe_do_task(&qp->comp.task); diff --git a/drivers/infiniband/sw/rxe/rxe_resp.c b/drivers/infiniband/sw/rxe/rxe_resp.c index e8f435fa6e4d..f589f4dde35c 100644 --- a/drivers/infiniband/sw/rxe/rxe_resp.c +++ b/drivers/infiniband/sw/rxe/rxe_resp.c @@ -632,7 +632,7 @@ static struct sk_buff *prepare_ack_packet(struct rxe_qp *qp, if (ack->mask & RXE_ATMACK_MASK) atmack_set_orig(ack, qp->resp.atomic_orig); - err = rxe_prepare(ack, skb); + err = rxe_prepare(&qp->pri_av, ack, skb); if (err) { kfree_skb(skb); return NULL; From patchwork Thu Dec 16 23:31:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 12683003 X-Patchwork-Delegate: jgg@ziepe.ca Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22216C4321E for ; Thu, 16 Dec 2021 23:34:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229744AbhLPXeD (ORCPT ); Thu, 16 Dec 2021 18:34:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58360 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229749AbhLPXeD (ORCPT ); Thu, 16 Dec 2021 18:34:03 -0500 Received: from mail-ot1-x336.google.com (mail-ot1-x336.google.com [IPv6:2607:f8b0:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEB22C061574 for ; Thu, 16 Dec 2021 15:34:02 -0800 (PST) Received: by mail-ot1-x336.google.com with SMTP id h19-20020a9d3e53000000b0056547b797b2so792933otg.4 for ; Thu, 16 Dec 2021 15:34:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tSG/yPkJyGVBBupNaOjL+CRjSnN8d65PWUmoqX4f20o=; b=mO78CSMW5KuXAbItFkeCtfZtpYiZoRFbYG5mnnqWNAFM8VDp2ZAQiYY/oVFmyKDTV+ MyNIlSOtBiP62yz4PHpqCUmdduGPK7vS7U02vPIdiAH/tsT802AwuMyXVkKmbsie7XV4 vt9dHVrC/Ilc0ahF74EhIK/iaKxAOIqtrjVZuq54AY0TtdJoon0raHFgrb8hJU7+tSJZ byKMYqWsEdcuivNK86/rvYNynYk2ZD3wy40IMNWUNXb7Jq+wL66TQ4XD1ewz8HKQNpRX +bGmvRKpPMgt5uR4zE+xBgJYiOMJnKafDm8ZLHN6UJ6X6napS26IfcmgTMZDGWNtNtA4 Oc5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tSG/yPkJyGVBBupNaOjL+CRjSnN8d65PWUmoqX4f20o=; b=cFbvVRtxWR20ucL99DIQuYJd9IT1yDRaCDztwybcLMfwR7Mq++NegxX+SK64Pb5LKu HJvIfkrrDEmv1ZGPvwKlnbxU35cn8v0Aaaz3VZAD7DUFzWuouDzW55+Goy/stJnea00q HDG5oX+r544PKkCvp5VHrg/TkgArmBeoguV625NfkjMQXUyqQkyENsgYNlsyRZ4X16n3 jmbl0r1gXLKuLqfaeIB7xJPVsqqTnzy77HiYxuS0TyoUHn3B/8mFuLQIOf1KP9FYNhmB G5k9sUsfQUhrNu9joqvBxj6srumLaAREbz7CV5VeDYMM5RRcRGfVIG2cZHuXNL94HP/S nhlA== X-Gm-Message-State: AOAM5304Agh2oqvLNnHOcKMpM7L1Ew3CMBOENaES4UJIpUymlxcKLcxO FCkZOpIa/MxE60ebbpjm/+rFQk9kuBw= X-Google-Smtp-Source: ABdhPJzPUZp9rKWtWzKdXFMHztemrANSlJvhw/Fi+6Z7h5binsOg/8jKQz9wCc+2YvlCkpBCf6bbZA== X-Received: by 2002:a9d:ef8:: with SMTP id 111mr355266otj.218.1639697642257; Thu, 16 Dec 2021 15:34:02 -0800 (PST) Received: from ubuntu-21.tx.rr.com (2603-8081-140c-1a00-ec41-d089-dfdb-6fb5.res6.spectrum.com. [2603:8081:140c:1a00:ec41:d089:dfdb:6fb5]) by smtp.googlemail.com with ESMTPSA id w19sm1253888oih.44.2021.12.16.15.34.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Dec 2021 15:34:01 -0800 (PST) From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next v8 5/8] RDMA/rxe: Replace mr by rkey in responder resources Date: Thu, 16 Dec 2021 17:31:59 -0600 Message-Id: <20211216233201.14893-6-rpearsonhpe@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211216233201.14893-1-rpearsonhpe@gmail.com> References: <20211216233201.14893-1-rpearsonhpe@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Currently rxe saves a copy of MR in responder resources for RDMA reads. Since the responder resources are never freed just over written if more are needed this MR may not have a reference freed until the QP is destroyed. This patch uses the rkey instead of the MR and on subsequent packets of a multipacket read reply message it looks up the MR from the rkey for each packet. This makes it possible for a user to deregister an MR or unbind a MW on the fly and get correct behaviour. Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe_qp.c | 10 +-- drivers/infiniband/sw/rxe/rxe_resp.c | 123 ++++++++++++++++++-------- drivers/infiniband/sw/rxe/rxe_verbs.h | 1 - 3 files changed, 87 insertions(+), 47 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe_qp.c b/drivers/infiniband/sw/rxe/rxe_qp.c index afe11f475b8c..0558253fe734 100644 --- a/drivers/infiniband/sw/rxe/rxe_qp.c +++ b/drivers/infiniband/sw/rxe/rxe_qp.c @@ -135,12 +135,8 @@ static void free_rd_atomic_resources(struct rxe_qp *qp) void free_rd_atomic_resource(struct rxe_qp *qp, struct resp_res *res) { - if (res->type == RXE_ATOMIC_MASK) { + if (res->type == RXE_ATOMIC_MASK) kfree_skb(res->atomic.skb); - } else if (res->type == RXE_READ_MASK) { - if (res->read.mr) - rxe_drop_ref(res->read.mr); - } res->type = 0; } @@ -817,10 +813,8 @@ static void rxe_qp_do_cleanup(struct work_struct *work) if (qp->pd) rxe_drop_ref(qp->pd); - if (qp->resp.mr) { + if (qp->resp.mr) rxe_drop_ref(qp->resp.mr); - qp->resp.mr = NULL; - } if (qp_type(qp) == IB_QPT_RC) sk_dst_reset(qp->sk->sk); diff --git a/drivers/infiniband/sw/rxe/rxe_resp.c b/drivers/infiniband/sw/rxe/rxe_resp.c index f589f4dde35c..c776289842e5 100644 --- a/drivers/infiniband/sw/rxe/rxe_resp.c +++ b/drivers/infiniband/sw/rxe/rxe_resp.c @@ -641,6 +641,78 @@ static struct sk_buff *prepare_ack_packet(struct rxe_qp *qp, return skb; } +static struct resp_res *rxe_prepare_read_res(struct rxe_qp *qp, + struct rxe_pkt_info *pkt) +{ + struct resp_res *res; + u32 pkts; + + res = &qp->resp.resources[qp->resp.res_head]; + rxe_advance_resp_resource(qp); + free_rd_atomic_resource(qp, res); + + res->type = RXE_READ_MASK; + res->replay = 0; + res->read.va = qp->resp.va + qp->resp.offset; + res->read.va_org = qp->resp.va + qp->resp.offset; + res->read.resid = qp->resp.resid; + res->read.length = qp->resp.resid; + res->read.rkey = qp->resp.rkey; + + pkts = max_t(u32, (reth_len(pkt) + qp->mtu - 1)/qp->mtu, 1); + res->first_psn = pkt->psn; + res->cur_psn = pkt->psn; + res->last_psn = (pkt->psn + pkts - 1) & BTH_PSN_MASK; + + res->state = rdatm_res_state_new; + + return res; +} + +/** + * rxe_recheck_mr - revalidate MR from rkey and get a reference + * @qp: the qp + * @rkey: the rkey + * + * This code allows the MR to be invalidated or deregistered or + * the MW if one was used to be invalidated or deallocated. + * It is assumed that the access permissions if originally good + * are OK and the mappings to be unchanged. + * + * Return: mr on success else NULL + */ +static struct rxe_mr *rxe_recheck_mr(struct rxe_qp *qp, u32 rkey) +{ + struct rxe_dev *rxe = to_rdev(qp->ibqp.device); + struct rxe_mr *mr; + struct rxe_mw *mw; + + if (rkey_is_mw(rkey)) { + mw = rxe_pool_get_index(&rxe->mw_pool, rkey >> 8); + if (!mw || mw->rkey != rkey) + return NULL; + + if (mw->state != RXE_MW_STATE_VALID) { + rxe_drop_ref(mw); + return NULL; + } + + mr = mw->mr; + rxe_drop_ref(mw); + } else { + mr = rxe_pool_get_index(&rxe->mr_pool, rkey >> 8); + if (!mr || mr->rkey != rkey) + return NULL; + } + + if (mr->state != RXE_MR_STATE_VALID) { + rxe_drop_ref(mr); + return NULL; + } + + return mr; +} + /* RDMA read response. If res is not NULL, then we have a current RDMA request * being processed or replayed. */ @@ -655,53 +727,26 @@ static enum resp_states read_reply(struct rxe_qp *qp, int opcode; int err; struct resp_res *res = qp->resp.res; + struct rxe_mr *mr; if (!res) { - /* This is the first time we process that request. Get a - * resource - */ - res = &qp->resp.resources[qp->resp.res_head]; - - free_rd_atomic_resource(qp, res); - rxe_advance_resp_resource(qp); - - res->type = RXE_READ_MASK; - res->replay = 0; - - res->read.va = qp->resp.va + - qp->resp.offset; - res->read.va_org = qp->resp.va + - qp->resp.offset; - - res->first_psn = req_pkt->psn; - - if (reth_len(req_pkt)) { - res->last_psn = (req_pkt->psn + - (reth_len(req_pkt) + mtu - 1) / - mtu - 1) & BTH_PSN_MASK; - } else { - res->last_psn = res->first_psn; - } - res->cur_psn = req_pkt->psn; - - res->read.resid = qp->resp.resid; - res->read.length = qp->resp.resid; - res->read.rkey = qp->resp.rkey; - - /* note res inherits the reference to mr from qp */ - res->read.mr = qp->resp.mr; - qp->resp.mr = NULL; - - qp->resp.res = res; - res->state = rdatm_res_state_new; + res = rxe_prepare_read_res(qp, req_pkt); + qp->resp.res = res; } if (res->state == rdatm_res_state_new) { + mr = qp->resp.mr; + qp->resp.mr = NULL; + if (res->read.resid <= mtu) opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY; else opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST; } else { + mr = rxe_recheck_mr(qp, res->read.rkey); + if (!mr) + return RESPST_ERR_RKEY_VIOLATION; + if (res->read.resid > mtu) opcode = IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE; else @@ -717,10 +762,12 @@ static enum resp_states read_reply(struct rxe_qp *qp, if (!skb) return RESPST_ERR_RNR; - err = rxe_mr_copy(res->read.mr, res->read.va, payload_addr(&ack_pkt), + err = rxe_mr_copy(mr, res->read.va, payload_addr(&ack_pkt), payload, RXE_FROM_MR_OBJ); if (err) pr_err("Failed copying memory\n"); + if (mr) + rxe_drop_ref(mr); if (bth_pad(&ack_pkt)) { u8 *pad = payload_addr(&ack_pkt) + payload; diff --git a/drivers/infiniband/sw/rxe/rxe_verbs.h b/drivers/infiniband/sw/rxe/rxe_verbs.h index caf1ce118765..022abba4fb6b 100644 --- a/drivers/infiniband/sw/rxe/rxe_verbs.h +++ b/drivers/infiniband/sw/rxe/rxe_verbs.h @@ -157,7 +157,6 @@ struct resp_res { struct sk_buff *skb; } atomic; struct { - struct rxe_mr *mr; u64 va_org; u32 rkey; u32 length; From patchwork Thu Dec 16 23:32:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 12683007 X-Patchwork-Delegate: jgg@ziepe.ca Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BCADEC433EF for ; Thu, 16 Dec 2021 23:34:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229752AbhLPXeE (ORCPT ); Thu, 16 Dec 2021 18:34:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229750AbhLPXeD (ORCPT ); Thu, 16 Dec 2021 18:34:03 -0500 Received: from mail-ot1-x32b.google.com (mail-ot1-x32b.google.com [IPv6:2607:f8b0:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8655DC061574 for ; Thu, 16 Dec 2021 15:34:03 -0800 (PST) Received: by mail-ot1-x32b.google.com with SMTP id w6-20020a9d77c6000000b0055e804fa524so805984otl.3 for ; Thu, 16 Dec 2021 15:34:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IHJlHBGQEKLPNB7NU0XNAwaiiI209QcU3LGAPQ9g4f8=; b=GEiaiQAtjiZCq+dpw06FDVZDJYhLz3zxsvJDleWlnfeuhwOZQCaA1/HvwvzK1vTS+g R0xZoR+Mz9tZFNQPDIrxVRv2EhdC1059gamK7euoptXfCscssQIJdFXBvSKPoGULDnVp yvE1Qr6gEKAWoMrvKiWTkD/UeuFU5ibzN61JHtHIPFAJSLd8WUKFh4+Twqvu3F3MZleT GLCEUbJ/Cf3pTJ/Arw8UpJoVqeY/s9l7lodEIJanYJUgtZwdAwRqtTxwMOKYFaRz9Q0q aFWKO+WMqVpIDDz9HQN/evNMltZNwzoDOzewHEpEgavYyFnOZ59pHwItntMrZeVCdpoV eSew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IHJlHBGQEKLPNB7NU0XNAwaiiI209QcU3LGAPQ9g4f8=; b=W8rZRuwWqjVRuxk4lPie/ACru2pdMldj86FsSX0j+m+7Es9oJMWIFlMd8Jd5l+eMvt GIkycJAsWmGuQIGRWF83B+tgM93ujGr6Oe+PNt/nQiE1GijYlj0Woj+EO3a3gYOOM9SO imnIzsgkiydA3hHIc1ZZ+2IM2FNxqzmspvUJ85UgxJjFrZ33Zo4J9IStDO8U/0mXt+vI 9ewQRnRZfVGS8cQ2DzBBIqaDneACGz63XLj0M3/u1n9l7kbIrO0g2KpEZBkt71ZHRHqp x8w/qcSnhKcpXchwNBG3QvZJanB6Ht9AZht0ZSveG1S9LRXi99D138UdZ6sRMSEVT2SF rmDQ== X-Gm-Message-State: AOAM5308QO7H1G6lGIDuoRfWrnAs+NATZjzyFz/9Xi/JGZWw4PTQ+BmT pqsmR8Cxsw6u25X5X3wQcEwYnlKBtNU= X-Google-Smtp-Source: ABdhPJzbmFm5Kgxxkczl1fonXWlmdaXm+LYAyVn1Rr/SyTytxCha2ieohbZ8GOjmOFjBAQmZSWU1WQ== X-Received: by 2002:a9d:7dca:: with SMTP id k10mr344918otn.274.1639697642980; Thu, 16 Dec 2021 15:34:02 -0800 (PST) Received: from ubuntu-21.tx.rr.com (2603-8081-140c-1a00-ec41-d089-dfdb-6fb5.res6.spectrum.com. [2603:8081:140c:1a00:ec41:d089:dfdb:6fb5]) by smtp.googlemail.com with ESMTPSA id w19sm1253888oih.44.2021.12.16.15.34.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Dec 2021 15:34:02 -0800 (PST) From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next v8 6/8] RDMA/rxe: Minor cleanups in rxe_pool.c/rxe_pool.h Date: Thu, 16 Dec 2021 17:32:00 -0600 Message-Id: <20211216233201.14893-7-rpearsonhpe@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211216233201.14893-1-rpearsonhpe@gmail.com> References: <20211216233201.14893-1-rpearsonhpe@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org This patch includes a couple of minor cleanups in rxe_pool.c and rxe_pool.h Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe_pool.c | 10 +++------- drivers/infiniband/sw/rxe/rxe_pool.h | 1 - 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c index a5ff2fd692c9..dcfbc2b932af 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.c +++ b/drivers/infiniband/sw/rxe/rxe_pool.c @@ -97,11 +97,8 @@ static const struct rxe_type_info { }, }; -void rxe_pool_init( - struct rxe_dev *rxe, - struct rxe_pool *pool, - enum rxe_elem_type type, - unsigned int max_elem) +void rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool, + enum rxe_elem_type type, unsigned int max_elem) { const struct rxe_type_info *info = &rxe_type_info[type]; @@ -109,7 +106,6 @@ void rxe_pool_init( pool->rxe = rxe; pool->name = info->name; - pool->type = type; pool->max_elem = max_elem; pool->elem_size = ALIGN(info->size, RXE_POOL_ALIGN); pool->elem_offset = info->elem_offset; @@ -227,7 +223,7 @@ int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem) } /** - * rxe_pool_get_index - lookup object from index + * rxe_pool_get_index() - lookup object from index * @pool: the object pool * @index: the index of the object * diff --git a/drivers/infiniband/sw/rxe/rxe_pool.h b/drivers/infiniband/sw/rxe/rxe_pool.h index 894ffef4d6bd..be3b962d1c78 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.h +++ b/drivers/infiniband/sw/rxe/rxe_pool.h @@ -46,7 +46,6 @@ struct rxe_pool { int (*init)(struct rxe_pool_elem *elem); void (*cleanup)(struct rxe_pool_elem *elem); enum rxe_pool_flags flags; - enum rxe_elem_type type; struct list_head free_list; unsigned int max_elem; From patchwork Thu Dec 16 23:32:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 12683013 X-Patchwork-Delegate: jgg@ziepe.ca Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9BB77C433F5 for ; Thu, 16 Dec 2021 23:34:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229757AbhLPXeF (ORCPT ); Thu, 16 Dec 2021 18:34:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229749AbhLPXeE (ORCPT ); Thu, 16 Dec 2021 18:34:04 -0500 Received: from mail-oi1-x22b.google.com (mail-oi1-x22b.google.com [IPv6:2607:f8b0:4864:20::22b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BBE4C06173E for ; Thu, 16 Dec 2021 15:34:04 -0800 (PST) Received: by mail-oi1-x22b.google.com with SMTP id w64so1118183oif.10 for ; Thu, 16 Dec 2021 15:34:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ouh7zjGbN+ZSOtaWaqJLRp0o3MFaoSAc5TutDyk3nFc=; b=mg869s7mmuqTOfP5THD03dzYLJ6eLTSG2bGpzJmnU+pa+D9/kpjqq8BuO2AQhbTUai G2lT0a7cPVPGN2xOY5xDbsEx1RtcR+YrqOKpnNqNxO998dUKXlVU3wF15RNwt79WaWzD f0MACvu+GEN7FHaOzxZzsIiJdXQqHki2bW8QLA5uIE2RT97BusSa4cv1eyZmfSB/GkKP BOE4U4FFMmOcSt1lxvjQKvtxbVZPa2Y6BwrQjwoxeeMapJIcwkibe9YMmt4LTws6OpyF TL+yPgwRQ8jwfWCWk7OO46oagL0dc+MxBFg6krs3aX/dcZxomb0Lbo8AE1BthcvIqH4X G/Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ouh7zjGbN+ZSOtaWaqJLRp0o3MFaoSAc5TutDyk3nFc=; b=7nS0+6x2AiI560IlZc3w9Yrz1n3QfK2bUY22c73kuCJeKpFhz2KlGCxt9WSkNq1gT7 g2UX+hpM1k5ZcvwG2zMMYqX6GvAVs5TUme4WyWwDhBBUmUqHXO1GFjE7+mIcKqzY1B4l y5k0gdvH/mFLPuZMTwVVVpai3S8bjvTgMOKskWdxFC5bsi/zvA6FIjmXH5FhnhhiA2Oi r9Y+U/7O/tbnBZzGrcWi9R0x1ZHFewqby6tomPKyg7vRIaVBhheavUQl2mtui6DFU5ah RsQJERyF19YgaobRk3AqoRyst8k3SVoiUMe3MU5rA2lXYgL6j+xp/vPBAjHt/Hg465h7 YW3A== X-Gm-Message-State: AOAM530VPTMps9edYgyjUVHnZiGCXBWH2Aau4TJ5BojRu38Ek3sIWWAu LPUeu5wsHI20bNli7T6C45A= X-Google-Smtp-Source: ABdhPJyWiRKCok9pMgy5jPGxFiOdLyoM1FNv53C9jfbbn4VNw3Hr/xamxcfmNxNU70Cej6gLP9ArjQ== X-Received: by 2002:aca:603:: with SMTP id 3mr165723oig.98.1639697643703; Thu, 16 Dec 2021 15:34:03 -0800 (PST) Received: from ubuntu-21.tx.rr.com (2603-8081-140c-1a00-ec41-d089-dfdb-6fb5.res6.spectrum.com. [2603:8081:140c:1a00:ec41:d089:dfdb:6fb5]) by smtp.googlemail.com with ESMTPSA id w19sm1253888oih.44.2021.12.16.15.34.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Dec 2021 15:34:03 -0800 (PST) From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next v8 7/8] RDMA/rxe: Replace rxe_alloc by kzalloc for rxe_mc_elem Date: Thu, 16 Dec 2021 17:32:01 -0600 Message-Id: <20211216233201.14893-8-rpearsonhpe@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211216233201.14893-1-rpearsonhpe@gmail.com> References: <20211216233201.14893-1-rpearsonhpe@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Currently rxe_mc_elem structs are treated as rdma objects which is unneeded. This patch replaces rxe_alloc and rxe_drop_ref by kzalloc and kfree for these structs which hold associatons between multicast groups and QPs. Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe.c | 3 --- drivers/infiniband/sw/rxe/rxe_mcast.c | 22 ++++++++++++++-------- drivers/infiniband/sw/rxe/rxe_pool.c | 6 ------ drivers/infiniband/sw/rxe/rxe_pool.h | 1 - drivers/infiniband/sw/rxe/rxe_verbs.h | 2 -- 5 files changed, 14 insertions(+), 20 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe.c b/drivers/infiniband/sw/rxe/rxe.c index 09c73a0d8513..20a925aed29c 100644 --- a/drivers/infiniband/sw/rxe/rxe.c +++ b/drivers/infiniband/sw/rxe/rxe.c @@ -31,7 +31,6 @@ void rxe_dealloc(struct ib_device *ib_dev) rxe_pool_cleanup(&rxe->mr_pool); rxe_pool_cleanup(&rxe->mw_pool); rxe_pool_cleanup(&rxe->mc_grp_pool); - rxe_pool_cleanup(&rxe->mc_elem_pool); if (rxe->tfm) crypto_free_shash(rxe->tfm); @@ -128,8 +127,6 @@ static void rxe_init_pools(struct rxe_dev *rxe) rxe_pool_init(rxe, &rxe->mw_pool, RXE_TYPE_MW, rxe->attr.max_mw); rxe_pool_init(rxe, &rxe->mc_grp_pool, RXE_TYPE_MC_GRP, rxe->attr.max_mcast_grp); - rxe_pool_init(rxe, &rxe->mc_elem_pool, RXE_TYPE_MC_ELEM, - rxe->attr.max_total_mcast_qp_attach); } /* initialize rxe device state */ diff --git a/drivers/infiniband/sw/rxe/rxe_mcast.c b/drivers/infiniband/sw/rxe/rxe_mcast.c index e110c4d3fbf4..b935634f86cd 100644 --- a/drivers/infiniband/sw/rxe/rxe_mcast.c +++ b/drivers/infiniband/sw/rxe/rxe_mcast.c @@ -63,14 +63,15 @@ int rxe_mcast_add_grp_elem(struct rxe_dev *rxe, struct rxe_qp *qp, goto out; } - elem = rxe_alloc(&rxe->mc_elem_pool); + elem = kzalloc(sizeof(*elem), GFP_KERNEL); if (!elem) { err = -ENOMEM; goto out; } - /* each qp holds a ref on the grp */ + /* each elem holds a ref on the grp and the qp */ rxe_add_ref(grp); + rxe_add_ref(qp); grp->num_qp++; elem->qp = qp; @@ -91,6 +92,7 @@ int rxe_mcast_drop_grp_elem(struct rxe_dev *rxe, struct rxe_qp *qp, { struct rxe_mc_grp *grp; struct rxe_mc_elem *elem, *tmp; + int ret = -EINVAL; grp = rxe_pool_get_key(&rxe->mc_grp_pool, mgid); if (!grp) @@ -107,18 +109,21 @@ int rxe_mcast_drop_grp_elem(struct rxe_dev *rxe, struct rxe_qp *qp, spin_unlock_bh(&grp->mcg_lock); spin_unlock_bh(&qp->grp_lock); - rxe_drop_ref(elem); - rxe_drop_ref(grp); /* ref held by QP */ - rxe_drop_ref(grp); /* ref from get_key */ - return 0; + kfree(elem); + rxe_drop_ref(qp); /* ref held by elem */ + rxe_drop_ref(grp); /* ref held by elem */ + ret = 0; + goto out_drop_ref; } } spin_unlock_bh(&grp->mcg_lock); spin_unlock_bh(&qp->grp_lock); + +out_drop_ref: rxe_drop_ref(grp); /* ref from get_key */ err1: - return -EINVAL; + return ret; } void rxe_drop_all_mcast_groups(struct rxe_qp *qp) @@ -142,8 +147,9 @@ void rxe_drop_all_mcast_groups(struct rxe_qp *qp) list_del(&elem->qp_list); grp->num_qp--; spin_unlock_bh(&grp->mcg_lock); + rxe_drop_ref(qp); rxe_drop_ref(grp); - rxe_drop_ref(elem); + kfree(elem); } } diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c index dcfbc2b932af..0f785d14f646 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.c +++ b/drivers/infiniband/sw/rxe/rxe_pool.c @@ -89,12 +89,6 @@ static const struct rxe_type_info { .key_offset = offsetof(struct rxe_mc_grp, mgid), .key_size = sizeof(union ib_gid), }, - [RXE_TYPE_MC_ELEM] = { - .name = "rxe-mc_elem", - .size = sizeof(struct rxe_mc_elem), - .elem_offset = offsetof(struct rxe_mc_elem, elem), - .flags = RXE_POOL_ALLOC, - }, }; void rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool, diff --git a/drivers/infiniband/sw/rxe/rxe_pool.h b/drivers/infiniband/sw/rxe/rxe_pool.h index be3b962d1c78..ccb923b10276 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.h +++ b/drivers/infiniband/sw/rxe/rxe_pool.h @@ -23,7 +23,6 @@ enum rxe_elem_type { RXE_TYPE_MR, RXE_TYPE_MW, RXE_TYPE_MC_GRP, - RXE_TYPE_MC_ELEM, RXE_NUM_TYPES, /* keep me last */ }; diff --git a/drivers/infiniband/sw/rxe/rxe_verbs.h b/drivers/infiniband/sw/rxe/rxe_verbs.h index 022abba4fb6b..9f39b097a976 100644 --- a/drivers/infiniband/sw/rxe/rxe_verbs.h +++ b/drivers/infiniband/sw/rxe/rxe_verbs.h @@ -364,7 +364,6 @@ struct rxe_mc_grp { }; struct rxe_mc_elem { - struct rxe_pool_elem elem; struct list_head qp_list; struct list_head grp_list; struct rxe_qp *qp; @@ -402,7 +401,6 @@ struct rxe_dev { struct rxe_pool mr_pool; struct rxe_pool mw_pool; struct rxe_pool mc_grp_pool; - struct rxe_pool mc_elem_pool; spinlock_t pending_lock; /* guard pending_mmaps */ struct list_head pending_mmaps; From patchwork Thu Dec 16 23:32:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 12683015 X-Patchwork-Delegate: jgg@ziepe.ca Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ECBAEC433F5 for ; Thu, 16 Dec 2021 23:34:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229636AbhLPXeI (ORCPT ); Thu, 16 Dec 2021 18:34:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229758AbhLPXeF (ORCPT ); Thu, 16 Dec 2021 18:34:05 -0500 Received: from mail-oi1-x234.google.com (mail-oi1-x234.google.com [IPv6:2607:f8b0:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 287B7C061574 for ; Thu, 16 Dec 2021 15:34:05 -0800 (PST) Received: by mail-oi1-x234.google.com with SMTP id q25so1240936oiw.0 for ; Thu, 16 Dec 2021 15:34:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ctg4MN9ozVc98WDNFjU/p+VFavDwpuTq38LY2pJ8QQM=; b=VtIcOyJcJ9mI8syuPiZr6HUrGIui+ai8D2RvHOQGocvHSQBQJOqP9dg5vdYchqvJBy /PuYqT9gFT4lYxOlA038FuP8OwGsjtjxSdJfoepO9HES56abg32EfXHbINUz42iZQR85 BTnHw0AERt2bDwJ6bleF/P/jWtBGPrSYjpT47t/lEvM10D/oceif1xsHY1NsPoE6uhGJ 5kxexEO3pUt9xkp0MHoPGG2xzeEyfGh9yT8U68aqO0L/dnCCkDHOJ1Bnb35UzzyL95Af VpjKy6QPlYYiAPZS6setFtMcjD6aPeMWEOCjfvSJk+zZA5QHExM0badtZYYnPAhjZkmx VTvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ctg4MN9ozVc98WDNFjU/p+VFavDwpuTq38LY2pJ8QQM=; b=nLsk9QJi7HirwHI4GX//8gH5ZAD/c7dhR5adU7rl/ATw/axx7n4gn7xHregRUo7lYC jl2kAmm0hNkcSwN81t/20fLMCYQlQ0czdMioilv4U58KeoKnkh85jOCypkzq+RxTBVNO bkGQ+OFdCrCgP1gjmedW4RT2joeeyywVNMlSWEbMZ3V7+Who0XASXQRy6PWaNNUnXQlJ +WXuKBki+877wLtVgLApIZ8r21tXbrsClqquT3bcJJbVs/sV1rGRo3Hx5A3GnJkd80tp pPoEwmb4uKOpbdkgrItLRyCUUVCuxcS0OgTt7s4Wfi2iAf2Uww0/KT3tbZRwoNUqpbZ1 GtDg== X-Gm-Message-State: AOAM532Z85zIASzJNmYjhWnhonx1LPdiDMXxlByObmNRlYK+gjwFLFf2 dVY1jVS50tieJPVYuuQ347A= X-Google-Smtp-Source: ABdhPJxtCnwgy19yRu2FYrBAd13D58o1KPCDFBPtWOeMGFl/ndTnKADVN85FGPXkOUQZfqISySQKXA== X-Received: by 2002:a05:6808:1396:: with SMTP id c22mr6258479oiw.59.1639697644414; Thu, 16 Dec 2021 15:34:04 -0800 (PST) Received: from ubuntu-21.tx.rr.com (2603-8081-140c-1a00-ec41-d089-dfdb-6fb5.res6.spectrum.com. [2603:8081:140c:1a00:ec41:d089:dfdb:6fb5]) by smtp.googlemail.com with ESMTPSA id w19sm1253888oih.44.2021.12.16.15.34.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Dec 2021 15:34:04 -0800 (PST) From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson , kernel test robot Subject: [PATCH for-next v8 8/8] RDMA/rxe: Add wait for completion to obj destruct Date: Thu, 16 Dec 2021 17:32:02 -0600 Message-Id: <20211216233201.14893-9-rpearsonhpe@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211216233201.14893-1-rpearsonhpe@gmail.com> References: <20211216233201.14893-1-rpearsonhpe@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org This patch adds code to wait until pending activity on RDMA objects has completed before freeing or returning to rdma-core where the object may be freed. Reported-by: kernel test robot Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe_mcast.c | 10 +++++ drivers/infiniband/sw/rxe/rxe_mr.c | 2 + drivers/infiniband/sw/rxe/rxe_mw.c | 3 +- drivers/infiniband/sw/rxe/rxe_pool.c | 31 +++++++++++++- drivers/infiniband/sw/rxe/rxe_pool.h | 4 ++ drivers/infiniband/sw/rxe/rxe_verbs.c | 60 ++++++++++++++++++--------- 6 files changed, 89 insertions(+), 21 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe_mcast.c b/drivers/infiniband/sw/rxe/rxe_mcast.c index b935634f86cd..d91c2e30665a 100644 --- a/drivers/infiniband/sw/rxe/rxe_mcast.c +++ b/drivers/infiniband/sw/rxe/rxe_mcast.c @@ -122,6 +122,11 @@ int rxe_mcast_drop_grp_elem(struct rxe_dev *rxe, struct rxe_qp *qp, out_drop_ref: rxe_drop_ref(grp); /* ref from get_key */ + /* when grp ref count drops to zero + * go ahead and free it + */ + if (grp->elem.complete.done) + rxe_fini(grp); err1: return ret; } @@ -149,6 +154,11 @@ void rxe_drop_all_mcast_groups(struct rxe_qp *qp) spin_unlock_bh(&grp->mcg_lock); rxe_drop_ref(qp); rxe_drop_ref(grp); + /* when grp ref count drops to zero + * go ahead and free it + */ + if (grp->elem.complete.done) + rxe_fini(grp); kfree(elem); } } diff --git a/drivers/infiniband/sw/rxe/rxe_mr.c b/drivers/infiniband/sw/rxe/rxe_mr.c index 3c4390adfb80..5f8c08da352d 100644 --- a/drivers/infiniband/sw/rxe/rxe_mr.c +++ b/drivers/infiniband/sw/rxe/rxe_mr.c @@ -695,6 +695,8 @@ int rxe_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata) rxe_drop_ref(mr_pd(mr)); rxe_drop_ref(mr); + rxe_fini(mr); + return 0; } diff --git a/drivers/infiniband/sw/rxe/rxe_mw.c b/drivers/infiniband/sw/rxe/rxe_mw.c index 3ae981d77c25..666a641f9934 100644 --- a/drivers/infiniband/sw/rxe/rxe_mw.c +++ b/drivers/infiniband/sw/rxe/rxe_mw.c @@ -60,8 +60,9 @@ int rxe_dealloc_mw(struct ib_mw *ibmw) rxe_do_dealloc_mw(mw); spin_unlock_bh(&mw->lock); - rxe_drop_ref(mw); rxe_drop_ref(pd); + rxe_drop_ref(mw); + rxe_fini(mw); return 0; } diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c index 0f785d14f646..b23c58281eb6 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.c +++ b/drivers/infiniband/sw/rxe/rxe_pool.c @@ -6,6 +6,8 @@ #include "rxe.h" +/* timeout in jiffies for pool element to complete */ +#define RXE_POOL_TIMEOUT (100) #define RXE_POOL_ALIGN (16) static const struct rxe_type_info { @@ -160,6 +162,7 @@ void *rxe_alloc(struct rxe_pool *pool) elem->pool = pool; elem->obj = obj; kref_init(&elem->ref_cnt); + init_completion(&elem->complete); if (pool->init) { err = pool->init(elem); @@ -194,6 +197,7 @@ int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem) elem->pool = pool; elem->obj = (u8 *)elem - pool->elem_offset; kref_init(&elem->ref_cnt); + init_completion(&elem->complete); if (pool->init) { err = pool->init(elem); @@ -413,8 +417,33 @@ void rxe_elem_release(struct kref *kref) if (pool->cleanup) pool->cleanup(elem); + atomic_dec(&pool->num_elem); + + complete(&elem->complete); +} + +/** + * __rxe_fini() - wait for completion of pool element + * @elem: the pool elem + * + * Wait until the reference count of an object drops to zero when + * rxe_elem_release() will complete the object and then, if locally + * allocated, free the memory containing the object and return + * + * Returns: non-zero if the object completed successfully else zero + */ +int __rxe_fini(struct rxe_pool_elem *elem) +{ + struct rxe_pool *pool = elem->pool; + int ret; + + ret = wait_for_completion_timeout(&elem->complete, RXE_POOL_TIMEOUT); + if (!ret) + pr_warn("Timed out waiting for %s#%d to complete\n", + pool->name, elem->index); + if (pool->flags & RXE_POOL_ALLOC) kfree(elem->obj); - atomic_dec(&pool->num_elem); + return ret; } diff --git a/drivers/infiniband/sw/rxe/rxe_pool.h b/drivers/infiniband/sw/rxe/rxe_pool.h index ccb923b10276..252f25bf5a1a 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.h +++ b/drivers/infiniband/sw/rxe/rxe_pool.h @@ -30,6 +30,7 @@ struct rxe_pool_elem { struct rxe_pool *pool; void *obj; struct kref ref_cnt; + struct completion complete; struct list_head list; /* only used if keyed */ @@ -100,4 +101,7 @@ static inline bool __rxe_drop_ref(struct rxe_pool_elem *elem) } #define rxe_drop_ref(obj) __rxe_drop_ref(&(obj)->elem) +int __rxe_fini(struct rxe_pool_elem *elem); +#define rxe_fini(obj) __rxe_fini(&(obj)->elem) + #endif /* RXE_POOL_H */ diff --git a/drivers/infiniband/sw/rxe/rxe_verbs.c b/drivers/infiniband/sw/rxe/rxe_verbs.c index e3f64eae088c..450b5c557860 100644 --- a/drivers/infiniband/sw/rxe/rxe_verbs.c +++ b/drivers/infiniband/sw/rxe/rxe_verbs.c @@ -116,6 +116,7 @@ static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc) struct rxe_ucontext *uc = to_ruc(ibuc); rxe_drop_ref(uc); + rxe_fini(uc); } static int rxe_port_immutable(struct ib_device *dev, u32 port_num, @@ -150,6 +151,7 @@ static int rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) struct rxe_pd *pd = to_rpd(ibpd); rxe_drop_ref(pd); + rxe_fini(pd); return 0; } @@ -189,6 +191,7 @@ static int rxe_create_ah(struct ib_ah *ibah, sizeof(uresp->ah_num)); if (err) { rxe_drop_ref(ah); + rxe_fini(ah); return -EFAULT; } } else if (ah->is_user) { @@ -229,6 +232,7 @@ static int rxe_destroy_ah(struct ib_ah *ibah, u32 flags) struct rxe_ah *ah = to_rah(ibah); rxe_drop_ref(ah); + rxe_fini(ah); return 0; } @@ -297,25 +301,26 @@ static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init, err = rxe_srq_chk_attr(rxe, NULL, &init->attr, IB_SRQ_INIT_MASK); if (err) - goto err1; + goto err_out; err = rxe_add_to_pool(&rxe->srq_pool, srq); if (err) - goto err1; + goto err_out; rxe_add_ref(pd); srq->pd = pd; err = rxe_srq_from_init(rxe, srq, init, udata, uresp); if (err) - goto err2; + goto err_drop_pd; return 0; -err2: +err_drop_pd: rxe_drop_ref(pd); rxe_drop_ref(srq); -err1: + rxe_fini(srq); +err_out: return err; } @@ -373,6 +378,7 @@ static int rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata) rxe_drop_ref(srq->pd); rxe_drop_ref(srq); + rxe_fini(srq); return 0; } @@ -442,6 +448,7 @@ static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init, qp_init: rxe_drop_ref(qp); + rxe_fini(qp); return err; } @@ -486,6 +493,7 @@ static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata) rxe_qp_destroy(qp); rxe_drop_ref(qp); + rxe_fini(qp); return 0; } @@ -797,6 +805,7 @@ static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata) rxe_cq_disable(cq); rxe_drop_ref(cq); + rxe_fini(cq); return 0; } @@ -882,15 +891,22 @@ static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access) struct rxe_dev *rxe = to_rdev(ibpd->device); struct rxe_pd *pd = to_rpd(ibpd); struct rxe_mr *mr; + int err; mr = rxe_alloc(&rxe->mr_pool); - if (!mr) - return ERR_PTR(-ENOMEM); + if (!mr) { + err = -ENOMEM; + goto err_out; + } rxe_add_ref(pd); + rxe_mr_init_dma(pd, access, mr); return &mr->ibmr; + +err_out: + return ERR_PTR(err); } static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd, @@ -899,30 +915,30 @@ static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd, u64 iova, int access, struct ib_udata *udata) { - int err; struct rxe_dev *rxe = to_rdev(ibpd->device); struct rxe_pd *pd = to_rpd(ibpd); struct rxe_mr *mr; + int err; mr = rxe_alloc(&rxe->mr_pool); if (!mr) { err = -ENOMEM; - goto err2; + goto err_out; } - rxe_add_ref(pd); err = rxe_mr_init_user(pd, start, length, iova, access, mr); if (err) - goto err3; + goto err_drop_pd; return &mr->ibmr; -err3: +err_drop_pd: rxe_drop_ref(pd); rxe_drop_ref(mr); -err2: + rxe_fini(mr); +err_out: return ERR_PTR(err); } @@ -934,27 +950,30 @@ static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type, struct rxe_mr *mr; int err; - if (mr_type != IB_MR_TYPE_MEM_REG) - return ERR_PTR(-EINVAL); + if (mr_type != IB_MR_TYPE_MEM_REG) { + err = -EINVAL; + goto err_out; + } mr = rxe_alloc(&rxe->mr_pool); if (!mr) { err = -ENOMEM; - goto err1; + goto err_out; } rxe_add_ref(pd); err = rxe_mr_init_fast(pd, max_num_sg, mr); if (err) - goto err2; + goto err_drop_pd; return &mr->ibmr; -err2: +err_drop_pd: rxe_drop_ref(pd); rxe_drop_ref(mr); -err1: + rxe_fini(mr); +err_out: return ERR_PTR(err); } @@ -994,8 +1013,10 @@ static int rxe_attach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid) if (err) return err; + /* adds a ref on grp if successful */ err = rxe_mcast_add_grp_elem(rxe, qp, grp); + /* drops the ref from ..get_grp() */ rxe_drop_ref(grp); return err; } @@ -1005,6 +1026,7 @@ static int rxe_detach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid) struct rxe_dev *rxe = to_rdev(ibqp->device); struct rxe_qp *qp = to_rqp(ibqp); + /* drops a ref on grp if successful */ return rxe_mcast_drop_grp_elem(rxe, qp, mgid); }