Skip to content

Commit b045db6

Browse files
committed
RDMA/mlx5: Use ib_umem_find_best_pgoff() for SRQ
SRQ uses a quantized and scaled page_offset, which is another variation of ib_umem_find_best_pgsz(). Add mlx5_umem_find_best_quantized_pgoff() to perform this calculation for each mailbox. A macro shows how the calculation is directly connected to the mailbox format. This new routine replaces the limited mlx5_ib_cont_pages() and mlx5_ib_get_buf_offset() pairing which would reject valid configurations rather than adjust the page_size to make it work. In turn this is much more aggressive about choosing large page sizes for these objects and when THP is enabled it will now often find a single page solution. Link: https://lore.kernel.org/r/20201115114311.136250-2-leon@kernel.org Signed-off-by: Leon Romanovsky <leonro@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 8c030d7 commit b045db6

4 files changed

Lines changed: 128 additions & 10 deletions

File tree

‎drivers/infiniband/hw/mlx5/mem.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,51 @@ void mlx5_ib_populate_pas(struct ib_umem *umem, size_t page_size, __be64 *pas,
107107
}
108108
}
109109

110+
/*
111+
* Compute the page shift and page_offset for mailboxes that use a quantized
112+
* page_offset. The granulatity of the page offset scales according to page
113+
* size.
114+
*/
115+
unsigned long __mlx5_umem_find_best_quantized_pgoff(
116+
struct ib_umem *umem, unsigned long pgsz_bitmap,
117+
unsigned int page_offset_bits, u64 pgoff_bitmask, unsigned int scale,
118+
unsigned int *page_offset_quantized)
119+
{
120+
const u64 page_offset_mask = (1 << page_offset_bits) - 1;
121+
unsigned long page_size;
122+
u64 page_offset;
123+
124+
page_size = ib_umem_find_best_pgoff(umem, pgsz_bitmap, pgoff_bitmask);
125+
if (!page_size)
126+
return 0;
127+
128+
/*
129+
* page size is the largest possible page size.
130+
*
131+
* Reduce the page_size, and thus the page_offset and quanta, until the
132+
* page_offset fits into the mailbox field. Once page_size < scale this
133+
* loop is guaranteed to terminate.
134+
*/
135+
page_offset = ib_umem_dma_offset(umem, page_size);
136+
while (page_offset & ~(u64)(page_offset_mask * (page_size / scale))) {
137+
page_size /= 2;
138+
page_offset = ib_umem_dma_offset(umem, page_size);
139+
}
140+
141+
/*
142+
* The address is not aligned, or otherwise cannot be represented by the
143+
* page_offset.
144+
*/
145+
if (!(pgsz_bitmap & page_size))
146+
return 0;
147+
148+
*page_offset_quantized =
149+
(unsigned long)page_offset / (page_size / scale);
150+
if (WARN_ON(*page_offset_quantized > page_offset_mask))
151+
return 0;
152+
return page_size;
153+
}
154+
110155
int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
111156
{
112157
u64 page_size;

‎drivers/infiniband/hw/mlx5/mlx5_ib.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,37 @@ __mlx5_log_page_size_to_bitmap(unsigned int log_pgsz_bits,
6969
pgsz_shift), \
7070
iova)
7171

72+
static __always_inline unsigned long
73+
__mlx5_page_offset_to_bitmask(unsigned int page_offset_bits,
74+
unsigned int offset_shift)
75+
{
76+
unsigned int largest_offset_shift =
77+
min_t(unsigned long, page_offset_bits - 1 + offset_shift,
78+
BITS_PER_LONG - 1);
79+
80+
return GENMASK(largest_offset_shift, offset_shift);
81+
}
82+
83+
/*
84+
* QP/CQ/WQ/etc type commands take a page offset that satisifies:
85+
* page_offset_quantized * (page_size/scale) = page_offset
86+
* Which restricts allowed page sizes to ones that satisify the above.
87+
*/
88+
unsigned long __mlx5_umem_find_best_quantized_pgoff(
89+
struct ib_umem *umem, unsigned long pgsz_bitmap,
90+
unsigned int page_offset_bits, u64 pgoff_bitmask, unsigned int scale,
91+
unsigned int *page_offset_quantized);
92+
#define mlx5_umem_find_best_quantized_pgoff(umem, typ, log_pgsz_fld, \
93+
pgsz_shift, page_offset_fld, \
94+
scale, page_offset_quantized) \
95+
__mlx5_umem_find_best_quantized_pgoff( \
96+
umem, \
97+
__mlx5_log_page_size_to_bitmap( \
98+
__mlx5_bit_sz(typ, log_pgsz_fld), pgsz_shift), \
99+
__mlx5_bit_sz(typ, page_offset_fld), \
100+
GENMASK(31, order_base_2(scale)), scale, \
101+
page_offset_quantized)
102+
72103
enum {
73104
MLX5_IB_MMAP_OFFSET_START = 9,
74105
MLX5_IB_MMAP_OFFSET_END = 255,

‎drivers/infiniband/hw/mlx5/srq.c‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ static int create_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq,
5151
udata, struct mlx5_ib_ucontext, ibucontext);
5252
size_t ucmdlen;
5353
int err;
54-
int page_shift;
55-
u32 offset;
54+
unsigned int page_offset_quantized;
55+
unsigned int page_size;
5656
u32 uidx = MLX5_IB_DEFAULT_UIDX;
5757

5858
ucmdlen = min(udata->inlen, sizeof(ucmd));
@@ -85,31 +85,31 @@ static int create_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq,
8585
return err;
8686
}
8787

88-
mlx5_ib_cont_pages(srq->umem, ucmd.buf_addr, 0, &page_shift);
89-
err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift,
90-
&offset);
91-
if (err) {
88+
page_size = mlx5_umem_find_best_quantized_pgoff(
89+
srq->umem, srqc, log_page_size, MLX5_ADAPTER_PAGE_SHIFT,
90+
page_offset, 64, &page_offset_quantized);
91+
if (!page_size) {
9292
mlx5_ib_warn(dev, "bad offset\n");
9393
goto err_umem;
9494
}
9595

96-
in->pas = kvcalloc(ib_umem_num_dma_blocks(srq->umem, 1UL << page_shift),
96+
in->pas = kvcalloc(ib_umem_num_dma_blocks(srq->umem, page_size),
9797
sizeof(*in->pas), GFP_KERNEL);
9898
if (!in->pas) {
9999
err = -ENOMEM;
100100
goto err_umem;
101101
}
102102

103-
mlx5_ib_populate_pas(srq->umem, 1UL << page_shift, in->pas, 0);
103+
mlx5_ib_populate_pas(srq->umem, page_size, in->pas, 0);
104104

105105
err = mlx5_ib_db_map_user(ucontext, udata, ucmd.db_addr, &srq->db);
106106
if (err) {
107107
mlx5_ib_dbg(dev, "map doorbell failed\n");
108108
goto err_in;
109109
}
110110

111-
in->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
112-
in->page_offset = offset;
111+
in->log_page_size = order_base_2(page_size) - MLX5_ADAPTER_PAGE_SHIFT;
112+
in->page_offset = page_offset_quantized;
113113
in->uid = (in->type != IB_SRQT_XRC) ? to_mpd(pd)->uid : 0;
114114
if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 &&
115115
in->type != IB_SRQT_BASIC)

‎include/rdma/ib_umem.h‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ static inline int ib_umem_offset(struct ib_umem *umem)
3434
return umem->address & ~PAGE_MASK;
3535
}
3636

37+
static inline unsigned long ib_umem_dma_offset(struct ib_umem *umem,
38+
unsigned long pgsz)
39+
{
40+
return (sg_dma_address(umem->sg_head.sgl) + ib_umem_offset(umem)) &
41+
(pgsz - 1);
42+
}
43+
3744
static inline size_t ib_umem_num_dma_blocks(struct ib_umem *umem,
3845
unsigned long pgsz)
3946
{
@@ -79,6 +86,35 @@ int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
7986
unsigned long ib_umem_find_best_pgsz(struct ib_umem *umem,
8087
unsigned long pgsz_bitmap,
8188
unsigned long virt);
89+
/**
90+
* ib_umem_find_best_pgoff - Find best HW page size
91+
*
92+
* @umem: umem struct
93+
* @pgsz_bitmap bitmap of HW supported page sizes
94+
* @pgoff_bitmask: Mask of bits that can be represented with an offset
95+
*
96+
* This is very similar to ib_umem_find_best_pgsz() except instead of accepting
97+
* an IOVA it accepts a bitmask specifying what address bits can be represented
98+
* with a page offset.
99+
*
100+
* For instance if the HW has multiple page sizes, requires 64 byte alignemnt,
101+
* and can support aligned offsets up to 4032 then pgoff_bitmask would be
102+
* "111111000000".
103+
*
104+
* If the pgoff_bitmask requires either alignment in the low bit or an
105+
* unavailable page size for the high bits, this function returns 0.
106+
*/
107+
static inline unsigned long ib_umem_find_best_pgoff(struct ib_umem *umem,
108+
unsigned long pgsz_bitmap,
109+
u64 pgoff_bitmask)
110+
{
111+
struct scatterlist *sg = umem->sg_head.sgl;
112+
dma_addr_t dma_addr;
113+
114+
dma_addr = sg_dma_address(sg) + (umem->address & ~PAGE_MASK);
115+
return ib_umem_find_best_pgsz(umem, pgsz_bitmap,
116+
dma_addr & pgoff_bitmask);
117+
}
82118

83119
#else /* CONFIG_INFINIBAND_USER_MEM */
84120

@@ -101,6 +137,12 @@ static inline unsigned long ib_umem_find_best_pgsz(struct ib_umem *umem,
101137
{
102138
return 0;
103139
}
140+
static inline unsigned long ib_umem_find_best_pgoff(struct ib_umem *umem,
141+
unsigned long pgsz_bitmap,
142+
u64 pgoff_bitmask)
143+
{
144+
return 0;
145+
}
104146

105147
#endif /* CONFIG_INFINIBAND_USER_MEM */
106148

0 commit comments

Comments
 (0)