Skip to contentSkip to conversation

RDMA/siw: use kzalloc_flex

RFC PATCH Superseded [email protected] 1 participant 1 message May 7

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

RFC PATCH v2 1/1 RDMA/siw: use kzalloc_flex

From: Bernard Metzler <[email protected]>

Simplify umem allocation by using flexible array member. Add __counted_by to get extra runtime analysis.

---
v2:
1. Considering comments from sashiko.dev to original patch regarding:
- avoiding allocation of extra empty page chunk entry, if number of pages is an exact multiplier
of PAGES_PER_CHUNK,
- fix potential signed index overflow case for page list generation and access,
- restrict page list access in siw_get_upage() to allocated length.

2. Extend flexible array allocation to page list.

3. Remove useless PAGE_CHUNK_SIZE definition.

---
 drivers/infiniband/sw/siw/siw.h     |  7 ++--
 drivers/infiniband/sw/siw/siw_mem.c | 59 ++++++++++++++---------------
 drivers/infiniband/sw/siw/siw_mem.h |  9 +++--
 3 files changed, 37 insertions(+), 38 deletions(-)
drivers/infiniband/sw/siw/siw.h +4 -3
index f5fd71717b80..6ce30211d066 100644
@@ -114,14 +114,15 @@ struct siw_ucontext {
  */
 
 struct siw_page_chunk {
-	struct page **plist;
+	unsigned int nents;
+	struct page *plist[] __counted_by(nents);
 };
 
 struct siw_umem {
 	struct ib_umem *base_mem;
-	struct siw_page_chunk *page_chunk;
-	int num_pages;
+	unsigned int num_chunks;
 	u64 fp_addr; /* First page base address */
+	struct siw_page_chunk *page_chunk[] __counted_by(num_chunks);
 };
 
 struct siw_pble {
drivers/infiniband/sw/siw/siw_mem.c +28 -31

105 lines not on this page · raw message

drivers/infiniband/sw/siw/siw_mem.h +5 -4
index 8e769d30e2ac..47ab38faad7c 100644
@@ -17,7 +17,7 @@ int siw_check_mem(struct ib_pd *pd, struct siw_mem *mem, u64 addr,
 		  enum ib_access_flags perms, int len);
 int siw_check_sge(struct ib_pd *pd, struct siw_sge *sge,
 		  struct siw_mem *mem[], enum ib_access_flags perms,
-		  u32 off, int len);
+		  u32 off, u32 len);
 void siw_wqe_put_mem(struct siw_wqe *wqe, enum siw_opcode op);
 int siw_mr_add_mem(struct siw_mr *mr, struct ib_pd *pd, void *mem_obj,
 		   u64 start, u64 len, int rights);
@@ -45,7 +45,6 @@ static inline void siw_unref_mem_sgl(struct siw_mem **mem, unsigned int num_sge)
 #define CHUNK_SHIFT 9 /* sets number of pages per chunk */
 #define PAGES_PER_CHUNK (_AC(1, UL) << CHUNK_SHIFT)
 #define CHUNK_MASK (~(PAGES_PER_CHUNK - 1))
-#define PAGE_CHUNK_SIZE (PAGES_PER_CHUNK * sizeof(struct page *))
 
 /*
  * siw_get_upage()
@@ -60,9 +59,11 @@ static inline struct page *siw_get_upage(struct siw_umem *umem, u64 addr)
 	unsigned int page_idx = (addr - umem->fp_addr) >> PAGE_SHIFT,
 		     chunk_idx = page_idx >> CHUNK_SHIFT,
 		     page_in_chunk = page_idx & ~CHUNK_MASK;
+	struct siw_page_chunk *page_chunk = umem->page_chunk[chunk_idx];
 
-	if (likely(page_idx < umem->num_pages))
-		return umem->page_chunk[chunk_idx].plist[page_in_chunk];
+	if (likely(chunk_idx < umem->num_chunks &&
+		   page_in_chunk < page_chunk->nents))
+		return page_chunk->plist[page_in_chunk];
 
 	return NULL;
 }
Signature · 1 line
2.50.0