Add security hooks to binder and implement the hooks for SELinux.

Add security hooks to the binder and implement the hooks for SELinux.
The security hooks enable security modules such as SELinux to implement
controls over binder IPC.  The security hooks include support for
controlling what process can become the binder context manager
(binder_set_context_mgr), controlling the ability of a process
to invoke a binder transaction/IPC to another process (binder_transaction),
controlling the ability a process to transfer a binder reference to
another process (binder_transfer_binder), and controlling the ability
of a process to transfer an open file to another process (binder_transfer_file).

This support is used by SE Android, http://selinuxproject.org/page/SEAndroid.

cherry-picking from android-msm-mako-3.4-clean-wip
commit 6b3d6afa523a9996d529577f66e5df7c9f2f0734

Change-Id: I9a64a87825df2e60b9c51400377af4a9cd1c4049
Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov>
Signed-off-by: Devin Kim <dojip.kim@lge.com>
mm, oom: base root bonus on current usage

A 3% of system memory bonus is sometimes too excessive in comparison to
other processes.

With commit a63d83f427fb ("oom: badness heuristic rewrite"), the OOM
killer tries to avoid killing privileged tasks by subtracting 3% of
overall memory (system or cgroup) from their per-task consumption.  But
as a result, all root tasks that consume less than 3% of overall memory
are considered equal, and so it only takes 33+ privileged tasks pushing
the system out of memory for the OOM killer to do something stupid and
kill dhclient or other root-owned processes.  For example, on a 32G
machine it can't tell the difference between the 1M agetty and the 10G
fork bomb member.

The changelog describes this 3% boost as the equivalent to the global
overcommit limit being 3% higher for privileged tasks, but this is not
the same as discounting 3% of overall memory from _every privileged task
individually_ during OOM selection.

Replace the 3% of system memory bonus with a 3% of current memory usage
bonus.

By giving root tasks a bonus that is proportional to their actual size,
they remain comparable even when relatively small.  In the example
above, the OOM killer will discount the 1M agetty's 256 badness points
down to 179, and the 10G fork bomb's 262144 points down to 183500 points
and make the right choice, instead of discounting both to 0 and killing
agetty because it's first in the task list.

Signed-off-by: David Rientjes <rientjes@google.com>
Reported-by: Johannes Weiner <hannes@cmpxchg.org>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.cz>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: franciscofranco <franciscofranco.1990@gmail.com>
Add Kconfig options for memory probe and remove

These are needed to support physical hotplug and hotremove on
ARM platforms which contain memory that may be powered on and off
(instead of physically inserting and removing the memory).

Change-Id: Id0e5442924612370cbc4067a964f82e7a8305b88
Signed-off-by: Larry Bassel <lbassel@codeaurora.org>
(cherry picked from commit d77db7e30a3b3e3d7eac98bcfd09919da9959827)

Conflicts:

	arch/arm/Kconfig
msm: 7x30: Enable SPARSEMEM, HOTPLUG, HOTREMOVE.

Enable Sparsemem, Hotplug, Hotremove and Migration
for 7x30.

Change-Id: I55b96a6513b1da61263ac2f7708bbb894a156c73
Signed-off-by: Karthik Parsha <kparsha@codeaurora.org>
(cherry picked from commit c1fbe4a987b6fccffe11d86ee00b25348be426f7)

Conflicts:

	arch/arm/Kconfig
	arch/arm/mach-msm/Kconfig
arm: add support for ARCH_POPULATES_NODE_MAP

ARCH_POPULATES_NODE_MAP is used by most of the other
architectures and allows finer-grained control of
how and where zones are placed.

Signed-off-by: Larry Bassel <lbassel@codeaurora.org>

Conflicts:

	arch/arm/mm/init.c
(cherry picked from commit d4e809ea8cca0ae779706dd17a2d36af26efadca)

Conflicts:

	arch/arm/Kconfig

Change-Id: I3dfc842731d3cb2c224ef6a2c4b72074b14f0384
mm: add HAVE_MEMBLOCK_NODE_MAP support

ARCH_POPULATES_NODE_MAP in 3.0 is replaced by HAVE_MEMBLOCK_NODE_MAP in
3.4.  add_active_range() is replaced with memblock_set_node().  They do
basically the same thing, but embedding nid field into memblock_region
is much cleaner than a separate early_node_map.

HAVE_MEMBLOCK_NODE_MAP is not selected by default.

See commit 4a2164a7dbf0d3b6a1c2ef6f20c0d54350491a12 for more info.

Change-Id: Icb44a8cea365b2d32df80628a57535a3d46fbd55
Signed-off-by: Junjie Wu <junjiew@codeaurora.org>
(cherry picked from commit 9a2068712f9d7c648aaf19ab720b713d7df6afa9)

Conflicts:

	arch/arm/Kconfig
arm: Add support for CONFIG_ENABLE_DMM

Add a new config ENABLE_DMM that will turn on/off the DMM feature.
DMM stands for "dynamic memory management" (the ability to power
off some memory when the system is idle) and the reason this
is needed is that many customers do not want this functionality
and we want to provide a consistent and simple way of turning
this off, rather than the ad-hoc methods some have been using.

DMM currently is enabled by default on 8960.

The following configs are currently used only by DMM
and will be disabled if ENABLE_DMM is not true.

CONFIG_MEMORY_HOTPLUG
CONFIG_HOTREMOVE
CONFIG_ARCH_ENABLE_MEMORY_HOTPLUG
CONFIG_ARCH_ENABLE_MEMORY_HOTREMOVE
CONFIG_MIGRATION
CONFIG_ARCH_MEMORY_PROBE
CONFIG_ARCH_MEMORY_REMOVE

CRs-Fixed: 344176
Change-Id: I1f1fa8a8882136a38f0abd01b0f5cb59c9a25152
Signed-off-by: Jack Cheung <jackc@codeaurora.org>
Signed-off-by: Larry Bassel <lbassel@codeaurora.org>
(cherry picked from commit b8d6102e021946973b20f4c2646e24c256eaab32)

Conflicts:

	arch/arm/Kconfig
	arch/arm/mach-msm/Kconfig
	arch/arm/mach-msm/board-8930.c
	arch/arm/mach-msm/board-8960.c
	arch/arm/mach-msm/memory_topology.c
arm: add CONFIG_FIX_MOVABLE_ZONE

This config option signifies that the start and size
of the movable zone has been calculated and it
must be placed precisely there.

Change-Id: Ibecda03ca69282c4efc84c642885dd47d1080ca2
Signed-off-by: Larry Bassel <lbassel@codeaurora.org>
(cherry picked from commit 6eae21afbeffe9d820bd5de47884db1e3d940b4d)

Conflicts:

	arch/arm/Kconfig
msm: fix circular dependency warning when ENABLE_DMM is set

When ENABLE_DMM was set, it caused a warning about
a circular dependency with MEMORY_HOTPLUG to be emitted.

Fix this by making all DMM config options depend on ENABLE_DMM (only)
and not having ENABLE_DMM depend on anything.

Change-Id: I1c0faf92163d49dca38fb2895be68eac21e7d38a
Signed-off-by: Larry Bassel <lbassel@codeaurora.org>
(cherry picked from commit e509155d9bf38f5225f2ded0485efddaf671d4e1)

Conflicts:

	arch/arm/Kconfig
arm: add support for DONT_MAP_HOLE_AFTER_MEMBANK0

Some platforms have memory at the top of the
first memory bank which the kernel cannot
access. Mapping this is unnecessary and wastes
precious virtual space.

Change-Id: I20be40a17c5d80d889b0903f11fa0fe1b3190e7a
Signed-off-by: Larry Bassel <lbassel@codeaurora.org>
(cherry picked from commit fe101112b8354688c509ce7474ea88ff6ed86bef)

Conflicts:

	arch/arm/mach-msm/include/mach/memory.h
ARM: allow memory hotplug/hotremove

Add ARM to the supported list of architectures for MEMORY_HOTPLUG.  For
ARM, the selection of MEMORY_HOTPLUG/REMOVE is specific to the sub-arch
and has to be explicitly enabled by the sub-arch.

Signed-off-by: Bryan Huntsman <bryanh@codeaurora.org>
(cherry picked from commit 45f580d9c36b93204882dffc6fb9f4a254c3d34a)
memblock: Add memblock_overlaps_memory()

Add a new function, memblock_overlaps_memory(), to check if a
region overlaps with a memory bank. This will be used by
peripheral loader code to detect when kernel memory would be
overwritten.

Change-Id: I851f8f416a0f36e85c0e19536b5209f7d4bd431c
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
(cherry picked from commit 1aa4e5a974b3087d29510802810170c071df8546)

Conflicts:

	include/linux/memblock.h
mm: Cast lowmem_reserve to long

z->lowmem_reserve[classzone_idx] is an unsigned long but
free_pages and min are longs. If free_pages is
negative, the function will incorrectly return true
because it will treat the negative long as a large,
positive unsigned long.

This change casts z->lowmem_reserve to a long and
fixes a typo in the comment.

Change-Id: Icada1fa5ca650fbcdb0656f637adbb98f223eec5
Signed-off-by: Jack Cheung <jackc@codeaurora.org>
(cherry picked from commit 9f41da81017657a194a4e145bab337f13a4d7fd9)
mm: Add total_unmovable_pages global variable

Vmalloc will exit if the amount it needs to allocate is
greater than totalram_pages. Vmalloc cannot allocate
from the movable zone, so pages in the movable zone should
not be counted.

This change adds a new global variable: total_unmovable_pages.
It is calculated in init.c, based on totalram_pages minus
the pages in the movable zone. Vmalloc now looks at this new
global instead of totalram_pages.

total_unmovable_pages can be modified during memory_hotplug.
If the zone you are offlining/onlining is unmovable, then
you modify it similar to totalram_pages.  If the zone is
movable, then no change is needed.

Change-Id: Ie55c41051e9ad4b921eb04ecbb4798a8bd2344d6
Signed-off-by: Jack Cheung <jackc@codeaurora.org>
(cherry picked from commit 59f9f1c9ae463a3d4499cd9353619f8b1993371b)

Conflicts:

	arch/arm/mm/init.c
	mm/memory_hotplug.c
	mm/page_alloc.c
	mm/vmalloc.c
mm: use required fixed size of movable zone if FIX_MOVABLE_ZONE

If FIX_MOVABLE_ZONE is enabled, we want a specific size and
location of ZONE_MOVABLE.

Change-Id: I0b858c7310cd328e1118abc9d5fe6f364bb4ffad
Signed-off-by: Larry Bassel <lbassel@codeaurora.org>
(cherry picked from commit e6436864f939e77226c8e971610539649ed5f869)
mm: sync vmalloc address space page tables in alloc_vm_area()

commit 461ae488ecb125b140d7ea29ceeedbcce9327003 upstream.

Xen backend drivers (e.g., blkback and netback) would sometimes fail to
map grant pages into the vmalloc address space allocated with
alloc_vm_area().  The GNTTABOP_map_grant_ref would fail because Xen could
not find the page (in the L2 table) containing the PTEs it needed to
update.

(XEN) mm.c:3846:d0 Could not find L1 PTE for address fbb42000

netback and blkback were making the hypercall from a kernel thread where
task->active_mm != &init_mm and alloc_vm_area() was only updating the page
tables for init_mm.  The usual method of deferring the update to the page
tables of other processes (i.e., after taking a fault) doesn't work as a
fault cannot occur during the hypercall.

This would work on some systems depending on what else was using vmalloc.

Fix this by reverting ef691947d8a3 ("vmalloc: remove vmalloc_sync_all()
from alloc_vm_area()") and add a comment to explain why it's needed.

Signed-off-by: David Vrabel <david.vrabel@citrix.com>
Cc: Jeremy Fitzhardinge <jeremy.fitzhardinge@citrix.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Ian Campbell <Ian.Campbell@citrix.com>
Cc: Keir Fraser <keir.xen@gmail.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>

(cherry picked from commit d63c8a029e509ad48ee9290874731789f9008537)
mm: Make memory hotplug aware of memmap holes

This patch prevents memory hotplug from marking pages of the memmap that
only reference holes in the physical address space as private. Some
architectures (including ARM) attempt to free these unneeded parts of the
memmap, and attempting to free a private page will throw bad_page warnings
and tie up the memory indefinitely.

This patch also allows early_pfn_valid to be architecture specific and
defines it for ARM. The definition for ARM takes into account memory banks
and the holes in physical memory.

CRs-Fixed: 247010

Change-Id: Iad88d427b1b923a808b026c22d2899fa0483cb9e
Signed-off-by: jesset@codeaurora.org
(cherry picked from commit 0b610c773ad6281a3d217fbbe894b2476e9e71dd)

Conflicts:

	arch/arm/mm/init.c
mm: Drain pages after onlining

When onlining, the onlined pages must be added to the kernel's
list of free pages using __free_page(). However, pages are not
immediately added but placed in a queue to be processed
when the queue size reaches a watermark. The last pages in
the queue may not be processed in time, and if you try to
offline that memory before it is processed, offlining will
always fail.

This fix calls drain_all_pages(), which will process every
free page in the queue. This ensures that all pages are
accounted for when onlining and nothing gets stuck in the queue.

Change-Id: I54dbc0749556702407090e51ce9246abc5db7d1c
Signed-off-by: Jack Cheung <jackc@codeaurora.org>
(cherry picked from commit aa7e9dec5cfd309cb9eb6cb56a284a61607a925a)
mm: Fix infinite loop when offlining

When page migration is unable to find free memory it
will go into an infinite loop because the
all_unreclaimable flag is never set.

This change will allow memory offlining to fail
gracefully if there is not enough memory for page
migration.

__GFP_NORETRY tells the page_alloc to not retry.
__GFP_NOWARN suppresses page fault warnings when
page allocation fails.
__GFP_NOMEMALLOC prevents it from aggressively
allocating beyond zone watermarks.

Change-Id: I94dfd9059851c7b24953f44a4018a3bbac840688
Signed-off-by: Jack Cheung <jackc@codeaurora.org>
(cherry picked from commit 1e6ec3c7e399aa84d29ced48b16a91acaba962f0)
mm: Fix zone->present_pages underflow

If offlined_pages is greater than
zone->present_pages, underflow will occur.

This change will set zone->present_pages to 0 if
offlined_pages is greater.

Change-Id: I728e90c60fb7fc391de7b9c4828ab264ca38653b
Signed-off-by: Jack Cheung <jackc@codeaurora.org>
(cherry picked from commit 80c201e25e8dbc00427b73d90b1527c356526442)