Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 1 Sep 2021 23:07:37 GMT
From:      Marcin Wojtas <mw@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: cb98c439d66c - main - ena: Add locking assertions
Message-ID:  <202109012307.181N7bhH020874@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by mw:

URL: https://cgit.FreeBSD.org/src/commit/?id=cb98c439d66c303353a9f4abbbe9ddb51559c638

commit cb98c439d66c303353a9f4abbbe9ddb51559c638
Author:     Artur Rojek <ar@semihalf.com>
AuthorDate: 2021-08-12 08:34:26 +0000
Commit:     Marcin Wojtas <mw@FreeBSD.org>
CommitDate: 2021-09-01 23:06:21 +0000

    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 dbe5b9f785a9..faa1278c6d81 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 1d06a3cb56de..f05c2d69fbe4 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 e6286ebfc25f..6506c808e4cb 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;
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202109012307.181N7bhH020874>