Skip to content

Commit 4562116

Browse files
committed
net/mlx5e: Generalize IPsec work structs
IPsec logic has two work structs which are submitted to same workqueue. As a preparation to addition of new work which needs to be submitted too, let's generalize struct mlx5e_ipsec_work. Link: https://lore.kernel.org/r/285a1550242363de181bab3a07a69296f66ad9a8.1680162300.git.leonro@nvidia.com Reviewed-by: Raed Salem <[email protected]> Signed-off-by: Leon Romanovsky <[email protected]>
1 parent 20fbdab commit 4562116

File tree

3 files changed

+68
-48
lines changed

3 files changed

+68
-48
lines changed

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,16 @@ static int mlx5e_xfrm_validate_state(struct mlx5_core_dev *mdev,
406406
return 0;
407407
}
408408

409-
static void _update_xfrm_state(struct work_struct *work)
409+
static void mlx5e_ipsec_modify_state(struct work_struct *_work)
410410
{
411-
struct mlx5e_ipsec_modify_state_work *modify_work =
412-
container_of(work, struct mlx5e_ipsec_modify_state_work, work);
413-
struct mlx5e_ipsec_sa_entry *sa_entry = container_of(
414-
modify_work, struct mlx5e_ipsec_sa_entry, modify_work);
411+
struct mlx5e_ipsec_work *work =
412+
container_of(_work, struct mlx5e_ipsec_work, work);
413+
struct mlx5e_ipsec_sa_entry *sa_entry = work->sa_entry;
414+
struct mlx5_accel_esp_xfrm_attrs *attrs;
415415

416-
mlx5_accel_esp_modify_xfrm(sa_entry, &modify_work->attrs);
416+
attrs = &((struct mlx5e_ipsec_sa_entry *)work->data)->attrs;
417+
418+
mlx5_accel_esp_modify_xfrm(sa_entry, attrs);
417419
}
418420

419421
static void mlx5e_ipsec_set_esn_ops(struct mlx5e_ipsec_sa_entry *sa_entry)
@@ -432,6 +434,36 @@ static void mlx5e_ipsec_set_esn_ops(struct mlx5e_ipsec_sa_entry *sa_entry)
432434
sa_entry->set_iv_op = mlx5e_ipsec_set_iv;
433435
}
434436

437+
static int mlx5_ipsec_create_work(struct mlx5e_ipsec_sa_entry *sa_entry)
438+
{
439+
struct xfrm_state *x = sa_entry->x;
440+
struct mlx5e_ipsec_work *work;
441+
442+
switch (x->xso.type) {
443+
case XFRM_DEV_OFFLOAD_CRYPTO:
444+
if (!(x->props.flags & XFRM_STATE_ESN))
445+
return 0;
446+
break;
447+
default:
448+
return 0;
449+
}
450+
451+
work = kzalloc(sizeof(*work), GFP_KERNEL);
452+
if (!work)
453+
return -ENOMEM;
454+
455+
work->data = kzalloc(sizeof(*sa_entry), GFP_KERNEL);
456+
if (!work->data) {
457+
kfree(work);
458+
return -ENOMEM;
459+
}
460+
461+
INIT_WORK(&work->work, mlx5e_ipsec_modify_state);
462+
work->sa_entry = sa_entry;
463+
sa_entry->work = work;
464+
return 0;
465+
}
466+
435467
static int mlx5e_xfrm_add_state(struct xfrm_state *x,
436468
struct netlink_ext_ack *extack)
437469
{
@@ -467,10 +499,15 @@ static int mlx5e_xfrm_add_state(struct xfrm_state *x,
467499
mlx5e_ipsec_update_esn_state(sa_entry);
468500

469501
mlx5e_ipsec_build_accel_xfrm_attrs(sa_entry, &sa_entry->attrs);
502+
503+
err = mlx5_ipsec_create_work(sa_entry);
504+
if (err)
505+
goto err_xfrm;
506+
470507
/* create hw context */
471508
err = mlx5_ipsec_create_sa_ctx(sa_entry);
472509
if (err)
473-
goto err_xfrm;
510+
goto release_work;
474511

475512
err = mlx5e_accel_ipsec_fs_add_rule(sa_entry);
476513
if (err)
@@ -486,16 +523,6 @@ static int mlx5e_xfrm_add_state(struct xfrm_state *x,
486523
goto err_add_rule;
487524

488525
mlx5e_ipsec_set_esn_ops(sa_entry);
489-
490-
switch (x->xso.type) {
491-
case XFRM_DEV_OFFLOAD_CRYPTO:
492-
if (x->props.flags & XFRM_STATE_ESN)
493-
INIT_WORK(&sa_entry->modify_work.work,
494-
_update_xfrm_state);
495-
break;
496-
default:
497-
break;
498-
}
499526
out:
500527
x->xso.offload_handle = (unsigned long)sa_entry;
501528
return 0;
@@ -504,6 +531,9 @@ static int mlx5e_xfrm_add_state(struct xfrm_state *x,
504531
mlx5e_accel_ipsec_fs_del_rule(sa_entry);
505532
err_hw_ctx:
506533
mlx5_ipsec_free_sa_ctx(sa_entry);
534+
release_work:
535+
kfree(sa_entry->work->data);
536+
kfree(sa_entry->work);
507537
err_xfrm:
508538
kfree(sa_entry);
509539
NL_SET_ERR_MSG_MOD(extack, "Device failed to offload this policy");
@@ -530,17 +560,13 @@ static void mlx5e_xfrm_free_state(struct xfrm_state *x)
530560
if (x->xso.flags & XFRM_DEV_OFFLOAD_FLAG_ACQ)
531561
goto sa_entry_free;
532562

533-
switch (x->xso.type) {
534-
case XFRM_DEV_OFFLOAD_CRYPTO:
535-
if (x->props.flags & XFRM_STATE_ESN)
536-
cancel_work_sync(&sa_entry->modify_work.work);
537-
break;
538-
default:
539-
break;
540-
}
563+
if (sa_entry->work)
564+
cancel_work_sync(&sa_entry->work->work);
541565

542566
mlx5e_accel_ipsec_fs_del_rule(sa_entry);
543567
mlx5_ipsec_free_sa_ctx(sa_entry);
568+
kfree(sa_entry->work->data);
569+
kfree(sa_entry->work);
544570
sa_entry_free:
545571
kfree(sa_entry);
546572
}
@@ -626,16 +652,18 @@ static bool mlx5e_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
626652
static void mlx5e_xfrm_advance_esn_state(struct xfrm_state *x)
627653
{
628654
struct mlx5e_ipsec_sa_entry *sa_entry = to_ipsec_sa_entry(x);
629-
struct mlx5e_ipsec_modify_state_work *modify_work =
630-
&sa_entry->modify_work;
655+
struct mlx5e_ipsec_work *work = sa_entry->work;
656+
struct mlx5e_ipsec_sa_entry *sa_entry_shadow;
631657
bool need_update;
632658

633659
need_update = mlx5e_ipsec_update_esn_state(sa_entry);
634660
if (!need_update)
635661
return;
636662

637-
mlx5e_ipsec_build_accel_xfrm_attrs(sa_entry, &modify_work->attrs);
638-
queue_work(sa_entry->ipsec->wq, &modify_work->work);
663+
sa_entry_shadow = work->data;
664+
memset(sa_entry_shadow, 0x00, sizeof(*sa_entry_shadow));
665+
mlx5e_ipsec_build_accel_xfrm_attrs(sa_entry, &sa_entry_shadow->attrs);
666+
queue_work(sa_entry->ipsec->wq, &work->work);
639667
}
640668

641669
static void mlx5e_xfrm_update_curlft(struct xfrm_state *x)

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ struct mlx5e_ipsec_tx;
136136

137137
struct mlx5e_ipsec_work {
138138
struct work_struct work;
139-
struct mlx5e_ipsec *ipsec;
140-
u32 id;
139+
struct mlx5e_ipsec_sa_entry *sa_entry;
140+
void *data;
141141
};
142142

143143
struct mlx5e_ipsec_aso {
@@ -176,11 +176,6 @@ struct mlx5e_ipsec_rule {
176176
struct mlx5_fc *fc;
177177
};
178178

179-
struct mlx5e_ipsec_modify_state_work {
180-
struct work_struct work;
181-
struct mlx5_accel_esp_xfrm_attrs attrs;
182-
};
183-
184179
struct mlx5e_ipsec_limits {
185180
u64 round;
186181
u8 soft_limit_hit : 1;
@@ -197,7 +192,7 @@ struct mlx5e_ipsec_sa_entry {
197192
u32 ipsec_obj_id;
198193
u32 enc_key_id;
199194
struct mlx5e_ipsec_rule ipsec_rule;
200-
struct mlx5e_ipsec_modify_state_work modify_work;
195+
struct mlx5e_ipsec_work *work;
201196
struct mlx5e_ipsec_limits limits;
202197
};
203198

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -417,18 +417,12 @@ static void mlx5e_ipsec_handle_event(struct work_struct *_work)
417417
{
418418
struct mlx5e_ipsec_work *work =
419419
container_of(_work, struct mlx5e_ipsec_work, work);
420+
struct mlx5e_ipsec_sa_entry *sa_entry = work->data;
420421
struct mlx5_accel_esp_xfrm_attrs *attrs;
421-
struct mlx5e_ipsec_sa_entry *sa_entry;
422422
struct mlx5e_ipsec_aso *aso;
423-
struct mlx5e_ipsec *ipsec;
424423
int ret;
425424

426-
sa_entry = xa_load(&work->ipsec->sadb, work->id);
427-
if (!sa_entry)
428-
goto out;
429-
430-
ipsec = sa_entry->ipsec;
431-
aso = ipsec->aso;
425+
aso = sa_entry->ipsec->aso;
432426
attrs = &sa_entry->attrs;
433427

434428
spin_lock(&sa_entry->x->lock);
@@ -448,14 +442,14 @@ static void mlx5e_ipsec_handle_event(struct work_struct *_work)
448442

449443
unlock:
450444
spin_unlock(&sa_entry->x->lock);
451-
out:
452445
kfree(work);
453446
}
454447

455448
static int mlx5e_ipsec_event(struct notifier_block *nb, unsigned long event,
456449
void *data)
457450
{
458451
struct mlx5e_ipsec *ipsec = container_of(nb, struct mlx5e_ipsec, nb);
452+
struct mlx5e_ipsec_sa_entry *sa_entry;
459453
struct mlx5_eqe_obj_change *object;
460454
struct mlx5e_ipsec_work *work;
461455
struct mlx5_eqe *eqe = data;
@@ -470,13 +464,16 @@ static int mlx5e_ipsec_event(struct notifier_block *nb, unsigned long event,
470464
if (type != MLX5_GENERAL_OBJECT_TYPES_IPSEC)
471465
return NOTIFY_DONE;
472466

467+
sa_entry = xa_load(&ipsec->sadb, be32_to_cpu(object->obj_id));
468+
if (!sa_entry)
469+
return NOTIFY_DONE;
470+
473471
work = kmalloc(sizeof(*work), GFP_ATOMIC);
474472
if (!work)
475473
return NOTIFY_DONE;
476474

477475
INIT_WORK(&work->work, mlx5e_ipsec_handle_event);
478-
work->ipsec = ipsec;
479-
work->id = be32_to_cpu(object->obj_id);
476+
work->data = sa_entry;
480477

481478
queue_work(ipsec->wq, &work->work);
482479
return NOTIFY_OK;

0 commit comments

Comments
 (0)