diff mbox series

[2/2,v3] acpi: allow building without CONFIG_HAS_IOPORT

Message ID 20241030123701.1538919-2-arnd@kernel.org (mailing list archive)
State In Next
Delegated to: Rafael Wysocki
Headers show
Series [1/2,v3] acpi: processor_perflib: extend X86 dependency | expand

Commit Message

Arnd Bergmann Oct. 30, 2024, 12:36 p.m. UTC
From: Arnd Bergmann <arnd@arndb.de>

CONFIG_HAS_IOPORT will soon become optional and cause a build time
failure when it is disabled but a driver calls inb()/outb(). At the
moment, all architectures that can support ACPI have port I/O, but this
is not necessarily the case in the future on non-x86 architectures.
The result is a set of errors like:

drivers/acpi/osl.c: In function 'acpi_os_read_port':
include/asm-generic/io.h:542:14: error: call to '_inb' declared with attribute error: inb()) requires CONFIG_HAS_IOPORT

Nothing should actually call these functions in this configuration,
and if it does, the result would be undefined behavior today, possibly
a NULL pointer dereference.

Change the low-level functions to return a proper error code when
HAS_IOPORT is disabled.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
v3: fix the returned value and add a comment
---
 drivers/acpi/cppc_acpi.c |  6 ++++--
 drivers/acpi/osl.c       | 12 ++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)

Comments

Andy Shevchenko Oct. 30, 2024, 3:13 p.m. UTC | #1
On Wed, Oct 30, 2024 at 12:36:41PM +0000, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> CONFIG_HAS_IOPORT will soon become optional and cause a build time
> failure when it is disabled but a driver calls inb()/outb(). At the
> moment, all architectures that can support ACPI have port I/O, but this
> is not necessarily the case in the future on non-x86 architectures.
> The result is a set of errors like:
> 
> drivers/acpi/osl.c: In function 'acpi_os_read_port':
> include/asm-generic/io.h:542:14: error: call to '_inb' declared with attribute error: inb()) requires CONFIG_HAS_IOPORT
> 
> Nothing should actually call these functions in this configuration,
> and if it does, the result would be undefined behavior today, possibly
> a NULL pointer dereference.
> 
> Change the low-level functions to return a proper error code when
> HAS_IOPORT is disabled.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

...

> +	if (!IS_ENABLED(CONFIG_HAS_IOPORT)) {
> +		/*
> +		 * set all-1 result as if reading from non-existing
> +		 * I/O port
> +		 */

Don't know if Rafael can / want to tweak this, but would be nice to follow
standard style for multi-line comments.

		/*
		 * Set all-1 result as if reading from non-existing
		 * I/O port.
		 */

> +		*value = GENMASK(width, 0);
> +		return AE_NOT_IMPLEMENTED;
> +	}
Andy Shevchenko Oct. 30, 2024, 3:14 p.m. UTC | #2
On Wed, Oct 30, 2024 at 05:13:15PM +0200, Andy Shevchenko wrote:
> On Wed, Oct 30, 2024 at 12:36:41PM +0000, Arnd Bergmann wrote:
> > From: Arnd Bergmann <arnd@arndb.de>
> > 
> > CONFIG_HAS_IOPORT will soon become optional and cause a build time
> > failure when it is disabled but a driver calls inb()/outb(). At the
> > moment, all architectures that can support ACPI have port I/O, but this
> > is not necessarily the case in the future on non-x86 architectures.
> > The result is a set of errors like:
> > 
> > drivers/acpi/osl.c: In function 'acpi_os_read_port':
> > include/asm-generic/io.h:542:14: error: call to '_inb' declared with attribute error: inb()) requires CONFIG_HAS_IOPORT
> > 
> > Nothing should actually call these functions in this configuration,
> > and if it does, the result would be undefined behavior today, possibly
> > a NULL pointer dereference.
> > 
> > Change the low-level functions to return a proper error code when
> > HAS_IOPORT is disabled.
> 
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

...

> > +	if (!IS_ENABLED(CONFIG_HAS_IOPORT)) {
> > +		/*
> > +		 * set all-1 result as if reading from non-existing
> > +		 * I/O port
> > +		 */
> 
> Don't know if Rafael can / want to tweak this, but would be nice to follow
> standard style for multi-line comments.
> 
> 		/*
> 		 * Set all-1 result as if reading from non-existing
> 		 * I/O port.
> 		 */
> 
> > +		*value = GENMASK(width, 0);

Actually, shouldn't this be width - 1 ?

> > +		return AE_NOT_IMPLEMENTED;
> > +	}
diff mbox series

Patch

diff --git a/drivers/acpi/cppc_acpi.c b/drivers/acpi/cppc_acpi.c
index 1a40f0514eaa..3757424b715f 100644
--- a/drivers/acpi/cppc_acpi.c
+++ b/drivers/acpi/cppc_acpi.c
@@ -1017,7 +1017,8 @@  static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val)
 	*val = 0;
 	size = GET_BIT_WIDTH(reg);
 
-	if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
+	if (IS_ENABLED(CONFIG_HAS_IOPORT) &&
+	    reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
 		u32 val_u32;
 		acpi_status status;
 
@@ -1091,7 +1092,8 @@  static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val)
 
 	size = GET_BIT_WIDTH(reg);
 
-	if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
+	if (IS_ENABLED(CONFIG_HAS_IOPORT) &&
+	    reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
 		acpi_status status;
 
 		status = acpi_os_write_port((acpi_io_address)reg->address,
diff --git a/drivers/acpi/osl.c b/drivers/acpi/osl.c
index 78a81969d90e..8ab1802c164b 100644
--- a/drivers/acpi/osl.c
+++ b/drivers/acpi/osl.c
@@ -642,6 +642,15 @@  acpi_status acpi_os_read_port(acpi_io_address port, u32 *value, u32 width)
 {
 	u32 dummy;
 
+	if (!IS_ENABLED(CONFIG_HAS_IOPORT)) {
+		/*
+		 * set all-1 result as if reading from non-existing
+		 * I/O port
+		 */
+		*value = GENMASK(width, 0);
+		return AE_NOT_IMPLEMENTED;
+	}
+
 	if (value)
 		*value = 0;
 	else
@@ -665,6 +674,9 @@  EXPORT_SYMBOL(acpi_os_read_port);
 
 acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
 {
+	if (!IS_ENABLED(CONFIG_HAS_IOPORT))
+		return AE_NOT_IMPLEMENTED;
+
 	if (width <= 8) {
 		outb(value, port);
 	} else if (width <= 16) {