serial: PL011: clear pending interrupts
[linux-flexiantxendom0.git] / mm / swapfile.c
index a0f39f9..8bbb364 100644 (file)
 #include <linux/vmalloc.h>
 #include <linux/pagemap.h>
 #include <linux/namei.h>
-#include <linux/shm.h>
+#include <linux/shmem_fs.h>
 #include <linux/blkdev.h>
 #include <linux/random.h>
 #include <linux/writeback.h>
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
 #include <linux/init.h>
-#include <linux/module.h>
 #include <linux/ksm.h>
 #include <linux/rmap.h>
 #include <linux/security.h>
@@ -31,6 +30,7 @@
 #include <linux/syscalls.h>
 #include <linux/memcontrol.h>
 #include <linux/poll.h>
+#include <linux/oom.h>
 
 #include <asm/pgtable.h>
 #include <asm/tlbflush.h>
@@ -95,39 +95,6 @@ __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
 }
 
 /*
- * 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;
@@ -594,6 +561,12 @@ static unsigned char swap_entry_free(struct swap_info_struct *p,
                        disk->fops->swap_slot_free_notify(p->bdev, offset);
        }
 
+       if (!swap_count(count)) {
+               mem_cgroup_uncharge_swap(entry);
+                       if (p->notify_swap_entry_free_fn)
+                               p->notify_swap_entry_free_fn(offset);
+       }
+
        return usage;
 }
 
@@ -880,7 +853,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;
@@ -964,9 +937,7 @@ static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
        pmd = pmd_offset(pud, addr);
        do {
                next = pmd_addr_end(addr, end);
-               if (unlikely(pmd_trans_huge(*pmd)))
-                       continue;
-               if (pmd_none_or_clear_bad(pmd))
+               if (pmd_none_or_trans_huge_or_clear_bad(pmd))
                        continue;
                ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
                if (ret)
@@ -1550,6 +1521,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;
@@ -1558,6 +1559,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
        struct address_space *mapping;
        struct inode *inode;
        char *pathname;
+       int oom_score_adj;
        int i, type, prev;
        int err;
 
@@ -1616,37 +1618,22 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
        p->flags &= ~SWP_WRITEOK;
        spin_unlock(&swap_lock);
 
-       current->flags |= PF_OOM_ORIGIN;
+       oom_score_adj = test_set_oom_score_adj(OOM_SCORE_ADJ_MAX);
        err = try_to_unuse(type);
-       current->flags &= ~PF_OOM_ORIGIN;
+       compare_swap_oom_score_adj(OOM_SCORE_ADJ_MAX, oom_score_adj);
 
        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);
@@ -1697,19 +1684,14 @@ out:
 }
 
 #ifdef CONFIG_PROC_FS
-struct proc_swaps {
-       struct seq_file seq;
-       int event;
-};
-
 static unsigned swaps_poll(struct file *file, poll_table *wait)
 {
-       struct proc_swaps *s = file->private_data;
+       struct seq_file *seq = file->private_data;
 
        poll_wait(file, &proc_poll_wait, wait);
 
-       if (s->event != atomic_read(&proc_poll_event)) {
-               s->event = atomic_read(&proc_poll_event);
+       if (seq->poll_event != atomic_read(&proc_poll_event)) {
+               seq->poll_event = atomic_read(&proc_poll_event);
                return POLLIN | POLLRDNORM | POLLERR | POLLPRI;
        }
 
@@ -1799,24 +1781,16 @@ static const struct seq_operations swaps_op = {
 
 static int swaps_open(struct inode *inode, struct file *file)
 {
-       struct proc_swaps *s;
+       struct seq_file *seq;
        int ret;
 
-       s = kmalloc(sizeof(struct proc_swaps), GFP_KERNEL);
-       if (!s)
-               return -ENOMEM;
-
-       file->private_data = s;
-
        ret = seq_open(file, &swaps_op);
-       if (ret) {
-               kfree(s);
+       if (ret)
                return ret;
-       }
 
-       s->seq.private = s;
-       s->event = atomic_read(&proc_poll_event);
-       return ret;
+       seq = file->private_data;
+       seq->poll_event = atomic_read(&proc_poll_event);
+       return 0;
 }
 
 static const struct file_operations proc_swaps_operations = {
@@ -1953,20 +1927,24 @@ static unsigned long read_swap_header(struct swap_info_struct *p,
 
        /*
         * Find out how many pages are allowed for a single swap
-        * device. There are two limiting factors: 1) the number of
-        * bits for the swap offset in the swp_entry_t type and
-        * 2) the number of bits in the a swap pte as defined by
-        * the different architectures. In order to find the
-        * largest possible bit mask a swap entry with swap type 0
+        * device. There are three limiting factors: 1) the number
+        * of bits for the swap offset in the swp_entry_t type, and
+        * 2) the number of bits in the swap pte as defined by the
+        * the different architectures, and 3) the number of free bits
+        * in an exceptional radix_tree entry. In order to find the
+        * largest possible bit mask, a swap entry with swap type 0
         * and swap offset ~0UL is created, encoded to a swap pte,
-        * decoded to a swp_entry_t again and finally the swap
+        * decoded to a swp_entry_t again, and finally the swap
         * offset is extracted. This will mask all the bits from
         * the initial ~0UL mask that can't be encoded in either
         * the swp_entry_t or the architecture definition of a
-        * swap pte.
+        * swap pte.  Then the same is done for a radix_tree entry.
         */
        maxpages = swp_offset(pte_to_swp_entry(
-                       swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
+                       swp_entry_to_pte(swp_entry(0, ~0UL))));
+       maxpages = swp_offset(radix_to_swp_entry(
+                       swp_to_radix_entry(swp_entry(0, maxpages)))) + 1;
+
        if (maxpages > swap_header->info.last_page) {
                maxpages = swap_header->info.last_page + 1;
                /* p->max is an unsigned int: don't overflow it */
@@ -1998,7 +1976,6 @@ static int setup_swap_map_and_extents(struct swap_info_struct *p,
                                        sector_t *span)
 {
        int i;
-       int error;
        unsigned int nr_good_pages;
        int nr_extents;
 
@@ -2006,10 +1983,8 @@ static int setup_swap_map_and_extents(struct swap_info_struct *p,
 
        for (i = 0; i < swap_header->info.nr_badpages; i++) {
                unsigned int page_nr = swap_header->info.badpages[i];
-               if (page_nr == 0 || page_nr > swap_header->info.last_page) {
-                       error = -EINVAL;
-                       goto bad_swap;
-               }
+               if (page_nr == 0 || page_nr > swap_header->info.last_page)
+                       return -EINVAL;
                if (page_nr < maxpages) {
                        swap_map[page_nr] = SWAP_MAP_BAD;
                        nr_good_pages--;
@@ -2021,22 +1996,16 @@ static int setup_swap_map_and_extents(struct swap_info_struct *p,
                p->max = maxpages;
                p->pages = nr_good_pages;
                nr_extents = setup_swap_extents(p, span);
-               if (nr_extents < 0) {
-                       error = nr_extents;
-                       goto bad_swap;
-               }
+               if (nr_extents < 0)
+                       return nr_extents;
                nr_good_pages = p->pages;
        }
        if (!nr_good_pages) {
                printk(KERN_WARNING "Empty swap-file\n");
-               error = -EINVAL;
-               goto bad_swap;
+               return -EINVAL;
        }
 
        return nr_extents;
-
-bad_swap:
-       return error;
 }
 
 SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
@@ -2045,10 +2014,10 @@ 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;
-       unsigned int nr_good_pages;
        int nr_extents;
        sector_t span;
        unsigned long maxpages;
@@ -2078,7 +2047,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];
@@ -2091,6 +2059,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;
@@ -2132,7 +2102,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                error = nr_extents;
                goto bad_swap;
        }
-       nr_good_pages = p->pages;
 
        if (p->bdev) {
                if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
@@ -2144,37 +2113,19 @@ 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 += nr_good_pages;
-       total_swap_pages += nr_good_pages;
+       enable_swap_info(p, prio, swap_map);
 
        printk(KERN_INFO "Adding %uk swap on %s.  "
                        "Priority:%d extents:%d across:%lluk %s%s\n",
-               nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
+               p->pages<<(PAGE_SHIFT-10), name, p->prio,
                nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
                (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
                (p->flags & SWP_DISCARDABLE) ? "D" : "");
 
-       /* 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);
        mutex_unlock(&swapon_mutex);
        atomic_inc(&proc_poll_event);
        wake_up_interruptible(&proc_poll_wait);
@@ -2196,8 +2147,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:
@@ -2340,6 +2293,23 @@ int swapcache_prepare(swp_entry_t entry)
 }
 
 /*
+ * Sets callback for event when swap_map[offset] == 0
+ * i.e. page at this swap offset is not longer used.
+ *
+ * type: identifies swap file
+ * fn: callback function
+ */
+void set_notify_swap_entry_free(unsigned type, void (*fn) (unsigned long))
+{
+       struct swap_info_struct *sis;
+       sis = swap_info[type];
+       BUG_ON(!sis);
+       sis->notify_swap_entry_free_fn = fn;
+       return;
+}
+EXPORT_SYMBOL(set_notify_swap_entry_free);
+
+/*
  * swap_lock prevents swap_map being freed. Don't grab an extra
  * reference on the swaphandle, it doesn't matter if it becomes unused.
  */