Hans de Goede wrote:
> I had looking further into this / writing a patch on my todo list,
> looks like you beat me to it, thanks for working on this!
> 
> You're patch looks good, one possible optimization would be to
> give arm_timerfd_for_next_timeout() an extra "int stop_timer"
> argument and only call disarm_timerfd() when that argument is true.

The proposed patch is a bit more complicated than neccessary, and
arm_timerfd_for_next_timeout() uses the return value to indicate
if a timer is now running or not. Thanks to debugging by Vincent
the bug was fixed in libusb yesterday.

I'm attaching a patch that applies on top of libusbx.git, and for
convenience it is also available via

git fetch git://git.libusb.org/libusb-stuge.git for_libusbx/timerfd_fix && \
  git cherry-pick FETCH_HEAD


//Peter
>From 26ff10182876b67cc6035d4db7f2732dd3487d44 Mon Sep 17 00:00:00 2001
From: Peter Stuge <pe...@stuge.se>
Date: Tue, 10 Jul 2012 01:46:38 +0200
Subject: [PATCH] io.c: Only disarm timerfd when no flying transfer has a
 timeout

Commit 4630fc22cff8ad3e1afa9b223378c0aabe282b5c made libusb work
correctly when built to use timerfd but run on a kernel without
timerfd support. The commit unfortunately also broke the logic
during transfer completion which decides if the timerfd will be
disarmed or not, and disarm_timerfd() was called even if the
timerfd had already been armed with the next flying transfer
timeout.

Before the offending commit the timerfd would be disarmed only
when arm_timerfd_for_next_timeout() reported that there was no
flying transfer with a timeout. Let's restore that behavior.

The bug was spotted through code review both by the author and
by Hans de Goede, but was not squashed until Vincent Pellier
experienced transfers never timing out, and helped confirm
the source of the problem. Many thanks!

References http://libusb.org/ticket/73

libusb.git commit 1bd831c4e88857bff2f1670c89eda1d04da1cc54

Signed-off-by: Peter Stuge <pe...@stuge.se>
---
 libusb/io.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/libusb/io.c b/libusb/io.c
index d06d375..cf41ee6 100644
--- a/libusb/io.c
+++ b/libusb/io.c
@@ -1464,9 +1464,11 @@ int usbi_handle_transfer_completion(struct usbi_transfer 
*itransfer,
        if (usbi_using_timerfd(ctx)) {
                if (r < 0)
                        return r;
-               r = disarm_timerfd(ctx);
-               if (r < 0)
-                       return r;
+               else if (0 == r) {
+                       r = disarm_timerfd(ctx);
+                       if (r < 0)
+                               return r;
+               }
        }
 
        if (status == LIBUSB_TRANSFER_COMPLETED
-- 
1.7.4.1.343.ga91df.dirty

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
libusbx-devel mailing list
libusbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusbx-devel

Reply via email to