🐎 Count a surface's pages out of the contexts' lock
An ask that may defer its block counted its surface's pages under the contexts' lock, twice: once to decide, and again before deferring. A count makes a mapping of the surface's memory, and 0.2% of counts took 1 to 9.1 ms there (measured on a Radeon 780M host, macOS 26.7.2 and 11.7.11), holding every process's block allocations, surface asks and releases for that long. The ask now drops the lock for each count, takes it again and looks again, and counts again where it waited under the lock after a count, so each count still stands for the lock's span it decides in. Which blocks are deferred, and what the host is told, are as before; only the lock's hold shrinks.
This commit is contained in:
1 parent
f40f2d9e92
commit
86bb289a8e
3 files changed
+80
-42
No files matched your search
@@ -240,7 +240,8 @@ RgfxSurfacePageCounts rgfxSurfaceLocksPageCounts(OSObject *surface);
|
||||
// Whether `surface` reads fresh, to look at: its seed 0, its pages counted, none of them
|
||||
// compressed or paged out, none resident or as many as `listed`, the pages the ask lists of them
|
||||
// (rgfxDeferRead, RgfxDeferCarry.hpp; 0 for none), and its memory holding `length` bytes. Racy:
|
||||
// the deferral's own read is rgfxSurfaceLocksDefer's.
|
||||
// the deferral's own read is rgfxSurfaceLocksDefer's. Called holding no lock of the kext's: its
|
||||
// count makes a mapping, which may wait milliseconds.
|
||||
bool rgfxSurfaceLocksFresh(OSObject *surface, UInt64 length, uint64_t listed);
|
||||
// What rgfxSurfaceLocksDefer found.
|
||||
enum RgfxDeferAnswer {
|
||||
|
||||
@@ -1750,35 +1750,53 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
|
||||
} asker;
|
||||
if (inPlace && surfaceId && (surfaceCache || deferredBudget))
|
||||
asker.object = rgfxSurfaceLocksLookup(this, task, surfaceId);
|
||||
IOLockLock(contextLock);
|
||||
if (!table.entries) {
|
||||
auto *made = static_cast<RgfxSurfaceEntry *>(
|
||||
IOMalloc(sizeof(RgfxSurfaceEntry) * kMaxSurfaceBlocks));
|
||||
if (made) {
|
||||
bzero(made, sizeof(RgfxSurfaceEntry) * kMaxSurfaceBlocks);
|
||||
table = {made, kMaxSurfaceBlocks};
|
||||
}
|
||||
}
|
||||
// Whether the asker's surface reads fresh (rgfxSurfaceLocksFresh), counted out of the contexts'
|
||||
// lock, which every process's block allocations, surface asks and releases take: a count makes
|
||||
// a mapping of the surface's memory, which waited up to 9.1 ms (RgfxAcceleratorLocks.cpp). The
|
||||
// ask counts where a deferral is otherwise its, then takes the lock again and looks again;
|
||||
// where it waits under the lock after the count, it counts again.
|
||||
bool fresh = false, counted = false;
|
||||
bool defer = false, deferredFound = false, cachedFound = false;
|
||||
// The surface's resident pages the ask listed, where the host lets a deferral carry them
|
||||
// (RgfxDeferCarry.hpp): a surface fresh but for those may be deferred too.
|
||||
const uint64_t listed =
|
||||
resident && deferCarries ? rgfxResidentPages(*resident, length, PAGE_SIZE) : 0;
|
||||
if (!listed)
|
||||
resident = nullptr;
|
||||
if (asker.object && table.entries && surfaceCache)
|
||||
cachedFound = rgfxSurfaceFindCached(table, asker.object, length, size, true) >= 0;
|
||||
if (asker.object && table.entries && deferredBudget && !cachedFound) {
|
||||
int32_t d = -1;
|
||||
while (asker.object && (d = rgfxSurfaceFindDeferred(table, asker.object)) >= 0 &&
|
||||
table.entries[d].pending)
|
||||
IOLockSleep(contextLock, &surfaceTable, THREAD_UNINT);
|
||||
deferredFound = d >= 0;
|
||||
// Where the device answers which surfaces need a cache, the deferral asks with the
|
||||
// surface's layout: an ask with none (an older bundle's) stays in place, as it did.
|
||||
defer = asker.object && !deferredFound && cacheable && (!askCache || layout) &&
|
||||
rgfxSurfaceDeferFits(table, length, deferredBudget) &&
|
||||
rgfxSurfaceLocksFresh(asker.object, length, listed);
|
||||
uint64_t listed = 0;
|
||||
for (;;) {
|
||||
IOLockLock(contextLock);
|
||||
if (!table.entries) {
|
||||
auto *made = static_cast<RgfxSurfaceEntry *>(
|
||||
IOMalloc(sizeof(RgfxSurfaceEntry) * kMaxSurfaceBlocks));
|
||||
if (made) {
|
||||
bzero(made, sizeof(RgfxSurfaceEntry) * kMaxSurfaceBlocks);
|
||||
table = {made, kMaxSurfaceBlocks};
|
||||
}
|
||||
}
|
||||
defer = deferredFound = cachedFound = false;
|
||||
// The surface's resident pages the ask listed, where the host lets a deferral carry
|
||||
// them (RgfxDeferCarry.hpp): a surface fresh but for those may be deferred too.
|
||||
listed = resident && deferCarries ? rgfxResidentPages(*resident, length, PAGE_SIZE) : 0;
|
||||
if (!listed)
|
||||
resident = nullptr;
|
||||
if (asker.object && table.entries && surfaceCache)
|
||||
cachedFound = rgfxSurfaceFindCached(table, asker.object, length, size, true) >= 0;
|
||||
if (asker.object && table.entries && deferredBudget && !cachedFound) {
|
||||
int32_t d = -1;
|
||||
while (asker.object && (d = rgfxSurfaceFindDeferred(table, asker.object)) >= 0 &&
|
||||
table.entries[d].pending) {
|
||||
counted = false;
|
||||
IOLockSleep(contextLock, &surfaceTable, THREAD_UNINT);
|
||||
}
|
||||
deferredFound = d >= 0;
|
||||
// Where the device answers which surfaces need a cache, the deferral asks with the
|
||||
// surface's layout: an ask with none (an older bundle's) stays in place, as it did.
|
||||
defer = asker.object && !deferredFound && cacheable && (!askCache || layout) &&
|
||||
rgfxSurfaceDeferFits(table, length, deferredBudget);
|
||||
if (defer && !counted) {
|
||||
IOLockUnlock(contextLock);
|
||||
fresh = rgfxSurfaceLocksFresh(asker.object, length, listed);
|
||||
counted = true;
|
||||
continue;
|
||||
}
|
||||
defer = defer && fresh;
|
||||
}
|
||||
break;
|
||||
}
|
||||
IOLockUnlock(contextLock);
|
||||
// A cached block of the surface is the ask's, found by the surface, only where the asker
|
||||
@@ -1818,7 +1836,11 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
|
||||
IOLockLock(contextLock);
|
||||
// The ask finds no deferred block and has no pages: it wires them, and looks again.
|
||||
bool needPages = false;
|
||||
// The surface counted, out of the lock, just before this pass took it.
|
||||
bool countedNow = false;
|
||||
for (;;) {
|
||||
bool countedHere = countedNow;
|
||||
countedNow = false;
|
||||
int32_t d = -1;
|
||||
while (table.entries &&
|
||||
(rgfxSurfacePending(table, runs, count) ||
|
||||
@@ -1826,8 +1848,10 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
|
||||
table.entries[d].pending) ||
|
||||
((d = rgfxSurfaceFindCached(table, asker.object, length, size,
|
||||
mapsSurface)) >= 0 &&
|
||||
table.entries[d].pending)))
|
||||
table.entries[d].pending))) {
|
||||
countedHere = false;
|
||||
IOLockSleep(contextLock, &surfaceTable, THREAD_UNINT);
|
||||
}
|
||||
differs = false;
|
||||
at = -1;
|
||||
// A deferred block of the asker's surface is its block, held again where dormant, and
|
||||
@@ -1865,9 +1889,18 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
|
||||
break;
|
||||
}
|
||||
// No pages, no deferred block: a new one is deferred where its surface is fresh
|
||||
// still, and fits the budget.
|
||||
if (!count && (!defer || !rgfxSurfaceDeferFits(table, length, deferredBudget) ||
|
||||
!rgfxSurfaceLocksFresh(asker.object, length, listed))) {
|
||||
// still, and fits the budget. The surface is counted again out of the lock, and the
|
||||
// pass looks again under it.
|
||||
const bool fits =
|
||||
!count && defer && rgfxSurfaceDeferFits(table, length, deferredBudget);
|
||||
if (fits && !countedHere) {
|
||||
IOLockUnlock(contextLock);
|
||||
fresh = rgfxSurfaceLocksFresh(asker.object, length, listed);
|
||||
IOLockLock(contextLock);
|
||||
countedNow = true;
|
||||
continue;
|
||||
}
|
||||
if (!count && (!fits || !fresh)) {
|
||||
needPages = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -52,9 +52,10 @@
|
||||
*
|
||||
* A count of a surface's pages maps its memory into the kernel, walks the mapping's regions and
|
||||
* removes the mapping (rgfxSurfaceLocksPageCounts). Each ask that may defer a block counts
|
||||
* (rgfxSurfaceLocksFresh), and so does the deferral's read under the surface's lock. Measured
|
||||
* with every count's time logged (a scratch build of this code, the log after the timed span),
|
||||
* on a Radeon 780M Linux host, over a cold boot, Safari's scroll and Mission Control:
|
||||
* (rgfxSurfaceLocksFresh), out of the contexts' lock, and so does the deferral's read under the
|
||||
* surface's lock. Measured with every count's time logged (a scratch build of this code, the log
|
||||
* after the timed span), on a Radeon 780M Linux host, over a cold boot, Safari's scroll and
|
||||
* Mission Control:
|
||||
* - 26.7.2 (25G332): 3695 counts, median 9.8 us, 99th percentile 68 us, 70.7 ms in all;
|
||||
* - 11.7.11 (20G1443): 1516 counts, median 13.3 us, 99th percentile 65 us, 29.3 ms in all.
|
||||
* In medians, the mapping's making takes 4-8 us and its removal about 1 us, whatever the size;
|
||||
@@ -63,7 +64,10 @@
|
||||
* pages is slower: 46 us for a 7296 KiB tile holding some. Every mapping lay in one of IOKit's
|
||||
* pageable maps, in one region, and every count counted; no page was compressed or paged out in
|
||||
* these runs. The longest counts, 1-9 ms, 0.2% of them, waited mostly in the mapping's making:
|
||||
* on 26.7.2 the longest, 9.1 ms, was for a 4 KiB surface.
|
||||
* on 26.7.2 the longest, 9.1 ms, was for a 4 KiB surface. Under the contexts' lock, which every
|
||||
* process's block allocations, surface asks and releases, and context opens and closes take, such
|
||||
* a count held all of them that long: an ask counts out of it (RgfxAcceleratorContexts.cpp,
|
||||
* acquireSurfaceBlock).
|
||||
*
|
||||
* Our own code; it names SDK-public IOKit calls and IOSurface's interface (RgfxIOSurfaceKPI.hpp).
|
||||
*/
|
||||
@@ -810,11 +814,11 @@ RgfxSurfacePageCounts rgfxSurfaceLocksPageCounts(OSObject *surface)
|
||||
|
||||
bool rgfxSurfaceLocksFresh(OSObject *surface, UInt64 length, uint64_t listed)
|
||||
{
|
||||
// Called under the contexts' lock, kept there on purpose: IOSurface's getters here load a
|
||||
// field and take no lock (read from IOSurface's x86_64 code in macOS 26.6's KDK; op 5 calls
|
||||
// getClientRetainCount holding IOSurface's own lock at each client's release), and the count
|
||||
// maps and walks the surface's memory through IOKit and the VM, never IOSurface (the lock
|
||||
// order, ContextState in RgfxAcceleratorContexts.cpp).
|
||||
// Called out of the contexts' lock (acquireSurfaceBlock), whose holder would hold every
|
||||
// process's block allocations and surface asks for the count's mapping, which waited up to
|
||||
// 9.1 ms (above). It takes no lock of the kext's: IOSurface's getters here load a field and
|
||||
// take no lock (read from IOSurface's x86_64 code in macOS 26.6's KDK), and the count maps and
|
||||
// walks the surface's memory through IOKit and the VM, never IOSurface.
|
||||
const uint32_t seed = rgfxIOSurfaceLockSeed(surface);
|
||||
IOMemoryDescriptor *md = OSDynamicCast(IOMemoryDescriptor, rgfxIOSurfaceMemory(surface));
|
||||
return md && length <= md->getLength() &&
|
||||
|
||||
Reference in new issue
Block a user