Commit 6f38efca authored by Kamal Heib's avatar Kamal Heib Committed by Jason Gunthorpe

RDMA/core: Allocate the pkey cache only if the pkey_tbl_len is set

Allocate the pkey cache only if the pkey_tbl_len is set by the provider,
also add checks to avoid accessing the pkey cache when it not initialized.

Link: https://lore.kernel.org/r/20200714183414.61069-3-kamalheib1@gmail.comSigned-off-by: default avatarKamal Heib <kamalheib1@gmail.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
parent 90efc8b2
...@@ -1054,7 +1054,7 @@ int ib_get_cached_pkey(struct ib_device *device, ...@@ -1054,7 +1054,7 @@ int ib_get_cached_pkey(struct ib_device *device,
cache = device->port_data[port_num].cache.pkey; cache = device->port_data[port_num].cache.pkey;
if (index < 0 || index >= cache->table_len) if (!cache || index < 0 || index >= cache->table_len)
ret = -EINVAL; ret = -EINVAL;
else else
*pkey = cache->table[index]; *pkey = cache->table[index];
...@@ -1099,6 +1099,10 @@ int ib_find_cached_pkey(struct ib_device *device, ...@@ -1099,6 +1099,10 @@ int ib_find_cached_pkey(struct ib_device *device,
read_lock_irqsave(&device->cache_lock, flags); read_lock_irqsave(&device->cache_lock, flags);
cache = device->port_data[port_num].cache.pkey; cache = device->port_data[port_num].cache.pkey;
if (!cache) {
ret = -EINVAL;
goto err;
}
*index = -1; *index = -1;
...@@ -1117,6 +1121,7 @@ int ib_find_cached_pkey(struct ib_device *device, ...@@ -1117,6 +1121,7 @@ int ib_find_cached_pkey(struct ib_device *device,
ret = 0; ret = 0;
} }
err:
read_unlock_irqrestore(&device->cache_lock, flags); read_unlock_irqrestore(&device->cache_lock, flags);
return ret; return ret;
...@@ -1139,6 +1144,10 @@ int ib_find_exact_cached_pkey(struct ib_device *device, ...@@ -1139,6 +1144,10 @@ int ib_find_exact_cached_pkey(struct ib_device *device,
read_lock_irqsave(&device->cache_lock, flags); read_lock_irqsave(&device->cache_lock, flags);
cache = device->port_data[port_num].cache.pkey; cache = device->port_data[port_num].cache.pkey;
if (!cache) {
ret = -EINVAL;
goto err;
}
*index = -1; *index = -1;
...@@ -1149,6 +1158,7 @@ int ib_find_exact_cached_pkey(struct ib_device *device, ...@@ -1149,6 +1158,7 @@ int ib_find_exact_cached_pkey(struct ib_device *device,
break; break;
} }
err:
read_unlock_irqrestore(&device->cache_lock, flags); read_unlock_irqrestore(&device->cache_lock, flags);
return ret; return ret;
...@@ -1425,23 +1435,26 @@ ib_cache_update(struct ib_device *device, u8 port, bool enforce_security) ...@@ -1425,23 +1435,26 @@ ib_cache_update(struct ib_device *device, u8 port, bool enforce_security)
goto err; goto err;
} }
pkey_cache = kmalloc(struct_size(pkey_cache, table, if (tprops->pkey_tbl_len) {
tprops->pkey_tbl_len), pkey_cache = kmalloc(struct_size(pkey_cache, table,
GFP_KERNEL); tprops->pkey_tbl_len),
if (!pkey_cache) { GFP_KERNEL);
ret = -ENOMEM; if (!pkey_cache) {
goto err; ret = -ENOMEM;
} goto err;
}
pkey_cache->table_len = tprops->pkey_tbl_len; pkey_cache->table_len = tprops->pkey_tbl_len;
for (i = 0; i < pkey_cache->table_len; ++i) { for (i = 0; i < pkey_cache->table_len; ++i) {
ret = ib_query_pkey(device, port, i, pkey_cache->table + i); ret = ib_query_pkey(device, port, i,
if (ret) { pkey_cache->table + i);
dev_warn(&device->dev, if (ret) {
"ib_query_pkey failed (%d) for index %d\n", dev_warn(&device->dev,
ret, i); "ib_query_pkey failed (%d) for index %d\n",
goto err; ret, i);
goto err;
}
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment