Keithp.com

Serving from Portland for a while.

FD passing for DRI.Next

Using the DMA-BUF interfaces to pass DRI objects between the client
and server, as discussed in my previous blog posting on DRI-Next,
requires that we successfully pass file descriptors over the X
protocol socket.

Rumor has it that this has been tried and found to be difficult, and
so I decided to do a bit of experimentation to see how this could be
made to work within the existing X implementation.

(All of the examples shown here are licensed under the GPL, version 2
and are available from git://keithp.com/git/fdpassing)

Basics of FD passing

The kernel internals that support FD passing are actually quite simple
-- POSIX already require that two processes be able to share the same
underlying reference to a file because of the semantics of the fork(2)
call. Adding some ability to share arbitrary file descriptors between
two processes then is far more about how you ask the kernel than the
actual file descriptor sharing operation.

In Linux, file descriptors can be passed through local network
sockets. The sender constructs a mystic-looking sendmsg(2) call,
placing the file descriptor in the control field of that
operation. The kernel pulls the file descriptor out of the control
field, allocates a file descriptor in the target process which
references the same file object and then sticks the file descriptor in
a queue for the receiving process to fetch.

The receiver then constructs a matching call to recvmsg that provides
a place for the kernel to stick the new file descriptor.

A helper API for testing

I first write a stand-alone program that created a socketpair, forked
and then passed an fd from the parent to the child. Once that was
working, I decided that some short helper functions would make further
testing a whole lot easier.

Here's a function that writes some data and an optional file
descriptor:

When run, this demonstrates that the reader gets two bytes in the
first read along with a file descriptor followed by one byte in a
second read, without a file descriptor. This demonstrates that
a file descriptor message forms a barrier within the socket; multiple
messages will be merged together, but not past a message containing a
file descriptor.

Reading without accepting a file descriptor

What happens when the reader isn't expecting a file descriptor? Does
it just get lost? Does the reader not get the message until it asks
for the file descriptor? What about the boundary issue described above?

This shows that the first passed file descriptor is picked up by the
first sock_fd_read call, but the file descriptor is closed. The second
file descriptor passed is picked up by the second sock_fd_read call.

A summary of results

failing to accept an fd in the receiver results in the fd being
closed by the kernel.

a file descriptor must be accompanied by some data.

Make X pass file descriptors

I'd like to get X to pass a file descriptor without completely
rewriting the internals of both the library and the X server. Ideally,
without making any changes to the existing code paths for regular
request processing at all.

On the sending side, this seems pretty straightforward -- we just need
to get the X connection file descriptor and call sendmsg directly,
passing the desired file descriptor along. In XCB, this could be done
by using the xcb_take_socket interface to temporarily hijack the
protocol as Xlib does.

It's the receiving side where things are messier. Because a bare read
will discard any delivered file descriptor, we must make sure to use
recvmsg whenever we want to actually capture the file descriptor.

Kludge X server fd receiving

Because a passed fd creates a barrier in the bytestream, when the X
server reads requests from a client, the read will stop sending data
after the message with the file descriptor is consumed.

Of course, this process consumes the passed file descriptor, and if
that call isn't made with recvmsg set up to receive it, the fd will be
lost.

As a simple kludge, if we pass a meaningless fd with the X request and
then the 'real' fd with a following XNoOperation request, the existing
request reading code will get the request, discard the meaningless fd
and then stop reading at that point due to the barrier. Once into the
request processing code, recvmsg can be called to get the real file
descriptor and the associated XNoOperation request.

Fixing XCB to receive file descriptors

Multiple threads may be trying to get replies and events back from the
X server at the same time, which means the kludge of having the real
fd follow the message will likely lead to the wrong thread getting the
file descriptor.

Instead, I suspect the best plan will be to fix XCB to internally
capture passed file descriptors and save them with the associated
reply. Because the file descriptor message will form a barrier in the
read stream, xcb can associate any received file descriptor with the
last reply in the read data. The X server would then send the reply
with an explicit sendmsg call to pass both reply and file descriptor
together.

Next steps

The next thing to do is code up a simple fd passing extension and try
to get it working, passing descriptors back and forth to the X
server. Once that works, design of the rest of the DRM-Next extension
should be pretty straightforward.