Merge cherrypicks of ['android-review.googlesource.com/3161516', 'android-review.googlesource.com/3164298'] into 24Q3-release.

Change-Id: Ibc14ad8270eb2a152f9f43f891cdb509f5246891
diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java
index be1d3c7..5186203 100755
--- a/service/src/com/android/server/ConnectivityService.java
+++ b/service/src/com/android/server/ConnectivityService.java
@@ -1847,31 +1847,33 @@
         mHandler = new InternalHandler(mHandlerThread.getLooper());
         // Temporary hack to report netbpfload result.
         // TODO: remove in 2024-09 when netbpfload starts loading mainline bpf programs.
-        mHandler.postDelayed(() -> {
-            // Test Pitot pipeline, ignore this Log.wtf if it shows up in the logs.
-            final Random r = new Random();
-            if (Build.TYPE.equals("user") && r.nextInt(1000) == 0) {
-                Log.wtf(TAG, "NOT A FAILURE, PLEASE IGNORE! Test Pitot pipeline works correctly");
-            }
-            // Did netbpfload create the map?
-            try {
-                Os.access("/sys/fs/bpf/net_shared/map_gentle_test", F_OK);
-            } catch (ErrnoException e) {
-                Log.wtf(TAG, "netbpfload did not create map", e);
-            }
-            // Did netbpfload create the program?
-            try {
-                Os.access("/sys/fs/bpf/net_shared/prog_gentle_skfilter_accept", F_OK);
-            } catch (ErrnoException e) {
-                Log.wtf(TAG, "netbpfload did not create program", e);
-            }
-            // Did netbpfload run to completion?
-            try {
-                Os.access("/sys/fs/bpf/netd_shared/mainline_done", F_OK);
-            } catch (ErrnoException e) {
-                Log.wtf(TAG, "netbpfload did not run to completion", e);
-            }
-        }, 30_000 /* delayMillis */);
+        if (!mDeps.isAtLeastV()) {
+            mHandler.postDelayed(() -> {
+                // Test Log.wtf reporting pipeline. Ignore this Log.wtf if it shows up in the logs.
+                final Random r = new Random();
+                if (Build.TYPE.equals("user") && r.nextInt(1000) == 0) {
+                    Log.wtf(TAG, "NOT A FAILURE, PLEASE IGNORE! Ensure netbpfload result reported");
+                }
+                // Did netbpfload create the map?
+                try {
+                    Os.access("/sys/fs/bpf/net_shared/map_gentle_test", F_OK);
+                } catch (ErrnoException e) {
+                    Log.wtf(TAG, "netbpfload did not create map", e);
+                }
+                // Did netbpfload create the program?
+                try {
+                    Os.access("/sys/fs/bpf/net_shared/prog_gentle_skfilter_accept", F_OK);
+                } catch (ErrnoException e) {
+                    Log.wtf(TAG, "netbpfload did not create program", e);
+                }
+                // Did netbpfload run to completion?
+                try {
+                    Os.access("/sys/fs/bpf/netd_shared/mainline_done", F_OK);
+                } catch (ErrnoException e) {
+                    Log.wtf(TAG, "netbpfload did not run to completion", e);
+                }
+            }, 30_000 /* delayMillis */);
+        }
         mTrackerHandler = new NetworkStateTrackerHandler(mHandlerThread.getLooper());
         mConnectivityDiagnosticsHandler =
                 new ConnectivityDiagnosticsHandler(mHandlerThread.getLooper());
diff --git a/staticlibs/device/com/android/net/module/util/SingleWriterBpfMap.java b/staticlibs/device/com/android/net/module/util/SingleWriterBpfMap.java
index cd6bfec..a638cc4 100644
--- a/staticlibs/device/com/android/net/module/util/SingleWriterBpfMap.java
+++ b/staticlibs/device/com/android/net/module/util/SingleWriterBpfMap.java
@@ -19,6 +19,7 @@
 import android.system.ErrnoException;
 import android.util.Pair;
 
+import androidx.annotation.GuardedBy;
 import androidx.annotation.NonNull;
 import androidx.annotation.RequiresApi;
 
@@ -60,6 +61,7 @@
 public class SingleWriterBpfMap<K extends Struct, V extends Struct> extends BpfMap<K, V> {
     // HashMap instead of ArrayMap because it performs better on larger maps, and many maps used in
     // our code can contain hundreds of items.
+    @GuardedBy("this")
     private final HashMap<K, V> mCache = new HashMap<>();
 
     // This should only ever be called (hence private) once for a given 'path'.
@@ -72,10 +74,12 @@
         super(path, BPF_F_RDWR_EXCLUSIVE, key, value);
 
         // Populate cache with the current map contents.
-        K currentKey = super.getFirstKey();
-        while (currentKey != null) {
-            mCache.put(currentKey, super.getValue(currentKey));
-            currentKey = super.getNextKey(currentKey);
+        synchronized (this) {
+            K currentKey = super.getFirstKey();
+            while (currentKey != null) {
+                mCache.put(currentKey, super.getValue(currentKey));
+                currentKey = super.getNextKey(currentKey);
+            }
         }
     }