mmap not working?
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 40 lines · H. Peter Anvin
Quoted 27 lines · Rogier Wolff
I know about TLBs. I know how they work, and I think I've explained it well enough that rereading my message should allow you to understand what I'm saying. Still, let me try to picture it...
Situation A:
physical map. XX is the interesting part, | is a page boundary, = is "uninteresting stuff".
|========|XX======|========|========|========|virtual map:
|--------|XX======|--------|--------|--------| ^ | This is the pointer that mmap returns.- is "unmapped".
Situation B:
|========|======XX|========|========|========|virtual map:
Quoted 9 lines
|--------|======XX|--------|--------|--------| ^ | This is the pointer that mmap returns.In Situation A I get the 1K mapped that I wanted and 3 more because the MMU can't NOT give me access to that. Situation B is exactly the same, except that I get those extra 3K in front of the pointer that I get returned by mmap.
Quoted 8 lines
Just make the adjustment in userspace, if your application really can handle it. This is never going to fly generically (and therefore not get integrated into anything), because the PCI BIOS will typically map multiple things into that 4K chunk, and thus you have opened up your system to messing with a completely "innocent" device.
Since the only way is to avoid this involves moving your device to its own 4K chunk of I/O space anyway, you don't really have a choice.
There is this application that was written in '91-93 that works in situation A and not in situation B. It follows the ruls from "mmap"(*), but the kernel just doesn't do the obvious thing.
If I address something before my 1k window, in situation A, I'll get a segfault. If I address something beyond my 1k window in situation B I'll get a segfault.
If I address something after my 1k window in situation A, I'll access an innocent other device. Same if I address something before my window in situation B.
Now in practise, I agree that it is more likely in situation B that something is actually mapped there.
I'm not sure wether the kernel has been wrong all the time or if something changed recently. I posted the "workaround" the first time through, which also works from userspace. I can change my application. I can modify my libc.
However, I'd rather have "mmap" fixed, as that fixes it for all other applications too. Not just for mine on my system.
The SGI manpage says:
All implementations interpret an addr value of
zero as granting the system complete freedom in selecting pa, subject to constraints described below. A non-zero value of addr is taken to be a suggestion of a process address near which the mapping should be placed.
which hints at a possible non-alignment. It also mentions that "offset" should be page-aligned, which I disagree with here:
everything has been set up to "do the right thing" when the mapping is possible with an unaligned offset.
Roger.
(*) Allow mmap to chose the address, to allow mmap the maximum flexibilty of mapping your object.
Signature · 4 lines
** [email protected] ** http://www.BitWizard.nl/ ** +31-15-2137555 ** *-- BitWizard writes Linux device drivers for any device you may have! --* * There are old pilots, and there are bold pilots. * There are also old, bald pilots.
Quoted 6 lines · Rogier Wolff
The SGI manpage says:
All implementations interpret an addr value of
zero as granting the system complete freedom in selecting pa, subject to constraints described below. A non-zero value of addr is taken to be a suggestion of a process address near which the mapping should be placed.
^^^^^^^^^^
The key word here is "suggestion". There is absolutely no requirement that the OS actually uses this address.
Quoted 4 lines
which hints at a possible non-alignment. It also mentions that "offset" should be page-aligned, which I disagree with here:
everything has been set up to "do the right thing" when the mapping is possible with an unaligned offset.
I don't know what MMU your machine has, but on most (if not all) machines an mmap() is only possible when it's page-aligned.
regards,
Rik
Signature · 3 lines
Shortwave goes a long way: irc.starchat.net #swl http://www.surriel.com/ http://distro.conectiva.com/
Quoted 15 lines · [email protected] (Rogier Wolff) writes:
I'm not sure wether the kernel has been wrong all the time or if something changed recently. I posted the "workaround" the first time through, which also works from userspace. I can change my application. I can modify my libc.
However, I'd rather have "mmap" fixed, as that fixes it for all other applications too. Not just for mine on my system.
The SGI manpage says:
All implementations interpret an addr value of
zero as granting the system complete freedom in selecting pa, subject to constraints described below. A non-zero value of addr is taken to be a suggestion of a process address near which the mapping should be placed.
Quoted 4 lines
which hints at a possible non-alignment. It also mentions that "offset" should be page-aligned, which I disagree with here:
everything has been set up to "do the right thing" when the mapping is possible with an unaligned offset.
Except there is no way to give you enough information to munmap the page. As the address passed to munmap must be page aligned.
The current policy appears to make an application think as up front as possible about the need to be page aligned when talking to mmap, while not being overly harsh. We do have the silent rounding up of length until it is a multiple of PAGE_SIZE.
Beyond this the internal linux implementation of mmap does not even see the extra bits in the offset. Instead the most recent syscall entry point takes an argument as to which page you want to mmap from the device. This allows much larger devices to be mmaped while still using 32bit arithmetic.
So I neither see that it is easy or even desirable to ``fix'' mmap.
Eric