Skip to content

Commit c764f2d

Browse files
Hakon-Buggegregkh
authored andcommitted
RDMA/cma: Protect RMW with qp_mutex
[ Upstream commit ca0c448 ] The struct rdma_id_private contains three bit-fields, tos_set, timeout_set, and min_rnr_timer_set. These are set by accessor functions without any synchronization. If two or all accessor functions are invoked in close proximity in time, there will be Read-Modify-Write from several contexts to the same variable, and the result will be intermittent. Fixed by protecting the bit-fields by the qp_mutex in the accessor functions. The consumer of timeout_set and min_rnr_timer_set is in rdma_init_qp_attr(), which is called with qp_mutex held for connected QPs. Explicit locking is added for the consumers of tos and tos_set. This commit depends on ("RDMA/cma: Remove unnecessary INIT->INIT transition"), since the call to rdma_init_qp_attr() from cma_init_conn_qp() does not hold the qp_mutex. Fixes: 2c1619e ("IB/cma: Define option to set ack timeout and pack tos_set") Fixes: 3aeffc4 ("IB/cma: Introduce rdma_set_min_rnr_timer()") Link: https://lore.kernel.org/r/1624369197-24578-3-git-send-email-haakon.bugge@oracle.com Signed-off-by: Håkon Bugge <haakon.bugge@oracle.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent d52ceed commit c764f2d

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

  • drivers/infiniband/core

‎drivers/infiniband/core/cma.c‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2476,8 +2476,10 @@ static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
24762476
if (IS_ERR(id))
24772477
return PTR_ERR(id);
24782478

2479+
mutex_lock(&id_priv->qp_mutex);
24792480
id->tos = id_priv->tos;
24802481
id->tos_set = id_priv->tos_set;
2482+
mutex_unlock(&id_priv->qp_mutex);
24812483
id_priv->cm_id.iw = id;
24822484

24832485
memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
@@ -2537,8 +2539,10 @@ static int cma_listen_on_dev(struct rdma_id_private *id_priv,
25372539
cma_id_get(id_priv);
25382540
dev_id_priv->internal_id = 1;
25392541
dev_id_priv->afonly = id_priv->afonly;
2542+
mutex_lock(&id_priv->qp_mutex);
25402543
dev_id_priv->tos_set = id_priv->tos_set;
25412544
dev_id_priv->tos = id_priv->tos;
2545+
mutex_unlock(&id_priv->qp_mutex);
25422546

25432547
ret = rdma_listen(&dev_id_priv->id, id_priv->backlog);
25442548
if (ret)
@@ -2585,8 +2589,10 @@ void rdma_set_service_type(struct rdma_cm_id *id, int tos)
25852589
struct rdma_id_private *id_priv;
25862590

25872591
id_priv = container_of(id, struct rdma_id_private, id);
2592+
mutex_lock(&id_priv->qp_mutex);
25882593
id_priv->tos = (u8) tos;
25892594
id_priv->tos_set = true;
2595+
mutex_unlock(&id_priv->qp_mutex);
25902596
}
25912597
EXPORT_SYMBOL(rdma_set_service_type);
25922598

@@ -2613,8 +2619,10 @@ int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout)
26132619
return -EINVAL;
26142620

26152621
id_priv = container_of(id, struct rdma_id_private, id);
2622+
mutex_lock(&id_priv->qp_mutex);
26162623
id_priv->timeout = timeout;
26172624
id_priv->timeout_set = true;
2625+
mutex_unlock(&id_priv->qp_mutex);
26182626

26192627
return 0;
26202628
}
@@ -3000,8 +3008,11 @@ static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
30003008

30013009
u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num -
30023010
rdma_start_port(id_priv->cma_dev->device)];
3003-
u8 tos = id_priv->tos_set ? id_priv->tos : default_roce_tos;
3011+
u8 tos;
30043012

3013+
mutex_lock(&id_priv->qp_mutex);
3014+
tos = id_priv->tos_set ? id_priv->tos : default_roce_tos;
3015+
mutex_unlock(&id_priv->qp_mutex);
30053016

30063017
work = kzalloc(sizeof *work, GFP_KERNEL);
30073018
if (!work)
@@ -3048,8 +3059,10 @@ static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
30483059
* PacketLifeTime = local ACK timeout/2
30493060
* as a reasonable approximation for RoCE networks.
30503061
*/
3062+
mutex_lock(&id_priv->qp_mutex);
30513063
route->path_rec->packet_life_time = id_priv->timeout_set ?
30523064
id_priv->timeout - 1 : CMA_IBOE_PACKET_LIFETIME;
3065+
mutex_unlock(&id_priv->qp_mutex);
30533066

30543067
if (!route->path_rec->mtu) {
30553068
ret = -EINVAL;
@@ -4073,8 +4086,11 @@ static int cma_connect_iw(struct rdma_id_private *id_priv,
40734086
if (IS_ERR(cm_id))
40744087
return PTR_ERR(cm_id);
40754088

4089+
mutex_lock(&id_priv->qp_mutex);
40764090
cm_id->tos = id_priv->tos;
40774091
cm_id->tos_set = id_priv->tos_set;
4092+
mutex_unlock(&id_priv->qp_mutex);
4093+
40784094
id_priv->cm_id.iw = cm_id;
40794095

40804096
memcpy(&cm_id->local_addr, cma_src_addr(id_priv),

0 commit comments

Comments
 (0)