Bug 220974

Summary: WebGL IPC should use shared memory for synchronous messages
Product: WebKit Reporter: Kimmo Kinnunen <kkinnunen>
Component: WebGLAssignee: Kimmo Kinnunen <kkinnunen>
Status: RESOLVED FIXED    
Severity: Normal CC: dino, ggaren, kbr, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Local Build   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on: 219641, 222305, 222546    
Bug Blocks: 217211, 222726, 222722    
Attachments:
Description Flags
Patch
none
rebase
none
rebase
none
rebase none

Description Kimmo Kinnunen 2021-01-26 05:54:13 PST
WebGL IPC should send synchronised messages through the shared memory.

Also the replies should by default be written to the shared memory.
Comment 1 Radar WebKit Bug Importer 2021-02-02 05:55:13 PST
<rdar://problem/73876947>
Comment 2 Kimmo Kinnunen 2021-03-03 03:31:47 PST
Created attachment 422055 [details]
Patch
Comment 3 Geoffrey Garen 2021-03-10 14:14:01 PST
Comment on attachment 422055 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=422055&action=review

Nice speedup!

I think I may have spotted a small bug.

Can you re-check EWS (and the small bug) before landing?

> Source/WebKit/Platform/IPC/Connection.h:326
> +    bool pushPendingSyncRequestID(uint64_t syncRequestID = 0);

Can we remove the default 0 argument here? Feels like it might invite mistakes. Seems unused so far.

> Source/WebKit/Platform/IPC/StreamClientConnection.h:284
> +#if PLATFORM(COCOA)
> +        ClientLimit clientLimit = sharedClientLimit().exchange(ClientLimit::clientIsWaitingTag, std::memory_order_acq_rel);
> +        ClientOffset clientOffset = sharedClientOffset().load(std::memory_order_acquire);
> +#else
> +        ClientLimit clientLimit = sharedClientLimit().load(std::memory_order_acquire);
> +        ClientOffset clientOffset = sharedClientOffset().load(std::memory_order_acquire);
> +#endif
> +        if (!clientLimit && (clientOffset == ClientOffset::serverIsSleepingTag || !clientOffset))
> +            break;

In the iteration where we break out of the loop, we temporarily set clientIsWaitingTag, but never wait on our semaphore. What happens if the server notices clientIsWaitingTag, and signals our semaphore, before we have a chance to clear clientIsWaitingTag? Does that put our semaphore out of balance?

Perhaps it would be better to just do two loads (removing the cocoa-specific path), and only store clientIsWaitingTag right before we wait.
Comment 4 Kimmo Kinnunen 2021-03-15 05:49:51 PDT
Created attachment 423163 [details]
rebase
Comment 5 Kimmo Kinnunen 2021-03-15 05:51:34 PDT
Created attachment 423164 [details]
rebase
Comment 6 Kimmo Kinnunen 2021-03-15 06:11:56 PDT
Thanks for the review!
(In reply to Geoffrey Garen from comment #3)
> In the iteration where we break out of the loop, we temporarily set
> clientIsWaitingTag, but never wait on our semaphore. What happens if the
> server notices clientIsWaitingTag, and signals our semaphore, before we have
> a chance to clear clientIsWaitingTag? Does that put our semaphore out of
> balance?

All the sites expect spurious wakeups from the semaphore waits.
 
> Perhaps it would be better to just do two loads (removing the cocoa-specific
> path), and only store clientIsWaitingTag right before we wait.

That's what the code tries to avoid with the exchange. It is the race the wait structure always has.

E.g. it would be as follows:

for(;;) {
    clientOffset = sharedClientOffset.load();
    clientLimit = sharedClientLimit.load();
    if (!clientOffset && !clientLimit)
        break;

    // Race here: what if the server sets clientOffset = clientLimit = 0 here and goes on to wait
    // for next command from the client?

    sharedClientLimit.store(clientIsWaitingTag);
    wait();  // If the above happens, this waits forever.
}
Comment 7 Kimmo Kinnunen 2021-03-15 06:14:51 PDT
Created attachment 423167 [details]
rebase
Comment 8 EWS 2021-03-15 13:02:08 PDT
Committed r274433: <https://commits.webkit.org/r274433>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 423167 [details].
Comment 9 Geoffrey Garen 2021-03-16 13:59:35 PDT
> All the sites expect spurious wakeups from the semaphore waits.

Got it. I hadn't noticed this before. Thanks!