From patchwork Wed Dec 16 23:15:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 11978735 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 X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DD86C0018C for ; Wed, 16 Dec 2020 23:16:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 58DDF23437 for ; Wed, 16 Dec 2020 23:16:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730767AbgLPXQi (ORCPT ); Wed, 16 Dec 2020 18:16:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726160AbgLPXQi (ORCPT ); Wed, 16 Dec 2020 18:16:38 -0500 Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DC78C06179C for ; Wed, 16 Dec 2020 15:15:58 -0800 (PST) Received: by mail-oi1-x22d.google.com with SMTP id l200so29819768oig.9 for ; Wed, 16 Dec 2020 15:15:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=y8uD86olxiCaZrASlwVO7mc1iqr605LiqaRsth4EE+A=; b=tBoAoJO56IsllZmiL5T3V6sOyAklINXeMJYxjje+YpHxYf1Mp+Yk3x8toAP3ZHBKk4 BqX4poR3Bo7fL6KiLaJI0Fmht0SijnIhRHVP4de3Tv0HHlefPJQNW+WrTd+4OUu6zdDr DjsvhgzNwbhCEVkixIu5dgX6WJn/Nx1IK+0kUk7KDsxY4AdwiYXsf1PtklZKDFpQVwpu 2pYWNPLY+wiz8+TzDjmb1w1Ovwbxjhsu7wOiF4Rco9xg2BmNdPcFD2Nl6Y3+artIINWM d+uJPRM3IOSTiREKZULjKiUaULwEG3LNVwzCS4ICPUYhQyI6RPWW9/QqEqt84rfv9NDu KopQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=y8uD86olxiCaZrASlwVO7mc1iqr605LiqaRsth4EE+A=; b=D73tPf+ezC8S261/2nYgwW8Me6ylE71fHHSC8JeVtv318ALHsLvgulJDAEE+yi4IwF zUhMg1mn5fbSBj5QxqE1ETHG56xhag4QJclsFybqIYaxSgGGnEtXpBVc0uUkx22RpK6h Il38ynMBrfAaV/QWPoUanwpZT4REe5gpwH8TW5f36hhqMC0KTZmEhgwI7aYDGego84mD OnQ3F8e8g+Pp829aeEyR9uzcbsAXCUT107qTGX3F4izUnB6mgI8rr0tBylZEIiwOC/05 2KXE5jQvHRgrHwHMbyjq0yPnG13JQODVEm3zPnl23qPNA2Qc7zZPeD7BS5F1pS4y8Cnw kpsw== X-Gm-Message-State: AOAM531nom8nQxuDF/VmvtToSmiAhQFM9GZUtM/PUFzGJrQYT3vThrax pZadEWLjTydUg0ieAry0VF4= X-Google-Smtp-Source: ABdhPJw7n5FqSlkCIJFfeJfhILH7hQsDPqVnXQsj3KwQRUjo7DXI9mOCDUAFaqdjY0qrh8p8dMmlLA== X-Received: by 2002:aca:c3c6:: with SMTP id t189mr3283542oif.21.1608160558006; Wed, 16 Dec 2020 15:15:58 -0800 (PST) Received: from rpearson-X570-AORUS-PRO-WIFI.tx.rr.com (2603-8081-140c-1a00-6a03-8d89-0aec-a801.res6.spectrum.com. [2603:8081:140c:1a00:6a03:8d89:aec:a801]) by smtp.gmail.com with ESMTPSA id f18sm793437otf.55.2020.12.16.15.15.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Dec 2020 15:15:57 -0800 (PST) From: Bob Pearson X-Google-Original-From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next 1/7] RDMA/rxe: Remove unneeded RXE_POOL_ATOMIC flag Date: Wed, 16 Dec 2020 17:15:44 -0600 Message-Id: <20201216231550.27224-2-rpearson@hpe.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201216231550.27224-1-rpearson@hpe.com> References: <20201216231550.27224-1-rpearson@hpe.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Remove RXE_POOL_ATOMIC flag from rxe_type_info for AH objects. These objects are now allocated by rdma/core so there is no further reason for this flag. Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe_pool.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c index b374eb53e2fe..11571ff64a8f 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.c +++ b/drivers/infiniband/sw/rxe/rxe_pool.c @@ -25,7 +25,7 @@ struct rxe_type_info rxe_type_info[RXE_NUM_TYPES] = { [RXE_TYPE_AH] = { .name = "rxe-ah", .size = sizeof(struct rxe_ah), - .flags = RXE_POOL_ATOMIC | RXE_POOL_NO_ALLOC, + .flags = RXE_POOL_NO_ALLOC, }, [RXE_TYPE_SRQ] = { .name = "rxe-srq", From patchwork Wed Dec 16 23:15:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 11978737 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 X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6CB43C1B0D8 for ; Wed, 16 Dec 2020 23:16:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3D97423718 for ; Wed, 16 Dec 2020 23:16:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730772AbgLPXQj (ORCPT ); Wed, 16 Dec 2020 18:16:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726160AbgLPXQj (ORCPT ); Wed, 16 Dec 2020 18:16:39 -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 4D2DFC0617A6 for ; Wed, 16 Dec 2020 15:15:59 -0800 (PST) Received: by mail-oi1-x234.google.com with SMTP id w124so26730384oia.6 for ; Wed, 16 Dec 2020 15:15:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UpdEfZgI1U6K2h3/ZYa6eV5wU64O80JlfodScrcgxNI=; b=j743CrFaclY8ZiJVV7eVkWrhox1dHgGUjCvYYl1xfLQWoKumwFvcHfs4Yt86ckgrRI P4Xf+y/kjR00BoYGInq0nFou+uLeU16JwiiuHy8QYS3yGOnh6jjnLQ/4lYuMGwDweds2 VifrS8fBivDwX/Zcn2cV6Y1ZNxxn2V0JLe9BYMRl7cpouyK0rsCKOn3+xRrJzaXoAYCa l9It/iarK486v/RO1d4FuW1cfHlftrRjjWldANoegi1bY7BZRIdjFyqGbD22qBA81gsX QlZzcN1U0RcfEKqQatGEyPcreYet4Y19/Bn7JAkEFEwiqCMr63C66dOExQ1nHbmCw6yG Ef7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UpdEfZgI1U6K2h3/ZYa6eV5wU64O80JlfodScrcgxNI=; b=VWQeT4ntxCYsyTlDQAWQ5lVLJSMT7P+Z1SA829uAmvG1XmxuGBYtsaSOUYuf1bHC6B 7JqzeN0Oz+RdX/C6mMDICf4/XkavK9Yp0UeYYkZB/nflC75qhsKaRQP2+Er1DD+pWeMK zSqT6ruH/zXY7Hg7baMWhDjGuo2f8PvSie7vkSq4Hx46MMXvS2WFnuL6yKS1OVnxsXtS e3mS1r8g0yNLe2i4aZeSMAVH4VULBLA0fdcIEO/VppW1c72bKPtZ7p/SwAMcxgK9kOXJ jn23XuQGU+Upn+oK+X+/3S0XK4f8elKlc4/VoZkTq1JFxONyPhWkwjY9A7my/L2Whb+u Narg== X-Gm-Message-State: AOAM531kKeJ2fz0qletLmDktr/CLVN1kZ0ggd+2whisFwVDtImCJuIi9 mrCc6dL9Fg1u+rE0PjMxID8= X-Google-Smtp-Source: ABdhPJzS/dpYtiFSBB7flCk0u5NUJIhtYBkkJSII+SgT8y7m0OOS7NzvDSwREhS6VQH0lTBkdV9tRQ== X-Received: by 2002:aca:4d08:: with SMTP id a8mr3318238oib.128.1608160558735; Wed, 16 Dec 2020 15:15:58 -0800 (PST) Received: from rpearson-X570-AORUS-PRO-WIFI.tx.rr.com (2603-8081-140c-1a00-6a03-8d89-0aec-a801.res6.spectrum.com. [2603:8081:140c:1a00:6a03:8d89:aec:a801]) by smtp.gmail.com with ESMTPSA id f18sm793437otf.55.2020.12.16.15.15.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Dec 2020 15:15:58 -0800 (PST) From: Bob Pearson X-Google-Original-From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next 2/7] RDMA/rxe: Let pools support both keys and indices Date: Wed, 16 Dec 2020 17:15:45 -0600 Message-Id: <20201216231550.27224-3-rpearson@hpe.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201216231550.27224-1-rpearson@hpe.com> References: <20201216231550.27224-1-rpearson@hpe.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Allow both indices and keys to exist for objects in pools. Previously you were limited to one or the other. This is required for later implementing rxe memory windows. Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe_pool.c | 73 ++++++++++++++-------------- drivers/infiniband/sw/rxe/rxe_pool.h | 32 +++++++----- 2 files changed, 58 insertions(+), 47 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c index 11571ff64a8f..f1ecf5983742 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.c +++ b/drivers/infiniband/sw/rxe/rxe_pool.c @@ -94,18 +94,18 @@ static int rxe_pool_init_index(struct rxe_pool *pool, u32 max, u32 min) goto out; } - pool->max_index = max; - pool->min_index = min; + pool->index.max_index = max; + pool->index.min_index = min; size = BITS_TO_LONGS(max - min + 1) * sizeof(long); - pool->table = kmalloc(size, GFP_KERNEL); - if (!pool->table) { + pool->index.table = kmalloc(size, GFP_KERNEL); + if (!pool->index.table) { err = -ENOMEM; goto out; } - pool->table_size = size; - bitmap_zero(pool->table, max - min + 1); + pool->index.table_size = size; + bitmap_zero(pool->index.table, max - min + 1); out: return err; @@ -127,7 +127,8 @@ int rxe_pool_init( pool->max_elem = max_elem; pool->elem_size = ALIGN(size, RXE_POOL_ALIGN); pool->flags = rxe_type_info[type].flags; - pool->tree = RB_ROOT; + pool->index.tree = RB_ROOT; + pool->key.tree = RB_ROOT; pool->cleanup = rxe_type_info[type].cleanup; atomic_set(&pool->num_elem, 0); @@ -145,8 +146,8 @@ int rxe_pool_init( } if (rxe_type_info[type].flags & RXE_POOL_KEY) { - pool->key_offset = rxe_type_info[type].key_offset; - pool->key_size = rxe_type_info[type].key_size; + pool->key.key_offset = rxe_type_info[type].key_offset; + pool->key.key_size = rxe_type_info[type].key_size; } pool->state = RXE_POOL_STATE_VALID; @@ -160,7 +161,7 @@ static void rxe_pool_release(struct kref *kref) struct rxe_pool *pool = container_of(kref, struct rxe_pool, ref_cnt); pool->state = RXE_POOL_STATE_INVALID; - kfree(pool->table); + kfree(pool->index.table); } static void rxe_pool_put(struct rxe_pool *pool) @@ -185,27 +186,27 @@ void rxe_pool_cleanup(struct rxe_pool *pool) static u32 alloc_index(struct rxe_pool *pool) { u32 index; - u32 range = pool->max_index - pool->min_index + 1; + u32 range = pool->index.max_index - pool->index.min_index + 1; - index = find_next_zero_bit(pool->table, range, pool->last); + index = find_next_zero_bit(pool->index.table, range, pool->index.last); if (index >= range) - index = find_first_zero_bit(pool->table, range); + index = find_first_zero_bit(pool->index.table, range); WARN_ON_ONCE(index >= range); - set_bit(index, pool->table); - pool->last = index; - return index + pool->min_index; + set_bit(index, pool->index.table); + pool->index.last = index; + return index + pool->index.min_index; } static void insert_index(struct rxe_pool *pool, struct rxe_pool_entry *new) { - struct rb_node **link = &pool->tree.rb_node; + struct rb_node **link = &pool->index.tree.rb_node; struct rb_node *parent = NULL; struct rxe_pool_entry *elem; while (*link) { parent = *link; - elem = rb_entry(parent, struct rxe_pool_entry, node); + elem = rb_entry(parent, struct rxe_pool_entry, index_node); if (elem->index == new->index) { pr_warn("element already exists!\n"); @@ -218,25 +219,25 @@ static void insert_index(struct rxe_pool *pool, struct rxe_pool_entry *new) link = &(*link)->rb_right; } - rb_link_node(&new->node, parent, link); - rb_insert_color(&new->node, &pool->tree); + rb_link_node(&new->index_node, parent, link); + rb_insert_color(&new->index_node, &pool->index.tree); out: return; } static void insert_key(struct rxe_pool *pool, struct rxe_pool_entry *new) { - struct rb_node **link = &pool->tree.rb_node; + struct rb_node **link = &pool->key.tree.rb_node; struct rb_node *parent = NULL; struct rxe_pool_entry *elem; int cmp; while (*link) { parent = *link; - elem = rb_entry(parent, struct rxe_pool_entry, node); + elem = rb_entry(parent, struct rxe_pool_entry, key_node); - cmp = memcmp((u8 *)elem + pool->key_offset, - (u8 *)new + pool->key_offset, pool->key_size); + 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"); @@ -249,8 +250,8 @@ static void insert_key(struct rxe_pool *pool, struct rxe_pool_entry *new) link = &(*link)->rb_right; } - rb_link_node(&new->node, parent, link); - rb_insert_color(&new->node, &pool->tree); + rb_link_node(&new->key_node, parent, link); + rb_insert_color(&new->key_node, &pool->key.tree); out: return; } @@ -262,7 +263,7 @@ void rxe_add_key(void *arg, void *key) unsigned long flags; write_lock_irqsave(&pool->pool_lock, flags); - memcpy((u8 *)elem + pool->key_offset, key, pool->key_size); + memcpy((u8 *)elem + pool->key.key_offset, key, pool->key.key_size); insert_key(pool, elem); write_unlock_irqrestore(&pool->pool_lock, flags); } @@ -274,7 +275,7 @@ void rxe_drop_key(void *arg) unsigned long flags; write_lock_irqsave(&pool->pool_lock, flags); - rb_erase(&elem->node, &pool->tree); + rb_erase(&elem->key_node, &pool->key.tree); write_unlock_irqrestore(&pool->pool_lock, flags); } @@ -297,8 +298,8 @@ void rxe_drop_index(void *arg) unsigned long flags; write_lock_irqsave(&pool->pool_lock, flags); - clear_bit(elem->index - pool->min_index, pool->table); - rb_erase(&elem->node, &pool->tree); + clear_bit(elem->index - pool->index.min_index, pool->index.table); + rb_erase(&elem->index_node, &pool->index.tree); write_unlock_irqrestore(&pool->pool_lock, flags); } @@ -402,10 +403,10 @@ void *rxe_pool_get_index(struct rxe_pool *pool, u32 index) if (pool->state != RXE_POOL_STATE_VALID) goto out; - node = pool->tree.rb_node; + node = pool->index.tree.rb_node; while (node) { - elem = rb_entry(node, struct rxe_pool_entry, node); + elem = rb_entry(node, struct rxe_pool_entry, index_node); if (elem->index > index) node = node->rb_left; @@ -434,13 +435,13 @@ void *rxe_pool_get_key(struct rxe_pool *pool, void *key) if (pool->state != RXE_POOL_STATE_VALID) goto out; - node = pool->tree.rb_node; + node = pool->key.tree.rb_node; while (node) { - elem = rb_entry(node, struct rxe_pool_entry, node); + elem = rb_entry(node, struct rxe_pool_entry, key_node); - cmp = memcmp((u8 *)elem + pool->key_offset, - key, pool->key_size); + cmp = memcmp((u8 *)elem + pool->key.key_offset, + key, pool->key.key_size); if (cmp > 0) node = node->rb_left; diff --git a/drivers/infiniband/sw/rxe/rxe_pool.h b/drivers/infiniband/sw/rxe/rxe_pool.h index 432745ffc8d4..3d722aae5f15 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.h +++ b/drivers/infiniband/sw/rxe/rxe_pool.h @@ -56,8 +56,11 @@ struct rxe_pool_entry { struct kref ref_cnt; struct list_head list; - /* only used if indexed or keyed */ - struct rb_node node; + /* only used if keyed */ + struct rb_node key_node; + + /* only used if indexed */ + struct rb_node index_node; u32 index; }; @@ -74,15 +77,22 @@ struct rxe_pool { unsigned int max_elem; atomic_t num_elem; - /* only used if indexed or keyed */ - struct rb_root tree; - unsigned long *table; - size_t table_size; - u32 max_index; - u32 min_index; - u32 last; - size_t key_offset; - size_t key_size; + /* only used if indexed */ + struct { + struct rb_root tree; + unsigned long *table; + size_t table_size; + u32 last; + u32 max_index; + u32 min_index; + } index; + + /* only used if keyed */ + struct { + struct rb_root tree; + size_t key_offset; + size_t key_size; + } key; }; /* initialize a pool of objects with given limit on From patchwork Wed Dec 16 23:15:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 11978739 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 X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D35E7C4361B for ; Wed, 16 Dec 2020 23:16:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A806223718 for ; Wed, 16 Dec 2020 23:16:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730773AbgLPXQk (ORCPT ); Wed, 16 Dec 2020 18:16:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726160AbgLPXQk (ORCPT ); Wed, 16 Dec 2020 18:16:40 -0500 Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D024CC0617A7 for ; Wed, 16 Dec 2020 15:15:59 -0800 (PST) Received: by mail-oi1-x232.google.com with SMTP id s75so29870760oih.1 for ; Wed, 16 Dec 2020 15:15:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Aq4UndTq5JISLlGGeXFVBwfYx3fYkDJyOVTup1G9Obo=; b=DLtFcscaVEONPIDdQStpfBFUFzW0Uh0p79glHdTDoffzlhH6XzK1a1/tTFSntH+d38 wBj1W87gpQcbaqI/ol3dF7gSqI1xFiBQKGCVzxeccCSAEHbvOlfBteFNadVGelG7OLYi nDgHiwzX7hX4Avy97TbkmuOE7IqiarPseAEc6x98lipEtsUjC/qKoAPgsU0eJ98z51xc DfUvKpLsXDKidduAL/xpuLFp4ET6I95hxtU1HvASajEDC7P0WAqI/sQL3wtb+De4AJ3v NncY2Vyxnffc+ufmJ70K0/fMz0mj4ZkZaaaPjXAGNbFUjhR8SQD4fJhF5zqPPkBk0HML gvnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Aq4UndTq5JISLlGGeXFVBwfYx3fYkDJyOVTup1G9Obo=; b=Wu0MoDCbd0mD9+fbMQNcIhRocH3pRpDjxEzhJlR4AAgHF+Acsko/WIvyORJmQ2JY22 6C7lBD/ObPSMy6wr8bhsQiIgDueW/kU/uA/y/hyo0JXrq1PBVsquP6HNEjJUJioXA/QM 5hpCm7d59nRO41hWvcBfUFSDA/3TdaRg1tkkdG0ImlyT9CfLE88pGt3oFQWVvp1+W8ar LAuWBnZBGwIrUuTaFE0pXWbCnddMfCuHqUkwb63PS14oSmhTa1SG2BpeZiPkQq8WTWdH B9cnE2adHEAaLj1qfoiDRbPvmL/+CWKodZP9QD/wy9jKCo0phJnkQp/ob6dersxgcES/ Sdpw== X-Gm-Message-State: AOAM532SvOA5otZDbP90CSaQF9FHH//RMjAcT8gKDb5s2EOQDbCm76bK OvHNO05ZDcfRQUMKqiwXOT8= X-Google-Smtp-Source: ABdhPJzod8sCZjXYvhqu8mcVMTbP9qHyP7I3ZHnYHcpTuQmOf0Hb0qewUvDB8KqhSJnU2Bn96beFxA== X-Received: by 2002:aca:568f:: with SMTP id k137mr3287246oib.138.1608160559321; Wed, 16 Dec 2020 15:15:59 -0800 (PST) Received: from rpearson-X570-AORUS-PRO-WIFI.tx.rr.com (2603-8081-140c-1a00-6a03-8d89-0aec-a801.res6.spectrum.com. [2603:8081:140c:1a00:6a03:8d89:aec:a801]) by smtp.gmail.com with ESMTPSA id f18sm793437otf.55.2020.12.16.15.15.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Dec 2020 15:15:59 -0800 (PST) From: Bob Pearson X-Google-Original-From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next 3/7] RDMA/rxe: Add elem_offset field to rxe_type_info Date: Wed, 16 Dec 2020 17:15:46 -0600 Message-Id: <20201216231550.27224-4-rpearson@hpe.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201216231550.27224-1-rpearson@hpe.com> References: <20201216231550.27224-1-rpearson@hpe.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org The rxe verbs objects each include an rdma-core object 'ib_xxx' and a rxe_pool_entry 'pelem' in addition to rxe specific data. Originally these all had pelem first and ib_xxx second. Currently about half have ib_xxx first and half have pelem first. Saving the offset of the pelem field in rxe_type info will enable making the rxe_pool APIs type safe as the pelem field continues to vary. Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe_pool.c | 10 ++++++++++ drivers/infiniband/sw/rxe/rxe_pool.h | 1 + 2 files changed, 11 insertions(+) diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c index f1ecf5983742..4d667b78af9b 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.c +++ b/drivers/infiniband/sw/rxe/rxe_pool.c @@ -15,21 +15,25 @@ struct rxe_type_info rxe_type_info[RXE_NUM_TYPES] = { [RXE_TYPE_UC] = { .name = "rxe-uc", .size = sizeof(struct rxe_ucontext), + .elem_offset = offsetof(struct rxe_ucontext, pelem), .flags = RXE_POOL_NO_ALLOC, }, [RXE_TYPE_PD] = { .name = "rxe-pd", .size = sizeof(struct rxe_pd), + .elem_offset = offsetof(struct rxe_pd, pelem), .flags = RXE_POOL_NO_ALLOC, }, [RXE_TYPE_AH] = { .name = "rxe-ah", .size = sizeof(struct rxe_ah), + .elem_offset = offsetof(struct rxe_ah, pelem), .flags = RXE_POOL_NO_ALLOC, }, [RXE_TYPE_SRQ] = { .name = "rxe-srq", .size = sizeof(struct rxe_srq), + .elem_offset = offsetof(struct rxe_srq, pelem), .flags = RXE_POOL_INDEX | RXE_POOL_NO_ALLOC, .min_index = RXE_MIN_SRQ_INDEX, .max_index = RXE_MAX_SRQ_INDEX, @@ -37,6 +41,7 @@ struct rxe_type_info rxe_type_info[RXE_NUM_TYPES] = { [RXE_TYPE_QP] = { .name = "rxe-qp", .size = sizeof(struct rxe_qp), + .elem_offset = offsetof(struct rxe_qp, pelem), .cleanup = rxe_qp_cleanup, .flags = RXE_POOL_INDEX, .min_index = RXE_MIN_QP_INDEX, @@ -45,12 +50,14 @@ struct rxe_type_info rxe_type_info[RXE_NUM_TYPES] = { [RXE_TYPE_CQ] = { .name = "rxe-cq", .size = sizeof(struct rxe_cq), + .elem_offset = offsetof(struct rxe_cq, pelem), .flags = RXE_POOL_NO_ALLOC, .cleanup = rxe_cq_cleanup, }, [RXE_TYPE_MR] = { .name = "rxe-mr", .size = sizeof(struct rxe_mem), + .elem_offset = offsetof(struct rxe_mem, pelem), .cleanup = rxe_mem_cleanup, .flags = RXE_POOL_INDEX, .max_index = RXE_MAX_MR_INDEX, @@ -59,6 +66,7 @@ struct rxe_type_info rxe_type_info[RXE_NUM_TYPES] = { [RXE_TYPE_MW] = { .name = "rxe-mw", .size = sizeof(struct rxe_mem), + .elem_offset = offsetof(struct rxe_mem, pelem), .flags = RXE_POOL_INDEX, .max_index = RXE_MAX_MW_INDEX, .min_index = RXE_MIN_MW_INDEX, @@ -66,6 +74,7 @@ struct rxe_type_info rxe_type_info[RXE_NUM_TYPES] = { [RXE_TYPE_MC_GRP] = { .name = "rxe-mc_grp", .size = sizeof(struct rxe_mc_grp), + .elem_offset = offsetof(struct rxe_mc_grp, pelem), .cleanup = rxe_mc_cleanup, .flags = RXE_POOL_KEY, .key_offset = offsetof(struct rxe_mc_grp, mgid), @@ -74,6 +83,7 @@ struct rxe_type_info rxe_type_info[RXE_NUM_TYPES] = { [RXE_TYPE_MC_ELEM] = { .name = "rxe-mc_elem", .size = sizeof(struct rxe_mc_elem), + .elem_offset = offsetof(struct rxe_mc_elem, pelem), .flags = RXE_POOL_ATOMIC, }, }; diff --git a/drivers/infiniband/sw/rxe/rxe_pool.h b/drivers/infiniband/sw/rxe/rxe_pool.h index 3d722aae5f15..b37df6198e8a 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.h +++ b/drivers/infiniband/sw/rxe/rxe_pool.h @@ -36,6 +36,7 @@ struct rxe_pool_entry; struct rxe_type_info { const char *name; size_t size; + size_t elem_offset; void (*cleanup)(struct rxe_pool_entry *obj); enum rxe_pool_flags flags; u32 max_index; From patchwork Wed Dec 16 23:15:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 11978741 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 X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8530CC2BB9A for ; Wed, 16 Dec 2020 23:16:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 553C723718 for ; Wed, 16 Dec 2020 23:16:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730780AbgLPXQk (ORCPT ); Wed, 16 Dec 2020 18:16:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726160AbgLPXQk (ORCPT ); Wed, 16 Dec 2020 18:16:40 -0500 Received: from mail-ot1-x332.google.com (mail-ot1-x332.google.com [IPv6:2607:f8b0:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D60FC0617B0 for ; Wed, 16 Dec 2020 15:16:00 -0800 (PST) Received: by mail-ot1-x332.google.com with SMTP id b24so7656145otj.0 for ; Wed, 16 Dec 2020 15:16:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PsXzxcoOGom3blPQmGb/0N1UjQgyx0UMORYQXYP7Tyk=; b=F+LwnogOAvXos1JF1d8/HyoXJ8YZ/GA3X5fbVEQgSW5M8WRGX4mIfIyTEOqXoF9uQ/ GjO6RdetMIicQbFFN4MBcOZtWG+F/DZPjxSkMsK163DFkXWVYIP1Gxzw5q4axbK63boW 88wptPFo8w6ZFrbF2jRe8X5kXo8lHs6AeZb+nBd4nFi3SPUgBoljjJa4E4k/pg0066px JiIq5U/S53i044B9WxyG8dd/Qf8KnxDKINyYgH+D2C7v/E/P+sY1wL/SFkIBx1Kv075b HFG7AE+3/DawqXxeeymKsTgND9SMaU+fFXV0xcxWWFCvyEHx+FbOKx40Yz8L3+1YnxvV ofIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PsXzxcoOGom3blPQmGb/0N1UjQgyx0UMORYQXYP7Tyk=; b=RwGJ2/3uRtwh/pSYo6xGWI2kbEvq/WyF1QU5/UpzZQ1dBrrG0b9a7CJxmPoWBu8nHL RbVDmi/oIRdBC+03AMbb5u4R8AyEfQ5pHw7uPz/LR2cRzC/wFPxp/6pPfwFvuaR2w/PN nck64eu80TSLOET3tcnLmnNN0PfKPYWfleQqwWC03sq7RMkNUVVP0e+nQVhluP3ybfkX u4de026gGEA1nAhT5oqRFO6ApjaUXNLI0zwogvX8n9YySFyB++jyKHMKYZ5MZPhJ5EnC o3hRRWj2mtbO0KM2xdM2Kx15rDmODjONTsmR1cKl/L3UcqiZ125oTzc3e03J6XzyFj3c Qx5Q== X-Gm-Message-State: AOAM531EvNvCRfBdMi7CF4yisiv5sHAbW0psRF0wOdea2mRBin4eOCbv /LWYRec7XdQsgtBsUKXGwAM= X-Google-Smtp-Source: ABdhPJykQuqzwSBWdV3d4tS79KSMFDlKGT3GGbWzaZH9KZeWEKjPdqN+Dse1D13wAWOuUQrZiFqOIA== X-Received: by 2002:a9d:6f8f:: with SMTP id h15mr28037695otq.125.1608160559923; Wed, 16 Dec 2020 15:15:59 -0800 (PST) Received: from rpearson-X570-AORUS-PRO-WIFI.tx.rr.com (2603-8081-140c-1a00-6a03-8d89-0aec-a801.res6.spectrum.com. [2603:8081:140c:1a00:6a03:8d89:aec:a801]) by smtp.gmail.com with ESMTPSA id f18sm793437otf.55.2020.12.16.15.15.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Dec 2020 15:15:59 -0800 (PST) From: Bob Pearson X-Google-Original-From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next 4/7] RDMA/rxe: Make pool lookup and alloc APIs type safe Date: Wed, 16 Dec 2020 17:15:47 -0600 Message-Id: <20201216231550.27224-5-rpearson@hpe.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201216231550.27224-1-rpearson@hpe.com> References: <20201216231550.27224-1-rpearson@hpe.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org The allocate, lookup index, lookup key and cleanup routines in rxe_pool.c currently are not type safe against relocating the pelem field in the objects. Planned changes to move allocation of objects into rdma-core make addressing this a requirement. Use the elem_offset field in rxe_type_info make these APIs safe against moving the pelem field. Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe_pool.c | 55 +++++++++++++++++++--------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c index 4d667b78af9b..2873ecfb84c2 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.c +++ b/drivers/infiniband/sw/rxe/rxe_pool.c @@ -315,7 +315,9 @@ void rxe_drop_index(void *arg) void *rxe_alloc(struct rxe_pool *pool) { + struct rxe_type_info *info = &rxe_type_info[pool->type]; struct rxe_pool_entry *elem; + u8 *obj; unsigned long flags; might_sleep_if(!(pool->flags & RXE_POOL_ATOMIC)); @@ -334,16 +336,17 @@ void *rxe_alloc(struct rxe_pool *pool) if (atomic_inc_return(&pool->num_elem) > pool->max_elem) goto out_cnt; - elem = kzalloc(rxe_type_info[pool->type].size, - (pool->flags & RXE_POOL_ATOMIC) ? - GFP_ATOMIC : GFP_KERNEL); - if (!elem) + obj = kzalloc(info->size, (pool->flags & RXE_POOL_ATOMIC) ? + GFP_ATOMIC : GFP_KERNEL); + if (!obj) goto out_cnt; + elem = (struct rxe_pool_entry *)(obj + info->elem_offset); + elem->pool = pool; kref_init(&elem->ref_cnt); - return elem; + return obj; out_cnt: atomic_dec(&pool->num_elem); @@ -391,12 +394,17 @@ void rxe_elem_release(struct kref *kref) struct rxe_pool_entry *elem = container_of(kref, struct rxe_pool_entry, ref_cnt); struct rxe_pool *pool = elem->pool; + struct rxe_type_info *info = &rxe_type_info[pool->type]; + u8 *obj; if (pool->cleanup) pool->cleanup(elem); - if (!(pool->flags & RXE_POOL_NO_ALLOC)) - kfree(elem); + if (!(pool->flags & RXE_POOL_NO_ALLOC)) { + obj = (u8 *)elem - info->elem_offset; + kfree(obj); + } + atomic_dec(&pool->num_elem); ib_device_put(&pool->rxe->ib_dev); rxe_pool_put(pool); @@ -404,8 +412,10 @@ void rxe_elem_release(struct kref *kref) void *rxe_pool_get_index(struct rxe_pool *pool, u32 index) { - struct rb_node *node = NULL; - struct rxe_pool_entry *elem = NULL; + struct rxe_type_info *info = &rxe_type_info[pool->type]; + struct rb_node *node; + struct rxe_pool_entry *elem; + u8 *obj = NULL; unsigned long flags; read_lock_irqsave(&pool->pool_lock, flags); @@ -422,21 +432,28 @@ void *rxe_pool_get_index(struct rxe_pool *pool, u32 index) node = node->rb_left; else if (elem->index < index) node = node->rb_right; - else { - kref_get(&elem->ref_cnt); + else break; - } + } + + if (node) { + kref_get(&elem->ref_cnt); + obj = (u8 *)elem - info->elem_offset; + } else { + obj = NULL; } out: read_unlock_irqrestore(&pool->pool_lock, flags); - return node ? elem : NULL; + return obj; } void *rxe_pool_get_key(struct rxe_pool *pool, void *key) { - struct rb_node *node = NULL; - struct rxe_pool_entry *elem = NULL; + struct rxe_type_info *info = &rxe_type_info[pool->type]; + struct rb_node *node; + struct rxe_pool_entry *elem; + u8 *obj = NULL; int cmp; unsigned long flags; @@ -461,10 +478,14 @@ void *rxe_pool_get_key(struct rxe_pool *pool, void *key) break; } - if (node) + if (node) { kref_get(&elem->ref_cnt); + obj = (u8 *)elem - info->elem_offset; + } else { + obj = NULL; + } out: read_unlock_irqrestore(&pool->pool_lock, flags); - return node ? elem : NULL; + return obj; } From patchwork Wed Dec 16 23:15:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 11978743 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 X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08ADFC4361B for ; Wed, 16 Dec 2020 23:17:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C45E123437 for ; Wed, 16 Dec 2020 23:17:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726575AbgLPXRT (ORCPT ); Wed, 16 Dec 2020 18:17:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730697AbgLPXRS (ORCPT ); Wed, 16 Dec 2020 18:17:18 -0500 Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31F55C06138C for ; Wed, 16 Dec 2020 15:16:01 -0800 (PST) Received: by mail-oi1-x22d.google.com with SMTP id 15so29860135oix.8 for ; Wed, 16 Dec 2020 15:16:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/uZPXIVA1jpJj0yYxg2Xu3Y+knpQFR2IKCnC6gEECtc=; b=YNoKKOkVxGR9D5E2T0LvAkPADP4K2O1bkBsfe32fXi6B0kanFglXoLyhtcBxmSzRNg Di4pEgF32wurWg6aCU07XNUoIrQgITBm5B94zMYATcGRhGioAGtpDU6hAJGBHGinT4NP 3FWVH20n/BRClgf6x9IenBWY+fLr6/EsTBGfpLPNmP6K6JB5HZiBKkeFxBz8o7oEdjdk IpAa9IcsnhBQZ/2AtuE6Ei0OYWSvbNsWkwdbZI9TBzgL9iXG73GgzqkawrMr1IroxtBh RXMQsyt98KvV8pDTS8xbHQZOsvab9JuCfQ4jHpurF893RqrYYeuUXUv6wybi5KHJv0Vm z6zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/uZPXIVA1jpJj0yYxg2Xu3Y+knpQFR2IKCnC6gEECtc=; b=iqX+9RvWveNBpKaC8vqnkjGpwmkEIkEc4Lukhv5W+D3EVbOq8EZm6Y0lmxZVqivgsC L0jtB915tfQLaoNqdsovZ7mJ9XLZJs5thknoQS7SV+Df7VBlXWYc1LxFUWppvnKkUJsk ocsuwBjj8+R6JWUflbZJ/2d690Le31wea8sLd++YdUS5vI0/z94TV4RGr9kxNLYYbcrH FRhdPit1qqvlZiO1i8xW8ddvb/KIUZHUBDNtNlsqBE+0V+8+y/9ibAn5cVOeptbW/tAf qQVIxqEFJkAzLZ2MBPtVs5uEF5l3Yaae4Up9lkXqCJ3yuh/vGRHvOcQULV2/gEdhyVNC lLPQ== X-Gm-Message-State: AOAM532Lb9ZPS+dBRgK3qtF1KONwR2rEwPS6J2IamhLuwLORC501DpZe neDRJTyWy7VCIiP7XCI2pe4= X-Google-Smtp-Source: ABdhPJzcddSr+fNRlR8ujIfcImUqDJHsUnvn/WRNuex2MTVn71qs55XpfDUBGiRAxQ0YLtvA7/HLMw== X-Received: by 2002:aca:4909:: with SMTP id w9mr3243693oia.166.1608160560678; Wed, 16 Dec 2020 15:16:00 -0800 (PST) Received: from rpearson-X570-AORUS-PRO-WIFI.tx.rr.com (2603-8081-140c-1a00-6a03-8d89-0aec-a801.res6.spectrum.com. [2603:8081:140c:1a00:6a03:8d89:aec:a801]) by smtp.gmail.com with ESMTPSA id f18sm793437otf.55.2020.12.16.15.15.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Dec 2020 15:16:00 -0800 (PST) From: Bob Pearson X-Google-Original-From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next 5/7] RDMA/rxe: Make add/drop key/index APIs type safe Date: Wed, 16 Dec 2020 17:15:48 -0600 Message-Id: <20201216231550.27224-6-rpearson@hpe.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201216231550.27224-1-rpearson@hpe.com> References: <20201216231550.27224-1-rpearson@hpe.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Replace 'void *' parameters with 'struct rxe_pool_entry *' and use a macro to allow rxe_add_index, rxe_drop_index, rxe_add_key, rxe_drop_key and rxe_add_to_pool APIs to be type safe against changing the position of pelem in the objects. Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe_pool.c | 14 +++++--------- drivers/infiniband/sw/rxe/rxe_pool.h | 20 +++++++++++++++----- drivers/infiniband/sw/rxe/rxe_verbs.c | 16 ++++++++-------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c index 2873ecfb84c2..c8a6d0d7f01a 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.c +++ b/drivers/infiniband/sw/rxe/rxe_pool.c @@ -266,9 +266,8 @@ static void insert_key(struct rxe_pool *pool, struct rxe_pool_entry *new) return; } -void rxe_add_key(void *arg, void *key) +void __rxe_add_key(struct rxe_pool_entry *elem, void *key) { - struct rxe_pool_entry *elem = arg; struct rxe_pool *pool = elem->pool; unsigned long flags; @@ -278,9 +277,8 @@ void rxe_add_key(void *arg, void *key) write_unlock_irqrestore(&pool->pool_lock, flags); } -void rxe_drop_key(void *arg) +void __rxe_drop_key(struct rxe_pool_entry *elem) { - struct rxe_pool_entry *elem = arg; struct rxe_pool *pool = elem->pool; unsigned long flags; @@ -289,9 +287,8 @@ void rxe_drop_key(void *arg) write_unlock_irqrestore(&pool->pool_lock, flags); } -void rxe_add_index(void *arg) +void __rxe_add_index(struct rxe_pool_entry *elem) { - struct rxe_pool_entry *elem = arg; struct rxe_pool *pool = elem->pool; unsigned long flags; @@ -301,9 +298,8 @@ void rxe_add_index(void *arg) write_unlock_irqrestore(&pool->pool_lock, flags); } -void rxe_drop_index(void *arg) +void __rxe_drop_index(struct rxe_pool_entry *elem) { - struct rxe_pool_entry *elem = arg; struct rxe_pool *pool = elem->pool; unsigned long flags; @@ -356,7 +352,7 @@ void *rxe_alloc(struct rxe_pool *pool) return NULL; } -int rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem) +int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem) { unsigned long flags; diff --git a/drivers/infiniband/sw/rxe/rxe_pool.h b/drivers/infiniband/sw/rxe/rxe_pool.h index b37df6198e8a..5db0bdde185e 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.h +++ b/drivers/infiniband/sw/rxe/rxe_pool.h @@ -110,23 +110,33 @@ void rxe_pool_cleanup(struct rxe_pool *pool); void *rxe_alloc(struct rxe_pool *pool); /* connect already allocated object to pool */ -int rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem); +int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem); + +#define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->pelem) /* assign an index to an indexed object and insert object into * pool's rb tree */ -void rxe_add_index(void *elem); +void __rxe_add_index(struct rxe_pool_entry *elem); + +#define rxe_add_index(obj) __rxe_add_index(&(obj)->pelem) /* drop an index and remove object from rb tree */ -void rxe_drop_index(void *elem); +void __rxe_drop_index(struct rxe_pool_entry *elem); + +#define rxe_drop_index(obj) __rxe_drop_index(&(obj)->pelem) /* assign a key to a keyed object and insert object into * pool's rb tree */ -void rxe_add_key(void *elem, void *key); +void __rxe_add_key(struct rxe_pool_entry *elem, void *key); + +#define rxe_add_key(obj, key) __rxe_add_key(&(obj)->pelem, key) /* remove elem from rb tree */ -void rxe_drop_key(void *elem); +void __rxe_drop_key(struct rxe_pool_entry *elem); + +#define rxe_drop_key(obj) __rxe_drop_key(&(obj)->pelem) /* lookup an indexed object from index. takes a reference on object */ void *rxe_pool_get_index(struct rxe_pool *pool, u32 index); diff --git a/drivers/infiniband/sw/rxe/rxe_verbs.c b/drivers/infiniband/sw/rxe/rxe_verbs.c index a031514e2f41..7483a33bcec5 100644 --- a/drivers/infiniband/sw/rxe/rxe_verbs.c +++ b/drivers/infiniband/sw/rxe/rxe_verbs.c @@ -106,12 +106,12 @@ static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev, return IB_LINK_LAYER_ETHERNET; } -static int rxe_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata) +static int rxe_alloc_ucontext(struct ib_ucontext *ibuc, struct ib_udata *udata) { - struct rxe_dev *rxe = to_rdev(uctx->device); - struct rxe_ucontext *uc = to_ruc(uctx); + struct rxe_dev *rxe = to_rdev(ibuc->device); + struct rxe_ucontext *uc = to_ruc(ibuc); - return rxe_add_to_pool(&rxe->uc_pool, &uc->pelem); + return rxe_add_to_pool(&rxe->uc_pool, uc); } static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc) @@ -145,7 +145,7 @@ static int rxe_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) struct rxe_dev *rxe = to_rdev(ibpd->device); struct rxe_pd *pd = to_rpd(ibpd); - return rxe_add_to_pool(&rxe->pd_pool, &pd->pelem); + return rxe_add_to_pool(&rxe->pd_pool, pd); } static int rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) @@ -169,7 +169,7 @@ static int rxe_create_ah(struct ib_ah *ibah, if (err) return err; - err = rxe_add_to_pool(&rxe->ah_pool, &ah->pelem); + err = rxe_add_to_pool(&rxe->ah_pool, ah); if (err) return err; @@ -273,7 +273,7 @@ static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init, if (err) goto err1; - err = rxe_add_to_pool(&rxe->srq_pool, &srq->pelem); + err = rxe_add_to_pool(&rxe->srq_pool, srq); if (err) goto err1; @@ -774,7 +774,7 @@ static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, if (err) return err; - return rxe_add_to_pool(&rxe->cq_pool, &cq->pelem); + return rxe_add_to_pool(&rxe->cq_pool, cq); } static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata) From patchwork Wed Dec 16 23:15:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 11978747 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 X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A865C1B0D8 for ; Wed, 16 Dec 2020 23:17:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4787423718 for ; Wed, 16 Dec 2020 23:17:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730782AbgLPXRT (ORCPT ); Wed, 16 Dec 2020 18:17:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730728AbgLPXRS (ORCPT ); Wed, 16 Dec 2020 18:17:18 -0500 Received: from mail-oo1-xc32.google.com (mail-oo1-xc32.google.com [IPv6:2607:f8b0:4864:20::c32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E01B0C061282 for ; Wed, 16 Dec 2020 15:16:01 -0800 (PST) Received: by mail-oo1-xc32.google.com with SMTP id j21so3679133oou.11 for ; Wed, 16 Dec 2020 15:16:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6rF+OAVZuTeZFAnM/LfFZzqXN0u42TbVx7IRY4i4rrA=; b=NTrks4FTwqMdmOV8zD/5qc3VFPb0ibCsoe5Sxf/LMf6BpXShoeOQGXZ97WnEezkb6C WDF01QWIsmmLlcjsNlmz8L8FDaVOMFP6sm8rlSGqiZwlINFv0nBbDWH50ik03kBCByzn xItKF5MNkNKkHIQGkJAH6qgeSl11tfiMJ8yPu1L/KY4yv2w+cP3PgVWwcn7bveCZi1v2 SnzOkxXbbNRX87ZyVB5TmHeOz8ukQlV5XQR5/wnZIJI259Ix80y7/WRqReIVkzY8huR/ BaqG298FfVBgrc8yjwHrhxy/t6TkDo8BWXLKiH93MN8x40s/KwK5q3pH9K+d4kaUffnW Wo/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6rF+OAVZuTeZFAnM/LfFZzqXN0u42TbVx7IRY4i4rrA=; b=EP5jpyPXCjogACMQKmKCTysJ8OF3m3hEk8K0LUoCGgD4C136PCZ3GQHxbkSCjAGRoG JYK/ILTqjrdSYzQaiRpRoicmqP0K56QFYtO+H1xJDubrdu43Y+lNPK1GvIEwbdy+M15Y yTQs108Z+KR5YYPs7eCXU/9GLAFdmwkAWW2lYiZuHzqOTL9W/TN4iVFgJFo8bpD+989G ivfB/wDbZdQ80TP1hsQXwpq2zDWDeziYBLpH+/rIXLvtOty9iikHjE5hP0T6ZMrNK3ZG sN0JlnketeTBmik2jutP3JVwDA3OyY2SJsC27Eyf/EJ6ClENdzx8lr34m1HECdc973LE DNZw== X-Gm-Message-State: AOAM533gpQnuEmZiBsTq0DqXugk6LccIIIZ++iMSr7s1bLADXfnQ1aMf PZgLXrZ4/KyNxD5E7JI1psA= X-Google-Smtp-Source: ABdhPJyRUuYwRT4zr+x2ieN4Xph8Lzz5G7UUAyKzydVXsiAUYFbPTS29IcS/FeS96lQ5vZMYmpIkjA== X-Received: by 2002:a4a:d41a:: with SMTP id n26mr26998127oos.65.1608160561343; Wed, 16 Dec 2020 15:16:01 -0800 (PST) Received: from rpearson-X570-AORUS-PRO-WIFI.tx.rr.com (2603-8081-140c-1a00-6a03-8d89-0aec-a801.res6.spectrum.com. [2603:8081:140c:1a00:6a03:8d89:aec:a801]) by smtp.gmail.com with ESMTPSA id f18sm793437otf.55.2020.12.16.15.16.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Dec 2020 15:16:00 -0800 (PST) From: Bob Pearson X-Google-Original-From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH for-next 6/7] RDMA/rxe: Add unlocked versions of pool APIs Date: Wed, 16 Dec 2020 17:15:49 -0600 Message-Id: <20201216231550.27224-7-rpearson@hpe.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201216231550.27224-1-rpearson@hpe.com> References: <20201216231550.27224-1-rpearson@hpe.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org The existing pool APIs use the rw_lock pool_lock to protect critical sections that change the pool state. This does not correctly implement a typical sequence like the following elem = if found use elem else elem = Which is racy if multiple threads are attempting to perform this at the same time. We want the second thread to use the elem created by the first thread not create two equivalent elems. This patch adds new APIs that are the same as existing APIs but do not take the pool_lock. A caller can then take the lock and perform a sequence of pool operations and then release the lock. Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe_pool.c | 82 +++++++++++++++++++++------- drivers/infiniband/sw/rxe/rxe_pool.h | 41 ++++++++++++-- 2 files changed, 97 insertions(+), 26 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe_pool.c b/drivers/infiniband/sw/rxe/rxe_pool.c index c8a6d0d7f01a..d26730eec720 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.c +++ b/drivers/infiniband/sw/rxe/rxe_pool.c @@ -266,65 +266,89 @@ static void insert_key(struct rxe_pool *pool, struct rxe_pool_entry *new) return; } +void __rxe_add_key_nl(struct rxe_pool_entry *elem, void *key) +{ + struct rxe_pool *pool = elem->pool; + + memcpy((u8 *)elem + pool->key.key_offset, key, pool->key.key_size); + insert_key(pool, elem); +} + void __rxe_add_key(struct rxe_pool_entry *elem, void *key) { struct rxe_pool *pool = elem->pool; unsigned long flags; write_lock_irqsave(&pool->pool_lock, flags); - memcpy((u8 *)elem + pool->key.key_offset, key, pool->key.key_size); - insert_key(pool, elem); + __rxe_add_key_nl(elem, key); write_unlock_irqrestore(&pool->pool_lock, flags); } +void __rxe_drop_key_nl(struct rxe_pool_entry *elem) +{ + struct rxe_pool *pool = elem->pool; + + rb_erase(&elem->key_node, &pool->key.tree); +} + void __rxe_drop_key(struct rxe_pool_entry *elem) { struct rxe_pool *pool = elem->pool; unsigned long flags; write_lock_irqsave(&pool->pool_lock, flags); - rb_erase(&elem->key_node, &pool->key.tree); + __rxe_drop_key_nl(elem); write_unlock_irqrestore(&pool->pool_lock, flags); } +void __rxe_add_index_nl(struct rxe_pool_entry *elem) +{ + struct rxe_pool *pool = elem->pool; + + elem->index = alloc_index(pool); + insert_index(pool, elem); +} + void __rxe_add_index(struct rxe_pool_entry *elem) { struct rxe_pool *pool = elem->pool; unsigned long flags; write_lock_irqsave(&pool->pool_lock, flags); - elem->index = alloc_index(pool); - insert_index(pool, elem); + __rxe_add_index_nl(elem); write_unlock_irqrestore(&pool->pool_lock, flags); } +void __rxe_drop_index_nl(struct rxe_pool_entry *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_entry *elem) { struct rxe_pool *pool = elem->pool; unsigned long flags; write_lock_irqsave(&pool->pool_lock, flags); - clear_bit(elem->index - pool->index.min_index, pool->index.table); - rb_erase(&elem->index_node, &pool->index.tree); + __rxe_drop_index_nl(elem); write_unlock_irqrestore(&pool->pool_lock, flags); } -void *rxe_alloc(struct rxe_pool *pool) +void *rxe_alloc_nl(struct rxe_pool *pool) { struct rxe_type_info *info = &rxe_type_info[pool->type]; struct rxe_pool_entry *elem; u8 *obj; - unsigned long flags; might_sleep_if(!(pool->flags & RXE_POOL_ATOMIC)); - read_lock_irqsave(&pool->pool_lock, flags); - if (pool->state != RXE_POOL_STATE_VALID) { - read_unlock_irqrestore(&pool->pool_lock, flags); + if (pool->state != RXE_POOL_STATE_VALID) return NULL; - } + kref_get(&pool->ref_cnt); - read_unlock_irqrestore(&pool->pool_lock, flags); if (!ib_device_try_get(&pool->rxe->ib_dev)) goto out_put_pool; @@ -352,11 +376,21 @@ void *rxe_alloc(struct rxe_pool *pool) return NULL; } -int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem) +void *rxe_alloc(struct rxe_pool *pool) { + u8 *obj; unsigned long flags; - might_sleep_if(!(pool->flags & RXE_POOL_ATOMIC)); + read_lock_irqsave(&pool->pool_lock, flags); + obj = rxe_alloc_nl(pool); + read_unlock_irqrestore(&pool->pool_lock, flags); + + return obj; +} + +int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem) +{ + unsigned long flags; read_lock_irqsave(&pool->pool_lock, flags); if (pool->state != RXE_POOL_STATE_VALID) { @@ -444,16 +478,13 @@ void *rxe_pool_get_index(struct rxe_pool *pool, u32 index) return obj; } -void *rxe_pool_get_key(struct rxe_pool *pool, void *key) +void *rxe_pool_get_key_nl(struct rxe_pool *pool, void *key) { struct rxe_type_info *info = &rxe_type_info[pool->type]; struct rb_node *node; struct rxe_pool_entry *elem; u8 *obj = NULL; int cmp; - unsigned long flags; - - read_lock_irqsave(&pool->pool_lock, flags); if (pool->state != RXE_POOL_STATE_VALID) goto out; @@ -482,6 +513,17 @@ void *rxe_pool_get_key(struct rxe_pool *pool, void *key) } out: + return obj; +} + +void *rxe_pool_get_key(struct rxe_pool *pool, void *key) +{ + u8 *obj = NULL; + unsigned long flags; + + read_lock_irqsave(&pool->pool_lock, flags); + obj = rxe_pool_get_key_nl(pool, key); read_unlock_irqrestore(&pool->pool_lock, flags); + return obj; } diff --git a/drivers/infiniband/sw/rxe/rxe_pool.h b/drivers/infiniband/sw/rxe/rxe_pool.h index 5db0bdde185e..373e08554c1c 100644 --- a/drivers/infiniband/sw/rxe/rxe_pool.h +++ b/drivers/infiniband/sw/rxe/rxe_pool.h @@ -109,41 +109,70 @@ void rxe_pool_cleanup(struct rxe_pool *pool); /* allocate an object from pool */ void *rxe_alloc(struct rxe_pool *pool); +/* allocate an object from pool - no lock */ +void *rxe_alloc_nl(struct rxe_pool *pool); + /* connect already allocated object to pool */ int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem); #define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->pelem) /* assign an index to an indexed object and insert object into - * pool's rb tree + * pool's rb tree with and without holding the pool_lock */ void __rxe_add_index(struct rxe_pool_entry *elem); #define rxe_add_index(obj) __rxe_add_index(&(obj)->pelem) -/* drop an index and remove object from rb tree */ +void __rxe_add_index_nl(struct rxe_pool_entry *elem); + +#define rxe_add_index_nl(obj) __rxe_add_index_nl(&(obj)->pelem) + +/* drop an index and remove object from rb tree + * with and without holding the pool_lock + */ void __rxe_drop_index(struct rxe_pool_entry *elem); #define rxe_drop_index(obj) __rxe_drop_index(&(obj)->pelem) +void __rxe_drop_index_nl(struct rxe_pool_entry *elem); + +#define rxe_drop_index_nl(obj) __rxe_drop_index_nl(&(obj)->pelem) + /* assign a key to a keyed object and insert object into - * pool's rb tree + * pool's rb tree with and without holding pool_lock */ void __rxe_add_key(struct rxe_pool_entry *elem, void *key); #define rxe_add_key(obj, key) __rxe_add_key(&(obj)->pelem, key) -/* remove elem from rb tree */ +void __rxe_add_key_nl(struct rxe_pool_entry *elem, void *key); + +#define rxe_add_key_nl(obj, key) __rxe_add_key_nl(&(obj)->pelem, key) + +/* remove elem from rb tree with and without holding pool_lock */ void __rxe_drop_key(struct rxe_pool_entry *elem); #define rxe_drop_key(obj) __rxe_drop_key(&(obj)->pelem) -/* lookup an indexed object from index. takes a reference on object */ +void __rxe_drop_key_nl(struct rxe_pool_entry *elem); + +#define rxe_drop_key_nl(obj) __rxe_drop_key_nl(&(obj)->pelem) + +/* lookup an indexed object from index with and without holding pool_lock. + * takes a reference on object + */ void *rxe_pool_get_index(struct rxe_pool *pool, u32 index); -/* lookup keyed object from key. takes a reference on the object */ +void *rxe_pool_get_index_nl(struct rxe_pool *pool, u32 index); + +/* lookup keyed object from key with and without holding pool_lock. + * takes a reference on the objecti + */ void *rxe_pool_get_key(struct rxe_pool *pool, void *key); +void *rxe_pool_get_key_nl(struct rxe_pool *pool, void *key); + /* cleanup an object when all references are dropped */ void rxe_elem_release(struct kref *kref); From patchwork Wed Dec 16 23:15:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bob Pearson X-Patchwork-Id: 11978745 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 X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 556D5C4361B for ; Wed, 16 Dec 2020 23:17:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2F86C23437 for ; Wed, 16 Dec 2020 23:17:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730697AbgLPXRT (ORCPT ); Wed, 16 Dec 2020 18:17:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730782AbgLPXRS (ORCPT ); Wed, 16 Dec 2020 18:17:18 -0500 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 975D7C061285 for ; Wed, 16 Dec 2020 15:16:02 -0800 (PST) Received: by mail-oi1-x22c.google.com with SMTP id s2so29854893oij.2 for ; Wed, 16 Dec 2020 15:16:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tFN4GfjJ+3JUFU0+MXiNzXNZ+QcrK3QOX7uArKKgoYc=; b=mg0SJX6tUpUif9uH0YQuxxSez/JS7mAYWeJEQH8UTwnDyLCEMvhhG2GiVdTqtA0ovt +cN2tksdZEF/1I31jHg/eTLyofW2jFomvVbbMG1v7yn0q1HZ6QiRTLCjkVODF2uK6SI2 6069lLBRknzIvatX/7ogTGuS7FoAnymtth29Kh+v9uPHhm4nlOLgXR9oVJbf+dLVy3l1 41VcPoXidKM+RvikFfixCFLbnAt6Sc4G3gHfK/v30Y4KrRdtT4zvqtqP0KHgZlHMzICW Rd3zFXLVrpUPi3jdcRgNUR/GTl2cJLw1GgeW5xADcZJDRmJu7id3ZVvY++gG6F0pesbG yIWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tFN4GfjJ+3JUFU0+MXiNzXNZ+QcrK3QOX7uArKKgoYc=; b=aJvoiucIwecB8iIZ+d2OWwvTAq+y8GHdHwVOM/aBCQqL6lWPMuc9qu6I2IS/dCfxhU EkrHIl/YqJW54kZn8PncvHrFZrE0P4Jp6tsTHgwvAUIsXo8FdpUBJthJrFvw8dGav4zh UgqMafvviOdvAucYcaW70p9ND00puerA9NKWMI3G2HPG6uosq1b6Pw1oGRSyJTifXXBe tCY1gRXYKMHPsjcRqHyp4+jdTkiHplqtYwGp2r1lLAZpTqlRUKMhgaMHXGaPQGnYG66F IMQUyOwb5+2JSeBvFnrdyOAtCxJCKvzbjLh8hiGvplT1FjTubhuaCxgq/9z3sJoeKX3k Bk6g== X-Gm-Message-State: AOAM531ygNgPUChWWnzS9xLjxzqYWEsCF+JheuFIFLLnF2+vsTCYKOEi wk+OH+56DIQ3S847sSTvVw0= X-Google-Smtp-Source: ABdhPJw8yrzJ28SSfsAR9VdsBxl6/pMSmsVpR817T3Lt8m9k2wxxiUUqf3IsKLj0AuLl1z28zvk5ig== X-Received: by 2002:aca:b06:: with SMTP id 6mr3233607oil.74.1608160562079; Wed, 16 Dec 2020 15:16:02 -0800 (PST) Received: from rpearson-X570-AORUS-PRO-WIFI.tx.rr.com (2603-8081-140c-1a00-6a03-8d89-0aec-a801.res6.spectrum.com. [2603:8081:140c:1a00:6a03:8d89:aec:a801]) by smtp.gmail.com with ESMTPSA id f18sm793437otf.55.2020.12.16.15.16.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Dec 2020 15:16:01 -0800 (PST) From: Bob Pearson X-Google-Original-From: Bob Pearson To: jgg@nvidia.com, zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org Cc: Bob Pearson Subject: [PATCH 7/7] RDMA/rxe: Fix race in rxe_mcast.c Date: Wed, 16 Dec 2020 17:15:50 -0600 Message-Id: <20201216231550.27224-8-rpearson@hpe.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201216231550.27224-1-rpearson@hpe.com> References: <20201216231550.27224-1-rpearson@hpe.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Fix a race in rxe_mcast.c that occurs when two QPs try at the same time to attach a multicast address. Both QPs lookup the mgid address in a pool of multicast groups and if they do not find it create a new group elem. Fix this by locking the lookup/alloc/add key sequence and using the unlocked APIs added in this patch set. Signed-off-by: Bob Pearson --- drivers/infiniband/sw/rxe/rxe_mcast.c | 64 +++++++++++++++++---------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/drivers/infiniband/sw/rxe/rxe_mcast.c b/drivers/infiniband/sw/rxe/rxe_mcast.c index c02315aed8d1..5be47ce7d319 100644 --- a/drivers/infiniband/sw/rxe/rxe_mcast.c +++ b/drivers/infiniband/sw/rxe/rxe_mcast.c @@ -7,45 +7,61 @@ #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 err; + struct rxe_mc_grp *grp; + + grp = rxe_alloc_nl(&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_nl(grp, mgid); + + err = rxe_mcast_add(rxe, mgid); + if (unlikely(err)) { + rxe_drop_key_nl(grp); + rxe_drop_ref(grp); + return ERR_PTR(err); + } + + return grp; +} + 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; + unsigned long flags; - if (rxe->attr.max_mcast_qp_attach == 0) { - err = -EINVAL; - goto err1; - } + if (rxe->attr.max_mcast_qp_attach == 0) + return -EINVAL; - grp = rxe_pool_get_key(&rxe->mc_grp_pool, mgid); + write_lock_irqsave(&pool->pool_lock, flags); + + grp = rxe_pool_get_key_nl(pool, mgid); if (grp) goto done; - grp = rxe_alloc(&rxe->mc_grp_pool); - if (!grp) { - err = -ENOMEM; - goto err1; + grp = create_grp(rxe, pool, mgid); + if (IS_ERR(grp)) { + write_unlock_irqrestore(&pool->pool_lock, flags); + err = PTR_ERR(grp); + return err; } - INIT_LIST_HEAD(&grp->qp_list); - spin_lock_init(&grp->mcg_lock); - grp->rxe = rxe; - - rxe_add_key(grp, mgid); - - err = rxe_mcast_add(rxe, mgid); - if (err) - goto err2; - done: + write_unlock_irqrestore(&pool->pool_lock, flags); *grp_p = grp; return 0; - -err2: - rxe_drop_ref(grp); -err1: - return err; } int rxe_mcast_add_grp_elem(struct rxe_dev *rxe, struct rxe_qp *qp,