🐛 Notify pipe transactions only when they carry a cookie

WindowServer drives the display pipe in the transactional model while
kIOWindowServerActiveAttribute carries kIOWSAA_Transactional ('wsrv'
0x80000013 at its start, then 0x11). When its session restarts it hands the
display back to the CPU ('wsrv' 0x2) and submits the shutdown's frames, each
with cookie 0 at +0x100, waiting on each with selector 9 before it exits. The
kext notified every completion. On a macOS 26 guest, WindowServer aborted in
IOAccelerator's _ioAccelDisplayPipeTransactionNotifyCallback about 5 ms after
the kext sent the notification of the first cookie-less transaction, and the
session's next WindowServer started over it.

The cookie is the transaction's own sign that a notification is wanted. The
pipe now notifies a completion only where its transaction carries a cookie;
every transaction still completes for its waiters and for the display. The
bookkeeping moves into RgfxPipeTransactions.hpp, with a native test that
replays the hand-over's order.

Measured on a cold boot of the x86 guest with the same rule keyed to 'wsrv'
instead of the cookie, which notifies the same transactions on every one
measured: 4469 transactions over three hand-overs (a reboot, a logout and a
loginwindow death). All 4461 submitted in the model carried a cookie and were
notified; each hand-over's 4 shutdown frames carried none and were not. Each
WindowServer then exits with exit(0) and no crash report, every wait returns
(26 ms at most), and the next WindowServer enters the model and composites
with Metal.
This commit is contained in:
Anees Iqbal committed 2026-11-05 21:59:19 -08:00
1 parent 01f5cbe2f0
commit 29f97fddcf
5 files changed
+259 -44

No files matched your search

+1 -1
View File
@@ -89,7 +89,7 @@ check: $(BINARY) $(BUILD)/obj/layout.o
# The native tests: header-only rules of the kext's, built for this Mac.
TESTS = $(BUILD)/tests/spans_test $(BUILD)/tests/blocks_test $(BUILD)/tests/surface_blocks_test \
$(BUILD)/tests/surface_test $(BUILD)/tests/pipe_clients_test \
$(BUILD)/tests/surface_test $(BUILD)/tests/pipe_clients_test $(BUILD)/tests/pipe_transactions_test \
$(BUILD)/tests/window_test $(BUILD)/tests/scanout_owner_test \
$(BUILD)/tests/display_modes_test
+32 -30
View File
@@ -53,6 +53,8 @@ constexpr UInt64 kAcceleratorId = 1;
// every 600th transaction.
constexpr UInt32 kLogFirst = 48;
constexpr UInt32 kLogEvery = 600;
// The transactions with no cookie logged, per client.
constexpr UInt32 kLogUnnotified = 8;
// A frame at 60 Hz in mach_absolute_time units (nanoseconds on x86-64): the VBL's next-tick
// estimate before two ticks have been seen.
@@ -105,7 +107,8 @@ bool RgfxDisplayPipeUserClient::initWithTask(task_t owningTask, void *securityID
completed = 0;
lastVblTime = 0;
logged = 0;
outstandingCount = 0;
unnotifiedLogged = 0;
transactions = Transactions();
return lock != nullptr;
}
@@ -381,22 +384,28 @@ IOReturn RgfxDisplayPipeUserClient::sSubmit(OSObject *target, void *, IOExternal
// Room for one more outstanding transaction, waiting up to five seconds.
uint64_t deadline = 0;
clock_interval_to_deadline(5000, kMillisecondScale, &deadline);
while (self->outstandingCount >= kMaxOutstanding) {
while (!self->transactions.hasRoom()) {
if (IOLockSleepDeadline(self->lock, &self->completed, deadline, THREAD_UNINT) == THREAD_TIMED_OUT &&
self->outstandingCount >= kMaxOutstanding) {
!self->transactions.hasRoom()) {
IOLockUnlock(self->lock);
IOLog("RGFX pipe: transaction %u: no room in 5 s, not sent\n", id);
return kIOReturnTimeout;
}
}
// Notified at its completion where it carries a cookie (RgfxPipeTransactions.hpp).
bool notified = rgfxPipeNotifies(cookie);
if (!notified && self->unnotifiedLogged < kLogUnnotified) {
self->unnotifiedLogged++;
IOLog("RGFX pipe: transaction %u (options 0x%x) carries no cookie: it completes for its "
"waiters, without a notification\n",
id, rd32(t, kTxnOptions));
}
if (planeType != kPlaneIOSurface || surface == 0) {
// Nothing for the host to show: the display keeps its surface, and the transaction is
// done with the last one before it (at once where none is outstanding).
if (self->outstandingCount == 0)
self->completeLocked(id, cookie, mach_absolute_time());
else
self->outstanding[self->outstandingCount++] = {
self->outstanding[self->outstandingCount - 1].seqno, id, cookie};
Transactions::Completion now = {};
if (self->transactions.nothingToShow(id, cookie, notified, &now))
self->completeLocked(now, mach_absolute_time());
IOLockUnlock(self->lock);
return kIOReturnSuccess;
}
@@ -409,7 +418,7 @@ IOReturn RgfxDisplayPipeUserClient::sSubmit(OSObject *target, void *, IOExternal
IOLog("RGFX pipe: transaction %u: PRESENT_SURFACE not sent: 0x%x\n", id, r);
return r;
}
self->outstanding[self->outstandingCount++] = {seqno, id, cookie};
self->transactions.sent(seqno, id, cookie, notified);
IOLockUnlock(self->lock);
return kIOReturnSuccess;
}
@@ -448,16 +457,16 @@ IOReturn RgfxDisplayPipeUserClient::sWait(OSObject *target, void *, IOExternalMe
// MARK: - Notifications
// Completes transaction `transactionId`: its completion notification, and waiters woken. Called
// with the lock held.
void RgfxDisplayPipeUserClient::completeLocked(UInt32 transactionId, UInt64 cookie, UInt64 time)
// Completes a transaction: waiters woken, and its completion notification where it is notified
// (RgfxPipeTransactions.hpp) and one is registered. Called with the lock held.
void RgfxDisplayPipeUserClient::completeLocked(const Transactions::Completion &c, UInt64 time)
{
if (transactionId > completed)
completed = transactionId;
if (registered[kKindTransaction]) {
if (c.id > completed)
completed = c.id;
if (c.notified && registered[kKindTransaction]) {
io_user_reference_t words[kCompletionWords] = {};
words[0] = cookie;
words[1] = static_cast<io_user_reference_t>(transactionId) << 32;
words[0] = c.cookie;
words[1] = static_cast<io_user_reference_t>(c.id) << 32;
words[3] = time;
words[8] = 1;
sendAsyncResult64(references[kKindTransaction], kIOReturnSuccess, words, kCompletionWords);
@@ -477,27 +486,20 @@ void RgfxDisplayPipeUserClient::presentHook(OSObject *client, UInt64 seqno, UInt
void RgfxDisplayPipeUserClient::presented(UInt64 seqno, UInt32 status)
{
IOLockLock(lock);
UInt32 match = kMaxOutstanding;
for (UInt32 i = 0; i < outstandingCount; i++) {
if (outstanding[i].seqno == seqno)
match = i;
}
if (match == kMaxOutstanding) {
Transactions::Completion done[kMaxOutstanding];
UInt32 n = transactions.answered(seqno, done);
if (n == 0) {
IOLockUnlock(lock);
return;
}
if (status != 0 && logged < kLogFirst) {
logged++;
IOLog("RGFX pipe: transaction %u: the host did not show its IOSurface (status %u)\n",
outstanding[match].id, status);
done[n - 1].id, status);
}
UInt64 now = mach_absolute_time();
for (UInt32 i = 0; i <= match; i++)
completeLocked(outstanding[i].id, outstanding[i].cookie, now);
UInt32 left = outstandingCount - (match + 1);
for (UInt32 i = 0; i < left; i++)
outstanding[i] = outstanding[match + 1 + i];
outstandingCount = left;
for (UInt32 i = 0; i < n; i++)
completeLocked(done[i], now);
IOLockUnlock(lock);
}
+12 -13
View File
@@ -21,10 +21,11 @@
* A submitted transaction's plane 0, where it is an IOSurface, goes to the host on the control
* ring as PRESENT_SURFACE through the framebuffer (ReimsGraphics owns that ring), and submit
* returns at once. The transaction completes when the host answers that PRESENT_SURFACE, which
* it does once the display shows the surface: its completion notification goes out and a waiter
* returns. An answer completes every transaction submitted before it, oldest first. At most two
* are outstanding; a third submit waits up to five seconds for room, then fails unsent. A
* transaction with no IOSurface on plane 0 keeps the surface shown and completes at once. VBL
* it does once the display shows the surface: a waiter returns, and its completion notification
* goes out where it carries a cookie (RgfxPipeTransactions.hpp). An answer completes every
* transaction submitted before it, oldest first. At most two are outstanding; a third submit
* waits up to five seconds for room, then fails unsent. A transaction with no IOSurface on
* plane 0 keeps the surface shown and completes with the one before it, or at once. VBL
* notifications follow the device's VBLANK events while one is registered.
*
* Our own code; it names only SDK-public IOKit classes.
@@ -36,6 +37,8 @@
#include <IOKit/IOLocks.h>
#include <IOKit/IOUserClient.h>
#include "RgfxPipeTransactions.hpp"
class RgfxAccelerator;
class ReimsGraphics;
@@ -60,13 +63,9 @@ public:
private:
// The registrations a client keeps (selector 3's kinds).
enum { kKindVbl = 0, kKindTransaction = 1, kKinds = 2 };
// The transactions submitted to the host and not yet answered, oldest first.
// The transactions submitted to the host and not yet answered.
enum { kMaxOutstanding = 2 };
struct Outstanding {
UInt64 seqno;
UInt32 id;
UInt64 cookie;
};
typedef RgfxPipeTransactions<kMaxOutstanding> Transactions;
static IOReturn sAssociate(OSObject *target, void *reference, IOExternalMethodArguments *a);
static IOReturn sModeScaler(OSObject *target, void *reference, IOExternalMethodArguments *a);
@@ -81,7 +80,7 @@ private:
static void vblankHook(OSObject *client, UInt64 count, UInt64 time);
static void presentHook(OSObject *client, UInt64 seqno, UInt32 status);
void presented(UInt64 seqno, UInt32 status);
void completeLocked(UInt32 transactionId, UInt64 cookie, UInt64 time);
void completeLocked(const Transactions::Completion &c, UInt64 time);
void sendVbl(UInt64 count, UInt64 time);
void releaseRegistrations();
@@ -101,8 +100,8 @@ private:
UInt32 completed;
UInt64 lastVblTime;
UInt32 logged;
Outstanding outstanding[kMaxOutstanding];
UInt32 outstandingCount;
UInt32 unnotifiedLogged;
Transactions transactions;
};
#endif // RGFX_DISPLAY_PIPE_USER_CLIENT_HPP_
+100
View File
@@ -0,0 +1,100 @@
// RgfxPipeTransactions: a display pipe client's transactions from their submit to their
// completion (RgfxDisplayPipeUserClient), and which completions are notified. Header-only and
// free of the kernel's headers, so its native test (tests/pipe_transactions_test.cpp) builds it
// as the kext does.
//
// A transaction that shows an IOSurface goes to the host as a PRESENT_SURFACE and stays
// outstanding until the host answers it. An answer completes that transaction and every one
// before it, oldest first. A transaction with nothing to show completes with the one before it,
// or at once where none is outstanding. At most `Max` are outstanding; the caller waits for room.
//
// Which completions are notified: a transaction carries a cookie, which its completion's
// notification gives back, or 0 for none. One with no cookie completes for its waiters
// (selector 9) and the display only. Measured on a macOS 26 guest: WindowServer drives the display
// in the transactional model ('wsrv' 0x80000013, then 0x11), each transaction with a cookie
// (4461 of them over three WindowServers). At its shutdown, when the session restarts, it hands
// the display back to the CPU ('wsrv' 0x2) and submits its last frames with none, waiting on each
// with selector 9 (4 per shutdown). Where the kext notified those, WindowServer aborted in
// IOAccelerator's transaction-notification callback at the first one's notification.
//
// Our own code.
#ifndef RGFX_PIPE_TRANSACTIONS_HPP_
#define RGFX_PIPE_TRANSACTIONS_HPP_
#include <stdint.h>
// Whether a transaction with cookie `cookie` is notified when it completes.
inline bool rgfxPipeNotifies(uint64_t cookie)
{
return cookie != 0;
}
template <uint32_t Max> class RgfxPipeTransactions {
public:
// A completed transaction: its id, the cookie its notification carries, and whether it is
// notified (rgfxPipeNotifies).
struct Completion {
uint32_t id;
uint64_t cookie;
bool notified;
};
bool hasRoom() const { return count_ < Max; }
uint32_t outstanding() const { return count_; }
// A transaction that shows a surface, sent to the host as `seqno`: outstanding until the
// host answers it. Needs room.
void sent(uint64_t seqno, uint32_t id, uint64_t cookie, bool notified)
{
if (count_ < Max)
items_[count_++] = {seqno, {id, cookie, notified}};
}
// A transaction with nothing to show. Where one is outstanding, it completes with the last
// of them: false. Where none is, it completes now: true, and `*now` is its completion.
// Needs room.
bool nothingToShow(uint32_t id, uint64_t cookie, bool notified, Completion *now)
{
Completion c = {id, cookie, notified};
if (count_ == 0) {
*now = c;
return true;
}
if (count_ < Max) {
items_[count_] = {items_[count_ - 1].seqno, c};
count_++;
}
return false;
}
// The host answered `seqno`: the transactions it completes, oldest first, into `out` (room
// for `Max`), and their count; 0 where `seqno` is none of this client's.
uint32_t answered(uint64_t seqno, Completion *out)
{
uint32_t match = Max;
for (uint32_t i = 0; i < count_; i++) {
if (items_[i].seqno == seqno)
match = i;
}
if (match == Max)
return 0;
uint32_t n = match + 1;
for (uint32_t i = 0; i < n; i++)
out[i] = items_[i].completion;
for (uint32_t i = n; i < count_; i++)
items_[i - n] = items_[i];
count_ -= n;
return n;
}
private:
struct Item {
uint64_t seqno;
Completion completion;
};
Item items_[Max] = {};
uint32_t count_ = 0;
};
#endif // RGFX_PIPE_TRANSACTIONS_HPP_
+114
View File
@@ -0,0 +1,114 @@
// A display pipe client's transactions (RgfxPipeTransactions.hpp), built natively: answers
// complete transactions oldest first, a transaction with nothing to show rides on the one before
// it, and only transactions with a cookie are notified, as at WindowServer's hand-over of the
// display to the CPU at its shutdown.
#include <cstdio>
#include "RgfxPipeTransactions.hpp"
static int g_checks = 0, g_failed = 0;
#define CHECK(e) \
do { \
g_checks++; \
if (!(e)) { \
g_failed++; \
std::fprintf(stderr, "%s:%d: CHECK(%s) failed\n", __FILE__, __LINE__, #e); \
} \
} while (0)
typedef RgfxPipeTransactions<2> Transactions;
static void rule()
{
CHECK(rgfxPipeNotifies(0x6000002de3a0)); // a cookie, as WindowServer's transactional frames carry
CHECK(!rgfxPipeNotifies(0)); // none, as its shutdown's frames carry
}
static void answers()
{
Transactions t;
Transactions::Completion out[2] = {};
// One sent, answered: it completes, notified, with its cookie.
CHECK(t.hasRoom() && t.outstanding() == 0);
t.sent(10, 1, 0xc001, true);
CHECK(t.outstanding() == 1 && t.hasRoom());
CHECK(t.answered(99, out) == 0); // another client's seqno: nothing
CHECK(t.answered(10, out) == 1 && out[0].id == 1 && out[0].cookie == 0xc001 && out[0].notified);
CHECK(t.outstanding() == 0 && t.answered(10, out) == 0); // answered once
// Two sent: no room. The second's answer completes both, oldest first.
t.sent(11, 2, 0xc002, true);
t.sent(12, 3, 0xc003, true);
CHECK(!t.hasRoom() && t.outstanding() == 2);
CHECK(t.answered(12, out) == 2 && out[0].id == 2 && out[1].id == 3);
CHECK(t.outstanding() == 0 && t.hasRoom());
// The first's answer completes it alone; the second stays.
t.sent(13, 4, 0xc004, true);
t.sent(14, 5, 0xc005, true);
CHECK(t.answered(13, out) == 1 && out[0].id == 4);
CHECK(t.outstanding() == 1 && t.answered(14, out) == 1 && out[0].id == 5);
}
static void nothingToShow()
{
Transactions t;
Transactions::Completion now = {}, out[2] = {};
// None outstanding: it completes now.
CHECK(t.nothingToShow(1, 0xa, true, &now) && now.id == 1 && now.cookie == 0xa && now.notified);
CHECK(t.outstanding() == 0);
// One outstanding: it completes with it, at that one's answer, oldest first.
t.sent(20, 2, 0xb, true);
CHECK(!t.nothingToShow(3, 0xc, true, &now) && t.outstanding() == 2);
CHECK(t.answered(20, out) == 2 && out[0].id == 2 && out[1].id == 3 && out[1].cookie == 0xc);
CHECK(t.outstanding() == 0);
}
// WindowServer's shutdown at a session restart, as measured on a macOS 26 guest: transaction 770
// with a cookie, then 'wsrv' 0x2 and transactions 771-774 with none (options 0x801, 0x801,
// 0x40801, 0x801), each waited on with selector 9. 770 is notified, also where its answer comes
// after the hand-over; 771-774 are not.
static void handOver()
{
Transactions t;
Transactions::Completion out[2] = {};
t.sent(100, 770, 0x6000002e9fe0, rgfxPipeNotifies(0x6000002e9fe0));
CHECK(t.answered(100, out) == 1 && out[0].id == 770 && out[0].notified);
for (uint32_t id = 771; id <= 774; id++) {
t.sent(100 + id, id, 0, rgfxPipeNotifies(0));
CHECK(t.answered(100 + id, out) == 1 && out[0].id == id && !out[0].notified);
}
// One with a cookie and one with none outstanding together: one answer completes both, only
// the first notified.
t.sent(200, 21, 0x21, rgfxPipeNotifies(0x21));
t.sent(201, 22, 0, rgfxPipeNotifies(0));
CHECK(t.answered(201, out) == 2);
CHECK(out[0].id == 21 && out[0].notified && out[1].id == 22 && !out[1].notified);
// A frame with nothing to show and no cookie, none outstanding: done now, unnotified.
Transactions::Completion now = {};
CHECK(t.nothingToShow(23, 0, rgfxPipeNotifies(0), &now) && now.id == 23 && !now.notified);
// The next WindowServer's first frame carries a cookie again: notified.
t.sent(300, 1, 0x6000002de3a0, rgfxPipeNotifies(0x6000002de3a0));
CHECK(t.answered(300, out) == 1 && out[0].notified);
}
int main()
{
rule();
answers();
nothingToShow();
handOver();
if (g_failed) {
std::fprintf(stderr, "pipe_transactions_test: %d of %d checks failed\n", g_failed, g_checks);
return 1;
}
std::printf("pipe_transactions_test: OK (%d checks)\n", g_checks);
return 0;
}