Skip to content

Commit ea1075e

Browse files
committed
RDMA: Add and use rdma_for_each_port
We have many loops iterating over all of the end port numbers on a struct ib_device, simplify them with a for_each helper. Reviewed-by: Parav Pandit <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent f2a0e45 commit ea1075e

File tree

12 files changed

+53
-46
lines changed

12 files changed

+53
-46
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ ForEachMacros:
361361
- 'radix_tree_for_each_slot'
362362
- 'radix_tree_for_each_tagged'
363363
- 'rbtree_postorder_for_each_entry_safe'
364+
- 'rdma_for_each_port'
364365
- 'resource_list_for_each_entry'
365366
- 'resource_list_for_each_entry_safe'
366367
- 'rhl_for_each_entry_rcu'

drivers/infiniband/core/cache.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ static void ib_cache_event(struct ib_event_handler *handler,
14281428

14291429
int ib_cache_setup_one(struct ib_device *device)
14301430
{
1431-
int p;
1431+
unsigned int p;
14321432
int err;
14331433

14341434
rwlock_init(&device->cache.lock);
@@ -1447,8 +1447,8 @@ int ib_cache_setup_one(struct ib_device *device)
14471447
return err;
14481448
}
14491449

1450-
for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1451-
ib_cache_update(device, p + rdma_start_port(device), true);
1450+
rdma_for_each_port (device, p)
1451+
ib_cache_update(device, p, true);
14521452

14531453
INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
14541454
device, ib_cache_event);

drivers/infiniband/core/cma.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ static int cma_acquire_dev_by_src_ip(struct rdma_id_private *id_priv)
659659
struct cma_device *cma_dev;
660660
enum ib_gid_type gid_type;
661661
int ret = -ENODEV;
662-
u8 port;
662+
unsigned int port;
663663

664664
if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
665665
id_priv->id.ps == RDMA_PS_IPOIB)
@@ -673,8 +673,7 @@ static int cma_acquire_dev_by_src_ip(struct rdma_id_private *id_priv)
673673

674674
mutex_lock(&lock);
675675
list_for_each_entry(cma_dev, &dev_list, list) {
676-
for (port = rdma_start_port(cma_dev->device);
677-
port <= rdma_end_port(cma_dev->device); port++) {
676+
rdma_for_each_port (cma_dev->device, port) {
678677
gidp = rdma_protocol_roce(cma_dev->device, port) ?
679678
&iboe_gid : &gid;
680679
gid_type = cma_dev->default_gid_type[port - 1];
@@ -4548,7 +4547,7 @@ static void cma_add_one(struct ib_device *device)
45484547
if (!cma_dev->default_roce_tos)
45494548
goto free_gid_type;
45504549

4551-
for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
4550+
rdma_for_each_port (device, i) {
45524551
supported_gids = roce_gid_type_mask_support(device, i);
45534552
WARN_ON(!supported_gids);
45544553
if (supported_gids & (1 << CMA_PREFERRED_ROCE_GID_TYPE))

drivers/infiniband/core/device.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,8 @@ static int verify_immutable(const struct ib_device *dev, u8 port)
470470

471471
static int read_port_immutable(struct ib_device *device)
472472
{
473+
unsigned int port;
473474
int ret;
474-
u8 start_port = rdma_start_port(device);
475-
u8 end_port = rdma_end_port(device);
476-
u8 port;
477475

478476
/**
479477
* device->port_immutable is indexed directly by the port number to make
@@ -482,13 +480,13 @@ static int read_port_immutable(struct ib_device *device)
482480
* Therefore port_immutable is declared as a 1 based array with
483481
* potential empty slots at the beginning.
484482
*/
485-
device->port_immutable = kcalloc(end_port + 1,
486-
sizeof(*device->port_immutable),
487-
GFP_KERNEL);
483+
device->port_immutable =
484+
kcalloc(rdma_end_port(device) + 1,
485+
sizeof(*device->port_immutable), GFP_KERNEL);
488486
if (!device->port_immutable)
489487
return -ENOMEM;
490488

491-
for (port = start_port; port <= end_port; ++port) {
489+
rdma_for_each_port (device, port) {
492490
ret = device->ops.get_port_immutable(
493491
device, port, &device->port_immutable[port]);
494492
if (ret)
@@ -540,9 +538,9 @@ static void ib_policy_change_task(struct work_struct *work)
540538

541539
down_read(&devices_rwsem);
542540
xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
543-
int i;
541+
unsigned int i;
544542

545-
for (i = rdma_start_port(dev); i <= rdma_end_port(dev); i++) {
543+
rdma_for_each_port (dev, i) {
546544
u64 sp;
547545
int ret = ib_get_cached_subnet_prefix(dev,
548546
i,
@@ -1060,10 +1058,9 @@ void ib_enum_roce_netdev(struct ib_device *ib_dev,
10601058
roce_netdev_callback cb,
10611059
void *cookie)
10621060
{
1063-
u8 port;
1061+
unsigned int port;
10641062

1065-
for (port = rdma_start_port(ib_dev); port <= rdma_end_port(ib_dev);
1066-
port++)
1063+
rdma_for_each_port (ib_dev, port)
10671064
if (rdma_protocol_roce(ib_dev, port)) {
10681065
struct net_device *idev = NULL;
10691066

@@ -1217,9 +1214,10 @@ int ib_find_gid(struct ib_device *device, union ib_gid *gid,
12171214
u8 *port_num, u16 *index)
12181215
{
12191216
union ib_gid tmp_gid;
1220-
int ret, port, i;
1217+
unsigned int port;
1218+
int ret, i;
12211219

1222-
for (port = rdma_start_port(device); port <= rdma_end_port(device); ++port) {
1220+
rdma_for_each_port (device, port) {
12231221
if (!rdma_protocol_ib(device, port))
12241222
continue;
12251223

drivers/infiniband/core/mad.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3326,9 +3326,9 @@ static void ib_mad_init_device(struct ib_device *device)
33263326

33273327
static void ib_mad_remove_device(struct ib_device *device, void *client_data)
33283328
{
3329-
int i;
3329+
unsigned int i;
33303330

3331-
for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
3331+
rdma_for_each_port (device, i) {
33323332
if (!rdma_cap_ib_mad(device, i))
33333333
continue;
33343334

drivers/infiniband/core/nldev.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ static int nldev_port_get_dumpit(struct sk_buff *skb,
774774
u32 idx = 0;
775775
u32 ifindex;
776776
int err;
777-
u32 p;
777+
unsigned int p;
778778

779779
err = nlmsg_parse(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
780780
nldev_policy, NULL);
@@ -786,7 +786,7 @@ static int nldev_port_get_dumpit(struct sk_buff *skb,
786786
if (!device)
787787
return -EINVAL;
788788

789-
for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
789+
rdma_for_each_port (device, p) {
790790
/*
791791
* The dumpit function returns all information from specific
792792
* index. This specific index is taken from the netlink

drivers/infiniband/core/security.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,15 @@ void ib_close_shared_qp_security(struct ib_qp_security *sec)
422422

423423
int ib_create_qp_security(struct ib_qp *qp, struct ib_device *dev)
424424
{
425-
u8 i = rdma_start_port(dev);
425+
unsigned int i;
426426
bool is_ib = false;
427427
int ret;
428428

429-
while (i <= rdma_end_port(dev) && !is_ib)
429+
rdma_for_each_port (dev, i) {
430430
is_ib = rdma_protocol_ib(dev, i++);
431+
if (is_ib)
432+
break;
433+
}
431434

432435
/* If this isn't an IB device don't create the security context */
433436
if (!is_ib)
@@ -561,9 +564,9 @@ void ib_security_cache_change(struct ib_device *device,
561564
void ib_security_release_port_pkey_list(struct ib_device *device)
562565
{
563566
struct pkey_index_qp_list *pkey, *tmp_pkey;
564-
int i;
567+
unsigned int i;
565568

566-
for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
569+
rdma_for_each_port (device, i) {
567570
list_for_each_entry_safe(pkey,
568571
tmp_pkey,
569572
&device->port_pkey_list[i].pkey_list,

drivers/infiniband/core/sysfs.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,23 +1308,17 @@ static void ib_free_port_attrs(struct ib_device *device)
13081308

13091309
static int ib_setup_port_attrs(struct ib_device *device)
13101310
{
1311+
unsigned int port;
13111312
int ret;
1312-
int i;
13131313

13141314
device->ports_kobj = kobject_create_and_add("ports", &device->dev.kobj);
13151315
if (!device->ports_kobj)
13161316
return -ENOMEM;
13171317

1318-
if (rdma_cap_ib_switch(device)) {
1319-
ret = add_port(device, 0);
1318+
rdma_for_each_port (device, port) {
1319+
ret = add_port(device, port);
13201320
if (ret)
13211321
goto err_put;
1322-
} else {
1323-
for (i = 1; i <= device->phys_port_cnt; ++i) {
1324-
ret = add_port(device, i);
1325-
if (ret)
1326-
goto err_put;
1327-
}
13281322
}
13291323

13301324
return 0;

drivers/infiniband/core/user_mad.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,14 +1323,15 @@ static void ib_umad_add_one(struct ib_device *device)
13231323
static void ib_umad_remove_one(struct ib_device *device, void *client_data)
13241324
{
13251325
struct ib_umad_device *umad_dev = client_data;
1326-
int i;
1326+
unsigned int i;
13271327

13281328
if (!umad_dev)
13291329
return;
13301330

1331-
for (i = 0; i <= rdma_end_port(device) - rdma_start_port(device); ++i) {
1332-
if (rdma_cap_ib_mad(device, i + rdma_start_port(device)))
1333-
ib_umad_kill_port(&umad_dev->ports[i]);
1331+
rdma_for_each_port (device, i) {
1332+
if (rdma_cap_ib_mad(device, i))
1333+
ib_umad_kill_port(
1334+
&umad_dev->ports[i - rdma_start_port(device)]);
13341335
}
13351336
/* balances kref_init() */
13361337
ib_umad_dev_put(umad_dev);

drivers/infiniband/ulp/ipoib/ipoib_main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,7 +2495,7 @@ static void ipoib_add_one(struct ib_device *device)
24952495
struct list_head *dev_list;
24962496
struct net_device *dev;
24972497
struct ipoib_dev_priv *priv;
2498-
int p;
2498+
unsigned int p;
24992499
int count = 0;
25002500

25012501
dev_list = kmalloc(sizeof(*dev_list), GFP_KERNEL);
@@ -2504,7 +2504,7 @@ static void ipoib_add_one(struct ib_device *device)
25042504

25052505
INIT_LIST_HEAD(dev_list);
25062506

2507-
for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
2507+
rdma_for_each_port (device, p) {
25082508
if (!rdma_protocol_ib(device, p))
25092509
continue;
25102510
dev = ipoib_add_port("ib%d", device, p);

drivers/infiniband/ulp/srp/ib_srp.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4127,7 +4127,8 @@ static void srp_add_one(struct ib_device *device)
41274127
struct srp_device *srp_dev;
41284128
struct ib_device_attr *attr = &device->attrs;
41294129
struct srp_host *host;
4130-
int mr_page_shift, p;
4130+
int mr_page_shift;
4131+
unsigned int p;
41314132
u64 max_pages_per_mr;
41324133
unsigned int flags = 0;
41334134

@@ -4194,7 +4195,7 @@ static void srp_add_one(struct ib_device *device)
41944195
WARN_ON_ONCE(srp_dev->global_rkey == 0);
41954196
}
41964197

4197-
for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
4198+
rdma_for_each_port (device, p) {
41984199
host = srp_add_port(srp_dev, p);
41994200
if (host)
42004201
list_add_tail(&host->list, &srp_dev->dev_list);

include/rdma/ib_verbs.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,6 +2827,16 @@ static inline u8 rdma_start_port(const struct ib_device *device)
28272827
return rdma_cap_ib_switch(device) ? 0 : 1;
28282828
}
28292829

2830+
/**
2831+
* rdma_for_each_port - Iterate over all valid port numbers of the IB device
2832+
* @device - The struct ib_device * to iterate over
2833+
* @iter - The unsigned int to store the port number
2834+
*/
2835+
#define rdma_for_each_port(device, iter) \
2836+
for (iter = rdma_start_port(device + BUILD_BUG_ON_ZERO(!__same_type( \
2837+
unsigned int, iter))); \
2838+
iter <= rdma_end_port(device); (iter)++)
2839+
28302840
/**
28312841
* rdma_end_port - Return the last valid port number for the device
28322842
* specified

0 commit comments

Comments
 (0)