ALSA: usb-audio: support partially write-protected UAC2 controls
[safe/jmp/linux-2.6] / mm / swapfile.c
index e74112e..03aa2d5 100644 (file)
@@ -22,6 +22,7 @@
 #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>
 #include <linux/backing-dev.h>
@@ -138,7 +139,8 @@ static int discard_swap(struct swap_info_struct *si)
        nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
        if (nr_blocks) {
                err = blkdev_issue_discard(si->bdev, start_block,
-                               nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
+                               nr_blocks, GFP_KERNEL,
+                               BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
                if (err)
                        return err;
                cond_resched();
@@ -149,7 +151,8 @@ static int discard_swap(struct swap_info_struct *si)
                nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
 
                err = blkdev_issue_discard(si->bdev, start_block,
-                               nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
+                               nr_blocks, GFP_KERNEL,
+                               BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
                if (err)
                        break;
 
@@ -188,7 +191,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, DISCARD_FL_BARRIER))
+                                   nr_blocks, GFP_NOIO, BLKDEV_IFL_WAIT |
+                                                       BLKDEV_IFL_BARRIER))
                                break;
                }
 
@@ -573,6 +577,7 @@ static unsigned char swap_entry_free(struct swap_info_struct *p,
 
        /* free if no reference */
        if (!usage) {
+               struct gendisk *disk = p->bdev->bd_disk;
                if (offset < p->lowest_bit)
                        p->lowest_bit = offset;
                if (offset > p->highest_bit)
@@ -582,6 +587,9 @@ static unsigned char swap_entry_free(struct swap_info_struct *p,
                        swap_list.next = p->type;
                nr_swap_pages++;
                p->inuse_pages--;
+               if ((p->flags & SWP_BLKDEV) &&
+                               disk->fops->swap_slot_free_notify)
+                       disk->fops->swap_slot_free_notify(p->bdev, offset);
        }
 
        return usage;
@@ -650,6 +658,8 @@ int reuse_swap_page(struct page *page)
        int count;
 
        VM_BUG_ON(!PageLocked(page));
+       if (unlikely(PageKsm(page)))
+               return 0;
        count = page_mapcount(page);
        if (count <= 1 && PageSwapCache(page)) {
                count += page_swapcount(page);
@@ -658,7 +668,7 @@ int reuse_swap_page(struct page *page)
                        SetPageDirty(page);
                }
        }
-       return count == 1;
+       return count <= 1;
 }
 
 /*
@@ -720,6 +730,37 @@ int free_swap_and_cache(swp_entry_t entry)
        return p != NULL;
 }
 
+#ifdef CONFIG_CGROUP_MEM_RES_CTLR
+/**
+ * mem_cgroup_count_swap_user - count the user of a swap entry
+ * @ent: the swap entry to be checked
+ * @pagep: the pointer for the swap cache page of the entry to be stored
+ *
+ * Returns the number of the user of the swap entry. The number is valid only
+ * for swaps of anonymous pages.
+ * If the entry is found on swap cache, the page is stored to pagep with
+ * refcount of it being incremented.
+ */
+int mem_cgroup_count_swap_user(swp_entry_t ent, struct page **pagep)
+{
+       struct page *page;
+       struct swap_info_struct *p;
+       int count = 0;
+
+       page = find_get_page(&swapper_space, ent.val);
+       if (page)
+               count += page_mapcount(page);
+       p = swap_info_get(ent);
+       if (p) {
+               count += swap_count(p->swap_map[swp_offset(ent)]);
+               spin_unlock(&swap_lock);
+       }
+
+       *pagep = page;
+       return count;
+}
+#endif
+
 #ifdef CONFIG_HIBERNATION
 /*
  * Find the swap type that corresponds to given device (if any).
@@ -837,7 +878,8 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
                goto out;
        }
 
-       inc_mm_counter(vma->vm_mm, anon_rss);
+       dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
+       inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
        get_page(page);
        set_pte_at(vma->vm_mm, addr, pte,
                   pte_mkold(mk_pte(page, vma->vm_page_prot)));
@@ -1185,6 +1227,12 @@ static int try_to_unuse(unsigned int type)
                 * read from disk into another page.  Splitting into two
                 * pages would be incorrect if swap supported "shared
                 * private" pages, but they are handled by tmpfs files.
+                *
+                * Given how unuse_vma() targets one particular offset
+                * in an anon_vma, once the anon_vma has been determined,
+                * this splitting happens to be just what is needed to
+                * handle where KSM pages have been swapped out: re-reading
+                * is unnecessarily slow, but we can fix that later on.
                 */
                if (swap_count(*swap_map) &&
                     PageDirty(page) && PageSwapCache(page)) {
@@ -1750,11 +1798,11 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
        unsigned int type;
        int i, prev;
        int error;
-       union swap_header *swap_header = NULL;
-       unsigned int nr_good_pages = 0;
+       union swap_header *swap_header;
+       unsigned int nr_good_pages;
        int nr_extents = 0;
        sector_t span;
-       unsigned long maxpages = 1;
+       unsigned long maxpages;
        unsigned long swapfilepages;
        unsigned char *swap_map = NULL;
        struct page *page = NULL;
@@ -1843,6 +1891,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
                if (error < 0)
                        goto bad_swap;
                p->bdev = bdev;
+               p->flags |= SWP_BLKDEV;
        } else if (S_ISREG(inode->i_mode)) {
                p->bdev = inode->i_sb->s_bdev;
                mutex_lock(&inode->i_mutex);
@@ -1913,9 +1962,13 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
         * swap pte.
         */
        maxpages = swp_offset(pte_to_swp_entry(
-                       swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1;
-       if (maxpages > swap_header->info.last_page)
-               maxpages = swap_header->info.last_page;
+                       swp_entry_to_pte(swp_entry(0, ~0UL)))) + 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 */
+               if ((unsigned int)maxpages == 0)
+                       maxpages = UINT_MAX;
+       }
        p->highest_bit = maxpages - 1;
 
        error = -EINVAL;
@@ -1939,23 +1992,24 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
        }
 
        memset(swap_map, 0, maxpages);
+       nr_good_pages = maxpages - 1;   /* omit header page */
+
        for (i = 0; i < swap_header->info.nr_badpages; i++) {
-               int page_nr = swap_header->info.badpages[i];
-               if (page_nr <= 0 || page_nr >= swap_header->info.last_page) {
+               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;
                }
-               swap_map[page_nr] = SWAP_MAP_BAD;
+               if (page_nr < maxpages) {
+                       swap_map[page_nr] = SWAP_MAP_BAD;
+                       nr_good_pages--;
+               }
        }
 
        error = swap_cgroup_swapon(type, maxpages);
        if (error)
                goto bad_swap;
 
-       nr_good_pages = swap_header->info.last_page -
-                       swap_header->info.nr_badpages -
-                       1 /* header page */;
-
        if (nr_good_pages) {
                swap_map[0] = SWAP_MAP_BAD;
                p->max = maxpages;
@@ -2146,7 +2200,11 @@ void swap_shmem_alloc(swp_entry_t entry)
 }
 
 /*
- * increase reference count of swap entry by 1.
+ * Increase reference count of swap entry by 1.
+ * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
+ * but could not be atomically allocated.  Returns 0, just as if it succeeded,
+ * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
+ * might occur if a page table entry has got corrupted.
  */
 int swap_duplicate(swp_entry_t entry)
 {