x86: pat.c final cleanup of loop body in reserve_memtype
[safe/jmp/linux-2.6] / arch / x86 / mm / pat.c
index a6507bf..a885a10 100644 (file)
@@ -178,6 +178,33 @@ static unsigned long pat_x_mtrr_type(u64 start, u64 end, unsigned long req_type)
        return req_type;
 }
 
+static int chk_conflict(struct memtype *new, struct memtype *entry,
+                       unsigned long *type)
+{
+       if (new->type != entry->type) {
+               if (type) {
+                       new->type = entry->type;
+                       *type = entry->type;
+               } else
+                       goto conflict;
+       }
+
+        /* check overlaps with more than one entry in the list */
+       list_for_each_entry_continue(entry, &memtype_list, nd) {
+               if (new->end <= entry->start)
+                       break;
+               else if (new->type != entry->type)
+                       goto conflict;
+       }
+       return 0;
+
+ conflict:
+       printk(KERN_INFO "%s:%d conflicting memory types "
+              "%Lx-%Lx %s<->%s\n", current->comm, current->pid, new->start,
+              new->end, cattr_name(new->type), cattr_name(entry->type));
+       return -EBUSY;
+}
+
 /*
  * req_type typically has one of the:
  * - _PAGE_CACHE_WB
@@ -198,9 +225,11 @@ int reserve_memtype(u64 start, u64 end, unsigned long req_type,
 {
        struct memtype *new, *entry;
        unsigned long actual_type;
+       struct list_head *where;
        int err = 0;
 
-       /* Only track when pat_enabled */
+       BUG_ON(start >= end); /* end is exclusive */
+
        if (!pat_enabled) {
                /* This is identical to page table setting without PAT */
                if (new_type) {
@@ -228,17 +257,13 @@ int reserve_memtype(u64 start, u64 end, unsigned long req_type,
                 */
                u8 mtrr_type = mtrr_type_lookup(start, end);
 
-               if (mtrr_type == MTRR_TYPE_WRBACK) {
-                       req_type = _PAGE_CACHE_WB;
+               if (mtrr_type == MTRR_TYPE_WRBACK)
                        actual_type = _PAGE_CACHE_WB;
-               } else {
-                       req_type = _PAGE_CACHE_UC_MINUS;
+               else
                        actual_type = _PAGE_CACHE_UC_MINUS;
-               }
-       } else {
-               req_type &= _PAGE_CACHE_MASK;
-               actual_type = pat_x_mtrr_type(start, end, req_type);
-       }
+       } else
+               actual_type = pat_x_mtrr_type(start, end,
+                                             req_type & _PAGE_CACHE_MASK);
 
        new  = kmalloc(sizeof(struct memtype), GFP_KERNEL);
        if (!new)
@@ -254,150 +279,50 @@ int reserve_memtype(u64 start, u64 end, unsigned long req_type,
        spin_lock(&memtype_lock);
 
        /* Search for existing mapping that overlaps the current range */
+       where = NULL;
        list_for_each_entry(entry, &memtype_list, nd) {
-               struct memtype *saved_ptr;
-
-               if (entry->start >= end) {
-                       dprintk("New Entry\n");
-                       list_add(&new->nd, entry->nd.prev);
-                       new = NULL;
+               if (end <= entry->start) {
+                       where = entry->nd.prev;
                        break;
-               }
-
-               if (start <= entry->start && end >= entry->start) {
-                       if (actual_type != entry->type && new_type) {
-                               actual_type = entry->type;
-                               *new_type = actual_type;
-                               new->type = actual_type;
-                       }
-
-                       if (actual_type != entry->type) {
-                               printk(
-               KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
-                                       current->comm, current->pid,
-                                       start, end,
-                                       cattr_name(actual_type),
-                                       cattr_name(entry->type));
-                               err = -EBUSY;
-                               break;
-                       }
-
-                       saved_ptr = entry;
-                       /*
-                        * Check to see whether the request overlaps more
-                        * than one entry in the list
-                        */
-                       list_for_each_entry_continue(entry, &memtype_list, nd) {
-                               if (end <= entry->start) {
-                                       break;
-                               }
-
-                               if (actual_type != entry->type) {
-                                       printk(
-               KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
-                                               current->comm, current->pid,
-                                               start, end,
-                                               cattr_name(actual_type),
-                                               cattr_name(entry->type));
-                                       err = -EBUSY;
-                                       break;
-                               }
-                       }
-
-                       if (err) {
-                               break;
+               } else if (start <= entry->start) { /* end > entry->start */
+                       err = chk_conflict(new, entry, new_type);
+                       if (!err) {
+                               dprintk("Overlap at 0x%Lx-0x%Lx\n",
+                                       entry->start, entry->end);
+                               where = entry->nd.prev;
                        }
-
-                       dprintk("Overlap at 0x%Lx-0x%Lx\n",
-                              saved_ptr->start, saved_ptr->end);
-                       /* No conflict. Go ahead and add this new entry */
-                       list_add(&new->nd, saved_ptr->nd.prev);
-                       new = NULL;
                        break;
-               }
-
-               if (start < entry->end) {
-                       if (actual_type != entry->type && new_type) {
-                               actual_type = entry->type;
-                               *new_type = actual_type;
-                               new->type = actual_type;
-                       }
-
-                       if (actual_type != entry->type) {
-                               printk(
-               KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
-                                       current->comm, current->pid,
-                                       start, end,
-                                       cattr_name(actual_type),
-                                       cattr_name(entry->type));
-                               err = -EBUSY;
-                               break;
+               } else if (start < entry->end) { /* start > entry->start */
+                       err = chk_conflict(new, entry, new_type);
+                       if (!err) {
+                               dprintk("Overlap at 0x%Lx-0x%Lx\n",
+                                       entry->start, entry->end);
+                               where = &entry->nd;
                        }
-
-                       saved_ptr = entry;
-                       /*
-                        * Check to see whether the request overlaps more
-                        * than one entry in the list
-                        */
-                       list_for_each_entry_continue(entry, &memtype_list, nd) {
-                               if (end <= entry->start) {
-                                       break;
-                               }
-
-                               if (actual_type != entry->type) {
-                                       printk(
-               KERN_INFO "%s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
-                                               current->comm, current->pid,
-                                               start, end,
-                                               cattr_name(actual_type),
-                                               cattr_name(entry->type));
-                                       err = -EBUSY;
-                                       break;
-                               }
-                       }
-
-                       if (err) {
-                               break;
-                       }
-
-                       dprintk("Overlap at 0x%Lx-0x%Lx\n",
-                                saved_ptr->start, saved_ptr->end);
-                       /* No conflict. Go ahead and add this new entry */
-                       list_add(&new->nd, &saved_ptr->nd);
-                       new = NULL;
                        break;
                }
        }
 
        if (err) {
-               printk(KERN_INFO
-       "reserve_memtype failed 0x%Lx-0x%Lx, track %s, req %s\n",
-                       start, end, cattr_name(new->type),
-                       cattr_name(req_type));
+               printk(KERN_INFO "reserve_memtype failed 0x%Lx-0x%Lx, "
+                      "track %s, req %s\n",
+                      start, end, cattr_name(new->type), cattr_name(req_type));
                kfree(new);
                spin_unlock(&memtype_lock);
                return err;
        }
 
-       if (new) {
-               /* No conflict. Not yet added to the list. Add to the tail */
+       if (where)
+               list_add(&new->nd, where);
+       else
                list_add_tail(&new->nd, &memtype_list);
-               dprintk("New Entry\n");
-       }
-
-       if (new_type) {
-               dprintk(
-       "reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n",
-                       start, end, cattr_name(actual_type),
-                       cattr_name(req_type), cattr_name(*new_type));
-       } else {
-               dprintk(
-       "reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s\n",
-                       start, end, cattr_name(actual_type),
-                       cattr_name(req_type));
-       }
 
        spin_unlock(&memtype_lock);
+
+       dprintk("reserve_memtype added 0x%Lx-0x%Lx, track %s, req %s, ret %s\n",
+               start, end, cattr_name(new->type), cattr_name(req_type),
+               new_type ? cattr_name(*new_type) : "-");
+
        return err;
 }
 
@@ -406,10 +331,8 @@ int free_memtype(u64 start, u64 end)
        struct memtype *entry;
        int err = -EINVAL;
 
-       /* Only track when pat_enabled */
-       if (!pat_enabled) {
+       if (!pat_enabled)
                return 0;
-       }
 
        /* Low ISA region is always mapped WB. No need to track */
        if (is_ISA_range(start, end - 1))