Skip to content

Commit

Permalink
libhns: Add support for lock-free SRQ
Browse files Browse the repository at this point in the history
Drop SRQ locks when associated to a PAD holding a TD.

Signed-off-by: Junxian Huang <huangjunxian6@hisilicon.com>
  • Loading branch information
Junxian Huang authored and Junxian Huang committed Jul 5, 2024
1 parent 179f015 commit b38bae4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion providers/hns/hns_roce_u.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ struct hns_roce_srq {
struct verbs_srq verbs_srq;
struct hns_roce_idx_que idx_que;
struct hns_roce_buf wqe_buf;
pthread_spinlock_t lock;
struct hns_roce_spinlock hr_lock;
unsigned long *wrid;
unsigned int srqn;
unsigned int wqe_cnt;
Expand Down
8 changes: 4 additions & 4 deletions providers/hns/hns_roce_u_hw_v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ static void hns_roce_free_srq_wqe(struct hns_roce_srq *srq, uint16_t ind)
uint32_t bitmap_num;
int bit_num;

pthread_spin_lock(&srq->lock);
hns_roce_spin_lock(&srq->hr_lock);

bitmap_num = ind / BIT_CNT_PER_LONG;
bit_num = ind % BIT_CNT_PER_LONG;
srq->idx_que.bitmap[bitmap_num] |= (1ULL << bit_num);
srq->idx_que.tail++;

pthread_spin_unlock(&srq->lock);
hns_roce_spin_unlock(&srq->hr_lock);
}

static int get_srq_from_cqe(struct hns_roce_v2_cqe *cqe,
Expand Down Expand Up @@ -1768,7 +1768,7 @@ static int hns_roce_u_v2_post_srq_recv(struct ibv_srq *ib_srq,
int ret = 0;
void *wqe;

pthread_spin_lock(&srq->lock);
hns_roce_spin_lock(&srq->hr_lock);

max_sge = srq->max_gs - srq->rsv_sge;
for (nreq = 0; wr; ++nreq, wr = wr->next) {
Expand Down Expand Up @@ -1805,7 +1805,7 @@ static int hns_roce_u_v2_post_srq_recv(struct ibv_srq *ib_srq,
update_srq_db(ctx, &srq_db, srq);
}

pthread_spin_unlock(&srq->lock);
hns_roce_spin_unlock(&srq->hr_lock);

return ret;
}
Expand Down
23 changes: 20 additions & 3 deletions providers/hns/hns_roce_u_verbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,14 @@ static int hns_roce_cq_spinlock_init(struct hns_roce_cq *cq,
return hns_roce_spinlock_init(&cq->hr_lock, need_lock);
}

static int hns_roce_srq_spinlock_init(struct hns_roce_srq *srq,
struct ibv_srq_init_attr_ex *attr)
{
bool need_lock = hns_roce_whether_need_lock(attr->pd);

return hns_roce_spinlock_init(&srq->hr_lock, need_lock);
}

static int hns_roce_alloc_cq_buf(struct hns_roce_cq *cq)
{
int buf_size = hr_hw_page_align(cq->cq_depth * cq->cqe_size);
Expand Down Expand Up @@ -804,6 +812,7 @@ static struct ibv_srq *create_srq(struct ibv_context *context,
struct ibv_srq_init_attr_ex *init_attr)
{
struct hns_roce_context *hr_ctx = to_hr_ctx(context);
struct hns_roce_pad *pad = to_hr_pad(init_attr->pd);
struct hns_roce_srq *srq;
int ret;

Expand All @@ -817,7 +826,10 @@ static struct ibv_srq *create_srq(struct ibv_context *context,
goto err;
}

if (pthread_spin_init(&srq->lock, PTHREAD_PROCESS_PRIVATE))
if (pad)
atomic_fetch_add(&pad->pd.refcount, 1);

if (hns_roce_srq_spinlock_init(srq, init_attr))
goto err_free_srq;

set_srq_param(context, srq, init_attr);
Expand Down Expand Up @@ -852,7 +864,7 @@ static struct ibv_srq *create_srq(struct ibv_context *context,
free_srq_buf(srq);

err_destroy_lock:
pthread_spin_destroy(&srq->lock);
hns_roce_spinlock_destroy(&srq->hr_lock);

err_free_srq:
free(srq);
Expand Down Expand Up @@ -918,6 +930,7 @@ int hns_roce_u_query_srq(struct ibv_srq *srq, struct ibv_srq_attr *srq_attr)
int hns_roce_u_destroy_srq(struct ibv_srq *ibv_srq)
{
struct hns_roce_context *ctx = to_hr_ctx(ibv_srq->context);
struct hns_roce_pad *pad = to_hr_pad(ibv_srq->pd);
struct hns_roce_srq *srq = to_hr_srq(ibv_srq);
int ret;

Expand All @@ -930,7 +943,11 @@ int hns_roce_u_destroy_srq(struct ibv_srq *ibv_srq)
hns_roce_free_db(ctx, srq->rdb, HNS_ROCE_SRQ_TYPE_DB);
free_srq_buf(srq);

pthread_spin_destroy(&srq->lock);
hns_roce_spinlock_destroy(&srq->hr_lock);

if (pad)
atomic_fetch_sub(&pad->pd.refcount, 1);

free(srq);

return 0;
Expand Down

0 comments on commit b38bae4

Please sign in to comment.