2008-08-02 22:25:35

by Brian Beattie

[permalink] [raw]
Subject: ioctl's suck?

The other day Linus (I think) made the statement, that I don't disagree
with, that I will parapharse as "ioctl's suck". If I recall correctly
and understand he was saying that a device that uses ioctls is broken.
(this is my paraphrase and if it offends anybody it is my fault not the
original author's).

This got me to thinking about a device driver that I'm working on.
Currently I have some ioctls to handle status and out of band messages
and I'm wondering about eliminating the ioctls. I'm wondering if
anybody has any ideas or opinions that they would like to share, about
just what i wrong with ioctls and/or how to avoid them.

I can see a number of problems with ioctls that I can'tr quite put into
words.

I could add a control device and pass ascii strings for status and OOB
messages, would that be an improvement?
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting a bad thing?

Brian Beattie LFS12947 | "Honor isn't about making the right choices.
[email protected] | It's about dealing with the consequences."
http://www.beattie-home.net | -- Midori Koto


2008-08-03 01:18:49

by Mikael Pettersson

[permalink] [raw]
Subject: Re: ioctl's suck?

Brian Beattie writes:
> The other day Linus (I think) made the statement, that I don't disagree
> with, that I will parapharse as "ioctl's suck". If I recall correctly
> and understand he was saying that a device that uses ioctls is broken.
...
> I could add a control device and pass ascii strings for status and OOB
> messages, would that be an improvement?

The problem is that the classical unix I/O abstraction really only handles
two cases well, viz. "read next chunk of data" and "write this data and
advance position". Anything else is seen as something strange.

ioctls() are the equivalent of device-specific RPCs (remote procedure
calls): you present some arguments and a call code, and you get some
results. Nothing wrong with that.

In practice however there are some pitfalls. For instance, ioctls()
are often abused for quick hacks with little concern for backwards
compatibility. So when someone changes an ioctl, and breaks user-space,
who gets the blame? ioctls in general! I would argue that that is
why, and the main reason why, some people put ioctls() down.

There may be something wrong with people abusing ioctls, but that
is not a problem with ioctls per se. (I can easily design broken
ABIs around read()/write()/open()-only interfaces.)

2008-08-03 01:37:12

by Robert Hancock

[permalink] [raw]
Subject: Re: ioctl's suck?

Brian Beattie wrote:
> The other day Linus (I think) made the statement, that I don't disagree
> with, that I will parapharse as "ioctl's suck". If I recall correctly
> and understand he was saying that a device that uses ioctls is broken.
> (this is my paraphrase and if it offends anybody it is my fault not the
> original author's).
>
> This got me to thinking about a device driver that I'm working on.
> Currently I have some ioctls to handle status and out of band messages
> and I'm wondering about eliminating the ioctls. I'm wondering if
> anybody has any ideas or opinions that they would like to share, about
> just what i wrong with ioctls and/or how to avoid them.

As I see it the main problems are:

-Unless the ioctl parameter structures are laid out carefully, you end
up with problems like different structure layouts between 32/64-bit
processes, etc.

-They can't really be used by anything other than a C or C++ program.
Anything else (shell script, Python, Java, etc.) is pretty much out of
luck unless it can use a C shim layer of some sort.

>
> I can see a number of problems with ioctls that I can'tr quite put into
> words.
>
> I could add a control device and pass ascii strings for status and OOB
> messages, would that be an improvement?

Quite likely. For something like a status that's being read out of the
device, a sysfs file would seem a more logical choice. If you're sitting
there waiting for messages to show up, though, a separate device node
might be better.

2008-08-03 13:13:41

by Alan

[permalink] [raw]
Subject: Re: ioctl's suck?

> I could add a control device and pass ascii strings for status and OOB
> messages, would that be an improvement?

Usually not. The idea that ioctl can be replaced with ascii messages is
clueless rubbish that generally gets spouted by people with their head
in the clouds of conceptual elegance and no grasp of reality.

There are certain things you can expose that way usefully via sysfs
- things like general stateless status information. Ioctl however provides
an interface tied to file handle not name (which is essential in a hotplug
environment) and an ordering to events so you know the response you get
matches the query you made.

There are good things to do with ioctls:
- Make sure the size of all the fields are consistent across 32/64bit
(for 32bit binaries on 64bit)
- Use the IOR/IOW/IORW macros

Alan

2008-08-03 17:04:59

by Brian Beattie

[permalink] [raw]
Subject: Re: ioctl's suck?

On Sun, 2008-08-03 at 13:56 +0100, Alan Cox wrote:
> > I could add a control device and pass ascii strings for status and OOB
> > messages, would that be an improvement?
>
> Usually not. The idea that ioctl can be replaced with ascii messages is
> clueless rubbish that generally gets spouted by people with their head
> in the clouds of conceptual elegance and no grasp of reality.

Yeah, I can see that. Though the case has been made that ioctls are
only usable by C/C++ while I'm sure there are other languages, I'm
working in the embedded space and mostly work with C and sh so my
experience is limited and that is of less direct importance to me.

>
> There are certain things you can expose that way usefully via sysfs
> - things like general stateless status information. Ioctl however provides
> an interface tied to file handle not name (which is essential in a hotplug
> environment) and an ordering to events so you know the response you get
> matches the query you made.

Yeah coherence (if I'm using that word correctly) would be critical, and
a separate file handle would make that tricky.


> Alan
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting a bad thing?

Brian Beattie LFS12947 | "Honor isn't about making the right choices.
[email protected] | It's about dealing with the consequences."
http://www.beattie-home.net | -- Midori Koto