Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Linux: Kernel

Calling free_pages on part of the memory returned by get_free_pages?

 

 

Linux kernel RSS feed   Index | Next | Previous | View Threaded


timur at freescale

Apr 21, 2008, 2:12 PM

Post #1 of 4 (151 views)
Permalink
Calling free_pages on part of the memory returned by get_free_pages?

According to LDD3, if I call get_free_pages() to allocate X bytes, I have to
free all of those pages with free_pages(). The VM internals are a little bit
over my head, but I looked at the code and I didn't see why that is a requirement.

For example, let's say I want to allocated 6MB of physically-contiguous memory.
If I call x = get_free_pages(11) to get 8MB. What happens if I then do
"free_pages(x + 6 * 1024 * 1024, 9)"?

I remember doing this on the 2.4 kernel, and it never gave me any problems.

--
Timur Tabi
Linux kernel developer at Freescale
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo[at]vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


andi at firstfloor

Apr 24, 2008, 12:27 PM

Post #2 of 4 (134 views)
Permalink
Re: Calling free_pages on part of the memory returned by get_free_pages? [In reply to]

Timur Tabi <timur[at]freescale.com> writes:

> According to LDD3, if I call get_free_pages() to allocate X bytes, I have to
> free all of those pages with free_pages(). The VM internals are a little bit
> over my head, but I looked at the code and I didn't see why that is a requirement.
>
> For example, let's say I want to allocated 6MB of physically-contiguous memory.
> If I call x = get_free_pages(11) to get 8MB. What happens if I then do
> "free_pages(x + 6 * 1024 * 1024, 9)"?
>
> I remember doing this on the 2.4 kernel, and it never gave me any problems.

It is ok, as long as you don't use compound pages (__GFP_COMP) and call
split_page() to fix up the reference counts.

Also you do this to save memory right? The large system hash code does it too
and I used to do it in some 2.4 change with an alloc_pages_exact()
which never made it into 2.6.

If it's reasonably common we should re-add alloc/get_pages_exact() helper to
make this pattern clear and easier to use.

-Andi
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo[at]vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


timur at freescale

Apr 24, 2008, 12:32 PM

Post #3 of 4 (132 views)
Permalink
Re: Calling free_pages on part of the memory returned by get_free_pages? [In reply to]

Andi Kleen wrote:

> It is ok, as long as you don't use compound pages (__GFP_COMP) and call
> split_page() to fix up the reference counts.

Thanks. I've never even heard of compound pages, so that's not a problem. I'll
look up split_page().

> Also you do this to save memory right?

Yes, I expect to allocate a 5MB chunk of memory, which means I'll be wasting 3MB
of physically-contiguous memory on an embedded system.

> The large system hash code does it too

Ah, I see that in alloc_large_system_hash(). Thanks

> If it's reasonably common we should re-add alloc/get_pages_exact() helper to
> make this pattern clear and easier to use.

I'd be in favor of that! I was planning on doing something like that anyway.

--
Timur Tabi
Linux kernel developer at Freescale
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo[at]vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


timur at freescale

May 9, 2008, 3:33 PM

Post #4 of 4 (120 views)
Permalink
Re: Calling free_pages on part of the memory returned by get_free_pages? [In reply to]

Andi Kleen wrote:
> Timur Tabi <timur[at]freescale.com> writes:
>
>> According to LDD3, if I call get_free_pages() to allocate X bytes, I have to
>> free all of those pages with free_pages(). The VM internals are a little bit
>> over my head, but I looked at the code and I didn't see why that is a requirement.
>>
>> For example, let's say I want to allocated 6MB of physically-contiguous memory.
>> If I call x = get_free_pages(11) to get 8MB. What happens if I then do
>> "free_pages(x + 6 * 1024 * 1024, 9)"?
>>
>> I remember doing this on the 2.4 kernel, and it never gave me any problems.
>
> It is ok, as long as you don't use compound pages (__GFP_COMP) and call
> split_page() to fix up the reference counts.
>
> Also you do this to save memory right? The large system hash code does it too
> and I used to do it in some 2.4 change with an alloc_pages_exact()
> which never made it into 2.6.
>
> If it's reasonably common we should re-add alloc/get_pages_exact() helper to
> make this pattern clear and easier to use.

Follow-up question:

Say I allocate 8MB with __get_free_pages() and then use multiple free_pages() /
split_page() calls to free the last 3MB. Will the de-allocated blocks be merged
into larger-order chunks, if they're contiguous? That is, if I repeat this process:

1. Allocate 8MB
2. Free 3MB
3. Free 5MB

over and over again, will I totally fragment memory to the point that 8MB
allocates will never work again?

--
Timur Tabi
Linux kernel developer at Freescale
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo[at]vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Linux kernel RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.