cgroup: fix cpuset permission check

    The permission check code added by "cgroup: Add generic cgroup subsystem
    permission checks" does not build and also leaves a variable uninitialized.
    Fix it.

    Change-Id: I8009d6315527725d45b20792b7cd74aedc9bff44
    Signed-off-by: Rabin Vincent <rabin.vincent@stericsson.com>
    Signed-off-by: Christian Bejram <christian.bejram@stericsson.com>
PM tweaks and a shit ton for autogroup and cgroups
memcg: fix memory migration of shmem swapcache

In the current implementation mem_cgroup_end_migration() decides whether
the page migration has succeeded or not by checking "oldpage->mapping".

But if we are tring to migrate a shmem swapcache, the page->mapping of it
is NULL from the begining, so the check would be invalid.  As a result,
mem_cgroup_end_migration() assumes the migration has succeeded even if
it's not, so "newpage" would be freed while it's not uncharged.

This patch fixes it by passing mem_cgroup_end_migration() the result of
the page migration.

Signed-off-by: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp>
Reviewed-by: Minchan Kim <minchan.kim@gmail.com>
Acked-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Acked-by: Balbir Singh <balbir@linux.vnet.ibm.com>
Cc: Minchan Kim <minchan.kim@gmail.com>
Reviewed-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
vmscan: zone_reclaim don't call disable_swap_token()

Swap token don't works when zone reclaim is enabled since it was born.
Because __zone_reclaim() always call disable_swap_token() unconditionally.

This kill swap token feature completely.  As far as I know, nobody want to
that.  Remove it.

Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Acked-by: Rik van Riel <riel@redhat.com>
Reviewed-by: Minchan Kim <minchan.kim@gmail.com>
Cc: Christoph Lameter <cl@linux-foundation.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
vmscan: recalculate lru_pages on each priority

shrink_zones() need relatively long time and lru_pages can change
dramatically during shrink_zones().  So lru_pages should be recalculated
for each priority.

Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Acked-by: Rik van Riel <riel@redhat.com>
Reviewed-by: Minchan Kim <minchan.kim@gmail.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
mm: From CodeAurora:

mm: unify module_alloc code for vmalloc

    Four architectures (arm, mips, sparc, x86) use __vmalloc_area() for
    module_init().  Much of the code is duplicated and can be generalized in a
    globally accessible function, __vmalloc_node_range().

    __vmalloc_node() now calls into __vmalloc_node_range() with a range of
    [VMALLOC_START, VMALLOC_END) for functionally equivalent behavior.

    Each architecture may then use __vmalloc_node_range() directly to remove
    the duplication of code.

    Signed-off-by: David Rientjes <rientjes@google.com>
    Cc: Christoph Lameter <cl@linux.com>
    Cc: Russell King <linux@arm.linux.org.uk>
    Cc: Ralf Baechle <ralf@linux-mips.org>
    Cc: "David S. Miller" <davem@davemloft.net>
    Cc: Ingo Molnar <mingo@redhat.com>
    Cc: "H. Peter Anvin" <hpa@zytor.com>
    Cc: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

mm: clear PageError bit in msync & fsync

    Temporary IO failures, eg.  due to loss of both multipath paths, can
    permanently leave the PageError bit set on a page, resulting in msync or
    fsync returning -EIO over and over again, even if IO is now getting to the
    disk correctly.

    We already clear the AS_ENOSPC and AS_IO bits in mapping->flags in the
    filemap_fdatawait_range function.  Also clearing the PageError bit on the
    page allows subsequent msync or fsync calls on this file to return without
    an error, if the subsequent IO succeeds.

    Unfortunately data written out in the msync or fsync call that returned
    -EIO can still get lost, because the page dirty bit appears to not get
    restored on IO error.  However, the alternative could be potentially all
    of memory filling up with uncleanable dirty pages, hanging the system, so
    there is no nice choice here...

    Signed-off-by: Rik van Riel <riel@redhat.com>
    Acked-by: Valerie Aurora <vaurora@redhat.com>
    Acked-by: Jeff Layton <jlayton@redhat.com>
    Cc: Theodore Ts'o <tytso@mit.edu>
    Acked-by: Jan Kara <jack@suse.cz>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

mm: set correct numa_zonelist_order string when configured on the kernel command line

    When numa_zonelist_order parameter is set to "node" or "zone" on the
    command line it's still showing as "default" in sysctl.  That's because
    early_param parsing function changes only user_zonelist_order variable.
    Fix this by copying user-provided string to numa_zonelist_order if it was
    successfully parsed.

    Signed-off-by: Volodymyr G Lukiianyk <volodymyrgl@gmail.com>
    Acked-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

mm: clear pages_scanned only if draining a pcp adds pages to the buddy allocator

    Commit 0e093d99763e ("writeback: do not sleep on the congestion queue if
    there are no congested BDIs or if significant congestion is not being
    encountered in the current zone") uncovered a livelock in the page
    allocator that resulted in tasks infinitely looping trying to find
    memory and kswapd running at 100% cpu.

    The issue occurs because drain_all_pages() is called immediately
    following direct reclaim when no memory is freed and try_to_free_pages()
    returns non-zero because all zones in the zonelist do not have their
    all_unreclaimable flag set.

    When draining the per-cpu pagesets back to the buddy allocator for each
    zone, the zone->pages_scanned counter is cleared to avoid erroneously
    setting zone->all_unreclaimable later.  The problem is that no pages may
    actually be drained and, thus, the unreclaimable logic never fails
    direct reclaim so the oom killer may be invoked.

    This apparently only manifested after wait_iff_congested() was
    introduced and the zone was full of anonymous memory that would not
    congest the backing store.  The page allocator would infinitely loop if
    there were no other tasks waiting to be scheduled and clear
    zone->pages_scanned because of drain_all_pages() as the result of this
    change before kswapd could scan enough pages to trigger the reclaim
    logic.  Additionally, with every loop of the page allocator and in the
    reclaim path, kswapd would be kicked and would end up running at 100%
    cpu.  In this scenario, current and kswapd are all running continuously
    with kswapd incrementing zone->pages_scanned and current clearing it.

    The problem is even more pronounced when current swaps some of its
    memory to swap cache and the reclaimable logic then considers all active
    anonymous memory in the all_unreclaimable logic, which requires a much
    higher zone->pages_scanned value for try_to_free_pages() to return zero
    that is never attainable in this scenario.

    Before wait_iff_congested(), the page allocator would incur an
    unconditional timeout and allow kswapd to elevate zone->pages_scanned to
    a level that the oom killer would be called the next time it loops.

    The fix is to only attempt to drain pcp pages if there is actually a
    quantity to be drained.  The unconditional clearing of
    zone->pages_scanned in free_pcppages_bulk() need not be changed since
    other callers already ensure that draining will occur.  This patch
    ensures that free_pcppages_bulk() will actually free memory before
    calling into it from drain_all_pages() so zone->pages_scanned is only
    cleared if appropriate.

    Signed-off-by: David Rientjes <rientjes@google.com>
    Cc: Mel Gorman <mel@csn.ul.ie>
    Reviewed-by: Johannes Weiner <hannes@cmpxchg.org>
    Cc: Minchan Kim <minchan.kim@gmail.com>
    Cc: Wu Fengguang <fengguang.wu@intel.com>
    Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
    Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
    Reviewed-by: Rik van Riel <riel@redhat.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

mm: <asm-generic/pgtable.h> must include <linux/mm_types.h>

    Commit e2cda3226481 ("thp: add pmd mangling generic functions") replaced
    some macros in <asm-generic/pgtable.h> with inline functions.

    If the functions are to be defined (not all architectures need them)
    then struct vm_area_struct must be defined first.  So include
    <linux/mm_types.h>.

    Fixes a build failure seen in Debian:

        CC [M]  drivers/media/dvb/mantis/mantis_pci.o
      In file included from arch/arm/include/asm/pgtable.h:460,
                       from drivers/media/dvb/mantis/mantis_pci.c:25:
      include/asm-generic/pgtable.h: In function 'ptep_test_and_clear_young':
      include/asm-generic/pgtable.h:29: error: dereferencing pointer to incomplete type

    Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

mm: use alloc_bootmem_node_nopanic() on really needed path

    commit 8f389a99b652aab5b42297280bd94d95933ad12f upstream.

    Stefan found nobootmem does not work on his system that has only 8M of
    RAM.  This causes an early panic:

      BIOS-provided physical RAM map:
       BIOS-88: 0000000000000000 - 000000000009f000 (usable)
       BIOS-88: 0000000000100000 - 0000000000840000 (usable)
      bootconsole [earlyser0] enabled
      Notice: NX (Execute Disable) protection missing in CPU or disabled in BIOS!
      DMI not present or invalid.
      last_pfn = 0x840 max_arch_pfn = 0x100000
      init_memory_mapping: 0000000000000000-0000000000840000
      8MB LOWMEM available.
        mapped low ram: 0 - 00840000
        low ram: 0 - 00840000
      Zone PFN ranges:
        DMA      0x00000001 -> 0x00001000
        Normal   empty
      Movable zone start PFN for each node
      early_node_map[2] active PFN ranges
          0: 0x00000001 -> 0x0000009f
          0: 0x00000100 -> 0x00000840
      BUG: Int 6: CR2 (null)
           EDI c034663c  ESI (null)  EBP c0329f38  ESP c0329ef4
           EBX c0346380  EDX 00000006  ECX ffffffff  EAX fffffff4
           err (null)  EIP c0353191   CS c0320060  flg 00010082
      Stack: (null) c030c533 000007cd (null) c030c533 00000001 (null) (null)
             00000003 0000083f 00000018 00000002 00000002 c0329f6c c03534d6 (null)
             (null) 00000100 00000840 (null) c0329f64 00000001 00001000 (null)
      Pid: 0, comm: swapper Not tainted 2.6.36 #5
      Call Trace:
       [<c02e3707>] ? 0xc02e3707
       [<c035e6e5>] 0xc035e6e5
       [<c0353191>] ? 0xc0353191
       [<c03534d6>] 0xc03534d6
       [<c034f1cd>] 0xc034f1cd
       [<c034a824>] 0xc034a824
       [<c03513cb>] ? 0xc03513cb
       [<c0349432>] 0xc0349432
       [<c0349066>] 0xc0349066

    It turns out that we should ignore the low limit of 16M.

    Use alloc_bootmem_node_nopanic() in this case.

    [akpm@linux-foundation.org: less mess]
    Signed-off-by: Yinghai LU <yinghai@kernel.org>
    Reported-by: Stefan Hellermann <stefan@the2masters.de>
    Tested-by: Stefan Hellermann <stefan@the2masters.de>
    Cc: Ingo Molnar <mingo@elte.hu>
    Cc: "H. Peter Anvin" <hpa@linux.intel.com>
    Cc: Thomas Gleixner <tglx@linutronix.de>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
    Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

mm: vmscan: correctly check if reclaimer should schedule during shrink_slab

    commit f06590bd718ed950c98828e30ef93204028f3210 upstream.

    It has been reported on some laptops that kswapd is consuming large
    amounts of CPU and not being scheduled when SLUB is enabled during large
    amounts of file copying.  It is expected that this is due to kswapd
    missing every cond_resched() point because;

    shrink_page_list() calls cond_resched() if inactive pages were isolated
            which in turn may not happen if all_unreclaimable is set in
            shrink_zones(). If for whatver reason, all_unreclaimable is
            set on all zones, we can miss calling cond_resched().

    balance_pgdat() only calls cond_resched if the zones are not
            balanced. For a high-order allocation that is balanced, it
            checks order-0 again. During that window, order-0 might have
            become unbalanced so it loops again for order-0 and returns
            that it was reclaiming for order-0 to kswapd(). It can then
            find that a caller has rewoken kswapd for a high-order and
            re-enters balance_pgdat() without ever calling cond_resched().

    shrink_slab only calls cond_resched() if we are reclaiming slab
    	pages. If there are a large number of direct reclaimers, the
    	shrinker_rwsem can be contended and prevent kswapd calling
    	cond_resched().

    This patch modifies the shrink_slab() case.  If the semaphore is
    contended, the caller will still check cond_resched().  After each
    successful call into a shrinker, the check for cond_resched() remains in
    case one shrinker is particularly slow.

    [mgorman@suse.de: preserve call to cond_resched after each call into shrinker]
    Signed-off-by: Mel Gorman <mgorman@suse.de>
    Signed-off-by: Minchan Kim <minchan.kim@gmail.com>
    Cc: Rik van Riel <riel@redhat.com>
    Cc: Johannes Weiner <hannes@cmpxchg.org>
    Cc: Wu Fengguang <fengguang.wu@intel.com>
    Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
    Tested-by: Colin King <colin.king@canonical.com>
    Cc: Raghavendra D Prabhu <raghu.prabhu13@gmail.com>
    Cc: Jan Kara <jack@suse.cz>
    Cc: Chris Mason <chris.mason@oracle.com>
    Cc: Christoph Lameter <cl@linux.com>
    Cc: Pekka Enberg <penberg@kernel.org>
    Cc: Rik van Riel <riel@redhat.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
    Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

memcg: more mem_cgroup_uncharge() batching

    It seems odd that truncate_inode_pages_range(), called not only when
    truncating but also when evicting inodes, has mem_cgroup_uncharge_start
    and _end() batching in its second loop to clear up a few leftovers, but
    not in its first loop that does almost all the work: add them there too.

    Signed-off-by: Hugh Dickins <hughd@google.com>
    Acked-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
    Acked-by: Balbir Singh <balbir@linux.vnet.ibm.com>
    Acked-by: Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

mm/migrate.c: fix compilation error

    GCC complained about update_mmu_cache() not being defined in migrate.c.
    Including <asm/tlbflush.h> seems to solve the problem.

    Signed-off-by: Michal Nazarewicz <m.nazarewicz@samsung.com>
    Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
RTC: Release mutex in error path of rtc_alarm_irq_enable

    On hardware that doesn't support alarm interrupts, rtc_alarm_irq_enable

    could return without releasing the ops_lock mutex.

    This was introduced in

        aa0be0f (RTC: Propagate error handling via rtc_timer_enqueue properly)

    This patch corrects the issue by only returning once the mutex is

    released.

    [john.stultz: Reworded the commit log]
rtc alarm: fix bad index when canceling alarms[]

(backport from common android-3.0
commit: 692e468137ebb2a31431c4b3fad1dca0a2da7659)

It was using ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP_MASK as an
index.

Change-Id: I919860cc71254453e382616bce9fd5455802cb3d
Signed-off-by: JP Abgrall <jpa@google.com>
PM / Suspend: Fix ordering of calls in suspend error paths

    The ACPI suspend code calls suspend_nvs_free() at a wrong place,
    which may lead to a memory leak if there's an error executing
    acpi_pm_prepare(), because acpi_pm_finish() will not be called in
    that case.  However, the root cause of this problem is the
    apparently confusing ordering of calls in suspend error paths that
    needs to be fixed.

    In addition to that, fix a typo in a label name in suspend.c.

    Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
    Acked-by: Len Brown <len.brown@intel.com>