Skip to content

Commit 732d41c

Browse files
committed
RDMA/cma: Make the locking for automatic state transition more clear
Re-organize things so the state variable is not read unlocked. The first attempt to go directly from ADDR_BOUND immediately tells us if the ID is already bound, if we can't do that then the attempt inside rdma_bind_addr() to go from IDLE to ADDR_BOUND confirms the ID needs binding. Link: https://lore.kernel.org/r/20200902081122.745412-3-leon@kernel.org Signed-off-by: Leon Romanovsky <leonro@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 2a7cec5 commit 732d41c

1 file changed

Lines changed: 45 additions & 22 deletions

File tree

  • drivers/infiniband/core

‎drivers/infiniband/core/cma.c‎

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,32 +3248,54 @@ static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
32483248
return rdma_bind_addr(id, src_addr);
32493249
}
32503250

3251-
int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3252-
const struct sockaddr *dst_addr, unsigned long timeout_ms)
3251+
/*
3252+
* If required, resolve the source address for bind and leave the id_priv in
3253+
* state RDMA_CM_ADDR_BOUND. This oddly uses the state to determine the prior
3254+
* calls made by ULP, a previously bound ID will not be re-bound and src_addr is
3255+
* ignored.
3256+
*/
3257+
static int resolve_prepare_src(struct rdma_id_private *id_priv,
3258+
struct sockaddr *src_addr,
3259+
const struct sockaddr *dst_addr)
32533260
{
3254-
struct rdma_id_private *id_priv;
32553261
int ret;
32563262

3257-
id_priv = container_of(id, struct rdma_id_private, id);
32583263
memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
3259-
if (id_priv->state == RDMA_CM_IDLE) {
3260-
ret = cma_bind_addr(id, src_addr, dst_addr);
3261-
if (ret) {
3262-
memset(cma_dst_addr(id_priv), 0,
3263-
rdma_addr_size(dst_addr));
3264-
return ret;
3264+
if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) {
3265+
/* For a well behaved ULP state will be RDMA_CM_IDLE */
3266+
ret = cma_bind_addr(&id_priv->id, src_addr, dst_addr);
3267+
if (ret)
3268+
goto err_dst;
3269+
if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3270+
RDMA_CM_ADDR_QUERY))) {
3271+
ret = -EINVAL;
3272+
goto err_dst;
32653273
}
32663274
}
32673275

32683276
if (cma_family(id_priv) != dst_addr->sa_family) {
3269-
memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
3270-
return -EINVAL;
3277+
ret = -EINVAL;
3278+
goto err_state;
32713279
}
3280+
return 0;
32723281

3273-
if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) {
3274-
memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
3275-
return -EINVAL;
3276-
}
3282+
err_state:
3283+
cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3284+
err_dst:
3285+
memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
3286+
return ret;
3287+
}
3288+
3289+
int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3290+
const struct sockaddr *dst_addr, unsigned long timeout_ms)
3291+
{
3292+
struct rdma_id_private *id_priv =
3293+
container_of(id, struct rdma_id_private, id);
3294+
int ret;
3295+
3296+
ret = resolve_prepare_src(id_priv, src_addr, dst_addr);
3297+
if (ret)
3298+
return ret;
32773299

32783300
if (cma_any_addr(dst_addr)) {
32793301
ret = cma_resolve_loopback(id_priv);
@@ -3646,20 +3668,21 @@ static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
36463668

36473669
int rdma_listen(struct rdma_cm_id *id, int backlog)
36483670
{
3649-
struct rdma_id_private *id_priv;
3671+
struct rdma_id_private *id_priv =
3672+
container_of(id, struct rdma_id_private, id);
36503673
int ret;
36513674

3652-
id_priv = container_of(id, struct rdma_id_private, id);
3653-
if (id_priv->state == RDMA_CM_IDLE) {
3675+
if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) {
3676+
/* For a well behaved ULP state will be RDMA_CM_IDLE */
36543677
id->route.addr.src_addr.ss_family = AF_INET;
36553678
ret = rdma_bind_addr(id, cma_src_addr(id_priv));
36563679
if (ret)
36573680
return ret;
3681+
if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3682+
RDMA_CM_LISTEN)))
3683+
return -EINVAL;
36583684
}
36593685

3660-
if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
3661-
return -EINVAL;
3662-
36633686
if (id_priv->reuseaddr) {
36643687
ret = cma_bind_listen(id_priv);
36653688
if (ret)

0 commit comments

Comments
 (0)