Skip to contentSkip to conversation

pps: improve PREEMPT_RT performance

PATCH Applied Calvin Owens 5 participants 10 messages Jun 2 – Jul 6

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v7 cover pps: improve PREEMPT_RT performance

Hello all,

I'm relaying v7 for Michael.

A quick note, this conflicts with the patch I have out to remove
capture_clear: it's trivial to resolve, but if it saves anybody time let me know and I can respin one or the other.

Thanks,
Calvin

---
From: Michael Byczkowski <[email protected]>

Changes since v6: https://lore.kernel.org/lkml/[email protected]/
- patches 2 and 3 are dropped since neither lock is ever taken in hardirq context.

Changes since v5: https://lore.kernel.org/lkml/[email protected]/
- Reordered: the pps_kc_hardpps_lock conversion now precedes the
pps_device.lock conversion. The previous order would have briefly
produced a raw_spinlock holding a sleeping spinlock on PREEMPT_RT
(Sebastian).
- Patch 1/3: commit message reworded to describe the handler split
structurally first, then its PREEMPT_RT benefit (Sebastian).
- Patch 2/3: refactored pps_kc_bind() and pps_kc_remove() to use guard(raw_spinlock_irq) for scope-based lock release. Eliminates
four duplicated unlock call sites in pps_kc_bind() and the
ambiguous bracket structure that resulted from them (Sebastian).
- Rodolfo's Acked-by on patch 2/3 is preserved from v5; the guard()
refactor is purely stylistic and was suggested by Sebastian, but
please re-ack or NAK if disagreement.

Changes since v4: https://lore.kernel.org/lkml/[email protected]/
- Patch 2/3: added Acked-by: Rodolfo Giometti <[email protected]>

Changes since v3: https://lore.kernel.org/lkml/[email protected]/
- Patch 2/3: fixed lost indentation on pps_kc_event() call
(reported by Rodolfo Giometti <[email protected]>)

Changes since v2: https://lore.kernel.org/lkml/[email protected]/
- Patch 2/3: moved wake_up_interruptible_all() and kill_fasync() out
of raw_spinlock section to avoid sleeping-in-atomic on PREEMPT_RT
(reported by Nikolaus Buchwitz <[email protected]>)

This patchset addresses three sources of PPS jitter under PREEMPT_RT, while being fully backward-compatible with non-RT kernels:

1. pps-gpio: The IRQ handler is force-threaded on PREEMPT_RT, so the PPS timestamp is captured after scheduling delay rather than at interrupt entry. Fix: split into a hardirq primary handler (captures timestamp only) and a threaded handler (processes the event).

Tested on a Raspberry Pi 5 running 7.0.1 and 7.1-rc PREEMPT_RT kernels. On non-RT kernels there is zero behavioral change.

Michael Byczkowski (1):
pps: pps-gpio: split IRQ handler into hardirq timestamper + threaded
handler

 drivers/pps/clients/pps-gpio.c | 37 +++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 12 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v7 1/1 pps: pps-gpio: split IRQ handler into hardirq timestamper + threaded handler

From: Michael Byczkowski <[email protected]>

Split the pps-gpio interrupt handler into a primary (hardirq) handler that captures the PPS timestamp at interrupt entry, and a threaded handler that processes the event. This produces the same two-part handler structure on both PREEMPT_RT and non-RT kernels.

On non-RT kernels the threaded portion runs immediately after the primary, with no behavioral change compared to the previous single-handler implementation.

On PREEMPT_RT, where interrupt handlers are force-threaded by default, the previous single-handler implementation captured the timestamp inside the threaded portion, after IRQ-thread scheduling delay. With the split, the timestamp is captured in true hardirq context as it is on non-RT kernels, eliminating a significant source of PPS jitter on RT systems.

---
 drivers/pps/clients/pps-gpio.c | 37 +++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 12 deletions(-)
drivers/pps/clients/pps-gpio.c +25 -12
index 935da68610c7..ed111621ee5f 100644
@@ -35,33 +35,44 @@ struct pps_gpio_device_data {
 	bool capture_clear;
 	unsigned int echo_active_ms;	/* PPS echo active duration */
 	unsigned long echo_timeout;	/* timer timeout value in jiffies */
+	struct pps_event_time ts;	/* timestamp captured in hardirq */
 };
 
 /*
  * Report the PPS event
  */
 
-static irqreturn_t pps_gpio_irq_handler(int irq, void *data)
+/*
+ * Primary hardirq handler -- runs in hardirq context even on PREEMPT_RT.
+ * Only captures the timestamp; all other work is deferred to the thread.
+ */
+static irqreturn_t pps_gpio_irq_hardirq(int irq, void *data)
 {
-	const struct pps_gpio_device_data *info;
-	struct pps_event_time ts;
-	int rising_edge;
+	struct pps_gpio_device_data *info = data;
+
+	pps_get_ts(&info->ts);
 
-	/* Get the time stamp first */
-	pps_get_ts(&ts);
+	return IRQ_WAKE_THREAD;
+}
 
-	info = data;
+/*
+ * Threaded handler -- processes the PPS event using the timestamp
+ * captured in hardirq context above.
+ */
+static irqreturn_t pps_gpio_irq_thread(int irq, void *data)
+{
+	struct pps_gpio_device_data *info = data;
+	int rising_edge;
 
-	/* Small trick to bypass the check on edge's direction when capture_clear is unset */
 	rising_edge = info->capture_clear ?
 		      gpiod_get_value(info->gpio_pin) : !info->assert_falling_edge;
 	if ((rising_edge && !info->assert_falling_edge) ||
 			(!rising_edge && info->assert_falling_edge))
-		pps_event(info->pps, &ts, PPS_CAPTUREASSERT, data);
+		pps_event(info->pps, &info->ts, PPS_CAPTUREASSERT, data);
 	else if (info->capture_clear &&
 			((rising_edge && info->assert_falling_edge) ||
 			(!rising_edge && !info->assert_falling_edge)))
-		pps_event(info->pps, &ts, PPS_CAPTURECLEAR, data);
+		pps_event(info->pps, &info->ts, PPS_CAPTURECLEAR, data);
 	else
 		dev_warn_ratelimited(&info->pps->dev, "IRQ did not trigger any PPS event\n");
 
@@ -210,8 +221,10 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	}
 
 	/* register IRQ interrupt handler */
-	ret = request_irq(data->irq, pps_gpio_irq_handler,
-			  get_irqf_trigger_flags(data), data->info.name, data);
+	ret = request_threaded_irq(data->irq,
+			  pps_gpio_irq_hardirq, pps_gpio_irq_thread,
+			  get_irqf_trigger_flags(data) | IRQF_ONESHOT,
+			  data->info.name, data);
 	if (ret) {
 		pps_unregister_source(data->pps);
 		dev_err(dev, "failed to acquire IRQ %d\n", data->irq);
Signature · 1 line
2.47.3
maintainer Reviewed-by on v7 1/1
#2 lore raw

Re: [PATCH v7 1/1] pps: pps-gpio: split IRQ handler into hardirq timestamper + threaded handler

Quoted 18 lines · Calvin Owens

From: Michael Byczkowski <[email protected]>

Split the pps-gpio interrupt handler into a primary (hardirq) handler that captures the PPS timestamp at interrupt entry, and a threaded handler that processes the event. This produces the same two-part handler structure on both PREEMPT_RT and non-RT kernels.

On non-RT kernels the threaded portion runs immediately after the primary, with no behavioral change compared to the previous single-handler implementation.

On PREEMPT_RT, where interrupt handlers are force-threaded by default, the previous single-handler implementation captured the timestamp inside the threaded portion, after IRQ-thread scheduling delay. With the split, the timestamp is captured in true hardirq context as it is on non-RT kernels, eliminating a significant source of PPS jitter on RT systems.

Quoted 6 lines

---

Sebastian

#3 lore raw

Re: [PATCH v7 1/1] pps: pps-gpio: split IRQ handler into hardirq timestamper + threaded handler

Thank you Sebastian, and thank you Calvin for relaying. I appreciate the thorough review that got this to the right shape.

Best regards,
Michael

Quoted 31 lines · Sebastian Andrzej Siewior
Quoted 18 lines · Calvin Owens

From: Michael Byczkowski <[email protected]>

Split the pps-gpio interrupt handler into a primary (hardirq) handler that captures the PPS timestamp at interrupt entry, and a threaded handler that processes the event. This produces the same two-part handler structure on both PREEMPT_RT and non-RT kernels.

On non-RT kernels the threaded portion runs immediately after the primary, with no behavioral change compared to the previous single-handler implementation.

On PREEMPT_RT, where interrupt handlers are force-threaded by default, the previous single-handler implementation captured the timestamp inside the threaded portion, after IRQ-thread scheduling delay. With the split, the timestamp is captured in true hardirq context as it is on non-RT kernels, eliminating a significant source of PPS jitter on RT systems.

Quoted 6 lines

---

Sebastian

#4 lore raw

Re: [PATCH v7 1/1] pps: pps-gpio: split IRQ handler into hardirq timestamper + threaded handler

Dear All,

Now that 7.2-rc1 is out and this didn't make the merge window, could someone please confirm which tree will carry this for 7.3? So far, the patch has been:
• Reviewed-by: Sebastian Andrzej Siewior (Jun 2)
• Acked-by: Rodolfo Giometti (PPS maintainer, from earlier revisions)
• Tested-by from Calvin Owens and myself

I'm not sure whether it should go via drivers/pps (Rodolfo), the rt tree, or -mm. Happy to help route it wherever makes sense.
lore: https://lore.kernel.org/lkml/[email protected]/

Thank you very much and best regards,
Michael

Quoted 41 lines · Michael Byczkowski

Thank you Sebastian, and thank you Calvin for relaying. I appreciate the thorough review that got this to the right shape.

Best regards,
Michael

Quoted 31 lines · Sebastian Andrzej Siewior
Quoted 18 lines · Calvin Owens

From: Michael Byczkowski <[email protected]>

Split the pps-gpio interrupt handler into a primary (hardirq) handler that captures the PPS timestamp at interrupt entry, and a threaded handler that processes the event. This produces the same two-part handler structure on both PREEMPT_RT and non-RT kernels.

On non-RT kernels the threaded portion runs immediately after the primary, with no behavioral change compared to the previous single-handler implementation.

On PREEMPT_RT, where interrupt handlers are force-threaded by default, the previous single-handler implementation captured the timestamp inside the threaded portion, after IRQ-thread scheduling delay. With the split, the timestamp is captured in true hardirq context as it is on non-RT kernels, eliminating a significant source of PPS jitter on RT systems.

Quoted 6 lines

---

Sebastian

maintainer
#5 lore raw

Re: [PATCH v7 1/1] pps: pps-gpio: split IRQ handler into hardirq timestamper + threaded handler

Hi Andrew,

Could you please pick this patch up for the 7.3 merge window via the -mm tree?

Since the PPS subsystem doesn't have its own dedicated git tree, routing it through -mm is the standard path. The patch already has my Acked-by, along with all the necessary reviews and test tags.

Thanks,
Rodolfo

Quoted 58 lines · Michael Byczkowski

Dear All,

Now that 7.2-rc1 is out and this didn't make the merge window, could someone please confirm which tree will carry this for 7.3? So far, the patch has been:
• Reviewed-by: Sebastian Andrzej Siewior (Jun 2)
• Acked-by: Rodolfo Giometti (PPS maintainer, from earlier revisions)
• Tested-by from Calvin Owens and myself

I'm not sure whether it should go via drivers/pps (Rodolfo), the rt tree, or -mm. Happy to help route it wherever makes sense.
lore: https://lore.kernel.org/lkml/[email protected]/

Thank you very much and best regards,
Michael

Quoted 41 lines · Michael Byczkowski

Thank you Sebastian, and thank you Calvin for relaying. I appreciate the thorough review that got this to the right shape.

Best regards,
Michael

Quoted 31 lines · Sebastian Andrzej Siewior
Quoted 18 lines · Calvin Owens

From: Michael Byczkowski <[email protected]>

Split the pps-gpio interrupt handler into a primary (hardirq) handler that captures the PPS timestamp at interrupt entry, and a threaded handler that processes the event. This produces the same two-part handler structure on both PREEMPT_RT and non-RT kernels.

On non-RT kernels the threaded portion runs immediately after the primary, with no behavioral change compared to the previous single-handler implementation.

On PREEMPT_RT, where interrupt handlers are force-threaded by default, the previous single-handler implementation captured the timestamp inside the threaded portion, after IRQ-thread scheduling delay. With the split, the timestamp is captured in true hardirq context as it is on non-RT kernels, eliminating a significant source of PPS jitter on RT systems.

Quoted 6 lines

---

Sebastian

Signature · 4 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming
#6 lore raw

Re: [PATCH v7 1/1] pps: pps-gpio: split IRQ handler into hardirq timestamper + threaded handler

In reply to Calvin Owens
Quoted 9 lines · On Mon, 1 Jun 2026 17:44:09 -0700 Calvin Owens <[email protected]> wrote:

Split the pps-gpio interrupt handler into a primary (hardirq) handler that captures the PPS timestamp at interrupt entry, and a threaded handler that processes the event. This produces the same two-part handler structure on both PREEMPT_RT and non-RT kernels.

On non-RT kernels the threaded portion runs immediately after the primary, with no behavioral change compared to the previous single-handler implementation.

Quoted 6 lines

On PREEMPT_RT, where interrupt handlers are force-threaded by default, the previous single-handler implementation captured the timestamp inside the threaded portion, after IRQ-thread scheduling delay. With the split, the timestamp is captured in true hardirq context as it is on non-RT kernels, eliminating a significant source of PPS jitter on RT systems.

Thanks, I'll add this to mm.git's mm-nonmm-unstable branch. This means it will get linux-next exposure immediately.

All being well, I'll later move this into the non-rebasing mm-nonmm-stable branch with the intent to upstream it in the next merge window.

Sashiko AI review might have found some issues, one of them pre-existing. Please check it out:

	https://sashiko.dev/#/patchset/2e32729029fbf6977ecf04665eb00f2efd3e2c17.1780359378.git.calvin@wbinvd.org
#7 lore raw

Re: [PATCH v7 1/1] pps: pps-gpio: split IRQ handler into hardirq timestamper + threaded handler

Quoted 11 lines · Andrew Morton

Thanks, I'll add this to mm.git's mm-nonmm-unstable branch. This means it will get linux-next exposure immediately.

All being well, I'll later move this into the non-rebasing mm-nonmm-stable branch with the intent to upstream it in the next merge window.

Sashiko AI review might have found some issues, one of them pre-existing. Please check it out:

	https://sashiko.dev/#/patchset/2e32729029fbf6977ecf04665eb00f2efd3e2c17.1780359378.git.calvin@wbinvd.org

Hi Andrew,

I've sent three patches which address all the sashiko issues:

    https://lore.kernel.org/lkml/c5c97c3b3c9d66010382094fd538e59a38f4aacf.1781289959.git.calvin@wbinvd.org/
    https://lore.kernel.org/lkml/672778c177ac9b6fdcb445e35c97ac4ca7d1149f.1780506611.git.calvin@wbinvd.org/
    https://lore.kernel.org/lkml/7a71b8e74bce9582d3286d31e6c85ac77465e421.1780108620.git.calvin@wbinvd.org/

I can resend those as a series if it saves any time, let me know (the last one has a trivial conflict with the patch you just merged).

Thanks,
Calvin

#8 lore raw

Re: [PATCH v7 1/1] pps: pps-gpio: split IRQ handler into hardirq timestamper + threaded handler

Quoted 11 lines · On Sat, 4 Jul 2026 18:35:53 -0700 Calvin Owens <[email protected]> wrote:
Quoted 10 lines · Andrew Morton

Thanks, I'll add this to mm.git's mm-nonmm-unstable branch. This means it will get linux-next exposure immediately.

All being well, I'll later move this into the non-rebasing mm-nonmm-stable branch with the intent to upstream it in the next merge window.

Sashiko AI review might have found some issues, one of them pre-existing. Please check it out:

Quoted 8 lines
Quoted 1 line
	https://sashiko.dev/#/patchset/2e32729029fbf6977ecf04665eb00f2efd3e2c17.1780359378.git.calvin@wbinvd.org

Hi Andrew,

I've sent three patches which address all the sashiko issues:

    https://lore.kernel.org/lkml/c5c97c3b3c9d66010382094fd538e59a38f4aacf.1781289959.git.calvin@wbinvd.org/
    https://lore.kernel.org/lkml/672778c177ac9b6fdcb445e35c97ac4ca7d1149f.1780506611.git.calvin@wbinvd.org/

OK, thanks, grabbed.

Quoted 4 lines
    https://lore.kernel.org/lkml/7a71b8e74bce9582d3286d31e6c85ac77465e421.1780108620.git.calvin@wbinvd.org/

I can resend those as a series if it saves any time, let me know (the last one has a trivial conflict with the patch you just merged).

The last patch ("pps: pps-gpio: split IRQ handler into hardirq timestamper + threaded handler") appears to be altering code which isn't there any more. So yes please, resend. Please cc myself.

#9 lore raw

PATCH v2 1/1 pps-gpio: Remove dead capture_clear code

The capture_clear field is never set, and all code conditional on it being set has been unreachable since the platform data logic was removed from pps-gpio in ee89646619ba ("pps: clients: gpio: Get rid of legacy platform data").

I think the only logical thing to do here is to remove it all, since no in-tree code ever actually used it in the first place, and it has been completely dead code for over five years (since v5.13).

Sashiko asked some questions about the gpiod_get_value() call which caused me to look deeper and figure this out, but it did not actually notice capture_clear is never set.

Fixes ee89646619ba ("pps: clients: gpio: Get rid of legacy platform data")

---
Changes in v2:
* Rebased onto next-20260706 for Andrew

v1: https://lore.kernel.org/lkml/7a71b8e74bce9582d3286d31e6c85ac77465e421.1780108620.git.calvin@wbinvd.org/

 drivers/pps/clients/pps-gpio.c | 37 ++++------------------------------
 1 file changed, 4 insertions(+), 33 deletions(-)
drivers/pps/clients/pps-gpio.c +4 -33
index 7d87481280b21..73ec2c7335e51 100644
@@ -31,7 +31,6 @@ struct pps_gpio_device_data {
 	struct gpio_desc *echo_pin;
 	struct timer_list echo_timer;	/* timer to reset echo active state */
 	bool assert_falling_edge;
-	bool capture_clear;
 	unsigned int echo_active_ms;	/* PPS echo active duration */
 	unsigned long echo_timeout;	/* timer timeout value in jiffies */
 	struct pps_event_time ts;	/* timestamp captured in hardirq */
@@ -61,19 +60,8 @@ static irqreturn_t pps_gpio_irq_hardirq(int irq, void *data)
 static irqreturn_t pps_gpio_irq_thread(int irq, void *data)
 {
 	struct pps_gpio_device_data *info = data;
-	int rising_edge;
-
-	rising_edge = info->capture_clear ?
-		      gpiod_get_value(info->gpio_pin) : !info->assert_falling_edge;
-	if ((rising_edge && !info->assert_falling_edge) ||
-			(!rising_edge && info->assert_falling_edge))
-		pps_event(info->pps, &info->ts, PPS_CAPTUREASSERT, data);
-	else if (info->capture_clear &&
-			((rising_edge && info->assert_falling_edge) ||
-			(!rising_edge && !info->assert_falling_edge)))
-		pps_event(info->pps, &info->ts, PPS_CAPTURECLEAR, data);
-	else
-		dev_warn_ratelimited(&info->pps->dev, "IRQ did not trigger any PPS event\n");
+
+	pps_event(info->pps, &info->ts, PPS_CAPTUREASSERT, data);
 
 	return IRQ_HANDLED;
 }
@@ -89,11 +77,6 @@ static void pps_gpio_echo(struct pps_device *pps, int event, void *data)
 		if (pps->params.mode & PPS_ECHOASSERT)
 			gpiod_set_value(info->echo_pin, 1);
 		break;
-
-	case PPS_CAPTURECLEAR:
-		if (pps->params.mode & PPS_ECHOCLEAR)
-			gpiod_set_value(info->echo_pin, 1);
-		break;
 	}
 
 	/* fire the timer */
@@ -155,15 +138,8 @@ static int pps_gpio_setup(struct device *dev)
 static unsigned long
 get_irqf_trigger_flags(const struct pps_gpio_device_data *data)
 {
-	unsigned long flags = data->assert_falling_edge ?
-		IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
-
-	if (data->capture_clear) {
-		flags |= ((flags & IRQF_TRIGGER_RISING) ?
-				IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING);
-	}
-
-	return flags;
+	return data->assert_falling_edge ? IRQF_TRIGGER_FALLING :
+					   IRQF_TRIGGER_RISING;
 }
 
 static int pps_gpio_probe(struct platform_device *pdev)
@@ -196,9 +172,6 @@ static int pps_gpio_probe(struct platform_device *pdev)
 	/* initialize PPS specific parts of the bookkeeping data structure. */
 	data->info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
 		PPS_ECHOASSERT | PPS_CANWAIT | PPS_TSFMT_TSPEC;
-	if (data->capture_clear)
-		data->info.mode |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR |
-			PPS_ECHOCLEAR;
 	data->info.owner = THIS_MODULE;
 	snprintf(data->info.name, PPS_MAX_NAME_LEN - 1, "%s.%d",
 		 pdev->name, pdev->id);
@@ -210,8 +183,6 @@ static int pps_gpio_probe(struct platform_device *pdev)
 
 	/* register PPS source */
 	pps_default_params = PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
-	if (data->capture_clear)
-		pps_default_params |= PPS_CAPTURECLEAR | PPS_OFFSETCLEAR;
 	data->pps = pps_register_source(&data->info, pps_default_params);
 	if (IS_ERR(data->pps)) {
 		dev_err(dev, "failed to register IRQ %d as PPS source\n",
Signature · 1 line
2.47.3