__get_free_pages(): is the MEM really mine?
Thanks, Ingo.
Quoted 2 lines
perfectly legal - but there is no guarantee you will succeed getting two nearby 2 MB pages. You will get it if your driver initializes during
Yes, I try until I have enough (2 in 2.4.x, 32 in 2.2.x) contig chunks and free the rest again. But statistically I do get 2 contig chunks immediately quite often if the X11 is not already running. 256 MB is in the box
Quoted 4 lines
Quoted 2 lines
When I run the user appl. again after short time I mostly get the same chunk of physical memory (
virt_to_busis identical!)have you perpahs freed that page?
Yes, as expected.
Quoted 4 lines
printk every occasion of
allocating/freeing a 2 MB buffer and i'm sure you'll see the problem. (Perhaps it's theclose()implicitly done byexit()that frees the buffer?)
Yes, that is definitely the case and I expect it.
Quoted 1 line
Quoted 1 line
Now close '
/dev/aprsc027' fd = 3 ...
The test program does a close and on the console
But I tend to conclude from getting the same phys address again after some time that noone else uses much memory inbetween. Plus, the first page of the area stays Zero all the time while the higher pages seem to be used by someone. I know that this is no prove that the 1st page was really not used otherwise but... And I know it is also legal that other procs use the very same RAM. But the prob is that the system gets unstable. And it doesn't get unstable if order count is 0, which i use in minor 0..23 to allocate a small kernel buffer. Only minor 26 and 27 allocate a 4 MB contig buffer in open() and mmap that buffer to user space, while minor 28 and 29 only allocate a small buffer to write the FIFOs and mmap the 32 MB PCI area of the card.
The impression I have is that only large allocations behave strangely. But the instability is not visible immediately. Too bad. Only after some time do I see strange behaviour of the system. But I think I don't see them if I only use the functionality of the minors with smaller buffers.
could my nopage() method be inmplemented wrongly?
I read Alessandro Rubini's book I learned how to implement it:
chn_ptr = (struct RSC_SOFTC *)vma_ptr->vm_private_data; card_ptr = chn_ptr->card_ptr; minor = chn_ptr->minor; card_chn = minor & APRSC_CARD_CHNS_MASK; page_ptr = NOPAGE_SIGBUS;
Iprintf(" address=$%08lX ad - vm_start=$%08lX VMA_OFFSET=$%08lX \n",
address,
address - vma_ptr->vm_start,
vma_ptr->vm_pgoff << PAGE_SHIFT);offset = address - vma_ptr->vm_start + (vma_ptr->vm_pgoff << PAGE_SHIFT);
if(card_chn == APRSC_DEV_PER_CARD - 6) /* Bild 1 Ch 26 dieser Karte */
{
if(offset > card_ptr->contig_len0)
{
return(page_ptr);
}
/*endif()*/
start = (ULONG)card_ptr->dma_mem0;
}
else if(card_chn == APRSC_DEV_PER_CARD - 5) /* Bild 2 Ch 27 dieser Karte
*/
{
if(offset > card_ptr->contig_len1)
{
return(page_ptr);
}
/*endif()*/
start = (ULONG)card_ptr->dma_mem1;
}
else
{
return(page_ptr); }
/*endif(card_chn == APRSC_DEV_PER_CARD - [(>=7), (<=4)] usw.)*/ page_ptr = virt_to_page(start + offset);
Iprintf(" start+off=$%08lX page_ptr=$%8p \n",
start + offset,
page_ptr);
get_page(page_ptr);
return(page_ptr);
Here is the console output of my driver during the test program:
Sep 27 11:43:28 pcma73 kernel: rsc_open() minor=$1B Sep 27 11:43:28 pcma73 kernel: DMA blk 0 at KV:$CE800000 BUS:$0E800000 Sep 27 11:43:28 pcma73 kernel: DMA blk 1 at KV:$CE600000 BUS:$0E600000 contig < Sep 27 11:43:28 pcma73 kernel: Max Buffer Frag at BUS:$0E600000 len $00400000 bytes Sep 27 11:43:28 pcma73 kernel: Collected DMA Buffer1 at KS:$0000CE600000 BUS:$0E600000 len $00400000 bytes Sep 27 11:43:28 pcma73 kernel: rsc_ioctl() Sep 27 11:43:28 pcma73 kernel: RSC_IOC_GET_FIX: copy_to_user() returned $0 Sep 27 11:43:28 pcma73 kernel: rsc_ioctl() Sep 27 11:43:28 pcma73 kernel: rsc_mmap() minor=$1B offset=$00000000 Sep 27 11:43:28 pcma73 kernel: rsc_vma_open() Sep 27 11:43:28 pcma73 kernel: rsc_nopage() Sep 27 11:43:28 pcma73 kernel: address=$40132000 ad - vm_start=$00000000 VMA_OFFSET=$00000000 Sep 27 11:43:28 pcma73 kernel: start+off=$CE600000 page_ptr=$c1398000 Sep 27 11:43:28 pcma73 kernel: rsc_nopage() Sep 27 11:43:28 pcma73 kernel: address=$40134000 ad - vm_start=$00002000 VMA_OFFSET=$00000000 Sep 27 11:43:28 pcma73 kernel: start+off=$CE602000 page_ptr=$c1398080 Sep 27 11:43:28 pcma73 kernel: rsc_ioctl() Sep 27 11:43:28 pcma73 kernel: RSC_IOC_DMA_OUT Sep 27 11:43:28 pcma73 kernel: rsc_vma_close() Sep 27 11:43:28 pcma73 kernel: rsc_close() Sep 27 11:43:28 pcma73 kernel: PCIRSC: DMA0CSR=$10 ok. Sep 27 11:43:28 pcma73 kernel: PCIRSC: DMA1CSR=$10 ok. Sep 27 11:43:28 pcma73 kernel: PCIRSC: PCISR=$0290 ok. Sep 27 11:43:28 pcma73 kernel: Free DMA blk 0 at KS:$CE800000 Sep 27 11:43:28 pcma73 kernel: Free DMA blk 1 at KS:$CE600000
Thank you very much for your help!
Signature · 9 lines
Bernd Harries [email protected] http://bharries.freeyellow.com [email protected] Tel. +49 421 809 7343 priv. | MSB First! [email protected] +49 421 457 3966 offi. | Linux-m68k [email protected] +49 172 139 6054 handy | Medusa T40 GMX - Die Kommunikationsplattform im Internet. http://www.gmx.net
Quoted 3 lines · Bernd Harries
Quoted 1 line
have you perpahs freed that page?
Yes, as expected.
well - what did you expect to happen? A freed page is going to be reused for other purposes. A big 2MB allocation can be reused in part, once memory usage grows. So you should not expect the device to be able to DMA into a page that got freed, unpunished. Perhaps i'm misunderstanding the problem.
Quoted 4 lines
But I tend to conclude from getting the same phys address again after some time that noone else uses much memory inbetween. Plus, the first page of the area stays Zero all the time while the higher pages seem to be used by someone. [...]
the buddy allocator allocates top down. Plus, if you allocate a 2MB physically continuous chunk then the likelyhood is high that there were fragmented pages skipped during the initial search for a 2MB block - so you still have a fair likelyhood to reallocate it after some time, if memory usage is light. But this likelyhood nears zero once RAM usage gets near 100%.
Ingo
Greetings from the 2001 Linux Devel meeting in Oldenburg!
Roman Zippel looked at my driver and added code to print the usage counter for each page after a 9-order __get_free_pages().
We found that only the first (!) page has a count of 1, the others have 0!
That would cover my impression, that only the 1st page is really mine...
Roman found that strange and added this:
struct page * page = virt_to_page(card_ptr->dma_blk1[n]);
int i;
for(i = 0; i < (1 << max_order); i++, page++)
{
atomic_set(&page->count, 1);
}And the freeing of the pages is now done page by page in the _vma_close() function.
I will now test the version but I have only a 1-CPU box here. On an SMP Box I could imagine that even between __get_free_pages() and the atomic_set(&page->count, 1) someone else already uses my pages.
Could you please comment on this?
Thanks,
Signature · 6 lines
Bernd Harries [email protected] http://www.freeyellow.com/members/bharries [email protected] Tel. +49 421 809 7343 priv. | MSB First! [email protected] +49 421 457 3966 offi. | Linux-m68k [email protected] 8.48'21" E 52.48'52" N | Medusa T40
Quoted 5 lines · Bernd Harries
Roman Zippel looked at my driver and added code to print the usage counter for each page after a 9-order
__get_free_pages().We found that only the first (!) page has a count of 1, the others have 0!
This is a property of Linux's buddy allocator. If you allocate a 9th order 'big page', that does not mean you can free the pages one by one. Higher order pages are 'one unit' and are typically handled as such. Eg. the kernel stack is allocated and freed as order 1 pages.
Quoted 9 lines
struct page * page = virt_to_page(card_ptr->dma_blk1[n]); int i; for(i = 0; i < (1 << max_order); i++, page++) { atomic_set(&page->count, 1); }And the freeing of the pages is now done page by page in the
_vma_close()function.
while unconventional, doing this is safe. There is nothing in the page structure that says that the page was allocated as a higher order page. So if you fix up the page counts, freeing them as separate entities is safe. (in fact it's even safe to split it up into 8k or 16k pages - not that this would be useful for you.) But the above is an 'internal' property of the Linux page allocator, so it's not guaranteed to stay so forever.
(the Linux kernel does not do the above for understandable reasons: it takes a loop of 512 iterations to fix up the page counts in the above way, which is noticeable runtime overhead.)
is it a fundamental property of the hardware that it needs a continuous physical memory buffer? If not then i'd strongly suggest updating the driver to do scatter-gather instead of trying to allocate a 2 MB page. Being able to allocate a 2 MB page is only guaranteed during bootup. There is just no mechanizm in Linux that guarantees it for you to be able to allocate a 2 MB page (let alone two adjacent 2 MB pages), in even a moderately utilized system. Scatter-gather avoids all these problems.
Ingo
Quoted 2 lines · Ingo Molnar
This is a property of Linux's buddy allocator. If you allocate a 9th order 'big page', that does not mean you can free the pages one by one.
I used free_pages((ULONG)card_ptr->dma_blk0[n], max_order); before Roman changed it. And still do for minor 26 now...
Quoted 5 lines
while unconventional, doing this is safe. There is nothing in the page structure that says that the page was allocated as a higher order page.
But the above is an 'internal' property of
the Linux page allocator, so it's not guaranteed to stay so forever.
Thats why I don't like it so much. But it seems I must do it for some strange reason:
On minor 26 I do it the old way, on minor 27 I use Romans fix. What shall I say:
Reading and writing to the buffer allocated with Roman's fix so far never crashed the system. But doing it the normal way (minor 26) how I also learned it from A. Rubini's book,
does harm to the system.
After usage of the normally allocated buffer the strangest thing occur:
- Issing w caused a dump on the console once.
- Halt doesn't really halt the system completely
- Reboot caused everything to hang, partitions still dirty...
Quoted 3 lines
(the Linux kernel does not do the above for understandable reasons: it takes a loop of 512 iterations to fix up the page counts in the above way, which is noticeable runtime overhead.)
Oh yes, indeed! But:
Is there a guarantee that the n - 1 pages above the 1st one are not donated to other programs while my driver uses them?
Quoted 2 lines
is it a fundamental property of the hardware that it needs a continuous physical memory buffer?
Yes. The FW on the card demands it.
Quoted 4 lines
Being able to allocate a 2 MB page is only guaranteed during bootup. There is just no mechanizm in Linux that guarantees it for you to be able to allocate a 2 MB page (let alone two adjacent 2 MB pages), in even a moderately utilized system. Scatter-gather avoids all these problems.
I'll move the code to init_module later once it is stable.
Ciao,
Signature · 9 lines
Bernd Harries [email protected] http://www.freeyellow.com/members/bharries [email protected] Tel. +49 421 809 7343 priv. | MSB First! [email protected] +49 421 457 3966 offi. | Linux-m68k [email protected] 8.48'21" E 52.48'52" N | Medusa T40 <>_<> _______ _____ .---|'"`|---. | |_| |_|_|_|_|_|_|_ (_____) .-----. ______`o"O-OO-OO-O"o'`-o---o-'`-oo-----oo-'`-o---o-'`-o---o-'___
Quoted 2 lines · Bernd Harries
Is there a guarantee that the n - 1 pages above the 1st one are not donated to other programs while my driver uses them?
yes. The 2MB block of 512 x 4k pages (we should perhaps call it a 'order 9 page') is yours.
Quoted 4 lines
Quoted 2 lines
is it a fundamental property of the hardware that it needs a continuous physical memory buffer?
Yes. The FW on the card demands it.
ok. then i'd suggest to do all this allocation at boot-time, and do not deallocate it. This is the safest method. Unless it's a point to have the driver as a module (for other than development purposes).
Quoted 1 line
I'll move the code to
init_modulelater once it is stable.
even init_module() can be executed much later: eg. kmod removes the module because it's unused, and it's reinserted later. So generally it's really unrobust to expect a 9th order allocation to succeed at module_init() time.
the fundamental issue is not the lazyness of Linux VM developers. 99.9% of all allocations are order 0. 99.9% of the remaining allocations are order 1 or 2. It takes a fair amount of overhead and complexity to handle high-order allocations 'well' - it takes even more effort (and a perverse limitation on the use of pointers) to guarantee the success of such allocations all the time.
there is a longer-term and robust solution that could be used though. We could support a generic 'physical memory pool', that gets allocated on bootup (via eg. a physmem=10m kernel boot option), and never gets used for other than such critical allocations. Your driver could call eg. alloc_physmem(size) and free_physmem(). It would work similarly to bootmem.c. This 'physical memory pool' would never be used by generic subsystems - only drivers which support hardware with such limitations are allowed to use it. The advantage of this approach is that there would be one generic way to put physically continuous RAM aside for such drivers - so the driver would not have to worry about the VM situation. The other advantage is that we could decrease MAX_ORDER significantly (to around 7)
- support for higher orders increases the runtime overhead of the buddy allocator, even for low-order allocations.
(later on we could even add support to grow and shrink the size of the physical memory pool (within certain boundaries), so it could be sized boot-time.)
would anything like this be useful? Since it's a completely separate pool (in fact it wont even show up in the normal memory statistics), it does not disturb the existing VM in any way.
Ingo
Hi Ingo,
The problem with mmapping a Kernel buffer to userspace is still there .
It appears that __get_free_pages(GFP_KERNEL, max_order) alone is not enough to request a reliable buffer. On Monday I already sent a message to the list which you may have overseen.
In my driver I have now the normal method on minor 26 and Roman Zippel's method on minor 27. I have used minor 27 quite heavy already and it appears stable. Using minor 26 makes the system instable quite instantly.
I would like you to try my driver either on my system via remote login or I could try to reproduce the effect without DMA accesses to the buffer and modify the driver so that you can try it without hardware in your Computer.
Is one of these 2 ways possible for you?
Thanks,
Signature · 6 lines
Bernd Harries [email protected] http://bharries.freeyellow.com [email protected] Tel. +49 421 809 7343 priv. | MSB First! [email protected] +49 421 457 3966 offi. | Linux-m68k [email protected] +49 172 139 6054 handy | Medusa T40