[PATCH v4 1/3] net: mvpp2: tai: add refcount for ptp worker

From: Shmuel Hazan
Date: Sun Apr 30 2023 - 13:07:22 EST


In some configurations, a single TAI can be responsible for multiple
mvpp2 interfaces. However, the mvpp2 driver will call mvpp22_tai_stop
and mvpp22_tai_start per interface RX timestamp disable/enable.

As a result, disabling timestamping for one interface would stop the
worker and corrupt the other interface's RX timestamps.

This commit solves the issue by introducing a simpler ref count for each
TAI instance.

Due to the ref count, we need now to lock tai->refcount_lock before
doing anything. As a result, we can't call mvpp22_tai_do_aux_work as it
will cause a deadlock. Therefore, we will just schedule the worker to
start immediately.

Fixes: ce3497e2072e ("net: mvpp2: ptp: add support for receive timestamping")
Signed-off-by: Shmuel Hazan <shmuel.h@xxxxxxxxx>
---
v1 -> v2: lock tai->lock before touching poll_worker_refcount.
v2 -> v3: no change
v3 -> v4: added additional lock for poll_worker_refcount due to
a possible deadlock.
---
.../net/ethernet/marvell/mvpp2/mvpp2_tai.c | 28 +++++++++++++++++--
1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/marvell/mvpp2/mvpp2_tai.c b/drivers/net/ethernet/marvell/mvpp2/mvpp2_tai.c
index 95862aff49f1..d8ce8bdae046 100644
--- a/drivers/net/ethernet/marvell/mvpp2/mvpp2_tai.c
+++ b/drivers/net/ethernet/marvell/mvpp2/mvpp2_tai.c
@@ -32,6 +32,7 @@
*
* Consequently, we support none of these.
*/
+#include "linux/spinlock.h"
#include <linux/io.h>
#include <linux/ptp_clock_kernel.h>
#include <linux/slab.h>
@@ -61,6 +62,8 @@ struct mvpp2_tai {
u64 period; // nanosecond period in 32.32 fixed point
/* This timestamp is updated every two seconds */
struct timespec64 stamp;
+ spinlock_t refcount_lock; /* Protects the poll_worker_refcount variable */
+ u16 poll_worker_refcount;
};

static void mvpp2_tai_modify(void __iomem *reg, u32 mask, u32 set)
@@ -370,16 +373,34 @@ void mvpp22_tai_tstamp(struct mvpp2_tai *tai, u32 tstamp,

void mvpp22_tai_start(struct mvpp2_tai *tai)
{
- long delay;
+ unsigned long flags;
+
+ spin_lock_irqsave(&tai->refcount_lock, flags);

- delay = mvpp22_tai_aux_work(&tai->caps);
+ tai->poll_worker_refcount++;
+ if (tai->poll_worker_refcount > 1)
+ goto out_unlock;

- ptp_schedule_worker(tai->ptp_clock, delay);
+ ptp_schedule_worker(tai->ptp_clock, 0);
+
+out_unlock:
+ spin_unlock_irqrestore(&tai->refcount_lock, flags);
}

void mvpp22_tai_stop(struct mvpp2_tai *tai)
{
+ unsigned long flags;
+
+ spin_lock_irqsave(&tai->refcount_lock, flags);
+
+ tai->poll_worker_refcount--;
+ if (tai->poll_worker_refcount)
+ goto unlock_out;
+
ptp_cancel_worker_sync(tai->ptp_clock);
+
+unlock_out:
+ spin_unlock_irqrestore(&tai->refcount_lock, flags);
}

static void mvpp22_tai_remove(void *priv)
@@ -400,6 +421,7 @@ int mvpp22_tai_probe(struct device *dev, struct mvpp2 *priv)
return -ENOMEM;

spin_lock_init(&tai->lock);
+ spin_lock_init(&tai->refcount_lock);

tai->base = priv->iface_base;

--
2.40.1