percpu: make embedding first chunk allocator check vmalloc space size
[safe/jmp/linux-2.6] / mm / swapfile.c
index 0d72969..4de7f02 100644 (file)
@@ -79,6 +79,32 @@ static inline unsigned short encode_swapmap(int count, bool has_cache)
        return ret;
 }
 
+/* returnes 1 if swap entry is freed */
+static int
+__try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
+{
+       int type = si - swap_info;
+       swp_entry_t entry = swp_entry(type, offset);
+       struct page *page;
+       int ret = 0;
+
+       page = find_get_page(&swapper_space, entry.val);
+       if (!page)
+               return 0;
+       /*
+        * This function is called from scan_swap_map() and it's called
+        * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
+        * We have to use trylock for avoiding deadlock. This is a special
+        * case and you should use try_to_free_swap() with explicit lock_page()
+        * in usual operations.
+        */
+       if (trylock_page(page)) {
+               ret = try_to_free_swap(page);
+               unlock_page(page);
+       }
+       page_cache_release(page);
+       return ret;
+}
 
 /*
  * We need this because the bdev->unplug_fn can sleep and we cannot
@@ -135,7 +161,8 @@ static int discard_swap(struct swap_info_struct *si)
                }
 
                err = blkdev_issue_discard(si->bdev, start_block,
-                                               nr_blocks, GFP_KERNEL);
+                                               nr_blocks, GFP_KERNEL,
+                                               DISCARD_FL_BARRIER);
                if (err)
                        break;
 
@@ -174,7 +201,8 @@ static void discard_swap_cluster(struct swap_info_struct *si,
                        start_block <<= PAGE_SHIFT - 9;
                        nr_blocks <<= PAGE_SHIFT - 9;
                        if (blkdev_issue_discard(si->bdev, start_block,
-                                                       nr_blocks, GFP_NOIO))
+                                                       nr_blocks, GFP_NOIO,
+                                                       DISCARD_FL_BARRIER))
                                break;
                }
 
@@ -301,6 +329,19 @@ checks:
                goto no_page;
        if (offset > si->highest_bit)
                scan_base = offset = si->lowest_bit;
+
+       /* reuse swap entry of cache-only swap if not busy. */
+       if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+               int swap_was_freed;
+               spin_unlock(&swap_lock);
+               swap_was_freed = __try_to_reclaim_swap(si, offset);
+               spin_lock(&swap_lock);
+               /* entry was freed successfully, try to use this again */
+               if (swap_was_freed)
+                       goto checks;
+               goto scan; /* check next one */
+       }
+
        if (si->swap_map[offset])
                goto scan;
 
@@ -382,6 +423,10 @@ scan:
                        spin_lock(&swap_lock);
                        goto checks;
                }
+               if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+                       spin_lock(&swap_lock);
+                       goto checks;
+               }
                if (unlikely(--latency_ration < 0)) {
                        cond_resched();
                        latency_ration = LATENCY_LIMIT;
@@ -393,6 +438,10 @@ scan:
                        spin_lock(&swap_lock);
                        goto checks;
                }
+               if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+                       spin_lock(&swap_lock);
+                       goto checks;
+               }
                if (unlikely(--latency_ration < 0)) {
                        cond_resched();
                        latency_ration = LATENCY_LIMIT;
@@ -536,8 +585,9 @@ static int swap_entry_free(struct swap_info_struct *p,
                        swap_list.next = p - swap_info;
                nr_swap_pages++;
                p->inuse_pages--;
-               mem_cgroup_uncharge_swap(ent);
        }
+       if (!swap_count(count))
+               mem_cgroup_uncharge_swap(ent);
        return count;
 }
 
@@ -562,12 +612,19 @@ void swap_free(swp_entry_t entry)
 void swapcache_free(swp_entry_t entry, struct page *page)
 {
        struct swap_info_struct *p;
+       int ret;
 
-       if (page)
-               mem_cgroup_uncharge_swapcache(page, entry);
        p = swap_info_get(entry);
        if (p) {
-               swap_entry_free(p, entry, SWAP_CACHE);
+               ret = swap_entry_free(p, entry, SWAP_CACHE);
+               if (page) {
+                       bool swapout;
+                       if (ret)
+                               swapout = true; /* the end of swap out */
+                       else
+                               swapout = false; /* no more swap users! */
+                       mem_cgroup_uncharge_swapcache(page, entry, swapout);
+               }
                spin_unlock(&swap_lock);
        }
        return;
@@ -642,7 +699,7 @@ int free_swap_and_cache(swp_entry_t entry)
        struct swap_info_struct *p;
        struct page *page = NULL;
 
-       if (is_migration_entry(entry))
+       if (non_swap_entry(entry))
                return 1;
 
        p = swap_info_get(entry);
@@ -698,7 +755,7 @@ int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
 
                if (!bdev) {
                        if (bdev_p)
-                               *bdev_p = bdget(sis->bdev->bd_dev);
+                               *bdev_p = bdgrab(sis->bdev);
 
                        spin_unlock(&swap_lock);
                        return i;
@@ -710,7 +767,7 @@ int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
                                        struct swap_extent, list);
                        if (se->start_block == offset) {
                                if (bdev_p)
-                                       *bdev_p = bdget(sis->bdev->bd_dev);
+                                       *bdev_p = bdgrab(sis->bdev);
 
                                spin_unlock(&swap_lock);
                                bdput(bdev);
@@ -1518,9 +1575,9 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
        p->flags &= ~SWP_WRITEOK;
        spin_unlock(&swap_lock);
 
-       current->flags |= PF_SWAPOFF;
+       current->flags |= PF_OOM_ORIGIN;
        err = try_to_unuse(type);
-       current->flags &= ~PF_SWAPOFF;
+       current->flags &= ~PF_OOM_ORIGIN;
 
        if (err) {
                /* re-insert swap space back into swap_list */
@@ -2028,7 +2085,7 @@ static int __swap_duplicate(swp_entry_t entry, bool cache)
        int count;
        bool has_cache;
 
-       if (is_migration_entry(entry))
+       if (non_swap_entry(entry))
                return -EINVAL;
 
        type = swp_type(entry);