Skip to content

Commit fffb038

Browse files
Christoph Hellwigdledford
authored andcommitted
IB/core: add a simple MR pool
Signed-off-by: Christoph Hellwig <[email protected]> Tested-by: Steve Wise <[email protected]> Reviewed-by: Bart Van Assche <[email protected]> Reviewed-by: Sagi Grimberg <[email protected]> Reviewed-by: Steve Wise <[email protected]> Signed-off-by: Doug Ledford <[email protected]>
1 parent 04c41bf commit fffb038

File tree

5 files changed

+124
-2
lines changed

5 files changed

+124
-2
lines changed

drivers/infiniband/core/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ obj-$(CONFIG_INFINIBAND_USER_ACCESS) += ib_uverbs.o ib_ucm.o \
1010

1111
ib_core-y := packer.o ud_header.o verbs.o cq.o sysfs.o \
1212
device.o fmr_pool.o cache.o netlink.o \
13-
roce_gid_mgmt.o
13+
roce_gid_mgmt.o mr_pool.o
1414
ib_core-$(CONFIG_INFINIBAND_USER_MEM) += umem.o
1515
ib_core-$(CONFIG_INFINIBAND_ON_DEMAND_PAGING) += umem_odp.o umem_rbtree.o
1616

drivers/infiniband/core/mr_pool.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2016 HGST, a Western Digital Company.
3+
*
4+
* This program is free software; you can redistribute it and/or modify it
5+
* under the terms and conditions of the GNU General Public License,
6+
* version 2, as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope it will be useful, but WITHOUT
9+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11+
* more details.
12+
*/
13+
#include <rdma/ib_verbs.h>
14+
#include <rdma/mr_pool.h>
15+
16+
struct ib_mr *ib_mr_pool_get(struct ib_qp *qp, struct list_head *list)
17+
{
18+
struct ib_mr *mr;
19+
unsigned long flags;
20+
21+
spin_lock_irqsave(&qp->mr_lock, flags);
22+
mr = list_first_entry_or_null(list, struct ib_mr, qp_entry);
23+
if (mr) {
24+
list_del(&mr->qp_entry);
25+
qp->mrs_used++;
26+
}
27+
spin_unlock_irqrestore(&qp->mr_lock, flags);
28+
29+
return mr;
30+
}
31+
EXPORT_SYMBOL(ib_mr_pool_get);
32+
33+
void ib_mr_pool_put(struct ib_qp *qp, struct list_head *list, struct ib_mr *mr)
34+
{
35+
unsigned long flags;
36+
37+
spin_lock_irqsave(&qp->mr_lock, flags);
38+
list_add(&mr->qp_entry, list);
39+
qp->mrs_used--;
40+
spin_unlock_irqrestore(&qp->mr_lock, flags);
41+
}
42+
EXPORT_SYMBOL(ib_mr_pool_put);
43+
44+
int ib_mr_pool_init(struct ib_qp *qp, struct list_head *list, int nr,
45+
enum ib_mr_type type, u32 max_num_sg)
46+
{
47+
struct ib_mr *mr;
48+
unsigned long flags;
49+
int ret, i;
50+
51+
for (i = 0; i < nr; i++) {
52+
mr = ib_alloc_mr(qp->pd, type, max_num_sg);
53+
if (IS_ERR(mr)) {
54+
ret = PTR_ERR(mr);
55+
goto out;
56+
}
57+
58+
spin_lock_irqsave(&qp->mr_lock, flags);
59+
list_add_tail(&mr->qp_entry, list);
60+
spin_unlock_irqrestore(&qp->mr_lock, flags);
61+
}
62+
63+
return 0;
64+
out:
65+
ib_mr_pool_destroy(qp, list);
66+
return ret;
67+
}
68+
EXPORT_SYMBOL(ib_mr_pool_init);
69+
70+
void ib_mr_pool_destroy(struct ib_qp *qp, struct list_head *list)
71+
{
72+
struct ib_mr *mr;
73+
unsigned long flags;
74+
75+
spin_lock_irqsave(&qp->mr_lock, flags);
76+
while (!list_empty(list)) {
77+
mr = list_first_entry(list, struct ib_mr, qp_entry);
78+
list_del(&mr->qp_entry);
79+
80+
spin_unlock_irqrestore(&qp->mr_lock, flags);
81+
ib_dereg_mr(mr);
82+
spin_lock_irqsave(&qp->mr_lock, flags);
83+
}
84+
spin_unlock_irqrestore(&qp->mr_lock, flags);
85+
}
86+
EXPORT_SYMBOL(ib_mr_pool_destroy);

drivers/infiniband/core/verbs.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,9 @@ struct ib_qp *ib_create_qp(struct ib_pd *pd,
762762
qp->qp_type = qp_init_attr->qp_type;
763763

764764
atomic_set(&qp->usecnt, 0);
765+
qp->mrs_used = 0;
766+
spin_lock_init(&qp->mr_lock);
767+
765768
if (qp_init_attr->qp_type == IB_QPT_XRC_TGT)
766769
return ib_create_xrc_qp(qp, qp_init_attr);
767770

@@ -1255,6 +1258,8 @@ int ib_destroy_qp(struct ib_qp *qp)
12551258
struct ib_srq *srq;
12561259
int ret;
12571260

1261+
WARN_ON_ONCE(qp->mrs_used > 0);
1262+
12581263
if (atomic_read(&qp->usecnt))
12591264
return -EBUSY;
12601265

include/rdma/ib_verbs.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,9 +1421,12 @@ struct ib_qp {
14211421
struct ib_pd *pd;
14221422
struct ib_cq *send_cq;
14231423
struct ib_cq *recv_cq;
1424+
spinlock_t mr_lock;
1425+
int mrs_used;
14241426
struct ib_srq *srq;
14251427
struct ib_xrcd *xrcd; /* XRC TGT QPs only */
14261428
struct list_head xrcd_list;
1429+
14271430
/* count times opened, mcast attaches, flow attaches */
14281431
atomic_t usecnt;
14291432
struct list_head open_list;
@@ -1438,12 +1441,15 @@ struct ib_qp {
14381441
struct ib_mr {
14391442
struct ib_device *device;
14401443
struct ib_pd *pd;
1441-
struct ib_uobject *uobject;
14421444
u32 lkey;
14431445
u32 rkey;
14441446
u64 iova;
14451447
u32 length;
14461448
unsigned int page_size;
1449+
union {
1450+
struct ib_uobject *uobject; /* user */
1451+
struct list_head qp_entry; /* FR */
1452+
};
14471453
};
14481454

14491455
struct ib_mw {

include/rdma/mr_pool.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2016 HGST, a Western Digital Company.
3+
*
4+
* This program is free software; you can redistribute it and/or modify it
5+
* under the terms and conditions of the GNU General Public License,
6+
* version 2, as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope it will be useful, but WITHOUT
9+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11+
* more details.
12+
*/
13+
#ifndef _RDMA_MR_POOL_H
14+
#define _RDMA_MR_POOL_H 1
15+
16+
#include <rdma/ib_verbs.h>
17+
18+
struct ib_mr *ib_mr_pool_get(struct ib_qp *qp, struct list_head *list);
19+
void ib_mr_pool_put(struct ib_qp *qp, struct list_head *list, struct ib_mr *mr);
20+
21+
int ib_mr_pool_init(struct ib_qp *qp, struct list_head *list, int nr,
22+
enum ib_mr_type type, u32 max_num_sg);
23+
void ib_mr_pool_destroy(struct ib_qp *qp, struct list_head *list);
24+
25+
#endif /* _RDMA_MR_POOL_H */

0 commit comments

Comments
 (0)