[PATCH 2/4] pps: generators: cancel TIO timer outside spinlock

From: KhaiWenTan

Date: Wed Dec 02 2026 - 04:08:54 EST


From: Voon Weifeng <weifeng.voon@xxxxxxxxx>

Move hrtimer_cancel() out of the spinlocked section. The timer
callback takes the same lock, so cancelling while holding it can
deadlock. Serialize enable, disable and removal with a mutex.

Fixes: c89755d1111f ("pps: generators: Add PPS Generator TIO Driver")
Signed-off-by: Voon Weifeng <weifeng.voon@xxxxxxxxx>
Signed-off-by: KhaiWenTan <khai.wen.tan@xxxxxxxxxxxxxxx>
---
drivers/pps/generators/pps_gen_tio.c | 25 +++++++++++++++++++++----
1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/drivers/pps/generators/pps_gen_tio.c b/drivers/pps/generators/pps_gen_tio.c
index c6d41fb2105f..9e3142041269 100644
--- a/drivers/pps/generators/pps_gen_tio.c
+++ b/drivers/pps/generators/pps_gen_tio.c
@@ -13,6 +13,7 @@
#include <linux/hrtimer.h>
#include <linux/io-64-nonatomic-hi-lo.h>
#include <linux/module.h>
+#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/pps_gen_kernel.h>
#include <linux/timekeeping.h>
@@ -44,6 +45,7 @@ struct pps_tio {
struct hrtimer timer;
void __iomem *base;
u32 prev_count;
+ struct mutex mutex;
spinlock_t lock;
struct device *dev;
};
@@ -167,22 +169,31 @@ static enum hrtimer_restart hrtimer_callback(struct hrtimer *timer)
static int pps_tio_gen_enable(struct pps_gen_device *pps_gen, bool enable)
{
struct pps_tio *tio = container_of(pps_gen->info, struct pps_tio, gen_info);
+ unsigned long flags;
+ int ret = 0;

+ mutex_lock(&tio->mutex);
if (!timekeeping_clocksource_has_base(CSID_X86_ART)) {
dev_err_once(tio->dev, "PPS cannot be used as clock is not related to ART");
- return -ENODEV;
+ ret = -ENODEV;
+ goto out_mutex;
}

- guard(spinlock_irqsave)(&tio->lock);
+ if (!enable)
+ hrtimer_cancel(&tio->timer);
+
+ spin_lock_irqsave(&tio->lock, flags);
if (enable && !pps_gen->enabled) {
pps_tio_direction_output(tio);
hrtimer_start(&tio->timer, first_event(tio), HRTIMER_MODE_ABS);
} else if (!enable && pps_gen->enabled) {
- hrtimer_cancel(&tio->timer);
pps_tio_disable(tio);
}
+ spin_unlock_irqrestore(&tio->lock, flags);

- return 0;
+out_mutex:
+ mutex_unlock(&tio->mutex);
+ return ret;
}

static int pps_tio_get_time(struct pps_gen_device *pps_gen,
@@ -206,6 +217,7 @@ static int pps_gen_tio_probe(struct platform_device *pdev)
tio = devm_kzalloc_obj(dev, *tio);
if (!tio)
return -ENOMEM;
+ mutex_init(&tio->mutex);

tio->gen_info.use_system_clock = true;
tio->gen_info.enable = pps_tio_gen_enable;
@@ -233,9 +245,14 @@ static int pps_gen_tio_probe(struct platform_device *pdev)
static void pps_gen_tio_remove(struct platform_device *pdev)
{
struct pps_tio *tio = platform_get_drvdata(pdev);
+ unsigned long flags;

+ mutex_lock(&tio->mutex);
hrtimer_cancel(&tio->timer);
+ spin_lock_irqsave(&tio->lock, flags);
pps_tio_disable(tio);
+ spin_unlock_irqrestore(&tio->lock, flags);
+ mutex_unlock(&tio->mutex);
pps_gen_unregister_source(tio->pps_gen);
}

--
2.43.0