Skip to content

Commit 8bc205e

Browse files
rleonjgunthorpe
authored andcommitted
RDMA/counter: Combine allocation and bind logic
RDMA counters are allocated and bounded to QP immediately after that. Only after this two step process they are really usable. By combining the logic, we are ensuring that once counter is returned to the caller, it will have everything set. Link: https://lore.kernel.org/r/20201104144008.3808124-3-leon@kernel.org Signed-off-by: Leon Romanovsky <leonro@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 7af80c0 commit 8bc205e

1 file changed

Lines changed: 62 additions & 74 deletions

File tree

‎drivers/infiniband/core/counters.c‎

Lines changed: 62 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,40 @@ int rdma_counter_set_auto_mode(struct ib_device *dev, u8 port,
6464
return ret;
6565
}
6666

67-
static struct rdma_counter *rdma_counter_alloc(struct ib_device *dev, u8 port,
68-
enum rdma_nl_counter_mode mode)
67+
static void auto_mode_init_counter(struct rdma_counter *counter,
68+
const struct ib_qp *qp,
69+
enum rdma_nl_counter_mask new_mask)
70+
{
71+
struct auto_mode_param *param = &counter->mode.param;
72+
73+
counter->mode.mode = RDMA_COUNTER_MODE_AUTO;
74+
counter->mode.mask = new_mask;
75+
76+
if (new_mask & RDMA_COUNTER_MASK_QP_TYPE)
77+
param->qp_type = qp->qp_type;
78+
}
79+
80+
static int __rdma_counter_bind_qp(struct rdma_counter *counter,
81+
struct ib_qp *qp)
82+
{
83+
int ret;
84+
85+
if (qp->counter)
86+
return -EINVAL;
87+
88+
if (!qp->device->ops.counter_bind_qp)
89+
return -EOPNOTSUPP;
90+
91+
mutex_lock(&counter->lock);
92+
ret = qp->device->ops.counter_bind_qp(counter, qp);
93+
mutex_unlock(&counter->lock);
94+
95+
return ret;
96+
}
97+
98+
static struct rdma_counter *alloc_and_bind(struct ib_device *dev, u8 port,
99+
struct ib_qp *qp,
100+
enum rdma_nl_counter_mode mode)
69101
{
70102
struct rdma_port_counter *port_counter;
71103
struct rdma_counter *counter;
@@ -88,11 +120,22 @@ static struct rdma_counter *rdma_counter_alloc(struct ib_device *dev, u8 port,
88120

89121
port_counter = &dev->port_data[port].port_counter;
90122
mutex_lock(&port_counter->lock);
91-
if (mode == RDMA_COUNTER_MODE_MANUAL) {
123+
switch (mode) {
124+
case RDMA_COUNTER_MODE_MANUAL:
92125
ret = __counter_set_mode(&port_counter->mode,
93126
RDMA_COUNTER_MODE_MANUAL, 0);
94-
if (ret)
127+
if (ret) {
128+
mutex_unlock(&port_counter->lock);
95129
goto err_mode;
130+
}
131+
break;
132+
case RDMA_COUNTER_MODE_AUTO:
133+
auto_mode_init_counter(counter, qp, port_counter->mode.mask);
134+
break;
135+
default:
136+
ret = -EOPNOTSUPP;
137+
mutex_unlock(&port_counter->lock);
138+
goto err_mode;
96139
}
97140

98141
port_counter->num_counters++;
@@ -102,10 +145,15 @@ static struct rdma_counter *rdma_counter_alloc(struct ib_device *dev, u8 port,
102145
kref_init(&counter->kref);
103146
mutex_init(&counter->lock);
104147

148+
ret = __rdma_counter_bind_qp(counter, qp);
149+
if (ret)
150+
goto err_mode;
151+
152+
rdma_restrack_parent_name(&counter->res, &qp->res);
153+
rdma_restrack_add(&counter->res);
105154
return counter;
106155

107156
err_mode:
108-
mutex_unlock(&port_counter->lock);
109157
kfree(counter->stats);
110158
err_stats:
111159
rdma_restrack_put(&counter->res);
@@ -132,19 +180,6 @@ static void rdma_counter_free(struct rdma_counter *counter)
132180
kfree(counter);
133181
}
134182

135-
static void auto_mode_init_counter(struct rdma_counter *counter,
136-
const struct ib_qp *qp,
137-
enum rdma_nl_counter_mask new_mask)
138-
{
139-
struct auto_mode_param *param = &counter->mode.param;
140-
141-
counter->mode.mode = RDMA_COUNTER_MODE_AUTO;
142-
counter->mode.mask = new_mask;
143-
144-
if (new_mask & RDMA_COUNTER_MASK_QP_TYPE)
145-
param->qp_type = qp->qp_type;
146-
}
147-
148183
static bool auto_mode_match(struct ib_qp *qp, struct rdma_counter *counter,
149184
enum rdma_nl_counter_mask auto_mask)
150185
{
@@ -161,24 +196,6 @@ static bool auto_mode_match(struct ib_qp *qp, struct rdma_counter *counter,
161196
return match;
162197
}
163198

164-
static int __rdma_counter_bind_qp(struct rdma_counter *counter,
165-
struct ib_qp *qp)
166-
{
167-
int ret;
168-
169-
if (qp->counter)
170-
return -EINVAL;
171-
172-
if (!qp->device->ops.counter_bind_qp)
173-
return -EOPNOTSUPP;
174-
175-
mutex_lock(&counter->lock);
176-
ret = qp->device->ops.counter_bind_qp(counter, qp);
177-
mutex_unlock(&counter->lock);
178-
179-
return ret;
180-
}
181-
182199
static int __rdma_counter_unbind_qp(struct ib_qp *qp)
183200
{
184201
struct rdma_counter *counter = qp->counter;
@@ -247,13 +264,6 @@ static struct rdma_counter *rdma_get_counter_auto_mode(struct ib_qp *qp,
247264
return counter;
248265
}
249266

250-
static void rdma_counter_res_add(struct rdma_counter *counter,
251-
struct ib_qp *qp)
252-
{
253-
rdma_restrack_parent_name(&counter->res, &qp->res);
254-
rdma_restrack_add(&counter->res);
255-
}
256-
257267
static void counter_release(struct kref *kref)
258268
{
259269
struct rdma_counter *counter;
@@ -293,19 +303,9 @@ int rdma_counter_bind_qp_auto(struct ib_qp *qp, u8 port)
293303
return ret;
294304
}
295305
} else {
296-
counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_AUTO);
306+
counter = alloc_and_bind(dev, port, qp, RDMA_COUNTER_MODE_AUTO);
297307
if (!counter)
298308
return -ENOMEM;
299-
300-
auto_mode_init_counter(counter, qp, port_counter->mode.mask);
301-
302-
ret = __rdma_counter_bind_qp(counter, qp);
303-
if (ret) {
304-
rdma_counter_free(counter);
305-
return ret;
306-
}
307-
308-
rdma_counter_res_add(counter, qp);
309309
}
310310

311311
return 0;
@@ -419,15 +419,6 @@ static struct ib_qp *rdma_counter_get_qp(struct ib_device *dev, u32 qp_num)
419419
return NULL;
420420
}
421421

422-
static int rdma_counter_bind_qp_manual(struct rdma_counter *counter,
423-
struct ib_qp *qp)
424-
{
425-
if ((counter->device != qp->device) || (counter->port != qp->port))
426-
return -EINVAL;
427-
428-
return __rdma_counter_bind_qp(counter, qp);
429-
}
430-
431422
static struct rdma_counter *rdma_get_counter_by_id(struct ib_device *dev,
432423
u32 counter_id)
433424
{
@@ -475,7 +466,12 @@ int rdma_counter_bind_qpn(struct ib_device *dev, u8 port,
475466
goto err_task;
476467
}
477468

478-
ret = rdma_counter_bind_qp_manual(counter, qp);
469+
if ((counter->device != qp->device) || (counter->port != qp->port)) {
470+
ret = -EINVAL;
471+
goto err_task;
472+
}
473+
474+
ret = __rdma_counter_bind_qp(counter, qp);
479475
if (ret)
480476
goto err_task;
481477

@@ -520,26 +516,18 @@ int rdma_counter_bind_qpn_alloc(struct ib_device *dev, u8 port,
520516
goto err;
521517
}
522518

523-
counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_MANUAL);
519+
counter = alloc_and_bind(dev, port, qp, RDMA_COUNTER_MODE_MANUAL);
524520
if (!counter) {
525521
ret = -ENOMEM;
526522
goto err;
527523
}
528524

529-
ret = rdma_counter_bind_qp_manual(counter, qp);
530-
if (ret)
531-
goto err_bind;
532-
533525
if (counter_id)
534526
*counter_id = counter->id;
535527

536-
rdma_counter_res_add(counter, qp);
537-
538528
rdma_restrack_put(&qp->res);
539-
return ret;
529+
return 0;
540530

541-
err_bind:
542-
rdma_counter_free(counter);
543531
err:
544532
rdma_restrack_put(&qp->res);
545533
return ret;

0 commit comments

Comments
 (0)