negative seek offsets in VFS
Quoted 1 line
The addresses returned from
/proc/kallsymson thex86_64are negative and
when
Quoted 1 line
I print the address of a kernel variable with "%p" it comes out negative.
Wow. So can someone explain this? Does the x86_64 architecture actually have a concept of negative addresses?
Ordinarily, I'd think this was a bug if I saw it, but I don't see how %p could format a minus sign without it being deliberate.
In any case, I know POSIX doesn't have a concept of a negative absolute file offset, so a kmem device cannot represent a negative address as a negative file offset. A negative loff_t means something else.
Quoted 6 lines · Bryan Henderson
Quoted 1 line
The addresses returned from
/proc/kallsymson thex86_64are negative andwhen
Quoted 1 line
I print the address of a kernel variable with "%p" it comes out negative.
Wow. So can someone explain this? Does the
x86_64architecture actually have a concept of negative addresses?
Yes, it has. It has a 48bit address space, and the 48th bit is sign extended to 64bits. So you have a big hole in the middle and positive and negative address spaces. The kernel uses the negative half, user space the positive half.
Quoted 3 lines
Ordinarily, I'd think this was a bug if I saw it, but I don't see how %p could format a minus sign without it being deliberate.
It doesnt, but they are still negative.
Quoted 3 lines
In any case, I know POSIX doesn't have a concept of a negative absolute file offset, so a kmem device cannot represent a negative address as a negative file offset. A negative
loff_tmeans something else.
I dont think POSIX has anything to say about /dev/kmem.
-Andi
Quoted 1 line
Quoted 1 line
I dont think POSIX has anything to say about
/dev/kmem.
Not directly, but since the entire point of /dev/kmem is to make kernel memory accessible via the POSIX file interface, POSIX has plenty to say about it.
In architectures with unsigned addresses, there's an obvious way to implement /dev/kmem -- consider the memory address to be the file offset.
I don't see how you can do that with negative memory addresses. POSIX doesn't have negative file offsets. There is nothing before the beginning of a file. How does a POSIX program read address -1 via /dev/kmem?
Quoted 6 lines
if you allowed a negative offset and just
Quoted 3 lines
declared that it stands for the large positive offset you'd get if you coerced it to an unsigned 64 bit integer, then how would you tell a success from a failure in the return code?
The same as any other Linux kernel interfaces do it. The range from
-4096 to -1
Quoted 1 line
is reserved for error codes, the rest is free to use.
I'm familiar with the ERR_PTR strategy for packing a kernel address and an errno into a single word, but I haven't seen this for anything else. I'm talking about the POSIX interface -- i.e. what the caller of the lseek() C library routine would see.
I know we're looking at interfaces below the POSIX interface, but I don't see how recognizing negative file offsets at this level is possible without also recognizing them at the POSIX level.
--
Bryan Henderson IBM Almaden Research Center
San Jose CA Filesystems
Quoted 4 lines
I'm familiar with the
ERR_PTRstrategy for packing a kernel address and an errno into a single word, but I haven't seen this for anything else. I'm talking about the POSIX interface -- i.e. what the caller of thelseek()C library routine would see.
I am not talking about kernel internal interfaces like ERR_PTR. The linux system call interface on most (all?) architectures reserves -1 to -4095 for error returns. When such a error is detected it is converted to errno and -1. This applies to all system calls.
Take a look at unistd.h of your favourite architecture if you dont believe me.
-Andi
Quoted 5 lines
I am not talking about kernel internal interfaces like
ERR_PTR. The linux system call interface on most (all?) architectures reserves -1 to -4095 for error returns. When such a error is detected it is converted to errno and -1. This applies to all system calls.
OK, I remember that now. I probably forgot it because I couldn't think of any use for that special range of values more negative than -4095. Can you give an example of a system call that returns integers less than -4095?
And separately, I'm still confused about how you expect /dev/kmem file offsets to work. While I was mistaken about the existence of negative addresses, I know there are no negative file offsets in POSIX. So how does one look at a negative address via the POSIX file interface and /dev/kmem?
Even if you add the concept of negative file offsets as a glibc/Linux extension of POSIX, you have an ambiguity problem at least with offset -1, right?
--
Bryan Henderson IBM Almaden Research Center
San Jose CA Filesystems
Quoted 1 line · Andi Kleen
The linux system call interface on most (all?) architectures
Actually that's not true on many architectures, including most of the 64-bit ones. See (e.g.) the Alpha, MIPS32, MIPS64, IA64, PPC32 and PPC64 versions of <asm/unistd.h>, which use a separate register to indicate an error return.
That's the reason for the `force_successful_syscall_return' macro, defined in <linux/ptrace.h>:
/*
* System call handlers that, upon successful completion, need
* to return a negative value should call
* force_successful_syscall_return() right before returning.
* On architectures where the syscall convention provides for
* a separate error flag (e.g., alpha, ia64, ppc{,64},
* sparc{,64}, possibly others), this macro can be used to
* ensure that the error flag will not get set. On
* architectures which do not support a separate error flag,
* the macro is a no-op and the spurious error condition needs
* to be filtered out by some other means (e.g., in
* user-level, by passing an extra argument to the syscall
* handler, or something along those lines).
*/
Quoted 6 lines
reserves -1 to -4095 for error returns. When such a error is detected it is converted to errno and -1. This applies to all system calls.
Take a look at
unistd.hof your favourite architecture if you dont believe me.
Most unistd.h's are wrong by now, as they don't test against -4095.
Example, from <asm-x86_64/unistd.h>:
if ((unsigned long)(res) >= (unsigned long)(-127)) { \
errno = -(res); \
res = -1; \
} \
return (type) (res); \Quoted 1 line
From <
asm-x86_64/errno.h>:
#include <asm-generic/errno.h>
Quoted 1 line
From <
asm-generic/errno.h>:
#define ECANCELED 125 /* Operation Canceled */ #define ENOKEY 126 /* Required key not available */ #define EKEYEXPIRED 127 /* Key has expired */ #define EKEYREVOKED 128 /* Key has been revoked */ #define EKEYREJECTED 129 /* Key was rejected by service */
Spot the inconsistency :)
x86_64 tests against -127; SH64 against -125; etc. most of the ones I looked at are wrong, they're different from each other (despite using <asm-generic/errno.h>), and most of them have a comment preceding the test which states yet another different wrong number.
Example, from <asm-s390/unistd.h>:
/* user-visible error numbers are in the range -1 - -122:
see <asm-s390/errno.h> */
#define __syscall_return(type, res) \
do { \
if ((unsigned long)(res) >= (unsigned long)(-125)) { \-- Jamie
Quoted 4 lines · Bryan Henderson
OK, I remember that now. I probably forgot it because I couldn't think of any use for that special range of values more negative than -4095. Can you give an example of a system call that returns integers less than -4095?
fcntl(fd, F_GETOWN, 0) can.
See fs/fcntl.c.
So can ptrace() on some architectures.
-- Jamie
Quoted 26 lines · Jamie Lokier
Quoted 1 line · Andi Kleen
The linux system call interface on most (all?) architectures
Actually that's not true on many architectures, including most of the 64-bit ones. See (e.g.) the Alpha, MIPS32, MIPS64, IA64, PPC32 and PPC64 versions of <
asm/unistd.h>, which use a separate register to indicate an error return.That's the reason for the `force_successful_syscall_return' macro, defined in <
linux/ptrace.h>:/*
* System call handlers that, upon successful completion, need
* to return a negative value should call
*force_successful_syscall_return()right before returning.
* On architectures where the syscall convention provides for
* a separate error flag (e.g., alpha, ia64, ppc{,64},
* sparc{,64}, possibly others), this macro can be used to
* ensure that the error flag will not get set. On
* architectures which do not support a separate error flag,
* the macro is a no-op and the spurious error condition needs
* to be filtered out by some other means (e.g., in
* user-level, by passing an extra argument to the syscall
* handler, or something along those lines).
*/
Quoted 8 lines
Quoted 6 lines
reserves -1 to -4095 for error returns. When such a error is detected it is converted to errno and -1. This applies to all system calls.
Take a look at
unistd.hof your favourite architecture if you dont believe me.Most unistd.h's are wrong by now, as they don't test against -4095.
You are right, but at least glibc tests against -4095.
I will fix x86-64.
Quoted 10 lines
Quoted 2 lines
From <
asm-generic/errno.h>:#define ECANCELED 125 /* Operation Canceled */ #define ENOKEY 126 /* Required key not available */ #define EKEYEXPIRED 127 /* Key has expired */ #define EKEYREVOKED 128 /* Key has been revoked */ #define EKEYREJECTED 129 /* Key was rejected by service */Spot the inconsistency :)
Very nasty indeed. But fortunately near nobody uses the unistd.h macros anyways.
-Andi
Quoted 4 lines · Bryan Henderson
OK, I remember that now. I probably forgot it because I couldn't think of any use for that special range of values more negative than -4095. Can you give an example of a system call that returns integers less than -4095?
Various ioctls for example can return arbitary values.
Quoted 6 lines
And separately, I'm still confused about how you expect
/dev/kmemfile offsets to work. While I was mistaken about the existence of negative addresses, I know there are no negative file offsets in POSIX. So how does one look at a negative address via the POSIX file interface and/dev/kmem?
pread,pwrite,lseek(...,SEEK_SET) should all work.
/dev/kmem is firmly outside POSIX, so this is fine. For other devices the behaviour will not change.
Quoted 4 lines
Even if you add the concept of negative file offsets as a glibc/Linux extension of POSIX, you have an ambiguity problem at least with offset -1, right?
The kernel doesn't use this range as address, so in practice the problem does not occur.
-Andi
Quoted 2 lines
Quoted 1 line · Bryan Henderson
OK, I remember that now. I probably forgot it because I couldn't think
of
Quoted 6 lines
Quoted 4 lines
any use for that special range of values more negative than -4095. Can
you give an example of a system call that returns integers less than -4095?
fcntl(fd, F_GETOWN, 0)can.
Thanks. Actually, I was looking for an example that returns integers -1 - -4095 in the usual way (=errno) and < -4095 meaning something else, but this is even better.
So does fcntl() work on i386? From what I can see in the code, a -9 return from a fcntl(F_GETOWN) system call is ambiguous. It could mean process group 9 or "bad file descriptor." Is that so? If so, is libc able to resolve the ambiguity, or is the fcntl() libc call broken in this arch?
--
Bryan Henderson IBM Almaden Research Center
San Jose CA Filesystems
Quoted 7 lines
pread,pwrite,lseek(...,SEEK_SET) should all work.
/dev/kmemis firmly outside POSIX, so this is fine. For other devices the behaviour will not change.Quoted 2 lines
Even if you add the concept of negative file offsets as a glibc/Linux extension of POSIX, you have an ambiguity problem at least with offset
-1,
Quoted 4 lines
Quoted 1 line
right?
The kernel doesn't use this range as address, so in practice the problem does not occur.
OK, I get it.
But it appears that my answer to your original question (why is there code in VFS to reject a negative file offset argument) is right: that code was designed for a POSIX interface, and putting the check there relieved all the drivers for POSIX files from some of the responsibility of implementing POSIX.
Quoted 1 line
From what I see on the mailing list, people today are willing to open up
the VFS layer to allow some quasi-POSIX file access.
Frankly, I'd much rather see /dev/kmem stick to the file metaphor and use some scheme that doesn't involve negative "file offsets." There is probably a lot of generic file access code, now and in the future, that would appreciate it.
--
Bryan Henderson IBM Almaden Research Center
San Jose CA Filesystems