[PATCH v2 2/3] ACPI: APEI: GHES: Extract helper functions for error status handling

Shuai Xue posted 3 patches 1 month ago
There is a newer version of this series
[PATCH v2 2/3] ACPI: APEI: GHES: Extract helper functions for error status handling
Posted by Shuai Xue 1 month ago
Refactors the GHES driver by extracting common functionality into
reusable helper functions:

1. ghes_has_active_errors() - Checks if any error sources in a given list
   have active errors
2. ghes_map_error_status() - Maps error status address to virtual address
3. ghes_unmap_error_status() - Unmaps error status virtual address

These helpers eliminate code duplication in the NMI path and prepare for
similar usage in the SEA path in a subsequent patch.

No functional change intended.

Tested-by: Tony Luck <tony.luck@intel.com>
Reviewed-by: Tony Luck <tony.luck@intel.com>
Signed-off-by: Shuai Xue <xueshuai@linux.alibaba.com>
---
 drivers/acpi/apei/ghes.c | 93 +++++++++++++++++++++++++++++++---------
 1 file changed, 72 insertions(+), 21 deletions(-)

diff --git a/drivers/acpi/apei/ghes.c b/drivers/acpi/apei/ghes.c
index 0e97d50b0240..7600063fe263 100644
--- a/drivers/acpi/apei/ghes.c
+++ b/drivers/acpi/apei/ghes.c
@@ -1406,6 +1406,75 @@ static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
 	return ret;
 }
 
+/**
+ * ghes_has_active_errors - Check if there are active errors in error sources
+ * @ghes_list: List of GHES entries to check for active errors
+ *
+ * This function iterates through all GHES entries in the given list and
+ * checks if any of them has active error status by reading the error
+ * status register.
+ *
+ * Return: true if at least one source has active error, false otherwise.
+ */
+static bool __maybe_unused ghes_has_active_errors(struct list_head *ghes_list)
+{
+	bool active_error = false;
+	struct ghes *ghes;
+
+	rcu_read_lock();
+	list_for_each_entry_rcu(ghes, ghes_list, list) {
+		if (ghes->error_status_vaddr &&
+		    readl(ghes->error_status_vaddr)) {
+			active_error = true;
+			break;
+		}
+	}
+	rcu_read_unlock();
+
+	return active_error;
+}
+
+/**
+ * ghes_map_error_status - Map error status address to virtual address
+ * @ghes: pointer to GHES structure
+ *
+ * Reads the error status address from ACPI HEST table and maps it to a virtual
+ * address that can be accessed by the kernel.
+ *
+ * Return: 0 on success, error code on failure.
+ */
+static int __maybe_unused ghes_map_error_status(struct ghes *ghes)
+{
+	struct acpi_hest_generic *g = ghes->generic;
+	u64 paddr;
+	int rc;
+
+	rc = apei_read(&paddr, &g->error_status_address);
+	if (rc)
+		return rc;
+
+	ghes->error_status_vaddr =
+		acpi_os_ioremap(paddr, sizeof(ghes->estatus->block_status));
+	if (!ghes->error_status_vaddr)
+		return -EINVAL;
+
+	return 0;
+}
+
+/**
+ * ghes_unmap_error_status - Unmap error status virtual address
+ * @ghes: pointer to GHES structure
+ *
+ * Unmaps the error status address if it was previously mapped.
+ */
+static void __maybe_unused ghes_unmap_error_status(struct ghes *ghes)
+{
+	if (ghes->error_status_vaddr) {
+		iounmap(ghes->error_status_vaddr);
+		ghes->error_status_vaddr = NULL;
+	}
+}
+
 #ifdef CONFIG_ACPI_APEI_SEA
 static LIST_HEAD(ghes_sea);
 
@@ -1456,20 +1525,9 @@ static LIST_HEAD(ghes_nmi);
 static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
 {
 	static DEFINE_RAW_SPINLOCK(ghes_notify_lock_nmi);
-	bool active_error = false;
 	int ret = NMI_DONE;
-	struct ghes *ghes;
-
-	rcu_read_lock();
-	list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
-		if (ghes->error_status_vaddr && readl(ghes->error_status_vaddr)) {
-			active_error = true;
-			break;
-		}
-	}
-	rcu_read_unlock();
 
-	if (!active_error)
+	if (!ghes_has_active_errors(&ghes_nmi))
 		return ret;
 
 	if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
@@ -1486,18 +1544,12 @@ static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
 
 static int ghes_nmi_add(struct ghes *ghes)
 {
-	struct acpi_hest_generic *g = ghes->generic;
-	u64 paddr;
 	int rc;
 
-	rc = apei_read(&paddr, &g->error_status_address);
+	rc = ghes_map_error_status(ghes);
 	if (rc)
 		return rc;
 
-	ghes->error_status_vaddr = acpi_os_ioremap(paddr, sizeof(ghes->estatus->block_status));
-	if (!ghes->error_status_vaddr)
-		return -EINVAL;
-
 	mutex_lock(&ghes_list_mutex);
 	if (list_empty(&ghes_nmi))
 		register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
@@ -1515,8 +1567,7 @@ static void ghes_nmi_remove(struct ghes *ghes)
 		unregister_nmi_handler(NMI_LOCAL, "ghes");
 	mutex_unlock(&ghes_list_mutex);
 
-	if (ghes->error_status_vaddr)
-		iounmap(ghes->error_status_vaddr);
+	ghes_unmap_error_status(ghes);
 
 	/*
 	 * To synchronize with NMI handler, ghes can only be
-- 
2.39.3
Re: [PATCH v2 2/3] ACPI: APEI: GHES: Extract helper functions for error status handling
Posted by Donglin Peng 1 month ago
On Sun, Jan 4, 2026 at 8:05 PM Shuai Xue <xueshuai@linux.alibaba.com> wrote:
>
> Refactors the GHES driver by extracting common functionality into
> reusable helper functions:
>
> 1. ghes_has_active_errors() - Checks if any error sources in a given list
>    have active errors
> 2. ghes_map_error_status() - Maps error status address to virtual address
> 3. ghes_unmap_error_status() - Unmaps error status virtual address
>
> These helpers eliminate code duplication in the NMI path and prepare for
> similar usage in the SEA path in a subsequent patch.
>
> No functional change intended.
>
> Tested-by: Tony Luck <tony.luck@intel.com>
> Reviewed-by: Tony Luck <tony.luck@intel.com>
> Signed-off-by: Shuai Xue <xueshuai@linux.alibaba.com>
> ---
>  drivers/acpi/apei/ghes.c | 93 +++++++++++++++++++++++++++++++---------
>  1 file changed, 72 insertions(+), 21 deletions(-)
>
> diff --git a/drivers/acpi/apei/ghes.c b/drivers/acpi/apei/ghes.c
> index 0e97d50b0240..7600063fe263 100644
> --- a/drivers/acpi/apei/ghes.c
> +++ b/drivers/acpi/apei/ghes.c
> @@ -1406,6 +1406,75 @@ static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
>         return ret;
>  }
>
> +/**
> + * ghes_has_active_errors - Check if there are active errors in error sources
> + * @ghes_list: List of GHES entries to check for active errors
> + *
> + * This function iterates through all GHES entries in the given list and
> + * checks if any of them has active error status by reading the error
> + * status register.
> + *
> + * Return: true if at least one source has active error, false otherwise.
> + */
> +static bool __maybe_unused ghes_has_active_errors(struct list_head *ghes_list)
> +{
> +       bool active_error = false;
> +       struct ghes *ghes;
> +
> +       rcu_read_lock();

Nit: Use `guard(rcu)()` instead of explicit
`rcu_read_lock()`/`rcu_read_unlock()`.

Thanks,
Donglin

> +       list_for_each_entry_rcu(ghes, ghes_list, list) {
> +               if (ghes->error_status_vaddr &&
> +                   readl(ghes->error_status_vaddr)) {
> +                       active_error = true;
> +                       break;
> +               }
> +       }
> +       rcu_read_unlock();
> +
> +       return active_error;
> +}
> +
> +/**
> + * ghes_map_error_status - Map error status address to virtual address
> + * @ghes: pointer to GHES structure
> + *
> + * Reads the error status address from ACPI HEST table and maps it to a virtual
> + * address that can be accessed by the kernel.
> + *
> + * Return: 0 on success, error code on failure.
> + */
> +static int __maybe_unused ghes_map_error_status(struct ghes *ghes)
> +{
> +       struct acpi_hest_generic *g = ghes->generic;
> +       u64 paddr;
> +       int rc;
> +
> +       rc = apei_read(&paddr, &g->error_status_address);
> +       if (rc)
> +               return rc;
> +
> +       ghes->error_status_vaddr =
> +               acpi_os_ioremap(paddr, sizeof(ghes->estatus->block_status));
> +       if (!ghes->error_status_vaddr)
> +               return -EINVAL;
> +
> +       return 0;
> +}
> +
> +/**
> + * ghes_unmap_error_status - Unmap error status virtual address
> + * @ghes: pointer to GHES structure
> + *
> + * Unmaps the error status address if it was previously mapped.
> + */
> +static void __maybe_unused ghes_unmap_error_status(struct ghes *ghes)
> +{
> +       if (ghes->error_status_vaddr) {
> +               iounmap(ghes->error_status_vaddr);
> +               ghes->error_status_vaddr = NULL;
> +       }
> +}
> +
>  #ifdef CONFIG_ACPI_APEI_SEA
>  static LIST_HEAD(ghes_sea);
>
> @@ -1456,20 +1525,9 @@ static LIST_HEAD(ghes_nmi);
>  static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
>  {
>         static DEFINE_RAW_SPINLOCK(ghes_notify_lock_nmi);
> -       bool active_error = false;
>         int ret = NMI_DONE;
> -       struct ghes *ghes;
> -
> -       rcu_read_lock();
> -       list_for_each_entry_rcu(ghes, &ghes_nmi, list) {
> -               if (ghes->error_status_vaddr && readl(ghes->error_status_vaddr)) {
> -                       active_error = true;
> -                       break;
> -               }
> -       }
> -       rcu_read_unlock();
>
> -       if (!active_error)
> +       if (!ghes_has_active_errors(&ghes_nmi))
>                 return ret;
>
>         if (!atomic_add_unless(&ghes_in_nmi, 1, 1))
> @@ -1486,18 +1544,12 @@ static int ghes_notify_nmi(unsigned int cmd, struct pt_regs *regs)
>
>  static int ghes_nmi_add(struct ghes *ghes)
>  {
> -       struct acpi_hest_generic *g = ghes->generic;
> -       u64 paddr;
>         int rc;
>
> -       rc = apei_read(&paddr, &g->error_status_address);
> +       rc = ghes_map_error_status(ghes);
>         if (rc)
>                 return rc;
>
> -       ghes->error_status_vaddr = acpi_os_ioremap(paddr, sizeof(ghes->estatus->block_status));
> -       if (!ghes->error_status_vaddr)
> -               return -EINVAL;
> -
>         mutex_lock(&ghes_list_mutex);
>         if (list_empty(&ghes_nmi))
>                 register_nmi_handler(NMI_LOCAL, ghes_notify_nmi, 0, "ghes");
> @@ -1515,8 +1567,7 @@ static void ghes_nmi_remove(struct ghes *ghes)
>                 unregister_nmi_handler(NMI_LOCAL, "ghes");
>         mutex_unlock(&ghes_list_mutex);
>
> -       if (ghes->error_status_vaddr)
> -               iounmap(ghes->error_status_vaddr);
> +       ghes_unmap_error_status(ghes);
>
>         /*
>          * To synchronize with NMI handler, ghes can only be
> --
> 2.39.3
>
>
Re: [PATCH v2 2/3] ACPI: APEI: GHES: Extract helper functions for error status handling
Posted by Shuai Xue 1 month ago

On 1/5/26 1:12 PM, Donglin Peng wrote:
> On Sun, Jan 4, 2026 at 8:05 PM Shuai Xue <xueshuai@linux.alibaba.com> wrote:
>>
>> Refactors the GHES driver by extracting common functionality into
>> reusable helper functions:
>>
>> 1. ghes_has_active_errors() - Checks if any error sources in a given list
>>     have active errors
>> 2. ghes_map_error_status() - Maps error status address to virtual address
>> 3. ghes_unmap_error_status() - Unmaps error status virtual address
>>
>> These helpers eliminate code duplication in the NMI path and prepare for
>> similar usage in the SEA path in a subsequent patch.
>>
>> No functional change intended.
>>
>> Tested-by: Tony Luck <tony.luck@intel.com>
>> Reviewed-by: Tony Luck <tony.luck@intel.com>
>> Signed-off-by: Shuai Xue <xueshuai@linux.alibaba.com>
>> ---
>>   drivers/acpi/apei/ghes.c | 93 +++++++++++++++++++++++++++++++---------
>>   1 file changed, 72 insertions(+), 21 deletions(-)
>>
>> diff --git a/drivers/acpi/apei/ghes.c b/drivers/acpi/apei/ghes.c
>> index 0e97d50b0240..7600063fe263 100644
>> --- a/drivers/acpi/apei/ghes.c
>> +++ b/drivers/acpi/apei/ghes.c
>> @@ -1406,6 +1406,75 @@ static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
>>          return ret;
>>   }
>>
>> +/**
>> + * ghes_has_active_errors - Check if there are active errors in error sources
>> + * @ghes_list: List of GHES entries to check for active errors
>> + *
>> + * This function iterates through all GHES entries in the given list and
>> + * checks if any of them has active error status by reading the error
>> + * status register.
>> + *
>> + * Return: true if at least one source has active error, false otherwise.
>> + */
>> +static bool __maybe_unused ghes_has_active_errors(struct list_head *ghes_list)
>> +{
>> +       bool active_error = false;
>> +       struct ghes *ghes;
>> +
>> +       rcu_read_lock();
> 
> Nit: Use `guard(rcu)()` instead of explicit
> `rcu_read_lock()`/`rcu_read_unlock()`.
> 
> Thanks,
> Donglin
> 

Hi, Donglin,

Good point. Will fix in next version.

Thanks.
Shuai
Re: [PATCH v2 2/3] ACPI: APEI: GHES: Extract helper functions for error status handling
Posted by Rafael J. Wysocki 1 month ago
On Mon, Jan 5, 2026 at 6:42 AM Shuai Xue <xueshuai@linux.alibaba.com> wrote:
>
>
>
> On 1/5/26 1:12 PM, Donglin Peng wrote:
> > On Sun, Jan 4, 2026 at 8:05 PM Shuai Xue <xueshuai@linux.alibaba.com> wrote:
> >>
> >> Refactors the GHES driver by extracting common functionality into
> >> reusable helper functions:
> >>
> >> 1. ghes_has_active_errors() - Checks if any error sources in a given list
> >>     have active errors
> >> 2. ghes_map_error_status() - Maps error status address to virtual address
> >> 3. ghes_unmap_error_status() - Unmaps error status virtual address
> >>
> >> These helpers eliminate code duplication in the NMI path and prepare for
> >> similar usage in the SEA path in a subsequent patch.
> >>
> >> No functional change intended.
> >>
> >> Tested-by: Tony Luck <tony.luck@intel.com>
> >> Reviewed-by: Tony Luck <tony.luck@intel.com>
> >> Signed-off-by: Shuai Xue <xueshuai@linux.alibaba.com>
> >> ---
> >>   drivers/acpi/apei/ghes.c | 93 +++++++++++++++++++++++++++++++---------
> >>   1 file changed, 72 insertions(+), 21 deletions(-)
> >>
> >> diff --git a/drivers/acpi/apei/ghes.c b/drivers/acpi/apei/ghes.c
> >> index 0e97d50b0240..7600063fe263 100644
> >> --- a/drivers/acpi/apei/ghes.c
> >> +++ b/drivers/acpi/apei/ghes.c
> >> @@ -1406,6 +1406,75 @@ static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
> >>          return ret;
> >>   }
> >>
> >> +/**
> >> + * ghes_has_active_errors - Check if there are active errors in error sources
> >> + * @ghes_list: List of GHES entries to check for active errors
> >> + *
> >> + * This function iterates through all GHES entries in the given list and
> >> + * checks if any of them has active error status by reading the error
> >> + * status register.
> >> + *
> >> + * Return: true if at least one source has active error, false otherwise.
> >> + */
> >> +static bool __maybe_unused ghes_has_active_errors(struct list_head *ghes_list)
> >> +{
> >> +       bool active_error = false;
> >> +       struct ghes *ghes;
> >> +
> >> +       rcu_read_lock();
> >
> > Nit: Use `guard(rcu)()` instead of explicit
> > `rcu_read_lock()`/`rcu_read_unlock()`.
> >
> > Thanks,
> > Donglin
> >
>
> Hi, Donglin,
>
> Good point. Will fix in next version.

If you do so, you may also get rid of the active_error local variable.
Re: [PATCH v2 2/3] ACPI: APEI: GHES: Extract helper functions for error status handling
Posted by Shuai Xue 4 weeks ago

On 1/10/26 5:22 AM, Rafael J. Wysocki wrote:
> On Mon, Jan 5, 2026 at 6:42 AM Shuai Xue <xueshuai@linux.alibaba.com> wrote:
>>
>>
>>
>> On 1/5/26 1:12 PM, Donglin Peng wrote:
>>> On Sun, Jan 4, 2026 at 8:05 PM Shuai Xue <xueshuai@linux.alibaba.com> wrote:
>>>>
>>>> Refactors the GHES driver by extracting common functionality into
>>>> reusable helper functions:
>>>>
>>>> 1. ghes_has_active_errors() - Checks if any error sources in a given list
>>>>      have active errors
>>>> 2. ghes_map_error_status() - Maps error status address to virtual address
>>>> 3. ghes_unmap_error_status() - Unmaps error status virtual address
>>>>
>>>> These helpers eliminate code duplication in the NMI path and prepare for
>>>> similar usage in the SEA path in a subsequent patch.
>>>>
>>>> No functional change intended.
>>>>
>>>> Tested-by: Tony Luck <tony.luck@intel.com>
>>>> Reviewed-by: Tony Luck <tony.luck@intel.com>
>>>> Signed-off-by: Shuai Xue <xueshuai@linux.alibaba.com>
>>>> ---
>>>>    drivers/acpi/apei/ghes.c | 93 +++++++++++++++++++++++++++++++---------
>>>>    1 file changed, 72 insertions(+), 21 deletions(-)
>>>>
>>>> diff --git a/drivers/acpi/apei/ghes.c b/drivers/acpi/apei/ghes.c
>>>> index 0e97d50b0240..7600063fe263 100644
>>>> --- a/drivers/acpi/apei/ghes.c
>>>> +++ b/drivers/acpi/apei/ghes.c
>>>> @@ -1406,6 +1406,75 @@ static int ghes_in_nmi_spool_from_list(struct list_head *rcu_list,
>>>>           return ret;
>>>>    }
>>>>
>>>> +/**
>>>> + * ghes_has_active_errors - Check if there are active errors in error sources
>>>> + * @ghes_list: List of GHES entries to check for active errors
>>>> + *
>>>> + * This function iterates through all GHES entries in the given list and
>>>> + * checks if any of them has active error status by reading the error
>>>> + * status register.
>>>> + *
>>>> + * Return: true if at least one source has active error, false otherwise.
>>>> + */
>>>> +static bool __maybe_unused ghes_has_active_errors(struct list_head *ghes_list)
>>>> +{
>>>> +       bool active_error = false;
>>>> +       struct ghes *ghes;
>>>> +
>>>> +       rcu_read_lock();
>>>
>>> Nit: Use `guard(rcu)()` instead of explicit
>>> `rcu_read_lock()`/`rcu_read_unlock()`.
>>>
>>> Thanks,
>>> Donglin
>>>
>>
>> Hi, Donglin,
>>
>> Good point. Will fix in next version.
> 
> If you do so, you may also get rid of the active_error local variable.

Got it. Thanks for reminding.
Shuai