Repository navigation
mnt: retry read-only remounts on EBUSY - #347
HanziJiang wants to merge 3 commits into
Conversation
|
Hi @robertswiecki, are these something you’d like to accept? |
mangeshraut712
left a comment
There was a problem hiding this comment.
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:
| long int __result; \ | ||
| int __retries = 0; \ | ||
| while ((__result = (long int)(expression)) == -1L && errno == EBUSY && \ | ||
| __retries++ < 3) { \ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| int __retries = 0; \ | ||
| while ((__result = (long int)(expression)) == -1L && errno == EBUSY && \ |
There was a problem hiding this comment.
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.
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:
This PR adds retries to the read-only switch up to 3 times on EBUSY, sleeping 1ms before each retry.