Merge "adb: Use new libusbhost library for USB support on Linux host."
diff --git a/libcutils/sched_policy.c b/libcutils/sched_policy.c
index 6d2727b..f9c111e 100644
--- a/libcutils/sched_policy.c
+++ b/libcutils/sched_policy.c
@@ -27,8 +27,10 @@
 #include "cutils/log.h"
 
 #ifdef HAVE_SCHED_H
+#ifdef HAVE_PTHREADS
 
 #include <sched.h>
+#include <pthread.h>
 
 #include <cutils/sched_policy.h>
 
@@ -42,15 +44,28 @@
 
 #define POLICY_DEBUG 0
 
+static pthread_once_t the_once = PTHREAD_ONCE_INIT;
+
 static int __sys_supports_schedgroups = -1;
 
-/* Add tid to the group defined by dev_path ("/dev/cpuctl/.../tasks") */
-static int add_tid_to_cgroup(int tid, const char *dev_path)
+// File descriptors open to /dev/cpuctl/../tasks, setup by initialize, or -1 on error.
+static int normal_cgroup_fd = -1;
+static int bg_cgroup_fd = -1;
+
+/* Add tid to the scheduling group defined by the policy */
+static int add_tid_to_cgroup(int tid, SchedPolicy policy)
 {
     int fd;
-    if ((fd = open(dev_path, O_WRONLY)) < 0) {
-        SLOGE("add_tid_to_cgroup failed to open '%s' (%s)\n", dev_path,
-             strerror(errno));
+
+    if (policy == SP_BACKGROUND) {
+        fd = bg_cgroup_fd;
+    } else {
+        fd = normal_cgroup_fd;
+    }
+
+    if (fd < 0) {
+        SLOGE("add_tid_to_cgroup failed; background=%d\n",
+              policy == SP_BACKGROUND ? 1 : 0);
         return -1;
     }
 
@@ -65,30 +80,38 @@
     }
 
     if (write(fd, ptr, end - ptr) < 0) {
-        close(fd);
-	/*
-	 * If the thread is in the process of exiting,
-	 * don't flag an error
-	 */
-	if (errno == ESRCH)
-		return 0;
-        SLOGW("add_tid_to_cgroup failed to write '%s' to '%s' (%s)\n",
-             ptr, dev_path, strerror(errno));
+        /*
+         * If the thread is in the process of exiting,
+         * don't flag an error
+         */
+        if (errno == ESRCH)
+                return 0;
+        SLOGW("add_tid_to_cgroup failed to write '%s' (%s); background=%d\n",
+              ptr, strerror(errno), policy == SP_BACKGROUND ? 1 : 0);
         return -1;
     }
 
-    close(fd);
     return 0;
 }
 
-static inline void initialize()
-{
-    if (__sys_supports_schedgroups < 0) {
-        if (!access("/dev/cpuctl/tasks", F_OK)) {
-            __sys_supports_schedgroups = 1;
-        } else {
-            __sys_supports_schedgroups = 0;
+static void __initialize(void) {
+    char* filename;
+    if (!access("/dev/cpuctl/tasks", F_OK)) {
+        __sys_supports_schedgroups = 1;
+
+        filename = "/dev/cpuctl/tasks";
+        normal_cgroup_fd = open(filename, O_WRONLY);
+        if (normal_cgroup_fd < 0) {
+            SLOGE("open of %s failed: %s\n", filename, strerror(errno));
         }
+
+        filename = "/dev/cpuctl/bg_non_interactive/tasks";
+        bg_cgroup_fd = open(filename, O_WRONLY);
+        if (bg_cgroup_fd < 0) {
+            SLOGE("open of %s failed: %s\n", filename, strerror(errno));
+        }
+    } else {
+        __sys_supports_schedgroups = 0;
     }
 }
 
@@ -166,7 +189,7 @@
 
 int get_sched_policy(int tid, SchedPolicy *policy)
 {
-    initialize();
+    pthread_once(&the_once, __initialize);
 
     if (__sys_supports_schedgroups) {
         char grpBuf[32];
@@ -198,7 +221,7 @@
 
 int set_sched_policy(int tid, SchedPolicy policy)
 {
-    initialize();
+    pthread_once(&the_once, __initialize);
 
 #if POLICY_DEBUG
     char statfile[64];
@@ -233,14 +256,7 @@
 #endif
 
     if (__sys_supports_schedgroups) {
-        const char *dev_path;
-        if (policy == SP_BACKGROUND) {
-            dev_path = "/dev/cpuctl/bg_non_interactive/tasks";
-        } else {
-            dev_path = "/dev/cpuctl/tasks";
-        }
-
-        if (add_tid_to_cgroup(tid, dev_path)) {
+        if (add_tid_to_cgroup(tid, policy)) {
             if (errno != ESRCH && errno != ENOENT)
                 return -errno;
         }
@@ -257,4 +273,5 @@
     return 0;
 }
 
+#endif /* HAVE_PTHREADS */
 #endif /* HAVE_SCHED_H */
diff --git a/libpixelflinger/codeflinger/CodeCache.cpp b/libpixelflinger/codeflinger/CodeCache.cpp
index 29410c8..5877ff4 100644
--- a/libpixelflinger/codeflinger/CodeCache.cpp
+++ b/libpixelflinger/codeflinger/CodeCache.cpp
@@ -19,6 +19,8 @@
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <sys/mman.h>
 
 #include <cutils/log.h>
 #include <cutils/atomic.h>
@@ -39,15 +41,14 @@
 Assembly::Assembly(size_t size)
     : mCount(1), mSize(0)
 {
-    mBase = (uint32_t*)malloc(size);
-    if (mBase) {
-        mSize = size;
-    }
+    mBase = (uint32_t*)mspace_malloc(getMspace(), size);
+    mSize = size;
+    ensureMbaseExecutable();
 }
 
 Assembly::~Assembly()
 {
-    free(mBase);
+    mspace_free(getMspace(), mBase);
 }
 
 void Assembly::incStrong(const void*) const
@@ -75,11 +76,32 @@
 
 ssize_t Assembly::resize(size_t newSize)
 {
-    mBase = (uint32_t*)realloc(mBase, newSize);
+    mBase = (uint32_t*)mspace_realloc(getMspace(), mBase, newSize);
     mSize = newSize;
+    ensureMbaseExecutable();
     return size();
 }
 
+mspace Assembly::getMspace()
+{
+    static mspace msp = create_contiguous_mspace(2 * 1024, 1024 * 1024, /*locked=*/ false);
+    return msp;
+}
+
+void Assembly::ensureMbaseExecutable()
+{
+    long pagesize = sysconf(_SC_PAGESIZE);
+    long pagemask = ~(pagesize - 1);  // assumes pagesize is a power of 2
+
+    uint32_t* pageStart = (uint32_t*) (((uintptr_t) mBase) & pagemask);
+    size_t adjustedLength = mBase - pageStart + mSize;
+
+    if (mBase && mprotect(pageStart, adjustedLength, PROT_READ | PROT_WRITE | PROT_EXEC) != 0) {
+        mspace_free(getMspace(), mBase);
+        mBase = NULL;
+    }
+}
+
 // ----------------------------------------------------------------------------
 
 CodeCache::CodeCache(size_t size)
diff --git a/libpixelflinger/codeflinger/CodeCache.h b/libpixelflinger/codeflinger/CodeCache.h
index 8ff1366..aaafd26 100644
--- a/libpixelflinger/codeflinger/CodeCache.h
+++ b/libpixelflinger/codeflinger/CodeCache.h
@@ -22,6 +22,7 @@
 #include <stdint.h>
 #include <pthread.h>
 #include <sys/types.h>
+#include <cutils/mspace.h>
 
 #include "tinyutils/KeyedVector.h"
 #include "tinyutils/smartpointer.h"
@@ -67,9 +68,12 @@
     typedef void    weakref_type;
 
 private:
+    static  mspace  getMspace();
+            void    ensureMbaseExecutable();
+
     mutable int32_t     mCount;
             uint32_t*   mBase;
-            ssize_t     mSize;
+            size_t      mSize;
 };
 
 // ----------------------------------------------------------------------------