From 6dcd3c2ad568cddde54d1b350130369d6cbb69e3 Mon Sep 17 00:00:00 2001 From: David Mason Date: Tue, 13 Feb 2024 00:47:38 -0800 Subject: [PATCH 1/2] Update jitinterface.cpp --- src/coreclr/vm/jitinterface.cpp | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/coreclr/vm/jitinterface.cpp b/src/coreclr/vm/jitinterface.cpp index f2f7d229d546f1..68281c3fe292c5 100644 --- a/src/coreclr/vm/jitinterface.cpp +++ b/src/coreclr/vm/jitinterface.cpp @@ -8241,17 +8241,30 @@ void CEEInfo::reportInliningDecision (CORINFO_METHOD_HANDLE inlinerHnd, if (CORProfilerEnableRejit()) { - // If ReJIT is enabled, there is a chance that a race happened where the profiler - // requested a ReJIT on a method, but before the ReJIT occurred an inlining happened. - // If we end up reporting an inlining on a method with non-default IL it means the race - // happened and we need to manually request ReJIT for it since it was missed. - CodeVersionManager* pCodeVersionManager = pCallee->GetCodeVersionManager(); - CodeVersionManager::LockHolder codeVersioningLockHolder; - ILCodeVersion ilVersion = pCodeVersionManager->GetActiveILCodeVersion(pCallee); - if (ilVersion.GetRejitState() != ILCodeVersion::kStateActive || !ilVersion.HasDefaultIL()) + ModuleID modId = 0; + mdMethodDef methodDef = mdMethodDefNil; + BOOL shouldCallReJIT = FALSE; + + { + // If ReJIT is enabled, there is a chance that a race happened where the profiler + // requested a ReJIT on a method, but before the ReJIT occurred an inlining happened. + // If we end up reporting an inlining on a method with non-default IL it means the race + // happened and we need to manually request ReJIT for it since it was missed. + CodeVersionManager* pCodeVersionManager = pCallee->GetCodeVersionManager(); + CodeVersionManager::LockHolder codeVersioningLockHolder; + ILCodeVersion ilVersion = pCodeVersionManager->GetActiveILCodeVersion(pCallee); + if (ilVersion.GetRejitState() != ILCodeVersion::kStateActive || !ilVersion.HasDefaultIL()) + { + shouldCallReJIT = TRUE; + modId = reinterpret_cast(pCaller->GetModule()); + methodDef = pCaller->GetMemberDef(); + } + } + + if (shouldCallReJIT) { - ModuleID modId = reinterpret_cast(pCaller->GetModule()); - mdMethodDef methodDef = pCaller->GetMemberDef(); + _ASSERTE(modId != 0); + _ASSERTE(methodDef != mdMethodDefNil); ReJitManager::RequestReJIT(1, &modId, &methodDef, static_cast(0)); } } From b8384228be63c1f28581715b73d75df293c06a7b Mon Sep 17 00:00:00 2001 From: David Mason Date: Wed, 14 Feb 2024 15:14:45 -0800 Subject: [PATCH 2/2] Update src/coreclr/vm/jitinterface.cpp --- src/coreclr/vm/jitinterface.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/coreclr/vm/jitinterface.cpp b/src/coreclr/vm/jitinterface.cpp index 68281c3fe292c5..20c8321a7c4960 100644 --- a/src/coreclr/vm/jitinterface.cpp +++ b/src/coreclr/vm/jitinterface.cpp @@ -8258,6 +8258,8 @@ void CEEInfo::reportInliningDecision (CORINFO_METHOD_HANDLE inlinerHnd, shouldCallReJIT = TRUE; modId = reinterpret_cast(pCaller->GetModule()); methodDef = pCaller->GetMemberDef(); + // Do Not call RequestReJIT inside this scope, calling RequestReJIT while holding the CodeVersionManager lock + // will cause deadlocks with other threads calling RequestReJIT since it tries to obtain the CodeVersionManager lock } }