diff mbox series

[3/3] input: gpio-keys: Use hrtimer for software debounce

Message ID 20210305170111.214782-3-paul@crapouillou.net (mailing list archive)
State Superseded
Headers show
Series [1/3] input: gpio-keys: Remove extra call to input_sync | expand

Commit Message

Paul Cercueil March 5, 2021, 5:01 p.m. UTC
We want to be able to report the input event as soon as the debounce
delay elapsed. However, the current code does not really ensure that,
as it uses the jiffies-based schedule_delayed_work() API. With a small
enough HZ value (HZ <= 100), this results in some input events being
lost, when a key is quickly pressed then released (on a human's time
scale).

Switching to hrtimers fixes this issue, and will work even on extremely
low HZ values (tested at HZ=24).

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
---
 drivers/input/keyboard/gpio_keys.c | 33 +++++++++++++++---------------
 1 file changed, 17 insertions(+), 16 deletions(-)

Comments

Dmitry Torokhov March 5, 2021, 6:35 p.m. UTC | #1
Hi Paul,

On Fri, Mar 05, 2021 at 05:01:11PM +0000, Paul Cercueil wrote:
> -static void gpio_keys_gpio_work_func(struct work_struct *work)
> +static enum hrtimer_restart gpio_keys_debounce_timer(struct hrtimer *t)
>  {
> -	struct gpio_button_data *bdata =
> -		container_of(work, struct gpio_button_data, work.work);
> +	struct gpio_button_data *bdata = container_of(t,
> +						      struct gpio_button_data,
> +						      debounce_timer);
>  
>  	gpio_keys_gpio_report_event(bdata);

I am not sure how this works. As far as I know, even
HRTIMER_MODE_REL_SOFT do not allow sleeping in the timer handlers, and
gpio_keys_gpio_report_event() use sleeping variant of GPIOD API (and
that is not going to change).

It seems to me that if you want to use software debounce in gpio keys
driver you need to set up sufficiently high HZ for your system. Maybe we
could thrown a warning when we see low debounce delay and low HZ to
alert system developer.

Thanks.
Paul Cercueil March 5, 2021, 8 p.m. UTC | #2
Hi Dmitry,

Le ven. 5 mars 2021 à 10:35, Dmitry Torokhov 
<dmitry.torokhov@gmail.com> a écrit :
> Hi Paul,
> 
> On Fri, Mar 05, 2021 at 05:01:11PM +0000, Paul Cercueil wrote:
>>  -static void gpio_keys_gpio_work_func(struct work_struct *work)
>>  +static enum hrtimer_restart gpio_keys_debounce_timer(struct 
>> hrtimer *t)
>>   {
>>  -	struct gpio_button_data *bdata =
>>  -		container_of(work, struct gpio_button_data, work.work);
>>  +	struct gpio_button_data *bdata = container_of(t,
>>  +						      struct gpio_button_data,
>>  +						      debounce_timer);
>> 
>>   	gpio_keys_gpio_report_event(bdata);
> 
> I am not sure how this works. As far as I know, even
> HRTIMER_MODE_REL_SOFT do not allow sleeping in the timer handlers, and
> gpio_keys_gpio_report_event() use sleeping variant of GPIOD API (and
> that is not going to change).

Quoting <linux/hrtimers.h>, the "timer callback will be executed in 
soft irq context", so sleeping should be possible.

But I guess in this case I can use HRTIMER_MODE_REL.

> It seems to me that if you want to use software debounce in gpio keys
> driver you need to set up sufficiently high HZ for your system. Maybe 
> we
> could thrown a warning when we see low debounce delay and low HZ to
> alert system developer.

This is exactly what we should not do. I certainly don't want to have 
250+ timer interrupts per second just so that input events aren't lost, 
to work around a sucky debounce implementation. Besides, if you 
consider the hrtimers doc (Documentation/timers/hrtimers.rst), hrtimers 
really are what should be used here.

-Paul
Dmitry Torokhov March 7, 2021, 8:20 p.m. UTC | #3
On Fri, Mar 05, 2021 at 08:00:43PM +0000, Paul Cercueil wrote:
> Hi Dmitry,
> 
> Le ven. 5 mars 2021 à 10:35, Dmitry Torokhov <dmitry.torokhov@gmail.com> a
> écrit :
> > Hi Paul,
> > 
> > On Fri, Mar 05, 2021 at 05:01:11PM +0000, Paul Cercueil wrote:
> > >  -static void gpio_keys_gpio_work_func(struct work_struct *work)
> > >  +static enum hrtimer_restart gpio_keys_debounce_timer(struct
> > > hrtimer *t)
> > >   {
> > >  -	struct gpio_button_data *bdata =
> > >  -		container_of(work, struct gpio_button_data, work.work);
> > >  +	struct gpio_button_data *bdata = container_of(t,
> > >  +						      struct gpio_button_data,
> > >  +						      debounce_timer);
> > > 
> > >   	gpio_keys_gpio_report_event(bdata);
> > 
> > I am not sure how this works. As far as I know, even
> > HRTIMER_MODE_REL_SOFT do not allow sleeping in the timer handlers, and
> > gpio_keys_gpio_report_event() use sleeping variant of GPIOD API (and
> > that is not going to change).
> 
> Quoting <linux/hrtimers.h>, the "timer callback will be executed in soft irq
> context", so sleeping should be possible.

I am afraid you misunderstand what soft irq context is, as softirqs and
tasklets still run in interrupt context and therefore can not sleep,
only code running in process context may sleep.

You can test it yourself by sticking "msleep(1)" in
gpio_keys_debounce_timer() and see if you will get "scheduling while
atomic" in logs.

> 
> But I guess in this case I can use HRTIMER_MODE_REL.

This changes selected clock source, but has no effect on whether timer
handler can sleep or not.

> 
> > It seems to me that if you want to use software debounce in gpio keys
> > driver you need to set up sufficiently high HZ for your system. Maybe we
> > could thrown a warning when we see low debounce delay and low HZ to
> > alert system developer.
> 
> This is exactly what we should not do. I certainly don't want to have 250+
> timer interrupts per second just so that input events aren't lost, to work
> around a sucky debounce implementation. Besides, if you consider the
> hrtimers doc (Documentation/timers/hrtimers.rst), hrtimers really are what
> should be used here.

I explained why they can't. They could be if you restrict gpio_keys to
only be used with GPIOs that do not require sleep to read their state,
but I am not willing to accept such restriction. You either need to have
longer debounce, higher HZ, or see if you can use GPIO controller that
supports debounce handling. See also if you can enable dynamic
ticks/NO_HZ to limit number of timer interrupts on idle system.

Thanks.
Paul Cercueil March 7, 2021, 9:11 p.m. UTC | #4
Hi Dmitry,

Le dim. 7 mars 2021 à 12:20, Dmitry Torokhov 
<dmitry.torokhov@gmail.com> a écrit :
> On Fri, Mar 05, 2021 at 08:00:43PM +0000, Paul Cercueil wrote:
>>  Hi Dmitry,
>> 
>>  Le ven. 5 mars 2021 à 10:35, Dmitry Torokhov 
>> <dmitry.torokhov@gmail.com> a
>>  écrit :
>>  > Hi Paul,
>>  >
>>  > On Fri, Mar 05, 2021 at 05:01:11PM +0000, Paul Cercueil wrote:
>>  > >  -static void gpio_keys_gpio_work_func(struct work_struct *work)
>>  > >  +static enum hrtimer_restart gpio_keys_debounce_timer(struct
>>  > > hrtimer *t)
>>  > >   {
>>  > >  -	struct gpio_button_data *bdata =
>>  > >  -		container_of(work, struct gpio_button_data, work.work);
>>  > >  +	struct gpio_button_data *bdata = container_of(t,
>>  > >  +						      struct gpio_button_data,
>>  > >  +						      debounce_timer);
>>  > >
>>  > >   	gpio_keys_gpio_report_event(bdata);
>>  >
>>  > I am not sure how this works. As far as I know, even
>>  > HRTIMER_MODE_REL_SOFT do not allow sleeping in the timer 
>> handlers, and
>>  > gpio_keys_gpio_report_event() use sleeping variant of GPIOD API 
>> (and
>>  > that is not going to change).
>> 
>>  Quoting <linux/hrtimers.h>, the "timer callback will be executed in 
>> soft irq
>>  context", so sleeping should be possible.
> 
> I am afraid you misunderstand what soft irq context is, as softirqs 
> and
> tasklets still run in interrupt context and therefore can not sleep,
> only code running in process context may sleep.

I probably do. My understanding of "softirq" is that the callback runs 
in a threaded interrupt handler.

> You can test it yourself by sticking "msleep(1)" in
> gpio_keys_debounce_timer() and see if you will get "scheduling while
> atomic" in logs.

I tested it, it locks up.

>> 
>>  But I guess in this case I can use HRTIMER_MODE_REL.
> 
> This changes selected clock source, but has no effect on whether timer
> handler can sleep or not.
> 
>> 
>>  > It seems to me that if you want to use software debounce in gpio 
>> keys
>>  > driver you need to set up sufficiently high HZ for your system. 
>> Maybe we
>>  > could thrown a warning when we see low debounce delay and low HZ 
>> to
>>  > alert system developer.
>> 
>>  This is exactly what we should not do. I certainly don't want to 
>> have 250+
>>  timer interrupts per second just so that input events aren't lost, 
>> to work
>>  around a sucky debounce implementation. Besides, if you consider the
>>  hrtimers doc (Documentation/timers/hrtimers.rst), hrtimers really 
>> are what
>>  should be used here.
> 
> I explained why they can't. They could be if you restrict gpio_keys to
> only be used with GPIOs that do not require sleep to read their state,
> but I am not willing to accept such restriction. You either need to 
> have
> longer debounce, higher HZ, or see if you can use GPIO controller that
> supports debounce handling. See also if you can enable dynamic
> ticks/NO_HZ to limit number of timer interrupts on idle system.

We can also use the hrtimer approach if the GPIO doesn't require sleep, 
and fall back to the standard timer if it does. It's possible to detect 
that with gpiod_cansleep(). The diff would be pretty slim. Would you 
accept something like that?

Switching from HZ=250 to HZ=24 leads to a 3% overall performance 
increase across all apps on our system, so a pretty big optimization, 
and this is the only blocker.

Cheers,
-Paul
Dmitry Torokhov March 7, 2021, 9:47 p.m. UTC | #5
On Sun, Mar 07, 2021 at 09:11:18PM +0000, Paul Cercueil wrote:
> Hi Dmitry,
> 
> Le dim. 7 mars 2021 à 12:20, Dmitry Torokhov <dmitry.torokhov@gmail.com> a
> écrit :
> > On Fri, Mar 05, 2021 at 08:00:43PM +0000, Paul Cercueil wrote:
> > >  Hi Dmitry,
> > > 
> > >  Le ven. 5 mars 2021 à 10:35, Dmitry Torokhov
> > > <dmitry.torokhov@gmail.com> a
> > >  écrit :
> > >  > Hi Paul,
> > >  >
> > >  > On Fri, Mar 05, 2021 at 05:01:11PM +0000, Paul Cercueil wrote:
> > >  > >  -static void gpio_keys_gpio_work_func(struct work_struct *work)
> > >  > >  +static enum hrtimer_restart gpio_keys_debounce_timer(struct
> > >  > > hrtimer *t)
> > >  > >   {
> > >  > >  -	struct gpio_button_data *bdata =
> > >  > >  -		container_of(work, struct gpio_button_data, work.work);
> > >  > >  +	struct gpio_button_data *bdata = container_of(t,
> > >  > >  +						      struct gpio_button_data,
> > >  > >  +						      debounce_timer);
> > >  > >
> > >  > >   	gpio_keys_gpio_report_event(bdata);
> > >  >
> > >  > I am not sure how this works. As far as I know, even
> > >  > HRTIMER_MODE_REL_SOFT do not allow sleeping in the timer
> > > handlers, and
> > >  > gpio_keys_gpio_report_event() use sleeping variant of GPIOD API
> > > (and
> > >  > that is not going to change).
> > > 
> > >  Quoting <linux/hrtimers.h>, the "timer callback will be executed in
> > > soft irq
> > >  context", so sleeping should be possible.
> > 
> > I am afraid you misunderstand what soft irq context is, as softirqs and
> > tasklets still run in interrupt context and therefore can not sleep,
> > only code running in process context may sleep.
> 
> I probably do. My understanding of "softirq" is that the callback runs in a
> threaded interrupt handler.

No, you are thinking about threaded interrupts, which are separate
beasts. Softirqs are traditional bottom halfs that run after exit of
hard interrupt, but still are non-preemptible so sleeping is not
allowed.

> 
> > You can test it yourself by sticking "msleep(1)" in
> > gpio_keys_debounce_timer() and see if you will get "scheduling while
> > atomic" in logs.
> 
> I tested it, it locks up.
> 
> > > 
> > >  But I guess in this case I can use HRTIMER_MODE_REL.
> > 
> > This changes selected clock source, but has no effect on whether timer
> > handler can sleep or not.
> > 
> > > 
> > >  > It seems to me that if you want to use software debounce in gpio
> > > keys
> > >  > driver you need to set up sufficiently high HZ for your system.
> > > Maybe we
> > >  > could thrown a warning when we see low debounce delay and low HZ
> > > to
> > >  > alert system developer.
> > > 
> > >  This is exactly what we should not do. I certainly don't want to
> > > have 250+
> > >  timer interrupts per second just so that input events aren't lost,
> > > to work
> > >  around a sucky debounce implementation. Besides, if you consider the
> > >  hrtimers doc (Documentation/timers/hrtimers.rst), hrtimers really
> > > are what
> > >  should be used here.
> > 
> > I explained why they can't. They could be if you restrict gpio_keys to
> > only be used with GPIOs that do not require sleep to read their state,
> > but I am not willing to accept such restriction. You either need to have
> > longer debounce, higher HZ, or see if you can use GPIO controller that
> > supports debounce handling. See also if you can enable dynamic
> > ticks/NO_HZ to limit number of timer interrupts on idle system.
> 
> We can also use the hrtimer approach if the GPIO doesn't require sleep, and
> fall back to the standard timer if it does. It's possible to detect that
> with gpiod_cansleep(). The diff would be pretty slim. Would you accept
> something like that?
> 
> Switching from HZ=250 to HZ=24 leads to a 3% overall performance increase
> across all apps on our system, so a pretty big optimization, and this is the
> only blocker.

Let me take a look at the updated patch and we will see.

Thanks.
diff mbox series

Patch

diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
index 1ab112267aa8..267ed99e1911 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -22,7 +22,6 @@ 
 #include <linux/platform_device.h>
 #include <linux/input.h>
 #include <linux/gpio_keys.h>
-#include <linux/workqueue.h>
 #include <linux/gpio.h>
 #include <linux/gpio/consumer.h>
 #include <linux/of.h>
@@ -40,7 +39,7 @@  struct gpio_button_data {
 	struct hrtimer release_timer;
 	unsigned int release_delay;	/* in msecs, for IRQ-only buttons */
 
-	struct delayed_work work;
+	struct hrtimer debounce_timer;
 	unsigned int software_debounce;	/* in msecs, for GPIO-driven buttons */
 
 	unsigned int irq;
@@ -145,7 +144,7 @@  static void gpio_keys_disable_button(struct gpio_button_data *bdata)
 		disable_irq(bdata->irq);
 
 		if (bdata->gpiod)
-			cancel_delayed_work_sync(&bdata->work);
+			hrtimer_cancel(&bdata->debounce_timer);
 		else
 			hrtimer_cancel(&bdata->release_timer);
 
@@ -377,15 +376,18 @@  static void gpio_keys_gpio_report_event(struct gpio_button_data *bdata)
 	input_sync(input);
 }
 
-static void gpio_keys_gpio_work_func(struct work_struct *work)
+static enum hrtimer_restart gpio_keys_debounce_timer(struct hrtimer *t)
 {
-	struct gpio_button_data *bdata =
-		container_of(work, struct gpio_button_data, work.work);
+	struct gpio_button_data *bdata = container_of(t,
+						      struct gpio_button_data,
+						      debounce_timer);
 
 	gpio_keys_gpio_report_event(bdata);
 
 	if (bdata->button->wakeup)
 		pm_relax(bdata->input->dev.parent);
+
+	return HRTIMER_NORESTART;
 }
 
 static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
@@ -409,9 +411,9 @@  static irqreturn_t gpio_keys_gpio_isr(int irq, void *dev_id)
 		}
 	}
 
-	mod_delayed_work(system_wq,
-			 &bdata->work,
-			 msecs_to_jiffies(bdata->software_debounce));
+	hrtimer_start(&bdata->debounce_timer,
+		      ms_to_ktime(bdata->software_debounce),
+		      HRTIMER_MODE_REL_SOFT);
 
 	return IRQ_HANDLED;
 }
@@ -472,7 +474,7 @@  static void gpio_keys_quiesce_key(void *data)
 	struct gpio_button_data *bdata = data;
 
 	if (bdata->gpiod)
-		cancel_delayed_work_sync(&bdata->work);
+		hrtimer_cancel(&bdata->debounce_timer);
 	else
 		hrtimer_cancel(&bdata->release_timer);
 }
@@ -562,11 +564,13 @@  static int gpio_keys_setup_key(struct platform_device *pdev,
 			bdata->irq = irq;
 		}
 
-		INIT_DELAYED_WORK(&bdata->work, gpio_keys_gpio_work_func);
-
 		isr = gpio_keys_gpio_isr;
 		irqflags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
 
+		hrtimer_init(&bdata->debounce_timer,
+			     CLOCK_REALTIME, HRTIMER_MODE_REL_SOFT);
+		bdata->debounce_timer.function = gpio_keys_debounce_timer;
+
 		switch (button->wakeup_event_action) {
 		case EV_ACT_ASSERTED:
 			bdata->wakeup_trigger_type = active_low ?
@@ -615,10 +619,7 @@  static int gpio_keys_setup_key(struct platform_device *pdev,
 	*bdata->code = button->code;
 	input_set_capability(input, button->type ?: EV_KEY, *bdata->code);
 
-	/*
-	 * Install custom action to cancel release timer and
-	 * workqueue item.
-	 */
+	/* Install custom action to cancel timers. */
 	error = devm_add_action(dev, gpio_keys_quiesce_key, bdata);
 	if (error) {
 		dev_err(dev, "failed to register quiesce action, error: %d\n",