From 74b51ee152b6d99e61ba329799a039453fb9438f Mon Sep 17 00:00:00 2001 From: Konstantin Khlebnikov Date: Sun, 9 Nov 2014 13:53:37 +0400 Subject: [PATCH 1/6] ACPI / osl: speedup grace period in acpi_os_map_cleanup ACPI maintains cache of ioremap regions to speed up operations and access to them from irq context where ioremap() calls aren't allowed. This code abuses synchronize_rcu() on unmap path for synchronization with fast-path in acpi_os_read/write_memory which uses this cache. Since v3.10 CPUs are allowed to enter idle state even if they have RCU callbacks queued, see commit c0f4dfd4f90f1667d234d21f15153ea09a2eaa66 ("rcu: Make RCU_FAST_NO_HZ take advantage of numbered callbacks"). That change caused problems with nvidia proprietary driver which calls acpi_os_map/unmap_generic_address several times during initialization. Each unmap calls synchronize_rcu and adds significant delay. Totally initialization is slowed for a couple of seconds and that is enough to trigger timeout in hardware, gpu decides to "fell off the bus". Widely spread workaround is reducing "rcu_idle_gp_delay" from 4 to 1 jiffy. This patch replaces synchronize_rcu() with synchronize_rcu_expedited() which is much faster. Link: https://devtalk.nvidia.com/default/topic/567297/linux/linux-3-10-driver-crash/ Signed-off-by: Konstantin Khlebnikov Reported-and-tested-by: Alexander Monakov Reviewed-by: Paul E. McKenney Signed-off-by: Rafael J. Wysocki --- drivers/acpi/osl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/acpi/osl.c b/drivers/acpi/osl.c index 9964f70be98d..217713c11aaa 100644 --- a/drivers/acpi/osl.c +++ b/drivers/acpi/osl.c @@ -436,7 +436,7 @@ static void acpi_os_drop_map_ref(struct acpi_ioremap *map) static void acpi_os_map_cleanup(struct acpi_ioremap *map) { if (!map->refcount) { - synchronize_rcu(); + synchronize_rcu_expedited(); acpi_unmap(map->phys, map->virt); kfree(map); } From 90253a792ec23481402474704ef498ee81abe4e3 Mon Sep 17 00:00:00 2001 From: Lv Zheng Date: Wed, 5 Nov 2014 15:06:13 +0800 Subject: [PATCH 2/6] ACPI / OSL: Add IRQ handler flushing support in the OSL. It is possible that a GPE handler or a fixed event handler still accessed after removing the handlers by invoking acpi_remove_gpe_handler() or acpi_remove_fixed_event_handler(), this possibility can crash OPSM after a module removal. In the Linux kernel, though all other GPE drivers are not modules, since the IPMI_SI (ipmi_si_intf.c) can be compiled as a module, we still need to consider a solution for this issue when the driver switches to ACPI_GPE_RAW_HANDLER mode in order to invoke GPE APIs. ACPICA expects acpi_os_wait_events_complete() to be invoked after GPE disabling so that OSPM can ensure all running GPE handlers have exitted. But currently acpi_os_wait_events_complete() can only flush _Lxx/_Exx evaluation work queue and this philosophy cannot work for drivers that have installed a dedicated GPE handler. The only way to protect a callback is to perform some state holders (reference count, state machine) before invoking the callback. Then this issue can only be fixed by the following means: 1. Flush GPE in ACPICA before invoking the GPE handler. But currently, there is no such implementation in acpi_ev_gpe_dispatch(). 2. Flush GPE in ACPICA OSL before invoking the SCI handler. But currently, there is no such implementation in acpi_irq(). 3. Flush IRQ in OSPM IRQ layer before invoking the IRQ handler. In Linus kernel, this can be done by synchronize_irq(). 4. Flush scheduling in OSPM vector entry layer before invoking the vector. In Linux, this can be done by synchronize_sched(). Since ACPICA expects the GPE handlers to be flushed by the ACPICA OSL or the GPE drivers. If it is implemented by the GPE driver, we should see synchronize_irq()/synchronize_sched() invoked in such drivers. If it is implemented by the ACPICA OSL, ACPICA currently provides acpi_os_wait_events_complete() hook to achieve this. After the following commit: Commit: 69c841b6dd8313c9a673246cc0e2535174272cab Author: Lv Zheng Subject: ACPICA: Update use of acpi_os_wait_events_complete interface. The OSL acpi_os_wait_events_complete() is invoked after a GPE handler is removed from acpi_remove_gpe_handler() or a fixed event handler is removed from acpi_remove_fixed_event_handler(). Thus it is possible to implement GPE handler flushing using this ACPICA OSL now. So the solution 1 is currently not taken into account. By examining the IPMI_SI driver, we noticed that the IPMI_SI driver: 1. Uses free_irq() to flush non GPE based IRQ handlers, in free_irq(), synchronize_irq() is invoked, and 2. Uses acpi_remove_gpe_handler() to flush GPE based IRQ handlers, for such IRQ handlers, there is no synchronize_irq() invoked. Since there isn't synchronize_sched() implemented for this driver, from the driver's perspective, acpi_remove_gpe_handler() should have properly flushed the GPE handlers for it. Since the driver doesn't invoke synchronize_irq(), the solution 3 is not what the drivers expect. This patch implements solution 2. But since given the fact that the GPE is managed inside of ACPICA, and implementing the GPE flushing requires to implement the whole GPE management code again in the OSL, instead of flushing GPE, this patch flushes IRQ in acpi_os_wait_events_complete(). The flushing could last longer than expected as though the target GPE/fixed event that is removed can be fastly flushed, other GPEs/fix events can still be issued during the flushing period. This patch fixes this issue by invoking synchronize_hardirq() in acpi_os_wait_events_complete(). The reason why we don't invoke synchronize_irq() is: currently ACPICA is not threaded IRQ capable and the only difference between synchronize_irq() and synchronize_hardirq() is synchronize_irq() also flushes threaded IRQ handlers. Thus using synchronize_hardirq() can help to reduce the overall synchronization time for the current ACPICA implementation. Signed-off-by: Lv Zheng Cc: Rafael J. Wysocki Cc: Len Brown Cc: Robert Moore Cc: Corey Minyard Cc: linux-acpi@vger.kernel.org Cc: devel@acpica.org Cc: openipmi-developer@lists.sourceforge.net Signed-off-by: Rafael J. Wysocki --- drivers/acpi/osl.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/acpi/osl.c b/drivers/acpi/osl.c index 217713c11aaa..f9eeae871593 100644 --- a/drivers/acpi/osl.c +++ b/drivers/acpi/osl.c @@ -1188,6 +1188,12 @@ EXPORT_SYMBOL(acpi_os_execute); void acpi_os_wait_events_complete(void) { + /* + * Make sure the GPE handler or the fixed event handler is not used + * on another CPU after removal. + */ + if (acpi_irq_handler) + synchronize_hardirq(acpi_gbl_FADT.sci_interrupt); flush_workqueue(kacpid_wq); flush_workqueue(kacpi_notify_wq); } From 99a33ffcf6b431e49cd04097b85f48aa17499a6a Mon Sep 17 00:00:00 2001 From: Hanjun Guo Date: Fri, 14 Nov 2014 17:44:07 +0800 Subject: [PATCH 3/6] ACPI / Kconfig: Remove redundant depends on ACPI Since config ACPI_REDUCED_HARDWARE_ONLY is already depended on ACPI (inside if ACPI / endif), so depdens on ACPI is redundant, remove it and fix the minor syntax problem also. Signed-off-by: Hanjun Guo Signed-off-by: Rafael J. Wysocki --- drivers/acpi/Kconfig | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/drivers/acpi/Kconfig b/drivers/acpi/Kconfig index b23fe37f67c0..79078b8f5697 100644 --- a/drivers/acpi/Kconfig +++ b/drivers/acpi/Kconfig @@ -360,15 +360,14 @@ config ACPI_BGRT config ACPI_REDUCED_HARDWARE_ONLY bool "Hardware-reduced ACPI support only" if EXPERT def_bool n - depends on ACPI help - This config item changes the way the ACPI code is built. When this - option is selected, the kernel will use a specialized version of - ACPICA that ONLY supports the ACPI "reduced hardware" mode. The - resulting kernel will be smaller but it will also be restricted to - running in ACPI reduced hardware mode ONLY. + This config item changes the way the ACPI code is built. When this + option is selected, the kernel will use a specialized version of + ACPICA that ONLY supports the ACPI "reduced hardware" mode. The + resulting kernel will be smaller but it will also be restricted to + running in ACPI reduced hardware mode ONLY. - If you are unsure what to do, do not enable this option. + If you are unsure what to do, do not enable this option. source "drivers/acpi/apei/Kconfig" From d93de3455d0f04b1c5a60af41399b7f6f7b13c47 Mon Sep 17 00:00:00 2001 From: Fabian Frederick Date: Sun, 16 Nov 2014 10:57:00 +0100 Subject: [PATCH 4/6] ACPI: remove unnecessary sizeof(u8) sizeof(u8) is always 1. Signed-off-by: Fabian Frederick Signed-off-by: Rafael J. Wysocki --- drivers/acpi/utils.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/drivers/acpi/utils.c b/drivers/acpi/utils.c index 371ac12d25b1..dd8ff63ee2b4 100644 --- a/drivers/acpi/utils.c +++ b/drivers/acpi/utils.c @@ -136,8 +136,7 @@ acpi_extract_package(union acpi_object *package, break; case 'B': size_required += - sizeof(u8 *) + - (element->buffer.length * sizeof(u8)); + sizeof(u8 *) + element->buffer.length; tail_offset += sizeof(u8 *); break; default: @@ -255,7 +254,7 @@ acpi_extract_package(union acpi_object *package, memcpy(tail, element->buffer.pointer, element->buffer.length); head += sizeof(u8 *); - tail += element->buffer.length * sizeof(u8); + tail += element->buffer.length; break; default: /* Should never get here */ From f08bb472bff3c0397fb7d6f47bc5cec41dad76e3 Mon Sep 17 00:00:00 2001 From: Ashwin Chaugule Date: Wed, 26 Nov 2014 22:01:13 +0800 Subject: [PATCH 5/6] ACPI / table: Add new function to get table entries The acpi_table_parse() function has a callback that passes a pointer to a table_header. Add a new function which takes this pointer and parses its entries. This eliminates the need to re-traverse all the tables for each call. e.g. as in acpi_table_parse_madt() which is normally called after acpi_table_parse(). Acked-by: Grant Likely Signed-off-by: Ashwin Chaugule Signed-off-by: Tomasz Nowicki Signed-off-by: Hanjun Guo Signed-off-by: Rafael J. Wysocki --- drivers/acpi/tables.c | 63 ++++++++++++++++++++++++++++++------------- include/linux/acpi.h | 4 +++ 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/drivers/acpi/tables.c b/drivers/acpi/tables.c index 6d5a6cda0734..f1debe97dcfc 100644 --- a/drivers/acpi/tables.c +++ b/drivers/acpi/tables.c @@ -190,30 +190,24 @@ void acpi_table_print_madt_entry(struct acpi_subtable_header *header) } } - int __init -acpi_table_parse_entries(char *id, - unsigned long table_size, - int entry_id, - acpi_tbl_entry_handler handler, - unsigned int max_entries) +acpi_parse_entries(char *id, unsigned long table_size, + acpi_tbl_entry_handler handler, + struct acpi_table_header *table_header, + int entry_id, unsigned int max_entries) { - struct acpi_table_header *table_header = NULL; struct acpi_subtable_header *entry; - unsigned int count = 0; + int count = 0; unsigned long table_end; - acpi_size tbl_size; if (acpi_disabled) return -ENODEV; - if (!handler) + if (!id || !handler) return -EINVAL; - if (strncmp(id, ACPI_SIG_MADT, 4) == 0) - acpi_get_table_with_size(id, acpi_apic_instance, &table_header, &tbl_size); - else - acpi_get_table_with_size(id, 0, &table_header, &tbl_size); + if (!table_size) + return -EINVAL; if (!table_header) { pr_warn("%4.4s not present\n", id); @@ -232,7 +226,7 @@ acpi_table_parse_entries(char *id, if (entry->type == entry_id && (!max_entries || count++ < max_entries)) if (handler(entry, table_end)) - goto err; + return -EINVAL; /* * If entry->length is 0, break from this loop to avoid @@ -240,22 +234,53 @@ acpi_table_parse_entries(char *id, */ if (entry->length == 0) { pr_err("[%4.4s:0x%02x] Invalid zero length\n", id, entry_id); - goto err; + return -EINVAL; } entry = (struct acpi_subtable_header *) ((unsigned long)entry + entry->length); } + if (max_entries && count > max_entries) { pr_warn("[%4.4s:0x%02x] ignored %i entries of %i found\n", id, entry_id, count - max_entries, count); } + return count; +} + +int __init +acpi_table_parse_entries(char *id, + unsigned long table_size, + int entry_id, + acpi_tbl_entry_handler handler, + unsigned int max_entries) +{ + struct acpi_table_header *table_header = NULL; + acpi_size tbl_size; + int count; + u32 instance = 0; + + if (acpi_disabled) + return -ENODEV; + + if (!id || !handler) + return -EINVAL; + + if (!strncmp(id, ACPI_SIG_MADT, 4)) + instance = acpi_apic_instance; + + acpi_get_table_with_size(id, instance, &table_header, &tbl_size); + if (!table_header) { + pr_warn("%4.4s not present\n", id); + return -ENODEV; + } + + count = acpi_parse_entries(id, table_size, handler, table_header, + entry_id, max_entries); + early_acpi_os_unmap_memory((char *)table_header, tbl_size); return count; -err: - early_acpi_os_unmap_memory((char *)table_header, tbl_size); - return -EINVAL; } int __init diff --git a/include/linux/acpi.h b/include/linux/acpi.h index 407a12f663eb..3bd0c59e1fb9 100644 --- a/include/linux/acpi.h +++ b/include/linux/acpi.h @@ -123,6 +123,10 @@ int acpi_numa_init (void); int acpi_table_init (void); int acpi_table_parse(char *id, acpi_tbl_table_handler handler); +int __init acpi_parse_entries(char *id, unsigned long table_size, + acpi_tbl_entry_handler handler, + struct acpi_table_header *table_header, + int entry_id, unsigned int max_entries); int __init acpi_table_parse_entries(char *id, unsigned long table_size, int entry_id, acpi_tbl_entry_handler handler, From 4ceacd02f5a1795c5c697e0345ee10beef675290 Mon Sep 17 00:00:00 2001 From: Tomasz Nowicki Date: Wed, 26 Nov 2014 22:01:14 +0800 Subject: [PATCH 6/6] ACPI / table: Always count matched and successfully parsed entries acpi_parse_entries() allows to traverse all available table entries (aka subtables) by passing max_entries parameter equal to 0, but since its count variable is only incremented if max_entries is not 0, the function always returns 0 for max_entries equal to 0. It would be more useful if it returned the number of entries matched instead, so make it increment count in that case too. Acked-by: Grant Likely Signed-off-by: Tomasz Nowicki Signed-off-by: Hanjun Guo Signed-off-by: Rafael J. Wysocki --- drivers/acpi/tables.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/acpi/tables.c b/drivers/acpi/tables.c index f1debe97dcfc..93b81523a2fe 100644 --- a/drivers/acpi/tables.c +++ b/drivers/acpi/tables.c @@ -224,10 +224,13 @@ acpi_parse_entries(char *id, unsigned long table_size, while (((unsigned long)entry) + sizeof(struct acpi_subtable_header) < table_end) { if (entry->type == entry_id - && (!max_entries || count++ < max_entries)) + && (!max_entries || count < max_entries)) { if (handler(entry, table_end)) return -EINVAL; + count++; + } + /* * If entry->length is 0, break from this loop to avoid * infinite loop.