Skip to content

Commit c897c2c

Browse files
mrgolinrleon
authored andcommitted
RDMA/core: Add umem "is_contiguous" and "start_dma_addr" helpers
In some cases drivers may need to check if a given umem is contiguous. Add a helper function in core code so that drivers don't need to deal with umem or scatter-gather lists structure. Additionally add a helper for getting umem's start DMA address and use it in other helper functions that open code it. Signed-off-by: Michael Margolin <[email protected]> Link: https://patch.msgid.link/[email protected] Reviewed-by: Jason Gunthorpe <[email protected]> Signed-off-by: Leon Romanovsky <[email protected]>
1 parent 1a40c36 commit c897c2c

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

include/rdma/ib_umem.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ static inline int ib_umem_offset(struct ib_umem *umem)
5252
return umem->address & ~PAGE_MASK;
5353
}
5454

55+
static inline dma_addr_t ib_umem_start_dma_addr(struct ib_umem *umem)
56+
{
57+
return sg_dma_address(umem->sgt_append.sgt.sgl) + ib_umem_offset(umem);
58+
}
59+
5560
static inline unsigned long ib_umem_dma_offset(struct ib_umem *umem,
5661
unsigned long pgsz)
5762
{
58-
return (sg_dma_address(umem->sgt_append.sgt.sgl) + ib_umem_offset(umem)) &
59-
(pgsz - 1);
63+
return ib_umem_start_dma_addr(umem) & (pgsz - 1);
6064
}
6165

6266
static inline size_t ib_umem_num_dma_blocks(struct ib_umem *umem,
@@ -135,14 +139,27 @@ static inline unsigned long ib_umem_find_best_pgoff(struct ib_umem *umem,
135139
unsigned long pgsz_bitmap,
136140
u64 pgoff_bitmask)
137141
{
138-
struct scatterlist *sg = umem->sgt_append.sgt.sgl;
139142
dma_addr_t dma_addr;
140143

141-
dma_addr = sg_dma_address(sg) + (umem->address & ~PAGE_MASK);
144+
dma_addr = ib_umem_start_dma_addr(umem);
142145
return ib_umem_find_best_pgsz(umem, pgsz_bitmap,
143146
dma_addr & pgoff_bitmask);
144147
}
145148

149+
static inline bool ib_umem_is_contiguous(struct ib_umem *umem)
150+
{
151+
dma_addr_t dma_addr;
152+
unsigned long pgsz;
153+
154+
/*
155+
* Select the smallest aligned page that can contain the whole umem if
156+
* it was contiguous.
157+
*/
158+
dma_addr = ib_umem_start_dma_addr(umem);
159+
pgsz = roundup_pow_of_two((dma_addr ^ (umem->length - 1 + dma_addr)) + 1);
160+
return !!ib_umem_find_best_pgoff(umem, pgsz, U64_MAX);
161+
}
162+
146163
struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
147164
unsigned long offset, size_t size,
148165
int fd, int access,

0 commit comments

Comments
 (0)