block: add QUEUE_FLAG_NOWAIT
[linux-2.6-microblaze.git] / mm / swap_state.c
index e82f4f8..c16eebb 100644 (file)
@@ -57,8 +57,8 @@ static bool enable_vma_readahead __read_mostly = true;
 #define GET_SWAP_RA_VAL(vma)                                   \
        (atomic_long_read(&(vma)->swap_readahead_info) ? : 4)
 
-#define INC_CACHE_INFO(x)      do { swap_cache_info.x++; } while (0)
-#define ADD_CACHE_INFO(x, nr)  do { swap_cache_info.x += (nr); } while (0)
+#define INC_CACHE_INFO(x)      data_race(swap_cache_info.x++)
+#define ADD_CACHE_INFO(x, nr)  data_race(swap_cache_info.x += (nr))
 
 static struct {
        unsigned long add_total;
@@ -106,16 +106,32 @@ void show_swap_cache_info(void)
        printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
 }
 
+void *get_shadow_from_swap_cache(swp_entry_t entry)
+{
+       struct address_space *address_space = swap_address_space(entry);
+       pgoff_t idx = swp_offset(entry);
+       struct page *page;
+
+       page = find_get_entry(address_space, idx);
+       if (xa_is_value(page))
+               return page;
+       if (page)
+               put_page(page);
+       return NULL;
+}
+
 /*
  * add_to_swap_cache resembles add_to_page_cache_locked on swapper_space,
  * but sets SwapCache flag and private instead of mapping and index.
  */
-int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp)
+int add_to_swap_cache(struct page *page, swp_entry_t entry,
+                       gfp_t gfp, void **shadowp)
 {
        struct address_space *address_space = swap_address_space(entry);
        pgoff_t idx = swp_offset(entry);
        XA_STATE_ORDER(xas, &address_space->i_pages, idx, compound_order(page));
-       unsigned long i, nr = hpage_nr_pages(page);
+       unsigned long i, nr = thp_nr_pages(page);
+       void *old;
 
        VM_BUG_ON_PAGE(!PageLocked(page), page);
        VM_BUG_ON_PAGE(PageSwapCache(page), page);
@@ -125,16 +141,25 @@ int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp)
        SetPageSwapCache(page);
 
        do {
+               unsigned long nr_shadows = 0;
+
                xas_lock_irq(&xas);
                xas_create_range(&xas);
                if (xas_error(&xas))
                        goto unlock;
                for (i = 0; i < nr; i++) {
                        VM_BUG_ON_PAGE(xas.xa_index != idx + i, page);
+                       old = xas_load(&xas);
+                       if (xa_is_value(old)) {
+                               nr_shadows++;
+                               if (shadowp)
+                                       *shadowp = old;
+                       }
                        set_page_private(page + i, entry.val + i);
                        xas_store(&xas, page);
                        xas_next(&xas);
                }
+               address_space->nrexceptional -= nr_shadows;
                address_space->nrpages += nr;
                __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
                ADD_CACHE_INFO(add_total, nr);
@@ -154,10 +179,11 @@ unlock:
  * This must be called only on pages that have
  * been verified to be in the swap cache.
  */
-void __delete_from_swap_cache(struct page *page, swp_entry_t entry)
+void __delete_from_swap_cache(struct page *page,
+                       swp_entry_t entry, void *shadow)
 {
        struct address_space *address_space = swap_address_space(entry);
-       int i, nr = hpage_nr_pages(page);
+       int i, nr = thp_nr_pages(page);
        pgoff_t idx = swp_offset(entry);
        XA_STATE(xas, &address_space->i_pages, idx);
 
@@ -166,12 +192,14 @@ void __delete_from_swap_cache(struct page *page, swp_entry_t entry)
        VM_BUG_ON_PAGE(PageWriteback(page), page);
 
        for (i = 0; i < nr; i++) {
-               void *entry = xas_store(&xas, NULL);
+               void *entry = xas_store(&xas, shadow);
                VM_BUG_ON_PAGE(entry != page, entry);
                set_page_private(page + i, 0);
                xas_next(&xas);
        }
        ClearPageSwapCache(page);
+       if (shadow)
+               address_space->nrexceptional += nr;
        address_space->nrpages -= nr;
        __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, -nr);
        ADD_CACHE_INFO(del_total, nr);
@@ -208,7 +236,7 @@ int add_to_swap(struct page *page)
         * Add it to the swap cache.
         */
        err = add_to_swap_cache(page, entry,
-                       __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN);
+                       __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN, NULL);
        if (err)
                /*
                 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
@@ -246,11 +274,42 @@ void delete_from_swap_cache(struct page *page)
        struct address_space *address_space = swap_address_space(entry);
 
        xa_lock_irq(&address_space->i_pages);
-       __delete_from_swap_cache(page, entry);
+       __delete_from_swap_cache(page, entry, NULL);
        xa_unlock_irq(&address_space->i_pages);
 
        put_swap_page(page, entry);
-       page_ref_sub(page, hpage_nr_pages(page));
+       page_ref_sub(page, thp_nr_pages(page));
+}
+
+void clear_shadow_from_swap_cache(int type, unsigned long begin,
+                               unsigned long end)
+{
+       unsigned long curr = begin;
+       void *old;
+
+       for (;;) {
+               unsigned long nr_shadows = 0;
+               swp_entry_t entry = swp_entry(type, curr);
+               struct address_space *address_space = swap_address_space(entry);
+               XA_STATE(xas, &address_space->i_pages, curr);
+
+               xa_lock_irq(&address_space->i_pages);
+               xas_for_each(&xas, old, end) {
+                       if (!xa_is_value(old))
+                               continue;
+                       xas_store(&xas, NULL);
+                       nr_shadows++;
+               }
+               address_space->nrexceptional -= nr_shadows;
+               xa_unlock_irq(&address_space->i_pages);
+
+               /* search the next swapcache until we meet end */
+               curr >>= SWAP_ADDRESS_SPACE_SHIFT;
+               curr++;
+               curr <<= SWAP_ADDRESS_SPACE_SHIFT;
+               if (curr > end)
+                       break;
+       }
 }
 
 /* 
@@ -361,6 +420,7 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
 {
        struct swap_info_struct *si;
        struct page *page;
+       void *shadow = NULL;
 
        *new_page_allocated = false;
 
@@ -429,7 +489,7 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
        __SetPageSwapBacked(page);
 
        /* May fail (-ENOMEM) if XArray node allocation failed. */
-       if (add_to_swap_cache(page, entry, gfp_mask & GFP_RECLAIM_MASK)) {
+       if (add_to_swap_cache(page, entry, gfp_mask & GFP_RECLAIM_MASK, &shadow)) {
                put_swap_page(page, entry);
                goto fail_unlock;
        }
@@ -439,10 +499,8 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
                goto fail_unlock;
        }
 
-       /* XXX: Move to lru_cache_add() when it supports new vs putback */
-       spin_lock_irq(&page_pgdat(page)->lru_lock);
-       lru_note_cost_page(page);
-       spin_unlock_irq(&page_pgdat(page)->lru_lock);
+       if (shadow)
+               workingset_refault(page, shadow);
 
        /* Caller will initiate read into locked page */
        SetPageWorkingset(page);