Skip to content

Commit 8f82f89

Browse files
PatrisiousHaddadkuba-moo
authored andcommitted
net/mlx5: Refactor devcom to return NULL on failure
Devcom device and component registration isn't always critical to the functionality of the caller, hence the registration can fail and we can continue working with an ERR_PTR value saved inside a variable. In order to avoid that make sure all devcom failures return NULL. Signed-off-by: Patrisious Haddad <[email protected]> Reviewed-by: Leon Romanovsky <[email protected]> Signed-off-by: Tariq Toukan <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent d58a9a9 commit 8f82f89

File tree

7 files changed

+39
-42
lines changed

7 files changed

+39
-42
lines changed

drivers/net/ethernet/mellanox/mlx5/core/en_main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ static int mlx5e_devcom_init_mpv(struct mlx5e_priv *priv, u64 *data)
242242
&attr,
243243
mlx5e_devcom_event_mpv,
244244
priv);
245-
if (IS_ERR(priv->devcom))
246-
return PTR_ERR(priv->devcom);
245+
if (!priv->devcom)
246+
return -EINVAL;
247247

248248
if (mlx5_core_is_mp_master(priv->mdev)) {
249249
mlx5_devcom_send_event(priv->devcom, MPV_DEVCOM_MASTER_UP,
@@ -256,7 +256,7 @@ static int mlx5e_devcom_init_mpv(struct mlx5e_priv *priv, u64 *data)
256256

257257
static void mlx5e_devcom_cleanup_mpv(struct mlx5e_priv *priv)
258258
{
259-
if (IS_ERR_OR_NULL(priv->devcom))
259+
if (!priv->devcom)
260260
return;
261261

262262
if (mlx5_core_is_mp_master(priv->mdev)) {

drivers/net/ethernet/mellanox/mlx5/core/eswitch_offloads.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3129,7 +3129,7 @@ void mlx5_esw_offloads_devcom_init(struct mlx5_eswitch *esw,
31293129
attr,
31303130
mlx5_esw_offloads_devcom_event,
31313131
esw);
3132-
if (IS_ERR(esw->devcom))
3132+
if (!esw->devcom)
31333133
return;
31343134

31353135
mlx5_devcom_send_event(esw->devcom,
@@ -3140,7 +3140,7 @@ void mlx5_esw_offloads_devcom_init(struct mlx5_eswitch *esw,
31403140

31413141
void mlx5_esw_offloads_devcom_cleanup(struct mlx5_eswitch *esw)
31423142
{
3143-
if (IS_ERR_OR_NULL(esw->devcom))
3143+
if (!esw->devcom)
31443144
return;
31453145

31463146
mlx5_devcom_send_event(esw->devcom,

drivers/net/ethernet/mellanox/mlx5/core/lag/lag.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,11 +1430,10 @@ static int mlx5_lag_register_hca_devcom_comp(struct mlx5_core_dev *dev)
14301430
mlx5_devcom_register_component(dev->priv.devc,
14311431
MLX5_DEVCOM_HCA_PORTS,
14321432
&attr, NULL, dev);
1433-
if (IS_ERR(dev->priv.hca_devcom_comp)) {
1433+
if (!dev->priv.hca_devcom_comp) {
14341434
mlx5_core_err(dev,
1435-
"Failed to register devcom HCA component, err: %ld\n",
1436-
PTR_ERR(dev->priv.hca_devcom_comp));
1437-
return PTR_ERR(dev->priv.hca_devcom_comp);
1435+
"Failed to register devcom HCA component.");
1436+
return -EINVAL;
14381437
}
14391438

14401439
return 0;

drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,7 @@ static void mlx5_shared_clock_register(struct mlx5_core_dev *mdev, u64 key)
14441444
compd = mlx5_devcom_register_component(mdev->priv.devc,
14451445
MLX5_DEVCOM_SHARED_CLOCK,
14461446
&attr, NULL, mdev);
1447-
if (IS_ERR(compd))
1447+
if (!compd)
14481448
return;
14491449

14501450
mdev->clock_state->compdev = compd;

drivers/net/ethernet/mellanox/mlx5/core/lib/devcom.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,18 @@ mlx5_devcom_dev_alloc(struct mlx5_core_dev *dev)
7676
struct mlx5_devcom_dev *
7777
mlx5_devcom_register_device(struct mlx5_core_dev *dev)
7878
{
79-
struct mlx5_devcom_dev *devc;
79+
struct mlx5_devcom_dev *devc = NULL;
8080

8181
mutex_lock(&dev_list_lock);
8282

8383
if (devcom_dev_exists(dev)) {
84-
devc = ERR_PTR(-EEXIST);
84+
mlx5_core_err(dev, "devcom device already exists");
8585
goto out;
8686
}
8787

8888
devc = mlx5_devcom_dev_alloc(dev);
89-
if (!devc) {
90-
devc = ERR_PTR(-ENOMEM);
89+
if (!devc)
9190
goto out;
92-
}
9391

9492
list_add_tail(&devc->list, &devcom_dev_list);
9593
out:
@@ -110,8 +108,10 @@ mlx5_devcom_dev_release(struct kref *ref)
110108

111109
void mlx5_devcom_unregister_device(struct mlx5_devcom_dev *devc)
112110
{
113-
if (!IS_ERR_OR_NULL(devc))
114-
kref_put(&devc->ref, mlx5_devcom_dev_release);
111+
if (!devc)
112+
return;
113+
114+
kref_put(&devc->ref, mlx5_devcom_dev_release);
115115
}
116116

117117
static struct mlx5_devcom_comp *
@@ -122,7 +122,7 @@ mlx5_devcom_comp_alloc(u64 id, const struct mlx5_devcom_match_attr *attr,
122122

123123
comp = kzalloc(sizeof(*comp), GFP_KERNEL);
124124
if (!comp)
125-
return ERR_PTR(-ENOMEM);
125+
return NULL;
126126

127127
comp->id = id;
128128
comp->key.key = attr->key;
@@ -160,7 +160,7 @@ devcom_alloc_comp_dev(struct mlx5_devcom_dev *devc,
160160

161161
devcom = kzalloc(sizeof(*devcom), GFP_KERNEL);
162162
if (!devcom)
163-
return ERR_PTR(-ENOMEM);
163+
return NULL;
164164

165165
kref_get(&devc->ref);
166166
devcom->devc = devc;
@@ -240,31 +240,28 @@ mlx5_devcom_register_component(struct mlx5_devcom_dev *devc,
240240
mlx5_devcom_event_handler_t handler,
241241
void *data)
242242
{
243-
struct mlx5_devcom_comp_dev *devcom;
243+
struct mlx5_devcom_comp_dev *devcom = NULL;
244244
struct mlx5_devcom_comp *comp;
245245

246-
if (IS_ERR_OR_NULL(devc))
247-
return ERR_PTR(-EINVAL);
246+
if (!devc)
247+
return NULL;
248248

249249
mutex_lock(&comp_list_lock);
250250
comp = devcom_component_get(devc, id, attr, handler);
251-
if (IS_ERR(comp)) {
252-
devcom = ERR_PTR(-EINVAL);
251+
if (IS_ERR(comp))
253252
goto out_unlock;
254-
}
255253

256254
if (!comp) {
257255
comp = mlx5_devcom_comp_alloc(id, attr, handler);
258-
if (IS_ERR(comp)) {
259-
devcom = ERR_CAST(comp);
256+
if (!comp)
260257
goto out_unlock;
261-
}
258+
262259
list_add_tail(&comp->comp_list, &devcom_comp_list);
263260
}
264261
mutex_unlock(&comp_list_lock);
265262

266263
devcom = devcom_alloc_comp_dev(devc, comp, data);
267-
if (IS_ERR(devcom))
264+
if (!devcom)
268265
kref_put(&comp->ref, mlx5_devcom_comp_release);
269266

270267
return devcom;
@@ -276,8 +273,10 @@ mlx5_devcom_register_component(struct mlx5_devcom_dev *devc,
276273

277274
void mlx5_devcom_unregister_component(struct mlx5_devcom_comp_dev *devcom)
278275
{
279-
if (!IS_ERR_OR_NULL(devcom))
280-
devcom_free_comp_dev(devcom);
276+
if (!devcom)
277+
return;
278+
279+
devcom_free_comp_dev(devcom);
281280
}
282281

283282
int mlx5_devcom_comp_get_size(struct mlx5_devcom_comp_dev *devcom)
@@ -296,7 +295,7 @@ int mlx5_devcom_send_event(struct mlx5_devcom_comp_dev *devcom,
296295
int err = 0;
297296
void *data;
298297

299-
if (IS_ERR_OR_NULL(devcom))
298+
if (!devcom)
300299
return -ENODEV;
301300

302301
comp = devcom->comp;
@@ -338,7 +337,7 @@ void mlx5_devcom_comp_set_ready(struct mlx5_devcom_comp_dev *devcom, bool ready)
338337

339338
bool mlx5_devcom_comp_is_ready(struct mlx5_devcom_comp_dev *devcom)
340339
{
341-
if (IS_ERR_OR_NULL(devcom))
340+
if (!devcom)
342341
return false;
343342

344343
return READ_ONCE(devcom->comp->ready);
@@ -348,7 +347,7 @@ bool mlx5_devcom_for_each_peer_begin(struct mlx5_devcom_comp_dev *devcom)
348347
{
349348
struct mlx5_devcom_comp *comp;
350349

351-
if (IS_ERR_OR_NULL(devcom))
350+
if (!devcom)
352351
return false;
353352

354353
comp = devcom->comp;
@@ -421,21 +420,21 @@ void *mlx5_devcom_get_next_peer_data_rcu(struct mlx5_devcom_comp_dev *devcom,
421420

422421
void mlx5_devcom_comp_lock(struct mlx5_devcom_comp_dev *devcom)
423422
{
424-
if (IS_ERR_OR_NULL(devcom))
423+
if (!devcom)
425424
return;
426425
down_write(&devcom->comp->sem);
427426
}
428427

429428
void mlx5_devcom_comp_unlock(struct mlx5_devcom_comp_dev *devcom)
430429
{
431-
if (IS_ERR_OR_NULL(devcom))
430+
if (!devcom)
432431
return;
433432
up_write(&devcom->comp->sem);
434433
}
435434

436435
int mlx5_devcom_comp_trylock(struct mlx5_devcom_comp_dev *devcom)
437436
{
438-
if (IS_ERR_OR_NULL(devcom))
437+
if (!devcom)
439438
return 0;
440439
return down_write_trylock(&devcom->comp->sem);
441440
}

drivers/net/ethernet/mellanox/mlx5/core/lib/sd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ static int sd_register(struct mlx5_core_dev *dev)
221221
attr.net = mlx5_core_net(dev);
222222
devcom = mlx5_devcom_register_component(dev->priv.devc, MLX5_DEVCOM_SD_GROUP,
223223
&attr, NULL, dev);
224-
if (IS_ERR(devcom))
225-
return PTR_ERR(devcom);
224+
if (!devcom)
225+
return -EINVAL;
226226

227227
sd->devcom = devcom;
228228

drivers/net/ethernet/mellanox/mlx5/core/main.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -978,9 +978,8 @@ static int mlx5_init_once(struct mlx5_core_dev *dev)
978978
int err;
979979

980980
dev->priv.devc = mlx5_devcom_register_device(dev);
981-
if (IS_ERR(dev->priv.devc))
982-
mlx5_core_warn(dev, "failed to register devcom device %pe\n",
983-
dev->priv.devc);
981+
if (!dev->priv.devc)
982+
mlx5_core_warn(dev, "failed to register devcom device\n");
984983

985984
err = mlx5_query_board_id(dev);
986985
if (err) {

0 commit comments

Comments
 (0)