ARM: DMA: top-down allocation in DMA coherent region
authorRussell King <rmk+kernel@arm.linux.org.uk>
Sat, 8 Jan 2011 11:49:20 +0000 (11:49 +0000)
committerRussell King <rmk+kernel@arm.linux.org.uk>
Wed, 23 Feb 2011 17:24:11 +0000 (17:24 +0000)
Achieve better usage of the DMA coherent region by doing top-down
allocation rather than bottom up.  If we ask for a 128kB allocation,
this will be aligned to 128kB and satisfied from the very bottom
address.  If we then ask for a 600kB allocation, this will be aligned
to 1MB, and we will have a 896kB hole.

Performing top-down allocation resolves this by allocating the 128kB
at the very top, and then the 600kB can come in below it without any
unnecessary wastage.

This problem was reported by Janusz Krzysztofik, who had 2 x 128kB +
1 x 640kB allocations which wouldn't fit into 1MB.

Tested-by: Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
arch/arm/mm/vmregion.c

index 935993e1b1ef53f5372344cbc6a5c764186557b9..036fdbfdd62f6e91e0c329c7c12a9152dca21d7b 100644 (file)
@@ -38,7 +38,7 @@ struct arm_vmregion *
 arm_vmregion_alloc(struct arm_vmregion_head *head, size_t align,
                   size_t size, gfp_t gfp)
 {
-       unsigned long addr = head->vm_start, end = head->vm_end - size;
+       unsigned long start = head->vm_start, addr = head->vm_end;
        unsigned long flags;
        struct arm_vmregion *c, *new;
 
@@ -54,21 +54,20 @@ arm_vmregion_alloc(struct arm_vmregion_head *head, size_t align,
 
        spin_lock_irqsave(&head->vm_lock, flags);
 
-       list_for_each_entry(c, &head->vm_list, vm_list) {
-               if ((addr + size) < addr)
-                       goto nospc;
-               if ((addr + size) <= c->vm_start)
+       addr = rounddown(addr - size, align);
+       list_for_each_entry_reverse(c, &head->vm_list, vm_list) {
+               if (addr >= c->vm_end)
                        goto found;
-               addr = ALIGN(c->vm_end, align);
-               if (addr > end)
+               addr = rounddown(c->vm_start - size, align);
+               if (addr < start)
                        goto nospc;
        }
 
  found:
        /*
-        * Insert this entry _before_ the one we found.
+        * Insert this entry after the one we found.
         */
-       list_add_tail(&new->vm_list, &c->vm_list);
+       list_add(&new->vm_list, &c->vm_list);
        new->vm_start = addr;
        new->vm_end = addr + size;
        new->vm_active = 1;