<div dir="ltr">I encounter another "bad" behavior related to multiple firings of the UDev 'add' event for a given input node.<div><br></div><div>Typically on modest embedded systems, you do not want to run the exhaustive 'udevadm trigger' during the main booting sequence. That causes hundreds or thousands of UDev nodes to be crawled and processed by udevd all during the first moments of userspace. These overall bulk of these nodes are pretty much irrelevant to the use-case of the device. The time spent processing these non-essential /sys devices can easily slow down the device's progress toward starting a UI with basic touch support and loading drivers for the handful of essential peripherals by 10 or 15 seconds.</div><div><br></div><div>Instead, it works better to manually command the same UDev coldplugging that would have been done by 'udevadm trigger', but for a very small hand-picked set of devices rather than everything in /sys/devices. The time savings are large. Of course, for completeness you do eventually have to run 'udevadm trigger' so that the full set of hardware and kernel software features are activated, but that can wait until after the main KPIs are achieved.</div><div><br></div><div>This scheme generally works just fine. Manually stimulating udevd to coldplug the specific devices you need keeps everything general: applications that find their hardware with UDev (such as with libinput or Weston's DRM backend) all get to rely on their usual well-tested codepaths.</div><div><br></div><div>But there is a snag: if a device like /dev/input/event0 has been coldplugged once with the hands-on technique, then all the daemons that care about it have already seen one UDev ACTION=add event for it. When the late-running 'udevadm trigger' does its exhaustive sweep across /sys/devices, this will cause a second ACTION=add event to be triggered for /dev/input/event0. Currently (well, with libinput 1.1.1) this causes libinput -- and consequently Weston -- to open a second filedescriptor against /dev/input/event0, so that all input events are received in duplicate. That confuses the compositor's and applications' input event handling.</div><div><br></div><div>Would it be tolerable to put a patch into either libinput or Weston to guard against double-opening the same input device? I realize that the scheme outlined above is probably technically in violation of the expected UDev initialization scheme, but I'm not aware of any way to suppress udevd from broadcasting the 'add' action to all udev clients even though the device has already been coldplugged once. It seems to me at least plausible that the Weston stack would benefit from guarding against getting into this bad state from receiving unexpected UDev events.</div><div><br></div><div>-Matt</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Apr 4, 2018 at 6:51 AM, Pekka Paalanen <span dir="ltr"><<a href="mailto:ppaalanen@gmail.com" target="_blank">ppaalanen@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Pekka Paalanen <<a href="mailto:pekka.paalanen@collabora.co.uk">pekka.paalanen@collabora.co.<wbr>uk</a>><br>
<br>
Hi,<br>
<br>
here is a patch for a race that was troubling a distribution for an<br>
embedded device. The original form of this patch has been in developer<br>
use for months now, and it seems to work there. For this version I have<br>
only changed the logging and fixed a leak.<br>
<br>
I originally discussed this issue on #wayland with Peter Hutterer on<br>
September 22, 2017. The discussion is quoted below to refresh our<br>
memories.<br>
<br>
In the distribution, Weston is started by systemd, the system is not<br>
very CPU-powerful, and lots of things are happening during the boot,<br>
which may all contribute to making this race possible to lose.<br>
<br>
I've only tested briefly on my work desktop to see that Weston still<br>
appears to find the input devices I expect. Obviously my desktop would<br>
never lose the race, because there are no input devices being hotplugged<br>
at the same time as Weston is starting up.<br>
<br>
The timer/idle callback idea is not implemented here, and neither is the<br>
double-add filtering. Let me know if you require those.<br>
<br>
<br>
Thanks,<br>
pq<br>
<br>
<br>
< pq> whot, btw. we've looking into some fun libinput vs. udev<br>
device initialization race here. Apparently the device enumeration<br>
on libinput start-up may race with udev preparing devices at the<br>
same time, so the initial device enumeration for weston may see<br>
devices that have not had all their udev properties set yet. Do you<br>
recall ever fighting such issues?<br>
<br>
< pq> whot, another issue is that we may see a double-add of a<br>
device, first from the initial enumeration (with possibly missing<br>
properties) and then a second time as a hotplug event because<br>
libinput (correctly) listens for events before it enume existing<br>
devices.<br>
<br>
< whot> pq: yes, I've seen this a lot with the test suite, but never<br>
in real life<br>
<br>
< whot> I'm pretty sure in my case it's always a lingering udev<br>
device or some lingering properties<br>
<br>
< whot> e.g. a tablet has some keyboard properties set because the<br>
kernel re-uses the event node<br>
<br>
< whot> but that's triggered by the test suite using the path<br>
interface and there's bound to be a window where we can race. it<br>
shouldn't happen with the udev device, I think<br>
<br>
< pq> specifically, in the target device/system with touchscreens<br>
and weston, there are udev rules to set WL_OUTPUT on the device, but<br>
weston does not always see it.<br>
<br>
< pq> well, we use libinput via weston, and I don't think weston<br>
uses the path-based function. It's really the initial enumeration of<br>
input devices in libinput that has the issue.<br>
<br>
< whot> pq: a double-add is a bug, I can picture that happening with<br>
the current code. should be fixable<br>
<br>
 * whot mumples something about syspath comparisons to filter that<br>
out<br>
<br>
< whot> mumbles, even. but it's late nough that I'm also open for<br>
mumpling<br>
<br>
< pq> whot, nice. Yeah, let's see if we have a patch to submit or<br>
just a bug report, it might take a while.<br>
<br>
< whot> pq: for the missing properties I have no idea, but that<br>
would, if anything, be a systemd bug<br>
<br>
< pq> whot, really? What's udev_device_get_is_<wbr>initialised() for<br>
then? Does it not apply to the initial device enumeration as done by<br>
libinput?<br>
<br>
< pq> whot, how does initial device enumeration work? Does it go the<br>
udev daemon or just look in sysfs on its own?<br>
<br>
< whot> udev_device_get_is_initialized may help but it didn't with<br>
the test suite<br>
<br>
< whot> but for your case, it might just because you're fighting a<br>
different race<br>
<br>
< whot> should be easy enough to printf that to the log in your case<br>
and check if it's false when it fails<br>
<br>
< pq> whot, oh yeah, this didn't feel like a lingering device issue.<br>
<br>
< whot> initial enumeration: see udev_input_add_devices in<br>
src/udev-seat.c<br>
<br>
< whot> basically: "get list of devices maching subsystem input,<br>
create udev device for each syspath in that list"<br>
<br>
< pq> whot, our current idea is to have the initial enumeration to<br>
ignore devices where udev_device_get_is_<wbr>initialized() returns false,<br>
because presumably we will get the hotplug event later anyway.<br>
<br>
< pq> that would avoid any busy-loop waiting<br>
<br>
< whot> you could schedule a callback for that device's syspath<br>
<br>
< whot> takes the whole "presumably" out of the question :)<br>
<br>
< pq> you mean like an idle task to re-check it?<br>
<br>
< whot> yeah, that or a timer<br>
<br>
< whot> not sure we have idle sources in libinput right now, so a<br>
timer is your better option here<br>
<br>
< pq> whot, ok, sure. Thanks for the tip. :-)<br>
<br>
Nandor Han (1):<br>
  udev: validate input devices during cold-plug<br>
<br>
 src/udev-seat.c | 11 +++++++++++<br>
 1 file changed, 11 insertions(+)<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
2.16.1<br>
<br>
______________________________<wbr>_________________<br>
wayland-devel mailing list<br>
<a href="mailto:wayland-devel@lists.freedesktop.org">wayland-devel@lists.<wbr>freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/wayland-devel" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/wayland-devel</a><br>
</font></span></blockquote></div><br></div></div>