Skip to contentSkip to conversation

Remove or convert empty ioctls ?

RFC Thomas Gleixner 4 participants 11 messages Oct 15, 2009

lore
Share & tools
lore
#0 lore raw

Hi,

while looking into pushing down BKL to ioctls I noticed that we have a lot of ioctls which simply return -EINVAL or some other fancy error code.

The question is whether we should convert them to unlocked_ioctl or simply remove them and let the sys_ioctl code return the default -ENOTTY error code.

One could argue that this is a user visible change, but OTOH there is no particular value of EINVAL or any other weird error code when it just says: there is no ioctl for this fd.

Thanks,

tglx

#1 lore raw
Quoted 6 lines · Thomas Gleixner

Hi,

while looking into pushing down BKL to ioctls I noticed that we have a lot of ioctls which simply return -EINVAL or some other fancy error code.

Quoted 7 lines

The question is whether we should convert them to unlocked_ioctl or simply remove them and let the sys_ioctl code return the default -ENOTTY error code.

One could argue that this is a user visible change, but OTOH there is no particular value of EINVAL or any other weird error code when it just says: there is no ioctl for this fd.

It seems like a mistake to generalize a rule out of this, especially if it leads to error return values changing unexpectedly in years-old code.

"no particular value" is highly subjective, and I think unprovable, without an exhaustive survey of userland programs interacting with kernel drivers. Userland programs often interact with a -class- of drivers, expecting predictable behavior from a DoThisThing ioctl, with EINVAL or "other weird error code" returned intentionally.

Changing the return codes seems quite unwise.

Jeff

#2 lore raw
Quoted 7 lines

"no particular value" is highly subjective, and I think unprovable, without an exhaustive survey of userland programs interacting with kernel drivers. Userland programs often interact with a -class- of drivers, expecting predictable behavior from a DoThisThing ioctl, with EINVAL or "other weird error code" returned intentionally.

Changing the return codes seems quite unwise.

We've changed lots of them to -ENOTTY over the past few years, nobody has even noticed (you included ;))

SuS says an unknown ioctl code returns -ENOTTY.

Alan

#3 lore raw
Quoted 6 lines · Alan Cox
Quoted 6 lines

"no particular value" is highly subjective, and I think unprovable, without an exhaustive survey of userland programs interacting with kernel drivers. Userland programs often interact with a -class- of drivers, expecting predictable behavior from a DoThisThing ioctl, with EINVAL or "other weird error code" returned intentionally.

Quoted 6 lines
Quoted 1 line

Changing the return codes seems quite unwise.

We've changed lots of them to -ENOTTY over the past few years, nobody has even noticed (you included ;))

SuS says an unknown ioctl code returns -ENOTTY.

These are not unknown ioctls; they are ioctls that the driver author close to implement rather than the default (ENOTTY).

Jeff

#4 lore raw
Quoted 9 lines · On Thu, 15 Oct 2009 11:22:40 -0400 Jeff Garzik <[email protected]> wrote:
Quoted 8 lines · Alan Cox
Quoted 7 lines

"no particular value" is highly subjective, and I think unprovable, without an exhaustive survey of userland programs interacting with kernel drivers. Userland programs often interact with a -class- of drivers, expecting predictable behavior from a DoThisThing ioctl, with EINVAL or "other weird error code" returned intentionally.

Changing the return codes seems quite unwise.

Quoted 7 lines
Quoted 4 lines

We've changed lots of them to -ENOTTY over the past few years, nobody has even noticed (you included ;))

SuS says an unknown ioctl code returns -ENOTTY.

These are not unknown ioctls; they are ioctls that the driver author close to implement rather than the default (ENOTTY).

That makes them ENOTTY please go and read the standards document.

EINVAL means you used an ioctl that is correct for the driver but that for some reason the driver didn't like it.

#5 lore raw
Quoted 2 lines · Alan Cox

EINVAL means you used an ioctl that is correct for the driver but that for some reason the driver didn't like it.

Precisely.

The driver author proactively chose to implement the ioctl and return a value other than ENOTTY.

It is invalid to assume that all such cases are a direct result of author mistakes or ignorance.

Jeff

#6 lore raw
Quoted 8 lines · On Thu, 15 Oct 2009 11:35:45 -0400 Jeff Garzik <[email protected]> wrote:
Quoted 2 lines · Alan Cox

EINVAL means you used an ioctl that is correct for the driver but that for some reason the driver didn't like it.

Precisely.

The driver author proactively chose to implement the ioctl and return a value other than ENOTTY.

Jeff - wake up - take coffee if neeeded.

The case in question is the case where ->unlocked_ioctl() == NULL. In that case the driver author didn't implement anything and the correct return from the layer doing the handling is -ENOTTY.

In the case where there is an ioctl handler the common

foo_ioctl()

{
	return -EINVAL;
}

stub case is simply broken, and caused by the fact a lot of early Linux drivers were wrong and people keep propogating mistakes (a serious problem in the open source world is that most code is produced by copying stuff but often by copying buggy code).

Essentially the only times you should be returning -EINVAL is where the driver actually has

	case IOCWIBBLE:
		/* Do stuff */
		if (foo < 1)
			return -EINVAL;

All the "I don't know this" cases should be -ENOTTY.

There are cases where

	case IOCWIBBLE:
		return -EINVAL;

is correct but they are unusual - basically the case where the driver author wants to "support" the ioctl but there is no argument that it can be given which is correct.

Anyway the case discussed which is ".unlocked_ioctl = NULL" should return -ENOTTY, and there isn't any argument about the driver authors intentions.

#7 lore raw
Quoted 8 lines · Alan Cox

There are cases where

	case IOCWIBBLE:
		return -EINVAL;

is correct but they are unusual - basically the case where the driver author wants to "support" the ioctl but there is no argument that it can be given which is correct.

This is the type of case I was referring to.

Jeff

#8 lore raw
Quoted 11 lines · Jeff Garzik
Quoted 8 lines · Alan Cox

There are cases where

	case IOCWIBBLE:
		return -EINVAL;

is correct but they are unusual - basically the case where the driver author wants to "support" the ioctl but there is no argument that it can be given which is correct.

This is the type of case I was referring to.

There is no real reason to believe that

int ioctl()

{
	return -EINVAL;
}

fall into above category.

Thanks,

tglx

#9 lore raw
In reply to Alan Cox
Quoted 2 lines · Alan Cox

Anyway the case discussed which is ".unlocked_ioctl = NULL" should return -ENOTTY, and there isn't any argument about the driver authors intentions.

I think we got some confusion finally. :)

If both unlocked_ioctl and ioctl are NULL the return code is -ENOTTY.

We have locked ioctl functions which return -ENOIOCTLCMD. vfs_ioctl() returns that to user space, but for unlocked_ioctl it is translated to -EINVAL. I guess we need to fix that either in the ioctl implementations or let vfs_ioctl() translate it for locked ioctls as well.

The other category of ioctls (both locked and unlocked) are the stub functions which simply return -EINVAL or -ENOIOCTLCMD.

The spec says:
EINVAL: The request or arg argument is not valid for this device.

ENOTTY: The fildes argument is not associated with a STREAMS
device that accepts control functions.

So for the stub ioctl functions EINVAL is a correct return value because the driver has an ioctl function, but does not handle the request.

But I completely agree, that we should remove those stubs simply because they handle no request at all which is basically the same as no ioctl function.

Thanks,

tglx

#10 lore raw
In reply to Jeff Garzik
Quoted 11 lines · Jeff Garzik
Quoted 2 lines · Alan Cox

EINVAL means you used an ioctl that is correct for the driver but that for some reason the driver didn't like it.

Precisely.

The driver author proactively chose to implement the ioctl and return a value other than ENOTTY.

It is invalid to assume that all such cases are a direct result of author mistakes or ignorance.

If 10% of the cases the error return was for a reason it's still worth doing this. If it matters we'll hear about it.

Something that looks like crap should not get extra protection to stay in the kernel just because it 'might' be non-crap. If it's not clearly documented in the source to be -EINVAL for a good reason (and frankly i cannot think of any) then i'd suggest to do what Alan has been doing for
years: just change it and see if it causes any problems.

It's not like it's hard to revert such a change. (and we'll also add a comment explaining the reason for the -EINVAL in that case.)

Ingo