Allow setting a minimum scheduler policy for a service.

The binder kernel driver supports priority inheritance
at a node (service)-basis, that makes sure all transactions
into that service are executed at a specified minimum
scheduler policy and priority.

This change allows users of HIDL interfaces to set
such a policy for their interface.

Bug: 37293077
Bug: 63899698
Test: verified min_prio in /d/binder output
Merged-In: If72dd8322381246832b460c386dda44fbd225757
Change-Id: If72dd8322381246832b460c386dda44fbd225757
diff --git a/transport/HidlTransportSupport.cpp b/transport/HidlTransportSupport.cpp
index a5ec8e2..ea2e32c 100644
--- a/transport/HidlTransportSupport.cpp
+++ b/transport/HidlTransportSupport.cpp
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 #include <hidl/HidlTransportSupport.h>
-
 #include <hidl/HidlBinderSupport.h>
 
 namespace android {
@@ -29,5 +28,30 @@
     joinBinderRpcThreadpool();
 }
 
+bool setMinSchedulerPolicy(const sp<::android::hidl::base::V1_0::IBase>& service,
+                           int policy, int priority) {
+    if (service->isRemote()) {
+        ALOGE("Can't set scheduler policy on remote service.");
+        return false;
+    }
+
+    if (policy != SCHED_NORMAL && policy != SCHED_FIFO && policy != SCHED_RR) {
+        ALOGE("Invalid scheduler policy %d", policy);
+        return false;
+    }
+
+    if (policy == SCHED_NORMAL && (priority < -20 || priority > 19)) {
+        ALOGE("Invalid priority for SCHED_NORMAL: %d", priority);
+        return false;
+    } else if (priority < 1 || priority > 99) {
+        ALOGE("Invalid priority for real-time policy: %d", priority);
+        return false;
+    }
+
+    details::gServicePrioMap.set(service, { policy, priority });
+
+    return true;
 }
-}
\ No newline at end of file
+
+}
+}
diff --git a/transport/Static.cpp b/transport/Static.cpp
index 496c8f0..18cb475 100644
--- a/transport/Static.cpp
+++ b/transport/Static.cpp
@@ -32,6 +32,8 @@
 ConcurrentMap<std::string, std::function<sp<IBinder>(void *)>>
         gBnConstructorMap{};
 
+ConcurrentMap<wp<::android::hidl::base::V1_0::IBase>, SchedPrio> gServicePrioMap{};
+
 ConcurrentMap<std::string, std::function<sp<::android::hidl::base::V1_0::IBase>(void *)>>
         gBsConstructorMap;
 
diff --git a/transport/include/hidl/HidlTransportSupport.h b/transport/include/hidl/HidlTransportSupport.h
index 3cac1e9..0c174f7 100644
--- a/transport/include/hidl/HidlTransportSupport.h
+++ b/transport/include/hidl/HidlTransportSupport.h
@@ -17,6 +17,7 @@
 #ifndef ANDROID_HIDL_TRANSPORT_SUPPORT_H
 #define ANDROID_HIDL_TRANSPORT_SUPPORT_H
 
+#include <android/hidl/base/1.0/IBase.h>
 #include <hidl/HidlBinderSupport.h>
 #include <hidl/HidlSupport.h>
 #include <hidl/HidlTransportUtils.h>
@@ -47,6 +48,20 @@
  */
 void joinRpcThreadpool();
 
+/**
+ * Sets a minimum scheduler policy for all transactions coming into this
+ * service.
+ *
+ * This method MUST be called before passing this service to another process
+ * and/or registering it with registerAsService().
+ *
+ * @param service the service to set the policy for
+ * @param policy scheduler policy as defined in linux UAPI
+ * @param priority priority. [-20..19] for SCHED_NORMAL, [1..99] for RT
+ */
+bool setMinSchedulerPolicy(const sp<::android::hidl::base::V1_0::IBase>& service,
+                           int policy, int priority);
+
 namespace details {
 
 // cast the interface IParent to IChild.
diff --git a/transport/include/hidl/Static.h b/transport/include/hidl/Static.h
index e6c9139..0133ff7 100644
--- a/transport/include/hidl/Static.h
+++ b/transport/include/hidl/Static.h
@@ -28,12 +28,19 @@
 namespace hardware {
 namespace details {
 
+struct SchedPrio {
+    int sched_policy;
+    int prio;
+};
+
 // For HidlBinderSupport and autogenerated code
 // value function receives reinterpret_cast<void *>(static_cast<IFoo *>(foo)),
 // returns sp<IBinder>
 extern ConcurrentMap<std::string,
         std::function<sp<IBinder>(void *)>> gBnConstructorMap;
 
+extern ConcurrentMap<wp<::android::hidl::base::V1_0::IBase>, SchedPrio> gServicePrioMap;
+
 // For HidlPassthroughSupport and autogenerated code
 // value function receives reinterpret_cast<void *>(static_cast<IFoo *>(foo)),
 // returns sp<IBase>