Skip to contentSkip to conversation

Fix to RTDM open problems

PATCH In review Anders Blomdell 3 participants 3 messages Jan 27, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix to RTDM open problems

When RTDM is exposed to code like this:

  device1 = rt_dev_open("some_device", O_RDWR);
  device2 = rt_dev_open("some_device", O_RDWR);

I get a SEGFAULT, which I attribute to a missing assignment to context_ptr in the case when the device is already busy, the lack of this assignment leads to a segfault in cleanup_instance.

ksrc/skins/rtdm/core.c 2006-01-27 11:14:43.000000000 +0100 renamed from ksrc/skins/rtdm/core.c~ 2006-01-07 18:08:34.000000000 +0100 +2 -0
@@ -136,6 +136,8 @@
 
         if (context->device) {
             xnlock_put_irqrestore(&rt_dev_lock, s);
+
+            *context_ptr = NULL;
             return -EBUSY;
         }
         context->device = device;
#1 lore raw
Quoted 16 lines · Anders Blomdell

When RTDM is exposed to code like this:

  device1 = rt_dev_open("some_device", O_RDWR);
  device2 = rt_dev_open("some_device", O_RDWR);

I get a SEGFAULT, which I attribute to a missing assignment to context_ptr in the case when the device is already busy, the lack of this assignment leads to a segfault in cleanup_instance.

--- xenomai-2.1-rc2/ksrc/skins/rtdm/core.c~ 2006-01-07
18:08:34.000000000 +0100
+++ xenomai-2.1-rc2/ksrc/skins/rtdm/core.c 2006-01-27
11:14:43.000000000 +0100

Review line 143

Quoted 8 lines
(quoted diff) +2 -0
@@ -136,6 +136,8 @@
 
         if (context->device) {
             xnlock_put_irqrestore(&rt_dev_lock, s);
+
+            *context_ptr = NULL;
             return -EBUSY;
         }
         context->device = device;

Yep, correct. Don't know, I must have smoked something while trying to fix this bug some weeks ago. Thanks for reporting and also providing a patch.

I just gave it another try, also re-organising the involved function create_instance() slightly. Please checkout latest SVN or try the following patch and let me know if this works now, really.

Thanks again,
Jan

core.c (working copy) renamed from core.c (revision 475) +7 -8
Index: core.c
===================================================================
@@ -116,13 +116,13 @@
     spl_t                   s;
 
 
+    *context_ptr = NULL;
+
     xnlock_get_irqsave(&rt_fildes_lock, s);
 
     *fildes_ptr = fildes = free_fildes;
-    if (!fildes) {
+    if (unlikely(!fildes)) {
         xnlock_put_irqrestore(&rt_fildes_lock, s);
-
-        *context_ptr = NULL;
         return -ENFILE;
     }
     free_fildes = fildes->next;
@@ -134,15 +134,13 @@
     if (context) {
         xnlock_get_irqsave(&rt_dev_lock, s);
 
-        if (context->device) {
+        if (unlikely(context->device)) {
             xnlock_put_irqrestore(&rt_dev_lock, s);
             return -EBUSY;
         }
         context->device = device;
 
         xnlock_put_irqrestore(&rt_dev_lock, s);
-
-        *context_ptr = context;
     } else {
         if (nrt_mem)
             context = kmalloc(sizeof(struct rtdm_dev_context) +
@@ -150,13 +148,14 @@
         else
             context = xnmalloc(sizeof(struct rtdm_dev_context) +
                                device->context_size);
-        *context_ptr = context;
-        if (!context)
+        if (unlikely(!context))
             return -ENOMEM;
 
         context->device = device;
     }
 
+    *context_ptr = context;
+
     context->fd  = get_fd(fildes);
     context->ops = &device->ops;
     atomic_set(&context->close_lock_count, 0);
#2 lore raw

Jan Kiszka schrieb:
..........

Quoted 2 lines

Yep, correct. Don't know, I must have smoked something while trying to fix this bug some weeks ago.

...........

Ah, now I know why it always smells a little funny in your office, must be good stuff. :-)

SCNR.

Cheers
Fu