Skip to content

Commit 9a41e38

Browse files
willy@infradead.orgjgunthorpe
authored andcommitted
IB/mad: Use IDR for agent IDs
Allocate agent IDs from a global IDR instead of an atomic variable. This eliminates the possibility of reusing an ID which is already in use after 4 billion registrations. We limit the assigned ID to be less than 2^24 as the mlx4 driver uses the most significant byte of the agent ID to store the slave number. Users unlucky enough to see a collision between agent numbers and slave numbers see messages like: mlx4_ib: egress mad has non-null tid msb:1 class:4 slave:0 and the MAD layer stops working. We look up the agent under protection of the RCU lock, which means we have to free the agent using kfree_rcu, and only increment the reference counter if it is not 0. Signed-off-by: Matthew Wilcox <[email protected]> Reported-by: Hans Westgaard Ry <[email protected]> Acked-by: Jack Morgenstein <[email protected]> Tested-by: Jack Morgenstein <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 3c60e86 commit 9a41e38

File tree

2 files changed

+55
-35
lines changed

2 files changed

+55
-35
lines changed

drivers/infiniband/core/mad.c

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3939

4040
#include <linux/dma-mapping.h>
41+
#include <linux/idr.h>
4142
#include <linux/slab.h>
4243
#include <linux/module.h>
4344
#include <linux/security.h>
@@ -58,8 +59,13 @@ MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests
5859
module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
5960
MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
6061

62+
/*
63+
* The mlx4 driver uses the top byte to distinguish which virtual function
64+
* generated the MAD, so we must avoid using it.
65+
*/
66+
#define AGENT_ID_LIMIT (1 << 24)
67+
static DEFINE_IDR(ib_mad_clients);
6168
static struct list_head ib_mad_port_list;
62-
static atomic_t ib_mad_client_id = ATOMIC_INIT(0);
6369

6470
/* Port list lock */
6571
static DEFINE_SPINLOCK(ib_mad_port_list_lock);
@@ -377,13 +383,24 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
377383
goto error4;
378384
}
379385

380-
spin_lock_irq(&port_priv->reg_lock);
381-
mad_agent_priv->agent.hi_tid = atomic_inc_return(&ib_mad_client_id);
386+
idr_preload(GFP_KERNEL);
387+
idr_lock(&ib_mad_clients);
388+
ret2 = idr_alloc_cyclic(&ib_mad_clients, mad_agent_priv, 0,
389+
AGENT_ID_LIMIT, GFP_ATOMIC);
390+
idr_unlock(&ib_mad_clients);
391+
idr_preload_end();
392+
393+
if (ret2 < 0) {
394+
ret = ERR_PTR(ret2);
395+
goto error5;
396+
}
397+
mad_agent_priv->agent.hi_tid = ret2;
382398

383399
/*
384400
* Make sure MAD registration (if supplied)
385401
* is non overlapping with any existing ones
386402
*/
403+
spin_lock_irq(&port_priv->reg_lock);
387404
if (mad_reg_req) {
388405
mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
389406
if (!is_vendor_class(mgmt_class)) {
@@ -394,7 +411,7 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
394411
if (method) {
395412
if (method_in_use(&method,
396413
mad_reg_req))
397-
goto error5;
414+
goto error6;
398415
}
399416
}
400417
ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
@@ -410,24 +427,25 @@ struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
410427
if (is_vendor_method_in_use(
411428
vendor_class,
412429
mad_reg_req))
413-
goto error5;
430+
goto error6;
414431
}
415432
}
416433
ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
417434
}
418435
if (ret2) {
419436
ret = ERR_PTR(ret2);
420-
goto error5;
437+
goto error6;
421438
}
422439
}
423-
424-
/* Add mad agent into port's agent list */
425-
list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
426440
spin_unlock_irq(&port_priv->reg_lock);
427441

428442
return &mad_agent_priv->agent;
429-
error5:
443+
error6:
430444
spin_unlock_irq(&port_priv->reg_lock);
445+
idr_lock(&ib_mad_clients);
446+
idr_remove(&ib_mad_clients, mad_agent_priv->agent.hi_tid);
447+
idr_unlock(&ib_mad_clients);
448+
error5:
431449
ib_mad_agent_security_cleanup(&mad_agent_priv->agent);
432450
error4:
433451
kfree(reg_req);
@@ -589,8 +607,10 @@ static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
589607

590608
spin_lock_irq(&port_priv->reg_lock);
591609
remove_mad_reg_req(mad_agent_priv);
592-
list_del(&mad_agent_priv->agent_list);
593610
spin_unlock_irq(&port_priv->reg_lock);
611+
idr_lock(&ib_mad_clients);
612+
idr_remove(&ib_mad_clients, mad_agent_priv->agent.hi_tid);
613+
idr_unlock(&ib_mad_clients);
594614

595615
flush_workqueue(port_priv->wq);
596616
ib_cancel_rmpp_recvs(mad_agent_priv);
@@ -601,7 +621,7 @@ static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
601621
ib_mad_agent_security_cleanup(&mad_agent_priv->agent);
602622

603623
kfree(mad_agent_priv->reg_req);
604-
kfree(mad_agent_priv);
624+
kfree_rcu(mad_agent_priv, rcu);
605625
}
606626

607627
static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
@@ -1722,22 +1742,19 @@ find_mad_agent(struct ib_mad_port_private *port_priv,
17221742
struct ib_mad_agent_private *mad_agent = NULL;
17231743
unsigned long flags;
17241744

1725-
spin_lock_irqsave(&port_priv->reg_lock, flags);
17261745
if (ib_response_mad(mad_hdr)) {
17271746
u32 hi_tid;
1728-
struct ib_mad_agent_private *entry;
17291747

17301748
/*
17311749
* Routing is based on high 32 bits of transaction ID
17321750
* of MAD.
17331751
*/
17341752
hi_tid = be64_to_cpu(mad_hdr->tid) >> 32;
1735-
list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
1736-
if (entry->agent.hi_tid == hi_tid) {
1737-
mad_agent = entry;
1738-
break;
1739-
}
1740-
}
1753+
rcu_read_lock();
1754+
mad_agent = idr_find(&ib_mad_clients, hi_tid);
1755+
if (mad_agent && !atomic_inc_not_zero(&mad_agent->refcount))
1756+
mad_agent = NULL;
1757+
rcu_read_unlock();
17411758
} else {
17421759
struct ib_mad_mgmt_class_table *class;
17431760
struct ib_mad_mgmt_method_table *method;
@@ -1746,6 +1763,7 @@ find_mad_agent(struct ib_mad_port_private *port_priv,
17461763
const struct ib_vendor_mad *vendor_mad;
17471764
int index;
17481765

1766+
spin_lock_irqsave(&port_priv->reg_lock, flags);
17491767
/*
17501768
* Routing is based on version, class, and method
17511769
* For "newer" vendor MADs, also based on OUI
@@ -1785,20 +1803,19 @@ find_mad_agent(struct ib_mad_port_private *port_priv,
17851803
~IB_MGMT_METHOD_RESP];
17861804
}
17871805
}
1806+
if (mad_agent)
1807+
atomic_inc(&mad_agent->refcount);
1808+
out:
1809+
spin_unlock_irqrestore(&port_priv->reg_lock, flags);
17881810
}
17891811

1790-
if (mad_agent) {
1791-
if (mad_agent->agent.recv_handler)
1792-
atomic_inc(&mad_agent->refcount);
1793-
else {
1794-
dev_notice(&port_priv->device->dev,
1795-
"No receive handler for client %p on port %d\n",
1796-
&mad_agent->agent, port_priv->port_num);
1797-
mad_agent = NULL;
1798-
}
1812+
if (mad_agent && !mad_agent->agent.recv_handler) {
1813+
dev_notice(&port_priv->device->dev,
1814+
"No receive handler for client %p on port %d\n",
1815+
&mad_agent->agent, port_priv->port_num);
1816+
deref_mad_agent(mad_agent);
1817+
mad_agent = NULL;
17991818
}
1800-
out:
1801-
spin_unlock_irqrestore(&port_priv->reg_lock, flags);
18021819

18031820
return mad_agent;
18041821
}
@@ -3161,7 +3178,6 @@ static int ib_mad_port_open(struct ib_device *device,
31613178
port_priv->device = device;
31623179
port_priv->port_num = port_num;
31633180
spin_lock_init(&port_priv->reg_lock);
3164-
INIT_LIST_HEAD(&port_priv->agent_list);
31653181
init_mad_qp(port_priv, &port_priv->qp_info[0]);
31663182
init_mad_qp(port_priv, &port_priv->qp_info[1]);
31673183

@@ -3340,6 +3356,9 @@ int ib_mad_init(void)
33403356

33413357
INIT_LIST_HEAD(&ib_mad_port_list);
33423358

3359+
/* Client ID 0 is used for snoop-only clients */
3360+
idr_alloc(&ib_mad_clients, NULL, 0, 0, GFP_KERNEL);
3361+
33433362
if (ib_register_client(&mad_client)) {
33443363
pr_err("Couldn't register ib_mad client\n");
33453364
return -EINVAL;

drivers/infiniband/core/mad_priv.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ struct ib_rmpp_segment {
8989
};
9090

9191
struct ib_mad_agent_private {
92-
struct list_head agent_list;
9392
struct ib_mad_agent agent;
9493
struct ib_mad_reg_req *reg_req;
9594
struct ib_mad_qp_info *qp_info;
@@ -105,7 +104,10 @@ struct ib_mad_agent_private {
105104
struct list_head rmpp_list;
106105

107106
atomic_t refcount;
108-
struct completion comp;
107+
union {
108+
struct completion comp;
109+
struct rcu_head rcu;
110+
};
109111
};
110112

111113
struct ib_mad_snoop_private {
@@ -203,7 +205,6 @@ struct ib_mad_port_private {
203205

204206
spinlock_t reg_lock;
205207
struct ib_mad_mgmt_version_table version[MAX_MGMT_VERSION];
206-
struct list_head agent_list;
207208
struct workqueue_struct *wq;
208209
struct ib_mad_qp_info qp_info[IB_MAD_QPS_CORE];
209210
};

0 commit comments

Comments
 (0)