mlockall and mmap of IO devices don't mix
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 6 lines · Joe Korty
I do not believe that the above constitutes a correct fix. The problem is that
follow_pages()is fundamentally not able to handle a mapping which does not have a 'struct page' backing it up, and a mapping to IO memory by definition has no 'struct page' structure to back it up.
The 2.4 vm scanner handles this by always checking VALID_PAGE().
Maybe follow_pages() should do that too?
-Andi
Quoted 4 lines · Andi Kleen
Quoted 1 line · Joe Korty
Quoted 8 lines
Quoted 5 lines
I do not believe that the above constitutes a correct fix. The problem is that
follow_pages()is fundamentally not able to handle a mapping which does not have a 'struct page' backing it up, and a mapping to IO memory by definition has no 'struct page' structure to back it up.The 2.4 vm scanner handles this by always checking
VALID_PAGE().
VALID_PAGE got nuked.
It still exists in vestigial form in some architectures, but x86 does not implement it and core kernel does not use it.
It is not a trivial thing to do now we no longer have a single mem_map[].
Quoted 1 line
It is not a trivial thing to do now we no longer have a single mem_map[].
2.4/DISCONTIGMEM also didn't have a single mem_map and it worked there fine.
I implemented it for x86-64. Arguably it was a bit ugly, but not too bad.
-Andi
Hi,
Quoted 10 lines · Andi Kleen
Quoted 5 lines · Joe Korty
I do not believe that the above constitutes a correct fix. The problem is that
follow_pages()is fundamentally not able to handle a mapping which does not have a 'struct page' backing it up, and a mapping to IO memory by definition has no 'struct page' structure to back it up.The 2.4 vm scanner handles this by always checking
VALID_PAGE().Maybe
follow_pages()should do that too?
It does already.
Just see the most indented check there. It tries to find a struct page from the page frame number (pfn_to_page) after obtaining the pfn from the pte.
This check is only done, if it is a valid pfn (pfn_valid()) of a present pte.
Since make_pages_present uses get_user_pages(), which in turn calls follow_pages() with the above checks, this is properly checked.
The only path where is is NOT checked, is hugetlb stuff. So the error must be there.
If somebody does the changes, please remove the useless get_page_map(), since it just does the above checks done already again after a reverse lookup and I could not find ANY case, where the check in get_page_map() would fail.
So maybe a BUG_ON() the checks in get_page_map() and a LTP run combinded with bttv usage afterwards would be good.
Regards
Ingo Oeser
Quoted 2 lines
This check is only done, if it is a valid pfn (
pfn_valid()) of a present pte.
pfn_valid is useless, it doesn't handle all IO holes on x86 for examples.
-Andi
Quoted 4 lines · Andi Kleen
Quoted 2 lines
This check is only done, if it is a valid pfn (
pfn_valid()) of a present pte.
pfn_validis useless, it doesn't handle all IO holes on x86 for examples.
Sounds like pfn_valid() is buggy on x86. It's supposed to definitively indicate whether the PFN is a valid page of ram (and has a valid struct page entry.) If it doesn't do that, the architecture implementation is wrong.
Signature · 4 lines
Russell King ([email protected]) http://www.arm.linux.org.uk/personal/ Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/ maintainer of: 2.6 PCMCIA - http://pcmcia.arm.linux.org.uk/ 2.6 Serial core
Hi there,
Quoted 10 lines · you
Quoted 4 lines · Andi Kleen
Quoted 2 lines
This check is only done, if it is a valid pfn (
pfn_valid()) of a present pte.
pfn_validis useless, it doesn't handle all IO holes on x86 for examples.Sounds like
pfn_valid()is buggy on x86. It's supposed to definitively indicate whether the PFN is a valid page of ram (and has a valid struct page entry.) If it doesn't do that, the architecture implementation is wrong.
Looks like it. But it also has to be fast (see include/asm-i386/mmzone.h) and doesn't even hide the holes in NUMA machines.
We had a page_is_ram() for this somewhere. I don't know, why this is gone. It would be useful in other places as well.
If the page_is_ram() test could be done using the vma only now, this would be even better and should be called vma_is_ram() to generalize these corner cases (today and in the future) and make more clear what these kind of tests want to do.
Regards
Ingo Oeser
Quoted 7 lines · Ingo Oeser
Quoted 6 lines · you
Quoted 4 lines · Andi Kleen
Quoted 2 lines
This check is only done, if it is a valid pfn (
pfn_valid()) of a present pte.
pfn_validis useless, it doesn't handle all IO holes on x86 for examples.
Quoted 7 lines
Quoted 4 lines
Sounds like
pfn_valid()is buggy on x86. It's supposed to definitively indicate whether the PFN is a valid page of ram (and has a valid struct page entry.) If it doesn't do that, the architecture implementation is wrong.Looks like it. But it also has to be fast (see
include/asm-i386/mmzone.h) and doesn't even hide the holes in NUMA machines.
It has to be correct. We do the following in a hell of a lot of places:
pfn = pte_pfn(pte);
if (pfn_valid(pfn)) {
struct page *page = pfn_to_page(pfn);
/* do something with page */
}basically this type of thing happens in any of the PTE manipulation functions (eg, copy_page_range, zap_pte_range, etc.)
If pfn_valid is returning false positives, and you happen to mmap() an area which gives false positives from a user space application, your kernel will either oops or will corrupt RAM when that application exits.
I believe the comment in mmzone.h therefore is an opinion, and indicates a concern for performance rather than correctness and stability.
Signature · 4 lines
Russell King ([email protected]) http://www.arm.linux.org.uk/personal/ Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/ maintainer of: 2.6 PCMCIA - http://pcmcia.arm.linux.org.uk/ 2.6 Serial core
Hi there,
CC'ed linux-mm and Andrew Morton for expertise.
Quoted 10 lines · Russell King
It has to be correct. We do the following in a hell of a lot of places:
pfn = pte_pfn(pte); if (pfn_valid(pfn)) { struct page *page = pfn_to_page(pfn); /* do something with page */ }basically this type of thing happens in any of the PTE manipulation functions (eg,
copy_page_range,zap_pte_range, etc.)
These functions are called always with pages, where we know, that this is RAM, AFICS. Since sometimes other things are encoded in the PTE, whe check this via pfn_valid().
If I'm wrong about this the gurus from LINUX-MM should complain loudly.
Quoted 6 lines
If
pfn_validis returning false positives, and you happen tommap()an area which gives false positives from a user space application, your kernel will either oops or will corrupt RAM when that application exits.I believe the comment in
mmzone.htherefore is an opinion, and indicates a concern for performance rather than correctness and stability.
I hope you are wrong about this, but I'm not totally sure. So I included the linux-mm mailing list and Andrew Morton for expert advice.
Regards
Ingo Oeser
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 9 lines
Quoted 6 lines
Quoted 1 line
pfn_validis useless, it doesn't handle all IO holes on x86 for examples.Sounds like
pfn_valid()is buggy on x86. It's supposed to definitively indicate whether the PFN is a valid page of ram (and has a valid struct page entry.) If it doesn't do that, the architecture implementation is wrong.Looks like it. But it also has to be fast (see
include/asm-i386/mmzone.h) and doesn't even hide the holes in NUMA machines.
There are no holes between nodes for any i386 NUMA machines at the moment, and we don't free back the struct pages for internal holes yet. So we have pfn_valid set up for i386 such that there's a valid struct page if pfn_valid is true.
Quoted 7 lines
We had a
page_is_ram()for this somewhere. I don't know, why this is gone. It would be useful in other places as well.If the
page_is_ram()test could be done using the vma only now, this would be even better and should be calledvma_is_ram()to generalize these corner cases (today and in the future) and make more clear what these kind of tests want to do.
page_is_ram is defined in arch/i386/mm/init.c:
static inline int page_is_ram(unsigned long pagenr)
{
int i;
for (i = 0; i < e820.nr_map; i++) {
unsigned long addr, end;
if (e820.map[i].type != E820_RAM) /* not usable memory */
continue;
/*
* !!!FIXME!!! Some BIOSen report areas as RAM that
* are not. Notably the 640->1Mb area. We need a sanity
* check here.
*/
addr = (e820.map[i].addr+PAGE_SIZE-1) >> PAGE_SHIFT;
end = (e820.map[i].addr+e820.map[i].size) >> PAGE_SHIFT;
if ((pagenr >= addr) && (pagenr < end))
return 1;
}
return 0;
}However, we probably want a runtime one that checks some aspect of the struct page itself to see whether it's a valid memory page or not.
I believe it's useful to have the faster and slower tests still. Most things using pfn_valid seem to be doing it before a pfn_to_page translation to check it's safe, and we still seem to have that correct. pfn_has_struct_page or something might be a better name, but still.
M.