Skip to contentSkip to conversation

Add new bitmap API

PATCH In review Yishai Hadas 1 participant 7 messages Feb 28, 2022 – Mar 10, 2022 InfiniBand

lore
Share & tools
lore
#0 lore raw

cover Add new bitmap API

This series adds new bitmap implementation to util directory which replaces the ccan equivalent.

The ccan/bitmap stuff was published under the LGPLv2+ license which is not fitting in rdma-core.

As of the above, the applicable bitmap code in rdma-core was adapted to use the util new API.

PR was sent:
https://github.com/linux-rdma/rdma-core/pull/1144

Maher Sanalla (5):
util: Add new bitmap API
mlx5: Adapt bitmap usage to use util API
libhns: Adapt bitmap usage to use util API
verbs: Adapt bitmap usage to use util API
ccan: Remove bitmap code

 ccan/CMakeLists.txt           |   2 -
 ccan/bitmap.c                 | 125 ----------------------
 ccan/bitmap.h                 | 239 ------------------------------------------
 libibverbs/ibverbs.h          |   4 +-
 providers/hns/hns_roce_u.h    |   4 +-
 providers/hns/hns_roce_u_db.c |   4 +-
 providers/mlx5/bitmap.h       | 111 --------------------
 providers/mlx5/buf.c          | 187 +++++++--------------------------
 providers/mlx5/dr_buddy.c     |  12 +--
 providers/mlx5/mlx5.h         |  17 +--
 providers/mlx5/mlx5_vfio.c    |   3 +-
 providers/mlx5/mlx5_vfio.h    |   2 +-
 providers/mlx5/mlx5dv_dr.h    |   6 +-
 util/CMakeLists.txt           |   2 +
 util/bitmap.c                 | 180 +++++++++++++++++++++++++++++++
 util/bitmap.h                 | 120 +++++++++++++++++++++
 util/util.h                   |   1 +
 17 files changed, 362 insertions(+), 657 deletions(-)
 delete mode 100644 ccan/bitmap.c
 delete mode 100644 ccan/bitmap.h
 delete mode 100644 providers/mlx5/bitmap.h
 create mode 100644 util/bitmap.c
 create mode 100644 util/bitmap.h
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/5 util: Add new bitmap API

From: Maher Sanalla <[email protected]>

Adds new bitmap implementation to util directory, which replaces the ccan equivalent, due to the license used (LGPLv2+) which is not fitting in rdma-core.

---
 providers/mlx5/bitmap.h |   2 -
 util/CMakeLists.txt     |   2 +
 util/bitmap.c           | 180 ++++++++++++++++++++++++++++++++++++++++++++++++
 util/bitmap.h           | 120 ++++++++++++++++++++++++++++++++
 util/util.h             |   1 +
 5 files changed, 303 insertions(+), 2 deletions(-)
 create mode 100644 util/bitmap.c
 create mode 100644 util/bitmap.h
providers/mlx5/bitmap.h +0 -2
index 034fb98..b2c8a36 100644
@@ -54,8 +54,6 @@
 #define MLX5_SHMAT_FLAGS 0
 #endif
 
-#define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_LONG)
-
 #ifndef HPAGE_SIZE
 #define HPAGE_SIZE		(2UL * 1024 * 1024)
 #endif
util/CMakeLists.txt +2 -0
index d8a66be..58c77d5 100644
@@ -1,4 +1,5 @@
 publish_internal_headers(util
+  bitmap.h
   cl_qmap.h
   compiler.h
   interval_set.h
@@ -9,6 +10,7 @@ publish_internal_headers(util
   )
 
 set(C_FILES
+  bitmap.c
   cl_map.c
   interval_set.c
   node_name_map.c
util/bitmap.c new file +180 -0

180 lines not on this page · raw message

util/bitmap.h new file +120 -0

120 lines not on this page · raw message

util/util.h +1 -0
index f721b83..af03c42 100644
@@ -28,6 +28,7 @@ static inline bool __good_snprintf(size_t len, int rc)
 
 #define BITS_PER_LONG	   (8 * sizeof(long))
 #define BITS_PER_LONG_LONG (8 * sizeof(long long))
+#define BITS_TO_LONGS(nr)  (((nr) + BITS_PER_LONG - 1) / BITS_PER_LONG)
 
 #define GENMASK(h, l) \
 	(((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/5 mlx5: Adapt bitmap usage to use util API

In reply to Yishai Hadas

From: Maher Sanalla <[email protected]>

Replace the usage of ccan bitmap with the newly added bitmap implementation in util.

As part of it, remove mlx5 bitmap API.

---
 providers/mlx5/bitmap.h    | 109 --------------------------
 providers/mlx5/buf.c       | 187 +++++++++------------------------------------
 providers/mlx5/dr_buddy.c  |  12 +--
 providers/mlx5/mlx5.h      |  17 +----
 providers/mlx5/mlx5_vfio.c |   3 +-
 providers/mlx5/mlx5_vfio.h |   2 +-
 providers/mlx5/mlx5dv_dr.h |   6 +-
 7 files changed, 53 insertions(+), 283 deletions(-)
 delete mode 100644 providers/mlx5/bitmap.h
providers/mlx5/bitmap.h deleted +0 -109
deleted file mode 100644
index b2c8a36..0000000
@@ -1,109 +0,0 @@
-/*
- * Copyright (c) 2000, 2011 Mellanox Technology Inc.  All rights reserved.
- *
- * This software is available to you under a choice of one of two
- * licenses.  You may choose to be licensed under the terms of the GNU
- * General Public License (GPL) Version 2, available from the file
- * COPYING in the main directory of this source tree, or the
- * OpenIB.org BSD license below:
- *
- *     Redistribution and use in source and binary forms, with or
- *     without modification, are permitted provided that the following
- *     conditions are met:
- *
- *      - Redistributions of source code must retain the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer.
- *
- *      - Redistributions in binary form must reproduce the above
- *        copyright notice, this list of conditions and the following
- *        disclaimer in the documentation and/or other materials
- *        provided with the distribution.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-#ifndef BITMAP_H
-#define BITMAP_H
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <pthread.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#include <sys/mman.h>
-#include <linux/errno.h>
-#include <util/util.h>
-#include "mlx5.h"
-
-/* Only ia64 requires this */
-#ifdef __ia64__
-#define MLX5_SHM_ADDR ((void *)0x8000000000000000UL)
-#define MLX5_SHMAT_FLAGS (SHM_RND)
-#else
-#define MLX5_SHM_ADDR NULL
-#define MLX5_SHMAT_FLAGS 0
-#endif
-
-#ifndef HPAGE_SIZE
-#define HPAGE_SIZE		(2UL * 1024 * 1024)
-#endif
-
-#define MLX5_SHM_LENGTH		HPAGE_SIZE
-#define MLX5_Q_CHUNK_SIZE	32768
-#define MLX5_SHM_NUM_REGION	64
-
-static inline unsigned long mlx5_ffz(uint32_t word)
-{
-	return __builtin_ffs(~word) - 1;
-}
-
-static inline uint32_t mlx5_find_first_zero_bit(const unsigned long *addr,
-					 uint32_t size)
-{
-	const unsigned long *p = addr;
-	uint32_t result = 0;
-	unsigned long tmp;
-
-	while (size & ~(BITS_PER_LONG - 1)) {
-		tmp = *(p++);
-		if (~tmp)
-			goto found;
-		result += BITS_PER_LONG;
-		size -= BITS_PER_LONG;
-	}
-	if (!size)
-		return result;
-
-	tmp = (*p) | (~0UL << size);
-	if (tmp == (uint32_t)~0UL)	/* Are any bits zero? */
-		return result + size;	/* Nope. */
-found:
-	return result + mlx5_ffz(tmp);
-}
-
-static inline void mlx5_set_bit(unsigned int nr, unsigned long *addr)
-{
-	addr[(nr / BITS_PER_LONG)] |= (1 << (nr % BITS_PER_LONG));
-}
-
-static inline void mlx5_clear_bit(unsigned int nr,  unsigned long *addr)
-{
-	addr[(nr / BITS_PER_LONG)] &= ~(1 << (nr % BITS_PER_LONG));
-}
-
-static inline int mlx5_test_bit(unsigned int nr, const unsigned long *addr)
-{
-	return !!(addr[(nr / BITS_PER_LONG)] & (1 <<  (nr % BITS_PER_LONG)));
-}
-
-#endif
providers/mlx5/buf.c +37 -150

245 lines not on this page · raw message

providers/mlx5/dr_buddy.c +6 -6
index e153677..8713fe4 100644
@@ -34,23 +34,23 @@
  */
 
 #include <stdlib.h>
-#include <ccan/bitmap.h>
+#include <util/bitmap.h>
 #include "mlx5dv_dr.h"
 
 struct dr_icm_pool;
 struct dr_icm_buddy_mem;
 
-static int dr_find_first_bit(const bitmap *set_addr,
-			     const bitmap *addr,
+static int dr_find_first_bit(const unsigned long *set_addr,
+			     const unsigned long *addr,
 			     unsigned int size)
 {
 	unsigned int set_size = (size - 1) / BITS_PER_LONG + 1;
 	unsigned long set_idx;
 
 	/* find the first free in the first level */
-	set_idx =  bitmap_ffs(set_addr, 0, set_size);
+	set_idx =  bitmap_find_first_bit(set_addr, 0, set_size);
 	/* find the next level */
-	return bitmap_ffs(addr, set_idx * BITS_PER_LONG, size);
+	return bitmap_find_first_bit(addr, set_idx * BITS_PER_LONG, size);
 }
 
 int dr_buddy_init(struct dr_icm_buddy_mem *buddy, uint32_t max_order)
@@ -161,7 +161,7 @@ static void dr_buddy_update_upper_bitmap(struct dr_icm_buddy_mem *buddy,
 
 	/* clear upper layer of search if needed */
 	dr_buddy_get_seg_borders(seg, &l, &h);
-	m = bitmap_ffs(buddy->bits[order], l, h);
+	m = bitmap_find_first_bit(buddy->bits[order], l, h);
 	if (m == h) /* nothing in the long that includes seg */
 		bitmap_clear_bit(buddy->set_bit[order], seg / BITS_PER_LONG);
 }
providers/mlx5/mlx5.h +4 -13
index 1eca478..4656153 100644
@@ -44,9 +44,8 @@
 #include <util/udma_barrier.h>
 #include <util/util.h>
 #include "mlx5-abi.h"
-#include <ccan/bitmap.h>
+#include <util/bitmap.h>
 #include <ccan/list.h>
-#include "bitmap.h"
 #include <ccan/minmax.h>
 #include "mlx5dv.h"
 
@@ -289,7 +288,7 @@ struct mlx5_hca_cap_2_caps {
 };
 
 struct reserved_qpn_blk {
-	bitmap *bmp;
+	unsigned long *bmp;
 	uint32_t first_qpn;
 	struct list_node entry;
 	unsigned int next_avail_slot;
@@ -419,19 +418,11 @@ struct mlx5_context {
 	pthread_mutex_t			crypto_login_mutex;
 };
 
-struct mlx5_bitmap {
-	uint32_t		last;
-	uint32_t		top;
-	uint32_t		max;
-	uint32_t		avail;
-	uint32_t		mask;
-	unsigned long	       *table;
-};
-
 struct mlx5_hugetlb_mem {
 	int			shmid;
 	void		       *shmaddr;
-	struct mlx5_bitmap	bitmap;
+	unsigned long		*bitmap;
+	unsigned long		bmp_size;
 	struct list_node	entry;
 };
 
providers/mlx5/mlx5_vfio.c +2 -1
index 3f1811d..b9cdfeb 100644
@@ -142,7 +142,8 @@ static int mlx5_vfio_alloc_page(struct mlx5_vfio_context *ctx, uint64_t *iova)
 	pthread_mutex_lock(&ctx->mem_alloc.block_list_mutex);
 	while (true) {
 		list_for_each(&ctx->mem_alloc.block_list, page_block, next_block) {
-			pg = bitmap_ffs(page_block->free_pages, 0, MLX5_VFIO_BLOCK_NUM_PAGES);
+			pg = bitmap_find_first_bit(page_block->free_pages, 0,
+						   MLX5_VFIO_BLOCK_NUM_PAGES);
 			if (pg != MLX5_VFIO_BLOCK_NUM_PAGES) {
 				bitmap_clear_bit(page_block->free_pages, pg);
 				*iova = page_block->iova + pg * MLX5_ADAPTER_PAGE_SIZE;
providers/mlx5/mlx5_vfio.h +1 -1
index 2165a22..88cc332 100644
@@ -163,7 +163,7 @@ struct page_block {
 	void *page_ptr;
 	uint64_t iova;
 	struct list_node next_block;
-	BITMAP_DECLARE(free_pages, MLX5_VFIO_BLOCK_NUM_PAGES);
+	BMP_DECLARE(free_pages, MLX5_VFIO_BLOCK_NUM_PAGES);
 };
 
 struct vfio_mem_allocator {
providers/mlx5/mlx5dv_dr.h +3 -3
index 16e5340..3cc3035 100644
@@ -35,7 +35,7 @@
 
 #include <ccan/list.h>
 #include <ccan/minmax.h>
-#include <ccan/bitmap.h>
+#include <util/bitmap.h>
 #include <stdatomic.h>
 #include "mlx5dv.h"
 #include "mlx5_ifc.h"
@@ -1619,9 +1619,9 @@ int dr_send_postsend_action(struct mlx5dv_dr_domain *dmn,
 struct dr_icm_mr;
 
 struct dr_icm_buddy_mem {
-	bitmap			**bits;
+	unsigned long		**bits;
 	unsigned int		*num_free;
-	bitmap			**set_bit;
+	unsigned long		**set_bit;
 	uint32_t		max_order;
 	struct list_node	list_node;
 	struct dr_icm_mr	*icm_mr;
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/5 libhns: Adapt bitmap usage to use util API

In reply to Yishai Hadas

From: Maher Sanalla <[email protected]>

Replace the usage of ccan bitmap with the newly added bitmap implementation in util.

---
 providers/hns/hns_roce_u.h    | 4 ++--
 providers/hns/hns_roce_u_db.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
providers/hns/hns_roce_u.h +2 -2
index 2b4ba18..d786efb 100644
@@ -42,7 +42,7 @@
 #include <util/util.h>
 #include <infiniband/verbs.h>
 #include <ccan/array_size.h>
-#include <ccan/bitmap.h>
+#include <util/bitmap.h>
 #include <ccan/container_of.h>
 #include <linux/if_ether.h>
 #include "hns_roce_u_abi.h"
@@ -193,7 +193,7 @@ struct hns_roce_db_page {
 	struct hns_roce_buf	buf;
 	unsigned int		num_db;
 	unsigned int		use_cnt;
-	bitmap			*bitmap;
+	unsigned long		*bitmap;
 };
 
 struct hns_roce_context {
providers/hns/hns_roce_u_db.c +2 -2
index f7aaa6e..f5acac2 100644
@@ -33,7 +33,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
-#include <ccan/bitmap.h>
+#include <util/bitmap.h>
 #include "hns_roce_u.h"
 #include "hns_roce_u_db.h"
 
@@ -109,7 +109,7 @@ void *hns_roce_alloc_db(struct hns_roce_context *ctx,
 found:
 	++page->use_cnt;
 
-	npos = bitmap_ffs(page->bitmap, 0, page->num_db);
+	npos = bitmap_find_first_bit(page->bitmap, 0, page->num_db);
 	bitmap_clear_bit(page->bitmap, npos);
 	db = page->buf.buf + npos * db_size[type];
 
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 4/5 verbs: Adapt bitmap usage to use util API

In reply to Yishai Hadas

From: Maher Sanalla <[email protected]>

Replace the usage of ccan bitmap with the newly added bitmap implementation in util.

---
 libibverbs/ibverbs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libibverbs/ibverbs.h +2 -2
index 5e60ace..33f83a7 100644
@@ -37,7 +37,7 @@
 #include <pthread.h>
 
 #include <infiniband/driver.h>
-#include <ccan/bitmap.h>
+#include <util/bitmap.h>
 
 #define INIT		__attribute__((constructor))
 
@@ -70,7 +70,7 @@ void load_drivers(void);
 #endif
 
 struct verbs_ex_private {
-	BITMAP_DECLARE(unsupported_ioctls, VERBS_OPS_NUM);
+	BMP_DECLARE(unsupported_ioctls, VERBS_OPS_NUM);
 	uint32_t driver_id;
 	bool use_ioctl_write;
 	struct verbs_context_ops ops;
Signature · 1 line
1.8.3.1
#5 lore raw

PATCH 5/5 ccan: Remove bitmap code

In reply to Yishai Hadas

From: Maher Sanalla <[email protected]>

Removes ccan bitmap implementation, along with adjustments to the ccan Makefile.

---
 ccan/CMakeLists.txt |   2 -
 ccan/bitmap.c       | 125 ---------------------------
 ccan/bitmap.h       | 239 ----------------------------------------------------
 3 files changed, 366 deletions(-)
 delete mode 100644 ccan/bitmap.c
 delete mode 100644 ccan/bitmap.h
ccan/CMakeLists.txt +0 -2
index 5c5c6a2..d370a8e 100644
@@ -1,6 +1,5 @@
 publish_internal_headers(ccan
   array_size.h
-  bitmap.h
   build_assert.h
   check_type.h
   compiler.h
@@ -13,7 +12,6 @@ publish_internal_headers(ccan
   )
 
 set(C_FILES
-  bitmap.c
   ilog.c
   list.c
   str.c
ccan/bitmap.c deleted +0 -125

125 lines not on this page · raw message

ccan/bitmap.h deleted +0 -239

239 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#6 lore raw
In reply to Yishai Hadas
Quoted 37 lines · Yishai Hadas

This series adds new bitmap implementation to util directory which replaces the ccan equivalent.

The ccan/bitmap stuff was published under the LGPLv2+ license which is not fitting in rdma-core.

As of the above, the applicable bitmap code in rdma-core was adapted to use the util new API.

PR was sent:
https://github.com/linux-rdma/rdma-core/pull/1144

Maher Sanalla (5):
util: Add new bitmap API
mlx5: Adapt bitmap usage to use util API
libhns: Adapt bitmap usage to use util API
verbs: Adapt bitmap usage to use util API
ccan: Remove bitmap code

  ccan/CMakeLists.txt           |   2 -
  ccan/bitmap.c                 | 125 ----------------------
  ccan/bitmap.h                 | 239 ------------------------------------------
  libibverbs/ibverbs.h          |   4 +-
  providers/hns/hns_roce_u.h    |   4 +-
  providers/hns/hns_roce_u_db.c |   4 +-
  providers/mlx5/bitmap.h       | 111 --------------------
  providers/mlx5/buf.c          | 187 +++++++--------------------------
  providers/mlx5/dr_buddy.c     |  12 +--
  providers/mlx5/mlx5.h         |  17 +--
  providers/mlx5/mlx5_vfio.c    |   3 +-
  providers/mlx5/mlx5_vfio.h    |   2 +-
  providers/mlx5/mlx5dv_dr.h    |   6 +-
  util/CMakeLists.txt           |   2 +
  util/bitmap.c                 | 180 +++++++++++++++++++++++++++++++
  util/bitmap.h                 | 120 +++++++++++++++++++++
  util/util.h                   |   1 +
  17 files changed, 362 insertions(+), 657 deletions(-)
Quoted 6 lines

delete mode 100644 ccan/bitmap.c
delete mode 100644 ccan/bitmap.h
delete mode 100644 providers/mlx5/bitmap.h
create mode 100644 util/bitmap.c
create mode 100644 util/bitmap.h

The PR was merged.

Yishai