@@ -258,9 +258,12 @@ mca_rcache_base_registration_t *mca_rcache_base_vma_tree_find (mca_rcache_base_v
258
258
mca_rcache_base_vma_item_t * vma ;
259
259
mca_rcache_base_vma_reg_list_item_t * item ;
260
260
261
+ opal_mutex_lock (& vma_module -> vma_lock );
262
+
261
263
vma = (mca_rcache_base_vma_item_t * ) opal_rb_tree_find_with (& vma_module -> rb_tree , base ,
262
264
mca_rcache_base_vma_tree_node_compare_search );
263
265
if (!vma ) {
266
+ opal_mutex_unlock (& vma_module -> vma_lock );
264
267
return NULL ;
265
268
}
266
269
@@ -269,12 +272,18 @@ mca_rcache_base_registration_t *mca_rcache_base_vma_tree_find (mca_rcache_base_v
269
272
continue ;
270
273
}
271
274
272
- if (item -> reg -> bound >= bound )
275
+ if (item -> reg -> bound >= bound ) {
276
+ opal_mutex_unlock (& vma_module -> vma_lock );
273
277
return item -> reg ;
274
- if (!(item -> reg -> flags & MCA_RCACHE_FLAGS_PERSIST ))
278
+ }
279
+
280
+ if (!(item -> reg -> flags & MCA_RCACHE_FLAGS_PERSIST )) {
275
281
break ;
282
+ }
276
283
}
277
284
285
+ opal_mutex_unlock (& vma_module -> vma_lock );
286
+
278
287
return NULL ;
279
288
}
280
289
@@ -299,6 +308,8 @@ int mca_rcache_base_vma_tree_find_all (mca_rcache_base_vma_module_t *vma_module,
299
308
if (opal_list_get_size (& vma_module -> vma_list ) == 0 )
300
309
return cnt ;
301
310
311
+ opal_mutex_lock (& vma_module -> vma_lock );
312
+
302
313
do {
303
314
mca_rcache_base_vma_item_t * vma ;
304
315
mca_rcache_base_vma_reg_list_item_t * vma_item ;
@@ -316,25 +327,23 @@ int mca_rcache_base_vma_tree_find_all (mca_rcache_base_vma_module_t *vma_module,
316
327
}
317
328
318
329
OPAL_LIST_FOREACH (vma_item , & vma -> reg_list , mca_rcache_base_vma_reg_list_item_t ) {
319
- if (( vma_item -> reg -> flags & MCA_RCACHE_FLAGS_INVALID ) ||
330
+ if (vma_item -> reg -> flags & MCA_RCACHE_FLAGS_INVALID ||
320
331
is_reg_in_array (regs , cnt , vma_item -> reg )) {
321
332
continue ;
322
333
}
323
334
regs [cnt ++ ] = vma_item -> reg ;
324
335
if (cnt == reg_cnt ) {
336
+ opal_mutex_unlock (& vma_module -> vma_lock );
325
337
return cnt ; /* no space left in the provided array */
326
338
}
327
339
}
328
340
329
341
base = (unsigned char * )vma -> end + 1 ;
330
- } while (bound >= base );
342
+ } while (bound >= base );
331
343
332
- return cnt ;
333
- }
344
+ opal_mutex_unlock (& vma_module -> vma_lock );
334
345
335
- static inline int mca_rcache_base_vma_can_insert (mca_rcache_base_vma_module_t * vma_module , size_t nbytes , size_t limit )
336
- {
337
- return (0 == limit || vma_module -> reg_cur_cache_size + nbytes <= limit );
346
+ return cnt ;
338
347
}
339
348
340
349
static inline void mca_rcache_base_vma_update_byte_count (mca_rcache_base_vma_module_t * vma_module ,
@@ -343,12 +352,74 @@ static inline void mca_rcache_base_vma_update_byte_count (mca_rcache_base_vma_mo
343
352
vma_module -> reg_cur_cache_size += nbytes ;
344
353
}
345
354
355
+ int mca_rcache_base_vma_tree_iterate (mca_rcache_base_vma_module_t * vma_module , unsigned char * base ,
356
+ size_t size , int (* callback_fn ) (struct mca_rcache_base_registration_t * , void * ),
357
+ void * ctx )
358
+ {
359
+ unsigned char * bound = base + size - 1 ;
360
+ mca_rcache_base_vma_item_t * vma ;
361
+ int rc = OPAL_SUCCESS ;
362
+
363
+ if (opal_list_get_size (& vma_module -> vma_list ) == 0 ) {
364
+ /* nothin to do */
365
+ return OPAL_SUCCESS ;
366
+ }
367
+
368
+ opal_mutex_lock (& vma_module -> vma_lock );
369
+
370
+ do {
371
+ mca_rcache_base_vma_reg_list_item_t * vma_item , * next ;
372
+ vma = (mca_rcache_base_vma_item_t * ) opal_rb_tree_find_with (& vma_module -> rb_tree , base ,
373
+ mca_rcache_base_vma_tree_node_compare_closest );
374
+
375
+ if (NULL == vma ) {
376
+ /* base is bigger than any registered memory */
377
+ break ;
378
+ }
379
+
380
+ if (base < (unsigned char * ) vma -> start ) {
381
+ base = (unsigned char * ) vma -> start ;
382
+ continue ;
383
+ }
384
+
385
+ base = (unsigned char * )vma -> end + 1 ;
386
+
387
+ /* all the registrations in the vma may be deleted by the callback so keep a
388
+ * reference until we are done with it. */
389
+ OBJ_RETAIN (vma );
390
+
391
+ OPAL_LIST_FOREACH_SAFE (vma_item , next , & vma -> reg_list , mca_rcache_base_vma_reg_list_item_t ) {
392
+ rc = callback_fn (vma_item -> reg , ctx );
393
+ if (OPAL_SUCCESS != rc ) {
394
+ break ;
395
+ }
396
+ }
397
+
398
+ OBJ_RELEASE (vma );
399
+
400
+ if (OPAL_SUCCESS != rc ) {
401
+ break ;
402
+ }
403
+ } while (bound >= base );
404
+
405
+ opal_mutex_unlock (& vma_module -> vma_lock );
406
+
407
+ return rc ;
408
+ }
409
+
410
+ static inline int mca_rcache_base_vma_can_insert (mca_rcache_base_vma_module_t * vma_module , size_t nbytes , size_t limit )
411
+ {
412
+ return (0 == limit || vma_module -> reg_cur_cache_size + nbytes <= limit );
413
+ }
414
+
346
415
int mca_rcache_base_vma_tree_insert (mca_rcache_base_vma_module_t * vma_module ,
347
416
mca_rcache_base_registration_t * reg , size_t limit )
348
417
{
349
418
mca_rcache_base_vma_item_t * i ;
350
419
uintptr_t begin = (uintptr_t )reg -> base , end = (uintptr_t )reg -> bound ;
351
420
421
+ opal_mutex_lock (& vma_module -> vma_lock );
422
+
352
423
i = (mca_rcache_base_vma_item_t * ) opal_rb_tree_find_with (& vma_module -> rb_tree ,
353
424
(void * ) begin , mca_rcache_base_vma_tree_node_compare_closest );
354
425
@@ -373,6 +444,7 @@ int mca_rcache_base_vma_tree_insert (mca_rcache_base_vma_module_t *vma_module,
373
444
opal_list_append (& vma_module -> vma_list , & vma -> super );
374
445
begin = vma -> end + 1 ;
375
446
mca_rcache_base_vma_add_reg (vma , reg );
447
+ opal_mutex_unlock (& vma_module -> vma_lock );
376
448
return OPAL_SUCCESS ;
377
449
}
378
450
@@ -434,10 +506,14 @@ int mca_rcache_base_vma_tree_insert (mca_rcache_base_vma_module_t *vma_module,
434
506
i = (mca_rcache_base_vma_item_t * ) opal_list_get_next (& i -> super );
435
507
}
436
508
509
+ opal_mutex_unlock (& vma_module -> vma_lock );
510
+
437
511
return OPAL_SUCCESS ;
438
512
439
513
remove :
440
514
mca_rcache_base_vma_tree_delete (vma_module , reg );
515
+ opal_mutex_unlock (& vma_module -> vma_lock );
516
+
441
517
return OPAL_ERR_TEMP_OUT_OF_RESOURCE ;
442
518
}
443
519
@@ -453,17 +529,23 @@ int mca_rcache_base_vma_tree_delete (mca_rcache_base_vma_module_t *vma_module,
453
529
mca_rcache_base_registration_t * reg )
454
530
{
455
531
mca_rcache_base_vma_item_t * vma ;
532
+ opal_list_t deleted_vmas ;
533
+
534
+ opal_mutex_lock (& vma_module -> vma_lock );
456
535
457
536
vma = (mca_rcache_base_vma_item_t * )
458
537
opal_rb_tree_find_with (& vma_module -> rb_tree , reg -> base ,
459
538
mca_rcache_base_vma_tree_node_compare_search );
460
539
461
540
if (!vma ) {
541
+ opal_mutex_unlock (& vma_module -> vma_lock );
462
542
return OPAL_ERROR ;
463
543
}
464
544
545
+ OBJ_CONSTRUCT (& deleted_vmas , opal_list_t );
546
+
465
547
while (vma != (mca_rcache_base_vma_item_t * ) opal_list_get_end (& vma_module -> vma_list )
466
- && vma -> start <= (uintptr_t ) reg -> bound ) {
548
+ && vma -> start <= (uintptr_t ) reg -> bound ) {
467
549
mca_rcache_base_vma_remove_reg (vma , reg );
468
550
469
551
if (opal_list_is_empty (& vma -> reg_list )) {
@@ -473,7 +555,7 @@ int mca_rcache_base_vma_tree_delete (mca_rcache_base_vma_module_t *vma_module,
473
555
mca_rcache_base_vma_update_byte_count (vma_module ,
474
556
vma -> start - vma -> end - 1 );
475
557
opal_list_remove_item (& vma_module -> vma_list , & vma -> super );
476
- OBJ_RELEASE ( vma );
558
+ opal_list_append ( & deleted_vmas , & vma -> super );
477
559
vma = next ;
478
560
} else {
479
561
int merged ;
@@ -491,7 +573,7 @@ int mca_rcache_base_vma_tree_delete (mca_rcache_base_vma_module_t *vma_module,
491
573
prev -> end = vma -> end ;
492
574
opal_list_remove_item (& vma_module -> vma_list , & vma -> super );
493
575
opal_rb_tree_delete (& vma_module -> rb_tree , vma );
494
- OBJ_RELEASE ( vma );
576
+ opal_list_append ( & deleted_vmas , & vma -> super );
495
577
vma = prev ;
496
578
merged = 1 ;
497
579
}
@@ -505,7 +587,7 @@ int mca_rcache_base_vma_tree_delete (mca_rcache_base_vma_module_t *vma_module,
505
587
vma -> end = next -> end ;
506
588
opal_list_remove_item (& vma_module -> vma_list , & next -> super );
507
589
opal_rb_tree_delete (& vma_module -> rb_tree , next );
508
- OBJ_RELEASE ( next );
590
+ opal_list_append ( & deleted_vmas , & next -> super );
509
591
merged = 1 ;
510
592
}
511
593
} while (merged );
@@ -514,6 +596,11 @@ int mca_rcache_base_vma_tree_delete (mca_rcache_base_vma_module_t *vma_module,
514
596
}
515
597
}
516
598
599
+ opal_mutex_unlock (& vma_module -> vma_lock );
600
+
601
+ /* actually free vmas now that the lock has been dropped */
602
+ OPAL_LIST_DESTRUCT (& deleted_vmas );
603
+
517
604
return 0 ;
518
605
}
519
606
@@ -558,7 +645,7 @@ void mca_rcache_base_vma_tree_dump_range (mca_rcache_base_vma_module_t *vma_modu
558
645
OPAL_LIST_FOREACH (vma_item , & vma -> reg_list , mca_rcache_base_vma_reg_list_item_t ) {
559
646
reg = vma_item -> reg ;
560
647
opal_output (0 , " reg: base=%p, bound=%p, ref_count=%d, flags=0x%x" ,
561
- reg -> base , reg -> bound , reg -> ref_count , reg -> flags );
648
+ ( void * ) reg -> base , ( void * ) reg -> bound , reg -> ref_count , reg -> flags );
562
649
}
563
650
base = (unsigned char * )vma -> end + 1 ;
564
651
} while (bound >= base );
0 commit comments