Re: [PATCH 04/12] mm: alloc_contig_freed_pages() added

From: Dave Hansen
Date: Thu Mar 31 2011 - 11:58:19 EST


On Thu, 2011-03-31 at 15:16 +0200, Marek Szyprowski wrote:
>
> +unsigned long alloc_contig_freed_pages(unsigned long start, unsigned long end,
> + gfp_t flag)
> +{
> + unsigned long pfn = start, count;
> + struct page *page;
> + struct zone *zone;
> + int order;
> +
> + VM_BUG_ON(!pfn_valid(start));

This seems kinda mean. Could we return an error? I understand that
this is largely going to be an early-boot thing, but surely trying to
punt on crappy input beats a full-on BUG().

if (!pfn_valid(start))
return -1;

> + zone = page_zone(pfn_to_page(start));
> +
> + spin_lock_irq(&zone->lock);
> +
> + page = pfn_to_page(pfn);
> + for (;;) {
> + VM_BUG_ON(page_count(page) || !PageBuddy(page));
> + list_del(&page->lru);
> + order = page_order(page);
> + zone->free_area[order].nr_free--;
> + rmv_page_order(page);
> + __mod_zone_page_state(zone, NR_FREE_PAGES, -(1UL << order));
> + pfn += 1 << order;
> + if (pfn >= end)
> + break;

If start->end happens to span the end of a zone, I believe this will
jump out of the zone. It will still be pfn_valid(), but potentially not
in the same zone.

> + VM_BUG_ON(!pfn_valid(pfn));
> + page += 1 << order;
> + }

That will break on SPARSEMEM. You potentially need to revalidate the
pfn->page mapping on every MAX_ORDER pfn change. It's easiest to just
do pfn_to_page() on each loop.

> +
> + spin_unlock_irq(&zone->lock);
>
> +void free_contig_pages(struct page *page, int nr_pages)
> +{
> + for (; nr_pages; --nr_pages, ++page)
> + __free_page(page);
> +}

Can't help but notice that this resembles a bit of a patch I posted last
week:

http://www.spinics.net/lists/linux-mm/msg16364.html

We'll have to make sure we only have one copy of this in the end.

-- Dave

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