10

This is more or less a question about methodology and rationale than anything. In programming various kernel modules for Linux, I'm confounded by what I consider to be a clunky way of designing functions. For example, to retrieve the inode of a file given its path, I had to use something like:

struct inode *inode;
struct path path;
kern_path(path_name, LOOKUP_FOLLOW, &path);
inode = path.dentry->d_inode;

Why not just a function that works like:

struct inode inode;
struct path path = kern_path(path_name, LOOKUP_FOLLOW);
inode = path.dentry->d_inode;

Seems much more intuitive.

2
  • 2
    ...because many C functions are written by good C Programmers. Jul 2, 2015 at 19:52
  • ...because only very few functions return simple data, and exception information is almost universally preferred as the returned data. Whereas referenced information in shared memory can be vast, and is often rolled up into a #defined macro if the reference is boilerplate, as in somestatus = kern_path(KERN_PATH_PARMS).
    – mckenzm
    Jul 3, 2015 at 2:01

5 Answers 5

16

And what would you do with the int that kern_path returns?

It's important for functions to be able to return some kind of error code so the user can ensure the function succeeded. There are two obvious options:

  1. Return the error code.

    This means you must take the other value that you'd like to return as a parameter.

  2. Return the value.

    This means you must take the error code as a parameter.

Since you can only return 1 value in C, you've got to take something as a parameter, as ultimately you want to return two things to the user (the error code and the value).

2
  • 2
    Some would argue that returning the value and passing the error in an argument makes it easier to ensure that you don't ignore the error. Of course, there is __attribute__((warn_unused_result)), but people are usually too lazy to use it consistently.
    – o11c
    Jul 3, 2015 at 2:46
  • @o11c: Wow, didn't know about warn_unused_result. I've grown used to a similar feature in go. Of course, in go it's an error instead of a warning and you can't turn it off (it's not optional).
    – slebetman
    Jul 3, 2015 at 3:44
7

The kernel is a C program with special constraints. In particular, the call stack is not allowed to be deep (IIRC, it is limited to 4Kbytes).

When you return a struct, the ABI (see the x86-64 ABI ...) mandates that (except for some short struct fitting in two words) the returned struct goes thru the stack. So such a style would favor quite big stack frames, hence would more easily meet the stack limit.

BTW, the usual style would be to return some integer error code, and modify the data pointed by argument pointers.

On x86-64/Linux returning a structure of two scalar values (integers, pointers, enums, doubles, ....) is definitely worth it, see this.

4
  • The limit is 16KB for some time now, at least on x86-64. It used to be 8KB for several years and even that was not enough (filesystem code, mostly xfs, would overflow very easily esp. when memory reclaim was triggered whilst deep within i/o stack).
    – user4822941
    Jul 2, 2015 at 22:22
  • The struct is often on the stack anyway.
    – o11c
    Jul 3, 2015 at 2:44
  • @o11c: but returning a struct is making a silent copy of it, so you could gets (temporarily) two copies of it on the stack. Jul 3, 2015 at 4:22
  • @BasileStarynkevitch not on any decent compiler. The only thing you might have to worry about is if it can't resolve the aliasing.
    – o11c
    Jul 3, 2015 at 4:27
2

That has a benefit of reducing copying memory blocks back and forth.

Using this methodology allows the invoked function to only modify certain parts of the passed in struct and no memory is being copied at all.

3
  • The benefit is when passing the address the function can operate on the data at that address and the changes are available to the caller. When passing a copy that isn't possible. Jul 2, 2015 at 19:45
  • 1
    Returned structs are usually passed by pointer unless they are small enough to fit in registers. There is no large memory-copy involved unless your compiler is really dumb.
    – o11c
    Jul 3, 2015 at 2:43
  • @o11c that's completely irrelevant. Even if a "smart" compiler can optimize return values, the calling code might need to store the result in a local variable and then you get copying. Also, a lot of the relevant code was written at a time when compilers were significantly less advanced and no one is going to change a whole coding pattern just because compilers improved. Last but not least, your entire assumption relies on full source code, but that doesn't work for libraries, which is exactly the case here (at least for some of the functions) (Also see Basile's answer - Same family of reason)
    – Amit
    Jul 3, 2015 at 8:04
1

Passing in a pointer to a struct that is used for returning data is useful to pass back a large amount of data without having to actually pass it. You're only passing a pointer back and forth instead of a giant struct.

Your example only showed one element that gets filled out. There are drivers that pass back structs with dozens (if not over a hundred) fields. That would get very expensive memory and cpu wise. You want to squeeze as much from your kernel as possible.

Another thing this method allows you to do is to let the memory management happen outside of the function, making the function more generic and reusable.

Not to mention that if you want to return more than 1 thing, this is pretty much the only way of doing it.

A nice side benefit is it allows you to very clearly define the interface to the driver. Declare a struct and there's your API. Nothing else goes in or out.

1

The actual reason is far simpler than the other answers: it's just habit, from a constraint that no longer exists. All other justifications have been invented after the fact; there is usually an equal and opposite justification for the opposite.

In old C compilers, all arguments and return-value had to be able to fit in registers. That meant, if they were larger, they would have to be passed by address.

Not the answer you're looking for? Browse other questions tagged or ask your own question.