[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [RFC Patch v2 10/17] blktap2: use correct way to get free event id



If we register/unregister event too many times, and we use event id
from 1 again. But we don't check it if it is used.

Signed-off-by: Wen Congyang <wency@xxxxxxxxxxxxxx>
Cc: Shriram Rajagopalan <rshriram@xxxxxxxxx>
---
 tools/blktap2/drivers/scheduler.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)

diff --git a/tools/blktap2/drivers/scheduler.c 
b/tools/blktap2/drivers/scheduler.c
index 6b8d009..dd608dd 100644
--- a/tools/blktap2/drivers/scheduler.c
+++ b/tools/blktap2/drivers/scheduler.c
@@ -160,6 +160,31 @@ scheduler_run_events(scheduler_t *s)
        }
 }
 
+static int
+get_free_id(scheduler_t *s)
+{
+       event_t *event, *tmp;
+       int old_uuid = s->uuid;
+       int id = s->uuid++;
+
+       if (!s->uuid)
+               s->uuid++;
+
+retry:
+       scheduler_for_each_event(s, event, tmp)
+               if (event->id == id) {
+                       id = s->uuid++;
+                       if (!s->uuid)
+                               s->uuid++;
+                       if (id == old_uuid)
+                               return 0;
+
+                       goto retry;
+               }
+
+       return id;
+}
+
 int
 scheduler_register_event(scheduler_t *s, char mode, int fd,
                         int timeout, event_cb_t cb, void *private)
@@ -187,10 +212,12 @@ scheduler_register_event(scheduler_t *s, char mode, int 
fd,
        event->deadline = now.tv_sec + timeout;
        event->cb       = cb;
        event->private  = private;
-       event->id       = s->uuid++;
+       event->id       = get_free_id(s);
 
-       if (!s->uuid)
-               s->uuid++;
+       if (!event->id) {
+               free(event);
+               return -EBUSY;
+       }
 
        list_add_tail(&event->next, &s->events);
 
-- 
1.9.3


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.