Skip to contentSkip to conversation

set td->io_ops to NULL before dlclose()

PATCH Justin Eno (jeno) 2 participants 2 messages Oct 10, 2017

lore
Share & tools
lore
#0 lore raw

Hi Jens,

I'm top-posting here to catch your attention. If it wasn't clear, the patch I sent is generally applicable and not intended just for the original reporter, i.e., it's a candidate for integration.

Thanks,
Justin

Quoted 73 lines · Justin Eno (jeno)
Quoted 33 lines · James R
Quoted 33 lines · James R

If using ioengine=/path/to/ioengine to dynamically load an ioengine, there is a race between free_ioengine()'s dlclose() and the td->io_ops dereference in reap_threads().

To resolve, td->io_ops must be set to NULL before calling dlclose() - this ensures the name check in reap_threads() does not try to reference memory no longer accessible due to the ioengine getting closed.

---
ioengines.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-) 
ioengines.c +2 -2
index c90a2ca5..6caf73da 100644 
@@ -170,10 +170,10 @@ void free_ioengine(struct thread_data *td) 
              td->eo = NULL; 
      } 
 
+    td->io_ops = NULL; 
+ 
      if (td->io_ops_dlhandle) 
              dlclose(td->io_ops_dlhandle); 
- 
-    td->io_ops = NULL; 
 } 
 
Quoted 8 lines
Quoted 4 lines
ioengines.c +0 -0
 void close_ioengine(struct thread_data *td) 
Signature · 1 line
2.12.2 

Please ignore.  This only narrows the race window - it does not close it.

I also have a system that is prone to this segfault.  Without some type of mutex, td_iops cannot be dereferenced safely in reap_threads().

This alternative patch works for me:

---
backend.c | 6 +-----

1 file changed, 1 insertion(+), 5 deletions(-) 
backend.c +1 -5
index ba6f585..17c0d5b 100644 
@@ -1929,11 +1929,7 @@ static void reap_threads(unsigned int *nr_running, uint64_t *t_rate, 
         for_each_td(td, i) { 
                 int flags = 0; 
 
-               /* 
-                * ->io_ops is NULL for a thread that has closed its 
-                * io engine 
-                */ 
-               if (td->io_ops && !strcmp(td->io_ops->name, "cpuio")) 
+               if (!strcmp(td->o.ioengine, "cpuio")) 
                         cputhreads++; 

                else
                        realthreads++;

Signature · 3 lines
1.8.3.1 

-- 
#1 lore raw
Quoted 5 lines · Justin Eno (jeno)

Hi Jens,

I'm top-posting here to catch your attention. If it wasn't clear, the patch I sent is generally applicable and not intended just for the original reporter, i.e., it's a candidate for integration.

Looks good to me, that should close the race. Applied, thanks.

Signature · 1 line
Jens Axboe