Skip to content

mnt: retry read-only remounts on EBUSY - #347

Open
HanziJiang wants to merge 3 commits into
google:masterfrom
HanziJiang:hanzi/remount-ebusy-retry
Open

HanziJiang wants to merge 3 commits into
google:masterfrom
HanziJiang:hanzi/remount-ebusy-retry

Conversation

@HanziJiang

@HanziJiang HanziJiang commented Sep 23, 2026 •

Copy link
Copy Markdown

A read-only remount of a bind fails with EBUSY if any task holds write access to the mount at that moment.

nsjail mounts each bind read-write first, and only remounts it read-only after the rest of the jail is set up. If a host process accesses files in the jail during that gap and briefly takes write access to the mount, the read-only remount fails with EBUSY and nsjail aborts the launch.

We hit this with a host security agent following a symlink inside a bind. Following a symlink updates its atime, which briefly takes write access, and on an overlayfs lower layer the atime is never stored, so every follow does it.

Kernel level reproduction as root:

sudo bash -c '
mkdir -p /tmp/r/lower/d /tmp/r/upper /tmp/r/work /tmp/r/merged /tmp/r/bind
echo data > /tmp/r/lower/d/real && ln -s real /tmp/r/lower/d/link
mount -t overlay overlay -o lowerdir=/tmp/r/lower,upperdir=/tmp/r/upper,workdir=/tmp/r/work /tmp/r/merged
mount --bind /tmp/r/merged/d /tmp/r/bind
while :; do [ -e /tmp/r/bind/link/x ]; done &
loop=$!
n=0; for i in $(seq 2000); do mount -o remount,bind,ro /tmp/r/bind 2>/dev/null || n=$((n+1)); mount -o remount,bind,rw /tmp/r/bind; done
echo "failed read-only remounts: $n/2000"
kill $loop
umount /tmp/r/bind /tmp/r/merged && rm -rf /tmp/r
'
failed read-only remounts: 140/2000

This PR adds retries to the read-only switch up to 3 times on EBUSY, sleeping 1ms before each retry.

@HanziJiang
HanziJiang marked this pull request as ready for review September 24, 2026 00:11
@HanziJiang

HanziJiang commented Nov 10, 2026 •

Copy link
Copy Markdown
Author

Hi @robertswiecki, are these something you’d like to accept?

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repro is convincing, and I checked that the four call sites cover every place a read-only flag gets applied. In the new-API path, per-mount RO goes through remountPt → remountWithLegacyMount (the applyMountFlags calls at L457/L498/L569 all mask off MS_RDONLY), and the root uses the mount_setattr call in buildMountTree. Two small things on the macro:

Comment thread macros.h Outdated
long int __result; \
int __retries = 0; \
while ((__result = (long int)(expression)) == -1L && errno == EBUSY && \
__retries++ < 3) { \

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With 140/2000 failures in your loop, three 1 ms retries should cover the transient atime case. There's also a persistent EBUSY case, though: something really does hold a file open for writing on the bind. That now fails ~3 ms later with the same PLOG_W/PLOG_E and nothing to say a retry happened. A LOG_D("EBUSY, retrying (%d)", __retries) inside the loop would make it easy to tell the two apart in debug logs. macros.h doesn't pull in logs.h, though, so you might do that at the call sites instead, or pull the retry out into a small helper in mnt.cc.

@HanziJiang HanziJiang Nov 13, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! Yes I verified the 1ms retry was enough to cover our case.

Regarding retry logs, I lean towards not adding the debug log between attempts. This follows the convention in this repository (e.g. TEMP_FAILURE_RETRY), the retries are silent, and the error is logged at the call site only if the call finally fails. So if we see the log, it means all attempts have exhausted.

Comment thread macros.h
Comment on lines +43 to +44
int __retries = 0; \
while ((__result = (long int)(expression)) == -1L && errno == EBUSY && \

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the 3 and 1000 are magic numbers inside a macro other files may reuse. Named constants (kEbusyRetries, kEbusyRetryDelayUs) next to it would make the 3 ms upper bound visible from the call sites.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I extracted in 95488bc

This branch has not been deployed

No deployments
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.

2 participants