mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ACPI: sysfs: Map only the requested range in acpi_data_show()
@ 2026-10-13 18:07 Jamie Nguyen
  0 siblings, 0 replies; only message in thread
From: Jamie Nguyen @ 2026-10-13 18:07 UTC (permalink / raw)
  To: Rafael J. Wysocki
  Cc: Len Brown, Tony Luck, Punit Agrawal, linux-acpi, linux-kernel

acpi_data_show() maps the whole data region on every read(), although
it copies at most count bytes and sysfs passes at most PAGE_SIZE bytes
per call. acpi_os_unmap_iomem() defers the iounmap until after an RCU
grace period, so a process reading one of these files in a loop keeps
many full-size mappings in flight.

Map only the range being read. acpi_os_map_iomem() rounds the mapping
to page boundaries, so each mapping is now at most two pages, whatever
the size of the region.

A malformed table can describe a region that wraps around the end of
the physical address space. acpi_os_map_iomem() doesn't check for that,
and its lookup of existing mappings then matches an unrelated mapping,
with or without this change. Fail reads of such a region before mapping
anything.

Assisted-by: LLM
Signed-off-by: Jamie Nguyen <jamien@nvidia.com>
---
As discussed on the BDAT v2 thread:
https://lore.kernel.org/r/CAJZ5v0ixQ_5jmrfm4mz50tZtCEq6nJt1b2eMoBdhr0f3AY+k7w@mail.gmail.com

This doesn't depend on that series and applies on its own.

Tested on an arm64 server with 4K and 64K pages, with the BDAT v2
series applied:

- The BERT and BDAT data files match /dev/mem when read whole, byte by
  byte, page by page, and across page boundaries.
- Each read maps at most two pages. With 4K pages, looping full reads
  of the 90754-byte BDAT file kept at most 167 MiB of vmalloc space
  mapped (1740 MiB before), and looping 1-byte reads at most 355 MiB
  (3090 MiB before).
- Malformed BERT tables loaded via initrd table upgrade, with regions
  that wrap around the end of the physical address space, fail every
  read with -EINVAL.

 drivers/acpi/sysfs.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/acpi/sysfs.c b/drivers/acpi/sysfs.c
index dd4a99f09efea65..138009659a7b55f 100644
--- a/drivers/acpi/sysfs.c
+++ b/drivers/acpi/sysfs.c
@@ -419,6 +419,7 @@ static ssize_t acpi_data_show(struct file *filp, struct kobject *kobj,
 	struct acpi_data_attr *data_attr;
 	void __iomem *base;
 	ssize_t size;
+	u64 end;
 
 	data_attr = container_of(bin_attr, struct acpi_data_attr, attr);
 	size = data_attr->attr.size;
@@ -432,13 +433,17 @@ static ssize_t acpi_data_show(struct file *filp, struct kobject *kobj,
 	if (count > size - offset)
 		count = size - offset;
 
-	base = acpi_os_map_iomem(data_attr->addr, size);
+	/* acpi_os_map_iomem() can't handle a range that wraps around */
+	if (check_add_overflow(data_attr->addr, size, &end))
+		return -EINVAL;
+
+	base = acpi_os_map_iomem(data_attr->addr + offset, count);
 	if (!base)
 		return -ENOMEM;
 
-	memcpy_fromio(buf, base + offset, count);
+	memcpy_fromio(buf, base, count);
 
-	acpi_os_unmap_iomem(base, size);
+	acpi_os_unmap_iomem(base, count);
 
 	return count;
 }
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-10-13 18:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-13 18:07 [PATCH] ACPI: sysfs: Map only the requested range in acpi_data_show() Jamie Nguyen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®