From cb98c439d66c303353a9f4abbbe9ddb51559c638 Mon Sep 17 00:00:00 2001 From: Artur Rojek Date: Thu, 12 Aug 2021 10:34:26 +0200 Subject: [PATCH] ena: Add locking assertions ENA silently assumed that ena_up, ena_down and ena_start_xmit routines should be called within locked context. Driver's logic heavily assumes on concurrent access to those routines, so for safety and better documentation about this assumption, the locking assertions were added to the above functions. The assertion was added only for the main steps (skipping the helper functions) which can be called from multiple places including the kernel and the driver itself. Obtained from: Semihalf MFC after: 2 weeks Sponsored by: Amazon, Inc. --- sys/dev/ena/ena.c | 4 ++++ sys/dev/ena/ena.h | 4 ++++ sys/dev/ena/ena_datapath.c | 2 ++ 3 files changed, 10 insertions(+) diff --git a/sys/dev/ena/ena.c b/sys/dev/ena/ena.c index dbe5b9f785a..faa1278c6d8 100644 --- a/sys/dev/ena/ena.c +++ b/sys/dev/ena/ena.c @@ -2092,6 +2092,8 @@ ena_up(struct ena_adapter *adapter) { int rc = 0; + ENA_LOCK_ASSERT(adapter); + if (unlikely(device_is_attached(adapter->pdev) == 0)) { ena_log(adapter->pdev, ERR, "device is not attached!\n"); return (ENXIO); @@ -2465,6 +2467,8 @@ ena_down(struct ena_adapter *adapter) { int rc; + ENA_LOCK_ASSERT(adapter); + if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) return; diff --git a/sys/dev/ena/ena.h b/sys/dev/ena/ena.h index 1d06a3cb56d..f05c2d69fbe 100644 --- a/sys/dev/ena/ena.h +++ b/sys/dev/ena/ena.h @@ -480,12 +480,16 @@ struct ena_adapter { #define ENA_RING_MTX_LOCK(_ring) mtx_lock(&(_ring)->ring_mtx) #define ENA_RING_MTX_TRYLOCK(_ring) mtx_trylock(&(_ring)->ring_mtx) #define ENA_RING_MTX_UNLOCK(_ring) mtx_unlock(&(_ring)->ring_mtx) +#define ENA_RING_MTX_ASSERT(_ring) \ + mtx_assert(&(_ring)->ring_mtx, MA_OWNED) #define ENA_LOCK_INIT(adapter) \ sx_init(&(adapter)->global_lock, "ENA global lock") #define ENA_LOCK_DESTROY(adapter) sx_destroy(&(adapter)->global_lock) #define ENA_LOCK_LOCK(adapter) sx_xlock(&(adapter)->global_lock) #define ENA_LOCK_UNLOCK(adapter) sx_unlock(&(adapter)->global_lock) +#define ENA_LOCK_ASSERT(adapter) \ + sx_assert(&(adapter)->global_lock, SA_XLOCKED) #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max) #define clamp_val(val, lo, hi) clamp_t(__typeof(val), val, lo, hi) diff --git a/sys/dev/ena/ena_datapath.c b/sys/dev/ena/ena_datapath.c index e6286ebfc25..6506c808e4c 100644 --- a/sys/dev/ena/ena_datapath.c +++ b/sys/dev/ena/ena_datapath.c @@ -1075,6 +1075,8 @@ ena_start_xmit(struct ena_ring *tx_ring) int ena_qid; int ret = 0; + ENA_RING_MTX_ASSERT(tx_ring); + if (unlikely((if_getdrvflags(adapter->ifp) & IFF_DRV_RUNNING) == 0)) return;