ACPI: EC: Clear GPE on interrupt handling only
On multiple devices I work on, we noticed that /sys/firmware/acpi/interrupts/sci_not is non-zero and keeps increasing over time.
It turns out that there is a race condition between servicing a GPE interrupt and handling task driven transactions.
If a GPE interrupt is received at the same time ec_poll() is running, the advance_transaction() clears the GPE flag and the interrupt is not serviced as acpi_ev_detect_gpe() relies on the GPE flag to call the handler. As a result, `sci_not' is increased.
--- drivers/acpi/ec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/acpi/ec.c +1 -1
@@ -674,7 +674,7 @@ static void advance_transaction(struct acpi_ec *ec, bool interrupt)* 2. As long as software can ensure only clearing it when it is set, * hardware won't set it in parallel. */ -if (ec->gpe >=0 &&acpi_ec_gpe_status_set (ec)) +if (interrupt && ec->gpe >=0 &&acpi_ec_gpe_status_set (ec))acpi_clear_gpe (NULL , ec->gpe); status =acpi_ec_read_status (ec);
Signature · 1 line
2.40.1
Rafael J. Wysocki
maintainerQuoted 5 lines · Jeremy
On multiple devices I work on, we noticed that
/sys/firmware/acpi/interrupts/sci_notis non-zero and keeps increasing over time.
Quoted 7 lines
It turns out that there is a race condition between servicing a GPE interrupt and handling task driven transactions.
If a GPE interrupt is received at the same time
ec_poll()is running, theadvance_transaction()clears the GPE flag and the interrupt is not serviced asacpi_ev_detect_gpe()relies on the GPE flag to call the handler. As a result, `sci_not' is increased.
And if I'm not mistaken, it is not necessary to run the entire interrupt handler in that case, because the currently running advance_transaction() will take care of the pending event anyway.
I agree that it is confusing to increase sci_not in that case, but I'm not sure if running the entire advance_transaction() for the same transaction twice in a row, once from ec_poll() and once from the interrupt handler is entirely correct.
Quoted 21 lines
Signed-off-by Jeremy Compostella <[email protected]>--- drivers/acpi/ec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/acpi/ec.c +1
-2@@ -674,7 +674,7 @@ static void advance_transaction(struct acpi_ec *ec, bool interrupt)* 2. As long as software can ensure only clearing it when it is set, * hardware won't set it in parallel. */ -if (ec->gpe >=0 &&acpi_ec_gpe_status_set (ec)) +if (interrupt && ec->gpe >=0 &&acpi_ec_gpe_status_set (ec))acpi_clear_gpe (NULL , ec->gpe); status =acpi_ec_read_status (ec); --2.40.1
Rafael J. Wysocki
maintainerQuoted 16 lines · Rafael J. Wysocki
Quoted 5 lines · Jeremy
On multiple devices I work on, we noticed that
/sys/firmware/acpi/interrupts/sci_notis non-zero and keeps increasing over time.Quoted 7 lines
It turns out that there is a race condition between servicing a GPE interrupt and handling task driven transactions.
If a GPE interrupt is received at the same time
ec_poll()is running, theadvance_transaction()clears the GPE flag and the interrupt is not serviced asacpi_ev_detect_gpe()relies on the GPE flag to call the handler. As a result, `sci_not' is increased.
Quoted 8 lines
And if I'm not mistaken, it is not necessary to run the entire interrupt handler in that case, because the currently running
advance_transaction()will take care of the pending event anyway.I agree that it is confusing to increase
sci_notin that case, but I'm not sure if running the entireadvance_transaction()for the same transaction twice in a row, once fromec_poll()and once from the interrupt handler is entirely correct.
However, if the interrupt handler wins the race, advance_transaction() will run for the same transaction twice in a row anyway, so this change will only make it happen more often.
So no objections, but I would move the GPE clearing piece directly into acpi_ec_handle_interrupt(), because it will only be needed there and it doesn't depend on anything else in advance_transaction().
Quoted 19 lines
Quoted 19 lines
Signed-off-by Jeremy Compostella <[email protected]>--- drivers/acpi/ec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/acpi/ec.c +1
-2@@ -674,7 +674,7 @@ static void advance_transaction(struct acpi_ec *ec, bool interrupt)* 2. As long as software can ensure only clearing it when it is set, * hardware won't set it in parallel. */ -if (ec->gpe >=0 &&acpi_ec_gpe_status_set (ec)) +if (interrupt && ec->gpe >=0 &&acpi_ec_gpe_status_set (ec))acpi_clear_gpe (NULL , ec->gpe); status =acpi_ec_read_status (ec); --
Quoted 26 lines · Rafael J. Wysocki
Quoted 16 lines · Rafael J. Wysocki
Quoted 5 lines · Jeremy
On multiple devices I work on, we noticed that
/sys/firmware/acpi/interrupts/sci_notis non-zero and keeps increasing over time.Quoted 7 lines
It turns out that there is a race condition between servicing a GPE interrupt and handling task driven transactions.
If a GPE interrupt is received at the same time
ec_poll()is running, theadvance_transaction()clears the GPE flag and the interrupt is not serviced asacpi_ev_detect_gpe()relies on the GPE flag to call the handler. As a result, `sci_not' is increased.Quoted 8 lines
And if I'm not mistaken, it is not necessary to run the entire interrupt handler in that case, because the currently running
advance_transaction()will take care of the pending event anyway.I agree that it is confusing to increase
sci_notin that case, but I'm not sure if running the entireadvance_transaction()for the same transaction twice in a row, once fromec_poll()and once from the interrupt handler is entirely correct.
Quoted 7 lines
However, if the interrupt handler wins the race,
advance_transaction()will run for the same transaction twice in a row anyway, so this change will only make it happen more often.So no objections, but I would move the GPE clearing piece directly into
acpi_ec_handle_interrupt(), because it will only be needed there and it doesn't depend on anything else inadvance_transaction().
I took into account your suggestion (cf. patch in attachment).
From 42fa736fcd5d6a2e17c550f493a12e8df2e7cd72 Mon Sep 17 00:00:00 2001
From: Jeremy Compostella <[email protected]>
Date: Mon, 15 May 2023 16:49:19 -0700
Subject: [PATCH] ACPI: EC: Clear GPE on interrupt handling only
On multiple devices I work on, we noticed that /sys/firmware/acpi/interrupts/sci_not is non-zero and keeps increasing over time.
It turns out that there is a race condition between servicing a GPE interrupt and handling task driven transactions.
If a GPE interrupt is received at the same time ec_poll() is running, the advance_transaction() clears the GPE flag and the interrupt is not serviced as acpi_ev_detect_gpe() relies on the GPE flag to call the handler. As a result, `sci_not' is increased.
--- drivers/acpi/ec.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)
drivers/acpi/ec.c +16 -15
@@ -662,21 +662,6 @@ static void advance_transaction(struct acpi_ec *ec, bool interrupt)ec_dbg_stm ("%s (%d)" , interrupt ?"IRQ" :"TASK" ,smp_processor_id ()); -/* -* Clear GPE_STS upfront to allow subsequent hardware GPE_STS 0->1 -* changes to always trigger a GPE interrupt. -* -* GPE STS is a W1C register, which means: -* -* 1. Software can clear it without worrying about clearing the other -* GPEs' STS bits when the hardware sets them in parallel. -* -* 2. As long as software can ensure only clearing it when it is set, -* hardware won't set it in parallel. -*/ -if (ec->gpe >=0 &&acpi_ec_gpe_status_set (ec)) -acpi_clear_gpe (NULL , ec->gpe); - status =acpi_ec_read_status (ec);/*
@@ -1287,6 +1272,22 @@ static void acpi_ec_handle_interrupt(struct acpi_ec *ec)unsigned long flags;spin_lock_irqsave (&ec->lock, flags); + +/* +* Clear GPE_STS upfront to allow subsequent hardware GPE_STS 0->1 +* changes to always trigger a GPE interrupt. +* +* GPE STS is a W1C register, which means: +* +* 1. Software can clear it without worrying about clearing the other +* GPEs' STS bits when the hardware sets them in parallel. +* +* 2. As long as software can ensure only clearing it when it is set, +* hardware won't set it in parallel. +*/ +if (ec->gpe >=0 &&acpi_ec_gpe_status_set (ec)) +acpi_clear_gpe (NULL , ec->gpe); +advance_transaction (ec, true);spin_unlock_irqrestore (&ec->lock, flags); }
Signature · 1 line
2.40.1
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Signed-off-by Jeremy Compostella <[email protected]>--- drivers/acpi/ec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/acpi/ec.c +1
-2@@ -674,7 +674,7 @@ static void advance_transaction(struct acpi_ec *ec, bool interrupt)* 2. As long as software can ensure only clearing it when it is set, * hardware won't set it in parallel. */ -if (ec->gpe >=0 &&acpi_ec_gpe_status_set (ec)) +if (interrupt && ec->gpe >=0 &&acpi_ec_gpe_status_set (ec))acpi_clear_gpe (NULL , ec->gpe); status =acpi_ec_read_status (ec); --
Rafael J. Wysocki
maintainerQuoted 33 lines · Jeremy
Quoted 30 lines · Rafael J. Wysocki
Quoted 16 lines · Rafael J. Wysocki
Quoted 5 lines · Jeremy
On multiple devices I work on, we noticed that
/sys/firmware/acpi/interrupts/sci_notis non-zero and keeps increasing over time.Quoted 7 lines
It turns out that there is a race condition between servicing a GPE interrupt and handling task driven transactions.
If a GPE interrupt is received at the same time
ec_poll()is running, theadvance_transaction()clears the GPE flag and the interrupt is not serviced asacpi_ev_detect_gpe()relies on the GPE flag to call the handler. As a result, `sci_not' is increased.Quoted 8 lines
And if I'm not mistaken, it is not necessary to run the entire interrupt handler in that case, because the currently running
advance_transaction()will take care of the pending event anyway.I agree that it is confusing to increase
sci_notin that case, but I'm not sure if running the entireadvance_transaction()for the same transaction twice in a row, once fromec_poll()and once from the interrupt handler is entirely correct.However, if the interrupt handler wins the race,
advance_transaction()will run for the same transaction twice in a row anyway, so this change will only make it happen more often.
Quoted 5 lines
Quoted 3 lines
So no objections, but I would move the GPE clearing piece directly into
acpi_ec_handle_interrupt(), because it will only be needed there and it doesn't depend on anything else inadvance_transaction().I took into account your suggestion (cf. patch in attachment).
Yes, this is what I meant.
I think that you can resend it as a v2 with the same changelog.
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Signed-off-by Jeremy Compostella <[email protected]>--- drivers/acpi/ec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/acpi/ec.c +1
-2@@ -674,7 +674,7 @@ static void advance_transaction(struct acpi_ec *ec, bool interrupt)* 2. As long as software can ensure only clearing it when it is set, * hardware won't set it in parallel. */ -if (ec->gpe >=0 &&acpi_ec_gpe_status_set (ec)) +if (interrupt && ec->gpe >=0 &&acpi_ec_gpe_status_set (ec))acpi_clear_gpe (NULL , ec->gpe); status =acpi_ec_read_status (ec); --