Skip to content

Commit fe6c83a

Browse files
authored
Merge pull request intel#2507 from AllanZyne/review/yang/fix_msan_shadow
[DeviceMSAN] Fix MemToShadow algorithm and VA reservation
2 parents 93fc133 + cf0c948 commit fe6c83a

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

source/loader/layers/sanitizer/asan/asan_shadow.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,14 @@ ur_result_t ShadowMemoryGPU::Setup() {
104104
// shadow memory for each contexts, this will cause out-of-resource error when user uses
105105
// multiple contexts. Therefore, we just create one shadow memory here.
106106
static ur_result_t Result = [this]() {
107-
size_t ShadowSize = GetShadowSize();
107+
const size_t ShadowSize = GetShadowSize();
108+
// To reserve very large amount of GPU virtual memroy, the pStart param should be beyond
109+
// the SVM range, so that GFX driver will automatically switch to reservation on the GPU
110+
// heap.
111+
const void *StartAddress = (void *)(0x100'0000'0000'0000ULL);
108112
// TODO: Protect Bad Zone
109113
auto Result = getContext()->urDdiTable.VirtualMem.pfnReserve(
110-
Context, nullptr, ShadowSize, (void **)&ShadowBegin);
114+
Context, StartAddress, ShadowSize, (void **)&ShadowBegin);
111115
if (Result != UR_RESULT_SUCCESS) {
112116
getContext()->logger.error(
113117
"Shadow memory reserved failed with size {}: {}",

source/loader/layers/sanitizer/msan/msan_shadow.cpp

+26-13
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,23 @@ ur_result_t MsanShadowMemoryGPU::Setup() {
134134
// shadow memory for each contexts, this will cause out-of-resource error when user uses
135135
// multiple contexts. Therefore, we just create one shadow memory here.
136136
static ur_result_t Result = [this]() {
137-
size_t ShadowSize = GetShadowSize();
137+
const size_t ShadowSize = GetShadowSize();
138+
// To reserve very large amount of GPU virtual memroy, the pStart param should be beyond
139+
// the SVM range, so that GFX driver will automatically switch to reservation on the GPU
140+
// heap.
141+
const void *StartAddress = (void *)(0x100'0000'0000'0000ULL);
138142
// TODO: Protect Bad Zone
139143
auto Result = getContext()->urDdiTable.VirtualMem.pfnReserve(
140-
Context, nullptr, ShadowSize, (void **)&ShadowBegin);
141-
if (Result == UR_RESULT_SUCCESS) {
142-
ShadowEnd = ShadowBegin + ShadowSize;
143-
// Retain the context which reserves shadow memory
144-
getContext()->urDdiTable.Context.pfnRetain(Context);
144+
Context, StartAddress, ShadowSize, (void **)&ShadowBegin);
145+
if (Result != UR_RESULT_SUCCESS) {
146+
getContext()->logger.error(
147+
"Shadow memory reserved failed with size {}: {}",
148+
(void *)ShadowSize, Result);
149+
return Result;
145150
}
146-
147-
// Set shadow memory for null pointer
148-
ManagedQueue Queue(Context, Device);
151+
ShadowEnd = ShadowBegin + ShadowSize;
152+
// Retain the context which reserves shadow memory
153+
getContext()->urDdiTable.Context.pfnRetain(Context);
149154
return UR_RESULT_SUCCESS;
150155
}();
151156
return Result;
@@ -278,13 +283,21 @@ MsanShadowMemoryGPU::ReleaseShadow(std::shared_ptr<MsanAllocInfo> AI) {
278283
}
279284

280285
uptr MsanShadowMemoryPVC::MemToShadow(uptr Ptr) {
281-
assert(Ptr & 0xFF00000000000000ULL && "Ptr must be device USM");
282-
return ShadowBegin + (Ptr & 0x3FFF'FFFF'FFFFULL);
286+
assert(Ptr & 0xff00'0000'0000'0000ULL && "Ptr must be device USM");
287+
if (Ptr < ShadowBegin) {
288+
return Ptr + (ShadowBegin - 0xff00'0000'0000'0000ULL);
289+
} else {
290+
return Ptr - (0xff00'ffff'ffff'ffffULL - ShadowEnd);
291+
}
283292
}
284293

285294
uptr MsanShadowMemoryDG2::MemToShadow(uptr Ptr) {
286-
assert(Ptr & 0xFFFF000000000000ULL && "Ptr must be device USM");
287-
return ShadowBegin + (Ptr & 0x3FFF'FFFF'FFFFULL);
295+
assert(Ptr & 0xffff'0000'0000'0000ULL && "Ptr must be device USM");
296+
if (Ptr < ShadowBegin) {
297+
return Ptr + (ShadowBegin - 0xffff'8000'0000'0000ULL);
298+
} else {
299+
return Ptr - (0xffff'ffff'ffff'ffffULL - ShadowEnd);
300+
}
288301
}
289302

290303
} // namespace msan

0 commit comments

Comments
 (0)