<div dir="ltr"><div dir="ltr">On Tue, Aug 6, 2019 at 1:56 PM Rob Landley <<a href="mailto:rob@landley.net">rob@landley.net</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 8/5/19 7:40 PM, Ryan Prichard via Toybox wrote:<br>
> I think this patch fixes a race I noticed, but in practice I was hitting a<br>
> different race. After LOOP_CTL_GET_FREE had created a new loop device, losetup<br>
> tried to open /dev/block/loopXXX before the device file existed,<br>
<br>
Why is the ioctl returning before the block device exists?<br></blockquote><div><br></div><div>I think the /dev/block/loopXXX file is created by a userspace daemon (probably ueventd in Android). The ioctl creates the kernel device, then sends an asynchronous uevent message to ueventd about the new device. ueventd and losetup then race each other to create the file and to open it.</div><div><br></div><div>It looks like this code is responsible for creating device files on Android:</div><div><a href="https://android.googlesource.com/platform/system/core/+/924858cd18589de74e7f4347d27c77ca6a33716d/init/devices.cpp#270">https://android.googlesource.com/platform/system/core/+/924858cd18589de74e7f4347d27c77ca6a33716d/init/devices.cpp#270</a><br></div><div><br></div><div>It looks like someone worked around a similar race involving losetup and udevd:</div><div><br></div><div><a href="https://bugzilla.redhat.com/show_bug.cgi?id=1045432#c10">https://bugzilla.redhat.com/show_bug.cgi?id=1045432#c10</a><br></div><div><a href="https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/commit/?id=663bf040611240d164f2464b4d892ecd2b02fa6f">https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/commit/?id=663bf040611240d164f2464b4d892ecd2b02fa6f</a><br></div><div><br></div><div>Summarizing the util-linux workaround:</div><div> - Make 16 attempts to open the device file.</div><div> - If the open fails with EACCES or ENOENT, sleep for 25ms, then try again.</div><div><br></div><div>-Ryan</div><div><br></div></div></div>