Skip to content

mtd/cfi: add rwsem to serialize CFI command sequences - #22019

Open
jingfei195887 wants to merge 1 commit into
apache:masterfrom
jingfei195887:fs/mtd-cfi-rwsem
Open

jingfei195887 wants to merge 1 commit into
apache:masterfrom
jingfei195887:fs/mtd-cfi-rwsem

Conversation

@jingfei195887

Copy link
Copy Markdown
Contributor

Summary

  • mtd/cfi: add rwsem to serialize CFI command sequences.
    CFI command sequences were not serialized, so a read, write or erase on
    one partition could interleave with a write on another partition of the
    same flash and corrupt both: a write or erase leaves the chip out of
    read-array mode until it completes. Add a per-device read-write lock
    (rw_semaphore_t) to struct cfi_dev_s: reads are shared, write and
    erase are exclusive until the chip is back in read-array mode.

Impact

  • New feature / API change: NO.
  • Behavior: CFI reads may run in parallel; a write or erase now excludes
    every other access to the same device.
  • Build, hardware, documentation, security, compatibility: NO.

Testing

Host: Ubuntu 22.04 x86_64, Arm GNU Toolchain 13.2.rel1.

qemu-armv7a:full (CONFIG_MTD_CFI, littlefs mounted on
/dev/cfi-flash1 at /data), run with
qemu-system-arm -cpu cortex-a7 -machine virt,highmem=off,virtualization=off,gic-version=2:

nsh> echo cfi-test > /data/a.txt
nsh> cat /data/a.txt
cfi-test
nsh> dd if=/dev/zero of=/data/big bs=4096 count=64
nsh> ls -l /data
/data:
 -rw-rw-rw-           9 2023-04-25 00:00 a.txt
 -rw-r--r--      262144 2023-04-25 00:00 big
nsh> rm /data/big
nsh> ls /data
/data:
 a.txt

The output is identical on master fc8d354 and with this change. The
concurrent interleaving was not reproduced on QEMU; the test shows that
reads, writes and erases through the lock still work.

  • ./tools/checkpatch.sh -g master..HEAD: all checks pass.
  • sim:nsh with CONFIG_MTD and CONFIG_MTD_CFI: Make and CMake builds
    pass with no new warnings in the changed files; ostest exits with
    status 0.
  • qemu-armv7a:full: build passes, run log above.
  • Verified on QEMU and sim only.

PR verification Self-Check

  • This PR introduces only one functional change.
  • I have updated all required description fields above.
  • My PR adheres to Contributing Guidelines and Documentation (git commit title and message, coding standard, etc).
  • My PR is still work in progress (not ready for review).
  • My PR is ready for review and can be safely merged into a codebase.

CFI command sequences were not serialized, so a read/write/erase on
one partition could interleave with a write on another partition of
the same flash and corrupt both. Add a per-device read-write lock:
reads are shared, write and erase are exclusive until read array.

Signed-off-by: zhaoxingyu1 <zhaoxingyu1@xiaomi.com>
Signed-off-by: jingfei <jingfei@xiaomi.com>
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: S The size of the change in this PR is small labels Nov 18, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants