Snap for 6259218 from f18803427541f34082fd659a4dd0deb49a8a4b89 to sdk-release

Change-Id: Ic63e04f7968576840eced89c84dead37537cc1a4
diff --git a/time_in_state.c b/time_in_state.c
index 0b5c4b2..5befc6d 100644
--- a/time_in_state.c
+++ b/time_in_state.c
@@ -17,19 +17,20 @@
 #include <bpf_helpers.h>
 #include <bpf_timeinstate.h>
 
-DEFINE_BPF_MAP(uid_time_in_state_map, PERCPU_HASH, time_key_t, tis_val_t, 1024)
+DEFINE_BPF_MAP_GRW(uid_time_in_state_map, PERCPU_HASH, time_key_t, tis_val_t, 1024, AID_SYSTEM)
 
-DEFINE_BPF_MAP(uid_concurrent_times_map, PERCPU_HASH, time_key_t, concurrent_val_t, 1024)
+DEFINE_BPF_MAP_GRW(uid_concurrent_times_map, PERCPU_HASH, time_key_t, concurrent_val_t, 1024, AID_SYSTEM)
+DEFINE_BPF_MAP_GRW(uid_last_update_map, HASH, uint32_t, uint64_t, 1024, AID_SYSTEM)
 
-DEFINE_BPF_MAP(cpu_last_update_map, PERCPU_ARRAY, uint32_t, uint64_t, 1)
+DEFINE_BPF_MAP_GWO(cpu_last_update_map, PERCPU_ARRAY, uint32_t, uint64_t, 1, AID_SYSTEM)
 
-DEFINE_BPF_MAP(cpu_policy_map, ARRAY, uint32_t, uint32_t, 1024)
-DEFINE_BPF_MAP(policy_freq_idx_map, ARRAY, uint32_t, uint8_t, 1024)
+DEFINE_BPF_MAP_GWO(cpu_policy_map, ARRAY, uint32_t, uint32_t, 1024, AID_SYSTEM)
+DEFINE_BPF_MAP_GWO(policy_freq_idx_map, ARRAY, uint32_t, uint8_t, 1024, AID_SYSTEM)
 
-DEFINE_BPF_MAP(freq_to_idx_map, HASH, freq_idx_key_t, uint8_t, 2048)
+DEFINE_BPF_MAP_GWO(freq_to_idx_map, HASH, freq_idx_key_t, uint8_t, 2048, AID_SYSTEM)
 
-DEFINE_BPF_MAP(nr_active_map, ARRAY, uint32_t, uint32_t, 1)
-DEFINE_BPF_MAP(policy_nr_active_map, ARRAY, uint32_t, uint32_t, 1024)
+DEFINE_BPF_MAP_GWO(nr_active_map, ARRAY, uint32_t, uint32_t, 1, AID_SYSTEM)
+DEFINE_BPF_MAP_GWO(policy_nr_active_map, ARRAY, uint32_t, uint32_t, 1024, AID_SYSTEM)
 
 struct switch_args {
     unsigned long long ignore;
@@ -42,25 +43,26 @@
     int next_prio;
 };
 
-SEC("tracepoint/sched/sched_switch")
-int tp_sched_switch(struct switch_args* args) {
+DEFINE_BPF_PROG("tracepoint/sched/sched_switch", AID_ROOT, AID_SYSTEM, tp_sched_switch)
+(struct switch_args* args) {
+    const int ALLOW = 1;  // return 1 to avoid blocking simpleperf from receiving events.
     uint32_t zero = 0;
     uint64_t* last = bpf_cpu_last_update_map_lookup_elem(&zero);
-    if (!last) return 0;
+    if (!last) return ALLOW;
     uint64_t old_last = *last;
     uint64_t time = bpf_ktime_get_ns();
     *last = time;
 
     uint32_t* active = bpf_nr_active_map_lookup_elem(&zero);
-    if (!active) return 0;
+    if (!active) return ALLOW;
 
     uint32_t cpu = bpf_get_smp_processor_id();
     uint32_t* policyp = bpf_cpu_policy_map_lookup_elem(&cpu);
-    if (!policyp) return 0;
+    if (!policyp) return ALLOW;
     uint32_t policy = *policyp;
 
     uint32_t* policy_active = bpf_policy_nr_active_map_lookup_elem(&policy);
-    if (!policy_active) return 0;
+    if (!policy_active) return ALLOW;
 
     uint32_t nactive = *active - 1;
     uint32_t policy_nactive = *policy_active - 1;
@@ -76,7 +78,7 @@
     // 2) old_last == 0, so this is the first time we've seen this CPU. Any delta will be invalid,
     //    and our active CPU counts don't include this CPU yet so we shouldn't decrement them even
     //    if we're going idle.
-    if (!args->prev_pid || !old_last) return 0;
+    if (!args->prev_pid || !old_last) return ALLOW;
 
     if (!args->next_pid) {
         __sync_fetch_and_add(active, -1);
@@ -84,7 +86,7 @@
     }
 
     uint8_t* freq_idxp = bpf_policy_freq_idx_map_lookup_elem(&policy);
-    if (!freq_idxp || !*freq_idxp) return 0;
+    if (!freq_idxp || !*freq_idxp) return ALLOW;
     // freq_to_idx_map uses 1 as its minimum index so that *freq_idxp == 0 only when uninitialized
     uint8_t freq_idx = *freq_idxp - 1;
 
@@ -118,7 +120,13 @@
         }
     }
     if (ct) ct->policy[policy_nactive % CPUS_PER_ENTRY] += delta;
-    return 0;
+    uint64_t* uid_last_update = bpf_uid_last_update_map_lookup_elem(&uid);
+    if (uid_last_update) {
+        *uid_last_update = time;
+    } else {
+        bpf_uid_last_update_map_update_elem(&uid, &time, BPF_NOEXIST);
+    }
+    return ALLOW;
 }
 
 struct cpufreq_args {
@@ -127,8 +135,8 @@
     unsigned int cpu_id;
 };
 
-SEC("tracepoint/power/cpu_frequency")
-int tp_cpufreq(struct cpufreq_args* args) {
+DEFINE_BPF_PROG("tracepoint/power/cpu_frequency", AID_ROOT, AID_SYSTEM, tp_cpufreq)
+(struct cpufreq_args* args) {
     uint32_t cpu = args->cpu_id;
     unsigned int new = args->state;
     uint32_t* policyp = bpf_cpu_policy_map_lookup_elem(&cpu);