1*4882a593Smuzhiyun================================= 2*4882a593Smuzhiyun(How to avoid) Botching up ioctls 3*4882a593Smuzhiyun================================= 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunFrom: https://blog.ffwll.ch/2013/11/botching-up-ioctls.html 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunBy: Daniel Vetter, Copyright © 2013 Intel Corporation 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunOne clear insight kernel graphics hackers gained in the past few years is that 10*4882a593Smuzhiyuntrying to come up with a unified interface to manage the execution units and 11*4882a593Smuzhiyunmemory on completely different GPUs is a futile effort. So nowadays every 12*4882a593Smuzhiyundriver has its own set of ioctls to allocate memory and submit work to the GPU. 13*4882a593SmuzhiyunWhich is nice, since there's no more insanity in the form of fake-generic, but 14*4882a593Smuzhiyunactually only used once interfaces. But the clear downside is that there's much 15*4882a593Smuzhiyunmore potential to screw things up. 16*4882a593Smuzhiyun 17*4882a593SmuzhiyunTo avoid repeating all the same mistakes again I've written up some of the 18*4882a593Smuzhiyunlessons learned while botching the job for the drm/i915 driver. Most of these 19*4882a593Smuzhiyunonly cover technicalities and not the big-picture issues like what the command 20*4882a593Smuzhiyunsubmission ioctl exactly should look like. Learning these lessons is probably 21*4882a593Smuzhiyunsomething every GPU driver has to do on its own. 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun 24*4882a593SmuzhiyunPrerequisites 25*4882a593Smuzhiyun------------- 26*4882a593Smuzhiyun 27*4882a593SmuzhiyunFirst the prerequisites. Without these you have already failed, because you 28*4882a593Smuzhiyunwill need to add a 32-bit compat layer: 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun * Only use fixed sized integers. To avoid conflicts with typedefs in userspace 31*4882a593Smuzhiyun the kernel has special types like __u32, __s64. Use them. 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun * Align everything to the natural size and use explicit padding. 32-bit 34*4882a593Smuzhiyun platforms don't necessarily align 64-bit values to 64-bit boundaries, but 35*4882a593Smuzhiyun 64-bit platforms do. So we always need padding to the natural size to get 36*4882a593Smuzhiyun this right. 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun * Pad the entire struct to a multiple of 64-bits if the structure contains 39*4882a593Smuzhiyun 64-bit types - the structure size will otherwise differ on 32-bit versus 40*4882a593Smuzhiyun 64-bit. Having a different structure size hurts when passing arrays of 41*4882a593Smuzhiyun structures to the kernel, or if the kernel checks the structure size, which 42*4882a593Smuzhiyun e.g. the drm core does. 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun * Pointers are __u64, cast from/to a uintprt_t on the userspace side and 45*4882a593Smuzhiyun from/to a void __user * in the kernel. Try really hard not to delay this 46*4882a593Smuzhiyun conversion or worse, fiddle the raw __u64 through your code since that 47*4882a593Smuzhiyun diminishes the checking tools like sparse can provide. The macro 48*4882a593Smuzhiyun u64_to_user_ptr can be used in the kernel to avoid warnings about integers 49*4882a593Smuzhiyun and pointers of different sizes. 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun 52*4882a593SmuzhiyunBasics 53*4882a593Smuzhiyun------ 54*4882a593Smuzhiyun 55*4882a593SmuzhiyunWith the joys of writing a compat layer avoided we can take a look at the basic 56*4882a593Smuzhiyunfumbles. Neglecting these will make backward and forward compatibility a real 57*4882a593Smuzhiyunpain. And since getting things wrong on the first attempt is guaranteed you 58*4882a593Smuzhiyunwill have a second iteration or at least an extension for any given interface. 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun * Have a clear way for userspace to figure out whether your new ioctl or ioctl 61*4882a593Smuzhiyun extension is supported on a given kernel. If you can't rely on old kernels 62*4882a593Smuzhiyun rejecting the new flags/modes or ioctls (since doing that was botched in the 63*4882a593Smuzhiyun past) then you need a driver feature flag or revision number somewhere. 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun * Have a plan for extending ioctls with new flags or new fields at the end of 66*4882a593Smuzhiyun the structure. The drm core checks the passed-in size for each ioctl call 67*4882a593Smuzhiyun and zero-extends any mismatches between kernel and userspace. That helps, 68*4882a593Smuzhiyun but isn't a complete solution since newer userspace on older kernels won't 69*4882a593Smuzhiyun notice that the newly added fields at the end get ignored. So this still 70*4882a593Smuzhiyun needs a new driver feature flags. 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun * Check all unused fields and flags and all the padding for whether it's 0, 73*4882a593Smuzhiyun and reject the ioctl if that's not the case. Otherwise your nice plan for 74*4882a593Smuzhiyun future extensions is going right down the gutters since someone will submit 75*4882a593Smuzhiyun an ioctl struct with random stack garbage in the yet unused parts. Which 76*4882a593Smuzhiyun then bakes in the ABI that those fields can never be used for anything else 77*4882a593Smuzhiyun but garbage. This is also the reason why you must explicitly pad all 78*4882a593Smuzhiyun structures, even if you never use them in an array - the padding the compiler 79*4882a593Smuzhiyun might insert could contain garbage. 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun * Have simple testcases for all of the above. 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunFun with Error Paths 85*4882a593Smuzhiyun-------------------- 86*4882a593Smuzhiyun 87*4882a593SmuzhiyunNowadays we don't have any excuse left any more for drm drivers being neat 88*4882a593Smuzhiyunlittle root exploits. This means we both need full input validation and solid 89*4882a593Smuzhiyunerror handling paths - GPUs will die eventually in the oddmost corner cases 90*4882a593Smuzhiyunanyway: 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun * The ioctl must check for array overflows. Also it needs to check for 93*4882a593Smuzhiyun over/underflows and clamping issues of integer values in general. The usual 94*4882a593Smuzhiyun example is sprite positioning values fed directly into the hardware with the 95*4882a593Smuzhiyun hardware just having 12 bits or so. Works nicely until some odd display 96*4882a593Smuzhiyun server doesn't bother with clamping itself and the cursor wraps around the 97*4882a593Smuzhiyun screen. 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun * Have simple testcases for every input validation failure case in your ioctl. 100*4882a593Smuzhiyun Check that the error code matches your expectations. And finally make sure 101*4882a593Smuzhiyun that you only test for one single error path in each subtest by submitting 102*4882a593Smuzhiyun otherwise perfectly valid data. Without this an earlier check might reject 103*4882a593Smuzhiyun the ioctl already and shadow the codepath you actually want to test, hiding 104*4882a593Smuzhiyun bugs and regressions. 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun * Make all your ioctls restartable. First X really loves signals and second 107*4882a593Smuzhiyun this will allow you to test 90% of all error handling paths by just 108*4882a593Smuzhiyun interrupting your main test suite constantly with signals. Thanks to X's 109*4882a593Smuzhiyun love for signal you'll get an excellent base coverage of all your error 110*4882a593Smuzhiyun paths pretty much for free for graphics drivers. Also, be consistent with 111*4882a593Smuzhiyun how you handle ioctl restarting - e.g. drm has a tiny drmIoctl helper in its 112*4882a593Smuzhiyun userspace library. The i915 driver botched this with the set_tiling ioctl, 113*4882a593Smuzhiyun now we're stuck forever with some arcane semantics in both the kernel and 114*4882a593Smuzhiyun userspace. 115*4882a593Smuzhiyun 116*4882a593Smuzhiyun * If you can't make a given codepath restartable make a stuck task at least 117*4882a593Smuzhiyun killable. GPUs just die and your users won't like you more if you hang their 118*4882a593Smuzhiyun entire box (by means of an unkillable X process). If the state recovery is 119*4882a593Smuzhiyun still too tricky have a timeout or hangcheck safety net as a last-ditch 120*4882a593Smuzhiyun effort in case the hardware has gone bananas. 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun * Have testcases for the really tricky corner cases in your error recovery code 123*4882a593Smuzhiyun - it's way too easy to create a deadlock between your hangcheck code and 124*4882a593Smuzhiyun waiters. 125*4882a593Smuzhiyun 126*4882a593Smuzhiyun 127*4882a593SmuzhiyunTime, Waiting and Missing it 128*4882a593Smuzhiyun---------------------------- 129*4882a593Smuzhiyun 130*4882a593SmuzhiyunGPUs do most everything asynchronously, so we have a need to time operations and 131*4882a593Smuzhiyunwait for outstanding ones. This is really tricky business; at the moment none of 132*4882a593Smuzhiyunthe ioctls supported by the drm/i915 get this fully right, which means there's 133*4882a593Smuzhiyunstill tons more lessons to learn here. 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun * Use CLOCK_MONOTONIC as your reference time, always. It's what alsa, drm and 136*4882a593Smuzhiyun v4l use by default nowadays. But let userspace know which timestamps are 137*4882a593Smuzhiyun derived from different clock domains like your main system clock (provided 138*4882a593Smuzhiyun by the kernel) or some independent hardware counter somewhere else. Clocks 139*4882a593Smuzhiyun will mismatch if you look close enough, but if performance measuring tools 140*4882a593Smuzhiyun have this information they can at least compensate. If your userspace can 141*4882a593Smuzhiyun get at the raw values of some clocks (e.g. through in-command-stream 142*4882a593Smuzhiyun performance counter sampling instructions) consider exposing those also. 143*4882a593Smuzhiyun 144*4882a593Smuzhiyun * Use __s64 seconds plus __u64 nanoseconds to specify time. It's not the most 145*4882a593Smuzhiyun convenient time specification, but it's mostly the standard. 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun * Check that input time values are normalized and reject them if not. Note 148*4882a593Smuzhiyun that the kernel native struct ktime has a signed integer for both seconds 149*4882a593Smuzhiyun and nanoseconds, so beware here. 150*4882a593Smuzhiyun 151*4882a593Smuzhiyun * For timeouts, use absolute times. If you're a good fellow and made your 152*4882a593Smuzhiyun ioctl restartable relative timeouts tend to be too coarse and can 153*4882a593Smuzhiyun indefinitely extend your wait time due to rounding on each restart. 154*4882a593Smuzhiyun Especially if your reference clock is something really slow like the display 155*4882a593Smuzhiyun frame counter. With a spec lawyer hat on this isn't a bug since timeouts can 156*4882a593Smuzhiyun always be extended - but users will surely hate you if their neat animations 157*4882a593Smuzhiyun starts to stutter due to this. 158*4882a593Smuzhiyun 159*4882a593Smuzhiyun * Consider ditching any synchronous wait ioctls with timeouts and just deliver 160*4882a593Smuzhiyun an asynchronous event on a pollable file descriptor. It fits much better 161*4882a593Smuzhiyun into event driven applications' main loop. 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun * Have testcases for corner-cases, especially whether the return values for 164*4882a593Smuzhiyun already-completed events, successful waits and timed-out waits are all sane 165*4882a593Smuzhiyun and suiting to your needs. 166*4882a593Smuzhiyun 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunLeaking Resources, Not 169*4882a593Smuzhiyun---------------------- 170*4882a593Smuzhiyun 171*4882a593SmuzhiyunA full-blown drm driver essentially implements a little OS, but specialized to 172*4882a593Smuzhiyunthe given GPU platforms. This means a driver needs to expose tons of handles 173*4882a593Smuzhiyunfor different objects and other resources to userspace. Doing that right 174*4882a593Smuzhiyunentails its own little set of pitfalls: 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun * Always attach the lifetime of your dynamically created resources to the 177*4882a593Smuzhiyun lifetime of a file descriptor. Consider using a 1:1 mapping if your resource 178*4882a593Smuzhiyun needs to be shared across processes - fd-passing over unix domain sockets 179*4882a593Smuzhiyun also simplifies lifetime management for userspace. 180*4882a593Smuzhiyun 181*4882a593Smuzhiyun * Always have O_CLOEXEC support. 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun * Ensure that you have sufficient insulation between different clients. By 184*4882a593Smuzhiyun default pick a private per-fd namespace which forces any sharing to be done 185*4882a593Smuzhiyun explicitly. Only go with a more global per-device namespace if the objects 186*4882a593Smuzhiyun are truly device-unique. One counterexample in the drm modeset interfaces is 187*4882a593Smuzhiyun that the per-device modeset objects like connectors share a namespace with 188*4882a593Smuzhiyun framebuffer objects, which mostly are not shared at all. A separate 189*4882a593Smuzhiyun namespace, private by default, for framebuffers would have been more 190*4882a593Smuzhiyun suitable. 191*4882a593Smuzhiyun 192*4882a593Smuzhiyun * Think about uniqueness requirements for userspace handles. E.g. for most drm 193*4882a593Smuzhiyun drivers it's a userspace bug to submit the same object twice in the same 194*4882a593Smuzhiyun command submission ioctl. But then if objects are shareable userspace needs 195*4882a593Smuzhiyun to know whether it has seen an imported object from a different process 196*4882a593Smuzhiyun already or not. I haven't tried this myself yet due to lack of a new class 197*4882a593Smuzhiyun of objects, but consider using inode numbers on your shared file descriptors 198*4882a593Smuzhiyun as unique identifiers - it's how real files are told apart, too. 199*4882a593Smuzhiyun Unfortunately this requires a full-blown virtual filesystem in the kernel. 200*4882a593Smuzhiyun 201*4882a593Smuzhiyun 202*4882a593SmuzhiyunLast, but not Least 203*4882a593Smuzhiyun------------------- 204*4882a593Smuzhiyun 205*4882a593SmuzhiyunNot every problem needs a new ioctl: 206*4882a593Smuzhiyun 207*4882a593Smuzhiyun * Think hard whether you really want a driver-private interface. Of course 208*4882a593Smuzhiyun it's much quicker to push a driver-private interface than engaging in 209*4882a593Smuzhiyun lengthy discussions for a more generic solution. And occasionally doing a 210*4882a593Smuzhiyun private interface to spearhead a new concept is what's required. But in the 211*4882a593Smuzhiyun end, once the generic interface comes around you'll end up maintainer two 212*4882a593Smuzhiyun interfaces. Indefinitely. 213*4882a593Smuzhiyun 214*4882a593Smuzhiyun * Consider other interfaces than ioctls. A sysfs attribute is much better for 215*4882a593Smuzhiyun per-device settings, or for child objects with fairly static lifetimes (like 216*4882a593Smuzhiyun output connectors in drm with all the detection override attributes). Or 217*4882a593Smuzhiyun maybe only your testsuite needs this interface, and then debugfs with its 218*4882a593Smuzhiyun disclaimer of not having a stable ABI would be better. 219*4882a593Smuzhiyun 220*4882a593SmuzhiyunFinally, the name of the game is to get it right on the first attempt, since if 221*4882a593Smuzhiyunyour driver proves popular and your hardware platforms long-lived then you'll 222*4882a593Smuzhiyunbe stuck with a given ioctl essentially forever. You can try to deprecate 223*4882a593Smuzhiyunhorrible ioctls on newer iterations of your hardware, but generally it takes 224*4882a593Smuzhiyunyears to accomplish this. And then again years until the last user able to 225*4882a593Smuzhiyuncomplain about regressions disappears, too. 226