This is an archive of the discontinued LLVM Phabricator instance.

[libomptarget] Fix a race condition in checkDeviceAndCtors
ClosedPublic

Authored by lechenyu on Oct 28 2022, 7:51 AM.

Details

Summary

When multiple threads invoke checkDeviceAndCtors, both of them may read true from the shared variable Device.HasPendingGlobals, and then invoke initLibrary redundantly. Therefore only protecting the access to Device.HasPendingGlobals is not sufficient to guarantee that initLibrary is invoked just once.

To fix this race condition, we move the invocation of initLibrary into the critical section, and remove the same lock inside initLibrary,

Diff Detail

Event Timeline

lechenyu created this revision.Oct 28 2022, 7:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 28 2022, 7:51 AM
lechenyu requested review of this revision.Oct 28 2022, 7:51 AM

This should work. On the other hand, is it possible to use init_once somehow? I think that'd be a better way.

This should work. On the other hand, is it possible to use init_once somehow? I think that'd be a better way.

Could we use init_once? We need to do this once per device right after the images have been loaded.

jhuber6 accepted this revision.Oct 28 2022, 11:21 AM

I think this should be fine. I believe I hit a deadlock here before so it's good to fix.

This revision is now accepted and ready to land.Oct 28 2022, 11:21 AM

This should work. On the other hand, is it possible to use init_once somehow? I think that'd be a better way.

Could we use init_once? We need to do this once per device right after the images have been loaded.

Once per device?

Once per device?

It just means that we'd need an array of std::once_flags for each device. It's not a huge barrier but not sure if it's that much more convenient.

tianshilei1992 added a comment.EditedOct 28 2022, 2:36 PM

Once per device?

It just means that we'd need an array of std::once_flags for each device. It's not a huge barrier but not sure if it's that much more convenient.

Yeah, that's what I mean. I generally think std::once_flags is cleaner than this method. I'm fine using PendingGlobalsMtx here as we can't get rid of it anyway.

Once per device?

It just means that we'd need an array of std::once_flags for each device. It's not a huge barrier but not sure if it's that much more convenient.

Yeah, that's what I mean. I generally think std::once_flags is cleaner than this method. I'm fine using PendingGlobalsMtx here as we can't get rid of it anyway.

Hi, Shilei,

Should I land this patch or replace the lock with std::once_flags?

tianshilei1992 accepted this revision.Oct 29 2022, 12:05 PM

Let’s just land it for now. Thanks.