KVM: x86 emulator: add and use new callbacks set_idt(), set_gdt()
[pandora-kernel.git] / mm / swapfile.c
index df3dc7a..8c6b3ce 100644 (file)
@@ -94,39 +94,6 @@ __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
        return ret;
 }
 
-/*
- * We need this because the bdev->unplug_fn can sleep and we cannot
- * hold swap_lock while calling the unplug_fn. And swap_lock
- * cannot be turned into a mutex.
- */
-static DECLARE_RWSEM(swap_unplug_sem);
-
-void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
-{
-       swp_entry_t entry;
-
-       down_read(&swap_unplug_sem);
-       entry.val = page_private(page);
-       if (PageSwapCache(page)) {
-               struct block_device *bdev = swap_info[swp_type(entry)]->bdev;
-               struct backing_dev_info *bdi;
-
-               /*
-                * If the page is removed from swapcache from under us (with a
-                * racy try_to_unuse/swapoff) we need an additional reference
-                * count to avoid reading garbage from page_private(page) above.
-                * If the WARN_ON triggers during a swapoff it maybe the race
-                * condition and it's harmless. However if it triggers without
-                * swapoff it signals a problem.
-                */
-               WARN_ON(page_count(page) <= 1);
-
-               bdi = bdev->bd_inode->i_mapping->backing_dev_info;
-               blk_run_backing_dev(bdi, page);
-       }
-       up_read(&swap_unplug_sem);
-}
-
 /*
  * swapon tell device that all the old swap contents can be discarded,
  * to allow the swap device to optimize its wear-levelling.
@@ -212,8 +179,8 @@ static int wait_for_discard(void *word)
 #define SWAPFILE_CLUSTER       256
 #define LATENCY_LIMIT          256
 
-static inline unsigned long scan_swap_map(struct swap_info_struct *si,
-                                         unsigned char usage)
+static unsigned long scan_swap_map(struct swap_info_struct *si,
+                                  unsigned char usage)
 {
        unsigned long offset;
        unsigned long scan_base;
@@ -880,7 +847,7 @@ unsigned int count_swap_pages(int type, int free)
 static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
                unsigned long addr, swp_entry_t entry, struct page *page)
 {
-       struct mem_cgroup *ptr = NULL;
+       struct mem_cgroup *ptr;
        spinlock_t *ptl;
        pte_t *pte;
        int ret = 1;
@@ -1550,6 +1517,36 @@ bad_bmap:
        goto out;
 }
 
+static void enable_swap_info(struct swap_info_struct *p, int prio,
+                               unsigned char *swap_map)
+{
+       int i, prev;
+
+       spin_lock(&swap_lock);
+       if (prio >= 0)
+               p->prio = prio;
+       else
+               p->prio = --least_priority;
+       p->swap_map = swap_map;
+       p->flags |= SWP_WRITEOK;
+       nr_swap_pages += p->pages;
+       total_swap_pages += p->pages;
+
+       /* insert swap space into swap_list: */
+       prev = -1;
+       for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
+               if (p->prio >= swap_info[i]->prio)
+                       break;
+               prev = i;
+       }
+       p->next = i;
+       if (prev < 0)
+               swap_list.head = swap_list.next = p->type;
+       else
+               swap_info[prev]->next = p->type;
+       spin_unlock(&swap_lock);
+}
+
 SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
 {
        struct swap_info_struct *p = NULL;
@@ -1621,32 +1618,17 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
        current->flags &= ~PF_OOM_ORIGIN;
 
        if (err) {
+               /*
+                * reading p->prio and p->swap_map outside the lock is
+                * safe here because only sys_swapon and sys_swapoff
+                * change them, and there can be no other sys_swapon or
+                * sys_swapoff for this swap_info_struct at this point.
+                */
                /* re-insert swap space back into swap_list */
-               spin_lock(&swap_lock);
-               if (p->prio < 0)
-                       p->prio = --least_priority;
-               prev = -1;
-               for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
-                       if (p->prio >= swap_info[i]->prio)
-                               break;
-                       prev = i;
-               }
-               p->next = i;
-               if (prev < 0)
-                       swap_list.head = swap_list.next = type;
-               else
-                       swap_info[prev]->next = type;
-               nr_swap_pages += p->pages;
-               total_swap_pages += p->pages;
-               p->flags |= SWP_WRITEOK;
-               spin_unlock(&swap_lock);
+               enable_swap_info(p, p->prio, p->swap_map);
                goto out_dput;
        }
 
-       /* wait for any unplug function to finish */
-       down_write(&swap_unplug_sem);
-       up_write(&swap_unplug_sem);
-
        destroy_swap_extents(p);
        if (p->flags & SWP_CONTINUED)
                free_swap_count_continuations(p);
@@ -2036,7 +2018,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
        char *name;
        struct file *swap_file = NULL;
        struct address_space *mapping;
-       int i, prev;
+       int i;
+       int prio;
        int error;
        union swap_header *swap_header;
        int nr_extents;
@@ -2068,7 +2051,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 
        p->swap_file = swap_file;
        mapping = swap_file->f_mapping;
-       inode = mapping->host;
 
        for (i = 0; i < nr_swapfiles; i++) {
                struct swap_info_struct *q = swap_info[i];
@@ -2081,6 +2063,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                }
        }
 
+       inode = mapping->host;
+       /* If S_ISREG(inode->i_mode) will do mutex_lock(&inode->i_mutex); */
        error = claim_swapfile(p, inode);
        if (unlikely(error))
                goto bad_swap;
@@ -2133,30 +2117,11 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
        }
 
        mutex_lock(&swapon_mutex);
-       spin_lock(&swap_lock);
+       prio = -1;
        if (swap_flags & SWAP_FLAG_PREFER)
-               p->prio =
+               prio =
                  (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
-       else
-               p->prio = --least_priority;
-       p->swap_map = swap_map;
-       p->flags |= SWP_WRITEOK;
-       nr_swap_pages += p->pages;
-       total_swap_pages += p->pages;
-
-       /* insert swap space into swap_list: */
-       prev = -1;
-       for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
-               if (p->prio >= swap_info[i]->prio)
-                       break;
-               prev = i;
-       }
-       p->next = i;
-       if (prev < 0)
-               swap_list.head = swap_list.next = p->type;
-       else
-               swap_info[prev]->next = p->type;
-       spin_unlock(&swap_lock);
+       enable_swap_info(p, prio, swap_map);
 
        printk(KERN_INFO "Adding %uk swap on %s.  "
                        "Priority:%d extents:%d across:%lluk %s%s\n",
@@ -2186,8 +2151,10 @@ bad_swap:
        spin_unlock(&swap_lock);
        vfree(swap_map);
        if (swap_file) {
-               if (inode && S_ISREG(inode->i_mode))
+               if (inode && S_ISREG(inode->i_mode)) {
                        mutex_unlock(&inode->i_mutex);
+                       inode = NULL;
+               }
                filp_close(swap_file, NULL);
        }
 out: