Skip to content

tools: PAGE_SIZE mmap fallback for CONFIG_IO_STRICT_DEVMEM kernels#172

Merged
widgetii merged 1 commit into
masterfrom
fix/mem-reg-page-fallback-strict-devmem
Jun 4, 2026
Merged

tools: PAGE_SIZE mmap fallback for CONFIG_IO_STRICT_DEVMEM kernels#172
widgetii merged 1 commit into
masterfrom
fix/mem-reg-page-fallback-strict-devmem

Conversation

@widgetii
Copy link
Copy Markdown
Member

@widgetii widgetii commented Jun 4, 2026

Summary

  • mem_reg() mmaps a 64 KiB window per call. On CONFIG_IO_STRICT_DEVMEM=y kernels, the kernel rejects any /dev/mem mapping whose range overlaps a driver-claimed page — even for root.
  • On Hi3516CV608 the first probe (SCSYSID @ 0x11020EE0) opens a window [0x11020000..0x1102FFFF] that covers PWM @ 0x11029000, so the mmap fails with EPERM and ipctool aborts before printing the chip name.
  • On EPERM, retry with a single PAGE_SIZE window aligned to the requested address. The cache-invalidation logic is reworked to compare against the actual loaded window range, so reads of adjacent registers in the same page still hit the cache after a fallback.

Validation

Tested on OpenIPC hi3516cv6xx (kernel 5.10.221 armv7, dual A7) at 10.216.128.115 with stock OpenIPC defconfig (both CONFIG_STRICT_DEVMEM=y and CONFIG_IO_STRICT_DEVMEM=y).

Before:

# ipctool
read_mem_reg mmap error: Operation not permitted (1)
# ipcinfo -s
read_mem_reg mmap error: Operation not permitted (1)

After:

# ipctool
chip:
  vendor: HiSilicon
  model: 3516CV608
board:
  vendor: OpenIPC
  version: 2.6.06.04
...
# ipcinfo -c
hi3516cv608

Three EPERMs remain on register blocks whose pages really are kernel-claimed (0x11010000 CRG/clocks, 0x11060000 I2C, 0x1109xxxx GPIO). Those need the kernel-side companion fix (drop CONFIG_IO_STRICT_DEVMEM from hi3516cv6xx.generic.config — separate firmware PR).

Test plan

  • Cross-built for armv7 musl with the in-tree arm-openipc-linux-musleabi.cmake toolchain file
  • Deployed and run on hi3516cv6xx — chip detection works, no regression on non-strict kernels (page fallback only triggers on EPERM)
  • Smoke-test on a non-strict kernel (e.g. any V4 board) — the success path is unchanged

🤖 Generated with Claude Code

mem_reg() always mmap()s a 64 KiB window so consecutive register reads
in the same SoC block hit a cache. On kernels built with
CONFIG_IO_STRICT_DEVMEM=y (the kernel default since v4.6, and what the
OpenIPC hi3516cv6xx defconfig currently ships) any /dev/mem mapping
whose range overlaps a page already claimed by a driver via
request_mem_region() is rejected with EPERM, even for root.

On Hi3516CV608 the very first probe touches SCSYSID at 0x11020EE0.
The 64 KiB window [0x11020000..0x1102FFFF] covers 0x11029000, claimed
by the PWM driver, so mmap fails and ipctool aborts before printing
the chip name -- even though the SYSCTRL page itself is unclaimed and
readable.

When EPERM is observed, retry the mmap with a single PAGE_SIZE window
aligned to the requested address. That unblocks chip-ID, HPM, DDR PHY
and any other register block whose own page isn't kernel-claimed.
Pages that *are* claimed (CRG/clocks, I2C, SPI, GPIO, UART) still need
the kernel-side companion fix in the OpenIPC firmware defconfig.

Cache invalidation is rewritten to track the actual loaded window
range instead of comparing against a fixed 64 KiB offset, so the
fallback path's smaller window still benefits from the read-side
cache for adjacent registers in the same page.

Verified on OpenIPC hi3516cv6xx (kernel 5.10.221, CONFIG_STRICT_DEVMEM
and CONFIG_IO_STRICT_DEVMEM both =y): before the patch ipctool exited
with "read_mem_reg mmap error: Operation not permitted (1)"; after,
it correctly identifies model 3516CV608 and emits NOR/RAM/firmware
sections of the YAML report.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@widgetii widgetii merged commit 5972354 into master Jun 4, 2026
4 checks passed
@widgetii widgetii deleted the fix/mem-reg-page-fallback-strict-devmem branch June 4, 2026 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant